how to insert value in hex file using srec_cat - hex

I have a value say 0xAABBCCDD that I want to add to a hex file at a known address. I have not found an srec_cat option to do this yet, is this possible?

It is possible by combination of srec_cat commands.
Let in.hex be input file, out.hex be output file, 0x64 is the address to place 4-byte value so that the value will occupy addresses from 0x64 to 0x67 (0x68 is the first address after the value).
srec_cat in.hex -I -E 0x64 0x68 -GEN 0x64 0x68 -LECONST 0xAABBCCDD 4 -O out.hex -I
Step by step:
in.hex -I
read input file in.hex, format Intel hex
-E 0x64 0x68
apply filter Exclude—remove (if any) data in range 64 (including) — 0x68 (excluding)
-GEN 0x64 0x68
GENerate content for address range 0x64 (including) — 0x68 (excluding)
-LECONST 0xAABBCCDD 4
the content is Little Endian CONSTant 0xAABBCCDD with width 4 bytes.
-O out.hex -I
write Out result data.
p.s. See srec_cat and srec_input (for filter description)

Related

FFmpeg: how to stream multiple http outputs

I'm assigned to search for a solution to stream more than one http output in ffmpeg. I tried the code below which uses pipe, but only the last output works, other ones don't work. The solutions I tried with tee muxer also couldn't work. Any idea how can I do that?
ffmpeg -re -i sample1.mp3 -i sample2.mp3 -filter_complex amix=inputs=2 -f mp3 pipe:1 | ffmpeg -f mp3 -re -i pipe:0 -c copy -listen 1 -f mp3 http://127.0.0.1:4000 -c copy -listen 1 -f mp3 http://127.0.0.1:4010
The code I tried with tee muxer is below:
ffmpeg -re -i sample1.mp3 -i sample2.mp3 -filter_complex amix=inputs=2 -c:a mp3 -f tee "[listen=1:f=mp3]http://127.0.0.1:4000|[listen=1:f=mp3]http://127.0.0.1:4010"
edit: shared the log with code.
ffmpeg -loglevel debug -hide_banner -re -i sample1.mp3 -filter_complex asplit=2 -c:a mp3 -listen 1 -f mp3 http://127.0.0.1:4000 -c:a mp3 -listen 1 -f mp3 http://127.0.0.1:4010
Splitting the commandline.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'.
Reading option '-hide_banner' ... matched as option 'hide_banner' (do not show program banner) with argument '1'.
Reading option '-re' ... matched as option 're' (read input at native frame rate; equivalent to -readrate 1) with argument '1'.
Reading option '-i' ... matched as input url with argument 'sample1.mp3'.
Reading option '-filter_complex' ... matched as option 'filter_complex' (create a complex filtergraph) with argument 'asplit=2'.
Reading option '-c:a' ... matched as option 'c' (codec name) with argument 'mp3'.
Reading option '-listen' ... matched as AVOption 'listen' with argument '1'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'mp3'.
Reading option 'http://127.0.0.1:4000' ... matched as output url.
Reading option '-c:a' ... matched as option 'c' (codec name) with argument 'mp3'.
Reading option '-listen' ... matched as AVOption 'listen' with argument '1'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'mp3'.
Reading option 'http://127.0.0.1:4010' ... matched as output url.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option loglevel (set logging level) with argument debug.
Applying option hide_banner (do not show program banner) with argument 1.
Applying option filter_complex (create a complex filtergraph) with argument asplit=2.
Successfully parsed a group of options.
Parsing a group of options: input url sample1.mp3.
Applying option re (read input at native frame rate; equivalent to -readrate 1) with argument 1.
Successfully parsed a group of options.
Opening an input file: sample1.mp3.
[NULL # 00000216ab345b80] Opening 'sample1.mp3' for reading
[file # 00000216ab346180] Setting default whitelist 'file,crypto,data'
[mp3 # 00000216ab345b80] Format mp3 probed with size=4096 and score=51
id3v2 ver:4 flags:00 len:134
[mp3 # 00000216ab345b80] pad 576 576
[mp3 # 00000216ab345b80] Skipping 0 bytes of junk at 561.
[mp3 # 00000216ab345b80] Before avformat_find_stream_info() pos: 561 bytes read:32768 seeks:0 nb_streams:1
[mp3 # 00000216ab345b80] demuxer injecting skip 1105 / discard 0
[mp3float # 00000216ab34f3c0] skip 1105 / discard 0 samples due to side data
[mp3float # 00000216ab34f3c0] skip 1105/1152 samples
[mp3 # 00000216ab345b80] All info found
[mp3 # 00000216ab345b80] After avformat_find_stream_info() pos: 22065 bytes read:32768 seeks:0 frames:50
Input #0, mp3, from 'sample1.mp3':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2mp41
encoder : Lavf58.45.100
Duration: 00:12:58.48, start: 0.025057, bitrate: 128 kb/s
Stream #0:0, 50, 1/14112000: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc58.91
Successfully opened the file.
[Parsed_asplit_0 # 00000216ab34fc80] Setting 'outputs' to value '2'
Parsing a group of options: output url http://127.0.0.1:4000.
Applying option c:a (codec name) with argument mp3.
Applying option f (force format) with argument mp3.
Successfully parsed a group of options.
Opening an output file: http://127.0.0.1:4000.
Matched encoder 'libmp3lame' for codec 'mp3'.
Last message repeated 1 times
[http # 00000216ab44d980] Setting default whitelist 'http,https,tls,rtp,tcp,udp,crypto,httpproxy,data'
FFmpeg can output multiple outputs by default (w/out using the tee muxer):
ffmpeg -re -i sample1.mp3 -i sample2.mp3 \
-filter_complex amix=inputs=2,asplit \
-c:a mp3 -listen 1 f=mp3 http://127.0.0.1:4000 \
-c:a mp3 -listen 1 f=mp3 http://127.0.0.1:4010
Because you need to route the filtergraph output to 2 streams, you need to add asplit filter to your graph, and all you need to do is to have 2 output option sequences in series.
edited: fixed -listen option as pointed out by op

How can I use hex to go back and create a file

I have some hex, and I want to recreate the file. I don't know what the file format is. It starts with "6554 6c6c 6d20" so on and so forth. Unfortunately I'm unable to give you the hex, so I would appreciate it if you could give me some ideas as to where to go on from here.
Also when I converted each value from hex to ASCII, it was clearly garbage..
Thanks in Advance.
R.
Magic Numbers
You are describing the magic number, which is a series of bytes that appears at the beginning of a file that uniquely identifies it. Gary Kessler maintains a curated list of these file signatures. On *nix systems, you can use file to identify files.
Recreating Files from Hex
Let's get some sample hex by echoing to xxd.
bash-5.0$ hexstring=$(printf 'Hello World!' | xxd -g 2 | cut -c11-49)
bash-5.0$ echo $hexstring
4865 6c6c 6f20 576f 726c 6421
To convert back, use xxd -p -r:
bash-5.0$ echo $hexstring | xxd -p -r
Hello World!
Here, -p prints the result and -r does the reverse of what xxd normally does.

IMFSourceReaderCallback::OnReadSample Function

To read samples from your webcam and write them to file you can implement IMFSourceReaderCallback and the call back function OnReadSample will send IMFSample *pSample with some others parameters like the timestamp you can use them with ISinkWriter to write them on file(You can check MSDN example MFCaptureToFile Sample )
STDMETHODIMP OnReadSample(
HRESULT hrStatus,
DWORD dwStreamIndex,
DWORD dwStreamFlags,
LONGLONG llTimestamp,
IMFSample *pSample
);
I have set my encoded parameters to H264 .I convert pSample to rawdata(set of bytes) and removed ISinkWriter.
I have a tricky question *"is IMFSample pSample encoded or they become encoded inside ISinkWriter as when I convert them to RawData I got a huge file so are they encoded?" One more question how can I test them after writing them to file(they are in a raw format)
The sample will be in a raw format such as YUV or RGB. You can't set the output media type to H264 on a video device source reader (unless that device happens to natively support H264). When you introduce the SinkWriter with an .mp4 file as the destination the Media Foundation will take care of resolving the topology which in this case would mean including the H264 Media Foundation Transform.
One way I use to test the raw output from a SourceReader is to specify an output format of MFVideoFormat_IYUV and then after writing to a file stream use ffmpeg to extract a jpeg or convert it to a avi video file.
std::ofstream outputBuffer("rawframes.yuv", std::ios::out | std::ios::binary);
IMFMediaBuffer *buf = NULL;
DWORD bufLength;
pSample->ConvertToContiguousBuffer(&buf);
buf->GetCurrentLength(&bufLength);
printf("Writing sample %i, sample time %i, sample duration %i, sample size .\n", sampleCount, llVideoTimeStamp, llSampleDuration);
byte *byteBuffer;
DWORD buffCurrLen = 0;
DWORD buffMaxLen = 0;
buf->Lock(&byteBuffer, &buffMaxLen, &buffCurrLen);
outputBuffer.write((char *)byteBuffer, bufLength);
outputBuffer.flush();
And then the ffmpeg commands:
ffmpeg -vcodec rawvideo -s 640x480 -pix_fmt yuv420p -i rawframes.yuv -vframes 1 output.jpeg
ffmpeg -vcodec rawvideo -s 640x480 -pix_fmt yuv420p -i rawframes.yuv out.avi
Sample console app that uses the above snippet available on github.

HEX 00 value between characters

I have a file here that contais some texts, and I want edit them.
But between the characters have a decimal value 00. If I remove it, gives erros in the file and nothing appears in the program! But if I edit keeping the 00 values ​​between the letters, it works.
Have a program that "hide" these values? By this mode, it is very difficult for me to edit so many letters one by one in a file of 13 MB! Here goes a print:
http://img211.imageshack.us/img211/2286/fsfsz.png
What can I do?
Thanks all in advance!
Your file looks like an UTF-16 text file, it means each character is coded in 16 bits instead of 8 bits.
If you try to edit this file as a standard text file, you get null characters between each letters.
You can use libiconv to convert the file format, or you can write your own converter.
Using iconv :
iconv -f UTF-16 -t UTF-8 yourFile.txt > fileToEdit.txt
iconv -f UTF-8 -t UTF-16 editedFile.txt > programFile.txt
If you're on Windows, you can use the MinGW distribution of libiconv.
The file is encoded in Unicode. UTF-16, most likely. If you open it in a decent text editor (e.g. Notepad++) it will automatically detect this and allow you to change the encoding. However, 'the program' (whatever that is) probably wants to consume the file with UTF-16 encoding. It's not clear why you're trying to change it but the answer is probably keep the 00s.

Endianness in Unix hexdump

The following *nix command pipes a hex representation of an IP and port (127.0.0.1:80) into the hexdump command.
printf "\x7F\x00\x00\x01\x00\x50" | hexdump -e '3/1 "%u." /1 "%u:" 1/2 "%u" "\n"'
The -e flag allows an arbitrary format to parse the input. In this case, we are parsing the first three octets of the IP into unsigned decimals followed by a dot. The final octet is also parsed into an unsigned decimal but it is followed by a colon. Finally -- and this is where the problem lies -- the 2 bytes for the port are parsed as a single unsigned decimal followed by a newline.
Depending on the endianness of the system executing this command, the result will differ. A big-endian system will properly show port 80; whereas a little-endian system will show port 20480.
Is there any way to manipulate hexdump to be aware of endianness while still allowing the arbitrary format specification via -e?
I don't know that it can be done with hexdump, but it's easy enough
in perl:
$ printf '\x00\x50' | perl -nE 'say unpack "S>"'
80
$ printf '\x00\x50' | perl -nE 'say unpack "S<"'
20480
You can tweak that to get the format you desire. ('say'
requires perl 5.10. Use print for perl < 5.10)
(To clarify for the person who wishes to downvote because I didn't
"answer the question". I'm suggesting that the OP replace
hexdump with perl. Downvote if you must.)

Resources