1 use nihav_codec_support::codecs::ZIGZAG;
2 use super::super::vp6data::*;
3 use crate::codecs::vpenc::models::*;
4 pub use crate::codecs::vpenc::models::PROB_BITS;
6 #[derive(Clone,Copy,Default)]
7 pub struct VP56MVModel {
10 pub raw_probs: [u8; 8],
11 pub tree_probs: [u8; 7],
14 #[derive(Clone,Copy,Default)]
15 pub struct VP56MBTypeModel {
19 #[derive(Clone,Copy,Default)]
20 pub struct VP56CoeffModel {
21 pub dc_token_probs: [[[u8; 5]; 6]; 6],
22 pub dc_value_probs: [u8; 11],
23 pub ac_val_probs: [[[u8; 11]; 6]; 3],
27 pub struct VP6Models {
28 pub scan_order: [usize; 64],
29 pub scan: [usize; 64],
30 pub zigzag: [usize; 64],
31 pub zero_run_probs: [[u8; 14]; 2],
34 const MAX_HUFF_ELEMS: usize = 12;
35 #[derive(Clone,Copy,Default)]
37 pub codes: [u16; MAX_HUFF_ELEMS],
38 pub bits: [u8; MAX_HUFF_ELEMS],
41 #[derive(Clone,Copy,Default)]
49 fn prob2weight(a: u8, b: u8) -> u8 {
50 let w = ((u16::from(a) * u16::from(b)) >> 8) as u8;
59 pub fn build_codes(&mut self, probs: &[u8; 11]) {
60 let mut weights = [0u8; 12];
62 weights[11] = prob2weight( probs[0], probs[ 1]);
63 weights[ 0] = prob2weight( probs[0], !probs[ 1]);
64 weights[ 1] = prob2weight(!probs[0], probs[ 2]);
65 let lvroot = prob2weight(!probs[0], !probs[ 2]);
66 let tworoot = prob2weight( lvroot, probs[ 3]);
67 let hlroot = prob2weight( lvroot, !probs[ 3]);
68 weights[ 2] = prob2weight( tworoot, probs[ 4]);
69 let root34 = prob2weight( tworoot, !probs[ 4]);
70 weights[ 3] = prob2weight( root34, probs[ 5]);
71 weights[ 4] = prob2weight( root34, !probs[ 5]);
72 let c1root = prob2weight( hlroot, probs[ 6]);
73 let c34root = prob2weight( hlroot, !probs[ 6]);
74 weights[ 5] = prob2weight( c1root, probs[ 7]);
75 weights[ 6] = prob2weight( c1root, !probs[ 7]);
76 let c3root = prob2weight( c34root, probs[ 8]);
77 let c4root = prob2weight( c34root, !probs[ 8]);
78 weights[ 7] = prob2weight( c3root, probs[ 9]);
79 weights[ 8] = prob2weight( c3root, !probs[ 9]);
80 weights[ 9] = prob2weight( c4root, probs[10]);
81 weights[10] = prob2weight( c4root, !probs[10]);
85 pub fn build_codes_zero_run(&mut self, probs: &[u8; 14]) {
86 let mut weights = [0u8; 9];
88 let root = prob2weight( probs[0], probs[1]);
89 weights[0] = prob2weight( root, probs[2]);
90 weights[1] = prob2weight( root, !probs[2]);
92 let root = prob2weight( probs[0], !probs[1]);
93 weights[2] = prob2weight( root, probs[3]);
94 weights[3] = prob2weight( root, !probs[3]);
96 let root = prob2weight(!probs[0], probs[4]);
97 weights[8] = prob2weight(!probs[0], !probs[4]);
98 let root1 = prob2weight( root, probs[5]);
99 let root2 = prob2weight( root, !probs[5]);
100 weights[4] = prob2weight( root1, probs[6]);
101 weights[5] = prob2weight( root1, !probs[6]);
102 weights[6] = prob2weight( root2, probs[7]);
103 weights[7] = prob2weight( root2, !probs[7]);
105 self.build(&weights);
107 fn build(&mut self, weights: &[u8]) {
108 let mut nodes = [Node::default(); MAX_HUFF_ELEMS * 2];
111 for w in weights.iter().rev() {
112 let weight = u16::from(*w);
115 if nodes[i].weight > weight {
120 for j in (pos..nlen).rev() {
121 nodes[j + 1] = nodes[j];
123 nodes[pos].weight = weight;
124 nodes[pos].sym = (weights.len() - nlen - 1) as i8;
131 weight: nodes[low + 0].weight + nodes[low + 1].weight,
138 while (pos < nlen) && (nodes[pos].weight < nnode.weight) {
141 for j in (pos..nlen).rev() {
142 nodes[j + 1] = nodes[j];
147 self.get_codes(&nodes, nlen - 1, 0, 0);
148 for i in nlen..self.codes.len() {
149 self.codes[i] = self.codes[0];
150 self.bits[i] = self.bits[0];
153 fn get_codes(&mut self, nodes: &[Node], pos: usize, code: u16, len: u8) {
154 if nodes[pos].sym >= 0 {
155 self.codes[nodes[pos].sym as usize] = code;
156 self.bits [nodes[pos].sym as usize] = len;
158 self.get_codes(nodes, nodes[pos].ch0, (code << 1) | 0, len + 1);
159 self.get_codes(nodes, nodes[pos].ch1, (code << 1) | 1, len + 1);
164 #[derive(Clone,Copy,Default)]
165 pub struct VP6HuffModels {
166 pub dc_token_tree: [VP6Huff; 2],
167 pub ac_token_tree: [[[VP6Huff; 6]; 3]; 2],
168 pub zero_run_tree: [VP6Huff; 2],
177 zero_run_probs: [[0; 14]; 2],
183 pub struct VP56Models {
184 pub mv_models: [VP56MVModel; 2],
185 pub mbtype_models: [[VP56MBTypeModel; 10]; 3],
186 pub coeff_models: [VP56CoeffModel; 2],
187 pub prob_xmitted: [[u8; 20]; 3],
188 pub vp6models: VP6Models,
189 pub vp6huff: VP6HuffModels,
193 pub fn new() -> Self {
195 mv_models: [VP56MVModel::default(); 2],
196 mbtype_models: [[VP56MBTypeModel::default(); 10]; 3],
197 coeff_models: [VP56CoeffModel::default(); 2],
198 prob_xmitted: [[0; 20]; 3],
199 vp6models: VP6Models::new(),
200 vp6huff: VP6HuffModels::default(),
203 pub fn reset(&mut self, interlaced: bool) {
204 for (i, mdl) in self.mv_models.iter_mut().enumerate() {
205 mdl.nz_prob = NZ_PROBS[i];
207 mdl.raw_probs.copy_from_slice(&RAW_PROBS[i]);
208 mdl.tree_probs.copy_from_slice(&TREE_PROBS[i]);
211 for mdl in self.coeff_models.iter_mut() {
212 mdl.dc_value_probs = [128; 11];
213 mdl.ac_val_probs = [[[128; 11]; 6]; 3];
215 self.vp6models.zero_run_probs.copy_from_slice(&ZERO_RUN_PROBS);
216 reset_scan(&mut self.vp6models, interlaced);
218 pub fn reset_mbtype_models(&mut self) {
219 const DEFAULT_XMITTED_PROBS: [[u8; 20]; 3] = [
220 [ 42, 69, 2, 1, 7, 1, 42, 44, 22, 6, 3, 1, 2, 0, 5, 1, 1, 0, 0, 0 ],
221 [ 8, 229, 1, 1, 8, 0, 0, 0, 0, 0, 2, 1, 1, 0, 0, 0, 1, 1, 0, 0 ],
222 [ 35, 122, 1, 1, 6, 1, 34, 46, 0, 0, 2, 1, 1, 0, 1, 0, 1, 1, 0, 0 ]
224 self.prob_xmitted.copy_from_slice(&DEFAULT_XMITTED_PROBS);
228 pub fn reset_scan(model: &mut VP6Models, interlaced: bool) {
230 model.scan_order.copy_from_slice(&VP6_DEFAULT_SCAN_ORDER);
232 model.scan_order.copy_from_slice(&VP6_INTERLACED_SCAN_ORDER);
234 for i in 0..64 { model.scan[i] = i; }
235 model.zigzag.copy_from_slice(&ZIGZAG);
238 #[derive(Clone,Copy,Default)]
239 pub struct VP56MVModelStat {
240 pub nz_prob: ProbCounter,
241 pub sign_prob: ProbCounter,
242 pub raw_probs: [ProbCounter; 8],
243 pub tree_probs: [ProbCounter; 7],
246 #[derive(Clone,Copy,Default)]
247 pub struct VP56CoeffModelStat {
248 pub dc_token_probs: [[[ProbCounter; 5]; 6]; 6],
249 pub dc_value_probs: [ProbCounter; 11],
250 pub ac_val_probs: [[[ProbCounter; 11]; 6]; 3],
254 pub struct VP6ModelsStat {
255 pub zero_run_probs: [[ProbCounter; 14]; 2],
258 pub struct VP56ModelsStat {
259 pub mv_models: [VP56MVModelStat; 2],
260 pub mbtype_models: [[[usize; 10]; 10]; 3],
261 pub coeff_models: [VP56CoeffModelStat; 2],
262 pub vp6models: VP6ModelsStat,
265 impl VP56ModelsStat {
266 pub fn new() -> Self {
268 mv_models: [VP56MVModelStat::default(); 2],
269 mbtype_models: [[[0; 10]; 10]; 3],
270 coeff_models: [VP56CoeffModelStat::default(); 2],
271 vp6models: VP6ModelsStat::default(),
274 pub fn reset(&mut self) {
275 self.mv_models = [VP56MVModelStat::default(); 2];
276 self.mbtype_models = [[[0; 10]; 10]; 3];
277 self.coeff_models = [VP56CoeffModelStat::default(); 2];
278 self.vp6models = VP6ModelsStat::default();
280 pub fn generate(&self, dst: &mut VP56Models, is_intra: bool) {
282 for (dmv, smv) in dst.mv_models.iter_mut().zip(self.mv_models.iter()) {
283 dmv.nz_prob = smv.nz_prob.to_prob_worthy(dmv.nz_prob);
284 dmv.sign_prob = smv.sign_prob.to_prob_worthy(dmv.sign_prob);
285 for (dp, sp) in dmv.raw_probs.iter_mut().zip(smv.raw_probs.iter()) {
286 *dp = sp.to_prob_worthy(*dp);
288 for (dp, sp) in dmv.tree_probs.iter_mut().zip(smv.tree_probs.iter()) {
289 *dp = sp.to_prob_worthy(*dp);
292 for (xmit, mdl) in dst.prob_xmitted.iter_mut().zip(self.mbtype_models.iter()) {
293 Self::generate_prob_xmitted(xmit, mdl);
296 for (dmv, smv) in dst.coeff_models.iter_mut().zip(self.coeff_models.iter()) {
297 for (dp, sp) in dmv.dc_value_probs.iter_mut().zip(smv.dc_value_probs.iter()) {
298 *dp = sp.to_prob_worthy(*dp);
300 for (dp, sp) in dmv.ac_val_probs.iter_mut().zip(smv.ac_val_probs.iter()) {
301 for (dp, sp) in dp.iter_mut().zip(sp.iter()) {
302 for (dp, sp) in dp.iter_mut().zip(sp.iter()) {
303 *dp = sp.to_prob_worthy(*dp);
308 for (dp, sp) in dst.vp6models.zero_run_probs.iter_mut().zip(self.vp6models.zero_run_probs.iter()) {
309 for (dp, sp) in dp.iter_mut().zip(sp.iter()) {
310 *dp = sp.to_prob_worthy(*dp);
315 VPMBType::InterNoMV => 0,
316 VPMBType::Intra => 1,
317 VPMBType::InterMV => 2,
318 VPMBType::InterNearest => 3,
319 VPMBType::InterNear => 4,
320 VPMBType::GoldenNoMV => 5,
321 VPMBType::GoldenMV => 6,
322 VPMBType::InterFourMV => 7,
323 VPMBType::GoldenNearest => 8,
324 VPMBType::GoldenNear => 9,
326 fn generate_prob_xmitted(probs: &mut [u8; 20], mbtype: &[[usize; 10]; 10]) {
327 let mut sums = [0; 20];
329 for (last, row) in mbtype.iter().enumerate() {
330 for (cur, &count) in row.iter().enumerate() {
332 sums[cur * 2 + 1] = count;
334 sums[cur * 2] += count;
341 for (dprob, &sprob) in probs.iter_mut().zip(sums.iter()) {
343 *dprob = ((sprob * 256 + total - 1) / total).min(255) as u8;
344 sum += u16::from(*dprob);
350 for prob in probs.iter_mut() {