Merge branch 'simplify/b11' into simplify/w1a
Browse files at this commit4 changed files +46 -40
- crates/ec-alac/src/encode.rs +37 -31
- crates/ec-alac/tests/encode.rs +7 -7
- tools/ec-bench/src/main.rs +1 -1
- tools/golden/src/encode_audio.rs +1 -1
crates/ec-alac/src/encode.rsmodified+37 -31
| @@ -453,6 +453,13 @@ fn plan_of(c: Candidate) -> ChannelPlan { | ||
| 453 | 453 | } |
| 454 | 454 | } |
| 455 | 455 | |
| 456 | +fn sample_at(plane: &[u8], at: usize, format: SampleFormat) -> Option<i32> { | |
| 457 | + match format { | |
| 458 | + SampleFormat::S16 => Some(i32::from(i16::from_ne_bytes(plane.get(at..at + 2)?.try_into().ok()?))), | |
| 459 | + _ => Some(i32::from_ne_bytes(plane.get(at..at + 4)?.try_into().ok()?)), | |
| 460 | + } | |
| 461 | +} | |
| 462 | + | |
| 456 | 463 | /// `n` low bits set, for masking a two's-complement value into an escape's |
| 457 | 464 | /// raw field. |
| 458 | 465 | fn mask(n: u32) -> u32 { |
| @@ -543,9 +550,15 @@ impl AlacEncoder { | ||
| 543 | 550 | /// container-shifted range [`AlacDecoder::decode`] hands back), up to |
| 544 | 551 | /// `frame_length` samples per channel — a shorter slice writes a partial |
| 545 | 552 | /// frame, exactly as a stream's last frame does. |
| 546 | - pub fn encode_frame(&self, samples: &[i32]) -> Vec<u8> { | |
| 553 | + pub fn encode_frame(&self, samples: &[i32]) -> Result<Vec<u8>> { | |
| 547 | 554 | let channels = self.channels; |
| 548 | 555 | let n = samples.len() / channels; |
| 556 | + if n > self.cookie.frame_length as usize { | |
| 557 | + return Err(Error::unsupported( | |
| 558 | + format!("ALAC frame of {n} samples"), | |
| 559 | + format!("frame length is {}", self.cookie.frame_length), | |
| 560 | + )); | |
| 561 | + } | |
| 549 | 562 | let mut planes: Vec<Vec<i32>> = vec![Vec::with_capacity(n); channels]; |
| 550 | 563 | for chunk in samples.chunks_exact(channels) { |
| 551 | 564 | for (c, plane) in planes.iter_mut().enumerate() { |
| @@ -556,7 +569,7 @@ impl AlacEncoder { | ||
| 556 | 569 | w.put(if channels == 2 { ID_CPE } else { ID_SCE }, 3); |
| 557 | 570 | write_element(&self.cookie, &mut w, &planes, n, self.allow_shift); |
| 558 | 571 | w.put(ID_END, 3); |
| 559 | - w.finish() | |
| 572 | + Ok(w.finish()) | |
| 560 | 573 | } |
| 561 | 574 | } |
| 562 | 575 | |
| @@ -572,40 +585,32 @@ impl Encoder for AlacEncoder { | ||
| 572 | 585 | let channels = self.channels; |
| 573 | 586 | let shift = self.cookie.container_shift(); |
| 574 | 587 | let want = self.cookie.sample_format(); |
| 588 | + if audio.channels() != channels { | |
| 589 | + return Err(Error::unsupported( | |
| 590 | + format!("{}-channel input", audio.channels()), | |
| 591 | + format!("this encoder was opened for {channels} channels"), | |
| 592 | + )); | |
| 593 | + } | |
| 575 | 594 | if audio.format != want { |
| 576 | 595 | return Err(Error::unsupported( |
| 577 | 596 | format!("{:?} input", audio.format), |
| 578 | 597 | "this encoder takes samples in the cookie's own sample format", |
| 579 | 598 | )); |
| 580 | 599 | } |
| 600 | + let width = match want { | |
| 601 | + SampleFormat::S16 => 2, | |
| 602 | + _ => 4, | |
| 603 | + }; | |
| 604 | + let short = || Error::corrupt("ALAC: audio plane shorter than its sample count"); | |
| 581 | 605 | let mut interleaved = Vec::with_capacity(audio.samples * channels); |
| 582 | - match (audio.planar, want) { | |
| 583 | - (false, SampleFormat::S16) => { | |
| 584 | - for pair in audio.data[0].as_chunks::<2>().0.iter().take(audio.samples * channels) { | |
| 585 | - interleaved.push(i32::from(i16::from_ne_bytes([pair[0], pair[1]])) >> shift); | |
| 586 | - } | |
| 587 | - } | |
| 588 | - (false, _) => { | |
| 589 | - for word in audio.data[0].as_chunks::<4>().0.iter().take(audio.samples * channels) { | |
| 590 | - interleaved.push(i32::from_ne_bytes([word[0], word[1], word[2], word[3]]) >> shift); | |
| 591 | - } | |
| 592 | - } | |
| 593 | - (true, SampleFormat::S16) => { | |
| 594 | - for i in 0..audio.samples { | |
| 595 | - for plane in audio.data.iter().take(channels) { | |
| 596 | - let at = i * 2; | |
| 597 | - interleaved.push(i32::from(i16::from_ne_bytes([plane[at], plane[at + 1]])) >> shift); | |
| 598 | - } | |
| 599 | - } | |
| 600 | - } | |
| 601 | - (true, _) => { | |
| 602 | - for i in 0..audio.samples { | |
| 603 | - for plane in audio.data.iter().take(channels) { | |
| 604 | - let at = i * 4; | |
| 605 | - let w = [plane[at], plane[at + 1], plane[at + 2], plane[at + 3]]; | |
| 606 | - interleaved.push(i32::from_ne_bytes(w) >> shift); | |
| 607 | - } | |
| 608 | - } | |
| 606 | + for i in 0..audio.samples { | |
| 607 | + for c in 0..channels { | |
| 608 | + let (plane, at) = match audio.planar { | |
| 609 | + true => (audio.data.get(c), i * width), | |
| 610 | + false => (audio.data.first(), (i * channels + c) * width), | |
| 611 | + }; | |
| 612 | + let sample = plane.and_then(|p| sample_at(p, at, want)).ok_or_else(short)?; | |
| 613 | + interleaved.push(sample >> shift); | |
| 609 | 614 | } |
| 610 | 615 | } |
| 611 | 616 | self.pending.extend(interleaved); |
| @@ -613,7 +618,7 @@ impl Encoder for AlacEncoder { | ||
| 613 | 618 | let rest = self.pending.split_off(self.pending.len() / per_frame * per_frame); |
| 614 | 619 | let full = std::mem::replace(&mut self.pending, rest); |
| 615 | 620 | for frame in full.chunks_exact(per_frame) { |
| 616 | - let packet = self.encode_frame(frame); | |
| 621 | + let packet = self.encode_frame(frame)?; | |
| 617 | 622 | self.packets.push_back(packet); |
| 618 | 623 | } |
| 619 | 624 | Ok(()) |
| @@ -630,7 +635,8 @@ impl Encoder for AlacEncoder { | ||
| 630 | 635 | fn flush(&mut self) -> Result<()> { |
| 631 | 636 | if !self.pending.is_empty() { |
| 632 | 637 | let frame = std::mem::take(&mut self.pending); |
| 633 | - self.packets.push_back(self.encode_frame(&frame)); | |
| 638 | + let packet = self.encode_frame(&frame)?; | |
| 639 | + self.packets.push_back(packet); | |
| 634 | 640 | } |
| 635 | 641 | self.eof = true; |
| 636 | 642 | Ok(()) |
crates/ec-alac/tests/encode.rsmodified+7 -7
| @@ -34,7 +34,7 @@ fn round_trip(sample_rate: u32, channels: u8, bit_depth: u8, frame_length: u32, | ||
| 34 | 34 | let per_frame = frame_length as usize * channels as usize; |
| 35 | 35 | let mut decoded = Vec::with_capacity(samples.len()); |
| 36 | 36 | for chunk in samples.chunks(per_frame) { |
| 37 | - let packet = enc.encode_frame(chunk); | |
| 37 | + let packet = enc.encode_frame(chunk).expect("frame"); | |
| 38 | 38 | let out = dec.decode(&packet).expect("decode"); |
| 39 | 39 | // decode() shifts into the PCM container; undo that to compare |
| 40 | 40 | // against the true-range input encode_frame took. |
| @@ -55,7 +55,7 @@ fn mux_to_m4a(enc: &AlacEncoder, channels: usize, sample_rate: u32, samples: &[i | ||
| 55 | 55 | let mut pts = 0i64; |
| 56 | 56 | for chunk in samples.chunks(4096 * channels) { |
| 57 | 57 | let n = chunk.len() / channels; |
| 58 | - let data = enc.encode_frame(chunk); | |
| 58 | + let data = enc.encode_frame(chunk).expect("frame"); | |
| 59 | 59 | let mut packet = Packet::new(stream, time_base, data).with_pts(pts); |
| 60 | 60 | packet.duration = Some(n as i64); |
| 61 | 61 | mp4.write_packet(&packet).expect("write_packet"); |
| @@ -107,11 +107,11 @@ fn twenty_four_bit_uses_byte_shift_when_cheaper() { | ||
| 107 | 107 | |
| 108 | 108 | let per_frame = 4096 * channels as usize; |
| 109 | 109 | let with_shift = AlacEncoder::new(spec.sample_rate, channels, 24, 4096).expect("encoder"); |
| 110 | - let with_bytes: usize = samples.chunks(per_frame).map(|c| with_shift.encode_frame(c).len()).sum(); | |
| 110 | + let with_bytes: usize = samples.chunks(per_frame).map(|c| with_shift.encode_frame(c).expect("frame").len()).sum(); | |
| 111 | 111 | |
| 112 | 112 | let mut without_shift = AlacEncoder::new(spec.sample_rate, channels, 24, 4096).expect("encoder"); |
| 113 | 113 | without_shift.set_byte_shift(false); |
| 114 | - let without_bytes: usize = samples.chunks(per_frame).map(|c| without_shift.encode_frame(c).len()).sum(); | |
| 114 | + let without_bytes: usize = samples.chunks(per_frame).map(|c| without_shift.encode_frame(c).expect("frame").len()).sum(); | |
| 115 | 115 | |
| 116 | 116 | eprintln!("24-bit shifted: {with_bytes} bytes, unshifted: {without_bytes} bytes"); |
| 117 | 117 | assert!( |
| @@ -141,7 +141,7 @@ fn twenty_four_bit_tonal_content_round_trips_bit_exactly() { | ||
| 141 | 141 | let shift = enc.cookie().container_shift(); |
| 142 | 142 | let mut decoded = Vec::new(); |
| 143 | 143 | for chunk in samples.chunks(4096 * channels as usize) { |
| 144 | - decoded.extend(dec.decode(&enc.encode_frame(chunk)).expect("decode").iter().map(|&s| s >> shift)); | |
| 144 | + decoded.extend(dec.decode(&enc.encode_frame(chunk).expect("frame")).expect("decode").iter().map(|&s| s >> shift)); | |
| 145 | 145 | } |
| 146 | 146 | assert_eq!(&decoded, samples, "{channels}ch 24-bit tone, shift={allow_shift}"); |
| 147 | 147 | |
| @@ -180,7 +180,7 @@ fn real_audio_round_trips_and_compresses() { | ||
| 180 | 180 | let per_frame = 4096 * spec.channels as usize; |
| 181 | 181 | let mut coded_bytes = 0usize; |
| 182 | 182 | for chunk in samples.chunks(per_frame) { |
| 183 | - coded_bytes += enc.encode_frame(chunk).len(); | |
| 183 | + coded_bytes += enc.encode_frame(chunk).expect("frame").len(); | |
| 184 | 184 | } |
| 185 | 185 | let raw_bytes = samples.len() * 2; |
| 186 | 186 | let ratio = coded_bytes as f64 / raw_bytes as f64; |
| @@ -257,7 +257,7 @@ fn round_trip_both_decoders(channels: u8, samples: &[i32], allow_shift: bool, wh | ||
| 257 | 257 | let shift = enc.cookie().container_shift(); |
| 258 | 258 | let mut decoded = Vec::new(); |
| 259 | 259 | for chunk in samples.chunks(4096 * channels as usize) { |
| 260 | - decoded.extend(dec.decode(&enc.encode_frame(chunk)).expect("decode").iter().map(|&s| s >> shift)); | |
| 260 | + decoded.extend(dec.decode(&enc.encode_frame(chunk).expect("frame")).expect("decode").iter().map(|&s| s >> shift)); | |
| 261 | 261 | } |
| 262 | 262 | assert_eq!(&decoded, samples, "{what}: {channels}ch 24-bit, shift={allow_shift}"); |
| 263 | 263 |
tools/ec-bench/src/main.rsmodified+1 -1
| @@ -828,7 +828,7 @@ fn bench_audio_encode(rows: &mut Vec<Row>) { | ||
| 828 | 828 | let per_frame = 4096 * channels; |
| 829 | 829 | let start = Instant::now(); |
| 830 | 830 | for chunk in samples.chunks(per_frame) { |
| 831 | - let _ = enc.encode_frame(chunk); | |
| 831 | + enc.encode_frame(chunk).expect("alac frame"); | |
| 832 | 832 | } |
| 833 | 833 | let wall = start.elapsed().as_secs_f64(); |
| 834 | 834 | rows.push(Row { |
tools/golden/src/encode_audio.rsmodified+1 -1
| @@ -283,7 +283,7 @@ fn alac_encode(channels: u8, bits: u8, frame: u32) -> ec_core::Result<Vec<u8>> { | ||
| 283 | 283 | let samples = to_int(&synth(usize::from(channels), 48_000, FRAMES), u32::from(bits)); |
| 284 | 284 | let mut out = enc.cookie().to_bytes().to_vec(); |
| 285 | 285 | for chunk in samples.chunks(frame as usize * usize::from(channels)) { |
| 286 | - out.extend(enc.encode_frame(chunk)); | |
| 286 | + out.extend(enc.encode_frame(chunk)?); | |
| 287 | 287 | } |
| 288 | 288 | Ok(out) |
| 289 | 289 | } |