comparison bink.c @ 11231:0fc1cdd984b7 libavcodec

Bink video decoder
author kostya
date Sun, 21 Feb 2010 13:28:46 +0000
parents
children eb773ca000d9
comparison
equal deleted inserted replaced
11230:9f25ae41c807 11231:0fc1cdd984b7
1 /*
2 * Bink video decoder
3 * Copyright (c) 2009 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "avcodec.h"
23 #include "dsputil.h"
24 #include "binkdata.h"
25 #include "mathops.h"
26
27 #define ALT_BITSTREAM_READER_LE
28 #include "get_bits.h"
29
30 static VLC bink_trees[16];
31
32 /**
33 * IDs for different data types used in Bink video codec
34 */
35 enum Sources {
36 BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
37 BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
38 BINK_SRC_COLORS, ///< pixel values used for different block types
39 BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
40 BINK_SRC_X_OFF, ///< X components of motion value
41 BINK_SRC_Y_OFF, ///< Y components of motion value
42 BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
43 BINK_SRC_INTER_DC, ///< DC values for intrablocks with DCT
44 BINK_SRC_RUN, ///< run lengths for special fill block
45
46 BINK_NB_SRC
47 };
48
49 /**
50 * data needed to decode 4-bit Huffman-coded value
51 */
52 typedef struct Tree {
53 int vlc_num; ///< tree number (in bink_trees[])
54 uint8_t syms[16]; ///< leaf value to symbol mapping
55 } Tree;
56
57 #define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
58 bink_trees[(tree).vlc_num].bits, 1)]
59
60 /**
61 * data structure used for decoding single Bink data type
62 */
63 typedef struct Bundle {
64 int len; ///< length of number of entries to decode (in bits)
65 Tree tree; ///< Huffman tree-related data
66 uint8_t *data; ///< buffer for decoded symbols
67 uint8_t *data_end; ///< buffer end
68 uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
69 uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
70 } Bundle;
71
72 /*
73 * Decoder context
74 */
75 typedef struct BinkContext {
76 AVCodecContext *avctx;
77 DSPContext dsp;
78 AVFrame pic, last;
79 int version; ///< internal Bink file version
80 int swap_planes;
81 ScanTable scantable; ///< permutated scantable for DCT coeffs decoding
82
83 Bundle bundle[BINK_NB_SRC]; ///< bundles for decoding all data types
84 Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
85 int col_lastval; ///< value of last decoded high nibble in "colours" data type
86 } BinkContext;
87
88 /**
89 * Bink video block types
90 */
91 enum BlockTypes {
92 SKIP_BLOCK = 0, ///< skipped block
93 SCALED_BLOCK, ///< block has size 16x16
94 MOTION_BLOCK, ///< block is copied from previous frame with some offset
95 RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
96 RESIDUE_BLOCK, ///< motion block with some difference added
97 INTRA_BLOCK, ///< intra DCT block
98 FILL_BLOCK, ///< block is filled with single colour
99 INTER_BLOCK, ///< motion block with DCT applied to the difference
100 PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
101 RAW_BLOCK, ///< uncoded 8x8 block
102 };
103
104 /**
105 * Initializes length length in all bundles.
106 *
107 * @param c decoder context
108 * @param width plane width
109 * @param bw plane width in 8x8 blocks
110 */
111 static void init_lengths(BinkContext *c, int width, int bw)
112 {
113 c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
114
115 c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
116
117 c->bundle[BINK_SRC_COLORS].len = av_log2((width >> 3)*64 + 511) + 1;
118
119 c->bundle[BINK_SRC_INTRA_DC].len =
120 c->bundle[BINK_SRC_INTER_DC].len =
121 c->bundle[BINK_SRC_X_OFF].len =
122 c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
123
124 c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
125
126 c->bundle[BINK_SRC_RUN].len = av_log2((width >> 3)*48 + 511) + 1;
127 }
128
129 /**
130 * Allocates memory for bundles.
131 *
132 * @param c decoder context
133 */
134 static av_cold void init_bundles(BinkContext *c)
135 {
136 int bw, bh, blocks;
137 int i;
138
139 bw = (c->avctx->width + 7) >> 3;
140 bh = (c->avctx->height + 7) >> 3;
141 blocks = bw * bh;
142
143 for (i = 0; i < BINK_NB_SRC; i++) {
144 c->bundle[i].data = av_malloc(blocks * 64);
145 c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
146 }
147 }
148
149 /**
150 * Frees memory used by bundles.
151 *
152 * @param c decoder context
153 */
154 static av_cold void free_bundles(BinkContext *c)
155 {
156 int i;
157 for (i = 0; i < BINK_NB_SRC; i++)
158 av_freep(&c->bundle[i].data);
159 }
160
161 /**
162 * Merges two consequent lists of equal size depending on bits read.
163 *
164 * @param gb context for reading bits
165 * @param dst buffer where merged list will be written to
166 * @param src pointer to the head of the first list (the second lists starts at src+size)
167 * @param size input lists size
168 */
169 static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
170 {
171 uint8_t *src2 = src + size;
172 int size2 = size;
173
174 do {
175 if (!get_bits1(gb)) {
176 *dst++ = *src++;
177 size--;
178 } else {
179 *dst++ = *src2++;
180 size2--;
181 }
182 } while (size && size2);
183
184 while (size--)
185 *dst++ = *src++;
186 while (size2--)
187 *dst++ = *src2++;
188 }
189
190 /**
191 * Reads information about Huffman tree used to decode data.
192 *
193 * @param gb context for reading bits
194 * @param tree pointer for storing tree data
195 */
196 static void read_tree(GetBitContext *gb, Tree *tree)
197 {
198 uint8_t tmp1[16], tmp2[16], *in = tmp1, *out = tmp2;
199 int i, t, len;
200
201 tree->vlc_num = get_bits(gb, 4);
202 if (!tree->vlc_num) {
203 for (i = 0; i < 16; i++)
204 tree->syms[i] = i;
205 return;
206 }
207 if (get_bits1(gb)) {
208 len = get_bits(gb, 3);
209 memset(tmp1, 0, sizeof(tmp1));
210 for (i = 0; i <= len; i++) {
211 tree->syms[i] = get_bits(gb, 4);
212 tmp1[tree->syms[i]] = 1;
213 }
214 for (i = 0; i < 16; i++)
215 if (!tmp1[i])
216 tree->syms[++len] = i;
217 } else {
218 len = get_bits(gb, 2);
219 for (i = 0; i < 16; i++)
220 in[i] = i;
221 for (i = 0; i <= len; i++) {
222 int size = 1 << i;
223 for (t = 0; t < 16; t += size << 1)
224 merge(gb, out + t, in + t, size);
225 FFSWAP(uint8_t*, in, out);
226 }
227 memcpy(tree->syms, in, 16);
228 }
229 }
230
231 /**
232 * Prepares bundle for decoding data.
233 *
234 * @param gb context for reading bits
235 * @param c decoder context
236 * @param bundle_num number of the bundle to initialize
237 */
238 static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
239 {
240 int i;
241
242 if (bundle_num == BINK_SRC_COLORS) {
243 for (i = 0; i < 16; i++)
244 read_tree(gb, &c->col_high[i]);
245 c->col_lastval = 0;
246 }
247 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
248 read_tree(gb, &c->bundle[bundle_num].tree);
249 c->bundle[bundle_num].cur_dec =
250 c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
251 }
252
253 /**
254 * common check before starting decoding bundle data
255 *
256 * @param gb context for reading bits
257 * @param b bundle
258 * @param t variable where number of elements to decode will be stored
259 */
260 #define CHECK_READ_VAL(gb, b, t) \
261 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
262 return 0; \
263 t = get_bits(gb, b->len); \
264 if (!t) { \
265 b->cur_dec = NULL; \
266 return 0; \
267 } \
268
269 static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
270 {
271 int t, v;
272 const uint8_t *dec_end;
273
274 CHECK_READ_VAL(gb, b, t);
275 dec_end = b->cur_dec + t;
276 if (dec_end > b->data_end) {
277 av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
278 return -1;
279 }
280 if (get_bits1(gb)) {
281 v = get_bits(gb, 4);
282 memset(b->cur_dec, v, t);
283 b->cur_dec += t;
284 } else {
285 while (b->cur_dec < dec_end)
286 *b->cur_dec++ = GET_HUFF(gb, b->tree);
287 }
288 return 0;
289 }
290
291 static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
292 {
293 int t, sign, v;
294 const uint8_t *dec_end;
295
296 CHECK_READ_VAL(gb, b, t);
297 dec_end = b->cur_dec + t;
298 if (dec_end > b->data_end) {
299 av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
300 return -1;
301 }
302 if (get_bits1(gb)) {
303 v = get_bits(gb, 4);
304 if (v) {
305 sign = -get_bits1(gb);
306 v = (v ^ sign) - sign;
307 }
308 memset(b->cur_dec, v, t);
309 b->cur_dec += t;
310 } else {
311 do {
312 v = GET_HUFF(gb, b->tree);
313 if (v) {
314 sign = -get_bits1(gb);
315 v = (v ^ sign) - sign;
316 }
317 *b->cur_dec++ = v;
318 } while (b->cur_dec < dec_end);
319 }
320 return 0;
321 }
322
323 const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
324
325 static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
326 {
327 int t, v;
328 int last = 0;
329 const uint8_t *dec_end;
330
331 CHECK_READ_VAL(gb, b, t);
332 dec_end = b->cur_dec + t;
333 if (dec_end > b->data_end) {
334 av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
335 return -1;
336 }
337 if (get_bits1(gb)) {
338 v = get_bits(gb, 4);
339 memset(b->cur_dec, v, t);
340 b->cur_dec += t;
341 } else {
342 do {
343 v = GET_HUFF(gb, b->tree);
344 if (v < 12) {
345 last = v;
346 *b->cur_dec++ = v;
347 } else {
348 int run = bink_rlelens[v - 12];
349
350 memset(b->cur_dec, last, run);
351 b->cur_dec += run;
352 }
353 } while (b->cur_dec < dec_end);
354 }
355 return 0;
356 }
357
358 static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
359 {
360 int t, v;
361 const uint8_t *dec_end;
362
363 CHECK_READ_VAL(gb, b, t);
364 dec_end = b->cur_dec + t;
365 if (dec_end > b->data_end) {
366 av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
367 return -1;
368 }
369 while (b->cur_dec < dec_end) {
370 v = GET_HUFF(gb, b->tree);
371 v |= GET_HUFF(gb, b->tree) << 4;
372 *b->cur_dec++ = v;
373 }
374
375 return 0;
376 }
377
378 static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
379 {
380 int t, sign, v;
381 const uint8_t *dec_end;
382
383 CHECK_READ_VAL(gb, b, t);
384 dec_end = b->cur_dec + t;
385 if (dec_end > b->data_end) {
386 av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
387 return -1;
388 }
389 if (get_bits1(gb)) {
390 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
391 v = GET_HUFF(gb, b->tree);
392 v = (c->col_lastval << 4) | v;
393 if (c->version < 'i') {
394 sign = ((int8_t) v) >> 7;
395 v = ((v & 0x7F) ^ sign) - sign;
396 v += 0x80;
397 }
398 memset(b->cur_dec, v, t);
399 b->cur_dec += t;
400 } else {
401 while (b->cur_dec < dec_end) {
402 c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
403 v = GET_HUFF(gb, b->tree);
404 v = (c->col_lastval << 4) | v;
405 if (c->version < 'i') {
406 sign = ((int8_t) v) >> 7;
407 v = ((v & 0x7F) ^ sign) - sign;
408 v += 0x80;
409 }
410 *b->cur_dec++ = v;
411 }
412 }
413 return 0;
414 }
415
416 /** number of bits used to store first DC value in bundle */
417 #define DC_START_BITS 11
418
419 static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
420 int start_bits, int has_sign)
421 {
422 int i, j, len, len2, bsize, sign, v, v2;
423 int16_t *dst = (int16_t*)b->cur_dec;
424
425 CHECK_READ_VAL(gb, b, len);
426 v = get_bits(gb, start_bits - has_sign);
427 if (v && has_sign) {
428 sign = -get_bits1(gb);
429 v = (v ^ sign) - sign;
430 }
431 *dst++ = v;
432 len--;
433 for (i = 0; i < len; i += 8) {
434 len2 = FFMIN(len - i, 8);
435 bsize = get_bits(gb, 4);
436 if (bsize) {
437 for (j = 0; j < len2; j++) {
438 v2 = get_bits(gb, bsize);
439 if (v2) {
440 sign = -get_bits1(gb);
441 v2 = (v2 ^ sign) - sign;
442 }
443 v += v2;
444 *dst++ = v;
445 if (v < -32768 || v > 32767) {
446 av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
447 return -1;
448 }
449 }
450 } else {
451 for (j = 0; j < len2; j++)
452 *dst++ = v;
453 }
454 }
455
456 b->cur_dec = (uint8_t*)dst;
457 return 0;
458 }
459
460 /**
461 * Retrieves next value from bundle.
462 *
463 * @param c decoder context
464 * @param bundle bundle number
465 */
466 static inline int get_value(BinkContext *c, int bundle)
467 {
468 int16_t ret;
469
470 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
471 return *c->bundle[bundle].cur_ptr++;
472 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
473 return (int8_t)*c->bundle[bundle].cur_ptr++;
474 ret = *(int16_t*)c->bundle[bundle].cur_ptr;
475 c->bundle[bundle].cur_ptr += 2;
476 return ret;
477 }
478
479 /**
480 * Reads 8x8 block of DCT coefficients.
481 *
482 * @param gb context for reading bits
483 * @param block place for storing coefficients
484 * @param scan scan order table
485 * @param is_intra tells what set of quantizer matrices to use
486 * @return 0 for success, negative value in other cases
487 */
488 static int read_dct_coeffs(GetBitContext *gb, DCTELEM block[64], const uint8_t *scan,
489 int is_intra)
490 {
491 int coef_list[128];
492 int mode_list[128];
493 int i, t, mask, bits, ccoef, mode, sign;
494 int list_start = 64, list_end = 64, list_pos;
495 int coef_count = 0;
496 int coef_idx[64];
497 int quant_idx;
498 const uint32_t *quant;
499
500 coef_list[list_end] = 4; mode_list[list_end++] = 0;
501 coef_list[list_end] = 24; mode_list[list_end++] = 0;
502 coef_list[list_end] = 44; mode_list[list_end++] = 0;
503 coef_list[list_end] = 1; mode_list[list_end++] = 3;
504 coef_list[list_end] = 2; mode_list[list_end++] = 3;
505 coef_list[list_end] = 3; mode_list[list_end++] = 3;
506
507 bits = get_bits(gb, 4) - 1;
508 for (mask = 1 << bits; bits >= 0; mask >>= 1, bits--) {
509 list_pos = list_start;
510 while (list_pos < list_end) {
511 if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
512 list_pos++;
513 continue;
514 }
515 ccoef = coef_list[list_pos];
516 mode = mode_list[list_pos];
517 switch (mode) {
518 case 0:
519 coef_list[list_pos] = ccoef + 4;
520 mode_list[list_pos] = 1;
521 case 2:
522 if (mode == 2) {
523 coef_list[list_pos] = 0;
524 mode_list[list_pos++] = 0;
525 }
526 for (i = 0; i < 4; i++, ccoef++) {
527 if (get_bits1(gb)) {
528 coef_list[--list_start] = ccoef;
529 mode_list[ list_start] = 3;
530 } else {
531 int t;
532 if (!bits) {
533 t = 1 - (get_bits1(gb) << 1);
534 } else {
535 t = get_bits(gb, bits) | mask;
536 sign = -get_bits1(gb);
537 t = (t ^ sign) - sign;
538 }
539 block[scan[ccoef]] = t;
540 coef_idx[coef_count++] = ccoef;
541 }
542 }
543 break;
544 case 1:
545 mode_list[list_pos] = 2;
546 for (i = 0; i < 3; i++) {
547 ccoef += 4;
548 coef_list[list_end] = ccoef;
549 mode_list[list_end++] = 2;
550 }
551 break;
552 case 3:
553 if (!bits) {
554 t = 1 - (get_bits1(gb) << 1);
555 } else {
556 t = get_bits(gb, bits) | mask;
557 sign = -get_bits1(gb);
558 t = (t ^ sign) - sign;
559 }
560 block[scan[ccoef]] = t;
561 coef_idx[coef_count++] = ccoef;
562 coef_list[list_pos] = 0;
563 mode_list[list_pos++] = 0;
564 break;
565 }
566 }
567 }
568
569 quant_idx = get_bits(gb, 4);
570 quant = is_intra ? bink_intra_quant[quant_idx]
571 : bink_inter_quant[quant_idx];
572 block[0] = (block[0] * quant[0]) >> 11;
573 for (i = 0; i < coef_count; i++) {
574 int idx = coef_idx[i];
575 block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
576 }
577
578 return 0;
579 }
580
581 /**
582 * Reads 8x8 block with residue after motion compensation.
583 *
584 * @param gb context for reading bits
585 * @param block place to store read data
586 * @param masks_count number of masks to decode
587 * @return 0 on success, negative value in other cases
588 */
589 static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
590 {
591 int coef_list[128];
592 int mode_list[128];
593 int i, sign, mask, ccoef, mode;
594 int list_start = 64, list_end = 64, list_pos;
595 int nz_coeff[64];
596 int nz_coeff_count = 0;
597
598 coef_list[list_end] = 4; mode_list[list_end++] = 0;
599 coef_list[list_end] = 24; mode_list[list_end++] = 0;
600 coef_list[list_end] = 44; mode_list[list_end++] = 0;
601 coef_list[list_end] = 0; mode_list[list_end++] = 2;
602
603 for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
604 for (i = 0; i < nz_coeff_count; i++) {
605 if (!get_bits1(gb))
606 continue;
607 if (block[nz_coeff[i]] < 0)
608 block[nz_coeff[i]] -= mask;
609 else
610 block[nz_coeff[i]] += mask;
611 masks_count--;
612 if (masks_count < 0)
613 return 0;
614 }
615 list_pos = list_start;
616 while (list_pos < list_end) {
617 if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
618 list_pos++;
619 continue;
620 }
621 ccoef = coef_list[list_pos];
622 mode = mode_list[list_pos];
623 switch (mode) {
624 case 0:
625 coef_list[list_pos] = ccoef + 4;
626 mode_list[list_pos] = 1;
627 case 2:
628 if (mode == 2) {
629 coef_list[list_pos] = 0;
630 mode_list[list_pos++] = 0;
631 }
632 for (i = 0; i < 4; i++, ccoef++) {
633 if (get_bits1(gb)) {
634 coef_list[--list_start] = ccoef;
635 mode_list[ list_start] = 3;
636 } else {
637 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
638 sign = -get_bits1(gb);
639 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
640 masks_count--;
641 if (masks_count < 0)
642 return 0;
643 }
644 }
645 break;
646 case 1:
647 mode_list[list_pos] = 2;
648 for (i = 0; i < 3; i++) {
649 ccoef += 4;
650 coef_list[list_end] = ccoef;
651 mode_list[list_end++] = 2;
652 }
653 break;
654 case 3:
655 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
656 sign = -get_bits1(gb);
657 block[bink_scan[ccoef]] = (mask ^ sign) - sign;
658 coef_list[list_pos] = 0;
659 mode_list[list_pos++] = 0;
660 masks_count--;
661 if (masks_count < 0)
662 return 0;
663 break;
664 }
665 }
666 }
667
668 return 0;
669 }
670
671 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
672 {
673 BinkContext * const c = avctx->priv_data;
674 GetBitContext gb;
675 int blk;
676 int i, j, plane, plane_idx, bx, by;
677 uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
678 int v, col[2];
679 const uint8_t *scan;
680 int xoff, yoff;
681 DECLARE_ALIGNED_16(DCTELEM, block[64]);
682 DECLARE_ALIGNED_16(uint8_t, ublock[64]);
683 int coordmap[64];
684
685 if(c->pic.data[0])
686 avctx->release_buffer(avctx, &c->pic);
687
688 if(avctx->get_buffer(avctx, &c->pic) < 0){
689 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
690 return -1;
691 }
692
693 init_get_bits(&gb, pkt->data, pkt->size*8);
694 if (c->version >= 'i')
695 skip_bits_long(&gb, 32);
696
697 for (plane = 0; plane < 3; plane++) {
698 const int stride = c->pic.linesize[plane];
699 int bw = plane ? (avctx->width + 15) >> 4 : (avctx->width + 7) >> 3;
700 int bh = plane ? (avctx->height + 15) >> 4 : (avctx->height + 7) >> 3;
701
702 init_lengths(c, avctx->width >> !!plane, bw);
703 for (i = 0; i < BINK_NB_SRC; i++)
704 read_bundle(&gb, c, i);
705
706 plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
707 ref_start = c->last.data[plane_idx];
708 ref_end = c->last.data[plane_idx]
709 + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
710
711 for (i = 0; i < 64; i++)
712 coordmap[i] = (i & 7) + (i >> 3) * stride;
713
714 for (by = 0; by < bh; by++) {
715 if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
716 return -1;
717 if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
718 return -1;
719 if (read_colors(&gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
720 return -1;
721 if (read_patterns(avctx, &gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
722 return -1;
723 if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
724 return -1;
725 if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
726 return -1;
727 if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
728 return -1;
729 if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
730 return -1;
731 if (read_runs(avctx, &gb, &c->bundle[BINK_SRC_RUN]) < 0)
732 return -1;
733
734 if (by == bh)
735 break;
736 dst = c->pic.data[plane_idx] + 8*by*stride;
737 prev = c->last.data[plane_idx] + 8*by*stride;
738 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
739 blk = get_value(c, BINK_SRC_BLOCK_TYPES);
740 // 16x16 block type on odd line means part of the already decoded block, so skip it
741 if ((by & 1) && blk == SCALED_BLOCK) {
742 bx++;
743 dst += 8;
744 prev += 8;
745 continue;
746 }
747 switch (blk) {
748 case SKIP_BLOCK:
749 c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
750 break;
751 case SCALED_BLOCK:
752 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
753 switch (blk) {
754 case RUN_BLOCK:
755 scan = bink_patterns[get_bits(&gb, 4)];
756 i = 0;
757 do {
758 int run = get_value(c, BINK_SRC_RUN) + 1;
759
760 i += run;
761 if (i > 64) {
762 av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\n");
763 return -1;
764 }
765 if (get_bits1(&gb)) {
766 v = get_value(c, BINK_SRC_COLORS);
767 for (j = 0; j < run; j++)
768 ublock[*scan++] = v;
769 } else {
770 for (j = 0; j < run; j++)
771 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
772 }
773 } while (i < 63);
774 if (i == 63)
775 ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
776 break;
777 case INTRA_BLOCK:
778 c->dsp.clear_block(block);
779 block[0] = get_value(c, BINK_SRC_INTRA_DC);
780 read_dct_coeffs(&gb, block, c->scantable.permutated, 1);
781 c->dsp.idct(block);
782 c->dsp.put_pixels_nonclamped(block, ublock, 8);
783 break;
784 case FILL_BLOCK:
785 v = get_value(c, BINK_SRC_COLORS);
786 c->dsp.fill_block_tab[0](dst, v, stride, 16);
787 break;
788 case PATTERN_BLOCK:
789 for (i = 0; i < 2; i++)
790 col[i] = get_value(c, BINK_SRC_COLORS);
791 for (j = 0; j < 8; j++) {
792 v = get_value(c, BINK_SRC_PATTERN);
793 for (i = 0; i < 8; i++, v >>= 1)
794 ublock[i + j*8] = col[v & 1];
795 }
796 break;
797 case RAW_BLOCK:
798 for (j = 0; j < 8; j++)
799 for (i = 0; i < 8; i++)
800 ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
801 break;
802 default:
803 av_log(avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
804 return -1;
805 }
806 if (blk != FILL_BLOCK)
807 c->dsp.scale_block(ublock, dst, stride);
808 bx++;
809 dst += 8;
810 prev += 8;
811 break;
812 case MOTION_BLOCK:
813 xoff = get_value(c, BINK_SRC_X_OFF);
814 yoff = get_value(c, BINK_SRC_Y_OFF);
815 ref = prev + xoff + yoff * stride;
816 if (ref < ref_start || ref > ref_end) {
817 av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
818 bx*8 + xoff, by*8 + yoff);
819 return -1;
820 }
821 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
822 break;
823 case RUN_BLOCK:
824 scan = bink_patterns[get_bits(&gb, 4)];
825 i = 0;
826 do {
827 int run = get_value(c, BINK_SRC_RUN) + 1;
828
829 i += run;
830 if (i > 64) {
831 av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\n");
832 return -1;
833 }
834 if (get_bits1(&gb)) {
835 v = get_value(c, BINK_SRC_COLORS);
836 for (j = 0; j < run; j++)
837 dst[coordmap[*scan++]] = v;
838 } else {
839 for (j = 0; j < run; j++)
840 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
841 }
842 } while (i < 63);
843 if (i == 63)
844 dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
845 break;
846 case RESIDUE_BLOCK:
847 xoff = get_value(c, BINK_SRC_X_OFF);
848 yoff = get_value(c, BINK_SRC_Y_OFF);
849 ref = prev + xoff + yoff * stride;
850 if (ref < ref_start || ref > ref_end) {
851 av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
852 bx*8 + xoff, by*8 + yoff);
853 return -1;
854 }
855 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
856 c->dsp.clear_block(block);
857 v = get_bits(&gb, 7);
858 read_residue(&gb, block, v);
859 c->dsp.add_pixels8(dst, block, stride);
860 break;
861 case INTRA_BLOCK:
862 c->dsp.clear_block(block);
863 block[0] = get_value(c, BINK_SRC_INTRA_DC);
864 read_dct_coeffs(&gb, block, c->scantable.permutated, 1);
865 c->dsp.idct_put(dst, stride, block);
866 break;
867 case FILL_BLOCK:
868 v = get_value(c, BINK_SRC_COLORS);
869 c->dsp.fill_block_tab[1](dst, v, stride, 8);
870 break;
871 case INTER_BLOCK:
872 xoff = get_value(c, BINK_SRC_X_OFF);
873 yoff = get_value(c, BINK_SRC_Y_OFF);
874 ref = prev + xoff + yoff * stride;
875 c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
876 c->dsp.clear_block(block);
877 block[0] = get_value(c, BINK_SRC_INTER_DC);
878 read_dct_coeffs(&gb, block, c->scantable.permutated, 0);
879 c->dsp.idct_add(dst, stride, block);
880 break;
881 case PATTERN_BLOCK:
882 for (i = 0; i < 2; i++)
883 col[i] = get_value(c, BINK_SRC_COLORS);
884 for (i = 0; i < 8; i++) {
885 v = get_value(c, BINK_SRC_PATTERN);
886 for (j = 0; j < 8; j++, v >>= 1)
887 dst[i*stride + j] = col[v & 1];
888 }
889 break;
890 case RAW_BLOCK:
891 for (i = 0; i < 8; i++)
892 memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
893 c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
894 break;
895 default:
896 av_log(avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
897 return -1;
898 }
899 }
900 }
901 if (get_bits_count(&gb) & 0x1F) //next plane data starts at 32-bit boundary
902 skip_bits_long(&gb, 32 - (get_bits_count(&gb) & 0x1F));
903 }
904 emms_c();
905
906 *data_size = sizeof(AVFrame);
907 *(AVFrame*)data = c->pic;
908
909 FFSWAP(AVFrame, c->pic, c->last);
910
911 /* always report that the buffer was completely consumed */
912 return pkt->size;
913 }
914
915 static av_cold int decode_init(AVCodecContext *avctx)
916 {
917 BinkContext * const c = avctx->priv_data;
918 static VLC_TYPE table[16 * 128][2];
919 int i;
920
921 c->version = avctx->codec_tag >> 24;
922 if (c->version < 'c') {
923 av_log(avctx, AV_LOG_ERROR, "Too old version '%c'\n", c->version);
924 return -1;
925 }
926 c->swap_planes = c->version >= 'i';
927 if (!bink_trees[15].table) {
928 for (i = 0; i < 16; i++) {
929 const int maxbits = bink_tree_lens[i][15];
930 bink_trees[i].table = table + i*128;
931 bink_trees[i].table_allocated = 1 << maxbits;
932 init_vlc(&bink_trees[i], maxbits, 16,
933 bink_tree_lens[i], 1, 1,
934 bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
935 }
936 }
937 c->avctx = avctx;
938
939 c->pic.data[0] = NULL;
940
941 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
942 return 1;
943 }
944
945 avctx->pix_fmt = PIX_FMT_YUV420P;
946
947 avctx->idct_algo = FF_IDCT_BINK;
948 dsputil_init(&c->dsp, avctx);
949 ff_init_scantable(c->dsp.idct_permutation, &c->scantable, bink_scan);
950
951 init_bundles(c);
952
953 return 0;
954 }
955
956 static av_cold int decode_end(AVCodecContext *avctx)
957 {
958 BinkContext * const c = avctx->priv_data;
959
960 if (c->pic.data[0])
961 avctx->release_buffer(avctx, &c->pic);
962 if (c->last.data[0])
963 avctx->release_buffer(avctx, &c->last);
964
965 free_bundles(c);
966 return 0;
967 }
968
969 AVCodec bink_decoder = {
970 "binkvideo",
971 CODEC_TYPE_VIDEO,
972 CODEC_ID_BINKVIDEO,
973 sizeof(BinkContext),
974 decode_init,
975 NULL,
976 decode_end,
977 decode_frame,
978 .long_name = NULL_IF_CONFIG_SMALL("Bink video"),
979 };