22 #include <va/va_enc_hevc.h>
99 char *
data,
size_t *data_len,
113 "%zu < %zu.\n", *data_len,
135 "type = %d.\n",
header->nal_unit_type);
143 char *
data,
size_t *data_len)
177 char *
data,
size_t *data_len)
203 char *
data,
size_t *data_len)
248 *
type = VAEncPackedHeaderRawData;
267 VAEncSequenceParameterBufferHEVC *vseq =
ctx->codec_sequence_params;
268 VAEncPictureParameterBufferHEVC *vpic =
ctx->codec_picture_params;
289 #if VA_CHECK_VERSION(1, 13, 0)
292 VAConfigAttribValEncHEVCFeatures features = { .value = priv->
va_features };
295 sps->amp_enabled_flag =
297 sps->sample_adaptive_offset_enabled_flag =
299 sps->sps_temporal_mvp_enabled_flag =
300 !!features.bits.temporal_mvp;
301 sps->pcm_enabled_flag =
306 VAConfigAttribValEncHEVCBlockSizes bs = { .value = priv->
va_bs };
307 sps->log2_min_luma_coding_block_size_minus3 =
309 sps->log2_diff_max_min_luma_coding_block_size =
312 sps->log2_min_luma_transform_block_size_minus2 =
313 bs.bits.log2_min_luma_transform_block_size_minus2;
314 sps->log2_diff_max_min_luma_transform_block_size =
315 bs.bits.log2_max_luma_transform_block_size_minus2 -
316 bs.bits.log2_min_luma_transform_block_size_minus2;
318 sps->max_transform_hierarchy_depth_inter =
319 bs.bits.max_max_transform_hierarchy_depth_inter;
320 sps->max_transform_hierarchy_depth_intra =
321 bs.bits.max_max_transform_hierarchy_depth_intra;
326 VAConfigAttribValEncHEVCFeatures features = { .value = priv->
va_features };
327 if (
ctx->va_rc_mode != VA_RC_CQP)
328 pps->cu_qp_delta_enabled_flag =
329 !!features.bits.cu_qp_delta;
331 pps->transform_skip_enabled_flag =
332 !!features.bits.transform_skip;
335 if (
pps->cu_qp_delta_enabled_flag)
336 pps->diff_cu_qp_delta_depth =
sps->log2_diff_max_min_luma_coding_block_size;
342 *vseq = (VAEncSequenceParameterBufferHEVC) {
343 .general_profile_idc =
vps->profile_tier_level.general_profile_idc,
344 .general_level_idc =
vps->profile_tier_level.general_level_idc,
345 .general_tier_flag =
vps->profile_tier_level.general_tier_flag,
348 .intra_idr_period = base_ctx->
gop_size,
349 .ip_period = base_ctx->
b_per_p + 1,
350 .bits_per_second =
ctx->va_bit_rate,
352 .pic_width_in_luma_samples =
sps->pic_width_in_luma_samples,
353 .pic_height_in_luma_samples =
sps->pic_height_in_luma_samples,
356 .chroma_format_idc =
sps->chroma_format_idc,
357 .separate_colour_plane_flag =
sps->separate_colour_plane_flag,
358 .bit_depth_luma_minus8 =
sps->bit_depth_luma_minus8,
359 .bit_depth_chroma_minus8 =
sps->bit_depth_chroma_minus8,
360 .scaling_list_enabled_flag =
sps->scaling_list_enabled_flag,
361 .strong_intra_smoothing_enabled_flag =
362 sps->strong_intra_smoothing_enabled_flag,
363 .amp_enabled_flag =
sps->amp_enabled_flag,
364 .sample_adaptive_offset_enabled_flag =
365 sps->sample_adaptive_offset_enabled_flag,
366 .pcm_enabled_flag =
sps->pcm_enabled_flag,
367 .pcm_loop_filter_disabled_flag =
sps->pcm_loop_filter_disabled_flag,
368 .sps_temporal_mvp_enabled_flag =
sps->sps_temporal_mvp_enabled_flag,
371 .log2_min_luma_coding_block_size_minus3 =
372 sps->log2_min_luma_coding_block_size_minus3,
373 .log2_diff_max_min_luma_coding_block_size =
374 sps->log2_diff_max_min_luma_coding_block_size,
375 .log2_min_transform_block_size_minus2 =
376 sps->log2_min_luma_transform_block_size_minus2,
377 .log2_diff_max_min_transform_block_size =
378 sps->log2_diff_max_min_luma_transform_block_size,
379 .max_transform_hierarchy_depth_inter =
380 sps->max_transform_hierarchy_depth_inter,
381 .max_transform_hierarchy_depth_intra =
382 sps->max_transform_hierarchy_depth_intra,
384 .pcm_sample_bit_depth_luma_minus1 =
385 sps->pcm_sample_bit_depth_luma_minus1,
386 .pcm_sample_bit_depth_chroma_minus1 =
387 sps->pcm_sample_bit_depth_chroma_minus1,
388 .log2_min_pcm_luma_coding_block_size_minus3 =
389 sps->log2_min_pcm_luma_coding_block_size_minus3,
390 .log2_max_pcm_luma_coding_block_size_minus3 =
391 sps->log2_min_pcm_luma_coding_block_size_minus3 +
392 sps->log2_diff_max_min_pcm_luma_coding_block_size,
394 .vui_parameters_present_flag = 0,
397 *vpic = (VAEncPictureParameterBufferHEVC) {
398 .decoded_curr_pic = {
399 .picture_id = VA_INVALID_ID,
400 .flags = VA_PICTURE_HEVC_INVALID,
403 .coded_buf = VA_INVALID_ID,
405 .collocated_ref_pic_index =
sps->sps_temporal_mvp_enabled_flag ?
409 .pic_init_qp =
pps->init_qp_minus26 + 26,
410 .diff_cu_qp_delta_depth =
pps->diff_cu_qp_delta_depth,
411 .pps_cb_qp_offset =
pps->pps_cb_qp_offset,
412 .pps_cr_qp_offset =
pps->pps_cr_qp_offset,
414 .num_tile_columns_minus1 =
pps->num_tile_columns_minus1,
415 .num_tile_rows_minus1 =
pps->num_tile_rows_minus1,
417 .log2_parallel_merge_level_minus2 =
pps->log2_parallel_merge_level_minus2,
418 .ctu_max_bitsize_allowed = 0,
420 .num_ref_idx_l0_default_active_minus1 =
421 pps->num_ref_idx_l0_default_active_minus1,
422 .num_ref_idx_l1_default_active_minus1 =
423 pps->num_ref_idx_l1_default_active_minus1,
425 .slice_pic_parameter_set_id =
pps->pps_pic_parameter_set_id,
428 .sign_data_hiding_enabled_flag =
pps->sign_data_hiding_enabled_flag,
429 .constrained_intra_pred_flag =
pps->constrained_intra_pred_flag,
430 .transform_skip_enabled_flag =
pps->transform_skip_enabled_flag,
431 .cu_qp_delta_enabled_flag =
pps->cu_qp_delta_enabled_flag,
432 .weighted_pred_flag =
pps->weighted_pred_flag,
433 .weighted_bipred_flag =
pps->weighted_bipred_flag,
434 .transquant_bypass_enabled_flag =
pps->transquant_bypass_enabled_flag,
435 .tiles_enabled_flag =
pps->tiles_enabled_flag,
436 .entropy_coding_sync_enabled_flag =
pps->entropy_coding_sync_enabled_flag,
437 .loop_filter_across_tiles_enabled_flag =
438 pps->loop_filter_across_tiles_enabled_flag,
439 .pps_loop_filter_across_slices_enabled_flag =
440 pps->pps_loop_filter_across_slices_enabled_flag,
441 .scaling_list_data_present_flag = (
sps->sps_scaling_list_data_present_flag |
442 pps->pps_scaling_list_data_present_flag),
443 .screen_content_flag = 0,
444 .enable_gpu_weighted_prediction = 0,
445 .no_output_of_prior_pics_flag = 0,
449 if (
pps->tiles_enabled_flag) {
450 for (
i = 0;
i <= vpic->num_tile_rows_minus1;
i++)
451 vpic->row_height_minus1[
i] =
pps->row_height_minus1[
i];
452 for (
i = 0;
i <= vpic->num_tile_columns_minus1;
i++)
453 vpic->column_width_minus1[
i] =
pps->column_width_minus1[
i];
495 for (irap_ref = pic; irap_ref; irap_ref = irap_ref->
refs[1][0]) {
518 .nuh_temporal_id_plus1 = 1,
545 const int mapping[3] = {1, 2, 0};
546 const int chroma_den = 50000;
547 const int luma_den = 10000;
549 for (
i = 0;
i < 3;
i++) {
550 const int j = mapping[
i];
592 clli->max_pic_average_light_level =
FFMIN(clm->
MaxFALL, 65535);
600 size_t sei_a53cc_len;
614 vpic->decoded_curr_pic = (VAPictureHEVC) {
626 href =
ref->codec_priv;
628 vpic->reference_frames[j++] = (VAPictureHEVC) {
632 VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
634 VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
640 vpic->reference_frames[j] = (VAPictureHEVC) {
641 .picture_id = VA_INVALID_ID,
642 .flags = VA_PICTURE_HEVC_INVALID,
650 vpic->pic_fields.bits.reference_pic_flag = pic->
is_reference;
653 vpic->pic_fields.bits.idr_pic_flag = 1;
654 vpic->pic_fields.bits.coding_type = 1;
657 vpic->pic_fields.bits.idr_pic_flag = 0;
658 vpic->pic_fields.bits.coding_type = 1;
661 vpic->pic_fields.bits.idr_pic_flag = 0;
662 vpic->pic_fields.bits.coding_type = 2;
665 vpic->pic_fields.bits.idr_pic_flag = 0;
666 vpic->pic_fields.bits.coding_type = 3;
693 .nuh_temporal_id_plus1 = 1,
707 (1 << (
sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
714 int i, j, poc, rps_pics;
719 memset(rps, 0,
sizeof(*rps));
723 for (j = 0; j < pic->
nb_refs[
i]; j++) {
726 rps_used[rps_pics] = 1;
732 if (pic->
dpb[
i] == pic)
735 for (j = 0; j < pic->
nb_refs[0]; j++) {
739 if (j < pic->nb_refs[0])
742 for (j = 0; j < pic->
nb_refs[1]; j++) {
746 if (j < pic->nb_refs[1])
751 rps_used[rps_pics] = 0;
755 for (
i = 1;
i < rps_pics;
i++) {
756 for (j =
i; j > 0; j--) {
757 if (rps_poc[j] > rps_poc[j - 1])
760 FFSWAP(
int, rps_poc[j], rps_poc[j - 1]);
761 FFSWAP(
int, rps_used[j], rps_used[j - 1]);
767 for (
i = 0;
i < rps_pics;
i++) {
769 rps_poc[
i], rps_used[
i]);
773 for (
i = 0;
i < rps_pics;
i++) {
781 for (j =
i - 1; j >= 0; j--) {
789 for (j =
i; j < rps_pics; j++) {
801 sps->sps_temporal_mvp_enabled_flag;
814 sps->sample_adaptive_offset_enabled_flag;
824 *vslice = (VAEncSliceParameterBufferHEVC) {
846 .slice_fields.bits = {
847 .last_slice_of_pic_flag = slice->
index == vaapi_pic->
nb_slices - 1,
850 .slice_temporal_mvp_enabled_flag =
854 .num_ref_idx_active_override_flag =
858 .slice_deblocking_filter_disabled_flag =
860 .slice_loop_filter_across_slices_enabled_flag =
867 vslice->ref_pic_list0[
i].picture_id = VA_INVALID_ID;
868 vslice->ref_pic_list0[
i].flags = VA_PICTURE_HEVC_INVALID;
869 vslice->ref_pic_list1[
i].picture_id = VA_INVALID_ID;
870 vslice->ref_pic_list1[
i].flags = VA_PICTURE_HEVC_INVALID;
877 vslice->ref_pic_list0[0] = vpic->reference_frames[0];
880 vslice->ref_pic_list1[0] = vpic->reference_frames[0];
885 vslice->ref_pic_list1[0] = vpic->reference_frames[1];
891 vslice->ref_pic_list1[
i].picture_id = vslice->ref_pic_list0[
i].picture_id;
892 vslice->ref_pic_list1[
i].flags = vslice->ref_pic_list0[
i].flags;
904 #if VA_CHECK_VERSION(1, 13, 0)
907 VAConfigAttribValEncHEVCBlockSizes block_size;
911 attr.type = VAConfigAttribEncHEVCFeatures;
912 vas = vaGetConfigAttributes(
ctx->hwctx->display,
ctx->va_profile,
913 ctx->va_entrypoint, &attr, 1);
914 if (vas != VA_STATUS_SUCCESS) {
916 "features, using guessed defaults.\n");
918 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
920 "encoder features, using guessed defaults.\n");
925 attr.type = VAConfigAttribEncHEVCBlockSizes;
926 vas = vaGetConfigAttributes(
ctx->hwctx->display,
ctx->va_profile,
927 ctx->va_entrypoint, &attr, 1);
928 if (vas != VA_STATUS_SUCCESS) {
930 "block size, using guessed defaults.\n");
932 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
934 "encoder block size, using guessed defaults.\n");
936 priv->
va_bs = block_size.value = attr.value;
939 1 << block_size.bits.log2_max_coding_tree_block_size_minus3 + 3;
941 1 << block_size.bits.log2_min_luma_coding_block_size_minus3 + 3;
974 if (
ctx->va_rc_mode == VA_RC_CQP) {
994 "%d / %d / %d for IDR- / P- / B-frames.\n",
1004 ctx->roi_quant_range = 51 + 6 * (
ctx->profile->depth - 8);
1012 #if VA_CHECK_VERSION(0, 37, 0)
1016 #if VA_CHECK_VERSION(1, 2, 0)
1036 .default_quality = 25,
1043 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferHEVC),
1046 .picture_params_size =
sizeof(VAEncPictureParameterBufferHEVC),
1049 .slice_params_size =
sizeof(VAEncSliceParameterBufferHEVC),
1052 .sequence_header_type = VAEncPackedHeaderSequence,
1055 .slice_header_type = VAEncPackedHeaderHEVC_Slice,
1075 "in 8-bit unsigned integer.\n", avctx->
level);
1079 ctx->desired_packed_headers =
1080 VA_ENC_PACKED_HEADER_SEQUENCE |
1081 VA_ENC_PACKED_HEADER_SLICE |
1082 VA_ENC_PACKED_HEADER_MISC;
1085 ctx->explicit_qp = priv->
qp;
1101 #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
1102 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1108 {
"qp",
"Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1111 {
"aud",
"Include AUD",
1114 {
"profile",
"Set profile (general_profile_idc)",
1118 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1119 { .i64 = value }, 0, 0, FLAGS, .unit = "profile"
1125 {
"tier",
"Set tier (general_tier_flag)",
1127 { .i64 = 0 }, 0, 1,
FLAGS, .unit =
"tier" },
1129 { .i64 = 0 }, 0, 0,
FLAGS, .unit =
"tier" },
1131 { .i64 = 1 }, 0, 0,
FLAGS, .unit =
"tier" },
1133 {
"level",
"Set level (general_level_idc)",
1137 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1138 { .i64 = value }, 0, 0, FLAGS, .unit = "level"
1141 {
LEVEL(
"2.1", 63) },
1143 {
LEVEL(
"3.1", 93) },
1144 {
LEVEL(
"4", 120) },
1145 {
LEVEL(
"4.1", 123) },
1146 {
LEVEL(
"5", 150) },
1147 {
LEVEL(
"5.1", 153) },
1148 {
LEVEL(
"5.2", 156) },
1149 {
LEVEL(
"6", 180) },
1150 {
LEVEL(
"6.1", 183) },
1151 {
LEVEL(
"6.2", 186) },
1154 {
"sei",
"Set SEI to include",
1157 0, INT_MAX,
FLAGS, .unit =
"sei" },
1159 "Include HDR metadata for mastering display colour volume "
1160 "and content light level information",
1163 INT_MIN, INT_MAX,
FLAGS, .unit =
"sei" },
1165 "Include A/53 caption data",
1168 INT_MIN, INT_MAX,
FLAGS, .unit =
"sei" },
1170 {
"tiles",
"Tile columns x rows",
1181 {
"i_qfactor",
"1" },
1182 {
"i_qoffset",
"0" },
1183 {
"b_qfactor",
"6/5" },
1184 {
"b_qoffset",
"0" },
1198 .
p.
name =
"hevc_vaapi",
1218 .p.wrapper_name =
"vaapi",