Spec limiter band table (0.49/limBands octave thinning, 1.2/2/3 bands per octave, patch borders preferred)
Browse files at this commit0f6c7133a4240ab657b72206bf3511692ceab84eParent: 40bfd30- fix
- spec limiter band table (0.49/limBands octave thinning, 1.2/2/3 bands per octave, patch borders preferred)
1 changed file +27 -38
- crates/ec-aac/src/sbr_hf.rs +27 -38
crates/ec-aac/src/sbr_hf.rsmodified+27 -38
| @@ -188,17 +188,11 @@ pub fn build_patches(tables: &BandTables) -> Vec<Patch> { | |||
| 188 | patches | 188 | patches |
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | -/// Builds the limiter band table (ISO/IEC 14496-3 §4.6.18.7.2): the union | 191 | +/// Builds the limiter band table (ISO/IEC 14496-3 §4.6.18.7.2, Figure 4.33): |
| 192 | -/// of the low-resolution envelope band borders (`tables.f_low`) and the | 192 | +/// the union of `f_low` and the patch borders, sorted, then thinned so two |
| 193 | -/// patch target boundaries, thinned so no band is narrower than one | 193 | +/// borders closer than `0.49/limBands` octaves collapse (`bs_limiter_bands` |
| 194 | -/// limiter band's worth of octaves at the transmitted `bs_limiter_bands` | 194 | +/// 1/2/3 => 1.2/2/3 bands per octave), keeping patch borders over |
| 195 | -/// density (`0` => a single band spanning the whole HF region; `1`/`2`/`3` | 195 | +/// envelope borders when one of the pair must go. `0` => one band. |
| 196 | -/// => ~1/2/3 bands per octave, log2-spaced the same way [`f_master`]'s own | ||
| 197 | -/// band construction thins its candidate borders). The limiter's gain cap | ||
| 198 | -/// is applied per band on this table, aggregated over every cell it | ||
| 199 | -/// covers, so a content-empty target cell inherits its band's aggregate | ||
| 200 | -/// cap instead of amplifying numeric dust toward an unbounded per-cell | ||
| 201 | -/// ratio. | ||
| 202 | pub fn limiter_band_table(tables: &BandTables, patches: &[Patch], limiter_bands: u8) -> Vec<i64> { | 196 | pub fn limiter_band_table(tables: &BandTables, patches: &[Patch], limiter_bands: u8) -> Vec<i64> { |
| 203 | let kx = tables.kx; | 197 | let kx = tables.kx; |
| 204 | let k2 = tables.k2; | 198 | let k2 = tables.k2; |
| @@ -208,36 +202,31 @@ pub fn limiter_band_table(tables: &BandTables, patches: &[Patch], limiter_bands: | |||
| 208 | if limiter_bands == 0 { | 202 | if limiter_bands == 0 { |
| 209 | return vec![kx, k2]; | 203 | return vec![kx, k2]; |
| 210 | } | 204 | } |
| 211 | - let bands_per_octave = match limiter_bands { | 205 | + let warped = match limiter_bands { |
| 212 | - 1 => 1.0, | 206 | + 1 => 2f64.powf(0.49 / 1.2), |
| 213 | - 2 => 2.0, | 207 | + 2 => 2f64.powf(0.49 / 2.0), |
| 214 | - _ => 3.0, | 208 | + _ => 2f64.powf(0.49 / 3.0), |
| 215 | }; | 209 | }; |
| 216 | - let mut borders: Vec<i64> = tables | 210 | + let mut patch_borders: Vec<i64> = vec![kx]; |
| 217 | - .f_low | 211 | + for p in patches { |
| 218 | - .iter() | 212 | + patch_borders.push(patch_borders.last().unwrap() + p.width as i64); |
| 219 | - .copied() | 213 | + } |
| 220 | - .chain(patches.iter().map(|p| p.target_start as i64)) | 214 | + let mut table: Vec<i64> = tables.f_low.clone(); |
| 221 | - .chain(std::iter::once(k2)) | 215 | + table.extend(patch_borders.iter().skip(1).take(patches.len().saturating_sub(1))); |
| 222 | - .chain(std::iter::once(kx)) | 216 | + table.sort_unstable(); |
| 223 | - .filter(|&b| b >= kx && b <= k2) | 217 | + let is_patch = |b: i64| patch_borders.contains(&b); |
| 224 | - .collect(); | 218 | + let mut out: Vec<i64> = vec![table[0]]; |
| 225 | - borders.sort_unstable(); | 219 | + for &b in &table[1..] { |
| 226 | - borders.dedup(); | ||
| 227 | - let mut out = vec![borders[0]]; | ||
| 228 | - for &b in &borders[1..] { | ||
| 229 | let last = *out.last().unwrap(); | 220 | let last = *out.last().unwrap(); |
| 230 | - if b <= last { | 221 | + if (b as f64) >= (last as f64) * warped { |
| 231 | - continue; | 222 | + out.push(b); |
| 232 | - } | 223 | + } else if b == last || !is_patch(b) { |
| 233 | - let octaves = (b as f64 / last as f64).log2(); | 224 | + // drop the incoming border |
| 234 | - if octaves * bands_per_octave < 1.0 && b != k2 { | 225 | + } else if !is_patch(last) { |
| 235 | - continue; // too narrow on its own: merges into the current band | 226 | + *out.last_mut().unwrap() = b; |
| 227 | + } else { | ||
| 228 | + out.push(b); | ||
| 236 | } | 229 | } |
| 237 | - out.push(b); | ||
| 238 | - } | ||
| 239 | - if *out.last().unwrap() != k2 { | ||
| 240 | - out.push(k2); | ||
| 241 | } | 230 | } |
| 242 | out | 231 | out |
| 243 | } | 232 | } |