Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vp3.c @ 4cfbf61b

History | View | Annotate | Download (85.5 KB)

1
/*
2
 *
3
 * Copyright (C) 2003 the ffmpeg project
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 * VP3 Video Decoder by Mike Melanson (melanson@pcisys.net)
20
 *
21
 */
22

    
23
/**
24
 * @file vp3.c
25
 * On2 VP3 Video Decoder
26
 */
27

    
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#include <unistd.h>
32

    
33
#include "common.h"
34
#include "avcodec.h"
35
#include "dsputil.h"
36
#include "mpegvideo.h"
37
#include "dsputil.h"
38

    
39
#include "vp3data.h"
40

    
41
#define FRAGMENT_PIXELS 8
42

    
43
/* 
44
 * Debugging Variables
45
 * 
46
 * Define one or more of the following compile-time variables to 1 to obtain
47
 * elaborate information about certain aspects of the decoding process.
48
 *
49
 * KEYFRAMES_ONLY: set this to 1 to only see keyframes (VP3 slideshow mode)
50
 * DEBUG_VP3: high-level decoding flow
51
 * DEBUG_INIT: initialization parameters
52
 * DEBUG_DEQUANTIZERS: display how the dequanization tables are built
53
 * DEBUG_BLOCK_CODING: unpacking the superblock/macroblock/fragment coding
54
 * DEBUG_MODES: unpacking the coding modes for individual fragments
55
 * DEBUG_VECTORS: display the motion vectors
56
 * DEBUG_TOKEN: display exhaustive information about each DCT token
57
 * DEBUG_VLC: display the VLCs as they are extracted from the stream
58
 * DEBUG_DC_PRED: display the process of reversing DC prediction
59
 * DEBUG_IDCT: show every detail of the IDCT process
60
 */
