3 * Copyright (c) 2009 Konstantin Shishkov
4 * Copyright (C) 2011 Peter Ross <pross@xvid.org>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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"
30 #define ALT_BITSTREAM_READER_LE
33 #define BINK_FLAG_ALPHA 0x00100000
34 #define BINK_FLAG_GRAY 0x00020000
36 static VLC bink_trees[16];
39 * IDs for different data types used in old version of Bink video codec
42 BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
43 BINKB_SRC_COLORS, ///< pixel values used for different block types
44 BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
45 BINKB_SRC_X_OFF, ///< X components of motion value
46 BINKB_SRC_Y_OFF, ///< Y components of motion value
47 BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
48 BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
49 BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
50 BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
51 BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
56 static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
57 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
60 static const int binkb_bundle_signed[BINKB_NB_SRC] = {
61 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
64 static int32_t binkb_intra_quant[16][64];
65 static int32_t binkb_inter_quant[16][64];
68 * IDs for different data types used in Bink video codec
71 BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
72 BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
73 BINK_SRC_COLORS, ///< pixel values used for different block types
74 BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
75 BINK_SRC_X_OFF, ///< X components of motion value
76 BINK_SRC_Y_OFF, ///< Y components of motion value
77 BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
78 BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
79 BINK_SRC_RUN, ///< run lengths for special fill block
85 * data needed to decode 4-bit Huffman-coded value
88 int vlc_num; ///< tree number (in bink_trees[])
89 uint8_t syms[16]; ///< leaf value to symbol mapping
92 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
93 bink_trees[(tree).vlc_num].bits, 1)]
96 * data structure used for decoding single Bink data type
98 typedef struct Bundle {
99 int len; ///< length of number of entries to decode (in bits)
100 Tree tree; ///< Huffman tree-related data
101 uint8_t *data; ///< buffer for decoded symbols
102 uint8_t *data_end; ///< buffer end
103 uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
104 uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
110 typedef struct BinkContext {
111 AVCodecContext *avctx;
115 int version; ///< internal Bink file version
119 Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
120 Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
121 int col_lastval; ///< value of last decoded high nibble in "colours" data type
125 * Bink video block types
128 SKIP_BLOCK = 0, ///< skipped block
129 SCALED_BLOCK, ///< block has size 16x16
130 MOTION_BLOCK, ///< block is copied from previous frame with some offset
131 RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
132 RESIDUE_BLOCK, ///< motion block with some difference added
133 INTRA_BLOCK, ///< intra DCT block
134 FILL_BLOCK, ///< block is filled with single colour
135 INTER_BLOCK, ///< motion block with DCT applied to the difference
136 PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
137 RAW_BLOCK, ///< uncoded 8x8 block
141 * Initialize length length in all bundles.
143 * @param c decoder context
144 * @param width plane width
145 * @param bw plane width in 8x8 blocks
147 static void init_lengths(BinkContext *c, int width, int bw)
149 c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
151 c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
153 c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
155 c->bundle[BINK_SRC_INTRA_DC].len =
156 c->bundle[BINK_SRC_INTER_DC].len =
157 c->bundle[BINK_SRC_X_OFF].len =
158 c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
160 c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
162 c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
166 * Allocate memory for bundles.
168 * @param c decoder context
170 static av_cold void init_bundles(BinkContext *c)
175 bw = (c->avctx->width + 7) >> 3;
176 bh = (c->avctx->height + 7) >> 3;
179 for (i = 0; i < BINKB_NB_SRC; i++) {
180 c->bundle[i].data = av_malloc(blocks * 64);
181 c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
186 * Free memory used by bundles.
188 * @param c decoder context
190 static av_cold void free_bundles(BinkContext *c)
193 for (i = 0; i < BINKB_NB_SRC; i++)
194 av_freep(&c->bundle[i].data);
198 * Merge two consequent lists of equal size depending on bits read.
200 * @param gb context for reading bits
201 * @param dst buffer where merged list will be written to
202 * @param src pointer to the head of the first list (the second lists starts at src+size)
203 * @param size input lists size
205 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
207 uint8_t *src2 = src + size;
211 if (!get_bits1(gb)) {
218 } while (size && size2);
227 * Read information about Huffman tree used to decode data.
229 * @param gb context for reading bits
230 * @param tree pointer for storing tree data
232 static void read_tree(GetBitContext *gb, Tree *tree)
234 uint8_t tmp1[16], tmp2[16], *in = tmp1, *out = tmp2;
237 tree->vlc_num = get_bits(gb, 4);
238 if (!tree->vlc_num) {
239 for (i = 0; i < 16; i++)
244 len = get_bits(gb, 3);
245 memset(tmp1, 0, sizeof(tmp1));
246 for (i = 0; i <= len; i++) {
247 tree->syms[i] = get_bits(gb, 4);
248 tmp1[tree->syms[i]] = 1;
250 for (i = 0; i < 16; i++)
252 tree->syms[++len] = i;
254 len = get_bits(gb, 2);
255 for (i = 0; i < 16; i++)
257 for (i = 0; i <= len; i++) {
259 for (t = 0; t < 16; t += size << 1)
260 merge(gb, out + t, in + t, size);
261 FFSWAP(uint8_t*, in, out);
263 memcpy(tree->syms, in, 16);
268 * Prepare bundle for decoding data.
270 * @param gb context for reading bits
271 * @param c decoder context
272 * @param bundle_num number of the bundle to initialize
274 static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
278 if (bundle_num == BINK_SRC_COLORS) {
279 for (i = 0; i < 16; i++)
280 read_tree(gb, &c->col_high[i]);
283 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
284 read_tree(gb, &c->bundle[bundle_num].tree);
285 c->bundle[bundle_num].cur_dec =
286 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
290 * common check before starting decoding bundle data
292 * @param gb context for reading bits
294 * @param t variable where number of elements to decode will be stored
296 #define CHECK_READ_VAL(gb, b, t) \
297 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
299 t = get_bits(gb, b->len); \
305 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
308 const uint8_t *dec_end;
310 CHECK_READ_VAL(gb, b, t);
311 dec_end = b->cur_dec + t;
312 if (dec_end > b->data_end) {
313 av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
318 memset(b->cur_dec, v, t);
321 while (b->cur_dec < dec_end)
322 *b->cur_dec++ = GET_HUFF(gb, b->tree);
327 static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
330 const uint8_t *dec_end;
332 CHECK_READ_VAL(gb, b, t);
333 dec_end = b->cur_dec + t;
334 if (dec_end > b->data_end) {
335 av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
341 sign = -get_bits1(gb);
342 v = (v ^ sign) - sign;
344 memset(b->cur_dec, v, t);
348 v = GET_HUFF(gb, b->tree);
350 sign = -get_bits1(gb);
351 v = (v ^ sign) - sign;
354 } while (b->cur_dec < dec_end);
359 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
361 static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
365 const uint8_t *dec_end;
367 CHECK_READ_VAL(gb, b, t);
368 dec_end = b->cur_dec + t;
369 if (dec_end > b->data_end) {
370 av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
375 memset(b->cur_dec, v, t);
379 v = GET_HUFF(gb, b->tree);
384 int run = bink_rlelens[v - 12];
386 memset(b->cur_dec, last, run);
389 } while (b->cur_dec < dec_end);
394 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
397 const uint8_t *dec_end;
399 CHECK_READ_VAL(gb, b, t);
400 dec_end = b->cur_dec + t;
401 if (dec_end > b->data_end) {
402 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
405 while (b->cur_dec < dec_end) {
406 v = GET_HUFF(gb, b->tree);
407 v |= GET_HUFF(gb, b->tree) << 4;
414 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
417 const uint8_t *dec_end;
419 CHECK_READ_VAL(gb, b, t);
420 dec_end = b->cur_dec + t;
421 if (dec_end > b->data_end) {
422 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
426 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
427 v = GET_HUFF(gb, b->tree);
428 v = (c->col_lastval << 4) | v;
429 if (c->version < 'i') {
430 sign = ((int8_t) v) >> 7;
431 v = ((v & 0x7F) ^ sign) - sign;
434 memset(b->cur_dec, v, t);
437 while (b->cur_dec < dec_end) {
438 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
439 v = GET_HUFF(gb, b->tree);
440 v = (c->col_lastval << 4) | v;
441 if (c->version < 'i') {
442 sign = ((int8_t) v) >> 7;
443 v = ((v & 0x7F) ^ sign) - sign;
452 /** number of bits used to store first DC value in bundle */
453 #define DC_START_BITS 11
455 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
456 int start_bits, int has_sign)
458 int i, j, len, len2, bsize, sign, v, v2;
459 int16_t *dst = (int16_t*)b->cur_dec;
461 CHECK_READ_VAL(gb, b, len);
462 v = get_bits(gb, start_bits - has_sign);
464 sign = -get_bits1(gb);
465 v = (v ^ sign) - sign;
469 for (i = 0; i < len; i += 8) {
470 len2 = FFMIN(len - i, 8);
471 bsize = get_bits(gb, 4);
473 for (j = 0; j < len2; j++) {
474 v2 = get_bits(gb, bsize);
476 sign = -get_bits1(gb);
477 v2 = (v2 ^ sign) - sign;
481 if (v < -32768 || v > 32767) {
482 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
487 for (j = 0; j < len2; j++)
492 b->cur_dec = (uint8_t*)dst;
497 * Retrieve next value from bundle.
499 * @param c decoder context
500 * @param bundle bundle number
502 static inline int get_value(BinkContext *c, int bundle)
506 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
507 return *c->bundle[bundle].cur_ptr++;
508 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
509 return (int8_t)*c->bundle[bundle].cur_ptr++;
510 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
511 c->bundle[bundle].cur_ptr += 2;
515 static void binkb_init_bundle(BinkContext *c, int bundle_num)
517 c->bundle[bundle_num].cur_dec =
518 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
519 c->bundle[bundle_num].len = 13;
522 static void binkb_init_bundles(BinkContext *c)
525 for (i = 0; i < BINKB_NB_SRC; i++)
526 binkb_init_bundle(c, i);
529 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
531 const int bits = binkb_bundle_sizes[bundle_num];
532 const int mask = 1 << (bits - 1);
533 const int issigned = binkb_bundle_signed[bundle_num];
534 Bundle *b = &c->bundle[bundle_num];
537 CHECK_READ_VAL(gb, b, len);
540 for (i = 0; i < len; i++)
541 *b->cur_dec++ = get_bits(gb, bits);
543 for (i = 0; i < len; i++)
544 *b->cur_dec++ = get_bits(gb, bits) - mask;
547 int16_t *dst = (int16_t*)b->cur_dec;
550 for (i = 0; i < len; i++)
551 *dst++ = get_bits(gb, bits);
553 for (i = 0; i < len; i++)
554 *dst++ = get_bits(gb, bits) - mask;
556 b->cur_dec = (uint8_t*)dst;
561 static inline int binkb_get_value(BinkContext *c, int bundle_num)
564 const int bits = binkb_bundle_sizes[bundle_num];
567 int val = *c->bundle[bundle_num].cur_ptr++;
568 return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
570 ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
571 c->bundle[bundle_num].cur_ptr += 2;
576 * Read 8x8 block of DCT coefficients.
578 * @param gb context for reading bits
579 * @param block place for storing coefficients
580 * @param scan scan order table
581 * @param quant_matrices quantization matrices
582 * @return 0 for success, negative value in other cases
584 static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
585 const int32_t quant_matrices[16][64], int q)
589 int i, t, mask, bits, ccoef, mode, sign;
590 int list_start = 64, list_end = 64, list_pos;
594 const int32_t *quant;
596 coef_list[list_end] = 4; mode_list[list_end++] = 0;
597 coef_list[list_end] = 24; mode_list[list_end++] = 0;
598 coef_list[list_end] = 44; mode_list[list_end++] = 0;
599 coef_list[list_end] = 1; mode_list[list_end++] = 3;
600 coef_list[list_end] = 2; mode_list[list_end++] = 3;
601 coef_list[list_end] = 3; mode_list[list_end++] = 3;
603 bits = get_bits(gb, 4) - 1;
604 for (mask = 1 << bits; bits >= 0; mask >>= 1, bits--) {
605 list_pos = list_start;
606 while (list_pos < list_end) {
607 if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
611 ccoef = coef_list[list_pos];
612 mode = mode_list[list_pos];
615 coef_list[list_pos] = ccoef + 4;
616 mode_list[list_pos] = 1;
619 coef_list[list_pos] = 0;
620 mode_list[list_pos++] = 0;
622 for (i = 0; i < 4; i++, ccoef++) {
624 coef_list[--list_start] = ccoef;
625 mode_list[ list_start] = 3;
628 t = 1 - (get_bits1(gb) << 1);
630 t = get_bits(gb, bits) | mask;
631 sign = -get_bits1(gb);
632 t = (t ^ sign) - sign;
634 block[scan[ccoef]] = t;
635 coef_idx[coef_count++] = ccoef;
640 mode_list[list_pos] = 2;
641 for (i = 0; i < 3; i++) {
643 coef_list[list_end] = ccoef;
644 mode_list[list_end++] = 2;
649 t = 1 - (get_bits1(gb) << 1);
651 t = get_bits(gb, bits) | mask;
652 sign = -get_bits1(gb);
653 t = (t ^ sign) - sign;
655 block[scan[ccoef]] = t;
656 coef_idx[coef_count++] = ccoef;
657 coef_list[list_pos] = 0;
658 mode_list[list_pos++] = 0;
665 quant_idx = get_bits(gb, 4);
670 quant = quant_matrices[quant_idx];
672 block[0] = (block[0] * quant[0]) >> 11;
673 for (i = 0; i < coef_count; i++) {
674 int idx = coef_idx[i];
675 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
682 * Read 8x8 block with residue after motion compensation.
684 * @param gb context for reading bits
685 * @param block place to store read data
686 * @param masks_count number of masks to decode
687 * @return 0 on success, negative value in other cases
689 static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
693 int i, sign, mask, ccoef, mode;
694 int list_start = 64, list_end = 64, list_pos;
696 int nz_coeff_count = 0;
698 coef_list[list_end] = 4; mode_list[list_end++] = 0;
699 coef_list[list_end] = 24; mode_list[list_end++] = 0;
700 coef_list[list_end] = 44; mode_list[list_end++] = 0;
701 coef_list[list_end] = 0; mode_list[list_end++] = 2;
703 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
704 for (i = 0; i < nz_coeff_count; i++) {
707 if (block[nz_coeff[i]] < 0)
708 block[nz_coeff[i]] -= mask;
710 block[nz_coeff[i]] += mask;
715 list_pos = list_start;
716 while (list_pos < list_end) {
717 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
721 ccoef = coef_list[list_pos];
722 mode = mode_list[list_pos];
725 coef_list[list_pos] = ccoef + 4;
726 mode_list[list_pos] = 1;
729 coef_list[list_pos] = 0;
730 mode_list[list_pos++] = 0;
732 for (i = 0; i < 4; i++, ccoef++) {
734 coef_list[--list_start] = ccoef;
735 mode_list[ list_start] = 3;
737 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
738 sign = -get_bits1(gb);
739 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
747 mode_list[list_pos] = 2;
748 for (i = 0; i < 3; i++) {
750 coef_list[list_end] = ccoef;
751 mode_list[list_end++] = 2;
755 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
756 sign = -get_bits1(gb);
757 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
758 coef_list[list_pos] = 0;
759 mode_list[list_pos++] = 0;
772 * Copy 8x8 block from source to destination, where src and dst may be overlapped
774 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
778 for (i = 0; i < 8; i++)
779 memcpy(tmp + i*8, src + i*stride, 8);
780 for (i = 0; i < 8; i++)
781 memcpy(dst + i*stride, tmp + i*8, 8);
784 static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
785 int is_key, int is_chroma)
789 uint8_t *dst, *ref, *ref_start, *ref_end;
793 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
794 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
796 int ybias = is_key ? -15 : 0;
799 const int stride = c->pic.linesize[plane_idx];
800 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
801 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
803 binkb_init_bundles(c);
804 ref_start = c->pic.data[plane_idx];
805 ref_end = c->pic.data[plane_idx] + (bh * c->pic.linesize[plane_idx] + bw) * 8;
807 for (i = 0; i < 64; i++)
808 coordmap[i] = (i & 7) + (i >> 3) * stride;
810 for (by = 0; by < bh; by++) {
811 for (i = 0; i < BINKB_NB_SRC; i++) {
812 if (binkb_read_bundle(c, gb, i) < 0)
816 dst = c->pic.data[plane_idx] + 8*by*stride;
817 for (bx = 0; bx < bw; bx++, dst += 8) {
818 blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
823 scan = bink_patterns[get_bits(gb, 4)];
828 mode = get_bits1(gb);
829 run = get_bits(gb, binkb_runbits[i]) + 1;
833 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
837 v = binkb_get_value(c, BINKB_SRC_COLORS);
838 for (j = 0; j < run; j++)
839 dst[coordmap[*scan++]] = v;
841 for (j = 0; j < run; j++)
842 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
846 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
849 memset(dctblock, 0, sizeof(*dctblock) * 64);
850 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
851 qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
852 read_dct_coeffs(gb, dctblock, bink_scan, binkb_intra_quant, qp);
853 c->bdsp.idct_put(dst, stride, dctblock);
856 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
857 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
858 ref = dst + xoff + yoff * stride;
859 if (ref < ref_start || ref + 8*stride > ref_end) {
860 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
861 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
862 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
864 put_pixels8x8_overlapped(dst, ref, stride);
866 c->dsp.clear_block(block);
867 v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
868 read_residue(gb, block, v);
869 c->dsp.add_pixels8(dst, block, stride);
872 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
873 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
874 ref = dst + xoff + yoff * stride;
875 if (ref < ref_start || ref + 8 * stride > ref_end) {
876 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
877 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
878 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
880 put_pixels8x8_overlapped(dst, ref, stride);
882 memset(dctblock, 0, sizeof(*dctblock) * 64);
883 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
884 qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
885 read_dct_coeffs(gb, dctblock, bink_scan, binkb_inter_quant, qp);
886 c->bdsp.idct_add(dst, stride, dctblock);
889 v = binkb_get_value(c, BINKB_SRC_COLORS);
890 c->dsp.fill_block_tab[1](dst, v, stride, 8);
893 for (i = 0; i < 2; i++)
894 col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
895 for (i = 0; i < 8; i++) {
896 v = binkb_get_value(c, BINKB_SRC_PATTERN);
897 for (j = 0; j < 8; j++, v >>= 1)
898 dst[i*stride + j] = col[v & 1];
902 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
903 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
904 ref = dst + xoff + yoff * stride;
905 if (ref < ref_start || ref + 8 * stride > ref_end) {
906 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
907 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
908 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
910 put_pixels8x8_overlapped(dst, ref, stride);
914 for (i = 0; i < 8; i++)
915 memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
916 c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
919 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
924 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
925 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
930 static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
935 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
939 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
940 LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
941 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
944 const int stride = c->pic.linesize[plane_idx];
945 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
946 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
947 int width = c->avctx->width >> is_chroma;
949 init_lengths(c, FFMAX(width, 8), bw);
950 for (i = 0; i < BINK_NB_SRC; i++)
951 read_bundle(gb, c, i);
953 ref_start = c->last.data[plane_idx] ? c->last.data[plane_idx]
954 : c->pic.data[plane_idx];
956 + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
958 for (i = 0; i < 64; i++)
959 coordmap[i] = (i & 7) + (i >> 3) * stride;
961 for (by = 0; by < bh; by++) {
962 if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
964 if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
966 if (read_colors(gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
968 if (read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
970 if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
972 if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
974 if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
976 if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
978 if (read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN]) < 0)
983 dst = c->pic.data[plane_idx] + 8*by*stride;
984 prev = (c->last.data[plane_idx] ? c->last.data[plane_idx]
985 : c->pic.data[plane_idx]) + 8*by*stride;
986 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
987 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
988 // 16x16 block type on odd line means part of the already decoded block, so skip it
989 if ((by & 1) && blk == SCALED_BLOCK) {
997 c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1000 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
1003 scan = bink_patterns[get_bits(gb, 4)];
1006 int run = get_value(c, BINK_SRC_RUN) + 1;
1010 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1013 if (get_bits1(gb)) {
1014 v = get_value(c, BINK_SRC_COLORS);
1015 for (j = 0; j < run; j++)
1016 ublock[*scan++] = v;
1018 for (j = 0; j < run; j++)
1019 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1023 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1026 memset(dctblock, 0, sizeof(*dctblock) * 64);
1027 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1028 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1029 c->bdsp.idct_put(ublock, 8, dctblock);
1032 v = get_value(c, BINK_SRC_COLORS);
1033 c->dsp.fill_block_tab[0](dst, v, stride, 16);
1036 for (i = 0; i < 2; i++)
1037 col[i] = get_value(c, BINK_SRC_COLORS);
1038 for (j = 0; j < 8; j++) {
1039 v = get_value(c, BINK_SRC_PATTERN);
1040 for (i = 0; i < 8; i++, v >>= 1)
1041 ublock[i + j*8] = col[v & 1];
1045 for (j = 0; j < 8; j++)
1046 for (i = 0; i < 8; i++)
1047 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1050 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1053 if (blk != FILL_BLOCK)
1054 c->bdsp.scale_block(ublock, dst, stride);
1060 xoff = get_value(c, BINK_SRC_X_OFF);
1061 yoff = get_value(c, BINK_SRC_Y_OFF);
1062 ref = prev + xoff + yoff * stride;
1063 if (ref < ref_start || ref > ref_end) {
1064 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1065 bx*8 + xoff, by*8 + yoff);
1068 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1071 scan = bink_patterns[get_bits(gb, 4)];
1074 int run = get_value(c, BINK_SRC_RUN) + 1;
1078 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1081 if (get_bits1(gb)) {
1082 v = get_value(c, BINK_SRC_COLORS);
1083 for (j = 0; j < run; j++)
1084 dst[coordmap[*scan++]] = v;
1086 for (j = 0; j < run; j++)
1087 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1091 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1094 xoff = get_value(c, BINK_SRC_X_OFF);
1095 yoff = get_value(c, BINK_SRC_Y_OFF);
1096 ref = prev + xoff + yoff * stride;
1097 if (ref < ref_start || ref > ref_end) {
1098 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1099 bx*8 + xoff, by*8 + yoff);
1102 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1103 c->dsp.clear_block(block);
1104 v = get_bits(gb, 7);
1105 read_residue(gb, block, v);
1106 c->dsp.add_pixels8(dst, block, stride);
1109 memset(dctblock, 0, sizeof(*dctblock) * 64);
1110 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1111 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1112 c->bdsp.idct_put(dst, stride, dctblock);
1115 v = get_value(c, BINK_SRC_COLORS);
1116 c->dsp.fill_block_tab[1](dst, v, stride, 8);
1119 xoff = get_value(c, BINK_SRC_X_OFF);
1120 yoff = get_value(c, BINK_SRC_Y_OFF);
1121 ref = prev + xoff + yoff * stride;
1122 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1123 memset(dctblock, 0, sizeof(*dctblock) * 64);
1124 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1125 read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
1126 c->bdsp.idct_add(dst, stride, dctblock);
1129 for (i = 0; i < 2; i++)
1130 col[i] = get_value(c, BINK_SRC_COLORS);
1131 for (i = 0; i < 8; i++) {
1132 v = get_value(c, BINK_SRC_PATTERN);
1133 for (j = 0; j < 8; j++, v >>= 1)
1134 dst[i*stride + j] = col[v & 1];
1138 for (i = 0; i < 8; i++)
1139 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1140 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1143 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1148 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1149 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1154 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
1156 BinkContext * const c = avctx->priv_data;
1158 int plane, plane_idx;
1159 int bits_count = pkt->size << 3;
1161 if (c->version > 'b') {
1163 avctx->release_buffer(avctx, &c->pic);
1165 if(avctx->get_buffer(avctx, &c->pic) < 0){
1166 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1170 if(avctx->reget_buffer(avctx, &c->pic) < 0){
1171 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1176 init_get_bits(&gb, pkt->data, bits_count);
1178 if (c->version >= 'i')
1179 skip_bits_long(&gb, 32);
1180 if (bink_decode_plane(c, &gb, 3, 0) < 0)
1183 if (c->version >= 'i')
1184 skip_bits_long(&gb, 32);
1186 for (plane = 0; plane < 3; plane++) {
1187 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1189 if (c->version > 'b') {
1190 if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
1193 if (binkb_decode_plane(c, &gb, plane_idx, !pkt->pts, !!plane) < 0)
1196 if (get_bits_count(&gb) >= bits_count)
1201 *data_size = sizeof(AVFrame);
1202 *(AVFrame*)data = c->pic;
1204 if (c->version > 'b')
1205 FFSWAP(AVFrame, c->pic, c->last);
1207 /* always report that the buffer was completely consumed */
1212 * Caclulate quantization tables for version b
1214 static av_cold void binkb_calc_quant(void)
1216 uint8_t inv_bink_scan[64];
1220 for (j = 0; j < 8; j++) {
1221 for (i = 0; i < 8; i++) {
1224 s[j*8 + i] = cos(j * M_PI/16.0) * cos(i * M_PI/16.0) * 2.0;
1226 s[j*8 + i] = cos(j * M_PI/16.0) * sqrt(2.0);
1229 s[j*8 + i] = cos(i * M_PI/16.0) * sqrt(2.0);
1235 for (i = 0; i < 64; i++)
1236 inv_bink_scan[bink_scan[i]] = i;
1238 for (j = 0; j < 16; j++) {
1239 for (i = 0; i < 64; i++) {
1240 int k = inv_bink_scan[i];
1242 binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] *
1243 binkb_num[j]/binkb_den[j];
1244 binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] *
1245 binkb_num[j]/binkb_den[j];
1247 binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] * s[i] *
1248 binkb_num[j]/(double)binkb_den[j];
1249 binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] * s[i] *
1250 binkb_num[j]/(double)binkb_den[j];
1256 static av_cold int decode_init(AVCodecContext *avctx)
1258 BinkContext * const c = avctx->priv_data;
1259 static VLC_TYPE table[16 * 128][2];
1260 static int binkb_initialised = 0;
1264 c->version = avctx->codec_tag >> 24;
1265 if (avctx->extradata_size < 4) {
1266 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1269 flags = AV_RL32(avctx->extradata);
1270 c->has_alpha = flags & BINK_FLAG_ALPHA;
1271 c->swap_planes = c->version >= 'h';
1272 if (!bink_trees[15].table) {
1273 for (i = 0; i < 16; i++) {
1274 const int maxbits = bink_tree_lens[i][15];
1275 bink_trees[i].table = table + i*128;
1276 bink_trees[i].table_allocated = 1 << maxbits;
1277 init_vlc(&bink_trees[i], maxbits, 16,
1278 bink_tree_lens[i], 1, 1,
1279 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1284 c->pic.data[0] = NULL;
1286 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
1290 avctx->pix_fmt = c->has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
1292 avctx->idct_algo = FF_IDCT_BINK;
1293 dsputil_init(&c->dsp, avctx);
1294 ff_binkdsp_init(&c->bdsp);
1298 if (c->version == 'b') {
1299 if (!binkb_initialised) {
1301 binkb_initialised = 1;
1308 static av_cold int decode_end(AVCodecContext *avctx)
1310 BinkContext * const c = avctx->priv_data;
1313 avctx->release_buffer(avctx, &c->pic);
1314 if (c->last.data[0])
1315 avctx->release_buffer(avctx, &c->last);
1321 AVCodec ff_bink_decoder = {
1322 .name = "binkvideo",
1323 .type = AVMEDIA_TYPE_VIDEO,
1324 .id = CODEC_ID_BINKVIDEO,
1325 .priv_data_size = sizeof(BinkContext),
1326 .init = decode_init,
1327 .close = decode_end,
1328 .decode = decode_frame,
1329 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),