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
;
116 BlockDSPContext bdsp
;
118 BinkDSPContext binkdsp
;
120 int version
; ///< internal Bink file version
125 Bundle bundle
[BINKB_NB_SRC
]; ///< bundles for decoding all data types
126 Tree col_high
[16]; ///< trees for decoding high nibble in "colours" data type
127 int col_lastval
; ///< value of last decoded high nibble in "colours" data type
131 * Bink video block types
134 SKIP_BLOCK
= 0, ///< skipped block
135 SCALED_BLOCK
, ///< block has size 16x16
136 MOTION_BLOCK
, ///< block is copied from previous frame with some offset
137 RUN_BLOCK
, ///< block is composed from runs of colours with custom scan order
138 RESIDUE_BLOCK
, ///< motion block with some difference added
139 INTRA_BLOCK
, ///< intra DCT block
140 FILL_BLOCK
, ///< block is filled with single colour
141 INTER_BLOCK
, ///< motion block with DCT applied to the difference
142 PATTERN_BLOCK
, ///< block is filled with two colours following custom pattern
143 RAW_BLOCK
, ///< uncoded 8x8 block
147 * Initialize length in all bundles.
149 * @param c decoder context
150 * @param width plane width
151 * @param bw plane width in 8x8 blocks
153 static void init_lengths(BinkContext
*c
, int width
, int bw
)
155 width
= FFALIGN(width
, 8);
157 c
->bundle
[BINK_SRC_BLOCK_TYPES
].len
= av_log2((width
>> 3) + 511) + 1;
159 c
->bundle
[BINK_SRC_SUB_BLOCK_TYPES
].len
= av_log2((width
>> 4) + 511) + 1;
161 c
->bundle
[BINK_SRC_COLORS
].len
= av_log2(bw
*64 + 511) + 1;
163 c
->bundle
[BINK_SRC_INTRA_DC
].len
=
164 c
->bundle
[BINK_SRC_INTER_DC
].len
=
165 c
->bundle
[BINK_SRC_X_OFF
].len
=
166 c
->bundle
[BINK_SRC_Y_OFF
].len
= av_log2((width
>> 3) + 511) + 1;
168 c
->bundle
[BINK_SRC_PATTERN
].len
= av_log2((bw
<< 3) + 511) + 1;
170 c
->bundle
[BINK_SRC_RUN
].len
= av_log2(bw
*48 + 511) + 1;
174 * Allocate memory for bundles.
176 * @param c decoder context
178 static av_cold
int init_bundles(BinkContext
*c
)
183 bw
= (c
->avctx
->width
+ 7) >> 3;
184 bh
= (c
->avctx
->height
+ 7) >> 3;
187 for (i
= 0; i
< BINKB_NB_SRC
; i
++) {
188 c
->bundle
[i
].data
= av_mallocz(blocks
* 64);
189 if (!c
->bundle
[i
].data
)
190 return AVERROR(ENOMEM
);
191 c
->bundle
[i
].data_end
= c
->bundle
[i
].data
+ blocks
* 64;
198 * Free memory used by bundles.
200 * @param c decoder context
202 static av_cold
void free_bundles(BinkContext
*c
)
205 for (i
= 0; i
< BINKB_NB_SRC
; i
++)
206 av_freep(&c
->bundle
[i
].data
);
210 * Merge two consequent lists of equal size depending on bits read.
212 * @param gb context for reading bits
213 * @param dst buffer where merged list will be written to
214 * @param src pointer to the head of the first list (the second lists starts at src+size)
215 * @param size input lists size
217 static void merge(GetBitContext
*gb
, uint8_t *dst
, uint8_t *src
, int size
)
219 uint8_t *src2
= src
+ size
;
223 if (!get_bits1(gb
)) {
230 } while (size
&& size2
);
239 * Read information about Huffman tree used to decode data.
241 * @param gb context for reading bits
242 * @param tree pointer for storing tree data
244 static void read_tree(GetBitContext
*gb
, Tree
*tree
)
246 uint8_t tmp1
[16] = { 0 }, tmp2
[16], *in
= tmp1
, *out
= tmp2
;
249 tree
->vlc_num
= get_bits(gb
, 4);
250 if (!tree
->vlc_num
) {
251 for (i
= 0; i
< 16; i
++)
256 len
= get_bits(gb
, 3);
257 for (i
= 0; i
<= len
; i
++) {
258 tree
->syms
[i
] = get_bits(gb
, 4);
259 tmp1
[tree
->syms
[i
]] = 1;
261 for (i
= 0; i
< 16 && len
< 16 - 1; i
++)
263 tree
->syms
[++len
] = i
;
265 len
= get_bits(gb
, 2);
266 for (i
= 0; i
< 16; i
++)
268 for (i
= 0; i
<= len
; i
++) {
270 for (t
= 0; t
< 16; t
+= size
<< 1)
271 merge(gb
, out
+ t
, in
+ t
, size
);
272 FFSWAP(uint8_t*, in
, out
);
274 memcpy(tree
->syms
, in
, 16);
279 * Prepare bundle for decoding data.
281 * @param gb context for reading bits
282 * @param c decoder context
283 * @param bundle_num number of the bundle to initialize
285 static void read_bundle(GetBitContext
*gb
, BinkContext
*c
, int bundle_num
)
289 if (bundle_num
== BINK_SRC_COLORS
) {
290 for (i
= 0; i
< 16; i
++)
291 read_tree(gb
, &c
->col_high
[i
]);
294 if (bundle_num
!= BINK_SRC_INTRA_DC
&& bundle_num
!= BINK_SRC_INTER_DC
)
295 read_tree(gb
, &c
->bundle
[bundle_num
].tree
);
296 c
->bundle
[bundle_num
].cur_dec
=
297 c
->bundle
[bundle_num
].cur_ptr
= c
->bundle
[bundle_num
].data
;
301 * common check before starting decoding bundle data
303 * @param gb context for reading bits
305 * @param t variable where number of elements to decode will be stored
307 #define CHECK_READ_VAL(gb, b, t) \
308 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
310 t = get_bits(gb, b->len); \
316 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
319 const uint8_t *dec_end
;
321 CHECK_READ_VAL(gb
, b
, t
);
322 dec_end
= b
->cur_dec
+ t
;
323 if (dec_end
> b
->data_end
) {
324 av_log(avctx
, AV_LOG_ERROR
, "Run value went out of bounds\n");
325 return AVERROR_INVALIDDATA
;
329 memset(b
->cur_dec
, v
, t
);
332 while (b
->cur_dec
< dec_end
)
333 *b
->cur_dec
++ = GET_HUFF(gb
, b
->tree
);
338 static int read_motion_values(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
)
341 const uint8_t *dec_end
;
343 CHECK_READ_VAL(gb
, b
, t
);
344 dec_end
= b
->cur_dec
+ t
;
345 if (dec_end
> b
->data_end
) {
346 av_log(avctx
, AV_LOG_ERROR
, "Too many motion values\n");
347 return AVERROR_INVALIDDATA
;
352 sign
= -get_bits1(gb
);
353 v
= (v
^ sign
) - sign
;
355 memset(b
->cur_dec
, v
, t
);
358 while (b
->cur_dec
< dec_end
) {
359 v
= GET_HUFF(gb
, b
->tree
);
361 sign
= -get_bits1(gb
);
362 v
= (v
^ sign
) - sign
;
370 static const uint8_t bink_rlelens
[4] = { 4, 8, 12, 32 };
372 static int read_block_types(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
)
376 const uint8_t *dec_end
;
378 CHECK_READ_VAL(gb
, b
, t
);
379 dec_end
= b
->cur_dec
+ t
;
380 if (dec_end
> b
->data_end
) {
381 av_log(avctx
, AV_LOG_ERROR
, "Too many block type values\n");
382 return AVERROR_INVALIDDATA
;
386 memset(b
->cur_dec
, v
, t
);
389 while (b
->cur_dec
< dec_end
) {
390 v
= GET_HUFF(gb
, b
->tree
);
395 int run
= bink_rlelens
[v
- 12];
397 if (dec_end
- b
->cur_dec
< run
)
398 return AVERROR_INVALIDDATA
;
399 memset(b
->cur_dec
, last
, run
);
407 static int read_patterns(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
)
410 const uint8_t *dec_end
;
412 CHECK_READ_VAL(gb
, b
, t
);
413 dec_end
= b
->cur_dec
+ t
;
414 if (dec_end
> b
->data_end
) {
415 av_log(avctx
, AV_LOG_ERROR
, "Too many pattern values\n");
416 return AVERROR_INVALIDDATA
;
418 while (b
->cur_dec
< dec_end
) {
419 v
= GET_HUFF(gb
, b
->tree
);
420 v
|= GET_HUFF(gb
, b
->tree
) << 4;
427 static int read_colors(GetBitContext
*gb
, Bundle
*b
, BinkContext
*c
)
430 const uint8_t *dec_end
;
432 CHECK_READ_VAL(gb
, b
, t
);
433 dec_end
= b
->cur_dec
+ t
;
434 if (dec_end
> b
->data_end
) {
435 av_log(c
->avctx
, AV_LOG_ERROR
, "Too many color values\n");
436 return AVERROR_INVALIDDATA
;
439 c
->col_lastval
= GET_HUFF(gb
, c
->col_high
[c
->col_lastval
]);
440 v
= GET_HUFF(gb
, b
->tree
);
441 v
= (c
->col_lastval
<< 4) | v
;
442 if (c
->version
< 'i') {
443 sign
= ((int8_t) v
) >> 7;
444 v
= ((v
& 0x7F) ^ sign
) - sign
;
447 memset(b
->cur_dec
, v
, t
);
450 while (b
->cur_dec
< dec_end
) {
451 c
->col_lastval
= GET_HUFF(gb
, c
->col_high
[c
->col_lastval
]);
452 v
= GET_HUFF(gb
, b
->tree
);
453 v
= (c
->col_lastval
<< 4) | v
;
454 if (c
->version
< 'i') {
455 sign
= ((int8_t) v
) >> 7;
456 v
= ((v
& 0x7F) ^ sign
) - sign
;
465 /** number of bits used to store first DC value in bundle */
466 #define DC_START_BITS 11
468 static int read_dcs(AVCodecContext
*avctx
, GetBitContext
*gb
, Bundle
*b
,
469 int start_bits
, int has_sign
)
471 int i
, j
, len
, len2
, bsize
, sign
, v
, v2
;
472 int16_t *dst
= (int16_t*)b
->cur_dec
;
473 int16_t *dst_end
= (int16_t*)b
->data_end
;
475 CHECK_READ_VAL(gb
, b
, len
);
476 v
= get_bits(gb
, start_bits
- has_sign
);
478 sign
= -get_bits1(gb
);
479 v
= (v
^ sign
) - sign
;
481 if (dst_end
- dst
< 1)
482 return AVERROR_INVALIDDATA
;
485 for (i
= 0; i
< len
; i
+= 8) {
486 len2
= FFMIN(len
- i
, 8);
487 if (dst_end
- dst
< len2
)
488 return AVERROR_INVALIDDATA
;
489 bsize
= get_bits(gb
, 4);
491 for (j
= 0; j
< len2
; j
++) {
492 v2
= get_bits(gb
, bsize
);
494 sign
= -get_bits1(gb
);
495 v2
= (v2
^ sign
) - sign
;
499 if (v
< -32768 || v
> 32767) {
500 av_log(avctx
, AV_LOG_ERROR
, "DC value went out of bounds: %d\n", v
);
501 return AVERROR_INVALIDDATA
;
505 for (j
= 0; j
< len2
; j
++)
510 b
->cur_dec
= (uint8_t*)dst
;
515 * Retrieve next value from bundle.
517 * @param c decoder context
518 * @param bundle bundle number
520 static inline int get_value(BinkContext
*c
, int bundle
)
524 if (bundle
< BINK_SRC_X_OFF
|| bundle
== BINK_SRC_RUN
)
525 return *c
->bundle
[bundle
].cur_ptr
++;
526 if (bundle
== BINK_SRC_X_OFF
|| bundle
== BINK_SRC_Y_OFF
)
527 return (int8_t)*c
->bundle
[bundle
].cur_ptr
++;
528 ret
= *(int16_t*)c
->bundle
[bundle
].cur_ptr
;
529 c
->bundle
[bundle
].cur_ptr
+= 2;
533 static av_cold
void binkb_init_bundle(BinkContext
*c
, int bundle_num
)
535 c
->bundle
[bundle_num
].cur_dec
=
536 c
->bundle
[bundle_num
].cur_ptr
= c
->bundle
[bundle_num
].data
;
537 c
->bundle
[bundle_num
].len
= 13;
540 static av_cold
void binkb_init_bundles(BinkContext
*c
)
543 for (i
= 0; i
< BINKB_NB_SRC
; i
++)
544 binkb_init_bundle(c
, i
);
547 static int binkb_read_bundle(BinkContext
*c
, GetBitContext
*gb
, int bundle_num
)
549 const int bits
= binkb_bundle_sizes
[bundle_num
];
550 const int mask
= 1 << (bits
- 1);
551 const int issigned
= binkb_bundle_signed
[bundle_num
];
552 Bundle
*b
= &c
->bundle
[bundle_num
];
555 CHECK_READ_VAL(gb
, b
, len
);
556 if (b
->data_end
- b
->cur_dec
< len
* (1 + (bits
> 8)))
557 return AVERROR_INVALIDDATA
;
560 for (i
= 0; i
< len
; i
++)
561 *b
->cur_dec
++ = get_bits(gb
, bits
);
563 for (i
= 0; i
< len
; i
++)
564 *b
->cur_dec
++ = get_bits(gb
, bits
) - mask
;
567 int16_t *dst
= (int16_t*)b
->cur_dec
;
570 for (i
= 0; i
< len
; i
++)
571 *dst
++ = get_bits(gb
, bits
);
573 for (i
= 0; i
< len
; i
++)
574 *dst
++ = get_bits(gb
, bits
) - mask
;
576 b
->cur_dec
= (uint8_t*)dst
;
581 static inline int binkb_get_value(BinkContext
*c
, int bundle_num
)
584 const int bits
= binkb_bundle_sizes
[bundle_num
];
587 int val
= *c
->bundle
[bundle_num
].cur_ptr
++;
588 return binkb_bundle_signed
[bundle_num
] ? (int8_t)val
: val
;
590 ret
= *(int16_t*)c
->bundle
[bundle_num
].cur_ptr
;
591 c
->bundle
[bundle_num
].cur_ptr
+= 2;
596 * Read 8x8 block of DCT coefficients.
598 * @param gb context for reading bits
599 * @param block place for storing coefficients
600 * @param scan scan order table
601 * @param quant_matrices quantization matrices
602 * @return 0 for success, negative value in other cases
604 static int read_dct_coeffs(GetBitContext
*gb
, int32_t block
[64], const uint8_t *scan
,
605 const int32_t quant_matrices
[16][64], int q
)
609 int i
, t
, bits
, ccoef
, mode
, sign
;
610 int list_start
= 64, list_end
= 64, list_pos
;
614 const int32_t *quant
;
616 coef_list
[list_end
] = 4; mode_list
[list_end
++] = 0;
617 coef_list
[list_end
] = 24; mode_list
[list_end
++] = 0;
618 coef_list
[list_end
] = 44; mode_list
[list_end
++] = 0;
619 coef_list
[list_end
] = 1; mode_list
[list_end
++] = 3;
620 coef_list
[list_end
] = 2; mode_list
[list_end
++] = 3;
621 coef_list
[list_end
] = 3; mode_list
[list_end
++] = 3;
623 for (bits
= get_bits(gb
, 4) - 1; bits
>= 0; bits
--) {
624 list_pos
= list_start
;
625 while (list_pos
< list_end
) {
626 if (!(mode_list
[list_pos
] | coef_list
[list_pos
]) || !get_bits1(gb
)) {
630 ccoef
= coef_list
[list_pos
];
631 mode
= mode_list
[list_pos
];
634 coef_list
[list_pos
] = ccoef
+ 4;
635 mode_list
[list_pos
] = 1;
638 coef_list
[list_pos
] = 0;
639 mode_list
[list_pos
++] = 0;
641 for (i
= 0; i
< 4; i
++, ccoef
++) {
643 coef_list
[--list_start
] = ccoef
;
644 mode_list
[ list_start
] = 3;
647 t
= 1 - (get_bits1(gb
) << 1);
649 t
= get_bits(gb
, bits
) | 1 << bits
;
650 sign
= -get_bits1(gb
);
651 t
= (t
^ sign
) - sign
;
653 block
[scan
[ccoef
]] = t
;
654 coef_idx
[coef_count
++] = ccoef
;
659 mode_list
[list_pos
] = 2;
660 for (i
= 0; i
< 3; i
++) {
662 coef_list
[list_end
] = ccoef
;
663 mode_list
[list_end
++] = 2;
668 t
= 1 - (get_bits1(gb
) << 1);
670 t
= get_bits(gb
, bits
) | 1 << bits
;
671 sign
= -get_bits1(gb
);
672 t
= (t
^ sign
) - sign
;
674 block
[scan
[ccoef
]] = t
;
675 coef_idx
[coef_count
++] = ccoef
;
676 coef_list
[list_pos
] = 0;
677 mode_list
[list_pos
++] = 0;
684 quant_idx
= get_bits(gb
, 4);
687 if (quant_idx
> 15U) {
688 av_log(NULL
, AV_LOG_ERROR
, "quant_index %d out of range\n", quant_idx
);
689 return AVERROR_INVALIDDATA
;
693 quant
= quant_matrices
[quant_idx
];
695 block
[0] = (block
[0] * quant
[0]) >> 11;
696 for (i
= 0; i
< coef_count
; i
++) {
697 int idx
= coef_idx
[i
];
698 block
[scan
[idx
]] = (block
[scan
[idx
]] * quant
[idx
]) >> 11;
705 * Read 8x8 block with residue after motion compensation.
707 * @param gb context for reading bits
708 * @param block place to store read data
709 * @param masks_count number of masks to decode
710 * @return 0 on success, negative value in other cases
712 static int read_residue(GetBitContext
*gb
, int16_t block
[64], int masks_count
)
716 int i
, sign
, mask
, ccoef
, mode
;
717 int list_start
= 64, list_end
= 64, list_pos
;
719 int nz_coeff_count
= 0;
721 coef_list
[list_end
] = 4; mode_list
[list_end
++] = 0;
722 coef_list
[list_end
] = 24; mode_list
[list_end
++] = 0;
723 coef_list
[list_end
] = 44; mode_list
[list_end
++] = 0;
724 coef_list
[list_end
] = 0; mode_list
[list_end
++] = 2;
726 for (mask
= 1 << get_bits(gb
, 3); mask
; mask
>>= 1) {
727 for (i
= 0; i
< nz_coeff_count
; i
++) {
730 if (block
[nz_coeff
[i
]] < 0)
731 block
[nz_coeff
[i
]] -= mask
;
733 block
[nz_coeff
[i
]] += mask
;
738 list_pos
= list_start
;
739 while (list_pos
< list_end
) {
740 if (!(coef_list
[list_pos
] | mode_list
[list_pos
]) || !get_bits1(gb
)) {
744 ccoef
= coef_list
[list_pos
];
745 mode
= mode_list
[list_pos
];
748 coef_list
[list_pos
] = ccoef
+ 4;
749 mode_list
[list_pos
] = 1;
752 coef_list
[list_pos
] = 0;
753 mode_list
[list_pos
++] = 0;
755 for (i
= 0; i
< 4; i
++, ccoef
++) {
757 coef_list
[--list_start
] = ccoef
;
758 mode_list
[ list_start
] = 3;
760 nz_coeff
[nz_coeff_count
++] = bink_scan
[ccoef
];
761 sign
= -get_bits1(gb
);
762 block
[bink_scan
[ccoef
]] = (mask
^ sign
) - sign
;
770 mode_list
[list_pos
] = 2;
771 for (i
= 0; i
< 3; i
++) {
773 coef_list
[list_end
] = ccoef
;
774 mode_list
[list_end
++] = 2;
778 nz_coeff
[nz_coeff_count
++] = bink_scan
[ccoef
];
779 sign
= -get_bits1(gb
);
780 block
[bink_scan
[ccoef
]] = (mask
^ sign
) - sign
;
781 coef_list
[list_pos
] = 0;
782 mode_list
[list_pos
++] = 0;
795 * Copy 8x8 block from source to destination, where src and dst may be overlapped
797 static inline void put_pixels8x8_overlapped(uint8_t *dst
, uint8_t *src
, int stride
)
801 for (i
= 0; i
< 8; i
++)
802 memcpy(tmp
+ i
*8, src
+ i
*stride
, 8);
803 for (i
= 0; i
< 8; i
++)
804 memcpy(dst
+ i
*stride
, tmp
+ i
*8, 8);
807 static int binkb_decode_plane(BinkContext
*c
, AVFrame
*frame
, GetBitContext
*gb
,
808 int plane_idx
, int is_key
, int is_chroma
)
812 uint8_t *dst
, *ref
, *ref_start
, *ref_end
;
816 LOCAL_ALIGNED_16(int16_t, block
, [64]);
817 LOCAL_ALIGNED_16(int32_t, dctblock
, [64]);
819 int ybias
= is_key
? -15 : 0;
822 const int stride
= frame
->linesize
[plane_idx
];
823 int bw
= is_chroma
? (c
->avctx
->width
+ 15) >> 4 : (c
->avctx
->width
+ 7) >> 3;
824 int bh
= is_chroma
? (c
->avctx
->height
+ 15) >> 4 : (c
->avctx
->height
+ 7) >> 3;
826 binkb_init_bundles(c
);
827 ref_start
= frame
->data
[plane_idx
];
828 ref_end
= frame
->data
[plane_idx
] + (bh
* frame
->linesize
[plane_idx
] + bw
) * 8;
830 for (i
= 0; i
< 64; i
++)
831 coordmap
[i
] = (i
& 7) + (i
>> 3) * stride
;
833 for (by
= 0; by
< bh
; by
++) {
834 for (i
= 0; i
< BINKB_NB_SRC
; i
++) {
835 if ((ret
= binkb_read_bundle(c
, gb
, i
)) < 0)
839 dst
= frame
->data
[plane_idx
] + 8*by
*stride
;
840 for (bx
= 0; bx
< bw
; bx
++, dst
+= 8) {
841 blk
= binkb_get_value(c
, BINKB_SRC_BLOCK_TYPES
);
846 scan
= bink_patterns
[get_bits(gb
, 4)];
851 mode
= get_bits1(gb
);
852 run
= get_bits(gb
, binkb_runbits
[i
]) + 1;
856 av_log(c
->avctx
, AV_LOG_ERROR
, "Run went out of bounds\n");
857 return AVERROR_INVALIDDATA
;
860 v
= binkb_get_value(c
, BINKB_SRC_COLORS
);
861 for (j
= 0; j
< run
; j
++)
862 dst
[coordmap
[*scan
++]] = v
;
864 for (j
= 0; j
< run
; j
++)
865 dst
[coordmap
[*scan
++]] = binkb_get_value(c
, BINKB_SRC_COLORS
);
869 dst
[coordmap
[*scan
++]] = binkb_get_value(c
, BINKB_SRC_COLORS
);
872 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
873 dctblock
[0] = binkb_get_value(c
, BINKB_SRC_INTRA_DC
);
874 qp
= binkb_get_value(c
, BINKB_SRC_INTRA_Q
);
875 read_dct_coeffs(gb
, dctblock
, bink_scan
, (const int32_t (*)[64])binkb_intra_quant
, qp
);
876 c
->binkdsp
.idct_put(dst
, stride
, dctblock
);
879 xoff
= binkb_get_value(c
, BINKB_SRC_X_OFF
);
880 yoff
= binkb_get_value(c
, BINKB_SRC_Y_OFF
) + ybias
;
881 ref
= dst
+ xoff
+ yoff
* stride
;
882 if (ref
< ref_start
|| ref
+ 8*stride
> ref_end
) {
883 av_log(c
->avctx
, AV_LOG_WARNING
, "Reference block is out of bounds\n");
884 } else if (ref
+ 8*stride
< dst
|| ref
>= dst
+ 8*stride
) {
885 c
->hdsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
887 put_pixels8x8_overlapped(dst
, ref
, stride
);
889 c
->bdsp
.clear_block(block
);
890 v
= binkb_get_value(c
, BINKB_SRC_INTER_COEFS
);
891 read_residue(gb
, block
, v
);
892 c
->binkdsp
.add_pixels8(dst
, block
, stride
);
895 xoff
= binkb_get_value(c
, BINKB_SRC_X_OFF
);
896 yoff
= binkb_get_value(c
, BINKB_SRC_Y_OFF
) + ybias
;
897 ref
= dst
+ xoff
+ yoff
* stride
;
898 if (ref
< ref_start
|| ref
+ 8 * stride
> ref_end
) {
899 av_log(c
->avctx
, AV_LOG_WARNING
, "Reference block is out of bounds\n");
900 } else if (ref
+ 8*stride
< dst
|| ref
>= dst
+ 8*stride
) {
901 c
->hdsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
903 put_pixels8x8_overlapped(dst
, ref
, stride
);
905 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
906 dctblock
[0] = binkb_get_value(c
, BINKB_SRC_INTER_DC
);
907 qp
= binkb_get_value(c
, BINKB_SRC_INTER_Q
);
908 read_dct_coeffs(gb
, dctblock
, bink_scan
, (const int32_t (*)[64])binkb_inter_quant
, qp
);
909 c
->binkdsp
.idct_add(dst
, stride
, dctblock
);
912 v
= binkb_get_value(c
, BINKB_SRC_COLORS
);
913 c
->bdsp
.fill_block_tab
[1](dst
, v
, stride
, 8);
916 for (i
= 0; i
< 2; i
++)
917 col
[i
] = binkb_get_value(c
, BINKB_SRC_COLORS
);
918 for (i
= 0; i
< 8; i
++) {
919 v
= binkb_get_value(c
, BINKB_SRC_PATTERN
);
920 for (j
= 0; j
< 8; j
++, v
>>= 1)
921 dst
[i
*stride
+ j
] = col
[v
& 1];
925 xoff
= binkb_get_value(c
, BINKB_SRC_X_OFF
);
926 yoff
= binkb_get_value(c
, BINKB_SRC_Y_OFF
) + ybias
;
927 ref
= dst
+ xoff
+ yoff
* stride
;
928 if (ref
< ref_start
|| ref
+ 8 * stride
> ref_end
) {
929 av_log(c
->avctx
, AV_LOG_WARNING
, "Reference block is out of bounds\n");
930 } else if (ref
+ 8*stride
< dst
|| ref
>= dst
+ 8*stride
) {
931 c
->hdsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
933 put_pixels8x8_overlapped(dst
, ref
, stride
);
937 for (i
= 0; i
< 8; i
++)
938 memcpy(dst
+ i
*stride
, c
->bundle
[BINKB_SRC_COLORS
].cur_ptr
+ i
*8, 8);
939 c
->bundle
[BINKB_SRC_COLORS
].cur_ptr
+= 64;
942 av_log(c
->avctx
, AV_LOG_ERROR
, "Unknown block type %d\n", blk
);
943 return AVERROR_INVALIDDATA
;
947 if (get_bits_count(gb
) & 0x1F) //next plane data starts at 32-bit boundary
948 skip_bits_long(gb
, 32 - (get_bits_count(gb
) & 0x1F));
953 static int bink_decode_plane(BinkContext
*c
, AVFrame
*frame
, GetBitContext
*gb
,
954 int plane_idx
, int is_chroma
)
958 uint8_t *dst
, *prev
, *ref
, *ref_start
, *ref_end
;
962 LOCAL_ALIGNED_16(int16_t, block
, [64]);
963 LOCAL_ALIGNED_16(uint8_t, ublock
, [64]);
964 LOCAL_ALIGNED_16(int32_t, dctblock
, [64]);
967 const int stride
= frame
->linesize
[plane_idx
];
968 int bw
= is_chroma
? (c
->avctx
->width
+ 15) >> 4 : (c
->avctx
->width
+ 7) >> 3;
969 int bh
= is_chroma
? (c
->avctx
->height
+ 15) >> 4 : (c
->avctx
->height
+ 7) >> 3;
970 int width
= c
->avctx
->width
>> is_chroma
;
972 init_lengths(c
, FFMAX(width
, 8), bw
);
973 for (i
= 0; i
< BINK_NB_SRC
; i
++)
974 read_bundle(gb
, c
, i
);
976 ref_start
= c
->last
->data
[plane_idx
] ? c
->last
->data
[plane_idx
]
977 : frame
->data
[plane_idx
];
979 + (bw
- 1 + c
->last
->linesize
[plane_idx
] * (bh
- 1)) * 8;
981 for (i
= 0; i
< 64; i
++)
982 coordmap
[i
] = (i
& 7) + (i
>> 3) * stride
;
984 for (by
= 0; by
< bh
; by
++) {
985 if ((ret
= read_block_types(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_BLOCK_TYPES
])) < 0)
987 if ((ret
= read_block_types(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_SUB_BLOCK_TYPES
])) < 0)
989 if ((ret
= read_colors(gb
, &c
->bundle
[BINK_SRC_COLORS
], c
)) < 0)
991 if ((ret
= read_patterns(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_PATTERN
])) < 0)
993 if ((ret
= read_motion_values(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_X_OFF
])) < 0)
995 if ((ret
= read_motion_values(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_Y_OFF
])) < 0)
997 if ((ret
= read_dcs(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_INTRA_DC
], DC_START_BITS
, 0)) < 0)
999 if ((ret
= read_dcs(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_INTER_DC
], DC_START_BITS
, 1)) < 0)
1001 if ((ret
= read_runs(c
->avctx
, gb
, &c
->bundle
[BINK_SRC_RUN
])) < 0)
1006 dst
= frame
->data
[plane_idx
] + 8*by
*stride
;
1007 prev
= (c
->last
->data
[plane_idx
] ? c
->last
->data
[plane_idx
]
1008 : frame
->data
[plane_idx
]) + 8*by
*stride
;
1009 for (bx
= 0; bx
< bw
; bx
++, dst
+= 8, prev
+= 8) {
1010 blk
= get_value(c
, BINK_SRC_BLOCK_TYPES
);
1011 // 16x16 block type on odd line means part of the already decoded block, so skip it
1012 if ((by
& 1) && blk
== SCALED_BLOCK
) {
1020 c
->hdsp
.put_pixels_tab
[1][0](dst
, prev
, stride
, 8);
1023 blk
= get_value(c
, BINK_SRC_SUB_BLOCK_TYPES
);
1026 scan
= bink_patterns
[get_bits(gb
, 4)];
1029 int run
= get_value(c
, BINK_SRC_RUN
) + 1;
1033 av_log(c
->avctx
, AV_LOG_ERROR
, "Run went out of bounds\n");
1034 return AVERROR_INVALIDDATA
;
1036 if (get_bits1(gb
)) {
1037 v
= get_value(c
, BINK_SRC_COLORS
);
1038 for (j
= 0; j
< run
; j
++)
1039 ublock
[*scan
++] = v
;
1041 for (j
= 0; j
< run
; j
++)
1042 ublock
[*scan
++] = get_value(c
, BINK_SRC_COLORS
);
1046 ublock
[*scan
++] = get_value(c
, BINK_SRC_COLORS
);
1049 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
1050 dctblock
[0] = get_value(c
, BINK_SRC_INTRA_DC
);
1051 read_dct_coeffs(gb
, dctblock
, bink_scan
, bink_intra_quant
, -1);
1052 c
->binkdsp
.idct_put(ublock
, 8, dctblock
);
1055 v
= get_value(c
, BINK_SRC_COLORS
);
1056 c
->bdsp
.fill_block_tab
[0](dst
, v
, stride
, 16);
1059 for (i
= 0; i
< 2; i
++)
1060 col
[i
] = get_value(c
, BINK_SRC_COLORS
);
1061 for (j
= 0; j
< 8; j
++) {
1062 v
= get_value(c
, BINK_SRC_PATTERN
);
1063 for (i
= 0; i
< 8; i
++, v
>>= 1)
1064 ublock
[i
+ j
*8] = col
[v
& 1];
1068 for (j
= 0; j
< 8; j
++)
1069 for (i
= 0; i
< 8; i
++)
1070 ublock
[i
+ j
*8] = get_value(c
, BINK_SRC_COLORS
);
1073 av_log(c
->avctx
, AV_LOG_ERROR
, "Incorrect 16x16 block type %d\n", blk
);
1074 return AVERROR_INVALIDDATA
;
1076 if (blk
!= FILL_BLOCK
)
1077 c
->binkdsp
.scale_block(ublock
, dst
, stride
);
1083 xoff
= get_value(c
, BINK_SRC_X_OFF
);
1084 yoff
= get_value(c
, BINK_SRC_Y_OFF
);
1085 ref
= prev
+ xoff
+ yoff
* stride
;
1086 if (ref
< ref_start
|| ref
> ref_end
) {
1087 av_log(c
->avctx
, AV_LOG_ERROR
, "Copy out of bounds @%d, %d\n",
1088 bx
*8 + xoff
, by
*8 + yoff
);
1089 return AVERROR_INVALIDDATA
;
1091 c
->hdsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
1094 scan
= bink_patterns
[get_bits(gb
, 4)];
1097 int run
= get_value(c
, BINK_SRC_RUN
) + 1;
1101 av_log(c
->avctx
, AV_LOG_ERROR
, "Run went out of bounds\n");
1102 return AVERROR_INVALIDDATA
;
1104 if (get_bits1(gb
)) {
1105 v
= get_value(c
, BINK_SRC_COLORS
);
1106 for (j
= 0; j
< run
; j
++)
1107 dst
[coordmap
[*scan
++]] = v
;
1109 for (j
= 0; j
< run
; j
++)
1110 dst
[coordmap
[*scan
++]] = get_value(c
, BINK_SRC_COLORS
);
1114 dst
[coordmap
[*scan
++]] = get_value(c
, BINK_SRC_COLORS
);
1117 xoff
= get_value(c
, BINK_SRC_X_OFF
);
1118 yoff
= get_value(c
, BINK_SRC_Y_OFF
);
1119 ref
= prev
+ xoff
+ yoff
* stride
;
1120 if (ref
< ref_start
|| ref
> ref_end
) {
1121 av_log(c
->avctx
, AV_LOG_ERROR
, "Copy out of bounds @%d, %d\n",
1122 bx
*8 + xoff
, by
*8 + yoff
);
1123 return AVERROR_INVALIDDATA
;
1125 c
->hdsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
1126 c
->bdsp
.clear_block(block
);
1127 v
= get_bits(gb
, 7);
1128 read_residue(gb
, block
, v
);
1129 c
->binkdsp
.add_pixels8(dst
, block
, stride
);
1132 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
1133 dctblock
[0] = get_value(c
, BINK_SRC_INTRA_DC
);
1134 read_dct_coeffs(gb
, dctblock
, bink_scan
, bink_intra_quant
, -1);
1135 c
->binkdsp
.idct_put(dst
, stride
, dctblock
);
1138 v
= get_value(c
, BINK_SRC_COLORS
);
1139 c
->bdsp
.fill_block_tab
[1](dst
, v
, stride
, 8);
1142 xoff
= get_value(c
, BINK_SRC_X_OFF
);
1143 yoff
= get_value(c
, BINK_SRC_Y_OFF
);
1144 ref
= prev
+ xoff
+ yoff
* stride
;
1145 if (ref
< ref_start
|| ref
> ref_end
) {
1146 av_log(c
->avctx
, AV_LOG_ERROR
, "Copy out of bounds @%d, %d\n",
1147 bx
*8 + xoff
, by
*8 + yoff
);
1150 c
->hdsp
.put_pixels_tab
[1][0](dst
, ref
, stride
, 8);
1151 memset(dctblock
, 0, sizeof(*dctblock
) * 64);
1152 dctblock
[0] = get_value(c
, BINK_SRC_INTER_DC
);
1153 read_dct_coeffs(gb
, dctblock
, bink_scan
, bink_inter_quant
, -1);
1154 c
->binkdsp
.idct_add(dst
, stride
, dctblock
);
1157 for (i
= 0; i
< 2; i
++)
1158 col
[i
] = get_value(c
, BINK_SRC_COLORS
);
1159 for (i
= 0; i
< 8; i
++) {
1160 v
= get_value(c
, BINK_SRC_PATTERN
);
1161 for (j
= 0; j
< 8; j
++, v
>>= 1)
1162 dst
[i
*stride
+ j
] = col
[v
& 1];
1166 for (i
= 0; i
< 8; i
++)
1167 memcpy(dst
+ i
*stride
, c
->bundle
[BINK_SRC_COLORS
].cur_ptr
+ i
*8, 8);
1168 c
->bundle
[BINK_SRC_COLORS
].cur_ptr
+= 64;
1171 av_log(c
->avctx
, AV_LOG_ERROR
, "Unknown block type %d\n", blk
);
1172 return AVERROR_INVALIDDATA
;
1176 if (get_bits_count(gb
) & 0x1F) //next plane data starts at 32-bit boundary
1177 skip_bits_long(gb
, 32 - (get_bits_count(gb
) & 0x1F));
1182 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *got_frame
, AVPacket
*pkt
)
1184 BinkContext
* const c
= avctx
->priv_data
;
1185 AVFrame
*frame
= data
;
1187 int plane
, plane_idx
, ret
;
1188 int bits_count
= pkt
->size
<< 3;
1190 if (c
->version
> 'b') {
1191 if ((ret
= ff_get_buffer(avctx
, frame
, AV_GET_BUFFER_FLAG_REF
)) < 0)
1194 if ((ret
= ff_reget_buffer(avctx
, c
->last
)) < 0)
1196 if ((ret
= av_frame_ref(frame
, c
->last
)) < 0)
1200 init_get_bits(&gb
, pkt
->data
, bits_count
);
1202 if (c
->version
>= 'i')
1203 skip_bits_long(&gb
, 32);
1204 if ((ret
= bink_decode_plane(c
, frame
, &gb
, 3, 0)) < 0)
1207 if (c
->version
>= 'i')
1208 skip_bits_long(&gb
, 32);
1212 for (plane
= 0; plane
< 3; plane
++) {
1213 plane_idx
= (!plane
|| !c
->swap_planes
) ? plane
: (plane
^ 3);
1215 if (c
->version
> 'b') {
1216 if ((ret
= bink_decode_plane(c
, frame
, &gb
, plane_idx
, !!plane
)) < 0)
1219 if ((ret
= binkb_decode_plane(c
, frame
, &gb
, plane_idx
,
1220 c
->frame_num
== 1, !!plane
)) < 0)
1223 if (get_bits_count(&gb
) >= bits_count
)
1228 if (c
->version
> 'b') {
1229 av_frame_unref(c
->last
);
1230 if ((ret
= av_frame_ref(c
->last
, frame
)) < 0)
1236 /* always report that the buffer was completely consumed */
1241 * Caclulate quantization tables for version b
1243 static av_cold
void binkb_calc_quant(void)
1245 uint8_t inv_bink_scan
[64];
1246 static const int s
[64]={
1247 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1248 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1249 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1250 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1251 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1252 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1253 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1254 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1258 for (i
= 0; i
< 64; i
++)
1259 inv_bink_scan
[bink_scan
[i
]] = i
;
1261 for (j
= 0; j
< 16; j
++) {
1262 for (i
= 0; i
< 64; i
++) {
1263 int k
= inv_bink_scan
[i
];
1264 binkb_intra_quant
[j
][k
] = binkb_intra_seed
[i
] * (int64_t)s
[i
] *
1265 binkb_num
[j
]/(binkb_den
[j
] * (C
>>12));
1266 binkb_inter_quant
[j
][k
] = binkb_inter_seed
[i
] * (int64_t)s
[i
] *
1267 binkb_num
[j
]/(binkb_den
[j
] * (C
>>12));
1272 static av_cold
int decode_init(AVCodecContext
*avctx
)
1274 BinkContext
* const c
= avctx
->priv_data
;
1275 static VLC_TYPE table
[16 * 128][2];
1276 static int binkb_initialised
= 0;
1280 c
->version
= avctx
->codec_tag
>> 24;
1281 if (avctx
->extradata_size
< 4) {
1282 av_log(avctx
, AV_LOG_ERROR
, "Extradata missing or too short\n");
1283 return AVERROR_INVALIDDATA
;
1285 flags
= AV_RL32(avctx
->extradata
);
1286 c
->has_alpha
= flags
& BINK_FLAG_ALPHA
;
1287 c
->swap_planes
= c
->version
>= 'h';
1288 if (!bink_trees
[15].table
) {
1289 for (i
= 0; i
< 16; i
++) {
1290 const int maxbits
= bink_tree_lens
[i
][15];
1291 bink_trees
[i
].table
= table
+ i
*128;
1292 bink_trees
[i
].table_allocated
= 1 << maxbits
;
1293 init_vlc(&bink_trees
[i
], maxbits
, 16,
1294 bink_tree_lens
[i
], 1, 1,
1295 bink_tree_bits
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
1300 c
->last
= av_frame_alloc();
1302 return AVERROR(ENOMEM
);
1304 if ((ret
= av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
)) < 0)
1307 avctx
->pix_fmt
= c
->has_alpha
? AV_PIX_FMT_YUVA420P
: AV_PIX_FMT_YUV420P
;
1309 ff_blockdsp_init(&c
->bdsp
, avctx
);
1310 ff_hpeldsp_init(&c
->hdsp
, avctx
->flags
);
1311 ff_binkdsp_init(&c
->binkdsp
);
1313 if ((ret
= init_bundles(c
)) < 0) {
1318 if (c
->version
== 'b') {
1319 if (!binkb_initialised
) {
1321 binkb_initialised
= 1;
1328 static av_cold
int decode_end(AVCodecContext
*avctx
)
1330 BinkContext
* const c
= avctx
->priv_data
;
1332 av_frame_free(&c
->last
);
1338 static void flush(AVCodecContext
*avctx
)
1340 BinkContext
* const c
= avctx
->priv_data
;
1345 AVCodec ff_bink_decoder
= {
1346 .name
= "binkvideo",
1347 .long_name
= NULL_IF_CONFIG_SMALL("Bink video"),
1348 .type
= AVMEDIA_TYPE_VIDEO
,
1349 .id
= AV_CODEC_ID_BINKVIDEO
,
1350 .priv_data_size
= sizeof(BinkContext
),
1351 .init
= decode_init
,
1352 .close
= decode_end
,
1353 .decode
= decode_frame
,
1355 .capabilities
= CODEC_CAP_DR1
,