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