From 731634a6f1649d7f9bf27c275cdd918be1f1112d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 10:10:42 +0300 Subject: [PATCH] TrueHD: Atmos directory at 36 bytes, count from num_substreams (ffmpeg mlp_parse.c:71-74,160; mlpdec.c read_access_unit) 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.rs | 9 ++++++++- crates/ec-truehd/src/sync.rs | 13 +++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/ec-mpegts/src/track.rs b/crates/ec-mpegts/src/track.rs index 2e74a7c8..fc6f59e9 100644 --- a/crates/ec-mpegts/src/track.rs +++ b/crates/ec-mpegts/src/track.rs @@ -90,6 +90,7 @@ pub(crate) struct TrueHdFacts { pub rate: u32, pub layout: ChannelLayout, pub samples: u32, + pub substreams: usize, } /// How the program-stream front end hands pictures over (see `psvideo`). @@ -779,10 +780,15 @@ impl Track { // The access unit goes on in the next PES. break; } - let Ok(head) = AccessUnitHeader::parse(&rest[..len], 0) else { + let streams = self.cfg.truehd.as_ref().map_or(0, |t| t.substreams); + let Ok(head) = AccessUnitHeader::parse(&rest[..len], streams) else { cur += 1; continue; }; + if head.substreams.is_empty() { + cur += 1; + continue; + } if let Some(m) = head.major_sync && let Ok(layout) = head.channel_layout() { @@ -791,6 +797,7 @@ impl Track { layout, // A major sync's rate code sizes the access units: 40 << n samples. samples: 40 << (m.rate_code & 7), + substreams: head.substreams.len(), }); } if cur >= new_start && !anchored && p.pts.is_some() { diff --git a/crates/ec-truehd/src/sync.rs b/crates/ec-truehd/src/sync.rs index 5ea77428..0886f33f 100644 --- a/crates/ec-truehd/src/sync.rs +++ b/crates/ec-truehd/src/sync.rs @@ -121,10 +121,9 @@ pub struct MajorSyncInfo { /// L/R, 1 = C, 2 = LFE, 3 = Ls/Rs, 6 = Lrs/Rrs; `0x4F` is standard 7.1); /// 0 when the major sync is plain MLP, which has no such field. pub ch8_assignment: u16, - /// Substreams in the access unit's directory (the major sync's - /// `num_substreams`, top nibble of `data[16]`). + /// Substreams in the directory: the top nibble of `data[16]`. pub num_substreams: usize, - /// Bytes the major sync occupies, its extension block and checksum included. + /// Bytes the major sync occupies, extension block and checksum included. pub len: usize, } @@ -220,9 +219,7 @@ pub struct AccessUnitHeader { impl AccessUnitHeader { /// Parses one access unit's header from `data`, which must hold at least - /// [`frame_length`]'s worth of bytes. `stream_substreams` is the substream - /// count the stream's last major sync declared (0 before any); a unit with - /// its own major sync uses that sync's count instead. + /// [`frame_length`]'s worth of bytes. pub fn parse(data: &[u8], stream_substreams: usize) -> Result { let length = frame_length(data)?; if data.len() < length { @@ -423,8 +420,8 @@ mod tests { ]; au.extend_from_slice(&MAJOR_SYNC_TRUEHD.to_be_bytes()); au.push(0x0F); // sample rate: group1 code 0 (48000), group2 0xF (absent) - let mut rest = [0u8; 23]; // rest of the major sync, unused here - rest[11] = 0x20; // num_substreams = 2 (byte 16's top nibble) + let mut rest = [0u8; 23]; + rest[11] = 0x20; au.extend_from_slice(&rest); au.extend_from_slice(&[0x00, 0x01]); // substream 0: end +1 word (not the last) au.extend_from_slice(&[0x00, 0x04]); // substream 1: end +4 words (lands on EOF)