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