]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp9.h
Merge commit '07cac07c0c0360d67e73a7472214c79d6c520a4b'
[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 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_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 typedef void (*vp9_scaled_mc_func)(uint8_t *dst, ptrdiff_t dst_stride,
134                                    const uint8_t *ref, ptrdiff_t ref_stride,
135                                    int h, int mx, int my, int dx, int dy);
136
137 typedef struct VP9DSPContext {
138     /*
139      * dimension 1: 0=4x4, 1=8x8, 2=16x16, 3=32x32
140      * dimension 2: intra prediction modes
141      *
142      * dst/left/top is aligned by transform-size (i.e. 4, 8, 16 or 32 pixels)
143      * stride is aligned by 16 pixels
144      * top[-1] is top/left; top[4,7] is top-right for 4x4
145      */
146     // FIXME(rbultje) maybe replace left/top pointers with HAVE_TOP/
147     // HAVE_LEFT/HAVE_TOPRIGHT flags instead, and then handle it in-place?
148     // also needs to fit in with what H.264/VP8/etc do
149     void (*intra_pred[N_TXFM_SIZES][N_INTRA_PRED_MODES])(uint8_t *dst,
150                                                          ptrdiff_t stride,
151                                                          const uint8_t *left,
152                                                          const uint8_t *top);
153
154     /*
155      * dimension 1: 0=4x4, 1=8x8, 2=16x16, 3=32x32, 4=lossless (3-4=dct only)
156      * dimension 2: 0=dct/dct, 1=dct/adst, 2=adst/dct, 3=adst/adst
157      *
158      * dst is aligned by transform-size (i.e. 4, 8, 16 or 32 pixels)
159      * stride is aligned by 16 pixels
160      * block is 16-byte aligned
161      * eob indicates the position (+1) of the last non-zero coefficient,
162      * in scan-order. This can be used to write faster versions, e.g. a
163      * dc-only 4x4/8x8/16x16/32x32, or a 4x4-only (eob<10) 8x8/16x16/32x32,
164      * etc.
165      */
166     // FIXME also write idct_add_block() versions for whole (inter) pred
167     // blocks, so we can do 2 4x4s at once
168     void (*itxfm_add[N_TXFM_SIZES + 1][N_TXFM_TYPES])(uint8_t *dst,
169                                                       ptrdiff_t stride,
170                                                       int16_t *block, int eob);
171
172     /*
173      * dimension 1: width of filter (0=4, 1=8, 2=16)
174      * dimension 2: 0=col-edge filter (h), 1=row-edge filter (v)
175      *
176      * dst/stride are aligned by 8
177      */
178     void (*loop_filter_8[3][2])(uint8_t *dst, ptrdiff_t stride,
179                                 int mb_lim, int lim, int hev_thr);
180
181     /*
182      * dimension 1: 0=col-edge filter (h), 1=row-edge filter (v)
183      *
184      * The width of filter is assumed to be 16; dst/stride are aligned by 16
185      */
186     void (*loop_filter_16[2])(uint8_t *dst, ptrdiff_t stride,
187                               int mb_lim, int lim, int hev_thr);
188
189     /*
190      * dimension 1/2: width of filter (0=4, 1=8) for each filter half
191      * dimension 3: 0=col-edge filter (h), 1=row-edge filter (v)
192      *
193      * dst/stride are aligned by operation size
194      * this basically calls loop_filter[d1][d3][0](), followed by
195      * loop_filter[d2][d3][0]() on the next 8 pixels
196      * mb_lim/lim/hev_thr contain two values in the lowest two bytes of the
197      * integer.
198      */
199     // FIXME perhaps a mix4 that operates on 32px (for AVX2)
200     void (*loop_filter_mix2[2][2][2])(uint8_t *dst, ptrdiff_t stride,
201                                       int mb_lim, int lim, int hev_thr);
202
203     /*
204      * dimension 1: hsize (0: 64, 1: 32, 2: 16, 3: 8, 4: 4)
205      * dimension 2: filter type (0: smooth, 1: regular, 2: sharp, 3: bilin)
206      * dimension 3: averaging type (0: put, 1: avg)
207      * dimension 4: x subpel interpolation (0: none, 1: 8tap/bilin)
208      * dimension 5: y subpel interpolation (0: none, 1: 8tap/bilin)
209      *
210      * dst/stride are aligned by hsize
211      */
212     vp9_mc_func mc[5][4][2][2][2];
213
214     /*
215      * for scalable MC, first 3 dimensions identical to above, the other two
216      * don't exist since it changes per stepsize.
217      */
218     vp9_scaled_mc_func smc[5][4][2];
219 } VP9DSPContext;
220
221 enum CompPredMode {
222     PRED_SINGLEREF,
223     PRED_COMPREF,
224     PRED_SWITCHABLE,
225 };
226
227 typedef struct VP9mvrefPair {
228     VP56mv mv[2];
229     int8_t ref[2];
230 } VP9mvrefPair;
231
232 typedef struct VP9Filter {
233     uint8_t level[8 * 8];
234     uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
235                               [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
236 } VP9Filter;
237
238 typedef struct VP9Frame {
239     ThreadFrame tf;
240     AVBufferRef *extradata;
241     uint8_t *segmentation_map;
242     VP9mvrefPair *mv;
243     int uses_2pass;
244
245     AVBufferRef *hwaccel_priv_buf;
246     void *hwaccel_picture_private;
247 } VP9Frame;
248
249 enum BlockLevel {
250     BL_64X64,
251     BL_32X32,
252     BL_16X16,
253     BL_8X8,
254 };
255
256 enum BlockSize {
257     BS_64x64,
258     BS_64x32,
259     BS_32x64,
260     BS_32x32,
261     BS_32x16,
262     BS_16x32,
263     BS_16x16,
264     BS_16x8,
265     BS_8x16,
266     BS_8x8,
267     BS_8x4,
268     BS_4x8,
269     BS_4x4,
270     N_BS_SIZES,
271 };
272
273 typedef struct VP9Block {
274     uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
275     enum FilterMode filter;
276     VP56mv mv[4 /* b_idx */][2 /* ref */];
277     enum BlockSize bs;
278     enum TxfmMode tx, uvtx;
279     enum BlockLevel bl;
280     enum BlockPartition bp;
281 } VP9Block;
282
283 typedef struct VP9BitstreamHeader {
284     // bitstream header
285     uint8_t profile;
286     uint8_t bpp;
287     uint8_t keyframe;
288     uint8_t invisible;
289     uint8_t errorres;
290     uint8_t intraonly;
291     uint8_t resetctx;
292     uint8_t refreshrefmask;
293     uint8_t highprecisionmvs;
294     enum FilterMode filtermode;
295     uint8_t allowcompinter;
296     uint8_t refreshctx;
297     uint8_t parallelmode;
298     uint8_t framectxid;
299     uint8_t use_last_frame_mvs;
300     uint8_t refidx[3];
301     uint8_t signbias[3];
302     uint8_t fixcompref;
303     uint8_t varcompref[2];
304     struct {
305         uint8_t level;
306         int8_t sharpness;
307     } filter;
308     struct {
309         uint8_t enabled;
310         uint8_t updated;
311         int8_t mode[2];
312         int8_t ref[4];
313     } lf_delta;
314     uint8_t yac_qi;
315     int8_t ydc_qdelta, uvdc_qdelta, uvac_qdelta;
316     uint8_t lossless;
317 #define MAX_SEGMENT 8
318     struct {
319         uint8_t enabled;
320         uint8_t temporal;
321         uint8_t absolute_vals;
322         uint8_t update_map;
323         uint8_t prob[7];
324         uint8_t pred_prob[3];
325         struct {
326             uint8_t q_enabled;
327             uint8_t lf_enabled;
328             uint8_t ref_enabled;
329             uint8_t skip_enabled;
330             uint8_t ref_val;
331             int16_t q_val;
332             int8_t lf_val;
333             int16_t qmul[2][2];
334             uint8_t lflvl[4][2];
335         } feat[MAX_SEGMENT];
336     } segmentation;
337     enum TxfmMode txfmmode;
338     enum CompPredMode comppredmode;
339     struct {
340         unsigned log2_tile_cols, log2_tile_rows;
341         unsigned tile_cols, tile_rows;
342     } tiling;
343
344     int uncompressed_header_size;
345     int compressed_header_size;
346 } VP9BitstreamHeader;
347
348 typedef struct VP9SharedContext {
349     VP9BitstreamHeader h;
350
351     ThreadFrame refs[8];
352 #define CUR_FRAME 0
353 #define REF_FRAME_MVPAIR 1
354 #define REF_FRAME_SEGMAP 2
355     VP9Frame frames[3];
356 } VP9SharedContext;
357
358 typedef struct VP9Context {
359     VP9SharedContext s;
360
361     VP9DSPContext dsp;
362     VideoDSPContext vdsp;
363     GetBitContext gb;
364     VP56RangeCoder c;
365     VP56RangeCoder *c_b;
366     unsigned c_b_size;
367     VP9Block *b_base, *b;
368     int pass;
369     int row, row7, col, col7;
370     uint8_t *dst[3];
371     ptrdiff_t y_stride, uv_stride;
372
373     uint8_t ss_h, ss_v;
374     uint8_t last_bpp, bpp_index, bytesperpixel;
375     uint8_t last_keyframe;
376     // sb_cols/rows, rows/cols and last_fmt are used for allocating all internal
377     // arrays, and are thus per-thread. w/h and gf_fmt are synced between threads
378     // and are therefore per-stream. pix_fmt represents the value in the header
379     // of the currently processed frame.
380     int w, h;
381     enum AVPixelFormat pix_fmt, last_fmt, gf_fmt;
382     unsigned sb_cols, sb_rows, rows, cols;
383     ThreadFrame next_refs[8];
384
385     struct {
386         uint8_t lim_lut[64];
387         uint8_t mblim_lut[64];
388     } filter_lut;
389     unsigned tile_row_start, tile_row_end, tile_col_start, tile_col_end;
390     struct {
391         ProbContext p;
392         uint8_t coef[4][2][2][6][6][3];
393     } prob_ctx[4];
394     struct {
395         ProbContext p;
396         uint8_t coef[4][2][2][6][6][11];
397     } prob;
398     struct {
399         unsigned y_mode[4][10];
400         unsigned uv_mode[10][10];
401         unsigned filter[4][3];
402         unsigned mv_mode[7][4];
403         unsigned intra[4][2];
404         unsigned comp[5][2];
405         unsigned single_ref[5][2][2];
406         unsigned comp_ref[5][2];
407         unsigned tx32p[2][4];
408         unsigned tx16p[2][3];
409         unsigned tx8p[2][2];
410         unsigned skip[3][2];
411         unsigned mv_joint[4];
412         struct {
413             unsigned sign[2];
414             unsigned classes[11];
415             unsigned class0[2];
416             unsigned bits[10][2];
417             unsigned class0_fp[2][4];
418             unsigned fp[4];
419             unsigned class0_hp[2];
420             unsigned hp[2];
421         } mv_comp[2];
422         unsigned partition[4][4][4];
423         unsigned coef[4][2][2][6][6][3];
424         unsigned eob[4][2][2][6][6][2];
425     } counts;
426
427     // contextual (left/above) cache
428     DECLARE_ALIGNED(16, uint8_t, left_y_nnz_ctx)[16];
429     DECLARE_ALIGNED(16, uint8_t, left_mode_ctx)[16];
430     DECLARE_ALIGNED(16, VP56mv, left_mv_ctx)[16][2];
431     DECLARE_ALIGNED(16, uint8_t, left_uv_nnz_ctx)[2][16];
432     DECLARE_ALIGNED(8, uint8_t, left_partition_ctx)[8];
433     DECLARE_ALIGNED(8, uint8_t, left_skip_ctx)[8];
434     DECLARE_ALIGNED(8, uint8_t, left_txfm_ctx)[8];
435     DECLARE_ALIGNED(8, uint8_t, left_segpred_ctx)[8];
436     DECLARE_ALIGNED(8, uint8_t, left_intra_ctx)[8];
437     DECLARE_ALIGNED(8, uint8_t, left_comp_ctx)[8];
438     DECLARE_ALIGNED(8, uint8_t, left_ref_ctx)[8];
439     DECLARE_ALIGNED(8, uint8_t, left_filter_ctx)[8];
440     uint8_t *above_partition_ctx;
441     uint8_t *above_mode_ctx;
442     // FIXME maybe merge some of the below in a flags field?
443     uint8_t *above_y_nnz_ctx;
444     uint8_t *above_uv_nnz_ctx[2];
445     uint8_t *above_skip_ctx; // 1bit
446     uint8_t *above_txfm_ctx; // 2bit
447     uint8_t *above_segpred_ctx; // 1bit
448     uint8_t *above_intra_ctx; // 1bit
449     uint8_t *above_comp_ctx; // 1bit
450     uint8_t *above_ref_ctx; // 2bit
451     uint8_t *above_filter_ctx;
452     VP56mv (*above_mv_ctx)[2];
453
454     // whole-frame cache
455     uint8_t *intra_pred_data[3];
456     VP9Filter *lflvl;
457     DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[135 * 144 * 2];
458
459     // block reconstruction intermediates
460     int block_alloc_using_2pass;
461     int16_t *block_base, *block, *uvblock_base[2], *uvblock[2];
462     uint8_t *eob_base, *uveob_base[2], *eob, *uveob[2];
463     struct { int x, y; } min_mv, max_mv;
464     DECLARE_ALIGNED(32, uint8_t, tmp_y)[64 * 64 * 2];
465     DECLARE_ALIGNED(32, uint8_t, tmp_uv)[2][64 * 64 * 2];
466     uint16_t mvscale[3][2];
467     uint8_t mvstep[3][2];
468 } VP9Context;
469
470 extern const int16_t ff_vp9_subpel_filters[3][16][8];
471
472 void ff_vp9dsp_init(VP9DSPContext *dsp, int bpp, int bitexact);
473
474 void ff_vp9dsp_init_8(VP9DSPContext *dsp);
475 void ff_vp9dsp_init_10(VP9DSPContext *dsp);
476 void ff_vp9dsp_init_12(VP9DSPContext *dsp);
477
478 void ff_vp9dsp_init_aarch64(VP9DSPContext *dsp, int bpp);
479 void ff_vp9dsp_init_arm(VP9DSPContext *dsp, int bpp);
480 void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int bpp, int bitexact);
481 void ff_vp9dsp_init_mips(VP9DSPContext *dsp, int bpp);
482
483 void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb);
484
485 void ff_vp9_adapt_probs(VP9Context *s);
486
487 void ff_vp9_decode_block(AVCodecContext *ctx, int row, int col,
488                          VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
489                          enum BlockLevel bl, enum BlockPartition bp);
490
491 #endif /* AVCODEC_VP9_H */