3 * Copyright (c) 2009 Konstantin Shishkov
4 * Copyright (C) 2011 Peter Ross <pross@xvid.org>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/attributes.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/internal.h"
34 #define BITSTREAM_READER_LE
37 #define BINK_FLAG_ALPHA 0x00100000
38 #define BINK_FLAG_GRAY 0x00020000
40 static VLC bink_trees[16];
43 * IDs for different data types used in old version of Bink video codec
46 BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
47 BINKB_SRC_COLORS, ///< pixel values used for different block types
48 BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
49 BINKB_SRC_X_OFF, ///< X components of motion value
50 BINKB_SRC_Y_OFF, ///< Y components of motion value
51 BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
52 BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
53 BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
54 BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
55 BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
60 static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
61 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
64 static const int binkb_bundle_signed[BINKB_NB_SRC] = {
65 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
68 static int32_t binkb_intra_quant[16][64];
69 static int32_t binkb_inter_quant[16][64];
72 * IDs for different data types used in Bink video codec
75 BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
76 BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
77 BINK_SRC_COLORS, ///< pixel values used for different block types
78 BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
79 BINK_SRC_X_OFF, ///< X components of motion value
80 BINK_SRC_Y_OFF, ///< Y components of motion value
81 BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
82 BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
83 BINK_SRC_RUN, ///< run lengths for special fill block
89 * data needed to decode 4-bit Huffman-coded value
92 int vlc_num; ///< tree number (in bink_trees[])
93 uint8_t syms[16]; ///< leaf value to symbol mapping
96 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
97 bink_trees[(tree).vlc_num].bits, 1)]
100 * data structure used for decoding single Bink data type
102 typedef struct Bundle {
103 int len; ///< length of number of entries to decode (in bits)
104 Tree tree; ///< Huffman tree-related data
105 uint8_t *data; ///< buffer for decoded symbols
106 uint8_t *data_end; ///< buffer end
107 uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
108 uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
114 typedef struct BinkContext {
115 AVCodecContext *avctx;
120 int version; ///< internal Bink file version
124 Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
125 Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
126 int col_lastval; ///< value of last decoded high nibble in "colours" data type
130 * Bink video block types
133 SKIP_BLOCK = 0, ///< skipped block
134 SCALED_BLOCK, ///< block has size 16x16
135 MOTION_BLOCK, ///< block is copied from previous frame with some offset
136 RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
137 RESIDUE_BLOCK, ///< motion block with some difference added
138 INTRA_BLOCK, ///< intra DCT block
139 FILL_BLOCK, ///< block is filled with single colour
140 INTER_BLOCK, ///< motion block with DCT applied to the difference
141 PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
142 RAW_BLOCK, ///< uncoded 8x8 block
146 * Initialize length length in all bundles.
148 * @param c decoder context
149 * @param width plane width
150 * @param bw plane width in 8x8 blocks
152 static void init_lengths(BinkContext *c, int width, int bw)
154 width = FFALIGN(width, 8);
156 c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
158 c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
160 c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
162 c->bundle[BINK_SRC_INTRA_DC].len =
163 c->bundle[BINK_SRC_INTER_DC].len =
164 c->bundle[BINK_SRC_X_OFF].len =
165 c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
167 c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
169 c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
173 * Allocate memory for bundles.
175 * @param c decoder context
177 static av_cold int init_bundles(BinkContext *c)
182 bw = (c->avctx->width + 7) >> 3;
183 bh = (c->avctx->height + 7) >> 3;
186 for (i = 0; i < BINKB_NB_SRC; i++) {
187 c->bundle[i].data = av_malloc(blocks * 64);
188 if (!c->bundle[i].data)
189 return AVERROR(ENOMEM);
190 c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
197 * Free memory used by bundles.
199 * @param c decoder context
201 static av_cold void free_bundles(BinkContext *c)
204 for (i = 0; i < BINKB_NB_SRC; i++)
205 av_freep(&c->bundle[i].data);
209 * Merge two consequent lists of equal size depending on bits read.
211 * @param gb context for reading bits
212 * @param dst buffer where merged list will be written to
213 * @param src pointer to the head of the first list (the second lists starts at src+size)
214 * @param size input lists size
216 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
218 uint8_t *src2 = src + size;
222 if (!get_bits1(gb)) {
229 } while (size && size2);
238 * Read information about Huffman tree used to decode data.
240 * @param gb context for reading bits
241 * @param tree pointer for storing tree data
243 static void read_tree(GetBitContext *gb, Tree *tree)
245 uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
248 tree->vlc_num = get_bits(gb, 4);
249 if (!tree->vlc_num) {
250 for (i = 0; i < 16; i++)
255 len = get_bits(gb, 3);
256 for (i = 0; i <= len; i++) {
257 tree->syms[i] = get_bits(gb, 4);
258 tmp1[tree->syms[i]] = 1;
260 for (i = 0; i < 16 && len < 16 - 1; i++)
262 tree->syms[++len] = i;
264 len = get_bits(gb, 2);
265 for (i = 0; i < 16; i++)
267 for (i = 0; i <= len; i++) {
269 for (t = 0; t < 16; t += size << 1)
270 merge(gb, out + t, in + t, size);
271 FFSWAP(uint8_t*, in, out);
273 memcpy(tree->syms, in, 16);
278 * Prepare bundle for decoding data.
280 * @param gb context for reading bits
281 * @param c decoder context
282 * @param bundle_num number of the bundle to initialize
284 static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
288 if (bundle_num == BINK_SRC_COLORS) {
289 for (i = 0; i < 16; i++)
290 read_tree(gb, &c->col_high[i]);
293 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
294 read_tree(gb, &c->bundle[bundle_num].tree);
295 c->bundle[bundle_num].cur_dec =
296 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
300 * common check before starting decoding bundle data
302 * @param gb context for reading bits
304 * @param t variable where number of elements to decode will be stored
306 #define CHECK_READ_VAL(gb, b, t) \
307 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
309 t = get_bits(gb, b->len); \
315 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
318 const uint8_t *dec_end;
320 CHECK_READ_VAL(gb, b, t);
321 dec_end = b->cur_dec + t;
322 if (dec_end > b->data_end) {
323 av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
324 return AVERROR_INVALIDDATA;
328 memset(b->cur_dec, v, t);
331 while (b->cur_dec < dec_end)
332 *b->cur_dec++ = GET_HUFF(gb, b->tree);
337 static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
340 const uint8_t *dec_end;
342 CHECK_READ_VAL(gb, b, t);
343 dec_end = b->cur_dec + t;
344 if (dec_end > b->data_end) {
345 av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
346 return AVERROR_INVALIDDATA;
351 sign = -get_bits1(gb);
352 v = (v ^ sign) - sign;
354 memset(b->cur_dec, v, t);
357 while (b->cur_dec < dec_end) {
358 v = GET_HUFF(gb, b->tree);
360 sign = -get_bits1(gb);
361 v = (v ^ sign) - sign;
369 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
371 static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
375 const uint8_t *dec_end;
377 CHECK_READ_VAL(gb, b, t);
378 dec_end = b->cur_dec + t;
379 if (dec_end > b->data_end) {
380 av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
381 return AVERROR_INVALIDDATA;
385 memset(b->cur_dec, v, t);
388 while (b->cur_dec < dec_end) {
389 v = GET_HUFF(gb, b->tree);
394 int run = bink_rlelens[v - 12];
396 if (dec_end - b->cur_dec < run)
397 return AVERROR_INVALIDDATA;
398 memset(b->cur_dec, last, run);
406 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
409 const uint8_t *dec_end;
411 CHECK_READ_VAL(gb, b, t);
412 dec_end = b->cur_dec + t;
413 if (dec_end > b->data_end) {
414 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
415 return AVERROR_INVALIDDATA;
417 while (b->cur_dec < dec_end) {
418 v = GET_HUFF(gb, b->tree);
419 v |= GET_HUFF(gb, b->tree) << 4;
426 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
429 const uint8_t *dec_end;
431 CHECK_READ_VAL(gb, b, t);
432 dec_end = b->cur_dec + t;
433 if (dec_end > b->data_end) {
434 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
435 return AVERROR_INVALIDDATA;
438 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
439 v = GET_HUFF(gb, b->tree);
440 v = (c->col_lastval << 4) | v;
441 if (c->version < 'i') {
442 sign = ((int8_t) v) >> 7;
443 v = ((v & 0x7F) ^ sign) - sign;
446 memset(b->cur_dec, v, t);
449 while (b->cur_dec < dec_end) {
450 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
451 v = GET_HUFF(gb, b->tree);
452 v = (c->col_lastval << 4) | v;
453 if (c->version < 'i') {
454 sign = ((int8_t) v) >> 7;
455 v = ((v & 0x7F) ^ sign) - sign;
464 /** number of bits used to store first DC value in bundle */
465 #define DC_START_BITS 11
467 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
468 int start_bits, int has_sign)
470 int i, j, len, len2, bsize, sign, v, v2;
471 int16_t *dst = (int16_t*)b->cur_dec;
472 int16_t *dst_end = (int16_t*)b->data_end;
474 CHECK_READ_VAL(gb, b, len);
475 v = get_bits(gb, start_bits - has_sign);
477 sign = -get_bits1(gb);
478 v = (v ^ sign) - sign;
480 if (dst_end - dst < 1)
481 return AVERROR_INVALIDDATA;
484 for (i = 0; i < len; i += 8) {
485 len2 = FFMIN(len - i, 8);
486 if (dst_end - dst < len2)
487 return AVERROR_INVALIDDATA;
488 bsize = get_bits(gb, 4);
490 for (j = 0; j < len2; j++) {
491 v2 = get_bits(gb, bsize);
493 sign = -get_bits1(gb);
494 v2 = (v2 ^ sign) - sign;
498 if (v < -32768 || v > 32767) {
499 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
500 return AVERROR_INVALIDDATA;
504 for (j = 0; j < len2; j++)
509 b->cur_dec = (uint8_t*)dst;
514 * Retrieve next value from bundle.
516 * @param c decoder context
517 * @param bundle bundle number
519 static inline int get_value(BinkContext *c, int bundle)
523 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
524 return *c->bundle[bundle].cur_ptr++;
525 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
526 return (int8_t)*c->bundle[bundle].cur_ptr++;
527 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
528 c->bundle[bundle].cur_ptr += 2;
532 static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
534 c->bundle[bundle_num].cur_dec =
535 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
536 c->bundle[bundle_num].len = 13;
539 static av_cold void binkb_init_bundles(BinkContext *c)
542 for (i = 0; i < BINKB_NB_SRC; i++)
543 binkb_init_bundle(c, i);
546 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
548 const int bits = binkb_bundle_sizes[bundle_num];
549 const int mask = 1 << (bits - 1);
550 const int issigned = binkb_bundle_signed[bundle_num];
551 Bundle *b = &c->bundle[bundle_num];
554 CHECK_READ_VAL(gb, b, len);
555 if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
556 return AVERROR_INVALIDDATA;
559 for (i = 0; i < len; i++)
560 *b->cur_dec++ = get_bits(gb, bits);
562 for (i = 0; i < len; i++)
563 *b->cur_dec++ = get_bits(gb, bits) - mask;
566 int16_t *dst = (int16_t*)b->cur_dec;
569 for (i = 0; i < len; i++)
570 *dst++ = get_bits(gb, bits);
572 for (i = 0; i < len; i++)
573 *dst++ = get_bits(gb, bits) - mask;
575 b->cur_dec = (uint8_t*)dst;
580 static inline int binkb_get_value(BinkContext *c, int bundle_num)
583 const int bits = binkb_bundle_sizes[bundle_num];
586 int val = *c->bundle[bundle_num].cur_ptr++;
587 return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
589 ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
590 c->bundle[bundle_num].cur_ptr += 2;
595 * Read 8x8 block of DCT coefficients.
597 * @param gb context for reading bits
598 * @param block place for storing coefficients
599 * @param scan scan order table
600 * @param quant_matrices quantization matrices
601 * @return 0 for success, negative value in other cases
603 static int read_dct_coeffs(GetBitContext *gb, int32_t block[64], const uint8_t *scan,
604 const int32_t quant_matrices[16][64], int q)
608 int i, t, bits, ccoef, mode, sign;
609 int list_start = 64, list_end = 64, list_pos;
613 const int32_t *quant;
615 coef_list[list_end] = 4; mode_list[list_end++] = 0;
616 coef_list[list_end] = 24; mode_list[list_end++] = 0;
617 coef_list[list_end] = 44; mode_list[list_end++] = 0;
618 coef_list[list_end] = 1; mode_list[list_end++] = 3;
619 coef_list[list_end] = 2; mode_list[list_end++] = 3;
620 coef_list[list_end] = 3; mode_list[list_end++] = 3;
622 for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
623 list_pos = list_start;
624 while (list_pos < list_end) {
625 if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
629 ccoef = coef_list[list_pos];
630 mode = mode_list[list_pos];
633 coef_list[list_pos] = ccoef + 4;
634 mode_list[list_pos] = 1;
637 coef_list[list_pos] = 0;
638 mode_list[list_pos++] = 0;
640 for (i = 0; i < 4; i++, ccoef++) {
642 coef_list[--list_start] = ccoef;
643 mode_list[ list_start] = 3;
646 t = 1 - (get_bits1(gb) << 1);
648 t = get_bits(gb, bits) | 1 << bits;
649 sign = -get_bits1(gb);
650 t = (t ^ sign) - sign;
652 block[scan[ccoef]] = t;
653 coef_idx[coef_count++] = ccoef;
658 mode_list[list_pos] = 2;
659 for (i = 0; i < 3; i++) {
661 coef_list[list_end] = ccoef;
662 mode_list[list_end++] = 2;
667 t = 1 - (get_bits1(gb) << 1);
669 t = get_bits(gb, bits) | 1 << bits;
670 sign = -get_bits1(gb);
671 t = (t ^ sign) - sign;
673 block[scan[ccoef]] = t;
674 coef_idx[coef_count++] = ccoef;
675 coef_list[list_pos] = 0;
676 mode_list[list_pos++] = 0;
683 quant_idx = get_bits(gb, 4);
686 if (quant_idx > 15U) {
687 av_log(NULL, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
688 return AVERROR_INVALIDDATA;
692 quant = quant_matrices[quant_idx];
694 block[0] = (block[0] * quant[0]) >> 11;
695 for (i = 0; i < coef_count; i++) {
696 int idx = coef_idx[i];
697 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
704 * Read 8x8 block with residue after motion compensation.
706 * @param gb context for reading bits
707 * @param block place to store read data
708 * @param masks_count number of masks to decode
709 * @return 0 on success, negative value in other cases
711 static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
715 int i, sign, mask, ccoef, mode;
716 int list_start = 64, list_end = 64, list_pos;
718 int nz_coeff_count = 0;
720 coef_list[list_end] = 4; mode_list[list_end++] = 0;
721 coef_list[list_end] = 24; mode_list[list_end++] = 0;
722 coef_list[list_end] = 44; mode_list[list_end++] = 0;
723 coef_list[list_end] = 0; mode_list[list_end++] = 2;
725 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
726 for (i = 0; i < nz_coeff_count; i++) {
729 if (block[nz_coeff[i]] < 0)
730 block[nz_coeff[i]] -= mask;
732 block[nz_coeff[i]] += mask;
737 list_pos = list_start;
738 while (list_pos < list_end) {
739 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
743 ccoef = coef_list[list_pos];
744 mode = mode_list[list_pos];
747 coef_list[list_pos] = ccoef + 4;
748 mode_list[list_pos] = 1;
751 coef_list[list_pos] = 0;
752 mode_list[list_pos++] = 0;
754 for (i = 0; i < 4; i++, ccoef++) {
756 coef_list[--list_start] = ccoef;
757 mode_list[ list_start] = 3;
759 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
760 sign = -get_bits1(gb);
761 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
769 mode_list[list_pos] = 2;
770 for (i = 0; i < 3; i++) {
772 coef_list[list_end] = ccoef;
773 mode_list[list_end++] = 2;
777 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
778 sign = -get_bits1(gb);
779 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
780 coef_list[list_pos] = 0;
781 mode_list[list_pos++] = 0;
794 * Copy 8x8 block from source to destination, where src and dst may be overlapped
796 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
800 for (i = 0; i < 8; i++)
801 memcpy(tmp + i*8, src + i*stride, 8);
802 for (i = 0; i < 8; i++)
803 memcpy(dst + i*stride, tmp + i*8, 8);
806 static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
807 int plane_idx, int is_key, int is_chroma)
811 uint8_t *dst, *ref, *ref_start, *ref_end;
815 LOCAL_ALIGNED_16(int16_t, block, [64]);
816 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
818 int ybias = is_key ? -15 : 0;
821 const int stride = frame->linesize[plane_idx];
822 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
823 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
825 binkb_init_bundles(c);
826 ref_start = frame->data[plane_idx];
827 ref_end = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
829 for (i = 0; i < 64; i++)
830 coordmap[i] = (i & 7) + (i >> 3) * stride;
832 for (by = 0; by < bh; by++) {
833 for (i = 0; i < BINKB_NB_SRC; i++) {
834 if ((ret = binkb_read_bundle(c, gb, i)) < 0)
838 dst = frame->data[plane_idx] + 8*by*stride;
839 for (bx = 0; bx < bw; bx++, dst += 8) {
840 blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
845 scan = bink_patterns[get_bits(gb, 4)];
850 mode = get_bits1(gb);
851 run = get_bits(gb, binkb_runbits[i]) + 1;
855 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
856 return AVERROR_INVALIDDATA;
859 v = binkb_get_value(c, BINKB_SRC_COLORS);
860 for (j = 0; j < run; j++)
861 dst[coordmap[*scan++]] = v;
863 for (j = 0; j < run; j++)
864 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
868 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
871 memset(dctblock, 0, sizeof(*dctblock) * 64);
872 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
873 qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
874 read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_intra_quant, qp);
875 c->bdsp.idct_put(dst, stride, dctblock);
878 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
879 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
880 ref = dst + xoff + yoff * stride;
881 if (ref < ref_start || ref + 8*stride > ref_end) {
882 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
883 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
884 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
886 put_pixels8x8_overlapped(dst, ref, stride);
888 c->dsp.clear_block(block);
889 v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
890 read_residue(gb, block, v);
891 c->dsp.add_pixels8(dst, block, stride);
894 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
895 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
896 ref = dst + xoff + yoff * stride;
897 if (ref < ref_start || ref + 8 * stride > ref_end) {
898 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
899 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
900 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
902 put_pixels8x8_overlapped(dst, ref, stride);
904 memset(dctblock, 0, sizeof(*dctblock) * 64);
905 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
906 qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
907 read_dct_coeffs(gb, dctblock, bink_scan, (const int32_t (*)[64])binkb_inter_quant, qp);
908 c->bdsp.idct_add(dst, stride, dctblock);
911 v = binkb_get_value(c, BINKB_SRC_COLORS);
912 c->dsp.fill_block_tab[1](dst, v, stride, 8);
915 for (i = 0; i < 2; i++)
916 col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
917 for (i = 0; i < 8; i++) {
918 v = binkb_get_value(c, BINKB_SRC_PATTERN);
919 for (j = 0; j < 8; j++, v >>= 1)
920 dst[i*stride + j] = col[v & 1];
924 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
925 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
926 ref = dst + xoff + yoff * stride;
927 if (ref < ref_start || ref + 8 * stride > ref_end) {
928 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
929 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
930 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
932 put_pixels8x8_overlapped(dst, ref, stride);
936 for (i = 0; i < 8; i++)
937 memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
938 c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
941 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
942 return AVERROR_INVALIDDATA;
946 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
947 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
952 static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
953 int plane_idx, int is_chroma)
957 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
961 LOCAL_ALIGNED_16(int16_t, block, [64]);
962 LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
963 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
966 const int stride = frame->linesize[plane_idx];
967 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
968 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
969 int width = c->avctx->width >> is_chroma;
971 init_lengths(c, FFMAX(width, 8), bw);
972 for (i = 0; i < BINK_NB_SRC; i++)
973 read_bundle(gb, c, i);
975 ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
976 : frame->data[plane_idx];
978 + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
980 for (i = 0; i < 64; i++)
981 coordmap[i] = (i & 7) + (i >> 3) * stride;
983 for (by = 0; by < bh; by++) {
984 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
986 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
988 if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
990 if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
992 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
994 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
996 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
998 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
1000 if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
1005 dst = frame->data[plane_idx] + 8*by*stride;
1006 prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
1007 : frame->data[plane_idx]) + 8*by*stride;
1008 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1009 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
1010 // 16x16 block type on odd line means part of the already decoded block, so skip it
1011 if ((by & 1) && blk == SCALED_BLOCK) {
1019 c->hdsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1022 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
1025 scan = bink_patterns[get_bits(gb, 4)];
1028 int run = get_value(c, BINK_SRC_RUN) + 1;
1032 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1033 return AVERROR_INVALIDDATA;
1035 if (get_bits1(gb)) {
1036 v = get_value(c, BINK_SRC_COLORS);
1037 for (j = 0; j < run; j++)
1038 ublock[*scan++] = v;
1040 for (j = 0; j < run; j++)
1041 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1045 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1048 memset(dctblock, 0, sizeof(*dctblock) * 64);
1049 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1050 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1051 c->bdsp.idct_put(ublock, 8, dctblock);
1054 v = get_value(c, BINK_SRC_COLORS);
1055 c->dsp.fill_block_tab[0](dst, v, stride, 16);
1058 for (i = 0; i < 2; i++)
1059 col[i] = get_value(c, BINK_SRC_COLORS);
1060 for (j = 0; j < 8; j++) {
1061 v = get_value(c, BINK_SRC_PATTERN);
1062 for (i = 0; i < 8; i++, v >>= 1)
1063 ublock[i + j*8] = col[v & 1];
1067 for (j = 0; j < 8; j++)
1068 for (i = 0; i < 8; i++)
1069 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1072 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1073 return AVERROR_INVALIDDATA;
1075 if (blk != FILL_BLOCK)
1076 c->bdsp.scale_block(ublock, dst, stride);
1082 xoff = get_value(c, BINK_SRC_X_OFF);
1083 yoff = get_value(c, BINK_SRC_Y_OFF);
1084 ref = prev + xoff + yoff * stride;
1085 if (ref < ref_start || ref > ref_end) {
1086 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1087 bx*8 + xoff, by*8 + yoff);
1088 return AVERROR_INVALIDDATA;
1090 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1093 scan = bink_patterns[get_bits(gb, 4)];
1096 int run = get_value(c, BINK_SRC_RUN) + 1;
1100 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1101 return AVERROR_INVALIDDATA;
1103 if (get_bits1(gb)) {
1104 v = get_value(c, BINK_SRC_COLORS);
1105 for (j = 0; j < run; j++)
1106 dst[coordmap[*scan++]] = v;
1108 for (j = 0; j < run; j++)
1109 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1113 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1116 xoff = get_value(c, BINK_SRC_X_OFF);
1117 yoff = get_value(c, BINK_SRC_Y_OFF);
1118 ref = prev + xoff + yoff * stride;
1119 if (ref < ref_start || ref > ref_end) {
1120 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1121 bx*8 + xoff, by*8 + yoff);
1122 return AVERROR_INVALIDDATA;
1124 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1125 c->dsp.clear_block(block);
1126 v = get_bits(gb, 7);
1127 read_residue(gb, block, v);
1128 c->dsp.add_pixels8(dst, block, stride);
1131 memset(dctblock, 0, sizeof(*dctblock) * 64);
1132 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1133 read_dct_coeffs(gb, dctblock, bink_scan, bink_intra_quant, -1);
1134 c->bdsp.idct_put(dst, stride, dctblock);
1137 v = get_value(c, BINK_SRC_COLORS);
1138 c->dsp.fill_block_tab[1](dst, v, stride, 8);
1141 xoff = get_value(c, BINK_SRC_X_OFF);
1142 yoff = get_value(c, BINK_SRC_Y_OFF);
1143 ref = prev + xoff + yoff * stride;
1144 if (ref < ref_start || ref > ref_end) {
1145 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1146 bx*8 + xoff, by*8 + yoff);
1149 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
1150 memset(dctblock, 0, sizeof(*dctblock) * 64);
1151 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1152 read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
1153 c->bdsp.idct_add(dst, stride, dctblock);
1156 for (i = 0; i < 2; i++)
1157 col[i] = get_value(c, BINK_SRC_COLORS);
1158 for (i = 0; i < 8; i++) {
1159 v = get_value(c, BINK_SRC_PATTERN);
1160 for (j = 0; j < 8; j++, v >>= 1)
1161 dst[i*stride + j] = col[v & 1];
1165 for (i = 0; i < 8; i++)
1166 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1167 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1170 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1171 return AVERROR_INVALIDDATA;
1175 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1176 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1181 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
1183 BinkContext * const c = avctx->priv_data;
1184 AVFrame *frame = data;
1186 int plane, plane_idx, ret;
1187 int bits_count = pkt->size << 3;
1189 if (c->version > 'b') {
1190 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1193 if ((ret = ff_reget_buffer(avctx, c->last)) < 0)
1195 if ((ret = av_frame_ref(frame, c->last)) < 0)
1199 init_get_bits(&gb, pkt->data, bits_count);
1201 if (c->version >= 'i')
1202 skip_bits_long(&gb, 32);
1203 if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
1206 if (c->version >= 'i')
1207 skip_bits_long(&gb, 32);
1209 for (plane = 0; plane < 3; plane++) {
1210 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1212 if (c->version > 'b') {
1213 if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
1216 if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
1217 !avctx->frame_number, !!plane)) < 0)
1220 if (get_bits_count(&gb) >= bits_count)
1225 if (c->version > 'b') {
1226 av_frame_unref(c->last);
1227 if ((ret = av_frame_ref(c->last, frame)) < 0)
1233 /* always report that the buffer was completely consumed */
1238 * Caclulate quantization tables for version b
1240 static av_cold void binkb_calc_quant(void)
1242 uint8_t inv_bink_scan[64];
1243 static const int s[64]={
1244 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1245 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1246 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1247 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1248 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1249 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1250 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1251 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1255 for (i = 0; i < 64; i++)
1256 inv_bink_scan[bink_scan[i]] = i;
1258 for (j = 0; j < 16; j++) {
1259 for (i = 0; i < 64; i++) {
1260 int k = inv_bink_scan[i];
1261 binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
1262 binkb_num[j]/(binkb_den[j] * (C>>12));
1263 binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
1264 binkb_num[j]/(binkb_den[j] * (C>>12));
1269 static av_cold int decode_init(AVCodecContext *avctx)
1271 BinkContext * const c = avctx->priv_data;
1272 static VLC_TYPE table[16 * 128][2];
1273 static int binkb_initialised = 0;
1277 c->version = avctx->codec_tag >> 24;
1278 if (avctx->extradata_size < 4) {
1279 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1280 return AVERROR_INVALIDDATA;
1282 flags = AV_RL32(avctx->extradata);
1283 c->has_alpha = flags & BINK_FLAG_ALPHA;
1284 c->swap_planes = c->version >= 'h';
1285 if (!bink_trees[15].table) {
1286 for (i = 0; i < 16; i++) {
1287 const int maxbits = bink_tree_lens[i][15];
1288 bink_trees[i].table = table + i*128;
1289 bink_trees[i].table_allocated = 1 << maxbits;
1290 init_vlc(&bink_trees[i], maxbits, 16,
1291 bink_tree_lens[i], 1, 1,
1292 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1297 c->last = av_frame_alloc();
1299 return AVERROR(ENOMEM);
1301 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1304 avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1306 ff_dsputil_init(&c->dsp, avctx);
1307 ff_hpeldsp_init(&c->hdsp, avctx->flags);
1308 ff_binkdsp_init(&c->bdsp);
1310 if ((ret = init_bundles(c)) < 0) {
1315 if (c->version == 'b') {
1316 if (!binkb_initialised) {
1318 binkb_initialised = 1;
1325 static av_cold int decode_end(AVCodecContext *avctx)
1327 BinkContext * const c = avctx->priv_data;
1329 av_frame_free(&c->last);
1335 AVCodec ff_bink_decoder = {
1336 .name = "binkvideo",
1337 .type = AVMEDIA_TYPE_VIDEO,
1338 .id = AV_CODEC_ID_BINKVIDEO,
1339 .priv_data_size = sizeof(BinkContext),
1340 .init = decode_init,
1341 .close = decode_end,
1342 .decode = decode_frame,
1343 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
1344 .capabilities = CODEC_CAP_DR1,