3 * Copyright (c) 2009 Konstantin Shishkov
4 * Copyright (C) 2011 Peter Ross <pross@xvid.org>
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/imgutils.h"
31 #define BITSTREAM_READER_LE
34 #define BINK_FLAG_ALPHA 0x00100000
35 #define BINK_FLAG_GRAY 0x00020000
37 static VLC bink_trees[16];
40 * IDs for different data types used in old version of Bink video codec
43 BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
44 BINKB_SRC_COLORS, ///< pixel values used for different block types
45 BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
46 BINKB_SRC_X_OFF, ///< X components of motion value
47 BINKB_SRC_Y_OFF, ///< Y components of motion value
48 BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
49 BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
50 BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
51 BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
52 BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
57 static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
58 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
61 static const int binkb_bundle_signed[BINKB_NB_SRC] = {
62 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
65 static int32_t binkb_intra_quant[16][64];
66 static int32_t binkb_inter_quant[16][64];
69 * IDs for different data types used in Bink video codec
72 BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
73 BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
74 BINK_SRC_COLORS, ///< pixel values used for different block types
75 BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
76 BINK_SRC_X_OFF, ///< X components of motion value
77 BINK_SRC_Y_OFF, ///< Y components of motion value
78 BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
79 BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
80 BINK_SRC_RUN, ///< run lengths for special fill block
86 * data needed to decode 4-bit Huffman-coded value
89 int vlc_num; ///< tree number (in bink_trees[])
90 uint8_t syms[16]; ///< leaf value to symbol mapping
93 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
94 bink_trees[(tree).vlc_num].bits, 1)]
97 * data structure used for decoding single Bink data type
99 typedef struct Bundle {
100 int len; ///< length of number of entries to decode (in bits)
101 Tree tree; ///< Huffman tree-related data
102 uint8_t *data; ///< buffer for decoded symbols
103 uint8_t *data_end; ///< buffer end
104 uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
105 uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
111 typedef struct BinkContext {
112 AVCodecContext *avctx;
116 int version; ///< internal Bink file version
120 Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
121 Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
122 int col_lastval; ///< value of last decoded high nibble in "colours" data type
126 * Bink video block types
129 SKIP_BLOCK = 0, ///< skipped block
130 SCALED_BLOCK, ///< block has size 16x16
131 MOTION_BLOCK, ///< block is copied from previous frame with some offset
132 RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
133 RESIDUE_BLOCK, ///< motion block with some difference added
134 INTRA_BLOCK, ///< intra DCT block
135 FILL_BLOCK, ///< block is filled with single colour
136 INTER_BLOCK, ///< motion block with DCT applied to the difference
137 PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
138 RAW_BLOCK, ///< uncoded 8x8 block
142 * Initialize length length in all bundles.
144 * @param c decoder context
145 * @param width plane width
146 * @param bw plane width in 8x8 blocks
148 static void init_lengths(BinkContext *c, int width, int bw)
150 width = FFALIGN(width, 8);
152 c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
154 c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
156 c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
158 c->bundle[BINK_SRC_INTRA_DC].len =
159 c->bundle[BINK_SRC_INTER_DC].len =
160 c->bundle[BINK_SRC_X_OFF].len =
161 c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
163 c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
165 c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
169 * Allocate memory for bundles.
171 * @param c decoder context
173 static av_cold int init_bundles(BinkContext *c)
178 bw = (c->avctx->width + 7) >> 3;
179 bh = (c->avctx->height + 7) >> 3;
182 for (i = 0; i < BINKB_NB_SRC; i++) {
183 c->bundle[i].data = av_malloc(blocks * 64);
184 if (!c->bundle[i].data)
185 return AVERROR(ENOMEM);
186 c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
193 * Free memory used by bundles.
195 * @param c decoder context
197 static av_cold void free_bundles(BinkContext *c)
200 for (i = 0; i < BINKB_NB_SRC; i++)
201 av_freep(&c->bundle[i].data);
205 * Merge two consequent lists of equal size depending on bits read.
207 * @param gb context for reading bits
208 * @param dst buffer where merged list will be written to
209 * @param src pointer to the head of the first list (the second lists starts at src+size)
210 * @param size input lists size
212 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
214 uint8_t *src2 = src + size;
218 if (!get_bits1(gb)) {
225 } while (size && size2);
234 * Read information about Huffman tree used to decode data.
236 * @param gb context for reading bits
237 * @param tree pointer for storing tree data
239 static void read_tree(GetBitContext *gb, Tree *tree)
241 uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
244 tree->vlc_num = get_bits(gb, 4);
245 if (!tree->vlc_num) {
246 for (i = 0; i < 16; i++)
251 len = get_bits(gb, 3);
252 for (i = 0; i <= len; i++) {
253 tree->syms[i] = get_bits(gb, 4);
254 tmp1[tree->syms[i]] = 1;
256 for (i = 0; i < 16 && len < 16 - 1; i++)
258 tree->syms[++len] = i;
260 len = get_bits(gb, 2);
261 for (i = 0; i < 16; i++)
263 for (i = 0; i <= len; i++) {
265 for (t = 0; t < 16; t += size << 1)
266 merge(gb, out + t, in + t, size);
267 FFSWAP(uint8_t*, in, out);
269 memcpy(tree->syms, in, 16);
274 * Prepare bundle for decoding data.
276 * @param gb context for reading bits
277 * @param c decoder context
278 * @param bundle_num number of the bundle to initialize
280 static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
284 if (bundle_num == BINK_SRC_COLORS) {
285 for (i = 0; i < 16; i++)
286 read_tree(gb, &c->col_high[i]);
289 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
290 read_tree(gb, &c->bundle[bundle_num].tree);
291 c->bundle[bundle_num].cur_dec =
292 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
296 * common check before starting decoding bundle data
298 * @param gb context for reading bits
300 * @param t variable where number of elements to decode will be stored
302 #define CHECK_READ_VAL(gb, b, t) \
303 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
305 t = get_bits(gb, b->len); \
311 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
314 const uint8_t *dec_end;
316 CHECK_READ_VAL(gb, b, t);
317 dec_end = b->cur_dec + t;
318 if (dec_end > b->data_end) {
319 av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
320 return AVERROR_INVALIDDATA;
324 memset(b->cur_dec, v, t);
327 while (b->cur_dec < dec_end)
328 *b->cur_dec++ = GET_HUFF(gb, b->tree);
333 static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
336 const uint8_t *dec_end;
338 CHECK_READ_VAL(gb, b, t);
339 dec_end = b->cur_dec + t;
340 if (dec_end > b->data_end) {
341 av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
342 return AVERROR_INVALIDDATA;
347 sign = -get_bits1(gb);
348 v = (v ^ sign) - sign;
350 memset(b->cur_dec, v, t);
353 while (b->cur_dec < dec_end) {
354 v = GET_HUFF(gb, b->tree);
356 sign = -get_bits1(gb);
357 v = (v ^ sign) - sign;
365 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
367 static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
371 const uint8_t *dec_end;
373 CHECK_READ_VAL(gb, b, t);
374 dec_end = b->cur_dec + t;
375 if (dec_end > b->data_end) {
376 av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
377 return AVERROR_INVALIDDATA;
381 memset(b->cur_dec, v, t);
384 while (b->cur_dec < dec_end) {
385 v = GET_HUFF(gb, b->tree);
390 int run = bink_rlelens[v - 12];
392 if (dec_end - b->cur_dec < run)
393 return AVERROR_INVALIDDATA;
394 memset(b->cur_dec, last, run);
402 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
405 const uint8_t *dec_end;
407 CHECK_READ_VAL(gb, b, t);
408 dec_end = b->cur_dec + t;
409 if (dec_end > b->data_end) {
410 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
411 return AVERROR_INVALIDDATA;
413 while (b->cur_dec < dec_end) {
414 v = GET_HUFF(gb, b->tree);
415 v |= GET_HUFF(gb, b->tree) << 4;
422 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
425 const uint8_t *dec_end;
427 CHECK_READ_VAL(gb, b, t);
428 dec_end = b->cur_dec + t;
429 if (dec_end > b->data_end) {
430 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
431 return AVERROR_INVALIDDATA;
434 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
435 v = GET_HUFF(gb, b->tree);
436 v = (c->col_lastval << 4) | v;
437 if (c->version < 'i') {
438 sign = ((int8_t) v) >> 7;
439 v = ((v & 0x7F) ^ sign) - sign;
442 memset(b->cur_dec, v, t);
445 while (b->cur_dec < dec_end) {
446 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
447 v = GET_HUFF(gb, b->tree);
448 v = (c->col_lastval << 4) | v;
449 if (c->version < 'i') {
450 sign = ((int8_t) v) >> 7;
451 v = ((v & 0x7F) ^ sign) - sign;
460 /** number of bits used to store first DC value in bundle */
461 #define DC_START_BITS 11
463 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
464 int start_bits, int has_sign)
466 int i, j, len, len2, bsize, sign, v, v2;
467 int16_t *dst = (int16_t*)b->cur_dec;
468 int16_t *dst_end = (int16_t*)b->data_end;
470 CHECK_READ_VAL(gb, b, len);
471 v = get_bits(gb, start_bits - has_sign);
473 sign = -get_bits1(gb);
474 v = (v ^ sign) - sign;
476 if (dst_end - dst < 1)
477 return AVERROR_INVALIDDATA;
480 for (i = 0; i < len; i += 8) {
481 len2 = FFMIN(len - i, 8);
482 if (dst_end - dst < len2)
483 return AVERROR_INVALIDDATA;
484 bsize = get_bits(gb, 4);
486 for (j = 0; j < len2; j++) {
487 v2 = get_bits(gb, bsize);
489 sign = -get_bits1(gb);
490 v2 = (v2 ^ sign) - sign;
494 if (v < -32768 || v > 32767) {
495 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
496 return AVERROR_INVALIDDATA;
500 for (j = 0; j < len2; j++)
505 b->cur_dec = (uint8_t*)dst;
510 * Retrieve next value from bundle.
512 * @param c decoder context
513 * @param bundle bundle number
515 static inline int get_value(BinkContext *c, int bundle)
519 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
520 return *c->bundle[bundle].cur_ptr++;
521 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
522 return (int8_t)*c->bundle[bundle].cur_ptr++;
523 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
524 c->bundle[bundle].cur_ptr += 2;
528 static void binkb_init_bundle(BinkContext *c, int bundle_num)
530 c->bundle[bundle_num].cur_dec =
531 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
532 c->bundle[bundle_num].len = 13;
535 static void binkb_init_bundles(BinkContext *c)
538 for (i = 0; i < BINKB_NB_SRC; i++)
539 binkb_init_bundle(c, i);
542 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
544 const int bits = binkb_bundle_sizes[bundle_num];
545 const int mask = 1 << (bits - 1);
546 const int issigned = binkb_bundle_signed[bundle_num];
547 Bundle *b = &c->bundle[bundle_num];
550 CHECK_READ_VAL(gb, b, len);
551 if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
552 return AVERROR_INVALIDDATA;
555 for (i = 0; i < len; i++)
556 *b->cur_dec++ = get_bits(gb, bits);
558 for (i = 0; i < len; i++)
559 *b->cur_dec++ = get_bits(gb, bits) - mask;
562 int16_t *dst = (int16_t*)b->cur_dec;
565 for (i = 0; i < len; i++)
566 *dst++ = get_bits(gb, bits);
568 for (i = 0; i < len; i++)
569 *dst++ = get_bits(gb, bits) - mask;
571 b->cur_dec = (uint8_t*)dst;
576 static inline int binkb_get_value(BinkContext *c, int bundle_num)
579 const int bits = binkb_bundle_sizes[bundle_num];
582 int val = *c->bundle[bundle_num].cur_ptr++;
583 return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
585 ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
586 c->bundle[bundle_num].cur_ptr += 2;
591 * Read 8x8 block of DCT coefficients.
593 * @param gb context for reading bits
594 * @param block place for storing coefficients
595 * @param scan scan order table
596 * @param quant_matrices quantization matrices
597 * @return 0 for success, negative value in other cases
599 static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
600 const int32_t quant_matrices[16][64], int q)
604 int i, t, bits, ccoef, mode, sign;
605 int list_start = 64, list_end = 64, list_pos;
609 const int32_t *quant;
611 coef_list[list_end] = 4; mode_list[list_end++] = 0;
612 coef_list[list_end] = 24; mode_list[list_end++] = 0;
613 coef_list[list_end] = 44; mode_list[list_end++] = 0;
614 coef_list[list_end] = 1; mode_list[list_end++] = 3;
615 coef_list[list_end] = 2; mode_list[list_end++] = 3;
616 coef_list[list_end] = 3; mode_list[list_end++] = 3;
618 for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
619 list_pos = list_start;
620 while (list_pos < list_end) {
621 if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
625 ccoef = coef_list[list_pos];
626 mode = mode_list[list_pos];
629 coef_list[list_pos] = ccoef + 4;
630 mode_list[list_pos] = 1;
633 coef_list[list_pos] = 0;
634 mode_list[list_pos++] = 0;
636 for (i = 0; i < 4; i++, ccoef++) {
638 coef_list[--list_start] = ccoef;
639 mode_list[ list_start] = 3;
642 t = 1 - (get_bits1(gb) << 1);
644 t = get_bits(gb, bits) | 1 << bits;
645 sign = -get_bits1(gb);
646 t = (t ^ sign) - sign;
648 block[scan[ccoef]] = t;
649 coef_idx[coef_count++] = ccoef;
654 mode_list[list_pos] = 2;
655 for (i = 0; i < 3; i++) {
657 coef_list[list_end] = ccoef;
658 mode_list[list_end++] = 2;
663 t = 1 - (get_bits1(gb) << 1);
665 t = get_bits(gb, bits) | 1 << bits;
666 sign = -get_bits1(gb);
667 t = (t ^ sign) - sign;
669 block[scan[ccoef]] = t;
670 coef_idx[coef_count++] = ccoef;
671 coef_list[list_pos] = 0;
672 mode_list[list_pos++] = 0;
679 quant_idx = get_bits(gb, 4);
682 if (quant_idx > 15U) {
683 av_log(NULL, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
684 return AVERROR_INVALIDDATA;
688 quant = quant_matrices[quant_idx];
690 block[0] = (block[0] * quant[0]) >> 11;
691 for (i = 0; i < coef_count; i++) {
692 int idx = coef_idx[i];
693 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
700 * Read 8x8 block with residue after motion compensation.
702 * @param gb context for reading bits
703 * @param block place to store read data
704 * @param masks_count number of masks to decode
705 * @return 0 on success, negative value in other cases
707 static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
711 int i, sign, mask, ccoef, mode;
712 int list_start = 64, list_end = 64, list_pos;
714 int nz_coeff_count = 0;
716 coef_list[list_end] = 4; mode_list[list_end++] = 0;
717 coef_list[list_end] = 24; mode_list[list_end++] = 0;
718 coef_list[list_end] = 44; mode_list[list_end++] = 0;
719 coef_list[list_end] = 0; mode_list[list_end++] = 2;
721 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
722 for (i = 0; i < nz_coeff_count; i++) {
725 if (block[nz_coeff[i]] < 0)
726 block[nz_coeff[i]] -= mask;
728 block[nz_coeff[i]] += mask;
733 list_pos = list_start;
734 while (list_pos < list_end) {
735 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
739 ccoef = coef_list[list_pos];
740 mode = mode_list[list_pos];
743 coef_list[list_pos] = ccoef + 4;
744 mode_list[list_pos] = 1;
747 coef_list[list_pos] = 0;
748 mode_list[list_pos++] = 0;
750 for (i = 0; i < 4; i++, ccoef++) {
752 coef_list[--list_start] = ccoef;
753 mode_list[ list_start] = 3;
755 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
756 sign = -get_bits1(gb);
757 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
765 mode_list[list_pos] = 2;
766 for (i = 0; i < 3; i++) {
768 coef_list[list_end] = ccoef;
769 mode_list[list_end++] = 2;
773 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
774 sign = -get_bits1(gb);
775 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
776 coef_list[list_pos] = 0;
777 mode_list[list_pos++] = 0;
790 * Copy 8x8 block from source to destination, where src and dst may be overlapped
792 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
796 for (i = 0; i < 8; i++)
797 memcpy(tmp + i*8, src + i*stride, 8);
798 for (i = 0; i < 8; i++)
799 memcpy(dst + i*stride, tmp + i*8, 8);
802 static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
803 int is_key, int is_chroma)
807 uint8_t *dst, *ref, *ref_start, *ref_end;
811 LOCAL_ALIGNED_16(int16_t, block, [64]);
812 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
814 int ybias = is_key ? -15 : 0;
817 const int stride = c->pic->linesize[plane_idx];
818 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
819 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
821 binkb_init_bundles(c);
822 ref_start = c->pic->data[plane_idx];
823 ref_end = c->pic->data[plane_idx] + (bh * c->pic->linesize[plane_idx] + bw) * 8;
825 for (i = 0; i < 64; i++)
826 coordmap[i] = (i & 7) + (i >> 3) * stride;
828 for (by = 0; by < bh; by++) {
829 for (i = 0; i < BINKB_NB_SRC; i++) {
830 if ((ret = binkb_read_bundle(c, gb, i)) < 0)
834 dst = c->pic->data[plane_idx] + 8*by*stride;
835 for (bx = 0; bx < bw; bx++, dst += 8) {
836 blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
841 scan = bink_patterns[get_bits(gb, 4)];
846 mode = get_bits1(gb);
847 run = get_bits(gb, binkb_runbits[i]) + 1;
851 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
852 return AVERROR_INVALIDDATA;
855 v = binkb_get_value(c, BINKB_SRC_COLORS);
856 for (j = 0; j < run; j++)
857 dst[coordmap[*scan++]] = v;
859 for (j = 0; j < run; j++)
860 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
864 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
867 memset(dctblock, 0, sizeof(*dctblock) * 64);
868 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
869 qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
870 read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_intra_quant, qp);
871 c->bdsp.idct_put(dst, stride, dctblock);
874 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
875 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
876 ref = dst + xoff + yoff * stride;
877 if (ref < ref_start || ref + 8*stride > ref_end) {
878 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
879 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
880 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
882 put_pixels8x8_overlapped(dst, ref, stride);
884 c->dsp.clear_block(block);
885 v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
886 read_residue(gb, block, v);
887 c->dsp.add_pixels8(dst, block, stride);
890 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
891 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
892 ref = dst + xoff + yoff * stride;
893 if (ref < ref_start || ref + 8 * stride > ref_end) {
894 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
895 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
896 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
898 put_pixels8x8_overlapped(dst, ref, stride);
900 memset(dctblock, 0, sizeof(*dctblock) * 64);
901 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
902 qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
903 read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_inter_quant, qp);
904 c->bdsp.idct_add(dst, stride, dctblock);
907 v = binkb_get_value(c, BINKB_SRC_COLORS);
908 c->dsp.fill_block_tab[1](dst, v, stride, 8);
911 for (i = 0; i < 2; i++)
912 col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
913 for (i = 0; i < 8; i++) {
914 v = binkb_get_value(c, BINKB_SRC_PATTERN);
915 for (j = 0; j < 8; j++, v >>= 1)
916 dst[i*stride + j] = col[v & 1];
920 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
921 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
922 ref = dst + xoff + yoff * stride;
923 if (ref < ref_start || ref + 8 * stride > ref_end) {
924 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
925 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
926 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
928 put_pixels8x8_overlapped(dst, ref, stride);
932 for (i = 0; i < 8; i++)
933 memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
934 c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
937 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
938 return AVERROR_INVALIDDATA;
942 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
943 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
948 static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
953 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
957 LOCAL_ALIGNED_16(int16_t, block, [64]);
958 LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
959 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
962 const int stride = c->pic->linesize[plane_idx];
963 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
964 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
965 int width = c->avctx->width >> is_chroma;
967 init_lengths(c, FFMAX(width, 8), bw);
968 for (i = 0; i < BINK_NB_SRC; i++)
969 read_bundle(gb, c, i);
971 ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
972 : c->pic->data[plane_idx];
974 + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
976 for (i = 0; i < 64; i++)
977 coordmap[i] = (i & 7) + (i >> 3) * stride;
979 for (by = 0; by < bh; by++) {
980 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
982 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
984 if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
986 if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
988 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
990 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
992 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
994 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
996 if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
1001 dst = c->pic->data[plane_idx] + 8*by*stride;
1002 prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
1003 : c->pic->data[plane_idx]) + 8*by*stride;
1004 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1005 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
1006 // 16x16 block type on odd line means part of the already decoded block, so skip it
1007 if ((by & 1) && blk == SCALED_BLOCK) {
1015 c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1018 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
1021 scan = bink_patterns[get_bits(gb, 4)];
1024 int run = get_value(c, BINK_SRC_RUN) + 1;
1028 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1029 return AVERROR_INVALIDDATA;
1031 if (get_bits1(gb)) {
1032 v = get_value(c, BINK_SRC_COLORS);
1033 for (j = 0; j < run; j++)
1034 ublock[*scan++] = v;
1036 for (j = 0; j < run; j++)
1037 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1041 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1044 memset(dctblock, 0, sizeof(*dctblock) * 64);
1045 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1046 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1047 c->bdsp.idct_put(ublock, 8, dctblock);
1050 v = get_value(c, BINK_SRC_COLORS);
1051 c->dsp.fill_block_tab[0](dst, v, stride, 16);
1054 for (i = 0; i < 2; i++)
1055 col[i] = get_value(c, BINK_SRC_COLORS);
1056 for (j = 0; j < 8; j++) {
1057 v = get_value(c, BINK_SRC_PATTERN);
1058 for (i = 0; i < 8; i++, v >>= 1)
1059 ublock[i + j*8] = col[v & 1];
1063 for (j = 0; j < 8; j++)
1064 for (i = 0; i < 8; i++)
1065 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1068 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1069 return AVERROR_INVALIDDATA;
1071 if (blk != FILL_BLOCK)
1072 c->bdsp.scale_block(ublock, dst, stride);
1078 xoff = get_value(c, BINK_SRC_X_OFF);
1079 yoff = get_value(c, BINK_SRC_Y_OFF);
1080 ref = prev + xoff + yoff * stride;
1081 if (ref < ref_start || ref > ref_end) {
1082 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1083 bx*8 + xoff, by*8 + yoff);
1084 return AVERROR_INVALIDDATA;
1086 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1089 scan = bink_patterns[get_bits(gb, 4)];
1092 int run = get_value(c, BINK_SRC_RUN) + 1;
1096 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1097 return AVERROR_INVALIDDATA;
1099 if (get_bits1(gb)) {
1100 v = get_value(c, BINK_SRC_COLORS);
1101 for (j = 0; j < run; j++)
1102 dst[coordmap[*scan++]] = v;
1104 for (j = 0; j < run; j++)
1105 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1109 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1112 xoff = get_value(c, BINK_SRC_X_OFF);
1113 yoff = get_value(c, BINK_SRC_Y_OFF);
1114 ref = prev + xoff + yoff * stride;
1115 if (ref < ref_start || ref > ref_end) {
1116 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1117 bx*8 + xoff, by*8 + yoff);
1118 return AVERROR_INVALIDDATA;
1120 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1121 c->dsp.clear_block(block);
1122 v = get_bits(gb, 7);
1123 read_residue(gb, block, v);
1124 c->dsp.add_pixels8(dst, block, stride);
1127 memset(dctblock, 0, sizeof(*dctblock) * 64);
1128 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1129 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1130 c->bdsp.idct_put(dst, stride, dctblock);
1133 v = get_value(c, BINK_SRC_COLORS);
1134 c->dsp.fill_block_tab[1](dst, v, stride, 8);
1137 xoff = get_value(c, BINK_SRC_X_OFF);
1138 yoff = get_value(c, BINK_SRC_Y_OFF);
1139 ref = prev + xoff + yoff * stride;
1140 if (ref < ref_start || ref > ref_end) {
1141 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1142 bx*8 + xoff, by*8 + yoff);
1145 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1146 memset(dctblock, 0, sizeof(*dctblock) * 64);
1147 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1148 read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
1149 c->bdsp.idct_add(dst, stride, dctblock);
1152 for (i = 0; i < 2; i++)
1153 col[i] = get_value(c, BINK_SRC_COLORS);
1154 for (i = 0; i < 8; i++) {
1155 v = get_value(c, BINK_SRC_PATTERN);
1156 for (j = 0; j < 8; j++, v >>= 1)
1157 dst[i*stride + j] = col[v & 1];
1161 for (i = 0; i < 8; i++)
1162 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1163 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1166 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1167 return AVERROR_INVALIDDATA;
1171 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1172 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1177 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
1179 BinkContext * const c = avctx->priv_data;
1181 int plane, plane_idx, ret;
1182 int bits_count = pkt->size << 3;
1184 if (c->version > 'b') {
1186 avctx->release_buffer(avctx, c->pic);
1188 if ((ret = ff_get_buffer(avctx, c->pic)) < 0) {
1189 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1193 if ((ret = avctx->reget_buffer(avctx, c->pic)) < 0) {
1194 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1199 init_get_bits(&gb, pkt->data, bits_count);
1201 if (c->version >= 'i')
1202 skip_bits_long(&gb, 32);
1203 if ((ret = bink_decode_plane(c, &gb, 3, 0)) < 0)
1206 if (c->version >= 'i')
1207 skip_bits_long(&gb, 32);
1209 for (plane = 0; plane < 3; plane++) {
1210 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1212 if (c->version > 'b') {
1213 if ((ret = bink_decode_plane(c, &gb, plane_idx, !!plane)) < 0)
1216 if ((ret = binkb_decode_plane(c, &gb, plane_idx,
1217 !avctx->frame_number, !!plane)) < 0)
1220 if (get_bits_count(&gb) >= bits_count)
1226 *(AVFrame*)data = *c->pic;
1228 if (c->version > 'b')
1229 FFSWAP(AVFrame*, c->pic, c->last);
1231 /* always report that the buffer was completely consumed */
1236 * Caclulate quantization tables for version b
1238 static av_cold void binkb_calc_quant(void)
1240 uint8_t inv_bink_scan[64];
1241 static const int s[64]={
1242 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1243 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1244 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1245 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1246 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1247 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1248 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1249 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1253 for (i = 0; i < 64; i++)
1254 inv_bink_scan[bink_scan[i]] = i;
1256 for (j = 0; j < 16; j++) {
1257 for (i = 0; i < 64; i++) {
1258 int k = inv_bink_scan[i];
1259 binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
1260 binkb_num[j]/(binkb_den[j] * (C>>12));
1261 binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
1262 binkb_num[j]/(binkb_den[j] * (C>>12));
1267 static av_cold int decode_init(AVCodecContext *avctx)
1269 BinkContext * const c = avctx->priv_data;
1270 static VLC_TYPE table[16 * 128][2];
1271 static int binkb_initialised = 0;
1275 c->version = avctx->codec_tag >> 24;
1276 if (avctx->extradata_size < 4) {
1277 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1278 return AVERROR_INVALIDDATA;
1280 flags = AV_RL32(avctx->extradata);
1281 c->has_alpha = flags & BINK_FLAG_ALPHA;
1282 c->swap_planes = c->version >= 'h';
1283 if (!bink_trees[15].table) {
1284 for (i = 0; i < 16; i++) {
1285 const int maxbits = bink_tree_lens[i][15];
1286 bink_trees[i].table = table + i*128;
1287 bink_trees[i].table_allocated = 1 << maxbits;
1288 init_vlc(&bink_trees[i], maxbits, 16,
1289 bink_tree_lens[i], 1, 1,
1290 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1295 c->pic = avcodec_alloc_frame();
1296 c->last = avcodec_alloc_frame();
1297 if (!c->pic || !c->last) {
1298 avcodec_free_frame(&c->pic);
1299 avcodec_free_frame(&c->last);
1300 return AVERROR(ENOMEM);
1303 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1306 avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1308 ff_dsputil_init(&c->dsp, avctx);
1309 ff_binkdsp_init(&c->bdsp);
1311 if ((ret = init_bundles(c)) < 0) {
1316 if (c->version == 'b') {
1317 if (!binkb_initialised) {
1319 binkb_initialised = 1;
1326 static av_cold int decode_end(AVCodecContext *avctx)
1328 BinkContext * const c = avctx->priv_data;
1330 if (c->pic->data[0])
1331 avctx->release_buffer(avctx, c->pic);
1332 if (c->last->data[0])
1333 avctx->release_buffer(avctx, c->last);
1334 avcodec_free_frame(&c->pic);
1335 avcodec_free_frame(&c->last);
1341 AVCodec ff_bink_decoder = {
1342 .name = "binkvideo",
1343 .type = AVMEDIA_TYPE_VIDEO,
1344 .id = AV_CODEC_ID_BINKVIDEO,
1345 .priv_data_size = sizeof(BinkContext),
1346 .init = decode_init,
1347 .close = decode_end,
1348 .decode = decode_frame,
1349 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
1350 .capabilities = CODEC_CAP_DR1,