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/emms.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/mem_internal.h"
28 #include "libavutil/thread.h"
29 
30 #define BITSTREAM_READER_LE
31 #include "avcodec.h"
32 #include "binkdata.h"
33 #include "binkdsp.h"
34 #include "blockdsp.h"
35 #include "codec_internal.h"
36 #include "decode.h"
37 #include "get_bits.h"
38 #include "hpeldsp.h"
39 
40 #define BINK_FLAG_ALPHA 0x00100000
41 #define BINK_FLAG_GRAY 0x00020000
42 
43 static VLC bink_trees[16];
44 
45 /**
46  * IDs for different data types used in old version of Bink video codec
47  */
48 enum OldSources {
49  BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
50  BINKB_SRC_COLORS, ///< pixel values used for different block types
51  BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
52  BINKB_SRC_X_OFF, ///< X components of motion value
53  BINKB_SRC_Y_OFF, ///< Y components of motion value
54  BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
55  BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
56  BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
57  BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
58  BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
59 
61 };
62 
63 static const uint8_t binkb_bundle_sizes[BINKB_NB_SRC] = {
64  4, 8, 8, 5, 5, 11, 11, 4, 4, 7
65 };
66 
67 static const uint8_t binkb_bundle_signed[BINKB_NB_SRC] = {
68  0, 0, 0, 1, 1, 0, 1, 0, 0, 0
69 };
70 
71 static int32_t binkb_intra_quant[16][64];
72 static int32_t binkb_inter_quant[16][64];
73 
74 /**
75  * IDs for different data types used in Bink video codec
76  */
77 enum Sources {
78  BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
79  BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
80  BINK_SRC_COLORS, ///< pixel values used for different block types
81  BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
82  BINK_SRC_X_OFF, ///< X components of motion value
83  BINK_SRC_Y_OFF, ///< Y components of motion value
84  BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
85  BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
86  BINK_SRC_RUN, ///< run lengths for special fill block
87 
89 };
90 
91 /**
92  * data needed to decode 4-bit Huffman-coded value
93  */
94 typedef struct Tree {
95  int vlc_num; ///< tree number (in bink_trees[])
96  uint8_t syms[16]; ///< leaf value to symbol mapping
97 } Tree;
98 
99 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
100  bink_trees[(tree).vlc_num].bits, 1)]
101 
102 /**
103  * data structure used for decoding single Bink data type
104  */
105 typedef struct Bundle {
106  int len; ///< length of number of entries to decode (in bits)
107  Tree tree; ///< Huffman tree-related data
108  uint8_t *data; ///< buffer for decoded symbols
109  uint8_t *data_end; ///< buffer end
110  uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
111  uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
112 } Bundle;
113 
114 /*
115  * Decoder context
116  */
117 typedef struct BinkContext {
123  int version; ///< internal Bink file version
126  unsigned frame_num;
127 
128  Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
129  Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
130  int col_lastval; ///< value of last decoded high nibble in "colours" data type
131 } BinkContext;
132 
133 /**
134  * Bink video block types
135  */
137  SKIP_BLOCK = 0, ///< skipped block
138  SCALED_BLOCK, ///< block has size 16x16
139  MOTION_BLOCK, ///< block is copied from previous frame with some offset
140  RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
141  RESIDUE_BLOCK, ///< motion block with some difference added
142  INTRA_BLOCK, ///< intra DCT block
143  FILL_BLOCK, ///< block is filled with single colour
144  INTER_BLOCK, ///< motion block with DCT applied to the difference
145  PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
146  RAW_BLOCK, ///< uncoded 8x8 block
147 };
148 
149 /**
150  * Initialize length in all bundles.
151  *
152  * @param c decoder context
153  * @param width plane width
154  * @param bw plane width in 8x8 blocks
155  */
156 static void init_lengths(BinkContext *c, int width, int bw)
157 {
158  width = FFALIGN(width, 8);
159 
160  c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
161 
162  c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
163 
164  c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
165 
166  c->bundle[BINK_SRC_INTRA_DC].len =
167  c->bundle[BINK_SRC_INTER_DC].len =
168  c->bundle[BINK_SRC_X_OFF].len =
169  c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
170 
171  c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
172 
173  c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
174 }
175 
176 /**
177  * Allocate memory for bundles.
178  *
179  * @param c decoder context
180  */
182 {
183  int bw, bh, blocks;
184  uint8_t *tmp;
185  int i;
186 
187  bw = (c->avctx->width + 7) >> 3;
188  bh = (c->avctx->height + 7) >> 3;
189  blocks = bw * bh;
190 
191  tmp = av_calloc(blocks, 64 * BINKB_NB_SRC);
192  if (!tmp)
193  return AVERROR(ENOMEM);
194  for (i = 0; i < BINKB_NB_SRC; i++) {
195  c->bundle[i].data = tmp;
196  tmp += blocks * 64;
197  c->bundle[i].data_end = tmp;
198  }
199 
200  return 0;
201 }
202 
203 /**
204  * Free memory used by bundles.
205  *
206  * @param c decoder context
207  */
209 {
210  av_freep(&c->bundle[0].data);
211 }
212 
213 /**
214  * Merge two consequent lists of equal size depending on bits read.
215  *
216  * @param gb context for reading bits
217  * @param dst buffer where merged list will be written to
218  * @param src pointer to the head of the first list (the second lists starts at src+size)
219  * @param size input lists size
220  */
221 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
222 {
223  uint8_t *src2 = src + size;
224  int size2 = size;
225 
226  do {
227  if (!get_bits1(gb)) {
228  *dst++ = *src++;
229  size--;
230  } else {
231  *dst++ = *src2++;
232  size2--;
233  }
234  } while (size && size2);
235 
236  while (size--)
237  *dst++ = *src++;
238  while (size2--)
239  *dst++ = *src2++;
240 }
241 
242 /**
243  * Read information about Huffman tree used to decode data.
244  *
245  * @param gb context for reading bits
246  * @param tree pointer for storing tree data
247  */
248 static int read_tree(GetBitContext *gb, Tree *tree)
249 {
250  uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
251  int i, t, len;
252 
253  if (get_bits_left(gb) < 4)
254  return AVERROR_INVALIDDATA;
255 
256  tree->vlc_num = get_bits(gb, 4);
257  if (!tree->vlc_num) {
258  for (i = 0; i < 16; i++)
259  tree->syms[i] = i;
260  return 0;
261  }
262  if (get_bits1(gb)) {
263  len = get_bits(gb, 3);
264  for (i = 0; i <= len; i++) {
265  tree->syms[i] = get_bits(gb, 4);
266  tmp1[tree->syms[i]] = 1;
267  }
268  for (i = 0; i < 16 && len < 16 - 1; i++)
269  if (!tmp1[i])
270  tree->syms[++len] = i;
271  } else {
272  len = get_bits(gb, 2);
273  for (i = 0; i < 16; i++)
274  in[i] = i;
275  for (i = 0; i <= len; i++) {
276  int size = 1 << i;
277  for (t = 0; t < 16; t += size << 1)
278  merge(gb, out + t, in + t, size);
279  FFSWAP(uint8_t*, in, out);
280  }
281  memcpy(tree->syms, in, 16);
282  }
283  return 0;
284 }
285 
286 /**
287  * Prepare bundle for decoding data.
288  *
289  * @param gb context for reading bits
290  * @param c decoder context
291  * @param bundle_num number of the bundle to initialize
292  */
293 static int read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
294 {
295  int i;
296 
297  if (bundle_num == BINK_SRC_COLORS) {
298  for (i = 0; i < 16; i++) {
299  int ret = read_tree(gb, &c->col_high[i]);
300  if (ret < 0)
301  return ret;
302  }
303  c->col_lastval = 0;
304  }
305  if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC) {
306  int ret = read_tree(gb, &c->bundle[bundle_num].tree);
307  if (ret < 0)
308  return ret;
309  }
310  c->bundle[bundle_num].cur_dec =
311  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
312 
313  return 0;
314 }
315 
316 /**
317  * common check before starting decoding bundle data
318  *
319  * @param gb context for reading bits
320  * @param b bundle
321  * @param t variable where number of elements to decode will be stored
322  */
323 #define CHECK_READ_VAL(gb, b, t) \
324  if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
325  return 0; \
326  t = get_bits(gb, b->len); \
327  if (!t) { \
328  b->cur_dec = NULL; \
329  return 0; \
330  } \
331 
332 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
333 {
334  int t, v;
335  const uint8_t *dec_end;
336 
337  CHECK_READ_VAL(gb, b, t);
338  dec_end = b->cur_dec + t;
339  if (dec_end > b->data_end) {
340  av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
341  return AVERROR_INVALIDDATA;
342  }
343  if (get_bits_left(gb) < 1)
344  return AVERROR_INVALIDDATA;
345  if (get_bits1(gb)) {
346  v = get_bits(gb, 4);
347  memset(b->cur_dec, v, t);
348  b->cur_dec += t;
349  } else {
350  while (b->cur_dec < dec_end)
351  *b->cur_dec++ = GET_HUFF(gb, b->tree);
352  }
353  return 0;
354 }
355 
357 {
358  int t, sign, v;
359  const uint8_t *dec_end;
360 
361  CHECK_READ_VAL(gb, b, t);
362  dec_end = b->cur_dec + t;
363  if (dec_end > b->data_end) {
364  av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
365  return AVERROR_INVALIDDATA;
366  }
367  if (get_bits_left(gb) < 1)
368  return AVERROR_INVALIDDATA;
369  if (get_bits1(gb)) {
370  v = get_bits(gb, 4);
371  if (v) {
372  sign = -get_bits1(gb);
373  v = (v ^ sign) - sign;
374  }
375  memset(b->cur_dec, v, t);
376  b->cur_dec += t;
377  } else {
378  while (b->cur_dec < dec_end) {
379  v = GET_HUFF(gb, b->tree);
380  if (v) {
381  sign = -get_bits1(gb);
382  v = (v ^ sign) - sign;
383  }
384  *b->cur_dec++ = v;
385  }
386  }
387  return 0;
388 }
389 
390 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
391 
393 {
394  BinkContext * const c = avctx->priv_data;
395  int t, v;
396  int last = 0;
397  const uint8_t *dec_end;
398 
399  CHECK_READ_VAL(gb, b, t);
400  if (c->version == 'k') {
401  t ^= 0xBBu;
402  if (t == 0) {
403  b->cur_dec = NULL;
404  return 0;
405  }
406  }
407  dec_end = b->cur_dec + t;
408  if (dec_end > b->data_end) {
409  av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
410  return AVERROR_INVALIDDATA;
411  }
412  if (get_bits_left(gb) < 1)
413  return AVERROR_INVALIDDATA;
414  if (get_bits1(gb)) {
415  v = get_bits(gb, 4);
416  memset(b->cur_dec, v, t);
417  b->cur_dec += t;
418  } else {
419  while (b->cur_dec < dec_end) {
420  v = GET_HUFF(gb, b->tree);
421  if (v < 12) {
422  last = v;
423  *b->cur_dec++ = v;
424  } else {
425  int run = bink_rlelens[v - 12];
426 
427  if (dec_end - b->cur_dec < run)
428  return AVERROR_INVALIDDATA;
429  memset(b->cur_dec, last, run);
430  b->cur_dec += run;
431  }
432  }
433  }
434  return 0;
435 }
436 
438 {
439  int t, v;
440  const uint8_t *dec_end;
441 
442  CHECK_READ_VAL(gb, b, t);
443  dec_end = b->cur_dec + t;
444  if (dec_end > b->data_end) {
445  av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
446  return AVERROR_INVALIDDATA;
447  }
448  while (b->cur_dec < dec_end) {
449  if (get_bits_left(gb) < 2)
450  return AVERROR_INVALIDDATA;
451  v = GET_HUFF(gb, b->tree);
452  v |= GET_HUFF(gb, b->tree) << 4;
453  *b->cur_dec++ = v;
454  }
455 
456  return 0;
457 }
458 
460 {
461  int t, sign, v;
462  const uint8_t *dec_end;
463 
464  CHECK_READ_VAL(gb, b, t);
465  dec_end = b->cur_dec + t;
466  if (dec_end > b->data_end) {
467  av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
468  return AVERROR_INVALIDDATA;
469  }
470  if (get_bits_left(gb) < 1)
471  return AVERROR_INVALIDDATA;
472  if (get_bits1(gb)) {
473  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
474  v = GET_HUFF(gb, b->tree);
475  v = (c->col_lastval << 4) | v;
476  if (c->version < 'i') {
477  sign = ((int8_t) v) >> 7;
478  v = ((v & 0x7F) ^ sign) - sign;
479  v += 0x80;
480  }
481  memset(b->cur_dec, v, t);
482  b->cur_dec += t;
483  } else {
484  while (b->cur_dec < dec_end) {
485  if (get_bits_left(gb) < 2)
486  return AVERROR_INVALIDDATA;
487  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
488  v = GET_HUFF(gb, b->tree);
489  v = (c->col_lastval << 4) | v;
490  if (c->version < 'i') {
491  sign = ((int8_t) v) >> 7;
492  v = ((v & 0x7F) ^ sign) - sign;
493  v += 0x80;
494  }
495  *b->cur_dec++ = v;
496  }
497  }
498  return 0;
499 }
500 
501 /** number of bits used to store first DC value in bundle */
502 #define DC_START_BITS 11
503 
504 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
505  int start_bits, int has_sign)
506 {
507  int i, j, len, len2, bsize, sign, v, v2;
508  int16_t *dst = (int16_t*)b->cur_dec;
509  int16_t *dst_end = (int16_t*)b->data_end;
510 
511  CHECK_READ_VAL(gb, b, len);
512  if (get_bits_left(gb) < start_bits - has_sign)
513  return AVERROR_INVALIDDATA;
514  v = get_bits(gb, start_bits - has_sign);
515  if (v && has_sign) {
516  sign = -get_bits1(gb);
517  v = (v ^ sign) - sign;
518  }
519  if (dst_end - dst < 1)
520  return AVERROR_INVALIDDATA;
521  *dst++ = v;
522  len--;
523  for (i = 0; i < len; i += 8) {
524  len2 = FFMIN(len - i, 8);
525  if (dst_end - dst < len2)
526  return AVERROR_INVALIDDATA;
527  bsize = get_bits(gb, 4);
528  if (bsize) {
529  for (j = 0; j < len2; j++) {
530  v2 = get_bits(gb, bsize);
531  if (v2) {
532  sign = -get_bits1(gb);
533  v2 = (v2 ^ sign) - sign;
534  }
535  v += v2;
536  *dst++ = v;
537  if (v < -32768 || v > 32767) {
538  av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
539  return AVERROR_INVALIDDATA;
540  }
541  }
542  } else {
543  for (j = 0; j < len2; j++)
544  *dst++ = v;
545  }
546  }
547 
548  b->cur_dec = (uint8_t*)dst;
549  return 0;
550 }
551 
552 /**
553  * Retrieve next value from bundle.
554  *
555  * @param c decoder context
556  * @param bundle bundle number
557  */
558 static inline int get_value(BinkContext *c, int bundle)
559 {
560  int ret;
561 
562  if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
563  return *c->bundle[bundle].cur_ptr++;
564  if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
565  return (int8_t)*c->bundle[bundle].cur_ptr++;
566  ret = *(int16_t*)c->bundle[bundle].cur_ptr;
567  c->bundle[bundle].cur_ptr += 2;
568  return ret;
569 }
570 
571 static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
572 {
573  c->bundle[bundle_num].cur_dec =
574  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
575  c->bundle[bundle_num].len = 13;
576 }
577 
579 {
580  int i;
581  for (i = 0; i < BINKB_NB_SRC; i++)
583 }
584 
585 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
586 {
587  const int bits = binkb_bundle_sizes[bundle_num];
588  const int mask = 1 << (bits - 1);
589  const int issigned = binkb_bundle_signed[bundle_num];
590  Bundle *b = &c->bundle[bundle_num];
591  int i, len;
592 
593  CHECK_READ_VAL(gb, b, len);
594  if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
595  return AVERROR_INVALIDDATA;
596  if (bits <= 8) {
597  if (!issigned) {
598  for (i = 0; i < len; i++)
599  *b->cur_dec++ = get_bits(gb, bits);
600  } else {
601  for (i = 0; i < len; i++)
602  *b->cur_dec++ = get_bits(gb, bits) - mask;
603  }
604  } else {
605  int16_t *dst = (int16_t*)b->cur_dec;
606 
607  if (!issigned) {
608  for (i = 0; i < len; i++)
609  *dst++ = get_bits(gb, bits);
610  } else {
611  for (i = 0; i < len; i++)
612  *dst++ = get_bits(gb, bits) - mask;
613  }
614  b->cur_dec = (uint8_t*)dst;
615  }
616  return 0;
617 }
618 
619 static inline int binkb_get_value(BinkContext *c, int bundle_num)
620 {
621  int16_t ret;
622  const int bits = binkb_bundle_sizes[bundle_num];
623 
624  if (bits <= 8) {
625  int val = *c->bundle[bundle_num].cur_ptr++;
626  return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
627  }
628  ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
629  c->bundle[bundle_num].cur_ptr += 2;
630  return ret;
631 }
632 
633 /**
634  * Read 8x8 block of DCT coefficients.
635  *
636  * @param gb context for reading bits
637  * @param block place for storing coefficients
638  * @param scan scan order table
639  * @param quant_matrices quantization matrices
640  * @return 0 for success, negative value in other cases
641  */
643  const uint8_t *scan, int *coef_count_,
644  int coef_idx[64], int q)
645 {
646  int coef_list[128];
647  int mode_list[128];
648  int i, t, bits, ccoef, mode, sign;
649  int list_start = 64, list_end = 64, list_pos;
650  int coef_count = 0;
651  int quant_idx;
652 
653  if (get_bits_left(gb) < 4)
654  return AVERROR_INVALIDDATA;
655 
656  coef_list[list_end] = 4; mode_list[list_end++] = 0;
657  coef_list[list_end] = 24; mode_list[list_end++] = 0;
658  coef_list[list_end] = 44; mode_list[list_end++] = 0;
659  coef_list[list_end] = 1; mode_list[list_end++] = 3;
660  coef_list[list_end] = 2; mode_list[list_end++] = 3;
661  coef_list[list_end] = 3; mode_list[list_end++] = 3;
662 
663  for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
664  list_pos = list_start;
665  while (list_pos < list_end) {
666  if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
667  list_pos++;
668  continue;
669  }
670  ccoef = coef_list[list_pos];
671  mode = mode_list[list_pos];
672  switch (mode) {
673  case 0:
674  coef_list[list_pos] = ccoef + 4;
675  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;
796  case 2:
797  if (mode == 2) {
798  coef_list[list_pos] = 0;
799  mode_list[list_pos++] = 0;
800  }
801  for (i = 0; i < 4; i++, ccoef++) {
802  if (get_bits1(gb)) {
803  coef_list[--list_start] = ccoef;
804  mode_list[ list_start] = 3;
805  } else {
806  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
807  sign = -get_bits1(gb);
808  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
809  masks_count--;
810  if (masks_count < 0)
811  return 0;
812  }
813  }
814  break;
815  case 1:
816  mode_list[list_pos] = 2;
817  for (i = 0; i < 3; i++) {
818  ccoef += 4;
819  coef_list[list_end] = ccoef;
820  mode_list[list_end++] = 2;
821  }
822  break;
823  case 3:
824  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
825  sign = -get_bits1(gb);
826  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
827  coef_list[list_pos] = 0;
828  mode_list[list_pos++] = 0;
829  masks_count--;
830  if (masks_count < 0)
831  return 0;
832  break;
833  }
834  }
835  }
836 
837  return 0;
838 }
839 
840 /**
841  * Copy 8x8 block from source to destination, where src and dst may be overlapped
842  */
843 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
844 {
845  uint8_t tmp[64];
846  int i;
847  for (i = 0; i < 8; i++)
848  memcpy(tmp + i*8, src + i*stride, 8);
849  for (i = 0; i < 8; i++)
850  memcpy(dst + i*stride, tmp + i*8, 8);
851 }
852 
854  int plane_idx, int is_key, int is_chroma)
855 {
856  int blk, ret;
857  int i, j, bx, by;
858  uint8_t *dst, *ref, *ref_start, *ref_end;
859  int v, col[2];
860  const uint8_t *scan;
861  int xoff, yoff;
862  LOCAL_ALIGNED_32(int16_t, block, [64]);
863  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
864  int coordmap[64];
865  int ybias = is_key ? -15 : 0;
866  int qp, quant_idx, coef_count, coef_idx[64];
867 
868  const int stride = frame->linesize[plane_idx];
869  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
870  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
871 
873  ref_start = frame->data[plane_idx];
874  ref_end = frame->data[plane_idx] + ((bh - 1) * frame->linesize[plane_idx] + bw - 1) * 8;
875 
876  for (i = 0; i < 64; i++)
877  coordmap[i] = (i & 7) + (i >> 3) * stride;
878 
879  for (by = 0; by < bh; by++) {
880  for (i = 0; i < BINKB_NB_SRC; i++) {
881  if ((ret = binkb_read_bundle(c, gb, i)) < 0)
882  return ret;
883  }
884 
885  dst = frame->data[plane_idx] + 8*by*stride;
886  for (bx = 0; bx < bw; bx++, dst += 8) {
888  switch (blk) {
889  case 0:
890  break;
891  case 1:
892  scan = bink_patterns[get_bits(gb, 4)];
893  i = 0;
894  do {
895  int mode, run;
896 
897  mode = get_bits1(gb);
898  run = get_bits(gb, binkb_runbits[i]) + 1;
899 
900  i += run;
901  if (i > 64) {
902  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
903  return AVERROR_INVALIDDATA;
904  }
905  if (mode) {
907  for (j = 0; j < run; j++)
908  dst[coordmap[*scan++]] = v;
909  } else {
910  for (j = 0; j < run; j++)
911  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
912  }
913  } while (i < 63);
914  if (i == 63)
915  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
916  break;
917  case 2:
918  memset(dctblock, 0, sizeof(*dctblock) * 64);
919  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
921  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
922  return quant_idx;
923  unquantize_dct_coeffs(dctblock, binkb_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
924  c->binkdsp.idct_put(dst, stride, dctblock);
925  break;
926  case 3:
928  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
929  ref = dst + xoff + yoff * stride;
930  if (ref < ref_start || ref > ref_end) {
931  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
932  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
933  c->put_pixels_tab(dst, ref, stride, 8);
934  } else {
936  }
937  c->bdsp.clear_block(block);
939  read_residue(gb, block, v);
940  c->binkdsp.add_pixels8(dst, block, stride);
941  break;
942  case 4:
944  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
945  ref = dst + xoff + yoff * stride;
946  if (ref < ref_start || ref > ref_end) {
947  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
948  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
949  c->put_pixels_tab(dst, ref, stride, 8);
950  } else {
952  }
953  memset(dctblock, 0, sizeof(*dctblock) * 64);
954  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
956  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
957  return quant_idx;
958  unquantize_dct_coeffs(dctblock, binkb_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
959  c->binkdsp.idct_add(dst, stride, dctblock);
960  break;
961  case 5:
963  c->bdsp.fill_block_tab[1](dst, v, stride, 8);
964  break;
965  case 6:
966  for (i = 0; i < 2; i++)
968  for (i = 0; i < 8; i++) {
970  for (j = 0; j < 8; j++, v >>= 1)
971  dst[i*stride + j] = col[v & 1];
972  }
973  break;
974  case 7:
976  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
977  ref = dst + xoff + yoff * stride;
978  if (ref < ref_start || ref > ref_end) {
979  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
980  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
981  c->put_pixels_tab(dst, ref, stride, 8);
982  } else {
984  }
985  break;
986  case 8:
987  for (i = 0; i < 8; i++)
988  memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
989  c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
990  break;
991  default:
992  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
993  return AVERROR_INVALIDDATA;
994  }
995  }
996  }
997  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
998  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
999 
1000  return 0;
1001 }
1002 
1004  uint8_t *dst, uint8_t *prev, int stride,
1005  uint8_t *ref_start,
1006  uint8_t *ref_end)
1007 {
1008  int xoff = get_value(c, BINK_SRC_X_OFF);
1009  int yoff = get_value(c, BINK_SRC_Y_OFF);
1010  uint8_t *ref = prev + xoff + yoff * stride;
1011  if (ref < ref_start || ref > ref_end) {
1012  av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1013  xoff, yoff);
1014  return AVERROR_INVALIDDATA;
1015  }
1016  c->put_pixels_tab(dst, ref, stride, 8);
1017 
1018  return 0;
1019 }
1020 
1022  int plane_idx, int is_chroma)
1023 {
1024  int blk, ret;
1025  int i, j, bx, by;
1026  uint8_t *dst, *prev, *ref_start, *ref_end;
1027  int v, col[2];
1028  const uint8_t *scan;
1029  LOCAL_ALIGNED_32(int16_t, block, [64]);
1030  LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
1031  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
1032  int coordmap[64], quant_idx, coef_count, coef_idx[64];
1033 
1034  const int stride = frame->linesize[plane_idx];
1035  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
1036  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
1037  int width = c->avctx->width >> is_chroma;
1038  int height = c->avctx->height >> is_chroma;
1039 
1040  if (c->version == 'k' && get_bits1(gb)) {
1041  int fill = get_bits(gb, 8);
1042 
1043  dst = frame->data[plane_idx];
1044 
1045  for (i = 0; i < height; i++)
1046  memset(dst + i * stride, fill, width);
1047  goto end;
1048  }
1049 
1050  init_lengths(c, FFMAX(width, 8), bw);
1051  for (i = 0; i < BINK_NB_SRC; i++) {
1052  ret = read_bundle(gb, c, i);
1053  if (ret < 0)
1054  return ret;
1055  }
1056 
1057  ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
1058  : frame->data[plane_idx];
1059  ref_end = ref_start
1060  + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
1061 
1062  for (i = 0; i < 64; i++)
1063  coordmap[i] = (i & 7) + (i >> 3) * stride;
1064 
1065  for (by = 0; by < bh; by++) {
1066  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
1067  return ret;
1068  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
1069  return ret;
1070  if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
1071  return ret;
1072  if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
1073  return ret;
1074  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
1075  return ret;
1076  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
1077  return ret;
1078  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
1079  return ret;
1080  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
1081  return ret;
1082  if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
1083  return ret;
1084 
1085  dst = frame->data[plane_idx] + 8*by*stride;
1086  prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
1087  : frame->data[plane_idx]) + 8*by*stride;
1088  for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1090  // 16x16 block type on odd line means part of the already decoded block, so skip it
1091  if (((by & 1) || (bx & 1)) && blk == SCALED_BLOCK) {
1092  bx++;
1093  dst += 8;
1094  prev += 8;
1095  continue;
1096  }
1097  switch (blk) {
1098  case SKIP_BLOCK:
1099  c->put_pixels_tab(dst, prev, stride, 8);
1100  break;
1101  case SCALED_BLOCK:
1103  switch (blk) {
1104  case RUN_BLOCK:
1105  if (get_bits_left(gb) < 4)
1106  return AVERROR_INVALIDDATA;
1107  scan = bink_patterns[get_bits(gb, 4)];
1108  i = 0;
1109  do {
1110  int run = get_value(c, BINK_SRC_RUN) + 1;
1111 
1112  i += run;
1113  if (i > 64) {
1114  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1115  return AVERROR_INVALIDDATA;
1116  }
1117  if (get_bits1(gb)) {
1118  v = get_value(c, BINK_SRC_COLORS);
1119  for (j = 0; j < run; j++)
1120  ublock[*scan++] = v;
1121  } else {
1122  for (j = 0; j < run; j++)
1123  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1124  }
1125  } while (i < 63);
1126  if (i == 63)
1127  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1128  break;
1129  case INTRA_BLOCK:
1130  memset(dctblock, 0, sizeof(*dctblock) * 64);
1131  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1132  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1133  return quant_idx;
1134  unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1135  c->binkdsp.idct_put(ublock, 8, dctblock);
1136  break;
1137  case FILL_BLOCK:
1138  v = get_value(c, BINK_SRC_COLORS);
1139  c->bdsp.fill_block_tab[0](dst, v, stride, 16);
1140  break;
1141  case PATTERN_BLOCK:
1142  for (i = 0; i < 2; i++)
1143  col[i] = get_value(c, BINK_SRC_COLORS);
1144  for (j = 0; j < 8; j++) {
1146  for (i = 0; i < 8; i++, v >>= 1)
1147  ublock[i + j*8] = col[v & 1];
1148  }
1149  break;
1150  case RAW_BLOCK:
1151  for (j = 0; j < 8; j++)
1152  for (i = 0; i < 8; i++)
1153  ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1154  break;
1155  default:
1156  av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1157  return AVERROR_INVALIDDATA;
1158  }
1159  if (blk != FILL_BLOCK)
1160  c->binkdsp.scale_block(ublock, dst, stride);
1161  bx++;
1162  dst += 8;
1163  prev += 8;
1164  break;
1165  case MOTION_BLOCK:
1166  ret = bink_put_pixels(c, dst, prev, stride,
1167  ref_start, ref_end);
1168  if (ret < 0)
1169  return ret;
1170  break;
1171  case RUN_BLOCK:
1172  scan = bink_patterns[get_bits(gb, 4)];
1173  i = 0;
1174  do {
1175  int run = get_value(c, BINK_SRC_RUN) + 1;
1176 
1177  i += run;
1178  if (i > 64) {
1179  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1180  return AVERROR_INVALIDDATA;
1181  }
1182  if (get_bits1(gb)) {
1183  v = get_value(c, BINK_SRC_COLORS);
1184  for (j = 0; j < run; j++)
1185  dst[coordmap[*scan++]] = v;
1186  } else {
1187  for (j = 0; j < run; j++)
1188  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1189  }
1190  } while (i < 63);
1191  if (i == 63)
1192  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1193  break;
1194  case RESIDUE_BLOCK:
1195  ret = bink_put_pixels(c, dst, prev, stride,
1196  ref_start, ref_end);
1197  if (ret < 0)
1198  return ret;
1199  c->bdsp.clear_block(block);
1200  v = get_bits(gb, 7);
1201  read_residue(gb, block, v);
1202  c->binkdsp.add_pixels8(dst, block, stride);
1203  break;
1204  case INTRA_BLOCK:
1205  memset(dctblock, 0, sizeof(*dctblock) * 64);
1206  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1207  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1208  return quant_idx;
1209  unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1210  c->binkdsp.idct_put(dst, stride, dctblock);
1211  break;
1212  case FILL_BLOCK:
1213  v = get_value(c, BINK_SRC_COLORS);
1214  c->bdsp.fill_block_tab[1](dst, v, stride, 8);
1215  break;
1216  case INTER_BLOCK:
1217  ret = bink_put_pixels(c, dst, prev, stride,
1218  ref_start, ref_end);
1219  if (ret < 0)
1220  return ret;
1221  memset(dctblock, 0, sizeof(*dctblock) * 64);
1222  dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1223  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1224  return quant_idx;
1225  unquantize_dct_coeffs(dctblock, bink_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
1226  c->binkdsp.idct_add(dst, stride, dctblock);
1227  break;
1228  case PATTERN_BLOCK:
1229  for (i = 0; i < 2; i++)
1230  col[i] = get_value(c, BINK_SRC_COLORS);
1231  for (i = 0; i < 8; i++) {
1233  for (j = 0; j < 8; j++, v >>= 1)
1234  dst[i*stride + j] = col[v & 1];
1235  }
1236  break;
1237  case RAW_BLOCK:
1238  for (i = 0; i < 8; i++)
1239  memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1240  c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1241  break;
1242  default:
1243  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1244  return AVERROR_INVALIDDATA;
1245  }
1246  }
1247  }
1248 
1249 end:
1250  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1251  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1252 
1253  return 0;
1254 }
1255 
1257  int *got_frame, AVPacket *pkt)
1258 {
1259  BinkContext * const c = avctx->priv_data;
1260  GetBitContext gb;
1261  int plane, plane_idx, ret;
1262  int bits_count = pkt->size << 3;
1263 
1264  if (c->version > 'b') {
1265  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1266  return ret;
1267  } else {
1268  if ((ret = ff_reget_buffer(avctx, c->last, 0)) < 0)
1269  return ret;
1270  if ((ret = av_frame_ref(frame, c->last)) < 0)
1271  return ret;
1272  }
1273 
1274  init_get_bits(&gb, pkt->data, bits_count);
1275  if (c->has_alpha) {
1276  if (c->version >= 'i')
1277  skip_bits_long(&gb, 32);
1278  if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
1279  return ret;
1280  }
1281  if (c->version >= 'i')
1282  skip_bits_long(&gb, 32);
1283 
1284  c->frame_num++;
1285 
1286  for (plane = 0; plane < 3; plane++) {
1287  plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1288 
1289  if (c->version > 'b') {
1290  if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
1291  return ret;
1292  } else {
1293  if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
1294  c->frame_num == 1, !!plane)) < 0)
1295  return ret;
1296  }
1297  if (get_bits_count(&gb) >= bits_count)
1298  break;
1299  }
1300  emms_c();
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 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:107
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:853
init_bundles
static av_cold int init_bundles(BinkContext *c)
Allocate memory for bundles.
Definition: bink.c:181
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
Bundle::cur_dec
uint8_t * cur_dec
pointer to the not yet decoded part of the buffer
Definition: bink.c:110
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:843
BINKB_SRC_Y_OFF
@ BINKB_SRC_Y_OFF
Y components of motion value.
Definition: bink.c:53
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:42
blockdsp.h
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
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
mem_internal.h
init_lengths
static void init_lengths(BinkContext *c, int width, int bw)
Initialize length in all bundles.
Definition: bink.c:156
out
FILE * out
Definition: movenc.c:54
INTER_BLOCK
@ INTER_BLOCK
motion block with DCT applied to the difference
Definition: bink.c:144
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:250
bink_scan
static const uint8_t bink_scan[64]
Bink DCT and residue 8x8 block scan order.
Definition: binkdata.h:28
thread.h
flush
static void flush(AVCodecContext *avctx)
Definition: bink.c:1414
DC_START_BITS
#define DC_START_BITS
number of bits used to store first DC value in bundle
Definition: bink.c:502
BinkContext::col_high
Tree col_high[16]
trees for decoding high nibble in "colours" data type
Definition: bink.c:129
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:266
read_patterns
static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:437
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:130
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:221
INTRA_BLOCK
@ INTRA_BLOCK
intra DCT block
Definition: bink.c:142
ff_bink_decoder
const FFCodec ff_bink_decoder
Definition: bink.c:1421
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:683
AVPacket::data
uint8_t * data
Definition: packet.h:522
b
#define b
Definition: input.c:41
table
static const uint16_t table[]
Definition: prosumer.c:205
FFCodec
Definition: codec_internal.h:127
Tree::vlc_num
int vlc_num
tree number (in bink_trees[])
Definition: bink.c:95
binkb_bundle_signed
static const uint8_t binkb_bundle_signed[BINKB_NB_SRC]
Definition: bink.c:67
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:78
BINKB_SRC_INTER_DC
@ BINKB_SRC_INTER_DC
DC values for interblocks with DCT.
Definition: bink.c:55
BINK_SRC_X_OFF
@ BINK_SRC_X_OFF
X components of motion value.
Definition: bink.c:82
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:392
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
binkb_intra_quant
static int32_t binkb_intra_quant[16][64]
Definition: bink.c:71
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
binkb_num
static const uint8_t binkb_num[16]
Definition: binkdata.h:647
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
BinkContext::put_pixels_tab
op_pixels_func put_pixels_tab
Definition: bink.c:120
BinkContext::col_lastval
int col_lastval
value of last decoded high nibble in "colours" data type
Definition: bink.c:130
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
BinkContext::frame_num
unsigned frame_num
Definition: bink.c:126
bink_decode_plane
static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, int plane_idx, int is_chroma)
Definition: bink.c:1021
GetBitContext
Definition: get_bits.h:108
OldSources
OldSources
IDs for different data types used in old version of Bink video codec.
Definition: bink.c:48
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:502
val
static double val(void *priv, double ch)
Definition: aeval.c:78
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:1003
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:118
bink_trees
static VLC bink_trees[16]
Definition: bink.c:43
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
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
binkb_read_bundle
static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
Definition: bink.c:585
binkb_intra_seed
static const uint8_t binkb_intra_seed[64]
Definition: binkdata.h:625
mask
static const uint16_t mask[17]
Definition: lzw.c:38
emms_c
#define emms_c()
Definition: emms.h:63
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
BinkDSPContext
Definition: binkdsp.h:32
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
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:186
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:143
binkb_bundle_sizes
static const uint8_t binkb_bundle_sizes[BINKB_NB_SRC]
Definition: bink.c:63
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:425
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:54
bits
uint8_t bits
Definition: vp3data.h:128
LOCAL_ALIGNED_16
#define LOCAL_ALIGNED_16(t, v,...)
Definition: mem_internal.h:150
vlc_init
#define vlc_init(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:59
read_dcs
static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b, int start_bits, int has_sign)
Definition: bink.c:504
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:40
BinkContext::has_alpha
int has_alpha
Definition: bink.c:124
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
ff_hpeldsp_init
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
binkb_get_value
static int binkb_get_value(BinkContext *c, int bundle_num)
Definition: bink.c:619
if
if(ret)
Definition: filter_design.txt:179
BINK_SRC_Y_OFF
@ BINK_SRC_Y_OFF
Y components of motion value.
Definition: bink.c:83
BINK_SRC_RUN
@ BINK_SRC_RUN
run lengths for special fill block
Definition: bink.c:86
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:156
RUN_BLOCK
@ RUN_BLOCK
block is composed from runs of colours with custom scan order
Definition: bink.c:140
run
uint8_t run
Definition: svq3.c:203
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:695
PATTERN_BLOCK
@ PATTERN_BLOCK
block is filled with two colours following custom pattern
Definition: bink.c:145
read_runs
static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:332
BINKB_NB_SRC
@ BINKB_NB_SRC
Definition: bink.c:60
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
BINK_SRC_PATTERN
@ BINK_SRC_PATTERN
8-bit values for 2-colour pattern fill
Definition: bink.c:81
Bundle::data
uint8_t * data
buffer for decoded symbols
Definition: bink.c:108
Bundle::len
int len
length of number of entries to decode (in bits)
Definition: bink.c:106
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: bink.c:1256
read_bundle
static int read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
Prepare bundle for decoding data.
Definition: bink.c:293
BlockTypes
BlockTypes
Bink video block types.
Definition: bink.c:136
get_value
static int get_value(BinkContext *c, int bundle)
Retrieve next value from bundle.
Definition: bink.c:558
op_pixels_func
void(* op_pixels_func)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
Definition: hpeldsp.h:38
RESIDUE_BLOCK
@ RESIDUE_BLOCK
motion block with some difference added
Definition: bink.c:141
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:105
HpelDSPContext
Half-pel DSP context.
Definition: hpeldsp.h:45
VLC::table_allocated
int table_allocated
Definition: vlc.h:39
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:642
HpelDSPContext::put_pixels_tab
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
binkdata.h
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1569
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
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:523
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:354
codec_internal.h
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:50
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:58
size
int size
Definition: twinvq_data.h:10344
VLCElem
Definition: vlc.h:32
BinkContext::last
AVFrame * last
Definition: bink.c:122
height
#define height
Bundle::data_end
uint8_t * data_end
buffer end
Definition: bink.c:109
BinkContext::version
int version
internal Bink file version
Definition: bink.c:123
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:139
binkdsp.h
Tree
data needed to decode 4-bit Huffman-coded value
Definition: bink.c:94
BinkContext
Definition: bink.c:117
BINK_SRC_INTRA_DC
@ BINK_SRC_INTRA_DC
DC values for intrablocks with DCT.
Definition: bink.c:84
BINKB_SRC_X_OFF
@ BINKB_SRC_X_OFF
X components of motion value.
Definition: bink.c:52
emms.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
internal.h
src2
const pixel * src2
Definition: h264pred_template.c:422
BINK_SRC_COLORS
@ BINK_SRC_COLORS
pixel values used for different block types
Definition: bink.c:80
bink_rlelens
static const uint8_t bink_rlelens[4]
Definition: bink.c:390
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:56
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
BinkContext::bundle
Bundle bundle[BINKB_NB_SRC]
bundles for decoding all data types
Definition: bink.c:128
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:666
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
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:1678
ret
ret
Definition: filter_design.txt:187
BinkContext::swap_planes
int swap_planes
Definition: bink.c:125
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
Tree::syms
uint8_t syms[16]
leaf value to symbol mapping
Definition: bink.c:96
free_bundles
static av_cold void free_bundles(BinkContext *c)
Free memory used by bundles.
Definition: bink.c:208
BinkContext::binkdsp
BinkDSPContext binkdsp
Definition: bink.c:121
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:72
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:459
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:453
AVCodecContext
main external API structure.
Definition: avcodec.h:445
read_tree
static int read_tree(GetBitContext *gb, Tree *tree)
Read information about Huffman tree used to decode data.
Definition: bink.c:248
SKIP_BLOCK
@ SKIP_BLOCK
skipped block
Definition: bink.c:137
binkb_init_bundle
static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
Definition: bink.c:571
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:88
VLC
Definition: vlc.h:36
RAW_BLOCK
@ RAW_BLOCK
uncoded 8x8 block
Definition: bink.c:146
CHECK_READ_VAL
#define CHECK_READ_VAL(gb, b, t)
common check before starting decoding bundle data
Definition: bink.c:323
BINKB_SRC_INTER_Q
@ BINKB_SRC_INTER_Q
quantizer values for interblocks with DCT
Definition: bink.c:57
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
Bundle::cur_ptr
uint8_t * cur_ptr
pointer to the data that is not read from buffer yet
Definition: bink.c:111
VLC::table
VLCElem * table
Definition: vlc.h:38
binkb_init_bundles
static av_cold void binkb_init_bundles(BinkContext *c)
Definition: bink.c:578
BINK_SRC_SUB_BLOCK_TYPES
@ BINK_SRC_SUB_BLOCK_TYPES
16x16 block types (a subset of 8x8 block types)
Definition: bink.c:79
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:85
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
BinkContext::avctx
AVCodecContext * avctx
Definition: bink.c:118
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:470
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
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:138
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:618
BinkContext::bdsp
BlockDSPContext bdsp
Definition: bink.c:119
int32_t
int32_t
Definition: audioconvert.c:56
imgutils.h
hpeldsp.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
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:179
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
BINKB_SRC_PATTERN
@ BINKB_SRC_PATTERN
8-bit values for 2-colour pattern fill
Definition: bink.c:51
int
int
Definition: ffmpeg_filter.c:409
read_motion_values
static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:356
Sources
Sources
IDs for different data types used in Bink video codec.
Definition: bink.c:77
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
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:49
GET_HUFF
#define GET_HUFF(gb, tree)
Definition: bink.c:99
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: bink.c:1359