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