From 5f9bf7648f667952b787c77afde07d23ac7c01de Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Mon, 13 Mar 2023 13:46:59 +0100 Subject: [PATCH] aacsbr: fix high<->low resolution envelope mapping --- nihav-mpeg/src/codecs/aac/sbr/bs.rs | 4 ++-- nihav-mpeg/src/codecs/aac/sbr/mod.rs | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/nihav-mpeg/src/codecs/aac/sbr/bs.rs b/nihav-mpeg/src/codecs/aac/sbr/bs.rs index 74e820d..f12e9d9 100644 --- a/nihav-mpeg/src/codecs/aac/sbr/bs.rs +++ b/nihav-mpeg/src/codecs/aac/sbr/bs.rs @@ -259,8 +259,8 @@ fn read_envelope(br: &mut BitReader, chan: &mut SBRChannel, coupled: bool, cbs: for (i, band_env) in envelope[..num_env_bands].iter_mut().enumerate() { let delta = br.read_cb(t_cb)?; let last = match (freq_res, chan.last_freq_res) { - (true, false) => chan.last_envelope[state.high_to_low_res[i]], - (false, true) => chan.last_envelope[state.low_to_high_res[i]], + (false, true) => chan.last_envelope[state.high_to_low_res[i]], + (true, false) => chan.last_envelope[state.low_to_high_res[i]], _ => chan.last_envelope[i], }; *band_env = last + delta * scale; diff --git a/nihav-mpeg/src/codecs/aac/sbr/mod.rs b/nihav-mpeg/src/codecs/aac/sbr/mod.rs index f337e96..d2477b9 100644 --- a/nihav-mpeg/src/codecs/aac/sbr/mod.rs +++ b/nihav-mpeg/src/codecs/aac/sbr/mod.rs @@ -189,9 +189,10 @@ impl SBRState { } } for (dst, high) in self.low_to_high_res.iter_mut().zip(high_src.iter()) { - match low_src.binary_search(high) { - Ok(idx) | Err(idx) => *dst = idx, - }; + *dst = match low_src.binary_search(high) { + Ok(idx) => idx, + Err(idx) => idx - 1, + }; } let num_q = (((hdr.noise_bands as f32) * ((k2 as f32) / (k_x as f32)).log2()).round() as usize).max(1); -- 2.39.5