| 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/mem.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 "decode.h" | ||
| 36 | #include "get_bits.h" | ||
| 37 | #include "hpeldsp.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 uint8_t binkb_bundle_sizes[BINKB_NB_SRC] = { | ||
| 63 | 4, 8, 8, 5, 5, 11, 11, 4, 4, 7 | ||
| 64 | }; | ||
| 65 | |||
| 66 | static const uint8_t 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 | 1020 | static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num) | |
| 571 | { | ||
| 572 | 1020 | c->bundle[bundle_num].cur_dec = | |
| 573 | 1020 | c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data; | |
| 574 | 1020 | c->bundle[bundle_num].len = 13; | |
| 575 | 1020 | } | |
| 576 | |||
| 577 | 102 | static av_cold void binkb_init_bundles(BinkContext *c) | |
| 578 | { | ||
| 579 | int i; | ||
| 580 |
2/2✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 102 times.
|
1122 | for (i = 0; i < BINKB_NB_SRC; i++) |
| 581 | 1020 | binkb_init_bundle(c, i); | |
| 582 | 102 | } | |
| 583 | |||
| 584 | 10540 | static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num) | |
| 585 | { | ||
| 586 | 10540 | const int bits = binkb_bundle_sizes[bundle_num]; | |
| 587 | 10540 | const int mask = 1 << (bits - 1); | |
| 588 | 10540 | const int issigned = binkb_bundle_signed[bundle_num]; | |
| 589 | 10540 | Bundle *b = &c->bundle[bundle_num]; | |
| 590 | int i, len; | ||
| 591 | |||
| 592 |
6/6✓ Branch 0 taken 5397 times.
✓ Branch 1 taken 5143 times.
✓ Branch 2 taken 4310 times.
✓ Branch 3 taken 1087 times.
✓ Branch 5 taken 555 times.
✓ Branch 6 taken 532 times.
|
10540 | CHECK_READ_VAL(gb, b, len); |
| 593 |
3/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 525 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 532 times.
|
532 | if (b->data_end - b->cur_dec < len * (1 + (bits > 8))) |
| 594 | ✗ | return AVERROR_INVALIDDATA; | |
| 595 |
2/2✓ Branch 0 taken 525 times.
✓ Branch 1 taken 7 times.
|
532 | if (bits <= 8) { |
| 596 |
2/2✓ Branch 0 taken 321 times.
✓ Branch 1 taken 204 times.
|
525 | if (!issigned) { |
| 597 |
2/2✓ Branch 0 taken 44404 times.
✓ Branch 1 taken 321 times.
|
44725 | for (i = 0; i < len; i++) |
| 598 | 44404 | *b->cur_dec++ = get_bits(gb, bits); | |
| 599 | } else { | ||
| 600 |
2/2✓ Branch 0 taken 30806 times.
✓ Branch 1 taken 204 times.
|
31010 | for (i = 0; i < len; i++) |
| 601 | 30806 | *b->cur_dec++ = get_bits(gb, bits) - mask; | |
| 602 | } | ||
| 603 | } else { | ||
| 604 | 7 | int16_t *dst = (int16_t*)b->cur_dec; | |
| 605 | |||
| 606 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (!issigned) { |
| 607 | ✗ | for (i = 0; i < len; i++) | |
| 608 | ✗ | *dst++ = get_bits(gb, bits); | |
| 609 | } else { | ||
| 610 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | for (i = 0; i < len; i++) |
| 611 | 7 | *dst++ = get_bits(gb, bits) - mask; | |
| 612 | } | ||
| 613 | 7 | b->cur_dec = (uint8_t*)dst; | |
| 614 | } | ||
| 615 | 532 | return 0; | |
| 616 | } | ||
| 617 | |||
| 618 | 69393 | static inline int binkb_get_value(BinkContext *c, int bundle_num) | |
| 619 | { | ||
| 620 | int16_t ret; | ||
| 621 | 69393 | const int bits = binkb_bundle_sizes[bundle_num]; | |
| 622 | |||
| 623 |
2/2✓ Branch 0 taken 69386 times.
✓ Branch 1 taken 7 times.
|
69393 | if (bits <= 8) { |
| 624 | 69386 | int val = *c->bundle[bundle_num].cur_ptr++; | |
| 625 |
2/2✓ Branch 0 taken 30806 times.
✓ Branch 1 taken 38580 times.
|
69386 | return binkb_bundle_signed[bundle_num] ? (int8_t)val : val; |
| 626 | } | ||
| 627 | 7 | ret = *(int16_t*)c->bundle[bundle_num].cur_ptr; | |
| 628 | 7 | c->bundle[bundle_num].cur_ptr += 2; | |
| 629 | 7 | 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 | 45510 | 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 | 45510 | int list_start = 64, list_end = 64, list_pos; | |
| 649 | 45510 | int coef_count = 0; | |
| 650 | int quant_idx; | ||
| 651 | |||
| 652 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 45510 times.
|
45510 | if (get_bits_left(gb) < 4) |
| 653 | ✗ | return AVERROR_INVALIDDATA; | |
| 654 | |||
| 655 | 45510 | coef_list[list_end] = 4; mode_list[list_end++] = 0; | |
| 656 | 45510 | coef_list[list_end] = 24; mode_list[list_end++] = 0; | |
| 657 | 45510 | coef_list[list_end] = 44; mode_list[list_end++] = 0; | |
| 658 | 45510 | coef_list[list_end] = 1; mode_list[list_end++] = 3; | |
| 659 | 45510 | coef_list[list_end] = 2; mode_list[list_end++] = 3; | |
| 660 | 45510 | coef_list[list_end] = 3; mode_list[list_end++] = 3; | |
| 661 | |||
| 662 |
2/2✓ Branch 1 taken 101536 times.
✓ Branch 2 taken 45510 times.
|
147046 | for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) { |
| 663 | 101536 | list_pos = list_start; | |
| 664 |
2/2✓ Branch 0 taken 982747 times.
✓ Branch 1 taken 101536 times.
|
1084283 | while (list_pos < list_end) { |
| 665 |
4/4✓ Branch 0 taken 846990 times.
✓ Branch 1 taken 135757 times.
✓ Branch 3 taken 535292 times.
✓ Branch 4 taken 311698 times.
|
982747 | if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) { |
| 666 | 671049 | list_pos++; | |
| 667 | 671049 | continue; | |
| 668 | } | ||
| 669 | 311698 | ccoef = coef_list[list_pos]; | |
| 670 | 311698 | mode = mode_list[list_pos]; | |
| 671 |
4/5✓ Branch 0 taken 66860 times.
✓ Branch 1 taken 54324 times.
✓ Branch 2 taken 25484 times.
✓ Branch 3 taken 165030 times.
✗ Branch 4 not taken.
|
311698 | switch (mode) { |
| 672 | 66860 | case 0: | |
| 673 | 66860 | coef_list[list_pos] = ccoef + 4; | |
| 674 | 66860 | mode_list[list_pos] = 1; | |
| 675 | av_fallthrough; | ||
| 676 | 121184 | case 2: | |
| 677 |
2/2✓ Branch 0 taken 54324 times.
✓ Branch 1 taken 66860 times.
|
121184 | if (mode == 2) { |
| 678 | 54324 | coef_list[list_pos] = 0; | |
| 679 | 54324 | mode_list[list_pos++] = 0; | |
| 680 | } | ||
| 681 |
2/2✓ Branch 0 taken 484736 times.
✓ Branch 1 taken 121184 times.
|
605920 | for (i = 0; i < 4; i++, ccoef++) { |
| 682 |
2/2✓ Branch 1 taken 291260 times.
✓ Branch 2 taken 193476 times.
|
484736 | if (get_bits1(gb)) { |
| 683 | 291260 | coef_list[--list_start] = ccoef; | |
| 684 | 291260 | mode_list[ list_start] = 3; | |
| 685 | } else { | ||
| 686 |
2/2✓ Branch 0 taken 131107 times.
✓ Branch 1 taken 62369 times.
|
193476 | if (!bits) { |
| 687 | 131107 | 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 | 193476 | block[scan[ccoef]] = t; | |
| 694 | 193476 | coef_idx[coef_count++] = ccoef; | |
| 695 | } | ||
| 696 | } | ||
| 697 | 121184 | 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 | 165030 | case 3: | |
| 707 |
2/2✓ Branch 0 taken 92957 times.
✓ Branch 1 taken 72073 times.
|
165030 | if (!bits) { |
| 708 | 92957 | 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 | 165030 | block[scan[ccoef]] = t; | |
| 715 | 165030 | coef_idx[coef_count++] = ccoef; | |
| 716 | 165030 | coef_list[list_pos] = 0; | |
| 717 | 165030 | mode_list[list_pos++] = 0; | |
| 718 | 165030 | break; | |
| 719 | } | ||
| 720 | } | ||
| 721 | } | ||
| 722 | |||
| 723 |
2/2✓ Branch 0 taken 45503 times.
✓ Branch 1 taken 7 times.
|
45510 | if (q == -1) { |
| 724 | 45503 | quant_idx = get_bits(gb, 4); | |
| 725 | } else { | ||
| 726 | 7 | quant_idx = q; | |
| 727 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | 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 | 45510 | *coef_count_ = coef_count; | |
| 734 | |||
| 735 | 45510 | return quant_idx; | |
| 736 | } | ||
| 737 | |||
| 738 | 45510 | 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 | 45510 | block[0] = (int)(block[0] * quant[0]) >> 11; | |
| 744 |
2/2✓ Branch 0 taken 358506 times.
✓ Branch 1 taken 45510 times.
|
404016 | for (i = 0; i < coef_count; i++) { |
| 745 | 358506 | int idx = coef_idx[i]; | |
| 746 | 358506 | block[scan[idx]] = (int)(block[scan[idx]] * quant[idx]) >> 11; | |
| 747 | } | ||
| 748 | 45510 | } | |
| 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 | 19146 | 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 | 19146 | int list_start = 64, list_end = 64, list_pos; | |
| 764 | int nz_coeff[64]; | ||
| 765 | 19146 | int nz_coeff_count = 0; | |
| 766 | |||
| 767 | 19146 | coef_list[list_end] = 4; mode_list[list_end++] = 0; | |
| 768 | 19146 | coef_list[list_end] = 24; mode_list[list_end++] = 0; | |
| 769 | 19146 | coef_list[list_end] = 44; mode_list[list_end++] = 0; | |
| 770 | 19146 | coef_list[list_end] = 0; mode_list[list_end++] = 2; | |
| 771 | |||
| 772 |
1/2✓ Branch 1 taken 28759 times.
✗ Branch 2 not taken.
|
28759 | for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) { |
| 773 |
2/2✓ Branch 0 taken 71013 times.
✓ Branch 1 taken 27842 times.
|
98855 | for (i = 0; i < nz_coeff_count; i++) { |
| 774 |
2/2✓ Branch 1 taken 44713 times.
✓ Branch 2 taken 26300 times.
|
71013 | if (!get_bits1(gb)) |
| 775 | 44713 | continue; | |
| 776 |
2/2✓ Branch 0 taken 12271 times.
✓ Branch 1 taken 14029 times.
|
26300 | if (block[nz_coeff[i]] < 0) |
| 777 | 12271 | block[nz_coeff[i]] -= mask; | |
| 778 | else | ||
| 779 | 14029 | block[nz_coeff[i]] += mask; | |
| 780 | 26300 | masks_count--; | |
| 781 |
2/2✓ Branch 0 taken 917 times.
✓ Branch 1 taken 25383 times.
|
26300 | if (masks_count < 0) |
| 782 | 917 | return 0; | |
| 783 | } | ||
| 784 | 27842 | list_pos = list_start; | |
| 785 |
2/2✓ Branch 0 taken 379716 times.
✓ Branch 1 taken 9613 times.
|
389329 | while (list_pos < list_end) { |
| 786 |
4/4✓ Branch 0 taken 346133 times.
✓ Branch 1 taken 33583 times.
✓ Branch 3 taken 178350 times.
✓ Branch 4 taken 167783 times.
|
379716 | if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) { |
| 787 | 211933 | list_pos++; | |
| 788 | 211933 | continue; | |
| 789 | } | ||
| 790 | 167783 | ccoef = coef_list[list_pos]; | |
| 791 | 167783 | mode = mode_list[list_pos]; | |
| 792 |
4/5✓ Branch 0 taken 37223 times.
✓ Branch 1 taken 64050 times.
✓ Branch 2 taken 32241 times.
✓ Branch 3 taken 34269 times.
✗ Branch 4 not taken.
|
167783 | switch (mode) { |
| 793 | 37223 | case 0: | |
| 794 | 37223 | coef_list[list_pos] = ccoef + 4; | |
| 795 | 37223 | mode_list[list_pos] = 1; | |
| 796 | av_fallthrough; | ||
| 797 | 101273 | case 2: | |
| 798 |
2/2✓ Branch 0 taken 64050 times.
✓ Branch 1 taken 37223 times.
|
101273 | if (mode == 2) { |
| 799 | 64050 | coef_list[list_pos] = 0; | |
| 800 | 64050 | mode_list[list_pos++] = 0; | |
| 801 | } | ||
| 802 |
2/2✓ Branch 0 taken 381402 times.
✓ Branch 1 taken 84489 times.
|
465891 | for (i = 0; i < 4; i++, ccoef++) { |
| 803 |
2/2✓ Branch 1 taken 275208 times.
✓ Branch 2 taken 106194 times.
|
381402 | if (get_bits1(gb)) { |
| 804 | 275208 | coef_list[--list_start] = ccoef; | |
| 805 | 275208 | mode_list[ list_start] = 3; | |
| 806 | } else { | ||
| 807 | 106194 | nz_coeff[nz_coeff_count++] = bink_scan[ccoef]; | |
| 808 | 106194 | sign = -get_bits1(gb); | |
| 809 | 106194 | block[bink_scan[ccoef]] = (mask ^ sign) - sign; | |
| 810 | 106194 | masks_count--; | |
| 811 |
2/2✓ Branch 0 taken 16784 times.
✓ Branch 1 taken 89410 times.
|
106194 | if (masks_count < 0) |
| 812 | 16784 | return 0; | |
| 813 | } | ||
| 814 | } | ||
| 815 | 84489 | break; | |
| 816 | 32241 | case 1: | |
| 817 | 32241 | mode_list[list_pos] = 2; | |
| 818 |
2/2✓ Branch 0 taken 96723 times.
✓ Branch 1 taken 32241 times.
|
128964 | for (i = 0; i < 3; i++) { |
| 819 | 96723 | ccoef += 4; | |
| 820 | 96723 | coef_list[list_end] = ccoef; | |
| 821 | 96723 | mode_list[list_end++] = 2; | |
| 822 | } | ||
| 823 | 32241 | break; | |
| 824 | 34269 | case 3: | |
| 825 | 34269 | nz_coeff[nz_coeff_count++] = bink_scan[ccoef]; | |
| 826 | 34269 | sign = -get_bits1(gb); | |
| 827 | 34269 | block[bink_scan[ccoef]] = (mask ^ sign) - sign; | |
| 828 | 34269 | coef_list[list_pos] = 0; | |
| 829 | 34269 | mode_list[list_pos++] = 0; | |
| 830 | 34269 | masks_count--; | |
| 831 |
2/2✓ Branch 0 taken 1445 times.
✓ Branch 1 taken 32824 times.
|
34269 | if (masks_count < 0) |
| 832 | 1445 | return 0; | |
| 833 | 32824 | break; | |
| 834 | } | ||
| 835 | } | ||
| 836 | } | ||
| 837 | |||
| 838 | ✗ | return 0; | |
| 839 | } | ||
| 840 | |||
| 841 | /** | ||
| 842 | * Copy 8x8 block from source to destination, where src and dst may be overlapped | ||
| 843 | */ | ||
| 844 | 15235 | static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride) | |
| 845 | { | ||
| 846 | uint8_t tmp[64]; | ||
| 847 | int i; | ||
| 848 |
2/2✓ Branch 0 taken 121880 times.
✓ Branch 1 taken 15235 times.
|
137115 | for (i = 0; i < 8; i++) |
| 849 | 121880 | memcpy(tmp + i*8, src + i*stride, 8); | |
| 850 |
2/2✓ Branch 0 taken 121880 times.
✓ Branch 1 taken 15235 times.
|
137115 | for (i = 0; i < 8; i++) |
| 851 | 121880 | memcpy(dst + i*stride, tmp + i*8, 8); | |
| 852 | 15235 | } | |
| 853 | |||
| 854 | 102 | static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, | |
| 855 | int plane_idx, int is_key, int is_chroma) | ||
| 856 | { | ||
| 857 | int blk, ret; | ||
| 858 | int i, j, bx, by; | ||
| 859 | uint8_t *dst, *ref, *ref_start, *ref_end; | ||
| 860 | int v, col[2]; | ||
| 861 | const uint8_t *scan; | ||
| 862 | int xoff, yoff; | ||
| 863 | 102 | LOCAL_ALIGNED_32(int16_t, block, [64]); | |
| 864 | 102 | LOCAL_ALIGNED_16(int32_t, dctblock, [64]); | |
| 865 | int coordmap[64]; | ||
| 866 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 99 times.
|
102 | int ybias = is_key ? -15 : 0; |
| 867 | int qp, quant_idx, coef_count, coef_idx[64]; | ||
| 868 | |||
| 869 | 102 | const int stride = frame->linesize[plane_idx]; | |
| 870 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34 times.
|
102 | int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3; |
| 871 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34 times.
|
102 | int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3; |
| 872 | |||
| 873 | 102 | binkb_init_bundles(c); | |
| 874 | 102 | ref_start = frame->data[plane_idx]; | |
| 875 | 102 | ref_end = frame->data[plane_idx] + ((bh - 1) * frame->linesize[plane_idx] + bw - 1) * 8; | |
| 876 | |||
| 877 |
2/2✓ Branch 0 taken 6528 times.
✓ Branch 1 taken 102 times.
|
6630 | for (i = 0; i < 64; i++) |
| 878 | 6528 | coordmap[i] = (i & 7) + (i >> 3) * stride; | |
| 879 | |||
| 880 |
2/2✓ Branch 0 taken 1054 times.
✓ Branch 1 taken 102 times.
|
1156 | for (by = 0; by < bh; by++) { |
| 881 |
2/2✓ Branch 0 taken 10540 times.
✓ Branch 1 taken 1054 times.
|
11594 | for (i = 0; i < BINKB_NB_SRC; i++) { |
| 882 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10540 times.
|
10540 | if ((ret = binkb_read_bundle(c, gb, i)) < 0) |
| 883 | ✗ | return ret; | |
| 884 | } | ||
| 885 | |||
| 886 | 1054 | dst = frame->data[plane_idx] + 8*by*stride; | |
| 887 |
2/2✓ Branch 0 taken 19822 times.
✓ Branch 1 taken 1054 times.
|
20876 | for (bx = 0; bx < bw; bx++, dst += 8) { |
| 888 | 19822 | blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES); | |
| 889 |
7/10✓ Branch 0 taken 4089 times.
✓ Branch 1 taken 195 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15396 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 28 times.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 91 times.
✗ Branch 9 not taken.
|
19822 | switch (blk) { |
| 890 | 4089 | case 0: | |
| 891 | 4089 | break; | |
| 892 | 195 | case 1: | |
| 893 | 195 | scan = bink_patterns[get_bits(gb, 4)]; | |
| 894 | 195 | i = 0; | |
| 895 | do { | ||
| 896 | int mode, run; | ||
| 897 | |||
| 898 | 1825 | mode = get_bits1(gb); | |
| 899 | 1825 | run = get_bits(gb, binkb_runbits[i]) + 1; | |
| 900 | |||
| 901 | 1825 | i += run; | |
| 902 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1825 times.
|
1825 | if (i > 64) { |
| 903 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); | |
| 904 | ✗ | return AVERROR_INVALIDDATA; | |
| 905 | } | ||
| 906 |
2/2✓ Branch 0 taken 1241 times.
✓ Branch 1 taken 584 times.
|
1825 | if (mode) { |
| 907 | 1241 | v = binkb_get_value(c, BINKB_SRC_COLORS); | |
| 908 |
2/2✓ Branch 0 taken 10554 times.
✓ Branch 1 taken 1241 times.
|
11795 | for (j = 0; j < run; j++) |
| 909 | 10554 | dst[coordmap[*scan++]] = v; | |
| 910 | } else { | ||
| 911 |
2/2✓ Branch 0 taken 1876 times.
✓ Branch 1 taken 584 times.
|
2460 | for (j = 0; j < run; j++) |
| 912 | 1876 | dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS); | |
| 913 | } | ||
| 914 |
2/2✓ Branch 0 taken 1630 times.
✓ Branch 1 taken 195 times.
|
1825 | } while (i < 63); |
| 915 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 145 times.
|
195 | if (i == 63) |
| 916 | 50 | dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS); | |
| 917 | 195 | break; | |
| 918 | ✗ | case 2: | |
| 919 | ✗ | memset(dctblock, 0, sizeof(*dctblock) * 64); | |
| 920 | ✗ | dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC); | |
| 921 | ✗ | qp = binkb_get_value(c, BINKB_SRC_INTRA_Q); | |
| 922 | ✗ | if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0) | |
| 923 | ✗ | return quant_idx; | |
| 924 | ✗ | unquantize_dct_coeffs(dctblock, binkb_intra_quant[quant_idx], coef_count, coef_idx, bink_scan); | |
| 925 | ✗ | c->binkdsp.idct_put(dst, stride, dctblock); | |
| 926 | ✗ | break; | |
| 927 | 15396 | case 3: | |
| 928 | 15396 | xoff = binkb_get_value(c, BINKB_SRC_X_OFF); | |
| 929 | 15396 | yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias; | |
| 930 | 15396 | ref = dst + xoff + yoff * stride; | |
| 931 |
2/4✓ Branch 0 taken 15396 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15396 times.
|
15396 | if (ref < ref_start || ref > ref_end) { |
| 932 | ✗ | av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n"); | |
| 933 |
4/4✓ Branch 0 taken 15229 times.
✓ Branch 1 taken 167 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 15228 times.
|
15396 | } else if (ref + 8*stride < dst || ref >= dst + 8*stride) { |
| 934 | 168 | c->put_pixels_tab(dst, ref, stride, 8); | |
| 935 | } else { | ||
| 936 | 15228 | put_pixels8x8_overlapped(dst, ref, stride); | |
| 937 | } | ||
| 938 | 15396 | c->bdsp.clear_block(block); | |
| 939 | 15396 | v = binkb_get_value(c, BINKB_SRC_INTER_COEFS); | |
| 940 | 15396 | read_residue(gb, block, v); | |
| 941 | 15396 | c->binkdsp.add_pixels8(dst, block, stride); | |
| 942 | 15396 | break; | |
| 943 | 7 | case 4: | |
| 944 | 7 | xoff = binkb_get_value(c, BINKB_SRC_X_OFF); | |
| 945 | 7 | yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias; | |
| 946 | 7 | ref = dst + xoff + yoff * stride; | |
| 947 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | if (ref < ref_start || ref > ref_end) { |
| 948 | ✗ | av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n"); | |
| 949 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | } else if (ref + 8*stride < dst || ref >= dst + 8*stride) { |
| 950 | ✗ | c->put_pixels_tab(dst, ref, stride, 8); | |
| 951 | } else { | ||
| 952 | 7 | put_pixels8x8_overlapped(dst, ref, stride); | |
| 953 | } | ||
| 954 | 7 | memset(dctblock, 0, sizeof(*dctblock) * 64); | |
| 955 | 7 | dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC); | |
| 956 | 7 | qp = binkb_get_value(c, BINKB_SRC_INTER_Q); | |
| 957 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0) |
| 958 | ✗ | return quant_idx; | |
| 959 | 7 | unquantize_dct_coeffs(dctblock, binkb_inter_quant[quant_idx], coef_count, coef_idx, bink_scan); | |
| 960 | 7 | c->binkdsp.idct_add(dst, stride, dctblock); | |
| 961 | 7 | break; | |
| 962 | 28 | case 5: | |
| 963 | 28 | v = binkb_get_value(c, BINKB_SRC_COLORS); | |
| 964 | 28 | c->bdsp.fill_block_tab[1](dst, v, stride, 8); | |
| 965 | 28 | break; | |
| 966 | 16 | case 6: | |
| 967 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
|
48 | for (i = 0; i < 2; i++) |
| 968 | 32 | col[i] = binkb_get_value(c, BINKB_SRC_COLORS); | |
| 969 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 16 times.
|
144 | for (i = 0; i < 8; i++) { |
| 970 | 128 | v = binkb_get_value(c, BINKB_SRC_PATTERN); | |
| 971 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 128 times.
|
1152 | for (j = 0; j < 8; j++, v >>= 1) |
| 972 | 1024 | dst[i*stride + j] = col[v & 1]; | |
| 973 | } | ||
| 974 | 16 | break; | |
| 975 | ✗ | case 7: | |
| 976 | ✗ | xoff = binkb_get_value(c, BINKB_SRC_X_OFF); | |
| 977 | ✗ | yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias; | |
| 978 | ✗ | ref = dst + xoff + yoff * stride; | |
| 979 | ✗ | if (ref < ref_start || ref > ref_end) { | |
| 980 | ✗ | av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n"); | |
| 981 | ✗ | } else if (ref + 8*stride < dst || ref >= dst + 8*stride) { | |
| 982 | ✗ | c->put_pixels_tab(dst, ref, stride, 8); | |
| 983 | } else { | ||
| 984 | ✗ | put_pixels8x8_overlapped(dst, ref, stride); | |
| 985 | } | ||
| 986 | ✗ | break; | |
| 987 | 91 | case 8: | |
| 988 |
2/2✓ Branch 0 taken 728 times.
✓ Branch 1 taken 91 times.
|
819 | for (i = 0; i < 8; i++) |
| 989 | 728 | memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8); | |
| 990 | 91 | c->bundle[BINKB_SRC_COLORS].cur_ptr += 64; | |
| 991 | 91 | break; | |
| 992 | ✗ | default: | |
| 993 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk); | |
| 994 | ✗ | return AVERROR_INVALIDDATA; | |
| 995 | } | ||
| 996 | } | ||
| 997 | } | ||
| 998 |
2/2✓ Branch 1 taken 97 times.
✓ Branch 2 taken 5 times.
|
102 | if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary |
| 999 | 97 | skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F)); | |
| 1000 | |||
| 1001 | 102 | return 0; | |
| 1002 | } | ||
| 1003 | |||
| 1004 | 110296 | static int bink_put_pixels(BinkContext *c, | |
| 1005 | uint8_t *dst, uint8_t *prev, int stride, | ||
| 1006 | uint8_t *ref_start, | ||
| 1007 | uint8_t *ref_end) | ||
| 1008 | { | ||
| 1009 | 110296 | int xoff = get_value(c, BINK_SRC_X_OFF); | |
| 1010 | 110296 | int yoff = get_value(c, BINK_SRC_Y_OFF); | |
| 1011 | 110296 | uint8_t *ref = prev + xoff + yoff * stride; | |
| 1012 |
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) { |
| 1013 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n", | |
| 1014 | xoff, yoff); | ||
| 1015 | ✗ | return AVERROR_INVALIDDATA; | |
| 1016 | } | ||
| 1017 | 110296 | c->put_pixels_tab(dst, ref, stride, 8); | |
| 1018 | |||
| 1019 | 110296 | return 0; | |
| 1020 | } | ||
| 1021 | |||
| 1022 | 153 | static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, | |
| 1023 | int plane_idx, int is_chroma) | ||
| 1024 | { | ||
| 1025 | int blk, ret; | ||
| 1026 | int i, j, bx, by; | ||
| 1027 | uint8_t *dst, *prev, *ref_start, *ref_end; | ||
| 1028 | int v, col[2]; | ||
| 1029 | const uint8_t *scan; | ||
| 1030 | 153 | LOCAL_ALIGNED_32(int16_t, block, [64]); | |
| 1031 | 153 | LOCAL_ALIGNED_16(uint8_t, ublock, [64]); | |
| 1032 | 153 | LOCAL_ALIGNED_16(int32_t, dctblock, [64]); | |
| 1033 | int coordmap[64], quant_idx, coef_count, coef_idx[64]; | ||
| 1034 | |||
| 1035 | 153 | const int stride = frame->linesize[plane_idx]; | |
| 1036 |
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; |
| 1037 |
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; |
| 1038 | 153 | int width = c->avctx->width >> is_chroma; | |
| 1039 | 153 | int height = c->avctx->height >> is_chroma; | |
| 1040 | |||
| 1041 |
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)) { |
| 1042 | ✗ | int fill = get_bits(gb, 8); | |
| 1043 | |||
| 1044 | ✗ | dst = frame->data[plane_idx]; | |
| 1045 | |||
| 1046 | ✗ | for (i = 0; i < height; i++) | |
| 1047 | ✗ | memset(dst + i * stride, fill, width); | |
| 1048 | ✗ | goto end; | |
| 1049 | } | ||
| 1050 | |||
| 1051 | 153 | init_lengths(c, FFMAX(width, 8), bw); | |
| 1052 |
2/2✓ Branch 0 taken 1377 times.
✓ Branch 1 taken 153 times.
|
1530 | for (i = 0; i < BINK_NB_SRC; i++) { |
| 1053 | 1377 | ret = read_bundle(gb, c, i); | |
| 1054 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1377 times.
|
1377 | if (ret < 0) |
| 1055 | ✗ | return ret; | |
| 1056 | } | ||
| 1057 | |||
| 1058 | 453 | ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx] | |
| 1059 |
2/2✓ Branch 0 taken 147 times.
✓ Branch 1 taken 6 times.
|
153 | : frame->data[plane_idx]; |
| 1060 | 153 | ref_end = ref_start | |
| 1061 | 153 | + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8; | |
| 1062 | |||
| 1063 |
2/2✓ Branch 0 taken 9792 times.
✓ Branch 1 taken 153 times.
|
9945 | for (i = 0; i < 64; i++) |
| 1064 | 9792 | coordmap[i] = (i & 7) + (i >> 3) * stride; | |
| 1065 | |||
| 1066 |
2/2✓ Branch 0 taken 6120 times.
✓ Branch 1 taken 153 times.
|
6273 | for (by = 0; by < bh; by++) { |
| 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_BLOCK_TYPES])) < 0) |
| 1068 | ✗ | return ret; | |
| 1069 |
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) |
| 1070 | ✗ | return ret; | |
| 1071 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6120 times.
|
6120 | if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0) |
| 1072 | ✗ | return ret; | |
| 1073 |
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) |
| 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_X_OFF])) < 0) |
| 1076 | ✗ | return ret; | |
| 1077 |
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) |
| 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_INTRA_DC], DC_START_BITS, 0)) < 0) |
| 1080 | ✗ | return ret; | |
| 1081 |
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) |
| 1082 | ✗ | return ret; | |
| 1083 |
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) |
| 1084 | ✗ | return ret; | |
| 1085 | |||
| 1086 | 6120 | dst = frame->data[plane_idx] + 8*by*stride; | |
| 1087 | 18120 | prev = (c->last->data[plane_idx] ? c->last->data[plane_idx] | |
| 1088 |
2/2✓ Branch 0 taken 5880 times.
✓ Branch 1 taken 240 times.
|
6120 | : frame->data[plane_idx]) + 8*by*stride; |
| 1089 |
2/2✓ Branch 0 taken 314770 times.
✓ Branch 1 taken 6120 times.
|
320890 | for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) { |
| 1090 | 314770 | blk = get_value(c, BINK_SRC_BLOCK_TYPES); | |
| 1091 | // 16x16 block type on odd line means part of the already decoded block, so skip it | ||
| 1092 |
6/6✓ Branch 0 taken 157385 times.
✓ Branch 1 taken 157385 times.
✓ Branch 2 taken 65585 times.
✓ Branch 3 taken 91800 times.
✓ Branch 4 taken 26215 times.
✓ Branch 5 taken 196755 times.
|
314770 | if (((by & 1) || (bx & 1)) && blk == SCALED_BLOCK) { |
| 1093 | 26215 | bx++; | |
| 1094 | 26215 | dst += 8; | |
| 1095 | 26215 | prev += 8; | |
| 1096 | 26215 | continue; | |
| 1097 | } | ||
| 1098 |
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) { |
| 1099 | 85550 | case SKIP_BLOCK: | |
| 1100 | 85550 | c->put_pixels_tab(dst, prev, stride, 8); | |
| 1101 | 85550 | break; | |
| 1102 | 26215 | case SCALED_BLOCK: | |
| 1103 | 26215 | blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES); | |
| 1104 |
4/6✓ Branch 0 taken 902 times.
✓ Branch 1 taken 19454 times.
✓ Branch 2 taken 5108 times.
✓ Branch 3 taken 751 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
26215 | switch (blk) { |
| 1105 | 902 | case RUN_BLOCK: | |
| 1106 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 902 times.
|
902 | if (get_bits_left(gb) < 4) |
| 1107 | ✗ | return AVERROR_INVALIDDATA; | |
| 1108 | 902 | scan = bink_patterns[get_bits(gb, 4)]; | |
| 1109 | 902 | i = 0; | |
| 1110 | do { | ||
| 1111 | 6551 | int run = get_value(c, BINK_SRC_RUN) + 1; | |
| 1112 | |||
| 1113 | 6551 | i += run; | |
| 1114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6551 times.
|
6551 | if (i > 64) { |
| 1115 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); | |
| 1116 | ✗ | return AVERROR_INVALIDDATA; | |
| 1117 | } | ||
| 1118 |
2/2✓ Branch 1 taken 5908 times.
✓ Branch 2 taken 643 times.
|
6551 | if (get_bits1(gb)) { |
| 1119 | 5908 | v = get_value(c, BINK_SRC_COLORS); | |
| 1120 |
2/2✓ Branch 0 taken 56584 times.
✓ Branch 1 taken 5908 times.
|
62492 | for (j = 0; j < run; j++) |
| 1121 | 56584 | ublock[*scan++] = v; | |
| 1122 | } else { | ||
| 1123 |
2/2✓ Branch 0 taken 1047 times.
✓ Branch 1 taken 643 times.
|
1690 | for (j = 0; j < run; j++) |
| 1124 | 1047 | ublock[*scan++] = get_value(c, BINK_SRC_COLORS); | |
| 1125 | } | ||
| 1126 |
2/2✓ Branch 0 taken 5649 times.
✓ Branch 1 taken 902 times.
|
6551 | } while (i < 63); |
| 1127 |
2/2✓ Branch 0 taken 97 times.
✓ Branch 1 taken 805 times.
|
902 | if (i == 63) |
| 1128 | 97 | ublock[*scan++] = get_value(c, BINK_SRC_COLORS); | |
| 1129 | 902 | break; | |
| 1130 | 19454 | case INTRA_BLOCK: | |
| 1131 | 19454 | memset(dctblock, 0, sizeof(*dctblock) * 64); | |
| 1132 | 19454 | dctblock[0] = get_value(c, BINK_SRC_INTRA_DC); | |
| 1133 |
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) |
| 1134 | ✗ | return quant_idx; | |
| 1135 | 19454 | unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan); | |
| 1136 | 19454 | c->binkdsp.idct_put(ublock, 8, dctblock); | |
| 1137 | 19454 | break; | |
| 1138 | 5108 | case FILL_BLOCK: | |
| 1139 | 5108 | v = get_value(c, BINK_SRC_COLORS); | |
| 1140 | 5108 | c->bdsp.fill_block_tab[0](dst, v, stride, 16); | |
| 1141 | 5108 | break; | |
| 1142 | 751 | case PATTERN_BLOCK: | |
| 1143 |
2/2✓ Branch 0 taken 1502 times.
✓ Branch 1 taken 751 times.
|
2253 | for (i = 0; i < 2; i++) |
| 1144 | 1502 | col[i] = get_value(c, BINK_SRC_COLORS); | |
| 1145 |
2/2✓ Branch 0 taken 6008 times.
✓ Branch 1 taken 751 times.
|
6759 | for (j = 0; j < 8; j++) { |
| 1146 | 6008 | v = get_value(c, BINK_SRC_PATTERN); | |
| 1147 |
2/2✓ Branch 0 taken 48064 times.
✓ Branch 1 taken 6008 times.
|
54072 | for (i = 0; i < 8; i++, v >>= 1) |
| 1148 | 48064 | ublock[i + j*8] = col[v & 1]; | |
| 1149 | } | ||
| 1150 | 751 | break; | |
| 1151 | ✗ | case RAW_BLOCK: | |
| 1152 | ✗ | for (j = 0; j < 8; j++) | |
| 1153 | ✗ | for (i = 0; i < 8; i++) | |
| 1154 | ✗ | ublock[i + j*8] = get_value(c, BINK_SRC_COLORS); | |
| 1155 | ✗ | break; | |
| 1156 | ✗ | default: | |
| 1157 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk); | |
| 1158 | ✗ | return AVERROR_INVALIDDATA; | |
| 1159 | } | ||
| 1160 |
2/2✓ Branch 0 taken 21107 times.
✓ Branch 1 taken 5108 times.
|
26215 | if (blk != FILL_BLOCK) |
| 1161 | 21107 | c->binkdsp.scale_block(ublock, dst, stride); | |
| 1162 | 26215 | bx++; | |
| 1163 | 26215 | dst += 8; | |
| 1164 | 26215 | prev += 8; | |
| 1165 | 26215 | break; | |
| 1166 | 97378 | case MOTION_BLOCK: | |
| 1167 | 97378 | ret = bink_put_pixels(c, dst, prev, stride, | |
| 1168 | ref_start, ref_end); | ||
| 1169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 97378 times.
|
97378 | if (ret < 0) |
| 1170 | ✗ | return ret; | |
| 1171 | 97378 | break; | |
| 1172 | 11625 | case RUN_BLOCK: | |
| 1173 | 11625 | scan = bink_patterns[get_bits(gb, 4)]; | |
| 1174 | 11625 | i = 0; | |
| 1175 | do { | ||
| 1176 | 90262 | int run = get_value(c, BINK_SRC_RUN) + 1; | |
| 1177 | |||
| 1178 | 90262 | i += run; | |
| 1179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90262 times.
|
90262 | if (i > 64) { |
| 1180 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); | |
| 1181 | ✗ | return AVERROR_INVALIDDATA; | |
| 1182 | } | ||
| 1183 |
2/2✓ Branch 1 taken 76178 times.
✓ Branch 2 taken 14084 times.
|
90262 | if (get_bits1(gb)) { |
| 1184 | 76178 | v = get_value(c, BINK_SRC_COLORS); | |
| 1185 |
2/2✓ Branch 0 taken 695057 times.
✓ Branch 1 taken 76178 times.
|
771235 | for (j = 0; j < run; j++) |
| 1186 | 695057 | dst[coordmap[*scan++]] = v; | |
| 1187 | } else { | ||
| 1188 |
2/2✓ Branch 0 taken 48041 times.
✓ Branch 1 taken 14084 times.
|
62125 | for (j = 0; j < run; j++) |
| 1189 | 48041 | dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS); | |
| 1190 | } | ||
| 1191 |
2/2✓ Branch 0 taken 78637 times.
✓ Branch 1 taken 11625 times.
|
90262 | } while (i < 63); |
| 1192 |
2/2✓ Branch 0 taken 902 times.
✓ Branch 1 taken 10723 times.
|
11625 | if (i == 63) |
| 1193 | 902 | dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS); | |
| 1194 | 11625 | break; | |
| 1195 | 3750 | case RESIDUE_BLOCK: | |
| 1196 | 3750 | ret = bink_put_pixels(c, dst, prev, stride, | |
| 1197 | ref_start, ref_end); | ||
| 1198 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3750 times.
|
3750 | if (ret < 0) |
| 1199 | ✗ | return ret; | |
| 1200 | 3750 | c->bdsp.clear_block(block); | |
| 1201 | 3750 | v = get_bits(gb, 7); | |
| 1202 | 3750 | read_residue(gb, block, v); | |
| 1203 | 3750 | c->binkdsp.add_pixels8(dst, block, stride); | |
| 1204 | 3750 | break; | |
| 1205 | 16881 | case INTRA_BLOCK: | |
| 1206 | 16881 | memset(dctblock, 0, sizeof(*dctblock) * 64); | |
| 1207 | 16881 | dctblock[0] = get_value(c, BINK_SRC_INTRA_DC); | |
| 1208 |
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) |
| 1209 | ✗ | return quant_idx; | |
| 1210 | 16881 | unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan); | |
| 1211 | 16881 | c->binkdsp.idct_put(dst, stride, dctblock); | |
| 1212 | 16881 | break; | |
| 1213 | 31947 | case FILL_BLOCK: | |
| 1214 | 31947 | v = get_value(c, BINK_SRC_COLORS); | |
| 1215 | 31947 | c->bdsp.fill_block_tab[1](dst, v, stride, 8); | |
| 1216 | 31947 | break; | |
| 1217 | 9168 | case INTER_BLOCK: | |
| 1218 | 9168 | ret = bink_put_pixels(c, dst, prev, stride, | |
| 1219 | ref_start, ref_end); | ||
| 1220 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9168 times.
|
9168 | if (ret < 0) |
| 1221 | ✗ | return ret; | |
| 1222 | 9168 | memset(dctblock, 0, sizeof(*dctblock) * 64); | |
| 1223 | 9168 | dctblock[0] = get_value(c, BINK_SRC_INTER_DC); | |
| 1224 |
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) |
| 1225 | ✗ | return quant_idx; | |
| 1226 | 9168 | unquantize_dct_coeffs(dctblock, bink_inter_quant[quant_idx], coef_count, coef_idx, bink_scan); | |
| 1227 | 9168 | c->binkdsp.idct_add(dst, stride, dctblock); | |
| 1228 | 9168 | break; | |
| 1229 | 5911 | case PATTERN_BLOCK: | |
| 1230 |
2/2✓ Branch 0 taken 11822 times.
✓ Branch 1 taken 5911 times.
|
17733 | for (i = 0; i < 2; i++) |
| 1231 | 11822 | col[i] = get_value(c, BINK_SRC_COLORS); | |
| 1232 |
2/2✓ Branch 0 taken 47288 times.
✓ Branch 1 taken 5911 times.
|
53199 | for (i = 0; i < 8; i++) { |
| 1233 | 47288 | v = get_value(c, BINK_SRC_PATTERN); | |
| 1234 |
2/2✓ Branch 0 taken 378304 times.
✓ Branch 1 taken 47288 times.
|
425592 | for (j = 0; j < 8; j++, v >>= 1) |
| 1235 | 378304 | dst[i*stride + j] = col[v & 1]; | |
| 1236 | } | ||
| 1237 | 5911 | break; | |
| 1238 | 130 | case RAW_BLOCK: | |
| 1239 |
2/2✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 130 times.
|
1170 | for (i = 0; i < 8; i++) |
| 1240 | 1040 | memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8); | |
| 1241 | 130 | c->bundle[BINK_SRC_COLORS].cur_ptr += 64; | |
| 1242 | 130 | break; | |
| 1243 | ✗ | default: | |
| 1244 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk); | |
| 1245 | ✗ | return AVERROR_INVALIDDATA; | |
| 1246 | } | ||
| 1247 | } | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | 153 | end: | |
| 1251 |
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 |
| 1252 | 151 | skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F)); | |
| 1253 | |||
| 1254 | 153 | return 0; | |
| 1255 | } | ||
| 1256 | |||
| 1257 | 85 | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 1258 | int *got_frame, AVPacket *pkt) | ||
| 1259 | { | ||
| 1260 | 85 | BinkContext * const c = avctx->priv_data; | |
| 1261 | GetBitContext gb; | ||
| 1262 | int plane, plane_idx, ret; | ||
| 1263 | 85 | int bits_count = pkt->size << 3; | |
| 1264 | |||
| 1265 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 34 times.
|
85 | if (c->version > 'b') { |
| 1266 |
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) |
| 1267 | ✗ | return ret; | |
| 1268 | } else { | ||
| 1269 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
|
34 | if ((ret = ff_reget_buffer(avctx, c->last, 0)) < 0) |
| 1270 | ✗ | return ret; | |
| 1271 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
|
34 | if ((ret = av_frame_ref(frame, c->last)) < 0) |
| 1272 | ✗ | return ret; | |
| 1273 | } | ||
| 1274 | |||
| 1275 | 85 | init_get_bits(&gb, pkt->data, bits_count); | |
| 1276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
|
85 | if (c->has_alpha) { |
| 1277 | ✗ | if (c->version >= 'i') | |
| 1278 | ✗ | skip_bits_long(&gb, 32); | |
| 1279 | ✗ | if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0) | |
| 1280 | ✗ | return ret; | |
| 1281 | } | ||
| 1282 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 54 times.
|
85 | if (c->version >= 'i') |
| 1283 | 31 | skip_bits_long(&gb, 32); | |
| 1284 | |||
| 1285 | 85 | c->frame_num++; | |
| 1286 | |||
| 1287 |
1/2✓ Branch 0 taken 255 times.
✗ Branch 1 not taken.
|
255 | for (plane = 0; plane < 3; plane++) { |
| 1288 |
4/4✓ Branch 0 taken 170 times.
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 62 times.
✓ Branch 3 taken 108 times.
|
255 | plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3); |
| 1289 | |||
| 1290 |
2/2✓ Branch 0 taken 153 times.
✓ Branch 1 taken 102 times.
|
255 | if (c->version > 'b') { |
| 1291 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
|
153 | if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0) |
| 1292 | ✗ | return ret; | |
| 1293 | } else { | ||
| 1294 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
|
102 | if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx, |
| 1295 | 102 | c->frame_num == 1, !!plane)) < 0) | |
| 1296 | ✗ | return ret; | |
| 1297 | } | ||
| 1298 |
2/2✓ Branch 1 taken 85 times.
✓ Branch 2 taken 170 times.
|
255 | if (get_bits_count(&gb) >= bits_count) |
| 1299 | 85 | break; | |
| 1300 | } | ||
| 1301 | |||
| 1302 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 34 times.
|
85 | if (c->version > 'b') { |
| 1303 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
|
51 | if ((ret = av_frame_replace(c->last, frame)) < 0) |
| 1304 | ✗ | return ret; | |
| 1305 | } | ||
| 1306 | |||
| 1307 | 85 | *got_frame = 1; | |
| 1308 | |||
| 1309 | /* always report that the buffer was completely consumed */ | ||
| 1310 | 85 | 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 VLCElem table[976]; | ||
| 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 | vlc_init(&bink_trees[i], maxbits, 16, | |
| 1322 | bink_tree_lens[i], 1, 1, | ||
| 1323 | bink_tree_bits[i], 1, 1, VLC_INIT_USE_STATIC | VLC_INIT_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); | |
| 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 av_cold void flush(AVCodecContext *avctx) | |
| 1415 | { | ||
| 1416 | ✗ | BinkContext * const c = avctx->priv_data; | |
| 1417 | |||
| 1418 | ✗ | c->frame_num = 0; | |
| 1419 | ✗ | } | |
| 1420 | |||
| 1421 | const FFCodec ff_bink_decoder = { | ||
| 1422 | .p.name = "binkvideo", | ||
| 1423 | CODEC_LONG_NAME("Bink video"), | ||
| 1424 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 1425 | .p.id = AV_CODEC_ID_BINKVIDEO, | ||
| 1426 | .priv_data_size = sizeof(BinkContext), | ||
| 1427 | .init = decode_init, | ||
| 1428 | .close = decode_end, | ||
| 1429 | FF_CODEC_DECODE_CB(decode_frame), | ||
| 1430 | .flush = flush, | ||
| 1431 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 1432 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 1433 | }; | ||
| 1434 |