FFmpeg
Loading...
Searching...
No Matches
vulkan_h264.c
Go to the documentation of this file.
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
19#include "libavutil/mem.h"
20
21#include "h264dec.h"
22#include "h264_ps.h"
23
24#include "vulkan_decode.h"
25
27 .codec_id = AV_CODEC_ID_H264,
28 .decode_extension = FF_VK_EXT_VIDEO_DECODE_H264,
29 .queue_flags = VK_QUEUE_VIDEO_DECODE_BIT_KHR,
30 .decode_op = VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR,
31 .ext_props = {
32 .extensionName = VK_STD_VULKAN_VIDEO_CODEC_H264_DECODE_EXTENSION_NAME,
33 .specVersion = VK_STD_VULKAN_VIDEO_CODEC_H264_DECODE_SPEC_VERSION,
34 },
35};
36
39
40 /* Current picture */
41 StdVideoDecodeH264ReferenceInfo h264_ref;
42 VkVideoDecodeH264DpbSlotInfoKHR vkh264_ref;
43
44 /* Picture refs */
46 StdVideoDecodeH264ReferenceInfo h264_refs [H264_MAX_PICTURE_COUNT];
47 VkVideoDecodeH264DpbSlotInfoKHR vkh264_refs[H264_MAX_PICTURE_COUNT];
48
49 /* Current picture (contd.) */
50 StdVideoDecodeH264PictureInfo h264pic;
51 VkVideoDecodeH264PictureInfoKHR h264_pic_info;
53
54const static int h264_scaling_list8_order[] = { 0, 3, 1, 4, 2, 5 };
55
56static int vk_h264_fill_pict(AVCodecContext *avctx, H264Picture **ref_src,
57 VkVideoReferenceSlotInfoKHR *ref_slot, /* Main structure */
58 VkVideoPictureResourceInfoKHR *ref, /* Goes in ^ */
59 VkVideoDecodeH264DpbSlotInfoKHR *vkh264_ref, /* Goes in ^ */
60 StdVideoDecodeH264ReferenceInfo *h264_ref, /* Goes in ^ */
61 H264Picture *pic, int is_current,
62 int is_field, int picture_structure,
63 int dpb_slot_index)
64{
68 FFVulkanDecodePicture *vkpic = &hp->vp;
69
70 int err = ff_vk_decode_prepare_frame(dec, pic->f, vkpic, is_current,
71 dec->dedicated_dpb);
72 if (err < 0)
73 return err;
74
75 *h264_ref = (StdVideoDecodeH264ReferenceInfo) {
76 .FrameNum = pic->long_ref ? pic->pic_id : pic->frame_num,
77 .PicOrderCnt = { pic->field_poc[0], pic->field_poc[1] },
78 .flags = (StdVideoDecodeH264ReferenceInfoFlags) {
79 .top_field_flag = is_field ? !!(picture_structure & PICT_TOP_FIELD) : 0,
80 .bottom_field_flag = is_field ? !!(picture_structure & PICT_BOTTOM_FIELD) : 0,
81 .used_for_long_term_reference = pic->reference && pic->long_ref,
82 /*
83 * flags.is_non_existing is used to indicate whether the picture is marked as
84 * “non-existing” as defined in section 8.2.5.2 of the ITU-T H.264 Specification;
85 * 8.2.5.2 Decoding process for gaps in frame_num
86 * corresponds to the code in h264_slice.c:h264_field_start,
87 * which sets the invalid_gap flag when decoding.
88 */
89 .is_non_existing = pic->invalid_gap,
90 },
91 };
92
93 *vkh264_ref = (VkVideoDecodeH264DpbSlotInfoKHR) {
94 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_DPB_SLOT_INFO_KHR,
95 .pStdReferenceInfo = h264_ref,
96 };
97
98 *ref = (VkVideoPictureResourceInfoKHR) {
99 .sType = VK_STRUCTURE_TYPE_VIDEO_PICTURE_RESOURCE_INFO_KHR,
100 .codedOffset = (VkOffset2D){ 0, 0 },
101 .codedExtent = (VkExtent2D){ pic->f->width, pic->f->height },
102 .baseArrayLayer = ctx->common.layered_dpb ? dpb_slot_index : 0,
103 .imageViewBinding = vkpic->view.ref,
104 };
105
106 *ref_slot = (VkVideoReferenceSlotInfoKHR) {
107 .sType = VK_STRUCTURE_TYPE_VIDEO_REFERENCE_SLOT_INFO_KHR,
108 .pNext = vkh264_ref,
109 .slotIndex = dpb_slot_index,
110 .pPictureResource = ref,
111 };
112
113 if (ref_src)
114 *ref_src = pic;
115
116 return 0;
117}
118
119static StdVideoH264LevelIdc convert_to_vk_level_idc(int level_idc)
120{
121 switch (level_idc) {
122 case 10: return STD_VIDEO_H264_LEVEL_IDC_1_0;
123 case 11: return STD_VIDEO_H264_LEVEL_IDC_1_1;
124 case 12: return STD_VIDEO_H264_LEVEL_IDC_1_2;
125 case 13: return STD_VIDEO_H264_LEVEL_IDC_1_3;
126 case 20: return STD_VIDEO_H264_LEVEL_IDC_2_0;
127 case 21: return STD_VIDEO_H264_LEVEL_IDC_2_1;
128 case 22: return STD_VIDEO_H264_LEVEL_IDC_2_2;
129 case 30: return STD_VIDEO_H264_LEVEL_IDC_3_0;
130 case 31: return STD_VIDEO_H264_LEVEL_IDC_3_1;
131 case 32: return STD_VIDEO_H264_LEVEL_IDC_3_2;
132 case 40: return STD_VIDEO_H264_LEVEL_IDC_4_0;
133 case 41: return STD_VIDEO_H264_LEVEL_IDC_4_1;
134 case 42: return STD_VIDEO_H264_LEVEL_IDC_4_2;
135 case 50: return STD_VIDEO_H264_LEVEL_IDC_5_0;
136 case 51: return STD_VIDEO_H264_LEVEL_IDC_5_1;
137 case 52: return STD_VIDEO_H264_LEVEL_IDC_5_2;
138 case 60: return STD_VIDEO_H264_LEVEL_IDC_6_0;
139 case 61: return STD_VIDEO_H264_LEVEL_IDC_6_1;
140 default:
141 case 62: return STD_VIDEO_H264_LEVEL_IDC_6_2;
142 }
143}
144
145static void set_sps(const SPS *sps,
146 StdVideoH264ScalingLists *vksps_scaling,
147 StdVideoH264HrdParameters *vksps_vui_header,
148 StdVideoH264SequenceParameterSetVui *vksps_vui,
149 StdVideoH264SequenceParameterSet *vksps)
150{
151 *vksps_scaling = (StdVideoH264ScalingLists) {
152 .scaling_list_present_mask = sps->scaling_matrix_present_mask,
153 .use_default_scaling_matrix_mask = 0, /* We already fill in the default matrix */
154 };
155
156 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS; i++)
157 for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS; j++)
158 vksps_scaling->ScalingList4x4[i][j] = sps->scaling_matrix4[i][ff_zigzag_scan[j]];
159
160 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_LISTS; i++)
161 for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS; j++)
162 vksps_scaling->ScalingList8x8[i][j] =
163 sps->scaling_matrix8[h264_scaling_list8_order[i]][ff_zigzag_direct[j]];
164
165 *vksps_vui_header = (StdVideoH264HrdParameters) {
166 .cpb_cnt_minus1 = sps->cpb_cnt - 1,
167 .bit_rate_scale = sps->bit_rate_scale,
168 .initial_cpb_removal_delay_length_minus1 = sps->initial_cpb_removal_delay_length - 1,
169 .cpb_removal_delay_length_minus1 = sps->cpb_removal_delay_length - 1,
170 .dpb_output_delay_length_minus1 = sps->dpb_output_delay_length - 1,
171 .time_offset_length = sps->time_offset_length,
172 };
173
174 for (int i = 0; i < sps->cpb_cnt; i++) {
175 vksps_vui_header->bit_rate_value_minus1[i] = sps->bit_rate_value[i] - 1;
176 vksps_vui_header->cpb_size_value_minus1[i] = sps->cpb_size_value[i] - 1;
177 vksps_vui_header->cbr_flag[i] = (sps->cpr_flag >> i) & 0x1;
178 }
179
180 *vksps_vui = (StdVideoH264SequenceParameterSetVui) {
181 .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
182 .sar_width = sps->vui.sar.num,
183 .sar_height = sps->vui.sar.den,
184 .video_format = sps->vui.video_format,
185 .colour_primaries = sps->vui.colour_primaries,
186 .transfer_characteristics = sps->vui.transfer_characteristics,
187 .matrix_coefficients = sps->vui.matrix_coeffs,
188 .num_units_in_tick = sps->num_units_in_tick,
189 .time_scale = sps->time_scale,
190 .pHrdParameters = vksps_vui_header,
191 .max_num_reorder_frames = sps->num_reorder_frames,
192 .max_dec_frame_buffering = sps->max_dec_frame_buffering,
193 .flags = (StdVideoH264SpsVuiFlags) {
194 .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
195 .overscan_info_present_flag = sps->vui.overscan_info_present_flag,
196 .overscan_appropriate_flag = sps->vui.overscan_appropriate_flag,
197 .video_signal_type_present_flag = sps->vui.video_signal_type_present_flag,
198 .video_full_range_flag = sps->vui.video_full_range_flag,
199 .color_description_present_flag = sps->vui.colour_description_present_flag,
200 .chroma_loc_info_present_flag = sps->vui.chroma_loc_info_present_flag,
201 .timing_info_present_flag = sps->timing_info_present_flag,
202 .fixed_frame_rate_flag = sps->fixed_frame_rate_flag,
203 .bitstream_restriction_flag = sps->bitstream_restriction_flag,
204 .nal_hrd_parameters_present_flag = sps->nal_hrd_parameters_present_flag,
205 .vcl_hrd_parameters_present_flag = sps->vcl_hrd_parameters_present_flag,
206 },
207 };
208
209 *vksps = (StdVideoH264SequenceParameterSet) {
210 .profile_idc = sps->profile_idc,
211 .level_idc = convert_to_vk_level_idc(sps->level_idc),
212 .seq_parameter_set_id = sps->sps_id,
213 .chroma_format_idc = sps->chroma_format_idc,
214 .bit_depth_luma_minus8 = sps->bit_depth_luma - 8,
215 .bit_depth_chroma_minus8 = sps->bit_depth_chroma - 8,
216 .log2_max_frame_num_minus4 = sps->log2_max_frame_num - 4,
217 .pic_order_cnt_type = sps->poc_type,
218 .log2_max_pic_order_cnt_lsb_minus4 = sps->poc_type ? 0 : sps->log2_max_poc_lsb - 4,
219 .offset_for_non_ref_pic = sps->offset_for_non_ref_pic,
220 .offset_for_top_to_bottom_field = sps->offset_for_top_to_bottom_field,
221 .num_ref_frames_in_pic_order_cnt_cycle = sps->poc_cycle_length,
222 .max_num_ref_frames = sps->ref_frame_count,
223 .pic_width_in_mbs_minus1 = sps->mb_width - 1,
224 .pic_height_in_map_units_minus1 = (sps->mb_height/(2 - sps->frame_mbs_only_flag)) - 1,
225 .frame_crop_left_offset = sps->crop_left,
226 .frame_crop_right_offset = sps->crop_right,
227 .frame_crop_top_offset = sps->crop_top,
228 .frame_crop_bottom_offset = sps->crop_bottom,
229 .flags = (StdVideoH264SpsFlags) {
230 .constraint_set0_flag = (sps->constraint_set_flags >> 0) & 0x1,
231 .constraint_set1_flag = (sps->constraint_set_flags >> 1) & 0x1,
232 .constraint_set2_flag = (sps->constraint_set_flags >> 2) & 0x1,
233 .constraint_set3_flag = (sps->constraint_set_flags >> 3) & 0x1,
234 .constraint_set4_flag = (sps->constraint_set_flags >> 4) & 0x1,
235 .constraint_set5_flag = (sps->constraint_set_flags >> 5) & 0x1,
236 .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
237 .mb_adaptive_frame_field_flag = sps->mb_aff,
238 .frame_mbs_only_flag = sps->frame_mbs_only_flag,
239 .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
240 .separate_colour_plane_flag = sps->residual_color_transform_flag,
241 .gaps_in_frame_num_value_allowed_flag = sps->gaps_in_frame_num_allowed_flag,
242 .qpprime_y_zero_transform_bypass_flag = sps->transform_bypass,
243 .frame_cropping_flag = sps->crop,
244 .seq_scaling_matrix_present_flag = sps->scaling_matrix_present,
245 .vui_parameters_present_flag = sps->vui_parameters_present_flag,
246 },
247 .pOffsetForRefFrame = sps->offset_for_ref_frame,
248 .pScalingLists = vksps_scaling,
249 .pSequenceParameterSetVui = vksps_vui,
250 };
251}
252
253static void set_pps(const PPS *pps, const SPS *sps,
254 StdVideoH264ScalingLists *vkpps_scaling,
255 StdVideoH264PictureParameterSet *vkpps)
256{
257 *vkpps_scaling = (StdVideoH264ScalingLists) {
258 .scaling_list_present_mask = pps->pic_scaling_matrix_present_mask,
259 .use_default_scaling_matrix_mask = 0, /* We already fill in the default matrix */
260 };
261
262 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS; i++)
263 for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS; j++)
264 vkpps_scaling->ScalingList4x4[i][j] = pps->scaling_matrix4[i][ff_zigzag_scan[j]];
265
266 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_LISTS; i++)
267 for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS; j++)
268 vkpps_scaling->ScalingList8x8[i][j] =
269 pps->scaling_matrix8[h264_scaling_list8_order[i]][ff_zigzag_direct[j]];
270
271 *vkpps = (StdVideoH264PictureParameterSet) {
272 .seq_parameter_set_id = pps->sps_id,
273 .pic_parameter_set_id = pps->pps_id,
274 .num_ref_idx_l0_default_active_minus1 = pps->ref_count[0] - 1,
275 .num_ref_idx_l1_default_active_minus1 = pps->ref_count[1] - 1,
276 .weighted_bipred_idc = pps->weighted_bipred_idc,
277 .pic_init_qp_minus26 = pps->init_qp - 26,
278 .pic_init_qs_minus26 = pps->init_qs - 26,
279 .chroma_qp_index_offset = pps->chroma_qp_index_offset[0],
280 .second_chroma_qp_index_offset = pps->chroma_qp_index_offset[1],
281 .flags = (StdVideoH264PpsFlags) {
282 .transform_8x8_mode_flag = pps->transform_8x8_mode,
283 .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present,
284 .constrained_intra_pred_flag = pps->constrained_intra_pred,
285 .deblocking_filter_control_present_flag = pps->deblocking_filter_parameters_present,
286 .weighted_pred_flag = pps->weighted_pred,
287 .bottom_field_pic_order_in_frame_present_flag = pps->pic_order_present,
288 .entropy_coding_mode_flag = pps->cabac,
289 .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
290 },
291 .pScalingLists = vkpps_scaling,
292 };
293}
294
295/* Too large to put on the stack: musl's default thread stack is 128KiB */
296typedef struct VulkanH264Params {
297 /* SPS */
298 StdVideoH264ScalingLists vksps_scaling[MAX_SPS_COUNT];
299 StdVideoH264HrdParameters vksps_vui_header[MAX_SPS_COUNT];
300 StdVideoH264SequenceParameterSetVui vksps_vui[MAX_SPS_COUNT];
301 StdVideoH264SequenceParameterSet vksps[MAX_SPS_COUNT];
302
303 /* PPS */
304 StdVideoH264ScalingLists vkpps_scaling[MAX_PPS_COUNT];
305 StdVideoH264PictureParameterSet vkpps[MAX_PPS_COUNT];
307
308static int vk_h264_create_params(AVCodecContext *avctx, VkVideoSessionParametersKHR **buf)
309{
310 int err;
313 const H264Context *h = avctx->priv_data;
314
315 VulkanH264Params *par = av_malloc(sizeof(*par));
316 if (!par)
317 return AVERROR(ENOMEM);
318
319 VkVideoDecodeH264SessionParametersAddInfoKHR h264_params_info = {
320 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_SESSION_PARAMETERS_ADD_INFO_KHR,
321 .pStdSPSs = par->vksps,
322 .stdSPSCount = 0,
323 .pStdPPSs = par->vkpps,
324 .stdPPSCount = 0,
325 };
326 VkVideoDecodeH264SessionParametersCreateInfoKHR h264_params = {
327 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_SESSION_PARAMETERS_CREATE_INFO_KHR,
328 .pParametersAddInfo = &h264_params_info,
329 };
330 VkVideoSessionParametersCreateInfoKHR session_params_create = {
331 .sType = VK_STRUCTURE_TYPE_VIDEO_SESSION_PARAMETERS_CREATE_INFO_KHR,
332 .pNext = &h264_params,
333 .videoSession = ctx->common.session,
334 .videoSessionParametersTemplate = VK_NULL_HANDLE,
335 };
336
337 /* SPS list */
338 for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++) {
339 if (h->ps.sps_list[i]) {
340 const SPS *sps_l = h->ps.sps_list[i];
341 int idx = h264_params_info.stdSPSCount;
342 set_sps(sps_l, &par->vksps_scaling[idx], &par->vksps_vui_header[idx],
343 &par->vksps_vui[idx], &par->vksps[idx]);
344 h264_params_info.stdSPSCount++;
345 }
346 }
347
348 /* PPS list */
349 for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
350 if (h->ps.pps_list[i]) {
351 const PPS *pps_l = h->ps.pps_list[i];
352 int idx = h264_params_info.stdPPSCount;
353 set_pps(pps_l, pps_l->sps, &par->vkpps_scaling[idx], &par->vkpps[idx]);
354 h264_params_info.stdPPSCount++;
355 }
356 }
357
358 h264_params.maxStdSPSCount = h264_params_info.stdSPSCount;
359 h264_params.maxStdPPSCount = h264_params_info.stdPPSCount;
360
361 err = ff_vk_decode_create_params(buf, avctx, ctx, &session_params_create);
362 av_free(par);
363 if (err < 0)
364 return err;
365
366 av_log(avctx, AV_LOG_DEBUG, "Created frame parameters: %i SPS %i PPS\n",
367 h264_params_info.stdSPSCount, h264_params_info.stdPPSCount);
368
369 return 0;
370}
371
373 av_unused const AVBufferRef *buffer_ref,
374 av_unused const uint8_t *buffer,
375 av_unused uint32_t size)
376{
377 int err;
378 int dpb_slot_index = 0;
379 H264Context *h = avctx->priv_data;
380
381 H264Picture *pic = h->cur_pic_ptr;
383 FFVulkanDecodePicture *vp = &hp->vp;
384
385 /* Fill in main slot */
386 dpb_slot_index = 0;
387 for (unsigned slot = 0; slot < H264_MAX_PICTURE_COUNT; slot++) {
388 if (pic == &h->DPB[slot]) {
389 dpb_slot_index = slot;
390 break;
391 }
392 }
393
394 err = vk_h264_fill_pict(avctx, NULL, &vp->ref_slot, &vp->ref,
395 &hp->vkh264_ref, &hp->h264_ref, pic, 1,
396 h->DPB[dpb_slot_index].field_picture,
397 h->DPB[dpb_slot_index].reference,
398 dpb_slot_index);
399 if (err < 0)
400 return err;
401
402 /* Fill in short-term references */
403 for (int i = 0; i < h->short_ref_count; i++) {
404 dpb_slot_index = 0;
405 for (unsigned slot = 0; slot < H264_MAX_PICTURE_COUNT; slot++) {
406 if (h->short_ref[i] == &h->DPB[slot]) {
407 dpb_slot_index = slot;
408 break;
409 }
410 }
411 err = vk_h264_fill_pict(avctx, &hp->ref_src[i], &vp->ref_slots[i],
412 &vp->refs[i], &hp->vkh264_refs[i],
413 &hp->h264_refs[i], h->short_ref[i], 0,
414 h->DPB[dpb_slot_index].field_picture,
415 h->DPB[dpb_slot_index].reference,
416 dpb_slot_index);
417 if (err < 0)
418 return err;
419 }
420
421 /* Fill in long-term refs */
422 for (int r = 0, i = h->short_ref_count; r < H264_MAX_DPB_FRAMES &&
423 i < h->short_ref_count + h->long_ref_count; r++) {
424 if (!h->long_ref[r])
425 continue;
426
427 dpb_slot_index = 0;
428 for (unsigned slot = 0; slot < 16; slot++) {
429 if (h->long_ref[r] == &h->DPB[slot]) {
430 dpb_slot_index = slot;
431 break;
432 }
433 }
434 err = vk_h264_fill_pict(avctx, &hp->ref_src[i], &vp->ref_slots[i],
435 &vp->refs[i], &hp->vkh264_refs[i],
436 &hp->h264_refs[i], h->long_ref[r], 0,
437 h->DPB[dpb_slot_index].field_picture,
438 h->DPB[dpb_slot_index].reference,
439 dpb_slot_index);
440 if (err < 0)
441 return err;
442 i++;
443 }
444
445 hp->h264pic = (StdVideoDecodeH264PictureInfo) {
446 .seq_parameter_set_id = pic->pps->sps_id,
447 .pic_parameter_set_id = pic->pps->pps_id,
448 .frame_num = 0, /* Set later */
449 .idr_pic_id = 0, /* Set later */
450 .PicOrderCnt[0] = pic->field_poc[0],
451 .PicOrderCnt[1] = pic->field_poc[1],
452 .flags = (StdVideoDecodeH264PictureInfoFlags) {
453 .field_pic_flag = FIELD_PICTURE(h),
454 .is_intra = 1, /* Set later */
455 .IdrPicFlag = h->picture_idr,
456 .bottom_field_flag = h->picture_structure != PICT_FRAME &&
457 h->picture_structure & PICT_BOTTOM_FIELD,
458 .is_reference = h->nal_ref_idc != 0,
459 .complementary_field_pair = h->first_field && FIELD_PICTURE(h),
460 },
461 };
462
463 hp->h264_pic_info = (VkVideoDecodeH264PictureInfoKHR) {
464 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_PICTURE_INFO_KHR,
465 .pStdPictureInfo = &hp->h264pic,
466 };
467
468 vp->decode_info = (VkVideoDecodeInfoKHR) {
469 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_INFO_KHR,
470 .pNext = &hp->h264_pic_info,
471 .flags = 0x0,
472 .pSetupReferenceSlot = &vp->ref_slot,
473 .referenceSlotCount = h->short_ref_count + h->long_ref_count,
474 .pReferenceSlots = vp->ref_slots,
475 .dstPictureResource = (VkVideoPictureResourceInfoKHR) {
476 .sType = VK_STRUCTURE_TYPE_VIDEO_PICTURE_RESOURCE_INFO_KHR,
477 .codedOffset = (VkOffset2D){ 0, 0 },
478 .codedExtent = (VkExtent2D){ pic->f->width, pic->f->height },
479 .baseArrayLayer = 0,
480 .imageViewBinding = vp->view.out,
481 },
482 };
483
484 return 0;
485}
486
488 const uint8_t *data,
489 uint32_t size)
490{
491 const H264Context *h = avctx->priv_data;
492 const H264SliceContext *sl = &h->slice_ctx[0];
493 H264VulkanDecodePicture *hp = h->cur_pic_ptr->hwaccel_picture_private;
494 FFVulkanDecodePicture *vp = &hp->vp;
495
496 int err = ff_vk_decode_add_slice(avctx, vp, data, size, 1,
497 &hp->h264_pic_info.sliceCount,
498 &hp->h264_pic_info.pSliceOffsets);
499 if (err < 0)
500 return err;
501
502 hp->h264pic.frame_num = sl->frame_num;
503 hp->h264pic.idr_pic_id = sl->idr_pic_id;
504
505 /* Frame is only intra of all slices are marked as intra */
507 hp->h264pic.flags.is_intra = 0;
508
509 return 0;
510}
511
513{
514 const H264Context *h = avctx->priv_data;
517
518 H264Picture *pic = h->cur_pic_ptr;
520 FFVulkanDecodePicture *vp = &hp->vp;
522 AVFrame *rav[H264_MAX_PICTURE_COUNT] = { 0 };
523
524#ifdef VK_KHR_video_maintenance2
525 StdVideoH264ScalingLists vksps_scaling;
526 StdVideoH264HrdParameters vksps_vui_header;
527 StdVideoH264SequenceParameterSetVui vksps_vui;
528 StdVideoH264SequenceParameterSet vksps;
529 StdVideoH264ScalingLists vkpps_scaling;
530 StdVideoH264PictureParameterSet vkpps;
531 VkVideoDecodeH264InlineSessionParametersInfoKHR h264_params;
532
533 if (ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_2) {
534 set_sps(h->ps.sps, &vksps_scaling,
535 &vksps_vui_header, &vksps_vui, &vksps);
536 set_pps(h->ps.pps, h->ps.sps, &vkpps_scaling, &vkpps);
537 h264_params = (VkVideoDecodeH264InlineSessionParametersInfoKHR) {
538 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_INLINE_SESSION_PARAMETERS_INFO_KHR,
539 .pStdSPS = &vksps,
540 .pStdPPS = &vkpps,
541 };
542 hp->h264_pic_info.pNext = &h264_params;
543 }
544#endif
545
546 if (!hp->h264_pic_info.sliceCount)
547 return 0;
548
549 if (!vp->slices_buf)
550 return AVERROR(EINVAL);
551
552 if (!dec->session_params &&
553 !(ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_2)) {
554 int err = vk_h264_create_params(avctx, &dec->session_params);
555 if (err < 0)
556 return err;
557
558 hp->h264pic.seq_parameter_set_id = pic->pps->sps_id;
559 hp->h264pic.pic_parameter_set_id = pic->pps->pps_id;
560 }
561
562 for (int i = 0; i < vp->decode_info.referenceSlotCount; i++) {
563 H264Picture *rp = hp->ref_src[i];
565
566 rvp[i] = &rhp->vp;
567 rav[i] = hp->ref_src[i]->f;
568 }
569
570 av_log(avctx, AV_LOG_DEBUG, "Decoding frame, %zu bytes, %i slices\n",
571 vp->slices_size, hp->h264_pic_info.sliceCount);
572
573 return ff_vk_decode_frame(avctx, pic->f, vp, rav, rvp);
574}
575
577{
578 AVHWDeviceContext *hwctx = _hwctx.nc;
580
581 /* Free frame resources, this also destroys the session parameters. */
582 ff_vk_decode_free_frame(hwctx, &hp->vp);
583}
584
586 .p.name = "h264_vulkan",
587 .p.type = AVMEDIA_TYPE_VIDEO,
588 .p.id = AV_CODEC_ID_H264,
589 .p.pix_fmt = AV_PIX_FMT_VULKAN,
590 .start_frame = &vk_h264_start_frame,
591 .decode_slice = &vk_h264_decode_slice,
592 .end_frame = &vk_h264_end_frame,
593 .free_frame_priv = &vk_h264_free_frame_priv,
594 .frame_priv_data_size = sizeof(H264VulkanDecodePicture),
596 .update_thread_context = &ff_vk_update_thread_context,
597 .decode_params = &ff_vk_params_invalidate,
599 .frame_params = &ff_vk_frame_params,
600 .priv_data_size = sizeof(FFVulkanDecodeContext),
601 .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
602};
static AVFormatContext * ctx
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define NULL
Definition coverity.c:32
uint64_t pps
Definition dovi_rpuenc.c:36
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_CODEC_ID_H264
Definition codec_id.h:77
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_SI
Switching Intra.
Definition avutil.h:282
@ H264_MAX_DPB_FRAMES
Definition h264.h:76
H.264 parameter set handling.
#define MAX_SPS_COUNT
Definition h264_ps.h:37
#define MAX_PPS_COUNT
Definition h264_ps.h:38
H.264 / AVC / MPEG-4 part10 codec.
#define H264_MAX_PICTURE_COUNT
Definition h264dec.h:47
#define FIELD_PICTURE(h)
Definition h264dec.h:65
#define HWACCEL_CAP_ASYNC_SAFE
Header providing the internals of AVHWAccel.
const struct FFHWAccel ff_h264_vulkan_hwaccel
#define r
Definition input.c:42
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define av_unused
Definition attributes.h:164
const uint8_t ff_zigzag_direct[64]
Definition mathtables.c:137
const uint8_t ff_zigzag_scan[16+1]
Definition mathtables.c:148
Memory handling functions.
#define PICT_TOP_FIELD
Definition mpegutils.h:31
#define PICT_BOTTOM_FIELD
Definition mpegutils.h:32
#define PICT_FRAME
Definition mpegutils.h:33
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition pixfmt.h:379
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
void * hwaccel_priv_data
hwaccel-specific private data
Definition internal.h:130
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int width
Definition frame.h:544
int height
Definition frame.h:544
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
VkVideoSessionParametersKHR * session_params
FFVulkanDecodeShared * shared_ctx
struct FFVulkanDecodePicture::@321376256110110067000314232201030232256240036361 view
VkVideoReferenceSlotInfoKHR ref_slot
VkVideoPictureResourceInfoKHR refs[36]
VkVideoReferenceSlotInfoKHR ref_slots[36]
VkVideoDecodeInfoKHR decode_info
H264Context.
Definition h264dec.h:338
const PPS * pps
Definition h264dec.h:156
int invalid_gap
Definition h264dec.h:152
int reference
Definition h264dec.h:150
int pic_id
pic_num (short -> no wrap version of pic_num, pic_num & max_pic_num; long -> long_pic_num)
Definition h264dec.h:137
int frame_num
frame_num (raw frame_num from slice header)
Definition h264dec.h:134
int long_ref
1->long term reference 0->short term reference
Definition h264dec.h:139
void * hwaccel_picture_private
RefStruct reference for hardware accelerator private data.
Definition h264dec.h:128
AVFrame * f
Definition h264dec.h:113
int field_poc[2]
top/bottom POC
Definition h264dec.h:132
VkVideoDecodeH264DpbSlotInfoKHR vkh264_refs[H264_MAX_PICTURE_COUNT]
Definition vulkan_h264.c:47
VkVideoDecodeH264PictureInfoKHR h264_pic_info
Definition vulkan_h264.c:51
StdVideoDecodeH264PictureInfo h264pic
Definition vulkan_h264.c:50
StdVideoDecodeH264ReferenceInfo h264_ref
Definition vulkan_h264.c:41
FFVulkanDecodePicture vp
Definition vulkan_h264.c:38
H264Picture * ref_src[H264_MAX_PICTURE_COUNT]
Definition vulkan_h264.c:45
VkVideoDecodeH264DpbSlotInfoKHR vkh264_ref
Definition vulkan_h264.c:42
StdVideoDecodeH264ReferenceInfo h264_refs[H264_MAX_PICTURE_COUNT]
Definition vulkan_h264.c:46
Picture parameter set.
Definition h264_ps.h:110
unsigned int sps_id
Definition h264_ps.h:112
unsigned int pps_id
Definition h264_ps.h:111
const SPS * sps
RefStruct reference.
Definition h264_ps.h:141
Sequence parameter set.
Definition h264_ps.h:44
StdVideoH264PictureParameterSet vkpps[MAX_PPS_COUNT]
StdVideoH264ScalingLists vksps_scaling[MAX_SPS_COUNT]
StdVideoH264ScalingLists vkpps_scaling[MAX_PPS_COUNT]
StdVideoH264SequenceParameterSetVui vksps_vui[MAX_SPS_COUNT]
StdVideoH264SequenceParameterSet vksps[MAX_SPS_COUNT]
StdVideoH264HrdParameters vksps_vui_header[MAX_SPS_COUNT]
#define av_free(p)
#define av_log(a,...)
int level_idc
Definition h264_levels.c:29
static int ref[MAX_W *MAX_W]
static char buffer[20]
Definition seek.c:32
int size
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition refstruct.h:58
int ff_vk_decode_frame(AVCodecContext *avctx, AVFrame *pic, FFVulkanDecodePicture *vp, AVFrame *rpic[], FFVulkanDecodePicture *rvkp[])
Decode a frame.
int ff_vk_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Synchronize the contexts between 2 threads.
int ff_vk_decode_init(AVCodecContext *avctx)
Initialize decoder.
int ff_vk_decode_create_params(VkVideoSessionParametersKHR **par_ref, void *logctx, FFVulkanDecodeShared *ctx, const VkVideoSessionParametersCreateInfoKHR *session_params_create)
Create VkVideoSessionParametersKHR wrapped in an AVBufferRef.
int ff_vk_params_invalidate(AVCodecContext *avctx, int t, const uint8_t *b, uint32_t s)
Removes current session parameters to recreate them.
int ff_vk_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Initialize hw_frames_ctx with the parameters needed to decode the stream using the parameters from av...
int ff_vk_decode_add_slice(AVCodecContext *avctx, FFVulkanDecodePicture *vp, const uint8_t *data, size_t size, int add_startcode, uint32_t *nb_slices, const uint32_t **offsets)
Add slice data to frame.
void ff_vk_decode_free_frame(AVHWDeviceContext *dev_ctx, FFVulkanDecodePicture *vp)
Free a frame and its state.
int ff_vk_decode_uninit(AVCodecContext *avctx)
Free decoder.
int ff_vk_decode_prepare_frame(FFVulkanDecodeContext *dec, AVFrame *pic, FFVulkanDecodePicture *vkpic, int is_current, int alloc_dpb)
Prepare a frame, creates the image view, and sets up the dpb fields.
#define FF_VK_EXT_VIDEO_DECODE_H264
#define FF_VK_EXT_VIDEO_MAINTENANCE_2
static StdVideoH264LevelIdc convert_to_vk_level_idc(int level_idc)
static void vk_h264_free_frame_priv(AVRefStructOpaque _hwctx, void *data)
static void set_sps(const SPS *sps, StdVideoH264ScalingLists *vksps_scaling, StdVideoH264HrdParameters *vksps_vui_header, StdVideoH264SequenceParameterSetVui *vksps_vui, StdVideoH264SequenceParameterSet *vksps)
static void set_pps(const PPS *pps, const SPS *sps, StdVideoH264ScalingLists *vkpps_scaling, StdVideoH264PictureParameterSet *vkpps)
static const int h264_scaling_list8_order[]
Definition vulkan_h264.c:54
static int vk_h264_fill_pict(AVCodecContext *avctx, H264Picture **ref_src, VkVideoReferenceSlotInfoKHR *ref_slot, VkVideoPictureResourceInfoKHR *ref, VkVideoDecodeH264DpbSlotInfoKHR *vkh264_ref, StdVideoDecodeH264ReferenceInfo *h264_ref, H264Picture *pic, int is_current, int is_field, int picture_structure, int dpb_slot_index)
Definition vulkan_h264.c:56
static int vk_h264_start_frame(AVCodecContext *avctx, av_unused const AVBufferRef *buffer_ref, av_unused const uint8_t *buffer, av_unused uint32_t size)
static int vk_h264_end_frame(AVCodecContext *avctx)
static int vk_h264_create_params(AVCodecContext *avctx, VkVideoSessionParametersKHR **buf)
const FFVulkanDecodeDescriptor ff_vk_dec_h264_desc
Definition vulkan_h264.c:26
static int vk_h264_decode_slice(AVCodecContext *avctx, const uint8_t *data, uint32_t size)