FFmpeg
Loading...
Searching...
No Matches
vp3.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2003-2004 The FFmpeg project
3 * Copyright (C) 2019 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * On2 VP3/VP4 Video Decoder
25 *
26 * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
27 * For more information about the VP3 coding process, visit:
28 * http://wiki.multimedia.cx/index.php?title=On2_VP3
29 *
30 * Theora decoder by Alex Beregszaszi
31 */
32
33#include "config_components.h"
34
35#include <stddef.h>
36#include <string.h>
37
39#include "libavutil/imgutils.h"
40#include "libavutil/mem.h"
42#include "libavutil/thread.h"
43
44#include "avcodec.h"
45#include "codec_internal.h"
46#include "decode.h"
47#include "get_bits.h"
48#include "hpeldsp.h"
49#include "jpegquanttables.h"
50#include "mathops.h"
51#include "progressframe.h"
52#include "libavutil/refstruct.h"
53#include "thread.h"
54#include "videodsp.h"
55#include "vp3data.h"
56#include "vp4data.h"
57#include "vp3dsp.h"
58#include "xiph.h"
59
60#define VP3_MV_VLC_BITS 6
61#define VP4_MV_VLC_BITS 6
62#define SUPERBLOCK_VLC_BITS 6
63
64#define FRAGMENT_PIXELS 8
65
66// FIXME split things out into their own arrays
67typedef struct Vp3Fragment {
68 int16_t dc;
70 uint8_t qpi;
72
73#define SB_NOT_CODED 0
74#define SB_PARTIALLY_CODED 1
75#define SB_FULLY_CODED 2
76
77// This is the maximum length of a single long bit run that can be encoded
78// for superblock coding or block qps. Theora special-cases this to read a
79// bit instead of flipping the current bit to allow for runs longer than 4129.
80#define MAXIMUM_LONG_BIT_RUN 4129
81
82#define MODE_INTER_NO_MV 0
83#define MODE_INTRA 1
84#define MODE_INTER_PLUS_MV 2
85#define MODE_INTER_LAST_MV 3
86#define MODE_INTER_PRIOR_LAST 4
87#define MODE_USING_GOLDEN 5
88#define MODE_GOLDEN_MV 6
89#define MODE_INTER_FOURMV 7
90#define CODING_MODE_COUNT 8
91
92/* special internal mode */
93#define MODE_COPY 8
94
97static int theora_reinit(AVCodecContext *avctx, uint8_t *headers,
98 int headers_size);
99
100
101/* There are 6 preset schemes, plus a free-form scheme */
139
140static const uint8_t hilbert_offset[16][2] = {
141 { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
142 { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
143 { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
144 { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
145};
146
147enum {
153};
154
165
166static VLCElem superblock_run_length_vlc[88]; /* version < 2 */
167static VLCElem fragment_run_length_vlc[56]; /* version < 2 */
168static VLCElem motion_vector_vlc[112]; /* version < 2 */
169
170// The VP4 tables reuse this vlc.
171static VLCElem mode_code_vlc[24 + 2108 * CONFIG_VP4_DECODER];
172
173#if CONFIG_VP4_DECODER
174static const VLCElem *vp4_mv_vlc_table[2][7]; /* version >= 2 */
175static const VLCElem *block_pattern_vlc[2]; /* version >= 2 */
176#endif
177
178typedef struct {
179 int dc;
180 int type;
182
183#define MIN_DEQUANT_VAL 2
184
185typedef struct HuffEntry {
186 uint8_t len, sym;
187} HuffEntry;
188
189typedef struct HuffTable {
191 uint8_t nb_entries;
192} HuffTable;
193
194typedef struct CoeffVLCs {
195 const VLCElem *vlc_tabs[80];
197} CoeffVLCs;
198
199typedef struct Vp3DecodeContext {
209 uint8_t idct_permutation[64];
210 uint8_t idct_scantable[64];
214 DECLARE_ALIGNED(16, int16_t, block)[64];
218
219 int qps[3];
220 int nqps;
221
231 unsigned char *superblock_coding;
232
233 int macroblock_count; /* y macroblock count */
239 int yuv_macroblock_count; /* y+u+v macroblock count */
240
244
248 uint8_t offset_x;
249 uint8_t offset_y;
251
252 int8_t (*motion_val[2])[2];
253
254 /* tables */
255 uint16_t coded_dc_scale_factor[2][64];
257 uint8_t base_matrix[384][64];
258 uint8_t qr_count[2][3];
259 uint8_t qr_size[2][3][64];
260 uint16_t qr_base[2][3][64];
261
262 /**
263 * This is a list of all tokens in bitstream order. Reordering takes place
264 * by pulling from each level during IDCT. As a consequence, IDCT must be
265 * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
266 * otherwise. The 32 different tokens with up to 12 bits of extradata are
267 * collapsed into 3 types, packed as follows:
268 * (from the low to high bits)
269 *
270 * 2 bits: type (0,1,2)
271 * 0: EOB run, 14 bits for run length (12 needed)
272 * 1: zero run, 7 bits for run length
273 * 7 bits for the next coefficient (3 needed)
274 * 2: coefficient, 14 bits (11 needed)
275 *
276 * Coefficients are signed, so are packed in the highest bits for automatic
277 * sign extension.
278 */
279 int16_t *dct_tokens[3][64];
281#define TOKEN_EOB(eob_run) ((eob_run) << 2)
282#define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
283#define TOKEN_COEFF(coeff) (((coeff) * 4) + 2)
284
285 /**
286 * number of blocks that contain DCT coefficients at
287 * the given level or higher
288 */
289 int num_coded_frags[3][64];
291
292 /* this is a list of indexes into the all_fragments array indicating
293 * which of the fragments are coded */
295
299
300 /**
301 * The first 16 of the following VLCs are for the dc coefficients;
302 * the others are four groups of 16 VLCs each for ac coefficients.
303 * This is a RefStruct reference to share these VLCs between threads.
304 */
306
307 /**
308 * Headers taken from packet side data, NULL while the codec extradata
309 * applies; a RefStruct reference, padded for the bit reader.
310 */
311 uint8_t *headers;
313
314 /* these arrays need to be on 16-byte boundaries since SSE2 operations
315 * index into them */
316 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
317
318 /* This table contains superblock_count * 16 entries. Each set of 16
319 * numbers corresponds to the fragment indexes 0..15 of the superblock.
320 * An entry will be -1 to indicate that no entry corresponds to that
321 * index. */
323
324 /* This is an array that indicates how a particular macroblock
325 * is coded. */
326 unsigned char *macroblock_coding;
327
329
330 /* Huffman decode */
332
335
336 VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */
338
339/************************************************************************
340 * VP3 specific functions
341 ************************************************************************/
342
344{
345 Vp3DecodeContext *s = avctx->priv_data;
346
347 av_freep(&s->superblock_coding);
348 av_freep(&s->all_fragments);
349 av_freep(&s->nkf_coded_fragment_list);
350 av_freep(&s->kf_coded_fragment_list);
351 av_freep(&s->dct_tokens_base);
352 av_freep(&s->superblock_fragments);
353 av_freep(&s->macroblock_coding);
354 av_freep(&s->dc_pred_row);
355 av_freep(&s->motion_val[0]);
356 av_freep(&s->motion_val[1]);
357}
358
360{
361 Vp3DecodeContext *s = avctx->priv_data;
362
363 ff_progress_frame_unref(&s->golden_frame);
364 ff_progress_frame_unref(&s->last_frame);
365 ff_progress_frame_unref(&s->current_frame);
366}
367
369{
370 Vp3DecodeContext *s = avctx->priv_data;
371
372 free_tables(avctx);
373 av_freep(&s->edge_emu_buffer);
374
375 s->theora_tables = 0;
376
377 /* release all frames */
378 vp3_decode_flush(avctx);
379
380 av_refstruct_unref(&s->coeff_vlc);
381 av_refstruct_unref(&s->headers);
382
383 return 0;
384}
385
386/**
387 * This function sets up all of the various blocks mappings:
388 * superblocks <-> fragments, macroblocks <-> fragments,
389 * superblocks <-> macroblocks
390 *
391 * @return 0 is successful; returns 1 if *anything* went wrong.
392 */
394{
395 int j = 0;
396
397 for (int plane = 0; plane < 3; plane++) {
398 int sb_width = plane ? s->c_superblock_width
399 : s->y_superblock_width;
400 int sb_height = plane ? s->c_superblock_height
401 : s->y_superblock_height;
402 int frag_width = s->fragment_width[!!plane];
403 int frag_height = s->fragment_height[!!plane];
404
405 for (int sb_y = 0; sb_y < sb_height; sb_y++)
406 for (int sb_x = 0; sb_x < sb_width; sb_x++)
407 for (int i = 0; i < 16; i++) {
408 int x = 4 * sb_x + hilbert_offset[i][0];
409 int y = 4 * sb_y + hilbert_offset[i][1];
410
411 if (x < frag_width && y < frag_height)
412 s->superblock_fragments[j++] = s->fragment_start[plane] +
413 y * frag_width + x;
414 else
415 s->superblock_fragments[j++] = -1;
416 }
417 }
418
419 return 0; /* successful path out */
420}
421
422/*
423 * This function sets up the dequantization tables used for a particular
424 * frame.
425 */
426static void init_dequantizer(Vp3DecodeContext *s, int qpi)
427{
428 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
429
430 for (int inter = 0; inter < 2; inter++) {
431 for (int plane = 0; plane < 3; plane++) {
432 int dc_scale_factor = s->coded_dc_scale_factor[!!plane][s->qps[qpi]];
433 int sum = 0, bmi, bmj, qistart, qri;
434 for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
435 sum += s->qr_size[inter][plane][qri];
436 if (s->qps[qpi] <= sum)
437 break;
438 }
439 qistart = sum - s->qr_size[inter][plane][qri];
440 bmi = s->qr_base[inter][plane][qri];
441 bmj = s->qr_base[inter][plane][qri + 1];
442 for (int i = 0; i < 64; i++) {
443 int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] -
444 2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
445 s->qr_size[inter][plane][qri]) /
446 (2 * s->qr_size[inter][plane][qri]);
447
448 int qmin = 8 << (inter + !i);
449 int qscale = i ? ac_scale_factor : dc_scale_factor;
450 int qbias = (1 + inter) * 3;
451 s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
452 (i == 0 || s->version < 2) ? av_clip((qscale * coeff) / 100 * 4, qmin, 4096)
453 : (qscale * (coeff - qbias) / 100 + qbias) * 4;
454 }
455 /* all DC coefficients use the same quant so as not to interfere
456 * with DC prediction */
457 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
458 }
459 }
460}
461
462/*
463 * This function initializes the loop filter boundary limits if the frame's
464 * quality index is different from the previous frame's.
465 *
466 * The filter_limit_values may not be larger than 127.
467 */
469{
470 ff_vp3dsp_set_bounding_values(s->bounding_values_array, s->filter_limit_values[s->qps[0]]);
471}
472
473/*
474 * This function unpacks all of the superblock/macroblock/fragment coding
475 * information from the bitstream.
476 */
478{
479 const int superblock_starts[3] = {
480 0, s->u_superblock_start, s->v_superblock_start
481 };
482 int bit = 0;
483 int current_superblock = 0;
484 int current_run = 0;
485 int num_partial_superblocks = 0;
486
487 int current_fragment;
488 int plane0_num_coded_frags = 0;
489
490 if (s->keyframe) {
491 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
492 } else {
493 /* unpack the list of partially-coded superblocks */
494 bit = get_bits1(gb) ^ 1;
495 current_run = 0;
496
497 while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
498 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
499 bit = get_bits1(gb);
500 else
501 bit ^= 1;
502
503 current_run = get_vlc2(gb, superblock_run_length_vlc,
505 if (current_run == 34)
506 current_run += get_bits(gb, 12);
507
508 if (current_run > s->superblock_count - current_superblock) {
509 av_log(s->avctx, AV_LOG_ERROR,
510 "Invalid partially coded superblock run length\n");
511 return -1;
512 }
513
514 memset(s->superblock_coding + current_superblock, bit, current_run);
515
516 current_superblock += current_run;
517 if (bit)
518 num_partial_superblocks += current_run;
519 }
520
521 /* unpack the list of fully coded superblocks if any of the blocks were
522 * not marked as partially coded in the previous step */
523 if (num_partial_superblocks < s->superblock_count) {
524 int superblocks_decoded = 0;
525
526 current_superblock = 0;
527 bit = get_bits1(gb) ^ 1;
528 current_run = 0;
529
530 while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
531 get_bits_left(gb) > 0) {
532 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
533 bit = get_bits1(gb);
534 else
535 bit ^= 1;
536
537 current_run = get_vlc2(gb, superblock_run_length_vlc,
539 if (current_run == 34)
540 current_run += get_bits(gb, 12);
541
542 for (int j = 0; j < current_run; current_superblock++) {
543 if (current_superblock >= s->superblock_count) {
544 av_log(s->avctx, AV_LOG_ERROR,
545 "Invalid fully coded superblock run length\n");
546 return -1;
547 }
548
549 /* skip any superblocks already marked as partially coded */
550 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
551 s->superblock_coding[current_superblock] = 2 * bit;
552 j++;
553 }
554 }
555 superblocks_decoded += current_run;
556 }
557 }
558
559 /* if there were partial blocks, initialize bitstream for
560 * unpacking fragment codings */
561 if (num_partial_superblocks) {
562 current_run = 0;
563 bit = get_bits1(gb);
564 /* toggle the bit because as soon as the first run length is
565 * fetched the bit will be toggled again */
566 bit ^= 1;
567 }
568 }
569
570 /* figure out which fragments are coded; iterate through each
571 * superblock (all planes) */
572 s->total_num_coded_frags = 0;
573 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
574
575 s->coded_fragment_list[0] = s->keyframe ? s->kf_coded_fragment_list
576 : s->nkf_coded_fragment_list;
577
578 for (int plane = 0; plane < 3; plane++) {
579 int sb_start = superblock_starts[plane];
580 int sb_end = sb_start + (plane ? s->c_superblock_count
581 : s->y_superblock_count);
582 int num_coded_frags = 0;
583
584 if (s->keyframe) {
585 if (s->num_kf_coded_fragment[plane] == -1) {
586 for (int i = sb_start; i < sb_end; i++) {
587 /* iterate through all 16 fragments in a superblock */
588 for (int j = 0; j < 16; j++) {
589 /* if the fragment is in bounds, check its coding status */
590 current_fragment = s->superblock_fragments[i * 16 + j];
591 if (current_fragment != -1) {
592 s->coded_fragment_list[plane][num_coded_frags++] =
593 current_fragment;
594 }
595 }
596 }
597 s->num_kf_coded_fragment[plane] = num_coded_frags;
598 } else
599 num_coded_frags = s->num_kf_coded_fragment[plane];
600 } else {
601 for (int i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
602 if (get_bits_left(gb) < plane0_num_coded_frags >> 2) {
603 return AVERROR_INVALIDDATA;
604 }
605 /* iterate through all 16 fragments in a superblock */
606 for (int j = 0; j < 16; j++) {
607 /* if the fragment is in bounds, check its coding status */
608 current_fragment = s->superblock_fragments[i * 16 + j];
609 if (current_fragment != -1) {
610 int coded = s->superblock_coding[i];
611
612 if (coded == SB_PARTIALLY_CODED) {
613 /* fragment may or may not be coded; this is the case
614 * that cares about the fragment coding runs */
615 if (current_run-- == 0) {
616 bit ^= 1;
617 current_run = get_vlc2(gb, fragment_run_length_vlc, 5, 2);
618 }
619 coded = bit;
620 }
621
622 if (coded) {
623 /* default mode; actual mode will be decoded in
624 * the next phase */
625 s->all_fragments[current_fragment].coding_method =
627 s->coded_fragment_list[plane][num_coded_frags++] =
628 current_fragment;
629 } else {
630 /* not coded; copy this fragment from the prior frame */
631 s->all_fragments[current_fragment].coding_method =
632 MODE_COPY;
633 }
634 }
635 }
636 }
637 }
638 if (!plane)
639 plane0_num_coded_frags = num_coded_frags;
640 s->total_num_coded_frags += num_coded_frags;
641 for (int i = 0; i < 64; i++)
642 s->num_coded_frags[plane][i] = num_coded_frags;
643 if (plane < 2)
644 s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
645 num_coded_frags;
646 }
647 return 0;
648}
649
650#define BLOCK_X (2 * mb_x + (k & 1))
651#define BLOCK_Y (2 * mb_y + (k >> 1))
652
653#if CONFIG_VP4_DECODER
654/**
655 * @return number of blocks, or > yuv_macroblock_count on error.
656 * return value is always >= 1.
657 */
658static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb)
659{
660 int v = 1;
661 int bits;
662 while ((bits = show_bits(gb, 9)) == 0x1ff) {
663 skip_bits(gb, 9);
664 v += 256;
665 if (v > s->yuv_macroblock_count) {
666 av_log(s->avctx, AV_LOG_ERROR, "Invalid run length\n");
667 return v;
668 }
669 }
670#define body(n) { \
671 skip_bits(gb, 2 + n); \
672 v += (1 << n) + get_bits(gb, n); }
673#define thresh(n) (0x200 - (0x80 >> n))
674#define else_if(n) else if (bits < thresh(n)) body(n)
675 if (bits < 0x100) {
676 skip_bits(gb, 1);
677 } else if (bits < thresh(0)) {
678 skip_bits(gb, 2);
679 v += 1;
680 }
681 else_if(1)
682 else_if(2)
683 else_if(3)
684 else_if(4)
685 else_if(5)
686 else_if(6)
687 else body(7)
688#undef body
689#undef thresh
690#undef else_if
691 return v;
692}
693
694static int vp4_get_block_pattern(GetBitContext *gb, int *next_block_pattern_table)
695{
696 int v = get_vlc2(gb, block_pattern_vlc[*next_block_pattern_table], 5, 1);
697 *next_block_pattern_table = vp4_block_pattern_table_selector[v];
698 return v + 1;
699}
700
701static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb)
702{
703 int fragment;
704 int next_block_pattern_table;
705 int bit, current_run, has_partial;
706
707 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
708
709 if (s->keyframe)
710 return 0;
711
712 has_partial = 0;
713 bit = get_bits1(gb);
714 for (int i = 0; i < s->yuv_macroblock_count; i += current_run) {
715 if (get_bits_left(gb) <= 0)
716 return AVERROR_INVALIDDATA;
717 current_run = vp4_get_mb_count(s, gb);
718 if (current_run > s->yuv_macroblock_count - i)
719 return -1;
720 memset(s->superblock_coding + i, 2 * bit, current_run);
721 bit ^= 1;
722 has_partial |= bit;
723 }
724
725 if (has_partial) {
726 if (get_bits_left(gb) <= 0)
727 return AVERROR_INVALIDDATA;
728 bit = get_bits1(gb);
729 current_run = vp4_get_mb_count(s, gb);
730 for (int i = 0; i < s->yuv_macroblock_count; i++) {
731 if (!s->superblock_coding[i]) {
732 if (!current_run) {
733 bit ^= 1;
734 current_run = vp4_get_mb_count(s, gb);
735 }
736 s->superblock_coding[i] = bit;
737 current_run--;
738 }
739 }
740 if (current_run) /* handle situation when vp4_get_mb_count() fails */
741 return -1;
742 }
743
744 next_block_pattern_table = 0;
745 for (int plane = 0, i = 0; plane < 3; plane++) {
746 int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
747 int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
748 int mb_width = plane ? s->c_macroblock_width : s->macroblock_width;
749 int mb_height = plane ? s->c_macroblock_height : s->macroblock_height;
750 int fragment_width = s->fragment_width[!!plane];
751 int fragment_height = s->fragment_height[!!plane];
752
753 for (int sb_y = 0; sb_y < sb_height; sb_y++) {
754 for (int sb_x = 0; sb_x < sb_width; sb_x++) {
755 for (int j = 0; j < 4; j++) {
756 int mb_x = 2 * sb_x + (j >> 1);
757 int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1);
758 int mb_coded, pattern, coded;
759
760 if (mb_x >= mb_width || mb_y >= mb_height)
761 continue;
762
763 mb_coded = s->superblock_coding[i++];
764
765 if (mb_coded == SB_FULLY_CODED)
766 pattern = 0xF;
767 else if (mb_coded == SB_PARTIALLY_CODED)
768 pattern = vp4_get_block_pattern(gb, &next_block_pattern_table);
769 else
770 pattern = 0;
771
772 for (int k = 0; k < 4; k++) {
773 if (BLOCK_X >= fragment_width || BLOCK_Y >= fragment_height)
774 continue;
775 fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X;
776 coded = pattern & (8 >> k);
777 /* MODE_INTER_NO_MV is the default for coded fragments.
778 the actual method is decoded in the next phase. */
779 s->all_fragments[fragment].coding_method = coded ? MODE_INTER_NO_MV : MODE_COPY;
780 }
781 }
782 }
783 }
784 }
785 return 0;
786}
787#endif
788
789/*
790 * This function unpacks all the coding mode data for individual macroblocks
791 * from the bitstream.
792 */
794{
795 int scheme;
796 int current_macroblock;
797 int current_fragment;
798 int coding_mode;
799 int custom_mode_alphabet[CODING_MODE_COUNT];
800 const int *alphabet;
801 Vp3Fragment *frag;
802
803 if (s->keyframe) {
804 for (int i = 0; i < s->fragment_count; i++)
805 s->all_fragments[i].coding_method = MODE_INTRA;
806 } else {
807 /* fetch the mode coding scheme for this frame */
808 scheme = get_bits(gb, 3);
809
810 /* is it a custom coding scheme? */
811 if (scheme == 0) {
812 for (int i = 0; i < 8; i++)
813 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
814 for (int i = 0; i < 8; i++)
815 custom_mode_alphabet[get_bits(gb, 3)] = i;
816 alphabet = custom_mode_alphabet;
817 } else
818 alphabet = ModeAlphabet[scheme - 1];
819
820 /* iterate through all of the macroblocks that contain 1 or more
821 * coded fragments */
822 for (int sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
823 for (int sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
824 if (get_bits_left(gb) <= 0)
825 return -1;
826
827 for (int j = 0; j < 4; j++) {
828 int k;
829 int mb_x = 2 * sb_x + (j >> 1);
830 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
831 current_macroblock = mb_y * s->macroblock_width + mb_x;
832
833 if (mb_x >= s->macroblock_width ||
834 mb_y >= s->macroblock_height)
835 continue;
836
837 /* coding modes are only stored if the macroblock has
838 * at least one luma block coded, otherwise it must be
839 * INTER_NO_MV */
840 for (k = 0; k < 4; k++) {
841 current_fragment = BLOCK_Y *
842 s->fragment_width[0] + BLOCK_X;
843 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
844 break;
845 }
846 if (k == 4) {
847 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
848 continue;
849 }
850
851 /* mode 7 means get 3 bits for each coding mode */
852 if (scheme == 7)
853 coding_mode = get_bits(gb, 3);
854 else
855 coding_mode = alphabet[get_vlc2(gb, mode_code_vlc, 4, 2)];
856
857 s->macroblock_coding[current_macroblock] = coding_mode;
858 for (k = 0; k < 4; k++) {
859 frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
860 if (frag->coding_method != MODE_COPY)
861 frag->coding_method = coding_mode;
862 }
863
864#define SET_CHROMA_MODES \
865 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
866 frag[s->fragment_start[1]].coding_method = coding_mode; \
867 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
868 frag[s->fragment_start[2]].coding_method = coding_mode;
869
870 if (s->chroma_y_shift) {
871 frag = s->all_fragments + mb_y *
872 s->fragment_width[1] + mb_x;
874 } else if (s->chroma_x_shift) {
875 frag = s->all_fragments +
876 2 * mb_y * s->fragment_width[1] + mb_x;
877 for (k = 0; k < 2; k++) {
879 frag += s->fragment_width[1];
880 }
881 } else {
882 for (k = 0; k < 4; k++) {
883 frag = s->all_fragments +
884 BLOCK_Y * s->fragment_width[1] + BLOCK_X;
886 }
887 }
888 }
889 }
890 }
891 }
892
893 return 0;
894}
895
896static int vp4_get_mv(GetBitContext *gb, int axis, int last_motion)
897{
898#if CONFIG_VP4_DECODER
899 int v = get_vlc2(gb, vp4_mv_vlc_table[axis][vp4_mv_table_selector[FFABS(last_motion)]],
900 VP4_MV_VLC_BITS, 2);
901 return last_motion < 0 ? -v : v;
902#else
903 return 0;
904#endif
905}
906
907/*
908 * This function unpacks all the motion vectors for the individual
909 * macroblocks from the bitstream.
910 */
912{
913 int coding_mode;
914 int motion_x[4];
915 int motion_y[4];
916 int last_motion_x = 0;
917 int last_motion_y = 0;
918 int prior_last_motion_x = 0;
919 int prior_last_motion_y = 0;
920 int last_gold_motion_x = 0;
921 int last_gold_motion_y = 0;
922 int current_macroblock;
923 int current_fragment;
924 int frag;
925
926 if (s->keyframe)
927 return 0;
928
929 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme; 2 is VP4 code scheme */
930 coding_mode = s->version < 2 ? get_bits1(gb) : 2;
931
932 /* iterate through all of the macroblocks that contain 1 or more
933 * coded fragments */
934 for (int sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
935 for (int sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
936 if (get_bits_left(gb) <= 0)
937 return -1;
938
939 for (int j = 0; j < 4; j++) {
940 int mb_x = 2 * sb_x + (j >> 1);
941 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
942 current_macroblock = mb_y * s->macroblock_width + mb_x;
943
944 if (mb_x >= s->macroblock_width ||
945 mb_y >= s->macroblock_height ||
946 s->macroblock_coding[current_macroblock] == MODE_COPY)
947 continue;
948
949 switch (s->macroblock_coding[current_macroblock]) {
950 case MODE_GOLDEN_MV:
951 if (coding_mode == 2) { /* VP4 */
952 last_gold_motion_x = motion_x[0] = vp4_get_mv(gb, 0, last_gold_motion_x);
953 last_gold_motion_y = motion_y[0] = vp4_get_mv(gb, 1, last_gold_motion_y);
954 break;
955 }
958 /* all 6 fragments use the same motion vector */
959 if (coding_mode == 0) {
960 motion_x[0] = get_vlc2(gb, motion_vector_vlc,
961 VP3_MV_VLC_BITS, 2);
962 motion_y[0] = get_vlc2(gb, motion_vector_vlc,
963 VP3_MV_VLC_BITS, 2);
964 } else if (coding_mode == 1) {
965 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
966 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
967 } else { /* VP4 */
968 motion_x[0] = vp4_get_mv(gb, 0, last_motion_x);
969 motion_y[0] = vp4_get_mv(gb, 1, last_motion_y);
970 }
971
972 /* vector maintenance, only on MODE_INTER_PLUS_MV */
973 if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
974 prior_last_motion_x = last_motion_x;
975 prior_last_motion_y = last_motion_y;
976 last_motion_x = motion_x[0];
977 last_motion_y = motion_y[0];
978 }
979 break;
980
982 /* vector maintenance */
983 prior_last_motion_x = last_motion_x;
984 prior_last_motion_y = last_motion_y;
985
986 /* fetch 4 vectors from the bitstream, one for each
987 * Y fragment, then average for the C fragment vectors */
988 for (int k = 0; k < 4; k++) {
989 current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
990 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
991 if (coding_mode == 0) {
992 motion_x[k] = get_vlc2(gb, motion_vector_vlc,
993 VP3_MV_VLC_BITS, 2);
994 motion_y[k] = get_vlc2(gb, motion_vector_vlc,
995 VP3_MV_VLC_BITS, 2);
996 } else if (coding_mode == 1) {
997 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
998 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
999 } else { /* VP4 */
1000 motion_x[k] = vp4_get_mv(gb, 0, prior_last_motion_x);
1001 motion_y[k] = vp4_get_mv(gb, 1, prior_last_motion_y);
1002 }
1003 last_motion_x = motion_x[k];
1004 last_motion_y = motion_y[k];
1005 } else {
1006 motion_x[k] = 0;
1007 motion_y[k] = 0;
1008 }
1009 }
1010 break;
1011
1012 case MODE_INTER_LAST_MV:
1013 /* all 6 fragments use the last motion vector */
1014 motion_x[0] = last_motion_x;
1015 motion_y[0] = last_motion_y;
1016
1017 /* no vector maintenance (last vector remains the
1018 * last vector) */
1019 break;
1020
1022 /* all 6 fragments use the motion vector prior to the
1023 * last motion vector */
1024 motion_x[0] = prior_last_motion_x;
1025 motion_y[0] = prior_last_motion_y;
1026
1027 /* vector maintenance */
1028 prior_last_motion_x = last_motion_x;
1029 prior_last_motion_y = last_motion_y;
1030 last_motion_x = motion_x[0];
1031 last_motion_y = motion_y[0];
1032 break;
1033
1034 default:
1035 /* covers intra, inter without MV, golden without MV */
1036 motion_x[0] = 0;
1037 motion_y[0] = 0;
1038
1039 /* no vector maintenance */
1040 break;
1041 }
1042
1043 /* assign the motion vectors to the correct fragments */
1044 for (int k = 0; k < 4; k++) {
1045 current_fragment =
1046 BLOCK_Y * s->fragment_width[0] + BLOCK_X;
1047 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1048 s->motion_val[0][current_fragment][0] = motion_x[k];
1049 s->motion_val[0][current_fragment][1] = motion_y[k];
1050 } else {
1051 s->motion_val[0][current_fragment][0] = motion_x[0];
1052 s->motion_val[0][current_fragment][1] = motion_y[0];
1053 }
1054 }
1055
1056 if (s->chroma_y_shift) {
1057 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1058 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
1059 motion_x[2] + motion_x[3], 2);
1060 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
1061 motion_y[2] + motion_y[3], 2);
1062 }
1063 if (s->version <= 2) {
1064 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1065 motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
1066 }
1067 frag = mb_y * s->fragment_width[1] + mb_x;
1068 s->motion_val[1][frag][0] = motion_x[0];
1069 s->motion_val[1][frag][1] = motion_y[0];
1070 } else if (s->chroma_x_shift) {
1071 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1072 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
1073 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
1074 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
1075 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
1076 } else {
1077 motion_x[1] = motion_x[0];
1078 motion_y[1] = motion_y[0];
1079 }
1080 if (s->version <= 2) {
1081 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1082 motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
1083 }
1084 frag = 2 * mb_y * s->fragment_width[1] + mb_x;
1085 for (int k = 0; k < 2; k++) {
1086 s->motion_val[1][frag][0] = motion_x[k];
1087 s->motion_val[1][frag][1] = motion_y[k];
1088 frag += s->fragment_width[1];
1089 }
1090 } else {
1091 for (int k = 0; k < 4; k++) {
1092 frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
1093 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1094 s->motion_val[1][frag][0] = motion_x[k];
1095 s->motion_val[1][frag][1] = motion_y[k];
1096 } else {
1097 s->motion_val[1][frag][0] = motion_x[0];
1098 s->motion_val[1][frag][1] = motion_y[0];
1099 }
1100 }
1101 }
1102 }
1103 }
1104 }
1105
1106 return 0;
1107}
1108
1110{
1111 int num_blocks = s->total_num_coded_frags;
1112
1113 for (int qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
1114 int i = 0, blocks_decoded = 0, num_blocks_at_qpi = 0;
1115 int bit, run_length;
1116
1117 bit = get_bits1(gb) ^ 1;
1118 run_length = 0;
1119
1120 do {
1121 if (run_length == MAXIMUM_LONG_BIT_RUN)
1122 bit = get_bits1(gb);
1123 else
1124 bit ^= 1;
1125
1126 run_length = get_vlc2(gb, superblock_run_length_vlc,
1128 if (run_length == 34)
1129 run_length += get_bits(gb, 12);
1130 blocks_decoded += run_length;
1131
1132 if (!bit)
1133 num_blocks_at_qpi += run_length;
1134
1135 for (int j = 0; j < run_length; i++) {
1136 if (i >= s->total_num_coded_frags)
1137 return -1;
1138
1139 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
1140 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
1141 j++;
1142 }
1143 }
1144 } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
1145
1146 num_blocks -= num_blocks_at_qpi;
1147 }
1148
1149 return 0;
1150}
1151
1152static inline int get_eob_run(GetBitContext *gb, int token)
1153{
1154 int v = eob_run_table[token].base;
1155 if (eob_run_table[token].bits)
1156 v += get_bits(gb, eob_run_table[token].bits);
1157 return v;
1158}
1159
1160static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
1161{
1162 int bits_to_get, zero_run;
1163
1164 bits_to_get = coeff_get_bits[token];
1165 if (bits_to_get)
1166 bits_to_get = get_bits(gb, bits_to_get);
1167 *coeff = coeff_tables[token][bits_to_get];
1168
1169 zero_run = zero_run_base[token];
1170 if (zero_run_get_bits[token])
1171 zero_run += get_bits(gb, zero_run_get_bits[token]);
1172
1173 return zero_run;
1174}
1175
1176/*
1177 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1178 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1179 * data. This function unpacks all the VLCs for either the Y plane or both
1180 * C planes, and is called for DC coefficients or different AC coefficient
1181 * levels (since different coefficient types require different VLC tables.
1182 *
1183 * This function returns a residual eob run. E.g, if a particular token gave
1184 * instructions to EOB the next 5 fragments and there were only 2 fragments
1185 * left in the current fragment range, 3 would be returned so that it could
1186 * be passed into the next call to this same function.
1187 */
1189 const VLCElem *vlc_table, int coeff_index,
1190 int plane,
1191 int eob_run)
1192{
1193 int j = 0;
1194 int token;
1195 int zero_run = 0;
1196 int16_t coeff = 0;
1197 int blocks_ended;
1198 int coeff_i = 0;
1199 int num_coeffs = s->num_coded_frags[plane][coeff_index];
1200 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
1201
1202 /* local references to structure members to avoid repeated dereferences */
1203 const int *coded_fragment_list = s->coded_fragment_list[plane];
1204 Vp3Fragment *all_fragments = s->all_fragments;
1205
1206 if (num_coeffs < 0) {
1207 av_log(s->avctx, AV_LOG_ERROR,
1208 "Invalid number of coefficients at level %d\n", coeff_index);
1209 return AVERROR_INVALIDDATA;
1210 }
1211
1212 if (eob_run > num_coeffs) {
1213 coeff_i =
1214 blocks_ended = num_coeffs;
1215 eob_run -= num_coeffs;
1216 } else {
1217 coeff_i =
1218 blocks_ended = eob_run;
1219 eob_run = 0;
1220 }
1221
1222 // insert fake EOB token to cover the split between planes or zzi
1223 if (blocks_ended)
1224 dct_tokens[j++] = blocks_ended << 2;
1225
1226 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
1227 /* decode a VLC into a token */
1228 token = get_vlc2(gb, vlc_table, 11, 3);
1229 /* use the token to get a zero run, a coefficient, and an eob run */
1230 if ((unsigned) token <= 6U) {
1231 eob_run = get_eob_run(gb, token);
1232 if (!eob_run)
1233 eob_run = INT_MAX;
1234
1235 // record only the number of blocks ended in this plane,
1236 // any spill will be recorded in the next plane.
1237 if (eob_run > num_coeffs - coeff_i) {
1238 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
1239 blocks_ended += num_coeffs - coeff_i;
1240 eob_run -= num_coeffs - coeff_i;
1241 coeff_i = num_coeffs;
1242 } else {
1243 dct_tokens[j++] = TOKEN_EOB(eob_run);
1244 blocks_ended += eob_run;
1245 coeff_i += eob_run;
1246 eob_run = 0;
1247 }
1248 } else if (token >= 0) {
1249 zero_run = get_coeff(gb, token, &coeff);
1250
1251 if (zero_run) {
1252 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1253 } else {
1254 // Save DC into the fragment structure. DC prediction is
1255 // done in raster order, so the actual DC can't be in with
1256 // other tokens. We still need the token in dct_tokens[]
1257 // however, or else the structure collapses on itself.
1258 if (!coeff_index)
1259 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1260
1261 dct_tokens[j++] = TOKEN_COEFF(coeff);
1262 }
1263
1264 if (coeff_index + zero_run > 64) {
1265 av_log(s->avctx, AV_LOG_DEBUG,
1266 "Invalid zero run of %d with %d coeffs left\n",
1267 zero_run, 64 - coeff_index);
1268 zero_run = 64 - coeff_index;
1269 }
1270
1271 // zero runs code multiple coefficients,
1272 // so don't try to decode coeffs for those higher levels
1273 for (int i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1274 s->num_coded_frags[plane][i]--;
1275 coeff_i++;
1276 } else {
1277 av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1278 return -1;
1279 }
1280 }
1281
1282 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1283 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1284
1285 // decrement the number of blocks that have higher coefficients for each
1286 // EOB run at this level
1287 if (blocks_ended)
1288 for (int i = coeff_index + 1; i < 64; i++)
1289 s->num_coded_frags[plane][i] -= blocks_ended;
1290
1291 // setup the next buffer
1292 if (plane < 2)
1293 s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1294 else if (coeff_index < 63)
1295 s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1296
1297 return eob_run;
1298}
1299
1301 int first_fragment,
1302 int fragment_width,
1303 int fragment_height);
1304/*
1305 * This function unpacks all of the DCT coefficient data from the
1306 * bitstream.
1307 */
1309{
1310 const VLCElem *const *coeff_vlc = s->coeff_vlc->vlc_tabs;
1311 int dc_y_table;
1312 int dc_c_table;
1313 int ac_y_table;
1314 int ac_c_table;
1315 int residual_eob_run = 0;
1316 const VLCElem *y_tables[64], *c_tables[64];
1317
1318 s->dct_tokens[0][0] = s->dct_tokens_base;
1319
1320 if (get_bits_left(gb) < 16)
1321 return AVERROR_INVALIDDATA;
1322
1323 /* fetch the DC table indexes */
1324 dc_y_table = get_bits(gb, 4);
1325 dc_c_table = get_bits(gb, 4);
1326
1327 /* unpack the Y plane DC coefficients */
1328 residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_y_table], 0,
1329 0, residual_eob_run);
1330 if (residual_eob_run < 0)
1331 return residual_eob_run;
1332 if (get_bits_left(gb) < 8)
1333 return AVERROR_INVALIDDATA;
1334
1335 /* reverse prediction of the Y-plane DC coefficients */
1336 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1337
1338 /* unpack the C plane DC coefficients */
1339 residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_c_table], 0,
1340 1, residual_eob_run);
1341 if (residual_eob_run < 0)
1342 return residual_eob_run;
1343 residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_c_table], 0,
1344 2, residual_eob_run);
1345 if (residual_eob_run < 0)
1346 return residual_eob_run;
1347
1348 /* reverse prediction of the C-plane DC coefficients */
1349 if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1350 reverse_dc_prediction(s, s->fragment_start[1],
1351 s->fragment_width[1], s->fragment_height[1]);
1352 reverse_dc_prediction(s, s->fragment_start[2],
1353 s->fragment_width[1], s->fragment_height[1]);
1354 }
1355
1356 if (get_bits_left(gb) < 8)
1357 return AVERROR_INVALIDDATA;
1358 /* fetch the AC table indexes */
1359 ac_y_table = get_bits(gb, 4);
1360 ac_c_table = get_bits(gb, 4);
1361
1362 /* build tables of AC VLC tables */
1363 for (int i = 1; i <= 5; i++) {
1364 /* AC VLC table group 1 */
1365 y_tables[i] = coeff_vlc[ac_y_table + 16];
1366 c_tables[i] = coeff_vlc[ac_c_table + 16];
1367 }
1368 for (int i = 6; i <= 14; i++) {
1369 /* AC VLC table group 2 */
1370 y_tables[i] = coeff_vlc[ac_y_table + 32];
1371 c_tables[i] = coeff_vlc[ac_c_table + 32];
1372 }
1373 for (int i = 15; i <= 27; i++) {
1374 /* AC VLC table group 3 */
1375 y_tables[i] = coeff_vlc[ac_y_table + 48];
1376 c_tables[i] = coeff_vlc[ac_c_table + 48];
1377 }
1378 for (int i = 28; i <= 63; i++) {
1379 /* AC VLC table group 4 */
1380 y_tables[i] = coeff_vlc[ac_y_table + 64];
1381 c_tables[i] = coeff_vlc[ac_c_table + 64];
1382 }
1383
1384 /* decode all AC coefficients */
1385 for (int i = 1; i <= 63; i++) {
1386 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1387 0, residual_eob_run);
1388 if (residual_eob_run < 0)
1389 return residual_eob_run;
1390
1391 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1392 1, residual_eob_run);
1393 if (residual_eob_run < 0)
1394 return residual_eob_run;
1395 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1396 2, residual_eob_run);
1397 if (residual_eob_run < 0)
1398 return residual_eob_run;
1399 }
1400
1401 return 0;
1402}
1403
1404#if CONFIG_VP4_DECODER
1405/**
1406 * eob_tracker[] is instead of TOKEN_EOB(value)
1407 * a dummy TOKEN_EOB(0) value is used to make vp3_dequant work
1408 *
1409 * @return < 0 on error
1410 */
1411static int vp4_unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1412 const VLCElem *const vlc_tables[64],
1413 int plane, int eob_tracker[64], int fragment)
1414{
1415 int token;
1416 int zero_run = 0;
1417 int16_t coeff = 0;
1418 int coeff_i = 0;
1419 int eob_run;
1420
1421 while (!eob_tracker[coeff_i]) {
1422 if (get_bits_left(gb) < 1)
1423 return AVERROR_INVALIDDATA;
1424
1425 token = get_vlc2(gb, vlc_tables[coeff_i], 11, 3);
1426
1427 /* use the token to get a zero run, a coefficient, and an eob run */
1428 if ((unsigned) token <= 6U) {
1429 eob_run = get_eob_run(gb, token);
1430 *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1431 eob_tracker[coeff_i] = eob_run - 1;
1432 return 0;
1433 } else if (token >= 0) {
1434 zero_run = get_coeff(gb, token, &coeff);
1435
1436 if (zero_run) {
1437 if (coeff_i + zero_run > 64) {
1438 av_log(s->avctx, AV_LOG_DEBUG,
1439 "Invalid zero run of %d with %d coeffs left\n",
1440 zero_run, 64 - coeff_i);
1441 zero_run = 64 - coeff_i;
1442 }
1443 *s->dct_tokens[plane][coeff_i]++ = TOKEN_ZERO_RUN(coeff, zero_run);
1444 coeff_i += zero_run;
1445 } else {
1446 if (!coeff_i)
1447 s->all_fragments[fragment].dc = coeff;
1448
1449 *s->dct_tokens[plane][coeff_i]++ = TOKEN_COEFF(coeff);
1450 }
1451 coeff_i++;
1452 if (coeff_i >= 64) /* > 64 occurs when there is a zero_run overflow */
1453 return 0; /* stop */
1454 } else {
1455 av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1456 return -1;
1457 }
1458 }
1459 *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1460 eob_tracker[coeff_i]--;
1461 return 0;
1462}
1463
1464static void vp4_dc_predictor_reset(VP4Predictor *p)
1465{
1466 p->dc = 0;
1467 p->type = VP4_DC_UNDEFINED;
1468}
1469
1470static void vp4_dc_pred_before(const Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1471{
1472 for (int i = 0; i < 4; i++)
1473 dc_pred[0][i + 1] = s->dc_pred_row[sb_x * 4 + i];
1474
1475 for (int j = 1; j < 5; j++)
1476 for (int i = 0; i < 4; i++)
1477 vp4_dc_predictor_reset(&dc_pred[j][i + 1]);
1478}
1479
1480static void vp4_dc_pred_after(Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1481{
1482 for (int i = 0; i < 4; i++)
1483 s->dc_pred_row[sb_x * 4 + i] = dc_pred[4][i + 1];
1484
1485 for (int i = 1; i < 5; i++)
1486 dc_pred[i][0] = dc_pred[i][4];
1487}
1488
1489/* note: dc_pred points to the current block */
1490static int vp4_dc_pred(const Vp3DecodeContext *s, const VP4Predictor * dc_pred, const int * last_dc, int type, int plane)
1491{
1492 int count = 0;
1493 int dc = 0;
1494
1495 if (dc_pred[-6].type == type) {
1496 dc += dc_pred[-6].dc;
1497 count++;
1498 }
1499
1500 if (dc_pred[6].type == type) {
1501 dc += dc_pred[6].dc;
1502 count++;
1503 }
1504
1505 if (count != 2 && dc_pred[-1].type == type) {
1506 dc += dc_pred[-1].dc;
1507 count++;
1508 }
1509
1510 if (count != 2 && dc_pred[1].type == type) {
1511 dc += dc_pred[1].dc;
1512 count++;
1513 }
1514
1515 /* using division instead of shift to correctly handle negative values */
1516 return count == 2 ? dc / 2 : last_dc[type];
1517}
1518
1519static void vp4_set_tokens_base(Vp3DecodeContext *s)
1520{
1521 int16_t *base = s->dct_tokens_base;
1522 for (int plane = 0; plane < 3; plane++) {
1523 for (int i = 0; i < 64; i++) {
1524 s->dct_tokens[plane][i] = base;
1525 base += s->fragment_width[!!plane] * s->fragment_height[!!plane];
1526 }
1527 }
1528}
1529
1530static int vp4_unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1531{
1532 const VLCElem *const *coeff_vlc = s->coeff_vlc->vlc_tabs;
1533 int dc_y_table;
1534 int dc_c_table;
1535 int ac_y_table;
1536 int ac_c_table;
1537 const VLCElem *tables[2][64];
1538 int eob_tracker[64];
1539 VP4Predictor dc_pred[6][6];
1540 int last_dc[NB_VP4_DC_TYPES];
1541
1542 if (get_bits_left(gb) < 16)
1543 return AVERROR_INVALIDDATA;
1544
1545 /* fetch the DC table indexes */
1546 dc_y_table = get_bits(gb, 4);
1547 dc_c_table = get_bits(gb, 4);
1548
1549 ac_y_table = get_bits(gb, 4);
1550 ac_c_table = get_bits(gb, 4);
1551
1552 /* build tables of DC/AC VLC tables */
1553
1554 /* DC table group */
1555 tables[0][0] = coeff_vlc[dc_y_table];
1556 tables[1][0] = coeff_vlc[dc_c_table];
1557 for (int i = 1; i <= 5; i++) {
1558 /* AC VLC table group 1 */
1559 tables[0][i] = coeff_vlc[ac_y_table + 16];
1560 tables[1][i] = coeff_vlc[ac_c_table + 16];
1561 }
1562 for (int i = 6; i <= 14; i++) {
1563 /* AC VLC table group 2 */
1564 tables[0][i] = coeff_vlc[ac_y_table + 32];
1565 tables[1][i] = coeff_vlc[ac_c_table + 32];
1566 }
1567 for (int i = 15; i <= 27; i++) {
1568 /* AC VLC table group 3 */
1569 tables[0][i] = coeff_vlc[ac_y_table + 48];
1570 tables[1][i] = coeff_vlc[ac_c_table + 48];
1571 }
1572 for (int i = 28; i <= 63; i++) {
1573 /* AC VLC table group 4 */
1574 tables[0][i] = coeff_vlc[ac_y_table + 64];
1575 tables[1][i] = coeff_vlc[ac_c_table + 64];
1576 }
1577
1578 vp4_set_tokens_base(s);
1579
1580 memset(last_dc, 0, sizeof(last_dc));
1581
1582 for (int plane = 0; plane < ((s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 1 : 3); plane++) {
1583 memset(eob_tracker, 0, sizeof(eob_tracker));
1584
1585 /* initialise dc prediction */
1586 for (int i = 0; i < s->fragment_width[!!plane]; i++)
1587 vp4_dc_predictor_reset(&s->dc_pred_row[i]);
1588
1589 for (int j = 0; j < 6; j++)
1590 for (int i = 0; i < 6; i++)
1591 vp4_dc_predictor_reset(&dc_pred[j][i]);
1592
1593 for (int sb_y = 0; sb_y * 4 < s->fragment_height[!!plane]; sb_y++) {
1594 for (int sb_x = 0; sb_x *4 < s->fragment_width[!!plane]; sb_x++) {
1595 vp4_dc_pred_before(s, dc_pred, sb_x);
1596 for (int j = 0; j < 16; j++) {
1597 int hx = hilbert_offset[j][0];
1598 int hy = hilbert_offset[j][1];
1599 int x = 4 * sb_x + hx;
1600 int y = 4 * sb_y + hy;
1601 VP4Predictor *this_dc_pred = &dc_pred[hy + 1][hx + 1];
1602 int fragment, dc_block_type;
1603
1604 if (x >= s->fragment_width[!!plane] || y >= s->fragment_height[!!plane])
1605 continue;
1606
1607 fragment = s->fragment_start[plane] + y * s->fragment_width[!!plane] + x;
1608
1609 if (s->all_fragments[fragment].coding_method == MODE_COPY)
1610 continue;
1611
1612 if (vp4_unpack_vlcs(s, gb, tables[!!plane], plane, eob_tracker, fragment) < 0)
1613 return -1;
1614
1615 dc_block_type = vp4_pred_block_type_map[s->all_fragments[fragment].coding_method];
1616
1617 s->all_fragments[fragment].dc +=
1618 vp4_dc_pred(s, this_dc_pred, last_dc, dc_block_type, plane);
1619
1620 this_dc_pred->type = dc_block_type,
1621 this_dc_pred->dc = last_dc[dc_block_type] = s->all_fragments[fragment].dc;
1622 }
1623 vp4_dc_pred_after(s, dc_pred, sb_x);
1624 }
1625 }
1626 }
1627
1628 vp4_set_tokens_base(s);
1629
1630 return 0;
1631}
1632#endif
1633
1634/*
1635 * This function reverses the DC prediction for each coded fragment in
1636 * the frame. Much of this function is adapted directly from the original
1637 * VP3 source code.
1638 */
1639#define COMPATIBLE_FRAME(x) \
1640 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1641#define DC_COEFF(u) s->all_fragments[u].dc
1642
1644 int first_fragment,
1645 int fragment_width,
1646 int fragment_height)
1647{
1648#define PUL 8
1649#define PU 4
1650#define PUR 2
1651#define PL 1
1652
1653 int i = first_fragment;
1654
1655 int predicted_dc;
1656
1657 /* DC values for the left, up-left, up, and up-right fragments */
1658 int vl, vul, vu, vur;
1659
1660 /* indexes for the left, up-left, up, and up-right fragments */
1661 int l, ul, u, ur;
1662
1663 /*
1664 * The 6 fields mean:
1665 * 0: up-left multiplier
1666 * 1: up multiplier
1667 * 2: up-right multiplier
1668 * 3: left multiplier
1669 */
1670 static const int predictor_transform[16][4] = {
1671 { 0, 0, 0, 0 },
1672 { 0, 0, 0, 128 }, // PL
1673 { 0, 0, 128, 0 }, // PUR
1674 { 0, 0, 53, 75 }, // PUR|PL
1675 { 0, 128, 0, 0 }, // PU
1676 { 0, 64, 0, 64 }, // PU |PL
1677 { 0, 128, 0, 0 }, // PU |PUR
1678 { 0, 0, 53, 75 }, // PU |PUR|PL
1679 { 128, 0, 0, 0 }, // PUL
1680 { 0, 0, 0, 128 }, // PUL|PL
1681 { 64, 0, 64, 0 }, // PUL|PUR
1682 { 0, 0, 53, 75 }, // PUL|PUR|PL
1683 { 0, 128, 0, 0 }, // PUL|PU
1684 { -104, 116, 0, 116 }, // PUL|PU |PL
1685 { 24, 80, 24, 0 }, // PUL|PU |PUR
1686 { -104, 116, 0, 116 } // PUL|PU |PUR|PL
1687 };
1688
1689 /* This table shows which types of blocks can use other blocks for
1690 * prediction. For example, INTRA is the only mode in this table to
1691 * have a frame number of 0. That means INTRA blocks can only predict
1692 * from other INTRA blocks. There are 2 golden frame coding types;
1693 * blocks encoding in these modes can only predict from other blocks
1694 * that were encoded with these 1 of these 2 modes. */
1695 static const unsigned char compatible_frame[9] = {
1696 1, /* MODE_INTER_NO_MV */
1697 0, /* MODE_INTRA */
1698 1, /* MODE_INTER_PLUS_MV */
1699 1, /* MODE_INTER_LAST_MV */
1700 1, /* MODE_INTER_PRIOR_MV */
1701 2, /* MODE_USING_GOLDEN */
1702 2, /* MODE_GOLDEN_MV */
1703 1, /* MODE_INTER_FOUR_MV */
1704 3 /* MODE_COPY */
1705 };
1706 int current_frame_type;
1707
1708 /* there is a last DC predictor for each of the 3 frame types */
1709 short last_dc[3];
1710
1711 int transform = 0;
1712
1713 vul =
1714 vu =
1715 vur =
1716 vl = 0;
1717 last_dc[0] =
1718 last_dc[1] =
1719 last_dc[2] = 0;
1720
1721 /* for each fragment row... */
1722 for (int y = 0; y < fragment_height; y++) {
1723 /* for each fragment in a row... */
1724 for (int x = 0; x < fragment_width; x++, i++) {
1725
1726 /* reverse prediction if this block was coded */
1727 if (s->all_fragments[i].coding_method != MODE_COPY) {
1728 current_frame_type =
1729 compatible_frame[s->all_fragments[i].coding_method];
1730
1731 transform = 0;
1732 if (x) {
1733 l = i - 1;
1734 vl = DC_COEFF(l);
1735 if (COMPATIBLE_FRAME(l))
1736 transform |= PL;
1737 }
1738 if (y) {
1739 u = i - fragment_width;
1740 vu = DC_COEFF(u);
1741 if (COMPATIBLE_FRAME(u))
1742 transform |= PU;
1743 if (x) {
1744 ul = i - fragment_width - 1;
1745 vul = DC_COEFF(ul);
1746 if (COMPATIBLE_FRAME(ul))
1747 transform |= PUL;
1748 }
1749 if (x + 1 < fragment_width) {
1750 ur = i - fragment_width + 1;
1751 vur = DC_COEFF(ur);
1752 if (COMPATIBLE_FRAME(ur))
1753 transform |= PUR;
1754 }
1755 }
1756
1757 if (transform == 0) {
1758 /* if there were no fragments to predict from, use last
1759 * DC saved */
1760 predicted_dc = last_dc[current_frame_type];
1761 } else {
1762 /* apply the appropriate predictor transform */
1763 predicted_dc =
1764 (predictor_transform[transform][0] * vul) +
1765 (predictor_transform[transform][1] * vu) +
1766 (predictor_transform[transform][2] * vur) +
1767 (predictor_transform[transform][3] * vl);
1768
1769 predicted_dc /= 128;
1770
1771 /* check for outranging on the [ul u l] and
1772 * [ul u ur l] predictors */
1773 if ((transform == 15) || (transform == 13)) {
1774 if (FFABS(predicted_dc - vu) > 128)
1775 predicted_dc = vu;
1776 else if (FFABS(predicted_dc - vl) > 128)
1777 predicted_dc = vl;
1778 else if (FFABS(predicted_dc - vul) > 128)
1779 predicted_dc = vul;
1780 }
1781 }
1782
1783 /* at long last, apply the predictor */
1784 DC_COEFF(i) += predicted_dc;
1785 /* save the DC */
1786 last_dc[current_frame_type] = DC_COEFF(i);
1787 }
1788 }
1789 }
1790}
1791
1792static void apply_loop_filter(Vp3DecodeContext *s, int plane,
1793 int ystart, int yend)
1794{
1795 int *bounding_values = s->bounding_values_array + 127;
1796
1797 int width = s->fragment_width[!!plane];
1798 int height = s->fragment_height[!!plane];
1799 int fragment = s->fragment_start[plane] + ystart * width;
1800 ptrdiff_t stride = s->current_frame.f->linesize[plane];
1801 uint8_t *plane_data = s->current_frame.f->data[plane];
1802 if (!s->flipped_image)
1803 stride = -stride;
1804 plane_data += s->data_offset[plane] + 8 * ystart * stride;
1805
1806 for (int y = ystart; y < yend; y++) {
1807 for (int x = 0; x < width; x++) {
1808 /* This code basically just deblocks on the edges of coded blocks.
1809 * However, it has to be much more complicated because of the
1810 * brain damaged deblock ordering used in VP3/Theora. Order matters
1811 * because some pixels get filtered twice. */
1812 if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1813 /* do not perform left edge filter for left columns frags */
1814 if (x > 0) {
1815 s->vp3dsp.h_loop_filter(
1816 plane_data + 8 * x,
1817 stride, bounding_values);
1818 }
1819
1820 /* do not perform top edge filter for top row fragments */
1821 if (y > 0) {
1822 s->vp3dsp.v_loop_filter(
1823 plane_data + 8 * x,
1824 stride, bounding_values);
1825 }
1826
1827 /* do not perform right edge filter for right column
1828 * fragments or if right fragment neighbor is also coded
1829 * in this frame (it will be filtered in next iteration) */
1830 if ((x < width - 1) &&
1831 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1832 s->vp3dsp.h_loop_filter(
1833 plane_data + 8 * x + 8,
1834 stride, bounding_values);
1835 }
1836
1837 /* do not perform bottom edge filter for bottom row
1838 * fragments or if bottom fragment neighbor is also coded
1839 * in this frame (it will be filtered in the next row) */
1840 if ((y < height - 1) &&
1841 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1842 s->vp3dsp.v_loop_filter(
1843 plane_data + 8 * x + 8 * stride,
1844 stride, bounding_values);
1845 }
1846 }
1847
1848 fragment++;
1849 }
1850 plane_data += 8 * stride;
1851 }
1852}
1853
1854/**
1855 * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1856 * for the next block in coding order
1857 */
1858static inline int vp3_dequant(Vp3DecodeContext *s, const Vp3Fragment *frag,
1859 int plane, int inter, int16_t block[64])
1860{
1861 const int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1862 const uint8_t *perm = s->idct_scantable;
1863 int i = 0;
1864
1865 do {
1866 int token = *s->dct_tokens[plane][i];
1867 switch (token & 3) {
1868 case 0: // EOB
1869 if (--token < 4) // 0-3 are token types so the EOB run must now be 0
1870 s->dct_tokens[plane][i]++;
1871 else
1872 *s->dct_tokens[plane][i] = token & ~3;
1873 goto end;
1874 case 1: // zero run
1875 s->dct_tokens[plane][i]++;
1876 i += (token >> 2) & 0x7f;
1877 if (i > 63) {
1878 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1879 return i;
1880 }
1881 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1882 i++;
1883 break;
1884 case 2: // coeff
1885 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1886 s->dct_tokens[plane][i++]++;
1887 break;
1888 default: // shouldn't happen
1889 return i;
1890 }
1891 } while (i < 64);
1892 // return value is expected to be a valid level
1893 i--;
1894end:
1895 // the actual DC+prediction is in the fragment structure
1896 block[0] = frag->dc * s->qmat[0][inter][plane][0];
1897 return i;
1898}
1899
1900/**
1901 * called when all pixels up to row y are complete
1902 */
1904{
1905 int h, cy;
1907
1908 if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1909 /* At the end of the frame, report INT_MAX instead of the height of
1910 * the frame. This makes the other threads' ff_thread_await_progress()
1911 * calls cheaper, because they don't have to clip their values. */
1912 ff_progress_frame_report(&s->current_frame,
1913 y == s->height ? INT_MAX : y - 1);
1914 }
1915
1916 if (!s->avctx->draw_horiz_band)
1917 return;
1918
1919 h = y - s->last_slice_end;
1920 s->last_slice_end = y;
1921 y -= h;
1922
1923 if (!s->flipped_image)
1924 y = s->height - y - h;
1925
1926 cy = y >> s->chroma_y_shift;
1927 offset[0] = s->current_frame.f->linesize[0] * y;
1928 offset[1] = s->current_frame.f->linesize[1] * cy;
1929 offset[2] = s->current_frame.f->linesize[2] * cy;
1930 for (int i = 3; i < AV_NUM_DATA_POINTERS; i++)
1931 offset[i] = 0;
1932
1933 s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1934}
1935
1936/**
1937 * Wait for the reference frame of the current fragment.
1938 * The progress value is in luma pixel rows.
1939 */
1941 int motion_y, int y)
1942{
1943 const ProgressFrame *ref_frame;
1944 int ref_row;
1945 int border = motion_y & 1;
1946
1947 if (fragment->coding_method == MODE_USING_GOLDEN ||
1948 fragment->coding_method == MODE_GOLDEN_MV)
1949 ref_frame = &s->golden_frame;
1950 else
1951 ref_frame = &s->last_frame;
1952
1953 ref_row = y + (motion_y >> 1);
1954 ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1955
1957}
1958
1959#if CONFIG_VP4_DECODER
1960/**
1961 * @return non-zero if temp (edge_emu_buffer) was populated
1962 */
1963static int vp4_mc_loop_filter(Vp3DecodeContext *s, int plane, int motion_x, int motion_y, int bx, int by,
1964 const uint8_t *motion_source, ptrdiff_t stride,
1965 int src_x, int src_y, uint8_t *temp)
1966{
1967 int motion_shift = plane ? 4 : 2;
1968 int subpel_mask = plane ? 3 : 1;
1969 int *bounding_values = s->bounding_values_array + 127;
1970
1971 int x, y;
1972 int x2, y2;
1973 int x_subpel, y_subpel;
1974 int x_offset, y_offset;
1975
1976 int block_width = plane ? 8 : 16;
1977 int plane_width = s->width >> (plane && s->chroma_x_shift);
1978 int plane_height = s->height >> (plane && s->chroma_y_shift);
1979
1980#define loop_stride 12
1981 uint8_t loop[12 * loop_stride];
1982
1983 /* using division instead of shift to correctly handle negative values */
1984 x = 8 * bx + motion_x / motion_shift;
1985 y = 8 * by + motion_y / motion_shift;
1986
1987 x_subpel = motion_x & subpel_mask;
1988 y_subpel = motion_y & subpel_mask;
1989
1990 if (x_subpel || y_subpel) {
1991 x--;
1992 y--;
1993
1994 if (x_subpel)
1995 x = FFMIN(x, x + FFSIGN(motion_x));
1996
1997 if (y_subpel)
1998 y = FFMIN(y, y + FFSIGN(motion_y));
1999
2000 x2 = x + block_width;
2001 y2 = y + block_width;
2002
2003 if (x2 < 0 || x2 >= plane_width || y2 < 0 || y2 >= plane_height)
2004 return 0;
2005
2006 x_offset = (-(x + 2) & 7) + 2;
2007 y_offset = (-(y + 2) & 7) + 2;
2008
2009 av_assert1(!(x_offset > 8 + x_subpel && y_offset > 8 + y_subpel));
2010
2011 s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2012 loop_stride, stride,
2013 12, 12, src_x - 1, src_y - 1,
2014 plane_width,
2015 plane_height);
2016
2017 if (x_offset <= 8 + x_subpel)
2018 ff_vp3dsp_h_loop_filter_12(loop + x_offset, loop_stride, bounding_values);
2019
2020 if (y_offset <= 8 + y_subpel)
2021 ff_vp3dsp_v_loop_filter_12(loop + y_offset*loop_stride, loop_stride, bounding_values);
2022
2023 } else {
2024
2025 x_offset = -x & 7;
2026 y_offset = -y & 7;
2027
2028 if (!x_offset && !y_offset)
2029 return 0;
2030
2031 s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2032 loop_stride, stride,
2033 12, 12, src_x - 1, src_y - 1,
2034 plane_width,
2035 plane_height);
2036
2037#define safe_loop_filter(name, ptr, stride, bounding_values) \
2038 if (VP3_LOOP_FILTER_NO_UNALIGNED_SUPPORT && (uintptr_t)(ptr) & 7) \
2039 s->vp3dsp.name##_unaligned(ptr, stride, bounding_values); \
2040 else \
2041 s->vp3dsp.name(ptr, stride, bounding_values);
2042
2043 if (x_offset)
2044 safe_loop_filter(h_loop_filter, loop + loop_stride + x_offset + 1, loop_stride, bounding_values);
2045
2046 if (y_offset)
2047 safe_loop_filter(v_loop_filter, loop + (y_offset + 1)*loop_stride + 1, loop_stride, bounding_values);
2048 }
2049
2050 for (int i = 0; i < 9; i++)
2051 memcpy(temp + i*stride, loop + (i + 1) * loop_stride + 1, 9);
2052
2053 return 1;
2054}
2055#endif
2056
2057/*
2058 * Perform the final rendering for a particular slice of data.
2059 * The slice number ranges from 0..(c_superblock_height - 1).
2060 */
2061static void render_slice(Vp3DecodeContext *s, int slice)
2062{
2063 int16_t *block = s->block;
2064 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
2065 /* When decoding keyframes, the earlier frames may not be available,
2066 * so we just use the current frame in this case instead;
2067 * it also avoid using undefined pointer arithmetic. Nothing is
2068 * ever read from these frames in case of a keyframe. */
2069 const AVFrame *last_frame = s->last_frame.f ?
2070 s->last_frame.f : s->current_frame.f;
2071 const AVFrame *golden_frame = s->golden_frame.f ?
2072 s->golden_frame.f : s->current_frame.f;
2073 int motion_halfpel_index;
2074 int first_pixel;
2075
2076 if (slice >= s->c_superblock_height)
2077 return;
2078
2079 for (int plane = 0; plane < 3; plane++) {
2080 uint8_t *output_plane = s->current_frame.f->data[plane] +
2081 s->data_offset[plane];
2082 const uint8_t *last_plane = last_frame->data[plane] +
2083 s->data_offset[plane];
2084 const uint8_t *golden_plane = golden_frame->data[plane] +
2085 s->data_offset[plane];
2086 ptrdiff_t stride = s->current_frame.f->linesize[plane];
2087 int plane_width = s->width >> (plane && s->chroma_x_shift);
2088 int plane_height = s->height >> (plane && s->chroma_y_shift);
2089 const int8_t (*motion_val)[2] = s->motion_val[!!plane];
2090
2091 int sb_y = slice << (!plane && s->chroma_y_shift);
2092 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
2093 int slice_width = plane ? s->c_superblock_width
2094 : s->y_superblock_width;
2095
2096 int fragment_width = s->fragment_width[!!plane];
2097 int fragment_height = s->fragment_height[!!plane];
2098 int fragment_start = s->fragment_start[plane];
2099
2100 int do_await = !plane && HAVE_THREADS &&
2101 (s->avctx->active_thread_type & FF_THREAD_FRAME);
2102
2103 if (!s->flipped_image)
2104 stride = -stride;
2105 if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2106 continue;
2107
2108 /* for each superblock row in the slice (both of them)... */
2109 for (; sb_y < slice_height; sb_y++) {
2110 /* for each superblock in a row... */
2111 for (int sb_x = 0; sb_x < slice_width; sb_x++) {
2112 /* for each block in a superblock... */
2113 for (int j = 0; j < 16; j++) {
2114 int x = 4 * sb_x + hilbert_offset[j][0];
2115 int y = 4 * sb_y + hilbert_offset[j][1];
2116 int fragment = y * fragment_width + x;
2117
2118 int i = fragment_start + fragment;
2119
2120 // bounds check
2121 if (x >= fragment_width || y >= fragment_height)
2122 continue;
2123
2124 first_pixel = 8 * y * stride + 8 * x;
2125
2126 if (do_await &&
2127 s->all_fragments[i].coding_method != MODE_INTRA)
2128 await_reference_row(s, &s->all_fragments[i],
2129 motion_val[fragment][1],
2130 (16 * y) >> s->chroma_y_shift);
2131
2132 /* transform if this block was coded */
2133 if (s->all_fragments[i].coding_method != MODE_COPY) {
2134 const uint8_t *motion_source;
2135 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2136 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2137 motion_source = golden_plane;
2138 else
2139 motion_source = last_plane;
2140
2141 motion_source += first_pixel;
2142 motion_halfpel_index = 0;
2143
2144 /* sort out the motion vector if this fragment is coded
2145 * using a motion vector method */
2146 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2147 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2148 int src_x, src_y;
2149 int standard_mc = 1;
2150 motion_x = motion_val[fragment][0];
2151 motion_y = motion_val[fragment][1];
2152#if CONFIG_VP4_DECODER
2153 if (plane && s->version >= 2) {
2154 motion_x = (motion_x >> 1) | (motion_x & 1);
2155 motion_y = (motion_y >> 1) | (motion_y & 1);
2156 }
2157#endif
2158
2159 src_x = (motion_x >> 1) + 8 * x;
2160 src_y = (motion_y >> 1) + 8 * y;
2161
2162 motion_halfpel_index = motion_x & 0x01;
2163 motion_source += (motion_x >> 1);
2164
2165 motion_halfpel_index |= (motion_y & 0x01) << 1;
2166 motion_source += ((motion_y >> 1) * stride);
2167
2168#if CONFIG_VP4_DECODER
2169 if (s->version >= 2) {
2170 uint8_t *temp = s->edge_emu_buffer;
2171 if (stride < 0)
2172 temp -= 8 * stride;
2173 if (vp4_mc_loop_filter(s, plane, motion_val[fragment][0], motion_val[fragment][1], x, y, motion_source, stride, src_x, src_y, temp)) {
2174 motion_source = temp;
2175 standard_mc = 0;
2176 }
2177 }
2178#endif
2179
2180 if (standard_mc && (
2181 src_x < 0 || src_y < 0 ||
2182 src_x + 9 >= plane_width ||
2183 src_y + 9 >= plane_height)) {
2184 uint8_t *temp = s->edge_emu_buffer;
2185 if (stride < 0)
2186 temp -= 8 * stride;
2187
2188 s->vdsp.emulated_edge_mc(temp, motion_source,
2189 stride, stride,
2190 9, 9, src_x, src_y,
2191 plane_width,
2192 plane_height);
2193 motion_source = temp;
2194 }
2195 }
2196
2197 /* first, take care of copying a block from either the
2198 * previous or the golden frame */
2199 if (s->all_fragments[i].coding_method != MODE_INTRA) {
2200 /* Note, it is possible to implement all MC cases
2201 * with put_no_rnd_pixels_l2 which would look more
2202 * like the VP3 source but this would be slower as
2203 * put_no_rnd_pixels_tab is better optimized */
2204 if (motion_halfpel_index != 3) {
2205 s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
2206 output_plane + first_pixel,
2207 motion_source, stride, 8);
2208 } else {
2209 /* d is 0 if motion_x and _y have the same sign,
2210 * else -1 */
2211 int d = (motion_x ^ motion_y) >> 31;
2212 s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
2213 motion_source - d,
2214 motion_source + stride + 1 + d,
2215 stride, 8);
2216 }
2217 }
2218
2219 /* invert DCT and place (or add) in final output */
2220
2221 if (s->all_fragments[i].coding_method == MODE_INTRA) {
2222 vp3_dequant(s, s->all_fragments + i,
2223 plane, 0, block);
2224 s->vp3dsp.idct_put(output_plane + first_pixel,
2225 stride,
2226 block);
2227 } else {
2228 if (vp3_dequant(s, s->all_fragments + i,
2229 plane, 1, block)) {
2230 s->vp3dsp.idct_add(output_plane + first_pixel,
2231 stride,
2232 block);
2233 } else {
2234 s->vp3dsp.idct_dc_add(output_plane + first_pixel,
2235 stride, block);
2236 }
2237 }
2238 } else {
2239 /* copy directly from the previous frame */
2240 s->hdsp.put_pixels_tab[1][0](
2241 output_plane + first_pixel,
2242 last_plane + first_pixel,
2243 stride, 8);
2244 }
2245 }
2246 }
2247
2248 // Filter up to the last row in the superblock row
2249 if (s->version < 2 && !s->skip_loop_filter)
2250 apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
2251 FFMIN(4 * sb_y + 3, fragment_height - 1));
2252 }
2253 }
2254
2255 /* this looks like a good place for slice dispatch... */
2256 /* algorithm:
2257 * if (slice == s->macroblock_height - 1)
2258 * dispatch (both last slice & 2nd-to-last slice);
2259 * else if (slice > 0)
2260 * dispatch (slice - 1);
2261 */
2262
2263 vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
2264 s->height - 16));
2265}
2266
2267static av_cold void init_tables_once(void)
2268{
2270
2274 NULL, 0, 0, 1, 0);
2275
2278 NULL, 0, 0, 0, 0);
2279
2281 &motion_vector_vlc_table[0][1], 2,
2282 &motion_vector_vlc_table[0][0], 2, 1,
2283 -31, 0);
2284
2287 NULL, 0, 0, 0, 0);
2288
2289#if CONFIG_VP4_DECODER
2290 for (int j = 0; j < 2; j++)
2291 for (int i = 0; i < 7; i++) {
2292 vp4_mv_vlc_table[j][i] =
2294 &vp4_mv_vlc[j][i][0][1], 2,
2295 &vp4_mv_vlc[j][i][0][0], 2, 1,
2296 -31, 0);
2297 }
2298
2299 /* version >= 2 */
2300 for (int i = 0; i < 2; i++) {
2301 block_pattern_vlc[i] =
2302 ff_vlc_init_tables(&state, 5, 14,
2303 &vp4_block_pattern_vlc[i][0][1], 2, 1,
2304 &vp4_block_pattern_vlc[i][0][0], 2, 1, 0);
2305 }
2306#endif
2307}
2308
2309/// Allocate tables for per-frame data in Vp3DecodeContext
2311{
2312 Vp3DecodeContext *s = avctx->priv_data;
2313 int y_fragment_count, c_fragment_count;
2314
2315 free_tables(avctx);
2316
2317 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2318 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2319
2320 /* superblock_coding is used by unpack_superblocks (VP3/Theora) and vp4_unpack_macroblocks (VP4) */
2321 s->superblock_coding = av_mallocz(FFMAX(s->superblock_count, s->yuv_macroblock_count));
2322 s->all_fragments = av_calloc(s->fragment_count, sizeof(*s->all_fragments));
2323
2324 s-> kf_coded_fragment_list = av_calloc(s->fragment_count, sizeof(int));
2325 s->nkf_coded_fragment_list = av_calloc(s->fragment_count, sizeof(int));
2326 memset(s-> num_kf_coded_fragment, -1, sizeof(s-> num_kf_coded_fragment));
2327
2328 s->dct_tokens_base = av_calloc(s->fragment_count,
2329 64 * sizeof(*s->dct_tokens_base));
2330 s->motion_val[0] = av_calloc(y_fragment_count, sizeof(*s->motion_val[0]));
2331 s->motion_val[1] = av_calloc(c_fragment_count, sizeof(*s->motion_val[1]));
2332
2333 /* work out the block mapping tables */
2334 s->superblock_fragments = av_calloc(s->superblock_count, 16 * sizeof(int));
2335 s->macroblock_coding = av_mallocz(s->macroblock_count + 1);
2336
2337 s->dc_pred_row = av_malloc_array(s->y_superblock_width * 4, sizeof(*s->dc_pred_row));
2338
2339 if (!s->superblock_coding || !s->all_fragments ||
2340 !s->dct_tokens_base || !s->kf_coded_fragment_list ||
2341 !s->nkf_coded_fragment_list ||
2342 !s->superblock_fragments || !s->macroblock_coding ||
2343 !s->dc_pred_row ||
2344 !s->motion_val[0] || !s->motion_val[1]) {
2345 return -1;
2346 }
2347
2349
2350 return 0;
2351}
2352
2353
2355{
2356 CoeffVLCs *vlcs = obj;
2357
2358 for (int i = 0; i < FF_ARRAY_ELEMS(vlcs->vlcs); i++)
2359 ff_vlc_free(&vlcs->vlcs[i]);
2360}
2361
2363{
2364 static AVOnce init_static_once = AV_ONCE_INIT;
2365 Vp3DecodeContext *s = avctx->priv_data;
2366 int ret;
2367 int c_width;
2368 int c_height;
2369 int y_fragment_count, c_fragment_count;
2370
2371 if (avctx->codec_tag == MKTAG('V', 'P', '4', '0')) {
2372 s->version = 3;
2373#if !CONFIG_VP4_DECODER
2374 av_log(avctx, AV_LOG_ERROR, "This build does not support decoding VP4.\n");
2376#endif
2377 } else if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
2378 s->version = 0;
2379 else
2380 s->version = 1;
2381
2382 s->avctx = avctx;
2383 s->width = FFALIGN(avctx->coded_width, 16);
2384 s->height = FFALIGN(avctx->coded_height, 16);
2385 if (s->width < 18)
2386 return AVERROR_PATCHWELCOME;
2387 if (avctx->codec_id != AV_CODEC_ID_THEORA)
2388 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2391 ff_videodsp_init(&s->vdsp, 8);
2392 ff_vp3dsp_init(&s->vp3dsp);
2393
2394 for (int i = 0; i < 64; i++) {
2395#define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
2396 s->idct_permutation[i] = TRANSPOSE(i);
2397 s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
2398#undef TRANSPOSE
2399 }
2400
2401 /* initialize to an impossible value which will force a recalculation
2402 * in the first frame decode */
2403 for (int i = 0; i < 3; i++)
2404 s->qps[i] = -1;
2405
2406 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
2407 if (ret)
2408 return ret;
2409
2410 s->y_superblock_width = (s->width + 31) / 32;
2411 s->y_superblock_height = (s->height + 31) / 32;
2412 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2413
2414 /* work out the dimensions for the C planes */
2415 c_width = s->width >> s->chroma_x_shift;
2416 c_height = s->height >> s->chroma_y_shift;
2417 s->c_superblock_width = (c_width + 31) / 32;
2418 s->c_superblock_height = (c_height + 31) / 32;
2419 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2420
2421 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
2422 s->u_superblock_start = s->y_superblock_count;
2423 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
2424
2425 s->macroblock_width = (s->width + 15) / 16;
2426 s->macroblock_height = (s->height + 15) / 16;
2427 s->macroblock_count = s->macroblock_width * s->macroblock_height;
2428 s->c_macroblock_width = (c_width + 15) / 16;
2429 s->c_macroblock_height = (c_height + 15) / 16;
2430 s->c_macroblock_count = s->c_macroblock_width * s->c_macroblock_height;
2431 s->yuv_macroblock_count = s->macroblock_count + 2 * s->c_macroblock_count;
2432
2433 s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
2434 s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
2435 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
2436 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
2437
2438 /* fragment count covers all 8x8 blocks for all 3 planes */
2439 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2440 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2441 s->fragment_count = y_fragment_count + 2 * c_fragment_count;
2442 s->fragment_start[1] = y_fragment_count;
2443 s->fragment_start[2] = y_fragment_count + c_fragment_count;
2444
2445 if (!s->theora_tables) {
2446 for (int i = 0; i < 64; i++) {
2447 s->coded_dc_scale_factor[0][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_y_dc_scale_factor[i];
2448 s->coded_dc_scale_factor[1][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_uv_dc_scale_factor[i];
2449 s->coded_ac_scale_factor[i] = s->version < 2 ? vp31_ac_scale_factor[i] : vp4_ac_scale_factor[i];
2450 s->base_matrix[0][i] = s->version < 2 ? vp31_intra_y_dequant[i] : vp4_generic_dequant[i];
2451 s->base_matrix[1][i] = s->version < 2 ? ff_mjpeg_std_chrominance_quant_tbl[i] : vp4_generic_dequant[i];
2452 s->base_matrix[2][i] = s->version < 2 ? vp31_inter_dequant[i] : vp4_generic_dequant[i];
2453 s->filter_limit_values[i] = s->version < 2 ? vp31_filter_limit_values[i] : vp4_filter_limit_values[i];
2454 }
2455
2456 for (int inter = 0; inter < 2; inter++) {
2457 for (int plane = 0; plane < 3; plane++) {
2458 s->qr_count[inter][plane] = 1;
2459 s->qr_size[inter][plane][0] = 63;
2460 s->qr_base[inter][plane][0] =
2461 s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
2462 }
2463 }
2464 }
2465
2466 /* The first thread's tables may be another link's than these headers'. */
2467 if (s->headers ||
2469 CoeffVLCs *vlcs = av_refstruct_alloc_ext(sizeof(*s->coeff_vlc), 0,
2471 if (!vlcs)
2472 return AVERROR(ENOMEM);
2473
2474 s->coeff_vlc = vlcs;
2475
2476 if (!s->theora_tables) {
2477 const uint8_t (*bias_tabs)[32][2];
2478
2479 /* init VLC tables */
2480 bias_tabs = CONFIG_VP4_DECODER && s->version >= 2 ? vp4_bias : vp3_bias;
2481 for (int i = 0; i < FF_ARRAY_ELEMS(vlcs->vlcs); i++) {
2482 ret = ff_vlc_init_from_lengths(&vlcs->vlcs[i], 11, 32,
2483 &bias_tabs[i][0][1], 2,
2484 &bias_tabs[i][0][0], 2, 1,
2485 0, 0, avctx);
2486 if (ret < 0)
2487 return ret;
2488 vlcs->vlc_tabs[i] = vlcs->vlcs[i].table;
2489 }
2490 } else {
2491 for (int i = 0; i < FF_ARRAY_ELEMS(vlcs->vlcs); i++) {
2492 const HuffTable *tab = &s->huffman_table[i];
2493
2494 ret = ff_vlc_init_from_lengths(&vlcs->vlcs[i], 11, tab->nb_entries,
2495 &tab->entries[0].len, sizeof(*tab->entries),
2496 &tab->entries[0].sym, sizeof(*tab->entries), 1,
2497 0, 0, avctx);
2498 if (ret < 0)
2499 return ret;
2500 vlcs->vlc_tabs[i] = vlcs->vlcs[i].table;
2501 }
2502 }
2503 }
2504
2505 ff_thread_once(&init_static_once, init_tables_once);
2506
2507 return allocate_tables(avctx);
2508}
2509
2510#if HAVE_THREADS
2511static void ref_frames(Vp3DecodeContext *dst, const Vp3DecodeContext *src)
2512{
2513 ff_progress_frame_replace(&dst->current_frame, &src->current_frame);
2514 ff_progress_frame_replace(&dst->golden_frame, &src->golden_frame);
2515}
2516
2517static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
2518{
2519 Vp3DecodeContext *s = dst->priv_data;
2520 const Vp3DecodeContext *s1 = src->priv_data;
2521 int qps_changed = 0;
2522
2523#if CONFIG_THEORA_DECODER
2524 /* A thread before this one took up the headers of a new logical stream;
2525 * if they fail, data packets are rejected until a packet of this thread
2526 * brings new ones. */
2527 if (s1->headers && s->headers != s1->headers)
2529#endif
2530
2531 // copy previous frame data
2532 ref_frames(s, s1);
2533
2534 if (s != s1) {
2535 // copy qscale data if necessary
2536 for (int i = 0; i < 3; i++) {
2537 if (s->qps[i] != s1->qps[i]) {
2538 qps_changed = 1;
2539 memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
2540 }
2541 }
2542
2543 if (s->qps[0] != s1->qps[0])
2544 memcpy(&s->bounding_values_array, &s1->bounding_values_array,
2545 sizeof(s->bounding_values_array));
2546
2547 if (qps_changed) {
2548 memcpy(s->qps, s1->qps, sizeof(s->qps));
2549 s->nqps = s1->nqps;
2550 }
2551 }
2552 return 0;
2553}
2554#endif
2555
2557 int *got_frame, AVPacket *avpkt)
2558{
2559 const uint8_t *buf = avpkt->data;
2560 int buf_size = avpkt->size;
2561 Vp3DecodeContext *s = avctx->priv_data;
2562 GetBitContext gb;
2563 int ret;
2564
2565#if CONFIG_THEORA_DECODER
2566 if (s->theora) {
2567 size_t new_headers_size;
2568 const uint8_t *new_headers = av_packet_get_side_data(avpkt,
2570 &new_headers_size);
2571 if (new_headers) {
2572 uint8_t *headers;
2573
2574 if (new_headers_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
2575 return AVERROR_INVALIDDATA;
2576 headers = av_refstruct_allocz(new_headers_size +
2578 if (!headers)
2579 return AVERROR(ENOMEM);
2580 memcpy(headers, new_headers, new_headers_size);
2581 ret = theora_reinit(avctx, headers, new_headers_size);
2582 av_refstruct_unref(&headers);
2583 if (ret < 0)
2584 return ret;
2585 }
2586 }
2587#endif
2588
2589 if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
2590 return ret;
2591
2592#if CONFIG_THEORA_DECODER
2593 if (s->theora && get_bits1(&gb)) {
2594 int type = get_bits(&gb, 7);
2595 skip_bits_long(&gb, 6*8); /* "theora" */
2596
2597 if (s->avctx->active_thread_type&FF_THREAD_FRAME) {
2598 av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2599 return AVERROR_PATCHWELCOME;
2600 }
2601 if (type == 0) {
2602 vp3_decode_end(avctx);
2603 ret = theora_decode_header(avctx, &gb);
2604
2605 if (ret >= 0)
2606 ret = vp3_decode_init(avctx);
2607 if (ret < 0) {
2608 vp3_decode_end(avctx);
2609 return ret;
2610 }
2611 return buf_size;
2612 } else if (type == 2) {
2613 vp3_decode_end(avctx);
2614 ret = theora_decode_tables(avctx, &gb);
2615 if (ret >= 0)
2616 ret = vp3_decode_init(avctx);
2617 if (ret < 0) {
2618 vp3_decode_end(avctx);
2619 return ret;
2620 }
2621 return buf_size;
2622 }
2623
2624 av_log(avctx, AV_LOG_ERROR,
2625 "Header packet passed to frame decoder, skipping\n");
2626 return -1;
2627 }
2628#endif
2629
2630 s->keyframe = !get_bits1(&gb);
2631 if (!s->all_fragments) {
2632 av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
2633 return -1;
2634 }
2635 if (!s->theora)
2636 skip_bits(&gb, 1);
2637
2638 int last_qps[3];
2639 for (int i = 0; i < 3; i++)
2640 last_qps[i] = s->qps[i];
2641
2642 s->nqps = 0;
2643 do {
2644 s->qps[s->nqps++] = get_bits(&gb, 6);
2645 } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
2646 for (int i = s->nqps; i < 3; i++)
2647 s->qps[i] = -1;
2648
2649 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2650 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%"PRId64": Q index = %d\n",
2651 s->keyframe ? "key" : "", avctx->frame_num + 1, s->qps[0]);
2652
2653 s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
2654 avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
2656
2657 if (s->qps[0] != last_qps[0])
2659
2660 for (int i = 0; i < s->nqps; i++)
2661 // reinit all dequantizers if the first one changed, because
2662 // the DC of the first quantizer must be used for all matrices
2663 if (s->qps[i] != last_qps[i] || s->qps[0] != last_qps[0])
2665
2666 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2667 return buf_size;
2668
2669 ret = ff_progress_frame_get_buffer(avctx, &s->last_frame,
2671 if (ret < 0) {
2672 // Don't goto error here, as one can't report progress on or
2673 // unref a non-existent frame.
2674 return ret;
2675 }
2676 FFSWAP(ProgressFrame, s->last_frame, s->current_frame);
2677 s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2679 if (s->keyframe)
2680 s->current_frame.f->flags |= AV_FRAME_FLAG_KEY;
2681 else
2682 s->current_frame.f->flags &= ~AV_FRAME_FLAG_KEY;
2683
2684 if (!s->edge_emu_buffer) {
2685 s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
2686 if (!s->edge_emu_buffer) {
2687 ret = AVERROR(ENOMEM);
2688 goto error;
2689 }
2690 }
2691
2692 if (s->keyframe) {
2693 if (!s->theora) {
2694 skip_bits(&gb, 4); /* width code */
2695 skip_bits(&gb, 4); /* height code */
2696 if (s->version) {
2697 int version = get_bits(&gb, 5);
2698#if !CONFIG_VP4_DECODER
2699 if (version >= 2) {
2700 av_log(avctx, AV_LOG_ERROR, "This build does not support decoding VP4.\n");
2702 goto error;
2703 }
2704#endif
2705 s->version = version;
2706 if (avctx->frame_num == 0)
2707 av_log(s->avctx, AV_LOG_DEBUG,
2708 "VP version: %d\n", s->version);
2709 }
2710 }
2711 if (s->version || s->theora) {
2712 if (get_bits1(&gb))
2713 av_log(s->avctx, AV_LOG_ERROR,
2714 "Warning, unsupported keyframe coding type?!\n");
2715 skip_bits(&gb, 2); /* reserved? */
2716
2717#if CONFIG_VP4_DECODER
2718 if (s->version >= 2) {
2719 int mb_height, mb_width;
2720 int mb_width_mul, mb_width_div, mb_height_mul, mb_height_div;
2721
2722 mb_height = get_bits(&gb, 8);
2723 mb_width = get_bits(&gb, 8);
2724 if (mb_height != s->macroblock_height ||
2725 mb_width != s->macroblock_width)
2726 avpriv_request_sample(s->avctx, "macroblock dimension mismatch");
2727
2728 mb_width_mul = get_bits(&gb, 5);
2729 mb_width_div = get_bits(&gb, 3);
2730 mb_height_mul = get_bits(&gb, 5);
2731 mb_height_div = get_bits(&gb, 3);
2732 if (mb_width_mul != 1 || mb_width_div != 1 || mb_height_mul != 1 || mb_height_div != 1)
2733 avpriv_request_sample(s->avctx, "unexpected macroblock dimension multiplier/divider");
2734
2735 if (get_bits(&gb, 2))
2736 avpriv_request_sample(s->avctx, "unknown bits");
2737 }
2738#endif
2739 }
2740 ff_progress_frame_replace(&s->golden_frame, &s->current_frame);
2741 } else {
2742 if (!s->golden_frame.f) {
2743 av_log(s->avctx, AV_LOG_WARNING,
2744 "vp3: first frame not a keyframe\n");
2745
2746 if ((ret = ff_progress_frame_get_buffer(avctx, &s->golden_frame,
2748 goto error;
2749 s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
2750 ff_progress_frame_replace(&s->last_frame, &s->golden_frame);
2751 ff_progress_frame_report(&s->golden_frame, INT_MAX);
2752 }
2753 }
2755
2756 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2757
2758 if (s->version < 2) {
2759 if ((ret = unpack_superblocks(s, &gb)) < 0) {
2760 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2761 goto error;
2762 }
2763#if CONFIG_VP4_DECODER
2764 } else {
2765 if ((ret = vp4_unpack_macroblocks(s, &gb)) < 0) {
2766 av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_macroblocks\n");
2767 goto error;
2768 }
2769#endif
2770 }
2771 if ((ret = unpack_modes(s, &gb)) < 0) {
2772 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2773 goto error;
2774 }
2775 if (ret = unpack_vectors(s, &gb)) {
2776 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2777 goto error;
2778 }
2779 if ((ret = unpack_block_qpis(s, &gb)) < 0) {
2780 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2781 goto error;
2782 }
2783
2784 if (s->version < 2) {
2785 if ((ret = unpack_dct_coeffs(s, &gb)) < 0) {
2786 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2787 goto error;
2788 }
2789#if CONFIG_VP4_DECODER
2790 } else {
2791 if ((ret = vp4_unpack_dct_coeffs(s, &gb)) < 0) {
2792 av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_dct_coeffs\n");
2793 goto error;
2794 }
2795#endif
2796 }
2797
2798 for (int i = 0; i < 3; i++) {
2799 int height = s->height >> (i && s->chroma_y_shift);
2800 if (s->flipped_image)
2801 s->data_offset[i] = 0;
2802 else
2803 s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
2804 }
2805
2806 s->last_slice_end = 0;
2807 for (int i = 0; i < s->c_superblock_height; i++)
2808 render_slice(s, i);
2809
2810 // filter the last row
2811 if (s->version < 2)
2812 for (int i = 0; i < 3; i++) {
2813 int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
2814 apply_loop_filter(s, i, row, row + 1);
2815 }
2816 vp3_draw_horiz_band(s, s->height);
2817
2818 ff_progress_frame_unref(&s->last_frame);
2819
2820 /* output frame, offset as needed */
2821 if ((ret = av_frame_ref(frame, s->current_frame.f)) < 0)
2822 return ret;
2823
2824 frame->crop_left = s->offset_x;
2825 frame->crop_right = avctx->coded_width - avctx->width - s->offset_x;
2826 frame->crop_top = s->offset_y;
2827 frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y;
2828
2829 *got_frame = 1;
2830
2831 return buf_size;
2832
2833error:
2834 ff_progress_frame_report(&s->current_frame, INT_MAX);
2835 ff_progress_frame_unref(&s->last_frame);
2836
2837 return ret;
2838}
2839
2840static int read_huffman_tree(HuffTable *huff, GetBitContext *gb, int length,
2841 AVCodecContext *avctx)
2842{
2843 if (get_bits1(gb)) {
2844 int token;
2845 if (huff->nb_entries >= 32) { /* overflow */
2846 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2847 return -1;
2848 }
2849 token = get_bits(gb, 5);
2850 ff_dlog(avctx, "code length %d, curr entry %d, token %d\n",
2851 length, huff->nb_entries, token);
2852 huff->entries[huff->nb_entries++] = (HuffEntry){ length, token };
2853 } else {
2854 /* The following bound follows from the fact that nb_entries <= 32. */
2855 if (length >= 31) { /* overflow */
2856 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2857 return -1;
2858 }
2859 length++;
2860 if (read_huffman_tree(huff, gb, length, avctx))
2861 return -1;
2862 if (read_huffman_tree(huff, gb, length, avctx))
2863 return -1;
2864 }
2865 return 0;
2866}
2867
2868#if CONFIG_THEORA_DECODER
2869static const enum AVPixelFormat theora_pix_fmts[4] = {
2871};
2872
2874{
2875 Vp3DecodeContext *s = avctx->priv_data;
2876 int visible_width, visible_height, colorspace;
2877 uint8_t offset_x = 0, offset_y = 0;
2878 int ret;
2879 AVRational fps, aspect;
2880
2881 if (get_bits_left(gb) < 206)
2882 return AVERROR_INVALIDDATA;
2883
2884 s->theora_header = 0;
2885 s->theora = get_bits(gb, 24);
2886 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2887 if (!s->theora) {
2888 s->theora = 1;
2889 avpriv_request_sample(s->avctx, "theora 0");
2890 }
2891
2892 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
2893 * but previous versions have the image flipped relative to vp3 */
2894 if (s->theora < 0x030200) {
2895 s->flipped_image = 1;
2896 av_log(avctx, AV_LOG_DEBUG,
2897 "Old (<alpha3) Theora bitstream, flipped image\n");
2898 }
2899
2900 visible_width =
2901 s->width = get_bits(gb, 16) << 4;
2902 visible_height =
2903 s->height = get_bits(gb, 16) << 4;
2904
2905 if (s->theora >= 0x030200) {
2906 visible_width = get_bits(gb, 24);
2907 visible_height = get_bits(gb, 24);
2908
2909 offset_x = get_bits(gb, 8); /* offset x */
2910 offset_y = get_bits(gb, 8); /* offset y, from bottom */
2911 }
2912
2913 /* sanity check */
2914 if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 ||
2915 visible_width + offset_x > s->width ||
2916 visible_height + offset_y > s->height ||
2917 visible_width + 512 < s->width ||
2918 visible_height + 512 < s->height ||
2919 visible_width < 18
2920 ) {
2921 av_log(avctx, AV_LOG_ERROR,
2922 "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
2923 visible_width, visible_height, offset_x, offset_y,
2924 s->width, s->height);
2925 return AVERROR_INVALIDDATA;
2926 }
2927
2928 fps.num = get_bits_long(gb, 32);
2929 fps.den = get_bits_long(gb, 32);
2930 if (fps.num && fps.den) {
2931 if (fps.num < 0 || fps.den < 0) {
2932 av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
2933 return AVERROR_INVALIDDATA;
2934 }
2935 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
2936 fps.den, fps.num, 1 << 30);
2937 }
2938
2939 aspect.num = get_bits(gb, 24);
2940 aspect.den = get_bits(gb, 24);
2941 if (aspect.num && aspect.den) {
2943 &avctx->sample_aspect_ratio.den,
2944 aspect.num, aspect.den, 1 << 30);
2945 ff_set_sar(avctx, avctx->sample_aspect_ratio);
2946 }
2947
2948 if (s->theora < 0x030200)
2949 skip_bits(gb, 5); /* keyframe frequency force */
2950 colorspace = get_bits(gb, 8);
2951 skip_bits(gb, 24); /* bitrate */
2952
2953 skip_bits(gb, 6); /* quality hint */
2954
2955 if (s->theora >= 0x030200) {
2956 skip_bits(gb, 5); /* keyframe frequency force */
2957 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
2958 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
2959 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
2960 return AVERROR_INVALIDDATA;
2961 }
2962 skip_bits(gb, 3); /* reserved */
2963 } else
2964 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2965
2966 if (s->width < 18)
2967 return AVERROR_PATCHWELCOME;
2968 ret = ff_set_dimensions(avctx, s->width, s->height);
2969 if (ret < 0)
2970 return ret;
2971 if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP)) {
2972 avctx->width = visible_width;
2973 avctx->height = visible_height;
2974 // translate offsets from theora axis ([0,0] lower left)
2975 // to normal axis ([0,0] upper left)
2976 s->offset_x = offset_x;
2977 s->offset_y = s->height - visible_height - offset_y;
2978 }
2979
2980 if (colorspace == 1)
2982 else if (colorspace == 2)
2984
2985 if (colorspace == 1 || colorspace == 2) {
2987 avctx->color_trc = AVCOL_TRC_BT709;
2988 }
2989
2990 s->theora_header = 1;
2991 return 0;
2992}
2993
2995{
2996 Vp3DecodeContext *s = avctx->priv_data;
2997 int n, matrices, ret;
2998
2999 if (!s->theora_header)
3000 return AVERROR_INVALIDDATA;
3001
3002 if (s->theora >= 0x030200) {
3003 n = get_bits(gb, 3);
3004 /* loop filter limit values table */
3005 if (n)
3006 for (int i = 0; i < 64; i++)
3007 s->filter_limit_values[i] = get_bits(gb, n);
3008 }
3009
3010 if (s->theora >= 0x030200)
3011 n = get_bits(gb, 4) + 1;
3012 else
3013 n = 16;
3014 /* quality threshold table */
3015 for (int i = 0; i < 64; i++)
3016 s->coded_ac_scale_factor[i] = get_bits(gb, n);
3017
3018 if (s->theora >= 0x030200)
3019 n = get_bits(gb, 4) + 1;
3020 else
3021 n = 16;
3022 /* dc scale factor table */
3023 for (int i = 0; i < 64; i++)
3024 s->coded_dc_scale_factor[0][i] =
3025 s->coded_dc_scale_factor[1][i] = get_bits(gb, n);
3026
3027 if (s->theora >= 0x030200)
3028 matrices = get_bits(gb, 9) + 1;
3029 else
3030 matrices = 3;
3031
3032 if (matrices > 384) {
3033 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
3034 return -1;
3035 }
3036
3037 for (int j = 0; j < matrices; j++)
3038 for (int i = 0; i < 64; i++)
3039 s->base_matrix[j][i] = get_bits(gb, 8);
3040
3041 for (int inter = 0; inter <= 1; inter++) {
3042 for (int plane = 0; plane <= 2; plane++) {
3043 int newqr = 1;
3044 if (inter || plane > 0)
3045 newqr = get_bits1(gb);
3046 if (!newqr) {
3047 int qtj, plj;
3048 if (inter && get_bits1(gb)) {
3049 qtj = 0;
3050 plj = plane;
3051 } else {
3052 qtj = (3 * inter + plane - 1) / 3;
3053 plj = (plane + 2) % 3;
3054 }
3055 s->qr_count[inter][plane] = s->qr_count[qtj][plj];
3056 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
3057 sizeof(s->qr_size[0][0]));
3058 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
3059 sizeof(s->qr_base[0][0]));
3060 } else {
3061 int qri = 0;
3062 int qi = 0;
3063
3064 for (;;) {
3065 int i = get_bits(gb, av_log2(matrices - 1) + 1);
3066 if (i >= matrices) {
3067 av_log(avctx, AV_LOG_ERROR,
3068 "invalid base matrix index\n");
3069 return -1;
3070 }
3071 s->qr_base[inter][plane][qri] = i;
3072 if (qi >= 63)
3073 break;
3074 i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
3075 s->qr_size[inter][plane][qri++] = i;
3076 qi += i;
3077 }
3078
3079 if (qi > 63) {
3080 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
3081 return -1;
3082 }
3083 s->qr_count[inter][plane] = qri;
3084 }
3085 }
3086 }
3087
3088 /* Huffman tables */
3089 for (int i = 0; i < FF_ARRAY_ELEMS(s->huffman_table); i++) {
3090 s->huffman_table[i].nb_entries = 0;
3091 if ((ret = read_huffman_tree(&s->huffman_table[i], gb, 0, avctx)) < 0)
3092 return ret;
3093 }
3094
3095 s->theora_tables = 1;
3096
3097 return 0;
3098}
3099
3100static av_cold int theora_init_from_headers(AVCodecContext *avctx,
3101 const uint8_t *headers,
3102 int headers_size)
3103{
3104 Vp3DecodeContext *s = avctx->priv_data;
3105 GetBitContext gb;
3106 int ptype;
3107 const uint8_t *header_start[3];
3108 int header_len[3];
3109 int ret;
3110
3111 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
3112
3113 s->theora = 1;
3114
3115 if (!headers_size) {
3116 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
3117 return -1;
3118 }
3119
3120 if (avpriv_split_xiph_headers(headers, headers_size,
3121 42, header_start, header_len) < 0) {
3122 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
3123 return -1;
3124 }
3125
3126 for (int i = 0; i < 3; i++) {
3127 if (header_len[i] <= 0)
3128 continue;
3129 ret = init_get_bits8(&gb, header_start[i], header_len[i]);
3130 if (ret < 0)
3131 return ret;
3132
3133 ptype = get_bits(&gb, 8);
3134
3135 if (!(ptype & 0x80)) {
3136 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
3137// return -1;
3138 }
3139
3140 // FIXME: Check for this as well.
3141 skip_bits_long(&gb, 6 * 8); /* "theora" */
3142
3143 switch (ptype) {
3144 case 0x80:
3145 if (theora_decode_header(avctx, &gb) < 0)
3146 return -1;
3147 break;
3148 case 0x81:
3149// FIXME: is this needed? it breaks sometimes
3150// theora_decode_comments(avctx, gb);
3151 break;
3152 case 0x82:
3153 if (theora_decode_tables(avctx, &gb))
3154 return -1;
3155 break;
3156 default:
3157 av_log(avctx, AV_LOG_ERROR,
3158 "Unknown Theora config packet: %d\n", ptype & ~0x80);
3159 break;
3160 }
3161 if (ptype != 0x81 && get_bits_left(&gb) >= 8U)
3162 av_log(avctx, AV_LOG_WARNING,
3163 "%d bits left in packet %X\n",
3164 get_bits_left(&gb), ptype);
3165 if (s->theora < 0x030200)
3166 break;
3167 }
3168
3169 return vp3_decode_init(avctx);
3170}
3171
3172static av_cold int theora_decode_init(AVCodecContext *avctx)
3173{
3174 return theora_init_from_headers(avctx, avctx->extradata,
3175 avctx->extradata_size);
3176}
3177
3178/**
3179 * Set the decoder up again from the headers of a new logical stream, taking
3180 * a reference to them.
3181 */
3182static int theora_reinit(AVCodecContext *avctx, uint8_t *headers,
3183 int headers_size)
3184{
3185 Vp3DecodeContext *s = avctx->priv_data;
3186 int ret;
3187
3188 vp3_decode_end(avctx);
3189 av_refstruct_replace(&s->headers, headers);
3190 s->headers_size = headers_size;
3191 ret = theora_init_from_headers(avctx, s->headers, s->headers_size);
3192 if (ret < 0) {
3193 vp3_decode_end(avctx);
3194 /* Kept, so that the next frame thread rejects this stream too. */
3195 av_refstruct_replace(&s->headers, headers);
3196 }
3197
3198 return ret;
3199}
3200
3201const FFCodec ff_theora_decoder = {
3202 .p.name = "theora",
3203 CODEC_LONG_NAME("Theora"),
3204 .p.type = AVMEDIA_TYPE_VIDEO,
3205 .p.id = AV_CODEC_ID_THEORA,
3206 .priv_data_size = sizeof(Vp3DecodeContext),
3207 .init = theora_decode_init,
3212 .flush = vp3_decode_flush,
3213 UPDATE_THREAD_CONTEXT(vp3_update_thread_context),
3214 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
3217};
3218#endif
3219
3221 .p.name = "vp3",
3222 CODEC_LONG_NAME("On2 VP3"),
3223 .p.type = AVMEDIA_TYPE_VIDEO,
3224 .p.id = AV_CODEC_ID_VP3,
3225 .priv_data_size = sizeof(Vp3DecodeContext),
3231 .flush = vp3_decode_flush,
3232 UPDATE_THREAD_CONTEXT(vp3_update_thread_context),
3233 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
3235};
3236
3237#if CONFIG_VP4_DECODER
3238const FFCodec ff_vp4_decoder = {
3239 .p.name = "vp4",
3240 CODEC_LONG_NAME("On2 VP4"),
3241 .p.type = AVMEDIA_TYPE_VIDEO,
3242 .p.id = AV_CODEC_ID_VP4,
3243 .priv_data_size = sizeof(Vp3DecodeContext),
3249 .flush = vp3_decode_flush,
3250 UPDATE_THREAD_CONTEXT(vp3_update_thread_context),
3251 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
3253};
3254#endif
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
const FFCodec ff_theora_decoder
const FFCodec ff_vp3_decoder
Definition vp3.c:3220
const FFCodec ff_vp4_decoder
static const VLCElem * coeff_vlc[2][8][4]
Definition atrac9dec.c:110
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1600
#define FF_DEBUG_PICT_INFO
Definition avcodec.h:1399
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bit(string, value)
Definition cbs_mpeg2.c:56
#define s(width, name)
Definition cbs_vp9.c:198
#define UPDATE_THREAD_CONTEXT(func)
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
#define FF_CODEC_CAP_USES_PROGRESSFRAMES
The decoder might make use of the ProgressFrame API.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip
Definition common.h:100
#define RSHIFT(a, b)
Definition common.h:56
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define FFSIGN(a)
Definition common.h:75
#define NULL
Definition coverity.c:32
static int16_t block[64]
Definition dct.c:125
void ff_progress_frame_replace(ProgressFrame *dst, const ProgressFrame *src)
Do nothing if dst and src already refer to the same AVFrame; otherwise unreference dst and if src is ...
Definition decode.c:2006
void ff_progress_frame_await(const ProgressFrame *f, int n)
Wait for earlier decoding threads to finish reference frames.
Definition decode.c:2021
void ff_progress_frame_report(ProgressFrame *f, int n)
Notify later decoding threads when part of their reference frame is ready.
Definition decode.c:2016
enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
Allows to synchronize objects whose lifetime is the whole decoding process among all frame threads.
Definition decode.c:2027
int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int flags)
Wrapper around ff_progress_frame_alloc() and ff_thread_get_buffer().
Definition decode.c:1976
void ff_progress_frame_unref(ProgressFrame *f)
Give up a reference to the underlying frame contained in a ProgressFrame and reset the ProgressFrame,...
Definition decode.c:1999
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition utils.c:106
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
perm
Definition f_perms.c:75
static const uint8_t bits[8]
Definition fastaudio.c:100
int ret
Definition ffplay.c:391
static int loop
Definition ffplay.c:367
#define AV_NUM_DATA_POINTERS
Definition frame.h:480
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition get_bits.h:373
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition codec.h:41
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition avcodec.h:302
#define AV_CODEC_FLAG2_IGNORE_CROP
Discard cropping information from SPS.
Definition avcodec.h:355
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_VP3
Definition codec_id.h:79
@ AV_CODEC_ID_THEORA
Definition codec_id.h:80
@ AV_CODEC_ID_VP4
Definition codec_id.h:291
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AVDISCARD_ALL
discard all
Definition defs.h:248
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition defs.h:247
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition error.h:54
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:694
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:281
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition imgutils.c:318
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
#define PL(y)
static const int8_t transform[32][32]
Definition dsp.c:27
Half-pel DSP functions.
static VLCElem vlc_tables[VLC_TABLES_SIZE]
Definition imc.c:115
misc image utilities
static void output_plane(const Plane *plane, int buf_sel, uint8_t *dst, ptrdiff_t dst_pitch, int dst_height)
Convert and output the current plane.
Definition indeo3.c:1037
#define av_log2
Definition intmath.h:84
enum MPFType type
Definition jpegmpfenc.c:55
const uint8_t ff_mjpeg_std_chrominance_quant_tbl[64]
unsigned offset
Definition libaomenc.c:763
enum TrimAxis axis
Definition trim.c:49
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
#define u(width, name, range_min, range_max)
Definition cbs_apv.c:68
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition hpeldsp.c:337
Multithreading API for decoders.
@ FF_THREAD_IS_COPY
Definition thread.h:61
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition videodsp.c:39
void ff_vp3dsp_set_bounding_values(int *bounding_values_array, int filter_limit)
Definition vp3dsp.c:477
av_cold void ff_vp3dsp_init(VP3DSPContext *c)
Definition vp3dsp.c:448
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
version
Definition libkvazaar.c:313
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
const uint8_t ff_zigzag_direct[64]
Definition mathtables.c:137
static void body(uint32_t ABCD[4], const uint8_t *src, size_t nblocks)
Definition md5.c:103
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
static const char * obj
Definition mscl.c:57
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
#define av_malloc(s)
Definition ops_static.c:52
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition pixdesc.c:3512
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition pixfmt.h:814
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition pixfmt.h:654
@ AVCOL_PRI_BT470M
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition pixfmt.h:652
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition pixfmt.h:679
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition pixfmt.h:717
#define PU(x)
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition refstruct.c:161
static void * av_refstruct_allocz(size_t size)
Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
Definition refstruct.h:105
static void * av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition refstruct.h:94
#define FF_ARRAY_ELEMS(a)
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int width
picture width / height.
Definition avcodec.h:605
int flags2
AV_CODEC_FLAG2_*.
Definition avcodec.h:507
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:658
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1893
AVRational framerate
Definition avcodec.h:563
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition avcodec.h:629
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition avcodec.h:1663
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:672
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:665
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition avcodec.h:692
enum AVCodecID codec_id
Definition avcodec.h:453
int extradata_size
Definition avcodec.h:527
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:620
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1677
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
VLC vlcs[80]
Definition vp3.c:196
const VLCElem * vlc_tabs[80]
Definition vp3.c:195
Half-pel DSP context.
Definition hpeldsp.h:46
uint8_t len
Definition exr.c:96
uint16_t sym
Definition exr.c:97
HuffEntry entries[32]
Definition vp3.c:190
uint8_t nb_entries
Definition vp3.c:191
The ProgressFrame structure.
Definition vlc.h:32
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
Definition vlc.h:220
Definition vlc.h:50
VLCElem * table
Definition vlc.h:52
int dc
Definition vp3.c:179
int type
Definition vp3.c:180
uint16_t coded_dc_scale_factor[2][64]
Definition vp3.c:255
VideoDSPContext vdsp
Definition vp3.c:212
int num_coded_frags[3][64]
number of blocks that contain DCT coefficients at the given level or higher
Definition vp3.c:289
int macroblock_height
Definition vp3.c:235
CoeffVLCs * coeff_vlc
The first 16 of the following VLCs are for the dc coefficients; the others are four groups of 16 VLCs...
Definition vp3.c:305
ProgressFrame golden_frame
Definition vp3.c:205
int flipped_image
Definition vp3.c:215
int8_t(*[2] motion_val)[2]
Definition vp3.c:252
int theora_header
Definition vp3.c:201
uint8_t base_matrix[384][64]
Definition vp3.c:257
int c_superblock_width
Definition vp3.c:226
int theora_tables
Definition vp3.c:201
int c_superblock_count
Definition vp3.c:228
int * kf_coded_fragment_list
Definition vp3.c:296
int last_slice_end
Definition vp3.c:216
int offset_x_warned
Definition vp3.c:250
int chroma_y_shift
Definition vp3.c:204
VP4Predictor * dc_pred_row
Definition vp3.c:336
uint32_t coded_ac_scale_factor[64]
Definition vp3.c:256
int y_superblock_height
Definition vp3.c:224
int yuv_macroblock_count
Definition vp3.c:239
int16_t * dct_tokens_base
Definition vp3.c:280
int fragment_start[3]
Definition vp3.c:246
uint8_t idct_permutation[64]
Definition vp3.c:209
int16_t block[64]
Definition vp3.c:214
uint8_t * headers
Headers taken from packet side data, NULL while the codec extradata applies; a RefStruct reference,...
Definition vp3.c:311
AVCodecContext * avctx
Definition vp3.c:200
int chroma_x_shift
Definition vp3.c:204
ProgressFrame current_frame
Definition vp3.c:207
int c_macroblock_height
Definition vp3.c:238
unsigned char * macroblock_coding
Definition vp3.c:326
int c_macroblock_width
Definition vp3.c:237
uint8_t * edge_emu_buffer
Definition vp3.c:328
int c_superblock_height
Definition vp3.c:227
int fragment_width[2]
Definition vp3.c:242
int num_kf_coded_fragment[3]
Definition vp3.c:298
int skip_loop_filter
Definition vp3.c:217
int * coded_fragment_list[3]
Definition vp3.c:294
int bounding_values_array[256+4]
Definition vp3.c:334
int total_num_coded_frags
Definition vp3.c:290
HuffTable huffman_table[5 *16]
Definition vp3.c:331
int macroblock_width
Definition vp3.c:234
uint8_t filter_limit_values[64]
Definition vp3.c:333
int qps[3]
Definition vp3.c:219
int fragment_count
Definition vp3.c:241
int headers_size
Definition vp3.c:312
int16_t * dct_tokens[3][64]
This is a list of all tokens in bitstream order.
Definition vp3.c:279
ProgressFrame last_frame
Definition vp3.c:206
Vp3Fragment * all_fragments
Definition vp3.c:245
unsigned char * superblock_coding
Definition vp3.c:231
int macroblock_count
Definition vp3.c:233
int * nkf_coded_fragment_list
Definition vp3.c:297
uint8_t qr_count[2][3]
Definition vp3.c:258
uint8_t qr_size[2][3][64]
Definition vp3.c:259
int fragment_height[2]
Definition vp3.c:243
int y_superblock_width
Definition vp3.c:223
int16_t qmat[3][2][3][64]
qmat[qpi][is_inter][plane]
Definition vp3.c:316
uint8_t idct_scantable[64]
Definition vp3.c:210
HpelDSPContext hdsp
Definition vp3.c:211
int y_superblock_count
Definition vp3.c:225
uint8_t offset_y
Definition vp3.c:249
int superblock_count
Definition vp3.c:222
uint16_t qr_base[2][3][64]
Definition vp3.c:260
uint8_t offset_x
Definition vp3.c:248
VP3DSPContext vp3dsp
Definition vp3.c:213
int v_superblock_start
Definition vp3.c:230
int data_offset[3]
Definition vp3.c:247
int * superblock_fragments
Definition vp3.c:322
int c_macroblock_count
Definition vp3.c:236
int u_superblock_start
Definition vp3.c:229
uint8_t qpi
Definition vp3.c:70
int16_t dc
Definition vp3.c:68
uint8_t coding_method
Definition vp3.c:69
#define stride
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define ff_dlog(a,...)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
static struct @232240247175332074307271266321204261331170211232 state
static const uint8_t *const tables[]
static const struct twinvq_data tab
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition refstruct.h:58
else temp
Definition vf_mcdeint.c:275
Core video DSP helper functions.
int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition vlc.c:306
av_cold const VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
Definition vlc.c:366
void ff_vlc_free(VLC *vlc)
Definition vlc.c:580
#define VLC_INIT_STATE(_table)
Definition vlc.h:225
#define VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vlc_table, nb_bits, nb_codes, lens, lens_wrap, syms, syms_wrap, syms_size, offset, flags)
Definition vlc.h:288
static const VLCElem * ff_vlc_init_tables(VLCInitState *state, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, int flags)
Definition vlc.h:254
static int vp4_get_mv(GetBitContext *gb, int axis, int last_motion)
Definition vp3.c:896
static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
#define SB_FULLY_CODED
Definition vp3.c:75
static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
Definition vp3.c:1109
@ VP4_DC_GOLDEN
Definition vp3.c:150
@ VP4_DC_INTER
Definition vp3.c:149
@ VP4_DC_UNDEFINED
Definition vp3.c:152
@ VP4_DC_INTRA
Definition vp3.c:148
@ NB_VP4_DC_TYPES
Definition vp3.c:151
#define CODING_MODE_COUNT
Definition vp3.c:90
#define MODE_INTRA
Definition vp3.c:83
#define DC_COEFF(u)
Definition vp3.c:1641
#define MODE_GOLDEN_MV
Definition vp3.c:88
#define BLOCK_X
Definition vp3.c:650
static av_cold int vp3_decode_init(AVCodecContext *avctx)
Definition vp3.c:2362
#define COMPATIBLE_FRAME(x)
Definition vp3.c:1639
static void init_loop_filter(Vp3DecodeContext *s)
Definition vp3.c:468
#define MODE_INTER_PRIOR_LAST
Definition vp3.c:86
static int get_eob_run(GetBitContext *gb, int token)
Definition vp3.c:1152
static void reverse_dc_prediction(Vp3DecodeContext *s, int first_fragment, int fragment_width, int fragment_height)
Definition vp3.c:1643
#define TOKEN_EOB(eob_run)
Definition vp3.c:281
static void init_dequantizer(Vp3DecodeContext *s, int qpi)
Definition vp3.c:426
static VLCElem motion_vector_vlc[112]
Definition vp3.c:168
static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
Definition vp3.c:911
static av_cold int allocate_tables(AVCodecContext *avctx)
Allocate tables for per-frame data in Vp3DecodeContext.
Definition vp3.c:2310
#define MODE_COPY
Definition vp3.c:93
static av_cold void free_vlc_tables(AVRefStructOpaque unused, void *obj)
Definition vp3.c:2354
static VLCElem mode_code_vlc[24+2108 *CONFIG_VP4_DECODER]
Definition vp3.c:171
static av_cold void free_tables(AVCodecContext *avctx)
Definition vp3.c:343
static int vp3_dequant(Vp3DecodeContext *s, const Vp3Fragment *frag, int plane, int inter, int16_t block[64])
Pull DCT tokens from the 64 levels to decode and dequant the coefficients for the next block in codin...
Definition vp3.c:1858
#define MODE_INTER_LAST_MV
Definition vp3.c:85
#define VP3_MV_VLC_BITS
Definition vp3.c:60
static av_cold void vp3_decode_flush(AVCodecContext *avctx)
Definition vp3.c:359
#define MAXIMUM_LONG_BIT_RUN
Definition vp3.c:80
static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, const VLCElem *vlc_table, int coeff_index, int plane, int eob_run)
Definition vp3.c:1188
static void render_slice(Vp3DecodeContext *s, int slice)
Definition vp3.c:2061
#define MODE_INTER_FOURMV
Definition vp3.c:89
#define VP4_MV_VLC_BITS
Definition vp3.c:61
#define SB_PARTIALLY_CODED
Definition vp3.c:74
#define PUL
static void await_reference_row(Vp3DecodeContext *s, const Vp3Fragment *fragment, int motion_y, int y)
Wait for the reference frame of the current fragment.
Definition vp3.c:1940
static int read_huffman_tree(HuffTable *huff, GetBitContext *gb, int length, AVCodecContext *avctx)
Definition vp3.c:2840
static VLCElem superblock_run_length_vlc[88]
Definition vp3.c:166
static const uint8_t vp4_pred_block_type_map[8]
Definition vp3.c:155
#define SUPERBLOCK_VLC_BITS
Definition vp3.c:62
#define SB_NOT_CODED
Definition vp3.c:73
static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
Definition vp3.c:793
#define BLOCK_Y
Definition vp3.c:651
static av_cold int vp3_decode_end(AVCodecContext *avctx)
Definition vp3.c:368
static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
Definition vp3.c:1308
#define MODE_USING_GOLDEN
Definition vp3.c:87
static int init_block_mapping(Vp3DecodeContext *s)
This function sets up all of the various blocks mappings: superblocks <-> fragments,...
Definition vp3.c:393
static int theora_reinit(AVCodecContext *avctx, uint8_t *headers, int headers_size)
#define MODE_INTER_PLUS_MV
Definition vp3.c:84
#define SET_CHROMA_MODES
static const int ModeAlphabet[6][CODING_MODE_COUNT]
Definition vp3.c:102
static av_cold void init_tables_once(void)
Definition vp3.c:2267
#define MODE_INTER_NO_MV
Definition vp3.c:82
#define PUR
static int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
Definition vp3.c:1160
static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
Definition vp3.c:1792
static int vp3_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition vp3.c:2556
#define TRANSPOSE(x)
#define TOKEN_COEFF(coeff)
Definition vp3.c:283
static const uint8_t hilbert_offset[16][2]
Definition vp3.c:140
#define TOKEN_ZERO_RUN(coeff, zero_run)
Definition vp3.c:282
static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
called when all pixels up to row y are complete
Definition vp3.c:1903
static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
Definition vp3.c:477
#define FRAGMENT_PIXELS
Definition vp3.c:64
static VLCElem fragment_run_length_vlc[56]
Definition vp3.c:167
static const uint8_t zero_run_get_bits[32]
Definition vp3data.h:140
static const uint8_t vp31_filter_limit_values[64]
Definition vp3data.h:74
static const uint16_t vp31_ac_scale_factor[64]
Definition vp3data.h:63
static const uint8_t mode_code_vlc_len[8]
Definition vp3data.h:97
static const uint8_t zero_run_base[32]
Definition vp3data.h:133
static const int8_t fixed_motion_vector_table[64]
Definition vp3data.h:115
static const int16_t *const coeff_tables[32]
Definition vp3data.h:332
static const uint8_t vp31_dc_scale_factor[64]
Definition vp3data.h:52
static const uint8_t vp3_bias[5 *16][32][2]
Definition vp3data.h:370
static const uint8_t fragment_run_length_vlc_len[30]
Definition vp3data.h:92
static const uint8_t vp31_intra_y_dequant[64]
Definition vp3data.h:29
static const struct @370321105316032037314210141206274253244133335364 eob_run_table[7]
static const uint8_t vp31_inter_dequant[64]
Definition vp3data.h:41
uint8_t base
Definition vp3data.h:128
static const uint8_t coeff_get_bits[32]
Definition vp3data.h:148
static const uint8_t motion_vector_vlc_table[63][2]
Definition vp3data.h:101
static const uint8_t superblock_run_length_vlc_lens[34]
Definition vp3data.h:85
void ff_vp3dsp_h_loop_filter_12(uint8_t *first_pixel, ptrdiff_t stride, int *bounding_values)
void ff_vp3dsp_v_loop_filter_12(uint8_t *first_pixel, ptrdiff_t stride, int *bounding_values)
VP4 video decoder.
static const uint8_t vp4_mv_vlc[2][7][63][2]
Definition vp4data.h:112
static const uint8_t vp4_filter_limit_values[64]
Definition vp4data.h:75
static const uint8_t vp4_uv_dc_scale_factor[64]
Definition vp4data.h:53
static const uint8_t vp4_block_pattern_table_selector[14]
Definition vp4data.h:86
static const uint8_t vp4_mv_table_selector[32]
Definition vp4data.h:105
static const uint8_t vp4_block_pattern_vlc[2][14][2]
Definition vp4data.h:90
static const uint16_t vp4_ac_scale_factor[64]
Definition vp4data.h:64
static const uint8_t vp4_bias[5 *16][32][2]
Definition vp4data.h:329
static const uint8_t vp4_generic_dequant[64]
Definition vp4data.h:31
static const uint8_t vp4_y_dc_scale_factor[64]
Definition vp4data.h:42
static int ref_frame(VVCFrame *dst, const VVCFrame *src)
Definition dec.c:619
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use.
Definition xiph.c:26