61

    
62
#define KEYFRAMES_ONLY 0
63

    
64
#define DEBUG_VP3 0
65
#define DEBUG_INIT 0
66
#define DEBUG_DEQUANTIZERS 0
67
#define DEBUG_BLOCK_CODING 0
68
#define DEBUG_MODES 0
69
#define DEBUG_VECTORS 0
70
#define DEBUG_TOKEN 0
71
#define DEBUG_VLC 0
72
#define DEBUG_DC_PRED 0
73
#define DEBUG_IDCT 0
74

    
75
#if DEBUG_VP3
76
#define debug_vp3 printf
77
#else
78
static inline void debug_vp3(const char *format, ...) { }
79
#endif
80

    
81
#if DEBUG_INIT
82
#define debug_init printf
83
#else
84
static inline void debug_init(const char *format, ...) { }
85
#endif
86

    
87
#if DEBUG_DEQUANTIZERS
88
#define debug_dequantizers printf 
89
#else
90
static inline void debug_dequantizers(const char *format, ...) { } 
91
#endif
92

    
93
#if DEBUG_BLOCK_CODING
94
#define debug_block_coding printf 
95
#else
96
static inline void debug_block_coding(const char *format, ...) { } 
97
#endif
98

    
99
#if DEBUG_MODES
100
#define debug_modes printf 
101
#else
102
static inline void debug_modes(const char *format, ...) { } 
103
#endif
104

    
105
#if DEBUG_VECTORS
106
#define debug_vectors printf 
107
#else
108
static inline void debug_vectors(const char *format, ...) { } 
109
#endif
110

    
111
#if DEBUG_TOKEN 
112
#define debug_token printf 
113
#else
114
static inline void debug_token(const char *format, ...) { } 
115
#endif
116

    
117
#if DEBUG_VLC
118
#define debug_vlc printf 
119
#else
120
static inline void debug_vlc(const char *format, ...) { } 
121
#endif
122

    
123
#if DEBUG_DC_PRED
124
#define debug_dc_pred printf 
125
#else
126
static inline void debug_dc_pred(const char *format, ...) { } 
127
#endif
128

    
129
#if DEBUG_IDCT
130
#define debug_idct printf 
131
#else
132
static inline void debug_idct(const char *format, ...) { } 
133
#endif
134

    
135
typedef struct Vp3Fragment {
136
    DCTELEM coeffs[64];
137
    int coding_method;
138
    int coeff_count;
139
    int last_coeff;
140
    int motion_x;
141
    int motion_y;
142
    /* this indicates which ffmpeg put_pixels() function to use:
143
     * 00b = no halfpel, 01b = x halfpel, 10b = y halfpel, 11b = both halfpel */
144
    int motion_halfpel_index;
145
    /* address of first pixel taking into account which plane the fragment
146
     * lives on as well as the plane stride */
147
    int first_pixel;
148
    /* this is the macroblock that the fragment belongs to */
149
    int macroblock;
150
} Vp3Fragment;
151

    
152
#define SB_NOT_CODED        0
153
#define SB_PARTIALLY_CODED  1
154
#define SB_FULLY_CODED      2
155

    
156
#define MODE_INTER_NO_MV      0
157
#define MODE_INTRA            1
158
#define MODE_INTER_PLUS_MV    2
159
#define MODE_INTER_LAST_MV    3
160
#define MODE_INTER_PRIOR_LAST 4
161
#define MODE_USING_GOLDEN     5
162
#define MODE_GOLDEN_MV        6
163
#define MODE_INTER_FOURMV     7
164
#define CODING_MODE_COUNT     8
165

    
166
/* special internal mode */
167
#define MODE_COPY             8
168

    
169
/* There are 6 preset schemes, plus a free-form scheme */
170
static int ModeAlphabet[7][CODING_MODE_COUNT] =
171
{
172
    /* this is the custom scheme */
173
    { 0, 0, 0, 0, 0, 0, 0, 0 },
174

    
175
    /* scheme 1: Last motion vector dominates */
176
    {    MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,  
177
         MODE_INTER_PLUS_MV,    MODE_INTER_NO_MV,
178
         MODE_INTRA,            MODE_USING_GOLDEN,      
179
         MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
180

    
181
    /* scheme 2 */
182
    {    MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,  
183
         MODE_INTER_NO_MV,      MODE_INTER_PLUS_MV,
184
         MODE_INTRA,            MODE_USING_GOLDEN,      
185
         MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
186

    
187
    /* scheme 3 */
188
    {    MODE_INTER_LAST_MV,    MODE_INTER_PLUS_MV,     
189
         MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
190
         MODE_INTRA,            MODE_USING_GOLDEN,      
191
         MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
192

    
193
    /* scheme 4 */
194
    {    MODE_INTER_LAST_MV,    MODE_INTER_PLUS_MV,     
195
         MODE_INTER_NO_MV,      MODE_INTER_PRIOR_LAST,
196
         MODE_INTRA,            MODE_USING_GOLDEN,      
197
         MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
198

    
199
    /* scheme 5: No motion vector dominates */
200
    {    MODE_INTER_NO_MV,      MODE_INTER_LAST_MV,     
201
         MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
202
         MODE_INTRA,            MODE_USING_GOLDEN,      
203
         MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
204

    
205
    /* scheme 6 */
206
    {    MODE_INTER_NO_MV,      MODE_USING_GOLDEN,      
207
         MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
208
         MODE_INTER_PLUS_MV,    MODE_INTRA,             
209
         MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
210

    
211
};
212

    
213
#define MIN_DEQUANT_VAL 2
214

    
215
typedef struct Vp3DecodeContext {
216
    AVCodecContext *avctx;
217
    int width, height;
218
    AVFrame golden_frame;
219
    AVFrame last_frame;
220
    AVFrame current_frame;
221
    int keyframe;
222
    DSPContext dsp;
223

    
224
    int quality_index;
225
    int last_quality_index;
226

    
227
    int superblock_count;
228
    int superblock_width;
229
    int superblock_height;
230
    int y_superblock_width;
231
    int y_superblock_height;
232
    int c_superblock_width;
233
    int c_superblock_height;
234
    int u_superblock_start;
235
    int v_superblock_start;
236
    unsigned char *superblock_coding;
237

    
238
    int macroblock_count;
239
    int macroblock_width;
240
    int macroblock_height;
241

    
242
    int fragment_count;
243
    int fragment_width;
244
    int fragment_height;
245

    
246
    Vp3Fragment *all_fragments;
247
    int u_fragment_start;
248
    int v_fragment_start;
249

    
250
    /* this is a list of indices into the all_fragments array indicating
251
     * which of the fragments are coded */
252
    int *coded_fragment_list;
253
    int coded_fragment_list_index;
254
    int pixel_addresses_inited;
255

    
256
    VLC dc_vlc[16];
257
    VLC ac_vlc_1[16];
258
    VLC ac_vlc_2[16];
259
    VLC ac_vlc_3[16];
260
    VLC ac_vlc_4[16];
261

    
262
    int16_t intra_y_dequant[64];
263
    int16_t intra_c_dequant[64];
264
    int16_t inter_dequant[64];
265

    
266
    /* This table contains superblock_count * 16 entries. Each set of 16
267
     * numbers corresponds to the fragment indices 0..15 of the superblock.
268
     * An entry will be -1 to indicate that no entry corresponds to that
269
     * index. */
270
    int *superblock_fragments;
271

    
272
    /* This table contains superblock_count * 4 entries. Each set of 4
273
     * numbers corresponds to the macroblock indices 0..3 of the superblock.
274
     * An entry will be -1 to indicate that no entry corresponds to that
275
     * index. */
276
    int *superblock_macroblocks;
277

    
278
    /* This table contains macroblock_count * 6 entries. Each set of 6
279
     * numbers corresponds to the fragment indices 0..5 which comprise
280
     * the macroblock (4 Y fragments and 2 C fragments). */
281
    int *macroblock_fragments;
282
    /* This is an array that indicates how a particular macroblock 
283
     * is coded. */
284
    unsigned char *macroblock_coding;
285

    
286
    int first_coded_y_fragment;
287
    int first_coded_c_fragment;
288
    int last_coded_y_fragment;
289
    int last_coded_c_fragment;
290

    
291
} Vp3DecodeContext;
292

    
293
/************************************************************************
294
 * VP3 specific functions
295
 ************************************************************************/
296

    
297
/*
298
 * This function sets up all of the various blocks mappings:
299
 * superblocks <-> fragments, macroblocks <-> fragments,
300
 * superblocks <-> macroblocks
301
 *
302
 * Returns 0 is successful; returns 1 if *anything* went wrong.
303
 */
304
static int init_block_mapping(Vp3DecodeContext *s) 
305
{
306
    int i, j;
307
    signed int hilbert_walk_y[16];
308
    signed int hilbert_walk_c[16];
309
    signed int hilbert_walk_mb[4];
310

    
311
    int current_fragment = 0;
312
    int current_width = 0;
313
    int current_height = 0;
314
    int right_edge = 0;
315
    int bottom_edge = 0;
316
    int superblock_row_inc = 0;
317
    int *hilbert = NULL;
318
    int mapping_index = 0;
319

    
320
    int current_macroblock;
321
    int c_fragment;
322

    
323
    signed char travel_width[16] = {
324
         1,  1,  0, -1, 
325
         0,  0,  1,  0,
326
         1,  0,  1,  0,
327
         0, -1,  0,  1
328
    };
329

    
330
    signed char travel_height[16] = {
331
         0,  0,  1,  0,
332
         1,  1,  0, -1,
333
         0,  1,  0, -1,
334
        -1,  0, -1,  0
335
    };
336

    
337
    signed char travel_width_mb[4] = {
338
         1,  0,  1,  0
339
    };
340

    
341
    signed char travel_height_mb[4] = {
342
         0,  1,  0, -1
343
    };
344

    
345
    debug_vp3("  vp3: initialize block mapping tables\n");
346

    
347
    /* figure out hilbert pattern per these frame dimensions */
348
    hilbert_walk_y[0]  = 1;
349
    hilbert_walk_y[1]  = 1;
350
    hilbert_walk_y[2]  = s->fragment_width;
351
    hilbert_walk_y[3]  = -1;
352
    hilbert_walk_y[4]  = s->fragment_width;
353
    hilbert_walk_y[5]  = s->fragment_width;
354
    hilbert_walk_y[6]  = 1;
355
    hilbert_walk_y[7]  = -s->fragment_width;
356
    hilbert_walk_y[8]  = 1;
357
    hilbert_walk_y[9]  = s->fragment_width;
358
    hilbert_walk_y[10]  = 1;
359
    hilbert_walk_y[11] = -s->fragment_width;
360
    hilbert_walk_y[12] = -s->fragment_width;
361
    hilbert_walk_y[13] = -1;
362
    hilbert_walk_y[14] = -s->fragment_width;
363
    hilbert_walk_y[15] = 1;
364

    
365
    hilbert_walk_c[0]  = 1;
366
    hilbert_walk_c[1]  = 1;
367
    hilbert_walk_c[2]  = s->fragment_width / 2;
368
    hilbert_walk_c[3]  = -1;
369
    hilbert_walk_c[4]  = s->fragment_width / 2;
370
    hilbert_walk_c[5]  = s->fragment_width / 2;
371
    hilbert_walk_c[6]  = 1;
372
    hilbert_walk_c[7]  = -s->fragment_width / 2;
373
    hilbert_walk_c[8]  = 1;
374
    hilbert_walk_c[9]  = s->fragment_width / 2;
375
    hilbert_walk_c[10]  = 1;
376
    hilbert_walk_c[11] = -s->fragment_width / 2;
377
    hilbert_walk_c[12] = -s->fragment_width / 2;
378
    hilbert_walk_c[13] = -1;
379
    hilbert_walk_c[14] = -s->fragment_width / 2;
380
    hilbert_walk_c[15] = 1;
381

    
382
    hilbert_walk_mb[0] = 1;
383
    hilbert_walk_mb[1] = s->macroblock_width;
384
    hilbert_walk_mb[2] = 1;
385
    hilbert_walk_mb[3] = -s->macroblock_width;
386

    
387
    /* iterate through each superblock (all planes) and map the fragments */
388
    for (i = 0; i < s->superblock_count; i++) {
389
        debug_init("    superblock %d (u starts @ %d, v starts @ %d)\n",
390
            i, s->u_superblock_start, s->v_superblock_start);
391

    
392
        /* time to re-assign the limits? */
393
        if (i == 0) {
394

    
395
            /* start of Y superblocks */
396
            right_edge = s->fragment_width;
397
            bottom_edge = s->fragment_height;
398
            current_width = -1;
399
            current_height = 0;
400
            superblock_row_inc = 3 * s->fragment_width - 
401
                (s->y_superblock_width * 4 - s->fragment_width);
402
            hilbert = hilbert_walk_y;
403

    
404
            /* the first operation for this variable is to advance by 1 */
405
            current_fragment = -1;
406

    
407
        } else if (i == s->u_superblock_start) {
408

    
409
            /* start of U superblocks */
410
            right_edge = s->fragment_width / 2;
411
            bottom_edge = s->fragment_height / 2;
412
            current_width = -1;
413
            current_height = 0;
414
            superblock_row_inc = 3 * (s->fragment_width / 2) - 
415
                (s->c_superblock_width * 4 - s->fragment_width / 2);
416
            hilbert = hilbert_walk_c;
417

    
418
            /* the first operation for this variable is to advance by 1 */
419
            current_fragment = s->u_fragment_start - 1;
420

    
421
        } else if (i == s->v_superblock_start) {
422

    
423
            /* start of V superblocks */
424
            right_edge = s->fragment_width / 2;
425
            bottom_edge = s->fragment_height / 2;
426
            current_width = -1;
427
            current_height = 0;
428
            superblock_row_inc = 3 * (s->fragment_width / 2) - 
429
                (s->c_superblock_width * 4 - s->fragment_width / 2);
430
            hilbert = hilbert_walk_c;
431

    
432
            /* the first operation for this variable is to advance by 1 */
433
            current_fragment = s->v_fragment_start - 1;
434

    
435
        }
436

    
437
        if (current_width >= right_edge - 1) {
438
            /* reset width and move to next superblock row */
439
            current_width = -1;
440
            current_height += 4;
441

    
442
            /* fragment is now at the start of a new superblock row */
443
            current_fragment += superblock_row_inc;
444
        }
445

    
446
        /* iterate through all 16 fragments in a superblock */
447
        for (j = 0; j < 16; j++) {
448
            current_fragment += hilbert[j];
449
            current_width += travel_width[j];
450
            current_height += travel_height[j];
451

    
452
            /* check if the fragment is in bounds */
453
            if ((current_width < right_edge) &&
454
                (current_height < bottom_edge)) {
455
                s->superblock_fragments[mapping_index] = current_fragment;
456
                debug_init("    mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n", 
457
                    s->superblock_fragments[mapping_index], i, j,
458
                    current_width, right_edge, current_height, bottom_edge);
459
            } else {
460
                s->superblock_fragments[mapping_index] = -1;
461
                debug_init("    superblock %d, position %d has no fragment (%d/%d x %d/%d)\n", 
462
                    i, j,
463
                    current_width, right_edge, current_height, bottom_edge);
464
            }
465

    
466
            mapping_index++;
467
        }
468
    }
469

    
470
    /* initialize the superblock <-> macroblock mapping; iterate through
471
     * all of the Y plane superblocks to build this mapping */
472
    right_edge = s->macroblock_width;
473
    bottom_edge = s->macroblock_height;
474
    current_width = -1;
475
    current_height = 0;
476
    superblock_row_inc = s->macroblock_width -
477
        (s->y_superblock_width * 2 - s->macroblock_width);;
478
    hilbert = hilbert_walk_mb;
479
    mapping_index = 0;
480
    current_macroblock = -1;
481
    for (i = 0; i < s->u_superblock_start; i++) {
482

    
483
        if (current_width >= right_edge - 1) {
484
            /* reset width and move to next superblock row */
485
            current_width = -1;
486
            current_height += 2;
487

    
488
            /* macroblock is now at the start of a new superblock row */
489
            current_macroblock += superblock_row_inc;
490
        }
491

    
492
        /* iterate through each potential macroblock in the superblock */
493
        for (j = 0; j < 4; j++) {
494
            current_macroblock += hilbert_walk_mb[j];
495
            current_width += travel_width_mb[j];
496
            current_height += travel_height_mb[j];
497

    
498
            /* check if the macroblock is in bounds */
499
            if ((current_width < right_edge) &&
500
                (current_height < bottom_edge)) {
501
                s->superblock_macroblocks[mapping_index] = current_macroblock;
502
                debug_init("    mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
503
                    s->superblock_macroblocks[mapping_index], i, j,
504
                    current_width, right_edge, current_height, bottom_edge);
505
            } else {
506
                s->superblock_macroblocks[mapping_index] = -1;
507
                debug_init("    superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
508
                    i, j,
509
                    current_width, right_edge, current_height, bottom_edge);
510
            }
511

    
512
            mapping_index++;
513
        }
514
    }
515

    
516
    /* initialize the macroblock <-> fragment mapping */
517
    current_fragment = 0;
518
    current_macroblock = 0;
519
    mapping_index = 0;
520
    for (i = 0; i < s->fragment_height; i += 2) {
521

    
522
        for (j = 0; j < s->fragment_width; j += 2) {
523

    
524
            debug_init("    macroblock %d contains fragments: ", current_macroblock);
525
            s->all_fragments[current_fragment].macroblock = current_macroblock;
526
            s->macroblock_fragments[mapping_index++] = current_fragment;
527
            debug_init("%d ", current_fragment);
528

    
529
            if (j + 1 < s->fragment_width) {
530
                s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
531
                s->macroblock_fragments[mapping_index++] = current_fragment + 1;
532
                debug_init("%d ", current_fragment + 1);
533
            } else
534
                s->macroblock_fragments[mapping_index++] = -1;
535

    
536
            if (i + 1 < s->fragment_height) {
537
                s->all_fragments[current_fragment + s->fragment_width].macroblock = 
538
                    current_macroblock;
539
                s->macroblock_fragments[mapping_index++] = 
540
                    current_fragment + s->fragment_width;
541
                debug_init("%d ", current_fragment + s->fragment_width);
542
            } else
543
                s->macroblock_fragments[mapping_index++] = -1;
544

    
545
            if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
546
                s->all_fragments[current_fragment + s->fragment_width + 1].macroblock = 
547
                    current_macroblock;
548
                s->macroblock_fragments[mapping_index++] = 
549
                    current_fragment + s->fragment_width + 1;
550
                debug_init("%d ", current_fragment + s->fragment_width + 1);
551
            } else
552
                s->macroblock_fragments[mapping_index++] = -1;
553

    
554
            /* C planes */
555
            c_fragment = s->u_fragment_start + 
556
                (i * s->fragment_width / 4) + (j / 2);
557
            s->all_fragments[c_fragment].macroblock = s->macroblock_count;
558
            s->macroblock_fragments[mapping_index++] = c_fragment;
559
            debug_init("%d ", c_fragment);
560

    
561
            c_fragment = s->v_fragment_start + 
562
                (i * s->fragment_width / 4) + (j / 2);
563
            s->all_fragments[c_fragment].macroblock = s->macroblock_count;
564
            s->macroblock_fragments[mapping_index++] = c_fragment;
565
            debug_init("%d ", c_fragment);
566

    
567
            debug_init("\n");
568

    
569
            if (j + 2 <= s->fragment_width)
570
                current_fragment += 2;
571
            else 
572
                current_fragment++;
573
            current_macroblock++;
574
        }
575

    
576
        current_fragment += s->fragment_width;
577
    }
578

    
579
    return 0;  /* successful path out */
580
}
581

    
582
/*
583
 * This function unpacks a single token (which should be in the range 0..31)
584
 * and returns a zero run (number of zero coefficients in current DCT matrix
585
 * before next non-zero coefficient), the next DCT coefficient, and the
586
 * number of consecutive, non-EOB'd DCT blocks to EOB.
587
 */
588
static void unpack_token(GetBitContext *gb, int token, int *zero_run,
589
                         DCTELEM *coeff, int *eob_run) 
590
{
591
    int sign;
592

    
593
    *zero_run = 0;
594
    *eob_run = 0;
595
    *coeff = 0;
596

    
597
    debug_token("    vp3 token %d: ", token);
598
    switch (token) {
599

    
600
    case 0:
601
        debug_token("DCT_EOB_TOKEN, EOB next block\n");
602
        *eob_run = 1;
603
        break;
604

    
605
    case 1:
606
        debug_token("DCT_EOB_PAIR_TOKEN, EOB next 2 blocks\n");
607
        *eob_run = 2;
608
        break;
609

    
610
    case 2:
611
        debug_token("DCT_EOB_TRIPLE_TOKEN, EOB next 3 blocks\n");
612
        *eob_run = 3;
613
        break;
614

    
615
    case 3:
616
        debug_token("DCT_REPEAT_RUN_TOKEN, ");
617
        *eob_run = get_bits(gb, 2) + 4;
618
        debug_token("EOB the next %d blocks\n", *eob_run);
619
        break;
620

    
621
    case 4:
622
        debug_token("DCT_REPEAT_RUN2_TOKEN, ");
623
        *eob_run = get_bits(gb, 3) + 8;
624
        debug_token("EOB the next %d blocks\n", *eob_run);
625
        break;
626

    
627
    case 5:
628
        debug_token("DCT_REPEAT_RUN3_TOKEN, ");
629
        *eob_run = get_bits(gb, 4) + 16;
630
        debug_token("EOB the next %d blocks\n", *eob_run);
631
        break;
632

    
633
    case 6:
634
        debug_token("DCT_REPEAT_RUN4_TOKEN, ");
635
        *eob_run = get_bits(gb, 12);
636
        debug_token("EOB the next %d blocks\n", *eob_run);
637
        break;
638

    
639
    case 7:
640
        debug_token("DCT_SHORT_ZRL_TOKEN, ");
641
        /* note that this token actually indicates that (3 extra bits) + 1 0s
642
         * should be output; this case specifies a run of (3 EBs) 0s and a
643
         * coefficient of 0. */
644
        *zero_run = get_bits(gb, 3);
645
        *coeff = 0;
646
        debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
647
        break;
648

    
649
    case 8:
650
        debug_token("DCT_ZRL_TOKEN, ");
651
        /* note that this token actually indicates that (6 extra bits) + 1 0s
652
         * should be output; this case specifies a run of (6 EBs) 0s and a
653
         * coefficient of 0. */
654
        *zero_run = get_bits(gb, 6);
655
        *coeff = 0;
656
        debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
657
        break;
658

    
659
    case 9:
660
        debug_token("ONE_TOKEN, output 1\n");
661
        *coeff = 1;
662
        break;
663

    
664
    case 10:
665
        debug_token("MINUS_ONE_TOKEN, output -1\n");
666
        *coeff = -1;
667
        break;
668

    
669
    case 11:
670
        debug_token("TWO_TOKEN, output 2\n");
671
        *coeff = 2;
672
        break;
673

    
674
    case 12:
675
        debug_token("MINUS_TWO_TOKEN, output -2\n");
676
        *coeff = -2;
677
        break;
678

    
679
    case 13:
680
    case 14:
681
    case 15:
682
    case 16:
683
        debug_token("LOW_VAL_TOKENS, ");
684
        if (get_bits(gb, 1))
685
            *coeff = -(3 + (token - 13));
686
        else
687
            *coeff = 3 + (token - 13);
688
        debug_token("output %d\n", *coeff);
689
        break;
690

    
691
    case 17:
692
        debug_token("DCT_VAL_CATEGORY3, ");
693
        sign = get_bits(gb, 1);
694
        *coeff = 7 + get_bits(gb, 1);
695
        if (sign)
696
            *coeff = -(*coeff);
697
        debug_token("output %d\n", *coeff);
698
        break;
699

    
700
    case 18:
701
        debug_token("DCT_VAL_CATEGORY4, ");
702
        sign = get_bits(gb, 1);
703
        *coeff = 9 + get_bits(gb, 2);
704
        if (sign)
705
            *coeff = -(*coeff);
706
        debug_token("output %d\n", *coeff);
707
        break;
708

    
709
    case 19:
710
        debug_token("DCT_VAL_CATEGORY5, ");
711
        sign = get_bits(gb, 1);
712
        *coeff = 13 + get_bits(gb, 3);
713
        if (sign)
714
            *coeff = -(*coeff);
715
        debug_token("output %d\n", *coeff);
716
        break;
717

    
718
    case 20:
719
        debug_token("DCT_VAL_CATEGORY6, ");
720
        sign = get_bits(gb, 1);
721
        *coeff = 21 + get_bits(gb, 4);
722
        if (sign)
723
            *coeff = -(*coeff);
724
        debug_token("output %d\n", *coeff);
725
        break;
726

    
727
    case 21:
728
        debug_token("DCT_VAL_CATEGORY7, ");
729
        sign = get_bits(gb, 1);
730
        *coeff = 37 + get_bits(gb, 5);
731
        if (sign)
732
            *coeff = -(*coeff);
733
        debug_token("output %d\n", *coeff);
734
        break;
735

    
736
    case 22:
737
        debug_token("DCT_VAL_CATEGORY8, ");
738
        sign = get_bits(gb, 1);
739
        *coeff = 69 + get_bits(gb, 9);
740
        if (sign)
741
            *coeff = -(*coeff);
742
        debug_token("output %d\n", *coeff);
743
        break;
744

    
745
    case 23:
746
    case 24:
747
    case 25:
748
    case 26:
749
    case 27:
750
        debug_token("DCT_RUN_CATEGORY1, ");
751
        *zero_run = token - 22;
752
        if (get_bits(gb, 1))
753
            *coeff = -1;
754
        else
755
            *coeff = 1;
756
        debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
757
        break;
758

    
759
    case 28:
760
        debug_token("DCT_RUN_CATEGORY1B, ");
761
        if (get_bits(gb, 1))
762
            *coeff = -1;
763
        else
764
            *coeff = 1;
765
        *zero_run = 6 + get_bits(gb, 2);
766
        debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
767
        break;
768

    
769
    case 29:
770
        debug_token("DCT_RUN_CATEGORY1C, ");
771
        if (get_bits(gb, 1))
772
            *coeff = -1;
773
        else
774
            *coeff = 1;
775
        *zero_run = 10 + get_bits(gb, 3);
776
        debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
777
        break;
778

    
779
    case 30:
780
        debug_token("DCT_RUN_CATEGORY2, ");
781
        sign = get_bits(gb, 1);
782
        *coeff = 2 + get_bits(gb, 1);
783
        if (sign)
784
            *coeff = -(*coeff);
785
        *zero_run = 1;
786
        debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
787
        break;
788

    
789
    case 31:
790
        debug_token("DCT_RUN_CATEGORY2, ");
791
        sign = get_bits(gb, 1);
792
        *coeff = 2 + get_bits(gb, 1);
793
        if (sign)
794
            *coeff = -(*coeff);
795
        *zero_run = 2 + get_bits(gb, 1);
796
        debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
797
        break;
798

    
799
    default:
800
        printf ("  vp3: help! Got a bad token: %d > 31\n", token);
801
        break;
802

    
803
  }
804
}
805

    
806
/*
807
 * This function wipes out all of the fragment data.
808
 */
809
static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
810
{
811
    int i;
812

    
813
    /* zero out all of the fragment information */
814
    s->coded_fragment_list_index = 0;
815
    for (i = 0; i < s->fragment_count; i++) {
816
        memset(s->all_fragments[i].coeffs, 0, 64 * sizeof(DCTELEM));
817
        s->all_fragments[i].coeff_count = 0;
818
        s->all_fragments[i].last_coeff = 0;
819
    }
820
}
821

    
822
/*
823
 * This function sets of the dequantization tables used for a particular
824
 * frame.
825
 */
826
static void init_dequantizer(Vp3DecodeContext *s)
827
{
828

    
829
    int quality_scale = vp31_quality_threshold[s->quality_index];
830
    int dc_scale_factor = vp31_dc_scale_factor[s->quality_index];
831
    int i, j;
832

    
833
    debug_vp3("  vp3: initializing dequantization tables\n");
834

    
835
    /* 
836
     * Scale dequantizers:
837
     *
838
     *   quantizer * sf
839
     *   --------------
840
     *        100
841
     *
842
     * where sf = dc_scale_factor for DC quantizer
843
     *           or quality_scale for AC quantizer
844
     *
845
     * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL.
846
     */
847
#define SCALER 1
848

    
849
    /* scale DC quantizers */
850
    s->intra_y_dequant[0] = vp31_intra_y_dequant[0] * dc_scale_factor / 100;
851
    if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2)
852
        s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2;
853
    s->intra_y_dequant[0] *= SCALER;
854

    
855
    s->intra_c_dequant[0] = vp31_intra_c_dequant[0] * dc_scale_factor / 100;
856
    if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2)
857
        s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2;
858
    s->intra_c_dequant[0] *= SCALER;
859

    
860
    s->inter_dequant[0] = vp31_inter_dequant[0] * dc_scale_factor / 100;
861
    if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4)
862
        s->inter_dequant[0] = MIN_DEQUANT_VAL * 4;
863
    s->inter_dequant[0] *= SCALER;
864

    
865
    /* scale AC quantizers, zigzag at the same time in preparation for
866
     * the dequantization phase */
867
    for (i = 1; i < 64; i++) {
868

    
869
        j = zigzag_index[i];
870

    
871
        s->intra_y_dequant[j] = vp31_intra_y_dequant[i] * quality_scale / 100;
872
        if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL)
873
            s->intra_y_dequant[j] = MIN_DEQUANT_VAL;
874
        s->intra_y_dequant[j] *= SCALER;
875

    
876
        s->intra_c_dequant[j] = vp31_intra_c_dequant[i] * quality_scale / 100;
877
        if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL)
878
            s->intra_c_dequant[j] = MIN_DEQUANT_VAL;
879
        s->intra_c_dequant[j] *= SCALER;
880

    
881
        s->inter_dequant[j] = vp31_inter_dequant[i] * quality_scale / 100;
882
        if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2)
883
            s->inter_dequant[j] = MIN_DEQUANT_VAL * 2;
884
        s->inter_dequant[j] *= SCALER;
885
    }
