GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/vp3.c Lines: 1257 1560 80.6 %
Date: 2020-08-14 10:39:37 Branches: 814 1114 73.1 %

Line Branch Exec Source
1
/*
2
 * Copyright (C) 2003-2004 The FFmpeg project
3
 * Copyright (C) 2019 Peter Ross
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
/**
23
 * @file
24
 * On2 VP3/VP4 Video Decoder
25
 *
26
 * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
27
 * For more information about the VP3 coding process, visit:
28
 *   http://wiki.multimedia.cx/index.php?title=On2_VP3
29
 *
30
 * Theora decoder by Alex Beregszaszi
31
 */
32
33
#include <stdio.h>
34
#include <stdlib.h>
35
#include <string.h>
36
37
#include "libavutil/imgutils.h"
38
39
#include "avcodec.h"
40
#include "get_bits.h"
41
#include "hpeldsp.h"
42
#include "internal.h"
43
#include "mathops.h"
44
#include "thread.h"
45
#include "videodsp.h"
46
#include "vp3data.h"
47
#include "vp4data.h"
48
#include "vp3dsp.h"
49
#include "xiph.h"
50
51
#define FRAGMENT_PIXELS 8
52
53
// FIXME split things out into their own arrays
54
typedef struct Vp3Fragment {
55
    int16_t dc;
56
    uint8_t coding_method;
57
    uint8_t qpi;
58
} Vp3Fragment;
59
60
#define SB_NOT_CODED        0
61
#define SB_PARTIALLY_CODED  1
62
#define SB_FULLY_CODED      2
63
64
// This is the maximum length of a single long bit run that can be encoded
65
// for superblock coding or block qps. Theora special-cases this to read a
66
// bit instead of flipping the current bit to allow for runs longer than 4129.
67
#define MAXIMUM_LONG_BIT_RUN 4129
68
69
#define MODE_INTER_NO_MV      0
70
#define MODE_INTRA            1
71
#define MODE_INTER_PLUS_MV    2
72
#define MODE_INTER_LAST_MV    3
73
#define MODE_INTER_PRIOR_LAST 4
74
#define MODE_USING_GOLDEN     5
75
#define MODE_GOLDEN_MV        6
76
#define MODE_INTER_FOURMV     7
77
#define CODING_MODE_COUNT     8
78
79
/* special internal mode */
80
#define MODE_COPY             8
81
82
static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
83
static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
84
85
86
/* There are 6 preset schemes, plus a free-form scheme */
87
static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
88
    /* scheme 1: Last motion vector dominates */
89
    { MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
90
      MODE_INTER_PLUS_MV,    MODE_INTER_NO_MV,
91
      MODE_INTRA,            MODE_USING_GOLDEN,
92
      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
93
94
    /* scheme 2 */
95
    { MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
96
      MODE_INTER_NO_MV,      MODE_INTER_PLUS_MV,
97
      MODE_INTRA,            MODE_USING_GOLDEN,
98
      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
99
100
    /* scheme 3 */
101
    { MODE_INTER_LAST_MV,    MODE_INTER_PLUS_MV,
102
      MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
103
      MODE_INTRA,            MODE_USING_GOLDEN,
104
      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
105
106
    /* scheme 4 */
107
    { MODE_INTER_LAST_MV,    MODE_INTER_PLUS_MV,
108
      MODE_INTER_NO_MV,      MODE_INTER_PRIOR_LAST,
109
      MODE_INTRA,            MODE_USING_GOLDEN,
110
      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
111
112
    /* scheme 5: No motion vector dominates */
113
    { MODE_INTER_NO_MV,      MODE_INTER_LAST_MV,
114
      MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
115
      MODE_INTRA,            MODE_USING_GOLDEN,
116
      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
117
118
    /* scheme 6 */
119
    { MODE_INTER_NO_MV,      MODE_USING_GOLDEN,
120
      MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
121
      MODE_INTER_PLUS_MV,    MODE_INTRA,
122
      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
123
};
124
125
static const uint8_t hilbert_offset[16][2] = {
126
    { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
127
    { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
128
    { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
129
    { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
130
};
131
132
enum {
133
    VP4_DC_INTRA  = 0,
134
    VP4_DC_INTER  = 1,
135
    VP4_DC_GOLDEN = 2,
136
    NB_VP4_DC_TYPES,
137
    VP4_DC_UNDEFINED = NB_VP4_DC_TYPES
138
};
139
140
static const uint8_t vp4_pred_block_type_map[8] = {
141
    [MODE_INTER_NO_MV]      = VP4_DC_INTER,
142
    [MODE_INTRA]            = VP4_DC_INTRA,
143
    [MODE_INTER_PLUS_MV]    = VP4_DC_INTER,
144
    [MODE_INTER_LAST_MV]    = VP4_DC_INTER,
145
    [MODE_INTER_PRIOR_LAST] = VP4_DC_INTER,
146
    [MODE_USING_GOLDEN]     = VP4_DC_GOLDEN,
147
    [MODE_GOLDEN_MV]        = VP4_DC_GOLDEN,
148
    [MODE_INTER_FOURMV]     = VP4_DC_INTER,
149
};
150
151
typedef struct {
152
    int dc;
153
    int type;
154
} VP4Predictor;
155
156
#define MIN_DEQUANT_VAL 2
157
158
typedef struct Vp3DecodeContext {
159
    AVCodecContext *avctx;
160
    int theora, theora_tables, theora_header;
161
    int version;
162
    int width, height;
163
    int chroma_x_shift, chroma_y_shift;
164
    ThreadFrame golden_frame;
165
    ThreadFrame last_frame;
166
    ThreadFrame current_frame;
167
    int keyframe;
168
    uint8_t idct_permutation[64];
169
    uint8_t idct_scantable[64];
170
    HpelDSPContext hdsp;
171
    VideoDSPContext vdsp;
172
    VP3DSPContext vp3dsp;
173
    DECLARE_ALIGNED(16, int16_t, block)[64];
174
    int flipped_image;
175
    int last_slice_end;
176
    int skip_loop_filter;
177
178
    int qps[3];
179
    int nqps;
180
    int last_qps[3];
181
182
    int superblock_count;
183
    int y_superblock_width;
184
    int y_superblock_height;
185
    int y_superblock_count;
186
    int c_superblock_width;
187
    int c_superblock_height;
188
    int c_superblock_count;
189
    int u_superblock_start;
190
    int v_superblock_start;
191
    unsigned char *superblock_coding;
192
193
    int macroblock_count; /* y macroblock count */
194
    int macroblock_width;
195
    int macroblock_height;
196
    int c_macroblock_count;
197
    int c_macroblock_width;
198
    int c_macroblock_height;
199
    int yuv_macroblock_count; /* y+u+v macroblock count */
200
201
    int fragment_count;
202
    int fragment_width[2];
203
    int fragment_height[2];
204
205
    Vp3Fragment *all_fragments;
206
    int fragment_start[3];
207
    int data_offset[3];
208
    uint8_t offset_x;
209
    uint8_t offset_y;
210
    int offset_x_warned;
211
212
    int8_t (*motion_val[2])[2];
213
214
    /* tables */
215
    uint16_t coded_dc_scale_factor[2][64];
216
    uint32_t coded_ac_scale_factor[64];
217
    uint8_t base_matrix[384][64];
218
    uint8_t qr_count[2][3];
219
    uint8_t qr_size[2][3][64];
220
    uint16_t qr_base[2][3][64];
221
222
    /**
223
     * This is a list of all tokens in bitstream order. Reordering takes place
224
     * by pulling from each level during IDCT. As a consequence, IDCT must be
225
     * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
226
     * otherwise. The 32 different tokens with up to 12 bits of extradata are
227
     * collapsed into 3 types, packed as follows:
228
     *   (from the low to high bits)
229
     *
230
     * 2 bits: type (0,1,2)
231
     *   0: EOB run, 14 bits for run length (12 needed)
232
     *   1: zero run, 7 bits for run length
233
     *                7 bits for the next coefficient (3 needed)
234
     *   2: coefficient, 14 bits (11 needed)
235
     *
236
     * Coefficients are signed, so are packed in the highest bits for automatic
237
     * sign extension.
238
     */
239
    int16_t *dct_tokens[3][64];
240
    int16_t *dct_tokens_base;
241
#define TOKEN_EOB(eob_run)              ((eob_run) << 2)
242
#define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
243
#define TOKEN_COEFF(coeff)              (((coeff) * 4) + 2)
244
245
    /**
246
     * number of blocks that contain DCT coefficients at
247
     * the given level or higher
248
     */
249
    int num_coded_frags[3][64];
250
    int total_num_coded_frags;
251
252
    /* this is a list of indexes into the all_fragments array indicating
253
     * which of the fragments are coded */
254
    int *coded_fragment_list[3];
255
256
    int *kf_coded_fragment_list;
257
    int *nkf_coded_fragment_list;
258
    int num_kf_coded_fragment[3];
259
260
    VLC dc_vlc[16];
261
    VLC ac_vlc_1[16];
262
    VLC ac_vlc_2[16];
263
    VLC ac_vlc_3[16];
264
    VLC ac_vlc_4[16];
265
266
    VLC superblock_run_length_vlc; /* version < 2 */
267
    VLC fragment_run_length_vlc; /* version < 2 */
268
    VLC block_pattern_vlc[2]; /* version >= 2*/
269
    VLC mode_code_vlc;
270
    VLC motion_vector_vlc; /* version < 2 */
271
    VLC vp4_mv_vlc[2][7]; /* version >=2 */
272
273
    /* these arrays need to be on 16-byte boundaries since SSE2 operations
274
     * index into them */
275
    DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64];     ///< qmat[qpi][is_inter][plane]
276
277
    /* This table contains superblock_count * 16 entries. Each set of 16
278
     * numbers corresponds to the fragment indexes 0..15 of the superblock.
279
     * An entry will be -1 to indicate that no entry corresponds to that
280
     * index. */
281
    int *superblock_fragments;
282
283
    /* This is an array that indicates how a particular macroblock
284
     * is coded. */
285
    unsigned char *macroblock_coding;
286
287
    uint8_t *edge_emu_buffer;
288
289
    /* Huffman decode */
290
    int hti;
291
    unsigned int hbits;
292
    int entries;
293
    int huff_code_size;
294
    uint32_t huffman_table[80][32][2];
295
296
    uint8_t filter_limit_values[64];
297
    DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2];
298
299
    VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */
300
} Vp3DecodeContext;
301
302
/************************************************************************
303
 * VP3 specific functions
304
 ************************************************************************/
305
306
24
static av_cold void free_tables(AVCodecContext *avctx)
307
{
308
24
    Vp3DecodeContext *s = avctx->priv_data;
309
310
24
    av_freep(&s->superblock_coding);
311
24
    av_freep(&s->all_fragments);
312
24
    av_freep(&s->nkf_coded_fragment_list);
313
24
    av_freep(&s->kf_coded_fragment_list);
314
24
    av_freep(&s->dct_tokens_base);
315
24
    av_freep(&s->superblock_fragments);
316
24
    av_freep(&s->macroblock_coding);
317
24
    av_freep(&s->dc_pred_row);
318
24
    av_freep(&s->motion_val[0]);
319
24
    av_freep(&s->motion_val[1]);
320
24
}
321
322
12
static void vp3_decode_flush(AVCodecContext *avctx)
323
{
324
12
    Vp3DecodeContext *s = avctx->priv_data;
325
326
12
    if (s->golden_frame.f)
327
12
        ff_thread_release_buffer(avctx, &s->golden_frame);
328
12
    if (s->last_frame.f)
329
12
        ff_thread_release_buffer(avctx, &s->last_frame);
330
12
    if (s->current_frame.f)
331
12
        ff_thread_release_buffer(avctx, &s->current_frame);
332
12
}
333
334
12
static av_cold int vp3_decode_end(AVCodecContext *avctx)
335
{
336
12
    Vp3DecodeContext *s = avctx->priv_data;
337
    int i, j;
338
339
12
    free_tables(avctx);
340
12
    av_freep(&s->edge_emu_buffer);
341
342
12
    s->theora_tables = 0;
343
344
    /* release all frames */
345
12
    vp3_decode_flush(avctx);
346
12
    av_frame_free(&s->current_frame.f);
347
12
    av_frame_free(&s->last_frame.f);
348
12
    av_frame_free(&s->golden_frame.f);
349
350
204
    for (i = 0; i < 16; i++) {
351
192
        ff_free_vlc(&s->dc_vlc[i]);
352
192
        ff_free_vlc(&s->ac_vlc_1[i]);
353
192
        ff_free_vlc(&s->ac_vlc_2[i]);
354
192
        ff_free_vlc(&s->ac_vlc_3[i]);
355
192
        ff_free_vlc(&s->ac_vlc_4[i]);
356
    }
357
358
12
    ff_free_vlc(&s->superblock_run_length_vlc);
359
12
    ff_free_vlc(&s->fragment_run_length_vlc);
360
12
    ff_free_vlc(&s->mode_code_vlc);
361
12
    ff_free_vlc(&s->motion_vector_vlc);
362
363
36
    for (j = 0; j < 2; j++)
364
192
        for (i = 0; i < 7; i++)
365
168
            ff_free_vlc(&s->vp4_mv_vlc[j][i]);
366
367
36
    for (i = 0; i < 2; i++)
368
24
        ff_free_vlc(&s->block_pattern_vlc[i]);
369
12
    return 0;
370
}
371
372
/**
373
 * This function sets up all of the various blocks mappings:
374
 * superblocks <-> fragments, macroblocks <-> fragments,
375
 * superblocks <-> macroblocks
376
 *
377
 * @return 0 is successful; returns 1 if *anything* went wrong.
378
 */
379
12
static int init_block_mapping(Vp3DecodeContext *s)
380
{
381
    int sb_x, sb_y, plane;
382
12
    int x, y, i, j = 0;
383
384
48
    for (plane = 0; plane < 3; plane++) {
385
36
        int sb_width    = plane ? s->c_superblock_width
386
36
                                : s->y_superblock_width;
387
36
        int sb_height   = plane ? s->c_superblock_height
388
36
                                : s->y_superblock_height;
389
36
        int frag_width  = s->fragment_width[!!plane];
390
36
        int frag_height = s->fragment_height[!!plane];
391
392
551
        for (sb_y = 0; sb_y < sb_height; sb_y++)
393
26730
            for (sb_x = 0; sb_x < sb_width; sb_x++)
394
445655
                for (i = 0; i < 16; i++) {
395
419440
                    x = 4 * sb_x + hilbert_offset[i][0];
396
419440
                    y = 4 * sb_y + hilbert_offset[i][1];
397
398

419440
                    if (x < frag_width && y < frag_height)
399
404850
                        s->superblock_fragments[j++] = s->fragment_start[plane] +
400
404850
                                                       y * frag_width + x;
401
                    else
402
14590
                        s->superblock_fragments[j++] = -1;
403
                }
404
    }
405
406
12
    return 0;  /* successful path out */
407
}
408
409
/*
410
 * This function sets up the dequantization tables used for a particular
411
 * frame.
412
 */
413
62
static void init_dequantizer(Vp3DecodeContext *s, int qpi)
414
{
415
62
    int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
416
    int i, plane, inter, qri, bmi, bmj, qistart;
417
418
186
    for (inter = 0; inter < 2; inter++) {
419
496
        for (plane = 0; plane < 3; plane++) {
420
372
            int dc_scale_factor = s->coded_dc_scale_factor[!!plane][s->qps[qpi]];
421
372
            int sum = 0;
422
408
            for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
423
408
                sum += s->qr_size[inter][plane][qri];
424
408
                if (s->qps[qpi] <= sum)
425
372
                    break;
426
            }
427
372
            qistart = sum - s->qr_size[inter][plane][qri];
428
372
            bmi     = s->qr_base[inter][plane][qri];
429
372
            bmj     = s->qr_base[inter][plane][qri + 1];
430
24180
            for (i = 0; i < 64; i++) {
431
23808
                int coeff = (2 * (sum     - s->qps[qpi]) * s->base_matrix[bmi][i] -
432
23808
                             2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
433
23808
                             s->qr_size[inter][plane][qri]) /
434
23808
                            (2 * s->qr_size[inter][plane][qri]);
435
436
23808
                int qmin   = 8 << (inter + !i);
437
23808
                int qscale = i ? ac_scale_factor : dc_scale_factor;
438
23808
                int qbias = (1 + inter) * 3;
439
47616
                s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
440
23808
                    (i == 0 || s->version < 2) ? av_clip((qscale * coeff) / 100 * 4, qmin, 4096)
441
1512
                                               : (qscale * (coeff - qbias) / 100 + qbias) * 4;
442
            }
443
            /* all DC coefficients use the same quant so as not to interfere
444
             * with DC prediction */
445
372
            s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
446
        }
447
    }
448
62
}
449
450
/*
451
 * This function initializes the loop filter boundary limits if the frame's
452
 * quality index is different from the previous frame's.
453
 *
454
 * The filter_limit_values may not be larger than 127.
455
 */
456
62
static void init_loop_filter(Vp3DecodeContext *s)
457
{
458
62
    ff_vp3dsp_set_bounding_values(s->bounding_values_array, s->filter_limit_values[s->qps[0]]);
459
62
}
460
461
/*
462
 * This function unpacks all of the superblock/macroblock/fragment coding
463
 * information from the bitstream.
464
 */
465
140
static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
466
{
467
140
    int superblock_starts[3] = {
468
140
        0, s->u_superblock_start, s->v_superblock_start
469
    };
470
140
    int bit = 0;
471
140
    int current_superblock = 0;
472
140
    int current_run = 0;
473
140
    int num_partial_superblocks = 0;
474
475
    int i, j;
476
    int current_fragment;
477
    int plane;
478
140
    int plane0_num_coded_frags = 0;
479
480
140
    if (s->keyframe) {
481
4
        memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
482
    } else {
483
        /* unpack the list of partially-coded superblocks */
484
136
        bit         = get_bits1(gb) ^ 1;
485
136
        current_run = 0;
486
487

8681
        while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
488

8545
            if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
489
                bit = get_bits1(gb);
490
            else
491
8545
                bit ^= 1;
492
493
8545
            current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
494
                                   6, 2) + 1;
495
8545
            if (current_run == 34)
496
437
                current_run += get_bits(gb, 12);
497
498
8545
            if (current_run > s->superblock_count - current_superblock) {
499
                av_log(s->avctx, AV_LOG_ERROR,
500
                       "Invalid partially coded superblock run length\n");
501
                return -1;
502
            }
503
504
8545
            memset(s->superblock_coding + current_superblock, bit, current_run);
505
506
8545
            current_superblock += current_run;
507
8545
            if (bit)
508
4237
                num_partial_superblocks += current_run;
509
        }
510
511
        /* unpack the list of fully coded superblocks if any of the blocks were
512
         * not marked as partially coded in the previous step */
513
136
        if (num_partial_superblocks < s->superblock_count) {
514
136
            int superblocks_decoded = 0;
515
516
136
            current_superblock = 0;
517
136
            bit                = get_bits1(gb) ^ 1;
518
136
            current_run        = 0;
519
520

2240
            while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
521
1052
                   get_bits_left(gb) > 0) {
522

1052
                if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
523
6
                    bit = get_bits1(gb);
524
                else
525
1046
                    bit ^= 1;
526
527
1052
                current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
528
                                       6, 2) + 1;
529
1052
                if (current_run == 34)
530
218
                    current_run += get_bits(gb, 12);
531
532
105952
                for (j = 0; j < current_run; current_superblock++) {
533
104900
                    if (current_superblock >= s->superblock_count) {
534
                        av_log(s->avctx, AV_LOG_ERROR,
535
                               "Invalid fully coded superblock run length\n");
536
                        return -1;
537
                    }
538
539
                    /* skip any superblocks already marked as partially coded */
540
104900
                    if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
541
88554
                        s->superblock_coding[current_superblock] = 2 * bit;
542
88554
                        j++;
543
                    }
544
                }
545
1052
                superblocks_decoded += current_run;
546
            }
547
        }
548
549
        /* if there were partial blocks, initialize bitstream for
550
         * unpacking fragment codings */
551
136
        if (num_partial_superblocks) {
552
128
            current_run = 0;
553
128
            bit         = get_bits1(gb);
554
            /* toggle the bit because as soon as the first run length is
555
             * fetched the bit will be toggled again */
556
128
            bit ^= 1;
557
        }
558
    }
559
560
    /* figure out which fragments are coded; iterate through each
561
     * superblock (all planes) */
562
140
    s->total_num_coded_frags = 0;
563
140
    memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
564
565
280
    s->coded_fragment_list[0] = s->keyframe ? s->kf_coded_fragment_list
566
140
                                            : s->nkf_coded_fragment_list;
567
568
560
    for (plane = 0; plane < 3; plane++) {
569
420
        int sb_start = superblock_starts[plane];
570
420
        int sb_end   = sb_start + (plane ? s->c_superblock_count
571
420
                                         : s->y_superblock_count);
572
420
        int num_coded_frags = 0;
573
574
420
        if (s->keyframe) {
575
12
            if (s->num_kf_coded_fragment[plane] == -1) {
576
10538
                for (i = sb_start; i < sb_end; i++) {
577
                    /* iterate through all 16 fragments in a superblock */
578
178942
                    for (j = 0; j < 16; j++) {
579
                        /* if the fragment is in bounds, check its coding status */
580
168416
                        current_fragment = s->superblock_fragments[i * 16 + j];
581
168416
                        if (current_fragment != -1) {
582
162492
                            s->coded_fragment_list[plane][num_coded_frags++] =
583
                                current_fragment;
584
                        }
585
                    }
586
                }
587
12
                s->num_kf_coded_fragment[plane] = num_coded_frags;
588
            } else
589
                num_coded_frags = s->num_kf_coded_fragment[plane];
590
        } else {
591

105594
            for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
592
105186
                if (get_bits_left(gb) < plane0_num_coded_frags >> 2) {
593
                    return AVERROR_INVALIDDATA;
594
                }
595
                /* iterate through all 16 fragments in a superblock */
596
1788162
                for (j = 0; j < 16; j++) {
597
                    /* if the fragment is in bounds, check its coding status */
598
1682976
                    current_fragment = s->superblock_fragments[i * 16 + j];
599
1682976
                    if (current_fragment != -1) {
600
1598148
                        int coded = s->superblock_coding[i];
601
602
1598148
                        if (coded == SB_PARTIALLY_CODED) {
603
                            /* fragment may or may not be coded; this is the case
604
                             * that cares about the fragment coding runs */
605
241844
                            if (current_run-- == 0) {
606
70601
                                bit        ^= 1;
607
70601
                                current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);
608
                            }
609
241844
                            coded = bit;
610
                        }
611
612
1598148
                        if (coded) {
613
                            /* default mode; actual mode will be decoded in
614
                             * the next phase */
615
370084
                            s->all_fragments[current_fragment].coding_method =
616
                                MODE_INTER_NO_MV;
617
370084
                            s->coded_fragment_list[plane][num_coded_frags++] =
618
                                current_fragment;
619
                        } else {
620
                            /* not coded; copy this fragment from the prior frame */
621
1228064
                            s->all_fragments[current_fragment].coding_method =
622
                                MODE_COPY;
623
                        }
624
                    }
625
                }
626
            }
627
        }
628
420
        if (!plane)
629
140
            plane0_num_coded_frags = num_coded_frags;
630
420
        s->total_num_coded_frags += num_coded_frags;
631
27300
        for (i = 0; i < 64; i++)
632
26880
            s->num_coded_frags[plane][i] = num_coded_frags;
633
420
        if (plane < 2)
634
280
            s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
635
                                                num_coded_frags;
636
    }
