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 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 && len < 16 - 1; 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);
347 while (b->cur_dec < dec_end) {
348 v = GET_HUFF(gb, b->tree);
350 sign = -get_bits1(gb);
351 v = (v ^ sign) - sign;
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);
378 while (b->cur_dec < dec_end) {
379 v = GET_HUFF(gb, b->tree);
384 int run = bink_rlelens[v - 12];
386 if (dec_end - b->cur_dec < run)
388 memset(b->cur_dec, last, run);
396 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
399 const uint8_t *dec_end;
401 CHECK_READ_VAL(gb, b, t);
402 dec_end = b->cur_dec + t;
403 if (dec_end > b->data_end) {
404 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
407 while (b->cur_dec < dec_end) {
408 v = GET_HUFF(gb, b->tree);
409 v |= GET_HUFF(gb, b->tree) << 4;
416 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
419 const uint8_t *dec_end;
421 CHECK_READ_VAL(gb, b, t);
422 dec_end = b->cur_dec + t;
423 if (dec_end > b->data_end) {
424 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
428 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
429 v = GET_HUFF(gb, b->tree);
430 v = (c->col_lastval << 4) | v;
431 if (c->version < 'i') {
432 sign = ((int8_t) v) >> 7;
433 v = ((v & 0x7F) ^ sign) - sign;
436 memset(b->cur_dec, v, t);
439 while (b->cur_dec < dec_end) {
440 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
441 v = GET_HUFF(gb, b->tree);
442 v = (c->col_lastval << 4) | v;
443 if (c->version < 'i') {
444 sign = ((int8_t) v) >> 7;
445 v = ((v & 0x7F) ^ sign) - sign;
454 /** number of bits used to store first DC value in bundle */
455 #define DC_START_BITS 11
457 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
458 int start_bits, int has_sign)
460 int i, j, len, len2, bsize, sign, v, v2;
461 int16_t *dst = (int16_t*)b->cur_dec;
462 int16_t *dst_end = (int16_t*)b->data_end;
464 CHECK_READ_VAL(gb, b, len);
465 v = get_bits(gb, start_bits - has_sign);
467 sign = -get_bits1(gb);
468 v = (v ^ sign) - sign;
470 if (dst_end - dst < 1)
474 for (i = 0; i < len; i += 8) {
475 len2 = FFMIN(len - i, 8);
476 if (dst_end - dst < len2)
478 bsize = get_bits(gb, 4);
480 for (j = 0; j < len2; j++) {
481 v2 = get_bits(gb, bsize);
483 sign = -get_bits1(gb);
484 v2 = (v2 ^ sign) - sign;
488 if (v < -32768 || v > 32767) {
489 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
494 for (j = 0; j < len2; j++)
499 b->cur_dec = (uint8_t*)dst;
504 * Retrieve next value from bundle.
506 * @param c decoder context
507 * @param bundle bundle number
509 static inline int get_value(BinkContext *c, int bundle)
513 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
514 return *c->bundle[bundle].cur_ptr++;
515 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
516 return (int8_t)*c->bundle[bundle].cur_ptr++;
517 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
518 c->bundle[bundle].cur_ptr += 2;
522 static void binkb_init_bundle(BinkContext *c, int bundle_num)
524 c->bundle[bundle_num].cur_dec =
525 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
526 c->bundle[bundle_num].len = 13;
529 static void binkb_init_bundles(BinkContext *c)
532 for (i = 0; i < BINKB_NB_SRC; i++)
533 binkb_init_bundle(c, i);
536 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
538 const int bits = binkb_bundle_sizes[bundle_num];
539 const int mask = 1 << (bits - 1);
540 const int issigned = binkb_bundle_signed[bundle_num];
541 Bundle *b = &c->bundle[bundle_num];
544 CHECK_READ_VAL(gb, b, len);
545 if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
549 for (i = 0; i < len; i++)
550 *b->cur_dec++ = get_bits(gb, bits);
552 for (i = 0; i < len; i++)
553 *b->cur_dec++ = get_bits(gb, bits) - mask;
556 int16_t *dst = (int16_t*)b->cur_dec;
559 for (i = 0; i < len; i++)
560 *dst++ = get_bits(gb, bits);
562 for (i = 0; i < len; i++)
563 *dst++ = get_bits(gb, bits) - mask;
565 b->cur_dec = (uint8_t*)dst;
570 static inline int binkb_get_value(BinkContext *c, int bundle_num)
573 const int bits = binkb_bundle_sizes[bundle_num];
576 int val = *c->bundle[bundle_num].cur_ptr++;
577 return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
579 ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
580 c->bundle[bundle_num].cur_ptr += 2;
585 * Read 8x8 block of DCT coefficients.
587 * @param gb context for reading bits
588 * @param block place for storing coefficients
589 * @param scan scan order table
590 * @param quant_matrices quantization matrices
591 * @return 0 for success, negative value in other cases
593 static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
594 const int32_t quant_matrices[16][64], int q)
598 int i, t, bits, ccoef, mode, sign;
599 int list_start = 64, list_end = 64, list_pos;
603 const int32_t *quant;
605 coef_list[list_end] = 4; mode_list[list_end++] = 0;
606 coef_list[list_end] = 24; mode_list[list_end++] = 0;
607 coef_list[list_end] = 44; mode_list[list_end++] = 0;
608 coef_list[list_end] = 1; mode_list[list_end++] = 3;
609 coef_list[list_end] = 2; mode_list[list_end++] = 3;
610 coef_list[list_end] = 3; mode_list[list_end++] = 3;
612 for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
613 list_pos = list_start;
614 while (list_pos < list_end) {
615 if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
619 ccoef = coef_list[list_pos];
620 mode = mode_list[list_pos];
623 coef_list[list_pos] = ccoef + 4;
624 mode_list[list_pos] = 1;
627 coef_list[list_pos] = 0;
628 mode_list[list_pos++] = 0;
630 for (i = 0; i < 4; i++, ccoef++) {
632 coef_list[--list_start] = ccoef;
633 mode_list[ list_start] = 3;
636 t = 1 - (get_bits1(gb) << 1);
638 t = get_bits(gb, bits) | 1 << bits;
639 sign = -get_bits1(gb);
640 t = (t ^ sign) - sign;
642 block[scan[ccoef]] = t;
643 coef_idx[coef_count++] = ccoef;
648 mode_list[list_pos] = 2;
649 for (i = 0; i < 3; i++) {
651 coef_list[list_end] = ccoef;
652 mode_list[list_end++] = 2;
657 t = 1 - (get_bits1(gb) << 1);
659 t = get_bits(gb, bits) | 1 << bits;
660 sign = -get_bits1(gb);
661 t = (t ^ sign) - sign;
663 block[scan[ccoef]] = t;
664 coef_idx[coef_count++] = ccoef;
665 coef_list[list_pos] = 0;
666 mode_list[list_pos++] = 0;
673 quant_idx = get_bits(gb, 4);
678 quant = quant_matrices[quant_idx];
680 block[0] = (block[0] * quant[0]) >> 11;
681 for (i = 0; i < coef_count; i++) {
682 int idx = coef_idx[i];
683 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
690 * Read 8x8 block with residue after motion compensation.
692 * @param gb context for reading bits
693 * @param block place to store read data
694 * @param masks_count number of masks to decode
695 * @return 0 on success, negative value in other cases
697 static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
701 int i, sign, mask, ccoef, mode;
702 int list_start = 64, list_end = 64, list_pos;
704 int nz_coeff_count = 0;
706 coef_list[list_end] = 4; mode_list[list_end++] = 0;
707 coef_list[list_end] = 24; mode_list[list_end++] = 0;
708 coef_list[list_end] = 44; mode_list[list_end++] = 0;
709 coef_list[list_end] = 0; mode_list[list_end++] = 2;
711 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
712 for (i = 0; i < nz_coeff_count; i++) {
715 if (block[nz_coeff[i]] < 0)
716 block[nz_coeff[i]] -= mask;
718 block[nz_coeff[i]] += mask;
723 list_pos = list_start;
724 while (list_pos < list_end) {
725 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
729 ccoef = coef_list[list_pos];
730 mode = mode_list[list_pos];
733 coef_list[list_pos] = ccoef + 4;
734 mode_list[list_pos] = 1;
737 coef_list[list_pos] = 0;
738 mode_list[list_pos++] = 0;
740 for (i = 0; i < 4; i++, ccoef++) {
742 coef_list[--list_start] = ccoef;
743 mode_list[ list_start] = 3;
745 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
746 sign = -get_bits1(gb);
747 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
755 mode_list[list_pos] = 2;
756 for (i = 0; i < 3; i++) {
758 coef_list[list_end] = ccoef;
759 mode_list[list_end++] = 2;
763 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
764 sign = -get_bits1(gb);
765 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
766 coef_list[list_pos] = 0;
767 mode_list[list_pos++] = 0;
780 * Copy 8x8 block from source to destination, where src and dst may be overlapped
782 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
786 for (i = 0; i < 8; i++)
787 memcpy(tmp + i*8, src + i*stride, 8);
788 for (i = 0; i < 8; i++)
789 memcpy(dst + i*stride, tmp + i*8, 8);
792 static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
793 int is_key, int is_chroma)
797 uint8_t *dst, *ref, *ref_start, *ref_end;
801 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
802 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
804 int ybias = is_key ? -15 : 0;
807 const int stride = c->pic.linesize[plane_idx];
808 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
809 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
811 binkb_init_bundles(c);
812 ref_start = c->pic.data[plane_idx];
813 ref_end = c->pic.data[plane_idx] + (bh * c->pic.linesize[plane_idx] + bw) * 8;
815 for (i = 0; i < 64; i++)
816 coordmap[i] = (i & 7) + (i >> 3) * stride;
818 for (by = 0; by < bh; by++) {
819 for (i = 0; i < BINKB_NB_SRC; i++) {
820 if (binkb_read_bundle(c, gb, i) < 0)
824 dst = c->pic.data[plane_idx] + 8*by*stride;
825 for (bx = 0; bx < bw; bx++, dst += 8) {
826 blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
831 scan = bink_patterns[get_bits(gb, 4)];
836 mode = get_bits1(gb);
837 run = get_bits(gb, binkb_runbits[i]) + 1;
841 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
845 v = binkb_get_value(c, BINKB_SRC_COLORS);
846 for (j = 0; j < run; j++)
847 dst[coordmap[*scan++]] = v;
849 for (j = 0; j < run; j++)
850 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
854 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
857 memset(dctblock, 0, sizeof(*dctblock) * 64);
858 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
859 qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
860 read_dct_coeffs(gb, dctblock, bink_scan, binkb_intra_quant, qp);
861 c->bdsp.idct_put(dst, stride, dctblock);
864 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
865 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
866 ref = dst + xoff + yoff * stride;
867 if (ref < ref_start || ref + 8*stride > ref_end) {
868 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
869 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
870 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
872 put_pixels8x8_overlapped(dst, ref, stride);
874 c->dsp.clear_block(block);
875 v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
876 read_residue(gb, block, v);
877 c->dsp.add_pixels8(dst, block, stride);
880 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
881 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
882 ref = dst + xoff + yoff * stride;
883 if (ref < ref_start || ref + 8 * stride > ref_end) {
884 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
885 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
886 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
888 put_pixels8x8_overlapped(dst, ref, stride);
890 memset(dctblock, 0, sizeof(*dctblock) * 64);
891 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
892 qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
893 read_dct_coeffs(gb, dctblock, bink_scan, binkb_inter_quant, qp);
894 c->bdsp.idct_add(dst, stride, dctblock);
897 v = binkb_get_value(c, BINKB_SRC_COLORS);
898 c->dsp.fill_block_tab[1](dst, v, stride, 8);
901 for (i = 0; i < 2; i++)
902 col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
903 for (i = 0; i < 8; i++) {
904 v = binkb_get_value(c, BINKB_SRC_PATTERN);
905 for (j = 0; j < 8; j++, v >>= 1)
906 dst[i*stride + j] = col[v & 1];
910 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
911 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
912 ref = dst + xoff + yoff * stride;
913 if (ref < ref_start || ref + 8 * stride > ref_end) {
914 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
915 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
916 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
918 put_pixels8x8_overlapped(dst, ref, stride);
922 for (i = 0; i < 8; i++)
923 memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
924 c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
927 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
932 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
933 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
938 static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
943 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
947 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
948 LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
949 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
952 const int stride = c->pic.linesize[plane_idx];
953 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
954 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
955 int width = c->avctx->width >> is_chroma;
957 init_lengths(c, FFMAX(width, 8), bw);
958 for (i = 0; i < BINK_NB_SRC; i++)
959 read_bundle(gb, c, i);
961 ref_start = c->last.data[plane_idx] ? c->last.data[plane_idx]
962 : c->pic.data[plane_idx];
964 + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
966 for (i = 0; i < 64; i++)
967 coordmap[i] = (i & 7) + (i >> 3) * stride;
969 for (by = 0; by < bh; by++) {
970 if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
972 if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
974 if (read_colors(gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
976 if (read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
978 if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
980 if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
982 if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
984 if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
986 if (read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN]) < 0)
991 dst = c->pic.data[plane_idx] + 8*by*stride;
992 prev = (c->last.data[plane_idx] ? c->last.data[plane_idx]
993 : c->pic.data[plane_idx]) + 8*by*stride;
994 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
995 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
996 // 16x16 block type on odd line means part of the already decoded block, so skip it
997 if ((by & 1) && blk == SCALED_BLOCK) {
1005 c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1008 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
1011 scan = bink_patterns[get_bits(gb, 4)];
1014 int run = get_value(c, BINK_SRC_RUN) + 1;
1018 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1021 if (get_bits1(gb)) {
1022 v = get_value(c, BINK_SRC_COLORS);
1023 for (j = 0; j < run; j++)
1024 ublock[*scan++] = v;
1026 for (j = 0; j < run; j++)
1027 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1031 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1034 memset(dctblock, 0, sizeof(*dctblock) * 64);
1035 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1036 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1037 c->bdsp.idct_put(ublock, 8, dctblock);
1040 v = get_value(c, BINK_SRC_COLORS);
1041 c->dsp.fill_block_tab[0](dst, v, stride, 16);
1044 for (i = 0; i < 2; i++)
1045 col[i] = get_value(c, BINK_SRC_COLORS);
1046 for (j = 0; j < 8; j++) {
1047 v = get_value(c, BINK_SRC_PATTERN);
1048 for (i = 0; i < 8; i++, v >>= 1)
1049 ublock[i + j*8] = col[v & 1];
1053 for (j = 0; j < 8; j++)
1054 for (i = 0; i < 8; i++)
1055 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1058 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1061 if (blk != FILL_BLOCK)
1062 c->bdsp.scale_block(ublock, dst, stride);
1068 xoff = get_value(c, BINK_SRC_X_OFF);
1069 yoff = get_value(c, BINK_SRC_Y_OFF);
1070 ref = prev + xoff + yoff * stride;
1071 if (ref < ref_start || ref > ref_end) {
1072 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1073 bx*8 + xoff, by*8 + yoff);
1076 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1079 scan = bink_patterns[get_bits(gb, 4)];
1082 int run = get_value(c, BINK_SRC_RUN) + 1;
1086 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1089 if (get_bits1(gb)) {
1090 v = get_value(c, BINK_SRC_COLORS);
1091 for (j = 0; j < run; j++)
1092 dst[coordmap[*scan++]] = v;
1094 for (j = 0; j < run; j++)
1095 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1099 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1102 xoff = get_value(c, BINK_SRC_X_OFF);
1103 yoff = get_value(c, BINK_SRC_Y_OFF);
1104 ref = prev + xoff + yoff * stride;
1105 if (ref < ref_start || ref > ref_end) {
1106 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1107 bx*8 + xoff, by*8 + yoff);
1110 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1111 c->dsp.clear_block(block);
1112 v = get_bits(gb, 7);
1113 read_residue(gb, block, v);
1114 c->dsp.add_pixels8(dst, block, stride);
1117 memset(dctblock, 0, sizeof(*dctblock) * 64);
1118 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1119 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1120 c->bdsp.idct_put(dst, stride, dctblock);
1123 v = get_value(c, BINK_SRC_COLORS);
1124 c->dsp.fill_block_tab[1](dst, v, stride, 8);
1127 xoff = get_value(c, BINK_SRC_X_OFF);
1128 yoff = get_value(c, BINK_SRC_Y_OFF);
1129 ref = prev + xoff + yoff * stride;
1130 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1131 memset(dctblock, 0, sizeof(*dctblock) * 64);
1132 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1133 read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
1134 c->bdsp.idct_add(dst, stride, dctblock);
1137 for (i = 0; i < 2; i++)
1138 col[i] = get_value(c, BINK_SRC_COLORS);
1139 for (i = 0; i < 8; i++) {
1140 v = get_value(c, BINK_SRC_PATTERN);
1141 for (j = 0; j < 8; j++, v >>= 1)
1142 dst[i*stride + j] = col[v & 1];
1146 for (i = 0; i < 8; i++)
1147 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1148 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1151 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1156 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1157 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1162 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
1164 BinkContext * const c = avctx->priv_data;
1166 int plane, plane_idx;
1167 int bits_count = pkt->size << 3;
1169 if (c->version > 'b') {
1171 avctx->release_buffer(avctx, &c->pic);
1173 if(avctx->get_buffer(avctx, &c->pic) < 0){
1174 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1178 if(avctx->reget_buffer(avctx, &c->pic) < 0){
1179 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1184 init_get_bits(&gb, pkt->data, bits_count);
1186 if (c->version >= 'i')
1187 skip_bits_long(&gb, 32);
1188 if (bink_decode_plane(c, &gb, 3, 0) < 0)
1191 if (c->version >= 'i')
1192 skip_bits_long(&gb, 32);
1194 for (plane = 0; plane < 3; plane++) {
1195 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1197 if (c->version > 'b') {
1198 if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
1201 if (binkb_decode_plane(c, &gb, plane_idx, !pkt->pts, !!plane) < 0)
1204 if (get_bits_count(&gb) >= bits_count)
1209 *data_size = sizeof(AVFrame);
1210 *(AVFrame*)data = c->pic;
1212 if (c->version > 'b')
1213 FFSWAP(AVFrame, c->pic, c->last);
1215 /* always report that the buffer was completely consumed */
1220 * Caclulate quantization tables for version b
1222 static av_cold void binkb_calc_quant(void)
1224 uint8_t inv_bink_scan[64];
1228 for (j = 0; j < 8; j++) {
1229 for (i = 0; i < 8; i++) {
1232 s[j*8 + i] = cos(j * M_PI/16.0) * cos(i * M_PI/16.0) * 2.0;
1234 s[j*8 + i] = cos(j * M_PI/16.0) * sqrt(2.0);
1237 s[j*8 + i] = cos(i * M_PI/16.0) * sqrt(2.0);
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];
1250 binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] *
1251 binkb_num[j]/binkb_den[j];
1252 binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] *
1253 binkb_num[j]/binkb_den[j];
1255 binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] * s[i] *
1256 binkb_num[j]/(double)binkb_den[j];
1257 binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] * s[i] *
1258 binkb_num[j]/(double)binkb_den[j];
1264 static av_cold int decode_init(AVCodecContext *avctx)
1266 BinkContext * const c = avctx->priv_data;
1267 static VLC_TYPE table[16 * 128][2];
1268 static int binkb_initialised = 0;
1272 c->version = avctx->codec_tag >> 24;
1273 if (avctx->extradata_size < 4) {
1274 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1277 flags = AV_RL32(avctx->extradata);
1278 c->has_alpha = flags & BINK_FLAG_ALPHA;
1279 c->swap_planes = c->version >= 'h';
1280 if (!bink_trees[15].table) {
1281 for (i = 0; i < 16; i++) {
1282 const int maxbits = bink_tree_lens[i][15];
1283 bink_trees[i].table = table + i*128;
1284 bink_trees[i].table_allocated = 1 << maxbits;
1285 init_vlc(&bink_trees[i], maxbits, 16,
1286 bink_tree_lens[i], 1, 1,
1287 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1292 c->pic.data[0] = NULL;
1294 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
1298 avctx->pix_fmt = c->has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
1300 avctx->idct_algo = FF_IDCT_BINK;
1301 dsputil_init(&c->dsp, avctx);
1302 ff_binkdsp_init(&c->bdsp);
1306 if (c->version == 'b') {
1307 if (!binkb_initialised) {
1309 binkb_initialised = 1;
1316 static av_cold int decode_end(AVCodecContext *avctx)
1318 BinkContext * const c = avctx->priv_data;
1321 avctx->release_buffer(avctx, &c->pic);
1322 if (c->last.data[0])
1323 avctx->release_buffer(avctx, &c->last);
1329 AVCodec ff_bink_decoder = {
1330 .name = "binkvideo",
1331 .type = AVMEDIA_TYPE_VIDEO,
1332 .id = CODEC_ID_BINKVIDEO,
1333 .priv_data_size = sizeof(BinkContext),
1334 .init = decode_init,
1335 .close = decode_end,
1336 .decode = decode_frame,
1337 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),