886

    
887
    /* print debug information as requested */
888
    debug_dequantizers("intra Y dequantizers:\n");
889
    for (i = 0; i < 8; i++) {
890
      for (j = i * 8; j < i * 8 + 8; j++) {
891
        debug_dequantizers(" %4d,", s->intra_y_dequant[j]);
892
      }
893
      debug_dequantizers("\n");
894
    }
895
    debug_dequantizers("\n");
896

    
897
    debug_dequantizers("intra C dequantizers:\n");
898
    for (i = 0; i < 8; i++) {
899
      for (j = i * 8; j < i * 8 + 8; j++) {
900
        debug_dequantizers(" %4d,", s->intra_c_dequant[j]);
901
      }
902
      debug_dequantizers("\n");
903
    }
904
    debug_dequantizers("\n");
905

    
906
    debug_dequantizers("interframe dequantizers:\n");
907
    for (i = 0; i < 8; i++) {
908
      for (j = i * 8; j < i * 8 + 8; j++) {
909
        debug_dequantizers(" %4d,", s->inter_dequant[j]);
910
      }
911
      debug_dequantizers("\n");
912
    }
913
    debug_dequantizers("\n");
914
}
915

    
916
/*
917
 * This function is used to fetch runs of 1s or 0s from the bitstream for
918
 * use in determining which superblocks are fully and partially coded.
919
 *
920
 *  Codeword                RunLength
921
 *  0                       1
922
 *  10x                     2-3
923
 *  110x                    4-5
924
 *  1110xx                  6-9
925
 *  11110xxx                10-17
926
 *  111110xxxx              18-33
927
 *  111111xxxxxxxxxxxx      34-4129
928
 */
929
static int get_superblock_run_length(GetBitContext *gb)
930
{
931

    
932
    if (get_bits(gb, 1) == 0)
933
        return 1;
934

    
935
    else if (get_bits(gb, 1) == 0)
936
        return (2 + get_bits(gb, 1));
937

    
938
    else if (get_bits(gb, 1) == 0)
939
        return (4 + get_bits(gb, 1));
940

    
941
    else if (get_bits(gb, 1) == 0)
942
        return (6 + get_bits(gb, 2));
943

    
944
    else if (get_bits(gb, 1) == 0)
945
        return (10 + get_bits(gb, 3));
946

    
947
    else if (get_bits(gb, 1) == 0)
948
        return (18 + get_bits(gb, 4));
949

    
950
    else
951
        return (34 + get_bits(gb, 12));
952

    
953
}
954

    
955
/*
956
 * This function is used to fetch runs of 1s or 0s from the bitstream for
957
 * use in determining which particular fragments are coded.
958
 *
959
 * Codeword                RunLength
960
 * 0x                      1-2
961
 * 10x                     3-4
962
 * 110x                    5-6
963
 * 1110xx                  7-10
964
 * 11110xx                 11-14
965
 * 11111xxxx               15-30
966
 */
967
static int get_fragment_run_length(GetBitContext *gb)
968
{
969

    
970
    if (get_bits(gb, 1) == 0)
971
        return (1 + get_bits(gb, 1));
972

    
973
    else if (get_bits(gb, 1) == 0)
974
        return (3 + get_bits(gb, 1));
975

    
976
    else if (get_bits(gb, 1) == 0)
977
        return (5 + get_bits(gb, 1));
978

    
979
    else if (get_bits(gb, 1) == 0)
980
        return (7 + get_bits(gb, 2));
981

    
982
    else if (get_bits(gb, 1) == 0)
983
        return (11 + get_bits(gb, 2));
984

    
985
    else
986
        return (15 + get_bits(gb, 4));
987

    
988
}
989

    
990
/*
991
 * This function decodes a VLC from the bitstream and returns a number
992
 * that ranges from 0..7. The number indicates which of the 8 coding
993
 * modes to use.
994
 *
995
 *  VLC       Number
996
 *  0            0
997
 *  10           1
998
 *  110          2
999
 *  1110         3
1000
 *  11110        4
1001
 *  111110       5
1002
 *  1111110      6
1003
 *  1111111      7
1004
 *
1005
 */