637
140
    return 0;
638
}
639
640
#define BLOCK_X (2 * mb_x + (k & 1))
641
#define BLOCK_Y (2 * mb_y + (k >> 1))
642
643
#if CONFIG_VP4_DECODER
644
/**
645
 * @return number of blocks, or > yuv_macroblock_count on error.
646
 *         return value is always >= 1.
647
 */
648
7007
static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb)
649
{
650
7007
    int v = 1;
651
    int bits;
652
7007
    while ((bits = show_bits(gb, 9)) == 0x1ff) {
653
        skip_bits(gb, 9);
654
        v += 256;
655
        if (v > s->yuv_macroblock_count) {
656
            av_log(s->avctx, AV_LOG_ERROR, "Invalid run length\n");
657
            return v;
658
        }
659
    }
660
#define body(n) { \
661
    skip_bits(gb, 2 + n); \
662
    v += (1 << n) + get_bits(gb, n); }
663
#define thresh(n) (0x200 - (0x80 >> n))
664
#define else_if(n) else if (bits < thresh(n)) body(n)
665
7007
    if (bits < 0x100) {
666
2784
        skip_bits(gb, 1);
667
4223
    } else if (bits < thresh(0)) {
668
1763
        skip_bits(gb, 2);
669
1763
        v += 1;
670
    }
671
2460
    else_if(1)
672
1466
    else_if(2)
673
799
    else_if(3)
674
316
    else_if(4)
675
92
    else_if(5)
676
4
    else_if(6)
677
    else body(7)
678
#undef body
679
#undef thresh
680
#undef else_if
681
7007
    return v;
682
}
683
684
4556
static int vp4_get_block_pattern(Vp3DecodeContext *s, GetBitContext *gb, int *next_block_pattern_table)
685
{
686
4556
    int v = get_vlc2(gb, s->block_pattern_vlc[*next_block_pattern_table].table, 3, 2);
687
4556
    if (v == -1) {
688
        av_log(s->avctx, AV_LOG_ERROR, "Invalid block pattern\n");
689
        *next_block_pattern_table = 0;
690
        return 0;
691
    }
692
4556
    *next_block_pattern_table = vp4_block_pattern_table_selector[v];
693
4556
    return v + 1;
694
}
695
696
24
static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb)
697
{
698
    int plane, i, j, k, fragment;
699
    int next_block_pattern_table;
700
    int bit, current_run, has_partial;
701
702
24
    memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
703
704
24
    if (s->keyframe)
705
2
        return 0;
706
707
22
    has_partial = 0;
708
22
    bit         = get_bits1(gb);
709
4154
    for (i = 0; i < s->yuv_macroblock_count; i += current_run) {
710
4132
        if (get_bits_left(gb) <= 0)
711
            return AVERROR_INVALIDDATA;
712
4132
        current_run = vp4_get_mb_count(s, gb);
713
4132
        if (current_run > s->yuv_macroblock_count - i)
714
            return -1;
715
4132
        memset(s->superblock_coding + i, 2 * bit, current_run);
716
4132
        bit ^= 1;
717
4132
        has_partial |= bit;
718
    }
719
720
22
    if (has_partial) {
721
22
        if (get_bits_left(gb) <= 0)
722
            return AVERROR_INVALIDDATA;
723
22
        bit  = get_bits1(gb);
724
22
        current_run = vp4_get_mb_count(s, gb);
725
20086
        for (i = 0; i < s->yuv_macroblock_count; i++) {
726
20064
            if (!s->superblock_coding[i]) {
727
8403
                if (!current_run) {
728
2853
                    bit ^= 1;
729
2853
                    current_run = vp4_get_mb_count(s, gb);
730
                }
731
8403
                s->superblock_coding[i] = bit;
732
8403
                current_run--;
733
            }
734
        }
735
22
        if (current_run) /* handle situation when vp4_get_mb_count() fails */
736
            return -1;
737
    }
738
739
22
    next_block_pattern_table = 0;
740
22
    i = 0;
741
88
    for (plane = 0; plane < 3; plane++) {
742
        int sb_x, sb_y;
743
66
        int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
744
66
        int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
745
66
        int mb_width = plane ? s->c_macroblock_width : s->macroblock_width;
746
66
        int mb_height = plane ? s->c_macroblock_height : s->macroblock_height;
747
66
        int fragment_width = s->fragment_width[!!plane];
748
66
        int fragment_height = s->fragment_height[!!plane];
749
750
418
        for (sb_y = 0; sb_y < sb_height; sb_y++) {
751
5456
            for (sb_x = 0; sb_x < sb_width; sb_x++) {
752
25520
                for (j = 0; j < 4; j++) {
753
20416
                    int mb_x = 2 * sb_x + (j >> 1);
754
20416
                    int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1);
755
                    int mb_coded, pattern, coded;
756
757

20416
                    if (mb_x >= mb_width || mb_y >= mb_height)
758
352
                        continue;
759
760
20064
                    mb_coded = s->superblock_coding[i++];
761
762
20064
                    if (mb_coded == SB_FULLY_CODED)
763
11661
                        pattern = 0xF;
764
8403
                    else if (mb_coded == SB_PARTIALLY_CODED)
765
4556
                        pattern = vp4_get_block_pattern(s, gb, &next_block_pattern_table);
766
                    else
767
3847
                        pattern = 0;
768
769
100320
                    for (k = 0; k < 4; k++) {
770

80256
                        if (BLOCK_X >= fragment_width || BLOCK_Y >= fragment_height)
771
                            continue;
772
80256
                        fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X;
773
80256
                        coded = pattern & (8 >> k);
774
                        /* MODE_INTER_NO_MV is the default for coded fragments.
775
                           the actual method is decoded in the next phase. */
776
80256
                        s->all_fragments[fragment].coding_method = coded ? MODE_INTER_NO_MV : MODE_COPY;
777
                    }
778
                }
779
            }
780
        }
781
    }
782
22
    return 0;
783
}
784
#endif
785
786
/*
787
 * This function unpacks all the coding mode data for individual macroblocks
788
 * from the bitstream.
789
 */
790
164
static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
791
{
792
    int i, j, k, sb_x, sb_y;
793
    int scheme;
794
    int current_macroblock;
795
    int current_fragment;
796
    int coding_mode;
797
    int custom_mode_alphabet[CODING_MODE_COUNT];
798
    const int *alphabet;
799
    Vp3Fragment *frag;
800
801
164
    if (s->keyframe) {
802
169794
        for (i = 0; i < s->fragment_count; i++)
803
169788
            s->all_fragments[i].coding_method = MODE_INTRA;
804
    } else {
805
        /* fetch the mode coding scheme for this frame */
806
158
        scheme = get_bits(gb, 3);
807
808
        /* is it a custom coding scheme? */
809
158
        if (scheme == 0) {
810
207
            for (i = 0; i < 8; i++)
811
184
                custom_mode_alphabet[i] = MODE_INTER_NO_MV;
812
207
            for (i = 0; i < 8; i++)
813
184
                custom_mode_alphabet[get_bits(gb, 3)] = i;
814
23
            alphabet = custom_mode_alphabet;
815
        } else
816
135
            alphabet = ModeAlphabet[scheme - 1];
817
818
        /* iterate through all of the macroblocks that contain 1 or more
819
         * coded fragments */
820
2038
        for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
821
73930
            for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
822
72050
                if (get_bits_left(gb) <= 0)
823
                    return -1;
824
825
360250
                for (j = 0; j < 4; j++) {
826
288200
                    int mb_x = 2 * sb_x + (j >> 1);
827
288200
                    int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
828
288200
                    current_macroblock = mb_y * s->macroblock_width + mb_x;
829
830
288200
                    if (mb_x >= s->macroblock_width ||
831
287276
                        mb_y >= s->macroblock_height)
832
8466
                        continue;
833
834
                    /* coding modes are only stored if the macroblock has
835
                     * at least one luma block coded, otherwise it must be
836
                     * INTER_NO_MV */
837
1073976
                    for (k = 0; k < 4; k++) {
838
878591
                        current_fragment = BLOCK_Y *
839
878591
                                           s->fragment_width[0] + BLOCK_X;
840
878591
                        if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
841
84349
                            break;
842
                    }
843
279734
                    if (k == 4) {
844
195385
                        s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
845
195385
                        continue;
846
                    }
847
848
                    /* mode 7 means get 3 bits for each coding mode */
849
84349
                    if (scheme == 7)
850
2750
                        coding_mode = get_bits(gb, 3);
851
                    else
852
81599
                        coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
853
854
84349
                    s->macroblock_coding[current_macroblock] = coding_mode;
855
421745
                    for (k = 0; k < 4; k++) {
856
337396
                        frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
857
337396
                        if (frag->coding_method != MODE_COPY)
858
307251
                            frag->coding_method = coding_mode;
859
                    }
860
861
#define SET_CHROMA_MODES                                                      \
862
    if (frag[s->fragment_start[1]].coding_method != MODE_COPY)                \
863
        frag[s->fragment_start[1]].coding_method = coding_mode;               \
864
    if (frag[s->fragment_start[2]].coding_method != MODE_COPY)                \
865
        frag[s->fragment_start[2]].coding_method = coding_mode;
866
867
84349
                    if (s->chroma_y_shift) {
868
84349
                        frag = s->all_fragments + mb_y *
869
84349
                               s->fragment_width[1] + mb_x;
870

84349
                        SET_CHROMA_MODES
871
                    } else if (s->chroma_x_shift) {
872
                        frag = s->all_fragments +
873
                               2 * mb_y * s->fragment_width[1] + mb_x;
874
                        for (k = 0; k < 2; k++) {
875
                            SET_CHROMA_MODES
876
                            frag += s->fragment_width[1];
877
                        }
878
                    } else {
879
                        for (k = 0; k < 4; k++) {
880
                            frag = s->all_fragments +
881
                                   BLOCK_Y * s->fragment_width[1] + BLOCK_X;
882
                            SET_CHROMA_MODES
883
                        }
884
                    }
885
                }
886
            }
887
        }
