Log in

View Full Version : interleaveDolbyStreams.py: A Python Tool that Interleaves AC3 and TrueHD Bit Streams


digitalaudionerd
25th October 2022, 05:32
I wrote a command line script that interleaves the AC3 and Dolby TrueHD bit streams in the exact manner found in blu-ray. Even though I wrote this script for my selfish purposes, because it would be useful to others, I’ve decided to freely share it on GitHub: https://github.com/digitalaudionerd/Interleave-AC3-TrueHD-Streams

I developed this script on Linux, but, because Python is cross-platform, it should run on any system with Python 3.

If you wish to find out how I know that this script produces interleaved AC3/TrueHD bit streams exactly as they are found in blu-ray, please continue reading.

Testing

I had some .ac3, .thd and .thd+ac3 files. After having my script interleave the various .ac3 and .thd files, I compared the .thd+ac3 files it made to the original .thd+ac3 files. The interleaved .thd+ac3 files were identical to the original .thd+ac3 files.

Content with those tests, I interleaved a set of .ac3 and .thd files from an MKV file. I then used the video from the MKV file and its newly corresponding .thd+ac3 file to create an AVCHD with tsMuxer. I transferred the AVCHD to my NAS and watched the AVCHD with my Oppo UDP-203 blu-ray player (my selfish purpose).

My Oppo identified the audio stream as Dolby TrueHD (if the audio track is only TrueHD without the AC3 “core,” the Oppo identifies the stream as MLP). My Denon AV receiver decoded the TrueHD audio, just as with any Dolby TrueHD blu-ray or blu-ray backup.

How do I know the structure of AC3 and TrueHD interleaving in blu-ray?

Before writing this interleave script, I wrote a previous Python script that examined the AC3 frames and TrueHD access headers of the .thd+ac3 files I had. After making the script count the frames and access headers, I noticed a clearly distinct pattern consistent in all of the .thd+ac3 files.

tormento
25th October 2022, 09:46
I wrote a command line script that interleaves the AC3 and Dolby TrueHD bit streams
What are the differences with THDMerge, found in DGDecodeNV package?

digitalaudionerd
26th October 2022, 03:20
One difference is how the streams are interleaved. I saw this comment about how the output of thdmerge doesn’t exactly match ripped files: https://forum.doom9.org/showthread.php?p=1965447#post1965447

To investigate, I used thdmerge to create some .thd+ac3 files. I examined these test files and saw the discrepancy. In official blu-ray streams, there are 192 TrueHD access headers for every 5 AC3 frames (the only possible exception is at the end of the interleaved stream). Thdmerge starts with an error: there are 193 TrueHD access headers for the first 5 AC3 frames, and then the remaining bit stream is more correct, with 192 access headers per 5 frames, but not arranged exactly as in blu-ray.

Another difference is that thdmerge alternates reading and writing files throughout the interleaving process. I looked at the source code, and it looks like the code reads one input file, writes its data to the .thd+ac3 file, and then reads/writes for the other input file, repeating the process until reaching the end of each input file. (Thdmerge is written in C++, which isn’t my programming language, so I might be mistaken.) My interleaving script reads the entirety of the .ac3 and .thd files first, parses the data, interleaves the data, and then writes the data to the .thd+ac3 file.

The last difference I noticed is that my script doesn’t have the truncate feature that thdmerge has. I can add the truncate feature if requested.

gonca
30th October 2022, 17:23
Your app does appear to create a thd+ac3 file identical to the original, as per a binary file compare.
Good stuff

Hellboy.
6th June 2023, 21:24
What are the differences with eac3to.exe? And what the truncate feature do?
Thanks.

tebasuna51
7th June 2023, 09:31
eac3to read a input thd file, recode it to ac3 with libaften.dll (old and with some bugs) and then mux them.

Here the thd and the ac3 must be the input.

The truncate feature is, when the two streams have different length, truncate the output at short length.

Balling
18th August 2023, 06:46
Does not work with mono tracks.

Also truncate feature is useless. AC3 is always priming format.

Does not work on other samples too. BTW, found out that ffmpeg can truncate last ac3 frame on demux from m2ts compared to TSmuxer


ALSO:

There is no specification for the interleaving and no issues have been reported with streams interleaved by thdmerge. Saying it starts with an error is unsubstantiated FUD. I have looked at many BD disks and there is no consistent pattern to the interleaving, despite what the guy claims. thdmerge merges 5GB files in less than 5-10 seconds on my system. There is no performance issue and I seriously doubt a python solution is as fast.


From https://rationalqm.us/board/viewtopic.php?p=16528#p16528