media: present monochrome frames from the shared decoder (#4051)

This commit is contained in:
Terra854
2026-11-21 12:53:50 -05:00
committed by GitHub
parent 6e3459a8eb
commit f7ab031c3c
6 changed files with 98 additions and 1 deletions
+1
View File
@@ -162,3 +162,4 @@ under [`docs/brand/LICENSE-brand.txt`](docs/brand/LICENSE-brand.txt). Forks and
| Asset | Author | Source | Licence |
|---|---|---|---|
| `crates/media/tests/fixtures/pce61-ffmpeg.aac-fixture` | Dylan M. Taylor | Original sine waves; reproducible recipe in `crates/media/tests/fixtures/README.md` | MIT OR Apache-2.0 |
| `crates/media/tests/fixtures/monochrome-av1.mkv` | Terra854 | Original grayscale ramps from `crates/media/tests/fixtures/generate-monochrome-av1.sh` | MIT OR Apache-2.0 |
+32 -1
View File
@@ -2293,7 +2293,35 @@ fn to_craft_frame(vf: DecodedVideoFrame, presentation: FramePresentation, pts: T
PixelData::Yuv16 { planes: [y, cb, cr], chroma, bits: u32::from(bit_depth), alpha: a }
}
}
ColorFamily::Rgb | ColorFamily::Gray => return Err(MediaError::Unsupported("non-YCbCr pixel format".into())),
ColorFamily::Gray => {
let y = planes.into_iter().next().ok_or_else(|| MediaError::Decode("missing grayscale plane".into()))?;
let luma = aperture.and_then(|clap| clean_aperture_region(clap, (width, height)));
if let Some(r) = luma {
(width, height) = (r.w as u32, r.h as u32);
}
// Chroma is constant, so subsampling does not change the visible grayscale picture.
let chroma = Chroma::C420;
let chroma_len = (width as usize)
.div_ceil(2)
.checked_mul((height as usize).div_ceil(2))
.ok_or_else(|| MediaError::Decode("monochrome chroma size overflows".into()))?;
match bit_depth {
8 => {
let y = Arc::new(plane_u8(y, luma)?);
let alpha = alpha.map(|p| plane_u8(p, luma)).transpose()?.map(Arc::new);
let neutral = Arc::new(vec![128; chroma_len]);
PixelData::Yuv8 { planes: [y, neutral.clone(), neutral], chroma, alpha }
}
9..=16 => {
let y = Arc::new(plane_u16(y, luma)?);
let alpha = alpha.map(|p| plane_u16(p, luma)).transpose()?.map(Arc::new);
let neutral = Arc::new(vec![1u16 << (bit_depth - 1); chroma_len]);
PixelData::Yuv16 { planes: [y, neutral.clone(), neutral], chroma, bits: u32::from(bit_depth), alpha }
}
_ => return Err(MediaError::Unsupported("monochrome bit depth below 8".into())),
}
}
ColorFamily::Rgb => return Err(MediaError::Unsupported("non-YCbCr pixel format".into())),
};
let frame = VideoFrame { width, height, data, color, par, pts };
@@ -4793,3 +4821,6 @@ mod tests {
assert_eq!(read_count, 0);
}
}
#[cfg(test)]
mod monochrome_tests;
@@ -0,0 +1,49 @@
use super::*;
use crate::stream::{self, MediaSource as OpenedSource};
#[test]
fn shared_monochrome_av1_retains_luma_on_backward_seek() -> std::result::Result<(), Box<dyn std::error::Error>> {
let bytes: Arc<[u8]> = Arc::from(include_bytes!("../../tests/fixtures/monochrome-av1.mkv").as_slice());
let OpenedSource::Craft(source) = stream::open_bytes("monochrome-av1.mkv", bytes)? else {
panic!("expected the shared AV1 reader");
};
for i in [2u8, 0, 1] {
let pts = Tick::from_units(i64::from(i), 25);
let frame = source.video_frame(FrameRequest { time: pts, scale: 1.0 })?;
assert_eq!((frame.width, frame.height, frame.pts, frame.color.range), (16, 12, pts, Range::Full));
let PixelData::Yuv8 { planes: [y, cb, cr], chroma: Chroma::C420, alpha: None } = &frame.data else {
panic!("expected 8-bit luma with neutral chroma");
};
let expected: Vec<u8> = (0..12).flat_map(|row| (0..16).map(move |col| 16 + 64 * i + col + 2 * row)).collect();
assert_eq!(y.as_slice(), expected);
assert_eq!(cb.as_slice(), [128; 48]);
assert_eq!(cr.as_slice(), cb.as_slice());
let rgba = frame.to_rgba8()?;
for (pixel, &sample) in rgba.as_chunks::<4>().0.iter().zip(&expected) {
assert_eq!(*pixel, [sample, sample, sample, 255]);
}
}
Ok(())
}
#[test]
fn monochrome_10_bit_preserves_aperture_rotation_alpha_and_layout() -> std::result::Result<(), Box<dyn std::error::Error>> {
let y = Plane::packed(5, 5, 10, Samples::U16((64..89).collect()))?;
let alpha = Plane::packed(5, 5, 10, Samples::U16((900..925).collect()))?;
let mut decoded = DecodedVideoFrame::new(5, 5, ColorFamily::Gray, Subsampling::S444, vec![y], Some(alpha))?;
decoded.sar = Some((3, 2));
let aperture = isobmff::CleanAperture { width: (3, 1), height: (3, 1), horiz_offset: (0, 1), vert_offset: (0, 1) };
let presentation = FramePresentation { container_color: Some(ColorInfo::REC709), par: (1, 1), rotation: 1, aperture: Some(aperture) };
let pts = Tick::from_units(4, 25);
let frame = to_craft_frame(decoded, presentation, pts)?;
assert_eq!((frame.width, frame.height, frame.par, frame.pts), (3, 3, (2, 3), pts));
assert_eq!(frame.color, ColorInfo::REC709);
let PixelData::Yuv16 { planes: [y, cb, cr], chroma: Chroma::C420, bits: 10, alpha: Some(alpha) } = &frame.data else {
panic!("expected 10-bit luma and alpha");
};
assert_eq!(y.as_slice(), [80, 75, 70, 81, 76, 71, 82, 77, 72]);
assert_eq!(alpha.as_slice(), [916, 911, 906, 917, 912, 907, 918, 913, 908]);
assert_eq!(cb.as_slice(), [512; 4]);
assert_eq!(cr.as_slice(), cb.as_slice());
Ok(())
}
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env sh
set -eu
cd "$(dirname "$0")"
ffmpeg -hide_banner -loglevel error \
-f lavfi -i "nullsrc=s=16x12:r=25,format=gray,geq=lum='16+64*N+X+2*Y'" \
-frames:v 3 -c:v libaom-av1 -pix_fmt gray -color_range pc \
-color_primaries bt709 -color_trc bt709 -colorspace bt709 \
-g 1 -crf 0 -b:v 0 -aom-params lossless=1 -cpu-used 6 -threads 16 \
-fflags +bitexact -flags:v +bitexact -f matroska -y monochrome-av1.mkv
Binary file not shown.
@@ -0,0 +1,7 @@
asset: monochrome-av1.mkv
title: Synthetic monochrome AV1 seek fixture
author: Terra854
source: crates/media/tests/fixtures/generate-monochrome-av1.sh
license: MIT OR Apache-2.0
added: 2026-11-22
notes: Original 16x12 grayscale ramps generated with FFmpeg 7.1 and libaom-av1. SHA-256 0837f2039cad2324e7c7e3d4d11bbcf09b02bc9ee6acf84814c5a06e514c52d5.