1006
static int get_mode_code(GetBitContext *gb)
1007
{
1008

    
1009
    if (get_bits(gb, 1) == 0)
1010
        return 0;
1011

    
1012
    else if (get_bits(gb, 1) == 0)
1013
        return 1;
1014

    
1015
    else if (get_bits(gb, 1) == 0)
1016
        return 2;
1017

    
1018
    else if (get_bits(gb, 1) == 0)
1019
        return 3;
1020

    
1021
    else if (get_bits(gb, 1) == 0)
1022
        return 4;
1023

    
1024
    else if (get_bits(gb, 1) == 0)
1025
        return 5;
1026

    
1027
    else if (get_bits(gb, 1) == 0)
1028
        return 6;
1029

    
1030
    else
1031
        return 7;
1032

    
1033
}
1034

    
1035
/*
1036
 * This function extracts a motion vector from the bitstream using a VLC
1037
 * scheme. 3 bits are fetched from the bitstream and 1 of 8 actions is
1038
 * taken depending on the value on those 3 bits:
1039
 *
1040
 *  0: return 0
1041
 *  1: return 1
1042
 *  2: return -1
1043
 *  3: if (next bit is 1) return -2, else return 2
1044
 *  4: if (next bit is 1) return -3, else return 3
1045
 *  5: return 4 + (next 2 bits), next bit is sign
1046
 *  6: return 8 + (next 3 bits), next bit is sign
1047
 *  7: return 16 + (next 4 bits), next bit is sign
1048
 */
1049
static int get_motion_vector_vlc(GetBitContext *gb)
1050
{
1051
    int bits;
1052

    
1053
    bits = get_bits(gb, 3);
1054

    
1055
    switch(bits) {
1056

    
1057
    case 0:
1058
        bits = 0;
1059
        break;
1060

    
1061
    case 1:
1062
        bits = 1;
1063
        break;
1064

    
1065
    case 2:
1066
        bits = -1;
1067
        break;
1068

    
1069
    case 3:
1070
        if (get_bits(gb, 1) == 0)
1071
            bits = 2;
1072
        else
1073
            bits = -2;
1074
        break;
1075

    
1076
    case 4:
1077
        if (get_bits(gb, 1) == 0)
1078
            bits = 3;
1079
        else
1080
            bits = -3;
1081
        break;
1082

    
1083
    case 5:
1084
        bits = 4 + get_bits(gb, 2);
1085
        if (get_bits(gb, 1) == 1)
1086
            bits = -bits;
1087
        break;
1088

    
1089
    case 6:
1090
        bits = 8 + get_bits(gb, 3);
1091
        if (get_bits(gb, 1) == 1)
1092
            bits = -bits;
1093
        break;
1094

    
1095
    case 7:
1096
        bits = 16 + get_bits(gb, 4);
1097
        if (get_bits(gb, 1) == 1)
1098
            bits = -bits;
1099
        break;
1100

    
1101
    }
1102

    
1103
    return bits;
1104
}
1105

    
1106
/*
1107
 * This function fetches a 5-bit number from the stream followed by
1108
 * a sign and calls it a motion vector.
1109
 */
1110
static int get_motion_vector_fixed(GetBitContext *gb)
1111
{
1112

    
1113
    int bits;
1114

    
1115
    bits = get_bits(gb, 5);
1116

    
1117
    if (get_bits(gb, 1) == 1)
1118
        bits = -bits;
1119

    
1120
    return bits;
1121
}
1122

    
1123
/*
1124
 * This function unpacks all of the superblock/macroblock/fragment coding 
1125
 * information from the bitstream.
1126
 */
1127
static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
1128
{
1129
    int bit = 0;
1130
    int current_superblock = 0;
1131
    int current_run = 0;
1132
    int decode_fully_flags = 0;
1133
    int decode_partial_blocks = 0;
1134
    int first_c_fragment_seen;
1135

    
1136
    int i, j;
1137
    int current_fragment;
1138

    
1139
    debug_vp3("  vp3: unpacking superblock coding\n");
1140

    
1141
    if (s->keyframe) {
1142

    
1143
        debug_vp3("    keyframe-- all superblocks are fully coded\n");
1144
        memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
1145

    
1146
    } else {
1147

    
1148
        /* unpack the list of partially-coded superblocks */
1149
        bit = get_bits(gb, 1);
1150
        /* toggle the bit because as soon as the first run length is 
1151
         * fetched the bit will be toggled again */
1152
        bit ^= 1;
1153
        while (current_superblock < s->superblock_count) {
1154
            if (current_run == 0) {
1155
                bit ^= 1;
1156
                current_run = get_superblock_run_length(gb);
1157
                debug_block_coding("      setting superblocks %d..%d to %s\n",
1158
                    current_superblock,
1159
                    current_superblock + current_run - 1,
1160
                    (bit) ? "partially coded" : "not coded");
1161

    
1162
                /* if any of the superblocks are not partially coded, flag
1163
                 * a boolean to decode the list of fully-coded superblocks */
1164
                if (bit == 0)
1165
                    decode_fully_flags = 1;
1166
            } else {
1167

    
1168
                /* make a note of the fact that there are partially coded
1169
                 * superblocks */
1170
                decode_partial_blocks = 1;
1171

    
1172
            }
1173
            s->superblock_coding[current_superblock++] = 
1174
                (bit) ? SB_PARTIALLY_CODED : SB_NOT_CODED;
1175
            current_run--;
1176
        }
1177

    
1178
        /* unpack the list of fully coded superblocks if any of the blocks were
1179
         * not marked as partially coded in the previous step */
1180
        if (decode_fully_flags) {
1181

    
1182
            current_superblock = 0;
1183
            current_run = 0;
1184
            bit = get_bits(gb, 1);
1185
            /* toggle the bit because as soon as the first run length is 
1186
             * fetched the bit will be toggled again */
1187
            bit ^= 1;
1188
            while (current_superblock < s->superblock_count) {
1189

    
1190
                /* skip any superblocks already marked as partially coded */
1191
                if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
1192

    
1193
                    if (current_run == 0) {
1194
                        bit ^= 1;
1195
                        current_run = get_superblock_run_length(gb);
1196
                    }
1197

    
1198
                    debug_block_coding("      setting superblock %d to %s\n",
1199
                        current_superblock,
1200
                        (bit) ? "fully coded" : "not coded");
1201
                    s->superblock_coding[current_superblock] = 
1202
                        (bit) ? SB_FULLY_CODED : SB_NOT_CODED;
1203
                    current_run--;
1204
                }
1205
                current_superblock++;
1206
            }
1207
        }
1208

    
1209
        /* if there were partial blocks, initialize bitstream for
1210
         * unpacking fragment codings */
1211
        if (decode_partial_blocks) {
1212

    
1213
            current_run = 0;
1214
            bit = get_bits(gb, 1);
1215
            /* toggle the bit because as soon as the first run length is 
1216
             * fetched the bit will be toggled again */
1217
            bit ^= 1;
1218
        }
1219
    }
1220

    
1221
    /* figure out which fragments are coded; iterate through each
1222
     * superblock (all planes) */
1223
    s->coded_fragment_list_index = 0;
1224
    s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
1225
    s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
1226
    first_c_fragment_seen = 0;
1227
    memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
1228
    for (i = 0; i < s->superblock_count; i++) {
1229

    
1230
        /* iterate through all 16 fragments in a superblock */
1231
        for (j = 0; j < 16; j++) {
1232

    
1233
            /* if the fragment is in bounds, check its coding status */
1234
            current_fragment = s->superblock_fragments[i * 16 + j];
1235
            if (current_fragment >= s->fragment_count) {
1236
                printf ("  vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
1237
                    current_fragment, s->fragment_count);
1238
                return 1;
1239
            }
1240
            if (current_fragment != -1) {
1241
                if (s->superblock_coding[i] == SB_NOT_CODED) {
1242

    
1243
                    /* copy all the fragments from the prior frame */
1244
                    s->all_fragments[current_fragment].coding_method = 
1245
                        MODE_COPY;
1246

    
1247
                } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
1248

    
1249
                    /* fragment may or may not be coded; this is the case
1250
                     * that cares about the fragment coding runs */
1251
                    if (current_run == 0) {
1252
                        bit ^= 1;
1253
                        current_run = get_fragment_run_length(gb);
1254
                    }
1255

    
1256
                    if (bit) {
1257
                        /* default mode; actual mode will be decoded in 
1258
                         * the next phase */
1259
                        s->all_fragments[current_fragment].coding_method = 
1260
                            MODE_INTER_NO_MV;
1261
                        s->coded_fragment_list[s->coded_fragment_list_index] = 
1262
                            current_fragment;
1263
                        if ((current_fragment >= s->u_fragment_start) &&
1264
                            (s->last_coded_y_fragment == -1) &&
1265
                            (!first_c_fragment_seen)) {
1266
                            s->first_coded_c_fragment = s->coded_fragment_list_index;
1267
                            s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1268
                            first_c_fragment_seen = 1;
1269
                        }
1270
                        s->coded_fragment_list_index++;
1271
                        s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1272
                        debug_block_coding("      superblock %d is partially coded, fragment %d is coded\n",
1273
                            i, current_fragment);
1274
                    } else {
1275
                        /* not coded; copy this fragment from the prior frame */
1276
                        s->all_fragments[current_fragment].coding_method =
1277
                            MODE_COPY;
1278
                        debug_block_coding("      superblock %d is partially coded, fragment %d is not coded\n",
1279
                            i, current_fragment);
1280
                    }
1281

    
1282
                    current_run--;
1283

    
1284
                } else {
1285

    
1286
                    /* fragments are fully coded in this superblock; actual
1287
                     * coding will be determined in next step */
1288
                    s->all_fragments[current_fragment].coding_method = 
1289
                        MODE_INTER_NO_MV;
1290
                    s->coded_fragment_list[s->coded_fragment_list_index] = 
1291
                        current_fragment;
1292
                    if ((current_fragment >= s->u_fragment_start) &&
1293
                        (s->last_coded_y_fragment == -1) &&
1294
                        (!first_c_fragment_seen)) {
1295
                        s->first_coded_c_fragment = s->coded_fragment_list_index;
1296
                        s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1297
                        first_c_fragment_seen = 1;
1298
                    }
1299
                    s->coded_fragment_list_index++;
1300
                    s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1301
                    debug_block_coding("      superblock %d is fully coded, fragment %d is coded\n",
1302
                        i, current_fragment);
1303
                }
1304
            }
1305
        }
1306
    }
1307

    
1308
    if (!first_c_fragment_seen)
1309
        /* only Y fragments coded in this frame */
1310
        s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
1311
    else 
1312
        /* end the list of coded fragments */
1313
        s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
1314

    
1315
    debug_block_coding("    %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
1316
        s->coded_fragment_list_index,
1317
        s->first_coded_y_fragment,
1318
        s->last_coded_y_fragment,
1319
        s->first_coded_c_fragment,
1320
        s->last_coded_c_fragment);
1321

    
1322
    return 0;
1323
}
1324

    
1325
/*
1326
 * This function unpacks all the coding mode data for individual macroblocks
1327
 * from the bitstream.
1328
 */
