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/attributes.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/internal.h"
27 #define BITSTREAM_READER_LE
37 #define BINK_FLAG_ALPHA 0x00100000
38 #define BINK_FLAG_GRAY 0x00020000
40 static VLC bink_trees[16];
43 * IDs for different data types used in old version of Bink video codec
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
60 static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
61 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
64 static const int binkb_bundle_signed[BINKB_NB_SRC] = {
65 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
68 static int32_t binkb_intra_quant[16][64];
69 static int32_t binkb_inter_quant[16][64];
72 * IDs for different data types used in Bink video codec
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
89 * data needed to decode 4-bit Huffman-coded value
92 int vlc_num; ///< tree number (in bink_trees[])
93 uint8_t syms[16]; ///< leaf value to symbol mapping
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)]
100 * data structure used for decoding single Bink data type
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
114 typedef struct BinkContext {
115 AVCodecContext *avctx;
116 BlockDSPContext bdsp;
118 BinkDSPContext binkdsp;
120 int version; ///< internal Bink file version
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
131 * Bink video block types
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
147 * Initialize length in all bundles.
149 * @param c decoder context
150 * @param width plane width
151 * @param bw plane width in 8x8 blocks
153 static void init_lengths(BinkContext *c, int width, int bw)
155 width = FFALIGN(width, 8);
157 c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
159 c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
161 c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
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;
168 c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
170 c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
174 * Allocate memory for bundles.
176 * @param c decoder context
178 static av_cold int init_bundles(BinkContext *c)
183 bw = (c->avctx->width + 7) >> 3;
184 bh = (c->avctx->height + 7) >> 3;
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;
198 * Free memory used by bundles.
200 * @param c decoder context
202 static av_cold void free_bundles(BinkContext *c)
205 for (i = 0; i < BINKB_NB_SRC; i++)
206 av_freep(&c->bundle[i].data);
210 * Merge two consequent lists of equal size depending on bits read.
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
217 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
219 uint8_t *src2 = src + size;
223 if (!get_bits1(gb)) {
230 } while (size && size2);
239 * Read information about Huffman tree used to decode data.
241 * @param gb context for reading bits
242 * @param tree pointer for storing tree data
244 static int read_tree(GetBitContext *gb, Tree *tree)
246 uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
249 if (get_bits_left(gb) < 4)
250 return AVERROR_INVALIDDATA;
252 tree->vlc_num = get_bits(gb, 4);
253 if (!tree->vlc_num) {
254 for (i = 0; i < 16; i++)
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;
264 for (i = 0; i < 16 && len < 16 - 1; i++)
266 tree->syms[++len] = i;
268 len = get_bits(gb, 2);
269 for (i = 0; i < 16; i++)
271 for (i = 0; i <= len; i++) {
273 for (t = 0; t < 16; t += size << 1)
274 merge(gb, out + t, in + t, size);
275 FFSWAP(uint8_t*, in, out);
277 memcpy(tree->syms, in, 16);
283 * Prepare bundle for decoding data.
285 * @param gb context for reading bits
286 * @param c decoder context
287 * @param bundle_num number of the bundle to initialize
289 static int read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
293 if (bundle_num == BINK_SRC_COLORS) {
294 for (i = 0; i < 16; i++) {
295 int ret = read_tree(gb, &c->col_high[i]);
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);
306 c->bundle[bundle_num].cur_dec =
307 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
313 * common check before starting decoding bundle data
315 * @param gb context for reading bits
317 * @param t variable where number of elements to decode will be stored
319 #define CHECK_READ_VAL(gb, b, t) \
320 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
322 t = get_bits(gb, b->len); \
328 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
331 const uint8_t *dec_end;
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;
339 if (get_bits_left(gb) < 1)
340 return AVERROR_INVALIDDATA;
343 memset(b->cur_dec, v, t);
346 while (b->cur_dec < dec_end)
347 *b->cur_dec++ = GET_HUFF(gb, b->tree);
352 static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
355 const uint8_t *dec_end;
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;
363 if (get_bits_left(gb) < 1)
364 return AVERROR_INVALIDDATA;
368 sign = -get_bits1(gb);
369 v = (v ^ sign) - sign;
371 memset(b->cur_dec, v, t);
374 while (b->cur_dec < dec_end) {
375 v = GET_HUFF(gb, b->tree);
377 sign = -get_bits1(gb);
378 v = (v ^ sign) - sign;
386 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
388 static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
390 BinkContext * const c = avctx->priv_data;
393 const uint8_t *dec_end;
395 CHECK_READ_VAL(gb, b, t);
396 if (c->version == 'k') {
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;
408 if (get_bits_left(gb) < 1)
409 return AVERROR_INVALIDDATA;
412 memset(b->cur_dec, v, t);
415 while (b->cur_dec < dec_end) {
416 v = GET_HUFF(gb, b->tree);
421 int run = bink_rlelens[v - 12];
423 if (dec_end - b->cur_dec < run)
424 return AVERROR_INVALIDDATA;
425 memset(b->cur_dec, last, run);
433 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
436 const uint8_t *dec_end;
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;
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;
455 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
458 const uint8_t *dec_end;
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;
466 if (get_bits_left(gb) < 1)
467 return AVERROR_INVALIDDATA;
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;
477 memset(b->cur_dec, v, t);
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;
497 /** number of bits used to store first DC value in bundle */
498 #define DC_START_BITS 11
500 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
501 int start_bits, int has_sign)
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;
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);
512 sign = -get_bits1(gb);
513 v = (v ^ sign) - sign;
515 if (dst_end - dst < 1)
516 return AVERROR_INVALIDDATA;
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);
525 for (j = 0; j < len2; j++) {
526 v2 = get_bits(gb, bsize);
528 sign = -get_bits1(gb);
529 v2 = (v2 ^ sign) - sign;
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;
539 for (j = 0; j < len2; j++)
544 b->cur_dec = (uint8_t*)dst;
549 * Retrieve next value from bundle.
551 * @param c decoder context
552 * @param bundle bundle number
554 static inline int get_value(BinkContext *c, int bundle)
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;
567 static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
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;
574 static av_cold void binkb_init_bundles(BinkContext *c)
577 for (i = 0; i < BINKB_NB_SRC; i++)
578 binkb_init_bundle(c, i);
581 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
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];
589 CHECK_READ_VAL(gb, b, len);
590 if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
591 return AVERROR_INVALIDDATA;
594 for (i = 0; i < len; i++)
595 *b->cur_dec++ = get_bits(gb, bits);
597 for (i = 0; i < len; i++)
598 *b->cur_dec++ = get_bits(gb, bits) - mask;
601 int16_t *dst = (int16_t*)b->cur_dec;
604 for (i = 0; i < len; i++)
605 *dst++ = get_bits(gb, bits);
607 for (i = 0; i < len; i++)
608 *dst++ = get_bits(gb, bits) - mask;
610 b->cur_dec = (uint8_t*)dst;
615 static inline int binkb_get_value(BinkContext *c, int bundle_num)
618 const int bits = binkb_bundle_sizes[bundle_num];
621 int val = *c->bundle[bundle_num].cur_ptr++;
622 return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
624 ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
625 c->bundle[bundle_num].cur_ptr += 2;
630 * Read 8x8 block of DCT coefficients.
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
638 static int read_dct_coeffs(BinkContext *c, GetBitContext *gb, int32_t block[64],
639 const uint8_t *scan, int *coef_count_,
640 int coef_idx[64], int q)
644 int i, t, bits, ccoef, mode, sign;
645 int list_start = 64, list_end = 64, list_pos;
649 if (get_bits_left(gb) < 4)
650 return AVERROR_INVALIDDATA;
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;
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)) {
666 ccoef = coef_list[list_pos];
667 mode = mode_list[list_pos];
670 coef_list[list_pos] = ccoef + 4;
671 mode_list[list_pos] = 1;
674 coef_list[list_pos] = 0;
675 mode_list[list_pos++] = 0;
677 for (i = 0; i < 4; i++, ccoef++) {
679 coef_list[--list_start] = ccoef;
680 mode_list[ list_start] = 3;
683 t = 1 - (get_bits1(gb) << 1);
685 t = get_bits(gb, bits) | 1 << bits;
686 sign = -get_bits1(gb);
687 t = (t ^ sign) - sign;
689 block[scan[ccoef]] = t;
690 coef_idx[coef_count++] = ccoef;
695 mode_list[list_pos] = 2;
696 for (i = 0; i < 3; i++) {
698 coef_list[list_end] = ccoef;
699 mode_list[list_end++] = 2;
704 t = 1 - (get_bits1(gb) << 1);
706 t = get_bits(gb, bits) | 1 << bits;
707 sign = -get_bits1(gb);
708 t = (t ^ sign) - sign;
710 block[scan[ccoef]] = t;
711 coef_idx[coef_count++] = ccoef;
712 coef_list[list_pos] = 0;
713 mode_list[list_pos++] = 0;
720 quant_idx = get_bits(gb, 4);
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;
729 *coef_count_ = coef_count;
734 static void unquantize_dct_coeffs(int32_t block[64], const uint32_t quant[64],
735 int coef_count, int coef_idx[64],
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;
747 * Read 8x8 block with residue after motion compensation.
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
754 static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
758 int i, sign, mask, ccoef, mode;
759 int list_start = 64, list_end = 64, list_pos;
761 int nz_coeff_count = 0;
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;
768 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
769 for (i = 0; i < nz_coeff_count; i++) {
772 if (block[nz_coeff[i]] < 0)
773 block[nz_coeff[i]] -= mask;
775 block[nz_coeff[i]] += mask;
780 list_pos = list_start;
781 while (list_pos < list_end) {
782 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
786 ccoef = coef_list[list_pos];
787 mode = mode_list[list_pos];
790 coef_list[list_pos] = ccoef + 4;
791 mode_list[list_pos] = 1;
794 coef_list[list_pos] = 0;
795 mode_list[list_pos++] = 0;
797 for (i = 0; i < 4; i++, ccoef++) {
799 coef_list[--list_start] = ccoef;
800 mode_list[ list_start] = 3;
802 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
803 sign = -get_bits1(gb);
804 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
812 mode_list[list_pos] = 2;
813 for (i = 0; i < 3; i++) {
815 coef_list[list_end] = ccoef;
816 mode_list[list_end++] = 2;
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;
837 * Copy 8x8 block from source to destination, where src and dst may be overlapped
839 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
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);
849 static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
850 int plane_idx, int is_key, int is_chroma)
854 uint8_t *dst, *ref, *ref_start, *ref_end;
858 LOCAL_ALIGNED_32(int16_t, block, [64]);
859 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
861 int ybias = is_key ? -15 : 0;
862 int qp, quant_idx, coef_count, coef_idx[64];
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;
868 binkb_init_bundles(c);
869 ref_start = frame->data[plane_idx];
870 ref_end = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
872 for (i = 0; i < 64; i++)
873 coordmap[i] = (i & 7) + (i >> 3) * stride;
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)
881 dst = frame->data[plane_idx] + 8*by*stride;
882 for (bx = 0; bx < bw; bx++, dst += 8) {
883 blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
888 scan = bink_patterns[get_bits(gb, 4)];
893 mode = get_bits1(gb);
894 run = get_bits(gb, binkb_runbits[i]) + 1;
898 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
899 return AVERROR_INVALIDDATA;
902 v = binkb_get_value(c, BINKB_SRC_COLORS);
903 for (j = 0; j < run; j++)
904 dst[coordmap[*scan++]] = v;
906 for (j = 0; j < run; j++)
907 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
911 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
914 memset(dctblock, 0, sizeof(*dctblock) * 64);
915 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
916 qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
917 if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
919 unquantize_dct_coeffs(dctblock, binkb_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
920 c->binkdsp.idct_put(dst, stride, dctblock);
923 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
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);
931 put_pixels8x8_overlapped(dst, ref, stride);
933 c->bdsp.clear_block(block);
934 v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
935 read_residue(gb, block, v);
936 c->binkdsp.add_pixels8(dst, block, stride);
939 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
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);
947 put_pixels8x8_overlapped(dst, ref, stride);
949 memset(dctblock, 0, sizeof(*dctblock) * 64);
950 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
951 qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
952 if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
954 unquantize_dct_coeffs(dctblock, binkb_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
955 c->binkdsp.idct_add(dst, stride, dctblock);
958 v = binkb_get_value(c, BINKB_SRC_COLORS);
959 c->bdsp.fill_block_tab[1](dst, v, stride, 8);
962 for (i = 0; i < 2; i++)
963 col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
964 for (i = 0; i < 8; i++) {
965 v = binkb_get_value(c, BINKB_SRC_PATTERN);
966 for (j = 0; j < 8; j++, v >>= 1)
967 dst[i*stride + j] = col[v & 1];
971 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
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);
979 put_pixels8x8_overlapped(dst, ref, stride);
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;
988 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
989 return AVERROR_INVALIDDATA;
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));
999 static int bink_put_pixels(BinkContext *c,
1000 uint8_t *dst, uint8_t *prev, int stride,
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",
1010 return AVERROR_INVALIDDATA;
1012 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1017 static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
1018 int plane_idx, int is_chroma)
1022 uint8_t *dst, *prev, *ref_start, *ref_end;
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];
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;
1036 if (c->version == 'k' && get_bits1(gb)) {
1037 int fill = get_bits(gb, 8);
1039 dst = frame->data[plane_idx];
1041 for (i = 0; i < height; i++)
1042 memset(dst + i * stride, fill, width);
1046 init_lengths(c, FFMAX(width, 8), bw);
1047 for (i = 0; i < BINK_NB_SRC; i++) {
1048 ret = read_bundle(gb, c, i);
1053 ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
1054 : frame->data[plane_idx];
1056 + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
1058 for (i = 0; i < 64; i++)
1059 coordmap[i] = (i & 7) + (i >> 3) * stride;
1061 for (by = 0; by < bh; by++) {
1062 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
1064 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
1066 if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
1068 if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
1070 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
1072 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
1074 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
1076 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
1078 if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
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) {
1085 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
1086 // 16x16 block type on odd line means part of the already decoded block, so skip it
1087 if ((by & 1) && blk == SCALED_BLOCK) {
1095 c->hdsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1098 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
1101 if (get_bits_left(gb) < 4)
1102 return AVERROR_INVALIDDATA;
1103 scan = bink_patterns[get_bits(gb, 4)];
1106 int run = get_value(c, BINK_SRC_RUN) + 1;
1110 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1111 return AVERROR_INVALIDDATA;
1113 if (get_bits1(gb)) {
1114 v = get_value(c, BINK_SRC_COLORS);
1115 for (j = 0; j < run; j++)
1116 ublock[*scan++] = v;
1118 for (j = 0; j < run; j++)
1119 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1123 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
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)
1130 unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1131 c->binkdsp.idct_put(ublock, 8, dctblock);
1134 v = get_value(c, BINK_SRC_COLORS);
1135 c->bdsp.fill_block_tab[0](dst, v, stride, 16);
1138 for (i = 0; i < 2; i++)
1139 col[i] = get_value(c, BINK_SRC_COLORS);
1140 for (j = 0; j < 8; j++) {
1141 v = get_value(c, BINK_SRC_PATTERN);
1142 for (i = 0; i < 8; i++, v >>= 1)
1143 ublock[i + j*8] = col[v & 1];
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);
1152 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1153 return AVERROR_INVALIDDATA;
1155 if (blk != FILL_BLOCK)
1156 c->binkdsp.scale_block(ublock, dst, stride);
1162 ret = bink_put_pixels(c, dst, prev, stride,
1163 ref_start, ref_end);
1168 scan = bink_patterns[get_bits(gb, 4)];
1171 int run = get_value(c, BINK_SRC_RUN) + 1;
1175 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1176 return AVERROR_INVALIDDATA;
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;
1183 for (j = 0; j < run; j++)
1184 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1188 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1191 ret = bink_put_pixels(c, dst, prev, stride,
1192 ref_start, ref_end);
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);
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)
1205 unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1206 c->binkdsp.idct_put(dst, stride, dctblock);
1209 v = get_value(c, BINK_SRC_COLORS);
1210 c->bdsp.fill_block_tab[1](dst, v, stride, 8);
1213 ret = bink_put_pixels(c, dst, prev, stride,
1214 ref_start, ref_end);
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)
1221 unquantize_dct_coeffs(dctblock, bink_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
1222 c->binkdsp.idct_add(dst, stride, dctblock);
1225 for (i = 0; i < 2; i++)
1226 col[i] = get_value(c, BINK_SRC_COLORS);
1227 for (i = 0; i < 8; i++) {
1228 v = get_value(c, BINK_SRC_PATTERN);
1229 for (j = 0; j < 8; j++, v >>= 1)
1230 dst[i*stride + j] = col[v & 1];
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;
1239 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1240 return AVERROR_INVALIDDATA;
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));
1252 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
1254 BinkContext * const c = avctx->priv_data;
1255 AVFrame *frame = data;
1257 int plane, plane_idx, ret;
1258 int bits_count = pkt->size << 3;
1260 if (c->version > 'b') {
1261 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1264 if ((ret = ff_reget_buffer(avctx, c->last, 0)) < 0)
1266 if ((ret = av_frame_ref(frame, c->last)) < 0)
1270 init_get_bits(&gb, pkt->data, bits_count);
1272 if (c->version >= 'i')
1273 skip_bits_long(&gb, 32);
1274 if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
1277 if (c->version >= 'i')
1278 skip_bits_long(&gb, 32);
1282 for (plane = 0; plane < 3; plane++) {
1283 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1285 if (c->version > 'b') {
1286 if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
1289 if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
1290 c->frame_num == 1, !!plane)) < 0)
1293 if (get_bits_count(&gb) >= bits_count)
1298 if (c->version > 'b') {
1299 av_frame_unref(c->last);
1300 if ((ret = av_frame_ref(c->last, frame)) < 0)
1306 /* always report that the buffer was completely consumed */
1311 * Calculate quantization tables for version b
1313 static av_cold void binkb_calc_quant(void)
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,
1328 for (i = 0; i < 64; i++)
1329 inv_bink_scan[bink_scan[i]] = i;
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));
1342 static av_cold int decode_init(AVCodecContext *avctx)
1344 BinkContext * const c = avctx->priv_data;
1345 static VLC_TYPE table[16 * 128][2];
1346 static int binkb_initialised = 0;
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;
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,
1365 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1370 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1373 c->last = av_frame_alloc();
1375 return AVERROR(ENOMEM);
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;
1380 ff_blockdsp_init(&c->bdsp, avctx);
1381 ff_hpeldsp_init(&c->hdsp, avctx->flags);
1382 ff_binkdsp_init(&c->binkdsp);
1384 if ((ret = init_bundles(c)) < 0) {
1389 if (c->version == 'b') {
1390 if (!binkb_initialised) {
1392 binkb_initialised = 1;
1399 static av_cold int decode_end(AVCodecContext *avctx)
1401 BinkContext * const c = avctx->priv_data;
1403 av_frame_free(&c->last);
1409 static void flush(AVCodecContext *avctx)
1411 BinkContext * const c = avctx->priv_data;
1416 AVCodec ff_bink_decoder = {
1417 .name = "binkvideo",
1418 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
1419 .type = AVMEDIA_TYPE_VIDEO,
1420 .id = AV_CODEC_ID_BINKVIDEO,
1421 .priv_data_size = sizeof(BinkContext),
1422 .init = decode_init,
1423 .close = decode_end,
1424 .decode = decode_frame,
1426 .capabilities = AV_CODEC_CAP_DR1,