]> git.nihav.org Git - nihav.git/blame_incremental - coder.rs
add MPEG-4 ASP decoder
[nihav.git] / coder.rs
... / ...
CommitLineData
1use nihav_core::io::byteio::*;
2use nihav_core::codecs::{EncoderResult, EncoderError};
3use nihav_codec_support::codecs::MV;
4use super::super::vpcommon::*;
5use super::super::vp6data::*;
6use super::models::*;
7
8struct EncSeq {
9 bit: bool,
10 idx: u8,
11}
12
13pub struct TokenSeq<T: PartialEq> {
14 val: T,
15 seq: &'static [EncSeq],
16}
17
18macro_rules! bit_entry {
19 (T; $idx:expr) => {EncSeq {bit: true, idx: $idx }};
20 (F; $idx:expr) => {EncSeq {bit: false, idx: $idx }};
21}
22
23macro_rules! bit_seq {
24 ($val: expr; $( $bit:tt),* ; $( $idx:expr),* ) => {
25 TokenSeq {
26 val: $val,
27 seq:
28 &[
29 $(
30 bit_entry!($bit; $idx),
31 )*
32 ]
33 }
34 };
35}
36
37pub const MODE_TREE: &[TokenSeq<VPMBType>] = &[
38 bit_seq!(VPMBType::Intra; T, F, F; 0, 2, 5),
39 bit_seq!(VPMBType::InterFourMV; T, F, T; 0, 2, 5),
40 bit_seq!(VPMBType::InterNoMV; F, F, F; 0, 1, 3),
41 bit_seq!(VPMBType::InterMV; F, F, T; 0, 1, 3),
42 bit_seq!(VPMBType::InterNearest; F, T, F; 0, 1, 4),
43 bit_seq!(VPMBType::InterNear; F, T, T; 0, 1, 4),
44 bit_seq!(VPMBType::GoldenNoMV; T, T, F, F; 0, 2, 6, 7),
45 bit_seq!(VPMBType::GoldenMV; T, T, F, T; 0, 2, 6, 7),
46 bit_seq!(VPMBType::GoldenNearest; T, T, T, F; 0, 2, 6, 8),
47 bit_seq!(VPMBType::GoldenNear; T, T, T, T; 0, 2, 6, 8),
48];
49
50const MODE_TREE_DIFF: &[TokenSeq<u8>] = &[
51 bit_seq!(1; F, T; 0, 1),
52 bit_seq!(2; F, F; 0, 1),
53 bit_seq!(3; T, F, T; 0, 2, 3),
54 bit_seq!(4; T, F, F, T; 0, 2, 3, 4),
55 bit_seq!(5; T, F, F, F, T; 0, 2, 3, 4, 5),
56 bit_seq!(6; T, F, F, F, F; 0, 2, 3, 4, 5),
57 bit_seq!(7; T, T; 0, 2),
58];
59
60const MODE_TREE_DIFF_PROBS: &[u8; 6] = &[171, 83, 199, 140, 125, 104];
61
62const SHORT_MV_TREE: &[TokenSeq<u8>] = &[
63 bit_seq!(0; F, F, F; 0, 1, 2),
64 bit_seq!(1; F, F, T; 0, 1, 2),
65 bit_seq!(2; F, T, F; 0, 1, 3),
66 bit_seq!(3; F, T, T; 0, 1, 3),
67 bit_seq!(4; T, F, F; 0, 4, 5),
68 bit_seq!(5; T, F, T; 0, 4, 5),
69 bit_seq!(6; T, T, F; 0, 4, 6),
70 bit_seq!(7; T, T, T; 0, 4, 6),
71];
72
73const EOB: i8 = 42;
74
75const DC_TREE: &[TokenSeq<i8>] = &[
76 bit_seq!( 0; F; 0),
77 bit_seq!( 1; T, F; 0, 2),
78 bit_seq!( 2; T, T, F, F; 0, 2, 3, 4),
79 bit_seq!( 3; T, T, F, T, F; 0, 2, 3, 4, 5),
80 bit_seq!( 4; T, T, F, T, T; 0, 2, 3, 4, 5),
81 bit_seq!( -1; T, T, T, F, F; 0, 2, 3, 6, 7),
82 bit_seq!( -2; T, T, T, F, T; 0, 2, 3, 6, 7),
83 bit_seq!( -3; T, T, T, T, F, F; 0, 2, 3, 6, 8, 9),
84 bit_seq!( -4; T, T, T, T, F, T; 0, 2, 3, 6, 8, 9),
85 bit_seq!( -5; T, T, T, T, T, F; 0, 2, 3, 6, 8, 10),
86 bit_seq!( -6; T, T, T, T, T, T; 0, 2, 3, 6, 8, 10),
87];
88
89const NZ_COEF_TREE: &[TokenSeq<i8>] = &[
90 bit_seq!( 1; F; 2),
91 bit_seq!( 2; T, F, F; 2, 3, 4),
92 bit_seq!( 3; T, F, T, F; 2, 3, 4, 5),
93 bit_seq!( 4; T, F, T, T; 2, 3, 4, 5),
94 bit_seq!( -1; T, T, F, F; 2, 3, 6, 7),
95 bit_seq!( -2; T, T, F, T; 2, 3, 6, 7),
96 bit_seq!( -3; T, T, T, F, F; 2, 3, 6, 8, 9),
97 bit_seq!( -4; T, T, T, F, T; 2, 3, 6, 8, 9),
98 bit_seq!( -5; T, T, T, T, F; 2, 3, 6, 8, 10),
99 bit_seq!( -6; T, T, T, T, T; 2, 3, 6, 8, 10),
100];
101
102const COEF_TREE: &[TokenSeq<i8>] = &[
103 bit_seq!( 0; F, T; 0, 1),
104 bit_seq!(EOB; F, F; 0, 1),
105 bit_seq!( 1; T, F; 0, 2),
106 bit_seq!( 2; T, T, F, F; 0, 2, 3, 4),
107 bit_seq!( 3; T, T, F, T, F; 0, 2, 3, 4, 5),
108 bit_seq!( 4; T, T, F, T, T; 0, 2, 3, 4, 5),
109 bit_seq!( -1; T, T, T, F, F; 0, 2, 3, 6, 7),
110 bit_seq!( -2; T, T, T, F, T; 0, 2, 3, 6, 7),
111 bit_seq!( -3; T, T, T, T, F, F; 0, 2, 3, 6, 8, 9),
112 bit_seq!( -4; T, T, T, T, F, T; 0, 2, 3, 6, 8, 9),
113 bit_seq!( -5; T, T, T, T, T, F; 0, 2, 3, 6, 8, 10),
114 bit_seq!( -6; T, T, T, T, T, T; 0, 2, 3, 6, 8, 10),
115];
116
117fn coef_to_cat(coef: i16) -> i8 {
118 match coef.abs() {
119 0 ..=4 => coef.abs() as i8,
120 5 ..=6 => -1,
121 7 ..=10 => -2,
122 11..=18 => -3,
123 19..=34 => -4,
124 35..=66 => -5,
125 _ => -6,
126 }
127}
128
129const ZERO_RUN_TREE: &[TokenSeq<u8>] = &[
130 bit_seq!(1; F, F, F; 0, 1, 2),
131 bit_seq!(2; F, F, T; 0, 1, 2),
132 bit_seq!(3; F, T, F; 0, 1, 3),
133 bit_seq!(4; F, T, T; 0, 1, 3),
134 bit_seq!(5; T, F, F, F; 0, 4, 5, 6),
135 bit_seq!(6; T, F, F, T; 0, 4, 5, 6),
136 bit_seq!(7; T, F, T, F; 0, 4, 5, 7),
137 bit_seq!(8; T, F, T, T; 0, 4, 5, 7),
138 bit_seq!(9; T, T; 0, 4),
139];
140
141pub struct BoolEncoder<'a, 'b> {
142 bw: &'a mut ByteWriter<'b>,
143 val: u32,
144 range: u32,
145 bits: u8,
146 saved: u8,
147 run: usize,
148}
149
150impl<'a, 'b> BoolEncoder<'a, 'b> {
151 pub fn new(bw: &'a mut ByteWriter<'b>) -> Self {
152 Self {
153 bw,
154 val: 0,
155 range: 255,
156 bits: 0,
157 saved: 0,
158 run: 0,
159 }
160 }
161 pub fn put_bool(&mut self, bit: bool, prob: u8) -> EncoderResult<()> {
162 let split = 1 + (((self.range - 1) * u32::from(prob)) >> 8);
163 if bit {
164 self.range -= split;
165 self.val += split;
166 } else {
167 self.range = split;
168 }
169
170 if self.range < 128 {
171 self.renorm()?;
172 }
173 Ok(())
174 }
175 fn flush_run(&mut self, overflow: bool) -> EncoderResult<()> {
176 if self.run > 0 {
177 self.bw.write_byte(self.saved + (overflow as u8))?;
178 if !overflow {
179 for _ in 1..self.run {
180 self.bw.write_byte(0xFF)?;
181 }
182 } else {
183 for _ in 1..self.run {
184 self.bw.write_byte(0)?;
185 }
186 }
187 self.run = 0;
188 }
189 Ok(())
190 }
191 fn renorm(&mut self) -> EncoderResult<()> {
192 let bits = (self.range.leading_zeros() & 7) as u8;
193 self.range <<= bits;
194 if self.bits + bits < 23 {
195 self.bits += bits;
196 self.val <<= bits;
197 } else {
198 for _ in 0..bits {
199 if (self.bits == 23) && ((self.val >> 31) != 0) {
200 self.flush_run(true)?;
201 }
202 self.val <<= 1;
203 self.bits += 1;
204 if self.bits == 24 {
205 let tbyte = (self.val >> 24) as u8;
206 let nbyte = (self.val >> 16) as u8;
207 if tbyte < 0xFF {
208 self.flush_run(false)?;
209 if nbyte < 0xFE {
210 self.bw.write_byte(tbyte)?;
211 } else {
212 self.saved = tbyte;
213 self.run = 1;
214 }
215 } else {
216 self.run += 1;
217 }
218 self.val &= 0xFFFFFF;
219 self.bits -= 8;
220 }
221 }
222 }
223 Ok(())
224 }
225 pub fn flush(mut self) -> EncoderResult<()> {
226 self.flush_run(false)?;
227 self.val <<= 24 - self.bits;
228 self.bw.write_u32be(self.val)?;
229 Ok(())
230 }
231
232 pub fn put_bits(&mut self, val: u32, len: u8) -> EncoderResult<()> {
233 let mut mask = 1 << (len - 1);
234 while mask != 0 {
235 self.put_bool((val & mask) != 0, 128)?;
236 mask >>= 1;
237 }
238 Ok(())
239 }
240 fn put_probability(&mut self, prob: u8) -> EncoderResult<()> {
241 self.put_bits(u32::from(prob >> 1), 7)
242 }
243 fn encode_probability(&mut self, new: u8, old: u8, prob: u8) -> EncoderResult<()> {
244 self.put_bool(new != old, prob)?;
245 if new != old {
246 self.put_probability(new)?;
247 }
248 Ok(())
249 }
250 pub fn write_el<T: PartialEq>(&mut self, el: T, tree: &[TokenSeq<T>], probs: &[u8]) -> EncoderResult<()> {
251 for entry in tree.iter() {
252 if entry.val == el {
253 for seq in entry.seq.iter() {
254 self.put_bool(seq.bit, probs[seq.idx as usize])?;
255 }
256 return Ok(());
257 }
258 }
259 Err(EncoderError::Bug)
260 }
261 fn write_cat(&mut self, cat: i8, tree: &[TokenSeq<i8>], tok_probs: &[u8], val_probs: &[u8; 11]) -> EncoderResult<()> {
262 for entry in tree.iter() {
263 if entry.val == cat {
264 for seq in entry.seq.iter() {
265 let prob = if seq.idx < 5 {
266 tok_probs[seq.idx as usize]
267 } else {
268 val_probs[seq.idx as usize]
269 };
270 self.put_bool(seq.bit, prob)?;
271 }
272 return Ok(());
273 }
274 }
275 Err(EncoderError::Bug)
276 }
277 fn write_large_coef(&mut self, val: i16, cat: usize) -> EncoderResult<()> {
278 let base = VP56_COEF_BASE[cat];
279 let mut probs = VP56_COEF_ADD_PROBS[cat].iter();
280 let add = val.abs() - base;
281 let mut mask = 1 << (VP6_COEF_ADD_BITS[cat] - 1);
282 while mask != 0 {
283 self.put_bool((add & mask) != 0, *probs.next().unwrap())?;
284 mask >>= 1;
285 }
286 self.put_bool(val < 0, 128)?;
287
288 Ok(())
289 }
290 fn write_dc(&mut self, val: i16, tok_probs: &[u8; 5], val_probs: &[u8; 11]) -> EncoderResult<()> {
291 let cat = coef_to_cat(val);
292 self.write_cat(cat, DC_TREE, tok_probs, val_probs)?;
293 if cat < 0 {
294 self.write_large_coef(val, (-cat - 1) as usize)?;
295 } else if val != 0 {
296 self.put_bool(val < 0, 128)?;
297 }
298 Ok(())
299 }
300 fn write_ac(&mut self, val: i16, tree: &[TokenSeq<i8>], probs: &[u8; 11]) -> EncoderResult<()> {
301 let cat = coef_to_cat(val);
302 self.write_cat(cat, tree, probs, probs)?;
303 if cat < 0 {
304 self.write_large_coef(val, (-cat - 1) as usize)?;
305 } else if val != 0 {
306 self.put_bool(val < 0, 128)?;
307 }
308 Ok(())
309 }
310 fn write_zero_run(&mut self, val: usize, probs: &[u8; 14]) -> EncoderResult<()> {
311 self.write_el(val.min(9) as u8, ZERO_RUN_TREE, probs)?;
312 if val >= 9 {
313 let add = val - 9;
314 for i in 0..6 {
315 self.put_bool(((add >> i) & 1) != 0, probs[i + 8])?;
316 }
317 }
318 Ok(())
319 }
320}
321
322fn rescale_mb_mode_prob(prob: u32, total: u32) -> u8 {
323 (255 * prob / (1 + total)) as u8
324}
325
326fn calc_mb_model_probs(prob_xmitted: &[u8; 20], mbtype_models: &mut [VP56MBTypeModel; 10]) {
327 for mode in 0..10 {
328 let mdl = &mut mbtype_models[mode];
329 let mut cnt = [0u32; 10];
330 let mut total = 0;
331 for i in 0..10 {
332 if i == mode { continue; }
333 cnt[i] = 100 * u32::from(prob_xmitted[i * 2]);
334 total += cnt[i];
335 }
336 let sum = u32::from(prob_xmitted[mode * 2]) + u32::from(prob_xmitted[mode * 2 + 1]);
337 mdl.probs[9] = 255 - rescale_mb_mode_prob(u32::from(prob_xmitted[mode * 2 + 1]), sum);
338
339 let inter_mv0_weight = (cnt[0] as u32) + (cnt[2] as u32);
340 let inter_mv1_weight = (cnt[3] as u32) + (cnt[4] as u32);
341 let gold_mv0_weight = (cnt[5] as u32) + (cnt[6] as u32);
342 let gold_mv1_weight = (cnt[8] as u32) + (cnt[9] as u32);
343 let mix_weight = (cnt[1] as u32) + (cnt[7] as u32);
344 mdl.probs[0] = 1 + rescale_mb_mode_prob(inter_mv0_weight + inter_mv1_weight, total);
345 mdl.probs[1] = 1 + rescale_mb_mode_prob(inter_mv0_weight, inter_mv0_weight + inter_mv1_weight);
346 mdl.probs[2] = 1 + rescale_mb_mode_prob(mix_weight, mix_weight + gold_mv0_weight + gold_mv1_weight);
347 mdl.probs[3] = 1 + rescale_mb_mode_prob(cnt[0] as u32, inter_mv0_weight);
348 mdl.probs[4] = 1 + rescale_mb_mode_prob(cnt[3] as u32, inter_mv1_weight);
349 mdl.probs[5] = 1 + rescale_mb_mode_prob(cnt[1], mix_weight);
350 mdl.probs[6] = 1 + rescale_mb_mode_prob(gold_mv0_weight, gold_mv0_weight + gold_mv1_weight);
351 mdl.probs[7] = 1 + rescale_mb_mode_prob(cnt[5], gold_mv0_weight);
352 mdl.probs[8] = 1 + rescale_mb_mode_prob(cnt[8], gold_mv1_weight);
353 }
354}
355
356fn calc_mbtype_bits(prob_xmitted: &[u8; 20], stats: &[[usize; 10]; 10], mdl: &mut [VP56MBTypeModel; 10]) -> u32 {
357 const MB_TYPES: [VPMBType; 10] = [
358 VPMBType::InterNoMV,
359 VPMBType::Intra,
360 VPMBType::InterMV,
361 VPMBType::InterNearest,
362 VPMBType::InterNear,
363 VPMBType::GoldenNoMV,
364 VPMBType::GoldenMV,
365 VPMBType::InterFourMV,
366 VPMBType::GoldenNearest,
367 VPMBType::GoldenNear
368 ];
369
370 calc_mb_model_probs(prob_xmitted, mdl);
371 let mut nits = 0;
372 for (last, (srow, mdl)) in stats.iter().zip(mdl.iter()).enumerate() {
373 for (cur, &ccount) in srow.iter().enumerate() {
374 let ccount = ccount as u32;
375 nits += Estimator::est_nits(cur == last, mdl.probs[9]) * ccount;
376 if cur != last {
377 for entry in MODE_TREE.iter() {
378 if entry.val == MB_TYPES[cur] {
379 for seq in entry.seq.iter() {
380 nits += Estimator::est_nits(seq.bit, mdl.probs[seq.idx as usize]) * ccount;
381 }
382 break;
383 }
384 }
385 }
386 }
387 }
388
389 Estimator::nits_to_bits(nits)
390}
391
392fn find_model_vq(prob_xmitted: &[u8; 20], vq: &[[u8; 20]; 16]) -> usize {
393 let mut best_idx = 0;
394 let mut best_dist = i16::MAX;
395
396 for (idx, row) in vq.iter().enumerate() {
397 let mut dist = 0;
398 for i in 0..20 {
399 let a = prob_xmitted[i ^ 1];
400 let b = row[i];
401 dist += (i16::from(a) - i16::from(b)).abs();
402 }
403 if dist == 0 {
404 return idx;
405 }
406 if dist < best_dist {
407 best_dist = dist;
408 best_idx = idx;
409 }
410 }
411
412 best_idx
413}
414
415// todo per-delta decision, incremental updates and such
416fn deltas_bits(probs: &[u8; 20], base: &[u8; 20], stats: &[[usize; 10]; 10], tmp: &mut [VP56MBTypeModel; 10], deltas: &mut [i16; 20]) -> u32 {
417 const DELTA_PROBS: [u8; 8] = [
418 PROB_BITS[205],
419 PROB_BITS[256 - 205] + PROB_BITS[171] + PROB_BITS[256 - 83] + PROB_BITS[128],
420 PROB_BITS[256 - 205] + PROB_BITS[171] + PROB_BITS[83] + PROB_BITS[128],
421 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[199] + PROB_BITS[256 - 140] + PROB_BITS[128],
422 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[199] + PROB_BITS[140] + PROB_BITS[256 - 125] + PROB_BITS[128],
423 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[199] + PROB_BITS[140] + PROB_BITS[125] + PROB_BITS[256 - 104] + PROB_BITS[128],
424 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[199] + PROB_BITS[140] + PROB_BITS[125] + PROB_BITS[104] + PROB_BITS[128],
425 PROB_BITS[256 - 205] + PROB_BITS[256 - 171] + PROB_BITS[256 - 199] + 8 * PROB_BITS[128],
426 ];
427
428 let mut nits = 0;
429 let mut tprobs = [0u8; 20];
430
431 for i in 0..20 {
432 let old = i16::from(base[i]);
433 let new = i16::from(probs[i]);
434 let mut diff = (new - old) & !3;
435 if old + diff > 255 {
436 diff -= 4;
437 } else if old + diff < 0 || (old + diff == 0 && new != 0) {
438 diff += 4;
439 }
440 tprobs[i] = (old + diff) as u8;
441 deltas[i] = diff;
442 nits += u32::from(DELTA_PROBS[(diff.abs() >> 2).min(7) as usize]);
443 }
444
445 Estimator::nits_to_bits(nits) + calc_mbtype_bits(&tprobs, stats, tmp) + 5
446}
447
448pub fn encode_mode_prob_models(bc: &mut BoolEncoder, models: &mut VP56Models, pmodels: &VP56Models, stats: &[[[usize; 10]; 10]; 3]) -> EncoderResult<()> {
449 let mut tmp = [VP56MBTypeModel::default(); 10];
450 let mut tprob = [0; 20];
451 for ctx in 0..3 {
452 let mut models_changed = models.prob_xmitted[ctx] != pmodels.prob_xmitted[ctx];
453 if models_changed {
454 let old_bits = calc_mbtype_bits(&pmodels.prob_xmitted[ctx], &stats[ctx], &mut tmp);
455 let new_bits = calc_mbtype_bits(&models.prob_xmitted[ctx], &stats[ctx], &mut tmp) + 4;
456 if new_bits < old_bits {
457 let idx = find_model_vq(&models.prob_xmitted[ctx], &VP56_MODE_VQ[ctx]);
458 for i in 0..20 {
459 tprob[i ^ 1] = VP56_MODE_VQ[ctx][idx][i];
460 }
461 let vq_bits = calc_mbtype_bits(&tprob, &stats[ctx], &mut tmp) + 4;
462 if vq_bits < old_bits {
463 bc.put_bool(true, 174)?;
464 bc.put_bits(idx as u32, 4)?;
465 let mut diffs_present = tprob != models.prob_xmitted[ctx];
466 let mut deltas = [0; 20];
467 let delta_cost = deltas_bits(&models.prob_xmitted[ctx], &tprob, &stats[ctx], &mut tmp, &mut deltas);
468 if delta_cost + 1 >= new_bits {
469 diffs_present = false;
470 }
471 if diffs_present {
472 bc.put_bool(true, 254)?;
473 for i in 0..20 {
474 let diff = deltas[i ^ 1] >> 2;
475 bc.put_bool(diff != 0, 205)?;
476 if diff != 0 {
477 let d0 = diff.abs().min(7) as u8;
478 bc.put_bool(diff < 0, 128)?;
479 bc.write_el(d0, MODE_TREE_DIFF, MODE_TREE_DIFF_PROBS)?;
480 if d0 == 7 {
481 bc.put_bits(diff.abs() as u32, 7)?;
482 }
483 tprob[i ^ 1] = (i16::from(tprob[i ^ 1]) + deltas[i ^ 1]) as u8;
484 }
485 }
486 }
487 if !diffs_present {
488 bc.put_bool(false, 254)?;
489 }
490 } else {
491 models_changed = false;
492 }
493 } else {
494 models_changed = false;
495 }
496 }
497 if !models_changed {
498 bc.put_bool(false, 174)?;
499 bc.put_bool(false, 254)?;
500 models.prob_xmitted[ctx] = pmodels.prob_xmitted[ctx];
501 } else {
502 models.prob_xmitted[ctx] = tprob;
503 }
504 }
505 for ctx in 0..3 {
506 let prob_xmitted = &models.prob_xmitted[ctx];
507 calc_mb_model_probs(prob_xmitted, &mut models.mbtype_models[ctx]);
508 }
509 Ok(())
510}
511
512pub fn encode_mv_models(bc: &mut BoolEncoder, models: &[VP56MVModel; 2], pmodels: &[VP56MVModel; 2]) -> EncoderResult<()> {
513 for (i, (mdl, pmdl)) in models.iter().zip(pmodels.iter()).enumerate() {
514 bc.encode_probability(mdl.nz_prob, pmdl.nz_prob, HAS_NZ_PROB[i])?;
515 bc.encode_probability(mdl.sign_prob, pmdl.sign_prob, HAS_SIGN_PROB[i])?;
516 }
517 for (i, (mdl, pmdl)) in models.iter().zip(pmodels.iter()).enumerate() {
518 for (&coded_prob, (&prob, &pprob)) in HAS_TREE_PROB[i].iter().zip(mdl.tree_probs.iter().zip(pmdl.tree_probs.iter())) {
519 bc.encode_probability(prob, pprob, coded_prob)?;
520 }
521 }
522 for (i, (mdl, pmdl)) in models.iter().zip(pmodels.iter()).enumerate() {
523 for (&coded_prob, (&prob, &pprob)) in HAS_RAW_PROB[i].iter().zip(mdl.raw_probs.iter().zip(pmdl.raw_probs.iter())) {
524 bc.encode_probability(prob, pprob, coded_prob)?;
525 }
526 }
527 Ok(())
528}
529
530pub fn encode_coeff_models(bc: &mut BoolEncoder, models: &mut VP56Models, pmodels: &VP56Models, is_intra: bool, interlaced: bool) -> EncoderResult<()> {
531 let mut def_prob = [128u8; 11];
532 for plane in 0..2 {
533 for i in 0..11 {
534 let pprob = pmodels.coeff_models[plane].dc_value_probs[i];
535 let prob = models.coeff_models[plane].dc_value_probs[i];
536 let changed = (is_intra && prob != def_prob[i]) || (!is_intra && prob != pprob);
537 bc.put_bool(changed, HAS_COEF_PROBS[plane][i])?;
538 if changed {
539 bc.put_probability(prob)?;
540 def_prob[i] = prob;
541 }
542 }
543 }
544
545 bc.put_bool(false, 128)?;
546 reset_scan(&mut models.vp6models, interlaced);
547 /* for scan
548 for i in 1..64 {
549 if bc.read_prob(HAS_SCAN_UPD_PROBS[i]) {
550 models.vp6models.scan_order[i] = bc.read_bits(4) as usize;
551 }
552 }
553 update_scan(&mut models.vp6models);
554 */
555
556 for comp in 0..2 {
557 for i in 0..14 {
558 bc.encode_probability(models.vp6models.zero_run_probs[comp][i], pmodels.vp6models.zero_run_probs[comp][i], HAS_ZERO_RUN_PROBS[comp][i])?;
559 }
560 }
561
562 for ctype in 0..3 {
563 for plane in 0..2 {
564 for group in 0..6 {
565 for i in 0..11 {
566 let pprob = pmodels.coeff_models[plane].ac_val_probs[ctype][group][i];
567 let prob = models.coeff_models[plane].ac_val_probs[ctype][group][i];
568 let changed = (is_intra && prob != def_prob[i]) || (!is_intra && prob != pprob);
569 bc.put_bool(changed, VP6_AC_PROBS[ctype][plane][group][i])?;
570 if changed {
571 bc.put_probability(prob)?;
572 def_prob[i] = prob;
573 }
574 }
575 }
576 }
577 }
578
579 for plane in 0..2 {
580 let mdl = &mut models.coeff_models[plane];
581 for i in 0..3 {
582 for k in 0..5 {
583 mdl.dc_token_probs[0][i][k] = rescale_prob(mdl.dc_value_probs[k], &VP6_DC_WEIGHTS[k][i], 255);
584 }
585 }
586 }
587 Ok(())
588}
589
590pub fn encode_block(bc: &mut BoolEncoder, blk: &[i16; 64], dc_mode: usize, model: &VP56CoeffModel, vp6model: &VP6Models) -> EncoderResult<()> {
591 let mut last = 64;
592 for i in (0..64).rev() {
593 if blk[vp6model.zigzag[i]] != 0 {
594 last = i;
595 break;
596 }
597 }
598 if last < 64 {
599 bc.write_dc(blk[0], &model.dc_token_probs[0][dc_mode], &model.dc_value_probs)?;
600 let mut idx = 1;
601 let mut last_idx = 0;
602 let mut last_val = blk[0];
603 while idx <= last {
604 let val = blk[vp6model.zigzag[idx]];
605 let has_nnz = (idx == 1) || (last_val != 0);
606 if (val != 0) || has_nnz {
607 if last_val == 0 && idx != 1 {
608 let zrun = idx - last_idx;
609 bc.write_zero_run(zrun, &vp6model.zero_run_probs[if last_idx + 1 >= 7 { 1 } else { 0 }])?;
610 }
611 let ac_band = VP6_IDX_TO_AC_BAND[idx];
612 let ac_mode = last_val.abs().min(2) as usize;
613 let tree = if has_nnz { COEF_TREE } else { NZ_COEF_TREE };
614 bc.write_ac(val, tree, &model.ac_val_probs[ac_mode][ac_band])?;
615 last_val = val;
616 last_idx = idx;
617 }
618 idx += 1;
619 }
620 if idx < 64 {
621 let ac_band = VP6_IDX_TO_AC_BAND[idx];
622 let ac_mode = last_val.abs().min(2) as usize;
623 bc.write_el(EOB, COEF_TREE, &model.ac_val_probs[ac_mode][ac_band])?;
624 }
625 } else {
626 bc.write_cat(0, DC_TREE, &model.dc_token_probs[0][dc_mode], &model.dc_value_probs)?;
627 let ac_band = VP6_IDX_TO_AC_BAND[1];
628 bc.write_el(EOB, COEF_TREE, &model.ac_val_probs[0][ac_band])?;
629 }
630 Ok(())
631}
632
633fn map_mb_type(mbtype: VPMBType) -> usize {
634 match mbtype {
635 VPMBType::InterNoMV => 0,
636 VPMBType::Intra => 1,
637 VPMBType::InterMV => 2,
638 VPMBType::InterNearest => 3,
639 VPMBType::InterNear => 4,
640 VPMBType::GoldenNoMV => 5,
641 VPMBType::GoldenMV => 6,
642 VPMBType::InterFourMV => 7,
643 VPMBType::GoldenNearest => 8,
644 VPMBType::GoldenNear => 9,
645 }
646}
647
648pub fn encode_mb_type(bc: &mut BoolEncoder, mb_type: VPMBType, last_mb_type: VPMBType, ctx: usize, model: &VP56Models) -> EncoderResult<()> {
649 let probs = &model.mbtype_models[ctx][map_mb_type(last_mb_type)].probs;
650 bc.put_bool(mb_type == last_mb_type, probs[9])?;
651 if mb_type != last_mb_type {
652 bc.write_el(mb_type, MODE_TREE, probs)?;
653 }
654 Ok(())
655}
656
657fn encode_mv_component(bc: &mut BoolEncoder, mv: i16, model: &VP56MVModel) -> EncoderResult<()> {
658 let aval = mv.abs();
659 bc.put_bool(aval >= 8, model.nz_prob)?;
660 if aval < 8 {
661 bc.write_el(aval as u8, SHORT_MV_TREE, &model.tree_probs)?;
662 } else {
663 for &ord in LONG_VECTOR_ORDER.iter() {
664 bc.put_bool(((aval >> ord) & 1) != 0, model.raw_probs[ord])?;
665 }
666 if (aval & 0xF0) != 0 {
667 bc.put_bool((aval & (1 << 3)) != 0, model.raw_probs[3])?;
668 }
669 }
670 if aval != 0 {
671 bc.put_bool(mv < 0, model.sign_prob)?;
672 }
673 Ok(())
674}
675
676pub fn encode_mv(bc: &mut BoolEncoder, mv: MV, model: &VP56Models) -> EncoderResult<()> {
677 encode_mv_component(bc, mv.x, &model.mv_models[0])?;
678 encode_mv_component(bc, mv.y, &model.mv_models[1])?;
679 Ok(())
680}
681
682struct Estimator {}
683
684impl Estimator {
685 fn new() -> Self { Self{} }
686 fn write_el<T: PartialEq>(&self, el: T, tree: &[TokenSeq<T>], probs: &mut [ProbCounter]) {
687 for entry in tree.iter() {
688 if entry.val == el {
689 for seq in entry.seq.iter() {
690 probs[seq.idx as usize].add(seq.bit);
691 }
692 return;
693 }
694 }
695 }
696 fn write_cat(&self, cat: i8, tree: &[TokenSeq<i8>], probs: &mut [ProbCounter; 11]) {
697 for entry in tree.iter() {
698 if entry.val == cat {
699 for seq in entry.seq.iter() {
700 probs[seq.idx as usize].add(seq.bit);
701 }
702 return;
703 }
704 }
705 }
706 fn write_dc(&self, val: i16, probs: &mut [ProbCounter; 11]) {
707 self.write_cat(coef_to_cat(val), DC_TREE, probs);
708 }
709 fn write_ac(&self, val: i16, tree: &[TokenSeq<i8>], probs: &mut [ProbCounter; 11]) {
710 self.write_cat(coef_to_cat(val), tree, probs);
711 }
712 fn write_zero_run(&self, val: usize, probs: &mut [ProbCounter; 14]) {
713 self.write_el(val.min(9) as u8, ZERO_RUN_TREE, probs);
714 if val >= 9 {
715 let add = val - 9;
716 for i in 0..6 {
717 probs[i + 8].add(((add >> i) & 1) != 0);
718 }
719 }
720 }
721 fn est_nits(bit: bool, prob: u8) -> u32 {
722 if !bit {
723 u32::from(PROB_BITS[prob as usize])
724 } else {
725 u32::from(PROB_BITS[256 - (prob as usize)])
726 }
727 }
728 fn nits_to_bits(nits: u32) -> u32 { (nits + 7) >> 3 }
729}
730
731pub fn estimate_block(blk: &[i16; 64], _dc_mode: usize, model: &mut VP56CoeffModelStat, vp6model: &mut VP6ModelsStat, scan: &[usize; 64]) {
732 let bc = Estimator::new();
733
734 let mut last = 64;
735 for i in (0..64).rev() {
736 if blk[scan[i]] != 0 {
737 last = i;
738 break;
739 }
740 }
741 if last < 64 {
742 bc.write_dc(blk[0], &mut model.dc_value_probs);
743 let mut idx = 1;
744 let mut last_idx = 0;
745 let mut last_val = blk[0];
746 while idx <= last {
747 let val = blk[scan[idx]];
748 let has_nnz = (idx == 1) || (last_val != 0);
749 if (val != 0) || has_nnz {
750 if last_val == 0 && idx != 1 {
751 let zrun = idx - last_idx;
752 bc.write_zero_run(zrun, &mut vp6model.zero_run_probs[if last_idx + 1 >= 7 { 1 } else { 0 }]);
753 }
754 let ac_band = VP6_IDX_TO_AC_BAND[idx];
755 let ac_mode = last_val.abs().min(2) as usize;
756 let tree = if has_nnz { COEF_TREE } else { NZ_COEF_TREE };
757 bc.write_ac(val, tree, &mut model.ac_val_probs[ac_mode][ac_band]);
758 last_val = val;
759 last_idx = idx;
760 }
761 idx += 1;
762 }
763 if idx < 64 {
764 let ac_band = VP6_IDX_TO_AC_BAND[idx];
765 let ac_mode = last_val.abs().min(2) as usize;
766 bc.write_el(EOB, COEF_TREE, &mut model.ac_val_probs[ac_mode][ac_band]);
767 }
768 } else {
769 bc.write_cat(0, DC_TREE, &mut model.dc_value_probs);
770 let ac_band = VP6_IDX_TO_AC_BAND[1];
771 bc.write_el(EOB, COEF_TREE, &mut model.ac_val_probs[0][ac_band]);
772 }
773}
774
775pub fn estimate_mb_type(mb_type: VPMBType, last_mb_type: VPMBType, ctx: usize, model: &mut VP56ModelsStat) {
776 model.mbtype_models[ctx][map_mb_type(last_mb_type)][map_mb_type(mb_type)] += 1;
777}
778
779fn estimate_mv_component(mv: i16, model: &mut VP56MVModelStat) {
780 let bc = Estimator::new();
781 let aval = mv.abs();
782 model.nz_prob.add(aval >= 8);
783 if aval < 8 {
784 bc.write_el(aval as u8, SHORT_MV_TREE, &mut model.tree_probs);
785 } else {
786 for &ord in LONG_VECTOR_ORDER.iter() {
787 model.raw_probs[ord].add(((aval >> ord) & 1) != 0);
788 }
789 if (aval & 0xF0) != 0 {
790 model.raw_probs[3].add((aval & (1 << 3)) != 0);
791 }
792 }
793 if aval != 0 {
794 model.sign_prob.add(mv < 0);
795 }
796}
797
798pub fn estimate_mv(mv: MV, model: &mut VP56ModelsStat) {
799 estimate_mv_component(mv.x, &mut model.mv_models[0]);
800 estimate_mv_component(mv.y, &mut model.mv_models[1]);
801}
802
803const VP56_MODE_VQ: [[[u8; 20]; 16]; 3] = [
804 [
805 [ 9, 15, 32, 25, 7, 19, 9, 21, 1, 12, 14, 12, 3, 18, 14, 23, 3, 10, 0, 4 ],
806 [ 48, 39, 1, 2, 11, 27, 29, 44, 7, 27, 1, 4, 0, 3, 1, 6, 1, 2, 0, 0 ],
807 [ 21, 32, 1, 2, 4, 10, 32, 43, 6, 23, 2, 3, 1, 19, 1, 6, 12, 21, 0, 7 ],
808 [ 69, 83, 0, 0, 0, 2, 10, 29, 3, 12, 0, 1, 0, 3, 0, 3, 2, 2, 0, 0 ],
809 [ 11, 20, 1, 4, 18, 36, 43, 48, 13, 35, 0, 2, 0, 5, 3, 12, 1, 2, 0, 0 ],
810 [ 70, 44, 0, 1, 2, 10, 37, 46, 8, 26, 0, 2, 0, 2, 0, 2, 0, 1, 0, 0 ],
811 [ 8, 15, 0, 1, 8, 21, 74, 53, 22, 42, 0, 1, 0, 2, 0, 3, 1, 2, 0, 0 ],
812 [ 141, 42, 0, 0, 1, 4, 11, 24, 1, 11, 0, 1, 0, 1, 0, 2, 0, 0, 0, 0 ],
813 [ 8, 19, 4, 10, 24, 45, 21, 37, 9, 29, 0, 3, 1, 7, 11, 25, 0, 2, 0, 1 ],
814 [ 46, 42, 0, 1, 2, 10, 54, 51, 10, 30, 0, 2, 0, 2, 0, 1, 0, 1, 0, 0 ],
815 [ 28, 32, 0, 0, 3, 10, 75, 51, 14, 33, 0, 1, 0, 2, 0, 1, 1, 2, 0, 0 ],
816 [ 100, 46, 0, 1, 3, 9, 21, 37, 5, 20, 0, 1, 0, 2, 1, 2, 0, 1, 0, 0 ],
817 [ 27, 29, 0, 1, 9, 25, 53, 51, 12, 34, 0, 1, 0, 3, 1, 5, 0, 2, 0, 0 ],
818 [ 80, 38, 0, 0, 1, 4, 69, 33, 5, 16, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0 ],
819 [ 16, 20, 0, 0, 2, 8, 104, 49, 15, 33, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0 ],
820 [ 194, 16, 0, 0, 1, 1, 1, 9, 1, 3, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0 ],
821 ], [
822 [ 41, 22, 1, 0, 1, 31, 0, 0, 0, 0, 0, 1, 1, 7, 0, 1, 98, 25, 4, 10 ],
823 [ 123, 37, 6, 4, 1, 27, 0, 0, 0, 0, 5, 8, 1, 7, 0, 1, 12, 10, 0, 2 ],
824 [ 26, 14, 14, 12, 0, 24, 0, 0, 0, 0, 55, 17, 1, 9, 0, 36, 5, 7, 1, 3 ],
825 [ 209, 5, 0, 0, 0, 27, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 ],
826 [ 2, 5, 4, 5, 0, 121, 0, 0, 0, 0, 0, 3, 2, 4, 1, 4, 2, 2, 0, 1 ],
827 [ 175, 5, 0, 1, 0, 48, 0, 0, 0, 0, 0, 2, 0, 1, 0, 2, 0, 1, 0, 0 ],
828 [ 83, 5, 2, 3, 0, 102, 0, 0, 0, 0, 1, 3, 0, 2, 0, 1, 0, 0, 0, 0 ],
829 [ 233, 6, 0, 0, 0, 8, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0 ],
830 [ 34, 16, 112, 21, 1, 28, 0, 0, 0, 0, 6, 8, 1, 7, 0, 3, 2, 5, 0, 2 ],
831 [ 159, 35, 2, 2, 0, 25, 0, 0, 0, 0, 3, 6, 0, 5, 0, 1, 4, 4, 0, 1 ],
832 [ 75, 39, 5, 7, 2, 48, 0, 0, 0, 0, 3, 11, 2, 16, 1, 4, 7, 10, 0, 2 ],
833 [ 212, 21, 0, 1, 0, 9, 0, 0, 0, 0, 1, 2, 0, 2, 0, 0, 2, 2, 0, 0 ],
834 [ 4, 2, 0, 0, 0, 172, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0 ],
835 [ 187, 22, 1, 1, 0, 17, 0, 0, 0, 0, 3, 6, 0, 4, 0, 1, 4, 4, 0, 1 ],
836 [ 133, 6, 1, 2, 1, 70, 0, 0, 0, 0, 0, 2, 0, 4, 0, 3, 1, 1, 0, 0 ],
837 [ 251, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
838 ], [
839 [ 2, 3, 2, 3, 0, 2, 0, 2, 0, 0, 11, 4, 1, 4, 0, 2, 3, 2, 0, 4 ],
840 [ 49, 46, 3, 4, 7, 31, 42, 41, 0, 0, 2, 6, 1, 7, 1, 4, 2, 4, 0, 1 ],
841 [ 26, 25, 1, 1, 2, 10, 67, 39, 0, 0, 1, 1, 0, 14, 0, 2, 31, 26, 1, 6 ],
842 [ 103, 46, 1, 2, 2, 10, 33, 42, 0, 0, 1, 4, 0, 3, 0, 1, 1, 3, 0, 0 ],
843 [ 14, 31, 9, 13, 14, 54, 22, 29, 0, 0, 2, 6, 4, 18, 6, 13, 1, 5, 0, 1 ],
844 [ 85, 39, 0, 0, 1, 9, 69, 40, 0, 0, 0, 1, 0, 3, 0, 1, 2, 3, 0, 0 ],
845 [ 31, 28, 0, 0, 3, 14, 130, 34, 0, 0, 0, 1, 0, 3, 0, 1, 3, 3, 0, 1 ],
846 [ 171, 25, 0, 0, 1, 5, 25, 21, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 ],
847 [ 17, 21, 68, 29, 6, 15, 13, 22, 0, 0, 6, 12, 3, 14, 4, 10, 1, 7, 0, 3 ],
848 [ 51, 39, 0, 1, 2, 12, 91, 44, 0, 0, 0, 2, 0, 3, 0, 1, 2, 3, 0, 1 ],
849 [ 81, 25, 0, 0, 2, 9, 106, 26, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0 ],
850 [ 140, 37, 0, 1, 1, 8, 24, 33, 0, 0, 1, 2, 0, 2, 0, 1, 1, 2, 0, 0 ],
851 [ 14, 23, 1, 3, 11, 53, 90, 31, 0, 0, 0, 3, 1, 5, 2, 6, 1, 2, 0, 0 ],
852 [ 123, 29, 0, 0, 1, 7, 57, 30, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0 ],
853 [ 13, 14, 0, 0, 4, 20, 175, 20, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0 ],
854 [ 202, 23, 0, 0, 1, 3, 2, 9, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 ],
855 ]
856];