forge
Explore
Log in

Don't you dare go Hollow

ec-hw: AV1 film grain on the Vulkan path

Browse files at this commit

b0a8e4eb4e598221674fbb1457c8a116f1dbb8c5Ahmet Kaan GümüşParent: abe484f
RADV accepts filmGrainSupport=true but its output is the ungrained
decode, so grain is applied on the CPU after readback with ec-av1's
spec 7.18.3 synthesis. Shown frames are grained, references stay clean,
show_existing_frame re-applies the loaded parameters. 8-bit I420 only;
RGB and 10-bit grain still fall back to VA.

hw_decode gains a grain case (ffmpeg and VA: 0.000000).
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,9 +105,37 @@ fn main() {
105105 run(&dir, case);
106106 }
107107 ten_bit(&dir);
108+ grain(&dir);
108109 own_av1(&dir);
109110 }
110111
112+fn grain(dir: &Path) {
113+ const FRAMES: usize = 90;
114+ let stream = dir.join("grain.ivf");
115+ let reference = dir.join("grain.yuv");
116+ let stream_arg = stream.to_str().expect("utf-8 path");
117+ let reference_arg = reference.to_str().expect("utf-8 path");
118+ 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]);
119+ ffmpeg(&["-i", stream_arg, "-f", "rawvideo", "-pix_fmt", "yuv420p", reference_arg]);
120+ let data = fs::read(&stream).expect("read grain stream");
121+ let units = ivf_units(&data);
122+ let mut decoder = HwDecoder::open(Codec::Av1).expect("open decoder");
123+ let backend = decoder.backend();
124+ assert_eq!(backend, Backend::Vk, "grain must decode on Vulkan");
125+ let mut ours: Vec<I420> = Vec::new();
126+ for (pts, unit) in units.iter().enumerate() {
127+ decoder.decode(unit, pts as i64).expect("decode grain");
128+ drain(&mut decoder, &mut ours);
129+ }
130+ decoder.flush().expect("flush");
131+ drain(&mut decoder, &mut ours);
132+ assert_eq!(ours.len(), FRAMES, "grain: frame count");
133+ let raw = fs::read(&reference).expect("read reference");
134+ compare("grain", Codec::Av1, backend, (640, 360), &ours, &raw);
135+ let theirs = va_frames(Codec::Av1, &units);
136+ compare_i420("grain vs VA", Codec::Av1, backend, &ours, &theirs);
137+}
138+
111139 fn ten_bit(dir: &Path) {
112140 let hevc = dir.join("ten.hevc");
113141 let ivf = dir.join("ten_av1.ivf");
crates/ec-hw/src/vkdec_av1.rsmodified+67 -8
@@ -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,
@@ -20,7 +21,7 @@ use crate::error::{Error, Result};
2021 use ec_core::color::{ColorDescription, Tags};
2122
2223 use crate::hw::{RgbOrder, RgbOutput};
23-use crate::picture::{Decoded, RgbPicture, rgb_size, semi_planar_to_i420, yuv_to_rgb};
24+use crate::picture::{Decoded, I420, RgbPicture, rgb_size, semi_planar_to_i420, yuv_to_rgb};
2425
2526 const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1;
2627
@@ -104,7 +105,17 @@ impl VkAv1Decoder {
104105 "AV1 show_existing_frame names an empty reference slot",
105106 ))
106107 })?;
107- self.ready.push_back((timestamp, reference.picture.clone()));
108+ let shown = if header.film_grain.apply_grain {
109+ let seq = self
110+ .parser
111+ .sequence_header()
112+ .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?;
113+ grain_supported(seq, self.rgb.is_some())?;
114+ grained(reference.picture.clone(), &header.film_grain, seq)
115+ } else {
116+ reference.picture.clone()
117+ };
118+ self.ready.push_back((timestamp, shown));
108119 if header.frame_type == FrameType::Key {
109120 self.refs.refresh(u8::MAX, Some(reference));
110121 }
@@ -123,7 +134,7 @@ impl VkAv1Decoder {
123134 .sequence_header()
124135 .cloned()
125136 .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?;
126- check_supported(&seq, header)?;
137+ check_supported(&seq, header, self.rgb.is_some())?;
127138 let (width, height) = (header.upscaled_width, header.frame_height);
128139 self.ensure_session(&seq, width, height)?;
129140
@@ -201,7 +212,10 @@ impl VkAv1Decoder {
201212 segmentation_enabled: header.segmentation.enabled,
202213 };
203214 if header.show_frame {
204- self.ready.push_back((timestamp, decoded.clone()));
215+ self.ready.push_back((
216+ timestamp,
217+ grained(decoded.clone(), &header.film_grain, &seq),
218+ ));
205219 }
206220 if header.refresh_frame_flags != 0 {
207221 let reference = Rc::new(Reference {
@@ -259,7 +273,7 @@ impl VkAv1Decoder {
259273 }
260274 }
261275
262-fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> {
276+fn check_supported(seq: &SequenceHeader, header: &FrameHeader, rgb: bool) -> Result<()> {
263277 let c = &seq.color_config;
264278 if seq.seq_profile != 0
265279 || !matches!(c.bit_depth, 8 | 10)
@@ -278,9 +292,54 @@ fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> {
278292 if header.upscaled_width % 2 != 0 || header.frame_height % 2 != 0 {
279293 return Err(Error::unsupported("Vulkan AV1 decode", "odd frame size"));
280294 }
295+ if header.film_grain.apply_grain {
296+ grain_supported(seq, rgb)?;
297+ }
281298 Ok(())
282299 }
283300
301+fn grain_supported(seq: &SequenceHeader, rgb: bool) -> Result<()> {
302+ if rgb || seq.color_config.bit_depth != 8 {
303+ return Err(Error::unsupported(
304+ "Vulkan AV1 film grain",
305+ "only 8-bit I420 output",
306+ ));
307+ }
308+ Ok(())
309+}
310+
311+fn grained(picture: Decoded, grain: &FilmGrainParams, seq: &SequenceHeader) -> Decoded {
312+ match picture {
313+ Decoded::I420(frame) if grain.apply_grain => Decoded::I420(synthesize(frame, grain, seq)),
314+ other => other,
315+ }
316+}
317+
318+fn synthesize(frame: I420, grain: &FilmGrainParams, seq: &SequenceHeader) -> I420 {
319+ let wide = |p: &[u8]| p.iter().map(|&s| u16::from(s)).collect::<Vec<u16>>();
320+ let clean = Picture {
321+ width: frame.width as usize,
322+ height: frame.height as usize,
323+ y: wide(&frame.y),
324+ u: wide(&frame.u),
325+ v: wide(&frame.v),
326+ };
327+ let out = ec_av1::apply_film_grain(
328+ &clean,
329+ grain,
330+ seq.color_config.matrix_coefficients == 0,
331+ u32::from(seq.color_config.bit_depth),
332+ );
333+ let narrow = |p: &[u16]| p.iter().map(|&s| s as u8).collect::<Vec<u8>>();
334+ I420 {
335+ y: narrow(&out.y),
336+ u: narrow(&out.u),
337+ v: narrow(&out.v),
338+ width: frame.width,
339+ height: frame.height,
340+ }
341+}
342+
284343 fn av1_frame_type(t: FrameType) -> Av1FrameType {
285344 match t {
286345 FrameType::Key => Av1FrameType::Key,