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 commit19 changed files +1146 -378
- crates/ec-vp9/src/decode.rs +196 -151
- crates/ec-vp9/src/lib.rs +8 -6
- crates/ec-vp9/src/loopfilter.rs +425 -121
- crates/ec-vp9/src/mc.rs +59 -18
- crates/ec-vp9/src/tables/gen_tables.rs +13 -2
- crates/ec-vp9/src/tables/mod.rs +14 -20
- crates/ec-vp9/src/tokens.rs +14 -7
- crates/ec-vp9/tests/data/vp9-profile1-422.ivf +0 -0
- crates/ec-vp9/tests/data/vp9-profile1-440.ivf +0 -0
- crates/ec-vp9/tests/data/vp9-profile1-444.ivf +0 -0
- crates/ec-vp9/tests/data/vp9-profile2-420-12bit.ivf +0 -0
- crates/ec-vp9/tests/data/vp9-profile3-422-10bit.ivf +0 -0
- crates/ec-vp9/tests/data/vp9-profile3-440-10bit.ivf +0 -0
- crates/ec-vp9/tests/data/vp9-profile3-444-10bit.ivf +0 -0
- crates/ec-vp9/tests/data/vp9-profile3-444-12bit.ivf +0 -0
- crates/ec-vp9/tests/hbd_exact.rs +1 -22
- crates/ec-vp9/tests/keyframe_exact.rs +52 -31
- crates/ec-vp9/tests/subsampling_exact.rs +150 -0
- lanes/vp9444.report.md +214 -0
crates/ec-vp9/src/decode.rsmodified+196 -151
| @@ -4,21 +4,19 @@ | ||
| 4 | 4 | //! reconstruction and the loop filter (spec 6, 7, 8; libvpx |
| 5 | 5 | //! `vp9_decodeframe.c`). |
| 6 | 6 | //! |
| 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. | |
| 15 | 15 | |
| 16 | 16 | use crate::header::{FrameContext, read_compressed_header}; |
| 17 | 17 | use crate::intra::build_intra_predictors; |
| 18 | 18 | 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}; | |
| 22 | 20 | use crate::modes::{MiInfo, MiState, read_intra_frame_mode_info}; |
| 23 | 21 | use crate::tables::*; |
| 24 | 22 | use crate::tokens::{PlaneContexts, decode_coefs, scan_for}; |
| @@ -33,9 +31,10 @@ const SB_MI: usize = 8; // a 64x64 superblock in mi (8x8) units | ||
| 33 | 31 | pub struct Picture { |
| 34 | 32 | /// Luma plane, `stride`-spaced rows of `width` samples. |
| 35 | 33 | 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`). | |
| 37 | 36 | pub u: Vec<Sample>, |
| 38 | - /// V (Cr) plane, half resolution. | |
| 37 | + /// V (Cr) plane, chroma resolution. | |
| 39 | 38 | pub v: Vec<Sample>, |
| 40 | 39 | /// Frame width in pixels. |
| 41 | 40 | pub width: u16, |
| @@ -43,11 +42,17 @@ pub struct Picture { | ||
| 43 | 42 | pub height: u16, |
| 44 | 43 | /// Luma row pitch of the returned planes (== `width`; the crop packs rows). |
| 45 | 44 | 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). | |
| 48 | 47 | pub uv_stride: usize, |
| 49 | 48 | /// Bits per sample (8 for profile 0, 10 for profile 2). |
| 50 | 49 | 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, | |
| 51 | 56 | } |
| 52 | 57 | |
| 53 | 58 | struct Planes { |
| @@ -60,6 +65,12 @@ struct Planes { | ||
| 60 | 65 | ah: usize, |
| 61 | 66 | /// Bits per sample this frame was decoded at. |
| 62 | 67 | 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, | |
| 63 | 74 | } |
| 64 | 75 | |
| 65 | 76 | impl Planes { |
| @@ -67,18 +78,22 @@ impl Planes { | ||
| 67 | 78 | /// neutral grey `1 << (bd - 1)` (`128` at 8-bit, `512` at 10-bit). The |
| 68 | 79 | /// aligned the decode writes real samples into it (blocks at the frame |
| 69 | 80 | /// 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 { | |
| 72 | 84 | let fill = 1u16 << (bd - 1); |
| 85 | + let (cw, ch) = (aw >> ss_x, ah >> ss_y); | |
| 73 | 86 | Planes { |
| 74 | 87 | 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], | |
| 77 | 90 | ys: aw, |
| 78 | - uvs: aw / 2, | |
| 91 | + uvs: cw, | |
| 79 | 92 | aw, |
| 80 | 93 | ah, |
| 81 | 94 | bd, |
| 95 | + ss_x, | |
| 96 | + ss_y, | |
| 82 | 97 | } |
| 83 | 98 | } |
| 84 | 99 | |
| @@ -101,7 +116,7 @@ impl Planes { | ||
| 101 | 116 | if plane == 0 { |
| 102 | 117 | (self.aw, self.ah) |
| 103 | 118 | } else { |
| 104 | - (self.aw / 2, self.ah / 2) | |
| 119 | + (self.aw >> self.ss_x, self.ah >> self.ss_y) | |
| 105 | 120 | } |
| 106 | 121 | } |
| 107 | 122 | } |
| @@ -234,25 +249,10 @@ impl Decoder { | ||
| 234 | 249 | None => Err(corrupt("show_existing_frame names an empty slot")), |
| 235 | 250 | }; |
| 236 | 251 | } |
| 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 { | |
| 244 | 253 | return Err(Error::unsupported( |
| 245 | 254 | 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", | |
| 256 | 256 | )); |
| 257 | 257 | } |
| 258 | 258 | // One index per decoded frame (the dumps' `frame=` field), so this |
| @@ -405,16 +405,10 @@ impl Decoder { | ||
| 405 | 405 | if hdr.show_existing_frame { |
| 406 | 406 | continue; |
| 407 | 407 | } |
| 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 { | |
| 415 | 409 | return Err(Error::unsupported( |
| 416 | 410 | 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", | |
| 418 | 412 | )); |
| 419 | 413 | } |
| 420 | 414 | if hdr.frame_type == FrameType::Key { |
| @@ -447,6 +441,8 @@ impl Decoder { | ||
| 447 | 441 | index: usize, |
| 448 | 442 | pixels: bool, |
| 449 | 443 | ) -> Result<Option<Picture>> { |
| 444 | + let ss_x = hdr.subsampling_x as usize; | |
| 445 | + let ss_y = hdr.subsampling_y as usize; | |
| 450 | 446 | // Frame-context bookkeeping (spec 6.2 / libvpx `vp9_decode_frame`): |
| 451 | 447 | // 3 resets every stored context, 2 resets the selected one; 1 means |
| 452 | 448 | // "reset after this frame" and takes no action at frame start. |
| @@ -479,7 +475,7 @@ impl Decoder { | ||
| 479 | 475 | let height = hdr.height as usize; |
| 480 | 476 | let aw = width.div_ceil(64) * 64; |
| 481 | 477 | 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)); | |
| 483 | 479 | } |
| 484 | 480 | |
| 485 | 481 | let hdr_start = hdr.uncompressed_header_size as usize; |
| @@ -583,8 +579,8 @@ impl Decoder { | ||
| 583 | 579 | let aligned_cols = mi_cols.div_ceil(SB_MI) * SB_MI; |
| 584 | 580 | let mut ectx = [ |
| 585 | 581 | 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), | |
| 588 | 584 | ]; |
| 589 | 585 | ectx.iter_mut().for_each(|p| p.above.fill(0)); |
| 590 | 586 | let mut mi = MiState::new(mi_cols, mi_rows); |
| @@ -681,7 +677,10 @@ impl Decoder { | ||
| 681 | 677 | p.uvs, |
| 682 | 678 | vw, |
| 683 | 679 | vh, |
| 684 | - vh / 2, | |
| 680 | + vw >> ss_x, | |
| 681 | + vh >> ss_y, | |
| 682 | + ss_x, | |
| 683 | + ss_y, | |
| 685 | 684 | hdr.loop_filter.sharpness, |
| 686 | 685 | hdr.bit_depth, |
| 687 | 686 | ); |
| @@ -821,27 +820,36 @@ impl Decoder { | ||
| 821 | 820 | bw: usize, |
| 822 | 821 | bh: usize, |
| 823 | 822 | ) -> 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 | + ]; | |
| 826 | 834 | if info.skip { |
| 827 | 835 | // 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); | |
| 829 | 837 | return Ok(()); |
| 830 | 838 | } |
| 831 | 839 | let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; |
| 832 | 840 | let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; |
| 833 | 841 | for plane in 0..3usize { |
| 834 | - let s = usize::from(plane != 0); | |
| 842 | + let (ss_x, ss_y) = plane_ss(hdr, plane); | |
| 835 | 843 | let tx_size = if plane == 0 { |
| 836 | 844 | info.tx_size |
| 837 | 845 | } 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) | |
| 839 | 847 | }; |
| 840 | 848 | let step = 1usize << tx_size; |
| 841 | 849 | 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; | |
| 843 | 851 | 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; | |
| 845 | 853 | for brow in (0..max_blocks_high).step_by(step) { |
| 846 | 854 | for bcol in (0..max_blocks_wide).step_by(step) { |
| 847 | 855 | let _ = decode_inter_tx_tokens( |
| @@ -888,10 +896,19 @@ impl Decoder { | ||
| 888 | 896 | mi: &mut MiState, |
| 889 | 897 | tile_col_start: usize, |
| 890 | 898 | ) -> Result<()> { |
| 899 | + let (ss_x, ss_y) = plane_ss(hdr, 1); | |
| 891 | 900 | let x_mis = bw.min(mi.mi_cols - col); |
| 892 | 901 | 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 | + ]; | |
| 895 | 912 | let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; |
| 896 | 913 | let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; |
| 897 | 914 | |
| @@ -930,23 +947,23 @@ impl Decoder { | ||
| 930 | 947 | // top of `decode_block` (decodeframe.c:960), BEFORE the intra/inter |
| 931 | 948 | // split, so skipped intra blocks inside an inter frame reset too. |
| 932 | 949 | 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); | |
| 934 | 951 | } |
| 935 | 952 | |
| 936 | 953 | if !info.is_inter { |
| 937 | 954 | // `predict_and_reconstruct_intra_block` per transform block. |
| 938 | 955 | for plane in 0..3usize { |
| 939 | - let s = usize::from(plane != 0); | |
| 956 | + let (ss_x, ss_y) = plane_ss(hdr, plane); | |
| 940 | 957 | let tx_size = if plane == 0 { |
| 941 | 958 | info.tx_size |
| 942 | 959 | } 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) | |
| 944 | 961 | }; |
| 945 | 962 | let step = 1usize << tx_size; |
| 946 | 963 | 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; | |
| 948 | 965 | 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; | |
| 950 | 967 | for brow in (0..max_blocks_high).step_by(step) { |
| 951 | 968 | for bcol in (0..max_blocks_wide).step_by(step) { |
| 952 | 969 | self.predict_and_reconstruct( |
| @@ -1010,17 +1027,17 @@ impl Decoder { | ||
| 1010 | 1027 | // Reconstruction: `reconstruct_inter_block` per transform block. |
| 1011 | 1028 | let mut eobtotal = 0usize; |
| 1012 | 1029 | for plane in 0..3usize { |
| 1013 | - let s = usize::from(plane != 0); | |
| 1030 | + let (ss_x, ss_y) = plane_ss(hdr, plane); | |
| 1014 | 1031 | let tx_size = if plane == 0 { |
| 1015 | 1032 | info.tx_size |
| 1016 | 1033 | } 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) | |
| 1018 | 1035 | }; |
| 1019 | 1036 | let step = 1usize << tx_size; |
| 1020 | 1037 | 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; | |
| 1022 | 1039 | 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; | |
| 1024 | 1041 | for brow in (0..max_blocks_high).step_by(step) { |
| 1025 | 1042 | for bcol in (0..max_blocks_wide).step_by(step) { |
| 1026 | 1043 | let coef = decode_inter_tx_tokens( |
| @@ -1051,8 +1068,8 @@ impl Decoder { | ||
| 1051 | 1068 | let planes = self.planes.as_mut().expect("frame scratch"); |
| 1052 | 1069 | let stride = planes.dims(plane).0; |
| 1053 | 1070 | 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; | |
| 1056 | 1073 | let dst_off = y0 * stride + x0; |
| 1057 | 1074 | let (data, _) = planes.plane(plane); |
| 1058 | 1075 | inverse_transform_add( |
| @@ -1082,6 +1099,8 @@ impl Decoder { | ||
| 1082 | 1099 | } |
| 1083 | 1100 | |
| 1084 | 1101 | 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; | |
| 1085 | 1104 | let width = hdr.width as usize; |
| 1086 | 1105 | let height = hdr.height as usize; |
| 1087 | 1106 | // The decode buffer is padded to whole superblocks: libvpx decodes a |
| @@ -1092,7 +1111,7 @@ impl Decoder { | ||
| 1092 | 1111 | // mi_row 132 writes 8 rows past a 1080-row buffer). |
| 1093 | 1112 | let aw = width.div_ceil(64) * 64; |
| 1094 | 1113 | 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)); | |
| 1096 | 1115 | let tail_start = std::env::var("EC_VP9_FORCE_TAIL") |
| 1097 | 1116 | .ok() |
| 1098 | 1117 | .and_then(|s| s.parse().ok()) |
| @@ -1109,6 +1128,8 @@ impl Decoder { | ||
| 1109 | 1128 | tx_mode: u8, |
| 1110 | 1129 | tail: &[u8], |
| 1111 | 1130 | ) -> Result<Picture> { |
| 1131 | + let ss_x = hdr.subsampling_x as usize; | |
| 1132 | + let ss_y = hdr.subsampling_y as usize; | |
| 1112 | 1133 | let mi_cols = hdr.mi_cols() as usize; |
| 1113 | 1134 | let mi_rows = hdr.mi_rows() as usize; |
| 1114 | 1135 | let sb64_cols = mi_cols.div_ceil(SB_MI); |
| @@ -1171,8 +1192,8 @@ impl Decoder { | ||
| 1171 | 1192 | let aligned_cols = mi_cols.div_ceil(SB_MI) * SB_MI; |
| 1172 | 1193 | let mut ectx = [ |
| 1173 | 1194 | 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), | |
| 1176 | 1197 | ]; |
| 1177 | 1198 | ectx.iter_mut().for_each(|p| p.above.fill(0)); |
| 1178 | 1199 | let mut mi = MiState::new(mi_cols, mi_rows); |
| @@ -1248,7 +1269,10 @@ impl Decoder { | ||
| 1248 | 1269 | p.uvs, |
| 1249 | 1270 | vw, |
| 1250 | 1271 | vh, |
| 1251 | - vh / 2, | |
| 1272 | + vw >> ss_x, | |
| 1273 | + vh >> ss_y, | |
| 1274 | + ss_x, | |
| 1275 | + ss_y, | |
| 1252 | 1276 | hdr.loop_filter.sharpness, |
| 1253 | 1277 | hdr.bit_depth, |
| 1254 | 1278 | ); |
| @@ -1466,6 +1490,21 @@ impl Decoder { | ||
| 1466 | 1490 | let x_mis = bw.min(mi.mi_cols - col); |
| 1467 | 1491 | let y_mis = bh.min(mi.mi_rows - row); |
| 1468 | 1492 | |
| 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 | + | |
| 1469 | 1508 | // `frame_is_intra_only` (onyxc_int.h:363): a keyframe OR an |
| 1470 | 1509 | // intra-only frame walks the intra mode-info path. |
| 1471 | 1510 | let intra_frame = hdr.frame_type == FrameType::Key || hdr.intra_only; |
| @@ -1546,16 +1585,17 @@ impl Decoder { | ||
| 1546 | 1585 | // Per-plane 4x4 counts. Sub-8x8 shapes still occupy the WHOLE 8x8 |
| 1547 | 1586 | // mi area (libvpx set_plane_n4 with bw=bh=1): luma walks 2x2 4x4 |
| 1548 | 1587 | // 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 | + ]; | |
| 1559 | 1599 | let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; |
| 1560 | 1600 | let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; |
| 1561 | 1601 | |
| @@ -1575,13 +1615,13 @@ impl Decoder { | ||
| 1575 | 1615 | |
| 1576 | 1616 | if info.skip { |
| 1577 | 1617 | 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; | |
| 1581 | 1621 | for i in 0..n4w[plane] { |
| 1582 | 1622 | ectx[plane].above[offx + i] = 0; |
| 1583 | 1623 | } |
| 1584 | - let lrows = 32usize >> s; | |
| 1624 | + let lrows = 32usize >> sy; | |
| 1585 | 1625 | for i in 0..n4h[plane].min(lrows) { |
| 1586 | 1626 | ectx[plane].left[(offy + i) % lrows] = 0; |
| 1587 | 1627 | } |
| @@ -1589,17 +1629,17 @@ impl Decoder { | ||
| 1589 | 1629 | } |
| 1590 | 1630 | |
| 1591 | 1631 | for plane in 0..3usize { |
| 1592 | - let s = usize::from(plane != 0); | |
| 1632 | + let (ss_x, ss_y) = plane_ss(hdr, plane); | |
| 1593 | 1633 | let tx_size = if plane == 0 { |
| 1594 | 1634 | info.tx_size |
| 1595 | 1635 | } 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) | |
| 1597 | 1637 | }; |
| 1598 | 1638 | let step = 1usize << tx_size; |
| 1599 | 1639 | 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; | |
| 1601 | 1641 | 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; | |
| 1603 | 1643 | for brow in (0..max_blocks_high).step_by(step) { |
| 1604 | 1644 | for bcol in (0..max_blocks_wide).step_by(step) { |
| 1605 | 1645 | self.predict_and_reconstruct( |
| @@ -1656,7 +1696,7 @@ impl Decoder { | ||
| 1656 | 1696 | mb_to_bottom: i32, |
| 1657 | 1697 | tile_col_start: usize, |
| 1658 | 1698 | ) -> Result<()> { |
| 1659 | - let s = usize::from(plane != 0); | |
| 1699 | + let (ss_x, ss_y) = plane_ss(hdr, plane); | |
| 1660 | 1700 | let mode = if plane == 0 { |
| 1661 | 1701 | if sb_type < 3 { |
| 1662 | 1702 | info.bmi[(tx_row << 1) + tx_col] |
| @@ -1670,8 +1710,8 @@ impl Decoder { | ||
| 1670 | 1710 | let stride = planes.dims(plane).0; |
| 1671 | 1711 | let bd = planes.bd; |
| 1672 | 1712 | 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; | |
| 1675 | 1715 | // vp9_predict_intra_block `have_top = loff || (xd->above_mi != NULL)` |
| 1676 | 1716 | // (vp9_reconintra.c:411); `above_mi` is FRAME-gated |
| 1677 | 1717 | // (`set_mi_row_col`, vp9_onyxc_int.h:430: `mi_row != 0`), NOT |
| @@ -1683,15 +1723,15 @@ impl Decoder { | ||
| 1683 | 1723 | // last tx column of ITS OWN block (pd->n4_w units) — above-right |
| 1684 | 1724 | // past the block's right edge belongs to a not-yet-decoded |
| 1685 | 1725 | // 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; | |
| 1687 | 1727 | |
| 1688 | 1728 | // `xd->cur_buf->y_width/y_height` (vp9_reconintra.c:288-292) are the |
| 1689 | 1729 | // MI-ALIGNED (8-multiple) buffer extent, not the visible coded size: |
| 1690 | 1730 | // confirmed by instrumenting the oracle's staging for a 320x242 frame |
| 1691 | 1731 | // (`fh=248`, not 242). The extend-vs-direct DECISION is `mb_to_*_edge` |
| 1692 | 1732 | // (`: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; | |
| 1695 | 1735 | |
| 1696 | 1736 | // 1. Intra prediction (reads reconstructed neighbours). |
| 1697 | 1737 | { |
| @@ -1733,9 +1773,9 @@ impl Decoder { | ||
| 1733 | 1773 | [q.chroma_dc, q.chroma_ac] |
| 1734 | 1774 | }; |
| 1735 | 1775 | 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; | |
| 1739 | 1779 | // libvpx reads the context as !!*(uintN_t *)a / l over the WHOLE |
| 1740 | 1780 | // tx-block span (uint16 for 8x8, uint32 for 16x16, uint64 for |
| 1741 | 1781 | // 32x32), not just the first entry. |
| @@ -1783,10 +1823,10 @@ impl Decoder { | ||
| 1783 | 1823 | ); |
| 1784 | 1824 | |
| 1785 | 1825 | // 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; | |
| 1790 | 1830 | let nblocks = 1usize << tx_size; |
| 1791 | 1831 | let shift_a = if maxw > 0 && nblocks + tx_col > maxw { |
| 1792 | 1832 | (nblocks - (maxw - tx_col)) * 8 |
| @@ -1885,9 +1925,11 @@ fn crop_picture(planes: &Planes, width: u16, height: u16) -> Picture { | ||
| 1885 | 1925 | } |
| 1886 | 1926 | out |
| 1887 | 1927 | }; |
| 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); | |
| 1891 | 1933 | Picture { |
| 1892 | 1934 | y: crop(&planes.y, planes.ys, w, h), |
| 1893 | 1935 | u: crop(&planes.u, planes.uvs, cw, chh), |
| @@ -1897,30 +1939,36 @@ fn crop_picture(planes: &Planes, width: u16, height: u16) -> Picture { | ||
| 1897 | 1939 | stride: w, |
| 1898 | 1940 | uv_stride: cw, |
| 1899 | 1941 | bit_depth: planes.bd, |
| 1942 | + subsampling_x: sx as u8, | |
| 1943 | + subsampling_y: sy as u8, | |
| 1900 | 1944 | } |
| 1901 | 1945 | } |
| 1902 | 1946 | |
| 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) | |
| 1909 | 1952 | } else { |
| 1910 | - [bw * 2, bw, bw] | |
| 1953 | + (hdr.subsampling_x as usize, hdr.subsampling_y as usize) | |
| 1911 | 1954 | } |
| 1912 | 1955 | } |
| 1913 | 1956 | |
| 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 | + | |
| 1914 | 1965 | /// `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 | |
| 1921 | 1968 | } |
| 1922 | 1969 | |
| 1923 | 1970 | /// `dec_reset_skip_context` (`vp9_decodeframe.c:800`). |
| 1971 | +#[allow(clippy::too_many_arguments)] | |
| 1924 | 1972 | fn reset_skip_context( |
| 1925 | 1973 | ectx: &mut [PlaneContexts; 3], |
| 1926 | 1974 | row: usize, |
| @@ -1928,16 +1976,18 @@ fn reset_skip_context( | ||
| 1928 | 1976 | sb_type: usize, |
| 1929 | 1977 | n4w: [usize; 3], |
| 1930 | 1978 | n4h: [usize; 3], |
| 1979 | + ss_x: usize, | |
| 1980 | + ss_y: usize, | |
| 1931 | 1981 | ) { |
| 1932 | 1982 | let _ = sb_type; |
| 1933 | 1983 | 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; | |
| 1937 | 1987 | for i in 0..n4w[plane] { |
| 1938 | 1988 | ectx[plane].above[offx + i] = 0; |
| 1939 | 1989 | } |
| 1940 | - let lrows = 32usize >> s; | |
| 1990 | + let lrows = 32usize >> sy; | |
| 1941 | 1991 | for i in 0..n4h[plane].min(lrows) { |
| 1942 | 1992 | ectx[plane].left[(offy + i) % lrows] = 0; |
| 1943 | 1993 | } |
| @@ -1948,10 +1998,10 @@ fn reset_skip_context( | ||
| 1948 | 1998 | // block), not the whole rolling buffer from base index 0. |
| 1949 | 1999 | let mut line = format!("TDK row={row} col={col} n4w={n4w:?} n4h={n4h:?}"); |
| 1950 | 2000 | 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; | |
| 1955 | 2005 | let a: Vec<u8> = (0..n4w[plane]) |
| 1956 | 2006 | .map(|i| ectx[plane].above[offx + i]) |
| 1957 | 2007 | .collect(); |
| @@ -2031,7 +2081,7 @@ fn decode_inter_tx_tokens( | ||
| 2031 | 2081 | mb_to_right: i32, |
| 2032 | 2082 | mb_to_bottom: i32, |
| 2033 | 2083 | ) -> crate::tokens::CoeffBlock { |
| 2034 | - let s = usize::from(plane != 0); | |
| 2084 | + let (ss_x, ss_y) = plane_ss(hdr, plane); | |
| 2035 | 2085 | // `predict_and_reconstruct_intra_block` (decodeframe.c:320-345): the luma |
| 2036 | 2086 | // mode of a sub-8x8 block is the SUB-BLOCK's, and an intra block inside an |
| 2037 | 2087 | // inter frame picks its tx type from that mode — `reconstruct_inter_block` |
| @@ -2054,9 +2104,9 @@ fn decode_inter_tx_tokens( | ||
| 2054 | 2104 | [q.chroma_dc, q.chroma_ac] |
| 2055 | 2105 | }; |
| 2056 | 2106 | 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; | |
| 2060 | 2110 | let nblocks = 1usize << tx_size; |
| 2061 | 2111 | let ctx_in = usize::from(ectx[plane].above[ax..ax + nblocks].iter().any(|&v| v != 0)) |
| 2062 | 2112 | + usize::from(ectx[plane].left[ay..ay + nblocks].iter().any(|&v| v != 0)); |
| @@ -2084,10 +2134,10 @@ fn decode_inter_tx_tokens( | ||
| 2084 | 2134 | plane, |
| 2085 | 2135 | ); |
| 2086 | 2136 | // 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; | |
| 2091 | 2141 | let shift_a = if maxw > 0 && nblocks + bcol > maxw { |
| 2092 | 2142 | (nblocks - (maxw - bcol)) * 8 |
| 2093 | 2143 | } else { |
| @@ -2132,9 +2182,7 @@ fn inter_predict_plane( | ||
| 2132 | 2182 | mb_to_bottom: i32, |
| 2133 | 2183 | sb_type: usize, |
| 2134 | 2184 | ) -> 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); | |
| 2138 | 2186 | let pw = n4w[plane] * 4; |
| 2139 | 2187 | let ph = n4h[plane] * 4; |
| 2140 | 2188 | let mb_left = -((col as i32) * 64); |
| @@ -2179,27 +2227,24 @@ fn inter_predict_plane( | ||
| 2179 | 2227 | // plain `>> 1` is one short whenever the coded extent is odd and |
| 2180 | 2228 | // the predictor's border/inside test then clips the last chroma |
| 2181 | 2229 | // 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, | |
| 2184 | 2232 | bd: ref_frame.planes.bd, |
| 2185 | 2233 | }; |
| 2186 | 2234 | if sb_type < 3 { |
| 2187 | 2235 | // 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). | |
| 2190 | 2239 | let n4x = n4w[plane]; |
| 2191 | 2240 | let n4y = n4h[plane]; |
| 2192 | 2241 | for y in 0..n4y { |
| 2193 | 2242 | for xx in 0..n4x { |
| 2194 | 2243 | 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); | |
| 2200 | 2245 | let (data, _) = planes.plane(plane); |
| 2201 | 2246 | 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; | |
| 2203 | 2248 | build_inter_predictors( |
| 2204 | 2249 | &mut data[dst_off..], |
| 2205 | 2250 | dst_stride, |
| @@ -2227,7 +2272,7 @@ fn inter_predict_plane( | ||
| 2227 | 2272 | } |
| 2228 | 2273 | } else { |
| 2229 | 2274 | 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); | |
| 2231 | 2276 | build_inter_predictors( |
| 2232 | 2277 | &mut data[dst_off..], |
| 2233 | 2278 | 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). | |
| 2 | 3 | //! |
| 3 | 4 | //! The crate implements the VP9 bitstream specification directly |
| 4 | 5 | //! ([spec]); libvpx's output is used only as a test oracle |
| 5 | 6 | //! (sample-exactness witnesses against ffmpeg's libvpx-based decoder), |
| 6 | 7 | //! never as code linked at runtime. Every module documents the spec |
| 7 | 8 | //! 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. | |
| 10 | 11 | //! |
| 11 | 12 | //! Planes are stored as `u16` for every depth ([`Sample`]); kernels take the |
| 12 | 13 | //! 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). | |
| 16 | 18 | //! |
| 17 | 19 | //! [spec]: https://www.webmproject.org/docs/vp9/ |
| 18 | 20 | //! |
crates/ec-vp9/src/loopfilter.rsmodified+425 -121
| @@ -616,20 +616,22 @@ impl LfGrids { | ||
| 616 | 616 | /// column pass takes NO width bound (vp9_loopfilter.c:1394 iterates all |
| 617 | 617 | /// `MI_BLOCK_SIZE >> 1` column groups and `filter_selectively_vert_row2` |
| 618 | 618 | /// 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( | |
| 620 | 625 | &self, |
| 621 | - y: &mut [Sample], | |
| 626 | + data: &mut [Sample], | |
| 622 | 627 | 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, | |
| 629 | 630 | sharpness: u8, |
| 630 | 631 | bd: u8, |
| 632 | + trace: bool, | |
| 631 | 633 | ) { |
| 632 | - let lftrace = lftrace_on(); | |
| 634 | + let lftrace = trace && lftrace_on(); | |
| 633 | 635 | // Luma masks, ported from vp9_setup_mask/build_masks and |
| 634 | 636 | // vp9_adjust_mask (bit i = MI row i>>3, MI col i&7; each bit |
| 635 | 637 | // covers an 8x8 cell). Edges are emitted from the masks. |
| @@ -642,12 +644,12 @@ impl LfGrids { | ||
| 642 | 644 | let (left, above, int4) = self.luma_masks(sbr, sbc); |
| 643 | 645 | for row in 0..8usize { |
| 644 | 646 | let yy = sbr * 8 + row * 8; |
| 645 | - if yy >= y_h { | |
| 647 | + if yy >= h { | |
| 646 | 648 | break; |
| 647 | 649 | } |
| 648 | 650 | for col in 0..8usize { |
| 649 | 651 | let xx = sbc * 8 + col * 8; |
| 650 | - if xx >= y_w { | |
| 652 | + if xx >= w { | |
| 651 | 653 | break; |
| 652 | 654 | } |
| 653 | 655 | let bit = 1u64 << (row * 8 + col); |
| @@ -666,11 +668,11 @@ impl LfGrids { | ||
| 666 | 668 | } else { |
| 667 | 669 | 0 |
| 668 | 670 | }; |
| 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) { | |
| 671 | 673 | t = 8; |
| 672 | 674 | } |
| 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); | |
| 674 | 676 | if lftrace { |
| 675 | 677 | let kind = match t { |
| 676 | 678 | 16 => "V16", |
| @@ -680,8 +682,8 @@ impl LfGrids { | ||
| 680 | 682 | eprintln!("{kind} {} {}", xx, yy); |
| 681 | 683 | } |
| 682 | 684 | } |
| 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); | |
| 685 | 687 | if lftrace { |
| 686 | 688 | eprintln!("V4I {} {}", xx + 4, yy); |
| 687 | 689 | } |
| @@ -690,12 +692,12 @@ impl LfGrids { | ||
| 690 | 692 | } |
| 691 | 693 | for row in 0..8usize { |
| 692 | 694 | let yy = sbr * 8 + row * 8; |
| 693 | - if yy >= y_h { | |
| 695 | + if yy >= h { | |
| 694 | 696 | break; |
| 695 | 697 | } |
| 696 | 698 | for col in 0..8usize { |
| 697 | 699 | let xx = sbc * 8 + col * 8; |
| 698 | - if xx >= y_w { | |
| 700 | + if xx >= w { | |
| 699 | 701 | break; |
| 700 | 702 | } |
| 701 | 703 | let bit = 1u64 << (row * 8 + col); |
| @@ -715,11 +717,11 @@ impl LfGrids { | ||
| 715 | 717 | } else { |
| 716 | 718 | 0 |
| 717 | 719 | }; |
| 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) { | |
| 720 | 722 | t = 8; |
| 721 | 723 | } |
| 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); | |
| 723 | 725 | if lftrace { |
| 724 | 726 | let kind = match t { |
| 725 | 727 | 16 => "H16", |
| @@ -736,8 +738,19 @@ impl LfGrids { | ||
| 736 | 738 | // (vp9_loopfilter.c:1286-1311). `skip_border_4x4_r` |
| 737 | 739 | // exists only in the 4:2:0-chroma path (ss11, |
| 738 | 740 | // 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 | + ); | |
| 741 | 754 | if lftrace { |
| 742 | 755 | eprintln!("H4I {} {}", xx, yy + 4); |
| 743 | 756 | } |
| @@ -746,6 +759,18 @@ impl LfGrids { | ||
| 746 | 759 | } |
| 747 | 760 | } |
| 748 | 761 | } |
| 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 | + ) { | |
| 749 | 774 | // Chroma 4:2:0 (vp9_filter_block_plane_ss11). MI is 4 chroma |
| 750 | 775 | // pixels. The UV masks are built per 64x64 superblock exactly as |
| 751 | 776 | // vp9_setup_mask/build_masks do — one build_masks per block, only |
| @@ -775,7 +800,7 @@ impl LfGrids { | ||
| 775 | 800 | if (bor as usize, boc as usize) != (r, c) || self.level8[cell] == 0 { |
| 776 | 801 | continue; |
| 777 | 802 | } |
| 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); | |
| 779 | 804 | let shift = (((r & 7) >> 1) << 2) + ((c & 7) >> 1); |
| 780 | 805 | let b = bsize as usize; |
| 781 | 806 | left[uv_tx] |= UV_LEFT_PRED[b] << shift; |
| @@ -845,65 +870,49 @@ impl LfGrids { | ||
| 845 | 870 | *m &= 0xeeee; |
| 846 | 871 | } |
| 847 | 872 | } |
| 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 { | |
| 863 | 895 | continue; |
| 864 | 896 | } |
| 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 }; | |
| 901 | 910 | lpf_edge( |
| 902 | 911 | data, |
| 903 | - y8 * uv_stride + x8 + 4, | |
| 912 | + y8 * uv_stride + x8, | |
| 904 | 913 | uv_stride, |
| 905 | 914 | 1, |
| 906 | - 4, | |
| 915 | + t, | |
| 907 | 916 | bl, |
| 908 | 917 | li, |
| 909 | 918 | th, |
| @@ -912,60 +921,294 @@ impl LfGrids { | ||
| 912 | 921 | ); |
| 913 | 922 | } |
| 914 | 923 | } |
| 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 | + } | |
| 915 | 938 | } |
| 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 { | |
| 922 | 954 | continue; |
| 923 | 955 | } |
| 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) { | |
| 937 | 969 | 8 |
| 938 | - } else if above[TX_4X4] & bit != 0 { | |
| 939 | - 4 | |
| 940 | 970 | } else { |
| 941 | - 0 | |
| 971 | + taps | |
| 942 | 972 | }; |
| 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 | |
| 946 | 1076 | } 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; | |
| 961 | 1119 | } |
| 962 | 1120 | } |
| 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 { | |
| 965 | 1208 | lpf_edge( |
| 966 | 1209 | data, |
| 967 | - (y8 + 4) * uv_stride + x8, | |
| 968 | - uv_stride, | |
| 1210 | + (y + 4) * stride + x, | |
| 1211 | + stride, | |
| 969 | 1212 | 0, |
| 970 | 1213 | 4, |
| 971 | 1214 | bl, |
| @@ -975,10 +1218,71 @@ impl LfGrids { | ||
| 975 | 1218 | bd, |
| 976 | 1219 | ); |
| 977 | 1220 | } |
| 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); | |
| 978 | 1239 | } |
| 979 | 1240 | } |
| 1241 | + r += row_step; | |
| 980 | 1242 | } |
| 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); | |
| 981 | 1275 | } |
| 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); | |
| 982 | 1286 | } |
| 983 | 1287 | } |
| 984 | 1288 | } |
| @@ -1005,7 +1309,7 @@ mod tests { | ||
| 1005 | 1309 | fn run(g: &LfGrids, y: &mut [Sample]) { |
| 1006 | 1310 | let mut u = vec![128u16; 32 * 32]; |
| 1007 | 1311 | 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); | |
| 1009 | 1313 | } |
| 1010 | 1314 | |
| 1011 | 1315 | /// 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( | ||
| 455 | 455 | } |
| 456 | 456 | if subpel_y == 0 { |
| 457 | 457 | 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, | |
| 459 | 470 | ); |
| 460 | 471 | return; |
| 461 | 472 | } |
| 462 | 473 | if subpel_x == 0 { |
| 463 | 474 | 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, | |
| 465 | 487 | ); |
| 466 | 488 | return; |
| 467 | 489 | } |
| @@ -744,24 +766,43 @@ pub(crate) fn build_inter_predictors( | ||
| 744 | 766 | ); |
| 745 | 767 | } |
| 746 | 768 | |
| 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. | |
| 755 | 776 | /// |
| 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 | |
| 757 | 778 | /// 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 }; | |
| 759 | 787 | 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 | + } | |
| 765 | 807 | } |
| 766 | - (q4(row), q4(col)) | |
| 767 | 808 | } |
crates/ec-vp9/src/tables/gen_tables.rsmodified+13 -2
| @@ -588,6 +588,17 @@ pub(crate) const SS_SIZE_LOOKUP: [u8; 52] = [ | ||
| 588 | 588 | 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, |
| 589 | 589 | 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, |
| 590 | 590 | ]; |
| 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 | +]; | |
| 591 | 602 | pub(crate) const SUBSIZE_LOOKUP: [u8; 52] = [ |
| 592 | 603 | 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, |
| 593 | 604 | 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] = [ | ||
| 1451 | 1462 | pub(crate) const MV_FP_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; |
| 1452 | 1463 | /// `default_if_y_probs`, `[BLOCK_SIZE_GROUPS][INTRA_MODES - 1]`. |
| 1453 | 1464 | 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, | |
| 1456 | 1467 | ]; |
| 1457 | 1468 | /// `default_inter_mode_probs`, `[INTER_MODE_CONTEXTS][INTER_MODES - 1]`. |
| 1458 | 1469 | 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 | ||
| 51 | 51 | /// blockd.h `get_y_mode`. |
| 52 | 52 | #[inline] |
| 53 | 53 | 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 } | |
| 59 | 55 | } |
| 60 | 56 | |
| 61 | 57 | /// 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] { | ||
| 107 | 103 | out |
| 108 | 104 | } |
| 109 | 105 | |
| 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`. | |
| 111 | 112 | #[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 | |
| 114 | 115 | } |
| 115 | 116 | |
| 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]`. | |
| 117 | 119 | #[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 | |
| 124 | 122 | } |
| 125 | 123 | |
| 126 | 124 | pub(crate) fn corrupt(what: impl Into<String>) -> Error { |
| @@ -128,9 +126,5 @@ pub(crate) fn corrupt(what: impl Into<String>) -> Error { | ||
| 128 | 126 | } |
| 129 | 127 | |
| 130 | 128 | 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)) } | |
| 136 | 130 | } |
crates/ec-vp9/src/tokens.rsmodified+14 -7
| @@ -5,13 +5,20 @@ use crate::bool::BoolDecoder; | ||
| 5 | 5 | use crate::tables::*; |
| 6 | 6 | |
| 7 | 7 | /// `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. | |
| 11 | 9 | const CAT6_PROB_HIGH: [u8; 16] = [ |
| 12 | 10 | 255, 255, 254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129, |
| 13 | 11 | ]; |
| 14 | 12 | |
| 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 | + | |
| 15 | 22 | /// One plane's entropy contexts: a has-coefficient flag per 4x4 column |
| 16 | 23 | /// above the current superblock row and per 4x4 row to the left. |
| 17 | 24 | pub(crate) struct PlaneContexts { |
| @@ -121,10 +128,10 @@ pub(crate) fn decode_coefs( | ||
| 121 | 128 | if r.read_bool(p[5]) { |
| 122 | 129 | if r.read_bool(p[7]) { |
| 123 | 130 | 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), | |
| 128 | 135 | }; |
| 129 | 136 | } else { |
| 130 | 137 | 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 @@ | ||
| 5 | 5 | //! |
| 6 | 6 | //! Media-gated: the 10-bit corpus streams are gitignored, so the test skips |
| 7 | 7 | //! 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`. | |
| 10 | 9 | |
| 11 | 10 | mod ivf; |
| 12 | 11 | |
| @@ -79,23 +78,3 @@ fn profile2_10bit_is_byte_exact() { | ||
| 79 | 78 | } |
| 80 | 79 | assert_eq!(off, want.len(), "frame count / plane size mismatch"); |
| 81 | 80 | } |
| 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) { | ||
| 18 | 18 | let st = std::process::Command::new("ffmpeg") |
| 19 | 19 | .args([ |
| 20 | 20 | "-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", | |
| 29 | 37 | ]) |
| 30 | 38 | .arg(path) |
| 31 | 39 | .output() |
| @@ -71,13 +79,31 @@ fn compare(path: &std::path::Path) { | ||
| 71 | 79 | assert_eq!(pic.u.len(), want_u.len()); |
| 72 | 80 | assert_eq!(pic.v.len(), want_v.len()); |
| 73 | 81 | 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 | + ); | |
| 75 | 89 | } |
| 76 | 90 | 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 | + ); | |
| 78 | 98 | } |
| 79 | 99 | 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 | + ); | |
| 81 | 107 | } |
| 82 | 108 | shown += 1; |
| 83 | 109 | } |
| @@ -110,9 +136,18 @@ fn lossless_64_matches_ffmpeg() { | ||
| 110 | 136 | let want_y = &reference[..w * h]; |
| 111 | 137 | let want_u = &reference[w * h..w * h + uv]; |
| 112 | 138 | 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 | + ); | |
| 116 | 151 | assert_eq!((pic.width, pic.height), (w as u16, h as u16)); |
| 117 | 152 | } |
| 118 | 153 | |
| @@ -122,7 +157,9 @@ fn inter_stream_decodes_and_matches_ffmpeg() { | ||
| 122 | 157 | // used to be the "inter is refused by name" witness. Inter frames decode |
| 123 | 158 | // now, so the witness is the pixels: every shown frame must match |
| 124 | 159 | // 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 | + }; | |
| 126 | 163 | let path = dir.join("vp9-superframe-altref.ivf"); |
| 127 | 164 | let bytes = std::fs::read(&path).unwrap(); |
| 128 | 165 | let (_, w, h, frames) = ivf::parse_ivf(&bytes); |
| @@ -156,19 +193,3 @@ fn inter_stream_decodes_and_matches_ffmpeg() { | ||
| 156 | 193 | assert_eq!(hidden, 0, "superframe chunks show exactly one frame each"); |
| 157 | 194 | assert_eq!(shown * frame_bytes, expected.len()); |
| 158 | 195 | } |
| 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 |