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(
1415 }1415 }
1416 }1416 }
1417 1417
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+
1418 pub(crate) fn apply_grain(1434 pub(crate) fn apply_grain(
1419 picture: &Picture,1435 picture: &Picture,
1420 fg: &FilmGrainParams,1436 fg: &FilmGrainParams,
crates/ec-av1/src/lib.rsmodified+1 -0
@@ -34,6 +34,7 @@ pub mod encoder;
34 pub mod fault;34 pub mod fault;
35 mod envflags;35 mod envflags;
36 mod film_grain;36 mod film_grain;
37+pub use film_grain::apply_film_grain;
37 mod filter_search;38 mod filter_search;
38 pub mod frame;39 pub mod frame;
39 // Test-only: the real-library fixture root and the one presence probe every40 // 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
7 authors.workspace = true7 authors.workspace = true
8 8
9 [dependencies]9 [dependencies]
10+ec-av1.workspace = true
10 ec-av1-syntax.workspace = true11 ec-av1-syntax.workspace = true
11 ec-core.workspace = true12 ec-core.workspace = true
12 ec-dsp.workspace = true13 ec-dsp.workspace = true
@@ -17,7 +18,3 @@ ec-vp9-syntax.workspace = true
17 18
18 [target.'cfg(target_os = "linux")'.dependencies]19 [target.'cfg(target_os = "linux")'.dependencies]
19 ec-va.workspace = true20 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() {
105 run(&dir, case);105 run(&dir, case);
106 }106 }
107 ten_bit(&dir);107 ten_bit(&dir);
108+ grain(&dir);
108 own_av1(&dir);109 own_av1(&dir);
109 }110 }
110 111
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+
111 fn ten_bit(dir: &Path) {139 fn ten_bit(dir: &Path) {
112 let hevc = dir.join("ten.hevc");140 let hevc = dir.join("ten.hevc");
113 let ivf = dir.join("ten_av1.ivf");141 let ivf = dir.join("ten_av1.ivf");
crates/ec-hw/src/vkdec_av1.rsmodified+67 -8
@@ -3,10 +3,11 @@
3 use std::collections::VecDeque;3 use std::collections::VecDeque;
4 use std::rc::Rc;4 use std::rc::Rc;
5 5
6+use ec_av1::encode::Picture;
6 use ec_av1_syntax::{7 use ec_av1_syntax::{
7- Av1Parser, ChromaSamplePosition, ColorConfig, FrameHeader, FrameType, InterpolationFilter,8+ Av1Parser, ChromaSamplePosition, ColorConfig, FilmGrainParams, FrameHeader, FrameType,
8- NUM_REF_FRAMES, ObuKind, REFS_PER_FRAME, RestorationType, SequenceHeader, Tile, TxMode,9+ InterpolationFilter, NUM_REF_FRAMES, ObuKind, REFS_PER_FRAME, RestorationType, SequenceHeader,
9- WarpModel,10+ Tile, TxMode, WarpModel,
10 };11 };
11 use ec_vk::{12 use ec_vk::{
12 Av1Cdef, Av1ColorConfig, Av1DecodeConfig, Av1DecodePicture, Av1DecodeSession, Av1DeltaParams,13 Av1Cdef, Av1ColorConfig, Av1DecodeConfig, Av1DecodePicture, Av1DecodeSession, Av1DeltaParams,
@@ -20,7 +21,7 @@ use crate::error::{Error, Result};
20 use ec_core::color::{ColorDescription, Tags};21 use ec_core::color::{ColorDescription, Tags};
21 22
22 use crate::hw::{RgbOrder, RgbOutput};23 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};
24 25
25 const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1;26 const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1;
26 27
@@ -104,7 +105,17 @@ impl VkAv1Decoder {
104 "AV1 show_existing_frame names an empty reference slot",105 "AV1 show_existing_frame names an empty reference slot",
105 ))106 ))
106 })?;107 })?;
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));
108 if header.frame_type == FrameType::Key {119 if header.frame_type == FrameType::Key {
109 self.refs.refresh(u8::MAX, Some(reference));120 self.refs.refresh(u8::MAX, Some(reference));
110 }121 }
@@ -123,7 +134,7 @@ impl VkAv1Decoder {
123 .sequence_header()134 .sequence_header()
124 .cloned()135 .cloned()
125 .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?;136 .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())?;
127 let (width, height) = (header.upscaled_width, header.frame_height);138 let (width, height) = (header.upscaled_width, header.frame_height);
128 self.ensure_session(&seq, width, height)?;139 self.ensure_session(&seq, width, height)?;
129 140
@@ -201,7 +212,10 @@ impl VkAv1Decoder {
201 segmentation_enabled: header.segmentation.enabled,212 segmentation_enabled: header.segmentation.enabled,
202 };213 };
203 if header.show_frame {214 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+ ));
205 }219 }
206 if header.refresh_frame_flags != 0 {220 if header.refresh_frame_flags != 0 {
207 let reference = Rc::new(Reference {221 let reference = Rc::new(Reference {
@@ -259,7 +273,7 @@ impl VkAv1Decoder {
259 }273 }
260 }274 }
261 275
262-fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> {276+fn check_supported(seq: &SequenceHeader, header: &FrameHeader, rgb: bool) -> Result<()> {
263 let c = &seq.color_config;277 let c = &seq.color_config;
264 if seq.seq_profile != 0278 if seq.seq_profile != 0
265 || !matches!(c.bit_depth, 8 | 10)279 || !matches!(c.bit_depth, 8 | 10)
@@ -278,9 +292,54 @@ fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> {
278 if header.upscaled_width % 2 != 0 || header.frame_height % 2 != 0 {292 if header.upscaled_width % 2 != 0 || header.frame_height % 2 != 0 {
279 return Err(Error::unsupported("Vulkan AV1 decode", "odd frame size"));293 return Err(Error::unsupported("Vulkan AV1 decode", "odd frame size"));
280 }294 }
295+ if header.film_grain.apply_grain {
296+ grain_supported(seq, rgb)?;
297+ }
281 Ok(())298 Ok(())
282 }299 }
283 300
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+
284 fn av1_frame_type(t: FrameType) -> Av1FrameType {343 fn av1_frame_type(t: FrameType) -> Av1FrameType {
285 match t {344 match t {
286 FrameType::Key => Av1FrameType::Key,345 FrameType::Key => Av1FrameType::Key,