forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'simplify/s11' into simplify/w2

Browse files at this commit

f05fdb1fe9d500feca4b6502c0ee77e28f765b19Ahmet Kaan GümüşParent: 3f8e1d596cce03
crates/ec-ac3/src/decode.rsmodified+7 -0
@@ -35,6 +35,13 @@ pub enum Syntax {
3535 Eac3,
3636 }
3737
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+
3845 /// The decoder state a stream carries across blocks and frames.
3946 pub(crate) struct Core {
4047 pub(crate) syntax: Syntax,
crates/ec-ac3/src/decoder.rsmodified+31 -29
@@ -175,7 +175,7 @@ impl Ac3Decoder {
175175 let mut pos = first.consumed;
176176 while begins_syncframe(&data[pos..]) {
177177 let tail = &data[pos..];
178- if tail[5] >> 3 > 10 {
178+ if Syntax::of(tail) == Some(Syntax::Eac3) {
179179 let hdr = eac3::parse_from(&mut ec_core::BitReader::new(tail))?;
180180 if hdr.substreamid != 0 {
181181 return Err(Error::unsupported(
@@ -270,9 +270,8 @@ impl Ac3Decoder {
270270 // two for E-AC-3 — so read it where both agree it can be found: after
271271 // the sync word, E-AC-3 puts strmtyp/substreamid/frmsiz first, and
272272 // bsid is at a fixed offset from the frame start in both.
273- let bsid = data[5] >> 3;
274273 let mut r = ec_core::BitReader::new(data);
275- if bsid <= 10 {
274+ if Syntax::of(data) == Some(Syntax::Ac3) {
276275 let sync = syncinfo::parse_from(&mut r)?;
277276 let bsi = bsi::parse_from(&mut r)?;
278277 if data.len() < sync.frame_size {
@@ -398,26 +397,25 @@ impl Ac3Decoder {
398397 /// The layout of a frame whose channels sit at `positions` (canonical order).
399398 fn layout(&self, positions: &[ChannelPosition]) -> ChannelLayout {
400399 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+ },
402411 Downmix::Stereo => ChannelLayout::Stereo,
403412 Downmix::Mono => ChannelLayout::Mono,
404413 }
405414 }
406415
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>) {
416417 let clev = info.center_mix_level.unwrap_or(0.707);
417418 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.
421419 let (mut left, mut right) = (Vec::new(), Vec::new());
422420 for &p in positions {
423421 let (l, r) = match p {
@@ -438,6 +436,22 @@ impl Ac3Decoder {
438436 left.push(l);
439437 right.push(r);
440438 }
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);
441455 let peak = left
442456 .iter()
443457 .sum::<f32>()
@@ -513,18 +527,6 @@ impl Decoder for Ac3Decoder {
513527 }
514528 }
515529
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-
528530 /// One syncframe as decoded: its interleaved samples in the requested layout,
529531 /// the header summary, and the bytes its independent and dependent substreams
530532 /// took.
@@ -545,7 +547,7 @@ fn begins_syncframe(tail: &[u8]) -> bool {
545547 if tail.len() < 6 || u16::from_be_bytes([tail[0], tail[1]]) != syncinfo::SYNCWORD {
546548 return false;
547549 }
548- if tail[5] >> 3 > 10 {
550+ if Syntax::of(tail) == Some(Syntax::Eac3) {
549551 eac3::parse_from(&mut ec_core::BitReader::new(tail)).is_ok()
550552 } else {
551553 syncinfo::parse(tail).is_ok()
crates/ec-ac3/src/dependent.rsmodified+2 -1
@@ -29,6 +29,7 @@
2929 use ec_core::{BitReader, ChannelPosition, Error, Result};
3030
3131 use crate::bsi::Acmod;
32+use crate::decode::Syntax;
3233 use crate::eac3::{self, Eac3Bsi, StreamType};
3334 use crate::syncinfo::SYNCWORD;
3435
@@ -46,7 +47,7 @@ pub(crate) struct Channel {
4647 pub(crate) fn peek(data: &[u8]) -> Option<Eac3Bsi> {
4748 // Six bytes are needed for the sync word and bsid; AC-3 (bsid <= 10) has
4849 // 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 {
5051 return None;
5152 }
5253 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> {
100100 "AC-3: no syncword at the start of the frame",
101101 ));
102102 }
103- if data[5] >> 3 <= 10 {
103+ if decode::Syntax::of(data) == Some(decode::Syntax::Ac3) {
104104 Ok(syncinfo::parse(data)?.frame_size)
105105 } else {
106106 Ok(eac3::parse(&data[2..])?.frame_size)
crates/ec-flac/src/encode.rsmodified+28 -14
@@ -9,7 +9,7 @@ use ec_core::bitio::BitWriter;
99 use ec_core::error::{Error, Result};
1010
1111 use crate::checksum::{crc8, crc16, md5_of_samples};
12-use crate::decode::StreamInfo;
12+use crate::decode::{ChannelAssignment, StreamInfo};
1313
1414 /// Encoder settings. [`EncoderConfig::default`] is the level the family ships:
1515 /// 4096-sample blocks, LPC to order 12, Rice partitions to order 6.
@@ -222,7 +222,7 @@ fn encode_frame(
222222 .iter()
223223 .map(|plane| plan_subframe(config, plane, bits_per_sample, window))
224224 .collect();
225- let mut assignment = channels as u32 - 1;
225+ let mut assignment = ChannelAssignment::Independent(channels as u8);
226226 if stereo {
227227 let mid = plan_subframe(config, &scratch.mid, bits_per_sample, window);
228228 // The side channel needs one bit more than the stream's depth, which is
@@ -230,23 +230,23 @@ fn encode_frame(
230230 let side = plan_subframe(config, &scratch.side, bits_per_sample + 1, window);
231231 let (l, r) = (plans[0].bits, plans[1].bits);
232232 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),
237237 ]
238238 .into_iter()
239- .min()
239+ .min_by_key(|candidate| candidate.0)
240240 .expect("four candidates");
241241 assignment = best;
242242 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 => {
246246 plans[0] = mid;
247247 plans[1] = side;
248248 }
249- _ => {}
249+ ChannelAssignment::Independent(_) => {}
250250 }
251251 }
252252
@@ -263,7 +263,12 @@ fn encode_frame(
263263 let crc = crc8(w.as_bytes());
264264 w.write_bits(u32::from(crc), 8);
265265 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+ );
267272 write_subframe(&mut w, plan, bits_per_sample + u32::from(side));
268273 }
269274 w.align_to_byte();
@@ -272,11 +277,20 @@ fn encode_frame(
272277 w.into_bytes()
273278 }
274279
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+
275289 fn write_frame_header(
276290 w: &mut BitWriter,
277291 block_size: usize,
278292 sample_rate: u32,
279- assignment: u32,
293+ assignment: ChannelAssignment,
280294 bits_per_sample: u32,
281295 number: u64,
282296 ) {
@@ -330,7 +344,7 @@ fn write_frame_header(
330344 w.write_bit(false); // fixed block size: the coded number is a frame number
331345 w.write_bits(bs_code, 4);
332346 w.write_bits(sr_code, 4);
333- w.write_bits(assignment, 4);
347+ w.write_bits(assignment_code(assignment), 4);
334348 w.write_bits(bps_code, 3);
335349 w.write_bit(false); // reserved
336350 write_utf8_number(w, number);
crates/ec-opus/src/celt.rsmodified+1 -1
@@ -3001,7 +3001,7 @@ pub(crate) fn exp_rotation(
30013001 }
30023002 }
30033003
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) {
30053005 for i in 0..len.saturating_sub(stride) {
30063006 let x1 = x[i];
30073007 let x2 = x[i + stride];
crates/ec-opus/src/celt_flp/vq.rsmodified+1 -17
@@ -1,5 +1,5 @@
11 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};
33 use crate::range::RangeEncoder;
44
55 const SPREAD_FACTOR: [i32; 3] = [15, 10, 5];
@@ -13,22 +13,6 @@ fn inner_prod(x: &[f32], y: &[f32], n: usize) -> f32 {
1313 xy
1414 }
1515
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-
3216 /// `exp_rotation`.
3317 pub(crate) fn exp_rotation(
3418 x: &mut [f32],