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/thread.h"
33 #include "libavutil/internal.h"
34 
35 #include "vp9.h"
36 #include "vp9dsp.h"
37 #include "vp9shared.h"
38 
39 enum MVJoint {
40     MV_JOINT_ZERO,
41     MV_JOINT_H,
42     MV_JOINT_V,
43     MV_JOINT_HV,
44 };
45 
46 typedef struct ProbContext {
47     uint8_t y_mode[4][9];
48     uint8_t uv_mode[10][9];
49     uint8_t filter[4][2];
50     uint8_t mv_mode[7][3];
51     uint8_t intra[4];
52     uint8_t comp[5];
53     uint8_t single_ref[5][2];
54     uint8_t comp_ref[5];
55     uint8_t tx32p[2][3];
56     uint8_t tx16p[2][2];
57     uint8_t tx8p[2];
58     uint8_t skip[3];
59     uint8_t mv_joint[3];
60     struct {
61         uint8_t sign;
62         uint8_t classes[10];
63         uint8_t class0;
64         uint8_t bits[10];
65         uint8_t class0_fp[2][3];
66         uint8_t fp[3];
67         uint8_t class0_hp;
68         uint8_t hp;
69     } mv_comp[2];
70     uint8_t partition[4][4][3];
71 } ProbContext;
72 
73 typedef struct VP9Filter {
74     uint8_t level[8 * 8];
75     uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
76                               [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
77 } VP9Filter;
78 
79 typedef struct VP9Block {
80     uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
81     enum FilterMode filter;
82     VP56mv mv[4 /* b_idx */][2 /* ref */];
83     enum BlockSize bs;
84     enum TxfmMode tx, uvtx;
85     enum BlockLevel bl;
86     enum BlockPartition bp;
87 } VP9Block;
88 
89 typedef struct VP9TileData VP9TileData;
90 
91 typedef struct VP9Context {
92     VP9SharedContext s;
93     VP9TileData *td;
94 
95     VP9DSPContext dsp;
96     VideoDSPContext vdsp;
97     GetBitContext gb;
98     VP56RangeCoder c;
99     int pass, active_tile_cols;
100 
101 #if HAVE_THREADS
102     pthread_mutex_t progress_mutex;
103     pthread_cond_t progress_cond;
104     atomic_int *entries;
105 #endif
106 
107     uint8_t ss_h, ss_v;
108     uint8_t last_bpp, bpp_index, bytesperpixel;
109     uint8_t last_keyframe;
110     // sb_cols/rows, rows/cols and last_fmt are used for allocating all internal
111     // arrays, and are thus per-thread. w/h and gf_fmt are synced between threads
112     // and are therefore per-stream. pix_fmt represents the value in the header
113     // of the currently processed frame.
114     int w, h;
115     enum AVPixelFormat pix_fmt, last_fmt, gf_fmt;
116     unsigned sb_cols, sb_rows, rows, cols;
117     ThreadFrame next_refs[8];
118 
119     struct {
120         uint8_t lim_lut[64];
121         uint8_t mblim_lut[64];
122     } filter_lut;
123     struct {
124         ProbContext p;
125         uint8_t coef[4][2][2][6][6][3];
126     } prob_ctx[4];
127     struct {
128         ProbContext p;
129         uint8_t coef[4][2][2][6][6][11];
130     } prob;
131 
132     // contextual (above) cache
133     uint8_t *above_partition_ctx;
134     uint8_t *above_mode_ctx;
135     // FIXME maybe merge some of the below in a flags field?
136     uint8_t *above_y_nnz_ctx;
137     uint8_t *above_uv_nnz_ctx[2];
138     uint8_t *above_skip_ctx; // 1bit
139     uint8_t *above_txfm_ctx; // 2bit
140     uint8_t *above_segpred_ctx; // 1bit
141     uint8_t *above_intra_ctx; // 1bit
142     uint8_t *above_comp_ctx; // 1bit
143     uint8_t *above_ref_ctx; // 2bit
144     uint8_t *above_filter_ctx;
145     VP56mv (*above_mv_ctx)[2];
146 
147     // whole-frame cache
148     uint8_t *intra_pred_data[3];
149     VP9Filter *lflvl;
150 
151     // block reconstruction intermediates
152     int block_alloc_using_2pass;
153     uint16_t mvscale[3][2];
154     uint8_t mvstep[3][2];
155 } VP9Context;
156 
157 struct VP9TileData {
158     //VP9Context should be const, but because of the threading API(generates
159     //a lot of warnings) it's not.
160     VP9Context *s;
161     VP56RangeCoder *c_b;
162     VP56RangeCoder *c;
163     int row, row7, col, col7;
164     uint8_t *dst[3];
165     ptrdiff_t y_stride, uv_stride;
166     VP9Block *b_base, *b;
167     unsigned tile_col_start;
168 
169     struct {
170         unsigned y_mode[4][10];
171         unsigned uv_mode[10][10];
172         unsigned filter[4][3];
173         unsigned mv_mode[7][4];
174         unsigned intra[4][2];
175         unsigned comp[5][2];
176         unsigned single_ref[5][2][2];
177         unsigned comp_ref[5][2];
178         unsigned tx32p[2][4];
179         unsigned tx16p[2][3];
180         unsigned tx8p[2][2];
181         unsigned skip[3][2];
182         unsigned mv_joint[4];
183         struct {
184             unsigned sign[2];
185             unsigned classes[11];
186             unsigned class0[2];
187             unsigned bits[10][2];
188             unsigned class0_fp[2][4];
189             unsigned fp[4];
190             unsigned class0_hp[2];
191             unsigned hp[2];
192         } mv_comp[2];
193         unsigned partition[4][4][4];
194         unsigned coef[4][2][2][6][6][3];
195         unsigned eob[4][2][2][6][6][2];
196     } counts;
197 
198     // whole-frame cache
199     DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[135 * 144 * 2];
200 
201     // contextual (left) cache
202     DECLARE_ALIGNED(16, uint8_t, left_y_nnz_ctx)[16];
203     DECLARE_ALIGNED(16, uint8_t, left_mode_ctx)[16];
204     DECLARE_ALIGNED(16, VP56mv, left_mv_ctx)[16][2];
205     DECLARE_ALIGNED(16, uint8_t, left_uv_nnz_ctx)[2][16];
206     DECLARE_ALIGNED(8, uint8_t, left_partition_ctx)[8];
207     DECLARE_ALIGNED(8, uint8_t, left_skip_ctx)[8];
208     DECLARE_ALIGNED(8, uint8_t, left_txfm_ctx)[8];
209     DECLARE_ALIGNED(8, uint8_t, left_segpred_ctx)[8];
210     DECLARE_ALIGNED(8, uint8_t, left_intra_ctx)[8];
211     DECLARE_ALIGNED(8, uint8_t, left_comp_ctx)[8];
212     DECLARE_ALIGNED(8, uint8_t, left_ref_ctx)[8];
213     DECLARE_ALIGNED(8, uint8_t, left_filter_ctx)[8];
214     // block reconstruction intermediates
215     DECLARE_ALIGNED(32, uint8_t, tmp_y)[64 * 64 * 2];
216     DECLARE_ALIGNED(32, uint8_t, tmp_uv)[2][64 * 64 * 2];
217     struct { int x, y; } min_mv, max_mv;
218     int16_t *block_base, *block, *uvblock_base[2], *uvblock[2];
219     uint8_t *eob_base, *uveob_base[2], *eob, *uveob[2];
220 };
221 
222 void ff_vp9_fill_mv(VP9TileData *td, VP56mv *mv, int mode, int sb);
223 
224 void ff_vp9_adapt_probs(VP9Context *s);
225 
226 void ff_vp9_decode_block(VP9TileData *td, int row, int col,
227                          VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
228                          enum BlockLevel bl, enum BlockPartition bp);
229 
230 void ff_vp9_loopfilter_sb(AVCodecContext *avctx, VP9Filter *lflvl,
231                           int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff);
232 
233 void ff_vp9_intra_recon_8bpp(VP9TileData *td,
234                              ptrdiff_t y_off, ptrdiff_t uv_off);
235 void ff_vp9_intra_recon_16bpp(VP9TileData *td,
236                               ptrdiff_t y_off, ptrdiff_t uv_off);
237 void ff_vp9_inter_recon_8bpp(VP9TileData *td);
238 void ff_vp9_inter_recon_16bpp(VP9TileData *td);
239 
240 #endif /* AVCODEC_VP9DEC_H */
241