1329
static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
1330
{
1331
    int i, j, k;
1332
    int scheme;
1333
    int current_macroblock;
1334
    int current_fragment;
1335
    int coding_mode;
1336

    
1337
    debug_vp3("  vp3: unpacking encoding modes\n");
1338

    
1339
    if (s->keyframe) {
1340
        debug_vp3("    keyframe-- all blocks are coded as INTRA\n");
1341

    
1342
        for (i = 0; i < s->fragment_count; i++)
1343
            s->all_fragments[i].coding_method = MODE_INTRA;
1344

    
1345
    } else {
1346

    
1347
        /* fetch the mode coding scheme for this frame */
1348
        scheme = get_bits(gb, 3);
1349
        debug_modes("    using mode alphabet %d\n", scheme);
1350

    
1351
        /* is it a custom coding scheme? */
1352
        if (scheme == 0) {
1353
            debug_modes("    custom mode alphabet ahead:\n");
1354
            for (i = 0; i < 8; i++)
1355
                ModeAlphabet[scheme][get_bits(gb, 3)] = i;
1356
        }
1357

    
1358
        for (i = 0; i < 8; i++)
1359
            debug_modes("      mode[%d][%d] = %d\n", scheme, i, 
1360
                ModeAlphabet[scheme][i]);
1361

    
1362
        /* iterate through all of the macroblocks that contain 1 or more
1363
         * coded fragments */
1364
        for (i = 0; i < s->u_superblock_start; i++) {
1365

    
1366
            for (j = 0; j < 4; j++) {
1367
                current_macroblock = s->superblock_macroblocks[i * 4 + j];
1368
                if ((current_macroblock == -1) ||
1369
                    (s->macroblock_coding[current_macroblock] == MODE_COPY))
1370
                    continue;
1371
                if (current_macroblock >= s->macroblock_count) {
1372
                    printf ("  vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
1373
                        current_macroblock, s->macroblock_count);
1374
                    return 1;
1375
                }
1376

    
1377
                /* mode 7 means get 3 bits for each coding mode */
1378
                if (scheme == 7)
1379
                    coding_mode = get_bits(gb, 3);
1380
                else
1381
                    coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
1382

    
1383
                s->macroblock_coding[current_macroblock] = coding_mode;
1384
                for (k = 0; k < 6; k++) {
1385
                    current_fragment = 
1386
                        s->macroblock_fragments[current_macroblock * 6 + k];
1387
                    if (current_fragment == -1)
1388
                        continue;
1389
                    if (current_fragment >= s->fragment_count) {
1390
                        printf ("  vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
1391
                            current_fragment, s->fragment_count);
1392
                        return 1;
1393
                    }
1394
                    if (s->all_fragments[current_fragment].coding_method != 
1395
                        MODE_COPY)
1396
                        s->all_fragments[current_fragment].coding_method =
1397
                            coding_mode;
1398
                }
1399

    
1400
                debug_modes("    coding method for macroblock starting @ fragment %d = %d\n",
1401
                    s->macroblock_fragments[current_macroblock * 6], coding_mode);
1402
            }
1403
        }
1404
    }
1405

    
1406
    return 0;
1407
}
1408

    
1409
/*
1410
 * This function adjusts the components of a motion vector for the halfpel
1411
 * motion grid. c_plane indicates whether the vector applies to the U or V
1412
 * plane. The function returns the halfpel function index to be used in
1413
 * ffmpeg's put_pixels[]() array of functions.
1414
 */
1415
static inline int adjust_vector(int *x, int *y, int c_plane)
1416
{
1417
    int motion_halfpel_index = 0;
1418
    int x_halfpel;
1419
    int y_halfpel;
1420

    
1421
    if (!c_plane) {
1422

    
1423
        x_halfpel = *x & 1;
1424
        motion_halfpel_index |= x_halfpel;
1425
        if (*x >= 0)
1426
            *x >>= 1;
1427
        else
1428
            *x = -( (-(*x) >> 1) + x_halfpel);
1429

    
1430
        y_halfpel = *y & 1;
1431
        motion_halfpel_index |= (y_halfpel << 1);
1432
        if (*y >= 0)
1433
            *y >>= 1;
1434
        else
1435
            *y = -( (-(*y) >> 1) + y_halfpel);
1436

    
1437
    } else {
1438

    
1439
        x_halfpel = ((*x & 0x03) != 0);
1440
        motion_halfpel_index |= x_halfpel;
1441
        if (*x >= 0)
1442
            *x >>= 2;
1443
        else
1444
            *x = -( (-(*x) >> 2) + x_halfpel);
1445

    
1446
        y_halfpel = ((*y & 0x03) != 0);
1447
        motion_halfpel_index |= (y_halfpel << 1);
1448
        if (*y >= 0)
1449
            *y >>= 2;
1450
        else
1451
            *y = -( (-(*y) >> 2) + y_halfpel);
1452

    
1453
    }
1454

    
1455
    return motion_halfpel_index;
1456
}
1457

    
1458
/*
1459
 * This function unpacks all the motion vectors for the individual
1460
 * macroblocks from the bitstream.
1461
 */
1462
static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
1463
{
1464
    int i, j, k;
1465
    int coding_mode;
1466
    int motion_x[6];
1467
    int motion_y[6];
1468
    int last_motion_x = 0;
1469
    int last_motion_y = 0;
1470
    int prior_last_motion_x = 0;
1471
    int prior_last_motion_y = 0;
1472
    int current_macroblock;
1473
    int current_fragment;
1474

    
1475
    debug_vp3("  vp3: unpacking motion vectors\n");
1476

    
1477
    if (s->keyframe) {
1478

    
1479
        debug_vp3("    keyframe-- there are no motion vectors\n");
1480

    
1481
    } else {
1482

    
1483
        memset(motion_x, 0, 6 * sizeof(int));
1484
        memset(motion_y, 0, 6 * sizeof(int));
1485

    
1486
        /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
1487
        coding_mode = get_bits(gb, 1);
1488
        debug_vectors("    using %s scheme for unpacking motion vectors\n",
1489
            (coding_mode == 0) ? "VLC" : "fixed-length");
1490

    
1491
        /* iterate through all of the macroblocks that contain 1 or more
1492
         * coded fragments */
1493
        for (i = 0; i < s->u_superblock_start; i++) {
1494

    
1495
            for (j = 0; j < 4; j++) {
1496
                current_macroblock = s->superblock_macroblocks[i * 4 + j];
1497
                if ((current_macroblock == -1) ||
1498
                    (s->macroblock_coding[current_macroblock] == MODE_COPY))
1499
                    continue;
1500
                if (current_macroblock >= s->macroblock_count) {
1501
                    printf ("  vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
1502
                        current_macroblock, s->macroblock_count);
1503
                    return 1;
1504
                }
1505

    
1506
                current_fragment = s->macroblock_fragments[current_macroblock * 6];
1507
                if (current_fragment >= s->fragment_count) {
1508
                    printf ("  vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
1509
                        current_fragment, s->fragment_count);
1510
                    return 1;
1511
                }
1512
                switch (s->macroblock_coding[current_macroblock]) {
1513

    
1514
                case MODE_INTER_PLUS_MV:
1515
                case MODE_GOLDEN_MV:
1516
                    /* all 6 fragments use the same motion vector */
1517
                    if (coding_mode == 0) {
1518
                        motion_x[0] = get_motion_vector_vlc(gb);
1519
                        motion_y[0] = get_motion_vector_vlc(gb);
1520
                    } else {
1521
                        motion_x[0] = get_motion_vector_fixed(gb);
1522
                        motion_y[0] = get_motion_vector_fixed(gb);
1523
                    }
1524
                    for (k = 1; k < 6; k++) {
1525
                        motion_x[k] = motion_x[0];
1526
                        motion_y[k] = motion_y[0];
1527
                    }
1528

    
1529
                    /* vector maintenance, only on MODE_INTER_PLUS_MV */
1530
                    if (s->all_fragments[current_fragment].coding_method ==
1531
                        MODE_INTER_PLUS_MV) {
1532
                        prior_last_motion_x = last_motion_x;
1533
                        prior_last_motion_y = last_motion_y;
1534
                        last_motion_x = motion_x[0];
1535
                        last_motion_y = motion_y[0];
1536
                    }
1537
                    break;
1538

    
1539
                case MODE_INTER_FOURMV:
1540
                    /* fetch 4 vectors from the bitstream, one for each
1541
                     * Y fragment, then average for the C fragment vectors */
1542
                    motion_x[4] = motion_y[4] = 0;
1543
                    for (k = 0; k < 4; k++) {
1544
                        if (coding_mode == 0) {
1545
                            motion_x[k] = get_motion_vector_vlc(gb);
1546
                            motion_y[k] = get_motion_vector_vlc(gb);
1547
                        } else {
1548
                            motion_x[k] = get_motion_vector_fixed(gb);
1549
                            motion_y[k] = get_motion_vector_fixed(gb);
1550
                        }
1551
                        motion_x[4] += motion_x[k];
1552
                        motion_y[4] += motion_y[k];
1553
                    }
1554

    
1555
                    if (motion_x[4] >= 0) 
1556
                        motion_x[4] = (motion_x[4] + 2) / 4;
1557
                    else
1558
                        motion_x[4] = (motion_x[4] - 2) / 4;
1559
                    motion_x[5] = motion_x[4];
1560

    
1561
                    if (motion_y[4] >= 0) 
1562
                        motion_y[4] = (motion_y[4] + 2) / 4;
1563
                    else
1564
                        motion_y[4] = (motion_y[4] - 2) / 4;
1565
                    motion_y[5] = motion_y[4];
1566

    
1567
                    /* vector maintenance; vector[3] is treated as the
1568
                     * last vector in this case */
1569
                    prior_last_motion_x = last_motion_x;
1570
                    prior_last_motion_y = last_motion_y;
1571
                    last_motion_x = motion_x[3];
1572
                    last_motion_y = motion_y[3];
1573
                    break;
1574

    
1575
                case MODE_INTER_LAST_MV:
1576
                    /* all 6 fragments use the last motion vector */
1577
                    motion_x[0] = last_motion_x;
1578
                    motion_y[0] = last_motion_y;
1579
                    for (k = 1; k < 6; k++) {
1580
                        motion_x[k] = motion_x[0];
1581
                        motion_y[k] = motion_y[0];
1582
                    }
1583

    
1584
                    /* no vector maintenance (last vector remains the
1585
                     * last vector) */
1586
                    break;
1587

    
1588
                case MODE_INTER_PRIOR_LAST:
1589
                    /* all 6 fragments use the motion vector prior to the
1590
                     * last motion vector */
1591
                    motion_x[0] = prior_last_motion_x;
1592
                    motion_y[0] = prior_last_motion_y;
1593
                    for (k = 1; k < 6; k++) {
1594
                        motion_x[k] = motion_x[0];
1595
                        motion_y[k] = motion_y[0];
1596
                    }
1597

    
1598
                    /* vector maintenance */
1599
                    prior_last_motion_x = last_motion_x;
1600
                    prior_last_motion_y = last_motion_y;
1601
                    last_motion_x = motion_x[0];
1602
                    last_motion_y = motion_y[0];
1603
                    break;
1604

    
1605
                default:
1606
                    /* covers intra, inter without MV, golden without MV */
1607
                    memset(motion_x, 0, 6 * sizeof(int));
1608
                    memset(motion_y, 0, 6 * sizeof(int));
1609

    
1610
                    /* no vector maintenance */
1611
                    break;
1612
                }
1613

    
1614
                /* assign the motion vectors to the correct fragments */
1615
                debug_vectors("    vectors for macroblock starting @ fragment %d (coding method %d):\n",
1616
                    current_fragment,
1617
                    s->all_fragments[current_fragment].coding_method);
1618
                for (k = 0; k < 6; k++) {
1619
                    current_fragment = 
1620
                        s->macroblock_fragments[current_macroblock * 6 + k];
1621
                    if (current_fragment == -1)
1622
                        continue;
1623
                    if (current_fragment >= s->fragment_count) {
1624
                        printf ("  vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
1625
                            current_fragment, s->fragment_count);
1626
                        return 1;
1627
                    }
1628
                    s->all_fragments[current_fragment].motion_halfpel_index =
1629
                        adjust_vector(&motion_x[k], &motion_y[k],
1630
                        ((k == 4) || (k == 5)));
1631
                    s->all_fragments[current_fragment].motion_x = motion_x[k];
1632
                    s->all_fragments[current_fragment].motion_y = motion_y[k];
1633
                    debug_vectors("    vector %d: fragment %d = (%d, %d), index %d\n",
1634
                        k, current_fragment, motion_x[k], motion_y[k],
1635
                        s->all_fragments[current_fragment].motion_halfpel_index);
1636
                }
1637
            }
1638
        }
1639
    }
1640

    
1641
    return 0;
1642
}
1643

    
1644
/* 
1645
 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1646
 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1647
 * data. This function unpacks all the VLCs for either the Y plane or both
1648
 * C planes, and is called for DC coefficients or different AC coefficient
1649
 * levels (since different coefficient types require different VLC tables.
1650
 *
1651
 * This function returns a residual eob run. E.g, if a particular token gave
1652
 * instructions to EOB the next 5 fragments and there were only 2 fragments
1653
 * left in the current fragment range, 3 would be returned so that it could
1654
 * be passed into the next call to this same function.
1655
 */
