~ubuntu-branches/ubuntu/oneiric/libav/oneiric

« back to all changes in this revision

Viewing changes to libavcodec/bink.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-04-30 14:27:42 UTC
  • mfrom: (1.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110430142742-quvblxk1tj6adlh5
Tags: 4:0.7~b1-1ubuntu1
* Merge from debian. Remaining changes:
  - don't build against libfaad, libdirac, librtmp and libopenjpeg
    (all in universe)
  - explicitly --enable-pic on powerpc, cf. LP #654666
  - different arm configure bits that should probably better be
    merged into debian
* Cherry-picked from git: 
  - install doc/APIChanges and refer to them in NEWS.Debian (Closes: #623682)
  - don't try to install non-existing documentation, fixes FTBFS on powerpc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Bink video decoder
3
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
 
4
 * Copyright (C) 2011 Peter Ross <pross@xvid.org>
 
5
 *
 
6
 * This file is part of Libav.
 
7
 *
 
8
 * Libav is free software; you can redistribute it and/or
8
9
 * modify it under the terms of the GNU Lesser General Public
9
10
 * License as published by the Free Software Foundation; either
10
11
 * version 2.1 of the License, or (at your option) any later version.
11
12
 *
12
 
 * FFmpeg is distributed in the hope that it will be useful,
 
13
 * Libav is distributed in the hope that it will be useful,
13
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
16
 * Lesser General Public License for more details.
16
17
 *
17
18
 * 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
 * License along with Libav; if not, write to the Free Software
19
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21
 */
21
22
 
 
23
#include "libavutil/imgutils.h"
22
24
#include "avcodec.h"
23
25
#include "dsputil.h"
24
26
#include "binkdata.h"
33
35
static VLC bink_trees[16];
34
36
 
35
37
/**
 
38
 * IDs for different data types used in old version of Bink video codec
 
39
 */
 
40
enum OldSources {
 
41
    BINKB_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
 
42
    BINKB_SRC_COLORS,          ///< pixel values used for different block types
 
43
    BINKB_SRC_PATTERN,         ///< 8-bit values for 2-colour pattern fill
 
44
    BINKB_SRC_X_OFF,           ///< X components of motion value
 
45
    BINKB_SRC_Y_OFF,           ///< Y components of motion value
 
46
    BINKB_SRC_INTRA_DC,        ///< DC values for intrablocks with DCT
 
47
    BINKB_SRC_INTER_DC,        ///< DC values for interblocks with DCT
 
48
    BINKB_SRC_INTRA_Q,         ///< quantizer values for intrablocks with DCT
 
49
    BINKB_SRC_INTER_Q,         ///< quantizer values for interblocks with DCT
 
50
    BINKB_SRC_INTER_COEFS,     ///< number of coefficients for residue blocks
 
51
 
 
52
    BINKB_NB_SRC
 
53
};
 
54
 
 
55
static const int binkb_bundle_sizes[BINKB_NB_SRC] = {
 
56
    4, 8, 8, 5, 5, 11, 11, 4, 4, 7
 
57
};
 
58
 
 
59
static const int binkb_bundle_signed[BINKB_NB_SRC] = {
 
60
    0, 0, 0, 1, 1, 0, 1, 0, 0, 0
 
61
};
 
62
 
 
63
static uint32_t binkb_intra_quant[16][64];
 
64
static uint32_t binkb_inter_quant[16][64];
 
65
 
 
66
/**
36
67
 * IDs for different data types used in Bink video codec
37
68
 */
38
69
enum Sources {
84
115
    int            swap_planes;
85
116
    ScanTable      scantable;            ///< permutated scantable for DCT coeffs decoding
86
117
 
87
 
    Bundle         bundle[BINK_NB_SRC];  ///< bundles for decoding all data types
 
118
    Bundle         bundle[BINKB_NB_SRC]; ///< bundles for decoding all data types
88
119
    Tree           col_high[16];         ///< trees for decoding high nibble in "colours" data type
89
120
    int            col_lastval;          ///< value of last decoded high nibble in "colours" data type
90
121
} BinkContext;
106
137
};
107
138
 
108
139
/**
109
 
 * Initializes length length in all bundles.
 
140
 * Initialize length length in all bundles.
110
141
 *
111
142
 * @param c     decoder context
112
143
 * @param width plane width
118
149
 
119
150
    c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
120
151
 
121
 
    c->bundle[BINK_SRC_COLORS].len = av_log2((width >> 3)*64 + 511) + 1;
 
152
    c->bundle[BINK_SRC_COLORS].len = av_log2(bw*64 + 511) + 1;
122
153
 
123
154
    c->bundle[BINK_SRC_INTRA_DC].len =
124
155
    c->bundle[BINK_SRC_INTER_DC].len =
127
158
 
128
159
    c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
129
160
 
130
 
    c->bundle[BINK_SRC_RUN].len = av_log2((width >> 3)*48 + 511) + 1;
 
161
    c->bundle[BINK_SRC_RUN].len = av_log2(bw*48 + 511) + 1;
131
162
}
132
163
 
133
164
/**
134
 
 * Allocates memory for bundles.
 
165
 * Allocate memory for bundles.
135
166
 *
136
167
 * @param c decoder context
137
168
 */
144
175
    bh = (c->avctx->height + 7) >> 3;
145
176
    blocks = bw * bh;
146
177
 
147
 
    for (i = 0; i < BINK_NB_SRC; i++) {
 
178
    for (i = 0; i < BINKB_NB_SRC; i++) {
148
179
        c->bundle[i].data = av_malloc(blocks * 64);
149
180
        c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
150
181
    }
151
182
}
152
183
 
153
184
/**
154
 
 * Frees memory used by bundles.
 
185
 * Free memory used by bundles.
155
186
 *
156
187
 * @param c decoder context
157
188
 */
158
189
static av_cold void free_bundles(BinkContext *c)
159
190
{
160
191
    int i;
161
 
    for (i = 0; i < BINK_NB_SRC; i++)
 
192
    for (i = 0; i < BINKB_NB_SRC; i++)
162
193
        av_freep(&c->bundle[i].data);
163
194
}
164
195
 
165
196
/**
166
 
 * Merges two consequent lists of equal size depending on bits read.
 
197
 * Merge two consequent lists of equal size depending on bits read.
167
198
 *
168
199
 * @param gb   context for reading bits
169
200
 * @param dst  buffer where merged list will be written to
192
223
}
193
224
 
194
225
/**
195
 
 * Reads information about Huffman tree used to decode data.
 
226
 * Read information about Huffman tree used to decode data.
196
227
 *
197
228
 * @param gb   context for reading bits
198
229
 * @param tree pointer for storing tree data
233
264
}
234
265
 
235
266
/**
236
 
 * Prepares bundle for decoding data.
 
267
 * Prepare bundle for decoding data.
237
268
 *
238
269
 * @param gb          context for reading bits
239
270
 * @param c           decoder context
324
355
    return 0;
325
356
}
326
357
 
327
 
const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
 
358
static const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
328
359
 
329
360
static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
330
361
{
462
493
}
463
494
 
464
495
/**
465
 
 * Retrieves next value from bundle.
 
496
 * Retrieve next value from bundle.
466
497
 *
467
498
 * @param c      decoder context
468
499
 * @param bundle bundle number
469
500
 */
470
501
static inline int get_value(BinkContext *c, int bundle)
471
502
{
472
 
    int16_t ret;
 
503
    int ret;
473
504
 
474
505
    if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
475
506
        return *c->bundle[bundle].cur_ptr++;
480
511
    return ret;
481
512
}
482
513
 
 
514
static void binkb_init_bundle(BinkContext *c, int bundle_num)
 
515
{
 
516
    c->bundle[bundle_num].cur_dec =
 
517
    c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
 
518
    c->bundle[bundle_num].len = 13;
 
519
}
 
520
 
 
521
static void binkb_init_bundles(BinkContext *c)
 
522
{
 
523
    int i;
 
524
    for (i = 0; i < BINKB_NB_SRC; i++)
 
525
        binkb_init_bundle(c, i);
 
526
}
 
527
 
 
528
static int binkb_read_bundle(BinkContext *c, GetBitContext *gb, int bundle_num)
 
529
{
 
530
    const int bits = binkb_bundle_sizes[bundle_num];
 
531
    const int mask = 1 << (bits - 1);
 
532
    const int issigned = binkb_bundle_signed[bundle_num];
 
533
    Bundle *b = &c->bundle[bundle_num];
 
534
    int i, len;
 
535
 
 
536
    CHECK_READ_VAL(gb, b, len);
 
537
    if (bits <= 8) {
 
538
        if (!issigned) {
 
539
            for (i = 0; i < len; i++)
 
540
                *b->cur_dec++ = get_bits(gb, bits);
 
541
        } else {
 
542
            for (i = 0; i < len; i++)
 
543
                *b->cur_dec++ = get_bits(gb, bits) - mask;
 
544
        }
 
545
    } else {
 
546
        int16_t *dst = (int16_t*)b->cur_dec;
 
547
 
 
548
        if (!issigned) {
 
549
            for (i = 0; i < len; i++)
 
550
                *dst++ = get_bits(gb, bits);
 
551
        } else {
 
552
            for (i = 0; i < len; i++)
 
553
                *dst++ = get_bits(gb, bits) - mask;
 
554
        }
 
555
        b->cur_dec = (uint8_t*)dst;
 
556
    }
 
557
    return 0;
 
558
}
 
559
 
 
560
static inline int binkb_get_value(BinkContext *c, int bundle_num)
 
561
{
 
562
    int16_t ret;
 
563
    const int bits = binkb_bundle_sizes[bundle_num];
 
564
 
 
565
    if (bits <= 8) {
 
566
        int val = *c->bundle[bundle_num].cur_ptr++;
 
567
        return binkb_bundle_signed[bundle_num] ? (int8_t)val : val;
 
568
    }
 
569
    ret = *(int16_t*)c->bundle[bundle_num].cur_ptr;
 
570
    c->bundle[bundle_num].cur_ptr += 2;
 
571
    return ret;
 
572
}
 
573
 
483
574
/**
484
 
 * Reads 8x8 block of DCT coefficients.
 
575
 * Read 8x8 block of DCT coefficients.
485
576
 *
486
577
 * @param gb       context for reading bits
487
578
 * @param block    place for storing coefficients
488
579
 * @param scan     scan order table
489
 
 * @param is_intra tells what set of quantizer matrices to use
 
580
 * @param quant_matrices quantization matrices
490
581
 * @return 0 for success, negative value in other cases
491
582
 */
492
583
static int read_dct_coeffs(GetBitContext *gb, DCTELEM block[64], const uint8_t *scan,
493
 
                           int is_intra)
 
584
                           const uint32_t quant_matrices[16][64], int q)
