craft-audio: bound the Microsoft ADPCM step; regression tests for the review findings

This commit is contained in:
el-j committed 2026-11-16 02:26:50 +01:00
1 parent 853c8e8a4d
commit 8314189cb0
2 files changed
+41 -1

No files matched your search

+17 -1
View File
@@ -14,6 +14,9 @@ use super::{Error, Result, clip16};
/// The seven coefficient pairs `(c1, c2)` every Microsoft ADPCM stream uses.
pub const COEFFICIENTS: [(i32, i32); 7] = [(256, 0), (512, -256), (0, 0), (192, 64), (240, 0), (460, -208), (392, -232)];
/// How the step moves with the code's unsigned value (the index is the 4-bit pattern).
/// The largest step. The reference keeps it in 16 bits and no real stream gets near this bound; it
/// keeps every product in range however long a damaged block runs.
const MAX_DELTA: i32 = 1 << 24;
const ADAPT: [i32; 16] = [230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230];
#[derive(Clone, Copy)]
@@ -34,7 +37,7 @@ impl State {
self.s1 = sample;
let adapt = i64::from(ADAPT.get(usize::from(nibble)).copied().unwrap_or(256));
let new_delta = (adapt * i64::from(self.delta)) >> 8;
self.delta = (new_delta.min(i32::MAX as i64) as i32).max(16);
self.delta = (new_delta.min(i64::from(MAX_DELTA)) as i32).max(16);
sample as i16
}
@@ -207,6 +210,19 @@ mod tests {
assert_eq!(samples_per_block(100, 3), None);
}
#[test]
fn a_long_run_of_large_codes_cannot_overflow() {
// a step that starts at its largest 16-bit value and grows with every large code
let mut block = vec![0x77u8; 7 + 2000];
block[..7].copy_from_slice(&[0, 255, 127, 0, 0, 0, 0]);
for b in block.iter_mut().skip(7) {
*b = 0x88;
}
let mut out = Vec::new();
decode_block(&block, 1, &mut out).unwrap();
assert_eq!(out.len(), 2000 * 2 + 2);
}
#[test]
fn bad_predictor_index_is_an_error() {
let mut block = vec![0u8; 64];
+24
View File
@@ -126,6 +126,30 @@ fn damaged_input_is_an_error_or_decodes_never_panics() {
let _ = swf::decode(&bad, 1);
}
#[test]
fn huge_layouts_and_big_jumps_are_handled() {
// block sizes whose sample counts do not fit in a usize
assert_eq!(ima::samples_per_block(usize::MAX - 3, 1), None);
assert_eq!(ms::samples_per_block(usize::MAX, 1), None);
assert!(ima::encode(&[], 1, usize::MAX - 3).is_err());
assert!(ms::encode(&[], 1, usize::MAX).is_err());
// a Flash stream's header holds the step index in six bits: a jump from silence to full scale
// still decodes to what the encoder's own state says
for bits in [2u32, 3, 4, 5] {
let x = [0i16, 32767, -32768, 0, 0];
let y = swf::decode(&swf::encode(&x, 1, bits).unwrap(), 1).unwrap();
// the stream has no length: up to seven padding bits can read as one more code
assert!((x.len()..=x.len() + 3).contains(&y.len()), "{bits} bits: {} samples", y.len());
assert!(y[1] > 0 && y[2] < y[1], "{bits} bits: {y:?}");
}
// padding bits at the end of a stream are not a code: no incomplete stereo frame
for bits in [2u32, 3, 4, 5] {
let y = swf::decode(&swf::encode(&[0, 0, 5, -5, 9, 9], 2, bits).unwrap(), 2).unwrap();
assert_eq!(y.len() % 2, 0, "{bits} bits: {y:?}");
assert!((6..=8).contains(&y.len()), "{bits} bits: {} samples", y.len());
}
}
#[test]
fn partial_final_blocks_are_handled() {
let x = signal(1, 1000);