1 use nihav_core::codecs::{EncoderResult, EncoderError};
2 use nihav_codec_support::codecs::MV;
3 use crate::codecs::vpenc::coder::*;
4 use crate::codecs::vpenc::models::*;
5 use super::super::vpcommon::*;
6 use super::super::vp6data::*;
9 pub use crate::codecs::vpenc::coder::{BoolEncoder, Estimator};
11 pub const MODE_TREE: &[TokenSeq<VPMBType>] = &[
12 bit_seq!(VPMBType::Intra; T, F, F; 0, 2, 5),
13 bit_seq!(VPMBType::InterFourMV; T, F, T; 0, 2, 5),
14 bit_seq!(VPMBType::InterNoMV; F, F, F; 0, 1, 3),
15 bit_seq!(VPMBType::InterMV; F, F, T; 0, 1, 3),
16 bit_seq!(VPMBType::InterNearest; F, T, F; 0, 1, 4),
17 bit_seq!(VPMBType::InterNear; F, T, T; 0, 1, 4),
18 bit_seq!(VPMBType::GoldenNoMV; T, T, F, F; 0, 2, 6, 7),
19 bit_seq!(VPMBType::GoldenMV; T, T, F, T; 0, 2, 6, 7),
20 bit_seq!(VPMBType::GoldenNearest; T, T, T, F; 0, 2, 6, 8),
21 bit_seq!(VPMBType::GoldenNear; T, T, T, T; 0, 2, 6, 8),
24 const MODE_TREE_DIFF: &[TokenSeq<u8>] = &[
25 bit_seq!(1; F, T; 0, 1),
26 bit_seq!(2; F, F; 0, 1),
27 bit_seq!(3; T, F, T; 0, 2, 3),
28 bit_seq!(4; T, F, F, T; 0, 2, 3, 4),
29 bit_seq!(5; T, F, F, F, T; 0, 2, 3, 4, 5),
30 bit_seq!(6; T, F, F, F, F; 0, 2, 3, 4, 5),
31 bit_seq!(7; T, T; 0, 2),
34 const MODE_TREE_DIFF_PROBS: &[u8; 6] = &[171, 83, 199, 140, 125, 104];
36 const SHORT_MV_TREE: &[TokenSeq<u8>] = &[
37 bit_seq!(0; F, F, F; 0, 1, 2),
38 bit_seq!(1; F, F, T; 0, 1, 2),
39 bit_seq!(2; F, T, F; 0, 1, 3),
40 bit_seq!(3; F, T, T; 0, 1, 3),
41 bit_seq!(4; T, F, F; 0, 4, 5),
42 bit_seq!(5; T, F, T; 0, 4, 5),
43 bit_seq!(6; T, T, F; 0, 4, 6),
44 bit_seq!(7; T, T, T; 0, 4, 6),
49 const DC_TREE: &[TokenSeq<i8>] = &[
51 bit_seq!( 1; T, F; 0, 2),
52 bit_seq!( 2; T, T, F, F; 0, 2, 3, 4),
53 bit_seq!( 3; T, T, F, T, F; 0, 2, 3, 4, 5),
54 bit_seq!( 4; T, T, F, T, T; 0, 2, 3, 4, 5),
55 bit_seq!( -1; T, T, T, F, F; 0, 2, 3, 6, 7),
56 bit_seq!( -2; T, T, T, F, T; 0, 2, 3, 6, 7),
57 bit_seq!( -3; T, T, T, T, F, F; 0, 2, 3, 6, 8, 9),
58 bit_seq!( -4; T, T, T, T, F, T; 0, 2, 3, 6, 8, 9),
59 bit_seq!( -5; T, T, T, T, T, F; 0, 2, 3, 6, 8, 10),
60 bit_seq!( -6; T, T, T, T, T, T; 0, 2, 3, 6, 8, 10),
63 const NZ_COEF_TREE: &[TokenSeq<i8>] = &[
65 bit_seq!( 2; T, F, F; 2, 3, 4),
66 bit_seq!( 3; T, F, T, F; 2, 3, 4, 5),
67 bit_seq!( 4; T, F, T, T; 2, 3, 4, 5),
68 bit_seq!( -1; T, T, F, F; 2, 3, 6, 7),
69 bit_seq!( -2; T, T, F, T; 2, 3, 6, 7),
70 bit_seq!( -3; T, T, T, F, F; 2, 3, 6, 8, 9),
71 bit_seq!( -4; T, T, T, F, T; 2, 3, 6, 8, 9),
72 bit_seq!( -5; T, T, T, T, F; 2, 3, 6, 8, 10),
73 bit_seq!( -6; T, T, T, T, T; 2, 3, 6, 8, 10),
76 const COEF_TREE: &[TokenSeq<i8>] = &[
77 bit_seq!( 0; F, T; 0, 1),
78 bit_seq!(EOB; F, F; 0, 1),
79 bit_seq!( 1; T, F; 0, 2),
80 bit_seq!( 2; T, T, F, F; 0, 2, 3, 4),
81 bit_seq!( 3; T, T, F, T, F; 0, 2, 3, 4, 5),
82 bit_seq!( 4; T, T, F, T, T; 0, 2, 3, 4, 5),
83 bit_seq!( -1; T, T, T, F, F; 0, 2, 3, 6, 7),
84 bit_seq!( -2; T, T, T, F, T; 0, 2, 3, 6, 7),
85 bit_seq!( -3; T, T, T, T, F, F; 0, 2, 3, 6, 8, 9),
86 bit_seq!( -4; T, T, T, T, F, T; 0, 2, 3, 6, 8, 9),
87 bit_seq!( -5; T, T, T, T, T, F; 0, 2, 3, 6, 8, 10),
88 bit_seq!( -6; T, T, T, T, T, T; 0, 2, 3, 6, 8, 10),
91 fn coef_to_cat(coef: i16) -> i8 {
93 0 ..=4 => coef.abs() as i8,
103 const ZERO_RUN_TREE: &[TokenSeq<u8>] = &[
104 bit_seq!(1; F, F, F; 0, 1, 2),
105 bit_seq!(2; F, F, T; 0, 1, 2),
106 bit_seq!(3; F, T, F; 0, 1, 3),
107 bit_seq!(4; F, T, T; 0, 1, 3),
108 bit_seq!(5; T, F, F, F; 0, 4, 5, 6),
109 bit_seq!(6; T, F, F, T; 0, 4, 5, 6),
110 bit_seq!(7; T, F, T, F; 0, 4, 5, 7),
111 bit_seq!(8; T, F, T, T; 0, 4, 5, 7),
112 bit_seq!(9; T, T; 0, 4),
115 pub trait EncoderTrait {
116 fn write_cat(&mut self, cat: i8, tree: &[TokenSeq<i8>], tok_probs: &[u8], val_probs: &[u8; 11]) -> EncoderResult<()>;
117 fn write_large_coef(&mut self, val: i16, cat: usize) -> EncoderResult<()>;
118 fn write_dc(&mut self, val: i16, tok_probs: &[u8; 5], val_probs: &[u8; 11]) -> EncoderResult<()>;
119 fn write_ac(&mut self, val: i16, tree: &[TokenSeq<i8>], probs: &[u8; 11]) -> EncoderResult<()>;
120 fn write_zero_run(&mut self, val: usize, probs: &[u8; 14]) -> EncoderResult<()>;
123 impl<'a, 'b> EncoderTrait for BoolEncoder<'a, 'b> {
124 fn write_cat(&mut self, cat: i8, tree: &[TokenSeq<i8>], tok_probs: &[u8], val_probs: &[u8; 11]) -> EncoderResult<()> {
125 for entry in tree.iter() {
126 if entry.val == cat {
127 for seq in entry.seq.iter() {
128 let prob = if seq.idx < 5 {
129 tok_probs[seq.idx as usize]
131 val_probs[seq.idx as usize]
133 self.put_bool(seq.bit, prob)?;
138 Err(EncoderError::Bug)
140 fn write_large_coef(&mut self, val: i16, cat: usize) -> EncoderResult<()> {
141 let base = VP56_COEF_BASE[cat];
142 let mut probs = VP56_COEF_ADD_PROBS[cat].iter();
143 let add = val.abs() - base;
144 let mut mask = 1 << (VP6_COEF_ADD_BITS[cat] - 1);
146 self.put_bool((add & mask) != 0, *probs.next().unwrap())?;
149 self.put_bool(val < 0, 128)?;
153 fn write_dc(&mut self, val: i16, tok_probs: &[u8; 5], val_probs: &[u8; 11]) -> EncoderResult<()> {
154 let cat = coef_to_cat(val);
155 self.write_cat(cat, DC_TREE, tok_probs, val_probs)?;
157 self.write_large_coef(val, (-cat - 1) as usize)?;
159 self.put_bool(val < 0, 128)?;
163 fn write_ac(&mut self, val: i16, tree: &[TokenSeq<i8>], probs: &[u8; 11]) -> EncoderResult<()> {
164 let cat = coef_to_cat(val);
165 self.write_cat(cat, tree, probs, probs)?;
167 self.write_large_coef(val, (-cat - 1) as usize)?;
169 self.put_bool(val < 0, 128)?;
173 fn write_zero_run(&mut self, val: usize, probs: &[u8; 14]) -> EncoderResult<()> {
174 self.write_el(val.min(9) as u8, ZERO_RUN_TREE, probs)?;
178 self.put_bool(((add >> i) & 1) != 0, probs[i + 8])?;
185 fn rescale_mb_mode_prob(prob: u32, total: u32) -> u8 {
186 (255 * prob / (1 + total)) as u8
189 fn calc_mb_model_probs(prob_xmitted: &[u8; 20], mbtype_models: &mut [VP56MBTypeModel; 10]) {
191 let mdl = &mut mbtype_models[mode];
192 let mut cnt = [0u32; 10];
195 if i == mode { continue; }
196 cnt[i] = 100 * u32::from(prob_xmitted[i * 2]);
199 let sum = u32::from(prob_xmitted[mode * 2]) + u32::from(prob_xmitted[mode * 2 + 1]);
200 mdl.probs[9] = 255 - rescale_mb_mode_prob(u32::from(prob_xmitted[mode * 2 + 1]), sum);
202 let inter_mv0_weight = (cnt[0] as u32) + (cnt[2] as u32);
203 let inter_mv1_weight = (cnt[3] as u32) + (cnt[4] as u32);
204 let gold_mv0_weight = (cnt[5] as u32) + (cnt[6] as u32);
205 let gold_mv1_weight = (cnt[8] as u32) + (cnt[9] as u32);
206 let mix_weight = (cnt[1] as u32) + (cnt[7] as u32);
207 mdl.probs[0] = 1 + rescale_mb_mode_prob(inter_mv0_weight + inter_mv1_weight, total);
208 mdl.probs[1] = 1 + rescale_mb_mode_prob(inter_mv0_weight, inter_mv0_weight + inter_mv1_weight);
209 mdl.probs[2] = 1 + rescale_mb_mode_prob(mix_weight, mix_weight + gold_mv0_weight + gold_mv1_weight);
210 mdl.probs[3] = 1 + rescale_mb_mode_prob(cnt[0] as u32, inter_mv0_weight);
211 mdl.probs[4] = 1 + rescale_mb_mode_prob(cnt[3] as u32, inter_mv1_weight);
212 mdl.probs[5] = 1 + rescale_mb_mode_prob(cnt[1], mix_weight);
213 mdl.probs[6] = 1 + rescale_mb_mode_prob(gold_mv0_weight, gold_mv0_weight + gold_mv1_weight);
214 mdl.probs[7] = 1 + rescale_mb_mode_prob(cnt[5], gold_mv0_weight);
215 mdl.probs[8] = 1 + rescale_mb_mode_prob(cnt[8], gold_mv1_weight);
219 fn calc_mbtype_bits(prob_xmitted: &[u8; 20], stats: &[[usize; 10]; 10], mdl: &mut [VP56MBTypeModel; 10]) -> u32 {
220 const MB_TYPES: [VPMBType; 10] = [
224 VPMBType::InterNearest,
226 VPMBType::GoldenNoMV,
228 VPMBType::InterFourMV,
229 VPMBType::GoldenNearest,
233 calc_mb_model_probs(prob_xmitted, mdl);
235 for (last, (srow, mdl)) in stats.iter().zip(mdl.iter()).enumerate() {
236 for (cur, &ccount) in srow.iter().enumerate() {
237 let ccount = ccount as u32;
238 nits += Estimator::est_nits(cur == last, mdl.probs[9]) * ccount;
240 for entry in MODE_TREE.iter() {
241 if entry.val == MB_TYPES[cur] {
242 for seq in entry.seq.iter() {
243 nits += Estimator::est_nits(seq.bit, mdl.probs[seq.idx as usize]) * ccount;
252 Estimator::nits_to_bits(nits)
255 fn find_model_vq(prob_xmitted: &[u8; 20], vq: &[[u8; 20]; 16]) -> usize {
256 let mut best_idx = 0;
257 let mut best_dist = i16::MAX;
259 for (idx, row) in vq.iter().enumerate() {
262 let a = prob_xmitted[i ^ 1];
264 dist += (i16::from(a) - i16::from(b)).abs();
269 if dist < best_dist {
278 // todo per-delta decision, incremental updates and such
279 fn deltas_bits(probs: &[u8; 20], base: &[u8; 20], stats: &[[usize; 10]; 10], tmp: &mut [VP56MBTypeModel; 10], deltas: &mut [i16; 20]) -> u32 {
280 const DELTA_PROBS: [u8; 8] = [
282 PROB_BITS[256 - 205] + PROB_BITS[171] + PROB_BITS[256 - 83] + PROB_BITS[128],
283 PROB_BITS[256 - 205] + PROB_BITS[171] + PROB_BITS[83] + PROB_BITS[128],
284 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[199] + PROB_BITS[256 - 140] + PROB_BITS[128],
285 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[199] + PROB_BITS[140] + PROB_BITS[256 - 125] + PROB_BITS[128],
286 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[199] + PROB_BITS[140] + PROB_BITS[125] + PROB_BITS[256 - 104] + PROB_BITS[128],
287 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[199] + PROB_BITS[140] + PROB_BITS[125] + PROB_BITS[104] + PROB_BITS[128],
288 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[256 - 199] + 8 * PROB_BITS[128],
292 let mut tprobs = [0u8; 20];
295 let old = i16::from(base[i]);
296 let new = i16::from(probs[i]);
297 let mut diff = (new - old) & !3;
298 if old + diff > 255 {
300 } else if old + diff < 0 || (old + diff == 0 && new != 0) {
303 tprobs[i] = (old + diff) as u8;
305 nits += u32::from(DELTA_PROBS[(diff.abs() >> 2).min(7) as usize]);
308 Estimator::nits_to_bits(nits) + calc_mbtype_bits(&tprobs, stats, tmp) + 5
311 pub fn encode_mode_prob_models(bc: &mut BoolEncoder, models: &mut VP56Models, pmodels: &VP56Models, stats: &[[[usize; 10]; 10]; 3]) -> EncoderResult<()> {
312 let mut tmp = [VP56MBTypeModel::default(); 10];
313 let mut tprob = [0; 20];
315 let mut models_changed = models.prob_xmitted[ctx] != pmodels.prob_xmitted[ctx];
317 let old_bits = calc_mbtype_bits(&pmodels.prob_xmitted[ctx], &stats[ctx], &mut tmp);
318 let new_bits = calc_mbtype_bits(&models.prob_xmitted[ctx], &stats[ctx], &mut tmp) + 4;
319 if new_bits < old_bits {
320 let idx = find_model_vq(&models.prob_xmitted[ctx], &VP56_MODE_VQ[ctx]);
322 tprob[i ^ 1] = VP56_MODE_VQ[ctx][idx][i];
324 let vq_bits = calc_mbtype_bits(&tprob, &stats[ctx], &mut tmp) + 4;
325 if vq_bits < old_bits {
326 bc.put_bool(true, 174)?;
327 bc.put_bits(idx as u32, 4)?;
328 let mut diffs_present = tprob != models.prob_xmitted[ctx];
329 let mut deltas = [0; 20];
330 let delta_cost = deltas_bits(&models.prob_xmitted[ctx], &tprob, &stats[ctx], &mut tmp, &mut deltas);
331 if delta_cost + 1 >= new_bits {
332 diffs_present = false;
335 bc.put_bool(true, 254)?;
337 let diff = deltas[i ^ 1] >> 2;
338 bc.put_bool(diff != 0, 205)?;
340 let d0 = diff.abs().min(7) as u8;
341 bc.put_bool(diff < 0, 128)?;
342 bc.write_el(d0, MODE_TREE_DIFF, MODE_TREE_DIFF_PROBS)?;
344 bc.put_bits(diff.abs() as u32, 7)?;
346 tprob[i ^ 1] = (i16::from(tprob[i ^ 1]) + deltas[i ^ 1]) as u8;
351 bc.put_bool(false, 254)?;
354 models_changed = false;
357 models_changed = false;
361 bc.put_bool(false, 174)?;
362 bc.put_bool(false, 254)?;
363 models.prob_xmitted[ctx] = pmodels.prob_xmitted[ctx];
365 models.prob_xmitted[ctx] = tprob;
369 let prob_xmitted = &models.prob_xmitted[ctx];
370 calc_mb_model_probs(prob_xmitted, &mut models.mbtype_models[ctx]);
375 pub fn encode_mv_models(bc: &mut BoolEncoder, models: &[VP56MVModel; 2], pmodels: &[VP56MVModel; 2]) -> EncoderResult<()> {
376 for (i, (mdl, pmdl)) in models.iter().zip(pmodels.iter()).enumerate() {
377 bc.encode_probability(mdl.nz_prob, pmdl.nz_prob, HAS_NZ_PROB[i])?;
378 bc.encode_probability(mdl.sign_prob, pmdl.sign_prob, HAS_SIGN_PROB[i])?;
380 for (i, (mdl, pmdl)) in models.iter().zip(pmodels.iter()).enumerate() {
381 for (&coded_prob, (&prob, &pprob)) in HAS_TREE_PROB[i].iter().zip(mdl.tree_probs.iter().zip(pmdl.tree_probs.iter())) {
382 bc.encode_probability(prob, pprob, coded_prob)?;
385 for (i, (mdl, pmdl)) in models.iter().zip(pmodels.iter()).enumerate() {
386 for (&coded_prob, (&prob, &pprob)) in HAS_RAW_PROB[i].iter().zip(mdl.raw_probs.iter().zip(pmdl.raw_probs.iter())) {
387 bc.encode_probability(prob, pprob, coded_prob)?;
393 pub fn encode_coeff_models(bc: &mut BoolEncoder, models: &mut VP56Models, pmodels: &VP56Models, is_intra: bool, interlaced: bool) -> EncoderResult<()> {
394 let mut def_prob = [128u8; 11];
397 let pprob = pmodels.coeff_models[plane].dc_value_probs[i];
398 let prob = models.coeff_models[plane].dc_value_probs[i];
399 let changed = (is_intra && prob != def_prob[i]) || (!is_intra && prob != pprob);
400 bc.put_bool(changed, HAS_COEF_PROBS[plane][i])?;
402 bc.put_probability(prob)?;
408 bc.put_bool(false, 128)?;
409 reset_scan(&mut models.vp6models, interlaced);
412 if bc.read_prob(HAS_SCAN_UPD_PROBS[i]) {
413 models.vp6models.scan_order[i] = bc.read_bits(4) as usize;
416 update_scan(&mut models.vp6models);
421 bc.encode_probability(models.vp6models.zero_run_probs[comp][i], pmodels.vp6models.zero_run_probs[comp][i], HAS_ZERO_RUN_PROBS[comp][i])?;
429 let pprob = pmodels.coeff_models[plane].ac_val_probs[ctype][group][i];
430 let prob = models.coeff_models[plane].ac_val_probs[ctype][group][i];
431 let changed = (is_intra && prob != def_prob[i]) || (!is_intra && prob != pprob);
432 bc.put_bool(changed, VP6_AC_PROBS[ctype][plane][group][i])?;
434 bc.put_probability(prob)?;
443 let mdl = &mut models.coeff_models[plane];
446 mdl.dc_token_probs[0][i][k] = rescale_prob(mdl.dc_value_probs[k], &VP6_DC_WEIGHTS[k][i], 255);
453 pub fn encode_block(bc: &mut BoolEncoder, blk: &[i16; 64], dc_mode: usize, model: &VP56CoeffModel, vp6model: &VP6Models) -> EncoderResult<()> {
455 for i in (0..64).rev() {
456 if blk[vp6model.zigzag[i]] != 0 {
462 bc.write_dc(blk[0], &model.dc_token_probs[0][dc_mode], &model.dc_value_probs)?;
464 let mut last_idx = 0;
465 let mut last_val = blk[0];
467 let val = blk[vp6model.zigzag[idx]];
468 let has_nnz = (idx == 1) || (last_val != 0);
469 if (val != 0) || has_nnz {
470 if last_val == 0 && idx != 1 {
471 let zrun = idx - last_idx;
472 bc.write_zero_run(zrun, &vp6model.zero_run_probs[if last_idx + 1 >= 7 { 1 } else { 0 }])?;
474 let ac_band = VP6_IDX_TO_AC_BAND[idx];
475 let ac_mode = last_val.abs().min(2) as usize;
476 let tree = if has_nnz { COEF_TREE } else { NZ_COEF_TREE };
477 bc.write_ac(val, tree, &model.ac_val_probs[ac_mode][ac_band])?;
484 let ac_band = VP6_IDX_TO_AC_BAND[idx];
485 let ac_mode = last_val.abs().min(2) as usize;
486 bc.write_el(EOB, COEF_TREE, &model.ac_val_probs[ac_mode][ac_band])?;
489 bc.write_cat(0, DC_TREE, &model.dc_token_probs[0][dc_mode], &model.dc_value_probs)?;
490 let ac_band = VP6_IDX_TO_AC_BAND[1];
491 bc.write_el(EOB, COEF_TREE, &model.ac_val_probs[0][ac_band])?;
496 fn map_mb_type(mbtype: VPMBType) -> usize {
498 VPMBType::InterNoMV => 0,
499 VPMBType::Intra => 1,
500 VPMBType::InterMV => 2,
501 VPMBType::InterNearest => 3,
502 VPMBType::InterNear => 4,
503 VPMBType::GoldenNoMV => 5,
504 VPMBType::GoldenMV => 6,
505 VPMBType::InterFourMV => 7,
506 VPMBType::GoldenNearest => 8,
507 VPMBType::GoldenNear => 9,
511 pub fn encode_mb_type(bc: &mut BoolEncoder, mb_type: VPMBType, last_mb_type: VPMBType, ctx: usize, model: &VP56Models) -> EncoderResult<()> {
512 let probs = &model.mbtype_models[ctx][map_mb_type(last_mb_type)].probs;
513 bc.put_bool(mb_type == last_mb_type, probs[9])?;
514 if mb_type != last_mb_type {
515 bc.write_el(mb_type, MODE_TREE, probs)?;
520 fn encode_mv_component(bc: &mut BoolEncoder, mv: i16, model: &VP56MVModel) -> EncoderResult<()> {
522 bc.put_bool(aval >= 8, model.nz_prob)?;
524 bc.write_el(aval as u8, SHORT_MV_TREE, &model.tree_probs)?;
526 for &ord in LONG_VECTOR_ORDER.iter() {
527 bc.put_bool(((aval >> ord) & 1) != 0, model.raw_probs[ord])?;
529 if (aval & 0xF0) != 0 {
530 bc.put_bool((aval & (1 << 3)) != 0, model.raw_probs[3])?;
534 bc.put_bool(mv < 0, model.sign_prob)?;
539 pub fn encode_mv(bc: &mut BoolEncoder, mv: MV, model: &VP56Models) -> EncoderResult<()> {
540 encode_mv_component(bc, mv.x, &model.mv_models[0])?;
541 encode_mv_component(bc, mv.y, &model.mv_models[1])?;
545 pub trait VP6EstimatorTrait {
546 fn write_cat(&self, cat: i8, tree: &[TokenSeq<i8>], probs: &mut [ProbCounter; 11]);
547 fn write_dc(&self, val: i16, probs: &mut [ProbCounter; 11]);
548 fn write_ac(&self, val: i16, tree: &[TokenSeq<i8>], probs: &mut [ProbCounter; 11]);
549 fn write_zero_run(&self, val: usize, probs: &mut [ProbCounter; 14]);
552 impl VP6EstimatorTrait for Estimator {
553 fn write_cat(&self, cat: i8, tree: &[TokenSeq<i8>], probs: &mut [ProbCounter; 11]) {
554 for entry in tree.iter() {
555 if entry.val == cat {
556 for seq in entry.seq.iter() {
557 probs[seq.idx as usize].add(seq.bit);
563 fn write_dc(&self, val: i16, probs: &mut [ProbCounter; 11]) {
564 self.write_cat(coef_to_cat(val), DC_TREE, probs);
566 fn write_ac(&self, val: i16, tree: &[TokenSeq<i8>], probs: &mut [ProbCounter; 11]) {
567 self.write_cat(coef_to_cat(val), tree, probs);
569 fn write_zero_run(&self, val: usize, probs: &mut [ProbCounter; 14]) {
570 self.write_el(val.min(9) as u8, ZERO_RUN_TREE, probs);
574 probs[i + 8].add(((add >> i) & 1) != 0);
580 pub fn estimate_block(blk: &[i16; 64], _dc_mode: usize, model: &mut VP56CoeffModelStat, vp6model: &mut VP6ModelsStat, scan: &[usize; 64]) {
581 let bc = Estimator::new();
584 for i in (0..64).rev() {
585 if blk[scan[i]] != 0 {
591 bc.write_dc(blk[0], &mut model.dc_value_probs);
593 let mut last_idx = 0;
594 let mut last_val = blk[0];
596 let val = blk[scan[idx]];
597 let has_nnz = (idx == 1) || (last_val != 0);
598 if (val != 0) || has_nnz {
599 if last_val == 0 && idx != 1 {
600 let zrun = idx - last_idx;
601 bc.write_zero_run(zrun, &mut vp6model.zero_run_probs[if last_idx + 1 >= 7 { 1 } else { 0 }]);
603 let ac_band = VP6_IDX_TO_AC_BAND[idx];
604 let ac_mode = last_val.abs().min(2) as usize;
605 let tree = if has_nnz { COEF_TREE } else { NZ_COEF_TREE };
606 bc.write_ac(val, tree, &mut model.ac_val_probs[ac_mode][ac_band]);
613 let ac_band = VP6_IDX_TO_AC_BAND[idx];
614 let ac_mode = last_val.abs().min(2) as usize;
615 bc.write_el(EOB, COEF_TREE, &mut model.ac_val_probs[ac_mode][ac_band]);
618 bc.write_cat(0, DC_TREE, &mut model.dc_value_probs);
619 let ac_band = VP6_IDX_TO_AC_BAND[1];
620 bc.write_el(EOB, COEF_TREE, &mut model.ac_val_probs[0][ac_band]);
624 pub fn estimate_mb_type(mb_type: VPMBType, last_mb_type: VPMBType, ctx: usize, model: &mut VP56ModelsStat) {
625 model.mbtype_models[ctx][map_mb_type(last_mb_type)][map_mb_type(mb_type)] += 1;
628 fn estimate_mv_component(mv: i16, model: &mut VP56MVModelStat) {
629 let bc = Estimator::new();
631 model.nz_prob.add(aval >= 8);
633 bc.write_el(aval as u8, SHORT_MV_TREE, &mut model.tree_probs);
635 for &ord in LONG_VECTOR_ORDER.iter() {
636 model.raw_probs[ord].add(((aval >> ord) & 1) != 0);
638 if (aval & 0xF0) != 0 {
639 model.raw_probs[3].add((aval & (1 << 3)) != 0);
643 model.sign_prob.add(mv < 0);
647 pub fn estimate_mv(mv: MV, model: &mut VP56ModelsStat) {
648 estimate_mv_component(mv.x, &mut model.mv_models[0]);
649 estimate_mv_component(mv.y, &mut model.mv_models[1]);
652 const VP56_MODE_VQ: [[[u8; 20]; 16]; 3] = [
654 [ 9, 15, 32, 25, 7, 19, 9, 21, 1, 12, 14, 12, 3, 18, 14, 23, 3, 10, 0, 4 ],
655 [ 48, 39, 1, 2, 11, 27, 29, 44, 7, 27, 1, 4, 0, 3, 1, 6, 1, 2, 0, 0 ],
656 [ 21, 32, 1, 2, 4, 10, 32, 43, 6, 23, 2, 3, 1, 19, 1, 6, 12, 21, 0, 7 ],
657 [ 69, 83, 0, 0, 0, 2, 10, 29, 3, 12, 0, 1, 0, 3, 0, 3, 2, 2, 0, 0 ],
658 [ 11, 20, 1, 4, 18, 36, 43, 48, 13, 35, 0, 2, 0, 5, 3, 12, 1, 2, 0, 0 ],
659 [ 70, 44, 0, 1, 2, 10, 37, 46, 8, 26, 0, 2, 0, 2, 0, 2, 0, 1, 0, 0 ],
660 [ 8, 15, 0, 1, 8, 21, 74, 53, 22, 42, 0, 1, 0, 2, 0, 3, 1, 2, 0, 0 ],
661 [ 141, 42, 0, 0, 1, 4, 11, 24, 1, 11, 0, 1, 0, 1, 0, 2, 0, 0, 0, 0 ],
662 [ 8, 19, 4, 10, 24, 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 0, 1 ],
695 [ 171, 25, 0, 0, 1, 5, 25, 21, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 ],
696 [ 17, 21, 68, 29, 6, 15, 13, 22, 0, 0, 6, 12, 3, 14, 4, 10, 1, 7, 0, 3 ],
697 [ 51, 39, 0, 1, 2, 12, 91, 44, 0, 0, 0, 2, 0, 3, 0, 1, 2, 3, 0, 1 ],
698 [ 81, 25, 0, 0, 2, 9, 106, 26, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0 ],
699 [ 140, 37, 0, 1, 1, 8, 24, 33, 0, 0, 1, 2, 0, 2, 0, 1, 1, 2, 0, 0 ],
700 [ 14, 23, 1, 3, 11, 53, 90, 31, 0, 0, 0, 3, 1, 5, 2, 6, 1, 2, 0, 0 ],
701 [ 123, 29, 0, 0, 1, 7, 57, 30, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0 ],
702 [ 13, 14, 0, 0, 4, 20, 175, 20, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0 ],
703 [ 202, 23, 0, 0, 1, 3, 2, 9, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 ],