494
585
{
495
586
    int coef_list[128];
496
587
    int mode_list[128];
570
661
        }
571
662
    }
572
663
 
573
 
    quant_idx = get_bits(gb, 4);
574
 
    quant = is_intra ? bink_intra_quant[quant_idx]
575
 
                     : bink_inter_quant[quant_idx];
 
664
    if (q == -1) {
 
665
        quant_idx = get_bits(gb, 4);
 
666
    } else {
 
667
        quant_idx = q;
 
668
    }
 
669
 
 
670
    quant = quant_matrices[quant_idx];
 
671
 
576
672
    block[0] = (block[0] * quant[0]) >> 11;
577
673
    for (i = 0; i < coef_count; i++) {
578
674
        int idx = coef_idx[i];
583
679
}
584
680
 
585
681
/**
586
 
 * Reads 8x8 block with residue after motion compensation.
 
682
 * Read 8x8 block with residue after motion compensation.
587
683
 *
588
684
 * @param gb          context for reading bits
589
685
 * @param block       place to store read data
672
768
    return 0;
673
769
}
674
770
 
 
771
/**
 
772
 * Copy 8x8 block from source to destination, where src and dst may be overlapped
 
773
 */
 
774
static inline void put_pixels8x8_overlapped(uint8_t *dst, uint8_t *src, int stride)
 
