Log in

View Full Version : How to convert an AVC_H.264-ts recording


Schmendrick
19th September 2006, 17:50
How to convert an AVC/H.264-ts recording from e.g. satellite or terrestrial transmission into a directshow-avisynth usable input format e.g. for processing with VirtualDubMod



Software required (on http://www.doom9.org/ or at link given) :

1. Graphedit with installed Dump-filter (included within graphedit-package)

2. AVC/H.264-decoder, either:

a. Cyberlinks AVC/H.264-package included within PowerDVD Version 7 or PowerDVD Version 6 with AVC/H.264-package ( http://www.cyberlink.com/ )

b. Elecards AVC/H.264-decoder included within MPEG2-player package or EmuxerPro AVC/H.264-plugin-package ( http://www.elecard.com/ )

c. Coreavc H.264-decoder together with Haali-demuxer package (http://www.coreavc.com/ )

3. Yamb 1.6 or 2.0 with MP4Box (included within package)

4. VirtualDubMod

5. ProjectX

6. DVBStreamexplorer ( http://www.dvbstreamexplorer.dk/ )

7. ffdshow a recent build with enabled H.264-decoding


1. Demux ts-file with ProjectX and read from the demux protocol the listed "PES-IDs".
You will e.g. find the following:
"ok> PID 0x90E has PES-ID 0xE0 (MPEG Video) (104310 #556)
ok> PID 0x910 has PES-ID 0xC8 (MPEG Audio) (119568 #637)"
or instead of the last line:
"ok> PID 0x103 has PES-ID 0xBD (private stream 1) (39480 #211)"
This line is shown if an AC3-Dolby-Digital-Audio-stream is present. Usually either a MPEG-audio stream or an AC3-stream is present besides the MPEG-video-stream.

Near the end of the demux protocol e.g. the following is listed:
"Audio PTS: first packet 08:15:11.361, last packet 08:16:53.345"

As ProjectX is not able to produce an AVC/H.264-stream the Video PTS has to be found in a different way. To accomplish this you have to open the recorded ts-file with a hex editor like the hex editor available within VirtualDubMod under "Tools". Using the "Find"-function from the "Edit"-menu the "Find binary string" opens, then enter the following characters into the "Find"-field: "00 00 00 01 67" and click the tic-box "Hexadecimal search string" and finally click on "Find next" to find this string. For example at the bottom of the following picture the found string is shown. Now find the next "00 00 01 E0" string just ahead of this string. In this picture just a few byte above starting at memory location "E0DCD" in the right center. Then finally find the video PTS which is contained within 5 bytes starting at the 9th byte after the beginning of the last searched string. Here marked within a black box. Here it reads: "35 7D 95 01 F1". (Fig1.jpg)
6183
The video PTS is a 33 bit value where the most significant bit is a sign bit.
This 33bit value is extracted from these 5 bytes. On the first byte a bitwise AND operation has to be performed with "0E". The result has to be bitwise shifted right one bit. The resulting 3bit-value is the 33th to 31st bit (counted from left to right in the binary representation of this 3bit-value), where the 33th bit the sign bit means: "0" is positive, "1" is negative. The remaining value has to be multiplied by 2 to the power of 30. The second byte is multiplied by 2 to the power of 22. The third byte value is first converted with a bitwise AND operation with "FE". The result bitwise shifted right for one bit and the result muliplied by 2 to the power of 15. The forth byte is multiplied by 128. The fifth byte value is first converted with a bitwise AND operation with "FE". The result bitwise shifted right for one bit. All five results are summed together and fitted with the obtained sign from the 33 bit value leading to the video PTS.

The video PTS is the system 27 MHz clock divided by 300 so a 90 kHz clock value. To convert the found value into milliseconds the value has to be divided by 90. A division of the obtained millisecond results by 3600000 converts to hours, by 60000 to minutes and by 1000 to seconds. The hour, minute, second and millisecond values are obtained by deleting the corresponding value after the decimal points. In this case "35 7D 95 01 F1" results into: "8:15:13.2969777" ( 8 hours, 15 minutes, 13 seconds, 296.9777 milliseconds. This means that the audio stream is delayed by -1.9359777 seconds (calculated by subtracting the video PTS-value from the audio PTS-value).

To properly demux the ts-file into a video-AVC/H.264 stream file and an audio-AC3 stream file or an audio-MPEG stream file either graphedit or DVBstreamexplorer is used. Start graphedit use Graph - Insert Filters - Direct Show Filters - File Source (Async.). Select the ts-file which is to be demuxed. Then Insert - Direct Show Filters - Elecard MPEG Demultiplexer (empgdmx.ax (version: 1.0.75.60727), 1012 KB). Demuxing with either Cyberlinks Demuxer contained within PowerDVD 7 or 6 (with AVC/H.264-decoder pack) or with the Haali Media Splitter instead cannot be used.
Connect the "File Source (Async.)"-box with the "Elecard MPEG Demultiplexer"-Box". After that select the "Dump"-filter. After selection enter a new file name with the file extension "asf", as this is the only extension the "Dump"-filter accepts. Nevertheless the "Dump"-filter produces a plain dump of the output of the demultiplexer. Connect the "H264"-output pin of the "Elecard MPEG Demultiplexer"-box with the input pin of the "Dump"-box. Finally click on the green triangular play button. Now the red "Stop" button apears. After demultiplexing finishes the green button appears again and the red button is greyed out again. (Fig2.jpg)
6184
Instead of using graphedit with Elecards MPEG Demultiplexer also DVBStreamexplorer can be used to extract the H.264-video stream:
At first at "Device" - "Driver selection" "TS file driver" has to be selected. Then at "Device" - "Tuning" the ts-file to be demultiplexed has to be selected. Having done this at "Action" - "SI scanning" - "SI tables" on the SI tables-box click on "Defaults" and "OK". Then click on "Action" - "SI scanning" - "Current transponder". This opens the "SI Reading"-box. On this box click on "Start" to scan the ts-file for its content. After some time this box can be closed again after the "Close"-button is not greyed out anymore. In the lower window at least "Got 1 PMT section(s) for SID XXXX" is displayed (with e.g. 27D8 at XXXX). Double-click on "Transponder" within the upper window to display the content section found due to the scanning operation. Now click on the small "+"-symbol left of "PMT" and left of the following subfolders. This opens the content-service tree (Fig3.jpg):
6185
Below the stream info line as shown in this figure click on the line "ITU-T Rec. H.262..."-line. Within the top right window the stream type is shown and two lines lower the "ELEMENTARY PID" here 0xFF (255). In this case the H.264-video is encased within a H.262-MPEG2-system package. DVBstreamexplorer does not analyze its content. (Fig4.jpg)
6186
Then under stream info line as shown in the figure above click on the line:
"ITU.T Rec. H.222.....". On the right side again the stream type and the "ELEMENTARY PID" here 0x103 (259) is shown.

Now knowing the elementary PIDs of the video and audio streams demux these streams by clicking on "Plugins" - "Streaming to disk" - "Save A/V ES streams" opens the "A/V ES Streaming"-box. Here at VPID the video PID found (here 255) and a file name for this stream has to be entered. The same has to be done at APID for the audio PID found (here 259) and a file name for the audio stream. Both file by default will have "bin" as file extension. Then start demuxing by clicking on "Start". You will have to watch if the file size of the stream files keeps increasing or stops to increase as an indication that the demuxing is finished as the demuxing function does not automatically switch off at the end of the file as is was initially programmed for indefinitely streaming satellite transmissions. So by clicking on "Stop" and "OK" this box will be closed.

This example ts-file has been recorded using the KNC TV Station DVB-S2+-card and the "Globe TV Digital"-programme.

Other recording devices and the associated programmes appear to be able to detect the AVC/H.264-video stream properly like in a short clip which was transmitted by "BBC HD1" (Fig5.jpg):
6187
Now open the produced files with the Hex editor of VirtualDubMod and again search for the hex string "00 00 00 01 67" as explained above. After the string is found, open Edit - Extract segment. Within the "Extract file segment" box the "Address (hex):" field is already filled with the start address. The value for the "Length (hex):" is found by search for the address for the last byte of the file within the Hex editor. From this hex value the found start address has to be subtracted (e.g. using the calculator within the accessories of Windows XP after switching to hex mode within the scientific view mode) and the result has to be increased by one. After this value has been entered click on "OK" and enter a file name for the h264-file to be produced e.g. "Pro7HD.h264". Now an "AVC/H.264"-file is produced with the correct start code (a key frame) and can be further processed as explained following.

Guide continues on following posting as maximum number of attached attachements is reached.

Schmendrick
19th September 2006, 17:56
Continuation from previous posting (Fig6.jpg):
6188
Now use Yamb 1.6 or 2.0 to produce a MP4 out of it.

The mp4-file then can be used as video source by opening it within the file - open video file dialog of VirtualDubMod setting the "Use Avisynth template" to "DirectShowSource".
Then clicking Streams - Stream list opens the "Available streams" dialog box in which by clicking on Add the decoded AC3 or MPEG-Audio file can be added. A right click on the inserted display of the added stream opens an option box on which interleaving has to be clicked and in the Audio/Video Interleaving Options box an "Audio skew correction" box is shown where the audio delay determined as explained above should be entered as a negative millisecond value into the "Delay the audio track by" box. Then clicking OK closes this box and a click on OK closes the "Available streams" box.

After that under "Video" "Full processing mode" has to be clicked. Then under "Video" and "Filters" adding the "resize"-filter the video can be appropriately resized e.g. 1920x1088 what is transmitted from european HDTV satellites has an aspect ratio of 1:1.765 which coresponds to e.g. 1280x725 or 720x408 which has to be expanded to a displayable format like 1280x768.

Finally under "Video" and "Compression" XviD, the new DivX-versions or x264 can be used as appropriate compression encoder. The bitrate used should correspond to the display resolution to retain the video quality.

At last the encoding is started by giving a file name after clicking "File" - "Save as" into the "File name" field and a final click on "Save".

For whom it is too complicated to calculate the audio delay value from the five byte values for the video- and audio-PTS I have included a zipped Excel-file which does this calculation.

6181

Suggestions for improvements are appreciated.

Schmendrick

tomos
20th September 2006, 17:31
but directshow source works on the original transport stream though doesnt it? i've been messing around with this also and i have recompressed one video with megui to fit on 1 dvdr by doing that.

is there a need to use avisynth at all to import to vdub etc tho? i mean i used mplayer to get the raw 264 video (same with ac3 audio) and used avc2avi to get the video into an avi container. that opens up fine in vdub afterwards - or if you are anti VFW - follow bonds advice here (http://forum.doom9.org/showthread.php?p=876853#post876853)

i used mp4creator in this way to mux the 264 content into mp4 after trying out the avi route. mp4creator -create=raw.264 -rate=25.000 out.mp4


depends how you want to do it i guess

Schmendrick
20th September 2006, 18:18
When I tried to open the ts-file via directshow source or via avc2avi, as my source video is interlaced I always had doubled frames resulting in 50 instead of 25 frames per second. So far I have not found an other way to properly deinterlace the source video without doubling the framerate. Tried to use various Avisynth-deinterlacers and was not successful.

tomos
20th September 2006, 18:30
i use coreavc to play the video and that works fine. shows in vdub with dss as 25fps

blubberbirne
20th September 2006, 20:07
could you please upload the pictures to rapidshare or something other? can't click the pics, because they are not approved :(

Schmendrick
20th September 2006, 21:21
Following a link to a zip-file (74 kB) with a pdf-version of the guide and the excel-file:
http://download.yousendit.com/60A21E1B5BEC5396

Enjoy!

Schmendrick
20th September 2006, 21:39
@ tomos: I also use coreavc. I have the additional problem, that I am recording the H.264-transmissions using the KNC One TV Station DVB-S2+-card with a Pentium 4-2.67 GHz system which is too slow for realtime playback of the H.264-videos. Therefore I first have to record a transmission, reencode it to 1280x768 resolution which is the maximum resolution of my flat TV anyway. The resulting video encoded with x264 with 2MBit/sec has 50-70% processor load when played using Coreavc.

When I attempt to record the transmissions as ts-file the GlobeDigital-programme of KNC One crashes usually after just a few minutes of recording. As my system does not conform to the required specs for H.264 KNC One refuses to further help me.

To solve this problem I found a weird solution:
I have installed Mainconcepts Demo H.264 encoder. It comes along with a Demo H.264 decoder which does not work on my system, still this decoder is recognized as a valid H.264 decoder within the Globe Digital Programme, but if I select this decoder nothing actually is decoded but the channel reception still works and it is possible to do a recording of the video- and the audio-PID transmissions as raw stream files.
In this mode the Globe Digital programme runs stable ever over hours.

This raw file so far I can only further properly process with the graphedit/Elecard-Demuxer-method as explained above, as these raw video- and audio-files are no transport files but MPEG programm system files even though they only can be handled with Elecards Demultiplexer. PowerDVD7 and Haali demultiplexer do not work. For all other demultiplexers these files look like MPEG2-files and appear to be attempted to be treated as such files. Still no MPEG2-video is contained but H.264 video.

dumbas..
21st September 2006, 17:27
@Schmendrick

Thank you for the superb guide above:thanks: .
I hope you can help with a small problem.

I have a .ts file containing H.264 & ac3.
I have followed your demuxing guide using DVBstreamexplorer.
I have reached the section of saving the .H264 file using the VirtualDub Hex editor:
For some reason the file save in 'extract segment' appears to hang at 3.25GB out of a total save of circa 7Gb.
The original .bin file is 7.25GB.

VirtualDub in the task manager appears still to be creating file I/O reads. But the progress bar in VirtualDub has 'paused'.
I have to abort to exit the save.

The 'partial' file will play in MPC showing I chose the segment start correctly, I think. But, of course it hangs later on the incomplete .H264 file.

I repeated the steps and got the same result.

Any thoughts? Your help would be much appreciated.

Schmendrick
21st September 2006, 18:41
@ dumbas.. : I never have attempted to process file larger than 4 GB, as I always interrupt recording during commercials and restart them shortly thereafter to avoid having large files. Possibly a hex editor which has been programmed to handle files larger than 4 GB might work.
I have found a hex editor which claims to be able to handle files larger than 4GB under this location:
http://mh-nexus.de/hxd/
I have not tried to use it. May be it solves your problem.

dumbas..
21st September 2006, 19:10
@ dumbas.. : I never have attempted to process file larger than 4 GB, as I always interrupt recording during commercials and restart them shortly thereafter to avoid having large files. Possibly a hex editor which has been programmed to handle files larger than 4 GB might work.
I have found a hex editor which claims to be able to handle files larger than 4GB under this location:
http://mh-nexus.de/hxd/
I have not tried to use it. May be it solves your problem.

Thanks for the prompt reply.. I was just going to edit my question when I saw the above. Just to add complexity - The output from Yamb of the borked output appears at half height ??? ie 1920*544
rather than 1088!!

I've come to realise that somethings are just NOT meant to happen:rolleyes:

I will give your suggestion a try... Thanks once again

Schmendrick
22nd September 2006, 11:50
An output of 1920x544 is caused due to the fact that tha fact that the source video is interlaced does not seem to be properly addressed. Within the following thread:
http://forum.doom9.org/showthread.php?t=114361&highlight=avc2avi_mod
this issue was discussed and also a modified version of avc2avi is given.
Anyway with my method I have not found a problem.
By the way the hex editor of which I have given is link is really able to handle files >4 GB. After extraction of the H.264-video with either the graphedit/Elecard Demuxer or the DVBstreamexplorer method you just have to mark all byte ahead of the first occurence of the IDR flag (key frame "00 00 00 01 67"), delete this part and save the remaining part. If you also delete the last part of the file starting and including with the last occuring IDR flag you should obtain a H.264-file which meets all requirements, provided you did not had any transmission errors within the recording, which sometimes still is a problem with satellite transmissions. Here I guess only a really large satellite dish might help to prevent a poor reception signal due to heavy rain, clouds or snow fall.

dumbas..
22nd September 2006, 20:27
@Schmendrick

Yes, thanks for the Hex editor link.. That little program is very good. I was able to guess that I should delete the 'header' section as you say. Thanks for the extra bit tho' I need to delete the end section as well...

Thanks for the info on avc2avi.

Your comprehensive advice has been greatfully recieved.

As a video newbie I can only thank you! :thanks: