FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
hevc: never crash — no expect/unwrap in the decoder, deny them
Brandon Thomas970653628f6a
diff --git a/crates/hevc/src/decoder.rs b/crates/hevc/src/decoder.rsindex ee851f5b8..4b87295f4 100644--- a/crates/hevc/src/decoder.rs+++ b/crates/hevc/src/decoder.rs@@ -208,7 +208,7 @@ impl Decoder {if !o.frame.is_complete() && self.out_queue.len() <= max_queue {break;}- let o = self.out_queue.pop_front().expect("front exists");+ let Some(o) = self.out_queue.pop_front() else { break };pics.push(make_picture(&o));}Ok(pics)@@ -346,7 +346,9 @@ impl Decoder {let Some(p) = &self.pending else { return invalid("slice segment without the first slice of its picture") };ensure!(Arc::ptr_eq(&p.pps, &pps), "PPS changed within a picture");}- let pending = self.pending.as_mut().expect("picture started");+ let Some(pending) = self.pending.as_mut() else {+ return invalid("slice segment without the first slice of its picture");+ };let refs = build_ref_lists(&sh, &pending.rps)?;if !sh.dependent {pending.last_sh = sh.clone();diff --git a/crates/hevc/src/filter.rs b/crates/hevc/src/filter.rsindex 9f4546ced..479e26978 100644--- a/crates/hevc/src/filter.rs+++ b/crates/hevc/src/filter.rs@@ -501,10 +501,9 @@ fn sao_edge_run(cur: &[u16], na: &[u16], nb: &[u16], out: &mut [u16], by_sum: &[let n = cur.len();let full = n - n % SAO_LANES;for i in (0..full).step_by(SAO_LANES) {- let c: &[u16; SAO_LANES] = cur[i..i + SAO_LANES].try_into().expect("lanes");- let a: &[u16; SAO_LANES] = na[i..i + SAO_LANES].try_into().expect("lanes");- let b: &[u16; SAO_LANES] = nb[i..i + SAO_LANES].try_into().expect("lanes");- let d: &mut [u16; SAO_LANES] = (&mut out[i..i + SAO_LANES]).try_into().expect("lanes");+ let (Some(c), Some(a), Some(b), Some(d)) = (lanes(cur, i), lanes(na, i), lanes(nb, i), lanes_mut(out, i)) else {+ return;+ };for l in 0..SAO_LANES {d[l] = lane(c[l] as i16, a[l] as i16, b[l] as i16);}@@ -514,6 +513,17 @@ fn sao_edge_run(cur: &[u16], na: &[u16], nb: &[u16], out: &mut [u16], by_sum: &[}}+/// `SAO_LANES` samples from `i`, if all present.+#[inline(always)]+fn lanes(s: &[u16], i: usize) -> Option<&[u16; SAO_LANES]> {+ s.get(i..)?.first_chunk()+}++#[inline(always)]+fn lanes_mut(s: &mut [u16], i: usize) -> Option<&mut [u16; SAO_LANES]> {+ s.get_mut(i..)?.first_chunk_mut()+}+/// Band offset of a run of samples: the band index relative to `band_pos` selects one of the/// four offsets per lane.#[inline(always)]@@ -529,8 +539,9 @@ fn sao_band_run(src: &[u16], out: &mut [u16], shift: u32, (band_pos, offs): (i32let n = src.len();let full = n - n % SAO_LANES;for i in (0..full).step_by(SAO_LANES) {- let c: &[u16; SAO_LANES] = src[i..i + SAO_LANES].try_into().expect("lanes");- let d: &mut [u16; SAO_LANES] = (&mut out[i..i + SAO_LANES]).try_into().expect("lanes");+ let (Some(c), Some(d)) = (lanes(src, i), lanes_mut(out, i)) else {+ return;+ };for l in 0..SAO_LANES {d[l] = lane(c[l] as i16);}diff --git a/crates/hevc/src/inter.rs b/crates/hevc/src/inter.rsindex fc1e9aacb..b5feeb41f 100644--- a/crates/hevc/src/inter.rs+++ b/crates/hevc/src/inter.rs@@ -25,12 +25,16 @@ fn filt_w<const T: usize, const W: usize>(src: &[i16], step: usize, f: &[i8; T],let mut acc = [0i32; W];for k in 0..T {let c = f[k] as i32;- let p: &[i16; W] = src[k * step..k * step + W].try_into().expect("taps");+ let Some(p) = src.get(k * step..).and_then(|s| s.first_chunk::<W>()) else {+ return;+ };for j in 0..W {acc[j] += c * p[j] as i32;}}- let o: &mut [i16; W] = (&mut out[..W]).try_into().expect("row");+ let Some(o) = out.first_chunk_mut::<W>() else {+ return;+ };for j in 0..W {o[j] = (acc[j] >> shift) as i16;}diff --git a/crates/hevc/src/lib.rs b/crates/hevc/src/lib.rsindex 975e0c4f3..a9fa837f0 100644--- a/crates/hevc/src/lib.rs+++ b/crates/hevc/src/lib.rs@@ -19,6 +19,7 @@// Index loops over fixed-size blocks read more clearly than iterator chains in codec code.#![allow(clippy::needless_range_loop, clippy::too_many_arguments)]+#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]mod cabac;mod decoder;diff --git a/crates/hevc/src/params.rs b/crates/hevc/src/params.rsindex d866f9214..a7f7daf0b 100644--- a/crates/hevc/src/params.rs+++ b/crates/hevc/src/params.rs@@ -869,12 +869,16 @@ impl Layout {rowh.push(h - sh as u32);}let mut col_bd = vec![0];+ let mut acc = 0;for c in &colw {- col_bd.push(col_bd.last().unwrap() + c);+ acc += c;+ col_bd.push(acc);}let mut row_bd = vec![0];+ let mut acc = 0;for r in &rowh {- row_bd.push(row_bd.last().unwrap() + r);+ acc += r;+ row_bd.push(acc);}let n = (w * h) as usize;let mut rs_to_ts = vec![0u32; n];diff --git a/crates/hevc/src/slicedec.rs b/crates/hevc/src/slicedec.rsindex 4cd9deb88..949485648 100644--- a/crates/hevc/src/slicedec.rs+++ b/crates/hevc/src/slicedec.rs@@ -1464,15 +1464,14 @@ impl<'a> SliceDecoder<'a> {}let stride = if c == 0 { self.pic.width } else { self.pic.cwidth };let dst = &mut self.pic.planes[c][y as usize * stride + x as usize..];- if !weighted {+ let Some(pwt) = self.sh.pwt.as_ref().filter(|_| weighted) else {if bi {inter::put_bi(&self.pred0, &self.pred1, bw, bh, bd, dst, stride);} else {inter::put_uni(&self.pred0, bw, bh, bd, dst, stride);}continue;- }- let pwt = self.sh.pwt.as_ref().expect("weighted prediction table");+ };let shift1 = 14 - bd;let log2wd = if c == 0 { pwt.luma_log2_denom } else { pwt.chroma_log2_denom } + shift1;let get = |l: usize| -> (i32, i32) {