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 } h264_enc; 149 150 struct { 151 uint32_t max_h265_vps_count; 152 uint32_t max_h265_sps_count; 153 uint32_t max_h265_pps_count; 154 155 uint32_t h265_vps_count; 156 struct vk_video_h265_vps *h265_vps; 157 uint32_t h265_sps_count; 158 struct vk_video_h265_sps *h265_sps; 159 uint32_t h265_pps_count; 160 struct vk_video_h265_pps *h265_pps; 161 } h265_enc; 162 }; 163 }; 164 165 VkResult vk_video_session_init(struct vk_device *device, 166 struct vk_video_session *vid, 167 const VkVideoSessionCreateInfoKHR *create_info); 168 169 VkResult vk_video_session_parameters_init(struct vk_device *device, 170 struct vk_video_session_parameters *params, 171 const struct vk_video_session *vid, 172 const struct vk_video_session_parameters *templ, 173 const VkVideoSessionParametersCreateInfoKHR *create_info); 174 175 VkResult vk_video_session_parameters_update(struct vk_video_session_parameters *params, 176 const VkVideoSessionParametersUpdateInfoKHR *update); 177 178 void vk_video_session_parameters_finish(struct vk_device *device, 179 struct vk_video_session_parameters *params); 180 181 void vk_video_derive_h264_scaling_list(const StdVideoH264SequenceParameterSet *sps, 182 const StdVideoH264PictureParameterSet *pps, 183 StdVideoH264ScalingLists *list); 184 185 const StdVideoH264SequenceParameterSet * 186 vk_video_find_h264_dec_std_sps(const struct vk_video_session_parameters *params, 187 uint32_t id); 188 const StdVideoH264PictureParameterSet * 189 vk_video_find_h264_dec_std_pps(const struct vk_video_session_parameters *params, 190 uint32_t id); 191 const StdVideoH265VideoParameterSet * 192 vk_video_find_h265_dec_std_vps(const struct vk_video_session_parameters *params, 193 uint32_t id); 194 const StdVideoH265SequenceParameterSet * 195 vk_video_find_h265_dec_std_sps(const struct vk_video_session_parameters *params, 196 uint32_t id); 197 const StdVideoH265PictureParameterSet * 198 vk_video_find_h265_dec_std_pps(const struct vk_video_session_parameters *params, 199 uint32_t id); 200 201 struct vk_video_h265_slice_params { 202 uint32_t slice_size; 203 204 uint8_t first_slice_segment_in_pic_flag; 205 StdVideoH265SliceType slice_type; 206 uint8_t dependent_slice_segment; 207 uint8_t temporal_mvp_enable; 208 uint8_t loop_filter_across_slices_enable; 209 int32_t pic_order_cnt_lsb; 210 uint8_t sao_luma_flag; 211 uint8_t sao_chroma_flag; 212 uint8_t collocated_list; 213 uint32_t collocated_ref_idx; 214 uint8_t mvd_l1_zero_flag; 215 216 uint8_t num_ref_idx_l0_active; 217 uint8_t num_ref_idx_l1_active; 218 uint8_t rpl_modification_flag[2]; 219 uint8_t cabac_init_idc; 220 int8_t slice_qp_delta; 221 int8_t slice_cb_qp_offset; 222 int8_t slice_cr_qp_offset; 223 int8_t max_num_merge_cand; 224 uint32_t slice_data_bytes_offset; 225 uint8_t disable_deblocking_filter_idc; 226 int8_t tc_offset_div2; 227 int8_t beta_offset_div2; 228 uint32_t slice_segment_address; 229 230 uint8_t luma_log2_weight_denom; 231 uint8_t chroma_log2_weight_denom; 232 uint8_t luma_weight_l0_flag[16]; 233 int16_t luma_weight_l0[16]; 234 int16_t luma_offset_l0[16]; 235 uint8_t chroma_weight_l0_flag[16]; 236 int16_t chroma_weight_l0[16][2]; 237 int16_t chroma_offset_l0[16][2]; 238 uint8_t luma_weight_l1_flag[16]; 239 int16_t luma_weight_l1[16]; 240 int16_t luma_offset_l1[16]; 241 uint8_t chroma_weight_l1_flag[16]; 242 int16_t chroma_weight_l1[16][2]; 243 int16_t chroma_offset_l1[16][2]; 244 245 int8_t delta_luma_weight_l0[16]; 246 int8_t delta_luma_weight_l1[16]; 247 int8_t delta_chroma_weight_l0[16][2]; 248 int8_t delta_chroma_weight_l1[16][2]; 249 int16_t delta_chroma_offset_l0[16][2]; 250 int16_t delta_chroma_offset_l1[16][2]; 251 }; 252 253 void 254 vk_video_parse_h265_slice_header(const struct VkVideoDecodeInfoKHR *frame_info, 255 const VkVideoDecodeH265PictureInfoKHR *pic_info, 256 const StdVideoH265SequenceParameterSet *sps, 257 const StdVideoH265PictureParameterSet *pps, 258 void *slice_data, 259 uint32_t slice_size, 260 struct vk_video_h265_slice_params *params); 261 262 263 struct vk_video_h265_reference { 264 const VkVideoPictureResourceInfoKHR *pPictureResource; 265 StdVideoDecodeH265ReferenceInfoFlags flags; 266 uint32_t slot_index; 267 int32_t pic_order_cnt; 268 }; 269 270 int vk_video_h265_poc_by_slot(const struct VkVideoDecodeInfoKHR *frame_info, int slot); 271 272 void vk_fill_video_h265_reference_info(const VkVideoDecodeInfoKHR *frame_info, 273 const struct VkVideoDecodeH265PictureInfoKHR *pic, 274 const struct vk_video_h265_slice_params *slice_params, 275 struct vk_video_h265_reference ref_slots[][8]); 276 277 #define VK_VIDEO_H264_MACROBLOCK_WIDTH 16 278 #define VK_VIDEO_H264_MACROBLOCK_HEIGHT 16 279 280 #define VK_VIDEO_H265_CTU_MAX_WIDTH 64 281 #define VK_VIDEO_H265_CTU_MAX_HEIGHT 64 282 283 #define VK_VIDEO_AV1_BLOCK_WIDTH 128 284 #define VK_VIDEO_AV1_BLOCK_HEIGHT 128 285 286 void 287 vk_video_get_profile_alignments(const VkVideoProfileListInfoKHR *profile_list, 288 uint32_t *width_align_out, uint32_t *height_align_out); 289 290 uint8_t 291 vk_video_get_h264_level(StdVideoH264LevelIdc level); 292 293 const StdVideoH264SequenceParameterSet * 294 vk_video_find_h264_enc_std_sps(const struct vk_video_session_parameters *params, 295 uint32_t id); 296 const StdVideoH264PictureParameterSet * 297 vk_video_find_h264_enc_std_pps(const struct vk_video_session_parameters *params, 298 uint32_t id); 299 300 const StdVideoH265VideoParameterSet * 301 vk_video_find_h265_enc_std_vps(const struct vk_video_session_parameters *params, 302 uint32_t id); 303 const StdVideoH265SequenceParameterSet * 304 vk_video_find_h265_enc_std_sps(const struct vk_video_session_parameters *params, 305 uint32_t id); 306 const StdVideoH265PictureParameterSet * 307 vk_video_find_h265_enc_std_pps(const struct vk_video_session_parameters *params, 308 uint32_t id); 309 310 void 311 vk_video_encode_h264_sps(const StdVideoH264SequenceParameterSet *sps, 312 size_t size_limit, 313 size_t *data_size_ptr, 314 void *data_ptr); 315 316 void 317 vk_video_encode_h264_pps(const StdVideoH264PictureParameterSet *pps, 318 bool high_profile, 319 size_t size_limit, 320 size_t *data_size_ptr, 321 void *data_ptr); 322 323 unsigned 324 vk_video_get_h265_nal_unit(StdVideoH265PictureType pic_type, bool irap_pic_flag); 325 326 void 327 vk_video_encode_h265_vps(const StdVideoH265VideoParameterSet *vps, 328 size_t size_limit, 329 size_t *data_size, 330 void *data_ptr); 331 void 332 vk_video_encode_h265_sps(const StdVideoH265SequenceParameterSet *sps, 333 size_t size_limit, 334 size_t* pDataSize, 335 void* pData); 336 337 void 338 vk_video_encode_h265_pps(const StdVideoH265PictureParameterSet *pps, 339 size_t size_limit, 340 size_t *data_size, 341 void *data_ptr); 342 343 #ifdef __cplusplus 344 } 345 #endif 346 347 #endif 348