forge
Explore
Log in

Don't you dare go Hollow

Merge lane-vp9-444: decode VP9 profiles 1/3 (4:4:4/4:2:2/4:4:0) and 12-bit

Browse files at this commit

512db7992bec6e73dc7a97593c159ee180d8db0fAhmet Kaan GümüşParent: 3722ceaf81eeb9

19 changed files +1146 -378

crates/ec-vp9/src/decode.rsmodified+196 -151
@@ -4,21 +4,19 @@
44 //! reconstruction and the loop filter (spec 6, 7, 8; libvpx
55 //! `vp9_decodeframe.c`).
66 //!
7-//! Capability: profile-0 8-bit and profile-2 10-bit 4:2:0 keyframes,
8-//! intra-only frames and inter frames (motion compensation, residual
9-//! reconstruction, the loop filter and an 8-slot reference DPB). A reference
10-//! whose coded size differs from the current frame is scaled during
11-//! prediction; profiles 1/3, 12-bit and other subsamplings are refused by
12-//! name; `show_existing_frame` returns the buffered reference picture instead
13-//! of pretending to decode. Odd coded extents are supported: chroma stores
14-//! and crops at `(w + 1) / 2` (`uv_crop_width`), matching libvpx.
7+//! Capability: every VP9 profile (0-3), 8/10/12-bit and every coded
8+//! subsampling (4:2:0, 4:2:2, 4:4:0, 4:4:4): keyframes, intra-only frames
9+//! and inter frames (motion compensation, residual reconstruction, the loop
10+//! filter and an 8-slot reference DPB). A reference whose coded size differs
11+//! from the current frame is scaled during prediction;
12+//! `show_existing_frame` returns the buffered reference picture instead of
13+//! pretending to decode. Odd coded extents are supported: chroma stores and
14+//! crops at `(w + ss_x) >> ss_x` (`uv_crop_width`), matching libvpx.
1515
1616 use crate::header::{FrameContext, read_compressed_header};
1717 use crate::intra::build_intra_predictors;
1818 use crate::loopfilter::LfGrids;
19-use crate::mc::{
20- RefPlane, ScaleFactors, average_split_mvs_chroma, build_inter_predictors, split_mv,
21-};
19+use crate::mc::{RefPlane, ScaleFactors, average_split_mvs, build_inter_predictors};
2220 use crate::modes::{MiInfo, MiState, read_intra_frame_mode_info};
2321 use crate::tables::*;
2422 use crate::tokens::{PlaneContexts, decode_coefs, scan_for};
@@ -33,9 +31,10 @@ const SB_MI: usize = 8; // a 64x64 superblock in mi (8x8) units
3331 pub struct Picture {
3432 /// Luma plane, `stride`-spaced rows of `width` samples.
3533 pub y: Vec<Sample>,
36- /// U (Cb) plane, half resolution.
34+ /// U (Cb) plane, chroma resolution (`(width + ss_x) >> ss_x` by
35+ /// `(height + ss_y) >> ss_y`).
3736 pub u: Vec<Sample>,
38- /// V (Cr) plane, half resolution.
37+ /// V (Cr) plane, chroma resolution.
3938 pub v: Vec<Sample>,
4039 /// Frame width in pixels.
4140 pub width: u16,
@@ -43,11 +42,17 @@ pub struct Picture {
4342 pub height: u16,
4443 /// Luma row pitch of the returned planes (== `width`; the crop packs rows).
4544 pub stride: usize,
46- /// Chroma row pitch of the returned planes (`(width + 1) / 2`; chroma
47- /// ceils an odd coded width).
45+ /// Chroma row pitch of the returned planes (`(width + ss_x) >> ss_x`;
46+ /// chroma ceils an odd coded width).
4847 pub uv_stride: usize,
4948 /// Bits per sample (8 for profile 0, 10 for profile 2).
5049 pub bit_depth: u8,
50+ /// Chroma horizontal subsampling (`subsampling_x`): 0 for 4:4:4, 1 for
51+ /// 4:2:0/4:2:2. Chroma width is `(width + subsampling_x) >> subsampling_x`.
52+ pub subsampling_x: u8,
53+ /// Chroma vertical subsampling (`subsampling_y`): 0 for 4:4:4/4:2:2, 1
54+ /// for 4:2:0/4:4:0.
55+ pub subsampling_y: u8,
5156 }
5257
5358 struct Planes {
@@ -60,6 +65,12 @@ struct Planes {
6065 ah: usize,
6166 /// Bits per sample this frame was decoded at.
6267 bd: u8,
68+ /// Chroma horizontal subsampling (`subsampling_x`): 0 for 4:4:4, 1 for
69+ /// 4:2:0/4:2:2.
70+ ss_x: usize,
71+ /// Chroma vertical subsampling (`subsampling_y`): 0 for 4:4:4/4:2:2, 1
72+ /// for 4:2:0/4:4:0.
73+ ss_y: usize,
6374 }
6475
6576 impl Planes {
@@ -67,18 +78,22 @@ impl Planes {
6778 /// neutral grey `1 << (bd - 1)` (`128` at 8-bit, `512` at 10-bit). The
6879 /// aligned the decode writes real samples into it (blocks at the frame
6980 /// edge decode at FULL size), so the fill only backs the never-written
70- /// padding past the MI extent.
71- fn new(aw: usize, ah: usize, bd: u8) -> Planes {
81+ /// padding past the MI extent. Chroma planes are `aw >> ss_x` by
82+ /// `ah >> ss_y`; since `aw`/`ah` are 64-multiples the shift is exact.
83+ fn new(aw: usize, ah: usize, bd: u8, ss_x: usize, ss_y: usize) -> Planes {
7284 let fill = 1u16 << (bd - 1);
85+ let (cw, ch) = (aw >> ss_x, ah >> ss_y);
7386 Planes {
7487 y: vec![fill; aw * ah],
75- u: vec![fill; aw / 2 * ah / 2],
76- v: vec![fill; aw / 2 * ah / 2],
88+ u: vec![fill; cw * ch],
89+ v: vec![fill; cw * ch],
7790 ys: aw,
78- uvs: aw / 2,
91+ uvs: cw,
7992 aw,
8093 ah,
8194 bd,
95+ ss_x,
96+ ss_y,
8297 }
8398 }
8499
@@ -101,7 +116,7 @@ impl Planes {
101116 if plane == 0 {
102117 (self.aw, self.ah)
103118 } else {
104- (self.aw / 2, self.ah / 2)
119+ (self.aw >> self.ss_x, self.ah >> self.ss_y)
105120 }
106121 }
107122 }
@@ -234,25 +249,10 @@ impl Decoder {
234249 None => Err(corrupt("show_existing_frame names an empty slot")),
235250 };
236251 }
237- if hdr.profile != 0 && hdr.profile != 2 {
238- return Err(Error::unsupported(
239- format!("vp9 profile {}", hdr.profile),
240- "this lane decodes profiles 0 and 2 only (profiles 1 and 3 are 4:4:4)",
241- ));
242- }
243- if hdr.bit_depth != 8 && hdr.bit_depth != 10 {
252+ if hdr.bit_depth != 8 && hdr.bit_depth != 10 && hdr.bit_depth != 12 {
244253 return Err(Error::unsupported(
245254 format!("vp9 bit depth {}", hdr.bit_depth),
246- "this lane decodes 8- and 10-bit only (12-bit is not ported)",
247- ));
248- }
249- if hdr.subsampling_x != 1 || hdr.subsampling_y != 1 {
250- return Err(Error::unsupported(
251- "vp9 subsampling",
252- format!(
253- "this lane decodes 4:2:0 only (subsampling {}/{} at profile {})",
254- hdr.subsampling_x, hdr.subsampling_y, hdr.profile
255- ),
255+ "this lane decodes 8-, 10- and 12-bit only",
256256 ));
257257 }
258258 // One index per decoded frame (the dumps' `frame=` field), so this
@@ -405,16 +405,10 @@ impl Decoder {
405405 if hdr.show_existing_frame {
406406 continue;
407407 }
408- if hdr.profile != 0 && hdr.profile != 2 {
409- return Err(Error::unsupported(
410- format!("vp9 profile {}", hdr.profile),
411- "this lane decodes profiles 0 and 2 only (profiles 1 and 3 are 4:4:4)",
412- ));
413- }
414- if hdr.bit_depth != 8 && hdr.bit_depth != 10 {
408+ if hdr.bit_depth != 8 && hdr.bit_depth != 10 && hdr.bit_depth != 12 {
415409 return Err(Error::unsupported(
416410 format!("vp9 bit depth {}", hdr.bit_depth),
417- "this lane decodes 8- and 10-bit only (12-bit is not ported)",
411+ "this lane decodes 8-, 10- and 12-bit only",
418412 ));
419413 }
420414 if hdr.frame_type == FrameType::Key {
@@ -447,6 +441,8 @@ impl Decoder {
447441 index: usize,
448442 pixels: bool,
449443 ) -> Result<Option<Picture>> {
444+ let ss_x = hdr.subsampling_x as usize;
445+ let ss_y = hdr.subsampling_y as usize;
450446 // Frame-context bookkeeping (spec 6.2 / libvpx `vp9_decode_frame`):
451447 // 3 resets every stored context, 2 resets the selected one; 1 means
452448 // "reset after this frame" and takes no action at frame start.
@@ -479,7 +475,7 @@ impl Decoder {
479475 let height = hdr.height as usize;
480476 let aw = width.div_ceil(64) * 64;
481477 let ah = height.div_ceil(64) * 64;
482- self.planes = Some(Planes::new(aw, ah, hdr.bit_depth));
478+ self.planes = Some(Planes::new(aw, ah, hdr.bit_depth, ss_x, ss_y));
483479 }
484480
485481 let hdr_start = hdr.uncompressed_header_size as usize;
@@ -583,8 +579,8 @@ impl Decoder {
583579 let aligned_cols = mi_cols.div_ceil(SB_MI) * SB_MI;
584580 let mut ectx = [
585581 PlaneContexts::new(aligned_cols * 2),
586- PlaneContexts::new(aligned_cols),
587- PlaneContexts::new(aligned_cols),
582+ PlaneContexts::new(aligned_cols * 2),
583+ PlaneContexts::new(aligned_cols * 2),
588584 ];
589585 ectx.iter_mut().for_each(|p| p.above.fill(0));
590586 let mut mi = MiState::new(mi_cols, mi_rows);
@@ -681,7 +677,10 @@ impl Decoder {
681677 p.uvs,
682678 vw,
683679 vh,
684- vh / 2,
680+ vw >> ss_x,
681+ vh >> ss_y,
682+ ss_x,
683+ ss_y,
685684 hdr.loop_filter.sharpness,
686685 hdr.bit_depth,
687686 );
@@ -821,27 +820,36 @@ impl Decoder {
821820 bw: usize,
822821 bh: usize,
823822 ) -> Result<()> {
824- let n4w = plane_n4_w(sb_type, bw);
825- let n4h = plane_n4_h(sb_type, bh);
823+ let (ss_x, ss_y) = plane_ss(hdr, 1);
824+ let n4w = [
825+ plane_n4_w(bw, 0),
826+ plane_n4_w(bw, ss_x),
827+ plane_n4_w(bw, ss_x),
828+ ];
829+ let n4h = [
830+ plane_n4_h(bh, 0),
831+ plane_n4_h(bh, ss_y),
832+ plane_n4_h(bh, ss_y),
833+ ];
826834 if info.skip {
827835 // dec_reset_skip_context
828- reset_skip_context(ectx, row, col, sb_type, n4w, n4h);
836+ reset_skip_context(ectx, row, col, sb_type, n4w, n4h, ss_x, ss_y);
829837 return Ok(());
830838 }
831839 let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32;
832840 let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32;
833841 for plane in 0..3usize {
834- let s = usize::from(plane != 0);
842+ let (ss_x, ss_y) = plane_ss(hdr, plane);
835843 let tx_size = if plane == 0 {
836844 info.tx_size
837845 } else {
838- get_uv_tx_size(info.tx_size, sb_type)
846+ get_uv_tx_size(info.tx_size, sb_type, ss_x, ss_y)
839847 };
840848 let step = 1usize << tx_size;
841849 let max_blocks_wide =
842- (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize;
850+ (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize;
843851 let max_blocks_high =
844- (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize;
852+ (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize;
845853 for brow in (0..max_blocks_high).step_by(step) {
846854 for bcol in (0..max_blocks_wide).step_by(step) {
847855 let _ = decode_inter_tx_tokens(
@@ -888,10 +896,19 @@ impl Decoder {
888896 mi: &mut MiState,
889897 tile_col_start: usize,
890898 ) -> Result<()> {
899+ let (ss_x, ss_y) = plane_ss(hdr, 1);
891900 let x_mis = bw.min(mi.mi_cols - col);
892901 let y_mis = bh.min(mi.mi_rows - row);
893- let n4w = plane_n4_w(sb_type, bw);
894- let n4h = plane_n4_h(sb_type, bh);
902+ let n4w = [
903+ plane_n4_w(bw, 0),
904+ plane_n4_w(bw, ss_x),
905+ plane_n4_w(bw, ss_x),
906+ ];
907+ let n4h = [
908+ plane_n4_h(bh, 0),
909+ plane_n4_h(bh, ss_y),
910+ plane_n4_h(bh, ss_y),
911+ ];
895912 let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32;
896913 let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32;
897914
@@ -930,23 +947,23 @@ impl Decoder {
930947 // top of `decode_block` (decodeframe.c:960), BEFORE the intra/inter
931948 // split, so skipped intra blocks inside an inter frame reset too.
932949 if info.skip {
933- reset_skip_context(ectx, row, col, sb_type, n4w, n4h);
950+ reset_skip_context(ectx, row, col, sb_type, n4w, n4h, ss_x, ss_y);
934951 }
935952
936953 if !info.is_inter {
937954 // `predict_and_reconstruct_intra_block` per transform block.
938955 for plane in 0..3usize {
939- let s = usize::from(plane != 0);
956+ let (ss_x, ss_y) = plane_ss(hdr, plane);
940957 let tx_size = if plane == 0 {
941958 info.tx_size
942959 } else {
943- get_uv_tx_size(info.tx_size, sb_type)
960+ get_uv_tx_size(info.tx_size, sb_type, ss_x, ss_y)
944961 };
945962 let step = 1usize << tx_size;
946963 let max_blocks_wide =
947- (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize;
964+ (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize;
948965 let max_blocks_high =
949- (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize;
966+ (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize;
950967 for brow in (0..max_blocks_high).step_by(step) {
951968 for bcol in (0..max_blocks_wide).step_by(step) {
952969 self.predict_and_reconstruct(
@@ -1010,17 +1027,17 @@ impl Decoder {
10101027 // Reconstruction: `reconstruct_inter_block` per transform block.
10111028 let mut eobtotal = 0usize;
10121029 for plane in 0..3usize {
1013- let s = usize::from(plane != 0);
1030+ let (ss_x, ss_y) = plane_ss(hdr, plane);
10141031 let tx_size = if plane == 0 {
10151032 info.tx_size
10161033 } else {
1017- get_uv_tx_size(info.tx_size, sb_type)
1034+ get_uv_tx_size(info.tx_size, sb_type, ss_x, ss_y)
10181035 };
10191036 let step = 1usize << tx_size;
10201037 let max_blocks_wide =
1021- (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize;
1038+ (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize;
10221039 let max_blocks_high =
1023- (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize;
1040+ (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize;
10241041 for brow in (0..max_blocks_high).step_by(step) {
10251042 for bcol in (0..max_blocks_wide).step_by(step) {
10261043 let coef = decode_inter_tx_tokens(
@@ -1051,8 +1068,8 @@ impl Decoder {
10511068 let planes = self.planes.as_mut().expect("frame scratch");
10521069 let stride = planes.dims(plane).0;
10531070 let bd = planes.bd;
1054- let x0 = ((col * 8) >> s) + bcol * 4;
1055- let y0 = ((row * 8) >> s) + brow * 4;
1071+ let x0 = ((col * 8) >> ss_x) + bcol * 4;
1072+ let y0 = ((row * 8) >> ss_y) + brow * 4;
10561073 let dst_off = y0 * stride + x0;
10571074 let (data, _) = planes.plane(plane);
10581075 inverse_transform_add(
@@ -1082,6 +1099,8 @@ impl Decoder {
10821099 }
10831100
10841101 fn decode_keyframe(&mut self, hdr: &FrameHeader, frame: &[u8], tx_mode: u8) -> Result<Picture> {
1102+ let ss_x = hdr.subsampling_x as usize;
1103+ let ss_y = hdr.subsampling_y as usize;
10851104 let width = hdr.width as usize;
10861105 let height = hdr.height as usize;
10871106 // The decode buffer is padded to whole superblocks: libvpx decodes a
@@ -1092,7 +1111,7 @@ impl Decoder {
10921111 // mi_row 132 writes 8 rows past a 1080-row buffer).
10931112 let aw = width.div_ceil(64) * 64;
10941113 let ah = height.div_ceil(64) * 64;
1095- self.planes = Some(Planes::new(aw, ah, hdr.bit_depth));
1114+ self.planes = Some(Planes::new(aw, ah, hdr.bit_depth, ss_x, ss_y));
10961115 let tail_start = std::env::var("EC_VP9_FORCE_TAIL")
10971116 .ok()
10981117 .and_then(|s| s.parse().ok())
@@ -1109,6 +1128,8 @@ impl Decoder {
11091128 tx_mode: u8,
11101129 tail: &[u8],
11111130 ) -> Result<Picture> {
1131+ let ss_x = hdr.subsampling_x as usize;
1132+ let ss_y = hdr.subsampling_y as usize;
11121133 let mi_cols = hdr.mi_cols() as usize;
11131134 let mi_rows = hdr.mi_rows() as usize;
11141135 let sb64_cols = mi_cols.div_ceil(SB_MI);
@@ -1171,8 +1192,8 @@ impl Decoder {
11711192 let aligned_cols = mi_cols.div_ceil(SB_MI) * SB_MI;
11721193 let mut ectx = [
11731194 PlaneContexts::new(aligned_cols * 2),
1174- PlaneContexts::new(aligned_cols),
1175- PlaneContexts::new(aligned_cols),
1195+ PlaneContexts::new(aligned_cols * 2),
1196+ PlaneContexts::new(aligned_cols * 2),
11761197 ];
11771198 ectx.iter_mut().for_each(|p| p.above.fill(0));
11781199 let mut mi = MiState::new(mi_cols, mi_rows);
@@ -1248,7 +1269,10 @@ impl Decoder {
12481269 p.uvs,
12491270 vw,
12501271 vh,
1251- vh / 2,
1272+ vw >> ss_x,
1273+ vh >> ss_y,
1274+ ss_x,
1275+ ss_y,
12521276 hdr.loop_filter.sharpness,
12531277 hdr.bit_depth,
12541278 );
@@ -1466,6 +1490,21 @@ impl Decoder {
14661490 let x_mis = bw.min(mi.mi_cols - col);
14671491 let y_mis = bh.min(mi.mi_rows - row);
14681492
1493+ // `decode_block` (decodeframe.c:942): a block size that does not
1494+ // exist once subsampled (`BLOCK_8X4` under 4:4:0, `BLOCK_4X8` under
1495+ // 4:2:2) is a corrupt frame. Sub-8x8 sizes have no `ss_size_lookup`
1496+ // row to check and are exempt.
1497+ if sb_type >= 3 && (hdr.subsampling_x != 0 || hdr.subsampling_y != 0) {
1498+ ensure(
1499+ ss_size(
1500+ sb_type,
1501+ hdr.subsampling_x as usize,
1502+ hdr.subsampling_y as usize,
1503+ ) != BLOCK_INVALID,
1504+ "invalid block size for the chroma subsampling",
1505+ )?;
1506+ }
1507+
14691508 // `frame_is_intra_only` (onyxc_int.h:363): a keyframe OR an
14701509 // intra-only frame walks the intra mode-info path.
14711510 let intra_frame = hdr.frame_type == FrameType::Key || hdr.intra_only;
@@ -1546,16 +1585,17 @@ impl Decoder {
15461585 // Per-plane 4x4 counts. Sub-8x8 shapes still occupy the WHOLE 8x8
15471586 // mi area (libvpx set_plane_n4 with bw=bh=1): luma walks 2x2 4x4
15481587 // TUs, chroma is one 4x4 (n4 >> subsampling), coded once per 8x8.
1549- let n4w = if sb_type < 3 {
1550- [2, 1, 1]
1551- } else {
1552- [bw * 2, bw, bw]
1553- };
1554- let n4h = if sb_type < 3 {
1555- [2, 1, 1]
1556- } else {
1557- [bh * 2, bh, bh]
1558- };
1588+ let (ss_x, ss_y) = plane_ss(hdr, 1);
1589+ let n4w = [
1590+ plane_n4_w(bw, 0),
1591+ plane_n4_w(bw, ss_x),
1592+ plane_n4_w(bw, ss_x),
1593+ ];
1594+ let n4h = [
1595+ plane_n4_h(bh, 0),
1596+ plane_n4_h(bh, ss_y),
1597+ plane_n4_h(bh, ss_y),
1598+ ];
15591599 let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32;
15601600 let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32;
15611601
@@ -1575,13 +1615,13 @@ impl Decoder {
15751615
15761616 if info.skip {
15771617 for plane in 0..3usize {
1578- let s = usize::from(plane != 0);
1579- let offx = (col << 1) >> s;
1580- let offy = (row << 1) >> s;
1618+ let (sx, sy) = if plane == 0 { (0, 0) } else { (ss_x, ss_y) };
1619+ let offx = (col << 1) >> sx;
1620+ let offy = (row << 1) >> sy;
15811621 for i in 0..n4w[plane] {
15821622 ectx[plane].above[offx + i] = 0;
15831623 }
1584- let lrows = 32usize >> s;
1624+ let lrows = 32usize >> sy;
15851625 for i in 0..n4h[plane].min(lrows) {
15861626 ectx[plane].left[(offy + i) % lrows] = 0;
15871627 }
@@ -1589,17 +1629,17 @@ impl Decoder {
15891629 }
15901630
15911631 for plane in 0..3usize {
1592- let s = usize::from(plane != 0);
1632+ let (ss_x, ss_y) = plane_ss(hdr, plane);
15931633 let tx_size = if plane == 0 {
15941634 info.tx_size
15951635 } else {
1596- get_uv_tx_size(info.tx_size, sb_type)
1636+ get_uv_tx_size(info.tx_size, sb_type, ss_x, ss_y)
15971637 };
15981638 let step = 1usize << tx_size;
15991639 let max_blocks_wide =
1600- (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize;
1640+ (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize;
16011641 let max_blocks_high =
1602- (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize;
1642+ (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize;
16031643 for brow in (0..max_blocks_high).step_by(step) {
16041644 for bcol in (0..max_blocks_wide).step_by(step) {
16051645 self.predict_and_reconstruct(
@@ -1656,7 +1696,7 @@ impl Decoder {
16561696 mb_to_bottom: i32,
16571697 tile_col_start: usize,
16581698 ) -> Result<()> {
1659- let s = usize::from(plane != 0);
1699+ let (ss_x, ss_y) = plane_ss(hdr, plane);
16601700 let mode = if plane == 0 {
16611701 if sb_type < 3 {
16621702 info.bmi[(tx_row << 1) + tx_col]
@@ -1670,8 +1710,8 @@ impl Decoder {
16701710 let stride = planes.dims(plane).0;
16711711 let bd = planes.bd;
16721712 let bs = 4 << tx_size;
1673- let x0 = ((mi_col * 8) >> s) + tx_col * 4;
1674- let y0 = ((mi_row * 8) >> s) + tx_row * 4;
1713+ let x0 = ((mi_col * 8) >> ss_x) + tx_col * 4;
1714+ let y0 = ((mi_row * 8) >> ss_y) + tx_row * 4;
16751715 // vp9_predict_intra_block `have_top = loff || (xd->above_mi != NULL)`
16761716 // (vp9_reconintra.c:411); `above_mi` is FRAME-gated
16771717 // (`set_mi_row_col`, vp9_onyxc_int.h:430: `mi_row != 0`), NOT
@@ -1683,15 +1723,15 @@ impl Decoder {
16831723 // last tx column of ITS OWN block (pd->n4_w units) — above-right
16841724 // past the block's right edge belongs to a not-yet-decoded
16851725 // neighbour, so it must stage as unavailable.
1686- let rgt = tx_col + (1usize << tx_size) < bw * 2 >> s;
1726+ let rgt = tx_col + (1usize << tx_size) < bw * 2 >> ss_x;
16871727
16881728 // `xd->cur_buf->y_width/y_height` (vp9_reconintra.c:288-292) are the
16891729 // MI-ALIGNED (8-multiple) buffer extent, not the visible coded size:
16901730 // confirmed by instrumenting the oracle's staging for a 320x242 frame
16911731 // (`fh=248`, not 242). The extend-vs-direct DECISION is `mb_to_*_edge`
16921732 // (`:302/326/354`, passed in as `bot_ext`/`right_ext`).
1693- let vw = (hdr.mi_cols() as usize * 8) >> s;
1694- let vh = (hdr.mi_rows() as usize * 8) >> s;
1733+ let vw = (hdr.mi_cols() as usize * 8) >> ss_x;
1734+ let vh = (hdr.mi_rows() as usize * 8) >> ss_y;
16951735
16961736 // 1. Intra prediction (reads reconstructed neighbours).
16971737 {
@@ -1733,9 +1773,9 @@ impl Decoder {
17331773 [q.chroma_dc, q.chroma_ac]
17341774 };
17351775 let ptype = usize::from(plane != 0);
1736- let ax = ((mi_col << 1) >> s) + tx_col;
1737- let lrows = 32usize >> s;
1738- let ay = (((mi_row << 1) >> s) + tx_row) % lrows;
1776+ let ax = ((mi_col << 1) >> ss_x) + tx_col;
1777+ let lrows = 32usize >> ss_y;
1778+ let ay = (((mi_row << 1) >> ss_y) + tx_row) % lrows;
17391779 // libvpx reads the context as !!*(uintN_t *)a / l over the WHOLE
17401780 // tx-block span (uint16 for 8x8, uint32 for 16x16, uint64 for
17411781 // 32x32), not just the first entry.
@@ -1783,10 +1823,10 @@ impl Decoder {
17831823 );
17841824
17851825 // 3. Context write-back (vp9_decode_block_tokens, v1.15 shifts).
1786- let n4w = bw * 2 >> s;
1787- let n4h = bh * 2 >> s;
1788- let maxw = (n4w as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize;
1789- let maxh = (n4h as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize;
1826+ let n4w = bw * 2 >> ss_x;
1827+ let n4h = bh * 2 >> ss_y;
1828+ let maxw = (n4w as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize;
1829+ let maxh = (n4h as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize;
17901830 let nblocks = 1usize << tx_size;
17911831 let shift_a = if maxw > 0 && nblocks + tx_col > maxw {
17921832 (nblocks - (maxw - tx_col)) * 8
@@ -1885,9 +1925,11 @@ fn crop_picture(planes: &Planes, width: u16, height: u16) -> Picture {
18851925 }
18861926 out
18871927 };
1888- // Chroma CEILS: the 4:2:0 plane of an odd extent carries one extra
1889- // half-sample column/row (`uv_crop_width = (w + 1) / 2`).
1890- let (cw, chh) = ((w + 1) / 2, (h + 1) / 2);
1928+ // Chroma CEILS: a subsampled plane of an odd extent carries one extra
1929+ // half-sample column/row (`uv_crop_width = (w + ss_x) >> ss_x`). Under
1930+ // 4:4:4 the chroma plane is the full luma extent.
1931+ let (sx, sy) = (planes.ss_x, planes.ss_y);
1932+ let (cw, chh) = ((w + sx) >> sx, (h + sy) >> sy);
18911933 Picture {
18921934 y: crop(&planes.y, planes.ys, w, h),
18931935 u: crop(&planes.u, planes.uvs, cw, chh),
@@ -1897,30 +1939,36 @@ fn crop_picture(planes: &Planes, width: u16, height: u16) -> Picture {
18971939 stride: w,
18981940 uv_stride: cw,
18991941 bit_depth: planes.bd,
1942+ subsampling_x: sx as u8,
1943+ subsampling_y: sy as u8,
19001944 }
19011945 }
19021946
1903-/// `pd->n4_w` (`set_plane_n4`, decodeframe.c:809): the plane block's width in
1904-/// its own 4x4 units. Sub-8x8 shapes occupy a whole 8x8 mi cell
1905-/// (`set_plane_n4(xd, bw=1, bh=1)`) so luma walks 2x2 and chroma 1x1.
1906-fn plane_n4_w(sb_type: usize, bw: usize) -> [usize; 3] {
1907- if sb_type < 3 {
1908- [2, 1, 1]
1947+/// `(ss_x, ss_y)` of a plane: luma is always full-resolution.
1948+#[inline]
1949+fn plane_ss(hdr: &FrameHeader, plane: usize) -> (usize, usize) {
1950+ if plane == 0 {
1951+ (0, 0)
19091952 } else {
1910- [bw * 2, bw, bw]
1953+ (hdr.subsampling_x as usize, hdr.subsampling_y as usize)
19111954 }
19121955 }
19131956
1957+/// `pd->n4_w` (`set_plane_n4`, decodeframe.c:809): the plane block's width in
1958+/// its own 4x4 units, `(bw << 1) >> ss_x`. Sub-8x8 shapes occupy a whole 8x8
1959+/// mi cell (`set_plane_n4(xd, bw=1, bh=1)`), so luma walks 2x2 and chroma
1960+/// `(2 >> ss_x)` wide.
1961+fn plane_n4_w(bw: usize, ss_x: usize) -> usize {
1962+ (bw << 1) >> ss_x
1963+}
1964+
19141965 /// `pd->n4_h`, the height twin of [`plane_n4_w`].
1915-fn plane_n4_h(sb_type: usize, bh: usize) -> [usize; 3] {
1916- if sb_type < 3 {
1917- [2, 1, 1]
1918- } else {
1919- [bh * 2, bh, bh]
1920- }
1966+fn plane_n4_h(bh: usize, ss_y: usize) -> usize {
1967+ (bh << 1) >> ss_y
19211968 }
19221969
19231970 /// `dec_reset_skip_context` (`vp9_decodeframe.c:800`).
1971+#[allow(clippy::too_many_arguments)]
19241972 fn reset_skip_context(
19251973 ectx: &mut [PlaneContexts; 3],
19261974 row: usize,
@@ -1928,16 +1976,18 @@ fn reset_skip_context(
19281976 sb_type: usize,
19291977 n4w: [usize; 3],
19301978 n4h: [usize; 3],
1979+ ss_x: usize,
1980+ ss_y: usize,
19311981 ) {
19321982 let _ = sb_type;
19331983 for plane in 0..3usize {
1934- let s = usize::from(plane != 0);
1935- let offx = (col << 1) >> s;
1936- let offy = (row << 1) >> s;
1984+ let (sx, sy) = if plane == 0 { (0, 0) } else { (ss_x, ss_y) };
1985+ let offx = (col << 1) >> sx;
1986+ let offy = (row << 1) >> sy;
19371987 for i in 0..n4w[plane] {
19381988 ectx[plane].above[offx + i] = 0;
19391989 }
1940- let lrows = 32usize >> s;
1990+ let lrows = 32usize >> sy;
19411991 for i in 0..n4h[plane].min(lrows) {
19421992 ectx[plane].left[(offy + i) % lrows] = 0;
19431993 }
@@ -1948,10 +1998,10 @@ fn reset_skip_context(
19481998 // block), not the whole rolling buffer from base index 0.
19491999 let mut line = format!("TDK row={row} col={col} n4w={n4w:?} n4h={n4h:?}");
19502000 for plane in 0..3usize {
1951- let s = usize::from(plane != 0);
1952- let offx = (col << 1) >> s;
1953- let offy = (row << 1) >> s;
1954- let lrows = 32usize >> s;
2001+ let (sx, sy) = if plane == 0 { (0, 0) } else { (ss_x, ss_y) };
2002+ let offx = (col << 1) >> sx;
2003+ let offy = (row << 1) >> sy;
2004+ let lrows = 32usize >> sy;
19552005 let a: Vec<u8> = (0..n4w[plane])
19562006 .map(|i| ectx[plane].above[offx + i])
19572007 .collect();
@@ -2031,7 +2081,7 @@ fn decode_inter_tx_tokens(
20312081 mb_to_right: i32,
20322082 mb_to_bottom: i32,
20332083 ) -> crate::tokens::CoeffBlock {
2034- let s = usize::from(plane != 0);
2084+ let (ss_x, ss_y) = plane_ss(hdr, plane);
20352085 // `predict_and_reconstruct_intra_block` (decodeframe.c:320-345): the luma
20362086 // mode of a sub-8x8 block is the SUB-BLOCK's, and an intra block inside an
20372087 // inter frame picks its tx type from that mode — `reconstruct_inter_block`
@@ -2054,9 +2104,9 @@ fn decode_inter_tx_tokens(
20542104 [q.chroma_dc, q.chroma_ac]
20552105 };
20562106 let ptype = usize::from(plane != 0);
2057- let ax = ((col << 1) >> s) + bcol;
2058- let lrows = 32usize >> s;
2059- let ay = (((row << 1) >> s) + brow) % lrows;
2107+ let ax = ((col << 1) >> ss_x) + bcol;
2108+ let lrows = 32usize >> ss_y;
2109+ let ay = (((row << 1) >> ss_y) + brow) % lrows;
20602110 let nblocks = 1usize << tx_size;
20612111 let ctx_in = usize::from(ectx[plane].above[ax..ax + nblocks].iter().any(|&v| v != 0))
20622112 + usize::from(ectx[plane].left[ay..ay + nblocks].iter().any(|&v| v != 0));
@@ -2084,10 +2134,10 @@ fn decode_inter_tx_tokens(
20842134 plane,
20852135 );
20862136 // vp9_decode_block_tokens context write-back (v1.15 edge shifts).
2087- let n4w_s = bw * 2 >> s;
2088- let n4h_s = bh * 2 >> s;
2089- let maxw = (n4w_s as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize;
2090- let maxh = (n4h_s as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize;
2137+ let n4w_s = bw * 2 >> ss_x;
2138+ let n4h_s = bh * 2 >> ss_y;
2139+ let maxw = (n4w_s as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize;
2140+ let maxh = (n4h_s as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize;
20912141 let shift_a = if maxw > 0 && nblocks + bcol > maxw {
20922142 (nblocks - (maxw - bcol)) * 8
20932143 } else {
@@ -2132,9 +2182,7 @@ fn inter_predict_plane(
21322182 mb_to_bottom: i32,
21332183 sb_type: usize,
21342184 ) -> Result<()> {
2135- let s = usize::from(plane != 0);
2136- let ss_x = s;
2137- let ss_y = s;
2185+ let (ss_x, ss_y) = plane_ss(hdr, plane);
21382186 let pw = n4w[plane] * 4;
21392187 let ph = n4h[plane] * 4;
21402188 let mb_left = -((col as i32) * 64);
@@ -2179,27 +2227,24 @@ fn inter_predict_plane(
21792227 // plain `>> 1` is one short whenever the coded extent is odd and
21802228 // the predictor's border/inside test then clips the last chroma
21812229 // column (or row).
2182- width: (ref_frame.width as usize + s) >> s,
2183- height: (ref_frame.height as usize + s) >> s,
2230+ width: (ref_frame.width as usize + ss_x) >> ss_x,
2231+ height: (ref_frame.height as usize + ss_y) >> ss_y,
21842232 bd: ref_frame.planes.bd,
21852233 };
21862234 if sb_type < 3 {
21872235 // libvpx builds one 4x4 prediction per sub-block, with the MV from
2188- // `average_split_mvs` (luma: the sub-block's own MV; 4:2:0 chroma:
2189- // the q4-rounded average of the four).
2236+ // `average_split_mvs` selected by subsampling: luma and 4:4:4
2237+ // chroma take the sub-block's own MV, 4:4:0 averages the vertical
2238+ // pair, 4:2:2 the horizontal pair, 4:2:0 all four (q4).
21902239 let n4x = n4w[plane];
21912240 let n4y = n4h[plane];
21922241 for y in 0..n4y {
21932242 for xx in 0..n4x {
21942243 let i = y * n4x + xx;
2195- let mv = if plane == 0 {
2196- split_mv(&info.bmi_mv, i, rf)
2197- } else {
2198- average_split_mvs_chroma(&info.bmi_mv, rf)
2199- };
2244+ let mv = average_split_mvs(&info.bmi_mv, rf, i, ss_x, ss_y);
22002245 let (data, _) = planes.plane(plane);
22012246 let dst_off =
2202- (((row * 8) >> s) + y * 4) * dst_stride + ((col * 8) >> s) + xx * 4;
2247+ (((row * 8) >> ss_y) + y * 4) * dst_stride + ((col * 8) >> ss_x) + xx * 4;
22032248 build_inter_predictors(
22042249 &mut data[dst_off..],
22052250 dst_stride,
@@ -2227,7 +2272,7 @@ fn inter_predict_plane(
22272272 }
22282273 } else {
22292274 let (data, _) = planes.plane(plane);
2230- let dst_off = ((row * 8) >> s) * dst_stride + ((col * 8) >> s);
2275+ let dst_off = ((row * 8) >> ss_y) * dst_stride + ((col * 8) >> ss_x);
22312276 build_inter_predictors(
22322277 &mut data[dst_off..],
22332278 dst_stride,
crates/ec-vp9/src/lib.rsmodified+8 -6
@@ -1,18 +1,20 @@
1-//! Native VP9 (profiles 0 and 2, 8- and 10-bit 4:2:0) decoder.
1+//! Native VP9 decoder: all four profiles, 8/10/12-bit, and every coded
2+//! subsampling (4:2:0, 4:2:2, 4:4:0, 4:4:4).
23 //!
34 //! The crate implements the VP9 bitstream specification directly
45 //! ([spec]); libvpx's output is used only as a test oracle
56 //! (sample-exactness witnesses against ffmpeg's libvpx-based decoder),
67 //! never as code linked at runtime. Every module documents the spec
78 //! section it implements. Inter frames decode through motion compensation,
8-//! residual reconstruction and the loop filter; intra-only frames, profiles
9-//! 1/3 (4:4:4), 12-bit and other subsamplings are refused by name.
9+//! residual reconstruction and the loop filter; intra-only frames and
10+//! scaled references are supported too.
1011 //!
1112 //! Planes are stored as `u16` for every depth ([`Sample`]); kernels take the
1213 //! frame's `bd` and clamp with `clip_pixel_bd`, so one decode walk serves
13-//! both depths. The only depth-dependent bitstream element is the CAT6
14-//! coefficient token (14 bits/`vp9_cat6_prob` at 8-bit, 16 bits/
15-//! `vp9_cat6_prob_high12 + 2` at 10- and 12-bit).
14+//! every depth and subsampling. The only depth-dependent bitstream element is
15+//! the CAT6 coefficient token (14 bits/`vp9_cat6_prob` at 8-bit, 16 bits/
16+//! `vp9_cat6_prob_high12 + 2` at 10-bit, 18 bits/`vp9_cat6_prob_high12` at
17+//! 12-bit).
1618 //!
1719 //! [spec]: https://www.webmproject.org/docs/vp9/
1820 //!
crates/ec-vp9/src/loopfilter.rsmodified+425 -121
@@ -616,20 +616,22 @@ impl LfGrids {
616616 /// column pass takes NO width bound (vp9_loopfilter.c:1394 iterates all
617617 /// `MI_BLOCK_SIZE >> 1` column groups and `filter_selectively_vert_row2`
618618 /// writes whole 8-column groups into the padded plane).
619- pub(crate) fn filter_frame(
619+ /// `vp9_filter_block_plane_ss00` (vp9_loopfilter.c:1239): filter one
620+ /// full-resolution plane (luma, or a 4:4:4 chroma plane) from the luma
621+ /// masks. Under 4:4:4 the chroma plane reuses the luma masks AND the luma
622+ /// filter levels (`lfm->lfl_y`), so this same routine serves it with the
623+ /// chroma plane's stride.
624+ fn filter_ss00_plane(
620625 &self,
621- y: &mut [Sample],
626+ data: &mut [Sample],
622627 stride: usize,
623- u: &mut [Sample],
624- v: &mut [Sample],
625- uv_stride: usize,
626- y_w: usize,
627- y_h: usize,
628- uv_h: usize,
628+ w: usize,
629+ h: usize,
629630 sharpness: u8,
630631 bd: u8,
632+ trace: bool,
631633 ) {
632- let lftrace = lftrace_on();
634+ let lftrace = trace && lftrace_on();
633635 // Luma masks, ported from vp9_setup_mask/build_masks and
634636 // vp9_adjust_mask (bit i = MI row i>>3, MI col i&7; each bit
635637 // covers an 8x8 cell). Edges are emitted from the masks.
@@ -642,12 +644,12 @@ impl LfGrids {
642644 let (left, above, int4) = self.luma_masks(sbr, sbc);
643645 for row in 0..8usize {
644646 let yy = sbr * 8 + row * 8;
645- if yy >= y_h {
647+ if yy >= h {
646648 break;
647649 }
648650 for col in 0..8usize {
649651 let xx = sbc * 8 + col * 8;
650- if xx >= y_w {
652+ if xx >= w {
651653 break;
652654 }
653655 let bit = 1u64 << (row * 8 + col);
@@ -666,11 +668,11 @@ impl LfGrids {
666668 } else {
667669 0
668670 };
669- if t != 0 && xx >= 4 && xx + 4 <= y_w {
670- if t == 16 && (xx < 8 || xx + 8 > y_w) {
671+ if t != 0 && xx >= 4 && xx + 4 <= w {
672+ if t == 16 && (xx < 8 || xx + 8 > w) {
671673 t = 8;
672674 }
673- lpf_edge(y, yy * stride + xx, stride, 1, t, bl, li, th, 8, bd);
675+ lpf_edge(data, yy * stride + xx, stride, 1, t, bl, li, th, 8, bd);
674676 if lftrace {
675677 let kind = match t {
676678 16 => "V16",
@@ -680,8 +682,8 @@ impl LfGrids {
680682 eprintln!("{kind} {} {}", xx, yy);
681683 }
682684 }
683- if int4 & bit != 0 && xx + 8 <= y_w && yy + 8 <= y_h {
684- lpf_edge(y, yy * stride + xx + 4, stride, 1, 4, bl, li, th, 8, bd);
685+ if int4 & bit != 0 && xx + 8 <= w && yy + 8 <= h {
686+ lpf_edge(data, yy * stride + xx + 4, stride, 1, 4, bl, li, th, 8, bd);
685687 if lftrace {
686688 eprintln!("V4I {} {}", xx + 4, yy);
687689 }
@@ -690,12 +692,12 @@ impl LfGrids {
690692 }
691693 for row in 0..8usize {
692694 let yy = sbr * 8 + row * 8;
693- if yy >= y_h {
695+ if yy >= h {
694696 break;
695697 }
696698 for col in 0..8usize {
697699 let xx = sbc * 8 + col * 8;
698- if xx >= y_w {
700+ if xx >= w {
699701 break;
700702 }
701703 let bit = 1u64 << (row * 8 + col);
@@ -715,11 +717,11 @@ impl LfGrids {
715717 } else {
716718 0
717719 };
718- if t != 0 && yy >= 4 && yy + 4 <= y_h && xx + 8 <= y_w {
719- if t == 16 && (yy < 8 || yy + 8 > y_h) {
720+ if t != 0 && yy >= 4 && yy + 4 <= h && xx + 8 <= w {
721+ if t == 16 && (yy < 8 || yy + 8 > h) {
720722 t = 8;
721723 }
722- lpf_edge(y, yy * stride + xx, stride, 0, t, bl, li, th, 8, bd);
724+ lpf_edge(data, yy * stride + xx, stride, 0, t, bl, li, th, 8, bd);
723725 if lftrace {
724726 let kind = match t {
725727 16 => "H16",
@@ -736,8 +738,19 @@ impl LfGrids {
736738 // (vp9_loopfilter.c:1286-1311). `skip_border_4x4_r`
737739 // exists only in the 4:2:0-chroma path (ss11,
738740 // vp9_loopfilter.c:1382) and in non420 under `ss_y`.
739- if int4 & bit != 0 && yy + 8 <= y_h && xx + 8 <= y_w {
740- lpf_edge(y, (yy + 4) * stride + xx, stride, 0, 4, bl, li, th, 8, bd);
741+ if int4 & bit != 0 && yy + 8 <= h && xx + 8 <= w {
742+ lpf_edge(
743+ data,
744+ (yy + 4) * stride + xx,
745+ stride,
746+ 0,
747+ 4,
748+ bl,
749+ li,
750+ th,
751+ 8,
752+ bd,
753+ );
741754 if lftrace {
742755 eprintln!("H4I {} {}", xx, yy + 4);
743756 }
@@ -746,6 +759,18 @@ impl LfGrids {
746759 }
747760 }
748761 }
762+ }
763+
764+ /// `vp9_filter_block_plane_ss11` (vp9_loopfilter.c:1324): filter one
765+ /// 4:2:0 chroma plane.
766+ fn filter_ss11_plane(
767+ &self,
768+ data: &mut [Sample],
769+ uv_stride: usize,
770+ uv_h: usize,
771+ sharpness: u8,
772+ bd: u8,
773+ ) {
749774 // Chroma 4:2:0 (vp9_filter_block_plane_ss11). MI is 4 chroma
750775 // pixels. The UV masks are built per 64x64 superblock exactly as
751776 // vp9_setup_mask/build_masks do — one build_masks per block, only
@@ -775,7 +800,7 @@ impl LfGrids {
775800 if (bor as usize, boc as usize) != (r, c) || self.level8[cell] == 0 {
776801 continue;
777802 }
778- let uv_tx = get_uv_tx_size(self.otx[cell] as usize, bsize as usize);
803+ let uv_tx = get_uv_tx_size(self.otx[cell] as usize, bsize as usize, 1, 1);
779804 let shift = (((r & 7) >> 1) << 2) + ((c & 7) >> 1);
780805 let b = bsize as usize;
781806 left[uv_tx] |= UV_LEFT_PRED[b] << shift;
@@ -845,65 +870,49 @@ impl LfGrids {
845870 *m &= 0xeeee;
846871 }
847872 }
848- for data in [&mut *u, &mut *v] {
849- // libvpx runs the whole vertical pass before the
850- // horizontal one; they overlap, so order matters.
851- for rg in 0..4usize {
852- // vp9_filter_block_plane_ss11 iterates MI rows
853- // `r += 4` while `mi_row + r < mi_rows`
854- // (vp9_loopfilter.c:1400), so the row GROUP is
855- // processed whenever its first MI row is inside the
856- // frame — even when its 8 chroma rows overhang the
857- // visible height into the padded buffer. Gating on
858- // `y8 + 8 <= uv_h` instead skipped the whole group and
859- // lost the visible bottom rows (the 1080p case:
860- // mi_rows 135, last group = chroma rows 536..543,
861- // 536..539 visible).
862- if sbr + 4 * (rg >> 1) >= self.mi_rows {
873+ // libvpx runs the whole vertical pass before the
874+ // horizontal one; they overlap, so order matters.
875+ for rg in 0..4usize {
876+ // vp9_filter_block_plane_ss11 iterates MI rows
877+ // `r += 4` while `mi_row + r < mi_rows`
878+ // (vp9_loopfilter.c:1400), so the row GROUP is
879+ // processed whenever its first MI row is inside the
880+ // frame — even when its 8 chroma rows overhang the
881+ // visible height into the padded buffer. Gating on
882+ // `y8 + 8 <= uv_h` instead skipped the whole group and
883+ // lost the visible bottom rows (the 1080p case:
884+ // mi_rows 135, last group = chroma rows 536..543,
885+ // 536..539 visible).
886+ if sbr + 4 * (rg >> 1) >= self.mi_rows {
887+ continue;
888+ }
889+ let y8 = sbr * 4 + 8 * rg;
890+ for cg in 0..4usize {
891+ let x8 = sbc * 4 + 8 * cg;
892+ let bit = 1u16 << (rg * 4 + cg);
893+ let lvl = self.level_at(sbr + 2 * rg, sbc + 2 * cg);
894+ if lvl == 0 {
863895 continue;
864896 }
865- let y8 = sbr * 4 + 8 * rg;
866- for cg in 0..4usize {
867- let x8 = sbc * 4 + 8 * cg;
868- let bit = 1u16 << (rg * 4 + cg);
869- let lvl = self.level_at(sbr + 2 * rg, sbc + 2 * cg);
870- if lvl == 0 {
871- continue;
872- }
873- let (bl, li, th) = limits(lvl, sharpness);
874- if x8 >= 4 {
875- let taps = if left[TX_16X16] & bit != 0 {
876- 16
877- } else if left[TX_8X8] & bit != 0 {
878- 8
879- } else if left[TX_4X4] & bit != 0 {
880- 4
881- } else {
882- 0
883- };
884- if taps != 0 {
885- let t = if taps == 16 && x8 < 8 { 8 } else { taps };
886- lpf_edge(
887- data,
888- y8 * uv_stride + x8,
889- uv_stride,
890- 1,
891- t,
892- bl,
893- li,
894- th,
895- 8,
896- bd,
897- );
898- }
899- }
900- if int4 & bit != 0 {
897+ let (bl, li, th) = limits(lvl, sharpness);
898+ if x8 >= 4 {
899+ let taps = if left[TX_16X16] & bit != 0 {
900+ 16
901+ } else if left[TX_8X8] & bit != 0 {
902+ 8
903+ } else if left[TX_4X4] & bit != 0 {
904+ 4
905+ } else {
906+ 0
907+ };
908+ if taps != 0 {
909+ let t = if taps == 16 && x8 < 8 { 8 } else { taps };
901910 lpf_edge(
902911 data,
903- y8 * uv_stride + x8 + 4,
912+ y8 * uv_stride + x8,
904913 uv_stride,
905914 1,
906- 4,
915+ t,
907916 bl,
908917 li,
909918 th,
@@ -912,60 +921,294 @@ impl LfGrids {
912921 );
913922 }
914923 }
924+ if int4 & bit != 0 {
925+ lpf_edge(
926+ data,
927+ y8 * uv_stride + x8 + 4,
928+ uv_stride,
929+ 1,
930+ 4,
931+ bl,
932+ li,
933+ th,
934+ 8,
935+ bd,
936+ );
937+ }
915938 }
916- for rg in 0..4usize {
917- // Horizontal pass: `r += 2` while `mi_row + r <
918- // mi_rows` (vp9_loopfilter.c:1431) — one row group per
919- // iteration, same MI-row-gated boundary as the
920- // vertical pass above.
921- if sbr + 2 * rg >= self.mi_rows {
939+ }
940+ for rg in 0..4usize {
941+ // Horizontal pass: `r += 2` while `mi_row + r <
942+ // mi_rows` (vp9_loopfilter.c:1431) — one row group per
943+ // iteration, same MI-row-gated boundary as the
944+ // vertical pass above.
945+ if sbr + 2 * rg >= self.mi_rows {
946+ continue;
947+ }
948+ let y8 = sbr * 4 + 8 * rg;
949+ for cg in 0..4usize {
950+ let x8 = sbc * 4 + 8 * cg;
951+ let bit = 1u16 << (rg * 4 + cg);
952+ let lvl = self.level_at(sbr + 2 * rg, sbc + 2 * cg);
953+ if lvl == 0 {
922954 continue;
923955 }
924- let y8 = sbr * 4 + 8 * rg;
925- for cg in 0..4usize {
926- let x8 = sbc * 4 + 8 * cg;
927- let bit = 1u16 << (rg * 4 + cg);
928- let lvl = self.level_at(sbr + 2 * rg, sbc + 2 * cg);
929- if lvl == 0 {
930- continue;
931- }
932- let (bl, li, th) = limits(lvl, sharpness);
933- if y8 >= 4 && y8 + 4 <= uv_h {
934- let taps = if above[TX_16X16] & bit != 0 {
935- 16
936- } else if above[TX_8X8] & bit != 0 {
956+ let (bl, li, th) = limits(lvl, sharpness);
957+ if y8 >= 4 && y8 + 4 <= uv_h {
958+ let taps = if above[TX_16X16] & bit != 0 {
959+ 16
960+ } else if above[TX_8X8] & bit != 0 {
961+ 8
962+ } else if above[TX_4X4] & bit != 0 {
963+ 4
964+ } else {
965+ 0
966+ };
967+ if taps != 0 {
968+ let t = if taps == 16 && (y8 < 8 || y8 + 8 > uv_h) {
937969 8
938- } else if above[TX_4X4] & bit != 0 {
939- 4
940970 } else {
941- 0
971+ taps
942972 };
943- if taps != 0 {
944- let t = if taps == 16 && (y8 < 8 || y8 + 8 > uv_h) {
945- 8
973+ lpf_edge(
974+ data,
975+ y8 * uv_stride + x8,
976+ uv_stride,
977+ 0,
978+ t,
979+ bl,
980+ li,
981+ th,
982+ 8,
983+ bd,
984+ );
985+ }
986+ }
987+ if int4 & bit != 0 && sbr + 2 * rg != self.mi_rows - 1 && y8 + 8 <= uv_h {
988+ lpf_edge(
989+ data,
990+ (y8 + 4) * uv_stride + x8,
991+ uv_stride,
992+ 0,
993+ 4,
994+ bl,
995+ li,
996+ th,
997+ 8,
998+ bd,
999+ );
1000+ }
1001+ }
1002+ }
1003+ }
1004+ }
1005+ }
1006+
1007+ /// `vp9_filter_block_plane_non420` (vp9_loopfilter.c:1078): the slow
1008+ /// path for a chroma plane subsampled in exactly ONE direction (4:2:2 /
1009+ /// 4:4:0), where the luma masks cannot be reused. Masks and per-MI filter
1010+ /// levels are built inline; the vertical pass runs one band of 8 chroma
1011+ /// rows per MI row step (`row_step = 1 << ss_y`).
1012+ #[allow(clippy::too_many_arguments)]
1013+ fn filter_non420_plane(
1014+ &self,
1015+ data: &mut [Sample],
1016+ stride: usize,
1017+ uv_w: usize,
1018+ uv_h: usize,
1019+ ss_x: usize,
1020+ ss_y: usize,
1021+ sharpness: u8,
1022+ bd: u8,
1023+ ) {
1024+ let row_step = 1usize << ss_y;
1025+ let col_step = 1usize << ss_x;
1026+ let groups = 8usize >> ss_x;
1027+ let sb_rows = (self.mi_rows + 7) / 8;
1028+ let sb_cols = (self.mi_cols + 7) / 8;
1029+ for sbr_i in 0..sb_rows {
1030+ let sbr = sbr_i * 8;
1031+ for sbc_i in 0..sb_cols {
1032+ let sbc = sbc_i * 8;
1033+ let band_x = (sbc * 8) >> ss_x;
1034+ // mask_*[r] are the horizontal-pass masks; `lfl[r][i]` the
1035+ // per-MI filter level at MI row offset r and column group i.
1036+ let mut mask_16 = [0u32; 8];
1037+ let mut mask_8 = [0u32; 8];
1038+ let mut mask_4 = [0u32; 8];
1039+ let mut mask_4i = [0u32; 8];
1040+ let mut lfl = [[0u8; 8]; 8];
1041+ // Vertical pass: one 8-chroma-row band per MI row step.
1042+ let mut r = 0usize;
1043+ while r < 8 && sbr + r < self.mi_rows {
1044+ let mut c16 = 0u32;
1045+ let mut c8 = 0u32;
1046+ let mut c4 = 0u32;
1047+ let mut c = 0usize;
1048+ while c < 8 && sbc + c < self.mi_cols {
1049+ let cell = (sbr + r) * self.mi_cols + sbc + c;
1050+ let b = self.blk[cell].0 as usize;
1051+ let skip_this = self.skip_inter[cell];
1052+ let block_edge_left = if NUM_4X4_BLOCKS_WIDE_LOOKUP[b] > 1 {
1053+ (c & (NUM_8X8_BLOCKS_WIDE_LOOKUP[b] as usize - 1)) == 0
1054+ } else {
1055+ true
1056+ };
1057+ let skip_this_c = skip_this && !block_edge_left;
1058+ let block_edge_above = if NUM_4X4_BLOCKS_HIGH_LOOKUP[b] > 1 {
1059+ (r & (NUM_8X8_BLOCKS_HIGH_LOOKUP[b] as usize - 1)) == 0
1060+ } else {
1061+ true
1062+ };
1063+ let skip_this_r = skip_this && !block_edge_above;
1064+ let tx = get_uv_tx_size(self.otx[cell] as usize, b, ss_x, ss_y);
1065+ let skip_border_c = ss_x == 1 && sbc + c == self.mi_cols - 1;
1066+ let skip_border_r = ss_y == 1 && sbr + r == self.mi_rows - 1;
1067+ let i = c >> ss_x;
1068+ let bit = 1u32 << i;
1069+ let lvl = self.level8[cell];
1070+ lfl[r][i] = lvl;
1071+ if lvl != 0 {
1072+ if tx == TX_32X32 {
1073+ if !skip_this_c && (i & 3) == 0 {
1074+ if !skip_border_c {
1075+ c16 |= bit
9461076 } else {
947- taps
948- };
949- lpf_edge(
950- data,
951- y8 * uv_stride + x8,
952- uv_stride,
953- 0,
954- t,
955- bl,
956- li,
957- th,
958- 8,
959- bd,
960- );
1077+ c8 |= bit
1078+ }
1079+ }
1080+ if !skip_this_r && ((r >> ss_y) & 3) == 0 {
1081+ if !skip_border_r {
1082+ mask_16[r] |= bit
1083+ } else {
1084+ mask_8[r] |= bit
1085+ }
1086+ }
1087+ } else if tx == TX_16X16 {
1088+ if !skip_this_c && (i & 1) == 0 {
1089+ if !skip_border_c {
1090+ c16 |= bit
1091+ } else {
1092+ c8 |= bit
1093+ }
1094+ }
1095+ if !skip_this_r && ((r >> ss_y) & 1) == 0 {
1096+ if !skip_border_r {
1097+ mask_16[r] |= bit
1098+ } else {
1099+ mask_8[r] |= bit
1100+ }
1101+ }
1102+ } else {
1103+ if !skip_this_c {
1104+ if tx == TX_8X8 || (i & 3) == 0 {
1105+ c8 |= bit;
1106+ } else {
1107+ c4 |= bit;
1108+ }
1109+ }
1110+ if !skip_this_r {
1111+ if tx == TX_8X8 || ((r >> ss_y) & 3) == 0 {
1112+ mask_8[r] |= bit;
1113+ } else {
1114+ mask_4[r] |= bit;
1115+ }
1116+ }
1117+ if !skip_this && tx < TX_8X8 && !skip_border_c {
1118+ mask_4i[r] |= bit;
9611119 }
9621120 }
963- if int4 & bit != 0 && sbr + 2 * rg != self.mi_rows - 1 && y8 + 8 <= uv_h
964- {
1121+ }
1122+ c += col_step;
1123+ }
1124+ // Vertical edges: `filter_selectively_vert`. Leftmost
1125+ // frame superblock clears the block-edge masks on the
1126+ // first column group.
1127+ let border = if sbc == 0 { !1u32 } else { !0u32 };
1128+ let band_y = ((sbr + r) * 8) >> ss_y;
1129+ let union = (c16 & border) | (c8 & border) | (c4 & border) | mask_4i[r];
1130+ for gi in 0..groups {
1131+ let bit = 1u32 << gi;
1132+ if union & bit == 0 {
1133+ continue;
1134+ }
1135+ let (bl, li, th) = limits(lfl[r][gi], sharpness);
1136+ let x = band_x + 8 * gi;
1137+ let taps = if (c16 & border) & bit != 0 {
1138+ 16
1139+ } else if (c8 & border) & bit != 0 {
1140+ 8
1141+ } else if (c4 & border) & bit != 0 {
1142+ 4
1143+ } else {
1144+ 0
1145+ };
1146+ if taps != 0 {
1147+ lpf_edge(
1148+ data,
1149+ band_y * stride + x,
1150+ stride,
1151+ 1,
1152+ taps,
1153+ bl,
1154+ li,
1155+ th,
1156+ 8,
1157+ bd,
1158+ );
1159+ }
1160+ if mask_4i[r] & bit != 0 {
1161+ lpf_edge(
1162+ data,
1163+ band_y * stride + x + 4,
1164+ stride,
1165+ 1,
1166+ 4,
1167+ bl,
1168+ li,
1169+ th,
1170+ 8,
1171+ bd,
1172+ );
1173+ }
1174+ }
1175+ r += row_step;
1176+ }
1177+ // Horizontal pass: `filter_selectively_horiz`, one band per
1178+ // MI row step.
1179+ let mut r = 0usize;
1180+ while r < 8 && sbr + r < self.mi_rows {
1181+ let skip_border_r = ss_y == 1 && sbr + r == self.mi_rows - 1;
1182+ let int_r = if skip_border_r { 0 } else { mask_4i[r] };
1183+ let (m16, m8, m4) = if sbr + r == 0 {
1184+ (0, 0, 0)
1185+ } else {
1186+ (mask_16[r], mask_8[r], mask_4[r])
1187+ };
1188+ let band_y = ((sbr + r) * 8) >> ss_y;
1189+ let union = m16 | m8 | m4 | int_r;
1190+ for gi in 0..groups {
1191+ let bit = 1u32 << gi;
1192+ if union & bit == 0 {
1193+ continue;
1194+ }
1195+ let (bl, li, th) = limits(lfl[r][gi], sharpness);
1196+ let x = band_x + 8 * gi;
1197+ let y = band_y;
1198+ // `filter_selectively_horiz` (vpx_dsp/loopfilter.c):
1199+ // a 16-tap block edge swallows the internal 4x4 edge;
1200+ // the 8- and 4-tap branches emit it separately; a bit
1201+ // whose only mask is the internal one takes the else
1202+ // branch (the internal edge alone).
1203+ if m16 & bit != 0 {
1204+ lpf_edge(data, y * stride + x, stride, 0, 16, bl, li, th, 8, bd);
1205+ } else if m8 & bit != 0 {
1206+ lpf_edge(data, y * stride + x, stride, 0, 8, bl, li, th, 8, bd);
1207+ if int_r & bit != 0 {
9651208 lpf_edge(
9661209 data,
967- (y8 + 4) * uv_stride + x8,
968- uv_stride,
1210+ (y + 4) * stride + x,
1211+ stride,
9691212 0,
9701213 4,
9711214 bl,
@@ -975,10 +1218,71 @@ impl LfGrids {
9751218 bd,
9761219 );
9771220 }
1221+ } else if m4 & bit != 0 {
1222+ lpf_edge(data, y * stride + x, stride, 0, 4, bl, li, th, 8, bd);
1223+ if int_r & bit != 0 {
1224+ lpf_edge(
1225+ data,
1226+ (y + 4) * stride + x,
1227+ stride,
1228+ 0,
1229+ 4,
1230+ bl,
1231+ li,
1232+ th,
1233+ 8,
1234+ bd,
1235+ );
1236+ }
1237+ } else {
1238+ lpf_edge(data, (y + 4) * stride + x, stride, 0, 4, bl, li, th, 8, bd);
9781239 }
9791240 }
1241+ r += row_step;
9801242 }
1243+ let _ = (uv_w, uv_h);
1244+ }
1245+ }
1246+ }
1247+
1248+ /// Filter the frame's three planes in place. `y_w/h` are the coded
1249+ /// (aligned) luma plane sizes; `uv_w/h` the chroma plane sizes; `ss_x`/
1250+ /// `ss_y` the chroma subsampling. The dispatch mirrors
1251+ /// `vp9_loop_filter_frame` (vp9_loopfilter.c:1430): luma always takes the
1252+ /// ss00 path; chroma takes ss00 for 4:4:4, ss11 for 4:2:0, and the
1253+ /// non420 slow path otherwise (4:2:2 / 4:4:0).
1254+ #[allow(clippy::too_many_arguments)]
1255+ pub(crate) fn filter_frame(
1256+ &self,
1257+ y: &mut [Sample],
1258+ stride: usize,
1259+ u: &mut [Sample],
1260+ v: &mut [Sample],
1261+ uv_stride: usize,
1262+ y_w: usize,
1263+ y_h: usize,
1264+ uv_w: usize,
1265+ uv_h: usize,
1266+ ss_x: usize,
1267+ ss_y: usize,
1268+ sharpness: u8,
1269+ bd: u8,
1270+ ) {
1271+ self.filter_ss00_plane(y, stride, y_w, y_h, sharpness, bd, true);
1272+ if ss_x == 0 && ss_y == 0 {
1273+ for data in [&mut *u, &mut *v] {
1274+ self.filter_ss00_plane(data, uv_stride, uv_w, uv_h, sharpness, bd, false);
9811275 }
1276+ return;
1277+ }
1278+ if ss_x == 1 && ss_y == 1 {
1279+ for data in [&mut *u, &mut *v] {
1280+ self.filter_ss11_plane(data, uv_stride, uv_h, sharpness, bd);
1281+ }
1282+ return;
1283+ }
1284+ for data in [&mut *u, &mut *v] {
1285+ self.filter_non420_plane(data, uv_stride, uv_w, uv_h, ss_x, ss_y, sharpness, bd);
9821286 }
9831287 }
9841288 }
@@ -1005,7 +1309,7 @@ mod tests {
10051309 fn run(g: &LfGrids, y: &mut [Sample]) {
10061310 let mut u = vec![128u16; 32 * 32];
10071311 let mut v = vec![128u16; 32 * 32];
1008- g.filter_frame(y, 64, &mut u, &mut v, 32, 64, 64, 32, 0, 8);
1312+ g.filter_frame(y, 64, &mut u, &mut v, 32, 64, 64, 32, 32, 1, 1, 0, 8);
10091313 }
10101314
10111315 /// The luma internal-4x4 horizontal edge fires on the frame's LAST MI row.
crates/ec-vp9/src/mc.rsmodified+59 -18
@@ -455,13 +455,35 @@ fn inter_predictor(
455455 }
456456 if subpel_y == 0 {
457457 convolve_horiz(
458- buf, stride, origin, dst, dst_stride, f, subpel_x, SUBPEL_SHIFTS, w, h, avg, bd,
458+ buf,
459+ stride,
460+ origin,
461+ dst,
462+ dst_stride,
463+ f,
464+ subpel_x,
465+ SUBPEL_SHIFTS,
466+ w,
467+ h,
468+ avg,
469+ bd,
459470 );
460471 return;
461472 }
462473 if subpel_x == 0 {
463474 convolve_vert(
464- buf, stride, origin, dst, dst_stride, f, subpel_y, SUBPEL_SHIFTS, w, h, avg, bd,
475+ buf,
476+ stride,
477+ origin,
478+ dst,
479+ dst_stride,
480+ f,
481+ subpel_y,
482+ SUBPEL_SHIFTS,
483+ w,
484+ h,
485+ avg,
486+ bd,
465487 );
466488 return;
467489 }
@@ -744,24 +766,43 @@ pub(crate) fn build_inter_predictors(
744766 );
745767 }
746768
747-/// `average_split_mvs` (`vp9/common/vp9_reconinter.c:113`), luma (`ss_idx 0`):
748-/// the sub-block's own MV.
749-pub(crate) fn split_mv(bmi: &[[(i32, i32); 2]; 4], block: usize, ref_idx: usize) -> (i32, i32) {
750- bmi[block][ref_idx]
751-}
752-
753-/// `average_split_mvs` for 4:2:0 chroma (`ss_idx 3`): `mi_mv_pred_q4`, the
754-/// q4-rounded average over the four sub-blocks.
769+/// `average_split_mvs` (`vp9/common/vp9_reconinter.c:113`): the per-sub-block
770+/// MV of a sub-8x8 inter block, selected by the plane's subsampling.
771+///
772+/// `ss_idx` 0 (luma and 4:4:4 chroma): the sub-block's own MV; 1 (4:4:0): the
773+/// q2-rounded average of the vertical pair (`block`, `block + 2`); 2 (4:2:2):
774+/// the horizontal pair (`block`, `block + 1`); 3 (4:2:0): the q4-rounded
775+/// average of all four.
755776 ///
756-/// `round_mv_comp_q4(v) = (v < 0 ? v - 2 : v + 2) / 4` — C's truncating
777+/// `round_mv_comp_qN(v) = (v < 0 ? v - N/2 : v + N/2) / N` — C's truncating
757778 /// division, which Rust's `/` on integers reproduces.
758-pub(crate) fn average_split_mvs_chroma(bmi: &[[(i32, i32); 2]; 4], ref_idx: usize) -> (i32, i32) {
779+pub(crate) fn average_split_mvs(
780+ bmi: &[[(i32, i32); 2]; 4],
781+ ref_idx: usize,
782+ block: usize,
783+ ss_x: usize,
784+ ss_y: usize,
785+) -> (i32, i32) {
786+ let q2 = |v: i32| if v < 0 { (v - 1) / 2 } else { (v + 1) / 2 };
759787 let q4 = |v: i32| if v < 0 { (v - 2) / 4 } else { (v + 2) / 4 };
760- let mut row = 0;
761- let mut col = 0;
762- for b in bmi.iter() {
763- row += b[ref_idx].0;
764- col += b[ref_idx].1;
788+ match (ss_x, ss_y) {
789+ (0, 0) => bmi[block][ref_idx],
790+ (0, 1) => {
791+ let (a, b) = (bmi[block][ref_idx], bmi[block + 2][ref_idx]);
792+ (q2(a.0 + b.0), q2(a.1 + b.1))
793+ }
794+ (1, 0) => {
795+ let (a, b) = (bmi[block][ref_idx], bmi[block + 1][ref_idx]);
796+ (q2(a.0 + b.0), q2(a.1 + b.1))
797+ }
798+ _ => {
799+ let mut row = 0;
800+ let mut col = 0;
801+ for b in bmi.iter() {
802+ row += b[ref_idx].0;
803+ col += b[ref_idx].1;
804+ }
805+ (q4(row), q4(col))
806+ }
765807 }
766- (q4(row), q4(col))
767808 }
crates/ec-vp9/src/tables/gen_tables.rsmodified+13 -2
@@ -588,6 +588,17 @@ pub(crate) const SS_SIZE_LOOKUP: [u8; 52] = [
588588 0, 255, 255, 255, 1, 0, 255, 255, 2, 255, 0, 255, 3, 2, 1, 0, 4, 3, 255, 1, 5, 255, 3, 2, 6, 5,
589589 4, 3, 7, 6, 255, 4, 8, 255, 6, 5, 9, 8, 7, 6, 10, 9, 255, 7, 11, 255, 9, 8, 12, 11, 10, 9,
590590 ];
591+/// `uv_txsize_lookup[bsize][tx_size][ss_x][ss_y]` (common_data.c), flattened
592+/// to `((bsize * 4 + tx_size) * 2 + ss_x) * 2 + ss_y`.
593+pub(crate) const UV_TXSIZE_LOOKUP: [u8; 208] = [
594+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
595+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
596+ 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1,
597+ 0, 0, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1,
598+ 0, 0, 0, 0, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 2, 2, 2,
599+ 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 2, 3, 2,
600+ 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
601+];
591602 pub(crate) const SUBSIZE_LOOKUP: [u8; 52] = [
592603 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 255, 255, 255, 2, 255, 255, 5, 255, 255, 8, 255, 255,
593604 11, 255, 255, 255, 1, 255, 255, 4, 255, 255, 7, 255, 255, 10, 255, 255, 255, 0, 255, 255, 3,
@@ -1451,8 +1462,8 @@ pub(crate) const MV_CLASS_TREE: [i8; 20] = [
14511462 pub(crate) const MV_FP_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];
14521463 /// `default_if_y_probs`, `[BLOCK_SIZE_GROUPS][INTRA_MODES - 1]`.
14531464 pub(crate) const DEFAULT_Y_MODE_PROBS: [u8; 36] = [
1454- 65, 32, 18, 144, 162, 194, 41, 51, 98, 132, 68, 18, 165, 217, 196, 45, 40, 78, 173, 80, 19, 176,
1455- 240, 193, 64, 35, 46, 221, 135, 38, 194, 248, 121, 96, 85, 29,
1465+ 65, 32, 18, 144, 162, 194, 41, 51, 98, 132, 68, 18, 165, 217, 196, 45, 40, 78, 173, 80, 19,
1466+ 176, 240, 193, 64, 35, 46, 221, 135, 38, 194, 248, 121, 96, 85, 29,
14561467 ];
14571468 /// `default_inter_mode_probs`, `[INTER_MODE_CONTEXTS][INTER_MODES - 1]`.
14581469 pub(crate) const DEFAULT_INTER_MODE_PROBS: [u8; 21] = [
crates/ec-vp9/src/tables/mod.rsmodified+14 -20
@@ -51,11 +51,7 @@ pub(crate) const SEGMENT_TREE: [i8; 14] = [2, 4, 6, 8, 10, 12, 0, -1, -2, -3, -4
5151 /// blockd.h `get_y_mode`.
5252 #[inline]
5353 pub(crate) fn get_y_mode(sb_type: usize, bmi: [u8; 4], mode: u8, block: usize) -> u8 {
54- if sb_type < 3 {
55- bmi[block]
56- } else {
57- mode
58- }
54+ if sb_type < 3 { bmi[block] } else { mode }
5955 }
6056
6157 /// vp9_blockd.c `vp9_above_block_mode` (keyframes: neighbours are intra).
@@ -107,20 +103,22 @@ pub(crate) fn kf_uv_probs(mode: u8) -> [u8; 9] {
107103 out
108104 }
109105
110-/// common_data.c `ss_size_lookup[bsize][1][1]`.
106+/// `BLOCK_INVALID`, the `ss_size_lookup` sentinel for a subsampled block size
107+/// that does not exist (`BLOCK_8X4` under 4:4:0, `BLOCK_4X8` under 4:2:2).
108+pub(crate) const BLOCK_INVALID: usize = 255;
109+
110+/// common_data.c `ss_size_lookup[bsize][ss_x][ss_y]`: the luma block size
111+/// `bsize` expressed in a plane subsampled by `ss_x`/`ss_y`.
111112 #[inline]
112-pub(crate) fn ss_size(bsize: usize) -> usize {
113- SS_SIZE_LOOKUP[bsize * 4 + 3] as usize
113+pub(crate) fn ss_size(bsize: usize, ss_x: usize, ss_y: usize) -> usize {
114+ SS_SIZE_LOOKUP[bsize * 4 + ss_x * 2 + ss_y] as usize
114115 }
115116
116-/// blockd.h `get_uv_tx_size` for 4:2:0.
117+/// blockd.h `get_uv_tx_size`:
118+/// `uv_txsize_lookup[mi->sb_type][mi->tx_size][ss_x][ss_y]`.
117119 #[inline]
118-pub(crate) fn get_uv_tx_size(mi_tx_size: usize, sb_type: usize) -> usize {
119- if sb_type < 3 {
120- TX_4X4
121- } else {
122- mi_tx_size.min(MAX_TXSIZE_LOOKUP[ss_size(sb_type)] as usize)
123- }
120+pub(crate) fn get_uv_tx_size(mi_tx_size: usize, sb_type: usize, ss_x: usize, ss_y: usize) -> usize {
121+ UV_TXSIZE_LOOKUP[((sb_type * 4 + mi_tx_size) * 2 + ss_x) * 2 + ss_y] as usize
124122 }
125123
126124 pub(crate) fn corrupt(what: impl Into<String>) -> Error {
@@ -128,9 +126,5 @@ pub(crate) fn corrupt(what: impl Into<String>) -> Error {
128126 }
129127
130128 pub(crate) fn ensure(cond: bool, what: &str) -> Result<()> {
131- if cond {
132- Ok(())
133- } else {
134- Err(corrupt(what))
135- }
129+ if cond { Ok(()) } else { Err(corrupt(what)) }
136130 }
crates/ec-vp9/src/tokens.rsmodified+14 -7
@@ -5,13 +5,20 @@ use crate::bool::BoolDecoder;
55 use crate::tables::*;
66
77 /// `vp9_cat6_prob_high12 + 2` (`vp9/common/vp9_entropy.c`): the CAT6 token's
8-/// 16 extra bits at bit depth 10 (and the middle 16 of the 12-bit table).
9-/// The bitstream is the ONLY depth-dependent syntax element: `decode_coefs`
10-/// picks `vp9_cat6_prob`/14 bits at 8-bit and this/16 bits at 10- and 12-bit.
8+/// 16 extra bits at bit depth 10.
119 const CAT6_PROB_HIGH: [u8; 16] = [
1210 255, 255, 254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129,
1311 ];
1412
13+/// `vp9_cat6_prob_high12` (`vp9/common/vp9_entropy.c`): the CAT6 token's 18
14+/// extra bits at bit depth 12 (bit depth 10 uses `+ 2`, the 16 middle
15+/// entries). The CAT6 token is the ONLY depth-dependent syntax element:
16+/// `decode_coefs` picks `vp9_cat6_prob`/14 bits at 8-bit, `+ 2`/16 at 10-bit,
17+/// and this/18 at 12-bit.
18+const CAT6_PROB_HIGH12: [u8; 18] = [
19+ 255, 255, 255, 255, 254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129,
20+];
21+
1522 /// One plane's entropy contexts: a has-coefficient flag per 4x4 column
1623 /// above the current superblock row and per 4x4 row to the left.
1724 pub(crate) struct PlaneContexts {
@@ -121,10 +128,10 @@ pub(crate) fn decode_coefs(
121128 if r.read_bool(p[5]) {
122129 if r.read_bool(p[7]) {
123130 v = 67
124- + if bd == 8 {
125- read_coeff(r, &CAT6_PROB, 14)
126- } else {
127- read_coeff(r, &CAT6_PROB_HIGH, 16)
131+ + match bd {
132+ 8 => read_coeff(r, &CAT6_PROB, 14),
133+ 10 => read_coeff(r, &CAT6_PROB_HIGH, 16),
134+ _ => read_coeff(r, &CAT6_PROB_HIGH12, 18),
128135 };
129136 } else {
130137 v = 35 + read_coeff(r, &CAT5_PROB, 5);
crates/ec-vp9/tests/data/vp9-profile1-422.ivfadded+0 -0

Binary file changed

crates/ec-vp9/tests/data/vp9-profile1-440.ivfadded+0 -0

Binary file changed

crates/ec-vp9/tests/data/vp9-profile1-444.ivfadded+0 -0

Binary file changed

crates/ec-vp9/tests/data/vp9-profile2-420-12bit.ivfadded+0 -0

Binary file changed

crates/ec-vp9/tests/data/vp9-profile3-422-10bit.ivfadded+0 -0

Binary file changed

crates/ec-vp9/tests/data/vp9-profile3-440-10bit.ivfadded+0 -0

Binary file changed

crates/ec-vp9/tests/data/vp9-profile3-444-10bit.ivfadded+0 -0

Binary file changed

crates/ec-vp9/tests/data/vp9-profile3-444-12bit.ivfadded+0 -0

Binary file changed

crates/ec-vp9/tests/hbd_exact.rsmodified+1 -22
@@ -5,8 +5,7 @@
55 //!
66 //! Media-gated: the 10-bit corpus streams are gitignored, so the test skips
77 //! by name when the fixture is absent (set the fixture tree up per the lane
8-//! notes). The other two cases pin the refusals that STAY: non-4:2:0
9-//! (profiles 1/3) and 12-bit.
8+//! notes). The 4:4:4 and 12-bit shapes are covered by `subsampling_exact.rs`.
109
1110 mod ivf;
1211
@@ -79,23 +78,3 @@ fn profile2_10bit_is_byte_exact() {
7978 }
8079 assert_eq!(off, want.len(), "frame count / plane size mismatch");
8180 }
82-
83-/// The refusals that STAY: profile 1 (4:4:4) is still refused by name.
84-#[test]
85-fn profile1_still_refused_by_name() {
86- let Some(p) = fixture("vp9-profile1-444.ivf") else {
87- eprintln!("SKIP profile1_still_refused_by_name: fixture absent");
88- return;
89- };
90- let bytes = std::fs::read(&p).unwrap();
91- let (_f, _w, _h, frames) = ivf::parse_ivf(&bytes);
92- let mut decoder = Decoder::new();
93- let err = decoder
94- .decode(&frames[0].data)
95- .expect_err("profile 1 must stay refused");
96- let msg = err.to_string();
97- assert!(
98- msg.contains("profile 1"),
99- "the refusal must name the profile, got: {msg}"
100- );
101-}
crates/ec-vp9/tests/keyframe_exact.rsmodified+52 -31
@@ -18,14 +18,22 @@ fn ensure_fixture(path: &std::path::Path) {
1818 let st = std::process::Command::new("ffmpeg")
1919 .args([
2020 "-y",
21- "-f", "lavfi",
22- "-i", "testsrc2=size=320x240:rate=30:duration=1",
23- "-pix_fmt", "yuv420p",
24- "-c:v", "libvpx-vp9",
25- "-g", "1",
26- "-cpu-used", "4",
27- "-deadline", "realtime",
28- "-frames:v", "2",
21+ "-f",
22+ "lavfi",
23+ "-i",
24+ "testsrc2=size=320x240:rate=30:duration=1",
25+ "-pix_fmt",
26+ "yuv420p",
27+ "-c:v",
28+ "libvpx-vp9",
29+ "-g",
30+ "1",
31+ "-cpu-used",
32+ "4",
33+ "-deadline",
34+ "realtime",
35+ "-frames:v",
36+ "2",
2937 ])
3038 .arg(path)
3139 .output()
@@ -71,13 +79,31 @@ fn compare(path: &std::path::Path) {
7179 assert_eq!(pic.u.len(), want_u.len());
7280 assert_eq!(pic.v.len(), want_v.len());
7381 for (i, (&a, &b)) in pic.y.iter().zip(want_y).enumerate() {
74- assert_eq!(a as u8, b, "Y plane mismatch at pixel ({}, {}) frame {shown}", i % w, i / w);
82+ assert_eq!(
83+ a as u8,
84+ b,
85+ "Y plane mismatch at pixel ({}, {}) frame {shown}",
86+ i % w,
87+ i / w
88+ );
7589 }
7690 for (i, (&a, &b)) in pic.u.iter().zip(want_u).enumerate() {
77- assert_eq!(a as u8, b, "U plane mismatch at ({}, {}) frame {shown}", i % (w / 2), i / (w / 2));
91+ assert_eq!(
92+ a as u8,
93+ b,
94+ "U plane mismatch at ({}, {}) frame {shown}",
95+ i % (w / 2),
96+ i / (w / 2)
97+ );
7898 }
7999 for (i, (&a, &b)) in pic.v.iter().zip(want_v).enumerate() {
80- assert_eq!(a as u8, b, "V plane mismatch at ({}, {}) frame {shown}", i % (w / 2), i / (w / 2));
100+ assert_eq!(
101+ a as u8,
102+ b,
103+ "V plane mismatch at ({}, {}) frame {shown}",
104+ i % (w / 2),
105+ i / (w / 2)
106+ );
81107 }
82108 shown += 1;
83109 }
@@ -110,9 +136,18 @@ fn lossless_64_matches_ffmpeg() {
110136 let want_y = &reference[..w * h];
111137 let want_u = &reference[w * h..w * h + uv];
112138 let want_v = &reference[w * h + uv..];
113- assert!(pic.y.iter().map(|&v| v as u8).eq(want_y.iter().copied()), "lossless Y must be byte-exact");
114- assert!(pic.u.iter().map(|&v| v as u8).eq(want_u.iter().copied()), "lossless U must be byte-exact");
115- assert!(pic.v.iter().map(|&v| v as u8).eq(want_v.iter().copied()), "lossless V must be byte-exact");
139+ assert!(
140+ pic.y.iter().map(|&v| v as u8).eq(want_y.iter().copied()),
141+ "lossless Y must be byte-exact"
142+ );
143+ assert!(
144+ pic.u.iter().map(|&v| v as u8).eq(want_u.iter().copied()),
145+ "lossless U must be byte-exact"
146+ );
147+ assert!(
148+ pic.v.iter().map(|&v| v as u8).eq(want_v.iter().copied()),
149+ "lossless V must be byte-exact"
150+ );
116151 assert_eq!((pic.width, pic.height), (w as u16, h as u16));
117152 }
118153
@@ -122,7 +157,9 @@ fn inter_stream_decodes_and_matches_ffmpeg() {
122157 // used to be the "inter is refused by name" witness. Inter frames decode
123158 // now, so the witness is the pixels: every shown frame must match
124159 // ffmpeg's libvpx output byte for byte.
125- let Some(dir) = ivf::fixture_dir() else { return };
160+ let Some(dir) = ivf::fixture_dir() else {
161+ return;
162+ };
126163 let path = dir.join("vp9-superframe-altref.ivf");
127164 let bytes = std::fs::read(&path).unwrap();
128165 let (_, w, h, frames) = ivf::parse_ivf(&bytes);
@@ -156,19 +193,3 @@ fn inter_stream_decodes_and_matches_ffmpeg() {
156193 assert_eq!(hidden, 0, "superframe chunks show exactly one frame each");
157194 assert_eq!(shown * frame_bytes, expected.len());
158195 }
159-
160-#[test]
161-fn profile1_444_is_named_unsupported() {
162- let Some(dir) = ivf::fixture_dir() else { return };
163- let path = dir.join("vp9-profile1-444.ivf");
164- let bytes = std::fs::read(&path).unwrap();
165- let (_, _, _, frames) = ivf::parse_ivf(&bytes);
166- let mut decoder = Decoder::new();
167- let err = decoder
168- .decode(&frames[0].data)
169- .expect_err("profile 1 4:4:4 must be refused");
170- assert!(
171- format!("{err}").contains("vp9 profile 1"),
172- "the refusal must name the profile: {err}"
173- );
174-}
crates/ec-vp9/tests/subsampling_exact.rsadded+150 -0
@@ -0,0 +1,150 @@
1+//! Non-4:2:0 subsampling and 12-bit witnesses: profiles 1 and 3 (4:4:4 /
2+//! 4:2:2 / 4:4:0 at 8 and 10 bits) and profile 2/3 12-bit decode byte-exact
3+//! against ffmpeg's libvpx.
4+//!
5+//! Every fixture under `tests/data/` is committed (see the lane report for
6+//! the exact generator commands), so these cases always run; the two
7+//! `corpus_*` cases additionally cover the media-gated 4:4:4 corpus streams
8+//! and skip by name when the fixture tree is absent.
9+//!
10+//! The comparison is the whole flattened plane stream (u16 samples) against
11+//! ffmpeg's `rawvideo` output for the matching `pix_fmt` — full-resolution
12+//! chroma for 4:4:4, half-width for 4:2:2, half-height for 4:4:0, and the
13+//! 16-bit LE sample layout for 10/12-bit.
14+
15+mod ivf;
16+
17+use ec_vp9::decode::Decoder;
18+use std::path::{Path, PathBuf};
19+
20+fn data(name: &str) -> PathBuf {
21+ Path::new(env!("CARGO_MANIFEST_DIR"))
22+ .join("tests/data")
23+ .join(name)
24+}
25+
26+fn corpus(name: &str) -> Option<PathBuf> {
27+ let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
28+ p.pop();
29+ p.pop();
30+ let p = p.join("fixtures/bitstreams").join(name);
31+ p.exists().then_some(p)
32+}
33+
34+/// ffmpeg's decoded samples as a flat u16 stream (8-bit zero-extended,
35+/// 10/12-bit little-endian pairs).
36+fn ffmpeg_samples(path: &Path, pix_fmt: &str, bd: u8) -> Vec<u16> {
37+ let out = std::process::Command::new("ffmpeg")
38+ .args(["-v", "error", "-i"])
39+ .arg(path)
40+ .args(["-f", "rawvideo", "-pix_fmt", pix_fmt, "-"])
41+ .output()
42+ .expect("ffmpeg must be on PATH for the witnesses");
43+ assert!(out.status.success(), "ffmpeg failed on {path:?}");
44+ if bd == 8 {
45+ out.stdout.iter().map(|&b| b as u16).collect()
46+ } else {
47+ out.stdout
48+ .chunks_exact(2)
49+ .map(|c| u16::from_le_bytes([c[0], c[1]]))
50+ .collect()
51+ }
52+}
53+
54+fn check(path: &Path, pix_fmt: &str, bd: u8) {
55+ let bytes = std::fs::read(path).expect("committed fixture");
56+ let (_, _, _, frames) = ivf::parse_ivf(&bytes);
57+ let want = ffmpeg_samples(path, pix_fmt, bd);
58+
59+ let mut decoder = Decoder::new();
60+ let mut ours: Vec<u16> = Vec::new();
61+ let mut shown = 0usize;
62+ for (i, frame) in frames.iter().enumerate() {
63+ match decoder
64+ .decode(&frame.data)
65+ .unwrap_or_else(|e| panic!("{}: frame {i}: {e}", path.display()))
66+ {
67+ Some(pic) => {
68+ assert_eq!(pic.bit_depth, bd, "{}: frame {i} bit depth", path.display());
69+ ours.extend(pic.y.iter().copied());
70+ ours.extend(pic.u.iter().copied());
71+ ours.extend(pic.v.iter().copied());
72+ shown += 1;
73+ }
74+ None => {}
75+ }
76+ }
77+ assert!(shown >= 2, "{}: the stream shows frames", path.display());
78+ assert_eq!(
79+ ours.len(),
80+ want.len(),
81+ "{}: plane stream length",
82+ path.display()
83+ );
84+ if let Some(i) = ours.iter().zip(&want).position(|(a, b)| a != b) {
85+ panic!(
86+ "{}: sample {i} differs (ours {} want {})",
87+ path.display(),
88+ ours[i],
89+ want[i]
90+ );
91+ }
92+}
93+
94+#[test]
95+fn profile1_444_8bit_is_byte_exact() {
96+ check(&data("vp9-profile1-444.ivf"), "yuv444p", 8);
97+}
98+
99+#[test]
100+fn profile3_444_10bit_is_byte_exact() {
101+ check(&data("vp9-profile3-444-10bit.ivf"), "yuv444p10le", 10);
102+}
103+
104+#[test]
105+fn profile3_444_12bit_is_byte_exact() {
106+ check(&data("vp9-profile3-444-12bit.ivf"), "yuv444p12le", 12);
107+}
108+
109+#[test]
110+fn profile1_422_8bit_is_byte_exact() {
111+ check(&data("vp9-profile1-422.ivf"), "yuv422p", 8);
112+}
113+
114+#[test]
115+fn profile3_422_10bit_is_byte_exact() {
116+ check(&data("vp9-profile3-422-10bit.ivf"), "yuv422p10le", 10);
117+}
118+
119+#[test]
120+fn profile1_440_8bit_is_byte_exact() {
121+ check(&data("vp9-profile1-440.ivf"), "yuv440p", 8);
122+}
123+
124+#[test]
125+fn profile3_440_10bit_is_byte_exact() {
126+ check(&data("vp9-profile3-440-10bit.ivf"), "yuv440p10le", 10);
127+}
128+
129+#[test]
130+fn profile2_420_12bit_is_byte_exact() {
131+ check(&data("vp9-profile2-420-12bit.ivf"), "yuv420p12le", 12);
132+}
133+
134+#[test]
135+fn corpus_profile1_444_matches_ffmpeg() {
136+ let Some(p) = corpus("vp9-profile1-444.ivf") else {
137+ eprintln!("SKIP corpus_profile1_444_matches_ffmpeg: fixture absent");
138+ return;
139+ };
140+ check(&p, "yuv444p", 8);
141+}
142+
143+#[test]
144+fn corpus_profile3_444_10bit_matches_ffmpeg() {
145+ let Some(p) = corpus("vp9-profile3-444-10bit.ivf") else {
146+ eprintln!("SKIP corpus_profile3_444_10bit_matches_ffmpeg: fixture absent");
147+ return;
148+ };
149+ check(&p, "yuv444p10le", 10);
150+}
lanes/vp9444.report.mdadded+214 -0
@@ -0,0 +1,214 @@
1+# Lane vp9444: VP9 profiles 1/3 (4:4:4 / 4:2:2 / 4:4:0) and 12-bit
2+
3+Branch `lane-vp9-444`, base `main f0727a87`. Crate scope: `crates/ec-vp9`
4+(+ `tables` constants). Worktree
5+`/home/user/Documents/Code/Rust/edith_codecs-vp9444`, private
6+`CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9444`.
7+
8+Deliverable: the last two named refusals — the profile gate (`profile != 0 &&
9+profile != 2`) and the subsampling gate (`subsampling_x != 1 || subsampling_y
10+!= 1`) — are removed, plus the 12-bit refusal. Every VP9 profile (0-3), every
11+coded subsampling (4:2:0 / 4:2:2 / 4:4:0 / 4:4:4) and 8/10/12-bit decode
12+byte-identically to a libvpx 1.15.0 oracle.
13+
14+## Baseline (milestone 0)
15+
16+ $ CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9444 cargo test -p ec-vp9
17+ (pre-fix) 26 test binaries, 0 failed, 0 ignored
18+
19+Pre-existing warnings (unchanged, present at base, exactly 5): `modes.rs`
20+unused `partition_probs` / `x_mis` / `y_mis` + `read_partition` never used,
21+`tables/mod.rs` `TM_PRED` never used.
22+
23+## Fail-pre-fix witnesses (milestone 1)
24+
25+Both refusals fire on the first frame of every new fixture. Captured on a
26+throwaway base worktree at `f0727a87` (`git worktree add ... f0727a87`, a
27+`scratch_prewitness` test decoding frame 0 and printing the error):
28+
29+ $ PREW_IVF=fixtures/bitstreams/vp9-profile1-444.ivf ... cargo test -p ec-vp9 --test scratch_prewitness
30+ PREW ... REFUSED: unsupported: vp9 profile 1 (this lane decodes profiles 0 and 2 only (profiles 1 and 3 are 4:4:4))
31+ $ PREW_IVF=fixtures/bitstreams/vp9-profile3-444-10bit.ivf ...
32+ PREW ... REFUSED: unsupported: vp9 profile 3 (this lane decodes profiles 0 and 2 only (profiles 1 and 3 are 4:4:4))
33+ $ PREW_IVF=crates/ec-vp9/tests/data/vp9-profile1-422.ivf ...
34+ PREW ... REFUSED: unsupported: vp9 profile 1 (this lane decodes profiles 0 and 2 only (profiles 1 and 3 are 4:4:4))
35+ $ PREW_IVF=crates/ec-vp9/tests/data/vp9-profile3-440-10bit.ivf ...
36+ PREW ... REFUSED: unsupported: vp9 profile 3 (this lane decodes profiles 0 and 2 only (profiles 1 and 3 are 4:4:4))
37+ $ PREW_IVF=crates/ec-vp9/tests/data/vp9-profile2-420-12bit.ivf ...
38+ PREW ... REFUSED: unsupported: vp9 bit depth 12 (this lane decodes 8- and 10-bit only (12-bit is not ported))
39+
40+The subsampling gate is UNREACHABLE behind the profile gate for every real
41+stream (only profiles 1/3 may code non-4:2:0, and they were refused first), so
42+the pre-fix witness for 4:2:2 / 4:4:0 is the profile refusal above. The
43+`vp9 subsampling` string itself is covered by the same delete.
44+
45+The two tests that pinned these refusals
46+(`keyframe_exact::profile1_444_is_named_unsupported`,
47+`hbd_exact::profile1_still_refused_by_name`) are deleted, replaced by the
48+byte-exactness witnesses below.
49+
50+## Root cause / design
51+
52+The crate already stored planes as `u16` with a single decode walk and
53+`clip_pixel_bd`, and the HBD/inter lanes had already parameterized MC, the
54+intra predictor and the transform by `(ss_x, ss_y)` in the places the oracle
55+needed them. The remaining hardcoding was a single idiom repeated in ~20
56+sites: `let s = usize::from(plane != 0)` — every plane-geometry shift, the
57+n4 block counts, the chroma transform size, the crop extents, the chroma
58+sub-block MV averaging and the loop-filter plane dispatch assumed 4:2:0.
59+
60+The port threads the frame's real `subsampling_x`/`subsampling_y` through, per
61+libvpx 1.15:
62+
63+- `Planes` gains `ss_x`/`ss_y`; chroma is `aw >> ss_x` by `ah >> ss_y` (the
64+ aligned extent is a 64-multiple so the shift is exact) and `dims()`/crop use
65+ `(w + ss_x) >> ss_x` (`uv_crop_width`).
66+- `plane_ss(hdr, plane)` replaces `usize::from(plane != 0)`; every `>> s` /
67+ `<< s` / `(5 + s)` splits into its `ss_x` / `ss_y` axis.
68+- `pd->n4_w/h` = `(bw << 1) >> ss_x` (`set_plane_n4`).
69+- `get_uv_tx_size` becomes the real `uv_txsize_lookup[sb_type][tx_size][ss_x]
70+ [ss_y]` (new `UV_TXSIZE_LOOKUP`, 208 entries, extracted verbatim from
71+ `vp9_common_data.c`); `ss_size` keeps its `ss_size_lookup[bsize][ss_x][ss_y]`
72+ form and now also backs libvpx's `BLOCK_INVALID` corrupt-frame check for
73+ `bsize >= 8x8` under any subsampling.
74+- `average_split_mvs` (mc.rs) selects by `ss_idx`: luma/4:4:4 own sub-block MV,
75+ 4:4:0 the q2 vertical pair, 4:2:2 the q2 horizontal pair, 4:2:0 the q4
76+ average over four.
77+- Entropy contexts: the above arrays are now `2 * mi_cols_aligned_to_sb` for
78+ EVERY plane (libvpx sizes them so: `2 * mi_cols_aligned_to_sb * MAX_MB_PLANE`),
79+ because a 4:4:4 chroma block indexes the same span as luma.
80+- Loop filter (`filter_frame`) dispatches exactly like `vp9_loop_filter_frame`
81+ (vp9_loopfilter.c:1430): luma and 4:4:4 chroma through
82+ `filter_block_plane_ss00` (4:4:4 chroma reuses the LUMA masks AND luma filter
83+ levels — `lfm->lfl_y`), 4:2:0 chroma through the existing `ss11` pass, and
84+ 4:2:2 / 4:4:0 through a new `filter_non420_plane` port of
85+ `vp9_filter_block_plane_non420` (masks + `get_filter_level` built inline per
86+ superblock band).
87+- 12-bit: the only depth-dependent syntax element is the CAT6 token; 12-bit
88+ needs the FULL `vp9_cat6_prob_high12` with 18 bits (`vp9_detokenize.c:137`),
89+ not the 16-bit `+ 2` view 10-bit uses. New `CAT6_PROB_HIGH12[18]`; the
90+ `decode_coefs` branch is now `8 => 14 bits / 10 => 16 / 12 => 18`.
91+
92+## Oracle dumper
93+
94+New `$HOME/.cache/vp9pix/drv_pixss.c` (built against the HBD libvpx 1.15.0 at
95+`$HOME/.cache/vp9hbd/lbuild`): dumps EVERY shown frame's cropped planes as
96+little-endian u16 (8-bit zero-extended) for any `vpx_image` subsampling and bit
97+depth (chroma `ceil(dim / 2^shift)`). Cross-validated against ffmpeg rawvideo,
98+`cmp`-identical, on one stream per layout before use:
99+
100+| layout | pix_fmt | oracle vs ffmpeg |
101+|---|---|---|
102+| 4:4:4 8-bit | yuv444p | IDENTICAL |
103+| 4:4:4 10-bit | yuv444p10le| IDENTICAL |
104+| 4:4:4 12-bit | yuv444p12le| IDENTICAL |
105+| 4:2:2 8-bit | yuv422p | IDENTICAL |
106+| 4:2:2 10-bit | yuv422p10le| IDENTICAL |
107+| 4:4:0 8-bit | yuv440p | IDENTICAL |
108+| 4:4:0 10-bit | yuv440p10le| IDENTICAL |
109+| 4:2:0 12-bit | yuv420p12le| IDENTICAL |
110+
111+## Byte-exactness (milestone 3)
112+
113+`$HOME/.cache/vp9pix/sweepss.sh <worktree> <stream>`: libvpx-oracle dump vs our
114+`scratch_pixdump10` dump (both u16 LE), `cmp` (no parser). Our side dumps only
115+SHOWN frames; the oracle dumps only shown frames too, so a hidden frame would
116+show as a size mismatch, not a silent pass.
117+
118+New shapes (frame-aligned byte counts quoted):
119+
120+| stream | shape | bytes | oracle vs ours |
121+|---|---|---|---|
122+| vp9-profile1-444.ivf (corpus, 60f) | 4:4:4 8-bit | 27,648,000 | IDENTICAL |
123+| vp9-profile3-444-10bit.ivf (corpus, 60f) | 4:4:4 10-bit | 27,648,000 | IDENTICAL |
124+| tests/data vp9-profile1-444.ivf (30f) | 4:4:4 8-bit | 13,824,000 | IDENTICAL |
125+| tests/data vp9-profile3-444-10bit.ivf | 4:4:4 10-bit | 13,824,000 | IDENTICAL |
126+| tests/data vp9-profile3-444-12bit.ivf | 4:4:4 12-bit | 13,824,000 | IDENTICAL |
127+| tests/data vp9-profile1-422.ivf | 4:2:2 8-bit | 9,216,000 | IDENTICAL |
128+| tests/data vp9-profile3-422-10bit.ivf | 4:2:2 10-bit | 9,216,000 | IDENTICAL |
129+| tests/data vp9-profile1-440.ivf | 4:4:0 8-bit | 9,216,000 | IDENTICAL |
130+| tests/data vp9-profile3-440-10bit.ivf | 4:4:0 10-bit | 9,216,000 | IDENTICAL |
131+| tests/data vp9-profile2-420-12bit.ivf | 4:2:0 12-bit | 6,912,000 | IDENTICAL |
132+
133+Non-vacuity for the loop-filter port: with `EC_VP9_SKIP_LF=1` (ours) and
134+`SKIPLF=1` (oracle) the 4:2:2 / 4:4:0 streams were IDENTICAL on the first
135+non420 cut, which localized the first divergence to the new
136+`filter_non420_plane`; after the fix the full-filter dumps match. (The bug was
137+the horizontal `filter_selectively_horiz` gating: a 16-tap block edge swallows
138+the internal 4x4 edge, the 8-/4-tap branches emit it separately, and an
139+internal-only bit takes the else branch.)
140+
141+## Full regression corpus (milestone 4) — all IDENTICAL
142+
143+| stream | frames-class | bytes | result |
144+|---|---|---|---|
145+| fixtures/bitstreams/vp9-1080p-23.976-8bit.ivf | 48 | 298,598,400 | IDENTICAL |
146+| fixtures/bitstreams/vp9-1080p-60-8bit.ivf | 120 | 746,496,000 | IDENTICAL |
147+| fixtures/bitstreams/vp9-2160p-23.976-8bit.ivf | 48 | 1,194,393,600 | IDENTICAL |
148+| fixtures/bitstreams/vp9-superframe-altref.ivf | 60 | 13,824,000 | IDENTICAL |
149+| fixtures/bitstreams/vp9-tiles-1280.ivf | 30 | 82,944,000 | IDENTICAL |
150+| fixtures/bitstreams/vp9-1080p-23.976-10bit.ivf| 48 | 298,598,400 | IDENTICAL |
151+| fixtures/bitstreams/vp9-1080p-60-10bit.ivf | 120 | 746,496,000 | IDENTICAL |
152+| ~/.cache/vp9pix/inter.ivf | 12 | 18,662,400 | IDENTICAL |
153+| ~/.cache/vp9pix/inter1.ivf | 12 | 18,662,400 | IDENTICAL |
154+| target/vp9-witness/odd-321x241.ivf | 12 | 2,791,752 | IDENTICAL |
155+| tests/data/intraonly.ivf | 10 | 2,304,000 | IDENTICAL |
156+| tests/data/scaledref.ivf | ? | 8,985,600 | IDENTICAL |
157+| tests/data/scaledref_odd.ivf | ? | 8,981,602 | IDENTICAL |
158+
159+## Tests added (milestone 5)
160+
161+New `crates/ec-vp9/tests/subsampling_exact.rs` (10 cases): the eight committed
162+fixtures above, each compared as a flat u16 stream against ffmpeg rawvideo for
163+the matching `pix_fmt`, plus two media-gated cases over the canonical corpus
164+4:4:4 streams (`vp9-profile1-444.ivf`, `vp9-profile3-444-10bit.ivf`) that SKIP
165+by name when the fixture tree is absent.
166+
167+## Fixtures + generators (reproducible)
168+
169+Committed under `crates/ec-vp9/tests/data/`. Every generator is
170+`ffmpeg -f lavfi -i testsrc2=size=320x240:rate=30:duration=1 -pix_fmt <PF>
171+-c:v libvpx-vp9 -profile:v <P> -b:v 500k -frames:v 30 -speed 8 <out>`;
172+re-running each reproduces the committed bytes exactly (verified with `cmp`
173+on two of them; ffmpeg 8.1.2 / libvpx 1.15.0).
174+
175+| file | PF | P | sha256 |
176+|---|---|---|---|
177+| vp9-profile1-444.ivf | yuv444p | 1 | 2358c5c0e19aebe3e90e2e1071365fb45a0aa6d15e285fb9c1c93337377149d8 |
178+| vp9-profile3-444-10bit.ivf | yuv444p10le | 3 | b9e3347f13881811238f74d23c4a17dd57f1c6dbb8c978087cf3615a62b78d70 |
179+| vp9-profile3-444-12bit.ivf | yuv444p12le | 3 | b91b7541ff8daa3b7655efde327e026524a61c64b81e1ec73fc35714b8bbd692 |
180+| vp9-profile1-422.ivf | yuv422p | 1 | b5381b5e123572563c1594fec7cace77f6b8cb185cea628e1323b08481e7d891 |
181+| vp9-profile3-422-10bit.ivf | yuv422p10le | 3 | 609dac28238a7c49158ef165b0d25687005cddc90ed10807916d1d3f40fd4d36 |
182+| vp9-profile1-440.ivf | yuv440p | 1 | 605a52469b68baf4daaa1dd728bd76313907f8ea078e3637d2c139dc44d96012 |
183+| vp9-profile3-440-10bit.ivf | yuv440p10le | 3 | a91ec788cf0569988e47d629c9aab6a384cbedd33402cce1a416877f079438c5 |
184+| vp9-profile2-420-12bit.ivf | yuv420p12le | 2 | 5c4e25ea07edc708d0559092cf775a41b6d828aaeb882e889e416cfc1eabc3bd |
185+
186+(The corpus 4:4:4 streams `vp9-profile1-444.ivf` / `vp9-profile3-444-10bit.ivf`
187+in `fixtures/bitstreams/` remain media-gated and unchanged.)
188+
189+## Gates
190+
191+ $ CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9444 cargo test -p ec-vp9
192+ 26 test binaries, 0 failed, 0 ignored
193+ $ ... cargo check -p ec-vp9
194+ warning: `ec-vp9` (lib) generated 5 warnings (exactly the 5 pre-existing)
195+
196+Scoped rustfmt applied to the changed leaf files only (decode.rs, loopfilter.rs,
197+mc.rs, tokens.rs, tables/mod.rs + gen_tables.rs via recursion, and the touched
198+test files); `git status` shows no sibling file touched.
199+
200+## Deferred / dispositions
201+
202+- Accepted: a 4:2:0 12-bit stream (profile 2) is proven; a 12-bit 4:2:2 /
203+ 4:4:0 / 4:4:4 stream (profile 3) was also generated and proven IDENTICAL
204+ (see `$HOME/.cache/vp9pix/sweepss.sh` on the /tmp 60-frame set) but only the
205+ 4:2:0 12-bit and 4:4:4 12-bit are committed as regression fixtures; the
206+ remaining 12-bit layouts live in the sweep evidence only.
207+- Accepted (residual risk, pre-existing): the Rust clamps the MV
208+ unconditionally; libvpx only in the is_scaled branch (no-op for encoder
209+ output).
210+
211+## git log
212+
213+ $ git log --oneline f0727a87..lane-vp9-444
214+ vp9444: decode profiles 1/3 (4:4:4/4:2:2/4:4:0) and 12-bit