775
{
 
776
    uint8_t tmp[64];
 
777
    int i;
 
778
    for (i = 0; i < 8; i++)
 
779
        memcpy(tmp + i*8, src + i*stride, 8);
 
780
    for (i = 0; i < 8; i++)
 
781
        memcpy(dst + i*stride, tmp + i*8, 8);
 
782
}
 
783
 
 
784
static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
 
785
                              int is_key, int is_chroma)
 
786
{
 
787
    int blk;
 
788
    int i, j, bx, by;
 
789
    uint8_t *dst, *ref, *ref_start, *ref_end;
 
790
    int v, col[2];
 
791
    const uint8_t *scan;
 
792
    int xoff, yoff;
 
793
    LOCAL_ALIGNED_16(DCTELEM, block, [64]);
 
794
    int coordmap[64];
 
795
    int ybias = is_key ? -15 : 0;
 
796
    int qp;
 
797
 
 
798
    const int stride = c->pic.linesize[plane_idx];
 
799
    int bw = is_chroma ? (c->avctx->width  + 15) >> 4 : (c->avctx->width  + 7) >> 3;
 
800
    int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
 
801
 
 
802
    binkb_init_bundles(c);
 
803
    ref_start = c->pic.data[plane_idx];
 
804
    ref_end   = c->pic.data[plane_idx] + (bh * c->pic.linesize[plane_idx] + bw) * 8;
 
805
 
 
806
    for (i = 0; i < 64; i++)
 
807
        coordmap[i] = (i & 7) + (i >> 3) * stride;
 
808
 
 
809
    for (by = 0; by < bh; by++) {
 
810
        for (i = 0; i < BINKB_NB_SRC; i++) {
 
811
            if (binkb_read_bundle(c, gb, i) < 0)
 
812
                return -1;
 
813
        }
 
814
 
 
815
        dst  = c->pic.data[plane_idx]  + 8*by*stride;
 
816
        for (bx = 0; bx < bw; bx++, dst += 8) {
 
817
            blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
 
818
            switch (blk) {
 
819
            case 0:
 
820
                break;
 
821
            case 1:
 
822
                scan = bink_patterns[get_bits(gb, 4)];
 
823
                i = 0;
 
824
                do {
 
825
                    int mode, run;
 
826
 
 
827
                    mode = get_bits1(gb);
 
828
                    run = get_bits(gb, binkb_runbits[i]) + 1;
 
829
 
 
830
                    i += run;
 
831
                    if (i > 64) {
 
832
                        av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
 
833
                        return -1;
 
834
                    }
 
835
                    if (mode) {
 
836
                        v = binkb_get_value(c, BINKB_SRC_COLORS);
 
837
                        for (j = 0; j < run; j++)
 
838
                            dst[coordmap[*scan++]] = v;
 
839
                    } else {
 
840
                        for (j = 0; j < run; j++)
 
841
                            dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
 
842
                    }
 
843
                } while (i < 63);
 
844
                if (i == 63)
 
845
                    dst[coordmap[*scan++]] = binkb_get_value(c, BINKB_SRC_COLORS);
 
846
                break;
 
847
            case 2:
 
848
                c->dsp.clear_block(block);
 
849
                block[0] = binkb_get_value(c, BINKB_SRC_INTRA_DC);
 
850
                qp = binkb_get_value(c, BINKB_SRC_INTRA_Q);
 
851
                read_dct_coeffs(gb, block, c->scantable.permutated, binkb_intra_quant, qp);
 
852
                c->dsp.idct_put(dst, stride, block);
 
853
                break;
 
854
            case 3:
 
855
                xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
 
856
                yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
 
857
                ref = dst + xoff + yoff * stride;
 
858
                if (ref < ref_start || ref + 8*stride > ref_end) {
 
859
                    av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
 
860
                } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
 
861
                    c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
 
862
                } else {
 
863
                    put_pixels8x8_overlapped(dst, ref, stride);
 
864
                }
 
865
                c->dsp.clear_block(block);
 
866
                v = binkb_get_value(c, BINKB_SRC_INTER_COEFS);
 
867
                read_residue(gb, block, v);
 
868
                c->dsp.add_pixels8(dst, block, stride);
 
869
                break;
 
870
            case 4:
 
871
                xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
 
872
                yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
 
873
                ref = dst + xoff + yoff * stride;
 
874
                if (ref < ref_start || ref + 8 * stride > ref_end) {
 
875
                    av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
 
876
                } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
 
877
                    c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
 
878
                } else {
 
879
                    put_pixels8x8_overlapped(dst, ref, stride);
 
880
                }
 
881
                c->dsp.clear_block(block);
 
882
                block[0] = binkb_get_value(c, BINKB_SRC_INTER_DC);
 
883
                qp = binkb_get_value(c, BINKB_SRC_INTER_Q);
 
884
                read_dct_coeffs(gb, block, c->scantable.permutated, binkb_inter_quant, qp);
 
885
                c->dsp.idct_add(dst, stride, block);
 
886
                break;
 
887
            case 5:
 
888
                v = binkb_get_value(c, BINKB_SRC_COLORS);
 
889
                c->dsp.fill_block_tab[1](dst, v, stride, 8);
 
890
                break;
 
891
            case 6:
 
892
                for (i = 0; i < 2; i++)
 
893
                    col[i] = binkb_get_value(c, BINKB_SRC_COLORS);
 
894
                for (i = 0; i < 8; i++) {
 
895
                    v = binkb_get_value(c, BINKB_SRC_PATTERN);
 
896
                    for (j = 0; j < 8; j++, v >>= 1)
 
897
                        dst[i*stride + j] = col[v & 1];
 
898
                }
 
899
                break;
 
900
            case 7:
 
901
                xoff = binkb_get_value(c, BINKB_SRC_X_OFF);
 
902
                yoff = binkb_get_value(c, BINKB_SRC_Y_OFF) + ybias;
 
903
                ref = dst + xoff + yoff * stride;
 
904
                if (ref < ref_start || ref + 8 * stride > ref_end) {
 
905
                    av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
 
906
                } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
 
907
                    c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
 
908
                } else {
 
909
                    put_pixels8x8_overlapped(dst, ref, stride);
 
910
                }
 
911
                break;
 
912
            case 8:
 
913
                for (i = 0; i < 8; i++)
 
914
                    memcpy(dst + i*stride, c->bundle[BINKB_SRC_COLORS].cur_ptr + i*8, 8);
 
915
                c->bundle[BINKB_SRC_COLORS].cur_ptr += 64;
 
916
                break;
 
917
            default:
 
918
                av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
 
919
                return -1;
 
920
            }
 
