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
36 #include "libavutil/imgutils.h"
47 #define FRAGMENT_PIXELS 8
49 //FIXME split things out into their own arrays
50 typedef struct Vp3Fragment {
52 uint8_t coding_method;
56 #define SB_NOT_CODED 0
57 #define SB_PARTIALLY_CODED 1
58 #define SB_FULLY_CODED 2
60 // This is the maximum length of a single long bit run that can be encoded
61 // for superblock coding or block qps. Theora special-cases this to read a
62 // bit instead of flipping the current bit to allow for runs longer than 4129.
63 #define MAXIMUM_LONG_BIT_RUN 4129
65 #define MODE_INTER_NO_MV 0
67 #define MODE_INTER_PLUS_MV 2
68 #define MODE_INTER_LAST_MV 3
69 #define MODE_INTER_PRIOR_LAST 4
70 #define MODE_USING_GOLDEN 5
71 #define MODE_GOLDEN_MV 6
72 #define MODE_INTER_FOURMV 7
73 #define CODING_MODE_COUNT 8
75 /* special internal mode */
78 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
79 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
82 /* There are 6 preset schemes, plus a free-form scheme */
83 static const int ModeAlphabet[6][CODING_MODE_COUNT] =
85 /* scheme 1: Last motion vector dominates */
86 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
87 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
88 MODE_INTRA, MODE_USING_GOLDEN,
89 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
92 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
93 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
94 MODE_INTRA, MODE_USING_GOLDEN,
95 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
98 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
99 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
100 MODE_INTRA, MODE_USING_GOLDEN,
101 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
104 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
105 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
106 MODE_INTRA, MODE_USING_GOLDEN,
107 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
109 /* scheme 5: No motion vector dominates */
110 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
111 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
112 MODE_INTRA, MODE_USING_GOLDEN,
113 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
116 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
117 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
118 MODE_INTER_PLUS_MV, MODE_INTRA,
119 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
123 static const uint8_t hilbert_offset[16][2] = {
124 {0,0}, {1,0}, {1,1}, {0,1},
125 {0,2}, {0,3}, {1,3}, {1,2},
126 {2,2}, {2,3}, {3,3}, {3,2},
127 {3,1}, {2,1}, {2,0}, {3,0}
130 #define MIN_DEQUANT_VAL 2
132 typedef struct Vp3DecodeContext {
133 AVCodecContext *avctx;
134 int theora, theora_tables;
137 int chroma_x_shift, chroma_y_shift;
138 AVFrame golden_frame;
140 AVFrame current_frame;
143 VideoDSPContext vdsp;
144 VP3DSPContext vp3dsp;
145 DECLARE_ALIGNED(16, int16_t, block)[64];
148 int skip_loop_filter;
154 int superblock_count;
155 int y_superblock_width;
156 int y_superblock_height;
157 int y_superblock_count;
158 int c_superblock_width;
159 int c_superblock_height;
160 int c_superblock_count;
161 int u_superblock_start;
162 int v_superblock_start;
163 unsigned char *superblock_coding;
165 int macroblock_count;
166 int macroblock_width;
167 int macroblock_height;
170 int fragment_width[2];
171 int fragment_height[2];
173 Vp3Fragment *all_fragments;
174 int fragment_start[3];
177 int8_t (*motion_val[2])[2];
182 uint16_t coded_dc_scale_factor[64];
183 uint32_t coded_ac_scale_factor[64];
184 uint8_t base_matrix[384][64];
185 uint8_t qr_count[2][3];
186 uint8_t qr_size [2][3][64];
187 uint16_t qr_base[2][3][64];
190 * This is a list of all tokens in bitstream order. Reordering takes place
191 * by pulling from each level during IDCT. As a consequence, IDCT must be
192 * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
193 * otherwise. The 32 different tokens with up to 12 bits of extradata are
194 * collapsed into 3 types, packed as follows:
195 * (from the low to high bits)
197 * 2 bits: type (0,1,2)
198 * 0: EOB run, 14 bits for run length (12 needed)
199 * 1: zero run, 7 bits for run length
200 * 7 bits for the next coefficient (3 needed)
201 * 2: coefficient, 14 bits (11 needed)
203 * Coefficients are signed, so are packed in the highest bits for automatic
206 int16_t *dct_tokens[3][64];
207 int16_t *dct_tokens_base;
208 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
209 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
210 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
213 * number of blocks that contain DCT coefficients at the given level or higher
215 int num_coded_frags[3][64];
216 int total_num_coded_frags;
218 /* this is a list of indexes into the all_fragments array indicating
219 * which of the fragments are coded */
220 int *coded_fragment_list[3];
228 VLC superblock_run_length_vlc;
229 VLC fragment_run_length_vlc;
231 VLC motion_vector_vlc;
233 /* these arrays need to be on 16-byte boundaries since SSE2 operations
235 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
237 /* This table contains superblock_count * 16 entries. Each set of 16
238 * numbers corresponds to the fragment indexes 0..15 of the superblock.
239 * An entry will be -1 to indicate that no entry corresponds to that
241 int *superblock_fragments;
243 /* This is an array that indicates how a particular macroblock
245 unsigned char *macroblock_coding;
247 uint8_t *edge_emu_buffer;
254 uint32_t huffman_table[80][32][2];
256 uint8_t filter_limit_values[64];
257 DECLARE_ALIGNED(8, int, bounding_values_array)[256+2];
260 /************************************************************************
261 * VP3 specific functions
262 ************************************************************************/
264 static void vp3_decode_flush(AVCodecContext *avctx)
266 Vp3DecodeContext *s = avctx->priv_data;
268 if (s->golden_frame.data[0]) {
269 if (s->golden_frame.data[0] == s->last_frame.data[0])
270 memset(&s->last_frame, 0, sizeof(AVFrame));
271 if (s->current_frame.data[0] == s->golden_frame.data[0])
272 memset(&s->current_frame, 0, sizeof(AVFrame));
273 ff_thread_release_buffer(avctx, &s->golden_frame);
275 if (s->last_frame.data[0]) {
276 if (s->current_frame.data[0] == s->last_frame.data[0])
277 memset(&s->current_frame, 0, sizeof(AVFrame));
278 ff_thread_release_buffer(avctx, &s->last_frame);
280 if (s->current_frame.data[0])
281 ff_thread_release_buffer(avctx, &s->current_frame);
284 static av_cold int vp3_decode_end(AVCodecContext *avctx)
286 Vp3DecodeContext *s = avctx->priv_data;
289 av_freep(&s->superblock_coding);
290 av_freep(&s->all_fragments);
291 av_freep(&s->coded_fragment_list[0]);
292 av_freep(&s->dct_tokens_base);
293 av_freep(&s->superblock_fragments);
294 av_freep(&s->macroblock_coding);
295 av_freep(&s->motion_val[0]);
296 av_freep(&s->motion_val[1]);
297 av_freep(&s->edge_emu_buffer);
299 s->theora_tables = 0;
301 if (avctx->internal->is_copy)
304 for (i = 0; i < 16; i++) {
305 ff_free_vlc(&s->dc_vlc[i]);
306 ff_free_vlc(&s->ac_vlc_1[i]);
307 ff_free_vlc(&s->ac_vlc_2[i]);
308 ff_free_vlc(&s->ac_vlc_3[i]);
309 ff_free_vlc(&s->ac_vlc_4[i]);
312 ff_free_vlc(&s->superblock_run_length_vlc);
313 ff_free_vlc(&s->fragment_run_length_vlc);
314 ff_free_vlc(&s->mode_code_vlc);
315 ff_free_vlc(&s->motion_vector_vlc);
317 /* release all frames */
318 vp3_decode_flush(avctx);
324 * This function sets up all of the various blocks mappings:
325 * superblocks <-> fragments, macroblocks <-> fragments,
326 * superblocks <-> macroblocks
328 * @return 0 is successful; returns 1 if *anything* went wrong.
330 static int init_block_mapping(Vp3DecodeContext *s)
332 int sb_x, sb_y, plane;
335 for (plane = 0; plane < 3; plane++) {
336 int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
337 int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
338 int frag_width = s->fragment_width[!!plane];
339 int frag_height = s->fragment_height[!!plane];
341 for (sb_y = 0; sb_y < sb_height; sb_y++)
342 for (sb_x = 0; sb_x < sb_width; sb_x++)
343 for (i = 0; i < 16; i++) {
344 x = 4*sb_x + hilbert_offset[i][0];
345 y = 4*sb_y + hilbert_offset[i][1];
347 if (x < frag_width && y < frag_height)
348 s->superblock_fragments[j++] = s->fragment_start[plane] + y*frag_width + x;
350 s->superblock_fragments[j++] = -1;
354 return 0; /* successful path out */
358 * This function sets up the dequantization tables used for a particular
361 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
363 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
364 int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
365 int i, plane, inter, qri, bmi, bmj, qistart;
367 for(inter=0; inter<2; inter++){
368 for(plane=0; plane<3; plane++){
370 for(qri=0; qri<s->qr_count[inter][plane]; qri++){
371 sum+= s->qr_size[inter][plane][qri];
372 if(s->qps[qpi] <= sum)
375 qistart= sum - s->qr_size[inter][plane][qri];
376 bmi= s->qr_base[inter][plane][qri ];
377 bmj= s->qr_base[inter][plane][qri+1];
379 int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i]
380 - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i]
381 + s->qr_size[inter][plane][qri])
382 / (2*s->qr_size[inter][plane][qri]);
384 int qmin= 8<<(inter + !i);
385 int qscale= i ? ac_scale_factor : dc_scale_factor;
387 s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
389 // all DC coefficients use the same quant so as not to interfere with DC prediction
390 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
396 * This function initializes the loop filter boundary limits if the frame's
397 * quality index is different from the previous frame's.
399 * The filter_limit_values may not be larger than 127.
401 static void init_loop_filter(Vp3DecodeContext *s)
403 int *bounding_values= s->bounding_values_array+127;
408 filter_limit = s->filter_limit_values[s->qps[0]];
409 av_assert0(filter_limit < 128U);
411 /* set up the bounding values */
412 memset(s->bounding_values_array, 0, 256 * sizeof(int));
413 for (x = 0; x < filter_limit; x++) {
414 bounding_values[-x] = -x;
415 bounding_values[x] = x;
417 for (x = value = filter_limit; x < 128 && value; x++, value--) {
418 bounding_values[ x] = value;
419 bounding_values[-x] = -value;
422 bounding_values[128] = value;
423 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
427 * This function unpacks all of the superblock/macroblock/fragment coding
428 * information from the bitstream.
430 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
432 int superblock_starts[3] = { 0, s->u_superblock_start, s->v_superblock_start };
434 int current_superblock = 0;
436 int num_partial_superblocks = 0;
439 int current_fragment;
443 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
447 /* unpack the list of partially-coded superblocks */
448 bit = get_bits1(gb) ^ 1;
451 while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
452 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
457 current_run = get_vlc2(gb,
458 s->superblock_run_length_vlc.table, 6, 2) + 1;
459 if (current_run == 34)
460 current_run += get_bits(gb, 12);
462 if (current_superblock + current_run > s->superblock_count) {
463 av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n");
467 memset(s->superblock_coding + current_superblock, bit, current_run);
469 current_superblock += current_run;
471 num_partial_superblocks += current_run;
474 /* unpack the list of fully coded superblocks if any of the blocks were
475 * not marked as partially coded in the previous step */
476 if (num_partial_superblocks < s->superblock_count) {
477 int superblocks_decoded = 0;
479 current_superblock = 0;
480 bit = get_bits1(gb) ^ 1;
483 while (superblocks_decoded < s->superblock_count - num_partial_superblocks
484 && get_bits_left(gb) > 0) {
486 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
491 current_run = get_vlc2(gb,
492 s->superblock_run_length_vlc.table, 6, 2) + 1;
493 if (current_run == 34)
494 current_run += get_bits(gb, 12);
496 for (j = 0; j < current_run; current_superblock++) {
497 if (current_superblock >= s->superblock_count) {
498 av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded superblock run length\n");
502 /* skip any superblocks already marked as partially coded */
503 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
504 s->superblock_coding[current_superblock] = 2*bit;
508 superblocks_decoded += current_run;
512 /* if there were partial blocks, initialize bitstream for
513 * unpacking fragment codings */
514 if (num_partial_superblocks) {
518 /* toggle the bit because as soon as the first run length is
519 * fetched the bit will be toggled again */
524 /* figure out which fragments are coded; iterate through each
525 * superblock (all planes) */
526 s->total_num_coded_frags = 0;
527 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
529 for (plane = 0; plane < 3; plane++) {
530 int sb_start = superblock_starts[plane];
531 int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock_count);
532 int num_coded_frags = 0;
534 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
536 /* iterate through all 16 fragments in a superblock */
537 for (j = 0; j < 16; j++) {
539 /* if the fragment is in bounds, check its coding status */
540 current_fragment = s->superblock_fragments[i * 16 + j];
541 if (current_fragment != -1) {
542 int coded = s->superblock_coding[i];
544 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
546 /* fragment may or may not be coded; this is the case
547 * that cares about the fragment coding runs */
548 if (current_run-- == 0) {
550 current_run = get_vlc2(gb,
551 s->fragment_run_length_vlc.table, 5, 2);
557 /* default mode; actual mode will be decoded in
559 s->all_fragments[current_fragment].coding_method =
561 s->coded_fragment_list[plane][num_coded_frags++] =
564 /* not coded; copy this fragment from the prior frame */
565 s->all_fragments[current_fragment].coding_method =
571 s->total_num_coded_frags += num_coded_frags;
572 for (i = 0; i < 64; i++)
573 s->num_coded_frags[plane][i] = num_coded_frags;
575 s->coded_fragment_list[plane+1] = s->coded_fragment_list[plane] + num_coded_frags;
581 * This function unpacks all the coding mode data for individual macroblocks
582 * from the bitstream.
584 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
586 int i, j, k, sb_x, sb_y;
588 int current_macroblock;
589 int current_fragment;
591 int custom_mode_alphabet[CODING_MODE_COUNT];
596 for (i = 0; i < s->fragment_count; i++)
597 s->all_fragments[i].coding_method = MODE_INTRA;
601 /* fetch the mode coding scheme for this frame */
602 scheme = get_bits(gb, 3);
604 /* is it a custom coding scheme? */
606 for (i = 0; i < 8; i++)
607 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
608 for (i = 0; i < 8; i++)
609 custom_mode_alphabet[get_bits(gb, 3)] = i;
610 alphabet = custom_mode_alphabet;
612 alphabet = ModeAlphabet[scheme-1];
614 /* iterate through all of the macroblocks that contain 1 or more
616 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
617 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
618 if (get_bits_left(gb) <= 0)
621 for (j = 0; j < 4; j++) {
622 int mb_x = 2*sb_x + (j>>1);
623 int mb_y = 2*sb_y + (((j>>1)+j)&1);
624 current_macroblock = mb_y * s->macroblock_width + mb_x;
626 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height)
629 #define BLOCK_X (2*mb_x + (k&1))
630 #define BLOCK_Y (2*mb_y + (k>>1))
631 /* coding modes are only stored if the macroblock has at least one
632 * luma block coded, otherwise it must be INTER_NO_MV */
633 for (k = 0; k < 4; k++) {
634 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
635 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
639 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
643 /* mode 7 means get 3 bits for each coding mode */
645 coding_mode = get_bits(gb, 3);
647 coding_mode = alphabet
648 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
650 s->macroblock_coding[current_macroblock] = coding_mode;
651 for (k = 0; k < 4; k++) {
652 frag = s->all_fragments + BLOCK_Y*s->fragment_width[0] + BLOCK_X;
653 if (frag->coding_method != MODE_COPY)
654 frag->coding_method = coding_mode;
657 #define SET_CHROMA_MODES \
658 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
659 frag[s->fragment_start[1]].coding_method = coding_mode;\
660 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
661 frag[s->fragment_start[2]].coding_method = coding_mode;
663 if (s->chroma_y_shift) {
664 frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x;
666 } else if (s->chroma_x_shift) {
667 frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x;
668 for (k = 0; k < 2; k++) {
670 frag += s->fragment_width[1];
673 for (k = 0; k < 4; k++) {
674 frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X;
687 * This function unpacks all the motion vectors for the individual
688 * macroblocks from the bitstream.
690 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
692 int j, k, sb_x, sb_y;
696 int last_motion_x = 0;
697 int last_motion_y = 0;
698 int prior_last_motion_x = 0;
699 int prior_last_motion_y = 0;
700 int current_macroblock;
701 int current_fragment;
707 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
708 coding_mode = get_bits1(gb);
710 /* iterate through all of the macroblocks that contain 1 or more
712 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
713 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
714 if (get_bits_left(gb) <= 0)
717 for (j = 0; j < 4; j++) {
718 int mb_x = 2*sb_x + (j>>1);
719 int mb_y = 2*sb_y + (((j>>1)+j)&1);
720 current_macroblock = mb_y * s->macroblock_width + mb_x;
722 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||
723 (s->macroblock_coding[current_macroblock] == MODE_COPY))
726 switch (s->macroblock_coding[current_macroblock]) {
728 case MODE_INTER_PLUS_MV:
730 /* all 6 fragments use the same motion vector */
731 if (coding_mode == 0) {
732 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
733 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
735 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
736 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
739 /* vector maintenance, only on MODE_INTER_PLUS_MV */
740 if (s->macroblock_coding[current_macroblock] ==
741 MODE_INTER_PLUS_MV) {
742 prior_last_motion_x = last_motion_x;
743 prior_last_motion_y = last_motion_y;
744 last_motion_x = motion_x[0];
745 last_motion_y = motion_y[0];
749 case MODE_INTER_FOURMV:
750 /* vector maintenance */
751 prior_last_motion_x = last_motion_x;
752 prior_last_motion_y = last_motion_y;
754 /* fetch 4 vectors from the bitstream, one for each
755 * Y fragment, then average for the C fragment vectors */
756 for (k = 0; k < 4; k++) {
757 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
758 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
759 if (coding_mode == 0) {
760 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
761 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
763 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
764 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
766 last_motion_x = motion_x[k];
767 last_motion_y = motion_y[k];
775 case MODE_INTER_LAST_MV:
776 /* all 6 fragments use the last motion vector */
777 motion_x[0] = last_motion_x;
778 motion_y[0] = last_motion_y;
780 /* no vector maintenance (last vector remains the
784 case MODE_INTER_PRIOR_LAST:
785 /* all 6 fragments use the motion vector prior to the
786 * last motion vector */
787 motion_x[0] = prior_last_motion_x;
788 motion_y[0] = prior_last_motion_y;
790 /* vector maintenance */
791 prior_last_motion_x = last_motion_x;
792 prior_last_motion_y = last_motion_y;
793 last_motion_x = motion_x[0];
794 last_motion_y = motion_y[0];
798 /* covers intra, inter without MV, golden without MV */
802 /* no vector maintenance */
806 /* assign the motion vectors to the correct fragments */
807 for (k = 0; k < 4; k++) {
809 BLOCK_Y*s->fragment_width[0] + BLOCK_X;
810 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
811 s->motion_val[0][current_fragment][0] = motion_x[k];
812 s->motion_val[0][current_fragment][1] = motion_y[k];
814 s->motion_val[0][current_fragment][0] = motion_x[0];
815 s->motion_val[0][current_fragment][1] = motion_y[0];
819 if (s->chroma_y_shift) {
820 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
821 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
822 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
824 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
825 motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);
826 frag = mb_y*s->fragment_width[1] + mb_x;
827 s->motion_val[1][frag][0] = motion_x[0];
828 s->motion_val[1][frag][1] = motion_y[0];
829 } else if (s->chroma_x_shift) {
830 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
831 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
832 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
833 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
834 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
836 motion_x[1] = motion_x[0];
837 motion_y[1] = motion_y[0];
839 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
840 motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);
842 frag = 2*mb_y*s->fragment_width[1] + mb_x;
843 for (k = 0; k < 2; k++) {
844 s->motion_val[1][frag][0] = motion_x[k];
845 s->motion_val[1][frag][1] = motion_y[k];
846 frag += s->fragment_width[1];
849 for (k = 0; k < 4; k++) {
850 frag = BLOCK_Y*s->fragment_width[1] + BLOCK_X;
851 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
852 s->motion_val[1][frag][0] = motion_x[k];
853 s->motion_val[1][frag][1] = motion_y[k];
855 s->motion_val[1][frag][0] = motion_x[0];
856 s->motion_val[1][frag][1] = motion_y[0];
867 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
869 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
870 int num_blocks = s->total_num_coded_frags;
872 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) {
873 i = blocks_decoded = num_blocks_at_qpi = 0;
875 bit = get_bits1(gb) ^ 1;
879 if (run_length == MAXIMUM_LONG_BIT_RUN)
884 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
885 if (run_length == 34)
886 run_length += get_bits(gb, 12);
887 blocks_decoded += run_length;
890 num_blocks_at_qpi += run_length;
892 for (j = 0; j < run_length; i++) {
893 if (i >= s->total_num_coded_frags)
896 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
897 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
901 } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
903 num_blocks -= num_blocks_at_qpi;
910 * This function is called by unpack_dct_coeffs() to extract the VLCs from
911 * the bitstream. The VLCs encode tokens which are used to unpack DCT
912 * data. This function unpacks all the VLCs for either the Y plane or both
913 * C planes, and is called for DC coefficients or different AC coefficient
914 * levels (since different coefficient types require different VLC tables.
916 * This function returns a residual eob run. E.g, if a particular token gave
917 * instructions to EOB the next 5 fragments and there were only 2 fragments
918 * left in the current fragment range, 3 would be returned so that it could
919 * be passed into the next call to this same function.
921 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
922 VLC *table, int coeff_index,
933 int num_coeffs = s->num_coded_frags[plane][coeff_index];
934 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
936 /* local references to structure members to avoid repeated deferences */
937 int *coded_fragment_list = s->coded_fragment_list[plane];
938 Vp3Fragment *all_fragments = s->all_fragments;
939 VLC_TYPE (*vlc_table)[2] = table->table;
942 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\n", coeff_index);
944 if (eob_run > num_coeffs) {
945 coeff_i = blocks_ended = num_coeffs;
946 eob_run -= num_coeffs;
948 coeff_i = blocks_ended = eob_run;
952 // insert fake EOB token to cover the split between planes or zzi
954 dct_tokens[j++] = blocks_ended << 2;
956 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
957 /* decode a VLC into a token */
958 token = get_vlc2(gb, vlc_table, 11, 3);
959 /* use the token to get a zero run, a coefficient, and an eob run */
960 if ((unsigned) token <= 6U) {
961 eob_run = eob_run_base[token];
962 if (eob_run_get_bits[token])
963 eob_run += get_bits(gb, eob_run_get_bits[token]);
965 // record only the number of blocks ended in this plane,
966 // any spill will be recorded in the next plane.
967 if (eob_run > num_coeffs - coeff_i) {
968 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
969 blocks_ended += num_coeffs - coeff_i;
970 eob_run -= num_coeffs - coeff_i;
971 coeff_i = num_coeffs;
973 dct_tokens[j++] = TOKEN_EOB(eob_run);
974 blocks_ended += eob_run;
978 } else if (token >= 0) {
979 bits_to_get = coeff_get_bits[token];
981 bits_to_get = get_bits(gb, bits_to_get);
982 coeff = coeff_tables[token][bits_to_get];
984 zero_run = zero_run_base[token];
985 if (zero_run_get_bits[token])
986 zero_run += get_bits(gb, zero_run_get_bits[token]);
989 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
991 // Save DC into the fragment structure. DC prediction is
992 // done in raster order, so the actual DC can't be in with
993 // other tokens. We still need the token in dct_tokens[]
994 // however, or else the structure collapses on itself.
996 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
998 dct_tokens[j++] = TOKEN_COEFF(coeff);
1001 if (coeff_index + zero_run > 64) {
1002 av_log(s->avctx, AV_LOG_DEBUG, "Invalid zero run of %d with"
1003 " %d coeffs left\n", zero_run, 64-coeff_index);
1004 zero_run = 64 - coeff_index;
1007 // zero runs code multiple coefficients,
1008 // so don't try to decode coeffs for those higher levels
1009 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
1010 s->num_coded_frags[plane][i]--;
1013 av_log(s->avctx, AV_LOG_ERROR,
1014 "Invalid token %d\n", token);
1019 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1020 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1022 // decrement the number of blocks that have higher coeffecients for each
1023 // EOB run at this level
1025 for (i = coeff_index+1; i < 64; i++)
1026 s->num_coded_frags[plane][i] -= blocks_ended;
1028 // setup the next buffer
1030 s->dct_tokens[plane+1][coeff_index] = dct_tokens + j;
1031 else if (coeff_index < 63)
1032 s->dct_tokens[0][coeff_index+1] = dct_tokens + j;
1037 static void reverse_dc_prediction(Vp3DecodeContext *s,
1040 int fragment_height);
1042 * This function unpacks all of the DCT coefficient data from the
1045 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1052 int residual_eob_run = 0;
1056 s->dct_tokens[0][0] = s->dct_tokens_base;
1058 /* fetch the DC table indexes */
1059 dc_y_table = get_bits(gb, 4);
1060 dc_c_table = get_bits(gb, 4);
1062 /* unpack the Y plane DC coefficients */
1063 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1064 0, residual_eob_run);
1065 if (residual_eob_run < 0)
1066 return residual_eob_run;
1068 /* reverse prediction of the Y-plane DC coefficients */
1069 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1071 /* unpack the C plane DC coefficients */
1072 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1073 1, residual_eob_run);
1074 if (residual_eob_run < 0)
1075 return residual_eob_run;
1076 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1077 2, residual_eob_run);
1078 if (residual_eob_run < 0)
1079 return residual_eob_run;
1081 /* reverse prediction of the C-plane DC coefficients */
1082 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1084 reverse_dc_prediction(s, s->fragment_start[1],
1085 s->fragment_width[1], s->fragment_height[1]);
1086 reverse_dc_prediction(s, s->fragment_start[2],
1087 s->fragment_width[1], s->fragment_height[1]);
1090 /* fetch the AC table indexes */
1091 ac_y_table = get_bits(gb, 4);
1092 ac_c_table = get_bits(gb, 4);
1094 /* build tables of AC VLC tables */
1095 for (i = 1; i <= 5; i++) {
1096 y_tables[i] = &s->ac_vlc_1[ac_y_table];
1097 c_tables[i] = &s->ac_vlc_1[ac_c_table];
1099 for (i = 6; i <= 14; i++) {
1100 y_tables[i] = &s->ac_vlc_2[ac_y_table];
1101 c_tables[i] = &s->ac_vlc_2[ac_c_table];
1103 for (i = 15; i <= 27; i++) {
1104 y_tables[i] = &s->ac_vlc_3[ac_y_table];
1105 c_tables[i] = &s->ac_vlc_3[ac_c_table];
1107 for (i = 28; i <= 63; i++) {
1108 y_tables[i] = &s->ac_vlc_4[ac_y_table];
1109 c_tables[i] = &s->ac_vlc_4[ac_c_table];
1112 /* decode all AC coefficents */
1113 for (i = 1; i <= 63; i++) {
1114 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1115 0, residual_eob_run);
1116 if (residual_eob_run < 0)
1117 return residual_eob_run;
1119 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1120 1, residual_eob_run);
1121 if (residual_eob_run < 0)
1122 return residual_eob_run;
1123 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1124 2, residual_eob_run);
1125 if (residual_eob_run < 0)
1126 return residual_eob_run;
1133 * This function reverses the DC prediction for each coded fragment in
1134 * the frame. Much of this function is adapted directly from the original
1137 #define COMPATIBLE_FRAME(x) \
1138 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1139 #define DC_COEFF(u) s->all_fragments[u].dc
1141 static void reverse_dc_prediction(Vp3DecodeContext *s,
1144 int fragment_height)
1153 int i = first_fragment;
1157 /* DC values for the left, up-left, up, and up-right fragments */
1158 int vl, vul, vu, vur;
1160 /* indexes for the left, up-left, up, and up-right fragments */
1164 * The 6 fields mean:
1165 * 0: up-left multiplier
1167 * 2: up-right multiplier
1168 * 3: left multiplier
1170 static const int predictor_transform[16][4] = {
1172 { 0, 0, 0,128}, // PL
1173 { 0, 0,128, 0}, // PUR
1174 { 0, 0, 53, 75}, // PUR|PL
1175 { 0,128, 0, 0}, // PU
1176 { 0, 64, 0, 64}, // PU|PL
1177 { 0,128, 0, 0}, // PU|PUR
1178 { 0, 0, 53, 75}, // PU|PUR|PL
1179 {128, 0, 0, 0}, // PUL
1180 { 0, 0, 0,128}, // PUL|PL
1181 { 64, 0, 64, 0}, // PUL|PUR
1182 { 0, 0, 53, 75}, // PUL|PUR|PL
1183 { 0,128, 0, 0}, // PUL|PU
1184 {-104,116, 0,116}, // PUL|PU|PL
1185 { 24, 80, 24, 0}, // PUL|PU|PUR
1186 {-104,116, 0,116} // PUL|PU|PUR|PL
1189 /* This table shows which types of blocks can use other blocks for
1190 * prediction. For example, INTRA is the only mode in this table to
1191 * have a frame number of 0. That means INTRA blocks can only predict
1192 * from other INTRA blocks. There are 2 golden frame coding types;
1193 * blocks encoding in these modes can only predict from other blocks
1194 * that were encoded with these 1 of these 2 modes. */
1195 static const unsigned char compatible_frame[9] = {
1196 1, /* MODE_INTER_NO_MV */
1198 1, /* MODE_INTER_PLUS_MV */
1199 1, /* MODE_INTER_LAST_MV */
1200 1, /* MODE_INTER_PRIOR_MV */
1201 2, /* MODE_USING_GOLDEN */
1202 2, /* MODE_GOLDEN_MV */
1203 1, /* MODE_INTER_FOUR_MV */
1206 int current_frame_type;
1208 /* there is a last DC predictor for each of the 3 frame types */
1213 vul = vu = vur = vl = 0;
1214 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1216 /* for each fragment row... */
1217 for (y = 0; y < fragment_height; y++) {
1219 /* for each fragment in a row... */
1220 for (x = 0; x < fragment_width; x++, i++) {
1222 /* reverse prediction if this block was coded */
1223 if (s->all_fragments[i].coding_method != MODE_COPY) {
1225 current_frame_type =
1226 compatible_frame[s->all_fragments[i].coding_method];
1232 if(COMPATIBLE_FRAME(l))
1236 u= i-fragment_width;
1238 if(COMPATIBLE_FRAME(u))
1241 ul= i-fragment_width-1;
1243 if(COMPATIBLE_FRAME(ul))
1246 if(x + 1 < fragment_width){
1247 ur= i-fragment_width+1;
1249 if(COMPATIBLE_FRAME(ur))
1254 if (transform == 0) {
1256 /* if there were no fragments to predict from, use last
1258 predicted_dc = last_dc[current_frame_type];
1261 /* apply the appropriate predictor transform */
1263 (predictor_transform[transform][0] * vul) +
1264 (predictor_transform[transform][1] * vu) +
1265 (predictor_transform[transform][2] * vur) +
1266 (predictor_transform[transform][3] * vl);
1268 predicted_dc /= 128;
1270 /* check for outranging on the [ul u l] and
1271 * [ul u ur l] predictors */
1272 if ((transform == 15) || (transform == 13)) {
1273 if (FFABS(predicted_dc - vu) > 128)
1275 else if (FFABS(predicted_dc - vl) > 128)
1277 else if (FFABS(predicted_dc - vul) > 128)
1282 /* at long last, apply the predictor */
1283 DC_COEFF(i) += predicted_dc;
1285 last_dc[current_frame_type] = DC_COEFF(i);
1291 static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
1294 int *bounding_values= s->bounding_values_array+127;
1296 int width = s->fragment_width[!!plane];
1297 int height = s->fragment_height[!!plane];
1298 int fragment = s->fragment_start [plane] + ystart * width;
1299 int stride = s->current_frame.linesize[plane];
1300 uint8_t *plane_data = s->current_frame.data [plane];
1301 if (!s->flipped_image) stride = -stride;
1302 plane_data += s->data_offset[plane] + 8*ystart*stride;
1304 for (y = ystart; y < yend; y++) {
1306 for (x = 0; x < width; x++) {
1307 /* This code basically just deblocks on the edges of coded blocks.
1308 * However, it has to be much more complicated because of the
1309 * braindamaged deblock ordering used in VP3/Theora. Order matters
1310 * because some pixels get filtered twice. */
1311 if( s->all_fragments[fragment].coding_method != MODE_COPY )
1313 /* do not perform left edge filter for left columns frags */
1315 s->vp3dsp.h_loop_filter(
1317 stride, bounding_values);
1320 /* do not perform top edge filter for top row fragments */
1322 s->vp3dsp.v_loop_filter(
1324 stride, bounding_values);
1327 /* do not perform right edge filter for right column
1328 * fragments or if right fragment neighbor is also coded
1329 * in this frame (it will be filtered in next iteration) */
1330 if ((x < width - 1) &&
1331 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1332 s->vp3dsp.h_loop_filter(
1333 plane_data + 8*x + 8,
1334 stride, bounding_values);
1337 /* do not perform bottom edge filter for bottom row
1338 * fragments or if bottom fragment neighbor is also coded
1339 * in this frame (it will be filtered in the next row) */
1340 if ((y < height - 1) &&
1341 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1342 s->vp3dsp.v_loop_filter(
1343 plane_data + 8*x + 8*stride,
1344 stride, bounding_values);
1350 plane_data += 8*stride;
1355 * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1356 * for the next block in coding order
1358 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1359 int plane, int inter, int16_t block[64])
1361 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1362 uint8_t *perm = s->scantable.permutated;
1366 int token = *s->dct_tokens[plane][i];
1367 switch (token & 3) {
1369 if (--token < 4) // 0-3 are token types, so the EOB run must now be 0
1370 s->dct_tokens[plane][i]++;
1372 *s->dct_tokens[plane][i] = token & ~3;
1375 s->dct_tokens[plane][i]++;
1376 i += (token >> 2) & 0x7f;
1378 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1381 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1385 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1386 s->dct_tokens[plane][i++]++;
1388 default: // shouldn't happen
1392 // return value is expected to be a valid level
1395 // the actual DC+prediction is in the fragment structure
1396 block[0] = frag->dc * s->qmat[0][inter][plane][0];
1401 * called when all pixels up to row y are complete
1403 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1406 int offset[AV_NUM_DATA_POINTERS];
1408 if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1409 int y_flipped = s->flipped_image ? s->avctx->height-y : y;
1411 // At the end of the frame, report INT_MAX instead of the height of the frame.
1412 // This makes the other threads' ff_thread_await_progress() calls cheaper, because
1413 // they don't have to clip their values.
1414 ff_thread_report_progress(&s->current_frame, y_flipped==s->avctx->height ? INT_MAX : y_flipped-1, 0);
1417 if(s->avctx->draw_horiz_band==NULL)
1420 h= y - s->last_slice_end;
1421 s->last_slice_end= y;
1424 if (!s->flipped_image) {
1425 y = s->avctx->height - y - h;
1428 cy = y >> s->chroma_y_shift;
1429 offset[0] = s->current_frame.linesize[0]*y;
1430 offset[1] = s->current_frame.linesize[1]*cy;
1431 offset[2] = s->current_frame.linesize[2]*cy;
1432 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
1436 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
1440 * Wait for the reference frame of the current fragment.
1441 * The progress value is in luma pixel rows.
1443 static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment, int motion_y, int y)
1447 int border = motion_y&1;
1449 if (fragment->coding_method == MODE_USING_GOLDEN ||
1450 fragment->coding_method == MODE_GOLDEN_MV)
1451 ref_frame = &s->golden_frame;
1453 ref_frame = &s->last_frame;
1455 ref_row = y + (motion_y>>1);
1456 ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1458 ff_thread_await_progress(ref_frame, ref_row, 0);
1462 * Perform the final rendering for a particular slice of data.
1463 * The slice number ranges from 0..(c_superblock_height - 1).
1465 static void render_slice(Vp3DecodeContext *s, int slice)
1467 int x, y, i, j, fragment;
1468 int16_t *block = s->block;
1469 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1470 int motion_halfpel_index;
1471 uint8_t *motion_source;
1472 int plane, first_pixel;
1474 if (slice >= s->c_superblock_height)
1477 for (plane = 0; plane < 3; plane++) {
1478 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
1479 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
1480 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
1481 int stride = s->current_frame.linesize[plane];
1482 int plane_width = s->width >> (plane && s->chroma_x_shift);
1483 int plane_height = s->height >> (plane && s->chroma_y_shift);
1484 int8_t (*motion_val)[2] = s->motion_val[!!plane];
1486 int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
1487 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
1488 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width;
1490 int fragment_width = s->fragment_width[!!plane];
1491 int fragment_height = s->fragment_height[!!plane];
1492 int fragment_start = s->fragment_start[plane];
1493 int do_await = !plane && HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_FRAME);
1495 if (!s->flipped_image) stride = -stride;
1496 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
1499 /* for each superblock row in the slice (both of them)... */
1500 for (; sb_y < slice_height; sb_y++) {
1502 /* for each superblock in a row... */
1503 for (sb_x = 0; sb_x < slice_width; sb_x++) {
1505 /* for each block in a superblock... */
1506 for (j = 0; j < 16; j++) {
1507 x = 4*sb_x + hilbert_offset[j][0];
1508 y = 4*sb_y + hilbert_offset[j][1];
1509 fragment = y*fragment_width + x;
1511 i = fragment_start + fragment;
1514 if (x >= fragment_width || y >= fragment_height)
1517 first_pixel = 8*y*stride + 8*x;
1519 if (do_await && s->all_fragments[i].coding_method != MODE_INTRA)
1520 await_reference_row(s, &s->all_fragments[i], motion_val[fragment][1], (16*y) >> s->chroma_y_shift);
1522 /* transform if this block was coded */
1523 if (s->all_fragments[i].coding_method != MODE_COPY) {
1524 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
1525 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
1526 motion_source= golden_plane;
1528 motion_source= last_plane;
1530 motion_source += first_pixel;
1531 motion_halfpel_index = 0;
1533 /* sort out the motion vector if this fragment is coded
1534 * using a motion vector method */
1535 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1536 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
1538 motion_x = motion_val[fragment][0];
1539 motion_y = motion_val[fragment][1];
1541 src_x= (motion_x>>1) + 8*x;
1542 src_y= (motion_y>>1) + 8*y;
1544 motion_halfpel_index = motion_x & 0x01;
1545 motion_source += (motion_x >> 1);
1547 motion_halfpel_index |= (motion_y & 0x01) << 1;
1548 motion_source += ((motion_y >> 1) * stride);
1550 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
1551 uint8_t *temp= s->edge_emu_buffer;
1552 if(stride<0) temp -= 8*stride;
1554 s->vdsp.emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
1555 motion_source= temp;
1560 /* first, take care of copying a block from either the
1561 * previous or the golden frame */
1562 if (s->all_fragments[i].coding_method != MODE_INTRA) {
1563 /* Note, it is possible to implement all MC cases with
1564 put_no_rnd_pixels_l2 which would look more like the
1565 VP3 source but this would be slower as
1566 put_no_rnd_pixels_tab is better optimzed */
1567 if(motion_halfpel_index != 3){
1568 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
1569 output_plane + first_pixel,
1570 motion_source, stride, 8);
1572 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
1573 s->vp3dsp.put_no_rnd_pixels_l2(
1574 output_plane + first_pixel,
1576 motion_source + stride + 1 + d,
1581 /* invert DCT and place (or add) in final output */
1583 if (s->all_fragments[i].coding_method == MODE_INTRA) {
1584 vp3_dequant(s, s->all_fragments + i, plane, 0, block);
1586 output_plane + first_pixel,
1590 if (vp3_dequant(s, s->all_fragments + i, plane, 1, block)) {
1592 output_plane + first_pixel,
1596 s->vp3dsp.idct_dc_add(output_plane + first_pixel, stride, block);
1601 /* copy directly from the previous frame */
1602 s->dsp.put_pixels_tab[1][0](
1603 output_plane + first_pixel,
1604 last_plane + first_pixel,
1611 // Filter up to the last row in the superblock row
1612 if (!s->skip_loop_filter)
1613 apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1));
1617 /* this looks like a good place for slice dispatch... */
1619 * if (slice == s->macroblock_height - 1)
1620 * dispatch (both last slice & 2nd-to-last slice);
1621 * else if (slice > 0)
1622 * dispatch (slice - 1);
1625 vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) -16, s->height-16));
1628 /// Allocate tables for per-frame data in Vp3DecodeContext
1629 static av_cold int allocate_tables(AVCodecContext *avctx)
1631 Vp3DecodeContext *s = avctx->priv_data;
1632 int y_fragment_count, c_fragment_count;
1634 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1635 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1637 s->superblock_coding = av_malloc(s->superblock_count);
1638 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
1639 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
1640 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base));
1641 s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0]));
1642 s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1]));
1644 /* work out the block mapping tables */
1645 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
1646 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
1648 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
1649 !s->coded_fragment_list[0] || !s->superblock_fragments || !s->macroblock_coding ||
1650 !s->motion_val[0] || !s->motion_val[1]) {
1651 vp3_decode_end(avctx);
1655 init_block_mapping(s);
1660 static av_cold int vp3_decode_init(AVCodecContext *avctx)
1662 Vp3DecodeContext *s = avctx->priv_data;
1663 int i, inter, plane;
1666 int y_fragment_count, c_fragment_count;
1668 if (avctx->codec_tag == MKTAG('V','P','3','0'))
1674 s->width = FFALIGN(avctx->width, 16);
1675 s->height = FFALIGN(avctx->height, 16);
1676 if (avctx->codec_id != AV_CODEC_ID_THEORA)
1677 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1678 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1679 ff_dsputil_init(&s->dsp, avctx);
1680 ff_videodsp_init(&s->vdsp, 8);
1681 ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
1683 ff_init_scantable_permutation(s->dsp.idct_permutation, s->vp3dsp.idct_perm);
1684 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
1686 /* initialize to an impossible value which will force a recalculation
1687 * in the first frame decode */
1688 for (i = 0; i < 3; i++)
1691 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
1693 s->y_superblock_width = (s->width + 31) / 32;
1694 s->y_superblock_height = (s->height + 31) / 32;
1695 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
1697 /* work out the dimensions for the C planes */
1698 c_width = s->width >> s->chroma_x_shift;
1699 c_height = s->height >> s->chroma_y_shift;
1700 s->c_superblock_width = (c_width + 31) / 32;
1701 s->c_superblock_height = (c_height + 31) / 32;
1702 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
1704 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
1705 s->u_superblock_start = s->y_superblock_count;
1706 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
1708 s->macroblock_width = (s->width + 15) / 16;
1709 s->macroblock_height = (s->height + 15) / 16;
1710 s->macroblock_count = s->macroblock_width * s->macroblock_height;
1712 s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
1713 s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
1714 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
1715 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
1717 /* fragment count covers all 8x8 blocks for all 3 planes */
1718 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1719 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1720 s->fragment_count = y_fragment_count + 2*c_fragment_count;
1721 s->fragment_start[1] = y_fragment_count;
1722 s->fragment_start[2] = y_fragment_count + c_fragment_count;
1724 if (!s->theora_tables)
1726 for (i = 0; i < 64; i++) {
1727 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
1728 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
1729 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
1730 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
1731 s->base_matrix[2][i] = vp31_inter_dequant[i];
1732 s->filter_limit_values[i] = vp31_filter_limit_values[i];
1735 for(inter=0; inter<2; inter++){
1736 for(plane=0; plane<3; plane++){
1737 s->qr_count[inter][plane]= 1;
1738 s->qr_size [inter][plane][0]= 63;
1739 s->qr_base [inter][plane][0]=
1740 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
1744 /* init VLC tables */
1745 for (i = 0; i < 16; i++) {
1748 init_vlc(&s->dc_vlc[i], 11, 32,
1749 &dc_bias[i][0][1], 4, 2,
1750 &dc_bias[i][0][0], 4, 2, 0);
1752 /* group 1 AC histograms */
1753 init_vlc(&s->ac_vlc_1[i], 11, 32,
1754 &ac_bias_0[i][0][1], 4, 2,
1755 &ac_bias_0[i][0][0], 4, 2, 0);
1757 /* group 2 AC histograms */
1758 init_vlc(&s->ac_vlc_2[i], 11, 32,
1759 &ac_bias_1[i][0][1], 4, 2,
1760 &ac_bias_1[i][0][0], 4, 2, 0);
1762 /* group 3 AC histograms */
1763 init_vlc(&s->ac_vlc_3[i], 11, 32,
1764 &ac_bias_2[i][0][1], 4, 2,
1765 &ac_bias_2[i][0][0], 4, 2, 0);
1767 /* group 4 AC histograms */
1768 init_vlc(&s->ac_vlc_4[i], 11, 32,
1769 &ac_bias_3[i][0][1], 4, 2,
1770 &ac_bias_3[i][0][0], 4, 2, 0);
1774 for (i = 0; i < 16; i++) {
1776 if (init_vlc(&s->dc_vlc[i], 11, 32,
1777 &s->huffman_table[i][0][1], 8, 4,
1778 &s->huffman_table[i][0][0], 8, 4, 0) < 0)
1781 /* group 1 AC histograms */
1782 if (init_vlc(&s->ac_vlc_1[i], 11, 32,
1783 &s->huffman_table[i+16][0][1], 8, 4,
1784 &s->huffman_table[i+16][0][0], 8, 4, 0) < 0)
1787 /* group 2 AC histograms */
1788 if (init_vlc(&s->ac_vlc_2[i], 11, 32,
1789 &s->huffman_table[i+16*2][0][1], 8, 4,
1790 &s->huffman_table[i+16*2][0][0], 8, 4, 0) < 0)
1793 /* group 3 AC histograms */
1794 if (init_vlc(&s->ac_vlc_3[i], 11, 32,
1795 &s->huffman_table[i+16*3][0][1], 8, 4,
1796 &s->huffman_table[i+16*3][0][0], 8, 4, 0) < 0)
1799 /* group 4 AC histograms */
1800 if (init_vlc(&s->ac_vlc_4[i], 11, 32,
1801 &s->huffman_table[i+16*4][0][1], 8, 4,
1802 &s->huffman_table[i+16*4][0][0], 8, 4, 0) < 0)
1807 init_vlc(&s->superblock_run_length_vlc, 6, 34,
1808 &superblock_run_length_vlc_table[0][1], 4, 2,
1809 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
1811 init_vlc(&s->fragment_run_length_vlc, 5, 30,
1812 &fragment_run_length_vlc_table[0][1], 4, 2,
1813 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
1815 init_vlc(&s->mode_code_vlc, 3, 8,
1816 &mode_code_vlc_table[0][1], 2, 1,
1817 &mode_code_vlc_table[0][0], 2, 1, 0);
1819 init_vlc(&s->motion_vector_vlc, 6, 63,
1820 &motion_vector_vlc_table[0][1], 2, 1,
1821 &motion_vector_vlc_table[0][0], 2, 1, 0);
1823 for (i = 0; i < 3; i++) {
1824 s->current_frame.data[i] = NULL;
1825 s->last_frame.data[i] = NULL;
1826 s->golden_frame.data[i] = NULL;
1829 return allocate_tables(avctx);
1832 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
1836 /// Release and shuffle frames after decode finishes
1837 static void update_frames(AVCodecContext *avctx)
1839 Vp3DecodeContext *s = avctx->priv_data;
1841 /* release the last frame, if it is allocated and if it is not the
1843 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
1844 ff_thread_release_buffer(avctx, &s->last_frame);
1846 /* shuffle frames (last = current) */
1847 s->last_frame= s->current_frame;
1850 if (s->golden_frame.data[0])
1851 ff_thread_release_buffer(avctx, &s->golden_frame);
1852 s->golden_frame = s->current_frame;
1853 s->last_frame.type = FF_BUFFER_TYPE_COPY;
1856 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
1859 static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1861 Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
1862 int qps_changed = 0, i, err;
1864 #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1866 if (!s1->current_frame.data[0]
1867 ||s->width != s1->width
1868 ||s->height!= s1->height) {
1870 copy_fields(s, s1, golden_frame, keyframe);
1875 // init tables if the first frame hasn't been decoded
1876 if (!s->current_frame.data[0]) {
1877 int y_fragment_count, c_fragment_count;
1879 err = allocate_tables(dst);
1882 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1883 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1884 memcpy(s->motion_val[0], s1->motion_val[0], y_fragment_count * sizeof(*s->motion_val[0]));
1885 memcpy(s->motion_val[1], s1->motion_val[1], c_fragment_count * sizeof(*s->motion_val[1]));
1888 // copy previous frame data
1889 copy_fields(s, s1, golden_frame, dsp);
1891 // copy qscale data if necessary
1892 for (i = 0; i < 3; i++) {
1893 if (s->qps[i] != s1->qps[1]) {
1895 memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
1899 if (s->qps[0] != s1->qps[0])
1900 memcpy(&s->bounding_values_array, &s1->bounding_values_array, sizeof(s->bounding_values_array));
1903 copy_fields(s, s1, qps, superblock_count);
1912 static int vp3_decode_frame(AVCodecContext *avctx,
1913 void *data, int *got_frame,
1916 const uint8_t *buf = avpkt->data;
1917 int buf_size = avpkt->size;
1918 Vp3DecodeContext *s = avctx->priv_data;
1923 init_get_bits(&gb, buf, buf_size * 8);
1925 #if CONFIG_THEORA_DECODER
1926 if (s->theora && get_bits1(&gb))
1928 int type = get_bits(&gb, 7);
1929 skip_bits_long(&gb, 6*8); /* "theora" */
1931 if (s->avctx->active_thread_type&FF_THREAD_FRAME) {
1932 av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
1933 return AVERROR_PATCHWELCOME;
1936 vp3_decode_end(avctx);
1937 ret = theora_decode_header(avctx, &gb);
1940 vp3_decode_end(avctx);
1942 ret = vp3_decode_init(avctx);
1944 } else if (type == 2) {
1945 ret = theora_decode_tables(avctx, &gb);
1947 vp3_decode_end(avctx);
1949 ret = vp3_decode_init(avctx);
1953 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
1958 s->keyframe = !get_bits1(&gb);
1959 if (!s->all_fragments) {
1960 av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
1965 for (i = 0; i < 3; i++)
1966 s->last_qps[i] = s->qps[i];
1970 s->qps[s->nqps++]= get_bits(&gb, 6);
1971 } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));
1972 for (i = s->nqps; i < 3; i++)
1975 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1976 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
1977 s->keyframe?"key":"", avctx->frame_number+1, s->qps[0]);
1979 s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
1980 avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL : AVDISCARD_NONKEY);
1982 if (s->qps[0] != s->last_qps[0])
1983 init_loop_filter(s);
1985 for (i = 0; i < s->nqps; i++)
1986 // reinit all dequantizers if the first one changed, because
1987 // the DC of the first quantizer must be used for all matrices
1988 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
1989 init_dequantizer(s, i);
1991 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
1994 s->current_frame.reference = 3;
1995 s->current_frame.pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1996 s->current_frame.key_frame = s->keyframe;
1997 if (ff_thread_get_buffer(avctx, &s->current_frame) < 0) {
1998 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2002 if (!s->edge_emu_buffer)
2003 s->edge_emu_buffer = av_malloc(9*FFABS(s->current_frame.linesize[0]));
2008 skip_bits(&gb, 4); /* width code */
2009 skip_bits(&gb, 4); /* height code */
2012 s->version = get_bits(&gb, 5);
2013 if (avctx->frame_number == 0)
2014 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
2017 if (s->version || s->theora)
2020 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
2021 skip_bits(&gb, 2); /* reserved? */
2024 if (!s->golden_frame.data[0]) {
2025 av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n");
2027 s->golden_frame.reference = 3;
2028 s->golden_frame.pict_type = AV_PICTURE_TYPE_I;
2029 if (ff_thread_get_buffer(avctx, &s->golden_frame) < 0) {
2030 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2033 s->last_frame = s->golden_frame;
2034 s->last_frame.type = FF_BUFFER_TYPE_COPY;
2035 ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
2039 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2040 ff_thread_finish_setup(avctx);
2042 if (unpack_superblocks(s, &gb)){
2043 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2046 if (unpack_modes(s, &gb)){
2047 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2050 if (unpack_vectors(s, &gb)){
2051 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2054 if (unpack_block_qpis(s, &gb)){
2055 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2058 if (unpack_dct_coeffs(s, &gb)){
2059 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2063 for (i = 0; i < 3; i++) {
2064 int height = s->height >> (i && s->chroma_y_shift);
2065 if (s->flipped_image)
2066 s->data_offset[i] = 0;
2068 s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
2071 s->last_slice_end = 0;
2072 for (i = 0; i < s->c_superblock_height; i++)
2075 // filter the last row
2076 for (i = 0; i < 3; i++) {
2077 int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
2078 apply_loop_filter(s, i, row, row+1);
2080 vp3_draw_horiz_band(s, s->avctx->height);
2083 *(AVFrame*)data= s->current_frame;
2085 if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
2086 update_frames(avctx);
2091 ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
2093 if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
2094 avctx->release_buffer(avctx, &s->current_frame);
2099 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
2101 Vp3DecodeContext *s = avctx->priv_data;
2103 if (get_bits1(gb)) {
2105 if (s->entries >= 32) { /* overflow */
2106 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2109 token = get_bits(gb, 5);
2110 av_dlog(avctx, "hti %d hbits %x token %d entry : %d size %d\n",
2111 s->hti, s->hbits, token, s->entries, s->huff_code_size);
2112 s->huffman_table[s->hti][token][0] = s->hbits;
2113 s->huffman_table[s->hti][token][1] = s->huff_code_size;
2117 if (s->huff_code_size >= 32) {/* overflow */
2118 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2121 s->huff_code_size++;
2123 if (read_huffman_tree(avctx, gb))
2126 if (read_huffman_tree(avctx, gb))
2129 s->huff_code_size--;
2134 static int vp3_init_thread_copy(AVCodecContext *avctx)
2136 Vp3DecodeContext *s = avctx->priv_data;
2138 s->superblock_coding = NULL;
2139 s->all_fragments = NULL;
2140 s->coded_fragment_list[0] = NULL;
2141 s->dct_tokens_base = NULL;
2142 s->superblock_fragments = NULL;
2143 s->macroblock_coding = NULL;
2144 s->motion_val[0] = NULL;
2145 s->motion_val[1] = NULL;
2146 s->edge_emu_buffer = NULL;
2151 #if CONFIG_THEORA_DECODER
2152 static const enum AVPixelFormat theora_pix_fmts[4] = {
2153 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
2156 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2158 Vp3DecodeContext *s = avctx->priv_data;
2159 int visible_width, visible_height, colorspace;
2160 int offset_x = 0, offset_y = 0;
2161 AVRational fps, aspect;
2163 s->theora = get_bits_long(gb, 24);
2164 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2166 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
2167 /* but previous versions have the image flipped relative to vp3 */
2168 if (s->theora < 0x030200)
2170 s->flipped_image = 1;
2171 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
2174 visible_width = s->width = get_bits(gb, 16) << 4;
2175 visible_height = s->height = get_bits(gb, 16) << 4;
2177 if(av_image_check_size(s->width, s->height, 0, avctx)){
2178 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
2179 s->width= s->height= 0;
2183 if (s->theora >= 0x030200) {
2184 visible_width = get_bits_long(gb, 24);
2185 visible_height = get_bits_long(gb, 24);
2187 offset_x = get_bits(gb, 8); /* offset x */
2188 offset_y = get_bits(gb, 8); /* offset y, from bottom */
2191 fps.num = get_bits_long(gb, 32);
2192 fps.den = get_bits_long(gb, 32);
2193 if (fps.num && fps.den) {
2194 av_reduce(&avctx->time_base.num, &avctx->time_base.den,
2195 fps.den, fps.num, 1<<30);
2198 aspect.num = get_bits_long(gb, 24);
2199 aspect.den = get_bits_long(gb, 24);
2200 if (aspect.num && aspect.den) {
2201 av_reduce(&avctx->sample_aspect_ratio.num,
2202 &avctx->sample_aspect_ratio.den,
2203 aspect.num, aspect.den, 1<<30);
2206 if (s->theora < 0x030200)
2207 skip_bits(gb, 5); /* keyframe frequency force */
2208 colorspace = get_bits(gb, 8);
2209 skip_bits(gb, 24); /* bitrate */
2211 skip_bits(gb, 6); /* quality hint */
2213 if (s->theora >= 0x030200)
2215 skip_bits(gb, 5); /* keyframe frequency force */
2216 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
2217 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
2218 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
2219 return AVERROR_INVALIDDATA;
2221 skip_bits(gb, 3); /* reserved */
2224 // align_get_bits(gb);
2226 if ( visible_width <= s->width && visible_width > s->width-16
2227 && visible_height <= s->height && visible_height > s->height-16
2228 && !offset_x && (offset_y == s->height - visible_height))
2229 avcodec_set_dimensions(avctx, visible_width, visible_height);
2231 avcodec_set_dimensions(avctx, s->width, s->height);
2233 if (colorspace == 1) {
2234 avctx->color_primaries = AVCOL_PRI_BT470M;
2235 } else if (colorspace == 2) {
2236 avctx->color_primaries = AVCOL_PRI_BT470BG;
2238 if (colorspace == 1 || colorspace == 2) {
2239 avctx->colorspace = AVCOL_SPC_BT470BG;
2240 avctx->color_trc = AVCOL_TRC_BT709;
2246 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2248 Vp3DecodeContext *s = avctx->priv_data;
2249 int i, n, matrices, inter, plane;
2251 if (s->theora >= 0x030200) {
2252 n = get_bits(gb, 3);
2253 /* loop filter limit values table */
2255 for (i = 0; i < 64; i++)
2256 s->filter_limit_values[i] = get_bits(gb, n);
2259 if (s->theora >= 0x030200)
2260 n = get_bits(gb, 4) + 1;
2263 /* quality threshold table */
2264 for (i = 0; i < 64; i++)
2265 s->coded_ac_scale_factor[i] = get_bits(gb, n);
2267 if (s->theora >= 0x030200)
2268 n = get_bits(gb, 4) + 1;
2271 /* dc scale factor table */
2272 for (i = 0; i < 64; i++)
2273 s->coded_dc_scale_factor[i] = get_bits(gb, n);
2275 if (s->theora >= 0x030200)
2276 matrices = get_bits(gb, 9) + 1;
2281 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
2285 for(n=0; n<matrices; n++){
2286 for (i = 0; i < 64; i++)
2287 s->base_matrix[n][i]= get_bits(gb, 8);
2290 for (inter = 0; inter <= 1; inter++) {
2291 for (plane = 0; plane <= 2; plane++) {
2293 if (inter || plane > 0)
2294 newqr = get_bits1(gb);
2297 if(inter && get_bits1(gb)){
2301 qtj= (3*inter + plane - 1) / 3;
2302 plj= (plane + 2) % 3;
2304 s->qr_count[inter][plane]= s->qr_count[qtj][plj];
2305 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
2306 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
2312 i= get_bits(gb, av_log2(matrices-1)+1);
2314 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
2317 s->qr_base[inter][plane][qri]= i;
2320 i = get_bits(gb, av_log2(63-qi)+1) + 1;
2321 s->qr_size[inter][plane][qri++]= i;
2326 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
2329 s->qr_count[inter][plane]= qri;
2334 /* Huffman tables */
2335 for (s->hti = 0; s->hti < 80; s->hti++) {
2337 s->huff_code_size = 1;
2338 if (!get_bits1(gb)) {
2340 if(read_huffman_tree(avctx, gb))
2343 if(read_huffman_tree(avctx, gb))
2348 s->theora_tables = 1;
2353 static av_cold int theora_decode_init(AVCodecContext *avctx)
2355 Vp3DecodeContext *s = avctx->priv_data;
2358 uint8_t *header_start[3];
2362 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2366 if (!avctx->extradata_size)
2368 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
2372 if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2373 42, header_start, header_len) < 0) {
2374 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2379 if (header_len[i] <= 0)
2381 init_get_bits(&gb, header_start[i], header_len[i] * 8);
2383 ptype = get_bits(&gb, 8);
2385 if (!(ptype & 0x80))
2387 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
2391 // FIXME: Check for this as well.
2392 skip_bits_long(&gb, 6*8); /* "theora" */
2397 if (theora_decode_header(avctx, &gb) < 0)
2401 // FIXME: is this needed? it breaks sometimes
2402 // theora_decode_comments(avctx, gb);
2405 if (theora_decode_tables(avctx, &gb))
2409 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
2412 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
2413 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
2414 if (s->theora < 0x030200)
2418 return vp3_decode_init(avctx);
2421 AVCodec ff_theora_decoder = {
2423 .type = AVMEDIA_TYPE_VIDEO,
2424 .id = AV_CODEC_ID_THEORA,
2425 .priv_data_size = sizeof(Vp3DecodeContext),
2426 .init = theora_decode_init,
2427 .close = vp3_decode_end,
2428 .decode = vp3_decode_frame,
2429 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
2430 CODEC_CAP_FRAME_THREADS,
2431 .flush = vp3_decode_flush,
2432 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
2433 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2434 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2438 AVCodec ff_vp3_decoder = {
2440 .type = AVMEDIA_TYPE_VIDEO,
2441 .id = AV_CODEC_ID_VP3,
2442 .priv_data_size = sizeof(Vp3DecodeContext),
2443 .init = vp3_decode_init,
2444 .close = vp3_decode_end,
2445 .decode = vp3_decode_frame,
2446 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
2447 CODEC_CAP_FRAME_THREADS,
2448 .flush = vp3_decode_flush,
2449 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
2450 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2451 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),