888
    }
889
890
164
    return 0;
891
}
892
893
3758
static int vp4_get_mv(Vp3DecodeContext *s, GetBitContext *gb, int axis, int last_motion)
894
{
895
3758
    int v = get_vlc2(gb, s->vp4_mv_vlc[axis][vp4_mv_table_selector[FFABS(last_motion)]].table, 6, 2) - 31;
896
3758
    return last_motion < 0 ? -v : v;
897
}
898
899
/*
900
 * This function unpacks all the motion vectors for the individual
901
 * macroblocks from the bitstream.
902
 */
903
164
static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
904
{
905
    int j, k, sb_x, sb_y;
906
    int coding_mode;
907
    int motion_x[4];
908
    int motion_y[4];
909
164
    int last_motion_x = 0;
910
164
    int last_motion_y = 0;
911
164
    int prior_last_motion_x = 0;
912
164
    int prior_last_motion_y = 0;
913
164
    int last_gold_motion_x = 0;
914
164
    int last_gold_motion_y = 0;
915
    int current_macroblock;
916
    int current_fragment;
917
    int frag;
918
919
164
    if (s->keyframe)
920
6
        return 0;
921
922
    /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme; 2 is VP4 code scheme */
923
158
    coding_mode = s->version < 2 ? get_bits1(gb) : 2;
924
925
    /* iterate through all of the macroblocks that contain 1 or more
926
     * coded fragments */
927
2038
    for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
928
73930
        for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
929
72050
            if (get_bits_left(gb) <= 0)
930
                return -1;
931
932
360250
            for (j = 0; j < 4; j++) {
933
288200
                int mb_x = 2 * sb_x + (j >> 1);
934
288200
                int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
935
288200
                current_macroblock = mb_y * s->macroblock_width + mb_x;
936
937
288200
                if (mb_x >= s->macroblock_width  ||
938
287276
                    mb_y >= s->macroblock_height ||
939
279734
                    s->macroblock_coding[current_macroblock] == MODE_COPY)
940
8466
                    continue;
941
942

279734
                switch (s->macroblock_coding[current_macroblock]) {
943
711
                case MODE_GOLDEN_MV:
944
711
                    if (coding_mode == 2) { /* VP4 */
945
337
                        last_gold_motion_x = motion_x[0] = vp4_get_mv(s, gb, 0, last_gold_motion_x);
946
337
                        last_gold_motion_y = motion_y[0] = vp4_get_mv(s, gb, 1, last_gold_motion_y);
947
337
                        break;
948
                    } /* otherwise fall through */
949
                case MODE_INTER_PLUS_MV:
950
                    /* all 6 fragments use the same motion vector */
951
14558
                    if (coding_mode == 0) {
952
13074
                        motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
953
13074
                        motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
954
1484
                    } else if (coding_mode == 1) {
955
42
                        motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
956
42
                        motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
957
                    } else { /* VP4 */
958
1442
                        motion_x[0] = vp4_get_mv(s, gb, 0, last_motion_x);
959
1442
                        motion_y[0] = vp4_get_mv(s, gb, 1, last_motion_y);
960
                    }
961
962
                    /* vector maintenance, only on MODE_INTER_PLUS_MV */
963
14558
                    if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
964
14184
                        prior_last_motion_x = last_motion_x;
965
14184
                        prior_last_motion_y = last_motion_y;
966
14184
                        last_motion_x       = motion_x[0];
967
14184
                        last_motion_y       = motion_y[0];
968
                    }
969
14558
                    break;
970
971
63
                case MODE_INTER_FOURMV:
972
                    /* vector maintenance */
973
63
                    prior_last_motion_x = last_motion_x;
974
63
                    prior_last_motion_y = last_motion_y;
975
976
                    /* fetch 4 vectors from the bitstream, one for each
977
                     * Y fragment, then average for the C fragment vectors */
978
315
                    for (k = 0; k < 4; k++) {
979
252
                        current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
980
252
                        if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
981
252
                            if (coding_mode == 0) {
982
48
                                motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
983
48
                                motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
984
204
                            } else if (coding_mode == 1) {
985
104
                                motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
986
104
                                motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
987
                            } else { /* VP4 */
988
100
                                motion_x[k] = vp4_get_mv(s, gb, 0, prior_last_motion_x);
989
100
                                motion_y[k] = vp4_get_mv(s, gb, 1, prior_last_motion_y);
990
                            }
991
252
                            last_motion_x = motion_x[k];
992
252
                            last_motion_y = motion_y[k];
993
                        } else {
994
                            motion_x[k] = 0;
995
                            motion_y[k] = 0;
996
                        }
997
                    }
998
63
                    break;
999
1000
42307
                case MODE_INTER_LAST_MV:
1001
                    /* all 6 fragments use the last motion vector */
1002
42307
                    motion_x[0] = last_motion_x;
1003
42307
                    motion_y[0] = last_motion_y;
1004
1005
                    /* no vector maintenance (last vector remains the
1006
                     * last vector) */
1007
42307
                    break;
1008
1009
11934
                case MODE_INTER_PRIOR_LAST:
1010
                    /* all 6 fragments use the motion vector prior to the
1011
                     * last motion vector */
1012
11934
                    motion_x[0] = prior_last_motion_x;
1013
11934
                    motion_y[0] = prior_last_motion_y;
1014
1015
                    /* vector maintenance */
1016
11934
                    prior_last_motion_x = last_motion_x;
1017
11934
                    prior_last_motion_y = last_motion_y;
1018
11934
                    last_motion_x       = motion_x[0];
1019
11934
                    last_motion_y       = motion_y[0];
1020
11934
                    break;
1021
1022
210535
                default:
1023
                    /* covers intra, inter without MV, golden without MV */
1024
210535
                    motion_x[0] = 0;
1025
210535
                    motion_y[0] = 0;
1026
1027
                    /* no vector maintenance */
1028
210535
                    break;
1029
                }
1030
1031
                /* assign the motion vectors to the correct fragments */
1032
1398670
                for (k = 0; k < 4; k++) {
1033
1118936
                    current_fragment =
1034
1118936
                        BLOCK_Y * s->fragment_width[0] + BLOCK_X;
1035
1118936
                    if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1036
252
                        s->motion_val[0][current_fragment][0] = motion_x[k];
1037
252
                        s->motion_val[0][current_fragment][1] = motion_y[k];
1038
                    } else {
1039
1118684
                        s->motion_val[0][current_fragment][0] = motion_x[0];
1040
1118684
                        s->motion_val[0][current_fragment][1] = motion_y[0];
1041
                    }
1042
                }
1043
1044
279734
                if (s->chroma_y_shift) {
1045
279734
                    if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1046
63
                        motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
1047
                                             motion_x[2] + motion_x[3], 2);
1048
63
                        motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
1049
                                             motion_y[2] + motion_y[3], 2);
1050
                    }
1051
279734
                    if (s->version <= 2) {
1052
266358
                        motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1053
266358
                        motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
1054
                    }
1055
279734
                    frag = mb_y * s->fragment_width[1] + mb_x;
1056
279734
                    s->motion_val[1][frag][0] = motion_x[0];
1057
279734
                    s->motion_val[1][frag][1] = motion_y[0];
1058
                } else if (s->chroma_x_shift) {
1059
                    if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1060
                        motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
1061
                        motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
1062
                        motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
1063
                        motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
1064
                    } else {
1065
                        motion_x[1] = motion_x[0];
1066
                        motion_y[1] = motion_y[0];
1067
                    }
1068
                    if (s->version <= 2) {
1069
                        motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1070
                        motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
1071
                    }
1072
                    frag = 2 * mb_y * s->fragment_width[1] + mb_x;
1073
                    for (k = 0; k < 2; k++) {
1074
                        s->motion_val[1][frag][0] = motion_x[k];
1075
                        s->motion_val[1][frag][1] = motion_y[k];
1076
                        frag += s->fragment_width[1];
1077
                    }
1078
                } else {
1079
                    for (k = 0; k < 4; k++) {
1080
                        frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
1081
                        if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1082
                            s->motion_val[1][frag][0] = motion_x[k];
1083
                            s->motion_val[1][frag][1] = motion_y[k];
1084
                        } else {
1085
                            s->motion_val[1][frag][0] = motion_x[0];
1086
                            s->motion_val[1][frag][1] = motion_y[0];
1087
                        }
1088
                    }
1089
                }
1090
            }
1091
        }
1092
    }
1093
1094
158
    return 0;
1095
}
1096
1097
164
static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
1098
{
1099
    int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
1100
164
    int num_blocks = s->total_num_coded_frags;
1101
1102

164
    for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
1103
        i = blocks_decoded = num_blocks_at_qpi = 0;
1104
1105
        bit        = get_bits1(gb) ^ 1;
1106
        run_length = 0;
1107
1108
        do {
1109
            if (run_length == MAXIMUM_LONG_BIT_RUN)
1110
                bit = get_bits1(gb);
1111
            else
1112
                bit ^= 1;
1113
1114
            run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
1115
            if (run_length == 34)
1116
                run_length += get_bits(gb, 12);
1117
            blocks_decoded += run_length;
1118
1119
            if (!bit)
1120
                num_blocks_at_qpi += run_length;
1121
1122
            for (j = 0; j < run_length; i++) {
1123
                if (i >= s->total_num_coded_frags)
1124
                    return -1;
1125
1126
                if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
1127
                    s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
1128
                    j++;
1129
                }
1130
            }
1131
        } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
1132
1133
        num_blocks -= num_blocks_at_qpi;
1134
    }
1135
1136
164
    return 0;
1137
}
1138
1139
259052
static inline int get_eob_run(GetBitContext *gb, int token)
1140
{
1141
259052
    int v = eob_run_table[token].base;
1142
259052
    if (eob_run_table[token].bits)
1143
33652
        v += get_bits(gb, eob_run_table[token].bits);
1144
259052
    return v;
1145
}
1146
1147
2755489
static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
1148
{
1149
    int bits_to_get, zero_run;
1150
1151
2755489
    bits_to_get = coeff_get_bits[token];
1152
2755489
    if (bits_to_get)
1153
1617513
        bits_to_get = get_bits(gb, bits_to_get);
1154
2755489
    *coeff = coeff_tables[token][bits_to_get];
1155
1156
2755489
    zero_run = zero_run_base[token];
1157
2755489
    if (zero_run_get_bits[token])
1158
342682
        zero_run += get_bits(gb, zero_run_get_bits[token]);
1159
1160
2755489
    return zero_run;
1161
}
1162
1163
/*
1164
 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1165
 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1166
 * data. This function unpacks all the VLCs for either the Y plane or both
1167
 * C planes, and is called for DC coefficients or different AC coefficient
1168
 * levels (since different coefficient types require different VLC tables.
1169
 *
1170
 * This function returns a residual eob run. E.g, if a particular token gave
1171
 * instructions to EOB the next 5 fragments and there were only 2 fragments
1172
 * left in the current fragment range, 3 would be returned so that it could
1173
 * be passed into the next call to this same function.
1174
 */
1175
26880
static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1176
                       VLC *table, int coeff_index,
1177
                       int plane,
1178
                       int eob_run)
1179
{
1180
26880
    int i, j = 0;
1181
    int token;
1182
26880
    int zero_run  = 0;
1183
26880
    int16_t coeff = 0;
1184
    int blocks_ended;
1185
26880
    int coeff_i = 0;
1186
26880
    int num_coeffs      = s->num_coded_frags[plane][coeff_index];
1187
26880
    int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
1188
1189
    /* local references to structure members to avoid repeated dereferences */
1190
26880
    int *coded_fragment_list   = s->coded_fragment_list[plane];
1191
26880
    Vp3Fragment *all_fragments = s->all_fragments;
1192
26880
    VLC_TYPE(*vlc_table)[2] = table->table;
1193
1194
26880
    if (num_coeffs < 0) {
1195
        av_log(s->avctx, AV_LOG_ERROR,
1196
               "Invalid number of coefficients at level %d\n", coeff_index);
1197
        return AVERROR_INVALIDDATA;
1198
    }
1199
1200
26880
    if (eob_run > num_coeffs) {
1201
3487
        coeff_i      =
1202
3487
        blocks_ended = num_coeffs;
1203
3487
        eob_run     -= num_coeffs;
1204
    } else {
1205
23393
        coeff_i      =
1206
23393
        blocks_ended = eob_run;
1207
23393
        eob_run      = 0;
1208
    }
1209
1210
    // insert fake EOB token to cover the split between planes or zzi
1211
26880
    if (blocks_ended)
1212
2019
        dct_tokens[j++] = blocks_ended << 2;
1213
1214

2087120
    while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
1215
        /* decode a VLC into a token */
1216
2060240
        token = get_vlc2(gb, vlc_table, 11, 3);
1217
        /* use the token to get a zero run, a coefficient, and an eob run */
1218
2060240
        if ((unsigned) token <= 6U) {
1219
220061
            eob_run = get_eob_run(gb, token);
1220
220061
            if (!eob_run)
1221
                eob_run = INT_MAX;
1222
1223
            // record only the number of blocks ended in this plane,
1224
            // any spill will be recorded in the next plane.
1225
220061
            if (eob_run > num_coeffs - coeff_i) {
1226
1656
                dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
1227
1656
                blocks_ended   += num_coeffs - coeff_i;
1228
1656
                eob_run        -= num_coeffs - coeff_i;
1229
1656
                coeff_i         = num_coeffs;
1230
            } else {
1231
218405
                dct_tokens[j++] = TOKEN_EOB(eob_run);
1232
218405
                blocks_ended   += eob_run;
1233
218405
                coeff_i        += eob_run;
1234
218405
                eob_run         = 0;
1235
            }
1236
1840179
        } else if (token >= 0) {
1237
1840179
            zero_run = get_coeff(gb, token, &coeff);
1238
1239
1840179
            if (zero_run) {
1240
601916
                dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1241
            } else {
1242
                // Save DC into the fragment structure. DC prediction is
1243
                // done in raster order, so the actual DC can't be in with
1244
                // other tokens. We still need the token in dct_tokens[]
1245
                // however, or else the structure collapses on itself.
1246
1238263
                if (!coeff_index)
1247
241967
                    all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1248
1249
1238263
                dct_tokens[j++] = TOKEN_COEFF(coeff);
1250
            }
1251
1252
1840179
            if (coeff_index + zero_run > 64) {
1253
                av_log(s->avctx, AV_LOG_DEBUG,
1254
                       "Invalid zero run of %d with %d coeffs left\n",
1255
                       zero_run, 64 - coeff_index);
1256
                zero_run = 64 - coeff_index;
1257
            }
1258
1259
            // zero runs code multiple coefficients,
1260
            // so don't try to decode coeffs for those higher levels
1261
4381268
            for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1262
2541089
                s->num_coded_frags[plane][i]--;
1263
1840179
            coeff_i++;
1264
        } else {
1265
            av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1266
            return -1;
1267
        }
1268
    }
1269
1270
26880
    if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1271
        av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1272
1273
    // decrement the number of blocks that have higher coefficients for each
1274
    // EOB run at this level
1275
26880
    if (blocks_ended)
1276
381061
        for (i = coeff_index + 1; i < 64; i++)
1277
372769
            s->num_coded_frags[plane][i] -= blocks_ended;
1278
1279
    // setup the next buffer
1280
26880
    if (plane < 2)
1281
17920
        s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1282
8960
    else if (coeff_index < 63)
1283
8820
        s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1284
1285
26880
    return eob_run;
1286
}
1287
1288
static void reverse_dc_prediction(Vp3DecodeContext *s,
1289
                                  int first_fragment,
1290
                                  int fragment_width,
1291
                                  int fragment_height);
1292
/*
1293
 * This function unpacks all of the DCT coefficient data from the
1294
 * bitstream.
1295
 */
