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