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