1296
140
static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1297
{
1298
    int i;
1299
    int dc_y_table;
1300
    int dc_c_table;
1301
    int ac_y_table;
1302
    int ac_c_table;
1303
140
    int residual_eob_run = 0;
1304
    VLC *y_tables[64];
1305
    VLC *c_tables[64];
1306
1307
140
    s->dct_tokens[0][0] = s->dct_tokens_base;
1308
1309
140
    if (get_bits_left(gb) < 16)
1310
        return AVERROR_INVALIDDATA;
1311
1312
    /* fetch the DC table indexes */
1313
140
    dc_y_table = get_bits(gb, 4);
1314
140
    dc_c_table = get_bits(gb, 4);
1315
1316
    /* unpack the Y plane DC coefficients */
1317
140
    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1318
                                   0, residual_eob_run);
1319
140
    if (residual_eob_run < 0)
1320
        return residual_eob_run;
1321
140
    if (get_bits_left(gb) < 8)
1322
        return AVERROR_INVALIDDATA;
1323
1324
    /* reverse prediction of the Y-plane DC coefficients */
1325
140
    reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1326
1327
    /* unpack the C plane DC coefficients */
1328
140
    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1329
                                   1, residual_eob_run);
1330
140
    if (residual_eob_run < 0)
1331
        return residual_eob_run;
1332
140
    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1333
                                   2, residual_eob_run);
1334
140
    if (residual_eob_run < 0)
1335
        return residual_eob_run;
1336
1337
    /* reverse prediction of the C-plane DC coefficients */
1338
140
    if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1339
140
        reverse_dc_prediction(s, s->fragment_start[1],
1340
                              s->fragment_width[1], s->fragment_height[1]);
1341
140
        reverse_dc_prediction(s, s->fragment_start[2],
1342
                              s->fragment_width[1], s->fragment_height[1]);
1343
    }
1344
1345
140
    if (get_bits_left(gb) < 8)
1346
        return AVERROR_INVALIDDATA;
1347
    /* fetch the AC table indexes */
1348
140
    ac_y_table = get_bits(gb, 4);
1349
140
    ac_c_table = get_bits(gb, 4);
1350
1351
    /* build tables of AC VLC tables */
1352
840
    for (i = 1; i <= 5; i++) {
1353
700
        y_tables[i] = &s->ac_vlc_1[ac_y_table];
1354
700
        c_tables[i] = &s->ac_vlc_1[ac_c_table];
1355
    }
1356
1400
    for (i = 6; i <= 14; i++) {
1357
1260
        y_tables[i] = &s->ac_vlc_2[ac_y_table];
1358
1260
        c_tables[i] = &s->ac_vlc_2[ac_c_table];
1359
    }
1360
1960
    for (i = 15; i <= 27; i++) {
1361
1820
        y_tables[i] = &s->ac_vlc_3[ac_y_table];
1362
1820
        c_tables[i] = &s->ac_vlc_3[ac_c_table];
1363
    }
1364
5180
    for (i = 28; i <= 63; i++) {
1365
5040
        y_tables[i] = &s->ac_vlc_4[ac_y_table];
1366
5040
        c_tables[i] = &s->ac_vlc_4[ac_c_table];
1367
    }
1368
1369
    /* decode all AC coefficients */
1370
8960
    for (i = 1; i <= 63; i++) {
1371
8820
        residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1372
                                       0, residual_eob_run);
1373
8820
        if (residual_eob_run < 0)
1374
            return residual_eob_run;
1375
1376
8820
        residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1377
                                       1, residual_eob_run);
1378
8820
        if (residual_eob_run < 0)
1379
            return residual_eob_run;
1380
8820
        residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1381
                                       2, residual_eob_run);
1382
8820
        if (residual_eob_run < 0)
1383
            return residual_eob_run;
1384
    }
1385
1386
140
    return 0;
1387
}
1388
1389
#if CONFIG_VP4_DECODER
1390
/**
1391
 * eob_tracker[] is instead of TOKEN_EOB(value)
1392
 * a dummy TOKEN_EOB(0) value is used to make vp3_dequant work
1393
 *
1394
 * @return < 0 on error
1395
 */
1396
63252
static int vp4_unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1397
                       VLC *vlc_tables[64],
1398
                       int plane, int eob_tracker[64], int fragment)
