1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
20
22
23 /* Maximum number of tiles specified by any defined level */
25
29 .decode_op = VK_VIDEO_CODEC_OPERATION_DECODE_AV1_BIT_KHR,
30 .ext_props = {
31 .extensionName = VK_STD_VULKAN_VIDEO_CODEC_AV1_DECODE_EXTENSION_NAME,
32 .specVersion = VK_STD_VULKAN_VIDEO_CODEC_AV1_DECODE_SPEC_VERSION,
33 },
34 };
35
38
39 /* TODO: investigate if this can be removed to make decoding completely
40 * independent. */
42
44
45 /* Current picture */
62
63 /* Picture refs */
67
72
74 VkVideoReferenceSlotInfoKHR *ref_slot, /* Main structure */
75 VkVideoPictureResourceInfoKHR *
ref,
/* Goes in ^ */
76 StdVideoDecodeAV1ReferenceInfo *vkav1_std_ref,
77 VkVideoDecodeAV1DpbSlotInfoKHR *vkav1_ref, /* Goes in ^ */
78 const AV1Frame *pic,
int is_current,
int has_grain,
79 const uint8_t *saved_order_hints)
80 {
85
88 if (err < 0)
89 return err;
90
91 *vkav1_std_ref = (StdVideoDecodeAV1ReferenceInfo) {
92 .flags = (StdVideoDecodeAV1ReferenceInfoFlags) {
95 },
99 };
100
101 if (saved_order_hints) {
103 for (
int i = 1;
i < STD_VIDEO_AV1_TOTAL_REFS_PER_FRAME;
i++)
104 vkav1_std_ref->SavedOrderHints[
i - 1] = saved_order_hints[
i];
105 else
106 for (
int i = 0;
i < STD_VIDEO_AV1_TOTAL_REFS_PER_FRAME;
i++)
107 vkav1_std_ref->SavedOrderHints[
i] = saved_order_hints[
i];
108 }
109
110 *vkav1_ref = (VkVideoDecodeAV1DpbSlotInfoKHR) {
111 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_DPB_SLOT_INFO_KHR,
112 .pStdReferenceInfo = vkav1_std_ref,
113 };
114
118
119 *
ref = (VkVideoPictureResourceInfoKHR) {
120 .sType = VK_STRUCTURE_TYPE_VIDEO_PICTURE_RESOURCE_INFO_KHR,
121 .codedOffset = (VkOffset2D){ 0, 0 },
122 .codedExtent = (VkExtent2D){ pic->
f->
width, pic->
f->
height },
123 .baseArrayLayer = ((has_grain || dec->
dedicated_dpb) &&
ctx->common.layered_dpb) ?
126 };
127
128 *ref_slot = (VkVideoReferenceSlotInfoKHR) {
129 .sType = VK_STRUCTURE_TYPE_VIDEO_REFERENCE_SLOT_INFO_KHR,
130 .pNext = vkav1_ref,
132 .pPictureResource =
ref,
133 };
134
135 if (ref_src)
136 *ref_src = pic;
137
138 return 0;
139 }
140
142 {
146
148
149 StdVideoAV1SequenceHeader av1_sequence_header;
150 StdVideoAV1TimingInfo av1_timing_info;
151 StdVideoAV1ColorConfig av1_color_config;
152 VkVideoDecodeAV1SessionParametersCreateInfoKHR av1_params;
153 VkVideoSessionParametersCreateInfoKHR session_params_create;
154
155 int err;
156
157 av1_timing_info = (StdVideoAV1TimingInfo) {
158 .flags = (StdVideoAV1TimingInfoFlags) {
160 },
164 };
165
166 av1_color_config = (StdVideoAV1ColorConfig) {
167 .flags = (StdVideoAV1ColorConfigFlags) {
171 },
179 };
180
181 av1_sequence_header = (StdVideoAV1SequenceHeader) {
182 .flags = (StdVideoAV1SequenceHeaderFlags) {
202 },
213 .pTimingInfo = &av1_timing_info,
214 .pColorConfig = &av1_color_config,
215 };
216
217 av1_params = (VkVideoDecodeAV1SessionParametersCreateInfoKHR) {
218 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_SESSION_PARAMETERS_CREATE_INFO_KHR,
219 .pStdSequenceHeader = &av1_sequence_header,
220 };
221 session_params_create = (VkVideoSessionParametersCreateInfoKHR) {
222 .sType = VK_STRUCTURE_TYPE_VIDEO_SESSION_PARAMETERS_CREATE_INFO_KHR,
223 .pNext = &av1_params,
224 .videoSession =
ctx->common.session,
225 .videoSessionParametersTemplate = VK_NULL_HANDLE,
226 };
227
229 if (err < 0)
230 return err;
231
233
234 return 0;
235 }
236
240 {
241 int err;
242 int ref_count = 0;
248
251
254 StdVideoAV1FrameRestorationType remap_lr_type[4] = { STD_VIDEO_AV1_FRAME_RESTORATION_TYPE_NONE,
255 STD_VIDEO_AV1_FRAME_RESTORATION_TYPE_SWITCHABLE,
256 STD_VIDEO_AV1_FRAME_RESTORATION_TYPE_WIENER,
257 STD_VIDEO_AV1_FRAME_RESTORATION_TYPE_SGRPROJ };
258
259 if (!dec->session_params) {
261 if (err < 0)
262 return err;
263 }
264
265 if (!ap->frame_id_set) {
266 unsigned slot_idx = 0;
267 for (
unsigned i = 0;
i < 32;
i++) {
268 if (!(dec->frame_id_alloc_mask & (1 <<
i))) {
270 break;
271 }
272 }
273 ap->frame_id = slot_idx;
274 ap->frame_id_set = 1;
275 dec->frame_id_alloc_mask |= (1 << slot_idx);
276 }
277
278 ap->ref_frame_sign_bias_mask = 0x0;
279 for (
int i = 0;
i < STD_VIDEO_AV1_TOTAL_REFS_PER_FRAME;
i++)
280 ap->ref_frame_sign_bias_mask |= pic->ref_frame_sign_bias[
i] <<
i;
281
282 for (
int i = 0;
i < STD_VIDEO_AV1_REFS_PER_FRAME;
i++) {
283 const int idx = pic->raw_frame_header->ref_frame_idx[
i];
286 int found = 0;
287
289 continue;
290
291 for (int j = 0; j < ref_count; j++) {
292 if (vp->ref_slots[j].slotIndex == hp->
frame_id) {
293 found = 1;
294 break;
295 }
296 }
297 if (found)
298 continue;
299
300 err =
vk_av1_fill_pict(avctx, &ap->ref_src[ref_count], &vp->ref_slots[ref_count],
301 &vp->refs[ref_count], &ap->std_refs[ref_count], &ap->vkav1_refs[ref_count],
303 if (err < 0)
304 return err;
305
306 ref_count++;
307 }
308
310 &ap->std_ref,
311 &ap->vkav1_ref,
312 pic, 1, apply_grain,
NULL);
313 if (err < 0)
314 return err;
315
316 ap->av1_pic_info = (VkVideoDecodeAV1PictureInfoKHR) {
317 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_PICTURE_INFO_KHR,
318 .pStdPictureInfo = &ap->std_pic_info,
319 .frameHeaderOffset = 0,
320 .tileCount = 0,
321 .pTileOffsets =
NULL,
322 .pTileSizes = ap->tile_sizes,
323 };
324
325 for (
int i = 0;
i < STD_VIDEO_AV1_REFS_PER_FRAME;
i++) {
326 const int idx = pic->raw_frame_header->ref_frame_idx[
i];
329
332 else
333 ap->av1_pic_info.referenceNameSlotIndices[
i] = hp->
frame_id;
334 }
335
336 vp->decode_info = (VkVideoDecodeInfoKHR) {
337 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_INFO_KHR,
338 .pNext = &ap->av1_pic_info,
339 .flags = 0x0,
340 .pSetupReferenceSlot = &vp->ref_slot,
341 .referenceSlotCount = ref_count,
342 .pReferenceSlots = vp->ref_slots,
343 .dstPictureResource = (VkVideoPictureResourceInfoKHR) {
344 .sType = VK_STRUCTURE_TYPE_VIDEO_PICTURE_RESOURCE_INFO_KHR,
345 .codedOffset = (VkOffset2D){ 0, 0 },
346 .codedExtent = (VkExtent2D){ pic->f->width, pic->f->height },
347 .baseArrayLayer = 0,
348 .imageViewBinding = vp->img_view_out,
349 },
350 };
351
352 ap->tile_info = (StdVideoAV1TileInfo) {
353 .flags = (StdVideoAV1TileInfoFlags) {
354 .uniform_tile_spacing_flag =
frame_header->uniform_tile_spacing_flag,
355 },
358 .context_update_tile_id =
frame_header->context_update_tile_id,
359 .tile_size_bytes_minus_1 =
frame_header->tile_size_bytes_minus1,
360 .pWidthInSbsMinus1 = ap->width_in_sbs_minus1,
361 .pHeightInSbsMinus1 = ap->height_in_sbs_minus1,
362 .pMiColStarts = ap->mi_col_starts,
363 .pMiRowStarts = ap->mi_row_starts,
364 };
365
366 ap->quantization = (StdVideoAV1Quantization) {
378 };
379
380 ap->loop_filter = (StdVideoAV1LoopFilter) {
381 .
flags = (StdVideoAV1LoopFilterFlags) {
382 .loop_filter_delta_enabled =
frame_header->loop_filter_delta_enabled,
383 .loop_filter_delta_update =
frame_header->loop_filter_delta_update,
384 },
385 .loop_filter_sharpness =
frame_header->loop_filter_sharpness,
386 };
387
388 for (
int i = 0;
i < STD_VIDEO_AV1_MAX_LOOP_FILTER_STRENGTHS;
i++)
389 ap->loop_filter.loop_filter_level[
i] =
frame_header->loop_filter_level[
i];
390 for (
int i = 0;
i < STD_VIDEO_AV1_LOOP_FILTER_ADJUSTMENTS;
i++)
391 ap->loop_filter.loop_filter_mode_deltas[
i] =
frame_header->loop_filter_mode_deltas[
i];
392
393 ap->cdef = (StdVideoAV1CDEF) {
394 .cdef_damping_minus_3 =
frame_header->cdef_damping_minus_3,
396 };
397
398 ap->loop_restoration = (StdVideoAV1LoopRestoration) {
399 .FrameRestorationType[0] = remap_lr_type[
frame_header->lr_type[0]],
400 .FrameRestorationType[1] = remap_lr_type[
frame_header->lr_type[1]],
401 .FrameRestorationType[2] = remap_lr_type[
frame_header->lr_type[2]],
402 .LoopRestorationSize[0] = 1 +
frame_header->lr_unit_shift,
405 };
406
407 ap->film_grain = (StdVideoAV1FilmGrain) {
408 .
flags = (StdVideoAV1FilmGrainFlags) {
409 .chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma,
410 .overlap_flag = film_grain->overlap_flag,
411 .clip_to_restricted_range = film_grain->clip_to_restricted_range,
412 },
413 .grain_scaling_minus_8 = film_grain->grain_scaling_minus_8,
414 .ar_coeff_lag = film_grain->ar_coeff_lag,
415 .ar_coeff_shift_minus_6 = film_grain->ar_coeff_shift_minus_6,
416 .grain_scale_shift = film_grain->grain_scale_shift,
417 .grain_seed = film_grain->grain_seed,
418 .film_grain_params_ref_idx = film_grain->film_grain_params_ref_idx,
419 .num_y_points = film_grain->num_y_points,
420 .num_cb_points = film_grain->num_cb_points,
421 .num_cr_points = film_grain->num_cr_points,
422 .cb_mult = film_grain->cb_mult,
423 .cb_luma_mult = film_grain->cb_luma_mult,
424 .cb_offset = film_grain->cb_offset,
425 .cr_mult = film_grain->cr_mult,
426 .cr_luma_mult = film_grain->cr_luma_mult,
427 .cr_offset = film_grain->cr_offset,
428 };
429
430 /* Setup frame header */
431 ap->std_pic_info = (StdVideoDecodeAV1PictureInfo) {
432 .flags = (StdVideoDecodeAV1PictureInfoFlags) {
433 .error_resilient_mode =
frame_header->error_resilient_mode,
436 .render_and_frame_size_different =
frame_header->render_and_frame_size_different,
437 .allow_screen_content_tools =
frame_header->allow_screen_content_tools,
438 .is_filter_switchable =
frame_header->is_filter_switchable,
439 .force_integer_mv = pic->force_integer_mv,
440 .frame_size_override_flag =
frame_header->frame_size_override_flag,
441 .buffer_removal_time_present_flag =
frame_header->buffer_removal_time_present_flag,
443 .frame_refs_short_signaling =
frame_header->frame_refs_short_signaling,
444 .allow_high_precision_mv =
frame_header->allow_high_precision_mv,
445 .is_motion_mode_switchable =
frame_header->is_motion_mode_switchable,
447 .disable_frame_end_update_cdf =
frame_header->disable_frame_end_update_cdf,
448 .allow_warped_motion =
frame_header->allow_warped_motion,
455 .segmentation_enabled =
frame_header->segmentation_enabled,
456 .segmentation_update_map =
frame_header->segmentation_update_map,
457 .segmentation_temporal_update =
frame_header->segmentation_temporal_update,
458 .segmentation_update_data =
frame_header->segmentation_update_data,
460 .apply_grain = apply_grain,
461 },
466 .refresh_frame_flags =
frame_header->refresh_frame_flags,
467 .interpolation_filter =
frame_header->interpolation_filter,
471 .SkipModeFrame[0] =
s->cur_frame.skip_mode_frame_idx[0],
472 .SkipModeFrame[1] =
s->cur_frame.skip_mode_frame_idx[1],
474 .pTileInfo = &ap->tile_info,
475 .pQuantization = &ap->quantization,
476 .pSegmentation = &ap->segmentation,
477 .pLoopFilter = &ap->loop_filter,
478 .pCDEF = &ap->cdef,
479 .pLoopRestoration = &ap->loop_restoration,
480 .pGlobalMotion = &ap->global_motion,
481 .pFilmGrain = apply_grain ? &ap->film_grain :
NULL,
482 };
483
484 for (
int i = 0;
i < 64;
i++) {
485 ap->width_in_sbs_minus1[
i] =
frame_header->width_in_sbs_minus_1[
i];
486 ap->height_in_sbs_minus1[
i] =
frame_header->height_in_sbs_minus_1[
i];
489 }
490
491 for (
int i = 0;
i < STD_VIDEO_AV1_MAX_SEGMENTS;
i++) {
492 ap->segmentation.FeatureEnabled[
i] = 0x0;
493 for (int j = 0; j < STD_VIDEO_AV1_SEG_LVL_MAX; j++) {
494 ap->segmentation.FeatureEnabled[
i] |= (
frame_header->feature_enabled[
i][j] << j);
495 ap->segmentation.FeatureData[
i][j] =
frame_header->feature_value[
i][j];
496 }
497 }
498
499 if (dec->quirk_av1_offset)
500 for (
int i = 1;
i < STD_VIDEO_AV1_TOTAL_REFS_PER_FRAME;
i++)
501 ap->std_pic_info.OrderHints[
i - 1] = pic->order_hints[
i];
502 else
503 for (
int i = 0;
i < STD_VIDEO_AV1_TOTAL_REFS_PER_FRAME;
i++)
504 ap->std_pic_info.OrderHints[
i] = pic->order_hints[
i];
505
506 for (
int i = 0;
i < STD_VIDEO_AV1_TOTAL_REFS_PER_FRAME;
i++) {
507 ap->loop_filter.loop_filter_ref_deltas[
i] =
frame_header->loop_filter_ref_deltas[
i];
508 ap->global_motion.GmType[
i] =
s->cur_frame.gm_type[
i];
509 for (int j = 0; j < STD_VIDEO_AV1_GLOBAL_MOTION_PARAMS; j++) {
510 ap->global_motion.gm_params[
i][j] =
s->cur_frame.gm_params[
i][j];
511 }
512 }
513
514 for (
int i = 0;
i < STD_VIDEO_AV1_MAX_CDEF_FILTER_STRENGTHS;
i++) {
515 ap->cdef.cdef_y_pri_strength[
i] =
frame_header->cdef_y_pri_strength[
i];
516 ap->cdef.cdef_y_sec_strength[
i] =
frame_header->cdef_y_sec_strength[
i];
517 ap->cdef.cdef_uv_pri_strength[
i] =
frame_header->cdef_uv_pri_strength[
i];
518 ap->cdef.cdef_uv_sec_strength[
i] =
frame_header->cdef_uv_sec_strength[
i];
519 }
520
521 if (apply_grain) {
522 for (
int i = 0;
i < STD_VIDEO_AV1_MAX_NUM_Y_POINTS;
i++) {
523 ap->film_grain.point_y_value[
i] = film_grain->point_y_value[
i];
524 ap->film_grain.point_y_scaling[
i] = film_grain->point_y_scaling[
i];
525 }
526
527 for (
int i = 0;
i < STD_VIDEO_AV1_MAX_NUM_CB_POINTS;
i++) {
528 ap->film_grain.point_cb_value[
i] = film_grain->point_cb_value[
i];
529 ap->film_grain.point_cb_scaling[
i] = film_grain->point_cb_scaling[
i];
530 ap->film_grain.point_cr_value[
i] = film_grain->point_cr_value[
i];
531 ap->film_grain.point_cr_scaling[
i] = film_grain->point_cr_scaling[
i];
532 }
533
534 for (
int i = 0;
i < STD_VIDEO_AV1_MAX_NUM_POS_LUMA;
i++)
535 ap->film_grain.ar_coeffs_y_plus_128[
i] = film_grain->ar_coeffs_y_plus_128[
i];
536
537 for (
int i = 0;
i < STD_VIDEO_AV1_MAX_NUM_POS_CHROMA;
i++) {
538 ap->film_grain.ar_coeffs_cb_plus_128[
i] = film_grain->ar_coeffs_cb_plus_128[
i];
539 ap->film_grain.ar_coeffs_cr_plus_128[
i] = film_grain->ar_coeffs_cr_plus_128[
i];
540 }
541 }
542
543 ap->dec = dec;
544
545 return 0;
546 }
547
551 {
552 int err;
556
557 /* Too many tiles, exceeding all defined levels in the AV1 spec */
560
561 for (
int i =
s->tg_start; i <= s->tg_end;
i++) {
563
565 data +
s->tile_group_info[
i].tile_offset,
566 s->tile_group_info[
i].tile_size, 0,
569 if (err < 0)
570 return err;
571 }
572
573 return 0;
574 }
575
577 {
585
587 return 0;
588
591 if (err < 0)
592 return err;
593 }
594
598
601 }
602
605
607 }
608
610 {
613
614 /* Workaround for a spec issue. */
617
618 /* Free frame resources, this also destroys the session parameters. */
620 }
621
623 .
p.
name =
"av1_vulkan",
639
640 /* NOTE: Threading is intentionally disabled here. Due to the design of Vulkan,
641 * where frames are opaque to users, and mostly opaque for driver developers,
642 * there's an issue with current hardware accelerator implementations of AV1,
643 * where they require an internal index. With regular hwaccel APIs, this index
644 * is given to users as an opaque handle directly. With Vulkan, due to increased
645 * flexibility, this index cannot be present anywhere.
646 * The current implementation tracks the index for the driver and submits it
647 * as necessary information. Due to needing to modify the decoding context,
648 * which is not thread-safe, on frame free, threading is disabled. */
650 };