OSDN Git Service

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