From acbfd4cf6bbc6a44f755e33fd0d790ce5b77903d 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 11:29:17 +0300 Subject: [PATCH] ec-ac3: place a dependent substream's channels like ffmpeg: the k-th channel-map entry takes output slot ff_ac3_dec_channel_map[acmod][lfeon][k] (ac3dec.c extended_channel_map); the_great_wall_7.1 (3/1 dependent, map Ls Rs Lrs/Rrs) had BL and SR swapped vs ffmpeg --- crates/ec-ac3/src/decoder.rs | 2 +- crates/ec-ac3/src/dependent.rs | 47 ++++++++++++++++++++++++++-------- crates/ec-ac3/src/mantissa.rs | 2 +- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/crates/ec-ac3/src/decoder.rs b/crates/ec-ac3/src/decoder.rs index 912c293e..939912e2 100644 --- a/crates/ec-ac3/src/decoder.rs +++ b/crates/ec-ac3/src/decoder.rs @@ -342,7 +342,7 @@ impl Ac3Decoder { ))); } let channels = hdr.nfchans + usize::from(hdr.bsi.lfeon); - let positions = dependent::positions(hdr.chanmap, channels)?; + let positions = dependent::positions(hdr.chanmap, channels, hdr.bsi.acmod, hdr.bsi.lfeon)?; if self.dependents.len() <= slot { self.dependents.push(Core::new()); } diff --git a/crates/ec-ac3/src/dependent.rs b/crates/ec-ac3/src/dependent.rs index 5a503f0a..3078a53a 100644 --- a/crates/ec-ac3/src/dependent.rs +++ b/crates/ec-ac3/src/dependent.rs @@ -28,6 +28,7 @@ use ec_core::{BitReader, ChannelPosition, Error, Result}; +use crate::bsi::Acmod; use crate::eac3::{self, Eac3Bsi, StreamType}; use crate::syncinfo::SYNCWORD; @@ -54,7 +55,7 @@ pub(crate) fn peek(data: &[u8]) -> Option { /// The loudspeaker positions a dependent substream's `channels` coded channels /// fill, in coded order, from its custom channel map (Table E2.5). -pub(crate) fn positions(map: Option, channels: usize) -> Result> { +pub(crate) fn positions(map: Option, channels: usize, acmod: Acmod, lfeon: bool) -> Result> { use ChannelPosition::*; let Some(map) = map else { return Err(Error::unsupported( @@ -99,7 +100,31 @@ pub(crate) fn positions(map: Option, channels: usize) -> Result &'static [u8] { + match (acmod, lfeon) { + (Acmod::DualMono | Acmod::Stereo, false) => &[0, 1], + (Acmod::DualMono | Acmod::Stereo, true) => &[0, 1, 2], + (Acmod::Mono, false) => &[0], + (Acmod::Mono, true) => &[0, 1], + (Acmod::Surround3_0, false) => &[0, 2, 1], + (Acmod::Surround3_0, true) => &[0, 2, 1, 3], + (Acmod::Surround2_1, false) => &[0, 1, 2], + (Acmod::Surround2_1, true) => &[0, 1, 3, 2], + (Acmod::Surround3_1, false) => &[0, 2, 1, 3], + (Acmod::Surround3_1, true) => &[0, 2, 1, 4, 3], + (Acmod::Surround2_2, false) => &[0, 1, 2, 3], + (Acmod::Surround2_2, true) => &[0, 1, 4, 2, 3], + (Acmod::Surround3_2, false) => &[0, 2, 1, 3, 4], + (Acmod::Surround3_2, true) => &[0, 2, 1, 5, 3, 4], + } } /// The Table E2.5 name of the map entry `i` that this build cannot place. @@ -188,26 +213,26 @@ mod tests { #[test] fn map_bits_name_positions_as_ffmpeg_does() { - assert_eq!(positions(Some(0x8000), 1).unwrap(), vec![FrontLeft]); - assert_eq!(positions(Some(0x0400), 2).unwrap(), vec![FrontLeftOfCenter, FrontRightOfCenter]); - assert_eq!(positions(Some(0x0200), 2).unwrap(), vec![BackLeft, BackRight]); - assert_eq!(positions(Some(0x1800), 2).unwrap(), vec![SideLeft, SideRight]); - assert_eq!(positions(Some(0x0001), 1).unwrap(), vec![Lfe]); - assert_eq!(positions(Some(0x8400), 3).unwrap(), vec![FrontLeft, FrontLeftOfCenter, FrontRightOfCenter]); + assert_eq!(positions(Some(0x8000), 1, Acmod::Mono, false).unwrap(), vec![FrontLeft]); + assert_eq!(positions(Some(0x0400), 2, Acmod::Stereo, false).unwrap(), vec![FrontLeftOfCenter, FrontRightOfCenter]); + assert_eq!(positions(Some(0x0200), 2, Acmod::Stereo, false).unwrap(), vec![BackLeft, BackRight]); + assert_eq!(positions(Some(0x1800), 2, Acmod::Stereo, false).unwrap(), vec![SideLeft, SideRight]); + assert_eq!(positions(Some(0x0001), 1, Acmod::Mono, false).unwrap(), vec![Lfe]); + assert_eq!(positions(Some(0x8400), 3, Acmod::Surround2_1, false).unwrap(), vec![FrontLeft, FrontLeftOfCenter, FrontRightOfCenter]); } #[test] fn unnamed_and_missing_maps_are_refused_by_name() { for map in [None, Some(0x0080), Some(0x0040), Some(0x0010), Some(0x0004), Some(0x0002)] { - let err = positions(map, 2).expect_err("refused"); + let err = positions(map, 2, Acmod::Stereo, false).expect_err("refused"); assert!(matches!(err, Error::Unsupported { .. }), "{map:?}: {err}"); } } #[test] fn map_that_disagrees_with_the_channel_count_is_corrupt() { - assert!(matches!(positions(Some(0x1800), 1), Err(Error::Corrupt { .. }))); - assert!(matches!(positions(Some(0x0400), 3), Err(Error::Corrupt { .. }))); + assert!(matches!(positions(Some(0x1800), 1, Acmod::Mono, false), Err(Error::Corrupt { .. }))); + assert!(matches!(positions(Some(0x0400), 3, Acmod::Surround2_1, false), Err(Error::Corrupt { .. }))); } #[test] diff --git a/crates/ec-ac3/src/mantissa.rs b/crates/ec-ac3/src/mantissa.rs index 7960c797..811945e6 100644 --- a/crates/ec-ac3/src/mantissa.rs +++ b/crates/ec-ac3/src/mantissa.rs @@ -50,7 +50,7 @@ impl Default for Dither { } impl Dither { - pub fn value(&mut self) -> f32 { + pub(crate) fn value(&mut self) -> f32 { let x = self.next_word() & 0x7f_ffff; (x as i32 - 0x40_0000) as f32 / 8_388_608.0 }