vp9: decode odd coded extents (chroma ceils at (w+1)/2)
Browse files at this commit73605cd992afe32efe3c2cfcdd8bd931e47dc1e6Parent: bdf08742 changed files +192 -21
crates/ec-vp9/src/decode.rsmodified+21 -21
| @@ -6,10 +6,11 @@ | |||
| 6 | //! | 6 | //! |
| 7 | //! Capability: profile-0 8-bit 4:2:0 keyframes and inter frames (motion | 7 | //! Capability: profile-0 8-bit 4:2:0 keyframes and inter frames (motion |
| 8 | //! compensation, residual reconstruction, the loop filter and an 8-slot | 8 | //! compensation, residual reconstruction, the loop filter and an 8-slot |
| 9 | -//! reference DPB). Intra-only frames, other profiles/subsamplings, a | 9 | +//! reference DPB). Intra-only frames, other profiles/subsamplings and a |
| 10 | -//! reference whose coded size differs from the current frame, and odd frame | 10 | +//! reference whose coded size differs from the current frame are refused by |
| 11 | -//! dimensions are refused by name; `show_existing_frame` returns the | 11 | +//! name; `show_existing_frame` returns the buffered reference picture instead |
| 12 | -//! buffered reference picture instead of pretending to decode. | 12 | +//! of pretending to decode. Odd coded extents are supported: chroma stores |
| 13 | +//! and crops at `(w + 1) / 2` (`uv_crop_width`), matching libvpx. | ||
| 13 | 14 | ||
| 14 | use crate::header::{FrameContext, read_compressed_header}; | 15 | use crate::header::{FrameContext, read_compressed_header}; |
| 15 | use crate::intra::build_intra_predictors; | 16 | use crate::intra::build_intra_predictors; |
| @@ -39,7 +40,8 @@ pub struct Picture { | |||
| 39 | pub height: u16, | 40 | pub height: u16, |
| 40 | /// Luma row pitch of the returned planes (== `width`; the crop packs rows). | 41 | /// Luma row pitch of the returned planes (== `width`; the crop packs rows). |
| 41 | pub stride: usize, | 42 | pub stride: usize, |
| 42 | - /// Chroma row pitch of the returned planes (== `width / 2`). | 43 | + /// Chroma row pitch of the returned planes (`(width + 1) / 2`; chroma |
| 44 | + /// ceils an odd coded width). | ||
| 43 | pub uv_stride: usize, | 45 | pub uv_stride: usize, |
| 44 | } | 46 | } |
| 45 | 47 | ||
| @@ -378,17 +380,6 @@ impl Decoder { | |||
| 378 | )); | 380 | )); |
| 379 | } | 381 | } |
| 380 | } | 382 | } |
| 381 | - if hdr.width % 2 != 0 || hdr.height % 2 != 0 { | ||
| 382 | - return Err(Error::unsupported( | ||
| 383 | - "vp9 inter odd frame dimensions", | ||
| 384 | - format!( | ||
| 385 | - "{}x{}: the reference chroma planes of an odd-sized frame are \ | ||
| 386 | - stored at floor(w/2) but the predictor reads libvpx's \ | ||
| 387 | - uv_crop_width = (w+1)/2", | ||
| 388 | - hdr.width, hdr.height | ||
| 389 | - ), | ||
| 390 | - )); | ||
| 391 | - } | ||
| 392 | let width = hdr.width as usize; | 383 | let width = hdr.width as usize; |
| 393 | let height = hdr.height as usize; | 384 | let height = hdr.height as usize; |
| 394 | let aw = width.div_ceil(64) * 64; | 385 | let aw = width.div_ceil(64) * 64; |
| @@ -1800,14 +1791,17 @@ fn crop_picture(planes: &Planes, width: u16, height: u16) -> Picture { | |||
| 1800 | } | 1791 | } |
| 1801 | out | 1792 | out |
| 1802 | }; | 1793 | }; |
| 1794 | + // Chroma CEILS: the 4:2:0 plane of an odd extent carries one extra | ||
| 1795 | + // half-sample column/row (`uv_crop_width = (w + 1) / 2`). | ||
| 1796 | + let (cw, chh) = ((w + 1) / 2, (h + 1) / 2); | ||
| 1803 | Picture { | 1797 | Picture { |
| 1804 | y: crop(&planes.y, planes.ys, w, h), | 1798 | y: crop(&planes.y, planes.ys, w, h), |
| 1805 | - u: crop(&planes.u, planes.uvs, w / 2, h / 2), | 1799 | + u: crop(&planes.u, planes.uvs, cw, chh), |
| 1806 | - v: crop(&planes.v, planes.uvs, w / 2, h / 2), | 1800 | + v: crop(&planes.v, planes.uvs, cw, chh), |
| 1807 | width, | 1801 | width, |
| 1808 | height, | 1802 | height, |
| 1809 | stride: w, | 1803 | stride: w, |
| 1810 | - uv_stride: w / 2, | 1804 | + uv_stride: cw, |
| 1811 | } | 1805 | } |
| 1812 | } | 1806 | } |
| 1813 | 1807 | ||
| @@ -2004,8 +1998,14 @@ fn inter_predict_plane( | |||
| 2004 | let refp = RefPlane { | 1998 | let refp = RefPlane { |
| 2005 | data: ref_data, | 1999 | data: ref_data, |
| 2006 | stride: ref_stride, | 2000 | stride: ref_stride, |
| 2007 | - width: (ref_frame.width as usize) >> s, | 2001 | + // `y_crop_width` / `uv_crop_width`: luma is the coded size, chroma |
| 2008 | - height: (ref_frame.height as usize) >> s, | 2002 | + // CEILS (`(w + 1) / 2`). libvpx stores `uv_crop_width` as |
| 2003 | + // `(y_crop_width + 1) >> 1` (`vpx_scale/yv12config.c`), so a | ||
| 2004 | + // plain `>> 1` is one short whenever the coded extent is odd and | ||
| 2005 | + // the predictor's border/inside test then clips the last chroma | ||
| 2006 | + // column (or row). | ||
| 2007 | + width: (ref_frame.width as usize + s) >> s, | ||
| 2008 | + height: (ref_frame.height as usize + s) >> s, | ||
| 2009 | }; | 2009 | }; |
| 2010 | if sb_type < 3 { | 2010 | if sb_type < 3 { |
| 2011 | // libvpx builds one 4x4 prediction per sub-block, with the MV from | 2011 | // libvpx builds one 4x4 prediction per sub-block, with the MV from |
crates/ec-vp9/tests/odd_dimensions_exact.rsadded+171 -0
| @@ -0,0 +1,171 @@ | |||
| 1 | +//! Odd coded-extent witness: a stream whose VP9 keyframe signals an odd | ||
| 2 | +//! width and/or height must decode byte-exactly against ffmpeg's libvpx | ||
| 3 | +//! output, chroma plane included. libvpx stores and crops chroma at | ||
| 4 | +//! `uv_crop_width = (w + 1) / 2` / `uv_crop_height = (h + 1) / 2`, i.e. it | ||
| 5 | +//! CEILS an odd coded extent; a `>> 1` layout is one column/row short and | ||
| 6 | +//! clips the last chroma sample. | ||
| 7 | +//! | ||
| 8 | +//! libvpx-vp9's encoder rounds an odd source through its scaler, so the | ||
| 9 | +//! fixture is an even 322x242 encode whose keyframe's uncompressed-header | ||
| 10 | +//! size fields are patched to the odd target. The change is mi-grid | ||
| 11 | +//! invariant for 322<->321 and 242<->241 (both ceil to 41x31 mi units), so | ||
| 12 | +//! the tile data stays valid; every later frame inherits the odd size through | ||
| 13 | +//! `found_ref`. ffmpeg's rawvideo output is the oracle, exactly as in | ||
| 14 | +//! `inter_pixels_exact.rs`. | ||
| 15 | + | ||
| 16 | +mod ivf; | ||
| 17 | + | ||
| 18 | +use ec_vp9::decode::Decoder; | ||
| 19 | +use std::path::Path; | ||
| 20 | + | ||
| 21 | +/// Generate the even 322x242 GOP with libvpx (frame 0 the only keyframe, so | ||
| 22 | +/// the whole stream inherits the patched size). | ||
| 23 | +fn ensure_even_fixture(path: &Path) { | ||
| 24 | + if path.exists() { | ||
| 25 | + return; | ||
| 26 | + } | ||
| 27 | + if let Some(dir) = path.parent() { | ||
| 28 | + std::fs::create_dir_all(dir).unwrap(); | ||
| 29 | + } | ||
| 30 | + let st = std::process::Command::new("ffmpeg") | ||
| 31 | + .args([ | ||
| 32 | + "-v", | ||
| 33 | + "error", | ||
| 34 | + "-y", | ||
| 35 | + "-f", | ||
| 36 | + "lavfi", | ||
| 37 | + "-i", | ||
| 38 | + "testsrc2=size=322x242:rate=24:duration=0.5", | ||
| 39 | + "-c:v", | ||
| 40 | + "libvpx-vp9", | ||
| 41 | + "-g", | ||
| 42 | + "999", | ||
| 43 | + "-auto-alt-ref", | ||
| 44 | + "0", | ||
| 45 | + "-crf", | ||
| 46 | + "30", | ||
| 47 | + "-deadline", | ||
| 48 | + "good", | ||
| 49 | + "-cpu-used", | ||
| 50 | + "4", | ||
| 51 | + "-pix_fmt", | ||
| 52 | + "yuv420p", | ||
| 53 | + "-f", | ||
| 54 | + "ivf", | ||
| 55 | + ]) | ||
| 56 | + .arg(path) | ||
| 57 | + .output() | ||
| 58 | + .expect("ffmpeg with libvpx-vp9 must be on PATH for the witnesses"); | ||
| 59 | + assert!(st.status.success(), "fixture generation failed: {st:?}"); | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +/// Patch the FIRST frame's keyframe size fields and rewrite the IVF container | ||
| 63 | +/// size. Profile 0, 8-bit, 4:2:0, `error_resilient = 0`: 8 header bits + | ||
| 64 | +/// 24-bit sync code + 3 `color_space` + 1 `color_range` = the 16-bit | ||
| 65 | +/// `width-1` / `height-1` fields start at bit 36 (VP9 reads MSB-first). | ||
| 66 | +fn patched(ivf: &[u8], nw: u16, nh: u16) -> Vec<u8> { | ||
| 67 | + let mut d = ivf.to_vec(); | ||
| 68 | + let payload = 32 + 12; // IVF file header + first frame chunk header | ||
| 69 | + for (base, value) in [(36usize, nw - 1), (52, nh - 1)] { | ||
| 70 | + for i in 0..16 { | ||
| 71 | + let bit = (value >> (15 - i)) & 1; | ||
| 72 | + let pos = base + i; | ||
| 73 | + let byte = payload + (pos >> 3); | ||
| 74 | + let mask = 1u8 << (7 - (pos & 7)); | ||
| 75 | + if bit == 1 { | ||
| 76 | + d[byte] |= mask; | ||
| 77 | + } else { | ||
| 78 | + d[byte] &= !mask; | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + } | ||
| 82 | + d[12..14].copy_from_slice(&nw.to_le_bytes()); | ||
| 83 | + d[14..16].copy_from_slice(&nh.to_le_bytes()); | ||
| 84 | + d | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +/// ffmpeg's decoded rawvideo byte stream (frame-packed I420, ceil chroma). | ||
| 88 | +fn ffmpeg_raw_yuv(path: &Path) -> Vec<u8> { | ||
| 89 | + let out = std::process::Command::new("ffmpeg") | ||
| 90 | + .args(["-v", "error", "-i"]) | ||
| 91 | + .arg(path) | ||
| 92 | + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"]) | ||
| 93 | + .output() | ||
| 94 | + .expect("ffmpeg on PATH"); | ||
| 95 | + assert!(out.status.success(), "ffmpeg decode failed: {out:?}"); | ||
| 96 | + out.stdout | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +/// Decode the patched odd stream and compare every shown frame byte-exactly; | ||
| 100 | +/// every frame must carry the odd coded size (frame 0 sets it, the rest | ||
| 101 | +/// inherit it), and there must be inter frames. | ||
| 102 | +fn check(nw: u16, nh: u16) { | ||
| 103 | + let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/vp9-witness"); | ||
| 104 | + let even = dir.join("odd-src-322x242.ivf"); | ||
| 105 | + ensure_even_fixture(&even); | ||
| 106 | + let odd = dir.join(format!("odd-{nw}x{nh}.ivf")); | ||
| 107 | + let patched = patched(&std::fs::read(&even).unwrap(), nw, nh); | ||
| 108 | + std::fs::write(&odd, &patched).unwrap(); | ||
| 109 | + | ||
| 110 | + let cw = (nw as usize + 1) / 2; | ||
| 111 | + let ch = (nh as usize + 1) / 2; | ||
| 112 | + let frame_bytes = nw as usize * nh as usize + 2 * cw * ch; | ||
| 113 | + let expected = ffmpeg_raw_yuv(&odd); | ||
| 114 | + assert_eq!( | ||
| 115 | + expected.len() % frame_bytes, | ||
| 116 | + 0, | ||
| 117 | + "ffmpeg's odd-size output is not frame-aligned" | ||
| 118 | + ); | ||
| 119 | + | ||
| 120 | + let (_, _, _, frames) = ivf::parse_ivf(&patched); | ||
| 121 | + let mut decoder = Decoder::new(); | ||
| 122 | + let mut shown = 0usize; | ||
| 123 | + for (i, f) in frames.iter().enumerate() { | ||
| 124 | + let Some(pic) = decoder.decode(&f.data).expect("odd frame decodes") else { | ||
| 125 | + continue; | ||
| 126 | + }; | ||
| 127 | + assert_eq!( | ||
| 128 | + (pic.width, pic.height), | ||
| 129 | + (nw, nh), | ||
| 130 | + "frame {i} must carry the odd coded size" | ||
| 131 | + ); | ||
| 132 | + assert_eq!( | ||
| 133 | + (pic.u.len(), pic.v.len()), | ||
| 134 | + (cw * ch, cw * ch), | ||
| 135 | + "frame {i} chroma must ceil the odd extent" | ||
| 136 | + ); | ||
| 137 | + assert_eq!(pic.uv_stride, cw, "frame {i} chroma stride"); | ||
| 138 | + let off = shown * frame_bytes; | ||
| 139 | + assert!( | ||
| 140 | + off + frame_bytes <= expected.len(), | ||
| 141 | + "frame {i}: ffmpeg has fewer frames than we show" | ||
| 142 | + ); | ||
| 143 | + let mut got = Vec::with_capacity(frame_bytes); | ||
| 144 | + got.extend_from_slice(&pic.y); | ||
| 145 | + got.extend_from_slice(&pic.u); | ||
| 146 | + got.extend_from_slice(&pic.v); | ||
| 147 | + assert_eq!( | ||
| 148 | + got, | ||
| 149 | + expected[off..off + frame_bytes], | ||
| 150 | + "frame {i}: pixel mismatch vs ffmpeg ({nw}x{nh})" | ||
| 151 | + ); | ||
| 152 | + shown += 1; | ||
| 153 | + } | ||
| 154 | + assert!(shown >= 2, "the odd stream must show a keyframe and inter frames"); | ||
| 155 | + assert_eq!(shown * frame_bytes, expected.len(), "frame count mismatch"); | ||
| 156 | +} | ||
| 157 | + | ||
| 158 | +#[test] | ||
| 159 | +fn odd_width_matches_ffmpeg() { | ||
| 160 | + check(321, 242); | ||
| 161 | +} | ||
| 162 | + | ||
| 163 | +#[test] | ||
| 164 | +fn odd_height_matches_ffmpeg() { | ||
| 165 | + check(322, 241); | ||
| 166 | +} | ||
| 167 | + | ||
| 168 | +#[test] | ||
| 169 | +fn odd_width_and_height_matches_ffmpeg() { | ||
| 170 | + check(321, 241); | ||
| 171 | +} | ||