/src/ffmpeg/libavcodec/bink.c
Line | Count | 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 | 1.66k | #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 | 236M | #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\ |
99 | 236M | 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 | | static void init_lengths(BinkContext *c, int width, int bw) |
156 | 58.1k | { |
157 | 58.1k | width = FFALIGN(width, 8); |
158 | | |
159 | 58.1k | c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1; |
160 | | |
161 | 58.1k | c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1; |
162 | | |
163 | 58.1k | c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1; |
164 | | |
165 | 58.1k | c->bundle[BINK_SRC_INTRA_DC].len = |
166 | 58.1k | c->bundle[BINK_SRC_INTER_DC].len = |
167 | 58.1k | c->bundle[BINK_SRC_X_OFF].len = |
168 | 58.1k | c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1; |
169 | | |
170 | 58.1k | c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1; |
171 | | |
172 | 58.1k | c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1; |
173 | 58.1k | } |
174 | | |
175 | | /** |
176 | | * Allocate memory for bundles. |
177 | | * |
178 | | * @param c decoder context |
179 | | */ |
180 | | static av_cold int init_bundles(BinkContext *c) |
181 | 1.65k | { |
182 | 1.65k | int bw, bh, blocks; |
183 | 1.65k | uint8_t *tmp; |
184 | 1.65k | int i; |
185 | | |
186 | 1.65k | bw = (c->avctx->width + 7) >> 3; |
187 | 1.65k | bh = (c->avctx->height + 7) >> 3; |
188 | 1.65k | blocks = bw * bh; |
189 | | |
190 | 1.65k | tmp = av_calloc(blocks, 64 * BINKB_NB_SRC); |
191 | 1.65k | if (!tmp) |
192 | 0 | return AVERROR(ENOMEM); |
193 | 18.2k | for (i = 0; i < BINKB_NB_SRC; i++) { |
194 | 16.5k | c->bundle[i].data = tmp; |
195 | 16.5k | tmp += blocks * 64; |
196 | 16.5k | c->bundle[i].data_end = tmp; |
197 | 16.5k | } |
198 | | |
199 | 1.65k | return 0; |
200 | 1.65k | } |
201 | | |
202 | | /** |
203 | | * Free memory used by bundles. |
204 | | * |
205 | | * @param c decoder context |
206 | | */ |
207 | | static av_cold void free_bundles(BinkContext *c) |
208 | 1.83k | { |
209 | 1.83k | av_freep(&c->bundle[0].data); |
210 | 1.83k | } |
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 | | static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size) |
221 | 577k | { |
222 | 577k | uint8_t *src2 = src + size; |
223 | 577k | int size2 = size; |
224 | | |
225 | 897k | do { |
226 | 897k | if (!get_bits1(gb)) { |
227 | 642k | *dst++ = *src++; |
228 | 642k | size--; |
229 | 642k | } else { |
230 | 255k | *dst++ = *src2++; |
231 | 255k | size2--; |
232 | 255k | } |
233 | 897k | } while (size && size2); |
234 | | |
235 | 756k | while (size--) |
236 | 179k | *dst++ = *src++; |
237 | 1.14M | while (size2--) |
238 | 566k | *dst++ = *src2++; |
239 | 577k | } |
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 | | static int read_tree(GetBitContext *gb, Tree *tree) |
248 | 519k | { |
249 | 519k | uint8_t tmp1[16] = { 0 }, tmp2[16], *in = tmp1, *out = tmp2; |
250 | 519k | int i, t, len; |
251 | | |
252 | 519k | if (get_bits_left(gb) < 4) |
253 | 40.4k | return AVERROR_INVALIDDATA; |
254 | | |
255 | 479k | tree->vlc_num = get_bits(gb, 4); |
256 | 479k | if (!tree->vlc_num) { |
257 | 6.15M | for (i = 0; i < 16; i++) |
258 | 5.79M | tree->syms[i] = i; |
259 | 362k | return 0; |
260 | 362k | } |
261 | 116k | if (get_bits1(gb)) { |
262 | 64.0k | len = get_bits(gb, 3); |
263 | 390k | for (i = 0; i <= len; i++) { |
264 | 326k | tree->syms[i] = get_bits(gb, 4); |
265 | 326k | tmp1[tree->syms[i]] = 1; |
266 | 326k | } |
267 | 898k | for (i = 0; i < 16 && len < 16 - 1; i++) |
268 | 833k | if (!tmp1[i]) |
269 | 698k | tree->syms[++len] = i; |
270 | 64.0k | } else { |
271 | 52.9k | len = get_bits(gb, 2); |
272 | 899k | for (i = 0; i < 16; i++) |
273 | 846k | in[i] = i; |
274 | 155k | for (i = 0; i <= len; i++) { |
275 | 102k | int size = 1 << i; |
276 | 679k | for (t = 0; t < 16; t += size << 1) |
277 | 577k | merge(gb, out + t, in + t, size); |
278 | 102k | FFSWAP(uint8_t*, in, out); |
279 | 102k | } |
280 | 52.9k | memcpy(tree->syms, in, 16); |
281 | 52.9k | } |
282 | 116k | return 0; |
283 | 479k | } |
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 | | static int read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num) |
293 | 237k | { |
294 | 237k | int i; |
295 | | |
296 | 237k | if (bundle_num == BINK_SRC_COLORS) { |
297 | 345k | for (i = 0; i < 16; i++) { |
298 | 327k | int ret = read_tree(gb, &c->col_high[i]); |
299 | 327k | if (ret < 0) |
300 | 9.74k | return ret; |
301 | 327k | } |
302 | 18.2k | c->col_lastval = 0; |
303 | 18.2k | } |
304 | 227k | if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC) { |
305 | 191k | int ret = read_tree(gb, &c->bundle[bundle_num].tree); |
306 | 191k | if (ret < 0) |
307 | 30.6k | return ret; |
308 | 191k | } |
309 | 196k | c->bundle[bundle_num].cur_dec = |
310 | 196k | c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data; |
311 | | |
312 | 196k | return 0; |
313 | 227k | } |
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 | 440M | if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \ |
324 | 440M | return 0; \ |
325 | 440M | t = get_bits(gb, b->len); \ |
326 | 1.39M | if (!t) { \ |
327 | 1.23M | b->cur_dec = NULL; \ |
328 | 1.23M | return 0; \ |
329 | 1.23M | } \ |
330 | | |
331 | | static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b) |
332 | 4.22M | { |
333 | 4.22M | int t, v; |
334 | 4.22M | const uint8_t *dec_end; |
335 | | |
336 | 4.22M | CHECK_READ_VAL(gb, b, t); |
337 | 6.01k | dec_end = b->cur_dec + t; |
338 | 6.01k | if (dec_end > b->data_end) { |
339 | 199 | av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n"); |
340 | 199 | return AVERROR_INVALIDDATA; |
341 | 199 | } |
342 | 5.81k | if (get_bits_left(gb) < 1) |
343 | 258 | return AVERROR_INVALIDDATA; |
344 | 5.55k | if (get_bits1(gb)) { |
345 | 1.92k | v = get_bits(gb, 4); |
346 | 1.92k | memset(b->cur_dec, v, t); |
347 | 1.92k | b->cur_dec += t; |
348 | 3.63k | } else { |
349 | 167M | while (b->cur_dec < dec_end) |
350 | 167M | *b->cur_dec++ = GET_HUFF(gb, b->tree); |
351 | 3.63k | } |
352 | 5.55k | return 0; |
353 | 5.81k | } |
354 | | |
355 | | static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b) |
356 | 8.44M | { |
357 | 8.44M | int t, sign, v; |
358 | 8.44M | const uint8_t *dec_end; |
359 | | |
360 | 8.44M | CHECK_READ_VAL(gb, b, t); |
361 | 5.47k | dec_end = b->cur_dec + t; |
362 | 5.47k | if (dec_end > b->data_end) { |
363 | 398 | av_log(avctx, AV_LOG_ERROR, "Too many motion values\n"); |
364 | 398 | return AVERROR_INVALIDDATA; |
365 | 398 | } |
366 | 5.07k | if (get_bits_left(gb) < 1) |
367 | 226 | return AVERROR_INVALIDDATA; |
368 | 4.84k | if (get_bits1(gb)) { |
369 | 3.31k | v = get_bits(gb, 4); |
370 | 3.31k | if (v) { |
371 | 2.93k | sign = -get_bits1(gb); |
372 | 2.93k | v = (v ^ sign) - sign; |
373 | 2.93k | } |
374 | 3.31k | memset(b->cur_dec, v, t); |
375 | 3.31k | b->cur_dec += t; |
376 | 3.31k | } else { |
377 | 2.06M | while (b->cur_dec < dec_end) { |
378 | 2.06M | v = GET_HUFF(gb, b->tree); |
379 | 2.06M | if (v) { |
380 | 569k | sign = -get_bits1(gb); |
381 | 569k | v = (v ^ sign) - sign; |
382 | 569k | } |
383 | 2.06M | *b->cur_dec++ = v; |
384 | 2.06M | } |
385 | 1.53k | } |
386 | 4.84k | return 0; |
387 | 5.07k | } |
388 | | |
389 | | static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 }; |
390 | | |
391 | | static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b) |
392 | 8.45M | { |
393 | 8.45M | BinkContext * const c = avctx->priv_data; |
394 | 8.45M | int t, v; |
395 | 8.45M | int last = 0; |
396 | 8.45M | const uint8_t *dec_end; |
397 | | |
398 | 8.45M | CHECK_READ_VAL(gb, b, t); |
399 | 11.3k | if (c->version == 'k') { |
400 | 1.07k | t ^= 0xBBu; |
401 | 1.07k | if (t == 0) { |
402 | 205 | b->cur_dec = NULL; |
403 | 205 | return 0; |
404 | 205 | } |
405 | 1.07k | } |
406 | 11.1k | dec_end = b->cur_dec + t; |
407 | 11.1k | if (dec_end > b->data_end) { |
408 | 234 | av_log(avctx, AV_LOG_ERROR, "Too many block type values\n"); |
409 | 234 | return AVERROR_INVALIDDATA; |
410 | 234 | } |
411 | 10.9k | if (get_bits_left(gb) < 1) |
412 | 300 | return AVERROR_INVALIDDATA; |
413 | 10.6k | if (get_bits1(gb)) { |
414 | 4.30k | v = get_bits(gb, 4); |
415 | 4.30k | memset(b->cur_dec, v, t); |
416 | 4.30k | b->cur_dec += t; |
417 | 6.32k | } else { |
418 | 12.2M | while (b->cur_dec < dec_end) { |
419 | 12.2M | v = GET_HUFF(gb, b->tree); |
420 | 12.2M | if (v < 12) { |
421 | 12.1M | last = v; |
422 | 12.1M | *b->cur_dec++ = v; |
423 | 12.1M | } else { |
424 | 116k | int run = bink_rlelens[v - 12]; |
425 | | |
426 | 116k | if (dec_end - b->cur_dec < run) |
427 | 516 | return AVERROR_INVALIDDATA; |
428 | 116k | memset(b->cur_dec, last, run); |
429 | 116k | b->cur_dec += run; |
430 | 116k | } |
431 | 12.2M | } |
432 | 6.32k | } |
433 | 10.1k | return 0; |
434 | 10.6k | } |
435 | | |
436 | | static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b) |
437 | 4.22M | { |
438 | 4.22M | int t, v; |
439 | 4.22M | const uint8_t *dec_end; |
440 | | |
441 | 4.22M | CHECK_READ_VAL(gb, b, t); |
442 | 1.98k | dec_end = b->cur_dec + t; |
443 | 1.98k | if (dec_end > b->data_end) { |
444 | 217 | av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n"); |
445 | 217 | return AVERROR_INVALIDDATA; |
446 | 217 | } |
447 | 3.56M | while (b->cur_dec < dec_end) { |
448 | 3.56M | if (get_bits_left(gb) < 2) |
449 | 770 | return AVERROR_INVALIDDATA; |
450 | 3.56M | v = GET_HUFF(gb, b->tree); |
451 | 3.56M | v |= GET_HUFF(gb, b->tree) << 4; |
452 | 3.56M | *b->cur_dec++ = v; |
453 | 3.56M | } |
454 | | |
455 | 993 | return 0; |
456 | 1.76k | } |
457 | | |
458 | | static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c) |
459 | 4.22M | { |
460 | 4.22M | int t, sign, v; |
461 | 4.22M | const uint8_t *dec_end; |
462 | | |
463 | 4.22M | CHECK_READ_VAL(gb, b, t); |
464 | 11.9k | dec_end = b->cur_dec + t; |
465 | 11.9k | if (dec_end > b->data_end) { |
466 | 252 | av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n"); |
467 | 252 | return AVERROR_INVALIDDATA; |
468 | 252 | } |
469 | 11.7k | if (get_bits_left(gb) < 1) |
470 | 319 | return AVERROR_INVALIDDATA; |
471 | 11.3k | if (get_bits1(gb)) { |
472 | 4.36k | c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]); |
473 | 4.36k | v = GET_HUFF(gb, b->tree); |
474 | 4.36k | v = (c->col_lastval << 4) | v; |
475 | 4.36k | if (c->version < 'i') { |
476 | 3.80k | sign = ((int8_t) v) >> 7; |
477 | 3.80k | v = ((v & 0x7F) ^ sign) - sign; |
478 | 3.80k | v += 0x80; |
479 | 3.80k | } |
480 | 4.36k | memset(b->cur_dec, v, t); |
481 | 4.36k | b->cur_dec += t; |
482 | 7.01k | } else { |
483 | 23.5M | while (b->cur_dec < dec_end) { |
484 | 23.5M | if (get_bits_left(gb) < 2) |
485 | 890 | return AVERROR_INVALIDDATA; |
486 | 23.5M | c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]); |
487 | 23.5M | v = GET_HUFF(gb, b->tree); |
488 | 23.5M | v = (c->col_lastval << 4) | v; |
489 | 23.5M | if (c->version < 'i') { |
490 | 12.9M | sign = ((int8_t) v) >> 7; |
491 | 12.9M | v = ((v & 0x7F) ^ sign) - sign; |
492 | 12.9M | v += 0x80; |
493 | 12.9M | } |
494 | 23.5M | *b->cur_dec++ = v; |
495 | 23.5M | } |
496 | 7.01k | } |
497 | 10.4k | return 0; |
498 | 11.3k | } |
499 | | |
500 | | /** number of bits used to store first DC value in bundle */ |
501 | 8.44M | #define DC_START_BITS 11 |
502 | | |
503 | | static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b, |
504 | | int start_bits, int has_sign) |
505 | 8.44M | { |
506 | 8.44M | int i, j, len, len2, bsize, sign, v, v2; |
507 | 8.44M | int16_t *dst = (int16_t*)b->cur_dec; |
508 | 8.44M | int16_t *dst_end = (int16_t*)b->data_end; |
509 | | |
510 | 8.44M | CHECK_READ_VAL(gb, b, len); |
511 | 4.27k | if (get_bits_left(gb) < start_bits - has_sign) |
512 | 405 | return AVERROR_INVALIDDATA; |
513 | 3.86k | v = get_bits(gb, start_bits - has_sign); |
514 | 3.86k | if (v && has_sign) { |
515 | 793 | sign = -get_bits1(gb); |
516 | 793 | v = (v ^ sign) - sign; |
517 | 793 | } |
518 | 3.86k | if (dst_end - dst < 1) |
519 | 0 | return AVERROR_INVALIDDATA; |
520 | 3.86k | *dst++ = v; |
521 | 3.86k | len--; |
522 | 740k | for (i = 0; i < len; i += 8) { |
523 | 737k | len2 = FFMIN(len - i, 8); |
524 | 737k | if (dst_end - dst < len2) |
525 | 299 | return AVERROR_INVALIDDATA; |
526 | 737k | bsize = get_bits(gb, 4); |
527 | 737k | if (bsize) { |
528 | 201k | for (j = 0; j < len2; j++) { |
529 | 178k | v2 = get_bits(gb, bsize); |
530 | 178k | if (v2) { |
531 | 102k | sign = -get_bits1(gb); |
532 | 102k | v2 = (v2 ^ sign) - sign; |
533 | 102k | } |
534 | 178k | v += v2; |
535 | 178k | *dst++ = v; |
536 | 178k | if (v < -32768 || v > 32767) { |
537 | 624 | av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v); |
538 | 624 | return AVERROR_INVALIDDATA; |
539 | 624 | } |
540 | 178k | } |
541 | 714k | } else { |
542 | 6.42M | for (j = 0; j < len2; j++) |
543 | 5.71M | *dst++ = v; |
544 | 714k | } |
545 | 737k | } |
546 | | |
547 | 2.94k | b->cur_dec = (uint8_t*)dst; |
548 | 2.94k | return 0; |
549 | 3.86k | } |
550 | | |
551 | | /** |
552 | | * Retrieve next value from bundle. |
553 | | * |
554 | | * @param c decoder context |
555 | | * @param bundle bundle number |
556 | | */ |
557 | | static inline int get_value(BinkContext *c, int bundle) |
558 | 352M | { |
559 | 352M | int ret; |
560 | | |
561 | 352M | if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN) |
562 | 344M | return *c->bundle[bundle].cur_ptr++; |
563 | 7.30M | if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF) |
564 | 6.77M | return (int8_t)*c->bundle[bundle].cur_ptr++; |
565 | 538k | ret = *(int16_t*)c->bundle[bundle].cur_ptr; |
566 | 538k | c->bundle[bundle].cur_ptr += 2; |
567 | 538k | return ret; |
568 | 7.30M | } |
569 | | |
570 | | static av_cold void binkb_init_bundle(BinkContext *c, int bundle_num) |
571 | 1.15M | { |
572 | 1.15M | c->bundle[bundle_num].cur_dec = |
573 | 1.15M | c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data; |
574 | 1.15M | c->bundle[bundle_num].len = 13; |
575 | 1.15M | } |
576 | | |
577 | | static av_cold void binkb_init_bundles(BinkContext *c) |
578 | 115k | { |
579 | 115k | int i; |
580 | 1.27M | for (i = 0; i < BINKB_NB_SRC; i++) |
581 | 1.15M | binkb_init_bundle(c, i); |
582 | 115k | } |
583 | | |
584 | | static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num) |
585 | 402M | { |
586 | 402M | const int bits = binkb_bundle_sizes[bundle_num]; |
587 | 402M | const int mask = 1 << (bits - 1); |
588 | 402M | const int issigned = binkb_bundle_signed[bundle_num]; |
589 | 402M | Bundle *b = &c->bundle[bundle_num]; |
590 | 402M | int i, len; |
591 | | |
592 | 402M | CHECK_READ_VAL(gb, b, len); |
593 | 114k | if (b->data_end - b->cur_dec < len * (1 + (bits > 8))) |
594 | 570 | return AVERROR_INVALIDDATA; |
595 | 113k | if (bits <= 8) { |
596 | 112k | if (!issigned) { |
597 | 43.0M | for (i = 0; i < len; i++) |
598 | 42.9M | *b->cur_dec++ = get_bits(gb, bits); |
599 | 111k | } else { |
600 | 3.77M | for (i = 0; i < len; i++) |
601 | 3.77M | *b->cur_dec++ = get_bits(gb, bits) - mask; |
602 | 1.11k | } |
603 | 112k | } else { |
604 | 1.12k | int16_t *dst = (int16_t*)b->cur_dec; |
605 | | |
606 | 1.12k | if (!issigned) { |
607 | 1.27M | for (i = 0; i < len; i++) |
608 | 1.27M | *dst++ = get_bits(gb, bits); |
609 | 585 | } else { |
610 | 2.22M | for (i = 0; i < len; i++) |
611 | 2.22M | *dst++ = get_bits(gb, bits) - mask; |
612 | 536 | } |
613 | 1.12k | b->cur_dec = (uint8_t*)dst; |
614 | 1.12k | } |
615 | 113k | return 0; |
616 | 114k | } |
617 | | |
618 | | static inline int binkb_get_value(BinkContext *c, int bundle_num) |
619 | 2.22G | { |
620 | 2.22G | int16_t ret; |
621 | 2.22G | const int bits = binkb_bundle_sizes[bundle_num]; |
622 | | |
623 | 2.22G | if (bits <= 8) { |
624 | 2.22G | int val = *c->bundle[bundle_num].cur_ptr++; |
625 | 2.22G | return binkb_bundle_signed[bundle_num] ? (int8_t)val : val; |
626 | 2.22G | } |
627 | 39.4k | ret = *(int16_t*)c->bundle[bundle_num].cur_ptr; |
628 | 39.4k | c->bundle[bundle_num].cur_ptr += 2; |
629 | 39.4k | return ret; |
630 | 2.22G | } |
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 | | 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 | 577k | { |
645 | 577k | int coef_list[128]; |
646 | 577k | int mode_list[128]; |
647 | 577k | int i, t, bits, ccoef, mode, sign; |
648 | 577k | int list_start = 64, list_end = 64, list_pos; |
649 | 577k | int coef_count = 0; |
650 | 577k | int quant_idx; |
651 | | |
652 | 577k | if (get_bits_left(gb) < 4) |
653 | 7.71k | return AVERROR_INVALIDDATA; |
654 | | |
655 | 570k | coef_list[list_end] = 4; mode_list[list_end++] = 0; |
656 | 570k | coef_list[list_end] = 24; mode_list[list_end++] = 0; |
657 | 570k | coef_list[list_end] = 44; mode_list[list_end++] = 0; |
658 | 570k | coef_list[list_end] = 1; mode_list[list_end++] = 3; |
659 | 570k | coef_list[list_end] = 2; mode_list[list_end++] = 3; |
660 | 570k | coef_list[list_end] = 3; mode_list[list_end++] = 3; |
661 | | |
662 | 2.32M | for (bits = get_bits(gb, 4) - 1; bits >= 0; bits--) { |
663 | 1.75M | list_pos = list_start; |
664 | 34.8M | while (list_pos < list_end) { |
665 | 33.0M | if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) { |
666 | 26.1M | list_pos++; |
667 | 26.1M | continue; |
668 | 26.1M | } |
669 | 6.93M | ccoef = coef_list[list_pos]; |
670 | 6.93M | mode = mode_list[list_pos]; |
671 | 6.93M | switch (mode) { |
672 | 1.05M | case 0: |
673 | 1.05M | coef_list[list_pos] = ccoef + 4; |
674 | 1.05M | mode_list[list_pos] = 1; |
675 | 1.05M | av_fallthrough; |
676 | 2.92M | case 2: |
677 | 2.92M | if (mode == 2) { |
678 | 1.87M | coef_list[list_pos] = 0; |
679 | 1.87M | mode_list[list_pos++] = 0; |
680 | 1.87M | } |
681 | 14.6M | for (i = 0; i < 4; i++, ccoef++) { |
682 | 11.7M | if (get_bits1(gb)) { |
683 | 4.74M | coef_list[--list_start] = ccoef; |
684 | 4.74M | mode_list[ list_start] = 3; |
685 | 6.96M | } else { |
686 | 6.96M | if (!bits) { |
687 | 1.89M | t = 1 - (get_bits1(gb) << 1); |
688 | 5.06M | } else { |
689 | 5.06M | t = get_bits(gb, bits) | 1 << bits; |
690 | 5.06M | sign = -get_bits1(gb); |
691 | 5.06M | t = (t ^ sign) - sign; |
692 | 5.06M | } |
693 | 6.96M | block[scan[ccoef]] = t; |
694 | 6.96M | coef_idx[coef_count++] = ccoef; |
695 | 6.96M | } |
696 | 11.7M | } |
697 | 2.92M | break; |
698 | 689k | case 1: |
699 | 689k | mode_list[list_pos] = 2; |
700 | 2.75M | for (i = 0; i < 3; i++) { |
701 | 2.06M | ccoef += 4; |
702 | 2.06M | coef_list[list_end] = ccoef; |
703 | 2.06M | mode_list[list_end++] = 2; |
704 | 2.06M | } |
705 | 689k | break; |
706 | 3.31M | case 3: |
707 | 3.31M | if (!bits) { |
708 | 615k | t = 1 - (get_bits1(gb) << 1); |
709 | 2.70M | } else { |
710 | 2.70M | t = get_bits(gb, bits) | 1 << bits; |
711 | 2.70M | sign = -get_bits1(gb); |
712 | 2.70M | t = (t ^ sign) - sign; |
713 | 2.70M | } |
714 | 3.31M | block[scan[ccoef]] = t; |
715 | 3.31M | coef_idx[coef_count++] = ccoef; |
716 | 3.31M | coef_list[list_pos] = 0; |
717 | 3.31M | mode_list[list_pos++] = 0; |
718 | 3.31M | break; |
719 | 6.93M | } |
720 | 6.93M | } |
721 | 1.75M | } |
722 | | |
723 | 570k | if (q == -1) { |
724 | 536k | quant_idx = get_bits(gb, 4); |
725 | 536k | } else { |
726 | 33.9k | quant_idx = q; |
727 | 33.9k | if (quant_idx > 15U) { |
728 | 197 | av_log(c->avctx, AV_LOG_ERROR, "quant_index %d out of range\n", quant_idx); |
729 | 197 | return AVERROR_INVALIDDATA; |
730 | 197 | } |
731 | 33.9k | } |
732 | | |
733 | 570k | *coef_count_ = coef_count; |
734 | | |
735 | 570k | return quant_idx; |
736 | 570k | } |
737 | | |
738 | | 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 | 570k | { |
742 | 570k | int i; |
743 | 570k | block[0] = (int)(block[0] * quant[0]) >> 11; |
744 | 10.8M | for (i = 0; i < coef_count; i++) { |
745 | 10.2M | int idx = coef_idx[i]; |
746 | 10.2M | block[scan[idx]] = (int)(block[scan[idx]] * quant[idx]) >> 11; |
747 | 10.2M | } |
748 | 570k | } |
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 | | static int read_residue(GetBitContext *gb, int16_t block[64], int masks_count) |
759 | 1.74M | { |
760 | 1.74M | int coef_list[128]; |
761 | 1.74M | int mode_list[128]; |
762 | 1.74M | int i, sign, mask, ccoef, mode; |
763 | 1.74M | int list_start = 64, list_end = 64, list_pos; |
764 | 1.74M | int nz_coeff[64]; |
765 | 1.74M | int nz_coeff_count = 0; |
766 | | |
767 | 1.74M | coef_list[list_end] = 4; mode_list[list_end++] = 0; |
768 | 1.74M | coef_list[list_end] = 24; mode_list[list_end++] = 0; |
769 | 1.74M | coef_list[list_end] = 44; mode_list[list_end++] = 0; |
770 | 1.74M | coef_list[list_end] = 0; mode_list[list_end++] = 2; |
771 | | |
772 | 3.87M | for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) { |
773 | 9.30M | for (i = 0; i < nz_coeff_count; i++) { |
774 | 7.05M | if (!get_bits1(gb)) |
775 | 4.03M | continue; |
776 | 3.01M | if (block[nz_coeff[i]] < 0) |
777 | 824k | block[nz_coeff[i]] -= mask; |
778 | 2.18M | else |
779 | 2.18M | block[nz_coeff[i]] += mask; |
780 | 3.01M | masks_count--; |
781 | 3.01M | if (masks_count < 0) |
782 | 93.7k | return 0; |
783 | 3.01M | } |
784 | 2.25M | list_pos = list_start; |
785 | 19.3M | while (list_pos < list_end) { |
786 | 17.2M | if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) { |
787 | 13.0M | list_pos++; |
788 | 13.0M | continue; |
789 | 13.0M | } |
790 | 4.16M | ccoef = coef_list[list_pos]; |
791 | 4.16M | mode = mode_list[list_pos]; |
792 | 4.16M | switch (mode) { |
793 | 885k | case 0: |
794 | 885k | coef_list[list_pos] = ccoef + 4; |
795 | 885k | mode_list[list_pos] = 1; |
796 | 885k | av_fallthrough; |
797 | 2.42M | case 2: |
798 | 2.42M | if (mode == 2) { |
799 | 1.53M | coef_list[list_pos] = 0; |
800 | 1.53M | mode_list[list_pos++] = 0; |
801 | 1.53M | } |
802 | 11.6M | for (i = 0; i < 4; i++, ccoef++) { |
803 | 9.35M | if (get_bits1(gb)) { |
804 | 3.97M | coef_list[--list_start] = ccoef; |
805 | 3.97M | mode_list[ list_start] = 3; |
806 | 5.38M | } else { |
807 | 5.38M | nz_coeff[nz_coeff_count++] = bink_scan[ccoef]; |
808 | 5.38M | sign = -get_bits1(gb); |
809 | 5.38M | block[bink_scan[ccoef]] = (mask ^ sign) - sign; |
810 | 5.38M | masks_count--; |
811 | 5.38M | if (masks_count < 0) |
812 | 127k | return 0; |
813 | 5.38M | } |
814 | 9.35M | } |
815 | 2.29M | break; |
816 | 2.29M | case 1: |
817 | 504k | mode_list[list_pos] = 2; |
818 | 2.01M | for (i = 0; i < 3; i++) { |
819 | 1.51M | ccoef += 4; |
820 | 1.51M | coef_list[list_end] = ccoef; |
821 | 1.51M | mode_list[list_end++] = 2; |
822 | 1.51M | } |
823 | 504k | break; |
824 | 1.23M | case 3: |
825 | 1.23M | nz_coeff[nz_coeff_count++] = bink_scan[ccoef]; |
826 | 1.23M | sign = -get_bits1(gb); |
827 | 1.23M | block[bink_scan[ccoef]] = (mask ^ sign) - sign; |
828 | 1.23M | coef_list[list_pos] = 0; |
829 | 1.23M | mode_list[list_pos++] = 0; |
830 | 1.23M | masks_count--; |
831 | 1.23M | if (masks_count < 0) |
832 | 2.16k | return 0; |
833 | 1.23M | break; |
834 | 4.16M | } |
835 | 4.16M | } |
836 | 2.25M | } |
837 | | |
838 | 1.52M | return 0; |
839 | 1.74M | } |
840 | | |
841 | | /** |
842 | | * Copy 8x8 block from source to destination, where src and dst may be overlapped |
843 | | */ |
844 | | static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride) |
845 | 384k | { |
846 | 384k | uint8_t tmp[64]; |
847 | 384k | int i; |
848 | 3.46M | for (i = 0; i < 8; i++) |
849 | 3.07M | memcpy(tmp + i*8, src + i*stride, 8); |
850 | 3.46M | for (i = 0; i < 8; i++) |
851 | 3.07M | memcpy(dst + i*stride, tmp + i*8, 8); |
852 | 384k | } |
853 | | |
854 | | static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, |
855 | | int plane_idx, int is_key, int is_chroma) |
856 | 115k | { |
857 | 115k | int blk, ret; |
858 | 115k | int i, j, bx, by; |
859 | 115k | uint8_t *dst, *ref, *ref_start, *ref_end; |
860 | 115k | int v, col[2]; |
861 | 115k | const uint8_t *scan; |
862 | 115k | int xoff, yoff; |
863 | 115k | LOCAL_ALIGNED_32(int16_t, block, [64]); |
864 | 115k | LOCAL_ALIGNED_16(int32_t, dctblock, [64]); |
865 | 115k | int coordmap[64]; |
866 | 115k | int ybias = is_key ? -15 : 0; |
867 | 115k | int qp, quant_idx, coef_count, coef_idx[64]; |
868 | | |
869 | 115k | const int stride = frame->linesize[plane_idx]; |
870 | 115k | int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3; |
871 | 115k | int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3; |
872 | | |
873 | 115k | binkb_init_bundles(c); |
874 | 115k | ref_start = frame->data[plane_idx]; |
875 | 115k | ref_end = frame->data[plane_idx] + ((bh - 1) * frame->linesize[plane_idx] + bw - 1) * 8; |
876 | | |
877 | 7.52M | for (i = 0; i < 64; i++) |
878 | 7.41M | coordmap[i] = (i & 7) + (i >> 3) * stride; |
879 | | |
880 | 40.3M | for (by = 0; by < bh; by++) { |
881 | 442M | for (i = 0; i < BINKB_NB_SRC; i++) { |
882 | 402M | if ((ret = binkb_read_bundle(c, gb, i)) < 0) |
883 | 570 | return ret; |
884 | 402M | } |
885 | | |
886 | 40.2M | dst = frame->data[plane_idx] + 8*by*stride; |
887 | 2.26G | for (bx = 0; bx < bw; bx++, dst += 8) { |
888 | 2.22G | blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES); |
889 | 2.22G | switch (blk) { |
890 | 2.21G | case 0: |
891 | 2.21G | break; |
892 | 18.6k | case 1: |
893 | 18.6k | scan = bink_patterns[get_bits(gb, 4)]; |
894 | 18.6k | i = 0; |
895 | 1.11M | do { |
896 | 1.11M | int mode, run; |
897 | | |
898 | 1.11M | mode = get_bits1(gb); |
899 | 1.11M | run = get_bits(gb, binkb_runbits[i]) + 1; |
900 | | |
901 | 1.11M | i += run; |
902 | 1.11M | if (i > 64) { |
903 | 271 | av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); |
904 | 271 | return AVERROR_INVALIDDATA; |
905 | 271 | } |
906 | 1.11M | if (mode) { |
907 | 1.20k | v = binkb_get_value(c, BINKB_SRC_COLORS); |
908 | 16.7k | for (j = 0; j < run; j++) |
909 | 15.5k | dst[coordmap[*scan++]] = v; |
910 | 1.11M | } else { |
911 | 2.27M | for (j = 0; j < run; j++) |
912 | 1.15M | dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS); |
913 | 1.11M | } |
914 | 1.11M | } while (i < 63); |
915 | 18.4k | if (i == 63) |
916 | 17.8k | dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS); |
917 | 18.4k | break; |
918 | 8.99k | case 2: |
919 | 8.99k | memset(dctblock, 0, sizeof(*dctblock) * 64); |
920 | 8.99k | dctblock[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC); |
921 | 8.99k | qp = binkb_get_value(c, BINKB_SRC_INTRA_Q); |
922 | 8.99k | if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0) |
923 | 4.74k | return quant_idx; |
924 | 4.24k | unquantize_dct_coeffs(dctblock, binkb_intra_quant[quant_idx], coef_count, coef_idx, bink_scan); |
925 | 4.24k | c->binkdsp.idct_put(dst, stride, dctblock); |
926 | 4.24k | break; |
927 | 1.03M | case 3: |
928 | 1.03M | xoff = binkb_get_value(c, BINKB_SRC_X_OFF); |
929 | 1.03M | yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias; |
930 | 1.03M | ref = dst + xoff + yoff * stride; |
931 | 1.03M | if (ref < ref_start || ref > ref_end) { |
932 | 561k | av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n"); |
933 | 561k | } else if (ref + 8*stride < dst || ref >= dst + 8*stride) { |
934 | 90.7k | c->put_pixels_tab(dst, ref, stride, 8); |
935 | 380k | } else { |
936 | 380k | put_pixels8x8_overlapped(dst, ref, stride); |
937 | 380k | } |
938 | 1.03M | c->bdsp.clear_block(block); |
939 | 1.03M | v = binkb_get_value(c, BINKB_SRC_INTER_COEFS); |
940 | 1.03M | read_residue(gb, block, v); |
941 | 1.03M | c->binkdsp.add_pixels8(dst, block, stride); |
942 | 1.03M | break; |
943 | 30.4k | case 4: |
944 | 30.4k | xoff = binkb_get_value(c, BINKB_SRC_X_OFF); |
945 | 30.4k | yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias; |
946 | 30.4k | ref = dst + xoff + yoff * stride; |
947 | 30.4k | if (ref < ref_start || ref > ref_end) { |
948 | 2.76k | av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n"); |
949 | 27.6k | } else if (ref + 8*stride < dst || ref >= dst + 8*stride) { |
950 | 25.2k | c->put_pixels_tab(dst, ref, stride, 8); |
951 | 25.2k | } else { |
952 | 2.37k | put_pixels8x8_overlapped(dst, ref, stride); |
953 | 2.37k | } |
954 | 30.4k | memset(dctblock, 0, sizeof(*dctblock) * 64); |
955 | 30.4k | dctblock[0] = binkb_get_value(c, BINKB_SRC_INTER_DC); |
956 | 30.4k | qp = binkb_get_value(c, BINKB_SRC_INTER_Q); |
957 | 30.4k | if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, qp)) < 0) |
958 | 909 | return quant_idx; |
959 | 29.5k | unquantize_dct_coeffs(dctblock, binkb_inter_quant[quant_idx], coef_count, coef_idx, bink_scan); |
960 | 29.5k | c->binkdsp.idct_add(dst, stride, dctblock); |
961 | 29.5k | break; |
962 | 27.5k | case 5: |
963 | 27.5k | v = binkb_get_value(c, BINKB_SRC_COLORS); |
964 | 27.5k | c->bdsp.fill_block_tab[1](dst, v, stride, 8); |
965 | 27.5k | break; |
966 | 28.9k | case 6: |
967 | 86.9k | for (i = 0; i < 2; i++) |
968 | 57.9k | col[i] = binkb_get_value(c, BINKB_SRC_COLORS); |
969 | 260k | for (i = 0; i < 8; i++) { |
970 | 231k | v = binkb_get_value(c, BINKB_SRC_PATTERN); |
971 | 2.08M | for (j = 0; j < 8; j++, v >>= 1) |
972 | 1.85M | dst[i*stride + j] = col[v & 1]; |
973 | 231k | } |
974 | 28.9k | break; |
975 | 30.1k | case 7: |
976 | 30.1k | xoff = binkb_get_value(c, BINKB_SRC_X_OFF); |
977 | 30.1k | yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias; |
978 | 30.1k | ref = dst + xoff + yoff * stride; |
979 | 30.1k | if (ref < ref_start || ref > ref_end) { |
980 | 5.01k | av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n"); |
981 | 25.1k | } else if (ref + 8*stride < dst || ref >= dst + 8*stride) { |
982 | 22.8k | c->put_pixels_tab(dst, ref, stride, 8); |
983 | 22.8k | } else { |
984 | 2.32k | put_pixels8x8_overlapped(dst, ref, stride); |
985 | 2.32k | } |
986 | 30.1k | break; |
987 | 1.40M | case 8: |
988 | 12.6M | for (i = 0; i < 8; i++) |
989 | 11.2M | memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8); |
990 | 1.40M | c->bundle[BINKB_SRC_COLORS].cur_ptr += 64; |
991 | 1.40M | break; |
992 | 5.98k | default: |
993 | 5.98k | av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk); |
994 | 5.98k | return AVERROR_INVALIDDATA; |
995 | 2.22G | } |
996 | 2.22G | } |
997 | 40.2M | } |
998 | 103k | if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary |
999 | 102k | skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F)); |
1000 | | |
1001 | 103k | return 0; |
1002 | 115k | } |
1003 | | |
1004 | | 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 | 3.38M | { |
1009 | 3.38M | int xoff = get_value(c, BINK_SRC_X_OFF); |
1010 | 3.38M | int yoff = get_value(c, BINK_SRC_Y_OFF); |
1011 | 3.38M | uint8_t *ref = prev + xoff + yoff * stride; |
1012 | 3.38M | if (ref < ref_start || ref > ref_end) { |
1013 | 2.00k | av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n", |
1014 | 2.00k | xoff, yoff); |
1015 | 2.00k | return AVERROR_INVALIDDATA; |
1016 | 2.00k | } |
1017 | 3.38M | c->put_pixels_tab(dst, ref, stride, 8); |
1018 | | |
1019 | 3.38M | return 0; |
1020 | 3.38M | } |
1021 | | |
1022 | | static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb, |
1023 | | int plane_idx, int is_chroma) |
1024 | 65.9k | { |
1025 | 65.9k | int blk, ret; |
1026 | 65.9k | int i, j, bx, by; |
1027 | 65.9k | uint8_t *dst, *prev, *ref_start, *ref_end; |
1028 | 65.9k | int v, col[2]; |
1029 | 65.9k | const uint8_t *scan; |
1030 | 65.9k | LOCAL_ALIGNED_32(int16_t, block, [64]); |
1031 | 65.9k | LOCAL_ALIGNED_16(uint8_t, ublock, [64]); |
1032 | 65.9k | LOCAL_ALIGNED_16(int32_t, dctblock, [64]); |
1033 | 65.9k | int coordmap[64], quant_idx, coef_count, coef_idx[64]; |
1034 | | |
1035 | 65.9k | const int stride = frame->linesize[plane_idx]; |
1036 | 65.9k | int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3; |
1037 | 65.9k | int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3; |
1038 | 65.9k | int width = c->avctx->width >> is_chroma; |
1039 | 65.9k | int height = c->avctx->height >> is_chroma; |
1040 | | |
1041 | 65.9k | if (c->version == 'k' && get_bits1(gb)) { |
1042 | 7.82k | int fill = get_bits(gb, 8); |
1043 | | |
1044 | 7.82k | dst = frame->data[plane_idx]; |
1045 | | |
1046 | 71.9M | for (i = 0; i < height; i++) |
1047 | 71.8M | memset(dst + i * stride, fill, width); |
1048 | 7.82k | goto end; |
1049 | 7.82k | } |
1050 | | |
1051 | 58.1k | init_lengths(c, FFMAX(width, 8), bw); |
1052 | 254k | for (i = 0; i < BINK_NB_SRC; i++) { |
1053 | 237k | ret = read_bundle(gb, c, i); |
1054 | 237k | if (ret < 0) |
1055 | 40.4k | return ret; |
1056 | 237k | } |
1057 | | |
1058 | 17.7k | ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx] |
1059 | 17.7k | : frame->data[plane_idx]; |
1060 | 17.7k | ref_end = ref_start |
1061 | 17.7k | + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8; |
1062 | | |
1063 | 1.15M | for (i = 0; i < 64; i++) |
1064 | 1.13M | coordmap[i] = (i & 7) + (i >> 3) * stride; |
1065 | | |
1066 | 4.23M | for (by = 0; by < bh; by++) { |
1067 | 4.22M | if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES])) < 0) |
1068 | 622 | return ret; |
1069 | 4.22M | if ((ret = read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES])) < 0) |
1070 | 428 | return ret; |
1071 | 4.22M | if ((ret = read_colors(gb, &c->bundle[BINK_SRC_COLORS], c)) < 0) |
1072 | 1.46k | return ret; |
1073 | 4.22M | if ((ret = read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN])) < 0) |
1074 | 987 | return ret; |
1075 | 4.22M | if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF])) < 0) |
1076 | 368 | return ret; |
1077 | 4.22M | if ((ret = read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF])) < 0) |
1078 | 256 | return ret; |
1079 | 4.22M | if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0)) < 0) |
1080 | 906 | return ret; |
1081 | 4.22M | if ((ret = read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1)) < 0) |
1082 | 422 | return ret; |
1083 | 4.22M | if ((ret = read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN])) < 0) |
1084 | 457 | return ret; |
1085 | | |
1086 | 4.22M | dst = frame->data[plane_idx] + 8*by*stride; |
1087 | 4.22M | prev = (c->last->data[plane_idx] ? c->last->data[plane_idx] |
1088 | 4.22M | : frame->data[plane_idx]) + 8*by*stride; |
1089 | 99.5M | for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) { |
1090 | 95.3M | 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 | 95.3M | if (((by & 1) || (bx & 1)) && blk == SCALED_BLOCK) { |
1093 | 727k | bx++; |
1094 | 727k | dst += 8; |
1095 | 727k | prev += 8; |
1096 | 727k | continue; |
1097 | 727k | } |
1098 | 94.6M | switch (blk) { |
1099 | 86.3M | case SKIP_BLOCK: |
1100 | 86.3M | c->put_pixels_tab(dst, prev, stride, 8); |
1101 | 86.3M | break; |
1102 | 164k | case SCALED_BLOCK: |
1103 | 164k | blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES); |
1104 | 164k | switch (blk) { |
1105 | 22.5k | case RUN_BLOCK: |
1106 | 22.5k | if (get_bits_left(gb) < 4) |
1107 | 298 | return AVERROR_INVALIDDATA; |
1108 | 22.2k | scan = bink_patterns[get_bits(gb, 4)]; |
1109 | 22.2k | i = 0; |
1110 | 1.25M | do { |
1111 | 1.25M | int run = get_value(c, BINK_SRC_RUN) + 1; |
1112 | | |
1113 | 1.25M | i += run; |
1114 | 1.25M | if (i > 64) { |
1115 | 198 | av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); |
1116 | 198 | return AVERROR_INVALIDDATA; |
1117 | 198 | } |
1118 | 1.25M | if (get_bits1(gb)) { |
1119 | 362k | v = get_value(c, BINK_SRC_COLORS); |
1120 | 755k | for (j = 0; j < run; j++) |
1121 | 393k | ublock[*scan++] = v; |
1122 | 892k | } else { |
1123 | 1.90M | for (j = 0; j < run; j++) |
1124 | 1.01M | ublock[*scan++] = get_value(c, BINK_SRC_COLORS); |
1125 | 892k | } |
1126 | 1.25M | } while (i < 63); |
1127 | 22.0k | if (i == 63) |
1128 | 20.1k | ublock[*scan++] = get_value(c, BINK_SRC_COLORS); |
1129 | 22.0k | break; |
1130 | 8.91k | case INTRA_BLOCK: |
1131 | 8.91k | memset(dctblock, 0, sizeof(*dctblock) * 64); |
1132 | 8.91k | dctblock[0] = get_value(c, BINK_SRC_INTRA_DC); |
1133 | 8.91k | if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0) |
1134 | 228 | return quant_idx; |
1135 | 8.68k | unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan); |
1136 | 8.68k | c->binkdsp.idct_put(ublock, 8, dctblock); |
1137 | 8.68k | break; |
1138 | 35.0k | case FILL_BLOCK: |
1139 | 35.0k | v = get_value(c, BINK_SRC_COLORS); |
1140 | 35.0k | c->bdsp.fill_block_tab[0](dst, v, stride, 16); |
1141 | 35.0k | break; |
1142 | 2.06k | case PATTERN_BLOCK: |
1143 | 6.19k | for (i = 0; i < 2; i++) |
1144 | 4.13k | col[i] = get_value(c, BINK_SRC_COLORS); |
1145 | 18.5k | for (j = 0; j < 8; j++) { |
1146 | 16.5k | v = get_value(c, BINK_SRC_PATTERN); |
1147 | 148k | for (i = 0; i < 8; i++, v >>= 1) |
1148 | 132k | ublock[i + j*8] = col[v & 1]; |
1149 | 16.5k | } |
1150 | 2.06k | break; |
1151 | 94.9k | case RAW_BLOCK: |
1152 | 854k | for (j = 0; j < 8; j++) |
1153 | 6.83M | for (i = 0; i < 8; i++) |
1154 | 6.07M | ublock[i + j*8] = get_value(c, BINK_SRC_COLORS); |
1155 | 94.9k | break; |
1156 | 727 | default: |
1157 | 727 | av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk); |
1158 | 727 | return AVERROR_INVALIDDATA; |
1159 | 164k | } |
1160 | 162k | if (blk != FILL_BLOCK) |
1161 | 127k | c->binkdsp.scale_block(ublock, dst, stride); |
1162 | 162k | bx++; |
1163 | 162k | dst += 8; |
1164 | 162k | prev += 8; |
1165 | 162k | break; |
1166 | 2.58M | case MOTION_BLOCK: |
1167 | 2.58M | ret = bink_put_pixels(c, dst, prev, stride, |
1168 | 2.58M | ref_start, ref_end); |
1169 | 2.58M | if (ret < 0) |
1170 | 430 | return ret; |
1171 | 2.58M | break; |
1172 | 2.58M | case RUN_BLOCK: |
1173 | 1.78M | scan = bink_patterns[get_bits(gb, 4)]; |
1174 | 1.78M | i = 0; |
1175 | 109M | do { |
1176 | 109M | int run = get_value(c, BINK_SRC_RUN) + 1; |
1177 | | |
1178 | 109M | i += run; |
1179 | 109M | if (i > 64) { |
1180 | 293 | av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); |
1181 | 293 | return AVERROR_INVALIDDATA; |
1182 | 293 | } |
1183 | 109M | if (get_bits1(gb)) { |
1184 | 3.52M | v = get_value(c, BINK_SRC_COLORS); |
1185 | 7.09M | for (j = 0; j < run; j++) |
1186 | 3.56M | dst[coordmap[*scan++]] = v; |
1187 | 105M | } else { |
1188 | 214M | for (j = 0; j < run; j++) |
1189 | 108M | dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS); |
1190 | 105M | } |
1191 | 109M | } while (i < 63); |
1192 | 1.78M | if (i == 63) |
1193 | 1.69M | dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS); |
1194 | 1.78M | break; |
1195 | 716k | case RESIDUE_BLOCK: |
1196 | 716k | ret = bink_put_pixels(c, dst, prev, stride, |
1197 | 716k | ref_start, ref_end); |
1198 | 716k | if (ret < 0) |
1199 | 1.14k | return ret; |
1200 | 715k | c->bdsp.clear_block(block); |
1201 | 715k | v = get_bits(gb, 7); |
1202 | 715k | read_residue(gb, block, v); |
1203 | 715k | c->binkdsp.add_pixels8(dst, block, stride); |
1204 | 715k | break; |
1205 | 444k | case INTRA_BLOCK: |
1206 | 444k | memset(dctblock, 0, sizeof(*dctblock) * 64); |
1207 | 444k | dctblock[0] = get_value(c, BINK_SRC_INTRA_DC); |
1208 | 444k | if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0) |
1209 | 1.38k | return quant_idx; |
1210 | 443k | unquantize_dct_coeffs(dctblock, bink_intra_quant[quant_idx], coef_count, coef_idx, bink_scan); |
1211 | 443k | c->binkdsp.idct_put(dst, stride, dctblock); |
1212 | 443k | break; |
1213 | 435k | case FILL_BLOCK: |
1214 | 435k | v = get_value(c, BINK_SRC_COLORS); |
1215 | 435k | c->bdsp.fill_block_tab[1](dst, v, stride, 8); |
1216 | 435k | break; |
1217 | 85.5k | case INTER_BLOCK: |
1218 | 85.5k | ret = bink_put_pixels(c, dst, prev, stride, |
1219 | 85.5k | ref_start, ref_end); |
1220 | 85.5k | if (ret < 0) |
1221 | 435 | return ret; |
1222 | 85.1k | memset(dctblock, 0, sizeof(*dctblock) * 64); |
1223 | 85.1k | dctblock[0] = get_value(c, BINK_SRC_INTER_DC); |
1224 | 85.1k | if ((quant_idx = read_dct_coeffs(c, gb, dctblock, bink_scan, &coef_count, coef_idx, -1)) < 0) |
1225 | 650 | return quant_idx; |
1226 | 84.4k | unquantize_dct_coeffs(dctblock, bink_inter_quant[quant_idx], coef_count, coef_idx, bink_scan); |
1227 | 84.4k | c->binkdsp.idct_add(dst, stride, dctblock); |
1228 | 84.4k | break; |
1229 | 1.70M | case PATTERN_BLOCK: |
1230 | 5.11M | for (i = 0; i < 2; i++) |
1231 | 3.41M | col[i] = get_value(c, BINK_SRC_COLORS); |
1232 | 15.3M | for (i = 0; i < 8; i++) { |
1233 | 13.6M | v = get_value(c, BINK_SRC_PATTERN); |
1234 | 122M | for (j = 0; j < 8; j++, v >>= 1) |
1235 | 109M | dst[i*stride + j] = col[v & 1]; |
1236 | 13.6M | } |
1237 | 1.70M | break; |
1238 | 287k | case RAW_BLOCK: |
1239 | 2.58M | for (i = 0; i < 8; i++) |
1240 | 2.30M | memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8); |
1241 | 287k | c->bundle[BINK_SRC_COLORS].cur_ptr += 64; |
1242 | 287k | break; |
1243 | 2.29k | default: |
1244 | 2.29k | av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk); |
1245 | 2.29k | return AVERROR_INVALIDDATA; |
1246 | 94.6M | } |
1247 | 94.6M | } |
1248 | 4.22M | } |
1249 | | |
1250 | 11.5k | end: |
1251 | 11.5k | if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary |
1252 | 10.7k | skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F)); |
1253 | | |
1254 | 11.5k | return 0; |
1255 | 17.7k | } |
1256 | | |
1257 | | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, |
1258 | | int *got_frame, AVPacket *pkt) |
1259 | 173k | { |
1260 | 173k | BinkContext * const c = avctx->priv_data; |
1261 | 173k | GetBitContext gb; |
1262 | 173k | int plane, plane_idx, ret; |
1263 | 173k | int bits_count = pkt->size << 3; |
1264 | | |
1265 | 173k | if (c->version > 'b') { |
1266 | 24.1k | if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) |
1267 | 318 | return ret; |
1268 | 149k | } else { |
1269 | 149k | if ((ret = ff_reget_buffer(avctx, c->last, 0)) < 0) |
1270 | 231 | return ret; |
1271 | 149k | if ((ret = av_frame_ref(frame, c->last)) < 0) |
1272 | 0 | return ret; |
1273 | 149k | } |
1274 | | |
1275 | 173k | init_get_bits(&gb, pkt->data, bits_count); |
1276 | 173k | if (c->has_alpha) { |
1277 | 47.4k | if (c->version >= 'i') |
1278 | 9.92k | skip_bits_long(&gb, 32); |
1279 | 47.4k | if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0) |
1280 | 43.5k | return ret; |
1281 | 47.4k | } |
1282 | 129k | if (c->version >= 'i') |
1283 | 14.3k | skip_bits_long(&gb, 32); |
1284 | | |
1285 | 129k | c->frame_num++; |
1286 | | |
1287 | 135k | for (plane = 0; plane < 3; plane++) { |
1288 | 134k | plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3); |
1289 | | |
1290 | 134k | if (c->version > 'b') { |
1291 | 18.4k | if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0) |
1292 | 10.8k | return ret; |
1293 | 115k | } else { |
1294 | 115k | if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx, |
1295 | 115k | c->frame_num == 1, !!plane)) < 0) |
1296 | 12.4k | return ret; |
1297 | 115k | } |
1298 | 110k | if (get_bits_count(&gb) >= bits_count) |
1299 | 105k | break; |
1300 | 110k | } |
1301 | | |
1302 | 106k | if (c->version > 'b') { |
1303 | 3.61k | if ((ret = av_frame_replace(c->last, frame)) < 0) |
1304 | 0 | return ret; |
1305 | 3.61k | } |
1306 | | |
1307 | 106k | *got_frame = 1; |
1308 | | |
1309 | | /* always report that the buffer was completely consumed */ |
1310 | 106k | return pkt->size; |
1311 | 106k | } |
1312 | | |
1313 | | static av_cold void bink_init_vlcs(void) |
1314 | 1 | { |
1315 | 17 | for (int i = 0, offset = 0; i < 16; i++) { |
1316 | 16 | static VLCElem table[976]; |
1317 | 16 | const int maxbits = bink_tree_lens[i][15]; |
1318 | 16 | bink_trees[i].table = table + offset; |
1319 | 16 | bink_trees[i].table_allocated = 1 << maxbits; |
1320 | 16 | offset += bink_trees[i].table_allocated; |
1321 | 16 | vlc_init(&bink_trees[i], maxbits, 16, |
1322 | 16 | bink_tree_lens[i], 1, 1, |
1323 | 16 | bink_tree_bits[i], 1, 1, VLC_INIT_USE_STATIC | VLC_INIT_LE); |
1324 | 16 | } |
1325 | 1 | } |
1326 | | |
1327 | | /** |
1328 | | * Calculate quantization tables for version b |
1329 | | */ |
1330 | | static av_cold void binkb_calc_quant(void) |
1331 | 1 | { |
1332 | 1 | uint8_t inv_bink_scan[64]; |
1333 | 1 | static const int s[64]={ |
1334 | 1 | 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703, |
1335 | 1 | 1489322693,2065749918,1945893874,1751258219,1489322693,1170153332, 806015634, 410903207, |
1336 | 1 | 1402911301,1945893874,1832991949,1649649171,1402911301,1102260336, 759250125, 387062357, |
1337 | 1 | 1262586814,1751258219,1649649171,1484645031,1262586814, 992008094, 683307060, 348346918, |
1338 | 1 | 1073741824,1489322693,1402911301,1262586814,1073741824, 843633538, 581104888, 296244703, |
1339 | 1 | 843633538,1170153332,1102260336, 992008094, 843633538, 662838617, 456571181, 232757969, |
1340 | 1 | 581104888, 806015634, 759250125, 683307060, 581104888, 456571181, 314491699, 160326478, |
1341 | 1 | 296244703, 410903207, 387062357, 348346918, 296244703, 232757969, 160326478, 81733730, |
1342 | 1 | }; |
1343 | 1 | int i, j; |
1344 | 2.04k | #define C (1LL<<30) |
1345 | 65 | for (i = 0; i < 64; i++) |
1346 | 64 | inv_bink_scan[bink_scan[i]] = i; |
1347 | | |
1348 | 17 | for (j = 0; j < 16; j++) { |
1349 | 1.04k | for (i = 0; i < 64; i++) { |
1350 | 1.02k | int k = inv_bink_scan[i]; |
1351 | 1.02k | binkb_intra_quant[j][k] = binkb_intra_seed[i] * (int64_t)s[i] * |
1352 | 1.02k | binkb_num[j]/(binkb_den[j] * (C>>12)); |
1353 | 1.02k | binkb_inter_quant[j][k] = binkb_inter_seed[i] * (int64_t)s[i] * |
1354 | 1.02k | binkb_num[j]/(binkb_den[j] * (C>>12)); |
1355 | 1.02k | } |
1356 | 16 | } |
1357 | 1 | } |
1358 | | |
1359 | | static av_cold int decode_init(AVCodecContext *avctx) |
1360 | 1.83k | { |
1361 | 1.83k | static AVOnce init_static_once = AV_ONCE_INIT; |
1362 | 1.83k | BinkContext * const c = avctx->priv_data; |
1363 | 1.83k | HpelDSPContext hdsp; |
1364 | 1.83k | int ret; |
1365 | 1.83k | int flags; |
1366 | | |
1367 | 1.83k | c->version = avctx->codec_tag >> 24; |
1368 | 1.83k | if (avctx->extradata_size < 4) { |
1369 | 171 | av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n"); |
1370 | 171 | return AVERROR_INVALIDDATA; |
1371 | 171 | } |
1372 | 1.66k | flags = AV_RL32(avctx->extradata); |
1373 | 1.66k | c->has_alpha = flags & BINK_FLAG_ALPHA; |
1374 | 1.66k | c->swap_planes = c->version >= 'h'; |
1375 | 1.66k | c->avctx = avctx; |
1376 | | |
1377 | 1.66k | if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0) |
1378 | 8 | return ret; |
1379 | | |
1380 | 1.65k | c->last = av_frame_alloc(); |
1381 | 1.65k | if (!c->last) |
1382 | 0 | return AVERROR(ENOMEM); |
1383 | | |
1384 | 1.65k | avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P; |
1385 | 1.65k | avctx->color_range = c->version == 'k' ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; |
1386 | | |
1387 | 1.65k | ff_blockdsp_init(&c->bdsp); |
1388 | 1.65k | ff_hpeldsp_init(&hdsp, avctx->flags); |
1389 | 1.65k | c->put_pixels_tab = hdsp.put_pixels_tab[1][0]; |
1390 | 1.65k | ff_binkdsp_init(&c->binkdsp); |
1391 | | |
1392 | 1.65k | if ((ret = init_bundles(c)) < 0) |
1393 | 0 | return ret; |
1394 | | |
1395 | 1.65k | if (c->version == 'b') { |
1396 | 4 | static AVOnce binkb_init_once = AV_ONCE_INIT; |
1397 | 4 | ff_thread_once(&binkb_init_once, binkb_calc_quant); |
1398 | 4 | } |
1399 | 1.65k | ff_thread_once(&init_static_once, bink_init_vlcs); |
1400 | | |
1401 | 1.65k | return 0; |
1402 | 1.65k | } |
1403 | | |
1404 | | static av_cold int decode_end(AVCodecContext *avctx) |
1405 | 1.83k | { |
1406 | 1.83k | BinkContext * const c = avctx->priv_data; |
1407 | | |
1408 | 1.83k | av_frame_free(&c->last); |
1409 | | |
1410 | 1.83k | free_bundles(c); |
1411 | 1.83k | return 0; |
1412 | 1.83k | } |
1413 | | |
1414 | | static av_cold void flush(AVCodecContext *avctx) |
1415 | 57.4k | { |
1416 | 57.4k | BinkContext * const c = avctx->priv_data; |
1417 | | |
1418 | 57.4k | c->frame_num = 0; |
1419 | 57.4k | } |
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 | | }; |