]> git.nihav.org Git - nihav.git/blob - nihav-rad/src/codecs/bink2.rs
rad: fix or silence clippy warnings
[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 out[..16].copy_from_slice(&row[..16]);
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 out[..8].copy_from_slice(&src[..8]);
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 #[allow(clippy::cyclomatic_complexity)]
1031 fn decode_frame_new(&mut self, br: &mut BitReader, buf: &mut NAVideoBuffer<u8>, is_intra: bool) -> DecoderResult<()> {
1032 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));
1033 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));
1034 let (ooff_y, ooff_u, ooff_v, ooff_a) = (off_y, off_u, off_v, off_a);
1035 let (width, height) = buf.get_dimensions(0);
1036 let data = buf.get_data_mut().unwrap();
1037 let dst = data.as_mut_slice();
1038 let bw = (width + 31) >> 5;
1039 let bheight = (height + 31) >> 5;
1040 self.cur_w = (width + 7) & !7;
1041 self.cur_h = ((height + 7) & !7) >> 1;
1042
1043 let frame_flags = br.read(32)?;
1044 let mut offsets: [u32; 7] = [0; 7];
1045 for i in 0..self.num_slices-1 {
1046 offsets[i] = br.read(32)?;
1047 }
1048 let mut do_alpha = self.has_alpha;
1049 if (frame_flags & 0x80000) != 0 && self.has_alpha {
1050 do_alpha = false;
1051 let fillval = (frame_flags >> 24) as u8;
1052 let aplane = &mut dst[off_a..][..stride_a * bheight * 32];
1053 for el in aplane.iter_mut() {
1054 *el = fillval;
1055 }
1056 }
1057 let mut row_flags: Vec<bool> = Vec::with_capacity(bheight * 4);
1058 let mut col_flags: Vec<bool> = Vec::with_capacity(bw * 4);
1059 if (frame_flags & 0x10000) != 0 {
1060 if (frame_flags & 0x8000) == 0 {
1061 let len = (height + 15) >> 4;
1062 decode_flags(br, &mut row_flags, 1, len * 2 - 1)?;
1063 }
1064 if (frame_flags & 0x4000) == 0 {
1065 let len = (width + 15) >> 4;
1066 decode_flags(br, &mut col_flags, 1, len * 2 - 1)?;
1067 }
1068 }
1069 row_flags.resize(bheight * 4, false);
1070 col_flags.resize(bw * 4, false);
1071 //store frame_flags * 8 & 0x7F8
1072
1073 let mut start_by = 0;
1074 for slice_no in 0..self.num_slices {
1075 let end_by = self.slice_h[slice_no];
1076 if slice_no != 0 {
1077 br.seek(offsets[slice_no - 1] * 8)?;
1078 }
1079 off_y = ooff_y + stride_y * start_by * 32;
1080 off_u = ooff_u + stride_u * start_by * 16;
1081 off_v = ooff_v + stride_v * start_by * 16;
1082 off_a = ooff_a + stride_a * start_by * 32;
1083
1084 let mut row_state = frame_flags & 0x2E000;
1085 if is_intra {
1086 row_state |= 0x10000;
1087 }
1088 self.qinfo.resize(bw);
1089 self.y_dcs.resize(bw);
1090 self.u_dcs.resize(bw);
1091 self.v_dcs.resize(bw);
1092 self.a_dcs.resize(bw);
1093 self.mvs.resize(bw);
1094 for by in start_by..end_by {
1095 let mut cbp_y = 0;
1096 let mut cbp_u = 0;
1097 let mut cbp_v = 0;
1098 let mut cbp_a = 0;
1099 let mut cbp_y_p = 0;
1100 let mut cbp_u_p = 0;
1101 let mut cbp_v_p = 0;
1102 let mut cbp_a_p = 0;
1103 let mut q_y = 8;
1104 let mut q_u = 8;
1105 let mut q_v = 8;
1106 let mut q_a = 8;
1107 let mut q_y_p = 8;
1108 let mut q_u_p = 8;
1109 let mut q_v_p = 8;
1110 let mut q_a_p = 8;
1111 let rflags = (row_flags[by] as u32) * 4;
1112 row_state = (row_state & 0x3FFFFFF) | ((row_state >> 4) & 0xC000000) | (rflags << 28);
1113 if by == start_by {
1114 row_state |= 0x80;
1115 // } else {
1116 // row_state |= 0x8;
1117 }
1118 if by + 2 >= end_by {
1119 row_state |= 0x100;
1120 }
1121
1122 let mut btype_lru: [u8; 4] = [ 2, 3, 1, 0 ];
1123 let mut edge_state = 0;
1124 let is_top = by == start_by;
1125 for bx in 0..bw {
1126 let mut blk_state = row_state | (edge_state & 0x3FC0000);
1127 if bx == 0 {
1128 blk_state |= 0x20;
1129 }
1130 if bx + 2 < bw {
1131 blk_state |= 0x40;
1132 }
1133 if (bx & 1) != 0 {
1134 blk_state |= 0x200;
1135 }
1136 let clflags = (col_flags[bx] as u32) * 4;
1137 let edge_state_c = ((blk_state >> 4) & 0x3C0000) | (blk_state & 0xFC03FFFF) | ((clflags & 0xF) << 22);
1138 let edge_state_y = (frame_flags & 0x40000) | (blk_state & 0x3FFFF);
1139 edge_state = edge_state_c;
1140
1141 //let is_top = (edge_state & 0x88) != 0;
1142 let is_left = bx == 0; //(edge_state & 0x20) != 0;
1143
1144 let btype;
1145 if is_intra {
1146 btype = 0;
1147 } else {
1148 if (blk_state & 0x2000) != 0 {
1149 let val = br.read_code(UintCodeType::LimitedUnary(3, 1))?;
1150 match val {
1151 0 => {
1152 btype = btype_lru[0];
1153 },
1154 1 => {
1155 btype = btype_lru[1];
1156 btype_lru[1] = btype_lru[0];
1157 btype_lru[0] = btype;
1158 },
1159 2 => {
1160 btype = btype_lru[3];
1161 btype_lru[3] = btype_lru[2];
1162 btype_lru[2] = btype;
1163 },
1164 3 => {
1165 btype = btype_lru[2];
1166 btype_lru[2] = btype_lru[1];
1167 btype_lru[1] = btype;
1168 },
1169 _ => unreachable!(),
1170 };
1171 } else {
1172 btype = br.read(2)? as u8;
1173 }
1174 }
1175 if !is_intra {
1176 let q = self.qinfo.pred_quant(bx * 2, is_top);
1177 self.qinfo.set_quant(bx * 2, q);
1178 let q = self.qinfo.pred_quant(bx * 2 + 1, is_top);
1179 self.qinfo.set_quant(bx * 2 + 1, q);
1180 }
1181 match btype {
1182 0 => { // intra
1183 if (frame_flags & 0x2000) != 0 {
1184 let mut yblk: [[[i32; 64]; 4]; 4] = [[[0; 64]; 4]; 4];
1185 let mut ublk: [[i32; 64]; 4] = [[0; 64]; 4];
1186 let mut vblk: [[i32; 64]; 4] = [[0; 64]; 4];
1187 let q = get_new_quant(br, self.qinfo.pred_quant(bx * 2, is_top))?;
1188 self.qinfo.set_quant(bx * 2, q);
1189 cbp_y = decode_luma_intra(br, &self.codes, cbp_y, q, &mut yblk, edge_state_y, &mut self.y_dcs, bx)?;
1190 cbp_v = decode_chroma_intra(br, &self.codes, cbp_v, q, &mut vblk, edge_state_c, &mut self.v_dcs, bx)?;
1191 cbp_u = decode_chroma_intra(br, &self.codes, cbp_u, q, &mut ublk, edge_state_c, &mut self.u_dcs, bx)?;
1192 if do_alpha {
1193 let mut ablk: [[[i32; 64]; 4]; 4] = [[[0; 64]; 4]; 4];
1194 cbp_a = decode_luma_intra(br, &self.codes, cbp_a, q, &mut ablk, edge_state_y, &mut self.a_dcs, bx)?;
1195 Bink2DSP::put_mb4(dst, off_a + bx * 32 + 0 + 0 * stride_a, stride_a, &mut ablk[0]);
1196 Bink2DSP::put_mb4(dst, off_a + bx * 32 + 16 + 0 * stride_a, stride_a, &mut ablk[1]);
1197 Bink2DSP::put_mb4(dst, off_a + bx * 32 + 0 + 16 * stride_a, stride_a, &mut ablk[2]);
1198 Bink2DSP::put_mb4(dst, off_a + bx * 32 + 16 + 16 * stride_a, stride_a, &mut ablk[3]);
1199 }
1200 //if smth else decode one more y
1201 Bink2DSP::put_mb4(dst, off_y + bx * 32 + 0 + 0 * stride_y, stride_y, &mut yblk[0]);
1202 Bink2DSP::put_mb4(dst, off_y + bx * 32 + 16 + 0 * stride_y, stride_y, &mut yblk[1]);
1203 Bink2DSP::put_mb4(dst, off_y + bx * 32 + 0 + 16 * stride_y, stride_y, &mut yblk[2]);
1204 Bink2DSP::put_mb4(dst, off_y + bx * 32 + 16 + 16 * stride_y, stride_y, &mut yblk[3]);
1205 Bink2DSP::put_mb4(dst, off_u + bx * 16, stride_u, &mut ublk);
1206 Bink2DSP::put_mb4(dst, off_v + bx * 16, stride_v, &mut vblk);
1207 } else {
1208 let mut yblk: [[[f32; 64]; 4]; 4] = [[[0.0; 64]; 4]; 4];
1209 let mut ublk: [[f32; 64]; 4] = [[0.0; 64]; 4];
1210 let mut vblk: [[f32; 64]; 4] = [[0.0; 64]; 4];
1211 cbp_y = decode_luma_intra_old(br, &self.codes, cbp_y, &mut yblk, edge_state_y, &mut self.y_dcs, bx, &mut q_y)?;
1212 cbp_v = decode_chroma_intra_old(br, &self.codes, cbp_v, &mut vblk, edge_state_c, &mut self.v_dcs, bx, &mut q_v)?;
1213 cbp_u = decode_chroma_intra_old(br, &self.codes, cbp_u, &mut ublk, edge_state_c, &mut self.u_dcs, bx, &mut q_u)?;
1214 if do_alpha {
1215 let mut ablk: [[[f32; 64]; 4]; 4] = [[[0.0; 64]; 4]; 4];
1216 cbp_a = decode_luma_intra_old(br, &self.codes, cbp_a, &mut ablk, edge_state_y, &mut self.a_dcs, bx, &mut q_a)?;
1217 Bink2DSP::put_mb4_old(dst, off_a + bx * 32 + 0 + 0 * stride_a, stride_a, &mut ablk[0]);
1218 Bink2DSP::put_mb4_old(dst, off_a + bx * 32 + 16 + 0 * stride_a, stride_a, &mut ablk[1]);
1219 Bink2DSP::put_mb4_old(dst, off_a + bx * 32 + 0 + 16 * stride_a, stride_a, &mut ablk[2]);
1220 Bink2DSP::put_mb4_old(dst, off_a + bx * 32 + 16 + 16 * stride_a, stride_a, &mut ablk[3]);
1221 }
1222 Bink2DSP::put_mb4_old(dst, off_y + bx * 32 + 0 + 0 * stride_y, stride_y, &mut yblk[0]);
1223 Bink2DSP::put_mb4_old(dst, off_y + bx * 32 + 16 + 0 * stride_y, stride_y, &mut yblk[1]);
1224 Bink2DSP::put_mb4_old(dst, off_y + bx * 32 + 0 + 16 * stride_y, stride_y, &mut yblk[2]);
1225 Bink2DSP::put_mb4_old(dst, off_y + bx * 32 + 16 + 16 * stride_y, stride_y, &mut yblk[3]);
1226 Bink2DSP::put_mb4_old(dst, off_u + bx * 16, stride_u, &mut ublk);
1227 Bink2DSP::put_mb4_old(dst, off_v + bx * 16, stride_v, &mut vblk);
1228 }
1229 if !is_intra {
1230 self.mvs.predict_mvs(bx, is_top, is_left, (frame_flags & 0x2000) != 0);
1231 }
1232 },
1233 1 => { // skip
1234 if let Some(ref ref_pic) = self.ips.get_ref() {
1235 for blk_no in 0..4 {
1236 let xoff = bx * 32 + (blk_no & 1) * 16;
1237 let yoff = by * 32 + (blk_no & 2) * 8;
1238 Bink2DSP::mc_luma(&mut dst[off_y..], stride_y, ref_pic, xoff, yoff, ZERO_MV, 0)?;
1239 Bink2DSP::mc_chroma(&mut dst[off_u..], stride_u, ref_pic, xoff >> 1, yoff >> 1, ZERO_MV, 1)?;
1240 Bink2DSP::mc_chroma(&mut dst[off_v..], stride_v, ref_pic, xoff >> 1, yoff >> 1, ZERO_MV, 2)?;
1241 if do_alpha {
1242 Bink2DSP::mc_luma(&mut dst[off_a..], stride_a, ref_pic, xoff, yoff, ZERO_MV, 3)?;
1243 }
1244 }
1245 } else {
1246 return Err(DecoderError::MissingReference);
1247 }
1248 self.mvs.zero(bx);
1249 },
1250 2 => { // motion
1251 if (frame_flags & 0x2000) != 0 {
1252 self.mvs.decode_mv_new(br, &self.codes.mv_cb, bx, is_top, is_left)?;
1253 } else {
1254 self.mvs.decode_mv_old(br, bx, is_top, is_left)?;
1255 }
1256 if let Some(ref ref_pic) = self.ips.get_ref() {
1257 for blk_no in 0..4 {
1258 let xoff = bx * 32 + (blk_no & 1) * 16;
1259 let yoff = by * 32 + (blk_no & 2) * 8;
1260 let mv = self.mvs.get_mv(bx, blk_no);
1261 Bink2DSP::mc_luma(&mut dst[off_y..], stride_y, ref_pic, xoff, yoff, mv, 0)?;
1262 Bink2DSP::mc_chroma(&mut dst[off_u..], stride_u, ref_pic, xoff >> 1, yoff >> 1, mv, 1)?;
1263 Bink2DSP::mc_chroma(&mut dst[off_v..], stride_v, ref_pic, xoff >> 1, yoff >> 1, mv, 2)?;
1264 if do_alpha {
1265 Bink2DSP::mc_luma(&mut dst[off_a..], stride_a, ref_pic, xoff, yoff, mv, 3)?;
1266 }
1267 }
1268 } else {
1269 return Err(DecoderError::MissingReference);
1270 }
1271 },
1272 3 => { // inter
1273 if (frame_flags & 0x2000) != 0 {
1274 self.mvs.decode_mv_new(br, &self.codes.mv_cb, bx, is_top, is_left)?;
1275 } else {
1276 self.mvs.decode_mv_old(br, bx, is_top, is_left)?;
1277 }
1278 if let Some(ref ref_pic) = self.ips.get_ref() {
1279 for blk_no in 0..4 {
1280 let xoff = bx * 32 + (blk_no & 1) * 16;
1281 let yoff = by * 32 + (blk_no & 2) * 8;
1282 let mv = self.mvs.get_mv(bx, blk_no);
1283 Bink2DSP::mc_luma(&mut dst[off_y..], stride_y, ref_pic, xoff, yoff, mv, 0)?;
1284 Bink2DSP::mc_chroma(&mut dst[off_u..], stride_u, ref_pic, xoff >> 1, yoff >> 1, mv, 1)?;
1285 Bink2DSP::mc_chroma(&mut dst[off_v..], stride_v, ref_pic, xoff >> 1, yoff >> 1, mv, 2)?;
1286 if do_alpha {
1287 Bink2DSP::mc_luma(&mut dst[off_a..], stride_a, ref_pic, xoff, yoff, mv, 3)?;
1288 }
1289 }
1290 } else {
1291 return Err(DecoderError::MissingReference);
1292 }
1293 if (frame_flags & 0x2000) != 0 {
1294 let mut yblk: [[[i32; 64]; 4]; 4] = [[[0; 64]; 4]; 4];
1295 let mut ublk: [[i32; 64]; 4] = [[0; 64]; 4];
1296 let mut vblk: [[i32; 64]; 4] = [[0; 64]; 4];
1297 let q = get_new_quant(br, self.qinfo.pred_quant(bx * 2 + 1, is_top))?;
1298 self.qinfo.set_quant(bx * 2 + 1, q);
1299 cbp_y_p = decode_luma_inter(br, &self.codes, cbp_y_p, q, &mut yblk, edge_state_y, &mut self.y_dcs)?;
1300 if br.read_bool()? {
1301 cbp_v_p = decode_chroma_inter(br, &self.codes, cbp_v_p, q, &mut vblk, edge_state_y, &mut self.v_dcs)?;
1302 cbp_u_p = decode_chroma_inter(br, &self.codes, cbp_u_p, q, &mut ublk, edge_state_y, &mut self.u_dcs)?;
1303 } else {
1304 cbp_v_p = 0;
1305 cbp_u_p = 0;
1306 }
1307 if do_alpha {
1308 let mut ablk: [[[i32; 64]; 4]; 4] = [[[0; 64]; 4]; 4];
1309 cbp_a_p = decode_luma_inter(br, &self.codes, cbp_a_p, q, &mut ablk, edge_state_y, &mut self.a_dcs)?;
1310 Bink2DSP::add_mb4(dst, off_a + bx * 32 + 0 + 0 * stride_a, stride_a, &mut ablk[0]);
1311 Bink2DSP::add_mb4(dst, off_a + bx * 32 + 16 + 0 * stride_a, stride_a, &mut ablk[1]);
1312 Bink2DSP::add_mb4(dst, off_a + bx * 32 + 0 + 16 * stride_a, stride_a, &mut ablk[2]);
1313 Bink2DSP::add_mb4(dst, off_a + bx * 32 + 16 + 16 * stride_a, stride_a, &mut ablk[3]);
1314 }
1315 Bink2DSP::add_mb4(dst, off_y + bx * 32 + 0 + 0 * stride_y, stride_y, &mut yblk[0]);
1316 Bink2DSP::add_mb4(dst, off_y + bx * 32 + 16 + 0 * stride_y, stride_y, &mut yblk[1]);
1317 Bink2DSP::add_mb4(dst, off_y + bx * 32 + 0 + 16 * stride_y, stride_y, &mut yblk[2]);
1318 Bink2DSP::add_mb4(dst, off_y + bx * 32 + 16 + 16 * stride_y, stride_y, &mut yblk[3]);
1319 Bink2DSP::add_mb4(dst, off_u + bx * 16, stride_u, &mut ublk);
1320 Bink2DSP::add_mb4(dst, off_v + bx * 16, stride_v, &mut vblk);
1321 } else {
1322 let mut yblk: [[[f32; 64]; 4]; 4] = [[[0.0; 64]; 4]; 4];
1323 let mut ublk: [[f32; 64]; 4] = [[0.0; 64]; 4];
1324 let mut vblk: [[f32; 64]; 4] = [[0.0; 64]; 4];
1325 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)?;
1326 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)?;
1327 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)?;
1328 if do_alpha {
1329 let mut ablk: [[[f32; 64]; 4]; 4] = [[[0.0; 64]; 4]; 4];
1330 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)?;
1331 Bink2DSP::add_mb4_old(dst, off_a + bx * 32 + 0 + 0 * stride_a, stride_a, &mut ablk[0]);
1332 Bink2DSP::add_mb4_old(dst, off_a + bx * 32 + 16 + 0 * stride_a, stride_a, &mut ablk[1]);
1333 Bink2DSP::add_mb4_old(dst, off_a + bx * 32 + 0 + 16 * stride_a, stride_a, &mut ablk[2]);
1334 Bink2DSP::add_mb4_old(dst, off_a + bx * 32 + 16 + 16 * stride_a, stride_a, &mut ablk[3]);
1335 }
1336 Bink2DSP::add_mb4_old(dst, off_y + bx * 32 + 0 + 0 * stride_y, stride_y, &mut yblk[0]);
1337 Bink2DSP::add_mb4_old(dst, off_y + bx * 32 + 16 + 0 * stride_y, stride_y, &mut yblk[1]);
1338 Bink2DSP::add_mb4_old(dst, off_y + bx * 32 + 0 + 16 * stride_y, stride_y, &mut yblk[2]);
1339 Bink2DSP::add_mb4_old(dst, off_y + bx * 32 + 16 + 16 * stride_y, stride_y, &mut yblk[3]);
1340 Bink2DSP::add_mb4_old(dst, off_u + bx * 16, stride_u, &mut ublk);
1341 Bink2DSP::add_mb4_old(dst, off_v + bx * 16, stride_v, &mut vblk);
1342 }
1343 },
1344 _ => unreachable!(),
1345 };
1346 if btype != 0 {
1347 let src = &dst[off_y + bx * 32..];
1348 let dc5 = Bink2DSP::calc_dc(&src[24..], stride_y);
1349 let dc7 = Bink2DSP::calc_dc(&src[24 + stride_y * 8..], stride_y);
1350 let dc13 = Bink2DSP::calc_dc(&src[24 + stride_y * 16..], stride_y);
1351 let dc10 = Bink2DSP::calc_dc(&src[ 0 + stride_y * 24..], stride_y);
1352 let dc11 = Bink2DSP::calc_dc(&src[ 8 + stride_y * 24..], stride_y);
1353 let dc14 = Bink2DSP::calc_dc(&src[16 + stride_y * 24..], stride_y);
1354 let dc15 = Bink2DSP::calc_dc(&src[24 + stride_y * 24..], stride_y);
1355 self.y_dcs.set_dcs(bx, dc5, dc7, dc13, dc10, dc11, dc14, dc15);
1356 let src = &dst[off_u + bx * 16..];
1357 let dc1 = Bink2DSP::calc_dc(&src[8..], stride_u);
1358 let dc2 = Bink2DSP::calc_dc(&src[0 + stride_u * 8..], stride_u);
1359 let dc3 = Bink2DSP::calc_dc(&src[8 + stride_u * 8..], stride_u);
1360 self.u_dcs.set_dcs(bx, dc1, dc2, dc3);
1361 let src = &dst[off_v + bx * 16..];
1362 let dc1 = Bink2DSP::calc_dc(&src[8..], stride_v);
1363 let dc2 = Bink2DSP::calc_dc(&src[0 + stride_v * 8..], stride_v);
1364 let dc3 = Bink2DSP::calc_dc(&src[8 + stride_v * 8..], stride_v);
1365 self.v_dcs.set_dcs(bx, dc1, dc2, dc3);
1366 if do_alpha {
1367 let src = &dst[off_a + bx * 32..];
1368 let dc5 = Bink2DSP::calc_dc(&src[24..], stride_a);
1369 let dc7 = Bink2DSP::calc_dc(&src[24 + stride_y * 8..], stride_a);
1370 let dc13 = Bink2DSP::calc_dc(&src[24 + stride_y * 16..], stride_a);
1371 let dc10 = Bink2DSP::calc_dc(&src[ 0 + stride_y * 24..], stride_a);
1372 let dc11 = Bink2DSP::calc_dc(&src[ 8 + stride_y * 24..], stride_a);
1373 let dc14 = Bink2DSP::calc_dc(&src[16 + stride_y * 24..], stride_a);
1374 let dc15 = Bink2DSP::calc_dc(&src[24 + stride_y * 24..], stride_a);
1375 self.a_dcs.set_dcs(bx, dc5, dc7, dc13, dc10, dc11, dc14, dc15);
1376 }
1377 }
1378 }
1379 self.qinfo.update_line();
1380 self.y_dcs.update_line();
1381 self.u_dcs.update_line();
1382 self.v_dcs.update_line();
1383 self.a_dcs.update_line();
1384 self.mvs.update_line();
1385 off_y += stride_y * 32;
1386 off_u += stride_u * 16;
1387 off_v += stride_v * 16;
1388 off_a += stride_a * 32;
1389 row_state = (row_state & !0x190) | ((row_state & 4) << 2);
1390 }
1391 start_by = self.slice_h[slice_no];
1392 }
1393 Ok(())
1394 }
1395 }
1396
1397 fn decode_flags(br: &mut BitReader, dst: &mut Vec<bool>, start: usize, nbits: usize) -> DecoderResult<()> {
1398 if start > 0 {
1399 dst.push(false);
1400 }
1401 if !br.read_bool()? {
1402 for _ in 0..nbits {
1403 let bit = br.read_bool()?;
1404 dst.push(bit);
1405 }
1406 } else {
1407 let mut cur_bits = nbits;
1408 let mut mode = 0;
1409 let mut lastbit = false;
1410 while cur_bits > 0 {
1411 if !br.read_bool()? {
1412 lastbit = if mode == 3 { !lastbit } else { br.read_bool()? };
1413 dst.push(lastbit);
1414 cur_bits -= 1;
1415 let len = cur_bits.min(4);
1416 for _ in 0..len {
1417 let bit = br.read_bool()?;
1418 dst.push(bit);
1419 }
1420 cur_bits -= len;
1421 mode = 2;
1422 } else {
1423 let bread: u8;
1424 if cur_bits < 4 {
1425 bread = 2;
1426 } else if cur_bits < 16 {
1427 bread = 4;
1428 } else {
1429 bread = 4 | 1;
1430 }
1431 lastbit = if mode == 3 { !lastbit } else { br.read_bool()? };
1432 let mut run = (if mode == 3 { bread + 1 } else { bread + 2 } as usize).min(cur_bits);
1433 if run != cur_bits {
1434 let add_run = br.read(bread)? as usize;
1435 run += add_run;
1436 mode = if add_run == (1 << bread) - 1 { 1 } else { 3 };
1437 }
1438 for _ in 0..run {
1439 dst.push(lastbit);
1440 }
1441 cur_bits -= run;
1442 }
1443 }
1444 }
1445 Ok(())
1446 }
1447
1448 fn get_new_quant(br: &mut BitReader, prev_q: u8) -> DecoderResult<u8> {
1449 let mut code = br.read_code(UintCodeType::LimitedUnary(4, 1))? as u8;
1450 if code == 0 {
1451 Ok(prev_q)
1452 } else {
1453 if code == 3 {
1454 code += br.read(1)? as u8;
1455 } else if code == 4 {
1456 code = (br.read(5)? as u8) + 5;
1457 }
1458 let ret;
1459 if !br.read_bool()? {
1460 ret = prev_q.checked_add(code);
1461 } else {
1462 ret = prev_q.checked_sub(code);
1463 }
1464 validate!(ret.is_some());
1465 let val = ret.unwrap();
1466 validate!(val < (BINK2_QUANT_DC.len() as u8));
1467 Ok(val)
1468 }
1469 }
1470
1471 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> {
1472 let cbp = decode_cbp_luma(br, prev_cbp, edge_state)?;
1473 let _coded = dcinfo.decode_dcs(br, q)?;
1474 dcinfo.predict(bx, (edge_state & 0x88) != 0, (edge_state & 0x20) != 0, 0, 0x7FF);
1475 let dcs = &dcinfo.dcs;
1476 for i in 0..4 {
1477 decode_acs_4blocks(br, codes, &mut dst[i], BINK2_QUANT_INTRA_LUMA, q, cbp >> (i * 4))?;
1478 for j in 0..4 { dst[i][j][0] = dcs[i * 4 + j] * 8 + 32; }
1479 }
1480 Ok(cbp)
1481 }
1482
1483 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> {
1484 let cbp = decode_cbp_chroma(br, prev_cbp)?;
1485 let _coded = dcinfo.decode_dcs(br, q)?;
1486 dcinfo.predict(bx, (edge_state & 0x88) != 0, (edge_state & 0x20) != 0, 0, 0x7FF);
1487 decode_acs_4blocks(br, codes, dst, BINK2_QUANT_INTRA_CHROMA, q, cbp)?;
1488 let dcs = &dcinfo.dcs;
1489 for i in 0..4 { dst[i][0] = dcs[i] * 8 + 32; }
1490 Ok(cbp)
1491 }
1492
1493 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> {
1494 let cbp = decode_cbp_luma(br, prev_cbp, edge_state)?;
1495 let (min_dc, max_dc) = if (edge_state & 0x40000) != 0 { (-0x7FF, 0x7FF) } else { (-0x3FF, 0x3FF) };
1496 let _coded = dcinfo.decode_dcs(br, q)?;
1497 dcinfo.predict_inter(min_dc, max_dc);
1498 let dcs = &dcinfo.dcs;
1499 for i in 0..4 {
1500 decode_acs_4blocks(br, codes, &mut dst[i], BINK2_QUANT_INTER, q, cbp >> (i * 4))?;
1501 for j in 0..4 { dst[i][j][0] = dcs[i * 4 + j] * 8; }
1502 }
1503 Ok(cbp)
1504 }
1505
1506 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> {
1507 let cbp = decode_cbp_chroma(br, prev_cbp)?;
1508 let (min_dc, max_dc) = if (edge_state & 0x40000) != 0 { (-0x7FF, 0x7FF) } else { (-0x3FF, 0x3FF) };
1509 let _coded = dcinfo.decode_dcs(br, q)?;
1510 dcinfo.predict_inter(min_dc, max_dc);
1511 let dcs = &dcinfo.dcs;
1512 decode_acs_4blocks(br, codes, dst, BINK2_QUANT_INTER, q, cbp)?;
1513 for i in 0..4 { dst[i][0] = dcs[i] * 8; }
1514 Ok(cbp)
1515 }
1516
1517 fn decode_cbp_luma(br: &mut BitReader, prev_cbp: u32, edge_state: u32) -> DecoderResult<u32> {
1518 let cnt = (prev_cbp as u16).count_ones() as usize;
1519 let (min_count, mask) = if cnt < 8 { (cnt, 0x0000) } else { (16 - cnt, 0xFFFF) };
1520
1521 let mut cbp;
1522 if br.read_bool()? {
1523 cbp = 0;
1524 } else if min_count >= 4 {
1525 cbp = br.read(16)?;
1526 } else {
1527 cbp = 0;
1528 for i in 0..4 {
1529 if !br.read_bool()? {
1530 let cbp4 = br.read(4)?;
1531 cbp |= cbp4 << (i * 4);
1532 }
1533 }
1534 }
1535 cbp ^= mask;
1536 if ((edge_state & 0x40000) == 0) || (cbp != 0) {
1537 if br.read_bool()? {
1538 cbp |= cbp << 16;
1539 }
1540 }
1541 Ok(cbp)
1542 }
1543 fn decode_cbp_chroma(br: &mut BitReader, last_cbp: u32) -> DecoderResult<u32> {
1544 if br.read_bool()? {
1545 Ok((last_cbp & 0xF0000) | BINK2_CHROMA_CBPS[(last_cbp & 0xF) as usize])
1546 } else {
1547 let ncbp = br.read(4)?;
1548 if br.read_bool()? {
1549 Ok(ncbp | (ncbp << 16))
1550 } else {
1551 Ok(ncbp)
1552 }
1553 }
1554 }
1555
1556 trait ReadBink2Code {
1557 fn read_bink2_code_zero(&mut self) -> DecoderResult<i32>;
1558 fn read_bink2_code_nz(&mut self) -> DecoderResult<i32>;
1559 }
1560
1561 impl<'a> ReadBink2Code for BitReader<'a> {
1562 fn read_bink2_code_zero(&mut self) -> DecoderResult<i32> {
1563 let pfx = self.read_code(UintCodeType::LimitedUnary(12, 0))? as i32;
1564 if pfx > 0 {
1565 let val: i32;
1566 if pfx >= 4 {
1567 let add_bits = self.read((pfx as u8) - 3)? as i32;
1568 val = (1 << (pfx - 3)) + add_bits + 2;
1569 } else {
1570 val = pfx;
1571 }
1572 Ok(val)
1573 } else {
1574 Ok(0)
1575 }
1576 }
1577 fn read_bink2_code_nz(&mut self) -> DecoderResult<i32> {
1578 let pfx = self.read_code(UintCodeType::LimitedUnary(12, 0))? + 1;
1579 let val: i32;
1580 if pfx >= 4 {
1581 let add_bits = self.read((pfx as u8) - 3)? as i32;
1582 val = (1 << (pfx - 3)) + add_bits + 2;
1583 } else {
1584 val = pfx as i32;
1585 }
1586 if self.read_bool()? {
1587 Ok(-val)
1588 } else {
1589 Ok(val)
1590 }
1591 }
1592 }
1593
1594 fn decode_acs_4blocks(br: &mut BitReader, codes: &Bink2Codes, dst: &mut [[i32; 64]; 4], quant_mat: &[[i32; 64]; 4], q: u8, cbp: u32) -> DecoderResult<()> {
1595 let cb = if (cbp >> 16) != 0 { &codes.ac_cb1 } else { &codes.ac_cb2 };
1596 let qmat = &quant_mat[(q & 3) as usize];
1597 let shift = q >> 2;
1598 for blk_no in 0..4 {
1599 if ((cbp >> blk_no) & 1) == 0 { continue; }
1600 let mut esc_len = 0;
1601 let mut idx = 1;
1602 while idx < 64 {
1603 if esc_len >= 1 {
1604 esc_len -= 1;
1605 } else {
1606 let sym = br.read_cb(cb)?;
1607 let skip;
1608 if sym == 11 {
1609 skip = br.read(6)? as usize;
1610 } else {
1611 skip = BINK2_AC_RUNS[sym as usize];
1612 }
1613 idx += skip;
1614 if idx > 63 { break; }
1615 esc_len = if sym == 13 { 6 } else { 0 };
1616 }
1617 let level = br.read_bink2_code_nz()?;
1618 let pos = BINK2_ZIGZAG[idx];
1619 dst[blk_no][pos] = (((level * qmat[pos]) << shift) + 0x40) >> 7;
1620 idx += 1;
1621 }
1622 // set blk info to (dc & 0x1FFF) | (intra ? 0x8000 : 0) | (((ncoded == 0) + (ncoded < 4) + (ncoded < 8)) << 13)
1623 }
1624 Ok(())
1625 }
1626
1627 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> {
1628 let cbp = decode_cbp_luma_old(br, prev_cbp)?;
1629 let q = decode_quant_old(br, &codes.quant_cb, *old_q)?;
1630 *old_q = q;
1631 let is_top = (edge_state & 0x88) != 0;
1632 let is_left = (edge_state & 0x20) != 0;
1633 decode_dcs_old(br, &mut dcinfo.dc_buf, q, is_top && is_left)?;
1634 dcinfo.predict(bx, is_top, is_left, 0, 0x7FF);
1635 let dcs = &dcinfo.dcs;
1636 for i in 0..4 {
1637 decode_acs_4blocks_old(br, codes, &mut dst[i], BINK2_QMAT_INTRA_LUMA_OLD, q, cbp >> (i * 4), BINK2_LUMA_SCAN_OLD)?;
1638 for j in 0..4 { dst[i][j][0] = (dcs[i * 4 + j] as f32) * 0.125; }
1639 }
1640 Ok(cbp)
1641 }
1642
1643 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> {
1644 let cbp = decode_cbp_chroma_old(br, prev_cbp)?;
1645 let q = decode_quant_old(br, &codes.quant_cb, *old_q)?;
1646 *old_q = q;
1647 let is_top = (edge_state & 0x88) != 0;
1648 let is_left = (edge_state & 0x20) != 0;
1649 decode_dcs_old(br, &mut dcinfo.dc_buf, q, is_top && is_left)?;
1650 dcinfo.predict(bx, is_top, is_left, 0, 0x7FF);
1651 decode_acs_4blocks_old(br, codes, dst, BINK2_QMAT_CHROMA_OLD, q, cbp, BINK2_CHROMA_SCAN_OLD)?;
1652 let dcs = &dcinfo.dcs;
1653 for i in 0..4 { dst[i][0] = (dcs[i] as f32) * 0.125; }
1654 Ok(cbp)
1655 }
1656
1657
1658 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> {
1659 let cbp = decode_cbp_luma_old(br, prev_cbp)?;
1660 let q = decode_quant_old(br, &codes.quant_cb, *old_q)?;
1661 *old_q = q;
1662 decode_dcs_old(br, &mut dcinfo.dc_buf, q, false)?;
1663 dcinfo.predict_inter(-1023, 1023);
1664 let dcs = &dcinfo.dcs;
1665 for i in 0..4 {
1666 decode_acs_4blocks_old(br, codes, &mut dst[i], BINK2_QMAT_INTER_LUMA_OLD, q, cbp >> (i * 4), BINK2_LUMA_SCAN_OLD)?;
1667 for j in 0..4 { dst[i][j][0] = (dcs[i * 4 + j] as f32) * 0.125; }
1668 }
1669 Ok(cbp)
1670 }
1671
1672 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> {
1673 let cbp = decode_cbp_chroma_old(br, prev_cbp)?;
1674 let q = decode_quant_old(br, &codes.quant_cb, *old_q)?;
1675 *old_q = q;
1676 decode_dcs_old(br, &mut dcinfo.dc_buf, q, false)?;
1677 dcinfo.predict_inter(-1023, 1023);
1678 let dcs = &dcinfo.dcs;
1679 decode_acs_4blocks_old(br, codes, dst, BINK2_QMAT_CHROMA_OLD, q, cbp, BINK2_CHROMA_SCAN_OLD)?;
1680 for i in 0..4 { dst[i][0] = (dcs[i] as f32) * 0.125; }
1681 Ok(cbp)
1682 }
1683
1684 fn decode_quant_old(br: &mut BitReader, quant_cb: &Codebook<u8>, prev_quant: u8) -> DecoderResult<u8> {
1685 let diff = br.read_cb(quant_cb)?;
1686 if diff == 0 {
1687 Ok(prev_quant)
1688 } else {
1689 let res;
1690 if br.read_bool()? {
1691 res = prev_quant.checked_sub(diff);
1692 } else {
1693 res = prev_quant.checked_add(diff);
1694 }
1695 validate!(res.is_some());
1696 let q = res.unwrap();
1697 validate!(q < 16);
1698 Ok(q)
1699 }
1700 }
1701
1702 fn decode_cbp_luma_old(br: &mut BitReader, prev_cbp: u32) -> DecoderResult<u32> {
1703 let low;
1704 if br.read_bool()? {
1705 if br.read_bool()? {
1706 return Ok(prev_cbp);
1707 }
1708 low = prev_cbp & 0xFFFF;
1709 } else {
1710 let mut nib1 = (prev_cbp >> 4) & 0xF;
1711 if !br.read_bool()? {
1712 nib1 = br.read(4)?;
1713 }
1714 let mut new_cbp = nib1;
1715 if !br.read_bool()? {
1716 nib1 = br.read(4)?;
1717 }
1718 new_cbp |= nib1 << 4;
1719 if !br.read_bool()? {
1720 nib1 = br.read(4)?;
1721 }
1722 new_cbp |= nib1 << 8;
1723 if !br.read_bool()? {
1724 nib1 = br.read(4)?;
1725 }
1726 low = new_cbp | (nib1 << 12);
1727 }
1728 let mut nib_hi = (prev_cbp >> 20) & 0xF;
1729 let mut high = 0;
1730 for i in 0..4 {
1731 let nib = (low >> (4 * i)) & 0xF;
1732 if nib.count_ones() == 0 {
1733 nib_hi = 0;
1734 } else if (nib.count_ones() == 1) || !br.read_bool()? {
1735 nib_hi = 0;
1736 for bit in 0..4 {
1737 if (((nib >> bit) & 1) != 0) && br.read_bool()? {
1738 nib_hi |= 1 << bit;
1739 }
1740 }
1741 }
1742 nib_hi &= nib;
1743 high = (high >> 4) | (nib_hi << 28);
1744 }
1745 Ok(low | high)
1746 }
1747
1748 fn decode_cbp_chroma_old(br: &mut BitReader, prev_cbp: u32) -> DecoderResult<u32> {
1749 let low;
1750 if !br.read_bool()? {
1751 low = br.read(4)?;
1752 } else {
1753 if br.read_bool()? {
1754 return Ok(prev_cbp);
1755 }
1756 low = prev_cbp & 0xF;
1757 }
1758 let mut high;
1759 if low.count_ones() == 0 {
1760 return Ok(low);
1761 }
1762 if low.count_ones() != 1 {
1763 high = (prev_cbp >> 16) & low;
1764 if br.read_bool()? {
1765 return Ok((high << 16) | low);
1766 }
1767 }
1768 high = 0;
1769 for bit in 0..4 {
1770 if (((low >> bit) & 1) != 0) && br.read_bool()? {
1771 high |= 1 << bit;
1772 }
1773 }
1774 Ok((high << 16) | low)
1775 }
1776
1777 fn decode_dcs_old(br: &mut BitReader, dcs: &mut [i32], q: u8, read_bias: bool) -> DecoderResult<()> {
1778 let mut bits = br.read(3)? as u8;
1779 if bits == 7 {
1780 bits += br.read(2)? as u8;
1781 }
1782 let scale = BINK2_OLD_DC_QUANTS[q as usize];
1783 if bits == 0 {
1784 for el in dcs.iter_mut() { *el = 0; }
1785 } else {
1786 for dcb in dcs.chunks_mut(4) {
1787 for dc in dcb.iter_mut() {
1788 *dc = br.read(bits)? as i32;
1789 }
1790 for dc in dcb.iter_mut() {
1791 if *dc == 0 { continue; }
1792 *dc *= scale;
1793 if br.read_bool()? {
1794 *dc = -*dc;
1795 }
1796 }
1797 }
1798 }
1799 if read_bias {
1800 let add_bits = BINK2_DC_EXTRA_BITS[q as usize] + bits;
1801 if add_bits < 10 {
1802 let pfx = br.read(10 - add_bits)? as i32;
1803 let base_dc;
1804 if pfx > 0 {
1805 if br.read_bool()? {
1806 base_dc = -(pfx << bits);
1807 } else {
1808 base_dc = pfx << bits;
1809 }
1810 } else {
1811 base_dc = 0;
1812 }
1813 dcs[0] += base_dc * scale;
1814 }
1815 }
1816 Ok(())
1817 }
1818
1819 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<()> {
1820 let quant = BINK2_OLD_QUANTS[q as usize];
1821 for blk_no in 0..4 {
1822 if ((cbp >> blk_no) & 1) == 0 { continue; }
1823 let val_cb = if ((cbp >> (16 + blk_no)) & 1) != 0 { &codes.val_cb2 } else { &codes.val_cb1 };
1824 let skip_cb = if ((cbp >> (16 + blk_no)) & 1) != 0 { &codes.skip_cb2 } else { &codes.skip_cb1 };
1825 let mut esc_len = 0;
1826 let mut idx = 1;
1827 while idx < 64 {
1828 let val = br.read_cb(val_cb)?;
1829 if val != 0 {
1830 let mut level;
1831 if val >= 4 {
1832 let add_bits = br.read(val - 3)? as i32;
1833 level = (1 << (val - 3)) + add_bits + 2;
1834 } else {
1835 level = val as i32;
1836 }
1837 if br.read_bool()? {
1838 level = -level;
1839 }
1840 let pos = scan[idx];
1841 dst[blk_no][pos] = (level as f32) * quant_mat[(pos & 7) * 8 + (pos >> 3)] * quant;
1842 }
1843 idx += 1;
1844 if idx >= 64 { break; }
1845 if esc_len >= 1 {
1846 esc_len -= 1;
1847 } else {
1848 let sym = br.read_cb(skip_cb)?;
1849 let skip;
1850 if sym == 11 {
1851 skip = br.read(6)? as usize;
1852 } else {
1853 skip = BINK2_AC_RUNS[sym as usize];
1854 }
1855 idx += skip;
1856 if idx > 63 { break; }
1857 esc_len = if sym == 13 { 6 } else { 0 };
1858 }
1859 }
1860 }
1861 Ok(())
1862 }
1863
1864 const KB2H_NUM_SLICES: [usize; 4] = [ 2, 3, 4, 8 ];
1865
1866 impl NADecoder for Bink2Decoder {
1867 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
1868 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
1869 let w = vinfo.get_width();
1870 let h = vinfo.get_height();
1871
1872 let edata = info.get_extradata().unwrap();
1873 validate!(edata.len() >= 8);
1874
1875 let mut mr = MemoryReader::new_read(&edata);
1876 let mut br = ByteReader::new(&mut mr);
1877 let magic = br.read_u32be()?;
1878 let flags = br.read_u32le()?;
1879
1880 self.version = magic;
1881 self.has_alpha = (flags & 0x100000) != 0;
1882
1883 let height_a = (h + 31) & !31;
1884 if self.version <= mktag!(b"KB2f") {
1885 self.num_slices = 2;
1886 self.slice_h[0] = (h + 32) >> 6;
1887 } else if self.version == mktag!(b"KB2g") {
1888 if height_a < 128 {
1889 self.num_slices = 1;
1890 } else {
1891 self.num_slices = 2;
1892 self.slice_h[0] = (h + 31) >> 6;
1893 }
1894 } else {
1895 self.num_slices = KB2H_NUM_SLICES[(flags & 3) as usize];
1896 let mut start = 0;
1897 let mut end = height_a + 32 * self.num_slices - 1;
1898 for i in 0..self.num_slices - 1 {
1899 start += ((end - start) / (self.num_slices - i)) & !31;
1900 end -= 32;
1901 self.slice_h[i] = start >> 5;
1902 }
1903 }
1904 self.slice_h[self.num_slices - 1] = height_a >> 5;
1905
1906 let fmt;
1907 let aplane = if self.has_alpha { Some(NAPixelChromaton::new(0, 0, false, 8, 0, 3, 1)) } else { None };
1908 fmt = NAPixelFormaton::new(ColorModel::YUV(YUVSubmodel::YUVJ),
1909 Some(NAPixelChromaton::new(0, 0, false, 8, 0, 0, 1)),
1910 Some(NAPixelChromaton::new(1, 1, false, 8, 0, 1, 1)),
1911 Some(NAPixelChromaton::new(1, 1, false, 8, 0, 2, 1)),
1912 aplane, None,
1913 if self.has_alpha { FORMATON_FLAG_ALPHA } else { 0 },
1914 if self.has_alpha { 4 } else { 3 });
1915 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
1916 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
1917
1918 Ok(())
1919 } else {
1920 Err(DecoderError::InvalidData)
1921 }
1922 }
1923 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
1924 let src = pkt.get_buffer();
1925
1926 let mut br = BitReader::new(&src, BitReaderMode::LE);
1927
1928 let mut buf;
1929 self.key_frame = pkt.is_keyframe();
1930
1931 let bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 5)?;
1932 buf = bufinfo.get_vbuf().unwrap();
1933
1934 self.decode_frame_new(&mut br, &mut buf, pkt.is_keyframe())?;
1935 let bufinfo = NABufferType::Video(buf);
1936 self.ips.add_frame(bufinfo.get_vbuf().unwrap());
1937
1938 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
1939 frm.set_frame_type(if self.key_frame { FrameType::I } else { FrameType::P });
1940 Ok(frm.into_ref())
1941 }
1942 fn flush(&mut self) {
1943 self.ips.clear();
1944 }
1945 }
1946
1947 impl NAOptionHandler for Bink2Decoder {
1948 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
1949 fn set_options(&mut self, _options: &[NAOption]) { }
1950 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
1951 }
1952
1953 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
1954 Box::new(Bink2Decoder::new())
1955 }
1956
1957 #[cfg(test)]
1958 mod test {
1959 use nihav_core::codecs::RegisteredDecoders;
1960 use nihav_core::demuxers::RegisteredDemuxers;
1961 use nihav_codec_support::test::dec_video::*;
1962 use crate::rad_register_all_codecs;
1963 use crate::rad_register_all_demuxers;
1964 #[test]
1965 fn test_bink2() {
1966 let mut dmx_reg = RegisteredDemuxers::new();
1967 rad_register_all_demuxers(&mut dmx_reg);
1968 let mut dec_reg = RegisteredDecoders::new();
1969 rad_register_all_codecs(&mut dec_reg);
1970
1971 //let file = "assets/RAD/Open_Logos_partial.bik";
1972 //let file = "assets/RAD/sc13_01_partial.bk2";
1973 let file = "assets/RAD/ge_video_86l.bk2";
1974 //let file = "assets/RAD/eg_club_0.bk2";
1975 test_file_decoding("bink", file, Some(8), true, false, None/*Some("bink2")*/, &dmx_reg, &dec_reg);
1976 }
1977 }
1978
1979 const BINK2_ZIGZAG: [usize; 64] = [
1980 0, 8, 1, 2, 9, 16, 24, 17,
1981 10, 3, 4, 11, 18, 25, 32, 40,
1982 33, 26, 19, 12, 5, 6, 13, 20,
1983 27, 34, 41, 48, 56, 49, 42, 35,
1984 28, 21, 14, 7, 15, 22, 29, 36,
1985 43, 50, 57, 58, 51, 44, 37, 30,
1986 23, 31, 38, 45, 52, 59, 60, 53,
1987 46, 39, 47, 54, 61, 62, 55, 63
1988 ];
1989 const BINK2_LUMA_SCAN_OLD: &[usize; 64] = &[
1990 0, 2, 1, 8, 9, 17, 10, 16,
1991 24, 3, 18, 25, 32, 11, 33, 26,
1992 4, 40, 19, 12, 27, 41, 34, 5,
1993 20, 48, 6, 28, 15, 42, 23, 35,
1994 21, 13, 14, 7, 31, 43, 49, 36,
1995 22, 56, 39, 50, 30, 44, 29, 51,
1996 57, 47, 58, 59, 63, 61, 55, 38,
1997 52, 62, 45, 37, 60, 46, 54, 53
1998 ];
1999 const BINK2_CHROMA_SCAN_OLD: &[usize; 64] = &[
2000 0, 1, 8, 2, 9, 16, 10, 17,
2001 3, 24, 11, 18, 25, 13, 14, 4,
2002 15, 5, 6, 7, 12, 19, 20, 21,
2003 22, 23, 26, 27, 28, 29, 30, 31,
2004 32, 33, 34, 35, 36, 37, 38, 39,
2005 40, 41, 42, 43, 44, 45, 46, 47,
2006 48, 49, 50, 51, 52, 53, 54, 55,
2007 56, 57, 58, 59, 60, 61, 62, 63
2008 ];
2009
2010 const BINK2_CHROMA_CBPS: [u32; 16] = [
2011 0x0, 0x0, 0x0, 0xF,
2012 0x0, 0xF, 0xF, 0xF,
2013 0x0, 0xF, 0xF, 0xF,
2014 0xF, 0xF, 0xF, 0xF
2015 ];
2016
2017 const BINK2_QUANT_DC: [i32; 37] = [
2018 0x400, 0x4C2, 0x5A8, 0x6BA, 0x800, 0x983, 0xB50, 0xD74,
2019 0x1000, 0x1307, 0x16A1, 0x1AE9, 0x2000, 0x260E, 0x2D41, 0x35D1,
2020 0x4000, 0x4C1C, 0x5A82, 0x6BA3, 0x8000, 0x9838, 0xB505, 0xD745,
2021 0x10000, 0x13070, 0x16A0A, 0x1AE8A, 0x20000, 0x260E0, 0x2D414, 0x35D14,
2022 0x40000, 0x4C1C0, 0x5A828, 0x6BA28, 0x80000
2023 ];
2024
2025 const BINK2_QUANT_INTRA_LUMA: &[[i32; 64]; 4] = &[
2026 [
2027 0x400, 0x598, 0x5E2, 0x49D, 0x733, 0x7E9, 0x1497, 0x2190,
2028 0x521, 0x685, 0x65E, 0x688, 0xA41, 0xD72, 0x1F57, 0x31FA,
2029 0x434, 0x6DB, 0x710, 0x79E, 0xF8C, 0x130B, 0x226D, 0x2E85,
2030 0x546, 0x74C, 0x84F, 0x7E0, 0x1275, 0x1162, 0x1E20, 0x25A5,
2031 0x99A, 0xC1F, 0x10CF, 0x10CF, 0x1B33, 0x1AB3, 0x2B47, 0x3435,
2032 0xD2F, 0x1648, 0x13BC, 0x17A1, 0x23EE, 0x1C40, 0x29E5, 0x266A,
2033 0x156E, 0x1D61, 0x1E75, 0x1BB3, 0x2B47, 0x2720, 0x34F7, 0x3270,
2034 0x1C6F, 0x1DE1, 0x1B6C, 0x17D1, 0x23E5, 0x2357, 0x3175, 0x35C8
2035 ], [
2036 0x4C2, 0x6A7, 0x6FF, 0x57D, 0x890, 0x968, 0x187C, 0x27EA,
2037 0x619, 0x7C1, 0x792, 0x7C4, 0xC32, 0xFFD, 0x2545, 0x3B6F,
2038 0x4FF, 0x827, 0x866, 0x90F, 0x127D, 0x16A6, 0x28F1, 0x3752,
2039 0x646, 0x8AE, 0x9E2, 0x95E, 0x15F3, 0x14AC, 0x23D3, 0x2CC4,
2040 0xB6B, 0xE6A, 0x13FD, 0x13FE, 0x2059, 0x1FC0, 0x3378, 0x3E16,
2041 0xFAE, 0x1A7F, 0x1778, 0x1C1A, 0x2ABA, 0x2198, 0x31D3, 0x2DAE,
2042 0x197C, 0x22F0, 0x2438, 0x20F1, 0x3378, 0x2E87, 0x3EFD, 0x3BFA,
2043 0x21D1, 0x2388, 0x209C, 0x1C52, 0x2AAF, 0x2A07, 0x3AD0, 0x3FF5
2044 ], [
2045 0x5A8, 0x7E9, 0x852, 0x687, 0xA2F, 0xB30, 0x1D1E, 0x2F77,
2046 0x740, 0x938, 0x901, 0x93C, 0xE81, 0x1303, 0x2C52, 0x46AE,
2047 0x5F1, 0x9B2, 0x9FD, 0xAC6, 0x15FC, 0x1AEE, 0x30B0, 0x41C9,
2048 0x775, 0xA52, 0xBC0, 0xB24, 0x1A1B, 0x1895, 0x2A9A, 0x353D,
2049 0xD94, 0x1124, 0x17C5, 0x17C6, 0x2677, 0x25C2, 0x3D34, 0x49D5,
2050 0x12A5, 0x1F82, 0x1BE9, 0x216B, 0x32CF, 0x27F3, 0x3B40, 0x3653,
2051 0x1E4E, 0x298D, 0x2B12, 0x272C, 0x3D34, 0x3755, 0x4AE7, 0x4753,
2052 0x2836, 0x2A41, 0x26C7, 0x21AE, 0x32C3, 0x31FA, 0x45F1, 0x4C0E
2053 ], [
2054 0x6BA, 0x968, 0x9E5, 0x7C3, 0xC1C, 0xD4E, 0x22A0, 0x3872,
2055 0x8A0, 0xAF7, 0xAB5, 0xAFB, 0x113F, 0x169C, 0x34B5, 0x540D,
2056 0x711, 0xB87, 0xBE1, 0xCD0, 0x1A25, 0x2007, 0x39E6, 0x4E3C,
2057 0x8DF, 0xC46, 0xDFA, 0xD3F, 0x1F0B, 0x1D3C, 0x32A9, 0x3F4F,
2058 0x1025, 0x1462, 0x1C44, 0x1C46, 0x2DBF, 0x2CE7, 0x48C9, 0x57CE,
2059 0x162D, 0x2578, 0x2131, 0x27BE, 0x3C6D, 0x2F82, 0x4676, 0x409A,
2060 0x240A, 0x3169, 0x3338, 0x2E96, 0x48C9, 0x41CD, 0x5914, 0x54D3,
2061 0x2FD2, 0x3240, 0x2E1E, 0x280E, 0x3C5E, 0x3B6F, 0x532D, 0x5A73
2062 ]
2063 ];
2064
2065 const BINK2_QUANT_INTRA_CHROMA: &[[i32; 64]; 4] = &[
2066 [
2067 0x400, 0x4A9, 0x59A, 0x89B, 0x1600, 0x1221, 0x171C, 0x19A3,
2068 0x4A9, 0x656, 0x713, 0xE16, 0x19A3, 0x1520, 0x1AEE, 0x1DE1,
2069 0x59A, 0x713, 0xDBB, 0x130B, 0x171C, 0x130B, 0x1847, 0x1AEE,
2070 0x89B, 0xE16, 0x130B, 0xEF0, 0x1221, 0xEF0, 0x130B, 0x1520,
2071 0x1600, 0x19A3, 0x171C, 0x1221, 0x1600, 0x1221, 0x171C, 0x19A3,
2072 0x1221, 0x1520, 0x130B, 0xEF0, 0x1221, 0xEF0, 0x130B, 0x1520,
2073 0x171C, 0x1AEE, 0x1847, 0x130B, 0x171C, 0x130B, 0x1847, 0x1AEE,
2074 0x19A3, 0x1DE1, 0x1AEE, 0x1520, 0x19A3, 0x1520, 0x1AEE, 0x1DE1
2075 ], [
2076 0x4C2, 0x58B, 0x6AA, 0xA3C, 0x1A2A, 0x158F, 0x1B7B, 0x1E7D,
2077 0x58B, 0x789, 0x869, 0x10C0, 0x1E7D, 0x1920, 0x2007, 0x2388,
2078 0x6AA, 0x869, 0x1054, 0x16A6, 0x1B7B, 0x16A6, 0x1CDE, 0x2007,
2079 0xA3C, 0x10C0, 0x16A6, 0x11C4, 0x158F, 0x11C4, 0x16A6, 0x1920,
2080 0x1A2A, 0x1E7D, 0x1B7B, 0x158F, 0x1A2A, 0x158F, 0x1B7B, 0x1E7D,
2081 0x158F, 0x1920, 0x16A6, 0x11C4, 0x158F, 0x11C4, 0x16A6, 0x1920,
2082 0x1B7B, 0x2007, 0x1CDE, 0x16A6, 0x1B7B, 0x16A6, 0x1CDE, 0x2007,
2083 0x1E7D, 0x2388, 0x2007, 0x1920, 0x1E7D, 0x1920, 0x2007, 0x2388
2084 ], [
2085 0x5A8, 0x698, 0x7EC, 0xC2C, 0x1F1D, 0x19A3, 0x20AF, 0x2442,
2086 0x698, 0x8F6, 0xA01, 0x13EB, 0x2442, 0x1DE1, 0x2616, 0x2A41,
2087 0x7EC, 0xA01, 0x136B, 0x1AEE, 0x20AF, 0x1AEE, 0x2255, 0x2616,
2088 0xC2C, 0x13EB, 0x1AEE, 0x1520, 0x19A3, 0x1520, 0x1AEE, 0x1DE1,
2089 0x1F1D, 0x2442, 0x20AF, 0x19A3, 0x1F1D, 0x19A3, 0x20AF, 0x2442,
2090 0x19A3, 0x1DE1, 0x1AEE, 0x1520, 0x19A3, 0x1520, 0x1AEE, 0x1DE1,
2091 0x20AF, 0x2616, 0x2255, 0x1AEE, 0x20AF, 0x1AEE, 0x2255, 0x2616,
2092 0x2442, 0x2A41, 0x2616, 0x1DE1, 0x2442, 0x1DE1, 0x2616, 0x2A41
2093 ], [
2094 0x6BA, 0x7D7, 0x96C, 0xE7A, 0x2500, 0x1E7D, 0x26DE, 0x2B1E,
2095 0x7D7, 0xAA9, 0xBE5, 0x17B0, 0x2B1E, 0x2388, 0x2D4B, 0x3240,
2096 0x96C, 0xBE5, 0x1718, 0x2007, 0x26DE, 0x2007, 0x28D4, 0x2D4B,
2097 0xE7A, 0x17B0, 0x2007, 0x1920, 0x1E7D, 0x1920, 0x2007, 0x2388,
2098 0x2500, 0x2B1E, 0x26DE, 0x1E7D, 0x2500, 0x1E7D, 0x26DE, 0x2B1E,
2099 0x1E7D, 0x2388, 0x2007, 0x1920, 0x1E7D, 0x1920, 0x2007, 0x2388,
2100 0x26DE, 0x2D4B, 0x28D4, 0x2007, 0x26DE, 0x2007, 0x28D4, 0x2D4B,
2101 0x2B1E, 0x3240, 0x2D4B, 0x2388, 0x2B1E, 0x2388, 0x2D4B, 0x3240
2102 ]
2103 ];
2104
2105 const BINK2_QUANT_INTER: &[[i32; 64]; 4] = &[
2106 [
2107 0x400, 0x4A9, 0x434, 0x34C, 0x41C, 0x392, 0x4C9, 0x5D4,
2108 0x4A9, 0x56F, 0x4E6, 0x3D7, 0x4CB, 0x429, 0x5B7, 0x718,
2109 0x434, 0x4E6, 0x489, 0x3A8, 0x4AB, 0x40A, 0x5A4, 0x6CD,
2110 0x34C, 0x3D7, 0x3A8, 0x32B, 0x41F, 0x39F, 0x519, 0x630,
2111 0x41C, 0x4CB, 0x4AB, 0x41F, 0x5AB, 0x538, 0x778, 0x932,
2112 0x392, 0x429, 0x40A, 0x39F, 0x538, 0x521, 0x799, 0x9B6,
2113 0x4C9, 0x5B7, 0x5A4, 0x519, 0x778, 0x799, 0xBE4, 0xFC7,
2114 0x5D4, 0x718, 0x6CD, 0x630, 0x932, 0x9B6, 0xFC7, 0x162F
2115 ], [
2116 0x4C2, 0x58B, 0x4FF, 0x3EB, 0x4E4, 0x43F, 0x5B1, 0x6EE,
2117 0x58B, 0x676, 0x5D3, 0x491, 0x5B3, 0x4F3, 0x6CB, 0x86F,
2118 0x4FF, 0x5D3, 0x565, 0x459, 0x58D, 0x4CE, 0x6B5, 0x816,
2119 0x3EB, 0x491, 0x459, 0x3C5, 0x4E6, 0x44F, 0x610, 0x75C,
2120 0x4E4, 0x5B3, 0x58D, 0x4E6, 0x6BD, 0x635, 0x8E2, 0xAEF,
2121 0x43F, 0x4F3, 0x4CE, 0x44F, 0x635, 0x61A, 0x909, 0xB8C,
2122 0x5B1, 0x6CB, 0x6B5, 0x610, 0x8E2, 0x909, 0xE24, 0x12C3,
2123 0x6EE, 0x86F, 0x816, 0x75C, 0xAEF, 0xB8C, 0x12C3, 0x1A61
2124 ], [
2125 0x5A8, 0x698, 0x5F1, 0x4A9, 0x5D0, 0x50D, 0x6C4, 0x83E,
2126 0x698, 0x7AF, 0x6ED, 0x56F, 0x6C7, 0x5E3, 0x814, 0xA08,
2127 0x5F1, 0x6ED, 0x66A, 0x52B, 0x69A, 0x5B6, 0x7FA, 0x99E,
2128 0x4A9, 0x56F, 0x52B, 0x47B, 0x5D4, 0x51F, 0x735, 0x8C1,
2129 0x5D0, 0x6C7, 0x69A, 0x5D4, 0x804, 0x761, 0xA90, 0xD00,
2130 0x50D, 0x5E3, 0x5B6, 0x51F, 0x761, 0x741, 0xABF, 0xDBB,
2131 0x6C4, 0x814, 0x7FA, 0x735, 0xA90, 0xABF, 0x10D2, 0x1650,
2132 0x83E, 0xA08, 0x99E, 0x8C1, 0xD00, 0xDBB, 0x1650, 0x1F5F
2133 ], [
2134 0x6BA, 0x7D7, 0x711, 0x58B, 0x6EA, 0x601, 0x80C, 0x9CD,
2135 0x7D7, 0x923, 0x83C, 0x676, 0x80F, 0x700, 0x99C, 0xBEE,
2136 0x711, 0x83C, 0x7A1, 0x626, 0x7DA, 0x6CB, 0x97C, 0xB70,
2137 0x58B, 0x676, 0x626, 0x554, 0x6EE, 0x617, 0x893, 0xA68,
2138 0x6EA, 0x80F, 0x7DA, 0x6EE, 0x988, 0x8C7, 0xC90, 0xF76,
2139 0x601, 0x700, 0x6CB, 0x617, 0x8C7, 0x8A1, 0xCC7, 0x1055,
2140 0x80C, 0x99C, 0x97C, 0x893, 0xC90, 0xCC7, 0x1400, 0x1A89,
2141 0x9CD, 0xBEE, 0xB70, 0xA68, 0xF76, 0x1055, 0x1A89, 0x254E
2142 ]
2143 ];
2144
2145 const BINK2_AC_CODES1: &[u8] = &[
2146 0x01, 0x04, 0x00, 0x08, 0x02, 0x32, 0x0A, 0x12,
2147 0x3A, 0x7A, 0xFA, 0x72, 0x06, 0x1A
2148 ];
2149 const BINK2_AC_BITS1: &[u8] = &[ 1, 3, 4, 4, 5, 7, 5, 6, 7, 8, 8, 7, 3, 6 ];
2150 const BINK2_AC_CODES2: &[u16] = &[
2151 0x01, 0x00, 0x04, 0x2C, 0x6C, 0x0C, 0x4C, 0xAC,
2152 0xEC, 0x12C, 0x16C, 0x1AC, 0x02, 0x1C
2153 ];
2154 const BINK2_AC_BITS2: &[u8] = &[ 1, 3, 4, 9, 9, 7, 7, 9, 8, 9, 9, 9, 2, 5 ];
2155
2156 const BINK2_AC_RUNS: [usize; 14] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 64, 0 ];
2157
2158 const BINK2_MV_CODES: &[u8] = &[
2159 0x01, 0x06, 0x0C, 0x1C, 0x18, 0x38, 0x58, 0x78,
2160 0x68, 0x48, 0x28, 0x08, 0x14, 0x04, 0x02, 0x00
2161 ];
2162 const BINK2_MV_BITS: &[u8] = &[
2163 1, 3, 5, 5, 7, 7, 7, 7,
2164 7, 7, 7, 7, 5, 5, 3, 4
2165 ];
2166 const BINK2_MV_ESC: i8 = 42;
2167 const BINK2_MV_SYMS: &[i8] = &[
2168 0, 1, 2, 3, 4, 5, 6, 7,
2169 -7, -6, -5, -4, -3, -2, -1, BINK2_MV_ESC
2170 ];
2171
2172 const BINK2_AC_VAL_CODES1: &[u16] = &[
2173 0x004, 0x01, 0x002, 0x00, 0x08, 0x18, 0xF8, 0x178,
2174 0x138, 0x38, 0x1B8, 0x78, 0xB8
2175 ];
2176 const BINK2_AC_VAL_BITS1: &[u8] = &[ 3, 1, 2, 4, 5, 6, 8, 9, 9, 9, 9, 9, 9 ];
2177 const BINK2_AC_VAL_CODES2: &[u16] = &[
2178 0x0A, 0x001, 0x004, 0x08, 0x06, 0x00, 0x02, 0x1A,
2179 0x2A, 0x16A, 0x1EA, 0x6A, 0xEA
2180 ];
2181 const BINK2_AC_VAL_BITS2: &[u8] = &[ 6, 1, 3, 4, 3, 4, 4, 5, 7, 9, 9, 9, 9 ];
2182 const BINK2_AC_SKIP_CODES1: &[u16] = &[
2183 0x00, 0x001, 0x0D, 0x15, 0x45, 0x85, 0xA5, 0x165,
2184 0x65, 0x1E5, 0xE5, 0x25, 0x03, 0x05
2185 ];
2186 const BINK2_AC_SKIP_BITS1: &[u8] = &[ 1, 3, 4, 5, 7, 8, 8, 9, 9, 9, 9, 8, 2, 8 ];
2187 const BINK2_AC_SKIP_CODES2: &[u16] = &[
2188 0x00, 0x01, 0x003, 0x07, 0x1F, 0x1B, 0x0F, 0x2F,
2189 0x5B, 0xDB, 0x1DB, 0x3B, 0x05, 0x0B
2190 ];
2191 const BINK2_AC_SKIP_BITS2: &[u8] = &[ 1, 3, 4, 4, 5, 7, 6, 6, 8, 9, 9, 6, 3, 5 ];
2192 const BINK2_QUANT_CODES: &[u8] = &[
2193 0x01, 0x02, 0x04, 0x08, 0x10, 0x30, 0x50, 0x70,
2194 0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xE0
2195 ];
2196 const BINK2_QUANT_BITS: &[u8] = &[ 1, 2, 3, 4, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8 ];
2197
2198 const BINK2_OLD_QUANTS: [f32; 16] = [
2199 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
2200 ];
2201 const BINK2_OLD_DC_QUANTS: [i32; 16] = [
2202 4, 4, 4, 4, 4, 6, 7, 8, 10, 12, 16, 24, 32, 48, 64, 128
2203 ];
2204 const BINK2_DC_EXTRA_BITS: [u8; 16] = [ 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6 ];
2205 const BINK2_QMAT_INTRA_LUMA_OLD: &[f32; 64] = &[
2206 0.125, 0.208056, 0.22864901, 0.205778, 0.22499999, 0.235708, 0.33148301, 0.248309,
2207 0.190718, 0.288582, 0.29449099, 0.34658501, 0.38143599, 0.47678301, 0.600528, 0.44008601,
2208 0.16332, 0.31714499, 0.34142101, 0.42249799, 0.604285, 0.70576, 0.68942899, 0.42807001,
2209 0.235175, 0.38735899, 0.46090701, 0.50122303, 0.82311302, 0.73910397, 0.69206202, 0.39741901,
2210 0.30000001, 0.45078799, 0.65328097, 0.74962097, 0.85000002, 0.79551601, 0.69678998, 0.38625899,
2211 0.392847, 0.79009801, 0.73142397, 1.004719, 1.070509, 0.80251199, 0.64313799, 0.27096599,
2212 0.34501299, 0.56299502, 0.60987997, 0.636379, 0.69678998, 0.60061598, 0.43934, 0.19224399,
2213 0.210373, 0.26309499, 0.252336, 0.25142801, 0.265553, 0.24928901, 0.188511, 0.094199002
2214 ];
2215 const BINK2_QMAT_CHROMA_OLD: &[f32; 64] = &[
2216 0.125, 0.17338, 0.217761, 0.383793, 0.6875, 0.54016501, 0.37207201, 0.18968099,
2217 0.17338, 0.28056601, 0.32721299, 0.74753499, 0.95358998, 0.74923098, 0.51607901, 0.26309499,
2218 0.217761, 0.32721299, 0.66387498, 1.056244, 0.89826202, 0.70576, 0.48613599, 0.24783,
2219 0.383793, 0.74753499, 1.056244, 0.95059502, 0.80841398, 0.635167, 0.437511, 0.223041,
2220 0.6875, 0.95358998, 0.89826202, 0.80841398, 0.6875, 0.54016501, 0.37207201, 0.18968099,
2221 0.54016501, 0.74923098, 0.70576, 0.635167, 0.54016501, 0.42440501, 0.292335, 0.149031,
2222 0.37207201, 0.51607901, 0.48613599, 0.437511, 0.37207201, 0.292335, 0.201364, 0.102655,
2223 0.18968099, 0.26309499, 0.24783, 0.223041, 0.18968099, 0.149031, 0.102655, 0.052333001
2224 ];
2225 const BINK2_QMAT_INTER_LUMA_OLD: &[f32; 64] = &[
2226 0.125, 0.17338, 0.16332, 0.146984, 0.128475, 0.106393, 0.077045999, 0.043109,
2227 0.17338, 0.240485, 0.226532, 0.20387299, 0.17820001, 0.147571, 0.109474, 0.062454,
2228 0.16332, 0.226532, 0.219321, 0.202722, 0.181465, 0.149711, 0.112943, 0.062583998,
2229 0.146984, 0.20387299, 0.202722, 0.201647, 0.183731, 0.15397599, 0.11711, 0.065334998,
2230 0.128475, 0.17820001, 0.181465, 0.183731, 0.17708801, 0.155499, 0.120267, 0.068016,
2231 0.106393, 0.147571, 0.149711, 0.15397599, 0.155499, 0.14575601, 0.116636, 0.068494998,
2232 0.077045999, 0.109474, 0.112943, 0.11711, 0.120267, 0.116636, 0.098646, 0.060141001,
2233 0.043109, 0.062454, 0.062583998, 0.065334998, 0.068016, 0.068494998, 0.060141001, 0.038853001
2234 ];