3 * Copyright (c) 2009 Konstantin Shishkov
4 * Copyright (C) 2011 Peter Ross <pross@xvid.org>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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"
31 #define BITSTREAM_READER_LE
34 #define BINK_FLAG_ALPHA 0x00100000
35 #define BINK_FLAG_GRAY 0x00020000
37 static VLC bink_trees
[16];
40 * IDs for different data types used in old version of Bink video codec
43 BINKB_SRC_BLOCK_TYPES
= 0, ///< 8x8 block types
44 BINKB_SRC_COLORS
, ///< pixel values used for different block types
45 BINKB_SRC_PATTERN
, ///< 8-bit values for 2-colour pattern fill
46 BINKB_SRC_X_OFF
, ///< X components of motion value
47 BINKB_SRC_Y_OFF
, ///< Y components of motion value
48 BINKB_SRC_INTRA_DC
, ///< DC values for intrablocks with DCT
49 BINKB_SRC_INTER_DC
, ///< DC values for interblocks with DCT
50 BINKB_SRC_INTRA_Q
, ///< quantizer values for intrablocks with DCT
51 BINKB_SRC_INTER_Q
, ///< quantizer values for interblocks with DCT
52 BINKB_SRC_INTER_COEFS
, ///< number of coefficients for residue blocks
57 static const int binkb_bundle_sizes
[BINKB_NB_SRC
] = {
58 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
61 static const int binkb_bundle_signed
[BINKB_NB_SRC
] = {
62 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
65 static int32_t binkb_intra_quant
[16][64];
66 static int32_t binkb_inter_quant
[16][64];
69 * IDs for different data types used in Bink video codec
72 BINK_SRC_BLOCK_TYPES
= 0, ///< 8x8 block types
73 BINK_SRC_SUB_BLOCK_TYPES
, ///< 16x16 block types (a subset of 8x8 block types)
74 BINK_SRC_COLORS
, ///< pixel values used for different block types
75 BINK_SRC_PATTERN
, ///< 8-bit values for 2-colour pattern fill
76 BINK_SRC_X_OFF
, ///< X components of motion value
77 BINK_SRC_Y_OFF
, ///< Y components of motion value
78 BINK_SRC_INTRA_DC
, ///< DC values for intrablocks with DCT
79 BINK_SRC_INTER_DC
, ///< DC values for interblocks with DCT
80 BINK_SRC_RUN
, ///< run lengths for special fill block
86 * data needed to decode 4-bit Huffman-coded value
89 int vlc_num
; ///< tree number (in bink_trees[])
90 uint8_t syms
[16]; ///< leaf value to symbol mapping
93 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
94 bink_trees[(tree).vlc_num].bits, 1)]
97 * data structure used for decoding single Bink data type
99 typedef struct Bundle
{
100 int len
; ///< length of number of entries to decode (in bits)
101 Tree tree
; ///< Huffman tree-related data
102 uint8_t *data
; ///< buffer for decoded symbols
103 uint8_t *data_end
; ///< buffer end
104 uint8_t *cur_dec
; ///< pointer to the not yet decoded part of the buffer
105 uint8_t *cur_ptr
; ///< pointer to the data that is not read from buffer yet
111 typedef struct BinkContext
{
112 AVCodecContext
*avctx
;
116 int version
; ///< internal Bink file version
120 Bundle bundle
[BINKB_NB_SRC
]; ///< bundles for decoding all data types
121 Tree col_high
[16]; ///< trees for decoding high nibble in "colours" data type
122 int col_lastval
; ///< value of last decoded high nibble in "colours" data type
126 * Bink video block types
129 SKIP_BLOCK
= 0, ///< skipped block
130 SCALED_BLOCK
, ///< block has size 16x16
131 MOTION_BLOCK
, ///< block is copied from previous frame with some offset
132 RUN_BLOCK
, ///< block is composed from runs of colours with custom scan order
133 RESIDUE_BLOCK
, ///< motion block with some difference added
134 INTRA_BLOCK
, ///< intra DCT block
135 FILL_BLOCK
, ///< block is filled with single colour
136 INTER_BLOCK
, ///< motion block with DCT applied to the difference
137 PATTERN_BLOCK
, ///< block is filled with two colours following custom pattern
138 RAW_BLOCK
, ///< uncoded 8x8 block
142 * Initialize length length in all bundles.
144 * @param c decoder context
145 * @param width plane width
146 * @param bw plane width in 8x8 blocks
148 static void init_lengths(BinkContext
*c
, int width
, int bw
)
150 width
= FFALIGN(width
, 8);
152 c
->bundle
[BINK_SRC_BLOCK_TYPES
].len
= av_log2((width
>> 3) + 511) + 1;
154 c
->bundle
[BINK_SRC_SUB_BLOCK_TYPES
].len
= av_log2((width
>> 4) + 511) + 1;
156 c
->bundle
[BINK_SRC_COLORS
].len
= av_log2(bw
*64 + 511) + 1;
158 c
->bundle
[BINK_SRC_INTRA_DC
].len
=
159 c
->bundle
[BINK_SRC_INTER_DC
].len
=
160 c
->bundle
[BINK_SRC_X_OFF
].len
=
161 c
->bundle
[BINK_SRC_Y_OFF
].len
= av_log2((width
>> 3) + 511) + 1;
163 c
->bundle
[BINK_SRC_PATTERN
].len
= av_log2((bw
<< 3) + 511) + 1;
165 c
->bundle
[BINK_SRC_RUN
].len
= av_log2(bw
*48 + 511) + 1;
169 * Allocate memory for bundles.
171 * @param c decoder context
173 static av_cold
void init_bundles(BinkContext
*c
)
178 bw
= (c
->avctx
->width
+ 7) >> 3;
179 bh
= (c
->avctx
->height
+ 7) >> 3;
182 for (i
= 0; i
< BINKB_NB_SRC
; i
++) {
183 c
->bundle
[i
].data
= av_malloc(blocks
* 64);
184 c
->bundle
[i
].data_end
= c
->bundle
[i
].data
+ blocks
* 64;
189 * Free memory used by bundles.
191 * @param c decoder context
193 static av_cold
void free_bundles(BinkContext
*c
)
196 for (i
= 0; i
< BINKB_NB_SRC
; i
++)
197 av_freep(&c
->bundle
[i
].data
);
201 * Merge two consequent lists of equal size depending on bits read.
203 * @param gb context for reading bits
204 * @param dst buffer where merged list will be written to
205 * @param src pointer to the head of the first list (the second lists starts at src+size)
206 * @param size input lists size
208 static void merge(GetBitContext
*gb
, uint8_t *dst
, uint8_t *src
, int size
)
210 uint8_t *src2
= src
+ size
;
214 if (!get_bits1(gb
)) {
221 } while (size
&& size2
);
230 * Read information about Huffman tree used to decode data.
232 * @param gb context for reading bits
233 * @param tree pointer for storing tree data
235 static void read_tree(GetBitContext
*gb
, Tree
*tree
)
237 uint8_t tmp1
[16] = { 0 }, tmp2
[16], *in
= tmp1
, *out
= tmp2
;
240 tree
->vlc_num
= get_bits(gb
, 4);
241 if (!tree
->vlc_num
) {
242 for (i
= 0; i
< 16; i
++)
247 len
= get_bits(gb
, 3);
248 for (i
= 0; i
<= len
; i
++) {
249 tree
->syms
[i
] = get_bits(gb
, 4);
250 tmp1
[tree
->syms
[i
]] = 1;
252 for (i
= 0; i
< 16 && len
< 16 - 1; i
++)
254 tree
->syms
[++len
] = i
;
256 len
= get_bits(gb
, 2);
257 for (i
= 0; i
< 16; i
++)
259 for (i
= 0; i
<= len
; i
++) {
261 for (t
= 0; t
< 16; t
+= size
<< 1)
262 merge(gb
, out
+ t
, in
+ t
, size
);
263 FFSWAP(uint8_t*, in
, out
);
265 memcpy(tree
->syms
, in
, 16);
270 * Prepare bundle for decoding data.
272 * @param gb context for reading bits
273 * @param c decoder context
274 * @param bundle_num number of the bundle to initialize
276 static void read_bundle(GetBitContext
*gb
, BinkContext
*c
, int bundle_num
)
280 if (bundle_num
== BINK_SRC_COLORS
) {
281 for (i
= 0; i
< 16; i
++)
282 read_tree(gb
, &c
->col_high
[i
]);
285 if (bundle_num
!= BINK_SRC_INTRA_DC
&& bundle_num
!= BINK_SRC_INTER_DC
)
286 read_tree(gb
, &c
->bundle
[bundle_num
].tree
);
287 c
->bundle
[bundle_num
].cur_dec
=
288 c
->bundle
[bundle_num
].cur_ptr
= c
->bundle
[bundle_num
].data
;
292 * common check before starting decoding bundle data
294 * @param gb context for reading bits
296 * @param t variable where number of elements to decode will be stored
298 #define CHECK_READ_VAL(gb, b, t) \
299 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
301 t = get_bits(gb, b->len); \
307 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
310 const uint8_t *dec_end
;
312 CHECK_READ_VAL(gb
, b
, t
);
313 dec_end
= b
->cur_dec
+ t
;
314 if (dec_end
> b
->data_end
) {
315 av_log(avctx
, AV_LOG_ERROR
, "Run value went out of bounds\n");
320 memset(b
->cur_dec
, v
, t
);
323 while (b
->cur_dec
< dec_end
)
324 *b
->cur_dec
++ = GET_HUFF(gb
, b
->tree
);
329 static int read_motion_values(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
)
332 const uint8_t *dec_end
;
334 CHECK_READ_VAL(gb
, b
, t
);
335 dec_end
= b
->cur_dec
+ t
;
336 if (dec_end
> b
->data_end
) {
337 av_log(avctx
, AV_LOG_ERROR
, "Too many motion values\n");
343 sign
= -get_bits1(gb
);
344 v
= (v
^ sign
) - sign
;
346 memset(b
->cur_dec
, v
, t
);
349 while (b
->cur_dec
< dec_end
) {
350 v
= GET_HUFF(gb
, b
->tree
);
352 sign
= -get_bits1(gb
);
353 v
= (v
^ sign
) - sign
;
361 static const uint8_t bink_rlelens
[4] = { 4, 8, 12, 32 };
363 static int read_block_types(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
)
367 const uint8_t *dec_end
;
369 CHECK_READ_VAL(gb
, b
, t
);
370 dec_end
= b
->cur_dec
+ t
;
371 if (dec_end
> b
->data_end
) {
372 av_log(avctx
, AV_LOG_ERROR
, "Too many block type values\n");
377 memset(b
->cur_dec
, v
, t
);
380 while (b
->cur_dec
< dec_end
) {
381 v
= GET_HUFF(gb
, b
->tree
);
386 int run
= bink_rlelens
[v
- 12];
388 if (dec_end
- b
->cur_dec
< run
)
390 memset(b
->cur_dec
, last
, run
);
398 static int read_patterns(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
)
401 const uint8_t *dec_end
;
403 CHECK_READ_VAL(gb
, b
, t
);
404 dec_end
= b
->cur_dec
+ t
;
405 if (dec_end
> b
->data_end
) {
406 av_log(avctx
, AV_LOG_ERROR
, "Too many pattern values\n");
409 while (b
->cur_dec
< dec_end
) {
410 v
= GET_HUFF(gb
, b
->tree
);
411 v
|= GET_HUFF(gb
, b
->tree
) << 4;
418 static int read_colors(GetBitContext
*gb
, Bundle
*b
, BinkContext
*c
)
421 const uint8_t *dec_end
;
423 CHECK_READ_VAL(gb
, b
, t
);
424 dec_end
= b
->cur_dec
+ t
;
425 if (dec_end
> b
->data_end
) {
426 av_log(c
->avctx
, AV_LOG_ERROR
, "Too many color values\n");
430 c
->col_lastval
= GET_HUFF(gb
, c
->col_high
[c
->col_lastval
]);
431 v
= GET_HUFF(gb
, b
->tree
);
432 v
= (c
->col_lastval
<< 4) | v
;
433 if (c
->version
< 'i') {
434 sign
= ((int8_t) v
) >> 7;
435 v
= ((v
& 0x7F) ^ sign
) - sign
;
438 memset(b
->cur_dec
, v
, t
);
441 while (b
->cur_dec
< dec_end
) {
442 c
->col_lastval
= GET_HUFF(gb
, c
->col_high
[c
->col_lastval
]);
443 v
= GET_HUFF(gb
, b
->tree
);
444 v
= (c
->col_lastval
<< 4) | v
;
445 if (c
->version
< 'i') {
446 sign
= ((int8_t) v
) >> 7;
447 v
= ((v
& 0x7F) ^ sign
) - sign
;
456 /** number of bits used to store first DC value in bundle */
457 #define DC_START_BITS 11
459 static int read_dcs(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
,
460 int start_bits
, int has_sign
)
462 int i
, j
, len
, len2
, bsize
, sign
, v
, v2
;
463 int16_t *dst
= (int16_t*)b
->cur_dec
;
464 int16_t *dst_end
= (int16_t*)b
->data_end
;
466 CHECK_READ_VAL(gb
, b
, len
);
467 v
= get_bits(gb
, start_bits
- has_sign
);
469 sign
= -get_bits1(gb
);
470 v
= (v
^ sign
) - sign
;
472 if (dst_end
- dst
< 1)
476 for (i
= 0; i
< len
; i
+= 8) {
477 len2
= FFMIN(len
- i
, 8);
478 if (dst_end
- dst
< len2
)
480 bsize
= get_bits(gb
, 4);
482 for (j
= 0; j
< len2
; j
++) {
483 v2
= get_bits(gb
, bsize
);
485 sign
= -get_bits1(gb
);
486 v2
= (v2
^ sign
) - sign
;
490 if (v
< -32768 || v
> 32767) {
491 av_log(avctx
, AV_LOG_ERROR
, "DC value went out of bounds: %d\n", v
);
496 for (j
= 0; j
< len2
; j
++)
501 b
->cur_dec
= (uint8_t*)dst
;
506 * Retrieve next value from bundle.
508 * @param c decoder context
509 * @param bundle bundle number
511 static inline int get_value(BinkContext
*c
, int bundle
)
515 if (bundle
< BINK_SRC_X_OFF
|| bundle
== BINK_SRC_RUN
)
516 return *c
->bundle
[bundle
].cur_ptr
++;
517 if (bundle
== BINK_SRC_X_OFF
|| bundle
== BINK_SRC_Y_OFF
)
518 return (int8_t)*c
->bundle
[bundle
].cur_ptr
++;
519 ret
= *(int16_t*)c
->bundle
[bundle
].cur_ptr
;
520 c
->bundle
[bundle
].cur_ptr
+= 2;
524 static void binkb_init_bundle(BinkContext
*c
, int bundle_num
)
526 c
->bundle
[bundle_num
].cur_dec
=
527 c
->bundle
[bundle_num
].cur_ptr
= c
->bundle
[bundle_num
].data
;
528 c
->bundle
[bundle_num
].len
= 13;
531 static void binkb_init_bundles(BinkContext
*c
)
534 for (i
= 0; i
< BINKB_NB_SRC
; i
++)
535 binkb_init_bundle(c
, i
);
538 static int binkb_read_bundle(BinkContext
*c
, GetBitContext
*gb
, int bundle_num
)
540 const int bits
= binkb_bundle_sizes
[bundle_num
];
541 const int mask
= 1 << (bits
- 1);
542 const int issigned
= binkb_bundle_signed
[bundle_num
];
543 Bundle
*b
= &c
->bundle
[bundle_num
];
546 CHECK_READ_VAL(gb
, b
, len
);
547 if (b
->data_end
- b
->cur_dec
< len
* (1 + (bits
> 8)))
551 for (i
= 0; i
< len
; i
++)
552 *b
->cur_dec
++ = get_bits(gb
, bits
);
554 for (i
= 0; i
< len
; i
++)
555 *b
->cur_dec
++ = get_bits(gb
, bits
) - mask
;
558 int16_t *dst
= (int16_t*)b
->cur_dec
;
561 for (i
= 0; i
< len
; i
++)
562 *dst
++ = get_bits(gb
, bits
);
564 for (i
= 0; i
< len
; i
++)
565 *dst
++ = get_bits(gb
, bits
) - mask
;
567 b
->cur_dec
= (uint8_t*)dst
;
572 static inline int binkb_get_value(BinkContext
*c
, int bundle_num
)
575 const int bits
= binkb_bundle_sizes
[bundle_num
];
578 int val
= *c
->bundle
[bundle_num
].cur_ptr
++;
579 return binkb_bundle_signed
[bundle_num
] ?
(int8_t)val
: val
;
581 ret
= *(int16_t*)c
->bundle
[bundle_num
].cur_ptr
;
582 c
->bundle
[bundle_num
].cur_ptr
+= 2;
587 * Read 8x8 block of DCT coefficients.
589 * @param gb context for reading bits
590 * @param block place for storing coefficients
591 * @param scan scan order table
592 * @param quant_matrices quantization matrices
593 * @return 0 for success, negative value in other cases
595 static int read_dct_coeffs(GetBitContext
*gb
, int32_t block
[64], const uint8_t *scan
,
596 const int32_t quant_matrices
[16][64], int q
)
600 int i
, t
, bits
, ccoef
, mode
, sign
;
601 int list_start
= 64, list_end
= 64, list_pos
;
605 const int32_t *quant
;
607 coef_list
[list_end
] = 4; mode_list
[list_end
++] = 0;
608 coef_list
[list_end
] = 24; mode_list
[list_end
++] = 0;
609 coef_list
[list_end
] = 44; mode_list
[list_end
++] = 0;
610 coef_list
[list_end
] = 1; mode_list
[list_end
++] = 3;
611 coef_list
[list_end
] = 2; mode_list
[list_end
++] = 3;
612 coef_list
[list_end
] = 3; mode_list
[list_end
++] = 3;
614 for (bits
= get_bits(gb
, 4) - 1; bits
>= 0; bits
--) {
615 list_pos
= list_start
;
616 while (list_pos
< list_end
) {
617 if (!(mode_list
[list_pos
] | coef_list
[list_pos
]) || !get_bits1(gb
)) {
621 ccoef
= coef_list
[list_pos
];
622 mode
= mode_list
[list_pos
];
625 coef_list
[list_pos
] = ccoef
+ 4;
626 mode_list
[list_pos
] = 1;
629 coef_list
[list_pos
] = 0;
630 mode_list
[list_pos
++] = 0;
632 for (i
= 0; i
< 4; i
++, ccoef
++) {
634 coef_list
[--list_start
] = ccoef
;
635 mode_list
[ list_start
] = 3;
638 t
= 1 - (get_bits1(gb
) << 1);
640 t
= get_bits(gb
, bits
) | 1 << bits
;
641 sign
= -get_bits1(gb
);
642 t
= (t
^ sign
) - sign
;
644 block
[scan
[ccoef
]] = t
;
645 coef_idx
[coef_count
++] = ccoef
;
650 mode_list
[list_pos
] = 2;
651 for (i
= 0; i
< 3; i
++) {
653 coef_list
[list_end
] = ccoef
;
654 mode_list
[list_end
++] = 2;
659 t
= 1 - (get_bits1(gb
) << 1);
661 t
= get_bits(gb
, bits
) | 1 << bits
;
662 sign
= -get_bits1(gb
);
663 t
= (t
^ sign
) - sign
;
665 block
[scan
[ccoef
]] = t
;
666 coef_idx
[coef_count
++] = ccoef
;
667 coef_list
[list_pos
] = 0;
668 mode_list
[list_pos
++] = 0;
675 quant_idx
= get_bits(gb
, 4);
680 quant
= quant_matrices
[quant_idx
];
682 block
[0] = (block
[0] * quant
[0]) >> 11;
683 for (i
= 0; i
< coef_count
; i
++) {
684 int idx
= coef_idx
[i
];
685 block
[scan
[idx
]] = (block
[scan
[idx
]] * quant
[idx
]) >> 11;
692 * Read 8x8 block with residue after motion compensation.
694 * @param gb context for reading bits
695 * @param block place to store read data
696 * @param masks_count number of masks to decode
697 * @return 0 on success, negative value in other cases
699 static int read_residue(GetBitContext
*gb
, DCTELEM block
[64], int masks_count
)
703 int i
, sign
, mask
, ccoef
, mode
;
704 int list_start
= 64, list_end
= 64, list_pos
;
706 int nz_coeff_count
= 0;
708 coef_list
[list_end
] = 4; mode_list
[list_end
++] = 0;
709 coef_list
[list_end
] = 24; mode_list
[list_end
++] = 0;
710 coef_list
[list_end
] = 44; mode_list
[list_end
++] = 0;
711 coef_list
[list_end
] = 0; mode_list
[list_end
++] = 2;
713 for (mask
= 1 << get_bits(gb
, 3); mask
; mask
>>= 1) {
714 for (i
= 0; i
< nz_coeff_count
; i
++) {
717 if (block
[nz_coeff
[i
]] < 0)
718 block
[nz_coeff
[i
]] -= mask
;
720 block
[nz_coeff
[i
]] += mask
;
725 list_pos
= list_start
;
726 while (list_pos
< list_end
) {
727 if (!(coef_list
[list_pos
] | mode_list
[list_pos
]) || !get_bits1(gb
)) {
731 ccoef
= coef_list
[list_pos
];
732 mode
= mode_list
[list_pos
];
735 coef_list
[list_pos
] = ccoef
+ 4;
736 mode_list
[list_pos
] = 1;
739 coef_list
[list_pos
] = 0;
740 mode_list
[list_pos
++] = 0;
742 for (i
= 0; i
< 4; i
++, ccoef
++) {
744 coef_list
[--list_start
] = ccoef
;
745 mode_list
[ list_start
] = 3;
747 nz_coeff
[nz_coeff_count
++] = bink_scan
[ccoef
];
748 sign
= -get_bits1(gb
);
749 block
[bink_scan
[ccoef
]] = (mask
^ sign
) - sign
;
757 mode_list
[list_pos
] = 2;
758 for (i
= 0; i
< 3; i
++) {
760 coef_list
[list_end
] = ccoef
;
761 mode_list
[list_end
++] = 2;
765 nz_coeff
[nz_coeff_count
++] = bink_scan
[ccoef
];
766 sign
= -get_bits1(gb
);
767 block
[bink_scan
[ccoef
]] = (mask
^ sign
) - sign
;
768 coef_list
[list_pos
] = 0;
769 mode_list
[list_pos
++] = 0;
782 * Copy 8x8 block from source to destination, where src and dst may be overlapped
784 static inline void put_pixels8x8_overlapped(uint8_t *dst
, uint8_t *src
, int stride
)
788 for (i
= 0; i
< 8; i
++)
789 memcpy(tmp
+ i
*8, src
+ i
*stride
, 8);
790 for (i
= 0; i
< 8; i
++)
791 memcpy(dst
+ i
*stride
, tmp
+ i
*8, 8);
794 static int binkb_decode_plane(BinkContext
*c
, GetBitContext
*gb
, int plane_idx
,
795 int is_key
, int is_chroma
)
799 uint8_t *dst
, *ref
, *ref_start
, *ref_end
;
803 LOCAL_ALIGNED_16(DCTELEM
, block
, [64]);
804 LOCAL_ALIGNED_16(int32_t, dctblock
, [64]);
806 int ybias
= is_key ?
-15 : 0;
809 const int stride
= c
->pic
.linesize
[plane_idx
];
810 int bw
= is_chroma ?
(c
->avctx
->width
+ 15) >> 4 : (c
->avctx
->width
+ 7) >> 3;
811 int bh
= is_chroma ?
(c
->avctx
->height
+ 15) >> 4 : (c
->avctx
->height
+ 7) >> 3;
813 binkb_init_bundles(c
);
814 ref_start
= c
->pic
.data
[plane_idx
];
815 ref_end
= c
->pic
.data
[plane_idx
] + (bh
* c
->pic
.linesize
[plane_idx
] + bw
) * 8;
817 for (i
= 0; i
< 64; i
++)
818 coordmap
[i
] = (i
& 7) + (i
>> 3) * stride
;
820 for (by
= 0; by
< bh
; by
++) {
821 for (i
= 0; i
< BINKB_NB_SRC
; i
++) {
822 if (binkb_read_bundle(c
, gb
, i
) < 0)
826 dst
= c
->pic
.data
[plane_idx
] + 8*by
*stride
;
827 for (bx
= 0; bx
< bw
; bx
++, dst
+= 8) {
828 blk
= binkb_get_value(c
, BINKB_SRC_BLOCK_TYPES
);
833 scan
= bink_patterns
[get_bits(gb
, 4)];
838 mode
= get_bits1(gb
);
839 run
= get_bits(gb
, binkb_runbits
[i
]) + 1;
843 av_log(c
->avctx
, AV_LOG_ERROR
, "Run went out of bounds\n");
847 v
= binkb_get_value(c
, BINKB_SRC_COLORS
);
848 for (j
= 0; j
< run
; j
++)
849 dst
[coordmap
[*scan
++]] = v
;
851 for (j
= 0; j
< run
; j
++)
852 dst
[coordmap
[*scan
++]] = binkb_get_value(c
, BINKB_SRC_COLORS
);
856 dst
[coordmap
[*scan
++]] = binkb_get_value(c
, BINKB_SRC_COLORS
);
859 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
860 dctblock
[0] = binkb_get_value(c
, BINKB_SRC_INTRA_DC
);
861 qp
= binkb_get_value(c
, BINKB_SRC_INTRA_Q
);
862 read_dct_coeffs(gb
, dctblock
, bink_scan
, binkb_intra_quant
, qp
);
863 c
->bdsp
.idct_put(dst
, stride
, dctblock
);
866 xoff
= binkb_get_value(c
, BINKB_SRC_X_OFF
);
867 yoff
= binkb_get_value(c
, BINKB_SRC_Y_OFF
) + ybias
;
868 ref
= dst
+ xoff
+ yoff
* stride
;
869 if (ref
< ref_start
|| ref
+ 8*stride
> ref_end
) {
870 av_log(c
->avctx
, AV_LOG_WARNING
, "Reference block is out of bounds\n");
871 } else if (ref
+ 8*stride
< dst
|| ref
>= dst
+ 8*stride
) {
872 c
->dsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
874 put_pixels8x8_overlapped(dst
, ref
, stride
);
876 c
->dsp
.clear_block(block
);
877 v
= binkb_get_value(c
, BINKB_SRC_INTER_COEFS
);
878 read_residue(gb
, block
, v
);
879 c
->dsp
.add_pixels8(dst
, block
, stride
);
882 xoff
= binkb_get_value(c
, BINKB_SRC_X_OFF
);
883 yoff
= binkb_get_value(c
, BINKB_SRC_Y_OFF
) + ybias
;
884 ref
= dst
+ xoff
+ yoff
* stride
;
885 if (ref
< ref_start
|| ref
+ 8 * stride
> ref_end
) {
886 av_log(c
->avctx
, AV_LOG_WARNING
, "Reference block is out of bounds\n");
887 } else if (ref
+ 8*stride
< dst
|| ref
>= dst
+ 8*stride
) {
888 c
->dsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
890 put_pixels8x8_overlapped(dst
, ref
, stride
);
892 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
893 dctblock
[0] = binkb_get_value(c
, BINKB_SRC_INTER_DC
);
894 qp
= binkb_get_value(c
, BINKB_SRC_INTER_Q
);
895 read_dct_coeffs(gb
, dctblock
, bink_scan
, binkb_inter_quant
, qp
);
896 c
->bdsp
.idct_add(dst
, stride
, dctblock
);
899 v
= binkb_get_value(c
, BINKB_SRC_COLORS
);
900 c
->dsp
.fill_block_tab
[1](dst
, v
, stride
, 8);
903 for (i
= 0; i
< 2; i
++)
904 col
[i
] = binkb_get_value(c
, BINKB_SRC_COLORS
);
905 for (i
= 0; i
< 8; i
++) {
906 v
= binkb_get_value(c
, BINKB_SRC_PATTERN
);
907 for (j
= 0; j
< 8; j
++, v
>>= 1)
908 dst
[i
*stride
+ j
] = col
[v
& 1];
912 xoff
= binkb_get_value(c
, BINKB_SRC_X_OFF
);
913 yoff
= binkb_get_value(c
, BINKB_SRC_Y_OFF
) + ybias
;
914 ref
= dst
+ xoff
+ yoff
* stride
;
915 if (ref
< ref_start
|| ref
+ 8 * stride
> ref_end
) {
916 av_log(c
->avctx
, AV_LOG_WARNING
, "Reference block is out of bounds\n");
917 } else if (ref
+ 8*stride
< dst
|| ref
>= dst
+ 8*stride
) {
918 c
->dsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
920 put_pixels8x8_overlapped(dst
, ref
, stride
);
924 for (i
= 0; i
< 8; i
++)
925 memcpy(dst
+ i
*stride
, c
->bundle
[BINKB_SRC_COLORS
].cur_ptr
+ i
*8, 8);
926 c
->bundle
[BINKB_SRC_COLORS
].cur_ptr
+= 64;
929 av_log(c
->avctx
, AV_LOG_ERROR
, "Unknown block type %d\n", blk
);
934 if (get_bits_count(gb
) & 0x1F) //next plane data starts at 32-bit boundary
935 skip_bits_long(gb
, 32 - (get_bits_count(gb
) & 0x1F));
940 static int bink_decode_plane(BinkContext
*c
, GetBitContext
*gb
, int plane_idx
,
945 uint8_t *dst
, *prev
, *ref
, *ref_start
, *ref_end
;
949 LOCAL_ALIGNED_16(DCTELEM
, block
, [64]);
950 LOCAL_ALIGNED_16(uint8_t, ublock
, [64]);
951 LOCAL_ALIGNED_16(int32_t, dctblock
, [64]);
954 const int stride
= c
->pic
.linesize
[plane_idx
];
955 int bw
= is_chroma ?
(c
->avctx
->width
+ 15) >> 4 : (c
->avctx
->width
+ 7) >> 3;
956 int bh
= is_chroma ?
(c
->avctx
->height
+ 15) >> 4 : (c
->avctx
->height
+ 7) >> 3;
957 int width
= c
->avctx
->width
>> is_chroma
;
959 init_lengths(c
, FFMAX(width
, 8), bw
);
960 for (i
= 0; i
< BINK_NB_SRC
; i
++)
961 read_bundle(gb
, c
, i
);
963 ref_start
= c
->last
.data
[plane_idx
] ? c
->last
.data
[plane_idx
]
964 : c
->pic
.data
[plane_idx
];
966 + (bw
- 1 + c
->last
.linesize
[plane_idx
] * (bh
- 1)) * 8;
968 for (i
= 0; i
< 64; i
++)
969 coordmap
[i
] = (i
& 7) + (i
>> 3) * stride
;
971 for (by
= 0; by
< bh
; by
++) {
972 if (read_block_types(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_BLOCK_TYPES
]) < 0)
974 if (read_block_types(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_SUB_BLOCK_TYPES
]) < 0)
976 if (read_colors(gb
, &c
->bundle
[BINK_SRC_COLORS
], c
) < 0)
978 if (read_patterns(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_PATTERN
]) < 0)
980 if (read_motion_values(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_X_OFF
]) < 0)
982 if (read_motion_values(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_Y_OFF
]) < 0)
984 if (read_dcs(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_INTRA_DC
], DC_START_BITS
, 0) < 0)
986 if (read_dcs(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_INTER_DC
], DC_START_BITS
, 1) < 0)
988 if (read_runs(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_RUN
]) < 0)
993 dst
= c
->pic
.data
[plane_idx
] + 8*by
*stride
;
994 prev
= (c
->last
.data
[plane_idx
] ? c
->last
.data
[plane_idx
]
995 : c
->pic
.data
[plane_idx
]) + 8*by
*stride
;
996 for (bx
= 0; bx
< bw
; bx
++, dst
+= 8, prev
+= 8) {
997 blk
= get_value(c
, BINK_SRC_BLOCK_TYPES
);
998 // 16x16 block type on odd line means part of the already decoded block, so skip it
999 if ((by
& 1) && blk
== SCALED_BLOCK
) {
1007 c
->dsp
.put_pixels_tab
[1][0](dst
, prev
, stride
, 8);
1010 blk
= get_value(c
, BINK_SRC_SUB_BLOCK_TYPES
);
1013 scan
= bink_patterns
[get_bits(gb
, 4)];
1016 int run
= get_value(c
, BINK_SRC_RUN
) + 1;
1020 av_log(c
->avctx
, AV_LOG_ERROR
, "Run went out of bounds\n");
1023 if (get_bits1(gb
)) {
1024 v
= get_value(c
, BINK_SRC_COLORS
);
1025 for (j
= 0; j
< run
; j
++)
1026 ublock
[*scan
++] = v
;
1028 for (j
= 0; j
< run
; j
++)
1029 ublock
[*scan
++] = get_value(c
, BINK_SRC_COLORS
);
1033 ublock
[*scan
++] = get_value(c
, BINK_SRC_COLORS
);
1036 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
1037 dctblock
[0] = get_value(c
, BINK_SRC_INTRA_DC
);
1038 read_dct_coeffs(gb
, dctblock
, bink_scan
, bink_intra_quant
, -1);
1039 c
->bdsp
.idct_put(ublock
, 8, dctblock
);
1042 v
= get_value(c
, BINK_SRC_COLORS
);
1043 c
->dsp
.fill_block_tab
[0](dst
, v
, stride
, 16);
1046 for (i
= 0; i
< 2; i
++)
1047 col
[i
] = get_value(c
, BINK_SRC_COLORS
);
1048 for (j
= 0; j
< 8; j
++) {
1049 v
= get_value(c
, BINK_SRC_PATTERN
);
1050 for (i
= 0; i
< 8; i
++, v
>>= 1)
1051 ublock
[i
+ j
*8] = col
[v
& 1];
1055 for (j
= 0; j
< 8; j
++)
1056 for (i
= 0; i
< 8; i
++)
1057 ublock
[i
+ j
*8] = get_value(c
, BINK_SRC_COLORS
);
1060 av_log(c
->avctx
, AV_LOG_ERROR
, "Incorrect 16x16 block type %d\n", blk
);
1063 if (blk
!= FILL_BLOCK
)
1064 c
->bdsp
.scale_block(ublock
, dst
, stride
);
1070 xoff
= get_value(c
, BINK_SRC_X_OFF
);
1071 yoff
= get_value(c
, BINK_SRC_Y_OFF
);
1072 ref
= prev
+ xoff
+ yoff
* stride
;
1073 if (ref
< ref_start
|| ref
> ref_end
) {
1074 av_log(c
->avctx
, AV_LOG_ERROR
, "Copy out of bounds @%d, %d\n",
1075 bx
*8 + xoff
, by
*8 + yoff
);
1078 c
->dsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
1081 scan
= bink_patterns
[get_bits(gb
, 4)];
1084 int run
= get_value(c
, BINK_SRC_RUN
) + 1;
1088 av_log(c
->avctx
, AV_LOG_ERROR
, "Run went out of bounds\n");
1091 if (get_bits1(gb
)) {
1092 v
= get_value(c
, BINK_SRC_COLORS
);
1093 for (j
= 0; j
< run
; j
++)
1094 dst
[coordmap
[*scan
++]] = v
;
1096 for (j
= 0; j
< run
; j
++)
1097 dst
[coordmap
[*scan
++]] = get_value(c
, BINK_SRC_COLORS
);
1101 dst
[coordmap
[*scan
++]] = get_value(c
, BINK_SRC_COLORS
);
1104 xoff
= get_value(c
, BINK_SRC_X_OFF
);
1105 yoff
= get_value(c
, BINK_SRC_Y_OFF
);
1106 ref
= prev
+ xoff
+ yoff
* stride
;
1107 if (ref
< ref_start
|| ref
> ref_end
) {
1108 av_log(c
->avctx
, AV_LOG_ERROR
, "Copy out of bounds @%d, %d\n",
1109 bx
*8 + xoff
, by
*8 + yoff
);
1112 c
->dsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
1113 c
->dsp
.clear_block(block
);
1114 v
= get_bits(gb
, 7);
1115 read_residue(gb
, block
, v
);
1116 c
->dsp
.add_pixels8(dst
, block
, stride
);
1119 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
1120 dctblock
[0] = get_value(c
, BINK_SRC_INTRA_DC
);
1121 read_dct_coeffs(gb
, dctblock
, bink_scan
, bink_intra_quant
, -1);
1122 c
->bdsp
.idct_put(dst
, stride
, dctblock
);
1125 v
= get_value(c
, BINK_SRC_COLORS
);
1126 c
->dsp
.fill_block_tab
[1](dst
, v
, stride
, 8);
1129 xoff
= get_value(c
, BINK_SRC_X_OFF
);
1130 yoff
= get_value(c
, BINK_SRC_Y_OFF
);
1131 ref
= prev
+ xoff
+ yoff
* stride
;
1132 c
->dsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
1133 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
1134 dctblock
[0] = get_value(c
, BINK_SRC_INTER_DC
);
1135 read_dct_coeffs(gb
, dctblock
, bink_scan
, bink_inter_quant
, -1);
1136 c
->bdsp
.idct_add(dst
, stride
, dctblock
);
1139 for (i
= 0; i
< 2; i
++)
1140 col
[i
] = get_value(c
, BINK_SRC_COLORS
);
1141 for (i
= 0; i
< 8; i
++) {
1142 v
= get_value(c
, BINK_SRC_PATTERN
);
1143 for (j
= 0; j
< 8; j
++, v
>>= 1)
1144 dst
[i
*stride
+ j
] = col
[v
& 1];
1148 for (i
= 0; i
< 8; i
++)
1149 memcpy(dst
+ i
*stride
, c
->bundle
[BINK_SRC_COLORS
].cur_ptr
+ i
*8, 8);
1150 c
->bundle
[BINK_SRC_COLORS
].cur_ptr
+= 64;
1153 av_log(c
->avctx
, AV_LOG_ERROR
, "Unknown block type %d\n", blk
);
1158 if (get_bits_count(gb
) & 0x1F) //next plane data starts at 32-bit boundary
1159 skip_bits_long(gb
, 32 - (get_bits_count(gb
) & 0x1F));
1164 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *got_frame
, AVPacket
*pkt
)
1166 BinkContext
* const c
= avctx
->priv_data
;
1168 int plane
, plane_idx
;
1169 int bits_count
= pkt
->size
<< 3;
1171 if (c
->version
> 'b') {
1173 avctx
->release_buffer(avctx
, &c
->pic
);
1175 if(ff_get_buffer(avctx
, &c
->pic
) < 0){
1176 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
1180 if(avctx
->reget_buffer(avctx
, &c
->pic
) < 0){
1181 av_log(avctx
, AV_LOG_ERROR
, "reget_buffer() failed\n");
1186 init_get_bits(&gb
, pkt
->data
, bits_count
);
1188 if (c
->version
>= 'i')
1189 skip_bits_long(&gb
, 32);
1190 if (bink_decode_plane(c
, &gb
, 3, 0) < 0)
1193 if (c
->version
>= 'i')
1194 skip_bits_long(&gb
, 32);
1196 for (plane
= 0; plane
< 3; plane
++) {
1197 plane_idx
= (!plane
|| !c
->swap_planes
) ? plane
: (plane
^ 3);
1199 if (c
->version
> 'b') {
1200 if (bink_decode_plane(c
, &gb
, plane_idx
, !!plane
) < 0)
1203 if (binkb_decode_plane(c
, &gb
, plane_idx
, !pkt
->pts
, !!plane
) < 0)
1206 if (get_bits_count(&gb
) >= bits_count
)
1212 *(AVFrame
*)data
= c
->pic
;
1214 if (c
->version
> 'b')
1215 FFSWAP(AVFrame
, c
->pic
, c
->last
);
1217 /* always report that the buffer was completely consumed */
1222 * Caclulate quantization tables for version b
1224 static av_cold
void binkb_calc_quant(void)
1226 uint8_t inv_bink_scan
[64];
1230 for (j
= 0; j
< 8; j
++) {
1231 for (i
= 0; i
< 8; i
++) {
1234 s
[j
*8 + i
] = cos(j
* M_PI
/16.0) * cos(i
* M_PI
/16.0) * 2.0;
1236 s
[j
*8 + i
] = cos(j
* M_PI
/16.0) * sqrt(2.0);
1239 s
[j
*8 + i
] = cos(i
* M_PI
/16.0) * sqrt(2.0);
1245 for (i
= 0; i
< 64; i
++)
1246 inv_bink_scan
[bink_scan
[i
]] = i
;
1248 for (j
= 0; j
< 16; j
++) {
1249 for (i
= 0; i
< 64; i
++) {
1250 int k
= inv_bink_scan
[i
];
1252 binkb_intra_quant
[j
][k
] = (1L << 12) * binkb_intra_seed
[i
] *
1253 binkb_num
[j
]/binkb_den
[j
];
1254 binkb_inter_quant
[j
][k
] = (1L << 12) * binkb_inter_seed
[i
] *
1255 binkb_num
[j
]/binkb_den
[j
];
1257 binkb_intra_quant
[j
][k
] = (1L << 12) * binkb_intra_seed
[i
] * s
[i
] *
1258 binkb_num
[j
]/(double)binkb_den
[j
];
1259 binkb_inter_quant
[j
][k
] = (1L << 12) * binkb_inter_seed
[i
] * s
[i
] *
1260 binkb_num
[j
]/(double)binkb_den
[j
];
1266 static av_cold
int decode_init(AVCodecContext
*avctx
)
1268 BinkContext
* const c
= avctx
->priv_data
;
1269 static VLC_TYPE table
[16 * 128][2];
1270 static int binkb_initialised
= 0;
1274 c
->version
= avctx
->codec_tag
>> 24;
1275 if (avctx
->extradata_size
< 4) {
1276 av_log(avctx
, AV_LOG_ERROR
, "Extradata missing or too short\n");
1279 flags
= AV_RL32(avctx
->extradata
);
1280 c
->has_alpha
= flags
& BINK_FLAG_ALPHA
;
1281 c
->swap_planes
= c
->version
>= 'h';
1282 if (!bink_trees
[15].table
) {
1283 for (i
= 0; i
< 16; i
++) {
1284 const int maxbits
= bink_tree_lens
[i
][15];
1285 bink_trees
[i
].table
= table
+ i
*128;
1286 bink_trees
[i
].table_allocated
= 1 << maxbits
;
1287 init_vlc(&bink_trees
[i
], maxbits
, 16,
1288 bink_tree_lens
[i
], 1, 1,
1289 bink_tree_bits
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
1294 c
->pic
.data
[0] = NULL
;
1296 if (av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
) < 0) {
1300 avctx
->pix_fmt
= c
->has_alpha ? AV_PIX_FMT_YUVA420P
: AV_PIX_FMT_YUV420P
;
1302 avctx
->idct_algo
= FF_IDCT_BINK
;
1303 ff_dsputil_init(&c
->dsp
, avctx
);
1304 ff_binkdsp_init(&c
->bdsp
);
1308 if (c
->version
== 'b') {
1309 if (!binkb_initialised
) {
1311 binkb_initialised
= 1;
1318 static av_cold
int decode_end(AVCodecContext
*avctx
)
1320 BinkContext
* const c
= avctx
->priv_data
;
1323 avctx
->release_buffer(avctx
, &c
->pic
);
1324 if (c
->last
.data
[0])
1325 avctx
->release_buffer(avctx
, &c
->last
);
1331 AVCodec ff_bink_decoder
= {
1332 .name
= "binkvideo",
1333 .type
= AVMEDIA_TYPE_VIDEO
,
1334 .id
= AV_CODEC_ID_BINKVIDEO
,
1335 .priv_data_size
= sizeof(BinkContext
),
1336 .init
= decode_init
,
1337 .close
= decode_end
,
1338 .decode
= decode_frame
,
1339 .long_name
= NULL_IF_CONFIG_SMALL("Bink video"),
1340 .capabilities
= CODEC_CAP_DR1
,