PageRenderTime 141ms CodeModel.GetById 22ms app.highlight 103ms RepoModel.GetById 2ms app.codeStats 1ms

/libavcodec/vp3.c

http://github.com/FFmpeg/FFmpeg
C | 3260 lines | 2458 code | 454 blank | 348 comment | 614 complexity | 503483eb2da5ccc94c391a2e91805d76 MD5 | raw file

Large files files are truncated, but you can click here to view the full 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 <stdio.h>
  34#include <stdlib.h>
  35#include <string.h>
  36
  37#include "libavutil/imgutils.h"
  38
  39#include "avcodec.h"
  40#include "get_bits.h"
  41#include "hpeldsp.h"
  42#include "internal.h"
  43#include "mathops.h"
  44#include "thread.h"
  45#include "videodsp.h"
  46#include "vp3data.h"
  47#include "vp4data.h"
  48#include "vp3dsp.h"
  49#include "xiph.h"
  50
  51#define FRAGMENT_PIXELS 8
  52
  53// FIXME split things out into their own arrays
  54typedef struct Vp3Fragment {
  55    int16_t dc;
  56    uint8_t coding_method;
  57    uint8_t qpi;
  58} Vp3Fragment;
  59
  60#define SB_NOT_CODED        0
  61#define SB_PARTIALLY_CODED  1
  62#define SB_FULLY_CODED      2
  63
  64// This is the maximum length of a single long bit run that can be encoded
  65// for superblock coding or block qps. Theora special-cases this to read a
  66// bit instead of flipping the current bit to allow for runs longer than 4129.
  67#define MAXIMUM_LONG_BIT_RUN 4129
  68
  69#define MODE_INTER_NO_MV      0
  70#define MODE_INTRA            1
  71#define MODE_INTER_PLUS_MV    2
  72#define MODE_INTER_LAST_MV    3
  73#define MODE_INTER_PRIOR_LAST 4
  74#define MODE_USING_GOLDEN     5
  75#define MODE_GOLDEN_MV        6
  76#define MODE_INTER_FOURMV     7
  77#define CODING_MODE_COUNT     8
  78
  79/* special internal mode */
  80#define MODE_COPY             8
  81
  82static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
  83static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
  84
  85
  86/* There are 6 preset schemes, plus a free-form scheme */
  87static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
  88    /* scheme 1: Last motion vector dominates */
  89    { MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
  90      MODE_INTER_PLUS_MV,    MODE_INTER_NO_MV,
  91      MODE_INTRA,            MODE_USING_GOLDEN,
  92      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
  93
  94    /* scheme 2 */
  95    { MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
  96      MODE_INTER_NO_MV,      MODE_INTER_PLUS_MV,
  97      MODE_INTRA,            MODE_USING_GOLDEN,
  98      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
  99
 100    /* scheme 3 */
 101    { MODE_INTER_LAST_MV,    MODE_INTER_PLUS_MV,
 102      MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
 103      MODE_INTRA,            MODE_USING_GOLDEN,
 104      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
 105
 106    /* scheme 4 */
 107    { MODE_INTER_LAST_MV,    MODE_INTER_PLUS_MV,
 108      MODE_INTER_NO_MV,      MODE_INTER_PRIOR_LAST,
 109      MODE_INTRA,            MODE_USING_GOLDEN,
 110      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
 111
 112    /* scheme 5: No motion vector dominates */
 113    { MODE_INTER_NO_MV,      MODE_INTER_LAST_MV,
 114      MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
 115      MODE_INTRA,            MODE_USING_GOLDEN,
 116      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
 117
 118    /* scheme 6 */
 119    { MODE_INTER_NO_MV,      MODE_USING_GOLDEN,
 120      MODE_INTER_LAST_MV,    MODE_INTER_PRIOR_LAST,
 121      MODE_INTER_PLUS_MV,    MODE_INTRA,
 122      MODE_GOLDEN_MV,        MODE_INTER_FOURMV },
 123};
 124
 125static const uint8_t hilbert_offset[16][2] = {
 126    { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
 127    { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
 128    { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
 129    { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
 130};
 131
 132enum {
 133    VP4_DC_INTRA  = 0,
 134    VP4_DC_INTER  = 1,
 135    VP4_DC_GOLDEN = 2,
 136    NB_VP4_DC_TYPES,
 137    VP4_DC_UNDEFINED = NB_VP4_DC_TYPES
 138};
 139
 140static const uint8_t vp4_pred_block_type_map[8] = {
 141    [MODE_INTER_NO_MV]      = VP4_DC_INTER,
 142    [MODE_INTRA]            = VP4_DC_INTRA,
 143    [MODE_INTER_PLUS_MV]    = VP4_DC_INTER,
 144    [MODE_INTER_LAST_MV]    = VP4_DC_INTER,
 145    [MODE_INTER_PRIOR_LAST] = VP4_DC_INTER,
 146    [MODE_USING_GOLDEN]     = VP4_DC_GOLDEN,
 147    [MODE_GOLDEN_MV]        = VP4_DC_GOLDEN,
 148    [MODE_INTER_FOURMV]     = VP4_DC_INTER,
 149};
 150
 151typedef struct {
 152    int dc;
 153    int type;
 154} VP4Predictor;
 155
 156#define MIN_DEQUANT_VAL 2
 157
 158typedef struct Vp3DecodeContext {
 159    AVCodecContext *avctx;
 160    int theora, theora_tables, theora_header;
 161    int version;
 162    int width, height;
 163    int chroma_x_shift, chroma_y_shift;
 164    ThreadFrame golden_frame;
 165    ThreadFrame last_frame;
 166    ThreadFrame current_frame;
 167    int keyframe;
 168    uint8_t idct_permutation[64];
 169    uint8_t idct_scantable[64];
 170    HpelDSPContext hdsp;
 171    VideoDSPContext vdsp;
 172    VP3DSPContext vp3dsp;
 173    DECLARE_ALIGNED(16, int16_t, block)[64];
 174    int flipped_image;
 175    int last_slice_end;
 176    int skip_loop_filter;
 177
 178    int qps[3];
 179    int nqps;
 180    int last_qps[3];
 181
 182    int superblock_count;
 183    int y_superblock_width;
 184    int y_superblock_height;
 185    int y_superblock_count;
 186    int c_superblock_width;
 187    int c_superblock_height;
 188    int c_superblock_count;
 189    int u_superblock_start;
 190    int v_superblock_start;
 191    unsigned char *superblock_coding;
 192
 193    int macroblock_count; /* y macroblock count */
 194    int macroblock_width;
 195    int macroblock_height;
 196    int c_macroblock_count;
 197    int c_macroblock_width;
 198    int c_macroblock_height;
 199    int yuv_macroblock_count; /* y+u+v macroblock count */
 200
 201    int fragment_count;
 202    int fragment_width[2];
 203    int fragment_height[2];
 204
 205    Vp3Fragment *all_fragments;
 206    int fragment_start[3];
 207    int data_offset[3];
 208    uint8_t offset_x;
 209    uint8_t offset_y;
 210    int offset_x_warned;
 211
 212    int8_t (*motion_val[2])[2];
 213
 214    /* tables */
 215    uint16_t coded_dc_scale_factor[2][64];
 216    uint32_t coded_ac_scale_factor[64];
 217    uint8_t base_matrix[384][64];
 218    uint8_t qr_count[2][3];
 219    uint8_t qr_size[2][3][64];
 220    uint16_t qr_base[2][3][64];
 221
 222    /**
 223     * This is a list of all tokens in bitstream order. Reordering takes place
 224     * by pulling from each level during IDCT. As a consequence, IDCT must be
 225     * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
 226     * otherwise. The 32 different tokens with up to 12 bits of extradata are
 227     * collapsed into 3 types, packed as follows:
 228     *   (from the low to high bits)
 229     *
 230     * 2 bits: type (0,1,2)
 231     *   0: EOB run, 14 bits for run length (12 needed)
 232     *   1: zero run, 7 bits for run length
 233     *                7 bits for the next coefficient (3 needed)
 234     *   2: coefficient, 14 bits (11 needed)
 235     *
 236     * Coefficients are signed, so are packed in the highest bits for automatic
 237     * sign extension.
 238     */
 239    int16_t *dct_tokens[3][64];
 240    int16_t *dct_tokens_base;
 241#define TOKEN_EOB(eob_run)              ((eob_run) << 2)
 242#define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
 243#define TOKEN_COEFF(coeff)              (((coeff) * 4) + 2)
 244
 245    /**
 246     * number of blocks that contain DCT coefficients at
 247     * the given level or higher
 248     */
 249    int num_coded_frags[3][64];
 250    int total_num_coded_frags;
 251
 252    /* this is a list of indexes into the all_fragments array indicating
 253     * which of the fragments are coded */
 254    int *coded_fragment_list[3];
 255
 256    int *kf_coded_fragment_list;
 257    int *nkf_coded_fragment_list;
 258    int num_kf_coded_fragment[3];
 259
 260    VLC dc_vlc[16];
 261    VLC ac_vlc_1[16];
 262    VLC ac_vlc_2[16];
 263    VLC ac_vlc_3[16];
 264    VLC ac_vlc_4[16];
 265
 266    VLC superblock_run_length_vlc; /* version < 2 */
 267    VLC fragment_run_length_vlc; /* version < 2 */
 268    VLC block_pattern_vlc[2]; /* version >= 2*/
 269    VLC mode_code_vlc;
 270    VLC motion_vector_vlc; /* version < 2 */
 271    VLC vp4_mv_vlc[2][7]; /* version >=2 */
 272
 273    /* these arrays need to be on 16-byte boundaries since SSE2 operations
 274     * index into them */
 275    DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64];     ///< qmat[qpi][is_inter][plane]
 276
 277    /* This table contains superblock_count * 16 entries. Each set of 16
 278     * numbers corresponds to the fragment indexes 0..15 of the superblock.
 279     * An entry will be -1 to indicate that no entry corresponds to that
 280     * index. */
 281    int *superblock_fragments;
 282
 283    /* This is an array that indicates how a particular macroblock
 284     * is coded. */
 285    unsigned char *macroblock_coding;
 286
 287    uint8_t *edge_emu_buffer;
 288
 289    /* Huffman decode */
 290    int hti;
 291    unsigned int hbits;
 292    int entries;
 293    int huff_code_size;
 294    uint32_t huffman_table[80][32][2];
 295
 296    uint8_t filter_limit_values[64];
 297    DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2];
 298
 299    VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */
 300} Vp3DecodeContext;
 301
 302/************************************************************************
 303 * VP3 specific functions
 304 ************************************************************************/
 305
 306static av_cold void free_tables(AVCodecContext *avctx)
 307{
 308    Vp3DecodeContext *s = avctx->priv_data;
 309
 310    av_freep(&s->superblock_coding);
 311    av_freep(&s->all_fragments);
 312    av_freep(&s->nkf_coded_fragment_list);
 313    av_freep(&s->kf_coded_fragment_list);
 314    av_freep(&s->dct_tokens_base);
 315    av_freep(&s->superblock_fragments);
 316    av_freep(&s->macroblock_coding);
 317    av_freep(&s->dc_pred_row);
 318    av_freep(&s->motion_val[0]);
 319    av_freep(&s->motion_val[1]);
 320}
 321
 322static void vp3_decode_flush(AVCodecContext *avctx)
 323{
 324    Vp3DecodeContext *s = avctx->priv_data;
 325
 326    if (s->golden_frame.f)
 327        ff_thread_release_buffer(avctx, &s->golden_frame);
 328    if (s->last_frame.f)
 329        ff_thread_release_buffer(avctx, &s->last_frame);
 330    if (s->current_frame.f)
 331        ff_thread_release_buffer(avctx, &s->current_frame);
 332}
 333
 334static av_cold int vp3_decode_end(AVCodecContext *avctx)
 335{
 336    Vp3DecodeContext *s = avctx->priv_data;
 337    int i, j;
 338
 339    free_tables(avctx);
 340    av_freep(&s->edge_emu_buffer);
 341
 342    s->theora_tables = 0;
 343
 344    /* release all frames */
 345    vp3_decode_flush(avctx);
 346    av_frame_free(&s->current_frame.f);
 347    av_frame_free(&s->last_frame.f);
 348    av_frame_free(&s->golden_frame.f);
 349
 350    for (i = 0; i < 16; i++) {
 351        ff_free_vlc(&s->dc_vlc[i]);
 352        ff_free_vlc(&s->ac_vlc_1[i]);
 353        ff_free_vlc(&s->ac_vlc_2[i]);
 354        ff_free_vlc(&s->ac_vlc_3[i]);
 355        ff_free_vlc(&s->ac_vlc_4[i]);
 356    }
 357
 358    ff_free_vlc(&s->superblock_run_length_vlc);
 359    ff_free_vlc(&s->fragment_run_length_vlc);
 360    ff_free_vlc(&s->mode_code_vlc);
 361    ff_free_vlc(&s->motion_vector_vlc);
 362
 363    for (j = 0; j < 2; j++)
 364        for (i = 0; i < 7; i++)
 365            ff_free_vlc(&s->vp4_mv_vlc[j][i]);
 366
 367    for (i = 0; i < 2; i++)
 368        ff_free_vlc(&s->block_pattern_vlc[i]);
 369    return 0;
 370}
 371
 372/**
 373 * This function sets up all of the various blocks mappings:
 374 * superblocks <-> fragments, macroblocks <-> fragments,
 375 * superblocks <-> macroblocks
 376 *
 377 * @return 0 is successful; returns 1 if *anything* went wrong.
 378 */
 379static int init_block_mapping(Vp3DecodeContext *s)
 380{
 381    int sb_x, sb_y, plane;
 382    int x, y, i, j = 0;
 383
 384    for (plane = 0; plane < 3; plane++) {
 385        int sb_width    = plane ? s->c_superblock_width
 386                                : s->y_superblock_width;
 387        int sb_height   = plane ? s->c_superblock_height
 388                                : s->y_superblock_height;
 389        int frag_width  = s->fragment_width[!!plane];
 390        int frag_height = s->fragment_height[!!plane];
 391
 392        for (sb_y = 0; sb_y < sb_height; sb_y++)
 393            for (sb_x = 0; sb_x < sb_width; sb_x++)
 394                for (i = 0; i < 16; i++) {
 395                    x = 4 * sb_x + hilbert_offset[i][0];
 396                    y = 4 * sb_y + hilbert_offset[i][1];
 397
 398                    if (x < frag_width && y < frag_height)
 399                        s->superblock_fragments[j++] = s->fragment_start[plane] +
 400                                                       y * frag_width + x;
 401                    else
 402                        s->superblock_fragments[j++] = -1;
 403                }
 404    }
 405
 406    return 0;  /* successful path out */
 407}
 408
 409/*
 410 * This function sets up the dequantization tables used for a particular
 411 * frame.
 412 */
 413static void init_dequantizer(Vp3DecodeContext *s, int qpi)
 414{
 415    int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
 416    int i, plane, inter, qri, bmi, bmj, qistart;
 417
 418    for (inter = 0; inter < 2; inter++) {
 419        for (plane = 0; plane < 3; plane++) {
 420            int dc_scale_factor = s->coded_dc_scale_factor[!!plane][s->qps[qpi]];
 421            int sum = 0;
 422            for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
 423                sum += s->qr_size[inter][plane][qri];
 424                if (s->qps[qpi] <= sum)
 425                    break;
 426            }
 427            qistart = sum - s->qr_size[inter][plane][qri];
 428            bmi     = s->qr_base[inter][plane][qri];
 429            bmj     = s->qr_base[inter][plane][qri + 1];
 430            for (i = 0; i < 64; i++) {
 431                int coeff = (2 * (sum     - s->qps[qpi]) * s->base_matrix[bmi][i] -
 432                             2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
 433                             s->qr_size[inter][plane][qri]) /
 434                            (2 * s->qr_size[inter][plane][qri]);
 435
 436                int qmin   = 8 << (inter + !i);
 437                int qscale = i ? ac_scale_factor : dc_scale_factor;
 438                int qbias = (1 + inter) * 3;
 439                s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
 440                    (i == 0 || s->version < 2) ? av_clip((qscale * coeff) / 100 * 4, qmin, 4096)
 441                                               : (qscale * (coeff - qbias) / 100 + qbias) * 4;
 442            }
 443            /* all DC coefficients use the same quant so as not to interfere
 444             * with DC prediction */
 445            s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
 446        }
 447    }
 448}
 449
 450/*
 451 * This function initializes the loop filter boundary limits if the frame's
 452 * quality index is different from the previous frame's.
 453 *
 454 * The filter_limit_values may not be larger than 127.
 455 */
 456static void init_loop_filter(Vp3DecodeContext *s)
 457{
 458    ff_vp3dsp_set_bounding_values(s->bounding_values_array, s->filter_limit_values[s->qps[0]]);
 459}
 460
 461/*
 462 * This function unpacks all of the superblock/macroblock/fragment coding
 463 * information from the bitstream.
 464 */
 465static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
 466{
 467    int superblock_starts[3] = {
 468        0, s->u_superblock_start, s->v_superblock_start
 469    };
 470    int bit = 0;
 471    int current_superblock = 0;
 472    int current_run = 0;
 473    int num_partial_superblocks = 0;
 474
 475    int i, j;
 476    int current_fragment;
 477    int plane;
 478    int plane0_num_coded_frags = 0;
 479
 480    if (s->keyframe) {
 481        memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
 482    } else {
 483        /* unpack the list of partially-coded superblocks */
 484        bit         = get_bits1(gb) ^ 1;
 485        current_run = 0;
 486
 487        while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
 488            if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
 489                bit = get_bits1(gb);
 490            else
 491                bit ^= 1;
 492
 493            current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
 494                                   6, 2) + 1;
 495            if (current_run == 34)
 496                current_run += get_bits(gb, 12);
 497
 498            if (current_run > s->superblock_count - current_superblock) {
 499                av_log(s->avctx, AV_LOG_ERROR,
 500                       "Invalid partially coded superblock run length\n");
 501                return -1;
 502            }
 503
 504            memset(s->superblock_coding + current_superblock, bit, current_run);
 505
 506            current_superblock += current_run;
 507            if (bit)
 508                num_partial_superblocks += current_run;
 509        }
 510
 511        /* unpack the list of fully coded superblocks if any of the blocks were
 512         * not marked as partially coded in the previous step */
 513        if (num_partial_superblocks < s->superblock_count) {
 514            int superblocks_decoded = 0;
 515
 516            current_superblock = 0;
 517            bit                = get_bits1(gb) ^ 1;
 518            current_run        = 0;
 519
 520            while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
 521                   get_bits_left(gb) > 0) {
 522                if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
 523                    bit = get_bits1(gb);
 524                else
 525                    bit ^= 1;
 526
 527                current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
 528                                       6, 2) + 1;
 529                if (current_run == 34)
 530                    current_run += get_bits(gb, 12);
 531
 532                for (j = 0; j < current_run; current_superblock++) {
 533                    if (current_superblock >= s->superblock_count) {
 534                        av_log(s->avctx, AV_LOG_ERROR,
 535                               "Invalid fully coded superblock run length\n");
 536                        return -1;
 537                    }
 538
 539                    /* skip any superblocks already marked as partially coded */
 540                    if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
 541                        s->superblock_coding[current_superblock] = 2 * bit;
 542                        j++;
 543                    }
 544                }
 545                superblocks_decoded += current_run;
 546            }
 547        }
 548
 549        /* if there were partial blocks, initialize bitstream for
 550         * unpacking fragment codings */
 551        if (num_partial_superblocks) {
 552            current_run = 0;
 553            bit         = get_bits1(gb);
 554            /* toggle the bit because as soon as the first run length is
 555             * fetched the bit will be toggled again */
 556            bit ^= 1;
 557        }
 558    }
 559
 560    /* figure out which fragments are coded; iterate through each
 561     * superblock (all planes) */
 562    s->total_num_coded_frags = 0;
 563    memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
 564
 565    s->coded_fragment_list[0] = s->keyframe ? s->kf_coded_fragment_list
 566                                            : s->nkf_coded_fragment_list;
 567
 568    for (plane = 0; plane < 3; plane++) {
 569        int sb_start = superblock_starts[plane];
 570        int sb_end   = sb_start + (plane ? s->c_superblock_count
 571                                         : s->y_superblock_count);
 572        int num_coded_frags = 0;
 573
 574        if (s->keyframe) {
 575            if (s->num_kf_coded_fragment[plane] == -1) {
 576                for (i = sb_start; i < sb_end; i++) {
 577                    /* iterate through all 16 fragments in a superblock */
 578                    for (j = 0; j < 16; j++) {
 579                        /* if the fragment is in bounds, check its coding status */
 580                        current_fragment = s->superblock_fragments[i * 16 + j];
 581                        if (current_fragment != -1) {
 582                            s->coded_fragment_list[plane][num_coded_frags++] =
 583                                current_fragment;
 584                        }
 585                    }
 586                }
 587                s->num_kf_coded_fragment[plane] = num_coded_frags;
 588            } else
 589                num_coded_frags = s->num_kf_coded_fragment[plane];
 590        } else {
 591            for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
 592                if (get_bits_left(gb) < plane0_num_coded_frags >> 2) {
 593                    return AVERROR_INVALIDDATA;
 594                }
 595                /* iterate through all 16 fragments in a superblock */
 596                for (j = 0; j < 16; j++) {
 597                    /* if the fragment is in bounds, check its coding status */
 598                    current_fragment = s->superblock_fragments[i * 16 + j];
 599                    if (current_fragment != -1) {
 600                        int coded = s->superblock_coding[i];
 601
 602                        if (coded == SB_PARTIALLY_CODED) {
 603                            /* fragment may or may not be coded; this is the case
 604                             * that cares about the fragment coding runs */
 605                            if (current_run-- == 0) {
 606                                bit        ^= 1;
 607                                current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);
 608                            }
 609                            coded = bit;
 610                        }
 611
 612                        if (coded) {
 613                            /* default mode; actual mode will be decoded in
 614                             * the next phase */
 615                            s->all_fragments[current_fragment].coding_method =
 616                                MODE_INTER_NO_MV;
 617                            s->coded_fragment_list[plane][num_coded_frags++] =
 618                                current_fragment;
 619                        } else {
 620                            /* not coded; copy this fragment from the prior frame */
 621                            s->all_fragments[current_fragment].coding_method =
 622                                MODE_COPY;
 623                        }
 624                    }
 625                }
 626            }
 627        }
 628        if (!plane)
 629            plane0_num_coded_frags = num_coded_frags;
 630        s->total_num_coded_frags += num_coded_frags;
 631        for (i = 0; i < 64; i++)
 632            s->num_coded_frags[plane][i] = num_coded_frags;
 633        if (plane < 2)
 634            s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
 635                                                num_coded_frags;
 636    }
 637    return 0;
 638}
 639
 640#define BLOCK_X (2 * mb_x + (k & 1))
 641#define BLOCK_Y (2 * mb_y + (k >> 1))
 642
 643#if CONFIG_VP4_DECODER
 644/**
 645 * @return number of blocks, or > yuv_macroblock_count on error.
 646 *         return value is always >= 1.
 647 */
 648static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb)
 649{
 650    int v = 1;
 651    int bits;
 652    while ((bits = show_bits(gb, 9)) == 0x1ff) {
 653        skip_bits(gb, 9);
 654        v += 256;
 655        if (v > s->yuv_macroblock_count) {
 656            av_log(s->avctx, AV_LOG_ERROR, "Invalid run length\n");
 657            return v;
 658        }
 659    }
 660#define body(n) { \
 661    skip_bits(gb, 2 + n); \
 662    v += (1 << n) + get_bits(gb, n); }
 663#define thresh(n) (0x200 - (0x80 >> n))
 664#define else_if(n) else if (bits < thresh(n)) body(n)
 665    if (bits < 0x100) {
 666        skip_bits(gb, 1);
 667    } else if (bits < thresh(0)) {
 668        skip_bits(gb, 2);
 669        v += 1;
 670    }
 671    else_if(1)
 672    else_if(2)
 673    else_if(3)
 674    else_if(4)
 675    else_if(5)
 676    else_if(6)
 677    else body(7)
 678#undef body
 679#undef thresh
 680#undef else_if
 681    return v;
 682}
 683
 684static int vp4_get_block_pattern(Vp3DecodeContext *s, GetBitContext *gb, int *next_block_pattern_table)
 685{
 686    int v = get_vlc2(gb, s->block_pattern_vlc[*next_block_pattern_table].table, 3, 2);
 687    if (v == -1) {
 688        av_log(s->avctx, AV_LOG_ERROR, "Invalid block pattern\n");
 689        *next_block_pattern_table = 0;
 690        return 0;
 691    }
 692    *next_block_pattern_table = vp4_block_pattern_table_selector[v];
 693    return v + 1;
 694}
 695
 696static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb)
 697{
 698    int plane, i, j, k, fragment;
 699    int next_block_pattern_table;
 700    int bit, current_run, has_partial;
 701
 702    memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
 703
 704    if (s->keyframe)
 705        return 0;
 706
 707    has_partial = 0;
 708    bit         = get_bits1(gb);
 709    for (i = 0; i < s->yuv_macroblock_count; i += current_run) {
 710        if (get_bits_left(gb) <= 0)
 711            return AVERROR_INVALIDDATA;
 712        current_run = vp4_get_mb_count(s, gb);
 713        if (current_run > s->yuv_macroblock_count - i)
 714            return -1;
 715        memset(s->superblock_coding + i, 2 * bit, current_run);
 716        bit ^= 1;
 717        has_partial |= bit;
 718    }
 719
 720    if (has_partial) {
 721        if (get_bits_left(gb) <= 0)
 722            return AVERROR_INVALIDDATA;
 723        bit  = get_bits1(gb);
 724        current_run = vp4_get_mb_count(s, gb);
 725        for (i = 0; i < s->yuv_macroblock_count; i++) {
 726            if (!s->superblock_coding[i]) {
 727                if (!current_run) {
 728                    bit ^= 1;
 729                    current_run = vp4_get_mb_count(s, gb);
 730                }
 731                s->superblock_coding[i] = bit;
 732                current_run--;
 733            }
 734        }
 735        if (current_run) /* handle situation when vp4_get_mb_count() fails */
 736            return -1;
 737    }
 738
 739    next_block_pattern_table = 0;
 740    i = 0;
 741    for (plane = 0; plane < 3; plane++) {
 742        int sb_x, sb_y;
 743        int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
 744        int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
 745        int mb_width = plane ? s->c_macroblock_width : s->macroblock_width;
 746        int mb_height = plane ? s->c_macroblock_height : s->macroblock_height;
 747        int fragment_width = s->fragment_width[!!plane];
 748        int fragment_height = s->fragment_height[!!plane];
 749
 750        for (sb_y = 0; sb_y < sb_height; sb_y++) {
 751            for (sb_x = 0; sb_x < sb_width; sb_x++) {
 752                for (j = 0; j < 4; j++) {
 753                    int mb_x = 2 * sb_x + (j >> 1);
 754                    int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1);
 755                    int mb_coded, pattern, coded;
 756
 757                    if (mb_x >= mb_width || mb_y >= mb_height)
 758                        continue;
 759
 760                    mb_coded = s->superblock_coding[i++];
 761
 762                    if (mb_coded == SB_FULLY_CODED)
 763                        pattern = 0xF;
 764                    else if (mb_coded == SB_PARTIALLY_CODED)
 765                        pattern = vp4_get_block_pattern(s, gb, &next_block_pattern_table);
 766                    else
 767                        pattern = 0;
 768
 769                    for (k = 0; k < 4; k++) {
 770                        if (BLOCK_X >= fragment_width || BLOCK_Y >= fragment_height)
 771                            continue;
 772                        fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X;
 773                        coded = pattern & (8 >> k);
 774                        /* MODE_INTER_NO_MV is the default for coded fragments.
 775                           the actual method is decoded in the next phase. */
 776                        s->all_fragments[fragment].coding_method = coded ? MODE_INTER_NO_MV : MODE_COPY;
 777                    }
 778                }
 779            }
 780        }
 781    }
 782    return 0;
 783}
 784#endif
 785
 786/*
 787 * This function unpacks all the coding mode data for individual macroblocks
 788 * from the bitstream.
 789 */
 790static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
 791{
 792    int i, j, k, sb_x, sb_y;
 793    int scheme;
 794    int current_macroblock;
 795    int current_fragment;
 796    int coding_mode;
 797    int custom_mode_alphabet[CODING_MODE_COUNT];
 798    const int *alphabet;
 799    Vp3Fragment *frag;
 800
 801    if (s->keyframe) {
 802        for (i = 0; i < s->fragment_count; i++)
 803            s->all_fragments[i].coding_method = MODE_INTRA;
 804    } else {
 805        /* fetch the mode coding scheme for this frame */
 806        scheme = get_bits(gb, 3);
 807
 808        /* is it a custom coding scheme? */
 809        if (scheme == 0) {
 810            for (i = 0; i < 8; i++)
 811                custom_mode_alphabet[i] = MODE_INTER_NO_MV;
 812            for (i = 0; i < 8; i++)
 813                custom_mode_alphabet[get_bits(gb, 3)] = i;
 814            alphabet = custom_mode_alphabet;
 815        } else
 816            alphabet = ModeAlphabet[scheme - 1];
 817
 818        /* iterate through all of the macroblocks that contain 1 or more
 819         * coded fragments */
 820        for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
 821            for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
 822                if (get_bits_left(gb) <= 0)
 823                    return -1;
 824
 825                for (j = 0; j < 4; j++) {
 826                    int mb_x = 2 * sb_x + (j >> 1);
 827                    int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
 828                    current_macroblock = mb_y * s->macroblock_width + mb_x;
 829
 830                    if (mb_x >= s->macroblock_width ||
 831                        mb_y >= s->macroblock_height)
 832                        continue;
 833
 834                    /* coding modes are only stored if the macroblock has
 835                     * at least one luma block coded, otherwise it must be
 836                     * INTER_NO_MV */
 837                    for (k = 0; k < 4; k++) {
 838                        current_fragment = BLOCK_Y *
 839                                           s->fragment_width[0] + BLOCK_X;
 840                        if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
 841                            break;
 842                    }
 843                    if (k == 4) {
 844                        s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
 845                        continue;
 846                    }
 847
 848                    /* mode 7 means get 3 bits for each coding mode */
 849                    if (scheme == 7)
 850                        coding_mode = get_bits(gb, 3);
 851                    else
 852                        coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
 853
 854                    s->macroblock_coding[current_macroblock] = coding_mode;
 855                    for (k = 0; k < 4; k++) {
 856                        frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
 857                        if (frag->coding_method != MODE_COPY)
 858                            frag->coding_method = coding_mode;
 859                    }
 860
 861#define SET_CHROMA_MODES                                                      \
 862    if (frag[s->fragment_start[1]].coding_method != MODE_COPY)                \
 863        frag[s->fragment_start[1]].coding_method = coding_mode;               \
 864    if (frag[s->fragment_start[2]].coding_method != MODE_COPY)                \
 865        frag[s->fragment_start[2]].coding_method = coding_mode;
 866
 867                    if (s->chroma_y_shift) {
 868                        frag = s->all_fragments + mb_y *
 869                               s->fragment_width[1] + mb_x;
 870                        SET_CHROMA_MODES
 871                    } else if (s->chroma_x_shift) {
 872                        frag = s->all_fragments +
 873                               2 * mb_y * s->fragment_width[1] + mb_x;
 874                        for (k = 0; k < 2; k++) {
 875                            SET_CHROMA_MODES
 876                            frag += s->fragment_width[1];
 877                        }
 878                    } else {
 879                        for (k = 0; k < 4; k++) {
 880                            frag = s->all_fragments +
 881                                   BLOCK_Y * s->fragment_width[1] + BLOCK_X;
 882                            SET_CHROMA_MODES
 883                        }
 884                    }
 885                }
 886            }
 887        }
 888    }
 889
 890    return 0;
 891}
 892
 893static int vp4_get_mv(Vp3DecodeContext *s, GetBitContext *gb, int axis, int last_motion)
 894{
 895    int v = get_vlc2(gb, s->vp4_mv_vlc[axis][vp4_mv_table_selector[FFABS(last_motion)]].table, 6, 2) - 31;
 896    return last_motion < 0 ? -v : v;
 897}
 898
 899/*
 900 * This function unpacks all the motion vectors for the individual
 901 * macroblocks from the bitstream.
 902 */
 903static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
 904{
 905    int j, k, sb_x, sb_y;
 906    int coding_mode;
 907    int motion_x[4];
 908    int motion_y[4];
 909    int last_motion_x = 0;
 910    int last_motion_y = 0;
 911    int prior_last_motion_x = 0;
 912    int prior_last_motion_y = 0;
 913    int last_gold_motion_x = 0;
 914    int last_gold_motion_y = 0;
 915    int current_macroblock;
 916    int current_fragment;
 917    int frag;
 918
 919    if (s->keyframe)
 920        return 0;
 921
 922    /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme; 2 is VP4 code scheme */
 923    coding_mode = s->version < 2 ? get_bits1(gb) : 2;
 924
 925    /* iterate through all of the macroblocks that contain 1 or more
 926     * coded fragments */
 927    for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
 928        for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
 929            if (get_bits_left(gb) <= 0)
 930                return -1;
 931
 932            for (j = 0; j < 4; j++) {
 933                int mb_x = 2 * sb_x + (j >> 1);
 934                int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
 935                current_macroblock = mb_y * s->macroblock_width + mb_x;
 936
 937                if (mb_x >= s->macroblock_width  ||
 938                    mb_y >= s->macroblock_height ||
 939                    s->macroblock_coding[current_macroblock] == MODE_COPY)
 940                    continue;
 941
 942                switch (s->macroblock_coding[current_macroblock]) {
 943                case MODE_GOLDEN_MV:
 944                    if (coding_mode == 2) { /* VP4 */
 945                        last_gold_motion_x = motion_x[0] = vp4_get_mv(s, gb, 0, last_gold_motion_x);
 946                        last_gold_motion_y = motion_y[0] = vp4_get_mv(s, gb, 1, last_gold_motion_y);
 947                        break;
 948                    } /* otherwise fall through */
 949                case MODE_INTER_PLUS_MV:
 950                    /* all 6 fragments use the same motion vector */
 951                    if (coding_mode == 0) {
 952                        motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
 953                        motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
 954                    } else if (coding_mode == 1) {
 955                        motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
 956                        motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
 957                    } else { /* VP4 */
 958                        motion_x[0] = vp4_get_mv(s, gb, 0, last_motion_x);
 959                        motion_y[0] = vp4_get_mv(s, gb, 1, last_motion_y);
 960                    }
 961
 962                    /* vector maintenance, only on MODE_INTER_PLUS_MV */
 963                    if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
 964                        prior_last_motion_x = last_motion_x;
 965                        prior_last_motion_y = last_motion_y;
 966                        last_motion_x       = motion_x[0];
 967                        last_motion_y       = motion_y[0];
 968                    }
 969                    break;
 970
 971                case MODE_INTER_FOURMV:
 972                    /* vector maintenance */
 973                    prior_last_motion_x = last_motion_x;
 974                    prior_last_motion_y = last_motion_y;
 975
 976                    /* fetch 4 vectors from the bitstream, one for each
 977                     * Y fragment, then average for the C fragment vectors */
 978                    for (k = 0; k < 4; k++) {
 979                        current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
 980                        if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
 981                            if (coding_mode == 0) {
 982                                motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
 983                                motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
 984                            } else if (coding_mode == 1) {
 985                                motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
 986                                motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
 987                            } else { /* VP4 */
 988                                motion_x[k] = vp4_get_mv(s, gb, 0, prior_last_motion_x);
 989                                motion_y[k] = vp4_get_mv(s, gb, 1, prior_last_motion_y);
 990                            }
 991                            last_motion_x = motion_x[k];
 992                            last_motion_y = motion_y[k];
 993                        } else {
 994                            motion_x[k] = 0;
 995                            motion_y[k] = 0;
 996                        }
 997                    }
 998                    break;
 999
1000                case MODE_INTER_LAST_MV:
1001                    /* all 6 fragments use the last motion vector */
1002                    motion_x[0] = last_motion_x;
1003                    motion_y[0] = last_motion_y;
1004
1005                    /* no vector maintenance (last vector remains the
1006                     * last vector) */
1007                    break;
1008
1009                case MODE_INTER_PRIOR_LAST:
1010                    /* all 6 fragments use the motion vector prior to the
1011                     * last motion vector */
1012                    motion_x[0] = prior_last_motion_x;
1013                    motion_y[0] = prior_last_motion_y;
1014
1015                    /* vector maintenance */
1016                    prior_last_motion_x = last_motion_x;
1017                    prior_last_motion_y = last_motion_y;
1018                    last_motion_x       = motion_x[0];
1019                    last_motion_y       = motion_y[0];
1020                    break;
1021
1022                default:
1023                    /* covers intra, inter without MV, golden without MV */
1024                    motion_x[0] = 0;
1025                    motion_y[0] = 0;
1026
1027                    /* no vector maintenance */
1028                    break;
1029                }
1030
1031                /* assign the motion vectors to the correct fragments */
1032                for (k = 0; k < 4; k++) {
1033                    current_fragment =
1034                        BLOCK_Y * s->fragment_width[0] + BLOCK_X;
1035                    if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1036                        s->motion_val[0][current_fragment][0] = motion_x[k];
1037                        s->motion_val[0][current_fragment][1] = motion_y[k];
1038                    } else {
1039                        s->motion_val[0][current_fragment][0] = motion_x[0];
1040                        s->motion_val[0][current_fragment][1] = motion_y[0];
1041                    }
1042                }
1043
1044                if (s->chroma_y_shift) {
1045                    if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1046                        motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
1047                                             motion_x[2] + motion_x[3], 2);
1048                        motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
1049                                             motion_y[2] + motion_y[3], 2);
1050                    }
1051                    if (s->version <= 2) {
1052                        motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1053                        motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
1054                    }
1055                    frag = mb_y * s->fragment_width[1] + mb_x;
1056                    s->motion_val[1][frag][0] = motion_x[0];
1057                    s->motion_val[1][frag][1] = motion_y[0];
1058                } else if (s->chroma_x_shift) {
1059                    if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1060                        motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
1061                        motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
1062                        motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
1063                        motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
1064                    } else {
1065                        motion_x[1] = motion_x[0];
1066                        motion_y[1] = motion_y[0];
1067                    }
1068                    if (s->version <= 2) {
1069                        motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1070                        motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
1071                    }
1072                    frag = 2 * mb_y * s->fragment_width[1] + mb_x;
1073                    for (k = 0; k < 2; k++) {
1074                        s->motion_val[1][frag][0] = motion_x[k];
1075                        s->motion_val[1][frag][1] = motion_y[k];
1076                        frag += s->fragment_width[1];
1077                    }
1078                } else {
1079                    for (k = 0; k < 4; k++) {
1080                        frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
1081                        if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1082                            s->motion_val[1][frag][0] = motion_x[k];
1083                            s->motion_val[1][frag][1] = motion_y[k];
1084                        } else {
1085                            s->motion_val[1][frag][0] = motion_x[0];
1086                            s->motion_val[1][frag][1] = motion_y[0];
1087                        }
1088                    }
1089                }
1090            }
1091        }
1092    }
1093
1094    return 0;
1095}
1096
1097static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
1098{
1099    int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
1100    int num_blocks = s->total_num_coded_frags;
1101
1102    for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
1103        i = blocks_decoded = num_blocks_at_qpi = 0;
1104
1105        bit        = get_bits1(gb) ^ 1;
1106        run_length = 0;
1107
1108        do {
1109            if (run_length == MAXIMUM_LONG_BIT_RUN)
1110                bit = get_bits1(gb);
1111            else
1112                bit ^= 1;
1113
1114            run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
1115            if (run_length == 34)
1116                run_length += get_bits(gb, 12);
1117            blocks_decoded += run_length;
1118
1119            if (!bit)
1120                num_blocks_at_qpi += run_length;
1121
1122            for (j = 0; j < run_length; i++) {
1123                if (i >= s->total_num_coded_frags)
1124                    return -1;
1125
1126                if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
1127                    s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
1128                    j++;
1129                }
1130            }
1131        } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
1132
1133        num_blocks -= num_blocks_at_qpi;
1134    }
1135
1136    return 0;
1137}
1138
1139static inline int get_eob_run(GetBitContext *gb, int token)
1140{
1141    int v = eob_run_table[token].base;
1142    if (eob_run_table[token].bits)
1143        v += get_bits(gb, eob_run_table[token].bits);
1144    return v;
1145}
1146
1147static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
1148{
1149    int bits_to_get, zero_run;
1150
1151    bits_to_get = coeff_get_bits[token];
1152    if (bits_to_get)
1153        bits_to_get = get_bits(gb, bits_to_get);
1154    *coeff = coeff_tables[token][bits_to_get];
1155
1156    zero_run = zero_run_base[token];
1157    if (zero_run_get_bits[token])
1158        zero_run += get_bits(gb, zero_run_get_bits[token]);
1159
1160    return zero_run;
1161}
1162
1163/*
1164 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1165 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1166 * data. This function unpacks all the VLCs for either the Y plane or both
1167 * C planes, and is called for DC coefficients or different AC coefficient
1168 * levels (since different coefficient types require different VLC tables.
1169 *
1170 * This function returns a residual eob run. E.g, if a particular token gave
1171 * instructions to EOB the next 5 fragments and there were only 2 fragments
1172 * left in the current fragment range, 3 would be returned so that it could
1173 * be passed into the next call to this same function.
1174 */
1175static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1176                       VLC *table, int coeff_index,
1177                       int plane,
1178                       int eob_run)
1179{
1180    int i, j = 0;
1181    int token;
1182    int zero_run  = 0;
1183    int16_t coeff = 0;
1184    int blocks_ended;
1185    int coeff_i = 0;
1186    int num_coeffs      = s->num_coded_frags[plane][coeff_index];
1187    int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
1188
1189    /* local references to structure members to avoid repeated dereferences */
1190    int *coded_fragment_list   = s->coded_fragment_list[plane];
1191    Vp3Fragment *all_fragments = s->all_fragments;
1192    VLC_TYPE(*vlc_table)[2] = table->table;
1193
1194    if (num_coeffs < 0) {
1195        av_log(s->avctx, AV_LOG_ERROR,
1196               "Invalid number of coefficients at level %d\n", coeff_index);
1197        return AVERROR_INVALIDDATA;
1198    }
1199
1200    if (eob_run > num_coeffs) {
1201        coeff_i      =
1202        blocks_ended = num_coeffs;
1203        eob_run     -= num_coeffs;
1204    } else {
1205        coeff_i      =
1206        blocks_ended = eob_run;
1207        eob_run      = 0;
1208    }
1209
1210    // insert fake EOB token to cover the split between planes or zzi
1211    if (blocks_ended)
1212        dct_tokens[j++] = blocks_ended << 2;
1213
1214    while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
1215        /* decode a VLC into a token */
1216        token = get_vlc2(gb, vlc_table, 11, 3);
1217        /* use the token to get a zero run, a coefficient, and an eob run */
1218        if ((unsigned) token <= 6U) {
1219            eob_run = get_eob_run(gb, token);
1220            if (!eob_run)
1221                eob_run = INT_MAX;
1222
1223            // record only the number of blocks ended in this plane,
1224            // any spill will be recorded in the next plane.
1225            if (eob_run > num_coeffs - coeff_i) {
1226                dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
1227                blocks_ended   += num_coeffs - coeff_i;
1228                eob_run        -= num_coeffs - coeff_i;
1229                coeff_i         = num_coeffs;
1230            } else {
1231                dct_tokens[j++] = TOKEN_EOB(eob_run);
1232                blocks_ended   += eob_run;
1233                coeff_i        += eob_run;
1234                eob_run         = 0;
1235            }
1236        } else if (token >= 0) {
1237            zero_run = get_coeff(gb, token, &coeff);
1238
1239            if (zero_run) {
1240                dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1241            } else {
1242                // Save DC into the fragment structure. DC prediction is
1243                // done in raster order, so the actual DC can't be in with
1244                // other tokens. We still need the token in dct_tokens[]
1245                // however, or else the structure collapses on itself.
1246                if (!coeff_index)
1247                    all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1248
1249                dct_tokens[j++] = TOKEN_COEFF(coeff);
1250            }
1251
1252            if (coeff_index + zero_run > 64) {
1253                av_log(s->avctx, AV_LOG_DEBUG,
1254                       "Invalid zero run of %d with %d coeffs left\n",
1255                       zero_run, 64 - coeff_index);
1256                zero_run = 64 - coeff_index;
1257            }
1258
1259            // zero runs code multiple coefficients,
1260            // so don't try to decode coeffs for those higher levels
1261            for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1262                s->num_coded_frags[plane][i]--;
1263            coeff_i++;
1264        } else {
1265            av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1266            return -1;
1267        }
1268    }
1269
1270    if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1271        av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1272
1273    // decrement the number of blocks that have higher coefficients for each
1274    // EOB run at this level
1275    if (blocks_ended)
1276        for (i = coeff_index + 1; i < 64; i++)
1277            s->num_coded_frags[plane][i] -= blocks_ended;
1278
1279    // setup the next buffer
1280    if (plane < 2)
1281        s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1282    else if (coeff_index < 63)
1283        s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1284
1285    return eob_run;
1286}
1287
1288static void reverse_dc_prediction(Vp3DecodeContext *s,
1289                                  int first_fragment,
1290                                  int fragment_width,
1291                                  int fragment_height);
1292/*
1293 * This function unpacks all of the DCT coefficient data from the
1294 * bitstream.
1295 */
1296static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1297{
1298    int i;
1299    int dc_y_table;
1300    int dc_c_table;
1301    int ac_y_table;
1302    int ac_c_table;
1303    int residual_eob_run = 0;
1304    VLC *y_tables[64];
1305    VLC *c_tables[64];
1306
1307    s->dct_tokens[0][0] = s->dct_tokens_base;
1308
1309    if (get_bits_left(gb) < 16)
1310        return AVERROR_INVALIDDATA;
1311
1312    /* fetch the DC table indexes */
1313    dc_y_table = get_bits(gb, 4);
1314    dc_c_table = get_bits(gb, 4);
1315
1316    /* unpack the Y plane DC coefficients */
1317    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1318                                   0, residual_eob_run);
1319    if (residual_eob_run < 0)
1320        return residual_eob_run;
1321    if (get_bits_left(gb) < 8)
1322        return AVERROR_INVALIDDATA;
1323
1324    /* reverse prediction of the Y-plane DC coefficients */
1325    reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1326
1327    /* unpack the C plane DC coefficients */
1328    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1329                                   1, residual_eob_run);
1330    if (residual_eob_run < 0)
1331        return residual_eob_run;
1332    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1333                                   2, residual_eob_run);
1334    if (residual_eob_run < 0)
1335        return residual_eob_run;
1336
1337    /* reverse prediction of the C-plane DC coefficients */
1338    if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1339        reverse_dc_prediction(s, s->fragment_start[1],
1340                              s->fragment_width[1], s->fragment_height[1]);
1341        reverse_dc_prediction(s, s->fragment_start[2],
1342                              s->fragment_width[1], s->fragment_height[1]);
1343    }
1344
1345    if (get_bits_left(gb) < 8)
1346        return AVERROR_INVALIDDATA;
1347    /* fetch the AC table indexes */
1348    ac_y_table = get_bits(gb, 4);
1349    ac_c_table = get_bits(gb, 4);
1350
1351    /* build tables of AC VLC tables */
1352    for (i = 1; i <= 5; i++) {
1353        y_tables[i] = &s->ac_vlc_1[ac_y_table];
1354        c_tables[i] = &s->ac_vlc_1[ac_c_table];
1355    }
1356    for (i = 6; i <= 14; i++) {
1357        y_tables[i] = &s->ac_vlc_2[ac_y_table];
1358        c_tables[i] = &s->ac_vlc_2[ac_c_table];
1359    }
1360    for (i = 15; i <= 27; i++) {
1361        y_tables[

Large files files are truncated, but you can click here to view the full file