Merge branch 'simplify/s11' into simplify/w2
Browse files at this commit7 changed files +71 -63
- crates/ec-ac3/src/decode.rs +7 -0
- crates/ec-ac3/src/decoder.rs +31 -29
- crates/ec-ac3/src/dependent.rs +2 -1
- crates/ec-ac3/src/lib.rs +1 -1
- crates/ec-flac/src/encode.rs +28 -14
- crates/ec-opus/src/celt.rs +1 -1
- crates/ec-opus/src/celt_flp/vq.rs +1 -17
crates/ec-ac3/src/decode.rsmodified+7 -0
| @@ -35,6 +35,13 @@ pub enum Syntax { | ||
| 35 | 35 | Eac3, |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | +impl Syntax { | |
| 39 | + pub(crate) fn of(data: &[u8]) -> Option<Syntax> { | |
| 40 | + let bsid = *data.get(5)? >> 3; | |
| 41 | + Some(if bsid <= 10 { Syntax::Ac3 } else { Syntax::Eac3 }) | |
| 42 | + } | |
| 43 | +} | |
| 44 | + | |
| 38 | 45 | /// The decoder state a stream carries across blocks and frames. |
| 39 | 46 | pub(crate) struct Core { |
| 40 | 47 | pub(crate) syntax: Syntax, |
crates/ec-ac3/src/decoder.rsmodified+31 -29
| @@ -175,7 +175,7 @@ impl Ac3Decoder { | ||
| 175 | 175 | let mut pos = first.consumed; |
| 176 | 176 | while begins_syncframe(&data[pos..]) { |
| 177 | 177 | let tail = &data[pos..]; |
| 178 | - if tail[5] >> 3 > 10 { | |
| 178 | + if Syntax::of(tail) == Some(Syntax::Eac3) { | |
| 179 | 179 | let hdr = eac3::parse_from(&mut ec_core::BitReader::new(tail))?; |
| 180 | 180 | if hdr.substreamid != 0 { |
| 181 | 181 | return Err(Error::unsupported( |
| @@ -270,9 +270,8 @@ impl Ac3Decoder { | ||
| 270 | 270 | // two for E-AC-3 — so read it where both agree it can be found: after |
| 271 | 271 | // the sync word, E-AC-3 puts strmtyp/substreamid/frmsiz first, and |
| 272 | 272 | // bsid is at a fixed offset from the frame start in both. |
| 273 | - let bsid = data[5] >> 3; | |
| 274 | 273 | let mut r = ec_core::BitReader::new(data); |
| 275 | - if bsid <= 10 { | |
| 274 | + if Syntax::of(data) == Some(Syntax::Ac3) { | |
| 276 | 275 | let sync = syncinfo::parse_from(&mut r)?; |
| 277 | 276 | let bsi = bsi::parse_from(&mut r)?; |
| 278 | 277 | if data.len() < sync.frame_size { |
| @@ -398,26 +397,25 @@ impl Ac3Decoder { | ||
| 398 | 397 | /// The layout of a frame whose channels sit at `positions` (canonical order). |
| 399 | 398 | fn layout(&self, positions: &[ChannelPosition]) -> ChannelLayout { |
| 400 | 399 | match self.options.downmix { |
| 401 | - Downmix::Native => layout_of(positions), | |
| 400 | + Downmix::Native => match positions { | |
| 401 | + [ChannelPosition::FrontCenter] => ChannelLayout::Mono, | |
| 402 | + [ChannelPosition::FrontLeft, ChannelPosition::FrontRight] => ChannelLayout::Stereo, | |
| 403 | + _ if positions == ChannelLayout::Surround5_1.positions() => { | |
| 404 | + ChannelLayout::Surround5_1 | |
| 405 | + } | |
| 406 | + _ if positions == ChannelLayout::Surround7_1.positions() => { | |
| 407 | + ChannelLayout::Surround7_1 | |
| 408 | + } | |
| 409 | + _ => ChannelLayout::Custom(positions.to_vec()), | |
| 410 | + }, | |
| 402 | 411 | Downmix::Stereo => ChannelLayout::Stereo, |
| 403 | 412 | Downmix::Mono => ChannelLayout::Mono, |
| 404 | 413 | } |
| 405 | 414 | } |
| 406 | 415 | |
| 407 | - /// Apply the requested downmix, if any (§7.8.2). | |
| 408 | - fn fold(&self, samples: Vec<f32>, info: &FrameInfo, positions: &[ChannelPosition]) -> Vec<f32> { | |
| 409 | - if self.options.downmix == Downmix::Native { | |
| 410 | - return samples; | |
| 411 | - } | |
| 412 | - let channels = positions.len(); | |
| 413 | - if channels == 0 { | |
| 414 | - return samples; | |
| 415 | - } | |
| 416 | + fn downmix_gains(info: &FrameInfo, positions: &[ChannelPosition]) -> (Vec<f32>, Vec<f32>) { | |
| 416 | 417 | let clev = info.center_mix_level.unwrap_or(0.707); |
| 417 | 418 | let slev = info.surround_mix_level.unwrap_or(0.707); |
| 418 | - // §7.8.2 with the standard's own coefficients, then one scaling so a | |
| 419 | - // full-scale input cannot overflow the downmix — the standard's fixed | |
| 420 | - // -7.65 dB would quieten every stereo listener instead. | |
| 421 | 419 | let (mut left, mut right) = (Vec::new(), Vec::new()); |
| 422 | 420 | for &p in positions { |
| 423 | 421 | let (l, r) = match p { |
| @@ -438,6 +436,22 @@ impl Ac3Decoder { | ||
| 438 | 436 | left.push(l); |
| 439 | 437 | right.push(r); |
| 440 | 438 | } |
| 439 | + (left, right) | |
| 440 | + } | |
| 441 | + | |
| 442 | + /// Apply the requested downmix, if any (§7.8.2). | |
| 443 | + fn fold(&self, samples: Vec<f32>, info: &FrameInfo, positions: &[ChannelPosition]) -> Vec<f32> { | |
| 444 | + if self.options.downmix == Downmix::Native { | |
| 445 | + return samples; | |
| 446 | + } | |
| 447 | + let channels = positions.len(); | |
| 448 | + if channels == 0 { | |
| 449 | + return samples; | |
| 450 | + } | |
| 451 | + // The standard's own coefficients, then one scaling so a full-scale | |
| 452 | + // input cannot overflow the downmix — the standard's fixed -7.65 dB | |
| 453 | + // would quieten every stereo listener instead. | |
| 454 | + let (left, right) = Self::downmix_gains(info, positions); | |
| 441 | 455 | let peak = left |
| 442 | 456 | .iter() |
| 443 | 457 | .sum::<f32>() |
| @@ -513,18 +527,6 @@ impl Decoder for Ac3Decoder { | ||
| 513 | 527 | } |
| 514 | 528 | } |
| 515 | 529 | |
| 516 | -/// The layout of canonically ordered `positions`: the named layouts where they | |
| 517 | -/// match, a custom one otherwise. | |
| 518 | -fn layout_of(positions: &[ChannelPosition]) -> ChannelLayout { | |
| 519 | - match positions { | |
| 520 | - [ChannelPosition::FrontCenter] => ChannelLayout::Mono, | |
| 521 | - [ChannelPosition::FrontLeft, ChannelPosition::FrontRight] => ChannelLayout::Stereo, | |
| 522 | - _ if positions == ChannelLayout::Surround5_1.positions() => ChannelLayout::Surround5_1, | |
| 523 | - _ if positions == ChannelLayout::Surround7_1.positions() => ChannelLayout::Surround7_1, | |
| 524 | - _ => ChannelLayout::Custom(positions.to_vec()), | |
| 525 | - } | |
| 526 | -} | |
| 527 | - | |
| 528 | 530 | /// One syncframe as decoded: its interleaved samples in the requested layout, |
| 529 | 531 | /// the header summary, and the bytes its independent and dependent substreams |
| 530 | 532 | /// took. |
| @@ -545,7 +547,7 @@ fn begins_syncframe(tail: &[u8]) -> bool { | ||
| 545 | 547 | if tail.len() < 6 || u16::from_be_bytes([tail[0], tail[1]]) != syncinfo::SYNCWORD { |
| 546 | 548 | return false; |
| 547 | 549 | } |
| 548 | - if tail[5] >> 3 > 10 { | |
| 550 | + if Syntax::of(tail) == Some(Syntax::Eac3) { | |
| 549 | 551 | eac3::parse_from(&mut ec_core::BitReader::new(tail)).is_ok() |
| 550 | 552 | } else { |
| 551 | 553 | syncinfo::parse(tail).is_ok() |
crates/ec-ac3/src/dependent.rsmodified+2 -1
| @@ -29,6 +29,7 @@ | ||
| 29 | 29 | use ec_core::{BitReader, ChannelPosition, Error, Result}; |
| 30 | 30 | |
| 31 | 31 | use crate::bsi::Acmod; |
| 32 | +use crate::decode::Syntax; | |
| 32 | 33 | use crate::eac3::{self, Eac3Bsi, StreamType}; |
| 33 | 34 | use crate::syncinfo::SYNCWORD; |
| 34 | 35 | |
| @@ -46,7 +47,7 @@ pub(crate) struct Channel { | ||
| 46 | 47 | pub(crate) fn peek(data: &[u8]) -> Option<Eac3Bsi> { |
| 47 | 48 | // Six bytes are needed for the sync word and bsid; AC-3 (bsid <= 10) has |
| 48 | 49 | // no dependent substreams. |
| 49 | - if data.len() < 6 || u16::from_be_bytes([data[0], data[1]]) != SYNCWORD || data[5] >> 3 <= 10 { | |
| 50 | + if Syntax::of(data) != Some(Syntax::Eac3) || u16::from_be_bytes([data[0], data[1]]) != SYNCWORD { | |
| 50 | 51 | return None; |
| 51 | 52 | } |
| 52 | 53 | let hdr = eac3::parse_from(&mut BitReader::new(data)).ok()?; |
crates/ec-ac3/src/lib.rsmodified+1 -1
| @@ -100,7 +100,7 @@ pub fn frame_size(data: &[u8]) -> ec_core::Result<usize> { | ||
| 100 | 100 | "AC-3: no syncword at the start of the frame", |
| 101 | 101 | )); |
| 102 | 102 | } |
| 103 | - if data[5] >> 3 <= 10 { | |
| 103 | + if decode::Syntax::of(data) == Some(decode::Syntax::Ac3) { | |
| 104 | 104 | Ok(syncinfo::parse(data)?.frame_size) |
| 105 | 105 | } else { |
| 106 | 106 | Ok(eac3::parse(&data[2..])?.frame_size) |
crates/ec-flac/src/encode.rsmodified+28 -14
| @@ -9,7 +9,7 @@ use ec_core::bitio::BitWriter; | ||
| 9 | 9 | use ec_core::error::{Error, Result}; |
| 10 | 10 | |
| 11 | 11 | use crate::checksum::{crc8, crc16, md5_of_samples}; |
| 12 | -use crate::decode::StreamInfo; | |
| 12 | +use crate::decode::{ChannelAssignment, StreamInfo}; | |
| 13 | 13 | |
| 14 | 14 | /// Encoder settings. [`EncoderConfig::default`] is the level the family ships: |
| 15 | 15 | /// 4096-sample blocks, LPC to order 12, Rice partitions to order 6. |
| @@ -222,7 +222,7 @@ fn encode_frame( | ||
| 222 | 222 | .iter() |
| 223 | 223 | .map(|plane| plan_subframe(config, plane, bits_per_sample, window)) |
| 224 | 224 | .collect(); |
| 225 | - let mut assignment = channels as u32 - 1; | |
| 225 | + let mut assignment = ChannelAssignment::Independent(channels as u8); | |
| 226 | 226 | if stereo { |
| 227 | 227 | let mid = plan_subframe(config, &scratch.mid, bits_per_sample, window); |
| 228 | 228 | // The side channel needs one bit more than the stream's depth, which is |
| @@ -230,23 +230,23 @@ fn encode_frame( | ||
| 230 | 230 | let side = plan_subframe(config, &scratch.side, bits_per_sample + 1, window); |
| 231 | 231 | let (l, r) = (plans[0].bits, plans[1].bits); |
| 232 | 232 | let (_, best) = [ |
| 233 | - (l + r, 1u32), | |
| 234 | - (l + side.bits, 8), | |
| 235 | - (side.bits + r, 9), | |
| 236 | - (mid.bits + side.bits, 10), | |
| 233 | + (l + r, ChannelAssignment::Independent(2)), | |
| 234 | + (l + side.bits, ChannelAssignment::LeftSide), | |
| 235 | + (side.bits + r, ChannelAssignment::RightSide), | |
| 236 | + (mid.bits + side.bits, ChannelAssignment::MidSide), | |
| 237 | 237 | ] |
| 238 | 238 | .into_iter() |
| 239 | - .min() | |
| 239 | + .min_by_key(|candidate| candidate.0) | |
| 240 | 240 | .expect("four candidates"); |
| 241 | 241 | assignment = best; |
| 242 | 242 | match best { |
| 243 | - 8 => plans[1] = side, | |
| 244 | - 9 => plans[0] = side, | |
| 245 | - 10 => { | |
| 243 | + ChannelAssignment::LeftSide => plans[1] = side, | |
| 244 | + ChannelAssignment::RightSide => plans[0] = side, | |
| 245 | + ChannelAssignment::MidSide => { | |
| 246 | 246 | plans[0] = mid; |
| 247 | 247 | plans[1] = side; |
| 248 | 248 | } |
| 249 | - _ => {} | |
| 249 | + ChannelAssignment::Independent(_) => {} | |
| 250 | 250 | } |
| 251 | 251 | } |
| 252 | 252 | |
| @@ -263,7 +263,12 @@ fn encode_frame( | ||
| 263 | 263 | let crc = crc8(w.as_bytes()); |
| 264 | 264 | w.write_bits(u32::from(crc), 8); |
| 265 | 265 | for (c, plan) in plans.iter().enumerate() { |
| 266 | - let side = matches!((assignment, c), (8, 1) | (10, 1) | (9, 0)); | |
| 266 | + let side = matches!( | |
| 267 | + (assignment, c), | |
| 268 | + (ChannelAssignment::LeftSide, 1) | |
| 269 | + | (ChannelAssignment::MidSide, 1) | |
| 270 | + | (ChannelAssignment::RightSide, 0) | |
| 271 | + ); | |
| 267 | 272 | write_subframe(&mut w, plan, bits_per_sample + u32::from(side)); |
| 268 | 273 | } |
| 269 | 274 | w.align_to_byte(); |
| @@ -272,11 +277,20 @@ fn encode_frame( | ||
| 272 | 277 | w.into_bytes() |
| 273 | 278 | } |
| 274 | 279 | |
| 280 | +fn assignment_code(assignment: ChannelAssignment) -> u32 { | |
| 281 | + match assignment { | |
| 282 | + ChannelAssignment::Independent(n) => u32::from(n) - 1, | |
| 283 | + ChannelAssignment::LeftSide => 8, | |
| 284 | + ChannelAssignment::RightSide => 9, | |
| 285 | + ChannelAssignment::MidSide => 10, | |
| 286 | + } | |
| 287 | +} | |
| 288 | + | |
| 275 | 289 | fn write_frame_header( |
| 276 | 290 | w: &mut BitWriter, |
| 277 | 291 | block_size: usize, |
| 278 | 292 | sample_rate: u32, |
| 279 | - assignment: u32, | |
| 293 | + assignment: ChannelAssignment, | |
| 280 | 294 | bits_per_sample: u32, |
| 281 | 295 | number: u64, |
| 282 | 296 | ) { |
| @@ -330,7 +344,7 @@ fn write_frame_header( | ||
| 330 | 344 | w.write_bit(false); // fixed block size: the coded number is a frame number |
| 331 | 345 | w.write_bits(bs_code, 4); |
| 332 | 346 | w.write_bits(sr_code, 4); |
| 333 | - w.write_bits(assignment, 4); | |
| 347 | + w.write_bits(assignment_code(assignment), 4); | |
| 334 | 348 | w.write_bits(bps_code, 3); |
| 335 | 349 | w.write_bit(false); // reserved |
| 336 | 350 | write_utf8_number(w, number); |
crates/ec-opus/src/celt.rsmodified+1 -1
| @@ -3001,7 +3001,7 @@ pub(crate) fn exp_rotation( | ||
| 3001 | 3001 | } |
| 3002 | 3002 | } |
| 3003 | 3003 | |
| 3004 | -fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { | |
| 3004 | +pub(crate) fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { | |
| 3005 | 3005 | for i in 0..len.saturating_sub(stride) { |
| 3006 | 3006 | let x1 = x[i]; |
| 3007 | 3007 | let x2 = x[i + stride]; |
crates/ec-opus/src/celt_flp/vq.rsmodified+1 -17
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use super::{EPSILON, celt_atan2p_norm, celt_cos_norm, celt_rcp, celt_rsqrt, celt_sqrt}; |
| 2 | -use crate::celt::{pvq_u, pvq_v}; | |
| 2 | +use crate::celt::{exp_rotation1, pvq_u, pvq_v}; | |
| 3 | 3 | use crate::range::RangeEncoder; |
| 4 | 4 | |
| 5 | 5 | const SPREAD_FACTOR: [i32; 3] = [15, 10, 5]; |
| @@ -13,22 +13,6 @@ fn inner_prod(x: &[f32], y: &[f32], n: usize) -> f32 { | ||
| 13 | 13 | xy |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | -fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { | |
| 17 | - let ms = -s; | |
| 18 | - for i in 0..len.saturating_sub(stride) { | |
| 19 | - let x1 = x[i]; | |
| 20 | - let x2 = x[i + stride]; | |
| 21 | - x[i + stride] = c * x2 + s * x1; | |
| 22 | - x[i] = c * x1 + ms * x2; | |
| 23 | - } | |
| 24 | - for i in (0..len.saturating_sub(2 * stride)).rev() { | |
| 25 | - let x1 = x[i]; | |
| 26 | - let x2 = x[i + stride]; | |
| 27 | - x[i + stride] = c * x2 + s * x1; | |
| 28 | - x[i] = c * x1 + ms * x2; | |
| 29 | - } | |
| 30 | -} | |
| 31 | - | |
| 32 | 16 | /// `exp_rotation`. |
| 33 | 17 | pub(crate) fn exp_rotation( |
| 34 | 18 | x: &mut [f32], |