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 "vl/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
41static 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
107void 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.qm_y | 0xf;
196   context->desc.av1.picture_parameter.qmatrix_fields.qm_u = av1->qmatrix_fields.bits.qm_u | 0xf;
197   context->desc.av1.picture_parameter.qmatrix_fields.qm_v = av1->qmatrix_fields.bits.qm_v | 0xf;
198
199   /* Segmentation Params */
200   context->desc.av1.picture_parameter.seg_info.segment_info_fields.enabled =
201      av1->seg_info.segment_info_fields.bits.enabled;
202   context->desc.av1.picture_parameter.seg_info.segment_info_fields.update_map =
203      av1->seg_info.segment_info_fields.bits.update_map;
204   context->desc.av1.picture_parameter.seg_info.segment_info_fields.temporal_update =
205      av1->seg_info.segment_info_fields.bits.temporal_update;
206   for (i = 0; i < AV1_MAX_SEGMENTS; ++i) {
207      for (j = 0; j < AV1_SEG_LVL_MAX; ++j)
208         context->desc.av1.picture_parameter.seg_info.feature_data[i][j] =
209            av1->seg_info.feature_data[i][j];
210      context->desc.av1.picture_parameter.seg_info.feature_mask[i] = av1->seg_info.feature_mask[i];
211   }
212
213   /* Delta Q Params */
214   context->desc.av1.picture_parameter.mode_control_fields.delta_q_present_flag =
215      av1->mode_control_fields.bits.delta_q_present_flag;
216   context->desc.av1.picture_parameter.mode_control_fields.log2_delta_q_res =
217      av1->mode_control_fields.bits.log2_delta_q_res;
218
219   /* Delta LF Params */
220   context->desc.av1.picture_parameter.mode_control_fields.delta_lf_present_flag =
221      av1->mode_control_fields.bits.delta_lf_present_flag;
222   context->desc.av1.picture_parameter.mode_control_fields.log2_delta_lf_res =
223      av1->mode_control_fields.bits.log2_delta_lf_res;
224   context->desc.av1.picture_parameter.mode_control_fields.delta_lf_multi =
225      av1->mode_control_fields.bits.delta_lf_multi;
226
227   context->desc.av1.picture_parameter.mode_control_fields.tx_mode =
228      av1->mode_control_fields.bits.tx_mode;
229   context->desc.av1.picture_parameter.mode_control_fields.reference_select =
230      av1->mode_control_fields.bits.reference_select;
231   context->desc.av1.picture_parameter.mode_control_fields.reduced_tx_set_used =
232      av1->mode_control_fields.bits.reduced_tx_set_used;
233   context->desc.av1.picture_parameter.mode_control_fields.skip_mode_present =
234      av1->mode_control_fields.bits.skip_mode_present;
235
236   /* Loop Filter Params */
237   context->desc.av1.picture_parameter.interp_filter = av1->interp_filter;
238   for (i = 0; i < 2; ++i)
239      context->desc.av1.picture_parameter.filter_level[i] = av1->filter_level[i];
240   context->desc.av1.picture_parameter.filter_level_u = av1->filter_level_u;
241   context->desc.av1.picture_parameter.filter_level_v = av1->filter_level_v;
242   context->desc.av1.picture_parameter.loop_filter_info_fields.sharpness_level =
243      av1->loop_filter_info_fields.bits.sharpness_level;
244   context->desc.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_enabled =
245      av1->loop_filter_info_fields.bits.mode_ref_delta_enabled;
246   context->desc.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_update =
247      av1->loop_filter_info_fields.bits.mode_ref_delta_update;
248   for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)
249      context->desc.av1.picture_parameter.ref_deltas[i] = av1->ref_deltas[i];
250   for (i = 0; i < 2; ++i)
251      context->desc.av1.picture_parameter.mode_deltas[i] = av1->mode_deltas[i];
252
253   /* CDEF Params */
254   context->desc.av1.picture_parameter.cdef_damping_minus_3 = av1->cdef_damping_minus_3;
255   context->desc.av1.picture_parameter.cdef_bits = av1->cdef_bits;
256   for (i = 0; i < AV1_MAX_CDEF_BITS_ARRAY; ++i) {
257      context->desc.av1.picture_parameter.cdef_y_strengths[i] = av1->cdef_y_strengths[i];
258      context->desc.av1.picture_parameter.cdef_uv_strengths[i] = av1->cdef_uv_strengths[i];
259   }
260
261   /* Loop Restoration Params */
262   context->desc.av1.picture_parameter.loop_restoration_fields.yframe_restoration_type =
263      av1->loop_restoration_fields.bits.yframe_restoration_type;
264   context->desc.av1.picture_parameter.loop_restoration_fields.cbframe_restoration_type =
265      av1->loop_restoration_fields.bits.cbframe_restoration_type;
266   context->desc.av1.picture_parameter.loop_restoration_fields.crframe_restoration_type =
267      av1->loop_restoration_fields.bits.crframe_restoration_type;
268   if (!av1->loop_restoration_fields.bits.lr_unit_shift) {
269      context->desc.av1.picture_parameter.lr_unit_size[0] =
270         256 >> (2 - av1->loop_restoration_fields.bits.lr_unit_shift);
271      context->desc.av1.picture_parameter.lr_unit_size[1] =
272         context->desc.av1.picture_parameter.lr_unit_size[2] =
273         (context->desc.av1.picture_parameter.lr_unit_size[0] >>
274          av1->loop_restoration_fields.bits.lr_uv_shift);
275   } else {
276      for (i = 0; i < 3; ++i)
277         context->desc.av1.picture_parameter.lr_unit_size[i] = (1 << 8);
278   }
279
280   /* Global Motion Params */
281   for (i = 0; i < ARRAY_SIZE(av1->wm); ++i) {
282      context->desc.av1.picture_parameter.wm[i].wmtype = av1->wm[i].wmtype;
283      for (j = 0; j < ARRAY_SIZE(av1->wm[0].wmmat); ++j)
284         context->desc.av1.picture_parameter.wm[i].wmmat[j] = av1->wm[i].wmmat[j];
285   }
286
287   /* Film Grain Params */
288   context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.apply_grain =
289      av1->film_grain_info.film_grain_info_fields.bits.apply_grain;
290   context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.chroma_scaling_from_luma =
291      av1->film_grain_info.film_grain_info_fields.bits.chroma_scaling_from_luma;
292   context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.grain_scaling_minus_8 =
293      av1->film_grain_info.film_grain_info_fields.bits.grain_scaling_minus_8;
294   context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.ar_coeff_lag =
295      av1->film_grain_info.film_grain_info_fields.bits.ar_coeff_lag;
296   context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.ar_coeff_shift_minus_6 =
297      av1->film_grain_info.film_grain_info_fields.bits.ar_coeff_shift_minus_6;
298   context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.grain_scale_shift =
299      av1->film_grain_info.film_grain_info_fields.bits.grain_scale_shift;
300   context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.overlap_flag =
301      av1->film_grain_info.film_grain_info_fields.bits.overlap_flag;
302   context->desc.av1.picture_parameter.film_grain_info.film_grain_info_fields.clip_to_restricted_range =
303      av1->film_grain_info.film_grain_info_fields.bits.clip_to_restricted_range;
304
305   context->desc.av1.picture_parameter.film_grain_info.grain_seed = av1->film_grain_info.grain_seed;
306   context->desc.av1.picture_parameter.film_grain_info.num_y_points = av1->film_grain_info.num_y_points;
307   for (i = 0; i < AV1_FG_MAX_NUM_Y_POINTS; ++i) {
308      context->desc.av1.picture_parameter.film_grain_info.point_y_value[i] =
309         av1->film_grain_info.point_y_value[i];
310      context->desc.av1.picture_parameter.film_grain_info.point_y_scaling[i] =
311         av1->film_grain_info.point_y_scaling[i];
312   }
313   context->desc.av1.picture_parameter.film_grain_info.num_cb_points = av1->film_grain_info.num_cb_points;
314   context->desc.av1.picture_parameter.film_grain_info.num_cr_points = av1->film_grain_info.num_cr_points;
315   for (i = 0; i < AV1_FG_MAX_NUM_CBR_POINTS; ++i) {
316      context->desc.av1.picture_parameter.film_grain_info.point_cb_value[i] =
317         av1->film_grain_info.point_cb_value[i];
318      context->desc.av1.picture_parameter.film_grain_info.point_cb_scaling[i] =
319         av1->film_grain_info.point_cb_scaling[i];
320      context->desc.av1.picture_parameter.film_grain_info.point_cr_value[i] =
321         av1->film_grain_info.point_cr_value[i];
322      context->desc.av1.picture_parameter.film_grain_info.point_cr_scaling[i] =
323         av1->film_grain_info.point_cr_scaling[i];
324   }
325
326   for (i = 0; i < AV1_FG_MAX_NUM_POS_LUMA; ++i)
327      context->desc.av1.picture_parameter.film_grain_info.ar_coeffs_y[i] =
328         av1->film_grain_info.ar_coeffs_y[i];
329   for (i = 0; i < AV1_FG_MAX_NUM_POS_CHROMA; ++i) {
330      context->desc.av1.picture_parameter.film_grain_info.ar_coeffs_cb[i] =
331         av1->film_grain_info.ar_coeffs_cb[i];
332      context->desc.av1.picture_parameter.film_grain_info.ar_coeffs_cr[i] =
333         av1->film_grain_info.ar_coeffs_cr[i];
334   }
335   context->desc.av1.picture_parameter.film_grain_info.cb_mult = av1->film_grain_info.cb_mult;
336   context->desc.av1.picture_parameter.film_grain_info.cb_luma_mult = av1->film_grain_info.cb_luma_mult;
337   context->desc.av1.picture_parameter.film_grain_info.cb_offset = av1->film_grain_info.cb_offset;
338   context->desc.av1.picture_parameter.film_grain_info.cr_mult = av1->film_grain_info.cr_mult;
339   context->desc.av1.picture_parameter.film_grain_info.cr_luma_mult = av1->film_grain_info.cr_luma_mult;
340   context->desc.av1.picture_parameter.film_grain_info.cr_offset = av1->film_grain_info.cr_offset;
341
342   for (i = 0 ; i < AV1_NUM_REF_FRAMES; ++i) {
343      if (av1->pic_info_fields.bits.frame_type == 0)
344         context->desc.av1.ref[i] = NULL;
345      else
346         vlVaGetReferenceFrame(drv, av1->ref_frame_map[i], &context->desc.av1.ref[i]);
347   }
348}
349
350void vlVaHandleSliceParameterBufferAV1(vlVaContext *context, vlVaBuffer *buf, unsigned int num)
351{
352   VASliceParameterBufferAV1 *av1 = buf->data;
353
354   context->desc.av1.slice_parameter.slice_data_size[num >> 1] = av1->slice_data_size;
355   context->desc.av1.slice_parameter.slice_data_offset[num >> 1] = av1->slice_data_offset;
356}
357