diff --git a/crates/media/README.md b/crates/media/README.md index 8e5799ec4..9bd6d0e4a 100644 --- a/crates/media/README.md +++ b/crates/media/README.md @@ -840,7 +840,8 @@ for (i, frame) in file.frames.iter().enumerate() { `header_bytes()` is the same header. - `picture_rect(index)` places a video frame's update in the picture. Frame coordinates are screen coordinates; a frame as large as the picture that is not at the screen's corner gives - the picture's position from then on, as ffmpeg was seen to do. + the picture's position from then on, as ffmpeg was seen to do. A frame with no rectangle in + the picture can still carry a palette (`craft_video::vmd::Decoder::decode_palette`). Gaps: only the 816-byte header is read. The later Coktel Vision revisions with a longer header or one without a palette are refused (`Error::NotVmd`). Files with 16-bit, 24-bit or Indeo 3 diff --git a/crates/media/src/vmd/mod.rs b/crates/media/src/vmd/mod.rs index f671e49f1..7c8f19f4d 100644 --- a/crates/media/src/vmd/mod.rs +++ b/crates/media/src/vmd/mod.rs @@ -359,7 +359,12 @@ impl VmdFile { if f.offset.checked_add(u64::from(f.size)).is_none_or(|end| end > src.len()) { return Err(Error::Invalid(format!("frame {index} reaches past the end of the file"))); } - let mut data = vec![0u8; f.size as usize]; + // fallibly: a size the address space cannot hold (above 2 GiB on a 32-bit target) is an + // error, not a capacity-overflow panic + let size = f.size as usize; + let mut data = Vec::new(); + data.try_reserve_exact(size).map_err(|_| Error::Invalid(format!("frame {index} is too large to load")))?; + data.resize(size, 0); src.read_at(f.offset, &mut data)?; Ok(data) } diff --git a/crates/video/src/vmd/mod.rs b/crates/video/src/vmd/mod.rs index f171bef10..53e08500a 100644 --- a/crates/video/src/vmd/mod.rs +++ b/crates/video/src/vmd/mod.rs @@ -25,8 +25,13 @@ //! let mut decoder = Decoder::new(file.header_bytes())?; //! for (i, frame) in file.frames.iter().enumerate() { //! let FrameKind::Video { new_palette, .. } = frame.kind else { continue }; -//! let Some(rect) = file.picture_rect(i) else { continue }; -//! decoder.decode(Rect::from(rect), new_palette, &file.read_frame(&bytes, i)?)?; +//! let data = file.read_frame(&bytes, i)?; +//! match file.picture_rect(i) { +//! Some(rect) => decoder.decode(Rect::from(rect), new_palette, &data)?, +//! // a frame that paints nothing in the picture can still bring a palette +//! None if new_palette => decoder.decode_palette(&data)?, +//! None => {} +//! } //! let rgb = decoder.rgb24(); // the picture after this block //! } //! # Ok(()) @@ -188,13 +193,8 @@ impl Decoder { /// frames still decode against it. pub fn decode(&mut self, rect: Rect, new_palette: bool, data: &[u8]) -> Result<()> { let mut data = data; - if data.is_empty() { - return Ok(()); - } - if new_palette { - // first index and count (ignored, see the module documentation), then 256 entries - let chunk = data.get(..770).ok_or(Error::Invalid("the palette is cut short"))?; - self.load_palette(&chunk[2..]); + if new_palette && !data.is_empty() { + self.decode_palette(data)?; data = &data[770..]; } let Some((&method, body)) = data.split_first() else { return Ok(()) }; @@ -214,6 +214,20 @@ impl Decoder { result } + /// Take the palette that the data of a frame with a new palette begins with, and paint + /// nothing: for a video frame that has no rectangle in the picture (an empty one, as ffmpeg + /// was seen to accept, or one outside the picture). [`Decoder::decode`] does this itself for + /// the frames it is given. An empty frame changes nothing. + pub fn decode_palette(&mut self, data: &[u8]) -> Result<()> { + if data.is_empty() { + return Ok(()); + } + // first index and count (ignored, see the module documentation), then 256 entries + let chunk = data.get(..770).ok_or(Error::Invalid("the palette is cut short"))?; + self.load_palette(&chunk[2..]); + Ok(()) + } + fn load_palette(&mut self, vga: &[u8]) { for (entry, rgb) in self.palette.iter_mut().zip(vga.as_chunks::<3>().0) { for (c, &v) in entry.iter_mut().zip(rgb) { diff --git a/crates/video/tests/vmd/main.rs b/crates/video/tests/vmd/main.rs index 02d9617f0..be35e5138 100644 --- a/crates/video/tests/vmd/main.rs +++ b/crates/video/tests/vmd/main.rs @@ -112,8 +112,12 @@ fn decode_all(bytes: &[u8]) -> Vec<(Vec, Vec)> { let mut out = Vec::new(); for (i, frame) in file.frames.iter().enumerate() { let FrameKind::Video { new_palette, .. } = frame.kind else { continue }; - let rect = Rect::from(file.picture_rect(i).unwrap()); - decoder.decode(rect, new_palette, &file.read_frame(bytes, i).unwrap()).unwrap(); + let data = file.read_frame(bytes, i).unwrap(); + match file.picture_rect(i) { + Some(rect) => decoder.decode(rect.into(), new_palette, &data).unwrap(), + None if new_palette => decoder.decode_palette(&data).unwrap(), + None => {} + } out.push((decoder.indices().to_vec(), decoder.rgb24())); } out @@ -327,6 +331,38 @@ fn palette_changes() { assert_eq!(d.decode(FULL, true, &[0; 769]), Err(Error::Invalid("the palette is cut short"))); } +/// A frame whose rectangle is empty paints nothing, but its palette counts: the pictures after +/// it use it. ffmpeg was seen to do the same with such a frame. +#[test] +fn palette_of_a_frame_without_a_rectangle() { + let spec = Spec::new(4, 2); + let picture = |fill: u8| [vec![2], vec![fill; 8]].concat(); + let mut palette = [0u8; 768]; + palette[5 * 3..][..3].copy_from_slice(&[63, 0, 63]); + palette[6 * 3..][..3].copy_from_slice(&[0, 63, 0]); + let bytes = build( + &spec, + &[ + vec![Frame::video([0, 0, 3, 1], false, picture(5))], + // right is one less than left: no pixels, a palette and a picture that is not painted + vec![Frame::video([1, 1, 0, 1], true, [palette_chunk(&palette), picture(6)].concat())], + vec![Frame::video([0, 0, 1, 0], false, picture(6)[..3].to_vec())], + ], + ); + assert_eq!(open(&bytes).unwrap().picture_rect(1), None); + let pictures = decode_all(&bytes); + // index 5 is dark grey in the header's palette + assert_eq!(pictures[0], (vec![5; 8], [4u8; 24].to_vec())); + assert_eq!(pictures[1], (vec![5; 8], [255, 0, 255].repeat(8))); + assert_eq!(pictures[2].0, [6, 6, 5, 5, 5, 5, 5, 5]); + assert_eq!(pictures[2].1[..9], [0, 255, 0, 0, 255, 0, 255, 0, 255]); + + let mut d = flat(); + d.decode_palette(&[]).unwrap(); + assert_eq!(d.palette()[0x40], [0x41; 3]); + assert_eq!(d.decode_palette(&[0; 769]), Err(Error::Invalid("the palette is cut short"))); +} + #[test] fn partial_updates_and_empty_frames() { let mut d = flat(); @@ -390,6 +426,7 @@ fn hostile_input_never_panics() { if let Some(rect) = file.picture_rect(i) { let _ = decoder.decode(rect.into(), new_palette, &data); } + let _ = decoder.decode_palette(&data); } let _ = decoder.rgb24(); });