Register the TrueHD decoder in the unified reader
Browse files at this commit4026107686989689ac326f89aa222f42ea224b20Parent: 2f6b7e8- feat
- register the TrueHD decoder in the unified reader
9 changed files +64 -23
- Cargo.lock +1 -0
- Cargo.toml +1 -0
- crates/ec-matroska/src/demux.rs +0 -2
- crates/ec-matroska/src/mux.rs +1 -0
- crates/ec-matroska/tests/matroska.rs +36 -0
- crates/ec-probe/Cargo.toml +1 -0
- crates/ec-probe/src/decoder.rs +5 -0
- crates/ec-probe/src/lib.rs +1 -1
- crates/ec-probe/tests/library.rs +18 -20
Cargo.lockmodified+1 -0
| @@ -408,6 +408,7 @@ dependencies = [ | |||
| 408 | "ec-ogg", | 408 | "ec-ogg", |
| 409 | "ec-opus", | 409 | "ec-opus", |
| 410 | "ec-riff", | 410 | "ec-riff", |
| 411 | + "ec-truehd", | ||
| 411 | "ec-vorbis", | 412 | "ec-vorbis", |
| 412 | ] | 413 | ] |
| 413 | 414 | ||
Cargo.tomlmodified+1 -0
| @@ -30,6 +30,7 @@ ec-ogg = { path = "crates/ec-ogg", version = "0.1.0" } | |||
| 30 | ec-opus = { path = "crates/ec-opus", version = "0.1.0" } | 30 | ec-opus = { path = "crates/ec-opus", version = "0.1.0" } |
| 31 | ec-probe = { path = "crates/ec-probe", version = "0.1.0" } | 31 | ec-probe = { path = "crates/ec-probe", version = "0.1.0" } |
| 32 | ec-riff = { path = "crates/ec-riff", version = "0.1.0" } | 32 | ec-riff = { path = "crates/ec-riff", version = "0.1.0" } |
| 33 | +ec-truehd = { path = "crates/ec-truehd", version = "0.1.0" } | ||
| 33 | ec-va = { path = "crates/ec-va", version = "0.1.0" } | 34 | ec-va = { path = "crates/ec-va", version = "0.1.0" } |
| 34 | ec-vorbis = { path = "crates/ec-vorbis", version = "0.1.0" } | 35 | ec-vorbis = { path = "crates/ec-vorbis", version = "0.1.0" } |
| 35 | ec-vp9-syntax = { path = "crates/ec-vp9-syntax", version = "0.1.0" } | 36 | ec-vp9-syntax = { path = "crates/ec-vp9-syntax", version = "0.1.0" } |
crates/ec-matroska/src/demux.rsmodified+0 -2
| @@ -1118,8 +1118,6 @@ pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Option<CodecId> { | |||
| 1118 | "A_VORBIS" => CodecId::Vorbis, | 1118 | "A_VORBIS" => CodecId::Vorbis, |
| 1119 | "A_AC3" => CodecId::Ac3, | 1119 | "A_AC3" => CodecId::Ac3, |
| 1120 | "A_EAC3" => CodecId::EAc3, | 1120 | "A_EAC3" => CodecId::EAc3, |
| 1121 | - // Named rather than dropped: nothing here decodes them, and a track a | ||
| 1122 | - // player lists as unsupported is a better answer than a silent one. | ||
| 1123 | "A_TRUEHD" | "A_MLP" => CodecId::TrueHd, | 1121 | "A_TRUEHD" | "A_MLP" => CodecId::TrueHd, |
| 1124 | id if id.starts_with("A_DTS") => CodecId::Dts, | 1122 | id if id.starts_with("A_DTS") => CodecId::Dts, |
| 1125 | "A_FLAC" => CodecId::Flac, | 1123 | "A_FLAC" => CodecId::Flac, |
crates/ec-matroska/src/mux.rsmodified+1 -0
| @@ -676,6 +676,7 @@ pub(crate) fn matroska_codec_id(codec: CodecId) -> Result<&'static str> { | |||
| 676 | CodecId::Aac => "A_AAC", | 676 | CodecId::Aac => "A_AAC", |
| 677 | CodecId::Ac3 => "A_AC3", | 677 | CodecId::Ac3 => "A_AC3", |
| 678 | CodecId::EAc3 => "A_EAC3", | 678 | CodecId::EAc3 => "A_EAC3", |
| 679 | + CodecId::TrueHd => "A_TRUEHD", | ||
| 679 | CodecId::Alac => "A_ALAC", | 680 | CodecId::Alac => "A_ALAC", |
| 680 | CodecId::Flac => "A_FLAC", | 681 | CodecId::Flac => "A_FLAC", |
| 681 | CodecId::Mp3 => "A_MPEG/L3", | 682 | CodecId::Mp3 => "A_MPEG/L3", |
crates/ec-matroska/tests/matroska.rsmodified+36 -0
| @@ -49,6 +49,42 @@ fn aac_priming_round_trips_through_codec_delay() { | |||
| 49 | ); | 49 | ); |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | +/// A TrueHD track written by this crate's own muxer reads back as TrueHD: | ||
| 53 | +/// `A_TRUEHD` round trips through demux -> mux -> demux with the packets | ||
| 54 | +/// intact. | ||
| 55 | +#[test] | ||
| 56 | +fn truehd_codec_id_round_trips_through_remux() { | ||
| 57 | + let rate = TimeBase::from_rate(48_000); | ||
| 58 | + let mut info = StreamInfo::new(0, rate, ec_core::CodecParameters::new(CodecId::TrueHd)); | ||
| 59 | + if let MediaParameters::Audio(a) = &mut info.params.media { | ||
| 60 | + a.sample_rate = 48_000; | ||
| 61 | + a.layout = ec_core::ChannelLayout::Surround7_1; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + let src = work().join("truehd-source.mkv"); | ||
| 65 | + let mut muxer = MatroskaMuxer::new(File::create(&src).expect("output")); | ||
| 66 | + let stream = muxer.add_stream(info).expect("stream declared"); | ||
| 67 | + for i in 0..5i64 { | ||
| 68 | + let mut packet = Packet::new(stream, rate, vec![0xAAu8; 32]); | ||
| 69 | + packet.pts = Some(i * 40); | ||
| 70 | + packet.flags.keyframe = true; | ||
| 71 | + muxer.write_packet(&packet).expect("packet written"); | ||
| 72 | + } | ||
| 73 | + muxer.finish().expect("finished"); | ||
| 74 | + | ||
| 75 | + let dst = work().join("truehd-remuxed.mkv"); | ||
| 76 | + let packets = remux(&src, &dst); | ||
| 77 | + assert_eq!(packets, 5, "every packet carried across the remux"); | ||
| 78 | + | ||
| 79 | + let demuxer = | ||
| 80 | + MatroskaDemuxer::new(BufReader::new(File::open(&dst).expect("opens"))).expect("reopens"); | ||
| 81 | + assert_eq!( | ||
| 82 | + demuxer.streams()[0].params.codec, | ||
| 83 | + CodecId::TrueHd, | ||
| 84 | + "A_TRUEHD survives demux -> mux -> demux" | ||
| 85 | + ); | ||
| 86 | +} | ||
| 87 | + | ||
| 52 | fn fixtures() -> PathBuf { | 88 | fn fixtures() -> PathBuf { |
| 53 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures") | 89 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures") |
| 54 | } | 90 | } |
crates/ec-probe/Cargo.tomlmodified+1 -0
| @@ -18,4 +18,5 @@ ec-mp4 = { workspace = true } | |||
| 18 | ec-ogg = { workspace = true } | 18 | ec-ogg = { workspace = true } |
| 19 | ec-opus = { workspace = true } | 19 | ec-opus = { workspace = true } |
| 20 | ec-riff = { workspace = true } | 20 | ec-riff = { workspace = true } |
| 21 | +ec-truehd = { workspace = true } | ||
| 21 | ec-vorbis = { workspace = true } | 22 | ec-vorbis = { workspace = true } |
crates/ec-probe/src/decoder.rsmodified+5 -0
| @@ -18,6 +18,7 @@ enum Inner { | |||
| 18 | Mp3(Box<ec_mp3::Mp3Decoder>), | 18 | Mp3(Box<ec_mp3::Mp3Decoder>), |
| 19 | Vorbis(Box<ec_vorbis::VorbisDecoder>), | 19 | Vorbis(Box<ec_vorbis::VorbisDecoder>), |
| 20 | Ac3(Box<ec_ac3::Ac3Decoder>), | 20 | Ac3(Box<ec_ac3::Ac3Decoder>), |
| 21 | + TrueHd(Box<ec_truehd::TrueHdDecoder>), | ||
| 21 | Aac(Box<ec_aac::AacDecoder>), | 22 | Aac(Box<ec_aac::AacDecoder>), |
| 22 | Alac(Box<ec_alac::AlacDecoder>), | 23 | Alac(Box<ec_alac::AlacDecoder>), |
| 23 | Opus(Box<ec_opus::MultistreamDecoder>), | 24 | Opus(Box<ec_opus::MultistreamDecoder>), |
| @@ -99,6 +100,7 @@ impl AudioDecoder { | |||
| 99 | Inner::Vorbis(Box::new(ec_vorbis::VorbisDecoder::new(&headers)?)) | 100 | Inner::Vorbis(Box::new(ec_vorbis::VorbisDecoder::new(&headers)?)) |
| 100 | } | 101 | } |
| 101 | CodecId::Ac3 | CodecId::EAc3 => Inner::Ac3(Box::new(ec_ac3::Ac3Decoder::new())), | 102 | CodecId::Ac3 | CodecId::EAc3 => Inner::Ac3(Box::new(ec_ac3::Ac3Decoder::new())), |
| 103 | + CodecId::TrueHd => Inner::TrueHd(Box::new(ec_truehd::TrueHdDecoder::new())), | ||
| 102 | CodecId::Aac => { | 104 | CodecId::Aac => { |
| 103 | let decoder = match extradata { | 105 | let decoder = match extradata { |
| 104 | Some(asc) => ec_aac::AacDecoder::with_config_bytes(asc)?, | 106 | Some(asc) => ec_aac::AacDecoder::with_config_bytes(asc)?, |
| @@ -175,6 +177,7 @@ impl AudioDecoder { | |||
| 175 | Inner::Mp3(d) => drain(d.as_mut(), packet, out)?, | 177 | Inner::Mp3(d) => drain(d.as_mut(), packet, out)?, |
| 176 | Inner::Vorbis(d) => drain(d.as_mut(), packet, out)?, | 178 | Inner::Vorbis(d) => drain(d.as_mut(), packet, out)?, |
| 177 | Inner::Ac3(d) => drain(d.as_mut(), packet, out)?, | 179 | Inner::Ac3(d) => drain(d.as_mut(), packet, out)?, |
| 180 | + Inner::TrueHd(d) => drain(d.as_mut(), packet, out)?, | ||
| 178 | Inner::Aac(d) => { | 181 | Inner::Aac(d) => { |
| 179 | let audio = d.decode(&packet.data, packet.pts)?; | 182 | let audio = d.decode(&packet.data, packet.pts)?; |
| 180 | self.channels = usize::from(audio.channels).max(1); | 183 | self.channels = usize::from(audio.channels).max(1); |
| @@ -264,6 +267,7 @@ impl AudioDecoder { | |||
| 264 | Inner::Mp3(d) => flush_drain(d.as_mut(), out)?, | 267 | Inner::Mp3(d) => flush_drain(d.as_mut(), out)?, |
| 265 | Inner::Vorbis(d) => flush_drain(d.as_mut(), out)?, | 268 | Inner::Vorbis(d) => flush_drain(d.as_mut(), out)?, |
| 266 | Inner::Ac3(d) => flush_drain(d.as_mut(), out)?, | 269 | Inner::Ac3(d) => flush_drain(d.as_mut(), out)?, |
| 270 | + Inner::TrueHd(d) => flush_drain(d.as_mut(), out)?, | ||
| 267 | // AAC, ALAC, Opus and PCM decode a packet at a time with nothing | 271 | // AAC, ALAC, Opus and PCM decode a packet at a time with nothing |
| 268 | // held back across the end of the file. | 272 | // held back across the end of the file. |
| 269 | Inner::Aac(_) | Inner::Alac(_) | Inner::Opus(_) | Inner::Pcm(_) => {} | 273 | Inner::Aac(_) | Inner::Alac(_) | Inner::Opus(_) | Inner::Pcm(_) => {} |
| @@ -302,6 +306,7 @@ impl AudioDecoder { | |||
| 302 | Inner::Mp3(d) => d.reset(), | 306 | Inner::Mp3(d) => d.reset(), |
| 303 | Inner::Vorbis(d) => d.reset(), | 307 | Inner::Vorbis(d) => d.reset(), |
| 304 | Inner::Ac3(d) => d.reset(), | 308 | Inner::Ac3(d) => d.reset(), |
| 309 | + Inner::TrueHd(d) => d.reset(), | ||
| 305 | Inner::Aac(d) => **d = ec_aac::AacDecoder::new(), | 310 | Inner::Aac(d) => **d = ec_aac::AacDecoder::new(), |
| 306 | Inner::Alac(d) => d.reset(), | 311 | Inner::Alac(d) => d.reset(), |
| 307 | Inner::Opus(d) => d.reset(), | 312 | Inner::Opus(d) => d.reset(), |
crates/ec-probe/src/lib.rsmodified+1 -1
| @@ -20,7 +20,7 @@ | |||
| 20 | //! - **A track this cannot decode is listed, not hidden.** [`Reader::streams`] | 20 | //! - **A track this cannot decode is listed, not hidden.** [`Reader::streams`] |
| 21 | //! reports every stream the container declares and | 21 | //! reports every stream the container declares and |
| 22 | //! [`Reader::unsupported`] names the ones with no decoder *and why* — a | 22 | //! [`Reader::unsupported`] names the ones with no decoder *and why* — a |
| 23 | -//! TrueHD track says "TrueHD", not silence. Cover art is not a video track | 23 | +//! DTS track says "DTS", not silence. Cover art is not a video track |
| 24 | //! and never appears as one. | 24 | //! and never appears as one. |
| 25 | 25 | ||
| 26 | #![forbid(unsafe_code)] | 26 | #![forbid(unsafe_code)] |
crates/ec-probe/tests/library.rsmodified+18 -20
| @@ -193,11 +193,10 @@ fn a_real_library_sweep() { | |||
| 193 | assert!(failures.is_empty(), "{}", failures.join("\n")); | 193 | assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 194 | } | 194 | } |
| 195 | 195 | ||
| 196 | -/// The TrueHD remux in his library: the track is *listed*, with a reason, and | 196 | +/// The TrueHD remux in his library: the track decodes through the unified |
| 197 | -/// the AC-3 track beside it still decodes. A file whose sound is refused | 197 | +/// reader, 7.1 at 48 kHz, with the AC-3 track beside it still decoding too. |
| 198 | -/// silently is the failure this exists to prevent. | ||
| 199 | #[test] | 198 | #[test] |
| 200 | -fn an_undecodable_track_is_named_not_hidden() { | 199 | +fn a_truehd_track_decodes() { |
| 201 | let Some((path, ..)) = manifest() | 200 | let Some((path, ..)) = manifest() |
| 202 | .into_iter() | 201 | .into_iter() |
| 203 | .find(|(p, _, acodecs, _)| acodecs.contains("truehd") && p.exists()) | 202 | .find(|(p, _, acodecs, _)| acodecs.contains("truehd") && p.exists()) |
| @@ -205,18 +204,19 @@ fn an_undecodable_track_is_named_not_hidden() { | |||
| 205 | eprintln!("no TrueHD file in the manifest — skipping"); | 204 | eprintln!("no TrueHD file in the manifest — skipping"); |
| 206 | return; | 205 | return; |
| 207 | }; | 206 | }; |
| 208 | - let reader = Reader::open(&path).expect("open"); | 207 | + let mut reader = Reader::open(&path).expect("open"); |
| 209 | - let unsupported = reader.unsupported(); | 208 | + let truehd = reader |
| 210 | - let truehd = unsupported | 209 | + .streams() |
| 211 | .iter() | 210 | .iter() |
| 212 | - .find(|u| u.codec.name() == "truehd") | 211 | + .find(|s| s.params.codec.name() == "truehd") |
| 213 | - .expect("the TrueHD track is listed as unsupported"); | 212 | + .cloned() |
| 214 | - assert!( | 213 | + .expect("the TrueHD track is listed"); |
| 215 | - truehd.reason.contains("truehd"), | 214 | + let decoder = reader.make_decoder(truehd.index).expect("truehd decoder"); |
| 216 | - "the reason names the codec: {}", | 215 | + assert_eq!(decoder.channels(), 8, "7.1 TrueHD is 8 channels"); |
| 217 | - truehd.reason | 216 | + assert_eq!(decoder.sample_rate(), 48_000); |
| 218 | - ); | 217 | + let frames = decode_some(&mut reader, truehd.index, 1.0).expect("decode"); |
| 219 | - // ...and the other track of the same file plays. | 218 | + assert!(frames > 0, "no TrueHD audio came out"); |
| 219 | + // ...and the AC-3 track beside it still decodes. | ||
| 220 | let decodable: Vec<_> = reader | 220 | let decodable: Vec<_> = reader |
| 221 | .streams() | 221 | .streams() |
| 222 | .iter() | 222 | .iter() |
| @@ -225,14 +225,12 @@ fn an_undecodable_track_is_named_not_hidden() { | |||
| 225 | .map(|s| s.params.codec.name()) | 225 | .map(|s| s.params.codec.name()) |
| 226 | .collect(); | 226 | .collect(); |
| 227 | assert!( | 227 | assert!( |
| 228 | - !decodable.is_empty(), | 228 | + decodable.contains(&"ac3") || decodable.contains(&"truehd"), |
| 229 | - "a file with a TrueHD track and an AC-3 one came out with nothing to play" | 229 | + "a file with a TrueHD track and an AC-3 one came out with nothing to play: {decodable:?}" |
| 230 | ); | 230 | ); |
| 231 | eprintln!( | 231 | eprintln!( |
| 232 | - "{}: {} listed unsupported ({}), decodable: {decodable:?}", | 232 | + "{}: truehd decoded {frames} frames, tracks {decodable:?}", |
| 233 | path.file_name().unwrap().to_string_lossy(), | 233 | path.file_name().unwrap().to_string_lossy(), |
| 234 | - truehd.codec.name(), | ||
| 235 | - truehd.reason | ||
| 236 | ); | 234 | ); |
| 237 | } | 235 | } |
| 238 | 236 | ||