3 * Copyright (C) 2003 the ffmpeg project
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.
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.
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
19 * VP3 Video Decoder by Mike Melanson (melanson@pcisys.net)
25 * On2 VP3 Video Decoder
36 #include "mpegvideo.h"
41 #define FRAGMENT_PIXELS 8
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.
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
62 #define KEYFRAMES_ONLY 0
66 #define DEBUG_DEQUANTIZERS 0
67 #define DEBUG_BLOCK_CODING 0
69 #define DEBUG_VECTORS 0
72 #define DEBUG_DC_PRED 0
76 #define debug_vp3 printf
78 static inline void debug_vp3(const char *format, ...) { }
82 #define debug_init printf
84 static inline void debug_init(const char *format, ...) { }
87 #if DEBUG_DEQUANTIZERS
88 #define debug_dequantizers printf
90 static inline void debug_dequantizers(const char *format, ...) { }
93 #if DEBUG_BLOCK_CODING
94 #define debug_block_coding printf
96 static inline void debug_block_coding(const char *format, ...) { }
100 #define debug_modes printf
102 static inline void debug_modes(const char *format, ...) { }
106 #define debug_vectors printf
108 static inline void debug_vectors(const char *format, ...) { }
112 #define debug_token printf
114 static inline void debug_token(const char *format, ...) { }
118 #define debug_vlc printf
120 static inline void debug_vlc(const char *format, ...) { }
124 #define debug_dc_pred printf
126 static inline void debug_dc_pred(const char *format, ...) { }
130 #define debug_idct printf
132 static inline void debug_idct(const char *format, ...) { }
135 typedef struct Vp3Fragment {
142 /* address of first pixel taking into account which plane the fragment
143 * lives on as well as the plane stride */
145 /* this is the macroblock that the fragment belongs to */
149 #define SB_NOT_CODED 0
150 #define SB_PARTIALLY_CODED 1
151 #define SB_FULLY_CODED 2
153 #define MODE_INTER_NO_MV 0
155 #define MODE_INTER_PLUS_MV 2
156 #define MODE_INTER_LAST_MV 3
157 #define MODE_INTER_PRIOR_LAST 4
158 #define MODE_USING_GOLDEN 5
159 #define MODE_GOLDEN_MV 6
160 #define MODE_INTER_FOURMV 7
161 #define CODING_MODE_COUNT 8
163 /* special internal mode */
166 /* There are 6 preset schemes, plus a free-form scheme */
167 static int ModeAlphabet[7][CODING_MODE_COUNT] =
169 /* this is the custom scheme */
170 { 0, 0, 0, 0, 0, 0, 0, 0 },
172 /* scheme 1: Last motion vector dominates */
173 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
174 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
175 MODE_INTRA, MODE_USING_GOLDEN,
176 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
179 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
180 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
181 MODE_INTRA, MODE_USING_GOLDEN,
182 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
185 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
186 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
187 MODE_INTRA, MODE_USING_GOLDEN,
188 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
191 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
192 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
193 MODE_INTRA, MODE_USING_GOLDEN,
194 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
196 /* scheme 5: No motion vector dominates */
197 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
198 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
199 MODE_INTRA, MODE_USING_GOLDEN,
200 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
203 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
204 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
205 MODE_INTER_PLUS_MV, MODE_INTRA,
206 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
210 #define MIN_DEQUANT_VAL 2
212 typedef struct Vp3DecodeContext {
213 AVCodecContext *avctx;
215 AVFrame golden_frame;
217 AVFrame current_frame;
222 int last_quality_index;
224 int superblock_count;
225 int superblock_width;
226 int superblock_height;
227 int y_superblock_width;
228 int y_superblock_height;
229 int c_superblock_width;
230 int c_superblock_height;
231 int u_superblock_start;
232 int v_superblock_start;
233 unsigned char *superblock_coding;
235 int macroblock_count;
236 int macroblock_width;
237 int macroblock_height;
243 Vp3Fragment *all_fragments;
244 int u_fragment_start;
245 int v_fragment_start;
247 /* this is a list of indices into the all_fragments array indicating
248 * which of the fragments are coded */
249 int *coded_fragment_list;
250 int coded_fragment_list_index;
251 int pixel_addresses_inited;
259 int16_t intra_y_dequant[64];
260 int16_t intra_c_dequant[64];
261 int16_t inter_dequant[64];
263 /* This table contains superblock_count * 16 entries. Each set of 16
264 * numbers corresponds to the fragment indices 0..15 of the superblock.
265 * An entry will be -1 to indicate that no entry corresponds to that
267 int *superblock_fragments;
269 /* This table contains superblock_count * 4 entries. Each set of 4
270 * numbers corresponds to the macroblock indices 0..3 of the superblock.
271 * An entry will be -1 to indicate that no entry corresponds to that
273 int *superblock_macroblocks;
275 /* This table contains macroblock_count * 6 entries. Each set of 6
276 * numbers corresponds to the fragment indices 0..5 which comprise
277 * the macroblock (4 Y fragments and 2 C fragments). */
278 int *macroblock_fragments;
279 /* This is an array that indicates how a particular macroblock
281 unsigned char *macroblock_coding;
283 int first_coded_y_fragment;
284 int first_coded_c_fragment;
285 int last_coded_y_fragment;
286 int last_coded_c_fragment;
290 /************************************************************************
291 * VP3 specific functions
292 ************************************************************************/
295 * This function sets up all of the various blocks mappings:
296 * superblocks <-> fragments, macroblocks <-> fragments,
297 * superblocks <-> macroblocks
299 * Returns 0 is successful; returns 1 if *anything* went wrong.
301 static int init_block_mapping(Vp3DecodeContext *s)
304 signed int hilbert_walk_y[16];
305 signed int hilbert_walk_c[16];
306 signed int hilbert_walk_mb[4];
308 int current_fragment = 0;
309 int current_width = 0;
310 int current_height = 0;
313 int superblock_row_inc = 0;
315 int mapping_index = 0;
317 int current_macroblock;
320 signed char travel_width[16] = {
327 signed char travel_height[16] = {
334 signed char travel_width_mb[4] = {
338 signed char travel_height_mb[4] = {
342 debug_vp3(" vp3: initialize block mapping tables\n");
344 /* figure out hilbert pattern per these frame dimensions */
345 hilbert_walk_y[0] = 1;
346 hilbert_walk_y[1] = 1;
347 hilbert_walk_y[2] = s->fragment_width;
348 hilbert_walk_y[3] = -1;
349 hilbert_walk_y[4] = s->fragment_width;
350 hilbert_walk_y[5] = s->fragment_width;
351 hilbert_walk_y[6] = 1;
352 hilbert_walk_y[7] = -s->fragment_width;
353 hilbert_walk_y[8] = 1;
354 hilbert_walk_y[9] = s->fragment_width;
355 hilbert_walk_y[10] = 1;
356 hilbert_walk_y[11] = -s->fragment_width;
357 hilbert_walk_y[12] = -s->fragment_width;
358 hilbert_walk_y[13] = -1;
359 hilbert_walk_y[14] = -s->fragment_width;
360 hilbert_walk_y[15] = 1;
362 hilbert_walk_c[0] = 1;
363 hilbert_walk_c[1] = 1;
364 hilbert_walk_c[2] = s->fragment_width / 2;
365 hilbert_walk_c[3] = -1;
366 hilbert_walk_c[4] = s->fragment_width / 2;
367 hilbert_walk_c[5] = s->fragment_width / 2;
368 hilbert_walk_c[6] = 1;
369 hilbert_walk_c[7] = -s->fragment_width / 2;
370 hilbert_walk_c[8] = 1;
371 hilbert_walk_c[9] = s->fragment_width / 2;
372 hilbert_walk_c[10] = 1;
373 hilbert_walk_c[11] = -s->fragment_width / 2;
374 hilbert_walk_c[12] = -s->fragment_width / 2;
375 hilbert_walk_c[13] = -1;
376 hilbert_walk_c[14] = -s->fragment_width / 2;
377 hilbert_walk_c[15] = 1;
379 hilbert_walk_mb[0] = 1;
380 hilbert_walk_mb[1] = s->macroblock_width;
381 hilbert_walk_mb[2] = 1;
382 hilbert_walk_mb[3] = -s->macroblock_width;
384 /* iterate through each superblock (all planes) and map the fragments */
385 for (i = 0; i < s->superblock_count; i++) {
386 debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
387 i, s->u_superblock_start, s->v_superblock_start);
389 /* time to re-assign the limits? */
392 /* start of Y superblocks */
393 right_edge = s->fragment_width;
394 bottom_edge = s->fragment_height;
397 superblock_row_inc = 3 * s->fragment_width -
398 (s->y_superblock_width * 4 - s->fragment_width);
399 hilbert = hilbert_walk_y;
401 /* the first operation for this variable is to advance by 1 */
402 current_fragment = -1;
404 } else if (i == s->u_superblock_start) {
406 /* start of U superblocks */
407 right_edge = s->fragment_width / 2;
408 bottom_edge = s->fragment_height / 2;
411 superblock_row_inc = 3 * (s->fragment_width / 2) -
412 (s->c_superblock_width * 4 - s->fragment_width / 2);
413 hilbert = hilbert_walk_c;
415 /* the first operation for this variable is to advance by 1 */
416 current_fragment = s->u_fragment_start - 1;
418 } else if (i == s->v_superblock_start) {
420 /* start of V superblocks */
421 right_edge = s->fragment_width / 2;
422 bottom_edge = s->fragment_height / 2;
425 superblock_row_inc = 3 * (s->fragment_width / 2) -
426 (s->c_superblock_width * 4 - s->fragment_width / 2);
427 hilbert = hilbert_walk_c;
429 /* the first operation for this variable is to advance by 1 */
430 current_fragment = s->v_fragment_start - 1;
434 if (current_width >= right_edge - 1) {
435 /* reset width and move to next superblock row */
439 /* fragment is now at the start of a new superblock row */
440 current_fragment += superblock_row_inc;
443 /* iterate through all 16 fragments in a superblock */
444 for (j = 0; j < 16; j++) {
445 current_fragment += hilbert[j];
446 current_width += travel_width[j];
447 current_height += travel_height[j];
449 /* check if the fragment is in bounds */
450 if ((current_width < right_edge) &&
451 (current_height < bottom_edge)) {
452 s->superblock_fragments[mapping_index] = current_fragment;
453 debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
454 s->superblock_fragments[mapping_index], i, j,
455 current_width, right_edge, current_height, bottom_edge);
457 s->superblock_fragments[mapping_index] = -1;
458 debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
460 current_width, right_edge, current_height, bottom_edge);
467 /* initialize the superblock <-> macroblock mapping; iterate through
468 * all of the Y plane superblocks to build this mapping */
469 right_edge = s->macroblock_width;
470 bottom_edge = s->macroblock_height;
473 superblock_row_inc = s->macroblock_width -
474 (s->y_superblock_width * 2 - s->macroblock_width);;
475 hilbert = hilbert_walk_mb;
477 current_macroblock = -1;
478 for (i = 0; i < s->u_superblock_start; i++) {
480 if (current_width >= right_edge - 1) {
481 /* reset width and move to next superblock row */
485 /* macroblock is now at the start of a new superblock row */
486 current_macroblock += superblock_row_inc;
489 /* iterate through each potential macroblock in the superblock */
490 for (j = 0; j < 4; j++) {
491 current_macroblock += hilbert_walk_mb[j];
492 current_width += travel_width_mb[j];
493 current_height += travel_height_mb[j];
495 /* check if the macroblock is in bounds */
496 if ((current_width < right_edge) &&
497 (current_height < bottom_edge)) {
498 s->superblock_macroblocks[mapping_index] = current_macroblock;
499 debug_init(" mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
500 s->superblock_macroblocks[mapping_index], i, j,
501 current_width, right_edge, current_height, bottom_edge);
503 s->superblock_macroblocks[mapping_index] = -1;
504 debug_init(" superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
506 current_width, right_edge, current_height, bottom_edge);
513 /* initialize the macroblock <-> fragment mapping */
514 current_fragment = 0;
515 current_macroblock = 0;
517 for (i = 0; i < s->fragment_height; i += 2) {
519 for (j = 0; j < s->fragment_width; j += 2) {
521 debug_init(" macroblock %d contains fragments: ", current_macroblock);
522 s->all_fragments[current_fragment].macroblock = current_macroblock;
523 s->macroblock_fragments[mapping_index++] = current_fragment;
524 debug_init("%d ", current_fragment);
526 if (j + 1 < s->fragment_width) {
527 s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
528 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
529 debug_init("%d ", current_fragment + 1);
531 s->macroblock_fragments[mapping_index++] = -1;
533 if (i + 1 < s->fragment_height) {
534 s->all_fragments[current_fragment + s->fragment_width].macroblock =
536 s->macroblock_fragments[mapping_index++] =
537 current_fragment + s->fragment_width;
538 debug_init("%d ", current_fragment + s->fragment_width);
540 s->macroblock_fragments[mapping_index++] = -1;
542 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
543 s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
545 s->macroblock_fragments[mapping_index++] =
546 current_fragment + s->fragment_width + 1;
547 debug_init("%d ", current_fragment + s->fragment_width + 1);
549 s->macroblock_fragments[mapping_index++] = -1;
552 c_fragment = s->u_fragment_start +
553 (i * s->fragment_width / 4) + (j / 2);
554 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
555 s->macroblock_fragments[mapping_index++] = c_fragment;
556 debug_init("%d ", c_fragment);
558 c_fragment = s->v_fragment_start +
559 (i * s->fragment_width / 4) + (j / 2);
560 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
561 s->macroblock_fragments[mapping_index++] = c_fragment;
562 debug_init("%d ", c_fragment);
566 if (j + 2 <= s->fragment_width)
567 current_fragment += 2;
570 current_macroblock++;
573 current_fragment += s->fragment_width;
576 return 0; /* successful path out */
580 * This function unpacks a single token (which should be in the range 0..31)
581 * and returns a zero run (number of zero coefficients in current DCT matrix
582 * before next non-zero coefficient), the next DCT coefficient, and the
583 * number of consecutive, non-EOB'd DCT blocks to EOB.
585 static void unpack_token(GetBitContext *gb, int token, int *zero_run,
586 DCTELEM *coeff, int *eob_run)
594 debug_token(" vp3 token %d: ", token);
598 debug_token("DCT_EOB_TOKEN, EOB next block\n");
603 debug_token("DCT_EOB_PAIR_TOKEN, EOB next 2 blocks\n");
608 debug_token("DCT_EOB_TRIPLE_TOKEN, EOB next 3 blocks\n");
613 debug_token("DCT_REPEAT_RUN_TOKEN, ");
614 *eob_run = get_bits(gb, 2) + 4;
615 debug_token("EOB the next %d blocks\n", *eob_run);
619 debug_token("DCT_REPEAT_RUN2_TOKEN, ");
620 *eob_run = get_bits(gb, 3) + 8;
621 debug_token("EOB the next %d blocks\n", *eob_run);
625 debug_token("DCT_REPEAT_RUN3_TOKEN, ");
626 *eob_run = get_bits(gb, 4) + 16;
627 debug_token("EOB the next %d blocks\n", *eob_run);
631 debug_token("DCT_REPEAT_RUN4_TOKEN, ");
632 *eob_run = get_bits(gb, 12);
633 debug_token("EOB the next %d blocks\n", *eob_run);
637 debug_token("DCT_SHORT_ZRL_TOKEN, ");
638 /* note that this token actually indicates that (3 extra bits) + 1 0s
639 * should be output; this case specifies a run of (3 EBs) 0s and a
640 * coefficient of 0. */
641 *zero_run = get_bits(gb, 3);
643 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
647 debug_token("DCT_ZRL_TOKEN, ");
648 /* note that this token actually indicates that (6 extra bits) + 1 0s
649 * should be output; this case specifies a run of (6 EBs) 0s and a
650 * coefficient of 0. */
651 *zero_run = get_bits(gb, 6);
653 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
657 debug_token("ONE_TOKEN, output 1\n");
662 debug_token("MINUS_ONE_TOKEN, output -1\n");
667 debug_token("TWO_TOKEN, output 2\n");
672 debug_token("MINUS_TWO_TOKEN, output -2\n");
680 debug_token("LOW_VAL_TOKENS, ");
682 *coeff = -(3 + (token - 13));
684 *coeff = 3 + (token - 13);
685 debug_token("output %d\n", *coeff);
689 debug_token("DCT_VAL_CATEGORY3, ");
690 sign = get_bits(gb, 1);
691 *coeff = 7 + get_bits(gb, 1);
694 debug_token("output %d\n", *coeff);
698 debug_token("DCT_VAL_CATEGORY4, ");
699 sign = get_bits(gb, 1);
700 *coeff = 9 + get_bits(gb, 2);
703 debug_token("output %d\n", *coeff);
707 debug_token("DCT_VAL_CATEGORY5, ");
708 sign = get_bits(gb, 1);
709 *coeff = 13 + get_bits(gb, 3);
712 debug_token("output %d\n", *coeff);
716 debug_token("DCT_VAL_CATEGORY6, ");
717 sign = get_bits(gb, 1);
718 *coeff = 21 + get_bits(gb, 4);
721 debug_token("output %d\n", *coeff);
725 debug_token("DCT_VAL_CATEGORY7, ");
726 sign = get_bits(gb, 1);
727 *coeff = 37 + get_bits(gb, 5);
730 debug_token("output %d\n", *coeff);
734 debug_token("DCT_VAL_CATEGORY8, ");
735 sign = get_bits(gb, 1);
736 *coeff = 69 + get_bits(gb, 9);
739 debug_token("output %d\n", *coeff);
747 debug_token("DCT_RUN_CATEGORY1, ");
748 *zero_run = token - 22;
753 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
757 debug_token("DCT_RUN_CATEGORY1B, ");
762 *zero_run = 6 + get_bits(gb, 2);
763 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
767 debug_token("DCT_RUN_CATEGORY1C, ");
772 *zero_run = 10 + get_bits(gb, 3);
773 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
777 debug_token("DCT_RUN_CATEGORY2, ");
778 sign = get_bits(gb, 1);
779 *coeff = 2 + get_bits(gb, 1);
783 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
787 debug_token("DCT_RUN_CATEGORY2, ");
788 sign = get_bits(gb, 1);
789 *coeff = 2 + get_bits(gb, 1);
792 *zero_run = 2 + get_bits(gb, 1);
793 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
797 printf (" vp3: help! Got a bad token: %d > 31\n", token);
804 * This function wipes out all of the fragment data.
806 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
810 /* zero out all of the fragment information */
811 s->coded_fragment_list_index = 0;
812 for (i = 0; i < s->fragment_count; i++) {
813 memset(s->all_fragments[i].coeffs, 0, 64 * sizeof(DCTELEM));
814 s->all_fragments[i].coeff_count = 0;
815 s->all_fragments[i].last_coeff = 0;
816 s->all_fragments[i].motion_x = 0xbeef;
817 s->all_fragments[i].motion_y = 0xbeef;
822 * This function sets of the dequantization tables used for a particular
825 static void init_dequantizer(Vp3DecodeContext *s)
828 int quality_scale = vp31_quality_threshold[s->quality_index];
829 int dc_scale_factor = vp31_dc_scale_factor[s->quality_index];
832 debug_vp3(" vp3: initializing dequantization tables\n");
835 * Scale dequantizers:
841 * where sf = dc_scale_factor for DC quantizer
842 * or quality_scale for AC quantizer
844 * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL.
848 /* scale DC quantizers */
849 s->intra_y_dequant[0] = vp31_intra_y_dequant[0] * dc_scale_factor / 100;
850 if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2)
851 s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2;
852 s->intra_y_dequant[0] *= SCALER;
854 s->intra_c_dequant[0] = vp31_intra_c_dequant[0] * dc_scale_factor / 100;
855 if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2)
856 s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2;
857 s->intra_c_dequant[0] *= SCALER;
859 s->inter_dequant[0] = vp31_inter_dequant[0] * dc_scale_factor / 100;
860 if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4)
861 s->inter_dequant[0] = MIN_DEQUANT_VAL * 4;
862 s->inter_dequant[0] *= SCALER;
864 /* scale AC quantizers, zigzag at the same time in preparation for
865 * the dequantization phase */
866 for (i = 1; i < 64; i++) {
870 s->intra_y_dequant[j] = vp31_intra_y_dequant[i] * quality_scale / 100;
871 if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL)
872 s->intra_y_dequant[j] = MIN_DEQUANT_VAL;
873 s->intra_y_dequant[j] *= SCALER;
875 s->intra_c_dequant[j] = vp31_intra_c_dequant[i] * quality_scale / 100;
876 if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL)
877 s->intra_c_dequant[j] = MIN_DEQUANT_VAL;
878 s->intra_c_dequant[j] *= SCALER;
880 s->inter_dequant[j] = vp31_inter_dequant[i] * quality_scale / 100;
881 if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2)
882 s->inter_dequant[j] = MIN_DEQUANT_VAL * 2;
883 s->inter_dequant[j] *= SCALER;
886 /* print debug information as requested */
887 debug_dequantizers("intra Y dequantizers:\n");
888 for (i = 0; i < 8; i++) {
889 for (j = i * 8; j < i * 8 + 8; j++) {
890 debug_dequantizers(" %4d,", s->intra_y_dequant[j]);
892 debug_dequantizers("\n");
894 debug_dequantizers("\n");
896 debug_dequantizers("intra C dequantizers:\n");
897 for (i = 0; i < 8; i++) {
898 for (j = i * 8; j < i * 8 + 8; j++) {
899 debug_dequantizers(" %4d,", s->intra_c_dequant[j]);
901 debug_dequantizers("\n");
903 debug_dequantizers("\n");
905 debug_dequantizers("interframe dequantizers:\n");
906 for (i = 0; i < 8; i++) {
907 for (j = i * 8; j < i * 8 + 8; j++) {
908 debug_dequantizers(" %4d,", s->inter_dequant[j]);
910 debug_dequantizers("\n");
912 debug_dequantizers("\n");
916 * This function is used to fetch runs of 1s or 0s from the bitstream for
917 * use in determining which superblocks are fully and partially coded.
926 * 111111xxxxxxxxxxxx 34-4129
928 static int get_superblock_run_length(GetBitContext *gb)
931 if (get_bits(gb, 1) == 0)
934 else if (get_bits(gb, 1) == 0)
935 return (2 + get_bits(gb, 1));
937 else if (get_bits(gb, 1) == 0)
938 return (4 + get_bits(gb, 1));
940 else if (get_bits(gb, 1) == 0)
941 return (6 + get_bits(gb, 2));
943 else if (get_bits(gb, 1) == 0)
944 return (10 + get_bits(gb, 3));
946 else if (get_bits(gb, 1) == 0)
947 return (18 + get_bits(gb, 4));
950 return (34 + get_bits(gb, 12));
955 * This function is used to fetch runs of 1s or 0s from the bitstream for
956 * use in determining which particular fragments are coded.
966 static int get_fragment_run_length(GetBitContext *gb)
969 if (get_bits(gb, 1) == 0)
970 return (1 + get_bits(gb, 1));
972 else if (get_bits(gb, 1) == 0)
973 return (3 + get_bits(gb, 1));
975 else if (get_bits(gb, 1) == 0)
976 return (5 + get_bits(gb, 1));
978 else if (get_bits(gb, 1) == 0)
979 return (7 + get_bits(gb, 2));
981 else if (get_bits(gb, 1) == 0)
982 return (11 + get_bits(gb, 2));
985 return (15 + get_bits(gb, 4));
990 * This function decodes a VLC from the bitstream and returns a number
991 * that ranges from 0..7. The number indicates which of the 8 coding
1005 static int get_mode_code(GetBitContext *gb)
1008 if (get_bits(gb, 1) == 0)
1011 else if (get_bits(gb, 1) == 0)
1014 else if (get_bits(gb, 1) == 0)
1017 else if (get_bits(gb, 1) == 0)
1020 else if (get_bits(gb, 1) == 0)
1023 else if (get_bits(gb, 1) == 0)
1026 else if (get_bits(gb, 1) == 0)
1035 * This function extracts a motion vector from the bitstream using a VLC
1036 * scheme. 3 bits are fetched from the bitstream and 1 of 8 actions is
1037 * taken depending on the value on those 3 bits:
1042 * 3: if (next bit is 1) return -2, else return 2
1043 * 4: if (next bit is 1) return -3, else return 3
1044 * 5: return 4 + (next 2 bits), next bit is sign
1045 * 6: return 8 + (next 3 bits), next bit is sign
1046 * 7: return 16 + (next 4 bits), next bit is sign
1048 static int get_motion_vector_vlc(GetBitContext *gb)
1052 bits = get_bits(gb, 3);
1069 if (get_bits(gb, 1) == 0)
1076 if (get_bits(gb, 1) == 0)
1083 bits = 4 + get_bits(gb, 2);
1084 if (get_bits(gb, 1) == 1)
1089 bits = 8 + get_bits(gb, 3);
1090 if (get_bits(gb, 1) == 1)
1095 bits = 16 + get_bits(gb, 4);
1096 if (get_bits(gb, 1) == 1)
1106 * This function fetches a 5-bit number from the stream followed by
1107 * a sign and calls it a motion vector.
1109 static int get_motion_vector_fixed(GetBitContext *gb)
1114 bits = get_bits(gb, 5);
1116 if (get_bits(gb, 1) == 1)
1123 * This function unpacks all of the superblock/macroblock/fragment coding
1124 * information from the bitstream.
1126 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
1129 int current_superblock = 0;
1130 int current_run = 0;
1131 int decode_fully_flags = 0;
1132 int decode_partial_blocks = 0;
1133 int first_c_fragment_seen;
1136 int current_fragment;
1138 debug_vp3(" vp3: unpacking superblock coding\n");
1142 debug_vp3(" keyframe-- all superblocks are fully coded\n");
1143 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
1147 /* unpack the list of partially-coded superblocks */
1148 bit = get_bits(gb, 1);
1149 /* toggle the bit because as soon as the first run length is
1150 * fetched the bit will be toggled again */
1152 while (current_superblock < s->superblock_count) {
1153 if (current_run == 0) {
1155 current_run = get_superblock_run_length(gb);
1156 debug_block_coding(" setting superblocks %d..%d to %s\n",
1158 current_superblock + current_run - 1,
1159 (bit) ? "partially coded" : "not coded");
1161 /* if any of the superblocks are not partially coded, flag
1162 * a boolean to decode the list of fully-coded superblocks */
1164 decode_fully_flags = 1;
1167 /* make a note of the fact that there are partially coded
1169 decode_partial_blocks = 1;
1172 s->superblock_coding[current_superblock++] =
1173 (bit) ? SB_PARTIALLY_CODED : SB_NOT_CODED;
1177 /* unpack the list of fully coded superblocks if any of the blocks were
1178 * not marked as partially coded in the previous step */
1179 if (decode_fully_flags) {
1181 current_superblock = 0;
1183 bit = get_bits(gb, 1);
1184 /* toggle the bit because as soon as the first run length is
1185 * fetched the bit will be toggled again */
1187 while (current_superblock < s->superblock_count) {
1189 /* skip any superblocks already marked as partially coded */
1190 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
1192 if (current_run == 0) {
1194 current_run = get_superblock_run_length(gb);
1197 debug_block_coding(" setting superblock %d to %s\n",
1199 (bit) ? "fully coded" : "not coded");
1200 s->superblock_coding[current_superblock] =
1201 (bit) ? SB_FULLY_CODED : SB_NOT_CODED;
1204 current_superblock++;
1208 /* if there were partial blocks, initialize bitstream for
1209 * unpacking fragment codings */
1210 if (decode_partial_blocks) {
1213 bit = get_bits(gb, 1);
1214 /* toggle the bit because as soon as the first run length is
1215 * fetched the bit will be toggled again */
1220 /* figure out which fragments are coded; iterate through each
1221 * superblock (all planes) */
1222 s->coded_fragment_list_index = 0;
1223 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
1224 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
1225 first_c_fragment_seen = 0;
1226 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
1227 for (i = 0; i < s->superblock_count; i++) {
1229 /* iterate through all 16 fragments in a superblock */
1230 for (j = 0; j < 16; j++) {
1232 /* if the fragment is in bounds, check its coding status */
1233 current_fragment = s->superblock_fragments[i * 16 + j];
1234 if (current_fragment >= s->fragment_count) {
1235 printf (" vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
1236 current_fragment, s->fragment_count);
1239 if (current_fragment != -1) {
1240 if (s->superblock_coding[i] == SB_NOT_CODED) {
1242 /* copy all the fragments from the prior frame */
1243 s->all_fragments[current_fragment].coding_method =
1246 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
1248 /* fragment may or may not be coded; this is the case
1249 * that cares about the fragment coding runs */
1250 if (current_run == 0) {
1252 current_run = get_fragment_run_length(gb);
1256 /* default mode; actual mode will be decoded in
1258 s->all_fragments[current_fragment].coding_method =
1260 s->coded_fragment_list[s->coded_fragment_list_index] =
1262 if ((current_fragment >= s->u_fragment_start) &&
1263 (s->last_coded_y_fragment == -1) &&
1264 (!first_c_fragment_seen)) {
1265 s->first_coded_c_fragment = s->coded_fragment_list_index;
1266 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1267 first_c_fragment_seen = 1;
1269 s->coded_fragment_list_index++;
1270 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1271 debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
1272 i, current_fragment);
1274 /* not coded; copy this fragment from the prior frame */
1275 s->all_fragments[current_fragment].coding_method =
1277 debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
1278 i, current_fragment);
1285 /* fragments are fully coded in this superblock; actual
1286 * coding will be determined in next step */
1287 s->all_fragments[current_fragment].coding_method =
1289 s->coded_fragment_list[s->coded_fragment_list_index] =
1291 if ((current_fragment >= s->u_fragment_start) &&
1292 (s->last_coded_y_fragment == -1) &&
1293 (!first_c_fragment_seen)) {
1294 s->first_coded_c_fragment = s->coded_fragment_list_index;
1295 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1296 first_c_fragment_seen = 1;
1298 s->coded_fragment_list_index++;
1299 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1300 debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
1301 i, current_fragment);
1307 if (!first_c_fragment_seen)
1308 /* only Y fragments coded in this frame */
1309 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
1311 /* end the list of coded C fragments */
1312 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
1314 debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
1315 s->coded_fragment_list_index,
1316 s->first_coded_y_fragment,
1317 s->last_coded_y_fragment,
1318 s->first_coded_c_fragment,
1319 s->last_coded_c_fragment);
1325 * This function unpacks all the coding mode data for individual macroblocks
1326 * from the bitstream.
1328 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
1332 int current_macroblock;
1333 int current_fragment;
1336 debug_vp3(" vp3: unpacking encoding modes\n");
1339 debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
1341 for (i = 0; i < s->fragment_count; i++)
1342 s->all_fragments[i].coding_method = MODE_INTRA;
1346 /* fetch the mode coding scheme for this frame */
1347 scheme = get_bits(gb, 3);
1348 debug_modes(" using mode alphabet %d\n", scheme);
1350 /* is it a custom coding scheme? */
1352 debug_modes(" custom mode alphabet ahead:\n");
1353 for (i = 0; i < 8; i++)
1354 ModeAlphabet[scheme][get_bits(gb, 3)] = i;
1357 for (i = 0; i < 8; i++)
1358 debug_modes(" mode[%d][%d] = %d\n", scheme, i,
1359 ModeAlphabet[scheme][i]);
1361 /* iterate through all of the macroblocks that contain 1 or more
1362 * coded fragments */
1363 for (i = 0; i < s->u_superblock_start; i++) {
1365 for (j = 0; j < 4; j++) {
1366 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1367 if ((current_macroblock == -1) ||
1368 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1370 if (current_macroblock >= s->macroblock_count) {
1371 printf (" vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
1372 current_macroblock, s->macroblock_count);
1376 /* mode 7 means get 3 bits for each coding mode */
1378 coding_mode = get_bits(gb, 3);
1380 coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
1382 s->macroblock_coding[current_macroblock] = coding_mode;
1383 for (k = 0; k < 6; k++) {
1385 s->macroblock_fragments[current_macroblock * 6 + k];
1386 if (current_fragment == -1)
1388 if (current_fragment >= s->fragment_count) {
1389 printf (" vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
1390 current_fragment, s->fragment_count);
1393 if (s->all_fragments[current_fragment].coding_method !=
1395 s->all_fragments[current_fragment].coding_method =
1399 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
1400 s->macroblock_fragments[current_macroblock * 6], coding_mode);
1409 * This function unpacks all the motion vectors for the individual
1410 * macroblocks from the bitstream.
1412 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
1418 int last_motion_x = 0;
1419 int last_motion_y = 0;
1420 int prior_last_motion_x = 0;
1421 int prior_last_motion_y = 0;
1422 int current_macroblock;
1423 int current_fragment;
1425 debug_vp3(" vp3: unpacking motion vectors\n");
1429 debug_vp3(" keyframe-- there are no motion vectors\n");
1433 memset(motion_x, 0, 6 * sizeof(int));
1434 memset(motion_y, 0, 6 * sizeof(int));
1436 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
1437 coding_mode = get_bits(gb, 1);
1438 debug_vectors(" using %s scheme for unpacking motion vectors\n",
1439 (coding_mode == 0) ? "VLC" : "fixed-length");
1441 /* iterate through all of the macroblocks that contain 1 or more
1442 * coded fragments */
1443 for (i = 0; i < s->u_superblock_start; i++) {
1445 for (j = 0; j < 4; j++) {
1446 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1447 if ((current_macroblock == -1) ||
1448 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1450 if (current_macroblock >= s->macroblock_count) {
1451 printf (" vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
1452 current_macroblock, s->macroblock_count);
1456 current_fragment = s->macroblock_fragments[current_macroblock * 6];
1457 if (current_fragment >= s->fragment_count) {
1458 printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
1459 current_fragment, s->fragment_count);
1462 switch (s->macroblock_coding[current_macroblock]) {
1464 case MODE_INTER_PLUS_MV:
1465 case MODE_GOLDEN_MV:
1466 /* all 6 fragments use the same motion vector */
1467 if (coding_mode == 0) {
1468 motion_x[0] = get_motion_vector_vlc(gb);
1469 motion_y[0] = get_motion_vector_vlc(gb);
1471 motion_x[0] = get_motion_vector_fixed(gb);
1472 motion_y[0] = get_motion_vector_fixed(gb);
1474 for (k = 1; k < 6; k++) {
1475 motion_x[k] = motion_x[0];
1476 motion_y[k] = motion_y[0];
1479 /* vector maintenance, only on MODE_INTER_PLUS_MV */
1480 if (s->macroblock_coding[current_macroblock] ==
1481 MODE_INTER_PLUS_MV) {
1482 prior_last_motion_x = last_motion_x;
1483 prior_last_motion_y = last_motion_y;
1484 last_motion_x = motion_x[0];
1485 last_motion_y = motion_y[0];
1489 case MODE_INTER_FOURMV:
1490 /* fetch 4 vectors from the bitstream, one for each
1491 * Y fragment, then average for the C fragment vectors */
1492 motion_x[4] = motion_y[4] = 0;
1493 for (k = 0; k < 4; k++) {
1494 if (coding_mode == 0) {
1495 motion_x[k] = get_motion_vector_vlc(gb);
1496 motion_y[k] = get_motion_vector_vlc(gb);
1498 motion_x[k] = get_motion_vector_fixed(gb);
1499 motion_y[k] = get_motion_vector_fixed(gb);
1501 motion_x[4] += motion_x[k];
1502 motion_y[4] += motion_y[k];
1505 if (motion_x[4] >= 0)
1506 motion_x[4] = (motion_x[4] + 2) / 4;
1508 motion_x[4] = (motion_x[4] - 2) / 4;
1509 motion_x[5] = motion_x[4];
1511 if (motion_y[4] >= 0)
1512 motion_y[4] = (motion_y[4] + 2) / 4;
1514 motion_y[4] = (motion_y[4] - 2) / 4;
1515 motion_y[5] = motion_y[4];
1517 /* vector maintenance; vector[3] is treated as the
1518 * last vector in this case */
1519 prior_last_motion_x = last_motion_x;
1520 prior_last_motion_y = last_motion_y;
1521 last_motion_x = motion_x[3];
1522 last_motion_y = motion_y[3];
1525 case MODE_INTER_LAST_MV:
1526 /* all 6 fragments use the last motion vector */
1527 motion_x[0] = last_motion_x;
1528 motion_y[0] = last_motion_y;
1529 for (k = 1; k < 6; k++) {
1530 motion_x[k] = motion_x[0];
1531 motion_y[k] = motion_y[0];
1534 /* no vector maintenance (last vector remains the
1538 case MODE_INTER_PRIOR_LAST:
1539 /* all 6 fragments use the motion vector prior to the
1540 * last motion vector */
1541 motion_x[0] = prior_last_motion_x;
1542 motion_y[0] = prior_last_motion_y;
1543 for (k = 1; k < 6; k++) {
1544 motion_x[k] = motion_x[0];
1545 motion_y[k] = motion_y[0];
1548 /* vector maintenance */
1549 prior_last_motion_x = last_motion_x;
1550 prior_last_motion_y = last_motion_y;
1551 last_motion_x = motion_x[0];
1552 last_motion_y = motion_y[0];
1556 /* covers intra, inter without MV, golden without MV */
1557 memset(motion_x, 0, 6 * sizeof(int));
1558 memset(motion_y, 0, 6 * sizeof(int));
1560 /* no vector maintenance */
1564 /* assign the motion vectors to the correct fragments */
1565 debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
1567 s->macroblock_coding[current_macroblock]);
1568 for (k = 0; k < 6; k++) {
1570 s->macroblock_fragments[current_macroblock * 6 + k];
1571 if (current_fragment == -1)
1573 if (current_fragment >= s->fragment_count) {
1574 printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
1575 current_fragment, s->fragment_count);
1578 s->all_fragments[current_fragment].motion_x = motion_x[k];
1579 s->all_fragments[current_fragment].motion_y = motion_y[k];
1580 debug_vectors(" vector %d: fragment %d = (%d, %d)\n",
1581 k, current_fragment, motion_x[k], motion_y[k]);
1591 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1592 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1593 * data. This function unpacks all the VLCs for either the Y plane or both
1594 * C planes, and is called for DC coefficients or different AC coefficient
1595 * levels (since different coefficient types require different VLC tables.
1597 * This function returns a residual eob run. E.g, if a particular token gave
1598 * instructions to EOB the next 5 fragments and there were only 2 fragments
1599 * left in the current fragment range, 3 would be returned so that it could
1600 * be passed into the next call to this same function.
1602 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1603 VLC *table, int coeff_index,
1604 int first_fragment, int last_fragment,
1611 Vp3Fragment *fragment;
1613 if ((first_fragment >= s->fragment_count) ||
1614 (last_fragment >= s->fragment_count)) {
1616 printf (" vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
1617 first_fragment, last_fragment);
1621 for (i = first_fragment; i <= last_fragment; i++) {
1623 fragment = &s->all_fragments[s->coded_fragment_list[i]];
1624 if (fragment->coeff_count > coeff_index)
1628 /* decode a VLC into a token */
1629 token = get_vlc2(gb, table->table, 5, 3);
1630 debug_vlc(" token = %2d, ", token);
1631 /* use the token to get a zero run, a coefficient, and an eob run */
1632 unpack_token(gb, token, &zero_run, &coeff, &eob_run);
1636 fragment->coeff_count += zero_run;
1637 if (fragment->coeff_count < 64)
1638 fragment->coeffs[fragment->coeff_count++] = coeff;
1639 debug_vlc(" fragment %d coeff = %d\n",
1640 s->coded_fragment_list[i], fragment->coeffs[coeff_index]);
1642 fragment->last_coeff = fragment->coeff_count;
1643 fragment->coeff_count = 64;
1644 debug_vlc(" fragment %d eob with %d coefficients\n",
1645 s->coded_fragment_list[i], fragment->last_coeff);
1654 * This function unpacks all of the DCT coefficient data from the
1657 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1664 int residual_eob_run = 0;
1666 /* fetch the DC table indices */
1667 dc_y_table = get_bits(gb, 4);
1668 dc_c_table = get_bits(gb, 4);
1670 /* unpack the Y plane DC coefficients */
1671 debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
1673 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1674 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1676 /* unpack the C plane DC coefficients */
1677 debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
1679 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1680 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1682 /* fetch the AC table indices */
1683 ac_y_table = get_bits(gb, 4);
1684 ac_c_table = get_bits(gb, 4);
1686 /* unpack the group 1 AC coefficients (coeffs 1-5) */
1687 for (i = 1; i <= 5; i++) {
1689 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1691 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
1692 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1694 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1696 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
1697 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1700 /* unpack the group 2 AC coefficients (coeffs 6-14) */
1701 for (i = 6; i <= 14; i++) {
1703 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1705 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
1706 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1708 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1710 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
1711 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1714 /* unpack the group 3 AC coefficients (coeffs 15-27) */
1715 for (i = 15; i <= 27; i++) {
1717 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1719 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
1720 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1722 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1724 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
1725 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1728 /* unpack the group 4 AC coefficients (coeffs 28-63) */
1729 for (i = 28; i <= 63; i++) {
1731 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1733 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
1734 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1736 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1738 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
1739 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1746 * This function reverses the DC prediction for each coded fragment in
1747 * the frame. Much of this function is adapted directly from the original
1750 #define COMPATIBLE_FRAME(x) \
1751 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1752 #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
1753 static inline int iabs (int x) { return ((x < 0) ? -x : x); }
1755 static void reverse_dc_prediction(Vp3DecodeContext *s,
1758 int fragment_height)
1767 int i = first_fragment;
1770 * Fragment prediction groups:
1778 * Note: Groups 5 and 7 do not exist as it would mean that the
1779 * fragment's x coordinate is both 0 and (width - 1) at the same time.
1781 int predictor_group;
1784 /* validity flags for the left, up-left, up, and up-right fragments */
1785 int fl, ful, fu, fur;
1787 /* DC values for the left, up-left, up, and up-right fragments */
1788 int vl, vul, vu, vur;
1790 /* indices for the left, up-left, up, and up-right fragments */
1794 * The 6 fields mean:
1795 * 0: up-left multiplier
1797 * 2: up-right multiplier
1798 * 3: left multiplier
1800 * 5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
1802 int predictor_transform[16][6] = {
1803 { 0, 0, 0, 0, 0, 0 },
1804 { 0, 0, 0, 1, 0, 0 }, // PL
1805 { 0, 0, 1, 0, 0, 0 }, // PUR
1806 { 0, 0, 53, 75, 127, 7 }, // PUR|PL
1807 { 0, 1, 0, 0, 0, 0 }, // PU
1808 { 0, 1, 0, 1, 1, 1 }, // PU|PL
1809 { 0, 1, 0, 0, 0, 0 }, // PU|PUR
1810 { 0, 0, 53, 75, 127, 7 }, // PU|PUR|PL
1811 { 1, 0, 0, 0, 0, 0 }, // PUL
1812 { 0, 0, 0, 1, 0, 0 }, // PUL|PL
1813 { 1, 0, 1, 0, 1, 1 }, // PUL|PUR
1814 { 0, 0, 53, 75, 127, 7 }, // PUL|PUR|PL
1815 { 0, 1, 0, 0, 0, 0 }, // PUL|PU
1816 {-26, 29, 0, 29, 31, 5 }, // PUL|PU|PL
1817 { 3, 10, 3, 0, 15, 4 }, // PUL|PU|PUR
1818 {-26, 29, 0, 29, 31, 5 } // PUL|PU|PUR|PL
1821 /* This table shows which types of blocks can use other blocks for
1822 * prediction. For example, INTRA is the only mode in this table to
1823 * have a frame number of 0. That means INTRA blocks can only predict
1824 * from other INTRA blocks. There are 2 golden frame coding types;
1825 * blocks encoding in these modes can only predict from other blocks
1826 * that were encoded with these 1 of these 2 modes. */
1827 unsigned char compatible_frame[8] = {
1828 1, /* MODE_INTER_NO_MV */
1830 1, /* MODE_INTER_PLUS_MV */
1831 1, /* MODE_INTER_LAST_MV */
1832 1, /* MODE_INTER_PRIOR_MV */
1833 2, /* MODE_USING_GOLDEN */
1834 2, /* MODE_GOLDEN_MV */
1835 1 /* MODE_INTER_FOUR_MV */
1837 int current_frame_type;
1839 /* there is a last DC predictor for each of the 3 frame types */
1844 debug_vp3(" vp3: reversing DC prediction\n");
1846 vul = vu = vur = vl = 0;
1847 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1849 /* for each fragment row... */
1850 for (y = 0; y < fragment_height; y++) {
1852 /* for each fragment in a row... */
1853 for (x = 0; x < fragment_width; x++, i++) {
1855 /* reverse prediction if this block was coded */
1856 if (s->all_fragments[i].coding_method != MODE_COPY) {
1858 current_frame_type =
1859 compatible_frame[s->all_fragments[i].coding_method];
1860 predictor_group = (x == 0) + ((y == 0) << 1) +
1861 ((x + 1 == fragment_width) << 2);
1862 debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
1863 i, predictor_group, s->all_fragments[i].coeffs[0]);
1865 switch (predictor_group) {
1868 /* main body of fragments; consider all 4 possible
1869 * fragments for prediction */
1871 /* calculate the indices of the predicting fragments */
1872 ul = i - fragment_width - 1;
1873 u = i - fragment_width;
1874 ur = i - fragment_width + 1;
1877 /* fetch the DC values for the predicting fragments */
1878 vul = s->all_fragments[ul].coeffs[0];
1879 vu = s->all_fragments[u].coeffs[0];
1880 vur = s->all_fragments[ur].coeffs[0];
1881 vl = s->all_fragments[l].coeffs[0];
1883 /* figure out which fragments are valid */
1884 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
1885 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1886 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1887 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1889 /* decide which predictor transform to use */
1890 transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
1895 /* left column of fragments, not including top corner;
1896 * only consider up and up-right fragments */
1898 /* calculate the indices of the predicting fragments */
1899 u = i - fragment_width;
1900 ur = i - fragment_width + 1;
1902 /* fetch the DC values for the predicting fragments */
1903 vu = s->all_fragments[u].coeffs[0];
1904 vur = s->all_fragments[ur].coeffs[0];
1906 /* figure out which fragments are valid */
1907 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1908 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1910 /* decide which predictor transform to use */
1911 transform = (fu*PU) | (fur*PUR);
1917 /* top row of fragments, not including top-left frag;
1918 * only consider the left fragment for prediction */
1920 /* calculate the indices of the predicting fragments */
1923 /* fetch the DC values for the predicting fragments */
1924 vl = s->all_fragments[l].coeffs[0];
1926 /* figure out which fragments are valid */
1927 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1929 /* decide which predictor transform to use */
1930 transform = (fl*PL);
1935 /* top-left fragment */
1937 /* nothing to predict from in this case */
1943 /* right column of fragments, not including top corner;
1944 * consider up-left, up, and left fragments for
1947 /* calculate the indices of the predicting fragments */
1948 ul = i - fragment_width - 1;
1949 u = i - fragment_width;
1952 /* fetch the DC values for the predicting fragments */
1953 vul = s->all_fragments[ul].coeffs[0];
1954 vu = s->all_fragments[u].coeffs[0];
1955 vl = s->all_fragments[l].coeffs[0];
1957 /* figure out which fragments are valid */
1958 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
1959 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1960 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1962 /* decide which predictor transform to use */
1963 transform = (fl*PL) | (fu*PU) | (ful*PUL);
1969 debug_dc_pred("transform = %d, ", transform);
1971 if (transform == 0) {
1973 /* if there were no fragments to predict from, use last
1975 s->all_fragments[i].coeffs[0] += last_dc[current_frame_type];
1976 debug_dc_pred("from last DC (%d) = %d\n",
1977 current_frame_type, s->all_fragments[i].coeffs[0]);
1981 /* apply the appropriate predictor transform */
1983 (predictor_transform[transform][0] * vul) +
1984 (predictor_transform[transform][1] * vu) +
1985 (predictor_transform[transform][2] * vur) +
1986 (predictor_transform[transform][3] * vl);
1988 /* if there is a shift value in the transform, add
1989 * the sign bit before the shift */
1990 if (predictor_transform[transform][5] != 0) {
1991 predicted_dc += ((predicted_dc >> 15) &
1992 predictor_transform[transform][4]);
1993 predicted_dc >>= predictor_transform[transform][5];
1996 /* check for outranging on the [ul u l] and
1997 * [ul u ur l] predictors */
1998 if ((transform == 13) || (transform == 15)) {
1999 if (iabs(predicted_dc - vu) > 128)
2001 else if (iabs(predicted_dc - vl) > 128)
2003 else if (iabs(predicted_dc - vul) > 128)
2007 /* at long last, apply the predictor */
2008 s->all_fragments[i].coeffs[0] += predicted_dc;
2009 debug_dc_pred("from pred DC = %d\n",
2010 s->all_fragments[i].coeffs[0]);
2014 last_dc[current_frame_type] = s->all_fragments[i].coeffs[0];
2021 * This function performs the final rendering of each fragment's data
2022 * onto the output frame.
2024 static void render_fragments(Vp3DecodeContext *s,
2028 int plane /* 0 = Y, 1 = U, 2 = V */)
2032 int i = first_fragment;
2034 int16_t *dequantizer;
2035 DCTELEM dequant_block[64];
2036 DCTELEM dequant_block_permuted[64];
2037 unsigned char *output_plane;
2038 unsigned char *last_plane;
2039 unsigned char *golden_plane;
2041 int motion_x, motion_y;
2042 int upper_motion_limit, lower_motion_limit;
2043 int motion_halfpel_index;
2044 unsigned int motion_source;
2046 debug_vp3(" vp3: rendering final fragments for %s\n",
2047 (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
2049 /* set up plane-specific parameters */
2051 dequantizer = s->intra_y_dequant;
2052 output_plane = s->current_frame.data[0];
2053 last_plane = s->last_frame.data[0];
2054 golden_plane = s->golden_frame.data[0];
2055 stride = -s->current_frame.linesize[0];
2056 upper_motion_limit = 7 * s->current_frame.linesize[0];
2057 lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
2058 } else if (plane == 1) {
2059 dequantizer = s->intra_c_dequant;
2060 output_plane = s->current_frame.data[1];
2061 last_plane = s->last_frame.data[1];
2062 golden_plane = s->golden_frame.data[1];
2063 stride = -s->current_frame.linesize[1];
2064 upper_motion_limit = 7 * s->current_frame.linesize[1];
2065 lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
2067 dequantizer = s->intra_c_dequant;
2068 output_plane = s->current_frame.data[2];
2069 last_plane = s->last_frame.data[2];
2070 golden_plane = s->golden_frame.data[2];
2071 stride = -s->current_frame.linesize[2];
2072 upper_motion_limit = 7 * s->current_frame.linesize[2];
2073 lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
2076 /* for each fragment row... */
2077 for (y = 0; y < height; y += 8) {
2079 /* for each fragment in a row... */
2080 for (x = 0; x < width; x += 8, i++) {
2082 if ((i < 0) || (i >= s->fragment_count)) {
2083 printf (" vp3:render_fragments(): bad fragment number (%d)\n", i);
2087 /* transform if this block was coded */
2088 if (s->all_fragments[i].coding_method != MODE_COPY) {
2090 motion_source = s->all_fragments[i].first_pixel;
2091 motion_halfpel_index = 0;
2093 /* sort out the motion vector if this fragment is coded
2094 * using a motion vector method */
2095 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2096 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2097 motion_x = s->all_fragments[i].motion_x;
2098 motion_y = s->all_fragments[i].motion_y;
2099 if ((motion_x == 0xbeef) || (motion_y == 0xbeef))
2100 printf (" help! got beefy vector! (%X, %X)\n", motion_x, motion_y);
2102 if (motion_x >= 0) {
2103 motion_halfpel_index = motion_x & 0x01;
2104 motion_source += (motion_x >> 1);
2106 motion_x = -motion_x;
2107 motion_halfpel_index = motion_x & 0x01;
2108 motion_source -= ((motion_x + 1) >> 1);
2111 // motion_y = -motion_y;
2112 if (motion_y >= 0) {
2113 motion_halfpel_index |= (motion_y & 0x01) << 1;
2114 motion_source += ((motion_y >> 1) * stride);
2116 motion_y = -motion_y;
2117 motion_halfpel_index |= (motion_y & 0x01) << 1;
2118 motion_source -= (((motion_y + 1) >> 1) * stride);
2121 /* if the are any problems with a motion vector, refuse
2122 * to render the block */
2123 if ((motion_source < upper_motion_limit) ||
2124 (motion_source > lower_motion_limit)) {
2125 // printf (" vp3: help! motion source (%d) out of range (%d..%d)\n",
2126 // motion_source, upper_motion_limit, lower_motion_limit);
2131 /* first, take care of copying a block from either the
2132 * previous or the golden frame */
2133 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2134 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) {
2136 s->dsp.put_pixels_tab[1][motion_halfpel_index](
2137 output_plane + s->all_fragments[i].first_pixel,
2138 golden_plane + motion_source,
2142 if (s->all_fragments[i].coding_method != MODE_INTRA) {
2144 s->dsp.put_pixels_tab[1][motion_halfpel_index](
2145 output_plane + s->all_fragments[i].first_pixel,
2146 last_plane + motion_source,
2150 /* dequantize the DCT coefficients */
2151 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
2152 i, s->all_fragments[i].coding_method,
2153 s->all_fragments[i].coeffs[0], dequantizer[0]);
2154 for (j = 0; j < 64; j++)
2155 dequant_block[dezigzag_index[j]] =
2156 s->all_fragments[i].coeffs[j] *
2158 for (j = 0; j < 64; j++)
2159 dequant_block_permuted[s->dsp.idct_permutation[j]] =
2162 debug_idct("dequantized block:\n");
2163 for (m = 0; m < 8; m++) {
2164 for (n = 0; n < 8; n++) {
2165 debug_idct(" %5d", dequant_block[m * 8 + n]);
2171 /* invert DCT and place (or add) in final output */
2173 if (s->all_fragments[i].coding_method == MODE_INTRA) {
2174 dequant_block_permuted[0] += 1024;
2176 output_plane + s->all_fragments[i].first_pixel,
2177 stride, dequant_block_permuted);
2180 output_plane + s->all_fragments[i].first_pixel,
2181 stride, dequant_block_permuted);
2184 debug_idct("block after idct_%s():\n",
2185 (s->all_fragments[i].coding_method == MODE_INTRA)?
2187 for (m = 0; m < 8; m++) {
2188 for (n = 0; n < 8; n++) {
2189 debug_idct(" %3d", *(output_plane +
2190 s->all_fragments[i].first_pixel + (m * stride + n)));
2198 /* copy directly from the previous frame */
2199 s->dsp.put_pixels_tab[1][0](
2200 output_plane + s->all_fragments[i].first_pixel,
2201 last_plane + s->all_fragments[i].first_pixel,
2213 * This function computes the first pixel addresses for each fragment.
2214 * This function needs to be invoked after the first frame is allocated
2215 * so that it has access to the plane strides.
2217 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
2222 /* figure out the first pixel addresses for each of the fragments */
2225 for (y = s->fragment_height; y > 0; y--) {
2226 for (x = 0; x < s->fragment_width; x++) {
2227 s->all_fragments[i++].first_pixel =
2228 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2229 s->golden_frame.linesize[0] +
2230 x * FRAGMENT_PIXELS;
2231 debug_init(" fragment %d, first pixel @ %d\n",
2232 i-1, s->all_fragments[i-1].first_pixel);
2237 i = s->u_fragment_start;
2238 for (y = s->fragment_height / 2; y > 0; y--) {
2239 for (x = 0; x < s->fragment_width / 2; x++) {
2240 s->all_fragments[i++].first_pixel =
2241 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2242 s->golden_frame.linesize[1] +
2243 x * FRAGMENT_PIXELS;
2244 debug_init(" fragment %d, first pixel @ %d\n",
2245 i-1, s->all_fragments[i-1].first_pixel);
2250 i = s->v_fragment_start;
2251 for (y = s->fragment_height / 2; y > 0; y--) {
2252 for (x = 0; x < s->fragment_width / 2; x++) {
2253 s->all_fragments[i++].first_pixel =
2254 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2255 s->golden_frame.linesize[2] +
2256 x * FRAGMENT_PIXELS;
2257 debug_init(" fragment %d, first pixel @ %d\n",
2258 i-1, s->all_fragments[i-1].first_pixel);
2264 * This is the ffmpeg/libavcodec API init function.
2266 static int vp3_decode_init(AVCodecContext *avctx)
2268 Vp3DecodeContext *s = avctx->priv_data;
2272 int y_superblock_count;
2273 int c_superblock_count;
2277 s->width = avctx->width;
2278 s->height = avctx->height;
2280 s->width = (avctx->width + 15) & 0xFFFFFFF0;
2281 s->height = (avctx->height + 15) & 0xFFFFFFF0;
2283 avctx->pix_fmt = PIX_FMT_YUV420P;
2284 avctx->has_b_frames = 0;
2285 dsputil_init(&s->dsp, avctx);
2287 /* initialize to an impossible value which will force a recalculation
2288 * in the first frame decode */
2289 s->quality_index = -1;
2291 s->y_superblock_width = (s->width + 31) / 32;
2292 s->y_superblock_height = (s->height + 31) / 32;
2293 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2295 /* work out the dimensions for the C planes */
2296 c_width = s->width / 2;
2297 c_height = s->height / 2;
2298 s->c_superblock_width = (c_width + 31) / 32;
2299 s->c_superblock_height = (c_height + 31) / 32;
2300 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2302 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
2303 s->u_superblock_start = y_superblock_count;
2304 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
2305 s->superblock_coding = av_malloc(s->superblock_count);
2307 s->macroblock_width = (s->width + 15) / 16;
2308 s->macroblock_height = (s->height + 15) / 16;
2309 s->macroblock_count = s->macroblock_width * s->macroblock_height;
2311 s->fragment_width = s->width / FRAGMENT_PIXELS;
2312 s->fragment_height = s->height / FRAGMENT_PIXELS;
2314 /* fragment count covers all 8x8 blocks for all 3 planes */
2315 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
2316 s->u_fragment_start = s->fragment_width * s->fragment_height;
2317 s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
2319 debug_init(" Y plane: %d x %d\n", s->width, s->height);
2320 debug_init(" C plane: %d x %d\n", c_width, c_height);
2321 debug_init(" Y superblocks: %d x %d, %d total\n",
2322 s->y_superblock_width, s->y_superblock_height, y_superblock_count);
2323 debug_init(" C superblocks: %d x %d, %d total\n",
2324 s->c_superblock_width, s->c_superblock_height, c_superblock_count);
2325 debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
2326 s->superblock_count, s->u_superblock_start, s->v_superblock_start);
2327 debug_init(" macroblocks: %d x %d, %d total\n",
2328 s->macroblock_width, s->macroblock_height, s->macroblock_count);
2329 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
2333 s->u_fragment_start,
2334 s->v_fragment_start);
2336 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
2337 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
2338 s->pixel_addresses_inited = 0;
2340 /* init VLC tables */
2341 for (i = 0; i < 16; i++) {
2344 init_vlc(&s->dc_vlc[i], 5, 32,
2345 &dc_bias[i][0][1], 4, 2,
2346 &dc_bias[i][0][0], 4, 2);
2348 /* group 1 AC histograms */
2349 init_vlc(&s->ac_vlc_1[i], 5, 32,
2350 &ac_bias_0[i][0][1], 4, 2,
2351 &ac_bias_0[i][0][0], 4, 2);
2353 /* group 2 AC histograms */
2354 init_vlc(&s->ac_vlc_2[i], 5, 32,
2355 &ac_bias_1[i][0][1], 4, 2,
2356 &ac_bias_1[i][0][0], 4, 2);
2358 /* group 3 AC histograms */
2359 init_vlc(&s->ac_vlc_3[i], 5, 32,
2360 &ac_bias_2[i][0][1], 4, 2,
2361 &ac_bias_2[i][0][0], 4, 2);
2363 /* group 4 AC histograms */
2364 init_vlc(&s->ac_vlc_4[i], 5, 32,
2365 &ac_bias_3[i][0][1], 4, 2,
2366 &ac_bias_3[i][0][0], 4, 2);
2369 /* build quantization zigzag table */
2370 for (i = 0; i < 64; i++)
2371 zigzag_index[dezigzag_index[i]] = i;
2373 /* work out the block mapping tables */
2374 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
2375 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
2376 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
2377 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
2378 init_block_mapping(s);
2380 for (i = 0; i < 3; i++) {
2381 s->current_frame.data[i] = NULL;
2382 s->last_frame.data[i] = NULL;
2383 s->golden_frame.data[i] = NULL;
2390 * This is the ffmpeg/libavcodec API frame decode function.
2392 static int vp3_decode_frame(AVCodecContext *avctx,
2393 void *data, int *data_size,
2394 uint8_t *buf, int buf_size)
2396 Vp3DecodeContext *s = avctx->priv_data;
2398 static int counter = 0;
2402 init_get_bits(&gb, buf, buf_size * 8);
2404 s->keyframe = get_bits(&gb, 1);
2407 s->last_quality_index = s->quality_index;
2408 s->quality_index = get_bits(&gb, 6);
2410 debug_vp3(" VP3 frame #%d: Q index = %d", counter, s->quality_index);
2413 if (s->quality_index != s->last_quality_index)
2414 init_dequantizer(s);
2418 debug_vp3(", keyframe\n");
2419 /* skip the other 2 header bytes for now */
2422 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
2423 if (s->golden_frame.data[0])
2424 avctx->release_buffer(avctx, &s->golden_frame);
2426 if (s->golden_frame.data[0])
2427 avctx->release_buffer(avctx, &s->golden_frame);
2428 if (s->last_frame.data[0])
2429 avctx->release_buffer(avctx, &s->last_frame);
2432 s->golden_frame.reference = 0;
2433 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
2434 printf("vp3: get_buffer() failed\n");
2438 /* golden frame is also the current frame */
2439 memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
2441 /* time to figure out pixel addresses? */
2442 if (!s->pixel_addresses_inited)
2443 vp3_calculate_pixel_addresses(s);
2449 /* allocate a new current frame */
2450 s->current_frame.reference = 0;
2451 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
2452 printf("vp3: get_buffer() failed\n");
2462 memcpy(s->current_frame.data[0], s->golden_frame.data[0],
2463 s->current_frame.linesize[0] * s->height);
2464 memcpy(s->current_frame.data[1], s->golden_frame.data[1],
2465 s->current_frame.linesize[1] * s->height / 2);
2466 memcpy(s->current_frame.data[2], s->golden_frame.data[2],
2467 s->current_frame.linesize[2] * s->height / 2);
2472 if (unpack_superblocks(s, &gb) ||
2473 unpack_modes(s, &gb) ||
2474 unpack_vectors(s, &gb) ||
2475 unpack_dct_coeffs(s, &gb)) {
2477 printf(" vp3: could not decode frame\n");
2481 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
2482 reverse_dc_prediction(s, s->u_fragment_start,
2483 s->fragment_width / 2, s->fragment_height / 2);
2484 reverse_dc_prediction(s, s->v_fragment_start,
2485 s->fragment_width / 2, s->fragment_height / 2);
2487 render_fragments(s, 0, s->width, s->height, 0);
2489 render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
2490 render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
2492 memset(s->current_frame.data[1], 0x80, s->width * s->height / 4);
2493 memset(s->current_frame.data[2], 0x80, s->width * s->height / 4);
2500 *data_size=sizeof(AVFrame);
2501 *(AVFrame*)data= s->current_frame;
2503 /* release the last frame, if it is allocated and if it is not the
2505 if ((s->last_frame.data[0]) &&
2506 (s->last_frame.data[0] != s->golden_frame.data[0]))
2507 avctx->release_buffer(avctx, &s->last_frame);
2509 /* shuffle frames (last = current) */
2510 memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
2516 * This is the ffmpeg/libavcodec API module cleanup function.
2518 static int vp3_decode_end(AVCodecContext *avctx)
2520 Vp3DecodeContext *s = avctx->priv_data;
2522 av_free(s->all_fragments);
2523 av_free(s->coded_fragment_list);
2524 av_free(s->superblock_fragments);
2525 av_free(s->superblock_macroblocks);
2526 av_free(s->macroblock_fragments);
2527 av_free(s->macroblock_coding);
2529 /* release all frames */
2530 if (s->golden_frame.data[0])
2531 avctx->release_buffer(avctx, &s->golden_frame);
2532 if (s->last_frame.data[0])
2533 avctx->release_buffer(avctx, &s->last_frame);
2534 /* no need to release the current_frame since it will always be pointing
2535 * to the same frame as either the golden or last frame */
2540 AVCodec vp3_decoder = {
2544 sizeof(Vp3DecodeContext),