2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef AVCODEC_VULKAN_DECODE_H
20 #define AVCODEC_VULKAN_DECODE_H
23 #include "hwaccel_internal.h"
26 #include "vulkan_video.h"
28 typedef struct FFVulkanDecodeProfileData {
29 VkVideoDecodeH264ProfileInfoKHR h264_profile;
30 VkVideoDecodeH265ProfileInfoKHR h265_profile;
31 VkVideoDecodeAV1ProfileInfoMESA av1_profile;
32 VkVideoDecodeUsageInfoKHR usage;
33 VkVideoProfileInfoKHR profile;
34 VkVideoProfileListInfoKHR profile_list;
35 } FFVulkanDecodeProfileData;
37 typedef struct FFVulkanDecodeShared {
39 FFVkVideoCommon common;
40 FFVkQueueFamilyCtx qf;
42 VkVideoCapabilitiesKHR caps;
43 VkVideoDecodeCapabilitiesKHR dec_caps;
45 AVBufferRef *dpb_hwfc_ref; /* Only used for dedicated_dpb */
47 AVFrame *layered_frame; /* Only used for layered_dpb */
48 VkImageView layered_view;
49 VkImageAspectFlags layered_aspect;
51 VkVideoSessionParametersKHR empty_session_params;
53 VkSamplerYcbcrConversion yuv_sampler;
54 } FFVulkanDecodeShared;
56 typedef struct FFVulkanDecodeContext {
57 FFVulkanDecodeShared *shared_ctx;
58 AVBufferRef *session_params;
59 FFVkExecPool exec_pool;
61 int dedicated_dpb; /* Oddity #1 - separate DPB images */
62 int layered_dpb; /* Madness #1 - layered DPB images */
63 int external_fg; /* Oddity #2 - hardware can't apply film grain */
64 uint32_t frame_id_alloc_mask; /* For AV1 only */
66 /* Thread-local state below */
67 struct HEVCHeaderSet *hevc_headers;
68 size_t hevc_headers_size;
71 unsigned int slice_off_max;
72 } FFVulkanDecodeContext;
74 typedef struct FFVulkanDecodePicture {
75 AVFrame *dpb_frame; /* Only used for out-of-place decoding. */
77 VkImageView img_view_ref; /* Image representation view (reference) */
78 VkImageView img_view_out; /* Image representation view (output-only) */
79 VkImageView img_view_dest; /* Set to img_view_out if no layered refs are used */
80 VkImageAspectFlags img_aspect; /* Image plane mask bits */
81 VkImageAspectFlags img_aspect_ref; /* Only used for out-of-place decoding */
87 VkVideoPictureResourceInfoKHR ref;
88 VkVideoReferenceSlotInfoKHR ref_slot;
90 /* Picture refs. H264 has the maximum number of refs (36) of any supported codec. */
91 VkVideoPictureResourceInfoKHR refs [36];
92 VkVideoReferenceSlotInfoKHR ref_slots[36];
94 /* Main decoding struct */
95 VkVideoDecodeInfoKHR decode_info;
98 AVBufferRef *slices_buf;
101 /* Vulkan functions needed for destruction, as no other context is guaranteed to exist */
102 PFN_vkWaitSemaphores wait_semaphores;
103 PFN_vkDestroyImageView destroy_image_view;
104 } FFVulkanDecodePicture;
107 * Initialize decoder.
109 int ff_vk_decode_init(AVCodecContext *avctx);
112 * Synchronize the contexts between 2 threads.
114 int ff_vk_update_thread_context(AVCodecContext *dst, const AVCodecContext *src);
117 * Initialize hw_frames_ctx with the parameters needed to decode the stream
118 * using the parameters from avctx.
120 * NOTE: if avctx->internal->hwaccel_priv_data exists, will partially initialize
123 int ff_vk_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx);
126 * Removes current session parameters to recreate them
128 int ff_vk_params_invalidate(AVCodecContext *avctx, int t, const uint8_t *b, uint32_t s);
131 * Prepare a frame, creates the image view, and sets up the dpb fields.
133 int ff_vk_decode_prepare_frame(FFVulkanDecodeContext *dec, AVFrame *pic,
134 FFVulkanDecodePicture *vkpic, int is_current,
138 * Add slice data to frame.
140 int ff_vk_decode_add_slice(AVCodecContext *avctx, FFVulkanDecodePicture *vp,
141 const uint8_t *data, size_t size, int add_startcode,
142 uint32_t *nb_slices, const uint32_t **offsets);
147 int ff_vk_decode_frame(AVCodecContext *avctx,
148 AVFrame *pic, FFVulkanDecodePicture *vp,
149 AVFrame *rpic[], FFVulkanDecodePicture *rvkp[]);
152 * Free a frame and its state.
154 void ff_vk_decode_free_frame(AVHWDeviceContext *dev_ctx, FFVulkanDecodePicture *vp);
157 * Get an FFVkBuffer suitable for decoding from.
159 int ff_vk_get_decode_buffer(FFVulkanDecodeContext *ctx, AVBufferRef **buf,
160 void *create_pNext, size_t size);
163 * Create VkVideoSessionParametersKHR wrapped in an AVBufferRef.
165 int ff_vk_decode_create_params(AVBufferRef **par_ref, void *logctx, FFVulkanDecodeShared *ctx,
166 const VkVideoSessionParametersCreateInfoKHR *session_params_create);
171 void ff_vk_decode_flush(AVCodecContext *avctx);
176 int ff_vk_decode_uninit(AVCodecContext *avctx);
178 #endif /* AVCODEC_VULKAN_DECODE_H */