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, mask, 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 bits = get_bits(gb, 4) - 1;
613 for (mask = 1 << bits; bits >= 0; mask >>= 1, 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) | mask;
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) | mask;
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, 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, 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 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1132 memset(dctblock, 0, sizeof(*dctblock) * 64);
1133 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1134 read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
1135 c->bdsp.idct_add(dst, stride, dctblock);
1138 for (i = 0; i < 2; i++)
1139 col[i] = get_value(c, BINK_SRC_COLORS);
1140 for (i = 0; i < 8; i++) {
1141 v = get_value(c, BINK_SRC_PATTERN);
1142 for (j = 0; j < 8; j++, v >>= 1)
1143 dst[i*stride + j] = col[v & 1];
1147 for (i = 0; i < 8; i++)
1148 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1149 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1152 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1157 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1158 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1163 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
1165 BinkContext * const c = avctx->priv_data;
1167 int plane, plane_idx;
1168 int bits_count = pkt->size << 3;
1170 if (c->version > 'b') {
1172 avctx->release_buffer(avctx, &c->pic);
1174 if(avctx->get_buffer(avctx, &c->pic) < 0){
1175 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1179 if(avctx->reget_buffer(avctx, &c->pic) < 0){
1180 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1185 init_get_bits(&gb, pkt->data, bits_count);
1187 if (c->version >= 'i')
1188 skip_bits_long(&gb, 32);
1189 if (bink_decode_plane(c, &gb, 3, 0) < 0)
1192 if (c->version >= 'i')
1193 skip_bits_long(&gb, 32);
1195 for (plane = 0; plane < 3; plane++) {
1196 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1198 if (c->version > 'b') {
1199 if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
1202 if (binkb_decode_plane(c, &gb, plane_idx, !pkt->pts, !!plane) < 0)
1205 if (get_bits_count(&gb) >= bits_count)
1210 *data_size = sizeof(AVFrame);
1211 *(AVFrame*)data = c->pic;
1213 if (c->version > 'b')
1214 FFSWAP(AVFrame, c->pic, c->last);
1216 /* always report that the buffer was completely consumed */
1221 * Caclulate quantization tables for version b
1223 static av_cold void binkb_calc_quant(void)
1225 uint8_t inv_bink_scan[64];
1229 for (j = 0; j < 8; j++) {
1230 for (i = 0; i < 8; i++) {
1233 s[j*8 + i] = cos(j * M_PI/16.0) * cos(i * M_PI/16.0) * 2.0;
1235 s[j*8 + i] = cos(j * M_PI/16.0) * sqrt(2.0);
1238 s[j*8 + i] = cos(i * M_PI/16.0) * sqrt(2.0);
1244 for (i = 0; i < 64; i++)
1245 inv_bink_scan[bink_scan[i]] = i;
1247 for (j = 0; j < 16; j++) {
1248 for (i = 0; i < 64; i++) {
1249 int k = inv_bink_scan[i];
1251 binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] *
1252 binkb_num[j]/binkb_den[j];
1253 binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] *
1254 binkb_num[j]/binkb_den[j];
1256 binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] * s[i] *
1257 binkb_num[j]/(double)binkb_den[j];
1258 binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] * s[i] *
1259 binkb_num[j]/(double)binkb_den[j];
1265 static av_cold int decode_init(AVCodecContext *avctx)
1267 BinkContext * const c = avctx->priv_data;
1268 static VLC_TYPE table[16 * 128][2];
1269 static int binkb_initialised = 0;
1273 c->version = avctx->codec_tag >> 24;
1274 if (avctx->extradata_size < 4) {
1275 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1278 flags = AV_RL32(avctx->extradata);
1279 c->has_alpha = flags & BINK_FLAG_ALPHA;
1280 c->swap_planes = c->version >= 'h';
1281 if (!bink_trees[15].table) {
1282 for (i = 0; i < 16; i++) {
1283 const int maxbits = bink_tree_lens[i][15];
1284 bink_trees[i].table = table + i*128;
1285 bink_trees[i].table_allocated = 1 << maxbits;
1286 init_vlc(&bink_trees[i], maxbits, 16,
1287 bink_tree_lens[i], 1, 1,
1288 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1293 c->pic.data[0] = NULL;
1295 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
1299 avctx->pix_fmt = c->has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
1301 avctx->idct_algo = FF_IDCT_BINK;
1302 dsputil_init(&c->dsp, avctx);
1303 ff_binkdsp_init(&c->bdsp);
1307 if (c->version == 'b') {
1308 if (!binkb_initialised) {
1310 binkb_initialised = 1;
1317 static av_cold int decode_end(AVCodecContext *avctx)
1319 BinkContext * const c = avctx->priv_data;
1322 avctx->release_buffer(avctx, &c->pic);
1323 if (c->last.data[0])
1324 avctx->release_buffer(avctx, &c->last);
1330 AVCodec ff_bink_decoder = {
1331 .name = "binkvideo",
1332 .type = AVMEDIA_TYPE_VIDEO,
1333 .id = CODEC_ID_BINKVIDEO,
1334 .priv_data_size = sizeof(BinkContext),
1335 .init = decode_init,
1336 .close = decode_end,
1337 .decode = decode_frame,
1338 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),