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
27 #define ALT_BITSTREAM_READER_LE
30 #define BINK_FLAG_ALPHA 0x00100000
31 #define BINK_FLAG_GRAY 0x00020000
33 static VLC bink_trees
[16];
36 * IDs for different data types used in Bink video codec
39 BINK_SRC_BLOCK_TYPES
= 0, ///< 8x8 block types
40 BINK_SRC_SUB_BLOCK_TYPES
, ///< 16x16 block types (a subset of 8x8 block types)
41 BINK_SRC_COLORS
, ///< pixel values used for different block types
42 BINK_SRC_PATTERN
, ///< 8-bit values for 2-colour pattern fill
43 BINK_SRC_X_OFF
, ///< X components of motion value
44 BINK_SRC_Y_OFF
, ///< Y components of motion value
45 BINK_SRC_INTRA_DC
, ///< DC values for intrablocks with DCT
46 BINK_SRC_INTER_DC
, ///< DC values for interblocks with DCT
47 BINK_SRC_RUN
, ///< run lengths for special fill block
53 * data needed to decode 4-bit Huffman-coded value
56 int vlc_num
; ///< tree number (in bink_trees[])
57 uint8_t syms
[16]; ///< leaf value to symbol mapping
60 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
61 bink_trees[(tree).vlc_num].bits, 1)]
64 * data structure used for decoding single Bink data type
66 typedef struct Bundle
{
67 int len
; ///< length of number of entries to decode (in bits)
68 Tree tree
; ///< Huffman tree-related data
69 uint8_t *data
; ///< buffer for decoded symbols
70 uint8_t *data_end
; ///< buffer end
71 uint8_t *cur_dec
; ///< pointer to the not yet decoded part of the buffer
72 uint8_t *cur_ptr
; ///< pointer to the data that is not read from buffer yet
78 typedef struct BinkContext
{
79 AVCodecContext
*avctx
;
82 int version
; ///< internal Bink file version
85 ScanTable scantable
; ///< permutated scantable for DCT coeffs decoding
87 Bundle bundle
[BINK_NB_SRC
]; ///< bundles for decoding all data types
88 Tree col_high
[16]; ///< trees for decoding high nibble in "colours" data type
89 int col_lastval
; ///< value of last decoded high nibble in "colours" data type
93 * Bink video block types
96 SKIP_BLOCK
= 0, ///< skipped block
97 SCALED_BLOCK
, ///< block has size 16x16
98 MOTION_BLOCK
, ///< block is copied from previous frame with some offset
99 RUN_BLOCK
, ///< block is composed from runs of colours with custom scan order
100 RESIDUE_BLOCK
, ///< motion block with some difference added
101 INTRA_BLOCK
, ///< intra DCT block
102 FILL_BLOCK
, ///< block is filled with single colour
103 INTER_BLOCK
, ///< motion block with DCT applied to the difference
104 PATTERN_BLOCK
, ///< block is filled with two colours following custom pattern
105 RAW_BLOCK
, ///< uncoded 8x8 block
109 * Initializes length length in all bundles.
111 * @param c decoder context
112 * @param width plane width
113 * @param bw plane width in 8x8 blocks
115 static void init_lengths(BinkContext
*c
, int width
, int bw
)
117 c
->bundle
[BINK_SRC_BLOCK_TYPES
].len
= av_log2((width
>> 3) + 511) + 1;
119 c
->bundle
[BINK_SRC_SUB_BLOCK_TYPES
].len
= av_log2((width
>> 4) + 511) + 1;
121 c
->bundle
[BINK_SRC_COLORS
].len
= av_log2((width
>> 3)*64 + 511) + 1;
123 c
->bundle
[BINK_SRC_INTRA_DC
].len
=
124 c
->bundle
[BINK_SRC_INTER_DC
].len
=
125 c
->bundle
[BINK_SRC_X_OFF
].len
=
126 c
->bundle
[BINK_SRC_Y_OFF
].len
= av_log2((width
>> 3) + 511) + 1;
128 c
->bundle
[BINK_SRC_PATTERN
].len
= av_log2((bw
<< 3) + 511) + 1;
130 c
->bundle
[BINK_SRC_RUN
].len
= av_log2((width
>> 3)*48 + 511) + 1;
134 * Allocates memory for bundles.
136 * @param c decoder context
138 static av_cold
void init_bundles(BinkContext
*c
)
143 bw
= (c
->avctx
->width
+ 7) >> 3;
144 bh
= (c
->avctx
->height
+ 7) >> 3;
147 for (i
= 0; i
< BINK_NB_SRC
; i
++) {
148 c
->bundle
[i
].data
= av_malloc(blocks
* 64);
149 c
->bundle
[i
].data_end
= c
->bundle
[i
].data
+ blocks
* 64;
154 * Frees memory used by bundles.
156 * @param c decoder context
158 static av_cold
void free_bundles(BinkContext
*c
)
161 for (i
= 0; i
< BINK_NB_SRC
; i
++)
162 av_freep(&c
->bundle
[i
].data
);
166 * Merges two consequent lists of equal size depending on bits read.
168 * @param gb context for reading bits
169 * @param dst buffer where merged list will be written to
170 * @param src pointer to the head of the first list (the second lists starts at src+size)
171 * @param size input lists size
173 static void merge(GetBitContext
*gb
, uint8_t *dst
, uint8_t *src
, int size
)
175 uint8_t *src2
= src
+ size
;
179 if (!get_bits1(gb
)) {
186 } while (size
&& size2
);
195 * Reads information about Huffman tree used to decode data.
197 * @param gb context for reading bits
198 * @param tree pointer for storing tree data
200 static void read_tree(GetBitContext
*gb
, Tree
*tree
)
202 uint8_t tmp1
[16], tmp2
[16], *in
= tmp1
, *out
= tmp2
;
205 tree
->vlc_num
= get_bits(gb
, 4);
206 if (!tree
->vlc_num
) {
207 for (i
= 0; i
< 16; i
++)
212 len
= get_bits(gb
, 3);
213 memset(tmp1
, 0, sizeof(tmp1
));
214 for (i
= 0; i
<= len
; i
++) {
215 tree
->syms
[i
] = get_bits(gb
, 4);
216 tmp1
[tree
->syms
[i
]] = 1;
218 for (i
= 0; i
< 16; i
++)
220 tree
->syms
[++len
] = i
;
222 len
= get_bits(gb
, 2);
223 for (i
= 0; i
< 16; i
++)
225 for (i
= 0; i
<= len
; i
++) {
227 for (t
= 0; t
< 16; t
+= size
<< 1)
228 merge(gb
, out
+ t
, in
+ t
, size
);
229 FFSWAP(uint8_t*, in
, out
);
231 memcpy(tree
->syms
, in
, 16);
236 * Prepares bundle for decoding data.
238 * @param gb context for reading bits
239 * @param c decoder context
240 * @param bundle_num number of the bundle to initialize
242 static void read_bundle(GetBitContext
*gb
, BinkContext
*c
, int bundle_num
)
246 if (bundle_num
== BINK_SRC_COLORS
) {
247 for (i
= 0; i
< 16; i
++)
248 read_tree(gb
, &c
->col_high
[i
]);
251 if (bundle_num
!= BINK_SRC_INTRA_DC
&& bundle_num
!= BINK_SRC_INTER_DC
)
252 read_tree(gb
, &c
->bundle
[bundle_num
].tree
);
253 c
->bundle
[bundle_num
].cur_dec
=
254 c
->bundle
[bundle_num
].cur_ptr
= c
->bundle
[bundle_num
].data
;
258 * common check before starting decoding bundle data
260 * @param gb context for reading bits
262 * @param t variable where number of elements to decode will be stored
264 #define CHECK_READ_VAL(gb, b, t) \
265 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
267 t = get_bits(gb, b->len); \
273 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
276 const uint8_t *dec_end
;
278 CHECK_READ_VAL(gb
, b
, t
);
279 dec_end
= b
->cur_dec
+ t
;
280 if (dec_end
> b
->data_end
) {
281 av_log(avctx
, AV_LOG_ERROR
, "Run value went out of bounds\n");
286 memset(b
->cur_dec
, v
, t
);
289 while (b
->cur_dec
< dec_end
)
290 *b
->cur_dec
++ = GET_HUFF(gb
, b
->tree
);
295 static int read_motion_values(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
)
298 const uint8_t *dec_end
;
300 CHECK_READ_VAL(gb
, b
, t
);
301 dec_end
= b
->cur_dec
+ t
;
302 if (dec_end
> b
->data_end
) {
303 av_log(avctx
, AV_LOG_ERROR
, "Too many motion values\n");
309 sign
= -get_bits1(gb
);
310 v
= (v
^ sign
) - sign
;
312 memset(b
->cur_dec
, v
, t
);
316 v
= GET_HUFF(gb
, b
->tree
);
318 sign
= -get_bits1(gb
);
319 v
= (v
^ sign
) - sign
;
322 } while (b
->cur_dec
< dec_end
);
327 const uint8_t bink_rlelens
[4] = { 4, 8, 12, 32 };
329 static int read_block_types(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
)
333 const uint8_t *dec_end
;
335 CHECK_READ_VAL(gb
, b
, t
);
336 dec_end
= b
->cur_dec
+ t
;
337 if (dec_end
> b
->data_end
) {
338 av_log(avctx
, AV_LOG_ERROR
, "Too many block type values\n");
343 memset(b
->cur_dec
, v
, t
);
347 v
= GET_HUFF(gb
, b
->tree
);
352 int run
= bink_rlelens
[v
- 12];
354 memset(b
->cur_dec
, last
, run
);
357 } while (b
->cur_dec
< dec_end
);
362 static int read_patterns(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 pattern values\n");
373 while (b
->cur_dec
< dec_end
) {
374 v
= GET_HUFF(gb
, b
->tree
);
375 v
|= GET_HUFF(gb
, b
->tree
) << 4;
382 static int read_colors(GetBitContext
*gb
, Bundle
*b
, BinkContext
*c
)
385 const uint8_t *dec_end
;
387 CHECK_READ_VAL(gb
, b
, t
);
388 dec_end
= b
->cur_dec
+ t
;
389 if (dec_end
> b
->data_end
) {
390 av_log(c
->avctx
, AV_LOG_ERROR
, "Too many color values\n");
394 c
->col_lastval
= GET_HUFF(gb
, c
->col_high
[c
->col_lastval
]);
395 v
= GET_HUFF(gb
, b
->tree
);
396 v
= (c
->col_lastval
<< 4) | v
;
397 if (c
->version
< 'i') {
398 sign
= ((int8_t) v
) >> 7;
399 v
= ((v
& 0x7F) ^ sign
) - sign
;
402 memset(b
->cur_dec
, v
, t
);
405 while (b
->cur_dec
< dec_end
) {
406 c
->col_lastval
= GET_HUFF(gb
, c
->col_high
[c
->col_lastval
]);
407 v
= GET_HUFF(gb
, b
->tree
);
408 v
= (c
->col_lastval
<< 4) | v
;
409 if (c
->version
< 'i') {
410 sign
= ((int8_t) v
) >> 7;
411 v
= ((v
& 0x7F) ^ sign
) - sign
;
420 /** number of bits used to store first DC value in bundle */
421 #define DC_START_BITS 11
423 static int read_dcs(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
,
424 int start_bits
, int has_sign
)
426 int i
, j
, len
, len2
, bsize
, sign
, v
, v2
;
427 int16_t *dst
= (int16_t*)b
->cur_dec
;
429 CHECK_READ_VAL(gb
, b
, len
);
430 v
= get_bits(gb
, start_bits
- has_sign
);
432 sign
= -get_bits1(gb
);
433 v
= (v
^ sign
) - sign
;
437 for (i
= 0; i
< len
; i
+= 8) {
438 len2
= FFMIN(len
- i
, 8);
439 bsize
= get_bits(gb
, 4);
441 for (j
= 0; j
< len2
; j
++) {
442 v2
= get_bits(gb
, bsize
);
444 sign
= -get_bits1(gb
);
445 v2
= (v2
^ sign
) - sign
;
449 if (v
< -32768 || v
> 32767) {
450 av_log(avctx
, AV_LOG_ERROR
, "DC value went out of bounds: %d\n", v
);
455 for (j
= 0; j
< len2
; j
++)
460 b
->cur_dec
= (uint8_t*)dst
;
465 * Retrieves next value from bundle.
467 * @param c decoder context
468 * @param bundle bundle number
470 static inline int get_value(BinkContext
*c
, int bundle
)
474 if (bundle
< BINK_SRC_X_OFF
|| bundle
== BINK_SRC_RUN
)
475 return *c
->bundle
[bundle
].cur_ptr
++;
476 if (bundle
== BINK_SRC_X_OFF
|| bundle
== BINK_SRC_Y_OFF
)
477 return (int8_t)*c
->bundle
[bundle
].cur_ptr
++;
478 ret
= *(int16_t*)c
->bundle
[bundle
].cur_ptr
;
479 c
->bundle
[bundle
].cur_ptr
+= 2;
484 * Reads 8x8 block of DCT coefficients.
486 * @param gb context for reading bits
487 * @param block place for storing coefficients
488 * @param scan scan order table
489 * @param is_intra tells what set of quantizer matrices to use
490 * @return 0 for success, negative value in other cases
492 static int read_dct_coeffs(GetBitContext
*gb
, DCTELEM block
[64], const uint8_t *scan
,
497 int i
, t
, mask
, bits
, ccoef
, mode
, sign
;
498 int list_start
= 64, list_end
= 64, list_pos
;
502 const uint32_t *quant
;
504 coef_list
[list_end
] = 4; mode_list
[list_end
++] = 0;
505 coef_list
[list_end
] = 24; mode_list
[list_end
++] = 0;
506 coef_list
[list_end
] = 44; mode_list
[list_end
++] = 0;
507 coef_list
[list_end
] = 1; mode_list
[list_end
++] = 3;
508 coef_list
[list_end
] = 2; mode_list
[list_end
++] = 3;
509 coef_list
[list_end
] = 3; mode_list
[list_end
++] = 3;
511 bits
= get_bits(gb
, 4) - 1;
512 for (mask
= 1 << bits
; bits
>= 0; mask
>>= 1, bits
--) {
513 list_pos
= list_start
;
514 while (list_pos
< list_end
) {
515 if (!(mode_list
[list_pos
] | coef_list
[list_pos
]) || !get_bits1(gb
)) {
519 ccoef
= coef_list
[list_pos
];
520 mode
= mode_list
[list_pos
];
523 coef_list
[list_pos
] = ccoef
+ 4;
524 mode_list
[list_pos
] = 1;
527 coef_list
[list_pos
] = 0;
528 mode_list
[list_pos
++] = 0;
530 for (i
= 0; i
< 4; i
++, ccoef
++) {
532 coef_list
[--list_start
] = ccoef
;
533 mode_list
[ list_start
] = 3;
537 t
= 1 - (get_bits1(gb
) << 1);
539 t
= get_bits(gb
, bits
) | mask
;
540 sign
= -get_bits1(gb
);
541 t
= (t
^ sign
) - sign
;
543 block
[scan
[ccoef
]] = t
;
544 coef_idx
[coef_count
++] = ccoef
;
549 mode_list
[list_pos
] = 2;
550 for (i
= 0; i
< 3; i
++) {
552 coef_list
[list_end
] = ccoef
;
553 mode_list
[list_end
++] = 2;
558 t
= 1 - (get_bits1(gb
) << 1);
560 t
= get_bits(gb
, bits
) | mask
;
561 sign
= -get_bits1(gb
);
562 t
= (t
^ sign
) - sign
;
564 block
[scan
[ccoef
]] = t
;
565 coef_idx
[coef_count
++] = ccoef
;
566 coef_list
[list_pos
] = 0;
567 mode_list
[list_pos
++] = 0;
573 quant_idx
= get_bits(gb
, 4);
574 quant
= is_intra ? bink_intra_quant
[quant_idx
]
575 : bink_inter_quant
[quant_idx
];
576 block
[0] = (block
[0] * quant
[0]) >> 11;
577 for (i
= 0; i
< coef_count
; i
++) {
578 int idx
= coef_idx
[i
];
579 block
[scan
[idx
]] = (block
[scan
[idx
]] * quant
[idx
]) >> 11;
586 * Reads 8x8 block with residue after motion compensation.
588 * @param gb context for reading bits
589 * @param block place to store read data
590 * @param masks_count number of masks to decode
591 * @return 0 on success, negative value in other cases
593 static int read_residue(GetBitContext
*gb
, DCTELEM block
[64], int masks_count
)
597 int i
, sign
, mask
, ccoef
, mode
;
598 int list_start
= 64, list_end
= 64, list_pos
;
600 int nz_coeff_count
= 0;
602 coef_list
[list_end
] = 4; mode_list
[list_end
++] = 0;
603 coef_list
[list_end
] = 24; mode_list
[list_end
++] = 0;
604 coef_list
[list_end
] = 44; mode_list
[list_end
++] = 0;
605 coef_list
[list_end
] = 0; mode_list
[list_end
++] = 2;
607 for (mask
= 1 << get_bits(gb
, 3); mask
; mask
>>= 1) {
608 for (i
= 0; i
< nz_coeff_count
; i
++) {
611 if (block
[nz_coeff
[i
]] < 0)
612 block
[nz_coeff
[i
]] -= mask
;
614 block
[nz_coeff
[i
]] += mask
;
619 list_pos
= list_start
;
620 while (list_pos
< list_end
) {
621 if (!(coef_list
[list_pos
] | mode_list
[list_pos
]) || !get_bits1(gb
)) {
625 ccoef
= coef_list
[list_pos
];
626 mode
= mode_list
[list_pos
];
629 coef_list
[list_pos
] = ccoef
+ 4;
630 mode_list
[list_pos
] = 1;
633 coef_list
[list_pos
] = 0;
634 mode_list
[list_pos
++] = 0;
636 for (i
= 0; i
< 4; i
++, ccoef
++) {
638 coef_list
[--list_start
] = ccoef
;
639 mode_list
[ list_start
] = 3;
641 nz_coeff
[nz_coeff_count
++] = bink_scan
[ccoef
];
642 sign
= -get_bits1(gb
);
643 block
[bink_scan
[ccoef
]] = (mask
^ sign
) - sign
;
651 mode_list
[list_pos
] = 2;
652 for (i
= 0; i
< 3; i
++) {
654 coef_list
[list_end
] = ccoef
;
655 mode_list
[list_end
++] = 2;
659 nz_coeff
[nz_coeff_count
++] = bink_scan
[ccoef
];
660 sign
= -get_bits1(gb
);
661 block
[bink_scan
[ccoef
]] = (mask
^ sign
) - sign
;
662 coef_list
[list_pos
] = 0;
663 mode_list
[list_pos
++] = 0;
675 static int bink_decode_plane(BinkContext
*c
, GetBitContext
*gb
, int plane_idx
,
680 uint8_t *dst
, *prev
, *ref
, *ref_start
, *ref_end
;
684 DECLARE_ALIGNED(16, DCTELEM
, block
[64]);
685 DECLARE_ALIGNED(16, uint8_t, ublock
[64]);
688 const int stride
= c
->pic
.linesize
[plane_idx
];
689 int bw
= is_chroma ?
(c
->avctx
->width
+ 15) >> 4 : (c
->avctx
->width
+ 7) >> 3;
690 int bh
= is_chroma ?
(c
->avctx
->height
+ 15) >> 4 : (c
->avctx
->height
+ 7) >> 3;
691 int width
= c
->avctx
->width
>> is_chroma
;
693 init_lengths(c
, FFMAX(width
, 8), bw
);
694 for (i
= 0; i
< BINK_NB_SRC
; i
++)
695 read_bundle(gb
, c
, i
);
697 ref_start
= c
->last
.data
[plane_idx
];
698 ref_end
= c
->last
.data
[plane_idx
]
699 + (bw
- 1 + c
->last
.linesize
[plane_idx
] * (bh
- 1)) * 8;
701 for (i
= 0; i
< 64; i
++)
702 coordmap
[i
] = (i
& 7) + (i
>> 3) * stride
;
704 for (by
= 0; by
< bh
; by
++) {
705 if (read_block_types(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_BLOCK_TYPES
]) < 0)
707 if (read_block_types(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_SUB_BLOCK_TYPES
]) < 0)
709 if (read_colors(gb
, &c
->bundle
[BINK_SRC_COLORS
], c
) < 0)
711 if (read_patterns(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_PATTERN
]) < 0)
713 if (read_motion_values(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_X_OFF
]) < 0)
715 if (read_motion_values(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_Y_OFF
]) < 0)
717 if (read_dcs(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_INTRA_DC
], DC_START_BITS
, 0) < 0)
719 if (read_dcs(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_INTER_DC
], DC_START_BITS
, 1) < 0)
721 if (read_runs(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_RUN
]) < 0)
726 dst
= c
->pic
.data
[plane_idx
] + 8*by
*stride
;
727 prev
= c
->last
.data
[plane_idx
] + 8*by
*stride
;
728 for (bx
= 0; bx
< bw
; bx
++, dst
+= 8, prev
+= 8) {
729 blk
= get_value(c
, BINK_SRC_BLOCK_TYPES
);
730 // 16x16 block type on odd line means part of the already decoded block, so skip it
731 if ((by
& 1) && blk
== SCALED_BLOCK
) {
739 c
->dsp
.put_pixels_tab
[1][0](dst
, prev
, stride
, 8);
742 blk
= get_value(c
, BINK_SRC_SUB_BLOCK_TYPES
);
745 scan
= bink_patterns
[get_bits(gb
, 4)];
748 int run
= get_value(c
, BINK_SRC_RUN
) + 1;
752 av_log(c
->avctx
, AV_LOG_ERROR
, "Run went out of bounds\n");
756 v
= get_value(c
, BINK_SRC_COLORS
);
757 for (j
= 0; j
< run
; j
++)
760 for (j
= 0; j
< run
; j
++)
761 ublock
[*scan
++] = get_value(c
, BINK_SRC_COLORS
);
765 ublock
[*scan
++] = get_value(c
, BINK_SRC_COLORS
);
768 c
->dsp
.clear_block(block
);
769 block
[0] = get_value(c
, BINK_SRC_INTRA_DC
);
770 read_dct_coeffs(gb
, block
, c
->scantable
.permutated
, 1);
772 c
->dsp
.put_pixels_nonclamped(block
, ublock
, 8);
775 v
= get_value(c
, BINK_SRC_COLORS
);
776 c
->dsp
.fill_block_tab
[0](dst
, v
, stride
, 16);
779 for (i
= 0; i
< 2; i
++)
780 col
[i
] = get_value(c
, BINK_SRC_COLORS
);
781 for (j
= 0; j
< 8; j
++) {
782 v
= get_value(c
, BINK_SRC_PATTERN
);
783 for (i
= 0; i
< 8; i
++, v
>>= 1)
784 ublock
[i
+ j
*8] = col
[v
& 1];
788 for (j
= 0; j
< 8; j
++)
789 for (i
= 0; i
< 8; i
++)
790 ublock
[i
+ j
*8] = get_value(c
, BINK_SRC_COLORS
);
793 av_log(c
->avctx
, AV_LOG_ERROR
, "Incorrect 16x16 block type %d\n", blk
);
796 if (blk
!= FILL_BLOCK
)
797 c
->dsp
.scale_block(ublock
, dst
, stride
);
803 xoff
= get_value(c
, BINK_SRC_X_OFF
);
804 yoff
= get_value(c
, BINK_SRC_Y_OFF
);
805 ref
= prev
+ xoff
+ yoff
* stride
;
806 if (ref
< ref_start
|| ref
> ref_end
) {
807 av_log(c
->avctx
, AV_LOG_ERROR
, "Copy out of bounds @%d, %d\n",
808 bx
*8 + xoff
, by
*8 + yoff
);
811 c
->dsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
814 scan
= bink_patterns
[get_bits(gb
, 4)];
817 int run
= get_value(c
, BINK_SRC_RUN
) + 1;
821 av_log(c
->avctx
, AV_LOG_ERROR
, "Run went out of bounds\n");
825 v
= get_value(c
, BINK_SRC_COLORS
);
826 for (j
= 0; j
< run
; j
++)
827 dst
[coordmap
[*scan
++]] = v
;
829 for (j
= 0; j
< run
; j
++)
830 dst
[coordmap
[*scan
++]] = get_value(c
, BINK_SRC_COLORS
);
834 dst
[coordmap
[*scan
++]] = get_value(c
, BINK_SRC_COLORS
);
837 xoff
= get_value(c
, BINK_SRC_X_OFF
);
838 yoff
= get_value(c
, BINK_SRC_Y_OFF
);
839 ref
= prev
+ xoff
+ yoff
* stride
;
840 if (ref
< ref_start
|| ref
> ref_end
) {
841 av_log(c
->avctx
, AV_LOG_ERROR
, "Copy out of bounds @%d, %d\n",
842 bx
*8 + xoff
, by
*8 + yoff
);
845 c
->dsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
846 c
->dsp
.clear_block(block
);
848 read_residue(gb
, block
, v
);
849 c
->dsp
.add_pixels8(dst
, block
, stride
);
852 c
->dsp
.clear_block(block
);
853 block
[0] = get_value(c
, BINK_SRC_INTRA_DC
);
854 read_dct_coeffs(gb
, block
, c
->scantable
.permutated
, 1);
855 c
->dsp
.idct_put(dst
, stride
, block
);
858 v
= get_value(c
, BINK_SRC_COLORS
);
859 c
->dsp
.fill_block_tab
[1](dst
, v
, stride
, 8);
862 xoff
= get_value(c
, BINK_SRC_X_OFF
);
863 yoff
= get_value(c
, BINK_SRC_Y_OFF
);
864 ref
= prev
+ xoff
+ yoff
* stride
;
865 c
->dsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
866 c
->dsp
.clear_block(block
);
867 block
[0] = get_value(c
, BINK_SRC_INTER_DC
);
868 read_dct_coeffs(gb
, block
, c
->scantable
.permutated
, 0);
869 c
->dsp
.idct_add(dst
, stride
, block
);
872 for (i
= 0; i
< 2; i
++)
873 col
[i
] = get_value(c
, BINK_SRC_COLORS
);
874 for (i
= 0; i
< 8; i
++) {
875 v
= get_value(c
, BINK_SRC_PATTERN
);
876 for (j
= 0; j
< 8; j
++, v
>>= 1)
877 dst
[i
*stride
+ j
] = col
[v
& 1];
881 for (i
= 0; i
< 8; i
++)
882 memcpy(dst
+ i
*stride
, c
->bundle
[BINK_SRC_COLORS
].cur_ptr
+ i
*8, 8);
883 c
->bundle
[BINK_SRC_COLORS
].cur_ptr
+= 64;
886 av_log(c
->avctx
, AV_LOG_ERROR
, "Unknown block type %d\n", blk
);
891 if (get_bits_count(gb
) & 0x1F) //next plane data starts at 32-bit boundary
892 skip_bits_long(gb
, 32 - (get_bits_count(gb
) & 0x1F));
897 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *data_size
, AVPacket
*pkt
)
899 BinkContext
* const c
= avctx
->priv_data
;
901 int plane
, plane_idx
;
902 int bits_count
= pkt
->size
<< 3;
905 avctx
->release_buffer(avctx
, &c
->pic
);
907 if(avctx
->get_buffer(avctx
, &c
->pic
) < 0){
908 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
912 init_get_bits(&gb
, pkt
->data
, bits_count
);
914 if (c
->version
>= 'i')
915 skip_bits_long(&gb
, 32);
916 if (bink_decode_plane(c
, &gb
, 3, 0) < 0)
919 if (c
->version
>= 'i')
920 skip_bits_long(&gb
, 32);
922 for (plane
= 0; plane
< 3; plane
++) {
923 plane_idx
= (!plane
|| !c
->swap_planes
) ? plane
: (plane
^ 3);
925 if (bink_decode_plane(c
, &gb
, plane_idx
, !!plane
) < 0)
927 if (get_bits_count(&gb
) >= bits_count
)
932 *data_size
= sizeof(AVFrame
);
933 *(AVFrame
*)data
= c
->pic
;
935 FFSWAP(AVFrame
, c
->pic
, c
->last
);
937 /* always report that the buffer was completely consumed */
941 static av_cold
int decode_init(AVCodecContext
*avctx
)
943 BinkContext
* const c
= avctx
->priv_data
;
944 static VLC_TYPE table
[16 * 128][2];
948 c
->version
= avctx
->codec_tag
>> 24;
949 if (c
->version
< 'c') {
950 av_log(avctx
, AV_LOG_ERROR
, "Too old version '%c'\n", c
->version
);
953 if (avctx
->extradata_size
< 4) {
954 av_log(avctx
, AV_LOG_ERROR
, "Extradata missing or too short\n");
957 flags
= AV_RL32(avctx
->extradata
);
958 c
->has_alpha
= flags
& BINK_FLAG_ALPHA
;
959 c
->swap_planes
= c
->version
>= 'h';
960 if (!bink_trees
[15].table
) {
961 for (i
= 0; i
< 16; i
++) {
962 const int maxbits
= bink_tree_lens
[i
][15];
963 bink_trees
[i
].table
= table
+ i
*128;
964 bink_trees
[i
].table_allocated
= 1 << maxbits
;
965 init_vlc(&bink_trees
[i
], maxbits
, 16,
966 bink_tree_lens
[i
], 1, 1,
967 bink_tree_bits
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
972 c
->pic
.data
[0] = NULL
;
974 if (avcodec_check_dimensions(avctx
, avctx
->width
, avctx
->height
) < 0) {
978 avctx
->pix_fmt
= c
->has_alpha ? PIX_FMT_YUVA420P
: PIX_FMT_YUV420P
;
980 avctx
->idct_algo
= FF_IDCT_BINK
;
981 dsputil_init(&c
->dsp
, avctx
);
982 ff_init_scantable(c
->dsp
.idct_permutation
, &c
->scantable
, bink_scan
);
989 static av_cold
int decode_end(AVCodecContext
*avctx
)
991 BinkContext
* const c
= avctx
->priv_data
;
994 avctx
->release_buffer(avctx
, &c
->pic
);
996 avctx
->release_buffer(avctx
, &c
->last
);
1002 AVCodec bink_decoder
= {
1006 sizeof(BinkContext
),
1011 .long_name
= NULL_IF_CONFIG_SMALL("Bink video"),