]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp9.h
qsvdec: do not sync PIX_FMT_QSV surfaces
[ffmpeg] / libavcodec / vp9.h
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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; 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_VP9_H
25 #define AVCODEC_VP9_H
26
27 #include <stddef.h>
28 #include <stdint.h>
29
30 #include "libavutil/buffer.h"
31 #include "libavutil/internal.h"
32
33 #include "avcodec.h"
34 #include "thread.h"
35 #include "vp56.h"
36
37 enum TxfmMode {
38     TX_4X4,
39     TX_8X8,
40     TX_16X16,
41     TX_32X32,
42     N_TXFM_SIZES,
43     TX_SWITCHABLE = N_TXFM_SIZES,
44     N_TXFM_MODES
45 };
46
47 enum TxfmType {
48     DCT_DCT,
49     DCT_ADST,
50     ADST_DCT,
51     ADST_ADST,
52     N_TXFM_TYPES
53 };
54
55 enum IntraPredMode {
56     VERT_PRED,
57     HOR_PRED,
58     DC_PRED,
59     DIAG_DOWN_LEFT_PRED,
60     DIAG_DOWN_RIGHT_PRED,
61     VERT_RIGHT_PRED,
62     HOR_DOWN_PRED,
63     VERT_LEFT_PRED,
64     HOR_UP_PRED,
65     TM_VP8_PRED,
66     LEFT_DC_PRED,
67     TOP_DC_PRED,
68     DC_128_PRED,
69     DC_127_PRED,
70     DC_129_PRED,
71     N_INTRA_PRED_MODES
72 };
73
74 enum FilterMode {
75     FILTER_8TAP_SMOOTH,
76     FILTER_8TAP_REGULAR,
77     FILTER_8TAP_SHARP,
78     FILTER_BILINEAR,
79     FILTER_SWITCHABLE,
80 };
81
82 enum BlockPartition {
83     PARTITION_NONE,    // [ ] <-.
84     PARTITION_H,       // [-]   |
85     PARTITION_V,       // [|]   |
86     PARTITION_SPLIT,   // [+] --'
87 };
88
89 enum InterPredMode {
90     NEARESTMV = 10,
91     NEARMV    = 11,
92     ZEROMV    = 12,
93     NEWMV     = 13,
94 };
95
96 enum MVJoint {
97     MV_JOINT_ZERO,
98     MV_JOINT_H,
99     MV_JOINT_V,
100     MV_JOINT_HV,
101 };
102
103 typedef struct ProbContext {
104     uint8_t y_mode[4][9];
105     uint8_t uv_mode[10][9];
106     uint8_t filter[4][2];
107     uint8_t mv_mode[7][3];
108     uint8_t intra[4];
109     uint8_t comp[5];
110     uint8_t single_ref[5][2];
111     uint8_t comp_ref[5];
112     uint8_t tx32p[2][3];
113     uint8_t tx16p[2][2];
114     uint8_t tx8p[2];
115     uint8_t skip[3];
116     uint8_t mv_joint[3];
117     struct {
118         uint8_t sign;
119         uint8_t classes[10];
120         uint8_t class0;
121         uint8_t bits[10];
122         uint8_t class0_fp[2][3];
123         uint8_t fp[3];
124         uint8_t class0_hp;
125         uint8_t hp;
126     } mv_comp[2];
127     uint8_t partition[4][4][3];
128 } ProbContext;
129
130 typedef void (*vp9_mc_func)(uint8_t *dst, ptrdiff_t dst_stride,
131                             const uint8_t *ref, ptrdiff_t ref_stride,
132                             int h, int mx, int my);
133
134 typedef struct VP9DSPContext {
135     /*
136      * dimension 1: 0=4x4, 1=8x8, 2=16x16, 3=32x32
137      * dimension 2: intra prediction modes
138      *
139      * dst/left/top is aligned by transform-size (i.e. 4, 8, 16 or 32 pixels)
140      * stride is aligned by 16 pixels
141      * top[-1] is top/left; top[4,7] is top-right for 4x4
142      */
143     // FIXME(rbultje) maybe replace left/top pointers with HAVE_TOP/
144     // HAVE_LEFT/HAVE_TOPRIGHT flags instead, and then handle it in-place?
145     // also needs to fit in with what H.264/VP8/etc do
146     void (*intra_pred[N_TXFM_SIZES][N_INTRA_PRED_MODES])(uint8_t *dst,
147                                                          ptrdiff_t stride,
148                                                          const uint8_t *left,
149                                                          const uint8_t *top);
150
151     /*
152      * dimension 1: 0=4x4, 1=8x8, 2=16x16, 3=32x32, 4=lossless (3-4=dct only)
153      * dimension 2: 0=dct/dct, 1=dct/adst, 2=adst/dct, 3=adst/adst
154      *
155      * dst is aligned by transform-size (i.e. 4, 8, 16 or 32 pixels)
156      * stride is aligned by 16 pixels
157      * block is 16-byte aligned
158      * eob indicates the position (+1) of the last non-zero coefficient,
159      * in scan-order. This can be used to write faster versions, e.g. a
160      * dc-only 4x4/8x8/16x16/32x32, or a 4x4-only (eob<10) 8x8/16x16/32x32,
161      * etc.
162      */
163     // FIXME also write idct_add_block() versions for whole (inter) pred
164     // blocks, so we can do 2 4x4s at once
165     void (*itxfm_add[N_TXFM_SIZES + 1][N_TXFM_TYPES])(uint8_t *dst,
166                                                       ptrdiff_t stride,
167                                                       int16_t *block, int eob);
168
169     /*
170      * dimension 1: width of filter (0=4, 1=8, 2=16)
171      * dimension 2: 0=col-edge filter (h), 1=row-edge filter (v)
172      *
173      * dst/stride are aligned by 8
174      */
175     void (*loop_filter_8[3][2])(uint8_t *dst, ptrdiff_t stride,
176                                 int mb_lim, int lim, int hev_thr);
177
178     /*
179      * dimension 1: 0=col-edge filter (h), 1=row-edge filter (v)
180      *
181      * The width of filter is assumed to be 16; dst/stride are aligned by 16
182      */
183     void (*loop_filter_16[2])(uint8_t *dst, ptrdiff_t stride,
184                               int mb_lim, int lim, int hev_thr);
185
186     /*
187      * dimension 1/2: width of filter (0=4, 1=8) for each filter half
188      * dimension 3: 0=col-edge filter (h), 1=row-edge filter (v)
189      *
190      * dst/stride are aligned by operation size
191      * this basically calls loop_filter[d1][d3][0](), followed by
192      * loop_filter[d2][d3][0]() on the next 8 pixels
193      * mb_lim/lim/hev_thr contain two values in the lowest two bytes of the
194      * integer.
195      */
196     // FIXME perhaps a mix4 that operates on 32px (for AVX2)
197     void (*loop_filter_mix2[2][2][2])(uint8_t *dst, ptrdiff_t stride,
198                                       int mb_lim, int lim, int hev_thr);
199
200     /*
201      * dimension 1: hsize (0: 64, 1: 32, 2: 16, 3: 8, 4: 4)
202      * dimension 2: filter type (0: smooth, 1: regular, 2: sharp, 3: bilin)
203      * dimension 3: averaging type (0: put, 1: avg)
204      * dimension 4: x subpel interpolation (0: none, 1: 8tap/bilin)
205      * dimension 5: y subpel interpolation (1: none, 1: 8tap/bilin)
206      *
207      * dst/stride are aligned by hsize
208      */
209     vp9_mc_func mc[5][4][2][2][2];
210 } VP9DSPContext;
211
212 enum CompPredMode {
213     PRED_SINGLEREF,
214     PRED_COMPREF,
215     PRED_SWITCHABLE,
216 };
217
218 typedef struct VP9MVRefPair {
219     VP56mv mv[2];
220     int8_t ref[2];
221 } VP9MVRefPair;
222
223 typedef struct VP9Filter {
224     uint8_t level[8 * 8];
225     uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
226                               [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
227 } VP9Filter;
228
229 typedef struct VP9Frame {
230     ThreadFrame tf;
231
232     uint8_t *segmentation_map;
233     VP9MVRefPair *mv;
234
235     AVBufferRef *segmentation_map_buf;
236     AVBufferRef *mv_buf;
237 } VP9Frame;
238
239 enum BlockLevel {
240     BL_64X64,
241     BL_32X32,
242     BL_16X16,
243     BL_8X8,
244 };
245
246 enum BlockSize {
247     BS_64x64,
248     BS_64x32,
249     BS_32x64,
250     BS_32x32,
251     BS_32x16,
252     BS_16x32,
253     BS_16x16,
254     BS_16x8,
255     BS_8x16,
256     BS_8x8,
257     BS_8x4,
258     BS_4x8,
259     BS_4x4,
260     N_BS_SIZES,
261 };
262
263 typedef struct VP9Block {
264     uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
265     enum FilterMode filter;
266     VP56mv mv[4 /* b_idx */][2 /* ref */];
267     enum BlockSize bs;
268     enum TxfmMode tx, uvtx;
269
270     int row, row7, col, col7;
271     uint8_t *dst[3];
272     ptrdiff_t y_stride, uv_stride;
273
274     enum BlockLevel bl;
275     enum BlockPartition bp;
276 } VP9Block;
277
278 typedef struct VP9Context {
279     VP9DSPContext dsp;
280     VideoDSPContext vdsp;
281     GetBitContext gb;
282     VP56RangeCoder c;
283     VP56RangeCoder *c_b;
284     unsigned c_b_size;
285     VP9Block *b;
286     VP9Block *b_base;
287
288     int alloc_width;
289     int alloc_height;
290
291     int pass;
292     int uses_2pass;
293     int last_uses_2pass;
294     int setup_finished;
295
296     // bitstream header
297     uint8_t profile;
298     uint8_t keyframe, last_keyframe;
299     uint8_t invisible;
300     uint8_t use_last_frame_mvs;
301     uint8_t errorres;
302     uint8_t colorspace;
303     uint8_t sub_x;
304     uint8_t sub_y;
305     uint8_t fullrange;
306     uint8_t intraonly;
307     uint8_t resetctx;
308     uint8_t refreshrefmask;
309     uint8_t highprecisionmvs;
310     enum FilterMode filtermode;
311     uint8_t allowcompinter;
312     uint8_t fixcompref;
313     uint8_t refreshctx;
314     uint8_t parallelmode;
315     uint8_t framectxid;
316     uint8_t refidx[3];
317     uint8_t signbias[3];
318     uint8_t varcompref[2];
319
320     ThreadFrame refs[8];
321
322 #define CUR_FRAME 0
323 #define LAST_FRAME 1
324     VP9Frame frames[2];
325
326     struct {
327         uint8_t level;
328         int8_t sharpness;
329         uint8_t lim_lut[64];
330         uint8_t mblim_lut[64];
331     } filter;
332     struct {
333         uint8_t enabled;
334         int8_t mode[2];
335         int8_t ref[4];
336     } lf_delta;
337     uint8_t yac_qi;
338     int8_t ydc_qdelta, uvdc_qdelta, uvac_qdelta;
339     uint8_t lossless;
340     struct {
341         uint8_t enabled;
342         uint8_t temporal;
343         uint8_t absolute_vals;
344         uint8_t update_map;
345         #define MAX_SEGMENT 8
346         struct {
347             uint8_t q_enabled;
348             uint8_t lf_enabled;
349             uint8_t ref_enabled;
350             uint8_t skip_enabled;
351             uint8_t ref_val;
352             int16_t q_val;
353             int8_t lf_val;
354             int16_t qmul[2][2];
355             uint8_t lflvl[4][2];
356         } feat[MAX_SEGMENT];
357     } segmentation;
358     struct {
359         unsigned log2_tile_cols, log2_tile_rows;
360         unsigned tile_cols, tile_rows;
361         unsigned tile_row_start, tile_row_end, tile_col_start, tile_col_end;
362     } tiling;
363     unsigned sb_cols, sb_rows, rows, cols;
364     struct {
365         ProbContext p;
366         uint8_t coef[4][2][2][6][6][3];
367     } prob_ctx[4];
368     struct {
369         ProbContext p;
370         uint8_t coef[4][2][2][6][6][11];
371         uint8_t seg[7];
372         uint8_t segpred[3];
373     } prob;
374     struct {
375         unsigned y_mode[4][10];
376         unsigned uv_mode[10][10];
377         unsigned filter[4][3];
378         unsigned mv_mode[7][4];
379         unsigned intra[4][2];
380         unsigned comp[5][2];
381         unsigned single_ref[5][2][2];
382         unsigned comp_ref[5][2];
383         unsigned tx32p[2][4];
384         unsigned tx16p[2][3];
385         unsigned tx8p[2][2];
386         unsigned skip[3][2];
387         unsigned mv_joint[4];
388         struct {
389             unsigned sign[2];
390             unsigned classes[11];
391             unsigned class0[2];
392             unsigned bits[10][2];
393             unsigned class0_fp[2][4];
394             unsigned fp[4];
395             unsigned class0_hp[2];
396             unsigned hp[2];
397         } mv_comp[2];
398         unsigned partition[4][4][4];
399         unsigned coef[4][2][2][6][6][3];
400         unsigned eob[4][2][2][6][6][2];
401     } counts;
402     enum TxfmMode txfmmode;
403     enum CompPredMode comppredmode;
404
405     // contextual (left/above) cache
406     uint8_t left_partition_ctx[8], *above_partition_ctx;
407     uint8_t left_mode_ctx[16], *above_mode_ctx;
408     // FIXME maybe merge some of the below in a flags field?
409     uint8_t left_y_nnz_ctx[16], *above_y_nnz_ctx;
410     uint8_t left_uv_nnz_ctx[2][8], *above_uv_nnz_ctx[2];
411     uint8_t left_skip_ctx[8], *above_skip_ctx; // 1bit
412     uint8_t left_txfm_ctx[8], *above_txfm_ctx; // 2bit
413     uint8_t left_segpred_ctx[8], *above_segpred_ctx; // 1bit
414     uint8_t left_intra_ctx[8], *above_intra_ctx; // 1bit
415     uint8_t left_comp_ctx[8], *above_comp_ctx; // 1bit
416     uint8_t left_ref_ctx[8], *above_ref_ctx; // 2bit
417     uint8_t left_filter_ctx[8], *above_filter_ctx;
418     VP56mv left_mv_ctx[16][2], (*above_mv_ctx)[2];
419
420     // whole-frame cache
421     uint8_t *intra_pred_data[3];
422     VP9Filter *lflvl;
423     // This requires 64 + 8 rows, with 80 bytes stride
424     DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[72 * 80];
425
426     // block reconstruction intermediates
427     int16_t *block_base, *block, *uvblock_base[2], *uvblock[2];
428     uint8_t *eob_base, *uveob_base[2], *eob, *uveob[2];
429     struct { int x, y; } min_mv, max_mv;
430     DECLARE_ALIGNED(32, uint8_t, tmp_y)[64 * 64];
431     DECLARE_ALIGNED(32, uint8_t, tmp_uv)[2][32 * 32];
432 } VP9Context;
433
434 extern const int8_t ff_vp9_subpel_filters[3][15][8];
435
436 void ff_vp9dsp_init(VP9DSPContext *dsp);
437
438 void ff_vp9dsp_init_aarch64(VP9DSPContext *dsp);
439 void ff_vp9dsp_init_arm(VP9DSPContext *dsp);
440 void ff_vp9dsp_init_x86(VP9DSPContext *dsp);
441
442 void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb);
443
444 void ff_vp9_adapt_probs(VP9Context *s);
445
446 int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col,
447                         VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
448                         enum BlockLevel bl, enum BlockPartition bp);
449
450 #endif /* AVCODEC_VP9_H */