forge
Explore
Log in

Don't you dare go Hollow

Merge AV1 film grain on Vk (CPU synthesis via ec-av1 after clean GPU decode; shim told no grain), bit-exact vs ffmpeg and VA

Browse files at this commit

a9418fa0e79adec79f99ce09772f81be2769f702Ahmet Kaan GümüşParent: 59d3a02b0a8e4e
crates/ec-av1/src/film_grain.rsmodified+16 -0
@@ -1415,6 +1415,22 @@ fn assemble_block_noise(
14151415 }
14161416 }
14171417
1418+/// Film grain synthesis (spec 7.18.3) of a clean picture, for decoders that apply grain on the CPU.
1419+pub fn apply_film_grain(
1420+ picture: &Picture,
1421+ fg: &FilmGrainParams,
1422+ mc_identity: bool,
1423+ bit_depth: u32,
1424+) -> Picture {
1425+ apply_grain(
1426+ picture,
1427+ fg,
1428+ mc_identity,
1429+ bit_depth,
1430+ &crate::decode::FrameCtx::new(),
1431+ )
1432+}
1433+
14181434 pub(crate) fn apply_grain(
14191435 picture: &Picture,
14201436 fg: &FilmGrainParams,
crates/ec-av1/src/lib.rsmodified+1 -0
@@ -34,6 +34,7 @@ pub mod encoder;
3434 pub mod fault;
3535 mod envflags;
3636 mod film_grain;
37+pub use film_grain::apply_film_grain;
3738 mod filter_search;
3839 pub mod frame;
3940 // Test-only: the real-library fixture root and the one presence probe every
crates/ec-hw/Cargo.tomlmodified+1 -4
@@ -7,6 +7,7 @@ license.workspace = true
77 authors.workspace = true
88
99 [dependencies]
10+ec-av1.workspace = true
1011 ec-av1-syntax.workspace = true
1112 ec-core.workspace = true
1213 ec-dsp.workspace = true
@@ -17,7 +18,3 @@ ec-vp9-syntax.workspace = true
1718
1819 [target.'cfg(target_os = "linux")'.dependencies]
1920 ec-va.workspace = true
20-
21-[dev-dependencies]
22-# The second, independent decoder the AV1 encode gate compares ffmpeg against.
23-ec-av1.workspace = true
crates/ec-hw/examples/hw_decode.rsmodified+28 -0
@@ -105,6 +105,7 @@ fn main() {
105105 run(&dir, case);
106106 }
107107 ten_bit(&dir);
108+ grain(&dir);
108109 own_av1(&dir);
109110 vp9_cases(&dir);
110111 }
@@ -159,6 +160,33 @@ fn vp9_case(dir: &Path, name: &str, size: &str, frames: usize, encoder: &[&str],
159160 compare(name, Codec::Vp9, backend, dims(size), &decoded, &raw);
160161 }
161162
163+fn grain(dir: &Path) {
164+ const FRAMES: usize = 90;
165+ let stream = dir.join("grain.ivf");
166+ let reference = dir.join("grain.yuv");
167+ let stream_arg = stream.to_str().expect("utf-8 path");
168+ let reference_arg = reference.to_str().expect("utf-8 path");
169+ ffmpeg(&["-f", "lavfi", "-i", "testsrc2=size=640x360:rate=30", "-frames:v", "90", "-c:v", "libsvtav1", "-g", "30", "-svtav1-params", "film-grain=20", "-f", "ivf", stream_arg]);
170+ ffmpeg(&["-i", stream_arg, "-f", "rawvideo", "-pix_fmt", "yuv420p", reference_arg]);
171+ let data = fs::read(&stream).expect("read grain stream");
172+ let units = ivf_units(&data);
173+ let mut decoder = HwDecoder::open(Codec::Av1).expect("open decoder");
174+ let backend = decoder.backend();
175+ assert_eq!(backend, Backend::Vk, "grain must decode on Vulkan");
176+ let mut ours: Vec<I420> = Vec::new();
177+ for (pts, unit) in units.iter().enumerate() {
178+ decoder.decode(unit, pts as i64).expect("decode grain");
179+ drain(&mut decoder, &mut ours);
180+ }
181+ decoder.flush().expect("flush");
182+ drain(&mut decoder, &mut ours);
183+ assert_eq!(ours.len(), FRAMES, "grain: frame count");
184+ let raw = fs::read(&reference).expect("read reference");
185+ compare("grain", Codec::Av1, backend, (640, 360), &ours, &raw);
186+ let theirs = va_frames(Codec::Av1, &units);
187+ compare_i420("grain vs VA", Codec::Av1, backend, &ours, &theirs);
188+}
189+
162190 fn ten_bit(dir: &Path) {
163191 let hevc = dir.join("ten.hevc");
164192 let ivf = dir.join("ten_av1.ivf");
crates/ec-hw/src/vkdec_av1.rsmodified+68 -12
@@ -3,10 +3,11 @@
33 use std::collections::VecDeque;
44 use std::rc::Rc;
55
6+use ec_av1::encode::Picture;
67 use ec_av1_syntax::{
7- Av1Parser, ChromaSamplePosition, ColorConfig, FrameHeader, FrameType, InterpolationFilter,
8- NUM_REF_FRAMES, ObuKind, REFS_PER_FRAME, RestorationType, SequenceHeader, Tile, TxMode,
9- WarpModel,
8+ Av1Parser, ChromaSamplePosition, ColorConfig, FilmGrainParams, FrameHeader, FrameType,
9+ InterpolationFilter, NUM_REF_FRAMES, ObuKind, REFS_PER_FRAME, RestorationType, SequenceHeader,
10+ Tile, TxMode, WarpModel,
1011 };
1112 use ec_vk::{
1213 Av1Cdef, Av1ColorConfig, Av1DecodeConfig, Av1DecodePicture, Av1DecodeSession, Av1DeltaParams,
@@ -21,7 +22,7 @@ use ec_core::color::{ColorDescription, Tags};
2122
2223 use crate::hw::{Mode, RgbOrder};
2324 use crate::picture::{
24- Decoded, RgbPicture, rgb_size, semi_planar_to_i420, semi_planar_to_i420_16,
25+ Decoded, I420, RgbPicture, rgb_size, semi_planar_to_i420, semi_planar_to_i420_16,
2526 yuv_to_rgb,
2627 };
2728
@@ -107,7 +108,17 @@ impl VkAv1Decoder {
107108 "AV1 show_existing_frame names an empty reference slot",
108109 ))
109110 })?;
110- self.ready.push_back((timestamp, reference.picture.clone()));
111+ let shown = if header.film_grain.apply_grain {
112+ let seq = self
113+ .parser
114+ .sequence_header()
115+ .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?;
116+ grain_supported(seq, !matches!(self.mode, Mode::I420))?;
117+ grained(reference.picture.clone(), &header.film_grain, seq)
118+ } else {
119+ reference.picture.clone()
120+ };
121+ self.ready.push_back((timestamp, shown));
111122 if header.frame_type == FrameType::Key {
112123 self.refs.refresh(u8::MAX, Some(reference));
113124 }
@@ -126,7 +137,7 @@ impl VkAv1Decoder {
126137 .sequence_header()
127138 .cloned()
128139 .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?;
129- check_supported(&seq, header)?;
140+ check_supported(&seq, header, !matches!(self.mode, Mode::I420))?;
130141 let (width, height) = (header.upscaled_width, header.frame_height);
131142 self.ensure_session(&seq, width, height)?;
132143
@@ -215,7 +226,10 @@ impl VkAv1Decoder {
215226 segmentation_enabled: header.segmentation.enabled,
216227 };
217228 if header.show_frame {
218- self.ready.push_back((timestamp, decoded.clone()));
229+ self.ready.push_back((
230+ timestamp,
231+ grained(decoded.clone(), &header.film_grain, &seq),
232+ ));
219233 }
220234 if header.refresh_frame_flags != 0 {
221235 let reference = Rc::new(Reference {
@@ -273,7 +287,7 @@ impl VkAv1Decoder {
273287 }
274288 }
275289
276-fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> {
290+fn check_supported(seq: &SequenceHeader, header: &FrameHeader, rgb: bool) -> Result<()> {
277291 let c = &seq.color_config;
278292 if seq.seq_profile != 0
279293 || !matches!(c.bit_depth, 8 | 10)
@@ -286,15 +300,57 @@ fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> {
286300 "only Main 8- and 10-bit 4:2:0",
287301 ));
288302 }
289- if header.film_grain.apply_grain {
290- return Err(Error::unsupported("Vulkan AV1 decode", "film grain"));
291- }
292303 if header.upscaled_width % 2 != 0 || header.frame_height % 2 != 0 {
293304 return Err(Error::unsupported("Vulkan AV1 decode", "odd frame size"));
294305 }
306+ if header.film_grain.apply_grain {
307+ grain_supported(seq, rgb)?;
308+ }
295309 Ok(())
296310 }
297311
312+fn grain_supported(seq: &SequenceHeader, rgb: bool) -> Result<()> {
313+ if rgb || seq.color_config.bit_depth != 8 {
314+ return Err(Error::unsupported(
315+ "Vulkan AV1 film grain",
316+ "only 8-bit I420 output",
317+ ));
318+ }
319+ Ok(())
320+}
321+
322+fn grained(picture: Decoded, grain: &FilmGrainParams, seq: &SequenceHeader) -> Decoded {
323+ match picture {
324+ Decoded::I420(frame) if grain.apply_grain => Decoded::I420(synthesize(frame, grain, seq)),
325+ other => other,
326+ }
327+}
328+
329+fn synthesize(frame: I420, grain: &FilmGrainParams, seq: &SequenceHeader) -> I420 {
330+ let wide = |p: &[u8]| p.iter().map(|&s| u16::from(s)).collect::<Vec<u16>>();
331+ let clean = Picture {
332+ width: frame.width as usize,
333+ height: frame.height as usize,
334+ y: wide(&frame.y),
335+ u: wide(&frame.u),
336+ v: wide(&frame.v),
337+ };
338+ let out = ec_av1::apply_film_grain(
339+ &clean,
340+ grain,
341+ seq.color_config.matrix_coefficients == 0,
342+ u32::from(seq.color_config.bit_depth),
343+ );
344+ let narrow = |p: &[u16]| p.iter().map(|&s| s as u8).collect::<Vec<u8>>();
345+ I420 {
346+ y: narrow(&out.y),
347+ u: narrow(&out.u),
348+ v: narrow(&out.v),
349+ width: frame.width,
350+ height: frame.height,
351+ }
352+}
353+
298354 fn av1_frame_type(t: FrameType) -> Av1FrameType {
299355 match t {
300356 FrameType::Key => Av1FrameType::Key,
@@ -523,6 +579,6 @@ fn frame_header(h: &FrameHeader, s: &SequenceHeader) -> Av1FrameHeader {
523579 order_hints,
524580 skip_mode_frame: h.skip_mode_frame,
525581 expected_frame_id: [0; 8],
526- apply_grain: h.film_grain.apply_grain,
582+ apply_grain: false,
527583 }
528584 }