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