video: a VMD decoder's picture converts into a VideoFrame

`VideoFrame::try_from(&vmd::Decoder)` looks the picture's palette indices
up in the decoder's palette and gives 8-bit R, G, B planes (matrix 0). The
README and the frame module docs said every decoder's output converts by
moving its buffers; paletted video copies, as VMD keeps only indices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
alehelandClaude Opus 5.5 committed 2026-11-17 04:54:38 +01:00
1 parent 00e0075184
commit 73e6b12930
4 files changed
+71 -2

No files matched your search

+3 -1
View File
@@ -42,7 +42,9 @@ comes with its codec's feature):
- Planes are `[Y, Cb, Cr]`, `[R, G, B]` or `[Y]` (`ColorFamily`), plus an optional alpha plane.
DNx, VP9 and AV1 (identity matrix) store RGB as G, B, R; the conversion reorders them.
- Samples keep the decoder's storage, `u8` or `u16`, with a bit depth per plane (HEVC chroma may
differ from luma). Converting moves the buffers and copies no samples.
differ from luma). Converting moves the buffers and copies no samples, except for paletted
video: VMD holds its picture as palette indices in the decoder, so `VideoFrame::try_from(&decoder)`
looks them up and writes new 8-bit R, G, B planes.
- Chroma subsampling is a log2 shift per axis, 0 to 3 (FFV1 codes up to 3).
- Colour is given as ITU-T H.273 code points (`ColorTags`), mapped from each codec's own
signalling. Codecs that do not signal a value leave it unspecified (2).
+34
View File
@@ -369,3 +369,37 @@ impl TryFrom<crate::ffv1::Frame> for VideoFrame {
Ok(f)
}
}
#[cfg(feature = "vmd")]
impl TryFrom<&crate::vmd::Decoder> for VideoFrame {
type Error = FrameError;
/// The decoder's current picture, its palette indices looked up in its palette: 8-bit R, G, B
/// planes at full size (the samples are copied, as VMD stores none of them as planes). VMD
/// signals no colour description beyond RGB, nor timing.
fn try_from(d: &crate::vmd::Decoder) -> Result<Self, FrameError> {
paletted(d.width(), d.height(), d.indices(), d.palette())
}
}
/// A picture of palette indices, `width` a row, as 8-bit R, G, B planes. Matrix 0 (identity), the
/// other colour tags unspecified.
#[cfg(feature = "vmd")]
fn paletted(width: usize, height: usize, indices: &[u8], palette: &[[u8; 3]; 256]) -> Result<VideoFrame, FrameError> {
let too_large = FrameError::Shape("picture size does not fit in u32");
let (w, h) = (u32::try_from(width).map_err(|_| too_large.clone())?, u32::try_from(height).map_err(|_| too_large)?);
let n = width.checked_mul(height).ok_or(FrameError::Shape("picture size overflows"))?;
let indices = indices.get(..n).ok_or(FrameError::Plane("fewer palette indices than pixels"))?;
let mut planes = [Vec::with_capacity(n), Vec::with_capacity(n), Vec::with_capacity(n)];
for &i in indices {
let [r, g, b] = palette[usize::from(i)];
planes[0].push(r);
planes[1].push(g);
planes[2].push(b);
}
let [r, g, b] = planes;
let planes = vec![Plane::packed(w, h, 8, Samples::U8(r))?, Plane::packed(w, h, 8, Samples::U8(g))?, Plane::packed(w, h, 8, Samples::U8(b))?];
let mut f = VideoFrame::new(w, h, ColorFamily::Rgb, Subsampling::S444, planes, None)?;
f.color.matrix = 0;
Ok(f)
}
+3 -1
View File
@@ -4,7 +4,9 @@
//! common: planar samples at their stored width (`u8` or `u16`), per-plane bit depth, chroma
//! subsampling, an optional alpha plane, colour tags as ITU-T H.273 code points, and timing.
//! Every decoder output converts into it with `VideoFrame::try_from`, behind the codec's feature.
//! A conversion moves the sample buffers; it does not copy or rescale samples.
//! A conversion moves the sample buffers; it does not copy or rescale samples. Paletted video is
//! the exception: VMD's decoder holds palette indices, and converting it (`&vmd::Decoder`) writes
//! new 8-bit R, G, B planes from its palette.
//!
//! Plane order is fixed by [`ColorFamily`]: `[Y, Cb, Cr]`, `[R, G, B]` or `[Y]`. Decoders that
//! store RGB in another order (DNx, VP9 and identity-matrix AV1 store G, B, R) are reordered by
+31
View File
@@ -131,6 +131,37 @@ fn rgb(indices: &[u8], palette: &[u8; 768]) -> Vec<u8> {
/// larger than the LZ window (4096).
const MOVIES: [(u64, usize, usize, usize); 5] = [(1, 40, 30, 40), (2, 300, 5, 30), (3, 1, 1, 6), (4, 7, 64, 30), (5, 96, 80, 12)];
/// Each picture converts into a `VideoFrame` of R, G, B planes holding the colours the decoder
/// gives as RGB.
#[test]
fn pictures_convert_to_rgb_video_frames() {
use craft_video::frame::{ColorFamily, Samples, VideoFrame};
for (seed, w, h, frames) in MOVIES {
let m = movie(seed, w, h, frames);
let file = open(&m.bytes).unwrap();
let mut decoder = Decoder::new(file.header_bytes()).unwrap();
for (i, frame) in file.frames.iter().enumerate() {
let FrameKind::Video { new_palette, .. } = frame.kind else { continue };
let data = file.read_frame(&m.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 => {}
}
let f = VideoFrame::try_from(&decoder).unwrap();
assert_eq!((f.width(), f.height(), f.family(), f.bit_depth()), (w as u32, h as u32, ColorFamily::Rgb, 8));
assert_eq!(f.color.matrix, 0);
let plane = |p: usize| match f.plane(p).map(|p| p.samples()) {
Some(Samples::U8(s)) => s.clone(),
other => panic!("plane {p}: {other:?}"),
};
let (r, g, b) = (plane(0), plane(1), plane(2));
let interleaved: Vec<u8> = (0..w * h).flat_map(|i| [r[i], g[i], b[i]]).collect();
assert!(interleaved == decoder.rgb24(), "movie {seed}: frame {i}");
}
}
}
#[test]
fn decodes_the_pictures_that_were_packed() {
for (seed, w, h, frames) in MOVIES {