FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/attributes.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/internal.h"
26 
27 #define BITSTREAM_READER_LE
28 #include "avcodec.h"
29 #include "binkdata.h"
30 #include "binkdsp.h"
31 #include "blockdsp.h"
32 #include "get_bits.h"
33 #include "hpeldsp.h"
34 #include "internal.h"
35 #include "mathops.h"
36 
37 #define BINK_FLAG_ALPHA 0x00100000
38 #define BINK_FLAG_GRAY 0x00020000
39 
40 static VLC bink_trees[16];
41 
42 /**
43  * IDs for different data types used in old version of Bink video codec
44  */
45 enum OldSources {
46  BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
47  BINKB_SRC_COLORS, ///< pixel values used for different block types
48  BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
49  BINKB_SRC_X_OFF, ///< X components of motion value
50  BINKB_SRC_Y_OFF, ///< Y components of motion value
51  BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
52  BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
53  BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
54  BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
55  BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
56 
58 };
59 
60 static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
61  4, 8, 8, 5, 5, 11, 11, 4, 4, 7
62 };
63 
64 static const int binkb_bundle_signed[BINKB_NB_SRC] = {
65  0, 0, 0, 1, 1, 0, 1, 0, 0, 0
66 };
67 
68 static int32_t binkb_intra_quant[16][64];
69 static int32_t binkb_inter_quant[16][64];
70 
71 /**
72  * IDs for different data types used in Bink video codec
73  */
74 enum Sources {
75  BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
76  BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
77  BINK_SRC_COLORS, ///< pixel values used for different block types
78  BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
79  BINK_SRC_X_OFF, ///< X components of motion value
80  BINK_SRC_Y_OFF, ///< Y components of motion value
81  BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
82  BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
83  BINK_SRC_RUN, ///< run lengths for special fill block
84 
86 };
87 
88 /**
89  * data needed to decode 4-bit Huffman-coded value
90  */
91 typedef struct Tree {
92  int vlc_num; ///< tree number (in bink_trees[])
93  uint8_t syms[16]; ///< leaf value to symbol mapping
94 } Tree;
95 
96 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
97  bink_trees[(tree).vlc_num].bits, 1)]
98 
99 /**
100  * data structure used for decoding single Bink data type
101  */
102 typedef struct Bundle {
103  int len; ///< length of number of entries to decode (in bits)
104  Tree tree; ///< Huffman tree-related data
105  uint8_t *data; ///< buffer for decoded symbols
106  uint8_t *data_end; ///< buffer end
107  uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
108  uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
109 } Bundle;
110 
111 /*
112  * Decoder context
113  */
114 typedef struct BinkContext {
120  int version; ///< internal Bink file version
123  unsigned frame_num;
124 
125  Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
126  Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
127  int col_lastval; ///< value of last decoded high nibble in "colours" data type
128 } BinkContext;
129 
130 /**
131  * Bink video block types
132  */
134  SKIP_BLOCK = 0, ///< skipped block
135  SCALED_BLOCK, ///< block has size 16x16
136  MOTION_BLOCK, ///< block is copied from previous frame with some offset
137  RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
138  RESIDUE_BLOCK, ///< motion block with some difference added
139  INTRA_BLOCK, ///< intra DCT block
140  FILL_BLOCK, ///< block is filled with single colour
141  INTER_BLOCK, ///< motion block with DCT applied to the difference
142  PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
143  RAW_BLOCK, ///< uncoded 8x8 block
144 };
145 
146 /**
147  * Initialize length in all bundles.
148  *
149  * @param c decoder context
150  * @param width plane width
151  * @param bw plane width in 8x8 blocks
152  */
153 static void init_lengths(BinkContext *c, int width, int bw)
154 {
155  width = FFALIGN(width, 8);
156 
157  c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
158 
159  c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
160 
161  c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
162 
166  c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
167 
168  c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
169 
170  c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
171 }
172 
173 /**
174  * Allocate memory for bundles.
175  *
176  * @param c decoder context
177  */
179 {
180  int bw, bh, blocks;
181  int i;
182 
183  bw = (c->avctx->width + 7) >> 3;
184  bh = (c->avctx->height + 7) >> 3;
185  blocks = bw * bh;
186 
187  for (i = 0; i < BINKB_NB_SRC; i++) {
188  c->bundle[i].data = av_mallocz(blocks * 64);
189  if (!c->bundle[i].data)
190  return AVERROR(ENOMEM);
191  c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
192  }
193 
194  return 0;
195 }
196 
197 /**
198  * Free memory used by bundles.
199  *
200  * @param c decoder context
201  */
203 {
204  int i;
205  for (i = 0; i < BINKB_NB_SRC; i++)
206  av_freep(&c->bundle[i].data);
207 }
208 
209 /**
210  * Merge two consequent lists of equal size depending on bits read.
211  *
212  * @param gb context for reading bits
213  * @param dst buffer where merged list will be written to
214  * @param src pointer to the head of the first list (the second lists starts at src+size)
215  * @param size input lists size
216  */
217 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
218 {
219  uint8_t *src2 = src + size;
220  int size2 = size;
221 
222  do {
223  if (!get_bits1(gb)) {
224  *dst++ = *src++;
225  size--;
226  } else {
227  *dst++ = *src2++;
228  size2--;
229  }
230  } while (size && size2);
231 
232  while (size--)
233  *dst++ = *src++;
234  while (size2--)
235  *dst++ = *src2++;
236 }
237 
238 /**
239  * Read information about Huffman tree used to decode data.
240  *
241  * @param gb context for reading bits
242  * @param tree pointer for storing tree data
243  */
244 static void read_tree(GetBitContext *gb, Tree *tree)
245 {
246  uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
247  int i, t, len;
248 
249  tree->vlc_num = get_bits(gb, 4);
250  if (!tree->vlc_num) {
251  for (i = 0; i < 16; i++)
252  tree->syms[i] = i;
253  return;
254  }
255  if (get_bits1(gb)) {
256  len = get_bits(gb, 3);
257  for (i = 0; i <= len; i++) {
258  tree->syms[i] = get_bits(gb, 4);
259  tmp1[tree->syms[i]] = 1;
260  }
261  for (i = 0; i < 16 && len < 16 - 1; i++)
262  if (!tmp1[i])
263  tree->syms[++len] = i;
264  } else {
265  len = get_bits(gb, 2);
266  for (i = 0; i < 16; i++)
267  in[i] = i;
268  for (i = 0; i <= len; i++) {
269  int size = 1 << i;
270  for (t = 0; t < 16; t += size << 1)
271  merge(gb, out + t, in + t, size);
272  FFSWAP(uint8_t*, in, out);
273  }
274  memcpy(tree->syms, in, 16);
275  }
276 }
277 
278 /**
279  * Prepare bundle for decoding data.
280  *
281  * @param gb context for reading bits
282  * @param c decoder context
283  * @param bundle_num number of the bundle to initialize
284  */
285 static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
286 {
287  int i;
288 
289  if (bundle_num == BINK_SRC_COLORS) {
290  for (i = 0; i < 16; i++)
291  read_tree(gb, &c->col_high[i]);
292  c->col_lastval = 0;
293  }
294  if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
295  read_tree(gb, &c->bundle[bundle_num].tree);
296  c->bundle[bundle_num].cur_dec =
297  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
298 }
299 
300 /**
301  * common check before starting decoding bundle data
302  *
303  * @param gb context for reading bits
304  * @param b bundle
305  * @param t variable where number of elements to decode will be stored
306  */
307 #define CHECK_READ_VAL(gb, b, t) \
308  if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
309  return 0; \
310  t = get_bits(gb, b->len); \
311  if (!t) { \
312  b->cur_dec = NULL; \
313  return 0; \
314  } \
315 
316 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
317 {
318  int t, v;
319  const uint8_t *dec_end;
320 
321  CHECK_READ_VAL(gb, b, t);
322  dec_end = b->cur_dec + t;
323  if (dec_end > b->data_end) {
324  av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
325  return AVERROR_INVALIDDATA;
326  }
327  if (get_bits1(gb)) {
328  v = get_bits(gb, 4);
329  memset(b->cur_dec, v, t);
330  b->cur_dec += t;
331  } else {
332  while (b->cur_dec < dec_end)
333  *b->cur_dec++ = GET_HUFF(gb, b->tree);
334  }
335  return 0;
336 }
337 
339 {
340  int t, sign, v;
341  const uint8_t *dec_end;
342 
343  CHECK_READ_VAL(gb, b, t);
344  dec_end = b->cur_dec + t;
345  if (dec_end > b->data_end) {
346  av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
347  return AVERROR_INVALIDDATA;
348  }
349  if (get_bits1(gb)) {
350  v = get_bits(gb, 4);
351  if (v) {
352  sign = -get_bits1(gb);
353  v = (v ^ sign) - sign;
354  }
355  memset(b->cur_dec, v, t);
356  b->cur_dec += t;
357  } else {
358  while (b->cur_dec < dec_end) {
359  v = GET_HUFF(gb, b->tree);
360  if (v) {
361  sign = -get_bits1(gb);
362  v = (v ^ sign) - sign;
363  }
364  *b->cur_dec++ = v;
365  }
366  }
367  return 0;
368 }
369 
370 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
371 
373 {
374  int t, v;
375  int last = 0;
376  const uint8_t *dec_end;
377 
378  CHECK_READ_VAL(gb, b, t);
379  dec_end = b->cur_dec + t;
380  if (dec_end > b->data_end) {
381  av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
382  return AVERROR_INVALIDDATA;
383  }
384  if (get_bits1(gb)) {
385  v = get_bits(gb, 4);
386  memset(b->cur_dec, v, t);
387  b->cur_dec += t;
388  } else {
389  while (b->cur_dec < dec_end) {
390  v = GET_HUFF(gb, b->tree);
391  if (v < 12) {
392  last = v;
393  *b->cur_dec++ = v;
394  } else {
395  int run = bink_rlelens[v - 12];
396 
397  if (dec_end - b->cur_dec < run)
398  return AVERROR_INVALIDDATA;
399  memset(b->cur_dec, last, run);
400  b->cur_dec += run;
401  }
402  }
403  }
404  return 0;
405 }
406 
408 {
409  int t, v;
410  const uint8_t *dec_end;
411 
412  CHECK_READ_VAL(gb, b, t);
413  dec_end = b->cur_dec + t;
414  if (dec_end > b->data_end) {
415  av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
416  return AVERROR_INVALIDDATA;
417  }
418  while (b->cur_dec < dec_end) {
419  v = GET_HUFF(gb, b->tree);
420  v |= GET_HUFF(gb, b->tree) << 4;
421  *b->cur_dec++ = v;
422  }
423 
424  return 0;
425 }
426 
428 {
429  int t, sign, v;
430  const uint8_t *dec_end;
431 
432  CHECK_READ_VAL(gb, b, t);
433  dec_end = b->cur_dec + t;
434  if (dec_end > b->data_end) {
435  av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
436  return AVERROR_INVALIDDATA;
437  }
438  if (get_bits1(gb)) {
439  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
440  v = GET_HUFF(gb, b->tree);
441  v = (c->col_lastval << 4) | v;
442  if (c->version < 'i') {
443  sign = ((int8_t) v) >> 7;
444  v = ((v & 0x7F) ^ sign) - sign;
445  v += 0x80;
446  }
447  memset(b->cur_dec, v, t);
448  b->cur_dec += t;
449  } else {
450  while (b->cur_dec < dec_end) {
451  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
452  v = GET_HUFF(gb, b->tree);
453  v = (c->col_lastval << 4) | v;
454  if (c->version < 'i') {
455  sign = ((int8_t) v) >> 7;
456  v = ((v & 0x7F) ^ sign) - sign;
457  v += 0x80;
458  }
459  *b->cur_dec++ = v;
460  }
461  }
462  return 0;
463 }
464 
465 /** number of bits used to store first DC value in bundle */
466 #define DC_START_BITS 11
467 
468 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
469  int start_bits, int has_sign)
470 {
471  int i, j, len, len2, bsize, sign, v, v2;
472  int16_t *dst = (int16_t*)b->cur_dec;
473  int16_t *dst_end = (int16_t*)b->data_end;
474 
475  CHECK_READ_VAL(gb, b, len);
476  v = get_bits(gb, start_bits - has_sign);
477  if (v && has_sign) {
478  sign = -get_bits1(gb);
479  v = (v ^ sign) - sign;
480  }
481  if (dst_end - dst < 1)
482  return AVERROR_INVALIDDATA;
483  *dst++ = v;
484  len--;
485  for (i = 0; i < len; i += 8) {
486  len2 = FFMIN(len - i, 8);
487  if (dst_end - dst < len2)
488  return AVERROR_INVALIDDATA;
489  bsize = get_bits(gb, 4);
490  if (bsize) {
491  for (j = 0; j < len2; j++) {
492  v2 = get_bits(gb, bsize);
493  if (v2) {
494  sign = -get_bits1(gb);
495  v2 = (v2 ^ sign) - sign;
496  }
497  v += v2;
498  *dst++ = v;
499  if (v < -32768 || v > 32767) {
500  av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
501  return AVERROR_INVALIDDATA;
502  }
503  }
504  } else {
505  for (j = 0; j < len2; j++)
506  *dst++ = v;
507  }
508  }
509 
510  b->cur_dec = (uint8_t*)dst;
511  return 0;
512 }
513 
514 /**
515  * Retrieve next value from bundle.
516  *
517  * @param c decoder context
518  * @param bundle bundle number
519  */
520 static inline int get_value(BinkContext *c, int bundle)
521 {
522  int ret;
523 
524  if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
525  return *c->bundle[bundle].cur_ptr++;
526  if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
527  return (int8_t)*c->bundle[bundle].cur_ptr++;
528  ret = *(int16_t*)c->bundle[bundle].cur_ptr;
529  c->bundle[bundle].cur_ptr += 2;
530  return ret;
531 }
532 
533 static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
534 {
535  c->bundle[bundle_num].cur_dec =
536  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
537  c->bundle[bundle_num].len = 13;
538 }
539 
541 {
542  int i;
543  for (i = 0; i < BINKB_NB_SRC; i++)
544  binkb_init_bundle(c, i);
545 }
546 
547 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
548 {
549  const int bits = binkb_bundle_sizes[bundle_num];
550  const int mask = 1 << (bits - 1);
551  const int issigned = binkb_bundle_signed[bundle_num];
552  Bundle *b = &c->bundle[bundle_num];
553  int i, len;
554 
555  CHECK_READ_VAL(gb, b, len);
556  if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
557  return AVERROR_INVALIDDATA;
558  if (bits <= 8) {
559  if (!issigned) {
560  for (i = 0; i < len; i++)
561  *b->cur_dec++ = get_bits(gb, bits);
562  } else {
563  for (i = 0; i < len; i++)
564  *b->cur_dec++ = get_bits(gb, bits) - mask;
565  }
566  } else {
567  int16_t *dst = (int16_t*)b->cur_dec;
568 
569  if (!issigned) {
570  for (i = 0; i < len; i++)
571  *dst++ = get_bits(gb, bits);
572  } else {
573  for (i = 0; i < len; i++)
574  *dst++ = get_bits(gb, bits) - mask;
575  }
576  b->cur_dec = (uint8_t*)dst;
577  }
578  return 0;
579 }
580 
581 static inline int binkb_get_value(BinkContext *c, int bundle_num)
582 {
583  int16_t ret;
584  const int bits = binkb_bundle_sizes[bundle_num];
585 
586  if (bits <= 8) {
587  int val = *c->bundle[bundle_num].cur_ptr++;
588  return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
589  }
590  ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
591  c->bundle[bundle_num].cur_ptr += 2;
592  return ret;
593 }
594 
595 /**
596  * Read 8x8 block of DCT coefficients.
597  *
598  * @param gb context for reading bits
599  * @param block place for storing coefficients
600  * @param scan scan order table
601  * @param quant_matrices quantization matrices
602  * @return 0 for success, negative value in other cases
603  */
604 static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
605  const int32_t quant_matrices[16][64], int q)
606 {
607  int coef_list[128];
608  int mode_list[128];
609  int i, t, bits, ccoef, mode, sign;
610  int list_start = 64, list_end = 64, list_pos;
611  int coef_count = 0;
612  int coef_idx[64];
613  int quant_idx;
614  const int32_t *quant;
615 
616  coef_list[list_end] = 4; mode_list[list_end++] = 0;
617  coef_list[list_end] = 24; mode_list[list_end++] = 0;
618  coef_list[list_end] = 44; mode_list[list_end++] = 0;
619  coef_list[list_end] = 1; mode_list[list_end++] = 3;
620  coef_list[list_end] = 2; mode_list[list_end++] = 3;
621  coef_list[list_end] = 3; mode_list[list_end++] = 3;
622 
623  for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
624  list_pos = list_start;
625  while (list_pos < list_end) {
626  if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
627  list_pos++;
628  continue;
629  }
630  ccoef = coef_list[list_pos];
631  mode = mode_list[list_pos];
632  switch (mode) {
633  case 0:
634  coef_list[list_pos] = ccoef + 4;
635  mode_list[list_pos] = 1;
636  case 2:
637  if (mode == 2) {
638  coef_list[list_pos] = 0;
639  mode_list[list_pos++] = 0;
640  }
641  for (i = 0; i < 4; i++, ccoef++) {
642  if (get_bits1(gb)) {
643  coef_list[--list_start] = ccoef;
644  mode_list[ list_start] = 3;
645  } else {
646  if (!bits) {
647  t = 1 - (get_bits1(gb) << 1);
648  } else {
649  t = get_bits(gb, bits) | 1 << bits;
650  sign = -get_bits1(gb);
651  t = (t ^ sign) - sign;
652  }
653  block[scan[ccoef]] = t;
654  coef_idx[coef_count++] = ccoef;
655  }
656  }
657  break;
658  case 1:
659  mode_list[list_pos] = 2;
660  for (i = 0; i < 3; i++) {
661  ccoef += 4;
662  coef_list[list_end] = ccoef;
663  mode_list[list_end++] = 2;
664  }
665  break;
666  case 3:
667  if (!bits) {
668  t = 1 - (get_bits1(gb) << 1);
669  } else {
670  t = get_bits(gb, bits) | 1 << bits;
671  sign = -get_bits1(gb);
672  t = (t ^ sign) - sign;
673  }
674  block[scan[ccoef]] = t;
675  coef_idx[coef_count++] = ccoef;
676  coef_list[list_pos] = 0;
677  mode_list[list_pos++] = 0;
678  break;
679  }
680  }
681  }
682 
683  if (q == -1) {
684  quant_idx = get_bits(gb, 4);
685  } else {
686  quant_idx = q;
687  if (quant_idx > 15U) {
688  av_log(NULL, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
689  return AVERROR_INVALIDDATA;
690  }
691  }
692 
693  quant = quant_matrices[quant_idx];
694 
695  block[0] = (block[0] * quant[0]) >> 11;
696  for (i = 0; i < coef_count; i++) {
697  int idx = coef_idx[i];
698  block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
699  }
700 
701  return 0;
702 }
703 
704 /**
705  * Read 8x8 block with residue after motion compensation.
706  *
707  * @param gb context for reading bits
708  * @param block place to store read data
709  * @param masks_count number of masks to decode
710  * @return 0 on success, negative value in other cases
711  */
712 static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
713 {
714  int coef_list[128];
715  int mode_list[128];
716  int i, sign, mask, ccoef, mode;
717  int list_start = 64, list_end = 64, list_pos;
718  int nz_coeff[64];
719  int nz_coeff_count = 0;
720 
721  coef_list[list_end] = 4; mode_list[list_end++] = 0;
722  coef_list[list_end] = 24; mode_list[list_end++] = 0;
723  coef_list[list_end] = 44; mode_list[list_end++] = 0;
724  coef_list[list_end] = 0; mode_list[list_end++] = 2;
725 
726  for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
727  for (i = 0; i < nz_coeff_count; i++) {
728  if (!get_bits1(gb))
729  continue;
730  if (block[nz_coeff[i]] < 0)
731  block[nz_coeff[i]] -= mask;
732  else
733  block[nz_coeff[i]] += mask;
734  masks_count--;
735  if (masks_count < 0)
736  return 0;
737  }
738  list_pos = list_start;
739  while (list_pos < list_end) {
740  if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
741  list_pos++;
742  continue;
743  }
744  ccoef = coef_list[list_pos];
745  mode = mode_list[list_pos];
746  switch (mode) {
747  case 0:
748  coef_list[list_pos] = ccoef + 4;
749  mode_list[list_pos] = 1;
750  case 2:
751  if (mode == 2) {
752  coef_list[list_pos] = 0;
753  mode_list[list_pos++] = 0;
754  }
755  for (i = 0; i < 4; i++, ccoef++) {
756  if (get_bits1(gb)) {
757  coef_list[--list_start] = ccoef;
758  mode_list[ list_start] = 3;
759  } else {
760  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
761  sign = -get_bits1(gb);
762  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
763  masks_count--;
764  if (masks_count < 0)
765  return 0;
766  }
767  }
768  break;
769  case 1:
770  mode_list[list_pos] = 2;
771  for (i = 0; i < 3; i++) {
772  ccoef += 4;
773  coef_list[list_end] = ccoef;
774  mode_list[list_end++] = 2;
775  }
776  break;
777  case 3:
778  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
779  sign = -get_bits1(gb);
780  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
781  coef_list[list_pos] = 0;
782  mode_list[list_pos++] = 0;
783  masks_count--;
784  if (masks_count < 0)
785  return 0;
786  break;
787  }
788  }
789  }
790 
791  return 0;
792 }
793 
794 /**
795  * Copy 8x8 block from source to destination, where src and dst may be overlapped
796  */
797 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
798 {
799  uint8_t tmp[64];
800  int i;
801  for (i = 0; i < 8; i++)
802  memcpy(tmp + i*8, src + i*stride, 8);
803  for (i = 0; i < 8; i++)
804  memcpy(dst + i*stride, tmp + i*8, 8);
805 }
806 
808  int plane_idx, int is_key, int is_chroma)
809 {
810  int blk, ret;
811  int i, j, bx, by;
812  uint8_t *dst, *ref, *ref_start, *ref_end;
813  int v, col[2];
814  const uint8_t *scan;
815  int xoff, yoff;
816  LOCAL_ALIGNED_16(int16_t, block, [64]);
817  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
818  int coordmap[64];
819  int ybias = is_key ? -15 : 0;
820  int qp;
821 
822  const int stride = frame->linesize[plane_idx];
823  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
824  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
825 
827  ref_start = frame->data[plane_idx];
828  ref_end = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
829 
830  for (i = 0; i < 64; i++)
831  coordmap[i] = (i & 7) + (i >> 3) * stride;
832 
833  for (by = 0; by < bh; by++) {
834  for (i = 0; i < BINKB_NB_SRC; i++) {
835  if ((ret = binkb_read_bundle(c, gb, i)) < 0)
836  return ret;
837  }
838 
839  dst = frame->data[plane_idx] + 8*by*stride;
840  for (bx = 0; bx < bw; bx++, dst += 8) {
842  switch (blk) {
843  case 0:
844  break;
845  case 1:
846  scan = bink_patterns[get_bits(gb, 4)];
847  i = 0;
848  do {
849  int mode, run;
850 
851  mode = get_bits1(gb);
852  run = get_bits(gb, binkb_runbits[i]) + 1;
853 
854  i += run;
855  if (i > 64) {
856  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
857  return AVERROR_INVALIDDATA;
858  }
859  if (mode) {
861  for (j = 0; j < run; j++)
862  dst[coordmap[*scan++]] = v;
863  } else {
864  for (j = 0; j < run; j++)
865  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
866  }
867  } while (i < 63);
868  if (i == 63)
869  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
870  break;
871  case 2:
872  memset(dctblock, 0, sizeof(*dctblock) * 64);
873  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
875  read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_intra_quant, qp);
876  c->binkdsp.idct_put(dst, stride, dctblock);
877  break;
878  case 3:
879  xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
880  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
881  ref = dst + xoff + yoff * stride;
882  if (ref < ref_start || ref + 8*stride > ref_end) {
883  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
884  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
885  c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
886  } else {
887  put_pixels8x8_overlapped(dst, ref, stride);
888  }
889  c->bdsp.clear_block(block);
891  read_residue(gb, block, v);
892  c->binkdsp.add_pixels8(dst, block, stride);
893  break;
894  case 4:
895  xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
896  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
897  ref = dst + xoff + yoff * stride;
898  if (ref < ref_start || ref + 8 * stride > ref_end) {
899  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
900  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
901  c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
902  } else {
903  put_pixels8x8_overlapped(dst, ref, stride);
904  }
905  memset(dctblock, 0, sizeof(*dctblock) * 64);
906  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
908  read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_inter_quant, qp);
909  c->binkdsp.idct_add(dst, stride, dctblock);
910  break;
911  case 5:
913  c->bdsp.fill_block_tab[1](dst, v, stride, 8);
914  break;
915  case 6:
916  for (i = 0; i < 2; i++)
917  col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
918  for (i = 0; i < 8; i++) {
920  for (j = 0; j < 8; j++, v >>= 1)
921  dst[i*stride + j] = col[v & 1];
922  }
923  break;
924  case 7:
925  xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
926  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
927  ref = dst + xoff + yoff * stride;
928  if (ref < ref_start || ref + 8 * stride > ref_end) {
929  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
930  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
931  c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
932  } else {
933  put_pixels8x8_overlapped(dst, ref, stride);
934  }
935  break;
936  case 8:
937  for (i = 0; i < 8; i++)
938  memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
939  c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
940  break;
941  default:
942  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
943  return AVERROR_INVALIDDATA;
944  }
945  }
946  }
947  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
948  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
949 
950  return 0;
951 }
952 
954  uint8_t *dst, uint8_t *prev, int stride,
955  uint8_t *ref_start,
956  uint8_t *ref_end)
957 {
958  int xoff = get_value(c, BINK_SRC_X_OFF);
959  int yoff = get_value(c, BINK_SRC_Y_OFF);
960  uint8_t *ref = prev + xoff + yoff * stride;
961  if (ref < ref_start || ref > ref_end) {
962  av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
963  xoff, yoff);
964  return AVERROR_INVALIDDATA;
965  }
966  c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
967 
968  return 0;
969 }
970 
972  int plane_idx, int is_chroma)
973 {
974  int blk, ret;
975  int i, j, bx, by;
976  uint8_t *dst, *prev, *ref_start, *ref_end;
977  int v, col[2];
978  const uint8_t *scan;
979  LOCAL_ALIGNED_16(int16_t, block, [64]);
980  LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
981  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
982  int coordmap[64];
983 
984  const int stride = frame->linesize[plane_idx];
985  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
986  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
987  int width = c->avctx->width >> is_chroma;
988 
989  init_lengths(c, FFMAX(width, 8), bw);
990  for (i = 0; i < BINK_NB_SRC; i++)
991  read_bundle(gb, c, i);
992 
993  ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
994  : frame->data[plane_idx];
995  ref_end = ref_start
996  + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
997 
998  for (i = 0; i < 64; i++)
999  coordmap[i] = (i & 7) + (i >> 3) * stride;
1000 
1001  for (by = 0; by < bh; by++) {
1002  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
1003  return ret;
1004  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
1005  return ret;
1006  if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
1007  return ret;
1008  if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
1009  return ret;
1010  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
1011  return ret;
1012  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
1013  return ret;
1014  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
1015  return ret;
1016  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
1017  return ret;
1018  if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
1019  return ret;
1020 
1021  if (by == bh)
1022  break;
1023  dst = frame->data[plane_idx] + 8*by*stride;
1024  prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
1025  : frame->data[plane_idx]) + 8*by*stride;
1026  for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1027  blk = get_value(c, BINK_SRC_BLOCK_TYPES);
1028  // 16x16 block type on odd line means part of the already decoded block, so skip it
1029  if ((by & 1) && blk == SCALED_BLOCK) {
1030  bx++;
1031  dst += 8;
1032  prev += 8;
1033  continue;
1034  }
1035  switch (blk) {
1036  case SKIP_BLOCK:
1037  c->hdsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1038  break;
1039  case SCALED_BLOCK:
1041  switch (blk) {
1042  case RUN_BLOCK:
1043  scan = bink_patterns[get_bits(gb, 4)];
1044  i = 0;
1045  do {
1046  int run = get_value(c, BINK_SRC_RUN) + 1;
1047 
1048  i += run;
1049  if (i > 64) {
1050  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1051  return AVERROR_INVALIDDATA;
1052  }
1053  if (get_bits1(gb)) {
1054  v = get_value(c, BINK_SRC_COLORS);
1055  for (j = 0; j < run; j++)
1056  ublock[*scan++] = v;
1057  } else {
1058  for (j = 0; j < run; j++)
1059  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1060  }
1061  } while (i < 63);
1062  if (i == 63)
1063  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1064  break;
1065  case INTRA_BLOCK:
1066  memset(dctblock, 0, sizeof(*dctblock) * 64);
1067  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1068  read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1069  c->binkdsp.idct_put(ublock, 8, dctblock);
1070  break;
1071  case FILL_BLOCK:
1072  v = get_value(c, BINK_SRC_COLORS);
1073  c->bdsp.fill_block_tab[0](dst, v, stride, 16);
1074  break;
1075  case PATTERN_BLOCK:
1076  for (i = 0; i < 2; i++)
1077  col[i] = get_value(c, BINK_SRC_COLORS);
1078  for (j = 0; j < 8; j++) {
1079  v = get_value(c, BINK_SRC_PATTERN);
1080  for (i = 0; i < 8; i++, v >>= 1)
1081  ublock[i + j*8] = col[v & 1];
1082  }
1083  break;
1084  case RAW_BLOCK:
1085  for (j = 0; j < 8; j++)
1086  for (i = 0; i < 8; i++)
1087  ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1088  break;
1089  default:
1090  av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1091  return AVERROR_INVALIDDATA;
1092  }
1093  if (blk != FILL_BLOCK)
1094  c->binkdsp.scale_block(ublock, dst, stride);
1095  bx++;
1096  dst += 8;
1097  prev += 8;
1098  break;
1099  case MOTION_BLOCK:
1100  ret = bink_put_pixels(c, dst, prev, stride,
1101  ref_start, ref_end);
1102  if (ret < 0)
1103  return ret;
1104  break;
1105  case RUN_BLOCK:
1106  scan = bink_patterns[get_bits(gb, 4)];
1107  i = 0;
1108  do {
1109  int run = get_value(c, BINK_SRC_RUN) + 1;
1110 
1111  i += run;
1112  if (i > 64) {
1113  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1114  return AVERROR_INVALIDDATA;
1115  }
1116  if (get_bits1(gb)) {
1117  v = get_value(c, BINK_SRC_COLORS);
1118  for (j = 0; j < run; j++)
1119  dst[coordmap[*scan++]] = v;
1120  } else {
1121  for (j = 0; j < run; j++)
1122  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1123  }
1124  } while (i < 63);
1125  if (i == 63)
1126  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1127  break;
1128  case RESIDUE_BLOCK:
1129  ret = bink_put_pixels(c, dst, prev, stride,
1130  ref_start, ref_end);
1131  if (ret < 0)
1132  return ret;
1133  c->bdsp.clear_block(block);
1134  v = get_bits(gb, 7);
1135  read_residue(gb, block, v);
1136  c->binkdsp.add_pixels8(dst, block, stride);
1137  break;
1138  case INTRA_BLOCK:
1139  memset(dctblock, 0, sizeof(*dctblock) * 64);
1140  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1141  read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1142  c->binkdsp.idct_put(dst, stride, dctblock);
1143  break;
1144  case FILL_BLOCK:
1145  v = get_value(c, BINK_SRC_COLORS);
1146  c->bdsp.fill_block_tab[1](dst, v, stride, 8);
1147  break;
1148  case INTER_BLOCK:
1149  ret = bink_put_pixels(c, dst, prev, stride,
1150  ref_start, ref_end);
1151  if (ret < 0)
1152  return ret;
1153  memset(dctblock, 0, sizeof(*dctblock) * 64);
1154  dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1155  read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
1156  c->binkdsp.idct_add(dst, stride, dctblock);
1157  break;
1158  case PATTERN_BLOCK:
1159  for (i = 0; i < 2; i++)
1160  col[i] = get_value(c, BINK_SRC_COLORS);
1161  for (i = 0; i < 8; i++) {
1162  v = get_value(c, BINK_SRC_PATTERN);
1163  for (j = 0; j < 8; j++, v >>= 1)
1164  dst[i*stride + j] = col[v & 1];
1165  }
1166  break;
1167  case RAW_BLOCK:
1168  for (i = 0; i < 8; i++)
1169  memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1170  c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1171  break;
1172  default:
1173  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1174  return AVERROR_INVALIDDATA;
1175  }
1176  }
1177  }
1178  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1179  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1180 
1181  return 0;
1182 }
1183 
1184 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
1185 {
1186  BinkContext * const c = avctx->priv_data;
1187  AVFrame *frame = data;
1188  GetBitContext gb;
1189  int plane, plane_idx, ret;
1190  int bits_count = pkt->size << 3;
1191 
1192  if (c->version > 'b') {
1193  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1194  return ret;
1195  } else {
1196  if ((ret = ff_reget_buffer(avctx, c->last)) < 0)
1197  return ret;
1198  if ((ret = av_frame_ref(frame, c->last)) < 0)
1199  return ret;
1200  }
1201 
1202  init_get_bits(&gb, pkt->data, bits_count);
1203  if (c->has_alpha) {
1204  if (c->version >= 'i')
1205  skip_bits_long(&gb, 32);
1206  if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
1207  return ret;
1208  }
1209  if (c->version >= 'i')
1210  skip_bits_long(&gb, 32);
1211 
1212  c->frame_num++;
1213 
1214  for (plane = 0; plane < 3; plane++) {
1215  plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1216 
1217  if (c->version > 'b') {
1218  if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
1219  return ret;
1220  } else {
1221  if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
1222  c->frame_num == 1, !!plane)) < 0)
1223  return ret;
1224  }
1225  if (get_bits_count(&gb) >= bits_count)
1226  break;
1227  }
1228  emms_c();
1229 
1230  if (c->version > 'b') {
1231  av_frame_unref(c->last);
1232  if ((ret = av_frame_ref(c->last, frame)) < 0)
1233  return ret;
1234  }
1235 
1236  *got_frame = 1;
1237 
1238  /* always report that the buffer was completely consumed */
1239  return pkt->size;
1240 }
1241 
1242 /**
1243  * Calculate quantization tables for version b
1244  */
1245 static av_cold void binkb_calc_quant(void)
1246 {
1247  uint8_t inv_bink_scan[64];
1248  static const int s[64]={
1249  1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1250  1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1251  1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1252  1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1253  1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1254  843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1255  581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1256  296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1257  };
1258  int i, j;
1259 #define C (1LL<<30)
1260  for (i = 0; i < 64; i++)
1261  inv_bink_scan[bink_scan[i]] = i;
1262 
1263  for (j = 0; j < 16; j++) {
1264  for (i = 0; i < 64; i++) {
1265  int k = inv_bink_scan[i];
1266  binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
1267  binkb_num[j]/(binkb_den[j] * (C>>12));
1268  binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
1269  binkb_num[j]/(binkb_den[j] * (C>>12));
1270  }
1271  }
1272 }
1273 
1275 {
1276  BinkContext * const c = avctx->priv_data;
1277  static VLC_TYPE table[16 * 128][2];
1278  static int binkb_initialised = 0;
1279  int i, ret;
1280  int flags;
1281 
1282  c->version = avctx->codec_tag >> 24;
1283  if (avctx->extradata_size < 4) {
1284  av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1285  return AVERROR_INVALIDDATA;
1286  }
1287  flags = AV_RL32(avctx->extradata);
1288  c->has_alpha = flags & BINK_FLAG_ALPHA;
1289  c->swap_planes = c->version >= 'h';
1290  if (!bink_trees[15].table) {
1291  for (i = 0; i < 16; i++) {
1292  const int maxbits = bink_tree_lens[i][15];
1293  bink_trees[i].table = table + i*128;
1294  bink_trees[i].table_allocated = 1 << maxbits;
1295  init_vlc(&bink_trees[i], maxbits, 16,
1296  bink_tree_lens[i], 1, 1,
1298  }
1299  }
1300  c->avctx = avctx;
1301 
1302  c->last = av_frame_alloc();
1303  if (!c->last)
1304  return AVERROR(ENOMEM);
1305 
1306  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1307  return ret;
1308 
1310 
1311  ff_blockdsp_init(&c->bdsp, avctx);
1312  ff_hpeldsp_init(&c->hdsp, avctx->flags);
1313  ff_binkdsp_init(&c->binkdsp);
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  av_frame_free(&c->last);
1335 
1336  free_bundles(c);
1337  return 0;
1338 }
1339 
1340 static void flush(AVCodecContext *avctx)
1341 {
1342  BinkContext * const c = avctx->priv_data;
1343 
1344  c->frame_num = 0;
1345 }
1346 
1348  .name = "binkvideo",
1349  .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
1350  .type = AVMEDIA_TYPE_VIDEO,
1351  .id = AV_CODEC_ID_BINKVIDEO,
1352  .priv_data_size = sizeof(BinkContext),
1353  .init = decode_init,
1354  .close = decode_end,
1355  .decode = decode_frame,
1356  .flush = flush,
1357  .capabilities = AV_CODEC_CAP_DR1,
1358 };
int plane
Definition: avisynth_c.h:291
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static const uint8_t bink_tree_lens[16][16]
Definition: binkdata.h:106
8-bit values for 2-colour pattern fill
Definition: bink.c:48
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
av_cold void ff_binkdsp_init(BinkDSPContext *c)
Definition: binkdsp.c:151
static const uint8_t bink_tree_bits[16][16]
Definition: binkdata.h:39
#define C
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:35
#define CHECK_READ_VAL(gb, b, t)
common check before starting decoding bundle data
Definition: bink.c:307
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
void(* scale_block)(const uint8_t src[64], uint8_t *dst, int linesize)
Definition: binkdsp.h:35
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:204
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static const uint8_t binkb_den[16]
Definition: binkdata.h:651
static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
Read 8x8 block with residue after motion compensation.
Definition: bink.c:712
int size
Definition: avcodec.h:1589
const char * b
Definition: vf_curves.c:113
int av_log2(unsigned v)
Definition: intmath.c:26
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1885
8-bit values for 2-colour pattern fill
Definition: bink.c:78
static const uint8_t bink_scan[64]
Bink DCT and residue 8x8 block scan order.
Definition: binkdata.h:28
uint8_t run
Definition: svq3.c:192
static AVPacket pkt
#define blk(i)
Definition: sha.c:185
8x8 block types
Definition: bink.c:75
Tree col_high[16]
trees for decoding high nibble in "colours" data type
Definition: bink.c:126
AVCodec.
Definition: avcodec.h:3559
16x16 block types (a subset of 8x8 block types)
Definition: bink.c:76
void(* add_pixels8)(uint8_t *pixels, int16_t *block, int line_size)
Definition: binkdsp.h:36
int len
length of number of entries to decode (in bits)
Definition: bink.c:103
Macro definitions for various function/variable attributes.
HpelDSPContext hdsp
Definition: bink.c:117
static int32_t binkb_inter_quant[16][64]
Definition: bink.c:69
quantizer values for interblocks with DCT
Definition: bink.c:54
static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
Definition: bink.c:533
static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:407
X components of motion value.
Definition: bink.c:49
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
motion block with DCT applied to the difference
Definition: bink.c:141
uint8_t bits
Definition: crc.c:296
uint8_t
#define av_cold
Definition: attributes.h:82
intra DCT block
Definition: bink.c:139
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
static int binkb_get_value(BinkContext *c, int bundle_num)
Definition: bink.c:581
static void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
Copy 8x8 block from source to destination, where src and dst may be overlapped.
Definition: bink.c:797
Tree tree
Huffman tree-related data.
Definition: bink.c:104
mode
Definition: f_perms.c:27
BlockTypes
Bink video block types.
Definition: bink.c:133
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:374
uncoded 8x8 block
Definition: bink.c:143
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1772
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1588
static const int32_t bink_inter_quant[16][64]
Definition: binkdata.h:451
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
int vlc_num
tree number (in bink_trees[])
Definition: bink.c:92
skipped block
Definition: bink.c:134
bitstream reader API header.
static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:316
ptrdiff_t size
Definition: opengl_enc.c:101
data structure used for decoding single Bink data type
Definition: bink.c:102
#define FFALIGN(x, a)
Definition: macros.h:48
static const uint8_t bink_patterns[16][64]
Definition: binkdata.h:125
#define av_log(a,...)
uint8_t * data
buffer for decoded symbols
Definition: bink.c:105
Bink DSP routines.
#define U(x)
Definition: vp56_arith.h:37
block is copied from previous frame with some offset
Definition: bink.c:136
static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan, const int32_t quant_matrices[16][64], int q)
Read 8x8 block of DCT coefficients.
Definition: bink.c:604
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t * data_end
buffer end
Definition: bink.c:106
static const uint16_t mask[17]
Definition: lzw.c:38
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: vlc.h:38
unsigned frame_num
Definition: bink.c:123
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
AVCodecContext * avctx
Definition: bink.c:115
static av_cold void free_bundles(BinkContext *c)
Free memory used by bundles.
Definition: bink.c:202
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1752
AVCodec ff_bink_decoder
Definition: bink.c:1347
const char * name
Name of the codec implementation.
Definition: avcodec.h:3566
int col_lastval
value of last decoded high nibble in "colours" data type
Definition: bink.c:127
#define FFMAX(a, b)
Definition: common.h:94
BinkDSPContext binkdsp
Definition: bink.c:118
DC values for interblocks with DCT.
Definition: bink.c:82
Definition: vlc.h:26
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
block is composed from runs of colours with custom scan order
Definition: bink.c:137
common internal API header
block is filled with single colour
Definition: bink.c:140
static av_cold void binkb_calc_quant(void)
Calculate quantization tables for version b.
Definition: bink.c:1245
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:251
Half-pel DSP context.
Definition: hpeldsp.h:45
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:977
#define FFMIN(a, b)
Definition: common.h:96
#define width
int width
picture width / height.
Definition: avcodec.h:1844
static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, int plane_idx, int is_key, int is_chroma)
Definition: bink.c:807
Y components of motion value.
Definition: bink.c:50
number of coefficients for residue blocks
Definition: bink.c:55
int32_t
int has_alpha
Definition: bink.c:121
#define src
Definition: vp9dsp.c:530
block has size 16x16
Definition: bink.c:135
static const uint8_t binkb_inter_seed[64]
Definition: binkdata.h:636
#define INIT_VLC_LE
Definition: vlc.h:54
int swap_planes
Definition: bink.c:122
static const uint8_t binkb_runbits[64]
Definition: binkdata.h:614
static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, int plane_idx, int is_chroma)
Definition: bink.c:971
int table_allocated
Definition: vlc.h:29
data needed to decode 4-bit Huffman-coded value
Definition: bink.c:91
uint8_t * cur_dec
pointer to the not yet decoded part of the buffer
Definition: bink.c:107
Half-pel DSP functions.
Libavcodec external API header.
quantizer values for intrablocks with DCT
Definition: bink.c:53
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:58
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
#define DC_START_BITS
number of bits used to store first DC value in bundle
Definition: bink.c:466
main external API structure.
Definition: avcodec.h:1657
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:928
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1689
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
static av_cold int decode_init(AVCodecContext *avctx)
Definition: bink.c:1274
int extradata_size
Definition: avcodec.h:1773
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
static const int32_t bink_intra_quant[16][64]
Definition: binkdata.h:288
DC values for interblocks with DCT.
Definition: bink.c:52
Sources
IDs for different data types used in Bink video codec.
Definition: bink.c:74
block is filled with two colours following custom pattern
Definition: bink.c:142
static av_cold void binkb_init_bundles(BinkContext *c)
Definition: bink.c:540
static int32_t binkb_intra_quant[16][64]
Definition: bink.c:68
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:406
8x8 block types
Definition: bink.c:46
static const uint8_t binkb_num[16]
Definition: binkdata.h:647
#define GET_HUFF(gb, tree)
Definition: bink.c:96
run lengths for special fill block
Definition: bink.c:83
Y components of motion value.
Definition: bink.c:80
static VLC bink_trees[16]
Definition: bink.c:40
static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
Merge two consequent lists of equal size depending on bits read.
Definition: bink.c:217
const uint8_t * quant
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:484
Bundle bundle[BINKB_NB_SRC]
bundles for decoding all data types
Definition: bink.c:125
static void flush(AVCodecContext *avctx)
Definition: bink.c:1340
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
pixel values used for different block types
Definition: bink.c:77
AVFrame * last
Definition: bink.c:119
void(* idct_add)(uint8_t *dest, int line_size, int32_t *block)
Definition: binkdsp.h:34
X components of motion value.
Definition: bink.c:79
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:752
uint8_t * cur_ptr
pointer to the data that is not read from buffer yet
Definition: bink.c:108
#define BINK_FLAG_ALPHA
Definition: bink.c:37
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
static const uint8_t binkb_intra_seed[64]
Definition: binkdata.h:625
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
Definition: bink.c:547
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
static void init_lengths(BinkContext *c, int width, int bw)
Initialize length in all bundles.
Definition: bink.c:153
static av_cold int init_bundles(BinkContext *c)
Allocate memory for bundles.
Definition: bink.c:178
static double c[64]
#define INIT_VLC_USE_NEW_STATIC
Definition: vlc.h:55
static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
Definition: bink.c:427
static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:372
static void read_tree(GetBitContext *gb, Tree *tree)
Read information about Huffman tree used to decode data.
Definition: bink.c:244
uint8_t syms[16]
leaf value to symbol mapping
Definition: bink.c:93
static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b, int start_bits, int has_sign)
Definition: bink.c:468
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
Definition: bink.c:1184
DC values for intrablocks with DCT.
Definition: bink.c:51
void * priv_data
Definition: avcodec.h:1699
static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:338
motion block with some difference added
Definition: bink.c:138
int len
static uint8_t tmp[8]
Definition: des.c:38
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
void(* idct_put)(uint8_t *dest, int line_size, int32_t *block)
Definition: binkdsp.h:33
static int bink_put_pixels(BinkContext *c, uint8_t *dst, uint8_t *prev, int stride, uint8_t *ref_start, uint8_t *ref_end)
Definition: bink.c:953
static const int binkb_bundle_sizes[BINKB_NB_SRC]
Definition: bink.c:60
int version
internal Bink file version
Definition: bink.c:120
static const int binkb_bundle_signed[BINKB_NB_SRC]
Definition: bink.c:64
static av_cold int decode_end(AVCodecContext *avctx)
Definition: bink.c:1330
FILE * out
Definition: movenc.c:54
BlockDSPContext bdsp
Definition: bink.c:116
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:121
#define av_freep(p)
#define VLC_TYPE
Definition: vlc.h:24
#define FFSWAP(type, a, b)
Definition: common.h:99
static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
Prepare bundle for decoding data.
Definition: bink.c:285
static const uint8_t bink_rlelens[4]
Definition: bink.c:370
#define stride
pixel values used for different block types
Definition: bink.c:47
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
static int get_value(BinkContext *c, int bundle)
Retrieve next value from bundle.
Definition: bink.c:520
This structure stores compressed data.
Definition: avcodec.h:1565
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1341
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
DC values for intrablocks with DCT.
Definition: bink.c:81
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
OldSources
IDs for different data types used in old version of Bink video codec.
Definition: bink.c:45
op_fill_func fill_block_tab[2]
Definition: blockdsp.h:38
static int16_t block[64]
Definition: dct-test.c:112