S10: aac apply_data: slot analysis and band merge extracted to analyze_plane and merge_bands (verbatim, same order)
Browse files at this commitd3338f0b82828c1ce9ea0a85a04399424780169bParent: cc3e7cc1 changed file +56 -36
- crates/ec-aac/src/sbr_chain.rs +56 -36
crates/ec-aac/src/sbr_chain.rsmodified+56 -36
| @@ -377,31 +377,7 @@ impl SbrChain { | |||
| 377 | continue; | 377 | continue; |
| 378 | } | 378 | } |
| 379 | let state = &mut elem.channels[ch]; | 379 | let state = &mut elem.channels[ch]; |
| 380 | - let mut low_cur = vec![vec![Complex::ZERO; num_slots]; ANALYSIS_BANDS]; | 380 | + let (low_cur, raw) = analyze_plane(state, plane, num_slots); |
| 381 | - let mut raw = Vec::with_capacity(num_slots); | ||
| 382 | - for slot in 0..num_slots { | ||
| 383 | - let mut chunk = [0f32; ANALYSIS_BANDS]; | ||
| 384 | - chunk.copy_from_slice(&plane[slot * ANALYSIS_BANDS..(slot + 1) * ANALYSIS_BANDS]); | ||
| 385 | - // `plane` is already at the decoder's final output scale | ||
| 386 | - // (`decode::OUTPUT_SCALE`, applied by the core filterbank), | ||
| 387 | - // but the transmitted envelope/noise data is calibrated | ||
| 388 | - // against the core's un-normalized internal PCM domain -- | ||
| 389 | - // undo that scale before analysis so the SBR gain match | ||
| 390 | - // (raw QMF energy vs. the transmitted target) compares | ||
| 391 | - // like against like, and reapply it once below after | ||
| 392 | - // synthesis. Without this the gain step's `target/current` | ||
| 393 | - // ratio is inflated by `1/OUTPUT_SCALE^2` in energy, | ||
| 394 | - // exactly the multi-order-of-magnitude blowup this chain | ||
| 395 | - // used to produce on every real HE-AAC file. | ||
| 396 | - for s in &mut chunk { | ||
| 397 | - *s /= crate::decode::OUTPUT_SCALE; | ||
| 398 | - } | ||
| 399 | - let sub = state.analysis.process_slot(&chunk); | ||
| 400 | - for b in 0..ANALYSIS_BANDS { | ||
| 401 | - low_cur[b][slot] = sub[b]; | ||
| 402 | - } | ||
| 403 | - raw.push(sub); | ||
| 404 | - } | ||
| 405 | 381 | ||
| 406 | let sbr_ch = &data.channels[ch]; | 382 | let sbr_ch = &data.channels[ch]; |
| 407 | let hf = sbr_hf::generate( | 383 | let hf = sbr_hf::generate( |
| @@ -481,17 +457,7 @@ impl SbrChain { | |||
| 481 | 457 | ||
| 482 | let kx = (tables.kx as usize).min(ANALYSIS_BANDS); | 458 | let kx = (tables.kx as usize).min(ANALYSIS_BANDS); |
| 483 | let k2 = (tables.k2 as usize).min(band_limit); | 459 | let k2 = (tables.k2 as usize).min(band_limit); |
| 484 | - let mut vs: Vec<[Complex<f64>; SYNTHESIS_BANDS]> = Vec::with_capacity(num_slots); | 460 | + let vs = merge_bands(&raw, &hf, kx, k2, num_slots); |
| 485 | - for slot in 0..num_slots { | ||
| 486 | - let mut v = [Complex::ZERO; SYNTHESIS_BANDS]; | ||
| 487 | - v[0..kx].copy_from_slice(&raw[slot][0..kx]); | ||
| 488 | - for b in kx..k2 { | ||
| 489 | - if b - kx < hf.len() { | ||
| 490 | - v[b] = hf[b - kx][slot]; | ||
| 491 | - } | ||
| 492 | - } | ||
| 493 | - vs.push(v); | ||
| 494 | - } | ||
| 495 | let out = match (ps_mode && num_slots == PS_SLOTS, data.ps.as_ref()) { | 461 | let out = match (ps_mode && num_slots == PS_SLOTS, data.ps.as_ref()) { |
| 496 | (true, Some(ps)) => { | 462 | (true, Some(ps)) => { |
| 497 | let (left, right) = | 463 | let (left, right) = |
| @@ -530,6 +496,60 @@ impl SbrChain { | |||
| 530 | } | 496 | } |
| 531 | } | 497 | } |
| 532 | 498 | ||
| 499 | +fn analyze_plane( | ||
| 500 | + state: &mut ChannelState, | ||
| 501 | + plane: &[f32], | ||
| 502 | + num_slots: usize, | ||
| 503 | +) -> (Vec<Vec<Complex<f64>>>, Vec<[Complex<f64>; ANALYSIS_BANDS]>) { | ||
| 504 | + let mut low_cur = vec![vec![Complex::ZERO; num_slots]; ANALYSIS_BANDS]; | ||
| 505 | + let mut raw = Vec::with_capacity(num_slots); | ||
| 506 | + for slot in 0..num_slots { | ||
| 507 | + let mut chunk = [0f32; ANALYSIS_BANDS]; | ||
| 508 | + chunk.copy_from_slice(&plane[slot * ANALYSIS_BANDS..(slot + 1) * ANALYSIS_BANDS]); | ||
| 509 | + // `plane` is already at the decoder's final output scale | ||
| 510 | + // (`decode::OUTPUT_SCALE`, applied by the core filterbank), | ||
| 511 | + // but the transmitted envelope/noise data is calibrated | ||
| 512 | + // against the core's un-normalized internal PCM domain -- | ||
| 513 | + // undo that scale before analysis so the SBR gain match | ||
| 514 | + // (raw QMF energy vs. the transmitted target) compares | ||
| 515 | + // like against like, and reapply it once below after | ||
| 516 | + // synthesis. Without this the gain step's `target/current` | ||
| 517 | + // ratio is inflated by `1/OUTPUT_SCALE^2` in energy, | ||
| 518 | + // exactly the multi-order-of-magnitude blowup this chain | ||
| 519 | + // used to produce on every real HE-AAC file. | ||
| 520 | + for s in &mut chunk { | ||
| 521 | + *s /= crate::decode::OUTPUT_SCALE; | ||
| 522 | + } | ||
| 523 | + let sub = state.analysis.process_slot(&chunk); | ||
| 524 | + for b in 0..ANALYSIS_BANDS { | ||
| 525 | + low_cur[b][slot] = sub[b]; | ||
| 526 | + } | ||
| 527 | + raw.push(sub); | ||
| 528 | + } | ||
| 529 | + (low_cur, raw) | ||
| 530 | +} | ||
| 531 | + | ||
| 532 | +fn merge_bands( | ||
| 533 | + raw: &[[Complex<f64>; ANALYSIS_BANDS]], | ||
| 534 | + hf: &[Vec<Complex<f64>>], | ||
| 535 | + kx: usize, | ||
| 536 | + k2: usize, | ||
| 537 | + num_slots: usize, | ||
| 538 | +) -> Vec<[Complex<f64>; SYNTHESIS_BANDS]> { | ||
| 539 | + let mut vs: Vec<[Complex<f64>; SYNTHESIS_BANDS]> = Vec::with_capacity(num_slots); | ||
| 540 | + for slot in 0..num_slots { | ||
| 541 | + let mut v = [Complex::ZERO; SYNTHESIS_BANDS]; | ||
| 542 | + v[0..kx].copy_from_slice(&raw[slot][0..kx]); | ||
| 543 | + for b in kx..k2 { | ||
| 544 | + if b - kx < hf.len() { | ||
| 545 | + v[b] = hf[b - kx][slot]; | ||
| 546 | + } | ||
| 547 | + } | ||
| 548 | + vs.push(v); | ||
| 549 | + } | ||
| 550 | + vs | ||
| 551 | +} | ||
| 552 | + | ||
| 533 | /// Inserts the second output plane a PS element needs, right after its first | 553 | /// Inserts the second output plane a PS element needs, right after its first |
| 534 | /// (a copy, which the PS path then overwrites). Returns the element's new | 554 | /// (a copy, which the PS path then overwrites). Returns the element's new |
| 535 | /// plane count: 2 when a plane was inserted. | 555 | /// plane count: 2 when a plane was inserted. |