22 #include <va/va_enc_hevc.h>
89 char *
data,
size_t *data_len,
103 "%zu < %zu.\n", *data_len,
125 "type = %d.\n",
header->nal_unit_type);
133 char *
data,
size_t *data_len)
167 char *
data,
size_t *data_len)
193 char *
data,
size_t *data_len)
231 *
type = VAEncPackedHeaderRawData;
251 VAEncSequenceParameterBufferHEVC *vseq =
ctx->codec_sequence_params;
252 VAEncPictureParameterBufferHEVC *vpic =
ctx->codec_picture_params;
257 memset(
vps, 0,
sizeof(*
vps));
258 memset(
sps, 0,
sizeof(*
sps));
259 memset(
pps, 0,
sizeof(*
pps));
264 if (
desc->nb_components == 1) {
267 if (
desc->log2_chroma_w == 1 &&
desc->log2_chroma_h == 1) {
269 }
else if (
desc->log2_chroma_w == 1 &&
desc->log2_chroma_h == 0) {
271 }
else if (
desc->log2_chroma_w == 0 &&
desc->log2_chroma_h == 0) {
275 "%s is not supported.\n",
desc->name);
287 .nuh_temporal_id_plus1 = 1,
290 vps->vps_video_parameter_set_id = 0;
292 vps->vps_base_layer_internal_flag = 1;
293 vps->vps_base_layer_available_flag = 1;
294 vps->vps_max_layers_minus1 = 0;
295 vps->vps_max_sub_layers_minus1 = 0;
296 vps->vps_temporal_id_nesting_flag = 1;
302 if (chroma_format == 1) {
331 ctx->surface_width,
ctx->surface_height,
332 ctx->nb_slices,
ctx->tile_rows,
ctx->tile_cols,
333 (
ctx->b_per_p > 0) + 1);
339 "any normal level; using level 8.5.\n");
346 vps->vps_sub_layer_ordering_info_present_flag = 0;
347 vps->vps_max_dec_pic_buffering_minus1[0] =
ctx->max_b_depth + 1;
348 vps->vps_max_num_reorder_pics[0] =
ctx->max_b_depth;
349 vps->vps_max_latency_increase_plus1[0] = 0;
351 vps->vps_max_layer_id = 0;
352 vps->vps_num_layer_sets_minus1 = 0;
353 vps->layer_id_included_flag[0][0] = 1;
355 vps->vps_timing_info_present_flag = 1;
359 vps->vps_poc_proportional_to_timing_flag = 1;
360 vps->vps_num_ticks_poc_diff_one_minus1 = 0;
364 vps->vps_poc_proportional_to_timing_flag = 0;
366 vps->vps_num_hrd_parameters = 0;
374 .nuh_temporal_id_plus1 = 1,
377 sps->sps_video_parameter_set_id =
vps->vps_video_parameter_set_id;
379 sps->sps_max_sub_layers_minus1 =
vps->vps_max_sub_layers_minus1;
380 sps->sps_temporal_id_nesting_flag =
vps->vps_temporal_id_nesting_flag;
382 sps->profile_tier_level =
vps->profile_tier_level;
384 sps->sps_seq_parameter_set_id = 0;
386 sps->chroma_format_idc = chroma_format;
387 sps->separate_colour_plane_flag = 0;
389 sps->pic_width_in_luma_samples =
ctx->surface_width;
390 sps->pic_height_in_luma_samples =
ctx->surface_height;
392 if (avctx->
width !=
ctx->surface_width ||
394 sps->conformance_window_flag = 1;
395 sps->conf_win_left_offset = 0;
396 sps->conf_win_right_offset =
397 (
ctx->surface_width - avctx->
width) >>
desc->log2_chroma_w;
398 sps->conf_win_top_offset = 0;
399 sps->conf_win_bottom_offset =
400 (
ctx->surface_height - avctx->
height) >>
desc->log2_chroma_h;
402 sps->conformance_window_flag = 0;
408 sps->log2_max_pic_order_cnt_lsb_minus4 = 8;
410 sps->sps_sub_layer_ordering_info_present_flag =
411 vps->vps_sub_layer_ordering_info_present_flag;
412 for (
i = 0;
i <=
sps->sps_max_sub_layers_minus1;
i++) {
413 sps->sps_max_dec_pic_buffering_minus1[
i] =
414 vps->vps_max_dec_pic_buffering_minus1[
i];
415 sps->sps_max_num_reorder_pics[
i] =
416 vps->vps_max_num_reorder_pics[
i];
417 sps->sps_max_latency_increase_plus1[
i] =
418 vps->vps_max_latency_increase_plus1[
i];
425 sps->log2_min_luma_coding_block_size_minus3 = 0;
426 sps->log2_diff_max_min_luma_coding_block_size = 2;
428 sps->log2_min_luma_transform_block_size_minus2 = 0;
429 sps->log2_diff_max_min_luma_transform_block_size = 3;
431 sps->max_transform_hierarchy_depth_inter = 3;
432 sps->max_transform_hierarchy_depth_intra = 3;
434 sps->amp_enabled_flag = 1;
436 sps->sample_adaptive_offset_enabled_flag = 0;
437 sps->sps_temporal_mvp_enabled_flag = 0;
439 sps->pcm_enabled_flag = 0;
444 sps->num_short_term_ref_pic_sets = 0;
445 sps->long_term_ref_pics_present_flag = 0;
447 sps->vui_parameters_present_flag = 1;
453 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
454 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
455 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
456 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
462 if (num == sar_idc[
i].num &&
463 den == sar_idc[
i].den) {
519 .nuh_temporal_id_plus1 = 1,
522 pps->pps_pic_parameter_set_id = 0;
523 pps->pps_seq_parameter_set_id =
sps->sps_seq_parameter_set_id;
525 pps->num_ref_idx_l0_default_active_minus1 = 0;
526 pps->num_ref_idx_l1_default_active_minus1 = 0;
530 pps->cu_qp_delta_enabled_flag = (
ctx->va_rc_mode != VA_RC_CQP);
531 pps->diff_cu_qp_delta_depth = 0;
533 if (
ctx->tile_rows &&
ctx->tile_cols) {
536 pps->tiles_enabled_flag = 1;
537 pps->num_tile_columns_minus1 =
ctx->tile_cols - 1;
538 pps->num_tile_rows_minus1 =
ctx->tile_rows - 1;
543 for (
i = 0;
i <=
pps->num_tile_columns_minus1 &&
544 uniform_spacing;
i++) {
545 if (
ctx->col_width[
i] !=
546 (
i + 1) *
ctx->slice_block_cols /
ctx->tile_cols -
547 i *
ctx->slice_block_cols /
ctx->tile_cols)
550 for (
i = 0;
i <=
pps->num_tile_rows_minus1 &&
551 uniform_spacing;
i++) {
552 if (
ctx->row_height[
i] !=
553 (
i + 1) *
ctx->slice_block_rows /
ctx->tile_rows -
554 i *
ctx->slice_block_rows /
ctx->tile_rows)
557 pps->uniform_spacing_flag = uniform_spacing;
559 for (
i = 0;
i <=
pps->num_tile_columns_minus1;
i++)
560 pps->column_width_minus1[
i] =
ctx->col_width[
i] - 1;
561 for (
i = 0;
i <=
pps->num_tile_rows_minus1;
i++)
562 pps->row_height_minus1[
i] =
ctx->row_height[
i] - 1;
564 pps->loop_filter_across_tiles_enabled_flag = 1;
567 pps->pps_loop_filter_across_slices_enabled_flag = 1;
571 *vseq = (VAEncSequenceParameterBufferHEVC) {
572 .general_profile_idc =
vps->profile_tier_level.general_profile_idc,
573 .general_level_idc =
vps->profile_tier_level.general_level_idc,
574 .general_tier_flag =
vps->profile_tier_level.general_tier_flag,
576 .intra_period =
ctx->gop_size,
577 .intra_idr_period =
ctx->gop_size,
578 .ip_period =
ctx->b_per_p + 1,
579 .bits_per_second =
ctx->va_bit_rate,
581 .pic_width_in_luma_samples =
sps->pic_width_in_luma_samples,
582 .pic_height_in_luma_samples =
sps->pic_height_in_luma_samples,
585 .chroma_format_idc =
sps->chroma_format_idc,
586 .separate_colour_plane_flag =
sps->separate_colour_plane_flag,
587 .bit_depth_luma_minus8 =
sps->bit_depth_luma_minus8,
588 .bit_depth_chroma_minus8 =
sps->bit_depth_chroma_minus8,
589 .scaling_list_enabled_flag =
sps->scaling_list_enabled_flag,
590 .strong_intra_smoothing_enabled_flag =
591 sps->strong_intra_smoothing_enabled_flag,
592 .amp_enabled_flag =
sps->amp_enabled_flag,
593 .sample_adaptive_offset_enabled_flag =
594 sps->sample_adaptive_offset_enabled_flag,
595 .pcm_enabled_flag =
sps->pcm_enabled_flag,
596 .pcm_loop_filter_disabled_flag =
sps->pcm_loop_filter_disabled_flag,
597 .sps_temporal_mvp_enabled_flag =
sps->sps_temporal_mvp_enabled_flag,
600 .log2_min_luma_coding_block_size_minus3 =
601 sps->log2_min_luma_coding_block_size_minus3,
602 .log2_diff_max_min_luma_coding_block_size =
603 sps->log2_diff_max_min_luma_coding_block_size,
604 .log2_min_transform_block_size_minus2 =
605 sps->log2_min_luma_transform_block_size_minus2,
606 .log2_diff_max_min_transform_block_size =
607 sps->log2_diff_max_min_luma_transform_block_size,
608 .max_transform_hierarchy_depth_inter =
609 sps->max_transform_hierarchy_depth_inter,
610 .max_transform_hierarchy_depth_intra =
611 sps->max_transform_hierarchy_depth_intra,
613 .pcm_sample_bit_depth_luma_minus1 =
614 sps->pcm_sample_bit_depth_luma_minus1,
615 .pcm_sample_bit_depth_chroma_minus1 =
616 sps->pcm_sample_bit_depth_chroma_minus1,
617 .log2_min_pcm_luma_coding_block_size_minus3 =
618 sps->log2_min_pcm_luma_coding_block_size_minus3,
619 .log2_max_pcm_luma_coding_block_size_minus3 =
620 sps->log2_min_pcm_luma_coding_block_size_minus3 +
621 sps->log2_diff_max_min_pcm_luma_coding_block_size,
623 .vui_parameters_present_flag = 0,
626 *vpic = (VAEncPictureParameterBufferHEVC) {
627 .decoded_curr_pic = {
628 .picture_id = VA_INVALID_ID,
629 .flags = VA_PICTURE_HEVC_INVALID,
632 .coded_buf = VA_INVALID_ID,
634 .collocated_ref_pic_index = 0xff,
638 .pic_init_qp =
pps->init_qp_minus26 + 26,
639 .diff_cu_qp_delta_depth =
pps->diff_cu_qp_delta_depth,
640 .pps_cb_qp_offset =
pps->pps_cb_qp_offset,
641 .pps_cr_qp_offset =
pps->pps_cr_qp_offset,
643 .num_tile_columns_minus1 =
pps->num_tile_columns_minus1,
644 .num_tile_rows_minus1 =
pps->num_tile_rows_minus1,
646 .log2_parallel_merge_level_minus2 =
pps->log2_parallel_merge_level_minus2,
647 .ctu_max_bitsize_allowed = 0,
649 .num_ref_idx_l0_default_active_minus1 =
650 pps->num_ref_idx_l0_default_active_minus1,
651 .num_ref_idx_l1_default_active_minus1 =
652 pps->num_ref_idx_l1_default_active_minus1,
654 .slice_pic_parameter_set_id =
pps->pps_pic_parameter_set_id,
657 .sign_data_hiding_enabled_flag =
pps->sign_data_hiding_enabled_flag,
658 .constrained_intra_pred_flag =
pps->constrained_intra_pred_flag,
659 .transform_skip_enabled_flag =
pps->transform_skip_enabled_flag,
660 .cu_qp_delta_enabled_flag =
pps->cu_qp_delta_enabled_flag,
661 .weighted_pred_flag =
pps->weighted_pred_flag,
662 .weighted_bipred_flag =
pps->weighted_bipred_flag,
663 .transquant_bypass_enabled_flag =
pps->transquant_bypass_enabled_flag,
664 .tiles_enabled_flag =
pps->tiles_enabled_flag,
665 .entropy_coding_sync_enabled_flag =
pps->entropy_coding_sync_enabled_flag,
666 .loop_filter_across_tiles_enabled_flag =
667 pps->loop_filter_across_tiles_enabled_flag,
668 .scaling_list_data_present_flag = (
sps->sps_scaling_list_data_present_flag |
669 pps->pps_scaling_list_data_present_flag),
670 .screen_content_flag = 0,
671 .enable_gpu_weighted_prediction = 0,
672 .no_output_of_prior_pics_flag = 0,
676 if (
pps->tiles_enabled_flag) {
677 for (
i = 0;
i <= vpic->num_tile_rows_minus1;
i++)
678 vpic->row_height_minus1[
i] =
pps->row_height_minus1[
i];
679 for (
i = 0;
i <= vpic->num_tile_columns_minus1;
i++)
680 vpic->column_width_minus1[
i] =
pps->column_width_minus1[
i];
721 for (irap_ref = pic; irap_ref; irap_ref = irap_ref->
refs[1]) {
744 .nuh_temporal_id_plus1 = 1,
771 const int mapping[3] = {1, 2, 0};
772 const int chroma_den = 50000;
773 const int luma_den = 10000;
775 for (
i = 0;
i < 3;
i++) {
776 const int j = mapping[
i];
818 clli->max_pic_average_light_level =
FFMIN(clm->
MaxFALL, 65535);
824 vpic->decoded_curr_pic = (VAPictureHEVC) {
835 href =
ref->priv_data;
837 vpic->reference_frames[
i] = (VAPictureHEVC) {
838 .picture_id =
ref->recon_surface,
841 VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
843 VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
847 vpic->reference_frames[
i] = (VAPictureHEVC) {
848 .picture_id = VA_INVALID_ID,
849 .flags = VA_PICTURE_HEVC_INVALID,
859 vpic->pic_fields.bits.idr_pic_flag = 1;
860 vpic->pic_fields.bits.coding_type = 1;
861 vpic->pic_fields.bits.reference_pic_flag = 1;
864 vpic->pic_fields.bits.idr_pic_flag = 0;
865 vpic->pic_fields.bits.coding_type = 1;
866 vpic->pic_fields.bits.reference_pic_flag = 1;
869 vpic->pic_fields.bits.idr_pic_flag = 0;
870 vpic->pic_fields.bits.coding_type = 2;
871 vpic->pic_fields.bits.reference_pic_flag = 1;
874 vpic->pic_fields.bits.idr_pic_flag = 0;
875 vpic->pic_fields.bits.coding_type = 3;
876 vpic->pic_fields.bits.reference_pic_flag = 0;
901 .nuh_temporal_id_plus1 = 1,
912 (1 << (
sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
919 int i, j, poc, rps_pics;
924 memset(rps, 0,
sizeof(*rps));
930 rps_used[rps_pics] = 1;
934 if (pic->
dpb[
i] == pic)
936 for (j = 0; j < pic->
nb_refs; j++) {
940 if (j < pic->nb_refs)
944 rps_used[rps_pics] = 0;
948 for (
i = 1;
i < rps_pics;
i++) {
949 for (j =
i; j > 0; j--) {
950 if (rps_poc[j] > rps_poc[j - 1])
953 FFSWAP(
int, rps_poc[j], rps_poc[j - 1]);
954 FFSWAP(
int, rps_used[j], rps_used[j - 1]);
960 for (
i = 0;
i < rps_pics;
i++) {
962 rps_poc[
i], rps_used[
i]);
966 for (
i = 0;
i < rps_pics;
i++) {
974 for (j =
i - 1; j >= 0; j--) {
982 for (j =
i; j < rps_pics; j++) {
992 sps->sps_temporal_mvp_enabled_flag;
1004 sps->sample_adaptive_offset_enabled_flag;
1014 *vslice = (VAEncSliceParameterBufferHEVC) {
1036 .slice_fields.bits = {
1040 .slice_temporal_mvp_enabled_flag =
1044 .num_ref_idx_active_override_flag =
1048 .slice_deblocking_filter_disabled_flag =
1050 .slice_loop_filter_across_slices_enabled_flag =
1057 vslice->ref_pic_list0[
i].picture_id = VA_INVALID_ID;
1058 vslice->ref_pic_list0[
i].flags = VA_PICTURE_HEVC_INVALID;
1059 vslice->ref_pic_list1[
i].picture_id = VA_INVALID_ID;
1060 vslice->ref_pic_list1[
i].flags = VA_PICTURE_HEVC_INVALID;
1068 vslice->ref_pic_list0[0] = vpic->reference_frames[0];
1073 vslice->ref_pic_list1[0] = vpic->reference_frames[1];
1089 if (
ctx->va_rc_mode == VA_RC_CQP) {
1109 "%d / %d / %d for IDR- / P- / B-frames.\n",
1119 ctx->roi_quant_range = 51 + 6 * (
ctx->profile->depth - 8);
1127 #if VA_CHECK_VERSION(0, 37, 0)
1131 #if VA_CHECK_VERSION(1, 2, 0)
1146 .default_quality = 25,
1152 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferHEVC),
1155 .picture_params_size =
sizeof(VAEncPictureParameterBufferHEVC),
1158 .slice_params_size =
sizeof(VAEncSliceParameterBufferHEVC),
1161 .sequence_header_type = VAEncPackedHeaderSequence,
1164 .slice_header_type = VAEncPackedHeaderHEVC_Slice,
1184 "in 8-bit unsigned integer.\n", avctx->
level);
1188 ctx->desired_packed_headers =
1189 VA_ENC_PACKED_HEADER_SEQUENCE |
1190 VA_ENC_PACKED_HEADER_SLICE |
1191 VA_ENC_PACKED_HEADER_MISC;
1197 ctx->slice_block_width =
ctx->slice_block_height = 32;
1200 ctx->explicit_qp = priv->
qp;
1215 #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
1216 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1221 {
"qp",
"Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1224 {
"aud",
"Include AUD",
1227 {
"profile",
"Set profile (general_profile_idc)",
1231 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1232 { .i64 = value }, 0, 0, FLAGS, "profile"
1238 {
"tier",
"Set tier (general_tier_flag)",
1240 { .i64 = 0 }, 0, 1,
FLAGS,
"tier" },
1242 { .i64 = 0 }, 0, 0,
FLAGS,
"tier" },
1244 { .i64 = 1 }, 0, 0,
FLAGS,
"tier" },
1246 {
"level",
"Set level (general_level_idc)",
1250 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1251 { .i64 = value }, 0, 0, FLAGS, "level"
1254 {
LEVEL(
"2.1", 63) },
1256 {
LEVEL(
"3.1", 93) },
1257 {
LEVEL(
"4", 120) },
1258 {
LEVEL(
"4.1", 123) },
1259 {
LEVEL(
"5", 150) },
1260 {
LEVEL(
"5.1", 153) },
1261 {
LEVEL(
"5.2", 156) },
1262 {
LEVEL(
"6", 180) },
1263 {
LEVEL(
"6.1", 183) },
1264 {
LEVEL(
"6.2", 186) },
1267 {
"sei",
"Set SEI to include",
1270 0, INT_MAX,
FLAGS,
"sei" },
1272 "Include HDR metadata for mastering display colour volume "
1273 "and content light level information",
1276 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1278 {
"tiles",
"Tile columns x rows",
1289 {
"i_qfactor",
"1" },
1290 {
"i_qoffset",
"0" },
1291 {
"b_qfactor",
"6/5" },
1292 {
"b_qoffset",
"0" },
1306 .
name =
"hevc_vaapi",
1324 .wrapper_name =
"vaapi",