tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

vp8.h (10212B)


      1 /*
      2 * VP8 compatible video decoder
      3 *
      4 * Copyright (C) 2010 David Conrad
      5 * Copyright (C) 2010 Ronald S. Bultje
      6 * Copyright (C) 2010 Fiona Glaser
      7 * Copyright (C) 2012 Daniel Kang
      8 *
      9 * This file is part of FFmpeg.
     10 *
     11 * FFmpeg is free software; you can redistribute it and/or
     12 * modify it under the terms of the GNU Lesser General Public
     13 * License as published by the Free Software Foundation; either
     14 * version 2.1 of the License, or (at your option) any later version.
     15 *
     16 * FFmpeg is distributed in the hope that it will be useful,
     17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     19 * Lesser General Public License for more details.
     20 *
     21 * You should have received a copy of the GNU Lesser General Public
     22 * License along with FFmpeg; if not, write to the Free Software
     23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     24 */
     25 
     26 #ifndef AVCODEC_VP8_H
     27 #define AVCODEC_VP8_H
     28 
     29 #include <stdatomic.h>
     30 
     31 #include "libavutil/mem_internal.h"
     32 #include "libavutil/thread.h"
     33 
     34 #include "avcodec.h"
     35 #include "h264pred.h"
     36 #include "progressframe.h"
     37 #include "videodsp.h"
     38 #include "vp8dsp.h"
     39 #include "vpx_rac.h"
     40 
     41 #define VP8_MAX_QUANT 127
     42 
     43 typedef enum {
     44    VP8_FRAME_NONE     = -1,
     45    VP8_FRAME_CURRENT  =  0,
     46    VP8_FRAME_PREVIOUS =  1,
     47    VP8_FRAME_GOLDEN   =  2,
     48    VP8_FRAME_ALTREF   =  3,
     49 } VP8FrameType;
     50 
     51 enum dct_token {
     52    DCT_0,
     53    DCT_1,
     54    DCT_2,
     55    DCT_3,
     56    DCT_4,
     57    DCT_CAT1,
     58    DCT_CAT2,
     59    DCT_CAT3,
     60    DCT_CAT4,
     61    DCT_CAT5,
     62    DCT_CAT6,
     63    DCT_EOB,
     64 
     65    NUM_DCT_TOKENS
     66 };
     67 
     68 // used to signal 4x4 intra pred in luma MBs
     69 #define MODE_I4x4 4
     70 
     71 enum inter_mvmode {
     72    VP8_MVMODE_ZERO = MODE_I4x4 + 1,
     73    VP8_MVMODE_MV,
     74    VP8_MVMODE_SPLIT
     75 };
     76 
     77 enum inter_splitmvmode {
     78    VP8_SPLITMVMODE_16x8 = 0,    ///< 2 16x8 blocks (vertical)
     79    VP8_SPLITMVMODE_8x16,        ///< 2 8x16 blocks (horizontal)
     80    VP8_SPLITMVMODE_8x8,         ///< 2x2 blocks of 8x8px each
     81    VP8_SPLITMVMODE_4x4,         ///< 4x4 blocks of 4x4px each
     82    VP8_SPLITMVMODE_NONE,        ///< (only used in prediction) no split MVs
     83 };
     84 
     85 typedef struct VP8mv {
     86    DECLARE_ALIGNED(4, int16_t, x);
     87    int16_t y;
     88 } VP8mv;
     89 
     90 typedef struct VP8FilterStrength {
     91    uint8_t filter_level;
     92    uint8_t inner_limit;
     93    uint8_t inner_filter;
     94 } VP8FilterStrength;
     95 
     96 typedef struct VP8Macroblock {
     97    uint8_t skip;
     98    // TODO: make it possible to check for at least (i4x4 or split_mv)
     99    // in one op. are others needed?
    100    uint8_t mode;
    101    uint8_t ref_frame;
    102    uint8_t partitioning;
    103    uint8_t chroma_pred_mode;
    104    uint8_t segment;
    105    uint8_t intra4x4_pred_mode_mb[16];
    106    DECLARE_ALIGNED(4, uint8_t, intra4x4_pred_mode_top)[4];
    107    VP8mv mv;
    108    VP8mv bmv[16];
    109 } VP8Macroblock;
    110 
    111 typedef struct VP8intmv {
    112    int x;
    113    int y;
    114 } VP8intmv;
    115 
    116 typedef struct VP8mvbounds {
    117    VP8intmv mv_min;
    118    VP8intmv mv_max;
    119 } VP8mvbounds;
    120 
    121 typedef struct VP8ThreadData {
    122    DECLARE_ALIGNED(16, int16_t, block)[6][4][16];
    123    DECLARE_ALIGNED(16, int16_t, block_dc)[16];
    124    /**
    125     * This is the index plus one of the last non-zero coeff
    126     * for each of the blocks in the current macroblock.
    127     * So, 0 -> no coeffs
    128     *     1 -> dc-only (special transform)
    129     *     2+-> full transform
    130     */
    131    DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
    132    /**
    133     * For coeff decode, we need to know whether the above block had non-zero
    134     * coefficients. This means for each macroblock, we need data for 4 luma
    135     * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
    136     * per macroblock. We keep the last row in top_nnz.
    137     */
    138    DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
    139    int thread_nr;
    140 #if HAVE_THREADS
    141    pthread_mutex_t lock;
    142    pthread_cond_t cond;
    143 #endif
    144    atomic_int thread_mb_pos; // (mb_y << 16) | (mb_x & 0xFFFF)
    145    atomic_int wait_mb_pos; // What the current thread is waiting on.
    146 
    147 #define EDGE_EMU_LINESIZE 32
    148    DECLARE_ALIGNED(16, uint8_t, edge_emu_buffer)[21 * EDGE_EMU_LINESIZE];
    149    VP8FilterStrength *filter_strength;
    150    VP8mvbounds mv_bounds;
    151 } VP8ThreadData;
    152 
    153 typedef struct VP8Frame {
    154    ProgressFrame tf;
    155    uint8_t *seg_map; ///< RefStruct reference
    156 
    157    void *hwaccel_picture_private; ///< RefStruct reference
    158 } VP8Frame;
    159 
    160 #define MAX_THREADS 8
    161 typedef struct VP8Context {
    162    VP8ThreadData *thread_data;
    163    AVCodecContext *avctx;
    164    enum AVPixelFormat pix_fmt;
    165    int actually_webp;
    166 
    167    VP8Frame *framep[4];
    168    VP8Frame *next_framep[4];
    169    VP8Frame *curframe;
    170    VP8Frame *prev_frame;
    171 
    172    uint16_t mb_width;   /* number of horizontal MB */
    173    uint16_t mb_height;  /* number of vertical MB */
    174    ptrdiff_t linesize;
    175    ptrdiff_t uvlinesize;
    176 
    177    uint8_t keyframe;
    178    uint8_t deblock_filter;
    179    uint8_t mbskip_enabled;
    180    uint8_t profile;
    181    VP8mvbounds mv_bounds;
    182 
    183    int8_t sign_bias[4]; ///< one state [0, 1] per ref frame type
    184    int ref_count[3];
    185 
    186    /**
    187     * Base parameters for segmentation, i.e. per-macroblock parameters.
    188     * These must be kept unchanged even if segmentation is not used for
    189     * a frame, since the values persist between interframes.
    190     */
    191    struct {
    192        uint8_t enabled;
    193        uint8_t absolute_vals;
    194        uint8_t update_map;
    195        uint8_t update_feature_data;
    196        int8_t base_quant[4];
    197        int8_t filter_level[4];     ///< base loop filter level
    198    } segmentation;
    199 
    200    struct {
    201        uint8_t simple;
    202        uint8_t level;
    203        uint8_t sharpness;
    204    } filter;
    205 
    206    VP8Macroblock *macroblocks;
    207 
    208    uint8_t *intra4x4_pred_mode_top;
    209    uint8_t intra4x4_pred_mode_left[4];
    210 
    211    /**
    212     * Macroblocks can have one of 4 different quants in a frame when
    213     * segmentation is enabled.
    214     * If segmentation is disabled, only the first segment's values are used.
    215     */
    216    struct {
    217        // [0] - DC qmul  [1] - AC qmul
    218        int16_t luma_qmul[2];
    219        int16_t luma_dc_qmul[2];    ///< luma dc-only block quant
    220        int16_t chroma_qmul[2];
    221    } qmat[4];
    222 
    223    // Raw quantisation values, which may be needed by hwaccel decode.
    224    struct {
    225        int yac_qi;
    226        int ydc_delta;
    227        int y2dc_delta;
    228        int y2ac_delta;
    229        int uvdc_delta;
    230        int uvac_delta;
    231    } quant;
    232 
    233    struct {
    234        uint8_t enabled;    ///< whether each mb can have a different strength based on mode/ref
    235        uint8_t update;
    236 
    237        /**
    238         * filter strength adjustment for the following macroblock modes:
    239         * [0-3] - i16x16 (always zero)
    240         * [4]   - i4x4
    241         * [5]   - zero mv
    242         * [6]   - inter modes except for zero or split mv
    243         * [7]   - split mv
    244         *  i16x16 modes never have any adjustment
    245         */
    246        int8_t mode[VP8_MVMODE_SPLIT + 1];
    247 
    248        /**
    249         * filter strength adjustment for macroblocks that reference:
    250         * [0] - intra / VP8_FRAME_CURRENT
    251         * [1] - VP8_FRAME_PREVIOUS
    252         * [2] - VP8_FRAME_GOLDEN
    253         * [3] - altref / VP8_FRAME_ALTREF
    254         */
    255        int8_t ref[4];
    256    } lf_delta;
    257 
    258    uint8_t (*top_border)[16 + 8 + 8];
    259    uint8_t (*top_nnz)[9];
    260 
    261    VPXRangeCoder c;   ///< header context, includes mb modes and motion vectors
    262 
    263    /* This contains the entropy coder state at the end of the header
    264     * block, in the form specified by the standard.  For use by
    265     * hwaccels, so that a hardware decoder has the information to
    266     * start decoding at the macroblock layer.
    267     */
    268    struct {
    269        const uint8_t *input;
    270        uint32_t range;
    271        uint32_t value;
    272        int bit_count;
    273    } coder_state_at_header_end;
    274 
    275    int header_partition_size;
    276 
    277    /**
    278     * These are all of the updatable probabilities for binary decisions.
    279     * They are only implicitly reset on keyframes, making it quite likely
    280     * for an interframe to desync if a prior frame's header was corrupt
    281     * or missing outright!
    282     */
    283    struct {
    284        uint8_t segmentid[3];
    285        uint8_t mbskip;
    286        uint8_t intra;
    287        uint8_t last;
    288        uint8_t golden;
    289        uint8_t pred16x16[4];
    290        uint8_t pred8x8c[3];
    291        uint8_t token[4][16][3][NUM_DCT_TOKENS - 1];
    292        uint8_t mvc[2][19];
    293        uint8_t scan[16];
    294    } prob[2];
    295 
    296    VP8Macroblock *macroblocks_base;
    297    int invisible;
    298    int update_last;    ///< update VP8_FRAME_PREVIOUS with the current one
    299    int update_golden;  ///< VP8_FRAME_NONE if not updated, or which frame to copy if so
    300    int update_altref;
    301 
    302    /**
    303     * If this flag is not set, all the probability updates
    304     * are discarded after this frame is decoded.
    305     */
    306    int update_probabilities;
    307 
    308    /**
    309     * All coefficients are contained in separate arith coding contexts.
    310     * There can be 1, 2, 4, or 8 of these after the header context.
    311     */
    312    int num_coeff_partitions;
    313    VPXRangeCoder coeff_partition[8];
    314    int coeff_partition_size[8];
    315    VideoDSPContext vdsp;
    316    VP8DSPContext vp8dsp;
    317    H264PredContext hpc;
    318    vp8_mc_func put_pixels_tab[3][3][3];
    319    VP8Frame frames[5];
    320 
    321    uint8_t colorspace; ///< 0 is the only value allowed (meaning bt601)
    322    uint8_t fullrange;  ///< whether we can skip clamping in dsp functions
    323 
    324    int num_jobs;
    325    /**
    326     * This describes the macroblock memory layout.
    327     * 0 -> Only width+height*2+1 macroblocks allocated (frame/single thread).
    328     * 1 -> Macroblocks for entire frame allocated (sliced thread).
    329     */
    330    int mb_layout;
    331 
    332    int (*decode_mb_row_no_filter)(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr);
    333    void (*filter_mb_row)(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr);
    334 
    335    /**
    336     * Interframe DC prediction (VP7)
    337     * [0] VP8_FRAME_PREVIOUS
    338     * [1] VP8_FRAME_GOLDEN
    339     */
    340    uint16_t inter_dc_pred[2][2];
    341 
    342    /**
    343     * Macroblock features (VP7)
    344     */
    345    uint8_t feature_enabled[4];
    346    uint8_t feature_present_prob[4];
    347    uint8_t feature_index_prob[4][3];
    348    uint8_t feature_value[4][4];
    349 } VP8Context;
    350 
    351 int ff_vp8_decode_init(AVCodecContext *avctx);
    352 
    353 int ff_vp8_decode_frame(AVCodecContext *avctx, AVFrame *frame,
    354                        int *got_frame, AVPacket *avpkt);
    355 
    356 int ff_vp8_decode_free(AVCodecContext *avctx);
    357 
    358 #endif /* AVCODEC_VP8_H */