921
        }
 
922
    }
 
923
    if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
 
924
        skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
 
925
 
 
926
    return 0;
 
927
}
 
928
 
675
929
static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
676
930
                             int is_chroma)
677
931
{
681
935
    int v, col[2];
682
936
    const uint8_t *scan;
683
937
    int xoff, yoff;
684
 
    DECLARE_ALIGNED(16, DCTELEM, block[64]);
685
 
    DECLARE_ALIGNED(16, uint8_t, ublock[64]);
 
938
    LOCAL_ALIGNED_16(DCTELEM, block, [64]);
 
939
    LOCAL_ALIGNED_16(uint8_t, ublock, [64]);
686
940
    int coordmap[64];
687
941
 
688
942
    const int stride = c->pic.linesize[plane_idx];
767
1021
                case INTRA_BLOCK:
768
1022
                    c->dsp.clear_block(block);
769
1023
                    block[0] = get_value(c, BINK_SRC_INTRA_DC);
770
 
                    read_dct_coeffs(gb, block, c->scantable.permutated, 1);
 
1024
                    read_dct_coeffs(gb, block, c->scantable.permutated, bink_intra_quant, -1);
771
1025
                    c->dsp.idct(block);
772
1026
                    c->dsp.put_pixels_nonclamped(block, ublock, 8);
773
1027
                    break;
851
1105
            case INTRA_BLOCK:
852
1106
                c->dsp.clear_block(block);
853
1107
                block[0] = get_value(c, BINK_SRC_INTRA_DC);
854
 
                read_dct_coeffs(gb, block, c->scantable.permutated, 1);
 
1108
                read_dct_coeffs(gb, block, c->scantable.permutated, bink_intra_quant, -1);
855
1109
                c->dsp.idct_put(dst, stride, block);
856
1110
                break;
857
1111
            case FILL_BLOCK:
865
1119
                c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
866
1120
                c->dsp.clear_block(block);
867
1121
                block[0] = get_value(c, BINK_SRC_INTER_DC);
868
 
                read_dct_coeffs(gb, block, c->scantable.permutated, 0);
 
1122
                read_dct_coeffs(gb, block, c->scantable.permutated, bink_inter_quant, -1);
869
1123
                c->dsp.idct_add(dst, stride, block);
870
1124
                break;
871
1125
            case PATTERN_BLOCK:
901
1155
    int plane, plane_idx;
902
1156
    int bits_count = pkt->size << 3;
903
1157
 
904
 
    if(c->pic.data[0])
905
 
        avctx->release_buffer(avctx, &c->pic);
 
1158
    if (c->version > 'b') {
 
1159
        if(c->pic.data[0])
 
1160
            avctx->release_buffer(avctx, &c->pic);
906
1161
 
907
 
    if(avctx->get_buffer(avctx, &c->pic) < 0){
908
 
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
909
 
        return -1;
 
1162
        if(avctx->get_buffer(avctx, &c->pic) < 0){
 
1163
            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
 
1164
            return -1;
 
1165
        }
 
1166
    } else {
 
1167
        if(avctx->reget_buffer(avctx, &c->pic) < 0){
 
1168
            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
 
1169
            return -1;
 
1170
        }
910
1171
    }
911
1172
 
912
1173
    init_get_bits(&gb, pkt->data, bits_count);
922
1183
    for (plane = 0; plane < 3; plane++) {
923
1184
        plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
924
1185
 
925
 
        if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
926
 
            return -1;
 
1186
        if (c->version > 'b') {
 
1187
            if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
 
1188
                return -1;
 
1189
        } else {
 
1190
            if (binkb_decode_plane(c, &gb, plane_idx, !pkt->pts, !!plane) < 0)
 
1191
                return -1;
 
1192
        }
927
1193
        if (get_bits_count(&gb) >= bits_count)
928
1194
            break;
929
1195
    }
932
1198
    *data_size = sizeof(AVFrame);
933
1199
    *(AVFrame*)data = c->pic;
934
1200
 
935
 
    FFSWAP(AVFrame, c->pic, c->last);
 
1201
    if (c->version > 'b')
 
1202
        FFSWAP(AVFrame, c->pic, c->last);
936
1203
 
937
1204
    /* always report that the buffer was completely consumed */
938
1205
    return pkt->size;
939
1206
}
940
1207
 
 
1208
/**
 
1209
 * Caclulate quantization tables for version b
 
1210
 */
 
1211
static av_cold void binkb_calc_quant()
 
1212
{
 
1213
    uint8_t inv_bink_scan[64];
 
1214
    double s[64];
 
1215
    int i, j;
 
1216
 
 
1217
    for (j = 0; j < 8; j++) {
 
1218
        for (i = 0; i < 8; i++) {
 
1219
            if (j && j != 4)
 
1220
               if (i && i != 4)
 
1221
                   s[j*8 + i] = cos(j * M_PI/16.0) * cos(i * M_PI/16.0) * 2.0;
 
1222
               else
 
1223
                   s[j*8 + i] = cos(j * M_PI/16.0) * sqrt(2.0);
 
1224
            else
 
1225
               if (i && i != 4)
 
1226
                   s[j*8 + i] = cos(i * M_PI/16.0) * sqrt(2.0);
 
1227
               else
 
1228
                   s[j*8 + i] = 1.0;
 
1229
        }
 
1230
    }
 
1231
 
 
1232
    for (i = 0; i < 64; i++)
 
1233
        inv_bink_scan[bink_scan[i]] = i;
 
1234
 
 
1235
    for (j = 0; j < 16; j++) {
 
1236
        for (i = 0; i < 64; i++) {
 
1237
            int k = inv_bink_scan[i];
 
1238
            if (s[i] == 1.0) {
 
1239
                binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] *
 
1240
                                          binkb_num[j]/binkb_den[j];
 
1241
                binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] *
 
1242
                                          binkb_num[j]/binkb_den[j];
 
1243
            } else {
 
1244
                binkb_intra_quant[j][k] = (1L << 12) * binkb_intra_seed[i] * s[i] *
 
1245
                                          binkb_num[j]/(double)binkb_den[j];
 
1246
                binkb_inter_quant[j][k] = (1L << 12) * binkb_inter_seed[i] * s[i] *
 
1247
                                          binkb_num[j]/(double)binkb_den[j];
 
1248
            }
 
1249
        }
 
1250
    }
 
