mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 08:15:29 +00:00
craft-audio: address review findings on overflow checks and sample bounds
This commit is contained in:
1 parent
485f8e45f4
commit
bbe92b892e
4 files changed
+13
-8
No files matched your search
@@ -71,7 +71,7 @@ pub fn samples_per_block(block_align: usize, channels: usize) -> Option<usize> {
|
||||
}
|
||||
let header = 4 * channels;
|
||||
let body = block_align.checked_sub(header)?;
|
||||
(body > 0 && body.is_multiple_of(4 * channels)).then(|| body / channels * 2 + 1)
|
||||
if body > 0 && body.is_multiple_of(4 * channels) { (body / channels).checked_mul(2)?.checked_add(1) } else { None }
|
||||
}
|
||||
|
||||
/// Decode one block, appending its samples interleaved by channel to `out`.
|
||||
|
||||
@@ -32,7 +32,9 @@ impl State {
|
||||
let sample = clip16(predicted + signed * self.delta);
|
||||
self.s2 = self.s1;
|
||||
self.s1 = sample;
|
||||
self.delta = ((ADAPT.get(usize::from(nibble)).copied().unwrap_or(256) * self.delta) >> 8).max(16);
|
||||
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);
|
||||
sample as i16
|
||||
}
|
||||
|
||||
@@ -56,7 +58,7 @@ pub fn samples_per_block(block_align: usize, channels: usize) -> Option<usize> {
|
||||
}
|
||||
let body = block_align.checked_sub(7 * channels).filter(|&b| b > 0)?;
|
||||
// codes pair up across channels
|
||||
Some(body * 2 / channels + 2)
|
||||
body.checked_mul(2).map(|b| b / channels + 2)
|
||||
}
|
||||
|
||||
/// Decode one block, appending its samples interleaved by channel to `out`.
|
||||
|
||||
@@ -93,6 +93,9 @@ pub fn decode(data: &[u8], channels: usize) -> Result<Vec<i16>> {
|
||||
states.push(State { predictor: i32::from(sample), index });
|
||||
}
|
||||
'block: for _ in 1..BLOCK_SAMPLES {
|
||||
if r.bits_left() < channels * (bits as usize) {
|
||||
break 'block;
|
||||
}
|
||||
for st in states.iter_mut() {
|
||||
match r.read_bits(bits) {
|
||||
Ok(code) => out.push(st.apply(code, bits)),
|
||||
@@ -125,7 +128,7 @@ pub fn encode(samples: &[i16], channels: usize, bits: u32) -> Result<Vec<u8>> {
|
||||
let first = col.first().copied().unwrap_or(0);
|
||||
// the index whose first codes follow the signal most closely
|
||||
let mut best = (i64::MAX, 0);
|
||||
for index in 0..=88 {
|
||||
for index in 0..=63 {
|
||||
let mut st = State { predictor: i32::from(first), index };
|
||||
let mut err = 0i64;
|
||||
for &s in col.iter().skip(1).take(16) {
|
||||
|
||||
@@ -470,8 +470,8 @@ fn flash_adpcm_matches_ffmpeg_in_both_directions() {
|
||||
ffmpeg(&ff, &["-i", &s(&file), "-f", "s16le", "-acodec", "pcm_s16le", &s(&back)]);
|
||||
let theirs = read_pcm(&back);
|
||||
let ours: Vec<i16> = tags.iter().flat_map(|t| swf::decode(t, channels).expect("decode")).collect();
|
||||
let n = ours.len().min(theirs.len());
|
||||
assert!(n >= 4096 * 3 * channels - 8192, "{n} samples");
|
||||
assert_eq!(ours.len(), 4096 * 3 * channels);
|
||||
assert_eq!(theirs.len(), 4096 * 3 * channels);
|
||||
let alt: Vec<i16> = tags.iter().flat_map(|t| alt_swf_decode(t, channels)).collect();
|
||||
same_as_either(&format!("swf {channels}ch"), &ours, &alt, &theirs);
|
||||
// ffmpeg decodes ours, one 4096-sample stream per tag
|
||||
@@ -483,8 +483,8 @@ fn flash_adpcm_matches_ffmpeg_in_both_directions() {
|
||||
let back = dir.join(format!("our_swf{channels}_{bits}.pcm"));
|
||||
ffmpeg(&ff, &["-i", &s(&file), "-f", "s16le", "-acodec", "pcm_s16le", &s(&back)]);
|
||||
let theirs = read_pcm(&back);
|
||||
let n = ours.len().min(theirs.len());
|
||||
assert!(n >= 4096 * 3 * channels - 8192, "{bits} bits: {n} samples");
|
||||
assert_eq!(ours.len(), 4096 * 3 * channels);
|
||||
assert_eq!(theirs.len(), 4096 * 3 * channels);
|
||||
let alt: Vec<i16> = tags.iter().flat_map(|t| alt_swf_decode(t, channels)).collect();
|
||||
same_as_either(&format!("swf {channels}ch {bits} bits, our stream"), &ours, &alt, &theirs);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user