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)
20 * For more information about the VP3 coding process, visit:
21 * http://www.pcisys.net/~melanson/codecs/
27 * On2 VP3 Video Decoder
38 #include "mpegvideo.h"
43 #define FRAGMENT_PIXELS 8
48 * Define one or more of the following compile-time variables to 1 to obtain
49 * elaborate information about certain aspects of the decoding process.
51 * KEYFRAMES_ONLY: set this to 1 to only see keyframes (VP3 slideshow mode)
52 * DEBUG_VP3: high-level decoding flow
53 * DEBUG_INIT: initialization parameters
54 * DEBUG_DEQUANTIZERS: display how the dequanization tables are built
55 * DEBUG_BLOCK_CODING: unpacking the superblock/macroblock/fragment coding
56 * DEBUG_MODES: unpacking the coding modes for individual fragments
57 * DEBUG_VECTORS: display the motion vectors
58 * DEBUG_TOKEN: display exhaustive information about each DCT token
59 * DEBUG_VLC: display the VLCs as they are extracted from the stream
60 * DEBUG_DC_PRED: display the process of reversing DC prediction
61 * DEBUG_IDCT: show every detail of the IDCT process
64 #define KEYFRAMES_ONLY 0
68 #define DEBUG_DEQUANTIZERS 0
69 #define DEBUG_BLOCK_CODING 0
71 #define DEBUG_VECTORS 0
74 #define DEBUG_DC_PRED 0
78 #define debug_vp3 printf
80 static inline void debug_vp3(const char *format, ...) { }
84 #define debug_init printf
86 static inline void debug_init(const char *format, ...) { }
89 #if DEBUG_DEQUANTIZERS
90 #define debug_dequantizers printf
92 static inline void debug_dequantizers(const char *format, ...) { }
95 #if DEBUG_BLOCK_CODING
96 #define debug_block_coding printf
98 static inline void debug_block_coding(const char *format, ...) { }
102 #define debug_modes printf
104 static inline void debug_modes(const char *format, ...) { }
108 #define debug_vectors printf
110 static inline void debug_vectors(const char *format, ...) { }
114 #define debug_token printf
116 static inline void debug_token(const char *format, ...) { }
120 #define debug_vlc printf
122 static inline void debug_vlc(const char *format, ...) { }
126 #define debug_dc_pred printf
128 static inline void debug_dc_pred(const char *format, ...) { }
132 #define debug_idct printf
134 static inline void debug_idct(const char *format, ...) { }
137 typedef struct Vp3Fragment {
144 /* address of first pixel taking into account which plane the fragment
145 * lives on as well as the plane stride */
147 /* this is the macroblock that the fragment belongs to */
151 #define SB_NOT_CODED 0
152 #define SB_PARTIALLY_CODED 1
153 #define SB_FULLY_CODED 2
155 #define MODE_INTER_NO_MV 0
157 #define MODE_INTER_PLUS_MV 2
158 #define MODE_INTER_LAST_MV 3
159 #define MODE_INTER_PRIOR_LAST 4
160 #define MODE_USING_GOLDEN 5
161 #define MODE_GOLDEN_MV 6
162 #define MODE_INTER_FOURMV 7
163 #define CODING_MODE_COUNT 8
165 /* special internal mode */
168 /* There are 6 preset schemes, plus a free-form scheme */
169 static int ModeAlphabet[7][CODING_MODE_COUNT] =
171 /* this is the custom scheme */
172 { 0, 0, 0, 0, 0, 0, 0, 0 },
174 /* scheme 1: Last motion vector dominates */
175 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
176 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
177 MODE_INTRA, MODE_USING_GOLDEN,
178 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
181 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
182 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
183 MODE_INTRA, MODE_USING_GOLDEN,
184 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
187 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
188 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
189 MODE_INTRA, MODE_USING_GOLDEN,
190 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
193 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
194 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
195 MODE_INTRA, MODE_USING_GOLDEN,
196 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
198 /* scheme 5: No motion vector dominates */
199 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
200 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
201 MODE_INTRA, MODE_USING_GOLDEN,
202 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
205 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
206 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
207 MODE_INTER_PLUS_MV, MODE_INTRA,
208 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
212 #define MIN_DEQUANT_VAL 2
214 typedef struct Vp3DecodeContext {
215 AVCodecContext *avctx;
217 AVFrame golden_frame;
219 AVFrame current_frame;
224 int last_quality_index;
226 int superblock_count;
227 int superblock_width;
228 int superblock_height;
229 int y_superblock_width;
230 int y_superblock_height;
231 int c_superblock_width;
232 int c_superblock_height;
233 int u_superblock_start;
234 int v_superblock_start;
235 unsigned char *superblock_coding;
237 int macroblock_count;
238 int macroblock_width;
239 int macroblock_height;
245 Vp3Fragment *all_fragments;
246 int u_fragment_start;
247 int v_fragment_start;
249 /* this is a list of indices into the all_fragments array indicating
250 * which of the fragments are coded */
251 int *coded_fragment_list;
252 int coded_fragment_list_index;
253 int pixel_addresses_inited;
261 int16_t intra_y_dequant[64];
262 int16_t intra_c_dequant[64];
263 int16_t inter_dequant[64];
265 /* This table contains superblock_count * 16 entries. Each set of 16
266 * numbers corresponds to the fragment indices 0..15 of the superblock.
267 * An entry will be -1 to indicate that no entry corresponds to that
269 int *superblock_fragments;
271 /* This table contains superblock_count * 4 entries. Each set of 4
272 * numbers corresponds to the macroblock indices 0..3 of the superblock.
273 * An entry will be -1 to indicate that no entry corresponds to that
275 int *superblock_macroblocks;
277 /* This table contains macroblock_count * 6 entries. Each set of 6
278 * numbers corresponds to the fragment indices 0..5 which comprise
279 * the macroblock (4 Y fragments and 2 C fragments). */
280 int *macroblock_fragments;
281 /* This is an array that indicates how a particular macroblock
283 unsigned char *macroblock_coding;
285 int first_coded_y_fragment;
286 int first_coded_c_fragment;
287 int last_coded_y_fragment;
288 int last_coded_c_fragment;
290 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
291 uint8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
294 /************************************************************************
296 ************************************************************************/
298 #define IdctAdjustBeforeShift 8
307 void vp3_idct_c(int16_t *input_data, int16_t *dequant_matrix,
308 int16_t *output_data)
310 int32_t intermediate_data[64];
311 int32_t *ip = intermediate_data;
312 int16_t *op = output_data;
314 int32_t A_, B_, C_, D_, _Ad, _Bd, _Cd, _Dd, E_, F_, G_, H_;
315 int32_t _Ed, _Gd, _Add, _Bdd, _Fd, _Hd;
320 debug_idct("raw coefficient block:\n");
321 for (i = 0; i < 8; i++) {
322 for (j = 0; j < 8; j++) {
323 debug_idct(" %5d", input_data[i * 8 + j]);
329 for (i = 0; i < 64; i++) {
330 j = dezigzag_index[i];
331 intermediate_data[j] = dequant_matrix[i] * input_data[i];
334 debug_idct("dequantized block:\n");
335 for (i = 0; i < 8; i++) {
336 for (j = 0; j < 8; j++) {
337 debug_idct(" %5d", intermediate_data[i * 8 + j]);
343 /* Inverse DCT on the rows now */
344 for (i = 0; i < 8; i++) {
345 /* Check for non-zero values */
346 if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
347 t1 = (int32_t)(xC1S7 * ip[1]);
348 t2 = (int32_t)(xC7S1 * ip[7]);
353 t1 = (int32_t)(xC7S1 * ip[1]);
354 t2 = (int32_t)(xC1S7 * ip[7]);
359 t1 = (int32_t)(xC3S5 * ip[3]);
360 t2 = (int32_t)(xC5S3 * ip[5]);
365 t1 = (int32_t)(xC3S5 * ip[5]);
366 t2 = (int32_t)(xC5S3 * ip[3]);
372 t1 = (int32_t)(xC4S4 * (A_ - C_));
376 t1 = (int32_t)(xC4S4 * (B_ - D_));
384 t1 = (int32_t)(xC4S4 * (ip[0] + ip[4]));
388 t1 = (int32_t)(xC4S4 * (ip[0] - ip[4]));
392 t1 = (int32_t)(xC2S6 * ip[2]);
393 t2 = (int32_t)(xC6S2 * ip[6]);
398 t1 = (int32_t)(xC6S2 * ip[2]);
399 t2 = (int32_t)(xC2S6 * ip[6]);
414 /* Final sequence of operations over-write original inputs. */
415 ip[0] = (int16_t)((_Gd + _Cd ) >> 0);
416 ip[7] = (int16_t)((_Gd - _Cd ) >> 0);
418 ip[1] = (int16_t)((_Add + _Hd ) >> 0);
419 ip[2] = (int16_t)((_Add - _Hd ) >> 0);
421 ip[3] = (int16_t)((_Ed + _Dd ) >> 0);
422 ip[4] = (int16_t)((_Ed - _Dd ) >> 0);
424 ip[5] = (int16_t)((_Fd + _Bdd ) >> 0);
425 ip[6] = (int16_t)((_Fd - _Bdd ) >> 0);
429 ip += 8; /* next row */
432 ip = intermediate_data;
434 for ( i = 0; i < 8; i++) {
435 /* Check for non-zero values (bitwise or faster than ||) */
436 if ( ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
437 ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
439 t1 = (int32_t)(xC1S7 * ip[1*8]);
440 t2 = (int32_t)(xC7S1 * ip[7*8]);
445 t1 = (int32_t)(xC7S1 * ip[1*8]);
446 t2 = (int32_t)(xC1S7 * ip[7*8]);
451 t1 = (int32_t)(xC3S5 * ip[3*8]);
452 t2 = (int32_t)(xC5S3 * ip[5*8]);
457 t1 = (int32_t)(xC3S5 * ip[5*8]);
458 t2 = (int32_t)(xC5S3 * ip[3*8]);
464 t1 = (int32_t)(xC4S4 * (A_ - C_));
468 t1 = (int32_t)(xC4S4 * (B_ - D_));
476 t1 = (int32_t)(xC4S4 * (ip[0*8] + ip[4*8]));
480 t1 = (int32_t)(xC4S4 * (ip[0*8] - ip[4*8]));
484 t1 = (int32_t)(xC2S6 * ip[2*8]);
485 t2 = (int32_t)(xC6S2 * ip[6*8]);
490 t1 = (int32_t)(xC6S2 * ip[2*8]);
491 t2 = (int32_t)(xC2S6 * ip[6*8]);
506 _Gd += IdctAdjustBeforeShift;
507 _Add += IdctAdjustBeforeShift;
508 _Ed += IdctAdjustBeforeShift;
509 _Fd += IdctAdjustBeforeShift;
511 /* Final sequence of operations over-write original inputs. */
512 op[0*8] = (int16_t)((_Gd + _Cd ) >> 4);
513 op[7*8] = (int16_t)((_Gd - _Cd ) >> 4);
515 op[1*8] = (int16_t)((_Add + _Hd ) >> 4);
516 op[2*8] = (int16_t)((_Add - _Hd ) >> 4);
518 op[3*8] = (int16_t)((_Ed + _Dd ) >> 4);
519 op[4*8] = (int16_t)((_Ed - _Dd ) >> 4);
521 op[5*8] = (int16_t)((_Fd + _Bdd ) >> 4);
522 op[6*8] = (int16_t)((_Fd - _Bdd ) >> 4);
536 ip++; /* next column */
541 void vp3_idct_put(int16_t *input_data, int16_t *dequant_matrix,
542 uint8_t *dest, int stride)
544 int16_t transformed_data[64];
548 vp3_idct_c(input_data, dequant_matrix, transformed_data);
550 /* place in final output */
551 op = transformed_data;
552 for (i = 0; i < 8; i++) {
553 for (j = 0; j < 8; j++) {
559 *dest = (uint8_t)(*op + 128);
563 dest += (stride - 8);
567 void vp3_idct_add(int16_t *input_data, int16_t *dequant_matrix,
568 uint8_t *dest, int stride)
570 int16_t transformed_data[64];
575 vp3_idct_c(input_data, dequant_matrix, transformed_data);
577 /* place in final output */
578 op = transformed_data;
579 for (i = 0; i < 8; i++) {
580 for (j = 0; j < 8; j++) {
581 sample = *dest + *op;
584 else if (sample > 255)
587 *dest = (uint8_t)(sample & 0xFF);
591 dest += (stride - 8);
595 /************************************************************************
596 * VP3 specific functions
597 ************************************************************************/
600 * This function sets up all of the various blocks mappings:
601 * superblocks <-> fragments, macroblocks <-> fragments,
602 * superblocks <-> macroblocks
604 * Returns 0 is successful; returns 1 if *anything* went wrong.
606 static int init_block_mapping(Vp3DecodeContext *s)
609 signed int hilbert_walk_y[16];
610 signed int hilbert_walk_c[16];
611 signed int hilbert_walk_mb[4];
613 int current_fragment = 0;
614 int current_width = 0;
615 int current_height = 0;
618 int superblock_row_inc = 0;
620 int mapping_index = 0;
622 int current_macroblock;
625 signed char travel_width[16] = {
632 signed char travel_height[16] = {
639 signed char travel_width_mb[4] = {
643 signed char travel_height_mb[4] = {
647 debug_vp3(" vp3: initialize block mapping tables\n");
649 /* figure out hilbert pattern per these frame dimensions */
650 hilbert_walk_y[0] = 1;
651 hilbert_walk_y[1] = 1;
652 hilbert_walk_y[2] = s->fragment_width;
653 hilbert_walk_y[3] = -1;
654 hilbert_walk_y[4] = s->fragment_width;
655 hilbert_walk_y[5] = s->fragment_width;
656 hilbert_walk_y[6] = 1;
657 hilbert_walk_y[7] = -s->fragment_width;
658 hilbert_walk_y[8] = 1;
659 hilbert_walk_y[9] = s->fragment_width;
660 hilbert_walk_y[10] = 1;
661 hilbert_walk_y[11] = -s->fragment_width;
662 hilbert_walk_y[12] = -s->fragment_width;
663 hilbert_walk_y[13] = -1;
664 hilbert_walk_y[14] = -s->fragment_width;
665 hilbert_walk_y[15] = 1;
667 hilbert_walk_c[0] = 1;
668 hilbert_walk_c[1] = 1;
669 hilbert_walk_c[2] = s->fragment_width / 2;
670 hilbert_walk_c[3] = -1;
671 hilbert_walk_c[4] = s->fragment_width / 2;
672 hilbert_walk_c[5] = s->fragment_width / 2;
673 hilbert_walk_c[6] = 1;
674 hilbert_walk_c[7] = -s->fragment_width / 2;
675 hilbert_walk_c[8] = 1;
676 hilbert_walk_c[9] = s->fragment_width / 2;
677 hilbert_walk_c[10] = 1;
678 hilbert_walk_c[11] = -s->fragment_width / 2;
679 hilbert_walk_c[12] = -s->fragment_width / 2;
680 hilbert_walk_c[13] = -1;
681 hilbert_walk_c[14] = -s->fragment_width / 2;
682 hilbert_walk_c[15] = 1;
684 hilbert_walk_mb[0] = 1;
685 hilbert_walk_mb[1] = s->macroblock_width;
686 hilbert_walk_mb[2] = 1;
687 hilbert_walk_mb[3] = -s->macroblock_width;
689 /* iterate through each superblock (all planes) and map the fragments */
690 for (i = 0; i < s->superblock_count; i++) {
691 debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
692 i, s->u_superblock_start, s->v_superblock_start);
694 /* time to re-assign the limits? */
697 /* start of Y superblocks */
698 right_edge = s->fragment_width;
699 bottom_edge = s->fragment_height;
702 superblock_row_inc = 3 * s->fragment_width -
703 (s->y_superblock_width * 4 - s->fragment_width);
704 hilbert = hilbert_walk_y;
706 /* the first operation for this variable is to advance by 1 */
707 current_fragment = -1;
709 } else if (i == s->u_superblock_start) {
711 /* start of U superblocks */
712 right_edge = s->fragment_width / 2;
713 bottom_edge = s->fragment_height / 2;
716 superblock_row_inc = 3 * (s->fragment_width / 2) -
717 (s->c_superblock_width * 4 - s->fragment_width / 2);
718 hilbert = hilbert_walk_c;
720 /* the first operation for this variable is to advance by 1 */
721 current_fragment = s->u_fragment_start - 1;
723 } else if (i == s->v_superblock_start) {
725 /* start of V superblocks */
726 right_edge = s->fragment_width / 2;
727 bottom_edge = s->fragment_height / 2;
730 superblock_row_inc = 3 * (s->fragment_width / 2) -
731 (s->c_superblock_width * 4 - s->fragment_width / 2);
732 hilbert = hilbert_walk_c;
734 /* the first operation for this variable is to advance by 1 */
735 current_fragment = s->v_fragment_start - 1;
739 if (current_width >= right_edge - 1) {
740 /* reset width and move to next superblock row */
744 /* fragment is now at the start of a new superblock row */
745 current_fragment += superblock_row_inc;
748 /* iterate through all 16 fragments in a superblock */
749 for (j = 0; j < 16; j++) {
750 current_fragment += hilbert[j];
751 current_width += travel_width[j];
752 current_height += travel_height[j];
754 /* check if the fragment is in bounds */
755 if ((current_width < right_edge) &&
756 (current_height < bottom_edge)) {
757 s->superblock_fragments[mapping_index] = current_fragment;
758 debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
759 s->superblock_fragments[mapping_index], i, j,
760 current_width, right_edge, current_height, bottom_edge);
762 s->superblock_fragments[mapping_index] = -1;
763 debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
765 current_width, right_edge, current_height, bottom_edge);
772 /* initialize the superblock <-> macroblock mapping; iterate through
773 * all of the Y plane superblocks to build this mapping */
774 right_edge = s->macroblock_width;
775 bottom_edge = s->macroblock_height;
778 superblock_row_inc = s->macroblock_width -
779 (s->y_superblock_width * 2 - s->macroblock_width);;
780 hilbert = hilbert_walk_mb;
782 current_macroblock = -1;
783 for (i = 0; i < s->u_superblock_start; i++) {
785 if (current_width >= right_edge - 1) {
786 /* reset width and move to next superblock row */
790 /* macroblock is now at the start of a new superblock row */
791 current_macroblock += superblock_row_inc;
794 /* iterate through each potential macroblock in the superblock */
795 for (j = 0; j < 4; j++) {
796 current_macroblock += hilbert_walk_mb[j];
797 current_width += travel_width_mb[j];
798 current_height += travel_height_mb[j];
800 /* check if the macroblock is in bounds */
801 if ((current_width < right_edge) &&
802 (current_height < bottom_edge)) {
803 s->superblock_macroblocks[mapping_index] = current_macroblock;
804 debug_init(" mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
805 s->superblock_macroblocks[mapping_index], i, j,
806 current_width, right_edge, current_height, bottom_edge);
808 s->superblock_macroblocks[mapping_index] = -1;
809 debug_init(" superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
811 current_width, right_edge, current_height, bottom_edge);
818 /* initialize the macroblock <-> fragment mapping */
819 current_fragment = 0;
820 current_macroblock = 0;
822 for (i = 0; i < s->fragment_height; i += 2) {
824 for (j = 0; j < s->fragment_width; j += 2) {
826 debug_init(" macroblock %d contains fragments: ", current_macroblock);
827 s->all_fragments[current_fragment].macroblock = current_macroblock;
828 s->macroblock_fragments[mapping_index++] = current_fragment;
829 debug_init("%d ", current_fragment);
831 if (j + 1 < s->fragment_width) {
832 s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
833 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
834 debug_init("%d ", current_fragment + 1);
836 s->macroblock_fragments[mapping_index++] = -1;
838 if (i + 1 < s->fragment_height) {
839 s->all_fragments[current_fragment + s->fragment_width].macroblock =
841 s->macroblock_fragments[mapping_index++] =
842 current_fragment + s->fragment_width;
843 debug_init("%d ", current_fragment + s->fragment_width);
845 s->macroblock_fragments[mapping_index++] = -1;
847 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
848 s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
850 s->macroblock_fragments[mapping_index++] =
851 current_fragment + s->fragment_width + 1;
852 debug_init("%d ", current_fragment + s->fragment_width + 1);
854 s->macroblock_fragments[mapping_index++] = -1;
857 c_fragment = s->u_fragment_start +
858 (i * s->fragment_width / 4) + (j / 2);
859 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
860 s->macroblock_fragments[mapping_index++] = c_fragment;
861 debug_init("%d ", c_fragment);
863 c_fragment = s->v_fragment_start +
864 (i * s->fragment_width / 4) + (j / 2);
865 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
866 s->macroblock_fragments[mapping_index++] = c_fragment;
867 debug_init("%d ", c_fragment);
871 if (j + 2 <= s->fragment_width)
872 current_fragment += 2;
875 current_macroblock++;
878 current_fragment += s->fragment_width;
881 return 0; /* successful path out */
885 * This function unpacks a single token (which should be in the range 0..31)
886 * and returns a zero run (number of zero coefficients in current DCT matrix
887 * before next non-zero coefficient), the next DCT coefficient, and the
888 * number of consecutive, non-EOB'd DCT blocks to EOB.
890 static void unpack_token(GetBitContext *gb, int token, int *zero_run,
891 DCTELEM *coeff, int *eob_run)
899 debug_token(" vp3 token %d: ", token);
903 debug_token("DCT_EOB_TOKEN, EOB next block\n");
908 debug_token("DCT_EOB_PAIR_TOKEN, EOB next 2 blocks\n");
913 debug_token("DCT_EOB_TRIPLE_TOKEN, EOB next 3 blocks\n");
918 debug_token("DCT_REPEAT_RUN_TOKEN, ");
919 *eob_run = get_bits(gb, 2) + 4;
920 debug_token("EOB the next %d blocks\n", *eob_run);
924 debug_token("DCT_REPEAT_RUN2_TOKEN, ");
925 *eob_run = get_bits(gb, 3) + 8;
926 debug_token("EOB the next %d blocks\n", *eob_run);
930 debug_token("DCT_REPEAT_RUN3_TOKEN, ");
931 *eob_run = get_bits(gb, 4) + 16;
932 debug_token("EOB the next %d blocks\n", *eob_run);
936 debug_token("DCT_REPEAT_RUN4_TOKEN, ");
937 *eob_run = get_bits(gb, 12);
938 debug_token("EOB the next %d blocks\n", *eob_run);
942 debug_token("DCT_SHORT_ZRL_TOKEN, ");
943 /* note that this token actually indicates that (3 extra bits) + 1 0s
944 * should be output; this case specifies a run of (3 EBs) 0s and a
945 * coefficient of 0. */
946 *zero_run = get_bits(gb, 3);
948 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
952 debug_token("DCT_ZRL_TOKEN, ");
953 /* note that this token actually indicates that (6 extra bits) + 1 0s
954 * should be output; this case specifies a run of (6 EBs) 0s and a
955 * coefficient of 0. */
956 *zero_run = get_bits(gb, 6);
958 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
962 debug_token("ONE_TOKEN, output 1\n");
967 debug_token("MINUS_ONE_TOKEN, output -1\n");
972 debug_token("TWO_TOKEN, output 2\n");
977 debug_token("MINUS_TWO_TOKEN, output -2\n");
985 debug_token("LOW_VAL_TOKENS, ");
987 *coeff = -(3 + (token - 13));
989 *coeff = 3 + (token - 13);
990 debug_token("output %d\n", *coeff);
994 debug_token("DCT_VAL_CATEGORY3, ");
995 sign = get_bits(gb, 1);
996 *coeff = 7 + get_bits(gb, 1);
999 debug_token("output %d\n", *coeff);
1003 debug_token("DCT_VAL_CATEGORY4, ");
1004 sign = get_bits(gb, 1);
1005 *coeff = 9 + get_bits(gb, 2);
1008 debug_token("output %d\n", *coeff);
1012 debug_token("DCT_VAL_CATEGORY5, ");
1013 sign = get_bits(gb, 1);
1014 *coeff = 13 + get_bits(gb, 3);
1017 debug_token("output %d\n", *coeff);
1021 debug_token("DCT_VAL_CATEGORY6, ");
1022 sign = get_bits(gb, 1);
1023 *coeff = 21 + get_bits(gb, 4);
1026 debug_token("output %d\n", *coeff);
1030 debug_token("DCT_VAL_CATEGORY7, ");
1031 sign = get_bits(gb, 1);
1032 *coeff = 37 + get_bits(gb, 5);
1035 debug_token("output %d\n", *coeff);
1039 debug_token("DCT_VAL_CATEGORY8, ");
1040 sign = get_bits(gb, 1);
1041 *coeff = 69 + get_bits(gb, 9);
1044 debug_token("output %d\n", *coeff);
1052 debug_token("DCT_RUN_CATEGORY1, ");
1053 *zero_run = token - 22;
1054 if (get_bits(gb, 1))
1058 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1062 debug_token("DCT_RUN_CATEGORY1B, ");
1063 if (get_bits(gb, 1))
1067 *zero_run = 6 + get_bits(gb, 2);
1068 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1072 debug_token("DCT_RUN_CATEGORY1C, ");
1073 if (get_bits(gb, 1))
1077 *zero_run = 10 + get_bits(gb, 3);
1078 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1082 debug_token("DCT_RUN_CATEGORY2, ");
1083 sign = get_bits(gb, 1);
1084 *coeff = 2 + get_bits(gb, 1);
1088 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1092 debug_token("DCT_RUN_CATEGORY2, ");
1093 sign = get_bits(gb, 1);
1094 *coeff = 2 + get_bits(gb, 1);
1097 *zero_run = 2 + get_bits(gb, 1);
1098 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1102 printf (" vp3: help! Got a bad token: %d > 31\n", token);
1109 * This function wipes out all of the fragment data.
1111 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
1115 /* zero out all of the fragment information */
1116 s->coded_fragment_list_index = 0;
1117 for (i = 0; i < s->fragment_count; i++) {
1118 memset(s->all_fragments[i].coeffs, 0, 64 * sizeof(DCTELEM));
1119 s->all_fragments[i].coeff_count = 0;
1120 s->all_fragments[i].last_coeff = 0;
1121 s->all_fragments[i].motion_x = 0xbeef;
1122 s->all_fragments[i].motion_y = 0xbeef;
1127 * This function sets of the dequantization tables used for a particular
1130 static void init_dequantizer(Vp3DecodeContext *s)
1133 int quality_scale = vp31_quality_threshold[s->quality_index];
1134 int dc_scale_factor = vp31_dc_scale_factor[s->quality_index];
1137 debug_vp3(" vp3: initializing dequantization tables\n");
1140 * Scale dequantizers:
1146 * where sf = dc_scale_factor for DC quantizer
1147 * or quality_scale for AC quantizer
1149 * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL.
1153 /* scale DC quantizers */
1154 s->intra_y_dequant[0] = vp31_intra_y_dequant[0] * dc_scale_factor / 100;
1155 if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2)
1156 s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2;
1157 s->intra_y_dequant[0] *= SCALER;
1159 s->intra_c_dequant[0] = vp31_intra_c_dequant[0] * dc_scale_factor / 100;
1160 if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2)
1161 s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2;
1162 s->intra_c_dequant[0] *= SCALER;
1164 s->inter_dequant[0] = vp31_inter_dequant[0] * dc_scale_factor / 100;
1165 if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4)
1166 s->inter_dequant[0] = MIN_DEQUANT_VAL * 4;
1167 s->inter_dequant[0] *= SCALER;
1169 /* scale AC quantizers, zigzag at the same time in preparation for
1170 * the dequantization phase */
1171 for (i = 1; i < 64; i++) {
1173 j = zigzag_index[i];
1175 s->intra_y_dequant[j] = vp31_intra_y_dequant[i] * quality_scale / 100;
1176 if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL)
1177 s->intra_y_dequant[j] = MIN_DEQUANT_VAL;
1178 s->intra_y_dequant[j] *= SCALER;
1180 s->intra_c_dequant[j] = vp31_intra_c_dequant[i] * quality_scale / 100;
1181 if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL)
1182 s->intra_c_dequant[j] = MIN_DEQUANT_VAL;
1183 s->intra_c_dequant[j] *= SCALER;
1185 s->inter_dequant[j] = vp31_inter_dequant[i] * quality_scale / 100;
1186 if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2)
1187 s->inter_dequant[j] = MIN_DEQUANT_VAL * 2;
1188 s->inter_dequant[j] *= SCALER;
1191 memset(s->qscale_table, (FFMAX(s->intra_y_dequant[1], s->intra_c_dequant[1])+8)/16, 512); //FIXME finetune
1193 /* print debug information as requested */
1194 debug_dequantizers("intra Y dequantizers:\n");
1195 for (i = 0; i < 8; i++) {
1196 for (j = i * 8; j < i * 8 + 8; j++) {
1197 debug_dequantizers(" %4d,", s->intra_y_dequant[j]);
1199 debug_dequantizers("\n");
1201 debug_dequantizers("\n");
1203 debug_dequantizers("intra C dequantizers:\n");
1204 for (i = 0; i < 8; i++) {
1205 for (j = i * 8; j < i * 8 + 8; j++) {
1206 debug_dequantizers(" %4d,", s->intra_c_dequant[j]);
1208 debug_dequantizers("\n");
1210 debug_dequantizers("\n");
1212 debug_dequantizers("interframe dequantizers:\n");
1213 for (i = 0; i < 8; i++) {
1214 for (j = i * 8; j < i * 8 + 8; j++) {
1215 debug_dequantizers(" %4d,", s->inter_dequant[j]);
1217 debug_dequantizers("\n");
1219 debug_dequantizers("\n");
1223 * This function is used to fetch runs of 1s or 0s from the bitstream for
1224 * use in determining which superblocks are fully and partially coded.
1226 * Codeword RunLength
1233 * 111111xxxxxxxxxxxx 34-4129
1235 static int get_superblock_run_length(GetBitContext *gb)
1238 if (get_bits(gb, 1) == 0)
1241 else if (get_bits(gb, 1) == 0)
1242 return (2 + get_bits(gb, 1));
1244 else if (get_bits(gb, 1) == 0)
1245 return (4 + get_bits(gb, 1));
1247 else if (get_bits(gb, 1) == 0)
1248 return (6 + get_bits(gb, 2));
1250 else if (get_bits(gb, 1) == 0)
1251 return (10 + get_bits(gb, 3));
1253 else if (get_bits(gb, 1) == 0)
1254 return (18 + get_bits(gb, 4));
1257 return (34 + get_bits(gb, 12));
1262 * This function is used to fetch runs of 1s or 0s from the bitstream for
1263 * use in determining which particular fragments are coded.
1265 * Codeword RunLength
1273 static int get_fragment_run_length(GetBitContext *gb)
1276 if (get_bits(gb, 1) == 0)
1277 return (1 + get_bits(gb, 1));
1279 else if (get_bits(gb, 1) == 0)
1280 return (3 + get_bits(gb, 1));
1282 else if (get_bits(gb, 1) == 0)
1283 return (5 + get_bits(gb, 1));
1285 else if (get_bits(gb, 1) == 0)
1286 return (7 + get_bits(gb, 2));
1288 else if (get_bits(gb, 1) == 0)
1289 return (11 + get_bits(gb, 2));
1292 return (15 + get_bits(gb, 4));
1297 * This function decodes a VLC from the bitstream and returns a number
1298 * that ranges from 0..7. The number indicates which of the 8 coding
1312 static int get_mode_code(GetBitContext *gb)
1315 if (get_bits(gb, 1) == 0)
1318 else if (get_bits(gb, 1) == 0)
1321 else if (get_bits(gb, 1) == 0)
1324 else if (get_bits(gb, 1) == 0)
1327 else if (get_bits(gb, 1) == 0)
1330 else if (get_bits(gb, 1) == 0)
1333 else if (get_bits(gb, 1) == 0)
1342 * This function extracts a motion vector from the bitstream using a VLC
1343 * scheme. 3 bits are fetched from the bitstream and 1 of 8 actions is
1344 * taken depending on the value on those 3 bits:
1349 * 3: if (next bit is 1) return -2, else return 2
1350 * 4: if (next bit is 1) return -3, else return 3
1351 * 5: return 4 + (next 2 bits), next bit is sign
1352 * 6: return 8 + (next 3 bits), next bit is sign
1353 * 7: return 16 + (next 4 bits), next bit is sign
1355 static int get_motion_vector_vlc(GetBitContext *gb)
1359 bits = get_bits(gb, 3);
1376 if (get_bits(gb, 1) == 0)
1383 if (get_bits(gb, 1) == 0)
1390 bits = 4 + get_bits(gb, 2);
1391 if (get_bits(gb, 1) == 1)
1396 bits = 8 + get_bits(gb, 3);
1397 if (get_bits(gb, 1) == 1)
1402 bits = 16 + get_bits(gb, 4);
1403 if (get_bits(gb, 1) == 1)
1413 * This function fetches a 5-bit number from the stream followed by
1414 * a sign and calls it a motion vector.
1416 static int get_motion_vector_fixed(GetBitContext *gb)
1421 bits = get_bits(gb, 5);
1423 if (get_bits(gb, 1) == 1)
1430 * This function unpacks all of the superblock/macroblock/fragment coding
1431 * information from the bitstream.
1433 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
1436 int current_superblock = 0;
1437 int current_run = 0;
1438 int decode_fully_flags = 0;
1439 int decode_partial_blocks = 0;
1440 int first_c_fragment_seen;
1443 int current_fragment;
1445 debug_vp3(" vp3: unpacking superblock coding\n");
1449 debug_vp3(" keyframe-- all superblocks are fully coded\n");
1450 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
1454 /* unpack the list of partially-coded superblocks */
1455 bit = get_bits(gb, 1);
1456 /* toggle the bit because as soon as the first run length is
1457 * fetched the bit will be toggled again */
1459 while (current_superblock < s->superblock_count) {
1460 if (current_run == 0) {
1462 current_run = get_superblock_run_length(gb);
1463 debug_block_coding(" setting superblocks %d..%d to %s\n",
1465 current_superblock + current_run - 1,
1466 (bit) ? "partially coded" : "not coded");
1468 /* if any of the superblocks are not partially coded, flag
1469 * a boolean to decode the list of fully-coded superblocks */
1471 decode_fully_flags = 1;
1474 /* make a note of the fact that there are partially coded
1476 decode_partial_blocks = 1;
1479 s->superblock_coding[current_superblock++] =
1480 (bit) ? SB_PARTIALLY_CODED : SB_NOT_CODED;
1484 /* unpack the list of fully coded superblocks if any of the blocks were
1485 * not marked as partially coded in the previous step */
1486 if (decode_fully_flags) {
1488 current_superblock = 0;
1490 bit = get_bits(gb, 1);
1491 /* toggle the bit because as soon as the first run length is
1492 * fetched the bit will be toggled again */
1494 while (current_superblock < s->superblock_count) {
1496 /* skip any superblocks already marked as partially coded */
1497 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
1499 if (current_run == 0) {
1501 current_run = get_superblock_run_length(gb);
1504 debug_block_coding(" setting superblock %d to %s\n",
1506 (bit) ? "fully coded" : "not coded");
1507 s->superblock_coding[current_superblock] =
1508 (bit) ? SB_FULLY_CODED : SB_NOT_CODED;
1511 current_superblock++;
1515 /* if there were partial blocks, initialize bitstream for
1516 * unpacking fragment codings */
1517 if (decode_partial_blocks) {
1520 bit = get_bits(gb, 1);
1521 /* toggle the bit because as soon as the first run length is
1522 * fetched the bit will be toggled again */
1527 /* figure out which fragments are coded; iterate through each
1528 * superblock (all planes) */
1529 s->coded_fragment_list_index = 0;
1530 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
1531 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
1532 first_c_fragment_seen = 0;
1533 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
1534 for (i = 0; i < s->superblock_count; i++) {
1536 /* iterate through all 16 fragments in a superblock */
1537 for (j = 0; j < 16; j++) {
1539 /* if the fragment is in bounds, check its coding status */
1540 current_fragment = s->superblock_fragments[i * 16 + j];
1541 if (current_fragment >= s->fragment_count) {
1542 printf (" vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
1543 current_fragment, s->fragment_count);
1546 if (current_fragment != -1) {
1547 if (s->superblock_coding[i] == SB_NOT_CODED) {
1549 /* copy all the fragments from the prior frame */
1550 s->all_fragments[current_fragment].coding_method =
1553 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
1555 /* fragment may or may not be coded; this is the case
1556 * that cares about the fragment coding runs */
1557 if (current_run == 0) {
1559 current_run = get_fragment_run_length(gb);
1563 /* default mode; actual mode will be decoded in
1565 s->all_fragments[current_fragment].coding_method =
1567 s->coded_fragment_list[s->coded_fragment_list_index] =
1569 if ((current_fragment >= s->u_fragment_start) &&
1570 (s->last_coded_y_fragment == -1) &&
1571 (!first_c_fragment_seen)) {
1572 s->first_coded_c_fragment = s->coded_fragment_list_index;
1573 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1574 first_c_fragment_seen = 1;
1576 s->coded_fragment_list_index++;
1577 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1578 debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
1579 i, current_fragment);
1581 /* not coded; copy this fragment from the prior frame */
1582 s->all_fragments[current_fragment].coding_method =
1584 debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
1585 i, current_fragment);
1592 /* fragments are fully coded in this superblock; actual
1593 * coding will be determined in next step */
1594 s->all_fragments[current_fragment].coding_method =
1596 s->coded_fragment_list[s->coded_fragment_list_index] =
1598 if ((current_fragment >= s->u_fragment_start) &&
1599 (s->last_coded_y_fragment == -1) &&
1600 (!first_c_fragment_seen)) {
1601 s->first_coded_c_fragment = s->coded_fragment_list_index;
1602 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1603 first_c_fragment_seen = 1;
1605 s->coded_fragment_list_index++;
1606 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1607 debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
1608 i, current_fragment);
1614 if (!first_c_fragment_seen)
1615 /* only Y fragments coded in this frame */
1616 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
1618 /* end the list of coded C fragments */
1619 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
1621 debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
1622 s->coded_fragment_list_index,
1623 s->first_coded_y_fragment,
1624 s->last_coded_y_fragment,
1625 s->first_coded_c_fragment,
1626 s->last_coded_c_fragment);
1632 * This function unpacks all the coding mode data for individual macroblocks
1633 * from the bitstream.
1635 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
1639 int current_macroblock;
1640 int current_fragment;
1643 debug_vp3(" vp3: unpacking encoding modes\n");
1646 debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
1648 for (i = 0; i < s->fragment_count; i++)
1649 s->all_fragments[i].coding_method = MODE_INTRA;
1653 /* fetch the mode coding scheme for this frame */
1654 scheme = get_bits(gb, 3);
1655 debug_modes(" using mode alphabet %d\n", scheme);
1657 /* is it a custom coding scheme? */
1659 debug_modes(" custom mode alphabet ahead:\n");
1660 for (i = 0; i < 8; i++)
1661 ModeAlphabet[scheme][get_bits(gb, 3)] = i;
1664 for (i = 0; i < 8; i++)
1665 debug_modes(" mode[%d][%d] = %d\n", scheme, i,
1666 ModeAlphabet[scheme][i]);
1668 /* iterate through all of the macroblocks that contain 1 or more
1669 * coded fragments */
1670 for (i = 0; i < s->u_superblock_start; i++) {
1672 for (j = 0; j < 4; j++) {
1673 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1674 if ((current_macroblock == -1) ||
1675 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1677 if (current_macroblock >= s->macroblock_count) {
1678 printf (" vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
1679 current_macroblock, s->macroblock_count);
1683 /* mode 7 means get 3 bits for each coding mode */
1685 coding_mode = get_bits(gb, 3);
1687 coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
1689 s->macroblock_coding[current_macroblock] = coding_mode;
1690 for (k = 0; k < 6; k++) {
1692 s->macroblock_fragments[current_macroblock * 6 + k];
1693 if (current_fragment == -1)
1695 if (current_fragment >= s->fragment_count) {
1696 printf (" vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
1697 current_fragment, s->fragment_count);
1700 if (s->all_fragments[current_fragment].coding_method !=
1702 s->all_fragments[current_fragment].coding_method =
1706 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
1707 s->macroblock_fragments[current_macroblock * 6], coding_mode);
1716 * This function unpacks all the motion vectors for the individual
1717 * macroblocks from the bitstream.
1719 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
1725 int last_motion_x = 0;
1726 int last_motion_y = 0;
1727 int prior_last_motion_x = 0;
1728 int prior_last_motion_y = 0;
1729 int current_macroblock;
1730 int current_fragment;
1732 debug_vp3(" vp3: unpacking motion vectors\n");
1735 debug_vp3(" keyframe-- there are no motion vectors\n");
1739 memset(motion_x, 0, 6 * sizeof(int));
1740 memset(motion_y, 0, 6 * sizeof(int));
1742 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
1743 coding_mode = get_bits(gb, 1);
1744 debug_vectors(" using %s scheme for unpacking motion vectors\n",
1745 (coding_mode == 0) ? "VLC" : "fixed-length");
1747 /* iterate through all of the macroblocks that contain 1 or more
1748 * coded fragments */
1749 for (i = 0; i < s->u_superblock_start; i++) {
1751 for (j = 0; j < 4; j++) {
1752 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1753 if ((current_macroblock == -1) ||
1754 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1756 if (current_macroblock >= s->macroblock_count) {
1757 printf (" vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
1758 current_macroblock, s->macroblock_count);
1762 current_fragment = s->macroblock_fragments[current_macroblock * 6];
1763 if (current_fragment >= s->fragment_count) {
1764 printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
1765 current_fragment, s->fragment_count);
1768 switch (s->macroblock_coding[current_macroblock]) {
1770 case MODE_INTER_PLUS_MV:
1771 case MODE_GOLDEN_MV:
1772 /* all 6 fragments use the same motion vector */
1773 if (coding_mode == 0) {
1774 motion_x[0] = get_motion_vector_vlc(gb);
1775 motion_y[0] = get_motion_vector_vlc(gb);
1777 motion_x[0] = get_motion_vector_fixed(gb);
1778 motion_y[0] = get_motion_vector_fixed(gb);
1780 for (k = 1; k < 6; k++) {
1781 motion_x[k] = motion_x[0];
1782 motion_y[k] = motion_y[0];
1785 /* vector maintenance, only on MODE_INTER_PLUS_MV */
1786 if (s->macroblock_coding[current_macroblock] ==
1787 MODE_INTER_PLUS_MV) {
1788 prior_last_motion_x = last_motion_x;
1789 prior_last_motion_y = last_motion_y;
1790 last_motion_x = motion_x[0];
1791 last_motion_y = motion_y[0];
1795 case MODE_INTER_FOURMV:
1796 /* fetch 4 vectors from the bitstream, one for each
1797 * Y fragment, then average for the C fragment vectors */
1798 motion_x[4] = motion_y[4] = 0;
1799 for (k = 0; k < 4; k++) {
1800 if (coding_mode == 0) {
1801 motion_x[k] = get_motion_vector_vlc(gb);
1802 motion_y[k] = get_motion_vector_vlc(gb);
1804 motion_x[k] = get_motion_vector_fixed(gb);
1805 motion_y[k] = get_motion_vector_fixed(gb);
1807 motion_x[4] += motion_x[k];
1808 motion_y[4] += motion_y[k];
1811 if (motion_x[4] >= 0)
1812 motion_x[4] = (motion_x[4] + 2) / 4;
1814 motion_x[4] = (motion_x[4] - 2) / 4;
1815 motion_x[5] = motion_x[4];
1817 if (motion_y[4] >= 0)
1818 motion_y[4] = (motion_y[4] + 2) / 4;
1820 motion_y[4] = (motion_y[4] - 2) / 4;
1821 motion_y[5] = motion_y[4];
1823 /* vector maintenance; vector[3] is treated as the
1824 * last vector in this case */
1825 prior_last_motion_x = last_motion_x;
1826 prior_last_motion_y = last_motion_y;
1827 last_motion_x = motion_x[3];
1828 last_motion_y = motion_y[3];
1831 case MODE_INTER_LAST_MV:
1832 /* all 6 fragments use the last motion vector */
1833 motion_x[0] = last_motion_x;
1834 motion_y[0] = last_motion_y;
1835 for (k = 1; k < 6; k++) {
1836 motion_x[k] = motion_x[0];
1837 motion_y[k] = motion_y[0];
1840 /* no vector maintenance (last vector remains the
1844 case MODE_INTER_PRIOR_LAST:
1845 /* all 6 fragments use the motion vector prior to the
1846 * last motion vector */
1847 motion_x[0] = prior_last_motion_x;
1848 motion_y[0] = prior_last_motion_y;
1849 for (k = 1; k < 6; k++) {
1850 motion_x[k] = motion_x[0];
1851 motion_y[k] = motion_y[0];
1854 /* vector maintenance */
1855 prior_last_motion_x = last_motion_x;
1856 prior_last_motion_y = last_motion_y;
1857 last_motion_x = motion_x[0];
1858 last_motion_y = motion_y[0];
1862 /* covers intra, inter without MV, golden without MV */
1863 memset(motion_x, 0, 6 * sizeof(int));
1864 memset(motion_y, 0, 6 * sizeof(int));
1866 /* no vector maintenance */
1870 /* assign the motion vectors to the correct fragments */
1871 debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
1873 s->macroblock_coding[current_macroblock]);
1874 for (k = 0; k < 6; k++) {
1876 s->macroblock_fragments[current_macroblock * 6 + k];
1877 if (current_fragment == -1)
1879 if (current_fragment >= s->fragment_count) {
1880 printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
1881 current_fragment, s->fragment_count);
1884 s->all_fragments[current_fragment].motion_x = motion_x[k];
1885 s->all_fragments[current_fragment].motion_y = motion_y[k];
1886 debug_vectors(" vector %d: fragment %d = (%d, %d)\n",
1887 k, current_fragment, motion_x[k], motion_y[k]);
1897 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1898 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1899 * data. This function unpacks all the VLCs for either the Y plane or both
1900 * C planes, and is called for DC coefficients or different AC coefficient
1901 * levels (since different coefficient types require different VLC tables.
1903 * This function returns a residual eob run. E.g, if a particular token gave
1904 * instructions to EOB the next 5 fragments and there were only 2 fragments
1905 * left in the current fragment range, 3 would be returned so that it could
1906 * be passed into the next call to this same function.
1908 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1909 VLC *table, int coeff_index,
1910 int first_fragment, int last_fragment,
1917 Vp3Fragment *fragment;
1919 if ((first_fragment >= s->fragment_count) ||
1920 (last_fragment >= s->fragment_count)) {
1922 printf (" vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
1923 first_fragment, last_fragment);
1927 for (i = first_fragment; i <= last_fragment; i++) {
1929 fragment = &s->all_fragments[s->coded_fragment_list[i]];
1930 if (fragment->coeff_count > coeff_index)
1934 /* decode a VLC into a token */
1935 token = get_vlc2(gb, table->table, 5, 3);
1936 debug_vlc(" token = %2d, ", token);
1937 /* use the token to get a zero run, a coefficient, and an eob run */
1938 unpack_token(gb, token, &zero_run, &coeff, &eob_run);
1942 fragment->coeff_count += zero_run;
1943 if (fragment->coeff_count < 64)
1944 fragment->coeffs[fragment->coeff_count++] = coeff;
1945 debug_vlc(" fragment %d coeff = %d\n",
1946 s->coded_fragment_list[i], fragment->coeffs[coeff_index]);
1948 fragment->last_coeff = fragment->coeff_count;
1949 fragment->coeff_count = 64;
1950 debug_vlc(" fragment %d eob with %d coefficients\n",
1951 s->coded_fragment_list[i], fragment->last_coeff);
1960 * This function unpacks all of the DCT coefficient data from the
1963 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1970 int residual_eob_run = 0;
1972 /* fetch the DC table indices */
1973 dc_y_table = get_bits(gb, 4);
1974 dc_c_table = get_bits(gb, 4);
1976 /* unpack the Y plane DC coefficients */
1977 debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
1979 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1980 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1982 /* unpack the C plane DC coefficients */
1983 debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
1985 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1986 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1988 /* fetch the AC table indices */
1989 ac_y_table = get_bits(gb, 4);
1990 ac_c_table = get_bits(gb, 4);
1992 /* unpack the group 1 AC coefficients (coeffs 1-5) */
1993 for (i = 1; i <= 5; i++) {
1995 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1997 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
1998 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
2000 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
2002 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
2003 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
2006 /* unpack the group 2 AC coefficients (coeffs 6-14) */
2007 for (i = 6; i <= 14; i++) {
2009 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
2011 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
2012 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
2014 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
2016 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
2017 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
2020 /* unpack the group 3 AC coefficients (coeffs 15-27) */
2021 for (i = 15; i <= 27; i++) {
2023 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
2025 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
2026 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
2028 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
2030 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
2031 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
2034 /* unpack the group 4 AC coefficients (coeffs 28-63) */
2035 for (i = 28; i <= 63; i++) {
2037 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
2039 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
2040 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
2042 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
2044 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
2045 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
2052 * This function reverses the DC prediction for each coded fragment in
2053 * the frame. Much of this function is adapted directly from the original
2056 #define COMPATIBLE_FRAME(x) \
2057 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
2058 #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
2059 static inline int iabs (int x) { return ((x < 0) ? -x : x); }
2061 static void reverse_dc_prediction(Vp3DecodeContext *s,
2064 int fragment_height)
2073 int i = first_fragment;
2076 * Fragment prediction groups:
2084 * Note: Groups 5 and 7 do not exist as it would mean that the
2085 * fragment's x coordinate is both 0 and (width - 1) at the same time.
2087 int predictor_group;
2090 /* validity flags for the left, up-left, up, and up-right fragments */
2091 int fl, ful, fu, fur;
2093 /* DC values for the left, up-left, up, and up-right fragments */
2094 int vl, vul, vu, vur;
2096 /* indices for the left, up-left, up, and up-right fragments */
2100 * The 6 fields mean:
2101 * 0: up-left multiplier
2103 * 2: up-right multiplier
2104 * 3: left multiplier
2106 * 5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
2108 int predictor_transform[16][6] = {
2109 { 0, 0, 0, 0, 0, 0 },
2110 { 0, 0, 0, 1, 0, 0 }, // PL
2111 { 0, 0, 1, 0, 0, 0 }, // PUR
2112 { 0, 0, 53, 75, 127, 7 }, // PUR|PL
2113 { 0, 1, 0, 0, 0, 0 }, // PU
2114 { 0, 1, 0, 1, 1, 1 }, // PU|PL
2115 { 0, 1, 0, 0, 0, 0 }, // PU|PUR
2116 { 0, 0, 53, 75, 127, 7 }, // PU|PUR|PL
2117 { 1, 0, 0, 0, 0, 0 }, // PUL
2118 { 0, 0, 0, 1, 0, 0 }, // PUL|PL
2119 { 1, 0, 1, 0, 1, 1 }, // PUL|PUR
2120 { 0, 0, 53, 75, 127, 7 }, // PUL|PUR|PL
2121 { 0, 1, 0, 0, 0, 0 }, // PUL|PU
2122 {-26, 29, 0, 29, 31, 5 }, // PUL|PU|PL
2123 { 3, 10, 3, 0, 15, 4 }, // PUL|PU|PUR
2124 {-26, 29, 0, 29, 31, 5 } // PUL|PU|PUR|PL
2127 /* This table shows which types of blocks can use other blocks for
2128 * prediction. For example, INTRA is the only mode in this table to
2129 * have a frame number of 0. That means INTRA blocks can only predict
2130 * from other INTRA blocks. There are 2 golden frame coding types;
2131 * blocks encoding in these modes can only predict from other blocks
2132 * that were encoded with these 1 of these 2 modes. */
2133 unsigned char compatible_frame[8] = {
2134 1, /* MODE_INTER_NO_MV */
2136 1, /* MODE_INTER_PLUS_MV */
2137 1, /* MODE_INTER_LAST_MV */
2138 1, /* MODE_INTER_PRIOR_MV */
2139 2, /* MODE_USING_GOLDEN */
2140 2, /* MODE_GOLDEN_MV */
2141 1 /* MODE_INTER_FOUR_MV */
2143 int current_frame_type;
2145 /* there is a last DC predictor for each of the 3 frame types */
2150 debug_vp3(" vp3: reversing DC prediction\n");
2152 vul = vu = vur = vl = 0;
2153 last_dc[0] = last_dc[1] = last_dc[2] = 0;
2155 /* for each fragment row... */
2156 for (y = 0; y < fragment_height; y++) {
2158 /* for each fragment in a row... */
2159 for (x = 0; x < fragment_width; x++, i++) {
2161 /* reverse prediction if this block was coded */
2162 if (s->all_fragments[i].coding_method != MODE_COPY) {
2164 current_frame_type =
2165 compatible_frame[s->all_fragments[i].coding_method];
2166 predictor_group = (x == 0) + ((y == 0) << 1) +
2167 ((x + 1 == fragment_width) << 2);
2168 debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
2169 i, predictor_group, s->all_fragments[i].coeffs[0]);
2171 switch (predictor_group) {
2174 /* main body of fragments; consider all 4 possible
2175 * fragments for prediction */
2177 /* calculate the indices of the predicting fragments */
2178 ul = i - fragment_width - 1;
2179 u = i - fragment_width;
2180 ur = i - fragment_width + 1;
2183 /* fetch the DC values for the predicting fragments */
2184 vul = s->all_fragments[ul].coeffs[0];
2185 vu = s->all_fragments[u].coeffs[0];
2186 vur = s->all_fragments[ur].coeffs[0];
2187 vl = s->all_fragments[l].coeffs[0];
2189 /* figure out which fragments are valid */
2190 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
2191 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
2192 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
2193 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
2195 /* decide which predictor transform to use */
2196 transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
2201 /* left column of fragments, not including top corner;
2202 * only consider up and up-right fragments */
2204 /* calculate the indices of the predicting fragments */
2205 u = i - fragment_width;
2206 ur = i - fragment_width + 1;
2208 /* fetch the DC values for the predicting fragments */
2209 vu = s->all_fragments[u].coeffs[0];
2210 vur = s->all_fragments[ur].coeffs[0];
2212 /* figure out which fragments are valid */
2213 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
2214 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
2216 /* decide which predictor transform to use */
2217 transform = (fu*PU) | (fur*PUR);
2223 /* top row of fragments, not including top-left frag;
2224 * only consider the left fragment for prediction */
2226 /* calculate the indices of the predicting fragments */
2229 /* fetch the DC values for the predicting fragments */
2230 vl = s->all_fragments[l].coeffs[0];
2232 /* figure out which fragments are valid */
2233 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
2235 /* decide which predictor transform to use */
2236 transform = (fl*PL);
2241 /* top-left fragment */
2243 /* nothing to predict from in this case */
2249 /* right column of fragments, not including top corner;
2250 * consider up-left, up, and left fragments for
2253 /* calculate the indices of the predicting fragments */
2254 ul = i - fragment_width - 1;
2255 u = i - fragment_width;
2258 /* fetch the DC values for the predicting fragments */
2259 vul = s->all_fragments[ul].coeffs[0];
2260 vu = s->all_fragments[u].coeffs[0];
2261 vl = s->all_fragments[l].coeffs[0];
2263 /* figure out which fragments are valid */
2264 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
2265 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
2266 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
2268 /* decide which predictor transform to use */
2269 transform = (fl*PL) | (fu*PU) | (ful*PUL);
2275 debug_dc_pred("transform = %d, ", transform);
2277 if (transform == 0) {
2279 /* if there were no fragments to predict from, use last
2281 s->all_fragments[i].coeffs[0] += last_dc[current_frame_type];
2282 debug_dc_pred("from last DC (%d) = %d\n",
2283 current_frame_type, s->all_fragments[i].coeffs[0]);
2287 /* apply the appropriate predictor transform */
2289 (predictor_transform[transform][0] * vul) +
2290 (predictor_transform[transform][1] * vu) +
2291 (predictor_transform[transform][2] * vur) +
2292 (predictor_transform[transform][3] * vl);
2294 /* if there is a shift value in the transform, add
2295 * the sign bit before the shift */
2296 if (predictor_transform[transform][5] != 0) {
2297 predicted_dc += ((predicted_dc >> 15) &
2298 predictor_transform[transform][4]);
2299 predicted_dc >>= predictor_transform[transform][5];
2302 /* check for outranging on the [ul u l] and
2303 * [ul u ur l] predictors */
2304 if ((transform == 13) || (transform == 15)) {
2305 if (iabs(predicted_dc - vu) > 128)
2307 else if (iabs(predicted_dc - vl) > 128)
2309 else if (iabs(predicted_dc - vul) > 128)
2313 /* at long last, apply the predictor */
2314 s->all_fragments[i].coeffs[0] += predicted_dc;
2315 debug_dc_pred("from pred DC = %d\n",
2316 s->all_fragments[i].coeffs[0]);
2320 last_dc[current_frame_type] = s->all_fragments[i].coeffs[0];
2327 * This function performs the final rendering of each fragment's data
2328 * onto the output frame.
2330 static void render_fragments(Vp3DecodeContext *s,
2334 int plane /* 0 = Y, 1 = U, 2 = V */)
2338 int i = first_fragment;
2339 int16_t *dequantizer;
2340 unsigned char *output_plane;
2341 unsigned char *last_plane;
2342 unsigned char *golden_plane;
2344 int motion_x, motion_y;
2345 int upper_motion_limit, lower_motion_limit;
2346 int motion_halfpel_index;
2347 uint8_t *motion_source;
2349 debug_vp3(" vp3: rendering final fragments for %s\n",
2350 (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
2352 /* set up plane-specific parameters */
2354 dequantizer = s->intra_y_dequant;
2355 output_plane = s->current_frame.data[0];
2356 last_plane = s->last_frame.data[0];
2357 golden_plane = s->golden_frame.data[0];
2358 stride = -s->current_frame.linesize[0];
2359 upper_motion_limit = 7 * s->current_frame.linesize[0];
2360 lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
2361 } else if (plane == 1) {
2362 dequantizer = s->intra_c_dequant;
2363 output_plane = s->current_frame.data[1];
2364 last_plane = s->last_frame.data[1];
2365 golden_plane = s->golden_frame.data[1];
2366 stride = -s->current_frame.linesize[1];
2367 upper_motion_limit = 7 * s->current_frame.linesize[1];
2368 lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
2370 dequantizer = s->intra_c_dequant;
2371 output_plane = s->current_frame.data[2];
2372 last_plane = s->last_frame.data[2];
2373 golden_plane = s->golden_frame.data[2];
2374 stride = -s->current_frame.linesize[2];
2375 upper_motion_limit = 7 * s->current_frame.linesize[2];
2376 lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
2379 /* for each fragment row... */
2380 for (y = 0; y < height; y += 8) {
2382 /* for each fragment in a row... */
2383 for (x = 0; x < width; x += 8, i++) {
2385 if ((i < 0) || (i >= s->fragment_count)) {
2386 printf (" vp3:render_fragments(): bad fragment number (%d)\n", i);
2390 /* transform if this block was coded */
2391 if (s->all_fragments[i].coding_method != MODE_COPY) {
2393 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2394 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2395 motion_source= golden_plane;
2397 motion_source= last_plane;
2399 motion_source += s->all_fragments[i].first_pixel;
2400 motion_halfpel_index = 0;
2402 /* sort out the motion vector if this fragment is coded
2403 * using a motion vector method */
2404 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2405 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2407 motion_x = s->all_fragments[i].motion_x;
2408 motion_y = s->all_fragments[i].motion_y;
2410 motion_x= (motion_x>>1) | (motion_x&1);
2411 motion_y= (motion_y>>1) | (motion_y&1);
2414 src_x= (motion_x>>1) + x;
2415 src_y= (motion_y>>1) + y;
2416 if ((motion_x == 0xbeef) || (motion_y == 0xbeef))
2417 printf (" help! got beefy vector! (%X, %X)\n", motion_x, motion_y);
2419 motion_halfpel_index = motion_x & 0x01;
2420 motion_source += (motion_x >> 1);
2422 // motion_y = -motion_y;
2423 motion_halfpel_index |= (motion_y & 0x01) << 1;
2424 motion_source += ((motion_y >> 1) * stride);
2426 if(src_x<0 || src_y<0 || src_x + 9 >= width || src_y + 9 >= height){
2427 uint8_t *temp= s->edge_emu_buffer;
2428 if(stride<0) temp -= 9*stride;
2430 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, width, height);
2431 motion_source= temp;
2435 /* first, take care of copying a block from either the
2436 * previous or the golden frame */
2437 if (s->all_fragments[i].coding_method != MODE_INTRA) {
2439 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
2440 output_plane + s->all_fragments[i].first_pixel,
2445 /* dequantize the DCT coefficients */
2446 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
2447 i, s->all_fragments[i].coding_method,
2448 s->all_fragments[i].coeffs[0], dequantizer[0]);
2450 /* invert DCT and place (or add) in final output */
2451 if (s->all_fragments[i].coding_method == MODE_INTRA) {
2452 vp3_idct_put(s->all_fragments[i].coeffs, dequantizer,
2453 output_plane + s->all_fragments[i].first_pixel,
2456 vp3_idct_add(s->all_fragments[i].coeffs, dequantizer,
2457 output_plane + s->all_fragments[i].first_pixel,
2461 debug_idct("block after idct_%s():\n",
2462 (s->all_fragments[i].coding_method == MODE_INTRA)?
2464 for (m = 0; m < 8; m++) {
2465 for (n = 0; n < 8; n++) {
2466 debug_idct(" %3d", *(output_plane +
2467 s->all_fragments[i].first_pixel + (m * stride + n)));
2475 /* copy directly from the previous frame */
2476 s->dsp.put_pixels_tab[1][0](
2477 output_plane + s->all_fragments[i].first_pixel,
2478 last_plane + s->all_fragments[i].first_pixel,
2490 * This function computes the first pixel addresses for each fragment.
2491 * This function needs to be invoked after the first frame is allocated
2492 * so that it has access to the plane strides.
2494 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
2499 /* figure out the first pixel addresses for each of the fragments */
2502 for (y = s->fragment_height; y > 0; y--) {
2503 for (x = 0; x < s->fragment_width; x++) {
2504 s->all_fragments[i++].first_pixel =
2505 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2506 s->golden_frame.linesize[0] +
2507 x * FRAGMENT_PIXELS;
2508 debug_init(" fragment %d, first pixel @ %d\n",
2509 i-1, s->all_fragments[i-1].first_pixel);
2514 i = s->u_fragment_start;
2515 for (y = s->fragment_height / 2; y > 0; y--) {
2516 for (x = 0; x < s->fragment_width / 2; x++) {
2517 s->all_fragments[i++].first_pixel =
2518 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2519 s->golden_frame.linesize[1] +
2520 x * FRAGMENT_PIXELS;
2521 debug_init(" fragment %d, first pixel @ %d\n",
2522 i-1, s->all_fragments[i-1].first_pixel);
2527 i = s->v_fragment_start;
2528 for (y = s->fragment_height / 2; y > 0; y--) {
2529 for (x = 0; x < s->fragment_width / 2; x++) {
2530 s->all_fragments[i++].first_pixel =
2531 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2532 s->golden_frame.linesize[2] +
2533 x * FRAGMENT_PIXELS;
2534 debug_init(" fragment %d, first pixel @ %d\n",
2535 i-1, s->all_fragments[i-1].first_pixel);
2541 * This is the ffmpeg/libavcodec API init function.
2543 static int vp3_decode_init(AVCodecContext *avctx)
2545 Vp3DecodeContext *s = avctx->priv_data;
2549 int y_superblock_count;
2550 int c_superblock_count;
2554 s->width = avctx->width;
2555 s->height = avctx->height;
2557 s->width = (avctx->width + 15) & 0xFFFFFFF0;
2558 s->height = (avctx->height + 15) & 0xFFFFFFF0;
2560 avctx->pix_fmt = PIX_FMT_YUV420P;
2561 avctx->has_b_frames = 0;
2562 dsputil_init(&s->dsp, avctx);
2564 /* initialize to an impossible value which will force a recalculation
2565 * in the first frame decode */
2566 s->quality_index = -1;
2568 s->y_superblock_width = (s->width + 31) / 32;
2569 s->y_superblock_height = (s->height + 31) / 32;
2570 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2572 /* work out the dimensions for the C planes */
2573 c_width = s->width / 2;
2574 c_height = s->height / 2;
2575 s->c_superblock_width = (c_width + 31) / 32;
2576 s->c_superblock_height = (c_height + 31) / 32;
2577 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2579 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
2580 s->u_superblock_start = y_superblock_count;
2581 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
2582 s->superblock_coding = av_malloc(s->superblock_count);
2584 s->macroblock_width = (s->width + 15) / 16;
2585 s->macroblock_height = (s->height + 15) / 16;
2586 s->macroblock_count = s->macroblock_width * s->macroblock_height;
2588 s->fragment_width = s->width / FRAGMENT_PIXELS;
2589 s->fragment_height = s->height / FRAGMENT_PIXELS;
2591 /* fragment count covers all 8x8 blocks for all 3 planes */
2592 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
2593 s->u_fragment_start = s->fragment_width * s->fragment_height;
2594 s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
2596 debug_init(" Y plane: %d x %d\n", s->width, s->height);
2597 debug_init(" C plane: %d x %d\n", c_width, c_height);
2598 debug_init(" Y superblocks: %d x %d, %d total\n",
2599 s->y_superblock_width, s->y_superblock_height, y_superblock_count);
2600 debug_init(" C superblocks: %d x %d, %d total\n",
2601 s->c_superblock_width, s->c_superblock_height, c_superblock_count);
2602 debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
2603 s->superblock_count, s->u_superblock_start, s->v_superblock_start);
2604 debug_init(" macroblocks: %d x %d, %d total\n",
2605 s->macroblock_width, s->macroblock_height, s->macroblock_count);
2606 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
2610 s->u_fragment_start,
2611 s->v_fragment_start);
2613 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
2614 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
2615 s->pixel_addresses_inited = 0;
2617 /* init VLC tables */
2618 for (i = 0; i < 16; i++) {
2621 init_vlc(&s->dc_vlc[i], 5, 32,
2622 &dc_bias[i][0][1], 4, 2,
2623 &dc_bias[i][0][0], 4, 2);
2625 /* group 1 AC histograms */
2626 init_vlc(&s->ac_vlc_1[i], 5, 32,
2627 &ac_bias_0[i][0][1], 4, 2,
2628 &ac_bias_0[i][0][0], 4, 2);
2630 /* group 2 AC histograms */
2631 init_vlc(&s->ac_vlc_2[i], 5, 32,
2632 &ac_bias_1[i][0][1], 4, 2,
2633 &ac_bias_1[i][0][0], 4, 2);
2635 /* group 3 AC histograms */
2636 init_vlc(&s->ac_vlc_3[i], 5, 32,
2637 &ac_bias_2[i][0][1], 4, 2,
2638 &ac_bias_2[i][0][0], 4, 2);
2640 /* group 4 AC histograms */
2641 init_vlc(&s->ac_vlc_4[i], 5, 32,
2642 &ac_bias_3[i][0][1], 4, 2,
2643 &ac_bias_3[i][0][0], 4, 2);
2646 /* build quantization zigzag table */
2647 for (i = 0; i < 64; i++)
2648 zigzag_index[dezigzag_index[i]] = i;
2650 /* work out the block mapping tables */
2651 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
2652 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
2653 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
2654 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
2655 init_block_mapping(s);
2657 for (i = 0; i < 3; i++) {
2658 s->current_frame.data[i] = NULL;
2659 s->last_frame.data[i] = NULL;
2660 s->golden_frame.data[i] = NULL;
2667 * This is the ffmpeg/libavcodec API frame decode function.
2669 static int vp3_decode_frame(AVCodecContext *avctx,
2670 void *data, int *data_size,
2671 uint8_t *buf, int buf_size)
2673 Vp3DecodeContext *s = avctx->priv_data;
2675 static int counter = 0;
2679 init_get_bits(&gb, buf, buf_size * 8);
2681 s->keyframe = get_bits(&gb, 1);
2684 s->last_quality_index = s->quality_index;
2685 s->quality_index = get_bits(&gb, 6);
2687 debug_vp3(" VP3 frame #%d: Q index = %d", counter, s->quality_index);
2690 if (s->quality_index != s->last_quality_index)
2691 init_dequantizer(s);
2695 debug_vp3(", keyframe\n");
2696 /* skip the other 2 header bytes for now */
2698 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
2699 if (s->golden_frame.data[0])
2700 avctx->release_buffer(avctx, &s->golden_frame);
2701 s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
2703 if (s->golden_frame.data[0])
2704 avctx->release_buffer(avctx, &s->golden_frame);
2705 if (s->last_frame.data[0])
2706 avctx->release_buffer(avctx, &s->last_frame);
2709 s->golden_frame.reference = 3;
2710 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
2711 printf("vp3: get_buffer() failed\n");
2715 /* golden frame is also the current frame */
2716 memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
2718 /* time to figure out pixel addresses? */
2719 if (!s->pixel_addresses_inited)
2720 vp3_calculate_pixel_addresses(s);
2726 /* allocate a new current frame */
2727 s->current_frame.reference = 3;
2728 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
2729 printf("vp3: get_buffer() failed\n");
2734 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
2735 s->current_frame.qstride= 0;
2742 memcpy(s->current_frame.data[0], s->golden_frame.data[0],
2743 s->current_frame.linesize[0] * s->height);
2744 memcpy(s->current_frame.data[1], s->golden_frame.data[1],
2745 s->current_frame.linesize[1] * s->height / 2);
2746 memcpy(s->current_frame.data[2], s->golden_frame.data[2],
2747 s->current_frame.linesize[2] * s->height / 2);
2752 if (unpack_superblocks(s, &gb) ||
2753 unpack_modes(s, &gb) ||
2754 unpack_vectors(s, &gb) ||
2755 unpack_dct_coeffs(s, &gb)) {
2757 printf(" vp3: could not decode frame\n");
2761 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
2762 render_fragments(s, 0, s->width, s->height, 0);
2764 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
2765 reverse_dc_prediction(s, s->u_fragment_start,
2766 s->fragment_width / 2, s->fragment_height / 2);
2767 reverse_dc_prediction(s, s->v_fragment_start,
2768 s->fragment_width / 2, s->fragment_height / 2);
2769 render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
2770 render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
2772 memset(s->current_frame.data[1], 0x80, s->width * s->height / 4);
2773 memset(s->current_frame.data[2], 0x80, s->width * s->height / 4);
2780 *data_size=sizeof(AVFrame);
2781 *(AVFrame*)data= s->current_frame;
2783 /* release the last frame, if it is allocated and if it is not the
2785 if ((s->last_frame.data[0]) &&
2786 (s->last_frame.data[0] != s->golden_frame.data[0]))
2787 avctx->release_buffer(avctx, &s->last_frame);
2789 /* shuffle frames (last = current) */
2790 memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
2791 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
2797 * This is the ffmpeg/libavcodec API module cleanup function.
2799 static int vp3_decode_end(AVCodecContext *avctx)
2801 Vp3DecodeContext *s = avctx->priv_data;
2803 av_free(s->all_fragments);
2804 av_free(s->coded_fragment_list);
2805 av_free(s->superblock_fragments);
2806 av_free(s->superblock_macroblocks);
2807 av_free(s->macroblock_fragments);
2808 av_free(s->macroblock_coding);
2810 /* release all frames */
2811 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
2812 avctx->release_buffer(avctx, &s->golden_frame);
2813 if (s->last_frame.data[0])
2814 avctx->release_buffer(avctx, &s->last_frame);
2815 /* no need to release the current_frame since it will always be pointing
2816 * to the same frame as either the golden or last frame */
2821 AVCodec vp3_decoder = {
2825 sizeof(Vp3DecodeContext),