84388b86aa7fc80a4c713382a3a27472849af2a7
[libav.git] / libavcodec / bink.c
1 /*
2 * Bink video decoder
3 * Copyright (c) 2009 Konstantin Shishkov
4 * Copyright (C) 2011 Peter Ross <pross@xvid.org>
5 *
6 * This file is part of Libav.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "libavutil/attributes.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/internal.h"
26
27 #define BITSTREAM_READER_LE
28 #include "avcodec.h"
29 #include "binkdata.h"
30 #include "binkdsp.h"
31 #include "bitstream.h"
32 #include "blockdsp.h"
33 #include "hpeldsp.h"
34 #include "internal.h"
35 #include "mathops.h"
36
37 #define BINK_FLAG_ALPHA 0x00100000
38 #define BINK_FLAG_GRAY 0x00020000
39
40 static VLC bink_trees[16];
41
42 /**
43 * IDs for different data types used in old version of Bink video codec
44 */
45 enum OldSources {
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
56
57 BINKB_NB_SRC
58 };
59
60 static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
61 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
62 };
63
64 static const int binkb_bundle_signed[BINKB_NB_SRC] = {
65 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
66 };
67
68 static int32_t binkb_intra_quant[16][64];
69 static int32_t binkb_inter_quant[16][64];
70
71 /**
72 * IDs for different data types used in Bink video codec
73 */
74 enum Sources {
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
84
85 BINK_NB_SRC
86 };
87
88 /**
89 * data needed to decode 4-bit Huffman-coded value
90 */
91 typedef struct Tree {
92 int vlc_num; ///< tree number (in bink_trees[])
93 uint8_t syms[16]; ///< leaf value to symbol mapping
94 } Tree;
95
96 #define GET_HUFF(bc, tree) \
97 (tree).syms[bitstream_read_vlc(bc, bink_trees[(tree).vlc_num].table, \
98 bink_trees[(tree).vlc_num].bits, 1)]
99
100 /**
101 * data structure used for decoding single Bink data type
102 */
103 typedef struct Bundle {
104 int len; ///< length of number of entries to decode (in bits)
105 Tree tree; ///< Huffman tree-related data
106 uint8_t *data; ///< buffer for decoded symbols
107 uint8_t *data_end; ///< buffer end
108 uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
109 uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
110 } Bundle;
111
112 /*
113 * Decoder context
114 */
115 typedef struct BinkContext {
116 AVCodecContext *avctx;
117 BlockDSPContext bdsp;
118 HpelDSPContext hdsp;
119 BinkDSPContext binkdsp;
120 AVFrame *last;
121 int version; ///< internal Bink file version
122 int has_alpha;
123 int swap_planes;
124
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
128 } BinkContext;
129
130 /**
131 * Bink video block types
132 */
133 enum BlockTypes {
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
144 };
145
146 /**
147 * Initialize length length in all bundles.
148 *
149 * @param c decoder context
150 * @param width plane width
151 * @param bw plane width in 8x8 blocks
152 */
153 static void init_lengths(BinkContext *c, int width, int bw)
154 {
155 width = FFALIGN(width, 8);
156
157 c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
158
159 c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
160
161 c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
162
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;
167
168 c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
169
170 c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
171 }
172
173 /**
174 * Allocate memory for bundles.
175 *
176 * @param c decoder context
177 */
178 static av_cold void init_bundles(BinkContext *c)
179 {
180 int bw, bh, blocks;
181 int i;
182
183 bw = (c->avctx->width + 7) >> 3;
184 bh = (c->avctx->height + 7) >> 3;
185 blocks = bw * bh;
186
187 for (i = 0; i < BINKB_NB_SRC; i++) {
188 c->bundle[i].data = av_malloc(blocks * 64);
189 c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
190 }
191 }
192
193 /**
194 * Free memory used by bundles.
195 *
196 * @param c decoder context
197 */
198 static av_cold void free_bundles(BinkContext *c)
199 {
200 int i;
201 for (i = 0; i < BINKB_NB_SRC; i++)
202 av_freep(&c->bundle[i].data);
203 }
204
205 /**
206 * Merge two consequent lists of equal size depending on bits read.
207 *
208 * @param bc context for reading bits
209 * @param dst buffer where merged list will be written to
210 * @param src pointer to the head of the first list (the second lists starts at src+size)
211 * @param size input lists size
212 */
213 static void merge(BitstreamContext *bc, uint8_t *dst, uint8_t *src, int size)
214 {
215 uint8_t *src2 = src + size;
216 int size2 = size;
217
218 do {
219 if (!bitstream_read_bit(bc)) {
220 *dst++ = *src++;
221 size--;
222 } else {
223 *dst++ = *src2++;
224 size2--;
225 }
226 } while (size && size2);
227
228 while (size--)
229 *dst++ = *src++;
230 while (size2--)
231 *dst++ = *src2++;
232 }
233
234 /**
235 * Read information about Huffman tree used to decode data.
236 *
237 * @param bc context for reading bits
238 * @param tree pointer for storing tree data
239 */
240 static void read_tree(BitstreamContext *bc, Tree *tree)
241 {
242 uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
243 int i, t, len;
244
245 tree->vlc_num = bitstream_read(bc, 4);
246 if (!tree->vlc_num) {
247 for (i = 0; i < 16; i++)
248 tree->syms[i] = i;
249 return;
250 }
251 if (bitstream_read_bit(bc)) {
252 len = bitstream_read(bc, 3);
253 for (i = 0; i <= len; i++) {
254 tree->syms[i] = bitstream_read(bc, 4);
255 tmp1[tree->syms[i]] = 1;
256 }
257 for (i = 0; i < 16 && len < 16 - 1; i++)
258 if (!tmp1[i])
259 tree->syms[++len] = i;
260 } else {
261 len = bitstream_read(bc, 2);
262 for (i = 0; i < 16; i++)
263 in[i] = i;
264 for (i = 0; i <= len; i++) {
265 int size = 1 << i;
266 for (t = 0; t < 16; t += size << 1)
267 merge(bc, out + t, in + t, size);
268 FFSWAP(uint8_t*, in, out);
269 }
270 memcpy(tree->syms, in, 16);
271 }
272 }
273
274 /**
275 * Prepare bundle for decoding data.
276 *
277 * @param bc context for reading bits
278 * @param c decoder context
279 * @param bundle_num number of the bundle to initialize
280 */
281 static void read_bundle(BitstreamContext *bc, BinkContext *c, int bundle_num)
282 {
283 int i;
284
285 if (bundle_num == BINK_SRC_COLORS) {
286 for (i = 0; i < 16; i++)
287 read_tree(bc, &c->col_high[i]);
288 c->col_lastval = 0;
289 }
290 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
291 read_tree(bc, &c->bundle[bundle_num].tree);
292 c->bundle[bundle_num].cur_dec =
293 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
294 }
295
296 /**
297 * common check before starting decoding bundle data
298 *
299 * @param bc context for reading bits
300 * @param b bundle
301 * @param t variable where number of elements to decode will be stored
302 */
303 #define CHECK_READ_VAL(bc, b, t) \
304 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
305 return 0; \
306 t = bitstream_read(bc, b->len); \
307 if (!t) { \
308 b->cur_dec = NULL; \
309 return 0; \
310 } \
311
312 static int read_runs(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b)
313 {
314 int t, v;
315 const uint8_t *dec_end;
316
317 CHECK_READ_VAL(bc, b, t);
318 dec_end = b->cur_dec + t;
319 if (dec_end > b->data_end) {
320 av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
321 return AVERROR_INVALIDDATA;
322 }
323 if (bitstream_read_bit(bc)) {
324 v = bitstream_read(bc, 4);
325 memset(b->cur_dec, v, t);
326 b->cur_dec += t;
327 } else {
328 while (b->cur_dec < dec_end)
329 *b->cur_dec++ = GET_HUFF(bc, b->tree);
330 }
331 return 0;
332 }
333
334 static int read_motion_values(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b)
335 {
336 int t, v;
337 const uint8_t *dec_end;
338
339 CHECK_READ_VAL(bc, b, t);
340 dec_end = b->cur_dec + t;
341 if (dec_end > b->data_end) {
342 av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
343 return AVERROR_INVALIDDATA;
344 }
345 if (bitstream_read_bit(bc)) {
346 v = bitstream_read(bc, 4);
347 if (v) {
348 v = bitstream_apply_sign(bc, v);
349 }
350 memset(b->cur_dec, v, t);
351 b->cur_dec += t;
352 } else {
353 while (b->cur_dec < dec_end) {
354 v = GET_HUFF(bc, b->tree);
355 if (v) {
356 v = bitstream_apply_sign(bc, v);
357 }
358 *b->cur_dec++ = v;
359 }
360 }
361 return 0;
362 }
363
364 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
365
366 static int read_block_types(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b)
367 {
368 int t, v;
369 int last = 0;
370 const uint8_t *dec_end;
371
372 CHECK_READ_VAL(bc, b, t);
373 dec_end = b->cur_dec + t;
374 if (dec_end > b->data_end) {
375 av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
376 return AVERROR_INVALIDDATA;
377 }
378 if (bitstream_read_bit(bc)) {
379 v = bitstream_read(bc, 4);
380 memset(b->cur_dec, v, t);
381 b->cur_dec += t;
382 } else {
383 while (b->cur_dec < dec_end) {
384 v = GET_HUFF(bc, b->tree);
385 if (v < 12) {
386 last = v;
387 *b->cur_dec++ = v;
388 } else {
389 int run = bink_rlelens[v - 12];
390
391 if (dec_end - b->cur_dec < run)
392 return AVERROR_INVALIDDATA;
393 memset(b->cur_dec, last, run);
394 b->cur_dec += run;
395 }
396 }
397 }
398 return 0;
399 }
400
401 static int read_patterns(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b)
402 {
403 int t, v;
404 const uint8_t *dec_end;
405
406 CHECK_READ_VAL(bc, b, t);
407 dec_end = b->cur_dec + t;
408 if (dec_end > b->data_end) {
409 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
410 return AVERROR_INVALIDDATA;
411 }
412 while (b->cur_dec < dec_end) {
413 v = GET_HUFF(bc, b->tree);
414 v |= GET_HUFF(bc, b->tree) << 4;
415 *b->cur_dec++ = v;
416 }
417
418 return 0;
419 }
420
421 static int read_colors(BitstreamContext *bc, Bundle *b, BinkContext *c)
422 {
423 int t, sign, v;
424 const uint8_t *dec_end;
425
426 CHECK_READ_VAL(bc, b, t);
427 dec_end = b->cur_dec + t;
428 if (dec_end > b->data_end) {
429 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
430 return AVERROR_INVALIDDATA;
431 }
432 if (bitstream_read_bit(bc)) {
433 c->col_lastval = GET_HUFF(bc, c->col_high[c->col_lastval]);
434 v = GET_HUFF(bc, b->tree);
435 v = (c->col_lastval << 4) | v;
436 if (c->version < 'i') {
437 sign = ((int8_t) v) >> 7;
438 v = ((v & 0x7F) ^ sign) - sign;
439 v += 0x80;
440 }
441 memset(b->cur_dec, v, t);
442 b->cur_dec += t;
443 } else {
444 while (b->cur_dec < dec_end) {
445 c->col_lastval = GET_HUFF(bc, c->col_high[c->col_lastval]);
446 v = GET_HUFF(bc, b->tree);
447 v = (c->col_lastval << 4) | v;
448 if (c->version < 'i') {
449 sign = ((int8_t) v) >> 7;
450 v = ((v & 0x7F) ^ sign) - sign;
451 v += 0x80;
452 }
453 *b->cur_dec++ = v;
454 }
455 }
456 return 0;
457 }
458
459 /** number of bits used to store first DC value in bundle */
460 #define DC_START_BITS 11
461
462 static int read_dcs(AVCodecContext *avctx, BitstreamContext *bc, Bundle *b,
463 int start_bits, int has_sign)
464 {
465 int i, j, len, len2, bsize, v, v2;
466 int16_t *dst = (int16_t*)b->cur_dec;
467 int16_t *dst_end = (int16_t*)b->data_end;
468
469 CHECK_READ_VAL(bc, b, len);
470 v = bitstream_read(bc, start_bits - has_sign);
471 if (v && has_sign) {
472 v = bitstream_apply_sign(bc, v);
473 }
474 if (dst_end - dst < 1)
475 return AVERROR_INVALIDDATA;
476 *dst++ = v;
477 len--;
478 for (i = 0; i < len; i += 8) {
479 len2 = FFMIN(len - i, 8);
480 if (dst_end - dst < len2)
481 return AVERROR_INVALIDDATA;
482 bsize = bitstream_read(bc, 4);
483 if (bsize) {
484 for (j = 0; j < len2; j++) {
485 v2 = bitstream_read(bc, bsize);
486 if (v2) {
487 v2 = bitstream_apply_sign(bc, v2);
488 }
489 v += v2;
490 *dst++ = v;
491 if (v < -32768 || v > 32767) {
492 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
493 return AVERROR_INVALIDDATA;
494 }
495 }
496 } else {
497 for (j = 0; j < len2; j++)
498 *dst++ = v;
499 }
500 }
501
502 b->cur_dec = (uint8_t*)dst;
503 return 0;
504 }
505
506 /**
507 * Retrieve next value from bundle.
508 *
509 * @param c decoder context
510 * @param bundle bundle number
511 */
512 static inline int get_value(BinkContext *c, int bundle)
513 {
514 int ret;
515
516 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
517 return *c->bundle[bundle].cur_ptr++;
518 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
519 return (int8_t)*c->bundle[bundle].cur_ptr++;
520 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
521 c->bundle[bundle].cur_ptr += 2;
522 return ret;
523 }
524
525 static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
526 {
527 c->bundle[bundle_num].cur_dec =
528 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
529 c->bundle[bundle_num].len = 13;
530 }
531
532 static av_cold void binkb_init_bundles(BinkContext *c)
533 {
534 int i;
535 for (i = 0; i < BINKB_NB_SRC; i++)
536 binkb_init_bundle(c, i);
537 }
538
539 static int binkb_read_bundle(BinkContext *c, BitstreamContext *bc, int bundle_num)
540 {
541 const int bits = binkb_bundle_sizes[bundle_num];
542 const int mask = 1 << (bits - 1);
543 const int issigned = binkb_bundle_signed[bundle_num];
544 Bundle *b = &c->bundle[bundle_num];
545 int i, len;
546
547 CHECK_READ_VAL(bc, b, len);
548 if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
549 return AVERROR_INVALIDDATA;
550 if (bits <= 8) {
551 if (!issigned) {
552 for (i = 0; i < len; i++)
553 *b->cur_dec++ = bitstream_read(bc, bits);
554 } else {
555 for (i = 0; i < len; i++)
556 *b->cur_dec++ = bitstream_read(bc, bits) - mask;
557 }
558 } else {
559 int16_t *dst = (int16_t*)b->cur_dec;
560
561 if (!issigned) {
562 for (i = 0; i < len; i++)
563 *dst++ = bitstream_read(bc, bits);
564 } else {
565 for (i = 0; i < len; i++)
566 *dst++ = bitstream_read(bc, bits) - mask;
567 }
568 b->cur_dec = (uint8_t*)dst;
569 }
570 return 0;
571 }
572
573 static inline int binkb_get_value(BinkContext *c, int bundle_num)
574 {
575 int16_t ret;
576 const int bits = binkb_bundle_sizes[bundle_num];
577
578 if (bits <= 8) {
579 int val = *c->bundle[bundle_num].cur_ptr++;
580 return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
581 }
582 ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
583 c->bundle[bundle_num].cur_ptr += 2;
584 return ret;
585 }
586
587 /**
588 * Read 8x8 block of DCT coefficients.
589 *
590 * @param bc context for reading bits
591 * @param block place for storing coefficients
592 * @param scan scan order table
593 * @param quant_matrices quantization matrices
594 * @return 0 for success, negative value in other cases
595 */
596 static int read_dct_coeffs(BitstreamContext *bc, int32_t block[64],
597 const uint8_t *scan,
598 const int32_t quant_matrices[16][64], int q)
599 {
600 int coef_list[128];
601 int mode_list[128];
602 int i, t, bits, ccoef, mode;
603 int list_start = 64, list_end = 64, list_pos;
604 int coef_count = 0;
605 int coef_idx[64];
606 int quant_idx;
607 const int32_t *quant;
608
609 coef_list[list_end] = 4; mode_list[list_end++] = 0;
610 coef_list[list_end] = 24; mode_list[list_end++] = 0;
611 coef_list[list_end] = 44; mode_list[list_end++] = 0;
612 coef_list[list_end] = 1; mode_list[list_end++] = 3;
613 coef_list[list_end] = 2; mode_list[list_end++] = 3;
614 coef_list[list_end] = 3; mode_list[list_end++] = 3;
615
616 for (bits = bitstream_read(bc, 4) - 1; bits >= 0; bits--) {
617 list_pos = list_start;
618 while (list_pos < list_end) {
619 if (!(mode_list[list_pos] | coef_list[list_pos]) || !bitstream_read_bit(bc)) {
620 list_pos++;
621 continue;
622 }
623 ccoef = coef_list[list_pos];
624 mode = mode_list[list_pos];
625 switch (mode) {
626 case 0:
627 coef_list[list_pos] = ccoef + 4;
628 mode_list[list_pos] = 1;
629 case 2:
630 if (mode == 2) {
631 coef_list[list_pos] = 0;
632 mode_list[list_pos++] = 0;
633 }
634 for (i = 0; i < 4; i++, ccoef++) {
635 if (bitstream_read_bit(bc)) {
636 coef_list[--list_start] = ccoef;
637 mode_list[ list_start] = 3;
638 } else {
639 if (!bits) {
640 t = 1 - (bitstream_read_bit(bc) << 1);
641 } else {
642 t = bitstream_read(bc, bits) | 1 << bits;
643 t = bitstream_apply_sign(bc, t);
644 }
645 block[scan[ccoef]] = t;
646 coef_idx[coef_count++] = ccoef;
647 }
648 }
649 break;
650 case 1:
651 mode_list[list_pos] = 2;
652 for (i = 0; i < 3; i++) {
653 ccoef += 4;
654 coef_list[list_end] = ccoef;
655 mode_list[list_end++] = 2;
656 }
657 break;
658 case 3:
659 if (!bits) {
660 t = 1 - (bitstream_read_bit(bc) << 1);
661 } else {
662 t = bitstream_read(bc, bits) | 1 << bits;
663 t = bitstream_apply_sign(bc, t);
664 }
665 block[scan[ccoef]] = t;
666 coef_idx[coef_count++] = ccoef;
667 coef_list[list_pos] = 0;
668 mode_list[list_pos++] = 0;
669 break;
670 }
671 }
672 }
673
674 if (q == -1) {
675 quant_idx = bitstream_read(bc, 4);
676 } else {
677 quant_idx = q;
678 }
679
680 if (quant_idx >= 16)
681 return AVERROR_INVALIDDATA;
682
683 quant = quant_matrices[quant_idx];
684
685 block[0] = (block[0] * quant[0]) >> 11;
686 for (i = 0; i < coef_count; i++) {
687 int idx = coef_idx[i];
688 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
689 }
690
691 return 0;
692 }
693
694 /**
695 * Read 8x8 block with residue after motion compensation.
696 *
697 * @param bc context for reading bits
698 * @param block place to store read data
699 * @param masks_count number of masks to decode
700 * @return 0 on success, negative value in other cases
701 */
702 static int read_residue(BitstreamContext *bc, int16_t block[64], int masks_count)
703 {
704 int coef_list[128];
705 int mode_list[128];
706 int i, mask, ccoef, mode;
707 int list_start = 64, list_end = 64, list_pos;
708 int nz_coeff[64];
709 int nz_coeff_count = 0;
710
711 coef_list[list_end] = 4; mode_list[list_end++] = 0;
712 coef_list[list_end] = 24; mode_list[list_end++] = 0;
713 coef_list[list_end] = 44; mode_list[list_end++] = 0;
714 coef_list[list_end] = 0; mode_list[list_end++] = 2;
715
716 for (mask = 1 << bitstream_read(bc, 3); mask; mask >>= 1) {
717 for (i = 0; i < nz_coeff_count; i++) {
718 if (!bitstream_read_bit(bc))
719 continue;
720 if (block[nz_coeff[i]] < 0)
721 block[nz_coeff[i]] -= mask;
722 else
723 block[nz_coeff[i]] += mask;
724 masks_count--;
725 if (masks_count < 0)
726 return 0;
727 }
728 list_pos = list_start;
729 while (list_pos < list_end) {
730 if (!(coef_list[list_pos] | mode_list[list_pos]) || !bitstream_read_bit(bc)) {
731 list_pos++;
732 continue;
733 }
734 ccoef = coef_list[list_pos];
735 mode = mode_list[list_pos];
736 switch (mode) {
737 case 0:
738 coef_list[list_pos] = ccoef + 4;
739 mode_list[list_pos] = 1;
740 case 2:
741 if (mode == 2) {
742 coef_list[list_pos] = 0;
743 mode_list[list_pos++] = 0;
744 }
745 for (i = 0; i < 4; i++, ccoef++) {
746 if (bitstream_read_bit(bc)) {
747 coef_list[--list_start] = ccoef;
748 mode_list[ list_start] = 3;
749 } else {
750 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
751 block[bink_scan[ccoef]] = bitstream_apply_sign(bc, mask);
752 masks_count--;
753 if (masks_count < 0)
754 return 0;
755 }
756 }
757 break;
758 case 1:
759 mode_list[list_pos] = 2;
760 for (i = 0; i < 3; i++) {
761 ccoef += 4;
762 coef_list[list_end] = ccoef;
763 mode_list[list_end++] = 2;
764 }
765 break;
766 case 3:
767 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
768 block[bink_scan[ccoef]] = bitstream_apply_sign(bc, mask);
769 coef_list[list_pos] = 0;
770 mode_list[list_pos++] = 0;
771 masks_count--;
772 if (masks_count < 0)
773 return 0;
774 break;
775 }
776 }
777 }
778
779 return 0;
780 }
781
782 /**
783 * Copy 8x8 block from source to destination, where src and dst may be overlapped
784 */
785 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
786 {
787 uint8_t tmp[64];
788 int i;
789 for (i = 0; i < 8; i++)
790 memcpy(tmp + i*8, src + i*stride, 8);
791 for (i = 0; i < 8; i++)
792 memcpy(dst + i*stride, tmp + i*8, 8);
793 }
794
795 static int binkb_decode_plane(BinkContext *c, AVFrame *frame, BitstreamContext *bc,
796 int plane_idx, int is_key, int is_chroma)
797 {
798 int blk, ret;
799 int i, j, bx, by;
800 uint8_t *dst, *ref, *ref_start, *ref_end;
801 int v, col[2];
802 const uint8_t *scan;
803 int xoff, yoff;
804 LOCAL_ALIGNED_16(int16_t, block, [64]);
805 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
806 int coordmap[64];
807 int ybias = is_key ? -15 : 0;
808 int qp;
809
810 const int stride = frame->linesize[plane_idx];
811 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
812 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
813
814 binkb_init_bundles(c);
815 ref_start = frame->data[plane_idx];
816 ref_end = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
817
818 for (i = 0; i < 64; i++)
819 coordmap[i] = (i & 7) + (i >> 3) * stride;
820
821 for (by = 0; by < bh; by++) {
822 for (i = 0; i < BINKB_NB_SRC; i++) {
823 if ((ret = binkb_read_bundle(c, bc, i)) < 0)
824 return ret;
825 }
826
827 dst = frame->data[plane_idx] + 8*by*stride;
828 for (bx = 0; bx < bw; bx++, dst += 8) {
829 blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
830 switch (blk) {
831 case 0:
832 break;
833 case 1:
834 scan = bink_patterns[bitstream_read(bc, 4)];
835 i = 0;
836 do {
837 int mode = bitstream_read_bit(bc);
838 int run = bitstream_read(bc, binkb_runbits[i]) + 1;
839
840 i += run;
841 if (i > 64) {
842 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
843 return AVERROR_INVALIDDATA;
844 }
845 if (mode) {
846 v = binkb_get_value(c, BINKB_SRC_COLORS);
847 for (j = 0; j < run; j++)
848 dst[coordmap[*scan++]] = v;
849 } else {
850 for (j = 0; j < run; j++)
851 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
852 }
853 } while (i < 63);
854 if (i == 63)
855 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
856 break;
857 case 2:
858 memset(dctblock, 0, sizeof(*dctblock) * 64);
859 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
860 qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
861 read_dct_coeffs(bc, dctblock, bink_scan, binkb_intra_quant, qp);
862 c->binkdsp.idct_put(dst, stride, dctblock);
863 break;
864 case 3:
865 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
866 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
867 ref = dst + xoff + yoff * stride;
868 if (ref < ref_start || ref + 8*stride > ref_end) {
869 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
870 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
871 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
872 } else {
873 put_pixels8x8_overlapped(dst, ref, stride);
874 }
875 c->bdsp.clear_block(block);
876 v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
877 read_residue(bc, block, v);
878 c->binkdsp.add_pixels8(dst, block, stride);
879 break;
880 case 4:
881 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
882 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
883 ref = dst + xoff + yoff * stride;
884 if (ref < ref_start || ref + 8 * stride > ref_end) {
885 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
886 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
887 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
888 } else {
889 put_pixels8x8_overlapped(dst, ref, stride);
890 }
891 memset(dctblock, 0, sizeof(*dctblock) * 64);
892 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
893 qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
894 read_dct_coeffs(bc, dctblock, bink_scan, binkb_inter_quant, qp);
895 c->binkdsp.idct_add(dst, stride, dctblock);
896 break;
897 case 5:
898 v = binkb_get_value(c, BINKB_SRC_COLORS);
899 c->bdsp.fill_block_tab[1](dst, v, stride, 8);
900 break;
901 case 6:
902 for (i = 0; i < 2; i++)
903 col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
904 for (i = 0; i < 8; i++) {
905 v = binkb_get_value(c, BINKB_SRC_PATTERN);
906 for (j = 0; j < 8; j++, v >>= 1)
907 dst[i*stride + j] = col[v & 1];
908 }
909 break;
910 case 7:
911 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
912 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
913 ref = dst + xoff + yoff * stride;
914 if (ref < ref_start || ref + 8 * stride > ref_end) {
915 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
916 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
917 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
918 } else {
919 put_pixels8x8_overlapped(dst, ref, stride);
920 }
921 break;
922 case 8:
923 for (i = 0; i < 8; i++)
924 memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
925 c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
926 break;
927 default:
928 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
929 return AVERROR_INVALIDDATA;
930 }
931 }
932 }
933 if (bitstream_tell(bc) & 0x1F) // next plane data starts at 32-bit boundary
934 bitstream_skip(bc, 32 - (bitstream_tell(bc) & 0x1F));
935
936 return 0;
937 }
938
939 static int bink_put_pixels(BinkContext *c,
940 uint8_t *dst, uint8_t *prev, int stride,
941 uint8_t *ref_start,
942 uint8_t *ref_end)
943 {
944 int xoff = get_value(c, BINK_SRC_X_OFF);
945 int yoff = get_value(c, BINK_SRC_Y_OFF);
946 uint8_t *ref = prev + xoff + yoff * stride;
947 if (ref < ref_start || ref > ref_end) {
948 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
949 xoff, yoff);
950 return AVERROR_INVALIDDATA;
951 }
952 c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
953
954 return 0;
955 }
956
957 static int bink_decode_plane(BinkContext *c, AVFrame *frame, BitstreamContext *bc,
958 int plane_idx, int is_chroma)
959 {
960 int blk, ret;
961 int i, j, bx, by;
962 uint8_t *dst, *prev, *ref_start, *ref_end;
963 int v, col[2];
964 const uint8_t *scan;
965 LOCAL_ALIGNED_16(int16_t, block, [64]);
966 LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
967 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
968 int coordmap[64];
969
970 const int stride = frame->linesize[plane_idx];
971 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
972 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
973 int width = c->avctx->width >> is_chroma;
974
975 init_lengths(c, FFMAX(width, 8), bw);
976 for (i = 0; i < BINK_NB_SRC; i++)
977 read_bundle(bc, c, i);
978
979 ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
980 : frame->data[plane_idx];
981 ref_end = ref_start
982 + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
983
984 for (i = 0; i < 64; i++)
985 coordmap[i] = (i & 7) + (i >> 3) * stride;
986
987 for (by = 0; by < bh; by++) {
988 if ((ret = read_block_types(c->avctx, bc, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
989 return ret;
990 if ((ret = read_block_types(c->avctx, bc, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
991 return ret;
992 if ((ret = read_colors(bc, &c->bundle[BINK_SRC_COLORS], c)) < 0)
993 return ret;
994 if ((ret = read_patterns(c->avctx, bc, &c->bundle[BINK_SRC_PATTERN])) < 0)
995 return ret;
996 if ((ret = read_motion_values(c->avctx, bc, &c->bundle[BINK_SRC_X_OFF])) < 0)
997 return ret;
998 if ((ret = read_motion_values(c->avctx, bc, &c->bundle[BINK_SRC_Y_OFF])) < 0)
999 return ret;
1000 if ((ret = read_dcs(c->avctx, bc, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
1001 return ret;
1002 if ((ret = read_dcs(c->avctx, bc, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
1003 return ret;
1004 if ((ret = read_runs(c->avctx, bc, &c->bundle[BINK_SRC_RUN])) < 0)
1005 return ret;
1006
1007 if (by == bh)
1008 break;
1009 dst = frame->data[plane_idx] + 8*by*stride;
1010 prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
1011 : frame->data[plane_idx]) + 8*by*stride;
1012 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1013 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
1014 // 16x16 block type on odd line means part of the already decoded block, so skip it
1015 if ((by & 1) && blk == SCALED_BLOCK) {
1016 bx++;
1017 dst += 8;
1018 prev += 8;
1019 continue;
1020 }
1021 switch (blk) {
1022 case SKIP_BLOCK:
1023 c->hdsp.put_pixels_tab[1][0](dst, prev, stride, 8);
1024 break;
1025 case SCALED_BLOCK:
1026 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
1027 switch (blk) {
1028 case RUN_BLOCK:
1029 scan = bink_patterns[bitstream_read(bc, 4)];
1030 i = 0;
1031 do {
1032 int run = get_value(c, BINK_SRC_RUN) + 1;
1033
1034 i += run;
1035 if (i > 64) {
1036 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1037 return AVERROR_INVALIDDATA;
1038 }
1039 if (bitstream_read_bit(bc)) {
1040 v = get_value(c, BINK_SRC_COLORS);
1041 for (j = 0; j < run; j++)
1042 ublock[*scan++] = v;
1043 } else {
1044 for (j = 0; j < run; j++)
1045 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1046 }
1047 } while (i < 63);
1048 if (i == 63)
1049 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1050 break;
1051 case INTRA_BLOCK:
1052 memset(dctblock, 0, sizeof(*dctblock) * 64);
1053 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1054 read_dct_coeffs(bc, dctblock, bink_scan, bink_intra_quant, -1);
1055 c->binkdsp.idct_put(ublock, 8, dctblock);
1056 break;
1057 case FILL_BLOCK:
1058 v = get_value(c, BINK_SRC_COLORS);
1059 c->bdsp.fill_block_tab[0](dst, v, stride, 16);
1060 break;
1061 case PATTERN_BLOCK:
1062 for (i = 0; i < 2; i++)
1063 col[i] = get_value(c, BINK_SRC_COLORS);
1064 for (j = 0; j < 8; j++) {
1065 v = get_value(c, BINK_SRC_PATTERN);
1066 for (i = 0; i < 8; i++, v >>= 1)
1067 ublock[i + j*8] = col[v & 1];
1068 }
1069 break;
1070 case RAW_BLOCK:
1071 for (j = 0; j < 8; j++)
1072 for (i = 0; i < 8; i++)
1073 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1074 break;
1075 default:
1076 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1077 return AVERROR_INVALIDDATA;
1078 }
1079 if (blk != FILL_BLOCK)
1080 c->binkdsp.scale_block(ublock, dst, stride);
1081 bx++;
1082 dst += 8;
1083 prev += 8;
1084 break;
1085 case MOTION_BLOCK:
1086 ret = bink_put_pixels(c, dst, prev, stride,
1087 ref_start, ref_end);
1088 if (ret < 0)
1089 return ret;
1090 break;
1091 case RUN_BLOCK:
1092 scan = bink_patterns[bitstream_read(bc, 4)];
1093 i = 0;
1094 do {
1095 int run = get_value(c, BINK_SRC_RUN) + 1;
1096
1097 i += run;
1098 if (i > 64) {
1099 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1100 return AVERROR_INVALIDDATA;
1101 }
1102 if (bitstream_read_bit(bc)) {
1103 v = get_value(c, BINK_SRC_COLORS);
1104 for (j = 0; j < run; j++)
1105 dst[coordmap[*scan++]] = v;
1106 } else {
1107 for (j = 0; j < run; j++)
1108 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1109 }
1110 } while (i < 63);
1111 if (i == 63)
1112 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1113 break;
1114 case RESIDUE_BLOCK:
1115 ret = bink_put_pixels(c, dst, prev, stride,
1116 ref_start, ref_end);
1117 if (ret < 0)
1118 return ret;
1119 c->bdsp.clear_block(block);
1120 v = bitstream_read(bc, 7);
1121 read_residue(bc, block, v);
1122 c->binkdsp.add_pixels8(dst, block, stride);
1123 break;
1124 case INTRA_BLOCK:
1125 memset(dctblock, 0, sizeof(*dctblock) * 64);
1126 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1127 read_dct_coeffs(bc, dctblock, bink_scan, bink_intra_quant, -1);
1128 c->binkdsp.idct_put(dst, stride, dctblock);
1129 break;
1130 case FILL_BLOCK:
1131 v = get_value(c, BINK_SRC_COLORS);
1132 c->bdsp.fill_block_tab[1](dst, v, stride, 8);
1133 break;
1134 case INTER_BLOCK:
1135 ret = bink_put_pixels(c, dst, prev, stride,
1136 ref_start, ref_end);
1137 if (ret < 0)
1138 return ret;
1139 memset(dctblock, 0, sizeof(*dctblock) * 64);
1140 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1141 read_dct_coeffs(bc, dctblock, bink_scan, bink_inter_quant, -1);
1142 c->binkdsp.idct_add(dst, stride, dctblock);
1143 break;
1144 case PATTERN_BLOCK:
1145 for (i = 0; i < 2; i++)
1146 col[i] = get_value(c, BINK_SRC_COLORS);
1147 for (i = 0; i < 8; i++) {
1148 v = get_value(c, BINK_SRC_PATTERN);
1149 for (j = 0; j < 8; j++, v >>= 1)
1150 dst[i*stride + j] = col[v & 1];
1151 }
1152 break;
1153 case RAW_BLOCK:
1154 for (i = 0; i < 8; i++)
1155 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1156 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1157 break;
1158 default:
1159 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1160 return AVERROR_INVALIDDATA;
1161 }
1162 }
1163 }
1164 if (bitstream_tell(bc) & 0x1F) // next plane data starts at 32-bit boundary
1165 bitstream_skip(bc, 32 - (bitstream_tell(bc) & 0x1F));
1166
1167 return 0;
1168 }
1169
1170 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
1171 {
1172 BinkContext * const c = avctx->priv_data;
1173 AVFrame *frame = data;
1174 BitstreamContext bc;
1175 int plane, plane_idx, ret;
1176 int bits_count = pkt->size << 3;
1177
1178 if (c->version > 'b') {
1179 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) {
1180 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1181 return ret;
1182 }
1183 } else {
1184 if ((ret = ff_reget_buffer(avctx, c->last)) < 0) {
1185 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1186 return ret;
1187 }
1188 if ((ret = av_frame_ref(frame, c->last)) < 0)
1189 return ret;
1190 }
1191
1192 bitstream_init(&bc, pkt->data, bits_count);
1193 if (c->has_alpha) {
1194 if (c->version >= 'i')
1195 bitstream_skip(&bc, 32);
1196 if ((ret = bink_decode_plane(c, frame, &bc, 3, 0)) < 0)
1197 return ret;
1198 }
1199 if (c->version >= 'i')
1200 bitstream_skip(&bc, 32);
1201
1202 for (plane = 0; plane < 3; plane++) {
1203 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1204
1205 if (c->version > 'b') {
1206 if ((ret = bink_decode_plane(c, frame, &bc, plane_idx, !!plane)) < 0)
1207 return ret;
1208 } else {
1209 if ((ret = binkb_decode_plane(c, frame, &bc, plane_idx,
1210 !avctx->frame_number, !!plane)) < 0)
1211 return ret;
1212 }
1213 if (bitstream_tell(&bc) >= bits_count)
1214 break;
1215 }
1216 emms_c();
1217
1218 if (c->version > 'b') {
1219 av_frame_unref(c->last);
1220 if ((ret = av_frame_ref(c->last, frame)) < 0)
1221 return ret;
1222 }
1223
1224 *got_frame = 1;
1225
1226 /* always report that the buffer was completely consumed */
1227 return pkt->size;
1228 }
1229
1230 /**
1231 * Calculate quantization tables for version b
1232 */
1233 static av_cold void binkb_calc_quant(void)
1234 {
1235 uint8_t inv_bink_scan[64];
1236 double s[64];
1237 int i, j;
1238
1239 for (j = 0; j < 8; j++) {
1240 for (i = 0; i < 8; i++) {
1241 if (j && j != 4)
1242 if (i && i != 4)
1243 s[j*8 + i] = cos(j * M_PI/16.0) * cos(i * M_PI/16.0) * 2.0;
1244 else
1245 s[j*8 + i] = cos(j * M_PI/16.0) * sqrt(2.0);
1246 else
1247 if (i && i != 4)
1248 s[j*8 + i] = cos(i * M_PI/16.0) * sqrt(2.0);
1249 else
1250 s[j*8 + i] = 1.0;
1251 }
1252 }
1253
1254 for (i = 0; i < 64; i++)
1255 inv_bink_scan[bink_scan[i]] = i;
1256
1257 for (j = 0; j < 16; j++) {
1258 for (i = 0; i < 64; i++) {
1259 int k = inv_bink_scan[i];
1260 if (s[i] == 1.0) {
1261 binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] *
1262 binkb_num[j]/binkb_den[j];
1263 binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] *
1264 binkb_num[j]/binkb_den[j];
1265 } else {
1266 binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] * s[i] *
1267 binkb_num[j]/(double)binkb_den[j];
1268 binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] * s[i] *
1269 binkb_num[j]/(double)binkb_den[j];
1270 }
1271 }
1272 }
1273 }
1274
1275 static av_cold int decode_init(AVCodecContext *avctx)
1276 {
1277 BinkContext * const c = avctx->priv_data;
1278 static VLC_TYPE table[16 * 128][2];
1279 static int binkb_initialised = 0;
1280 int i, ret;
1281 int flags;
1282
1283 c->version = avctx->codec_tag >> 24;
1284 if (avctx->extradata_size < 4) {
1285 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1286 return AVERROR_INVALIDDATA;
1287 }
1288 flags = AV_RL32(avctx->extradata);
1289 c->has_alpha = flags & BINK_FLAG_ALPHA;
1290 c->swap_planes = c->version >= 'h';
1291 if (!bink_trees[15].table) {
1292 for (i = 0; i < 16; i++) {
1293 const int maxbits = bink_tree_lens[i][15];
1294 bink_trees[i].table = table + i*128;
1295 bink_trees[i].table_allocated = 1 << maxbits;
1296 init_vlc(&bink_trees[i], maxbits, 16,
1297 bink_tree_lens[i], 1, 1,
1298 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1299 }
1300 }
1301 c->avctx = avctx;
1302
1303 c->last = av_frame_alloc();
1304 if (!c->last)
1305 return AVERROR(ENOMEM);
1306
1307 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1308 return ret;
1309
1310 avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1311
1312 ff_blockdsp_init(&c->bdsp);
1313 ff_hpeldsp_init(&c->hdsp, avctx->flags);
1314 ff_binkdsp_init(&c->binkdsp);
1315
1316 init_bundles(c);
1317
1318 if (c->version == 'b') {
1319 if (!binkb_initialised) {
1320 binkb_calc_quant();
1321 binkb_initialised = 1;
1322 }
1323 }
1324
1325 return 0;
1326 }
1327
1328 static av_cold int decode_end(AVCodecContext *avctx)
1329 {
1330 BinkContext * const c = avctx->priv_data;
1331
1332 av_frame_free(&c->last);
1333
1334 free_bundles(c);
1335 return 0;
1336 }
1337
1338 AVCodec ff_bink_decoder = {
1339 .name = "binkvideo",
1340 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
1341 .type = AVMEDIA_TYPE_VIDEO,
1342 .id = AV_CODEC_ID_BINKVIDEO,
1343 .priv_data_size = sizeof(BinkContext),
1344 .init = decode_init,
1345 .close = decode_end,
1346 .decode = decode_frame,
1347 .capabilities = AV_CODEC_CAP_DR1,
1348 };