FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bink.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 670 814 82.3%
Branches: 413 525 78.7%

Line Branch Exec Source
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 FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "libavutil/attributes.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/mem_internal.h"
27 #include "libavutil/thread.h"
28
29 #define BITSTREAM_READER_LE
30 #include "avcodec.h"
31 #include "binkdata.h"
32 #include "binkdsp.h"
33 #include "blockdsp.h"
34 #include "codec_internal.h"
35 #include "get_bits.h"
36 #include "hpeldsp.h"
37 #include "internal.h"
38 #include "mathops.h"
39
40 #define BINK_FLAG_ALPHA 0x00100000
41 #define BINK_FLAG_GRAY 0x00020000
42
43 static VLC bink_trees[16];
44
45 /**
46 * IDs for different data types used in old version of Bink video codec
47 */
48 enum OldSources {
49 BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
50 BINKB_SRC_COLORS, ///< pixel values used for different block types
51 BINKB_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
52 BINKB_SRC_X_OFF, ///< X components of motion value
53 BINKB_SRC_Y_OFF, ///< Y components of motion value
54 BINKB_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
55 BINKB_SRC_INTER_DC, ///< DC values for interblocks with DCT
56 BINKB_SRC_INTRA_Q, ///< quantizer values for intrablocks with DCT
57 BINKB_SRC_INTER_Q, ///< quantizer values for interblocks with DCT
58 BINKB_SRC_INTER_COEFS, ///< number of coefficients for residue blocks
59
60 BINKB_NB_SRC
61 };
62
63 static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
64 4, 8, 8, 5, 5, 11, 11, 4, 4, 7
65 };
66
67 static const int binkb_bundle_signed[BINKB_NB_SRC] = {
68 0, 0, 0, 1, 1, 0, 1, 0, 0, 0
69 };
70
71 static int32_t binkb_intra_quant[16][64];
72 static int32_t binkb_inter_quant[16][64];
73
74 /**
75 * IDs for different data types used in Bink video codec
76 */
77 enum Sources {
78 BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
79 BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
80 BINK_SRC_COLORS, ///< pixel values used for different block types
81 BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
82 BINK_SRC_X_OFF, ///< X components of motion value
83 BINK_SRC_Y_OFF, ///< Y components of motion value
84 BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
85 BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
86 BINK_SRC_RUN, ///< run lengths for special fill block
87
88 BINK_NB_SRC
89 };
90
91 /**
92 * data needed to decode 4-bit Huffman-coded value
93 */
94 typedef struct Tree {
95 int vlc_num; ///< tree number (in bink_trees[])
96 uint8_t syms[16]; ///< leaf value to symbol mapping
97 } Tree;
98
99 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
100 bink_trees[(tree).vlc_num].bits, 1)]
101
102 /**
103 * data structure used for decoding single Bink data type
104 */
105 typedef struct Bundle {
106 int len; ///< length of number of entries to decode (in bits)
107 Tree tree; ///< Huffman tree-related data
108 uint8_t *data; ///< buffer for decoded symbols
109 uint8_t *data_end; ///< buffer end
110 uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
111 uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
112 } Bundle;
113
114 /*
115 * Decoder context
116 */
117 typedef struct BinkContext {
118 AVCodecContext *avctx;
119 BlockDSPContext bdsp;
120 op_pixels_func put_pixels_tab;
121 BinkDSPContext binkdsp;
122 AVFrame *last;
123 int version; ///< internal Bink file version
124 int has_alpha;
125 int swap_planes;
126 unsigned frame_num;
127
128 Bundle bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
129 Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
130 int col_lastval; ///< value of last decoded high nibble in "colours" data type
131 } BinkContext;
132
133 /**
134 * Bink video block types
135 */
136 enum BlockTypes {
137 SKIP_BLOCK = 0, ///< skipped block
138 SCALED_BLOCK, ///< block has size 16x16
139 MOTION_BLOCK, ///< block is copied from previous frame with some offset
140 RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
141 RESIDUE_BLOCK, ///< motion block with some difference added
142 INTRA_BLOCK, ///< intra DCT block
143 FILL_BLOCK, ///< block is filled with single colour
144 INTER_BLOCK, ///< motion block with DCT applied to the difference
145 PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
146 RAW_BLOCK, ///< uncoded 8x8 block
147 };
148
149 /**
150 * Initialize length in all bundles.
151 *
152 * @param c decoder context
153 * @param width plane width
154 * @param bw plane width in 8x8 blocks
155 */
156 153 static void init_lengths(BinkContext *c, int width, int bw)
157 {
158 153 width = FFALIGN(width, 8);
159
160 153 c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
161
162 153 c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
163
164 153 c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
165
166 153 c->bundle[BINK_SRC_INTRA_DC].len =
167 153 c->bundle[BINK_SRC_INTER_DC].len =
168 153 c->bundle[BINK_SRC_X_OFF].len =
169 153 c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
170
171 153 c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
172
173 153 c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
174 153 }
175
176 /**
177 * Allocate memory for bundles.
178 *
179 * @param c decoder context
180 */
181 9 static av_cold int init_bundles(BinkContext *c)
182 {
183 int bw, bh, blocks;
184 uint8_t *tmp;
185 int i;
186
187 9 bw = (c->avctx->width + 7) >> 3;
188 9 bh = (c->avctx->height + 7) >> 3;
189 9 blocks = bw * bh;
190
191 9 tmp = av_calloc(blocks, 64 * BINKB_NB_SRC);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!tmp)
193 return AVERROR(ENOMEM);
194
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 9 times.
99 for (i = 0; i < BINKB_NB_SRC; i++) {
195 90 c->bundle[i].data = tmp;
196 90 tmp += blocks * 64;
197 90 c->bundle[i].data_end = tmp;
198 }
199
200 9 return 0;
201 }
202
203 /**
204 * Free memory used by bundles.
205 *
206 * @param c decoder context
207 */
208 9 static av_cold void free_bundles(BinkContext *c)
209 {
210 9 av_freep(&c->bundle[0].data);
211 9 }
212
213 /**
214 * Merge two consequent lists of equal size depending on bits read.
215 *
216 * @param gb context for reading bits
217 * @param dst buffer where merged list will be written to
218 * @param src pointer to the head of the first list (the second lists starts at src+size)
219 * @param size input lists size
220 */
221 1600 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
222 {
223 1600 uint8_t *src2 = src + size;
224 1600 int size2 = size;
225
226 do {
227
2/2
✓ Branch 1 taken 2289 times.
✓ Branch 2 taken 1472 times.
3761 if (!get_bits1(gb)) {
228 2289 *dst++ = *src++;
229 2289 size--;
230 } else {
231 1472 *dst++ = *src2++;
232 1472 size2--;
233 }
234
4/4
✓ Branch 0 taken 2557 times.
✓ Branch 1 taken 1204 times.
✓ Branch 2 taken 2161 times.
✓ Branch 3 taken 396 times.
3761 } while (size && size2);
235
236
2/2
✓ Branch 0 taken 951 times.
✓ Branch 1 taken 1600 times.
2551 while (size--)
237 951 *dst++ = *src++;
238
2/2
✓ Branch 0 taken 1768 times.
✓ Branch 1 taken 1600 times.
3368 while (size2--)
239 1768 *dst++ = *src2++;
240 1600 }
241
242 /**
243 * Read information about Huffman tree used to decode data.
244 *
245 * @param gb context for reading bits
246 * @param tree pointer for storing tree data
247 */
248 3519 static int read_tree(GetBitContext *gb, Tree *tree)
249 {
250 3519 uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2;
251 int i, t, len;
252
253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3519 times.
3519 if (get_bits_left(gb) < 4)
254 return AVERROR_INVALIDDATA;
255
256 3519 tree->vlc_num = get_bits(gb, 4);
257
2/2
✓ Branch 0 taken 1539 times.
✓ Branch 1 taken 1980 times.
3519 if (!tree->vlc_num) {
258
2/2
✓ Branch 0 taken 24624 times.
✓ Branch 1 taken 1539 times.
26163 for (i = 0; i < 16; i++)
259 24624 tree->syms[i] = i;
260 1539 return 0;
261 }
262
2/2
✓ Branch 1 taken 1871 times.
✓ Branch 2 taken 109 times.
1980 if (get_bits1(gb)) {
263 1871 len = get_bits(gb, 3);
264
2/2
✓ Branch 0 taken 6542 times.
✓ Branch 1 taken 1871 times.
8413 for (i = 0; i <= len; i++) {
265 6542 tree->syms[i] = get_bits(gb, 4);
266 6542 tmp1[tree->syms[i]] = 1;
267 }
268
4/4
✓ Branch 0 taken 29464 times.
✓ Branch 1 taken 1486 times.
✓ Branch 2 taken 29079 times.
✓ Branch 3 taken 385 times.
30950 for (i = 0; i < 16 && len < 16 - 1; i++)
269
2/2
✓ Branch 0 taken 23394 times.
✓ Branch 1 taken 5685 times.
29079 if (!tmp1[i])
270 23394 tree->syms[++len] = i;
271 } else {
272 109 len = get_bits(gb, 2);
273
2/2
✓ Branch 0 taken 1744 times.
✓ Branch 1 taken 109 times.
1853 for (i = 0; i < 16; i++)
274 1744 in[i] = i;
275
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 109 times.
514 for (i = 0; i <= len; i++) {
276 405 int size = 1 << i;
277
2/2
✓ Branch 0 taken 1600 times.
✓ Branch 1 taken 405 times.
2005 for (t = 0; t < 16; t += size << 1)
278 1600 merge(gb, out + t, in + t, size);
279 405 FFSWAP(uint8_t*, in, out);
280 }
281 109 memcpy(tree->syms, in, 16);
282 }
283 1980 return 0;
284 }
285
286 /**
287 * Prepare bundle for decoding data.
288 *
289 * @param gb context for reading bits
290 * @param c decoder context
291 * @param bundle_num number of the bundle to initialize
292 */
293 1377 static int read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
294 {
295 int i;
296
297
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 1224 times.
1377 if (bundle_num == BINK_SRC_COLORS) {
298
2/2
✓ Branch 0 taken 2448 times.
✓ Branch 1 taken 153 times.
2601 for (i = 0; i < 16; i++) {
299 2448 int ret = read_tree(gb, &c->col_high[i]);
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2448 times.
2448 if (ret < 0)
301 return ret;
302 }
303 153 c->col_lastval = 0;
304 }
305
4/4
✓ Branch 0 taken 1224 times.
✓ Branch 1 taken 153 times.
✓ Branch 2 taken 1071 times.
✓ Branch 3 taken 153 times.
1377 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC) {
306 1071 int ret = read_tree(gb, &c->bundle[bundle_num].tree);
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1071 times.
1071 if (ret < 0)
308 return ret;
309 }
310 1377 c->bundle[bundle_num].cur_dec =
311 1377 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
312
313 1377 return 0;
314 }
315
316 /**
317 * common check before starting decoding bundle data
318 *
319 * @param gb context for reading bits
320 * @param b bundle
321 * @param t variable where number of elements to decode will be stored
322 */
323 #define CHECK_READ_VAL(gb, b, t) \
324 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
325 return 0; \
326 t = get_bits(gb, b->len); \
327 if (!t) { \
328 b->cur_dec = NULL; \
329 return 0; \
330 } \
331
332 6120 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
333 {
334 int t, v;
335 const uint8_t *dec_end;
336
337
6/6
✓ Branch 0 taken 5773 times.
✓ Branch 1 taken 347 times.
✓ Branch 2 taken 5394 times.
✓ Branch 3 taken 379 times.
✓ Branch 5 taken 94 times.
✓ Branch 6 taken 285 times.
6120 CHECK_READ_VAL(gb, b, t);
338 285 dec_end = b->cur_dec + t;
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 285 times.
285 if (dec_end > b->data_end) {
340 av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
341 return AVERROR_INVALIDDATA;
342 }
343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 285 times.
285 if (get_bits_left(gb) < 1)
344 return AVERROR_INVALIDDATA;
345
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 285 times.
285 if (get_bits1(gb)) {
346 v = get_bits(gb, 4);
347 memset(b->cur_dec, v, t);
348 b->cur_dec += t;
349 } else {
350
2/2
✓ Branch 0 taken 96813 times.
✓ Branch 1 taken 285 times.
97098 while (b->cur_dec < dec_end)
351 96813 *b->cur_dec++ = GET_HUFF(gb, b->tree);
352 }
353 285 return 0;
354 }
355
356 12240 static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
357 {
358 int t, sign, v;
359 const uint8_t *dec_end;
360
361
6/6
✓ Branch 0 taken 11772 times.
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 11188 times.
✓ Branch 3 taken 584 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 572 times.
12240 CHECK_READ_VAL(gb, b, t);
362 572 dec_end = b->cur_dec + t;
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 572 times.
572 if (dec_end > b->data_end) {
364 av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
365 return AVERROR_INVALIDDATA;
366 }
367
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 572 times.
572 if (get_bits_left(gb) < 1)
368 return AVERROR_INVALIDDATA;
369
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 572 times.
572 if (get_bits1(gb)) {
370 v = get_bits(gb, 4);
371 if (v) {
372 sign = -get_bits1(gb);
373 v = (v ^ sign) - sign;
374 }
375 memset(b->cur_dec, v, t);
376 b->cur_dec += t;
377 } else {
378
2/2
✓ Branch 0 taken 220592 times.
✓ Branch 1 taken 572 times.
221164 while (b->cur_dec < dec_end) {
379 220592 v = GET_HUFF(gb, b->tree);
380
2/2
✓ Branch 0 taken 207492 times.
✓ Branch 1 taken 13100 times.
220592 if (v) {
381 207492 sign = -get_bits1(gb);
382 207492 v = (v ^ sign) - sign;
383 }
384 220592 *b->cur_dec++ = v;
385 }
386 }
387 572 return 0;
388 }
389
390 static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
391
392 12240 static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
393 {
394 12240 BinkContext * const c = avctx->priv_data;
395 int t, v;
396 12240 int last = 0;
397 const uint8_t *dec_end;
398
399
6/6
✓ Branch 0 taken 12049 times.
✓ Branch 1 taken 191 times.
✓ Branch 2 taken 11064 times.
✓ Branch 3 taken 985 times.
✓ Branch 5 taken 153 times.
✓ Branch 6 taken 832 times.
12240 CHECK_READ_VAL(gb, b, t);
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 832 times.
832 if (c->version == 'k') {
401 t ^= 0xBBu;
402 if (t == 0) {
403 b->cur_dec = NULL;
404 return 0;
405 }
406 }
407 832 dec_end = b->cur_dec + t;
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 832 times.
832 if (dec_end > b->data_end) {
409 av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
410 return AVERROR_INVALIDDATA;
411 }
412
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 832 times.
832 if (get_bits_left(gb) < 1)
413 return AVERROR_INVALIDDATA;
414
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 832 times.
832 if (get_bits1(gb)) {
415 v = get_bits(gb, 4);
416 memset(b->cur_dec, v, t);
417 b->cur_dec += t;
418 } else {
419
2/2
✓ Branch 0 taken 251420 times.
✓ Branch 1 taken 832 times.
252252 while (b->cur_dec < dec_end) {
420 251420 v = GET_HUFF(gb, b->tree);
421
2/2
✓ Branch 0 taken 238193 times.
✓ Branch 1 taken 13227 times.
251420 if (v < 12) {
422 238193 last = v;
423 238193 *b->cur_dec++ = v;
424 } else {
425 13227 int run = bink_rlelens[v - 12];
426
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13227 times.
13227 if (dec_end - b->cur_dec < run)
428 return AVERROR_INVALIDDATA;
429 13227 memset(b->cur_dec, last, run);
430 13227 b->cur_dec += run;
431 }
432 }
433 }
434 832 return 0;
435 }
436
437 6120 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
438 {
439 int t, v;
440 const uint8_t *dec_end;
441
442
6/6
✓ Branch 0 taken 5596 times.
✓ Branch 1 taken 524 times.
✓ Branch 2 taken 5295 times.
✓ Branch 3 taken 301 times.
✓ Branch 5 taken 95 times.
✓ Branch 6 taken 206 times.
6120 CHECK_READ_VAL(gb, b, t);
443 206 dec_end = b->cur_dec + t;
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 if (dec_end > b->data_end) {
445 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
446 return AVERROR_INVALIDDATA;
447 }
448
2/2
✓ Branch 0 taken 53296 times.
✓ Branch 1 taken 206 times.
53502 while (b->cur_dec < dec_end) {
449
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 53296 times.
53296 if (get_bits_left(gb) < 2)
450 return AVERROR_INVALIDDATA;
451 53296 v = GET_HUFF(gb, b->tree);
452 53296 v |= GET_HUFF(gb, b->tree) << 4;
453 53296 *b->cur_dec++ = v;
454 }
455
456 206 return 0;
457 }
458
459 6120 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
460 {
461 int t, sign, v;
462 const uint8_t *dec_end;
463
464
6/6
✓ Branch 0 taken 6115 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5694 times.
✓ Branch 3 taken 421 times.
✓ Branch 5 taken 11 times.
✓ Branch 6 taken 410 times.
6120 CHECK_READ_VAL(gb, b, t);
465 410 dec_end = b->cur_dec + t;
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410 times.
410 if (dec_end > b->data_end) {
467 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
468 return AVERROR_INVALIDDATA;
469 }
470
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 410 times.
410 if (get_bits_left(gb) < 1)
471 return AVERROR_INVALIDDATA;
472
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 410 times.
410 if (get_bits1(gb)) {
473 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
474 v = GET_HUFF(gb, b->tree);
475 v = (c->col_lastval << 4) | v;
476 if (c->version < 'i') {
477 sign = ((int8_t) v) >> 7;
478 v = ((v & 0x7F) ^ sign) - sign;
479 v += 0x80;
480 }
481 memset(b->cur_dec, v, t);
482 b->cur_dec += t;
483 } else {
484
2/2
✓ Branch 0 taken 190872 times.
✓ Branch 1 taken 410 times.
191282 while (b->cur_dec < dec_end) {
485
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 190872 times.
190872 if (get_bits_left(gb) < 2)
486 return AVERROR_INVALIDDATA;
487 190872 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
488 190872 v = GET_HUFF(gb, b->tree);
489 190872 v = (c->col_lastval << 4) | v;
490
2/2
✓ Branch 0 taken 148464 times.
✓ Branch 1 taken 42408 times.
190872 if (c->version < 'i') {
491 148464 sign = ((int8_t) v) >> 7;
492 148464 v = ((v & 0x7F) ^ sign) - sign;
493 148464 v += 0x80;
494 }
495 190872 *b->cur_dec++ = v;
496 }
497 }
498 410 return 0;
499 }
500
501 /** number of bits used to store first DC value in bundle */
502 #define DC_START_BITS 11
503
504 12240 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
505 int start_bits, int has_sign)
506 {
507 int i, j, len, len2, bsize, sign, v, v2;
508 12240 int16_t *dst = (int16_t*)b->cur_dec;
509 12240 int16_t *dst_end = (int16_t*)b->data_end;
510
511
6/6
✓ Branch 0 taken 11664 times.
✓ Branch 1 taken 576 times.
✓ Branch 2 taken 11230 times.
✓ Branch 3 taken 434 times.
✓ Branch 5 taken 102 times.
✓ Branch 6 taken 332 times.
12240 CHECK_READ_VAL(gb, b, len);
512
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 332 times.
332 if (get_bits_left(gb) < start_bits - has_sign)
513 return AVERROR_INVALIDDATA;
514 332 v = get_bits(gb, start_bits - has_sign);
515
4/4
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 129 times.
✓ Branch 3 taken 185 times.
332 if (v && has_sign) {
516 129 sign = -get_bits1(gb);
517 129 v = (v ^ sign) - sign;
518 }
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 332 times.
332 if (dst_end - dst < 1)
520 return AVERROR_INVALIDDATA;
521 332 *dst++ = v;
522 332 len--;
523
2/2
✓ Branch 0 taken 5793 times.
✓ Branch 1 taken 332 times.
6125 for (i = 0; i < len; i += 8) {
524 5793 len2 = FFMIN(len - i, 8);
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5793 times.
5793 if (dst_end - dst < len2)
526 return AVERROR_INVALIDDATA;
527 5793 bsize = get_bits(gb, 4);
528
2/2
✓ Branch 0 taken 5789 times.
✓ Branch 1 taken 4 times.
5793 if (bsize) {
529
2/2
✓ Branch 0 taken 45153 times.
✓ Branch 1 taken 5789 times.
50942 for (j = 0; j < len2; j++) {
530 45153 v2 = get_bits(gb, bsize);
531
2/2
✓ Branch 0 taken 42581 times.
✓ Branch 1 taken 2572 times.
45153 if (v2) {
532 42581 sign = -get_bits1(gb);
533 42581 v2 = (v2 ^ sign) - sign;
534 }
535 45153 v += v2;
536 45153 *dst++ = v;
537
2/4
✓ Branch 0 taken 45153 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45153 times.
45153 if (v < -32768 || v > 32767) {
538 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
539 return AVERROR_INVALIDDATA;
540 }
541 }
542 } else {
543
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
22 for (j = 0; j < len2; j++)
544 18 *dst++ = v;
545 }
546 }
547
548 332 b->cur_dec = (uint8_t*)dst;
549 332 return 0;
550 }
551
552 /**
553 * Retrieve next value from bundle.
554 *
555 * @param c decoder context
556 * @param bundle bundle number
557 */
558 939741 static inline int get_value(BinkContext *c, int bundle)
559 {
560 int ret;
561
562
4/4
✓ Branch 0 taken 362908 times.
✓ Branch 1 taken 576833 times.
✓ Branch 2 taken 96813 times.
✓ Branch 3 taken 266095 times.
939741 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
563 673646 return *c->bundle[bundle].cur_ptr++;
564
4/4
✓ Branch 0 taken 155799 times.
✓ Branch 1 taken 110296 times.
✓ Branch 2 taken 110296 times.
✓ Branch 3 taken 45503 times.
266095 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
565 220592 return (int8_t)*c->bundle[bundle].cur_ptr++;
566 45503 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
567 45503 c->bundle[bundle].cur_ptr += 2;
568 45503 return ret;
569 }
570
571 900 static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num)
572 {
573 900 c->bundle[bundle_num].cur_dec =
574 900 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
575 900 c->bundle[bundle_num].len = 13;
576 900 }
577
578 90 static av_cold void binkb_init_bundles(BinkContext *c)
579 {
580 int i;
581
2/2
✓ Branch 0 taken 900 times.
✓ Branch 1 taken 90 times.
990 for (i = 0; i < BINKB_NB_SRC; i++)
582 900 binkb_init_bundle(c, i);
583 90 }
584
585 9300 static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
586 {
587 9300 const int bits = binkb_bundle_sizes[bundle_num];
588 9300 const int mask = 1 << (bits - 1);
589 9300 const int issigned = binkb_bundle_signed[bundle_num];
590 9300 Bundle *b = &c->bundle[bundle_num];
591 int i, len;
592
593
6/6
✓ Branch 0 taken 4745 times.
✓ Branch 1 taken 4555 times.
✓ Branch 2 taken 3784 times.
✓ Branch 3 taken 961 times.
✓ Branch 5 taken 493 times.
✓ Branch 6 taken 468 times.
9300 CHECK_READ_VAL(gb, b, len);
594
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 463 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 468 times.
468 if (b->data_end - b->cur_dec < len * (1 + (bits > 8)))
595 return AVERROR_INVALIDDATA;
596
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 5 times.
468 if (bits <= 8) {
597
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 180 times.
463 if (!issigned) {
598
2/2
✓ Branch 0 taken 39801 times.
✓ Branch 1 taken 283 times.
40084 for (i = 0; i < len; i++)
599 39801 *b->cur_dec++ = get_bits(gb, bits);
600 } else {
601
2/2
✓ Branch 0 taken 27170 times.
✓ Branch 1 taken 180 times.
27350 for (i = 0; i < len; i++)
602 27170 *b->cur_dec++ = get_bits(gb, bits) - mask;
603 }
604 } else {
605 5 int16_t *dst = (int16_t*)b->cur_dec;
606
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!issigned) {
608 for (i = 0; i < len; i++)
609 *dst++ = get_bits(gb, bits);
610 } else {
611
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 for (i = 0; i < len; i++)
612 5 *dst++ = get_bits(gb, bits) - mask;
613 }
614 5 b->cur_dec = (uint8_t*)dst;
615 }
616 468 return 0;
617 }
618
619 61472 static inline int binkb_get_value(BinkContext *c, int bundle_num)
620 {
621 int16_t ret;
622 61472 const int bits = binkb_bundle_sizes[bundle_num];
623
624
2/2
✓ Branch 0 taken 61467 times.
✓ Branch 1 taken 5 times.
61472 if (bits <= 8) {
625 61467 int val = *c->bundle[bundle_num].cur_ptr++;
626
2/2
✓ Branch 0 taken 27170 times.
✓ Branch 1 taken 34297 times.
61467 return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
627 }
628 5 ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
629 5 c->bundle[bundle_num].cur_ptr += 2;
630 5 return ret;
631 }
632
633 /**
634 * Read 8x8 block of DCT coefficients.
635 *
636 * @param gb context for reading bits
637 * @param block place for storing coefficients
638 * @param scan scan order table
639 * @param quant_matrices quantization matrices
640 * @return 0 for success, negative value in other cases
641 */
642 45508 static int read_dct_coeffs(BinkContext *c, GetBitContext *gb, int32_t block[64],
643 const uint8_t *scan, int *coef_count_,
644 int coef_idx[64], int q)
645 {
646 int coef_list[128];
647 int mode_list[128];
648 int i, t, bits, ccoef, mode, sign;
649 45508 int list_start = 64, list_end = 64, list_pos;
650 45508 int coef_count = 0;
651 int quant_idx;
652
653
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45508 times.
45508 if (get_bits_left(gb) < 4)
654 return AVERROR_INVALIDDATA;
655
656 45508 coef_list[list_end] = 4; mode_list[list_end++] = 0;
657 45508 coef_list[list_end] = 24; mode_list[list_end++] = 0;
658 45508 coef_list[list_end] = 44; mode_list[list_end++] = 0;
659 45508 coef_list[list_end] = 1; mode_list[list_end++] = 3;
660 45508 coef_list[list_end] = 2; mode_list[list_end++] = 3;
661 45508 coef_list[list_end] = 3; mode_list[list_end++] = 3;
662
663
2/2
✓ Branch 1 taken 101534 times.
✓ Branch 2 taken 45508 times.
147042 for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) {
664 101534 list_pos = list_start;
665
2/2
✓ Branch 0 taken 982733 times.
✓ Branch 1 taken 101534 times.
1084267 while (list_pos < list_end) {
666
4/4
✓ Branch 0 taken 846976 times.
✓ Branch 1 taken 135757 times.
✓ Branch 3 taken 535282 times.
✓ Branch 4 taken 311694 times.
982733 if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
667 671039 list_pos++;
668 671039 continue;
669 }
670 311694 ccoef = coef_list[list_pos];
671 311694 mode = mode_list[list_pos];
672
4/5
✓ Branch 0 taken 66858 times.
✓ Branch 1 taken 54324 times.
✓ Branch 2 taken 25484 times.
✓ Branch 3 taken 165028 times.
✗ Branch 4 not taken.
311694 switch (mode) {
673 66858 case 0:
674 66858 coef_list[list_pos] = ccoef + 4;
675 66858 mode_list[list_pos] = 1;
676 121182 case 2:
677
2/2
✓ Branch 0 taken 54324 times.
✓ Branch 1 taken 66858 times.
121182 if (mode == 2) {
678 54324 coef_list[list_pos] = 0;
679 54324 mode_list[list_pos++] = 0;
680 }
681
2/2
✓ Branch 0 taken 484728 times.
✓ Branch 1 taken 121182 times.
605910 for (i = 0; i < 4; i++, ccoef++) {
682
2/2
✓ Branch 1 taken 291256 times.
✓ Branch 2 taken 193472 times.
484728 if (get_bits1(gb)) {
683 291256 coef_list[--list_start] = ccoef;
684 291256 mode_list[ list_start] = 3;
685 } else {
686
2/2
✓ Branch 0 taken 131103 times.
✓ Branch 1 taken 62369 times.
193472 if (!bits) {
687 131103 t = 1 - (get_bits1(gb) << 1);
688 } else {
689 62369 t = get_bits(gb, bits) | 1 << bits;
690 62369 sign = -get_bits1(gb);
691 62369 t = (t ^ sign) - sign;
692 }
693 193472 block[scan[ccoef]] = t;
694 193472 coef_idx[coef_count++] = ccoef;
695 }
696 }
697 121182 break;
698 25484 case 1:
699 25484 mode_list[list_pos] = 2;
700
2/2
✓ Branch 0 taken 76452 times.
✓ Branch 1 taken 25484 times.
101936 for (i = 0; i < 3; i++) {
701 76452 ccoef += 4;
702 76452 coef_list[list_end] = ccoef;
703 76452 mode_list[list_end++] = 2;
704 }
705 25484 break;
706 165028 case 3:
707
2/2
✓ Branch 0 taken 92955 times.
✓ Branch 1 taken 72073 times.
165028 if (!bits) {
708 92955 t = 1 - (get_bits1(gb) << 1);
709 } else {
710 72073 t = get_bits(gb, bits) | 1 << bits;
711 72073 sign = -get_bits1(gb);
712 72073 t = (t ^ sign) - sign;
713 }
714 165028 block[scan[ccoef]] = t;
715 165028 coef_idx[coef_count++] = ccoef;
716 165028 coef_list[list_pos] = 0;
717 165028 mode_list[list_pos++] = 0;
718 165028 break;
719 }
720 }
721 }
722
723
2/2
✓ Branch 0 taken 45503 times.
✓ Branch 1 taken 5 times.
45508 if (q == -1) {
724 45503 quant_idx = get_bits(gb, 4);
725 } else {
726 5 quant_idx = q;
727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (quant_idx > 15U) {
728 av_log(c->avctx, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx);
729 return AVERROR_INVALIDDATA;
730 }
731 }
732
733 45508 *coef_count_ = coef_count;
734
735 45508 return quant_idx;
736 }
737
738 45508 static void unquantize_dct_coeffs(int32_t block[64], const uint32_t quant[64],
739 int coef_count, int coef_idx[64],
740 const uint8_t *scan)
741 {
742 int i;
743 45508 block[0] = (int)(block[0] * quant[0]) >> 11;
744
2/2
✓ Branch 0 taken 358500 times.
✓ Branch 1 taken 45508 times.
404008 for (i = 0; i < coef_count; i++) {
745 358500 int idx = coef_idx[i];
746 358500 block[scan[idx]] = (int)(block[scan[idx]] * quant[idx]) >> 11;
747 }
748 45508 }
749
750 /**
751 * Read 8x8 block with residue after motion compensation.
752 *
753 * @param gb context for reading bits
754 * @param block place to store read data
755 * @param masks_count number of masks to decode
756 * @return 0 on success, negative value in other cases
757 */
758 17330 static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count)
759 {
760 int coef_list[128];
761 int mode_list[128];
762 int i, sign, mask, ccoef, mode;
763 17330 int list_start = 64, list_end = 64, list_pos;
764 int nz_coeff[64];
765 17330 int nz_coeff_count = 0;
766
767 17330 coef_list[list_end] = 4; mode_list[list_end++] = 0;
768 17330 coef_list[list_end] = 24; mode_list[list_end++] = 0;
769 17330 coef_list[list_end] = 44; mode_list[list_end++] = 0;
770 17330 coef_list[list_end] = 0; mode_list[list_end++] = 2;
771
772
1/2
✓ Branch 1 taken 26427 times.
✗ Branch 2 not taken.
26427 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
773
2/2
✓ Branch 0 taken 67080 times.
✓ Branch 1 taken 25514 times.
92594 for (i = 0; i < nz_coeff_count; i++) {
774
2/2
✓ Branch 1 taken 42197 times.
✓ Branch 2 taken 24883 times.
67080 if (!get_bits1(gb))
775 42197 continue;
776
2/2
✓ Branch 0 taken 11567 times.
✓ Branch 1 taken 13316 times.
24883 if (block[nz_coeff[i]] < 0)
777 11567 block[nz_coeff[i]] -= mask;
778 else
779 13316 block[nz_coeff[i]] += mask;
780 24883 masks_count--;
781
2/2
✓ Branch 0 taken 913 times.
✓ Branch 1 taken 23970 times.
24883 if (masks_count < 0)
782 913 return 0;
783 }
784 25514 list_pos = list_start;
785
2/2
✓ Branch 0 taken 347825 times.
✓ Branch 1 taken 9097 times.
356922 while (list_pos < list_end) {
786
4/4
✓ Branch 0 taken 316737 times.
✓ Branch 1 taken 31088 times.
✓ Branch 3 taken 163484 times.
✓ Branch 4 taken 153253 times.
347825 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
787 194572 list_pos++;
788 194572 continue;
789 }
790 153253 ccoef = coef_list[list_pos];
791 153253 mode = mode_list[list_pos];
792
4/5
✓ Branch 0 taken 33694 times.
✓ Branch 1 taken 58279 times.
✓ Branch 2 taken 29117 times.
✓ Branch 3 taken 32163 times.
✗ Branch 4 not taken.
153253 switch (mode) {
793 33694 case 0:
794 33694 coef_list[list_pos] = ccoef + 4;
795 33694 mode_list[list_pos] = 1;
796 91973 case 2:
797
2/2
✓ Branch 0 taken 58279 times.
✓ Branch 1 taken 33694 times.
91973 if (mode == 2) {
798 58279 coef_list[list_pos] = 0;
799 58279 mode_list[list_pos++] = 0;
800 }
801
2/2
✓ Branch 0 taken 346698 times.
✓ Branch 1 taken 76987 times.
423685 for (i = 0; i < 4; i++, ccoef++) {
802
2/2
✓ Branch 1 taken 249374 times.
✓ Branch 2 taken 97324 times.
346698 if (get_bits1(gb)) {
803 249374 coef_list[--list_start] = ccoef;
804 249374 mode_list[ list_start] = 3;
805 } else {
806 97324 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
807 97324 sign = -get_bits1(gb);
808 97324 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
809 97324 masks_count--;
810
2/2
✓ Branch 0 taken 14986 times.
✓ Branch 1 taken 82338 times.
97324 if (masks_count < 0)
811 14986 return 0;
812 }
813 }
814 76987 break;
815 29117 case 1:
816 29117 mode_list[list_pos] = 2;
817
2/2
✓ Branch 0 taken 87351 times.
✓ Branch 1 taken 29117 times.
116468 for (i = 0; i < 3; i++) {
818 87351 ccoef += 4;
819 87351 coef_list[list_end] = ccoef;
820 87351 mode_list[list_end++] = 2;
821 }
822 29117 break;
823 32163 case 3:
824 32163 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
825 32163 sign = -get_bits1(gb);
826 32163 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
827 32163 coef_list[list_pos] = 0;
828 32163 mode_list[list_pos++] = 0;
829 32163 masks_count--;
830
2/2
✓ Branch 0 taken 1431 times.
✓ Branch 1 taken 30732 times.
32163 if (masks_count < 0)
831 1431 return 0;
832 30732 break;
833 }
834 }
835 }
836
837 return 0;
838 }
839
840 /**
841 * Copy 8x8 block from source to destination, where src and dst may be overlapped
842 */
843 13417 static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
844 {
845 uint8_t tmp[64];
846 int i;
847
2/2
✓ Branch 0 taken 107336 times.
✓ Branch 1 taken 13417 times.
120753 for (i = 0; i < 8; i++)
848 107336 memcpy(tmp + i*8, src + i*stride, 8);
849
2/2
✓ Branch 0 taken 107336 times.
✓ Branch 1 taken 13417 times.
120753 for (i = 0; i < 8; i++)
850 107336 memcpy(dst + i*stride, tmp + i*8, 8);
851 13417 }
852
853 90 static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
854 int plane_idx, int is_key, int is_chroma)
855 {
856 int blk, ret;
857 int i, j, bx, by;
858 uint8_t *dst, *ref, *ref_start, *ref_end;
859 int v, col[2];
860 const uint8_t *scan;
861 int xoff, yoff;
862 90 LOCAL_ALIGNED_32(int16_t, block, [64]);
863 90 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
864 int coordmap[64];
865
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 87 times.
90 int ybias = is_key ? -15 : 0;
866 int qp, quant_idx, coef_count, coef_idx[64];
867
868 90 const int stride = frame->linesize[plane_idx];
869
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
870
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
871
872 90 binkb_init_bundles(c);
873 90 ref_start = frame->data[plane_idx];
874 90 ref_end = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
875
876
2/2
✓ Branch 0 taken 5760 times.
✓ Branch 1 taken 90 times.
5850 for (i = 0; i < 64; i++)
877 5760 coordmap[i] = (i & 7) + (i >> 3) * stride;
878
879
2/2
✓ Branch 0 taken 930 times.
✓ Branch 1 taken 90 times.
1020 for (by = 0; by < bh; by++) {
880
2/2
✓ Branch 0 taken 9300 times.
✓ Branch 1 taken 930 times.
10230 for (i = 0; i < BINKB_NB_SRC; i++) {
881
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9300 times.
9300 if ((ret = binkb_read_bundle(c, gb, i)) < 0)
882 return ret;
883 }
884
885 930 dst = frame->data[plane_idx] + 8*by*stride;
886
2/2
✓ Branch 0 taken 17490 times.
✓ Branch 1 taken 930 times.
18420 for (bx = 0; bx < bw; bx++, dst += 8) {
887 17490 blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
888
7/10
✓ Branch 0 taken 3593 times.
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13580 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 27 times.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 86 times.
✗ Branch 9 not taken.
17490 switch (blk) {
889 3593 case 0:
890 3593 break;
891 184 case 1:
892 184 scan = bink_patterns[get_bits(gb, 4)];
893 184 i = 0;
894 do {
895 int mode, run;
896
897 1732 mode = get_bits1(gb);
898 1732 run = get_bits(gb, binkb_runbits[i]) + 1;
899
900 1732 i += run;
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1732 times.
1732 if (i > 64) {
902 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
903 return AVERROR_INVALIDDATA;
904 }
905
2/2
✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 560 times.
1732 if (mode) {
906 1172 v = binkb_get_value(c, BINKB_SRC_COLORS);
907
2/2
✓ Branch 0 taken 9903 times.
✓ Branch 1 taken 1172 times.
11075 for (j = 0; j < run; j++)
908 9903 dst[coordmap[*scan++]] = v;
909 } else {
910
2/2
✓ Branch 0 taken 1828 times.
✓ Branch 1 taken 560 times.
2388 for (j = 0; j < run; j++)
911 1828 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
912 }
913
2/2
✓ Branch 0 taken 1548 times.
✓ Branch 1 taken 184 times.
1732 } while (i < 63);
914
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 139 times.
184 if (i == 63)
915 45 dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
916 184 break;
917 case 2:
918 memset(dctblock, 0, sizeof(*dctblock) * 64);
919 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
920 qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
921 if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
922 return quant_idx;
923 unquantize_dct_coeffs(dctblock, binkb_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
924 c->binkdsp.idct_put(dst, stride, dctblock);
925 break;
926 13580 case 3:
927 13580 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
928 13580 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
929 13580 ref = dst + xoff + yoff * stride;
930
2/4
✓ Branch 0 taken 13580 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13580 times.
13580 if (ref < ref_start || ref + 8*stride > ref_end) {
931 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
932
4/4
✓ Branch 0 taken 13413 times.
✓ Branch 1 taken 167 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 13412 times.
13580 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
933 168 c->put_pixels_tab(dst, ref, stride, 8);
934 } else {
935 13412 put_pixels8x8_overlapped(dst, ref, stride);
936 }
937 13580 c->bdsp.clear_block(block);
938 13580 v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
939 13580 read_residue(gb, block, v);
940 13580 c->binkdsp.add_pixels8(dst, block, stride);
941 13580 break;
942 5 case 4:
943 5 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
944 5 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
945 5 ref = dst + xoff + yoff * stride;
946
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (ref < ref_start || ref + 8 * stride > ref_end) {
947 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
948
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
949 c->put_pixels_tab(dst, ref, stride, 8);
950 } else {
951 5 put_pixels8x8_overlapped(dst, ref, stride);
952 }
953 5 memset(dctblock, 0, sizeof(*dctblock) * 64);
954 5 dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
955 5 qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
956
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0)
957 return quant_idx;
958 5 unquantize_dct_coeffs(dctblock, binkb_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
959 5 c->binkdsp.idct_add(dst, stride, dctblock);
960 5 break;
961 27 case 5:
962 27 v = binkb_get_value(c, BINKB_SRC_COLORS);
963 27 c->bdsp.fill_block_tab[1](dst, v, stride, 8);
964 27 break;
965 15 case 6:
966
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 15 times.
45 for (i = 0; i < 2; i++)
967 30 col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
968
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 15 times.
135 for (i = 0; i < 8; i++) {
969 120 v = binkb_get_value(c, BINKB_SRC_PATTERN);
970
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 120 times.
1080 for (j = 0; j < 8; j++, v >>= 1)
971 960 dst[i*stride + j] = col[v & 1];
972 }
973 15 break;
974 case 7:
975 xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
976 yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
977 ref = dst + xoff + yoff * stride;
978 if (ref < ref_start || ref + 8 * stride > ref_end) {
979 av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
980 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
981 c->put_pixels_tab(dst, ref, stride, 8);
982 } else {
983 put_pixels8x8_overlapped(dst, ref, stride);
984 }
985 break;
986 86 case 8:
987
2/2
✓ Branch 0 taken 688 times.
✓ Branch 1 taken 86 times.
774 for (i = 0; i < 8; i++)
988 688 memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
989 86 c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
990 86 break;
991 default:
992 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
993 return AVERROR_INVALIDDATA;
994 }
995 }
996 }
997
2/2
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 3 times.
90 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
998 87 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
999
1000 90 return 0;
1001 }
1002
1003 110296 static int bink_put_pixels(BinkContext *c,
1004 uint8_t *dst, uint8_t *prev, int stride,
1005 uint8_t *ref_start,
1006 uint8_t *ref_end)
1007 {
1008 110296 int xoff = get_value(c, BINK_SRC_X_OFF);
1009 110296 int yoff = get_value(c, BINK_SRC_Y_OFF);
1010 110296 uint8_t *ref = prev + xoff + yoff * stride;
1011
2/4
✓ Branch 0 taken 110296 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 110296 times.
110296 if (ref < ref_start || ref > ref_end) {
1012 av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
1013 xoff, yoff);
1014 return AVERROR_INVALIDDATA;
1015 }
1016 110296 c->put_pixels_tab(dst, ref, stride, 8);
1017
1018 110296 return 0;
1019 }
1020
1021 153 static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
1022 int plane_idx, int is_chroma)
1023 {
1024 int blk, ret;
1025 int i, j, bx, by;
1026 uint8_t *dst, *prev, *ref_start, *ref_end;
1027 int v, col[2];
1028 const uint8_t *scan;
1029 153 LOCAL_ALIGNED_32(int16_t, block, [64]);
1030 153 LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
1031 153 LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
1032 int coordmap[64], quant_idx, coef_count, coef_idx[64];
1033
1034 153 const int stride = frame->linesize[plane_idx];
1035
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 51 times.
153 int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
1036
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 51 times.
153 int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
1037 153 int width = c->avctx->width >> is_chroma;
1038 153 int height = c->avctx->height >> is_chroma;
1039
1040
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
153 if (c->version == 'k' && get_bits1(gb)) {
1041 int fill = get_bits(gb, 8);
1042
1043 dst = frame->data[plane_idx];
1044
1045 for (i = 0; i < height; i++)
1046 memset(dst + i * stride, fill, width);
1047 goto end;
1048 }
1049
1050 153 init_lengths(c, FFMAX(width, 8), bw);
1051
2/2
✓ Branch 0 taken 1377 times.
✓ Branch 1 taken 153 times.
1530 for (i = 0; i < BINK_NB_SRC; i++) {
1052 1377 ret = read_bundle(gb, c, i);
1053
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1377 times.
1377 if (ret < 0)
1054 return ret;
1055 }
1056
1057 453 ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
1058
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 6 times.
153 : frame->data[plane_idx];
1059 153 ref_end = ref_start
1060 153 + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
1061
1062
2/2
✓ Branch 0 taken 9792 times.
✓ Branch 1 taken 153 times.
9945 for (i = 0; i < 64; i++)
1063 9792 coordmap[i] = (i & 7) + (i >> 3) * stride;
1064
1065
2/2
✓ Branch 0 taken 6120 times.
✓ Branch 1 taken 153 times.
6273 for (by = 0; by < bh; by++) {
1066
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
6120 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0)
1067 return ret;
1068
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
6120 if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0)
1069 return ret;
1070
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
6120 if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0)
1071 return ret;
1072
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
6120 if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0)
1073 return ret;
1074
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
6120 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0)
1075 return ret;
1076
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
6120 if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0)
1077 return ret;
1078
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
6120 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0)
1079 return ret;
1080
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
6120 if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0)
1081 return ret;
1082
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
6120 if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0)
1083 return ret;
1084
1085 6120 dst = frame->data[plane_idx] + 8*by*stride;
1086 18120 prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
1087
2/2
✓ Branch 0 taken 5880 times.
✓ Branch 1 taken 240 times.
6120 : frame->data[plane_idx]) + 8*by*stride;
1088
2/2
✓ Branch 0 taken 314770 times.
✓ Branch 1 taken 6120 times.
320890 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1089 314770 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
1090 // 16x16 block type on odd line means part of the already decoded block, so skip it
1091
4/4
✓ Branch 0 taken 157385 times.
✓ Branch 1 taken 157385 times.
✓ Branch 2 taken 26215 times.
✓ Branch 3 taken 131170 times.
314770 if ((by & 1) && blk == SCALED_BLOCK) {
1092 26215 bx++;
1093 26215 dst += 8;
1094 26215 prev += 8;
1095 26215 continue;
1096 }
1097
10/11
✓ Branch 0 taken 85550 times.
✓ Branch 1 taken 26215 times.
✓ Branch 2 taken 97378 times.
✓ Branch 3 taken 11625 times.
✓ Branch 4 taken 3750 times.
✓ Branch 5 taken 16881 times.
✓ Branch 6 taken 31947 times.
✓ Branch 7 taken 9168 times.
✓ Branch 8 taken 5911 times.
✓ Branch 9 taken 130 times.
✗ Branch 10 not taken.
288555 switch (blk) {
1098 85550 case SKIP_BLOCK:
1099 85550 c->put_pixels_tab(dst, prev, stride, 8);
1100 85550 break;
1101 26215 case SCALED_BLOCK:
1102 26215 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
1103 switch (blk) {
1104 902 case RUN_BLOCK:
1105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 902 times.
902 if (get_bits_left(gb) < 4)
1106 return AVERROR_INVALIDDATA;
1107 902 scan = bink_patterns[get_bits(gb, 4)];
1108 902 i = 0;
1109 do {
1110 6551 int run = get_value(c, BINK_SRC_RUN) + 1;
1111
1112 6551 i += run;
1113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6551 times.
6551 if (i > 64) {
1114 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1115 return AVERROR_INVALIDDATA;
1116 }
1117
2/2
✓ Branch 1 taken 5908 times.
✓ Branch 2 taken 643 times.
6551 if (get_bits1(gb)) {
1118 5908 v = get_value(c, BINK_SRC_COLORS);
1119
2/2
✓ Branch 0 taken 56584 times.
✓ Branch 1 taken 5908 times.
62492 for (j = 0; j < run; j++)
1120 56584 ublock[*scan++] = v;
1121 } else {
1122
2/2
✓ Branch 0 taken 1047 times.
✓ Branch 1 taken 643 times.
1690 for (j = 0; j < run; j++)
1123 1047 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1124 }
1125
2/2
✓ Branch 0 taken 5649 times.
✓ Branch 1 taken 902 times.
6551 } while (i < 63);
1126
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 805 times.
902 if (i == 63)
1127 97 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
1128 902 break;
1129 19454 case INTRA_BLOCK:
1130 19454 memset(dctblock, 0, sizeof(*dctblock) * 64);
1131 19454 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19454 times.
19454 if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1133 return quant_idx;
1134 19454 unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1135 19454 c->binkdsp.idct_put(ublock, 8, dctblock);
1136 19454 break;
1137 5108 case FILL_BLOCK:
1138 5108 v = get_value(c, BINK_SRC_COLORS);
1139 5108 c->bdsp.fill_block_tab[0](dst, v, stride, 16);
1140 5108 break;
1141 751 case PATTERN_BLOCK:
1142
2/2
✓ Branch 0 taken 1502 times.
✓ Branch 1 taken 751 times.
2253 for (i = 0; i < 2; i++)
1143 1502 col[i] = get_value(c, BINK_SRC_COLORS);
1144
2/2
✓ Branch 0 taken 6008 times.
✓ Branch 1 taken 751 times.
6759 for (j = 0; j < 8; j++) {
1145 6008 v = get_value(c, BINK_SRC_PATTERN);
1146
2/2
✓ Branch 0 taken 48064 times.
✓ Branch 1 taken 6008 times.
54072 for (i = 0; i < 8; i++, v >>= 1)
1147 48064 ublock[i + j*8] = col[v & 1];
1148 }
1149 751 break;
1150 case RAW_BLOCK:
1151 for (j = 0; j < 8; j++)
1152 for (i = 0; i < 8; i++)
1153 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
1154 break;
1155 default:
1156 av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
1157 return AVERROR_INVALIDDATA;
1158 }
1159
2/2
✓ Branch 0 taken 21107 times.
✓ Branch 1 taken 5108 times.
26215 if (blk != FILL_BLOCK)
1160 21107 c->binkdsp.scale_block(ublock, dst, stride);
1161 26215 bx++;
1162 26215 dst += 8;
1163 26215 prev += 8;
1164 26215 break;
1165 97378 case MOTION_BLOCK:
1166 97378 ret = bink_put_pixels(c, dst, prev, stride,
1167 ref_start, ref_end);
1168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97378 times.
97378 if (ret < 0)
1169 return ret;
1170 97378 break;
1171 11625 case RUN_BLOCK:
1172 11625 scan = bink_patterns[get_bits(gb, 4)];
1173 11625 i = 0;
1174 do {
1175 90262 int run = get_value(c, BINK_SRC_RUN) + 1;
1176
1177 90262 i += run;
1178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90262 times.
90262 if (i > 64) {
1179 av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
1180 return AVERROR_INVALIDDATA;
1181 }
1182
2/2
✓ Branch 1 taken 76178 times.
✓ Branch 2 taken 14084 times.
90262 if (get_bits1(gb)) {
1183 76178 v = get_value(c, BINK_SRC_COLORS);
1184
2/2
✓ Branch 0 taken 695057 times.
✓ Branch 1 taken 76178 times.
771235 for (j = 0; j < run; j++)
1185 695057 dst[coordmap[*scan++]] = v;
1186 } else {
1187
2/2
✓ Branch 0 taken 48041 times.
✓ Branch 1 taken 14084 times.
62125 for (j = 0; j < run; j++)
1188 48041 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1189 }
1190
2/2
✓ Branch 0 taken 78637 times.
✓ Branch 1 taken 11625 times.
90262 } while (i < 63);
1191
2/2
✓ Branch 0 taken 902 times.
✓ Branch 1 taken 10723 times.
11625 if (i == 63)
1192 902 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
1193 11625 break;
1194 3750 case RESIDUE_BLOCK:
1195 3750 ret = bink_put_pixels(c, dst, prev, stride,
1196 ref_start, ref_end);
1197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3750 times.
3750 if (ret < 0)
1198 return ret;
1199 3750 c->bdsp.clear_block(block);
1200 3750 v = get_bits(gb, 7);
1201 3750 read_residue(gb, block, v);
1202 3750 c->binkdsp.add_pixels8(dst, block, stride);
1203 3750 break;
1204 16881 case INTRA_BLOCK:
1205 16881 memset(dctblock, 0, sizeof(*dctblock) * 64);
1206 16881 dctblock[0] = get_value(c, BINK_SRC_INTRA_DC);
1207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16881 times.
16881 if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1208 return quant_idx;
1209 16881 unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan);
1210 16881 c->binkdsp.idct_put(dst, stride, dctblock);
1211 16881 break;
1212 31947 case FILL_BLOCK:
1213 31947 v = get_value(c, BINK_SRC_COLORS);
1214 31947 c->bdsp.fill_block_tab[1](dst, v, stride, 8);
1215 31947 break;
1216 9168 case INTER_BLOCK:
1217 9168 ret = bink_put_pixels(c, dst, prev, stride,
1218 ref_start, ref_end);
1219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9168 times.
9168 if (ret < 0)
1220 return ret;
1221 9168 memset(dctblock, 0, sizeof(*dctblock) * 64);
1222 9168 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
1223
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9168 times.
9168 if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0)
1224 return quant_idx;
1225 9168 unquantize_dct_coeffs(dctblock, bink_inter_quant[quant_idx], coef_count, coef_idx, bink_scan);
1226 9168 c->binkdsp.idct_add(dst, stride, dctblock);
1227 9168 break;
1228 5911 case PATTERN_BLOCK:
1229
2/2
✓ Branch 0 taken 11822 times.
✓ Branch 1 taken 5911 times.
17733 for (i = 0; i < 2; i++)
1230 11822 col[i] = get_value(c, BINK_SRC_COLORS);
1231
2/2
✓ Branch 0 taken 47288 times.
✓ Branch 1 taken 5911 times.
53199 for (i = 0; i < 8; i++) {
1232 47288 v = get_value(c, BINK_SRC_PATTERN);
1233
2/2
✓ Branch 0 taken 378304 times.
✓ Branch 1 taken 47288 times.
425592 for (j = 0; j < 8; j++, v >>= 1)
1234 378304 dst[i*stride + j] = col[v & 1];
1235 }
1236 5911 break;
1237 130 case RAW_BLOCK:
1238
2/2
✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 130 times.
1170 for (i = 0; i < 8; i++)
1239 1040 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1240 130 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
1241 130 break;
1242 default:
1243 av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
1244 return AVERROR_INVALIDDATA;
1245 }
1246 }
1247 }
1248
1249 153 end:
1250
2/2
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 2 times.
153 if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
1251 151 skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
1252
1253 153 return 0;
1254 }
1255
1256 81 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
1257 int *got_frame, AVPacket *pkt)
1258 {
1259 81 BinkContext * const c = avctx->priv_data;
1260 GetBitContext gb;
1261 int plane, plane_idx, ret;
1262 81 int bits_count = pkt->size << 3;
1263
1264
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 30 times.
81 if (c->version > 'b') {
1265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1266 return ret;
1267 } else {
1268
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if ((ret = ff_reget_buffer(avctx, c->last, 0)) < 0)
1269 return ret;
1270
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if ((ret = av_frame_ref(frame, c->last)) < 0)
1271 return ret;
1272 }
1273
1274 81 init_get_bits(&gb, pkt->data, bits_count);
1275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (c->has_alpha) {
1276 if (c->version >= 'i')
1277 skip_bits_long(&gb, 32);
1278 if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
1279 return ret;
1280 }
1281
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 50 times.
81 if (c->version >= 'i')
1282 31 skip_bits_long(&gb, 32);
1283
1284 81 c->frame_num++;
1285
1286
1/2
✓ Branch 0 taken 243 times.
✗ Branch 1 not taken.
243 for (plane = 0; plane < 3; plane++) {
1287
4/4
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 62 times.
✓ Branch 3 taken 100 times.
243 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
1288
1289
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 90 times.
243 if (c->version > 'b') {
1290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
153 if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
1291 return ret;
1292 } else {
1293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
1294 90 c->frame_num == 1, !!plane)) < 0)
1295 return ret;
1296 }
1297
2/2
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 162 times.
243 if (get_bits_count(&gb) >= bits_count)
1298 81 break;
1299 }
1300 81 emms_c();
1301
1302
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 30 times.
81 if (c->version > 'b') {
1303 51 av_frame_unref(c->last);
1304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = av_frame_ref(c->last, frame)) < 0)
1305 return ret;
1306 }
1307
1308 81 *got_frame = 1;
1309
1310 /* always report that the buffer was completely consumed */
1311 81 return pkt->size;
1312 }
1313
1314 6 static av_cold void bink_init_vlcs(void)
1315 {
1316
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 6 times.
102 for (int i = 0, offset = 0; i < 16; i++) {
1317 static VLCElem table[976];
1318 96 const int maxbits = bink_tree_lens[i][15];
1319 96 bink_trees[i].table = table + offset;
1320 96 bink_trees[i].table_allocated = 1 << maxbits;
1321 96 offset += bink_trees[i].table_allocated;
1322 96 init_vlc(&bink_trees[i], maxbits, 16,
1323 bink_tree_lens[i], 1, 1,
1324 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
1325 }
1326 6 }
1327
1328 /**
1329 * Calculate quantization tables for version b
1330 */
1331 1 static av_cold void binkb_calc_quant(void)
1332 {
1333 uint8_t inv_bink_scan[64];
1334 static const int s[64]={
1335 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1336 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207,
1337 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357,
1338 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918,
1339 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703,
1340 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969,
1341 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478,
1342 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730,
1343 };
1344 int i, j;
1345 #define C (1LL<<30)
1346
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 1 times.
65 for (i = 0; i < 64; i++)
1347 64 inv_bink_scan[bink_scan[i]] = i;
1348
1349
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
17 for (j = 0; j < 16; j++) {
1350
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 16 times.
1040 for (i = 0; i < 64; i++) {
1351 1024 int k = inv_bink_scan[i];
1352 1024 binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] *
1353 1024 binkb_num[j]/(binkb_den[j] * (C>>12));
1354 1024 binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] *
1355 1024 binkb_num[j]/(binkb_den[j] * (C>>12));
1356 }
1357 }
1358 1 }
1359
1360 9 static av_cold int decode_init(AVCodecContext *avctx)
1361 {
1362 static AVOnce init_static_once = AV_ONCE_INIT;
1363 9 BinkContext * const c = avctx->priv_data;
1364 HpelDSPContext hdsp;
1365 int ret;
1366 int flags;
1367
1368 9 c->version = avctx->codec_tag >> 24;
1369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (avctx->extradata_size < 4) {
1370 av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
1371 return AVERROR_INVALIDDATA;
1372 }
1373 9 flags = AV_RL32(avctx->extradata);
1374 9 c->has_alpha = flags & BINK_FLAG_ALPHA;
1375 9 c->swap_planes = c->version >= 'h';
1376 9 c->avctx = avctx;
1377
1378
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1379 return ret;
1380
1381 9 c->last = av_frame_alloc();
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!c->last)
1383 return AVERROR(ENOMEM);
1384
1385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 avctx->color_range = c->version == 'k' ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
1387
1388 9 ff_blockdsp_init(&c->bdsp, avctx);
1389 9 ff_hpeldsp_init(&hdsp, avctx->flags);
1390 9 c->put_pixels_tab = hdsp.put_pixels_tab[1][0];
1391 9 ff_binkdsp_init(&c->binkdsp);
1392
1393
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = init_bundles(c)) < 0)
1394 return ret;
1395
1396
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (c->version == 'b') {
1397 static AVOnce binkb_init_once = AV_ONCE_INIT;
1398 2 ff_thread_once(&binkb_init_once, binkb_calc_quant);
1399 }
1400 9 ff_thread_once(&init_static_once, bink_init_vlcs);
1401
1402 9 return 0;
1403 }
1404
1405 9 static av_cold int decode_end(AVCodecContext *avctx)
1406 {
1407 9 BinkContext * const c = avctx->priv_data;
1408
1409 9 av_frame_free(&c->last);
1410
1411 9 free_bundles(c);
1412 9 return 0;
1413 }
1414
1415 static void flush(AVCodecContext *avctx)
1416 {
1417 BinkContext * const c = avctx->priv_data;
1418
1419 c->frame_num = 0;
1420 }
1421
1422 const FFCodec ff_bink_decoder = {
1423 .p.name = "binkvideo",
1424 .p.long_name = NULL_IF_CONFIG_SMALL("Bink video"),
1425 .p.type = AVMEDIA_TYPE_VIDEO,
1426 .p.id = AV_CODEC_ID_BINKVIDEO,
1427 .priv_data_size = sizeof(BinkContext),
1428 .init = decode_init,
1429 .close = decode_end,
1430 FF_CODEC_DECODE_CB(decode_frame),
1431 .flush = flush,
1432 .p.capabilities = AV_CODEC_CAP_DR1,
1433 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1434 };
1435