xref: /aosp_15_r20/external/mesa3d/src/vulkan/runtime/vk_video.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2021 Red Hat
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #ifndef VK_VIDEO_H
24 #define VK_VIDEO_H
25 
26 #include "vk_object.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 struct vk_video_h264_sps {
33    StdVideoH264SequenceParameterSet base;
34    int32_t offsets_for_ref_frame[256];
35    StdVideoH264ScalingLists scaling_lists;
36    StdVideoH264SequenceParameterSetVui vui;
37    StdVideoH264HrdParameters vui_hrd_parameters;
38 };
39 
40 struct vk_video_h264_pps {
41    StdVideoH264PictureParameterSet base;
42    StdVideoH264ScalingLists scaling_lists;
43 };
44 
45 struct vk_video_h265_vps {
46    StdVideoH265VideoParameterSet base;
47    StdVideoH265DecPicBufMgr dec_pic_buf_mgr;
48    StdVideoH265SubLayerHrdParameters hrd_parameters_nal;
49    StdVideoH265SubLayerHrdParameters hrd_parameters_vcl;
50    StdVideoH265HrdParameters hrd_parameters;
51    StdVideoH265ProfileTierLevel tier_level;
52 };
53 
54 struct vk_video_h265_sps {
55    StdVideoH265SequenceParameterSet base;
56    StdVideoH265ProfileTierLevel tier_level;
57    StdVideoH265DecPicBufMgr dec_pic_buf_mgr;
58    StdVideoH265ScalingLists scaling_lists;
59    StdVideoH265ShortTermRefPicSet short_term_ref_pic_set;
60    StdVideoH265LongTermRefPicsSps long_term_ref_pics_sps;
61    StdVideoH265SubLayerHrdParameters hrd_parameters_nal;
62    StdVideoH265SubLayerHrdParameters hrd_parameters_vcl;
63    StdVideoH265HrdParameters hrd_parameters;
64    StdVideoH265SequenceParameterSetVui vui;
65    StdVideoH265PredictorPaletteEntries palette_entries;
66 };
67 
68 struct vk_video_h265_pps {
69    StdVideoH265PictureParameterSet base;
70    StdVideoH265ScalingLists scaling_lists;
71    StdVideoH265PredictorPaletteEntries palette_entries;
72 };
73 
74 struct vk_video_av1_seq_hdr {
75    StdVideoAV1SequenceHeader base;
76    StdVideoAV1ColorConfig color_config;
77    StdVideoAV1TimingInfo timing_info;
78 };
79 
80 struct vk_video_session {
81    struct vk_object_base base;
82    VkVideoSessionCreateFlagsKHR flags;
83    VkVideoCodecOperationFlagsKHR op;
84    VkExtent2D max_coded;
85    VkFormat picture_format;
86    VkFormat ref_format;
87    uint32_t max_dpb_slots;
88    uint32_t max_active_ref_pics;
89 
90    struct {
91       VkVideoEncodeUsageFlagsKHR video_usage_hints;
92       VkVideoEncodeContentFlagsKHR video_content_hints;
93       VkVideoEncodeTuningModeKHR tuning_mode;
94    } enc_usage;
95    union {
96       struct {
97          StdVideoH264ProfileIdc profile_idc;
98       } h264;
99       struct {
100          StdVideoH265ProfileIdc profile_idc;
101       } h265;
102       struct {
103          StdVideoAV1Profile profile;
104          int film_grain_support;
105       } av1;
106    };
107 };
108 
109 struct vk_video_session_parameters {
110    struct vk_object_base base;
111    VkVideoCodecOperationFlagsKHR op;
112    union {
113       struct {
114          uint32_t max_h264_sps_count;
115          uint32_t max_h264_pps_count;
116 
117          uint32_t h264_sps_count;
118          struct vk_video_h264_sps *h264_sps;
119          uint32_t h264_pps_count;
120          struct vk_video_h264_pps *h264_pps;
121       } h264_dec;
122 
123       struct {
124          uint32_t max_h265_vps_count;
125          uint32_t max_h265_sps_count;
126          uint32_t max_h265_pps_count;
127 
128          uint32_t h265_vps_count;
129          struct vk_video_h265_vps *h265_vps;
130          uint32_t h265_sps_count;
131          struct vk_video_h265_sps *h265_sps;
132          uint32_t h265_pps_count;
133          struct vk_video_h265_pps *h265_pps;
134       } h265_dec;
135 
136       struct {
137          struct vk_video_av1_seq_hdr seq_hdr;
138       } av1_dec;
139 
140       struct {
141          uint32_t max_h264_sps_count;
142          uint32_t max_h264_pps_count;
143 
144          uint32_t h264_sps_count;
145          struct vk_video_h264_sps *h264_sps;
146          uint32_t h264_pps_count;
147          struct vk_video_h264_pps *h264_pps;
148          StdVideoH264ProfileIdc profile_idc;
149       } h264_enc;
150 
151       struct {
152          uint32_t max_h265_vps_count;
153          uint32_t max_h265_sps_count;
154          uint32_t max_h265_pps_count;
155 
156          uint32_t h265_vps_count;
157          struct vk_video_h265_vps *h265_vps;
158          uint32_t h265_sps_count;
159          struct vk_video_h265_sps *h265_sps;
160          uint32_t h265_pps_count;
161          struct vk_video_h265_pps *h265_pps;
162       } h265_enc;
163    };
164 };
165 
166 VkResult vk_video_session_init(struct vk_device *device,
167                                struct vk_video_session *vid,
168                                const VkVideoSessionCreateInfoKHR *create_info);
169 
170 VkResult vk_video_session_parameters_init(struct vk_device *device,
171                                           struct vk_video_session_parameters *params,
172                                           const struct vk_video_session *vid,
173                                           const struct vk_video_session_parameters *templ,
174                                           const VkVideoSessionParametersCreateInfoKHR *create_info);
175 
176 VkResult vk_video_session_parameters_update(struct vk_video_session_parameters *params,
177                                             const VkVideoSessionParametersUpdateInfoKHR *update);
178 
179 void vk_video_session_parameters_finish(struct vk_device *device,
180                                         struct vk_video_session_parameters *params);
181 
182 void vk_video_derive_h264_scaling_list(const StdVideoH264SequenceParameterSet *sps,
183                                        const StdVideoH264PictureParameterSet *pps,
184                                        StdVideoH264ScalingLists *list);
185 
186 const StdVideoH264SequenceParameterSet *
187 vk_video_find_h264_dec_std_sps(const struct vk_video_session_parameters *params,
188                                uint32_t id);
189 const StdVideoH264PictureParameterSet *
190 vk_video_find_h264_dec_std_pps(const struct vk_video_session_parameters *params,
191                                uint32_t id);
192 const StdVideoH265VideoParameterSet *
193 vk_video_find_h265_dec_std_vps(const struct vk_video_session_parameters *params,
194                                uint32_t id);
195 const StdVideoH265SequenceParameterSet *
196 vk_video_find_h265_dec_std_sps(const struct vk_video_session_parameters *params,
197                                uint32_t id);
198 const StdVideoH265PictureParameterSet *
199 vk_video_find_h265_dec_std_pps(const struct vk_video_session_parameters *params,
200                                uint32_t id);
201 
202 struct vk_video_h265_slice_params {
203    uint32_t slice_size;
204 
205    uint8_t first_slice_segment_in_pic_flag;
206    StdVideoH265SliceType slice_type;
207    uint8_t dependent_slice_segment;
208    uint8_t temporal_mvp_enable;
209    uint8_t loop_filter_across_slices_enable;
210    int32_t pic_order_cnt_lsb;
211    uint8_t sao_luma_flag;
212    uint8_t sao_chroma_flag;
213    uint8_t collocated_list;
214    uint32_t collocated_ref_idx;
215    uint8_t mvd_l1_zero_flag;
216 
217    uint8_t num_ref_idx_l0_active;
218    uint8_t num_ref_idx_l1_active;
219    uint8_t rpl_modification_flag[2];
220    uint8_t cabac_init_idc;
221    int8_t slice_qp_delta;
222    int8_t slice_cb_qp_offset;
223    int8_t slice_cr_qp_offset;
224    int8_t max_num_merge_cand;
225    uint32_t slice_data_bytes_offset;
226    uint8_t disable_deblocking_filter_idc;
227    int8_t tc_offset_div2;
228    int8_t beta_offset_div2;
229    uint32_t slice_segment_address;
230 
231    uint8_t luma_log2_weight_denom;
232    uint8_t chroma_log2_weight_denom;
233    uint8_t luma_weight_l0_flag[16];
234    int16_t luma_weight_l0[16];
235    int16_t luma_offset_l0[16];
236    uint8_t chroma_weight_l0_flag[16];
237    int16_t chroma_weight_l0[16][2];
238    int16_t chroma_offset_l0[16][2];
239    uint8_t luma_weight_l1_flag[16];
240    int16_t luma_weight_l1[16];
241    int16_t luma_offset_l1[16];
242    uint8_t chroma_weight_l1_flag[16];
243    int16_t chroma_weight_l1[16][2];
244    int16_t chroma_offset_l1[16][2];
245 
246    int8_t delta_luma_weight_l0[16];
247    int8_t delta_luma_weight_l1[16];
248    int8_t delta_chroma_weight_l0[16][2];
249    int8_t delta_chroma_weight_l1[16][2];
250    int16_t delta_chroma_offset_l0[16][2];
251    int16_t delta_chroma_offset_l1[16][2];
252 };
253 
254 void
255 vk_video_parse_h265_slice_header(const struct VkVideoDecodeInfoKHR *frame_info,
256                                  const VkVideoDecodeH265PictureInfoKHR *pic_info,
257                                  const StdVideoH265SequenceParameterSet *sps,
258                                  const StdVideoH265PictureParameterSet *pps,
259                                  void *slice_data,
260                                  uint32_t slice_size,
261                                  struct vk_video_h265_slice_params *params);
262 
263 
264 struct vk_video_h265_reference {
265    const VkVideoPictureResourceInfoKHR *pPictureResource;
266    StdVideoDecodeH265ReferenceInfoFlags flags;
267    uint32_t slot_index;
268    int32_t pic_order_cnt;
269 };
270 
271 int vk_video_h265_poc_by_slot(const struct VkVideoDecodeInfoKHR *frame_info, int slot);
272 
273 void vk_fill_video_h265_reference_info(const VkVideoDecodeInfoKHR *frame_info,
274                                        const struct VkVideoDecodeH265PictureInfoKHR *pic,
275                                        const struct vk_video_h265_slice_params *slice_params,
276                                        struct vk_video_h265_reference ref_slots[][8]);
277 
278 #define VK_VIDEO_H264_MACROBLOCK_WIDTH 16
279 #define VK_VIDEO_H264_MACROBLOCK_HEIGHT 16
280 
281 #define VK_VIDEO_H265_CTU_MAX_WIDTH 64
282 #define VK_VIDEO_H265_CTU_MAX_HEIGHT 64
283 
284 #define VK_VIDEO_AV1_BLOCK_WIDTH 128
285 #define VK_VIDEO_AV1_BLOCK_HEIGHT 128
286 
287 void
288 vk_video_get_profile_alignments(const VkVideoProfileListInfoKHR *profile_list,
289                                 uint32_t *width_align_out, uint32_t *height_align_out);
290 
291 uint8_t
292 vk_video_get_h264_level(StdVideoH264LevelIdc level);
293 
294 const StdVideoH264SequenceParameterSet *
295 vk_video_find_h264_enc_std_sps(const struct vk_video_session_parameters *params,
296                                uint32_t id);
297 const StdVideoH264PictureParameterSet *
298 vk_video_find_h264_enc_std_pps(const struct vk_video_session_parameters *params,
299                                uint32_t id);
300 
301 const StdVideoH265VideoParameterSet *
302 vk_video_find_h265_enc_std_vps(const struct vk_video_session_parameters *params,
303                                uint32_t id);
304 const StdVideoH265SequenceParameterSet *
305 vk_video_find_h265_enc_std_sps(const struct vk_video_session_parameters *params,
306                                uint32_t id);
307 const StdVideoH265PictureParameterSet *
308 vk_video_find_h265_enc_std_pps(const struct vk_video_session_parameters *params,
309                                uint32_t id);
310 
311 void
312 vk_video_encode_h264_sps(const StdVideoH264SequenceParameterSet *sps,
313                          size_t size_limit,
314                          size_t *data_size_ptr,
315                          void *data_ptr);
316 
317 void
318 vk_video_encode_h264_pps(const StdVideoH264PictureParameterSet *pps,
319                          bool high_profile,
320                          size_t size_limit,
321                          size_t *data_size_ptr,
322                          void *data_ptr);
323 
324 unsigned
325 vk_video_get_h265_nal_unit(const StdVideoEncodeH265PictureInfo *pic_info);
326 
327 void
328 vk_video_encode_h265_vps(const StdVideoH265VideoParameterSet *vps,
329                          size_t size_limit,
330                          size_t *data_size,
331                          void *data_ptr);
332 void
333 vk_video_encode_h265_sps(const StdVideoH265SequenceParameterSet *sps,
334                          size_t size_limit,
335                          size_t* pDataSize,
336                          void* pData);
337 
338 void
339 vk_video_encode_h265_pps(const StdVideoH265PictureParameterSet *pps,
340                          size_t size_limit,
341                          size_t *data_size,
342                          void *data_ptr);
343 
344 #ifdef __cplusplus
345 }
346 #endif
347 
348 void
349 vk_video_encode_h264_slice_header(const StdVideoEncodeH264PictureInfo *pic_info,
350                                   const StdVideoH264SequenceParameterSet *sps,
351                                   const StdVideoH264PictureParameterSet *pps,
352                                   const StdVideoEncodeH264SliceHeader *slice_header,
353                                   const int8_t slice_qp_delta,
354                                   size_t *data_size_ptr,
355                                   void *data_ptr);
356 
357 void
358 vk_video_encode_h265_slice_header(const StdVideoEncodeH265PictureInfo *pic_info,
359                                   const StdVideoH265VideoParameterSet *vps,
360                                   const StdVideoH265SequenceParameterSet *sps,
361                                   const StdVideoH265PictureParameterSet *pps,
362                                   const StdVideoEncodeH265SliceSegmentHeader *slice_header,
363                                   const int8_t slice_qp_delta,
364                                   size_t *data_size_ptr,
365                                   void *data_ptr);
366 #endif
367