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