1656
static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1657
                        VLC *table, int coeff_index,
1658
                        int first_fragment, int last_fragment,
1659
                        int eob_run)
1660
{
1661
    int i;
1662
    int token;
1663
    int zero_run;
1664
    DCTELEM coeff;
1665
    Vp3Fragment *fragment;
1666

    
1667
    if ((first_fragment >= s->fragment_count) ||
1668
        (last_fragment >= s->fragment_count)) {
1669

    
1670
        printf ("  vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
1671
            first_fragment, last_fragment);
1672
        return 0;
1673
    }
1674

    
1675
    for (i = first_fragment; i <= last_fragment; i++) {
1676

    
1677
        fragment = &s->all_fragments[s->coded_fragment_list[i]];
1678
        if (fragment->coeff_count > coeff_index)
1679
            continue;
1680

    
1681
        if (!eob_run) {
1682
            /* decode a VLC into a token */
1683
            token = get_vlc2(gb, table->table, 5, 3);
1684
            debug_vlc(" token = %2d, ", token);
1685
            /* use the token to get a zero run, a coefficient, and an eob run */
1686
            unpack_token(gb, token, &zero_run, &coeff, &eob_run);
1687
        }
1688

    
1689
        if (!eob_run) {
1690
            fragment->coeff_count += zero_run;
1691
            if (fragment->coeff_count < 64)
1692
                fragment->coeffs[fragment->coeff_count++] = coeff;
1693
            debug_vlc(" fragment %d coeff = %d\n",
1694
                s->coded_fragment_list[i], fragment->coeffs[coeff_index]);
1695
        } else {
1696
            fragment->last_coeff = fragment->coeff_count;
1697
            fragment->coeff_count = 64;
1698
            debug_vlc(" fragment %d eob with %d coefficients\n", 
1699
                s->coded_fragment_list[i], fragment->last_coeff);
1700
            eob_run--;
1701
        }
1702
    }
1703

    
1704
    return eob_run;
1705
}
1706

    
1707
/*
1708
 * This function unpacks all of the DCT coefficient data from the
1709
 * bitstream.
1710
 */
1711
static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1712
{
1713
    int i;
1714
    int dc_y_table;
1715
    int dc_c_table;
1716
    int ac_y_table;
1717
    int ac_c_table;
1718
    int residual_eob_run = 0;
1719

    
1720
    /* fetch the DC table indices */
1721
    dc_y_table = get_bits(gb, 4);
1722
    dc_c_table = get_bits(gb, 4);
1723

    
1724
    /* unpack the Y plane DC coefficients */
1725
    debug_vp3("  vp3: unpacking Y plane DC coefficients using table %d\n",
1726
        dc_y_table);
1727
    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0, 
1728
        s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1729

    
1730
    /* unpack the C plane DC coefficients */
1731
    debug_vp3("  vp3: unpacking C plane DC coefficients using table %d\n",
1732
        dc_c_table);
1733
    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1734
        s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1735

    
1736
    /* fetch the AC table indices */
1737
    ac_y_table = get_bits(gb, 4);
1738
    ac_c_table = get_bits(gb, 4);
1739

    
1740
    /* unpack the group 1 AC coefficients (coeffs 1-5) */
1741
    for (i = 1; i <= 5; i++) {
1742

    
1743
        debug_vp3("  vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1744
            i, ac_y_table);
1745
        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i, 
1746
            s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1747

    
1748
        debug_vp3("  vp3: unpacking level %d C plane AC coefficients using table %d\n",
1749
            i, ac_c_table);
1750
        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i, 
1751
            s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1752
    }
1753

    
1754
    /* unpack the group 2 AC coefficients (coeffs 6-14) */
1755
    for (i = 6; i <= 14; i++) {
1756

    
1757
        debug_vp3("  vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1758
            i, ac_y_table);
1759
        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i, 
1760
            s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1761

    
1762
        debug_vp3("  vp3: unpacking level %d C plane AC coefficients using table %d\n",
1763
            i, ac_c_table);
1764
        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i, 
1765
            s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1766
    }
1767

    
1768
    /* unpack the group 3 AC coefficients (coeffs 15-27) */
1769
    for (i = 15; i <= 27; i++) {
1770

    
1771
        debug_vp3("  vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1772
            i, ac_y_table);
1773
        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i, 
1774
            s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1775

    
1776
        debug_vp3("  vp3: unpacking level %d C plane AC coefficients using table %d\n",
1777
            i, ac_c_table);
1778
        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i, 
1779
            s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1780
    }
1781

    
1782
    /* unpack the group 4 AC coefficients (coeffs 28-63) */
1783
    for (i = 28; i <= 63; i++) {
1784

    
1785
        debug_vp3("  vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1786
            i, ac_y_table);
1787
        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i, 
1788
            s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1789

    
1790
        debug_vp3("  vp3: unpacking level %d C plane AC coefficients using table %d\n",
1791
            i, ac_c_table);
1792
        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i, 
1793
            s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1794
    }
1795

    
1796
    return 0;
1797
}
1798

    
1799
/*
1800
 * This function reverses the DC prediction for each coded fragment in
1801
 * the frame. Much of this function is adapted directly from the original 
1802
 * VP3 source code.
1803
 */
1804
#define COMPATIBLE_FRAME(x) \
1805
  (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1806
#define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
1807
static inline int iabs (int x) { return ((x < 0) ? -x : x); }
1808

    
1809
static void reverse_dc_prediction(Vp3DecodeContext *s,
1810
                                  int first_fragment,
1811
                                  int fragment_width,
1812
                                  int fragment_height) 
1813
{
1814

    
1815
#define PUL 8
1816
#define PU 4
1817
#define PUR 2
1818
#define PL 1
1819

    
1820
    int x, y;
1821
    int i = first_fragment;
1822

    
1823
    /*
1824
     * Fragment prediction groups:
1825
     *
1826
     * 32222222226
1827
     * 10000000004
1828
     * 10000000004
1829
     * 10000000004
1830
     * 10000000004
1831
     *
1832
     * Note: Groups 5 and 7 do not exist as it would mean that the 
1833
     * fragment's x coordinate is both 0 and (width - 1) at the same time.
1834
     */
1835
    int predictor_group;
1836
    short predicted_dc;
1837

    
1838
    /* validity flags for the left, up-left, up, and up-right fragments */
1839
    int fl, ful, fu, fur;
1840

    
1841
    /* DC values for the left, up-left, up, and up-right fragments */
1842
    int vl, vul, vu, vur;
1843

    
1844
    /* indices for the left, up-left, up, and up-right fragments */
1845
    int l, ul, u, ur;
1846

    
1847
    /* 
1848
     * The 6 fields mean:
1849
     *   0: up-left multiplier
1850
     *   1: up multiplier
1851
     *   2: up-right multiplier
1852
     *   3: left multiplier
1853
     *   4: mask
1854
     *   5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
1855
     */
1856
    int predictor_transform[16][6] = {
1857
        {  0,  0,  0,  0,   0,  0 },
1858
        {  0,  0,  0,  1,   0,  0 },        // PL
1859
        {  0,  0,  1,  0,   0,  0 },        // PUR
1860
        {  0,  0, 53, 75, 127,  7 },        // PUR|PL
1861
        {  0,  1,  0,  0,   0,  0 },        // PU
1862
        {  0,  1,  0,  1,   1,  1 },        // PU|PL
1863
        {  0,  1,  0,  0,   0,  0 },        // PU|PUR
1864
        {  0,  0, 53, 75, 127,  7 },        // PU|PUR|PL
1865
        {  1,  0,  0,  0,   0,  0 },        // PUL
1866
        {  0,  0,  0,  1,   0,  0 },        // PUL|PL
1867
        {  1,  0,  1,  0,   1,  1 },        // PUL|PUR
1868
        {  0,  0, 53, 75, 127,  7 },        // PUL|PUR|PL
1869
        {  0,  1,  0,  0,   0,  0 },        // PUL|PU
1870
        {-26, 29,  0, 29,  31,  5 },        // PUL|PU|PL
1871
        {  3, 10,  3,  0,  15,  4 },        // PUL|PU|PUR
1872
        {-26, 29,  0, 29,  31,  5 }         // PUL|PU|PUR|PL
1873
    };
1874

    
1875
    /* This table shows which types of blocks can use other blocks for
1876
     * prediction. For example, INTRA is the only mode in this table to
1877
     * have a frame number of 0. That means INTRA blocks can only predict
1878
     * from other INTRA blocks. There are 2 golden frame coding types; 
1879
     * blocks encoding in these modes can only predict from other blocks
1880
     * that were encoded with these 1 of these 2 modes. */
1881
    unsigned char compatible_frame[8] = {
1882
        1,    /* MODE_INTER_NO_MV */
1883
        0,    /* MODE_INTRA */
1884
        1,    /* MODE_INTER_PLUS_MV */
1885
        1,    /* MODE_INTER_LAST_MV */
1886
        1,    /* MODE_INTER_PRIOR_MV */
1887
        2,    /* MODE_USING_GOLDEN */
1888
        2,    /* MODE_GOLDEN_MV */
1889
        1     /* MODE_INTER_FOUR_MV */
1890
    };
1891
    int current_frame_type;
1892

    
1893
    /* there is a last DC predictor for each of the 3 frame types */
1894
    short last_dc[3];
1895

    
1896
    int transform = 0;
1897

    
1898
    debug_vp3("  vp3: reversing DC prediction\n");
1899

    
1900
    vul = vu = vur = vl = 0;
1901
    last_dc[0] = last_dc[1] = last_dc[2] = 0;
1902

    
1903
    /* for each fragment row... */
1904
    for (y = 0; y < fragment_height; y++) {
1905

    
1906
        /* for each fragment in a row... */
1907
        for (x = 0; x < fragment_width; x++, i++) {
1908

    
1909
            /* reverse prediction if this block was coded */
1910
            if (s->all_fragments[i].coding_method != MODE_COPY) {
1911

    
1912
                current_frame_type = 
1913
                    compatible_frame[s->all_fragments[i].coding_method];
1914
                predictor_group = (x == 0) + ((y == 0) << 1) +
1915
                    ((x + 1 == fragment_width) << 2);
1916
                debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
1917
                    i, predictor_group, s->all_fragments[i].coeffs[0]);
1918

    
1919
                switch (predictor_group) {
1920

    
1921
                case 0:
1922
                    /* main body of fragments; consider all 4 possible
1923
                     * fragments for prediction */
1924

    
1925
                    /* calculate the indices of the predicting fragments */
1926
                    ul = i - fragment_width - 1;
1927
                    u = i - fragment_width;
1928
                    ur = i - fragment_width + 1;
1929
                    l = i - 1;
1930

    
1931
                    /* fetch the DC values for the predicting fragments */
1932
                    vul = s->all_fragments[ul].coeffs[0];
1933
                    vu = s->all_fragments[u].coeffs[0];
1934
                    vur = s->all_fragments[ur].coeffs[0];
1935
                    vl = s->all_fragments[l].coeffs[0];
1936

    
1937
                    /* figure out which fragments are valid */
1938
                    ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
1939
                    fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1940
                    fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1941
                    fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1942

    
1943
                    /* decide which predictor transform to use */
1944
                    transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
1945

    
1946
                    break;
1947

    
1948
                case 1:
1949
                    /* left column of fragments, not including top corner;
1950
                     * only consider up and up-right fragments */
1951

    
1952
                    /* calculate the indices of the predicting fragments */
1953
                    u = i - fragment_width;
1954
                    ur = i - fragment_width + 1;
1955

    
1956
                    /* fetch the DC values for the predicting fragments */
1957
                    vu = s->all_fragments[u].coeffs[0];
1958
                    vur = s->all_fragments[ur].coeffs[0];
1959

    
1960
                    /* figure out which fragments are valid */
1961
                    fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1962
                    fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1963

    
1964
                    /* decide which predictor transform to use */
1965
                    transform = (fu*PU) | (fur*PUR);
1966

    
1967
                    break;
1968

    
1969
                case 2:
1970
                case 6:
1971
                    /* top row of fragments, not including top-left frag;
1972
                     * only consider the left fragment for prediction */
1973

    
1974
                    /* calculate the indices of the predicting fragments */
1975
                    l = i - 1;
1976

    
1977
                    /* fetch the DC values for the predicting fragments */
1978
                    vl = s->all_fragments[l].coeffs[0];
1979

    
1980
                    /* figure out which fragments are valid */
1981
                    fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1982

    
1983
                    /* decide which predictor transform to use */
1984
                    transform = (fl*PL);
1985

    
1986
                    break;
1987

    
1988
                case 3:
1989
                    /* top-left fragment */
1990

    
1991
                    /* nothing to predict from in this case */
1992
                    transform = 0;
1993

    
1994
                    break;
1995

    
1996
                case 4:
1997
                    /* right column of fragments, not including top corner;
1998
                     * consider up-left, up, and left fragments for
1999
                     * prediction */
2000

    
2001
                    /* calculate the indices of the predicting fragments */
2002
                    ul = i - fragment_width - 1;
2003
                    u = i - fragment_width;
2004
                    l = i - 1;
2005

    
2006
                    /* fetch the DC values for the predicting fragments */
2007
                    vul = s->all_fragments[ul].coeffs[0];
2008
                    vu = s->all_fragments[u].coeffs[0];
2009
                    vl = s->all_fragments[l].coeffs[0];
2010

    
2011
                    /* figure out which fragments are valid */
2012
                    ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
2013
                    fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
2014
                    fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
2015

    
2016
                    /* decide which predictor transform to use */
2017
                    transform = (fl*PL) | (fu*PU) | (ful*PUL);
2018

    
2019
                    break;
2020

    
2021
                }
2022

    
2023
                debug_dc_pred("transform = %d, ", transform);
2024

    
2025
                if (transform == 0) {
2026

    
2027
                    /* if there were no fragments to predict from, use last
2028
                     * DC saved */
2029
                    s->all_fragments[i].coeffs[0] += last_dc[current_frame_type];
2030
                    debug_dc_pred("from last DC (%d) = %d\n", 
2031
                        current_frame_type, s->all_fragments[i].coeffs[0]);
2032

    
2033
                } else {
2034

    
2035
                    /* apply the appropriate predictor transform */
2036
                    predicted_dc =
2037
                        (predictor_transform[transform][0] * vul) +
2038
                        (predictor_transform[transform][1] * vu) +
2039
                        (predictor_transform[transform][2] * vur) +
2040
                        (predictor_transform[transform][3] * vl);
2041

    
2042
                    /* if there is a shift value in the transform, add
2043
                     * the sign bit before the shift */
2044
                    if (predictor_transform[transform][5] != 0) {
2045
                        predicted_dc += ((predicted_dc >> 15) & 
2046
                            predictor_transform[transform][4]);
2047
                        predicted_dc >>= predictor_transform[transform][5];
2048
                    }
2049

    
2050
                    /* check for outranging on the [ul u l] and
2051
                     * [ul u ur l] predictors */
2052
                    if ((transform == 13) || (transform == 15)) {
2053
                        if (iabs(predicted_dc - vu) > 128)
2054
                            predicted_dc = vu;
2055
                        else if (iabs(predicted_dc - vl) > 128)
2056
                            predicted_dc = vl;
2057
                        else if (iabs(predicted_dc - vul) > 128)
2058
                            predicted_dc = vul;
2059
                    }
2060

    
2061
                    /* at long last, apply the predictor */
2062
                    s->all_fragments[i].coeffs[0] += predicted_dc;
2063
                    debug_dc_pred("from pred DC = %d\n", 
2064
                    s->all_fragments[i].coeffs[0]);
2065
                }
2066

    
2067
                /* save the DC */
2068
                last_dc[current_frame_type] = s->all_fragments[i].coeffs[0];
2069
            }
2070
        }
2071
    }
2072
}
2073

    
2074
/*
2075
 * This function performs the final rendering of each fragment's data
2076
 * onto the output frame.
2077
 */
