print obmc bug info only if avctx->debug&1
[libav.git] / libavcodec / vp3.c
CommitLineData
d86053a4
MM
1/*
2 *
3 * Copyright (C) 2003 the ffmpeg project
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * VP3 Video Decoder by Mike Melanson (melanson@pcisys.net)
20 *
21 */
22
23/**
24 * @file vp3.c
25 * On2 VP3 Video Decoder
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32
33#include "common.h"
34#include "avcodec.h"
35#include "dsputil.h"
36#include "mpegvideo.h"
37#include "dsputil.h"
38#include "bswap.h"
39
40#include "vp3data.h"
41
42#define FRAGMENT_PIXELS 8
43
44/*
45 * Debugging Variables
46 *
47 * Define one or more of the following compile-time variables to 1 to obtain
48 * elaborate information about certain aspects of the decoding process.
49 *
50 * DEBUG_VP3: high-level decoding flow
51 * DEBUG_INIT: initialization parameters
52 * DEBUG_DEQUANTIZERS: display how the dequanization tables are built
53 * DEBUG_BLOCK_CODING: unpacking the superblock/macroblock/fragment coding
54 * DEBUG_MODES: unpacking the coding modes for individual fragments
55 * DEBUG_VECTORS: display the motion vectors
56 * DEBUG_TOKEN: display exhaustive information about each DCT token
57 * DEBUG_VLC: display the VLCs as they are extracted from the stream
58 * DEBUG_DC_PRED: display the process of reversing DC prediction
59 * DEBUG_IDCT: show every detail of the IDCT process
60 */
61
62#define DEBUG_VP3 0
63#define DEBUG_INIT 0
64#define DEBUG_DEQUANTIZERS 0
65#define DEBUG_BLOCK_CODING 0
66#define DEBUG_MODES 0
67#define DEBUG_VECTORS 0
68#define DEBUG_TOKEN 0
69#define DEBUG_VLC 0
70#define DEBUG_DC_PRED 0
71#define DEBUG_IDCT 0
72
73#if DEBUG_VP3
74#define debug_vp3 printf
75#else
76static inline void debug_vp3(const char *format, ...) { }
77#endif
78
79#if DEBUG_INIT
80#define debug_init printf
81#else
82static inline void debug_init(const char *format, ...) { }
83#endif
84
85#if DEBUG_DEQUANTIZERS
86#define debug_dequantizers printf
87#else
88static inline void debug_dequantizers(const char *format, ...) { }
89#endif
90
91#if DEBUG_BLOCK_CODING
92#define debug_block_coding printf
93#else
94static inline void debug_block_coding(const char *format, ...) { }
95#endif
96
97#if DEBUG_MODES
98#define debug_modes printf
99#else
100static inline void debug_modes(const char *format, ...) { }
101#endif
102
103#if DEBUG_VECTORS
104#define debug_vectors printf
105#else
106static inline void debug_vectors(const char *format, ...) { }
107#endif
108
109#if DEBUG_TOKEN
110#define debug_token printf
111#else
112static inline void debug_token(const char *format, ...) { }
113#endif
114
115#if DEBUG_VLC
116#define debug_vlc printf
117#else
118static inline void debug_vlc(const char *format, ...) { }
119#endif
120
121#if DEBUG_DC_PRED
122#define debug_dc_pred printf
123#else
124static inline void debug_dc_pred(const char *format, ...) { }
125#endif
126
127#if DEBUG_IDCT
128#define debug_idct printf
129#else
130static inline void debug_idct(const char *format, ...) { }
131#endif
132
133typedef struct Vp3Fragment {
134 DCTELEM coeffs[64];
135 int coding_method;
136 int coeff_count;
137 int last_coeff;
138 int motion_x;
139 int motion_y;
44ae98dd
MM
140 /* this indicates which ffmpeg put_pixels() function to use:
141 * 00b = no halfpel, 01b = x halfpel, 10b = y halfpel, 11b = both halfpel */
142 int motion_halfpel_index;
d86053a4
MM
143 /* address of first pixel taking into account which plane the fragment
144 * lives on as well as the plane stride */
145 int first_pixel;
146 /* this is the macroblock that the fragment belongs to */
147 int macroblock;
148} Vp3Fragment;
149
150#define SB_NOT_CODED 0
151#define SB_PARTIALLY_CODED 1
152#define SB_FULLY_CODED 2
153
154#define MODE_INTER_NO_MV 0
155#define MODE_INTRA 1
156#define MODE_INTER_PLUS_MV 2
157#define MODE_INTER_LAST_MV 3
158#define MODE_INTER_PRIOR_LAST 4
159#define MODE_USING_GOLDEN 5
160#define MODE_GOLDEN_MV 6
161#define MODE_INTER_FOURMV 7
162#define CODING_MODE_COUNT 8
163
164/* special internal mode */
165#define MODE_COPY 8
166
167/* There are 6 preset schemes, plus a free-form scheme */
168static int ModeAlphabet[7][CODING_MODE_COUNT] =
169{
170 /* this is the custom scheme */
171 { 0, 0, 0, 0, 0, 0, 0, 0 },
172
173 /* scheme 1: Last motion vector dominates */
174 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
175 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
176 MODE_INTRA, MODE_USING_GOLDEN,
177 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
178
179 /* scheme 2 */
180 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
181 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
182 MODE_INTRA, MODE_USING_GOLDEN,
183 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
184
185 /* scheme 3 */
186 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
187 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
188 MODE_INTRA, MODE_USING_GOLDEN,
189 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
190
191 /* scheme 4 */
192 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
193 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
194 MODE_INTRA, MODE_USING_GOLDEN,
195 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
196
197 /* scheme 5: No motion vector dominates */
198 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
199 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
200 MODE_INTRA, MODE_USING_GOLDEN,
201 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
202
203 /* scheme 6 */
204 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
205 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
206 MODE_INTER_PLUS_MV, MODE_INTRA,
207 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
208
209};
210
211#define MIN_DEQUANT_VAL 2
212
213typedef struct Vp3DecodeContext {
214 AVCodecContext *avctx;
215 int width, height;
d86053a4
MM
216 AVFrame golden_frame;
217 AVFrame last_frame;
218 AVFrame current_frame;
219 int keyframe;
220 DSPContext dsp;
221
222 int quality_index;
223 int last_quality_index;
224
225 int superblock_count;
226 int superblock_width;
227 int superblock_height;
228 int u_superblock_start;
229 int v_superblock_start;
230 unsigned char *superblock_coding;
231
232 int macroblock_count;
233 int macroblock_width;
234 int macroblock_height;
235
236 int fragment_count;
237 int fragment_width;
238 int fragment_height;
239
240 Vp3Fragment *all_fragments;
241 int u_fragment_start;
242 int v_fragment_start;
243
244 /* this is a list of indices into the all_fragments array indicating
245 * which of the fragments are coded */
246 int *coded_fragment_list;
247 int coded_fragment_list_index;
248 int pixel_addresses_inited;
249
250 VLC dc_vlc[16];
251 VLC ac_vlc_1[16];
252 VLC ac_vlc_2[16];
253 VLC ac_vlc_3[16];
254 VLC ac_vlc_4[16];
255
256 int16_t intra_y_dequant[64];
257 int16_t intra_c_dequant[64];
258 int16_t inter_dequant[64];
259
260 /* This table contains superblock_count * 16 entries. Each set of 16
261 * numbers corresponds to the fragment indices 0..15 of the superblock.
262 * An entry will be -1 to indicate that no entry corresponds to that
263 * index. */
264 int *superblock_fragments;
265
266 /* This table contains superblock_count * 4 entries. Each set of 4
267 * numbers corresponds to the macroblock indices 0..3 of the superblock.
268 * An entry will be -1 to indicate that no entry corresponds to that
269 * index. */
270 int *superblock_macroblocks;
271
272 /* This table contains macroblock_count * 6 entries. Each set of 6
273 * numbers corresponds to the fragment indices 0..5 which comprise
274 * the macroblock (4 Y fragments and 2 C fragments). */
275 int *macroblock_fragments;
276 /* This is an array of flags indicating whether a particular
277 * macroblock is coded. */
278 unsigned char *macroblock_coded;
279
04331882
MM
280 int first_coded_y_fragment;
281 int first_coded_c_fragment;
282 int last_coded_y_fragment;
283 int last_coded_c_fragment;
284
d86053a4
MM
285} Vp3DecodeContext;
286
287/************************************************************************
288 * VP3 specific functions
289 ************************************************************************/
290
291/*
292 * This function sets up all of the various blocks mappings:
293 * superblocks <-> fragments, macroblocks <-> fragments,
294 * superblocks <-> macroblocks
295 */
296static void init_block_mapping(Vp3DecodeContext *s)
297{
298 int i, j;
299 signed int hilbert_walk_y[16];
300 signed int hilbert_walk_c[16];
301 signed int hilbert_walk_mb[4];
302
303 int current_fragment = 0;
304 int current_width = 0;
305 int current_height = 0;
306 int right_edge = 0;
307 int bottom_edge = 0;
308 int superblock_row_inc = 0;
309 int *hilbert = NULL;
310 int mapping_index = 0;
311
312 int current_macroblock;
313 int c_fragment;
314
315 signed char travel_width[16] = {
316 1, 1, 0, -1,
317 0, 0, 1, 0,
318 1, 0, 1, 0,
319 0, -1, 0, 1
320 };
321
322 signed char travel_height[16] = {
323 0, 0, 1, 0,
324 1, 1, 0, -1,
325 0, 1, 0, -1,
326 -1, 0, -1, 0
327 };
328
329 signed char travel_width_mb[4] = {
330 1, 0, 1, 0
331 };
332
333 signed char travel_height_mb[4] = {
334 0, 1, 0, -1
335 };
336
337 debug_vp3(" vp3: initialize block mapping tables\n");
338
339 /* figure out hilbert pattern per these frame dimensions */
340 hilbert_walk_y[0] = 1;
341 hilbert_walk_y[1] = 1;
342 hilbert_walk_y[2] = s->fragment_width;
343 hilbert_walk_y[3] = -1;
344 hilbert_walk_y[4] = s->fragment_width;
345 hilbert_walk_y[5] = s->fragment_width;
346 hilbert_walk_y[6] = 1;
347 hilbert_walk_y[7] = -s->fragment_width;
348 hilbert_walk_y[8] = 1;
349 hilbert_walk_y[9] = s->fragment_width;
350 hilbert_walk_y[10] = 1;
351 hilbert_walk_y[11] = -s->fragment_width;
352 hilbert_walk_y[12] = -s->fragment_width;
353 hilbert_walk_y[13] = -1;
354 hilbert_walk_y[14] = -s->fragment_width;
355 hilbert_walk_y[15] = 1;
356
357 hilbert_walk_c[0] = 1;
358 hilbert_walk_c[1] = 1;
359 hilbert_walk_c[2] = s->fragment_width / 2;
360 hilbert_walk_c[3] = -1;
361 hilbert_walk_c[4] = s->fragment_width / 2;
362 hilbert_walk_c[5] = s->fragment_width / 2;
363 hilbert_walk_c[6] = 1;
364 hilbert_walk_c[7] = -s->fragment_width / 2;
365 hilbert_walk_c[8] = 1;
366 hilbert_walk_c[9] = s->fragment_width / 2;
367 hilbert_walk_c[10] = 1;
368 hilbert_walk_c[11] = -s->fragment_width / 2;
369 hilbert_walk_c[12] = -s->fragment_width / 2;
370 hilbert_walk_c[13] = -1;
371 hilbert_walk_c[14] = -s->fragment_width / 2;
372 hilbert_walk_c[15] = 1;
373
374 hilbert_walk_mb[0] = 1;
375 hilbert_walk_mb[1] = s->macroblock_width;
376 hilbert_walk_mb[2] = 1;
377 hilbert_walk_mb[3] = -s->macroblock_width;
378
379 /* iterate through each superblock (all planes) and map the fragments */
380 for (i = 0; i < s->superblock_count; i++) {
381 debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
382 i, s->u_superblock_start, s->v_superblock_start);
383
384 /* time to re-assign the limits? */
385 if (i == 0) {
386
387 /* start of Y superblocks */
388 right_edge = s->fragment_width;
389 bottom_edge = s->fragment_height;
390 current_width = 0;
391 current_height = 0;
392 superblock_row_inc = 3 * s->fragment_width;
393 hilbert = hilbert_walk_y;
394
395 /* the first operation for this variable is to advance by 1 */
396 current_fragment = -1;
397
398 } else if (i == s->u_superblock_start) {
399
400 /* start of U superblocks */
401 right_edge = s->fragment_width / 2;
402 bottom_edge = s->fragment_height / 2;
403 current_width = 0;
404 current_height = 0;
405 superblock_row_inc = 3 * (s->fragment_width / 2);
406 hilbert = hilbert_walk_c;
407
408 /* the first operation for this variable is to advance by 1 */
409 current_fragment = s->u_fragment_start - 1;
410
411 } else if (i == s->v_superblock_start) {
412
413 /* start of V superblocks */
414 right_edge = s->fragment_width / 2;
415 bottom_edge = s->fragment_height / 2;
416 current_width = 0;
417 current_height = 0;
418 superblock_row_inc = 3 * (s->fragment_width / 2);
419 hilbert = hilbert_walk_c;
420
421 /* the first operation for this variable is to advance by 1 */
422 current_fragment = s->v_fragment_start - 1;
423
424 }
425
426 if (current_width >= right_edge) {
427 /* reset width and move to next superblock row */
428 current_width = 0;
429 current_height += 4;
430
431 /* fragment is now at the start of a new superblock row */
432 current_fragment += superblock_row_inc;
433 }
434
435 /* iterate through all 16 fragments in a superblock */
436 for (j = 0; j < 16; j++) {
437 current_fragment += hilbert[j];
438 current_height += travel_height[j];
439
440 /* check if the fragment is in bounds */
441 if ((current_width <= right_edge) &&
442 (current_height < bottom_edge)) {
443 s->superblock_fragments[mapping_index] = current_fragment;
444 debug_init(" mapping fragment %d to superblock %d, position %d\n",
445 s->superblock_fragments[mapping_index], i, j);
446 } else {
447 s->superblock_fragments[mapping_index] = -1;
448 debug_init(" superblock %d, position %d has no fragment\n",
449 i, j);
450 }
451
452 current_width += travel_width[j];
453 mapping_index++;
454 }
455 }
456
457 /* initialize the superblock <-> macroblock mapping; iterate through
458 * all of the Y plane superblocks to build this mapping */
459 right_edge = s->macroblock_width;
460 bottom_edge = s->macroblock_height;
461 current_width = 0;
462 current_height = 0;
463 superblock_row_inc = s->macroblock_width;
464 hilbert = hilbert_walk_mb;
465 mapping_index = 0;
466 current_macroblock = -1;
467 for (i = 0; i < s->u_superblock_start; i++) {
468
469 if (current_width >= right_edge) {
470 /* reset width and move to next superblock row */
471 current_width = 0;
472 current_height += 2;
473
474 /* macroblock is now at the start of a new superblock row */
475 current_macroblock += superblock_row_inc;
476 }
477
478 /* iterate through each potential macroblock in the superblock */
479 for (j = 0; j < 4; j++) {
480 current_macroblock += hilbert_walk_mb[j];
481 current_height += travel_height_mb[j];
482
483 /* check if the macroblock is in bounds */
484 if ((current_width <= right_edge) &&
485 (current_height < bottom_edge)) {
486 s->superblock_macroblocks[mapping_index] = current_macroblock;
487 debug_init(" mapping macroblock %d to superblock %d, position %d\n",
488 s->superblock_macroblocks[mapping_index], i, j);
489 } else {
490 s->superblock_macroblocks[mapping_index] = -1;
491 debug_init(" superblock %d, position %d has no macroblock\n",
492 i, j);
493 }
494
495 current_width += travel_width_mb[j];
496 mapping_index++;
497 }
498 }
499
500 /* initialize the macroblock <-> fragment mapping */
501 current_fragment = 0;
502 current_macroblock = 0;
503 mapping_index = 0;
504 for (i = 0; i < s->fragment_height; i += 2) {
505
506 for (j = 0; j < s->fragment_width; j += 2) {
507
508 debug_init(" macroblock %d contains fragments: ", current_macroblock);
509 s->all_fragments[current_fragment].macroblock = current_macroblock;
510 s->macroblock_fragments[mapping_index++] = current_fragment;
511 debug_init("%d ", current_fragment);
512
513 if (j + 1 < s->fragment_width) {
514 s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
515 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
516 debug_init("%d ", current_fragment + 1);
517 } else
518 s->macroblock_fragments[mapping_index++] = -1;
519
520 if (i + 1 < s->fragment_height) {
521 s->all_fragments[current_fragment + s->fragment_width].macroblock =
522 current_macroblock;
523 s->macroblock_fragments[mapping_index++] =
524 current_fragment + s->fragment_width;
525 debug_init("%d ", current_fragment + s->fragment_width);
526 } else
527 s->macroblock_fragments[mapping_index++] = -1;
528
529 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
530 s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
531 current_macroblock;
532 s->macroblock_fragments[mapping_index++] =
533 current_fragment + s->fragment_width + 1;
534 debug_init("%d ", current_fragment + s->fragment_width + 1);
535 } else
536 s->macroblock_fragments[mapping_index++] = -1;
537
538 /* C planes */
539 c_fragment = s->u_fragment_start +
540 (i * s->fragment_width / 4) + (j / 2);
541 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
542 s->macroblock_fragments[mapping_index++] = c_fragment;
543 debug_init("%d ", c_fragment);
544
545 c_fragment = s->v_fragment_start +
546 (i * s->fragment_width / 4) + (j / 2);
547 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
548 s->macroblock_fragments[mapping_index++] = c_fragment;
549 debug_init("%d ", c_fragment);
550
551 debug_init("\n");
552
553 if (j + 2 <= s->fragment_width)
554 current_fragment += 2;
555 else
556 current_fragment++;
557 current_macroblock++;
558 }
559
560 current_fragment += s->fragment_width;
561 }
562}
563
564/*
565 * This function unpacks a single token (which should be in the range 0..31)
566 * and returns a zero run (number of zero coefficients in current DCT matrix
567 * before next non-zero coefficient), the next DCT coefficient, and the
568 * number of consecutive, non-EOB'd DCT blocks to EOB.
569 */
570static void unpack_token(GetBitContext *gb, int token, int *zero_run,
571 DCTELEM *coeff, int *eob_run)
572{
573 int sign;
574
575 *zero_run = 0;
576 *eob_run = 0;
577 *coeff = 0;
578
579 debug_token(" vp3 token %d: ", token);
580 switch (token) {
581
582 case 0:
583 debug_token("DCT_EOB_TOKEN, EOB next block\n");
584 *eob_run = 1;
585 break;
586
587 case 1:
588 debug_token("DCT_EOB_PAIR_TOKEN, EOB next 2 blocks\n");
589 *eob_run = 2;
590 break;
591
592 case 2:
593 debug_token("DCT_EOB_TRIPLE_TOKEN, EOB next 3 blocks\n");
594 *eob_run = 3;
595 break;
596
597 case 3:
598 debug_token("DCT_REPEAT_RUN_TOKEN, ");
599 *eob_run = get_bits(gb, 2) + 4;
600 debug_token("EOB the next %d blocks\n", *eob_run);
601 break;
602
603 case 4:
604 debug_token("DCT_REPEAT_RUN2_TOKEN, ");
605 *eob_run = get_bits(gb, 3) + 8;
606 debug_token("EOB the next %d blocks\n", *eob_run);
607 break;
608
609 case 5:
610 debug_token("DCT_REPEAT_RUN3_TOKEN, ");
611 *eob_run = get_bits(gb, 4) + 16;
612 debug_token("EOB the next %d blocks\n", *eob_run);
613 break;
614
615 case 6:
616 debug_token("DCT_REPEAT_RUN4_TOKEN, ");
617 *eob_run = get_bits(gb, 12);
618 debug_token("EOB the next %d blocks\n", *eob_run);
619 break;
620
621 case 7:
622 debug_token("DCT_SHORT_ZRL_TOKEN, ");
623 /* note that this token actually indicates that (3 extra bits) + 1 0s
624 * should be output; this case specifies a run of (3 EBs) 0s and a
625 * coefficient of 0. */
626 *zero_run = get_bits(gb, 3);
627 *coeff = 0;
628 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
629 break;
630
631 case 8:
632 debug_token("DCT_ZRL_TOKEN, ");
633 /* note that this token actually indicates that (6 extra bits) + 1 0s
634 * should be output; this case specifies a run of (6 EBs) 0s and a
635 * coefficient of 0. */
636 *zero_run = get_bits(gb, 6);
637 *coeff = 0;
638 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
639 break;
640
641 case 9:
642 debug_token("ONE_TOKEN, output 1\n");
643 *coeff = 1;
644 break;
645
646 case 10:
647 debug_token("MINUS_ONE_TOKEN, output -1\n");
648 *coeff = -1;
649 break;
650
651 case 11:
652 debug_token("TWO_TOKEN, output 2\n");
653 *coeff = 2;
654 break;
655
656 case 12:
657 debug_token("MINUS_TWO_TOKEN, output -2\n");
658 *coeff = -2;
659 break;
660
661 case 13:
662 case 14:
663 case 15:
664 case 16:
665 debug_token("LOW_VAL_TOKENS, ");
666 if (get_bits(gb, 1))
667 *coeff = -(3 + (token - 13));
668 else
669 *coeff = 3 + (token - 13);
670 debug_token("output %d\n", *coeff);
671 break;
672
673 case 17:
674 debug_token("DCT_VAL_CATEGORY3, ");
675 sign = get_bits(gb, 1);
676 *coeff = 7 + get_bits(gb, 1);
677 if (sign)
678 *coeff = -(*coeff);
679 debug_token("output %d\n", *coeff);
680 break;
681
682 case 18:
683 debug_token("DCT_VAL_CATEGORY4, ");
684 sign = get_bits(gb, 1);
685 *coeff = 9 + get_bits(gb, 2);
686 if (sign)
687 *coeff = -(*coeff);
688 debug_token("output %d\n", *coeff);
689 break;
690
691 case 19:
692 debug_token("DCT_VAL_CATEGORY5, ");
693 sign = get_bits(gb, 1);
694 *coeff = 13 + get_bits(gb, 3);
695 if (sign)
696 *coeff = -(*coeff);
697 debug_token("output %d\n", *coeff);
698 break;
699
700 case 20:
701 debug_token("DCT_VAL_CATEGORY6, ");
702 sign = get_bits(gb, 1);
703 *coeff = 21 + get_bits(gb, 4);
704 if (sign)
705 *coeff = -(*coeff);
706 debug_token("output %d\n", *coeff);
707 break;
708
709 case 21:
710 debug_token("DCT_VAL_CATEGORY7, ");
711 sign = get_bits(gb, 1);
712 *coeff = 37 + get_bits(gb, 5);
713 if (sign)
714 *coeff = -(*coeff);
715 debug_token("output %d\n", *coeff);
716 break;
717
718 case 22:
719 debug_token("DCT_VAL_CATEGORY8, ");
720 sign = get_bits(gb, 1);
721 *coeff = 69 + get_bits(gb, 9);
722 if (sign)
723 *coeff = -(*coeff);
724 debug_token("output %d\n", *coeff);
725 break;
726
727 case 23:
728 case 24:
729 case 25:
730 case 26:
731 case 27:
732 debug_token("DCT_RUN_CATEGORY1, ");
733 *zero_run = token - 22;
734 if (get_bits(gb, 1))
735 *coeff = -1;
736 else
737 *coeff = 1;
738 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
739 break;
740
741 case 28:
742 debug_token("DCT_RUN_CATEGORY1B, ");
743 if (get_bits(gb, 1))
744 *coeff = -1;
745 else
746 *coeff = 1;
747 *zero_run = 6 + get_bits(gb, 2);
748 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
749 break;
750
751 case 29:
752 debug_token("DCT_RUN_CATEGORY1C, ");
753 if (get_bits(gb, 1))
754 *coeff = -1;
755 else
756 *coeff = 1;
757 *zero_run = 10 + get_bits(gb, 3);
758 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
759 break;
760
761 case 30:
762 debug_token("DCT_RUN_CATEGORY2, ");
763 sign = get_bits(gb, 1);
764 *coeff = 2 + get_bits(gb, 1);
765 if (sign)
766 *coeff = -(*coeff);
767 *zero_run = 1;
768 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
769 break;
770
771 case 31:
772 debug_token("DCT_RUN_CATEGORY2, ");
773 sign = get_bits(gb, 1);
774 *coeff = 2 + get_bits(gb, 1);
775 if (sign)
776 *coeff = -(*coeff);
777 *zero_run = 2 + get_bits(gb, 1);
778 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
779 break;
780
781 default:
782 printf (" vp3: help! Got a bad token: %d > 31\n", token);
783 break;
784
785 }
786}
787
788/*
789 * This function wipes out all of the fragment data.
790 */
791static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
792{
793 int i;
794
795 /* zero out all of the fragment information */
796 s->coded_fragment_list_index = 0;
797 for (i = 0; i < s->fragment_count; i++) {
798 memset(s->all_fragments[i].coeffs, 0, 64 * sizeof(DCTELEM));
799 s->all_fragments[i].coeff_count = 0;
800 s->all_fragments[i].last_coeff = 0;
801 }
802}
803
804/*
805 * This function sets of the dequantization tables used for a particular
806 * frame.
807 */
808static void init_dequantizer(Vp3DecodeContext *s)
809{
810
811 int quality_scale = vp31_quality_threshold[s->quality_index];
812 int dc_scale_factor = vp31_dc_scale_factor[s->quality_index];
813 int i, j;
814
815 debug_vp3(" vp3: initializing dequantization tables\n");
816
817 /*
818 * Scale dequantizers:
819 *
820 * quantizer * sf
821 * --------------
822 * 100
823 *
824 * where sf = dc_scale_factor for DC quantizer
825 * or quality_scale for AC quantizer
826 *
827 * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL.
828 */
829#define SCALER 1
830
831 /* scale DC quantizers */
832 s->intra_y_dequant[0] = vp31_intra_y_dequant[0] * dc_scale_factor / 100;
833 if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2)
834 s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2;
835 s->intra_y_dequant[0] *= SCALER;
836
837 s->intra_c_dequant[0] = vp31_intra_c_dequant[0] * dc_scale_factor / 100;
838 if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2)
839 s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2;
840 s->intra_c_dequant[0] *= SCALER;
841
842 s->inter_dequant[0] = vp31_inter_dequant[0] * dc_scale_factor / 100;
843 if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4)
844 s->inter_dequant[0] = MIN_DEQUANT_VAL * 4;
845 s->inter_dequant[0] *= SCALER;
846
847 /* scale AC quantizers, zigzag at the same time in preparation for
848 * the dequantization phase */
849 for (i = 1; i < 64; i++) {
850
851 j = quant_index[i];
852
853 s->intra_y_dequant[j] = vp31_intra_y_dequant[i] * quality_scale / 100;
854 if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL)
855 s->intra_y_dequant[j] = MIN_DEQUANT_VAL;
856 s->intra_y_dequant[j] *= SCALER;
857
858 s->intra_c_dequant[j] = vp31_intra_c_dequant[i] * quality_scale / 100;
859 if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL)
860 s->intra_c_dequant[j] = MIN_DEQUANT_VAL;
861 s->intra_c_dequant[j] *= SCALER;
862
863 s->inter_dequant[j] = vp31_inter_dequant[i] * quality_scale / 100;
864 if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2)
865 s->inter_dequant[j] = MIN_DEQUANT_VAL * 2;
866 s->inter_dequant[j] *= SCALER;
867 }
868
869 /* print debug information as requested */
870 debug_dequantizers("intra Y dequantizers:\n");
871 for (i = 0; i < 8; i++) {
872 for (j = i * 8; j < i * 8 + 8; j++) {
873 debug_dequantizers(" %4d,", s->intra_y_dequant[j]);
874 }
875 debug_dequantizers("\n");
876 }
877 debug_dequantizers("\n");
878
879 debug_dequantizers("intra C dequantizers:\n");
880 for (i = 0; i < 8; i++) {
881 for (j = i * 8; j < i * 8 + 8; j++) {
882 debug_dequantizers(" %4d,", s->intra_c_dequant[j]);
883 }
884 debug_dequantizers("\n");
885 }
886 debug_dequantizers("\n");
887
888 debug_dequantizers("interframe dequantizers:\n");
889 for (i = 0; i < 8; i++) {
890 for (j = i * 8; j < i * 8 + 8; j++) {
891 debug_dequantizers(" %4d,", s->inter_dequant[j]);
892 }
893 debug_dequantizers("\n");
894 }
895 debug_dequantizers("\n");
896}
897
898/*
899 * This function is used to fetch runs of 1s or 0s from the bitstream for
900 * use in determining which superblocks are fully and partially coded.
901 *
902 * Codeword RunLength
903 * 0 1
904 * 10x 2-3
905 * 110x 4-5
906 * 1110xx 6-9
907 * 11110xxx 10-17
908 * 111110xxxx 18-33
909 * 111111xxxxxxxxxxxx 34-4129
910 */
911static int get_superblock_run_length(GetBitContext *gb)
912{
913
914 if (get_bits(gb, 1) == 0)
915 return 1;
916
917 else if (get_bits(gb, 1) == 0)
918 return (2 + get_bits(gb, 1));
919
920 else if (get_bits(gb, 1) == 0)
921 return (4 + get_bits(gb, 1));
922
923 else if (get_bits(gb, 1) == 0)
924 return (6 + get_bits(gb, 2));
925
926 else if (get_bits(gb, 1) == 0)
927 return (10 + get_bits(gb, 3));
928
929 else if (get_bits(gb, 1) == 0)
930 return (18 + get_bits(gb, 4));
931
932 else
933 return (34 + get_bits(gb, 12));
934
935}
936
937/*
938 * This function is used to fetch runs of 1s or 0s from the bitstream for
939 * use in determining which particular fragments are coded.
940 *
941 * Codeword RunLength
942 * 0x 1-2
943 * 10x 3-4
944 * 110x 5-6
945 * 1110xx 7-10
946 * 11110xx 11-14
947 * 11111xxxx 15-30
948 */
949static int get_fragment_run_length(GetBitContext *gb)
950{
951
952 if (get_bits(gb, 1) == 0)
953 return (1 + get_bits(gb, 1));
954
955 else if (get_bits(gb, 1) == 0)
956 return (3 + get_bits(gb, 1));
957
958 else if (get_bits(gb, 1) == 0)
959 return (5 + get_bits(gb, 1));
960
961 else if (get_bits(gb, 1) == 0)
962 return (7 + get_bits(gb, 2));
963
964 else if (get_bits(gb, 1) == 0)
965 return (11 + get_bits(gb, 2));
966
967 else
968 return (15 + get_bits(gb, 4));
969
970}
971
972/*
973 * This function decodes a VLC from the bitstream and returns a number
974 * that ranges from 0..7. The number indicates which of the 8 coding
975 * modes to use.
976 *
977 * VLC Number
978 * 0 0
979 * 10 1
980 * 110 2
981 * 1110 3
982 * 11110 4
983 * 111110 5
984 * 1111110 6
985 * 1111111 7
986 *
987 */
988static int get_mode_code(GetBitContext *gb)
989{
990
991 if (get_bits(gb, 1) == 0)
992 return 0;
993
994 else if (get_bits(gb, 1) == 0)
995 return 1;
996
997 else if (get_bits(gb, 1) == 0)
998 return 2;
999
1000 else if (get_bits(gb, 1) == 0)
1001 return 3;
1002
1003 else if (get_bits(gb, 1) == 0)
1004 return 4;
1005
1006 else if (get_bits(gb, 1) == 0)
1007 return 5;
1008
1009 else if (get_bits(gb, 1) == 0)
1010 return 6;
1011
1012 else
1013 return 7;
1014
1015}
1016
1017/*
1018 * This function extracts a motion vector from the bitstream using a VLC
1019 * scheme. 3 bits are fetched from the bitstream and 1 of 8 actions is
1020 * taken depending on the value on those 3 bits:
1021 *
1022 * 0: return 0
1023 * 1: return 1
1024 * 2: return -1
1025 * 3: if (next bit is 1) return -2, else return 2
1026 * 4: if (next bit is 1) return -3, else return 3
1027 * 5: return 4 + (next 2 bits), next bit is sign
1028 * 6: return 8 + (next 3 bits), next bit is sign
1029 * 7: return 16 + (next 4 bits), next bit is sign
1030 */
1031static int get_motion_vector_vlc(GetBitContext *gb)
1032{
1033 int bits;
1034
1035 bits = get_bits(gb, 3);
1036
1037 switch(bits) {
1038
1039 case 0:
1040 bits = 0;
1041 break;
1042
1043 case 1:
1044 bits = 1;
1045 break;
1046
1047 case 2:
1048 bits = -1;
1049 break;
1050
1051 case 3:
1052 if (get_bits(gb, 1) == 0)
1053 bits = 2;
1054 else
1055 bits = -2;
1056 break;
1057
1058 case 4:
1059 if (get_bits(gb, 1) == 0)
1060 bits = 3;
1061 else
1062 bits = -3;
1063 break;
1064
1065 case 5:
1066 bits = 4 + get_bits(gb, 2);
1067 if (get_bits(gb, 1) == 1)
1068 bits = -bits;
1069 break;
1070
1071 case 6:
1072 bits = 8 + get_bits(gb, 3);
1073 if (get_bits(gb, 1) == 1)
1074 bits = -bits;
1075 break;
1076
1077 case 7:
1078 bits = 16 + get_bits(gb, 4);
1079 if (get_bits(gb, 1) == 1)
1080 bits = -bits;
1081 break;
1082
1083 }
1084
1085 return bits;
1086}
1087
1088/*
1089 * This function fetches a 5-bit number from the stream followed by
1090 * a sign and calls it a motion vector.
1091 */
1092static int get_motion_vector_fixed(GetBitContext *gb)
1093{
1094
1095 int bits;
1096
1097 bits = get_bits(gb, 5);
1098
1099 if (get_bits(gb, 1) == 1)
1100 bits = -bits;
1101
1102 return bits;
1103}
1104
1105/*
1106 * This function unpacks all of the superblock/macroblock/fragment coding
1107 * information from the bitstream.
1108 */
1109static void unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
1110{
1111 int bit = 0;
1112 int current_superblock = 0;
1113 int current_run = 0;
1114 int decode_fully_flags = 0;
1115 int decode_partial_blocks = 0;
1116
1117 int i, j;
1118 int current_fragment;
1119
1120 debug_vp3(" vp3: unpacking superblock coding\n");
1121
1122 if (s->keyframe) {
1123
1124 debug_vp3(" keyframe-- all superblocks are fully coded\n");
1125 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
1126
1127 } else {
1128
1129 /* unpack the list of partially-coded superblocks */
1130 bit = get_bits(gb, 1);
1131 /* toggle the bit because as soon as the first run length is
1132 * fetched the bit will be toggled again */
1133 bit ^= 1;
1134 while (current_superblock < s->superblock_count) {
1135 if (current_run == 0) {
1136 bit ^= 1;
1137 current_run = get_superblock_run_length(gb);
1138 debug_block_coding(" setting superblocks %d..%d to %s\n",
1139 current_superblock,
1140 current_superblock + current_run - 1,
1141 (bit) ? "partially coded" : "not coded");
1142
1143 /* if any of the superblocks are not partially coded, flag
1144 * a boolean to decode the list of fully-coded superblocks */
1145 if (bit == 0)
1146 decode_fully_flags = 1;
1147 } else {
1148
1149 /* make a note of the fact that there are partially coded
1150 * superblocks */
1151 decode_partial_blocks = 1;
1152
1153 }
1154 s->superblock_coding[current_superblock++] =
1155 (bit) ? SB_PARTIALLY_CODED : SB_NOT_CODED;
1156 current_run--;
1157 }
1158
1159 /* unpack the list of fully coded superblocks if any of the blocks were
1160 * not marked as partially coded in the previous step */
1161 if (decode_fully_flags) {
1162
1163 current_superblock = 0;
1164 current_run = 0;
1165 bit = get_bits(gb, 1);
1166 /* toggle the bit because as soon as the first run length is
1167 * fetched the bit will be toggled again */
1168 bit ^= 1;
1169 while (current_superblock < s->superblock_count) {
1170
1171 /* skip any superblocks already marked as partially coded */
1172 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
1173
1174 if (current_run == 0) {
1175 bit ^= 1;
1176 current_run = get_superblock_run_length(gb);
1177 }
1178
1179 debug_block_coding(" setting superblock %d to %s\n",
1180 current_superblock,
1181 (bit) ? "fully coded" : "not coded");
1182 s->superblock_coding[current_superblock] =
1183 (bit) ? SB_FULLY_CODED : SB_NOT_CODED;
1184 current_run--;
1185 }
1186 current_superblock++;
1187 }
1188 }
1189
1190 /* if there were partial blocks, initialize bitstream for
1191 * unpacking fragment codings */
1192 if (decode_partial_blocks) {
1193
1194 current_run = 0;
1195 bit = get_bits(gb, 1);
1196 /* toggle the bit because as soon as the first run length is
1197 * fetched the bit will be toggled again */
1198 bit ^= 1;
1199 }
1200 }
1201
1202 /* figure out which fragments are coded; iterate through each
1203 * superblock (all planes) */
1204 s->coded_fragment_list_index = 0;
04331882
MM
1205 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
1206 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
d86053a4
MM
1207 memset(s->macroblock_coded, 0, s->macroblock_count);
1208 for (i = 0; i < s->superblock_count; i++) {
1209
1210 /* iterate through all 16 fragments in a superblock */
1211 for (j = 0; j < 16; j++) {
1212
1213 /* if the fragment is in bounds, check its coding status */
1214 current_fragment = s->superblock_fragments[i * 16 + j];
1215 if (current_fragment != -1) {
1216 if (s->superblock_coding[i] == SB_NOT_CODED) {
1217
1218 /* copy all the fragments from the prior frame */
1219 s->all_fragments[current_fragment].coding_method =
1220 MODE_COPY;
1221
1222 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
1223
1224 /* fragment may or may not be coded; this is the case
1225 * that cares about the fragment coding runs */
1226 if (current_run == 0) {
1227 bit ^= 1;
1228 current_run = get_fragment_run_length(gb);
1229 }
1230
1231 if (bit) {
1232 /* mode will be decoded in the next phase */
1233 s->all_fragments[current_fragment].coding_method =
1234 MODE_INTER_NO_MV;
04331882 1235 s->coded_fragment_list[s->coded_fragment_list_index] =
d86053a4 1236 current_fragment;
04331882
MM
1237 if ((current_fragment >= s->u_fragment_start) &&
1238 (s->last_coded_y_fragment == -1)) {
1239 s->first_coded_c_fragment = s->coded_fragment_list_index;
1240 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1241 }
1242 s->coded_fragment_list_index++;
d86053a4
MM
1243 s->macroblock_coded[s->all_fragments[current_fragment].macroblock] = 1;
1244 debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
1245 i, current_fragment);
1246 } else {
1247 /* not coded; copy this fragment from the prior frame */
1248 s->all_fragments[current_fragment].coding_method =
1249 MODE_COPY;
1250 debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
1251 i, current_fragment);
1252 }
1253
1254 current_run--;
1255
1256 } else {
1257
1258 /* fragments are fully coded in this superblock; actual
1259 * coding will be determined in next step */
1260 s->all_fragments[current_fragment].coding_method =
1261 MODE_INTER_NO_MV;
04331882 1262 s->coded_fragment_list[s->coded_fragment_list_index] =
d86053a4 1263 current_fragment;
04331882
MM
1264 if ((current_fragment >= s->u_fragment_start) &&
1265 (s->last_coded_y_fragment == -1)) {
1266 s->first_coded_c_fragment = s->coded_fragment_list_index;
1267 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1268 }
1269 s->coded_fragment_list_index++;
d86053a4
MM
1270 s->macroblock_coded[s->all_fragments[current_fragment].macroblock] = 1;
1271 debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
1272 i, current_fragment);
1273 }
1274 }
1275 }
1276 }
04331882
MM
1277
1278 if (s->first_coded_c_fragment == 0)
1279 /* no C fragments coded */
1280 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
1281 else
1282 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
1283 debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
1284 s->coded_fragment_list_index,
1285 s->first_coded_y_fragment,
1286 s->last_coded_y_fragment,
1287 s->first_coded_c_fragment,
1288 s->last_coded_c_fragment);
d86053a4
MM
1289}
1290
1291/*
1292 * This function unpacks all the coding mode data for individual macroblocks
1293 * from the bitstream.
1294 */
1295static void unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
1296{
1297 int i, j, k;
1298 int scheme;
1299 int current_macroblock;
1300 int current_fragment;
1301 int coding_mode;
1302
1303 debug_vp3(" vp3: unpacking encoding modes\n");
1304
1305 if (s->keyframe) {
1306 debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
1307
1308 for (i = 0; i < s->fragment_count; i++)
1309 s->all_fragments[i].coding_method = MODE_INTRA;
1310
1311 } else {
1312
1313 /* fetch the mode coding scheme for this frame */
1314 scheme = get_bits(gb, 3);
1315 debug_modes(" using mode alphabet %d\n", scheme);
1316
1317 /* is it a custom coding scheme? */
1318 if (scheme == 0) {
1319 debug_modes(" custom mode alphabet ahead:\n");
1320 for (i = 0; i < 8; i++)
a466e345 1321 ModeAlphabet[scheme][get_bits(gb, 3)] = i;
d86053a4
MM
1322 }
1323
1324 for (i = 0; i < 8; i++)
1325 debug_modes(" mode[%d][%d] = %d\n", scheme, i,
1326 ModeAlphabet[scheme][i]);
1327
1328 /* iterate through all of the macroblocks that contain 1 or more
1329 * coded fragments */
1330 for (i = 0; i < s->u_superblock_start; i++) {
1331
1332 for (j = 0; j < 4; j++) {
1333 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1334 if ((current_macroblock == -1) ||
1335 (!s->macroblock_coded[current_macroblock]))
1336 continue;
1337
1338 /* mode 7 means get 3 bits for each coding mode */
1339 if (scheme == 7)
1340 coding_mode = get_bits(gb, 3);
1341 else
1342 coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
1343
1344 for (k = 0; k < 6; k++) {
1345 current_fragment =
1346 s->macroblock_fragments[current_macroblock * 6 + k];
1347 if (s->all_fragments[current_fragment].coding_method !=
1348 MODE_COPY)
1349 s->all_fragments[current_fragment].coding_method =
1350 coding_mode;
1351 }
1352
1353 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
1354 s->macroblock_fragments[current_macroblock * 6], coding_mode);
1355 }
1356 }
1357 }
44ae98dd
MM
1358}
1359
1360/*
1361 * This function adjusts the components of a motion vector for the halfpel
1362 * motion grid. c_plane indicates whether the vector applies to the U or V
1363 * plane. The function returns the halfpel function index to be used in
1364 * ffmpeg's put_pixels[]() array of functions.
1365 */
1366static inline int adjust_vector(int *x, int *y, int c_plane)
1367{
1368 int motion_halfpel_index = 0;
1369 int x_halfpel;
1370 int y_halfpel;
1371
1372 if (!c_plane) {
1373
1374 x_halfpel = *x & 1;
1375 motion_halfpel_index |= x_halfpel;
1376 if (*x >= 0)
1377 *x >>= 1;
1378 else
1379 *x = -( (-(*x) >> 1) + x_halfpel);
1380
1381 y_halfpel = *y & 1;
1382 motion_halfpel_index |= (y_halfpel << 1);
1383 if (*y >= 0)
1384 *y >>= 1;
1385 else
1386 *y = -( (-(*y) >> 1) + y_halfpel);
1387
1388 } else {
1389
1390 x_halfpel = ((*x & 0x03) != 0);
1391 motion_halfpel_index |= x_halfpel;
1392 if (*x >= 0)
1393 *x >>= 2;
1394 else
1395 *x = -( (-(*x) >> 2) + x_halfpel);
1396
1397 y_halfpel = ((*y & 0x03) != 0);
1398 motion_halfpel_index |= (y_halfpel << 1);
1399 if (*y >= 0)
1400 *y >>= 2;
1401 else
1402 *y = -( (-(*y) >> 2) + y_halfpel);
1403
1404 }
d86053a4 1405
44ae98dd 1406 return motion_halfpel_index;
d86053a4
MM
1407}
1408
1409/*
1410 * This function unpacks all the motion vectors for the individual
1411 * macroblocks from the bitstream.
1412 */
1413static void unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
1414{
1415 int i, j, k;
1416 int coding_mode;
1417 int motion_x[6];
1418 int motion_y[6];
1419 int last_motion_x = 0;
1420 int last_motion_y = 0;
1421 int prior_last_motion_x = 0;
1422 int prior_last_motion_y = 0;
1423 int current_macroblock;
1424 int current_fragment;
1425
1426 debug_vp3(" vp3: unpacking motion vectors\n");
1427
1428 if (s->keyframe) {
1429
1430 debug_vp3(" keyframe-- there are no motion vectors\n");
1431
1432 } else {
1433
1434 memset(motion_x, 0, 6 * sizeof(int));
1435 memset(motion_y, 0, 6 * sizeof(int));
1436
1437 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
1438 coding_mode = get_bits(gb, 1);
1439 debug_vectors(" using %s scheme for unpacking motion vectors\n",
1440 (coding_mode == 0) ? "VLC" : "fixed-length");
1441
1442 /* iterate through all of the macroblocks that contain 1 or more
1443 * coded fragments */
1444 for (i = 0; i < s->u_superblock_start; i++) {
1445
1446 for (j = 0; j < 4; j++) {
1447 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1448 if ((current_macroblock == -1) ||
1449 (!s->macroblock_coded[current_macroblock]))
1450 continue;
1451
1452 current_fragment = s->macroblock_fragments[current_macroblock * 6];
1453 switch (s->all_fragments[current_fragment].coding_method) {
1454
1455 case MODE_INTER_PLUS_MV:
1456 case MODE_GOLDEN_MV:
1457 /* all 6 fragments use the same motion vector */
1458 if (coding_mode == 0) {
1459 motion_x[0] = get_motion_vector_vlc(gb);
1460 motion_y[0] = get_motion_vector_vlc(gb);
1461 } else {
1462 motion_x[0] = get_motion_vector_fixed(gb);
1463 motion_y[0] = get_motion_vector_fixed(gb);
1464 }
1465 for (k = 1; k < 6; k++) {
1466 motion_x[k] = motion_x[0];
1467 motion_y[k] = motion_y[0];
1468 }
1469
1470 /* vector maintenance, only on MODE_INTER_PLUS_MV */
1471 if (s->all_fragments[current_fragment].coding_method ==
1472 MODE_INTER_PLUS_MV) {
1473 prior_last_motion_x = last_motion_x;
1474 prior_last_motion_y = last_motion_y;
1475 last_motion_x = motion_x[0];
1476 last_motion_y = motion_y[0];
1477 }
1478 break;
1479
1480 case MODE_INTER_FOURMV:
1481 /* fetch 4 vectors from the bitstream, one for each
1482 * Y fragment, then average for the C fragment vectors */
1483 motion_x[4] = motion_y[4] = 0;
1484 for (k = 0; k < 4; k++) {
1485 if (coding_mode == 0) {
1486 motion_x[k] = get_motion_vector_vlc(gb);
1487 motion_y[k] = get_motion_vector_vlc(gb);
1488 } else {
1489 motion_x[k] = get_motion_vector_fixed(gb);
1490 motion_y[k] = get_motion_vector_fixed(gb);
1491 }
1492 motion_x[4] += motion_x[k];
1493 motion_y[4] += motion_y[k];
1494 }
1495
1496 if (motion_x[4] >= 0)
1497 motion_x[4] = (motion_x[4] + 2) / 4;
1498 else
1499 motion_x[4] = (motion_x[4] - 2) / 4;
1500 motion_x[5] = motion_x[4];
1501
1502 if (motion_y[4] >= 0)
1503 motion_y[4] = (motion_y[4] + 2) / 4;
1504 else
1505 motion_y[4] = (motion_y[4] - 2) / 4;
1506 motion_y[5] = motion_y[4];
1507
1508 /* vector maintenance; vector[3] is treated as the
1509 * last vector in this case */
1510 prior_last_motion_x = last_motion_x;
1511 prior_last_motion_y = last_motion_y;
1512 last_motion_x = motion_x[3];
1513 last_motion_y = motion_y[3];
1514 break;
1515
1516 case MODE_INTER_LAST_MV:
1517 /* all 6 fragments use the last motion vector */
1518 motion_x[0] = last_motion_x;
1519 motion_y[0] = last_motion_y;
1520 for (k = 1; k < 6; k++) {
1521 motion_x[k] = motion_x[0];
1522 motion_y[k] = motion_y[0];
1523 }
1524
1525 /* no vector maintenance (last vector remains the
1526 * last vector) */
1527 break;
1528
1529 case MODE_INTER_PRIOR_LAST:
1530 /* all 6 fragments use the motion vector prior to the
1531 * last motion vector */
1532 motion_x[0] = prior_last_motion_x;
1533 motion_y[0] = prior_last_motion_y;
1534 for (k = 1; k < 6; k++) {
1535 motion_x[k] = motion_x[0];
1536 motion_y[k] = motion_y[0];
1537 }
1538
1539 /* vector maintenance */
1540 prior_last_motion_x = last_motion_x;
1541 prior_last_motion_y = last_motion_y;
1542 last_motion_x = motion_x[0];
1543 last_motion_y = motion_y[0];
1544 break;
44ae98dd
MM
1545
1546 default:
1547 /* covers intra, inter without MV, golden without MV */
1548 memset(motion_x, 0, 6 * sizeof(int));
1549 memset(motion_y, 0, 6 * sizeof(int));
1550
1551 /* no vector maintenance */
1552 break;
d86053a4
MM
1553 }
1554
1555 /* assign the motion vectors to the correct fragments */
1556 debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
1557 current_fragment,
1558 s->all_fragments[current_fragment].coding_method);
1559 for (k = 0; k < 6; k++) {
1560 current_fragment =
1561 s->macroblock_fragments[current_macroblock * 6 + k];
44ae98dd
MM
1562 s->all_fragments[current_fragment].motion_halfpel_index =
1563 adjust_vector(&motion_x[k], &motion_y[k],
1564 ((k == 4) || (k == 5)));
d86053a4 1565 s->all_fragments[current_fragment].motion_x = motion_x[k];
44ae98dd
MM
1566 s->all_fragments[current_fragment].motion_y = motion_y[k];
1567 debug_vectors(" vector %d: fragment %d = (%d, %d), index %d\n",
1568 k, current_fragment, motion_x[k], motion_y[k],
1569 s->all_fragments[current_fragment].motion_halfpel_index);
d86053a4
MM
1570 }
1571 }
1572 }
1573 }
1574}
1575
1576/*
1577 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1578 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1579 * data. This function unpacks all the VLCs for either the Y plane or both
1580 * C planes, and is called for DC coefficients or different AC coefficient
1581 * levels (since different coefficient types require different VLC tables.
1582 *
1583 * This function returns a residual eob run. E.g, if a particular token gave
1584 * instructions to EOB the next 5 fragments and there were only 2 fragments
1585 * left in the current fragment range, 3 would be returned so that it could
1586 * be passed into the next call to this same function.
1587 */
1588static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1589 VLC *table, int coeff_index,
1590 int first_fragment, int last_fragment,
1591 int eob_run)
1592{
1593 int i;
1594 int token;
1595 int zero_run;
1596 DCTELEM coeff;
1597 Vp3Fragment *fragment;
1598
04331882 1599 for (i = first_fragment; i <= last_fragment; i++) {
d86053a4
MM
1600
1601 fragment = &s->all_fragments[s->coded_fragment_list[i]];
1602 if (fragment->coeff_count > coeff_index)
1603 continue;
1604
1605 if (!eob_run) {
1606 /* decode a VLC into a token */
1607 token = get_vlc2(gb, table->table, 5, 3);
1608 debug_vlc(" token = %2d, ", token);
1609 /* use the token to get a zero run, a coefficient, and an eob run */
1610 unpack_token(gb, token, &zero_run, &coeff, &eob_run);
1611 }
1612
1613 if (!eob_run) {
1614 fragment->coeff_count += zero_run;
1615 if (fragment->coeff_count < 64)
1616 fragment->coeffs[fragment->coeff_count++] = coeff;
1617 debug_vlc(" fragment %d coeff = %d\n",
1618 s->coded_fragment_list[i], fragment->coeffs[coeff_index]);
1619 } else {
1620 fragment->last_coeff = fragment->coeff_count;
1621 fragment->coeff_count = 64;
1622 debug_vlc(" fragment %d eob with %d coefficients\n",
1623 s->coded_fragment_list[i], fragment->last_coeff);
1624 eob_run--;
1625 }
1626 }
1627
1628 return eob_run;
1629}
1630
1631/*
1632 * This function unpacks all of the DCT coefficient data from the
1633 * bitstream.
1634 */
1635static void unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1636{
1637 int i;
1638 int dc_y_table;
1639 int dc_c_table;
1640 int ac_y_table;
1641 int ac_c_table;
1642 int residual_eob_run = 0;
1643
d86053a4
MM
1644 /* fetch the DC table indices */
1645 dc_y_table = get_bits(gb, 4);
1646 dc_c_table = get_bits(gb, 4);
1647
1648 /* unpack the Y plane DC coefficients */
1649 debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
1650 dc_y_table);
1651 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
04331882 1652 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
d86053a4
MM
1653
1654 /* unpack the C plane DC coefficients */
1655 debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
1656 dc_c_table);
1657 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
04331882 1658 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
d86053a4 1659
a466e345 1660 /* fetch the AC table indices */
d86053a4
MM
1661 ac_y_table = get_bits(gb, 4);
1662 ac_c_table = get_bits(gb, 4);
1663
a466e345 1664 /* unpack the group 1 AC coefficients (coeffs 1-5) */
d86053a4
MM
1665 for (i = 1; i <= 5; i++) {
1666
1667 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1668 i, ac_y_table);
1669 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
04331882 1670 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
d86053a4
MM
1671
1672 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1673 i, ac_c_table);
1674 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
04331882 1675 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
d86053a4
MM
1676 }
1677
a466e345 1678 /* unpack the group 2 AC coefficients (coeffs 6-14) */
d86053a4
MM
1679 for (i = 6; i <= 14; i++) {
1680
1681 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1682 i, ac_y_table);
1683 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
04331882 1684 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
d86053a4
MM
1685
1686 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1687 i, ac_c_table);
1688 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
04331882 1689 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
d86053a4
MM
1690 }
1691
a466e345 1692 /* unpack the group 3 AC coefficients (coeffs 15-27) */
d86053a4
MM
1693 for (i = 15; i <= 27; i++) {
1694
1695 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1696 i, ac_y_table);
1697 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
04331882 1698 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
d86053a4
MM
1699
1700 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1701 i, ac_c_table);
1702 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
04331882 1703 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
d86053a4
MM
1704 }
1705
a466e345 1706 /* unpack the group 4 AC coefficients (coeffs 28-63) */
d86053a4
MM
1707 for (i = 28; i <= 63; i++) {
1708
1709 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1710 i, ac_y_table);
1711 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
04331882 1712 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
d86053a4
MM
1713
1714 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1715 i, ac_c_table);
1716 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
04331882 1717 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
d86053a4
MM
1718 }
1719}
1720
1721/*
1722 * This function reverses the DC prediction for each coded fragment in
1723 * the frame. Much of this function is adapted directly from the original
1724 * VP3 source code.
1725 */
1726#define COMPATIBLE_FRAME(x) \
1727 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1728#define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
d86053a4
MM
1729static inline int iabs (int x) { return ((x < 0) ? -x : x); }
1730
1731static void reverse_dc_prediction(Vp3DecodeContext *s,
1732 int first_fragment,
1733 int fragment_width,
1734 int fragment_height)
1735{
1736
1737#define PUL 8
1738#define PU 4
1739#define PUR 2
1740#define PL 1
1741
1742 int x, y;
1743 int i = first_fragment;
1744
1745 /*
1746 * Fragment prediction groups:
1747 *
1748 * 32222222226
1749 * 10000000004
1750 * 10000000004
1751 * 10000000004
1752 * 10000000004
1753 *
1754 * Note: Groups 5 and 7 do not exist as it would mean that the
1755 * fragment's x coordinate is both 0 and (width - 1) at the same time.
1756 */
1757 int predictor_group;
1758 short predicted_dc;
1759
1760 /* validity flags for the left, up-left, up, and up-right fragments */
1761 int fl, ful, fu, fur;
1762
1763 /* DC values for the left, up-left, up, and up-right fragments */
1764 int vl, vul, vu, vur;
1765
1766 /* indices for the left, up-left, up, and up-right fragments */
1767 int l, ul, u, ur;
1768
1769 /*
1770 * The 6 fields mean:
1771 * 0: up-left multiplier
1772 * 1: up multiplier
1773 * 2: up-right multiplier
1774 * 3: left multiplier
1775 * 4: mask
1776 * 5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
1777 */
1778 int predictor_transform[16][6] = {
1779 { 0, 0, 0, 0, 0, 0 },
1780 { 0, 0, 0, 1, 0, 0 }, // PL
1781 { 0, 0, 1, 0, 0, 0 }, // PUR
1782 { 0, 0, 53, 75, 127, 7 }, // PUR|PL
1783 { 0, 1, 0, 0, 0, 0 }, // PU
1784 { 0, 1, 0, 1, 1, 1 }, // PU|PL
1785 { 0, 1, 0, 0, 0, 0 }, // PU|PUR
1786 { 0, 0, 53, 75, 127, 7 }, // PU|PUR|PL
1787 { 1, 0, 0, 0, 0, 0 }, // PUL
1788 { 0, 0, 0, 1, 0, 0 }, // PUL|PL
1789 { 1, 0, 1, 0, 1, 1 }, // PUL|PUR
1790 { 0, 0, 53, 75, 127, 7 }, // PUL|PUR|PL
1791 { 0, 1, 0, 0, 0, 0 }, // PUL|PU
1792 {-26, 29, 0, 29, 31, 5 }, // PUL|PU|PL
1793 { 3, 10, 3, 0, 15, 4 }, // PUL|PU|PUR
1794 {-26, 29, 0, 29, 31, 5 } // PUL|PU|PUR|PL
1795 };
1796
1797 /* This table shows which types of blocks can use other blocks for
1798 * prediction. For example, INTRA is the only mode in this table to
1799 * have a frame number of 0. That means INTRA blocks can only predict
1800 * from other INTRA blocks. There are 2 golden frame coding types;
1801 * blocks encoding in these modes can only predict from other blocks
1802 * that were encoded with these 1 of these 2 modes. */
1803 unsigned char compatible_frame[8] = {
1804 1, /* MODE_INTER_NO_MV */
1805 0, /* MODE_INTRA */
1806 1, /* MODE_INTER_PLUS_MV */
1807 1, /* MODE_INTER_LAST_MV */
1808 1, /* MODE_INTER_PRIOR_MV */
1809 2, /* MODE_USING_GOLDEN */
1810 2, /* MODE_GOLDEN_MV */
1811 1 /* MODE_INTER_FOUR_MV */
1812 };
1813 int current_frame_type;
1814
1815 /* there is a last DC predictor for each of the 3 frame types */
1816 short last_dc[3];
1817
1818 int transform = 0;
1819
1820 debug_vp3(" vp3: reversing DC prediction\n");
1821
1822 vul = vu = vur = vl = 0;
1823 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1824
1825 /* for each fragment row... */
1826 for (y = 0; y < fragment_height; y++) {
1827
1828 /* for each fragment in a row... */
1829 for (x = 0; x < fragment_width; x++, i++) {
1830
1831 /* reverse prediction if this block was coded */
1832 if (s->all_fragments[i].coding_method != MODE_COPY) {
1833
1834 current_frame_type =
1835 compatible_frame[s->all_fragments[i].coding_method];
1836 predictor_group = (x == 0) + ((y == 0) << 1) +
1837 ((x + 1 == fragment_width) << 2);
1838 debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
1839 i, predictor_group, s->all_fragments[i].coeffs[0]);
1840
1841 switch (predictor_group) {
1842
1843 case 0:
1844 /* main body of fragments; consider all 4 possible
1845 * fragments for prediction */
1846
1847 /* calculate the indices of the predicting fragments */
1848 ul = i - fragment_width - 1;
1849 u = i - fragment_width;
1850 ur = i - fragment_width + 1;
1851 l = i - 1;
1852
1853 /* fetch the DC values for the predicting fragments */
1854 vul = s->all_fragments[ul].coeffs[0];
1855 vu = s->all_fragments[u].coeffs[0];
1856 vur = s->all_fragments[ur].coeffs[0];
1857 vl = s->all_fragments[l].coeffs[0];
1858
1859 /* figure out which fragments are valid */
1860 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
1861 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1862 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1863 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1864
1865 /* decide which predictor transform to use */
1866 transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
1867
1868 break;
1869
1870 case 1:
1871 /* left column of fragments, not including top corner;
1872 * only consider up and up-right fragments */
1873
1874 /* calculate the indices of the predicting fragments */
1875 u = i - fragment_width;
1876 ur = i - fragment_width + 1;
1877
1878 /* fetch the DC values for the predicting fragments */
1879 vu = s->all_fragments[u].coeffs[0];
1880 vur = s->all_fragments[ur].coeffs[0];
1881
1882 /* figure out which fragments are valid */
1883 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1884 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1885
1886 /* decide which predictor transform to use */
1887 transform = (fu*PU) | (fur*PUR);
1888
1889 break;
1890
1891 case 2:
1892 case 6:
1893 /* top row of fragments, not including top-left frag;
1894 * only consider the left fragment for prediction */
1895
1896 /* calculate the indices of the predicting fragments */
1897 l = i - 1;
1898
1899 /* fetch the DC values for the predicting fragments */
1900 vl = s->all_fragments[l].coeffs[0];
1901
1902 /* figure out which fragments are valid */
1903 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1904
1905 /* decide which predictor transform to use */
1906 transform = (fl*PL);
1907
1908 break;
1909
1910 case 3:
1911 /* top-left fragment */
1912
1913 /* nothing to predict from in this case */
1914 transform = 0;
1915
1916 break;
1917
1918 case 4:
1919 /* right column of fragments, not including top corner;
1920 * consider up-left, up, and left fragments for
1921 * prediction */
1922
1923 /* calculate the indices of the predicting fragments */
1924 ul = i - fragment_width - 1;
1925 u = i - fragment_width;
1926 l = i - 1;
1927
1928 /* fetch the DC values for the predicting fragments */
1929 vul = s->all_fragments[ul].coeffs[0];
1930 vu = s->all_fragments[u].coeffs[0];
1931 vl = s->all_fragments[l].coeffs[0];
1932
1933 /* figure out which fragments are valid */
1934 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
1935 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1936 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1937
1938 /* decide which predictor transform to use */
1939 transform = (fl*PL) | (fu*PU) | (ful*PUL);
1940
1941 break;
1942
1943 }
1944
1945 debug_dc_pred("transform = %d, ", transform);
1946
1947 if (transform == 0) {
1948
1949 /* if there were no fragments to predict from, use last
1950 * DC saved */
1951 s->all_fragments[i].coeffs[0] += last_dc[current_frame_type];
1952 debug_dc_pred("from last DC (%d) = %d\n",
1953 current_frame_type, s->all_fragments[i].coeffs[0]);
1954
1955 } else {
1956
1957 /* apply the appropriate predictor transform */
1958 predicted_dc =
1959 (predictor_transform[transform][0] * vul) +
1960 (predictor_transform[transform][1] * vu) +
1961 (predictor_transform[transform][2] * vur) +
1962 (predictor_transform[transform][3] * vl);
1963
1964 /* if there is a shift value in the transform, add
1965 * the sign bit before the shift */
1966 if (predictor_transform[transform][5] != 0) {
1967 predicted_dc += ((predicted_dc >> 15) &
1968 predictor_transform[transform][4]);
1969 predicted_dc >>= predictor_transform[transform][5];
1970 }
1971
1972 /* check for outranging on the [ul u l] and
1973 * [ul u ur l] predictors */
1974 if ((transform == 13) || (transform == 15)) {
1975 if (iabs(predicted_dc - vu) > 128)
1976 predicted_dc = vu;
1977 else if (iabs(predicted_dc - vl) > 128)
1978 predicted_dc = vl;
1979 else if (iabs(predicted_dc - vul) > 128)
1980 predicted_dc = vul;
1981 }
1982
1983 /* at long last, apply the predictor */
1984 s->all_fragments[i].coeffs[0] += predicted_dc;
1985 debug_dc_pred("from pred DC = %d\n",
1986 s->all_fragments[i].coeffs[0]);
1987 }
1988
1989 /* save the DC */
1990 last_dc[current_frame_type] = s->all_fragments[i].coeffs[0];
1991 }
1992 }
1993 }
1994}
1995
1996/*
1997 * This function performs the final rendering of each fragment's data
1998 * onto the output frame.
1999 */
2000static void render_fragments(Vp3DecodeContext *s,
2001 int first_fragment,
44ae98dd
MM
2002 int width,
2003 int height,
d86053a4
MM
2004 int plane /* 0 = Y, 1 = U, 2 = V */)
2005{
2006 int x, y;
2007 int m, n;
2008 int i = first_fragment;
2009 int j;
2010 int16_t *dequantizer;
2011 DCTELEM dequant_block[64];
2012 unsigned char *output_plane;
2013 unsigned char *last_plane;
2014 unsigned char *golden_plane;
2015 int stride;
44ae98dd 2016 int motion_x, motion_y;
a466e345 2017 int upper_motion_limit, lower_motion_limit;
44ae98dd 2018 int motion_halfpel_index;
4e80eb21 2019 unsigned int motion_source;
d86053a4
MM
2020
2021 debug_vp3(" vp3: rendering final fragments for %s\n",
2022 (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
2023
2024 /* set up plane-specific parameters */
2025 if (plane == 0) {
2026 dequantizer = s->intra_y_dequant;
2027 output_plane = s->current_frame.data[0];
61873c4a
MM
2028 last_plane = s->last_frame.data[0];
2029 golden_plane = s->golden_frame.data[0];
d86053a4 2030 stride = -s->current_frame.linesize[0];
a466e345
MM
2031 upper_motion_limit = 7 * s->current_frame.linesize[0];
2032 lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
d86053a4
MM
2033 } else if (plane == 1) {
2034 dequantizer = s->intra_c_dequant;
2035 output_plane = s->current_frame.data[1];
61873c4a
MM
2036 last_plane = s->last_frame.data[1];
2037 golden_plane = s->golden_frame.data[1];
d86053a4 2038 stride = -s->current_frame.linesize[1];
a466e345
MM
2039 upper_motion_limit = 7 * s->current_frame.linesize[1];
2040 lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
d86053a4
MM
2041 } else {
2042 dequantizer = s->intra_c_dequant;
2043 output_plane = s->current_frame.data[2];
61873c4a
MM
2044 last_plane = s->last_frame.data[2];
2045 golden_plane = s->golden_frame.data[2];
d86053a4 2046 stride = -s->current_frame.linesize[2];
a466e345
MM
2047 upper_motion_limit = 7 * s->current_frame.linesize[2];
2048 lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
d86053a4
MM
2049 }
2050
2051 /* for each fragment row... */
44ae98dd 2052 for (y = 0; y < height; y += 8) {
d86053a4
MM
2053
2054 /* for each fragment in a row... */
44ae98dd 2055 for (x = 0; x < width; x += 8, i++) {
d86053a4
MM
2056
2057 /* transform if this block was coded */
44ae98dd 2058 if (s->all_fragments[i].coding_method != MODE_COPY) {
44ae98dd
MM
2059
2060 /* sort out the motion vector */
4e80eb21
MM
2061 motion_x = s->all_fragments[i].motion_x;
2062 motion_y = s->all_fragments[i].motion_y;
44ae98dd
MM
2063 motion_halfpel_index = s->all_fragments[i].motion_halfpel_index;
2064
4e80eb21
MM
2065 motion_source = s->all_fragments[i].first_pixel;
2066 motion_source += motion_x;
2067 motion_source += (motion_y * stride);
44ae98dd 2068
a466e345
MM
2069 /* if the are any problems with a motion vector, refuse
2070 * to render the block */
2071 if ((motion_source < upper_motion_limit) ||
2072 (motion_source > lower_motion_limit)) {
2073// printf (" vp3: help! motion source (%d) out of range (%d..%d)\n",
2074// motion_source, upper_motion_limit, lower_motion_limit);
2075 }
2076
44ae98dd
MM
2077 /* first, take care of copying a block from either the
2078 * previous or the golden frame */
2079 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2080 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) {
2081
44ae98dd
MM
2082 s->dsp.put_pixels_tab[1][motion_halfpel_index](
2083 output_plane + s->all_fragments[i].first_pixel,
4e80eb21 2084 golden_plane + motion_source,
44ae98dd
MM
2085 stride, 8);
2086
2087 } else
2088 if (s->all_fragments[i].coding_method != MODE_INTRA) {
2089
44ae98dd
MM
2090 s->dsp.put_pixels_tab[1][motion_halfpel_index](
2091 output_plane + s->all_fragments[i].first_pixel,
4e80eb21 2092 last_plane + motion_source,
44ae98dd
MM
2093 stride, 8);
2094 }
2095
d86053a4 2096 /* dequantize the DCT coefficients */
44ae98dd
MM
2097 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
2098 i, s->all_fragments[i].coding_method,
2099 s->all_fragments[i].coeffs[0], dequantizer[0]);
d86053a4
MM
2100 for (j = 0; j < 64; j++)
2101 dequant_block[dequant_index[j]] =
2102 s->all_fragments[i].coeffs[j] *
2103 dequantizer[j];
d86053a4 2104
d86053a4
MM
2105 debug_idct("dequantized block:\n");
2106 for (m = 0; m < 8; m++) {
2107 for (n = 0; n < 8; n++) {
2108 debug_idct(" %5d", dequant_block[m * 8 + n]);
2109 }
2110 debug_idct("\n");
2111 }
2112 debug_idct("\n");
2113
4e80eb21 2114 /* invert DCT and place (or add) in final output */
d86053a4 2115
4e80eb21
MM
2116 if (s->all_fragments[i].coding_method == MODE_INTRA) {
2117 dequant_block[0] += 1024;
44ae98dd
MM
2118 s->dsp.idct_put(
2119 output_plane + s->all_fragments[i].first_pixel,
2120 stride, dequant_block);
4e80eb21
MM
2121 } else {
2122 s->dsp.idct_add(
44ae98dd
MM
2123 output_plane + s->all_fragments[i].first_pixel,
2124 stride, dequant_block);
4e80eb21 2125 }
44ae98dd
MM
2126
2127 debug_idct("block after idct_%s():\n",
2128 (s->all_fragments[i].coding_method == MODE_INTRA)?
2129 "put" : "add");
d86053a4
MM
2130 for (m = 0; m < 8; m++) {
2131 for (n = 0; n < 8; n++) {
44ae98dd
MM
2132 debug_idct(" %3d", *(output_plane +
2133 s->all_fragments[i].first_pixel + (m * stride + n)));
d86053a4
MM
2134 }
2135 debug_idct("\n");
2136 }
2137 debug_idct("\n");
d86053a4
MM
2138
2139 } else {
2140
44ae98dd
MM
2141 /* copy directly from the previous frame */
2142 s->dsp.put_pixels_tab[1][0](
2143 output_plane + s->all_fragments[i].first_pixel,
2144 last_plane + s->all_fragments[i].first_pixel,
2145 stride, 8);
d86053a4
MM
2146
2147 }
2148 }
2149 }
2150
2151 emms_c();
2152
2153}
2154
2155/*
2156 * This function computes the first pixel addresses for each fragment.
2157 * This function needs to be invoked after the first frame is allocated
2158 * so that it has access to the plane strides.
2159 */
2160static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
2161{
2162
2163 int i, x, y;
2164
2165 /* figure out the first pixel addresses for each of the fragments */
2166 /* Y plane */
2167 i = 0;
2168 for (y = s->fragment_height; y > 0; y--) {
2169 for (x = 0; x < s->fragment_width; x++) {
2170 s->all_fragments[i++].first_pixel =
2171 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2172 s->golden_frame.linesize[0] +
2173 x * FRAGMENT_PIXELS;
2174 debug_init(" fragment %d, first pixel @ %d\n",
2175 i-1, s->all_fragments[i-1].first_pixel);
2176 }
2177 }
2178
2179 /* U plane */
2180 i = s->u_fragment_start;
2181 for (y = s->fragment_height / 2; y > 0; y--) {
2182 for (x = 0; x < s->fragment_width / 2; x++) {
2183 s->all_fragments[i++].first_pixel =
2184 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2185 s->golden_frame.linesize[1] +
2186 x * FRAGMENT_PIXELS;
2187 debug_init(" fragment %d, first pixel @ %d\n",
2188 i-1, s->all_fragments[i-1].first_pixel);
2189 }
2190 }
2191
2192 /* V plane */
2193 i = s->v_fragment_start;
2194 for (y = s->fragment_height / 2; y > 0; y--) {
2195 for (x = 0; x < s->fragment_width / 2; x++) {
2196 s->all_fragments[i++].first_pixel =
2197 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2198 s->golden_frame.linesize[2] +
2199 x * FRAGMENT_PIXELS;
2200 debug_init(" fragment %d, first pixel @ %d\n",
2201 i-1, s->all_fragments[i-1].first_pixel);
2202 }
2203 }
2204}
2205
2206/*
2207 * This is the ffmpeg/libavcodec API init function.
2208 */
2209static int vp3_decode_init(AVCodecContext *avctx)
2210{
2211 Vp3DecodeContext *s = avctx->priv_data;
2212 int i;
2213
2214 s->avctx = avctx;
2215 s->width = avctx->width;
2216 s->height = avctx->height;
2217 avctx->pix_fmt = PIX_FMT_YUV420P;
2218 avctx->has_b_frames = 0;
2219 dsputil_init(&s->dsp, avctx);
2220
2221 /* initialize to an impossible value which will force a recalculation
2222 * in the first frame decode */
2223 s->quality_index = -1;
2224
2225 s->superblock_width = (s->width + 31) / 32;
2226 s->superblock_height = (s->height + 31) / 32;
2227 s->superblock_count = s->superblock_width * s->superblock_height * 3 / 2;
2228 s->u_superblock_start = s->superblock_width * s->superblock_height;
2229 s->v_superblock_start = s->superblock_width * s->superblock_height * 5 / 4;
2230 s->superblock_coding = av_malloc(s->superblock_count);
2231
2232 s->macroblock_width = (s->width + 15) / 16;
2233 s->macroblock_height = (s->height + 15) / 16;
2234 s->macroblock_count = s->macroblock_width * s->macroblock_height;
2235
2236 s->fragment_width = s->width / FRAGMENT_PIXELS;
2237 s->fragment_height = s->height / FRAGMENT_PIXELS;
2238
2239 /* fragment count covers all 8x8 blocks for all 3 planes */
2240 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
2241 s->u_fragment_start = s->fragment_width * s->fragment_height;
2242 s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
2243
2244 debug_init(" width: %d x %d\n", s->width, s->height);
2245 debug_init(" superblocks: %d x %d, %d total\n",
2246 s->superblock_width, s->superblock_height, s->superblock_count);
2247 debug_init(" macroblocks: %d x %d, %d total\n",
2248 s->macroblock_width, s->macroblock_height, s->macroblock_count);
2249 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
2250 s->fragment_count,
2251 s->fragment_width,
2252 s->fragment_height,
2253 s->u_fragment_start,
2254 s->v_fragment_start);
2255
2256 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
2257 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
2258 s->pixel_addresses_inited = 0;
2259
2260 /* init VLC tables */
2261 for (i = 0; i < 16; i++) {
2262
2263 /* Dc histograms */
2264 init_vlc(&s->dc_vlc[i], 5, 32,
2265 &dc_bias[i][0][1], 4, 2,
2266 &dc_bias[i][0][0], 4, 2);
2267
a466e345 2268 /* group 1 AC histograms */
d86053a4
MM
2269 init_vlc(&s->ac_vlc_1[i], 5, 32,
2270 &ac_bias_0[i][0][1], 4, 2,
2271 &ac_bias_0[i][0][0], 4, 2);
2272
a466e345 2273 /* group 2 AC histograms */
d86053a4
MM
2274 init_vlc(&s->ac_vlc_2[i], 5, 32,
2275 &ac_bias_1[i][0][1], 4, 2,
2276 &ac_bias_1[i][0][0], 4, 2);
2277
a466e345 2278 /* group 3 AC histograms */
d86053a4
MM
2279 init_vlc(&s->ac_vlc_3[i], 5, 32,
2280 &ac_bias_2[i][0][1], 4, 2,
2281 &ac_bias_2[i][0][0], 4, 2);
2282
a466e345 2283 /* group 4 AC histograms */
d86053a4
MM
2284 init_vlc(&s->ac_vlc_4[i], 5, 32,
2285 &ac_bias_3[i][0][1], 4, 2,
2286 &ac_bias_3[i][0][0], 4, 2);
2287 }
2288
2289 /* build quantization table */
2290 for (i = 0; i < 64; i++)
2291 quant_index[dequant_index[i]] = i;
2292
2293 /* work out the block mapping tables */
2294 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
2295 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
2296 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
2297 s->macroblock_coded = av_malloc(s->macroblock_count + 1);
2298 init_block_mapping(s);
2299
44ae98dd
MM
2300 for (i = 0; i < 3; i++) {
2301 s->current_frame.data[i] = NULL;
2302 s->last_frame.data[i] = NULL;
2303 s->golden_frame.data[i] = NULL;
61873c4a
MM
2304 }
2305
d86053a4
MM
2306 return 0;
2307}
2308
2309/*
2310 * This is the ffmpeg/libavcodec API frame decode function.
2311 */
2312static int vp3_decode_frame(AVCodecContext *avctx,
2313 void *data, int *data_size,
2314 uint8_t *buf, int buf_size)
2315{
2316 Vp3DecodeContext *s = avctx->priv_data;
2317 GetBitContext gb;
2318 static int counter = 0;
2319
2320 *data_size = 0;
2321
2322 init_get_bits(&gb, buf, buf_size * 8);
2323
2324 s->keyframe = get_bits(&gb, 1);
2325 s->keyframe ^= 1;
2326 skip_bits(&gb, 1);
2327 s->last_quality_index = s->quality_index;
2328 s->quality_index = get_bits(&gb, 6);
2329 if (s->quality_index != s->last_quality_index)
2330 init_dequantizer(s);
2331
2332 debug_vp3(" VP3 frame #%d: Q index = %d", counter, s->quality_index);
2333 counter++;
2334
2335 if (s->keyframe) {
2336 /* release the previous golden frame and get a new one */
44ae98dd
MM
2337 if (s->golden_frame.data[0])
2338 avctx->release_buffer(avctx, &s->golden_frame);
2339
2340 /* last frame, if allocated, is hereby invalidated */
2341 if (s->last_frame.data[0])
2342 avctx->release_buffer(avctx, &s->last_frame);
d86053a4
MM
2343
2344 s->golden_frame.reference = 0;
2345 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
2346 printf("vp3: get_buffer() failed\n");
2347 return -1;
2348 }
2349
d86053a4 2350 /* golden frame is also the current frame */
61873c4a 2351 memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
d86053a4
MM
2352
2353 /* time to figure out pixel addresses? */
2354 if (!s->pixel_addresses_inited)
2355 vp3_calculate_pixel_addresses(s);
2356
2357 } else {
2358
2359 /* allocate a new current frame */
2360 s->current_frame.reference = 0;
2361 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
2362 printf("vp3: get_buffer() failed\n");
2363 return -1;
2364 }
2365
2366 }
2367
2368 if (s->keyframe) {
2369 debug_vp3(", keyframe\n");
2370 /* skip the other 2 header bytes for now */
2371 skip_bits(&gb, 16);
2372 } else
2373 debug_vp3("\n");
2374
2375 init_frame(s, &gb);
2376
2377 unpack_superblocks(s, &gb);
2378 unpack_modes(s, &gb);
2379 unpack_vectors(s, &gb);
2380 unpack_dct_coeffs(s, &gb);
2381
2382 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
2383 reverse_dc_prediction(s, s->u_fragment_start,
2384 s->fragment_width / 2, s->fragment_height / 2);
2385 reverse_dc_prediction(s, s->v_fragment_start,
2386 s->fragment_width / 2, s->fragment_height / 2);
2387
44ae98dd
MM
2388 render_fragments(s, 0, s->width, s->height, 0);
2389 render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
2390 render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
d86053a4 2391
d86053a4
MM
2392 *data_size=sizeof(AVFrame);
2393 *(AVFrame*)data= s->current_frame;
2394
44ae98dd
MM
2395 /* release the last frame, if it is allocated and if it is not the
2396 * golden frame */
2397 if ((s->last_frame.data[0]) &&
2398 (s->last_frame.data[0] != s->golden_frame.data[0]))
2399 avctx->release_buffer(avctx, &s->last_frame);
d86053a4 2400
61873c4a
MM
2401 /* shuffle frames (last = current) */
2402 memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
d86053a4
MM
2403
2404 return buf_size;
2405}
2406
2407/*
2408 * This is the ffmpeg/libavcodec API module cleanup function.
2409 */
2410static int vp3_decode_end(AVCodecContext *avctx)
2411{
2412 Vp3DecodeContext *s = avctx->priv_data;
2413
2414 av_free(s->all_fragments);
2415 av_free(s->coded_fragment_list);
2416 av_free(s->superblock_fragments);
2417 av_free(s->superblock_macroblocks);
2418 av_free(s->macroblock_fragments);
2419 av_free(s->macroblock_coded);
2420
2421 /* release all frames */
2422 avctx->release_buffer(avctx, &s->golden_frame);
2423 avctx->release_buffer(avctx, &s->last_frame);
2424 avctx->release_buffer(avctx, &s->current_frame);
2425
2426 return 0;
2427}
2428
2429AVCodec vp3_decoder = {
2430 "vp3",
2431 CODEC_TYPE_VIDEO,
2432 CODEC_ID_VP3,
2433 sizeof(Vp3DecodeContext),
2434 vp3_decode_init,
2435 NULL,
2436 vp3_decode_end,
2437 vp3_decode_frame,
2438 0,
2439 NULL
2440};