libavcodec/vulkan_encode_h265.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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/internal.h" | ||
| 20 | #include "libavutil/opt.h" | ||
| 21 | #include "libavutil/mem.h" | ||
| 22 | |||
| 23 | #include "cbs.h" | ||
| 24 | #include "cbs_h265.h" | ||
| 25 | #include "atsc_a53.h" | ||
| 26 | #include "libavutil/mastering_display_metadata.h" | ||
| 27 | |||
| 28 | #include "codec_internal.h" | ||
| 29 | #include "version.h" | ||
| 30 | #include "hw_base_encode_h265.h" | ||
| 31 | |||
| 32 | #include "vulkan_encode.h" | ||
| 33 | |||
| 34 | enum UnitElems { | ||
| 35 | UNIT_AUD = 1 << 0, | ||
| 36 | UNIT_SEI_MASTERING_DISPLAY = 1 << 1, | ||
| 37 | UNIT_SEI_CONTENT_LIGHT_LEVEL = 1 << 2, | ||
| 38 | UNIT_SEI_A53_CC = 1 << 3, | ||
| 39 | }; | ||
| 40 | |||
| 41 | const FFVulkanEncodeDescriptor ff_vk_enc_h265_desc = { | ||
| 42 | .codec_id = AV_CODEC_ID_H265, | ||
| 43 | .encode_extension = FF_VK_EXT_VIDEO_ENCODE_H265, | ||
| 44 | .encode_op = VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR, | ||
| 45 | .ext_props = { | ||
| 46 | .extensionName = VK_STD_VULKAN_VIDEO_CODEC_H265_ENCODE_EXTENSION_NAME, | ||
| 47 | .specVersion = VK_STD_VULKAN_VIDEO_CODEC_H265_ENCODE_SPEC_VERSION, | ||
| 48 | }, | ||
| 49 | }; | ||
| 50 | |||
| 51 | typedef struct VulkanEncodeH265Picture { | ||
| 52 | int frame_num; | ||
| 53 | int64_t last_idr_frame; | ||
| 54 | uint16_t idr_pic_id; | ||
| 55 | int primary_pic_type; | ||
| 56 | int slice_type; | ||
| 57 | int pic_order_cnt; | ||
| 58 | int pic_type; | ||
| 59 | |||
| 60 | enum UnitElems units_needed; | ||
| 61 | |||
| 62 | VkVideoEncodeH265RateControlInfoKHR vkrc_info; | ||
| 63 | VkVideoEncodeH265RateControlLayerInfoKHR vkrc_layer_info; | ||
| 64 | |||
| 65 | StdVideoEncodeH265PictureInfo h265pic_info; | ||
| 66 | VkVideoEncodeH265PictureInfoKHR vkh265pic_info; | ||
| 67 | |||
| 68 | StdVideoEncodeH265WeightTable slice_wt; | ||
| 69 | StdVideoEncodeH265SliceSegmentHeader slice_hdr; | ||
| 70 | VkVideoEncodeH265NaluSliceSegmentInfoKHR vkslice; | ||
| 71 | |||
| 72 | StdVideoEncodeH265ReferenceInfo h265dpb_info; | ||
| 73 | VkVideoEncodeH265DpbSlotInfoKHR vkh265dpb_info; | ||
| 74 | |||
| 75 | StdVideoEncodeH265ReferenceListsInfo ref_list_info; | ||
| 76 | StdVideoEncodeH265LongTermRefPics l_rps; | ||
| 77 | StdVideoH265ShortTermRefPicSet s_rps; | ||
| 78 | } VulkanEncodeH265Picture; | ||
| 79 | |||
| 80 | typedef struct VulkanEncodeH265Context { | ||
| 81 | FFVulkanEncodeContext common; | ||
| 82 | |||
| 83 | FFHWBaseEncodeH265 units; | ||
| 84 | FFHWBaseEncodeH265Opts unit_opts; | ||
| 85 | |||
| 86 | enum UnitElems unit_elems; | ||
| 87 | |||
| 88 | uint8_t fixed_qp_idr; | ||
| 89 | uint8_t fixed_qp_p; | ||
| 90 | uint8_t fixed_qp_b; | ||
| 91 | |||
| 92 | uint64_t hrd_buffer_size; | ||
| 93 | uint64_t initial_buffer_fullness; | ||
| 94 | |||
| 95 | VkVideoEncodeH265ProfileInfoKHR profile; | ||
| 96 | |||
| 97 | VkVideoEncodeH265CapabilitiesKHR caps; | ||
| 98 | VkVideoEncodeH265QualityLevelPropertiesKHR quality_props; | ||
| 99 | |||
| 100 | CodedBitstreamContext *cbs; | ||
| 101 | CodedBitstreamFragment current_access_unit; | ||
| 102 | |||
| 103 | H265RawAUD raw_aud; | ||
| 104 | |||
| 105 | SEIRawMasteringDisplayColourVolume sei_mastering_display; | ||
| 106 | SEIRawContentLightLevelInfo sei_content_light_level; | ||
| 107 | SEIRawUserDataRegistered sei_a53cc; | ||
| 108 | void *sei_a53cc_data; | ||
| 109 | } VulkanEncodeH265Context; | ||
| 110 | |||
| 111 | ✗ | static int init_pic_rc(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, | |
| 112 | VkVideoEncodeRateControlInfoKHR *rc_info, | ||
| 113 | VkVideoEncodeRateControlLayerInfoKHR *rc_layer) | ||
| 114 | { | ||
| 115 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 116 | ✗ | FFVulkanEncodeContext *ctx = &enc->common; | |
| 117 | ✗ | VulkanEncodeH265Picture *hp = pic->codec_priv; | |
| 118 | |||
| 119 | ✗ | hp->vkrc_info = (VkVideoEncodeH265RateControlInfoKHR) { | |
| 120 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_RATE_CONTROL_INFO_KHR, | ||
| 121 | .flags = VK_VIDEO_ENCODE_H265_RATE_CONTROL_REFERENCE_PATTERN_FLAT_BIT_KHR | | ||
| 122 | VK_VIDEO_ENCODE_H265_RATE_CONTROL_REGULAR_GOP_BIT_KHR, | ||
| 123 | ✗ | .idrPeriod = ctx->base.gop_size, | |
| 124 | ✗ | .gopFrameCount = ctx->base.gop_size, | |
| 125 | ✗ | .consecutiveBFrameCount = FFMAX(ctx->base.b_per_p - 1, 0), | |
| 126 | .subLayerCount = 0, | ||
| 127 | }; | ||
| 128 | ✗ | rc_info->pNext = &hp->vkrc_info; | |
| 129 | |||
| 130 | ✗ | if (rc_info->rateControlMode > VK_VIDEO_ENCODE_RATE_CONTROL_MODE_DISABLED_BIT_KHR) { | |
| 131 | ✗ | rc_info->virtualBufferSizeInMs = (enc->hrd_buffer_size * 1000LL) / avctx->bit_rate; | |
| 132 | ✗ | rc_info->initialVirtualBufferSizeInMs = (enc->initial_buffer_fullness * 1000LL) / avctx->bit_rate; | |
| 133 | |||
| 134 | ✗ | hp->vkrc_layer_info = (VkVideoEncodeH265RateControlLayerInfoKHR) { | |
| 135 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_RATE_CONTROL_LAYER_INFO_KHR, | ||
| 136 | |||
| 137 | ✗ | .useMinQp = avctx->qmin > 0, | |
| 138 | ✗ | .minQp.qpI = avctx->qmin > 0 ? avctx->qmin : 0, | |
| 139 | ✗ | .minQp.qpP = avctx->qmin > 0 ? avctx->qmin : 0, | |
| 140 | ✗ | .minQp.qpB = avctx->qmin > 0 ? avctx->qmin : 0, | |
| 141 | |||
| 142 | ✗ | .useMaxQp = avctx->qmax > 0, | |
| 143 | ✗ | .maxQp.qpI = avctx->qmax > 0 ? avctx->qmax : 0, | |
| 144 | ✗ | .maxQp.qpP = avctx->qmax > 0 ? avctx->qmax : 0, | |
| 145 | ✗ | .maxQp.qpB = avctx->qmax > 0 ? avctx->qmax : 0, | |
| 146 | |||
| 147 | .useMaxFrameSize = 0, | ||
| 148 | }; | ||
| 149 | ✗ | rc_layer->pNext = &hp->vkrc_layer_info; | |
| 150 | ✗ | hp->vkrc_info.subLayerCount = 1; | |
| 151 | } | ||
| 152 | |||
| 153 | ✗ | return 0; | |
| 154 | } | ||
| 155 | |||
| 156 | ✗ | static int vk_enc_h265_update_pic_info(AVCodecContext *avctx, | |
| 157 | FFHWBaseEncodePicture *pic) | ||
| 158 | { | ||
| 159 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 160 | ✗ | VulkanEncodeH265Picture *hp = pic->codec_priv; | |
| 161 | ✗ | FFHWBaseEncodePicture *prev = pic->prev; | |
| 162 | ✗ | VulkanEncodeH265Picture *hprev = prev ? prev->codec_priv : NULL; | |
| 163 | |||
| 164 | ✗ | if (pic->type == FF_HW_PICTURE_TYPE_IDR) { | |
| 165 | ✗ | av_assert0(pic->display_order == pic->encode_order); | |
| 166 | |||
| 167 | ✗ | hp->last_idr_frame = pic->display_order; | |
| 168 | |||
| 169 | ✗ | hp->slice_type = STD_VIDEO_H265_SLICE_TYPE_I; | |
| 170 | ✗ | hp->pic_type = STD_VIDEO_H265_PICTURE_TYPE_IDR; | |
| 171 | } else { | ||
| 172 | ✗ | av_assert0(prev); | |
| 173 | ✗ | hp->last_idr_frame = hprev->last_idr_frame; | |
| 174 | |||
| 175 | ✗ | if (pic->type == FF_HW_PICTURE_TYPE_I) { | |
| 176 | ✗ | hp->slice_type = STD_VIDEO_H265_SLICE_TYPE_I; | |
| 177 | ✗ | hp->pic_type = STD_VIDEO_H265_PICTURE_TYPE_I; | |
| 178 | ✗ | } else if (pic->type == FF_HW_PICTURE_TYPE_P) { | |
| 179 | ✗ | av_assert0(pic->refs[0]); | |
| 180 | ✗ | hp->slice_type = STD_VIDEO_H265_SLICE_TYPE_P; | |
| 181 | ✗ | hp->pic_type = STD_VIDEO_H265_PICTURE_TYPE_P; | |
| 182 | } else { | ||
| 183 | FFHWBaseEncodePicture *irap_ref; | ||
| 184 | ✗ | av_assert0(pic->refs[0][0] && pic->refs[1][0]); | |
| 185 | ✗ | for (irap_ref = pic; irap_ref; irap_ref = irap_ref->refs[1][0]) { | |
| 186 | ✗ | if (irap_ref->type == FF_HW_PICTURE_TYPE_I) | |
| 187 | ✗ | break; | |
| 188 | } | ||
| 189 | ✗ | hp->slice_type = STD_VIDEO_H265_SLICE_TYPE_B; | |
| 190 | ✗ | hp->pic_type = STD_VIDEO_H265_PICTURE_TYPE_B; | |
| 191 | } | ||
| 192 | } | ||
| 193 | ✗ | hp->pic_order_cnt = pic->display_order - hp->last_idr_frame; | |
| 194 | |||
| 195 | ✗ | hp->units_needed = 0; | |
| 196 | |||
| 197 | ✗ | if (enc->unit_elems & UNIT_AUD) { | |
| 198 | ✗ | hp->units_needed |= UNIT_AUD; | |
| 199 | ✗ | enc->raw_aud = (H265RawAUD) { | |
| 200 | .nal_unit_header = { | ||
| 201 | .nal_unit_type = HEVC_NAL_AUD, | ||
| 202 | .nuh_layer_id = 0, | ||
| 203 | .nuh_temporal_id_plus1 = 1, | ||
| 204 | }, | ||
| 205 | ✗ | .pic_type = hp->pic_type, | |
| 206 | }; | ||
| 207 | } | ||
| 208 | |||
| 209 | // Only look for the metadata on I/IDR frame on the output. We | ||
| 210 | // may force an IDR frame on the output where the metadata gets | ||
| 211 | // changed on the input frame. | ||
| 212 | ✗ | if ((enc->unit_elems & UNIT_SEI_MASTERING_DISPLAY) && | |
| 213 | ✗ | (pic->type == FF_HW_PICTURE_TYPE_I || pic->type == FF_HW_PICTURE_TYPE_IDR)) { | |
| 214 | AVFrameSideData *sd = | ||
| 215 | ✗ | av_frame_get_side_data(pic->input_image, | |
| 216 | AV_FRAME_DATA_MASTERING_DISPLAY_METADATA); | ||
| 217 | |||
| 218 | ✗ | if (sd) { | |
| 219 | ✗ | AVMasteringDisplayMetadata *mdm = (AVMasteringDisplayMetadata *)sd->data; | |
| 220 | |||
| 221 | // SEI is needed when both the primaries and luminance are set | ||
| 222 | ✗ | if (mdm->has_primaries && mdm->has_luminance) { | |
| 223 | ✗ | SEIRawMasteringDisplayColourVolume *mdcv = | |
| 224 | &enc->sei_mastering_display; | ||
| 225 | ✗ | const int mapping[3] = {1, 2, 0}; | |
| 226 | ✗ | const int chroma_den = 50000; | |
| 227 | ✗ | const int luma_den = 10000; | |
| 228 | |||
| 229 | ✗ | for (int i = 0; i < 3; i++) { | |
| 230 | ✗ | const int j = mapping[i]; | |
| 231 | ✗ | mdcv->display_primaries_x[i] = | |
| 232 | ✗ | FFMIN(lrint(chroma_den * | |
| 233 | av_q2d(mdm->display_primaries[j][0])), | ||
| 234 | chroma_den); | ||
| 235 | ✗ | mdcv->display_primaries_y[i] = | |
| 236 | ✗ | FFMIN(lrint(chroma_den * | |
| 237 | av_q2d(mdm->display_primaries[j][1])), | ||
| 238 | chroma_den); | ||
| 239 | } | ||
| 240 | |||
| 241 | ✗ | mdcv->white_point_x = | |
| 242 | ✗ | FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])), | |
| 243 | chroma_den); | ||
| 244 | ✗ | mdcv->white_point_y = | |
| 245 | ✗ | FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])), | |
| 246 | chroma_den); | ||
| 247 | |||
| 248 | ✗ | mdcv->max_display_mastering_luminance = | |
| 249 | ✗ | lrint(luma_den * av_q2d(mdm->max_luminance)); | |
| 250 | ✗ | mdcv->min_display_mastering_luminance = | |
| 251 | ✗ | FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)), | |
| 252 | mdcv->max_display_mastering_luminance); | ||
| 253 | |||
| 254 | ✗ | hp->units_needed |= UNIT_SEI_MASTERING_DISPLAY; | |
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | ✗ | if ((enc->unit_elems & UNIT_SEI_CONTENT_LIGHT_LEVEL) && | |
| 260 | ✗ | (pic->type == FF_HW_PICTURE_TYPE_I || pic->type == FF_HW_PICTURE_TYPE_IDR)) { | |
| 261 | ✗ | AVFrameSideData *sd = av_frame_get_side_data(pic->input_image, | |
| 262 | AV_FRAME_DATA_CONTENT_LIGHT_LEVEL); | ||
| 263 | |||
| 264 | ✗ | if (sd) { | |
| 265 | ✗ | AVContentLightMetadata *clm = (AVContentLightMetadata *)sd->data; | |
| 266 | ✗ | SEIRawContentLightLevelInfo *clli = &enc->sei_content_light_level; | |
| 267 | |||
| 268 | ✗ | clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535); | |
| 269 | ✗ | clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535); | |
| 270 | |||
| 271 | ✗ | hp->units_needed |= UNIT_SEI_CONTENT_LIGHT_LEVEL; | |
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | ✗ | if (enc->unit_elems & UNIT_SEI_A53_CC) { | |
| 276 | int err; | ||
| 277 | size_t sei_a53cc_len; | ||
| 278 | ✗ | av_freep(&enc->sei_a53cc_data); | |
| 279 | ✗ | err = ff_alloc_a53_sei(pic->input_image, 0, &enc->sei_a53cc_data, &sei_a53cc_len); | |
| 280 | ✗ | if (err < 0) | |
| 281 | ✗ | return err; | |
| 282 | ✗ | if (enc->sei_a53cc_data != NULL) { | |
| 283 | ✗ | enc->sei_a53cc.itu_t_t35_country_code = 181; | |
| 284 | ✗ | enc->sei_a53cc.data = (uint8_t *)enc->sei_a53cc_data + 1; | |
| 285 | ✗ | enc->sei_a53cc.data_length = sei_a53cc_len - 1; | |
| 286 | |||
| 287 | ✗ | hp->units_needed |= UNIT_SEI_A53_CC; | |
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | ✗ | return 0; | |
| 292 | } | ||
| 293 | |||
| 294 | ✗ | static void setup_slices(AVCodecContext *avctx, | |
| 295 | FFHWBaseEncodePicture *pic) | ||
| 296 | { | ||
| 297 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 298 | ✗ | VulkanEncodeH265Picture *hp = pic->codec_priv; | |
| 299 | |||
| 300 | ✗ | hp->slice_wt = (StdVideoEncodeH265WeightTable) { | |
| 301 | .flags = (StdVideoEncodeH265WeightTableFlags) { | ||
| 302 | .luma_weight_l0_flag = 0, | ||
| 303 | .chroma_weight_l0_flag = 0, | ||
| 304 | .luma_weight_l1_flag = 0, | ||
| 305 | .chroma_weight_l1_flag = 0, | ||
| 306 | }, | ||
| 307 | .luma_log2_weight_denom = 0, | ||
| 308 | .delta_chroma_log2_weight_denom = 0, | ||
| 309 | .delta_luma_weight_l0 = { 0 }, | ||
| 310 | .luma_offset_l0 = { 0 }, | ||
| 311 | .delta_chroma_weight_l0 = { { 0 } }, | ||
| 312 | .delta_chroma_offset_l0 = { { 0 } }, | ||
| 313 | .delta_luma_weight_l1 = { 0 }, | ||
| 314 | .luma_offset_l1 = { 0 }, | ||
| 315 | .delta_chroma_weight_l1 = { { 0 } }, | ||
| 316 | .delta_chroma_offset_l1 = { { 0 } }, | ||
| 317 | }; | ||
| 318 | |||
| 319 | ✗ | hp->slice_hdr = (StdVideoEncodeH265SliceSegmentHeader) { | |
| 320 | .flags = (StdVideoEncodeH265SliceSegmentHeaderFlags) { | ||
| 321 | .first_slice_segment_in_pic_flag = 1, | ||
| 322 | .dependent_slice_segment_flag = 0, | ||
| 323 | ✗ | .slice_sao_luma_flag = enc->units.raw_sps.sample_adaptive_offset_enabled_flag, | |
| 324 | ✗ | .slice_sao_chroma_flag = enc->units.raw_sps.sample_adaptive_offset_enabled_flag, | |
| 325 | .num_ref_idx_active_override_flag = 0, | ||
| 326 | .mvd_l1_zero_flag = 0, | ||
| 327 | .cabac_init_flag = 0, | ||
| 328 | .cu_chroma_qp_offset_enabled_flag = 0, | ||
| 329 | .deblocking_filter_override_flag = 0, | ||
| 330 | .slice_deblocking_filter_disabled_flag = 0, | ||
| 331 | .collocated_from_l0_flag = 1, | ||
| 332 | .slice_loop_filter_across_slices_enabled_flag = 0, | ||
| 333 | /* Reserved */ | ||
| 334 | }, | ||
| 335 | ✗ | .slice_type = hp->slice_type, | |
| 336 | .slice_segment_address = 0, | ||
| 337 | .collocated_ref_idx = 0, | ||
| 338 | .MaxNumMergeCand = 5, | ||
| 339 | .slice_cb_qp_offset = 0, | ||
| 340 | .slice_cr_qp_offset = 0, | ||
| 341 | .slice_beta_offset_div2 = 0, | ||
| 342 | .slice_tc_offset_div2 = 0, | ||
| 343 | .slice_act_y_qp_offset = 0, | ||
| 344 | .slice_act_cb_qp_offset = 0, | ||
| 345 | .slice_act_cr_qp_offset = 0, | ||
| 346 | .slice_qp_delta = 0, /* Filled in below */ | ||
| 347 | /* Reserved */ | ||
| 348 | .pWeightTable = NULL, // &hp->slice_wt, | ||
| 349 | }; | ||
| 350 | |||
| 351 | ✗ | hp->vkslice = (VkVideoEncodeH265NaluSliceSegmentInfoKHR) { | |
| 352 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_NALU_SLICE_SEGMENT_INFO_KHR, | ||
| 353 | .pNext = NULL, | ||
| 354 | ✗ | .constantQp = pic->type == FF_HW_PICTURE_TYPE_B ? enc->fixed_qp_b : | |
| 355 | ✗ | pic->type == FF_HW_PICTURE_TYPE_P ? enc->fixed_qp_p : | |
| 356 | ✗ | enc->fixed_qp_idr, | |
| 357 | ✗ | .pStdSliceSegmentHeader = &hp->slice_hdr, | |
| 358 | }; | ||
| 359 | |||
| 360 | ✗ | if (enc->common.opts.rc_mode != VK_VIDEO_ENCODE_RATE_CONTROL_MODE_DISABLED_BIT_KHR) | |
| 361 | ✗ | hp->vkslice.constantQp = 0; | |
| 362 | |||
| 363 | ✗ | hp->slice_hdr.slice_qp_delta = hp->vkslice.constantQp - | |
| 364 | ✗ | (enc->units.raw_pps.init_qp_minus26 + 26); | |
| 365 | |||
| 366 | ✗ | hp->vkh265pic_info.pNaluSliceSegmentEntries = &hp->vkslice; | |
| 367 | ✗ | hp->vkh265pic_info.naluSliceSegmentEntryCount = 1; | |
| 368 | ✗ | } | |
| 369 | |||
| 370 | ✗ | static void setup_refs(AVCodecContext *avctx, | |
| 371 | FFHWBaseEncodePicture *pic, | ||
| 372 | VkVideoEncodeInfoKHR *encode_info) | ||
| 373 | { | ||
| 374 | int i, j; | ||
| 375 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 376 | ✗ | VulkanEncodeH265Picture *hp = pic->codec_priv; | |
| 377 | |||
| 378 | ✗ | hp->ref_list_info = (StdVideoEncodeH265ReferenceListsInfo) { | |
| 379 | .flags = (StdVideoEncodeH265ReferenceListsInfoFlags) { | ||
| 380 | .ref_pic_list_modification_flag_l0 = 0, | ||
| 381 | .ref_pic_list_modification_flag_l1 = 0, | ||
| 382 | /* Reserved */ | ||
| 383 | }, | ||
| 384 | /* May be overridden during setup_slices() */ | ||
| 385 | ✗ | .num_ref_idx_l0_active_minus1 = pic->nb_refs[0] - 1, | |
| 386 | ✗ | .num_ref_idx_l1_active_minus1 = pic->nb_refs[1] - 1, | |
| 387 | /* Reserved */ | ||
| 388 | .list_entry_l0 = { 0 }, | ||
| 389 | .list_entry_l1 = { 0 }, | ||
| 390 | }; | ||
| 391 | |||
| 392 | ✗ | for (i = 0; i < STD_VIDEO_H265_MAX_NUM_LIST_REF; i++) | |
| 393 | ✗ | hp->ref_list_info.RefPicList0[i] = hp->ref_list_info.RefPicList1[i] = -1; | |
| 394 | |||
| 395 | /* Note: really not sure */ | ||
| 396 | ✗ | for (i = 0; i < pic->nb_refs[0]; i++) { | |
| 397 | VkVideoReferenceSlotInfoKHR *slot_info; | ||
| 398 | ✗ | slot_info = (VkVideoReferenceSlotInfoKHR *)&encode_info->pReferenceSlots[i]; | |
| 399 | ✗ | hp->ref_list_info.RefPicList0[i] = slot_info->slotIndex; | |
| 400 | } | ||
| 401 | |||
| 402 | /* Note: really not sure */ | ||
| 403 | ✗ | for (i = 0; i < pic->nb_refs[1]; i++) { | |
| 404 | VkVideoReferenceSlotInfoKHR *slot_info; | ||
| 405 | ✗ | slot_info = (VkVideoReferenceSlotInfoKHR *)&encode_info->pReferenceSlots[pic->nb_refs[0] + i]; | |
| 406 | ✗ | hp->ref_list_info.RefPicList1[i] = slot_info->slotIndex; | |
| 407 | } | ||
| 408 | |||
| 409 | ✗ | hp->h265pic_info.pRefLists = &hp->ref_list_info; | |
| 410 | |||
| 411 | ✗ | if (pic->type != FF_HW_PICTURE_TYPE_IDR) { | |
| 412 | StdVideoH265ShortTermRefPicSet *rps; | ||
| 413 | VulkanEncodeH265Picture *strp; | ||
| 414 | int rps_poc[MAX_DPB_SIZE]; | ||
| 415 | int rps_used[MAX_DPB_SIZE]; | ||
| 416 | int poc, rps_pics; | ||
| 417 | |||
| 418 | ✗ | hp->h265pic_info.flags.short_term_ref_pic_set_sps_flag = 0; | |
| 419 | |||
| 420 | ✗ | rps = &hp->s_rps; | |
| 421 | ✗ | memset(rps, 0, sizeof(*rps)); | |
| 422 | |||
| 423 | ✗ | rps_pics = 0; | |
| 424 | ✗ | for (i = 0; i < MAX_REFERENCE_LIST_NUM; i++) { | |
| 425 | ✗ | for (j = 0; j < pic->nb_refs[i]; j++) { | |
| 426 | ✗ | strp = pic->refs[i][j]->codec_priv; | |
| 427 | ✗ | rps_poc[rps_pics] = strp->pic_order_cnt; | |
| 428 | ✗ | rps_used[rps_pics] = 1; | |
| 429 | ✗ | ++rps_pics; | |
| 430 | } | ||
| 431 | } | ||
| 432 | |||
| 433 | ✗ | for (i = 0; i < pic->nb_dpb_pics; i++) { | |
| 434 | ✗ | if (pic->dpb[i] == pic) | |
| 435 | ✗ | continue; | |
| 436 | |||
| 437 | ✗ | for (j = 0; j < pic->nb_refs[0]; j++) { | |
| 438 | ✗ | if (pic->dpb[i] == pic->refs[0][j]) | |
| 439 | ✗ | break; | |
| 440 | } | ||
| 441 | ✗ | if (j < pic->nb_refs[0]) | |
| 442 | ✗ | continue; | |
| 443 | |||
| 444 | ✗ | for (j = 0; j < pic->nb_refs[1]; j++) { | |
| 445 | ✗ | if (pic->dpb[i] == pic->refs[1][j]) | |
| 446 | ✗ | break; | |
| 447 | } | ||
| 448 | ✗ | if (j < pic->nb_refs[1]) | |
| 449 | ✗ | continue; | |
| 450 | |||
| 451 | ✗ | strp = pic->dpb[i]->codec_priv; | |
| 452 | ✗ | rps_poc[rps_pics] = strp->pic_order_cnt; | |
| 453 | ✗ | rps_used[rps_pics] = 0; | |
| 454 | ✗ | ++rps_pics; | |
| 455 | } | ||
| 456 | |||
| 457 | ✗ | for (i = 1; i < rps_pics; i++) { | |
| 458 | ✗ | for (j = i; j > 0; j--) { | |
| 459 | ✗ | if (rps_poc[j] > rps_poc[j - 1]) | |
| 460 | ✗ | break; | |
| 461 | ✗ | av_assert0(rps_poc[j] != rps_poc[j - 1]); | |
| 462 | ✗ | FFSWAP(int, rps_poc[j], rps_poc[j - 1]); | |
| 463 | ✗ | FFSWAP(int, rps_used[j], rps_used[j - 1]); | |
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | ✗ | av_log(avctx, AV_LOG_DEBUG, "RPS for POC %d:", hp->pic_order_cnt); | |
| 468 | ✗ | for (i = 0; i < rps_pics; i++) | |
| 469 | ✗ | av_log(avctx, AV_LOG_DEBUG, " (%d,%d)", rps_poc[i], rps_used[i]); | |
| 470 | |||
| 471 | ✗ | av_log(avctx, AV_LOG_DEBUG, "\n"); | |
| 472 | |||
| 473 | ✗ | for (i = 0; i < rps_pics; i++) { | |
| 474 | ✗ | av_assert0(rps_poc[i] != hp->pic_order_cnt); | |
| 475 | ✗ | if (rps_poc[i] > hp->pic_order_cnt) | |
| 476 | ✗ | break; | |
| 477 | } | ||
| 478 | |||
| 479 | ✗ | rps->num_negative_pics = i; | |
| 480 | ✗ | rps->used_by_curr_pic_s0_flag = 0x0; | |
| 481 | ✗ | poc = hp->pic_order_cnt; | |
| 482 | ✗ | for (j = i - 1; j >= 0; j--) { | |
| 483 | ✗ | rps->delta_poc_s0_minus1[i - 1 - j] = poc - rps_poc[j] - 1; | |
| 484 | ✗ | rps->used_by_curr_pic_s0_flag |= rps_used[j] << (i - 1 - j); | |
| 485 | ✗ | poc = rps_poc[j]; | |
| 486 | } | ||
| 487 | |||
| 488 | ✗ | rps->num_positive_pics = rps_pics - i; | |
| 489 | ✗ | rps->used_by_curr_pic_s1_flag = 0x0; | |
| 490 | ✗ | poc = hp->pic_order_cnt; | |
| 491 | ✗ | for (j = i; j < rps_pics; j++) { | |
| 492 | ✗ | rps->delta_poc_s1_minus1[j - i] = rps_poc[j] - poc - 1; | |
| 493 | ✗ | rps->used_by_curr_pic_s1_flag |= rps_used[j] << (j - i); | |
| 494 | ✗ | poc = rps_poc[j]; | |
| 495 | } | ||
| 496 | |||
| 497 | ✗ | hp->l_rps.num_long_term_sps = 0; | |
| 498 | ✗ | hp->l_rps.num_long_term_pics = 0; | |
| 499 | |||
| 500 | // when this flag is not present, it is inerred to 1. | ||
| 501 | ✗ | hp->slice_hdr.flags.collocated_from_l0_flag = 1; | |
| 502 | ✗ | hp->h265pic_info.flags.slice_temporal_mvp_enabled_flag = | |
| 503 | ✗ | enc->units.raw_sps.sps_temporal_mvp_enabled_flag; | |
| 504 | ✗ | if (hp->h265pic_info.flags.slice_temporal_mvp_enabled_flag) { | |
| 505 | ✗ | if (hp->slice_hdr.slice_type == STD_VIDEO_H265_SLICE_TYPE_B) | |
| 506 | ✗ | hp->slice_hdr.flags.collocated_from_l0_flag = 1; | |
| 507 | ✗ | hp->slice_hdr.collocated_ref_idx = 0; | |
| 508 | } | ||
| 509 | |||
| 510 | ✗ | hp->slice_hdr.flags.num_ref_idx_active_override_flag = 0; | |
| 511 | ✗ | hp->ref_list_info.num_ref_idx_l0_active_minus1 = enc->units.raw_pps.num_ref_idx_l0_default_active_minus1; | |
| 512 | ✗ | hp->ref_list_info.num_ref_idx_l1_active_minus1 = enc->units.raw_pps.num_ref_idx_l1_default_active_minus1; | |
| 513 | } | ||
| 514 | |||
| 515 | ✗ | hp->h265pic_info.pShortTermRefPicSet = &hp->s_rps; | |
| 516 | ✗ | hp->h265pic_info.pLongTermRefPics = &hp->l_rps; | |
| 517 | ✗ | } | |
| 518 | |||
| 519 | ✗ | static int init_pic_params(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, | |
| 520 | VkVideoEncodeInfoKHR *encode_info) | ||
| 521 | { | ||
| 522 | int err; | ||
| 523 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 524 | ✗ | FFVulkanEncodePicture *vp = pic->priv; | |
| 525 | ✗ | VulkanEncodeH265Picture *hp = pic->codec_priv; | |
| 526 | VkVideoReferenceSlotInfoKHR *ref_slot; | ||
| 527 | |||
| 528 | ✗ | err = vk_enc_h265_update_pic_info(avctx, pic); | |
| 529 | ✗ | if (err < 0) | |
| 530 | ✗ | return err; | |
| 531 | |||
| 532 | ✗ | hp->vkh265pic_info = (VkVideoEncodeH265PictureInfoKHR) { | |
| 533 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_PICTURE_INFO_KHR, | ||
| 534 | .pNext = NULL, | ||
| 535 | .pNaluSliceSegmentEntries = NULL, // Filled in during setup_slices() | ||
| 536 | .naluSliceSegmentEntryCount = 0, // Filled in during setup_slices() | ||
| 537 | ✗ | .pStdPictureInfo = &hp->h265pic_info, | |
| 538 | }; | ||
| 539 | |||
| 540 | ✗ | hp->h265pic_info = (StdVideoEncodeH265PictureInfo) { | |
| 541 | .flags = (StdVideoEncodeH265PictureInfoFlags) { | ||
| 542 | ✗ | .is_reference = pic->is_reference, | |
| 543 | ✗ | .IrapPicFlag = pic->type == FF_HW_PICTURE_TYPE_IDR, | |
| 544 | .used_for_long_term_reference = 0, | ||
| 545 | .discardable_flag = 0, | ||
| 546 | .cross_layer_bla_flag = 0, | ||
| 547 | .pic_output_flag = 1, | ||
| 548 | .no_output_of_prior_pics_flag = 0, | ||
| 549 | .short_term_ref_pic_set_sps_flag = 0, | ||
| 550 | ✗ | .slice_temporal_mvp_enabled_flag = enc->units.raw_sps.sps_temporal_mvp_enabled_flag, | |
| 551 | /* Reserved */ | ||
| 552 | }, | ||
| 553 | ✗ | .pic_type = hp->pic_type, | |
| 554 | .sps_video_parameter_set_id = 0, | ||
| 555 | .pps_seq_parameter_set_id = 0, | ||
| 556 | .pps_pic_parameter_set_id = 0, | ||
| 557 | .short_term_ref_pic_set_idx = 0, | ||
| 558 | ✗ | .PicOrderCntVal = hp->pic_order_cnt, | |
| 559 | .TemporalId = 0, | ||
| 560 | /* Reserved */ | ||
| 561 | .pRefLists = NULL, // Filled in during setup_refs | ||
| 562 | .pShortTermRefPicSet = NULL, | ||
| 563 | .pLongTermRefPics = NULL, | ||
| 564 | }; | ||
| 565 | ✗ | encode_info->pNext = &hp->vkh265pic_info; | |
| 566 | |||
| 567 | ✗ | hp->h265dpb_info = (StdVideoEncodeH265ReferenceInfo) { | |
| 568 | .flags = (StdVideoEncodeH265ReferenceInfoFlags) { | ||
| 569 | .used_for_long_term_reference = 0, | ||
| 570 | .unused_for_reference = 0, | ||
| 571 | /* Reserved */ | ||
| 572 | }, | ||
| 573 | ✗ | .pic_type = hp->h265pic_info.pic_type, | |
| 574 | ✗ | .PicOrderCntVal = hp->h265pic_info.PicOrderCntVal, | |
| 575 | ✗ | .TemporalId = hp->h265pic_info.TemporalId, | |
| 576 | }; | ||
| 577 | ✗ | hp->vkh265dpb_info = (VkVideoEncodeH265DpbSlotInfoKHR) { | |
| 578 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_DPB_SLOT_INFO_KHR, | ||
| 579 | ✗ | .pStdReferenceInfo = &hp->h265dpb_info, | |
| 580 | }; | ||
| 581 | |||
| 582 | ✗ | vp->dpb_slot.pNext = &hp->vkh265dpb_info; | |
| 583 | |||
| 584 | ✗ | ref_slot = (VkVideoReferenceSlotInfoKHR *)encode_info->pSetupReferenceSlot; | |
| 585 | ✗ | ref_slot->pNext = &hp->vkh265dpb_info; | |
| 586 | |||
| 587 | ✗ | setup_refs(avctx, pic, encode_info); | |
| 588 | |||
| 589 | ✗ | setup_slices(avctx, pic); | |
| 590 | |||
| 591 | ✗ | return 0; | |
| 592 | } | ||
| 593 | |||
| 594 | ✗ | static int init_profile(AVCodecContext *avctx, | |
| 595 | VkVideoProfileInfoKHR *profile, void *pnext) | ||
| 596 | { | ||
| 597 | VkResult ret; | ||
| 598 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 599 | ✗ | FFVulkanEncodeContext *ctx = &enc->common; | |
| 600 | ✗ | FFVulkanContext *s = &ctx->s; | |
| 601 | ✗ | FFVulkanFunctions *vk = &ctx->s.vkfn; | |
| 602 | ✗ | VkVideoEncodeH265CapabilitiesKHR h265_caps = { | |
| 603 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_CAPABILITIES_KHR, | ||
| 604 | }; | ||
| 605 | ✗ | VkVideoEncodeCapabilitiesKHR enc_caps = { | |
| 606 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_CAPABILITIES_KHR, | ||
| 607 | .pNext = &h265_caps, | ||
| 608 | }; | ||
| 609 | ✗ | VkVideoCapabilitiesKHR caps = { | |
| 610 | .sType = VK_STRUCTURE_TYPE_VIDEO_CAPABILITIES_KHR, | ||
| 611 | .pNext = &enc_caps, | ||
| 612 | }; | ||
| 613 | |||
| 614 | /* In order of preference */ | ||
| 615 | ✗ | int last_supported = AV_PROFILE_UNKNOWN; | |
| 616 | static const int known_profiles[] = { | ||
| 617 | AV_PROFILE_HEVC_MAIN, | ||
| 618 | AV_PROFILE_HEVC_MAIN_10, | ||
| 619 | AV_PROFILE_HEVC_REXT, | ||
| 620 | }; | ||
| 621 | ✗ | int nb_profiles = FF_ARRAY_ELEMS(known_profiles); | |
| 622 | |||
| 623 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->frames->sw_format); | |
| 624 | ✗ | if (!desc) | |
| 625 | ✗ | return AVERROR(EINVAL); | |
| 626 | |||
| 627 | ✗ | if (s->frames->sw_format == AV_PIX_FMT_NV12) | |
| 628 | ✗ | nb_profiles = 1; | |
| 629 | ✗ | else if (s->frames->sw_format == AV_PIX_FMT_P010) | |
| 630 | ✗ | nb_profiles = 2; | |
| 631 | |||
| 632 | ✗ | enc->profile = (VkVideoEncodeH265ProfileInfoKHR) { | |
| 633 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_PROFILE_INFO_KHR, | ||
| 634 | .pNext = pnext, | ||
| 635 | ✗ | .stdProfileIdc = ff_vk_h265_profile_to_vk(avctx->profile), | |
| 636 | }; | ||
| 637 | ✗ | profile->pNext = &enc->profile; | |
| 638 | |||
| 639 | /* Set level */ | ||
| 640 | ✗ | if (avctx->level == AV_LEVEL_UNKNOWN) | |
| 641 | ✗ | avctx->level = enc->common.opts.level; | |
| 642 | |||
| 643 | /* User has explicitly specified a profile. */ | ||
| 644 | ✗ | if (avctx->profile != AV_PROFILE_UNKNOWN) | |
| 645 | ✗ | return 0; | |
| 646 | |||
| 647 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Supported profiles:\n"); | |
| 648 | ✗ | for (int i = 0; i < nb_profiles; i++) { | |
| 649 | ✗ | enc->profile.stdProfileIdc = ff_vk_h265_profile_to_vk(known_profiles[i]); | |
| 650 | ✗ | ret = vk->GetPhysicalDeviceVideoCapabilitiesKHR(s->hwctx->phys_dev, | |
| 651 | profile, | ||
| 652 | &caps); | ||
| 653 | ✗ | if (ret == VK_SUCCESS) { | |
| 654 | ✗ | av_log(avctx, AV_LOG_DEBUG, " %s\n", | |
| 655 | ✗ | avcodec_profile_name(avctx->codec_id, known_profiles[i])); | |
| 656 | ✗ | last_supported = known_profiles[i]; | |
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | ✗ | if (last_supported == AV_PROFILE_UNKNOWN) { | |
| 661 | ✗ | av_log(avctx, AV_LOG_ERROR, "No supported profiles for given format\n"); | |
| 662 | ✗ | return AVERROR(ENOTSUP); | |
| 663 | } | ||
| 664 | |||
| 665 | ✗ | enc->profile.stdProfileIdc = ff_vk_h265_profile_to_vk(last_supported); | |
| 666 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Using profile %s\n", | |
| 667 | avcodec_profile_name(avctx->codec_id, last_supported)); | ||
| 668 | ✗ | avctx->profile = last_supported; | |
| 669 | |||
| 670 | ✗ | return 0; | |
| 671 | } | ||
| 672 | |||
| 673 | ✗ | static int init_enc_options(AVCodecContext *avctx) | |
| 674 | { | ||
| 675 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 676 | |||
| 677 | ✗ | if (avctx->rc_buffer_size) | |
| 678 | ✗ | enc->hrd_buffer_size = avctx->rc_buffer_size; | |
| 679 | ✗ | else if (avctx->rc_max_rate > 0) | |
| 680 | ✗ | enc->hrd_buffer_size = avctx->rc_max_rate; | |
| 681 | else | ||
| 682 | ✗ | enc->hrd_buffer_size = avctx->bit_rate; | |
| 683 | |||
| 684 | ✗ | if (avctx->rc_initial_buffer_occupancy) { | |
| 685 | ✗ | if (avctx->rc_initial_buffer_occupancy > enc->hrd_buffer_size) { | |
| 686 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid RC buffer settings: " | |
| 687 | "must have initial buffer size (%d) <= " | ||
| 688 | "buffer size (%"PRId64").\n", | ||
| 689 | avctx->rc_initial_buffer_occupancy, enc->hrd_buffer_size); | ||
| 690 | ✗ | return AVERROR(EINVAL); | |
| 691 | } | ||
| 692 | ✗ | enc->initial_buffer_fullness = avctx->rc_initial_buffer_occupancy; | |
| 693 | } else { | ||
| 694 | ✗ | enc->initial_buffer_fullness = enc->hrd_buffer_size * 3 / 4; | |
| 695 | } | ||
| 696 | |||
| 697 | ✗ | if (enc->common.opts.rc_mode == VK_VIDEO_ENCODE_RATE_CONTROL_MODE_DISABLED_BIT_KHR) { | |
| 698 | ✗ | enc->fixed_qp_p = av_clip(enc->common.opts.qp, | |
| 699 | ✗ | enc->caps.minQp, enc->caps.maxQp); | |
| 700 | |||
| 701 | ✗ | if (avctx->i_quant_factor > 0.0) | |
| 702 | ✗ | enc->fixed_qp_idr = av_clip((avctx->i_quant_factor * enc->fixed_qp_p + | |
| 703 | ✗ | avctx->i_quant_offset) + 0.5, | |
| 704 | ✗ | enc->caps.minQp, enc->caps.maxQp); | |
| 705 | else | ||
| 706 | ✗ | enc->fixed_qp_idr = enc->fixed_qp_p; | |
| 707 | |||
| 708 | ✗ | if (avctx->b_quant_factor > 0.0) | |
| 709 | ✗ | enc->fixed_qp_b = av_clip((avctx->b_quant_factor * enc->fixed_qp_p + | |
| 710 | ✗ | avctx->b_quant_offset) + 0.5, | |
| 711 | ✗ | enc->caps.minQp, enc->caps.maxQp); | |
| 712 | else | ||
| 713 | ✗ | enc->fixed_qp_b = enc->fixed_qp_p; | |
| 714 | |||
| 715 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = " | |
| 716 | "%d / %d / %d for IDR- / P- / B-frames.\n", | ||
| 717 | ✗ | enc->fixed_qp_idr, enc->fixed_qp_p, enc->fixed_qp_b); | |
| 718 | } else { | ||
| 719 | ✗ | enc->fixed_qp_idr = 26; | |
| 720 | ✗ | enc->fixed_qp_p = 26; | |
| 721 | ✗ | enc->fixed_qp_b = 26; | |
| 722 | } | ||
| 723 | |||
| 724 | ✗ | return 0; | |
| 725 | } | ||
| 726 | |||
| 727 | ✗ | static av_cold int init_sequence_headers(AVCodecContext *avctx) | |
| 728 | { | ||
| 729 | int err; | ||
| 730 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 731 | ✗ | FFVulkanEncodeContext *ctx = &enc->common; | |
| 732 | ✗ | FFHWBaseEncodeContext *base_ctx = &ctx->base; | |
| 733 | |||
| 734 | ✗ | FFHWBaseEncodeH265 *units = &enc->units; | |
| 735 | ✗ | FFHWBaseEncodeH265Opts *unit_opts = &enc->unit_opts; | |
| 736 | |||
| 737 | int max_ctb_size; | ||
| 738 | unsigned min_tb_size; | ||
| 739 | unsigned max_tb_size; | ||
| 740 | unsigned max_transform_hierarchy; | ||
| 741 | |||
| 742 | ✗ | unit_opts->tier = enc->common.opts.tier; | |
| 743 | ✗ | unit_opts->fixed_qp_idr = enc->fixed_qp_idr; | |
| 744 | ✗ | unit_opts->cu_qp_delta_enabled_flag = enc->common.opts.rc_mode != VK_VIDEO_ENCODE_RATE_CONTROL_MODE_DISABLED_BIT_KHR; | |
| 745 | |||
| 746 | ✗ | unit_opts->nb_slices = 1; | |
| 747 | |||
| 748 | ✗ | unit_opts->slice_block_rows = (avctx->height + base_ctx->slice_block_height - 1) / | |
| 749 | ✗ | base_ctx->slice_block_height; | |
| 750 | ✗ | unit_opts->slice_block_cols = (avctx->width + base_ctx->slice_block_width - 1) / | |
| 751 | ✗ | base_ctx->slice_block_width; | |
| 752 | |||
| 753 | /* cabac already set via an option */ | ||
| 754 | /* fixed_qp_idr initialized in init_enc_options() */ | ||
| 755 | /* hrd_buffer_size initialized in init_enc_options() */ | ||
| 756 | /* initial_buffer_fullness initialized in init_enc_options() */ | ||
| 757 | |||
| 758 | ✗ | err = ff_hw_base_encode_init_params_h265(&enc->common.base, avctx, | |
| 759 | units, unit_opts); | ||
| 760 | ✗ | if (err < 0) | |
| 761 | ✗ | return err; | |
| 762 | |||
| 763 | ✗ | units->raw_sps.sample_adaptive_offset_enabled_flag = | |
| 764 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_SAMPLE_ADAPTIVE_OFFSET_ENABLED_FLAG_SET_BIT_KHR); | |
| 765 | ✗ | units->raw_pps.transform_skip_enabled_flag = | |
| 766 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_TRANSFORM_SKIP_ENABLED_FLAG_SET_BIT_KHR); | |
| 767 | |||
| 768 | ✗ | max_ctb_size = 16; | |
| 769 | |||
| 770 | /* coding blocks from 8x8 to max CTB size. */ | ||
| 771 | ✗ | if (enc->caps.ctbSizes & VK_VIDEO_ENCODE_H265_CTB_SIZE_64_BIT_KHR) | |
| 772 | ✗ | max_ctb_size = 64; | |
| 773 | ✗ | else if (enc->caps.ctbSizes & VK_VIDEO_ENCODE_H265_CTB_SIZE_32_BIT_KHR) | |
| 774 | ✗ | max_ctb_size = 32; | |
| 775 | |||
| 776 | ✗ | min_tb_size = 0; | |
| 777 | ✗ | max_tb_size = 0; | |
| 778 | ✗ | if (enc->caps.transformBlockSizes & VK_VIDEO_ENCODE_H265_TRANSFORM_BLOCK_SIZE_4_BIT_KHR) | |
| 779 | ✗ | min_tb_size = 4; | |
| 780 | ✗ | else if (enc->caps.transformBlockSizes & VK_VIDEO_ENCODE_H265_TRANSFORM_BLOCK_SIZE_8_BIT_KHR) | |
| 781 | ✗ | min_tb_size = 8; | |
| 782 | ✗ | else if (enc->caps.transformBlockSizes & VK_VIDEO_ENCODE_H265_TRANSFORM_BLOCK_SIZE_16_BIT_KHR) | |
| 783 | ✗ | min_tb_size = 16; | |
| 784 | ✗ | else if (enc->caps.transformBlockSizes & VK_VIDEO_ENCODE_H265_TRANSFORM_BLOCK_SIZE_32_BIT_KHR) | |
| 785 | ✗ | min_tb_size = 32; | |
| 786 | |||
| 787 | ✗ | if (enc->caps.transformBlockSizes & VK_VIDEO_ENCODE_H265_TRANSFORM_BLOCK_SIZE_32_BIT_KHR) | |
| 788 | ✗ | max_tb_size = 32; | |
| 789 | ✗ | else if (enc->caps.transformBlockSizes & VK_VIDEO_ENCODE_H265_TRANSFORM_BLOCK_SIZE_16_BIT_KHR) | |
| 790 | ✗ | max_tb_size = 16; | |
| 791 | ✗ | else if (enc->caps.transformBlockSizes & VK_VIDEO_ENCODE_H265_TRANSFORM_BLOCK_SIZE_8_BIT_KHR) | |
| 792 | ✗ | max_tb_size = 8; | |
| 793 | ✗ | else if (enc->caps.transformBlockSizes & VK_VIDEO_ENCODE_H265_TRANSFORM_BLOCK_SIZE_4_BIT_KHR) | |
| 794 | ✗ | max_tb_size = 4; | |
| 795 | |||
| 796 | /* Prefer 16x16 min CU when the CTB is at least 32; 8x8 min CU is much | ||
| 797 | * more expensive on some implementations for the common quality levels. */ | ||
| 798 | ✗ | if (max_ctb_size >= 32) { | |
| 799 | ✗ | units->raw_sps.log2_min_luma_coding_block_size_minus3 = 1; | |
| 800 | ✗ | units->raw_sps.log2_diff_max_min_luma_coding_block_size = av_log2(max_ctb_size) - 4; | |
| 801 | } else { | ||
| 802 | ✗ | units->raw_sps.log2_min_luma_coding_block_size_minus3 = 0; | |
| 803 | ✗ | units->raw_sps.log2_diff_max_min_luma_coding_block_size = av_log2(max_ctb_size) - 3; | |
| 804 | } | ||
| 805 | ✗ | units->raw_sps.log2_min_luma_transform_block_size_minus2 = av_log2(min_tb_size) - 2; | |
| 806 | ✗ | units->raw_sps.log2_diff_max_min_luma_transform_block_size = av_log2(max_tb_size) - av_log2(min_tb_size); | |
| 807 | |||
| 808 | ✗ | max_transform_hierarchy = av_log2(max_ctb_size) - av_log2(min_tb_size); | |
| 809 | ✗ | units->raw_sps.max_transform_hierarchy_depth_intra = max_transform_hierarchy; | |
| 810 | ✗ | units->raw_sps.max_transform_hierarchy_depth_inter = max_transform_hierarchy; | |
| 811 | |||
| 812 | ✗ | units->raw_sps.vui.bitstream_restriction_flag = 0; | |
| 813 | ✗ | units->raw_sps.vui.max_bytes_per_pic_denom = 2; | |
| 814 | ✗ | units->raw_sps.vui.max_bits_per_min_cu_denom = 1; | |
| 815 | |||
| 816 | ✗ | units->raw_sps.sps_temporal_mvp_enabled_flag = 0; | |
| 817 | |||
| 818 | ✗ | if (base_ctx->gop_size & base_ctx->gop_size - 1 == 0) | |
| 819 | ✗ | units->raw_sps.log2_max_pic_order_cnt_lsb_minus4 = FFMAX(av_log2(base_ctx->gop_size) - 4, 0); | |
| 820 | else | ||
| 821 | ✗ | units->raw_sps.log2_max_pic_order_cnt_lsb_minus4 = FFMAX(av_log2(base_ctx->gop_size) - 3, 0); | |
| 822 | |||
| 823 | ✗ | return 0; | |
| 824 | } | ||
| 825 | |||
| 826 | typedef struct VulkanH265Units { | ||
| 827 | StdVideoH265SequenceParameterSet sps; | ||
| 828 | StdVideoH265ShortTermRefPicSet str[STD_VIDEO_H265_SUBLAYERS_LIST_SIZE]; | ||
| 829 | StdVideoH265LongTermRefPicsSps ltr; | ||
| 830 | StdVideoH265ProfileTierLevel ptl_sps; | ||
| 831 | StdVideoH265DecPicBufMgr dpbm_sps; | ||
| 832 | |||
| 833 | StdVideoH265HrdParameters vui_header_sps; | ||
| 834 | StdVideoH265SequenceParameterSetVui vui_sps; | ||
| 835 | |||
| 836 | StdVideoH265SubLayerHrdParameters slhdrnal[HEVC_MAX_SUB_LAYERS]; | ||
| 837 | StdVideoH265SubLayerHrdParameters slhdrvcl[HEVC_MAX_SUB_LAYERS]; | ||
| 838 | |||
| 839 | StdVideoH265PictureParameterSet pps; | ||
| 840 | StdVideoH265ScalingLists pps_scaling; | ||
| 841 | |||
| 842 | StdVideoH265VideoParameterSet vps; | ||
| 843 | StdVideoH265ProfileTierLevel ptl_vps; | ||
| 844 | StdVideoH265DecPicBufMgr dpbm_vps; | ||
| 845 | StdVideoH265HrdParameters vui_header_vps; | ||
| 846 | } VulkanH265Units; | ||
| 847 | |||
| 848 | ✗ | static av_cold int base_unit_to_vk(AVCodecContext *avctx, | |
| 849 | VulkanH265Units *vk_units) | ||
| 850 | { | ||
| 851 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 852 | |||
| 853 | ✗ | H265RawSPS *sps = &enc->units.raw_sps; | |
| 854 | ✗ | StdVideoH265SequenceParameterSet *vksps = &vk_units->sps; | |
| 855 | ✗ | StdVideoH265ShortTermRefPicSet *str = vk_units->str; | |
| 856 | ✗ | StdVideoH265LongTermRefPicsSps *ltr = &vk_units->ltr; | |
| 857 | ✗ | StdVideoH265ProfileTierLevel *ptl_sps = &vk_units->ptl_sps; | |
| 858 | ✗ | StdVideoH265DecPicBufMgr *dpbm_sps = &vk_units->dpbm_sps; | |
| 859 | |||
| 860 | ✗ | StdVideoH265HrdParameters *vui_header_sps = &vk_units->vui_header_sps; | |
| 861 | ✗ | StdVideoH265SequenceParameterSetVui *vui_sps = &vk_units->vui_sps; | |
| 862 | |||
| 863 | ✗ | StdVideoH265SubLayerHrdParameters *slhdrnal = vk_units->slhdrnal; | |
| 864 | ✗ | StdVideoH265SubLayerHrdParameters *slhdrvcl = vk_units->slhdrvcl; | |
| 865 | |||
| 866 | ✗ | H265RawPPS *pps = &enc->units.raw_pps; | |
| 867 | ✗ | StdVideoH265PictureParameterSet *vkpps = &vk_units->pps; | |
| 868 | |||
| 869 | ✗ | H265RawVPS *vps = &enc->units.raw_vps; | |
| 870 | ✗ | StdVideoH265VideoParameterSet *vkvps = &vk_units->vps; | |
| 871 | ✗ | StdVideoH265ProfileTierLevel *ptl_vps = &vk_units->ptl_vps; | |
| 872 | ✗ | StdVideoH265DecPicBufMgr *dpbm_vps = &vk_units->dpbm_vps; | |
| 873 | ✗ | StdVideoH265HrdParameters *vui_header_vps = &vk_units->vui_header_vps; | |
| 874 | |||
| 875 | /* SPS */ | ||
| 876 | ✗ | for (int i = 0; i < HEVC_MAX_SUB_LAYERS; i++) { | |
| 877 | ✗ | memcpy(&slhdrnal[i], &sps->vui.hrd_parameters.nal_sub_layer_hrd_parameters[i], sizeof(*slhdrnal)); | |
| 878 | ✗ | memcpy(&slhdrvcl[i], &sps->vui.hrd_parameters.vcl_sub_layer_hrd_parameters[i], sizeof(*slhdrvcl)); | |
| 879 | ✗ | slhdrnal[i].cbr_flag = 0x0; | |
| 880 | ✗ | slhdrvcl[i].cbr_flag = 0x0; | |
| 881 | ✗ | for (int j = 0; j < HEVC_MAX_CPB_CNT; j++) { | |
| 882 | ✗ | slhdrnal[i].cbr_flag |= sps->vui.hrd_parameters.nal_sub_layer_hrd_parameters[i].cbr_flag[j] << i; | |
| 883 | ✗ | slhdrvcl[i].cbr_flag |= sps->vui.hrd_parameters.vcl_sub_layer_hrd_parameters[i].cbr_flag[j] << i; | |
| 884 | } | ||
| 885 | } | ||
| 886 | |||
| 887 | ✗ | *vui_header_sps = (StdVideoH265HrdParameters) { | |
| 888 | .flags = (StdVideoH265HrdFlags) { | ||
| 889 | ✗ | .nal_hrd_parameters_present_flag = sps->vui.hrd_parameters.nal_hrd_parameters_present_flag, | |
| 890 | ✗ | .vcl_hrd_parameters_present_flag = sps->vui.hrd_parameters.vcl_hrd_parameters_present_flag, | |
| 891 | ✗ | .sub_pic_hrd_params_present_flag = sps->vui.hrd_parameters.sub_pic_hrd_params_present_flag, | |
| 892 | ✗ | .sub_pic_cpb_params_in_pic_timing_sei_flag = sps->vui.hrd_parameters.sub_pic_cpb_params_in_pic_timing_sei_flag, | |
| 893 | .fixed_pic_rate_general_flag = 0x0, | ||
| 894 | .fixed_pic_rate_within_cvs_flag = 0x0, | ||
| 895 | .low_delay_hrd_flag = 0x0, | ||
| 896 | }, | ||
| 897 | ✗ | .tick_divisor_minus2 = sps->vui.hrd_parameters.tick_divisor_minus2, | |
| 898 | ✗ | .du_cpb_removal_delay_increment_length_minus1 = sps->vui.hrd_parameters.du_cpb_removal_delay_increment_length_minus1, | |
| 899 | ✗ | .dpb_output_delay_du_length_minus1 = sps->vui.hrd_parameters.dpb_output_delay_du_length_minus1, | |
| 900 | ✗ | .bit_rate_scale = sps->vui.hrd_parameters.bit_rate_scale, | |
| 901 | ✗ | .cpb_size_scale = sps->vui.hrd_parameters.cpb_size_scale, | |
| 902 | ✗ | .cpb_size_du_scale = sps->vui.hrd_parameters.cpb_size_du_scale, | |
| 903 | ✗ | .initial_cpb_removal_delay_length_minus1 = sps->vui.hrd_parameters.initial_cpb_removal_delay_length_minus1, | |
| 904 | ✗ | .au_cpb_removal_delay_length_minus1 = sps->vui.hrd_parameters.au_cpb_removal_delay_length_minus1, | |
| 905 | ✗ | .dpb_output_delay_length_minus1 = sps->vui.hrd_parameters.dpb_output_delay_length_minus1, | |
| 906 | /* Reserved - 3*16 bits */ | ||
| 907 | .pSubLayerHrdParametersNal = slhdrnal, | ||
| 908 | .pSubLayerHrdParametersVcl = slhdrvcl, | ||
| 909 | }; | ||
| 910 | |||
| 911 | ✗ | for (int i = 0; i < HEVC_MAX_SUB_LAYERS; i++) { | |
| 912 | ✗ | vui_header_sps->flags.fixed_pic_rate_general_flag |= sps->vui.hrd_parameters.fixed_pic_rate_general_flag[i] << i; | |
| 913 | ✗ | vui_header_sps->flags.fixed_pic_rate_within_cvs_flag |= sps->vui.hrd_parameters.fixed_pic_rate_within_cvs_flag[i] << i; | |
| 914 | ✗ | vui_header_sps->flags.low_delay_hrd_flag |= sps->vui.hrd_parameters.low_delay_hrd_flag[i] << i; | |
| 915 | } | ||
| 916 | |||
| 917 | ✗ | for (int i = 0; i < STD_VIDEO_H265_SUBLAYERS_LIST_SIZE; i++) { | |
| 918 | ✗ | dpbm_sps->max_latency_increase_plus1[i] = sps->sps_max_latency_increase_plus1[i]; | |
| 919 | ✗ | dpbm_sps->max_dec_pic_buffering_minus1[i] = sps->sps_max_dec_pic_buffering_minus1[i]; | |
| 920 | ✗ | dpbm_sps->max_num_reorder_pics[i] = sps->sps_max_num_reorder_pics[i]; | |
| 921 | } | ||
| 922 | |||
| 923 | ✗ | *ptl_sps = (StdVideoH265ProfileTierLevel) { | |
| 924 | .flags = (StdVideoH265ProfileTierLevelFlags) { | ||
| 925 | ✗ | .general_tier_flag = sps->profile_tier_level.general_tier_flag, | |
| 926 | ✗ | .general_progressive_source_flag = sps->profile_tier_level.general_progressive_source_flag, | |
| 927 | ✗ | .general_interlaced_source_flag = sps->profile_tier_level.general_interlaced_source_flag, | |
| 928 | ✗ | .general_non_packed_constraint_flag = sps->profile_tier_level.general_non_packed_constraint_flag, | |
| 929 | ✗ | .general_frame_only_constraint_flag = sps->profile_tier_level.general_frame_only_constraint_flag, | |
| 930 | }, | ||
| 931 | ✗ | .general_profile_idc = ff_vk_h265_profile_to_vk(sps->profile_tier_level.general_profile_idc), | |
| 932 | ✗ | .general_level_idc = ff_vk_h265_level_to_vk(sps->profile_tier_level.general_level_idc), | |
| 933 | }; | ||
| 934 | |||
| 935 | ✗ | for (int i = 0; i < STD_VIDEO_H265_MAX_SHORT_TERM_REF_PIC_SETS; i++) { | |
| 936 | ✗ | const H265RawSTRefPicSet *st_rps = &sps->st_ref_pic_set[i]; | |
| 937 | |||
| 938 | ✗ | str[i] = (StdVideoH265ShortTermRefPicSet) { | |
| 939 | .flags = (StdVideoH265ShortTermRefPicSetFlags) { | ||
| 940 | ✗ | .inter_ref_pic_set_prediction_flag = st_rps->inter_ref_pic_set_prediction_flag, | |
| 941 | ✗ | .delta_rps_sign = st_rps->delta_rps_sign, | |
| 942 | }, | ||
| 943 | ✗ | .delta_idx_minus1 = st_rps->delta_idx_minus1, | |
| 944 | .use_delta_flag = 0x0, | ||
| 945 | ✗ | .abs_delta_rps_minus1 = st_rps->abs_delta_rps_minus1, | |
| 946 | .used_by_curr_pic_flag = 0x0, | ||
| 947 | .used_by_curr_pic_s0_flag = 0x0, | ||
| 948 | .used_by_curr_pic_s1_flag = 0x0, | ||
| 949 | /* Reserved */ | ||
| 950 | /* Reserved */ | ||
| 951 | /* Reserved */ | ||
| 952 | ✗ | .num_negative_pics = st_rps->num_negative_pics, | |
| 953 | ✗ | .num_positive_pics = st_rps->num_positive_pics, | |
| 954 | }; | ||
| 955 | |||
| 956 | ✗ | for (int j = 0; j < HEVC_MAX_REFS; j++) { | |
| 957 | ✗ | str[i].use_delta_flag |= st_rps->use_delta_flag[j] << i; | |
| 958 | ✗ | str[i].used_by_curr_pic_flag |= st_rps->used_by_curr_pic_flag[j] << i; | |
| 959 | ✗ | str[i].used_by_curr_pic_s0_flag |= st_rps->used_by_curr_pic_s0_flag[j] << i; | |
| 960 | ✗ | str[i].used_by_curr_pic_s1_flag |= st_rps->used_by_curr_pic_s1_flag[j] << i; | |
| 961 | ✗ | str[i].delta_poc_s0_minus1[j] = st_rps->delta_poc_s0_minus1[j]; | |
| 962 | ✗ | str[i].delta_poc_s1_minus1[j] = st_rps->delta_poc_s1_minus1[j]; | |
| 963 | } | ||
| 964 | } | ||
| 965 | |||
| 966 | ✗ | ltr->used_by_curr_pic_lt_sps_flag = 0; | |
| 967 | ✗ | for (int i = 0; i < STD_VIDEO_H265_MAX_LONG_TERM_REF_PICS_SPS; i++) { | |
| 968 | ✗ | ltr->used_by_curr_pic_lt_sps_flag |= sps->lt_ref_pic_poc_lsb_sps[i] << i; | |
| 969 | ✗ | ltr->lt_ref_pic_poc_lsb_sps[i] = sps->lt_ref_pic_poc_lsb_sps[i]; | |
| 970 | } | ||
| 971 | |||
| 972 | ✗ | *vksps = (StdVideoH265SequenceParameterSet) { | |
| 973 | .flags = (StdVideoH265SpsFlags) { | ||
| 974 | ✗ | .sps_temporal_id_nesting_flag = sps->sps_temporal_id_nesting_flag, | |
| 975 | ✗ | .separate_colour_plane_flag = sps->separate_colour_plane_flag, | |
| 976 | ✗ | .conformance_window_flag = sps->conformance_window_flag, | |
| 977 | ✗ | .sps_sub_layer_ordering_info_present_flag = sps->sps_sub_layer_ordering_info_present_flag, | |
| 978 | ✗ | .scaling_list_enabled_flag = sps->scaling_list_enabled_flag, | |
| 979 | ✗ | .sps_scaling_list_data_present_flag = sps->sps_scaling_list_data_present_flag, | |
| 980 | ✗ | .amp_enabled_flag = sps->amp_enabled_flag, | |
| 981 | ✗ | .sample_adaptive_offset_enabled_flag = sps->sample_adaptive_offset_enabled_flag, | |
| 982 | ✗ | .pcm_enabled_flag = sps->pcm_enabled_flag, | |
| 983 | ✗ | .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag, | |
| 984 | ✗ | .long_term_ref_pics_present_flag = sps->long_term_ref_pics_present_flag, | |
| 985 | ✗ | .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag, | |
| 986 | ✗ | .strong_intra_smoothing_enabled_flag = sps->strong_intra_smoothing_enabled_flag, | |
| 987 | ✗ | .vui_parameters_present_flag = sps->vui_parameters_present_flag, | |
| 988 | ✗ | .sps_extension_present_flag = sps->sps_extension_present_flag, | |
| 989 | ✗ | .sps_range_extension_flag = sps->sps_range_extension_flag, | |
| 990 | ✗ | .transform_skip_rotation_enabled_flag = sps->transform_skip_rotation_enabled_flag, | |
| 991 | ✗ | .transform_skip_context_enabled_flag = sps->transform_skip_context_enabled_flag, | |
| 992 | ✗ | .implicit_rdpcm_enabled_flag = sps->implicit_rdpcm_enabled_flag, | |
| 993 | ✗ | .explicit_rdpcm_enabled_flag = sps->explicit_rdpcm_enabled_flag, | |
| 994 | ✗ | .extended_precision_processing_flag = sps->extended_precision_processing_flag, | |
| 995 | ✗ | .intra_smoothing_disabled_flag = sps->intra_smoothing_disabled_flag, | |
| 996 | ✗ | .high_precision_offsets_enabled_flag = sps->high_precision_offsets_enabled_flag, | |
| 997 | ✗ | .persistent_rice_adaptation_enabled_flag = sps->persistent_rice_adaptation_enabled_flag, | |
| 998 | ✗ | .cabac_bypass_alignment_enabled_flag = sps->cabac_bypass_alignment_enabled_flag, | |
| 999 | ✗ | .sps_scc_extension_flag = sps->sps_scc_extension_flag, | |
| 1000 | ✗ | .sps_curr_pic_ref_enabled_flag = sps->sps_curr_pic_ref_enabled_flag, | |
| 1001 | ✗ | .palette_mode_enabled_flag = sps->palette_mode_enabled_flag, | |
| 1002 | ✗ | .sps_palette_predictor_initializers_present_flag = sps->sps_palette_predictor_initializer_present_flag, | |
| 1003 | ✗ | .intra_boundary_filtering_disabled_flag = sps->intra_boundary_filtering_disable_flag, | |
| 1004 | }, | ||
| 1005 | ✗ | .chroma_format_idc = sps->chroma_format_idc, | |
| 1006 | ✗ | .pic_width_in_luma_samples = sps->pic_width_in_luma_samples, | |
| 1007 | ✗ | .pic_height_in_luma_samples = sps->pic_height_in_luma_samples, | |
| 1008 | ✗ | .sps_video_parameter_set_id = sps->sps_video_parameter_set_id, | |
| 1009 | ✗ | .sps_max_sub_layers_minus1 = sps->sps_max_sub_layers_minus1, | |
| 1010 | ✗ | .sps_seq_parameter_set_id = sps->sps_seq_parameter_set_id, | |
| 1011 | ✗ | .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8, | |
| 1012 | ✗ | .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8, | |
| 1013 | ✗ | .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4, | |
| 1014 | ✗ | .log2_min_luma_coding_block_size_minus3 = sps->log2_min_luma_coding_block_size_minus3, | |
| 1015 | ✗ | .log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_luma_coding_block_size, | |
| 1016 | ✗ | .log2_min_luma_transform_block_size_minus2 = sps->log2_min_luma_transform_block_size_minus2, | |
| 1017 | ✗ | .log2_diff_max_min_luma_transform_block_size = sps->log2_diff_max_min_luma_transform_block_size, | |
| 1018 | ✗ | .max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter, | |
| 1019 | ✗ | .max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra, | |
| 1020 | ✗ | .num_short_term_ref_pic_sets = sps->num_short_term_ref_pic_sets, | |
| 1021 | ✗ | .num_long_term_ref_pics_sps = sps->num_long_term_ref_pics_sps, | |
| 1022 | ✗ | .pcm_sample_bit_depth_luma_minus1 = sps->pcm_sample_bit_depth_luma_minus1, | |
| 1023 | ✗ | .pcm_sample_bit_depth_chroma_minus1 = sps->pcm_sample_bit_depth_chroma_minus1, | |
| 1024 | ✗ | .log2_min_pcm_luma_coding_block_size_minus3 = sps->log2_min_pcm_luma_coding_block_size_minus3, | |
| 1025 | ✗ | .log2_diff_max_min_pcm_luma_coding_block_size = sps->log2_diff_max_min_pcm_luma_coding_block_size, | |
| 1026 | /* Reserved */ | ||
| 1027 | /* Reserved */ | ||
| 1028 | ✗ | .palette_max_size = sps->palette_max_size, | |
| 1029 | ✗ | .delta_palette_max_predictor_size = sps->delta_palette_max_predictor_size, | |
| 1030 | ✗ | .motion_vector_resolution_control_idc = sps->motion_vector_resolution_control_idc, | |
| 1031 | ✗ | .sps_num_palette_predictor_initializers_minus1 = sps->sps_num_palette_predictor_initializer_minus1, | |
| 1032 | ✗ | .conf_win_left_offset = sps->conf_win_left_offset, | |
| 1033 | ✗ | .conf_win_right_offset = sps->conf_win_right_offset, | |
| 1034 | ✗ | .conf_win_top_offset = sps->conf_win_top_offset, | |
| 1035 | ✗ | .conf_win_bottom_offset = sps->conf_win_bottom_offset, | |
| 1036 | .pProfileTierLevel = ptl_sps, | ||
| 1037 | .pDecPicBufMgr = dpbm_sps, | ||
| 1038 | .pScalingLists = NULL, | ||
| 1039 | .pShortTermRefPicSet = str, | ||
| 1040 | .pLongTermRefPicsSps = ltr, | ||
| 1041 | .pSequenceParameterSetVui = vui_sps, | ||
| 1042 | .pPredictorPaletteEntries = NULL, | ||
| 1043 | }; | ||
| 1044 | |||
| 1045 | /* PPS */ | ||
| 1046 | ✗ | *vkpps = (StdVideoH265PictureParameterSet) { | |
| 1047 | .flags = (StdVideoH265PpsFlags) { | ||
| 1048 | ✗ | .dependent_slice_segments_enabled_flag = pps->dependent_slice_segments_enabled_flag, | |
| 1049 | ✗ | .output_flag_present_flag = pps->output_flag_present_flag, | |
| 1050 | ✗ | .sign_data_hiding_enabled_flag = pps->sign_data_hiding_enabled_flag, | |
| 1051 | ✗ | .cabac_init_present_flag = pps->cabac_init_present_flag, | |
| 1052 | ✗ | .constrained_intra_pred_flag = pps->constrained_intra_pred_flag, | |
| 1053 | ✗ | .transform_skip_enabled_flag = pps->transform_skip_enabled_flag, | |
| 1054 | ✗ | .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag, | |
| 1055 | ✗ | .pps_slice_chroma_qp_offsets_present_flag = pps->pps_slice_chroma_qp_offsets_present_flag, | |
| 1056 | ✗ | .weighted_pred_flag = pps->weighted_pred_flag, | |
| 1057 | ✗ | .weighted_bipred_flag = pps->weighted_bipred_flag, | |
| 1058 | ✗ | .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag, | |
| 1059 | ✗ | .tiles_enabled_flag = pps->tiles_enabled_flag, | |
| 1060 | ✗ | .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag, | |
| 1061 | ✗ | .uniform_spacing_flag = pps->uniform_spacing_flag, | |
| 1062 | ✗ | .loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag, | |
| 1063 | ✗ | .pps_loop_filter_across_slices_enabled_flag = pps->pps_loop_filter_across_slices_enabled_flag, | |
| 1064 | ✗ | .deblocking_filter_control_present_flag = pps->deblocking_filter_control_present_flag, | |
| 1065 | ✗ | .deblocking_filter_override_enabled_flag = pps->deblocking_filter_override_enabled_flag, | |
| 1066 | ✗ | .pps_deblocking_filter_disabled_flag = pps->pps_deblocking_filter_disabled_flag, | |
| 1067 | ✗ | .pps_scaling_list_data_present_flag = pps->pps_scaling_list_data_present_flag, | |
| 1068 | ✗ | .lists_modification_present_flag = pps->lists_modification_present_flag, | |
| 1069 | ✗ | .slice_segment_header_extension_present_flag = pps->slice_segment_header_extension_present_flag, | |
| 1070 | ✗ | .pps_extension_present_flag = pps->pps_extension_present_flag, | |
| 1071 | ✗ | .cross_component_prediction_enabled_flag = pps->cross_component_prediction_enabled_flag, | |
| 1072 | ✗ | .chroma_qp_offset_list_enabled_flag = pps->chroma_qp_offset_list_enabled_flag, | |
| 1073 | ✗ | .pps_curr_pic_ref_enabled_flag = pps->pps_curr_pic_ref_enabled_flag, | |
| 1074 | ✗ | .residual_adaptive_colour_transform_enabled_flag = pps->residual_adaptive_colour_transform_enabled_flag, | |
| 1075 | ✗ | .pps_slice_act_qp_offsets_present_flag = pps->pps_slice_act_qp_offsets_present_flag, | |
| 1076 | ✗ | .pps_palette_predictor_initializers_present_flag = pps->pps_palette_predictor_initializer_present_flag, | |
| 1077 | ✗ | .monochrome_palette_flag = pps->monochrome_palette_flag, | |
| 1078 | ✗ | .pps_range_extension_flag = pps->pps_range_extension_flag, | |
| 1079 | }, | ||
| 1080 | ✗ | .pps_pic_parameter_set_id = pps->pps_pic_parameter_set_id, | |
| 1081 | ✗ | .pps_seq_parameter_set_id = pps->pps_seq_parameter_set_id, | |
| 1082 | ✗ | .sps_video_parameter_set_id = sps->sps_video_parameter_set_id, | |
| 1083 | ✗ | .num_extra_slice_header_bits = pps->num_extra_slice_header_bits, | |
| 1084 | ✗ | .num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active_minus1, | |
| 1085 | ✗ | .num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active_minus1, | |
| 1086 | ✗ | .init_qp_minus26 = pps->init_qp_minus26, | |
| 1087 | ✗ | .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth, | |
| 1088 | ✗ | .pps_cb_qp_offset = pps->pps_cb_qp_offset, | |
| 1089 | ✗ | .pps_cr_qp_offset = pps->pps_cr_qp_offset, | |
| 1090 | ✗ | .pps_beta_offset_div2 = pps->pps_beta_offset_div2, | |
| 1091 | ✗ | .pps_tc_offset_div2 = pps->pps_tc_offset_div2, | |
| 1092 | ✗ | .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2, | |
| 1093 | ✗ | .log2_max_transform_skip_block_size_minus2 = pps->log2_max_transform_skip_block_size_minus2, | |
| 1094 | ✗ | .diff_cu_chroma_qp_offset_depth = pps->diff_cu_chroma_qp_offset_depth, | |
| 1095 | ✗ | .chroma_qp_offset_list_len_minus1 = pps->chroma_qp_offset_list_len_minus1, | |
| 1096 | ✗ | .log2_sao_offset_scale_luma = pps->log2_sao_offset_scale_luma, | |
| 1097 | ✗ | .log2_sao_offset_scale_chroma = pps->log2_sao_offset_scale_chroma, | |
| 1098 | ✗ | .pps_act_y_qp_offset_plus5 = pps->pps_act_y_qp_offset_plus5, | |
| 1099 | ✗ | .pps_act_cb_qp_offset_plus5 = pps->pps_act_cb_qp_offset_plus5, | |
| 1100 | ✗ | .pps_act_cr_qp_offset_plus3 = pps->pps_act_cr_qp_offset_plus3, | |
| 1101 | ✗ | .pps_num_palette_predictor_initializers = pps->pps_num_palette_predictor_initializer, | |
| 1102 | ✗ | .luma_bit_depth_entry_minus8 = pps->luma_bit_depth_entry_minus8, | |
| 1103 | ✗ | .chroma_bit_depth_entry_minus8 = pps->chroma_bit_depth_entry_minus8, | |
| 1104 | ✗ | .num_tile_columns_minus1 = pps->num_tile_columns_minus1, | |
| 1105 | ✗ | .num_tile_rows_minus1 = pps->num_tile_rows_minus1, | |
| 1106 | .pScalingLists = NULL, | ||
| 1107 | .pPredictorPaletteEntries = NULL, | ||
| 1108 | }; | ||
| 1109 | |||
| 1110 | ✗ | for (int i = 0; i < pps->num_tile_columns_minus1; i++) | |
| 1111 | ✗ | vkpps->column_width_minus1[i] = pps->column_width_minus1[i]; | |
| 1112 | |||
| 1113 | ✗ | for (int i = 0; i < pps->num_tile_rows_minus1; i++) | |
| 1114 | ✗ | vkpps->row_height_minus1[i] = pps->row_height_minus1[i]; | |
| 1115 | |||
| 1116 | ✗ | for (int i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) { | |
| 1117 | ✗ | vkpps->cb_qp_offset_list[i] = pps->cb_qp_offset_list[i]; | |
| 1118 | ✗ | vkpps->cr_qp_offset_list[i] = pps->cr_qp_offset_list[i]; | |
| 1119 | } | ||
| 1120 | |||
| 1121 | /* VPS */ | ||
| 1122 | ✗ | for (int i = 0; i < STD_VIDEO_H265_SUBLAYERS_LIST_SIZE; i++) { | |
| 1123 | ✗ | dpbm_vps->max_latency_increase_plus1[i] = vps->vps_max_latency_increase_plus1[i]; | |
| 1124 | ✗ | dpbm_vps->max_dec_pic_buffering_minus1[i] = vps->vps_max_dec_pic_buffering_minus1[i]; | |
| 1125 | ✗ | dpbm_vps->max_num_reorder_pics[i] = vps->vps_max_num_reorder_pics[i]; | |
| 1126 | } | ||
| 1127 | |||
| 1128 | ✗ | *ptl_vps = (StdVideoH265ProfileTierLevel) { | |
| 1129 | .flags = (StdVideoH265ProfileTierLevelFlags) { | ||
| 1130 | ✗ | .general_tier_flag = vps->profile_tier_level.general_tier_flag, | |
| 1131 | ✗ | .general_progressive_source_flag = vps->profile_tier_level.general_progressive_source_flag, | |
| 1132 | ✗ | .general_interlaced_source_flag = vps->profile_tier_level.general_interlaced_source_flag, | |
| 1133 | ✗ | .general_non_packed_constraint_flag = vps->profile_tier_level.general_non_packed_constraint_flag, | |
| 1134 | ✗ | .general_frame_only_constraint_flag = vps->profile_tier_level.general_frame_only_constraint_flag, | |
| 1135 | }, | ||
| 1136 | ✗ | .general_profile_idc = ff_vk_h265_profile_to_vk(vps->profile_tier_level.general_profile_idc), | |
| 1137 | ✗ | .general_level_idc = ff_vk_h265_level_to_vk(vps->profile_tier_level.general_level_idc), | |
| 1138 | }; | ||
| 1139 | |||
| 1140 | ✗ | *vkvps = (StdVideoH265VideoParameterSet) { | |
| 1141 | .flags = (StdVideoH265VpsFlags) { | ||
| 1142 | ✗ | .vps_temporal_id_nesting_flag = vps->vps_temporal_id_nesting_flag, | |
| 1143 | ✗ | .vps_sub_layer_ordering_info_present_flag = vps->vps_sub_layer_ordering_info_present_flag, | |
| 1144 | ✗ | .vps_timing_info_present_flag = vps->vps_timing_info_present_flag, | |
| 1145 | ✗ | .vps_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag, | |
| 1146 | }, | ||
| 1147 | ✗ | .vps_video_parameter_set_id = vps->vps_video_parameter_set_id, | |
| 1148 | ✗ | .vps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1, | |
| 1149 | /* Reserved */ | ||
| 1150 | /* Reserved */ | ||
| 1151 | ✗ | .vps_num_units_in_tick = vps->vps_num_units_in_tick, | |
| 1152 | ✗ | .vps_time_scale = vps->vps_time_scale, | |
| 1153 | ✗ | .vps_num_ticks_poc_diff_one_minus1 = vps->vps_num_ticks_poc_diff_one_minus1, | |
| 1154 | /* Reserved */ | ||
| 1155 | .pDecPicBufMgr = dpbm_vps, | ||
| 1156 | .pHrdParameters = vui_header_vps, | ||
| 1157 | .pProfileTierLevel = ptl_vps, | ||
| 1158 | }; | ||
| 1159 | |||
| 1160 | ✗ | return 0; | |
| 1161 | } | ||
| 1162 | |||
| 1163 | ✗ | static int create_session_params(AVCodecContext *avctx) | |
| 1164 | { | ||
| 1165 | int err; | ||
| 1166 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 1167 | ✗ | FFVulkanEncodeContext *ctx = &enc->common; | |
| 1168 | ✗ | FFVulkanContext *s = &ctx->s; | |
| 1169 | ✗ | FFVulkanFunctions *vk = &ctx->s.vkfn; | |
| 1170 | |||
| 1171 | ✗ | VulkanH265Units vk_units = { 0 }; | |
| 1172 | |||
| 1173 | VkVideoEncodeH265SessionParametersAddInfoKHR h265_params_info; | ||
| 1174 | VkVideoEncodeH265SessionParametersCreateInfoKHR h265_params; | ||
| 1175 | |||
| 1176 | /* Convert it to Vulkan */ | ||
| 1177 | ✗ | err = base_unit_to_vk(avctx, &vk_units); | |
| 1178 | ✗ | if (err < 0) { | |
| 1179 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unable to convert SPS/PPS units to Vulkan: %s\n", | |
| 1180 | ✗ | av_err2str(err)); | |
| 1181 | ✗ | return err; | |
| 1182 | } | ||
| 1183 | |||
| 1184 | /* Destroy the session params */ | ||
| 1185 | ✗ | if (ctx->session_params) | |
| 1186 | ✗ | vk->DestroyVideoSessionParametersKHR(s->hwctx->act_dev, | |
| 1187 | ctx->session_params, | ||
| 1188 | ✗ | s->hwctx->alloc); | |
| 1189 | |||
| 1190 | ✗ | h265_params_info = (VkVideoEncodeH265SessionParametersAddInfoKHR) { | |
| 1191 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_SESSION_PARAMETERS_ADD_INFO_KHR, | ||
| 1192 | .pStdSPSs = &vk_units.sps, | ||
| 1193 | .stdSPSCount = 1, | ||
| 1194 | .pStdPPSs = &vk_units.pps, | ||
| 1195 | .stdPPSCount = 1, | ||
| 1196 | .pStdVPSs = &vk_units.vps, | ||
| 1197 | .stdVPSCount = 1, | ||
| 1198 | }; | ||
| 1199 | ✗ | h265_params = (VkVideoEncodeH265SessionParametersCreateInfoKHR) { | |
| 1200 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_SESSION_PARAMETERS_CREATE_INFO_KHR, | ||
| 1201 | .maxStdSPSCount = 1, | ||
| 1202 | .maxStdPPSCount = 1, | ||
| 1203 | .maxStdVPSCount = 1, | ||
| 1204 | .pParametersAddInfo = &h265_params_info, | ||
| 1205 | }; | ||
| 1206 | |||
| 1207 | ✗ | return ff_vulkan_encode_create_session_params(avctx, ctx, &h265_params); | |
| 1208 | } | ||
| 1209 | |||
| 1210 | ✗ | static int parse_feedback_units(AVCodecContext *avctx, | |
| 1211 | const uint8_t *data, size_t size, | ||
| 1212 | int sps_override, int pps_override) | ||
| 1213 | { | ||
| 1214 | int err; | ||
| 1215 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 1216 | |||
| 1217 | CodedBitstreamContext *cbs; | ||
| 1218 | ✗ | CodedBitstreamFragment au = { 0 }; | |
| 1219 | |||
| 1220 | ✗ | err = ff_cbs_init(&cbs, AV_CODEC_ID_HEVC, avctx); | |
| 1221 | ✗ | if (err < 0) | |
| 1222 | ✗ | return err; | |
| 1223 | |||
| 1224 | ✗ | err = ff_cbs_read(cbs, &au, NULL, data, size); | |
| 1225 | ✗ | if (err < 0) { | |
| 1226 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unable to parse feedback units, bad drivers: %s\n", | |
| 1227 | ✗ | av_err2str(err)); | |
| 1228 | ✗ | goto fail; | |
| 1229 | } | ||
| 1230 | |||
| 1231 | ✗ | if (sps_override) { | |
| 1232 | ✗ | for (int i = 0; i < au.nb_units; i++) { | |
| 1233 | ✗ | if (au.units[i].type == HEVC_NAL_SPS) { | |
| 1234 | ✗ | H265RawSPS *sps = au.units[i].content; | |
| 1235 | ✗ | enc->units.raw_sps.pic_width_in_luma_samples = sps->pic_width_in_luma_samples; | |
| 1236 | ✗ | enc->units.raw_sps.pic_height_in_luma_samples = sps->pic_height_in_luma_samples; | |
| 1237 | ✗ | enc->units.raw_sps.conformance_window_flag = sps->conformance_window_flag; | |
| 1238 | ✗ | enc->units.raw_sps.conf_win_left_offset = sps->conf_win_left_offset; | |
| 1239 | ✗ | enc->units.raw_sps.conf_win_right_offset = sps->conf_win_right_offset; | |
| 1240 | ✗ | enc->units.raw_sps.conf_win_top_offset = sps->conf_win_top_offset; | |
| 1241 | ✗ | enc->units.raw_sps.conf_win_bottom_offset = sps->conf_win_bottom_offset; | |
| 1242 | ✗ | enc->units.raw_sps.log2_min_luma_coding_block_size_minus3 = | |
| 1243 | ✗ | sps->log2_min_luma_coding_block_size_minus3; | |
| 1244 | ✗ | enc->units.raw_sps.log2_diff_max_min_luma_coding_block_size = | |
| 1245 | ✗ | sps->log2_diff_max_min_luma_coding_block_size; | |
| 1246 | ✗ | enc->units.raw_sps.log2_min_luma_transform_block_size_minus2 = | |
| 1247 | ✗ | sps->log2_min_luma_transform_block_size_minus2; | |
| 1248 | ✗ | enc->units.raw_sps.log2_diff_max_min_luma_transform_block_size = | |
| 1249 | ✗ | sps->log2_diff_max_min_luma_transform_block_size; | |
| 1250 | ✗ | enc->units.raw_sps.max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter; | |
| 1251 | ✗ | enc->units.raw_sps.max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra; | |
| 1252 | } | ||
| 1253 | } | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | /* If PPS has an override, just copy it entirely. */ | ||
| 1257 | ✗ | if (pps_override) { | |
| 1258 | ✗ | for (int i = 0; i < au.nb_units; i++) { | |
| 1259 | ✗ | if (au.units[i].type == HEVC_NAL_PPS) { | |
| 1260 | ✗ | H265RawPPS *pps = au.units[i].content; | |
| 1261 | ✗ | memcpy(&enc->units.raw_pps, pps, sizeof(*pps)); | |
| 1262 | ✗ | enc->fixed_qp_idr = pps->init_qp_minus26 + 26; | |
| 1263 | ✗ | break; | |
| 1264 | } | ||
| 1265 | } | ||
| 1266 | } | ||
| 1267 | |||
| 1268 | ✗ | err = 0; | |
| 1269 | ✗ | fail: | |
| 1270 | ✗ | ff_cbs_fragment_free(&au); | |
| 1271 | ✗ | ff_cbs_close(&cbs); | |
| 1272 | |||
| 1273 | ✗ | return err; | |
| 1274 | } | ||
| 1275 | |||
| 1276 | ✗ | static int init_base_units(AVCodecContext *avctx) | |
| 1277 | { | ||
| 1278 | int err; | ||
| 1279 | VkResult ret; | ||
| 1280 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 1281 | ✗ | FFVulkanEncodeContext *ctx = &enc->common; | |
| 1282 | ✗ | FFVulkanContext *s = &ctx->s; | |
| 1283 | ✗ | FFVulkanFunctions *vk = &ctx->s.vkfn; | |
| 1284 | |||
| 1285 | VkVideoEncodeH265SessionParametersGetInfoKHR h265_params_info; | ||
| 1286 | VkVideoEncodeSessionParametersGetInfoKHR params_info; | ||
| 1287 | VkVideoEncodeH265SessionParametersFeedbackInfoKHR h265_params_feedback; | ||
| 1288 | VkVideoEncodeSessionParametersFeedbackInfoKHR params_feedback; | ||
| 1289 | |||
| 1290 | ✗ | void *data = NULL; | |
| 1291 | ✗ | size_t data_size = 0; | |
| 1292 | |||
| 1293 | /* Generate SPS/PPS unit info */ | ||
| 1294 | ✗ | err = init_sequence_headers(avctx); | |
| 1295 | ✗ | if (err < 0) { | |
| 1296 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unable to initialize SPS/PPS units: %s\n", | |
| 1297 | ✗ | av_err2str(err)); | |
| 1298 | ✗ | return err; | |
| 1299 | } | ||
| 1300 | |||
| 1301 | /* Create session parameters from them */ | ||
| 1302 | ✗ | err = create_session_params(avctx); | |
| 1303 | ✗ | if (err < 0) | |
| 1304 | ✗ | return err; | |
| 1305 | |||
| 1306 | ✗ | h265_params_info = (VkVideoEncodeH265SessionParametersGetInfoKHR) { | |
| 1307 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_SESSION_PARAMETERS_GET_INFO_KHR, | ||
| 1308 | .writeStdSPS = 1, | ||
| 1309 | .writeStdPPS = 1, | ||
| 1310 | .writeStdVPS = 1, | ||
| 1311 | ✗ | .stdSPSId = enc->units.raw_sps.sps_seq_parameter_set_id, | |
| 1312 | ✗ | .stdPPSId = enc->units.raw_pps.pps_pic_parameter_set_id, | |
| 1313 | ✗ | .stdVPSId = enc->units.raw_vps.vps_video_parameter_set_id, | |
| 1314 | }; | ||
| 1315 | ✗ | params_info = (VkVideoEncodeSessionParametersGetInfoKHR) { | |
| 1316 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_SESSION_PARAMETERS_GET_INFO_KHR, | ||
| 1317 | .pNext = &h265_params_info, | ||
| 1318 | ✗ | .videoSessionParameters = ctx->session_params, | |
| 1319 | }; | ||
| 1320 | |||
| 1321 | ✗ | h265_params_feedback = (VkVideoEncodeH265SessionParametersFeedbackInfoKHR) { | |
| 1322 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_SESSION_PARAMETERS_FEEDBACK_INFO_KHR, | ||
| 1323 | }; | ||
| 1324 | ✗ | params_feedback = (VkVideoEncodeSessionParametersFeedbackInfoKHR) { | |
| 1325 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_SESSION_PARAMETERS_FEEDBACK_INFO_KHR, | ||
| 1326 | .pNext = &h265_params_feedback, | ||
| 1327 | }; | ||
| 1328 | |||
| 1329 | ✗ | ret = vk->GetEncodedVideoSessionParametersKHR(s->hwctx->act_dev, ¶ms_info, | |
| 1330 | ¶ms_feedback, | ||
| 1331 | &data_size, data); | ||
| 1332 | ✗ | if (ret == VK_INCOMPLETE || | |
| 1333 | ✗ | (ret == VK_SUCCESS) && (data_size > 0)) { | |
| 1334 | ✗ | data = av_mallocz(data_size); | |
| 1335 | ✗ | if (!data) | |
| 1336 | ✗ | return AVERROR(ENOMEM); | |
| 1337 | } else { | ||
| 1338 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unable to get feedback for H.265 units = %zu\n", data_size); | |
| 1339 | ✗ | return AVERROR_EXTERNAL; | |
| 1340 | } | ||
| 1341 | |||
| 1342 | ✗ | ret = vk->GetEncodedVideoSessionParametersKHR(s->hwctx->act_dev, ¶ms_info, | |
| 1343 | ¶ms_feedback, | ||
| 1344 | &data_size, data); | ||
| 1345 | ✗ | if (ret != VK_SUCCESS) { | |
| 1346 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error writing feedback units\n"); | |
| 1347 | ✗ | err = AVERROR_EXTERNAL; | |
| 1348 | ✗ | goto end; | |
| 1349 | } | ||
| 1350 | |||
| 1351 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Feedback units written, overrides: %i (SPS: %i PPS: %i VPS: %i)\n", | |
| 1352 | params_feedback.hasOverrides, | ||
| 1353 | h265_params_feedback.hasStdSPSOverrides, | ||
| 1354 | h265_params_feedback.hasStdPPSOverrides, | ||
| 1355 | h265_params_feedback.hasStdVPSOverrides); | ||
| 1356 | |||
| 1357 | ✗ | params_feedback.hasOverrides = 1; | |
| 1358 | ✗ | h265_params_feedback.hasStdSPSOverrides = 1; | |
| 1359 | ✗ | h265_params_feedback.hasStdPPSOverrides = 1; | |
| 1360 | |||
| 1361 | /* No need to sync any overrides */ | ||
| 1362 | ✗ | err = 0; | |
| 1363 | ✗ | if (!params_feedback.hasOverrides) | |
| 1364 | ✗ | goto end; | |
| 1365 | |||
| 1366 | /* Parse back tne units and override */ | ||
| 1367 | ✗ | err = parse_feedback_units(avctx, data, data_size, | |
| 1368 | ✗ | h265_params_feedback.hasStdSPSOverrides, | |
| 1369 | ✗ | h265_params_feedback.hasStdPPSOverrides); | |
| 1370 | ✗ | if (err < 0) | |
| 1371 | ✗ | goto end; | |
| 1372 | |||
| 1373 | /* Create final session parameters */ | ||
| 1374 | ✗ | err = create_session_params(avctx); | |
| 1375 | |||
| 1376 | ✗ | end: | |
| 1377 | ✗ | av_free(data); | |
| 1378 | ✗ | return err; | |
| 1379 | } | ||
| 1380 | |||
| 1381 | ✗ | static int vulkan_encode_h265_add_nal(AVCodecContext *avctx, | |
| 1382 | CodedBitstreamFragment *au, | ||
| 1383 | void *nal_unit) | ||
| 1384 | { | ||
| 1385 | ✗ | H265RawNALUnitHeader *header = nal_unit; | |
| 1386 | |||
| 1387 | ✗ | int err = ff_cbs_insert_unit_content(au, -1, | |
| 1388 | ✗ | header->nal_unit_type, nal_unit, NULL); | |
| 1389 | ✗ | if (err < 0) | |
| 1390 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: " | |
| 1391 | ✗ | "type = %d.\n", header->nal_unit_type); | |
| 1392 | |||
| 1393 | ✗ | return err; | |
| 1394 | } | ||
| 1395 | |||
| 1396 | ✗ | static int write_access_unit(AVCodecContext *avctx, | |
| 1397 | uint8_t *data, size_t *data_len, | ||
| 1398 | CodedBitstreamFragment *au) | ||
| 1399 | { | ||
| 1400 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 1401 | |||
| 1402 | ✗ | int err = ff_cbs_write_fragment_data(enc->cbs, au); | |
| 1403 | ✗ | if (err < 0) { | |
| 1404 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n"); | |
| 1405 | ✗ | return err; | |
| 1406 | } | ||
| 1407 | |||
| 1408 | ✗ | if (*data_len < au->data_size) { | |
| 1409 | ✗ | av_log(avctx, AV_LOG_ERROR, "Access unit too large: %zu < %zu.\n", | |
| 1410 | *data_len, au->data_size); | ||
| 1411 | ✗ | return AVERROR(ENOSPC); | |
| 1412 | } | ||
| 1413 | |||
| 1414 | ✗ | memcpy(data, au->data, au->data_size); | |
| 1415 | ✗ | *data_len = au->data_size; | |
| 1416 | |||
| 1417 | ✗ | return 0; | |
| 1418 | } | ||
| 1419 | |||
| 1420 | ✗ | static int write_sequence_headers(AVCodecContext *avctx, | |
| 1421 | FFHWBaseEncodePicture *base_pic, | ||
| 1422 | uint8_t *data, size_t *data_len) | ||
| 1423 | { | ||
| 1424 | int err; | ||
| 1425 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 1426 | ✗ | VulkanEncodeH265Picture *hp = base_pic ? base_pic->codec_priv : NULL; | |
| 1427 | ✗ | CodedBitstreamFragment *au = &enc->current_access_unit; | |
| 1428 | |||
| 1429 | ✗ | if (hp && hp->units_needed & UNIT_AUD) { | |
| 1430 | ✗ | err = vulkan_encode_h265_add_nal(avctx, au, &enc->raw_aud); | |
| 1431 | ✗ | if (err < 0) | |
| 1432 | ✗ | goto fail; | |
| 1433 | ✗ | hp->units_needed &= ~UNIT_AUD; | |
| 1434 | } | ||
| 1435 | |||
| 1436 | ✗ | err = vulkan_encode_h265_add_nal(avctx, au, &enc->units.raw_vps); | |
| 1437 | ✗ | if (err < 0) | |
| 1438 | ✗ | goto fail; | |
| 1439 | |||
| 1440 | ✗ | err = vulkan_encode_h265_add_nal(avctx, au, &enc->units.raw_sps); | |
| 1441 | ✗ | if (err < 0) | |
| 1442 | ✗ | goto fail; | |
| 1443 | |||
| 1444 | ✗ | err = vulkan_encode_h265_add_nal(avctx, au, &enc->units.raw_pps); | |
| 1445 | ✗ | if (err < 0) | |
| 1446 | ✗ | goto fail; | |
| 1447 | |||
| 1448 | ✗ | err = write_access_unit(avctx, data, data_len, au); | |
| 1449 | ✗ | fail: | |
| 1450 | ✗ | ff_cbs_fragment_reset(au); | |
| 1451 | ✗ | return err; | |
| 1452 | } | ||
| 1453 | |||
| 1454 | ✗ | static int write_extra_headers(AVCodecContext *avctx, | |
| 1455 | FFHWBaseEncodePicture *base_pic, | ||
| 1456 | uint8_t *data, size_t *data_len) | ||
| 1457 | { | ||
| 1458 | int err; | ||
| 1459 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 1460 | ✗ | VulkanEncodeH265Picture *hp = base_pic->codec_priv; | |
| 1461 | ✗ | CodedBitstreamFragment *au = &enc->current_access_unit; | |
| 1462 | |||
| 1463 | ✗ | if (hp->units_needed & UNIT_AUD) { | |
| 1464 | ✗ | err = vulkan_encode_h265_add_nal(avctx, au, &enc->raw_aud); | |
| 1465 | ✗ | if (err < 0) | |
| 1466 | ✗ | goto fail; | |
| 1467 | } | ||
| 1468 | |||
| 1469 | ✗ | if (hp->units_needed & UNIT_SEI_MASTERING_DISPLAY) { | |
| 1470 | ✗ | err = ff_cbs_sei_add_message(enc->cbs, au, 1, | |
| 1471 | SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME, | ||
| 1472 | ✗ | &enc->sei_mastering_display, NULL); | |
| 1473 | ✗ | if (err < 0) | |
| 1474 | ✗ | goto fail; | |
| 1475 | } | ||
| 1476 | |||
| 1477 | ✗ | if (hp->units_needed & UNIT_SEI_CONTENT_LIGHT_LEVEL) { | |
| 1478 | ✗ | err = ff_cbs_sei_add_message(enc->cbs, au, 1, | |
| 1479 | SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO, | ||
| 1480 | ✗ | &enc->sei_content_light_level, NULL); | |
| 1481 | ✗ | if (err < 0) | |
| 1482 | ✗ | goto fail; | |
| 1483 | } | ||
| 1484 | ✗ | if (hp->units_needed & UNIT_SEI_A53_CC) { | |
| 1485 | ✗ | err = ff_cbs_sei_add_message(enc->cbs, au, 1, | |
| 1486 | SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35, | ||
| 1487 | ✗ | &enc->sei_a53cc, NULL); | |
| 1488 | ✗ | if (err < 0) | |
| 1489 | ✗ | goto fail; | |
| 1490 | } | ||
| 1491 | |||
| 1492 | ✗ | if (hp->units_needed) { | |
| 1493 | ✗ | err = write_access_unit(avctx, data, data_len, au); | |
| 1494 | ✗ | if (err < 0) | |
| 1495 | ✗ | goto fail; | |
| 1496 | } else { | ||
| 1497 | ✗ | err = 0; | |
| 1498 | ✗ | *data_len = 0; | |
| 1499 | } | ||
| 1500 | |||
| 1501 | ✗ | fail: | |
| 1502 | ✗ | ff_cbs_fragment_reset(au); | |
| 1503 | ✗ | return err; | |
| 1504 | } | ||
| 1505 | |||
| 1506 | ✗ | static int write_filler(AVCodecContext *avctx, uint32_t filler, | |
| 1507 | uint8_t *data, size_t *data_len) | ||
| 1508 | { | ||
| 1509 | int err; | ||
| 1510 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 1511 | ✗ | CodedBitstreamFragment *au = &enc->current_access_unit; | |
| 1512 | |||
| 1513 | ✗ | H265RawFiller raw_filler = { | |
| 1514 | .nal_unit_header = | ||
| 1515 | { | ||
| 1516 | .nal_unit_type = HEVC_NAL_FD_NUT, | ||
| 1517 | .nuh_temporal_id_plus1 = 1, | ||
| 1518 | }, | ||
| 1519 | .filler_size = filler, | ||
| 1520 | }; | ||
| 1521 | |||
| 1522 | ✗ | err = vulkan_encode_h265_add_nal(avctx, au, &raw_filler); | |
| 1523 | ✗ | if (err < 0) | |
| 1524 | ✗ | goto fail; | |
| 1525 | |||
| 1526 | ✗ | err = write_access_unit(avctx, data, data_len, au); | |
| 1527 | ✗ | fail: | |
| 1528 | ✗ | ff_cbs_fragment_reset(au); | |
| 1529 | ✗ | return err; | |
| 1530 | } | ||
| 1531 | |||
| 1532 | static const FFVulkanCodec enc_cb = { | ||
| 1533 | .flags = FF_HW_FLAG_B_PICTURES | | ||
| 1534 | FF_HW_FLAG_B_PICTURE_REFERENCES | | ||
| 1535 | FF_HW_FLAG_NON_IDR_KEY_PICTURES, | ||
| 1536 | .picture_priv_data_size = sizeof(VulkanEncodeH265Picture), | ||
| 1537 | .filler_header_size = 7, | ||
| 1538 | .init_profile = init_profile, | ||
| 1539 | .init_pic_rc = init_pic_rc, | ||
| 1540 | .init_pic_params = init_pic_params, | ||
| 1541 | .write_sequence_headers = write_sequence_headers, | ||
| 1542 | .write_extra_headers = write_extra_headers, | ||
| 1543 | .write_filler = write_filler, | ||
| 1544 | }; | ||
| 1545 | |||
| 1546 | ✗ | static av_cold int vulkan_encode_h265_init(AVCodecContext *avctx) | |
| 1547 | { | ||
| 1548 | int err, ref_l0, ref_l1; | ||
| 1549 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 1550 | ✗ | FFVulkanEncodeContext *ctx = &enc->common; | |
| 1551 | ✗ | FFHWBaseEncodeContext *base_ctx = &ctx->base; | |
| 1552 | int flags; | ||
| 1553 | |||
| 1554 | ✗ | if (avctx->profile == AV_PROFILE_UNKNOWN) | |
| 1555 | ✗ | avctx->profile = enc->common.opts.profile; | |
| 1556 | |||
| 1557 | ✗ | enc->caps = (VkVideoEncodeH265CapabilitiesKHR) { | |
| 1558 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_CAPABILITIES_KHR, | ||
| 1559 | }; | ||
| 1560 | |||
| 1561 | ✗ | enc->quality_props = (VkVideoEncodeH265QualityLevelPropertiesKHR) { | |
| 1562 | .sType = VK_STRUCTURE_TYPE_VIDEO_ENCODE_H265_QUALITY_LEVEL_PROPERTIES_KHR, | ||
| 1563 | }; | ||
| 1564 | |||
| 1565 | ✗ | err = ff_vulkan_encode_init(avctx, &enc->common, | |
| 1566 | &ff_vk_enc_h265_desc, &enc_cb, | ||
| 1567 | ✗ | &enc->caps, &enc->quality_props); | |
| 1568 | ✗ | if (err < 0) | |
| 1569 | ✗ | return err; | |
| 1570 | |||
| 1571 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "H265 encoder capabilities:\n"); | |
| 1572 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " Standard capability flags:\n"); | |
| 1573 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " separate_color_plane: %i\n", | |
| 1574 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_SEPARATE_COLOR_PLANE_FLAG_SET_BIT_KHR)); | |
| 1575 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " sample_adaptive_offset: %i\n", | |
| 1576 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_SAMPLE_ADAPTIVE_OFFSET_ENABLED_FLAG_SET_BIT_KHR)); | |
| 1577 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " scaling_lists: %i\n", | |
| 1578 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_SCALING_LIST_DATA_PRESENT_FLAG_SET_BIT_KHR)); | |
| 1579 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " pcm: %i\n", | |
| 1580 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_PCM_ENABLED_FLAG_SET_BIT_KHR)); | |
| 1581 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " temporal_mvp: %i\n", | |
| 1582 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_SPS_TEMPORAL_MVP_ENABLED_FLAG_SET_BIT_KHR)); | |
| 1583 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " init_qp: %i\n", | |
| 1584 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_INIT_QP_MINUS26_BIT_KHR)); | |
| 1585 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " weighted:%s%s\n", | |
| 1586 | ✗ | enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_WEIGHTED_PRED_FLAG_SET_BIT_KHR ? | |
| 1587 | " pred" : "", | ||
| 1588 | ✗ | enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_WEIGHTED_BIPRED_FLAG_SET_BIT_KHR ? | |
| 1589 | " bipred" : ""); | ||
| 1590 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " parallel_merge_level: %i\n", | |
| 1591 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_LOG2_PARALLEL_MERGE_LEVEL_MINUS2_BIT_KHR)); | |
| 1592 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " sign_data_hiding: %i\n", | |
| 1593 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_SIGN_DATA_HIDING_ENABLED_FLAG_SET_BIT_KHR)); | |
| 1594 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " transform_skip:%s%s\n", | |
| 1595 | ✗ | enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_TRANSFORM_SKIP_ENABLED_FLAG_SET_BIT_KHR ? | |
| 1596 | " set" : "", | ||
| 1597 | ✗ | enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_TRANSFORM_SKIP_ENABLED_FLAG_UNSET_BIT_KHR ? | |
| 1598 | " unset" : ""); | ||
| 1599 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " slice_chroma_qp_offsets: %i\n", | |
| 1600 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_PPS_SLICE_CHROMA_QP_OFFSETS_PRESENT_FLAG_SET_BIT_KHR)); | |
| 1601 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " transquant_bypass: %i\n", | |
| 1602 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_TRANSQUANT_BYPASS_ENABLED_FLAG_SET_BIT_KHR)); | |
| 1603 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " constrained_intra_pred: %i\n", | |
| 1604 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_CONSTRAINED_INTRA_PRED_FLAG_SET_BIT_KHR)); | |
| 1605 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " entrypy_coding_sync: %i\n", | |
| 1606 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_ENTROPY_CODING_SYNC_ENABLED_FLAG_SET_BIT_KHR)); | |
| 1607 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " dependent_slice_segment:%s%s\n", | |
| 1608 | ✗ | enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_DEPENDENT_SLICE_SEGMENTS_ENABLED_FLAG_SET_BIT_KHR ? | |
| 1609 | " enabled" : "", | ||
| 1610 | ✗ | enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_DEPENDENT_SLICE_SEGMENT_FLAG_SET_BIT_KHR ? | |
| 1611 | " set" : ""); | ||
| 1612 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " slice_qp_delta: %i\n", | |
| 1613 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_SLICE_QP_DELTA_BIT_KHR)); | |
| 1614 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " different_slice_qp_delta: %i\n", | |
| 1615 | ✗ | !!(enc->caps.stdSyntaxFlags & VK_VIDEO_ENCODE_H265_STD_DIFFERENT_SLICE_QP_DELTA_BIT_KHR)); | |
| 1616 | |||
| 1617 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " Capability flags:\n"); | |
| 1618 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " hdr_compliance: %i\n", | |
| 1619 | ✗ | !!(enc->caps.flags & VK_VIDEO_ENCODE_H265_CAPABILITY_HRD_COMPLIANCE_BIT_KHR)); | |
| 1620 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " pred_weight_table_generated: %i\n", | |
| 1621 | ✗ | !!(enc->caps.flags & VK_VIDEO_ENCODE_H265_CAPABILITY_PREDICTION_WEIGHT_TABLE_GENERATED_BIT_KHR)); | |
| 1622 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " row_unaligned_slice: %i\n", | |
| 1623 | ✗ | !!(enc->caps.flags & VK_VIDEO_ENCODE_H265_CAPABILITY_ROW_UNALIGNED_SLICE_SEGMENT_BIT_KHR)); | |
| 1624 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " different_slice_type: %i\n", | |
| 1625 | ✗ | !!(enc->caps.flags & VK_VIDEO_ENCODE_H265_CAPABILITY_DIFFERENT_SLICE_SEGMENT_TYPE_BIT_KHR)); | |
| 1626 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " b_frame_in_l0_list: %i\n", | |
| 1627 | ✗ | !!(enc->caps.flags & VK_VIDEO_ENCODE_H265_CAPABILITY_B_FRAME_IN_L0_LIST_BIT_KHR)); | |
| 1628 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " b_frame_in_l1_list: %i\n", | |
| 1629 | ✗ | !!(enc->caps.flags & VK_VIDEO_ENCODE_H265_CAPABILITY_B_FRAME_IN_L1_LIST_BIT_KHR)); | |
| 1630 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " per_pict_type_min_max_qp: %i\n", | |
| 1631 | ✗ | !!(enc->caps.flags & VK_VIDEO_ENCODE_H265_CAPABILITY_PER_PICTURE_TYPE_MIN_MAX_QP_BIT_KHR)); | |
| 1632 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " per_slice_constant_qp: %i\n", | |
| 1633 | ✗ | !!(enc->caps.flags & VK_VIDEO_ENCODE_H265_CAPABILITY_PER_SLICE_SEGMENT_CONSTANT_QP_BIT_KHR)); | |
| 1634 | |||
| 1635 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " Capabilities:\n"); | |
| 1636 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " maxLevelIdc: %i\n", | |
| 1637 | ✗ | enc->caps.maxLevelIdc); | |
| 1638 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " maxSliceCount: %i\n", | |
| 1639 | enc->caps.maxSliceSegmentCount); | ||
| 1640 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " maxTiles: %ix%i\n", | |
| 1641 | enc->caps.maxTiles.width, enc->caps.maxTiles.height); | ||
| 1642 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " cbtSizes: 0x%x\n", | |
| 1643 | enc->caps.ctbSizes); | ||
| 1644 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " transformBlockSizes: 0x%x\n", | |
| 1645 | enc->caps.transformBlockSizes); | ||
| 1646 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " max(P/B)PictureL0ReferenceCount: %i P's; %i B's\n", | |
| 1647 | enc->caps.maxPPictureL0ReferenceCount, | ||
| 1648 | enc->caps.maxBPictureL0ReferenceCount); | ||
| 1649 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " maxL1ReferenceCount: %i\n", | |
| 1650 | enc->caps.maxL1ReferenceCount); | ||
| 1651 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " maxSubLayerCount: %i\n", | |
| 1652 | enc->caps.maxSubLayerCount); | ||
| 1653 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " expectDyadicTemporalLayerPattern: %i\n", | |
| 1654 | enc->caps.expectDyadicTemporalSubLayerPattern); | ||
| 1655 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " min/max Qp: [%i, %i]\n", | |
| 1656 | enc->caps.minQp, enc->caps.maxQp); | ||
| 1657 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " prefersGopRemainingFrames: %i\n", | |
| 1658 | enc->caps.prefersGopRemainingFrames); | ||
| 1659 | ✗ | av_log(avctx, AV_LOG_VERBOSE, " requiresGopRemainingFrames: %i\n", | |
| 1660 | enc->caps.requiresGopRemainingFrames); | ||
| 1661 | |||
| 1662 | ✗ | err = init_enc_options(avctx); | |
| 1663 | ✗ | if (err < 0) | |
| 1664 | ✗ | return err; | |
| 1665 | |||
| 1666 | ✗ | flags = ctx->codec->flags; | |
| 1667 | ✗ | if (!enc->caps.maxPPictureL0ReferenceCount && | |
| 1668 | ✗ | !enc->caps.maxBPictureL0ReferenceCount && | |
| 1669 | ✗ | !enc->caps.maxL1ReferenceCount) { | |
| 1670 | /* Intra-only */ | ||
| 1671 | ✗ | flags |= FF_HW_FLAG_INTRA_ONLY; | |
| 1672 | ✗ | ref_l0 = ref_l1 = 0; | |
| 1673 | ✗ | } else if (!enc->caps.maxPPictureL0ReferenceCount) { | |
| 1674 | /* No P-frames? How. */ | ||
| 1675 | ✗ | base_ctx->p_to_gpb = 1; | |
| 1676 | ✗ | ref_l0 = enc->caps.maxBPictureL0ReferenceCount; | |
| 1677 | ✗ | ref_l1 = enc->caps.maxL1ReferenceCount; | |
| 1678 | ✗ | } else if (!enc->caps.maxBPictureL0ReferenceCount && | |
| 1679 | ✗ | !enc->caps.maxL1ReferenceCount) { | |
| 1680 | /* No B-frames */ | ||
| 1681 | ✗ | flags &= ~(FF_HW_FLAG_B_PICTURES | FF_HW_FLAG_B_PICTURE_REFERENCES); | |
| 1682 | ✗ | ref_l0 = enc->caps.maxPPictureL0ReferenceCount; | |
| 1683 | ✗ | ref_l1 = 0; | |
| 1684 | } else { | ||
| 1685 | /* P and B frames */ | ||
| 1686 | ✗ | ref_l0 = FFMIN(enc->caps.maxPPictureL0ReferenceCount, | |
| 1687 | enc->caps.maxBPictureL0ReferenceCount); | ||
| 1688 | ✗ | ref_l1 = enc->caps.maxL1ReferenceCount; | |
| 1689 | } | ||
| 1690 | |||
| 1691 | ✗ | err = ff_hw_base_init_gop_structure(base_ctx, avctx, ref_l0, ref_l1, | |
| 1692 | flags, 0); | ||
| 1693 | ✗ | if (err < 0) | |
| 1694 | ✗ | return err; | |
| 1695 | |||
| 1696 | ✗ | base_ctx->output_delay = base_ctx->b_per_p; | |
| 1697 | ✗ | base_ctx->decode_delay = base_ctx->max_b_depth; | |
| 1698 | |||
| 1699 | /* Init CBS */ | ||
| 1700 | ✗ | err = ff_cbs_init(&enc->cbs, AV_CODEC_ID_HEVC, avctx); | |
| 1701 | ✗ | if (err < 0) | |
| 1702 | ✗ | return err; | |
| 1703 | |||
| 1704 | /* Create units and session parameters */ | ||
| 1705 | ✗ | err = init_base_units(avctx); | |
| 1706 | ✗ | if (err < 0) | |
| 1707 | ✗ | return err; | |
| 1708 | |||
| 1709 | /* Write out extradata */ | ||
| 1710 | ✗ | err = ff_vulkan_write_global_header(avctx, &enc->common); | |
| 1711 | ✗ | if (err < 0) | |
| 1712 | ✗ | return err; | |
| 1713 | |||
| 1714 | ✗ | return 0; | |
| 1715 | } | ||
| 1716 | |||
| 1717 | ✗ | static av_cold int vulkan_encode_h265_close(AVCodecContext *avctx) | |
| 1718 | { | ||
| 1719 | ✗ | VulkanEncodeH265Context *enc = avctx->priv_data; | |
| 1720 | |||
| 1721 | ✗ | ff_cbs_fragment_free(&enc->current_access_unit); | |
| 1722 | ✗ | ff_cbs_close(&enc->cbs); | |
| 1723 | |||
| 1724 | ✗ | av_freep(&enc->sei_a53cc_data); | |
| 1725 | |||
| 1726 | ✗ | ff_vulkan_encode_uninit(&enc->common); | |
| 1727 | ✗ | return 0; | |
| 1728 | } | ||
| 1729 | |||
| 1730 | #define OFFSET(x) offsetof(VulkanEncodeH265Context, x) | ||
| 1731 | #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM) | ||
| 1732 | static const AVOption vulkan_encode_h265_options[] = { | ||
| 1733 | HW_BASE_ENCODE_COMMON_OPTIONS, | ||
| 1734 | VULKAN_ENCODE_COMMON_OPTIONS, | ||
| 1735 | |||
| 1736 | { "profile", "Set profile (profile_idc and constraint_set*_flag)", | ||
| 1737 | OFFSET(common.opts.profile), AV_OPT_TYPE_INT, | ||
| 1738 | { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xffff, FLAGS, .unit = "profile" }, | ||
| 1739 | |||
| 1740 | #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \ | ||
| 1741 | { .i64 = value }, 0, 0, FLAGS, .unit = "profile" | ||
| 1742 | { PROFILE("main", AV_PROFILE_HEVC_MAIN) }, | ||
| 1743 | { PROFILE("main10", AV_PROFILE_HEVC_MAIN_10) }, | ||
| 1744 | { PROFILE("rext", AV_PROFILE_HEVC_REXT) }, | ||
| 1745 | #undef PROFILE | ||
| 1746 | |||
| 1747 | { "tier", "Set tier (general_tier_flag)", OFFSET(common.opts.tier), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, .unit = "tier" }, | ||
| 1748 | { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, .unit = "tier" }, | ||
| 1749 | { "high", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, .unit = "tier" }, | ||
| 1750 | |||
| 1751 | { "level", "Set level (general_level_idc)", | ||
| 1752 | OFFSET(common.opts.level), AV_OPT_TYPE_INT, | ||
| 1753 | { .i64 = AV_LEVEL_UNKNOWN }, AV_LEVEL_UNKNOWN, 0xff, FLAGS, .unit = "level" }, | ||
| 1754 | |||
| 1755 | #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \ | ||
| 1756 | { .i64 = value }, 0, 0, FLAGS, .unit = "level" | ||
| 1757 | { LEVEL("1", 30) }, | ||
| 1758 | { LEVEL("2", 60) }, | ||
| 1759 | { LEVEL("2.1", 63) }, | ||
| 1760 | { LEVEL("3", 90) }, | ||
| 1761 | { LEVEL("3.1", 93) }, | ||
| 1762 | { LEVEL("4", 120) }, | ||
| 1763 | { LEVEL("4.1", 123) }, | ||
| 1764 | { LEVEL("5", 150) }, | ||
| 1765 | { LEVEL("5.1", 153) }, | ||
| 1766 | { LEVEL("5.2", 156) }, | ||
| 1767 | { LEVEL("6", 180) }, | ||
| 1768 | { LEVEL("6.1", 183) }, | ||
| 1769 | { LEVEL("6.2", 186) }, | ||
| 1770 | #undef LEVEL | ||
| 1771 | |||
| 1772 | { "units", "Set units to include", OFFSET(unit_elems), AV_OPT_TYPE_FLAGS, { .i64 = UNIT_SEI_MASTERING_DISPLAY | UNIT_SEI_CONTENT_LIGHT_LEVEL | UNIT_SEI_A53_CC }, 0, INT_MAX, FLAGS, "units" }, | ||
| 1773 | { "hdr", "Include HDR metadata for mastering display colour volume and content light level information", 0, AV_OPT_TYPE_CONST, { .i64 = UNIT_SEI_MASTERING_DISPLAY | UNIT_SEI_CONTENT_LIGHT_LEVEL }, INT_MIN, INT_MAX, FLAGS, "units" }, | ||
| 1774 | { "a53_cc", "Include A/53 caption data", 0, AV_OPT_TYPE_CONST, { .i64 = UNIT_SEI_A53_CC }, INT_MIN, INT_MAX, FLAGS, "units" }, | ||
| 1775 | |||
| 1776 | { NULL }, | ||
| 1777 | }; | ||
| 1778 | |||
| 1779 | static const FFCodecDefault vulkan_encode_h265_defaults[] = { | ||
| 1780 | { "b", "0" }, | ||
| 1781 | { "bf", "2" }, | ||
| 1782 | { "g", "300" }, | ||
| 1783 | { "i_qfactor", "1" }, | ||
| 1784 | { "i_qoffset", "0" }, | ||
| 1785 | { "b_qfactor", "6/5" }, | ||
| 1786 | { "b_qoffset", "0" }, | ||
| 1787 | { "qmin", "-1" }, | ||
| 1788 | { "qmax", "-1" }, | ||
| 1789 | { "refs", "0" }, | ||
| 1790 | { NULL }, | ||
| 1791 | }; | ||
| 1792 | |||
| 1793 | static const AVClass vulkan_encode_h265_class = { | ||
| 1794 | .class_name = "hevc_vulkan", | ||
| 1795 | .item_name = av_default_item_name, | ||
| 1796 | .option = vulkan_encode_h265_options, | ||
| 1797 | .version = LIBAVUTIL_VERSION_INT, | ||
| 1798 | }; | ||
| 1799 | |||
| 1800 | const FFCodec ff_hevc_vulkan_encoder = { | ||
| 1801 | .p.name = "hevc_vulkan", | ||
| 1802 | CODEC_LONG_NAME("H.265/HEVC (Vulkan)"), | ||
| 1803 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 1804 | .p.id = AV_CODEC_ID_HEVC, | ||
| 1805 | .priv_data_size = sizeof(VulkanEncodeH265Context), | ||
| 1806 | .init = &vulkan_encode_h265_init, | ||
| 1807 | FF_CODEC_RECEIVE_PACKET_CB(&ff_vulkan_encode_receive_packet), | ||
| 1808 | .close = &vulkan_encode_h265_close, | ||
| 1809 | .p.priv_class = &vulkan_encode_h265_class, | ||
| 1810 | .p.capabilities = AV_CODEC_CAP_DELAY | | ||
| 1811 | AV_CODEC_CAP_HARDWARE | | ||
| 1812 | AV_CODEC_CAP_DR1 | | ||
| 1813 | AV_CODEC_CAP_ENCODER_FLUSH | | ||
| 1814 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 1815 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 1816 | .defaults = vulkan_encode_h265_defaults, | ||
| 1817 | CODEC_PIXFMTS(AV_PIX_FMT_VULKAN), | ||
| 1818 | .hw_configs = ff_vulkan_encode_hw_configs, | ||
| 1819 | .p.wrapper_name = "vulkan", | ||
| 1820 | }; | ||
| 1821 |