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 int 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  if (get_bits_left(gb) < 4)
250  return AVERROR_INVALIDDATA;
251 
252  tree->vlc_num = get_bits(gb, 4);
253  if (!tree->vlc_num) {
254  for (i = 0; i < 16; i++)
255  tree->syms[i] = i;
256  return 0;
257  }
258  if (get_bits1(gb)) {
259  len = get_bits(gb, 3);
260  for (i = 0; i <= len; i++) {
261  tree->syms[i] = get_bits(gb, 4);
262  tmp1[tree->syms[i]] = 1;
263  }
264  for (i = 0; i < 16 && len < 16 - 1; i++)
265  if (!tmp1[i])
266  tree->syms[++len] = i;
267  } else {
268  len = get_bits(gb, 2);
269  for (i = 0; i < 16; i++)
270  in[i] = i;
271  for (i = 0; i <= len; i++) {
272  int size = 1 << i;
273  for (t = 0; t < 16; t += size << 1)
274  merge(gb, out + t, in + t, size);
275  FFSWAP(uint8_t*, in, out);
276  }
277  memcpy(tree->syms, in, 16);
278  }
279  return 0;
280 }
281 
282 /**
283  * Prepare bundle for decoding data.
284  *
285  * @param gb context for reading bits
286  * @param c decoder context
287  * @param bundle_num number of the bundle to initialize
288  */
289 static int read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
290 {
291  int i;
292 
293  if (bundle_num == BINK_SRC_COLORS) {
294  for (i = 0; i < 16; i++) {
295  int ret = read_tree(gb, &c->col_high[i]);
296  if (ret < 0)
297  return ret;
298  }
299  c->col_lastval = 0;
300  }
301  if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC) {
302  int ret = read_tree(gb, &c->bundle[bundle_num].tree);
303  if (ret < 0)
304  return ret;
305  }
306  c->bundle[bundle_num].cur_dec =
307  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
308 
309  return 0;
310 }
311 
312 /**
313  * common check before starting decoding bundle data
314  *
315  * @param gb context for reading bits
316  * @param b bundle
317  * @param t variable where number of elements to decode will be stored
318  */
319 #define CHECK_READ_VAL(gb, b, t) \
320  if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
321  return 0; \
322  t = get_bits(gb, b->len); \
323  if (!t) { \
324  b->cur_dec = NULL; \
325  return 0; \
326  } \
327 
328 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
329 {
330  int t, v;
331  const uint8_t *dec_end;
332 
333  CHECK_READ_VAL(gb, b, t);
334  dec_end = b->cur_dec + t;
335  if (dec_end > b->data_end) {
336  av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
337  return AVERROR_INVALIDDATA;
338  }
339  if (get_bits_left(gb) < 1)
340  return AVERROR_INVALIDDATA;
341  if (get_bits1(gb)) {
342  v = get_bits(gb, 4);
343  memset(b->cur_dec, v, t);
344  b->cur_dec += t;
345  } else {
346  while (b->cur_dec < dec_end)
347  *b->cur_dec++ = GET_HUFF(gb, b->tree);
348  }
349  return 0;
350 }
351 
353 {
354  int t, sign, v;
355  const uint8_t *dec_end;
356 
357  CHECK_READ_VAL(gb, b, t);
358  dec_end = b->cur_dec + t;
359  if (dec_end > b->data_end) {
360  av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
361  return AVERROR_INVALIDDATA;
362  }
363  if (get_bits_left(gb) < 1)
364  return AVERROR_INVALIDDATA;
365  if (get_bits1(gb)) {
366  v = get_bits(gb, 4);
367  if (v) {
368  sign = -get_bits1(gb);
369  v = (v ^ sign) - sign;
370  }
371  memset(b->cur_dec, v, t);
372  b->cur_dec += t;
373  } else {
374  while (b->cur_dec < dec_end) {
375  v = GET_HUFF(gb, b->tree);
376  if (v) {
377  sign = -get_bits1(gb);
378  v = (v ^ sign) - sign;
379  }
380  *b->cur_dec++ = v;
381  }
382  }
383  return 0;
384 }
385 
386 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
387 
389 {
390  BinkContext * const c = avctx->priv_data;
391  int t, v;
392  int last = 0;
393  const uint8_t *dec_end;
394 
395  CHECK_READ_VAL(gb, b, t);
396  if (c->version == 'k') {
397  t ^= 0xBBu;
398  if (t == 0) {
399  b->cur_dec = NULL;
400  return 0;
401  }
402  }
403  dec_end = b->cur_dec + t;
404  if (dec_end > b->data_end) {
405  av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
406  return AVERROR_INVALIDDATA;
407  }
408  if (get_bits_left(gb) < 1)
409  return AVERROR_INVALIDDATA;
410  if (get_bits1(gb)) {
411  v = get_bits(gb, 4);
412  memset(b->cur_dec, v, t);
413  b->cur_dec += t;
414  } else {
415  while (b->cur_dec < dec_end) {
416  v = GET_HUFF(gb, b->tree);
417  if (v < 12) {
418  last = v;
419  *b->cur_dec++ = v;
420  } else {
421  int run = bink_rlelens[v - 12];
422 
423  if (dec_end - b->cur_dec < run)
424  return AVERROR_INVALIDDATA;
425  memset(b->cur_dec, last, run);
426  b->cur_dec += run;
427  }
428  }
429  }
430  return 0;
431 }
432 
434 {
435  int t, v;
436  const uint8_t *dec_end;
437 
438  CHECK_READ_VAL(gb, b, t);
439  dec_end = b->cur_dec + t;
440  if (dec_end > b->data_end) {
441  av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
442  return AVERROR_INVALIDDATA;
443  }
444  while (b->cur_dec < dec_end) {
445  if (get_bits_left(gb) < 2)
446  return AVERROR_INVALIDDATA;
447  v = GET_HUFF(gb, b->tree);
448  v |= GET_HUFF(gb, b->tree) << 4;
449  *b->cur_dec++ = v;
450  }
451 
452  return 0;
453 }
454 
456 {
457  int t, sign, v;
458  const uint8_t *dec_end;
459 
460  CHECK_READ_VAL(gb, b, t);
461  dec_end = b->cur_dec + t;
462  if (dec_end > b->data_end) {
463  av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
464  return AVERROR_INVALIDDATA;
465  }
466  if (get_bits_left(gb) < 1)
467  return AVERROR_INVALIDDATA;
468  if (get_bits1(gb)) {
469  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
470  v = GET_HUFF(gb, b->tree);
471  v = (c->col_lastval << 4) | v;
472  if (c->version < 'i') {
473  sign = ((int8_t) v) >> 7;
474  v = ((v & 0x7F) ^ sign) - sign;
475  v += 0x80;
476  }
477  memset(b->cur_dec, v, t);
478  b->cur_dec += t;
479  } else {
480  while (b->cur_dec < dec_end) {
481  if (get_bits_left(gb) < 2)
482  return AVERROR_INVALIDDATA;
483  c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
484  v = GET_HUFF(gb, b->tree);
485  v = (c->col_lastval << 4) | v;
486  if (c->version < 'i') {
487  sign = ((int8_t) v) >> 7;
488  v = ((v & 0x7F) ^ sign) - sign;
489  v += 0x80;
490  }
491  *b->cur_dec++ = v;
492  }
493  }
494  return 0;
495 }
496 
497 /** number of bits used to store first DC value in bundle */
498 #define DC_START_BITS 11
499 
500 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
501  int start_bits, int has_sign)
502 {
503  int i, j, len, len2, bsize, sign, v, v2;
504  int16_t *dst = (int16_t*)b->cur_dec;
505  int16_t *dst_end = (int16_t*)b->data_end;
506 
507  CHECK_READ_VAL(gb, b, len);
508  if (get_bits_left(gb) < start_bits - has_sign)
509  return AVERROR_INVALIDDATA;
510  v = get_bits(gb, start_bits - has_sign);
511  if (v && has_sign) {
512  sign = -get_bits1(gb);
513  v = (v ^ sign) - sign;
514  }
515  if (dst_end - dst < 1)
516  return AVERROR_INVALIDDATA;
517  *dst++ = v;
518  len--;
519  for (i = 0; i < len; i += 8) {
520  len2 = FFMIN(len - i, 8);
521  if (dst_end - dst < len2)
522  return AVERROR_INVALIDDATA;
523  bsize = get_bits(gb, 4);
524  if (bsize) {
525  for (j = 0; j < len2; j++) {
526  v2 = get_bits(gb, bsize);
527  if (v2) {
528  sign = -get_bits1(gb);
529  v2 = (v2 ^ sign) - sign;
530  }
531  v += v2;
532  *dst++ = v;
533  if (v < -32768 || v > 32767) {
534  av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
535  return AVERROR_INVALIDDATA;
536  }
537  }
538  } else {
539  for (j = 0; j < len2; j++)
540  *dst++ = v;
541  }
542  }
543 
544  b->cur_dec = (uint8_t*)dst;
545  return 0;
546 }
547 
548 /**
549  * Retrieve next value from bundle.
550  *
551  * @param c decoder context
552  * @param bundle bundle number
553  */
554 static inline int get_value(BinkContext *c, int bundle)
555 {
556  int ret;
557 
558  if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
559  return *c->bundle[bundle].cur_ptr++;
560  if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
561  return (int8_t)*c->bundle[bundle].cur_ptr++;
562  ret = *(int16_t*)c->bundle[bundle].cur_ptr;
563  c->bundle[bundle].cur_ptr += 2;
564  return ret;
565 }
566 
567 static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
568 {
569  c->bundle[bundle_num].cur_dec =
570  c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
571  c->bundle[bundle_num].len = 13;
572 }
573 
575 {
576  int i;
577  for (i = 0; i < BINKB_NB_SRC; i++)
579 }
580 
581 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
582 {
583  const int bits = binkb_bundle_sizes[bundle_num];
584  const int mask = 1 << (bits - 1);
585  const int issigned = binkb_bundle_signed[bundle_num];
586  Bundle *b = &c->bundle[bundle_num];
587  int i, len;
588 
589  CHECK_READ_VAL(gb, b, len);
590  if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
591  return AVERROR_INVALIDDATA;
592  if (bits <= 8) {
593  if (!issigned) {
594  for (i = 0; i < len; i++)
595  *b->cur_dec++ = get_bits(gb, bits);
596  } else {
597  for (i = 0; i < len; i++)
598  *b->cur_dec++ = get_bits(gb, bits) - mask;
599  }
600  } else {
601  int16_t *dst = (int16_t*)b->cur_dec;
602 
603  if (!issigned) {
604  for (i = 0; i < len; i++)
605  *dst++ = get_bits(gb, bits);
606  } else {
607  for (i = 0; i < len; i++)
608  *dst++ = get_bits(gb, bits) - mask;
609  }
610  b->cur_dec = (uint8_t*)dst;
611  }
612  return 0;
613 }
614 
615 static inline int binkb_get_value(BinkContext *c, int bundle_num)
616 {
617  int16_t ret;
618  const int bits = binkb_bundle_sizes[bundle_num];
619 
620  if (bits <= 8) {
621  int val = *c->bundle[bundle_num].cur_ptr++;
622  return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
623  }
624  ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
625  c->bundle[bundle_num].cur_ptr += 2;
626  return ret;
627 }
628 
629 /**
630  * Read 8x8 block of DCT coefficients.
631  *
632  * @param gb context for reading bits
633  * @param block place for storing coefficients
634  * @param scan scan order table
635  * @param quant_matrices quantization matrices
636  * @return 0 for success, negative value in other cases
637  */
639  const uint8_t *scan, int *coef_count_,
640  int coef_idx[64], int q)
641 {
642  int coef_list[128];
643  int mode_list[128];
644  int i, t, bits, ccoef, mode, sign;
645  int list_start = 64, list_end = 64, list_pos;
646  int coef_count = 0;
647  int quant_idx;
648 
649  if (get_bits_left(gb) < 4)
650  return AVERROR_INVALIDDATA;
651 
652  coef_list[list_end] = 4; mode_list[list_end++] = 0;
653  coef_list[list_end] = 24; mode_list[list_end++] = 0;
654  coef_list[list_end] = 44; mode_list[list_end++] = 0;
655  coef_list[list_end] = 1; mode_list[list_end++] = 3;
656  coef_list[list_end] = 2; mode_list[list_end++] = 3;
657  coef_list[list_end] = 3; mode_list[list_end++] = 3;
658 
659  for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
660  list_pos = list_start;
661  while (list_pos < list_end) {
662  if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
663  list_pos++;
664  continue;
665  }
666  ccoef = coef_list[list_pos];
667  mode = mode_list[list_pos];
668  switch (mode) {
669  case 0:
670  coef_list[list_pos] = ccoef + 4;
671  mode_list[list_pos] = 1;
672  case 2:
673  if (mode == 2) {
674  coef_list[list_pos] = 0;
675  mode_list[list_pos++] = 0;
676  }
677  for (i = 0; i < 4; i++, ccoef++) {
678  if (get_bits1(gb)) {
679  coef_list[--list_start] = ccoef;
680  mode_list[ list_start] = 3;
681  } else {
682  if (!bits) {
683  t = 1 - (get_bits1(gb) << 1);
684  } else {
685  t = get_bits(gb, bits) | 1 << bits;
686  sign = -get_bits1(gb);
687  t = (t ^ sign) - sign;
688  }
689  block[scan[ccoef]] = t;
690  coef_idx[coef_count++] = ccoef;
691  }
692  }
693  break;
694  case 1:
695  mode_list[list_pos] = 2;
696  for (i = 0; i < 3; i++) {
697  ccoef += 4;
698  coef_list[list_end] = ccoef;
699  mode_list[list_end++] = 2;
700  }
701  break;
702  case 3:
703  if (!bits) {
704  t = 1 - (get_bits1(gb) << 1);
705  } else {
706  t = get_bits(gb, bits) | 1 << bits;
707  sign = -get_bits1(gb);
708  t = (t ^ sign) - sign;
709  }
710  block[scan[ccoef]] = t;
711  coef_idx[coef_count++] = ccoef;
712  coef_list[list_pos] = 0;
713  mode_list[list_pos++] = 0;
714  break;
715  }
716  }
717  }
718 
719  if (q == -1) {
720  quant_idx = get_bits(gb, 4);
721  } else {
722  quant_idx = q;
723  if (quant_idx > 15U) {
724  av_log(c->avctx, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
725  return AVERROR_INVALIDDATA;
726  }
727  }
728 
729  *coef_count_ = coef_count;
730 
731  return quant_idx;
732 }
733 
734 static void unquantize_dct_coeffs(int32_t block[64], const uint32_t quant[64],
735  int coef_count, int coef_idx[64],
736  const uint8_t *scan)
737 {
738  int i;
739  block[0] = (int)(block[0] * quant[0]) >> 11;
740  for (i = 0; i < coef_count; i++) {
741  int idx = coef_idx[i];
742  block[scan[idx]] = (int)(block[scan[idx]] * quant[idx]) >> 11;
743  }
744 }
745 
746 /**
747  * Read 8x8 block with residue after motion compensation.
748  *
749  * @param gb context for reading bits
750  * @param block place to store read data
751  * @param masks_count number of masks to decode
752  * @return 0 on success, negative value in other cases
753  */
754 static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
755 {
756  int coef_list[128];
757  int mode_list[128];
758  int i, sign, mask, ccoef, mode;
759  int list_start = 64, list_end = 64, list_pos;
760  int nz_coeff[64];
761  int nz_coeff_count = 0;
762 
763  coef_list[list_end] = 4; mode_list[list_end++] = 0;
764  coef_list[list_end] = 24; mode_list[list_end++] = 0;
765  coef_list[list_end] = 44; mode_list[list_end++] = 0;
766  coef_list[list_end] = 0; mode_list[list_end++] = 2;
767 
768  for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
769  for (i = 0; i < nz_coeff_count; i++) {
770  if (!get_bits1(gb))
771  continue;
772  if (block[nz_coeff[i]] < 0)
773  block[nz_coeff[i]] -= mask;
774  else
775  block[nz_coeff[i]] += mask;
776  masks_count--;
777  if (masks_count < 0)
778  return 0;
779  }
780  list_pos = list_start;
781  while (list_pos < list_end) {
782  if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
783  list_pos++;
784  continue;
785  }
786  ccoef = coef_list[list_pos];
787  mode = mode_list[list_pos];
788  switch (mode) {
789  case 0:
790  coef_list[list_pos] = ccoef + 4;
791  mode_list[list_pos] = 1;
792  case 2:
793  if (mode == 2) {
794  coef_list[list_pos] = 0;
795  mode_list[list_pos++] = 0;
796  }
797  for (i = 0; i < 4; i++, ccoef++) {
798  if (get_bits1(gb)) {
799  coef_list[--list_start] = ccoef;
800  mode_list[ list_start] = 3;
801  } else {
802  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
803  sign = -get_bits1(gb);
804  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
805  masks_count--;
806  if (masks_count < 0)
807  return 0;
808  }
809  }
810  break;
811  case 1:
812  mode_list[list_pos] = 2;
813  for (i = 0; i < 3; i++) {
814  ccoef += 4;
815  coef_list[list_end] = ccoef;
816  mode_list[list_end++] = 2;
817  }
818  break;
819  case 3:
820  nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
821  sign = -get_bits1(gb);
822  block[bink_scan[ccoef]] = (mask ^ sign) - sign;
823  coef_list[list_pos] = 0;
824  mode_list[list_pos++] = 0;
825  masks_count--;
826  if (masks_count < 0)
827  return 0;
828  break;
829  }
830  }
831  }
832 
833  return 0;
834 }
835 
836 /**
837  * Copy 8x8 block from source to destination, where src and dst may be overlapped
838  */
839 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
840 {
841  uint8_t tmp[64];
842  int i;
843  for (i = 0; i < 8; i++)
844  memcpy(tmp + i*8, src + i*stride, 8);
845  for (i = 0; i < 8; i++)
846  memcpy(dst + i*stride, tmp + i*8, 8);
847 }
848 
850  int plane_idx, int is_key, int is_chroma)
851 {
852  int blk, ret;
853  int i, j, bx, by;
854  uint8_t *dst, *ref, *ref_start, *ref_end;
855  int v, col[2];
856  const uint8_t *scan;
857  int xoff, yoff;
858  LOCAL_ALIGNED_32(int16_t, block, [64]);
859  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
860  int coordmap[64];
861  int ybias = is_key ? -15 : 0;
862  int qp, quant_idx, coef_count, coef_idx[64];
863 
864  const int stride = frame->linesize[plane_idx];
865  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
866  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
867 
869  ref_start = frame->data[plane_idx];
870  ref_end = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
871 
872  for (i = 0; i < 64; i++)
873  coordmap[i] = (i & 7) + (i >> 3) * stride;
874 
875  for (by = 0; by < bh; by++) {
876  for (i = 0; i < BINKB_NB_SRC; i++) {
877  if ((ret = binkb_read_bundle(c, gb, i)) < 0)
878  return ret;
879  }
880 
881  dst = frame->data[plane_idx] + 8*by*stride;
882  for (bx = 0; bx < bw; bx++, dst += 8) {
884  switch (blk) {
885  case 0:
886  break;
887  case 1:
888  scan = bink_patterns[get_bits(gb, 4)];
889  i = 0;
890  do {
891  int mode, run;
892 
893  mode = get_bits1(gb);
894  run = get_bits(gb, binkb_runbits[i]) + 1;
895 
896  i += run;
897  if (i > 64) {
898  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
899  return AVERROR_INVALIDDATA;
900  }
901  if (mode) {
903  for (j = 0; j < run; j++)
904  dst[coordmap[*scan++]] = v;
905  } else {
906  for (j = 0; j < run; j++)
907  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
908  }
909  } while (i < 63);
910  if (i == 63)
911  dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
912  break;
913  case 2:
914  memset(dctblock, 0, sizeof(*dctblock) * 64);
915  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
917  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
918  return quant_idx;
919  unquantize_dct_coeffs(dctblock, binkb_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
920  c->binkdsp.idct_put(dst, stride, dctblock);
921  break;
922  case 3:
924  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
925  ref = dst + xoff + yoff * stride;
926  if (ref < ref_start || ref + 8*stride > ref_end) {
927  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
928  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
929  c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
930  } else {
932  }
933  c->bdsp.clear_block(block);
935  read_residue(gb, block, v);
936  c->binkdsp.add_pixels8(dst, block, stride);
937  break;
938  case 4:
940  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
941  ref = dst + xoff + yoff * stride;
942  if (ref < ref_start || ref + 8 * stride > ref_end) {
943  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
944  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
945  c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
946  } else {
948  }
949  memset(dctblock, 0, sizeof(*dctblock) * 64);
950  dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
952  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
953  return quant_idx;
954  unquantize_dct_coeffs(dctblock, binkb_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
955  c->binkdsp.idct_add(dst, stride, dctblock);
956  break;
957  case 5:
959  c->bdsp.fill_block_tab[1](dst, v, stride, 8);
960  break;
961  case 6:
962  for (i = 0; i < 2; i++)
964  for (i = 0; i < 8; i++) {
966  for (j = 0; j < 8; j++, v >>= 1)
967  dst[i*stride + j] = col[v & 1];
968  }
969  break;
970  case 7:
972  yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
973  ref = dst + xoff + yoff * stride;
974  if (ref < ref_start || ref + 8 * stride > ref_end) {
975  av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
976  } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
977  c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
978  } else {
980  }
981  break;
982  case 8:
983  for (i = 0; i < 8; i++)
984  memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
985  c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
986  break;
987  default:
988  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
989  return AVERROR_INVALIDDATA;
990  }
991  }
992  }
993  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
994  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
995 
996  return 0;
997 }
998 
1000  uint8_t *dst, uint8_t *prev, int stride,
1001  uint8_t *ref_start,
1002  uint8_t *ref_end)
1003 {
1004  int xoff = get_value(c, BINK_SRC_X_OFF);
1005  int yoff = get_value(c, BINK_SRC_Y_OFF);
1006  uint8_t *ref = prev + xoff + yoff * stride;
1007  if (ref < ref_start || ref > ref_end) {
1008  av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1009  xoff, yoff);
1010  return AVERROR_INVALIDDATA;
1011  }
1012  c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1013 
1014  return 0;
1015 }
1016 
1018  int plane_idx, int is_chroma)
1019 {
1020  int blk, ret;
1021  int i, j, bx, by;
1022  uint8_t *dst, *prev, *ref_start, *ref_end;
1023  int v, col[2];
1024  const uint8_t *scan;
1025  LOCAL_ALIGNED_32(int16_t, block, [64]);
1026  LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
1027  LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
1028  int coordmap[64], quant_idx, coef_count, coef_idx[64];
1029 
1030  const int stride = frame->linesize[plane_idx];
1031  int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
1032  int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
1033  int width = c->avctx->width >> is_chroma;
1034  int height = c->avctx->height >> is_chroma;
1035 
1036  if (c->version == 'k' && get_bits1(gb)) {
1037  int fill = get_bits(gb, 8);
1038 
1039  dst = frame->data[plane_idx];
1040 
1041  for (i = 0; i < height; i++)
1042  memset(dst + i * stride, fill, width);
1043  goto end;
1044  }
1045 
1046  init_lengths(c, FFMAX(width, 8), bw);
1047  for (i = 0; i < BINK_NB_SRC; i++) {
1048  ret = read_bundle(gb, c, i);
1049  if (ret < 0)
1050  return ret;
1051  }
1052 
1053  ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
1054  : frame->data[plane_idx];
1055  ref_end = ref_start
1056  + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
1057 
1058  for (i = 0; i < 64; i++)
1059  coordmap[i] = (i & 7) + (i >> 3) * stride;
1060 
1061  for (by = 0; by < bh; by++) {
1062  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
1063  return ret;
1064  if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
1065  return ret;
1066  if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
1067  return ret;
1068  if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
1069  return ret;
1070  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
1071  return ret;
1072  if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
1073  return ret;
1074  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
1075  return ret;
1076  if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
1077  return ret;
1078  if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
1079  return ret;
1080 
1081  dst = frame->data[plane_idx] + 8*by*stride;
1082  prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
1083  : frame->data[plane_idx]) + 8*by*stride;
1084  for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1086  // 16x16 block type on odd line means part of the already decoded block, so skip it
1087  if ((by & 1) && blk == SCALED_BLOCK) {
1088  bx++;
1089  dst += 8;
1090  prev += 8;
1091  continue;
1092  }
1093  switch (blk) {
1094  case SKIP_BLOCK:
1095  c->hdsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1096  break;
1097  case SCALED_BLOCK:
1099  switch (blk) {
1100  case RUN_BLOCK:
1101  if (get_bits_left(gb) < 4)
1102  return AVERROR_INVALIDDATA;
1103  scan = bink_patterns[get_bits(gb, 4)];
1104  i = 0;
1105  do {
1106  int run = get_value(c, BINK_SRC_RUN) + 1;
1107 
1108  i += run;
1109  if (i > 64) {
1110  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1111  return AVERROR_INVALIDDATA;
1112  }
1113  if (get_bits1(gb)) {
1114  v = get_value(c, BINK_SRC_COLORS);
1115  for (j = 0; j < run; j++)
1116  ublock[*scan++] = v;
1117  } else {
1118  for (j = 0; j < run; j++)
1119  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1120  }
1121  } while (i < 63);
1122  if (i == 63)
1123  ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1124  break;
1125  case INTRA_BLOCK:
1126  memset(dctblock, 0, sizeof(*dctblock) * 64);
1127  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1128  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1129  return quant_idx;
1130  unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1131  c->binkdsp.idct_put(ublock, 8, dctblock);
1132  break;
1133  case FILL_BLOCK:
1134  v = get_value(c, BINK_SRC_COLORS);
1135  c->bdsp.fill_block_tab[0](dst, v, stride, 16);
1136  break;
1137  case PATTERN_BLOCK:
1138  for (i = 0; i < 2; i++)
1139  col[i] = get_value(c, BINK_SRC_COLORS);
1140  for (j = 0; j < 8; j++) {
1142  for (i = 0; i < 8; i++, v >>= 1)
1143  ublock[i + j*8] = col[v & 1];
1144  }
1145  break;
1146  case RAW_BLOCK:
1147  for (j = 0; j < 8; j++)
1148  for (i = 0; i < 8; i++)
1149  ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1150  break;
1151  default:
1152  av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1153  return AVERROR_INVALIDDATA;
1154  }
1155  if (blk != FILL_BLOCK)
1156  c->binkdsp.scale_block(ublock, dst, stride);
1157  bx++;
1158  dst += 8;
1159  prev += 8;
1160  break;
1161  case MOTION_BLOCK:
1162  ret = bink_put_pixels(c, dst, prev, stride,
1163  ref_start, ref_end);
1164  if (ret < 0)
1165  return ret;
1166  break;
1167  case RUN_BLOCK:
1168  scan = bink_patterns[get_bits(gb, 4)];
1169  i = 0;
1170  do {
1171  int run = get_value(c, BINK_SRC_RUN) + 1;
1172 
1173  i += run;
1174  if (i > 64) {
1175  av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1176  return AVERROR_INVALIDDATA;
1177  }
1178  if (get_bits1(gb)) {
1179  v = get_value(c, BINK_SRC_COLORS);
1180  for (j = 0; j < run; j++)
1181  dst[coordmap[*scan++]] = v;
1182  } else {
1183  for (j = 0; j < run; j++)
1184  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1185  }
1186  } while (i < 63);
1187  if (i == 63)
1188  dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1189  break;
1190  case RESIDUE_BLOCK:
1191  ret = bink_put_pixels(c, dst, prev, stride,
1192  ref_start, ref_end);
1193  if (ret < 0)
1194  return ret;
1195  c->bdsp.clear_block(block);
1196  v = get_bits(gb, 7);
1197  read_residue(gb, block, v);
1198  c->binkdsp.add_pixels8(dst, block, stride);
1199  break;
1200  case INTRA_BLOCK:
1201  memset(dctblock, 0, sizeof(*dctblock) * 64);
1202  dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1203  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1204  return quant_idx;
1205  unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1206  c->binkdsp.idct_put(dst, stride, dctblock);
1207  break;
1208  case FILL_BLOCK:
1209  v = get_value(c, BINK_SRC_COLORS);
1210  c->bdsp.fill_block_tab[1](dst, v, stride, 8);
1211  break;
1212  case INTER_BLOCK:
1213  ret = bink_put_pixels(c, dst, prev, stride,
1214  ref_start, ref_end);
1215  if (ret < 0)
1216  return ret;
1217  memset(dctblock, 0, sizeof(*dctblock) * 64);
1218  dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1219  if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1220  return quant_idx;
1221  unquantize_dct_coeffs(dctblock, bink_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
1222  c->binkdsp.idct_add(dst, stride, dctblock);
1223  break;
1224  case PATTERN_BLOCK:
1225  for (i = 0; i < 2; i++)
1226  col[i] = get_value(c, BINK_SRC_COLORS);
1227  for (i = 0; i < 8; i++) {
1229  for (j = 0; j < 8; j++, v >>= 1)
1230  dst[i*stride + j] = col[v & 1];
1231  }
1232  break;
1233  case RAW_BLOCK:
1234  for (i = 0; i < 8; i++)
1235  memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1236  c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1237  break;
1238  default:
1239  av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1240  return AVERROR_INVALIDDATA;
1241  }
1242  }
1243  }
1244 
1245 end:
1246  if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1247  skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1248 
1249  return 0;
1250 }
1251 
1252 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
1253 {
1254  BinkContext * const c = avctx->priv_data;
1255  AVFrame *frame = data;
1256  GetBitContext gb;
1257  int plane, plane_idx, ret;
1258  int bits_count = pkt->size << 3;
1259 
1260  if (c->version > 'b') {
1261  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1262  return ret;
1263  } else {
1264  if ((ret = ff_reget_buffer(avctx, c->last, 0)) < 0)
1265  return ret;
1266  if ((ret = av_frame_ref(frame, c->last)) < 0)
1267  return ret;
1268  }
1269 
1270  init_get_bits(&gb, pkt->data, bits_count);
1271  if (c->has_alpha) {
1272  if (c->version >= 'i')
1273  skip_bits_long(&gb, 32);
1274  if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
1275  return ret;
1276  }
1277  if (c->version >= 'i')
1278  skip_bits_long(&gb, 32);
1279 
1280  c->frame_num++;
1281 
1282  for (plane = 0; plane < 3; plane++) {
1283  plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1284 
1285  if (c->version > 'b') {
1286  if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
1287  return ret;
1288  } else {
1289  if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
1290  c->frame_num == 1, !!plane)) < 0)
1291  return ret;
1292  }
1293  if (get_bits_count(&gb) >= bits_count)
1294  break;
1295  }
1296  emms_c();
1297 
1298  if (c->version > 'b') {
1299  av_frame_unref(c->last);
1300  if ((ret = av_frame_ref(c->last, frame)) < 0)
1301  return ret;
1302  }
1303 
1304  *got_frame = 1;
1305 
1306  /* always report that the buffer was completely consumed */
1307  return pkt->size;
1308 }
1309 
1310 /**
1311  * Calculate quantization tables for version b
1312  */
1313 static av_cold void binkb_calc_quant(void)
1314 {
1315  uint8_t inv_bink_scan[64];
1316  static const int s[64]={
1317  1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1318  1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1319  1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1320  1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1321  1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1322  843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1323  581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1324  296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1325  };
1326  int i, j;
1327 #define C (1LL<<30)
1328  for (i = 0; i < 64; i++)
1329  inv_bink_scan[bink_scan[i]] = i;
1330 
1331  for (j = 0; j < 16; j++) {
1332  for (i = 0; i < 64; i++) {
1333  int k = inv_bink_scan[i];
1334  binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
1335  binkb_num[j]/(binkb_den[j] * (C>>12));
1336  binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
1337  binkb_num[j]/(binkb_den[j] * (C>>12));
1338  }
1339  }
1340 }
1341 
1343 {
1344  BinkContext * const c = avctx->priv_data;
1345  static VLC_TYPE table[16 * 128][2];
1346  static int binkb_initialised = 0;
1347  int i, ret;
1348  int flags;
1349 
1350  c->version = avctx->codec_tag >> 24;
1351  if (avctx->extradata_size < 4) {
1352  av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1353  return AVERROR_INVALIDDATA;
1354  }
1355  flags = AV_RL32(avctx->extradata);
1356  c->has_alpha = flags & BINK_FLAG_ALPHA;
1357  c->swap_planes = c->version >= 'h';
1358  if (!bink_trees[15].table) {
1359  for (i = 0; i < 16; i++) {
1360  const int maxbits = bink_tree_lens[i][15];
1361  bink_trees[i].table = table + i*128;
1362  bink_trees[i].table_allocated = 1 << maxbits;
1363  init_vlc(&bink_trees[i], maxbits, 16,
1364  bink_tree_lens[i], 1, 1,
1366  }
1367  }
1368  c->avctx = avctx;
1369 
1370  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1371  return ret;
1372 
1373  c->last = av_frame_alloc();
1374  if (!c->last)
1375  return AVERROR(ENOMEM);
1376 
1377  avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1378  avctx->color_range = c->version == 'k' ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
1379 
1380  ff_blockdsp_init(&c->bdsp, avctx);
1381  ff_hpeldsp_init(&c->hdsp, avctx->flags);
1382  ff_binkdsp_init(&c->binkdsp);
1383 
1384  if ((ret = init_bundles(c)) < 0)
1385  return ret;
1386 
1387  if (c->version == 'b') {
1388  if (!binkb_initialised) {
1389  binkb_calc_quant();
1390  binkb_initialised = 1;
1391  }
1392  }
1393 
1394  return 0;
1395 }
1396 
1398 {
1399  BinkContext * const c = avctx->priv_data;
1400 
1401  av_frame_free(&c->last);
1402 
1403  free_bundles(c);
1404  return 0;
1405 }
1406 
1407 static void flush(AVCodecContext *avctx)
1408 {
1409  BinkContext * const c = avctx->priv_data;
1410 
1411  c->frame_num = 0;
1412 }
1413 
1415  .name = "binkvideo",
1416  .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
1417  .type = AVMEDIA_TYPE_VIDEO,
1418  .id = AV_CODEC_ID_BINKVIDEO,
1419  .priv_data_size = sizeof(BinkContext),
1420  .init = decode_init,
1421  .close = decode_end,
1422  .decode = decode_frame,
1423  .flush = flush,
1424  .capabilities = AV_CODEC_CAP_DR1,
1425  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1426 };
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: codec.h:190
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:849
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:839
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
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
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:262
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:1407
DC_START_BITS
#define DC_START_BITS
number of bits used to store first DC value in bundle
Definition: bink.c:498
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:433
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
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:92
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
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:535
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
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
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
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:388
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
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:1017
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:606
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:999
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
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:90
binkb_read_bundle
static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
Definition: bink.c:581
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:628
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:509
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:500
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:615
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:209
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1161
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:328
BINKB_NB_SRC
@ BINKB_NB_SRC
Definition: bink.c:57
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
src
#define src
Definition: vp8dsp.c:254
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
read_bundle
static int read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
Prepare bundle for decoding data.
Definition: bink.c:289
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:554
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:638
binkdata.h
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
AVPacket::size
int size
Definition: packet.h:356
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:444
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:1397
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:1313
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
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:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
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:48
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:386
ff_bink_decoder
AVCodec ff_bink_decoder
Definition: bink.c:1414
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:554
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:237
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: codec.h:197
len
int len
Definition: vorbis_enc_data.h:452
AVCodecContext::height
int height
Definition: avcodec.h:699
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
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:534
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:1961
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:455
AVCodecContext
main external API structure.
Definition: avcodec.h:526
read_tree
static int read_tree(GetBitContext *gb, Tree *tree)
Read information about Huffman tree used to decode data.
Definition: bink.c:244
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:567
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AV_CODEC_ID_BINKVIDEO
@ AV_CODEC_ID_BINKVIDEO
Definition: codec_id.h:184
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:734
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:319
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:1252
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:574
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:754
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:551
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: packet.h:332
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
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:699
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:192
read_motion_values
static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
Definition: bink.c:352
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:1342