xref: /third_party/ffmpeg/libavcodec/vp9dec.h (revision cabdff1a)
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/buffer.h"
32#include "libavutil/mem_internal.h"
33#include "libavutil/thread.h"
34#include "libavutil/internal.h"
35
36#include "vp9.h"
37#include "vp9dsp.h"
38#include "vp9shared.h"
39
40#define REF_INVALID_SCALE 0xFFFF
41
42enum MVJoint {
43    MV_JOINT_ZERO,
44    MV_JOINT_H,
45    MV_JOINT_V,
46    MV_JOINT_HV,
47};
48
49typedef struct ProbContext {
50    uint8_t y_mode[4][9];
51    uint8_t uv_mode[10][9];
52    uint8_t filter[4][2];
53    uint8_t mv_mode[7][3];
54    uint8_t intra[4];
55    uint8_t comp[5];
56    uint8_t single_ref[5][2];
57    uint8_t comp_ref[5];
58    uint8_t tx32p[2][3];
59    uint8_t tx16p[2][2];
60    uint8_t tx8p[2];
61    uint8_t skip[3];
62    uint8_t mv_joint[3];
63    struct {
64        uint8_t sign;
65        uint8_t classes[10];
66        uint8_t class0;
67        uint8_t bits[10];
68        uint8_t class0_fp[2][3];
69        uint8_t fp[3];
70        uint8_t class0_hp;
71        uint8_t hp;
72    } mv_comp[2];
73    uint8_t partition[4][4][3];
74} ProbContext;
75
76typedef struct VP9Filter {
77    uint8_t level[8 * 8];
78    uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
79                              [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
80} VP9Filter;
81
82typedef struct VP9Block {
83    uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
84    enum FilterMode filter;
85    VP56mv mv[4 /* b_idx */][2 /* ref */];
86    enum BlockSize bs;
87    enum TxfmMode tx, uvtx;
88    enum BlockLevel bl;
89    enum BlockPartition bp;
90} VP9Block;
91
92typedef struct VP9TileData VP9TileData;
93
94typedef struct VP9Context {
95    VP9SharedContext s;
96    VP9TileData *td;
97
98    VP9DSPContext dsp;
99    VideoDSPContext vdsp;
100    GetBitContext gb;
101    VP56RangeCoder c;
102    int pass, active_tile_cols;
103
104#if HAVE_THREADS
105    pthread_mutex_t progress_mutex;
106    pthread_cond_t progress_cond;
107    atomic_int *entries;
108    unsigned pthread_init_cnt;
109#endif
110
111    uint8_t ss_h, ss_v;
112    uint8_t last_bpp, bpp_index, bytesperpixel;
113    uint8_t last_keyframe;
114    // sb_cols/rows, rows/cols and last_fmt are used for allocating all internal
115    // arrays, and are thus per-thread. w/h and gf_fmt are synced between threads
116    // and are therefore per-stream. pix_fmt represents the value in the header
117    // of the currently processed frame.
118    int w, h;
119    enum AVPixelFormat pix_fmt, last_fmt, gf_fmt;
120    unsigned sb_cols, sb_rows, rows, cols;
121    ThreadFrame next_refs[8];
122
123    struct {
124        uint8_t lim_lut[64];
125        uint8_t mblim_lut[64];
126    } filter_lut;
127    struct {
128        ProbContext p;
129        uint8_t coef[4][2][2][6][6][3];
130    } prob_ctx[4];
131    struct {
132        ProbContext p;
133        uint8_t coef[4][2][2][6][6][11];
134    } prob;
135
136    // contextual (above) cache
137    uint8_t *above_partition_ctx;
138    uint8_t *above_mode_ctx;
139    // FIXME maybe merge some of the below in a flags field?
140    uint8_t *above_y_nnz_ctx;
141    uint8_t *above_uv_nnz_ctx[2];
142    uint8_t *above_skip_ctx; // 1bit
143    uint8_t *above_txfm_ctx; // 2bit
144    uint8_t *above_segpred_ctx; // 1bit
145    uint8_t *above_intra_ctx; // 1bit
146    uint8_t *above_comp_ctx; // 1bit
147    uint8_t *above_ref_ctx; // 2bit
148    uint8_t *above_filter_ctx;
149    VP56mv (*above_mv_ctx)[2];
150
151    // whole-frame cache
152    uint8_t *intra_pred_data[3];
153    VP9Filter *lflvl;
154
155    // block reconstruction intermediates
156    int block_alloc_using_2pass;
157    uint16_t mvscale[3][2];
158    uint8_t mvstep[3][2];
159
160    // frame specific buffer pools
161    AVBufferPool *frame_extradata_pool;
162    int frame_extradata_pool_size;
163} VP9Context;
164
165struct VP9TileData {
166    //VP9Context should be const, but because of the threading API(generates
167    //a lot of warnings) it's not.
168    VP9Context *s;
169    VP56RangeCoder *c_b;
170    VP56RangeCoder *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, VP56mv, 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
240void ff_vp9_fill_mv(VP9TileData *td, VP56mv *mv, int mode, int sb);
241
242void ff_vp9_adapt_probs(VP9Context *s);
243
244void 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
248void ff_vp9_loopfilter_sb(AVCodecContext *avctx, VP9Filter *lflvl,
249                          int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff);
250
251void ff_vp9_intra_recon_8bpp(VP9TileData *td,
252                             ptrdiff_t y_off, ptrdiff_t uv_off);
253void ff_vp9_intra_recon_16bpp(VP9TileData *td,
254                              ptrdiff_t y_off, ptrdiff_t uv_off);
255void ff_vp9_inter_recon_8bpp(VP9TileData *td);
256void ff_vp9_inter_recon_16bpp(VP9TileData *td);
257
258#endif /* AVCODEC_VP9DEC_H */
259