forge
Explore
Log in

Don't you dare go Hollow

TrueHD: Atmos directory at 36 bytes, count from num_substreams (ffmpeg mlp_parse.c:71-74,160; mlpdec.c read_access_unit)

Browse files at this commit

731634a6f1649d7f9bf27c275cdd918be1f1112dAhmet Kaan GümüşParent: 2c05ed8
Root cause, sync.rs AccessUnitHeader::parse: the major sync was fixed at 28 bytes.
ffmpeg mlp_get_major_sync_size adds 2 + 2*(buf[26]>>4) when buf[25]&1 (TrueHD).
atmos.thd AU0 sets buf[25]&1 with buf[26]>>4 == 1, so the directory starts at
36 (4 + 32), not 32. The entry count was inferred from end pointers landing on
the AU length; ffmpeg reads num_substreams (buf[16]>>4) and ignores trailing
bytes. atmos.thd AU25/63/102 end 4 words short of the AU length, so the walk
ran past 4 entries.

Fix: extension-aware major sync length; count from num_substreams, carried in
TrueHdFacts for access units without a major sync; the scanner skips access
units that have no known count. ffmpeg evidence: AU0 4 entries, last end
1092 bytes past dir end 44 = 1136 AU length; sub-entries 246/578/750/1136 with
the extension sync.
crates/ec-mpegts/src/track.rsmodified+8 -1
@@ -90,6 +90,7 @@ pub(crate) struct TrueHdFacts {
90 pub rate: u32,90 pub rate: u32,
91 pub layout: ChannelLayout,91 pub layout: ChannelLayout,
92 pub samples: u32,92 pub samples: u32,
93+ pub substreams: usize,
93 }94 }
94 95
95 /// How the program-stream front end hands pictures over (see `psvideo`).96 /// How the program-stream front end hands pictures over (see `psvideo`).
@@ -779,10 +780,15 @@ impl Track {
779 // The access unit goes on in the next PES.780 // The access unit goes on in the next PES.
780 break;781 break;
781 }782 }
782- let Ok(head) = AccessUnitHeader::parse(&rest[..len], 0) else {783+ let streams = self.cfg.truehd.as_ref().map_or(0, |t| t.substreams);
784+ let Ok(head) = AccessUnitHeader::parse(&rest[..len], streams) else {
783 cur += 1;785 cur += 1;
784 continue;786 continue;
785 };787 };
788+ if head.substreams.is_empty() {
789+ cur += 1;
790+ continue;
791+ }
786 if let Some(m) = head.major_sync792 if let Some(m) = head.major_sync
787 && let Ok(layout) = head.channel_layout()793 && let Ok(layout) = head.channel_layout()
788 {794 {
@@ -791,6 +797,7 @@ impl Track {
791 layout,797 layout,
792 // A major sync's rate code sizes the access units: 40 << n samples.798 // A major sync's rate code sizes the access units: 40 << n samples.
793 samples: 40 << (m.rate_code & 7),799 samples: 40 << (m.rate_code & 7),
800+ substreams: head.substreams.len(),
794 });801 });
795 }802 }
796 if cur >= new_start && !anchored && p.pts.is_some() {803 if cur >= new_start && !anchored && p.pts.is_some() {
crates/ec-truehd/src/sync.rsmodified+5 -8
@@ -121,10 +121,9 @@ pub struct MajorSyncInfo {
121 /// L/R, 1 = C, 2 = LFE, 3 = Ls/Rs, 6 = Lrs/Rrs; `0x4F` is standard 7.1);121 /// L/R, 1 = C, 2 = LFE, 3 = Ls/Rs, 6 = Lrs/Rrs; `0x4F` is standard 7.1);
122 /// 0 when the major sync is plain MLP, which has no such field.122 /// 0 when the major sync is plain MLP, which has no such field.
123 pub ch8_assignment: u16,123 pub ch8_assignment: u16,
124- /// Substreams in the access unit's directory (the major sync's124+ /// Substreams in the directory: the top nibble of `data[16]`.
125- /// `num_substreams`, top nibble of `data[16]`).
126 pub num_substreams: usize,125 pub num_substreams: usize,
127- /// Bytes the major sync occupies, its extension block and checksum included.126+ /// Bytes the major sync occupies, extension block and checksum included.
128 pub len: usize,127 pub len: usize,
129 }128 }
130 129
@@ -220,9 +219,7 @@ pub struct AccessUnitHeader {
220 219
221 impl AccessUnitHeader {220 impl AccessUnitHeader {
222 /// Parses one access unit's header from `data`, which must hold at least221 /// Parses one access unit's header from `data`, which must hold at least
223- /// [`frame_length`]'s worth of bytes. `stream_substreams` is the substream222+ /// [`frame_length`]'s worth of bytes.
224- /// count the stream's last major sync declared (0 before any); a unit with
225- /// its own major sync uses that sync's count instead.
226 pub fn parse(data: &[u8], stream_substreams: usize) -> Result<AccessUnitHeader> {223 pub fn parse(data: &[u8], stream_substreams: usize) -> Result<AccessUnitHeader> {
227 let length = frame_length(data)?;224 let length = frame_length(data)?;
228 if data.len() < length {225 if data.len() < length {
@@ -423,8 +420,8 @@ mod tests {
423 ];420 ];
424 au.extend_from_slice(&MAJOR_SYNC_TRUEHD.to_be_bytes());421 au.extend_from_slice(&MAJOR_SYNC_TRUEHD.to_be_bytes());
425 au.push(0x0F); // sample rate: group1 code 0 (48000), group2 0xF (absent)422 au.push(0x0F); // sample rate: group1 code 0 (48000), group2 0xF (absent)
426- let mut rest = [0u8; 23]; // rest of the major sync, unused here423+ let mut rest = [0u8; 23];
427- rest[11] = 0x20; // num_substreams = 2 (byte 16's top nibble)424+ rest[11] = 0x20;
428 au.extend_from_slice(&rest);425 au.extend_from_slice(&rest);
429 au.extend_from_slice(&[0x00, 0x01]); // substream 0: end +1 word (not the last)426 au.extend_from_slice(&[0x00, 0x01]); // substream 0: end +1 word (not the last)
430 au.extend_from_slice(&[0x00, 0x04]); // substream 1: end +4 words (lands on EOF)427 au.extend_from_slice(&[0x00, 0x04]); // substream 1: end +4 words (lands on EOF)