1 /**************************************************************************
2  *
3  * Copyright 2020 Advanced Micro Devices, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #ifndef VID_DEC_AV1_H
29 #define VID_DEC_AV1_H
30 
31 #include "os/os_thread.h"
32 
33 enum av1_obu_type {
34    AV1_OBU_SEQUENCE_HEADER = 1,
35    AV1_OBU_TEMPORAL_DELIMITER,
36    AV1_OBU_FRAME_HEADER,
37    AV1_OBU_TILE_GROUP,
38    AV1_OBU_METADATA,
39    AV1_OBU_FRAME,
40    AV1_OBU_REDUNDANT_FRAME_HEADER,
41    AV1_OBU_TILE_LIST,
42    AV1_OBU_PADDING = 15
43 };
44 
45 enum av1_obu_frame_type {
46    AV1_KEY_FRAME = 0,
47    AV1_INTER_FRAME,
48    AV1_INTRA_ONLY_FRAME,
49    AV1_SWITCH_FRAME
50 };
51 
52 enum av1_obu_color_primary {
53    AV1_CP_BT_709 = 1,
54    AV1_CP_UNSPECIFIED,
55    AV1_CP_BT_470_M = 4,
56    AV1_CP_BT_470_B_G,
57    AV1_CP_BT_601,
58    AV1_CP_SMPTE_240,
59    AV1_CP_GENERIC_FILM,
60    AV1_CP_BT_2020,
61    AV1_CP_XYZ,
62    AV1_CP_SMPTE_431,
63    AV1_CP_SMPTE_432,
64    AV1_CP_EBU_3213 = 22
65 };
66 
67 enum av1_obu_transfer_characteristic {
68    AV1_TC_RESERVED_0 = 0,
69    AV1_TC_BT_709,
70    AV1_TC_UNSPECIFIED,
71    AV1_TC_RESERVED_3,
72    AV1_TC_BT_470_M,
73    AV1_TC_BT_470_B_G,
74    AV1_TC_BT_601,
75    AV1_TC_SMPTE_240,
76    AV1_TC_LINEAR,
77    AV1_TC_LOG_100,
78    AV1_TC_LOG_100_SQRT10,
79    AV1_TC_IEC_61966,
80    AV1_TC_BT_1361,
81    AV1_TC_SRGB,
82    AV1_TC_BT_2020_10_BIT,
83    AV1_TC_BT_2020_12_BIT,
84    AV1_TC_SMPTE_2084,
85    AV1_TC_SMPTE_428,
86    AV1_TC_HLG
87 };
88 
89 enum av1_obu_matrix_coefficient {
90    AV1_MC_IDENTITY = 0,
91    AV1_MC_BT_709,
92    AV1_MC_UNSPECIFIED,
93    AV1_MC_RESERVED_3,
94    AV1_MC_FCC,
95    AV1_MC_BT_470_B_G,
96    AV1_MC_BT_601,
97    AV1_MC_SMPTE_240,
98    AV1_MC_SMPTE_YCGCO,
99    AV1_MC_BT_2020_NCL,
100    AV1_MC_BT_2020_CL,
101    AV1_MC_SMPTE_2085,
102    AV1_MC_CHROMAT_NCL,
103    AV1_MC_CHROMAT_CL,
104    AV1_MC_ICTCP
105 };
106 
107 enum av1_obu_seg_lvl {
108    AV1_SEG_LVL_ALT_Q = 0,
109    AV1_SEG_LVL_ALT_LF_Y_V,
110    AV1_SEG_LVL_REF_FRAME = 5,
111    AV1_SEG_LVL_SKIP,
112    AV1_SEG_LVL_GLOBALMV,
113    AV1_SEG_LVL_MAX
114 };
115 
116 enum av1_obu_lrtype {
117    AV1_RESTORE_NONE = 0,
118    AV1_RESTORE_WIENER,
119    AV1_RESTORE_SGRPROJ,
120    AV1_RESTORE_SWITCHABLE
121 };
122 
123 enum av1_obu_txmode {
124    AV1_ONLY_4X4 = 0,
125    AV1_TX_MODE_LARGEST,
126    AV1_TX_MODE_SELECT
127 };
128 
129 enum av1_obu_gmtype {
130    AV1_IDENTITY = 0,
131    AV1_TRANSLATION,
132    AV1_ROTZOOM,
133    AV1_AFFINE
134 };
135 
136 enum av1_frame_refs {
137    AV1_NONE = -1,
138    AV1_INTRA_FRAME = 0,
139    AV1_LAST_FRAME,
140    AV1_LAST2_FRAME,
141    AV1_LAST3_FRAME,
142    AV1_GOLDEN_FRAME,
143    AV1_BWDREF_FRAME,
144    AV1_ALTREF2_FRAME,
145    AV1_ALTREF_FRAME
146 };
147 
148 #define AV1_SELECT_SCREEN_CONTENT_TOOLS 2
149 #define AV1_SELECT_INTEGER_MV           2
150 
151 #define AV1_PRIMARY_REF_NONE            7
152 #define AV1_REFS_PER_FRAME              7
153 #define AV1_NUM_REF_FRAMES              8
154 
155 #define AV1_MAX_TILE_WIDTH              4096
156 #define AV1_MAX_TILE_AREA               (4096 * 2304)
157 #define AV1_MAX_TILE_ROWS               64
158 #define AV1_MAX_TILE_COLS               64
159 #define AV1_MAX_NUM_TILES               AV1_MAX_TILE_ROWS * AV1_MAX_TILE_COLS
160 
161 #define AV1_MAX_SEGMENTS                8
162 #define AV1_MAX_CDEF_BITS_ARRAY         8
163 #define AV1_RESTORATION_TILESIZE        256
164 #define AV1_WARPEDMODEL_PREC_BITS       16
165 #define AV1_FG_MAX_NUM_Y_POINTS         14
166 #define AV1_FG_MAX_NUM_CBR_POINTS       10
167 #define AV1_FG_MAX_NUM_POS_LUMA         24
168 #define AV1_FG_MAX_NUM_POS_CHROMA       25
169 
170 struct av1_obu_extension_header_obu
171 {
172    unsigned temporal_id;
173    unsigned spatial_id;
174 };
175 
176 struct av1_sequence_header_obu {
177    uint8_t seq_profile;
178    bool reduced_still_picture_header;
179 
180    struct {
181       bool equal_picture_interval;
182    } timing_info;
183 
184    bool decoder_model_info_present_flag;
185    struct {
186       unsigned num_units_in_decoding_tick;
187       uint8_t buffer_removal_time_length_minus_1;
188       uint8_t frame_presentation_time_length_minus_1;
189    } decoder_model_info;
190 
191    uint8_t operating_points_cnt_minus_1;
192    uint16_t operating_point_idc[32];
193    bool decoder_model_present_for_this_op[32];
194 
195    uint8_t frame_width_bits_minus_1;
196    uint8_t frame_height_bits_minus_1;
197    unsigned max_frame_width_minus_1;
198    unsigned max_frame_height_minus_1;
199    bool frame_id_numbers_present_flag;
200    uint8_t delta_frame_id_length_minus_2;
201    uint8_t additional_frame_id_length_minus_1;
202 
203    bool use_128x128_superblock;
204    bool enable_filter_intra;
205    bool enable_intra_edge_filter;
206    bool enable_interintra_compound;
207    bool enable_masked_compound;
208    bool enable_warped_motion;
209    bool enable_dual_filter;
210    bool enable_order_hint;
211    bool enable_jnt_comp;
212    bool enable_ref_frame_mvs;
213    uint8_t seq_force_screen_content_tools;
214    uint8_t seq_force_integer_mv;
215 
216    uint8_t order_hint_bits_minus_1;
217    uint8_t OrderHintBits;
218    bool enable_superres;
219    bool enable_cdef;
220    bool enable_restoration;
221    struct {
222       uint8_t BitDepth;
223       bool mono_chrome;
224       uint8_t NumPlanes;
225       bool subsampling_x;
226       bool subsampling_y;
227       bool separate_uv_delta_q;
228    } color_config;
229    bool film_grain_params_present;
230 };
231 
232 struct ref_frame {
233    uint8_t RefFrameType;
234    unsigned RefFrameId;
235    unsigned RefUpscaledWidth;
236    unsigned RefFrameWidth;
237    unsigned RefFrameHeight;
238    unsigned RefRenderWidth;
239    unsigned RefRenderHeight;
240 };
241 
242 struct tile_info {
243    unsigned TileColsLog2;
244    unsigned TileRowsLog2;
245    int tile_col_start_sb[AV1_MAX_TILE_COLS + 1];
246    int tile_row_start_sb[AV1_MAX_TILE_ROWS + 1];
247    unsigned TileCols;
248    unsigned TileRows;
249    unsigned context_update_tile_id;
250    uint8_t TileSizeBytes;
251 };
252 
253 struct quantization_params {
254    uint8_t base_q_idx;
255    int DeltaQYDc;
256    int DeltaQUDc;
257    int DeltaQUAc;
258    int DeltaQVDc;
259    int DeltaQVAc;
260    uint8_t qm_y;
261    uint8_t qm_u;
262    uint8_t qm_v;
263 };
264 
265 struct segmentation_params {
266    bool segmentation_enabled;
267    bool segmentation_update_map;
268    bool segmentation_temporal_update;
269    bool FeatureEnabled[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX];
270    int FeatureData[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX];
271    int FeatureMask[AV1_MAX_SEGMENTS];
272 };
273 
274 struct delta_q_params {
275    bool delta_q_present;
276    uint8_t delta_q_res;
277 };
278 
279 struct delta_lf_params {
280    bool delta_lf_present;
281    uint8_t delta_lf_res;
282    bool delta_lf_multi;
283 };
284 
285 struct loop_filter_params {
286    uint8_t loop_filter_level[4];
287    uint8_t loop_filter_sharpness;
288    bool loop_filter_delta_enabled;
289    bool loop_filter_delta_update;
290    int8_t loop_filter_ref_deltas[AV1_NUM_REF_FRAMES];
291    int8_t loop_filter_mode_deltas[2];
292 };
293 
294 struct cdef_params {
295    uint8_t cdef_damping_minus_3;
296    uint8_t cdef_bits;
297    uint16_t cdef_y_strengths[AV1_MAX_CDEF_BITS_ARRAY];
298    uint16_t cdef_uv_strengths[AV1_MAX_CDEF_BITS_ARRAY];
299 };
300 
301 struct loop_restoration_params {
302    uint8_t FrameRestorationType[3];
303    uint16_t LoopRestorationSize[3];
304 };
305 
306 struct tx_mode_params {
307    uint8_t TxMode;
308 };
309 
310 enum reference_mode {
311    SINGLE_REFERENCE = 0,
312    COMPOUND_REFERENCE = 1,
313    REFERENCE_MODE_SELECT = 2,
314    REFERENCE_MODES = 3,
315 };
316 
317 struct skip_mode_params {
318    bool skip_mode_present;
319 };
320 
321 struct global_motion_params {
322    uint8_t GmType[AV1_NUM_REF_FRAMES];
323    int gm_params[AV1_NUM_REF_FRAMES][6];
324 };
325 
326 struct film_grain_params {
327    bool apply_grain;
328    uint16_t grain_seed;
329    uint8_t num_y_points;
330    uint8_t point_y_value[AV1_FG_MAX_NUM_Y_POINTS];
331    uint8_t point_y_scaling[AV1_FG_MAX_NUM_Y_POINTS];
332    bool chroma_scaling_from_luma;
333    uint8_t num_cb_points;
334    uint8_t num_cr_points;
335    uint8_t point_cb_value[AV1_FG_MAX_NUM_CBR_POINTS];
336    uint8_t point_cb_scaling[AV1_FG_MAX_NUM_CBR_POINTS];
337    uint8_t point_cr_value[AV1_FG_MAX_NUM_CBR_POINTS];
338    uint8_t point_cr_scaling[AV1_FG_MAX_NUM_CBR_POINTS];
339    uint8_t grain_scaling_minus_8;
340    uint8_t ar_coeff_lag;
341    int ar_coeffs_y[AV1_FG_MAX_NUM_POS_LUMA];
342    int ar_coeffs_cb[AV1_FG_MAX_NUM_POS_CHROMA];
343    int ar_coeffs_cr[AV1_FG_MAX_NUM_POS_CHROMA];
344    uint8_t ar_coeff_shift_minus_6;
345    int ar_coeff_shift;
346    uint8_t grain_scale_shift;
347    uint8_t cb_mult;
348    uint8_t cb_luma_mult;
349    uint16_t cb_offset;
350    uint8_t cr_mult;
351    uint8_t cr_luma_mult;
352    uint16_t cr_offset;
353    bool overlap_flag;
354    bool clip_to_restricted_range;
355 };
356 
357 struct av1_uncompressed_header_obu
358 {
359    uint8_t frame_type;
360    bool FrameIsIntra;
361    bool show_frame;
362    bool showable_frame;
363    bool show_existing_frame;
364    uint8_t frame_to_show_map_idx;
365 
366    unsigned RefOrderHint[AV1_NUM_REF_FRAMES];
367    bool error_resilient_mode;
368    bool disable_cdf_update;
369    bool allow_screen_content_tools;
370    bool force_integer_mv;
371 
372    unsigned current_frame_id;
373    bool frame_size_override_flag;
374    unsigned OrderHint;
375    uint8_t primary_ref_frame;
376 
377    uint8_t refresh_frame_flags;
378    unsigned FrameWidth;
379    unsigned FrameHeight;
380    bool use_superres;
381    unsigned SuperresDenom;
382    unsigned UpscaledWidth;
383    unsigned MiCols;
384    unsigned MiRows;
385 
386    unsigned RenderWidth;
387    unsigned RenderHeight;
388 
389    uint8_t last_frame_idx;
390    uint8_t gold_frame_idx;
391    uint8_t ref_frame_idx[AV1_REFS_PER_FRAME];
392    uint8_t usedFrame[AV1_NUM_REF_FRAMES];
393    unsigned curFrameHint;
394    unsigned shiftedOrderHints[AV1_NUM_REF_FRAMES];
395 
396    bool allow_high_precision_mv;
397    bool use_ref_frame_mvs;
398    bool allow_intrabc;
399    uint8_t interpolation_filter;
400    bool is_motion_mode_switchable;
401 
402    bool disable_frame_end_update_cdf;
403    struct tile_info ti;
404    struct quantization_params qp;
405    struct segmentation_params sp;
406    struct delta_q_params dqp;
407    struct delta_lf_params dlfp;
408 
409    bool CodedLossless;
410    bool AllLossless;
411    struct loop_filter_params lfp;
412    struct cdef_params cdefp;
413    struct loop_restoration_params lrp;
414    struct tx_mode_params tm;
415    enum reference_mode reference_select;
416    struct skip_mode_params smp;
417    bool allow_warped_motion;
418    bool reduced_tx_set;
419    struct global_motion_params gmp;
420    struct film_grain_params fgp;
421 };
422 
423 struct dec_av1_task {
424    struct list_head list;
425 
426    struct pipe_video_buffer *buf;
427    bool no_show_frame;
428    unsigned buf_ref_count;
429    struct pipe_video_buffer **buf_ref;
430    bool is_sef_task;
431 };
432 
433 struct input_buf_private {
434    struct list_head tasks;
435    struct list_head inps;
436 };
437 
438 struct dec_av1 {
439    struct av1_obu_extension_header_obu ext;
440    struct av1_sequence_header_obu seq;
441    struct av1_uncompressed_header_obu uncompressed_header;
442    struct av1_uncompressed_header_obu refs[AV1_NUM_REF_FRAMES];
443    struct ref_frame RefFrames[AV1_NUM_REF_FRAMES];
444 
445    uint8_t bs_obu_td_buf[8];
446    unsigned bs_obu_td_sz;
447    uint8_t bs_obu_seq_buf[128];
448    unsigned bs_obu_seq_sz;
449    struct pipe_video_buffer *frame_refs[AV1_NUM_REF_FRAMES];
450    struct list_head free_tasks;
451    struct list_head started_tasks;
452    struct list_head finished_tasks;
453    struct list_head decode_tasks;
454    unsigned que_num;
455    bool stacked_frame;
456    mtx_t mutex;
457 };
458 
459 #endif
460