1399
{
1400
    int token;
1401
63252
    int zero_run  = 0;
1402
63252
    int16_t coeff = 0;
1403
63252
    int coeff_i = 0;
1404
    int eob_run;
1405
1406
973472
    while (!eob_tracker[coeff_i]) {
1407
954301
        if (get_bits_left(gb) < 1)
1408
            return AVERROR_INVALIDDATA;
1409
1410
954301
        token = get_vlc2(gb, vlc_tables[coeff_i]->table, 11, 3);
1411
1412
        /* use the token to get a zero run, a coefficient, and an eob run */
1413
954301
        if ((unsigned) token <= 6U) {
1414
38991
            eob_run = get_eob_run(gb, token);
1415
38991
            *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1416
38991
            eob_tracker[coeff_i] = eob_run - 1;
1417
38991
            return 0;
1418
915310
        } else if (token >= 0) {
1419
915310
            zero_run = get_coeff(gb, token, &coeff);
1420
1421
915310
            if (zero_run) {
1422
311952
                if (coeff_i + zero_run > 64) {
1423
                    av_log(s->avctx, AV_LOG_DEBUG,
1424
                        "Invalid zero run of %d with %d coeffs left\n",
1425
                        zero_run, 64 - coeff_i);
1426
                    zero_run = 64 - coeff_i;
1427
                }
1428
311952
                *s->dct_tokens[plane][coeff_i]++ = TOKEN_ZERO_RUN(coeff, zero_run);
1429
311952
                coeff_i += zero_run;
1430
            } else {
1431
603358
                if (!coeff_i)
1432
40795
                    s->all_fragments[fragment].dc = coeff;
1433
1434
603358
                *s->dct_tokens[plane][coeff_i]++ = TOKEN_COEFF(coeff);
1435
            }
1436
915310
            coeff_i++;
1437
915310
            if (coeff_i >= 64) /* > 64 occurs when there is a zero_run overflow */
1438
5090
                return 0; /* stop */
1439
        } else {
1440
            av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1441
            return -1;
1442
        }
1443
    }
1444
19171
    *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1445
19171
    eob_tracker[coeff_i]--;
1446
19171
    return 0;
1447
}
1448
1449
95328
static void vp4_dc_predictor_reset(VP4Predictor *p)
1450
{
1451
95328
    p->dc = 0;
1452
95328
    p->type = VP4_DC_UNDEFINED;
1453
95328
}
1454
1455
5568
static void vp4_dc_pred_before(const Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1456
{
1457
    int i, j;
1458
1459
27840
    for (i = 0; i < 4; i++)
1460
22272
        dc_pred[0][i + 1] = s->dc_pred_row[sb_x * 4 + i];
1461
1462
27840
    for (j = 1; j < 5; j++)
1463
111360
        for (i = 0; i < 4; i++)
1464
89088
            vp4_dc_predictor_reset(&dc_pred[j][i + 1]);
1465
5568
}
1466
1467
5568
static void vp4_dc_pred_after(Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1468
{
1469
    int i;
1470
1471
27840
    for (i = 0; i < 4; i++)
1472
22272
        s->dc_pred_row[sb_x * 4 + i] = dc_pred[4][i + 1];
1473
1474
27840
    for (i = 1; i < 5; i++)
1475
22272
        dc_pred[i][0] = dc_pred[i][4];
1476
5568
}
1477
1478
/* note: dc_pred points to the current block */
1479
63252
static int vp4_dc_pred(const Vp3DecodeContext *s, const VP4Predictor * dc_pred, const int * last_dc, int type, int plane)
1480
{
1481
63252
    int count = 0;
1482
63252
    int dc = 0;
1483
1484
63252
    if (dc_pred[-6].type == type) {
1485
29726
        dc += dc_pred[-6].dc;
1486
29726
        count++;
1487
    }
1488
1489
63252
    if (dc_pred[6].type == type) {
1490
19581
        dc += dc_pred[6].dc;
1491
19581
        count++;
1492
    }
1493
1494

63252
    if (count != 2 && dc_pred[-1].type == type) {
1495
38121
        dc += dc_pred[-1].dc;
1496
38121
        count++;
1497
    }
1498
1499

63252
    if (count != 2 && dc_pred[1].type == type) {
1500
2073
        dc += dc_pred[1].dc;
1501
2073
        count++;
1502
    }
1503
1504
    /* using division instead of shift to correctly handle negative values */
1505
63252
    return count == 2 ? dc / 2 : last_dc[type];
1506
}
1507
1508
48
static void vp4_set_tokens_base(Vp3DecodeContext *s)
1509
{
1510
    int plane, i;
1511
48
    int16_t *base = s->dct_tokens_base;
1512
192
    for (plane = 0; plane < 3; plane++) {
1513
9360
        for (i = 0; i < 64; i++) {
1514
9216
            s->dct_tokens[plane][i] = base;
1515
9216
            base += s->fragment_width[!!plane] * s->fragment_height[!!plane];
1516
        }
1517
    }
1518
48
}
1519
1520
24
static int vp4_unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1521
{
1522
    int i, j;
1523
    int dc_y_table;
1524
    int dc_c_table;
1525
    int ac_y_table;
1526
    int ac_c_table;
1527
    VLC *tables[2][64];
1528
    int plane, sb_y, sb_x;
1529
    int eob_tracker[64];
1530
    VP4Predictor dc_pred[6][6];
1531
    int last_dc[NB_VP4_DC_TYPES];
1532
1533
24
    if (get_bits_left(gb) < 16)
1534
        return AVERROR_INVALIDDATA;
1535
1536
    /* fetch the DC table indexes */
1537
24
    dc_y_table = get_bits(gb, 4);
1538
24
    dc_c_table = get_bits(gb, 4);
1539
1540
24
    ac_y_table = get_bits(gb, 4);
1541
24
    ac_c_table = get_bits(gb, 4);
1542
1543
    /* build tables of DC/AC VLC tables */
1544
1545
24
    tables[0][0] = &s->dc_vlc[dc_y_table];
1546
24
    tables[1][0] = &s->dc_vlc[dc_c_table];
1547
144
    for (i = 1; i <= 5; i++) {
1548
120
        tables[0][i] = &s->ac_vlc_1[ac_y_table];
1549
120
        tables[1][i] = &s->ac_vlc_1[ac_c_table];
1550
    }
1551
240
    for (i = 6; i <= 14; i++) {
1552
216
        tables[0][i] = &s->ac_vlc_2[ac_y_table];
1553
216
        tables[1][i] = &s->ac_vlc_2[ac_c_table];
1554
    }
1555
336
    for (i = 15; i <= 27; i++) {
1556
312
        tables[0][i] = &s->ac_vlc_3[ac_y_table];
1557
312
        tables[1][i] = &s->ac_vlc_3[ac_c_table];
1558
    }
1559
888
    for (i = 28; i <= 63; i++) {
1560
864
        tables[0][i] = &s->ac_vlc_4[ac_y_table];
1561
864
        tables[1][i] = &s->ac_vlc_4[ac_c_table];
1562
    }
1563
1564
24
    vp4_set_tokens_base(s);
1565
1566
24
    memset(last_dc, 0, sizeof(last_dc));
1567
1568

96
    for (plane = 0; plane < ((s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 1 : 3); plane++) {
1569
72
        memset(eob_tracker, 0, sizeof(eob_tracker));
1570
1571
        /* initialise dc prediction */
1572
3720
        for (i = 0; i < s->fragment_width[!!plane]; i++)
1573
3648
            vp4_dc_predictor_reset(&s->dc_pred_row[i]);
1574
1575
504
        for (j = 0; j < 6; j++)
1576
3024
            for (i = 0; i < 6; i++)
1577
2592
                vp4_dc_predictor_reset(&dc_pred[j][i]);
1578
1579
456
        for (sb_y = 0; sb_y * 4 < s->fragment_height[!!plane]; sb_y++) {
1580
5952
            for (sb_x = 0; sb_x *4 < s->fragment_width[!!plane]; sb_x++) {
1581
5568
                vp4_dc_pred_before(s, dc_pred, sb_x);
1582
94656
                for (j = 0; j < 16; j++) {
1583
89088
                        int hx = hilbert_offset[j][0];
1584
89088
                        int hy = hilbert_offset[j][1];
1585
89088
                        int x  = 4 * sb_x + hx;
1586
89088
                        int y  = 4 * sb_y + hy;
1587
89088
                        VP4Predictor *this_dc_pred = &dc_pred[hy + 1][hx + 1];
1588
                        int fragment, dc_block_type;
1589
1590

89088
                        if (x >= s->fragment_width[!!plane] || y >= s->fragment_height[!!plane])
1591
1536
                            continue;
1592
1593
87552
                        fragment = s->fragment_start[plane] + y * s->fragment_width[!!plane] + x;
1594
1595
87552
                        if (s->all_fragments[fragment].coding_method == MODE_COPY)
1596
24300
                            continue;
1597
1598
63252
                        if (vp4_unpack_vlcs(s, gb, tables[!!plane], plane, eob_tracker, fragment) < 0)
1599
                            return -1;
1600
1601
63252
                        dc_block_type = vp4_pred_block_type_map[s->all_fragments[fragment].coding_method];
1602
1603
63252
                        s->all_fragments[fragment].dc +=
1604
63252
                            vp4_dc_pred(s, this_dc_pred, last_dc, dc_block_type, plane);
1605
1606
63252
                        this_dc_pred->type = dc_block_type,
1607
63252
                        this_dc_pred->dc   = last_dc[dc_block_type] = s->all_fragments[fragment].dc;
1608
                }
1609
5568
                vp4_dc_pred_after(s, dc_pred, sb_x);
1610
            }
1611
        }
1612
    }
1613
1614
24
    vp4_set_tokens_base(s);
1615
1616
24
    return 0;
1617
}
1618
#endif
1619
1620
/*
1621
 * This function reverses the DC prediction for each coded fragment in
1622
 * the frame. Much of this function is adapted directly from the original
1623
 * VP3 source code.
1624
 */
1625
#define COMPATIBLE_FRAME(x)                                                   \
1626
    (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1627
#define DC_COEFF(u) s->all_fragments[u].dc
1628
1629
420
static void reverse_dc_prediction(Vp3DecodeContext *s,
1630
                                  int first_fragment,
1631
                                  int fragment_width,
1632
                                  int fragment_height)
1633
{
1634
#define PUL 8
1635
#define PU 4
1636
#define PUR 2
1637
#define PL 1
1638
1639
    int x, y;
1640
420
    int i = first_fragment;
1641
1642
    int predicted_dc;
1643
1644
    /* DC values for the left, up-left, up, and up-right fragments */
1645
    int vl, vul, vu, vur;
1646
1647
    /* indexes for the left, up-left, up, and up-right fragments */
1648
    int l, ul, u, ur;
1649
1650
    /*
1651
     * The 6 fields mean:
1652
     *   0: up-left multiplier
1653
     *   1: up multiplier
1654
     *   2: up-right multiplier
1655
     *   3: left multiplier
1656
     */
1657
    static const int predictor_transform[16][4] = {
1658
        {    0,   0,   0,   0 },
1659
        {    0,   0,   0, 128 }, // PL
1660
        {    0,   0, 128,   0 }, // PUR
1661
        {    0,   0,  53,  75 }, // PUR|PL
1662
        {    0, 128,   0,   0 }, // PU
1663
        {    0,  64,   0,  64 }, // PU |PL
1664
        {    0, 128,   0,   0 }, // PU |PUR
1665
        {    0,   0,  53,  75 }, // PU |PUR|PL
1666
        {  128,   0,   0,   0 }, // PUL
1667
        {    0,   0,   0, 128 }, // PUL|PL
1668
        {   64,   0,  64,   0 }, // PUL|PUR
1669
        {    0,   0,  53,  75 }, // PUL|PUR|PL
1670
        {    0, 128,   0,   0 }, // PUL|PU
1671
        { -104, 116,   0, 116 }, // PUL|PU |PL
1672
        {   24,  80,  24,   0 }, // PUL|PU |PUR
1673
        { -104, 116,   0, 116 }  // PUL|PU |PUR|PL
1674
    };
1675
1676
    /* This table shows which types of blocks can use other blocks for
1677
     * prediction. For example, INTRA is the only mode in this table to
1678
     * have a frame number of 0. That means INTRA blocks can only predict
1679
     * from other INTRA blocks. There are 2 golden frame coding types;
1680
     * blocks encoding in these modes can only predict from other blocks
1681
     * that were encoded with these 1 of these 2 modes. */
1682
    static const unsigned char compatible_frame[9] = {
1683
        1,    /* MODE_INTER_NO_MV */
1684
        0,    /* MODE_INTRA */
1685
        1,    /* MODE_INTER_PLUS_MV */
1686
        1,    /* MODE_INTER_LAST_MV */
1687
        1,    /* MODE_INTER_PRIOR_MV */
1688
        2,    /* MODE_USING_GOLDEN */
1689
        2,    /* MODE_GOLDEN_MV */
1690
        1,    /* MODE_INTER_FOUR_MV */
1691
        3     /* MODE_COPY */
1692
    };
1693
    int current_frame_type;
1694
1695
    /* there is a last DC predictor for each of the 3 frame types */
1696
    short last_dc[3];
1697
1698
420
    int transform = 0;
1699
1700
420
    vul =
1701
420
    vu  =
1702
420
    vur =
1703
420
    vl  = 0;
1704
420
    last_dc[0] =
1705
420
    last_dc[1] =
1706
420
    last_dc[2] = 0;
1707
1708
    /* for each fragment row... */
1709
14292
    for (y = 0; y < fragment_height; y++) {
1710
        /* for each fragment in a row... */
1711
1774512
        for (x = 0; x < fragment_width; x++, i++) {
1712
1713
            /* reverse prediction if this block was coded */
1714
1760640
            if (s->all_fragments[i].coding_method != MODE_COPY) {
1715
532576
                current_frame_type =
1716
532576
                    compatible_frame[s->all_fragments[i].coding_method];
1717
1718
532576
                transform = 0;
1719
532576
                if (x) {
1720
528445
                    l  = i - 1;
1721
528445
                    vl = DC_COEFF(l);
1722
528445
                    if (COMPATIBLE_FRAME(l))
1723
483186
                        transform |= PL;
1724
                }
1725
532576
                if (y) {
1726
513468
                    u  = i - fragment_width;
1727
513468
                    vu = DC_COEFF(u);
1728
513468
                    if (COMPATIBLE_FRAME(u))
1729
476069
                        transform |= PU;
1730
513468
                    if (x) {
1731
509661
                        ul  = i - fragment_width - 1;
1732
509661
                        vul = DC_COEFF(ul);
1733
509661
                        if (COMPATIBLE_FRAME(ul))
1734
461368
                            transform |= PUL;
1735
                    }
1736
513468
                    if (x + 1 < fragment_width) {
1737
505812
                        ur  = i - fragment_width + 1;
1738
505812
                        vur = DC_COEFF(ur);
1739
505812
                        if (COMPATIBLE_FRAME(ur))
1740
459980
                            transform |= PUR;
1741
                    }
1742
                }
1743
1744
532576
                if (transform == 0) {
1745
                    /* if there were no fragments to predict from, use last
1746
                     * DC saved */
1747
10817
                    predicted_dc = last_dc[current_frame_type];
1748
                } else {
1749
                    /* apply the appropriate predictor transform */
1750
521759
                    predicted_dc =
1751
521759
                        (predictor_transform[transform][0] * vul) +
1752
521759
                        (predictor_transform[transform][1] * vu) +
1753
521759
                        (predictor_transform[transform][2] * vur) +
1754
521759
                        (predictor_transform[transform][3] * vl);
1755
1756
521759
                    predicted_dc /= 128;
1757
1758
                    /* check for outranging on the [ul u l] and
1759
                     * [ul u ur l] predictors */
1760

521759
                    if ((transform == 15) || (transform == 13)) {
1761
434711
                        if (FFABS(predicted_dc - vu) > 128)
1762
2351
                            predicted_dc = vu;
1763
432360
                        else if (FFABS(predicted_dc - vl) > 128)
1764
1111
                            predicted_dc = vl;
1765
431249
                        else if (FFABS(predicted_dc - vul) > 128)
1766
2253
                            predicted_dc = vul;
1767
                    }
1768
                }
1769
1770
                /* at long last, apply the predictor */
1771
532576
                DC_COEFF(i) += predicted_dc;
1772
                /* save the DC */
1773
532576
                last_dc[current_frame_type] = DC_COEFF(i);
1774
            }
1775
        }
1776
    }
1777
420
}
1778
1779
2700
static void apply_loop_filter(Vp3DecodeContext *s, int plane,
1780
                              int ystart, int yend)
1781
{
1782
    int x, y;
1783
2700
    int *bounding_values = s->bounding_values_array + 127;
1784
1785
2700
    int width           = s->fragment_width[!!plane];
1786
2700
    int height          = s->fragment_height[!!plane];
1787
2700
    int fragment        = s->fragment_start[plane] + ystart * width;
1788
2700
    ptrdiff_t stride    = s->current_frame.f->linesize[plane];
1789
2700
    uint8_t *plane_data = s->current_frame.f->data[plane];
1790
2700
    if (!s->flipped_image)
1791
2700
        stride = -stride;
1792
2700
    plane_data += s->data_offset[plane] + 8 * ystart * stride;
1793
1794
10530
    for (y = ystart; y < yend; y++) {
1795
486710
        for (x = 0; x < width; x++) {
1796
            /* This code basically just deblocks on the edges of coded blocks.
1797
             * However, it has to be much more complicated because of the
1798
             * brain damaged deblock ordering used in VP3/Theora. Order matters
1799
             * because some pixels get filtered twice. */
1800
478880
            if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1801
                /* do not perform left edge filter for left columns frags */
1802
367025
                if (x > 0) {
1803
363601
                    s->vp3dsp.h_loop_filter(
1804
363601
                        plane_data + 8 * x,
1805
                        stride, bounding_values);
1806
                }
1807
1808
                /* do not perform top edge filter for top row fragments */
1809
367025
                if (y > 0) {
1810
349625
                    s->vp3dsp.v_loop_filter(
1811
349625
                        plane_data + 8 * x,
1812
                        stride, bounding_values);
1813
                }
1814
1815
                /* do not perform right edge filter for right column
1816
                 * fragments or if right fragment neighbor is also coded
1817
                 * in this frame (it will be filtered in next iteration) */
1818
367025
                if ((x < width - 1) &&
1819
359725
                    (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1820
32268
                    s->vp3dsp.h_loop_filter(
1821
32268
                        plane_data + 8 * x + 8,
1822
                        stride, bounding_values);
1823
                }
1824
1825
                /* do not perform bottom edge filter for bottom row
1826
                 * fragments or if bottom fragment neighbor is also coded
1827
                 * in this frame (it will be filtered in the next row) */
1828
367025
                if ((y < height - 1) &&
1829
352272
                    (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1830
32946
                    s->vp3dsp.v_loop_filter(
1831
32946
                        plane_data + 8 * x + 8 * stride,
1832
                        stride, bounding_values);
1833
                }
1834
            }
1835
1836
478880
            fragment++;
1837
        }
1838
7830
        plane_data += 8 * stride;
1839
    }
1840
2700
}
1841
1842
/**
1843
 * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1844
 * for the next block in coding order
1845
 */
1846
595828
static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1847
                              int plane, int inter, int16_t block[64])
1848
{
1849
595828
    int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1850
595828
    uint8_t *perm = s->idct_scantable;
1851
595828
    int i = 0;
1852
1853
    do {
1854
3343039
        int token = *s->dct_tokens[plane][i];
1855

3343039
        switch (token & 3) {
1856
587550
        case 0: // EOB
1857
587550
            if (--token < 4) // 0-3 are token types so the EOB run must now be 0
1858
280242
                s->dct_tokens[plane][i]++;
1859
            else
1860
307308
                *s->dct_tokens[plane][i] = token & ~3;
1861
587550
            goto end;
1862
913868
        case 1: // zero run
1863
913868
            s->dct_tokens[plane][i]++;
1864
913868
            i += (token >> 2) & 0x7f;
1865
913868
            if (i > 63) {
1866
                av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1867
                return i;
1868
            }
1869
913868
            block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1870
913868
            i++;
1871
913868
            break;
1872
1841621
        case 2: // coeff
1873
1841621
            block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1874
1841621
            s->dct_tokens[plane][i++]++;
1875
1841621
            break;
1876
        default: // shouldn't happen
1877
            return i;
1878
        }
1879
2755489
    } while (i < 64);
1880
    // return value is expected to be a valid level
1881
8278
    i--;
1882
595828
end:
1883
    // the actual DC+prediction is in the fragment structure
1884
595828
    block[0] = frag->dc * s->qmat[0][inter][plane][0];
1885
595828
    return i;
1886
}
1887
1888
/**
1889
 * called when all pixels up to row y are complete
1890
 */
1891
1232
static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1892
{
1893
    int h, cy, i;
1894
    int offset[AV_NUM_DATA_POINTERS];
1895
1896
1232
    if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1897
        int y_flipped = s->flipped_image ? s->height - y : y;
1898
1899
        /* At the end of the frame, report INT_MAX instead of the height of
1900
         * the frame. This makes the other threads' ff_thread_await_progress()
1901
         * calls cheaper, because they don't have to clip their values. */
1902
        ff_thread_report_progress(&s->current_frame,
1903
                                  y_flipped == s->height ? INT_MAX
1904
                                                         : y_flipped - 1,
1905
                                  0);
1906
    }
1907
1908
1232
    if (!s->avctx->draw_horiz_band)
1909
1232
        return;
1910
1911
    h = y - s->last_slice_end;
1912
    s->last_slice_end = y;
1913
    y -= h;
1914
1915
    if (!s->flipped_image)
1916
        y = s->height - y - h;
1917
1918
    cy        = y >> s->chroma_y_shift;
1919
    offset[0] = s->current_frame.f->linesize[0] * y;
1920
    offset[1] = s->current_frame.f->linesize[1] * cy;
1921
    offset[2] = s->current_frame.f->linesize[2] * cy;
1922
    for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
1923
        offset[i] = 0;
1924
1925
    emms_c();
1926
    s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1927
}
1928
1929
/**
1930
 * Wait for the reference frame of the current fragment.
1931
 * The progress value is in luma pixel rows.
1932
 */
1933
static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment,
1934
                                int motion_y, int y)
1935
{
1936
    ThreadFrame *ref_frame;
1937
    int ref_row;
1938
    int border = motion_y & 1;
1939
1940
    if (fragment->coding_method == MODE_USING_GOLDEN ||
1941
        fragment->coding_method == MODE_GOLDEN_MV)
1942
        ref_frame = &s->golden_frame;
1943
    else
1944
        ref_frame = &s->last_frame;
1945
1946
    ref_row = y + (motion_y >> 1);
1947
    ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1948
1949
    ff_thread_await_progress(ref_frame, ref_row, 0);
1950
}
1951
1952
#if CONFIG_VP4_DECODER
1953
/**
1954
 * @return non-zero if temp (edge_emu_buffer) was populated
1955
 */
1956
36568
static int vp4_mc_loop_filter(Vp3DecodeContext *s, int plane, int motion_x, int motion_y, int bx, int by,
1957
       uint8_t * motion_source, int stride, int src_x, int src_y, uint8_t *temp)
1958
{
1959
36568
    int motion_shift = plane ? 4 : 2;
1960
36568
    int subpel_mask = plane ? 3 : 1;
1961
36568
    int *bounding_values = s->bounding_values_array + 127;
1962
1963
    int i;
1964
    int x, y;
1965
    int x2, y2;
1966
    int x_subpel, y_subpel;
1967
    int x_offset, y_offset;
1968
1969
36568
    int block_width = plane ? 8 : 16;
1970

36568
    int plane_width  = s->width  >> (plane && s->chroma_x_shift);
1971

36568
    int plane_height = s->height >> (plane && s->chroma_y_shift);
1972
1973
#define loop_stride 12
1974
    uint8_t loop[12 * loop_stride];
1975
1976
    /* using division instead of shift to correctly handle negative values */
1977
36568
    x = 8 * bx + motion_x / motion_shift;
1978
36568
    y = 8 * by + motion_y / motion_shift;
1979
1980
36568
    x_subpel = motion_x & subpel_mask;
1981
36568
    y_subpel = motion_y & subpel_mask;
1982
1983

36568
    if (x_subpel || y_subpel) {
1984
30063
        x--;
1985
30063
        y--;
1986
1987
30063
        if (x_subpel)
1988

22774
            x = FFMIN(x, x + FFSIGN(motion_x));
1989
1990
30063
        if (y_subpel)
1991

15343
            y = FFMIN(y, y + FFSIGN(motion_y));
1992
1993
30063
        x2 = x + block_width;
1994
30063
        y2 = y + block_width;
1995
1996


30063
        if (x2 < 0 || x2 >= plane_width || y2 < 0 || y2 >= plane_height)
1997
596
            return 0;
1998
1999
29467
        x_offset = (-(x + 2) & 7) + 2;
2000
29467
        y_offset = (-(y + 2) & 7) + 2;
2001
2002

29467
        if (x_offset > 8 + x_subpel && y_offset > 8 + y_subpel)
2003
            return 0;
2004
2005
29467
        s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2006
             loop_stride, stride,
2007
             12, 12, src_x - 1, src_y - 1,
2008
             plane_width,
2009
             plane_height);
2010
2011
29467
        if (x_offset <= 8 + x_subpel)
2012
25000
            ff_vp3dsp_h_loop_filter_12(loop + x_offset, loop_stride, bounding_values);
2013
2014
29467
        if (y_offset <= 8 + y_subpel)
2015
17032
            ff_vp3dsp_v_loop_filter_12(loop + y_offset*loop_stride, loop_stride, bounding_values);
2016
2017
    } else {
2018
2019
6505
        x_offset = -x & 7;
2020
6505
        y_offset = -y & 7;
2021
2022

6505
        if (!x_offset && !y_offset)
2023
118
            return 0;
2024
2025
6387
        s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2026
             loop_stride, stride,
2027
             12, 12, src_x - 1, src_y - 1,
2028
             plane_width,
2029
             plane_height);
2030
2031
#define safe_loop_filter(name, ptr, stride, bounding_values) \
2032
    if ((uintptr_t)(ptr) & 7) \
2033
        s->vp3dsp.name##_unaligned(ptr, stride, bounding_values); \
2034
    else \
2035
        s->vp3dsp.name(ptr, stride, bounding_values);
2036
2037
6387
        if (x_offset)
2038
5671
            safe_loop_filter(h_loop_filter, loop + loop_stride + x_offset + 1, loop_stride, bounding_values);
2039
2040
6387
        if (y_offset)
2041
3055
            safe_loop_filter(v_loop_filter, loop + (y_offset + 1)*loop_stride + 1, loop_stride, bounding_values);
2042
    }
2043
2044
358540
    for (i = 0; i < 9; i++)
2045
322686
        memcpy(temp + i*stride, loop + (i + 1) * loop_stride + 1, 9);
2046
2047
35854
    return 1;
2048
}
2049
#endif
2050
2051
/*
2052
 * Perform the final rendering for a particular slice of data.
2053
 * The slice number ranges from 0..(c_superblock_height - 1).
2054
 */
