]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp3.c
dxva2: Keep code shared between dxva2 and d3d11va under the correct #if
[ffmpeg] / libavcodec / vp3.c
1 /*
2  * Copyright (C) 2003-2004 The FFmpeg project
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * On2 VP3 Video Decoder
24  *
25  * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
26  * For more information about the VP3 coding process, visit:
27  *   http://wiki.multimedia.cx/index.php?title=On2_VP3
28  *
29  * Theora decoder by Alex Beregszaszi
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "libavutil/imgutils.h"
37
38 #include "avcodec.h"
39 #include "get_bits.h"
40 #include "hpeldsp.h"
41 #include "internal.h"
42 #include "mathops.h"
43 #include "thread.h"
44 #include "videodsp.h"
45 #include "vp3data.h"
46 #include "vp3dsp.h"
47 #include "xiph.h"
48
49 #define FRAGMENT_PIXELS 8
50
51 // FIXME split things out into their own arrays
52 typedef struct Vp3Fragment {
53     int16_t dc;
54     uint8_t coding_method;
55     uint8_t qpi;
56 } Vp3Fragment;
57
58 #define SB_NOT_CODED        0
59 #define SB_PARTIALLY_CODED  1
60 #define SB_FULLY_CODED      2
61
62 // This is the maximum length of a single long bit run that can be encoded
63 // for superblock coding or block qps. Theora special-cases this to read a
64 // bit instead of flipping the current bit to allow for runs longer than 4129.
65 #define MAXIMUM_LONG_BIT_RUN 4129
66
67 #define MODE_INTER_NO_MV      0
68 #define MODE_INTRA            1
69 #define MODE_INTER_PLUS_MV    2
70 #define MODE_INTER_LAST_MV    3
71 #define MODE_INTER_PRIOR_LAST 4
72 #define MODE_USING_GOLDEN     5
73 #define MODE_GOLDEN_MV        6
74 #define MODE_INTER_FOURMV     7
75 #define CODING_MODE_COUNT     8
76
77 /* special internal mode */
78 #define MODE_COPY             8
79
80 /* There are 6 preset schemes, plus a free-form scheme */
81 static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
82     /* scheme 1: Last motion vector dominates */
83     { MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
84       MODE_INTER_PLUS_MV,    MODE_INTER_NO_MV,
85       MODE_INTRA,            MODE_USING_GOLDEN,
86       MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
87
88     /* scheme 2 */
89     { MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
90       MODE_INTER_NO_MV,      MODE_INTER_PLUS_MV,
91       MODE_INTRA,            MODE_USING_GOLDEN,
92       MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
93
94     /* scheme 3 */
95     { MODE_INTER_LAST_MV,    MODE_INTER_PLUS_MV,
96       MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
97       MODE_INTRA,            MODE_USING_GOLDEN,
98       MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
99
100     /* scheme 4 */
101     { MODE_INTER_LAST_MV,    MODE_INTER_PLUS_MV,
102       MODE_INTER_NO_MV,      MODE_INTER_PRIOR_LAST,
103       MODE_INTRA,            MODE_USING_GOLDEN,
104       MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
105
106     /* scheme 5: No motion vector dominates */
107     { MODE_INTER_NO_MV,      MODE_INTER_LAST_MV,
108       MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
109       MODE_INTRA,            MODE_USING_GOLDEN,
110       MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
111
112     /* scheme 6 */
113     { MODE_INTER_NO_MV,      MODE_USING_GOLDEN,
114       MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
115       MODE_INTER_PLUS_MV,    MODE_INTRA,
116       MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
117 };
118
119 static const uint8_t hilbert_offset[16][2] = {
120     { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
121     { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
122     { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
123     { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
124 };
125
126 #define MIN_DEQUANT_VAL 2
127
128 typedef struct Vp3DecodeContext {
129     AVCodecContext *avctx;
130     int theora, theora_tables;
131     int version;
132     int width, height;
133     int chroma_x_shift, chroma_y_shift;
134     ThreadFrame golden_frame;
135     ThreadFrame last_frame;
136     ThreadFrame current_frame;
137     int keyframe;
138     uint8_t idct_permutation[64];
139     uint8_t idct_scantable[64];
140     HpelDSPContext hdsp;
141     VideoDSPContext vdsp;
142     VP3DSPContext vp3dsp;
143     DECLARE_ALIGNED(16, int16_t, block)[64];
144     int flipped_image;
145     int last_slice_end;
146     int skip_loop_filter;
147
148     int qps[3];
149     int nqps;
150     int last_qps[3];
151
152     int superblock_count;
153     int y_superblock_width;
154     int y_superblock_height;
155     int y_superblock_count;
156     int c_superblock_width;
157     int c_superblock_height;
158     int c_superblock_count;
159     int u_superblock_start;
160     int v_superblock_start;
161     unsigned char *superblock_coding;
162
163     int macroblock_count;
164     int macroblock_width;
165     int macroblock_height;
166
167     int fragment_count;
168     int fragment_width[2];
169     int fragment_height[2];
170
171     Vp3Fragment *all_fragments;
172     int fragment_start[3];
173     int data_offset[3];
174     uint8_t offset_x;
175     uint8_t offset_y;
176
177     int8_t (*motion_val[2])[2];
178
179     /* tables */
180     uint16_t coded_dc_scale_factor[64];
181     uint32_t coded_ac_scale_factor[64];
182     uint8_t base_matrix[384][64];
183     uint8_t qr_count[2][3];
184     uint8_t qr_size[2][3][64];
185     uint16_t qr_base[2][3][64];
186
187     /**
188      * This is a list of all tokens in bitstream order. Reordering takes place
189      * by pulling from each level during IDCT. As a consequence, IDCT must be
190      * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
191      * otherwise. The 32 different tokens with up to 12 bits of extradata are
192      * collapsed into 3 types, packed as follows:
193      *   (from the low to high bits)
194      *
195      * 2 bits: type (0,1,2)
196      *   0: EOB run, 14 bits for run length (12 needed)
197      *   1: zero run, 7 bits for run length
198      *                7 bits for the next coefficient (3 needed)
199      *   2: coefficient, 14 bits (11 needed)
200      *
201      * Coefficients are signed, so are packed in the highest bits for automatic
202      * sign extension.
203      */
204     int16_t *dct_tokens[3][64];
205     int16_t *dct_tokens_base;
206 #define TOKEN_EOB(eob_run)              ((eob_run) << 2)
207 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
208 #define TOKEN_COEFF(coeff)              (((coeff) << 2) + 2)
209
210     /**
211      * number of blocks that contain DCT coefficients at
212      * the given level or higher
213      */
214     int num_coded_frags[3][64];
215     int total_num_coded_frags;
216
217     /* this is a list of indexes into the all_fragments array indicating
218      * which of the fragments are coded */
219     int *coded_fragment_list[3];
220
221     VLC dc_vlc[16];
222     VLC ac_vlc_1[16];
223     VLC ac_vlc_2[16];
224     VLC ac_vlc_3[16];
225     VLC ac_vlc_4[16];
226
227     VLC superblock_run_length_vlc;
228     VLC fragment_run_length_vlc;
229     VLC mode_code_vlc;
230     VLC motion_vector_vlc;
231
232     /* these arrays need to be on 16-byte boundaries since SSE2 operations
233      * index into them */
234     DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64];     ///< qmat[qpi][is_inter][plane]
235
236     /* This table contains superblock_count * 16 entries. Each set of 16
237      * numbers corresponds to the fragment indexes 0..15 of the superblock.
238      * An entry will be -1 to indicate that no entry corresponds to that
239      * index. */
240     int *superblock_fragments;
241
242     /* This is an array that indicates how a particular macroblock
243      * is coded. */
244     unsigned char *macroblock_coding;
245
246     uint8_t *edge_emu_buffer;
247
248     /* Huffman decode */
249     int hti;
250     unsigned int hbits;
251     int entries;
252     int huff_code_size;
253     uint32_t huffman_table[80][32][2];
254
255     uint8_t filter_limit_values[64];
256     DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2];
257 } Vp3DecodeContext;
258
259 /************************************************************************
260  * VP3 specific functions
261  ************************************************************************/
262
263 static void vp3_decode_flush(AVCodecContext *avctx)
264 {
265     Vp3DecodeContext *s = avctx->priv_data;
266
267     if (s->golden_frame.f)
268         ff_thread_release_buffer(avctx, &s->golden_frame);
269     if (s->last_frame.f)
270         ff_thread_release_buffer(avctx, &s->last_frame);
271     if (s->current_frame.f)
272         ff_thread_release_buffer(avctx, &s->current_frame);
273 }
274
275 static av_cold int vp3_decode_end(AVCodecContext *avctx)
276 {
277     Vp3DecodeContext *s = avctx->priv_data;
278     int i;
279
280     av_freep(&s->superblock_coding);
281     av_freep(&s->all_fragments);
282     av_freep(&s->coded_fragment_list[0]);
283     av_freep(&s->dct_tokens_base);
284     av_freep(&s->superblock_fragments);
285     av_freep(&s->macroblock_coding);
286     av_freep(&s->motion_val[0]);
287     av_freep(&s->motion_val[1]);
288     av_freep(&s->edge_emu_buffer);
289
290     /* release all frames */
291     vp3_decode_flush(avctx);
292     av_frame_free(&s->current_frame.f);
293     av_frame_free(&s->last_frame.f);
294     av_frame_free(&s->golden_frame.f);
295
296     if (avctx->internal->is_copy)
297         return 0;
298
299     for (i = 0; i < 16; i++) {
300         ff_free_vlc(&s->dc_vlc[i]);
301         ff_free_vlc(&s->ac_vlc_1[i]);
302         ff_free_vlc(&s->ac_vlc_2[i]);
303         ff_free_vlc(&s->ac_vlc_3[i]);
304         ff_free_vlc(&s->ac_vlc_4[i]);
305     }
306
307     ff_free_vlc(&s->superblock_run_length_vlc);
308     ff_free_vlc(&s->fragment_run_length_vlc);
309     ff_free_vlc(&s->mode_code_vlc);
310     ff_free_vlc(&s->motion_vector_vlc);
311
312     return 0;
313 }
314
315 /*
316  * This function sets up all of the various blocks mappings:
317  * superblocks <-> fragments, macroblocks <-> fragments,
318  * superblocks <-> macroblocks
319  *
320  * @return 0 is successful; returns 1 if *anything* went wrong.
321  */
322 static int init_block_mapping(Vp3DecodeContext *s)
323 {
324     int sb_x, sb_y, plane;
325     int x, y, i, j = 0;
326
327     for (plane = 0; plane < 3; plane++) {
328         int sb_width    = plane ? s->c_superblock_width
329                                 : s->y_superblock_width;
330         int sb_height   = plane ? s->c_superblock_height
331                                 : s->y_superblock_height;
332         int frag_width  = s->fragment_width[!!plane];
333         int frag_height = s->fragment_height[!!plane];
334
335         for (sb_y = 0; sb_y < sb_height; sb_y++)
336             for (sb_x = 0; sb_x < sb_width; sb_x++)
337                 for (i = 0; i < 16; i++) {
338                     x = 4 * sb_x + hilbert_offset[i][0];
339                     y = 4 * sb_y + hilbert_offset[i][1];
340
341                     if (x < frag_width && y < frag_height)
342                         s->superblock_fragments[j++] = s->fragment_start[plane] +
343                                                        y * frag_width + x;
344                     else
345                         s->superblock_fragments[j++] = -1;
346                 }
347     }
348
349     return 0;  /* successful path out */
350 }
351
352 /*
353  * This function sets up the dequantization tables used for a particular
354  * frame.
355  */
356 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
357 {
358     int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
359     int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
360     int i, plane, inter, qri, bmi, bmj, qistart;
361
362     for (inter = 0; inter < 2; inter++) {
363         for (plane = 0; plane < 3; plane++) {
364             int sum = 0;
365             for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
366                 sum += s->qr_size[inter][plane][qri];
367                 if (s->qps[qpi] <= sum)
368                     break;
369             }
370             qistart = sum - s->qr_size[inter][plane][qri];
371             bmi     = s->qr_base[inter][plane][qri];
372             bmj     = s->qr_base[inter][plane][qri + 1];
373             for (i = 0; i < 64; i++) {
374                 int coeff = (2 * (sum     - s->qps[qpi]) * s->base_matrix[bmi][i] -
375                              2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
376                              s->qr_size[inter][plane][qri]) /
377                             (2 * s->qr_size[inter][plane][qri]);
378
379                 int qmin   = 8 << (inter + !i);
380                 int qscale = i ? ac_scale_factor : dc_scale_factor;
381
382                 s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
383                     av_clip((qscale * coeff) / 100 * 4, qmin, 4096);
384             }
385             /* all DC coefficients use the same quant so as not to interfere
386              * with DC prediction */
387             s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
388         }
389     }
390 }
391
392 /*
393  * This function initializes the loop filter boundary limits if the frame's
394  * quality index is different from the previous frame's.
395  *
396  * The filter_limit_values may not be larger than 127.
397  */
398 static void init_loop_filter(Vp3DecodeContext *s)
399 {
400     int *bounding_values = s->bounding_values_array + 127;
401     int filter_limit;
402     int x;
403     int value;
404
405     filter_limit = s->filter_limit_values[s->qps[0]];
406     assert(filter_limit < 128);
407
408     /* set up the bounding values */
409     memset(s->bounding_values_array, 0, 256 * sizeof(int));
410     for (x = 0; x < filter_limit; x++) {
411         bounding_values[-x] = -x;
412         bounding_values[x] = x;
413     }
414     for (x = value = filter_limit; x < 128 && value; x++, value--) {
415         bounding_values[ x] =  value;
416         bounding_values[-x] = -value;
417     }
418     if (value)
419         bounding_values[128] = value;
420     bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
421 }
422
423 /*
424  * This function unpacks all of the superblock/macroblock/fragment coding
425  * information from the bitstream.
426  */
427 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
428 {
429     int superblock_starts[3] = {
430         0, s->u_superblock_start, s->v_superblock_start
431     };
432     int bit = 0;
433     int current_superblock = 0;
434     int current_run = 0;
435     int num_partial_superblocks = 0;
436
437     int i, j;
438     int current_fragment;
439     int plane;
440
441     if (s->keyframe) {
442         memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
443     } else {
444         /* unpack the list of partially-coded superblocks */
445         bit         = get_bits1(gb) ^ 1;
446         current_run = 0;
447
448         while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
449             if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
450                 bit = get_bits1(gb);
451             else
452                 bit ^= 1;
453
454             current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
455                                    6, 2) + 1;
456             if (current_run == 34)
457                 current_run += get_bits(gb, 12);
458
459             if (current_superblock + current_run > s->superblock_count) {
460                 av_log(s->avctx, AV_LOG_ERROR,
461                        "Invalid partially coded superblock run length\n");
462                 return -1;
463             }
464
465             memset(s->superblock_coding + current_superblock, bit, current_run);
466
467             current_superblock += current_run;
468             if (bit)
469                 num_partial_superblocks += current_run;
470         }
471
472         /* unpack the list of fully coded superblocks if any of the blocks were
473          * not marked as partially coded in the previous step */
474         if (num_partial_superblocks < s->superblock_count) {
475             int superblocks_decoded = 0;
476
477             current_superblock = 0;
478             bit                = get_bits1(gb) ^ 1;
479             current_run        = 0;
480
481             while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
482                    get_bits_left(gb) > 0) {
483                 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
484                     bit = get_bits1(gb);
485                 else
486                     bit ^= 1;
487
488                 current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
489                                        6, 2) + 1;
490                 if (current_run == 34)
491                     current_run += get_bits(gb, 12);
492
493                 for (j = 0; j < current_run; current_superblock++) {
494                     if (current_superblock >= s->superblock_count) {
495                         av_log(s->avctx, AV_LOG_ERROR,
496                                "Invalid fully coded superblock run length\n");
497                         return -1;
498                     }
499
500                     /* skip any superblocks already marked as partially coded */
501                     if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
502                         s->superblock_coding[current_superblock] = 2 * bit;
503                         j++;
504                     }
505                 }
506                 superblocks_decoded += current_run;
507             }
508         }
509
510         /* if there were partial blocks, initialize bitstream for
511          * unpacking fragment codings */
512         if (num_partial_superblocks) {
513             current_run = 0;
514             bit         = get_bits1(gb);
515             /* toggle the bit because as soon as the first run length is
516              * fetched the bit will be toggled again */
517             bit ^= 1;
518         }
519     }
520
521     /* figure out which fragments are coded; iterate through each
522      * superblock (all planes) */
523     s->total_num_coded_frags = 0;
524     memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
525
526     for (plane = 0; plane < 3; plane++) {
527         int sb_start = superblock_starts[plane];
528         int sb_end   = sb_start + (plane ? s->c_superblock_count
529                                          : s->y_superblock_count);
530         int num_coded_frags = 0;
531
532         for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
533             /* iterate through all 16 fragments in a superblock */
534             for (j = 0; j < 16; j++) {
535                 /* if the fragment is in bounds, check its coding status */
536                 current_fragment = s->superblock_fragments[i * 16 + j];
537                 if (current_fragment != -1) {
538                     int coded = s->superblock_coding[i];
539
540                     if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
541                         /* fragment may or may not be coded; this is the case
542                          * that cares about the fragment coding runs */
543                         if (current_run-- == 0) {
544                             bit        ^= 1;
545                             current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);
546                         }
547                         coded = bit;
548                     }
549
550                     if (coded) {
551                         /* default mode; actual mode will be decoded in
552                          * the next phase */
553                         s->all_fragments[current_fragment].coding_method =
554                             MODE_INTER_NO_MV;
555                         s->coded_fragment_list[plane][num_coded_frags++] =
556                             current_fragment;
557                     } else {
558                         /* not coded; copy this fragment from the prior frame */
559                         s->all_fragments[current_fragment].coding_method =
560                             MODE_COPY;
561                     }
562                 }
563             }
564         }
565         s->total_num_coded_frags += num_coded_frags;
566         for (i = 0; i < 64; i++)
567             s->num_coded_frags[plane][i] = num_coded_frags;
568         if (plane < 2)
569             s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
570                                                 num_coded_frags;
571     }
572     return 0;
573 }
574
575 /*
576  * This function unpacks all the coding mode data for individual macroblocks
577  * from the bitstream.
578  */
579 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
580 {
581     int i, j, k, sb_x, sb_y;
582     int scheme;
583     int current_macroblock;
584     int current_fragment;
585     int coding_mode;
586     int custom_mode_alphabet[CODING_MODE_COUNT];
587     const int *alphabet;
588     Vp3Fragment *frag;
589
590     if (s->keyframe) {
591         for (i = 0; i < s->fragment_count; i++)
592             s->all_fragments[i].coding_method = MODE_INTRA;
593     } else {
594         /* fetch the mode coding scheme for this frame */
595         scheme = get_bits(gb, 3);
596
597         /* is it a custom coding scheme? */
598         if (scheme == 0) {
599             for (i = 0; i < 8; i++)
600                 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
601             for (i = 0; i < 8; i++)
602                 custom_mode_alphabet[get_bits(gb, 3)] = i;
603             alphabet = custom_mode_alphabet;
604         } else
605             alphabet = ModeAlphabet[scheme - 1];
606
607         /* iterate through all of the macroblocks that contain 1 or more
608          * coded fragments */
609         for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
610             for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
611                 if (get_bits_left(gb) <= 0)
612                     return -1;
613
614                 for (j = 0; j < 4; j++) {
615                     int mb_x = 2 * sb_x + (j >> 1);
616                     int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
617                     current_macroblock = mb_y * s->macroblock_width + mb_x;
618
619                     if (mb_x >= s->macroblock_width ||
620                         mb_y >= s->macroblock_height)
621                         continue;
622
623 #define BLOCK_X (2 * mb_x + (k & 1))
624 #define BLOCK_Y (2 * mb_y + (k >> 1))
625                     /* coding modes are only stored if the macroblock has
626                      * at least one luma block coded, otherwise it must be
627                      * INTER_NO_MV */
628                     for (k = 0; k < 4; k++) {
629                         current_fragment = BLOCK_Y *
630                                            s->fragment_width[0] + BLOCK_X;
631                         if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
632                             break;
633                     }
634                     if (k == 4) {
635                         s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
636                         continue;
637                     }
638
639                     /* mode 7 means get 3 bits for each coding mode */
640                     if (scheme == 7)
641                         coding_mode = get_bits(gb, 3);
642                     else
643                         coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
644
645                     s->macroblock_coding[current_macroblock] = coding_mode;
646                     for (k = 0; k < 4; k++) {
647                         frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
648                         if (frag->coding_method != MODE_COPY)
649                             frag->coding_method = coding_mode;
650                     }
651
652 #define SET_CHROMA_MODES                                                      \
653     if (frag[s->fragment_start[1]].coding_method != MODE_COPY)                \
654         frag[s->fragment_start[1]].coding_method = coding_mode;               \
655     if (frag[s->fragment_start[2]].coding_method != MODE_COPY)                \
656         frag[s->fragment_start[2]].coding_method = coding_mode;
657
658                     if (s->chroma_y_shift) {
659                         frag = s->all_fragments + mb_y *
660                                s->fragment_width[1] + mb_x;
661                         SET_CHROMA_MODES
662                     } else if (s->chroma_x_shift) {
663                         frag = s->all_fragments +
664                                2 * mb_y * s->fragment_width[1] + mb_x;
665                         for (k = 0; k < 2; k++) {
666                             SET_CHROMA_MODES
667                             frag += s->fragment_width[1];
668                         }
669                     } else {
670                         for (k = 0; k < 4; k++) {
671                             frag = s->all_fragments +
672                                    BLOCK_Y * s->fragment_width[1] + BLOCK_X;
673                             SET_CHROMA_MODES
674                         }
675                     }
676                 }
677             }
678         }
679     }
680
681     return 0;
682 }
683
684 /*
685  * This function unpacks all the motion vectors for the individual
686  * macroblocks from the bitstream.
687  */
688 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
689 {
690     int j, k, sb_x, sb_y;
691     int coding_mode;
692     int motion_x[4];
693     int motion_y[4];
694     int last_motion_x = 0;
695     int last_motion_y = 0;
696     int prior_last_motion_x = 0;
697     int prior_last_motion_y = 0;
698     int current_macroblock;
699     int current_fragment;
700     int frag;
701
702     if (s->keyframe)
703         return 0;
704
705     /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
706     coding_mode = get_bits1(gb);
707
708     /* iterate through all of the macroblocks that contain 1 or more
709      * coded fragments */
710     for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
711         for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
712             if (get_bits_left(gb) <= 0)
713                 return -1;
714
715             for (j = 0; j < 4; j++) {
716                 int mb_x = 2 * sb_x + (j >> 1);
717                 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
718                 current_macroblock = mb_y * s->macroblock_width + mb_x;
719
720                 if (mb_x >= s->macroblock_width  ||
721                     mb_y >= s->macroblock_height ||
722                     s->macroblock_coding[current_macroblock] == MODE_COPY)
723                     continue;
724
725                 switch (s->macroblock_coding[current_macroblock]) {
726                 case MODE_INTER_PLUS_MV:
727                 case MODE_GOLDEN_MV:
728                     /* all 6 fragments use the same motion vector */
729                     if (coding_mode == 0) {
730                         motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
731                         motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
732                     } else {
733                         motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
734                         motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
735                     }
736
737                     /* vector maintenance, only on MODE_INTER_PLUS_MV */
738                     if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
739                         prior_last_motion_x = last_motion_x;
740                         prior_last_motion_y = last_motion_y;
741                         last_motion_x       = motion_x[0];
742                         last_motion_y       = motion_y[0];
743                     }
744                     break;
745
746                 case MODE_INTER_FOURMV:
747                     /* vector maintenance */
748                     prior_last_motion_x = last_motion_x;
749                     prior_last_motion_y = last_motion_y;
750
751                     /* fetch 4 vectors from the bitstream, one for each
752                      * Y fragment, then average for the C fragment vectors */
753                     for (k = 0; k < 4; k++) {
754                         current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
755                         if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
756                             if (coding_mode == 0) {
757                                 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
758                                 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
759                             } else {
760                                 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
761                                 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
762                             }
763                             last_motion_x = motion_x[k];
764                             last_motion_y = motion_y[k];
765                         } else {
766                             motion_x[k] = 0;
767                             motion_y[k] = 0;
768                         }
769                     }
770                     break;
771
772                 case MODE_INTER_LAST_MV:
773                     /* all 6 fragments use the last motion vector */
774                     motion_x[0] = last_motion_x;
775                     motion_y[0] = last_motion_y;
776
777                     /* no vector maintenance (last vector remains the
778                      * last vector) */
779                     break;
780
781                 case MODE_INTER_PRIOR_LAST:
782                     /* all 6 fragments use the motion vector prior to the
783                      * last motion vector */
784                     motion_x[0] = prior_last_motion_x;
785                     motion_y[0] = prior_last_motion_y;
786
787                     /* vector maintenance */
788                     prior_last_motion_x = last_motion_x;
789                     prior_last_motion_y = last_motion_y;
790                     last_motion_x       = motion_x[0];
791                     last_motion_y       = motion_y[0];
792                     break;
793
794                 default:
795                     /* covers intra, inter without MV, golden without MV */
796                     motion_x[0] = 0;
797                     motion_y[0] = 0;
798
799                     /* no vector maintenance */
800                     break;
801                 }
802
803                 /* assign the motion vectors to the correct fragments */
804                 for (k = 0; k < 4; k++) {
805                     current_fragment =
806                         BLOCK_Y * s->fragment_width[0] + BLOCK_X;
807                     if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
808                         s->motion_val[0][current_fragment][0] = motion_x[k];
809                         s->motion_val[0][current_fragment][1] = motion_y[k];
810                     } else {
811                         s->motion_val[0][current_fragment][0] = motion_x[0];
812                         s->motion_val[0][current_fragment][1] = motion_y[0];
813                     }
814                 }
815
816                 if (s->chroma_y_shift) {
817                     if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
818                         motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
819                                              motion_x[2] + motion_x[3], 2);
820                         motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
821                                              motion_y[2] + motion_y[3], 2);
822                     }
823                     motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
824                     motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
825                     frag = mb_y * s->fragment_width[1] + mb_x;
826                     s->motion_val[1][frag][0] = motion_x[0];
827                     s->motion_val[1][frag][1] = motion_y[0];
828                 } else if (s->chroma_x_shift) {
829                     if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
830                         motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
831                         motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
832                         motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
833                         motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
834                     } else {
835                         motion_x[1] = motion_x[0];
836                         motion_y[1] = motion_y[0];
837                     }
838                     motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
839                     motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
840
841                     frag = 2 * mb_y * s->fragment_width[1] + mb_x;
842                     for (k = 0; k < 2; k++) {
843                         s->motion_val[1][frag][0] = motion_x[k];
844                         s->motion_val[1][frag][1] = motion_y[k];
845                         frag += s->fragment_width[1];
846                     }
847                 } else {
848                     for (k = 0; k < 4; k++) {
849                         frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
850                         if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
851                             s->motion_val[1][frag][0] = motion_x[k];
852                             s->motion_val[1][frag][1] = motion_y[k];
853                         } else {
854                             s->motion_val[1][frag][0] = motion_x[0];
855                             s->motion_val[1][frag][1] = motion_y[0];
856                         }
857                     }
858                 }
859             }
860         }
861     }
862
863     return 0;
864 }
865
866 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
867 {
868     int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
869     int num_blocks = s->total_num_coded_frags;
870
871     for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
872         i = blocks_decoded = num_blocks_at_qpi = 0;
873
874         bit        = get_bits1(gb) ^ 1;
875         run_length = 0;
876
877         do {
878             if (run_length == MAXIMUM_LONG_BIT_RUN)
879                 bit = get_bits1(gb);
880             else
881                 bit ^= 1;
882
883             run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
884             if (run_length == 34)
885                 run_length += get_bits(gb, 12);
886             blocks_decoded += run_length;
887
888             if (!bit)
889                 num_blocks_at_qpi += run_length;
890
891             for (j = 0; j < run_length; i++) {
892                 if (i >= s->total_num_coded_frags)
893                     return -1;
894
895                 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
896                     s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
897                     j++;
898                 }
899             }
900         } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
901
902         num_blocks -= num_blocks_at_qpi;
903     }
904
905     return 0;
906 }
907
908 /*
909  * This function is called by unpack_dct_coeffs() to extract the VLCs from
910  * the bitstream. The VLCs encode tokens which are used to unpack DCT
911  * data. This function unpacks all the VLCs for either the Y plane or both
912  * C planes, and is called for DC coefficients or different AC coefficient
913  * levels (since different coefficient types require different VLC tables.
914  *
915  * This function returns a residual eob run. E.g, if a particular token gave
916  * instructions to EOB the next 5 fragments and there were only 2 fragments
917  * left in the current fragment range, 3 would be returned so that it could
918  * be passed into the next call to this same function.
919  */
920 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
921                        VLC *table, int coeff_index,
922                        int plane,
923                        int eob_run)
924 {
925     int i, j = 0;
926     int token;
927     int zero_run  = 0;
928     int16_t coeff = 0;
929     int bits_to_get;
930     int blocks_ended;
931     int coeff_i = 0;
932     int num_coeffs      = s->num_coded_frags[plane][coeff_index];
933     int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
934
935     /* local references to structure members to avoid repeated dereferences */
936     int *coded_fragment_list   = s->coded_fragment_list[plane];
937     Vp3Fragment *all_fragments = s->all_fragments;
938     VLC_TYPE(*vlc_table)[2] = table->table;
939
940     if (num_coeffs < 0)
941         av_log(s->avctx, AV_LOG_ERROR,
942                "Invalid number of coefficients at level %d\n", coeff_index);
943
944     if (eob_run > num_coeffs) {
945         coeff_i      =
946         blocks_ended = num_coeffs;
947         eob_run     -= num_coeffs;
948     } else {
949         coeff_i      =
950         blocks_ended = eob_run;
951         eob_run      = 0;
952     }
953
954     // insert fake EOB token to cover the split between planes or zzi
955     if (blocks_ended)
956         dct_tokens[j++] = blocks_ended << 2;
957
958     while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
959         /* decode a VLC into a token */
960         token = get_vlc2(gb, vlc_table, 11, 3);
961         /* use the token to get a zero run, a coefficient, and an eob run */
962         if ((unsigned) token <= 6U) {
963             eob_run = eob_run_base[token];
964             if (eob_run_get_bits[token])
965                 eob_run += get_bits(gb, eob_run_get_bits[token]);
966
967             // record only the number of blocks ended in this plane,
968             // any spill will be recorded in the next plane.
969             if (eob_run > num_coeffs - coeff_i) {
970                 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
971                 blocks_ended   += num_coeffs - coeff_i;
972                 eob_run        -= num_coeffs - coeff_i;
973                 coeff_i         = num_coeffs;
974             } else {
975                 dct_tokens[j++] = TOKEN_EOB(eob_run);
976                 blocks_ended   += eob_run;
977                 coeff_i        += eob_run;
978                 eob_run         = 0;
979             }
980         } else if (token >= 0) {
981             bits_to_get = coeff_get_bits[token];
982             if (bits_to_get)
983                 bits_to_get = get_bits(gb, bits_to_get);
984             coeff = coeff_tables[token][bits_to_get];
985
986             zero_run = zero_run_base[token];
987             if (zero_run_get_bits[token])
988                 zero_run += get_bits(gb, zero_run_get_bits[token]);
989
990             if (zero_run) {
991                 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
992             } else {
993                 // Save DC into the fragment structure. DC prediction is
994                 // done in raster order, so the actual DC can't be in with
995                 // other tokens. We still need the token in dct_tokens[]
996                 // however, or else the structure collapses on itself.
997                 if (!coeff_index)
998                     all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
999
1000                 dct_tokens[j++] = TOKEN_COEFF(coeff);
1001             }
1002
1003             if (coeff_index + zero_run > 64) {
1004                 av_log(s->avctx, AV_LOG_DEBUG,
1005                        "Invalid zero run of %d with %d coeffs left\n",
1006                        zero_run, 64 - coeff_index);
1007                 zero_run = 64 - coeff_index;
1008             }
1009
1010             // zero runs code multiple coefficients,
1011             // so don't try to decode coeffs for those higher levels
1012             for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1013                 s->num_coded_frags[plane][i]--;
1014             coeff_i++;
1015         } else {
1016             av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1017             return -1;
1018         }
1019     }
1020
1021     if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1022         av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1023
1024     // decrement the number of blocks that have higher coefficients for each
1025     // EOB run at this level
1026     if (blocks_ended)
1027         for (i = coeff_index + 1; i < 64; i++)
1028             s->num_coded_frags[plane][i] -= blocks_ended;
1029
1030     // setup the next buffer
1031     if (plane < 2)
1032         s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1033     else if (coeff_index < 63)
1034         s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1035
1036     return eob_run;
1037 }
1038
1039 static void reverse_dc_prediction(Vp3DecodeContext *s,
1040                                   int first_fragment,
1041                                   int fragment_width,
1042                                   int fragment_height);
1043 /*
1044  * This function unpacks all of the DCT coefficient data from the
1045  * bitstream.
1046  */
1047 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1048 {
1049     int i;
1050     int dc_y_table;
1051     int dc_c_table;
1052     int ac_y_table;
1053     int ac_c_table;
1054     int residual_eob_run = 0;
1055     VLC *y_tables[64];
1056     VLC *c_tables[64];
1057
1058     s->dct_tokens[0][0] = s->dct_tokens_base;
1059
1060     /* fetch the DC table indexes */
1061     dc_y_table = get_bits(gb, 4);
1062     dc_c_table = get_bits(gb, 4);
1063
1064     /* unpack the Y plane DC coefficients */
1065     residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1066                                    0, residual_eob_run);
1067     if (residual_eob_run < 0)
1068         return residual_eob_run;
1069
1070     /* reverse prediction of the Y-plane DC coefficients */
1071     reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1072
1073     /* unpack the C plane DC coefficients */
1074     residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1075                                    1, residual_eob_run);
1076     if (residual_eob_run < 0)
1077         return residual_eob_run;
1078     residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1079                                    2, residual_eob_run);
1080     if (residual_eob_run < 0)
1081         return residual_eob_run;
1082
1083     /* reverse prediction of the C-plane DC coefficients */
1084     if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1085         reverse_dc_prediction(s, s->fragment_start[1],
1086                               s->fragment_width[1], s->fragment_height[1]);
1087         reverse_dc_prediction(s, s->fragment_start[2],
1088                               s->fragment_width[1], s->fragment_height[1]);
1089     }
1090
1091     /* fetch the AC table indexes */
1092     ac_y_table = get_bits(gb, 4);
1093     ac_c_table = get_bits(gb, 4);
1094
1095     /* build tables of AC VLC tables */
1096     for (i = 1; i <= 5; i++) {
1097         y_tables[i] = &s->ac_vlc_1[ac_y_table];
1098         c_tables[i] = &s->ac_vlc_1[ac_c_table];
1099     }
1100     for (i = 6; i <= 14; i++) {
1101         y_tables[i] = &s->ac_vlc_2[ac_y_table];
1102         c_tables[i] = &s->ac_vlc_2[ac_c_table];
1103     }
1104     for (i = 15; i <= 27; i++) {
1105         y_tables[i] = &s->ac_vlc_3[ac_y_table];
1106         c_tables[i] = &s->ac_vlc_3[ac_c_table];
1107     }
1108     for (i = 28; i <= 63; i++) {
1109         y_tables[i] = &s->ac_vlc_4[ac_y_table];
1110         c_tables[i] = &s->ac_vlc_4[ac_c_table];
1111     }
1112
1113     /* decode all AC coefficients */
1114     for (i = 1; i <= 63; i++) {
1115         residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1116                                        0, residual_eob_run);
1117         if (residual_eob_run < 0)
1118             return residual_eob_run;
1119
1120         residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1121                                        1, residual_eob_run);
1122         if (residual_eob_run < 0)
1123             return residual_eob_run;
1124         residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1125                                        2, residual_eob_run);
1126         if (residual_eob_run < 0)
1127             return residual_eob_run;
1128     }
1129
1130     return 0;
1131 }
1132
1133 /*
1134  * This function reverses the DC prediction for each coded fragment in
1135  * the frame. Much of this function is adapted directly from the original
1136  * VP3 source code.
1137  */
1138 #define COMPATIBLE_FRAME(x)                                                   \
1139     (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1140 #define DC_COEFF(u) s->all_fragments[u].dc
1141
1142 static void reverse_dc_prediction(Vp3DecodeContext *s,
1143                                   int first_fragment,
1144                                   int fragment_width,
1145                                   int fragment_height)
1146 {
1147 #define PUL 8
1148 #define PU 4
1149 #define PUR 2
1150 #define PL 1
1151
1152     int x, y;
1153     int i = first_fragment;
1154
1155     int predicted_dc;
1156
1157     /* DC values for the left, up-left, up, and up-right fragments */
1158     int vl, vul, vu, vur;
1159
1160     /* indexes for the left, up-left, up, and up-right fragments */
1161     int l, ul, u, ur;
1162
1163     /*
1164      * The 6 fields mean:
1165      *   0: up-left multiplier
1166      *   1: up multiplier
1167      *   2: up-right multiplier
1168      *   3: left multiplier
1169      */
1170     static const int predictor_transform[16][4] = {
1171         {    0,   0,   0,   0 },
1172         {    0,   0,   0, 128 }, // PL
1173         {    0,   0, 128,   0 }, // PUR
1174         {    0,   0,  53,  75 }, // PUR|PL
1175         {    0, 128,   0,   0 }, // PU
1176         {    0,  64,   0,  64 }, // PU |PL
1177         {    0, 128,   0,   0 }, // PU |PUR
1178         {    0,   0,  53,  75 }, // PU |PUR|PL
1179         {  128,   0,   0,   0 }, // PUL
1180         {    0,   0,   0, 128 }, // PUL|PL
1181         {   64,   0,  64,   0 }, // PUL|PUR
1182         {    0,   0,  53,  75 }, // PUL|PUR|PL
1183         {    0, 128,   0,   0 }, // PUL|PU
1184         { -104, 116,   0, 116 }, // PUL|PU |PL
1185         {   24,  80,  24,   0 }, // PUL|PU |PUR
1186         { -104, 116,   0, 116 }  // PUL|PU |PUR|PL
1187     };
1188
1189     /* This table shows which types of blocks can use other blocks for
1190      * prediction. For example, INTRA is the only mode in this table to
1191      * have a frame number of 0. That means INTRA blocks can only predict
1192      * from other INTRA blocks. There are 2 golden frame coding types;
1193      * blocks encoding in these modes can only predict from other blocks
1194      * that were encoded with these 1 of these 2 modes. */
1195     static const unsigned char compatible_frame[9] = {
1196         1,    /* MODE_INTER_NO_MV */
1197         0,    /* MODE_INTRA */
1198         1,    /* MODE_INTER_PLUS_MV */
1199         1,    /* MODE_INTER_LAST_MV */
1200         1,    /* MODE_INTER_PRIOR_MV */
1201         2,    /* MODE_USING_GOLDEN */
1202         2,    /* MODE_GOLDEN_MV */
1203         1,    /* MODE_INTER_FOUR_MV */
1204         3     /* MODE_COPY */
1205     };
1206     int current_frame_type;
1207
1208     /* there is a last DC predictor for each of the 3 frame types */
1209     short last_dc[3];
1210
1211     int transform = 0;
1212
1213     vul =
1214     vu  =
1215     vur =
1216     vl  = 0;
1217     last_dc[0] =
1218     last_dc[1] =
1219     last_dc[2] = 0;
1220
1221     /* for each fragment row... */
1222     for (y = 0; y < fragment_height; y++) {
1223         /* for each fragment in a row... */
1224         for (x = 0; x < fragment_width; x++, i++) {
1225
1226             /* reverse prediction if this block was coded */
1227             if (s->all_fragments[i].coding_method != MODE_COPY) {
1228                 current_frame_type =
1229                     compatible_frame[s->all_fragments[i].coding_method];
1230
1231                 transform = 0;
1232                 if (x) {
1233                     l  = i - 1;
1234                     vl = DC_COEFF(l);
1235                     if (COMPATIBLE_FRAME(l))
1236                         transform |= PL;
1237                 }
1238                 if (y) {
1239                     u  = i - fragment_width;
1240                     vu = DC_COEFF(u);
1241                     if (COMPATIBLE_FRAME(u))
1242                         transform |= PU;
1243                     if (x) {
1244                         ul  = i - fragment_width - 1;
1245                         vul = DC_COEFF(ul);
1246                         if (COMPATIBLE_FRAME(ul))
1247                             transform |= PUL;
1248                     }
1249                     if (x + 1 < fragment_width) {
1250                         ur  = i - fragment_width + 1;
1251                         vur = DC_COEFF(ur);
1252                         if (COMPATIBLE_FRAME(ur))
1253                             transform |= PUR;
1254                     }
1255                 }
1256
1257                 if (transform == 0) {
1258                     /* if there were no fragments to predict from, use last
1259                      * DC saved */
1260                     predicted_dc = last_dc[current_frame_type];
1261                 } else {
1262                     /* apply the appropriate predictor transform */
1263                     predicted_dc =
1264                         (predictor_transform[transform][0] * vul) +
1265                         (predictor_transform[transform][1] * vu) +
1266                         (predictor_transform[transform][2] * vur) +
1267                         (predictor_transform[transform][3] * vl);
1268
1269                     predicted_dc /= 128;
1270
1271                     /* check for outranging on the [ul u l] and
1272                      * [ul u ur l] predictors */
1273                     if ((transform == 15) || (transform == 13)) {
1274                         if (FFABS(predicted_dc - vu) > 128)
1275                             predicted_dc = vu;
1276                         else if (FFABS(predicted_dc - vl) > 128)
1277                             predicted_dc = vl;
1278                         else if (FFABS(predicted_dc - vul) > 128)
1279                             predicted_dc = vul;
1280                     }
1281                 }
1282
1283                 /* at long last, apply the predictor */
1284                 DC_COEFF(i) += predicted_dc;
1285                 /* save the DC */
1286                 last_dc[current_frame_type] = DC_COEFF(i);
1287             }
1288         }
1289     }
1290 }
1291
1292 static void apply_loop_filter(Vp3DecodeContext *s, int plane,
1293                               int ystart, int yend)
1294 {
1295     int x, y;
1296     int *bounding_values = s->bounding_values_array + 127;
1297
1298     int width           = s->fragment_width[!!plane];
1299     int height          = s->fragment_height[!!plane];
1300     int fragment        = s->fragment_start[plane] + ystart * width;
1301     ptrdiff_t stride    = s->current_frame.f->linesize[plane];
1302     uint8_t *plane_data = s->current_frame.f->data[plane];
1303     if (!s->flipped_image)
1304         stride = -stride;
1305     plane_data += s->data_offset[plane] + 8 * ystart * stride;
1306
1307     for (y = ystart; y < yend; y++) {
1308         for (x = 0; x < width; x++) {
1309             /* This code basically just deblocks on the edges of coded blocks.
1310              * However, it has to be much more complicated because of the
1311              * brain damaged deblock ordering used in VP3/Theora. Order matters
1312              * because some pixels get filtered twice. */
1313             if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1314                 /* do not perform left edge filter for left columns frags */
1315                 if (x > 0) {
1316                     s->vp3dsp.h_loop_filter(
1317                         plane_data + 8 * x,
1318                         stride, bounding_values);
1319                 }
1320
1321                 /* do not perform top edge filter for top row fragments */
1322                 if (y > 0) {
1323                     s->vp3dsp.v_loop_filter(
1324                         plane_data + 8 * x,
1325                         stride, bounding_values);
1326                 }
1327
1328                 /* do not perform right edge filter for right column
1329                  * fragments or if right fragment neighbor is also coded
1330                  * in this frame (it will be filtered in next iteration) */
1331                 if ((x < width - 1) &&
1332                     (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1333                     s->vp3dsp.h_loop_filter(
1334                         plane_data + 8 * x + 8,
1335                         stride, bounding_values);
1336                 }
1337
1338                 /* do not perform bottom edge filter for bottom row
1339                  * fragments or if bottom fragment neighbor is also coded
1340                  * in this frame (it will be filtered in the next row) */
1341                 if ((y < height - 1) &&
1342                     (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1343                     s->vp3dsp.v_loop_filter(
1344                         plane_data + 8 * x + 8 * stride,
1345                         stride, bounding_values);
1346                 }
1347             }
1348
1349             fragment++;
1350         }
1351         plane_data += 8 * stride;
1352     }
1353 }
1354
1355 /**
1356  * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1357  * for the next block in coding order
1358  */
1359 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1360                               int plane, int inter, int16_t block[64])
1361 {
1362     int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1363     uint8_t *perm = s->idct_scantable;
1364     int i = 0;
1365
1366     do {
1367         int token = *s->dct_tokens[plane][i];
1368         switch (token & 3) {
1369         case 0: // EOB
1370             if (--token < 4) // 0-3 are token types so the EOB run must now be 0
1371                 s->dct_tokens[plane][i]++;
1372             else
1373                 *s->dct_tokens[plane][i] = token & ~3;
1374             goto end;
1375         case 1: // zero run
1376             s->dct_tokens[plane][i]++;
1377             i += (token >> 2) & 0x7f;
1378             if (i > 63) {
1379                 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1380                 return i;
1381             }
1382             block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1383             i++;
1384             break;
1385         case 2: // coeff
1386             block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1387             s->dct_tokens[plane][i++]++;
1388             break;
1389         default: // shouldn't happen
1390             return i;
1391         }
1392     } while (i < 64);
1393     // return value is expected to be a valid level
1394     i--;
1395 end:
1396     // the actual DC+prediction is in the fragment structure
1397     block[0] = frag->dc * s->qmat[0][inter][plane][0];
1398     return i;
1399 }
1400
1401 /**
1402  * called when all pixels up to row y are complete
1403  */
1404 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1405 {
1406     int h, cy, i;
1407     int offset[AV_NUM_DATA_POINTERS];
1408
1409     if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1410         int y_flipped = s->flipped_image ? s->height - y : y;
1411
1412         /* At the end of the frame, report INT_MAX instead of the height of
1413          * the frame. This makes the other threads' ff_thread_await_progress()
1414          * calls cheaper, because they don't have to clip their values. */
1415         ff_thread_report_progress(&s->current_frame,
1416                                   y_flipped == s->height ? INT_MAX
1417                                                          : y_flipped - 1,
1418                                   0);
1419     }
1420
1421     if (!s->avctx->draw_horiz_band)
1422         return;
1423
1424     h = y - s->last_slice_end;
1425     s->last_slice_end = y;
1426     y -= h;
1427
1428     if (!s->flipped_image)
1429         y = s->height - y - h;
1430
1431     cy        = y >> s->chroma_y_shift;
1432     offset[0] = s->current_frame.f->linesize[0] * y;
1433     offset[1] = s->current_frame.f->linesize[1] * cy;
1434     offset[2] = s->current_frame.f->linesize[2] * cy;
1435     for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
1436         offset[i] = 0;
1437
1438     emms_c();
1439     s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1440 }
1441
1442 /**
1443  * Wait for the reference frame of the current fragment.
1444  * The progress value is in luma pixel rows.
1445  */
1446 static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment,
1447                                 int motion_y, int y)
1448 {
1449     ThreadFrame *ref_frame;
1450     int ref_row;
1451     int border = motion_y & 1;
1452
1453     if (fragment->coding_method == MODE_USING_GOLDEN ||
1454         fragment->coding_method == MODE_GOLDEN_MV)
1455         ref_frame = &s->golden_frame;
1456     else
1457         ref_frame = &s->last_frame;
1458
1459     ref_row = y + (motion_y >> 1);
1460     ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1461
1462     ff_thread_await_progress(ref_frame, ref_row, 0);
1463 }
1464
1465 /*
1466  * Perform the final rendering for a particular slice of data.
1467  * The slice number ranges from 0..(c_superblock_height - 1).
1468  */
1469 static void render_slice(Vp3DecodeContext *s, int slice)
1470 {
1471     int x, y, i, j, fragment;
1472     int16_t *block = s->block;
1473     int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1474     int motion_halfpel_index;
1475     uint8_t *motion_source;
1476     int plane, first_pixel;
1477
1478     if (slice >= s->c_superblock_height)
1479         return;
1480
1481     for (plane = 0; plane < 3; plane++) {
1482         uint8_t *output_plane = s->current_frame.f->data[plane] +
1483                                 s->data_offset[plane];
1484         uint8_t *last_plane = s->last_frame.f->data[plane] +
1485                               s->data_offset[plane];
1486         uint8_t *golden_plane = s->golden_frame.f->data[plane] +
1487                                 s->data_offset[plane];
1488         ptrdiff_t stride = s->current_frame.f->linesize[plane];
1489         int plane_width  = s->width  >> (plane && s->chroma_x_shift);
1490         int plane_height = s->height >> (plane && s->chroma_y_shift);
1491         int8_t(*motion_val)[2] = s->motion_val[!!plane];
1492
1493         int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
1494         int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
1495         int slice_width  = plane ? s->c_superblock_width
1496                                  : s->y_superblock_width;
1497
1498         int fragment_width  = s->fragment_width[!!plane];
1499         int fragment_height = s->fragment_height[!!plane];
1500         int fragment_start  = s->fragment_start[plane];
1501
1502         int do_await = !plane && HAVE_THREADS &&
1503                        (s->avctx->active_thread_type & FF_THREAD_FRAME);
1504
1505         if (!s->flipped_image)
1506             stride = -stride;
1507         if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1508             continue;
1509
1510         /* for each superblock row in the slice (both of them)... */
1511         for (; sb_y < slice_height; sb_y++) {
1512             /* for each superblock in a row... */
1513             for (sb_x = 0; sb_x < slice_width; sb_x++) {
1514                 /* for each block in a superblock... */
1515                 for (j = 0; j < 16; j++) {
1516                     x        = 4 * sb_x + hilbert_offset[j][0];
1517                     y        = 4 * sb_y + hilbert_offset[j][1];
1518                     fragment = y * fragment_width + x;
1519
1520                     i = fragment_start + fragment;
1521
1522                     // bounds check
1523                     if (x >= fragment_width || y >= fragment_height)
1524                         continue;
1525
1526                     first_pixel = 8 * y * stride + 8 * x;
1527
1528                     if (do_await &&
1529                         s->all_fragments[i].coding_method != MODE_INTRA)
1530                         await_reference_row(s, &s->all_fragments[i],
1531                                             motion_val[fragment][1],
1532                                             (16 * y) >> s->chroma_y_shift);
1533
1534                     /* transform if this block was coded */
1535                     if (s->all_fragments[i].coding_method != MODE_COPY) {
1536                         if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
1537                             (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
1538                             motion_source = golden_plane;
1539                         else
1540                             motion_source = last_plane;
1541
1542                         motion_source       += first_pixel;
1543                         motion_halfpel_index = 0;
1544
1545                         /* sort out the motion vector if this fragment is coded
1546                          * using a motion vector method */
1547                         if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1548                             (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
1549                             int src_x, src_y;
1550                             motion_x = motion_val[fragment][0];
1551                             motion_y = motion_val[fragment][1];
1552
1553                             src_x = (motion_x >> 1) + 8 * x;
1554                             src_y = (motion_y >> 1) + 8 * y;
1555
1556                             motion_halfpel_index = motion_x & 0x01;
1557                             motion_source       += (motion_x >> 1);
1558
1559                             motion_halfpel_index |= (motion_y & 0x01) << 1;
1560                             motion_source        += ((motion_y >> 1) * stride);
1561
1562                             if (src_x < 0 || src_y < 0 ||
1563                                 src_x + 9 >= plane_width ||
1564                                 src_y + 9 >= plane_height) {
1565                                 uint8_t *temp = s->edge_emu_buffer;
1566                                 if (stride < 0)
1567                                     temp -= 8 * stride;
1568
1569                                 s->vdsp.emulated_edge_mc(temp, motion_source,
1570                                                          stride, stride,
1571                                                          9, 9, src_x, src_y,
1572                                                          plane_width,
1573                                                          plane_height);
1574                                 motion_source = temp;
1575                             }
1576                         }
1577
1578                         /* first, take care of copying a block from either the
1579                          * previous or the golden frame */
1580                         if (s->all_fragments[i].coding_method != MODE_INTRA) {
1581                             /* Note, it is possible to implement all MC cases
1582                              * with put_no_rnd_pixels_l2 which would look more
1583                              * like the VP3 source but this would be slower as
1584                              * put_no_rnd_pixels_tab is better optimized */
1585                             if (motion_halfpel_index != 3) {
1586                                 s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
1587                                     output_plane + first_pixel,
1588                                     motion_source, stride, 8);
1589                             } else {
1590                                 /* d is 0 if motion_x and _y have the same sign,
1591                                  * else -1 */
1592                                 int d = (motion_x ^ motion_y) >> 31;
1593                                 s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
1594                                                                motion_source - d,
1595                                                                motion_source + stride + 1 + d,
1596                                                                stride, 8);
1597                             }
1598                         }
1599
1600                         /* invert DCT and place (or add) in final output */
1601
1602                         if (s->all_fragments[i].coding_method == MODE_INTRA) {
1603                             int index;
1604                             index = vp3_dequant(s, s->all_fragments + i,
1605                                                 plane, 0, block);
1606                             if (index > 63)
1607                                 continue;
1608                             s->vp3dsp.idct_put(output_plane + first_pixel,
1609                                                stride,
1610                                                block);
1611                         } else {
1612                             int index = vp3_dequant(s, s->all_fragments + i,
1613                                                     plane, 1, block);
1614                             if (index > 63)
1615                                 continue;
1616                             if (index > 0) {
1617                                 s->vp3dsp.idct_add(output_plane + first_pixel,
1618                                                    stride,
1619                                                    block);
1620                             } else {
1621                                 s->vp3dsp.idct_dc_add(output_plane + first_pixel,
1622                                                       stride, block);
1623                             }
1624                         }
1625                     } else {
1626                         /* copy directly from the previous frame */
1627                         s->hdsp.put_pixels_tab[1][0](
1628                             output_plane + first_pixel,
1629                             last_plane + first_pixel,
1630                             stride, 8);
1631                     }
1632                 }
1633             }
1634
1635             // Filter up to the last row in the superblock row
1636             if (!s->skip_loop_filter)
1637                 apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
1638                                   FFMIN(4 * sb_y + 3, fragment_height - 1));
1639         }
1640     }
1641
1642     /* this looks like a good place for slice dispatch... */
1643     /* algorithm:
1644      *   if (slice == s->macroblock_height - 1)
1645      *     dispatch (both last slice & 2nd-to-last slice);
1646      *   else if (slice > 0)
1647      *     dispatch (slice - 1);
1648      */
1649
1650     vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
1651                                  s->height - 16));
1652 }
1653
1654 /// Allocate tables for per-frame data in Vp3DecodeContext
1655 static av_cold int allocate_tables(AVCodecContext *avctx)
1656 {
1657     Vp3DecodeContext *s = avctx->priv_data;
1658     int y_fragment_count, c_fragment_count;
1659
1660     y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1661     c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1662
1663     s->superblock_coding = av_malloc(s->superblock_count);
1664     s->all_fragments     = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
1665
1666     s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
1667
1668     s->dct_tokens_base = av_malloc(64 * s->fragment_count *
1669                                    sizeof(*s->dct_tokens_base));
1670     s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0]));
1671     s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1]));
1672
1673     /* work out the block mapping tables */
1674     s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
1675     s->macroblock_coding    = av_malloc(s->macroblock_count + 1);
1676
1677     if (!s->superblock_coding    || !s->all_fragments          ||
1678         !s->dct_tokens_base      || !s->coded_fragment_list[0] ||
1679         !s->superblock_fragments || !s->macroblock_coding      ||
1680         !s->motion_val[0]        || !s->motion_val[1]) {
1681         vp3_decode_end(avctx);
1682         return -1;
1683     }
1684
1685     init_block_mapping(s);
1686
1687     return 0;
1688 }
1689
1690 static av_cold int init_frames(Vp3DecodeContext *s)
1691 {
1692     s->current_frame.f = av_frame_alloc();
1693     s->last_frame.f    = av_frame_alloc();
1694     s->golden_frame.f  = av_frame_alloc();
1695
1696     if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) {
1697         av_frame_free(&s->current_frame.f);
1698         av_frame_free(&s->last_frame.f);
1699         av_frame_free(&s->golden_frame.f);
1700         return AVERROR(ENOMEM);
1701     }
1702
1703     return 0;
1704 }
1705
1706 static av_cold int vp3_decode_init(AVCodecContext *avctx)
1707 {
1708     Vp3DecodeContext *s = avctx->priv_data;
1709     int i, inter, plane, ret;
1710     int c_width;
1711     int c_height;
1712     int y_fragment_count, c_fragment_count;
1713
1714     ret = init_frames(s);
1715     if (ret < 0)
1716         return ret;
1717
1718     avctx->internal->allocate_progress = 1;
1719
1720     if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
1721         s->version = 0;
1722     else
1723         s->version = 1;
1724
1725     s->avctx  = avctx;
1726     s->width  = FFALIGN(avctx->coded_width, 16);
1727     s->height = FFALIGN(avctx->coded_height, 16);
1728     if (avctx->pix_fmt == AV_PIX_FMT_NONE)
1729         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1730     avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1731     ff_hpeldsp_init(&s->hdsp, avctx->flags | AV_CODEC_FLAG_BITEXACT);
1732     ff_videodsp_init(&s->vdsp, 8);
1733     ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
1734
1735     for (i = 0; i < 64; i++) {
1736 #define TRANSPOSE(x) (x >> 3) | ((x & 7) << 3)
1737         s->idct_permutation[i] = TRANSPOSE(i);
1738         s->idct_scantable[i]   = TRANSPOSE(ff_zigzag_direct[i]);
1739 #undef TRANSPOSE
1740     }
1741
1742     /* initialize to an impossible value which will force a recalculation
1743      * in the first frame decode */
1744     for (i = 0; i < 3; i++)
1745         s->qps[i] = -1;
1746
1747     av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift,
1748                                      &s->chroma_y_shift);
1749
1750     s->y_superblock_width  = (s->width  + 31) / 32;
1751     s->y_superblock_height = (s->height + 31) / 32;
1752     s->y_superblock_count  = s->y_superblock_width * s->y_superblock_height;
1753
1754     /* work out the dimensions for the C planes */
1755     c_width                = s->width >> s->chroma_x_shift;
1756     c_height               = s->height >> s->chroma_y_shift;
1757     s->c_superblock_width  = (c_width  + 31) / 32;
1758     s->c_superblock_height = (c_height + 31) / 32;
1759     s->c_superblock_count  = s->c_superblock_width * s->c_superblock_height;
1760
1761     s->superblock_count   = s->y_superblock_count + (s->c_superblock_count * 2);
1762     s->u_superblock_start = s->y_superblock_count;
1763     s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
1764
1765     s->macroblock_width  = (s->width  + 15) / 16;
1766     s->macroblock_height = (s->height + 15) / 16;
1767     s->macroblock_count  = s->macroblock_width * s->macroblock_height;
1768
1769     s->fragment_width[0]  = s->width / FRAGMENT_PIXELS;
1770     s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
1771     s->fragment_width[1]  = s->fragment_width[0] >> s->chroma_x_shift;
1772     s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
1773
1774     /* fragment count covers all 8x8 blocks for all 3 planes */
1775     y_fragment_count     = s->fragment_width[0] * s->fragment_height[0];
1776     c_fragment_count     = s->fragment_width[1] * s->fragment_height[1];
1777     s->fragment_count    = y_fragment_count + 2 * c_fragment_count;
1778     s->fragment_start[1] = y_fragment_count;
1779     s->fragment_start[2] = y_fragment_count + c_fragment_count;
1780
1781     if (!s->theora_tables) {
1782         for (i = 0; i < 64; i++) {
1783             s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
1784             s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
1785             s->base_matrix[0][i]        = vp31_intra_y_dequant[i];
1786             s->base_matrix[1][i]        = vp31_intra_c_dequant[i];
1787             s->base_matrix[2][i]        = vp31_inter_dequant[i];
1788             s->filter_limit_values[i]   = vp31_filter_limit_values[i];
1789         }
1790
1791         for (inter = 0; inter < 2; inter++) {
1792             for (plane = 0; plane < 3; plane++) {
1793                 s->qr_count[inter][plane]   = 1;
1794                 s->qr_size[inter][plane][0] = 63;
1795                 s->qr_base[inter][plane][0] =
1796                 s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
1797             }
1798         }
1799
1800         /* init VLC tables */
1801         for (i = 0; i < 16; i++) {
1802             /* DC histograms */
1803             init_vlc(&s->dc_vlc[i], 11, 32,
1804                      &dc_bias[i][0][1], 4, 2,
1805                      &dc_bias[i][0][0], 4, 2, 0);
1806
1807             /* group 1 AC histograms */
1808             init_vlc(&s->ac_vlc_1[i], 11, 32,
1809                      &ac_bias_0[i][0][1], 4, 2,
1810                      &ac_bias_0[i][0][0], 4, 2, 0);
1811
1812             /* group 2 AC histograms */
1813             init_vlc(&s->ac_vlc_2[i], 11, 32,
1814                      &ac_bias_1[i][0][1], 4, 2,
1815                      &ac_bias_1[i][0][0], 4, 2, 0);
1816
1817             /* group 3 AC histograms */
1818             init_vlc(&s->ac_vlc_3[i], 11, 32,
1819                      &ac_bias_2[i][0][1], 4, 2,
1820                      &ac_bias_2[i][0][0], 4, 2, 0);
1821
1822             /* group 4 AC histograms */
1823             init_vlc(&s->ac_vlc_4[i], 11, 32,
1824                      &ac_bias_3[i][0][1], 4, 2,
1825                      &ac_bias_3[i][0][0], 4, 2, 0);
1826         }
1827     } else {
1828         for (i = 0; i < 16; i++) {
1829             /* DC histograms */
1830             if (init_vlc(&s->dc_vlc[i], 11, 32,
1831                          &s->huffman_table[i][0][1], 8, 4,
1832                          &s->huffman_table[i][0][0], 8, 4, 0) < 0)
1833                 goto vlc_fail;
1834
1835             /* group 1 AC histograms */
1836             if (init_vlc(&s->ac_vlc_1[i], 11, 32,
1837                          &s->huffman_table[i + 16][0][1], 8, 4,
1838                          &s->huffman_table[i + 16][0][0], 8, 4, 0) < 0)
1839                 goto vlc_fail;
1840
1841             /* group 2 AC histograms */
1842             if (init_vlc(&s->ac_vlc_2[i], 11, 32,
1843                          &s->huffman_table[i + 16 * 2][0][1], 8, 4,
1844                          &s->huffman_table[i + 16 * 2][0][0], 8, 4, 0) < 0)
1845                 goto vlc_fail;
1846
1847             /* group 3 AC histograms */
1848             if (init_vlc(&s->ac_vlc_3[i], 11, 32,
1849                          &s->huffman_table[i + 16 * 3][0][1], 8, 4,
1850                          &s->huffman_table[i + 16 * 3][0][0], 8, 4, 0) < 0)
1851                 goto vlc_fail;
1852
1853             /* group 4 AC histograms */
1854             if (init_vlc(&s->ac_vlc_4[i], 11, 32,
1855                          &s->huffman_table[i + 16 * 4][0][1], 8, 4,
1856                          &s->huffman_table[i + 16 * 4][0][0], 8, 4, 0) < 0)
1857                 goto vlc_fail;
1858         }
1859     }
1860
1861     init_vlc(&s->superblock_run_length_vlc, 6, 34,
1862              &superblock_run_length_vlc_table[0][1], 4, 2,
1863              &superblock_run_length_vlc_table[0][0], 4, 2, 0);
1864
1865     init_vlc(&s->fragment_run_length_vlc, 5, 30,
1866              &fragment_run_length_vlc_table[0][1], 4, 2,
1867              &fragment_run_length_vlc_table[0][0], 4, 2, 0);
1868
1869     init_vlc(&s->mode_code_vlc, 3, 8,
1870              &mode_code_vlc_table[0][1], 2, 1,
1871              &mode_code_vlc_table[0][0], 2, 1, 0);
1872
1873     init_vlc(&s->motion_vector_vlc, 6, 63,
1874              &motion_vector_vlc_table[0][1], 2, 1,
1875              &motion_vector_vlc_table[0][0], 2, 1, 0);
1876
1877     return allocate_tables(avctx);
1878
1879 vlc_fail:
1880     av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
1881     return -1;
1882 }
1883
1884 /// Release and shuffle frames after decode finishes
1885 static int update_frames(AVCodecContext *avctx)
1886 {
1887     Vp3DecodeContext *s = avctx->priv_data;
1888     int ret = 0;
1889
1890     /* shuffle frames (last = current) */
1891     ff_thread_release_buffer(avctx, &s->last_frame);
1892     ret = ff_thread_ref_frame(&s->last_frame, &s->current_frame);
1893     if (ret < 0)
1894         goto fail;
1895
1896     if (s->keyframe) {
1897         ff_thread_release_buffer(avctx, &s->golden_frame);
1898         ret = ff_thread_ref_frame(&s->golden_frame, &s->current_frame);
1899     }
1900
1901 fail:
1902     ff_thread_release_buffer(avctx, &s->current_frame);
1903     return ret;
1904 }
1905
1906 static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
1907 {
1908     ff_thread_release_buffer(s->avctx, dst);
1909     if (src->f->data[0])
1910         return ff_thread_ref_frame(dst, src);
1911     return 0;
1912 }
1913
1914 static int ref_frames(Vp3DecodeContext *dst, Vp3DecodeContext *src)
1915 {
1916     int ret;
1917     if ((ret = ref_frame(dst, &dst->current_frame, &src->current_frame)) < 0 ||
1918         (ret = ref_frame(dst, &dst->golden_frame,  &src->golden_frame)) < 0  ||
1919         (ret = ref_frame(dst, &dst->last_frame,    &src->last_frame)) < 0)
1920         return ret;
1921     return 0;
1922 }
1923
1924 static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1925 {
1926     Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
1927     int qps_changed = 0, i, err;
1928
1929 #define copy_fields(to, from, start_field, end_field)                         \
1930     memcpy(&to->start_field, &from->start_field,                              \
1931            (char *) &to->end_field - (char *) &to->start_field)
1932
1933     if (!s1->current_frame.f->data[0] ||
1934         s->width != s1->width || s->height != s1->height) {
1935         if (s != s1)
1936             ref_frames(s, s1);
1937         return -1;
1938     }
1939
1940     if (s != s1) {
1941         // init tables if the first frame hasn't been decoded
1942         if (!s->current_frame.f->data[0]) {
1943             int y_fragment_count, c_fragment_count;
1944             s->avctx = dst;
1945             err = allocate_tables(dst);
1946             if (err)
1947                 return err;
1948             y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1949             c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1950             memcpy(s->motion_val[0], s1->motion_val[0],
1951                    y_fragment_count * sizeof(*s->motion_val[0]));
1952             memcpy(s->motion_val[1], s1->motion_val[1],
1953                    c_fragment_count * sizeof(*s->motion_val[1]));
1954         }
1955
1956         // copy previous frame data
1957         if ((err = ref_frames(s, s1)) < 0)
1958             return err;
1959
1960         s->keyframe = s1->keyframe;
1961
1962         // copy qscale data if necessary
1963         for (i = 0; i < 3; i++) {
1964             if (s->qps[i] != s1->qps[1]) {
1965                 qps_changed = 1;
1966                 memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
1967             }
1968         }
1969
1970         if (s->qps[0] != s1->qps[0])
1971             memcpy(&s->bounding_values_array, &s1->bounding_values_array,
1972                    sizeof(s->bounding_values_array));
1973
1974         if (qps_changed)
1975             copy_fields(s, s1, qps, superblock_count);
1976 #undef copy_fields
1977     }
1978
1979     return update_frames(dst);
1980 }
1981
1982 static int vp3_decode_frame(AVCodecContext *avctx,
1983                             void *data, int *got_frame,
1984                             AVPacket *avpkt)
1985 {
1986     const uint8_t *buf  = avpkt->data;
1987     int buf_size        = avpkt->size;
1988     Vp3DecodeContext *s = avctx->priv_data;
1989     GetBitContext gb;
1990     int i, ret;
1991
1992     init_get_bits(&gb, buf, buf_size * 8);
1993
1994     if (s->theora && get_bits1(&gb)) {
1995         av_log(avctx, AV_LOG_ERROR,
1996                "Header packet passed to frame decoder, skipping\n");
1997         return -1;
1998     }
1999
2000     s->keyframe = !get_bits1(&gb);
2001     if (!s->theora)
2002         skip_bits(&gb, 1);
2003     for (i = 0; i < 3; i++)
2004         s->last_qps[i] = s->qps[i];
2005
2006     s->nqps = 0;
2007     do {
2008         s->qps[s->nqps++] = get_bits(&gb, 6);
2009     } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
2010     for (i = s->nqps; i < 3; i++)
2011         s->qps[i] = -1;
2012
2013     if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2014         av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2015                s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
2016
2017     s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
2018                           avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
2019                                                                   : AVDISCARD_NONKEY);
2020
2021     if (s->qps[0] != s->last_qps[0])
2022         init_loop_filter(s);
2023
2024     for (i = 0; i < s->nqps; i++)
2025         // reinit all dequantizers if the first one changed, because
2026         // the DC of the first quantizer must be used for all matrices
2027         if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
2028             init_dequantizer(s, i);
2029
2030     if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2031         return buf_size;
2032
2033     s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2034                                                 : AV_PICTURE_TYPE_P;
2035     if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0) {
2036         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2037         goto error;
2038     }
2039
2040     if (!s->edge_emu_buffer)
2041         s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
2042
2043     if (s->keyframe) {
2044         if (!s->theora) {
2045             skip_bits(&gb, 4); /* width code */
2046             skip_bits(&gb, 4); /* height code */
2047             if (s->version) {
2048                 s->version = get_bits(&gb, 5);
2049                 if (avctx->frame_number == 0)
2050                     av_log(s->avctx, AV_LOG_DEBUG,
2051                            "VP version: %d\n", s->version);
2052             }
2053         }
2054         if (s->version || s->theora) {
2055             if (get_bits1(&gb))
2056                 av_log(s->avctx, AV_LOG_ERROR,
2057                        "Warning, unsupported keyframe coding type?!\n");
2058             skip_bits(&gb, 2); /* reserved? */
2059         }
2060     } else {
2061         if (!s->golden_frame.f->data[0]) {
2062             av_log(s->avctx, AV_LOG_WARNING,
2063                    "vp3: first frame not a keyframe\n");
2064
2065             s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
2066             if (ff_thread_get_buffer(avctx, &s->golden_frame,
2067                                      AV_GET_BUFFER_FLAG_REF) < 0) {
2068                 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2069                 goto error;
2070             }
2071             ff_thread_release_buffer(avctx, &s->last_frame);
2072             if ((ret = ff_thread_ref_frame(&s->last_frame,
2073                                            &s->golden_frame)) < 0)
2074                 goto error;
2075             ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
2076         }
2077     }
2078
2079     memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2080     ff_thread_finish_setup(avctx);
2081
2082     if (unpack_superblocks(s, &gb)) {
2083         av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2084         goto error;
2085     }
2086     if (unpack_modes(s, &gb)) {
2087         av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2088         goto error;
2089     }
2090     if (unpack_vectors(s, &gb)) {
2091         av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2092         goto error;
2093     }
2094     if (unpack_block_qpis(s, &gb)) {
2095         av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2096         goto error;
2097     }
2098     if (unpack_dct_coeffs(s, &gb)) {
2099         av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2100         goto error;
2101     }
2102
2103     for (i = 0; i < 3; i++) {
2104         int height = s->height >> (i && s->chroma_y_shift);
2105         if (s->flipped_image)
2106             s->data_offset[i] = 0;
2107         else
2108             s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
2109     }
2110
2111     s->last_slice_end = 0;
2112     for (i = 0; i < s->c_superblock_height; i++)
2113         render_slice(s, i);
2114
2115     // filter the last row
2116     for (i = 0; i < 3; i++) {
2117         int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
2118         apply_loop_filter(s, i, row, row + 1);
2119     }
2120     vp3_draw_horiz_band(s, s->height);
2121
2122     /* output frame, offset as needed */
2123     if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
2124         return ret;
2125     for (i = 0; i < 3; i++) {
2126         AVFrame *dst = data;
2127         int off = (s->offset_x >> (i && s->chroma_y_shift)) +
2128                   (s->offset_y >> (i && s->chroma_y_shift)) * dst->linesize[i];
2129         dst->data[i] += off;
2130     }
2131     *got_frame = 1;
2132
2133     if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
2134         ret = update_frames(avctx);
2135         if (ret < 0)
2136             return ret;
2137     }
2138
2139     return buf_size;
2140
2141 error:
2142     ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
2143
2144     if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
2145         av_frame_unref(s->current_frame.f);
2146
2147     return -1;
2148 }
2149
2150 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
2151 {
2152     Vp3DecodeContext *s = avctx->priv_data;
2153
2154     if (get_bits1(gb)) {
2155         int token;
2156         if (s->entries >= 32) { /* overflow */
2157             av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2158             return -1;
2159         }
2160         token = get_bits(gb, 5);
2161         ff_dlog(avctx, "hti %d hbits %x token %d entry : %d size %d\n",
2162                 s->hti, s->hbits, token, s->entries, s->huff_code_size);
2163         s->huffman_table[s->hti][token][0] = s->hbits;
2164         s->huffman_table[s->hti][token][1] = s->huff_code_size;
2165         s->entries++;
2166     } else {
2167         if (s->huff_code_size >= 32) { /* overflow */
2168             av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2169             return -1;
2170         }
2171         s->huff_code_size++;
2172         s->hbits <<= 1;
2173         if (read_huffman_tree(avctx, gb))
2174             return -1;
2175         s->hbits |= 1;
2176         if (read_huffman_tree(avctx, gb))
2177             return -1;
2178         s->hbits >>= 1;
2179         s->huff_code_size--;
2180     }
2181     return 0;
2182 }
2183
2184 static int vp3_init_thread_copy(AVCodecContext *avctx)
2185 {
2186     Vp3DecodeContext *s = avctx->priv_data;
2187
2188     s->superblock_coding      = NULL;
2189     s->all_fragments          = NULL;
2190     s->coded_fragment_list[0] = NULL;
2191     s->dct_tokens_base        = NULL;
2192     s->superblock_fragments   = NULL;
2193     s->macroblock_coding      = NULL;
2194     s->motion_val[0]          = NULL;
2195     s->motion_val[1]          = NULL;
2196     s->edge_emu_buffer        = NULL;
2197
2198     return init_frames(s);
2199 }
2200
2201 #if CONFIG_THEORA_DECODER
2202 static const enum AVPixelFormat theora_pix_fmts[4] = {
2203     AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
2204 };
2205
2206 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2207 {
2208     Vp3DecodeContext *s = avctx->priv_data;
2209     int visible_width, visible_height, colorspace;
2210     uint8_t offset_x = 0, offset_y = 0;
2211     int ret;
2212     AVRational fps, aspect;
2213
2214     s->theora = get_bits_long(gb, 24);
2215     av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2216
2217     /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
2218      * but previous versions have the image flipped relative to vp3 */
2219     if (s->theora < 0x030200) {
2220         s->flipped_image = 1;
2221         av_log(avctx, AV_LOG_DEBUG,
2222                "Old (<alpha3) Theora bitstream, flipped image\n");
2223     }
2224
2225     visible_width  =
2226     s->width       = get_bits(gb, 16) << 4;
2227     visible_height =
2228     s->height      = get_bits(gb, 16) << 4;
2229
2230     if (s->theora >= 0x030200) {
2231         visible_width  = get_bits_long(gb, 24);
2232         visible_height = get_bits_long(gb, 24);
2233
2234         offset_x = get_bits(gb, 8); /* offset x */
2235         offset_y = get_bits(gb, 8); /* offset y, from bottom */
2236     }
2237
2238     /* sanity check */
2239     if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 ||
2240         visible_width  + offset_x > s->width ||
2241         visible_height + offset_y > s->height) {
2242         av_log(s, AV_LOG_ERROR,
2243                "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
2244                visible_width, visible_height, offset_x, offset_y,
2245                s->width, s->height);
2246         return AVERROR_INVALIDDATA;
2247     }
2248
2249     fps.num = get_bits_long(gb, 32);
2250     fps.den = get_bits_long(gb, 32);
2251     if (fps.num && fps.den) {
2252         if (fps.num < 0 || fps.den < 0) {
2253             av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
2254             return AVERROR_INVALIDDATA;
2255         }
2256         av_reduce(&avctx->framerate.den, &avctx->framerate.num,
2257                   fps.den, fps.num, 1 << 30);
2258     }
2259
2260     aspect.num = get_bits_long(gb, 24);
2261     aspect.den = get_bits_long(gb, 24);
2262     if (aspect.num && aspect.den) {
2263         av_reduce(&avctx->sample_aspect_ratio.num,
2264                   &avctx->sample_aspect_ratio.den,
2265                   aspect.num, aspect.den, 1 << 30);
2266         ff_set_sar(avctx, avctx->sample_aspect_ratio);
2267     }
2268
2269     if (s->theora < 0x030200)
2270         skip_bits(gb, 5); /* keyframe frequency force */
2271     colorspace = get_bits(gb, 8);
2272     skip_bits(gb, 24); /* bitrate */
2273
2274     skip_bits(gb, 6); /* quality hint */
2275
2276     if (s->theora >= 0x030200) {
2277         skip_bits(gb, 5); /* keyframe frequency force */
2278         avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
2279         skip_bits(gb, 3); /* reserved */
2280     }
2281
2282     ret = ff_set_dimensions(avctx, s->width, s->height);
2283     if (ret < 0)
2284         return ret;
2285     if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) &&
2286         (visible_width != s->width || visible_height != s->height)) {
2287         avctx->width  = visible_width;
2288         avctx->height = visible_height;
2289         // translate offsets from theora axis ([0,0] lower left)
2290         // to normal axis ([0,0] upper left)
2291         s->offset_x = offset_x;
2292         s->offset_y = s->height - visible_height - offset_y;
2293
2294         if ((s->offset_x & 0x1F) && !(avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {
2295             s->offset_x &= ~0x1F;
2296             av_log(avctx, AV_LOG_WARNING, "Reducing offset_x from %d to %d"
2297                    "chroma samples to preserve alignment.\n",
2298                    offset_x, s->offset_x);
2299         }
2300     }
2301
2302     if (colorspace == 1)
2303         avctx->color_primaries = AVCOL_PRI_BT470M;
2304     else if (colorspace == 2)
2305         avctx->color_primaries = AVCOL_PRI_BT470BG;
2306
2307     if (colorspace == 1 || colorspace == 2) {
2308         avctx->colorspace = AVCOL_SPC_BT470BG;
2309         avctx->color_trc  = AVCOL_TRC_BT709;
2310     }
2311
2312     return 0;
2313 }
2314
2315 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2316 {
2317     Vp3DecodeContext *s = avctx->priv_data;
2318     int i, n, matrices, inter, plane;
2319
2320     if (s->theora >= 0x030200) {
2321         n = get_bits(gb, 3);
2322         /* loop filter limit values table */
2323         if (n)
2324             for (i = 0; i < 64; i++)
2325                 s->filter_limit_values[i] = get_bits(gb, n);
2326     }
2327
2328     if (s->theora >= 0x030200)
2329         n = get_bits(gb, 4) + 1;
2330     else
2331         n = 16;
2332     /* quality threshold table */
2333     for (i = 0; i < 64; i++)
2334         s->coded_ac_scale_factor[i] = get_bits(gb, n);
2335
2336     if (s->theora >= 0x030200)
2337         n = get_bits(gb, 4) + 1;
2338     else
2339         n = 16;
2340     /* dc scale factor table */
2341     for (i = 0; i < 64; i++)
2342         s->coded_dc_scale_factor[i] = get_bits(gb, n);
2343
2344     if (s->theora >= 0x030200)
2345         matrices = get_bits(gb, 9) + 1;
2346     else
2347         matrices = 3;
2348
2349     if (matrices > 384) {
2350         av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
2351         return -1;
2352     }
2353
2354     for (n = 0; n < matrices; n++)
2355         for (i = 0; i < 64; i++)
2356             s->base_matrix[n][i] = get_bits(gb, 8);
2357
2358     for (inter = 0; inter <= 1; inter++) {
2359         for (plane = 0; plane <= 2; plane++) {
2360             int newqr = 1;
2361             if (inter || plane > 0)
2362                 newqr = get_bits1(gb);
2363             if (!newqr) {
2364                 int qtj, plj;
2365                 if (inter && get_bits1(gb)) {
2366                     qtj = 0;
2367                     plj = plane;
2368                 } else {
2369                     qtj = (3 * inter + plane - 1) / 3;
2370                     plj = (plane + 2) % 3;
2371                 }
2372                 s->qr_count[inter][plane] = s->qr_count[qtj][plj];
2373                 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
2374                        sizeof(s->qr_size[0][0]));
2375                 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
2376                        sizeof(s->qr_base[0][0]));
2377             } else {
2378                 int qri = 0;
2379                 int qi  = 0;
2380
2381                 for (;;) {
2382                     i = get_bits(gb, av_log2(matrices - 1) + 1);
2383                     if (i >= matrices) {
2384                         av_log(avctx, AV_LOG_ERROR,
2385                                "invalid base matrix index\n");
2386                         return -1;
2387                     }
2388                     s->qr_base[inter][plane][qri] = i;
2389                     if (qi >= 63)
2390                         break;
2391                     i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
2392                     s->qr_size[inter][plane][qri++] = i;
2393                     qi += i;
2394                 }
2395
2396                 if (qi > 63) {
2397                     av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
2398                     return -1;
2399                 }
2400                 s->qr_count[inter][plane] = qri;
2401             }
2402         }
2403     }
2404
2405     /* Huffman tables */
2406     for (s->hti = 0; s->hti < 80; s->hti++) {
2407         s->entries        = 0;
2408         s->huff_code_size = 1;
2409         if (!get_bits1(gb)) {
2410             s->hbits = 0;
2411             if (read_huffman_tree(avctx, gb))
2412                 return -1;
2413             s->hbits = 1;
2414             if (read_huffman_tree(avctx, gb))
2415                 return -1;
2416         }
2417     }
2418
2419     s->theora_tables = 1;
2420
2421     return 0;
2422 }
2423
2424 static av_cold int theora_decode_init(AVCodecContext *avctx)
2425 {
2426     Vp3DecodeContext *s = avctx->priv_data;
2427     GetBitContext gb;
2428     int ptype;
2429     uint8_t *header_start[3];
2430     int header_len[3];
2431     int i;
2432
2433     s->theora = 1;
2434
2435     if (!avctx->extradata_size) {
2436         av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
2437         return -1;
2438     }
2439
2440     if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2441                                   42, header_start, header_len) < 0) {
2442         av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2443         return -1;
2444     }
2445
2446     for (i = 0; i < 3; i++) {
2447         if (header_len[i] <= 0)
2448             continue;
2449         init_get_bits(&gb, header_start[i], header_len[i] * 8);
2450
2451         ptype = get_bits(&gb, 8);
2452
2453         if (!(ptype & 0x80)) {
2454             av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
2455 //          return -1;
2456         }
2457
2458         // FIXME: Check for this as well.
2459         skip_bits_long(&gb, 6 * 8); /* "theora" */
2460
2461         switch (ptype) {
2462         case 0x80:
2463             theora_decode_header(avctx, &gb);
2464             break;
2465         case 0x81:
2466 // FIXME: is this needed? it breaks sometimes
2467 //            theora_decode_comments(avctx, gb);
2468             break;
2469         case 0x82:
2470             if (theora_decode_tables(avctx, &gb))
2471                 return -1;
2472             break;
2473         default:
2474             av_log(avctx, AV_LOG_ERROR,
2475                    "Unknown Theora config packet: %d\n", ptype & ~0x80);
2476             break;
2477         }
2478         if (ptype != 0x81 && 8 * header_len[i] != get_bits_count(&gb))
2479             av_log(avctx, AV_LOG_WARNING,
2480                    "%d bits left in packet %X\n",
2481                    8 * header_len[i] - get_bits_count(&gb), ptype);
2482         if (s->theora < 0x030200)
2483             break;
2484     }
2485
2486     return vp3_decode_init(avctx);
2487 }
2488
2489 AVCodec ff_theora_decoder = {
2490     .name                  = "theora",
2491     .long_name             = NULL_IF_CONFIG_SMALL("Theora"),
2492     .type                  = AVMEDIA_TYPE_VIDEO,
2493     .id                    = AV_CODEC_ID_THEORA,
2494     .priv_data_size        = sizeof(Vp3DecodeContext),
2495     .init                  = theora_decode_init,
2496     .close                 = vp3_decode_end,
2497     .decode                = vp3_decode_frame,
2498     .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
2499                              AV_CODEC_CAP_FRAME_THREADS,
2500     .flush                 = vp3_decode_flush,
2501     .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2502     .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2503 };
2504 #endif
2505
2506 AVCodec ff_vp3_decoder = {
2507     .name                  = "vp3",
2508     .long_name             = NULL_IF_CONFIG_SMALL("On2 VP3"),
2509     .type                  = AVMEDIA_TYPE_VIDEO,
2510     .id                    = AV_CODEC_ID_VP3,
2511     .priv_data_size        = sizeof(Vp3DecodeContext),
2512     .init                  = vp3_decode_init,
2513     .close                 = vp3_decode_end,
2514     .decode                = vp3_decode_frame,
2515     .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
2516                              AV_CODEC_CAP_FRAME_THREADS,
2517     .flush                 = vp3_decode_flush,
2518     .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2519     .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
2520 };