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