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/
23 * Theora decoder by Alex Beregszaszi
29 * On2 VP3 Video Decoder
40 #include "mpegvideo.h"
45 #define FRAGMENT_PIXELS 8
50 * Define one or more of the following compile-time variables to 1 to obtain
51 * elaborate information about certain aspects of the decoding process.
53 * KEYFRAMES_ONLY: set this to 1 to only see keyframes (VP3 slideshow mode)
54 * DEBUG_VP3: high-level decoding flow
55 * DEBUG_INIT: initialization parameters
56 * DEBUG_DEQUANTIZERS: display how the dequanization tables are built
57 * DEBUG_BLOCK_CODING: unpacking the superblock/macroblock/fragment coding
58 * DEBUG_MODES: unpacking the coding modes for individual fragments
59 * DEBUG_VECTORS: display the motion vectors
60 * DEBUG_TOKEN: display exhaustive information about each DCT token
61 * DEBUG_VLC: display the VLCs as they are extracted from the stream
62 * DEBUG_DC_PRED: display the process of reversing DC prediction
63 * DEBUG_IDCT: show every detail of the IDCT process
66 #define KEYFRAMES_ONLY 0
70 #define DEBUG_DEQUANTIZERS 0
71 #define DEBUG_BLOCK_CODING 0
73 #define DEBUG_VECTORS 0
76 #define DEBUG_DC_PRED 0
80 #define debug_vp3 printf
82 static inline void debug_vp3(const char *format, ...) { }
86 #define debug_init printf
88 static inline void debug_init(const char *format, ...) { }
91 #if DEBUG_DEQUANTIZERS
92 #define debug_dequantizers printf
94 static inline void debug_dequantizers(const char *format, ...) { }
97 #if DEBUG_BLOCK_CODING
98 #define debug_block_coding printf
100 static inline void debug_block_coding(const char *format, ...) { }
104 #define debug_modes printf
106 static inline void debug_modes(const char *format, ...) { }
110 #define debug_vectors printf
112 static inline void debug_vectors(const char *format, ...) { }
116 #define debug_token printf
118 static inline void debug_token(const char *format, ...) { }
122 #define debug_vlc printf
124 static inline void debug_vlc(const char *format, ...) { }
128 #define debug_dc_pred printf
130 static inline void debug_dc_pred(const char *format, ...) { }
134 #define debug_idct printf
136 static inline void debug_idct(const char *format, ...) { }
139 typedef struct Vp3Fragment {
146 /* address of first pixel taking into account which plane the fragment
147 * lives on as well as the plane stride */
149 /* this is the macroblock that the fragment belongs to */
153 #define SB_NOT_CODED 0
154 #define SB_PARTIALLY_CODED 1
155 #define SB_FULLY_CODED 2
157 #define MODE_INTER_NO_MV 0
159 #define MODE_INTER_PLUS_MV 2
160 #define MODE_INTER_LAST_MV 3
161 #define MODE_INTER_PRIOR_LAST 4
162 #define MODE_USING_GOLDEN 5
163 #define MODE_GOLDEN_MV 6
164 #define MODE_INTER_FOURMV 7
165 #define CODING_MODE_COUNT 8
167 /* special internal mode */
170 /* There are 6 preset schemes, plus a free-form scheme */
171 static int ModeAlphabet[7][CODING_MODE_COUNT] =
173 /* this is the custom scheme */
174 { 0, 0, 0, 0, 0, 0, 0, 0 },
176 /* scheme 1: Last motion vector dominates */
177 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
178 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
179 MODE_INTRA, MODE_USING_GOLDEN,
180 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
183 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
184 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
185 MODE_INTRA, MODE_USING_GOLDEN,
186 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
189 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
190 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
191 MODE_INTRA, MODE_USING_GOLDEN,
192 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
195 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
196 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
197 MODE_INTRA, MODE_USING_GOLDEN,
198 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
200 /* scheme 5: No motion vector dominates */
201 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
202 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
203 MODE_INTRA, MODE_USING_GOLDEN,
204 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
207 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
208 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
209 MODE_INTER_PLUS_MV, MODE_INTRA,
210 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
214 #define MIN_DEQUANT_VAL 2
216 typedef struct Vp3DecodeContext {
217 AVCodecContext *avctx;
218 int theora, theora_tables;
221 AVFrame golden_frame;
223 AVFrame current_frame;
229 int last_quality_index;
231 int superblock_count;
232 int superblock_width;
233 int superblock_height;
234 int y_superblock_width;
235 int y_superblock_height;
236 int c_superblock_width;
237 int c_superblock_height;
238 int u_superblock_start;
239 int v_superblock_start;
240 unsigned char *superblock_coding;
242 int macroblock_count;
243 int macroblock_width;
244 int macroblock_height;
250 Vp3Fragment *all_fragments;
251 int u_fragment_start;
252 int v_fragment_start;
255 uint16_t coded_dc_scale_factor[64];
256 uint32_t coded_quality_threshold[64];
257 uint16_t coded_intra_y_dequant[64];
258 uint16_t coded_intra_c_dequant[64];
259 uint16_t coded_inter_dequant[64];
261 /* this is a list of indices into the all_fragments array indicating
262 * which of the fragments are coded */
263 int *coded_fragment_list;
264 int coded_fragment_list_index;
265 int pixel_addresses_inited;
273 int16_t intra_y_dequant[64];
274 int16_t intra_c_dequant[64];
275 int16_t inter_dequant[64];
277 /* This table contains superblock_count * 16 entries. Each set of 16
278 * numbers corresponds to the fragment indices 0..15 of the superblock.
279 * An entry will be -1 to indicate that no entry corresponds to that
281 int *superblock_fragments;
283 /* This table contains superblock_count * 4 entries. Each set of 4
284 * numbers corresponds to the macroblock indices 0..3 of the superblock.
285 * An entry will be -1 to indicate that no entry corresponds to that
287 int *superblock_macroblocks;
289 /* This table contains macroblock_count * 6 entries. Each set of 6
290 * numbers corresponds to the fragment indices 0..5 which comprise
291 * the macroblock (4 Y fragments and 2 C fragments). */
292 int *macroblock_fragments;
293 /* This is an array that indicates how a particular macroblock
295 unsigned char *macroblock_coding;
297 int first_coded_y_fragment;
298 int first_coded_c_fragment;
299 int last_coded_y_fragment;
300 int last_coded_c_fragment;
302 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
303 uint8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
306 static int theora_decode_comments(AVCodecContext *avctx, GetBitContext gb);
307 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb);
309 /************************************************************************
311 ************************************************************************/
313 #define IdctAdjustBeforeShift 8
322 void vp3_idct_c(int16_t *input_data, int16_t *dequant_matrix,
323 int16_t *output_data)
325 int32_t intermediate_data[64];
326 int32_t *ip = intermediate_data;
327 int16_t *op = output_data;
329 int32_t A_, B_, C_, D_, _Ad, _Bd, _Cd, _Dd, E_, F_, G_, H_;
330 int32_t _Ed, _Gd, _Add, _Bdd, _Fd, _Hd;
335 debug_idct("raw coefficient block:\n");
336 for (i = 0; i < 8; i++) {
337 for (j = 0; j < 8; j++) {
338 debug_idct(" %5d", input_data[i * 8 + j]);
344 for (i = 0; i < 64; i++) {
345 j = dezigzag_index[i];
346 intermediate_data[j] = dequant_matrix[i] * input_data[i];
349 debug_idct("dequantized block:\n");
350 for (i = 0; i < 8; i++) {
351 for (j = 0; j < 8; j++) {
352 debug_idct(" %5d", intermediate_data[i * 8 + j]);
358 /* Inverse DCT on the rows now */
359 for (i = 0; i < 8; i++) {
360 /* Check for non-zero values */
361 if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
362 t1 = (int32_t)(xC1S7 * ip[1]);
363 t2 = (int32_t)(xC7S1 * ip[7]);
368 t1 = (int32_t)(xC7S1 * ip[1]);
369 t2 = (int32_t)(xC1S7 * ip[7]);
374 t1 = (int32_t)(xC3S5 * ip[3]);
375 t2 = (int32_t)(xC5S3 * ip[5]);
380 t1 = (int32_t)(xC3S5 * ip[5]);
381 t2 = (int32_t)(xC5S3 * ip[3]);
387 t1 = (int32_t)(xC4S4 * (A_ - C_));
391 t1 = (int32_t)(xC4S4 * (B_ - D_));
399 t1 = (int32_t)(xC4S4 * (ip[0] + ip[4]));
403 t1 = (int32_t)(xC4S4 * (ip[0] - ip[4]));
407 t1 = (int32_t)(xC2S6 * ip[2]);
408 t2 = (int32_t)(xC6S2 * ip[6]);
413 t1 = (int32_t)(xC6S2 * ip[2]);
414 t2 = (int32_t)(xC2S6 * ip[6]);
429 /* Final sequence of operations over-write original inputs. */
430 ip[0] = (int16_t)((_Gd + _Cd ) >> 0);
431 ip[7] = (int16_t)((_Gd - _Cd ) >> 0);
433 ip[1] = (int16_t)((_Add + _Hd ) >> 0);
434 ip[2] = (int16_t)((_Add - _Hd ) >> 0);
436 ip[3] = (int16_t)((_Ed + _Dd ) >> 0);
437 ip[4] = (int16_t)((_Ed - _Dd ) >> 0);
439 ip[5] = (int16_t)((_Fd + _Bdd ) >> 0);
440 ip[6] = (int16_t)((_Fd - _Bdd ) >> 0);
444 ip += 8; /* next row */
447 ip = intermediate_data;
449 for ( i = 0; i < 8; i++) {
450 /* Check for non-zero values (bitwise or faster than ||) */
451 if ( ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
452 ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
454 t1 = (int32_t)(xC1S7 * ip[1*8]);
455 t2 = (int32_t)(xC7S1 * ip[7*8]);
460 t1 = (int32_t)(xC7S1 * ip[1*8]);
461 t2 = (int32_t)(xC1S7 * ip[7*8]);
466 t1 = (int32_t)(xC3S5 * ip[3*8]);
467 t2 = (int32_t)(xC5S3 * ip[5*8]);
472 t1 = (int32_t)(xC3S5 * ip[5*8]);
473 t2 = (int32_t)(xC5S3 * ip[3*8]);
479 t1 = (int32_t)(xC4S4 * (A_ - C_));
483 t1 = (int32_t)(xC4S4 * (B_ - D_));
491 t1 = (int32_t)(xC4S4 * (ip[0*8] + ip[4*8]));
495 t1 = (int32_t)(xC4S4 * (ip[0*8] - ip[4*8]));
499 t1 = (int32_t)(xC2S6 * ip[2*8]);
500 t2 = (int32_t)(xC6S2 * ip[6*8]);
505 t1 = (int32_t)(xC6S2 * ip[2*8]);
506 t2 = (int32_t)(xC2S6 * ip[6*8]);
521 _Gd += IdctAdjustBeforeShift;
522 _Add += IdctAdjustBeforeShift;
523 _Ed += IdctAdjustBeforeShift;
524 _Fd += IdctAdjustBeforeShift;
526 /* Final sequence of operations over-write original inputs. */
527 op[0*8] = (int16_t)((_Gd + _Cd ) >> 4);
528 op[7*8] = (int16_t)((_Gd - _Cd ) >> 4);
530 op[1*8] = (int16_t)((_Add + _Hd ) >> 4);
531 op[2*8] = (int16_t)((_Add - _Hd ) >> 4);
533 op[3*8] = (int16_t)((_Ed + _Dd ) >> 4);
534 op[4*8] = (int16_t)((_Ed - _Dd ) >> 4);
536 op[5*8] = (int16_t)((_Fd + _Bdd ) >> 4);
537 op[6*8] = (int16_t)((_Fd - _Bdd ) >> 4);
551 ip++; /* next column */
556 void vp3_idct_put(int16_t *input_data, int16_t *dequant_matrix,
557 uint8_t *dest, int stride)
559 int16_t transformed_data[64];
563 vp3_idct_c(input_data, dequant_matrix, transformed_data);
565 /* place in final output */
566 op = transformed_data;
567 for (i = 0; i < 8; i++) {
568 for (j = 0; j < 8; j++) {
574 *dest = (uint8_t)(*op + 128);
578 dest += (stride - 8);
582 void vp3_idct_add(int16_t *input_data, int16_t *dequant_matrix,
583 uint8_t *dest, int stride)
585 int16_t transformed_data[64];
590 vp3_idct_c(input_data, dequant_matrix, transformed_data);
592 /* place in final output */
593 op = transformed_data;
594 for (i = 0; i < 8; i++) {
595 for (j = 0; j < 8; j++) {
596 sample = *dest + *op;
599 else if (sample > 255)
602 *dest = (uint8_t)(sample & 0xFF);
606 dest += (stride - 8);
610 /************************************************************************
611 * VP3 specific functions
612 ************************************************************************/
615 * This function sets up all of the various blocks mappings:
616 * superblocks <-> fragments, macroblocks <-> fragments,
617 * superblocks <-> macroblocks
619 * Returns 0 is successful; returns 1 if *anything* went wrong.
621 static int init_block_mapping(Vp3DecodeContext *s)
624 signed int hilbert_walk_y[16];
625 signed int hilbert_walk_c[16];
626 signed int hilbert_walk_mb[4];
628 int current_fragment = 0;
629 int current_width = 0;
630 int current_height = 0;
633 int superblock_row_inc = 0;
635 int mapping_index = 0;
637 int current_macroblock;
640 signed char travel_width[16] = {
647 signed char travel_height[16] = {
654 signed char travel_width_mb[4] = {
658 signed char travel_height_mb[4] = {
662 debug_vp3(" vp3: initialize block mapping tables\n");
664 /* figure out hilbert pattern per these frame dimensions */
665 hilbert_walk_y[0] = 1;
666 hilbert_walk_y[1] = 1;
667 hilbert_walk_y[2] = s->fragment_width;
668 hilbert_walk_y[3] = -1;
669 hilbert_walk_y[4] = s->fragment_width;
670 hilbert_walk_y[5] = s->fragment_width;
671 hilbert_walk_y[6] = 1;
672 hilbert_walk_y[7] = -s->fragment_width;
673 hilbert_walk_y[8] = 1;
674 hilbert_walk_y[9] = s->fragment_width;
675 hilbert_walk_y[10] = 1;
676 hilbert_walk_y[11] = -s->fragment_width;
677 hilbert_walk_y[12] = -s->fragment_width;
678 hilbert_walk_y[13] = -1;
679 hilbert_walk_y[14] = -s->fragment_width;
680 hilbert_walk_y[15] = 1;
682 hilbert_walk_c[0] = 1;
683 hilbert_walk_c[1] = 1;
684 hilbert_walk_c[2] = s->fragment_width / 2;
685 hilbert_walk_c[3] = -1;
686 hilbert_walk_c[4] = s->fragment_width / 2;
687 hilbert_walk_c[5] = s->fragment_width / 2;
688 hilbert_walk_c[6] = 1;
689 hilbert_walk_c[7] = -s->fragment_width / 2;
690 hilbert_walk_c[8] = 1;
691 hilbert_walk_c[9] = s->fragment_width / 2;
692 hilbert_walk_c[10] = 1;
693 hilbert_walk_c[11] = -s->fragment_width / 2;
694 hilbert_walk_c[12] = -s->fragment_width / 2;
695 hilbert_walk_c[13] = -1;
696 hilbert_walk_c[14] = -s->fragment_width / 2;
697 hilbert_walk_c[15] = 1;
699 hilbert_walk_mb[0] = 1;
700 hilbert_walk_mb[1] = s->macroblock_width;
701 hilbert_walk_mb[2] = 1;
702 hilbert_walk_mb[3] = -s->macroblock_width;
704 /* iterate through each superblock (all planes) and map the fragments */
705 for (i = 0; i < s->superblock_count; i++) {
706 debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
707 i, s->u_superblock_start, s->v_superblock_start);
709 /* time to re-assign the limits? */
712 /* start of Y superblocks */
713 right_edge = s->fragment_width;
714 bottom_edge = s->fragment_height;
717 superblock_row_inc = 3 * s->fragment_width -
718 (s->y_superblock_width * 4 - s->fragment_width);
719 hilbert = hilbert_walk_y;
721 /* the first operation for this variable is to advance by 1 */
722 current_fragment = -1;
724 } else if (i == s->u_superblock_start) {
726 /* start of U superblocks */
727 right_edge = s->fragment_width / 2;
728 bottom_edge = s->fragment_height / 2;
731 superblock_row_inc = 3 * (s->fragment_width / 2) -
732 (s->c_superblock_width * 4 - s->fragment_width / 2);
733 hilbert = hilbert_walk_c;
735 /* the first operation for this variable is to advance by 1 */
736 current_fragment = s->u_fragment_start - 1;
738 } else if (i == s->v_superblock_start) {
740 /* start of V superblocks */
741 right_edge = s->fragment_width / 2;
742 bottom_edge = s->fragment_height / 2;
745 superblock_row_inc = 3 * (s->fragment_width / 2) -
746 (s->c_superblock_width * 4 - s->fragment_width / 2);
747 hilbert = hilbert_walk_c;
749 /* the first operation for this variable is to advance by 1 */
750 current_fragment = s->v_fragment_start - 1;
754 if (current_width >= right_edge - 1) {
755 /* reset width and move to next superblock row */
759 /* fragment is now at the start of a new superblock row */
760 current_fragment += superblock_row_inc;
763 /* iterate through all 16 fragments in a superblock */
764 for (j = 0; j < 16; j++) {
765 current_fragment += hilbert[j];
766 current_width += travel_width[j];
767 current_height += travel_height[j];
769 /* check if the fragment is in bounds */
770 if ((current_width < right_edge) &&
771 (current_height < bottom_edge)) {
772 s->superblock_fragments[mapping_index] = current_fragment;
773 debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
774 s->superblock_fragments[mapping_index], i, j,
775 current_width, right_edge, current_height, bottom_edge);
777 s->superblock_fragments[mapping_index] = -1;
778 debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
780 current_width, right_edge, current_height, bottom_edge);
787 /* initialize the superblock <-> macroblock mapping; iterate through
788 * all of the Y plane superblocks to build this mapping */
789 right_edge = s->macroblock_width;
790 bottom_edge = s->macroblock_height;
793 superblock_row_inc = s->macroblock_width -
794 (s->y_superblock_width * 2 - s->macroblock_width);;
795 hilbert = hilbert_walk_mb;
797 current_macroblock = -1;
798 for (i = 0; i < s->u_superblock_start; i++) {
800 if (current_width >= right_edge - 1) {
801 /* reset width and move to next superblock row */
805 /* macroblock is now at the start of a new superblock row */
806 current_macroblock += superblock_row_inc;
809 /* iterate through each potential macroblock in the superblock */
810 for (j = 0; j < 4; j++) {
811 current_macroblock += hilbert_walk_mb[j];
812 current_width += travel_width_mb[j];
813 current_height += travel_height_mb[j];
815 /* check if the macroblock is in bounds */
816 if ((current_width < right_edge) &&
817 (current_height < bottom_edge)) {
818 s->superblock_macroblocks[mapping_index] = current_macroblock;
819 debug_init(" mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
820 s->superblock_macroblocks[mapping_index], i, j,
821 current_width, right_edge, current_height, bottom_edge);
823 s->superblock_macroblocks[mapping_index] = -1;
824 debug_init(" superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
826 current_width, right_edge, current_height, bottom_edge);
833 /* initialize the macroblock <-> fragment mapping */
834 current_fragment = 0;
835 current_macroblock = 0;
837 for (i = 0; i < s->fragment_height; i += 2) {
839 for (j = 0; j < s->fragment_width; j += 2) {
841 debug_init(" macroblock %d contains fragments: ", current_macroblock);
842 s->all_fragments[current_fragment].macroblock = current_macroblock;
843 s->macroblock_fragments[mapping_index++] = current_fragment;
844 debug_init("%d ", current_fragment);
846 if (j + 1 < s->fragment_width) {
847 s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
848 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
849 debug_init("%d ", current_fragment + 1);
851 s->macroblock_fragments[mapping_index++] = -1;
853 if (i + 1 < s->fragment_height) {
854 s->all_fragments[current_fragment + s->fragment_width].macroblock =
856 s->macroblock_fragments[mapping_index++] =
857 current_fragment + s->fragment_width;
858 debug_init("%d ", current_fragment + s->fragment_width);
860 s->macroblock_fragments[mapping_index++] = -1;
862 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
863 s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
865 s->macroblock_fragments[mapping_index++] =
866 current_fragment + s->fragment_width + 1;
867 debug_init("%d ", current_fragment + s->fragment_width + 1);
869 s->macroblock_fragments[mapping_index++] = -1;
872 c_fragment = s->u_fragment_start +
873 (i * s->fragment_width / 4) + (j / 2);
874 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
875 s->macroblock_fragments[mapping_index++] = c_fragment;
876 debug_init("%d ", c_fragment);
878 c_fragment = s->v_fragment_start +
879 (i * s->fragment_width / 4) + (j / 2);
880 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
881 s->macroblock_fragments[mapping_index++] = c_fragment;
882 debug_init("%d ", c_fragment);
886 if (j + 2 <= s->fragment_width)
887 current_fragment += 2;
890 current_macroblock++;
893 current_fragment += s->fragment_width;
896 return 0; /* successful path out */
900 * This function unpacks a single token (which should be in the range 0..31)
901 * and returns a zero run (number of zero coefficients in current DCT matrix
902 * before next non-zero coefficient), the next DCT coefficient, and the
903 * number of consecutive, non-EOB'd DCT blocks to EOB.
905 static void unpack_token(GetBitContext *gb, int token, int *zero_run,
906 DCTELEM *coeff, int *eob_run)
914 debug_token(" vp3 token %d: ", token);
918 debug_token("DCT_EOB_TOKEN, EOB next block\n");
923 debug_token("DCT_EOB_PAIR_TOKEN, EOB next 2 blocks\n");
928 debug_token("DCT_EOB_TRIPLE_TOKEN, EOB next 3 blocks\n");
933 debug_token("DCT_REPEAT_RUN_TOKEN, ");
934 *eob_run = get_bits(gb, 2) + 4;
935 debug_token("EOB the next %d blocks\n", *eob_run);
939 debug_token("DCT_REPEAT_RUN2_TOKEN, ");
940 *eob_run = get_bits(gb, 3) + 8;
941 debug_token("EOB the next %d blocks\n", *eob_run);
945 debug_token("DCT_REPEAT_RUN3_TOKEN, ");
946 *eob_run = get_bits(gb, 4) + 16;
947 debug_token("EOB the next %d blocks\n", *eob_run);
951 debug_token("DCT_REPEAT_RUN4_TOKEN, ");
952 *eob_run = get_bits(gb, 12);
953 debug_token("EOB the next %d blocks\n", *eob_run);
957 debug_token("DCT_SHORT_ZRL_TOKEN, ");
958 /* note that this token actually indicates that (3 extra bits) + 1 0s
959 * should be output; this case specifies a run of (3 EBs) 0s and a
960 * coefficient of 0. */
961 *zero_run = get_bits(gb, 3);
963 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
967 debug_token("DCT_ZRL_TOKEN, ");
968 /* note that this token actually indicates that (6 extra bits) + 1 0s
969 * should be output; this case specifies a run of (6 EBs) 0s and a
970 * coefficient of 0. */
971 *zero_run = get_bits(gb, 6);
973 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
977 debug_token("ONE_TOKEN, output 1\n");
982 debug_token("MINUS_ONE_TOKEN, output -1\n");
987 debug_token("TWO_TOKEN, output 2\n");
992 debug_token("MINUS_TWO_TOKEN, output -2\n");
1000 debug_token("LOW_VAL_TOKENS, ");
1001 if (get_bits(gb, 1))
1002 *coeff = -(3 + (token - 13));
1004 *coeff = 3 + (token - 13);
1005 debug_token("output %d\n", *coeff);
1009 debug_token("DCT_VAL_CATEGORY3, ");
1010 sign = get_bits(gb, 1);
1011 *coeff = 7 + get_bits(gb, 1);
1014 debug_token("output %d\n", *coeff);
1018 debug_token("DCT_VAL_CATEGORY4, ");
1019 sign = get_bits(gb, 1);
1020 *coeff = 9 + get_bits(gb, 2);
1023 debug_token("output %d\n", *coeff);
1027 debug_token("DCT_VAL_CATEGORY5, ");
1028 sign = get_bits(gb, 1);
1029 *coeff = 13 + get_bits(gb, 3);
1032 debug_token("output %d\n", *coeff);
1036 debug_token("DCT_VAL_CATEGORY6, ");
1037 sign = get_bits(gb, 1);
1038 *coeff = 21 + get_bits(gb, 4);
1041 debug_token("output %d\n", *coeff);
1045 debug_token("DCT_VAL_CATEGORY7, ");
1046 sign = get_bits(gb, 1);
1047 *coeff = 37 + get_bits(gb, 5);
1050 debug_token("output %d\n", *coeff);
1054 debug_token("DCT_VAL_CATEGORY8, ");
1055 sign = get_bits(gb, 1);
1056 *coeff = 69 + get_bits(gb, 9);
1059 debug_token("output %d\n", *coeff);
1067 debug_token("DCT_RUN_CATEGORY1, ");
1068 *zero_run = token - 22;
1069 if (get_bits(gb, 1))
1073 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1077 debug_token("DCT_RUN_CATEGORY1B, ");
1078 if (get_bits(gb, 1))
1082 *zero_run = 6 + get_bits(gb, 2);
1083 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1087 debug_token("DCT_RUN_CATEGORY1C, ");
1088 if (get_bits(gb, 1))
1092 *zero_run = 10 + get_bits(gb, 3);
1093 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1097 debug_token("DCT_RUN_CATEGORY2, ");
1098 sign = get_bits(gb, 1);
1099 *coeff = 2 + get_bits(gb, 1);
1103 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1107 debug_token("DCT_RUN_CATEGORY2, ");
1108 sign = get_bits(gb, 1);
1109 *coeff = 2 + get_bits(gb, 1);
1112 *zero_run = 2 + get_bits(gb, 1);
1113 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
1117 av_log(NULL, AV_LOG_ERROR, " vp3: help! Got a bad token: %d > 31\n", token);
1124 * This function wipes out all of the fragment data.
1126 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
1130 /* zero out all of the fragment information */
1131 s->coded_fragment_list_index = 0;
1132 for (i = 0; i < s->fragment_count; i++) {
1133 memset(s->all_fragments[i].coeffs, 0, 64 * sizeof(DCTELEM));
1134 s->all_fragments[i].coeff_count = 0;
1135 s->all_fragments[i].last_coeff = 0;
1136 s->all_fragments[i].motion_x = 0xbeef;
1137 s->all_fragments[i].motion_y = 0xbeef;
1142 * This function sets of the dequantization tables used for a particular
1145 static void init_dequantizer(Vp3DecodeContext *s)
1148 int quality_scale = s->coded_quality_threshold[s->quality_index];
1149 int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index];
1152 debug_vp3(" vp3: initializing dequantization tables\n");
1155 * Scale dequantizers:
1161 * where sf = dc_scale_factor for DC quantizer
1162 * or quality_scale for AC quantizer
1164 * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL.
1168 /* scale DC quantizers */
1169 s->intra_y_dequant[0] = s->coded_intra_y_dequant[0] * dc_scale_factor / 100;
1170 if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2)
1171 s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2;
1172 s->intra_y_dequant[0] *= SCALER;
1174 s->intra_c_dequant[0] = s->coded_intra_c_dequant[0] * dc_scale_factor / 100;
1175 if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2)
1176 s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2;
1177 s->intra_c_dequant[0] *= SCALER;
1179 s->inter_dequant[0] = s->coded_inter_dequant[0] * dc_scale_factor / 100;
1180 if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4)
1181 s->inter_dequant[0] = MIN_DEQUANT_VAL * 4;
1182 s->inter_dequant[0] *= SCALER;
1184 /* scale AC quantizers, zigzag at the same time in preparation for
1185 * the dequantization phase */
1186 for (i = 1; i < 64; i++) {
1188 j = zigzag_index[i];
1190 s->intra_y_dequant[j] = s->coded_intra_y_dequant[i] * quality_scale / 100;
1191 if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL)
1192 s->intra_y_dequant[j] = MIN_DEQUANT_VAL;
1193 s->intra_y_dequant[j] *= SCALER;
1195 s->intra_c_dequant[j] = s->coded_intra_c_dequant[i] * quality_scale / 100;
1196 if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL)
1197 s->intra_c_dequant[j] = MIN_DEQUANT_VAL;
1198 s->intra_c_dequant[j] *= SCALER;
1200 s->inter_dequant[j] = s->coded_inter_dequant[i] * quality_scale / 100;
1201 if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2)
1202 s->inter_dequant[j] = MIN_DEQUANT_VAL * 2;
1203 s->inter_dequant[j] *= SCALER;
1206 memset(s->qscale_table, (FFMAX(s->intra_y_dequant[1], s->intra_c_dequant[1])+8)/16, 512); //FIXME finetune
1208 /* print debug information as requested */
1209 debug_dequantizers("intra Y dequantizers:\n");
1210 for (i = 0; i < 8; i++) {
1211 for (j = i * 8; j < i * 8 + 8; j++) {
1212 debug_dequantizers(" %4d,", s->intra_y_dequant[j]);
1214 debug_dequantizers("\n");
1216 debug_dequantizers("\n");
1218 debug_dequantizers("intra C dequantizers:\n");
1219 for (i = 0; i < 8; i++) {
1220 for (j = i * 8; j < i * 8 + 8; j++) {
1221 debug_dequantizers(" %4d,", s->intra_c_dequant[j]);
1223 debug_dequantizers("\n");
1225 debug_dequantizers("\n");
1227 debug_dequantizers("interframe dequantizers:\n");
1228 for (i = 0; i < 8; i++) {
1229 for (j = i * 8; j < i * 8 + 8; j++) {
1230 debug_dequantizers(" %4d,", s->inter_dequant[j]);
1232 debug_dequantizers("\n");
1234 debug_dequantizers("\n");
1238 * This function is used to fetch runs of 1s or 0s from the bitstream for
1239 * use in determining which superblocks are fully and partially coded.
1241 * Codeword RunLength
1248 * 111111xxxxxxxxxxxx 34-4129
1250 static int get_superblock_run_length(GetBitContext *gb)
1253 if (get_bits(gb, 1) == 0)
1256 else if (get_bits(gb, 1) == 0)
1257 return (2 + get_bits(gb, 1));
1259 else if (get_bits(gb, 1) == 0)
1260 return (4 + get_bits(gb, 1));
1262 else if (get_bits(gb, 1) == 0)
1263 return (6 + get_bits(gb, 2));
1265 else if (get_bits(gb, 1) == 0)
1266 return (10 + get_bits(gb, 3));
1268 else if (get_bits(gb, 1) == 0)
1269 return (18 + get_bits(gb, 4));
1272 return (34 + get_bits(gb, 12));
1277 * This function is used to fetch runs of 1s or 0s from the bitstream for
1278 * use in determining which particular fragments are coded.
1280 * Codeword RunLength
1288 static int get_fragment_run_length(GetBitContext *gb)
1291 if (get_bits(gb, 1) == 0)
1292 return (1 + get_bits(gb, 1));
1294 else if (get_bits(gb, 1) == 0)
1295 return (3 + get_bits(gb, 1));
1297 else if (get_bits(gb, 1) == 0)
1298 return (5 + get_bits(gb, 1));
1300 else if (get_bits(gb, 1) == 0)
1301 return (7 + get_bits(gb, 2));
1303 else if (get_bits(gb, 1) == 0)
1304 return (11 + get_bits(gb, 2));
1307 return (15 + get_bits(gb, 4));
1312 * This function decodes a VLC from the bitstream and returns a number
1313 * that ranges from 0..7. The number indicates which of the 8 coding
1327 static int get_mode_code(GetBitContext *gb)
1330 if (get_bits(gb, 1) == 0)
1333 else if (get_bits(gb, 1) == 0)
1336 else if (get_bits(gb, 1) == 0)
1339 else if (get_bits(gb, 1) == 0)
1342 else if (get_bits(gb, 1) == 0)
1345 else if (get_bits(gb, 1) == 0)
1348 else if (get_bits(gb, 1) == 0)
1357 * This function extracts a motion vector from the bitstream using a VLC
1358 * scheme. 3 bits are fetched from the bitstream and 1 of 8 actions is
1359 * taken depending on the value on those 3 bits:
1364 * 3: if (next bit is 1) return -2, else return 2
1365 * 4: if (next bit is 1) return -3, else return 3
1366 * 5: return 4 + (next 2 bits), next bit is sign
1367 * 6: return 8 + (next 3 bits), next bit is sign
1368 * 7: return 16 + (next 4 bits), next bit is sign
1370 static int get_motion_vector_vlc(GetBitContext *gb)
1374 bits = get_bits(gb, 3);
1391 if (get_bits(gb, 1) == 0)
1398 if (get_bits(gb, 1) == 0)
1405 bits = 4 + get_bits(gb, 2);
1406 if (get_bits(gb, 1) == 1)
1411 bits = 8 + get_bits(gb, 3);
1412 if (get_bits(gb, 1) == 1)
1417 bits = 16 + get_bits(gb, 4);
1418 if (get_bits(gb, 1) == 1)
1428 * This function fetches a 5-bit number from the stream followed by
1429 * a sign and calls it a motion vector.
1431 static int get_motion_vector_fixed(GetBitContext *gb)
1436 bits = get_bits(gb, 5);
1438 if (get_bits(gb, 1) == 1)
1445 * This function unpacks all of the superblock/macroblock/fragment coding
1446 * information from the bitstream.
1448 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
1451 int current_superblock = 0;
1452 int current_run = 0;
1453 int decode_fully_flags = 0;
1454 int decode_partial_blocks = 0;
1455 int first_c_fragment_seen;
1458 int current_fragment;
1460 debug_vp3(" vp3: unpacking superblock coding\n");
1464 debug_vp3(" keyframe-- all superblocks are fully coded\n");
1465 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
1469 /* unpack the list of partially-coded superblocks */
1470 bit = get_bits(gb, 1);
1471 /* toggle the bit because as soon as the first run length is
1472 * fetched the bit will be toggled again */
1474 while (current_superblock < s->superblock_count) {
1475 if (current_run == 0) {
1477 current_run = get_superblock_run_length(gb);
1478 debug_block_coding(" setting superblocks %d..%d to %s\n",
1480 current_superblock + current_run - 1,
1481 (bit) ? "partially coded" : "not coded");
1483 /* if any of the superblocks are not partially coded, flag
1484 * a boolean to decode the list of fully-coded superblocks */
1486 decode_fully_flags = 1;
1489 /* make a note of the fact that there are partially coded
1491 decode_partial_blocks = 1;
1494 s->superblock_coding[current_superblock++] =
1495 (bit) ? SB_PARTIALLY_CODED : SB_NOT_CODED;
1499 /* unpack the list of fully coded superblocks if any of the blocks were
1500 * not marked as partially coded in the previous step */
1501 if (decode_fully_flags) {
1503 current_superblock = 0;
1505 bit = get_bits(gb, 1);
1506 /* toggle the bit because as soon as the first run length is
1507 * fetched the bit will be toggled again */
1509 while (current_superblock < s->superblock_count) {
1511 /* skip any superblocks already marked as partially coded */
1512 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
1514 if (current_run == 0) {
1516 current_run = get_superblock_run_length(gb);
1519 debug_block_coding(" setting superblock %d to %s\n",
1521 (bit) ? "fully coded" : "not coded");
1522 s->superblock_coding[current_superblock] =
1523 (bit) ? SB_FULLY_CODED : SB_NOT_CODED;
1526 current_superblock++;
1530 /* if there were partial blocks, initialize bitstream for
1531 * unpacking fragment codings */
1532 if (decode_partial_blocks) {
1535 bit = get_bits(gb, 1);
1536 /* toggle the bit because as soon as the first run length is
1537 * fetched the bit will be toggled again */
1542 /* figure out which fragments are coded; iterate through each
1543 * superblock (all planes) */
1544 s->coded_fragment_list_index = 0;
1545 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
1546 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
1547 first_c_fragment_seen = 0;
1548 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
1549 for (i = 0; i < s->superblock_count; i++) {
1551 /* iterate through all 16 fragments in a superblock */
1552 for (j = 0; j < 16; j++) {
1554 /* if the fragment is in bounds, check its coding status */
1555 current_fragment = s->superblock_fragments[i * 16 + j];
1556 if (current_fragment >= s->fragment_count) {
1557 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
1558 current_fragment, s->fragment_count);
1561 if (current_fragment != -1) {
1562 if (s->superblock_coding[i] == SB_NOT_CODED) {
1564 /* copy all the fragments from the prior frame */
1565 s->all_fragments[current_fragment].coding_method =
1568 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
1570 /* fragment may or may not be coded; this is the case
1571 * that cares about the fragment coding runs */
1572 if (current_run == 0) {
1574 current_run = get_fragment_run_length(gb);
1578 /* default mode; actual mode will be decoded in
1580 s->all_fragments[current_fragment].coding_method =
1582 s->coded_fragment_list[s->coded_fragment_list_index] =
1584 if ((current_fragment >= s->u_fragment_start) &&
1585 (s->last_coded_y_fragment == -1) &&
1586 (!first_c_fragment_seen)) {
1587 s->first_coded_c_fragment = s->coded_fragment_list_index;
1588 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1589 first_c_fragment_seen = 1;
1591 s->coded_fragment_list_index++;
1592 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1593 debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
1594 i, current_fragment);
1596 /* not coded; copy this fragment from the prior frame */
1597 s->all_fragments[current_fragment].coding_method =
1599 debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
1600 i, current_fragment);
1607 /* fragments are fully coded in this superblock; actual
1608 * coding will be determined in next step */
1609 s->all_fragments[current_fragment].coding_method =
1611 s->coded_fragment_list[s->coded_fragment_list_index] =
1613 if ((current_fragment >= s->u_fragment_start) &&
1614 (s->last_coded_y_fragment == -1) &&
1615 (!first_c_fragment_seen)) {
1616 s->first_coded_c_fragment = s->coded_fragment_list_index;
1617 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1618 first_c_fragment_seen = 1;
1620 s->coded_fragment_list_index++;
1621 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1622 debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
1623 i, current_fragment);
1629 if (!first_c_fragment_seen)
1630 /* only Y fragments coded in this frame */
1631 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
1633 /* end the list of coded C fragments */
1634 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
1636 debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
1637 s->coded_fragment_list_index,
1638 s->first_coded_y_fragment,
1639 s->last_coded_y_fragment,
1640 s->first_coded_c_fragment,
1641 s->last_coded_c_fragment);
1647 * This function unpacks all the coding mode data for individual macroblocks
1648 * from the bitstream.
1650 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
1654 int current_macroblock;
1655 int current_fragment;
1658 debug_vp3(" vp3: unpacking encoding modes\n");
1661 debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
1663 for (i = 0; i < s->fragment_count; i++)
1664 s->all_fragments[i].coding_method = MODE_INTRA;
1668 /* fetch the mode coding scheme for this frame */
1669 scheme = get_bits(gb, 3);
1670 debug_modes(" using mode alphabet %d\n", scheme);
1672 /* is it a custom coding scheme? */
1674 debug_modes(" custom mode alphabet ahead:\n");
1675 for (i = 0; i < 8; i++)
1676 ModeAlphabet[scheme][get_bits(gb, 3)] = i;
1679 for (i = 0; i < 8; i++)
1680 debug_modes(" mode[%d][%d] = %d\n", scheme, i,
1681 ModeAlphabet[scheme][i]);
1683 /* iterate through all of the macroblocks that contain 1 or more
1684 * coded fragments */
1685 for (i = 0; i < s->u_superblock_start; i++) {
1687 for (j = 0; j < 4; j++) {
1688 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1689 if ((current_macroblock == -1) ||
1690 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1692 if (current_macroblock >= s->macroblock_count) {
1693 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
1694 current_macroblock, s->macroblock_count);
1698 /* mode 7 means get 3 bits for each coding mode */
1700 coding_mode = get_bits(gb, 3);
1702 coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
1704 s->macroblock_coding[current_macroblock] = coding_mode;
1705 for (k = 0; k < 6; k++) {
1707 s->macroblock_fragments[current_macroblock * 6 + k];
1708 if (current_fragment == -1)
1710 if (current_fragment >= s->fragment_count) {
1711 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
1712 current_fragment, s->fragment_count);
1715 if (s->all_fragments[current_fragment].coding_method !=
1717 s->all_fragments[current_fragment].coding_method =
1721 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
1722 s->macroblock_fragments[current_macroblock * 6], coding_mode);
1731 * This function unpacks all the motion vectors for the individual
1732 * macroblocks from the bitstream.
1734 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
1740 int last_motion_x = 0;
1741 int last_motion_y = 0;
1742 int prior_last_motion_x = 0;
1743 int prior_last_motion_y = 0;
1744 int current_macroblock;
1745 int current_fragment;
1747 debug_vp3(" vp3: unpacking motion vectors\n");
1750 debug_vp3(" keyframe-- there are no motion vectors\n");
1754 memset(motion_x, 0, 6 * sizeof(int));
1755 memset(motion_y, 0, 6 * sizeof(int));
1757 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
1758 coding_mode = get_bits(gb, 1);
1759 debug_vectors(" using %s scheme for unpacking motion vectors\n",
1760 (coding_mode == 0) ? "VLC" : "fixed-length");
1762 /* iterate through all of the macroblocks that contain 1 or more
1763 * coded fragments */
1764 for (i = 0; i < s->u_superblock_start; i++) {
1766 for (j = 0; j < 4; j++) {
1767 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1768 if ((current_macroblock == -1) ||
1769 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1771 if (current_macroblock >= s->macroblock_count) {
1772 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
1773 current_macroblock, s->macroblock_count);
1777 current_fragment = s->macroblock_fragments[current_macroblock * 6];
1778 if (current_fragment >= s->fragment_count) {
1779 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
1780 current_fragment, s->fragment_count);
1783 switch (s->macroblock_coding[current_macroblock]) {
1785 case MODE_INTER_PLUS_MV:
1786 case MODE_GOLDEN_MV:
1787 /* all 6 fragments use the same motion vector */
1788 if (coding_mode == 0) {
1789 motion_x[0] = get_motion_vector_vlc(gb);
1790 motion_y[0] = get_motion_vector_vlc(gb);
1792 motion_x[0] = get_motion_vector_fixed(gb);
1793 motion_y[0] = get_motion_vector_fixed(gb);
1795 for (k = 1; k < 6; k++) {
1796 motion_x[k] = motion_x[0];
1797 motion_y[k] = motion_y[0];
1800 /* vector maintenance, only on MODE_INTER_PLUS_MV */
1801 if (s->macroblock_coding[current_macroblock] ==
1802 MODE_INTER_PLUS_MV) {
1803 prior_last_motion_x = last_motion_x;
1804 prior_last_motion_y = last_motion_y;
1805 last_motion_x = motion_x[0];
1806 last_motion_y = motion_y[0];
1810 case MODE_INTER_FOURMV:
1811 /* fetch 4 vectors from the bitstream, one for each
1812 * Y fragment, then average for the C fragment vectors */
1813 motion_x[4] = motion_y[4] = 0;
1814 for (k = 0; k < 4; k++) {
1815 if (coding_mode == 0) {
1816 motion_x[k] = get_motion_vector_vlc(gb);
1817 motion_y[k] = get_motion_vector_vlc(gb);
1819 motion_x[k] = get_motion_vector_fixed(gb);
1820 motion_y[k] = get_motion_vector_fixed(gb);
1822 motion_x[4] += motion_x[k];
1823 motion_y[4] += motion_y[k];
1826 if (motion_x[4] >= 0)
1827 motion_x[4] = (motion_x[4] + 2) / 4;
1829 motion_x[4] = (motion_x[4] - 2) / 4;
1830 motion_x[5] = motion_x[4];
1832 if (motion_y[4] >= 0)
1833 motion_y[4] = (motion_y[4] + 2) / 4;
1835 motion_y[4] = (motion_y[4] - 2) / 4;
1836 motion_y[5] = motion_y[4];
1838 /* vector maintenance; vector[3] is treated as the
1839 * last vector in this case */
1840 prior_last_motion_x = last_motion_x;
1841 prior_last_motion_y = last_motion_y;
1842 last_motion_x = motion_x[3];
1843 last_motion_y = motion_y[3];
1846 case MODE_INTER_LAST_MV:
1847 /* all 6 fragments use the last motion vector */
1848 motion_x[0] = last_motion_x;
1849 motion_y[0] = last_motion_y;
1850 for (k = 1; k < 6; k++) {
1851 motion_x[k] = motion_x[0];
1852 motion_y[k] = motion_y[0];
1855 /* no vector maintenance (last vector remains the
1859 case MODE_INTER_PRIOR_LAST:
1860 /* all 6 fragments use the motion vector prior to the
1861 * last motion vector */
1862 motion_x[0] = prior_last_motion_x;
1863 motion_y[0] = prior_last_motion_y;
1864 for (k = 1; k < 6; k++) {
1865 motion_x[k] = motion_x[0];
1866 motion_y[k] = motion_y[0];
1869 /* vector maintenance */
1870 prior_last_motion_x = last_motion_x;
1871 prior_last_motion_y = last_motion_y;
1872 last_motion_x = motion_x[0];
1873 last_motion_y = motion_y[0];
1877 /* covers intra, inter without MV, golden without MV */
1878 memset(motion_x, 0, 6 * sizeof(int));
1879 memset(motion_y, 0, 6 * sizeof(int));
1881 /* no vector maintenance */
1885 /* assign the motion vectors to the correct fragments */
1886 debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
1888 s->macroblock_coding[current_macroblock]);
1889 for (k = 0; k < 6; k++) {
1891 s->macroblock_fragments[current_macroblock * 6 + k];
1892 if (current_fragment == -1)
1894 if (current_fragment >= s->fragment_count) {
1895 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
1896 current_fragment, s->fragment_count);
1899 s->all_fragments[current_fragment].motion_x = motion_x[k];
1900 s->all_fragments[current_fragment].motion_y = motion_y[k];
1901 debug_vectors(" vector %d: fragment %d = (%d, %d)\n",
1902 k, current_fragment, motion_x[k], motion_y[k]);
1912 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1913 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1914 * data. This function unpacks all the VLCs for either the Y plane or both
1915 * C planes, and is called for DC coefficients or different AC coefficient
1916 * levels (since different coefficient types require different VLC tables.
1918 * This function returns a residual eob run. E.g, if a particular token gave
1919 * instructions to EOB the next 5 fragments and there were only 2 fragments
1920 * left in the current fragment range, 3 would be returned so that it could
1921 * be passed into the next call to this same function.
1923 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1924 VLC *table, int coeff_index,
1925 int first_fragment, int last_fragment,
1932 Vp3Fragment *fragment;
1934 if ((first_fragment >= s->fragment_count) ||
1935 (last_fragment >= s->fragment_count)) {
1937 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
1938 first_fragment, last_fragment);
1942 for (i = first_fragment; i <= last_fragment; i++) {
1944 fragment = &s->all_fragments[s->coded_fragment_list[i]];
1945 if (fragment->coeff_count > coeff_index)
1949 /* decode a VLC into a token */
1950 token = get_vlc2(gb, table->table, 5, 3);
1951 debug_vlc(" token = %2d, ", token);
1952 /* use the token to get a zero run, a coefficient, and an eob run */
1953 unpack_token(gb, token, &zero_run, &coeff, &eob_run);
1957 fragment->coeff_count += zero_run;
1958 if (fragment->coeff_count < 64)
1959 fragment->coeffs[fragment->coeff_count++] = coeff;
1960 debug_vlc(" fragment %d coeff = %d\n",
1961 s->coded_fragment_list[i], fragment->coeffs[coeff_index]);
1963 fragment->last_coeff = fragment->coeff_count;
1964 fragment->coeff_count = 64;
1965 debug_vlc(" fragment %d eob with %d coefficients\n",
1966 s->coded_fragment_list[i], fragment->last_coeff);
1975 * This function unpacks all of the DCT coefficient data from the
1978 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1985 int residual_eob_run = 0;
1987 /* fetch the DC table indices */
1988 dc_y_table = get_bits(gb, 4);
1989 dc_c_table = get_bits(gb, 4);
1991 /* unpack the Y plane DC coefficients */
1992 debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
1994 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1995 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1997 /* unpack the C plane DC coefficients */
1998 debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
2000 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
2001 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
2003 /* fetch the AC table indices */
2004 ac_y_table = get_bits(gb, 4);
2005 ac_c_table = get_bits(gb, 4);
2007 /* unpack the group 1 AC coefficients (coeffs 1-5) */
2008 for (i = 1; i <= 5; i++) {
2010 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
2012 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
2013 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
2015 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
2017 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
2018 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
2021 /* unpack the group 2 AC coefficients (coeffs 6-14) */
2022 for (i = 6; i <= 14; i++) {
2024 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
2026 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
2027 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
2029 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
2031 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
2032 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
2035 /* unpack the group 3 AC coefficients (coeffs 15-27) */
2036 for (i = 15; i <= 27; i++) {
2038 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
2040 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
2041 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
2043 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
2045 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
2046 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
2049 /* unpack the group 4 AC coefficients (coeffs 28-63) */
2050 for (i = 28; i <= 63; i++) {
2052 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
2054 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
2055 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
2057 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
2059 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
2060 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
2067 * This function reverses the DC prediction for each coded fragment in
2068 * the frame. Much of this function is adapted directly from the original
2071 #define COMPATIBLE_FRAME(x) \
2072 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
2073 #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
2074 static inline int iabs (int x) { return ((x < 0) ? -x : x); }
2076 static void reverse_dc_prediction(Vp3DecodeContext *s,
2079 int fragment_height)
2088 int i = first_fragment;
2091 * Fragment prediction groups:
2099 * Note: Groups 5 and 7 do not exist as it would mean that the
2100 * fragment's x coordinate is both 0 and (width - 1) at the same time.
2102 int predictor_group;
2105 /* validity flags for the left, up-left, up, and up-right fragments */
2106 int fl, ful, fu, fur;
2108 /* DC values for the left, up-left, up, and up-right fragments */
2109 int vl, vul, vu, vur;
2111 /* indices for the left, up-left, up, and up-right fragments */
2115 * The 6 fields mean:
2116 * 0: up-left multiplier
2118 * 2: up-right multiplier
2119 * 3: left multiplier
2121 * 5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
2123 int predictor_transform[16][6] = {
2124 { 0, 0, 0, 0, 0, 0 },
2125 { 0, 0, 0, 1, 0, 0 }, // PL
2126 { 0, 0, 1, 0, 0, 0 }, // PUR
2127 { 0, 0, 53, 75, 127, 7 }, // PUR|PL
2128 { 0, 1, 0, 0, 0, 0 }, // PU
2129 { 0, 1, 0, 1, 1, 1 }, // PU|PL
2130 { 0, 1, 0, 0, 0, 0 }, // PU|PUR
2131 { 0, 0, 53, 75, 127, 7 }, // PU|PUR|PL
2132 { 1, 0, 0, 0, 0, 0 }, // PUL
2133 { 0, 0, 0, 1, 0, 0 }, // PUL|PL
2134 { 1, 0, 1, 0, 1, 1 }, // PUL|PUR
2135 { 0, 0, 53, 75, 127, 7 }, // PUL|PUR|PL
2136 { 0, 1, 0, 0, 0, 0 }, // PUL|PU
2137 {-26, 29, 0, 29, 31, 5 }, // PUL|PU|PL
2138 { 3, 10, 3, 0, 15, 4 }, // PUL|PU|PUR
2139 {-26, 29, 0, 29, 31, 5 } // PUL|PU|PUR|PL
2142 /* This table shows which types of blocks can use other blocks for
2143 * prediction. For example, INTRA is the only mode in this table to
2144 * have a frame number of 0. That means INTRA blocks can only predict
2145 * from other INTRA blocks. There are 2 golden frame coding types;
2146 * blocks encoding in these modes can only predict from other blocks
2147 * that were encoded with these 1 of these 2 modes. */
2148 unsigned char compatible_frame[8] = {
2149 1, /* MODE_INTER_NO_MV */
2151 1, /* MODE_INTER_PLUS_MV */
2152 1, /* MODE_INTER_LAST_MV */
2153 1, /* MODE_INTER_PRIOR_MV */
2154 2, /* MODE_USING_GOLDEN */
2155 2, /* MODE_GOLDEN_MV */
2156 1 /* MODE_INTER_FOUR_MV */
2158 int current_frame_type;
2160 /* there is a last DC predictor for each of the 3 frame types */
2165 debug_vp3(" vp3: reversing DC prediction\n");
2167 vul = vu = vur = vl = 0;
2168 last_dc[0] = last_dc[1] = last_dc[2] = 0;
2170 /* for each fragment row... */
2171 for (y = 0; y < fragment_height; y++) {
2173 /* for each fragment in a row... */
2174 for (x = 0; x < fragment_width; x++, i++) {
2176 /* reverse prediction if this block was coded */
2177 if (s->all_fragments[i].coding_method != MODE_COPY) {
2179 current_frame_type =
2180 compatible_frame[s->all_fragments[i].coding_method];
2181 predictor_group = (x == 0) + ((y == 0) << 1) +
2182 ((x + 1 == fragment_width) << 2);
2183 debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
2184 i, predictor_group, s->all_fragments[i].coeffs[0]);
2186 switch (predictor_group) {
2189 /* main body of fragments; consider all 4 possible
2190 * fragments for prediction */
2192 /* calculate the indices of the predicting fragments */
2193 ul = i - fragment_width - 1;
2194 u = i - fragment_width;
2195 ur = i - fragment_width + 1;
2198 /* fetch the DC values for the predicting fragments */
2199 vul = s->all_fragments[ul].coeffs[0];
2200 vu = s->all_fragments[u].coeffs[0];
2201 vur = s->all_fragments[ur].coeffs[0];
2202 vl = s->all_fragments[l].coeffs[0];
2204 /* figure out which fragments are valid */
2205 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
2206 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
2207 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
2208 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
2210 /* decide which predictor transform to use */
2211 transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
2216 /* left column of fragments, not including top corner;
2217 * only consider up and up-right fragments */
2219 /* calculate the indices of the predicting fragments */
2220 u = i - fragment_width;
2221 ur = i - fragment_width + 1;
2223 /* fetch the DC values for the predicting fragments */
2224 vu = s->all_fragments[u].coeffs[0];
2225 vur = s->all_fragments[ur].coeffs[0];
2227 /* figure out which fragments are valid */
2228 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
2229 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
2231 /* decide which predictor transform to use */
2232 transform = (fu*PU) | (fur*PUR);
2238 /* top row of fragments, not including top-left frag;
2239 * only consider the left fragment for prediction */
2241 /* calculate the indices of the predicting fragments */
2244 /* fetch the DC values for the predicting fragments */
2245 vl = s->all_fragments[l].coeffs[0];
2247 /* figure out which fragments are valid */
2248 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
2250 /* decide which predictor transform to use */
2251 transform = (fl*PL);
2256 /* top-left fragment */
2258 /* nothing to predict from in this case */
2264 /* right column of fragments, not including top corner;
2265 * consider up-left, up, and left fragments for
2268 /* calculate the indices of the predicting fragments */
2269 ul = i - fragment_width - 1;
2270 u = i - fragment_width;
2273 /* fetch the DC values for the predicting fragments */
2274 vul = s->all_fragments[ul].coeffs[0];
2275 vu = s->all_fragments[u].coeffs[0];
2276 vl = s->all_fragments[l].coeffs[0];
2278 /* figure out which fragments are valid */
2279 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
2280 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
2281 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
2283 /* decide which predictor transform to use */
2284 transform = (fl*PL) | (fu*PU) | (ful*PUL);
2290 debug_dc_pred("transform = %d, ", transform);
2292 if (transform == 0) {
2294 /* if there were no fragments to predict from, use last
2296 s->all_fragments[i].coeffs[0] += last_dc[current_frame_type];
2297 debug_dc_pred("from last DC (%d) = %d\n",
2298 current_frame_type, s->all_fragments[i].coeffs[0]);
2302 /* apply the appropriate predictor transform */
2304 (predictor_transform[transform][0] * vul) +
2305 (predictor_transform[transform][1] * vu) +
2306 (predictor_transform[transform][2] * vur) +
2307 (predictor_transform[transform][3] * vl);
2309 /* if there is a shift value in the transform, add
2310 * the sign bit before the shift */
2311 if (predictor_transform[transform][5] != 0) {
2312 predicted_dc += ((predicted_dc >> 15) &
2313 predictor_transform[transform][4]);
2314 predicted_dc >>= predictor_transform[transform][5];
2317 /* check for outranging on the [ul u l] and
2318 * [ul u ur l] predictors */
2319 if ((transform == 13) || (transform == 15)) {
2320 if (iabs(predicted_dc - vu) > 128)
2322 else if (iabs(predicted_dc - vl) > 128)
2324 else if (iabs(predicted_dc - vul) > 128)
2328 /* at long last, apply the predictor */
2329 s->all_fragments[i].coeffs[0] += predicted_dc;
2330 debug_dc_pred("from pred DC = %d\n",
2331 s->all_fragments[i].coeffs[0]);
2335 last_dc[current_frame_type] = s->all_fragments[i].coeffs[0];
2342 * This function performs the final rendering of each fragment's data
2343 * onto the output frame.
2345 static void render_fragments(Vp3DecodeContext *s,
2349 int plane /* 0 = Y, 1 = U, 2 = V */)
2353 int i = first_fragment;
2354 int16_t *dequantizer;
2355 unsigned char *output_plane;
2356 unsigned char *last_plane;
2357 unsigned char *golden_plane;
2359 int motion_x, motion_y;
2360 int upper_motion_limit, lower_motion_limit;
2361 int motion_halfpel_index;
2362 uint8_t *motion_source;
2364 debug_vp3(" vp3: rendering final fragments for %s\n",
2365 (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
2367 /* set up plane-specific parameters */
2369 dequantizer = s->intra_y_dequant;
2370 output_plane = s->current_frame.data[0];
2371 last_plane = s->last_frame.data[0];
2372 golden_plane = s->golden_frame.data[0];
2373 stride = s->current_frame.linesize[0];
2374 if (!s->flipped_image) stride = -stride;
2375 upper_motion_limit = 7 * s->current_frame.linesize[0];
2376 lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
2377 } else if (plane == 1) {
2378 dequantizer = s->intra_c_dequant;
2379 output_plane = s->current_frame.data[1];
2380 last_plane = s->last_frame.data[1];
2381 golden_plane = s->golden_frame.data[1];
2382 stride = s->current_frame.linesize[1];
2383 if (!s->flipped_image) stride = -stride;
2384 upper_motion_limit = 7 * s->current_frame.linesize[1];
2385 lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
2387 dequantizer = s->intra_c_dequant;
2388 output_plane = s->current_frame.data[2];
2389 last_plane = s->last_frame.data[2];
2390 golden_plane = s->golden_frame.data[2];
2391 stride = s->current_frame.linesize[2];
2392 if (!s->flipped_image) stride = -stride;
2393 upper_motion_limit = 7 * s->current_frame.linesize[2];
2394 lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
2397 /* for each fragment row... */
2398 for (y = 0; y < height; y += 8) {
2400 /* for each fragment in a row... */
2401 for (x = 0; x < width; x += 8, i++) {
2403 if ((i < 0) || (i >= s->fragment_count)) {
2404 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_fragments(): bad fragment number (%d)\n", i);
2408 /* transform if this block was coded */
2409 if ((s->all_fragments[i].coding_method != MODE_COPY) &&
2410 !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
2412 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2413 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2414 motion_source= golden_plane;
2416 motion_source= last_plane;
2418 motion_source += s->all_fragments[i].first_pixel;
2419 motion_halfpel_index = 0;
2421 /* sort out the motion vector if this fragment is coded
2422 * using a motion vector method */
2423 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2424 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2426 motion_x = s->all_fragments[i].motion_x;
2427 motion_y = s->all_fragments[i].motion_y;
2429 motion_x= (motion_x>>1) | (motion_x&1);
2430 motion_y= (motion_y>>1) | (motion_y&1);
2433 src_x= (motion_x>>1) + x;
2434 src_y= (motion_y>>1) + y;
2435 if ((motion_x == 0xbeef) || (motion_y == 0xbeef))
2436 av_log(s->avctx, AV_LOG_ERROR, " help! got beefy vector! (%X, %X)\n", motion_x, motion_y);
2438 motion_halfpel_index = motion_x & 0x01;
2439 motion_source += (motion_x >> 1);
2441 // motion_y = -motion_y;
2442 motion_halfpel_index |= (motion_y & 0x01) << 1;
2443 motion_source += ((motion_y >> 1) * stride);
2445 if(src_x<0 || src_y<0 || src_x + 9 >= width || src_y + 9 >= height){
2446 uint8_t *temp= s->edge_emu_buffer;
2447 if(stride<0) temp -= 9*stride;
2448 else temp += 9*stride;
2450 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, width, height);
2451 motion_source= temp;
2456 /* first, take care of copying a block from either the
2457 * previous or the golden frame */
2458 if (s->all_fragments[i].coding_method != MODE_INTRA) {
2459 //Note, it is possible to implement all MC cases with put_no_rnd_pixels_l2 which would look more like the VP3 source but this would be slower as put_no_rnd_pixels_tab is better optimzed
2460 if(motion_halfpel_index != 3){
2461 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
2462 output_plane + s->all_fragments[i].first_pixel,
2463 motion_source, stride, 8);
2465 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
2466 s->dsp.put_no_rnd_pixels_l2[1](
2467 output_plane + s->all_fragments[i].first_pixel,
2469 motion_source + stride + 1 + d,
2474 /* dequantize the DCT coefficients */
2475 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
2476 i, s->all_fragments[i].coding_method,
2477 s->all_fragments[i].coeffs[0], dequantizer[0]);
2479 /* invert DCT and place (or add) in final output */
2480 if (s->all_fragments[i].coding_method == MODE_INTRA) {
2481 vp3_idct_put(s->all_fragments[i].coeffs, dequantizer,
2482 output_plane + s->all_fragments[i].first_pixel,
2485 vp3_idct_add(s->all_fragments[i].coeffs, dequantizer,
2486 output_plane + s->all_fragments[i].first_pixel,
2490 debug_idct("block after idct_%s():\n",
2491 (s->all_fragments[i].coding_method == MODE_INTRA)?
2493 for (m = 0; m < 8; m++) {
2494 for (n = 0; n < 8; n++) {
2495 debug_idct(" %3d", *(output_plane +
2496 s->all_fragments[i].first_pixel + (m * stride + n)));
2504 /* copy directly from the previous frame */
2505 s->dsp.put_pixels_tab[1][0](
2506 output_plane + s->all_fragments[i].first_pixel,
2507 last_plane + s->all_fragments[i].first_pixel,
2519 * This function computes the first pixel addresses for each fragment.
2520 * This function needs to be invoked after the first frame is allocated
2521 * so that it has access to the plane strides.
2523 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
2528 /* figure out the first pixel addresses for each of the fragments */
2531 for (y = s->fragment_height; y > 0; y--) {
2532 for (x = 0; x < s->fragment_width; x++) {
2533 s->all_fragments[i++].first_pixel =
2534 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2535 s->golden_frame.linesize[0] +
2536 x * FRAGMENT_PIXELS;
2537 debug_init(" fragment %d, first pixel @ %d\n",
2538 i-1, s->all_fragments[i-1].first_pixel);
2543 i = s->u_fragment_start;
2544 for (y = s->fragment_height / 2; y > 0; y--) {
2545 for (x = 0; x < s->fragment_width / 2; x++) {
2546 s->all_fragments[i++].first_pixel =
2547 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2548 s->golden_frame.linesize[1] +
2549 x * FRAGMENT_PIXELS;
2550 debug_init(" fragment %d, first pixel @ %d\n",
2551 i-1, s->all_fragments[i-1].first_pixel);
2556 i = s->v_fragment_start;
2557 for (y = s->fragment_height / 2; y > 0; y--) {
2558 for (x = 0; x < s->fragment_width / 2; x++) {
2559 s->all_fragments[i++].first_pixel =
2560 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2561 s->golden_frame.linesize[2] +
2562 x * FRAGMENT_PIXELS;
2563 debug_init(" fragment %d, first pixel @ %d\n",
2564 i-1, s->all_fragments[i-1].first_pixel);
2569 /* FIXME: this should be merged with the above! */
2570 static void theora_calculate_pixel_addresses(Vp3DecodeContext *s)
2575 /* figure out the first pixel addresses for each of the fragments */
2578 for (y = 1; y <= s->fragment_height; y++) {
2579 for (x = 0; x < s->fragment_width; x++) {
2580 s->all_fragments[i++].first_pixel =
2581 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2582 s->golden_frame.linesize[0] +
2583 x * FRAGMENT_PIXELS;
2584 debug_init(" fragment %d, first pixel @ %d\n",
2585 i-1, s->all_fragments[i-1].first_pixel);
2590 i = s->u_fragment_start;
2591 for (y = 1; y <= s->fragment_height / 2; y++) {
2592 for (x = 0; x < s->fragment_width / 2; x++) {
2593 s->all_fragments[i++].first_pixel =
2594 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2595 s->golden_frame.linesize[1] +
2596 x * FRAGMENT_PIXELS;
2597 debug_init(" fragment %d, first pixel @ %d\n",
2598 i-1, s->all_fragments[i-1].first_pixel);
2603 i = s->v_fragment_start;
2604 for (y = 1; y <= s->fragment_height / 2; y++) {
2605 for (x = 0; x < s->fragment_width / 2; x++) {
2606 s->all_fragments[i++].first_pixel =
2607 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2608 s->golden_frame.linesize[2] +
2609 x * FRAGMENT_PIXELS;
2610 debug_init(" fragment %d, first pixel @ %d\n",
2611 i-1, s->all_fragments[i-1].first_pixel);
2617 * This is the ffmpeg/libavcodec API init function.
2619 static int vp3_decode_init(AVCodecContext *avctx)
2621 Vp3DecodeContext *s = avctx->priv_data;
2625 int y_superblock_count;
2626 int c_superblock_count;
2628 if (avctx->codec_tag == MKTAG('V','P','3','0'))
2635 s->width = avctx->width;
2636 s->height = avctx->height;
2638 s->width = (avctx->width + 15) & 0xFFFFFFF0;
2639 s->height = (avctx->height + 15) & 0xFFFFFFF0;
2641 avctx->pix_fmt = PIX_FMT_YUV420P;
2642 avctx->has_b_frames = 0;
2643 dsputil_init(&s->dsp, avctx);
2645 /* initialize to an impossible value which will force a recalculation
2646 * in the first frame decode */
2647 s->quality_index = -1;
2649 s->y_superblock_width = (s->width + 31) / 32;
2650 s->y_superblock_height = (s->height + 31) / 32;
2651 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2653 /* work out the dimensions for the C planes */
2654 c_width = s->width / 2;
2655 c_height = s->height / 2;
2656 s->c_superblock_width = (c_width + 31) / 32;
2657 s->c_superblock_height = (c_height + 31) / 32;
2658 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2660 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
2661 s->u_superblock_start = y_superblock_count;
2662 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
2663 s->superblock_coding = av_malloc(s->superblock_count);
2665 s->macroblock_width = (s->width + 15) / 16;
2666 s->macroblock_height = (s->height + 15) / 16;
2667 s->macroblock_count = s->macroblock_width * s->macroblock_height;
2669 s->fragment_width = s->width / FRAGMENT_PIXELS;
2670 s->fragment_height = s->height / FRAGMENT_PIXELS;
2672 /* fragment count covers all 8x8 blocks for all 3 planes */
2673 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
2674 s->u_fragment_start = s->fragment_width * s->fragment_height;
2675 s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
2677 debug_init(" Y plane: %d x %d\n", s->width, s->height);
2678 debug_init(" C plane: %d x %d\n", c_width, c_height);
2679 debug_init(" Y superblocks: %d x %d, %d total\n",
2680 s->y_superblock_width, s->y_superblock_height, y_superblock_count);
2681 debug_init(" C superblocks: %d x %d, %d total\n",
2682 s->c_superblock_width, s->c_superblock_height, c_superblock_count);
2683 debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
2684 s->superblock_count, s->u_superblock_start, s->v_superblock_start);
2685 debug_init(" macroblocks: %d x %d, %d total\n",
2686 s->macroblock_width, s->macroblock_height, s->macroblock_count);
2687 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
2691 s->u_fragment_start,
2692 s->v_fragment_start);
2694 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
2695 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
2696 s->pixel_addresses_inited = 0;
2698 if (!s->theora_tables)
2700 for (i = 0; i < 64; i++)
2701 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
2702 for (i = 0; i < 64; i++)
2703 s->coded_quality_threshold[i] = vp31_quality_threshold[i];
2704 for (i = 0; i < 64; i++)
2705 s->coded_intra_y_dequant[i] = vp31_intra_y_dequant[i];
2706 for (i = 0; i < 64; i++)
2707 s->coded_intra_c_dequant[i] = vp31_intra_c_dequant[i];
2708 for (i = 0; i < 64; i++)
2709 s->coded_inter_dequant[i] = vp31_inter_dequant[i];
2712 /* init VLC tables */
2713 for (i = 0; i < 16; i++) {
2716 init_vlc(&s->dc_vlc[i], 5, 32,
2717 &dc_bias[i][0][1], 4, 2,
2718 &dc_bias[i][0][0], 4, 2);
2720 /* group 1 AC histograms */
2721 init_vlc(&s->ac_vlc_1[i], 5, 32,
2722 &ac_bias_0[i][0][1], 4, 2,
2723 &ac_bias_0[i][0][0], 4, 2);
2725 /* group 2 AC histograms */
2726 init_vlc(&s->ac_vlc_2[i], 5, 32,
2727 &ac_bias_1[i][0][1], 4, 2,
2728 &ac_bias_1[i][0][0], 4, 2);
2730 /* group 3 AC histograms */
2731 init_vlc(&s->ac_vlc_3[i], 5, 32,
2732 &ac_bias_2[i][0][1], 4, 2,
2733 &ac_bias_2[i][0][0], 4, 2);
2735 /* group 4 AC histograms */
2736 init_vlc(&s->ac_vlc_4[i], 5, 32,
2737 &ac_bias_3[i][0][1], 4, 2,
2738 &ac_bias_3[i][0][0], 4, 2);
2741 /* build quantization zigzag table */
2742 for (i = 0; i < 64; i++)
2743 zigzag_index[dezigzag_index[i]] = i;
2745 /* work out the block mapping tables */
2746 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
2747 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
2748 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
2749 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
2750 init_block_mapping(s);
2752 for (i = 0; i < 3; i++) {
2753 s->current_frame.data[i] = NULL;
2754 s->last_frame.data[i] = NULL;
2755 s->golden_frame.data[i] = NULL;
2762 * This is the ffmpeg/libavcodec API frame decode function.
2764 static int vp3_decode_frame(AVCodecContext *avctx,
2765 void *data, int *data_size,
2766 uint8_t *buf, int buf_size)
2768 Vp3DecodeContext *s = avctx->priv_data;
2770 static int counter = 0;
2774 init_get_bits(&gb, buf, buf_size * 8);
2776 if (s->theora && get_bits1(&gb))
2778 int ptype = get_bits(&gb, 7);
2780 skip_bits(&gb, 6*8); /* "theora" */
2785 theora_decode_comments(avctx, gb);
2788 theora_decode_tables(avctx, gb);
2789 init_dequantizer(s);
2792 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype);
2797 s->keyframe = !get_bits1(&gb);
2800 s->last_quality_index = s->quality_index;
2801 s->quality_index = get_bits(&gb, 6);
2802 if (s->theora >= 0x030300)
2805 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2806 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2807 s->keyframe?"key":"", counter, s->quality_index);
2810 if (s->quality_index != s->last_quality_index)
2811 init_dequantizer(s);
2816 skip_bits(&gb, 4); /* width code */
2817 skip_bits(&gb, 4); /* height code */
2820 s->version = get_bits(&gb, 5);
2822 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
2825 if (s->version || s->theora)
2828 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
2829 skip_bits(&gb, 2); /* reserved? */
2832 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
2833 if (s->golden_frame.data[0])
2834 avctx->release_buffer(avctx, &s->golden_frame);
2835 s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
2837 if (s->golden_frame.data[0])
2838 avctx->release_buffer(avctx, &s->golden_frame);
2839 if (s->last_frame.data[0])
2840 avctx->release_buffer(avctx, &s->last_frame);
2843 s->golden_frame.reference = 3;
2844 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
2845 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2849 /* golden frame is also the current frame */
2850 memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
2852 /* time to figure out pixel addresses? */
2853 if (!s->pixel_addresses_inited)
2855 if (!s->flipped_image)
2856 vp3_calculate_pixel_addresses(s);
2858 theora_calculate_pixel_addresses(s);
2861 /* allocate a new current frame */
2862 s->current_frame.reference = 3;
2863 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
2864 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2869 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
2870 s->current_frame.qstride= 0;
2877 memcpy(s->current_frame.data[0], s->golden_frame.data[0],
2878 s->current_frame.linesize[0] * s->height);
2879 memcpy(s->current_frame.data[1], s->golden_frame.data[1],
2880 s->current_frame.linesize[1] * s->height / 2);
2881 memcpy(s->current_frame.data[2], s->golden_frame.data[2],
2882 s->current_frame.linesize[2] * s->height / 2);
2887 if (unpack_superblocks(s, &gb) ||
2888 unpack_modes(s, &gb) ||
2889 unpack_vectors(s, &gb) ||
2890 unpack_dct_coeffs(s, &gb)) {
2892 av_log(s->avctx, AV_LOG_ERROR, " vp3: could not decode frame\n");
2896 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
2897 render_fragments(s, 0, s->width, s->height, 0);
2899 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
2900 reverse_dc_prediction(s, s->u_fragment_start,
2901 s->fragment_width / 2, s->fragment_height / 2);
2902 reverse_dc_prediction(s, s->v_fragment_start,
2903 s->fragment_width / 2, s->fragment_height / 2);
2904 render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
2905 render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
2907 memset(s->current_frame.data[1], 0x80, s->width * s->height / 4);
2908 memset(s->current_frame.data[2], 0x80, s->width * s->height / 4);
2915 *data_size=sizeof(AVFrame);
2916 *(AVFrame*)data= s->current_frame;
2918 /* release the last frame, if it is allocated and if it is not the
2920 if ((s->last_frame.data[0]) &&
2921 (s->last_frame.data[0] != s->golden_frame.data[0]))
2922 avctx->release_buffer(avctx, &s->last_frame);
2924 /* shuffle frames (last = current) */
2925 memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
2926 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
2932 * This is the ffmpeg/libavcodec API module cleanup function.
2934 static int vp3_decode_end(AVCodecContext *avctx)
2936 Vp3DecodeContext *s = avctx->priv_data;
2938 av_free(s->all_fragments);
2939 av_free(s->coded_fragment_list);
2940 av_free(s->superblock_fragments);
2941 av_free(s->superblock_macroblocks);
2942 av_free(s->macroblock_fragments);
2943 av_free(s->macroblock_coding);
2945 /* release all frames */
2946 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
2947 avctx->release_buffer(avctx, &s->golden_frame);
2948 if (s->last_frame.data[0])
2949 avctx->release_buffer(avctx, &s->last_frame);
2950 /* no need to release the current_frame since it will always be pointing
2951 * to the same frame as either the golden or last frame */
2956 static int theora_decode_header(AVCodecContext *avctx, GetBitContext gb)
2958 Vp3DecodeContext *s = avctx->priv_data;
2959 int major, minor, micro;
2961 major = get_bits(&gb, 8); /* version major */
2962 minor = get_bits(&gb, 8); /* version minor */
2963 micro = get_bits(&gb, 8); /* version micro */
2964 av_log(avctx, AV_LOG_INFO, "Theora bitstream version %d.%d.%d\n",
2965 major, minor, micro);
2967 /* FIXME: endianess? */
2968 s->theora = (major << 16) | (minor << 8) | micro;
2970 /* 3.3.0 aka alpha3 has the same frame orientation as original vp3 */
2971 /* but previous versions have the image flipped relative to vp3 */
2972 if (s->theora < 0x030300)
2974 s->flipped_image = 1;
2975 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
2978 s->width = get_bits(&gb, 16) << 4;
2979 s->height = get_bits(&gb, 16) << 4;
2981 skip_bits(&gb, 24); /* frame width */
2982 skip_bits(&gb, 24); /* frame height */
2984 skip_bits(&gb, 8); /* offset x */
2985 skip_bits(&gb, 8); /* offset y */
2987 skip_bits(&gb, 32); /* fps numerator */
2988 skip_bits(&gb, 32); /* fps denumerator */
2989 skip_bits(&gb, 24); /* aspect numerator */
2990 skip_bits(&gb, 24); /* aspect denumerator */
2992 if (s->theora < 0x030300)
2993 skip_bits(&gb, 5); /* keyframe frequency force */
2994 skip_bits(&gb, 8); /* colorspace */
2995 skip_bits(&gb, 24); /* bitrate */
2997 skip_bits(&gb, 6); /* last(?) quality index */
2999 if (s->theora >= 0x030300)
3001 skip_bits(&gb, 5); /* keyframe frequency force */
3002 skip_bits(&gb, 5); /* spare bits */
3005 // align_get_bits(&gb);
3007 avctx->width = s->width;
3008 avctx->height = s->height;
3010 vp3_decode_init(avctx);
3015 static int theora_decode_comments(AVCodecContext *avctx, GetBitContext gb)
3017 int nb_comments, i, tmp;
3019 tmp = get_bits(&gb, 32);
3020 tmp = be2me_32(tmp);
3024 nb_comments = get_bits(&gb, 32);
3025 nb_comments = be2me_32(nb_comments);
3026 for (i = 0; i < nb_comments; i++)
3028 tmp = get_bits(&gb, 32);
3029 tmp = be2me_32(tmp);
3037 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb)
3039 Vp3DecodeContext *s = avctx->priv_data;
3042 /* quality threshold table */
3043 for (i = 0; i < 64; i++)
3044 s->coded_quality_threshold[i] = get_bits(&gb, 16);
3046 /* dc scale factor table */
3047 for (i = 0; i < 64; i++)
3048 s->coded_dc_scale_factor[i] = get_bits(&gb, 16);
3051 for (i = 0; i < 64; i++)
3052 s->coded_intra_y_dequant[i] = get_bits(&gb, 8);
3055 for (i = 0; i < 64; i++)
3056 s->coded_intra_c_dequant[i] = get_bits(&gb, 8);
3059 for (i = 0; i < 64; i++)
3060 s->coded_inter_dequant[i] = get_bits(&gb, 8);
3062 /* FIXME: read huffmann tree.. */
3064 s->theora_tables = 1;
3069 static int theora_decode_init(AVCodecContext *avctx)
3071 Vp3DecodeContext *s = avctx->priv_data;
3077 if (!avctx->extradata_size)
3080 init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
3082 ptype = get_bits(&gb, 8);
3083 debug_vp3("Theora headerpacket type: %x\n", ptype);
3085 if (!(ptype & 0x80))
3088 skip_bits(&gb, 6*8); /* "theora" */
3093 theora_decode_header(avctx, gb);
3094 vp3_decode_init(avctx);
3097 theora_decode_comments(avctx, gb);
3100 theora_decode_tables(avctx, gb);
3107 AVCodec vp3_decoder = {
3111 sizeof(Vp3DecodeContext),
3120 AVCodec theora_decoder = {
3124 sizeof(Vp3DecodeContext),