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