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"
30 #define 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 width = FFALIGN(width, 8);
151 c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
153 c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
155 c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
157 c->bundle[BINK_SRC_INTRA_DC].len =
158 c->bundle[BINK_SRC_INTER_DC].len =
159 c->bundle[BINK_SRC_X_OFF].len =
160 c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
162 c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
164 c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
168 * Allocate memory for bundles.
170 * @param c decoder context
172 static av_cold void init_bundles(BinkContext *c)
177 bw = (c->avctx->width + 7) >> 3;
178 bh = (c->avctx->height + 7) >> 3;
181 for (i = 0; i < BINKB_NB_SRC; i++) {
182 c->bundle[i].data = av_malloc(blocks * 64);
183 c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
188 * Free memory used by bundles.
190 * @param c decoder context
192 static av_cold void free_bundles(BinkContext *c)
195 for (i = 0; i < BINKB_NB_SRC; i++)
196 av_freep(&c->bundle[i].data);
200 * Merge two consequent lists of equal size depending on bits read.
202 * @param gb context for reading bits
203 * @param dst buffer where merged list will be written to
204 * @param src pointer to the head of the first list (the second lists starts at src+size)
205 * @param size input lists size
207 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
209 uint8_t *src2 = src + size;
213 if (!get_bits1(gb)) {
220 } while (size && size2);
229 * Read information about Huffman tree used to decode data.
231 * @param gb context for reading bits
232 * @param tree pointer for storing tree data
234 static void read_tree(GetBitContext *gb, Tree *tree)
236 uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
239 tree->vlc_num = get_bits(gb, 4);
240 if (!tree->vlc_num) {
241 for (i = 0; i < 16; i++)
246 len = get_bits(gb, 3);
247 for (i = 0; i <= len; i++) {
248 tree->syms[i] = get_bits(gb, 4);
249 tmp1[tree->syms[i]] = 1;
251 for (i = 0; i < 16 && len < 16 - 1; i++)
253 tree->syms[++len] = i;
255 len = get_bits(gb, 2);
256 for (i = 0; i < 16; i++)
258 for (i = 0; i <= len; i++) {
260 for (t = 0; t < 16; t += size << 1)
261 merge(gb, out + t, in + t, size);
262 FFSWAP(uint8_t*, in, out);
264 memcpy(tree->syms, in, 16);
269 * Prepare bundle for decoding data.
271 * @param gb context for reading bits
272 * @param c decoder context
273 * @param bundle_num number of the bundle to initialize
275 static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
279 if (bundle_num == BINK_SRC_COLORS) {
280 for (i = 0; i < 16; i++)
281 read_tree(gb, &c->col_high[i]);
284 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
285 read_tree(gb, &c->bundle[bundle_num].tree);
286 c->bundle[bundle_num].cur_dec =
287 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
291 * common check before starting decoding bundle data
293 * @param gb context for reading bits
295 * @param t variable where number of elements to decode will be stored
297 #define CHECK_READ_VAL(gb, b, t) \
298 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
300 t = get_bits(gb, b->len); \
306 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
309 const uint8_t *dec_end;
311 CHECK_READ_VAL(gb, b, t);
312 dec_end = b->cur_dec + t;
313 if (dec_end > b->data_end) {
314 av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
319 memset(b->cur_dec, v, t);
322 while (b->cur_dec < dec_end)
323 *b->cur_dec++ = GET_HUFF(gb, b->tree);
328 static int read_motion_values(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, "Too many motion values\n");
342 sign = -get_bits1(gb);
343 v = (v ^ sign) - sign;
345 memset(b->cur_dec, v, t);
348 while (b->cur_dec < dec_end) {
349 v = GET_HUFF(gb, b->tree);
351 sign = -get_bits1(gb);
352 v = (v ^ sign) - sign;
360 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
362 static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
366 const uint8_t *dec_end;
368 CHECK_READ_VAL(gb, b, t);
369 dec_end = b->cur_dec + t;
370 if (dec_end > b->data_end) {
371 av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
376 memset(b->cur_dec, v, t);
379 while (b->cur_dec < dec_end) {
380 v = GET_HUFF(gb, b->tree);
385 int run = bink_rlelens[v - 12];
387 if (dec_end - b->cur_dec < run)
389 memset(b->cur_dec, last, run);
397 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
400 const uint8_t *dec_end;
402 CHECK_READ_VAL(gb, b, t);
403 dec_end = b->cur_dec + t;
404 if (dec_end > b->data_end) {
405 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
408 while (b->cur_dec < dec_end) {
409 v = GET_HUFF(gb, b->tree);
410 v |= GET_HUFF(gb, b->tree) << 4;
417 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
420 const uint8_t *dec_end;
422 CHECK_READ_VAL(gb, b, t);
423 dec_end = b->cur_dec + t;
424 if (dec_end > b->data_end) {
425 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
429 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
430 v = GET_HUFF(gb, b->tree);
431 v = (c->col_lastval << 4) | v;
432 if (c->version < 'i') {
433 sign = ((int8_t) v) >> 7;
434 v = ((v & 0x7F) ^ sign) - sign;
437 memset(b->cur_dec, v, t);
440 while (b->cur_dec < dec_end) {
441 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
442 v = GET_HUFF(gb, b->tree);
443 v = (c->col_lastval << 4) | v;
444 if (c->version < 'i') {
445 sign = ((int8_t) v) >> 7;
446 v = ((v & 0x7F) ^ sign) - sign;
455 /** number of bits used to store first DC value in bundle */
456 #define DC_START_BITS 11
458 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
459 int start_bits, int has_sign)
461 int i, j, len, len2, bsize, sign, v, v2;
462 int16_t *dst = (int16_t*)b->cur_dec;
463 int16_t *dst_end = (int16_t*)b->data_end;
465 CHECK_READ_VAL(gb, b, len);
466 v = get_bits(gb, start_bits - has_sign);
468 sign = -get_bits1(gb);
469 v = (v ^ sign) - sign;
471 if (dst_end - dst < 1)
475 for (i = 0; i < len; i += 8) {
476 len2 = FFMIN(len - i, 8);
477 if (dst_end - dst < len2)
479 bsize = get_bits(gb, 4);
481 for (j = 0; j < len2; j++) {
482 v2 = get_bits(gb, bsize);
484 sign = -get_bits1(gb);
485 v2 = (v2 ^ sign) - sign;
489 if (v < -32768 || v > 32767) {
490 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
495 for (j = 0; j < len2; j++)
500 b->cur_dec = (uint8_t*)dst;
505 * Retrieve next value from bundle.
507 * @param c decoder context
508 * @param bundle bundle number
510 static inline int get_value(BinkContext *c, int bundle)
514 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
515 return *c->bundle[bundle].cur_ptr++;
516 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
517 return (int8_t)*c->bundle[bundle].cur_ptr++;
518 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
519 c->bundle[bundle].cur_ptr += 2;
523 static void binkb_init_bundle(BinkContext *c, int bundle_num)
525 c->bundle[bundle_num].cur_dec =
526 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
527 c->bundle[bundle_num].len = 13;
530 static void binkb_init_bundles(BinkContext *c)
533 for (i = 0; i < BINKB_NB_SRC; i++)
534 binkb_init_bundle(c, i);
537 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
539 const int bits = binkb_bundle_sizes[bundle_num];
540 const int mask = 1 << (bits - 1);
541 const int issigned = binkb_bundle_signed[bundle_num];
542 Bundle *b = &c->bundle[bundle_num];
545 CHECK_READ_VAL(gb, b, len);
546 if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
550 for (i = 0; i < len; i++)
551 *b->cur_dec++ = get_bits(gb, bits);
553 for (i = 0; i < len; i++)
554 *b->cur_dec++ = get_bits(gb, bits) - mask;
557 int16_t *dst = (int16_t*)b->cur_dec;
560 for (i = 0; i < len; i++)
561 *dst++ = get_bits(gb, bits);
563 for (i = 0; i < len; i++)
564 *dst++ = get_bits(gb, bits) - mask;
566 b->cur_dec = (uint8_t*)dst;
571 static inline int binkb_get_value(BinkContext *c, int bundle_num)
574 const int bits = binkb_bundle_sizes[bundle_num];
577 int val = *c->bundle[bundle_num].cur_ptr++;
578 return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
580 ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
581 c->bundle[bundle_num].cur_ptr += 2;
586 * Read 8x8 block of DCT coefficients.
588 * @param gb context for reading bits
589 * @param block place for storing coefficients
590 * @param scan scan order table
591 * @param quant_matrices quantization matrices
592 * @return 0 for success, negative value in other cases
594 static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
595 const int32_t quant_matrices[16][64], int q)
599 int i, t, bits, ccoef, mode, sign;
600 int list_start = 64, list_end = 64, list_pos;
604 const int32_t *quant;
606 coef_list[list_end] = 4; mode_list[list_end++] = 0;
607 coef_list[list_end] = 24; mode_list[list_end++] = 0;
608 coef_list[list_end] = 44; mode_list[list_end++] = 0;
609 coef_list[list_end] = 1; mode_list[list_end++] = 3;
610 coef_list[list_end] = 2; mode_list[list_end++] = 3;
611 coef_list[list_end] = 3; mode_list[list_end++] = 3;
613 for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
614 list_pos = list_start;
615 while (list_pos < list_end) {
616 if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
620 ccoef = coef_list[list_pos];
621 mode = mode_list[list_pos];
624 coef_list[list_pos] = ccoef + 4;
625 mode_list[list_pos] = 1;
628 coef_list[list_pos] = 0;
629 mode_list[list_pos++] = 0;
631 for (i = 0; i < 4; i++, ccoef++) {
633 coef_list[--list_start] = ccoef;
634 mode_list[ list_start] = 3;
637 t = 1 - (get_bits1(gb) << 1);
639 t = get_bits(gb, bits) | 1 << bits;
640 sign = -get_bits1(gb);
641 t = (t ^ sign) - sign;
643 block[scan[ccoef]] = t;
644 coef_idx[coef_count++] = ccoef;
649 mode_list[list_pos] = 2;
650 for (i = 0; i < 3; i++) {
652 coef_list[list_end] = ccoef;
653 mode_list[list_end++] = 2;
658 t = 1 - (get_bits1(gb) << 1);
660 t = get_bits(gb, bits) | 1 << bits;
661 sign = -get_bits1(gb);
662 t = (t ^ sign) - sign;
664 block[scan[ccoef]] = t;
665 coef_idx[coef_count++] = ccoef;
666 coef_list[list_pos] = 0;
667 mode_list[list_pos++] = 0;
674 quant_idx = get_bits(gb, 4);
679 quant = quant_matrices[quant_idx];
681 block[0] = (block[0] * quant[0]) >> 11;
682 for (i = 0; i < coef_count; i++) {
683 int idx = coef_idx[i];
684 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
691 * Read 8x8 block with residue after motion compensation.
693 * @param gb context for reading bits
694 * @param block place to store read data
695 * @param masks_count number of masks to decode
696 * @return 0 on success, negative value in other cases
698 static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
702 int i, sign, mask, ccoef, mode;
703 int list_start = 64, list_end = 64, list_pos;
705 int nz_coeff_count = 0;
707 coef_list[list_end] = 4; mode_list[list_end++] = 0;
708 coef_list[list_end] = 24; mode_list[list_end++] = 0;
709 coef_list[list_end] = 44; mode_list[list_end++] = 0;
710 coef_list[list_end] = 0; mode_list[list_end++] = 2;
712 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
713 for (i = 0; i < nz_coeff_count; i++) {
716 if (block[nz_coeff[i]] < 0)
717 block[nz_coeff[i]] -= mask;
719 block[nz_coeff[i]] += mask;
724 list_pos = list_start;
725 while (list_pos < list_end) {
726 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
730 ccoef = coef_list[list_pos];
731 mode = mode_list[list_pos];
734 coef_list[list_pos] = ccoef + 4;
735 mode_list[list_pos] = 1;
738 coef_list[list_pos] = 0;
739 mode_list[list_pos++] = 0;
741 for (i = 0; i < 4; i++, ccoef++) {
743 coef_list[--list_start] = ccoef;
744 mode_list[ list_start] = 3;
746 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
747 sign = -get_bits1(gb);
748 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
756 mode_list[list_pos] = 2;
757 for (i = 0; i < 3; i++) {
759 coef_list[list_end] = ccoef;
760 mode_list[list_end++] = 2;
764 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
765 sign = -get_bits1(gb);
766 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
767 coef_list[list_pos] = 0;
768 mode_list[list_pos++] = 0;
781 * Copy 8x8 block from source to destination, where src and dst may be overlapped
783 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
787 for (i = 0; i < 8; i++)
788 memcpy(tmp + i*8, src + i*stride, 8);
789 for (i = 0; i < 8; i++)
790 memcpy(dst + i*stride, tmp + i*8, 8);
793 static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
794 int is_key, int is_chroma)
798 uint8_t *dst, *ref, *ref_start, *ref_end;
802 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
803 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
805 int ybias = is_key ? -15 : 0;
808 const int stride = c->pic.linesize[plane_idx];
809 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
810 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
812 binkb_init_bundles(c);
813 ref_start = c->pic.data[plane_idx];
814 ref_end = c->pic.data[plane_idx] + (bh * c->pic.linesize[plane_idx] + bw) * 8;
816 for (i = 0; i < 64; i++)
817 coordmap[i] = (i & 7) + (i >> 3) * stride;
819 for (by = 0; by < bh; by++) {
820 for (i = 0; i < BINKB_NB_SRC; i++) {
821 if (binkb_read_bundle(c, gb, i) < 0)
825 dst = c->pic.data[plane_idx] + 8*by*stride;
826 for (bx = 0; bx < bw; bx++, dst += 8) {
827 blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
832 scan = bink_patterns[get_bits(gb, 4)];
837 mode = get_bits1(gb);
838 run = get_bits(gb, binkb_runbits[i]) + 1;
842 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
846 v = binkb_get_value(c, BINKB_SRC_COLORS);
847 for (j = 0; j < run; j++)
848 dst[coordmap[*scan++]] = v;
850 for (j = 0; j < run; j++)
851 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
855 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
858 memset(dctblock, 0, sizeof(*dctblock) * 64);
859 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
860 qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
861 read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_intra_quant, qp);
862 c->bdsp.idct_put(dst, stride, dctblock);
865 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
866 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
867 ref = dst + xoff + yoff * stride;
868 if (ref < ref_start || ref + 8*stride > ref_end) {
869 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
870 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
871 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
873 put_pixels8x8_overlapped(dst, ref, stride);
875 c->dsp.clear_block(block);
876 v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
877 read_residue(gb, block, v);
878 c->dsp.add_pixels8(dst, block, stride);
881 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
882 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
883 ref = dst + xoff + yoff * stride;
884 if (ref < ref_start || ref + 8 * stride > ref_end) {
885 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
886 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
887 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
889 put_pixels8x8_overlapped(dst, ref, stride);
891 memset(dctblock, 0, sizeof(*dctblock) * 64);
892 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
893 qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
894 read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_inter_quant, qp);
895 c->bdsp.idct_add(dst, stride, dctblock);
898 v = binkb_get_value(c, BINKB_SRC_COLORS);
899 c->dsp.fill_block_tab[1](dst, v, stride, 8);
902 for (i = 0; i < 2; i++)
903 col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
904 for (i = 0; i < 8; i++) {
905 v = binkb_get_value(c, BINKB_SRC_PATTERN);
906 for (j = 0; j < 8; j++, v >>= 1)
907 dst[i*stride + j] = col[v & 1];
911 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
912 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
913 ref = dst + xoff + yoff * stride;
914 if (ref < ref_start || ref + 8 * stride > ref_end) {
915 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
916 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
917 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
919 put_pixels8x8_overlapped(dst, ref, stride);
923 for (i = 0; i < 8; i++)
924 memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
925 c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
928 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
933 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
934 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
939 static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
944 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
948 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
949 LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
950 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
953 const int stride = c->pic.linesize[plane_idx];
954 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
955 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
956 int width = c->avctx->width >> is_chroma;
958 init_lengths(c, FFMAX(width, 8), bw);
959 for (i = 0; i < BINK_NB_SRC; i++)
960 read_bundle(gb, c, i);
962 ref_start = c->last.data[plane_idx] ? c->last.data[plane_idx]
963 : c->pic.data[plane_idx];
965 + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
967 for (i = 0; i < 64; i++)
968 coordmap[i] = (i & 7) + (i >> 3) * stride;
970 for (by = 0; by < bh; by++) {
971 if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
973 if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
975 if (read_colors(gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
977 if (read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
979 if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
981 if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
983 if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
985 if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
987 if (read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN]) < 0)
992 dst = c->pic.data[plane_idx] + 8*by*stride;
993 prev = (c->last.data[plane_idx] ? c->last.data[plane_idx]
994 : c->pic.data[plane_idx]) + 8*by*stride;
995 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
996 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
997 // 16x16 block type on odd line means part of the already decoded block, so skip it
998 if ((by & 1) && blk == SCALED_BLOCK) {
1006 c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1009 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
1012 scan = bink_patterns[get_bits(gb, 4)];
1015 int run = get_value(c, BINK_SRC_RUN) + 1;
1019 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1022 if (get_bits1(gb)) {
1023 v = get_value(c, BINK_SRC_COLORS);
1024 for (j = 0; j < run; j++)
1025 ublock[*scan++] = v;
1027 for (j = 0; j < run; j++)
1028 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1032 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1035 memset(dctblock, 0, sizeof(*dctblock) * 64);
1036 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1037 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1038 c->bdsp.idct_put(ublock, 8, dctblock);
1041 v = get_value(c, BINK_SRC_COLORS);
1042 c->dsp.fill_block_tab[0](dst, v, stride, 16);
1045 for (i = 0; i < 2; i++)
1046 col[i] = get_value(c, BINK_SRC_COLORS);
1047 for (j = 0; j < 8; j++) {
1048 v = get_value(c, BINK_SRC_PATTERN);
1049 for (i = 0; i < 8; i++, v >>= 1)
1050 ublock[i + j*8] = col[v & 1];
1054 for (j = 0; j < 8; j++)
1055 for (i = 0; i < 8; i++)
1056 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1059 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1062 if (blk != FILL_BLOCK)
1063 c->bdsp.scale_block(ublock, dst, stride);
1069 xoff = get_value(c, BINK_SRC_X_OFF);
1070 yoff = get_value(c, BINK_SRC_Y_OFF);
1071 ref = prev + xoff + yoff * stride;
1072 if (ref < ref_start || ref > ref_end) {
1073 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1074 bx*8 + xoff, by*8 + yoff);
1077 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1080 scan = bink_patterns[get_bits(gb, 4)];
1083 int run = get_value(c, BINK_SRC_RUN) + 1;
1087 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1090 if (get_bits1(gb)) {
1091 v = get_value(c, BINK_SRC_COLORS);
1092 for (j = 0; j < run; j++)
1093 dst[coordmap[*scan++]] = v;
1095 for (j = 0; j < run; j++)
1096 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1100 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1103 xoff = get_value(c, BINK_SRC_X_OFF);
1104 yoff = get_value(c, BINK_SRC_Y_OFF);
1105 ref = prev + xoff + yoff * stride;
1106 if (ref < ref_start || ref > ref_end) {
1107 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1108 bx*8 + xoff, by*8 + yoff);
1111 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1112 c->dsp.clear_block(block);
1113 v = get_bits(gb, 7);
1114 read_residue(gb, block, v);
1115 c->dsp.add_pixels8(dst, block, stride);
1118 memset(dctblock, 0, sizeof(*dctblock) * 64);
1119 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1120 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1121 c->bdsp.idct_put(dst, stride, dctblock);
1124 v = get_value(c, BINK_SRC_COLORS);
1125 c->dsp.fill_block_tab[1](dst, v, stride, 8);
1128 xoff = get_value(c, BINK_SRC_X_OFF);
1129 yoff = get_value(c, BINK_SRC_Y_OFF);
1130 ref = prev + xoff + yoff * stride;
1131 if (ref < ref_start || ref > ref_end) {
1132 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1133 bx*8 + xoff, by*8 + yoff);
1136 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1137 memset(dctblock, 0, sizeof(*dctblock) * 64);
1138 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1139 read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
1140 c->bdsp.idct_add(dst, stride, dctblock);
1143 for (i = 0; i < 2; i++)
1144 col[i] = get_value(c, BINK_SRC_COLORS);
1145 for (i = 0; i < 8; i++) {
1146 v = get_value(c, BINK_SRC_PATTERN);
1147 for (j = 0; j < 8; j++, v >>= 1)
1148 dst[i*stride + j] = col[v & 1];
1152 for (i = 0; i < 8; i++)
1153 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1154 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1157 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1162 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1163 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1168 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
1170 BinkContext * const c = avctx->priv_data;
1172 int plane, plane_idx;
1173 int bits_count = pkt->size << 3;
1175 if (c->version > 'b') {
1177 avctx->release_buffer(avctx, &c->pic);
1179 if(avctx->get_buffer(avctx, &c->pic) < 0){
1180 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1184 if(avctx->reget_buffer(avctx, &c->pic) < 0){
1185 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1190 init_get_bits(&gb, pkt->data, bits_count);
1192 if (c->version >= 'i')
1193 skip_bits_long(&gb, 32);
1194 if (bink_decode_plane(c, &gb, 3, 0) < 0)
1197 if (c->version >= 'i')
1198 skip_bits_long(&gb, 32);
1200 for (plane = 0; plane < 3; plane++) {
1201 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1203 if (c->version > 'b') {
1204 if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
1207 if (binkb_decode_plane(c, &gb, plane_idx, !pkt->pts, !!plane) < 0)
1210 if (get_bits_count(&gb) >= bits_count)
1215 *data_size = sizeof(AVFrame);
1216 *(AVFrame*)data = c->pic;
1218 if (c->version > 'b')
1219 FFSWAP(AVFrame, c->pic, c->last);
1221 /* always report that the buffer was completely consumed */
1226 * Caclulate quantization tables for version b
1228 static av_cold void binkb_calc_quant(void)
1230 uint8_t inv_bink_scan[64];
1231 static const int s[64]={
1232 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1233 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1234 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1235 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1236 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1237 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1238 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1239 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1243 for (i = 0; i < 64; i++)
1244 inv_bink_scan[bink_scan[i]] = i;
1246 for (j = 0; j < 16; j++) {
1247 for (i = 0; i < 64; i++) {
1248 int k = inv_bink_scan[i];
1249 binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
1250 binkb_num[j]/(binkb_den[j] * (C>>12));
1251 binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
1252 binkb_num[j]/(binkb_den[j] * (C>>12));
1257 static av_cold int decode_init(AVCodecContext *avctx)
1259 BinkContext * const c = avctx->priv_data;
1260 static VLC_TYPE table[16 * 128][2];
1261 static int binkb_initialised = 0;
1265 c->version = avctx->codec_tag >> 24;
1266 if (avctx->extradata_size < 4) {
1267 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1270 flags = AV_RL32(avctx->extradata);
1271 c->has_alpha = flags & BINK_FLAG_ALPHA;
1272 c->swap_planes = c->version >= 'h';
1273 if (!bink_trees[15].table) {
1274 for (i = 0; i < 16; i++) {
1275 const int maxbits = bink_tree_lens[i][15];
1276 bink_trees[i].table = table + i*128;
1277 bink_trees[i].table_allocated = 1 << maxbits;
1278 init_vlc(&bink_trees[i], maxbits, 16,
1279 bink_tree_lens[i], 1, 1,
1280 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1285 c->pic.data[0] = NULL;
1287 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
1291 avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1293 avctx->idct_algo = FF_IDCT_BINK;
1294 ff_dsputil_init(&c->dsp, avctx);
1295 ff_binkdsp_init(&c->bdsp);
1299 if (c->version == 'b') {
1300 if (!binkb_initialised) {
1302 binkb_initialised = 1;
1309 static av_cold int decode_end(AVCodecContext *avctx)
1311 BinkContext * const c = avctx->priv_data;
1314 avctx->release_buffer(avctx, &c->pic);
1315 if (c->last.data[0])
1316 avctx->release_buffer(avctx, &c->last);
1322 AVCodec ff_bink_decoder = {
1323 .name = "binkvideo",
1324 .type = AVMEDIA_TYPE_VIDEO,
1325 .id = AV_CODEC_ID_BINKVIDEO,
1326 .priv_data_size = sizeof(BinkContext),
1327 .init = decode_init,
1328 .close = decode_end,
1329 .decode = decode_frame,
1330 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),