FFmpeg
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/mem.h"
26 #include "libavutil/mem_internal.h"
27 #include "libavutil/thread.h"
28 
29 #define BITSTREAM_READER_LE
30 #include "avcodec.h"
31 #include "binkdata.h"
32 #include "binkdsp.h"
33 #include "blockdsp.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "get_bits.h"
37 #include "hpeldsp.h"
38 
39 #define BINK_FLAG_ALPHA 0x00100000
40 #define BINK_FLAG_GRAY 0x00020000
41 
42 static VLC bink_trees[16];
43 
44 /**
45  * IDs for different data types used in old version of Bink video codec
46  */
47 enum OldSources {
48  BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
49  BINKB_SRC_COLORS, ///< pixel values used for different block types
50  BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
51  BINKB_SRC_X_OFF, ///< X components of motion value
52  BINKB_SRC_Y_OFF, ///< Y components of motion value
53  BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
54  BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
55  BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
56  BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
57  BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
58 
60 };
61 
62 static const uint8_t binkb_bundle_sizes[BINKB_NB_SRC] = {
63  4, 8, 8, 5, 5, 11, 11, 4, 4, 7
64 };
65 
66 static const uint8_t binkb_bundle_signed[BINKB_NB_SRC] = {
67  0, 0, 0, 1, 1, 0, 1, 0, 0, 0
68 };
69 
70 static int32_t binkb_intra_quant[16][64];
71 static int32_t binkb_inter_quant[16][64];
72 
73 /**
74  * IDs for different data types used in Bink video codec
75  */
76 enum Sources {
77  BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
78  BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
79  BINK_SRC_COLORS, ///< pixel values used for different block types
80  BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
81  BINK_SRC_X_OFF, ///< X components of motion value
82  BINK_SRC_Y_OFF, ///< Y components of motion value
83  BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
84  BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
85  BINK_SRC_RUN, ///< run lengths for special fill block
86 
88 };
89 
90 /**
91  * data needed to decode 4-bit Huffman-coded value
92  */
93 typedef struct Tree {
94  int vlc_num; ///< tree number (in bink_trees[])
95  uint8_t syms[16]; ///< leaf value to symbol mapping
96 } Tree;
97 
98 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
99  bink_trees[(tree).vlc_num].bits, 1)]
100 
101 /**
102  * data structure used for decoding single Bink data type
103  */
104 typedef struct Bundle {
105  int len; ///< length of number of entries to decode (in bits)
106  Tree tree; ///< Huffman tree-related data
107  uint8_t *data; ///< buffer for decoded symbols
108  uint8_t *data_end; ///< buffer end
109  uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
110  uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
111 } Bundle;
112 
113 /*
114  * Decoder context
115  */
116 typedef struct BinkContext {
122  int version; ///< internal Bink file version
125  unsigned frame_num;
126 
127  Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
128  Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
129  int col_lastval; ///< value of last decoded high nibble in "colours" data type
130 } BinkContext;
131 
132 /**
133  * Bink video block types
134  */
136  SKIP_BLOCK = 0, ///< skipped block
137  SCALED_BLOCK, ///< block has size 16x16
138  MOTION_BLOCK, ///< block is copied from previous frame with some offset
139  RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
140  RESIDUE_BLOCK, ///< motion block with some difference added
141  INTRA_BLOCK, ///< intra DCT block
142  FILL_BLOCK, ///< block is filled with single colour
143  INTER_BLOCK, ///< motion block with DCT applied to the difference
144  PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
145  RAW_BLOCK, ///< uncoded 8x8 block
146 };
147 
148 /**
149  * Initialize length in all bundles.
150  *
151  * @param c decoder context
152  * @param width plane width
153  * @param bw plane width in 8x8 blocks
154  */
155 static void init_lengths(BinkContext *c, int width, int bw)
156 {
157  width = FFALIGN(width, 8);
158 
159  c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
160 
161  c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
162 
163  c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
164 
165  c->bundle[BINK_SRC_INTRA_DC].len =
166  c->bundle[BINK_SRC_INTER_DC].len =
167  c->bundle[BINK_SRC_X_OFF].len =
168  c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
169 
170  c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
171 
172  c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
173 }
174 
175 /**
176  * Allocate memory for bundles.
177  *
178  * @param c decoder context
179  */
181 {
182  int bw, bh, blocks;
183  uint8_t *tmp;
184  int i;
185 
186  bw = (c->avctx->width + 7) >> 3;
187  bh = (c->avctx->height + 7) >> 3;
188  blocks = bw * bh;
189 
190  tmp = av_calloc(blocks, 64 * BINKB_NB_SRC);
191  if (!tmp)
192  return AVERROR(ENOMEM);
193  for (i = 0; i < BINKB_NB_SRC; i++) {
194  c->bundle[i].data = tmp;
195  tmp += blocks * 64;
196  c->bundle[i].data_end = tmp;
197  }
198 
199  return 0;
200 }
201 
202 /**
203  * Free memory used by bundles.
204  *
205  * @param c decoder context
206  */
208 {
209  av_freep(&c->bundle[0].data);
210 }
211 
212 /**
213  * Merge two consequent lists of equal size depending on bits read.
214  *
215  * @param gb context for reading bits
216  * @param dst buffer where merged list will be written to
217  * @param src pointer to the head of the first list (the second lists starts at src+size)
218  * @param size input lists size
219  */
220 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
221 {
222  uint8_t *src2 = src + size;
223  int size2 = size;
224 
225  do {
226  if (!get_bits1(gb)) {
227  *dst++ = *src++;
228  size--;
229  } else {
230  *dst++ = *src2++;
231  size2--;
232  }
233  } while (size && size2);
234 
235  while (size--)
236  *dst++ = *src++;
237  while (size2--)
238  *dst++ = *src2++;
239 }
240 
241 /**
242  * Read information about Huffman tree used to decode data.
243  *
244  * @param gb context for reading bits
245  * @param tree pointer for storing tree data
246  */
247 static int read_tree(GetBitContext *gb, Tree *tree)
248 {
249  uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
250  int i, t, len;
251 
252  if (get_bits_left(gb) < 4)
253  return AVERROR_INVALIDDATA;
254 
255  tree->vlc_num = get_bits(gb, 4);
256  if (!tree->vlc_num) {
257  for (i = 0; i < 16; i++)
258  tree->syms[i] = i;
259  return 0;
260  }
261  if (get_bits1(gb)) {
262  len = get_bits(gb, 3);
263  for (i = 0; i <= len; i++) {
264  tree->syms[i] = get_bits(gb, 4);
265  tmp1[tree->syms[i]] = 1;
266  }
267  for (i = 0; i < 16 && len < 16 - 1; i++)
268  if (!tmp1[i])
269  tree->syms[++len] = i;
270  } else {
271  len = get_bits(gb, 2);
272  for (i = 0; i < 16; i++)
273  in[i] = i;
274  for (i = 0; i <= len; i++) {
275  int size = 1 << i;
276  for (t = 0; t < 16; t += size << 1)
277  merge(gb, out + t, in + t, size);
278  FFSWAP(uint8_t*, in, out);
279  }
280  memcpy(tree->syms, in, 16);
281  }
282  return 0;
283 }
284 
285 /**
286  * Prepare bundle for decoding data.
287  *
288  * @param gb context for reading bits
289  * @param c decoder context
290  * @param bundle_num number of the bundle to initialize
291  */
292 static int read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
293 {
294  int i;
295 
296  if (bundle_num == BINK_SRC_COLORS) {
297  for (i = 0; i < 16; i++) {
298  int ret = read_tree(gb, &c->col_high[i]);
299  if (ret < 0)
300  return ret;
301  }
302  c->col_lastval = 0;
303  }
304  if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC) {
305  int ret = read_tree(gb, &c->bundle[bundle_num].tree);
306  if (ret < 0)
307  return ret;
308  }
309  c->bundle[bundle_num].cur_dec =
310  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
311 
312  return 0;
313 }
314 
315 /**
316  * common check before starting decoding bundle data
317  *
318  * @param gb context for reading bits
319  * @param b bundle
320  * @param t variable where number of elements to decode will be stored
321  */
322 #define CHECK_READ_VAL(gb, b, t) \
323  if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
324  return 0; \
325  t = get_bits(gb, b->len); \
326  if (!t) { \
327  b->cur_dec = NULL; \
328  return 0; \
329  } \
330 
331 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
332 {
333  int t, v;
334  const uint8_t *dec_end;
335 
336  CHECK_READ_VAL(gb, b, t);
337  dec_end = b->cur_dec + t;
338  if (dec_end > b->data_end) {
339  av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
340  return AVERROR_INVALIDDATA;
341  }
342  if (get_bits_left(gb) < 1)
343  return AVERROR_INVALIDDATA;
344  if (get_bits1(gb)) {
345  v = get_bits(gb, 4);
346  memset(b->cur_dec, v, t);
347  b->cur_dec += t;
348  } else {
349  while (b->cur_dec < dec_end)
350  *b->cur_dec++ = GET_HUFF(gb, b->tree);
351  }
352  return 0;
353 }
354 
356 {
357  int t, sign, v;
358  const uint8_t *dec_end;
359 
360  CHECK_READ_VAL(gb, b, t);
361  dec_end = b->cur_dec + t;
362  if (dec_end > b->data_end) {
363  av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
364  return AVERROR_INVALIDDATA;
365  }
366  if (get_bits_left(gb) < 1)
367  return AVERROR_INVALIDDATA;
368  if (get_bits1(gb)) {
369  v = get_bits(gb, 4);
370  if (v) {
371  sign = -get_bits1(gb);
372  v = (v ^ sign) - sign;
373  }
374  memset(b->cur_dec, v, t);
375  b->cur_dec += t;
376  } else {
377  while (b->cur_dec < dec_end) {
378  v = GET_HUFF(gb, b->tree);
379  if (v) {
380  sign = -get_bits1(gb);
381  v = (v ^ sign) - sign;
382  }
383  *b->cur_dec++ = v;
384  }
385  }
386  return 0;
387 }
388 
389 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
390 
392 {
393  BinkContext * const c = avctx->priv_data;
394  int t, v;
395  int last = 0;
396  const uint8_t *dec_end;
397 
398  CHECK_READ_VAL(gb, b, t);
399  if (c->version == 'k') {
400  t ^= 0xBBu;
401  if (t == 0) {
402  b->cur_dec = NULL;
403  return 0;
404  }
405  }
406  dec_end = b->cur_dec + t;
407  if (dec_end > b->data_end) {
408  av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
409  return AVERROR_INVALIDDATA;
410  }
411  if (get_bits_left(gb) < 1)
412  return AVERROR_INVALIDDATA;
413  if (get_bits1(gb)) {
414  v = get_bits(gb, 4);
415  memset(b->cur_dec, v, t);
416  b->cur_dec += t;
417  } else {
418  while (b->cur_dec < dec_end) {
419  v = GET_HUFF(gb, b->tree);
420  if (v < 12) {
421  last = v;
422  *b->cur_dec++ = v;
423  } else {
424  int run = bink_rlelens[v - 12];
425 
426  if (dec_end - b->cur_dec < run)
427  return AVERROR_INVALIDDATA;
428  memset(b->cur_dec, last, run);
429  b->cur_dec += run;
430  }
431  }
432  }
433  return 0;
434 }
435 
437 {
438  int t, v;
439  const uint8_t *dec_end;
440 
441  CHECK_READ_VAL(gb, b, t);
442  dec_end = b->cur_dec + t;
443  if (dec_end > b->data_end) {
444  av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
445  return AVERROR_INVALIDDATA;
446  }
447  while (b->cur_dec < dec_end) {
448  if (get_bits_left(gb) < 2)
449  return AVERROR_INVALIDDATA;
450  v = GET_HUFF(gb, b->tree);
451  v |= GET_HUFF(gb, b->tree) << 4;
452  *b->cur_dec++ = v;
453  }
454 
455  return 0;
456 }
457 
459 {
460  int t, sign, v;
461  const uint8_t *dec_end;
462 
463  CHECK_READ_VAL(gb, b, t);
464  dec_end = b->cur_dec + t;
465  if (dec_end > b->data_end) {
466  av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
467  return AVERROR_INVALIDDATA;
468  }
469  if (get_bits_left(gb) < 1)
470  return AVERROR_INVALIDDATA;
471  if (get_bits1(gb)) {
472  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
473  v = GET_HUFF(gb, b->tree);
474  v = (c->col_lastval << 4) | v;
475  if (c->version < 'i') {
476  sign = ((int8_t) v) >> 7;
477  v = ((v & 0x7F) ^ sign) - sign;
478  v += 0x80;
479  }
480  memset(b->cur_dec, v, t);
481  b->cur_dec += t;
482  } else {
483  while (b->cur_dec < dec_end) {
484  if (get_bits_left(gb) < 2)
485  return AVERROR_INVALIDDATA;
486  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
487  v = GET_HUFF(gb, b->tree);
488  v = (c->col_lastval << 4) | v;
489  if (c->version < 'i') {
490  sign = ((int8_t) v) >> 7;
491  v = ((v & 0x7F) ^ sign) - sign;
492  v += 0x80;
493  }
494  *b->cur_dec++ = v;
495  }
496  }
497  return 0;
498 }
499 
500 /** number of bits used to store first DC value in bundle */
501 #define DC_START_BITS 11
502 
503 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
504  int start_bits, int has_sign)
505 {
506  int i, j, len, len2, bsize, sign, v, v2;
507  int16_t *dst = (int16_t*)b->cur_dec;
508  int16_t *dst_end = (int16_t*)b->data_end;
509 
510  CHECK_READ_VAL(gb, b, len);
511  if (get_bits_left(gb) < start_bits - has_sign)
512  return AVERROR_INVALIDDATA;
513  v = get_bits(gb, start_bits - has_sign);
514  if (v && has_sign) {
515  sign = -get_bits1(gb);
516  v = (v ^ sign) - sign;
517  }
518  if (dst_end - dst < 1)
519  return AVERROR_INVALIDDATA;
520  *dst++ = v;
521  len--;
522  for (i = 0; i < len; i += 8) {
523  len2 = FFMIN(len - i, 8);
524  if (dst_end - dst < len2)
525  return AVERROR_INVALIDDATA;
526  bsize = get_bits(gb, 4);
527  if (bsize) {
528  for (j = 0; j < len2; j++) {
529  v2 = get_bits(gb, bsize);
530  if (v2) {
531  sign = -get_bits1(gb);
532  v2 = (v2 ^ sign) - sign;
533  }
534  v += v2;
535  *dst++ = v;
536  if (v < -32768 || v > 32767) {
537  av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
538  return AVERROR_INVALIDDATA;
539  }
540  }
541  } else {
542  for (j = 0; j < len2; j++)
543  *dst++ = v;
544  }
545  }
546 
547  b->cur_dec = (uint8_t*)dst;
548  return 0;
549 }
550 
551 /**
552  * Retrieve next value from bundle.
553  *
554  * @param c decoder context
555  * @param bundle bundle number
556  */
557 static inline int get_value(BinkContext *c, int bundle)
558 {
559  int ret;
560 
561  if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
562  return *c->bundle[bundle].cur_ptr++;
563  if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
564  return (int8_t)*c->bundle[bundle].cur_ptr++;
565  ret = *(int16_t*)c->bundle[bundle].cur_ptr;
566  c->bundle[bundle].cur_ptr += 2;
567  return ret;
568 }
569 
570 static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
571 {
572  c->bundle[bundle_num].cur_dec =
573  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
574  c->bundle[bundle_num].len = 13;
575 }
576 
578 {
579  int i;
580  for (i = 0; i < BINKB_NB_SRC; i++)
582 }
583 
584 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
585 {
586  const int bits = binkb_bundle_sizes[bundle_num];
587  const int mask = 1 << (bits - 1);
588  const int issigned = binkb_bundle_signed[bundle_num];
589  Bundle *b = &c->bundle[bundle_num];
590  int i, len;
591 
592  CHECK_READ_VAL(gb, b, len);
593  if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
594  return AVERROR_INVALIDDATA;
595  if (bits <= 8) {
596  if (!issigned) {
597  for (i = 0; i < len; i++)
598  *b->cur_dec++ = get_bits(gb, bits);
599  } else {
600  for (i = 0; i < len; i++)
601  *b->cur_dec++ = get_bits(gb, bits) - mask;
602  }
603  } else {
604  int16_t *dst = (int16_t*)b->cur_dec;
605 
606  if (!issigned) {
607  for (i = 0; i < len; i++)
608  *dst++ = get_bits(gb, bits);
609  } else {
610  for (i = 0; i < len; i++)
611  *dst++ = get_bits(gb, bits) - mask;
612  }
613  b->cur_dec = (uint8_t*)dst;
614  }
615  return 0;
616 }
617 
618 static inline int binkb_get_value(BinkContext *c, int bundle_num)
619 {
620  int16_t ret;
621  const int bits = binkb_bundle_sizes[bundle_num];
622 
623  if (bits <= 8) {
624  int val = *c->bundle[bundle_num].cur_ptr++;
625  return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
626  }
627  ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
628  c->bundle[bundle_num].cur_ptr += 2;
629  return ret;
630 }
631 
632 /**
633  * Read 8x8 block of DCT coefficients.
634  *
635  * @param gb context for reading bits
636  * @param block place for storing coefficients
637  * @param scan scan order table
638  * @param quant_matrices quantization matrices
639  * @return 0 for success, negative value in other cases
640  */
642  const uint8_t *scan, int *coef_count_,
643  int coef_idx[64], int q)
644 {
645  int coef_list[128];
646  int mode_list[128];
647  int i, t, bits, ccoef, mode, sign;
648  int list_start = 64, list_end = 64, list_pos;
649  int coef_count = 0;
650  int quant_idx;
651 
652  if (get_bits_left(gb) < 4)
653  return AVERROR_INVALIDDATA;
654 
655  coef_list[list_end] = 4; mode_list[list_end++] = 0;
656  coef_list[list_end] = 24; mode_list[list_end++] = 0;
657  coef_list[list_end] = 44; mode_list[list_end++] = 0;
658  coef_list[list_end] = 1; mode_list[list_end++] = 3;
659  coef_list[list_end] = 2; mode_list[list_end++] = 3;
660  coef_list[list_end] = 3; mode_list[list_end++] = 3;
661 
662  for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
663  list_pos = list_start;
664  while (list_pos < list_end) {
665  if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
666  list_pos++;
667  continue;
668  }
669  ccoef = coef_list[list_pos];
670  mode = mode_list[list_pos];
671  switch (mode) {
672  case 0:
673  coef_list[list_pos] = ccoef + 4;
674  mode_list[list_pos] = 1;
676  case 2:
677  if (mode == 2) {
678  coef_list[list_pos] = 0;
679  mode_list[list_pos++] = 0;
680  }
681  for (i = 0; i < 4; i++, ccoef++) {
682  if (get_bits1(gb)) {
683  coef_list[--list_start] = ccoef;
684  mode_list[ list_start] = 3;
685  } else {
686  if (!bits) {
687  t = 1 - (get_bits1(gb) << 1);
688  } else {
689  t = get_bits(gb, bits) | 1 << bits;
690  sign = -get_bits1(gb);
691  t = (t ^ sign) - sign;
692  }
693  block[scan[ccoef]] = t;
694  coef_idx[coef_count++] = ccoef;
695  }
696  }
697  break;
698  case 1:
699  mode_list[list_pos] = 2;
700  for (i = 0; i < 3; i++) {
701  ccoef += 4;
702  coef_list[list_end] = ccoef;
703  mode_list[list_end++] = 2;
704  }
705  break;
706  case 3:
707  if (!bits) {
708  t = 1 - (get_bits1(gb) << 1);
709  } else {
710  t = get_bits(gb, bits) | 1 << bits;
711  sign = -get_bits1(gb);
712  t = (t ^ sign) - sign;
713  }
714  block[scan[ccoef]] = t;
715  coef_idx[coef_count++] = ccoef;
716  coef_list[list_pos] = 0;
717  mode_list[list_pos++] = 0;
718  break;
719  }
720  }
721  }
722 
723  if (q == -1) {
724  quant_idx = get_bits(gb, 4);
725  } else {
726  quant_idx = q;
727  if (quant_idx > 15U) {
728  av_log(c->avctx, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
729  return AVERROR_INVALIDDATA;
730  }
731  }
732 
733  *coef_count_ = coef_count;
734 
735  return quant_idx;
736 }
737 
738 static void unquantize_dct_coeffs(int32_t block[64], const uint32_t quant[64],
739  int coef_count, int coef_idx[64],
740  const uint8_t *scan)
741 {
742  int i;
743  block[0] = (int)(block[0] * quant[0]) >> 11;
744  for (i = 0; i < coef_count; i++) {
745  int idx = coef_idx[i];
746  block[scan[idx]] = (int)(block[scan[idx]] * quant[idx]) >> 11;
747  }
748 }
749 
750 /**
751  * Read 8x8 block with residue after motion compensation.
752  *
753  * @param gb context for reading bits
754  * @param block place to store read data
755  * @param masks_count number of masks to decode
756  * @return 0 on success, negative value in other cases
757  */
758 static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
759 {
760  int coef_list[128];
761  int mode_list[128];
762  int i, sign, mask, ccoef, mode;
763  int list_start = 64, list_end = 64, list_pos;
764  int nz_coeff[64];
765  int nz_coeff_count = 0;
766 
767  coef_list[list_end] = 4; mode_list[list_end++] = 0;
768  coef_list[list_end] = 24; mode_list[list_end++] = 0;
769  coef_list[list_end] = 44; mode_list[list_end++] = 0;
770  coef_list[list_end] = 0; mode_list[list_end++] = 2;
771 
772  for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
773  for (i = 0; i < nz_coeff_count; i++) {
774  if (!get_bits1(gb))
775  continue;
776  if (block[nz_coeff[i]] < 0)
777  block[nz_coeff[i]] -= mask;
778  else
779  block[nz_coeff[i]] += mask;
780  masks_count--;
781  if (masks_count < 0)
782  return 0;
783  }
784  list_pos = list_start;
785  while (list_pos < list_end) {
786  if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
787  list_pos++;
788  continue;
789  }
790  ccoef = coef_list[list_pos];
791  mode = mode_list[list_pos];
792  switch (mode) {
793  case 0:
794  coef_list[list_pos] = ccoef + 4;
795  mode_list[list_pos] = 1;
797  case 2:
798  if (mode == 2) {
799  coef_list[list_pos] = 0;
800  mode_list[list_pos++] = 0;
801  }
802  for (i = 0; i < 4; i++, ccoef++) {
803  if (get_bits1(gb)) {
804  coef_list[--list_start] = ccoef;
805  mode_list[ list_start] = 3;
806  } else {
807  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
808  sign = -get_bits1(gb);
809  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
810  masks_count--;
811  if (masks_count < 0)
812  return 0;
813  }
814  }
815  break;
816  case 1:
817  mode_list[list_pos] = 2;
818  for (i = 0; i < 3; i++) {
819  ccoef += 4;
820  coef_list[list_end] = ccoef;
821  mode_list[list_end++] = 2;
822  }
823  break;
824  case 3:
825  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
826  sign = -get_bits1(gb);
827  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
828  coef_list[list_pos] = 0;
829  mode_list[list_pos++] = 0;
830  masks_count--;
831  if (masks_count < 0)
832  return 0;
833  break;
834  }
835  }
836  }
837 
838  return 0;
839 }
840 
841 /**
842  * Copy 8x8 block from source to destination, where src and dst may be overlapped
843  */
844 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
845 {
846  uint8_t tmp[64];
847  int i;
848  for (i = 0; i < 8; i++)
849  memcpy(tmp + i*8, src + i*stride, 8);
850  for (i = 0; i < 8; i++)
851  memcpy(dst + i*stride, tmp + i*8, 8);
852 }
853 
855  int plane_idx, int is_key, int is_chroma)
856 {
857  int blk, ret;
858  int i, j, bx, by;
859  uint8_t *dst, *ref, *ref_start, *ref_end;
860  int v, col[2];
861  const uint8_t *scan;
862  int xoff, yoff;
863  LOCAL_ALIGNED_32(int16_t, block, [64]);
864  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
865  int coordmap[64];
866  int ybias = is_key ? -15 : 0;
867  int qp, quant_idx, coef_count, coef_idx[64];
868 
869  const int stride = frame->linesize[plane_idx];
870  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
871  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
872 
874  ref_start = frame->data[plane_idx];
875  ref_end = frame->data[plane_idx] + ((bh - 1) * frame->linesize[plane_idx] + bw - 1) * 8;
876 
877  for (i = 0; i < 64; i++)
878  coordmap[i] = (i & 7) + (i >> 3) * stride;
879 
880  for (by = 0; by < bh; by++) {
881  for (i = 0; i < BINKB_NB_SRC; i++) {
882  if ((ret = binkb_read_bundle(c, gb, i)) < 0)
883  return ret;
884  }
885 
886  dst = frame->data[plane_idx] + 8*by*stride;
887  for (bx = 0; bx < bw; bx++, dst += 8) {
889  switch (blk) {
890  case 0:
891  break;
892  case 1:
893  scan = bink_patterns[get_bits(gb, 4)];
894  i = 0;
895  do {
896  int mode, run;
897 
898  mode = get_bits1(gb);
899  run = get_bits(gb, binkb_runbits[i]) + 1;
900 
901  i += run;
902  if (i > 64) {
903  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
904  return AVERROR_INVALIDDATA;
905  }
906  if (mode) {
908  for (j = 0; j < run; j++)
909  dst[coordmap[*scan++]] = v;
910  } else {
911  for (j = 0; j < run; j++)
912  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
913  }
914  } while (i < 63);
915  if (i == 63)
916  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
917  break;
918  case 2:
919  memset(dctblock, 0, sizeof(*dctblock) * 64);
920  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
922  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
923  return quant_idx;
924  unquantize_dct_coeffs(dctblock, binkb_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
925  c->binkdsp.idct_put(dst, stride, dctblock);
926  break;
927  case 3:
929  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
930  ref = dst + xoff + yoff * stride;
931  if (ref < ref_start || ref > ref_end) {
932  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
933  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
934  c->put_pixels_tab(dst, ref, stride, 8);
935  } else {
937  }
938  c->bdsp.clear_block(block);
940  read_residue(gb, block, v);
941  c->binkdsp.add_pixels8(dst, block, stride);
942  break;
943  case 4:
945  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
946  ref = dst + xoff + yoff * stride;
947  if (ref < ref_start || ref > ref_end) {
948  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
949  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
950  c->put_pixels_tab(dst, ref, stride, 8);
951  } else {
953  }
954  memset(dctblock, 0, sizeof(*dctblock) * 64);
955  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
957  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
958  return quant_idx;
959  unquantize_dct_coeffs(dctblock, binkb_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
960  c->binkdsp.idct_add(dst, stride, dctblock);
961  break;
962  case 5:
964  c->bdsp.fill_block_tab[1](dst, v, stride, 8);
965  break;
966  case 6:
967  for (i = 0; i < 2; i++)
969  for (i = 0; i < 8; i++) {
971  for (j = 0; j < 8; j++, v >>= 1)
972  dst[i*stride + j] = col[v & 1];
973  }
974  break;
975  case 7:
977  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
978  ref = dst + xoff + yoff * stride;
979  if (ref < ref_start || ref > ref_end) {
980  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
981  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
982  c->put_pixels_tab(dst, ref, stride, 8);
983  } else {
985  }
986  break;
987  case 8:
988  for (i = 0; i < 8; i++)
989  memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
990  c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
991  break;
992  default:
993  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
994  return AVERROR_INVALIDDATA;
995  }
996  }
997  }
998  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
999  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1000 
1001  return 0;
1002 }
1003 
1005  uint8_t *dst, uint8_t *prev, int stride,
1006  uint8_t *ref_start,
1007  uint8_t *ref_end)
1008 {
1009  int xoff = get_value(c, BINK_SRC_X_OFF);
1010  int yoff = get_value(c, BINK_SRC_Y_OFF);
1011  uint8_t *ref = prev + xoff + yoff * stride;
1012  if (ref < ref_start || ref > ref_end) {
1013  av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1014  xoff, yoff);
1015  return AVERROR_INVALIDDATA;
1016  }
1017  c->put_pixels_tab(dst, ref, stride, 8);
1018 
1019  return 0;
1020 }
1021 
1023  int plane_idx, int is_chroma)
1024 {
1025  int blk, ret;
1026  int i, j, bx, by;
1027  uint8_t *dst, *prev, *ref_start, *ref_end;
1028  int v, col[2];
1029  const uint8_t *scan;
1030  LOCAL_ALIGNED_32(int16_t, block, [64]);
1031  LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
1032  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
1033  int coordmap[64], quant_idx, coef_count, coef_idx[64];
1034 
1035  const int stride = frame->linesize[plane_idx];
1036  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
1037  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
1038  int width = c->avctx->width >> is_chroma;
1039  int height = c->avctx->height >> is_chroma;
1040 
1041  if (c->version == 'k' && get_bits1(gb)) {
1042  int fill = get_bits(gb, 8);
1043 
1044  dst = frame->data[plane_idx];
1045 
1046  for (i = 0; i < height; i++)
1047  memset(dst + i * stride, fill, width);
1048  goto end;
1049  }
1050 
1051  init_lengths(c, FFMAX(width, 8), bw);
1052  for (i = 0; i < BINK_NB_SRC; i++) {
1053  ret = read_bundle(gb, c, i);
1054  if (ret < 0)
1055  return ret;
1056  }
1057 
1058  ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
1059  : frame->data[plane_idx];
1060  ref_end = ref_start
1061  + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
1062 
1063  for (i = 0; i < 64; i++)
1064  coordmap[i] = (i & 7) + (i >> 3) * stride;
1065 
1066  for (by = 0; by < bh; by++) {
1067  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
1068  return ret;
1069  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
1070  return ret;
1071  if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
1072  return ret;
1073  if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
1074  return ret;
1075  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
1076  return ret;
1077  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
1078  return ret;
1079  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
1080  return ret;
1081  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
1082  return ret;
1083  if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
1084  return ret;
1085 
1086  dst = frame->data[plane_idx] + 8*by*stride;
1087  prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
1088  : frame->data[plane_idx]) + 8*by*stride;
1089  for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1091  // 16x16 block type on odd line means part of the already decoded block, so skip it
1092  if (((by & 1) || (bx & 1)) && blk == SCALED_BLOCK) {
1093  bx++;
1094  dst += 8;
1095  prev += 8;
1096  continue;
1097  }
1098  switch (blk) {
1099  case SKIP_BLOCK:
1100  c->put_pixels_tab(dst, prev, stride, 8);
1101  break;
1102  case SCALED_BLOCK:
1104  switch (blk) {
1105  case RUN_BLOCK:
1106  if (get_bits_left(gb) < 4)
1107  return AVERROR_INVALIDDATA;
1108  scan = bink_patterns[get_bits(gb, 4)];
1109  i = 0;
1110  do {
1111  int run = get_value(c, BINK_SRC_RUN) + 1;
1112 
1113  i += run;
1114  if (i > 64) {
1115  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1116  return AVERROR_INVALIDDATA;
1117  }
1118  if (get_bits1(gb)) {
1119  v = get_value(c, BINK_SRC_COLORS);
1120  for (j = 0; j < run; j++)
1121  ublock[*scan++] = v;
1122  } else {
1123  for (j = 0; j < run; j++)
1124  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1125  }
1126  } while (i < 63);
1127  if (i == 63)
1128  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1129  break;
1130  case INTRA_BLOCK:
1131  memset(dctblock, 0, sizeof(*dctblock) * 64);
1132  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1133  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1134  return quant_idx;
1135  unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1136  c->binkdsp.idct_put(ublock, 8, dctblock);
1137  break;
1138  case FILL_BLOCK:
1139  v = get_value(c, BINK_SRC_COLORS);
1140  c->bdsp.fill_block_tab[0](dst, v, stride, 16);
1141  break;
1142  case PATTERN_BLOCK:
1143  for (i = 0; i < 2; i++)
1144  col[i] = get_value(c, BINK_SRC_COLORS);
1145  for (j = 0; j < 8; j++) {
1147  for (i = 0; i < 8; i++, v >>= 1)
1148  ublock[i + j*8] = col[v & 1];
1149  }
1150  break;
1151  case RAW_BLOCK:
1152  for (j = 0; j < 8; j++)
1153  for (i = 0; i < 8; i++)
1154  ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1155  break;
1156  default:
1157  av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1158  return AVERROR_INVALIDDATA;
1159  }
1160  if (blk != FILL_BLOCK)
1161  c->binkdsp.scale_block(ublock, dst, stride);
1162  bx++;
1163  dst += 8;
1164  prev += 8;
1165  break;
1166  case MOTION_BLOCK:
1167  ret = bink_put_pixels(c, dst, prev, stride,
1168  ref_start, ref_end);
1169  if (ret < 0)
1170  return ret;
1171  break;
1172  case RUN_BLOCK:
1173  scan = bink_patterns[get_bits(gb, 4)];
1174  i = 0;
1175  do {
1176  int run = get_value(c, BINK_SRC_RUN) + 1;
1177 
1178  i += run;
1179  if (i > 64) {
1180  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1181  return AVERROR_INVALIDDATA;
1182  }
1183  if (get_bits1(gb)) {
1184  v = get_value(c, BINK_SRC_COLORS);
1185  for (j = 0; j < run; j++)
1186  dst[coordmap[*scan++]] = v;
1187  } else {
1188  for (j = 0; j < run; j++)
1189  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1190  }
1191  } while (i < 63);
1192  if (i == 63)
1193  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1194  break;
1195  case RESIDUE_BLOCK:
1196  ret = bink_put_pixels(c, dst, prev, stride,
1197  ref_start, ref_end);
1198  if (ret < 0)
1199  return ret;
1200  c->bdsp.clear_block(block);
1201  v = get_bits(gb, 7);
1202  read_residue(gb, block, v);
1203  c->binkdsp.add_pixels8(dst, block, stride);
1204  break;
1205  case INTRA_BLOCK:
1206  memset(dctblock, 0, sizeof(*dctblock) * 64);
1207  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1208  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1209  return quant_idx;
1210  unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1211  c->binkdsp.idct_put(dst, stride, dctblock);
1212  break;
1213  case FILL_BLOCK:
1214  v = get_value(c, BINK_SRC_COLORS);
1215  c->bdsp.fill_block_tab[1](dst, v, stride, 8);
1216  break;
1217  case INTER_BLOCK:
1218  ret = bink_put_pixels(c, dst, prev, stride,
1219  ref_start, ref_end);
1220  if (ret < 0)
1221  return ret;
1222  memset(dctblock, 0, sizeof(*dctblock) * 64);
1223  dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1224  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1225  return quant_idx;
1226  unquantize_dct_coeffs(dctblock, bink_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
1227  c->binkdsp.idct_add(dst, stride, dctblock);
1228  break;
1229  case PATTERN_BLOCK:
1230  for (i = 0; i < 2; i++)
1231  col[i] = get_value(c, BINK_SRC_COLORS);
1232  for (i = 0; i < 8; i++) {
1234  for (j = 0; j < 8; j++, v >>= 1)
1235  dst[i*stride + j] = col[v & 1];
1236  }
1237  break;
1238  case RAW_BLOCK:
1239  for (i = 0; i < 8; i++)
1240  memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1241  c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1242  break;
1243  default:
1244  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1245  return AVERROR_INVALIDDATA;
1246  }
1247  }
1248  }
1249 
1250 end:
1251  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1252  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1253 
1254  return 0;
1255 }
1256 
1258  int *got_frame, AVPacket *pkt)
1259 {
1260  BinkContext * const c = avctx->priv_data;
1261  GetBitContext gb;
1262  int plane, plane_idx, ret;
1263  int bits_count = pkt->size << 3;
1264 
1265  if (c->version > 'b') {
1266  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1267  return ret;
1268  } else {
1269  if ((ret = ff_reget_buffer(avctx, c->last, 0)) < 0)
1270  return ret;
1271  if ((ret = av_frame_ref(frame, c->last)) < 0)
1272  return ret;
1273  }
1274 
1275  init_get_bits(&gb, pkt->data, bits_count);
1276  if (c->has_alpha) {
1277  if (c->version >= 'i')
1278  skip_bits_long(&gb, 32);
1279  if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
1280  return ret;
1281  }
1282  if (c->version >= 'i')
1283  skip_bits_long(&gb, 32);
1284 
1285  c->frame_num++;
1286 
1287  for (plane = 0; plane < 3; plane++) {
1288  plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1289 
1290  if (c->version > 'b') {
1291  if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
1292  return ret;
1293  } else {
1294  if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
1295  c->frame_num == 1, !!plane)) < 0)
1296  return ret;
1297  }
1298  if (get_bits_count(&gb) >= bits_count)
1299  break;
1300  }
1301 
1302  if (c->version > 'b') {
1303  if ((ret = av_frame_replace(c->last, frame)) < 0)
1304  return ret;
1305  }
1306 
1307  *got_frame = 1;
1308 
1309  /* always report that the buffer was completely consumed */
1310  return pkt->size;
1311 }
1312 
1313 static av_cold void bink_init_vlcs(void)
1314 {
1315  for (int i = 0, offset = 0; i < 16; i++) {
1316  static VLCElem table[976];
1317  const int maxbits = bink_tree_lens[i][15];
1318  bink_trees[i].table = table + offset;
1319  bink_trees[i].table_allocated = 1 << maxbits;
1321  vlc_init(&bink_trees[i], maxbits, 16,
1322  bink_tree_lens[i], 1, 1,
1324  }
1325 }
1326 
1327 /**
1328  * Calculate quantization tables for version b
1329  */
1330 static av_cold void binkb_calc_quant(void)
1331 {
1332  uint8_t inv_bink_scan[64];
1333  static const int s[64]={
1334  1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1335  1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1336  1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1337  1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1338  1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1339  843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1340  581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1341  296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1342  };
1343  int i, j;
1344 #define C (1LL<<30)
1345  for (i = 0; i < 64; i++)
1346  inv_bink_scan[bink_scan[i]] = i;
1347 
1348  for (j = 0; j < 16; j++) {
1349  for (i = 0; i < 64; i++) {
1350  int k = inv_bink_scan[i];
1352  binkb_num[j]/(binkb_den[j] * (C>>12));
1354  binkb_num[j]/(binkb_den[j] * (C>>12));
1355  }
1356  }
1357 }
1358 
1360 {
1361  static AVOnce init_static_once = AV_ONCE_INIT;
1362  BinkContext * const c = avctx->priv_data;
1363  HpelDSPContext hdsp;
1364  int ret;
1365  int flags;
1366 
1367  c->version = avctx->codec_tag >> 24;
1368  if (avctx->extradata_size < 4) {
1369  av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1370  return AVERROR_INVALIDDATA;
1371  }
1372  flags = AV_RL32(avctx->extradata);
1373  c->has_alpha = flags & BINK_FLAG_ALPHA;
1374  c->swap_planes = c->version >= 'h';
1375  c->avctx = avctx;
1376 
1377  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1378  return ret;
1379 
1380  c->last = av_frame_alloc();
1381  if (!c->last)
1382  return AVERROR(ENOMEM);
1383 
1384  avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1385  avctx->color_range = c->version == 'k' ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
1386 
1387  ff_blockdsp_init(&c->bdsp);
1388  ff_hpeldsp_init(&hdsp, avctx->flags);
1389  c->put_pixels_tab = hdsp.put_pixels_tab[1][0];
1390  ff_binkdsp_init(&c->binkdsp);
1391 
1392  if ((ret = init_bundles(c)) < 0)
1393  return ret;
1394 
1395  if (c->version == 'b') {
1396  static AVOnce binkb_init_once = AV_ONCE_INIT;
1397  ff_thread_once(&binkb_init_once, binkb_calc_quant);
1398  }
1399  ff_thread_once(&init_static_once, bink_init_vlcs);
1400 
1401  return 0;
1402 }
1403 
1405 {
1406  BinkContext * const c = avctx->priv_data;
1407 
1408  av_frame_free(&c->last);
1409 
1410  free_bundles(c);
1411  return 0;
1412 }
1413 
1414 static av_cold void flush(AVCodecContext *avctx)
1415 {
1416  BinkContext * const c = avctx->priv_data;
1417 
1418  c->frame_num = 0;
1419 }
1420 
1422  .p.name = "binkvideo",
1423  CODEC_LONG_NAME("Bink video"),
1424  .p.type = AVMEDIA_TYPE_VIDEO,
1425  .p.id = AV_CODEC_ID_BINKVIDEO,
1426  .priv_data_size = sizeof(BinkContext),
1427  .init = decode_init,
1428  .close = decode_end,
1430  .flush = flush,
1431  .p.capabilities = AV_CODEC_CAP_DR1,
1432  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1433 };
Bundle::tree
Tree tree
Huffman tree-related data.
Definition: bink.c:106
flags
const SwsFlags flags[]
Definition: swscale.c:72
C
#define C
ff_binkdsp_init
av_cold void ff_binkdsp_init(BinkDSPContext *c)
Definition: binkdsp.c:152
binkb_decode_plane
static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, int plane_idx, int is_key, int is_chroma)
Definition: bink.c:854
init_bundles
static av_cold int init_bundles(BinkContext *c)
Allocate memory for bundles.
Definition: bink.c:180
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:280
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
Bundle::cur_dec
uint8_t * cur_dec
pointer to the not yet decoded part of the buffer
Definition: bink.c:109
put_pixels8x8_overlapped
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:844
BINKB_SRC_Y_OFF
@ BINKB_SRC_Y_OFF
Y components of motion value.
Definition: bink.c:52
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
blockdsp.h
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:688
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
flush
static av_cold void flush(AVCodecContext *avctx)
Definition: bink.c:1414
mem_internal.h
init_lengths
static void init_lengths(BinkContext *c, int width, int bw)
Initialize length in all bundles.
Definition: bink.c:155
out
static FILE * out
Definition: movenc.c:55
INTER_BLOCK
@ INTER_BLOCK
motion block with DCT applied to the difference
Definition: bink.c:143
bink_scan
static const uint8_t bink_scan[64]
Bink DCT and residue 8x8 block scan order.
Definition: binkdata.h:28
thread.h
DC_START_BITS
#define DC_START_BITS
number of bits used to store first DC value in bundle
Definition: bink.c:501
BinkContext::col_high
Tree col_high[16]
trees for decoding high nibble in "colours" data type
Definition: bink.c:128
int64_t
long long int64_t
Definition: coverity.c:34
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:254
read_patterns
static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:436
mask
int mask
Definition: mediacodecdec_common.c:154
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
merge
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:220
INTRA_BLOCK
@ INTRA_BLOCK
intra DCT block
Definition: bink.c:141
ff_bink_decoder
const FFCodec ff_bink_decoder
Definition: bink.c:1421
mode
Definition: swscale.c:60
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:435
u
#define u(width, name, range_min, range_max)
Definition: cbs_apv.c:68
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:777
AVPacket::data
uint8_t * data
Definition: packet.h:595
b
#define b
Definition: input.c:43
table
static const uint16_t table[]
Definition: prosumer.c:203
FFCodec
Definition: codec_internal.h:127
Tree::vlc_num
int vlc_num
tree number (in bink_trees[])
Definition: bink.c:94
binkb_bundle_signed
static const uint8_t binkb_bundle_signed[BINKB_NB_SRC]
Definition: bink.c:66
binkb_inter_seed
static const uint8_t binkb_inter_seed[64]
Definition: binkdata.h:636
BINK_SRC_BLOCK_TYPES
@ BINK_SRC_BLOCK_TYPES
8x8 block types
Definition: bink.c:77
BINKB_SRC_INTER_DC
@ BINKB_SRC_INTER_DC
DC values for interblocks with DCT.
Definition: bink.c:54
BINK_SRC_X_OFF
@ BINK_SRC_X_OFF
X components of motion value.
Definition: bink.c:81
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
binkb_den
static const uint8_t binkb_den[16]
Definition: binkdata.h:651
BlockDSPContext
Definition: blockdsp.h:32
read_block_types
static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:391
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:517
binkb_intra_quant
static int32_t binkb_intra_quant[16][64]
Definition: bink.c:70
binkb_num
static const uint8_t binkb_num[16]
Definition: binkdata.h:647
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:197
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
BinkContext::put_pixels_tab
op_pixels_func put_pixels_tab
Definition: bink.c:119
BinkContext::col_lastval
int col_lastval
value of last decoded high nibble in "colours" data type
Definition: bink.c:129
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
BinkContext::frame_num
unsigned frame_num
Definition: bink.c:125
bink_decode_plane
static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, int plane_idx, int is_chroma)
Definition: bink.c:1022
GetBitContext
Definition: get_bits.h:109
OldSources
OldSources
IDs for different data types used in old version of Bink video codec.
Definition: bink.c:47
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:496
val
static double val(void *priv, double ch)
Definition: aeval.c:77
bink_put_pixels
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:1004
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
bink_trees
static VLC bink_trees[16]
Definition: bink.c:42
quant
static const uint8_t quant[64]
Definition: vmixdec.c:71
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:119
binkb_read_bundle
static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
Definition: bink.c:584
binkb_intra_seed
static const uint8_t binkb_intra_seed[64]
Definition: binkdata.h:625
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:523
BinkDSPContext
Definition: binkdsp.h:32
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:347
ff_blockdsp_init
av_cold void ff_blockdsp_init(BlockDSPContext *c)
Definition: blockdsp.c:58
s
#define s(width, name)
Definition: cbs_vp9.c:198
VLC_INIT_LE
#define VLC_INIT_LE
Definition: vlc.h:197
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:108
FILL_BLOCK
@ FILL_BLOCK
block is filled with single colour
Definition: bink.c:142
binkb_bundle_sizes
static const uint8_t binkb_bundle_sizes[BINKB_NB_SRC]
Definition: bink.c:62
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:411
bink_init_vlcs
static av_cold void bink_init_vlcs(void)
Definition: bink.c:1313
BINKB_SRC_INTRA_DC
@ BINKB_SRC_INTRA_DC
DC values for intrablocks with DCT.
Definition: bink.c:53
bits
uint8_t bits
Definition: vp3data.h:128
LOCAL_ALIGNED_16
#define LOCAL_ALIGNED_16(t, v,...)
Definition: mem_internal.h:130
vlc_init
#define vlc_init(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:70
read_dcs
static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b, int start_bits, int has_sign)
Definition: bink.c:503
decode.h
get_bits.h
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
blk
#define blk(i)
Definition: sha.c:186
BINK_FLAG_ALPHA
#define BINK_FLAG_ALPHA
Definition: bink.c:39
av_fallthrough
#define av_fallthrough
Definition: attributes.h:67
BinkContext::has_alpha
int has_alpha
Definition: bink.c:123
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
binkb_get_value
static int binkb_get_value(BinkContext *c, int bundle_num)
Definition: bink.c:618
if
if(ret)
Definition: filter_design.txt:179
BINK_SRC_Y_OFF
@ BINK_SRC_Y_OFF
Y components of motion value.
Definition: bink.c:82
BINK_SRC_RUN
@ BINK_SRC_RUN
run lengths for special fill block
Definition: bink.c:85
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
NULL
#define NULL
Definition: coverity.c:32
LOCAL_ALIGNED_32
#define LOCAL_ALIGNED_32(t, v,...)
Definition: mem_internal.h:132
RUN_BLOCK
@ RUN_BLOCK
block is composed from runs of colours with custom scan order
Definition: bink.c:139
run
uint8_t run
Definition: svq3.c:207
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:677
PATTERN_BLOCK
@ PATTERN_BLOCK
block is filled with two colours following custom pattern
Definition: bink.c:144
read_runs
static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:331
BINKB_NB_SRC
@ BINKB_NB_SRC
Definition: bink.c:59
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
BINK_SRC_PATTERN
@ BINK_SRC_PATTERN
8-bit values for 2-colour pattern fill
Definition: bink.c:80
Bundle::data
uint8_t * data
buffer for decoded symbols
Definition: bink.c:107
Bundle::len
int len
length of number of entries to decode (in bits)
Definition: bink.c:105
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: bink.c:1257
read_bundle
static int read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
Prepare bundle for decoding data.
Definition: bink.c:292
BlockTypes
BlockTypes
Bink video block types.
Definition: bink.c:135
get_value
static int get_value(BinkContext *c, int bundle)
Retrieve next value from bundle.
Definition: bink.c:557
op_pixels_func
void(* op_pixels_func)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
Average and put pixel Widths can be 16, 8, 4 or 2.
Definition: hpeldsp.h:39
RESIDUE_BLOCK
@ RESIDUE_BLOCK
motion block with some difference added
Definition: bink.c:140
AVOnce
#define AVOnce
Definition: thread.h:202
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
Bundle
data structure used for decoding single Bink data type
Definition: bink.c:104
HpelDSPContext
Half-pel DSP context.
Definition: hpeldsp.h:46
VLC::table_allocated
int table_allocated
Definition: vlc.h:53
read_dct_coeffs
static int read_dct_coeffs(BinkContext *c, GetBitContext *gb, int32_t block[64], const uint8_t *scan, int *coef_count_, int coef_idx[64], int q)
Read 8x8 block of DCT coefficients.
Definition: bink.c:641
HpelDSPContext::put_pixels_tab
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:57
binkdata.h
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1765
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:551
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:596
height
#define height
Definition: dsp.h:89
av_frame_ref
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:278
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: bink.c:1404
BINKB_SRC_COLORS
@ BINKB_SRC_COLORS
pixel values used for different block types
Definition: bink.c:49
binkb_calc_quant
static av_cold void binkb_calc_quant(void)
Calculate quantization tables for version b.
Definition: bink.c:1330
BINKB_SRC_INTER_COEFS
@ BINKB_SRC_INTER_COEFS
number of coefficients for residue blocks
Definition: bink.c:57
size
int size
Definition: twinvq_data.h:10344
VLCElem
Definition: vlc.h:32
BinkContext::last
AVFrame * last
Definition: bink.c:121
Bundle::data_end
uint8_t * data_end
buffer end
Definition: bink.c:108
BinkContext::version
int version
internal Bink file version
Definition: bink.c:122
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
attributes.h
MOTION_BLOCK
@ MOTION_BLOCK
block is copied from previous frame with some offset
Definition: bink.c:138
binkdsp.h
Tree
data needed to decode 4-bit Huffman-coded value
Definition: bink.c:93
BinkContext
Definition: bink.c:116
BINK_SRC_INTRA_DC
@ BINK_SRC_INTRA_DC
DC values for intrablocks with DCT.
Definition: bink.c:83
BINKB_SRC_X_OFF
@ BINKB_SRC_X_OFF
X components of motion value.
Definition: bink.c:51
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:522
src2
const pixel * src2
Definition: h264pred_template.c:421
BINK_SRC_COLORS
@ BINK_SRC_COLORS
pixel values used for different block types
Definition: bink.c:79
bink_rlelens
static const uint8_t bink_rlelens[4]
Definition: bink.c:389
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
binkb_runbits
static const uint8_t binkb_runbits[64]
Definition: binkdata.h:614
BINKB_SRC_INTRA_Q
@ BINKB_SRC_INTRA_Q
quantizer values for intrablocks with DCT
Definition: bink.c:55
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:600
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:639
BinkContext::bundle
Bundle bundle[BINKB_NB_SRC]
bundles for decoding all data types
Definition: bink.c:127
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:760
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
avcodec.h
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1894
ret
ret
Definition: filter_design.txt:187
BinkContext::swap_planes
int swap_planes
Definition: bink.c:124
bink_intra_quant
static const int32_t bink_intra_quant[16][64]
Definition: binkdata.h:288
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
Tree::syms
uint8_t syms[16]
leaf value to symbol mapping
Definition: bink.c:95
free_bundles
static av_cold void free_bundles(BinkContext *c)
Free memory used by bundles.
Definition: bink.c:207
BinkContext::binkdsp
BinkDSPContext binkdsp
Definition: bink.c:120
bink_inter_quant
static const int32_t bink_inter_quant[16][64]
Definition: binkdata.h:451
binkb_inter_quant
static int32_t binkb_inter_quant[16][64]
Definition: bink.c:71
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
read_colors
static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
Definition: bink.c:458
av_frame_replace
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition: frame.c:376
AVCodecContext
main external API structure.
Definition: avcodec.h:439
read_tree
static int read_tree(GetBitContext *gb, Tree *tree)
Read information about Huffman tree used to decode data.
Definition: bink.c:247
SKIP_BLOCK
@ SKIP_BLOCK
skipped block
Definition: bink.c:136
binkb_init_bundle
static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
Definition: bink.c:570
AV_CODEC_ID_BINKVIDEO
@ AV_CODEC_ID_BINKVIDEO
Definition: codec_id.h:187
unquantize_dct_coeffs
static void unquantize_dct_coeffs(int32_t block[64], const uint32_t quant[64], int coef_count, int coef_idx[64], const uint8_t *scan)
Definition: bink.c:738
mode
mode
Definition: ebur128.h:83
BINK_NB_SRC
@ BINK_NB_SRC
Definition: bink.c:87
VLC
Definition: vlc.h:50
RAW_BLOCK
@ RAW_BLOCK
uncoded 8x8 block
Definition: bink.c:145
CHECK_READ_VAL
#define CHECK_READ_VAL(gb, b, t)
common check before starting decoding bundle data
Definition: bink.c:322
BINKB_SRC_INTER_Q
@ BINKB_SRC_INTER_Q
quantizer values for interblocks with DCT
Definition: bink.c:56
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
Bundle::cur_ptr
uint8_t * cur_ptr
pointer to the data that is not read from buffer yet
Definition: bink.c:110
VLC::table
VLCElem * table
Definition: vlc.h:52
binkb_init_bundles
static av_cold void binkb_init_bundles(BinkContext *c)
Definition: bink.c:577
BINK_SRC_SUB_BLOCK_TYPES
@ BINK_SRC_SUB_BLOCK_TYPES
16x16 block types (a subset of 8x8 block types)
Definition: bink.c:78
read_residue
static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
Read 8x8 block with residue after motion compensation.
Definition: bink.c:758
BINK_SRC_INTER_DC
@ BINK_SRC_INTER_DC
DC values for interblocks with DCT.
Definition: bink.c:84
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
mem.h
BinkContext::avctx
AVCodecContext * avctx
Definition: bink.c:117
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:464
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:572
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:466
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
bink_patterns
static const uint8_t bink_patterns[16][64]
Definition: binkdata.h:125
SCALED_BLOCK
@ SCALED_BLOCK
block has size 16x16
Definition: bink.c:137
bink_tree_lens
static const uint8_t bink_tree_lens[16][16]
Definition: binkdata.h:106
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:600
BinkContext::bdsp
BlockDSPContext bdsp
Definition: bink.c:118
int32_t
int32_t
Definition: audioconvert.c:56
imgutils.h
hpeldsp.h
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
VLC_INIT_USE_STATIC
#define VLC_INIT_USE_STATIC
Definition: vlc.h:190
stride
#define stride
Definition: h264pred_template.c:536
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
av_image_check_size
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:318
width
#define width
Definition: dsp.h:89
BINKB_SRC_PATTERN
@ BINKB_SRC_PATTERN
8-bit values for 2-colour pattern fill
Definition: bink.c:50
read_motion_values
static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:355
Sources
Sources
IDs for different data types used in Bink video codec.
Definition: bink.c:76
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
src
#define src
Definition: vp8dsp.c:248
ff_hpeldsp_init
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:337
bink_tree_bits
static const uint8_t bink_tree_bits[16][16]
Definition: binkdata.h:39
BINKB_SRC_BLOCK_TYPES
@ BINKB_SRC_BLOCK_TYPES
8x8 block types
Definition: bink.c:48
GET_HUFF
#define GET_HUFF(gb, tree)
Definition: bink.c:98
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: bink.c:1359