2 * Copyright (C) 2003-2004 the ffmpeg project
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * On2 VP3 Video Decoder
25 * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
26 * For more information about the VP3 coding process, visit:
27 * http://wiki.multimedia.cx/index.php?title=On2_VP3
29 * Theora decoder by Alex Beregszaszi
39 #include "bitstream.h"
44 #define FRAGMENT_PIXELS 8
49 * Define one or more of the following compile-time variables to 1 to obtain
50 * elaborate information about certain aspects of the decoding process.
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
66 #define DEBUG_DEQUANTIZERS 0
67 #define DEBUG_BLOCK_CODING 0
69 #define DEBUG_VECTORS 0
72 #define DEBUG_DC_PRED 0
76 #define debug_vp3(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
78 static inline void debug_vp3(const char *format, ...) { }
82 #define debug_init(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
84 static inline void debug_init(const char *format, ...) { }
87 #if DEBUG_DEQUANTIZERS
88 #define debug_dequantizers(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
90 static inline void debug_dequantizers(const char *format, ...) { }
93 #if DEBUG_BLOCK_CODING
94 #define debug_block_coding(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
96 static inline void debug_block_coding(const char *format, ...) { }
100 #define debug_modes(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
102 static inline void debug_modes(const char *format, ...) { }
106 #define debug_vectors(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
108 static inline void debug_vectors(const char *format, ...) { }
112 #define debug_token(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
114 static inline void debug_token(const char *format, ...) { }
118 #define debug_vlc(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
120 static inline void debug_vlc(const char *format, ...) { }
124 #define debug_dc_pred(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
126 static inline void debug_dc_pred(const char *format, ...) { }
130 #define debug_idct(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
132 static inline void debug_idct(const char *format, ...) { }
135 typedef struct Coeff {
141 //FIXME split things out into their own arrays
142 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 */
149 uint8_t coding_method;
154 #define SB_NOT_CODED 0
155 #define SB_PARTIALLY_CODED 1
156 #define SB_FULLY_CODED 2
158 #define MODE_INTER_NO_MV 0
160 #define MODE_INTER_PLUS_MV 2
161 #define MODE_INTER_LAST_MV 3
162 #define MODE_INTER_PRIOR_LAST 4
163 #define MODE_USING_GOLDEN 5
164 #define MODE_GOLDEN_MV 6
165 #define MODE_INTER_FOURMV 7
166 #define CODING_MODE_COUNT 8
168 /* special internal mode */
171 /* There are 6 preset schemes, plus a free-form scheme */
172 static const int ModeAlphabet[6][CODING_MODE_COUNT] =
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;
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 uint8_t *coeff_counts;
254 int fragment_start[3];
259 uint16_t coded_dc_scale_factor[64];
260 uint32_t coded_ac_scale_factor[64];
261 uint8_t base_matrix[384][64];
262 uint8_t qr_count[2][3];
263 uint8_t qr_size [2][3][64];
264 uint16_t qr_base[2][3][64];
266 /* this is a list of indexes into the all_fragments array indicating
267 * which of the fragments are coded */
268 int *coded_fragment_list;
269 int coded_fragment_list_index;
270 int pixel_addresses_initialized;
278 VLC superblock_run_length_vlc;
279 VLC fragment_run_length_vlc;
281 VLC motion_vector_vlc;
283 /* these arrays need to be on 16-byte boundaries since SSE2 operations
285 DECLARE_ALIGNED_16(int16_t, qmat[2][4][64]); //<qmat[is_inter][plane]
287 /* This table contains superblock_count * 16 entries. Each set of 16
288 * numbers corresponds to the fragment indexes 0..15 of the superblock.
289 * An entry will be -1 to indicate that no entry corresponds to that
291 int *superblock_fragments;
293 /* This table contains superblock_count * 4 entries. Each set of 4
294 * numbers corresponds to the macroblock indexes 0..3 of the superblock.
295 * An entry will be -1 to indicate that no entry corresponds to that
297 int *superblock_macroblocks;
299 /* This table contains macroblock_count * 6 entries. Each set of 6
300 * numbers corresponds to the fragment indexes 0..5 which comprise
301 * the macroblock (4 Y fragments and 2 C fragments). */
302 int *macroblock_fragments;
303 /* This is an array that indicates how a particular macroblock
305 unsigned char *macroblock_coding;
307 int first_coded_y_fragment;
308 int first_coded_c_fragment;
309 int last_coded_y_fragment;
310 int last_coded_c_fragment;
312 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
313 int8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
320 uint16_t huffman_table[80][32][2];
322 uint32_t filter_limit_values[64];
323 int bounding_values_array[256];
326 /************************************************************************
327 * VP3 specific functions
328 ************************************************************************/
331 * This function sets up all of the various blocks mappings:
332 * superblocks <-> fragments, macroblocks <-> fragments,
333 * superblocks <-> macroblocks
335 * Returns 0 is successful; returns 1 if *anything* went wrong.
337 static int init_block_mapping(Vp3DecodeContext *s)
340 signed int hilbert_walk_mb[4];
342 int current_fragment = 0;
343 int current_width = 0;
344 int current_height = 0;
347 int superblock_row_inc = 0;
349 int mapping_index = 0;
351 int current_macroblock;
354 signed char travel_width[16] = {
361 signed char travel_height[16] = {
368 signed char travel_width_mb[4] = {
372 signed char travel_height_mb[4] = {
376 debug_vp3(" vp3: initialize block mapping tables\n");
378 hilbert_walk_mb[0] = 1;
379 hilbert_walk_mb[1] = s->macroblock_width;
380 hilbert_walk_mb[2] = 1;
381 hilbert_walk_mb[3] = -s->macroblock_width;
383 /* iterate through each superblock (all planes) and map the fragments */
384 for (i = 0; i < s->superblock_count; i++) {
385 debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
386 i, s->u_superblock_start, s->v_superblock_start);
388 /* time to re-assign the limits? */
391 /* start of Y superblocks */
392 right_edge = s->fragment_width;
393 bottom_edge = s->fragment_height;
396 superblock_row_inc = 3 * s->fragment_width -
397 (s->y_superblock_width * 4 - s->fragment_width);
399 /* the first operation for this variable is to advance by 1 */
400 current_fragment = -1;
402 } else if (i == s->u_superblock_start) {
404 /* start of U superblocks */
405 right_edge = s->fragment_width / 2;
406 bottom_edge = s->fragment_height / 2;
409 superblock_row_inc = 3 * (s->fragment_width / 2) -
410 (s->c_superblock_width * 4 - s->fragment_width / 2);
412 /* the first operation for this variable is to advance by 1 */
413 current_fragment = s->fragment_start[1] - 1;
415 } else if (i == s->v_superblock_start) {
417 /* start of V superblocks */
418 right_edge = s->fragment_width / 2;
419 bottom_edge = s->fragment_height / 2;
422 superblock_row_inc = 3 * (s->fragment_width / 2) -
423 (s->c_superblock_width * 4 - s->fragment_width / 2);
425 /* the first operation for this variable is to advance by 1 */
426 current_fragment = s->fragment_start[2] - 1;
430 if (current_width >= right_edge - 1) {
431 /* reset width and move to next superblock row */
435 /* fragment is now at the start of a new superblock row */
436 current_fragment += superblock_row_inc;
439 /* iterate through all 16 fragments in a superblock */
440 for (j = 0; j < 16; j++) {
441 current_fragment += travel_width[j] + right_edge * travel_height[j];
442 current_width += travel_width[j];
443 current_height += travel_height[j];
445 /* check if the fragment is in bounds */
446 if ((current_width < right_edge) &&
447 (current_height < bottom_edge)) {
448 s->superblock_fragments[mapping_index] = current_fragment;
449 debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
450 s->superblock_fragments[mapping_index], i, j,
451 current_width, right_edge, current_height, bottom_edge);
453 s->superblock_fragments[mapping_index] = -1;
454 debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
456 current_width, right_edge, current_height, bottom_edge);
463 /* initialize the superblock <-> macroblock mapping; iterate through
464 * all of the Y plane superblocks to build this mapping */
465 right_edge = s->macroblock_width;
466 bottom_edge = s->macroblock_height;
469 superblock_row_inc = s->macroblock_width -
470 (s->y_superblock_width * 2 - s->macroblock_width);
471 hilbert = hilbert_walk_mb;
473 current_macroblock = -1;
474 for (i = 0; i < s->u_superblock_start; i++) {
476 if (current_width >= right_edge - 1) {
477 /* reset width and move to next superblock row */
481 /* macroblock is now at the start of a new superblock row */
482 current_macroblock += superblock_row_inc;
485 /* iterate through each potential macroblock in the superblock */
486 for (j = 0; j < 4; j++) {
487 current_macroblock += hilbert_walk_mb[j];
488 current_width += travel_width_mb[j];
489 current_height += travel_height_mb[j];
491 /* check if the macroblock is in bounds */
492 if ((current_width < right_edge) &&
493 (current_height < bottom_edge)) {
494 s->superblock_macroblocks[mapping_index] = current_macroblock;
495 debug_init(" mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
496 s->superblock_macroblocks[mapping_index], i, j,
497 current_width, right_edge, current_height, bottom_edge);
499 s->superblock_macroblocks[mapping_index] = -1;
500 debug_init(" superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
502 current_width, right_edge, current_height, bottom_edge);
509 /* initialize the macroblock <-> fragment mapping */
510 current_fragment = 0;
511 current_macroblock = 0;
513 for (i = 0; i < s->fragment_height; i += 2) {
515 for (j = 0; j < s->fragment_width; j += 2) {
517 debug_init(" macroblock %d contains fragments: ", current_macroblock);
518 s->all_fragments[current_fragment].macroblock = current_macroblock;
519 s->macroblock_fragments[mapping_index++] = current_fragment;
520 debug_init("%d ", current_fragment);
522 if (j + 1 < s->fragment_width) {
523 s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
524 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
525 debug_init("%d ", current_fragment + 1);
527 s->macroblock_fragments[mapping_index++] = -1;
529 if (i + 1 < s->fragment_height) {
530 s->all_fragments[current_fragment + s->fragment_width].macroblock =
532 s->macroblock_fragments[mapping_index++] =
533 current_fragment + s->fragment_width;
534 debug_init("%d ", current_fragment + s->fragment_width);
536 s->macroblock_fragments[mapping_index++] = -1;
538 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
539 s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
541 s->macroblock_fragments[mapping_index++] =
542 current_fragment + s->fragment_width + 1;
543 debug_init("%d ", current_fragment + s->fragment_width + 1);
545 s->macroblock_fragments[mapping_index++] = -1;
548 c_fragment = s->fragment_start[1] +
549 (i * s->fragment_width / 4) + (j / 2);
550 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
551 s->macroblock_fragments[mapping_index++] = c_fragment;
552 debug_init("%d ", c_fragment);
554 c_fragment = s->fragment_start[2] +
555 (i * s->fragment_width / 4) + (j / 2);
556 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
557 s->macroblock_fragments[mapping_index++] = c_fragment;
558 debug_init("%d ", c_fragment);
562 if (j + 2 <= s->fragment_width)
563 current_fragment += 2;
566 current_macroblock++;
569 current_fragment += s->fragment_width;
572 return 0; /* successful path out */
576 * This function wipes out all of the fragment data.
578 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
582 /* zero out all of the fragment information */
583 s->coded_fragment_list_index = 0;
584 for (i = 0; i < s->fragment_count; i++) {
585 s->coeff_counts[i] = 0;
586 s->all_fragments[i].motion_x = 127;
587 s->all_fragments[i].motion_y = 127;
588 s->all_fragments[i].next_coeff= NULL;
590 s->coeffs[i].coeff=0;
591 s->coeffs[i].next= NULL;
596 * This function sets up the dequantization tables used for a particular
599 static void init_dequantizer(Vp3DecodeContext *s)
601 int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index];
602 int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index];
603 int i, plane, inter, qri, bmi, bmj, qistart;
605 debug_vp3(" vp3: initializing dequantization tables\n");
607 for(inter=0; inter<2; inter++){
608 for(plane=0; plane<3; plane++){
610 for(qri=0; qri<s->qr_count[inter][plane]; qri++){
611 sum+= s->qr_size[inter][plane][qri];
612 if(s->quality_index <= sum)
615 qistart= sum - s->qr_size[inter][plane][qri];
616 bmi= s->qr_base[inter][plane][qri ];
617 bmj= s->qr_base[inter][plane][qri+1];
619 int coeff= ( 2*(sum -s->quality_index)*s->base_matrix[bmi][i]
620 - 2*(qistart-s->quality_index)*s->base_matrix[bmj][i]
621 + s->qr_size[inter][plane][qri])
622 / (2*s->qr_size[inter][plane][qri]);
624 int qmin= 8<<(inter + !i);
625 int qscale= i ? ac_scale_factor : dc_scale_factor;
627 s->qmat[inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
632 memset(s->qscale_table, (FFMAX(s->qmat[0][0][1], s->qmat[0][1][1])+8)/16, 512); //FIXME finetune
636 * This function initializes the loop filter boundary limits if the frame's
637 * quality index is different from the previous frame's.
639 static void init_loop_filter(Vp3DecodeContext *s)
641 int *bounding_values= s->bounding_values_array+127;
645 filter_limit = s->filter_limit_values[s->quality_index];
647 /* set up the bounding values */
648 memset(s->bounding_values_array, 0, 256 * sizeof(int));
649 for (x = 0; x < filter_limit; x++) {
650 bounding_values[-x - filter_limit] = -filter_limit + x;
651 bounding_values[-x] = -x;
652 bounding_values[x] = x;
653 bounding_values[x + filter_limit] = filter_limit - x;
658 * This function unpacks all of the superblock/macroblock/fragment coding
659 * information from the bitstream.
661 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
664 int current_superblock = 0;
666 int decode_fully_flags = 0;
667 int decode_partial_blocks = 0;
668 int first_c_fragment_seen;
671 int current_fragment;
673 debug_vp3(" vp3: unpacking superblock coding\n");
677 debug_vp3(" keyframe-- all superblocks are fully coded\n");
678 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
682 /* unpack the list of partially-coded superblocks */
684 /* toggle the bit because as soon as the first run length is
685 * fetched the bit will be toggled again */
687 while (current_superblock < s->superblock_count) {
688 if (current_run-- == 0) {
690 current_run = get_vlc2(gb,
691 s->superblock_run_length_vlc.table, 6, 2);
692 if (current_run == 33)
693 current_run += get_bits(gb, 12);
694 debug_block_coding(" setting superblocks %d..%d to %s\n",
696 current_superblock + current_run - 1,
697 (bit) ? "partially coded" : "not coded");
699 /* if any of the superblocks are not partially coded, flag
700 * a boolean to decode the list of fully-coded superblocks */
702 decode_fully_flags = 1;
705 /* make a note of the fact that there are partially coded
707 decode_partial_blocks = 1;
710 s->superblock_coding[current_superblock++] = bit;
713 /* unpack the list of fully coded superblocks if any of the blocks were
714 * not marked as partially coded in the previous step */
715 if (decode_fully_flags) {
717 current_superblock = 0;
720 /* toggle the bit because as soon as the first run length is
721 * fetched the bit will be toggled again */
723 while (current_superblock < s->superblock_count) {
725 /* skip any superblocks already marked as partially coded */
726 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
728 if (current_run-- == 0) {
730 current_run = get_vlc2(gb,
731 s->superblock_run_length_vlc.table, 6, 2);
732 if (current_run == 33)
733 current_run += get_bits(gb, 12);
736 debug_block_coding(" setting superblock %d to %s\n",
738 (bit) ? "fully coded" : "not coded");
739 s->superblock_coding[current_superblock] = 2*bit;
741 current_superblock++;
745 /* if there were partial blocks, initialize bitstream for
746 * unpacking fragment codings */
747 if (decode_partial_blocks) {
751 /* toggle the bit because as soon as the first run length is
752 * fetched the bit will be toggled again */
757 /* figure out which fragments are coded; iterate through each
758 * superblock (all planes) */
759 s->coded_fragment_list_index = 0;
760 s->next_coeff= s->coeffs + s->fragment_count;
761 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
762 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
763 first_c_fragment_seen = 0;
764 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
765 for (i = 0; i < s->superblock_count; i++) {
767 /* iterate through all 16 fragments in a superblock */
768 for (j = 0; j < 16; j++) {
770 /* if the fragment is in bounds, check its coding status */
771 current_fragment = s->superblock_fragments[i * 16 + j];
772 if (current_fragment >= s->fragment_count) {
773 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
774 current_fragment, s->fragment_count);
777 if (current_fragment != -1) {
778 if (s->superblock_coding[i] == SB_NOT_CODED) {
780 /* copy all the fragments from the prior frame */
781 s->all_fragments[current_fragment].coding_method =
784 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
786 /* fragment may or may not be coded; this is the case
787 * that cares about the fragment coding runs */
788 if (current_run-- == 0) {
790 current_run = get_vlc2(gb,
791 s->fragment_run_length_vlc.table, 5, 2);
795 /* default mode; actual mode will be decoded in
797 s->all_fragments[current_fragment].coding_method =
799 s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
800 s->coded_fragment_list[s->coded_fragment_list_index] =
802 if ((current_fragment >= s->fragment_start[1]) &&
803 (s->last_coded_y_fragment == -1) &&
804 (!first_c_fragment_seen)) {
805 s->first_coded_c_fragment = s->coded_fragment_list_index;
806 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
807 first_c_fragment_seen = 1;
809 s->coded_fragment_list_index++;
810 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
811 debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
812 i, current_fragment);
814 /* not coded; copy this fragment from the prior frame */
815 s->all_fragments[current_fragment].coding_method =
817 debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
818 i, current_fragment);
823 /* fragments are fully coded in this superblock; actual
824 * coding will be determined in next step */
825 s->all_fragments[current_fragment].coding_method =
827 s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
828 s->coded_fragment_list[s->coded_fragment_list_index] =
830 if ((current_fragment >= s->fragment_start[1]) &&
831 (s->last_coded_y_fragment == -1) &&
832 (!first_c_fragment_seen)) {
833 s->first_coded_c_fragment = s->coded_fragment_list_index;
834 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
835 first_c_fragment_seen = 1;
837 s->coded_fragment_list_index++;
838 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
839 debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
840 i, current_fragment);
846 if (!first_c_fragment_seen)
847 /* only Y fragments coded in this frame */
848 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
850 /* end the list of coded C fragments */
851 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
853 debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
854 s->coded_fragment_list_index,
855 s->first_coded_y_fragment,
856 s->last_coded_y_fragment,
857 s->first_coded_c_fragment,
858 s->last_coded_c_fragment);
864 * This function unpacks all the coding mode data for individual macroblocks
865 * from the bitstream.
867 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
871 int current_macroblock;
872 int current_fragment;
874 int custom_mode_alphabet[CODING_MODE_COUNT];
876 debug_vp3(" vp3: unpacking encoding modes\n");
879 debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
881 for (i = 0; i < s->fragment_count; i++)
882 s->all_fragments[i].coding_method = MODE_INTRA;
886 /* fetch the mode coding scheme for this frame */
887 scheme = get_bits(gb, 3);
888 debug_modes(" using mode alphabet %d\n", scheme);
890 /* is it a custom coding scheme? */
892 debug_modes(" custom mode alphabet ahead:\n");
893 for (i = 0; i < 8; i++)
894 custom_mode_alphabet[get_bits(gb, 3)] = i;
897 for (i = 0; i < 8; i++) {
899 debug_modes(" mode[%d][%d] = %d\n", scheme, i,
900 ModeAlphabet[scheme-1][i]);
902 debug_modes(" mode[0][%d] = %d\n", i,
903 custom_mode_alphabet[i]);
906 /* iterate through all of the macroblocks that contain 1 or more
908 for (i = 0; i < s->u_superblock_start; i++) {
910 for (j = 0; j < 4; j++) {
911 current_macroblock = s->superblock_macroblocks[i * 4 + j];
912 if ((current_macroblock == -1) ||
913 (s->macroblock_coding[current_macroblock] == MODE_COPY))
915 if (current_macroblock >= s->macroblock_count) {
916 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
917 current_macroblock, s->macroblock_count);
921 /* mode 7 means get 3 bits for each coding mode */
923 coding_mode = get_bits(gb, 3);
925 coding_mode = custom_mode_alphabet
926 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
928 coding_mode = ModeAlphabet[scheme-1]
929 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
931 s->macroblock_coding[current_macroblock] = coding_mode;
932 for (k = 0; k < 6; k++) {
934 s->macroblock_fragments[current_macroblock * 6 + k];
935 if (current_fragment == -1)
937 if (current_fragment >= s->fragment_count) {
938 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
939 current_fragment, s->fragment_count);
942 if (s->all_fragments[current_fragment].coding_method !=
944 s->all_fragments[current_fragment].coding_method =
948 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
949 s->macroblock_fragments[current_macroblock * 6], coding_mode);
958 * This function unpacks all the motion vectors for the individual
959 * macroblocks from the bitstream.
961 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
967 int last_motion_x = 0;
968 int last_motion_y = 0;
969 int prior_last_motion_x = 0;
970 int prior_last_motion_y = 0;
971 int current_macroblock;
972 int current_fragment;
974 debug_vp3(" vp3: unpacking motion vectors\n");
977 debug_vp3(" keyframe-- there are no motion vectors\n");
981 memset(motion_x, 0, 6 * sizeof(int));
982 memset(motion_y, 0, 6 * sizeof(int));
984 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
985 coding_mode = get_bits1(gb);
986 debug_vectors(" using %s scheme for unpacking motion vectors\n",
987 (coding_mode == 0) ? "VLC" : "fixed-length");
989 /* iterate through all of the macroblocks that contain 1 or more
991 for (i = 0; i < s->u_superblock_start; i++) {
993 for (j = 0; j < 4; j++) {
994 current_macroblock = s->superblock_macroblocks[i * 4 + j];
995 if ((current_macroblock == -1) ||
996 (s->macroblock_coding[current_macroblock] == MODE_COPY))
998 if (current_macroblock >= s->macroblock_count) {
999 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
1000 current_macroblock, s->macroblock_count);
1004 current_fragment = s->macroblock_fragments[current_macroblock * 6];
1005 if (current_fragment >= s->fragment_count) {
1006 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
1007 current_fragment, s->fragment_count);
1010 switch (s->macroblock_coding[current_macroblock]) {
1012 case MODE_INTER_PLUS_MV:
1013 case MODE_GOLDEN_MV:
1014 /* all 6 fragments use the same motion vector */
1015 if (coding_mode == 0) {
1016 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
1017 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
1019 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
1020 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
1023 for (k = 1; k < 6; k++) {
1024 motion_x[k] = motion_x[0];
1025 motion_y[k] = motion_y[0];
1028 /* vector maintenance, only on MODE_INTER_PLUS_MV */
1029 if (s->macroblock_coding[current_macroblock] ==
1030 MODE_INTER_PLUS_MV) {
1031 prior_last_motion_x = last_motion_x;
1032 prior_last_motion_y = last_motion_y;
1033 last_motion_x = motion_x[0];
1034 last_motion_y = motion_y[0];
1038 case MODE_INTER_FOURMV:
1039 /* vector maintenance */
1040 prior_last_motion_x = last_motion_x;
1041 prior_last_motion_y = last_motion_y;
1043 /* fetch 4 vectors from the bitstream, one for each
1044 * Y fragment, then average for the C fragment vectors */
1045 motion_x[4] = motion_y[4] = 0;
1046 for (k = 0; k < 4; k++) {
1047 for (l = 0; l < s->coded_fragment_list_index; l++)
1048 if (s->coded_fragment_list[l] == s->macroblock_fragments[6*current_macroblock + k])
1050 if (l < s->coded_fragment_list_index) {
1051 if (coding_mode == 0) {
1052 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
1053 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
1055 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
1056 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
1058 last_motion_x = motion_x[k];
1059 last_motion_y = motion_y[k];
1064 motion_x[4] += motion_x[k];
1065 motion_y[4] += motion_y[k];
1069 motion_x[4]= RSHIFT(motion_x[4], 2);
1071 motion_y[4]= RSHIFT(motion_y[4], 2);
1074 case MODE_INTER_LAST_MV:
1075 /* all 6 fragments use the last motion vector */
1076 motion_x[0] = last_motion_x;
1077 motion_y[0] = last_motion_y;
1078 for (k = 1; k < 6; k++) {
1079 motion_x[k] = motion_x[0];
1080 motion_y[k] = motion_y[0];
1083 /* no vector maintenance (last vector remains the
1087 case MODE_INTER_PRIOR_LAST:
1088 /* all 6 fragments use the motion vector prior to the
1089 * last motion vector */
1090 motion_x[0] = prior_last_motion_x;
1091 motion_y[0] = prior_last_motion_y;
1092 for (k = 1; k < 6; k++) {
1093 motion_x[k] = motion_x[0];
1094 motion_y[k] = motion_y[0];
1097 /* vector maintenance */
1098 prior_last_motion_x = last_motion_x;
1099 prior_last_motion_y = last_motion_y;
1100 last_motion_x = motion_x[0];
1101 last_motion_y = motion_y[0];
1105 /* covers intra, inter without MV, golden without MV */
1106 memset(motion_x, 0, 6 * sizeof(int));
1107 memset(motion_y, 0, 6 * sizeof(int));
1109 /* no vector maintenance */
1113 /* assign the motion vectors to the correct fragments */
1114 debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
1116 s->macroblock_coding[current_macroblock]);
1117 for (k = 0; k < 6; k++) {
1119 s->macroblock_fragments[current_macroblock * 6 + k];
1120 if (current_fragment == -1)
1122 if (current_fragment >= s->fragment_count) {
1123 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
1124 current_fragment, s->fragment_count);
1127 s->all_fragments[current_fragment].motion_x = motion_x[k];
1128 s->all_fragments[current_fragment].motion_y = motion_y[k];
1129 debug_vectors(" vector %d: fragment %d = (%d, %d)\n",
1130 k, current_fragment, motion_x[k], motion_y[k]);
1140 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1141 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1142 * data. This function unpacks all the VLCs for either the Y plane or both
1143 * C planes, and is called for DC coefficients or different AC coefficient
1144 * levels (since different coefficient types require different VLC tables.
1146 * This function returns a residual eob run. E.g, if a particular token gave
1147 * instructions to EOB the next 5 fragments and there were only 2 fragments
1148 * left in the current fragment range, 3 would be returned so that it could
1149 * be passed into the next call to this same function.
1151 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1152 VLC *table, int coeff_index,
1153 int first_fragment, int last_fragment,
1160 Vp3Fragment *fragment;
1161 uint8_t *perm= s->scantable.permutated;
1164 if ((first_fragment >= s->fragment_count) ||
1165 (last_fragment >= s->fragment_count)) {
1167 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
1168 first_fragment, last_fragment);
1172 for (i = first_fragment; i <= last_fragment; i++) {
1173 int fragment_num = s->coded_fragment_list[i];
1175 if (s->coeff_counts[fragment_num] > coeff_index)
1177 fragment = &s->all_fragments[fragment_num];
1180 /* decode a VLC into a token */
1181 token = get_vlc2(gb, table->table, 5, 3);
1182 debug_vlc(" token = %2d, ", token);
1183 /* use the token to get a zero run, a coefficient, and an eob run */
1185 eob_run = eob_run_base[token];
1186 if (eob_run_get_bits[token])
1187 eob_run += get_bits(gb, eob_run_get_bits[token]);
1188 coeff = zero_run = 0;
1190 bits_to_get = coeff_get_bits[token];
1192 coeff = coeff_tables[token][0];
1194 coeff = coeff_tables[token][get_bits(gb, bits_to_get)];
1196 zero_run = zero_run_base[token];
1197 if (zero_run_get_bits[token])
1198 zero_run += get_bits(gb, zero_run_get_bits[token]);
1203 s->coeff_counts[fragment_num] += zero_run;
1204 if (s->coeff_counts[fragment_num] < 64){
1205 fragment->next_coeff->coeff= coeff;
1206 fragment->next_coeff->index= perm[s->coeff_counts[fragment_num]++]; //FIXME perm here already?
1207 fragment->next_coeff->next= s->next_coeff;
1208 s->next_coeff->next=NULL;
1209 fragment->next_coeff= s->next_coeff++;
1211 debug_vlc(" fragment %d coeff = %d\n",
1212 s->coded_fragment_list[i], fragment->next_coeff[coeff_index]);
1214 s->coeff_counts[fragment_num] |= 128;
1215 debug_vlc(" fragment %d eob with %d coefficients\n",
1216 s->coded_fragment_list[i], s->coeff_counts[fragment_num]&127);
1225 * This function unpacks all of the DCT coefficient data from the
1228 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1235 int residual_eob_run = 0;
1237 /* fetch the DC table indexes */
1238 dc_y_table = get_bits(gb, 4);
1239 dc_c_table = get_bits(gb, 4);
1241 /* unpack the Y plane DC coefficients */
1242 debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
1244 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1245 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1247 /* unpack the C plane DC coefficients */
1248 debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
1250 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1251 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1253 /* fetch the AC table indexes */
1254 ac_y_table = get_bits(gb, 4);
1255 ac_c_table = get_bits(gb, 4);
1257 /* unpack the group 1 AC coefficients (coeffs 1-5) */
1258 for (i = 1; i <= 5; i++) {
1260 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1262 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
1263 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1265 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1267 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
1268 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1271 /* unpack the group 2 AC coefficients (coeffs 6-14) */
1272 for (i = 6; i <= 14; i++) {
1274 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1276 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
1277 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1279 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1281 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
1282 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1285 /* unpack the group 3 AC coefficients (coeffs 15-27) */
1286 for (i = 15; i <= 27; i++) {
1288 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1290 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
1291 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1293 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1295 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
1296 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1299 /* unpack the group 4 AC coefficients (coeffs 28-63) */
1300 for (i = 28; i <= 63; i++) {
1302 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1304 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
1305 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1307 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1309 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
1310 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1317 * This function reverses the DC prediction for each coded fragment in
1318 * the frame. Much of this function is adapted directly from the original
1321 #define COMPATIBLE_FRAME(x) \
1322 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1323 #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
1324 #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
1326 static void reverse_dc_prediction(Vp3DecodeContext *s,
1329 int fragment_height)
1338 int i = first_fragment;
1342 /* DC values for the left, up-left, up, and up-right fragments */
1343 int vl, vul, vu, vur;
1345 /* indexes for the left, up-left, up, and up-right fragments */
1349 * The 6 fields mean:
1350 * 0: up-left multiplier
1352 * 2: up-right multiplier
1353 * 3: left multiplier
1355 int predictor_transform[16][4] = {
1357 { 0, 0, 0,128}, // PL
1358 { 0, 0,128, 0}, // PUR
1359 { 0, 0, 53, 75}, // PUR|PL
1360 { 0,128, 0, 0}, // PU
1361 { 0, 64, 0, 64}, // PU|PL
1362 { 0,128, 0, 0}, // PU|PUR
1363 { 0, 0, 53, 75}, // PU|PUR|PL
1364 {128, 0, 0, 0}, // PUL
1365 { 0, 0, 0,128}, // PUL|PL
1366 { 64, 0, 64, 0}, // PUL|PUR
1367 { 0, 0, 53, 75}, // PUL|PUR|PL
1368 { 0,128, 0, 0}, // PUL|PU
1369 {-104,116, 0,116}, // PUL|PU|PL
1370 { 24, 80, 24, 0}, // PUL|PU|PUR
1371 {-104,116, 0,116} // PUL|PU|PUR|PL
1374 /* This table shows which types of blocks can use other blocks for
1375 * prediction. For example, INTRA is the only mode in this table to
1376 * have a frame number of 0. That means INTRA blocks can only predict
1377 * from other INTRA blocks. There are 2 golden frame coding types;
1378 * blocks encoding in these modes can only predict from other blocks
1379 * that were encoded with these 1 of these 2 modes. */
1380 unsigned char compatible_frame[8] = {
1381 1, /* MODE_INTER_NO_MV */
1383 1, /* MODE_INTER_PLUS_MV */
1384 1, /* MODE_INTER_LAST_MV */
1385 1, /* MODE_INTER_PRIOR_MV */
1386 2, /* MODE_USING_GOLDEN */
1387 2, /* MODE_GOLDEN_MV */
1388 1 /* MODE_INTER_FOUR_MV */
1390 int current_frame_type;
1392 /* there is a last DC predictor for each of the 3 frame types */
1397 debug_vp3(" vp3: reversing DC prediction\n");
1399 vul = vu = vur = vl = 0;
1400 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1402 /* for each fragment row... */
1403 for (y = 0; y < fragment_height; y++) {
1405 /* for each fragment in a row... */
1406 for (x = 0; x < fragment_width; x++, i++) {
1408 /* reverse prediction if this block was coded */
1409 if (s->all_fragments[i].coding_method != MODE_COPY) {
1411 current_frame_type =
1412 compatible_frame[s->all_fragments[i].coding_method];
1413 debug_dc_pred(" frag %d: orig DC = %d, ",
1420 if(FRAME_CODED(l) && COMPATIBLE_FRAME(l))
1424 u= i-fragment_width;
1426 if(FRAME_CODED(u) && COMPATIBLE_FRAME(u))
1429 ul= i-fragment_width-1;
1431 if(FRAME_CODED(ul) && COMPATIBLE_FRAME(ul))
1434 if(x + 1 < fragment_width){
1435 ur= i-fragment_width+1;
1437 if(FRAME_CODED(ur) && COMPATIBLE_FRAME(ur))
1442 debug_dc_pred("transform = %d, ", transform);
1444 if (transform == 0) {
1446 /* if there were no fragments to predict from, use last
1448 predicted_dc = last_dc[current_frame_type];
1449 debug_dc_pred("from last DC (%d) = %d\n",
1450 current_frame_type, DC_COEFF(i));
1454 /* apply the appropriate predictor transform */
1456 (predictor_transform[transform][0] * vul) +
1457 (predictor_transform[transform][1] * vu) +
1458 (predictor_transform[transform][2] * vur) +
1459 (predictor_transform[transform][3] * vl);
1461 predicted_dc /= 128;
1463 /* check for outranging on the [ul u l] and
1464 * [ul u ur l] predictors */
1465 if ((transform == 13) || (transform == 15)) {
1466 if (FFABS(predicted_dc - vu) > 128)
1468 else if (FFABS(predicted_dc - vl) > 128)
1470 else if (FFABS(predicted_dc - vul) > 128)
1474 debug_dc_pred("from pred DC = %d\n",
1478 /* at long last, apply the predictor */
1479 if(s->coeffs[i].index){
1480 *s->next_coeff= s->coeffs[i];
1481 s->coeffs[i].index=0;
1482 s->coeffs[i].coeff=0;
1483 s->coeffs[i].next= s->next_coeff++;
1485 s->coeffs[i].coeff += predicted_dc;
1487 last_dc[current_frame_type] = DC_COEFF(i);
1488 if(DC_COEFF(i) && !(s->coeff_counts[i]&127)){
1489 s->coeff_counts[i]= 129;
1490 // s->all_fragments[i].next_coeff= s->next_coeff;
1491 s->coeffs[i].next= s->next_coeff;
1492 (s->next_coeff++)->next=NULL;
1500 static void horizontal_filter(unsigned char *first_pixel, int stride,
1501 int *bounding_values);
1502 static void vertical_filter(unsigned char *first_pixel, int stride,
1503 int *bounding_values);
1506 * Perform the final rendering for a particular slice of data.
1507 * The slice number ranges from 0..(macroblock_height - 1).
1509 static void render_slice(Vp3DecodeContext *s, int slice)
1513 int16_t *dequantizer;
1514 DECLARE_ALIGNED_16(DCTELEM, block[64]);
1515 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1516 int motion_halfpel_index;
1517 uint8_t *motion_source;
1519 int current_macroblock_entry = slice * s->macroblock_width * 6;
1521 if (slice >= s->macroblock_height)
1524 for (plane = 0; plane < 3; plane++) {
1525 uint8_t *output_plane = s->current_frame.data [plane];
1526 uint8_t * last_plane = s-> last_frame.data [plane];
1527 uint8_t *golden_plane = s-> golden_frame.data [plane];
1528 int stride = s->current_frame.linesize[plane];
1529 int plane_width = s->width >> !!plane;
1530 int plane_height = s->height >> !!plane;
1531 int y = slice * FRAGMENT_PIXELS << !plane ;
1532 int slice_height = y + (FRAGMENT_PIXELS << !plane);
1533 int i = s->macroblock_fragments[current_macroblock_entry + plane + 3*!!plane];
1535 if (!s->flipped_image) stride = -stride;
1538 if(FFABS(stride) > 2048)
1539 return; //various tables are fixed size
1541 /* for each fragment row in the slice (both of them)... */
1542 for (; y < slice_height; y += 8) {
1544 /* for each fragment in a row... */
1545 for (x = 0; x < plane_width; x += 8, i++) {
1547 if ((i < 0) || (i >= s->fragment_count)) {
1548 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
1552 /* transform if this block was coded */
1553 if ((s->all_fragments[i].coding_method != MODE_COPY) &&
1554 !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
1556 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
1557 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
1558 motion_source= golden_plane;
1560 motion_source= last_plane;
1562 motion_source += s->all_fragments[i].first_pixel;
1563 motion_halfpel_index = 0;
1565 /* sort out the motion vector if this fragment is coded
1566 * using a motion vector method */
1567 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1568 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
1570 motion_x = s->all_fragments[i].motion_x;
1571 motion_y = s->all_fragments[i].motion_y;
1573 motion_x= (motion_x>>1) | (motion_x&1);
1574 motion_y= (motion_y>>1) | (motion_y&1);
1577 src_x= (motion_x>>1) + x;
1578 src_y= (motion_y>>1) + y;
1579 if ((motion_x == 127) || (motion_y == 127))
1580 av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
1582 motion_halfpel_index = motion_x & 0x01;
1583 motion_source += (motion_x >> 1);
1585 motion_halfpel_index |= (motion_y & 0x01) << 1;
1586 motion_source += ((motion_y >> 1) * stride);
1588 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
1589 uint8_t *temp= s->edge_emu_buffer;
1590 if(stride<0) temp -= 9*stride;
1591 else temp += 9*stride;
1593 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
1594 motion_source= temp;
1599 /* first, take care of copying a block from either the
1600 * previous or the golden frame */
1601 if (s->all_fragments[i].coding_method != MODE_INTRA) {
1602 /* Note, it is possible to implement all MC cases with
1603 put_no_rnd_pixels_l2 which would look more like the
1604 VP3 source but this would be slower as
1605 put_no_rnd_pixels_tab is better optimzed */
1606 if(motion_halfpel_index != 3){
1607 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
1608 output_plane + s->all_fragments[i].first_pixel,
1609 motion_source, stride, 8);
1611 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
1612 s->dsp.put_no_rnd_pixels_l2[1](
1613 output_plane + s->all_fragments[i].first_pixel,
1615 motion_source + stride + 1 + d,
1618 dequantizer = s->qmat[1][plane];
1620 dequantizer = s->qmat[0][plane];
1623 /* dequantize the DCT coefficients */
1624 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
1625 i, s->all_fragments[i].coding_method,
1626 DC_COEFF(i), dequantizer[0]);
1628 if(s->avctx->idct_algo==FF_IDCT_VP3){
1629 Coeff *coeff= s->coeffs + i;
1630 memset(block, 0, sizeof(block));
1632 block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
1636 Coeff *coeff= s->coeffs + i;
1637 memset(block, 0, sizeof(block));
1639 block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
1644 /* invert DCT and place (or add) in final output */
1646 if (s->all_fragments[i].coding_method == MODE_INTRA) {
1647 if(s->avctx->idct_algo!=FF_IDCT_VP3)
1650 output_plane + s->all_fragments[i].first_pixel,
1655 output_plane + s->all_fragments[i].first_pixel,
1660 debug_idct("block after idct_%s():\n",
1661 (s->all_fragments[i].coding_method == MODE_INTRA)?
1663 for (m = 0; m < 8; m++) {
1664 for (n = 0; n < 8; n++) {
1665 debug_idct(" %3d", *(output_plane +
1666 s->all_fragments[i].first_pixel + (m * stride + n)));
1674 /* copy directly from the previous frame */
1675 s->dsp.put_pixels_tab[1][0](
1676 output_plane + s->all_fragments[i].first_pixel,
1677 last_plane + s->all_fragments[i].first_pixel,
1682 /* perform the left edge filter if:
1683 * - the fragment is not on the left column
1684 * - the fragment is coded in this frame
1685 * - the fragment is not coded in this frame but the left
1686 * fragment is coded in this frame (this is done instead
1687 * of a right edge filter when rendering the left fragment
1688 * since this fragment is not available yet) */
1690 ((s->all_fragments[i].coding_method != MODE_COPY) ||
1691 ((s->all_fragments[i].coding_method == MODE_COPY) &&
1692 (s->all_fragments[i - 1].coding_method != MODE_COPY)) )) {
1694 output_plane + s->all_fragments[i].first_pixel + 7*stride,
1695 -stride, s->bounding_values_array + 127);
1698 /* perform the top edge filter if:
1699 * - the fragment is not on the top row
1700 * - the fragment is coded in this frame
1701 * - the fragment is not coded in this frame but the above
1702 * fragment is coded in this frame (this is done instead
1703 * of a bottom edge filter when rendering the above
1704 * fragment since this fragment is not available yet) */
1706 ((s->all_fragments[i].coding_method != MODE_COPY) ||
1707 ((s->all_fragments[i].coding_method == MODE_COPY) &&
1708 (s->all_fragments[i - fragment_width].coding_method != MODE_COPY)) )) {
1710 output_plane + s->all_fragments[i].first_pixel - stride,
1711 -stride, s->bounding_values_array + 127);
1718 /* this looks like a good place for slice dispatch... */
1720 * if (slice == s->macroblock_height - 1)
1721 * dispatch (both last slice & 2nd-to-last slice);
1722 * else if (slice > 0)
1723 * dispatch (slice - 1);
1729 static void horizontal_filter(unsigned char *first_pixel, int stride,
1730 int *bounding_values)
1735 for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
1737 (first_pixel[-2] - first_pixel[ 1])
1738 +3*(first_pixel[ 0] - first_pixel[-1]);
1739 filter_value = bounding_values[(filter_value + 4) >> 3];
1740 first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
1741 first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
1745 static void vertical_filter(unsigned char *first_pixel, int stride,
1746 int *bounding_values)
1750 const int nstride= -stride;
1752 for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
1754 (first_pixel[2 * nstride] - first_pixel[ stride])
1755 +3*(first_pixel[0 ] - first_pixel[nstride]);
1756 filter_value = bounding_values[(filter_value + 4) >> 3];
1757 first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
1758 first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
1762 static void apply_loop_filter(Vp3DecodeContext *s)
1766 int *bounding_values= s->bounding_values_array+127;
1769 int bounding_values_array[256];
1772 /* find the right loop limit value */
1773 for (x = 63; x >= 0; x--) {
1774 if (vp31_ac_scale_factor[x] >= s->quality_index)
1777 filter_limit = vp31_filter_limit_values[s->quality_index];
1779 /* set up the bounding values */
1780 memset(bounding_values_array, 0, 256 * sizeof(int));
1781 for (x = 0; x < filter_limit; x++) {
1782 bounding_values[-x - filter_limit] = -filter_limit + x;
1783 bounding_values[-x] = -x;
1784 bounding_values[x] = x;
1785 bounding_values[x + filter_limit] = filter_limit - x;
1789 for (plane = 0; plane < 3; plane++) {
1790 int width = s->fragment_width >> !!plane;
1791 int height = s->fragment_height >> !!plane;
1792 int fragment = s->fragment_start [plane];
1793 int stride = s->current_frame.linesize[plane];
1794 uint8_t *plane_data = s->current_frame.data [plane];
1795 if (!s->flipped_image) stride = -stride;
1797 for (y = 0; y < height; y++) {
1799 for (x = 0; x < width; x++) {
1800 /* do not perform left edge filter for left columns frags */
1802 (s->all_fragments[fragment].coding_method != MODE_COPY)) {
1804 plane_data + s->all_fragments[fragment].first_pixel,
1805 stride, bounding_values);
1808 /* do not perform top edge filter for top row fragments */
1810 (s->all_fragments[fragment].coding_method != MODE_COPY)) {
1812 plane_data + s->all_fragments[fragment].first_pixel,
1813 stride, bounding_values);
1816 /* do not perform right edge filter for right column
1817 * fragments or if right fragment neighbor is also coded
1818 * in this frame (it will be filtered in next iteration) */
1819 if ((x < width - 1) &&
1820 (s->all_fragments[fragment].coding_method != MODE_COPY) &&
1821 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1823 plane_data + s->all_fragments[fragment + 1].first_pixel,
1824 stride, bounding_values);
1827 /* do not perform bottom edge filter for bottom row
1828 * fragments or if bottom fragment neighbor is also coded
1829 * in this frame (it will be filtered in the next row) */
1830 if ((y < height - 1) &&
1831 (s->all_fragments[fragment].coding_method != MODE_COPY) &&
1832 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1834 plane_data + s->all_fragments[fragment + width].first_pixel,
1835 stride, bounding_values);
1845 * This function computes the first pixel addresses for each fragment.
1846 * This function needs to be invoked after the first frame is allocated
1847 * so that it has access to the plane strides.
1849 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
1854 /* figure out the first pixel addresses for each of the fragments */
1857 for (y = s->fragment_height; y > 0; y--) {
1858 for (x = 0; x < s->fragment_width; x++) {
1859 s->all_fragments[i++].first_pixel =
1860 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
1861 s->golden_frame.linesize[0] +
1862 x * FRAGMENT_PIXELS;
1863 debug_init(" fragment %d, first pixel @ %d\n",
1864 i-1, s->all_fragments[i-1].first_pixel);
1869 i = s->fragment_start[1];
1870 for (y = s->fragment_height / 2; y > 0; y--) {
1871 for (x = 0; x < s->fragment_width / 2; x++) {
1872 s->all_fragments[i++].first_pixel =
1873 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
1874 s->golden_frame.linesize[1] +
1875 x * FRAGMENT_PIXELS;
1876 debug_init(" fragment %d, first pixel @ %d\n",
1877 i-1, s->all_fragments[i-1].first_pixel);
1882 i = s->fragment_start[2];
1883 for (y = s->fragment_height / 2; y > 0; y--) {
1884 for (x = 0; x < s->fragment_width / 2; x++) {
1885 s->all_fragments[i++].first_pixel =
1886 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
1887 s->golden_frame.linesize[2] +
1888 x * FRAGMENT_PIXELS;
1889 debug_init(" fragment %d, first pixel @ %d\n",
1890 i-1, s->all_fragments[i-1].first_pixel);
1895 /* FIXME: this should be merged with the above! */
1896 static void theora_calculate_pixel_addresses(Vp3DecodeContext *s)
1901 /* figure out the first pixel addresses for each of the fragments */
1904 for (y = 1; y <= s->fragment_height; y++) {
1905 for (x = 0; x < s->fragment_width; x++) {
1906 s->all_fragments[i++].first_pixel =
1907 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
1908 s->golden_frame.linesize[0] +
1909 x * FRAGMENT_PIXELS;
1910 debug_init(" fragment %d, first pixel @ %d\n",
1911 i-1, s->all_fragments[i-1].first_pixel);
1916 i = s->fragment_start[1];
1917 for (y = 1; y <= s->fragment_height / 2; y++) {
1918 for (x = 0; x < s->fragment_width / 2; x++) {
1919 s->all_fragments[i++].first_pixel =
1920 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
1921 s->golden_frame.linesize[1] +
1922 x * FRAGMENT_PIXELS;
1923 debug_init(" fragment %d, first pixel @ %d\n",
1924 i-1, s->all_fragments[i-1].first_pixel);
1929 i = s->fragment_start[2];
1930 for (y = 1; y <= s->fragment_height / 2; y++) {
1931 for (x = 0; x < s->fragment_width / 2; x++) {
1932 s->all_fragments[i++].first_pixel =
1933 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
1934 s->golden_frame.linesize[2] +
1935 x * FRAGMENT_PIXELS;
1936 debug_init(" fragment %d, first pixel @ %d\n",
1937 i-1, s->all_fragments[i-1].first_pixel);
1943 * This is the ffmpeg/libavcodec API init function.
1945 static av_cold int vp3_decode_init(AVCodecContext *avctx)
1947 Vp3DecodeContext *s = avctx->priv_data;
1948 int i, inter, plane;
1951 int y_superblock_count;
1952 int c_superblock_count;
1954 if (avctx->codec_tag == MKTAG('V','P','3','0'))
1960 s->width = (avctx->width + 15) & 0xFFFFFFF0;
1961 s->height = (avctx->height + 15) & 0xFFFFFFF0;
1962 avctx->pix_fmt = PIX_FMT_YUV420P;
1963 if(avctx->idct_algo==FF_IDCT_AUTO)
1964 avctx->idct_algo=FF_IDCT_VP3;
1965 dsputil_init(&s->dsp, avctx);
1967 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
1969 /* initialize to an impossible value which will force a recalculation
1970 * in the first frame decode */
1971 s->quality_index = -1;
1973 s->y_superblock_width = (s->width + 31) / 32;
1974 s->y_superblock_height = (s->height + 31) / 32;
1975 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
1977 /* work out the dimensions for the C planes */
1978 c_width = s->width / 2;
1979 c_height = s->height / 2;
1980 s->c_superblock_width = (c_width + 31) / 32;
1981 s->c_superblock_height = (c_height + 31) / 32;
1982 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
1984 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
1985 s->u_superblock_start = y_superblock_count;
1986 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
1987 s->superblock_coding = av_malloc(s->superblock_count);
1989 s->macroblock_width = (s->width + 15) / 16;
1990 s->macroblock_height = (s->height + 15) / 16;
1991 s->macroblock_count = s->macroblock_width * s->macroblock_height;
1993 s->fragment_width = s->width / FRAGMENT_PIXELS;
1994 s->fragment_height = s->height / FRAGMENT_PIXELS;
1996 /* fragment count covers all 8x8 blocks for all 3 planes */
1997 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
1998 s->fragment_start[1] = s->fragment_width * s->fragment_height;
1999 s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
2001 debug_init(" Y plane: %d x %d\n", s->width, s->height);
2002 debug_init(" C plane: %d x %d\n", c_width, c_height);
2003 debug_init(" Y superblocks: %d x %d, %d total\n",
2004 s->y_superblock_width, s->y_superblock_height, y_superblock_count);
2005 debug_init(" C superblocks: %d x %d, %d total\n",
2006 s->c_superblock_width, s->c_superblock_height, c_superblock_count);
2007 debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
2008 s->superblock_count, s->u_superblock_start, s->v_superblock_start);
2009 debug_init(" macroblocks: %d x %d, %d total\n",
2010 s->macroblock_width, s->macroblock_height, s->macroblock_count);
2011 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
2015 s->fragment_start[1],
2016 s->fragment_start[2]);
2018 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
2019 s->coeff_counts = av_malloc(s->fragment_count * sizeof(*s->coeff_counts));
2020 s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
2021 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
2022 s->pixel_addresses_initialized = 0;
2024 if (!s->theora_tables)
2026 for (i = 0; i < 64; i++) {
2027 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
2028 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
2029 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
2030 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
2031 s->base_matrix[2][i] = vp31_inter_dequant[i];
2032 s->filter_limit_values[i] = vp31_filter_limit_values[i];
2035 for(inter=0; inter<2; inter++){
2036 for(plane=0; plane<3; plane++){
2037 s->qr_count[inter][plane]= 1;
2038 s->qr_size [inter][plane][0]= 63;
2039 s->qr_base [inter][plane][0]=
2040 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
2044 /* init VLC tables */
2045 for (i = 0; i < 16; i++) {
2048 init_vlc(&s->dc_vlc[i], 5, 32,
2049 &dc_bias[i][0][1], 4, 2,
2050 &dc_bias[i][0][0], 4, 2, 0);
2052 /* group 1 AC histograms */
2053 init_vlc(&s->ac_vlc_1[i], 5, 32,
2054 &ac_bias_0[i][0][1], 4, 2,
2055 &ac_bias_0[i][0][0], 4, 2, 0);
2057 /* group 2 AC histograms */
2058 init_vlc(&s->ac_vlc_2[i], 5, 32,
2059 &ac_bias_1[i][0][1], 4, 2,
2060 &ac_bias_1[i][0][0], 4, 2, 0);
2062 /* group 3 AC histograms */
2063 init_vlc(&s->ac_vlc_3[i], 5, 32,
2064 &ac_bias_2[i][0][1], 4, 2,
2065 &ac_bias_2[i][0][0], 4, 2, 0);
2067 /* group 4 AC histograms */
2068 init_vlc(&s->ac_vlc_4[i], 5, 32,
2069 &ac_bias_3[i][0][1], 4, 2,
2070 &ac_bias_3[i][0][0], 4, 2, 0);
2073 for (i = 0; i < 16; i++) {
2076 init_vlc(&s->dc_vlc[i], 5, 32,
2077 &s->huffman_table[i][0][1], 4, 2,
2078 &s->huffman_table[i][0][0], 4, 2, 0);
2080 /* group 1 AC histograms */
2081 init_vlc(&s->ac_vlc_1[i], 5, 32,
2082 &s->huffman_table[i+16][0][1], 4, 2,
2083 &s->huffman_table[i+16][0][0], 4, 2, 0);
2085 /* group 2 AC histograms */
2086 init_vlc(&s->ac_vlc_2[i], 5, 32,
2087 &s->huffman_table[i+16*2][0][1], 4, 2,
2088 &s->huffman_table[i+16*2][0][0], 4, 2, 0);
2090 /* group 3 AC histograms */
2091 init_vlc(&s->ac_vlc_3[i], 5, 32,
2092 &s->huffman_table[i+16*3][0][1], 4, 2,
2093 &s->huffman_table[i+16*3][0][0], 4, 2, 0);
2095 /* group 4 AC histograms */
2096 init_vlc(&s->ac_vlc_4[i], 5, 32,
2097 &s->huffman_table[i+16*4][0][1], 4, 2,
2098 &s->huffman_table[i+16*4][0][0], 4, 2, 0);
2102 init_vlc(&s->superblock_run_length_vlc, 6, 34,
2103 &superblock_run_length_vlc_table[0][1], 4, 2,
2104 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
2106 init_vlc(&s->fragment_run_length_vlc, 5, 30,
2107 &fragment_run_length_vlc_table[0][1], 4, 2,
2108 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
2110 init_vlc(&s->mode_code_vlc, 3, 8,
2111 &mode_code_vlc_table[0][1], 2, 1,
2112 &mode_code_vlc_table[0][0], 2, 1, 0);
2114 init_vlc(&s->motion_vector_vlc, 6, 63,
2115 &motion_vector_vlc_table[0][1], 2, 1,
2116 &motion_vector_vlc_table[0][0], 2, 1, 0);
2118 /* work out the block mapping tables */
2119 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
2120 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
2121 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
2122 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
2123 init_block_mapping(s);
2125 for (i = 0; i < 3; i++) {
2126 s->current_frame.data[i] = NULL;
2127 s->last_frame.data[i] = NULL;
2128 s->golden_frame.data[i] = NULL;
2135 * This is the ffmpeg/libavcodec API frame decode function.
2137 static int vp3_decode_frame(AVCodecContext *avctx,
2138 void *data, int *data_size,
2139 const uint8_t *buf, int buf_size)
2141 Vp3DecodeContext *s = avctx->priv_data;
2143 static int counter = 0;
2146 init_get_bits(&gb, buf, buf_size * 8);
2148 if (s->theora && get_bits1(&gb))
2150 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
2154 s->keyframe = !get_bits1(&gb);
2157 s->last_quality_index = s->quality_index;
2161 s->qis[s->nqis++]= get_bits(&gb, 6);
2162 } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb));
2164 s->quality_index= s->qis[0];
2166 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2167 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2168 s->keyframe?"key":"", counter, s->quality_index);
2171 if (s->quality_index != s->last_quality_index) {
2172 init_dequantizer(s);
2173 init_loop_filter(s);
2176 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2182 skip_bits(&gb, 4); /* width code */
2183 skip_bits(&gb, 4); /* height code */
2186 s->version = get_bits(&gb, 5);
2188 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
2191 if (s->version || s->theora)
2194 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
2195 skip_bits(&gb, 2); /* reserved? */
2198 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
2199 if (s->golden_frame.data[0])
2200 avctx->release_buffer(avctx, &s->golden_frame);
2201 s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
2203 if (s->golden_frame.data[0])
2204 avctx->release_buffer(avctx, &s->golden_frame);
2205 if (s->last_frame.data[0])
2206 avctx->release_buffer(avctx, &s->last_frame);
2209 s->golden_frame.reference = 3;
2210 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
2211 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2215 /* golden frame is also the current frame */
2216 s->current_frame= s->golden_frame;
2218 /* time to figure out pixel addresses? */
2219 if (!s->pixel_addresses_initialized)
2221 if (!s->flipped_image)
2222 vp3_calculate_pixel_addresses(s);
2224 theora_calculate_pixel_addresses(s);
2225 s->pixel_addresses_initialized = 1;
2228 /* allocate a new current frame */
2229 s->current_frame.reference = 3;
2230 if (!s->pixel_addresses_initialized) {
2231 av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
2234 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
2235 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2240 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
2241 s->current_frame.qstride= 0;
2245 if (unpack_superblocks(s, &gb)){
2246 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2249 if (unpack_modes(s, &gb)){
2250 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2253 if (unpack_vectors(s, &gb)){
2254 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2257 if (unpack_dct_coeffs(s, &gb)){
2258 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2262 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
2263 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
2264 reverse_dc_prediction(s, s->fragment_start[1],
2265 s->fragment_width / 2, s->fragment_height / 2);
2266 reverse_dc_prediction(s, s->fragment_start[2],
2267 s->fragment_width / 2, s->fragment_height / 2);
2270 for (i = 0; i < s->macroblock_height; i++)
2273 apply_loop_filter(s);
2275 *data_size=sizeof(AVFrame);
2276 *(AVFrame*)data= s->current_frame;
2278 /* release the last frame, if it is allocated and if it is not the
2280 if ((s->last_frame.data[0]) &&
2281 (s->last_frame.data[0] != s->golden_frame.data[0]))
2282 avctx->release_buffer(avctx, &s->last_frame);
2284 /* shuffle frames (last = current) */
2285 s->last_frame= s->current_frame;
2286 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
2292 * This is the ffmpeg/libavcodec API module cleanup function.
2294 static av_cold int vp3_decode_end(AVCodecContext *avctx)
2296 Vp3DecodeContext *s = avctx->priv_data;
2299 av_free(s->superblock_coding);
2300 av_free(s->all_fragments);
2301 av_free(s->coeff_counts);
2303 av_free(s->coded_fragment_list);
2304 av_free(s->superblock_fragments);
2305 av_free(s->superblock_macroblocks);
2306 av_free(s->macroblock_fragments);
2307 av_free(s->macroblock_coding);
2309 for (i = 0; i < 16; i++) {
2310 free_vlc(&s->dc_vlc[i]);
2311 free_vlc(&s->ac_vlc_1[i]);
2312 free_vlc(&s->ac_vlc_2[i]);
2313 free_vlc(&s->ac_vlc_3[i]);
2314 free_vlc(&s->ac_vlc_4[i]);
2317 free_vlc(&s->superblock_run_length_vlc);
2318 free_vlc(&s->fragment_run_length_vlc);
2319 free_vlc(&s->mode_code_vlc);
2320 free_vlc(&s->motion_vector_vlc);
2322 /* release all frames */
2323 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
2324 avctx->release_buffer(avctx, &s->golden_frame);
2325 if (s->last_frame.data[0])
2326 avctx->release_buffer(avctx, &s->last_frame);
2327 /* no need to release the current_frame since it will always be pointing
2328 * to the same frame as either the golden or last frame */
2333 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
2335 Vp3DecodeContext *s = avctx->priv_data;
2337 if (get_bits1(gb)) {
2339 if (s->entries >= 32) { /* overflow */
2340 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2343 token = get_bits(gb, 5);
2344 //av_log(avctx, AV_LOG_DEBUG, "hti %d hbits %x token %d entry : %d size %d\n", s->hti, s->hbits, token, s->entries, s->huff_code_size);
2345 s->huffman_table[s->hti][token][0] = s->hbits;
2346 s->huffman_table[s->hti][token][1] = s->huff_code_size;
2350 if (s->huff_code_size >= 32) {/* overflow */
2351 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2354 s->huff_code_size++;
2356 read_huffman_tree(avctx, gb);
2358 read_huffman_tree(avctx, gb);
2360 s->huff_code_size--;
2365 #ifdef CONFIG_THEORA_DECODER
2366 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2368 Vp3DecodeContext *s = avctx->priv_data;
2369 int visible_width, visible_height;
2371 s->theora = get_bits_long(gb, 24);
2372 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2374 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
2375 /* but previous versions have the image flipped relative to vp3 */
2376 if (s->theora < 0x030200)
2378 s->flipped_image = 1;
2379 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
2382 s->width = get_bits(gb, 16) << 4;
2383 s->height = get_bits(gb, 16) << 4;
2385 if(avcodec_check_dimensions(avctx, s->width, s->height)){
2386 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
2387 s->width= s->height= 0;
2391 if (s->theora >= 0x030400)
2393 skip_bits(gb, 32); /* total number of superblocks in a frame */
2394 // fixme, the next field is 36bits long
2395 skip_bits(gb, 32); /* total number of blocks in a frame */
2396 skip_bits(gb, 4); /* total number of blocks in a frame */
2397 skip_bits(gb, 32); /* total number of macroblocks in a frame */
2400 visible_width = get_bits_long(gb, 24);
2401 visible_height = get_bits_long(gb, 24);
2403 if (s->theora >= 0x030200) {
2404 skip_bits(gb, 8); /* offset x */
2405 skip_bits(gb, 8); /* offset y */
2408 skip_bits(gb, 32); /* fps numerator */
2409 skip_bits(gb, 32); /* fps denumerator */
2410 skip_bits(gb, 24); /* aspect numerator */
2411 skip_bits(gb, 24); /* aspect denumerator */
2413 if (s->theora < 0x030200)
2414 skip_bits(gb, 5); /* keyframe frequency force */
2415 skip_bits(gb, 8); /* colorspace */
2416 if (s->theora >= 0x030400)
2417 skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
2418 skip_bits(gb, 24); /* bitrate */
2420 skip_bits(gb, 6); /* quality hint */
2422 if (s->theora >= 0x030200)
2424 skip_bits(gb, 5); /* keyframe frequency force */
2426 if (s->theora < 0x030400)
2427 skip_bits(gb, 5); /* spare bits */
2430 // align_get_bits(gb);
2432 if ( visible_width <= s->width && visible_width > s->width-16
2433 && visible_height <= s->height && visible_height > s->height-16)
2434 avcodec_set_dimensions(avctx, visible_width, visible_height);
2436 avcodec_set_dimensions(avctx, s->width, s->height);
2441 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2443 Vp3DecodeContext *s = avctx->priv_data;
2444 int i, n, matrices, inter, plane;
2446 if (s->theora >= 0x030200) {
2447 n = get_bits(gb, 3);
2448 /* loop filter limit values table */
2449 for (i = 0; i < 64; i++)
2450 s->filter_limit_values[i] = get_bits(gb, n);
2453 if (s->theora >= 0x030200)
2454 n = get_bits(gb, 4) + 1;
2457 /* quality threshold table */
2458 for (i = 0; i < 64; i++)
2459 s->coded_ac_scale_factor[i] = get_bits(gb, n);
2461 if (s->theora >= 0x030200)
2462 n = get_bits(gb, 4) + 1;
2465 /* dc scale factor table */
2466 for (i = 0; i < 64; i++)
2467 s->coded_dc_scale_factor[i] = get_bits(gb, n);
2469 if (s->theora >= 0x030200)
2470 matrices = get_bits(gb, 9) + 1;
2475 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
2479 for(n=0; n<matrices; n++){
2480 for (i = 0; i < 64; i++)
2481 s->base_matrix[n][i]= get_bits(gb, 8);
2484 for (inter = 0; inter <= 1; inter++) {
2485 for (plane = 0; plane <= 2; plane++) {
2487 if (inter || plane > 0)
2488 newqr = get_bits1(gb);
2491 if(inter && get_bits1(gb)){
2495 qtj= (3*inter + plane - 1) / 3;
2496 plj= (plane + 2) % 3;
2498 s->qr_count[inter][plane]= s->qr_count[qtj][plj];
2499 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
2500 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
2506 i= get_bits(gb, av_log2(matrices-1)+1);
2508 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
2511 s->qr_base[inter][plane][qri]= i;
2514 i = get_bits(gb, av_log2(63-qi)+1) + 1;
2515 s->qr_size[inter][plane][qri++]= i;
2520 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
2523 s->qr_count[inter][plane]= qri;
2528 /* Huffman tables */
2529 for (s->hti = 0; s->hti < 80; s->hti++) {
2531 s->huff_code_size = 1;
2532 if (!get_bits1(gb)) {
2534 read_huffman_tree(avctx, gb);
2536 read_huffman_tree(avctx, gb);
2540 s->theora_tables = 1;
2545 static int theora_decode_init(AVCodecContext *avctx)
2547 Vp3DecodeContext *s = avctx->priv_data;
2550 uint8_t *header_start[3];
2556 if (!avctx->extradata_size)
2558 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
2562 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2563 42, header_start, header_len) < 0) {
2564 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2569 init_get_bits(&gb, header_start[i], header_len[i]);
2571 ptype = get_bits(&gb, 8);
2572 debug_vp3("Theora headerpacket type: %x\n", ptype);
2574 if (!(ptype & 0x80))
2576 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
2580 // FIXME: Check for this as well.
2581 skip_bits(&gb, 6*8); /* "theora" */
2586 theora_decode_header(avctx, &gb);
2589 // FIXME: is this needed? it breaks sometimes
2590 // theora_decode_comments(avctx, gb);
2593 theora_decode_tables(avctx, &gb);
2596 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
2599 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
2600 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
2601 if (s->theora < 0x030200)
2605 vp3_decode_init(avctx);
2609 AVCodec theora_decoder = {
2613 sizeof(Vp3DecodeContext),
2620 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
2624 AVCodec vp3_decoder = {
2628 sizeof(Vp3DecodeContext),
2635 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),