tor-browser

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

vp9dec.h (7779B)


      1 /*
      2 * VP9 compatible video decoder
      3 *
      4 * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
      5 * Copyright (C) 2013 Clément Bœsch <u pkh me>
      6 *
      7 * This file is part of FFmpeg.
      8 *
      9 * FFmpeg is free software; you can redistribute it and/or
     10 * modify it under the terms of the GNU Lesser General Public
     11 * License as published by the Free Software Foundation; either
     12 * version 2.1 of the License, or (at your option) any later version.
     13 *
     14 * FFmpeg is distributed in the hope that it will be useful,
     15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     17 * Lesser General Public License for more details.
     18 *
     19 * You should have received a copy of the GNU Lesser General Public
     20 * License along with FFmpeg; if not, write to the Free Software
     21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     22 */
     23 
     24 #ifndef AVCODEC_VP9DEC_H
     25 #define AVCODEC_VP9DEC_H
     26 
     27 #include <stddef.h>
     28 #include <stdint.h>
     29 #include <stdatomic.h>
     30 
     31 #include "libavutil/mem_internal.h"
     32 #include "libavutil/pixfmt.h"
     33 #include "libavutil/thread.h"
     34 
     35 #include "get_bits.h"
     36 #include "videodsp.h"
     37 #include "vp9.h"
     38 #include "vp9dsp.h"
     39 #include "vp9shared.h"
     40 #include "vpx_rac.h"
     41 
     42 #define REF_INVALID_SCALE 0xFFFF
     43 
     44 enum MVJoint {
     45    MV_JOINT_ZERO,
     46    MV_JOINT_H,
     47    MV_JOINT_V,
     48    MV_JOINT_HV,
     49 };
     50 
     51 typedef struct ProbContext {
     52    uint8_t y_mode[4][9];
     53    uint8_t uv_mode[10][9];
     54    uint8_t filter[4][2];
     55    uint8_t mv_mode[7][3];
     56    uint8_t intra[4];
     57    uint8_t comp[5];
     58    uint8_t single_ref[5][2];
     59    uint8_t comp_ref[5];
     60    uint8_t tx32p[2][3];
     61    uint8_t tx16p[2][2];
     62    uint8_t tx8p[2];
     63    uint8_t skip[3];
     64    uint8_t mv_joint[3];
     65    struct {
     66        uint8_t sign;
     67        uint8_t classes[10];
     68        uint8_t class0;
     69        uint8_t bits[10];
     70        uint8_t class0_fp[2][3];
     71        uint8_t fp[3];
     72        uint8_t class0_hp;
     73        uint8_t hp;
     74    } mv_comp[2];
     75    uint8_t partition[4][4][3];
     76 } ProbContext;
     77 
     78 typedef struct VP9Filter {
     79    uint8_t level[8 * 8];
     80    uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
     81                              [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
     82 } VP9Filter;
     83 
     84 typedef struct VP9Block {
     85    uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
     86    enum FilterMode filter;
     87    VP9mv mv[4 /* b_idx */][2 /* ref */];
     88    enum BlockSize bs;
     89    enum TxfmMode tx, uvtx;
     90    enum BlockLevel bl;
     91    enum BlockPartition bp;
     92 } VP9Block;
     93 
     94 typedef struct VP9TileData VP9TileData;
     95 
     96 typedef struct VP9Context {
     97    VP9SharedContext s;
     98    VP9TileData *td;
     99 
    100    VP9DSPContext dsp;
    101    VideoDSPContext vdsp;
    102    GetBitContext gb;
    103    VPXRangeCoder c;
    104    int pass, active_tile_cols;
    105 
    106 #if HAVE_THREADS
    107    pthread_mutex_t progress_mutex;
    108    pthread_cond_t progress_cond;
    109    atomic_int *entries;
    110    unsigned pthread_init_cnt;
    111 #endif
    112 
    113    uint8_t ss_h, ss_v;
    114    uint8_t last_bpp, bpp_index, bytesperpixel;
    115    uint8_t last_keyframe;
    116    // sb_cols/rows, rows/cols and last_fmt are used for allocating all internal
    117    // arrays, and are thus per-thread. w/h and gf_fmt are synced between threads
    118    // and are therefore per-stream. pix_fmt represents the value in the header
    119    // of the currently processed frame.
    120    int w, h;
    121    enum AVPixelFormat pix_fmt, last_fmt, gf_fmt;
    122    unsigned sb_cols, sb_rows, rows, cols;
    123    ProgressFrame next_refs[8];
    124 
    125    struct {
    126        uint8_t lim_lut[64];
    127        uint8_t mblim_lut[64];
    128    } filter_lut;
    129    struct {
    130        ProbContext p;
    131        uint8_t coef[4][2][2][6][6][3];
    132    } prob_ctx[4];
    133    struct {
    134        ProbContext p;
    135        uint8_t coef[4][2][2][6][6][11];
    136    } prob;
    137 
    138    // contextual (above) cache
    139    uint8_t *above_partition_ctx;
    140    uint8_t *above_mode_ctx;
    141    // FIXME maybe merge some of the below in a flags field?
    142    uint8_t *above_y_nnz_ctx;
    143    uint8_t *above_uv_nnz_ctx[2];
    144    uint8_t *above_skip_ctx; // 1bit
    145    uint8_t *above_txfm_ctx; // 2bit
    146    uint8_t *above_segpred_ctx; // 1bit
    147    uint8_t *above_intra_ctx; // 1bit
    148    uint8_t *above_comp_ctx; // 1bit
    149    uint8_t *above_ref_ctx; // 2bit
    150    uint8_t *above_filter_ctx;
    151    VP9mv (*above_mv_ctx)[2];
    152 
    153    // whole-frame cache
    154    uint8_t *intra_pred_data[3];
    155    VP9Filter *lflvl;
    156 
    157    // block reconstruction intermediates
    158    int block_alloc_using_2pass;
    159    uint16_t mvscale[3][2];
    160    uint8_t mvstep[3][2];
    161 
    162    // frame specific buffer pools
    163    struct AVRefStructPool *frame_extradata_pool;
    164    int frame_extradata_pool_size;
    165 } VP9Context;
    166 
    167 struct VP9TileData {
    168    const VP9Context *s;
    169    VPXRangeCoder *c_b;
    170    VPXRangeCoder *c;
    171    int row, row7, col, col7;
    172    uint8_t *dst[3];
    173    ptrdiff_t y_stride, uv_stride;
    174    VP9Block *b_base, *b;
    175    unsigned tile_col_start;
    176 
    177    struct {
    178        unsigned y_mode[4][10];
    179        unsigned uv_mode[10][10];
    180        unsigned filter[4][3];
    181        unsigned mv_mode[7][4];
    182        unsigned intra[4][2];
    183        unsigned comp[5][2];
    184        unsigned single_ref[5][2][2];
    185        unsigned comp_ref[5][2];
    186        unsigned tx32p[2][4];
    187        unsigned tx16p[2][3];
    188        unsigned tx8p[2][2];
    189        unsigned skip[3][2];
    190        unsigned mv_joint[4];
    191        struct {
    192            unsigned sign[2];
    193            unsigned classes[11];
    194            unsigned class0[2];
    195            unsigned bits[10][2];
    196            unsigned class0_fp[2][4];
    197            unsigned fp[4];
    198            unsigned class0_hp[2];
    199            unsigned hp[2];
    200        } mv_comp[2];
    201        unsigned partition[4][4][4];
    202        unsigned coef[4][2][2][6][6][3];
    203        unsigned eob[4][2][2][6][6][2];
    204    } counts;
    205 
    206    // whole-frame cache
    207    DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[135 * 144 * 2];
    208 
    209    // contextual (left) cache
    210    DECLARE_ALIGNED(16, uint8_t, left_y_nnz_ctx)[16];
    211    DECLARE_ALIGNED(16, uint8_t, left_mode_ctx)[16];
    212    DECLARE_ALIGNED(16, VP9mv, left_mv_ctx)[16][2];
    213    DECLARE_ALIGNED(16, uint8_t, left_uv_nnz_ctx)[2][16];
    214    DECLARE_ALIGNED(8, uint8_t, left_partition_ctx)[8];
    215    DECLARE_ALIGNED(8, uint8_t, left_skip_ctx)[8];
    216    DECLARE_ALIGNED(8, uint8_t, left_txfm_ctx)[8];
    217    DECLARE_ALIGNED(8, uint8_t, left_segpred_ctx)[8];
    218    DECLARE_ALIGNED(8, uint8_t, left_intra_ctx)[8];
    219    DECLARE_ALIGNED(8, uint8_t, left_comp_ctx)[8];
    220    DECLARE_ALIGNED(8, uint8_t, left_ref_ctx)[8];
    221    DECLARE_ALIGNED(8, uint8_t, left_filter_ctx)[8];
    222    // block reconstruction intermediates
    223    DECLARE_ALIGNED(32, uint8_t, tmp_y)[64 * 64 * 2];
    224    DECLARE_ALIGNED(32, uint8_t, tmp_uv)[2][64 * 64 * 2];
    225    struct { int x, y; } min_mv, max_mv;
    226    int16_t *block_base, *block, *uvblock_base[2], *uvblock[2];
    227    uint8_t *eob_base, *uveob_base[2], *eob, *uveob[2];
    228 
    229    // error message
    230    int error_info;
    231    struct {
    232        unsigned int row:13;
    233        unsigned int col:13;
    234        unsigned int block_size_idx_x:2;
    235        unsigned int block_size_idx_y:2;
    236    } *block_structure;
    237    unsigned int nb_block_structure;
    238 };
    239 
    240 void ff_vp9_fill_mv(VP9TileData *td, VP9mv *mv, int mode, int sb);
    241 
    242 void ff_vp9_adapt_probs(VP9Context *s);
    243 
    244 void ff_vp9_decode_block(VP9TileData *td, int row, int col,
    245                         VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
    246                         enum BlockLevel bl, enum BlockPartition bp);
    247 
    248 void ff_vp9_loopfilter_sb(struct AVCodecContext *avctx, VP9Filter *lflvl,
    249                          int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff);
    250 
    251 void ff_vp9_intra_recon_8bpp(VP9TileData *td,
    252                             ptrdiff_t y_off, ptrdiff_t uv_off);
    253 void ff_vp9_intra_recon_16bpp(VP9TileData *td,
    254                              ptrdiff_t y_off, ptrdiff_t uv_off);
    255 void ff_vp9_inter_recon_8bpp(VP9TileData *td);
    256 void ff_vp9_inter_recon_16bpp(VP9TileData *td);
    257 
    258 #endif /* AVCODEC_VP9DEC_H */