2 * Copyright (C) 2003-2004 the ffmpeg project
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * VP3 Video Decoder by Mike Melanson (melanson@pcisys.net)
19 * For more information about the VP3 coding process, visit:
20 * http://www.pcisys.net/~melanson/codecs/
22 * Theora decoder by Alex Beregszaszi
28 * On2 VP3 Video Decoder
39 #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;
216 int theora, theora_tables;
219 AVFrame golden_frame;
221 AVFrame current_frame;
227 int last_quality_index;
229 int superblock_count;
230 int superblock_width;
231 int superblock_height;
232 int y_superblock_width;
233 int y_superblock_height;
234 int c_superblock_width;
235 int c_superblock_height;
236 int u_superblock_start;
237 int v_superblock_start;
238 unsigned char *superblock_coding;
240 int macroblock_count;
241 int macroblock_width;
242 int macroblock_height;
248 Vp3Fragment *all_fragments;
249 int u_fragment_start;
250 int v_fragment_start;
253 uint16_t coded_dc_scale_factor[64];
254 uint32_t coded_ac_scale_factor[64];
255 uint16_t coded_intra_y_dequant[64];
256 uint16_t coded_intra_c_dequant[64];
257 uint16_t coded_inter_dequant[64];
259 /* this is a list of indices into the all_fragments array indicating
260 * which of the fragments are coded */
261 int *coded_fragment_list;
262 int coded_fragment_list_index;
263 int pixel_addresses_inited;
271 /* these arrays need to be on 16-byte boundaries since SSE2 operations
273 int16_t __align16 intra_y_dequant[64];
274 int16_t __align16 intra_c_dequant[64];
275 int16_t __align16 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 /************************************************************************
310 * VP3 specific functions
311 ************************************************************************/
314 * This function sets up all of the various blocks mappings:
315 * superblocks <-> fragments, macroblocks <-> fragments,
316 * superblocks <-> macroblocks
318 * Returns 0 is successful; returns 1 if *anything* went wrong.
320 static int init_block_mapping(Vp3DecodeContext *s)
323 signed int hilbert_walk_y[16];
324 signed int hilbert_walk_c[16];
325 signed int hilbert_walk_mb[4];
327 int current_fragment = 0;
328 int current_width = 0;
329 int current_height = 0;
332 int superblock_row_inc = 0;
334 int mapping_index = 0;
336 int current_macroblock;
339 signed char travel_width[16] = {
346 signed char travel_height[16] = {
353 signed char travel_width_mb[4] = {
357 signed char travel_height_mb[4] = {
361 debug_vp3(" vp3: initialize block mapping tables\n");
363 /* figure out hilbert pattern per these frame dimensions */
364 hilbert_walk_y[0] = 1;
365 hilbert_walk_y[1] = 1;
366 hilbert_walk_y[2] = s->fragment_width;
367 hilbert_walk_y[3] = -1;
368 hilbert_walk_y[4] = s->fragment_width;
369 hilbert_walk_y[5] = s->fragment_width;
370 hilbert_walk_y[6] = 1;
371 hilbert_walk_y[7] = -s->fragment_width;
372 hilbert_walk_y[8] = 1;
373 hilbert_walk_y[9] = s->fragment_width;
374 hilbert_walk_y[10] = 1;
375 hilbert_walk_y[11] = -s->fragment_width;
376 hilbert_walk_y[12] = -s->fragment_width;
377 hilbert_walk_y[13] = -1;
378 hilbert_walk_y[14] = -s->fragment_width;
379 hilbert_walk_y[15] = 1;
381 hilbert_walk_c[0] = 1;
382 hilbert_walk_c[1] = 1;
383 hilbert_walk_c[2] = s->fragment_width / 2;
384 hilbert_walk_c[3] = -1;
385 hilbert_walk_c[4] = s->fragment_width / 2;
386 hilbert_walk_c[5] = s->fragment_width / 2;
387 hilbert_walk_c[6] = 1;
388 hilbert_walk_c[7] = -s->fragment_width / 2;
389 hilbert_walk_c[8] = 1;
390 hilbert_walk_c[9] = s->fragment_width / 2;
391 hilbert_walk_c[10] = 1;
392 hilbert_walk_c[11] = -s->fragment_width / 2;
393 hilbert_walk_c[12] = -s->fragment_width / 2;
394 hilbert_walk_c[13] = -1;
395 hilbert_walk_c[14] = -s->fragment_width / 2;
396 hilbert_walk_c[15] = 1;
398 hilbert_walk_mb[0] = 1;
399 hilbert_walk_mb[1] = s->macroblock_width;
400 hilbert_walk_mb[2] = 1;
401 hilbert_walk_mb[3] = -s->macroblock_width;
403 /* iterate through each superblock (all planes) and map the fragments */
404 for (i = 0; i < s->superblock_count; i++) {
405 debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
406 i, s->u_superblock_start, s->v_superblock_start);
408 /* time to re-assign the limits? */
411 /* start of Y superblocks */
412 right_edge = s->fragment_width;
413 bottom_edge = s->fragment_height;
416 superblock_row_inc = 3 * s->fragment_width -
417 (s->y_superblock_width * 4 - s->fragment_width);
418 hilbert = hilbert_walk_y;
420 /* the first operation for this variable is to advance by 1 */
421 current_fragment = -1;
423 } else if (i == s->u_superblock_start) {
425 /* start of U superblocks */
426 right_edge = s->fragment_width / 2;
427 bottom_edge = s->fragment_height / 2;
430 superblock_row_inc = 3 * (s->fragment_width / 2) -
431 (s->c_superblock_width * 4 - s->fragment_width / 2);
432 hilbert = hilbert_walk_c;
434 /* the first operation for this variable is to advance by 1 */
435 current_fragment = s->u_fragment_start - 1;
437 } else if (i == s->v_superblock_start) {
439 /* start of V superblocks */
440 right_edge = s->fragment_width / 2;
441 bottom_edge = s->fragment_height / 2;
444 superblock_row_inc = 3 * (s->fragment_width / 2) -
445 (s->c_superblock_width * 4 - s->fragment_width / 2);
446 hilbert = hilbert_walk_c;
448 /* the first operation for this variable is to advance by 1 */
449 current_fragment = s->v_fragment_start - 1;
453 if (current_width >= right_edge - 1) {
454 /* reset width and move to next superblock row */
458 /* fragment is now at the start of a new superblock row */
459 current_fragment += superblock_row_inc;
462 /* iterate through all 16 fragments in a superblock */
463 for (j = 0; j < 16; j++) {
464 current_fragment += hilbert[j];
465 current_width += travel_width[j];
466 current_height += travel_height[j];
468 /* check if the fragment is in bounds */
469 if ((current_width < right_edge) &&
470 (current_height < bottom_edge)) {
471 s->superblock_fragments[mapping_index] = current_fragment;
472 debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
473 s->superblock_fragments[mapping_index], i, j,
474 current_width, right_edge, current_height, bottom_edge);
476 s->superblock_fragments[mapping_index] = -1;
477 debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
479 current_width, right_edge, current_height, bottom_edge);
486 /* initialize the superblock <-> macroblock mapping; iterate through
487 * all of the Y plane superblocks to build this mapping */
488 right_edge = s->macroblock_width;
489 bottom_edge = s->macroblock_height;
492 superblock_row_inc = s->macroblock_width -
493 (s->y_superblock_width * 2 - s->macroblock_width);;
494 hilbert = hilbert_walk_mb;
496 current_macroblock = -1;
497 for (i = 0; i < s->u_superblock_start; i++) {
499 if (current_width >= right_edge - 1) {
500 /* reset width and move to next superblock row */
504 /* macroblock is now at the start of a new superblock row */
505 current_macroblock += superblock_row_inc;
508 /* iterate through each potential macroblock in the superblock */
509 for (j = 0; j < 4; j++) {
510 current_macroblock += hilbert_walk_mb[j];
511 current_width += travel_width_mb[j];
512 current_height += travel_height_mb[j];
514 /* check if the macroblock is in bounds */
515 if ((current_width < right_edge) &&
516 (current_height < bottom_edge)) {
517 s->superblock_macroblocks[mapping_index] = current_macroblock;
518 debug_init(" mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
519 s->superblock_macroblocks[mapping_index], i, j,
520 current_width, right_edge, current_height, bottom_edge);
522 s->superblock_macroblocks[mapping_index] = -1;
523 debug_init(" superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
525 current_width, right_edge, current_height, bottom_edge);
532 /* initialize the macroblock <-> fragment mapping */
533 current_fragment = 0;
534 current_macroblock = 0;
536 for (i = 0; i < s->fragment_height; i += 2) {
538 for (j = 0; j < s->fragment_width; j += 2) {
540 debug_init(" macroblock %d contains fragments: ", current_macroblock);
541 s->all_fragments[current_fragment].macroblock = current_macroblock;
542 s->macroblock_fragments[mapping_index++] = current_fragment;
543 debug_init("%d ", current_fragment);
545 if (j + 1 < s->fragment_width) {
546 s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
547 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
548 debug_init("%d ", current_fragment + 1);
550 s->macroblock_fragments[mapping_index++] = -1;
552 if (i + 1 < s->fragment_height) {
553 s->all_fragments[current_fragment + s->fragment_width].macroblock =
555 s->macroblock_fragments[mapping_index++] =
556 current_fragment + s->fragment_width;
557 debug_init("%d ", current_fragment + s->fragment_width);
559 s->macroblock_fragments[mapping_index++] = -1;
561 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
562 s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
564 s->macroblock_fragments[mapping_index++] =
565 current_fragment + s->fragment_width + 1;
566 debug_init("%d ", current_fragment + s->fragment_width + 1);
568 s->macroblock_fragments[mapping_index++] = -1;
571 c_fragment = s->u_fragment_start +
572 (i * s->fragment_width / 4) + (j / 2);
573 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
574 s->macroblock_fragments[mapping_index++] = c_fragment;
575 debug_init("%d ", c_fragment);
577 c_fragment = s->v_fragment_start +
578 (i * s->fragment_width / 4) + (j / 2);
579 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
580 s->macroblock_fragments[mapping_index++] = c_fragment;
581 debug_init("%d ", c_fragment);
585 if (j + 2 <= s->fragment_width)
586 current_fragment += 2;
589 current_macroblock++;
592 current_fragment += s->fragment_width;
595 return 0; /* successful path out */
599 * This function unpacks a single token (which should be in the range 0..31)
600 * and returns a zero run (number of zero coefficients in current DCT matrix
601 * before next non-zero coefficient), the next DCT coefficient, and the
602 * number of consecutive, non-EOB'd DCT blocks to EOB.
604 static void unpack_token(GetBitContext *gb, int token, int *zero_run,
605 DCTELEM *coeff, int *eob_run)
613 debug_token(" vp3 token %d: ", token);
617 debug_token("DCT_EOB_TOKEN, EOB next block\n");
622 debug_token("DCT_EOB_PAIR_TOKEN, EOB next 2 blocks\n");
627 debug_token("DCT_EOB_TRIPLE_TOKEN, EOB next 3 blocks\n");
632 debug_token("DCT_REPEAT_RUN_TOKEN, ");
633 *eob_run = get_bits(gb, 2) + 4;
634 debug_token("EOB the next %d blocks\n", *eob_run);
638 debug_token("DCT_REPEAT_RUN2_TOKEN, ");
639 *eob_run = get_bits(gb, 3) + 8;
640 debug_token("EOB the next %d blocks\n", *eob_run);
644 debug_token("DCT_REPEAT_RUN3_TOKEN, ");
645 *eob_run = get_bits(gb, 4) + 16;
646 debug_token("EOB the next %d blocks\n", *eob_run);
650 debug_token("DCT_REPEAT_RUN4_TOKEN, ");
651 *eob_run = get_bits(gb, 12);
652 debug_token("EOB the next %d blocks\n", *eob_run);
656 debug_token("DCT_SHORT_ZRL_TOKEN, ");
657 /* note that this token actually indicates that (3 extra bits) + 1 0s
658 * should be output; this case specifies a run of (3 EBs) 0s and a
659 * coefficient of 0. */
660 *zero_run = get_bits(gb, 3);
662 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
666 debug_token("DCT_ZRL_TOKEN, ");
667 /* note that this token actually indicates that (6 extra bits) + 1 0s
668 * should be output; this case specifies a run of (6 EBs) 0s and a
669 * coefficient of 0. */
670 *zero_run = get_bits(gb, 6);
672 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
676 debug_token("ONE_TOKEN, output 1\n");
681 debug_token("MINUS_ONE_TOKEN, output -1\n");
686 debug_token("TWO_TOKEN, output 2\n");
691 debug_token("MINUS_TWO_TOKEN, output -2\n");
699 debug_token("LOW_VAL_TOKENS, ");
701 *coeff = -(3 + (token - 13));
703 *coeff = 3 + (token - 13);
704 debug_token("output %d\n", *coeff);
708 debug_token("DCT_VAL_CATEGORY3, ");
709 sign = get_bits(gb, 1);
710 *coeff = 7 + get_bits(gb, 1);
713 debug_token("output %d\n", *coeff);
717 debug_token("DCT_VAL_CATEGORY4, ");
718 sign = get_bits(gb, 1);
719 *coeff = 9 + get_bits(gb, 2);
722 debug_token("output %d\n", *coeff);
726 debug_token("DCT_VAL_CATEGORY5, ");
727 sign = get_bits(gb, 1);
728 *coeff = 13 + get_bits(gb, 3);
731 debug_token("output %d\n", *coeff);
735 debug_token("DCT_VAL_CATEGORY6, ");
736 sign = get_bits(gb, 1);
737 *coeff = 21 + get_bits(gb, 4);
740 debug_token("output %d\n", *coeff);
744 debug_token("DCT_VAL_CATEGORY7, ");
745 sign = get_bits(gb, 1);
746 *coeff = 37 + get_bits(gb, 5);
749 debug_token("output %d\n", *coeff);
753 debug_token("DCT_VAL_CATEGORY8, ");
754 sign = get_bits(gb, 1);
755 *coeff = 69 + get_bits(gb, 9);
758 debug_token("output %d\n", *coeff);
766 debug_token("DCT_RUN_CATEGORY1, ");
767 *zero_run = token - 22;
772 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
776 debug_token("DCT_RUN_CATEGORY1B, ");
781 *zero_run = 6 + get_bits(gb, 2);
782 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
786 debug_token("DCT_RUN_CATEGORY1C, ");
791 *zero_run = 10 + get_bits(gb, 3);
792 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
796 debug_token("DCT_RUN_CATEGORY2, ");
797 sign = get_bits(gb, 1);
798 *coeff = 2 + get_bits(gb, 1);
802 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
806 debug_token("DCT_RUN_CATEGORY2, ");
807 sign = get_bits(gb, 1);
808 *coeff = 2 + get_bits(gb, 1);
811 *zero_run = 2 + get_bits(gb, 1);
812 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
816 av_log(NULL, AV_LOG_ERROR, " vp3: help! Got a bad token: %d > 31\n", token);
823 * This function wipes out all of the fragment data.
825 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
829 /* zero out all of the fragment information */
830 s->coded_fragment_list_index = 0;
831 for (i = 0; i < s->fragment_count; i++) {
832 memset(s->all_fragments[i].coeffs, 0, 64 * sizeof(DCTELEM));
833 s->all_fragments[i].coeff_count = 0;
834 s->all_fragments[i].last_coeff = 0;
835 s->all_fragments[i].motion_x = 0xbeef;
836 s->all_fragments[i].motion_y = 0xbeef;
841 * This function sets of the dequantization tables used for a particular
844 static void init_dequantizer(Vp3DecodeContext *s)
847 int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index];
848 int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index];
851 debug_vp3(" vp3: initializing dequantization tables\n");
854 * Scale dequantizers:
860 * where sf = dc_scale_factor for DC quantizer
861 * or ac_scale_factor for AC quantizer
863 * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL.
867 /* scale DC quantizers */
868 s->intra_y_dequant[0] = s->coded_intra_y_dequant[0] * dc_scale_factor / 100;
869 if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2)
870 s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2;
871 s->intra_y_dequant[0] *= SCALER;
873 s->intra_c_dequant[0] = s->coded_intra_c_dequant[0] * dc_scale_factor / 100;
874 if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2)
875 s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2;
876 s->intra_c_dequant[0] *= SCALER;
878 s->inter_dequant[0] = s->coded_inter_dequant[0] * dc_scale_factor / 100;
879 if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4)
880 s->inter_dequant[0] = MIN_DEQUANT_VAL * 4;
881 s->inter_dequant[0] *= SCALER;
883 /* scale AC quantizers, zigzag at the same time in preparation for
884 * the dequantization phase */
885 for (i = 1; i < 64; i++) {
889 s->intra_y_dequant[j] = s->coded_intra_y_dequant[i] * ac_scale_factor / 100;
890 if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL)
891 s->intra_y_dequant[j] = MIN_DEQUANT_VAL;
892 s->intra_y_dequant[j] *= SCALER;
894 s->intra_c_dequant[j] = s->coded_intra_c_dequant[i] * ac_scale_factor / 100;
895 if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL)
896 s->intra_c_dequant[j] = MIN_DEQUANT_VAL;
897 s->intra_c_dequant[j] *= SCALER;
899 s->inter_dequant[j] = s->coded_inter_dequant[i] * ac_scale_factor / 100;
900 if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2)
901 s->inter_dequant[j] = MIN_DEQUANT_VAL * 2;
902 s->inter_dequant[j] *= SCALER;
905 memset(s->qscale_table, (FFMAX(s->intra_y_dequant[1], s->intra_c_dequant[1])+8)/16, 512); //FIXME finetune
907 /* print debug information as requested */
908 debug_dequantizers("intra Y dequantizers:\n");
909 for (i = 0; i < 8; i++) {
910 for (j = i * 8; j < i * 8 + 8; j++) {
911 debug_dequantizers(" %4d,", s->intra_y_dequant[j]);
913 debug_dequantizers("\n");
915 debug_dequantizers("\n");
917 debug_dequantizers("intra C dequantizers:\n");
918 for (i = 0; i < 8; i++) {
919 for (j = i * 8; j < i * 8 + 8; j++) {
920 debug_dequantizers(" %4d,", s->intra_c_dequant[j]);
922 debug_dequantizers("\n");
924 debug_dequantizers("\n");
926 debug_dequantizers("interframe dequantizers:\n");
927 for (i = 0; i < 8; i++) {
928 for (j = i * 8; j < i * 8 + 8; j++) {
929 debug_dequantizers(" %4d,", s->inter_dequant[j]);
931 debug_dequantizers("\n");
933 debug_dequantizers("\n");
937 * This function is used to fetch runs of 1s or 0s from the bitstream for
938 * use in determining which superblocks are fully and partially coded.
947 * 111111xxxxxxxxxxxx 34-4129
949 static int get_superblock_run_length(GetBitContext *gb)
952 if (get_bits(gb, 1) == 0)
955 else if (get_bits(gb, 1) == 0)
956 return (2 + get_bits(gb, 1));
958 else if (get_bits(gb, 1) == 0)
959 return (4 + get_bits(gb, 1));
961 else if (get_bits(gb, 1) == 0)
962 return (6 + get_bits(gb, 2));
964 else if (get_bits(gb, 1) == 0)
965 return (10 + get_bits(gb, 3));
967 else if (get_bits(gb, 1) == 0)
968 return (18 + get_bits(gb, 4));
971 return (34 + get_bits(gb, 12));
976 * This function is used to fetch runs of 1s or 0s from the bitstream for
977 * use in determining which particular fragments are coded.
987 static int get_fragment_run_length(GetBitContext *gb)
990 if (get_bits(gb, 1) == 0)
991 return (1 + get_bits(gb, 1));
993 else if (get_bits(gb, 1) == 0)
994 return (3 + get_bits(gb, 1));
996 else if (get_bits(gb, 1) == 0)
997 return (5 + get_bits(gb, 1));
999 else if (get_bits(gb, 1) == 0)
1000 return (7 + get_bits(gb, 2));
1002 else if (get_bits(gb, 1) == 0)
1003 return (11 + get_bits(gb, 2));
1006 return (15 + get_bits(gb, 4));
1011 * This function decodes a VLC from the bitstream and returns a number
1012 * that ranges from 0..7. The number indicates which of the 8 coding
1026 static int get_mode_code(GetBitContext *gb)
1029 if (get_bits(gb, 1) == 0)
1032 else if (get_bits(gb, 1) == 0)
1035 else if (get_bits(gb, 1) == 0)
1038 else if (get_bits(gb, 1) == 0)
1041 else if (get_bits(gb, 1) == 0)
1044 else if (get_bits(gb, 1) == 0)
1047 else if (get_bits(gb, 1) == 0)
1056 * This function extracts a motion vector from the bitstream using a VLC
1057 * scheme. 3 bits are fetched from the bitstream and 1 of 8 actions is
1058 * taken depending on the value on those 3 bits:
1063 * 3: if (next bit is 1) return -2, else return 2
1064 * 4: if (next bit is 1) return -3, else return 3
1065 * 5: return 4 + (next 2 bits), next bit is sign
1066 * 6: return 8 + (next 3 bits), next bit is sign
1067 * 7: return 16 + (next 4 bits), next bit is sign
1069 static int get_motion_vector_vlc(GetBitContext *gb)
1073 bits = get_bits(gb, 3);
1090 if (get_bits(gb, 1) == 0)
1097 if (get_bits(gb, 1) == 0)
1104 bits = 4 + get_bits(gb, 2);
1105 if (get_bits(gb, 1) == 1)
1110 bits = 8 + get_bits(gb, 3);
1111 if (get_bits(gb, 1) == 1)
1116 bits = 16 + get_bits(gb, 4);
1117 if (get_bits(gb, 1) == 1)
1127 * This function fetches a 5-bit number from the stream followed by
1128 * a sign and calls it a motion vector.
1130 static int get_motion_vector_fixed(GetBitContext *gb)
1135 bits = get_bits(gb, 5);
1137 if (get_bits(gb, 1) == 1)
1144 * This function unpacks all of the superblock/macroblock/fragment coding
1145 * information from the bitstream.
1147 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
1150 int current_superblock = 0;
1151 int current_run = 0;
1152 int decode_fully_flags = 0;
1153 int decode_partial_blocks = 0;
1154 int first_c_fragment_seen;
1157 int current_fragment;
1159 debug_vp3(" vp3: unpacking superblock coding\n");
1163 debug_vp3(" keyframe-- all superblocks are fully coded\n");
1164 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
1168 /* unpack the list of partially-coded superblocks */
1169 bit = get_bits(gb, 1);
1170 /* toggle the bit because as soon as the first run length is
1171 * fetched the bit will be toggled again */
1173 while (current_superblock < s->superblock_count) {
1174 if (current_run == 0) {
1176 current_run = get_superblock_run_length(gb);
1177 debug_block_coding(" setting superblocks %d..%d to %s\n",
1179 current_superblock + current_run - 1,
1180 (bit) ? "partially coded" : "not coded");
1182 /* if any of the superblocks are not partially coded, flag
1183 * a boolean to decode the list of fully-coded superblocks */
1185 decode_fully_flags = 1;
1188 /* make a note of the fact that there are partially coded
1190 decode_partial_blocks = 1;
1193 s->superblock_coding[current_superblock++] =
1194 (bit) ? SB_PARTIALLY_CODED : SB_NOT_CODED;
1198 /* unpack the list of fully coded superblocks if any of the blocks were
1199 * not marked as partially coded in the previous step */
1200 if (decode_fully_flags) {
1202 current_superblock = 0;
1204 bit = get_bits(gb, 1);
1205 /* toggle the bit because as soon as the first run length is
1206 * fetched the bit will be toggled again */
1208 while (current_superblock < s->superblock_count) {
1210 /* skip any superblocks already marked as partially coded */
1211 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
1213 if (current_run == 0) {
1215 current_run = get_superblock_run_length(gb);
1218 debug_block_coding(" setting superblock %d to %s\n",
1220 (bit) ? "fully coded" : "not coded");
1221 s->superblock_coding[current_superblock] =
1222 (bit) ? SB_FULLY_CODED : SB_NOT_CODED;
1225 current_superblock++;
1229 /* if there were partial blocks, initialize bitstream for
1230 * unpacking fragment codings */
1231 if (decode_partial_blocks) {
1234 bit = get_bits(gb, 1);
1235 /* toggle the bit because as soon as the first run length is
1236 * fetched the bit will be toggled again */
1241 /* figure out which fragments are coded; iterate through each
1242 * superblock (all planes) */
1243 s->coded_fragment_list_index = 0;
1244 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
1245 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
1246 first_c_fragment_seen = 0;
1247 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
1248 for (i = 0; i < s->superblock_count; i++) {
1250 /* iterate through all 16 fragments in a superblock */
1251 for (j = 0; j < 16; j++) {
1253 /* if the fragment is in bounds, check its coding status */
1254 current_fragment = s->superblock_fragments[i * 16 + j];
1255 if (current_fragment >= s->fragment_count) {
1256 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
1257 current_fragment, s->fragment_count);
1260 if (current_fragment != -1) {
1261 if (s->superblock_coding[i] == SB_NOT_CODED) {
1263 /* copy all the fragments from the prior frame */
1264 s->all_fragments[current_fragment].coding_method =
1267 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
1269 /* fragment may or may not be coded; this is the case
1270 * that cares about the fragment coding runs */
1271 if (current_run == 0) {
1273 current_run = get_fragment_run_length(gb);
1277 /* default mode; actual mode will be decoded in
1279 s->all_fragments[current_fragment].coding_method =
1281 s->coded_fragment_list[s->coded_fragment_list_index] =
1283 if ((current_fragment >= s->u_fragment_start) &&
1284 (s->last_coded_y_fragment == -1) &&
1285 (!first_c_fragment_seen)) {
1286 s->first_coded_c_fragment = s->coded_fragment_list_index;
1287 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1288 first_c_fragment_seen = 1;
1290 s->coded_fragment_list_index++;
1291 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1292 debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
1293 i, current_fragment);
1295 /* not coded; copy this fragment from the prior frame */
1296 s->all_fragments[current_fragment].coding_method =
1298 debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
1299 i, current_fragment);
1306 /* fragments are fully coded in this superblock; actual
1307 * coding will be determined in next step */
1308 s->all_fragments[current_fragment].coding_method =
1310 s->coded_fragment_list[s->coded_fragment_list_index] =
1312 if ((current_fragment >= s->u_fragment_start) &&
1313 (s->last_coded_y_fragment == -1) &&
1314 (!first_c_fragment_seen)) {
1315 s->first_coded_c_fragment = s->coded_fragment_list_index;
1316 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1317 first_c_fragment_seen = 1;
1319 s->coded_fragment_list_index++;
1320 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1321 debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
1322 i, current_fragment);
1328 if (!first_c_fragment_seen)
1329 /* only Y fragments coded in this frame */
1330 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
1332 /* end the list of coded C fragments */
1333 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
1335 debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
1336 s->coded_fragment_list_index,
1337 s->first_coded_y_fragment,
1338 s->last_coded_y_fragment,
1339 s->first_coded_c_fragment,
1340 s->last_coded_c_fragment);
1346 * This function unpacks all the coding mode data for individual macroblocks
1347 * from the bitstream.
1349 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
1353 int current_macroblock;
1354 int current_fragment;
1357 debug_vp3(" vp3: unpacking encoding modes\n");
1360 debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
1362 for (i = 0; i < s->fragment_count; i++)
1363 s->all_fragments[i].coding_method = MODE_INTRA;
1367 /* fetch the mode coding scheme for this frame */
1368 scheme = get_bits(gb, 3);
1369 debug_modes(" using mode alphabet %d\n", scheme);
1371 /* is it a custom coding scheme? */
1373 debug_modes(" custom mode alphabet ahead:\n");
1374 for (i = 0; i < 8; i++)
1375 ModeAlphabet[scheme][get_bits(gb, 3)] = i;
1378 for (i = 0; i < 8; i++)
1379 debug_modes(" mode[%d][%d] = %d\n", scheme, i,
1380 ModeAlphabet[scheme][i]);
1382 /* iterate through all of the macroblocks that contain 1 or more
1383 * coded fragments */
1384 for (i = 0; i < s->u_superblock_start; i++) {
1386 for (j = 0; j < 4; j++) {
1387 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1388 if ((current_macroblock == -1) ||
1389 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1391 if (current_macroblock >= s->macroblock_count) {
1392 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
1393 current_macroblock, s->macroblock_count);
1397 /* mode 7 means get 3 bits for each coding mode */
1399 coding_mode = get_bits(gb, 3);
1401 coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
1403 s->macroblock_coding[current_macroblock] = coding_mode;
1404 for (k = 0; k < 6; k++) {
1406 s->macroblock_fragments[current_macroblock * 6 + k];
1407 if (current_fragment == -1)
1409 if (current_fragment >= s->fragment_count) {
1410 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
1411 current_fragment, s->fragment_count);
1414 if (s->all_fragments[current_fragment].coding_method !=
1416 s->all_fragments[current_fragment].coding_method =
1420 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
1421 s->macroblock_fragments[current_macroblock * 6], coding_mode);
1430 * This function unpacks all the motion vectors for the individual
1431 * macroblocks from the bitstream.
1433 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
1439 int last_motion_x = 0;
1440 int last_motion_y = 0;
1441 int prior_last_motion_x = 0;
1442 int prior_last_motion_y = 0;
1443 int current_macroblock;
1444 int current_fragment;
1446 debug_vp3(" vp3: unpacking motion vectors\n");
1449 debug_vp3(" keyframe-- there are no motion vectors\n");
1453 memset(motion_x, 0, 6 * sizeof(int));
1454 memset(motion_y, 0, 6 * sizeof(int));
1456 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
1457 coding_mode = get_bits(gb, 1);
1458 debug_vectors(" using %s scheme for unpacking motion vectors\n",
1459 (coding_mode == 0) ? "VLC" : "fixed-length");
1461 /* iterate through all of the macroblocks that contain 1 or more
1462 * coded fragments */
1463 for (i = 0; i < s->u_superblock_start; i++) {
1465 for (j = 0; j < 4; j++) {
1466 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1467 if ((current_macroblock == -1) ||
1468 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1470 if (current_macroblock >= s->macroblock_count) {
1471 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
1472 current_macroblock, s->macroblock_count);
1476 current_fragment = s->macroblock_fragments[current_macroblock * 6];
1477 if (current_fragment >= s->fragment_count) {
1478 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
1479 current_fragment, s->fragment_count);
1482 switch (s->macroblock_coding[current_macroblock]) {
1484 case MODE_INTER_PLUS_MV:
1485 case MODE_GOLDEN_MV:
1486 /* all 6 fragments use the same motion vector */
1487 if (coding_mode == 0) {
1488 motion_x[0] = get_motion_vector_vlc(gb);
1489 motion_y[0] = get_motion_vector_vlc(gb);
1491 motion_x[0] = get_motion_vector_fixed(gb);
1492 motion_y[0] = get_motion_vector_fixed(gb);
1494 for (k = 1; k < 6; k++) {
1495 motion_x[k] = motion_x[0];
1496 motion_y[k] = motion_y[0];
1499 /* vector maintenance, only on MODE_INTER_PLUS_MV */
1500 if (s->macroblock_coding[current_macroblock] ==
1501 MODE_INTER_PLUS_MV) {
1502 prior_last_motion_x = last_motion_x;
1503 prior_last_motion_y = last_motion_y;
1504 last_motion_x = motion_x[0];
1505 last_motion_y = motion_y[0];
1509 case MODE_INTER_FOURMV:
1510 /* fetch 4 vectors from the bitstream, one for each
1511 * Y fragment, then average for the C fragment vectors */
1512 motion_x[4] = motion_y[4] = 0;
1513 for (k = 0; k < 4; k++) {
1514 if (coding_mode == 0) {
1515 motion_x[k] = get_motion_vector_vlc(gb);
1516 motion_y[k] = get_motion_vector_vlc(gb);
1518 motion_x[k] = get_motion_vector_fixed(gb);
1519 motion_y[k] = get_motion_vector_fixed(gb);
1521 motion_x[4] += motion_x[k];
1522 motion_y[4] += motion_y[k];
1525 if (motion_x[4] >= 0)
1526 motion_x[4] = (motion_x[4] + 2) / 4;
1528 motion_x[4] = (motion_x[4] - 2) / 4;
1529 motion_x[5] = motion_x[4];
1531 if (motion_y[4] >= 0)
1532 motion_y[4] = (motion_y[4] + 2) / 4;
1534 motion_y[4] = (motion_y[4] - 2) / 4;
1535 motion_y[5] = motion_y[4];
1537 /* vector maintenance; vector[3] is treated as the
1538 * last vector in this case */
1539 prior_last_motion_x = last_motion_x;
1540 prior_last_motion_y = last_motion_y;
1541 last_motion_x = motion_x[3];
1542 last_motion_y = motion_y[3];
1545 case MODE_INTER_LAST_MV:
1546 /* all 6 fragments use the last motion vector */
1547 motion_x[0] = last_motion_x;
1548 motion_y[0] = last_motion_y;
1549 for (k = 1; k < 6; k++) {
1550 motion_x[k] = motion_x[0];
1551 motion_y[k] = motion_y[0];
1554 /* no vector maintenance (last vector remains the
1558 case MODE_INTER_PRIOR_LAST:
1559 /* all 6 fragments use the motion vector prior to the
1560 * last motion vector */
1561 motion_x[0] = prior_last_motion_x;
1562 motion_y[0] = prior_last_motion_y;
1563 for (k = 1; k < 6; k++) {
1564 motion_x[k] = motion_x[0];
1565 motion_y[k] = motion_y[0];
1568 /* vector maintenance */
1569 prior_last_motion_x = last_motion_x;
1570 prior_last_motion_y = last_motion_y;
1571 last_motion_x = motion_x[0];
1572 last_motion_y = motion_y[0];
1576 /* covers intra, inter without MV, golden without MV */
1577 memset(motion_x, 0, 6 * sizeof(int));
1578 memset(motion_y, 0, 6 * sizeof(int));
1580 /* no vector maintenance */
1584 /* assign the motion vectors to the correct fragments */
1585 debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
1587 s->macroblock_coding[current_macroblock]);
1588 for (k = 0; k < 6; k++) {
1590 s->macroblock_fragments[current_macroblock * 6 + k];
1591 if (current_fragment == -1)
1593 if (current_fragment >= s->fragment_count) {
1594 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
1595 current_fragment, s->fragment_count);
1598 s->all_fragments[current_fragment].motion_x = motion_x[k];
1599 s->all_fragments[current_fragment].motion_y = motion_y[k];
1600 debug_vectors(" vector %d: fragment %d = (%d, %d)\n",
1601 k, current_fragment, motion_x[k], motion_y[k]);
1611 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1612 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1613 * data. This function unpacks all the VLCs for either the Y plane or both
1614 * C planes, and is called for DC coefficients or different AC coefficient
1615 * levels (since different coefficient types require different VLC tables.
1617 * This function returns a residual eob run. E.g, if a particular token gave
1618 * instructions to EOB the next 5 fragments and there were only 2 fragments
1619 * left in the current fragment range, 3 would be returned so that it could
1620 * be passed into the next call to this same function.
1622 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1623 VLC *table, int coeff_index,
1624 int first_fragment, int last_fragment,
1631 Vp3Fragment *fragment;
1633 if ((first_fragment >= s->fragment_count) ||
1634 (last_fragment >= s->fragment_count)) {
1636 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
1637 first_fragment, last_fragment);
1641 for (i = first_fragment; i <= last_fragment; i++) {
1643 fragment = &s->all_fragments[s->coded_fragment_list[i]];
1644 if (fragment->coeff_count > coeff_index)
1648 /* decode a VLC into a token */
1649 token = get_vlc2(gb, table->table, 5, 3);
1650 debug_vlc(" token = %2d, ", token);
1651 /* use the token to get a zero run, a coefficient, and an eob run */
1652 unpack_token(gb, token, &zero_run, &coeff, &eob_run);
1656 fragment->coeff_count += zero_run;
1657 if (fragment->coeff_count < 64)
1658 fragment->coeffs[fragment->coeff_count++] = coeff;
1659 debug_vlc(" fragment %d coeff = %d\n",
1660 s->coded_fragment_list[i], fragment->coeffs[coeff_index]);
1662 fragment->last_coeff = fragment->coeff_count;
1663 fragment->coeff_count = 64;
1664 debug_vlc(" fragment %d eob with %d coefficients\n",
1665 s->coded_fragment_list[i], fragment->last_coeff);
1674 * This function unpacks all of the DCT coefficient data from the
1677 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1684 int residual_eob_run = 0;
1686 /* fetch the DC table indices */
1687 dc_y_table = get_bits(gb, 4);
1688 dc_c_table = get_bits(gb, 4);
1690 /* unpack the Y plane DC coefficients */
1691 debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
1693 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1694 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1696 /* unpack the C plane DC coefficients */
1697 debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
1699 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1700 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1702 /* fetch the AC table indices */
1703 ac_y_table = get_bits(gb, 4);
1704 ac_c_table = get_bits(gb, 4);
1706 /* unpack the group 1 AC coefficients (coeffs 1-5) */
1707 for (i = 1; i <= 5; i++) {
1709 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1711 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
1712 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1714 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1716 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
1717 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1720 /* unpack the group 2 AC coefficients (coeffs 6-14) */
1721 for (i = 6; i <= 14; i++) {
1723 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1725 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
1726 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1728 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1730 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
1731 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1734 /* unpack the group 3 AC coefficients (coeffs 15-27) */
1735 for (i = 15; i <= 27; i++) {
1737 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1739 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
1740 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1742 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1744 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
1745 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1748 /* unpack the group 4 AC coefficients (coeffs 28-63) */
1749 for (i = 28; i <= 63; i++) {
1751 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1753 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
1754 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1756 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1758 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
1759 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1766 * This function reverses the DC prediction for each coded fragment in
1767 * the frame. Much of this function is adapted directly from the original
1770 #define COMPATIBLE_FRAME(x) \
1771 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1772 #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
1773 static inline int iabs (int x) { return ((x < 0) ? -x : x); }
1775 static void reverse_dc_prediction(Vp3DecodeContext *s,
1778 int fragment_height)
1787 int i = first_fragment;
1790 * Fragment prediction groups:
1798 * Note: Groups 5 and 7 do not exist as it would mean that the
1799 * fragment's x coordinate is both 0 and (width - 1) at the same time.
1801 int predictor_group;
1804 /* validity flags for the left, up-left, up, and up-right fragments */
1805 int fl, ful, fu, fur;
1807 /* DC values for the left, up-left, up, and up-right fragments */
1808 int vl, vul, vu, vur;
1810 /* indices for the left, up-left, up, and up-right fragments */
1814 * The 6 fields mean:
1815 * 0: up-left multiplier
1817 * 2: up-right multiplier
1818 * 3: left multiplier
1820 * 5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
1822 int predictor_transform[16][6] = {
1823 { 0, 0, 0, 0, 0, 0 },
1824 { 0, 0, 0, 1, 0, 0 }, // PL
1825 { 0, 0, 1, 0, 0, 0 }, // PUR
1826 { 0, 0, 53, 75, 127, 7 }, // PUR|PL
1827 { 0, 1, 0, 0, 0, 0 }, // PU
1828 { 0, 1, 0, 1, 1, 1 }, // PU|PL
1829 { 0, 1, 0, 0, 0, 0 }, // PU|PUR
1830 { 0, 0, 53, 75, 127, 7 }, // PU|PUR|PL
1831 { 1, 0, 0, 0, 0, 0 }, // PUL
1832 { 0, 0, 0, 1, 0, 0 }, // PUL|PL
1833 { 1, 0, 1, 0, 1, 1 }, // PUL|PUR
1834 { 0, 0, 53, 75, 127, 7 }, // PUL|PUR|PL
1835 { 0, 1, 0, 0, 0, 0 }, // PUL|PU
1836 {-26, 29, 0, 29, 31, 5 }, // PUL|PU|PL
1837 { 3, 10, 3, 0, 15, 4 }, // PUL|PU|PUR
1838 {-26, 29, 0, 29, 31, 5 } // PUL|PU|PUR|PL
1841 /* This table shows which types of blocks can use other blocks for
1842 * prediction. For example, INTRA is the only mode in this table to
1843 * have a frame number of 0. That means INTRA blocks can only predict
1844 * from other INTRA blocks. There are 2 golden frame coding types;
1845 * blocks encoding in these modes can only predict from other blocks
1846 * that were encoded with these 1 of these 2 modes. */
1847 unsigned char compatible_frame[8] = {
1848 1, /* MODE_INTER_NO_MV */
1850 1, /* MODE_INTER_PLUS_MV */
1851 1, /* MODE_INTER_LAST_MV */
1852 1, /* MODE_INTER_PRIOR_MV */
1853 2, /* MODE_USING_GOLDEN */
1854 2, /* MODE_GOLDEN_MV */
1855 1 /* MODE_INTER_FOUR_MV */
1857 int current_frame_type;
1859 /* there is a last DC predictor for each of the 3 frame types */
1864 debug_vp3(" vp3: reversing DC prediction\n");
1866 vul = vu = vur = vl = 0;
1867 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1869 /* for each fragment row... */
1870 for (y = 0; y < fragment_height; y++) {
1872 /* for each fragment in a row... */
1873 for (x = 0; x < fragment_width; x++, i++) {
1875 /* reverse prediction if this block was coded */
1876 if (s->all_fragments[i].coding_method != MODE_COPY) {
1878 current_frame_type =
1879 compatible_frame[s->all_fragments[i].coding_method];
1880 predictor_group = (x == 0) + ((y == 0) << 1) +
1881 ((x + 1 == fragment_width) << 2);
1882 debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
1883 i, predictor_group, s->all_fragments[i].coeffs[0]);
1885 switch (predictor_group) {
1888 /* main body of fragments; consider all 4 possible
1889 * fragments for prediction */
1891 /* calculate the indices of the predicting fragments */
1892 ul = i - fragment_width - 1;
1893 u = i - fragment_width;
1894 ur = i - fragment_width + 1;
1897 /* fetch the DC values for the predicting fragments */
1898 vul = s->all_fragments[ul].coeffs[0];
1899 vu = s->all_fragments[u].coeffs[0];
1900 vur = s->all_fragments[ur].coeffs[0];
1901 vl = s->all_fragments[l].coeffs[0];
1903 /* figure out which fragments are valid */
1904 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
1905 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1906 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1907 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1909 /* decide which predictor transform to use */
1910 transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
1915 /* left column of fragments, not including top corner;
1916 * only consider up and up-right fragments */
1918 /* calculate the indices of the predicting fragments */
1919 u = i - fragment_width;
1920 ur = i - fragment_width + 1;
1922 /* fetch the DC values for the predicting fragments */
1923 vu = s->all_fragments[u].coeffs[0];
1924 vur = s->all_fragments[ur].coeffs[0];
1926 /* figure out which fragments are valid */
1927 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1928 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1930 /* decide which predictor transform to use */
1931 transform = (fu*PU) | (fur*PUR);
1937 /* top row of fragments, not including top-left frag;
1938 * only consider the left fragment for prediction */
1940 /* calculate the indices of the predicting fragments */
1943 /* fetch the DC values for the predicting fragments */
1944 vl = s->all_fragments[l].coeffs[0];
1946 /* figure out which fragments are valid */
1947 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1949 /* decide which predictor transform to use */
1950 transform = (fl*PL);
1955 /* top-left fragment */
1957 /* nothing to predict from in this case */
1963 /* right column of fragments, not including top corner;
1964 * consider up-left, up, and left fragments for
1967 /* calculate the indices of the predicting fragments */
1968 ul = i - fragment_width - 1;
1969 u = i - fragment_width;
1972 /* fetch the DC values for the predicting fragments */
1973 vul = s->all_fragments[ul].coeffs[0];
1974 vu = s->all_fragments[u].coeffs[0];
1975 vl = s->all_fragments[l].coeffs[0];
1977 /* figure out which fragments are valid */
1978 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
1979 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1980 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1982 /* decide which predictor transform to use */
1983 transform = (fl*PL) | (fu*PU) | (ful*PUL);
1989 debug_dc_pred("transform = %d, ", transform);
1991 if (transform == 0) {
1993 /* if there were no fragments to predict from, use last
1995 s->all_fragments[i].coeffs[0] += last_dc[current_frame_type];
1996 debug_dc_pred("from last DC (%d) = %d\n",
1997 current_frame_type, s->all_fragments[i].coeffs[0]);
2001 /* apply the appropriate predictor transform */
2003 (predictor_transform[transform][0] * vul) +
2004 (predictor_transform[transform][1] * vu) +
2005 (predictor_transform[transform][2] * vur) +
2006 (predictor_transform[transform][3] * vl);
2008 /* if there is a shift value in the transform, add
2009 * the sign bit before the shift */
2010 if (predictor_transform[transform][5] != 0) {
2011 predicted_dc += ((predicted_dc >> 15) &
2012 predictor_transform[transform][4]);
2013 predicted_dc >>= predictor_transform[transform][5];
2016 /* check for outranging on the [ul u l] and
2017 * [ul u ur l] predictors */
2018 if ((transform == 13) || (transform == 15)) {
2019 if (iabs(predicted_dc - vu) > 128)
2021 else if (iabs(predicted_dc - vl) > 128)
2023 else if (iabs(predicted_dc - vul) > 128)
2027 /* at long last, apply the predictor */
2028 s->all_fragments[i].coeffs[0] += predicted_dc;
2029 debug_dc_pred("from pred DC = %d\n",
2030 s->all_fragments[i].coeffs[0]);
2034 last_dc[current_frame_type] = s->all_fragments[i].coeffs[0];
2041 * This function performs the final rendering of each fragment's data
2042 * onto the output frame.
2044 static void render_fragments(Vp3DecodeContext *s,
2048 int plane /* 0 = Y, 1 = U, 2 = V */)
2052 int i = first_fragment;
2053 int16_t *dequantizer;
2054 DCTELEM __align16 output_samples[64];
2055 unsigned char *output_plane;
2056 unsigned char *last_plane;
2057 unsigned char *golden_plane;
2059 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
2060 int upper_motion_limit, lower_motion_limit;
2061 int motion_halfpel_index;
2062 uint8_t *motion_source;
2064 debug_vp3(" vp3: rendering final fragments for %s\n",
2065 (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
2067 /* set up plane-specific parameters */
2069 dequantizer = s->intra_y_dequant;
2070 output_plane = s->current_frame.data[0];
2071 last_plane = s->last_frame.data[0];
2072 golden_plane = s->golden_frame.data[0];
2073 stride = s->current_frame.linesize[0];
2074 if (!s->flipped_image) stride = -stride;
2075 upper_motion_limit = 7 * s->current_frame.linesize[0];
2076 lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
2077 } else if (plane == 1) {
2078 dequantizer = s->intra_c_dequant;
2079 output_plane = s->current_frame.data[1];
2080 last_plane = s->last_frame.data[1];
2081 golden_plane = s->golden_frame.data[1];
2082 stride = s->current_frame.linesize[1];
2083 if (!s->flipped_image) stride = -stride;
2084 upper_motion_limit = 7 * s->current_frame.linesize[1];
2085 lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
2087 dequantizer = s->intra_c_dequant;
2088 output_plane = s->current_frame.data[2];
2089 last_plane = s->last_frame.data[2];
2090 golden_plane = s->golden_frame.data[2];
2091 stride = s->current_frame.linesize[2];
2092 if (!s->flipped_image) stride = -stride;
2093 upper_motion_limit = 7 * s->current_frame.linesize[2];
2094 lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
2097 /* for each fragment row... */
2098 for (y = 0; y < height; y += 8) {
2100 /* for each fragment in a row... */
2101 for (x = 0; x < width; x += 8, i++) {
2103 if ((i < 0) || (i >= s->fragment_count)) {
2104 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_fragments(): bad fragment number (%d)\n", i);
2108 /* transform if this block was coded */
2109 if ((s->all_fragments[i].coding_method != MODE_COPY) &&
2110 !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
2112 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2113 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2114 motion_source= golden_plane;
2116 motion_source= last_plane;
2118 motion_source += s->all_fragments[i].first_pixel;
2119 motion_halfpel_index = 0;
2121 /* sort out the motion vector if this fragment is coded
2122 * using a motion vector method */
2123 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2124 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2126 motion_x = s->all_fragments[i].motion_x;
2127 motion_y = s->all_fragments[i].motion_y;
2129 motion_x= (motion_x>>1) | (motion_x&1);
2130 motion_y= (motion_y>>1) | (motion_y&1);
2133 src_x= (motion_x>>1) + x;
2134 src_y= (motion_y>>1) + y;
2135 if ((motion_x == 0xbeef) || (motion_y == 0xbeef))
2136 av_log(s->avctx, AV_LOG_ERROR, " help! got beefy vector! (%X, %X)\n", motion_x, motion_y);
2138 motion_halfpel_index = motion_x & 0x01;
2139 motion_source += (motion_x >> 1);
2141 // motion_y = -motion_y;
2142 motion_halfpel_index |= (motion_y & 0x01) << 1;
2143 motion_source += ((motion_y >> 1) * stride);
2145 if(src_x<0 || src_y<0 || src_x + 9 >= width || src_y + 9 >= height){
2146 uint8_t *temp= s->edge_emu_buffer;
2147 if(stride<0) temp -= 9*stride;
2148 else temp += 9*stride;
2150 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, width, height);
2151 motion_source= temp;
2156 /* first, take care of copying a block from either the
2157 * previous or the golden frame */
2158 if (s->all_fragments[i].coding_method != MODE_INTRA) {
2159 //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
2160 if(motion_halfpel_index != 3){
2161 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
2162 output_plane + s->all_fragments[i].first_pixel,
2163 motion_source, stride, 8);
2165 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
2166 s->dsp.put_no_rnd_pixels_l2[1](
2167 output_plane + s->all_fragments[i].first_pixel,
2169 motion_source + stride + 1 + d,
2174 /* dequantize the DCT coefficients */
2175 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
2176 i, s->all_fragments[i].coding_method,
2177 s->all_fragments[i].coeffs[0], dequantizer[0]);
2179 /* invert DCT and place (or add) in final output */
2180 s->dsp.vp3_idct(s->all_fragments[i].coeffs,
2182 s->all_fragments[i].coeff_count,
2184 if (s->all_fragments[i].coding_method == MODE_INTRA) {
2185 s->dsp.put_signed_pixels_clamped(output_samples,
2186 output_plane + s->all_fragments[i].first_pixel,
2189 s->dsp.add_pixels_clamped(output_samples,
2190 output_plane + s->all_fragments[i].first_pixel,
2194 debug_idct("block after idct_%s():\n",
2195 (s->all_fragments[i].coding_method == MODE_INTRA)?
2197 for (m = 0; m < 8; m++) {
2198 for (n = 0; n < 8; n++) {
2199 debug_idct(" %3d", *(output_plane +
2200 s->all_fragments[i].first_pixel + (m * stride + n)));
2208 /* copy directly from the previous frame */
2209 s->dsp.put_pixels_tab[1][0](
2210 output_plane + s->all_fragments[i].first_pixel,
2211 last_plane + s->all_fragments[i].first_pixel,
2223 * This function computes the first pixel addresses for each fragment.
2224 * This function needs to be invoked after the first frame is allocated
2225 * so that it has access to the plane strides.
2227 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
2232 /* figure out the first pixel addresses for each of the fragments */
2235 for (y = s->fragment_height; y > 0; y--) {
2236 for (x = 0; x < s->fragment_width; x++) {
2237 s->all_fragments[i++].first_pixel =
2238 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2239 s->golden_frame.linesize[0] +
2240 x * FRAGMENT_PIXELS;
2241 debug_init(" fragment %d, first pixel @ %d\n",
2242 i-1, s->all_fragments[i-1].first_pixel);
2247 i = s->u_fragment_start;
2248 for (y = s->fragment_height / 2; y > 0; y--) {
2249 for (x = 0; x < s->fragment_width / 2; x++) {
2250 s->all_fragments[i++].first_pixel =
2251 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2252 s->golden_frame.linesize[1] +
2253 x * FRAGMENT_PIXELS;
2254 debug_init(" fragment %d, first pixel @ %d\n",
2255 i-1, s->all_fragments[i-1].first_pixel);
2260 i = s->v_fragment_start;
2261 for (y = s->fragment_height / 2; y > 0; y--) {
2262 for (x = 0; x < s->fragment_width / 2; x++) {
2263 s->all_fragments[i++].first_pixel =
2264 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2265 s->golden_frame.linesize[2] +
2266 x * FRAGMENT_PIXELS;
2267 debug_init(" fragment %d, first pixel @ %d\n",
2268 i-1, s->all_fragments[i-1].first_pixel);
2273 /* FIXME: this should be merged with the above! */
2274 static void theora_calculate_pixel_addresses(Vp3DecodeContext *s)
2279 /* figure out the first pixel addresses for each of the fragments */
2282 for (y = 1; y <= s->fragment_height; y++) {
2283 for (x = 0; x < s->fragment_width; x++) {
2284 s->all_fragments[i++].first_pixel =
2285 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2286 s->golden_frame.linesize[0] +
2287 x * FRAGMENT_PIXELS;
2288 debug_init(" fragment %d, first pixel @ %d\n",
2289 i-1, s->all_fragments[i-1].first_pixel);
2294 i = s->u_fragment_start;
2295 for (y = 1; y <= s->fragment_height / 2; y++) {
2296 for (x = 0; x < s->fragment_width / 2; x++) {
2297 s->all_fragments[i++].first_pixel =
2298 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2299 s->golden_frame.linesize[1] +
2300 x * FRAGMENT_PIXELS;
2301 debug_init(" fragment %d, first pixel @ %d\n",
2302 i-1, s->all_fragments[i-1].first_pixel);
2307 i = s->v_fragment_start;
2308 for (y = 1; y <= s->fragment_height / 2; y++) {
2309 for (x = 0; x < s->fragment_width / 2; x++) {
2310 s->all_fragments[i++].first_pixel =
2311 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2312 s->golden_frame.linesize[2] +
2313 x * FRAGMENT_PIXELS;
2314 debug_init(" fragment %d, first pixel @ %d\n",
2315 i-1, s->all_fragments[i-1].first_pixel);
2321 * This is the ffmpeg/libavcodec API init function.
2323 static int vp3_decode_init(AVCodecContext *avctx)
2325 Vp3DecodeContext *s = avctx->priv_data;
2329 int y_superblock_count;
2330 int c_superblock_count;
2332 if (avctx->codec_tag == MKTAG('V','P','3','0'))
2339 s->width = avctx->width;
2340 s->height = avctx->height;
2342 s->width = (avctx->width + 15) & 0xFFFFFFF0;
2343 s->height = (avctx->height + 15) & 0xFFFFFFF0;
2345 avctx->pix_fmt = PIX_FMT_YUV420P;
2346 avctx->has_b_frames = 0;
2347 dsputil_init(&s->dsp, avctx);
2348 s->dsp.vp3_dsp_init();
2350 /* initialize to an impossible value which will force a recalculation
2351 * in the first frame decode */
2352 s->quality_index = -1;
2354 s->y_superblock_width = (s->width + 31) / 32;
2355 s->y_superblock_height = (s->height + 31) / 32;
2356 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2358 /* work out the dimensions for the C planes */
2359 c_width = s->width / 2;
2360 c_height = s->height / 2;
2361 s->c_superblock_width = (c_width + 31) / 32;
2362 s->c_superblock_height = (c_height + 31) / 32;
2363 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2365 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
2366 s->u_superblock_start = y_superblock_count;
2367 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
2368 s->superblock_coding = av_malloc(s->superblock_count);
2370 s->macroblock_width = (s->width + 15) / 16;
2371 s->macroblock_height = (s->height + 15) / 16;
2372 s->macroblock_count = s->macroblock_width * s->macroblock_height;
2374 s->fragment_width = s->width / FRAGMENT_PIXELS;
2375 s->fragment_height = s->height / FRAGMENT_PIXELS;
2377 /* fragment count covers all 8x8 blocks for all 3 planes */
2378 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
2379 s->u_fragment_start = s->fragment_width * s->fragment_height;
2380 s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
2382 debug_init(" Y plane: %d x %d\n", s->width, s->height);
2383 debug_init(" C plane: %d x %d\n", c_width, c_height);
2384 debug_init(" Y superblocks: %d x %d, %d total\n",
2385 s->y_superblock_width, s->y_superblock_height, y_superblock_count);
2386 debug_init(" C superblocks: %d x %d, %d total\n",
2387 s->c_superblock_width, s->c_superblock_height, c_superblock_count);
2388 debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
2389 s->superblock_count, s->u_superblock_start, s->v_superblock_start);
2390 debug_init(" macroblocks: %d x %d, %d total\n",
2391 s->macroblock_width, s->macroblock_height, s->macroblock_count);
2392 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
2396 s->u_fragment_start,
2397 s->v_fragment_start);
2399 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
2400 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
2401 s->pixel_addresses_inited = 0;
2403 if (!s->theora_tables)
2405 for (i = 0; i < 64; i++)
2406 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
2407 for (i = 0; i < 64; i++)
2408 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
2409 for (i = 0; i < 64; i++)
2410 s->coded_intra_y_dequant[i] = vp31_intra_y_dequant[i];
2411 for (i = 0; i < 64; i++)
2412 s->coded_intra_c_dequant[i] = vp31_intra_c_dequant[i];
2413 for (i = 0; i < 64; i++)
2414 s->coded_inter_dequant[i] = vp31_inter_dequant[i];
2417 /* init VLC tables */
2418 for (i = 0; i < 16; i++) {
2421 init_vlc(&s->dc_vlc[i], 5, 32,
2422 &dc_bias[i][0][1], 4, 2,
2423 &dc_bias[i][0][0], 4, 2);
2425 /* group 1 AC histograms */
2426 init_vlc(&s->ac_vlc_1[i], 5, 32,
2427 &ac_bias_0[i][0][1], 4, 2,
2428 &ac_bias_0[i][0][0], 4, 2);
2430 /* group 2 AC histograms */
2431 init_vlc(&s->ac_vlc_2[i], 5, 32,
2432 &ac_bias_1[i][0][1], 4, 2,
2433 &ac_bias_1[i][0][0], 4, 2);
2435 /* group 3 AC histograms */
2436 init_vlc(&s->ac_vlc_3[i], 5, 32,
2437 &ac_bias_2[i][0][1], 4, 2,
2438 &ac_bias_2[i][0][0], 4, 2);
2440 /* group 4 AC histograms */
2441 init_vlc(&s->ac_vlc_4[i], 5, 32,
2442 &ac_bias_3[i][0][1], 4, 2,
2443 &ac_bias_3[i][0][0], 4, 2);
2446 /* build quantization zigzag table */
2447 for (i = 0; i < 64; i++)
2448 zigzag_index[dezigzag_index[i]] = i;
2450 /* work out the block mapping tables */
2451 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
2452 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
2453 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
2454 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
2455 init_block_mapping(s);
2457 for (i = 0; i < 3; i++) {
2458 s->current_frame.data[i] = NULL;
2459 s->last_frame.data[i] = NULL;
2460 s->golden_frame.data[i] = NULL;
2467 * This is the ffmpeg/libavcodec API frame decode function.
2469 static int vp3_decode_frame(AVCodecContext *avctx,
2470 void *data, int *data_size,
2471 uint8_t *buf, int buf_size)
2473 Vp3DecodeContext *s = avctx->priv_data;
2475 static int counter = 0;
2477 init_get_bits(&gb, buf, buf_size * 8);
2479 if (s->theora && get_bits1(&gb))
2481 int ptype = get_bits(&gb, 7);
2483 skip_bits(&gb, 6*8); /* "theora" */
2488 theora_decode_comments(avctx, gb);
2491 theora_decode_tables(avctx, gb);
2492 init_dequantizer(s);
2495 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype);
2500 s->keyframe = !get_bits1(&gb);
2503 s->last_quality_index = s->quality_index;
2504 s->quality_index = get_bits(&gb, 6);
2505 if (s->theora >= 0x030300)
2508 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2509 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2510 s->keyframe?"key":"", counter, s->quality_index);
2513 if (s->quality_index != s->last_quality_index)
2514 init_dequantizer(s);
2519 skip_bits(&gb, 4); /* width code */
2520 skip_bits(&gb, 4); /* height code */
2523 s->version = get_bits(&gb, 5);
2525 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
2528 if (s->version || s->theora)
2531 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
2532 skip_bits(&gb, 2); /* reserved? */
2535 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
2536 if (s->golden_frame.data[0])
2537 avctx->release_buffer(avctx, &s->golden_frame);
2538 s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
2540 if (s->golden_frame.data[0])
2541 avctx->release_buffer(avctx, &s->golden_frame);
2542 if (s->last_frame.data[0])
2543 avctx->release_buffer(avctx, &s->last_frame);
2546 s->golden_frame.reference = 3;
2547 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
2548 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2552 /* golden frame is also the current frame */
2553 memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
2555 /* time to figure out pixel addresses? */
2556 if (!s->pixel_addresses_inited)
2558 if (!s->flipped_image)
2559 vp3_calculate_pixel_addresses(s);
2561 theora_calculate_pixel_addresses(s);
2564 /* allocate a new current frame */
2565 s->current_frame.reference = 3;
2566 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
2567 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2572 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
2573 s->current_frame.qstride= 0;
2580 memcpy(s->current_frame.data[0], s->golden_frame.data[0],
2581 s->current_frame.linesize[0] * s->height);
2582 memcpy(s->current_frame.data[1], s->golden_frame.data[1],
2583 s->current_frame.linesize[1] * s->height / 2);
2584 memcpy(s->current_frame.data[2], s->golden_frame.data[2],
2585 s->current_frame.linesize[2] * s->height / 2);
2590 if (unpack_superblocks(s, &gb) ||
2591 unpack_modes(s, &gb) ||
2592 unpack_vectors(s, &gb) ||
2593 unpack_dct_coeffs(s, &gb)) {
2595 av_log(s->avctx, AV_LOG_ERROR, " vp3: could not decode frame\n");
2599 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
2600 render_fragments(s, 0, s->width, s->height, 0);
2602 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
2603 reverse_dc_prediction(s, s->u_fragment_start,
2604 s->fragment_width / 2, s->fragment_height / 2);
2605 reverse_dc_prediction(s, s->v_fragment_start,
2606 s->fragment_width / 2, s->fragment_height / 2);
2607 render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
2608 render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
2610 memset(s->current_frame.data[1], 0x80, s->width * s->height / 4);
2611 memset(s->current_frame.data[2], 0x80, s->width * s->height / 4);
2618 *data_size=sizeof(AVFrame);
2619 *(AVFrame*)data= s->current_frame;
2621 /* release the last frame, if it is allocated and if it is not the
2623 if ((s->last_frame.data[0]) &&
2624 (s->last_frame.data[0] != s->golden_frame.data[0]))
2625 avctx->release_buffer(avctx, &s->last_frame);
2627 /* shuffle frames (last = current) */
2628 memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
2629 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
2635 * This is the ffmpeg/libavcodec API module cleanup function.
2637 static int vp3_decode_end(AVCodecContext *avctx)
2639 Vp3DecodeContext *s = avctx->priv_data;
2641 av_free(s->all_fragments);
2642 av_free(s->coded_fragment_list);
2643 av_free(s->superblock_fragments);
2644 av_free(s->superblock_macroblocks);
2645 av_free(s->macroblock_fragments);
2646 av_free(s->macroblock_coding);
2648 /* release all frames */
2649 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
2650 avctx->release_buffer(avctx, &s->golden_frame);
2651 if (s->last_frame.data[0])
2652 avctx->release_buffer(avctx, &s->last_frame);
2653 /* no need to release the current_frame since it will always be pointing
2654 * to the same frame as either the golden or last frame */
2659 static int theora_decode_header(AVCodecContext *avctx, GetBitContext gb)
2661 Vp3DecodeContext *s = avctx->priv_data;
2662 int major, minor, micro;
2664 major = get_bits(&gb, 8); /* version major */
2665 minor = get_bits(&gb, 8); /* version minor */
2666 micro = get_bits(&gb, 8); /* version micro */
2667 av_log(avctx, AV_LOG_INFO, "Theora bitstream version %d.%d.%d\n",
2668 major, minor, micro);
2670 /* FIXME: endianess? */
2671 s->theora = (major << 16) | (minor << 8) | micro;
2673 /* 3.3.0 aka alpha3 has the same frame orientation as original vp3 */
2674 /* but previous versions have the image flipped relative to vp3 */
2675 if (s->theora < 0x030300)
2677 s->flipped_image = 1;
2678 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
2681 s->width = get_bits(&gb, 16) << 4;
2682 s->height = get_bits(&gb, 16) << 4;
2684 skip_bits(&gb, 24); /* frame width */
2685 skip_bits(&gb, 24); /* frame height */
2687 skip_bits(&gb, 8); /* offset x */
2688 skip_bits(&gb, 8); /* offset y */
2690 skip_bits(&gb, 32); /* fps numerator */
2691 skip_bits(&gb, 32); /* fps denumerator */
2692 skip_bits(&gb, 24); /* aspect numerator */
2693 skip_bits(&gb, 24); /* aspect denumerator */
2695 if (s->theora < 0x030300)
2696 skip_bits(&gb, 5); /* keyframe frequency force */
2697 skip_bits(&gb, 8); /* colorspace */
2698 skip_bits(&gb, 24); /* bitrate */
2700 skip_bits(&gb, 6); /* last(?) quality index */
2702 if (s->theora >= 0x030300)
2704 skip_bits(&gb, 5); /* keyframe frequency force */
2705 skip_bits(&gb, 5); /* spare bits */
2708 // align_get_bits(&gb);
2710 avctx->width = s->width;
2711 avctx->height = s->height;
2713 vp3_decode_init(avctx);
2718 static int theora_decode_comments(AVCodecContext *avctx, GetBitContext gb)
2720 int nb_comments, i, tmp;
2722 tmp = get_bits_long(&gb, 32);
2723 tmp = be2me_32(tmp);
2727 nb_comments = get_bits_long(&gb, 32);
2728 nb_comments = be2me_32(nb_comments);
2729 for (i = 0; i < nb_comments; i++)
2731 tmp = get_bits_long(&gb, 32);
2732 tmp = be2me_32(tmp);
2740 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb)
2742 Vp3DecodeContext *s = avctx->priv_data;
2745 /* quality threshold table */
2746 for (i = 0; i < 64; i++)
2747 s->coded_ac_scale_factor[i] = get_bits(&gb, 16);
2749 /* dc scale factor table */
2750 for (i = 0; i < 64; i++)
2751 s->coded_dc_scale_factor[i] = get_bits(&gb, 16);
2754 for (i = 0; i < 64; i++)
2755 s->coded_intra_y_dequant[i] = get_bits(&gb, 8);
2758 for (i = 0; i < 64; i++)
2759 s->coded_intra_c_dequant[i] = get_bits(&gb, 8);
2762 for (i = 0; i < 64; i++)
2763 s->coded_inter_dequant[i] = get_bits(&gb, 8);
2765 /* FIXME: read huffmann tree.. */
2767 s->theora_tables = 1;
2772 static int theora_decode_init(AVCodecContext *avctx)
2774 Vp3DecodeContext *s = avctx->priv_data;
2780 if (!avctx->extradata_size)
2783 init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
2785 ptype = get_bits(&gb, 8);
2786 debug_vp3("Theora headerpacket type: %x\n", ptype);
2788 if (!(ptype & 0x80))
2791 skip_bits(&gb, 6*8); /* "theora" */
2796 theora_decode_header(avctx, gb);
2797 vp3_decode_init(avctx);
2800 theora_decode_comments(avctx, gb);
2803 theora_decode_tables(avctx, gb);
2810 AVCodec vp3_decoder = {
2814 sizeof(Vp3DecodeContext),
2823 AVCodec theora_decoder = {
2827 sizeof(Vp3DecodeContext),