edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·5c8eff8·Tahinli·2 months ago·2949 commits
| 1 | //! Enhanced AC-3 (A/52 Annex E): the header, the frame-level strategy fields |
| 2 | //! and the audio block syntax that differ from AC-3. |
| 3 | //! |
| 4 | //! Everything after parsing — bit allocation, mantissas, coupling, the inverse |
| 5 | //! transform — is the AC-3 machinery in [`crate::decode`], because Annex E |
| 6 | //! changes how the parameters are *sent*, not what they mean. Of what Annex E |
| 7 | //! adds on top, the adaptive hybrid transform lives in [`crate::aht`] and |
| 8 | //! spectral extension in this module; enhanced coupling is refused by name |
| 9 | //! until a stream that uses it turns up. |
| 10 | |
| 11 | use ec_core::{BitReader, Error, Result}; |
| 12 | |
| 13 | use crate::bitalloc::{BitAllocParams, DeltaBa}; |
| 14 | use crate::bsi::{Acmod, Bsi}; |
| 15 | use crate::decode::{CHANNELS, COEFFS, CPL, Core, LFE, Syntax}; |
| 16 | use crate::exps::{self, Strategy}; |
| 17 | |
| 18 | pub mod bsi { |
| 19 | //! `bsi()` of Annex E, shaped like [`crate::bsi`] so a caller can reach |
| 20 | //! either one the same way. |
| 21 | pub use super::{Eac3Bsi, parse, parse_from}; |
| 22 | } |
| 23 | |
| 24 | /// Blocks per syncframe by `numblkscod` (Table E2.4). |
| 25 | const BLOCKS: [usize; 4] = [1, 2, 3, 6]; |
| 26 | |
| 27 | /// Reduced sample rates by `fscod2` (Table E2.3). |
| 28 | const HALF_RATE: [u32; 4] = [24_000, 22_050, 16_000, 0]; |
| 29 | |
| 30 | /// Default coupling banding structure, `defcplbndstrc[]` (Table E2.12). |
| 31 | const DEFAULT_CPLBNDSTRC: [bool; 18] = [ |
| 32 | false, false, false, false, false, false, false, false, true, false, true, true, false, true, |
| 33 | true, true, true, true, |
| 34 | ]; |
| 35 | |
| 36 | /// Default spectral extension banding structure, `defspxbndstrc[]` (Table |
| 37 | /// E2.11): every other sub-band from 8 upwards joins the band below it. |
| 38 | const DEFAULT_SPXBNDSTRC: [bool; 18] = [ |
| 39 | false, false, false, false, false, false, false, false, true, false, true, false, true, false, |
| 40 | true, false, true, false, |
| 41 | ]; |
| 42 | |
| 43 | /// Spectral extension attenuation, `spxattentab[]` (Table E3.14). Only the |
| 44 | /// first three taps of the symmetric 5-tap notch are stored. |
| 45 | const SPXATTENTAB: [[f32; 3]; 32] = [ |
| 46 | [0.9548416, 0.9117225, 0.8705506], |
| 47 | [0.9117225, 0.8312379, 0.7578583], |
| 48 | [0.8705506, 0.7578583, 0.659754], |
| 49 | [0.8312379, 0.6909564, 0.5743492], |
| 50 | [0.7937005, 0.6299605, 0.5], |
| 51 | [0.7578583, 0.5743492, 0.4352753], |
| 52 | [0.7236346, 0.5236471, 0.3789291], |
| 53 | [0.6909564, 0.4774208, 0.329877], |
| 54 | [0.659754, 0.4352753, 0.2871746], |
| 55 | [0.6299605, 0.3968503, 0.25], |
| 56 | [0.6015125, 0.3618173, 0.2176376], |
| 57 | [0.5743492, 0.329877, 0.1894646], |
| 58 | [0.5484125, 0.3007563, 0.1649385], |
| 59 | [0.5236471, 0.2742062, 0.1435873], |
| 60 | [0.5, 0.25, 0.125], |
| 61 | [0.4774208, 0.2279306, 0.1088188], |
| 62 | [0.4558612, 0.2078095, 0.0947323], |
| 63 | [0.4352753, 0.1894646, 0.0824692], |
| 64 | [0.4156189, 0.1727391, 0.0717936], |
| 65 | [0.3968503, 0.1574901, 0.0625], |
| 66 | [0.3789291, 0.1435873, 0.0544094], |
| 67 | [0.3618173, 0.1309118, 0.0473661], |
| 68 | [0.3454782, 0.1193552, 0.0412346], |
| 69 | [0.329877, 0.1088188, 0.0358968], |
| 70 | [0.3149803, 0.0992126, 0.03125], |
| 71 | [0.3007563, 0.0904543, 0.0272047], |
| 72 | [0.2871746, 0.0824692, 0.0236831], |
| 73 | [0.2742062, 0.0751891, 0.0206173], |
| 74 | [0.2618235, 0.0685516, 0.0179484], |
| 75 | [0.25, 0.0625, 0.015625], |
| 76 | [0.2387104, 0.0569827, 0.0136024], |
| 77 | [0.2279306, 0.0519524, 0.0118415], |
| 78 | ]; |
| 79 | |
| 80 | /// First transform coefficient of spectral extension sub-band `sbnd` |
| 81 | /// (Table E3.13): 17 sub-bands of 12 coefficients from #25. |
| 82 | fn spx_band_start(sbnd: usize) -> usize { |
| 83 | 25 + 12 * sbnd |
| 84 | } |
| 85 | |
| 86 | /// Frame exponent strategy combinations (Table E2.10), as strategy codes. |
| 87 | const FRAME_EXP_STRATEGY: [[u8; 6]; 32] = [ |
| 88 | [1, 0, 0, 0, 0, 0], |
| 89 | [1, 0, 0, 0, 0, 3], |
| 90 | [1, 0, 0, 0, 2, 0], |
| 91 | [1, 0, 0, 0, 3, 3], |
| 92 | [2, 0, 0, 2, 0, 0], |
| 93 | [2, 0, 0, 2, 0, 3], |
| 94 | [2, 0, 0, 3, 2, 0], |
| 95 | [2, 0, 0, 3, 3, 3], |
| 96 | [2, 0, 1, 0, 0, 0], |
| 97 | [2, 0, 2, 0, 0, 3], |
| 98 | [2, 0, 2, 0, 2, 0], |
| 99 | [2, 0, 2, 0, 3, 3], |
| 100 | [2, 0, 3, 2, 0, 0], |
| 101 | [2, 0, 3, 2, 0, 3], |
| 102 | [2, 0, 3, 3, 2, 0], |
| 103 | [2, 0, 3, 3, 3, 3], |
| 104 | [3, 1, 0, 0, 0, 0], |
| 105 | [3, 1, 0, 0, 0, 3], |
| 106 | [3, 2, 0, 0, 2, 0], |
| 107 | [3, 2, 0, 0, 3, 3], |
| 108 | [3, 2, 0, 2, 0, 0], |
| 109 | [3, 2, 0, 2, 0, 3], |
| 110 | [3, 2, 0, 3, 2, 0], |
| 111 | [3, 2, 0, 3, 3, 3], |
| 112 | [3, 3, 1, 0, 0, 0], |
| 113 | [3, 3, 2, 0, 0, 3], |
| 114 | [3, 3, 2, 0, 2, 0], |
| 115 | [3, 3, 2, 0, 3, 3], |
| 116 | [3, 3, 3, 2, 0, 0], |
| 117 | [3, 3, 3, 2, 0, 3], |
| 118 | [3, 3, 3, 3, 2, 0], |
| 119 | [3, 3, 3, 3, 3, 3], |
| 120 | ]; |
| 121 | |
| 122 | /// Which substream a frame belongs to (Table E2.1). |
| 123 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 124 | pub enum StreamType { |
| 125 | /// Independent substream: decodable on its own. |
| 126 | Independent, |
| 127 | /// Dependent substream: channel extensions for the independent one before |
| 128 | /// it. |
| 129 | Dependent, |
| 130 | /// An AC-3 stream carried in Annex E framing. |
| 131 | Ac3Converted, |
| 132 | /// Reserved. |
| 133 | Reserved, |
| 134 | } |
| 135 | |
| 136 | impl StreamType { |
| 137 | fn from_code(code: u32) -> StreamType { |
| 138 | match code & 3 { |
| 139 | 0 => StreamType::Independent, |
| 140 | 1 => StreamType::Dependent, |
| 141 | 2 => StreamType::Ac3Converted, |
| 142 | _ => StreamType::Reserved, |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /// Everything the Annex E `bsi()` states. |
| 148 | #[derive(Debug, Clone, PartialEq)] |
| 149 | pub struct Eac3Bsi { |
| 150 | /// Substream type. |
| 151 | pub strmtyp: StreamType, |
| 152 | /// Substream identifier, 0..=7. |
| 153 | pub substreamid: u8, |
| 154 | /// Frame size code: the frame is `(frmsiz + 1) * 2` bytes. |
| 155 | pub frmsiz: u16, |
| 156 | /// Frame size in bytes. |
| 157 | pub frame_size: usize, |
| 158 | /// Sample rate code; 3 means the reduced rates of `fscod2`. |
| 159 | pub fscod: u8, |
| 160 | /// Reduced sample rate code, when `fscod == 3`. |
| 161 | pub fscod2: Option<u8>, |
| 162 | /// Sample rate in Hz. |
| 163 | pub sample_rate: u32, |
| 164 | /// Number of blocks per syncframe code. |
| 165 | pub numblkscod: u8, |
| 166 | /// Blocks in this syncframe: 1, 2, 3 or 6. |
| 167 | pub nblocks: usize, |
| 168 | /// Custom channel map of a dependent substream (Table E2.5). |
| 169 | pub chanmap: Option<u16>, |
| 170 | /// The fields Annex E shares with AC-3, so one caller can read either. |
| 171 | pub bsi: Bsi, |
| 172 | /// Full-bandwidth channels, LFE excluded — mirrors `bsi.nfchans`. |
| 173 | pub nfchans: usize, |
| 174 | } |
| 175 | |
| 176 | /// Parse the Annex E `bsi()` from the bytes after the 16-bit sync word. |
| 177 | /// |
| 178 | /// `data` starts at `strmtyp`, which is how a caller has it after matching the |
| 179 | /// sync word: `eac3::bsi::parse(&frame[2..])`. |
| 180 | pub fn parse(data: &[u8]) -> Result<Eac3Bsi> { |
| 181 | // The frame-size field is relative to the sync word, so a reader that |
| 182 | // starts after it has to be told where zero was: parse against a prefixed |
| 183 | // buffer only if the caller kept the sync word. Here the caller did not, |
| 184 | // so nothing in this function depends on absolute bit position. |
| 185 | parse_after_syncword(&mut BitReader::new(data)) |
| 186 | } |
| 187 | |
| 188 | /// [`parse`] against a reader positioned at the sync word. |
| 189 | pub fn parse_from(r: &mut BitReader<'_>) -> Result<Eac3Bsi> { |
| 190 | let syncword = r.read_bits(16)? as u16; |
| 191 | if syncword != crate::syncinfo::SYNCWORD { |
| 192 | return Err(Error::corrupt(format!( |
| 193 | "E-AC-3 bsi: syncword {syncword:#06x}, expected 0x0b77" |
| 194 | ))); |
| 195 | } |
| 196 | parse_after_syncword(r) |
| 197 | } |
| 198 | |
| 199 | fn parse_after_syncword(r: &mut BitReader<'_>) -> Result<Eac3Bsi> { |
| 200 | let strmtyp = StreamType::from_code(r.read_bits(2)?); |
| 201 | let substreamid = r.read_bits(3)? as u8; |
| 202 | let frmsiz = r.read_bits(11)? as u16; |
| 203 | let fscod = r.read_bits(2)? as u8; |
| 204 | let (fscod2, numblkscod) = if fscod == 3 { |
| 205 | (Some(r.read_bits(2)? as u8), 3) |
| 206 | } else { |
| 207 | (None, r.read_bits(2)? as u8) |
| 208 | }; |
| 209 | let sample_rate = match fscod2 { |
| 210 | Some(code) => HALF_RATE[code as usize], |
| 211 | None => crate::tables::SAMPLE_RATE[fscod as usize], |
| 212 | }; |
| 213 | if sample_rate == 0 { |
| 214 | return Err(Error::corrupt("E-AC-3 bsi: reserved sample rate code")); |
| 215 | } |
| 216 | let acmod = Acmod::from_code(r.read_bits(3)? as u8); |
| 217 | let lfeon = r.read_bit()?; |
| 218 | let bsid = r.read_bits(5)? as u8; |
| 219 | if !(11..=16).contains(&bsid) { |
| 220 | return Err(Error::unsupported( |
| 221 | format!("E-AC-3 bsi: bsid = {bsid}"), |
| 222 | "Annex E defines bsid 11 to 16; below that the stream is AC-3", |
| 223 | )); |
| 224 | } |
| 225 | let dialnorm = r.read_bits(5)? as u8; |
| 226 | let compr = read_optional(r, 8)?; |
| 227 | let (mut dialnorm2, mut compr2) = (None, None); |
| 228 | if acmod == Acmod::DualMono { |
| 229 | dialnorm2 = Some(r.read_bits(5)? as u8); |
| 230 | compr2 = read_optional(r, 8)?; |
| 231 | } |
| 232 | let mut chanmap = None; |
| 233 | if strmtyp == StreamType::Dependent && r.read_bit()? { |
| 234 | chanmap = Some(r.read_bits(16)? as u16); |
| 235 | } |
| 236 | |
| 237 | let mut bsi = Bsi { |
| 238 | bsid, |
| 239 | bsmod: 0, |
| 240 | acmod, |
| 241 | cmixlev: None, |
| 242 | surmixlev: None, |
| 243 | dsurmod: None, |
| 244 | lfeon, |
| 245 | dialnorm, |
| 246 | compr, |
| 247 | langcod: None, |
| 248 | mixlevel: None, |
| 249 | roomtyp: None, |
| 250 | dialnorm2, |
| 251 | compr2, |
| 252 | copyrightb: false, |
| 253 | origbs: false, |
| 254 | dmixmod: None, |
| 255 | ltrt_mixlev: None, |
| 256 | loro_mixlev: None, |
| 257 | dsurexmod: None, |
| 258 | nfchans: acmod.nfchans(), |
| 259 | channels: acmod.nfchans() + usize::from(lfeon), |
| 260 | }; |
| 261 | let nblocks = BLOCKS[numblkscod as usize]; |
| 262 | |
| 263 | // Mixing metadata. |
| 264 | if r.read_bit()? { |
| 265 | if acmod.code() > 2 { |
| 266 | bsi.dmixmod = Some(r.read_bits(2)? as u8); |
| 267 | } |
| 268 | if acmod.code() & 1 != 0 && acmod.code() > 2 { |
| 269 | let ltrtcmixlev = r.read_bits(3)? as u8; |
| 270 | let lorocmixlev = r.read_bits(3)? as u8; |
| 271 | bsi.ltrt_mixlev = Some((ltrtcmixlev, 0)); |
| 272 | bsi.loro_mixlev = Some((lorocmixlev, 0)); |
| 273 | } |
| 274 | if acmod.code() & 4 != 0 { |
| 275 | let ltrtsurmixlev = r.read_bits(3)? as u8; |
| 276 | let lorosurmixlev = r.read_bits(3)? as u8; |
| 277 | bsi.ltrt_mixlev = Some((bsi.ltrt_mixlev.map_or(0, |v| v.0), ltrtsurmixlev)); |
| 278 | bsi.loro_mixlev = Some((bsi.loro_mixlev.map_or(0, |v| v.0), lorosurmixlev)); |
| 279 | } |
| 280 | if lfeon && r.read_bit()? { |
| 281 | let _lfemixlevcod = r.read_bits(5)?; |
| 282 | } |
| 283 | if strmtyp == StreamType::Independent { |
| 284 | if r.read_bit()? { |
| 285 | let _pgmscl = r.read_bits(6)?; |
| 286 | } |
| 287 | if acmod == Acmod::DualMono && r.read_bit()? { |
| 288 | let _pgmscl2 = r.read_bits(6)?; |
| 289 | } |
| 290 | if r.read_bit()? { |
| 291 | let _extpgmscl = r.read_bits(6)?; |
| 292 | } |
| 293 | match r.read_bits(2)? { |
| 294 | 1 => { |
| 295 | let _premix = r.read_bits(5)?; |
| 296 | } |
| 297 | 2 => { |
| 298 | let _mixdata = r.read_bits(12)?; |
| 299 | } |
| 300 | 3 => read_mixdata_option4(r, nblocks)?, |
| 301 | _ => {} |
| 302 | } |
| 303 | if acmod.code() < 2 { |
| 304 | if r.read_bit()? { |
| 305 | let _panmean = r.read_bits(8)?; |
| 306 | let _paninfo = r.read_bits(6)?; |
| 307 | } |
| 308 | if acmod == Acmod::DualMono && r.read_bit()? { |
| 309 | let _panmean2 = r.read_bits(8)?; |
| 310 | let _paninfo2 = r.read_bits(6)?; |
| 311 | } |
| 312 | } |
| 313 | if r.read_bit()? { |
| 314 | if numblkscod == 0 { |
| 315 | let _blkmixcfginfo = r.read_bits(5)?; |
| 316 | } else { |
| 317 | for _ in 0..nblocks { |
| 318 | if r.read_bit()? { |
| 319 | let _blkmixcfginfo = r.read_bits(5)?; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // Informational metadata. |
| 328 | if r.read_bit()? { |
| 329 | bsi.bsmod = r.read_bits(3)? as u8; |
| 330 | bsi.copyrightb = r.read_bit()?; |
| 331 | bsi.origbs = r.read_bit()?; |
| 332 | if acmod == Acmod::Stereo { |
| 333 | bsi.dsurmod = Some(r.read_bits(2)? as u8); |
| 334 | let _dheadphonmod = r.read_bits(2)?; |
| 335 | } |
| 336 | if acmod.code() >= 6 { |
| 337 | bsi.dsurexmod = Some(r.read_bits(2)? as u8); |
| 338 | } |
| 339 | if r.read_bit()? { |
| 340 | bsi.mixlevel = Some(r.read_bits(5)? as u8); |
| 341 | bsi.roomtyp = Some(r.read_bits(2)? as u8); |
| 342 | let _adconvtyp = r.read_bit()?; |
| 343 | } |
| 344 | if acmod == Acmod::DualMono && r.read_bit()? { |
| 345 | let _mixlevel2 = r.read_bits(5)?; |
| 346 | let _roomtyp2 = r.read_bits(2)?; |
| 347 | let _adconvtyp2 = r.read_bit()?; |
| 348 | } |
| 349 | if fscod < 3 { |
| 350 | let _sourcefscod = r.read_bit()?; |
| 351 | } |
| 352 | } |
| 353 | if strmtyp == StreamType::Independent && numblkscod != 3 { |
| 354 | let _convsync = r.read_bit()?; |
| 355 | } |
| 356 | if strmtyp == StreamType::Ac3Converted { |
| 357 | let blkid = if numblkscod == 3 { true } else { r.read_bit()? }; |
| 358 | if blkid { |
| 359 | let _frmsizecod = r.read_bits(6)?; |
| 360 | } |
| 361 | } |
| 362 | if r.read_bit()? { |
| 363 | let addbsil = r.read_bits(6)? as u64; |
| 364 | r.skip_bits((addbsil + 1) * 8)?; |
| 365 | } |
| 366 | |
| 367 | Ok(Eac3Bsi { |
| 368 | strmtyp, |
| 369 | substreamid, |
| 370 | frmsiz, |
| 371 | frame_size: (usize::from(frmsiz) + 1) * 2, |
| 372 | fscod, |
| 373 | fscod2, |
| 374 | sample_rate, |
| 375 | numblkscod, |
| 376 | nblocks, |
| 377 | chanmap, |
| 378 | nfchans: bsi.nfchans, |
| 379 | bsi, |
| 380 | }) |
| 381 | } |
| 382 | |
| 383 | fn read_mixdata_option4(r: &mut BitReader<'_>, _nblocks: usize) -> Result<()> { |
| 384 | let mixdeflen = r.read_bits(5)? as u64; |
| 385 | let start = r.bit_position(); |
| 386 | if r.read_bit()? { |
| 387 | let _premixcmpsel = r.read_bit()?; |
| 388 | let _drcsrc = r.read_bit()?; |
| 389 | let _premixcmpscl = r.read_bits(3)?; |
| 390 | for _ in 0..7 { |
| 391 | if r.read_bit()? { |
| 392 | let _scale = r.read_bits(4)?; |
| 393 | } |
| 394 | } |
| 395 | if r.read_bit()? { |
| 396 | for _ in 0..2 { |
| 397 | if r.read_bit()? { |
| 398 | let _aux = r.read_bits(4)?; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | if r.read_bit()? { |
| 404 | let _spchdat = r.read_bits(5)?; |
| 405 | if r.read_bit()? { |
| 406 | let _spchdat1 = r.read_bits(5)?; |
| 407 | let _spchan1att = r.read_bits(2)?; |
| 408 | if r.read_bit()? { |
| 409 | let _spchdat2 = r.read_bits(5)?; |
| 410 | let _spchan2att = r.read_bits(3)?; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | // The field is a fixed-length block: whatever the sub-fields did not use is |
| 415 | // padding, so seek to its stated end rather than trusting the parse. |
| 416 | let used = r.bit_position() - start; |
| 417 | let total = 8 * (mixdeflen + 2); |
| 418 | if used > total { |
| 419 | return Err(Error::corrupt( |
| 420 | "E-AC-3 bsi: mixdata sub-fields overran mixdeflen", |
| 421 | )); |
| 422 | } |
| 423 | r.skip_bits(total - used)?; |
| 424 | Ok(()) |
| 425 | } |
| 426 | |
| 427 | fn read_optional(r: &mut BitReader<'_>, n: u32) -> Result<Option<u8>> { |
| 428 | if r.read_bit()? { |
| 429 | Ok(Some(r.read_bits(n)? as u8)) |
| 430 | } else { |
| 431 | Ok(None) |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /// The frame-level strategy fields of `audfrm()` (Table E1.3). |
| 436 | #[derive(Debug, Clone)] |
| 437 | struct AudFrm { |
| 438 | expstre: bool, |
| 439 | ahte: bool, |
| 440 | snroffststr: u32, |
| 441 | transproce: bool, |
| 442 | blkswe: bool, |
| 443 | dithflage: bool, |
| 444 | bamode: bool, |
| 445 | frmfgaincode: bool, |
| 446 | dbaflde: bool, |
| 447 | skipflde: bool, |
| 448 | spxattene: bool, |
| 449 | cplstre: [bool; 6], |
| 450 | cplinu: [bool; 6], |
| 451 | cplexpstr: [Strategy; 6], |
| 452 | chexpstr: [[Strategy; 5]; 6], |
| 453 | lfeexpstr: [Strategy; 6], |
| 454 | frmcsnroffst: u8, |
| 455 | frmfsnroffst: u8, |
| 456 | /// Per-channel spectral extension attenuation, from `spxattene`. |
| 457 | chinspxatten: [bool; 5], |
| 458 | spxattencod: [u8; 5], |
| 459 | /// Which channels use the adaptive hybrid transform this frame. |
| 460 | cplahtinu: bool, |
| 461 | chahtinu: [bool; 5], |
| 462 | lfeahtinu: bool, |
| 463 | } |
| 464 | |
| 465 | fn parse_audfrm(r: &mut BitReader<'_>, hdr: &Eac3Bsi) -> Result<AudFrm> { |
| 466 | let nfchans = hdr.nfchans; |
| 467 | let nblocks = hdr.nblocks; |
| 468 | let acmod = hdr.bsi.acmod; |
| 469 | let (expstre, ahte) = if hdr.numblkscod == 3 { |
| 470 | (r.read_bit()?, r.read_bit()?) |
| 471 | } else { |
| 472 | (true, false) |
| 473 | }; |
| 474 | let mut frm = AudFrm { |
| 475 | expstre, |
| 476 | ahte, |
| 477 | snroffststr: r.read_bits(2)?, |
| 478 | transproce: r.read_bit()?, |
| 479 | blkswe: r.read_bit()?, |
| 480 | dithflage: r.read_bit()?, |
| 481 | bamode: r.read_bit()?, |
| 482 | frmfgaincode: r.read_bit()?, |
| 483 | dbaflde: r.read_bit()?, |
| 484 | skipflde: r.read_bit()?, |
| 485 | spxattene: r.read_bit()?, |
| 486 | cplstre: [false; 6], |
| 487 | cplinu: [false; 6], |
| 488 | cplexpstr: [Strategy::Reuse; 6], |
| 489 | chexpstr: [[Strategy::Reuse; 5]; 6], |
| 490 | lfeexpstr: [Strategy::Reuse; 6], |
| 491 | frmcsnroffst: 0, |
| 492 | frmfsnroffst: 0, |
| 493 | chinspxatten: [false; 5], |
| 494 | spxattencod: [0; 5], |
| 495 | cplahtinu: false, |
| 496 | chahtinu: [false; 5], |
| 497 | lfeahtinu: false, |
| 498 | }; |
| 499 | |
| 500 | frm.cplstre[0] = true; |
| 501 | if acmod.code() > 1 { |
| 502 | frm.cplinu[0] = r.read_bit()?; |
| 503 | for blk in 1..nblocks { |
| 504 | frm.cplstre[blk] = r.read_bit()?; |
| 505 | frm.cplinu[blk] = if frm.cplstre[blk] { |
| 506 | r.read_bit()? |
| 507 | } else { |
| 508 | frm.cplinu[blk - 1] |
| 509 | }; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if frm.expstre { |
| 514 | for blk in 0..nblocks { |
| 515 | if frm.cplinu[blk] { |
| 516 | frm.cplexpstr[blk] = Strategy::from_code(r.read_bits(2)?); |
| 517 | } |
| 518 | for ch in 0..nfchans { |
| 519 | frm.chexpstr[blk][ch] = Strategy::from_code(r.read_bits(2)?); |
| 520 | } |
| 521 | } |
| 522 | } else { |
| 523 | let ncplblks = frm.cplinu[..nblocks].iter().filter(|&&v| v).count(); |
| 524 | if acmod.code() > 1 && ncplblks > 0 { |
| 525 | let code = r.read_bits(5)? as usize; |
| 526 | for blk in 0..nblocks { |
| 527 | frm.cplexpstr[blk] = Strategy::from_code(u32::from(FRAME_EXP_STRATEGY[code][blk])); |
| 528 | } |
| 529 | } |
| 530 | for ch in 0..nfchans { |
| 531 | let code = r.read_bits(5)? as usize; |
| 532 | for blk in 0..nblocks { |
| 533 | frm.chexpstr[blk][ch] = |
| 534 | Strategy::from_code(u32::from(FRAME_EXP_STRATEGY[code][blk])); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | if hdr.bsi.lfeon { |
| 539 | for blk in 0..nblocks { |
| 540 | frm.lfeexpstr[blk] = if r.read_bit()? { |
| 541 | Strategy::D15 |
| 542 | } else { |
| 543 | Strategy::Reuse |
| 544 | }; |
| 545 | } |
| 546 | } |
| 547 | if hdr.strmtyp == StreamType::Independent { |
| 548 | let convexpstre = if hdr.numblkscod != 3 { |
| 549 | r.read_bit()? |
| 550 | } else { |
| 551 | true |
| 552 | }; |
| 553 | if convexpstre { |
| 554 | for _ in 0..nfchans { |
| 555 | let _convexpstr = r.read_bits(5)?; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | if frm.ahte { |
| 560 | // The AHT flags are only sent where a single exponent region covers |
| 561 | // the frame; counting the regions is how the standard states it. |
| 562 | let ncplblks = frm.cplinu[..nblocks].iter().filter(|&&v| v).count(); |
| 563 | let ncplregs = (0..nblocks) |
| 564 | .filter(|&b| frm.cplstre[b] || frm.cplexpstr[b] != Strategy::Reuse) |
| 565 | .count(); |
| 566 | let cplahtinu = if ncplblks == 6 && ncplregs == 1 { |
| 567 | r.read_bit()? |
| 568 | } else { |
| 569 | false |
| 570 | }; |
| 571 | let mut chahtinu = [false; 5]; |
| 572 | for (ch, slot) in chahtinu.iter_mut().enumerate().take(nfchans) { |
| 573 | let nchregs = (0..nblocks) |
| 574 | .filter(|&b| frm.chexpstr[b][ch] != Strategy::Reuse) |
| 575 | .count(); |
| 576 | if nchregs == 1 { |
| 577 | *slot = r.read_bit()?; |
| 578 | } |
| 579 | } |
| 580 | let mut lfeahtinu = false; |
| 581 | let nlferegs = (0..nblocks) |
| 582 | .filter(|&b| frm.lfeexpstr[b] != Strategy::Reuse) |
| 583 | .count(); |
| 584 | if hdr.bsi.lfeon && nlferegs == 1 { |
| 585 | lfeahtinu = r.read_bit()?; |
| 586 | } |
| 587 | frm.cplahtinu = cplahtinu; |
| 588 | frm.chahtinu = chahtinu; |
| 589 | frm.lfeahtinu = lfeahtinu; |
| 590 | } |
| 591 | if frm.snroffststr == 0 { |
| 592 | frm.frmcsnroffst = r.read_bits(6)? as u8; |
| 593 | frm.frmfsnroffst = r.read_bits(4)? as u8; |
| 594 | } |
| 595 | if frm.transproce { |
| 596 | for _ in 0..nfchans { |
| 597 | if r.read_bit()? { |
| 598 | let _transprocloc = r.read_bits(10)?; |
| 599 | let _transproclen = r.read_bits(8)?; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | if frm.spxattene { |
| 604 | for ch in 0..nfchans { |
| 605 | frm.chinspxatten[ch] = r.read_bit()?; |
| 606 | if frm.chinspxatten[ch] { |
| 607 | frm.spxattencod[ch] = r.read_bits(5)? as u8; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | if hdr.numblkscod != 0 && r.read_bit()? { |
| 612 | // §E2.3.2.27: the field is sized from the frame size. |
| 613 | // §E2.3.2.27: (numblks - 1) * (4 + ceil(log2(frmsiz + 1))). |
| 614 | let words = u32::from(hdr.frmsiz) + 1; |
| 615 | let nblkstrtbits = (nblocks as u32 - 1) * (4 + words.next_power_of_two().trailing_zeros()); |
| 616 | r.skip_bits(u64::from(nblkstrtbits))?; |
| 617 | } |
| 618 | Ok(frm) |
| 619 | } |
| 620 | |
| 621 | /// Decode one Annex E syncframe: header already parsed, reader positioned at |
| 622 | /// `audfrm()`. |
| 623 | pub(crate) fn decode_frame( |
| 624 | core: &mut Core, |
| 625 | r: &mut BitReader<'_>, |
| 626 | hdr: &Eac3Bsi, |
| 627 | out: &mut Vec<f32>, |
| 628 | ) -> Result<()> { |
| 629 | let frm = parse_audfrm(r, hdr)?; |
| 630 | // Reduced rates index the hearing threshold table by the full rate they |
| 631 | // halve, which is what fscod2 numbers. |
| 632 | let fscod = hdr.fscod2.map_or(hdr.fscod as usize, |c| c as usize); |
| 633 | core.start_frame(Syntax::Eac3, fscod, &hdr.bsi); |
| 634 | // §E1.3, end of audfrm(): the coupling coordinates, the spectral extension |
| 635 | // coordinates and the leak state are all sent afresh in the first block of |
| 636 | // every frame that uses them. |
| 637 | core.first_cplco = [true; 5]; |
| 638 | core.first_spxco = [true; 5]; |
| 639 | core.first_cplleak = true; |
| 640 | core.chinspxatten = frm.chinspxatten; |
| 641 | core.spxattencod = frm.spxattencod; |
| 642 | core.aht = [false; CHANNELS]; |
| 643 | core.aht[CPL] = frm.cplahtinu; |
| 644 | core.aht[LFE] = frm.lfeahtinu; |
| 645 | core.aht[..5].copy_from_slice(&frm.chahtinu); |
| 646 | for blk in 0..hdr.nblocks { |
| 647 | parse_block(core, r, &frm, hdr, blk)?; |
| 648 | // §3.6: synthesis sits between decoupling and rematrixing — it fills |
| 649 | // the band above the coded one, which rematrixing never reaches. |
| 650 | core.decouple(); |
| 651 | if core.spxinu { |
| 652 | for ch in 0..core.nfchans { |
| 653 | if core.chinspx[ch] { |
| 654 | spectral_extension(core, ch); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | core.rematrix(); |
| 659 | core.apply_gain(); |
| 660 | core.transform(out); |
| 661 | } |
| 662 | Ok(()) |
| 663 | } |
| 664 | |
| 665 | #[allow(clippy::too_many_lines)] |
| 666 | fn parse_block( |
| 667 | core: &mut Core, |
| 668 | r: &mut BitReader<'_>, |
| 669 | frm: &AudFrm, |
| 670 | hdr: &Eac3Bsi, |
| 671 | blk: usize, |
| 672 | ) -> Result<()> { |
| 673 | let nfchans = core.nfchans; |
| 674 | let acmod = core.acmod; |
| 675 | |
| 676 | for ch in 0..nfchans { |
| 677 | core.blksw[ch] = if frm.blkswe { r.read_bit()? } else { false }; |
| 678 | } |
| 679 | for ch in 0..nfchans { |
| 680 | core.dithflag[ch] = if frm.dithflage { r.read_bit()? } else { true }; |
| 681 | } |
| 682 | if r.read_bit()? { |
| 683 | core.dynrng[0] = core.gain(r.read_bits(8)? as u8); |
| 684 | } else if blk == 0 { |
| 685 | core.dynrng[0] = 1.0; |
| 686 | } |
| 687 | if acmod == Acmod::DualMono { |
| 688 | if r.read_bit()? { |
| 689 | core.dynrng[1] = core.gain(r.read_bits(8)? as u8); |
| 690 | } else if blk == 0 { |
| 691 | core.dynrng[1] = 1.0; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | // Spectral extension strategy (§E2.2.4). |
| 696 | let spxstre = blk == 0 || r.read_bit()?; |
| 697 | if spxstre { |
| 698 | core.spxinu = r.read_bit()?; |
| 699 | if core.spxinu { |
| 700 | if acmod == Acmod::Mono { |
| 701 | core.chinspx[0] = true; |
| 702 | } else { |
| 703 | for ch in 0..nfchans { |
| 704 | core.chinspx[ch] = r.read_bit()?; |
| 705 | } |
| 706 | } |
| 707 | core.spxstrtf = r.read_bits(2)? as usize; |
| 708 | let spxbegf = r.read_bits(3)? as usize; |
| 709 | let spxendf = r.read_bits(3)? as usize; |
| 710 | core.spxbegf = spxbegf; |
| 711 | core.spx_begin = if spxbegf < 6 { |
| 712 | spxbegf + 2 |
| 713 | } else { |
| 714 | spxbegf * 2 - 3 |
| 715 | }; |
| 716 | core.spx_end = if spxendf < 3 { |
| 717 | spxendf + 5 |
| 718 | } else { |
| 719 | spxendf * 2 + 3 |
| 720 | }; |
| 721 | if core.spx_end <= core.spx_begin || core.spx_end > 17 { |
| 722 | return Err(Error::corrupt(format!( |
| 723 | "E-AC-3 spectral extension: sub-bands {}..{}", |
| 724 | core.spx_begin, core.spx_end |
| 725 | ))); |
| 726 | } |
| 727 | if r.read_bit()? { |
| 728 | for bnd in core.spx_begin + 1..core.spx_end { |
| 729 | core.spxbndstrc[bnd] = r.read_bit()?; |
| 730 | } |
| 731 | } else if blk == 0 { |
| 732 | core.spxbndstrc = DEFAULT_SPXBNDSTRC; |
| 733 | } |
| 734 | // §3.6.2: sub-bands merge into bands as spxbndstrc says. |
| 735 | core.nspxbnds = 1; |
| 736 | core.spxbndsz[0] = 12; |
| 737 | for bnd in core.spx_begin + 1..core.spx_end { |
| 738 | if core.spxbndstrc[bnd] { |
| 739 | core.spxbndsz[core.nspxbnds - 1] += 12; |
| 740 | } else { |
| 741 | core.spxbndsz[core.nspxbnds] = 12; |
| 742 | core.nspxbnds += 1; |
| 743 | } |
| 744 | } |
| 745 | } else { |
| 746 | core.chinspx = [false; 5]; |
| 747 | core.first_spxco = [true; 5]; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | // Spectral extension coordinates. |
| 752 | if core.spxinu { |
| 753 | for ch in 0..nfchans { |
| 754 | if !core.chinspx[ch] { |
| 755 | core.first_spxco[ch] = true; |
| 756 | continue; |
| 757 | } |
| 758 | let spxcoe = if core.first_spxco[ch] { |
| 759 | core.first_spxco[ch] = false; |
| 760 | true |
| 761 | } else { |
| 762 | r.read_bit()? |
| 763 | }; |
| 764 | if !spxcoe { |
| 765 | continue; |
| 766 | } |
| 767 | let spxblnd = r.read_bits(5)? as f32 / 32.0; |
| 768 | let mstrspxco = r.read_bits(2)? as i32; |
| 769 | let mut spxmant = spx_band_start(core.spx_begin) as f32; |
| 770 | let end = spx_band_start(core.spx_end) as f32; |
| 771 | for bnd in 0..core.nspxbnds { |
| 772 | let exp = r.read_bits(4)? as i32; |
| 773 | let mant = r.read_bits(2)? as f32; |
| 774 | let value = if exp == 15 { |
| 775 | mant / 4.0 |
| 776 | } else { |
| 777 | (mant + 4.0) / 8.0 |
| 778 | }; |
| 779 | core.spxco[ch][bnd] = |
| 780 | value * crate::mantissa::scale((exp + 3 * mstrspxco).min(24) as u8); |
| 781 | // §3.6.4.2.1: the blend follows the band's frequency midpoint. |
| 782 | let bandsize = core.spxbndsz[bnd] as f32; |
| 783 | let nratio = ((spxmant + 0.5 * bandsize) / end - spxblnd).clamp(0.0, 1.0); |
| 784 | core.nblend[ch][bnd] = nratio.sqrt(); |
| 785 | core.sblend[ch][bnd] = (1.0 - nratio).sqrt(); |
| 786 | spxmant += bandsize; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | // Coupling strategy. |
| 792 | // `cplstre[blk]` and `cplinu[blk]` were read in audfrm(), not here. |
| 793 | if frm.cplstre[blk] { |
| 794 | if frm.cplinu[blk] { |
| 795 | let ecplinu = r.read_bit()?; |
| 796 | if ecplinu { |
| 797 | return Err(Error::unsupported( |
| 798 | "E-AC-3 enhanced coupling (ecplinu)", |
| 799 | "the amplitude/angle/chaos parameterisation is not \ |
| 800 | implemented in this build", |
| 801 | )); |
| 802 | } |
| 803 | if acmod == Acmod::Stereo { |
| 804 | core.chincpl[0] = true; |
| 805 | core.chincpl[1] = true; |
| 806 | } else { |
| 807 | for ch in 0..nfchans { |
| 808 | core.chincpl[ch] = r.read_bit()?; |
| 809 | } |
| 810 | } |
| 811 | if acmod == Acmod::Stereo { |
| 812 | core.phsflginu = r.read_bit()?; |
| 813 | } |
| 814 | core.cplbegf = r.read_bits(4)? as usize; |
| 815 | // §E3.3.1: with spectral extension the coupling end is derived |
| 816 | // from spxbegf rather than sent, and may go negative. |
| 817 | let cplendf: i32 = if core.spxinu { |
| 818 | if core.spxbegf < 6 { |
| 819 | core.spxbegf as i32 - 2 |
| 820 | } else { |
| 821 | core.spxbegf as i32 * 2 - 7 |
| 822 | } |
| 823 | } else { |
| 824 | r.read_bits(4)? as i32 |
| 825 | }; |
| 826 | let ncplsubnd = 3 + cplendf - core.cplbegf as i32; |
| 827 | if !(1..=18).contains(&ncplsubnd) { |
| 828 | return Err(Error::corrupt(format!( |
| 829 | "E-AC-3 coupling: cplbegf {} with cplendf {cplendf}", |
| 830 | core.cplbegf |
| 831 | ))); |
| 832 | } |
| 833 | core.ncplsubnd = ncplsubnd as usize; |
| 834 | core.strtmant[CPL] = 37 + 12 * core.cplbegf; |
| 835 | core.endmant[CPL] = (37 + 12 * (cplendf + 3)) as usize; |
| 836 | // §E2.3.3.14: the banding array is indexed by *absolute* coupling |
| 837 | // sub-band, like the spectral extension one — which is what makes |
| 838 | // the default of Table E2.12 mean the same thing wherever coupling |
| 839 | // starts. `cplbndstrce == 0` in the first block using coupling |
| 840 | // means the default; in a later block it means "reuse". |
| 841 | if r.read_bit()? { |
| 842 | for bnd in 1..core.ncplsubnd { |
| 843 | core.cplbndstrc[core.cplbegf + bnd] = r.read_bit()?; |
| 844 | } |
| 845 | } else if blk == 0 { |
| 846 | core.cplbndstrc = DEFAULT_CPLBNDSTRC; |
| 847 | } |
| 848 | core.cplband[0] = 0; |
| 849 | core.ncplbnd = 1; |
| 850 | for sbnd in 1..core.ncplsubnd { |
| 851 | if !core.cplbndstrc[core.cplbegf + sbnd] { |
| 852 | core.ncplbnd += 1; |
| 853 | } |
| 854 | core.cplband[sbnd] = core.ncplbnd - 1; |
| 855 | } |
| 856 | } else { |
| 857 | // §E1.4, the `!cplinu[blk]` branch: switching coupling off also |
| 858 | // rearms the "first block of the run" state, so a frame that turns |
| 859 | // coupling on again later sends its coordinates and leak values |
| 860 | // unconditionally rather than behind a presence bit. |
| 861 | core.chincpl = [false; 5]; |
| 862 | core.phsflginu = false; |
| 863 | core.first_cplco = [true; 5]; |
| 864 | core.first_cplleak = true; |
| 865 | } |
| 866 | } |
| 867 | core.cplinu = frm.cplinu[blk]; |
| 868 | |
| 869 | // Coupling coordinates. Annex E sends them once per channel per coupling |
| 870 | // run: the first block of the run has them implicitly. |
| 871 | if core.cplinu { |
| 872 | let mut cplcoe = [false; 5]; |
| 873 | for ch in 0..nfchans { |
| 874 | if !core.chincpl[ch] { |
| 875 | core.first_cplco[ch] = true; |
| 876 | continue; |
| 877 | } |
| 878 | cplcoe[ch] = if core.first_cplco[ch] { |
| 879 | core.first_cplco[ch] = false; |
| 880 | true |
| 881 | } else { |
| 882 | r.read_bit()? |
| 883 | }; |
| 884 | if cplcoe[ch] { |
| 885 | let mstrcplco = r.read_bits(2)? as i32; |
| 886 | for bnd in 0..core.ncplbnd { |
| 887 | let cplcoexp = r.read_bits(4)? as i32; |
| 888 | let cplcomant = r.read_bits(4)? as f32; |
| 889 | let mant = if cplcoexp == 15 { |
| 890 | cplcomant / 16.0 |
| 891 | } else { |
| 892 | (cplcomant + 16.0) / 32.0 |
| 893 | }; |
| 894 | core.cplco[ch][bnd] = |
| 895 | mant * crate::mantissa::scale((cplcoexp + 3 * mstrcplco).min(24) as u8); |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | if acmod == Acmod::Stereo && core.phsflginu && (cplcoe[0] || cplcoe[1]) { |
| 900 | for bnd in 0..core.ncplbnd { |
| 901 | core.phsflg[bnd] = r.read_bit()?; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | // Rematrixing. |
| 907 | if acmod == Acmod::Stereo { |
| 908 | let rematstr = blk == 0 || r.read_bit()?; |
| 909 | if rematstr { |
| 910 | // §E3.3.2. |
| 911 | core.nrematbnd = if core.cplinu { |
| 912 | match core.cplbegf { |
| 913 | 0 => 2, |
| 914 | 1 | 2 => 3, |
| 915 | _ => 4, |
| 916 | } |
| 917 | } else if core.spxinu { |
| 918 | if core.spxbegf < 2 { 3 } else { 4 } |
| 919 | } else { |
| 920 | 4 |
| 921 | }; |
| 922 | for bnd in 0..core.nrematbnd { |
| 923 | core.rematflg[bnd] = r.read_bit()?; |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | // Bandwidth codes and exponents. |
| 929 | core.expstr = [Strategy::Reuse; CHANNELS]; |
| 930 | core.expstr[CPL] = frm.cplexpstr[blk]; |
| 931 | for ch in 0..nfchans { |
| 932 | core.expstr[ch] = frm.chexpstr[blk][ch]; |
| 933 | } |
| 934 | core.expstr[LFE] = frm.lfeexpstr[blk]; |
| 935 | for ch in 0..nfchans { |
| 936 | if core.expstr[ch] != Strategy::Reuse { |
| 937 | if core.chincpl[ch] && core.cplinu { |
| 938 | core.endmant[ch] = core.strtmant[CPL]; |
| 939 | } else if core.chinspx[ch] && core.spxinu { |
| 940 | // §E3.3.3: the coded band ends where synthesis begins. |
| 941 | core.endmant[ch] = spx_band_start(core.spx_begin); |
| 942 | } else { |
| 943 | let chbwcod = r.read_bits(6)? as usize; |
| 944 | if chbwcod > 60 { |
| 945 | return Err(Error::corrupt(format!( |
| 946 | "E-AC-3 audblk: chbwcod = {chbwcod} > 60" |
| 947 | ))); |
| 948 | } |
| 949 | core.endmant[ch] = (chbwcod + 12) * 3 + 37; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | if core.cplinu && core.expstr[CPL] != Strategy::Reuse { |
| 954 | let absexp = (r.read_bits(4)? << 1) as u8; |
| 955 | let (start, end) = (core.strtmant[CPL], core.endmant[CPL]); |
| 956 | let ngrps = core.expstr[CPL].coupling_groups(start, end); |
| 957 | exps::decode( |
| 958 | r, |
| 959 | core.expstr[CPL], |
| 960 | ngrps, |
| 961 | absexp, |
| 962 | start, |
| 963 | &mut core.exps[CPL], |
| 964 | )?; |
| 965 | } |
| 966 | for ch in 0..nfchans { |
| 967 | if core.expstr[ch] == Strategy::Reuse { |
| 968 | continue; |
| 969 | } |
| 970 | let absexp = r.read_bits(4)? as u8; |
| 971 | core.exps[ch][0] = absexp; |
| 972 | let ngrps = core.expstr[ch].fbw_groups(core.endmant[ch]); |
| 973 | exps::decode(r, core.expstr[ch], ngrps, absexp, 1, &mut core.exps[ch]).map_err( |
| 974 | |e| match e { |
| 975 | Error::Corrupt { context } => Error::corrupt(format!( |
| 976 | "{context} (block {blk}, channel {ch}, {:?}, endmant {}, spx {}, cpl {})", |
| 977 | core.expstr[ch], core.endmant[ch], core.chinspx[ch], core.chincpl[ch] |
| 978 | )), |
| 979 | other => other, |
| 980 | }, |
| 981 | )?; |
| 982 | let _gainrng = r.read_bits(2)?; |
| 983 | } |
| 984 | if core.lfeon && core.expstr[LFE] != Strategy::Reuse { |
| 985 | let absexp = r.read_bits(4)? as u8; |
| 986 | core.exps[LFE][0] = absexp; |
| 987 | exps::decode(r, Strategy::D15, 2, absexp, 1, &mut core.exps[LFE])?; |
| 988 | } |
| 989 | |
| 990 | // Bit allocation parameters. |
| 991 | if frm.bamode { |
| 992 | if r.read_bit()? { |
| 993 | core.ba = BitAllocParams { |
| 994 | sdcycod: r.read_bits(2)? as u8, |
| 995 | fdcycod: r.read_bits(2)? as u8, |
| 996 | sgaincod: r.read_bits(2)? as u8, |
| 997 | dbpbcod: r.read_bits(2)? as u8, |
| 998 | floorcod: r.read_bits(3)? as u8, |
| 999 | }; |
| 1000 | } |
| 1001 | } else { |
| 1002 | core.ba = BitAllocParams { |
| 1003 | sdcycod: 2, |
| 1004 | fdcycod: 1, |
| 1005 | sgaincod: 1, |
| 1006 | dbpbcod: 2, |
| 1007 | floorcod: 7, |
| 1008 | }; |
| 1009 | } |
| 1010 | |
| 1011 | // SNR offsets. |
| 1012 | if frm.snroffststr == 0 { |
| 1013 | core.csnroffst = frm.frmcsnroffst; |
| 1014 | core.fsnroffst = [frm.frmfsnroffst; CHANNELS]; |
| 1015 | } else { |
| 1016 | let snroffste = blk == 0 || r.read_bit()?; |
| 1017 | if snroffste { |
| 1018 | core.csnroffst = r.read_bits(6)? as u8; |
| 1019 | if frm.snroffststr == 1 { |
| 1020 | let blkfsnroffst = r.read_bits(4)? as u8; |
| 1021 | core.fsnroffst = [blkfsnroffst; CHANNELS]; |
| 1022 | } else { |
| 1023 | if core.cplinu { |
| 1024 | core.fsnroffst[CPL] = r.read_bits(4)? as u8; |
| 1025 | } |
| 1026 | for ch in 0..nfchans { |
| 1027 | core.fsnroffst[ch] = r.read_bits(4)? as u8; |
| 1028 | } |
| 1029 | if core.lfeon { |
| 1030 | core.fsnroffst[LFE] = r.read_bits(4)? as u8; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | // Fast gain codes. |
| 1037 | let fgaincode = frm.frmfgaincode && r.read_bit()?; |
| 1038 | if fgaincode { |
| 1039 | if core.cplinu { |
| 1040 | core.fgaincod[CPL] = r.read_bits(3)? as u8; |
| 1041 | } |
| 1042 | for ch in 0..nfchans { |
| 1043 | core.fgaincod[ch] = r.read_bits(3)? as u8; |
| 1044 | } |
| 1045 | if core.lfeon { |
| 1046 | core.fgaincod[LFE] = r.read_bits(3)? as u8; |
| 1047 | } |
| 1048 | } else { |
| 1049 | core.fgaincod = [4; CHANNELS]; |
| 1050 | } |
| 1051 | if hdr.strmtyp == StreamType::Independent && r.read_bit()? { |
| 1052 | let _convsnroffst = r.read_bits(10)?; |
| 1053 | } |
| 1054 | if core.cplinu { |
| 1055 | let cplleake = if core.first_cplleak { |
| 1056 | core.first_cplleak = false; |
| 1057 | true |
| 1058 | } else { |
| 1059 | r.read_bit()? |
| 1060 | }; |
| 1061 | if cplleake { |
| 1062 | core.cplleak = ( |
| 1063 | ((r.read_bits(3)? as i32) << 8) + 768, |
| 1064 | ((r.read_bits(3)? as i32) << 8) + 768, |
| 1065 | ); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | // Delta bit allocation. |
| 1070 | if frm.dbaflde && r.read_bit()? { |
| 1071 | let mut deltbae = [0u32; CHANNELS]; |
| 1072 | if core.cplinu { |
| 1073 | deltbae[CPL] = r.read_bits(2)?; |
| 1074 | } |
| 1075 | for ch in 0..nfchans { |
| 1076 | deltbae[ch] = r.read_bits(2)?; |
| 1077 | } |
| 1078 | let mut order = Vec::with_capacity(CHANNELS); |
| 1079 | if core.cplinu { |
| 1080 | order.push(CPL); |
| 1081 | } |
| 1082 | order.extend(0..nfchans); |
| 1083 | for ch in order { |
| 1084 | match deltbae[ch] { |
| 1085 | 0 => {} |
| 1086 | 1 => { |
| 1087 | core.deltba_on[ch] = true; |
| 1088 | let nseg = r.read_bits(3)? as usize + 1; |
| 1089 | let mut dba = DeltaBa { |
| 1090 | nseg, |
| 1091 | ..DeltaBa::default() |
| 1092 | }; |
| 1093 | for seg in 0..nseg { |
| 1094 | dba.offset[seg] = r.read_bits(5)? as u8; |
| 1095 | dba.length[seg] = r.read_bits(4)? as u8; |
| 1096 | dba.delta[seg] = r.read_bits(3)? as u8; |
| 1097 | } |
| 1098 | core.deltba[ch] = dba; |
| 1099 | } |
| 1100 | _ => core.deltba_on[ch] = false, |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | if frm.skipflde && r.read_bit()? { |
| 1106 | let skipl = r.read_bits(9)? as u64; |
| 1107 | r.skip_bits(skipl * 8)?; |
| 1108 | } |
| 1109 | |
| 1110 | core.allocate(); |
| 1111 | core.read_mantissas(r, blk) |
| 1112 | } |
| 1113 | |
| 1114 | /// §3.6.4: synthesise one channel's high band — translate, blend with noise, |
| 1115 | /// and scale to the transmitted envelope. |
| 1116 | fn spectral_extension(core: &mut Core, ch: usize) { |
| 1117 | let copystart = spx_band_start(core.spxstrtf); |
| 1118 | let copyend = spx_band_start(core.spx_begin); |
| 1119 | let nbnds = core.nspxbnds; |
| 1120 | |
| 1121 | // §3.6.4.1: translation, wrapping the copy region as it runs out. |
| 1122 | let mut wrapflag = [false; 18]; |
| 1123 | let mut copyindex = copystart; |
| 1124 | let mut insertindex = copyend; |
| 1125 | for bnd in 0..nbnds { |
| 1126 | let bandsize = core.spxbndsz[bnd]; |
| 1127 | if copyindex + bandsize > copyend { |
| 1128 | copyindex = copystart; |
| 1129 | wrapflag[bnd] = true; |
| 1130 | } |
| 1131 | for _ in 0..bandsize { |
| 1132 | if copyindex == copyend { |
| 1133 | copyindex = copystart; |
| 1134 | } |
| 1135 | if insertindex >= COEFFS { |
| 1136 | break; |
| 1137 | } |
| 1138 | core.coeffs[ch][insertindex] = core.coeffs[ch][copyindex]; |
| 1139 | insertindex += 1; |
| 1140 | copyindex += 1; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | // §3.6.4.2.2: banded RMS energy of what was just translated. |
| 1145 | let mut rms = [0.0f32; 18]; |
| 1146 | let mut spxmant = copyend; |
| 1147 | for bnd in 0..nbnds { |
| 1148 | let bandsize = core.spxbndsz[bnd]; |
| 1149 | let mut accum = 0.0f32; |
| 1150 | for _ in 0..bandsize { |
| 1151 | if spxmant >= COEFFS { |
| 1152 | break; |
| 1153 | } |
| 1154 | let v = core.coeffs[ch][spxmant]; |
| 1155 | accum += v * v; |
| 1156 | spxmant += 1; |
| 1157 | } |
| 1158 | rms[bnd] = (accum / bandsize as f32).sqrt(); |
| 1159 | } |
| 1160 | |
| 1161 | // §3.6.4.2.3: a 5-tap notch across the baseband border and every wrap. |
| 1162 | if core.chinspxatten[ch] { |
| 1163 | let tab = SPXATTENTAB[(core.spxattencod[ch] as usize).min(31)]; |
| 1164 | let taps = [tab[0], tab[1], tab[2], tab[1], tab[0]]; |
| 1165 | let mut filtbin = copyend.saturating_sub(2); |
| 1166 | for tap in taps { |
| 1167 | if filtbin < COEFFS { |
| 1168 | core.coeffs[ch][filtbin] *= tap; |
| 1169 | } |
| 1170 | filtbin += 1; |
| 1171 | } |
| 1172 | filtbin += core.spxbndsz[0]; |
| 1173 | for bnd in 1..nbnds { |
| 1174 | if wrapflag[bnd] { |
| 1175 | filtbin -= 5; |
| 1176 | for tap in taps { |
| 1177 | if filtbin < COEFFS { |
| 1178 | core.coeffs[ch][filtbin] *= tap; |
| 1179 | } |
| 1180 | filtbin += 1; |
| 1181 | } |
| 1182 | } |
| 1183 | filtbin += core.spxbndsz[bnd]; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | // §3.6.4.2.4 and §3.6.4.3: blend in energy-matched noise, then scale to |
| 1188 | // the transmitted envelope. |
| 1189 | let mut spxmant = copyend; |
| 1190 | for bnd in 0..nbnds { |
| 1191 | let bandsize = core.spxbndsz[bnd]; |
| 1192 | let nscale = rms[bnd] * core.nblend[ch][bnd]; |
| 1193 | let sscale = core.sblend[ch][bnd]; |
| 1194 | let coord = core.spxco[ch][bnd] * 32.0; |
| 1195 | for _ in 0..bandsize { |
| 1196 | if spxmant >= COEFFS { |
| 1197 | break; |
| 1198 | } |
| 1199 | // §E3.6.4.2.4's noise(): a zero-mean unit-variance source the |
| 1200 | // standard leaves to the implementation, so it is switched by the |
| 1201 | // same option as §7.3.4's dither. |
| 1202 | let noise = if core.dither_on { |
| 1203 | core.spx_noise.unit_variance() |
| 1204 | } else { |
| 1205 | 0.0 |
| 1206 | }; |
| 1207 | core.coeffs[ch][spxmant] = (core.coeffs[ch][spxmant] * sscale + noise * nscale) * coord; |
| 1208 | spxmant += 1; |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | #[cfg(test)] |
| 1214 | mod tests { |
| 1215 | use super::*; |
| 1216 | |
| 1217 | #[test] |
| 1218 | fn frame_exponent_strategy_table_is_the_printed_one() { |
| 1219 | // Table E2.10 corners: code 0 is D15 then all reuse, code 31 is D45 |
| 1220 | // in every block, and code 16 opens D45, D15. |
| 1221 | assert_eq!(FRAME_EXP_STRATEGY[0], [1, 0, 0, 0, 0, 0]); |
| 1222 | assert_eq!(FRAME_EXP_STRATEGY[31], [3, 3, 3, 3, 3, 3]); |
| 1223 | assert_eq!(FRAME_EXP_STRATEGY[16], [3, 1, 0, 0, 0, 0]); |
| 1224 | // Every combination starts by sending exponents, never by reusing |
| 1225 | // exponents that do not exist yet. |
| 1226 | for row in FRAME_EXP_STRATEGY { |
| 1227 | assert_ne!(row[0], 0); |
| 1228 | } |
| 1229 | } |
| 1230 | } |