| 1 | use super::super::vp78data::*; |
| 2 | pub use crate::codecs::vpenc::models::*; |
| 3 | |
| 4 | #[derive(Clone,Copy)] |
| 5 | pub struct VP7Models { |
| 6 | pub coef_probs: [[[[u8; 11]; 3]; 8]; 4], |
| 7 | pub mv_probs: [[u8; 17]; 2], |
| 8 | pub kf_ymode_prob: [u8; 4], |
| 9 | pub kf_uvmode_prob: [u8; 3], |
| 10 | pub prob_intra_pred: u8, |
| 11 | pub prob_last_pred: u8, |
| 12 | pub feature_present: [u8; 4], |
| 13 | pub feature_tree_probs: [[u8; 3]; 4], |
| 14 | } |
| 15 | |
| 16 | const DEFAULT_MV_PROBS: [[u8; 17]; 2] = [ |
| 17 | [ 162, 128, 225, 146, 172, 147, 214, 39, 156, 247, 210, 135, 68, 138, 220, 239, 246 ], |
| 18 | [ 164, 128, 204, 170, 119, 235, 140, 230, 228, 244, 184, 201, 44, 173, 221, 239, 253 ] |
| 19 | ]; |
| 20 | |
| 21 | impl VP7Models { |
| 22 | pub fn new() -> Self { |
| 23 | let mut obj: Self = unsafe { std::mem::zeroed() }; |
| 24 | obj.reset(); |
| 25 | obj |
| 26 | } |
| 27 | pub fn reset(&mut self) { |
| 28 | self.coef_probs.copy_from_slice(&DEFAULT_DCT_PROBS); |
| 29 | self.mv_probs.copy_from_slice(&DEFAULT_MV_PROBS); |
| 30 | self.kf_ymode_prob.copy_from_slice(Y_MODE_TREE_PROBS); |
| 31 | self.kf_uvmode_prob.copy_from_slice(UV_MODE_TREE_PROBS); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub trait VP7ProbCounter { |
| 36 | fn to_prob8(self) -> u8; |
| 37 | fn to_prob8_worthy(&self, ref_prob: &mut u8); |
| 38 | } |
| 39 | |
| 40 | impl VP7ProbCounter for ProbCounter { |
| 41 | fn to_prob8(self) -> u8 { |
| 42 | if self.total > 0 { |
| 43 | ((self.zeroes << 8) / self.total).min(255).max(1) as u8 |
| 44 | } else { |
| 45 | 128 |
| 46 | } |
| 47 | } |
| 48 | fn to_prob8_worthy(&self, ref_prob: &mut u8) { |
| 49 | if self.total > 0 { |
| 50 | let new_prob = self.to_prob(); |
| 51 | let new_bits = Self::est_bits(new_prob, self.zeroes, self.total); |
| 52 | let old_bits = Self::est_bits(*ref_prob, self.zeroes, self.total); |
| 53 | |
| 54 | if new_bits + 8 < old_bits { |
| 55 | *ref_prob = new_prob; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[derive(Clone,Copy,Default)] |
| 62 | pub struct VP7ModelsStat { |
| 63 | pub coef_probs: [[[[ProbCounter; 11]; 3]; 8]; 4], |
| 64 | pub mv_probs: [[ProbCounter; 17]; 2], |
| 65 | pub kf_ymode_prob: [ProbCounter; 4], |
| 66 | pub kf_uvmode_prob: [ProbCounter; 3], |
| 67 | pub prob_intra_pred: ProbCounter, |
| 68 | pub prob_last_pred: ProbCounter, |
| 69 | pub feature_present: [ProbCounter; 4], |
| 70 | pub feature_tree_probs: [[ProbCounter; 3]; 4], |
| 71 | } |
| 72 | |
| 73 | impl VP7ModelsStat { |
| 74 | pub fn new() -> Self { Self::default() } |
| 75 | pub fn reset(&mut self) { |
| 76 | *self = Self::default(); |
| 77 | } |
| 78 | pub fn generate(&self, dst: &mut VP7Models, is_intra: bool) { |
| 79 | for (dst, src) in dst.feature_present.iter_mut().zip(self.feature_present.iter()) { |
| 80 | *dst = src.to_prob8(); |
| 81 | } |
| 82 | for (dst, src) in dst.feature_tree_probs.iter_mut().zip(self.feature_tree_probs.iter()) { |
| 83 | for (dst, src) in dst.iter_mut().zip(src.iter()) { |
| 84 | if src.total != 0 { |
| 85 | *dst = src.to_prob8(); |
| 86 | } else { |
| 87 | *dst = 255; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | for (dst, src) in dst.coef_probs.iter_mut().zip(self.coef_probs.iter()) { |
| 92 | for (dst, src) in dst.iter_mut().zip(src.iter()) { |
| 93 | for (dst, src) in dst.iter_mut().zip(src.iter()) { |
| 94 | for (dst, src) in dst.iter_mut().zip(src.iter()) { |
| 95 | src.to_prob8_worthy(dst); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if !is_intra { |
| 102 | dst.prob_intra_pred = self.prob_intra_pred.to_prob8(); |
| 103 | dst.prob_last_pred = self.prob_last_pred.to_prob8(); |
| 104 | |
| 105 | for (dmv, smv) in dst.mv_probs.iter_mut().zip(self.mv_probs.iter()) { |
| 106 | for (dp, sp) in dmv.iter_mut().zip(smv.iter()) { |
| 107 | *dp = sp.to_prob_worthy(*dp); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | for (dp, sp) in dst.kf_ymode_prob.iter_mut().zip(self.kf_ymode_prob.iter()) { |
| 112 | sp.to_prob8_worthy(dp); |
| 113 | } |
| 114 | for (dp, sp) in dst.kf_uvmode_prob.iter_mut().zip(self.kf_uvmode_prob.iter()) { |
| 115 | sp.to_prob8_worthy(dp); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |