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