2055
1068
static void render_slice(Vp3DecodeContext *s, int slice)
2056
{
2057
    int x, y, i, j, fragment;
2058
1068
    int16_t *block = s->block;
2059
1068
    int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
2060
    int motion_halfpel_index;
2061
    uint8_t *motion_source;
2062
    int plane, first_pixel;
2063
2064
1068
    if (slice >= s->c_superblock_height)
2065
        return;
2066
2067
4272
    for (plane = 0; plane < 3; plane++) {
2068
3204
        uint8_t *output_plane = s->current_frame.f->data[plane] +
2069
3204
                                s->data_offset[plane];
2070
3204
        uint8_t *last_plane = s->last_frame.f->data[plane] +
2071
3204
                              s->data_offset[plane];
2072
3204
        uint8_t *golden_plane = s->golden_frame.f->data[plane] +
2073
3204
                                s->data_offset[plane];
2074
3204
        ptrdiff_t stride = s->current_frame.f->linesize[plane];
2075

3204
        int plane_width  = s->width  >> (plane && s->chroma_x_shift);
2076

3204
        int plane_height = s->height >> (plane && s->chroma_y_shift);
2077
3204
        int8_t(*motion_val)[2] = s->motion_val[!!plane];
2078
2079

3204
        int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
2080

3204
        int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
2081
3204
        int slice_width  = plane ? s->c_superblock_width
2082
3204
                                 : s->y_superblock_width;
2083
2084
3204
        int fragment_width  = s->fragment_width[!!plane];
2085
3204
        int fragment_height = s->fragment_height[!!plane];
2086
3204
        int fragment_start  = s->fragment_start[plane];
2087
2088
4272
        int do_await = !plane && HAVE_THREADS &&
2089
1068
                       (s->avctx->active_thread_type & FF_THREAD_FRAME);
2090
2091
3204
        if (!s->flipped_image)
2092
3204
            stride = -stride;
2093
        if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2094
            continue;
2095
2096
        /* for each superblock row in the slice (both of them)... */
2097
7476
        for (; sb_y < slice_height; sb_y++) {
2098
            /* for each superblock in a row... */
2099
129560
            for (sb_x = 0; sb_x < slice_width; sb_x++) {
2100
                /* for each block in a superblock... */
2101
2129896
                for (j = 0; j < 16; j++) {
2102
2004608
                    x        = 4 * sb_x + hilbert_offset[j][0];
2103
2004608
                    y        = 4 * sb_y + hilbert_offset[j][1];
2104
2004608
                    fragment = y * fragment_width + x;
2105
2106
2004608
                    i = fragment_start + fragment;
2107
2108
                    // bounds check
2109

2004608
                    if (x >= fragment_width || y >= fragment_height)
2110
156416
                        continue;
2111
2112
1848192
                    first_pixel = 8 * y * stride + 8 * x;
2113
2114
1848192
                    if (do_await &&
2115
                        s->all_fragments[i].coding_method != MODE_INTRA)
2116
                        await_reference_row(s, &s->all_fragments[i],
2117
                                            motion_val[fragment][1],
2118
                                            (16 * y) >> s->chroma_y_shift);
2119
2120
                    /* transform if this block was coded */
2121
1848192
                    if (s->all_fragments[i].coding_method != MODE_COPY) {
2122
595828
                        if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2123
592182
                            (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2124
7685
                            motion_source = golden_plane;
2125
                        else
2126
588143
                            motion_source = last_plane;
2127
2128
595828
                        motion_source       += first_pixel;
2129
595828
                        motion_halfpel_index = 0;
2130
2131
                        /* sort out the motion vector if this fragment is coded
2132
                         * using a motion vector method */
2133
595828
                        if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2134
377836
                            (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2135
                            int src_x, src_y;
2136
374190
                            int standard_mc = 1;
2137
374190
                            motion_x = motion_val[fragment][0];
2138
374190
                            motion_y = motion_val[fragment][1];
2139
#if CONFIG_VP4_DECODER
2140

374190
                            if (plane && s->version >= 2) {
2141
11931
                                motion_x = (motion_x >> 1) | (motion_x & 1);
2142
11931
                                motion_y = (motion_y >> 1) | (motion_y & 1);
2143
                            }
2144
#endif
2145
2146
374190
                            src_x = (motion_x >> 1) + 8 * x;
2147
374190
                            src_y = (motion_y >> 1) + 8 * y;
2148
2149
374190
                            motion_halfpel_index = motion_x & 0x01;
2150
374190
                            motion_source       += (motion_x >> 1);
2151
2152
374190
                            motion_halfpel_index |= (motion_y & 0x01) << 1;
2153
374190
                            motion_source        += ((motion_y >> 1) * stride);
2154
2155
#if CONFIG_VP4_DECODER
2156
374190
                            if (s->version >= 2) {
2157
36568
                                uint8_t *temp = s->edge_emu_buffer;
2158
36568
                                if (stride < 0)
2159
36568
                                    temp -= 8 * stride;
2160
36568
                                if (vp4_mc_loop_filter(s, plane, motion_val[fragment][0], motion_val[fragment][1], x, y, motion_source, stride, src_x, src_y, temp)) {
2161
35854
                                    motion_source = temp;
2162
35854
                                    standard_mc = 0;
2163
                                }
2164
                            }
2165
#endif
2166
2167

374190
                            if (standard_mc && (
2168
336975
                                src_x < 0 || src_y < 0 ||
2169
322220
                                src_x + 9 >= plane_width ||
2170
315161
                                src_y + 9 >= plane_height)) {
2171
34786
                                uint8_t *temp = s->edge_emu_buffer;
2172
34786
                                if (stride < 0)
2173
34786
                                    temp -= 8 * stride;
2174
2175
34786
                                s->vdsp.emulated_edge_mc(temp, motion_source,
2176
                                                         stride, stride,
2177
                                                         9, 9, src_x, src_y,
2178
                                                         plane_width,
2179
                                                         plane_height);
2180
34786
                                motion_source = temp;
2181
                            }
2182
                        }
2183
2184
                        /* first, take care of copying a block from either the
2185
                         * previous or the golden frame */
2186
595828
                        if (s->all_fragments[i].coding_method != MODE_INTRA) {
2187
                            /* Note, it is possible to implement all MC cases
2188
                             * with put_no_rnd_pixels_l2 which would look more
2189
                             * like the VP3 source but this would be slower as
2190
                             * put_no_rnd_pixels_tab is better optimized */
2191
411494
                            if (motion_halfpel_index != 3) {
2192
293916
                                s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
2193
                                    output_plane + first_pixel,
2194
                                    motion_source, stride, 8);
2195
                            } else {
2196
                                /* d is 0 if motion_x and _y have the same sign,
2197
                                 * else -1 */
2198
117578
                                int d = (motion_x ^ motion_y) >> 31;
2199
117578
                                s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
2200
117578
                                                               motion_source - d,
2201
117578
                                                               motion_source + stride + 1 + d,
2202
                                                               stride, 8);
2203
                            }
2204
                        }
2205
2206
                        /* invert DCT and place (or add) in final output */
2207
2208
595828
                        if (s->all_fragments[i].coding_method == MODE_INTRA) {
2209
184334
                            vp3_dequant(s, s->all_fragments + i,
2210
                                        plane, 0, block);
2211
184334
                            s->vp3dsp.idct_put(output_plane + first_pixel,
2212
                                               stride,
2213
                                               block);
2214
                        } else {
2215
411494
                            if (vp3_dequant(s, s->all_fragments + i,
2216
                                            plane, 1, block)) {
2217
284346
                                s->vp3dsp.idct_add(output_plane + first_pixel,
2218
                                                   stride,
2219
                                                   block);
2220
                            } else {
2221
127148
                                s->vp3dsp.idct_dc_add(output_plane + first_pixel,
2222
                                                      stride, block);
2223
                            }
2224
                        }
2225
                    } else {
2226
                        /* copy directly from the previous frame */
2227
1252364
                        s->hdsp.put_pixels_tab[1][0](
2228
                            output_plane + first_pixel,
2229
1252364
                            last_plane + first_pixel,
2230
                            stride, 8);
2231
                    }
2232
                }
2233
            }
2234
2235
            // Filter up to the last row in the superblock row
2236

4272
            if (s->version < 2 && !s->skip_loop_filter)
2237
2280
                apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
2238
2280
                                  FFMIN(4 * sb_y + 3, fragment_height - 1));
2239
        }
2240
    }
2241
2242
    /* this looks like a good place for slice dispatch... */
2243
    /* algorithm:
2244
     *   if (slice == s->macroblock_height - 1)
2245
     *     dispatch (both last slice & 2nd-to-last slice);
2246
     *   else if (slice > 0)
2247
     *     dispatch (slice - 1);
2248
     */
2249
2250
1068
    vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
2251
                                 s->height - 16));
2252
}
2253
2254
/// Allocate tables for per-frame data in Vp3DecodeContext
2255
12
static av_cold int allocate_tables(AVCodecContext *avctx)
2256
{
2257
12
    Vp3DecodeContext *s = avctx->priv_data;
2258
    int y_fragment_count, c_fragment_count;
2259
2260
12
    free_tables(avctx);
2261
2262
12
    y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2263
12
    c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2264
2265
    /* superblock_coding is used by unpack_superblocks (VP3/Theora) and vp4_unpack_macroblocks (VP4) */
2266
12
    s->superblock_coding = av_mallocz(FFMAX(s->superblock_count, s->yuv_macroblock_count));
2267
12
    s->all_fragments     = av_mallocz_array(s->fragment_count, sizeof(Vp3Fragment));
2268
2269
12
    s-> kf_coded_fragment_list = av_mallocz_array(s->fragment_count, sizeof(int));
2270
12
    s->nkf_coded_fragment_list = av_mallocz_array(s->fragment_count, sizeof(int));
2271
12
    memset(s-> num_kf_coded_fragment, -1, sizeof(s-> num_kf_coded_fragment));
2272
2273
12
    s->dct_tokens_base = av_mallocz_array(s->fragment_count,
2274
                                          64 * sizeof(*s->dct_tokens_base));
2275
12
    s->motion_val[0] = av_mallocz_array(y_fragment_count, sizeof(*s->motion_val[0]));
2276
12
    s->motion_val[1] = av_mallocz_array(c_fragment_count, sizeof(*s->motion_val[1]));
2277
2278
    /* work out the block mapping tables */
2279
12
    s->superblock_fragments = av_mallocz_array(s->superblock_count, 16 * sizeof(int));
2280
12
    s->macroblock_coding    = av_mallocz(s->macroblock_count + 1);
2281
2282
12
    s->dc_pred_row = av_malloc_array(s->y_superblock_width * 4, sizeof(*s->dc_pred_row));
2283
2284

12
    if (!s->superblock_coding    || !s->all_fragments          ||
2285

12
        !s->dct_tokens_base      || !s->kf_coded_fragment_list ||
2286
12
        !s->nkf_coded_fragment_list ||
2287

12
        !s->superblock_fragments || !s->macroblock_coding      ||
2288
12
        !s->dc_pred_row ||
2289

12
        !s->motion_val[0]        || !s->motion_val[1]) {
2290
        vp3_decode_end(avctx);
2291
        return -1;
2292
    }
2293
2294
12
    init_block_mapping(s);
2295
2296
12
    return 0;
2297
}
2298
2299
12
static av_cold int init_frames(Vp3DecodeContext *s)
2300
{
2301
12
    s->current_frame.f = av_frame_alloc();
2302
12
    s->last_frame.f    = av_frame_alloc();
2303
12
    s->golden_frame.f  = av_frame_alloc();
2304
2305

12
    if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) {
2306
        av_frame_free(&s->current_frame.f);
2307
        av_frame_free(&s->last_frame.f);
2308
        av_frame_free(&s->golden_frame.f);
2309
        return AVERROR(ENOMEM);
2310
    }
2311
2312
12
    return 0;
