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