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