1 /**************************************************************************
2 *
3 * Copyright 2021 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 #include "util/vl_vlc.h"
29 #include "va_private.h"
30
31 #define AV1_REFS_PER_FRAME 7
32 #define AV1_NUM_REF_FRAMES 8
33 #define AV1_MAX_SEGMENTS 8
34 #define AV1_SEG_LVL_MAX 8
35 #define AV1_MAX_CDEF_BITS_ARRAY 8
36 #define AV1_FG_MAX_NUM_Y_POINTS 14
37 #define AV1_FG_MAX_NUM_CBR_POINTS 10
38 #define AV1_FG_MAX_NUM_POS_LUMA 24
39 #define AV1_FG_MAX_NUM_POS_CHROMA 25
40
tile_info(vlVaContext * context,VADecPictureParameterBufferAV1 * av1)41 static void tile_info(vlVaContext *context, VADecPictureParameterBufferAV1 *av1)
42 {
43 unsigned sbCols;
44 unsigned sbRows;
45 int width_sb;
46 int height_sb;
47 unsigned startSb, i;
48 unsigned MiCols = 2 * ((av1->frame_width_minus1 + 8) >> 3);
49 unsigned MiRows = 2 * ((av1->frame_height_minus1 + 8) >> 3);
50
51 unsigned TileColsLog2 = util_logbase2_ceil(av1->tile_cols);
52 unsigned TileRowsLog2 = util_logbase2_ceil(av1->tile_rows);
53
54 sbCols = (av1->seq_info_fields.fields.use_128x128_superblock) ?
55 ((MiCols + 31) >> 5) : ((MiCols + 15) >> 4);
56 sbRows = (av1->seq_info_fields.fields.use_128x128_superblock) ?
57 ((MiRows + 31) >> 5) : ((MiRows + 15) >> 4);
58
59 width_sb = sbCols;
60 height_sb = sbRows;
61
62 if (av1->pic_info_fields.bits.uniform_tile_spacing_flag) {
63 unsigned tileWidthSb, tileHeightSb;
64
65 tileWidthSb = (sbCols + (1 << TileColsLog2) - 1) >> TileColsLog2;
66 i = 0;
67 for (startSb = 0; startSb < sbCols; startSb += tileWidthSb) {
68 context->desc.av1.picture_parameter.tile_col_start_sb[i] = startSb;
69 i++;
70 }
71 context->desc.av1.picture_parameter.tile_col_start_sb[i] = sbCols;
72
73 tileHeightSb = (sbRows + (1 << TileRowsLog2) - 1) >> TileRowsLog2;
74 i = 0;
75 for (startSb = 0; startSb < sbRows; startSb += tileHeightSb) {
76 context->desc.av1.picture_parameter.tile_row_start_sb[i] = startSb;
77 i++;
78 }
79 context->desc.av1.picture_parameter.tile_row_start_sb[i] = sbRows;
80 } else {
81 unsigned widestTileSb = 0;
82
83 startSb = 0;
84 for (i = 0; startSb < sbCols; ++i) {
85 unsigned sizeSb;
86
87 context->desc.av1.picture_parameter.tile_col_start_sb[i] = startSb;
88 sizeSb = (av1->width_in_sbs_minus_1)[i] + 1;
89 widestTileSb = MAX2(sizeSb, widestTileSb);
90 startSb += sizeSb;
91 width_sb -= sizeSb;
92 }
93 context->desc.av1.picture_parameter.tile_col_start_sb[i] = startSb + width_sb;
94
95 startSb = 0;
96 for (i = 0; startSb < sbRows; ++i) {
97 unsigned height_in_sbs_minus_1 = (av1->height_in_sbs_minus_1)[i];
98
99 context->desc.av1.picture_parameter.tile_row_start_sb[i] = startSb;
100 startSb += height_in_sbs_minus_1 + 1;
101 height_sb -= height_in_sbs_minus_1 + 1;
102 }
103 context->desc.av1.picture_parameter.tile_row_start_sb[i] = startSb + height_sb;
104 }
105 }
106
vlVaHandlePictureParameterBufferAV1(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)107 void vlVaHandlePictureParameterBufferAV1(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
108 {
109 VADecPictureParameterBufferAV1 *av1 = buf->data;
110 int i, j;
111
112 assert(buf->size >= sizeof(VADecPictureParameterBufferAV1) && buf->num_elements == 1);
113
114 context->desc.av1.picture_parameter.profile = av1->profile;
115 context->desc.av1.picture_parameter.seq_info_fields.use_128x128_superblock =
116 av1->seq_info_fields.fields.use_128x128_superblock;
117 context->desc.av1.picture_parameter.seq_info_fields.enable_filter_intra =
118 av1->seq_info_fields.fields.enable_filter_intra;
119 context->desc.av1.picture_parameter.seq_info_fields.enable_intra_edge_filter =
120 av1->seq_info_fields.fields.enable_intra_edge_filter;
121 context->desc.av1.picture_parameter.order_hint_bits_minus_1 = av1->order_hint_bits_minus_1;
122 context->desc.av1.picture_parameter.max_width = av1->frame_width_minus1 + 1;
123 context->desc.av1.picture_parameter.max_height = av1->frame_height_minus1 + 1;
124 context->desc.av1.picture_parameter.seq_info_fields.enable_interintra_compound =
125 av1->seq_info_fields.fields.enable_interintra_compound;
126 context->desc.av1.picture_parameter.seq_info_fields.enable_masked_compound =
127 av1->seq_info_fields.fields.enable_masked_compound;
128 context->desc.av1.picture_parameter.seq_info_fields.enable_dual_filter =
129 av1->seq_info_fields.fields.enable_dual_filter;
130 context->desc.av1.picture_parameter.seq_info_fields.enable_order_hint =
131 av1->seq_info_fields.fields.enable_order_hint;
132 context->desc.av1.picture_parameter.seq_info_fields.enable_jnt_comp =
133 av1->seq_info_fields.fields.enable_jnt_comp;
134
135 context->desc.av1.picture_parameter.seq_info_fields.ref_frame_mvs =
136 av1->seq_info_fields.fields.enable_order_hint;
137
138 context->desc.av1.picture_parameter.bit_depth_idx = av1->bit_depth_idx;
139 context->desc.av1.picture_parameter.seq_info_fields.mono_chrome =
140 av1->seq_info_fields.fields.mono_chrome;
141
142 context->desc.av1.picture_parameter.pic_info_fields.frame_type =
143 av1->pic_info_fields.bits.frame_type;
144 context->desc.av1.picture_parameter.pic_info_fields.show_frame =
145 av1->pic_info_fields.bits.show_frame;
146 context->desc.av1.picture_parameter.pic_info_fields.error_resilient_mode =
147 av1->pic_info_fields.bits.error_resilient_mode;
148 context->desc.av1.picture_parameter.pic_info_fields.disable_cdf_update =
149 av1->pic_info_fields.bits.disable_cdf_update;
150 context->desc.av1.picture_parameter.pic_info_fields.allow_screen_content_tools =
151 av1->pic_info_fields.bits.allow_screen_content_tools;
152 context->desc.av1.picture_parameter.pic_info_fields.force_integer_mv =
153 av1->pic_info_fields.bits.force_integer_mv;
154 context->desc.av1.picture_parameter.pic_info_fields.allow_intrabc =
155 av1->pic_info_fields.bits.allow_intrabc;
156 context->desc.av1.picture_parameter.pic_info_fields.use_superres =
157 av1->pic_info_fields.bits.use_superres;
158 context->desc.av1.picture_parameter.pic_info_fields.is_motion_mode_switchable =
159 av1->pic_info_fields.bits.is_motion_mode_switchable;
160 context->desc.av1.picture_parameter.pic_info_fields.allow_high_precision_mv =
161 av1->pic_info_fields.bits.allow_high_precision_mv;
162 context->desc.av1.picture_parameter.pic_info_fields.use_ref_frame_mvs =
163 av1->pic_info_fields.bits.use_ref_frame_mvs;
164 context->desc.av1.picture_parameter.pic_info_fields.disable_frame_end_update_cdf =
165 av1->pic_info_fields.bits.disable_frame_end_update_cdf;
166 context->desc.av1.picture_parameter.pic_info_fields.allow_warped_motion =
167 av1->pic_info_fields.bits.allow_warped_motion;
168
169 context->desc.av1.picture_parameter.current_frame_id = av1->current_frame;
170 context->desc.av1.picture_parameter.order_hint = av1->order_hint;
171 context->desc.av1.picture_parameter.primary_ref_frame = av1->primary_ref_frame;
172 context->desc.av1.picture_parameter.frame_width = av1->frame_width_minus1 + 1;
173 context->desc.av1.picture_parameter.frame_height = av1->frame_height_minus1 + 1;
174
175 context->desc.av1.picture_parameter.superres_scale_denominator =
176 av1->superres_scale_denominator;
177
178 for (i = 0; i < AV1_REFS_PER_FRAME; ++i)
179 context->desc.av1.picture_parameter.ref_frame_idx[i] = av1->ref_frame_idx[i];
180 context->desc.av1.picture_parameter.refresh_frame_flags = 1;
181
182 /* Tile Info */
183 context->desc.av1.picture_parameter.tile_cols = av1->tile_cols;
184 context->desc.av1.picture_parameter.tile_rows = av1->tile_rows;
185 context->desc.av1.picture_parameter.context_update_tile_id = av1->context_update_tile_id;
186 tile_info(context, av1);
187
188 /* Quantization Params */
189 context->desc.av1.picture_parameter.base_qindex = av1->base_qindex;
190 context->desc.av1.picture_parameter.y_dc_delta_q = av1->y_dc_delta_q;
191 context->desc.av1.picture_parameter.u_dc_delta_q = av1->u_dc_delta_q;
192 context->desc.av1.picture_parameter.u_ac_delta_q = av1->u_ac_delta_q;
193 context->desc.av1.picture_parameter.v_dc_delta_q = av1->v_dc_delta_q;
194 context->desc.av1.picture_parameter.v_ac_delta_q = av1->v_ac_delta_q;
195 context->desc.av1.picture_parameter.qmatrix_fields.qm_y = av1->qmatrix_fields.bits.using_qmatrix
196 ? av1->qmatrix_fields.bits.qm_y : 0xf;
197 context->desc.av1.picture_parameter.qmatrix_fields.qm_u = av1->qmatrix_fields.bits.using_qmatrix
198 ? av1->qmatrix_fields.bits.qm_u : 0xf;
199 context->desc.av1.picture_parameter.qmatrix_fields.qm_v = av1->qmatrix_fields.bits.using_qmatrix
200 ? av1->qmatrix_fields.bits.qm_v : 0xf;
201
202 /* Segmentation Params */
203 context->desc.av1.picture_parameter.seg_info.segment_info_fields.enabled =
204 av1->seg_info.segment_info_fields.bits.enabled;
205 context->desc.av1.picture_parameter.seg_info.segment_info_fields.update_map =
206 av1->seg_info.segment_info_fields.bits.update_map;
207 context->desc.av1.picture_parameter.seg_info.segment_info_fields.temporal_update =
208 av1->seg_info.segment_info_fields.bits.temporal_update;
209 for (i = 0; i < AV1_MAX_SEGMENTS; ++i) {
210 for (j = 0; j < AV1_SEG_LVL_MAX; ++j)
211 context->desc.av1.picture_parameter.seg_info.feature_data[i][j] =
212 av1->seg_info.feature_data[i][j];
213 context->desc.av1.picture_parameter.seg_info.feature_mask[i] = av1->seg_info.feature_mask[i];
214 }
215
216 /* Delta Q Params */
217 context->desc.av1.picture_parameter.mode_control_fields.delta_q_present_flag =
218 av1->mode_control_fields.bits.delta_q_present_flag;
219 context->desc.av1.picture_parameter.mode_control_fields.log2_delta_q_res =
220 av1->mode_control_fields.bits.log2_delta_q_res;
221
222 /* Delta LF Params */
223 context->desc.av1.picture_parameter.mode_control_fields.delta_lf_present_flag =
224 av1->mode_control_fields.bits.delta_lf_present_flag;
225 context->desc.av1.picture_parameter.mode_control_fields.log2_delta_lf_res =
226 av1->mode_control_fields.bits.log2_delta_lf_res;
227 context->desc.av1.picture_parameter.mode_control_fields.delta_lf_multi =
228 av1->mode_control_fields.bits.delta_lf_multi;
229
230 context->desc.av1.picture_parameter.mode_control_fields.tx_mode =
231 av1->mode_control_fields.bits.tx_mode;
232 context->desc.av1.picture_parameter.mode_control_fields.reference_select =
233 av1->mode_control_fields.bits.reference_select;
234 context->desc.av1.picture_parameter.mode_control_fields.reduced_tx_set_used =
235 av1->mode_control_fields.bits.reduced_tx_set_used;
236 context->desc.av1.picture_parameter.mode_control_fields.skip_mode_present =
237 av1->mode_control_fields.bits.skip_mode_present;
238
239 /* Loop Filter Params */
240 context->desc.av1.picture_parameter.interp_filter = av1->interp_filter;
241 for (i = 0; i < 2; ++i)
242 context->desc.av1.picture_parameter.filter_level[i] = av1->filter_level[i];
243 context->desc.av1.picture_parameter.filter_level_u = av1->filter_level_u;
244 context->desc.av1.picture_parameter.filter_level_v = av1->filter_level_v;
245 context->desc.av1.picture_parameter.loop_filter_info_fields.sharpness_level =
246 av1->loop_filter_info_fields.bits.sharpness_level;
247 context->desc.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_enabled =
248 av1->loop_filter_info_fields.bits.mode_ref_delta_enabled;
249 context->desc.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_update =
250 av1->loop_filter_info_fields.bits.mode_ref_delta_update;
251 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)
252 context->desc.av1.picture_parameter.ref_deltas[i] = av1->ref_deltas[i];
253 for (i = 0; i < 2; ++i)
254 context->desc.av1.picture_parameter.mode_deltas[i] = av1->mode_deltas[i];
255
256 /* CDEF Params */
257 context->desc.av1.picture_parameter.cdef_damping_minus_3 = av1->cdef_damping_minus_3;
258 context->desc.av1.picture_parameter.cdef_bits = av1->cdef_bits;
259 for (i = 0; i < AV1_MAX_CDEF_BITS_ARRAY; ++i) {
260 context->desc.av1.picture_parameter.cdef_y_strengths[i] = av1->cdef_y_strengths[i];
261 context->desc.av1.picture_parameter.cdef_uv_strengths[i] = av1->cdef_uv_strengths[i];
262 }
263
264 /* Loop Restoration Params */
265 context->desc.av1.picture_parameter.loop_restoration_fields.yframe_restoration_type =
266 av1->loop_restoration_fields.bits.yframe_restoration_type;
267 context->desc.av1.picture_parameter.loop_restoration_fields.cbframe_restoration_type =
268 av1->loop_restoration_fields.bits.cbframe_restoration_type;
269 context->desc.av1.picture_parameter.loop_restoration_fields.crframe_restoration_type =
270 av1->loop_restoration_fields.bits.crframe_restoration_type;
271 if (!av1->loop_restoration_fields.bits.lr_unit_shift) {
272 context->desc.av1.picture_parameter.lr_unit_size[0] =
273 256 >> (2 - av1->loop_restoration_fields.bits.lr_unit_shift);
274 context->desc.av1.picture_parameter.lr_unit_size[1] =
275 context->desc.av1.picture_parameter.lr_unit_size[2] =
276 (context->desc.av1.picture_parameter.lr_unit_size[0] >>
277 av1->loop_restoration_fields.bits.lr_uv_shift);
278 } else {
279 for (i = 0; i < 3; ++i)
280 context->desc.av1.picture_parameter.lr_unit_size[i] = (1 << 8);
281 }
282
283 /* Global Motion Params */
284 for (i = 0; i < ARRAY_SIZE(av1->wm); ++i) {
285 context->desc.av1.picture_parameter.wm[i].wmtype = av1->wm[i].wmtype;
286 for (j = 0; j < ARRAY_SIZE(av1->wm[0].wmmat); ++j)
287 context->desc.av1.picture_parameter.wm[i].wmmat[j] = av1->wm[i].wmmat[j];
288 }
289
290 /* Film Grain Params */
291 context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.apply_grain =
292 av1->film_grain_info.film_grain_info_fields.bits.apply_grain;
293 context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.chroma_scaling_from_luma =
294 av1->film_grain_info.film_grain_info_fields.bits.chroma_scaling_from_luma;
295 context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.grain_scaling_minus_8 =
296 av1->film_grain_info.film_grain_info_fields.bits.grain_scaling_minus_8;
297 context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.ar_coeff_lag =
298 av1->film_grain_info.film_grain_info_fields.bits.ar_coeff_lag;
299 context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.ar_coeff_shift_minus_6 =
300 av1->film_grain_info.film_grain_info_fields.bits.ar_coeff_shift_minus_6;
301 context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.grain_scale_shift =
302 av1->film_grain_info.film_grain_info_fields.bits.grain_scale_shift;
303 context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.overlap_flag =
304 av1->film_grain_info.film_grain_info_fields.bits.overlap_flag;
305 context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.clip_to_restricted_range =
306 av1->film_grain_info.film_grain_info_fields.bits.clip_to_restricted_range;
307
308 context->desc.av1.picture_parameter.film_grain_info.grain_seed = av1->film_grain_info.grain_seed;
309 context->desc.av1.picture_parameter.film_grain_info.num_y_points = av1->film_grain_info.num_y_points;
310 for (i = 0; i < AV1_FG_MAX_NUM_Y_POINTS; ++i) {
311 context->desc.av1.picture_parameter.film_grain_info.point_y_value[i] =
312 av1->film_grain_info.point_y_value[i];
313 context->desc.av1.picture_parameter.film_grain_info.point_y_scaling[i] =
314 av1->film_grain_info.point_y_scaling[i];
315 }
316 context->desc.av1.picture_parameter.film_grain_info.num_cb_points = av1->film_grain_info.num_cb_points;
317 context->desc.av1.picture_parameter.film_grain_info.num_cr_points = av1->film_grain_info.num_cr_points;
318 for (i = 0; i < AV1_FG_MAX_NUM_CBR_POINTS; ++i) {
319 context->desc.av1.picture_parameter.film_grain_info.point_cb_value[i] =
320 av1->film_grain_info.point_cb_value[i];
321 context->desc.av1.picture_parameter.film_grain_info.point_cb_scaling[i] =
322 av1->film_grain_info.point_cb_scaling[i];
323 context->desc.av1.picture_parameter.film_grain_info.point_cr_value[i] =
324 av1->film_grain_info.point_cr_value[i];
325 context->desc.av1.picture_parameter.film_grain_info.point_cr_scaling[i] =
326 av1->film_grain_info.point_cr_scaling[i];
327 }
328
329 for (i = 0; i < AV1_FG_MAX_NUM_POS_LUMA; ++i)
330 context->desc.av1.picture_parameter.film_grain_info.ar_coeffs_y[i] =
331 av1->film_grain_info.ar_coeffs_y[i];
332 for (i = 0; i < AV1_FG_MAX_NUM_POS_CHROMA; ++i) {
333 context->desc.av1.picture_parameter.film_grain_info.ar_coeffs_cb[i] =
334 av1->film_grain_info.ar_coeffs_cb[i];
335 context->desc.av1.picture_parameter.film_grain_info.ar_coeffs_cr[i] =
336 av1->film_grain_info.ar_coeffs_cr[i];
337 }
338 context->desc.av1.picture_parameter.film_grain_info.cb_mult = av1->film_grain_info.cb_mult;
339 context->desc.av1.picture_parameter.film_grain_info.cb_luma_mult = av1->film_grain_info.cb_luma_mult;
340 context->desc.av1.picture_parameter.film_grain_info.cb_offset = av1->film_grain_info.cb_offset;
341 context->desc.av1.picture_parameter.film_grain_info.cr_mult = av1->film_grain_info.cr_mult;
342 context->desc.av1.picture_parameter.film_grain_info.cr_luma_mult = av1->film_grain_info.cr_luma_mult;
343 context->desc.av1.picture_parameter.film_grain_info.cr_offset = av1->film_grain_info.cr_offset;
344
345 for (i = 0 ; i < AV1_NUM_REF_FRAMES; ++i) {
346 if (av1->pic_info_fields.bits.frame_type == 0)
347 context->desc.av1.ref[i] = NULL;
348 else
349 vlVaGetReferenceFrame(drv, av1->ref_frame_map[i], &context->desc.av1.ref[i]);
350 }
351 }
352
vlVaHandleSliceParameterBufferAV1(vlVaContext * context,vlVaBuffer * buf,unsigned int num)353 void vlVaHandleSliceParameterBufferAV1(vlVaContext *context, vlVaBuffer *buf, unsigned int num)
354 {
355 VASliceParameterBufferAV1 *av1 = buf->data;
356
357 context->desc.av1.slice_parameter.slice_data_size[num] = av1->slice_data_size;
358 context->desc.av1.slice_parameter.slice_data_offset[num] = av1->slice_data_offset;
359 }
360