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};
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)
14 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
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);
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;
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;
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];
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;
73 let t17 = t07 * 1.0823922 - t14 + t16;
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;
86 #[allow(clippy::erasing_op)]
87 fn bink2_idct(coeffs: &mut [i32; 64]) {
88 let mut tmp: [i32; 64] = [0; 64];
90 idct!(coeffs, 1, 8 * i, tmp, 8, i, 0, 0);
93 idct!(tmp, 1, 8 * i, coeffs, 1, 8 * i, 0, 6);
97 #[allow(clippy::erasing_op)]
98 fn bink2_idct_old(coeffs: &mut [f32; 64]) {
99 let mut tmp: [f32; 64] = [0.0; 64];
102 idct!(float; coeffs, 8, i, tmp, 8, i, 0, 0);
105 idct!(float; tmp, 1, 8 * i, coeffs, 1, 8 * i, 0, 6);
111 fn clip8(val: i32) -> u8 {
112 val.min(255).max(0) as u8
116 ($src: expr, $off: expr) => (
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
130 macro_rules! chroma_interp {
131 ($dst: expr, $dstride: expr, $h: expr, $ty: tt, $src: expr, $sstride: expr, $step: expr, $mode: expr, $shift: expr) => {
133 for out in $dst.chunks_mut($dstride).take($h) {
135 let e0 = el!($src, soff + i);
136 let e1 = el!($src, soff + i + $step);
137 out[i] = match $mode {
139 1 => (e0 * 3 + e1 + $shift) >> $shift,
140 2 => (e0 * 2 + e1 * 2 + $shift) >> $shift,
141 _ => (e0 + e1 * 3 + $shift) >> $shift,
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)));
154 #[allow(clippy::erasing_op)]
156 fn calc_dc(src: &[u8], stride: usize) -> i32 {
157 let mut sums = [0u16; 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));
170 for e in sums.iter() {
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]);
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]); }
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]); }
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]);
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]); }
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]); }
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]);
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); }
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); }
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]);
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); }
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); }
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) };
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();
276 let dst = &mut dst[xoff + (yoff & 31) * stride..];
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]; }
286 for out in dst.chunks_mut(stride).take(16) {
288 out[i] = clip8(luma_filter!(ppix, poff + i, 1));
294 for out in dst.chunks_mut(stride).take(16) {
296 out[i] = clip8(luma_filter!(ppix, poff + i, pstride));
302 let mut tmp = [0i16; 21 * 16];
303 for out in tmp.chunks_mut(16) {
305 out[i] = luma_filter!(ppix, poff - 2 * pstride + i, 1) as i16;
309 for (row, out) in dst.chunks_mut(stride).take(16).enumerate() {
311 out[i] = clip8(luma_filter!(tmp, (row + 2) * 16 + i, 16));
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);
324 let add_x = if mx != 0 { 1 } else { 0 };
325 let add_y = if my != 0 { 1 } else { 0 };
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();
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]; }
344 chroma_interp!(dst, stride, 8, u8, &ppix[poff..], pstride, 1, mx, 2);
346 chroma_interp!(dst, stride, 8, u8, &ppix[poff..], pstride, pstride, my, 2);
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);
356 fn mid_pred<T:PartialOrd>(a: T, b: T, c: T) -> T {
361 if a < c { c } else { a }
365 if a < c { a } else { c }
381 fn resize(&mut self, bw: usize) {
382 self.qstride = bw * 2;
383 self.quants.resize(self.qstride * 2, 0);
385 self.pqpos = self.qstride;
387 fn update_line(&mut self) {
388 mem::swap(&mut self.qpos, &mut self.pqpos);
390 fn pred_quant(&self, pos: usize, is_top: bool) -> u8 {
391 let is_left = pos < 2;
393 if is_left { 16 } else { self.quants[self.qpos + pos - 2] }
396 self.quants[self.pqpos + pos]
398 mid_pred(self.quants[self.pqpos + pos - 2],
399 self.quants[self.pqpos + pos],
400 self.quants[self.qpos + pos - 2])
404 fn set_quant(&mut self, pos: usize, q: u8) {
405 self.quants[self.qpos + pos] = q;
420 fn resize(&mut self, bw: usize) {
421 self.stride = bw * 4 + 4;
423 self.prev_dcs.resize(self.stride, 0);
424 self.new_dcs.resize(self.stride, 0);
426 fn update_line(&mut self) {
427 mem::swap(&mut self.prev_dcs, &mut self.new_dcs);
429 fn set_dcs(&mut self, bx: usize, dc5: i32, dc7: i32, dc13: i32, dc10: i32, dc11: i32, dc14: i32, dc15: i32) {
434 let new_dc = &mut self.new_dcs[bx * 4 + 4..];
453 fn resize(&mut self, bw: usize) {
454 self.stride = bw * 2 + 2;
456 self.prev_dcs.resize(self.stride, 0);
457 self.new_dcs.resize(self.stride, 0);
459 fn update_line(&mut self) {
460 mem::swap(&mut self.prev_dcs, &mut self.new_dcs);
462 fn set_dcs(&mut self, bx: usize, dc1: i32, dc2: i32, dc3: i32) {
466 let new_dc = &mut self.new_dcs[bx * 2 + 2..];
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);
478 fn dc_pred2(a: i32, b: i32) -> i32 {
479 (a.max(b)).min(a.min(b).max(a * 2 - b))
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))
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)
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;
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
598 let new_dc = &mut self.new_dcs[bx * 4 + 4..];
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);
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);
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);
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);
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)
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;
658 let a0; let a1; let a2; let a3;
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);
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);
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);
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);
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);
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);
685 let new_dc = &mut self.new_dcs[bx * 2 + 2..];
689 fn predict_inter(&mut self, min_dc: i32, max_dc: i32) {
690 let dst = &mut self.dcs;
691 let dcs = &self.dc_buf;
693 let a0; let a1; let a2; let a3;
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);
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() {
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()?;
718 *dc = (*dc * quant + 0x1FF) >> 10;
736 fn resize(&mut self, bw: usize) {
737 self.stride = bw * 4;
739 self.prev_off = self.stride;
740 self.mvs.resize(self.stride * 2, ZERO_MV);
742 fn update_line(&mut self) {
743 mem::swap(&mut self.mv_off, &mut self.prev_off);
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;
754 let raw_val = br.read_cb(mv_cb)?;
755 if raw_val != BINK2_MV_ESC {
756 val = raw_val as i16;
758 let len = br.read_code(UintCodeType::LimitedUnary(12, 1))? as u8;
760 let uval = (br.read(nlen)? as i32) + (1 << nlen) - 1;
762 val = (-(uval >> 1) - 1) as i16;
764 val = (uval >> 1) as i16;
768 mv[mv_pos + i].x = val;
770 mv[mv_pos + i].y = val;
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;
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;
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;
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;
816 let mut mv_pred = mv[mv_pos + 0];
819 mv_pred += MV::pred(mv[mv_pos - 4], mv[mv_pos - 3], mv[mv_pos - 1]);
823 mv_pred += MV::pred(mv[ref_pos - 1], mv[ref_pos + 2], mv[mv_pos - 3]);
825 mv_pred += MV::pred(mv[ref_pos + 0], mv[ref_pos + 2], mv[ref_pos + 3]);
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;
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;
843 let mut bits = br.read(3)? as u8;
845 bits += br.read(2)? as u8;
847 let mut mv_c: [i16; 4] = [0; 4];
850 mv_c[i] = br.read(bits)? as i16;
853 if (mv_c[i] != 0) && br.read_bool()? {
858 if is_top && is_left {
859 let mut val = br.read(5)? as i16;
860 if (val != 0) && br.read_bool()? {
871 mv[mv_pos + i].x = mv_c[i];
875 mv[mv_pos + i].y = mv_c[i];
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;
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;
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;
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;
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;
928 mv[mv_pos + i] = ZERO_MV;
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]);
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]);
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]);
946 mv[mv_pos + 2] = MV::pred(mv[mv_pos - 3], mv[mv_pos - 1], mv[mv_pos + 0]);
948 mv[mv_pos + 2] = MV::pred(mv[ref_pos + 2], mv[mv_pos + 1], mv[mv_pos + 0]);
950 mv[mv_pos + 3] = MV::pred(mv[mv_pos + 0], mv[mv_pos + 1], mv[mv_pos + 2]);
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;
966 ac_cb1: Codebook<u8>,
967 ac_cb2: Codebook<u8>,
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>,
976 fn map_ac(idx: usize) -> u8 { idx as u8 }
977 fn map_mv(idx: usize) -> i8 { BINK2_MV_SYMS[idx] }
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 }
1002 struct Bink2Decoder {
1003 info: NACodecInfoRef,
1008 slice_h: [usize; 8],
1024 #[allow(clippy::erasing_op)]
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;
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)?;
1047 let mut do_alpha = self.has_alpha;
1048 if (frame_flags & 0x80000) != 0 && self.has_alpha {
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() {
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)?;
1063 if (frame_flags & 0x4000) == 0 {
1064 let len = (width + 15) >> 4;
1065 decode_flags(br, &mut col_flags, 1, len * 2 - 1)?;
1068 row_flags.resize(bheight * 4, false);
1069 col_flags.resize(bw * 4, false);
1070 //store frame_flags * 8 & 0x7F8
1072 let mut start_by = 0;
1073 for slice_no in 0..self.num_slices {
1074 let end_by = self.slice_h[slice_no];
1076 br.seek(offsets[slice_no - 1] * 8)?;
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;
1083 let mut row_state = frame_flags & 0x2E000;
1085 row_state |= 0x10000;
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 {
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;
1110 let rflags = (row_flags[by] as u32) * 4;
1111 row_state = (row_state & 0x3FFFFFF) | ((row_state >> 4) & 0xC000000) | (rflags << 28);
1115 // row_state |= 0x8;
1117 if by + 2 >= end_by {
1121 let mut btype_lru: [u8; 4] = [ 2, 3, 1, 0 ];
1122 let mut edge_state = 0;
1123 let is_top = by == start_by;
1125 let mut blk_state = row_state | (edge_state & 0x3FC0000);
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;
1140 //let is_top = (edge_state & 0x88) != 0;
1141 let is_left = bx == 0; //(edge_state & 0x20) != 0;
1147 if (blk_state & 0x2000) != 0 {
1148 let val = br.read_code(UintCodeType::LimitedUnary(3, 1))?;
1151 btype = btype_lru[0];
1154 btype = btype_lru[1];
1155 btype_lru[1] = btype_lru[0];
1156 btype_lru[0] = btype;
1159 btype = btype_lru[3];
1160 btype_lru[3] = btype_lru[2];
1161 btype_lru[2] = btype;
1164 btype = btype_lru[2];
1165 btype_lru[2] = btype_lru[1];
1166 btype_lru[1] = btype;
1168 _ => unreachable!(),
1171 btype = br.read(2)? as u8;
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);
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)?;
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]);
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);
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)?;
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]);
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);
1229 self.mvs.predict_mvs(bx, is_top, is_left, (frame_flags & 0x2000) != 0);
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)?;
1241 Bink2DSP::mc_luma(&mut dst[off_a..], stride_a, ref_pic, xoff, yoff, ZERO_MV, 3)?;
1245 return Err(DecoderError::MissingReference);
1250 if (frame_flags & 0x2000) != 0 {
1251 self.mvs.decode_mv_new(br, &self.codes.mv_cb, bx, is_top, is_left)?;
1253 self.mvs.decode_mv_old(br, bx, is_top, is_left)?;
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)?;
1264 Bink2DSP::mc_luma(&mut dst[off_a..], stride_a, ref_pic, xoff, yoff, mv, 3)?;
1268 return Err(DecoderError::MissingReference);
1272 if (frame_flags & 0x2000) != 0 {
1273 self.mvs.decode_mv_new(br, &self.codes.mv_cb, bx, is_top, is_left)?;
1275 self.mvs.decode_mv_old(br, bx, is_top, is_left)?;
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)?;
1286 Bink2DSP::mc_luma(&mut dst[off_a..], stride_a, ref_pic, xoff, yoff, mv, 3)?;
1290 return Err(DecoderError::MissingReference);
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)?;
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]);
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);
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)?;
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]);
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);
1343 _ => unreachable!(),
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);
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);
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);
1390 start_by = self.slice_h[slice_no];
1396 fn decode_flags(br: &mut BitReader, dst: &mut Vec<bool>, start: usize, nbits: usize) -> DecoderResult<()> {
1400 if !br.read_bool()? {
1402 let bit = br.read_bool()?;
1406 let mut cur_bits = nbits;
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()? };
1414 let len = cur_bits.min(4);
1416 let bit = br.read_bool()?;
1425 } else if cur_bits < 16 {
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;
1435 mode = if add_run == (1 << bread) - 1 { 1 } else { 3 };
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;
1453 code += br.read(1)? as u8;
1454 } else if code == 4 {
1455 code = (br.read(5)? as u8) + 5;
1458 if !br.read_bool()? {
1459 ret = prev_q.checked_add(code);
1461 ret = prev_q.checked_sub(code);
1463 validate!(ret.is_some());
1464 let val = ret.unwrap();
1465 validate!(val < (BINK2_QUANT_DC.len() as u8));
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;
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; }
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; }
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;
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; }
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; }
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) };
1521 if br.read_bool()? {
1523 } else if min_count >= 4 {
1528 if !br.read_bool()? {
1529 let cbp4 = br.read(4)?;
1530 cbp |= cbp4 << (i * 4);
1535 if ((edge_state & 0x40000) == 0) || (cbp != 0) {
1536 if br.read_bool()? {
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])
1546 let ncbp = br.read(4)?;
1547 if br.read_bool()? {
1548 Ok(ncbp | (ncbp << 16))
1555 trait ReadBink2Code {
1556 fn read_bink2_code_zero(&mut self) -> DecoderResult<i32>;
1557 fn read_bink2_code_nz(&mut self) -> DecoderResult<i32>;
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;
1566 let add_bits = self.read((pfx as u8) - 3)? as i32;
1567 val = (1 << (pfx - 3)) + add_bits + 2;
1576 fn read_bink2_code_nz(&mut self) -> DecoderResult<i32> {
1577 let pfx = self.read_code(UintCodeType::LimitedUnary(12, 0))? + 1;
1580 let add_bits = self.read((pfx as u8) - 3)? as i32;
1581 val = (1 << (pfx - 3)) + add_bits + 2;
1585 if self.read_bool()? {
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];
1597 for blk_no in 0..4 {
1598 if ((cbp >> blk_no) & 1) == 0 { continue; }
1599 let mut esc_len = 0;
1605 let sym = br.read_cb(cb)?;
1608 skip = br.read(6)? as usize;
1610 skip = BINK2_AC_RUNS[sym as usize];
1613 if idx > 63 { break; }
1614 esc_len = if sym == 13 { 6 } else { 0 };
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;
1621 // set blk info to (dc & 0x1FFF) | (intra ? 0x8000 : 0) | (((ncoded == 0) + (ncoded < 4) + (ncoded < 8)) << 13)
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)?;
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;
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; }
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)?;
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; }
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)?;
1661 decode_dcs_old(br, &mut dcinfo.dc_buf, q, false)?;
1662 dcinfo.predict_inter(-1023, 1023);
1663 let dcs = &dcinfo.dcs;
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; }
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)?;
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; }
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)?;
1689 if br.read_bool()? {
1690 res = prev_quant.checked_sub(diff);
1692 res = prev_quant.checked_add(diff);
1694 validate!(res.is_some());
1695 let q = res.unwrap();
1701 fn decode_cbp_luma_old(br: &mut BitReader, prev_cbp: u32) -> DecoderResult<u32> {
1703 if br.read_bool()? {
1704 if br.read_bool()? {
1705 return Ok(prev_cbp);
1707 low = prev_cbp & 0xFFFF;
1709 let mut nib1 = (prev_cbp >> 4) & 0xF;
1710 if !br.read_bool()? {
1713 let mut new_cbp = nib1;
1714 if !br.read_bool()? {
1717 new_cbp |= nib1 << 4;
1718 if !br.read_bool()? {
1721 new_cbp |= nib1 << 8;
1722 if !br.read_bool()? {
1725 low = new_cbp | (nib1 << 12);
1727 let mut nib_hi = (prev_cbp >> 20) & 0xF;
1730 let nib = (low >> (4 * i)) & 0xF;
1731 if nib.count_ones() == 0 {
1733 } else if (nib.count_ones() == 1) || !br.read_bool()? {
1736 if (((nib >> bit) & 1) != 0) && br.read_bool()? {
1742 high = (high >> 4) | (nib_hi << 28);
1747 fn decode_cbp_chroma_old(br: &mut BitReader, prev_cbp: u32) -> DecoderResult<u32> {
1749 if !br.read_bool()? {
1752 if br.read_bool()? {
1753 return Ok(prev_cbp);
1755 low = prev_cbp & 0xF;
1758 if low.count_ones() == 0 {
1761 if low.count_ones() != 1 {
1762 high = (prev_cbp >> 16) & low;
1763 if br.read_bool()? {
1764 return Ok((high << 16) | low);
1769 if (((low >> bit) & 1) != 0) && br.read_bool()? {
1773 Ok((high << 16) | low)
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;
1779 bits += br.read(2)? as u8;
1781 let scale = BINK2_OLD_DC_QUANTS[q as usize];
1783 for el in dcs.iter_mut() { *el = 0; }
1785 for dcb in dcs.chunks_mut(4) {
1786 for dc in dcb.iter_mut() {
1787 *dc = br.read(bits)? as i32;
1789 for dc in dcb.iter_mut() {
1790 if *dc == 0 { continue; }
1792 if br.read_bool()? {
1799 let add_bits = BINK2_DC_EXTRA_BITS[q as usize] + bits;
1801 let pfx = br.read(10 - add_bits)? as i32;
1804 if br.read_bool()? {
1805 base_dc = -(pfx << bits);
1807 base_dc = pfx << bits;
1812 dcs[0] += base_dc * scale;
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;
1827 let val = br.read_cb(val_cb)?;
1831 let add_bits = br.read(val - 3)? as i32;
1832 level = (1 << (val - 3)) + add_bits + 2;
1836 if br.read_bool()? {
1839 let pos = scan[idx];
1840 dst[blk_no][pos] = (level as f32) * quant_mat[(pos & 7) * 8 + (pos >> 3)] * quant;
1843 if idx >= 64 { break; }
1847 let sym = br.read_cb(skip_cb)?;
1850 skip = br.read(6)? as usize;
1852 skip = BINK2_AC_RUNS[sym as usize];
1855 if idx > 63 { break; }
1856 esc_len = if sym == 13 { 6 } else { 0 };
1863 const KB2H_NUM_SLICES: [usize; 4] = [ 2, 3, 4, 8 ];
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();
1871 let edata = info.get_extradata().unwrap();
1872 validate!(edata.len() >= 8);
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()?;
1879 self.version = magic;
1880 self.has_alpha = (flags & 0x100000) != 0;
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") {
1888 self.num_slices = 1;
1890 self.num_slices = 2;
1891 self.slice_h[0] = (h + 31) >> 6;
1894 self.num_slices = KB2H_NUM_SLICES[(flags & 3) as usize];
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;
1900 self.slice_h[i] = start >> 5;
1903 self.slice_h[self.num_slices - 1] = height_a >> 5;
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)),
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();
1919 Err(DecoderError::InvalidData)
1922 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
1923 let src = pkt.get_buffer();
1925 let mut br = BitReader::new(&src, BitReaderMode::LE);
1928 self.key_frame = pkt.is_keyframe();
1930 let bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 5)?;
1931 buf = bufinfo.get_vbuf().unwrap();
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());
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 });
1941 fn flush(&mut self) {
1946 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
1947 Box::new(Bink2Decoder::new())
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;
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);
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);
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
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
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
2003 const BINK2_CHROMA_CBPS: [u32; 16] = [
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
2018 const BINK2_QUANT_INTRA_LUMA: &[[i32; 64]; 4] = &[
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
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
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
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
2058 const BINK2_QUANT_INTRA_CHROMA: &[[i32; 64]; 4] = &[
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
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
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
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
2098 const BINK2_QUANT_INTER: &[[i32; 64]; 4] = &[
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
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
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
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
2138 const BINK2_AC_CODES1: &[u8] = &[
2139 0x01, 0x04, 0x00, 0x08, 0x02, 0x32, 0x0A, 0x12,
2140 0x3A, 0x7A, 0xFA, 0x72, 0x06, 0x1A
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
2147 const BINK2_AC_BITS2: &[u8] = &[ 1, 3, 4, 9, 9, 7, 7, 9, 8, 9, 9, 9, 2, 5 ];
2149 const BINK2_AC_RUNS: [usize; 14] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 64, 0 ];
2151 const BINK2_MV_CODES: &[u8] = &[
2152 0x01, 0x06, 0x0C, 0x1C, 0x18, 0x38, 0x58, 0x78,
2153 0x68, 0x48, 0x28, 0x08, 0x14, 0x04, 0x02, 0x00
2155 const BINK2_MV_BITS: &[u8] = &[
2156 1, 3, 5, 5, 7, 7, 7, 7,
2157 7, 7, 7, 7, 5, 5, 3, 4
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
2165 const BINK2_AC_VAL_CODES1: &[u16] = &[
2166 0x004, 0x01, 0x002, 0x00, 0x08, 0x18, 0xF8, 0x178,
2167 0x138, 0x38, 0x1B8, 0x78, 0xB8
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
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
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
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
2189 const BINK2_QUANT_BITS: &[u8] = &[ 1, 2, 3, 4, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8 ];
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
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
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
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
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