forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'simplify/s7' into simplify/w2

Browse files at this commit

a72559c064e5dd0b2da358503b2556acc5bea946Ahmet Kaan GümüşParent: 74c8bf5b8521cc
crates/ec-vp8/src/decode.rsmodified+56 -59
@@ -95,9 +95,9 @@ impl Plane {
9595 #[derive(Clone, Default)]
9696 struct MbInfo {
9797 /// [`modes::YMode`] as its coded integer.
98- y_mode: u8,
99- uv_mode: u8,
100- b_modes: [u8; 16],
98+ y_mode: modes::YMode,
99+ uv_mode: modes::YMode,
100+ b_modes: [modes::BMode; 16],
101101 segment_id: u8,
102102 skip: bool,
103103 /// 0 = intra, 1 = last, 2 = golden, 3 = altref (§16.2). Key-frame
@@ -105,7 +105,7 @@ struct MbInfo {
105105 ref_frame: u8,
106106 /// Inter mv_ref leaf (§16.2): 0 = zero, 1 = nearest, 2 = near,
107107 /// 3 = new, 4 = split. 0 when intra.
108- mv_ref: u8,
108+ mv_ref: modes::MvRef,
109109 /// Whole-MB motion vector in eighth-pels; the bottom-right subblock's
110110 /// vector for SPLITMV (what future neighbours' census sees).
111111 mv: (i16, i16),
@@ -135,7 +135,7 @@ const MC_BORDER: usize = 32;
135135
136136 impl MbInfo {
137137 fn has_y2(&self) -> bool {
138- self.y_mode != modes::YMode::BPred.as_u8()
138+ self.y_mode != modes::YMode::BPred
139139 }
140140 }
141141
@@ -166,7 +166,7 @@ pub struct Decoder {
166166 /// six-tap filters, versions 1-3 the bilinear ones) and full-pel
167167 /// flag (version 3: `fullpixel_mask = ~7`). The decoder never reads
168168 /// `no_lpf` — dead there too.
169- mc_bilinear: bool,
169+ mc_mode: mc::McMode,
170170 mc_fullpel: bool,
171171 }
172172
@@ -219,7 +219,7 @@ impl Decoder {
219219 refs: [None, None, None, None],
220220 segment_map: Vec::new(),
221221 frame_decoded: false,
222- mc_bilinear: false,
222+ mc_mode: mc::McMode::SixTap,
223223 mc_fullpel: false,
224224 }
225225 }
@@ -268,7 +268,11 @@ impl Decoder {
268268 // vp8_setup_version (decodeframe.c:935): the tag's version bits
269269 // pick the MC interpolation filter and full-pel mode for this
270270 // frame (see the field docs on Decoder).
271- self.mc_bilinear = matches!(header.tag.version, 1..=3);
271+ self.mc_mode = if matches!(header.tag.version, 1..=3) {
272+ mc::McMode::Bilinear
273+ } else {
274+ mc::McMode::SixTap
275+ };
272276 self.mc_fullpel = header.tag.version == 3;
273277 let refresh_entropy = header.refresh.refresh_entropy;
274278 let pic = match header.tag.frame_type {
@@ -438,8 +442,7 @@ impl Decoder {
438442 self.refs[2] = Some(rf.clone());
439443 self.refs[3] = Some(rf);
440444
441- self.frame_decoded = true;
442- Ok(header.tag.show_frame.then(|| self.crop()))
445+ Ok(self.finish_frame(&header))
443446 }
444447
445448 /// Loop-filter post-pass over the whole frame (dixie order-equivalent
@@ -513,15 +516,15 @@ impl Decoder {
513516 if header.lf_delta_enabled {
514517 level += self.state.ref_lf_delta[usize::from(mb.ref_frame)];
515518 let mode_idx = if mb.ref_frame == 0 {
516- if mb.y_mode == modes::YMode::BPred.as_u8() {
519+ if mb.y_mode == modes::YMode::BPred {
517520 Some(0)
518521 } else {
519522 None
520523 }
521524 } else {
522525 match mb.mv_ref {
523- 0 => Some(1), // ZEROMV
524- 4 => Some(3), // SPLITMV
526+ modes::MvRef::Zero => Some(1), // ZEROMV
527+ modes::MvRef::Split => Some(3), // SPLITMV
525528 _ => Some(2), // NEAREST / NEAR / NEW
526529 }
527530 };
@@ -698,8 +701,7 @@ impl Decoder {
698701 self.refs[1] = Some(new);
699702 }
700703
701- self.frame_decoded = true;
702- Ok(header.tag.show_frame.then(|| self.crop()))
704+ Ok(self.finish_frame(&header))
703705 }
704706
705707 /// Inter-frame mode record (§16, libvpx `read_mb_modes_mv`): intra
@@ -725,13 +727,13 @@ impl Decoder {
725727 // context-dependent kf_bmode_prob table is key-frame-only).
726728 mb.ref_frame = 0;
727729 let ymode = modes::read_ymode(d, ymode_probs);
728- mb.y_mode = ymode.as_u8();
730+ mb.y_mode = ymode;
729731 if ymode == modes::YMode::BPred {
730732 for j in 0..16usize {
731- mb.b_modes[j] = modes::read_bmode(d, &modes::INTER_BMODE_PROBS).as_u8();
733+ mb.b_modes[j] = modes::read_bmode(d, &modes::INTER_BMODE_PROBS);
732734 }
733735 }
734- mb.uv_mode = modes::read_uvmode(d, uv_mode_probs).as_u8();
736+ mb.uv_mode = modes::read_uvmode(d, uv_mode_probs);
735737 return;
736738 }
737739
@@ -751,17 +753,17 @@ impl Decoder {
751753 let (mut near_mvs, cnt) = modes::find_near_mvs(above, left, aboveleft, to_bias);
752754
753755 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_INTRA] as usize][0]) {
754- mb.mv_ref = 0; // ZEROMV
756+ mb.mv_ref = modes::MvRef::Zero;
755757 return;
756758 }
757759 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAREST] as usize][1]) {
758- mb.mv_ref = 1; // NEARESTMV
760+ mb.mv_ref = modes::MvRef::Nearest;
759761 mb.mv = near_mvs[modes::CNT_NEAREST];
760762 modes::clamp_mv2(&mut mb.mv, edges);
761763 return;
762764 }
763765 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAR] as usize][2]) {
764- mb.mv_ref = 2; // NEARMV
766+ mb.mv_ref = modes::MvRef::Near;
765767 mb.mv = near_mvs[modes::CNT_NEAR];
766768 modes::clamp_mv2(&mut mb.mv, edges);
767769 return;
@@ -774,7 +776,7 @@ impl Decoder {
774776 modes::clamp_mv2(&mut near_mvs[near_index], edges);
775777 let best = near_mvs[near_index];
776778 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_SPLITMV] as usize][3]) {
777- mb.mv_ref = 3; // NEWMV
779+ mb.mv_ref = modes::MvRef::New;
778780 let mut mv = modes::read_mv(d, &self.state.mv_probs);
779781 mv.0 = mv.0.wrapping_add(best.0);
780782 mv.1 = mv.1.wrapping_add(best.1);
@@ -802,7 +804,7 @@ impl Decoder {
802804 intra: false,
803805 zero: mb.mv == (0, 0),
804806 mv: mb.mv,
805- is_split: mb.mv_ref == 4,
807+ is_split: mb.mv_ref == modes::MvRef::Split,
806808 sign_bias: sign_bias[usize::from(mb.ref_frame) - 1],
807809 }
808810 }
@@ -826,7 +828,7 @@ impl Decoder {
826828 let k = usize::from(modes::MBSPLIT_OFFSET[s][j]);
827829 let leftmv = if k % 4 == 0 {
828830 let lm = self.mbi(row, col - 1);
829- if lm.mv_ref == 4 {
831+ if lm.mv_ref == modes::MvRef::Split {
830832 lm.bmi[k + 3]
831833 } else {
832834 lm.mv
@@ -836,7 +838,7 @@ impl Decoder {
836838 };
837839 let abovemv = if k < 4 {
838840 let am = self.mbi(row - 1, col);
839- if am.mv_ref == 4 {
841+ if am.mv_ref == modes::MvRef::Split {
840842 am.bmi[k + 12]
841843 } else {
842844 am.mv
@@ -862,9 +864,9 @@ impl Decoder {
862864 mb.bmi[usize::from(off)] = blockmv;
863865 }
864866 }
865- mb.mv_ref = 4;
867+ mb.mv_ref = modes::MvRef::Split;
866868 // 4x4 residual coding without a Y2 block (like B_PRED).
867- mb.y_mode = modes::YMode::BPred.as_u8();
869+ mb.y_mode = modes::YMode::BPred;
868870 // Neighbours' census sees the bottom-right subblock's vector.
869871 mb.mv = mb.bmi[15];
870872 }
@@ -903,7 +905,7 @@ impl Decoder {
903905 // SPLITMV (identical output to libvpx's grouped predicts). Each
904906 // predict lands at its own (bx, by) slot of one 16x16 patch.
905907 let mut py = [0u8; 256];
906- if mb.mv_ref == 4 {
908+ if mb.mv_ref == modes::MvRef::Split {
907909 for b in 0..16usize {
908910 let (bx, by) = ((b % 4) * 4, (b / 4) * 4);
909911 // reconinter.c:370-374: the four corner subblocks clamp
@@ -912,13 +914,13 @@ impl Decoder {
912914 mc::predict_luma(
913915 &reff.y, reff.stride, MC_BORDER, w, h, x + bx, y + by, mb.bmi[b],
914916 mb.mv_clamp && corner,
915- &mut py[by * 16 + bx..], 16, 4, 4, self.mc_bilinear,
917+ &mut py[by * 16 + bx..], 16, 4, 4, self.mc_mode,
916918 );
917919 }
918920 } else {
919921 mc::predict_luma(
920922 &reff.y, reff.stride, MC_BORDER, w, h, x, y, mb.mv, mb.mv_clamp, &mut py, 16,
921- 16, 16, self.mc_bilinear,
923+ 16, 16, self.mc_mode,
922924 );
923925 }
924926
@@ -932,7 +934,7 @@ impl Decoder {
932934 // clamps only under need_to_clamp_mvs.
933935 let mut pu = [0u8; 256];
934936 let mut pv = [0u8; 256];
935- if mb.mv_ref == 4 {
937+ if mb.mv_ref == modes::MvRef::Split {
936938 for q in 0..4usize {
937939 // Quadrant q: 4x4 chroma pixels at (qx4, qy4) in the MB.
938940 let (qx4, qy4) = ((q % 2) * 4, (q / 2) * 4);
@@ -950,23 +952,23 @@ impl Decoder {
950952 mc::predict_chroma(
951953 &reff.u, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv,
952954 mb.mv_clamp, &mut pu[qy4 * 16 + qx4..], 16, 4, 4,
953- self.mc_bilinear,
955+ self.mc_mode,
954956 );
955957 mc::predict_chroma(
956958 &reff.v, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv,
957959 mb.mv_clamp, &mut pv[qy4 * 16 + qx4..], 16, 4, 4,
958- self.mc_bilinear,
960+ self.mc_mode,
959961 );
960962 }
961963 } else {
962964 let uvmv = modes::chroma_mv_whole(mb.mv, self.mc_fullpel);
963965 mc::predict_chroma(
964966 &reff.u, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true,
965- &mut pu, 16, 8, 8, self.mc_bilinear,
967+ &mut pu, 16, 8, 8, self.mc_mode,
966968 );
967969 mc::predict_chroma(
968970 &reff.v, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true,
969- &mut pv, 16, 8, 8, self.mc_bilinear,
971+ &mut pv, 16, 8, 8, self.mc_mode,
970972 );
971973 }
972974
@@ -1012,28 +1014,28 @@ impl Decoder {
10121014 mb: &mut MbInfo,
10131015 ) {
10141016 let ymode = modes::read_kf_ymode(d);
1015- mb.y_mode = ymode.as_u8();
1017+ mb.y_mode = ymode;
10161018 if ymode == modes::YMode::BPred {
10171019 for j in 0..16u8 {
10181020 let a = self.above_block_mode(row, col, j, mb);
10191021 let l = self.left_block_mode(row, col, j, mb);
10201022 mb.b_modes[usize::from(j)] =
1021- modes::read_bmode(d, &KF_BMODE_PROB[usize::from(a)][usize::from(l)]).as_u8();
1023+ modes::read_bmode(d, &KF_BMODE_PROB[usize::from(a.as_u8())][usize::from(l.as_u8())]);
10221024 }
10231025 }
1024- mb.uv_mode = modes::read_uvmode(d, &modes::KF_UV_MODE_PROBS).as_u8();
1026+ mb.uv_mode = modes::read_uvmode(d, &modes::KF_UV_MODE_PROBS);
10251027 }
10261028
10271029 /// Above subblock mode context (dixie `above_block_mode`): subblocks
10281030 /// 0-3 take the above MB's derived mode, inner subblocks the mode
10291031 /// `j-4` of this same MB (already decoded into `mb`).
1030- fn above_block_mode(&self, row: usize, col: usize, j: u8, mb: &MbInfo) -> u8 {
1032+ fn above_block_mode(&self, row: usize, col: usize, j: u8, mb: &MbInfo) -> modes::BMode {
10311033 if j < 4 {
10321034 let above = self.mbi(row - 1, col);
1033- if above.y_mode == modes::YMode::BPred.as_u8() {
1035+ if above.y_mode == modes::YMode::BPred {
10341036 above.b_modes[usize::from(j) + 12]
10351037 } else {
1036- ymode_as_bmode(above.y_mode)
1038+ above.y_mode.as_bmode()
10371039 }
10381040 } else {
10391041 mb.b_modes[usize::from(j) - 4]
@@ -1041,13 +1043,13 @@ impl Decoder {
10411043 }
10421044
10431045 /// Left subblock mode context (dixie `left_block_mode`).
1044- fn left_block_mode(&self, row: usize, col: usize, j: u8, mb: &MbInfo) -> u8 {
1046+ fn left_block_mode(&self, row: usize, col: usize, j: u8, mb: &MbInfo) -> modes::BMode {
10451047 if j.is_multiple_of(4) {
10461048 let left = self.mbi(row, col - 1);
1047- if left.y_mode == modes::YMode::BPred.as_u8() {
1049+ if left.y_mode == modes::YMode::BPred {
10481050 left.b_modes[usize::from(j) + 3]
10491051 } else {
1050- ymode_as_bmode(left.y_mode)
1052+ left.y_mode.as_bmode()
10511053 }
10521054 } else {
10531055 mb.b_modes[usize::from(j) - 1]
@@ -1083,7 +1085,7 @@ impl Decoder {
10831085 [0; 16]
10841086 };
10851087
1086- if y_mode == modes::YMode::BPred.as_u8() {
1088+ if y_mode == modes::YMode::BPred {
10871089 for b in 0..16usize {
10881090 let bx = (b % 4) * 4;
10891091 let by = (b / 4) * 4;
@@ -1105,7 +1107,7 @@ impl Decoder {
11051107 }
11061108 let p = self.y.data[self.y.at_above_left(x + bx, y + by)];
11071109 let mut block = [0u8; 16];
1108- intra::predict4(b_modes[b], &a8, &l4, p, &mut block)?;
1110+ intra::predict4(b_modes[b].as_u8(), &a8, &l4, p, &mut block)?;
11091111 for i in 0..4 {
11101112 let d = self.y.at(x + bx, y + by + i);
11111113 self.y.data[d..d + 4].copy_from_slice(&block[i * 4..i * 4 + 4]);
@@ -1135,7 +1137,7 @@ impl Decoder {
11351137 };
11361138 let mut pred = [0u8; 256];
11371139 intra::predict16(
1138- y_mode, have_above, have_left, &above, &left, above_left, &mut pred,
1140+ y_mode.as_u8(), have_above, have_left, &above, &left, above_left, &mut pred,
11391141 )?;
11401142 // loop indexes several parallel arrays; the index is clearer than zip chains
11411143 #[allow(clippy::needless_range_loop)]
@@ -1166,7 +1168,7 @@ impl Decoder {
11661168 let mut pred_u = [0u8; 64];
11671169 let mut pred_v = [0u8; 64];
11681170 intra::predict8(
1169- uv_mode,
1171+ uv_mode.as_u8(),
11701172 have_above,
11711173 have_left,
11721174 &above_u,
@@ -1175,7 +1177,7 @@ impl Decoder {
11751177 &mut pred_u,
11761178 )?;
11771179 intra::predict8(
1178- uv_mode,
1180+ uv_mode.as_u8(),
11791181 have_above,
11801182 have_left,
11811183 &above_v,
@@ -1242,6 +1244,11 @@ impl Decoder {
12421244 (a, l, al)
12431245 }
12441246
1247+ fn finish_frame(&mut self, header: &FrameHeader) -> Option<Picture> {
1248+ self.frame_decoded = true;
1249+ header.tag.show_frame.then(|| self.crop())
1250+ }
1251+
12451252 /// Crop the padded planes to the real frame size.
12461253 fn crop(&self) -> Picture {
12471254 let w = usize::from(self.dims.width);
@@ -1281,16 +1288,6 @@ impl Default for Decoder {
12811288 /// Segment-map coding tree (RFC 6386 §10): plain two-level split of 0-3.
12821289 const SEGMENT_TREE: [i8; 6] = [2, 4, 0, -1, -2, -3];
12831290
1284-fn ymode_as_bmode(y: u8) -> u8 {
1285- match y {
1286- 0 => 0, // DC_PRED -> B_DC_PRED
1287- 1 => 2, // V_PRED -> B_VE_PRED
1288- 2 => 3, // H_PRED -> B_HE_PRED
1289- 3 => 1, // TM_PRED -> B_TM_PRED
1290- _ => unreachable!("non-B_PRED luma mode {y}"),
1291- }
1292-}
1293-
12941291 /// Add one 4x4 block's dequantized-coefficient residual to its
12951292 /// prediction: reads the prediction patch from `pred`, clamps, writes
12961293 /// into the strided plane (§14.4/§14.5).
crates/ec-vp8/src/frame.rsmodified+4 -0
@@ -101,6 +101,10 @@ pub fn parse_keyframe_dims(data: &[u8]) -> Result<KeyFrameDims> {
101101 })
102102 }
103103
104+pub(crate) fn clamp255(v: i32) -> i32 {
105+ v.clamp(0, 255)
106+}
107+
104108 /// Number of 16×16 macroblock columns/rows covering `width × height`
105109 /// pixels (the encoded frame is padded up to whole macroblocks).
106110 pub fn mb_geometry(width: u16, height: u16) -> (usize, usize) {
crates/ec-vp8/src/intra.rsmodified+3 -9
@@ -30,6 +30,7 @@
3030 //! row); on the top frame row they are 127. [`predict4`] only consumes
3131 //! `A[0..8]` as given.
3232
33+use crate::frame::clamp255;
3334
3435 /// Weighted average of three adjacent pixels centered at `y`
3536 /// (RFC 6386 Section 12.3 `avg3`: `(x + 2y + z + 2) >> 2`; libvpx `AVG3`).
@@ -48,13 +49,6 @@ fn avg2(x: u8, y: u8) -> u8 {
4849 ((u32::from(x) + u32::from(y) + 1) >> 1) as u8
4950 }
5051
51-/// `clamp255(v)`: clamp an integer pixel difference into the valid `u8` range
52-/// (RFC 6386 Section 12.2 `TMpred` / Section 12.3 `B_TM_PRED`).
53-#[inline]
54-fn clamp255(v: i32) -> u8 {
55- v.clamp(0, 255) as u8
56-}
57-
5852 /// Shared DC/V/H/TM core for the 16x16 luma and 8x8 chroma blocks
5953 /// (RFC 6386 Section 12.2 semantics; Section 12.3 states 16x16 luma is
6054 /// "essentially identical" with 16-pixel edges).
@@ -106,7 +100,7 @@ fn predict_block<const N: usize>(
106100 3 => {
107101 for (row, &l) in out.as_chunks_mut::<N>().0.iter_mut().zip(left.iter()) {
108102 for (o, &a) in row.iter_mut().zip(above.iter()) {
109- *o = clamp255(i32::from(l) + i32::from(a) - i32::from(above_left));
103+ *o = clamp255(i32::from(l) + i32::from(a) - i32::from(above_left)) as u8;
110104 }
111105 }
112106 }
@@ -215,7 +209,7 @@ pub fn predict4(
215209 for r in 0..4 {
216210 for c in 0..4 {
217211 b[r * 4 + c] =
218- clamp255(i32::from(left[r]) + i32::from(above[c]) - i32::from(above_left));
212+ clamp255(i32::from(left[r]) + i32::from(above[c]) - i32::from(above_left)) as u8;
219213 }
220214 }
221215 }
crates/ec-vp8/src/mc.rsmodified+32 -67
@@ -23,6 +23,14 @@
2323 //! [RFC 6386]: https://www.rfc-editor.org/rfc/rfc6386
2424 use archmage::SimdToken;
2525 use multiversion::target::match_target;
26+use crate::frame::clamp255;
27+use crate::modes;
28+
29+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30+pub(crate) enum McMode {
31+ SixTap,
32+ Bilinear,
33+}
2634
2735 /// `VP8_FILTER_WEIGHT` (filter.c): tap weights sum to this, DC passes.
2836 const VP8_FILTER_WEIGHT: i32 = 128;
@@ -54,11 +62,6 @@ const BILINEAR_FILTERS: [[i32; 2]; 8] = [
5462 [16, 112],
5563 ];
5664
57-#[inline]
58-fn clamp255(v: i32) -> i32 {
59- v.clamp(0, 255)
60-}
61-
6265 /// Extends the visible `vis_w` x `vis_h` region of `plane` into the `border`
6366 /// ring on all four sides by edge replication: every out-of-image sample
6467 /// becomes the nearest in-image sample.
@@ -123,7 +126,7 @@ pub(crate) fn predict_luma(
123126 dst_stride: usize,
124127 bw: usize,
125128 bh: usize,
126- bilinear: bool,
129+ mode: McMode,
127130 ) {
128131 debug_assert!((4..=16).contains(&bw) && bw.is_multiple_of(4));
129132 debug_assert!((4..=16).contains(&bh) && bh.is_multiple_of(4));
@@ -141,7 +144,7 @@ pub(crate) fn predict_luma(
141144 // unclamped split MV may not, so the origin is brought back in range.
142145 let (sx, sy) = clamp_read_origin(sx, sy, stride, vis_h + 2 * border, bw, bh);
143146
144- if bilinear {
147+ if mode == McMode::Bilinear {
145148 bilinear_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
146149 } else {
147150 sixtap_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
@@ -682,7 +685,7 @@ fn bilinear_v_scalar(
682685 /// `vp8_setup_version` picks six-tap for version 0 (and reserved 4-7) and
683686 /// bilinear for versions 1-3 (decodeframe.c:851-860 assigns BOTH
684687 /// `subpixel_predict` and `subpixel_predict8x8` from that one switch, and
685-/// reconinter.c uses them for the chroma predicts). `bilinear` is that
688+/// reconinter.c uses them for the chroma predicts). `mode` is that
686689 /// per-frame selection.
687690 ///
688691 /// When `need_clamp` is set, the MV is first passed through
@@ -703,7 +706,7 @@ pub(crate) fn predict_chroma(
703706 dst_stride: usize,
704707 bw: usize,
705708 bh: usize,
706- bilinear: bool,
709+ mode: McMode,
707710 ) {
708711 debug_assert!((2..=16).contains(&bw) && bw.is_multiple_of(2));
709712 debug_assert!((2..=16).contains(&bh) && bh.is_multiple_of(2));
@@ -726,7 +729,7 @@ pub(crate) fn predict_chroma(
726729 // Chroma planes hold vis_h / 2 visible rows (`vis_*` are luma sizes).
727730 let (sx, sy) = clamp_read_origin(sx, sy, uv_stride, vis_h / 2 + 2 * border, bw, bh);
728731
729- if bilinear {
732+ if mode == McMode::Bilinear {
730733 bilinear_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
731734 } else {
732735 sixtap_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
@@ -755,29 +758,6 @@ fn clamp_read_origin(
755758 (sx.clamp(2, max_x) as usize, sy.clamp(2, max_y) as usize)
756759 }
757760
758-/// The macroblock-level UMV border edges in eighth-pel units, exactly the
759-/// decoder's `xd->mb_to_{left,right,top,bottom}_edge` (decodeframe.c:503-532):
760-///
761-/// ```c
762-/// xd->mb_to_top_edge = -((mb_row * 16) << 3);
763-/// xd->mb_to_bottom_edge = ((mb_rows - 1 - mb_row) * 16) << 3;
764-/// xd->mb_to_left_edge = -((mb_col * 16) << 3);
765-/// xd->mb_to_right_edge = ((mb_cols - 1 - mb_col) * 16) << 3;
766-/// ```
767-///
768-/// with `mb_rows`/`mb_cols` the full (partial MB inclusive) MB counts of the
769-/// visible frame. Returns `(left, right, top, bottom)`.
770-fn mb_edges(mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) -> (i32, i32, i32, i32) {
771- let mb_cols = (vis_w + 15) >> 4;
772- let mb_rows = (vis_h + 15) >> 4;
773- debug_assert!(mb_col < mb_cols && mb_row < mb_rows);
774- let left = -(((mb_col * 16) as i32) << 3);
775- let right = (((mb_cols - 1 - mb_col) * 16) as i32) << 3;
776- let top = -(((mb_row * 16) as i32) << 3);
777- let bottom = (((mb_rows - 1 - mb_row) * 16) as i32) << 3;
778- (left, right, top, bottom)
779-}
780-
781761 /// Port of `clamp_mv_to_umv_border` (reconinter.c:257-278), luma MV in
782762 /// eighth-pel units:
783763 ///
@@ -797,7 +777,7 @@ fn mb_edges(mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) -> (i32, i
797777 /// when `need_to_clamp_mvs` is set, for each SPLITMV subblock MV too
798778 /// (reconinter.c:370-375, 397-400).
799779 fn clamp_mv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) {
800- let (left, right, top, bottom) = mb_edges(mb_col, mb_row, vis_w, vis_h);
780+ let (left, right, top, bottom) = modes::mb_edges(mb_col, mb_row, (vis_w + 15) >> 4, (vis_h + 15) >> 4);
801781 if mv.1 < left - (19 << 3) {
802782 mv.1 = left - (16 << 3);
803783 } else if mv.1 > right + (18 << 3) {
@@ -836,7 +816,7 @@ fn clamp_mv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis
836816 /// (reconinter.c:336-339 — the 16x16 path clamps the chroma MV
837817 /// unconditionally for exactly this reason).
838818 fn clamp_uvmv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) {
839- let (left, right, top, bottom) = mb_edges(mb_col, mb_row, vis_w, vis_h);
819+ let (left, right, top, bottom) = modes::mb_edges(mb_col, mb_row, (vis_w + 15) >> 4, (vis_h + 15) >> 4);
840820
841821 if 2 * mv.1 < left - (19 << 3) {
842822 mv.1 = (left - (16 << 3)) >> 1;
@@ -943,8 +923,7 @@ mod tests {
943923 // mv.0 is the row component, mv.1 the column component. x=y=8 keeps
944924 // every read inside the visible region of the unextended plane.
945925 predict_luma(
946- &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16,
947- false);
926+ &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16, McMode::SixTap);
948927 for r in 0..16 {
949928 for c in 0..16 {
950929 assert_eq!(dst[r * 16 + c], vis(&p, 8 - 1 + c, 8 + 2 + r), "({r},{c})");
@@ -953,8 +932,7 @@ mod tests {
953932
954933 // Zero MV → identity copy.
955934 predict_luma(
956- &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16,
957- false);
935+ &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16, McMode::SixTap);
958936 for r in 0..16 {
959937 for c in 0..16 {
960938 assert_eq!(dst[r * 16 + c], vis(&p, 8 + c, 8 + r), "({r},{c})");
@@ -965,8 +943,7 @@ mod tests {
965943 // → full-pel offset (x+1, y-2).
966944 let mut dst8 = [0u8; 8 * 8];
967945 predict_chroma(
968- &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8,
969- false);
946+ &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8, McMode::SixTap);
970947 for r in 0..8 {
971948 for c in 0..8 {
972949 assert_eq!(dst8[r * 8 + c], vis(&p, 4 + 1 + c, 4 - 2 + r), "({r},{c})");
@@ -986,65 +963,53 @@ mod tests {
986963 // Luma: mv (-1000, -1000) < 0 - 152 → both components set to -128
987964 // (left - (16 << 3)); prediction must equal an explicit (-128, -128).
988965 predict_luma(
989- &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16,
990- false);
966+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16, McMode::SixTap);
991967 predict_luma(
992- &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,
993- false);
968+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, McMode::SixTap);
994969 assert_eq!(a, b);
995970
996971 // Strict threshold: (-152, -152) is NOT clamped (condition is strict
997972 // `<`), (-153, -153) IS clamped to (-128, -128).
998973 predict_luma(
999- &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16,
1000- false);
974+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16, McMode::SixTap);
1001975 predict_luma(
1002- &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16,
1003- false);
976+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16, McMode::SixTap);
1004977 assert_eq!(a, b);
1005978 predict_luma(
1006- &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16,
1007- false);
979+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16, McMode::SixTap);
1008980 predict_luma(
1009- &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,
1010- false);
981+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, McMode::SixTap);
1011982 assert_eq!(a, b);
1012983
1013984 // Right/bottom: mv (4000, 4000) > 0 + 144 → set to +128.
1014985 predict_luma(
1015- &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16,
1016- false);
986+ &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16, McMode::SixTap);
1017987 predict_luma(
1018- &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16,
1019- false);
988+ &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16, McMode::SixTap);
1020989 assert_eq!(a, b);
1021990
1022991 // Chroma: 2 * (-2000) < -152 → (-128 >> 1) = -64 per component.
1023992 let mut ca = [0u8; 8 * 8];
1024993 let mut cb = [0u8; 8 * 8];
1025994 predict_chroma(
1026- &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8,
1027- false);
995+ &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8, McMode::SixTap);
1028996 predict_chroma(
1029- &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8,
1030- false);
997+ &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8, McMode::SixTap);
1031998 assert_eq!(ca, cb);
1032999
10331000 // Chroma right/bottom: 2 * 2000 > 144 → (128 >> 1) = 64.
10341001 predict_chroma(
1035- &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8,
1036- false);
1002+ &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8, McMode::SixTap);
10371003 predict_chroma(
1038- &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8,
1039- false);
1004+ &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8, McMode::SixTap);
10401005 assert_eq!(ca, cb);
10411006
10421007 // An in-range MV with need_clamp=true must pass through untouched.
10431008 predict_luma(
1044- &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), true, &mut a, 16, 16, 16, false,
1009+ &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), true, &mut a, 16, 16, 16, McMode::SixTap,
10451010 );
10461011 predict_luma(
1047- &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), false, &mut b, 16, 16, 16, false,
1012+ &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), false, &mut b, 16, 16, 16, McMode::SixTap,
10481013 );
10491014 assert_eq!(a, b);
10501015 }
crates/ec-vp8/src/modes.rsmodified+52 -57
@@ -5,10 +5,11 @@ use crate::bool::BoolDecoder;
55
66 /// Intra macroblock luma modes (RFC 6386 §8.2 enum values are normative;
77 /// chroma modes are the first four).
8-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
99 #[repr(u8)]
1010 pub enum YMode {
1111 /// Predict DC from the row above and column to the left.
12+ #[default]
1213 DcPred = 0,
1314 /// Predict rows from the row above.
1415 VPred = 1,
@@ -40,10 +41,11 @@ impl YMode {
4041 }
4142
4243 /// The ten 4x4 subblock prediction modes (RFC 6386 §11.2).
43-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
4445 #[repr(u8)]
4546 pub enum BMode {
4647 /// DC from above row and left column.
48+ #[default]
4749 BDCPred = 0,
4850 /// True-motion.
4951 BTMPred = 1,
@@ -108,67 +110,58 @@ pub const BMODE_TREE: [i8; 18] = [
108110 /// array, no context (RFC 6386 §11.3 item 5 / §16).
109111 pub const INTER_BMODE_PROBS: [u8; 9] = [120, 90, 79, 133, 87, 85, 80, 111, 151];
110112
113+const YMODE_OF_LEAF: [YMode; 5] = [
114+ YMode::DcPred,
115+ YMode::VPred,
116+ YMode::HPred,
117+ YMode::TmPred,
118+ YMode::BPred,
119+];
120+
121+const BMODE_OF_LEAF: [BMode; 10] = [
122+ BMode::BDCPred,
123+ BMode::BTMPred,
124+ BMode::BVEPred,
125+ BMode::BHEPred,
126+ BMode::BLDPred,
127+ BMode::BRDPred,
128+ BMode::BVRPred,
129+ BMode::BVLPred,
130+ BMode::BHDPred,
131+ BMode::BHUPred,
132+];
133+
111134 /// Decode a key-frame Y mode (RFC 6386 §11.2).
112135 pub fn read_kf_ymode(d: &mut BoolDecoder<'_>) -> YMode {
113- YMode::from_u8(d.read_tree(&KF_YMODE_TREE, &KF_YMODE_PROBS))
136+ YMODE_OF_LEAF[usize::from(d.read_tree(&KF_YMODE_TREE, &KF_YMODE_PROBS))]
114137 }
115138
116139 /// Decode an interframe Y mode at the persisted probabilities
117140 /// (RFC 6386 §16.1; `probs` is the frame header's updated array).
118141 pub fn read_ymode(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> YMode {
119- YMode::from_u8(d.read_tree(&YMODE_TREE, probs))
142+ YMODE_OF_LEAF[usize::from(d.read_tree(&YMODE_TREE, probs))]
120143 }
121144
122145 /// Decode a chroma mode at the given (key-frame fixed or persisted
123146 /// interframe) probabilities.
124147 pub fn read_uvmode(d: &mut BoolDecoder<'_>, probs: &[u8; 3]) -> YMode {
125- YMode::from_u8(d.read_tree(&UV_MODE_TREE, probs))
148+ YMODE_OF_LEAF[usize::from(d.read_tree(&UV_MODE_TREE, probs))]
126149 }
127150
128151 /// Decode a 4x4 subblock mode at `probs` (RFC 6386 §11.3: in key frames
129152 /// the probabilities come from the above/left context table).
130153 pub fn read_bmode(d: &mut BoolDecoder<'_>, probs: &[u8]) -> BMode {
131- BMode::from_u8(d.read_tree(&BMODE_TREE, probs))
132-}
133-
134-impl YMode {
135- fn from_u8(v: u8) -> YMode {
136- match v {
137- 0 => YMode::DcPred,
138- 1 => YMode::VPred,
139- 2 => YMode::HPred,
140- 3 => YMode::TmPred,
141- 4 => YMode::BPred,
142- _ => unreachable!("tree cannot produce mode {v}"),
143- }
144- }
145-}
146-
147-impl BMode {
148- fn from_u8(v: u8) -> BMode {
149- match v {
150- 0 => BMode::BDCPred,
151- 1 => BMode::BTMPred,
152- 2 => BMode::BVEPred,
153- 3 => BMode::BHEPred,
154- 4 => BMode::BLDPred,
155- 5 => BMode::BRDPred,
156- 6 => BMode::BVRPred,
157- 7 => BMode::BVLPred,
158- 8 => BMode::BHDPred,
159- 9 => BMode::BHUPred,
160- _ => unreachable!("tree cannot produce bmode {v}"),
161- }
162- }
154+ BMODE_OF_LEAF[usize::from(d.read_tree(&BMODE_TREE, probs))]
163155 }
164156
165157 /// Inter-frame motion-vector reference modes (RFC 6386 §16.2), offset
166158 /// from the intra modes by `num_ymodes` in the reference enum; here they
167159 /// are their own small enum with the RFC's tree integers.
168-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
160+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
169161 #[repr(u8)]
170162 pub enum MvRef {
171163 /// Zero motion vector ("10" second branch; coded 0).
164+ #[default]
172165 Zero = 0,
173166 /// "Nearest" vector (coded 1).
174167 Nearest = 1,
@@ -184,6 +177,14 @@ pub enum MvRef {
184177 /// mv_ref coding tree (RFC 6386 §16.2); leaves are [`MvRef`] integers.
185178 pub const MV_REF_TREE: [i8; 8] = [0, 2, -1, 4, -2, 6, -3, -4];
186179
180+const MV_REF_OF_LEAF: [MvRef; 5] = [
181+ MvRef::Zero,
182+ MvRef::Nearest,
183+ MvRef::Near,
184+ MvRef::New,
185+ MvRef::Split,
186+];
187+
187188 /// Motion-vector component: row then column, each with the 19-probability
188189 /// table (§17.1).
189190 /// Probability-table offsets of one MV component (RFC 6386 §17.1).
@@ -255,6 +256,13 @@ pub enum SubMvRef {
255256 /// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers.
256257 pub const SUB_MV_REF_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];
257258
259+const SUB_MV_REF_OF_LEAF: [SubMvRef; 4] = [
260+ SubMvRef::Left4x4,
261+ SubMvRef::Above4x4,
262+ SubMvRef::Zero4x4,
263+ SubMvRef::New4x4,
264+];
265+
258266 /// Context-conditioned sub-MV probabilities (§16.4 `sub_mv_ref_prob`).
259267 pub const SUB_MV_REF_PROBS: [[u8; 3]; 5] = [
260268 [147, 136, 18],
@@ -297,14 +305,7 @@ pub const MODE_CONTEXTS: [[u8; 4]; 6] = [
297305
298306 /// Decode an mv_ref at the census-derived probabilities (§16.2/§16.3).
299307 pub fn read_mv_ref(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> MvRef {
300- match d.read_tree(&MV_REF_TREE, probs) {
301- 0 => MvRef::Zero,
302- 1 => MvRef::Nearest,
303- 2 => MvRef::Near,
304- 3 => MvRef::New,
305- 4 => MvRef::Split,
306- v => unreachable!("mv_ref tree produced {v}"),
307- }
308+ MV_REF_OF_LEAF[usize::from(d.read_tree(&MV_REF_TREE, probs))]
308309 }
309310
310311 /// Decode a SPLITMV partition shape (§16.4); returns the `MVpartition`
@@ -315,13 +316,7 @@ pub fn read_mv_partition(d: &mut BoolDecoder<'_>) -> usize {
315316
316317 /// Decode a sub-MV reference (§16.4) at the context's probabilities.
317318 pub fn read_sub_mv_ref(d: &mut BoolDecoder<'_>, ctx: usize) -> SubMvRef {
318- match d.read_tree(&SUB_MV_REF_TREE, &SUB_MV_REF_PROBS[ctx]) {
319- 0 => SubMvRef::Left4x4,
320- 1 => SubMvRef::Above4x4,
321- 2 => SubMvRef::Zero4x4,
322- 3 => SubMvRef::New4x4,
323- v => unreachable!("sub_mv_ref tree produced {v}"),
324- }
319+ SUB_MV_REF_OF_LEAF[usize::from(d.read_tree(&SUB_MV_REF_TREE, &SUB_MV_REF_PROBS[ctx]))]
325320 }
326321
327322 /// First subblock of each SPLITMV subset, per partition shape (libvpx
@@ -485,10 +480,10 @@ pub fn find_near_mvs(
485480 /// `mb_to_*_edge`): distance from the MB's edge to the frame edge.
486481 pub fn mb_edges(col: usize, row: usize, mb_cols: usize, mb_rows: usize) -> (i32, i32, i32, i32) {
487482 (
488- -((i32::try_from(col).unwrap() * 16) << 3),
489- ((i32::try_from(mb_cols - 1 - col).unwrap() * 16) << 3),
490- -((i32::try_from(row).unwrap() * 16) << 3),
491- ((i32::try_from(mb_rows - 1 - row).unwrap() * 16) << 3),
483+ -(((col * 16) as i32) << 3),
484+ (((mb_cols - 1 - col) * 16) as i32) << 3,
485+ -(((row * 16) as i32) << 3),
486+ (((mb_rows - 1 - row) * 16) as i32) << 3,
492487 )
493488 }
494489