2313
}
2314
2315
12
static av_cold int vp3_decode_init(AVCodecContext *avctx)
2316
{
2317
12
    Vp3DecodeContext *s = avctx->priv_data;
2318
    int i, inter, plane, ret;
2319
    int c_width;
2320
    int c_height;
2321
    int y_fragment_count, c_fragment_count;
2322
#if CONFIG_VP4_DECODER
2323
    int j;
2324
#endif
2325
2326
12
    ret = init_frames(s);
2327
12
    if (ret < 0)
2328
        return ret;
2329
2330
12
    if (avctx->codec_tag == MKTAG('V', 'P', '4', '0'))
2331
2
        s->version = 3;
2332
10
    else if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
2333
        s->version = 0;
2334
    else
2335
10
        s->version = 1;
2336
2337
12
    s->avctx  = avctx;
2338
12
    s->width  = FFALIGN(avctx->coded_width, 16);
2339
12
    s->height = FFALIGN(avctx->coded_height, 16);
2340
12
    if (avctx->codec_id != AV_CODEC_ID_THEORA)
2341
5
        avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2342
12
    avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
2343
12
    ff_hpeldsp_init(&s->hdsp, avctx->flags | AV_CODEC_FLAG_BITEXACT);
2344
12
    ff_videodsp_init(&s->vdsp, 8);
2345
12
    ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
2346
2347
780
    for (i = 0; i < 64; i++) {
2348
#define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
2349
768
        s->idct_permutation[i] = TRANSPOSE(i);
2350
768
        s->idct_scantable[i]   = TRANSPOSE(ff_zigzag_direct[i]);
2351
#undef TRANSPOSE
2352
    }
2353
2354
    /* initialize to an impossible value which will force a recalculation
2355
     * in the first frame decode */
2356
48
    for (i = 0; i < 3; i++)
2357
36
        s->qps[i] = -1;
2358
2359
12
    ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
2360
12
    if (ret)
2361
        return ret;
2362
2363
12
    s->y_superblock_width  = (s->width  + 31) / 32;
2364
12
    s->y_superblock_height = (s->height + 31) / 32;
2365
12
    s->y_superblock_count  = s->y_superblock_width * s->y_superblock_height;
2366
2367
    /* work out the dimensions for the C planes */
2368
12
    c_width                = s->width >> s->chroma_x_shift;
2369
12
    c_height               = s->height >> s->chroma_y_shift;
2370
12
    s->c_superblock_width  = (c_width  + 31) / 32;
2371
12
    s->c_superblock_height = (c_height + 31) / 32;
2372
12
    s->c_superblock_count  = s->c_superblock_width * s->c_superblock_height;
2373
2374
12
    s->superblock_count   = s->y_superblock_count + (s->c_superblock_count * 2);
2375
12
    s->u_superblock_start = s->y_superblock_count;
2376
12
    s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
2377
2378
12
    s->macroblock_width  = (s->width  + 15) / 16;
2379
12
    s->macroblock_height = (s->height + 15) / 16;
2380
12
    s->macroblock_count  = s->macroblock_width * s->macroblock_height;
2381
12
    s->c_macroblock_width  = (c_width  + 15) / 16;
2382
12
    s->c_macroblock_height = (c_height + 15) / 16;
2383
12
    s->c_macroblock_count  = s->c_macroblock_width * s->c_macroblock_height;
2384
12
    s->yuv_macroblock_count = s->macroblock_count + 2 * s->c_macroblock_count;
2385
2386
12
    s->fragment_width[0]  = s->width / FRAGMENT_PIXELS;
2387
12
    s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
2388
12
    s->fragment_width[1]  = s->fragment_width[0] >> s->chroma_x_shift;
2389
12
    s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
2390
2391
    /* fragment count covers all 8x8 blocks for all 3 planes */
2392
12
    y_fragment_count     = s->fragment_width[0] * s->fragment_height[0];
2393
12
    c_fragment_count     = s->fragment_width[1] * s->fragment_height[1];
2394
12
    s->fragment_count    = y_fragment_count + 2 * c_fragment_count;
2395
12
    s->fragment_start[1] = y_fragment_count;
2396
12
    s->fragment_start[2] = y_fragment_count + c_fragment_count;
2397
2398
12
    if (!s->theora_tables) {
2399
325
        for (i = 0; i < 64; i++) {
2400
320
            s->coded_dc_scale_factor[0][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_y_dc_scale_factor[i];
2401
320
            s->coded_dc_scale_factor[1][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_uv_dc_scale_factor[i];
2402
320
            s->coded_ac_scale_factor[i] = s->version < 2 ? vp31_ac_scale_factor[i] : vp4_ac_scale_factor[i];
2403
320
            s->base_matrix[0][i]        = s->version < 2 ? vp31_intra_y_dequant[i] : vp4_generic_dequant[i];
2404
320
            s->base_matrix[1][i]        = s->version < 2 ? vp31_intra_c_dequant[i] : vp4_generic_dequant[i];
2405
320
            s->base_matrix[2][i]        = s->version < 2 ? vp31_inter_dequant[i]   : vp4_generic_dequant[i];
2406
320
            s->filter_limit_values[i]   = s->version < 2 ? vp31_filter_limit_values[i] : vp4_filter_limit_values[i];
2407
        }
2408
2409
15
        for (inter = 0; inter < 2; inter++) {
2410
40
            for (plane = 0; plane < 3; plane++) {
2411
30
                s->qr_count[inter][plane]   = 1;
2412
30
                s->qr_size[inter][plane][0] = 63;
2413
30
                s->qr_base[inter][plane][0] =
2414
30
                s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
2415
            }
2416
        }
2417
2418
        /* init VLC tables */
2419
5
        if (s->version < 2) {
2420
51
            for (i = 0; i < 16; i++) {
2421
                /* DC histograms */
2422
48
                init_vlc(&s->dc_vlc[i], 11, 32,
2423
                         &dc_bias[i][0][1], 4, 2,
2424
                         &dc_bias[i][0][0], 4, 2, 0);
2425
2426
                /* group 1 AC histograms */
2427
48
                init_vlc(&s->ac_vlc_1[i], 11, 32,
2428
                         &ac_bias_0[i][0][1], 4, 2,
2429
                         &ac_bias_0[i][0][0], 4, 2, 0);
2430
2431
                /* group 2 AC histograms */
2432
48
                init_vlc(&s->ac_vlc_2[i], 11, 32,
2433
                         &ac_bias_1[i][0][1], 4, 2,
2434
                         &ac_bias_1[i][0][0], 4, 2, 0);
2435
2436
                /* group 3 AC histograms */
2437
48
                init_vlc(&s->ac_vlc_3[i], 11, 32,
2438
                         &ac_bias_2[i][0][1], 4, 2,
2439
                         &ac_bias_2[i][0][0], 4, 2, 0);
2440
2441
                /* group 4 AC histograms */
2442
48
                init_vlc(&s->ac_vlc_4[i], 11, 32,
2443
                         &ac_bias_3[i][0][1], 4, 2,
2444
                         &ac_bias_3[i][0][0], 4, 2, 0);
2445
            }
2446
#if CONFIG_VP4_DECODER
2447
        } else { /* version >= 2 */
2448
34
            for (i = 0; i < 16; i++) {
2449
                /* DC histograms */
2450
32
                init_vlc(&s->dc_vlc[i], 11, 32,
2451
                         &vp4_dc_bias[i][0][1], 4, 2,
2452
                         &vp4_dc_bias[i][0][0], 4, 2, 0);
2453
2454
                /* group 1 AC histograms */
2455
32
                init_vlc(&s->ac_vlc_1[i], 11, 32,
2456
                         &vp4_ac_bias_0[i][0][1], 4, 2,
2457
                         &vp4_ac_bias_0[i][0][0], 4, 2, 0);
2458
2459
                /* group 2 AC histograms */
2460
32
                init_vlc(&s->ac_vlc_2[i], 11, 32,
2461
                         &vp4_ac_bias_1[i][0][1], 4, 2,
2462
                         &vp4_ac_bias_1[i][0][0], 4, 2, 0);
2463
2464
                /* group 3 AC histograms */
2465
32
                init_vlc(&s->ac_vlc_3[i], 11, 32,
2466
                         &vp4_ac_bias_2[i][0][1], 4, 2,
2467
                         &vp4_ac_bias_2[i][0][0], 4, 2, 0);
2468
2469
                /* group 4 AC histograms */
2470
32
                init_vlc(&s->ac_vlc_4[i], 11, 32,
2471
                         &vp4_ac_bias_3[i][0][1], 4, 2,
2472
                         &vp4_ac_bias_3[i][0][0], 4, 2, 0);
2473
            }
2474
#endif
2475
        }
2476
    } else {
2477
119
        for (i = 0; i < 16; i++) {
2478
            /* DC histograms */
2479
112
            if (init_vlc(&s->dc_vlc[i], 11, 32,
2480
                         &s->huffman_table[i][0][1], 8, 4,
2481
                         &s->huffman_table[i][0][0], 8, 4, 0) < 0)
2482
                goto vlc_fail;
2483
2484
            /* group 1 AC histograms */
2485
112
            if (init_vlc(&s->ac_vlc_1[i], 11, 32,
2486
                         &s->huffman_table[i + 16][0][1], 8, 4,
2487
                         &s->huffman_table[i + 16][0][0], 8, 4, 0) < 0)
2488
                goto vlc_fail;
2489
2490
            /* group 2 AC histograms */
2491
112
            if (init_vlc(&s->ac_vlc_2[i], 11, 32,
2492
                         &s->huffman_table[i + 16 * 2][0][1], 8, 4,
2493
                         &s->huffman_table[i + 16 * 2][0][0], 8, 4, 0) < 0)
2494
                goto vlc_fail;
2495
2496
            /* group 3 AC histograms */
2497
112
            if (init_vlc(&s->ac_vlc_3[i], 11, 32,
2498
                         &s->huffman_table[i + 16 * 3][0][1], 8, 4,
2499
                         &s->huffman_table[i + 16 * 3][0][0], 8, 4, 0) < 0)
2500
                goto vlc_fail;
2501
2502
            /* group 4 AC histograms */
2503
112
            if (init_vlc(&s->ac_vlc_4[i], 11, 32,
2504
                         &s->huffman_table[i + 16 * 4][0][1], 8, 4,
2505
                         &s->huffman_table[i + 16 * 4][0][0], 8, 4, 0) < 0)
2506
                goto vlc_fail;
2507
        }
2508
    }
2509
2510
12
    init_vlc(&s->superblock_run_length_vlc, 6, 34,
2511
             &superblock_run_length_vlc_table[0][1], 4, 2,
2512
             &superblock_run_length_vlc_table[0][0], 4, 2, 0);
2513
2514
12
    init_vlc(&s->fragment_run_length_vlc, 5, 30,
2515
             &fragment_run_length_vlc_table[0][1], 4, 2,
2516
             &fragment_run_length_vlc_table[0][0], 4, 2, 0);
2517
2518
12
    init_vlc(&s->mode_code_vlc, 3, 8,
2519
             &mode_code_vlc_table[0][1], 2, 1,
2520
             &mode_code_vlc_table[0][0], 2, 1, 0);
2521
2522
12
    init_vlc(&s->motion_vector_vlc, 6, 63,
2523
             &motion_vector_vlc_table[0][1], 2, 1,
2524
             &motion_vector_vlc_table[0][0], 2, 1, 0);
2525
2526
#if CONFIG_VP4_DECODER
2527
36
    for (j = 0; j < 2; j++)
2528
192
        for (i = 0; i < 7; i++)
2529
168
            init_vlc(&s->vp4_mv_vlc[j][i], 6, 63,
2530
                 &vp4_mv_vlc[j][i][0][1], 4, 2,
2531
                 &vp4_mv_vlc[j][i][0][0], 4, 2, 0);
2532
2533
    /* version >= 2 */
2534
36
    for (i = 0; i < 2; i++)
2535
24
        init_vlc(&s->block_pattern_vlc[i], 3, 14,
2536
             &vp4_block_pattern_vlc[i][0][1], 2, 1,
2537
             &vp4_block_pattern_vlc[i][0][0], 2, 1, 0);
2538
#endif
2539
2540
12
    return allocate_tables(avctx);
2541
2542
vlc_fail:
2543
    av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
2544
    return -1;
2545
}
2546
2547
/// Release and shuffle frames after decode finishes
2548
164
static int update_frames(AVCodecContext *avctx)
2549
{
2550
164
    Vp3DecodeContext *s = avctx->priv_data;
2551
164
    int ret = 0;
2552
2553
    /* shuffle frames (last = current) */
2554
164
    ff_thread_release_buffer(avctx, &s->last_frame);
2555
164
    ret = ff_thread_ref_frame(&s->last_frame, &s->current_frame);
2556
164
    if (ret < 0)
2557
        goto fail;
2558
2559
164
    if (s->keyframe) {
2560
6
        ff_thread_release_buffer(avctx, &s->golden_frame);
2561
6
        ret = ff_thread_ref_frame(&s->golden_frame, &s->current_frame);
2562
    }
2563
2564
158
fail:
2565
164
    ff_thread_release_buffer(avctx, &s->current_frame);
2566
164
    return ret;
2567
}
2568
2569
#if HAVE_THREADS
2570
static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
2571
{
2572
    ff_thread_release_buffer(s->avctx, dst);
2573
    if (src->f->data[0])
2574
        return ff_thread_ref_frame(dst, src);
2575
    return 0;
2576
}
2577
2578
static int ref_frames(Vp3DecodeContext *dst, Vp3DecodeContext *src)
2579
{
2580
    int ret;
2581
    if ((ret = ref_frame(dst, &dst->current_frame, &src->current_frame)) < 0 ||
2582
        (ret = ref_frame(dst, &dst->golden_frame,  &src->golden_frame)) < 0  ||
2583
        (ret = ref_frame(dst, &dst->last_frame,    &src->last_frame)) < 0)
2584
        return ret;
2585
    return 0;
2586
}
2587
2588
static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
2589
{
2590
    Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
2591
    int qps_changed = 0, i, err;
2592
2593
    if (!s1->current_frame.f->data[0] ||
2594
        s->width != s1->width || s->height != s1->height) {
2595
        if (s != s1)
2596
            ref_frames(s, s1);
2597
        return -1;
2598
    }
2599
2600
    if (s != s1) {
2601
        // copy previous frame data
2602
        if ((err = ref_frames(s, s1)) < 0)
2603
            return err;
2604
2605
        s->keyframe = s1->keyframe;
2606
2607
        // copy qscale data if necessary
2608
        for (i = 0; i < 3; i++) {
2609
            if (s->qps[i] != s1->qps[1]) {
2610
                qps_changed = 1;
2611
                memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
2612
            }
2613
        }
2614
2615
        if (s->qps[0] != s1->qps[0])
2616
            memcpy(&s->bounding_values_array, &s1->bounding_values_array,
2617
                   sizeof(s->bounding_values_array));
2618
2619
        if (qps_changed) {
2620
            memcpy(s->qps,      s1->qps,      sizeof(s->qps));
2621
            memcpy(s->last_qps, s1->last_qps, sizeof(s->last_qps));
2622
            s->nqps = s1->nqps;
2623
        }
2624
    }
2625
2626
    return update_frames(dst);
2627
}
2628
#endif
2629
2630
164
static int vp3_decode_frame(AVCodecContext *avctx,
2631
                            void *data, int *got_frame,
2632
                            AVPacket *avpkt)
2633
{
2634
164
    AVFrame     *frame  = data;
2635
164
    const uint8_t *buf  = avpkt->data;
2636
164
    int buf_size        = avpkt->size;
2637
164
    Vp3DecodeContext *s = avctx->priv_data;
2638
    GetBitContext gb;
2639
    int i, ret;
2640
2641
164
    if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
2642
        return ret;
2643
2644
#if CONFIG_THEORA_DECODER
2645

164
    if (s->theora && get_bits1(&gb)) {
2646
        int type = get_bits(&gb, 7);
2647
        skip_bits_long(&gb, 6*8); /* "theora" */
2648
2649
        if (s->avctx->active_thread_type&FF_THREAD_FRAME) {
2650
            av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2651
            return AVERROR_PATCHWELCOME;
2652
        }
2653
        if (type == 0) {
2654
            vp3_decode_end(avctx);
2655
            ret = theora_decode_header(avctx, &gb);
2656
2657
            if (ret >= 0)
2658
                ret = vp3_decode_init(avctx);
2659
            if (ret < 0) {
2660
                vp3_decode_end(avctx);
2661
                return ret;
2662
            }
2663
            return buf_size;
2664
        } else if (type == 2) {
2665
            vp3_decode_end(avctx);
2666
            ret = theora_decode_tables(avctx, &gb);
2667
            if (ret >= 0)
2668
                ret = vp3_decode_init(avctx);
2669
            if (ret < 0) {
2670
                vp3_decode_end(avctx);
2671
                return ret;
2672
            }
2673
            return buf_size;
2674
        }
2675
2676
        av_log(avctx, AV_LOG_ERROR,
2677
               "Header packet passed to frame decoder, skipping\n");
2678
        return -1;
2679
    }
2680
#endif
2681
2682
164
    s->keyframe = !get_bits1(&gb);
2683
164
    if (!s->all_fragments) {
2684
        av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
2685
        return -1;
2686
    }
2687
164
    if (!s->theora)
2688
138
        skip_bits(&gb, 1);
2689
656
    for (i = 0; i < 3; i++)
2690
492
        s->last_qps[i] = s->qps[i];
2691
2692
164
    s->nqps = 0;
2693
    do {
2694
164
        s->qps[s->nqps++] = get_bits(&gb, 6);
2695

164
    } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
2696
492
    for (i = s->nqps; i < 3; i++)
2697
328
        s->qps[i] = -1;
2698
2699
164
    if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2700
        av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2701
               s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
2702
2703

302
    s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
2704
138
                          avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
2705
138
                                                                  : AVDISCARD_NONKEY);
2706
2707
164
    if (s->qps[0] != s->last_qps[0])
2708
62
        init_loop_filter(s);
2709
2710
328
    for (i = 0; i < s->nqps; i++)
2711
        // reinit all dequantizers if the first one changed, because
2712
        // the DC of the first quantizer must be used for all matrices
2713

164
        if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
2714
62
            init_dequantizer(s, i);
2715
2716

164
    if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2717
        return buf_size;
2718
2719
328
    s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2720
164
                                                : AV_PICTURE_TYPE_P;
2721
164
    s->current_frame.f->key_frame = s->keyframe;
2722
164
    if ((ret = ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF)) < 0)
2723
        goto error;
2724
2725
164
    if (!s->edge_emu_buffer)
2726
5
        s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
2727
2728
164
    if (s->keyframe) {
2729
6
        if (!s->theora) {
2730
3
            skip_bits(&gb, 4); /* width code */
2731
3
            skip_bits(&gb, 4); /* height code */
2732
3
            if (s->version) {
2733
3
                s->version = get_bits(&gb, 5);
2734
3
                if (avctx->frame_number == 0)
2735
2
                    av_log(s->avctx, AV_LOG_DEBUG,
2736
                           "VP version: %d\n", s->version);
2737
            }
2738
        }
2739

6
        if (s->version || s->theora) {
2740
6
            if (get_bits1(&gb))
2741
                av_log(s->avctx, AV_LOG_ERROR,
2742
                       "Warning, unsupported keyframe coding type?!\n");
2743
6
            skip_bits(&gb, 2); /* reserved? */
2744
2745
#if CONFIG_VP4_DECODER
2746
6
            if (s->version >= 2) {
2747
                int mb_height, mb_width;
2748
                int mb_width_mul, mb_width_div, mb_height_mul, mb_height_div;
2749
2750
2
                mb_height = get_bits(&gb, 8);
2751
2
                mb_width  = get_bits(&gb, 8);
2752
2
                if (mb_height != s->macroblock_height ||
2753
2
                    mb_width != s->macroblock_width)
2754
                    avpriv_request_sample(s->avctx, "macroblock dimension mismatch");
2755
2756
2
                mb_width_mul = get_bits(&gb, 5);
2757
2
                mb_width_div = get_bits(&gb, 3);
2758
2
                mb_height_mul = get_bits(&gb, 5);
2759
2
                mb_height_div = get_bits(&gb, 3);
2760


2
                if (mb_width_mul != 1 || mb_width_div != 1 || mb_height_mul != 1 || mb_height_div != 1)
2761
                    avpriv_request_sample(s->avctx, "unexpected macroblock dimension multipler/divider");
2762
2763
2
                if (get_bits(&gb, 2))
2764
                    avpriv_request_sample(s->avctx, "unknown bits");
2765
            }
2766
#endif
2767
        }
2768
    } else {
2769
158
        if (!s->golden_frame.f->data[0]) {
2770
            av_log(s->avctx, AV_LOG_WARNING,
2771
                   "vp3: first frame not a keyframe\n");
2772
2773
            s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
2774
            if ((ret = ff_thread_get_buffer(avctx, &s->golden_frame,
2775
                                     AV_GET_BUFFER_FLAG_REF)) < 0)
2776
                goto error;
2777
            ff_thread_release_buffer(avctx, &s->last_frame);
2778
            if ((ret = ff_thread_ref_frame(&s->last_frame,
2779
                                           &s->golden_frame)) < 0)
2780
                goto error;
2781
            ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
2782
        }
2783
    }
2784
2785
164
    memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2786
164
    ff_thread_finish_setup(avctx);
2787
2788
164
    if (s->version < 2) {
2789
140
        if ((ret = unpack_superblocks(s, &gb)) < 0) {
2790
            av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2791
            goto error;
2792
        }
2793
#if CONFIG_VP4_DECODER
2794
    } else {
2795
24
        if ((ret = vp4_unpack_macroblocks(s, &gb)) < 0) {
2796
            av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_macroblocks\n");
2797
            goto error;
2798
    }
2799
#endif
2800
    }
2801
164
    if ((ret = unpack_modes(s, &gb)) < 0) {
2802
        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2803
        goto error;
2804
    }
2805
164
    if (ret = unpack_vectors(s, &gb)) {
2806
        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2807
        goto error;
2808
    }
2809
164
    if ((ret = unpack_block_qpis(s, &gb)) < 0) {
2810
        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2811
        goto error;
2812
    }
2813
2814
164
    if (s->version < 2) {
2815
140
        if ((ret = unpack_dct_coeffs(s, &gb)) < 0) {
2816
            av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2817
            goto error;
2818
        }
2819
#if CONFIG_VP4_DECODER
2820
    } else {
2821
24
        if ((ret = vp4_unpack_dct_coeffs(s, &gb)) < 0) {
2822
            av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_dct_coeffs\n");
2823
            goto error;
2824
        }
2825
#endif
2826
    }
2827
2828
656
    for (i = 0; i < 3; i++) {
2829

492
        int height = s->height >> (i && s->chroma_y_shift);
2830
492
        if (s->flipped_image)
2831
            s->data_offset[i] = 0;
2832
        else
2833
492
            s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
2834
    }
2835
2836
164
    s->last_slice_end = 0;
2837
1232
    for (i = 0; i < s->c_superblock_height; i++)
2838
1068
        render_slice(s, i);
2839
2840
    // filter the last row
2841
164
    if (s->version < 2)
2842
560
        for (i = 0; i < 3; i++) {
2843

420
            int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
2844
420
            apply_loop_filter(s, i, row, row + 1);
2845
        }
2846
164
    vp3_draw_horiz_band(s, s->height);
2847
2848
    /* output frame, offset as needed */
2849
164
    if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
2850
        return ret;
2851
2852
164
    frame->crop_left   = s->offset_x;
2853
164
    frame->crop_right  = avctx->coded_width - avctx->width - s->offset_x;
2854
164
    frame->crop_top    = s->offset_y;
2855
164
    frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y;
2856
2857
164
    *got_frame = 1;
2858
2859
164
    if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
2860
164
        ret = update_frames(avctx);
2861
164
        if (ret < 0)
2862
            return ret;
2863
    }
