FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
security(h264): enforce long-term DPB limits (#2894)
Earl Coee871ca06c6b
diff --git a/crates/h264/src/decoder.rs b/crates/h264/src/decoder.rsindex 93c96f94d..135ebfa59 100644--- a/crates/h264/src/decoder.rs+++ b/crates/h264/src/decoder.rs@@ -238,7 +238,7 @@ impl Decoder {}}// The access unit is complete: hand its picture to a decoding job.- self.submit_pending();+ self.submit_pending()?;if let Some(e) = self.take_error() {result = result.and(Err(e));}@@ -258,7 +258,7 @@ impl Decoder {/// Output all remaining pictures (end of stream).pub fn flush(&mut self) -> Vec<Picture> {- self.submit_pending();+ let _ = self.submit_pending();let mut outs = Vec::new();self.dpb.flush(&mut outs);self.emit(outs);@@ -308,7 +308,7 @@ impl Decoder {nal_type::SLICE_DPA | nal_type::SLICE_DPB | nal_type::SLICE_DPC => {return unsupported("data partitioning (Extended profile)");}- nal_type::END_SEQ | nal_type::END_STREAM => self.submit_pending(),+ nal_type::END_SEQ | nal_type::END_STREAM => self.submit_pending()?,// SEI, AUD, filler, SPS extension, prefix NAL, subset SPS, auxiliary and extension slices: skipped._ => {}}@@ -357,7 +357,7 @@ impl Decoder {Some(p) => is_new_picture(&p.first, &sh, &sps) || sh.first_mb_in_slice == 0 || !Arc::ptr_eq(&p.sps, &sps),};if new_pic {- self.submit_pending();+ self.submit_pending()?;if sh.first_mb_in_slice != 0 && self.active_sps.is_none() {return invalid("stream does not start with the first slice of a picture");}@@ -472,8 +472,8 @@ impl Decoder {}/// Start decoding the pending picture and do its reference marking / DPB insertion.- fn submit_pending(&mut self) {- let Some(p) = self.pending.take() else { return };+ fn submit_pending(&mut self) -> Result<()> {+ let Some(p) = self.pending.take() else { return Ok(()) };let PendingPic { frame, sps, first, poc, pts, key, slices } = p;let draft = self.draft && first.nal_ref_idc == 0;self.dispatch(frame.clone(), slices, draft);@@ -484,8 +484,9 @@ impl Decoder {let meta = Arc::new(OutputMeta { draft, ..output_meta(&sps, pts, key) });let mut outs = Vec::new();let fpoc = frame.poc;- self.dpb.store_picture(&first, frame, fpoc, sps.max_frame_num(), sps.max_num_ref_frames as usize, meta, &mut outs);+ self.dpb.store_picture(&first, frame, fpoc, sps.max_frame_num(), sps.max_num_ref_frames as usize, meta, &mut outs)?;self.emit(outs);+ Ok(())}fn dispatch(&mut self, frame: FrameRef, slices: Vec<SliceJob>, draft: bool) {diff --git a/crates/h264/src/dpb.rs b/crates/h264/src/dpb.rsindex 657aa411a..df0385184 100644--- a/crates/h264/src/dpb.rs+++ b/crates/h264/src/dpb.rs@@ -260,7 +260,7 @@ impl<F: Clone> Dpb<F> {}/// Adaptive memory control (8.2.5.4). Returns true if MMCO 5 was executed.- fn apply_mmcos(&mut self, mmcos: &[Mmco], cur_frame_num: u32, max_frame_num: u32, cur_long: &mut Option<u32>) -> bool {+ fn apply_mmcos(&mut self, mmcos: &[Mmco], cur_frame_num: u32, max_frame_num: u32, cur_long: &mut Option<u32>) -> Result<bool> {let mut had5 = false;let curr_pic_num = cur_frame_num as i32;for m in mmcos {@@ -281,6 +281,7 @@ impl<F: Clone> Dpb<F> {}}3 => {+ ensure!(self.max_long_term_frame_idx.is_some_and(|max| m.long_term_frame_idx <= max), "MMCO long-term index exceeds the configured limit");let pn = curr_pic_num - (m.difference_of_pic_nums_minus1 as i32 + 1);for e in &mut self.entries {if e.mark == RefMark::Long && e.long_term_frame_idx == m.long_term_frame_idx {@@ -311,6 +312,7 @@ impl<F: Clone> Dpb<F> {had5 = true;}6 => {+ ensure!(self.max_long_term_frame_idx.is_some_and(|max| m.long_term_frame_idx <= max), "MMCO long-term index exceeds the configured limit");for e in &mut self.entries {if e.mark == RefMark::Long && e.long_term_frame_idx == m.long_term_frame_idx {e.mark = RefMark::Unused;@@ -322,7 +324,7 @@ impl<F: Clone> Dpb<F> {}}self.prune();- had5+ Ok(had5)}/// Reference marking for the just-decoded picture and insertion into the DPB with output bumping.@@ -337,7 +339,7 @@ impl<F: Clone> Dpb<F> {max_num_ref_frames: usize,meta: Arc<OutputMeta>,out: &mut Vec<Output<F>>,- ) {+ ) -> Result<()> {let mut mark = RefMark::Unused;let mut long_idx = 0;let mut had5 = false;@@ -353,7 +355,7 @@ impl<F: Clone> Dpb<F> {} else {let mut cur_long = None;if sh.adaptive_ref_pic_marking {- had5 = self.apply_mmcos(&sh.mmcos, sh.frame_num, max_frame_num, &mut cur_long);+ had5 = self.apply_mmcos(&sh.mmcos, sh.frame_num, max_frame_num, &mut cur_long)?;} else {self.sliding_window(sh.frame_num, max_frame_num, max_num_ref_frames);}@@ -369,6 +371,9 @@ impl<F: Clone> Dpb<F> {}}}+ if mark == RefMark::Long {+ ensure!(self.num_refs() < max_num_ref_frames.max(1), "long-term reference exceeds max_num_ref_frames");+ }if had5 {// All earlier pictures are output before the current one (C.4.4).self.flush(out);@@ -385,7 +390,7 @@ impl<F: Clone> Dpb<F> {let min_poc = self.entries.iter().filter(|e| e.needed_for_output).map(|e| e.poc).min();if min_poc.is_none_or(|m| poc < m) {out.push(Output { frame, poc, meta });- return;+ return Ok(());}if !self.bump(out) {break;@@ -405,6 +410,7 @@ impl<F: Clone> Dpb<F> {break;}}+ Ok(())}}diff --git a/crates/platform/src/vaapi/h264.rs b/crates/platform/src/vaapi/h264.rsindex bc79ee9a4..6e709c4ad 100644--- a/crates/platform/src/vaapi/h264.rs+++ b/crates/platform/src/vaapi/h264.rs@@ -333,7 +333,7 @@ impl<A: Accel> Front<A> {let fpoc = if mmco5 { 0 } else { p.poc.frame() };let meta = Arc::new(output_meta(&p.sps, p.pts, p.first.idr));let mut outs = Vec::new();- self.dpb.store_picture(&p.first, surface, fpoc, p.sps.max_frame_num(), p.sps.max_num_ref_frames as usize, meta, &mut outs);+ self.dpb.store_picture(&p.first, surface, fpoc, p.sps.max_frame_num(), p.sps.max_num_ref_frames as usize, meta, &mut outs).map_err(decode_err)?;self.read_out(outs, frames)}}