1 /*
2  * Copyright © 2018, VideoLAN and dav1d authors
3  * Copyright © 2018, Two Orioles, LLC
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *    list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef DAV1D_SRC_INTERNAL_H
29 #define DAV1D_SRC_INTERNAL_H
30 
31 #include <stdatomic.h>
32 
33 #include "dav1d/data.h"
34 
35 typedef struct Dav1dFrameContext Dav1dFrameContext;
36 typedef struct Dav1dTileState Dav1dTileState;
37 typedef struct Dav1dTileContext Dav1dTileContext;
38 
39 #include "common/attributes.h"
40 
41 #include "src/cdef.h"
42 #include "src/cdf.h"
43 #include "src/data.h"
44 #include "src/env.h"
45 #include "src/film_grain.h"
46 #include "src/intra_edge.h"
47 #include "src/ipred.h"
48 #include "src/itx.h"
49 #include "src/levels.h"
50 #include "src/lf_mask.h"
51 #include "src/loopfilter.h"
52 #include "src/looprestoration.h"
53 #include "src/mc.h"
54 #include "src/msac.h"
55 #include "src/picture.h"
56 #include "src/recon.h"
57 #include "src/refmvs.h"
58 #include "src/thread.h"
59 
60 typedef struct Dav1dDSPContext {
61     Dav1dFilmGrainDSPContext fg;
62     Dav1dIntraPredDSPContext ipred;
63     Dav1dMCDSPContext mc;
64     Dav1dInvTxfmDSPContext itx;
65     Dav1dLoopFilterDSPContext lf;
66     Dav1dCdefDSPContext cdef;
67     Dav1dLoopRestorationDSPContext lr;
68 } Dav1dDSPContext;
69 
70 struct Dav1dTileGroup {
71     Dav1dData data;
72     int start, end;
73 };
74 
75 struct Dav1dContext {
76     Dav1dFrameContext *fc;
77     unsigned n_fc;
78 
79     // cache of OBUs that make up a single frame before we submit them
80     // to a frame worker to be decoded
81     struct Dav1dTileGroup *tile;
82     int n_tile_data_alloc;
83     int n_tile_data;
84     int n_tiles;
85     Dav1dRef *seq_hdr_ref;
86     Dav1dSequenceHeader *seq_hdr;
87     Dav1dRef *frame_hdr_ref;
88     Dav1dFrameHeader *frame_hdr;
89 
90     Dav1dRef *content_light_ref;
91     Dav1dContentLightLevel *content_light;
92     Dav1dRef *mastering_display_ref;
93     Dav1dMasteringDisplay *mastering_display;
94     Dav1dRef *itut_t35_ref;
95     Dav1dITUTT35 *itut_t35;
96 
97     // decoded output picture queue
98     Dav1dData in;
99     Dav1dPicture out;
100     struct {
101         Dav1dThreadPicture *out_delayed;
102         unsigned next;
103         // dummy is a pointer to prevent compiler errors about atomic_load()
104         // not taking const arguments; the const attribute is not taken
105         // from pointers
106         atomic_int flush_mem, *flush;
107     } frame_thread;
108 
109     // reference/entropy state
110     struct {
111         Dav1dThreadPicture p;
112         Dav1dRef *segmap;
113         Dav1dRef *refmvs;
114         unsigned refpoc[7];
115     } refs[8];
116     CdfThreadContext cdf[8];
117 
118     Dav1dDSPContext dsp[3 /* 8, 10, 12 bits/component */];
119 
120     // tree to keep track of which edges are available
121     struct {
122         EdgeNode *root[2 /* BL_128X128 vs. BL_64X64 */];
123         EdgeBranch branch_sb128[1 + 4 + 16 + 64];
124         EdgeBranch branch_sb64[1 + 4 + 16];
125         EdgeTip tip_sb128[256];
126         EdgeTip tip_sb64[64];
127     } intra_edge;
128 
129     Dav1dPicAllocator allocator;
130     int apply_grain;
131     int operating_point;
132     unsigned operating_point_idc;
133     int all_layers;
134     unsigned frame_size_limit;
135     int drain;
136 
137     Dav1dLogger logger;
138 };
139 
140 struct Dav1dFrameContext {
141     Dav1dRef *seq_hdr_ref;
142     Dav1dSequenceHeader *seq_hdr;
143     Dav1dRef *frame_hdr_ref;
144     Dav1dFrameHeader *frame_hdr;
145     Dav1dThreadPicture refp[7];
146     Dav1dPicture cur; // during block coding / reconstruction
147     Dav1dThreadPicture sr_cur; // after super-resolution upscaling
148     Dav1dRef *mvs_ref;
149     refmvs_temporal_block *mvs, *ref_mvs[7];
150     Dav1dRef *ref_mvs_ref[7];
151     Dav1dRef *cur_segmap_ref, *prev_segmap_ref;
152     uint8_t *cur_segmap;
153     const uint8_t *prev_segmap;
154     unsigned refpoc[7], refrefpoc[7][7];
155     uint8_t gmv_warp_allowed[7];
156     CdfThreadContext in_cdf, out_cdf;
157     struct Dav1dTileGroup *tile;
158     int n_tile_data_alloc;
159     int n_tile_data;
160 
161     // for scalable references
162     struct ScalableMotionParams {
163         int scale; // if no scaling, this is 0
164         int step;
165     } svc[7][2 /* x, y */];
166     int resize_step[2 /* y, uv */], resize_start[2 /* y, uv */];
167 
168     const Dav1dContext *c;
169     Dav1dTileContext *tc;
170     int n_tc;
171     Dav1dTileState *ts;
172     int n_ts;
173     const Dav1dDSPContext *dsp;
174     struct {
175         recon_b_intra_fn recon_b_intra;
176         recon_b_inter_fn recon_b_inter;
177         filter_sbrow_fn filter_sbrow;
178         backup_ipred_edge_fn backup_ipred_edge;
179         read_coef_blocks_fn read_coef_blocks;
180     } bd_fn;
181 
182     int ipred_edge_sz;
183     pixel *ipred_edge[3];
184     ptrdiff_t b4_stride;
185     int w4, h4, bw, bh, sb128w, sb128h, sbh, sb_shift, sb_step, sr_sb128w;
186     uint16_t dq[DAV1D_MAX_SEGMENTS][3 /* plane */][2 /* dc/ac */];
187     const uint8_t *qm[2 /* is_1d */][N_RECT_TX_SIZES][3 /* plane */];
188     BlockContext *a;
189     int a_sz /* w*tile_rows */;
190     refmvs_frame rf;
191     uint8_t jnt_weights[7][7];
192     int bitdepth_max;
193 
194     struct {
195         struct thread_data td;
196         int pass, die;
197         // indexed using t->by * f->b4_stride + t->bx
198         Av1Block *b;
199         struct CodedBlockInfo {
200             int16_t eob[3 /* plane */];
201             uint8_t txtp[3 /* plane */];
202         } *cbi;
203         // indexed using (t->by >> 1) * (f->b4_stride >> 1) + (t->bx >> 1)
204         uint16_t (*pal)[3 /* plane */][8 /* idx */];
205         // iterated over inside tile state
206         uint8_t *pal_idx;
207         coef *cf;
208         int pal_sz, pal_idx_sz, cf_sz;
209         // start offsets per tile
210         int *tile_start_off;
211     } frame_thread;
212 
213     // loopfilter
214     struct {
215         uint8_t (*level)[4];
216         Av1Filter *mask;
217         Av1Restoration *lr_mask;
218         int top_pre_cdef_toggle;
219         int mask_sz /* w*h */, lr_mask_sz, cdef_line_sz[2] /* stride */;
220         int lr_line_sz, re_sz /* h */;
221         ALIGN(Av1FilterLUT lim_lut, 16);
222         int last_sharpness;
223         uint8_t lvl[8 /* seg_id */][4 /* dir */][8 /* ref */][2 /* is_gmv */];
224         uint8_t *tx_lpf_right_edge[2];
225         uint8_t *cdef_line_buf;
226         pixel *cdef_line[2 /* pre, post */][3 /* plane */];
227         pixel *lr_lpf_line[3 /* plane */];
228 
229         // in-loop filter per-frame state keeping
230         int tile_row; // for carry-over at tile row edges
231         pixel *p[3], *sr_p[3];
232         Av1Filter *mask_ptr, *prev_mask_ptr;
233         int restore_planes; // enum LrRestorePlanes
234     } lf;
235 
236     // threading (refer to tc[] for per-thread things)
237     struct FrameTileThreadData {
238         uint64_t available;
239         pthread_mutex_t lock;
240         pthread_cond_t cond, icond;
241         int tasks_left, num_tasks;
242         int (*task_idx_to_sby_and_tile_idx)[2];
243         int titsati_sz, titsati_init[2];
244         uint16_t titsati_index_rows[1 + DAV1D_MAX_TILE_ROWS];
245         int inited;
246     } tile_thread;
247 };
248 
249 struct Dav1dTileState {
250     CdfContext cdf;
251     MsacContext msac;
252 
253     struct {
254         int col_start, col_end, row_start, row_end; // in 4px units
255         int col, row; // in tile units
256     } tiling;
257 
258     atomic_int progress; // in sby units, TILE_ERROR after a decoding error
259     struct {
260         pthread_mutex_t lock;
261         pthread_cond_t cond;
262     } tile_thread;
263     struct {
264         uint8_t *pal_idx;
265         coef *cf;
266     } frame_thread;
267 
268     uint16_t dqmem[DAV1D_MAX_SEGMENTS][3 /* plane */][2 /* dc/ac */];
269     const uint16_t (*dq)[3][2];
270     int last_qidx;
271 
272     int8_t last_delta_lf[4];
273     uint8_t lflvlmem[8 /* seg_id */][4 /* dir */][8 /* ref */][2 /* is_gmv */];
274     const uint8_t (*lflvl)[4][8][2];
275 
276     Av1RestorationUnit *lr_ref[3];
277 };
278 
279 struct Dav1dTileContext {
280     const Dav1dFrameContext *f;
281     Dav1dTileState *ts;
282     int bx, by;
283     BlockContext l, *a;
284     ALIGN(union, 32) {
285         int16_t cf_8bpc [32 * 32];
286         int32_t cf_16bpc[32 * 32];
287     };
288     // FIXME types can be changed to pixel (and dynamically allocated)
289     // which would make copy/assign operations slightly faster?
290     uint16_t al_pal[2 /* a/l */][32 /* bx/y4 */][3 /* plane */][8 /* palette_idx */];
291     uint8_t pal_sz_uv[2 /* a/l */][32 /* bx4/by4 */];
292     uint8_t txtp_map[32 * 32]; // inter-only
293     refmvs_tile rt;
294     ALIGN(union, 64) {
295         struct {
296             union {
297                 uint8_t  lap_8bpc [128 * 32];
298                 uint16_t lap_16bpc[128 * 32];
299                 struct {
300                     int16_t compinter[2][128 * 128];
301                     uint8_t seg_mask[128 * 128];
302                 };
303             };
304             union {
305                 // stride=192 for non-SVC, or 320 for SVC
306                 uint8_t  emu_edge_8bpc [320 * (256 + 7)];
307                 uint16_t emu_edge_16bpc[320 * (256 + 7)];
308             };
309         };
310         struct {
311             union {
312                 uint8_t levels[32 * 34];
313                 struct {
314                     uint8_t pal_order[64][8];
315                     uint8_t pal_ctx[64];
316                 };
317             };
318             int16_t ac[32 * 32];
319             uint8_t pal_idx[2 * 64 * 64];
320             uint16_t pal[3 /* plane */][8 /* palette_idx */];
321             ALIGN(union, 32) {
322                 struct {
323                     uint8_t interintra_8bpc[64 * 64];
324                     uint8_t edge_8bpc[257];
325                 };
326                 struct {
327                     uint16_t interintra_16bpc[64 * 64];
328                     uint16_t edge_16bpc[257];
329                 };
330             };
331         };
332     } scratch;
333 
334     Dav1dWarpedMotionParams warpmv;
335     Av1Filter *lf_mask;
336     int8_t *cur_sb_cdef_idx_ptr;
337     // for chroma sub8x8, we need to know the filter for all 4 subblocks in
338     // a 4x4 area, but the top/left one can go out of cache already, so this
339     // keeps it accessible
340     enum Filter2d tl_4x4_filter;
341 
342     struct {
343         struct thread_data td;
344         struct FrameTileThreadData *fttd;
345         int die;
346     } tile_thread;
347 };
348 
349 #endif /* DAV1D_SRC_INTERNAL_H */
350