1251
}
 
1252
 
941
1253
static av_cold int decode_init(AVCodecContext *avctx)
942
1254
{
943
1255
    BinkContext * const c = avctx->priv_data;
944
1256
    static VLC_TYPE table[16 * 128][2];
 
1257
    static int binkb_initialised = 0;
945
1258
    int i;
946
1259
    int flags;
947
1260
 
948
1261
    c->version = avctx->codec_tag >> 24;
949
 
    if (c->version < 'c') {
950
 
        av_log(avctx, AV_LOG_ERROR, "Too old version '%c'\n", c->version);
951
 
        return -1;
952
 
    }
953
1262
    if (avctx->extradata_size < 4) {
954
1263
        av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
955
1264
        return -1;
971
1280
 
972
1281
    c->pic.data[0] = NULL;
973
1282
 
974
 
    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
 
1283
    if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
975
1284
        return 1;
976
1285
    }
977
1286
 
983
1292
 
984
1293
    init_bundles(c);
985
1294
 
 
1295
    if (c->version == 'b') {
 
1296
        if (!binkb_initialised) {
 
1297
            binkb_calc_quant();
 
1298
            binkb_initialised = 1;
 
1299
        }
 
1300
    }
 
1301
 
986
1302
    return 0;
987
1303
}
988
1304
 
999
1315
    return 0;
1000
1316
}
1001
1317
 
1002
 
AVCodec bink_decoder = {
 
1318
AVCodec ff_bink_decoder = {
1003
1319
    "binkvideo",
1004
1320
    AVMEDIA_TYPE_VIDEO,
1005
1321
    CODEC_ID_BINKVIDEO,