1 /*
2 * AV1 HW decode acceleration through VA API
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
28
33
36
37 /**
38 * For film grain case, VAAPI generate 2 output for each frame,
39 * current_frame will not apply film grain, and will be used for
40 * references for next frames. Maintain the reference list without
41 * applying film grain here. And current_display_picture will be
42 * used to apply film grain and push to downstream.
43 */
46
50
52 {
53 if (vf)
55 else
56 return VA_INVALID_SURFACE;
57 }
58
60 {
64
69 else {
71 "Couldn't get bit depth from profile:%d.\n", seq->
seq_profile);
72 return -1;
73 }
75 }
76
78 {
80
84
87 if (!
ctx->ref_tab[
i].frame)
89 ctx->ref_tab[
i].valid = 0;
90 }
91
93 }
94
96 {
98
100
103
105
107 }
108
109
113 {
120 VADecPictureParameterBufferAV1 pic_param;
121 int8_t bit_depth_idx;
122 int err = 0;
125 uint8_t segmentation_feature_signed[
AV1_SEG_LVL_MAX] = {1, 1, 1, 1, 1, 0, 0, 0};
128
130 if (bit_depth_idx < 0)
132
133 if (apply_grain) {
134 if (
ctx->tmp_frame->buf[0])
137 if (err < 0)
140 } else {
142 }
143
144 memset(&pic_param, 0, sizeof(VADecPictureParameterBufferAV1));
145 pic_param = (VADecPictureParameterBufferAV1) {
146 .profile = seq->seq_profile,
147 .order_hint_bits_minus_1 = seq->order_hint_bits_minus_1,
148 .bit_depth_idx = bit_depth_idx,
149 .matrix_coefficients = seq->color_config.matrix_coefficients,
150 .current_frame = pic->output_surface,
152 .frame_width_minus1 =
frame_header->frame_width_minus_1,
153 .frame_height_minus1 =
frame_header->frame_height_minus_1,
158 .context_update_tile_id =
frame_header->context_update_tile_id,
159 .superres_scale_denominator =
frame_header->use_superres ?
173 .cdef_damping_minus_3 =
frame_header->cdef_damping_minus_3,
175 .seq_info_fields.fields = {
176 .still_picture = seq->still_picture,
177 .use_128x128_superblock = seq->use_128x128_superblock,
178 .enable_filter_intra = seq->enable_filter_intra,
179 .enable_intra_edge_filter = seq->enable_intra_edge_filter,
180 .enable_interintra_compound = seq->enable_interintra_compound,
181 .enable_masked_compound = seq->enable_masked_compound,
182 .enable_dual_filter = seq->enable_dual_filter,
183 .enable_order_hint = seq->enable_order_hint,
184 .enable_jnt_comp = seq->enable_jnt_comp,
185 .enable_cdef = seq->enable_cdef,
186 .mono_chrome = seq->color_config.mono_chrome,
187 .color_range = seq->color_config.color_range,
188 .subsampling_x = seq->color_config.subsampling_x,
189 .subsampling_y = seq->color_config.subsampling_y,
190 .chroma_sample_position = seq->color_config.chroma_sample_position,
191 .film_grain_params_present = seq->film_grain_params_present &&
193 },
194 .seg_info.segment_info_fields.bits = {
197 .temporal_update =
frame_header->segmentation_temporal_update,
199 },
200 .film_grain_info = {
201 .film_grain_info_fields.bits = {
202 .apply_grain = apply_grain,
203 .chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma,
204 .grain_scaling_minus_8 = film_grain->grain_scaling_minus_8,
205 .ar_coeff_lag = film_grain->ar_coeff_lag,
206 .ar_coeff_shift_minus_6 = film_grain->ar_coeff_shift_minus_6,
207 .grain_scale_shift = film_grain->grain_scale_shift,
208 .overlap_flag = film_grain->overlap_flag,
209 .clip_to_restricted_range = film_grain->clip_to_restricted_range,
210 },
211 .grain_seed = film_grain->grain_seed,
212 .num_y_points = film_grain->num_y_points,
213 .num_cb_points = film_grain->num_cb_points,
214 .num_cr_points = film_grain->num_cr_points,
215 .cb_mult = film_grain->cb_mult,
216 .cb_luma_mult = film_grain->cb_luma_mult,
217 .cb_offset = film_grain->cb_offset,
218 .cr_mult = film_grain->cr_mult,
219 .cr_luma_mult = film_grain->cr_luma_mult,
220 .cr_offset = film_grain->cr_offset,
221 },
222 .pic_info_fields.bits = {
226 .error_resilient_mode =
frame_header->error_resilient_mode,
228 .allow_screen_content_tools =
frame_header->allow_screen_content_tools,
229 .force_integer_mv =
s->cur_frame.force_integer_mv,
232 .allow_high_precision_mv =
frame_header->allow_high_precision_mv,
233 .is_motion_mode_switchable =
frame_header->is_motion_mode_switchable,
235 .disable_frame_end_update_cdf =
frame_header->disable_frame_end_update_cdf,
236 .uniform_tile_spacing_flag =
frame_header->uniform_tile_spacing_flag,
237 .allow_warped_motion =
frame_header->allow_warped_motion,
238 },
239 .loop_filter_info_fields.bits = {
241 .mode_ref_delta_enabled =
frame_header->loop_filter_delta_enabled,
242 .mode_ref_delta_update =
frame_header->loop_filter_delta_update,
243 },
244 .mode_control_fields.bits = {
247 .delta_lf_present_flag =
frame_header->delta_lf_present,
254 },
255 .loop_restoration_fields.bits = {
256 .yframe_restoration_type = remap_lr_type[
frame_header->lr_type[0]],
257 .cbframe_restoration_type = remap_lr_type[
frame_header->lr_type[1]],
258 .crframe_restoration_type = remap_lr_type[
frame_header->lr_type[2]],
261 },
262 .qmatrix_fields.bits = {
267 }
268 };
269
272 pic_param.ref_frame_map[
i] = VA_INVALID_ID;
273 else
274 pic_param.ref_frame_map[
i] =
ctx->ref_tab[
i].valid ?
277 }
280 }
282 pic_param.ref_deltas[
i] =
frame_header->loop_filter_ref_deltas[
i];
283 }
284 for (
int i = 0;
i < 2;
i++) {
285 pic_param.mode_deltas[
i] =
frame_header->loop_filter_mode_deltas[
i];
286 }
288 pic_param.cdef_y_strengths[
i] =
291 pic_param.cdef_uv_strengths[
i] =
294 }
296 pic_param.width_in_sbs_minus_1[
i] =
298 }
300 pic_param.height_in_sbs_minus_1[
i] =
302 }
304 pic_param.wm[
i - 1].invalid =
s->cur_frame.gm_invalid[
i];
305 pic_param.wm[
i - 1].wmtype =
s->cur_frame.gm_type[
i];
306 for (int j = 0; j < 6; j++)
307 pic_param.wm[
i - 1].wmmat[j] =
s->cur_frame.gm_params[
i][j];
308 }
311 pic_param.seg_info.feature_mask[
i] |= (
frame_header->feature_enabled[
i][j] << j);
312 if (segmentation_feature_signed[j])
314 -segmentation_feature_max[j], segmentation_feature_max[j]);
315 else
317 0, segmentation_feature_max[j]);
318 }
319 }
320 if (apply_grain) {
321 for (
int i = 0;
i < film_grain->num_y_points;
i++) {
322 pic_param.film_grain_info.point_y_value[
i] =
323 film_grain->point_y_value[
i];
324 pic_param.film_grain_info.point_y_scaling[
i] =
325 film_grain->point_y_scaling[
i];
326 }
327 for (
int i = 0;
i < film_grain->num_cb_points;
i++) {
328 pic_param.film_grain_info.point_cb_value[
i] =
329 film_grain->point_cb_value[
i];
330 pic_param.film_grain_info.point_cb_scaling[
i] =
331 film_grain->point_cb_scaling[
i];
332 }
333 for (
int i = 0;
i < film_grain->num_cr_points;
i++) {
334 pic_param.film_grain_info.point_cr_value[
i] =
335 film_grain->point_cr_value[
i];
336 pic_param.film_grain_info.point_cr_scaling[
i] =
337 film_grain->point_cr_scaling[
i];
338 }
339 for (
int i = 0;
i < 24;
i++) {
340 pic_param.film_grain_info.ar_coeffs_y[
i] =
341 film_grain->ar_coeffs_y_plus_128[
i] - 128;
342 }
343 for (
int i = 0;
i < 25;
i++) {
344 pic_param.film_grain_info.ar_coeffs_cb[
i] =
345 film_grain->ar_coeffs_cb_plus_128[
i] - 128;
346 pic_param.film_grain_info.ar_coeffs_cr[
i] =
347 film_grain->ar_coeffs_cr_plus_128[
i] - 128;
348 }
349 }
351 VAPictureParameterBufferType,
352 &pic_param, sizeof(pic_param));
353 if (err < 0)
355
356 return 0;
357
360 return err;
361 }
362
364 {
370
376
378 if (
header->refresh_frame_flags & (1 <<
i)) {
379 if (
ctx->ref_tab[
i].frame->buf[0])
381
382 if (apply_grain) {
386 ctx->ref_tab[
i].valid = 1;
387 } else {
388 ctx->ref_tab[
i].valid = 0;
389 }
390 }
391 }
392
393 return 0;
394 }
395
399 {
403 int err, nb_params;
404
405 nb_params =
s->tg_end -
s->tg_start + 1;
406 if (
ctx->nb_slice_params < nb_params) {
408 nb_params,
409 sizeof(*
ctx->slice_params));
410 if (!
ctx->slice_params) {
411 ctx->nb_slice_params = 0;
414 }
415 ctx->nb_slice_params = nb_params;
416 }
417
418 for (
int i =
s->tg_start; i <= s->tg_end;
i++) {
419 ctx->slice_params[
i -
s->tg_start] = (VASliceParameterBufferAV1) {
420 .slice_data_size =
s->tile_group_info[
i].tile_size,
421 .slice_data_offset =
s->tile_group_info[
i].tile_offset,
422 .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
423 .tile_row =
s->tile_group_info[
i].tile_row,
424 .tile_column =
s->tile_group_info[
i].tile_column,
425 .tg_start =
s->tg_start,
427 };
428 }
429
431 sizeof(VASliceParameterBufferAV1),
434 if (err)
436
437 return 0;
438
441 return err;
442 }
443
445 .
p.
name =
"av1_vaapi",
458 };