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 {
95 #[derive(Clone, Default)]95 #[derive(Clone, Default)]
96 struct MbInfo {96 struct MbInfo {
97 /// [`modes::YMode`] as its coded integer.97 /// [`modes::YMode`] as its coded integer.
98- y_mode: u8,98+ y_mode: modes::YMode,
99- uv_mode: u8,99+ uv_mode: modes::YMode,
100- b_modes: [u8; 16],100+ b_modes: [modes::BMode; 16],
101 segment_id: u8,101 segment_id: u8,
102 skip: bool,102 skip: bool,
103 /// 0 = intra, 1 = last, 2 = golden, 3 = altref (§16.2). Key-frame103 /// 0 = intra, 1 = last, 2 = golden, 3 = altref (§16.2). Key-frame
@@ -105,7 +105,7 @@ struct MbInfo {
105 ref_frame: u8,105 ref_frame: u8,
106 /// Inter mv_ref leaf (§16.2): 0 = zero, 1 = nearest, 2 = near,106 /// Inter mv_ref leaf (§16.2): 0 = zero, 1 = nearest, 2 = near,
107 /// 3 = new, 4 = split. 0 when intra.107 /// 3 = new, 4 = split. 0 when intra.
108- mv_ref: u8,108+ mv_ref: modes::MvRef,
109 /// Whole-MB motion vector in eighth-pels; the bottom-right subblock's109 /// Whole-MB motion vector in eighth-pels; the bottom-right subblock's
110 /// vector for SPLITMV (what future neighbours' census sees).110 /// vector for SPLITMV (what future neighbours' census sees).
111 mv: (i16, i16),111 mv: (i16, i16),
@@ -135,7 +135,7 @@ const MC_BORDER: usize = 32;
135 135
136 impl MbInfo {136 impl MbInfo {
137 fn has_y2(&self) -> bool {137 fn has_y2(&self) -> bool {
138- self.y_mode != modes::YMode::BPred.as_u8()138+ self.y_mode != modes::YMode::BPred
139 }139 }
140 }140 }
141 141
@@ -166,7 +166,7 @@ pub struct Decoder {
166 /// six-tap filters, versions 1-3 the bilinear ones) and full-pel166 /// six-tap filters, versions 1-3 the bilinear ones) and full-pel
167 /// flag (version 3: `fullpixel_mask = ~7`). The decoder never reads167 /// flag (version 3: `fullpixel_mask = ~7`). The decoder never reads
168 /// `no_lpf` — dead there too.168 /// `no_lpf` — dead there too.
169- mc_bilinear: bool,169+ mc_mode: mc::McMode,
170 mc_fullpel: bool,170 mc_fullpel: bool,
171 }171 }
172 172
@@ -219,7 +219,7 @@ impl Decoder {
219 refs: [None, None, None, None],219 refs: [None, None, None, None],
220 segment_map: Vec::new(),220 segment_map: Vec::new(),
221 frame_decoded: false,221 frame_decoded: false,
222- mc_bilinear: false,222+ mc_mode: mc::McMode::SixTap,
223 mc_fullpel: false,223 mc_fullpel: false,
224 }224 }
225 }225 }
@@ -268,7 +268,11 @@ impl Decoder {
268 // vp8_setup_version (decodeframe.c:935): the tag's version bits268 // vp8_setup_version (decodeframe.c:935): the tag's version bits
269 // pick the MC interpolation filter and full-pel mode for this269 // pick the MC interpolation filter and full-pel mode for this
270 // frame (see the field docs on Decoder).270 // 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+ };
272 self.mc_fullpel = header.tag.version == 3;276 self.mc_fullpel = header.tag.version == 3;
273 let refresh_entropy = header.refresh.refresh_entropy;277 let refresh_entropy = header.refresh.refresh_entropy;
274 let pic = match header.tag.frame_type {278 let pic = match header.tag.frame_type {
@@ -438,8 +442,7 @@ impl Decoder {
438 self.refs[2] = Some(rf.clone());442 self.refs[2] = Some(rf.clone());
439 self.refs[3] = Some(rf);443 self.refs[3] = Some(rf);
440 444
441- self.frame_decoded = true;445+ Ok(self.finish_frame(&header))
442- Ok(header.tag.show_frame.then(|| self.crop()))
443 }446 }
444 447
445 /// Loop-filter post-pass over the whole frame (dixie order-equivalent448 /// Loop-filter post-pass over the whole frame (dixie order-equivalent
@@ -513,15 +516,15 @@ impl Decoder {
513 if header.lf_delta_enabled {516 if header.lf_delta_enabled {
514 level += self.state.ref_lf_delta[usize::from(mb.ref_frame)];517 level += self.state.ref_lf_delta[usize::from(mb.ref_frame)];
515 let mode_idx = if mb.ref_frame == 0 {518 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 {
517 Some(0)520 Some(0)
518 } else {521 } else {
519 None522 None
520 }523 }
521 } else {524 } else {
522 match mb.mv_ref {525 match mb.mv_ref {
523- 0 => Some(1), // ZEROMV526+ modes::MvRef::Zero => Some(1), // ZEROMV
524- 4 => Some(3), // SPLITMV527+ modes::MvRef::Split => Some(3), // SPLITMV
525 _ => Some(2), // NEAREST / NEAR / NEW528 _ => Some(2), // NEAREST / NEAR / NEW
526 }529 }
527 };530 };
@@ -698,8 +701,7 @@ impl Decoder {
698 self.refs[1] = Some(new);701 self.refs[1] = Some(new);
699 }702 }
700 703
701- self.frame_decoded = true;704+ Ok(self.finish_frame(&header))
702- Ok(header.tag.show_frame.then(|| self.crop()))
703 }705 }
704 706
705 /// Inter-frame mode record (§16, libvpx `read_mb_modes_mv`): intra707 /// Inter-frame mode record (§16, libvpx `read_mb_modes_mv`): intra
@@ -725,13 +727,13 @@ impl Decoder {
725 // context-dependent kf_bmode_prob table is key-frame-only).727 // context-dependent kf_bmode_prob table is key-frame-only).
726 mb.ref_frame = 0;728 mb.ref_frame = 0;
727 let ymode = modes::read_ymode(d, ymode_probs);729 let ymode = modes::read_ymode(d, ymode_probs);
728- mb.y_mode = ymode.as_u8();730+ mb.y_mode = ymode;
729 if ymode == modes::YMode::BPred {731 if ymode == modes::YMode::BPred {
730 for j in 0..16usize {732 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);
732 }734 }
733 }735 }
734- mb.uv_mode = modes::read_uvmode(d, uv_mode_probs).as_u8();736+ mb.uv_mode = modes::read_uvmode(d, uv_mode_probs);
735 return;737 return;
736 }738 }
737 739
@@ -751,17 +753,17 @@ impl Decoder {
751 let (mut near_mvs, cnt) = modes::find_near_mvs(above, left, aboveleft, to_bias);753 let (mut near_mvs, cnt) = modes::find_near_mvs(above, left, aboveleft, to_bias);
752 754
753 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_INTRA] as usize][0]) {755 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_INTRA] as usize][0]) {
754- mb.mv_ref = 0; // ZEROMV756+ mb.mv_ref = modes::MvRef::Zero;
755 return;757 return;
756 }758 }
757 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAREST] as usize][1]) {759 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAREST] as usize][1]) {
758- mb.mv_ref = 1; // NEARESTMV760+ mb.mv_ref = modes::MvRef::Nearest;
759 mb.mv = near_mvs[modes::CNT_NEAREST];761 mb.mv = near_mvs[modes::CNT_NEAREST];
760 modes::clamp_mv2(&mut mb.mv, edges);762 modes::clamp_mv2(&mut mb.mv, edges);
761 return;763 return;
762 }764 }
763 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAR] as usize][2]) {765 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAR] as usize][2]) {
764- mb.mv_ref = 2; // NEARMV766+ mb.mv_ref = modes::MvRef::Near;
765 mb.mv = near_mvs[modes::CNT_NEAR];767 mb.mv = near_mvs[modes::CNT_NEAR];
766 modes::clamp_mv2(&mut mb.mv, edges);768 modes::clamp_mv2(&mut mb.mv, edges);
767 return;769 return;
@@ -774,7 +776,7 @@ impl Decoder {
774 modes::clamp_mv2(&mut near_mvs[near_index], edges);776 modes::clamp_mv2(&mut near_mvs[near_index], edges);
775 let best = near_mvs[near_index];777 let best = near_mvs[near_index];
776 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_SPLITMV] as usize][3]) {778 if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_SPLITMV] as usize][3]) {
777- mb.mv_ref = 3; // NEWMV779+ mb.mv_ref = modes::MvRef::New;
778 let mut mv = modes::read_mv(d, &self.state.mv_probs);780 let mut mv = modes::read_mv(d, &self.state.mv_probs);
779 mv.0 = mv.0.wrapping_add(best.0);781 mv.0 = mv.0.wrapping_add(best.0);
780 mv.1 = mv.1.wrapping_add(best.1);782 mv.1 = mv.1.wrapping_add(best.1);
@@ -802,7 +804,7 @@ impl Decoder {
802 intra: false,804 intra: false,
803 zero: mb.mv == (0, 0),805 zero: mb.mv == (0, 0),
804 mv: mb.mv,806 mv: mb.mv,
805- is_split: mb.mv_ref == 4,807+ is_split: mb.mv_ref == modes::MvRef::Split,
806 sign_bias: sign_bias[usize::from(mb.ref_frame) - 1],808 sign_bias: sign_bias[usize::from(mb.ref_frame) - 1],
807 }809 }
808 }810 }
@@ -826,7 +828,7 @@ impl Decoder {
826 let k = usize::from(modes::MBSPLIT_OFFSET[s][j]);828 let k = usize::from(modes::MBSPLIT_OFFSET[s][j]);
827 let leftmv = if k % 4 == 0 {829 let leftmv = if k % 4 == 0 {
828 let lm = self.mbi(row, col - 1);830 let lm = self.mbi(row, col - 1);
829- if lm.mv_ref == 4 {831+ if lm.mv_ref == modes::MvRef::Split {
830 lm.bmi[k + 3]832 lm.bmi[k + 3]
831 } else {833 } else {
832 lm.mv834 lm.mv
@@ -836,7 +838,7 @@ impl Decoder {
836 };838 };
837 let abovemv = if k < 4 {839 let abovemv = if k < 4 {
838 let am = self.mbi(row - 1, col);840 let am = self.mbi(row - 1, col);
839- if am.mv_ref == 4 {841+ if am.mv_ref == modes::MvRef::Split {
840 am.bmi[k + 12]842 am.bmi[k + 12]
841 } else {843 } else {
842 am.mv844 am.mv
@@ -862,9 +864,9 @@ impl Decoder {
862 mb.bmi[usize::from(off)] = blockmv;864 mb.bmi[usize::from(off)] = blockmv;
863 }865 }
864 }866 }
865- mb.mv_ref = 4;867+ mb.mv_ref = modes::MvRef::Split;
866 // 4x4 residual coding without a Y2 block (like B_PRED).868 // 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;
868 // Neighbours' census sees the bottom-right subblock's vector.870 // Neighbours' census sees the bottom-right subblock's vector.
869 mb.mv = mb.bmi[15];871 mb.mv = mb.bmi[15];
870 }872 }
@@ -903,7 +905,7 @@ impl Decoder {
903 // SPLITMV (identical output to libvpx's grouped predicts). Each905 // SPLITMV (identical output to libvpx's grouped predicts). Each
904 // predict lands at its own (bx, by) slot of one 16x16 patch.906 // predict lands at its own (bx, by) slot of one 16x16 patch.
905 let mut py = [0u8; 256];907 let mut py = [0u8; 256];
906- if mb.mv_ref == 4 {908+ if mb.mv_ref == modes::MvRef::Split {
907 for b in 0..16usize {909 for b in 0..16usize {
908 let (bx, by) = ((b % 4) * 4, (b / 4) * 4);910 let (bx, by) = ((b % 4) * 4, (b / 4) * 4);
909 // reconinter.c:370-374: the four corner subblocks clamp911 // reconinter.c:370-374: the four corner subblocks clamp
@@ -912,13 +914,13 @@ impl Decoder {
912 mc::predict_luma(914 mc::predict_luma(
913 &reff.y, reff.stride, MC_BORDER, w, h, x + bx, y + by, mb.bmi[b],915 &reff.y, reff.stride, MC_BORDER, w, h, x + bx, y + by, mb.bmi[b],
914 mb.mv_clamp && corner,916 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,
916 );918 );
917 }919 }
918 } else {920 } else {
919 mc::predict_luma(921 mc::predict_luma(
920 &reff.y, reff.stride, MC_BORDER, w, h, x, y, mb.mv, mb.mv_clamp, &mut py, 16,922 &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,
922 );924 );
923 }925 }
924 926
@@ -932,7 +934,7 @@ impl Decoder {
932 // clamps only under need_to_clamp_mvs.934 // clamps only under need_to_clamp_mvs.
933 let mut pu = [0u8; 256];935 let mut pu = [0u8; 256];
934 let mut pv = [0u8; 256];936 let mut pv = [0u8; 256];
935- if mb.mv_ref == 4 {937+ if mb.mv_ref == modes::MvRef::Split {
936 for q in 0..4usize {938 for q in 0..4usize {
937 // Quadrant q: 4x4 chroma pixels at (qx4, qy4) in the MB.939 // Quadrant q: 4x4 chroma pixels at (qx4, qy4) in the MB.
938 let (qx4, qy4) = ((q % 2) * 4, (q / 2) * 4);940 let (qx4, qy4) = ((q % 2) * 4, (q / 2) * 4);
@@ -950,23 +952,23 @@ impl Decoder {
950 mc::predict_chroma(952 mc::predict_chroma(
951 &reff.u, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv,953 &reff.u, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv,
952 mb.mv_clamp, &mut pu[qy4 * 16 + qx4..], 16, 4, 4,954 mb.mv_clamp, &mut pu[qy4 * 16 + qx4..], 16, 4, 4,
953- self.mc_bilinear,955+ self.mc_mode,
954 );956 );
955 mc::predict_chroma(957 mc::predict_chroma(
956 &reff.v, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv,958 &reff.v, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv,
957 mb.mv_clamp, &mut pv[qy4 * 16 + qx4..], 16, 4, 4,959 mb.mv_clamp, &mut pv[qy4 * 16 + qx4..], 16, 4, 4,
958- self.mc_bilinear,960+ self.mc_mode,
959 );961 );
960 }962 }
961 } else {963 } else {
962 let uvmv = modes::chroma_mv_whole(mb.mv, self.mc_fullpel);964 let uvmv = modes::chroma_mv_whole(mb.mv, self.mc_fullpel);
963 mc::predict_chroma(965 mc::predict_chroma(
964 &reff.u, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true,966 &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,
966 );968 );
967 mc::predict_chroma(969 mc::predict_chroma(
968 &reff.v, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true,970 &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,
970 );972 );
971 }973 }
972 974
@@ -1012,28 +1014,28 @@ impl Decoder {
1012 mb: &mut MbInfo,1014 mb: &mut MbInfo,
1013 ) {1015 ) {
1014 let ymode = modes::read_kf_ymode(d);1016 let ymode = modes::read_kf_ymode(d);
1015- mb.y_mode = ymode.as_u8();1017+ mb.y_mode = ymode;
1016 if ymode == modes::YMode::BPred {1018 if ymode == modes::YMode::BPred {
1017 for j in 0..16u8 {1019 for j in 0..16u8 {
1018 let a = self.above_block_mode(row, col, j, mb);1020 let a = self.above_block_mode(row, col, j, mb);
1019 let l = self.left_block_mode(row, col, j, mb);1021 let l = self.left_block_mode(row, col, j, mb);
1020 mb.b_modes[usize::from(j)] =1022 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())]);
1022 }1024 }
1023 }1025 }
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);
1025 }1027 }
1026 1028
1027 /// Above subblock mode context (dixie `above_block_mode`): subblocks1029 /// Above subblock mode context (dixie `above_block_mode`): subblocks
1028 /// 0-3 take the above MB's derived mode, inner subblocks the mode1030 /// 0-3 take the above MB's derived mode, inner subblocks the mode
1029 /// `j-4` of this same MB (already decoded into `mb`).1031 /// `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 {
1031 if j < 4 {1033 if j < 4 {
1032 let above = self.mbi(row - 1, col);1034 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 {
1034 above.b_modes[usize::from(j) + 12]1036 above.b_modes[usize::from(j) + 12]
1035 } else {1037 } else {
1036- ymode_as_bmode(above.y_mode)1038+ above.y_mode.as_bmode()
1037 }1039 }
1038 } else {1040 } else {
1039 mb.b_modes[usize::from(j) - 4]1041 mb.b_modes[usize::from(j) - 4]
@@ -1041,13 +1043,13 @@ impl Decoder {
1041 }1043 }
1042 1044
1043 /// Left subblock mode context (dixie `left_block_mode`).1045 /// 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 {
1045 if j.is_multiple_of(4) {1047 if j.is_multiple_of(4) {
1046 let left = self.mbi(row, col - 1);1048 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 {
1048 left.b_modes[usize::from(j) + 3]1050 left.b_modes[usize::from(j) + 3]
1049 } else {1051 } else {
1050- ymode_as_bmode(left.y_mode)1052+ left.y_mode.as_bmode()
1051 }1053 }
1052 } else {1054 } else {
1053 mb.b_modes[usize::from(j) - 1]1055 mb.b_modes[usize::from(j) - 1]
@@ -1083,7 +1085,7 @@ impl Decoder {
1083 [0; 16]1085 [0; 16]
1084 };1086 };
1085 1087
1086- if y_mode == modes::YMode::BPred.as_u8() {1088+ if y_mode == modes::YMode::BPred {
1087 for b in 0..16usize {1089 for b in 0..16usize {
1088 let bx = (b % 4) * 4;1090 let bx = (b % 4) * 4;
1089 let by = (b / 4) * 4;1091 let by = (b / 4) * 4;
@@ -1105,7 +1107,7 @@ impl Decoder {
1105 }1107 }
1106 let p = self.y.data[self.y.at_above_left(x + bx, y + by)];1108 let p = self.y.data[self.y.at_above_left(x + bx, y + by)];
1107 let mut block = [0u8; 16];1109 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)?;
1109 for i in 0..4 {1111 for i in 0..4 {
1110 let d = self.y.at(x + bx, y + by + i);1112 let d = self.y.at(x + bx, y + by + i);
1111 self.y.data[d..d + 4].copy_from_slice(&block[i * 4..i * 4 + 4]);1113 self.y.data[d..d + 4].copy_from_slice(&block[i * 4..i * 4 + 4]);
@@ -1135,7 +1137,7 @@ impl Decoder {
1135 };1137 };
1136 let mut pred = [0u8; 256];1138 let mut pred = [0u8; 256];
1137 intra::predict16(1139 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,
1139 )?;1141 )?;
1140 // loop indexes several parallel arrays; the index is clearer than zip chains1142 // loop indexes several parallel arrays; the index is clearer than zip chains
1141 #[allow(clippy::needless_range_loop)]1143 #[allow(clippy::needless_range_loop)]
@@ -1166,7 +1168,7 @@ impl Decoder {
1166 let mut pred_u = [0u8; 64];1168 let mut pred_u = [0u8; 64];
1167 let mut pred_v = [0u8; 64];1169 let mut pred_v = [0u8; 64];
1168 intra::predict8(1170 intra::predict8(
1169- uv_mode,1171+ uv_mode.as_u8(),
1170 have_above,1172 have_above,
1171 have_left,1173 have_left,
1172 &above_u,1174 &above_u,
@@ -1175,7 +1177,7 @@ impl Decoder {
1175 &mut pred_u,1177 &mut pred_u,
1176 )?;1178 )?;
1177 intra::predict8(1179 intra::predict8(
1178- uv_mode,1180+ uv_mode.as_u8(),
1179 have_above,1181 have_above,
1180 have_left,1182 have_left,
1181 &above_v,1183 &above_v,
@@ -1242,6 +1244,11 @@ impl Decoder {
1242 (a, l, al)1244 (a, l, al)
1243 }1245 }
1244 1246
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+
1245 /// Crop the padded planes to the real frame size.1252 /// Crop the padded planes to the real frame size.
1246 fn crop(&self) -> Picture {1253 fn crop(&self) -> Picture {
1247 let w = usize::from(self.dims.width);1254 let w = usize::from(self.dims.width);
@@ -1281,16 +1288,6 @@ impl Default for Decoder {
1281 /// Segment-map coding tree (RFC 6386 §10): plain two-level split of 0-3.1288 /// Segment-map coding tree (RFC 6386 §10): plain two-level split of 0-3.
1282 const SEGMENT_TREE: [i8; 6] = [2, 4, 0, -1, -2, -3];1289 const SEGMENT_TREE: [i8; 6] = [2, 4, 0, -1, -2, -3];
1283 1290
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-
1294 /// Add one 4x4 block's dequantized-coefficient residual to its1291 /// Add one 4x4 block's dequantized-coefficient residual to its
1295 /// prediction: reads the prediction patch from `pred`, clamps, writes1292 /// prediction: reads the prediction patch from `pred`, clamps, writes
1296 /// into the strided plane (§14.4/§14.5).1293 /// 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> {
101 })101 })
102 }102 }
103 103
104+pub(crate) fn clamp255(v: i32) -> i32 {
105+ v.clamp(0, 255)
106+}
107+
104 /// Number of 16×16 macroblock columns/rows covering `width × height`108 /// Number of 16×16 macroblock columns/rows covering `width × height`
105 /// pixels (the encoded frame is padded up to whole macroblocks).109 /// pixels (the encoded frame is padded up to whole macroblocks).
106 pub fn mb_geometry(width: u16, height: u16) -> (usize, usize) {110 pub fn mb_geometry(width: u16, height: u16) -> (usize, usize) {
crates/ec-vp8/src/intra.rsmodified+3 -9
@@ -30,6 +30,7 @@
30 //! row); on the top frame row they are 127. [`predict4`] only consumes30 //! row); on the top frame row they are 127. [`predict4`] only consumes
31 //! `A[0..8]` as given.31 //! `A[0..8]` as given.
32 32
33+use crate::frame::clamp255;
33 34
34 /// Weighted average of three adjacent pixels centered at `y`35 /// Weighted average of three adjacent pixels centered at `y`
35 /// (RFC 6386 Section 12.3 `avg3`: `(x + 2y + z + 2) >> 2`; libvpx `AVG3`).36 /// (RFC 6386 Section 12.3 `avg3`: `(x + 2y + z + 2) >> 2`; libvpx `AVG3`).
@@ -48,13 +49,6 @@ fn avg2(x: u8, y: u8) -> u8 {
48 ((u32::from(x) + u32::from(y) + 1) >> 1) as u849 ((u32::from(x) + u32::from(y) + 1) >> 1) as u8
49 }50 }
50 51
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-
58 /// Shared DC/V/H/TM core for the 16x16 luma and 8x8 chroma blocks52 /// Shared DC/V/H/TM core for the 16x16 luma and 8x8 chroma blocks
59 /// (RFC 6386 Section 12.2 semantics; Section 12.3 states 16x16 luma is53 /// (RFC 6386 Section 12.2 semantics; Section 12.3 states 16x16 luma is
60 /// "essentially identical" with 16-pixel edges).54 /// "essentially identical" with 16-pixel edges).
@@ -106,7 +100,7 @@ fn predict_block<const N: usize>(
106 3 => {100 3 => {
107 for (row, &l) in out.as_chunks_mut::<N>().0.iter_mut().zip(left.iter()) {101 for (row, &l) in out.as_chunks_mut::<N>().0.iter_mut().zip(left.iter()) {
108 for (o, &a) in row.iter_mut().zip(above.iter()) {102 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;
110 }104 }
111 }105 }
112 }106 }
@@ -215,7 +209,7 @@ pub fn predict4(
215 for r in 0..4 {209 for r in 0..4 {
216 for c in 0..4 {210 for c in 0..4 {
217 b[r * 4 + c] =211 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;
219 }213 }
220 }214 }
221 }215 }
crates/ec-vp8/src/mc.rsmodified+32 -67
@@ -23,6 +23,14 @@
23 //! [RFC 6386]: https://www.rfc-editor.org/rfc/rfc638623 //! [RFC 6386]: https://www.rfc-editor.org/rfc/rfc6386
24 use archmage::SimdToken;24 use archmage::SimdToken;
25 use multiversion::target::match_target;25 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+}
26 34
27 /// `VP8_FILTER_WEIGHT` (filter.c): tap weights sum to this, DC passes.35 /// `VP8_FILTER_WEIGHT` (filter.c): tap weights sum to this, DC passes.
28 const VP8_FILTER_WEIGHT: i32 = 128;36 const VP8_FILTER_WEIGHT: i32 = 128;
@@ -54,11 +62,6 @@ const BILINEAR_FILTERS: [[i32; 2]; 8] = [
54 [16, 112],62 [16, 112],
55 ];63 ];
56 64
57-#[inline]
58-fn clamp255(v: i32) -> i32 {
59- v.clamp(0, 255)
60-}
61-
62 /// Extends the visible `vis_w` x `vis_h` region of `plane` into the `border`65 /// Extends the visible `vis_w` x `vis_h` region of `plane` into the `border`
63 /// ring on all four sides by edge replication: every out-of-image sample66 /// ring on all four sides by edge replication: every out-of-image sample
64 /// becomes the nearest in-image sample.67 /// becomes the nearest in-image sample.
@@ -123,7 +126,7 @@ pub(crate) fn predict_luma(
123 dst_stride: usize,126 dst_stride: usize,
124 bw: usize,127 bw: usize,
125 bh: usize,128 bh: usize,
126- bilinear: bool,129+ mode: McMode,
127 ) {130 ) {
128 debug_assert!((4..=16).contains(&bw) && bw.is_multiple_of(4));131 debug_assert!((4..=16).contains(&bw) && bw.is_multiple_of(4));
129 debug_assert!((4..=16).contains(&bh) && bh.is_multiple_of(4));132 debug_assert!((4..=16).contains(&bh) && bh.is_multiple_of(4));
@@ -141,7 +144,7 @@ pub(crate) fn predict_luma(
141 // unclamped split MV may not, so the origin is brought back in range.144 // unclamped split MV may not, so the origin is brought back in range.
142 let (sx, sy) = clamp_read_origin(sx, sy, stride, vis_h + 2 * border, bw, bh);145 let (sx, sy) = clamp_read_origin(sx, sy, stride, vis_h + 2 * border, bw, bh);
143 146
144- if bilinear {147+ if mode == McMode::Bilinear {
145 bilinear_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);148 bilinear_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
146 } else {149 } else {
147 sixtap_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);150 sixtap_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
@@ -682,7 +685,7 @@ fn bilinear_v_scalar(
682 /// `vp8_setup_version` picks six-tap for version 0 (and reserved 4-7) and685 /// `vp8_setup_version` picks six-tap for version 0 (and reserved 4-7) and
683 /// bilinear for versions 1-3 (decodeframe.c:851-860 assigns BOTH686 /// bilinear for versions 1-3 (decodeframe.c:851-860 assigns BOTH
684 /// `subpixel_predict` and `subpixel_predict8x8` from that one switch, and687 /// `subpixel_predict` and `subpixel_predict8x8` from that one switch, and
685-/// reconinter.c uses them for the chroma predicts). `bilinear` is that688+/// reconinter.c uses them for the chroma predicts). `mode` is that
686 /// per-frame selection.689 /// per-frame selection.
687 ///690 ///
688 /// When `need_clamp` is set, the MV is first passed through691 /// When `need_clamp` is set, the MV is first passed through
@@ -703,7 +706,7 @@ pub(crate) fn predict_chroma(
703 dst_stride: usize,706 dst_stride: usize,
704 bw: usize,707 bw: usize,
705 bh: usize,708 bh: usize,
706- bilinear: bool,709+ mode: McMode,
707 ) {710 ) {
708 debug_assert!((2..=16).contains(&bw) && bw.is_multiple_of(2));711 debug_assert!((2..=16).contains(&bw) && bw.is_multiple_of(2));
709 debug_assert!((2..=16).contains(&bh) && bh.is_multiple_of(2));712 debug_assert!((2..=16).contains(&bh) && bh.is_multiple_of(2));
@@ -726,7 +729,7 @@ pub(crate) fn predict_chroma(
726 // Chroma planes hold vis_h / 2 visible rows (`vis_*` are luma sizes).729 // Chroma planes hold vis_h / 2 visible rows (`vis_*` are luma sizes).
727 let (sx, sy) = clamp_read_origin(sx, sy, uv_stride, vis_h / 2 + 2 * border, bw, bh);730 let (sx, sy) = clamp_read_origin(sx, sy, uv_stride, vis_h / 2 + 2 * border, bw, bh);
728 731
729- if bilinear {732+ if mode == McMode::Bilinear {
730 bilinear_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);733 bilinear_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
731 } else {734 } else {
732 sixtap_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);735 sixtap_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
@@ -755,29 +758,6 @@ fn clamp_read_origin(
755 (sx.clamp(2, max_x) as usize, sy.clamp(2, max_y) as usize)758 (sx.clamp(2, max_x) as usize, sy.clamp(2, max_y) as usize)
756 }759 }
757 760
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-
781 /// Port of `clamp_mv_to_umv_border` (reconinter.c:257-278), luma MV in761 /// Port of `clamp_mv_to_umv_border` (reconinter.c:257-278), luma MV in
782 /// eighth-pel units:762 /// eighth-pel units:
783 ///763 ///
@@ -797,7 +777,7 @@ fn mb_edges(mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) -> (i32, i
797 /// when `need_to_clamp_mvs` is set, for each SPLITMV subblock MV too777 /// when `need_to_clamp_mvs` is set, for each SPLITMV subblock MV too
798 /// (reconinter.c:370-375, 397-400).778 /// (reconinter.c:370-375, 397-400).
799 fn clamp_mv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) {779 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);
801 if mv.1 < left - (19 << 3) {781 if mv.1 < left - (19 << 3) {
802 mv.1 = left - (16 << 3);782 mv.1 = left - (16 << 3);
803 } else if mv.1 > right + (18 << 3) {783 } 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
836 /// (reconinter.c:336-339 — the 16x16 path clamps the chroma MV816 /// (reconinter.c:336-339 — the 16x16 path clamps the chroma MV
837 /// unconditionally for exactly this reason).817 /// unconditionally for exactly this reason).
838 fn clamp_uvmv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) {818 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);
840 820
841 if 2 * mv.1 < left - (19 << 3) {821 if 2 * mv.1 < left - (19 << 3) {
842 mv.1 = (left - (16 << 3)) >> 1;822 mv.1 = (left - (16 << 3)) >> 1;
@@ -943,8 +923,7 @@ mod tests {
943 // mv.0 is the row component, mv.1 the column component. x=y=8 keeps923 // mv.0 is the row component, mv.1 the column component. x=y=8 keeps
944 // every read inside the visible region of the unextended plane.924 // every read inside the visible region of the unextended plane.
945 predict_luma(925 predict_luma(
946- &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16,926+ &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16, McMode::SixTap);
947- false);
948 for r in 0..16 {927 for r in 0..16 {
949 for c in 0..16 {928 for c in 0..16 {
950 assert_eq!(dst[r * 16 + c], vis(&p, 8 - 1 + c, 8 + 2 + r), "({r},{c})");929 assert_eq!(dst[r * 16 + c], vis(&p, 8 - 1 + c, 8 + 2 + r), "({r},{c})");
@@ -953,8 +932,7 @@ mod tests {
953 932
954 // Zero MV → identity copy.933 // Zero MV → identity copy.
955 predict_luma(934 predict_luma(
956- &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16,935+ &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16, McMode::SixTap);
957- false);
958 for r in 0..16 {936 for r in 0..16 {
959 for c in 0..16 {937 for c in 0..16 {
960 assert_eq!(dst[r * 16 + c], vis(&p, 8 + c, 8 + r), "({r},{c})");938 assert_eq!(dst[r * 16 + c], vis(&p, 8 + c, 8 + r), "({r},{c})");
@@ -965,8 +943,7 @@ mod tests {
965 // → full-pel offset (x+1, y-2).943 // → full-pel offset (x+1, y-2).
966 let mut dst8 = [0u8; 8 * 8];944 let mut dst8 = [0u8; 8 * 8];
967 predict_chroma(945 predict_chroma(
968- &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8,946+ &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8, McMode::SixTap);
969- false);
970 for r in 0..8 {947 for r in 0..8 {
971 for c in 0..8 {948 for c in 0..8 {
972 assert_eq!(dst8[r * 8 + c], vis(&p, 4 + 1 + c, 4 - 2 + r), "({r},{c})");949 assert_eq!(dst8[r * 8 + c], vis(&p, 4 + 1 + c, 4 - 2 + r), "({r},{c})");
@@ -986,65 +963,53 @@ mod tests {
986 // Luma: mv (-1000, -1000) < 0 - 152 → both components set to -128963 // Luma: mv (-1000, -1000) < 0 - 152 → both components set to -128
987 // (left - (16 << 3)); prediction must equal an explicit (-128, -128).964 // (left - (16 << 3)); prediction must equal an explicit (-128, -128).
988 predict_luma(965 predict_luma(
989- &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16,966+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16, McMode::SixTap);
990- false);
991 predict_luma(967 predict_luma(
992- &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,968+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, McMode::SixTap);
993- false);
994 assert_eq!(a, b);969 assert_eq!(a, b);
995 970
996 // Strict threshold: (-152, -152) is NOT clamped (condition is strict971 // Strict threshold: (-152, -152) is NOT clamped (condition is strict
997 // `<`), (-153, -153) IS clamped to (-128, -128).972 // `<`), (-153, -153) IS clamped to (-128, -128).
998 predict_luma(973 predict_luma(
999- &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16,974+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16, McMode::SixTap);
1000- false);
1001 predict_luma(975 predict_luma(
1002- &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16,976+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16, McMode::SixTap);
1003- false);
1004 assert_eq!(a, b);977 assert_eq!(a, b);
1005 predict_luma(978 predict_luma(
1006- &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16,979+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16, McMode::SixTap);
1007- false);
1008 predict_luma(980 predict_luma(
1009- &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,981+ &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, McMode::SixTap);
1010- false);
1011 assert_eq!(a, b);982 assert_eq!(a, b);
1012 983
1013 // Right/bottom: mv (4000, 4000) > 0 + 144 → set to +128.984 // Right/bottom: mv (4000, 4000) > 0 + 144 → set to +128.
1014 predict_luma(985 predict_luma(
1015- &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16,986+ &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16, McMode::SixTap);
1016- false);
1017 predict_luma(987 predict_luma(
1018- &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16,988+ &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16, McMode::SixTap);
1019- false);
1020 assert_eq!(a, b);989 assert_eq!(a, b);
1021 990
1022 // Chroma: 2 * (-2000) < -152 → (-128 >> 1) = -64 per component.991 // Chroma: 2 * (-2000) < -152 → (-128 >> 1) = -64 per component.
1023 let mut ca = [0u8; 8 * 8];992 let mut ca = [0u8; 8 * 8];
1024 let mut cb = [0u8; 8 * 8];993 let mut cb = [0u8; 8 * 8];
1025 predict_chroma(994 predict_chroma(
1026- &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8,995+ &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8, McMode::SixTap);
1027- false);
1028 predict_chroma(996 predict_chroma(
1029- &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8,997+ &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8, McMode::SixTap);
1030- false);
1031 assert_eq!(ca, cb);998 assert_eq!(ca, cb);
1032 999
1033 // Chroma right/bottom: 2 * 2000 > 144 → (128 >> 1) = 64.1000 // Chroma right/bottom: 2 * 2000 > 144 → (128 >> 1) = 64.
1034 predict_chroma(1001 predict_chroma(
1035- &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8,1002+ &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8, McMode::SixTap);
1036- false);
1037 predict_chroma(1003 predict_chroma(
1038- &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8,1004+ &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8, McMode::SixTap);
1039- false);
1040 assert_eq!(ca, cb);1005 assert_eq!(ca, cb);
1041 1006
1042 // An in-range MV with need_clamp=true must pass through untouched.1007 // An in-range MV with need_clamp=true must pass through untouched.
1043 predict_luma(1008 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,
1045 );1010 );
1046 predict_luma(1011 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,
1048 );1013 );
1049 assert_eq!(a, b);1014 assert_eq!(a, b);
1050 }1015 }
crates/ec-vp8/src/modes.rsmodified+52 -57
@@ -5,10 +5,11 @@ use crate::bool::BoolDecoder;
5 5
6 /// Intra macroblock luma modes (RFC 6386 §8.2 enum values are normative;6 /// Intra macroblock luma modes (RFC 6386 §8.2 enum values are normative;
7 /// chroma modes are the first four).7 /// chroma modes are the first four).
8-#[derive(Debug, Clone, Copy, PartialEq, Eq)]8+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
9 #[repr(u8)]9 #[repr(u8)]
10 pub enum YMode {10 pub enum YMode {
11 /// Predict DC from the row above and column to the left.11 /// Predict DC from the row above and column to the left.
12+ #[default]
12 DcPred = 0,13 DcPred = 0,
13 /// Predict rows from the row above.14 /// Predict rows from the row above.
14 VPred = 1,15 VPred = 1,
@@ -40,10 +41,11 @@ impl YMode {
40 }41 }
41 42
42 /// The ten 4x4 subblock prediction modes (RFC 6386 §11.2).43 /// 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)]
44 #[repr(u8)]45 #[repr(u8)]
45 pub enum BMode {46 pub enum BMode {
46 /// DC from above row and left column.47 /// DC from above row and left column.
48+ #[default]
47 BDCPred = 0,49 BDCPred = 0,
48 /// True-motion.50 /// True-motion.
49 BTMPred = 1,51 BTMPred = 1,
@@ -108,67 +110,58 @@ pub const BMODE_TREE: [i8; 18] = [
108 /// array, no context (RFC 6386 §11.3 item 5 / §16).110 /// array, no context (RFC 6386 §11.3 item 5 / §16).
109 pub const INTER_BMODE_PROBS: [u8; 9] = [120, 90, 79, 133, 87, 85, 80, 111, 151];111 pub const INTER_BMODE_PROBS: [u8; 9] = [120, 90, 79, 133, 87, 85, 80, 111, 151];
110 112
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+
111 /// Decode a key-frame Y mode (RFC 6386 §11.2).134 /// Decode a key-frame Y mode (RFC 6386 §11.2).
112 pub fn read_kf_ymode(d: &mut BoolDecoder<'_>) -> YMode {135 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))]
114 }137 }
115 138
116 /// Decode an interframe Y mode at the persisted probabilities139 /// Decode an interframe Y mode at the persisted probabilities
117 /// (RFC 6386 §16.1; `probs` is the frame header's updated array).140 /// (RFC 6386 §16.1; `probs` is the frame header's updated array).
118 pub fn read_ymode(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> YMode {141 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))]
120 }143 }
121 144
122 /// Decode a chroma mode at the given (key-frame fixed or persisted145 /// Decode a chroma mode at the given (key-frame fixed or persisted
123 /// interframe) probabilities.146 /// interframe) probabilities.
124 pub fn read_uvmode(d: &mut BoolDecoder<'_>, probs: &[u8; 3]) -> YMode {147 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))]
126 }149 }
127 150
128 /// Decode a 4x4 subblock mode at `probs` (RFC 6386 §11.3: in key frames151 /// Decode a 4x4 subblock mode at `probs` (RFC 6386 §11.3: in key frames
129 /// the probabilities come from the above/left context table).152 /// the probabilities come from the above/left context table).
130 pub fn read_bmode(d: &mut BoolDecoder<'_>, probs: &[u8]) -> BMode {153 pub fn read_bmode(d: &mut BoolDecoder<'_>, probs: &[u8]) -> BMode {
131- BMode::from_u8(d.read_tree(&BMODE_TREE, probs))154+ BMODE_OF_LEAF[usize::from(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- }
163 }155 }
164 156
165 /// Inter-frame motion-vector reference modes (RFC 6386 §16.2), offset157 /// Inter-frame motion-vector reference modes (RFC 6386 §16.2), offset
166 /// from the intra modes by `num_ymodes` in the reference enum; here they158 /// from the intra modes by `num_ymodes` in the reference enum; here they
167 /// are their own small enum with the RFC's tree integers.159 /// 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)]
169 #[repr(u8)]161 #[repr(u8)]
170 pub enum MvRef {162 pub enum MvRef {
171 /// Zero motion vector ("10" second branch; coded 0).163 /// Zero motion vector ("10" second branch; coded 0).
164+ #[default]
172 Zero = 0,165 Zero = 0,
173 /// "Nearest" vector (coded 1).166 /// "Nearest" vector (coded 1).
174 Nearest = 1,167 Nearest = 1,
@@ -184,6 +177,14 @@ pub enum MvRef {
184 /// mv_ref coding tree (RFC 6386 §16.2); leaves are [`MvRef`] integers.177 /// mv_ref coding tree (RFC 6386 §16.2); leaves are [`MvRef`] integers.
185 pub const MV_REF_TREE: [i8; 8] = [0, 2, -1, 4, -2, 6, -3, -4];178 pub const MV_REF_TREE: [i8; 8] = [0, 2, -1, 4, -2, 6, -3, -4];
186 179
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+
187 /// Motion-vector component: row then column, each with the 19-probability188 /// Motion-vector component: row then column, each with the 19-probability
188 /// table (§17.1).189 /// table (§17.1).
189 /// Probability-table offsets of one MV component (RFC 6386 §17.1).190 /// Probability-table offsets of one MV component (RFC 6386 §17.1).
@@ -255,6 +256,13 @@ pub enum SubMvRef {
255 /// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers.256 /// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers.
256 pub const SUB_MV_REF_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];257 pub const SUB_MV_REF_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];
257 258
259+const SUB_MV_REF_OF_LEAF: [SubMvRef; 4] = [
260+ SubMvRef::Left4x4,
261+ SubMvRef::Above4x4,
262+ SubMvRef::Zero4x4,
263+ SubMvRef::New4x4,
264+];
265+
258 /// Context-conditioned sub-MV probabilities (§16.4 `sub_mv_ref_prob`).266 /// Context-conditioned sub-MV probabilities (§16.4 `sub_mv_ref_prob`).
259 pub const SUB_MV_REF_PROBS: [[u8; 3]; 5] = [267 pub const SUB_MV_REF_PROBS: [[u8; 3]; 5] = [
260 [147, 136, 18],268 [147, 136, 18],
@@ -297,14 +305,7 @@ pub const MODE_CONTEXTS: [[u8; 4]; 6] = [
297 305
298 /// Decode an mv_ref at the census-derived probabilities (§16.2/§16.3).306 /// Decode an mv_ref at the census-derived probabilities (§16.2/§16.3).
299 pub fn read_mv_ref(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> MvRef {307 pub fn read_mv_ref(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> MvRef {
300- match d.read_tree(&MV_REF_TREE, probs) {308+ MV_REF_OF_LEAF[usize::from(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 }309 }
309 310
310 /// Decode a SPLITMV partition shape (§16.4); returns the `MVpartition`311 /// Decode a SPLITMV partition shape (§16.4); returns the `MVpartition`
@@ -315,13 +316,7 @@ pub fn read_mv_partition(d: &mut BoolDecoder<'_>) -> usize {
315 316
316 /// Decode a sub-MV reference (§16.4) at the context's probabilities.317 /// Decode a sub-MV reference (§16.4) at the context's probabilities.
317 pub fn read_sub_mv_ref(d: &mut BoolDecoder<'_>, ctx: usize) -> SubMvRef {318 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+ SUB_MV_REF_OF_LEAF[usize::from(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- }
325 }320 }
326 321
327 /// First subblock of each SPLITMV subset, per partition shape (libvpx322 /// First subblock of each SPLITMV subset, per partition shape (libvpx
@@ -485,10 +480,10 @@ pub fn find_near_mvs(
485 /// `mb_to_*_edge`): distance from the MB's edge to the frame edge.480 /// `mb_to_*_edge`): distance from the MB's edge to the frame edge.
486 pub fn mb_edges(col: usize, row: usize, mb_cols: usize, mb_rows: usize) -> (i32, i32, i32, i32) {481 pub fn mb_edges(col: usize, row: usize, mb_cols: usize, mb_rows: usize) -> (i32, i32, i32, i32) {
487 (482 (
488- -((i32::try_from(col).unwrap() * 16) << 3),483+ -(((col * 16) as i32) << 3),
489- ((i32::try_from(mb_cols - 1 - col).unwrap() * 16) << 3),484+ (((mb_cols - 1 - col) * 16) as i32) << 3,
490- -((i32::try_from(row).unwrap() * 16) << 3),485+ -(((row * 16) as i32) << 3),
491- ((i32::try_from(mb_rows - 1 - row).unwrap() * 16) << 3),486+ (((mb_rows - 1 - row) * 16) as i32) << 3,
492 )487 )
493 }488 }
494 489