3 * Copyright (c) 2009 Konstantin Shishkov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavcore/imgutils.h"
28 #define ALT_BITSTREAM_READER_LE
31 #define BINK_FLAG_ALPHA 0x00100000
32 #define BINK_FLAG_GRAY 0x00020000
34 static VLC bink_trees[16];
37 * IDs for different data types used in Bink video codec
40 BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
41 BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
42 BINK_SRC_COLORS, ///< pixel values used for different block types
43 BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
44 BINK_SRC_X_OFF, ///< X components of motion value
45 BINK_SRC_Y_OFF, ///< Y components of motion value
46 BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
47 BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
48 BINK_SRC_RUN, ///< run lengths for special fill block
54 * data needed to decode 4-bit Huffman-coded value
57 int vlc_num; ///< tree number (in bink_trees[])
58 uint8_t syms[16]; ///< leaf value to symbol mapping
61 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
62 bink_trees[(tree).vlc_num].bits, 1)]
65 * data structure used for decoding single Bink data type
67 typedef struct Bundle {
68 int len; ///< length of number of entries to decode (in bits)
69 Tree tree; ///< Huffman tree-related data
70 uint8_t *data; ///< buffer for decoded symbols
71 uint8_t *data_end; ///< buffer end
72 uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
73 uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
79 typedef struct BinkContext {
80 AVCodecContext *avctx;
83 int version; ///< internal Bink file version
86 ScanTable scantable; ///< permutated scantable for DCT coeffs decoding
88 Bundle bundle[BINK_NB_SRC]; ///< bundles for decoding all data types
89 Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
90 int col_lastval; ///< value of last decoded high nibble in "colours" data type
94 * Bink video block types
97 SKIP_BLOCK = 0, ///< skipped block
98 SCALED_BLOCK, ///< block has size 16x16
99 MOTION_BLOCK, ///< block is copied from previous frame with some offset
100 RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
101 RESIDUE_BLOCK, ///< motion block with some difference added
102 INTRA_BLOCK, ///< intra DCT block
103 FILL_BLOCK, ///< block is filled with single colour
104 INTER_BLOCK, ///< motion block with DCT applied to the difference
105 PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
106 RAW_BLOCK, ///< uncoded 8x8 block
110 * Initialize length length in all bundles.
112 * @param c decoder context
113 * @param width plane width
114 * @param bw plane width in 8x8 blocks
116 static void init_lengths(BinkContext *c, int width, int bw)
118 c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
120 c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
122 c->bundle[BINK_SRC_COLORS].len = av_log2((width >> 3)*64 + 511) + 1;
124 c->bundle[BINK_SRC_INTRA_DC].len =
125 c->bundle[BINK_SRC_INTER_DC].len =
126 c->bundle[BINK_SRC_X_OFF].len =
127 c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
129 c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
131 c->bundle[BINK_SRC_RUN].len = av_log2((width >> 3)*48 + 511) + 1;
135 * Allocate memory for bundles.
137 * @param c decoder context
139 static av_cold void init_bundles(BinkContext *c)
144 bw = (c->avctx->width + 7) >> 3;
145 bh = (c->avctx->height + 7) >> 3;
148 for (i = 0; i < BINK_NB_SRC; i++) {
149 c->bundle[i].data = av_malloc(blocks * 64);
150 c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
155 * Free memory used by bundles.
157 * @param c decoder context
159 static av_cold void free_bundles(BinkContext *c)
162 for (i = 0; i < BINK_NB_SRC; i++)
163 av_freep(&c->bundle[i].data);
167 * Merge two consequent lists of equal size depending on bits read.
169 * @param gb context for reading bits
170 * @param dst buffer where merged list will be written to
171 * @param src pointer to the head of the first list (the second lists starts at src+size)
172 * @param size input lists size
174 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
176 uint8_t *src2 = src + size;
180 if (!get_bits1(gb)) {
187 } while (size && size2);
196 * Read information about Huffman tree used to decode data.
198 * @param gb context for reading bits
199 * @param tree pointer for storing tree data
201 static void read_tree(GetBitContext *gb, Tree *tree)
203 uint8_t tmp1[16], tmp2[16], *in = tmp1, *out = tmp2;
206 tree->vlc_num = get_bits(gb, 4);
207 if (!tree->vlc_num) {
208 for (i = 0; i < 16; i++)
213 len = get_bits(gb, 3);
214 memset(tmp1, 0, sizeof(tmp1));
215 for (i = 0; i <= len; i++) {
216 tree->syms[i] = get_bits(gb, 4);
217 tmp1[tree->syms[i]] = 1;
219 for (i = 0; i < 16; i++)
221 tree->syms[++len] = i;
223 len = get_bits(gb, 2);
224 for (i = 0; i < 16; i++)
226 for (i = 0; i <= len; i++) {
228 for (t = 0; t < 16; t += size << 1)
229 merge(gb, out + t, in + t, size);
230 FFSWAP(uint8_t*, in, out);
232 memcpy(tree->syms, in, 16);
237 * Prepare bundle for decoding data.
239 * @param gb context for reading bits
240 * @param c decoder context
241 * @param bundle_num number of the bundle to initialize
243 static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
247 if (bundle_num == BINK_SRC_COLORS) {
248 for (i = 0; i < 16; i++)
249 read_tree(gb, &c->col_high[i]);
252 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
253 read_tree(gb, &c->bundle[bundle_num].tree);
254 c->bundle[bundle_num].cur_dec =
255 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
259 * common check before starting decoding bundle data
261 * @param gb context for reading bits
263 * @param t variable where number of elements to decode will be stored
265 #define CHECK_READ_VAL(gb, b, t) \
266 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
268 t = get_bits(gb, b->len); \
274 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
277 const uint8_t *dec_end;
279 CHECK_READ_VAL(gb, b, t);
280 dec_end = b->cur_dec + t;
281 if (dec_end > b->data_end) {
282 av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
287 memset(b->cur_dec, v, t);
290 while (b->cur_dec < dec_end)
291 *b->cur_dec++ = GET_HUFF(gb, b->tree);
296 static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
299 const uint8_t *dec_end;
301 CHECK_READ_VAL(gb, b, t);
302 dec_end = b->cur_dec + t;
303 if (dec_end > b->data_end) {
304 av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
310 sign = -get_bits1(gb);
311 v = (v ^ sign) - sign;
313 memset(b->cur_dec, v, t);
317 v = GET_HUFF(gb, b->tree);
319 sign = -get_bits1(gb);
320 v = (v ^ sign) - sign;
323 } while (b->cur_dec < dec_end);
328 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
330 static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
334 const uint8_t *dec_end;
336 CHECK_READ_VAL(gb, b, t);
337 dec_end = b->cur_dec + t;
338 if (dec_end > b->data_end) {
339 av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
344 memset(b->cur_dec, v, t);
348 v = GET_HUFF(gb, b->tree);
353 int run = bink_rlelens[v - 12];
355 memset(b->cur_dec, last, run);
358 } while (b->cur_dec < dec_end);
363 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
366 const uint8_t *dec_end;
368 CHECK_READ_VAL(gb, b, t);
369 dec_end = b->cur_dec + t;
370 if (dec_end > b->data_end) {
371 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
374 while (b->cur_dec < dec_end) {
375 v = GET_HUFF(gb, b->tree);
376 v |= GET_HUFF(gb, b->tree) << 4;
383 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
386 const uint8_t *dec_end;
388 CHECK_READ_VAL(gb, b, t);
389 dec_end = b->cur_dec + t;
390 if (dec_end > b->data_end) {
391 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
395 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
396 v = GET_HUFF(gb, b->tree);
397 v = (c->col_lastval << 4) | v;
398 if (c->version < 'i') {
399 sign = ((int8_t) v) >> 7;
400 v = ((v & 0x7F) ^ sign) - sign;
403 memset(b->cur_dec, v, t);
406 while (b->cur_dec < dec_end) {
407 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
408 v = GET_HUFF(gb, b->tree);
409 v = (c->col_lastval << 4) | v;
410 if (c->version < 'i') {
411 sign = ((int8_t) v) >> 7;
412 v = ((v & 0x7F) ^ sign) - sign;
421 /** number of bits used to store first DC value in bundle */
422 #define DC_START_BITS 11
424 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
425 int start_bits, int has_sign)
427 int i, j, len, len2, bsize, sign, v, v2;
428 int16_t *dst = (int16_t*)b->cur_dec;
430 CHECK_READ_VAL(gb, b, len);
431 v = get_bits(gb, start_bits - has_sign);
433 sign = -get_bits1(gb);
434 v = (v ^ sign) - sign;
438 for (i = 0; i < len; i += 8) {
439 len2 = FFMIN(len - i, 8);
440 bsize = get_bits(gb, 4);
442 for (j = 0; j < len2; j++) {
443 v2 = get_bits(gb, bsize);
445 sign = -get_bits1(gb);
446 v2 = (v2 ^ sign) - sign;
450 if (v < -32768 || v > 32767) {
451 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
456 for (j = 0; j < len2; j++)
461 b->cur_dec = (uint8_t*)dst;
466 * Retrieve next value from bundle.
468 * @param c decoder context
469 * @param bundle bundle number
471 static inline int get_value(BinkContext *c, int bundle)
475 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
476 return *c->bundle[bundle].cur_ptr++;
477 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
478 return (int8_t)*c->bundle[bundle].cur_ptr++;
479 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
480 c->bundle[bundle].cur_ptr += 2;
485 * Read 8x8 block of DCT coefficients.
487 * @param gb context for reading bits
488 * @param block place for storing coefficients
489 * @param scan scan order table
490 * @param is_intra tells what set of quantizer matrices to use
491 * @return 0 for success, negative value in other cases
493 static int read_dct_coeffs(GetBitContext *gb, DCTELEM block[64], const uint8_t *scan,
498 int i, t, mask, bits, ccoef, mode, sign;
499 int list_start = 64, list_end = 64, list_pos;
503 const uint32_t *quant;
505 coef_list[list_end] = 4; mode_list[list_end++] = 0;
506 coef_list[list_end] = 24; mode_list[list_end++] = 0;
507 coef_list[list_end] = 44; mode_list[list_end++] = 0;
508 coef_list[list_end] = 1; mode_list[list_end++] = 3;
509 coef_list[list_end] = 2; mode_list[list_end++] = 3;
510 coef_list[list_end] = 3; mode_list[list_end++] = 3;
512 bits = get_bits(gb, 4) - 1;
513 for (mask = 1 << bits; bits >= 0; mask >>= 1, bits--) {
514 list_pos = list_start;
515 while (list_pos < list_end) {
516 if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
520 ccoef = coef_list[list_pos];
521 mode = mode_list[list_pos];
524 coef_list[list_pos] = ccoef + 4;
525 mode_list[list_pos] = 1;
528 coef_list[list_pos] = 0;
529 mode_list[list_pos++] = 0;
531 for (i = 0; i < 4; i++, ccoef++) {
533 coef_list[--list_start] = ccoef;
534 mode_list[ list_start] = 3;
538 t = 1 - (get_bits1(gb) << 1);
540 t = get_bits(gb, bits) | mask;
541 sign = -get_bits1(gb);
542 t = (t ^ sign) - sign;
544 block[scan[ccoef]] = t;
545 coef_idx[coef_count++] = ccoef;
550 mode_list[list_pos] = 2;
551 for (i = 0; i < 3; i++) {
553 coef_list[list_end] = ccoef;
554 mode_list[list_end++] = 2;
559 t = 1 - (get_bits1(gb) << 1);
561 t = get_bits(gb, bits) | mask;
562 sign = -get_bits1(gb);
563 t = (t ^ sign) - sign;
565 block[scan[ccoef]] = t;
566 coef_idx[coef_count++] = ccoef;
567 coef_list[list_pos] = 0;
568 mode_list[list_pos++] = 0;
574 quant_idx = get_bits(gb, 4);
575 quant = is_intra ? bink_intra_quant[quant_idx]
576 : bink_inter_quant[quant_idx];
577 block[0] = (block[0] * quant[0]) >> 11;
578 for (i = 0; i < coef_count; i++) {
579 int idx = coef_idx[i];
580 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
587 * Read 8x8 block with residue after motion compensation.
589 * @param gb context for reading bits
590 * @param block place to store read data
591 * @param masks_count number of masks to decode
592 * @return 0 on success, negative value in other cases
594 static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
598 int i, sign, mask, ccoef, mode;
599 int list_start = 64, list_end = 64, list_pos;
601 int nz_coeff_count = 0;
603 coef_list[list_end] = 4; mode_list[list_end++] = 0;
604 coef_list[list_end] = 24; mode_list[list_end++] = 0;
605 coef_list[list_end] = 44; mode_list[list_end++] = 0;
606 coef_list[list_end] = 0; mode_list[list_end++] = 2;
608 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
609 for (i = 0; i < nz_coeff_count; i++) {
612 if (block[nz_coeff[i]] < 0)
613 block[nz_coeff[i]] -= mask;
615 block[nz_coeff[i]] += mask;
620 list_pos = list_start;
621 while (list_pos < list_end) {
622 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
626 ccoef = coef_list[list_pos];
627 mode = mode_list[list_pos];
630 coef_list[list_pos] = ccoef + 4;
631 mode_list[list_pos] = 1;
634 coef_list[list_pos] = 0;
635 mode_list[list_pos++] = 0;
637 for (i = 0; i < 4; i++, ccoef++) {
639 coef_list[--list_start] = ccoef;
640 mode_list[ list_start] = 3;
642 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
643 sign = -get_bits1(gb);
644 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
652 mode_list[list_pos] = 2;
653 for (i = 0; i < 3; i++) {
655 coef_list[list_end] = ccoef;
656 mode_list[list_end++] = 2;
660 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
661 sign = -get_bits1(gb);
662 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
663 coef_list[list_pos] = 0;
664 mode_list[list_pos++] = 0;
676 static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
681 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
685 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
686 LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
689 const int stride = c->pic.linesize[plane_idx];
690 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
691 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
692 int width = c->avctx->width >> is_chroma;
694 init_lengths(c, FFMAX(width, 8), bw);
695 for (i = 0; i < BINK_NB_SRC; i++)
696 read_bundle(gb, c, i);
698 ref_start = c->last.data[plane_idx];
699 ref_end = c->last.data[plane_idx]
700 + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
702 for (i = 0; i < 64; i++)
703 coordmap[i] = (i & 7) + (i >> 3) * stride;
705 for (by = 0; by < bh; by++) {
706 if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
708 if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
710 if (read_colors(gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
712 if (read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
714 if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
716 if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
718 if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
720 if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
722 if (read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN]) < 0)
727 dst = c->pic.data[plane_idx] + 8*by*stride;
728 prev = c->last.data[plane_idx] + 8*by*stride;
729 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
730 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
731 // 16x16 block type on odd line means part of the already decoded block, so skip it
732 if ((by & 1) && blk == SCALED_BLOCK) {
740 c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
743 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
746 scan = bink_patterns[get_bits(gb, 4)];
749 int run = get_value(c, BINK_SRC_RUN) + 1;
753 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
757 v = get_value(c, BINK_SRC_COLORS);
758 for (j = 0; j < run; j++)
761 for (j = 0; j < run; j++)
762 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
766 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
769 c->dsp.clear_block(block);
770 block[0] = get_value(c, BINK_SRC_INTRA_DC);
771 read_dct_coeffs(gb, block, c->scantable.permutated, 1);
773 c->dsp.put_pixels_nonclamped(block, ublock, 8);
776 v = get_value(c, BINK_SRC_COLORS);
777 c->dsp.fill_block_tab[0](dst, v, stride, 16);
780 for (i = 0; i < 2; i++)
781 col[i] = get_value(c, BINK_SRC_COLORS);
782 for (j = 0; j < 8; j++) {
783 v = get_value(c, BINK_SRC_PATTERN);
784 for (i = 0; i < 8; i++, v >>= 1)
785 ublock[i + j*8] = col[v & 1];
789 for (j = 0; j < 8; j++)
790 for (i = 0; i < 8; i++)
791 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
794 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
797 if (blk != FILL_BLOCK)
798 c->dsp.scale_block(ublock, dst, stride);
804 xoff = get_value(c, BINK_SRC_X_OFF);
805 yoff = get_value(c, BINK_SRC_Y_OFF);
806 ref = prev + xoff + yoff * stride;
807 if (ref < ref_start || ref > ref_end) {
808 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
809 bx*8 + xoff, by*8 + yoff);
812 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
815 scan = bink_patterns[get_bits(gb, 4)];
818 int run = get_value(c, BINK_SRC_RUN) + 1;
822 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
826 v = get_value(c, BINK_SRC_COLORS);
827 for (j = 0; j < run; j++)
828 dst[coordmap[*scan++]] = v;
830 for (j = 0; j < run; j++)
831 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
835 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
838 xoff = get_value(c, BINK_SRC_X_OFF);
839 yoff = get_value(c, BINK_SRC_Y_OFF);
840 ref = prev + xoff + yoff * stride;
841 if (ref < ref_start || ref > ref_end) {
842 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
843 bx*8 + xoff, by*8 + yoff);
846 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
847 c->dsp.clear_block(block);
849 read_residue(gb, block, v);
850 c->dsp.add_pixels8(dst, block, stride);
853 c->dsp.clear_block(block);
854 block[0] = get_value(c, BINK_SRC_INTRA_DC);
855 read_dct_coeffs(gb, block, c->scantable.permutated, 1);
856 c->dsp.idct_put(dst, stride, block);
859 v = get_value(c, BINK_SRC_COLORS);
860 c->dsp.fill_block_tab[1](dst, v, stride, 8);
863 xoff = get_value(c, BINK_SRC_X_OFF);
864 yoff = get_value(c, BINK_SRC_Y_OFF);
865 ref = prev + xoff + yoff * stride;
866 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
867 c->dsp.clear_block(block);
868 block[0] = get_value(c, BINK_SRC_INTER_DC);
869 read_dct_coeffs(gb, block, c->scantable.permutated, 0);
870 c->dsp.idct_add(dst, stride, block);
873 for (i = 0; i < 2; i++)
874 col[i] = get_value(c, BINK_SRC_COLORS);
875 for (i = 0; i < 8; i++) {
876 v = get_value(c, BINK_SRC_PATTERN);
877 for (j = 0; j < 8; j++, v >>= 1)
878 dst[i*stride + j] = col[v & 1];
882 for (i = 0; i < 8; i++)
883 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
884 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
887 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
892 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
893 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
898 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
900 BinkContext * const c = avctx->priv_data;
902 int plane, plane_idx;
903 int bits_count = pkt->size << 3;
906 avctx->release_buffer(avctx, &c->pic);
908 if(avctx->get_buffer(avctx, &c->pic) < 0){
909 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
913 init_get_bits(&gb, pkt->data, bits_count);
915 if (c->version >= 'i')
916 skip_bits_long(&gb, 32);
917 if (bink_decode_plane(c, &gb, 3, 0) < 0)
920 if (c->version >= 'i')
921 skip_bits_long(&gb, 32);
923 for (plane = 0; plane < 3; plane++) {
924 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
926 if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
928 if (get_bits_count(&gb) >= bits_count)
933 *data_size = sizeof(AVFrame);
934 *(AVFrame*)data = c->pic;
936 FFSWAP(AVFrame, c->pic, c->last);
938 /* always report that the buffer was completely consumed */
942 static av_cold int decode_init(AVCodecContext *avctx)
944 BinkContext * const c = avctx->priv_data;
945 static VLC_TYPE table[16 * 128][2];
949 c->version = avctx->codec_tag >> 24;
950 if (c->version < 'c') {
951 av_log(avctx, AV_LOG_ERROR, "Too old version '%c'\n", c->version);
954 if (avctx->extradata_size < 4) {
955 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
958 flags = AV_RL32(avctx->extradata);
959 c->has_alpha = flags & BINK_FLAG_ALPHA;
960 c->swap_planes = c->version >= 'h';
961 if (!bink_trees[15].table) {
962 for (i = 0; i < 16; i++) {
963 const int maxbits = bink_tree_lens[i][15];
964 bink_trees[i].table = table + i*128;
965 bink_trees[i].table_allocated = 1 << maxbits;
966 init_vlc(&bink_trees[i], maxbits, 16,
967 bink_tree_lens[i], 1, 1,
968 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
973 c->pic.data[0] = NULL;
975 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
979 avctx->pix_fmt = c->has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
981 avctx->idct_algo = FF_IDCT_BINK;
982 dsputil_init(&c->dsp, avctx);
983 ff_init_scantable(c->dsp.idct_permutation, &c->scantable, bink_scan);
990 static av_cold int decode_end(AVCodecContext *avctx)
992 BinkContext * const c = avctx->priv_data;
995 avctx->release_buffer(avctx, &c->pic);
997 avctx->release_buffer(avctx, &c->last);
1003 AVCodec bink_decoder = {
1007 sizeof(BinkContext),
1012 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),