Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavcodec / vulkan_decode.h
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 #ifndef AVCODEC_VULKAN_DECODE_H
20 #define AVCODEC_VULKAN_DECODE_H
21
22 #include "decode.h"
23 #include "hwaccel_internal.h"
24 #include "internal.h"
25
26 #include "vulkan_video.h"
27
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;
36
37 typedef struct FFVulkanDecodeShared {
38     FFVulkanContext s;
39     FFVkVideoCommon common;
40     FFVkQueueFamilyCtx qf;
41
42     VkVideoCapabilitiesKHR caps;
43     VkVideoDecodeCapabilitiesKHR dec_caps;
44
45     AVBufferRef *dpb_hwfc_ref;  /* Only used for dedicated_dpb */
46
47     AVFrame *layered_frame;     /* Only used for layered_dpb   */
48     VkImageView layered_view;
49     VkImageAspectFlags layered_aspect;
50
51     VkVideoSessionParametersKHR empty_session_params;
52
53     VkSamplerYcbcrConversion yuv_sampler;
54 } FFVulkanDecodeShared;
55
56 typedef struct FFVulkanDecodeContext {
57     FFVulkanDecodeShared *shared_ctx;
58     AVBufferRef *session_params;
59     FFVkExecPool exec_pool;
60
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 */
65
66     /* Thread-local state below */
67     struct HEVCHeaderSet *hevc_headers;
68     size_t hevc_headers_size;
69
70     uint32_t                       *slice_off;
71     unsigned int                    slice_off_max;
72 } FFVulkanDecodeContext;
73
74 typedef struct FFVulkanDecodePicture {
75     AVFrame                        *dpb_frame;      /* Only used for out-of-place decoding. */
76
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 */
82
83     VkSemaphore                     sem;
84     uint64_t                        sem_value;
85
86     /* Current picture */
87     VkVideoPictureResourceInfoKHR   ref;
88     VkVideoReferenceSlotInfoKHR     ref_slot;
89
90     /* Picture refs. H264 has the maximum number of refs (36) of any supported codec. */
91     VkVideoPictureResourceInfoKHR   refs     [36];
92     VkVideoReferenceSlotInfoKHR     ref_slots[36];
93
94     /* Main decoding struct */
95     VkVideoDecodeInfoKHR            decode_info;
96
97     /* Slice data */
98     AVBufferRef                    *slices_buf;
99     size_t                          slices_size;
100
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;
105
106 /**
107  * Initialize decoder.
108  */
109 int ff_vk_decode_init(AVCodecContext *avctx);
110
111 /**
112  * Synchronize the contexts between 2 threads.
113  */
114 int ff_vk_update_thread_context(AVCodecContext *dst, const AVCodecContext *src);
115
116 /**
117  * Initialize hw_frames_ctx with the parameters needed to decode the stream
118  * using the parameters from avctx.
119  *
120  * NOTE: if avctx->internal->hwaccel_priv_data exists, will partially initialize
121  * the context.
122  */
123 int ff_vk_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx);
124
125 /**
126  * Removes current session parameters to recreate them
127  */
128 int ff_vk_params_invalidate(AVCodecContext *avctx, int t, const uint8_t *b, uint32_t s);
129
130 /**
131  * Prepare a frame, creates the image view, and sets up the dpb fields.
132  */
133 int ff_vk_decode_prepare_frame(FFVulkanDecodeContext *dec, AVFrame *pic,
134                                FFVulkanDecodePicture *vkpic, int is_current,
135                                int alloc_dpb);
136
137 /**
138  * Add slice data to frame.
139  */
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);
143
144 /**
145  * Decode a frame.
146  */
147 int ff_vk_decode_frame(AVCodecContext *avctx,
148                        AVFrame *pic,    FFVulkanDecodePicture *vp,
149                        AVFrame *rpic[], FFVulkanDecodePicture *rvkp[]);
150
151 /**
152  * Free a frame and its state.
153  */
154 void ff_vk_decode_free_frame(AVHWDeviceContext *dev_ctx, FFVulkanDecodePicture *vp);
155
156 /**
157  * Get an FFVkBuffer suitable for decoding from.
158  */
159 int ff_vk_get_decode_buffer(FFVulkanDecodeContext *ctx, AVBufferRef **buf,
160                             void *create_pNext, size_t size);
161
162 /**
163  * Create VkVideoSessionParametersKHR wrapped in an AVBufferRef.
164  */
165 int ff_vk_decode_create_params(AVBufferRef **par_ref, void *logctx, FFVulkanDecodeShared *ctx,
166                                const VkVideoSessionParametersCreateInfoKHR *session_params_create);
167
168 /**
169  * Flush decoder.
170  */
171 void ff_vk_decode_flush(AVCodecContext *avctx);
172
173 /**
174  * Free decoder.
175  */
176 int ff_vk_decode_uninit(AVCodecContext *avctx);
177
178 #endif /* AVCODEC_VULKAN_DECODE_H */