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 void 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 c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
189 * Free memory used by bundles.
191 * @param c decoder context
193 static av_cold void free_bundles(BinkContext *c)
196 for (i = 0; i < BINKB_NB_SRC; i++)
197 av_freep(&c->bundle[i].data);
201 * Merge two consequent lists of equal size depending on bits read.
203 * @param gb context for reading bits
204 * @param dst buffer where merged list will be written to
205 * @param src pointer to the head of the first list (the second lists starts at src+size)
206 * @param size input lists size
208 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
210 uint8_t *src2 = src + size;
214 if (!get_bits1(gb)) {
221 } while (size && size2);
230 * Read information about Huffman tree used to decode data.
232 * @param gb context for reading bits
233 * @param tree pointer for storing tree data
235 static void read_tree(GetBitContext *gb, Tree *tree)
237 uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
240 tree->vlc_num = get_bits(gb, 4);
241 if (!tree->vlc_num) {
242 for (i = 0; i < 16; i++)
247 len = get_bits(gb, 3);
248 for (i = 0; i <= len; i++) {
249 tree->syms[i] = get_bits(gb, 4);
250 tmp1[tree->syms[i]] = 1;
252 for (i = 0; i < 16 && len < 16 - 1; i++)
254 tree->syms[++len] = i;
256 len = get_bits(gb, 2);
257 for (i = 0; i < 16; i++)
259 for (i = 0; i <= len; i++) {
261 for (t = 0; t < 16; t += size << 1)
262 merge(gb, out + t, in + t, size);
263 FFSWAP(uint8_t*, in, out);
265 memcpy(tree->syms, in, 16);
270 * Prepare bundle for decoding data.
272 * @param gb context for reading bits
273 * @param c decoder context
274 * @param bundle_num number of the bundle to initialize
276 static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
280 if (bundle_num == BINK_SRC_COLORS) {
281 for (i = 0; i < 16; i++)
282 read_tree(gb, &c->col_high[i]);
285 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
286 read_tree(gb, &c->bundle[bundle_num].tree);
287 c->bundle[bundle_num].cur_dec =
288 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
292 * common check before starting decoding bundle data
294 * @param gb context for reading bits
296 * @param t variable where number of elements to decode will be stored
298 #define CHECK_READ_VAL(gb, b, t) \
299 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
301 t = get_bits(gb, b->len); \
307 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
310 const uint8_t *dec_end;
312 CHECK_READ_VAL(gb, b, t);
313 dec_end = b->cur_dec + t;
314 if (dec_end > b->data_end) {
315 av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
316 return AVERROR_INVALIDDATA;
320 memset(b->cur_dec, v, t);
323 while (b->cur_dec < dec_end)
324 *b->cur_dec++ = GET_HUFF(gb, b->tree);
329 static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
332 const uint8_t *dec_end;
334 CHECK_READ_VAL(gb, b, t);
335 dec_end = b->cur_dec + t;
336 if (dec_end > b->data_end) {
337 av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
338 return AVERROR_INVALIDDATA;
343 sign = -get_bits1(gb);
344 v = (v ^ sign) - sign;
346 memset(b->cur_dec, v, t);
349 while (b->cur_dec < dec_end) {
350 v = GET_HUFF(gb, b->tree);
352 sign = -get_bits1(gb);
353 v = (v ^ sign) - sign;
361 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
363 static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
367 const uint8_t *dec_end;
369 CHECK_READ_VAL(gb, b, t);
370 dec_end = b->cur_dec + t;
371 if (dec_end > b->data_end) {
372 av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
373 return AVERROR_INVALIDDATA;
377 memset(b->cur_dec, v, t);
380 while (b->cur_dec < dec_end) {
381 v = GET_HUFF(gb, b->tree);
386 int run = bink_rlelens[v - 12];
388 if (dec_end - b->cur_dec < run)
389 return AVERROR_INVALIDDATA;
390 memset(b->cur_dec, last, run);
398 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
401 const uint8_t *dec_end;
403 CHECK_READ_VAL(gb, b, t);
404 dec_end = b->cur_dec + t;
405 if (dec_end > b->data_end) {
406 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
407 return AVERROR_INVALIDDATA;
409 while (b->cur_dec < dec_end) {
410 v = GET_HUFF(gb, b->tree);
411 v |= GET_HUFF(gb, b->tree) << 4;
418 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
421 const uint8_t *dec_end;
423 CHECK_READ_VAL(gb, b, t);
424 dec_end = b->cur_dec + t;
425 if (dec_end > b->data_end) {
426 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
427 return AVERROR_INVALIDDATA;
430 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
431 v = GET_HUFF(gb, b->tree);
432 v = (c->col_lastval << 4) | v;
433 if (c->version < 'i') {
434 sign = ((int8_t) v) >> 7;
435 v = ((v & 0x7F) ^ sign) - sign;
438 memset(b->cur_dec, v, t);
441 while (b->cur_dec < dec_end) {
442 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
443 v = GET_HUFF(gb, b->tree);
444 v = (c->col_lastval << 4) | v;
445 if (c->version < 'i') {
446 sign = ((int8_t) v) >> 7;
447 v = ((v & 0x7F) ^ sign) - sign;
456 /** number of bits used to store first DC value in bundle */
457 #define DC_START_BITS 11
459 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
460 int start_bits, int has_sign)
462 int i, j, len, len2, bsize, sign, v, v2;
463 int16_t *dst = (int16_t*)b->cur_dec;
464 int16_t *dst_end = (int16_t*)b->data_end;
466 CHECK_READ_VAL(gb, b, len);
467 v = get_bits(gb, start_bits - has_sign);
469 sign = -get_bits1(gb);
470 v = (v ^ sign) - sign;
472 if (dst_end - dst < 1)
473 return AVERROR_INVALIDDATA;
476 for (i = 0; i < len; i += 8) {
477 len2 = FFMIN(len - i, 8);
478 if (dst_end - dst < len2)
479 return AVERROR_INVALIDDATA;
480 bsize = get_bits(gb, 4);
482 for (j = 0; j < len2; j++) {
483 v2 = get_bits(gb, bsize);
485 sign = -get_bits1(gb);
486 v2 = (v2 ^ sign) - sign;
490 if (v < -32768 || v > 32767) {
491 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
492 return AVERROR_INVALIDDATA;
496 for (j = 0; j < len2; j++)
501 b->cur_dec = (uint8_t*)dst;
506 * Retrieve next value from bundle.
508 * @param c decoder context
509 * @param bundle bundle number
511 static inline int get_value(BinkContext *c, int bundle)
515 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
516 return *c->bundle[bundle].cur_ptr++;
517 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
518 return (int8_t)*c->bundle[bundle].cur_ptr++;
519 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
520 c->bundle[bundle].cur_ptr += 2;
524 static void binkb_init_bundle(BinkContext *c, int bundle_num)
526 c->bundle[bundle_num].cur_dec =
527 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
528 c->bundle[bundle_num].len = 13;
531 static void binkb_init_bundles(BinkContext *c)
534 for (i = 0; i < BINKB_NB_SRC; i++)
535 binkb_init_bundle(c, i);
538 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
540 const int bits = binkb_bundle_sizes[bundle_num];
541 const int mask = 1 << (bits - 1);
542 const int issigned = binkb_bundle_signed[bundle_num];
543 Bundle *b = &c->bundle[bundle_num];
546 CHECK_READ_VAL(gb, b, len);
547 if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
548 return AVERROR_INVALIDDATA;
551 for (i = 0; i < len; i++)
552 *b->cur_dec++ = get_bits(gb, bits);
554 for (i = 0; i < len; i++)
555 *b->cur_dec++ = get_bits(gb, bits) - mask;
558 int16_t *dst = (int16_t*)b->cur_dec;
561 for (i = 0; i < len; i++)
562 *dst++ = get_bits(gb, bits);
564 for (i = 0; i < len; i++)
565 *dst++ = get_bits(gb, bits) - mask;
567 b->cur_dec = (uint8_t*)dst;
572 static inline int binkb_get_value(BinkContext *c, int bundle_num)
575 const int bits = binkb_bundle_sizes[bundle_num];
578 int val = *c->bundle[bundle_num].cur_ptr++;
579 return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
581 ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
582 c->bundle[bundle_num].cur_ptr += 2;
587 * Read 8x8 block of DCT coefficients.
589 * @param gb context for reading bits
590 * @param block place for storing coefficients
591 * @param scan scan order table
592 * @param quant_matrices quantization matrices
593 * @return 0 for success, negative value in other cases
595 static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
596 const int32_t quant_matrices[16][64], int q)
600 int i, t, bits, ccoef, mode, sign;
601 int list_start = 64, list_end = 64, list_pos;
605 const int32_t *quant;
607 coef_list[list_end] = 4; mode_list[list_end++] = 0;
608 coef_list[list_end] = 24; mode_list[list_end++] = 0;
609 coef_list[list_end] = 44; mode_list[list_end++] = 0;
610 coef_list[list_end] = 1; mode_list[list_end++] = 3;
611 coef_list[list_end] = 2; mode_list[list_end++] = 3;
612 coef_list[list_end] = 3; mode_list[list_end++] = 3;
614 for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
615 list_pos = list_start;
616 while (list_pos < list_end) {
617 if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
621 ccoef = coef_list[list_pos];
622 mode = mode_list[list_pos];
625 coef_list[list_pos] = ccoef + 4;
626 mode_list[list_pos] = 1;
629 coef_list[list_pos] = 0;
630 mode_list[list_pos++] = 0;
632 for (i = 0; i < 4; i++, ccoef++) {
634 coef_list[--list_start] = ccoef;
635 mode_list[ list_start] = 3;
638 t = 1 - (get_bits1(gb) << 1);
640 t = get_bits(gb, bits) | 1 << bits;
641 sign = -get_bits1(gb);
642 t = (t ^ sign) - sign;
644 block[scan[ccoef]] = t;
645 coef_idx[coef_count++] = ccoef;
650 mode_list[list_pos] = 2;
651 for (i = 0; i < 3; i++) {
653 coef_list[list_end] = ccoef;
654 mode_list[list_end++] = 2;
659 t = 1 - (get_bits1(gb) << 1);
661 t = get_bits(gb, bits) | 1 << bits;
662 sign = -get_bits1(gb);
663 t = (t ^ sign) - sign;
665 block[scan[ccoef]] = t;
666 coef_idx[coef_count++] = ccoef;
667 coef_list[list_pos] = 0;
668 mode_list[list_pos++] = 0;
675 quant_idx = get_bits(gb, 4);
678 if (quant_idx > 15U) {
679 av_log(NULL, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
680 return AVERROR_INVALIDDATA;
684 quant = quant_matrices[quant_idx];
686 block[0] = (block[0] * quant[0]) >> 11;
687 for (i = 0; i < coef_count; i++) {
688 int idx = coef_idx[i];
689 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
696 * Read 8x8 block with residue after motion compensation.
698 * @param gb context for reading bits
699 * @param block place to store read data
700 * @param masks_count number of masks to decode
701 * @return 0 on success, negative value in other cases
703 static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
707 int i, sign, mask, ccoef, mode;
708 int list_start = 64, list_end = 64, list_pos;
710 int nz_coeff_count = 0;
712 coef_list[list_end] = 4; mode_list[list_end++] = 0;
713 coef_list[list_end] = 24; mode_list[list_end++] = 0;
714 coef_list[list_end] = 44; mode_list[list_end++] = 0;
715 coef_list[list_end] = 0; mode_list[list_end++] = 2;
717 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
718 for (i = 0; i < nz_coeff_count; i++) {
721 if (block[nz_coeff[i]] < 0)
722 block[nz_coeff[i]] -= mask;
724 block[nz_coeff[i]] += mask;
729 list_pos = list_start;
730 while (list_pos < list_end) {
731 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
735 ccoef = coef_list[list_pos];
736 mode = mode_list[list_pos];
739 coef_list[list_pos] = ccoef + 4;
740 mode_list[list_pos] = 1;
743 coef_list[list_pos] = 0;
744 mode_list[list_pos++] = 0;
746 for (i = 0; i < 4; i++, ccoef++) {
748 coef_list[--list_start] = ccoef;
749 mode_list[ list_start] = 3;
751 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
752 sign = -get_bits1(gb);
753 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
761 mode_list[list_pos] = 2;
762 for (i = 0; i < 3; i++) {
764 coef_list[list_end] = ccoef;
765 mode_list[list_end++] = 2;
769 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
770 sign = -get_bits1(gb);
771 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
772 coef_list[list_pos] = 0;
773 mode_list[list_pos++] = 0;
786 * Copy 8x8 block from source to destination, where src and dst may be overlapped
788 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
792 for (i = 0; i < 8; i++)
793 memcpy(tmp + i*8, src + i*stride, 8);
794 for (i = 0; i < 8; i++)
795 memcpy(dst + i*stride, tmp + i*8, 8);
798 static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
799 int is_key, int is_chroma)
803 uint8_t *dst, *ref, *ref_start, *ref_end;
807 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
808 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
810 int ybias = is_key ? -15 : 0;
813 const int stride = c->pic->linesize[plane_idx];
814 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
815 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
817 binkb_init_bundles(c);
818 ref_start = c->pic->data[plane_idx];
819 ref_end = c->pic->data[plane_idx] + (bh * c->pic->linesize[plane_idx] + bw) * 8;
821 for (i = 0; i < 64; i++)
822 coordmap[i] = (i & 7) + (i >> 3) * stride;
824 for (by = 0; by < bh; by++) {
825 for (i = 0; i < BINKB_NB_SRC; i++) {
826 if ((ret = binkb_read_bundle(c, gb, i)) < 0)
830 dst = c->pic->data[plane_idx] + 8*by*stride;
831 for (bx = 0; bx < bw; bx++, dst += 8) {
832 blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
837 scan = bink_patterns[get_bits(gb, 4)];
842 mode = get_bits1(gb);
843 run = get_bits(gb, binkb_runbits[i]) + 1;
847 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
848 return AVERROR_INVALIDDATA;
851 v = binkb_get_value(c, BINKB_SRC_COLORS);
852 for (j = 0; j < run; j++)
853 dst[coordmap[*scan++]] = v;
855 for (j = 0; j < run; j++)
856 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
860 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
863 memset(dctblock, 0, sizeof(*dctblock) * 64);
864 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
865 qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
866 read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_intra_quant, qp);
867 c->bdsp.idct_put(dst, stride, dctblock);
870 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
871 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
872 ref = dst + xoff + yoff * stride;
873 if (ref < ref_start || ref + 8*stride > ref_end) {
874 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
875 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
876 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
878 put_pixels8x8_overlapped(dst, ref, stride);
880 c->dsp.clear_block(block);
881 v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
882 read_residue(gb, block, v);
883 c->dsp.add_pixels8(dst, block, stride);
886 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
887 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
888 ref = dst + xoff + yoff * stride;
889 if (ref < ref_start || ref + 8 * stride > ref_end) {
890 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
891 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
892 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
894 put_pixels8x8_overlapped(dst, ref, stride);
896 memset(dctblock, 0, sizeof(*dctblock) * 64);
897 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
898 qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
899 read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_inter_quant, qp);
900 c->bdsp.idct_add(dst, stride, dctblock);
903 v = binkb_get_value(c, BINKB_SRC_COLORS);
904 c->dsp.fill_block_tab[1](dst, v, stride, 8);
907 for (i = 0; i < 2; i++)
908 col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
909 for (i = 0; i < 8; i++) {
910 v = binkb_get_value(c, BINKB_SRC_PATTERN);
911 for (j = 0; j < 8; j++, v >>= 1)
912 dst[i*stride + j] = col[v & 1];
916 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
917 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
918 ref = dst + xoff + yoff * stride;
919 if (ref < ref_start || ref + 8 * stride > ref_end) {
920 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
921 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
922 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
924 put_pixels8x8_overlapped(dst, ref, stride);
928 for (i = 0; i < 8; i++)
929 memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
930 c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
933 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
934 return AVERROR_INVALIDDATA;
938 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
939 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
944 static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
949 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
953 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
954 LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
955 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
958 const int stride = c->pic->linesize[plane_idx];
959 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
960 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
961 int width = c->avctx->width >> is_chroma;
963 init_lengths(c, FFMAX(width, 8), bw);
964 for (i = 0; i < BINK_NB_SRC; i++)
965 read_bundle(gb, c, i);
967 ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
968 : c->pic->data[plane_idx];
970 + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
972 for (i = 0; i < 64; i++)
973 coordmap[i] = (i & 7) + (i >> 3) * stride;
975 for (by = 0; by < bh; by++) {
976 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
978 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
980 if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
982 if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
984 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
986 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
988 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
990 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
992 if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
997 dst = c->pic->data[plane_idx] + 8*by*stride;
998 prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
999 : c->pic->data[plane_idx]) + 8*by*stride;
1000 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1001 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
1002 // 16x16 block type on odd line means part of the already decoded block, so skip it
1003 if ((by & 1) && blk == SCALED_BLOCK) {
1011 c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1014 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
1017 scan = bink_patterns[get_bits(gb, 4)];
1020 int run = get_value(c, BINK_SRC_RUN) + 1;
1024 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1025 return AVERROR_INVALIDDATA;
1027 if (get_bits1(gb)) {
1028 v = get_value(c, BINK_SRC_COLORS);
1029 for (j = 0; j < run; j++)
1030 ublock[*scan++] = v;
1032 for (j = 0; j < run; j++)
1033 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1037 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1040 memset(dctblock, 0, sizeof(*dctblock) * 64);
1041 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1042 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1043 c->bdsp.idct_put(ublock, 8, dctblock);
1046 v = get_value(c, BINK_SRC_COLORS);
1047 c->dsp.fill_block_tab[0](dst, v, stride, 16);
1050 for (i = 0; i < 2; i++)
1051 col[i] = get_value(c, BINK_SRC_COLORS);
1052 for (j = 0; j < 8; j++) {
1053 v = get_value(c, BINK_SRC_PATTERN);
1054 for (i = 0; i < 8; i++, v >>= 1)
1055 ublock[i + j*8] = col[v & 1];
1059 for (j = 0; j < 8; j++)
1060 for (i = 0; i < 8; i++)
1061 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1064 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1065 return AVERROR_INVALIDDATA;
1067 if (blk != FILL_BLOCK)
1068 c->bdsp.scale_block(ublock, dst, stride);
1074 xoff = get_value(c, BINK_SRC_X_OFF);
1075 yoff = get_value(c, BINK_SRC_Y_OFF);
1076 ref = prev + xoff + yoff * stride;
1077 if (ref < ref_start || ref > ref_end) {
1078 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1079 bx*8 + xoff, by*8 + yoff);
1080 return AVERROR_INVALIDDATA;
1082 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1085 scan = bink_patterns[get_bits(gb, 4)];
1088 int run = get_value(c, BINK_SRC_RUN) + 1;
1092 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1093 return AVERROR_INVALIDDATA;
1095 if (get_bits1(gb)) {
1096 v = get_value(c, BINK_SRC_COLORS);
1097 for (j = 0; j < run; j++)
1098 dst[coordmap[*scan++]] = v;
1100 for (j = 0; j < run; j++)
1101 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1105 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1108 xoff = get_value(c, BINK_SRC_X_OFF);
1109 yoff = get_value(c, BINK_SRC_Y_OFF);
1110 ref = prev + xoff + yoff * stride;
1111 if (ref < ref_start || ref > ref_end) {
1112 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1113 bx*8 + xoff, by*8 + yoff);
1114 return AVERROR_INVALIDDATA;
1116 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1117 c->dsp.clear_block(block);
1118 v = get_bits(gb, 7);
1119 read_residue(gb, block, v);
1120 c->dsp.add_pixels8(dst, block, stride);
1123 memset(dctblock, 0, sizeof(*dctblock) * 64);
1124 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1125 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1126 c->bdsp.idct_put(dst, stride, dctblock);
1129 v = get_value(c, BINK_SRC_COLORS);
1130 c->dsp.fill_block_tab[1](dst, v, stride, 8);
1133 xoff = get_value(c, BINK_SRC_X_OFF);
1134 yoff = get_value(c, BINK_SRC_Y_OFF);
1135 ref = prev + xoff + yoff * stride;
1136 if (ref < ref_start || ref > ref_end) {
1137 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1138 bx*8 + xoff, by*8 + yoff);
1141 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1142 memset(dctblock, 0, sizeof(*dctblock) * 64);
1143 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1144 read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
1145 c->bdsp.idct_add(dst, stride, dctblock);
1148 for (i = 0; i < 2; i++)
1149 col[i] = get_value(c, BINK_SRC_COLORS);
1150 for (i = 0; i < 8; i++) {
1151 v = get_value(c, BINK_SRC_PATTERN);
1152 for (j = 0; j < 8; j++, v >>= 1)
1153 dst[i*stride + j] = col[v & 1];
1157 for (i = 0; i < 8; i++)
1158 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1159 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1162 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1163 return AVERROR_INVALIDDATA;
1167 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1168 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1173 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
1175 BinkContext * const c = avctx->priv_data;
1177 int plane, plane_idx, ret;
1178 int bits_count = pkt->size << 3;
1180 if (c->version > 'b') {
1182 avctx->release_buffer(avctx, c->pic);
1184 if ((ret = ff_get_buffer(avctx, c->pic)) < 0) {
1185 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1189 if ((ret = avctx->reget_buffer(avctx, c->pic)) < 0) {
1190 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1195 init_get_bits(&gb, pkt->data, bits_count);
1197 if (c->version >= 'i')
1198 skip_bits_long(&gb, 32);
1199 if ((ret = bink_decode_plane(c, &gb, 3, 0)) < 0)
1202 if (c->version >= 'i')
1203 skip_bits_long(&gb, 32);
1205 for (plane = 0; plane < 3; plane++) {
1206 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1208 if (c->version > 'b') {
1209 if ((ret = bink_decode_plane(c, &gb, plane_idx, !!plane)) < 0)
1212 if ((ret = binkb_decode_plane(c, &gb, plane_idx, !pkt->pts, !!plane)) < 0)
1215 if (get_bits_count(&gb) >= bits_count)
1221 *(AVFrame*)data = *c->pic;
1223 if (c->version > 'b')
1224 FFSWAP(AVFrame*, c->pic, c->last);
1226 /* always report that the buffer was completely consumed */
1231 * Caclulate quantization tables for version b
1233 static av_cold void binkb_calc_quant(void)
1235 uint8_t inv_bink_scan[64];
1236 static const int s[64]={
1237 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1238 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1239 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1240 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1241 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1242 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1243 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1244 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1248 for (i = 0; i < 64; i++)
1249 inv_bink_scan[bink_scan[i]] = i;
1251 for (j = 0; j < 16; j++) {
1252 for (i = 0; i < 64; i++) {
1253 int k = inv_bink_scan[i];
1254 binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
1255 binkb_num[j]/(binkb_den[j] * (C>>12));
1256 binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
1257 binkb_num[j]/(binkb_den[j] * (C>>12));
1262 static av_cold int decode_init(AVCodecContext *avctx)
1264 BinkContext * const c = avctx->priv_data;
1265 static VLC_TYPE table[16 * 128][2];
1266 static int binkb_initialised = 0;
1270 c->version = avctx->codec_tag >> 24;
1271 if (avctx->extradata_size < 4) {
1272 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1273 return AVERROR_INVALIDDATA;
1275 flags = AV_RL32(avctx->extradata);
1276 c->has_alpha = flags & BINK_FLAG_ALPHA;
1277 c->swap_planes = c->version >= 'h';
1278 if (!bink_trees[15].table) {
1279 for (i = 0; i < 16; i++) {
1280 const int maxbits = bink_tree_lens[i][15];
1281 bink_trees[i].table = table + i*128;
1282 bink_trees[i].table_allocated = 1 << maxbits;
1283 init_vlc(&bink_trees[i], maxbits, 16,
1284 bink_tree_lens[i], 1, 1,
1285 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1290 c->pic = avcodec_alloc_frame();
1291 c->last = avcodec_alloc_frame();
1292 if (!c->pic || !c->last) {
1293 avcodec_free_frame(&c->pic);
1294 avcodec_free_frame(&c->last);
1295 return AVERROR(ENOMEM);
1298 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1301 avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1303 avctx->idct_algo = FF_IDCT_BINK;
1304 ff_dsputil_init(&c->dsp, avctx);
1305 ff_binkdsp_init(&c->bdsp);
1309 if (c->version == 'b') {
1310 if (!binkb_initialised) {
1312 binkb_initialised = 1;
1319 static av_cold int decode_end(AVCodecContext *avctx)
1321 BinkContext * const c = avctx->priv_data;
1323 if (c->pic->data[0])
1324 avctx->release_buffer(avctx, c->pic);
1325 if (c->last->data[0])
1326 avctx->release_buffer(avctx, c->last);
1327 avcodec_free_frame(&c->pic);
1328 avcodec_free_frame(&c->last);
1334 AVCodec ff_bink_decoder = {
1335 .name = "binkvideo",
1336 .type = AVMEDIA_TYPE_VIDEO,
1337 .id = AV_CODEC_ID_BINKVIDEO,
1338 .priv_data_size = sizeof(BinkContext),
1339 .init = decode_init,
1340 .close = decode_end,
1341 .decode = decode_frame,
1342 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
1343 .capabilities = CODEC_CAP_DR1,