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