FFmpeg  1.2.12
bink.c
Go to the documentation of this file.
1 /*
2  * Bink video decoder
3  * Copyright (c) 2009 Konstantin Shishkov
4  * Copyright (C) 2011 Peter Ross <pross@xvid.org>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/imgutils.h"
24 #include "libavutil/internal.h"
25 #include "avcodec.h"
26 #include "dsputil.h"
27 #include "binkdata.h"
28 #include "binkdsp.h"
29 #include "internal.h"
30 #include "mathops.h"
31 
32 #define BITSTREAM_READER_LE
33 #include "get_bits.h"
34 
35 #define BINK_FLAG_ALPHA 0x00100000
36 #define BINK_FLAG_GRAY 0x00020000
37 
38 static VLC bink_trees[16];
39 
43 enum OldSources {
54 
56 };
57 
58 static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
59  4, 8, 8, 5, 5, 11, 11, 4, 4, 7
60 };
61 
62 static const int binkb_bundle_signed[BINKB_NB_SRC] = {
63  0, 0, 0, 1, 1, 0, 1, 0, 0, 0
64 };
65 
66 static int32_t binkb_intra_quant[16][64];
67 static int32_t binkb_inter_quant[16][64];
68 
72 enum Sources {
82 
84 };
85 
89 typedef struct Tree {
90  int vlc_num;
91  uint8_t syms[16];
92 } Tree;
93 
94 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
95  bink_trees[(tree).vlc_num].bits, 1)]
96 
100 typedef struct Bundle {
101  int len;
107 } Bundle;
108 
109 /*
110  * Decoder context
111  */
112 typedef struct BinkContext {
117  int version;
120  unsigned frame_num;
121 
125 } BinkContext;
126 
141 };
142 
150 static void init_lengths(BinkContext *c, int width, int bw)
151 {
152  width = FFALIGN(width, 8);
153 
154  c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
155 
156  c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
157 
158  c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
159 
163  c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
164 
165  c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
166 
167  c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
168 }
169 
176 {
177  int bw, bh, blocks;
178  int i;
179 
180  bw = (c->avctx->width + 7) >> 3;
181  bh = (c->avctx->height + 7) >> 3;
182  blocks = bw * bh;
183 
184  for (i = 0; i < BINKB_NB_SRC; i++) {
185  c->bundle[i].data = av_malloc(blocks * 64);
186  if (!c->bundle[i].data)
187  return AVERROR(ENOMEM);
188  c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
189  }
190 
191  return 0;
192 }
193 
200 {
201  int i;
202  for (i = 0; i < BINKB_NB_SRC; i++)
203  av_freep(&c->bundle[i].data);
204 }
205 
214 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
215 {
216  uint8_t *src2 = src + size;
217  int size2 = size;
218 
219  do {
220  if (!get_bits1(gb)) {
221  *dst++ = *src++;
222  size--;
223  } else {
224  *dst++ = *src2++;
225  size2--;
226  }
227  } while (size && size2);
228 
229  while (size--)
230  *dst++ = *src++;
231  while (size2--)
232  *dst++ = *src2++;
233 }
234 
241 static void read_tree(GetBitContext *gb, Tree *tree)
242 {
243  uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
244  int i, t, len;
245 
246  tree->vlc_num = get_bits(gb, 4);
247  if (!tree->vlc_num) {
248  for (i = 0; i < 16; i++)
249  tree->syms[i] = i;
250  return;
251  }
252  if (get_bits1(gb)) {
253  len = get_bits(gb, 3);
254  for (i = 0; i <= len; i++) {
255  tree->syms[i] = get_bits(gb, 4);
256  tmp1[tree->syms[i]] = 1;
257  }
258  for (i = 0; i < 16 && len < 16 - 1; i++)
259  if (!tmp1[i])
260  tree->syms[++len] = i;
261  } else {
262  len = get_bits(gb, 2);
263  for (i = 0; i < 16; i++)
264  in[i] = i;
265  for (i = 0; i <= len; i++) {
266  int size = 1 << i;
267  for (t = 0; t < 16; t += size << 1)
268  merge(gb, out + t, in + t, size);
269  FFSWAP(uint8_t*, in, out);
270  }
271  memcpy(tree->syms, in, 16);
272  }
273 }
274 
282 static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
283 {
284  int i;
285 
286  if (bundle_num == BINK_SRC_COLORS) {
287  for (i = 0; i < 16; i++)
288  read_tree(gb, &c->col_high[i]);
289  c->col_lastval = 0;
290  }
291  if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
292  read_tree(gb, &c->bundle[bundle_num].tree);
293  c->bundle[bundle_num].cur_dec =
294  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
295 }
296 
304 #define CHECK_READ_VAL(gb, b, t) \
305  if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
306  return 0; \
307  t = get_bits(gb, b->len); \
308  if (!t) { \
309  b->cur_dec = NULL; \
310  return 0; \
311  } \
312 
313 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
314 {
315  int t, v;
316  const uint8_t *dec_end;
317 
318  CHECK_READ_VAL(gb, b, t);
319  dec_end = b->cur_dec + t;
320  if (dec_end > b->data_end) {
321  av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
322  return AVERROR_INVALIDDATA;
323  }
324  if (get_bits1(gb)) {
325  v = get_bits(gb, 4);
326  memset(b->cur_dec, v, t);
327  b->cur_dec += t;
328  } else {
329  while (b->cur_dec < dec_end)
330  *b->cur_dec++ = GET_HUFF(gb, b->tree);
331  }
332  return 0;
333 }
334 
336 {
337  int t, sign, v;
338  const uint8_t *dec_end;
339 
340  CHECK_READ_VAL(gb, b, t);
341  dec_end = b->cur_dec + t;
342  if (dec_end > b->data_end) {
343  av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
344  return AVERROR_INVALIDDATA;
345  }
346  if (get_bits1(gb)) {
347  v = get_bits(gb, 4);
348  if (v) {
349  sign = -get_bits1(gb);
350  v = (v ^ sign) - sign;
351  }
352  memset(b->cur_dec, v, t);
353  b->cur_dec += t;
354  } else {
355  while (b->cur_dec < dec_end) {
356  v = GET_HUFF(gb, b->tree);
357  if (v) {
358  sign = -get_bits1(gb);
359  v = (v ^ sign) - sign;
360  }
361  *b->cur_dec++ = v;
362  }
363  }
364  return 0;
365 }
366 
367 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
368 
370 {
371  int t, v;
372  int last = 0;
373  const uint8_t *dec_end;
374 
375  CHECK_READ_VAL(gb, b, t);
376  dec_end = b->cur_dec + t;
377  if (dec_end > b->data_end) {
378  av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
379  return AVERROR_INVALIDDATA;
380  }
381  if (get_bits1(gb)) {
382  v = get_bits(gb, 4);
383  memset(b->cur_dec, v, t);
384  b->cur_dec += t;
385  } else {
386  while (b->cur_dec < dec_end) {
387  v = GET_HUFF(gb, b->tree);
388  if (v < 12) {
389  last = v;
390  *b->cur_dec++ = v;
391  } else {
392  int run = bink_rlelens[v - 12];
393 
394  if (dec_end - b->cur_dec < run)
395  return AVERROR_INVALIDDATA;
396  memset(b->cur_dec, last, run);
397  b->cur_dec += run;
398  }
399  }
400  }
401  return 0;
402 }
403 
405 {
406  int t, v;
407  const uint8_t *dec_end;
408 
409  CHECK_READ_VAL(gb, b, t);
410  dec_end = b->cur_dec + t;
411  if (dec_end > b->data_end) {
412  av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
413  return AVERROR_INVALIDDATA;
414  }
415  while (b->cur_dec < dec_end) {
416  v = GET_HUFF(gb, b->tree);
417  v |= GET_HUFF(gb, b->tree) << 4;
418  *b->cur_dec++ = v;
419  }
420 
421  return 0;
422 }
423 
425 {
426  int t, sign, v;
427  const uint8_t *dec_end;
428 
429  CHECK_READ_VAL(gb, b, t);
430  dec_end = b->cur_dec + t;
431  if (dec_end > b->data_end) {
432  av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
433  return AVERROR_INVALIDDATA;
434  }
435  if (get_bits1(gb)) {
436  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
437  v = GET_HUFF(gb, b->tree);
438  v = (c->col_lastval << 4) | v;
439  if (c->version < 'i') {
440  sign = ((int8_t) v) >> 7;
441  v = ((v & 0x7F) ^ sign) - sign;
442  v += 0x80;
443  }
444  memset(b->cur_dec, v, t);
445  b->cur_dec += t;
446  } else {
447  while (b->cur_dec < dec_end) {
448  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
449  v = GET_HUFF(gb, b->tree);
450  v = (c->col_lastval << 4) | v;
451  if (c->version < 'i') {
452  sign = ((int8_t) v) >> 7;
453  v = ((v & 0x7F) ^ sign) - sign;
454  v += 0x80;
455  }
456  *b->cur_dec++ = v;
457  }
458  }
459  return 0;
460 }
461 
463 #define DC_START_BITS 11
464 
465 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
466  int start_bits, int has_sign)
467 {
468  int i, j, len, len2, bsize, sign, v, v2;
469  int16_t *dst = (int16_t*)b->cur_dec;
470  int16_t *dst_end = (int16_t*)b->data_end;
471 
472  CHECK_READ_VAL(gb, b, len);
473  v = get_bits(gb, start_bits - has_sign);
474  if (v && has_sign) {
475  sign = -get_bits1(gb);
476  v = (v ^ sign) - sign;
477  }
478  if (dst_end - dst < 1)
479  return AVERROR_INVALIDDATA;
480  *dst++ = v;
481  len--;
482  for (i = 0; i < len; i += 8) {
483  len2 = FFMIN(len - i, 8);
484  if (dst_end - dst < len2)
485  return AVERROR_INVALIDDATA;
486  bsize = get_bits(gb, 4);
487  if (bsize) {
488  for (j = 0; j < len2; j++) {
489  v2 = get_bits(gb, bsize);
490  if (v2) {
491  sign = -get_bits1(gb);
492  v2 = (v2 ^ sign) - sign;
493  }
494  v += v2;
495  *dst++ = v;
496  if (v < -32768 || v > 32767) {
497  av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
498  return AVERROR_INVALIDDATA;
499  }
500  }
501  } else {
502  for (j = 0; j < len2; j++)
503  *dst++ = v;
504  }
505  }
506 
507  b->cur_dec = (uint8_t*)dst;
508  return 0;
509 }
510 
517 static inline int get_value(BinkContext *c, int bundle)
518 {
519  int ret;
520 
521  if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
522  return *c->bundle[bundle].cur_ptr++;
523  if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
524  return (int8_t)*c->bundle[bundle].cur_ptr++;
525  ret = *(int16_t*)c->bundle[bundle].cur_ptr;
526  c->bundle[bundle].cur_ptr += 2;
527  return ret;
528 }
529 
530 static void binkb_init_bundle(BinkContext *c, int bundle_num)
531 {
532  c->bundle[bundle_num].cur_dec =
533  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
534  c->bundle[bundle_num].len = 13;
535 }
536 
538 {
539  int i;
540  for (i = 0; i < BINKB_NB_SRC; i++)
541  binkb_init_bundle(c, i);
542 }
543 
544 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
545 {
546  const int bits = binkb_bundle_sizes[bundle_num];
547  const int mask = 1 << (bits - 1);
548  const int issigned = binkb_bundle_signed[bundle_num];
549  Bundle *b = &c->bundle[bundle_num];
550  int i, len;
551 
552  CHECK_READ_VAL(gb, b, len);
553  if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
554  return AVERROR_INVALIDDATA;
555  if (bits <= 8) {
556  if (!issigned) {
557  for (i = 0; i < len; i++)
558  *b->cur_dec++ = get_bits(gb, bits);
559  } else {
560  for (i = 0; i < len; i++)
561  *b->cur_dec++ = get_bits(gb, bits) - mask;
562  }
563  } else {
564  int16_t *dst = (int16_t*)b->cur_dec;
565 
566  if (!issigned) {
567  for (i = 0; i < len; i++)
568  *dst++ = get_bits(gb, bits);
569  } else {
570  for (i = 0; i < len; i++)
571  *dst++ = get_bits(gb, bits) - mask;
572  }
573  b->cur_dec = (uint8_t*)dst;
574  }
575  return 0;
576 }
577 
578 static inline int binkb_get_value(BinkContext *c, int bundle_num)
579 {
580  int16_t ret;
581  const int bits = binkb_bundle_sizes[bundle_num];
582 
583  if (bits <= 8) {
584  int val = *c->bundle[bundle_num].cur_ptr++;
585  return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
586  }
587  ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
588  c->bundle[bundle_num].cur_ptr += 2;
589  return ret;
590 }
591 
601 static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
602  const int32_t quant_matrices[16][64], int q)
603 {
604  int coef_list[128];
605  int mode_list[128];
606  int i, t, bits, ccoef, mode, sign;
607  int list_start = 64, list_end = 64, list_pos;
608  int coef_count = 0;
609  int coef_idx[64];
610  int quant_idx;
611  const int32_t *quant;
612 
613  coef_list[list_end] = 4; mode_list[list_end++] = 0;
614  coef_list[list_end] = 24; mode_list[list_end++] = 0;
615  coef_list[list_end] = 44; mode_list[list_end++] = 0;
616  coef_list[list_end] = 1; mode_list[list_end++] = 3;
617  coef_list[list_end] = 2; mode_list[list_end++] = 3;
618  coef_list[list_end] = 3; mode_list[list_end++] = 3;
619 
620  for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
621  list_pos = list_start;
622  while (list_pos < list_end) {
623  if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
624  list_pos++;
625  continue;
626  }
627  ccoef = coef_list[list_pos];
628  mode = mode_list[list_pos];
629  switch (mode) {
630  case 0:
631  coef_list[list_pos] = ccoef + 4;
632  mode_list[list_pos] = 1;
633  case 2:
634  if (mode == 2) {
635  coef_list[list_pos] = 0;
636  mode_list[list_pos++] = 0;
637  }
638  for (i = 0; i < 4; i++, ccoef++) {
639  if (get_bits1(gb)) {
640  coef_list[--list_start] = ccoef;
641  mode_list[ list_start] = 3;
642  } else {
643  if (!bits) {
644  t = 1 - (get_bits1(gb) << 1);
645  } else {
646  t = get_bits(gb, bits) | 1 << bits;
647  sign = -get_bits1(gb);
648  t = (t ^ sign) - sign;
649  }
650  block[scan[ccoef]] = t;
651  coef_idx[coef_count++] = ccoef;
652  }
653  }
654  break;
655  case 1:
656  mode_list[list_pos] = 2;
657  for (i = 0; i < 3; i++) {
658  ccoef += 4;
659  coef_list[list_end] = ccoef;
660  mode_list[list_end++] = 2;
661  }
662  break;
663  case 3:
664  if (!bits) {
665  t = 1 - (get_bits1(gb) << 1);
666  } else {
667  t = get_bits(gb, bits) | 1 << bits;
668  sign = -get_bits1(gb);
669  t = (t ^ sign) - sign;
670  }
671  block[scan[ccoef]] = t;
672  coef_idx[coef_count++] = ccoef;
673  coef_list[list_pos] = 0;
674  mode_list[list_pos++] = 0;
675  break;
676  }
677  }
678  }
679 
680  if (q == -1) {
681  quant_idx = get_bits(gb, 4);
682  } else {
683  quant_idx = q;
684  if (quant_idx > 15U) {
685  av_log(NULL, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
686  return AVERROR_INVALIDDATA;
687  }
688  }
689 
690  quant = quant_matrices[quant_idx];
691 
692  block[0] = (block[0] * quant[0]) >> 11;
693  for (i = 0; i < coef_count; i++) {
694  int idx = coef_idx[i];
695  block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
696  }
697 
698  return 0;
699 }
700 
709 static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
710 {
711  int coef_list[128];
712  int mode_list[128];
713  int i, sign, mask, ccoef, mode;
714  int list_start = 64, list_end = 64, list_pos;
715  int nz_coeff[64];
716  int nz_coeff_count = 0;
717 
718  coef_list[list_end] = 4; mode_list[list_end++] = 0;
719  coef_list[list_end] = 24; mode_list[list_end++] = 0;
720  coef_list[list_end] = 44; mode_list[list_end++] = 0;
721  coef_list[list_end] = 0; mode_list[list_end++] = 2;
722 
723  for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
724  for (i = 0; i < nz_coeff_count; i++) {
725  if (!get_bits1(gb))
726  continue;
727  if (block[nz_coeff[i]] < 0)
728  block[nz_coeff[i]] -= mask;
729  else
730  block[nz_coeff[i]] += mask;
731  masks_count--;
732  if (masks_count < 0)
733  return 0;
734  }
735  list_pos = list_start;
736  while (list_pos < list_end) {
737  if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
738  list_pos++;
739  continue;
740  }
741  ccoef = coef_list[list_pos];
742  mode = mode_list[list_pos];
743  switch (mode) {
744  case 0:
745  coef_list[list_pos] = ccoef + 4;
746  mode_list[list_pos] = 1;
747  case 2:
748  if (mode == 2) {
749  coef_list[list_pos] = 0;
750  mode_list[list_pos++] = 0;
751  }
752  for (i = 0; i < 4; i++, ccoef++) {
753  if (get_bits1(gb)) {
754  coef_list[--list_start] = ccoef;
755  mode_list[ list_start] = 3;
756  } else {
757  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
758  sign = -get_bits1(gb);
759  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
760  masks_count--;
761  if (masks_count < 0)
762  return 0;
763  }
764  }
765  break;
766  case 1:
767  mode_list[list_pos] = 2;
768  for (i = 0; i < 3; i++) {
769  ccoef += 4;
770  coef_list[list_end] = ccoef;
771  mode_list[list_end++] = 2;
772  }
773  break;
774  case 3:
775  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
776  sign = -get_bits1(gb);
777  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
778  coef_list[list_pos] = 0;
779  mode_list[list_pos++] = 0;
780  masks_count--;
781  if (masks_count < 0)
782  return 0;
783  break;
784  }
785  }
786  }
787 
788  return 0;
789 }
790 
794 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
795 {
796  uint8_t tmp[64];
797  int i;
798  for (i = 0; i < 8; i++)
799  memcpy(tmp + i*8, src + i*stride, 8);
800  for (i = 0; i < 8; i++)
801  memcpy(dst + i*stride, tmp + i*8, 8);
802 }
803 
804 static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
805  int is_key, int is_chroma)
806 {
807  int blk, ret;
808  int i, j, bx, by;
809  uint8_t *dst, *ref, *ref_start, *ref_end;
810  int v, col[2];
811  const uint8_t *scan;
812  int xoff, yoff;
813  LOCAL_ALIGNED_16(int16_t, block, [64]);
814  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
815  int coordmap[64];
816  int ybias = is_key ? -15 : 0;
817  int qp;
818 
819  const int stride = c->pic->linesize[plane_idx];
820  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
821  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
822 
824  ref_start = c->pic->data[plane_idx];
825  ref_end = c->pic->data[plane_idx] + (bh * c->pic->linesize[plane_idx] + bw) * 8;
826 
827  for (i = 0; i < 64; i++)
828  coordmap[i] = (i & 7) + (i >> 3) * stride;
829 
830  for (by = 0; by < bh; by++) {
831  for (i = 0; i < BINKB_NB_SRC; i++) {
832  if ((ret = binkb_read_bundle(c, gb, i)) < 0)
833  return ret;
834  }
835 
836  dst = c->pic->data[plane_idx] + 8*by*stride;
837  for (bx = 0; bx < bw; bx++, dst += 8) {
839  switch (blk) {
840  case 0:
841  break;
842  case 1:
843  scan = bink_patterns[get_bits(gb, 4)];
844  i = 0;
845  do {
846  int mode, run;
847 
848  mode = get_bits1(gb);
849  run = get_bits(gb, binkb_runbits[i]) + 1;
850 
851  i += run;
852  if (i > 64) {
853  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
854  return AVERROR_INVALIDDATA;
855  }
856  if (mode) {
858  for (j = 0; j < run; j++)
859  dst[coordmap[*scan++]] = v;
860  } else {
861  for (j = 0; j < run; j++)
862  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
863  }
864  } while (i < 63);
865  if (i == 63)
866  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
867  break;
868  case 2:
869  memset(dctblock, 0, sizeof(*dctblock) * 64);
870  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
872  read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_intra_quant, qp);
873  c->bdsp.idct_put(dst, stride, dctblock);
874  break;
875  case 3:
876  xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
877  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
878  ref = dst + xoff + yoff * stride;
879  if (ref < ref_start || ref + 8*stride > ref_end) {
880  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
881  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
882  c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
883  } else {
884  put_pixels8x8_overlapped(dst, ref, stride);
885  }
886  c->dsp.clear_block(block);
888  read_residue(gb, block, v);
889  c->dsp.add_pixels8(dst, block, stride);
890  break;
891  case 4:
892  xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
893  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
894  ref = dst + xoff + yoff * stride;
895  if (ref < ref_start || ref + 8 * stride > ref_end) {
896  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
897  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
898  c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
899  } else {
900  put_pixels8x8_overlapped(dst, ref, stride);
901  }
902  memset(dctblock, 0, sizeof(*dctblock) * 64);
903  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
905  read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_inter_quant, qp);
906  c->bdsp.idct_add(dst, stride, dctblock);
907  break;
908  case 5:
910  c->dsp.fill_block_tab[1](dst, v, stride, 8);
911  break;
912  case 6:
913  for (i = 0; i < 2; i++)
914  col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
915  for (i = 0; i < 8; i++) {
917  for (j = 0; j < 8; j++, v >>= 1)
918  dst[i*stride + j] = col[v & 1];
919  }
920  break;
921  case 7:
922  xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
923  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
924  ref = dst + xoff + yoff * stride;
925  if (ref < ref_start || ref + 8 * stride > ref_end) {
926  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
927  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
928  c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
929  } else {
930  put_pixels8x8_overlapped(dst, ref, stride);
931  }
932  break;
933  case 8:
934  for (i = 0; i < 8; i++)
935  memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
936  c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
937  break;
938  default:
939  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
940  return AVERROR_INVALIDDATA;
941  }
942  }
943  }
944  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
945  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
946 
947  return 0;
948 }
949 
950 static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
951  int is_chroma)
952 {
953  int blk, ret;
954  int i, j, bx, by;
955  uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
956  int v, col[2];
957  const uint8_t *scan;
958  int xoff, yoff;
959  LOCAL_ALIGNED_16(int16_t, block, [64]);
960  LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
961  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
962  int coordmap[64];
963 
964  const int stride = c->pic->linesize[plane_idx];
965  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
966  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
967  int width = c->avctx->width >> is_chroma;
968 
969  init_lengths(c, FFMAX(width, 8), bw);
970  for (i = 0; i < BINK_NB_SRC; i++)
971  read_bundle(gb, c, i);
972 
973  ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
974  : c->pic->data[plane_idx];
975  ref_end = ref_start
976  + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
977 
978  for (i = 0; i < 64; i++)
979  coordmap[i] = (i & 7) + (i >> 3) * stride;
980 
981  for (by = 0; by < bh; by++) {
982  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
983  return ret;
984  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
985  return ret;
986  if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
987  return ret;
988  if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
989  return ret;
990  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
991  return ret;
992  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
993  return ret;
994  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
995  return ret;
996  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
997  return ret;
998  if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
999  return ret;
1000 
1001  if (by == bh)
1002  break;
1003  dst = c->pic->data[plane_idx] + 8*by*stride;
1004  prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
1005  : c->pic->data[plane_idx]) + 8*by*stride;
1006  for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1007  blk = get_value(c, BINK_SRC_BLOCK_TYPES);
1008  // 16x16 block type on odd line means part of the already decoded block, so skip it
1009  if ((by & 1) && blk == SCALED_BLOCK) {
1010  bx++;
1011  dst += 8;
1012  prev += 8;
1013  continue;
1014  }
1015  switch (blk) {
1016  case SKIP_BLOCK:
1017  c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1018  break;
1019  case SCALED_BLOCK:
1021  switch (blk) {
1022  case RUN_BLOCK:
1023  scan = bink_patterns[get_bits(gb, 4)];
1024  i = 0;
1025  do {
1026  int run = get_value(c, BINK_SRC_RUN) + 1;
1027 
1028  i += run;
1029  if (i > 64) {
1030  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1031  return AVERROR_INVALIDDATA;
1032  }
1033  if (get_bits1(gb)) {
1034  v = get_value(c, BINK_SRC_COLORS);
1035  for (j = 0; j < run; j++)
1036  ublock[*scan++] = v;
1037  } else {
1038  for (j = 0; j < run; j++)
1039  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1040  }
1041  } while (i < 63);
1042  if (i == 63)
1043  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1044  break;
1045  case INTRA_BLOCK:
1046  memset(dctblock, 0, sizeof(*dctblock) * 64);
1047  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1048  read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1049  c->bdsp.idct_put(ublock, 8, dctblock);
1050  break;
1051  case FILL_BLOCK:
1052  v = get_value(c, BINK_SRC_COLORS);
1053  c->dsp.fill_block_tab[0](dst, v, stride, 16);
1054  break;
1055  case PATTERN_BLOCK:
1056  for (i = 0; i < 2; i++)
1057  col[i] = get_value(c, BINK_SRC_COLORS);
1058  for (j = 0; j < 8; j++) {
1059  v = get_value(c, BINK_SRC_PATTERN);
1060  for (i = 0; i < 8; i++, v >>= 1)
1061  ublock[i + j*8] = col[v & 1];
1062  }
1063  break;
1064  case RAW_BLOCK:
1065  for (j = 0; j < 8; j++)
1066  for (i = 0; i < 8; i++)
1067  ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1068  break;
1069  default:
1070  av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1071  return AVERROR_INVALIDDATA;
1072  }
1073  if (blk != FILL_BLOCK)
1074  c->bdsp.scale_block(ublock, dst, stride);
1075  bx++;
1076  dst += 8;
1077  prev += 8;
1078  break;
1079  case MOTION_BLOCK:
1080  xoff = get_value(c, BINK_SRC_X_OFF);
1081  yoff = get_value(c, BINK_SRC_Y_OFF);
1082  ref = prev + xoff + yoff * stride;
1083  if (ref < ref_start || ref > ref_end) {
1084  av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1085  bx*8 + xoff, by*8 + yoff);
1086  return AVERROR_INVALIDDATA;
1087  }
1088  c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1089  break;
1090  case RUN_BLOCK:
1091  scan = bink_patterns[get_bits(gb, 4)];
1092  i = 0;
1093  do {
1094  int run = get_value(c, BINK_SRC_RUN) + 1;
1095 
1096  i += run;
1097  if (i > 64) {
1098  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1099  return AVERROR_INVALIDDATA;
1100  }
1101  if (get_bits1(gb)) {
1102  v = get_value(c, BINK_SRC_COLORS);
1103  for (j = 0; j < run; j++)
1104  dst[coordmap[*scan++]] = v;
1105  } else {
1106  for (j = 0; j < run; j++)
1107  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1108  }
1109  } while (i < 63);
1110  if (i == 63)
1111  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1112  break;
1113  case RESIDUE_BLOCK:
1114  xoff = get_value(c, BINK_SRC_X_OFF);
1115  yoff = get_value(c, BINK_SRC_Y_OFF);
1116  ref = prev + xoff + yoff * stride;
1117  if (ref < ref_start || ref > ref_end) {
1118  av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1119  bx*8 + xoff, by*8 + yoff);
1120  return AVERROR_INVALIDDATA;
1121  }
1122  c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1123  c->dsp.clear_block(block);
1124  v = get_bits(gb, 7);
1125  read_residue(gb, block, v);
1126  c->dsp.add_pixels8(dst, block, stride);
1127  break;
1128  case INTRA_BLOCK:
1129  memset(dctblock, 0, sizeof(*dctblock) * 64);
1130  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1131  read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1132  c->bdsp.idct_put(dst, stride, dctblock);
1133  break;
1134  case FILL_BLOCK:
1135  v = get_value(c, BINK_SRC_COLORS);
1136  c->dsp.fill_block_tab[1](dst, v, stride, 8);
1137  break;
1138  case INTER_BLOCK:
1139  xoff = get_value(c, BINK_SRC_X_OFF);
1140  yoff = get_value(c, BINK_SRC_Y_OFF);
1141  ref = prev + xoff + yoff * stride;
1142  if (ref < ref_start || ref > ref_end) {
1143  av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1144  bx*8 + xoff, by*8 + yoff);
1145  return -1;
1146  }
1147  c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1148  memset(dctblock, 0, sizeof(*dctblock) * 64);
1149  dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1150  read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
1151  c->bdsp.idct_add(dst, stride, dctblock);
1152  break;
1153  case PATTERN_BLOCK:
1154  for (i = 0; i < 2; i++)
1155  col[i] = get_value(c, BINK_SRC_COLORS);
1156  for (i = 0; i < 8; i++) {
1157  v = get_value(c, BINK_SRC_PATTERN);
1158  for (j = 0; j < 8; j++, v >>= 1)
1159  dst[i*stride + j] = col[v & 1];
1160  }
1161  break;
1162  case RAW_BLOCK:
1163  for (i = 0; i < 8; i++)
1164  memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1165  c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1166  break;
1167  default:
1168  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1169  return AVERROR_INVALIDDATA;
1170  }
1171  }
1172  }
1173  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1174  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1175 
1176  return 0;
1177 }
1178 
1179 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
1180 {
1181  BinkContext * const c = avctx->priv_data;
1182  GetBitContext gb;
1183  int plane, plane_idx, ret;
1184  int bits_count = pkt->size << 3;
1185 
1186  if (c->version > 'b') {
1187  if(c->pic->data[0])
1188  avctx->release_buffer(avctx, c->pic);
1189 
1190  if ((ret = ff_get_buffer(avctx, c->pic)) < 0) {
1191  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1192  return ret;
1193  }
1194  } else {
1195  if ((ret = avctx->reget_buffer(avctx, c->pic)) < 0) {
1196  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1197  return ret;
1198  }
1199  }
1200 
1201  init_get_bits(&gb, pkt->data, bits_count);
1202  if (c->has_alpha) {
1203  if (c->version >= 'i')
1204  skip_bits_long(&gb, 32);
1205  if ((ret = bink_decode_plane(c, &gb, 3, 0)) < 0)
1206  return ret;
1207  }
1208  if (c->version >= 'i')
1209  skip_bits_long(&gb, 32);
1210 
1211  c->frame_num++;
1212 
1213  for (plane = 0; plane < 3; plane++) {
1214  plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1215 
1216  if (c->version > 'b') {
1217  if ((ret = bink_decode_plane(c, &gb, plane_idx, !!plane)) < 0)
1218  return ret;
1219  } else {
1220  if ((ret = binkb_decode_plane(c, &gb, plane_idx,
1221  c->frame_num == 1, !!plane)) < 0)
1222  return ret;
1223  }
1224  if (get_bits_count(&gb) >= bits_count)
1225  break;
1226  }
1227  emms_c();
1228 
1229  *got_frame = 1;
1230  *(AVFrame*)data = *c->pic;
1231 
1232  if (c->version > 'b')
1233  FFSWAP(AVFrame*, c->pic, c->last);
1234 
1235  /* always report that the buffer was completely consumed */
1236  return pkt->size;
1237 }
1238 
1242 static av_cold void binkb_calc_quant(void)
1243 {
1244  uint8_t inv_bink_scan[64];
1245  static const int s[64]={
1246  1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1247  1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1248  1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1249  1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1250  1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1251  843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1252  581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1253  296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1254  };
1255  int i, j;
1256 #define C (1LL<<30)
1257  for (i = 0; i < 64; i++)
1258  inv_bink_scan[bink_scan[i]] = i;
1259 
1260  for (j = 0; j < 16; j++) {
1261  for (i = 0; i < 64; i++) {
1262  int k = inv_bink_scan[i];
1263  binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
1264  binkb_num[j]/(binkb_den[j] * (C>>12));
1265  binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
1266  binkb_num[j]/(binkb_den[j] * (C>>12));
1267  }
1268  }
1269 }
1270 
1272 {
1273  BinkContext * const c = avctx->priv_data;
1274  static VLC_TYPE table[16 * 128][2];
1275  static int binkb_initialised = 0;
1276  int i, ret;
1277  int flags;
1278 
1279  c->version = avctx->codec_tag >> 24;
1280  if (avctx->extradata_size < 4) {
1281  av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1282  return AVERROR_INVALIDDATA;
1283  }
1284  flags = AV_RL32(avctx->extradata);
1285  c->has_alpha = flags & BINK_FLAG_ALPHA;
1286  c->swap_planes = c->version >= 'h';
1287  if (!bink_trees[15].table) {
1288  for (i = 0; i < 16; i++) {
1289  const int maxbits = bink_tree_lens[i][15];
1290  bink_trees[i].table = table + i*128;
1291  bink_trees[i].table_allocated = 1 << maxbits;
1292  init_vlc(&bink_trees[i], maxbits, 16,
1293  bink_tree_lens[i], 1, 1,
1295  }
1296  }
1297  c->avctx = avctx;
1298 
1299  c->pic = avcodec_alloc_frame();
1300  c->last = avcodec_alloc_frame();
1301  if (!c->pic || !c->last) {
1302  avcodec_free_frame(&c->pic);
1303  avcodec_free_frame(&c->last);
1304  return AVERROR(ENOMEM);
1305  }
1306 
1307  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1308  return ret;
1309 
1311 
1312  ff_dsputil_init(&c->dsp, avctx);
1313  ff_binkdsp_init(&c->bdsp);
1314 
1315  if ((ret = init_bundles(c)) < 0) {
1316  free_bundles(c);
1317  return ret;
1318  }
1319 
1320  if (c->version == 'b') {
1321  if (!binkb_initialised) {
1322  binkb_calc_quant();
1323  binkb_initialised = 1;
1324  }
1325  }
1326 
1327  return 0;
1328 }
1329 
1331 {
1332  BinkContext * const c = avctx->priv_data;
1333 
1334  if (c->pic->data[0])
1335  avctx->release_buffer(avctx, c->pic);
1336  if (c->last->data[0])
1337  avctx->release_buffer(avctx, c->last);
1338  avcodec_free_frame(&c->pic);
1339  avcodec_free_frame(&c->last);
1340 
1341  free_bundles(c);
1342  return 0;
1343 }
1344 
1345 static void flush(AVCodecContext *avctx)
1346 {
1347  BinkContext * const c = avctx->priv_data;
1348 
1349  c->frame_num = 0;
1350 }
1351 
1353  .name = "binkvideo",
1354  .type = AVMEDIA_TYPE_VIDEO,
1355  .id = AV_CODEC_ID_BINKVIDEO,
1356  .priv_data_size = sizeof(BinkContext),
1357  .init = decode_init,
1358  .close = decode_end,
1359  .decode = decode_frame,
1360  .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
1361  .flush = flush,
1362  .capabilities = CODEC_CAP_DR1,
1363 };