2078
static void render_fragments(Vp3DecodeContext *s,
2079
                             int first_fragment,
2080
                             int width,
2081
                             int height,
2082
                             int plane /* 0 = Y, 1 = U, 2 = V */) 
2083
{
2084
    int x, y;
2085
    int m, n;
2086
    int i = first_fragment;
2087
    int j;
2088
    int16_t *dequantizer;
2089
    DCTELEM dequant_block[64];
2090
    DCTELEM dequant_block_permuted[64];
2091
    unsigned char *output_plane;
2092
    unsigned char *last_plane;
2093
    unsigned char *golden_plane;
2094
    int stride;
2095
    int motion_x, motion_y;
2096
    int upper_motion_limit, lower_motion_limit;
2097
    int motion_halfpel_index;
2098
    unsigned int motion_source;
2099

    
2100
    debug_vp3("  vp3: rendering final fragments for %s\n",
2101
        (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
2102

    
2103
    /* set up plane-specific parameters */
2104
    if (plane == 0) {
2105
        dequantizer = s->intra_y_dequant;
2106
        output_plane = s->current_frame.data[0];
2107
        last_plane = s->last_frame.data[0];
2108
        golden_plane = s->golden_frame.data[0];
2109
        stride = -s->current_frame.linesize[0];
2110
        upper_motion_limit = 7 * s->current_frame.linesize[0];
2111
        lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
2112
    } else if (plane == 1) {
2113
        dequantizer = s->intra_c_dequant;
2114
        output_plane = s->current_frame.data[1];
2115
        last_plane = s->last_frame.data[1];
2116
        golden_plane = s->golden_frame.data[1];
2117
        stride = -s->current_frame.linesize[1];
2118
        upper_motion_limit = 7 * s->current_frame.linesize[1];
2119
        lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
2120
    } else {
2121
        dequantizer = s->intra_c_dequant;
2122
        output_plane = s->current_frame.data[2];
2123
        last_plane = s->last_frame.data[2];
2124
        golden_plane = s->golden_frame.data[2];
2125
        stride = -s->current_frame.linesize[2];
2126
        upper_motion_limit = 7 * s->current_frame.linesize[2];
2127
        lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
2128
    }
2129

    
2130
    /* for each fragment row... */
2131
    for (y = 0; y < height; y += 8) {
2132

    
2133
        /* for each fragment in a row... */
2134
        for (x = 0; x < width; x += 8, i++) {
2135

    
2136
            if ((i < 0) || (i >= s->fragment_count)) {
2137
                printf ("  vp3:render_fragments(): bad fragment number (%d)\n", i);
2138
                return;
2139
            }
2140

    
2141
            /* transform if this block was coded */
2142
            if (s->all_fragments[i].coding_method != MODE_COPY) {
2143

    
2144
                /* sort out the motion vector */
2145
                motion_x = s->all_fragments[i].motion_x;
2146
                motion_y = s->all_fragments[i].motion_y;
2147
                motion_halfpel_index = s->all_fragments[i].motion_halfpel_index;
2148

    
2149
                motion_source = s->all_fragments[i].first_pixel;
2150
                motion_source += motion_x;
2151
                motion_source += (motion_y * stride);
2152

    
2153
                /* if the are any problems with a motion vector, refuse
2154
                 * to render the block */
2155
                if ((motion_source < upper_motion_limit) ||
2156
                    (motion_source > lower_motion_limit)) {
2157
//                    printf ("  vp3: help! motion source (%d) out of range (%d..%d)\n",
2158
//                        motion_source, upper_motion_limit, lower_motion_limit);
2159
                    continue;
2160
                }
2161

    
2162
                /* first, take care of copying a block from either the
2163
                 * previous or the golden frame */
2164
                if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2165
                    (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) {
2166

    
2167
                    s->dsp.put_pixels_tab[1][motion_halfpel_index](
2168
                        output_plane + s->all_fragments[i].first_pixel,
2169
                        golden_plane + motion_source,
2170
                        stride, 8);
2171

    
2172
                } else 
2173
                if (s->all_fragments[i].coding_method != MODE_INTRA) {
2174

    
2175
                    s->dsp.put_pixels_tab[1][motion_halfpel_index](
2176
                        output_plane + s->all_fragments[i].first_pixel,
2177
                        last_plane + motion_source,
2178
                        stride, 8);
2179
                }
2180

    
2181
                /* dequantize the DCT coefficients */
2182
                debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n", 
2183
                    i, s->all_fragments[i].coding_method, 
2184
                    s->all_fragments[i].coeffs[0], dequantizer[0]);
2185
                for (j = 0; j < 64; j++)
2186
                    dequant_block[dezigzag_index[j]] =
2187
                        s->all_fragments[i].coeffs[j] *
2188
                        dequantizer[j];
2189
                for (j = 0; j < 64; j++)
2190
                    dequant_block_permuted[s->dsp.idct_permutation[j]] =
2191
                        dequant_block[j];
2192

    
2193
                debug_idct("dequantized block:\n");
2194
                for (m = 0; m < 8; m++) {
2195
                    for (n = 0; n < 8; n++) {
2196
                        debug_idct(" %5d", dequant_block[m * 8 + n]);
2197
                    }
2198
                    debug_idct("\n");
2199
                }
2200
                debug_idct("\n");
2201

    
2202
                /* invert DCT and place (or add) in final output */
2203

    
2204
                if (s->all_fragments[i].coding_method == MODE_INTRA) {
2205
                    dequant_block_permuted[0] += 1024;
2206
                    s->dsp.idct_put(
2207
                        output_plane + s->all_fragments[i].first_pixel,
2208
                        stride, dequant_block_permuted);
2209
                } else {
2210
                    s->dsp.idct_add(
2211
                        output_plane + s->all_fragments[i].first_pixel,
2212
                        stride, dequant_block_permuted);
2213
                }
2214

    
2215
                debug_idct("block after idct_%s():\n",
2216
                    (s->all_fragments[i].coding_method == MODE_INTRA)?
2217
                    "put" : "add");
2218
                for (m = 0; m < 8; m++) {
2219
                    for (n = 0; n < 8; n++) {
2220
                        debug_idct(" %3d", *(output_plane + 
2221
                            s->all_fragments[i].first_pixel + (m * stride + n)));
2222
                    }
2223
                    debug_idct("\n");
2224
                }
2225
                debug_idct("\n");
2226

    
2227
            } else {
2228

    
2229
                /* copy directly from the previous frame */
2230
                s->dsp.put_pixels_tab[1][0](
2231
                    output_plane + s->all_fragments[i].first_pixel,
2232
                    last_plane + s->all_fragments[i].first_pixel,
2233
                    stride, 8);
2234

    
2235
            }
2236
        }
2237
    }
2238

    
2239
    emms_c();
2240

    
2241
}
2242

    
2243
/* 
2244
 * This function computes the first pixel addresses for each fragment.
2245
 * This function needs to be invoked after the first frame is allocated
2246
 * so that it has access to the plane strides.
2247
 */
2248
static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s) 
2249
{
2250

    
2251
    int i, x, y;
2252

    
2253
    /* figure out the first pixel addresses for each of the fragments */
2254
    /* Y plane */
2255
    i = 0;
2256
    for (y = s->fragment_height; y > 0; y--) {
2257
        for (x = 0; x < s->fragment_width; x++) {
2258
            s->all_fragments[i++].first_pixel = 
2259
                s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2260
                    s->golden_frame.linesize[0] +
2261
                    x * FRAGMENT_PIXELS;
2262
            debug_init("  fragment %d, first pixel @ %d\n", 
2263
                i-1, s->all_fragments[i-1].first_pixel);
2264
        }
2265
    }
2266

    
2267
    /* U plane */
2268
    i = s->u_fragment_start;
2269
    for (y = s->fragment_height / 2; y > 0; y--) {
2270
        for (x = 0; x < s->fragment_width / 2; x++) {
2271
            s->all_fragments[i++].first_pixel = 
2272
                s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2273
                    s->golden_frame.linesize[1] +
2274
                    x * FRAGMENT_PIXELS;
2275
            debug_init("  fragment %d, first pixel @ %d\n", 
2276
                i-1, s->all_fragments[i-1].first_pixel);
2277
        }
2278
    }
2279

    
2280
    /* V plane */
2281
    i = s->v_fragment_start;
2282
    for (y = s->fragment_height / 2; y > 0; y--) {
2283
        for (x = 0; x < s->fragment_width / 2; x++) {
2284
            s->all_fragments[i++].first_pixel = 
2285
                s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2286
                    s->golden_frame.linesize[2] +
2287
                    x * FRAGMENT_PIXELS;
2288
            debug_init("  fragment %d, first pixel @ %d\n", 
2289
                i-1, s->all_fragments[i-1].first_pixel);
2290
        }
2291
    }
2292
}
2293

    
2294
/*
2295
 * This is the ffmpeg/libavcodec API init function.
2296
 */
