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 commit5 changed files +114 -16
- crates/ec-av1/src/film_grain.rs +16 -0
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-hw/Cargo.toml +1 -4
- crates/ec-hw/examples/hw_decode.rs +28 -0
- crates/ec-hw/src/vkdec_av1.rs +68 -12
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 every | 40 | // 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 = true | 7 | authors.workspace = true |
| 8 | 8 | ||
| 9 | [dependencies] | 9 | [dependencies] |
| 10 | +ec-av1.workspace = true | ||
| 10 | ec-av1-syntax.workspace = true | 11 | ec-av1-syntax.workspace = true |
| 11 | ec-core.workspace = true | 12 | ec-core.workspace = true |
| 12 | ec-dsp.workspace = true | 13 | 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 = true | 20 | 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() { | |||
| 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 | vp9_cases(&dir); | 110 | vp9_cases(&dir); |
| 110 | } | 111 | } |
| @@ -159,6 +160,33 @@ fn vp9_case(dir: &Path, name: &str, size: &str, frames: usize, encoder: &[&str], | |||
| 159 | compare(name, Codec::Vp9, backend, dims(size), &decoded, &raw); | 160 | compare(name, Codec::Vp9, backend, dims(size), &decoded, &raw); |
| 160 | } | 161 | } |
| 161 | 162 | ||
| 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 | + | ||
| 162 | fn ten_bit(dir: &Path) { | 190 | fn ten_bit(dir: &Path) { |
| 163 | let hevc = dir.join("ten.hevc"); | 191 | let hevc = dir.join("ten.hevc"); |
| 164 | let ivf = dir.join("ten_av1.ivf"); | 192 | let ivf = dir.join("ten_av1.ivf"); |
crates/ec-hw/src/vkdec_av1.rsmodified+68 -12
| @@ -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, |
| @@ -21,7 +22,7 @@ use ec_core::color::{ColorDescription, Tags}; | |||
| 21 | 22 | ||
| 22 | use crate::hw::{Mode, RgbOrder}; | 23 | use crate::hw::{Mode, RgbOrder}; |
| 23 | use crate::picture::{ | 24 | 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, |
| 25 | yuv_to_rgb, | 26 | yuv_to_rgb, |
| 26 | }; | 27 | }; |
| 27 | 28 | ||
| @@ -107,7 +108,17 @@ impl VkAv1Decoder { | |||
| 107 | "AV1 show_existing_frame names an empty reference slot", | 108 | "AV1 show_existing_frame names an empty reference slot", |
| 108 | )) | 109 | )) |
| 109 | })?; | 110 | })?; |
| 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)); | ||
| 111 | if header.frame_type == FrameType::Key { | 122 | if header.frame_type == FrameType::Key { |
| 112 | self.refs.refresh(u8::MAX, Some(reference)); | 123 | self.refs.refresh(u8::MAX, Some(reference)); |
| 113 | } | 124 | } |
| @@ -126,7 +137,7 @@ impl VkAv1Decoder { | |||
| 126 | .sequence_header() | 137 | .sequence_header() |
| 127 | .cloned() | 138 | .cloned() |
| 128 | .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?; | 139 | .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))?; |
| 130 | let (width, height) = (header.upscaled_width, header.frame_height); | 141 | let (width, height) = (header.upscaled_width, header.frame_height); |
| 131 | self.ensure_session(&seq, width, height)?; | 142 | self.ensure_session(&seq, width, height)?; |
| 132 | 143 | ||
| @@ -215,7 +226,10 @@ impl VkAv1Decoder { | |||
| 215 | segmentation_enabled: header.segmentation.enabled, | 226 | segmentation_enabled: header.segmentation.enabled, |
| 216 | }; | 227 | }; |
| 217 | if header.show_frame { | 228 | 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 | + )); | ||
| 219 | } | 233 | } |
| 220 | if header.refresh_frame_flags != 0 { | 234 | if header.refresh_frame_flags != 0 { |
| 221 | let reference = Rc::new(Reference { | 235 | let reference = Rc::new(Reference { |
| @@ -273,7 +287,7 @@ impl VkAv1Decoder { | |||
| 273 | } | 287 | } |
| 274 | } | 288 | } |
| 275 | 289 | ||
| 276 | -fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> { | 290 | +fn check_supported(seq: &SequenceHeader, header: &FrameHeader, rgb: bool) -> Result<()> { |
| 277 | let c = &seq.color_config; | 291 | let c = &seq.color_config; |
| 278 | if seq.seq_profile != 0 | 292 | if seq.seq_profile != 0 |
| 279 | || !matches!(c.bit_depth, 8 | 10) | 293 | || !matches!(c.bit_depth, 8 | 10) |
| @@ -286,15 +300,57 @@ fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> { | |||
| 286 | "only Main 8- and 10-bit 4:2:0", | 300 | "only Main 8- and 10-bit 4:2:0", |
| 287 | )); | 301 | )); |
| 288 | } | 302 | } |
| 289 | - if header.film_grain.apply_grain { | ||
| 290 | - return Err(Error::unsupported("Vulkan AV1 decode", "film grain")); | ||
| 291 | - } | ||
| 292 | if header.upscaled_width % 2 != 0 || header.frame_height % 2 != 0 { | 303 | if header.upscaled_width % 2 != 0 || header.frame_height % 2 != 0 { |
| 293 | return Err(Error::unsupported("Vulkan AV1 decode", "odd frame size")); | 304 | return Err(Error::unsupported("Vulkan AV1 decode", "odd frame size")); |
| 294 | } | 305 | } |
| 306 | + if header.film_grain.apply_grain { | ||
| 307 | + grain_supported(seq, rgb)?; | ||
| 308 | + } | ||
| 295 | Ok(()) | 309 | Ok(()) |
| 296 | } | 310 | } |
| 297 | 311 | ||
| 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 | + | ||
| 298 | fn av1_frame_type(t: FrameType) -> Av1FrameType { | 354 | fn av1_frame_type(t: FrameType) -> Av1FrameType { |
| 299 | match t { | 355 | match t { |
| 300 | FrameType::Key => Av1FrameType::Key, | 356 | FrameType::Key => Av1FrameType::Key, |
| @@ -523,6 +579,6 @@ fn frame_header(h: &FrameHeader, s: &SequenceHeader) -> Av1FrameHeader { | |||
| 523 | order_hints, | 579 | order_hints, |
| 524 | skip_mode_frame: h.skip_mode_frame, | 580 | skip_mode_frame: h.skip_mode_frame, |
| 525 | expected_frame_id: [0; 8], | 581 | expected_frame_id: [0; 8], |
| 526 | - apply_grain: h.film_grain.apply_grain, | 582 | + apply_grain: false, |
| 527 | } | 583 | } |
| 528 | } | 584 | } |