FFmpeg coverage


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