2864
2865
164
    return buf_size;
2866
2867
error:
2868
    ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
2869
2870
    if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
2871
        av_frame_unref(s->current_frame.f);
2872
2873
    return ret;
2874
}
2875
2876
34720
static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
2877
{
2878
34720
    Vp3DecodeContext *s = avctx->priv_data;
2879
2880
34720
    if (get_bits1(gb)) {
2881
        int token;
2882
17920
        if (s->entries >= 32) { /* overflow */
2883
            av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2884
            return -1;
2885
        }
2886
17920
        token = get_bits(gb, 5);
2887
        ff_dlog(avctx, "hti %d hbits %x token %d entry : %d size %d\n",
2888
                s->hti, s->hbits, token, s->entries, s->huff_code_size);
2889
17920
        s->huffman_table[s->hti][token][0] = s->hbits;
2890
17920
        s->huffman_table[s->hti][token][1] = s->huff_code_size;
2891
17920
        s->entries++;
2892
    } else {
2893
16800
        if (s->huff_code_size >= 32) { /* overflow */
2894
            av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2895
            return -1;
2896
        }
2897
16800
        s->huff_code_size++;
2898
16800
        s->hbits <<= 1;
2899
16800
        if (read_huffman_tree(avctx, gb))
2900
            return -1;
2901
16800
        s->hbits |= 1;
2902
16800
        if (read_huffman_tree(avctx, gb))
2903
            return -1;
2904
16800
        s->hbits >>= 1;
2905
16800
        s->huff_code_size--;
2906
    }
2907
34720
    return 0;
2908
}
2909
2910
#if CONFIG_THEORA_DECODER
2911
static const enum AVPixelFormat theora_pix_fmts[4] = {
2912
    AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
2913
};
2914
2915
7
static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2916
{
2917
7
    Vp3DecodeContext *s = avctx->priv_data;
2918
    int visible_width, visible_height, colorspace;
2919
7
    uint8_t offset_x = 0, offset_y = 0;
2920
    int ret;
2921
    AVRational fps, aspect;
2922
2923
7
    s->theora_header = 0;
2924
7
    s->theora = get_bits(gb, 24);
2925
7
    av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2926
7
    if (!s->theora) {
2927
        s->theora = 1;
2928
        avpriv_request_sample(s->avctx, "theora 0");
2929
    }
2930
2931
    /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
2932
     * but previous versions have the image flipped relative to vp3 */
2933
7
    if (s->theora < 0x030200) {
2934
        s->flipped_image = 1;
2935
        av_log(avctx, AV_LOG_DEBUG,
2936
               "Old (<alpha3) Theora bitstream, flipped image\n");
2937
    }
2938
2939
7
    visible_width  =
2940
7
    s->width       = get_bits(gb, 16) << 4;
2941
7
    visible_height =
2942
7
    s->height      = get_bits(gb, 16) << 4;
2943
2944
7
    if (s->theora >= 0x030200) {
2945
7
        visible_width  = get_bits(gb, 24);
2946
7
        visible_height = get_bits(gb, 24);
2947
2948
7
        offset_x = get_bits(gb, 8); /* offset x */
2949
7
        offset_y = get_bits(gb, 8); /* offset y, from bottom */
2950
    }
2951
2952
    /* sanity check */
2953
7
    if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 ||
2954
7
        visible_width  + offset_x > s->width ||
2955
7
        visible_height + offset_y > s->height) {
2956
        av_log(avctx, AV_LOG_ERROR,
2957
               "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
2958
               visible_width, visible_height, offset_x, offset_y,
2959
               s->width, s->height);
2960
        return AVERROR_INVALIDDATA;
2961
    }
2962
2963
7
    fps.num = get_bits_long(gb, 32);
2964
7
    fps.den = get_bits_long(gb, 32);
2965

7
    if (fps.num && fps.den) {
2966

7
        if (fps.num < 0 || fps.den < 0) {
2967
            av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
2968
            return AVERROR_INVALIDDATA;
2969
        }
2970
7
        av_reduce(&avctx->framerate.den, &avctx->framerate.num,
2971
7
                  fps.den, fps.num, 1 << 30);
2972
    }
2973
2974
7
    aspect.num = get_bits(gb, 24);
2975
7
    aspect.den = get_bits(gb, 24);
2976

7
    if (aspect.num && aspect.den) {
2977
5
        av_reduce(&avctx->sample_aspect_ratio.num,
2978
                  &avctx->sample_aspect_ratio.den,
2979
5
                  aspect.num, aspect.den, 1 << 30);
2980
5
        ff_set_sar(avctx, avctx->sample_aspect_ratio);
2981
    }
2982
2983
7
    if (s->theora < 0x030200)
2984
        skip_bits(gb, 5); /* keyframe frequency force */
2985
7
    colorspace = get_bits(gb, 8);
2986
7
    skip_bits(gb, 24); /* bitrate */
2987
2988
7
    skip_bits(gb, 6); /* quality hint */
2989
2990
7
    if (s->theora >= 0x030200) {
2991
7
        skip_bits(gb, 5); /* keyframe frequency force */
2992
7
        avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
2993
7
        if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
2994
            av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
2995
            return AVERROR_INVALIDDATA;
2996
        }
2997
7
        skip_bits(gb, 3); /* reserved */
2998
    } else
2999
        avctx->pix_fmt = AV_PIX_FMT_YUV420P;
3000
3001
7
    ret = ff_set_dimensions(avctx, s->width, s->height);
3002
7
    if (ret < 0)
3003
        return ret;
3004
7
    if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP)) {
3005
7
        avctx->width  = visible_width;
3006
7
        avctx->height = visible_height;
3007
        // translate offsets from theora axis ([0,0] lower left)
3008
        // to normal axis ([0,0] upper left)
3009
7
        s->offset_x = offset_x;
3010
7
        s->offset_y = s->height - visible_height - offset_y;
3011
    }
3012
3013
7
    if (colorspace == 1)
3014
        avctx->color_primaries = AVCOL_PRI_BT470M;
3015
7
    else if (colorspace == 2)
3016
        avctx->color_primaries = AVCOL_PRI_BT470BG;
3017
3018

7
    if (colorspace == 1 || colorspace == 2) {
3019
        avctx->colorspace = AVCOL_SPC_BT470BG;
3020
        avctx->color_trc  = AVCOL_TRC_BT709;
3021
    }
3022
3023
7
    s->theora_header = 1;
3024
7
    return 0;
3025
}
3026
3027
7
static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
3028
{
3029
7
    Vp3DecodeContext *s = avctx->priv_data;
3030
    int i, n, matrices, inter, plane;
3031
3032
7
    if (!s->theora_header)
3033
        return AVERROR_INVALIDDATA;
3034
3035
7
    if (s->theora >= 0x030200) {
3036
7
        n = get_bits(gb, 3);
3037
        /* loop filter limit values table */
3038
7
        if (n)
3039
455
            for (i = 0; i < 64; i++)
3040
448
                s->filter_limit_values[i] = get_bits(gb, n);
3041
    }
3042
3043
7
    if (s->theora >= 0x030200)
3044
7
        n = get_bits(gb, 4) + 1;
3045
    else
3046
        n = 16;
3047
    /* quality threshold table */
3048
455
    for (i = 0; i < 64; i++)
3049
448
        s->coded_ac_scale_factor[i] = get_bits(gb, n);
3050
3051
7
    if (s->theora >= 0x030200)
3052
7
        n = get_bits(gb, 4) + 1;
3053
    else
3054
        n = 16;
3055
    /* dc scale factor table */
3056
455
    for (i = 0; i < 64; i++)
3057
448
        s->coded_dc_scale_factor[0][i] =
3058
448
        s->coded_dc_scale_factor[1][i] = get_bits(gb, n);
3059
3060
7
    if (s->theora >= 0x030200)
3061
7
        matrices = get_bits(gb, 9) + 1;
3062
    else
3063
        matrices = 3;
3064
3065
7
    if (matrices > 384) {
3066
        av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
3067
        return -1;
3068
    }
3069
3070
52
    for (n = 0; n < matrices; n++)
3071
2925
        for (i = 0; i < 64; i++)
3072
2880
            s->base_matrix[n][i] = get_bits(gb, 8);
3073
3074
21
    for (inter = 0; inter <= 1; inter++) {
3075
56
        for (plane = 0; plane <= 2; plane++) {
3076
42
            int newqr = 1;
3077

42
            if (inter || plane > 0)
3078
35
                newqr = get_bits1(gb);
3079
42
            if (!newqr) {
3080
                int qtj, plj;
3081

23
                if (inter && get_bits1(gb)) {
3082
6
                    qtj = 0;
3083
6
                    plj = plane;
3084
                } else {
3085
17
                    qtj = (3 * inter + plane - 1) / 3;
3086
17
                    plj = (plane + 2) % 3;
3087
                }
3088
23
                s->qr_count[inter][plane] = s->qr_count[qtj][plj];
3089
23
                memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
3090
                       sizeof(s->qr_size[0][0]));
3091
23
                memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
3092
                       sizeof(s->qr_base[0][0]));
3093
            } else {
3094
19
                int qri = 0;
3095
19
                int qi  = 0;
3096
3097
                for (;;) {
3098
62
                    i = get_bits(gb, av_log2(matrices - 1) + 1);
3099
62
                    if (i >= matrices) {
3100
                        av_log(avctx, AV_LOG_ERROR,
3101
                               "invalid base matrix index\n");
3102
                        return -1;
3103
                    }
3104
62
                    s->qr_base[inter][plane][qri] = i;
3105
62
                    if (qi >= 63)
3106
19
                        break;
3107
43
                    i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
3108
43
                    s->qr_size[inter][plane][qri++] = i;
3109
43
                    qi += i;
3110
                }
3111
3112
19
                if (qi > 63) {
3113
                    av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
3114
                    return -1;
3115
                }
3116
19
                s->qr_count[inter][plane] = qri;
3117
            }
3118
        }
3119
    }
3120
3121
    /* Huffman tables */
3122
567
    for (s->hti = 0; s->hti < 80; s->hti++) {
3123
560
        s->entries        = 0;
3124
560
        s->huff_code_size = 1;
3125
560
        if (!get_bits1(gb)) {
3126
560
            s->hbits = 0;
3127
560
            if (read_huffman_tree(avctx, gb))
3128
                return -1;
3129
560
            s->hbits = 1;
3130
560
            if (read_huffman_tree(avctx, gb))
3131
                return -1;
3132
        }
3133
    }
3134
3135
7
    s->theora_tables = 1;
3136
3137
7
    return 0;
3138
}
3139
3140
7
static av_cold int theora_decode_init(AVCodecContext *avctx)
3141
{
3142
7
    Vp3DecodeContext *s = avctx->priv_data;
3143
    GetBitContext gb;
3144
    int ptype;
3145
    const uint8_t *header_start[3];
3146
    int header_len[3];
3147
    int i;
3148
    int ret;
3149
3150
7
    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
3151
3152
7
    s->theora = 1;
3153
3154
7
    if (!avctx->extradata_size) {
3155
        av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
3156
        return -1;
3157
    }
3158
3159
7
    if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
3160
                                  42, header_start, header_len) < 0) {
3161
        av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
3162
        return -1;
3163
    }
3164
3165
28
    for (i = 0; i < 3; i++) {
3166
21
        if (header_len[i] <= 0)
3167
            continue;
3168
21
        ret = init_get_bits8(&gb, header_start[i], header_len[i]);
3169
21
        if (ret < 0)
3170
            return ret;
3171
3172
21
        ptype = get_bits(&gb, 8);
3173
3174
21
        if (!(ptype & 0x80)) {
3175
            av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
3176
//          return -1;
3177
        }
3178
3179
        // FIXME: Check for this as well.
3180
21
        skip_bits_long(&gb, 6 * 8); /* "theora" */
3181
3182

21
        switch (ptype) {
3183
7
        case 0x80:
3184
7
            if (theora_decode_header(avctx, &gb) < 0)
3185
                return -1;
3186
7
            break;
3187
7
        case 0x81:
3188
// FIXME: is this needed? it breaks sometimes
3189
//            theora_decode_comments(avctx, gb);
3190
7
            break;
3191
7
        case 0x82:
3192
7
            if (theora_decode_tables(avctx, &gb))
3193
                return -1;
3194
7
            break;
3195
        default:
3196
            av_log(avctx, AV_LOG_ERROR,
3197
                   "Unknown Theora config packet: %d\n", ptype & ~0x80);
3198
            break;
3199
        }
3200

21
        if (ptype != 0x81 && 8 * header_len[i] != get_bits_count(&gb))
3201
7
            av_log(avctx, AV_LOG_WARNING,
3202
                   "%d bits left in packet %X\n",
3203
7
                   8 * header_len[i] - get_bits_count(&gb), ptype);
3204
21
        if (s->theora < 0x030200)
3205
            break;
3206
    }
3207
3208
7
    return vp3_decode_init(avctx);
3209
}
3210
3211
AVCodec ff_theora_decoder = {
3212
    .name                  = "theora",
3213
    .long_name             = NULL_IF_CONFIG_SMALL("Theora"),
3214
    .type                  = AVMEDIA_TYPE_VIDEO,
3215
    .id                    = AV_CODEC_ID_THEORA,
3216
    .priv_data_size        = sizeof(Vp3DecodeContext),
3217
    .init                  = theora_decode_init,
3218
    .close                 = vp3_decode_end,
3219
    .decode                = vp3_decode_frame,
3220
    .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
3221
                             AV_CODEC_CAP_FRAME_THREADS,
3222
    .flush                 = vp3_decode_flush,
3223
    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
3224
    .caps_internal         = FF_CODEC_CAP_EXPORTS_CROPPING | FF_CODEC_CAP_ALLOCATE_PROGRESS,
3225
};
3226
#endif
3227
3228
AVCodec ff_vp3_decoder = {
3229
    .name                  = "vp3",
3230
    .long_name             = NULL_IF_CONFIG_SMALL("On2 VP3"),
3231
    .type                  = AVMEDIA_TYPE_VIDEO,
3232
    .id                    = AV_CODEC_ID_VP3,
3233
    .priv_data_size        = sizeof(Vp3DecodeContext),
3234
    .init                  = vp3_decode_init,
3235
    .close                 = vp3_decode_end,
3236
    .decode                = vp3_decode_frame,
3237
    .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
3238
                             AV_CODEC_CAP_FRAME_THREADS,
3239
    .flush                 = vp3_decode_flush,
3240
    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
3241
    .caps_internal         = FF_CODEC_CAP_ALLOCATE_PROGRESS,
3242
};
3243
3244
#if CONFIG_VP4_DECODER
3245
AVCodec ff_vp4_decoder = {
3246
    .name                  = "vp4",
3247
    .long_name             = NULL_IF_CONFIG_SMALL("On2 VP4"),
3248
    .type                  = AVMEDIA_TYPE_VIDEO,
3249
    .id                    = AV_CODEC_ID_VP4,
3250
    .priv_data_size        = sizeof(Vp3DecodeContext),
3251
    .init                  = vp3_decode_init,
3252
    .close                 = vp3_decode_end,
3253
    .decode                = vp3_decode_frame,
3254
    .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
3255
                             AV_CODEC_CAP_FRAME_THREADS,
3256
    .flush                 = vp3_decode_flush,
3257
    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
3258
    .caps_internal         = FF_CODEC_CAP_ALLOCATE_PROGRESS,
3259
};
3260
#endif