2297
static int vp3_decode_init(AVCodecContext *avctx)
2298
{
2299
    Vp3DecodeContext *s = avctx->priv_data;
2300
    int i;
2301
    int c_width;
2302
    int c_height;
2303
    int y_superblock_count;
2304
    int c_superblock_count;
2305

    
2306
    s->avctx = avctx;
2307
    s->width = avctx->width;
2308
    s->height = avctx->height;
2309
    avctx->pix_fmt = PIX_FMT_YUV420P;
2310
    avctx->has_b_frames = 0;
2311
    dsputil_init(&s->dsp, avctx);
2312

    
2313
    /* initialize to an impossible value which will force a recalculation
2314
     * in the first frame decode */
2315
    s->quality_index = -1;
2316

    
2317
    s->y_superblock_width = (s->width + 31) / 32;
2318
    s->y_superblock_height = (s->height + 31) / 32;
2319
    y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2320

    
2321
    /* work out the dimensions for the C planes */
2322
    c_width = s->width / 2;
2323
    c_height = s->height / 2;
2324
    s->c_superblock_width = (c_width + 31) / 32;
2325
    s->c_superblock_height = (c_height + 31) / 32;
2326
    c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2327

    
2328
    s->superblock_count = y_superblock_count + (c_superblock_count * 2);
2329
    s->u_superblock_start = y_superblock_count;
2330
    s->v_superblock_start = s->u_superblock_start + c_superblock_count;
2331
    s->superblock_coding = av_malloc(s->superblock_count);
2332

    
2333
    s->macroblock_width = (s->width + 15) / 16;
2334
    s->macroblock_height = (s->height + 15) / 16;
2335
    s->macroblock_count = s->macroblock_width * s->macroblock_height;
2336

    
2337
    s->fragment_width = s->width / FRAGMENT_PIXELS;
2338
    s->fragment_height = s->height / FRAGMENT_PIXELS;
2339

    
2340
    /* fragment count covers all 8x8 blocks for all 3 planes */
2341
    s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
2342
    s->u_fragment_start = s->fragment_width * s->fragment_height;
2343
    s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
2344

    
2345
    debug_init("  Y plane: %d x %d\n", s->width, s->height);
2346
    debug_init("  C plane: %d x %d\n", c_width, c_height);
2347
    debug_init("  Y superblocks: %d x %d, %d total\n",
2348
        s->y_superblock_width, s->y_superblock_height, y_superblock_count);
2349
    debug_init("  C superblocks: %d x %d, %d total\n",
2350
        s->c_superblock_width, s->c_superblock_height, c_superblock_count);
2351
    debug_init("  total superblocks = %d, U starts @ %d, V starts @ %d\n", 
2352
        s->superblock_count, s->u_superblock_start, s->v_superblock_start);
2353
    debug_init("  macroblocks: %d x %d, %d total\n",
2354
        s->macroblock_width, s->macroblock_height, s->macroblock_count);
2355
    debug_init("  %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
2356
        s->fragment_count,
2357
        s->fragment_width,
2358
        s->fragment_height,
2359
        s->u_fragment_start,
2360
        s->v_fragment_start);
2361

    
2362
    s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
2363
    s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
2364
    s->pixel_addresses_inited = 0;
2365

    
2366
    /* init VLC tables */
2367
    for (i = 0; i < 16; i++) {
2368

    
2369
        /* DC histograms */
2370
        init_vlc(&s->dc_vlc[i], 5, 32,
2371
            &dc_bias[i][0][1], 4, 2,
2372
            &dc_bias[i][0][0], 4, 2);
2373

    
2374
        /* group 1 AC histograms */
2375
        init_vlc(&s->ac_vlc_1[i], 5, 32,
2376
            &ac_bias_0[i][0][1], 4, 2,
2377
            &ac_bias_0[i][0][0], 4, 2);
2378

    
2379
        /* group 2 AC histograms */
2380
        init_vlc(&s->ac_vlc_2[i], 5, 32,
2381
            &ac_bias_1[i][0][1], 4, 2,
2382
            &ac_bias_1[i][0][0], 4, 2);
2383

    
2384
        /* group 3 AC histograms */
2385
        init_vlc(&s->ac_vlc_3[i], 5, 32,
2386
            &ac_bias_2[i][0][1], 4, 2,
2387
            &ac_bias_2[i][0][0], 4, 2);
2388

    
2389
        /* group 4 AC histograms */
2390
        init_vlc(&s->ac_vlc_4[i], 5, 32,
2391
            &ac_bias_3[i][0][1], 4, 2,
2392
            &ac_bias_3[i][0][0], 4, 2);
2393
    }
2394

    
2395
    /* build quantization zigzag table */
2396
    for (i = 0; i < 64; i++)
2397
        zigzag_index[dezigzag_index[i]] = i;
2398

    
2399
    /* work out the block mapping tables */
2400
    s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
2401
    s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
2402
    s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
2403
    s->macroblock_coding = av_malloc(s->macroblock_count + 1);
2404
    init_block_mapping(s);
2405

    
2406
    for (i = 0; i < 3; i++) {
2407
        s->current_frame.data[i] = NULL;
2408
        s->last_frame.data[i] = NULL;
2409
        s->golden_frame.data[i] = NULL;
2410
    }
2411

    
2412
    return 0;
2413
}
2414

    
2415
/*
2416
 * This is the ffmpeg/libavcodec API frame decode function.
2417
 */
2418
static int vp3_decode_frame(AVCodecContext *avctx, 
2419
                            void *data, int *data_size,
2420
                            uint8_t *buf, int buf_size)
2421
{
2422
    Vp3DecodeContext *s = avctx->priv_data;
2423
    GetBitContext gb;
2424
    static int counter = 0;
2425

    
2426
    *data_size = 0;
2427

    
2428
    init_get_bits(&gb, buf, buf_size * 8);
2429

    
2430
    s->keyframe = get_bits(&gb, 1);
2431
    s->keyframe ^= 1;
2432
    skip_bits(&gb, 1);
2433
    s->last_quality_index = s->quality_index;
2434
    s->quality_index = get_bits(&gb, 6);
2435
    if (s->quality_index != s->last_quality_index)
2436
        init_dequantizer(s);
2437

    
2438
    debug_vp3(" VP3 frame #%d: Q index = %d", counter, s->quality_index);
2439
    counter++;
2440

    
2441
    if (s->keyframe) {
2442

    
2443
        debug_vp3(", keyframe\n");
2444
        /* skip the other 2 header bytes for now */
2445
        skip_bits(&gb, 16);
2446

    
2447
        if (s->last_frame.data[0] == s->golden_frame.data[0]) {
2448
            if (s->golden_frame.data[0])
2449
                avctx->release_buffer(avctx, &s->golden_frame);
2450
        } else {
2451
            if (s->golden_frame.data[0])
2452
                avctx->release_buffer(avctx, &s->golden_frame);
2453
            if (s->last_frame.data[0])
2454
                avctx->release_buffer(avctx, &s->last_frame);
2455
        }
2456

    
2457
        s->golden_frame.reference = 0;
2458
        if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
2459
            printf("vp3: get_buffer() failed\n");
2460
            return -1;
2461
        }
2462

    
2463
        /* golden frame is also the current frame */
2464
        memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
2465

    
2466
        /* time to figure out pixel addresses? */
2467
        if (!s->pixel_addresses_inited)
2468
            vp3_calculate_pixel_addresses(s);
2469

    
2470
    } else {
2471

    
2472
        debug_vp3("\n");
2473

    
2474
        /* allocate a new current frame */
2475
        s->current_frame.reference = 0;
2476
        if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
2477
            printf("vp3: get_buffer() failed\n");
2478
            return -1;
2479
        }
2480
    }
2481

    
2482
    init_frame(s, &gb);
2483

    
2484
#if KEYFRAMES_ONLY
2485
if (!s->keyframe) {
2486

    
2487
    memcpy(s->current_frame.data[0], s->golden_frame.data[0],
2488
        s->current_frame.linesize[0] * s->height);
2489
    memcpy(s->current_frame.data[1], s->golden_frame.data[1],
2490
        s->current_frame.linesize[1] * s->height / 2);
2491
    memcpy(s->current_frame.data[2], s->golden_frame.data[2],
2492
        s->current_frame.linesize[2] * s->height / 2);
2493

    
2494
} else {
2495
#endif
2496

    
2497
    if (unpack_superblocks(s, &gb) ||
2498
        unpack_modes(s, &gb) ||
2499
        unpack_vectors(s, &gb) ||
2500
        unpack_dct_coeffs(s, &gb)) {
2501

    
2502
        printf("  vp3: could not decode frame\n");
2503
        return -1;
2504
    }
2505

    
2506
    reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
2507
    reverse_dc_prediction(s, s->u_fragment_start,
2508
        s->fragment_width / 2, s->fragment_height / 2);
2509
    reverse_dc_prediction(s, s->v_fragment_start,
2510
        s->fragment_width / 2, s->fragment_height / 2);
2511

    
2512
    render_fragments(s, 0, s->width, s->height, 0);
2513
    render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
2514
    render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
2515

    
2516
#if KEYFRAMES_ONLY
2517
}
2518
#endif
2519

    
2520
    *data_size=sizeof(AVFrame);
2521
    *(AVFrame*)data= s->current_frame;
2522

    
2523
    /* release the last frame, if it is allocated and if it is not the
2524
     * golden frame */
2525
    if ((s->last_frame.data[0]) &&
2526
        (s->last_frame.data[0] != s->golden_frame.data[0]))
2527
        avctx->release_buffer(avctx, &s->last_frame);
2528

    
2529
    /* shuffle frames (last = current) */
2530
    memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
2531

    
2532
    return buf_size;
2533
}
2534

    
2535
/*
2536
 * This is the ffmpeg/libavcodec API module cleanup function.
2537
 */
2538
static int vp3_decode_end(AVCodecContext *avctx)
2539
{
2540
    Vp3DecodeContext *s = avctx->priv_data;
2541

    
2542
    av_free(s->all_fragments);
2543
    av_free(s->coded_fragment_list);
2544
    av_free(s->superblock_fragments);
2545
    av_free(s->superblock_macroblocks);
2546
    av_free(s->macroblock_fragments);
2547
    av_free(s->macroblock_coding);
2548

    
2549
    /* release all frames */
2550
    if (s->golden_frame.data[0])
2551
        avctx->release_buffer(avctx, &s->golden_frame);
2552
    if (s->last_frame.data[0])
2553
        avctx->release_buffer(avctx, &s->last_frame);
2554
    /* no need to release the current_frame since it will always be pointing
2555
     * to the same frame as either the golden or last frame */
2556

    
2557
    return 0;
2558
}
2559

    
2560
AVCodec vp3_decoder = {
2561
    "vp3",
2562
    CODEC_TYPE_VIDEO,
2563
    CODEC_ID_VP3,
2564
    sizeof(Vp3DecodeContext),
2565
    vp3_decode_init,
2566
    NULL,
2567
    vp3_decode_end,
2568
    vp3_decode_frame,
2569
    0,
2570
    NULL
2571
};