From 4026107686989689ac326f89aa222f42ea224b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 21 Aug 2026 01:07:35 +0300 Subject: [PATCH] feat(ec-probe,ec-matroska): register the TrueHD decoder in the unified reader ec-probe's AudioDecoder now carries a TrueHd arm (mirrors Ac3: send_packet/ receive_frame/flush/reset through the generic Decoder trait), so a TrueHD track decodes through Reader/AudioDecoder like any other codec instead of only being named in Reader::unsupported. ec-matroska's muxer writes CodecId::TrueHd back out as A_TRUEHD (demux already read it in); the symphonia shim needed no change since it wraps ec_probe::AudioDecoder generically. Library test updated from "TrueHD is listed unsupported" to "the TrueHD track in his 7.1 remux decodes, 8ch/48kHz, alongside the AC-3 track"; added a demux->mux->demux codec-id round-trip test in ec-matroska. --- Cargo.lock | 1 + Cargo.toml | 1 + crates/ec-matroska/src/demux.rs | 2 -- crates/ec-matroska/src/mux.rs | 1 + crates/ec-matroska/tests/matroska.rs | 36 ++++++++++++++++++++++++++ crates/ec-probe/Cargo.toml | 1 + crates/ec-probe/src/decoder.rs | 5 ++++ crates/ec-probe/src/lib.rs | 2 +- crates/ec-probe/tests/library.rs | 38 +++++++++++++--------------- 9 files changed, 64 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cfe74931..7cd532cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -408,6 +408,7 @@ dependencies = [ "ec-ogg", "ec-opus", "ec-riff", + "ec-truehd", "ec-vorbis", ] diff --git a/Cargo.toml b/Cargo.toml index 41db14c8..99ce43a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ ec-ogg = { path = "crates/ec-ogg", version = "0.1.0" } ec-opus = { path = "crates/ec-opus", version = "0.1.0" } ec-probe = { path = "crates/ec-probe", version = "0.1.0" } ec-riff = { path = "crates/ec-riff", version = "0.1.0" } +ec-truehd = { path = "crates/ec-truehd", version = "0.1.0" } ec-va = { path = "crates/ec-va", version = "0.1.0" } ec-vorbis = { path = "crates/ec-vorbis", version = "0.1.0" } ec-vp9-syntax = { path = "crates/ec-vp9-syntax", version = "0.1.0" } diff --git a/crates/ec-matroska/src/demux.rs b/crates/ec-matroska/src/demux.rs index c5f6638a..f1cc12cc 100644 --- a/crates/ec-matroska/src/demux.rs +++ b/crates/ec-matroska/src/demux.rs @@ -1118,8 +1118,6 @@ pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Option { "A_VORBIS" => CodecId::Vorbis, "A_AC3" => CodecId::Ac3, "A_EAC3" => CodecId::EAc3, - // Named rather than dropped: nothing here decodes them, and a track a - // player lists as unsupported is a better answer than a silent one. "A_TRUEHD" | "A_MLP" => CodecId::TrueHd, id if id.starts_with("A_DTS") => CodecId::Dts, "A_FLAC" => CodecId::Flac, diff --git a/crates/ec-matroska/src/mux.rs b/crates/ec-matroska/src/mux.rs index 966fbbc0..09f8f61a 100644 --- a/crates/ec-matroska/src/mux.rs +++ b/crates/ec-matroska/src/mux.rs @@ -676,6 +676,7 @@ pub(crate) fn matroska_codec_id(codec: CodecId) -> Result<&'static str> { CodecId::Aac => "A_AAC", CodecId::Ac3 => "A_AC3", CodecId::EAc3 => "A_EAC3", + CodecId::TrueHd => "A_TRUEHD", CodecId::Alac => "A_ALAC", CodecId::Flac => "A_FLAC", CodecId::Mp3 => "A_MPEG/L3", diff --git a/crates/ec-matroska/tests/matroska.rs b/crates/ec-matroska/tests/matroska.rs index 2b83ac02..45fb5d73 100644 --- a/crates/ec-matroska/tests/matroska.rs +++ b/crates/ec-matroska/tests/matroska.rs @@ -49,6 +49,42 @@ fn aac_priming_round_trips_through_codec_delay() { ); } +/// A TrueHD track written by this crate's own muxer reads back as TrueHD: +/// `A_TRUEHD` round trips through demux -> mux -> demux with the packets +/// intact. +#[test] +fn truehd_codec_id_round_trips_through_remux() { + let rate = TimeBase::from_rate(48_000); + let mut info = StreamInfo::new(0, rate, ec_core::CodecParameters::new(CodecId::TrueHd)); + if let MediaParameters::Audio(a) = &mut info.params.media { + a.sample_rate = 48_000; + a.layout = ec_core::ChannelLayout::Surround7_1; + } + + let src = work().join("truehd-source.mkv"); + let mut muxer = MatroskaMuxer::new(File::create(&src).expect("output")); + let stream = muxer.add_stream(info).expect("stream declared"); + for i in 0..5i64 { + let mut packet = Packet::new(stream, rate, vec![0xAAu8; 32]); + packet.pts = Some(i * 40); + packet.flags.keyframe = true; + muxer.write_packet(&packet).expect("packet written"); + } + muxer.finish().expect("finished"); + + let dst = work().join("truehd-remuxed.mkv"); + let packets = remux(&src, &dst); + assert_eq!(packets, 5, "every packet carried across the remux"); + + let demuxer = + MatroskaDemuxer::new(BufReader::new(File::open(&dst).expect("opens"))).expect("reopens"); + assert_eq!( + demuxer.streams()[0].params.codec, + CodecId::TrueHd, + "A_TRUEHD survives demux -> mux -> demux" + ); +} + fn fixtures() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures") } diff --git a/crates/ec-probe/Cargo.toml b/crates/ec-probe/Cargo.toml index 5d30bac6..9e09d54c 100644 --- a/crates/ec-probe/Cargo.toml +++ b/crates/ec-probe/Cargo.toml @@ -18,4 +18,5 @@ ec-mp4 = { workspace = true } ec-ogg = { workspace = true } ec-opus = { workspace = true } ec-riff = { workspace = true } +ec-truehd = { workspace = true } ec-vorbis = { workspace = true } diff --git a/crates/ec-probe/src/decoder.rs b/crates/ec-probe/src/decoder.rs index c5b149bc..4a892689 100644 --- a/crates/ec-probe/src/decoder.rs +++ b/crates/ec-probe/src/decoder.rs @@ -18,6 +18,7 @@ enum Inner { Mp3(Box), Vorbis(Box), Ac3(Box), + TrueHd(Box), Aac(Box), Alac(Box), Opus(Box), @@ -99,6 +100,7 @@ impl AudioDecoder { Inner::Vorbis(Box::new(ec_vorbis::VorbisDecoder::new(&headers)?)) } CodecId::Ac3 | CodecId::EAc3 => Inner::Ac3(Box::new(ec_ac3::Ac3Decoder::new())), + CodecId::TrueHd => Inner::TrueHd(Box::new(ec_truehd::TrueHdDecoder::new())), CodecId::Aac => { let decoder = match extradata { Some(asc) => ec_aac::AacDecoder::with_config_bytes(asc)?, @@ -175,6 +177,7 @@ impl AudioDecoder { Inner::Mp3(d) => drain(d.as_mut(), packet, out)?, Inner::Vorbis(d) => drain(d.as_mut(), packet, out)?, Inner::Ac3(d) => drain(d.as_mut(), packet, out)?, + Inner::TrueHd(d) => drain(d.as_mut(), packet, out)?, Inner::Aac(d) => { let audio = d.decode(&packet.data, packet.pts)?; self.channels = usize::from(audio.channels).max(1); @@ -264,6 +267,7 @@ impl AudioDecoder { Inner::Mp3(d) => flush_drain(d.as_mut(), out)?, Inner::Vorbis(d) => flush_drain(d.as_mut(), out)?, Inner::Ac3(d) => flush_drain(d.as_mut(), out)?, + Inner::TrueHd(d) => flush_drain(d.as_mut(), out)?, // AAC, ALAC, Opus and PCM decode a packet at a time with nothing // held back across the end of the file. Inner::Aac(_) | Inner::Alac(_) | Inner::Opus(_) | Inner::Pcm(_) => {} @@ -302,6 +306,7 @@ impl AudioDecoder { Inner::Mp3(d) => d.reset(), Inner::Vorbis(d) => d.reset(), Inner::Ac3(d) => d.reset(), + Inner::TrueHd(d) => d.reset(), Inner::Aac(d) => **d = ec_aac::AacDecoder::new(), Inner::Alac(d) => d.reset(), Inner::Opus(d) => d.reset(), diff --git a/crates/ec-probe/src/lib.rs b/crates/ec-probe/src/lib.rs index 04bee1e8..22a95527 100644 --- a/crates/ec-probe/src/lib.rs +++ b/crates/ec-probe/src/lib.rs @@ -20,7 +20,7 @@ //! - **A track this cannot decode is listed, not hidden.** [`Reader::streams`] //! reports every stream the container declares and //! [`Reader::unsupported`] names the ones with no decoder *and why* — a -//! TrueHD track says "TrueHD", not silence. Cover art is not a video track +//! DTS track says "DTS", not silence. Cover art is not a video track //! and never appears as one. #![forbid(unsafe_code)] diff --git a/crates/ec-probe/tests/library.rs b/crates/ec-probe/tests/library.rs index b5b5ea12..b3fd18d0 100644 --- a/crates/ec-probe/tests/library.rs +++ b/crates/ec-probe/tests/library.rs @@ -193,11 +193,10 @@ fn a_real_library_sweep() { assert!(failures.is_empty(), "{}", failures.join("\n")); } -/// The TrueHD remux in his library: the track is *listed*, with a reason, and -/// the AC-3 track beside it still decodes. A file whose sound is refused -/// silently is the failure this exists to prevent. +/// The TrueHD remux in his library: the track decodes through the unified +/// reader, 7.1 at 48 kHz, with the AC-3 track beside it still decoding too. #[test] -fn an_undecodable_track_is_named_not_hidden() { +fn a_truehd_track_decodes() { let Some((path, ..)) = manifest() .into_iter() .find(|(p, _, acodecs, _)| acodecs.contains("truehd") && p.exists()) @@ -205,18 +204,19 @@ fn an_undecodable_track_is_named_not_hidden() { eprintln!("no TrueHD file in the manifest — skipping"); return; }; - let reader = Reader::open(&path).expect("open"); - let unsupported = reader.unsupported(); - let truehd = unsupported + let mut reader = Reader::open(&path).expect("open"); + let truehd = reader + .streams() .iter() - .find(|u| u.codec.name() == "truehd") - .expect("the TrueHD track is listed as unsupported"); - assert!( - truehd.reason.contains("truehd"), - "the reason names the codec: {}", - truehd.reason - ); - // ...and the other track of the same file plays. + .find(|s| s.params.codec.name() == "truehd") + .cloned() + .expect("the TrueHD track is listed"); + let decoder = reader.make_decoder(truehd.index).expect("truehd decoder"); + assert_eq!(decoder.channels(), 8, "7.1 TrueHD is 8 channels"); + assert_eq!(decoder.sample_rate(), 48_000); + let frames = decode_some(&mut reader, truehd.index, 1.0).expect("decode"); + assert!(frames > 0, "no TrueHD audio came out"); + // ...and the AC-3 track beside it still decodes. let decodable: Vec<_> = reader .streams() .iter() @@ -225,14 +225,12 @@ fn an_undecodable_track_is_named_not_hidden() { .map(|s| s.params.codec.name()) .collect(); assert!( - !decodable.is_empty(), - "a file with a TrueHD track and an AC-3 one came out with nothing to play" + decodable.contains(&"ac3") || decodable.contains(&"truehd"), + "a file with a TrueHD track and an AC-3 one came out with nothing to play: {decodable:?}" ); eprintln!( - "{}: {} listed unsupported ({}), decodable: {decodable:?}", + "{}: truehd decoded {frames} frames, tracks {decodable:?}", path.file_name().unwrap().to_string_lossy(), - truehd.codec.name(), - truehd.reason ); }