2abcf5eecce90c9f7f09593a85c442407850b401
[libav.git] / libavcodec / vp3.c
1 /*
2 * Copyright (C) 2003-2004 the ffmpeg project
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file libavcodec/vp3.c
23 * On2 VP3 Video Decoder
24 *
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
28 *
29 * Theora decoder by Alex Beregszaszi
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "get_bits.h"
39
40 #include "vp3data.h"
41 #include "xiph.h"
42
43 #define FRAGMENT_PIXELS 8
44
45 static av_cold int vp3_decode_end(AVCodecContext *avctx);
46
47 typedef struct Coeff {
48 struct Coeff *next;
49 DCTELEM coeff;
50 uint8_t index;
51 } Coeff;
52
53 //FIXME split things out into their own arrays
54 typedef struct Vp3Fragment {
55 Coeff *next_coeff;
56 uint8_t coding_method;
57 int8_t motion_x;
58 int8_t motion_y;
59 uint8_t qpi;
60 } Vp3Fragment;
61
62 #define SB_NOT_CODED 0
63 #define SB_PARTIALLY_CODED 1
64 #define SB_FULLY_CODED 2
65
66 // This is the maximum length of a single long bit run that can be encoded
67 // for superblock coding or block qps. Theora special-cases this to read a
68 // bit instead of flipping the current bit to allow for runs longer than 4129.
69 #define MAXIMUM_LONG_BIT_RUN 4129
70
71 #define MODE_INTER_NO_MV 0
72 #define MODE_INTRA 1
73 #define MODE_INTER_PLUS_MV 2
74 #define MODE_INTER_LAST_MV 3
75 #define MODE_INTER_PRIOR_LAST 4
76 #define MODE_USING_GOLDEN 5
77 #define MODE_GOLDEN_MV 6
78 #define MODE_INTER_FOURMV 7
79 #define CODING_MODE_COUNT 8
80
81 /* special internal mode */
82 #define MODE_COPY 8
83
84 /* There are 6 preset schemes, plus a free-form scheme */
85 static const int ModeAlphabet[6][CODING_MODE_COUNT] =
86 {
87 /* scheme 1: Last motion vector dominates */
88 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
89 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
90 MODE_INTRA, MODE_USING_GOLDEN,
91 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
92
93 /* scheme 2 */
94 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
95 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
96 MODE_INTRA, MODE_USING_GOLDEN,
97 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
98
99 /* scheme 3 */
100 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
101 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
102 MODE_INTRA, MODE_USING_GOLDEN,
103 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
104
105 /* scheme 4 */
106 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
107 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
108 MODE_INTRA, MODE_USING_GOLDEN,
109 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
110
111 /* scheme 5: No motion vector dominates */
112 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
113 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
114 MODE_INTRA, MODE_USING_GOLDEN,
115 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
116
117 /* scheme 6 */
118 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
119 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
120 MODE_INTER_PLUS_MV, MODE_INTRA,
121 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
122
123 };
124
125 #define MIN_DEQUANT_VAL 2
126
127 typedef struct Vp3DecodeContext {
128 AVCodecContext *avctx;
129 int theora, theora_tables;
130 int version;
131 int width, height;
132 AVFrame golden_frame;
133 AVFrame last_frame;
134 AVFrame current_frame;
135 int keyframe;
136 DSPContext dsp;
137 int flipped_image;
138 int last_slice_end;
139
140 int qps[3];
141 int nqps;
142 int last_qps[3];
143
144 int superblock_count;
145 int y_superblock_width;
146 int y_superblock_height;
147 int y_superblock_count;
148 int c_superblock_width;
149 int c_superblock_height;
150 int c_superblock_count;
151 int u_superblock_start;
152 int v_superblock_start;
153 unsigned char *superblock_coding;
154
155 int macroblock_count;
156 int macroblock_width;
157 int macroblock_height;
158
159 int fragment_count;
160 int fragment_width;
161 int fragment_height;
162
163 Vp3Fragment *all_fragments;
164 uint8_t *coeff_counts;
165 Coeff *coeffs;
166 Coeff *next_coeff;
167 int fragment_start[3];
168 int data_offset[3];
169
170 ScanTable scantable;
171
172 /* tables */
173 uint16_t coded_dc_scale_factor[64];
174 uint32_t coded_ac_scale_factor[64];
175 uint8_t base_matrix[384][64];
176 uint8_t qr_count[2][3];
177 uint8_t qr_size [2][3][64];
178 uint16_t qr_base[2][3][64];
179
180 /* this is a list of indexes into the all_fragments array indicating
181 * which of the fragments are coded */
182 int *coded_fragment_list;
183 int coded_fragment_list_index;
184
185 /* track which fragments have already been decoded; called 'fast'
186 * because this data structure avoids having to iterate through every
187 * fragment in coded_fragment_list; once a fragment has been fully
188 * decoded, it is removed from this list */
189 int *fast_fragment_list;
190 int fragment_list_y_head;
191 int fragment_list_c_head;
192
193 VLC dc_vlc[16];
194 VLC ac_vlc_1[16];
195 VLC ac_vlc_2[16];
196 VLC ac_vlc_3[16];
197 VLC ac_vlc_4[16];
198
199 VLC superblock_run_length_vlc;
200 VLC fragment_run_length_vlc;
201 VLC mode_code_vlc;
202 VLC motion_vector_vlc;
203
204 /* these arrays need to be on 16-byte boundaries since SSE2 operations
205 * index into them */
206 DECLARE_ALIGNED_16(int16_t, qmat)[3][2][3][64]; //<qmat[qpi][is_inter][plane]
207
208 /* This table contains superblock_count * 16 entries. Each set of 16
209 * numbers corresponds to the fragment indexes 0..15 of the superblock.
210 * An entry will be -1 to indicate that no entry corresponds to that
211 * index. */
212 int *superblock_fragments;
213
214 /* This is an array that indicates how a particular macroblock
215 * is coded. */
216 unsigned char *macroblock_coding;
217
218 int first_coded_y_fragment;
219 int first_coded_c_fragment;
220 int last_coded_y_fragment;
221 int last_coded_c_fragment;
222
223 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
224 int8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
225
226 /* Huffman decode */
227 int hti;
228 unsigned int hbits;
229 int entries;
230 int huff_code_size;
231 uint16_t huffman_table[80][32][2];
232
233 uint8_t filter_limit_values[64];
234 DECLARE_ALIGNED_8(int, bounding_values_array)[256+2];
235 } Vp3DecodeContext;
236
237 /************************************************************************
238 * VP3 specific functions
239 ************************************************************************/
240
241 /*
242 * This function sets up all of the various blocks mappings:
243 * superblocks <-> fragments, macroblocks <-> fragments,
244 * superblocks <-> macroblocks
245 *
246 * Returns 0 is successful; returns 1 if *anything* went wrong.
247 */
248 static int init_block_mapping(Vp3DecodeContext *s)
249 {
250 int i, j;
251 signed int hilbert_walk_mb[4];
252
253 int current_fragment = 0;
254 int current_width = 0;
255 int current_height = 0;
256 int right_edge = 0;
257 int bottom_edge = 0;
258 int superblock_row_inc = 0;
259 int mapping_index = 0;
260
261 static const signed char travel_width[16] = {
262 1, 1, 0, -1,
263 0, 0, 1, 0,
264 1, 0, 1, 0,
265 0, -1, 0, 1
266 };
267
268 static const signed char travel_height[16] = {
269 0, 0, 1, 0,
270 1, 1, 0, -1,
271 0, 1, 0, -1,
272 -1, 0, -1, 0
273 };
274
275 hilbert_walk_mb[0] = 1;
276 hilbert_walk_mb[1] = s->macroblock_width;
277 hilbert_walk_mb[2] = 1;
278 hilbert_walk_mb[3] = -s->macroblock_width;
279
280 /* iterate through each superblock (all planes) and map the fragments */
281 for (i = 0; i < s->superblock_count; i++) {
282 /* time to re-assign the limits? */
283 if (i == 0) {
284
285 /* start of Y superblocks */
286 right_edge = s->fragment_width;
287 bottom_edge = s->fragment_height;
288 current_width = -1;
289 current_height = 0;
290 superblock_row_inc = 3 * s->fragment_width -
291 (s->y_superblock_width * 4 - s->fragment_width);
292
293 /* the first operation for this variable is to advance by 1 */
294 current_fragment = -1;
295
296 } else if (i == s->u_superblock_start) {
297
298 /* start of U superblocks */
299 right_edge = s->fragment_width / 2;
300 bottom_edge = s->fragment_height / 2;
301 current_width = -1;
302 current_height = 0;
303 superblock_row_inc = 3 * (s->fragment_width / 2) -
304 (s->c_superblock_width * 4 - s->fragment_width / 2);
305
306 /* the first operation for this variable is to advance by 1 */
307 current_fragment = s->fragment_start[1] - 1;
308
309 } else if (i == s->v_superblock_start) {
310
311 /* start of V superblocks */
312 right_edge = s->fragment_width / 2;
313 bottom_edge = s->fragment_height / 2;
314 current_width = -1;
315 current_height = 0;
316 superblock_row_inc = 3 * (s->fragment_width / 2) -
317 (s->c_superblock_width * 4 - s->fragment_width / 2);
318
319 /* the first operation for this variable is to advance by 1 */
320 current_fragment = s->fragment_start[2] - 1;
321
322 }
323
324 if (current_width >= right_edge - 1) {
325 /* reset width and move to next superblock row */
326 current_width = -1;
327 current_height += 4;
328
329 /* fragment is now at the start of a new superblock row */
330 current_fragment += superblock_row_inc;
331 }
332
333 /* iterate through all 16 fragments in a superblock */
334 for (j = 0; j < 16; j++) {
335 current_fragment += travel_width[j] + right_edge * travel_height[j];
336 current_width += travel_width[j];
337 current_height += travel_height[j];
338
339 /* check if the fragment is in bounds */
340 if ((current_width < right_edge) &&
341 (current_height < bottom_edge)) {
342 s->superblock_fragments[mapping_index] = current_fragment;
343 } else {
344 s->superblock_fragments[mapping_index] = -1;
345 }
346
347 mapping_index++;
348 }
349 }
350
351 return 0; /* successful path out */
352 }
353
354 /*
355 * This function wipes out all of the fragment data.
356 */
357 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
358 {
359 int i;
360
361 /* zero out all of the fragment information */
362 s->coded_fragment_list_index = 0;
363 for (i = 0; i < s->fragment_count; i++) {
364 s->coeff_counts[i] = 0;
365 s->all_fragments[i].motion_x = 127;
366 s->all_fragments[i].motion_y = 127;
367 s->all_fragments[i].next_coeff= NULL;
368 s->all_fragments[i].qpi = 0;
369 s->coeffs[i].index=
370 s->coeffs[i].coeff=0;
371 s->coeffs[i].next= NULL;
372 }
373 }
374
375 /*
376 * This function sets up the dequantization tables used for a particular
377 * frame.
378 */
379 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
380 {
381 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
382 int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
383 int i, plane, inter, qri, bmi, bmj, qistart;
384
385 for(inter=0; inter<2; inter++){
386 for(plane=0; plane<3; plane++){
387 int sum=0;
388 for(qri=0; qri<s->qr_count[inter][plane]; qri++){
389 sum+= s->qr_size[inter][plane][qri];
390 if(s->qps[qpi] <= sum)
391 break;
392 }
393 qistart= sum - s->qr_size[inter][plane][qri];
394 bmi= s->qr_base[inter][plane][qri ];
395 bmj= s->qr_base[inter][plane][qri+1];
396 for(i=0; i<64; i++){
397 int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i]
398 - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i]
399 + s->qr_size[inter][plane][qri])
400 / (2*s->qr_size[inter][plane][qri]);
401
402 int qmin= 8<<(inter + !i);
403 int qscale= i ? ac_scale_factor : dc_scale_factor;
404
405 s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
406 }
407 // all DC coefficients use the same quant so as not to interfere with DC prediction
408 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
409 }
410 }
411
412 memset(s->qscale_table, (FFMAX(s->qmat[0][0][0][1], s->qmat[0][0][1][1])+8)/16, 512); //FIXME finetune
413 }
414
415 /*
416 * This function initializes the loop filter boundary limits if the frame's
417 * quality index is different from the previous frame's.
418 *
419 * The filter_limit_values may not be larger than 127.
420 */
421 static void init_loop_filter(Vp3DecodeContext *s)
422 {
423 int *bounding_values= s->bounding_values_array+127;
424 int filter_limit;
425 int x;
426 int value;
427
428 filter_limit = s->filter_limit_values[s->qps[0]];
429
430 /* set up the bounding values */
431 memset(s->bounding_values_array, 0, 256 * sizeof(int));
432 for (x = 0; x < filter_limit; x++) {
433 bounding_values[-x] = -x;
434 bounding_values[x] = x;
435 }
436 for (x = value = filter_limit; x < 128 && value; x++, value--) {
437 bounding_values[ x] = value;
438 bounding_values[-x] = -value;
439 }
440 if (value)
441 bounding_values[128] = value;
442 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
443 }
444
445 /*
446 * This function unpacks all of the superblock/macroblock/fragment coding
447 * information from the bitstream.
448 */
449 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
450 {
451 int bit = 0;
452 int current_superblock = 0;
453 int current_run = 0;
454 int num_partial_superblocks = 0;
455 int first_c_fragment_seen;
456
457 int i, j;
458 int current_fragment;
459 int plane;
460
461 if (s->keyframe) {
462 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
463
464 } else {
465
466 /* unpack the list of partially-coded superblocks */
467 bit = get_bits1(gb);
468 while (current_superblock < s->superblock_count) {
469 current_run = get_vlc2(gb,
470 s->superblock_run_length_vlc.table, 6, 2) + 1;
471 if (current_run == 34)
472 current_run += get_bits(gb, 12);
473
474 if (current_superblock + current_run > s->superblock_count) {
475 av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n");
476 return -1;
477 }
478
479 memset(s->superblock_coding + current_superblock, bit, current_run);
480
481 current_superblock += current_run;
482 if (bit)
483 num_partial_superblocks += current_run;
484
485 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
486 bit = get_bits1(gb);
487 else
488 bit ^= 1;
489 }
490
491 /* unpack the list of fully coded superblocks if any of the blocks were
492 * not marked as partially coded in the previous step */
493 if (num_partial_superblocks < s->superblock_count) {
494 int superblocks_decoded = 0;
495
496 current_superblock = 0;
497 bit = get_bits1(gb);
498 while (superblocks_decoded < s->superblock_count - num_partial_superblocks) {
499 current_run = get_vlc2(gb,
500 s->superblock_run_length_vlc.table, 6, 2) + 1;
501 if (current_run == 34)
502 current_run += get_bits(gb, 12);
503
504 for (j = 0; j < current_run; current_superblock++) {
505 if (current_superblock >= s->superblock_count) {
506 av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded superblock run length\n");
507 return -1;
508 }
509
510 /* skip any superblocks already marked as partially coded */
511 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
512 s->superblock_coding[current_superblock] = 2*bit;
513 j++;
514 }
515 }
516 superblocks_decoded += current_run;
517
518 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
519 bit = get_bits1(gb);
520 else
521 bit ^= 1;
522 }
523 }
524
525 /* if there were partial blocks, initialize bitstream for
526 * unpacking fragment codings */
527 if (num_partial_superblocks) {
528
529 current_run = 0;
530 bit = get_bits1(gb);
531 /* toggle the bit because as soon as the first run length is
532 * fetched the bit will be toggled again */
533 bit ^= 1;
534 }
535 }
536
537 /* figure out which fragments are coded; iterate through each
538 * superblock (all planes) */
539 s->coded_fragment_list_index = 0;
540 s->next_coeff= s->coeffs + s->fragment_count;
541 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
542 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
543 first_c_fragment_seen = 0;
544 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
545
546 for (plane = 0; plane < 3; plane++) {
547 int sb_start = (int[]){ 0, s->u_superblock_start, s->v_superblock_start }[plane];
548 int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock_count);
549
550 for (i = sb_start; i < sb_end; i++) {
551
552 /* iterate through all 16 fragments in a superblock */
553 for (j = 0; j < 16; j++) {
554
555 /* if the fragment is in bounds, check its coding status */
556 current_fragment = s->superblock_fragments[i * 16 + j];
557 if (current_fragment >= s->fragment_count) {
558 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
559 current_fragment, s->fragment_count);
560 return 1;
561 }
562 if (current_fragment != -1) {
563 int coded = s->superblock_coding[i];
564
565 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
566
567 /* fragment may or may not be coded; this is the case
568 * that cares about the fragment coding runs */
569 if (current_run-- == 0) {
570 bit ^= 1;
571 current_run = get_vlc2(gb,
572 s->fragment_run_length_vlc.table, 5, 2);
573 }
574 coded = bit;
575 }
576
577 if (coded) {
578 /* default mode; actual mode will be decoded in
579 * the next phase */
580 s->all_fragments[current_fragment].coding_method =
581 MODE_INTER_NO_MV;
582 s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
583 s->coded_fragment_list[s->coded_fragment_list_index] =
584 current_fragment;
585 if (plane && !first_c_fragment_seen) {
586 s->first_coded_c_fragment = s->coded_fragment_list_index;
587 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
588 first_c_fragment_seen = 1;
589 }
590 s->coded_fragment_list_index++;
591 } else {
592 /* not coded; copy this fragment from the prior frame */
593 s->all_fragments[current_fragment].coding_method =
594 MODE_COPY;
595 }
596 }
597 }
598 }
599 }
600
601 if (!first_c_fragment_seen)
602 /* only Y fragments coded in this frame */
603 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
604 else
605 /* end the list of coded C fragments */
606 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
607
608 for (i = 0; i < s->fragment_count - 1; i++) {
609 s->fast_fragment_list[i] = i + 1;
610 }
611 s->fast_fragment_list[s->fragment_count - 1] = -1;
612
613 if (s->last_coded_y_fragment == -1)
614 s->fragment_list_y_head = -1;
615 else {
616 s->fragment_list_y_head = s->first_coded_y_fragment;
617 s->fast_fragment_list[s->last_coded_y_fragment] = -1;
618 }
619
620 if (s->last_coded_c_fragment == -1)
621 s->fragment_list_c_head = -1;
622 else {
623 s->fragment_list_c_head = s->first_coded_c_fragment;
624 s->fast_fragment_list[s->last_coded_c_fragment] = -1;
625 }
626
627 return 0;
628 }
629
630 /*
631 * This function unpacks all the coding mode data for individual macroblocks
632 * from the bitstream.
633 */
634 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
635 {
636 int i, j, k, sb_x, sb_y;
637 int scheme;
638 int current_macroblock;
639 int current_fragment;
640 int coding_mode;
641 int custom_mode_alphabet[CODING_MODE_COUNT];
642 const int *alphabet;
643
644 if (s->keyframe) {
645 for (i = 0; i < s->fragment_count; i++)
646 s->all_fragments[i].coding_method = MODE_INTRA;
647
648 } else {
649
650 /* fetch the mode coding scheme for this frame */
651 scheme = get_bits(gb, 3);
652
653 /* is it a custom coding scheme? */
654 if (scheme == 0) {
655 for (i = 0; i < 8; i++)
656 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
657 for (i = 0; i < 8; i++)
658 custom_mode_alphabet[get_bits(gb, 3)] = i;
659 alphabet = custom_mode_alphabet;
660 } else
661 alphabet = ModeAlphabet[scheme-1];
662
663 /* iterate through all of the macroblocks that contain 1 or more
664 * coded fragments */
665 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
666 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
667
668 for (j = 0; j < 4; j++) {
669 int mb_x = 2*sb_x + (j>>1);
670 int mb_y = 2*sb_y + (((j>>1)+j)&1);
671 current_macroblock = mb_y * s->macroblock_width + mb_x;
672
673 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height)
674 continue;
675
676 #define BLOCK_X (2*mb_x + (k&1))
677 #define BLOCK_Y (2*mb_y + (k>>1))
678 /* coding modes are only stored if the macroblock has at least one
679 * luma block coded, otherwise it must be INTER_NO_MV */
680 for (k = 0; k < 4; k++) {
681 current_fragment = BLOCK_Y*s->fragment_width + BLOCK_X;
682 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
683 break;
684 }
685 if (k == 4) {
686 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
687 continue;
688 }
689
690 /* mode 7 means get 3 bits for each coding mode */
691 if (scheme == 7)
692 coding_mode = get_bits(gb, 3);
693 else
694 coding_mode = alphabet
695 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
696
697 s->macroblock_coding[current_macroblock] = coding_mode;
698 for (k = 0; k < 4; k++) {
699 current_fragment =
700 BLOCK_Y*s->fragment_width + BLOCK_X;
701 if (s->all_fragments[current_fragment].coding_method !=
702 MODE_COPY)
703 s->all_fragments[current_fragment].coding_method =
704 coding_mode;
705 }
706 for (k = 0; k < 2; k++) {
707 current_fragment = s->fragment_start[k+1] +
708 mb_y*(s->fragment_width>>1) + mb_x;
709 if (s->all_fragments[current_fragment].coding_method !=
710 MODE_COPY)
711 s->all_fragments[current_fragment].coding_method =
712 coding_mode;
713 }
714 }
715 }
716 }
717 }
718
719 return 0;
720 }
721
722 /*
723 * This function unpacks all the motion vectors for the individual
724 * macroblocks from the bitstream.
725 */
726 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
727 {
728 int j, k, sb_x, sb_y;
729 int coding_mode;
730 int motion_x[6];
731 int motion_y[6];
732 int last_motion_x = 0;
733 int last_motion_y = 0;
734 int prior_last_motion_x = 0;
735 int prior_last_motion_y = 0;
736 int current_macroblock;
737 int current_fragment;
738
739 if (s->keyframe)
740 return 0;
741
742 memset(motion_x, 0, 6 * sizeof(int));
743 memset(motion_y, 0, 6 * sizeof(int));
744
745 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
746 coding_mode = get_bits1(gb);
747
748 /* iterate through all of the macroblocks that contain 1 or more
749 * coded fragments */
750 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
751 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
752
753 for (j = 0; j < 4; j++) {
754 int mb_x = 2*sb_x + (j>>1);
755 int mb_y = 2*sb_y + (((j>>1)+j)&1);
756 current_macroblock = mb_y * s->macroblock_width + mb_x;
757
758 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||
759 (s->macroblock_coding[current_macroblock] == MODE_COPY))
760 continue;
761
762 switch (s->macroblock_coding[current_macroblock]) {
763
764 case MODE_INTER_PLUS_MV:
765 case MODE_GOLDEN_MV:
766 /* all 6 fragments use the same motion vector */
767 if (coding_mode == 0) {
768 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
769 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
770 } else {
771 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
772 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
773 }
774
775 /* vector maintenance, only on MODE_INTER_PLUS_MV */
776 if (s->macroblock_coding[current_macroblock] ==
777 MODE_INTER_PLUS_MV) {
778 prior_last_motion_x = last_motion_x;
779 prior_last_motion_y = last_motion_y;
780 last_motion_x = motion_x[0];
781 last_motion_y = motion_y[0];
782 }
783 break;
784
785 case MODE_INTER_FOURMV:
786 /* vector maintenance */
787 prior_last_motion_x = last_motion_x;
788 prior_last_motion_y = last_motion_y;
789
790 /* fetch 4 vectors from the bitstream, one for each
791 * Y fragment, then average for the C fragment vectors */
792 motion_x[4] = motion_y[4] = 0;
793 for (k = 0; k < 4; k++) {
794 current_fragment = BLOCK_Y*s->fragment_width + BLOCK_X;
795 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
796 if (coding_mode == 0) {
797 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
798 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
799 } else {
800 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
801 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
802 }
803 last_motion_x = motion_x[k];
804 last_motion_y = motion_y[k];
805 } else {
806 motion_x[k] = 0;
807 motion_y[k] = 0;
808 }
809 motion_x[4] += motion_x[k];
810 motion_y[4] += motion_y[k];
811 }
812
813 motion_x[5]=
814 motion_x[4]= RSHIFT(motion_x[4], 2);
815 motion_y[5]=
816 motion_y[4]= RSHIFT(motion_y[4], 2);
817 break;
818
819 case MODE_INTER_LAST_MV:
820 /* all 6 fragments use the last motion vector */
821 motion_x[0] = last_motion_x;
822 motion_y[0] = last_motion_y;
823
824 /* no vector maintenance (last vector remains the
825 * last vector) */
826 break;
827
828 case MODE_INTER_PRIOR_LAST:
829 /* all 6 fragments use the motion vector prior to the
830 * last motion vector */
831 motion_x[0] = prior_last_motion_x;
832 motion_y[0] = prior_last_motion_y;
833
834 /* vector maintenance */
835 prior_last_motion_x = last_motion_x;
836 prior_last_motion_y = last_motion_y;
837 last_motion_x = motion_x[0];
838 last_motion_y = motion_y[0];
839 break;
840
841 default:
842 /* covers intra, inter without MV, golden without MV */
843 motion_x[0] = 0;
844 motion_y[0] = 0;
845
846 /* no vector maintenance */
847 break;
848 }
849
850 /* assign the motion vectors to the correct fragments */
851 for (k = 0; k < 4; k++) {
852 current_fragment =
853 BLOCK_Y*s->fragment_width + BLOCK_X;
854 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
855 s->all_fragments[current_fragment].motion_x = motion_x[k];
856 s->all_fragments[current_fragment].motion_y = motion_y[k];
857 } else {
858 s->all_fragments[current_fragment].motion_x = motion_x[0];
859 s->all_fragments[current_fragment].motion_y = motion_y[0];
860 }
861 }
862 for (k = 0; k < 2; k++) {
863 current_fragment = s->fragment_start[k+1] +
864 mb_y*(s->fragment_width>>1) + mb_x;
865 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
866 s->all_fragments[current_fragment].motion_x = motion_x[k+4];
867 s->all_fragments[current_fragment].motion_y = motion_y[k+4];
868 } else {
869 s->all_fragments[current_fragment].motion_x = motion_x[0];
870 s->all_fragments[current_fragment].motion_y = motion_y[0];
871 }
872 }
873 }
874 }
875 }
876
877 return 0;
878 }
879
880 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
881 {
882 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
883 int num_blocks = s->coded_fragment_list_index;
884
885 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) {
886 i = blocks_decoded = num_blocks_at_qpi = 0;
887
888 bit = get_bits1(gb);
889
890 do {
891 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
892 if (run_length == 34)
893 run_length += get_bits(gb, 12);
894 blocks_decoded += run_length;
895
896 if (!bit)
897 num_blocks_at_qpi += run_length;
898
899 for (j = 0; j < run_length; i++) {
900 if (i >= s->coded_fragment_list_index)
901 return -1;
902
903 if (s->all_fragments[s->coded_fragment_list[i]].qpi == qpi) {
904 s->all_fragments[s->coded_fragment_list[i]].qpi += bit;
905 j++;
906 }
907 }
908
909 if (run_length == MAXIMUM_LONG_BIT_RUN)
910 bit = get_bits1(gb);
911 else
912 bit ^= 1;
913 } while (blocks_decoded < num_blocks);
914
915 num_blocks -= num_blocks_at_qpi;
916 }
917
918 return 0;
919 }
920
921 /*
922 * This function is called by unpack_dct_coeffs() to extract the VLCs from
923 * the bitstream. The VLCs encode tokens which are used to unpack DCT
924 * data. This function unpacks all the VLCs for either the Y plane or both
925 * C planes, and is called for DC coefficients or different AC coefficient
926 * levels (since different coefficient types require different VLC tables.
927 *
928 * This function returns a residual eob run. E.g, if a particular token gave
929 * instructions to EOB the next 5 fragments and there were only 2 fragments
930 * left in the current fragment range, 3 would be returned so that it could
931 * be passed into the next call to this same function.
932 */
933 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
934 VLC *table, int coeff_index,
935 int y_plane,
936 int eob_run)
937 {
938 int i;
939 int token;
940 int zero_run = 0;
941 DCTELEM coeff = 0;
942 Vp3Fragment *fragment;
943 int bits_to_get;
944 int next_fragment;
945 int previous_fragment;
946 int fragment_num;
947 int *list_head;
948
949 /* local references to structure members to avoid repeated deferences */
950 uint8_t *perm= s->scantable.permutated;
951 int *coded_fragment_list = s->coded_fragment_list;
952 Vp3Fragment *all_fragments = s->all_fragments;
953 uint8_t *coeff_counts = s->coeff_counts;
954 VLC_TYPE (*vlc_table)[2] = table->table;
955 int *fast_fragment_list = s->fast_fragment_list;
956
957 if (y_plane) {
958 next_fragment = s->fragment_list_y_head;
959 list_head = &s->fragment_list_y_head;
960 } else {
961 next_fragment = s->fragment_list_c_head;
962 list_head = &s->fragment_list_c_head;
963 }
964
965 i = next_fragment;
966 previous_fragment = -1; /* this indicates that the previous fragment is actually the list head */
967 while (i != -1) {
968 fragment_num = coded_fragment_list[i];
969
970 if (coeff_counts[fragment_num] > coeff_index) {
971 previous_fragment = i;
972 i = fast_fragment_list[i];
973 continue;
974 }
975 fragment = &all_fragments[fragment_num];
976
977 if (!eob_run) {
978 /* decode a VLC into a token */
979 token = get_vlc2(gb, vlc_table, 5, 3);
980 /* use the token to get a zero run, a coefficient, and an eob run */
981 if (token <= 6) {
982 eob_run = eob_run_base[token];
983 if (eob_run_get_bits[token])
984 eob_run += get_bits(gb, eob_run_get_bits[token]);
985 coeff = zero_run = 0;
986 } else {
987 bits_to_get = coeff_get_bits[token];
988 if (bits_to_get)
989 bits_to_get = get_bits(gb, bits_to_get);
990 coeff = coeff_tables[token][bits_to_get];
991
992 zero_run = zero_run_base[token];
993 if (zero_run_get_bits[token])
994 zero_run += get_bits(gb, zero_run_get_bits[token]);
995 }
996 }
997
998 if (!eob_run) {
999 coeff_counts[fragment_num] += zero_run;
1000 if (coeff_counts[fragment_num] < 64){
1001 fragment->next_coeff->coeff= coeff;
1002 fragment->next_coeff->index= perm[coeff_counts[fragment_num]++]; //FIXME perm here already?
1003 fragment->next_coeff->next= s->next_coeff;
1004 s->next_coeff->next=NULL;
1005 fragment->next_coeff= s->next_coeff++;
1006 }
1007 /* previous fragment is now this fragment */
1008 previous_fragment = i;
1009 } else {
1010 coeff_counts[fragment_num] |= 128;
1011 eob_run--;
1012 /* remove this fragment from the list */
1013 if (previous_fragment != -1)
1014 fast_fragment_list[previous_fragment] = fast_fragment_list[i];
1015 else
1016 *list_head = fast_fragment_list[i];
1017 /* previous fragment remains unchanged */
1018 }
1019
1020 i = fast_fragment_list[i];
1021 }
1022
1023 return eob_run;
1024 }
1025
1026 static void reverse_dc_prediction(Vp3DecodeContext *s,
1027 int first_fragment,
1028 int fragment_width,
1029 int fragment_height);
1030 /*
1031 * This function unpacks all of the DCT coefficient data from the
1032 * bitstream.
1033 */
1034 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1035 {
1036 int i;
1037 int dc_y_table;
1038 int dc_c_table;
1039 int ac_y_table;
1040 int ac_c_table;
1041 int residual_eob_run = 0;
1042 VLC *y_tables[64];
1043 VLC *c_tables[64];
1044
1045 /* fetch the DC table indexes */
1046 dc_y_table = get_bits(gb, 4);
1047 dc_c_table = get_bits(gb, 4);
1048
1049 /* unpack the Y plane DC coefficients */
1050 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1051 1, residual_eob_run);
1052
1053 /* reverse prediction of the Y-plane DC coefficients */
1054 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
1055
1056 /* unpack the C plane DC coefficients */
1057 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1058 0, residual_eob_run);
1059
1060 /* reverse prediction of the C-plane DC coefficients */
1061 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1062 {
1063 reverse_dc_prediction(s, s->fragment_start[1],
1064 s->fragment_width / 2, s->fragment_height / 2);
1065 reverse_dc_prediction(s, s->fragment_start[2],
1066 s->fragment_width / 2, s->fragment_height / 2);
1067 }
1068
1069 /* fetch the AC table indexes */
1070 ac_y_table = get_bits(gb, 4);
1071 ac_c_table = get_bits(gb, 4);
1072
1073 /* build tables of AC VLC tables */
1074 for (i = 1; i <= 5; i++) {
1075 y_tables[i] = &s->ac_vlc_1[ac_y_table];
1076 c_tables[i] = &s->ac_vlc_1[ac_c_table];
1077 }
1078 for (i = 6; i <= 14; i++) {
1079 y_tables[i] = &s->ac_vlc_2[ac_y_table];
1080 c_tables[i] = &s->ac_vlc_2[ac_c_table];
1081 }
1082 for (i = 15; i <= 27; i++) {
1083 y_tables[i] = &s->ac_vlc_3[ac_y_table];
1084 c_tables[i] = &s->ac_vlc_3[ac_c_table];
1085 }
1086 for (i = 28; i <= 63; i++) {
1087 y_tables[i] = &s->ac_vlc_4[ac_y_table];
1088 c_tables[i] = &s->ac_vlc_4[ac_c_table];
1089 }
1090
1091 /* decode all AC coefficents */
1092 for (i = 1; i <= 63; i++) {
1093 if (s->fragment_list_y_head != -1)
1094 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1095 1, residual_eob_run);
1096
1097 if (s->fragment_list_c_head != -1)
1098 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1099 0, residual_eob_run);
1100 }
1101
1102 return 0;
1103 }
1104
1105 /*
1106 * This function reverses the DC prediction for each coded fragment in
1107 * the frame. Much of this function is adapted directly from the original
1108 * VP3 source code.
1109 */
1110 #define COMPATIBLE_FRAME(x) \
1111 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1112 #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
1113
1114 static void reverse_dc_prediction(Vp3DecodeContext *s,
1115 int first_fragment,
1116 int fragment_width,
1117 int fragment_height)
1118 {
1119
1120 #define PUL 8
1121 #define PU 4
1122 #define PUR 2
1123 #define PL 1
1124
1125 int x, y;
1126 int i = first_fragment;
1127
1128 int predicted_dc;
1129
1130 /* DC values for the left, up-left, up, and up-right fragments */
1131 int vl, vul, vu, vur;
1132
1133 /* indexes for the left, up-left, up, and up-right fragments */
1134 int l, ul, u, ur;
1135
1136 /*
1137 * The 6 fields mean:
1138 * 0: up-left multiplier
1139 * 1: up multiplier
1140 * 2: up-right multiplier
1141 * 3: left multiplier
1142 */
1143 static const int predictor_transform[16][4] = {
1144 { 0, 0, 0, 0},
1145 { 0, 0, 0,128}, // PL
1146 { 0, 0,128, 0}, // PUR
1147 { 0, 0, 53, 75}, // PUR|PL
1148 { 0,128, 0, 0}, // PU
1149 { 0, 64, 0, 64}, // PU|PL
1150 { 0,128, 0, 0}, // PU|PUR
1151 { 0, 0, 53, 75}, // PU|PUR|PL
1152 {128, 0, 0, 0}, // PUL
1153 { 0, 0, 0,128}, // PUL|PL
1154 { 64, 0, 64, 0}, // PUL|PUR
1155 { 0, 0, 53, 75}, // PUL|PUR|PL
1156 { 0,128, 0, 0}, // PUL|PU
1157 {-104,116, 0,116}, // PUL|PU|PL
1158 { 24, 80, 24, 0}, // PUL|PU|PUR
1159 {-104,116, 0,116} // PUL|PU|PUR|PL
1160 };
1161
1162 /* This table shows which types of blocks can use other blocks for
1163 * prediction. For example, INTRA is the only mode in this table to
1164 * have a frame number of 0. That means INTRA blocks can only predict
1165 * from other INTRA blocks. There are 2 golden frame coding types;
1166 * blocks encoding in these modes can only predict from other blocks
1167 * that were encoded with these 1 of these 2 modes. */
1168 static const unsigned char compatible_frame[9] = {
1169 1, /* MODE_INTER_NO_MV */
1170 0, /* MODE_INTRA */
1171 1, /* MODE_INTER_PLUS_MV */
1172 1, /* MODE_INTER_LAST_MV */
1173 1, /* MODE_INTER_PRIOR_MV */
1174 2, /* MODE_USING_GOLDEN */
1175 2, /* MODE_GOLDEN_MV */
1176 1, /* MODE_INTER_FOUR_MV */
1177 3 /* MODE_COPY */
1178 };
1179 int current_frame_type;
1180
1181 /* there is a last DC predictor for each of the 3 frame types */
1182 short last_dc[3];
1183
1184 int transform = 0;
1185
1186 vul = vu = vur = vl = 0;
1187 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1188
1189 /* for each fragment row... */
1190 for (y = 0; y < fragment_height; y++) {
1191
1192 /* for each fragment in a row... */
1193 for (x = 0; x < fragment_width; x++, i++) {
1194
1195 /* reverse prediction if this block was coded */
1196 if (s->all_fragments[i].coding_method != MODE_COPY) {
1197
1198 current_frame_type =
1199 compatible_frame[s->all_fragments[i].coding_method];
1200
1201 transform= 0;
1202 if(x){
1203 l= i-1;
1204 vl = DC_COEFF(l);
1205 if(COMPATIBLE_FRAME(l))
1206 transform |= PL;
1207 }
1208 if(y){
1209 u= i-fragment_width;
1210 vu = DC_COEFF(u);
1211 if(COMPATIBLE_FRAME(u))
1212 transform |= PU;
1213 if(x){
1214 ul= i-fragment_width-1;
1215 vul = DC_COEFF(ul);
1216 if(COMPATIBLE_FRAME(ul))
1217 transform |= PUL;
1218 }
1219 if(x + 1 < fragment_width){
1220 ur= i-fragment_width+1;
1221 vur = DC_COEFF(ur);
1222 if(COMPATIBLE_FRAME(ur))
1223 transform |= PUR;
1224 }
1225 }
1226
1227 if (transform == 0) {
1228
1229 /* if there were no fragments to predict from, use last
1230 * DC saved */
1231 predicted_dc = last_dc[current_frame_type];
1232 } else {
1233
1234 /* apply the appropriate predictor transform */
1235 predicted_dc =
1236 (predictor_transform[transform][0] * vul) +
1237 (predictor_transform[transform][1] * vu) +
1238 (predictor_transform[transform][2] * vur) +
1239 (predictor_transform[transform][3] * vl);
1240
1241 predicted_dc /= 128;
1242
1243 /* check for outranging on the [ul u l] and
1244 * [ul u ur l] predictors */
1245 if ((transform == 15) || (transform == 13)) {
1246 if (FFABS(predicted_dc - vu) > 128)
1247 predicted_dc = vu;
1248 else if (FFABS(predicted_dc - vl) > 128)
1249 predicted_dc = vl;
1250 else if (FFABS(predicted_dc - vul) > 128)
1251 predicted_dc = vul;
1252 }
1253 }
1254
1255 /* at long last, apply the predictor */
1256 if(s->coeffs[i].index){
1257 *s->next_coeff= s->coeffs[i];
1258 s->coeffs[i].index=0;
1259 s->coeffs[i].coeff=0;
1260 s->coeffs[i].next= s->next_coeff++;
1261 }
1262 s->coeffs[i].coeff += predicted_dc;
1263 /* save the DC */
1264 last_dc[current_frame_type] = DC_COEFF(i);
1265 if(DC_COEFF(i) && !(s->coeff_counts[i]&127)){
1266 s->coeff_counts[i]= 129;
1267 // s->all_fragments[i].next_coeff= s->next_coeff;
1268 s->coeffs[i].next= s->next_coeff;
1269 (s->next_coeff++)->next=NULL;
1270 }
1271 }
1272 }
1273 }
1274 }
1275
1276 static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
1277 {
1278 int x, y;
1279 int *bounding_values= s->bounding_values_array+127;
1280
1281 int width = s->fragment_width >> !!plane;
1282 int height = s->fragment_height >> !!plane;
1283 int fragment = s->fragment_start [plane] + ystart * width;
1284 int stride = s->current_frame.linesize[plane];
1285 uint8_t *plane_data = s->current_frame.data [plane];
1286 if (!s->flipped_image) stride = -stride;
1287 plane_data += s->data_offset[plane] + 8*ystart*stride;
1288
1289 for (y = ystart; y < yend; y++) {
1290
1291 for (x = 0; x < width; x++) {
1292 /* This code basically just deblocks on the edges of coded blocks.
1293 * However, it has to be much more complicated because of the
1294 * braindamaged deblock ordering used in VP3/Theora. Order matters
1295 * because some pixels get filtered twice. */
1296 if( s->all_fragments[fragment].coding_method != MODE_COPY )
1297 {
1298 /* do not perform left edge filter for left columns frags */
1299 if (x > 0) {
1300 s->dsp.vp3_h_loop_filter(
1301 plane_data + 8*x,
1302 stride, bounding_values);
1303 }
1304
1305 /* do not perform top edge filter for top row fragments */
1306 if (y > 0) {
1307 s->dsp.vp3_v_loop_filter(
1308 plane_data + 8*x,
1309 stride, bounding_values);
1310 }
1311
1312 /* do not perform right edge filter for right column
1313 * fragments or if right fragment neighbor is also coded
1314 * in this frame (it will be filtered in next iteration) */
1315 if ((x < width - 1) &&
1316 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1317 s->dsp.vp3_h_loop_filter(
1318 plane_data + 8*x + 8,
1319 stride, bounding_values);
1320 }
1321
1322 /* do not perform bottom edge filter for bottom row
1323 * fragments or if bottom fragment neighbor is also coded
1324 * in this frame (it will be filtered in the next row) */
1325 if ((y < height - 1) &&
1326 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1327 s->dsp.vp3_v_loop_filter(
1328 plane_data + 8*x + 8*stride,
1329 stride, bounding_values);
1330 }
1331 }
1332
1333 fragment++;
1334 }
1335 plane_data += 8*stride;
1336 }
1337 }
1338
1339 /**
1340 * called when all pixels up to row y are complete
1341 */
1342 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1343 {
1344 int h, cy;
1345 int offset[4];
1346
1347 if(s->avctx->draw_horiz_band==NULL)
1348 return;
1349
1350 h= y - s->last_slice_end;
1351 y -= h;
1352
1353 if (!s->flipped_image) {
1354 if (y == 0)
1355 h -= s->height - s->avctx->height; // account for non-mod16
1356 y = s->height - y - h;
1357 }
1358
1359 cy = y >> 1;
1360 offset[0] = s->current_frame.linesize[0]*y;
1361 offset[1] = s->current_frame.linesize[1]*cy;
1362 offset[2] = s->current_frame.linesize[2]*cy;
1363 offset[3] = 0;
1364
1365 emms_c();
1366 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
1367 s->last_slice_end= y + h;
1368 }
1369
1370 /*
1371 * Perform the final rendering for a particular slice of data.
1372 * The slice number ranges from 0..(macroblock_height - 1).
1373 */
1374 static void render_slice(Vp3DecodeContext *s, int slice)
1375 {
1376 int x;
1377 int16_t *dequantizer;
1378 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
1379 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1380 int motion_halfpel_index;
1381 uint8_t *motion_source;
1382 int plane;
1383
1384 if (slice >= s->macroblock_height)
1385 return;
1386
1387 for (plane = 0; plane < 3; plane++) {
1388 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
1389 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
1390 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
1391 int stride = s->current_frame.linesize[plane];
1392 int plane_width = s->width >> !!plane;
1393 int plane_height = s->height >> !!plane;
1394 int y = slice * FRAGMENT_PIXELS << !plane ;
1395 int slice_height = y + (FRAGMENT_PIXELS << !plane);
1396 int i = s->fragment_start[plane] + (y>>3)*(s->fragment_width>>!!plane);
1397
1398 if (!s->flipped_image) stride = -stride;
1399 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
1400 continue;
1401
1402
1403 if(FFABS(stride) > 2048)
1404 return; //various tables are fixed size
1405
1406 /* for each fragment row in the slice (both of them)... */
1407 for (; y < slice_height; y += 8) {
1408
1409 /* for each fragment in a row... */
1410 for (x = 0; x < plane_width; x += 8, i++) {
1411 int first_pixel = y*stride + x;
1412
1413 if ((i < 0) || (i >= s->fragment_count)) {
1414 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
1415 return;
1416 }
1417
1418 /* transform if this block was coded */
1419 if (s->all_fragments[i].coding_method != MODE_COPY) {
1420
1421 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
1422 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
1423 motion_source= golden_plane;
1424 else
1425 motion_source= last_plane;
1426
1427 motion_source += first_pixel;
1428 motion_halfpel_index = 0;
1429
1430 /* sort out the motion vector if this fragment is coded
1431 * using a motion vector method */
1432 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1433 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
1434 int src_x, src_y;
1435 motion_x = s->all_fragments[i].motion_x;
1436 motion_y = s->all_fragments[i].motion_y;
1437 if(plane){
1438 motion_x= (motion_x>>1) | (motion_x&1);
1439 motion_y= (motion_y>>1) | (motion_y&1);
1440 }
1441
1442 src_x= (motion_x>>1) + x;
1443 src_y= (motion_y>>1) + y;
1444 if ((motion_x == 127) || (motion_y == 127))
1445 av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
1446
1447 motion_halfpel_index = motion_x & 0x01;
1448 motion_source += (motion_x >> 1);
1449
1450 motion_halfpel_index |= (motion_y & 0x01) << 1;
1451 motion_source += ((motion_y >> 1) * stride);
1452
1453 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
1454 uint8_t *temp= s->edge_emu_buffer;
1455 if(stride<0) temp -= 9*stride;
1456 else temp += 9*stride;
1457
1458 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
1459 motion_source= temp;
1460 }
1461 }
1462
1463
1464 /* first, take care of copying a block from either the
1465 * previous or the golden frame */
1466 if (s->all_fragments[i].coding_method != MODE_INTRA) {
1467 /* Note, it is possible to implement all MC cases with
1468 put_no_rnd_pixels_l2 which would look more like the
1469 VP3 source but this would be slower as
1470 put_no_rnd_pixels_tab is better optimzed */
1471 if(motion_halfpel_index != 3){
1472 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
1473 output_plane + first_pixel,
1474 motion_source, stride, 8);
1475 }else{
1476 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
1477 s->dsp.put_no_rnd_pixels_l2[1](
1478 output_plane + first_pixel,
1479 motion_source - d,
1480 motion_source + stride + 1 + d,
1481 stride, 8);
1482 }
1483 dequantizer = s->qmat[s->all_fragments[i].qpi][1][plane];
1484 }else{
1485 dequantizer = s->qmat[s->all_fragments[i].qpi][0][plane];
1486 }
1487
1488 /* dequantize the DCT coefficients */
1489 if(s->avctx->idct_algo==FF_IDCT_VP3){
1490 Coeff *coeff= s->coeffs + i;
1491 s->dsp.clear_block(block);
1492 while(coeff->next){
1493 block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
1494 coeff= coeff->next;
1495 }
1496 }else{
1497 Coeff *coeff= s->coeffs + i;
1498 s->dsp.clear_block(block);
1499 while(coeff->next){
1500 block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
1501 coeff= coeff->next;
1502 }
1503 }
1504
1505 /* invert DCT and place (or add) in final output */
1506
1507 if (s->all_fragments[i].coding_method == MODE_INTRA) {
1508 if(s->avctx->idct_algo!=FF_IDCT_VP3)
1509 block[0] += 128<<3;
1510 s->dsp.idct_put(
1511 output_plane + first_pixel,
1512 stride,
1513 block);
1514 } else {
1515 s->dsp.idct_add(
1516 output_plane + first_pixel,
1517 stride,
1518 block);
1519 }
1520 } else {
1521
1522 /* copy directly from the previous frame */
1523 s->dsp.put_pixels_tab[1][0](
1524 output_plane + first_pixel,
1525 last_plane + first_pixel,
1526 stride, 8);
1527
1528 }
1529 }
1530 // Filter the previous block row. We can't filter the current row yet
1531 // since it needs pixels from the next row
1532 if (y > 0)
1533 apply_loop_filter(s, plane, (y>>3)-1, (y>>3));
1534 }
1535 }
1536
1537 /* this looks like a good place for slice dispatch... */
1538 /* algorithm:
1539 * if (slice == s->macroblock_height - 1)
1540 * dispatch (both last slice & 2nd-to-last slice);
1541 * else if (slice > 0)
1542 * dispatch (slice - 1);
1543 */
1544
1545 // now that we've filtered the last rows, they're safe to display
1546 if (slice)
1547 vp3_draw_horiz_band(s, 16*slice);
1548 }
1549
1550 /*
1551 * This is the ffmpeg/libavcodec API init function.
1552 */
1553 static av_cold int vp3_decode_init(AVCodecContext *avctx)
1554 {
1555 Vp3DecodeContext *s = avctx->priv_data;
1556 int i, inter, plane;
1557 int c_width;
1558 int c_height;
1559
1560 if (avctx->codec_tag == MKTAG('V','P','3','0'))
1561 s->version = 0;
1562 else
1563 s->version = 1;
1564
1565 s->avctx = avctx;
1566 s->width = FFALIGN(avctx->width, 16);
1567 s->height = FFALIGN(avctx->height, 16);
1568 avctx->pix_fmt = PIX_FMT_YUV420P;
1569 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1570 if(avctx->idct_algo==FF_IDCT_AUTO)
1571 avctx->idct_algo=FF_IDCT_VP3;
1572 dsputil_init(&s->dsp, avctx);
1573
1574 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
1575
1576 /* initialize to an impossible value which will force a recalculation
1577 * in the first frame decode */
1578 for (i = 0; i < 3; i++)
1579 s->qps[i] = -1;
1580
1581 s->y_superblock_width = (s->width + 31) / 32;
1582 s->y_superblock_height = (s->height + 31) / 32;
1583 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
1584
1585 /* work out the dimensions for the C planes */
1586 c_width = s->width / 2;
1587 c_height = s->height / 2;
1588 s->c_superblock_width = (c_width + 31) / 32;
1589 s->c_superblock_height = (c_height + 31) / 32;
1590 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
1591
1592 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
1593 s->u_superblock_start = s->y_superblock_count;
1594 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
1595 s->superblock_coding = av_malloc(s->superblock_count);
1596
1597 s->macroblock_width = (s->width + 15) / 16;
1598 s->macroblock_height = (s->height + 15) / 16;
1599 s->macroblock_count = s->macroblock_width * s->macroblock_height;
1600
1601 s->fragment_width = s->width / FRAGMENT_PIXELS;
1602 s->fragment_height = s->height / FRAGMENT_PIXELS;
1603
1604 /* fragment count covers all 8x8 blocks for all 3 planes */
1605 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
1606 s->fragment_start[1] = s->fragment_width * s->fragment_height;
1607 s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
1608
1609 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
1610 s->coeff_counts = av_malloc(s->fragment_count * sizeof(*s->coeff_counts));
1611 s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
1612 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
1613 s->fast_fragment_list = av_malloc(s->fragment_count * sizeof(int));
1614 if (!s->superblock_coding || !s->all_fragments || !s->coeff_counts ||
1615 !s->coeffs || !s->coded_fragment_list || !s->fast_fragment_list) {
1616 vp3_decode_end(avctx);
1617 return -1;
1618 }
1619
1620 if (!s->theora_tables)
1621 {
1622 for (i = 0; i < 64; i++) {
1623 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
1624 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
1625 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
1626 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
1627 s->base_matrix[2][i] = vp31_inter_dequant[i];
1628 s->filter_limit_values[i] = vp31_filter_limit_values[i];
1629 }
1630
1631 for(inter=0; inter<2; inter++){
1632 for(plane=0; plane<3; plane++){
1633 s->qr_count[inter][plane]= 1;
1634 s->qr_size [inter][plane][0]= 63;
1635 s->qr_base [inter][plane][0]=
1636 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
1637 }
1638 }
1639
1640 /* init VLC tables */
1641 for (i = 0; i < 16; i++) {
1642
1643 /* DC histograms */
1644 init_vlc(&s->dc_vlc[i], 5, 32,
1645 &dc_bias[i][0][1], 4, 2,
1646 &dc_bias[i][0][0], 4, 2, 0);
1647
1648 /* group 1 AC histograms */
1649 init_vlc(&s->ac_vlc_1[i], 5, 32,
1650 &ac_bias_0[i][0][1], 4, 2,
1651 &ac_bias_0[i][0][0], 4, 2, 0);
1652
1653 /* group 2 AC histograms */
1654 init_vlc(&s->ac_vlc_2[i], 5, 32,
1655 &ac_bias_1[i][0][1], 4, 2,
1656 &ac_bias_1[i][0][0], 4, 2, 0);
1657
1658 /* group 3 AC histograms */
1659 init_vlc(&s->ac_vlc_3[i], 5, 32,
1660 &ac_bias_2[i][0][1], 4, 2,
1661 &ac_bias_2[i][0][0], 4, 2, 0);
1662
1663 /* group 4 AC histograms */
1664 init_vlc(&s->ac_vlc_4[i], 5, 32,
1665 &ac_bias_3[i][0][1], 4, 2,
1666 &ac_bias_3[i][0][0], 4, 2, 0);
1667 }
1668 } else {
1669 for (i = 0; i < 16; i++) {
1670
1671 /* DC histograms */
1672 if (init_vlc(&s->dc_vlc[i], 5, 32,
1673 &s->huffman_table[i][0][1], 4, 2,
1674 &s->huffman_table[i][0][0], 4, 2, 0) < 0)
1675 goto vlc_fail;
1676
1677 /* group 1 AC histograms */
1678 if (init_vlc(&s->ac_vlc_1[i], 5, 32,
1679 &s->huffman_table[i+16][0][1], 4, 2,
1680 &s->huffman_table[i+16][0][0], 4, 2, 0) < 0)
1681 goto vlc_fail;
1682
1683 /* group 2 AC histograms */
1684 if (init_vlc(&s->ac_vlc_2[i], 5, 32,
1685 &s->huffman_table[i+16*2][0][1], 4, 2,
1686 &s->huffman_table[i+16*2][0][0], 4, 2, 0) < 0)
1687 goto vlc_fail;
1688
1689 /* group 3 AC histograms */
1690 if (init_vlc(&s->ac_vlc_3[i], 5, 32,
1691 &s->huffman_table[i+16*3][0][1], 4, 2,
1692 &s->huffman_table[i+16*3][0][0], 4, 2, 0) < 0)
1693 goto vlc_fail;
1694
1695 /* group 4 AC histograms */
1696 if (init_vlc(&s->ac_vlc_4[i], 5, 32,
1697 &s->huffman_table[i+16*4][0][1], 4, 2,
1698 &s->huffman_table[i+16*4][0][0], 4, 2, 0) < 0)
1699 goto vlc_fail;
1700 }
1701 }
1702
1703 init_vlc(&s->superblock_run_length_vlc, 6, 34,
1704 &superblock_run_length_vlc_table[0][1], 4, 2,
1705 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
1706
1707 init_vlc(&s->fragment_run_length_vlc, 5, 30,
1708 &fragment_run_length_vlc_table[0][1], 4, 2,
1709 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
1710
1711 init_vlc(&s->mode_code_vlc, 3, 8,
1712 &mode_code_vlc_table[0][1], 2, 1,
1713 &mode_code_vlc_table[0][0], 2, 1, 0);
1714
1715 init_vlc(&s->motion_vector_vlc, 6, 63,
1716 &motion_vector_vlc_table[0][1], 2, 1,
1717 &motion_vector_vlc_table[0][0], 2, 1, 0);
1718
1719 /* work out the block mapping tables */
1720 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
1721 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
1722 if (!s->superblock_fragments || !s->macroblock_coding) {
1723 vp3_decode_end(avctx);
1724 return -1;
1725 }
1726 init_block_mapping(s);
1727
1728 for (i = 0; i < 3; i++) {
1729 s->current_frame.data[i] = NULL;
1730 s->last_frame.data[i] = NULL;
1731 s->golden_frame.data[i] = NULL;
1732 }
1733
1734 return 0;
1735
1736 vlc_fail:
1737 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
1738 return -1;
1739 }
1740
1741 /*
1742 * This is the ffmpeg/libavcodec API frame decode function.
1743 */
1744 static int vp3_decode_frame(AVCodecContext *avctx,
1745 void *data, int *data_size,
1746 AVPacket *avpkt)
1747 {
1748 const uint8_t *buf = avpkt->data;
1749 int buf_size = avpkt->size;
1750 Vp3DecodeContext *s = avctx->priv_data;
1751 GetBitContext gb;
1752 static int counter = 0;
1753 int i;
1754
1755 init_get_bits(&gb, buf, buf_size * 8);
1756
1757 if (s->theora && get_bits1(&gb))
1758 {
1759 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
1760 return -1;
1761 }
1762
1763 s->keyframe = !get_bits1(&gb);
1764 if (!s->theora)
1765 skip_bits(&gb, 1);
1766 for (i = 0; i < 3; i++)
1767 s->last_qps[i] = s->qps[i];
1768
1769 s->nqps=0;
1770 do{
1771 s->qps[s->nqps++]= get_bits(&gb, 6);
1772 } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));
1773 for (i = s->nqps; i < 3; i++)
1774 s->qps[i] = -1;
1775
1776 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1777 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
1778 s->keyframe?"key":"", counter, s->qps[0]);
1779 counter++;
1780
1781 if (s->qps[0] != s->last_qps[0])
1782 init_loop_filter(s);
1783
1784 for (i = 0; i < s->nqps; i++)
1785 // reinit all dequantizers if the first one changed, because
1786 // the DC of the first quantizer must be used for all matrices
1787 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
1788 init_dequantizer(s, i);
1789
1790 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
1791 return buf_size;
1792
1793 if (s->keyframe) {
1794 if (!s->theora)
1795 {
1796 skip_bits(&gb, 4); /* width code */
1797 skip_bits(&gb, 4); /* height code */
1798 if (s->version)
1799 {
1800 s->version = get_bits(&gb, 5);
1801 if (counter == 1)
1802 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
1803 }
1804 }
1805 if (s->version || s->theora)
1806 {
1807 if (get_bits1(&gb))
1808 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
1809 skip_bits(&gb, 2); /* reserved? */
1810 }
1811
1812 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
1813 if (s->golden_frame.data[0])
1814 avctx->release_buffer(avctx, &s->golden_frame);
1815 s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
1816 } else {
1817 if (s->golden_frame.data[0])
1818 avctx->release_buffer(avctx, &s->golden_frame);
1819 if (s->last_frame.data[0])
1820 avctx->release_buffer(avctx, &s->last_frame);
1821 }
1822
1823 s->golden_frame.reference = 3;
1824 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
1825 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
1826 return -1;
1827 }
1828
1829 /* golden frame is also the current frame */
1830 s->current_frame= s->golden_frame;
1831 } else {
1832 /* allocate a new current frame */
1833 s->current_frame.reference = 3;
1834 if (!s->golden_frame.data[0]) {
1835 av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
1836 return -1;
1837 }
1838 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
1839 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
1840 return -1;
1841 }
1842 }
1843
1844 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
1845 s->current_frame.qstride= 0;
1846
1847 init_frame(s, &gb);
1848
1849 if (unpack_superblocks(s, &gb)){
1850 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
1851 return -1;
1852 }
1853 if (unpack_modes(s, &gb)){
1854 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
1855 return -1;
1856 }
1857 if (unpack_vectors(s, &gb)){
1858 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
1859 return -1;
1860 }
1861 if (unpack_block_qpis(s, &gb)){
1862 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
1863 return -1;
1864 }
1865 if (unpack_dct_coeffs(s, &gb)){
1866 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
1867 return -1;
1868 }
1869
1870 for (i = 0; i < 3; i++) {
1871 if (s->flipped_image)
1872 s->data_offset[i] = 0;
1873 else
1874 s->data_offset[i] = ((s->height>>!!i)-1) * s->current_frame.linesize[i];
1875 }
1876
1877 s->last_slice_end = 0;
1878 for (i = 0; i < s->macroblock_height; i++)
1879 render_slice(s, i);
1880
1881 // filter the last row
1882 for (i = 0; i < 3; i++) {
1883 int row = (s->height >> (3+!!i)) - 1;
1884 apply_loop_filter(s, i, row, row+1);
1885 }
1886 vp3_draw_horiz_band(s, s->height);
1887
1888 *data_size=sizeof(AVFrame);
1889 *(AVFrame*)data= s->current_frame;
1890
1891 /* release the last frame, if it is allocated and if it is not the
1892 * golden frame */
1893 if ((s->last_frame.data[0]) &&
1894 (s->last_frame.data[0] != s->golden_frame.data[0]))
1895 avctx->release_buffer(avctx, &s->last_frame);
1896
1897 /* shuffle frames (last = current) */
1898 s->last_frame= s->current_frame;
1899 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
1900
1901 return buf_size;
1902 }
1903
1904 /*
1905 * This is the ffmpeg/libavcodec API module cleanup function.
1906 */
1907 static av_cold int vp3_decode_end(AVCodecContext *avctx)
1908 {
1909 Vp3DecodeContext *s = avctx->priv_data;
1910 int i;
1911
1912 av_free(s->superblock_coding);
1913 av_free(s->all_fragments);
1914 av_free(s->coeff_counts);
1915 av_free(s->coeffs);
1916 av_free(s->coded_fragment_list);
1917 av_free(s->fast_fragment_list);
1918 av_free(s->superblock_fragments);
1919 av_free(s->macroblock_coding);
1920
1921 for (i = 0; i < 16; i++) {
1922 free_vlc(&s->dc_vlc[i]);
1923 free_vlc(&s->ac_vlc_1[i]);
1924 free_vlc(&s->ac_vlc_2[i]);
1925 free_vlc(&s->ac_vlc_3[i]);
1926 free_vlc(&s->ac_vlc_4[i]);
1927 }
1928
1929 free_vlc(&s->superblock_run_length_vlc);
1930 free_vlc(&s->fragment_run_length_vlc);
1931 free_vlc(&s->mode_code_vlc);
1932 free_vlc(&s->motion_vector_vlc);
1933
1934 /* release all frames */
1935 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
1936 avctx->release_buffer(avctx, &s->golden_frame);
1937 if (s->last_frame.data[0])
1938 avctx->release_buffer(avctx, &s->last_frame);
1939 /* no need to release the current_frame since it will always be pointing
1940 * to the same frame as either the golden or last frame */
1941
1942 return 0;
1943 }
1944
1945 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
1946 {
1947 Vp3DecodeContext *s = avctx->priv_data;
1948
1949 if (get_bits1(gb)) {
1950 int token;
1951 if (s->entries >= 32) { /* overflow */
1952 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
1953 return -1;
1954 }
1955 token = get_bits(gb, 5);
1956 //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);
1957 s->huffman_table[s->hti][token][0] = s->hbits;
1958 s->huffman_table[s->hti][token][1] = s->huff_code_size;
1959 s->entries++;
1960 }
1961 else {
1962 if (s->huff_code_size >= 32) {/* overflow */
1963 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
1964 return -1;
1965 }
1966 s->huff_code_size++;
1967 s->hbits <<= 1;
1968 if (read_huffman_tree(avctx, gb))
1969 return -1;
1970 s->hbits |= 1;
1971 if (read_huffman_tree(avctx, gb))
1972 return -1;
1973 s->hbits >>= 1;
1974 s->huff_code_size--;
1975 }
1976 return 0;
1977 }
1978
1979 #if CONFIG_THEORA_DECODER
1980 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
1981 {
1982 Vp3DecodeContext *s = avctx->priv_data;
1983 int visible_width, visible_height, colorspace;
1984
1985 s->theora = get_bits_long(gb, 24);
1986 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
1987
1988 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
1989 /* but previous versions have the image flipped relative to vp3 */
1990 if (s->theora < 0x030200)
1991 {
1992 s->flipped_image = 1;
1993 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
1994 }
1995
1996 visible_width = s->width = get_bits(gb, 16) << 4;
1997 visible_height = s->height = get_bits(gb, 16) << 4;
1998
1999 if(avcodec_check_dimensions(avctx, s->width, s->height)){
2000 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
2001 s->width= s->height= 0;
2002 return -1;
2003 }
2004
2005 if (s->theora >= 0x030200) {
2006 visible_width = get_bits_long(gb, 24);
2007 visible_height = get_bits_long(gb, 24);
2008
2009 skip_bits(gb, 8); /* offset x */
2010 skip_bits(gb, 8); /* offset y */
2011 }
2012
2013 skip_bits(gb, 32); /* fps numerator */
2014 skip_bits(gb, 32); /* fps denumerator */
2015 skip_bits(gb, 24); /* aspect numerator */
2016 skip_bits(gb, 24); /* aspect denumerator */
2017
2018 if (s->theora < 0x030200)
2019 skip_bits(gb, 5); /* keyframe frequency force */
2020 colorspace = get_bits(gb, 8);
2021 skip_bits(gb, 24); /* bitrate */
2022
2023 skip_bits(gb, 6); /* quality hint */
2024
2025 if (s->theora >= 0x030200)
2026 {
2027 skip_bits(gb, 5); /* keyframe frequency force */
2028 skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
2029 skip_bits(gb, 3); /* reserved */
2030 }
2031
2032 // align_get_bits(gb);
2033
2034 if ( visible_width <= s->width && visible_width > s->width-16
2035 && visible_height <= s->height && visible_height > s->height-16)
2036 avcodec_set_dimensions(avctx, visible_width, visible_height);
2037 else
2038 avcodec_set_dimensions(avctx, s->width, s->height);
2039
2040 if (colorspace == 1) {
2041 avctx->color_primaries = AVCOL_PRI_BT470M;
2042 } else if (colorspace == 2) {
2043 avctx->color_primaries = AVCOL_PRI_BT470BG;
2044 }
2045 if (colorspace == 1 || colorspace == 2) {
2046 avctx->colorspace = AVCOL_SPC_BT470BG;
2047 avctx->color_trc = AVCOL_TRC_BT709;
2048 }
2049
2050 return 0;
2051 }
2052
2053 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2054 {
2055 Vp3DecodeContext *s = avctx->priv_data;
2056 int i, n, matrices, inter, plane;
2057
2058 if (s->theora >= 0x030200) {
2059 n = get_bits(gb, 3);
2060 /* loop filter limit values table */
2061 for (i = 0; i < 64; i++) {
2062 s->filter_limit_values[i] = get_bits(gb, n);
2063 if (s->filter_limit_values[i] > 127) {
2064 av_log(avctx, AV_LOG_ERROR, "filter limit value too large (%i > 127), clamping\n", s->filter_limit_values[i]);
2065 s->filter_limit_values[i] = 127;
2066 }
2067 }
2068 }
2069
2070 if (s->theora >= 0x030200)
2071 n = get_bits(gb, 4) + 1;
2072 else
2073 n = 16;
2074 /* quality threshold table */
2075 for (i = 0; i < 64; i++)
2076 s->coded_ac_scale_factor[i] = get_bits(gb, n);
2077
2078 if (s->theora >= 0x030200)
2079 n = get_bits(gb, 4) + 1;
2080 else
2081 n = 16;
2082 /* dc scale factor table */
2083 for (i = 0; i < 64; i++)
2084 s->coded_dc_scale_factor[i] = get_bits(gb, n);
2085
2086 if (s->theora >= 0x030200)
2087 matrices = get_bits(gb, 9) + 1;
2088 else
2089 matrices = 3;
2090
2091 if(matrices > 384){
2092 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
2093 return -1;
2094 }
2095
2096 for(n=0; n<matrices; n++){
2097 for (i = 0; i < 64; i++)
2098 s->base_matrix[n][i]= get_bits(gb, 8);
2099 }
2100
2101 for (inter = 0; inter <= 1; inter++) {
2102 for (plane = 0; plane <= 2; plane++) {
2103 int newqr= 1;
2104 if (inter || plane > 0)
2105 newqr = get_bits1(gb);
2106 if (!newqr) {
2107 int qtj, plj;
2108 if(inter && get_bits1(gb)){
2109 qtj = 0;
2110 plj = plane;
2111 }else{
2112 qtj= (3*inter + plane - 1) / 3;
2113 plj= (plane + 2) % 3;
2114 }
2115 s->qr_count[inter][plane]= s->qr_count[qtj][plj];
2116 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
2117 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
2118 } else {
2119 int qri= 0;
2120 int qi = 0;
2121
2122 for(;;){
2123 i= get_bits(gb, av_log2(matrices-1)+1);
2124 if(i>= matrices){
2125 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
2126 return -1;
2127 }
2128 s->qr_base[inter][plane][qri]= i;
2129 if(qi >= 63)
2130 break;
2131 i = get_bits(gb, av_log2(63-qi)+1) + 1;
2132 s->qr_size[inter][plane][qri++]= i;
2133 qi += i;
2134 }
2135
2136 if (qi > 63) {
2137 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
2138 return -1;
2139 }
2140 s->qr_count[inter][plane]= qri;
2141 }
2142 }
2143 }
2144
2145 /* Huffman tables */
2146 for (s->hti = 0; s->hti < 80; s->hti++) {
2147 s->entries = 0;
2148 s->huff_code_size = 1;
2149 if (!get_bits1(gb)) {
2150 s->hbits = 0;
2151 if(read_huffman_tree(avctx, gb))
2152 return -1;
2153 s->hbits = 1;
2154 if(read_huffman_tree(avctx, gb))
2155 return -1;
2156 }
2157 }
2158
2159 s->theora_tables = 1;
2160
2161 return 0;
2162 }
2163
2164 static av_cold int theora_decode_init(AVCodecContext *avctx)
2165 {
2166 Vp3DecodeContext *s = avctx->priv_data;
2167 GetBitContext gb;
2168 int ptype;
2169 uint8_t *header_start[3];
2170 int header_len[3];
2171 int i;
2172
2173 s->theora = 1;
2174
2175 if (!avctx->extradata_size)
2176 {
2177 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
2178 return -1;
2179 }
2180
2181 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2182 42, header_start, header_len) < 0) {
2183 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2184 return -1;
2185 }
2186
2187 for(i=0;i<3;i++) {
2188 init_get_bits(&gb, header_start[i], header_len[i] * 8);
2189
2190 ptype = get_bits(&gb, 8);
2191
2192 if (!(ptype & 0x80))
2193 {
2194 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
2195 // return -1;
2196 }
2197
2198 // FIXME: Check for this as well.
2199 skip_bits_long(&gb, 6*8); /* "theora" */
2200
2201 switch(ptype)
2202 {
2203 case 0x80:
2204 theora_decode_header(avctx, &gb);
2205 break;
2206 case 0x81:
2207 // FIXME: is this needed? it breaks sometimes
2208 // theora_decode_comments(avctx, gb);
2209 break;
2210 case 0x82:
2211 if (theora_decode_tables(avctx, &gb))
2212 return -1;
2213 break;
2214 default:
2215 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
2216 break;
2217 }
2218 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
2219 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
2220 if (s->theora < 0x030200)
2221 break;
2222 }
2223
2224 return vp3_decode_init(avctx);
2225 }
2226
2227 AVCodec theora_decoder = {
2228 "theora",
2229 CODEC_TYPE_VIDEO,
2230 CODEC_ID_THEORA,
2231 sizeof(Vp3DecodeContext),
2232 theora_decode_init,
2233 NULL,
2234 vp3_decode_end,
2235 vp3_decode_frame,
2236 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
2237 NULL,
2238 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
2239 };
2240 #endif
2241
2242 AVCodec vp3_decoder = {
2243 "vp3",
2244 CODEC_TYPE_VIDEO,
2245 CODEC_ID_VP3,
2246 sizeof(Vp3DecodeContext),
2247 vp3_decode_init,
2248 NULL,
2249 vp3_decode_end,
2250 vp3_decode_frame,
2251 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
2252 NULL,
2253 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
2254 };