]> git.nihav.org Git - nihav.git/blob - nihav-rad/src/codecs/bink2.rs
introduce option handling for demuxers
[nihav.git] / nihav-rad / src / codecs / bink2.rs
1 use std::mem;
2 use nihav_core::codecs::*;
3 use nihav_core::io::byteio::*;
4 use nihav_core::io::bitreader::*;
5 use nihav_core::io::codebook::*;
6 use nihav_core::io::intcode::*;
7 use nihav_codec_support::codecs::{IPShuffler, MV, ZERO_MV};
8
9 macro_rules! mktag {
10 ($a:expr, $b:expr, $c:expr, $d:expr) => ({
11 (($a as u32) << 24) | (($b as u32) << 16) | (($c as u32) << 8) | ($d as u32)
12 });
13 ($arr:expr) => ({
14 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
15 });
16 }
17
18 macro_rules! idct_mul {
19 (a; $val: expr) => ($val + ($val >> 2));
20 (b; $val: expr) => ($val >> 1);
21 (c; $val: expr) => ($val - ($val >> 2) - ($val >> 4));
22 (d; $val: expr) => ($val + ($val >> 2) - ($val >> 4));
23 (e; $val: expr) => ($val >> 2);
24 }
25
26 macro_rules! idct {
27 ($src: expr, $sstep: expr, $off: expr, $dst: expr, $dstep: expr, $doff: expr, $bias: expr, $shift: expr) => {
28 let tmp00 = $src[$off + 3 * $sstep] + $src[$off + 5 * $sstep];
29 let tmp01 = $src[$off + 3 * $sstep] - $src[$off + 5 * $sstep];
30 let tmp02 = idct_mul!(a; $src[$off + 2 * $sstep]) + idct_mul!(b; $src[$off + 6 * $sstep]);
31 let tmp03 = idct_mul!(b; $src[$off + 2 * $sstep]) - idct_mul!(a; $src[$off + 6 * $sstep]);
32 let tmp0 = ($src[$off + 0 * $sstep] + $src[$off + 4 * $sstep]) + tmp02;
33 let tmp1 = ($src[$off + 0 * $sstep] + $src[$off + 4 * $sstep]) - tmp02;
34 let tmp2 = $src[$off + 0 * $sstep] - $src[$off + 4 * $sstep];
35 let tmp3 = $src[$off + 1 * $sstep] + tmp00;
36 let tmp4 = $src[$off + 1 * $sstep] - tmp00;
37 let tmp5 = tmp01 + $src[$off + 7 * $sstep];
38 let tmp6 = tmp01 - $src[$off + 7 * $sstep];
39 let tmp7 = tmp4 + idct_mul!(c; tmp6);
40 let tmp8 = idct_mul!(c; tmp4) - tmp6;
41 let tmp9 = idct_mul!(d; tmp3) + idct_mul!(e; tmp5);
42 let tmp10 = idct_mul!(e; tmp3) - idct_mul!(d; tmp5);
43 let tmp11 = tmp2 + tmp03;
44 let tmp12 = tmp2 - tmp03;
45
46 $dst[$doff + 0 * $dstep] = (tmp0 + tmp9 + $bias) >> $shift;
47 $dst[$doff + 1 * $dstep] = (tmp11 + tmp7 + $bias) >> $shift;
48 $dst[$doff + 2 * $dstep] = (tmp12 + tmp8 + $bias) >> $shift;
49 $dst[$doff + 3 * $dstep] = (tmp1 + tmp10 + $bias) >> $shift;
50 $dst[$doff + 4 * $dstep] = (tmp1 - tmp10 + $bias) >> $shift;
51 $dst[$doff + 5 * $dstep] = (tmp12 - tmp8 + $bias) >> $shift;
52 $dst[$doff + 6 * $dstep] = (tmp11 - tmp7 + $bias) >> $shift;
53 $dst[$doff + 7 * $dstep] = (tmp0 - tmp9 + $bias) >> $shift;
54 };
55 (float; $src: expr, $sstep: expr, $off: expr, $dst: expr, $dstep: expr, $doff: expr, $bias: expr, $shift: expr) => {
56 let t00 = $src[$off + $sstep * 2] + $src[$off + $sstep * 6];
57 let t01 = ($src[$off + $sstep * 2] - $src[$off + $sstep * 6]) * std::f32::consts::SQRT_2 - t00;
58 let t02 = $src[$off + $sstep * 0] + $src[$off + $sstep * 4];
59 let t03 = $src[$off + $sstep * 0] - $src[$off + $sstep * 4];
60 let t04 = $src[$off + $sstep * 3] + $src[$off + $sstep * 5];
61 let t05 = $src[$off + $sstep * 3] - $src[$off + $sstep * 5];
62 let t06 = $src[$off + $sstep * 1] + $src[$off + $sstep * 7];
63 let t07 = $src[$off + $sstep * 1] - $src[$off + $sstep * 7];
64 let t08 = t02 + t00;
65 let t09 = t02 - t00;
66 let t10 = t03 + t01;
67 let t11 = t03 - t01;
68 let t12 = t06 + t04;
69 let t13 = (t06 - t04) * std::f32::consts::SQRT_2;
70 let t14 = (t07 - t05) * 1.847759;
71 let t15 = t05 * 2.613126 + t14 - t12;
72 let t16 = t13 - t15;
73 let t17 = t07 * 1.0823922 - t14 + t16;
74
75 $dst[$doff + 0 * $dstep] = t08 + t12;
76 $dst[$doff + 1 * $dstep] = t10 + t15;
77 $dst[$doff + 2 * $dstep] = t11 + t16;
78 $dst[$doff + 3 * $dstep] = t09 - t17;
79 $dst[$doff + 4 * $dstep] = t09 + t17;
80 $dst[$doff + 5 * $dstep] = t11 - t16;
81 $dst[$doff + 6 * $dstep] = t10 - t15;
82 $dst[$doff + 7 * $dstep] = t08 - t12;
83 };
84 }
85
86 #[allow(clippy::erasing_op)]
87 fn bink2_idct(coeffs: &mut [i32; 64]) {
88 let mut tmp: [i32; 64] = [0; 64];
89 for i in 0..8 {
90 idct!(coeffs, 1, 8 * i, tmp, 8, i, 0, 0);
91 }
92 for i in 0..8 {
93 idct!(tmp, 1, 8 * i, coeffs, 1, 8 * i, 0, 6);
94 }
95 }
96
97 #[allow(clippy::erasing_op)]
98 fn bink2_idct_old(coeffs: &mut [f32; 64]) {
99 let mut tmp: [f32; 64] = [0.0; 64];
100 coeffs[0] += 512.5;
101 for i in 0..8 {
102 idct!(float; coeffs, 8, i, tmp, 8, i, 0, 0);
103 }
104 for i in 0..8 {
105 idct!(float; tmp, 1, 8 * i, coeffs, 1, 8 * i, 0, 6);
106 }
107 }
108
109 struct Bink2DSP { }
110
111 fn clip8(val: i32) -> u8 {
112 val.min(255).max(0) as u8
113 }
114
115 macro_rules! el {
116 ($src: expr, $off: expr) => (
117 $src[$off] as i32
118 );
119 }
120
121 macro_rules! luma_filter {
122 ($src: expr, $off: expr, $step: expr) => ({
123 let t0 = el!($src, $off - 0 * $step) + el!($src, $off + 1 * $step);
124 let t1 = el!($src, $off - 1 * $step) + el!($src, $off + 2 * $step);
125 let t2 = el!($src, $off - 2 * $step) + el!($src, $off + 3 * $step);
126 (((t0 * 19) >> 1) - t1 * 2 + (t2 >> 1) + 8) >> 4
127 });
128 }
129
130 macro_rules! chroma_interp {
131 ($dst: expr, $dstride: expr, $h: expr, $ty: tt, $src: expr, $sstride: expr, $step: expr, $mode: expr, $shift: expr) => {
132 let mut soff = 0;
133 for out in $dst.chunks_mut($dstride).take($h) {
134 for i in 0..8 {
135 let e0 = el!($src, soff + i);
136 let e1 = el!($src, soff + i + $step);
137 out[i] = match $mode {
138 0 => e0,
139 1 => (e0 * 3 + e1 + $shift) >> $shift,
140 2 => (e0 * 2 + e1 * 2 + $shift) >> $shift,
141 _ => (e0 + e1 * 3 + $shift) >> $shift,
142 } as $ty;
143 }
144 soff += $sstride;
145 }
146 };
147 }
148
149 macro_rules! avg_tree {
150 ($a: expr, $b: expr) => (($a + $b + 1) >> 1);
151 ($a: expr, $b: expr, $c: expr, $d: expr) => (avg_tree!(avg_tree!($a, $b), avg_tree!($c, $d)));
152 }
153
154 #[allow(clippy::erasing_op)]
155 impl Bink2DSP {
156 fn calc_dc(src: &[u8], stride: usize) -> i32 {
157 let mut sums = [0u16; 8];
158 for i in 0..8 {
159 let s0 = src[i + stride * 0] as u16;
160 let s1 = src[i + stride * 1] as u16;
161 let s2 = src[i + stride * 2] as u16;
162 let s3 = src[i + stride * 3] as u16;
163 let s4 = src[i + stride * 4] as u16;
164 let s5 = src[i + stride * 5] as u16;
165 let s6 = src[i + stride * 6] as u16;
166 let s7 = src[i + stride * 7] as u16;
167 sums[i] = avg_tree!(avg_tree!(s0, s1, s2, s3), avg_tree!(s4, s5, s6, s7));
168 }
169 let mut sum = 0;
170 for e in sums.iter() {
171 sum += e;
172 }
173 sum as i32
174 }
175 fn put_mb4(dst: &mut [u8], mut off: usize, stride: usize, blk: &mut [[i32; 64]; 4]) {
176 bink2_idct(&mut blk[0]);
177 bink2_idct(&mut blk[1]);
178 bink2_idct(&mut blk[2]);
179 bink2_idct(&mut blk[3]);
180 {
181 let dout = &mut dst[off..];
182 for (row, (b0, b1)) in dout.chunks_mut(stride).zip(blk[0].chunks(8).zip(blk[1].chunks(8))) {
183 for i in 0..8 { row[i + 0] = clip8(b0[i]); }
184 for i in 0..8 { row[i + 8] = clip8(b1[i]); }
185 }
186 }
187 off += stride * 8;
188 {
189 let dout = &mut dst[off..];
190 for (row, (b2, b3)) in dout.chunks_mut(stride).zip(blk[2].chunks(8).zip(blk[3].chunks(8))) {
191 for i in 0..8 { row[i + 0] = clip8(b2[i]); }
192 for i in 0..8 { row[i + 8] = clip8(b3[i]); }
193 }
194 }
195 }
196 fn add_mb4(dst: &mut [u8], mut off: usize, stride: usize, blk: &mut [[i32; 64]; 4]) {
197 bink2_idct(&mut blk[0]);
198 bink2_idct(&mut blk[1]);
199 bink2_idct(&mut blk[2]);
200 bink2_idct(&mut blk[3]);
201 {
202 let dout = &mut dst[off..];
203 for (row, (b0, b1)) in dout.chunks_mut(stride).zip(blk[0].chunks(8).zip(blk[1].chunks(8))) {
204 for i in 0..8 { row[i + 0] = clip8((row[i + 0] as i32) + b0[i]); }
205 for i in 0..8 { row[i + 8] = clip8((row[i + 8] as i32) + b1[i]); }
206 }
207 }
208 off += stride * 8;
209 {
210 let dout = &mut dst[off..];
211 for (row, (b2, b3)) in dout.chunks_mut(stride).zip(blk[2].chunks(8).zip(blk[3].chunks(8))) {
212 for i in 0..8 { row[i + 0] = clip8((row[i + 0] as i32) + b2[i]); }
213 for i in 0..8 { row[i + 8] = clip8((row[i + 8] as i32) + b3[i]); }
214 }
215 }
216 }
217 fn put_mb4_old(dst: &mut [u8], mut off: usize, stride: usize, blk: &mut [[f32; 64]; 4]) {
218 bink2_idct_old(&mut blk[0]);
219 bink2_idct_old(&mut blk[1]);
220 bink2_idct_old(&mut blk[2]);
221 bink2_idct_old(&mut blk[3]);
222 {
223 let dout = &mut dst[off..];
224 for (row, (b0, b1)) in dout.chunks_mut(stride).zip(blk[0].chunks(8).zip(blk[1].chunks(8))) {
225 for i in 0..8 { row[i + 0] = clip8((b0[i] as i32) - 512); }
226 for i in 0..8 { row[i + 8] = clip8((b1[i] as i32) - 512); }
227 }
228 }
229 off += stride * 8;
230 {
231 let dout = &mut dst[off..];
232 for (row, (b2, b3)) in dout.chunks_mut(stride).zip(blk[2].chunks(8).zip(blk[3].chunks(8))) {
233 for i in 0..8 { row[i + 0] = clip8((b2[i] as i32) - 512); }
234 for i in 0..8 { row[i + 8] = clip8((b3[i] as i32) - 512); }
235 }
236 }
237 }
238 fn add_mb4_old(dst: &mut [u8], mut off: usize, stride: usize, blk: &mut [[f32; 64]; 4]) {
239 bink2_idct_old(&mut blk[0]);
240 bink2_idct_old(&mut blk[1]);
241 bink2_idct_old(&mut blk[2]);
242 bink2_idct_old(&mut blk[3]);
243 {
244 let dout = &mut dst[off..];
245 for (row, (b0, b1)) in dout.chunks_mut(stride).zip(blk[0].chunks(8).zip(blk[1].chunks(8))) {
246 for i in 0..8 { row[i + 0] = clip8((row[i + 0] as i32) + (b0[i] as i32) - 512); }
247 for i in 0..8 { row[i + 8] = clip8((row[i + 8] as i32) + (b1[i] as i32) - 512); }
248 }
249 }
250 off += stride * 8;
251 {
252 let dout = &mut dst[off..];
253 for (row, (b2, b3)) in dout.chunks_mut(stride).zip(blk[2].chunks(8).zip(blk[3].chunks(8))) {
254 for i in 0..8 { row[i + 0] = clip8((row[i + 0] as i32) + (b2[i] as i32) - 512); }
255 for i in 0..8 { row[i + 8] = clip8((row[i + 8] as i32) + (b3[i] as i32) - 512); }
256 }
257 }
258 }
259 fn mc_luma(dst: &mut [u8], stride: usize, ref_pic: &NAVideoBuffer<u8>, xoff: usize, yoff: usize, mv: MV, plane: usize) -> DecoderResult<()> {
260 let sx = (xoff as isize) + ((mv.x >> 1) as isize);
261 let sy = (yoff as isize) + ((mv.y >> 1) as isize);
262 let mode = (mv.x & 1) + (mv.y & 1) * 2;
263 let (d_x, add_x) = if (mv.x & 1) != 0 { (2, 5) } else { (0, 0) };
264 let (d_y, add_y) = if (mv.y & 1) != 0 { (2, 5) } else { (0, 0) };
265
266 let (w, h) = ref_pic.get_dimensions(plane);
267 let align_w = ((w + 31) & !31) as isize;
268 let align_h = ((h + 31) & !31) as isize;
269 validate!((sx - d_x >= 0) && (sx - d_x + add_x + 16 <= align_w));
270 validate!((sy - d_y >= 0) && (sy - d_y + add_y + 16 <= align_h));
271 let pstride = ref_pic.get_stride(plane);
272 let mut poff = ref_pic.get_offset(plane) + (sx as usize) + (sy as usize) * pstride;
273 let pdata = ref_pic.get_data();
274 let ppix = pdata.as_slice();
275
276 let dst = &mut dst[xoff + (yoff & 31) * stride..];
277
278 match mode {
279 0 => {
280 let src = &ppix[poff..];
281 for (out, row) in dst.chunks_mut(stride).take(16).zip(src.chunks(pstride)) {
282 for i in 0..16 { out[i] = row[i]; }
283 }
284 },
285 1 => {
286 for out in dst.chunks_mut(stride).take(16) {
287 for i in 0..16 {
288 out[i] = clip8(luma_filter!(ppix, poff + i, 1));
289 }
290 poff += pstride;
291 }
292 },
293 2 => {
294 for out in dst.chunks_mut(stride).take(16) {
295 for i in 0..16 {
296 out[i] = clip8(luma_filter!(ppix, poff + i, pstride));
297 }
298 poff += pstride;
299 }
300 },
301 3 => {
302 let mut tmp = [0i16; 21 * 16];
303 for out in tmp.chunks_mut(16) {
304 for i in 0..16 {
305 out[i] = luma_filter!(ppix, poff - 2 * pstride + i, 1) as i16;
306 }
307 poff += pstride;
308 }
309 for (row, out) in dst.chunks_mut(stride).take(16).enumerate() {
310 for i in 0..16 {
311 out[i] = clip8(luma_filter!(tmp, (row + 2) * 16 + i, 16));
312 }
313 }
314 },
315 _ => unreachable!(),
316 };
317 Ok(())
318 }
319 fn mc_chroma(dst: &mut [u8], stride: usize, ref_pic: &NAVideoBuffer<u8>, xoff: usize, yoff: usize, mv: MV, plane: usize) -> DecoderResult<()> {
320 let sx = (xoff as isize) + ((mv.x >> 2) as isize);
321 let sy = (yoff as isize) + ((mv.y >> 2) as isize);
322 let mx = mv.x & 3;
323 let my = mv.y & 3;
324 let add_x = if mx != 0 { 1 } else { 0 };
325 let add_y = if my != 0 { 1 } else { 0 };
326
327 let (w, h) = ref_pic.get_dimensions(plane);
328 let align_w = ((w + 15) & !15) as isize;
329 let align_h = ((h + 15) & !15) as isize;
330 validate!((sx >= 0) && (sx + add_x + 8 <= align_w));
331 validate!((sy >= 0) && (sy + add_y + 8 <= align_h));
332 let pstride = ref_pic.get_stride(plane);
333 let poff = ref_pic.get_offset(plane) + (sx as usize) + (sy as usize) * pstride;
334 let pdata = ref_pic.get_data();
335 let ppix = pdata.as_slice();
336
337 let dst = &mut dst[xoff + (yoff & 15) * stride..];
338 if (mx == 0) && (my == 0) {
339 let inpix = &ppix[poff..];
340 for (out, src) in dst.chunks_mut(stride).take(8).zip(inpix.chunks(pstride)) {
341 for i in 0..8 { out[i] = src[i]; }
342 }
343 } else if my == 0 {
344 chroma_interp!(dst, stride, 8, u8, &ppix[poff..], pstride, 1, mx, 2);
345 } else if mx == 0 {
346 chroma_interp!(dst, stride, 8, u8, &ppix[poff..], pstride, pstride, my, 2);
347 } else {
348 let mut tmp = [0u16; 9 * 8];
349 chroma_interp!(tmp, 8, 9, u16, &ppix[poff..], pstride, 1, mx, 0);
350 chroma_interp!(dst, stride, 8, u8, &tmp, 8, 8, my, 4);
351 }
352 Ok(())
353 }
354 }
355
356 fn mid_pred<T:PartialOrd>(a: T, b: T, c: T) -> T {
357 if a < b {
358 if b < c {
359 b
360 } else {
361 if a < c { c } else { a }
362 }
363 } else {
364 if b < c {
365 if a < c { a } else { c }
366 } else {
367 b
368 }
369 }
370 }
371
372 #[derive(Default)]
373 struct QuantInfo {
374 quants: Vec<u8>,
375 qstride: usize,
376 qpos: usize,
377 pqpos: usize,
378 }
379
380 impl QuantInfo {
381 fn resize(&mut self, bw: usize) {
382 self.qstride = bw * 2;
383 self.quants.resize(self.qstride * 2, 0);
384 self.qpos = 0;
385 self.pqpos = self.qstride;
386 }
387 fn update_line(&mut self) {
388 mem::swap(&mut self.qpos, &mut self.pqpos);
389 }
390 fn pred_quant(&self, pos: usize, is_top: bool) -> u8 {
391 let is_left = pos < 2;
392 if is_top {
393 if is_left { 16 } else { self.quants[self.qpos + pos - 2] }
394 } else {
395 if is_left {
396 self.quants[self.pqpos + pos]
397 } else {
398 mid_pred(self.quants[self.pqpos + pos - 2],
399 self.quants[self.pqpos + pos],
400 self.quants[self.qpos + pos - 2])
401 }
402 }
403 }
404 fn set_quant(&mut self, pos: usize, q: u8) {
405 self.quants[self.qpos + pos] = q;
406 }
407 }
408
409 #[derive(Default)]
410 struct YDCInfo {
411 dcs: [i32; 16],
412 dc_buf: [i32; 16],
413 prev_dcs: Vec<i32>,
414 new_dcs: Vec<i32>,
415 stride: usize,
416 prev_off: usize,
417 }
418
419 impl YDCInfo {
420 fn resize(&mut self, bw: usize) {
421 self.stride = bw * 4 + 4;
422 self.prev_off = 4;
423 self.prev_dcs.resize(self.stride, 0);
424 self.new_dcs.resize(self.stride, 0);
425 }
426 fn update_line(&mut self) {
427 mem::swap(&mut self.prev_dcs, &mut self.new_dcs);
428 }
429 fn set_dcs(&mut self, bx: usize, dc5: i32, dc7: i32, dc13: i32, dc10: i32, dc11: i32, dc14: i32, dc15: i32) {
430 self.dcs[ 5] = dc5;
431 self.dcs[ 7] = dc7;
432 self.dcs[13] = dc13;
433 self.dcs[15] = dc15;
434 let new_dc = &mut self.new_dcs[bx * 4 + 4..];
435 new_dc[0] = dc10;
436 new_dc[1] = dc11;
437 new_dc[2] = dc14;
438 new_dc[3] = dc15;
439 }
440 }
441
442 #[derive(Default)]
443 struct CDCInfo {
444 dcs: [i32; 4],
445 dc_buf: [i32; 4],
446 prev_dcs: Vec<i32>,
447 new_dcs: Vec<i32>,
448 stride: usize,
449 prev_off: usize,
450 }
451
452 impl CDCInfo {
453 fn resize(&mut self, bw: usize) {
454 self.stride = bw * 2 + 2;
455 self.prev_off = 2;
456 self.prev_dcs.resize(self.stride, 0);
457 self.new_dcs.resize(self.stride, 0);
458 }
459 fn update_line(&mut self) {
460 mem::swap(&mut self.prev_dcs, &mut self.new_dcs);
461 }
462 fn set_dcs(&mut self, bx: usize, dc1: i32, dc2: i32, dc3: i32) {
463 self.dcs[1] = dc1;
464 self.dcs[2] = dc2;
465 self.dcs[3] = dc3;
466 let new_dc = &mut self.new_dcs[bx * 2 + 2..];
467 new_dc[0] = dc2;
468 new_dc[1] = dc3;
469 }
470 }
471
472 trait DCInfo {
473 fn decode_dcs(&mut self, br: &mut BitReader, q: u8) -> DecoderResult<bool>;
474 fn predict(&mut self, bx: usize, is_top: bool, is_left: bool, min_dc: i32, max_dc: i32);
475 fn predict_inter(&mut self, min_dc: i32, max_dc: i32);
476 }
477
478 fn dc_pred2(a: i32, b: i32) -> i32 {
479 (a.max(b)).min(a.min(b).max(a * 2 - b))
480 }
481 fn dc_pred(a: i32, b: i32, c: i32) -> i32 {
482 (a.max(b).max(c)).min((a.min(b).min(c)).max(b - a + c))
483 }
484
485 impl DCInfo for YDCInfo {
486 fn decode_dcs(&mut self, br: &mut BitReader, q: u8) -> DecoderResult<bool> {
487 decode_dcs(br, &mut self.dc_buf, q)
488 }
489 #[allow(non_snake_case)]
490 fn predict(&mut self, bx: usize, is_top: bool, is_left: bool, min_dc: i32, max_dc: i32) {
491 let a0; let a1; let a2; let a3; let a4; let a5; let a6; let a7;
492 let a8; let a9; let aA; let aB; let aC; let aD; let aE; let aF;
493 let prev_dc = &self.prev_dcs[bx * 4..];
494 let dst = &mut self.dcs;
495 let dcs = &self.dc_buf;
496 if is_left {
497 if is_top { //zigzag in 2x2 blocks
498 let add = 1 << 10;//if has_prev_dc { 1 << 10 } else { 0 };
499 a0 = (dcs[ 0] + add).max(min_dc).min(max_dc);
500 a1 = (dcs[ 1] + a0).max(min_dc).min(max_dc);
501 a2 = (dcs[ 2] + dc_pred2(a0, a1)).max(min_dc).min(max_dc);
502 a3 = (dcs[ 3] + dc_pred(a0, a1, a2)).max(min_dc).min(max_dc);
503
504 a4 = (dcs[ 4] + dc_pred2(a1, a3)).max(min_dc).min(max_dc);
505 a5 = (dcs[ 5] + a4).max(min_dc).min(max_dc);
506 a6 = (dcs[ 6] + dc_pred(a1, a3, a4)).max(min_dc).min(max_dc);
507 a7 = (dcs[ 7] + dc_pred(a4, a5, a6)).max(min_dc).min(max_dc);
508
509 a8 = (dcs[ 8] + dc_pred2(a2, a3)).max(min_dc).min(max_dc);
510 a9 = (dcs[ 9] + dc_pred(a2, a3, a8)).max(min_dc).min(max_dc);
511 aA = (dcs[10] + dc_pred2(a8, a9)).max(min_dc).min(max_dc);
512 aB = (dcs[11] + dc_pred(a8, a9, aA)).max(min_dc).min(max_dc);
513
514 aC = (dcs[12] + dc_pred(a3, a6, a9)).max(min_dc).min(max_dc);
515 aD = (dcs[13] + dc_pred(a6, a7, aC)).max(min_dc).min(max_dc);
516 aE = (dcs[14] + dc_pred(a9, aB, aC)).max(min_dc).min(max_dc);
517 aF = (dcs[15] + dc_pred(aC, aD, aE)).max(min_dc).min(max_dc);
518 } else {
519 a0 = (dcs[ 0] + dc_pred2(prev_dc[4], prev_dc[5])).max(min_dc).min(max_dc);
520 a1 = (dcs[ 1] + dc_pred(prev_dc[4], prev_dc[5], a0)).max(min_dc).min(max_dc);
521 a2 = (dcs[ 2] + dc_pred2(a0, a1)).max(min_dc).min(max_dc);
522 a3 = (dcs[ 3] + dc_pred(a0, a1, a2)).max(min_dc).min(max_dc);
523
524 a4 = (dcs[ 4] + dc_pred(prev_dc[5], prev_dc[6], a1)).max(min_dc).min(max_dc);
525 a5 = (dcs[ 5] + dc_pred(prev_dc[6], prev_dc[7], a4)).max(min_dc).min(max_dc);
526 a6 = (dcs[ 6] + dc_pred(a1, a3, a4)).max(min_dc).min(max_dc);
527 a7 = (dcs[ 7] + dc_pred(a4, a5, a6)).max(min_dc).min(max_dc);
528
529 a8 = (dcs[ 8] + dc_pred2(a2, a3)).max(min_dc).min(max_dc);
530 a9 = (dcs[ 9] + dc_pred(a2, a3, a8)).max(min_dc).min(max_dc);
531 aA = (dcs[10] + dc_pred2(a8, a9)).max(min_dc).min(max_dc);
532 aB = (dcs[11] + dc_pred(a8, a9, aA)).max(min_dc).min(max_dc);
533
534 aC = (dcs[12] + dc_pred(a3, a6, a9)).max(min_dc).min(max_dc);
535 aD = (dcs[13] + dc_pred(a6, a7, aC)).max(min_dc).min(max_dc);
536 aE = (dcs[14] + dc_pred(a9, aB, aC)).max(min_dc).min(max_dc);
537 aF = (dcs[15] + dc_pred(aC, aD, aE)).max(min_dc).min(max_dc);
538 }
539 } else {
540 if is_top {
541 a0 = (dcs[ 0] + dc_pred2(dst[5], dst[7])).max(min_dc).min(max_dc);
542 a1 = (dcs[ 1] + a0).max(min_dc).min(max_dc);
543 a2 = (dcs[ 2] + dc_pred(dst[5], dst[7], a0)).max(min_dc).min(max_dc);
544 a3 = (dcs[ 3] + dc_pred(a0, a1, a2)).max(min_dc).min(max_dc);
545
546 a4 = (dcs[ 4] + dc_pred2(a1, a3)).max(min_dc).min(max_dc);
547 a5 = (dcs[ 5] + a4).max(min_dc).min(max_dc);
548 a6 = (dcs[ 6] + dc_pred(a1, a3, a4)).max(min_dc).min(max_dc);
549 a7 = (dcs[ 7] + dc_pred(a4, a5, a6)).max(min_dc).min(max_dc);
550
551 a8 = (dcs[ 8] + dc_pred(dst[7], dst[13], a2)).max(min_dc).min(max_dc);
552 a9 = (dcs[ 9] + dc_pred(a2, a3, a8)).max(min_dc).min(max_dc);
553 aA = (dcs[10] + dc_pred(dst[13], dst[15], a8)).max(min_dc).min(max_dc);
554 aB = (dcs[11] + dc_pred(a8, a9, aA)).max(min_dc).min(max_dc);
555
556 aC = (dcs[12] + dc_pred(a3, a6, a9)).max(min_dc).min(max_dc);
557 aD = (dcs[13] + dc_pred(a6, a7, aC)).max(min_dc).min(max_dc);
558 aE = (dcs[14] + dc_pred(a9, aB, aC)).max(min_dc).min(max_dc);
559 aF = (dcs[15] + dc_pred(aC, aD, aE)).max(min_dc).min(max_dc);
560 } else {
561 a0 = (dcs[ 0] + dc_pred(prev_dc[3], prev_dc[4], dst[5])).max(min_dc).min(max_dc);
562 a1 = (dcs[ 1] + dc_pred(prev_dc[4], prev_dc[5], a0)).max(min_dc).min(max_dc);
563 a2 = (dcs[ 2] + dc_pred(dst[5], dst[7], a0)).max(min_dc).min(max_dc);
564 a3 = (dcs[ 3] + dc_pred(a0, a1, a2)).max(min_dc).min(max_dc);
565
566 a4 = (dcs[ 4] + dc_pred(prev_dc[5], prev_dc[6], a1)).max(min_dc).min(max_dc);
567 a5 = (dcs[ 5] + dc_pred(prev_dc[6], prev_dc[7], a4)).max(min_dc).min(max_dc);
568 a6 = (dcs[ 6] + dc_pred(a1, a3, a4)).max(min_dc).min(max_dc);
569 a7 = (dcs[ 7] + dc_pred(a4, a5, a6)).max(min_dc).min(max_dc);
570
571 a8 = (dcs[ 8] + dc_pred(dst[7], dst[13], a2)).max(min_dc).min(max_dc);
572 a9 = (dcs[ 9] + dc_pred(a2, a3, a8)).max(min_dc).min(max_dc);
573 aA = (dcs[10] + dc_pred(dst[13], dst[15], a8)).max(min_dc).min(max_dc);
574 aB = (dcs[11] + dc_pred(a8, a9, aA)).max(min_dc).min(max_dc);
575
576 aC = (dcs[12] + dc_pred(a3, a6, a9)).max(min_dc).min(max_dc);
577 aD = (dcs[13] + dc_pred(a6, a7, aC)).max(min_dc).min(max_dc);
578 aE = (dcs[14] + dc_pred(a9, aB, aC)).max(min_dc).min(max_dc);
579 aF = (dcs[15] + dc_pred(aC, aD, aE)).max(min_dc).min(max_dc);
580 }
581 }
582 dst[ 0] = a0;
583 dst[ 1] = a1;
584 dst[ 2] = a2;
585 dst[ 3] = a3;
586 dst[ 4] = a4;
587 dst[ 5] = a5;
588 dst[ 6] = a6;
589 dst[ 7] = a7;
590 dst[ 8] = a8;
591 dst[ 9] = a9;
592 dst[10] = aA;
593 dst[11] = aB;
594 dst[12] = aC;
595 dst[13] = aD;
596 dst[14] = aE;
597 dst[15] = aF;
598 let new_dc = &mut self.new_dcs[bx * 4 + 4..];
599 new_dc[0] = aA;
600 new_dc[1] = aB;
601 new_dc[2] = aE;
602 new_dc[3] = aF;
603 }
604 #[allow(non_snake_case)]
605 fn predict_inter(&mut self, min_dc: i32, max_dc: i32) {
606 let a0; let a1; let a2; let a3; let a4; let a5; let a6; let a7;
607 let a8; let a9; let aA; let aB; let aC; let aD; let aE; let aF;
608 let dst = &mut self.dcs;
609 let dcs = &self.dc_buf;
610 a0 = (dcs[ 0] + 0).max(min_dc).min(max_dc);
611 a1 = (dcs[ 1] + a0).max(min_dc).min(max_dc);
612 a2 = (dcs[ 2] + dc_pred2(a0, a1)).max(min_dc).min(max_dc);
613 a3 = (dcs[ 3] + dc_pred(a0, a1, a2)).max(min_dc).min(max_dc);
614
615 a4 = (dcs[ 4] + dc_pred2(a1, a3)).max(min_dc).min(max_dc);
616 a5 = (dcs[ 5] + a4).max(min_dc).min(max_dc);
617 a6 = (dcs[ 6] + dc_pred(a1, a3, a4)).max(min_dc).min(max_dc);
618 a7 = (dcs[ 7] + dc_pred(a4, a5, a6)).max(min_dc).min(max_dc);
619
620 a8 = (dcs[ 8] + dc_pred2(a2, a3)).max(min_dc).min(max_dc);
621 a9 = (dcs[ 9] + dc_pred(a2, a3, a8)).max(min_dc).min(max_dc);
622 aA = (dcs[10] + dc_pred2(a8, a9)).max(min_dc).min(max_dc);
623 aB = (dcs[11] + dc_pred(a8, a9, aA)).max(min_dc).min(max_dc);
624
625 aC = (dcs[12] + dc_pred(a3, a6, a9)).max(min_dc).min(max_dc);
626 aD = (dcs[13] + dc_pred(a6, a7, aC)).max(min_dc).min(max_dc);
627 aE = (dcs[14] + dc_pred(a9, aB, aC)).max(min_dc).min(max_dc);
628 aF = (dcs[15] + dc_pred(aC, aD, aE)).max(min_dc).min(max_dc);
629
630 dst[ 0] = a0;
631 dst[ 1] = a1;
632 dst[ 2] = a2;
633 dst[ 3] = a3;
634 dst[ 4] = a4;
635 dst[ 5] = a5;
636 dst[ 6] = a6;
637 dst[ 7] = a7;
638 dst[ 8] = a8;
639 dst[ 9] = a9;
640 dst[10] = aA;
641 dst[11] = aB;
642 dst[12] = aC;
643 dst[13] = aD;
644 dst[14] = aE;
645 dst[15] = aF;
646 }
647 }
648
649 impl DCInfo for CDCInfo {
650 fn decode_dcs(&mut self, br: &mut BitReader, q: u8) -> DecoderResult<bool> {
651 decode_dcs(br, &mut self.dc_buf, q)
652 }
653 fn predict(&mut self, bx: usize, is_top: bool, is_left: bool, min_dc: i32, max_dc: i32) {
654 let prev_dc = &self.prev_dcs[bx * 2..];
655 let dst = &mut self.dcs;
656 let dcs = &self.dc_buf;
657
658 let a0; let a1; let a2; let a3;
659 if is_left {
660 if is_top {
661 let add = 1 << 10;//if has_prev_dc { 1 << 10 } else { 0 };
662 a0 = (dcs[ 0] + add).max(min_dc).min(max_dc);
663 a1 = (dcs[ 1] + a0).max(min_dc).min(max_dc);
664 } else {
665 a0 = (dcs[ 0] + dc_pred2(prev_dc[2], prev_dc[3])).max(min_dc).min(max_dc);
666 a1 = (dcs[ 1] + dc_pred(prev_dc[2], prev_dc[3], a0)).max(min_dc).min(max_dc);
667 }
668 a2 = (dcs[ 2] + dc_pred2(a0, a1)).max(min_dc).min(max_dc);
669 a3 = (dcs[ 3] + dc_pred(a0, a1, a2)).max(min_dc).min(max_dc);
670 } else {
671 if is_top {
672 a0 = (dcs[ 0] + dc_pred2(dst[1], dst[3])).max(min_dc).min(max_dc);
673 a1 = (dcs[ 1] + a0).max(min_dc).min(max_dc);
674 } else {
675 a0 = (dcs[ 0] + dc_pred(prev_dc[1], prev_dc[2], dst[1])).max(min_dc).min(max_dc);
676 a1 = (dcs[ 1] + dc_pred(prev_dc[2], prev_dc[3], a0)).max(min_dc).min(max_dc);
677 }
678 a2 = (dcs[ 2] + dc_pred(dst[1], dst[3], a0)).max(min_dc).min(max_dc);
679 a3 = (dcs[ 3] + dc_pred(a0, a1, a2)).max(min_dc).min(max_dc);
680 }
681 dst[0] = a0;
682 dst[1] = a1;
683 dst[2] = a2;
684 dst[3] = a3;
685 let new_dc = &mut self.new_dcs[bx * 2 + 2..];
686 new_dc[0] = a2;
687 new_dc[1] = a3;
688 }
689 fn predict_inter(&mut self, min_dc: i32, max_dc: i32) {
690 let dst = &mut self.dcs;
691 let dcs = &self.dc_buf;
692
693 let a0; let a1; let a2; let a3;
694
695 a0 = (dcs[ 0] + 0).max(min_dc).min(max_dc);
696 a1 = (dcs[ 1] + a0).max(min_dc).min(max_dc);
697 a2 = (dcs[ 2] + dc_pred2(a0, a1)).max(min_dc).min(max_dc);
698 a3 = (dcs[ 3] + dc_pred(a0, a1, a2)).max(min_dc).min(max_dc);
699
700 dst[0] = a0;
701 dst[1] = a1;
702 dst[2] = a2;
703 dst[3] = a3;
704 }
705 }
706
707 fn decode_dcs(br: &mut BitReader, dcs: &mut [i32], q: u8) -> DecoderResult<bool> {
708 if !br.read_bool()? {
709 for dc in dcs.iter_mut() {
710 *dc = 0;
711 }
712 Ok(false)
713 } else {
714 let quant = BINK2_QUANT_DC[q.max(8) as usize];
715 for dc in dcs.iter_mut() {
716 *dc = br.read_bink2_code_zero()?;
717 if *dc != 0 {
718 *dc = (*dc * quant + 0x1FF) >> 10;
719 if br.read_bool()? {
720 *dc = -*dc;
721 }
722 }
723 }
724 Ok(true)
725 }
726 }
727 #[derive(Default)]
728 struct MVInfo {
729 mvs: Vec<MV>,
730 stride: usize,
731 mv_off: usize,
732 prev_off: usize,
733 }
734
735 impl MVInfo {
736 fn resize(&mut self, bw: usize) {
737 self.stride = bw * 4;
738 self.mv_off = 0;
739 self.prev_off = self.stride;
740 self.mvs.resize(self.stride * 2, ZERO_MV);
741 }
742 fn update_line(&mut self) {
743 mem::swap(&mut self.mv_off, &mut self.prev_off);
744 }
745 fn decode_mv_new(&mut self, br: &mut BitReader, mv_cb: &Codebook<i8>, bx: usize, is_top: bool, is_left: bool) -> DecoderResult<()> {
746 let num_mv = if br.read_bool()? { 1 } else { 4 };
747 let mv = &mut self.mvs;
748 let mv_pos = self.mv_off + bx * 4;
749 let ref_pos = self.prev_off + bx * 4;
750
751 for comp in 0..2 {
752 for i in 0..num_mv {
753 let val;
754 let raw_val = br.read_cb(mv_cb)?;
755 if raw_val != BINK2_MV_ESC {
756 val = raw_val as i16;
757 } else {
758 let len = br.read_code(UintCodeType::LimitedUnary(12, 1))? as u8;
759 let nlen = len + 4;
760 let uval = (br.read(nlen)? as i32) + (1 << nlen) - 1;
761 if (uval & 1) != 0 {
762 val = (-(uval >> 1) - 1) as i16;
763 } else {
764 val = (uval >> 1) as i16;
765 }
766 }
767 if comp == 0 {
768 mv[mv_pos + i].x = val;
769 } else {
770 mv[mv_pos + i].y = val;
771 }
772 }
773 }
774
775 if num_mv > 1 {
776 if is_top {
777 if !is_left {
778 let mv0 = mv[mv_pos + 0] + MV::pred(mv[mv_pos - 4], mv[mv_pos - 3], mv[mv_pos - 1]);
779 mv[mv_pos + 0] = mv0;
780 let mv2 = mv[mv_pos + 2] + MV::pred(mv[mv_pos - 3], mv[mv_pos - 1], mv[mv_pos + 0]);
781 mv[mv_pos + 2] = mv2;
782 let mv1 = mv[mv_pos + 1] + MV::pred(mv[mv_pos - 3], mv[mv_pos + 0], mv[mv_pos + 2]);
783 mv[mv_pos + 1] = mv1;
784 let mv3 = mv[mv_pos + 3] + MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
785 mv[mv_pos + 3] = mv3;
786 } else {
787 let mv1 = mv[mv_pos + 1] + mv[mv_pos + 0];
788 mv[mv_pos + 1] = mv1;
789 let mv2 = mv[mv_pos + 2] + mv[mv_pos + 0];
790 mv[mv_pos + 2] = mv2;
791 let mv3 = mv[mv_pos + 3] + MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
792 mv[mv_pos + 3] = mv3;
793 }
794 } else {
795 if is_left {
796 let mv0 = mv[mv_pos + 0] + MV::pred(mv[ref_pos + 0], mv[ref_pos + 2], mv[ref_pos + 3]);
797 mv[mv_pos + 0] = mv0;
798 let mv1 = mv[mv_pos + 1] + MV::pred(mv[ref_pos + 2], mv[ref_pos + 3], mv[mv_pos + 0]);
799 mv[mv_pos + 1] = mv1;
800 let mv2 = mv[mv_pos + 2] + MV::pred(mv[ref_pos + 2], mv[mv_pos + 0], mv[mv_pos + 1]);
801 mv[mv_pos + 2] = mv2;
802 let mv3 = mv[mv_pos + 3] + MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
803 mv[mv_pos + 3] = mv3;
804 } else {
805 let mv0 = mv[mv_pos + 0] + MV::pred(mv[ref_pos + 2], mv[ref_pos - 1], mv[mv_pos - 3]);
806 mv[mv_pos + 0] = mv0;
807 let mv1 = mv[mv_pos + 1] + MV::pred(mv[ref_pos + 2], mv[ref_pos + 3], mv[mv_pos + 0]);
808 mv[mv_pos + 1] = mv1;
809 let mv2 = mv[mv_pos + 2] + MV::pred(mv[mv_pos - 3], mv[mv_pos - 1], mv[mv_pos + 0]);
810 mv[mv_pos + 2] = mv2;
811 let mv3 = mv[mv_pos + 3] + MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
812 mv[mv_pos + 3] = mv3;
813 }
814 }
815 } else {
816 let mut mv_pred = mv[mv_pos + 0];
817 if is_top {
818 if !is_left {
819 mv_pred += MV::pred(mv[mv_pos - 4], mv[mv_pos - 3], mv[mv_pos - 1]);
820 }
821 } else {
822 if !is_left {
823 mv_pred += MV::pred(mv[ref_pos - 1], mv[ref_pos + 2], mv[mv_pos - 3]);
824 } else {
825 mv_pred += MV::pred(mv[ref_pos + 0], mv[ref_pos + 2], mv[ref_pos + 3]);
826 }
827 }
828 mv[mv_pos + 0] = mv_pred;
829 mv[mv_pos + 1] = mv_pred;
830 mv[mv_pos + 2] = mv_pred;
831 mv[mv_pos + 3] = mv_pred;
832 }
833 Ok(())
834 }
835 fn decode_mv_old(&mut self, br: &mut BitReader, bx: usize, is_top: bool, is_left: bool) -> DecoderResult<()> {
836 let mv = &mut self.mvs;
837 let mv_pos = self.mv_off + bx * 4;
838 let ref_pos = self.prev_off + bx * 4;
839
840 let mut basex = 0;
841 let mut basey = 0;
842 for comp in 0..2 {
843 let mut bits = br.read(3)? as u8;
844 if bits == 7 {
845 bits += br.read(2)? as u8;
846 }
847 let mut mv_c: [i16; 4] = [0; 4];
848 if bits > 0 {
849 for i in 0..4 {
850 mv_c[i] = br.read(bits)? as i16;
851 }
852 for i in 0..4 {
853 if (mv_c[i] != 0) && br.read_bool()? {
854 mv_c[i] = -mv_c[i];
855 }
856 }
857 }
858 if is_top && is_left {
859 let mut val = br.read(5)? as i16;
860 if (val != 0) && br.read_bool()? {
861 val = -val;
862 }
863 if comp == 0 {
864 basex = val;
865 } else {
866 basey = val;
867 }
868 }
869 if comp == 0 {
870 for i in 0..4 {
871 mv[mv_pos + i].x = mv_c[i];
872 }
873 } else {
874 for i in 0..4 {
875 mv[mv_pos + i].y = mv_c[i];
876 }
877 }
878 }
879 if !is_top {
880 if is_left {
881 let mv0 = mv[mv_pos + 0] + MV::pred(mv[ref_pos + 0], mv[ref_pos + 2], mv[ref_pos + 3]);
882 mv[mv_pos + 0] = mv0;
883 let mv1 = mv[mv_pos + 1] + MV::pred(mv[ref_pos + 2], mv[ref_pos + 3], mv[mv_pos + 0]);
884 mv[mv_pos + 1] = mv1;
885 let mv2 = mv[mv_pos + 2] + MV::pred(mv[ref_pos + 2], mv[mv_pos + 0], mv[mv_pos + 1]);
886 mv[mv_pos + 2] = mv2;
887 let mv3 = mv[mv_pos + 3] + MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
888 mv[mv_pos + 3] = mv3;
889 } else {
890 let mv0 = mv[mv_pos + 0] + MV::pred(mv[ref_pos - 1], mv[ref_pos + 2], mv[mv_pos - 3]);
891 mv[mv_pos + 0] = mv0;
892 let mv1 = mv[mv_pos + 1] + MV::pred(mv[ref_pos + 2], mv[ref_pos + 3], mv[mv_pos + 0]);
893 mv[mv_pos + 1] = mv1;
894 let mv2 = mv[mv_pos + 2] + MV::pred(mv[ref_pos + 2], mv[mv_pos + 0], mv[mv_pos + 1]);
895 mv[mv_pos + 2] = mv2;
896 let mv3 = mv[mv_pos + 3] + MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
897 mv[mv_pos + 3] = mv3;
898 }
899 } else {
900 if is_left {
901 let base_mv = MV { x: basex * 16, y: basey * 16 };
902 mv[mv_pos + 0] += base_mv;
903 mv[mv_pos + 1] += base_mv;
904 mv[mv_pos + 2] += base_mv;
905 mv[mv_pos + 3] += base_mv;
906 } else {
907 let mv0 = mv[mv_pos + 0] + MV::pred(mv[mv_pos - 4], mv[mv_pos - 3], mv[mv_pos - 1]);
908 mv[mv_pos + 0] = mv0;
909 let mv2 = mv[mv_pos + 2] + MV::pred(mv[mv_pos - 3], mv[mv_pos - 1], mv[mv_pos + 0]);
910 mv[mv_pos + 2] = mv2;
911 let mv1 = mv[mv_pos + 1] + MV::pred(mv[mv_pos - 3], mv[mv_pos + 0], mv[mv_pos + 2]);
912 mv[mv_pos + 1] = mv1;
913 let mv3 = mv[mv_pos + 3] + MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
914 mv[mv_pos + 3] = mv3;
915 }
916 }
917 Ok(())
918 }
919
920 fn predict_mvs(&mut self, bx: usize, is_top: bool, is_left: bool, is_new: bool) {
921 let mv = &mut self.mvs;
922 let mv_pos = self.mv_off + bx * 4;
923 let ref_pos = self.prev_off + bx * 4;
924
925 if is_top {
926 if is_left {
927 for i in 0..4 {
928 mv[mv_pos + i] = ZERO_MV;
929 }
930 } else {
931 mv[mv_pos + 0] = MV::pred(mv[mv_pos - 4], mv[mv_pos - 3], mv[mv_pos - 1]);
932 mv[mv_pos + 2] = MV::pred(mv[mv_pos - 3], mv[mv_pos - 1], mv[mv_pos + 0]);
933 mv[mv_pos + 1] = MV::pred(mv[mv_pos - 1], mv[mv_pos + 0], mv[mv_pos + 2]);
934 mv[mv_pos + 3] = MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
935 }
936 } else {
937 if is_left {
938 mv[mv_pos + 0] = MV::pred(mv[ref_pos + 0], mv[ref_pos + 2], mv[ref_pos + 3]);
939 mv[mv_pos + 1] = MV::pred(mv[mv_pos + 0], mv[ref_pos + 2], mv[ref_pos + 3]);
940 mv[mv_pos + 2] = MV::pred(mv[ref_pos + 2], mv[mv_pos + 1], mv[mv_pos + 0]);
941 mv[mv_pos + 3] = MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
942 } else {
943 mv[mv_pos + 0] = MV::pred(mv[mv_pos - 3], mv[ref_pos - 1], mv[ref_pos + 2]);
944 mv[mv_pos + 1] = MV::pred(mv[mv_pos + 0], mv[ref_pos + 2], mv[ref_pos + 3]);
945 if is_new {
946 mv[mv_pos + 2] = MV::pred(mv[mv_pos - 3], mv[mv_pos - 1], mv[mv_pos + 0]);
947 } else {
948 mv[mv_pos + 2] = MV::pred(mv[ref_pos + 2], mv[mv_pos + 1], mv[mv_pos + 0]);
949 }
950 mv[mv_pos + 3] = MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
951 }
952 }
953 }
954
955 fn get_mv(&self, bx: usize, blk: usize) -> MV { self.mvs[self.mv_off + bx * 4 + blk] }
956 fn zero(&mut self, bx: usize) {
957 let pos = self.mv_off + bx * 4;
958 self.mvs[pos + 0] = ZERO_MV;
959 self.mvs[pos + 1] = ZERO_MV;
960 self.mvs[pos + 2] = ZERO_MV;
961 self.mvs[pos + 3] = ZERO_MV;
962 }
963 }
964
965 struct Bink2Codes {
966 ac_cb1: Codebook<u8>,
967 ac_cb2: Codebook<u8>,
968 mv_cb: Codebook<i8>,
969 val_cb1: Codebook<u8>,
970 val_cb2: Codebook<u8>,
971 skip_cb1: Codebook<u8>,
972 skip_cb2: Codebook<u8>,
973 quant_cb: Codebook<u8>,
974 }
975
976 fn map_ac(idx: usize) -> u8 { idx as u8 }
977 fn map_mv(idx: usize) -> i8 { BINK2_MV_SYMS[idx] }
978
979 impl Default for Bink2Codes {
980 fn default() -> Self {
981 let mut cbr = TableCodebookDescReader::new(BINK2_AC_CODES1, BINK2_AC_BITS1, map_ac);
982 let ac_cb1 = Codebook::new(&mut cbr, CodebookMode::LSB).unwrap();
983 let mut cbr = TableCodebookDescReader::new(BINK2_AC_CODES2, BINK2_AC_BITS2, map_ac);
984 let ac_cb2 = Codebook::new(&mut cbr, CodebookMode::LSB).unwrap();
985 let mut cbr = TableCodebookDescReader::new(BINK2_MV_CODES, BINK2_MV_BITS, map_mv);
986 let mv_cb = Codebook::new(&mut cbr, CodebookMode::LSB).unwrap();
987 let mut cbr = TableCodebookDescReader::new(BINK2_AC_VAL_CODES1, BINK2_AC_VAL_BITS1, map_ac);
988 let val_cb1 = Codebook::new(&mut cbr, CodebookMode::LSB).unwrap();
989 let mut cbr = TableCodebookDescReader::new(BINK2_AC_VAL_CODES2, BINK2_AC_VAL_BITS2, map_ac);
990 let val_cb2 = Codebook::new(&mut cbr, CodebookMode::LSB).unwrap();
991 let mut cbr = TableCodebookDescReader::new(BINK2_AC_SKIP_CODES1, BINK2_AC_SKIP_BITS1, map_ac);
992 let skip_cb1 = Codebook::new(&mut cbr, CodebookMode::LSB).unwrap();
993 let mut cbr = TableCodebookDescReader::new(BINK2_AC_SKIP_CODES2, BINK2_AC_SKIP_BITS2, map_ac);
994 let skip_cb2 = Codebook::new(&mut cbr, CodebookMode::LSB).unwrap();
995 let mut cbr = TableCodebookDescReader::new(BINK2_QUANT_CODES, BINK2_QUANT_BITS, map_ac);
996 let quant_cb = Codebook::new(&mut cbr, CodebookMode::LSB).unwrap();
997 Self { ac_cb1, ac_cb2, mv_cb, val_cb1, val_cb2, skip_cb1, skip_cb2, quant_cb }
998 }
999 }
1000
1001 #[derive(Default)]
1002 struct Bink2Decoder {
1003 info: NACodecInfoRef,
1004 ips: IPShuffler,
1005
1006 version: u32,
1007 has_alpha: bool,
1008 slice_h: [usize; 8],
1009 num_slices: usize,
1010
1011 key_frame: bool,
1012 cur_w: usize,
1013 cur_h: usize,
1014 qinfo: QuantInfo,
1015 y_dcs: YDCInfo,
1016 u_dcs: CDCInfo,
1017 v_dcs: CDCInfo,
1018 a_dcs: YDCInfo,
1019 mvs: MVInfo,
1020
1021 codes: Bink2Codes,
1022 }
1023
1024 #[allow(clippy::erasing_op)]
1025 impl Bink2Decoder {
1026 fn new() -> Self {
1027 Self::default()
1028 }
1029
1030 fn decode_frame_new(&mut self, br: &mut BitReader, buf: &mut NAVideoBuffer<u8>, is_intra: bool) -> DecoderResult<()> {
1031 let (stride_y, stride_u, stride_v, stride_a) = (buf.get_stride(0), buf.get_stride(1), buf.get_stride(2), buf.get_stride(3));
1032 let (mut off_y, mut off_u, mut off_v, mut off_a) = (buf.get_offset(0), buf.get_offset(1), buf.get_offset(2), buf.get_offset(3));
1033 let (ooff_y, ooff_u, ooff_v, ooff_a) = (off_y, off_u, off_v, off_a);
1034 let (width, height) = buf.get_dimensions(0);
1035 let data = buf.get_data_mut().unwrap();
1036 let dst = data.as_mut_slice();
1037 let bw = (width + 31) >> 5;
1038 let bheight = (height + 31) >> 5;
1039 self.cur_w = (width + 7) & !7;
1040 self.cur_h = ((height + 7) & !7) >> 1;
1041
1042 let frame_flags = br.read(32)?;
1043 let mut offsets: [u32; 7] = [0; 7];
1044 for i in 0..self.num_slices-1 {
1045 offsets[i] = br.read(32)?;
1046 }
1047 let mut do_alpha = self.has_alpha;
1048 if (frame_flags & 0x80000) != 0 && self.has_alpha {
1049 do_alpha = false;
1050 let fillval = (frame_flags >> 24) as u8;
1051 let aplane = &mut dst[off_a..][..stride_a * bheight * 32];
1052 for el in aplane.iter_mut() {
1053 *el = fillval;
1054 }
1055 }
1056 let mut row_flags: Vec<bool> = Vec::with_capacity(bheight * 4);
1057 let mut col_flags: Vec<bool> = Vec::with_capacity(bw * 4);
1058 if (frame_flags & 0x10000) != 0 {
1059 if (frame_flags & 0x8000) == 0 {
1060 let len = (height + 15) >> 4;
1061 decode_flags(br, &mut row_flags, 1, len * 2 - 1)?;
1062 }
1063 if (frame_flags & 0x4000) == 0 {
1064 let len = (width + 15) >> 4;
1065 decode_flags(br, &mut col_flags, 1, len * 2 - 1)?;
1066 }
1067 }
1068 row_flags.resize(bheight * 4, false);
1069 col_flags.resize(bw * 4, false);
1070 //store frame_flags * 8 & 0x7F8
1071
1072 let mut start_by = 0;
1073 for slice_no in 0..self.num_slices {
1074 let end_by = self.slice_h[slice_no];
1075 if slice_no != 0 {
1076 br.seek(offsets[slice_no - 1] * 8)?;
1077 }
1078 off_y = ooff_y + stride_y * start_by * 32;
1079 off_u = ooff_u + stride_u * start_by * 16;
1080 off_v = ooff_v + stride_v * start_by * 16;
1081 off_a = ooff_a + stride_a * start_by * 32;
1082
1083 let mut row_state = frame_flags & 0x2E000;
1084 if is_intra {
1085 row_state |= 0x10000;
1086 }
1087 self.qinfo.resize(bw);
1088 self.y_dcs.resize(bw);
1089 self.u_dcs.resize(bw);
1090 self.v_dcs.resize(bw);
1091 self.a_dcs.resize(bw);
1092 self.mvs.resize(bw);
1093 for by in start_by..end_by {
1094 let mut cbp_y = 0;
1095 let mut cbp_u = 0;
1096 let mut cbp_v = 0;
1097 let mut cbp_a = 0;
1098 let mut cbp_y_p = 0;
1099 let mut cbp_u_p = 0;
1100 let mut cbp_v_p = 0;
1101 let mut cbp_a_p = 0;
1102 let mut q_y = 8;
1103 let mut q_u = 8;
1104 let mut q_v = 8;
1105 let mut q_a = 8;
1106 let mut q_y_p = 8;
1107 let mut q_u_p = 8;
1108 let mut q_v_p = 8;
1109 let mut q_a_p = 8;
1110 let rflags = (row_flags[by] as u32) * 4;
1111 row_state = (row_state & 0x3FFFFFF) | ((row_state >> 4) & 0xC000000) | (rflags << 28);
1112 if by == start_by {
1113 row_state |= 0x80;
1114 // } else {
1115 // row_state |= 0x8;
1116 }
1117 if by + 2 >= end_by {
1118 row_state |= 0x100;
1119 }
1120
1121 let mut btype_lru: [u8; 4] = [ 2, 3, 1, 0 ];
1122 let mut edge_state = 0;
1123 let is_top = by == start_by;
1124 for bx in 0..bw {
1125 let mut blk_state = row_state | (edge_state & 0x3FC0000);
1126 if bx == 0 {
1127 blk_state |= 0x20;
1128 }
1129 if bx + 2 < bw {
1130 blk_state |= 0x40;
1131 }
1132 if (bx & 1) != 0 {
1133 blk_state |= 0x200;
1134 }
1135 let clflags = (col_flags[bx] as u32) * 4;
1136 let edge_state_c = ((blk_state >> 4) & 0x3C0000) | (blk_state & 0xFC03FFFF) | ((clflags & 0xF) << 22);
1137 let edge_state_y = (frame_flags & 0x40000) | (blk_state & 0x3FFFF);
1138 edge_state = edge_state_c;
1139
1140 //let is_top = (edge_state & 0x88) != 0;
1141 let is_left = bx == 0; //(edge_state & 0x20) != 0;
1142
1143 let btype;
1144 if is_intra {
1145 btype = 0;
1146 } else {
1147 if (blk_state & 0x2000) != 0 {
1148 let val = br.read_code(UintCodeType::LimitedUnary(3, 1))?;
1149 match val {
1150 0 => {
1151 btype = btype_lru[0];
1152 },
1153 1 => {
1154 btype = btype_lru[1];
1155 btype_lru[1] = btype_lru[0];
1156 btype_lru[0] = btype;
1157 },
1158 2 => {
1159 btype = btype_lru[3];
1160 btype_lru[3] = btype_lru[2];
1161 btype_lru[2] = btype;
1162 },
1163 3 => {
1164 btype = btype_lru[2];
1165 btype_lru[2] = btype_lru[1];
1166 btype_lru[1] = btype;
1167 },
1168 _ => unreachable!(),
1169 };
1170 } else {
1171 btype = br.read(2)? as u8;
1172 }
1173 }
1174 if !is_intra {
1175 let q = self.qinfo.pred_quant(bx * 2, is_top);
1176 self.qinfo.set_quant(bx * 2, q);
1177 let q = self.qinfo.pred_quant(bx * 2 + 1, is_top);
1178 self.qinfo.set_quant(bx * 2 + 1, q);
1179 }
1180 match btype {
1181 0 => { // intra
1182 if (frame_flags & 0x2000) != 0 {
1183 let mut yblk: [[[i32; 64]; 4]; 4] = [[[0; 64]; 4]; 4];
1184 let mut ublk: [[i32; 64]; 4] = [[0; 64]; 4];
1185 let mut vblk: [[i32; 64]; 4] = [[0; 64]; 4];
1186 let q = get_new_quant(br, self.qinfo.pred_quant(bx * 2, is_top))?;
1187 self.qinfo.set_quant(bx * 2, q);
1188 cbp_y = decode_luma_intra(br, &self.codes, cbp_y, q, &mut yblk, edge_state_y, &mut self.y_dcs, bx)?;
1189 cbp_v = decode_chroma_intra(br, &self.codes, cbp_v, q, &mut vblk, edge_state_c, &mut self.v_dcs, bx)?;
1190 cbp_u = decode_chroma_intra(br, &self.codes, cbp_u, q, &mut ublk, edge_state_c, &mut self.u_dcs, bx)?;
1191 if do_alpha {
1192 let mut ablk: [[[i32; 64]; 4]; 4] = [[[0; 64]; 4]; 4];
1193 cbp_a = decode_luma_intra(br, &self.codes, cbp_a, q, &mut ablk, edge_state_y, &mut self.a_dcs, bx)?;
1194 Bink2DSP::put_mb4(dst, off_a + bx * 32 + 0 + 0 * stride_a, stride_a, &mut ablk[0]);
1195 Bink2DSP::put_mb4(dst, off_a + bx * 32 + 16 + 0 * stride_a, stride_a, &mut ablk[1]);
1196 Bink2DSP::put_mb4(dst, off_a + bx * 32 + 0 + 16 * stride_a, stride_a, &mut ablk[2]);
1197 Bink2DSP::put_mb4(dst, off_a + bx * 32 + 16 + 16 * stride_a, stride_a, &mut ablk[3]);
1198 }
1199 //if smth else decode one more y
1200 Bink2DSP::put_mb4(dst, off_y + bx * 32 + 0 + 0 * stride_y, stride_y, &mut yblk[0]);
1201 Bink2DSP::put_mb4(dst, off_y + bx * 32 + 16 + 0 * stride_y, stride_y, &mut yblk[1]);
1202 Bink2DSP::put_mb4(dst, off_y + bx * 32 + 0 + 16 * stride_y, stride_y, &mut yblk[2]);
1203 Bink2DSP::put_mb4(dst, off_y + bx * 32 + 16 + 16 * stride_y, stride_y, &mut yblk[3]);
1204 Bink2DSP::put_mb4(dst, off_u + bx * 16, stride_u, &mut ublk);
1205 Bink2DSP::put_mb4(dst, off_v + bx * 16, stride_v, &mut vblk);
1206 } else {
1207 let mut yblk: [[[f32; 64]; 4]; 4] = [[[0.0; 64]; 4]; 4];
1208 let mut ublk: [[f32; 64]; 4] = [[0.0; 64]; 4];
1209 let mut vblk: [[f32; 64]; 4] = [[0.0; 64]; 4];
1210 cbp_y = decode_luma_intra_old(br, &self.codes, cbp_y, &mut yblk, edge_state_y, &mut self.y_dcs, bx, &mut q_y)?;
1211 cbp_v = decode_chroma_intra_old(br, &self.codes, cbp_v, &mut vblk, edge_state_c, &mut self.v_dcs, bx, &mut q_v)?;
1212 cbp_u = decode_chroma_intra_old(br, &self.codes, cbp_u, &mut ublk, edge_state_c, &mut self.u_dcs, bx, &mut q_u)?;
1213 if do_alpha {
1214 let mut ablk: [[[f32; 64]; 4]; 4] = [[[0.0; 64]; 4]; 4];
1215 cbp_a = decode_luma_intra_old(br, &self.codes, cbp_a, &mut ablk, edge_state_y, &mut self.a_dcs, bx, &mut q_a)?;
1216 Bink2DSP::put_mb4_old(dst, off_a + bx * 32 + 0 + 0 * stride_a, stride_a, &mut ablk[0]);
1217 Bink2DSP::put_mb4_old(dst, off_a + bx * 32 + 16 + 0 * stride_a, stride_a, &mut ablk[1]);
1218 Bink2DSP::put_mb4_old(dst, off_a + bx * 32 + 0 + 16 * stride_a, stride_a, &mut ablk[2]);
1219 Bink2DSP::put_mb4_old(dst, off_a + bx * 32 + 16 + 16 * stride_a, stride_a, &mut ablk[3]);
1220 }
1221 Bink2DSP::put_mb4_old(dst, off_y + bx * 32 + 0 + 0 * stride_y, stride_y, &mut yblk[0]);
1222 Bink2DSP::put_mb4_old(dst, off_y + bx * 32 + 16 + 0 * stride_y, stride_y, &mut yblk[1]);
1223 Bink2DSP::put_mb4_old(dst, off_y + bx * 32 + 0 + 16 * stride_y, stride_y, &mut yblk[2]);
1224 Bink2DSP::put_mb4_old(dst, off_y + bx * 32 + 16 + 16 * stride_y, stride_y, &mut yblk[3]);
1225 Bink2DSP::put_mb4_old(dst, off_u + bx * 16, stride_u, &mut ublk);
1226 Bink2DSP::put_mb4_old(dst, off_v + bx * 16, stride_v, &mut vblk);
1227 }
1228 if !is_intra {
1229 self.mvs.predict_mvs(bx, is_top, is_left, (frame_flags & 0x2000) != 0);
1230 }
1231 },
1232 1 => { // skip
1233 if let Some(ref ref_pic) = self.ips.get_ref() {
1234 for blk_no in 0..4 {
1235 let xoff = bx * 32 + (blk_no & 1) * 16;
1236 let yoff = by * 32 + (blk_no & 2) * 8;
1237 Bink2DSP::mc_luma(&mut dst[off_y..], stride_y, ref_pic, xoff, yoff, ZERO_MV, 0)?;
1238 Bink2DSP::mc_chroma(&mut dst[off_u..], stride_u, ref_pic, xoff >> 1, yoff >> 1, ZERO_MV, 1)?;
1239 Bink2DSP::mc_chroma(&mut dst[off_v..], stride_v, ref_pic, xoff >> 1, yoff >> 1, ZERO_MV, 2)?;
1240 if do_alpha {
1241 Bink2DSP::mc_luma(&mut dst[off_a..], stride_a, ref_pic, xoff, yoff, ZERO_MV, 3)?;
1242 }
1243 }
1244 } else {
1245 return Err(DecoderError::MissingReference);
1246 }
1247 self.mvs.zero(bx);
1248 },
1249 2 => { // motion
1250 if (frame_flags & 0x2000) != 0 {
1251 self.mvs.decode_mv_new(br, &self.codes.mv_cb, bx, is_top, is_left)?;
1252 } else {
1253 self.mvs.decode_mv_old(br, bx, is_top, is_left)?;
1254 }
1255 if let Some(ref ref_pic) = self.ips.get_ref() {
1256 for blk_no in 0..4 {
1257 let xoff = bx * 32 + (blk_no & 1) * 16;
1258 let yoff = by * 32 + (blk_no & 2) * 8;
1259 let mv = self.mvs.get_mv(bx, blk_no);
1260 Bink2DSP::mc_luma(&mut dst[off_y..], stride_y, ref_pic, xoff, yoff, mv, 0)?;
1261 Bink2DSP::mc_chroma(&mut dst[off_u..], stride_u, ref_pic, xoff >> 1, yoff >> 1, mv, 1)?;
1262 Bink2DSP::mc_chroma(&mut dst[off_v..], stride_v, ref_pic, xoff >> 1, yoff >> 1, mv, 2)?;
1263 if do_alpha {
1264 Bink2DSP::mc_luma(&mut dst[off_a..], stride_a, ref_pic, xoff, yoff, mv, 3)?;
1265 }
1266 }
1267 } else {
1268 return Err(DecoderError::MissingReference);
1269 }
1270 },
1271 3 => { // inter
1272 if (frame_flags & 0x2000) != 0 {
1273 self.mvs.decode_mv_new(br, &self.codes.mv_cb, bx, is_top, is_left)?;
1274 } else {
1275 self.mvs.decode_mv_old(br, bx, is_top, is_left)?;
1276 }
1277 if let Some(ref ref_pic) = self.ips.get_ref() {
1278 for blk_no in 0..4 {
1279 let xoff = bx * 32 + (blk_no & 1) * 16;
1280 let yoff = by * 32 + (blk_no & 2) * 8;
1281 let mv = self.mvs.get_mv(bx, blk_no);
1282 Bink2DSP::mc_luma(&mut dst[off_y..], stride_y, ref_pic, xoff, yoff, mv, 0)?;
1283 Bink2DSP::mc_chroma(&mut dst[off_u..], stride_u, ref_pic, xoff >> 1, yoff >> 1, mv, 1)?;
1284 Bink2DSP::mc_chroma(&mut dst[off_v..], stride_v, ref_pic, xoff >> 1, yoff >> 1, mv, 2)?;
1285 if do_alpha {
1286 Bink2DSP::mc_luma(&mut dst[off_a..], stride_a, ref_pic, xoff, yoff, mv, 3)?;
1287 }
1288 }
1289 } else {
1290 return Err(DecoderError::MissingReference);
1291 }
1292 if (frame_flags & 0x2000) != 0 {
1293 let mut yblk: [[[i32; 64]; 4]; 4] = [[[0; 64]; 4]; 4];
1294 let mut ublk: [[i32; 64]; 4] = [[0; 64]; 4];
1295 let mut vblk: [[i32; 64]; 4] = [[0; 64]; 4];
1296 let q = get_new_quant(br, self.qinfo.pred_quant(bx * 2 + 1, is_top))?;
1297 self.qinfo.set_quant(bx * 2 + 1, q);
1298 cbp_y_p = decode_luma_inter(br, &self.codes, cbp_y_p, q, &mut yblk, edge_state_y, &mut self.y_dcs)?;
1299 if br.read_bool()? {
1300 cbp_v_p = decode_chroma_inter(br, &self.codes, cbp_v_p, q, &mut vblk, edge_state_y, &mut self.v_dcs)?;
1301 cbp_u_p = decode_chroma_inter(br, &self.codes, cbp_u_p, q, &mut ublk, edge_state_y, &mut self.u_dcs)?;
1302 } else {
1303 cbp_v_p = 0;
1304 cbp_u_p = 0;
1305 }
1306 if do_alpha {
1307 let mut ablk: [[[i32; 64]; 4]; 4] = [[[0; 64]; 4]; 4];
1308 cbp_a_p = decode_luma_inter(br, &self.codes, cbp_a_p, q, &mut ablk, edge_state_y, &mut self.a_dcs)?;
1309 Bink2DSP::add_mb4(dst, off_a + bx * 32 + 0 + 0 * stride_a, stride_a, &mut ablk[0]);
1310 Bink2DSP::add_mb4(dst, off_a + bx * 32 + 16 + 0 * stride_a, stride_a, &mut ablk[1]);
1311 Bink2DSP::add_mb4(dst, off_a + bx * 32 + 0 + 16 * stride_a, stride_a, &mut ablk[2]);
1312 Bink2DSP::add_mb4(dst, off_a + bx * 32 + 16 + 16 * stride_a, stride_a, &mut ablk[3]);
1313 }
1314 Bink2DSP::add_mb4(dst, off_y + bx * 32 + 0 + 0 * stride_y, stride_y, &mut yblk[0]);
1315 Bink2DSP::add_mb4(dst, off_y + bx * 32 + 16 + 0 * stride_y, stride_y, &mut yblk[1]);
1316 Bink2DSP::add_mb4(dst, off_y + bx * 32 + 0 + 16 * stride_y, stride_y, &mut yblk[2]);
1317 Bink2DSP::add_mb4(dst, off_y + bx * 32 + 16 + 16 * stride_y, stride_y, &mut yblk[3]);
1318 Bink2DSP::add_mb4(dst, off_u + bx * 16, stride_u, &mut ublk);
1319 Bink2DSP::add_mb4(dst, off_v + bx * 16, stride_v, &mut vblk);
1320 } else {
1321 let mut yblk: [[[f32; 64]; 4]; 4] = [[[0.0; 64]; 4]; 4];
1322 let mut ublk: [[f32; 64]; 4] = [[0.0; 64]; 4];
1323 let mut vblk: [[f32; 64]; 4] = [[0.0; 64]; 4];
1324 cbp_y_p = decode_luma_inter_old(br, &self.codes, cbp_y_p, &mut yblk, edge_state_y, &mut self.y_dcs, &mut q_y_p)?;
1325 cbp_v_p = decode_chroma_inter_old(br, &self.codes, cbp_v_p, &mut vblk, edge_state_y, &mut self.v_dcs, &mut q_v_p)?;
1326 cbp_u_p = decode_chroma_inter_old(br, &self.codes, cbp_u_p, &mut ublk, edge_state_y, &mut self.u_dcs, &mut q_u_p)?;
1327 if do_alpha {
1328 let mut ablk: [[[f32; 64]; 4]; 4] = [[[0.0; 64]; 4]; 4];
1329 cbp_a_p = decode_luma_inter_old(br, &self.codes, cbp_a_p, &mut ablk, edge_state_y, &mut self.a_dcs, &mut q_a_p)?;
1330 Bink2DSP::add_mb4_old(dst, off_a + bx * 32 + 0 + 0 * stride_a, stride_a, &mut ablk[0]);
1331 Bink2DSP::add_mb4_old(dst, off_a + bx * 32 + 16 + 0 * stride_a, stride_a, &mut ablk[1]);
1332 Bink2DSP::add_mb4_old(dst, off_a + bx * 32 + 0 + 16 * stride_a, stride_a, &mut ablk[2]);
1333 Bink2DSP::add_mb4_old(dst, off_a + bx * 32 + 16 + 16 * stride_a, stride_a, &mut ablk[3]);
1334 }
1335 Bink2DSP::add_mb4_old(dst, off_y + bx * 32 + 0 + 0 * stride_y, stride_y, &mut yblk[0]);
1336 Bink2DSP::add_mb4_old(dst, off_y + bx * 32 + 16 + 0 * stride_y, stride_y, &mut yblk[1]);
1337 Bink2DSP::add_mb4_old(dst, off_y + bx * 32 + 0 + 16 * stride_y, stride_y, &mut yblk[2]);
1338 Bink2DSP::add_mb4_old(dst, off_y + bx * 32 + 16 + 16 * stride_y, stride_y, &mut yblk[3]);
1339 Bink2DSP::add_mb4_old(dst, off_u + bx * 16, stride_u, &mut ublk);
1340 Bink2DSP::add_mb4_old(dst, off_v + bx * 16, stride_v, &mut vblk);
1341 }
1342 },
1343 _ => unreachable!(),
1344 };
1345 if btype != 0 {
1346 let src = &dst[off_y + bx * 32..];
1347 let dc5 = Bink2DSP::calc_dc(&src[24..], stride_y);
1348 let dc7 = Bink2DSP::calc_dc(&src[24 + stride_y * 8..], stride_y);
1349 let dc13 = Bink2DSP::calc_dc(&src[24 + stride_y * 16..], stride_y);
1350 let dc10 = Bink2DSP::calc_dc(&src[ 0 + stride_y * 24..], stride_y);
1351 let dc11 = Bink2DSP::calc_dc(&src[ 8 + stride_y * 24..], stride_y);
1352 let dc14 = Bink2DSP::calc_dc(&src[16 + stride_y * 24..], stride_y);
1353 let dc15 = Bink2DSP::calc_dc(&src[24 + stride_y * 24..], stride_y);
1354 self.y_dcs.set_dcs(bx, dc5, dc7, dc13, dc10, dc11, dc14, dc15);
1355 let src = &dst[off_u + bx * 16..];
1356 let dc1 = Bink2DSP::calc_dc(&src[8..], stride_u);
1357 let dc2 = Bink2DSP::calc_dc(&src[0 + stride_u * 8..], stride_u);
1358 let dc3 = Bink2DSP::calc_dc(&src[8 + stride_u * 8..], stride_u);
1359 self.u_dcs.set_dcs(bx, dc1, dc2, dc3);
1360 let src = &dst[off_v + bx * 16..];
1361 let dc1 = Bink2DSP::calc_dc(&src[8..], stride_v);
1362 let dc2 = Bink2DSP::calc_dc(&src[0 + stride_v * 8..], stride_v);
1363 let dc3 = Bink2DSP::calc_dc(&src[8 + stride_v * 8..], stride_v);
1364 self.v_dcs.set_dcs(bx, dc1, dc2, dc3);
1365 if do_alpha {
1366 let src = &dst[off_a + bx * 32..];
1367 let dc5 = Bink2DSP::calc_dc(&src[24..], stride_a);
1368 let dc7 = Bink2DSP::calc_dc(&src[24 + stride_y * 8..], stride_a);
1369 let dc13 = Bink2DSP::calc_dc(&src[24 + stride_y * 16..], stride_a);
1370 let dc10 = Bink2DSP::calc_dc(&src[ 0 + stride_y * 24..], stride_a);
1371 let dc11 = Bink2DSP::calc_dc(&src[ 8 + stride_y * 24..], stride_a);
1372 let dc14 = Bink2DSP::calc_dc(&src[16 + stride_y * 24..], stride_a);
1373 let dc15 = Bink2DSP::calc_dc(&src[24 + stride_y * 24..], stride_a);
1374 self.a_dcs.set_dcs(bx, dc5, dc7, dc13, dc10, dc11, dc14, dc15);
1375 }
1376 }
1377 }
1378 self.qinfo.update_line();
1379 self.y_dcs.update_line();
1380 self.u_dcs.update_line();
1381 self.v_dcs.update_line();
1382 self.a_dcs.update_line();
1383 self.mvs.update_line();
1384 off_y += stride_y * 32;
1385 off_u += stride_u * 16;
1386 off_v += stride_v * 16;
1387 off_a += stride_a * 32;
1388 row_state = (row_state & !0x190) | ((row_state & 4) << 2);
1389 }
1390 start_by = self.slice_h[slice_no];
1391 }
1392 Ok(())
1393 }
1394 }
1395
1396 fn decode_flags(br: &mut BitReader, dst: &mut Vec<bool>, start: usize, nbits: usize) -> DecoderResult<()> {
1397 if start > 0 {
1398 dst.push(false);
1399 }
1400 if !br.read_bool()? {
1401 for _ in 0..nbits {
1402 let bit = br.read_bool()?;
1403 dst.push(bit);
1404 }
1405 } else {
1406 let mut cur_bits = nbits;
1407 let mut mode = 0;
1408 let mut lastbit = false;
1409 while cur_bits > 0 {
1410 if !br.read_bool()? {
1411 lastbit = if mode == 3 { !lastbit } else { br.read_bool()? };
1412 dst.push(lastbit);
1413 cur_bits -= 1;
1414 let len = cur_bits.min(4);
1415 for _ in 0..len {
1416 let bit = br.read_bool()?;
1417 dst.push(bit);
1418 }
1419 cur_bits -= len;
1420 mode = 2;
1421 } else {
1422 let bread: u8;
1423 if cur_bits < 4 {
1424 bread = 2;
1425 } else if cur_bits < 16 {
1426 bread = 4;
1427 } else {
1428 bread = 4 | 1;
1429 }
1430 lastbit = if mode == 3 { !lastbit } else { br.read_bool()? };
1431 let mut run = (if mode == 3 { bread + 1 } else { bread + 2 } as usize).min(cur_bits);
1432 if run != cur_bits {
1433 let add_run = br.read(bread)? as usize;
1434 run += add_run;
1435 mode = if add_run == (1 << bread) - 1 { 1 } else { 3 };
1436 }
1437 for _ in 0..run {
1438 dst.push(lastbit);
1439 }
1440 cur_bits -= run;
1441 }
1442 }
1443 }
1444 Ok(())
1445 }
1446
1447 fn get_new_quant(br: &mut BitReader, prev_q: u8) -> DecoderResult<u8> {
1448 let mut code = br.read_code(UintCodeType::LimitedUnary(4, 1))? as u8;
1449 if code == 0 {
1450 Ok(prev_q)
1451 } else {
1452 if code == 3 {
1453 code += br.read(1)? as u8;
1454 } else if code == 4 {
1455 code = (br.read(5)? as u8) + 5;
1456 }
1457 let ret;
1458 if !br.read_bool()? {
1459 ret = prev_q.checked_add(code);
1460 } else {
1461 ret = prev_q.checked_sub(code);
1462 }
1463 validate!(ret.is_some());
1464 let val = ret.unwrap();
1465 validate!(val < (BINK2_QUANT_DC.len() as u8));
1466 Ok(val)
1467 }
1468 }
1469
1470 fn decode_luma_intra(br: &mut BitReader, codes: &Bink2Codes, prev_cbp: u32, q: u8, dst: &mut [[[i32; 64]; 4]; 4], edge_state: u32, dcinfo: &mut YDCInfo, bx: usize) -> DecoderResult<u32> {
1471 let cbp = decode_cbp_luma(br, prev_cbp, edge_state)?;
1472 let _coded = dcinfo.decode_dcs(br, q)?;
1473 dcinfo.predict(bx, (edge_state & 0x88) != 0, (edge_state & 0x20) != 0, 0, 0x7FF);
1474 let dcs = &dcinfo.dcs;
1475 for i in 0..4 {
1476 decode_acs_4blocks(br, codes, &mut dst[i], BINK2_QUANT_INTRA_LUMA, q, cbp >> (i * 4))?;
1477 for j in 0..4 { dst[i][j][0] = dcs[i * 4 + j] * 8 + 32; }
1478 }
1479 Ok(cbp)
1480 }
1481
1482 fn decode_chroma_intra(br: &mut BitReader, codes: &Bink2Codes, prev_cbp: u32, q: u8, dst: &mut [[i32; 64]; 4], edge_state: u32, dcinfo: &mut CDCInfo, bx: usize) -> DecoderResult<u32> {
1483 let cbp = decode_cbp_chroma(br, prev_cbp)?;
1484 let _coded = dcinfo.decode_dcs(br, q)?;
1485 dcinfo.predict(bx, (edge_state & 0x88) != 0, (edge_state & 0x20) != 0, 0, 0x7FF);
1486 decode_acs_4blocks(br, codes, dst, BINK2_QUANT_INTRA_CHROMA, q, cbp)?;
1487 let dcs = &dcinfo.dcs;
1488 for i in 0..4 { dst[i][0] = dcs[i] * 8 + 32; }
1489 Ok(cbp)
1490 }
1491
1492 fn decode_luma_inter(br: &mut BitReader, codes: &Bink2Codes, prev_cbp: u32, q: u8, dst: &mut [[[i32; 64]; 4]; 4], edge_state: u32, dcinfo: &mut YDCInfo) -> DecoderResult<u32> {
1493 let cbp = decode_cbp_luma(br, prev_cbp, edge_state)?;
1494 let (min_dc, max_dc) = if (edge_state & 0x40000) != 0 { (-0x7FF, 0x7FF) } else { (-0x3FF, 0x3FF) };
1495 let _coded = dcinfo.decode_dcs(br, q)?;
1496 dcinfo.predict_inter(min_dc, max_dc);
1497 let dcs = &dcinfo.dcs;
1498 for i in 0..4 {
1499 decode_acs_4blocks(br, codes, &mut dst[i], BINK2_QUANT_INTER, q, cbp >> (i * 4))?;
1500 for j in 0..4 { dst[i][j][0] = dcs[i * 4 + j] * 8; }
1501 }
1502 Ok(cbp)
1503 }
1504
1505 fn decode_chroma_inter(br: &mut BitReader, codes: &Bink2Codes, prev_cbp: u32, q: u8, dst: &mut [[i32; 64]; 4], edge_state: u32, dcinfo: &mut CDCInfo) -> DecoderResult<u32> {
1506 let cbp = decode_cbp_chroma(br, prev_cbp)?;
1507 let (min_dc, max_dc) = if (edge_state & 0x40000) != 0 { (-0x7FF, 0x7FF) } else { (-0x3FF, 0x3FF) };
1508 let _coded = dcinfo.decode_dcs(br, q)?;
1509 dcinfo.predict_inter(min_dc, max_dc);
1510 let dcs = &dcinfo.dcs;
1511 decode_acs_4blocks(br, codes, dst, BINK2_QUANT_INTER, q, cbp)?;
1512 for i in 0..4 { dst[i][0] = dcs[i] * 8; }
1513 Ok(cbp)
1514 }
1515
1516 fn decode_cbp_luma(br: &mut BitReader, prev_cbp: u32, edge_state: u32) -> DecoderResult<u32> {
1517 let cnt = (prev_cbp as u16).count_ones() as usize;
1518 let (min_count, mask) = if cnt < 8 { (cnt, 0x0000) } else { (16 - cnt, 0xFFFF) };
1519
1520 let mut cbp;
1521 if br.read_bool()? {
1522 cbp = 0;
1523 } else if min_count >= 4 {
1524 cbp = br.read(16)?;
1525 } else {
1526 cbp = 0;
1527 for i in 0..4 {
1528 if !br.read_bool()? {
1529 let cbp4 = br.read(4)?;
1530 cbp |= cbp4 << (i * 4);
1531 }
1532 }
1533 }
1534 cbp ^= mask;
1535 if ((edge_state & 0x40000) == 0) || (cbp != 0) {
1536 if br.read_bool()? {
1537 cbp |= cbp << 16;
1538 }
1539 }
1540 Ok(cbp)
1541 }
1542 fn decode_cbp_chroma(br: &mut BitReader, last_cbp: u32) -> DecoderResult<u32> {
1543 if br.read_bool()? {
1544 Ok((last_cbp & 0xF0000) | BINK2_CHROMA_CBPS[(last_cbp & 0xF) as usize])
1545 } else {
1546 let ncbp = br.read(4)?;
1547 if br.read_bool()? {
1548 Ok(ncbp | (ncbp << 16))
1549 } else {
1550 Ok(ncbp)
1551 }
1552 }
1553 }
1554
1555 trait ReadBink2Code {
1556 fn read_bink2_code_zero(&mut self) -> DecoderResult<i32>;
1557 fn read_bink2_code_nz(&mut self) -> DecoderResult<i32>;
1558 }
1559
1560 impl<'a> ReadBink2Code for BitReader<'a> {
1561 fn read_bink2_code_zero(&mut self) -> DecoderResult<i32> {
1562 let pfx = self.read_code(UintCodeType::LimitedUnary(12, 0))? as i32;
1563 if pfx > 0 {
1564 let val: i32;
1565 if pfx >= 4 {
1566 let add_bits = self.read((pfx as u8) - 3)? as i32;
1567 val = (1 << (pfx - 3)) + add_bits + 2;
1568 } else {
1569 val = pfx;
1570 }
1571 Ok(val)
1572 } else {
1573 Ok(0)
1574 }
1575 }
1576 fn read_bink2_code_nz(&mut self) -> DecoderResult<i32> {
1577 let pfx = self.read_code(UintCodeType::LimitedUnary(12, 0))? + 1;
1578 let val: i32;
1579 if pfx >= 4 {
1580 let add_bits = self.read((pfx as u8) - 3)? as i32;
1581 val = (1 << (pfx - 3)) + add_bits + 2;
1582 } else {
1583 val = pfx as i32;
1584 }
1585 if self.read_bool()? {
1586 Ok(-val)
1587 } else {
1588 Ok(val)
1589 }
1590 }
1591 }
1592
1593 fn decode_acs_4blocks(br: &mut BitReader, codes: &Bink2Codes, dst: &mut [[i32; 64]; 4], quant_mat: &[[i32; 64]; 4], q: u8, cbp: u32) -> DecoderResult<()> {
1594 let cb = if (cbp >> 16) != 0 { &codes.ac_cb1 } else { &codes.ac_cb2 };
1595 let qmat = &quant_mat[(q & 3) as usize];
1596 let shift = q >> 2;
1597 for blk_no in 0..4 {
1598 if ((cbp >> blk_no) & 1) == 0 { continue; }
1599 let mut esc_len = 0;
1600 let mut idx = 1;
1601 while idx < 64 {
1602 if esc_len >= 1 {
1603 esc_len -= 1;
1604 } else {
1605 let sym = br.read_cb(cb)?;
1606 let skip;
1607 if sym == 11 {
1608 skip = br.read(6)? as usize;
1609 } else {
1610 skip = BINK2_AC_RUNS[sym as usize];
1611 }
1612 idx += skip;
1613 if idx > 63 { break; }
1614 esc_len = if sym == 13 { 6 } else { 0 };
1615 }
1616 let level = br.read_bink2_code_nz()?;
1617 let pos = BINK2_ZIGZAG[idx];
1618 dst[blk_no][pos] = (((level * qmat[pos]) << shift) + 0x40) >> 7;
1619 idx += 1;
1620 }
1621 // set blk info to (dc & 0x1FFF) | (intra ? 0x8000 : 0) | (((ncoded == 0) + (ncoded < 4) + (ncoded < 8)) << 13)
1622 }
1623 Ok(())
1624 }
1625
1626 fn decode_luma_intra_old(br: &mut BitReader, codes: &Bink2Codes, prev_cbp: u32, dst: &mut [[[f32; 64]; 4]; 4], edge_state: u32, dcinfo: &mut YDCInfo, bx: usize, old_q: &mut u8) -> DecoderResult<u32> {
1627 let cbp = decode_cbp_luma_old(br, prev_cbp)?;
1628 let q = decode_quant_old(br, &codes.quant_cb, *old_q)?;
1629 *old_q = q;
1630 let is_top = (edge_state & 0x88) != 0;
1631 let is_left = (edge_state & 0x20) != 0;
1632 decode_dcs_old(br, &mut dcinfo.dc_buf, q, is_top && is_left)?;
1633 dcinfo.predict(bx, is_top, is_left, 0, 0x7FF);
1634 let dcs = &dcinfo.dcs;
1635 for i in 0..4 {
1636 decode_acs_4blocks_old(br, codes, &mut dst[i], BINK2_QMAT_INTRA_LUMA_OLD, q, cbp >> (i * 4), BINK2_LUMA_SCAN_OLD)?;
1637 for j in 0..4 { dst[i][j][0] = (dcs[i * 4 + j] as f32) * 0.125; }
1638 }
1639 Ok(cbp)
1640 }
1641
1642 fn decode_chroma_intra_old(br: &mut BitReader, codes: &Bink2Codes, prev_cbp: u32, dst: &mut [[f32; 64]; 4], edge_state: u32, dcinfo: &mut CDCInfo, bx: usize, old_q: &mut u8) -> DecoderResult<u32> {
1643 let cbp = decode_cbp_chroma_old(br, prev_cbp)?;
1644 let q = decode_quant_old(br, &codes.quant_cb, *old_q)?;
1645 *old_q = q;
1646 let is_top = (edge_state & 0x88) != 0;
1647 let is_left = (edge_state & 0x20) != 0;
1648 decode_dcs_old(br, &mut dcinfo.dc_buf, q, is_top && is_left)?;
1649 dcinfo.predict(bx, is_top, is_left, 0, 0x7FF);
1650 decode_acs_4blocks_old(br, codes, dst, BINK2_QMAT_CHROMA_OLD, q, cbp, BINK2_CHROMA_SCAN_OLD)?;
1651 let dcs = &dcinfo.dcs;
1652 for i in 0..4 { dst[i][0] = (dcs[i] as f32) * 0.125; }
1653 Ok(cbp)
1654 }
1655
1656
1657 fn decode_luma_inter_old(br: &mut BitReader, codes: &Bink2Codes, prev_cbp: u32, dst: &mut [[[f32; 64]; 4]; 4], _edge_state: u32, dcinfo: &mut YDCInfo, old_q: &mut u8) -> DecoderResult<u32> {
1658 let cbp = decode_cbp_luma_old(br, prev_cbp)?;
1659 let q = decode_quant_old(br, &codes.quant_cb, *old_q)?;
1660 *old_q = q;
1661 decode_dcs_old(br, &mut dcinfo.dc_buf, q, false)?;
1662 dcinfo.predict_inter(-1023, 1023);
1663 let dcs = &dcinfo.dcs;
1664 for i in 0..4 {
1665 decode_acs_4blocks_old(br, codes, &mut dst[i], BINK2_QMAT_INTER_LUMA_OLD, q, cbp >> (i * 4), BINK2_LUMA_SCAN_OLD)?;
1666 for j in 0..4 { dst[i][j][0] = (dcs[i * 4 + j] as f32) * 0.125; }
1667 }
1668 Ok(cbp)
1669 }
1670
1671 fn decode_chroma_inter_old(br: &mut BitReader, codes: &Bink2Codes, prev_cbp: u32, dst: &mut [[f32; 64]; 4], _edge_state: u32, dcinfo: &mut CDCInfo, old_q: &mut u8) -> DecoderResult<u32> {
1672 let cbp = decode_cbp_chroma_old(br, prev_cbp)?;
1673 let q = decode_quant_old(br, &codes.quant_cb, *old_q)?;
1674 *old_q = q;
1675 decode_dcs_old(br, &mut dcinfo.dc_buf, q, false)?;
1676 dcinfo.predict_inter(-1023, 1023);
1677 let dcs = &dcinfo.dcs;
1678 decode_acs_4blocks_old(br, codes, dst, BINK2_QMAT_CHROMA_OLD, q, cbp, BINK2_CHROMA_SCAN_OLD)?;
1679 for i in 0..4 { dst[i][0] = (dcs[i] as f32) * 0.125; }
1680 Ok(cbp)
1681 }
1682
1683 fn decode_quant_old(br: &mut BitReader, quant_cb: &Codebook<u8>, prev_quant: u8) -> DecoderResult<u8> {
1684 let diff = br.read_cb(quant_cb)?;
1685 if diff == 0 {
1686 Ok(prev_quant)
1687 } else {
1688 let res;
1689 if br.read_bool()? {
1690 res = prev_quant.checked_sub(diff);
1691 } else {
1692 res = prev_quant.checked_add(diff);
1693 }
1694 validate!(res.is_some());
1695 let q = res.unwrap();
1696 validate!(q < 16);
1697 Ok(q)
1698 }
1699 }
1700
1701 fn decode_cbp_luma_old(br: &mut BitReader, prev_cbp: u32) -> DecoderResult<u32> {
1702 let low;
1703 if br.read_bool()? {
1704 if br.read_bool()? {
1705 return Ok(prev_cbp);
1706 }
1707 low = prev_cbp & 0xFFFF;
1708 } else {
1709 let mut nib1 = (prev_cbp >> 4) & 0xF;
1710 if !br.read_bool()? {
1711 nib1 = br.read(4)?;
1712 }
1713 let mut new_cbp = nib1;
1714 if !br.read_bool()? {
1715 nib1 = br.read(4)?;
1716 }
1717 new_cbp |= nib1 << 4;
1718 if !br.read_bool()? {
1719 nib1 = br.read(4)?;
1720 }
1721 new_cbp |= nib1 << 8;
1722 if !br.read_bool()? {
1723 nib1 = br.read(4)?;
1724 }
1725 low = new_cbp | (nib1 << 12);
1726 }
1727 let mut nib_hi = (prev_cbp >> 20) & 0xF;
1728 let mut high = 0;
1729 for i in 0..4 {
1730 let nib = (low >> (4 * i)) & 0xF;
1731 if nib.count_ones() == 0 {
1732 nib_hi = 0;
1733 } else if (nib.count_ones() == 1) || !br.read_bool()? {
1734 nib_hi = 0;
1735 for bit in 0..4 {
1736 if (((nib >> bit) & 1) != 0) && br.read_bool()? {
1737 nib_hi |= 1 << bit;
1738 }
1739 }
1740 }
1741 nib_hi &= nib;
1742 high = (high >> 4) | (nib_hi << 28);
1743 }
1744 Ok(low | high)
1745 }
1746
1747 fn decode_cbp_chroma_old(br: &mut BitReader, prev_cbp: u32) -> DecoderResult<u32> {
1748 let low;
1749 if !br.read_bool()? {
1750 low = br.read(4)?;
1751 } else {
1752 if br.read_bool()? {
1753 return Ok(prev_cbp);
1754 }
1755 low = prev_cbp & 0xF;
1756 }
1757 let mut high;
1758 if low.count_ones() == 0 {
1759 return Ok(low);
1760 }
1761 if low.count_ones() != 1 {
1762 high = (prev_cbp >> 16) & low;
1763 if br.read_bool()? {
1764 return Ok((high << 16) | low);
1765 }
1766 }
1767 high = 0;
1768 for bit in 0..4 {
1769 if (((low >> bit) & 1) != 0) && br.read_bool()? {
1770 high |= 1 << bit;
1771 }
1772 }
1773 Ok((high << 16) | low)
1774 }
1775
1776 fn decode_dcs_old(br: &mut BitReader, dcs: &mut [i32], q: u8, read_bias: bool) -> DecoderResult<()> {
1777 let mut bits = br.read(3)? as u8;
1778 if bits == 7 {
1779 bits += br.read(2)? as u8;
1780 }
1781 let scale = BINK2_OLD_DC_QUANTS[q as usize];
1782 if bits == 0 {
1783 for el in dcs.iter_mut() { *el = 0; }
1784 } else {
1785 for dcb in dcs.chunks_mut(4) {
1786 for dc in dcb.iter_mut() {
1787 *dc = br.read(bits)? as i32;
1788 }
1789 for dc in dcb.iter_mut() {
1790 if *dc == 0 { continue; }
1791 *dc *= scale;
1792 if br.read_bool()? {
1793 *dc = -*dc;
1794 }
1795 }
1796 }
1797 }
1798 if read_bias {
1799 let add_bits = BINK2_DC_EXTRA_BITS[q as usize] + bits;
1800 if add_bits < 10 {
1801 let pfx = br.read(10 - add_bits)? as i32;
1802 let base_dc;
1803 if pfx > 0 {
1804 if br.read_bool()? {
1805 base_dc = -(pfx << bits);
1806 } else {
1807 base_dc = pfx << bits;
1808 }
1809 } else {
1810 base_dc = 0;
1811 }
1812 dcs[0] += base_dc * scale;
1813 }
1814 }
1815 Ok(())
1816 }
1817
1818 fn decode_acs_4blocks_old(br: &mut BitReader, codes: &Bink2Codes, dst: &mut [[f32; 64]; 4], quant_mat: &[f32; 64], q: u8, cbp: u32, scan: &[usize; 64]) -> DecoderResult<()> {
1819 let quant = BINK2_OLD_QUANTS[q as usize];
1820 for blk_no in 0..4 {
1821 if ((cbp >> blk_no) & 1) == 0 { continue; }
1822 let val_cb = if ((cbp >> (16 + blk_no)) & 1) != 0 { &codes.val_cb2 } else { &codes.val_cb1 };
1823 let skip_cb = if ((cbp >> (16 + blk_no)) & 1) != 0 { &codes.skip_cb2 } else { &codes.skip_cb1 };
1824 let mut esc_len = 0;
1825 let mut idx = 1;
1826 while idx < 64 {
1827 let val = br.read_cb(val_cb)?;
1828 if val != 0 {
1829 let mut level;
1830 if val >= 4 {
1831 let add_bits = br.read(val - 3)? as i32;
1832 level = (1 << (val - 3)) + add_bits + 2;
1833 } else {
1834 level = val as i32;
1835 }
1836 if br.read_bool()? {
1837 level = -level;
1838 }
1839 let pos = scan[idx];
1840 dst[blk_no][pos] = (level as f32) * quant_mat[(pos & 7) * 8 + (pos >> 3)] * quant;
1841 }
1842 idx += 1;
1843 if idx >= 64 { break; }
1844 if esc_len >= 1 {
1845 esc_len -= 1;
1846 } else {
1847 let sym = br.read_cb(skip_cb)?;
1848 let skip;
1849 if sym == 11 {
1850 skip = br.read(6)? as usize;
1851 } else {
1852 skip = BINK2_AC_RUNS[sym as usize];
1853 }
1854 idx += skip;
1855 if idx > 63 { break; }
1856 esc_len = if sym == 13 { 6 } else { 0 };
1857 }
1858 }
1859 }
1860 Ok(())
1861 }
1862
1863 const KB2H_NUM_SLICES: [usize; 4] = [ 2, 3, 4, 8 ];
1864
1865 impl NADecoder for Bink2Decoder {
1866 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
1867 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
1868 let w = vinfo.get_width();
1869 let h = vinfo.get_height();
1870
1871 let edata = info.get_extradata().unwrap();
1872 validate!(edata.len() >= 8);
1873
1874 let mut mr = MemoryReader::new_read(&edata);
1875 let mut br = ByteReader::new(&mut mr);
1876 let magic = br.read_u32be()?;
1877 let flags = br.read_u32le()?;
1878
1879 self.version = magic;
1880 self.has_alpha = (flags & 0x100000) != 0;
1881
1882 let height_a = (h + 31) & !31;
1883 if self.version <= mktag!(b"KB2f") {
1884 self.num_slices = 2;
1885 self.slice_h[0] = (h + 32) >> 6;
1886 } else if self.version == mktag!(b"KB2g") {
1887 if height_a < 128 {
1888 self.num_slices = 1;
1889 } else {
1890 self.num_slices = 2;
1891 self.slice_h[0] = (h + 31) >> 6;
1892 }
1893 } else {
1894 self.num_slices = KB2H_NUM_SLICES[(flags & 3) as usize];
1895 let mut start = 0;
1896 let mut end = height_a + 32 * self.num_slices - 1;
1897 for i in 0..self.num_slices - 1 {
1898 start += ((end - start) / (self.num_slices - i)) & !31;
1899 end -= 32;
1900 self.slice_h[i] = start >> 5;
1901 }
1902 }
1903 self.slice_h[self.num_slices - 1] = height_a >> 5;
1904
1905 let fmt;
1906 let aplane = if self.has_alpha { Some(NAPixelChromaton::new(0, 0, false, 8, 0, 3, 1)) } else { None };
1907 fmt = NAPixelFormaton::new(ColorModel::YUV(YUVSubmodel::YUVJ),
1908 Some(NAPixelChromaton::new(0, 0, false, 8, 0, 0, 1)),
1909 Some(NAPixelChromaton::new(1, 1, false, 8, 0, 1, 1)),
1910 Some(NAPixelChromaton::new(1, 1, false, 8, 0, 2, 1)),
1911 aplane, None,
1912 if self.has_alpha { FORMATON_FLAG_ALPHA } else { 0 },
1913 if self.has_alpha { 4 } else { 3 });
1914 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
1915 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
1916
1917 Ok(())
1918 } else {
1919 Err(DecoderError::InvalidData)
1920 }
1921 }
1922 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
1923 let src = pkt.get_buffer();
1924
1925 let mut br = BitReader::new(&src, BitReaderMode::LE);
1926
1927 let mut buf;
1928 self.key_frame = pkt.is_keyframe();
1929
1930 let bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 5)?;
1931 buf = bufinfo.get_vbuf().unwrap();
1932
1933 self.decode_frame_new(&mut br, &mut buf, pkt.is_keyframe())?;
1934 let bufinfo = NABufferType::Video(buf);
1935 self.ips.add_frame(bufinfo.get_vbuf().unwrap());
1936
1937 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
1938 frm.set_frame_type(if self.key_frame { FrameType::I } else { FrameType::P });
1939 Ok(frm.into_ref())
1940 }
1941 fn flush(&mut self) {
1942 self.ips.clear();
1943 }
1944 }
1945
1946 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
1947 Box::new(Bink2Decoder::new())
1948 }
1949
1950 #[cfg(test)]
1951 mod test {
1952 use nihav_core::codecs::RegisteredDecoders;
1953 use nihav_core::demuxers::RegisteredDemuxers;
1954 use nihav_codec_support::test::dec_video::*;
1955 use crate::rad_register_all_codecs;
1956 use crate::rad_register_all_demuxers;
1957 #[test]
1958 fn test_bink2() {
1959 let mut dmx_reg = RegisteredDemuxers::new();
1960 rad_register_all_demuxers(&mut dmx_reg);
1961 let mut dec_reg = RegisteredDecoders::new();
1962 rad_register_all_codecs(&mut dec_reg);
1963
1964 //let file = "assets/RAD/Open_Logos_partial.bik";
1965 //let file = "assets/RAD/sc13_01_partial.bk2";
1966 let file = "assets/RAD/ge_video_86l.bk2";
1967 //let file = "assets/RAD/eg_club_0.bk2";
1968 test_file_decoding("bink", file, Some(8), true, false, None/*Some("bink2")*/, &dmx_reg, &dec_reg);
1969 }
1970 }
1971
1972 const BINK2_ZIGZAG: [usize; 64] = [
1973 0, 8, 1, 2, 9, 16, 24, 17,
1974 10, 3, 4, 11, 18, 25, 32, 40,
1975 33, 26, 19, 12, 5, 6, 13, 20,
1976 27, 34, 41, 48, 56, 49, 42, 35,
1977 28, 21, 14, 7, 15, 22, 29, 36,
1978 43, 50, 57, 58, 51, 44, 37, 30,
1979 23, 31, 38, 45, 52, 59, 60, 53,
1980 46, 39, 47, 54, 61, 62, 55, 63
1981 ];
1982 const BINK2_LUMA_SCAN_OLD: &[usize; 64] = &[
1983 0, 2, 1, 8, 9, 17, 10, 16,
1984 24, 3, 18, 25, 32, 11, 33, 26,
1985 4, 40, 19, 12, 27, 41, 34, 5,
1986 20, 48, 6, 28, 15, 42, 23, 35,
1987 21, 13, 14, 7, 31, 43, 49, 36,
1988 22, 56, 39, 50, 30, 44, 29, 51,
1989 57, 47, 58, 59, 63, 61, 55, 38,
1990 52, 62, 45, 37, 60, 46, 54, 53
1991 ];
1992 const BINK2_CHROMA_SCAN_OLD: &[usize; 64] = &[
1993 0, 1, 8, 2, 9, 16, 10, 17,
1994 3, 24, 11, 18, 25, 13, 14, 4,
1995 15, 5, 6, 7, 12, 19, 20, 21,
1996 22, 23, 26, 27, 28, 29, 30, 31,
1997 32, 33, 34, 35, 36, 37, 38, 39,
1998 40, 41, 42, 43, 44, 45, 46, 47,
1999 48, 49, 50, 51, 52, 53, 54, 55,
2000 56, 57, 58, 59, 60, 61, 62, 63
2001 ];
2002
2003 const BINK2_CHROMA_CBPS: [u32; 16] = [
2004 0x0, 0x0, 0x0, 0xF,
2005 0x0, 0xF, 0xF, 0xF,
2006 0x0, 0xF, 0xF, 0xF,
2007 0xF, 0xF, 0xF, 0xF
2008 ];
2009
2010 const BINK2_QUANT_DC: [i32; 37] = [
2011 0x400, 0x4C2, 0x5A8, 0x6BA, 0x800, 0x983, 0xB50, 0xD74,
2012 0x1000, 0x1307, 0x16A1, 0x1AE9, 0x2000, 0x260E, 0x2D41, 0x35D1,
2013 0x4000, 0x4C1C, 0x5A82, 0x6BA3, 0x8000, 0x9838, 0xB505, 0xD745,
2014 0x10000, 0x13070, 0x16A0A, 0x1AE8A, 0x20000, 0x260E0, 0x2D414, 0x35D14,
2015 0x40000, 0x4C1C0, 0x5A828, 0x6BA28, 0x80000
2016 ];
2017
2018 const BINK2_QUANT_INTRA_LUMA: &[[i32; 64]; 4] = &[
2019 [
2020 0x400, 0x598, 0x5E2, 0x49D, 0x733, 0x7E9, 0x1497, 0x2190,
2021 0x521, 0x685, 0x65E, 0x688, 0xA41, 0xD72, 0x1F57, 0x31FA,
2022 0x434, 0x6DB, 0x710, 0x79E, 0xF8C, 0x130B, 0x226D, 0x2E85,
2023 0x546, 0x74C, 0x84F, 0x7E0, 0x1275, 0x1162, 0x1E20, 0x25A5,
2024 0x99A, 0xC1F, 0x10CF, 0x10CF, 0x1B33, 0x1AB3, 0x2B47, 0x3435,
2025 0xD2F, 0x1648, 0x13BC, 0x17A1, 0x23EE, 0x1C40, 0x29E5, 0x266A,
2026 0x156E, 0x1D61, 0x1E75, 0x1BB3, 0x2B47, 0x2720, 0x34F7, 0x3270,
2027 0x1C6F, 0x1DE1, 0x1B6C, 0x17D1, 0x23E5, 0x2357, 0x3175, 0x35C8
2028 ], [
2029 0x4C2, 0x6A7, 0x6FF, 0x57D, 0x890, 0x968, 0x187C, 0x27EA,
2030 0x619, 0x7C1, 0x792, 0x7C4, 0xC32, 0xFFD, 0x2545, 0x3B6F,
2031 0x4FF, 0x827, 0x866, 0x90F, 0x127D, 0x16A6, 0x28F1, 0x3752,
2032 0x646, 0x8AE, 0x9E2, 0x95E, 0x15F3, 0x14AC, 0x23D3, 0x2CC4,
2033 0xB6B, 0xE6A, 0x13FD, 0x13FE, 0x2059, 0x1FC0, 0x3378, 0x3E16,
2034 0xFAE, 0x1A7F, 0x1778, 0x1C1A, 0x2ABA, 0x2198, 0x31D3, 0x2DAE,
2035 0x197C, 0x22F0, 0x2438, 0x20F1, 0x3378, 0x2E87, 0x3EFD, 0x3BFA,
2036 0x21D1, 0x2388, 0x209C, 0x1C52, 0x2AAF, 0x2A07, 0x3AD0, 0x3FF5
2037 ], [
2038 0x5A8, 0x7E9, 0x852, 0x687, 0xA2F, 0xB30, 0x1D1E, 0x2F77,
2039 0x740, 0x938, 0x901, 0x93C, 0xE81, 0x1303, 0x2C52, 0x46AE,
2040 0x5F1, 0x9B2, 0x9FD, 0xAC6, 0x15FC, 0x1AEE, 0x30B0, 0x41C9,
2041 0x775, 0xA52, 0xBC0, 0xB24, 0x1A1B, 0x1895, 0x2A9A, 0x353D,
2042 0xD94, 0x1124, 0x17C5, 0x17C6, 0x2677, 0x25C2, 0x3D34, 0x49D5,
2043 0x12A5, 0x1F82, 0x1BE9, 0x216B, 0x32CF, 0x27F3, 0x3B40, 0x3653,
2044 0x1E4E, 0x298D, 0x2B12, 0x272C, 0x3D34, 0x3755, 0x4AE7, 0x4753,
2045 0x2836, 0x2A41, 0x26C7, 0x21AE, 0x32C3, 0x31FA, 0x45F1, 0x4C0E
2046 ], [
2047 0x6BA, 0x968, 0x9E5, 0x7C3, 0xC1C, 0xD4E, 0x22A0, 0x3872,
2048 0x8A0, 0xAF7, 0xAB5, 0xAFB, 0x113F, 0x169C, 0x34B5, 0x540D,
2049 0x711, 0xB87, 0xBE1, 0xCD0, 0x1A25, 0x2007, 0x39E6, 0x4E3C,
2050 0x8DF, 0xC46, 0xDFA, 0xD3F, 0x1F0B, 0x1D3C, 0x32A9, 0x3F4F,
2051 0x1025, 0x1462, 0x1C44, 0x1C46, 0x2DBF, 0x2CE7, 0x48C9, 0x57CE,
2052 0x162D, 0x2578, 0x2131, 0x27BE, 0x3C6D, 0x2F82, 0x4676, 0x409A,
2053 0x240A, 0x3169, 0x3338, 0x2E96, 0x48C9, 0x41CD, 0x5914, 0x54D3,
2054 0x2FD2, 0x3240, 0x2E1E, 0x280E, 0x3C5E, 0x3B6F, 0x532D, 0x5A73
2055 ]
2056 ];
2057
2058 const BINK2_QUANT_INTRA_CHROMA: &[[i32; 64]; 4] = &[
2059 [
2060 0x400, 0x4A9, 0x59A, 0x89B, 0x1600, 0x1221, 0x171C, 0x19A3,
2061 0x4A9, 0x656, 0x713, 0xE16, 0x19A3, 0x1520, 0x1AEE, 0x1DE1,
2062 0x59A, 0x713, 0xDBB, 0x130B, 0x171C, 0x130B, 0x1847, 0x1AEE,
2063 0x89B, 0xE16, 0x130B, 0xEF0, 0x1221, 0xEF0, 0x130B, 0x1520,
2064 0x1600, 0x19A3, 0x171C, 0x1221, 0x1600, 0x1221, 0x171C, 0x19A3,
2065 0x1221, 0x1520, 0x130B, 0xEF0, 0x1221, 0xEF0, 0x130B, 0x1520,
2066 0x171C, 0x1AEE, 0x1847, 0x130B, 0x171C, 0x130B, 0x1847, 0x1AEE,
2067 0x19A3, 0x1DE1, 0x1AEE, 0x1520, 0x19A3, 0x1520, 0x1AEE, 0x1DE1
2068 ], [
2069 0x4C2, 0x58B, 0x6AA, 0xA3C, 0x1A2A, 0x158F, 0x1B7B, 0x1E7D,
2070 0x58B, 0x789, 0x869, 0x10C0, 0x1E7D, 0x1920, 0x2007, 0x2388,
2071 0x6AA, 0x869, 0x1054, 0x16A6, 0x1B7B, 0x16A6, 0x1CDE, 0x2007,
2072 0xA3C, 0x10C0, 0x16A6, 0x11C4, 0x158F, 0x11C4, 0x16A6, 0x1920,
2073 0x1A2A, 0x1E7D, 0x1B7B, 0x158F, 0x1A2A, 0x158F, 0x1B7B, 0x1E7D,
2074 0x158F, 0x1920, 0x16A6, 0x11C4, 0x158F, 0x11C4, 0x16A6, 0x1920,
2075 0x1B7B, 0x2007, 0x1CDE, 0x16A6, 0x1B7B, 0x16A6, 0x1CDE, 0x2007,
2076 0x1E7D, 0x2388, 0x2007, 0x1920, 0x1E7D, 0x1920, 0x2007, 0x2388
2077 ], [
2078 0x5A8, 0x698, 0x7EC, 0xC2C, 0x1F1D, 0x19A3, 0x20AF, 0x2442,
2079 0x698, 0x8F6, 0xA01, 0x13EB, 0x2442, 0x1DE1, 0x2616, 0x2A41,
2080 0x7EC, 0xA01, 0x136B, 0x1AEE, 0x20AF, 0x1AEE, 0x2255, 0x2616,
2081 0xC2C, 0x13EB, 0x1AEE, 0x1520, 0x19A3, 0x1520, 0x1AEE, 0x1DE1,
2082 0x1F1D, 0x2442, 0x20AF, 0x19A3, 0x1F1D, 0x19A3, 0x20AF, 0x2442,
2083 0x19A3, 0x1DE1, 0x1AEE, 0x1520, 0x19A3, 0x1520, 0x1AEE, 0x1DE1,
2084 0x20AF, 0x2616, 0x2255, 0x1AEE, 0x20AF, 0x1AEE, 0x2255, 0x2616,
2085 0x2442, 0x2A41, 0x2616, 0x1DE1, 0x2442, 0x1DE1, 0x2616, 0x2A41
2086 ], [
2087 0x6BA, 0x7D7, 0x96C, 0xE7A, 0x2500, 0x1E7D, 0x26DE, 0x2B1E,
2088 0x7D7, 0xAA9, 0xBE5, 0x17B0, 0x2B1E, 0x2388, 0x2D4B, 0x3240,
2089 0x96C, 0xBE5, 0x1718, 0x2007, 0x26DE, 0x2007, 0x28D4, 0x2D4B,
2090 0xE7A, 0x17B0, 0x2007, 0x1920, 0x1E7D, 0x1920, 0x2007, 0x2388,
2091 0x2500, 0x2B1E, 0x26DE, 0x1E7D, 0x2500, 0x1E7D, 0x26DE, 0x2B1E,
2092 0x1E7D, 0x2388, 0x2007, 0x1920, 0x1E7D, 0x1920, 0x2007, 0x2388,
2093 0x26DE, 0x2D4B, 0x28D4, 0x2007, 0x26DE, 0x2007, 0x28D4, 0x2D4B,
2094 0x2B1E, 0x3240, 0x2D4B, 0x2388, 0x2B1E, 0x2388, 0x2D4B, 0x3240
2095 ]
2096 ];
2097
2098 const BINK2_QUANT_INTER: &[[i32; 64]; 4] = &[
2099 [
2100 0x400, 0x4A9, 0x434, 0x34C, 0x41C, 0x392, 0x4C9, 0x5D4,
2101 0x4A9, 0x56F, 0x4E6, 0x3D7, 0x4CB, 0x429, 0x5B7, 0x718,
2102 0x434, 0x4E6, 0x489, 0x3A8, 0x4AB, 0x40A, 0x5A4, 0x6CD,
2103 0x34C, 0x3D7, 0x3A8, 0x32B, 0x41F, 0x39F, 0x519, 0x630,
2104 0x41C, 0x4CB, 0x4AB, 0x41F, 0x5AB, 0x538, 0x778, 0x932,
2105 0x392, 0x429, 0x40A, 0x39F, 0x538, 0x521, 0x799, 0x9B6,
2106 0x4C9, 0x5B7, 0x5A4, 0x519, 0x778, 0x799, 0xBE4, 0xFC7,
2107 0x5D4, 0x718, 0x6CD, 0x630, 0x932, 0x9B6, 0xFC7, 0x162F
2108 ], [
2109 0x4C2, 0x58B, 0x4FF, 0x3EB, 0x4E4, 0x43F, 0x5B1, 0x6EE,
2110 0x58B, 0x676, 0x5D3, 0x491, 0x5B3, 0x4F3, 0x6CB, 0x86F,
2111 0x4FF, 0x5D3, 0x565, 0x459, 0x58D, 0x4CE, 0x6B5, 0x816,
2112 0x3EB, 0x491, 0x459, 0x3C5, 0x4E6, 0x44F, 0x610, 0x75C,
2113 0x4E4, 0x5B3, 0x58D, 0x4E6, 0x6BD, 0x635, 0x8E2, 0xAEF,
2114 0x43F, 0x4F3, 0x4CE, 0x44F, 0x635, 0x61A, 0x909, 0xB8C,
2115 0x5B1, 0x6CB, 0x6B5, 0x610, 0x8E2, 0x909, 0xE24, 0x12C3,
2116 0x6EE, 0x86F, 0x816, 0x75C, 0xAEF, 0xB8C, 0x12C3, 0x1A61
2117 ], [
2118 0x5A8, 0x698, 0x5F1, 0x4A9, 0x5D0, 0x50D, 0x6C4, 0x83E,
2119 0x698, 0x7AF, 0x6ED, 0x56F, 0x6C7, 0x5E3, 0x814, 0xA08,
2120 0x5F1, 0x6ED, 0x66A, 0x52B, 0x69A, 0x5B6, 0x7FA, 0x99E,
2121 0x4A9, 0x56F, 0x52B, 0x47B, 0x5D4, 0x51F, 0x735, 0x8C1,
2122 0x5D0, 0x6C7, 0x69A, 0x5D4, 0x804, 0x761, 0xA90, 0xD00,
2123 0x50D, 0x5E3, 0x5B6, 0x51F, 0x761, 0x741, 0xABF, 0xDBB,
2124 0x6C4, 0x814, 0x7FA, 0x735, 0xA90, 0xABF, 0x10D2, 0x1650,
2125 0x83E, 0xA08, 0x99E, 0x8C1, 0xD00, 0xDBB, 0x1650, 0x1F5F
2126 ], [
2127 0x6BA, 0x7D7, 0x711, 0x58B, 0x6EA, 0x601, 0x80C, 0x9CD,
2128 0x7D7, 0x923, 0x83C, 0x676, 0x80F, 0x700, 0x99C, 0xBEE,
2129 0x711, 0x83C, 0x7A1, 0x626, 0x7DA, 0x6CB, 0x97C, 0xB70,
2130 0x58B, 0x676, 0x626, 0x554, 0x6EE, 0x617, 0x893, 0xA68,
2131 0x6EA, 0x80F, 0x7DA, 0x6EE, 0x988, 0x8C7, 0xC90, 0xF76,
2132 0x601, 0x700, 0x6CB, 0x617, 0x8C7, 0x8A1, 0xCC7, 0x1055,
2133 0x80C, 0x99C, 0x97C, 0x893, 0xC90, 0xCC7, 0x1400, 0x1A89,
2134 0x9CD, 0xBEE, 0xB70, 0xA68, 0xF76, 0x1055, 0x1A89, 0x254E
2135 ]
2136 ];
2137
2138 const BINK2_AC_CODES1: &[u8] = &[
2139 0x01, 0x04, 0x00, 0x08, 0x02, 0x32, 0x0A, 0x12,
2140 0x3A, 0x7A, 0xFA, 0x72, 0x06, 0x1A
2141 ];
2142 const BINK2_AC_BITS1: &[u8] = &[ 1, 3, 4, 4, 5, 7, 5, 6, 7, 8, 8, 7, 3, 6 ];
2143 const BINK2_AC_CODES2: &[u16] = &[
2144 0x01, 0x00, 0x04, 0x2C, 0x6C, 0x0C, 0x4C, 0xAC,
2145 0xEC, 0x12C, 0x16C, 0x1AC, 0x02, 0x1C
2146 ];
2147 const BINK2_AC_BITS2: &[u8] = &[ 1, 3, 4, 9, 9, 7, 7, 9, 8, 9, 9, 9, 2, 5 ];
2148
2149 const BINK2_AC_RUNS: [usize; 14] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 64, 0 ];
2150
2151 const BINK2_MV_CODES: &[u8] = &[
2152 0x01, 0x06, 0x0C, 0x1C, 0x18, 0x38, 0x58, 0x78,
2153 0x68, 0x48, 0x28, 0x08, 0x14, 0x04, 0x02, 0x00
2154 ];
2155 const BINK2_MV_BITS: &[u8] = &[
2156 1, 3, 5, 5, 7, 7, 7, 7,
2157 7, 7, 7, 7, 5, 5, 3, 4
2158 ];
2159 const BINK2_MV_ESC: i8 = 42;
2160 const BINK2_MV_SYMS: &[i8] = &[
2161 0, 1, 2, 3, 4, 5, 6, 7,
2162 -7, -6, -5, -4, -3, -2, -1, BINK2_MV_ESC
2163 ];
2164
2165 const BINK2_AC_VAL_CODES1: &[u16] = &[
2166 0x004, 0x01, 0x002, 0x00, 0x08, 0x18, 0xF8, 0x178,
2167 0x138, 0x38, 0x1B8, 0x78, 0xB8
2168 ];
2169 const BINK2_AC_VAL_BITS1: &[u8] = &[ 3, 1, 2, 4, 5, 6, 8, 9, 9, 9, 9, 9, 9 ];
2170 const BINK2_AC_VAL_CODES2: &[u16] = &[
2171 0x0A, 0x001, 0x004, 0x08, 0x06, 0x00, 0x02, 0x1A,
2172 0x2A, 0x16A, 0x1EA, 0x6A, 0xEA
2173 ];
2174 const BINK2_AC_VAL_BITS2: &[u8] = &[ 6, 1, 3, 4, 3, 4, 4, 5, 7, 9, 9, 9, 9 ];
2175 const BINK2_AC_SKIP_CODES1: &[u16] = &[
2176 0x00, 0x001, 0x0D, 0x15, 0x45, 0x85, 0xA5, 0x165,
2177 0x65, 0x1E5, 0xE5, 0x25, 0x03, 0x05
2178 ];
2179 const BINK2_AC_SKIP_BITS1: &[u8] = &[ 1, 3, 4, 5, 7, 8, 8, 9, 9, 9, 9, 8, 2, 8 ];
2180 const BINK2_AC_SKIP_CODES2: &[u16] = &[
2181 0x00, 0x01, 0x003, 0x07, 0x1F, 0x1B, 0x0F, 0x2F,
2182 0x5B, 0xDB, 0x1DB, 0x3B, 0x05, 0x0B
2183 ];
2184 const BINK2_AC_SKIP_BITS2: &[u8] = &[ 1, 3, 4, 4, 5, 7, 6, 6, 8, 9, 9, 6, 3, 5 ];
2185 const BINK2_QUANT_CODES: &[u8] = &[
2186 0x01, 0x02, 0x04, 0x08, 0x10, 0x30, 0x50, 0x70,
2187 0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xE0
2188 ];
2189 const BINK2_QUANT_BITS: &[u8] = &[ 1, 2, 3, 4, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8 ];
2190
2191 const BINK2_OLD_QUANTS: [f32; 16] = [
2192 1.0, 2.0, 2.5, 3.0, 3.5, 4.0, 6.0, 7.0, 8.0, 12.0, 16.0, 24.0, 32.0, 48.0, 64.0, 128.0
2193 ];
2194 const BINK2_OLD_DC_QUANTS: [i32; 16] = [
2195 4, 4, 4, 4, 4, 6, 7, 8, 10, 12, 16, 24, 32, 48, 64, 128
2196 ];
2197 const BINK2_DC_EXTRA_BITS: [u8; 16] = [ 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6 ];
2198 const BINK2_QMAT_INTRA_LUMA_OLD: &[f32; 64] = &[
2199 0.125, 0.208056, 0.22864901, 0.205778, 0.22499999, 0.235708, 0.33148301, 0.248309,
2200 0.190718, 0.288582, 0.29449099, 0.34658501, 0.38143599, 0.47678301, 0.600528, 0.44008601,
2201 0.16332, 0.31714499, 0.34142101, 0.42249799, 0.604285, 0.70576, 0.68942899, 0.42807001,
2202 0.235175, 0.38735899, 0.46090701, 0.50122303, 0.82311302, 0.73910397, 0.69206202, 0.39741901,
2203 0.30000001, 0.45078799, 0.65328097, 0.74962097, 0.85000002, 0.79551601, 0.69678998, 0.38625899,
2204 0.392847, 0.79009801, 0.73142397, 1.004719, 1.070509, 0.80251199, 0.64313799, 0.27096599,
2205 0.34501299, 0.56299502, 0.60987997, 0.636379, 0.69678998, 0.60061598, 0.43934, 0.19224399,
2206 0.210373, 0.26309499, 0.252336, 0.25142801, 0.265553, 0.24928901, 0.188511, 0.094199002
2207 ];
2208 const BINK2_QMAT_CHROMA_OLD: &[f32; 64] = &[
2209 0.125, 0.17338, 0.217761, 0.383793, 0.6875, 0.54016501, 0.37207201, 0.18968099,
2210 0.17338, 0.28056601, 0.32721299, 0.74753499, 0.95358998, 0.74923098, 0.51607901, 0.26309499,
2211 0.217761, 0.32721299, 0.66387498, 1.056244, 0.89826202, 0.70576, 0.48613599, 0.24783,
2212 0.383793, 0.74753499, 1.056244, 0.95059502, 0.80841398, 0.635167, 0.437511, 0.223041,
2213 0.6875, 0.95358998, 0.89826202, 0.80841398, 0.6875, 0.54016501, 0.37207201, 0.18968099,
2214 0.54016501, 0.74923098, 0.70576, 0.635167, 0.54016501, 0.42440501, 0.292335, 0.149031,
2215 0.37207201, 0.51607901, 0.48613599, 0.437511, 0.37207201, 0.292335, 0.201364, 0.102655,
2216 0.18968099, 0.26309499, 0.24783, 0.223041, 0.18968099, 0.149031, 0.102655, 0.052333001
2217 ];
2218 const BINK2_QMAT_INTER_LUMA_OLD: &[f32; 64] = &[
2219 0.125, 0.17338, 0.16332, 0.146984, 0.128475, 0.106393, 0.077045999, 0.043109,
2220 0.17338, 0.240485, 0.226532, 0.20387299, 0.17820001, 0.147571, 0.109474, 0.062454,
2221 0.16332, 0.226532, 0.219321, 0.202722, 0.181465, 0.149711, 0.112943, 0.062583998,
2222 0.146984, 0.20387299, 0.202722, 0.201647, 0.183731, 0.15397599, 0.11711, 0.065334998,
2223 0.128475, 0.17820001, 0.181465, 0.183731, 0.17708801, 0.155499, 0.120267, 0.068016,
2224 0.106393, 0.147571, 0.149711, 0.15397599, 0.155499, 0.14575601, 0.116636, 0.068494998,
2225 0.077045999, 0.109474, 0.112943, 0.11711, 0.120267, 0.116636, 0.098646, 0.060141001,
2226 0.043109, 0.062454, 0.062583998, 0.065334998, 0.068016, 0.068494998, 0.060141001, 0.038853001
2227 ];