22 #include <va/va_enc_h264.h>
51 0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
52 0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
115 char *
data,
size_t *data_len,
129 "%zu < %zu.\n", *data_len,
151 "type = %d.\n",
header->nal_unit_type);
159 char *
data,
size_t *data_len)
189 char *
data,
size_t *data_len)
215 char *
data,
size_t *data_len)
273 *
type = VAEncPackedHeaderRawData;
283 *
type = VAEncPackedHeaderH264_SEI;
303 VAEncSequenceParameterBufferH264 *vseq =
ctx->codec_sequence_params;
304 VAEncPictureParameterBufferH264 *vpic =
ctx->codec_picture_params;
308 memset(
sps, 0,
sizeof(*
sps));
309 memset(
pps, 0,
sizeof(*
pps));
313 if (
desc->nb_components == 1 ||
desc->log2_chroma_w != 1 ||
desc->log2_chroma_h != 1) {
315 "%s is not supported.\n",
desc->name);
320 sps->nal_unit_header.nal_ref_idc = 3;
327 sps->constraint_set1_flag = 1;
330 sps->constraint_set3_flag =
ctx->gop_size == 1;
334 sps->constraint_set4_flag = 1;
335 sps->constraint_set5_flag =
ctx->b_per_p == 0;
338 if (
ctx->gop_size == 1)
362 if (
level->constraint_set3_flag)
363 sps->constraint_set3_flag = 1;
367 "to any level: using level 6.2.\n");
372 sps->seq_parameter_set_id = 0;
373 sps->chroma_format_idc = 1;
377 sps->log2_max_frame_num_minus4 = 4;
378 sps->pic_order_cnt_type =
ctx->max_b_depth ? 0 : 2;
379 if (
sps->pic_order_cnt_type == 0) {
380 sps->log2_max_pic_order_cnt_lsb_minus4 = 4;
386 sps->pic_height_in_map_units_minus1 = priv->
mb_height - 1;
388 sps->frame_mbs_only_flag = 1;
389 sps->direct_8x8_inference_flag = 1;
393 sps->frame_cropping_flag = 1;
395 sps->frame_crop_left_offset = 0;
396 sps->frame_crop_right_offset =
398 sps->frame_crop_top_offset = 0;
399 sps->frame_crop_bottom_offset =
402 sps->frame_cropping_flag = 0;
405 sps->vui_parameters_present_flag = 1;
415 sps->vui.aspect_ratio_idc =
i;
420 sps->vui.aspect_ratio_idc = 255;
421 sps->vui.sar_width = num;
422 sps->vui.sar_height = den;
424 sps->vui.aspect_ratio_info_present_flag = 1;
428 sps->vui.video_format = 5;
429 sps->vui.video_full_range_flag =
437 sps->vui.colour_description_present_flag = 1;
439 sps->vui.colour_description_present_flag)
440 sps->vui.video_signal_type_present_flag = 1;
443 sps->vui.chroma_loc_info_present_flag = 1;
444 sps->vui.chroma_sample_loc_type_top_field =
445 sps->vui.chroma_sample_loc_type_bottom_field =
449 sps->vui.timing_info_present_flag = 1;
453 sps->vui.fixed_frame_rate_flag = 1;
457 sps->vui.fixed_frame_rate_flag = 0;
464 sps->vui.nal_hrd_parameters_present_flag = 1;
493 (uint64_t)
ctx->hrd_params.initial_buffer_fullness /
494 ctx->hrd_params.buffer_size;
497 sps->vui.nal_hrd_parameters_present_flag = 0;
498 sps->vui.low_delay_hrd_flag = 1 -
sps->vui.fixed_frame_rate_flag;
501 sps->vui.bitstream_restriction_flag = 1;
502 sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
503 sps->vui.log2_max_mv_length_horizontal = 15;
504 sps->vui.log2_max_mv_length_vertical = 15;
505 sps->vui.max_num_reorder_frames =
ctx->max_b_depth;
506 sps->vui.max_dec_frame_buffering =
ctx->max_b_depth + 1;
508 pps->nal_unit_header.nal_ref_idc = 3;
511 pps->pic_parameter_set_id = 0;
512 pps->seq_parameter_set_id = 0;
514 pps->entropy_coding_mode_flag =
518 if (!priv->
coder &&
pps->entropy_coding_mode_flag)
519 pps->entropy_coding_mode_flag = 0;
521 pps->num_ref_idx_l0_default_active_minus1 = 0;
522 pps->num_ref_idx_l1_default_active_minus1 = 0;
529 pps->more_rbsp_data = 0;
531 pps->more_rbsp_data = 1;
533 pps->transform_8x8_mode_flag = 1;
536 *vseq = (VAEncSequenceParameterBufferH264) {
537 .seq_parameter_set_id =
sps->seq_parameter_set_id,
538 .level_idc =
sps->level_idc,
539 .intra_period =
ctx->gop_size,
540 .intra_idr_period =
ctx->gop_size,
541 .ip_period =
ctx->b_per_p + 1,
543 .bits_per_second =
ctx->va_bit_rate,
544 .max_num_ref_frames =
sps->max_num_ref_frames,
545 .picture_width_in_mbs =
sps->pic_width_in_mbs_minus1 + 1,
546 .picture_height_in_mbs =
sps->pic_height_in_map_units_minus1 + 1,
549 .chroma_format_idc =
sps->chroma_format_idc,
550 .frame_mbs_only_flag =
sps->frame_mbs_only_flag,
551 .mb_adaptive_frame_field_flag =
sps->mb_adaptive_frame_field_flag,
552 .seq_scaling_matrix_present_flag =
sps->seq_scaling_matrix_present_flag,
553 .direct_8x8_inference_flag =
sps->direct_8x8_inference_flag,
554 .log2_max_frame_num_minus4 =
sps->log2_max_frame_num_minus4,
555 .pic_order_cnt_type =
sps->pic_order_cnt_type,
556 .log2_max_pic_order_cnt_lsb_minus4 =
sps->log2_max_pic_order_cnt_lsb_minus4,
557 .delta_pic_order_always_zero_flag =
sps->delta_pic_order_always_zero_flag,
560 .bit_depth_luma_minus8 =
sps->bit_depth_luma_minus8,
561 .bit_depth_chroma_minus8 =
sps->bit_depth_chroma_minus8,
563 .frame_cropping_flag =
sps->frame_cropping_flag,
564 .frame_crop_left_offset =
sps->frame_crop_left_offset,
565 .frame_crop_right_offset =
sps->frame_crop_right_offset,
566 .frame_crop_top_offset =
sps->frame_crop_top_offset,
567 .frame_crop_bottom_offset =
sps->frame_crop_bottom_offset,
569 .vui_parameters_present_flag =
sps->vui_parameters_present_flag,
572 .aspect_ratio_info_present_flag =
sps->vui.aspect_ratio_info_present_flag,
573 .timing_info_present_flag =
sps->vui.timing_info_present_flag,
574 .bitstream_restriction_flag =
sps->vui.bitstream_restriction_flag,
575 .log2_max_mv_length_horizontal =
sps->vui.log2_max_mv_length_horizontal,
576 .log2_max_mv_length_vertical =
sps->vui.log2_max_mv_length_vertical,
579 .aspect_ratio_idc =
sps->vui.aspect_ratio_idc,
580 .sar_width =
sps->vui.sar_width,
581 .sar_height =
sps->vui.sar_height,
582 .num_units_in_tick =
sps->vui.num_units_in_tick,
583 .time_scale =
sps->vui.time_scale,
586 *vpic = (VAEncPictureParameterBufferH264) {
588 .picture_id = VA_INVALID_ID,
589 .flags = VA_PICTURE_H264_INVALID,
592 .coded_buf = VA_INVALID_ID,
594 .pic_parameter_set_id =
pps->pic_parameter_set_id,
595 .seq_parameter_set_id =
pps->seq_parameter_set_id,
597 .pic_init_qp =
pps->pic_init_qp_minus26 + 26,
598 .num_ref_idx_l0_active_minus1 =
pps->num_ref_idx_l0_default_active_minus1,
599 .num_ref_idx_l1_active_minus1 =
pps->num_ref_idx_l1_default_active_minus1,
601 .chroma_qp_index_offset =
pps->chroma_qp_index_offset,
602 .second_chroma_qp_index_offset =
pps->second_chroma_qp_index_offset,
605 .entropy_coding_mode_flag =
pps->entropy_coding_mode_flag,
606 .weighted_pred_flag =
pps->weighted_pred_flag,
607 .weighted_bipred_idc =
pps->weighted_bipred_idc,
608 .constrained_intra_pred_flag =
pps->constrained_intra_pred_flag,
609 .transform_8x8_mode_flag =
pps->transform_8x8_mode_flag,
610 .deblocking_filter_control_present_flag =
611 pps->deblocking_filter_control_present_flag,
612 .redundant_pic_cnt_present_flag =
pps->redundant_pic_cnt_present_flag,
613 .pic_order_present_flag =
614 pps->bottom_field_pic_order_in_frame_present_flag,
615 .pic_scaling_matrix_present_flag =
pps->pic_scaling_matrix_present_flag,
686 if (
ctx->va_rc_mode == VA_RC_CBR)
702 .exact_match_flag = 1,
703 .broken_link_flag =
ctx->b_per_p > 0,
711 size_t sei_a53cc_len;
725 vpic->CurrPic = (VAPictureH264) {
738 href =
ref->priv_data;
740 vpic->ReferenceFrames[j++] = (VAPictureH264) {
741 .picture_id =
ref->recon_surface,
743 .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
751 vpic->ReferenceFrames[j] = (VAPictureH264) {
752 .picture_id = VA_INVALID_ID,
753 .flags = VA_PICTURE_H264_INVALID,
786 for (j = n; j > 0; j--) {
791 rpl0[j] = rpl0[j - 1];
793 rpl0[j] = prev->
dpb[
i];
796 for (j = n; j > 0; j--) {
807 rpl0[j] = rpl0[j - 1];
809 rpl0[j] = prev->
dpb[
i];
811 for (j = n; j > 0; j--) {
822 rpl1[j] = rpl1[j - 1];
824 rpl1[j] = prev->
dpb[
i];
831 for (
i = 0;
i < n;
i++) {
832 if (rpl0[
i] != rpl1[
i])
843 for (
i = 0;
i < n;
i++) {
846 hn->frame_num,
hn->pic_order_cnt);
853 for (
i = 0;
i < n;
i++) {
856 hn->frame_num,
hn->pic_order_cnt);
892 ((1 << (4 +
sps->log2_max_frame_num_minus4)) - 1);
895 ((1 << (4 +
sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
908 int discard = 0, keep = 0;
914 if (prev->
dpb[
i] == pic->
dpb[j])
918 discard_list[discard] = prev->
dpb[
i];
930 for (
i = 0;
i < discard;
i++) {
955 if (pic->
refs[0][
i] != def_l0[
i])
980 int need_rplm_l0 = 0, need_rplm_l1 = 0;
986 if (pic->
refs[0][
i] != def_l0[n0])
995 if (pic->
refs[1][
i] != def_l1[n1])
1049 vslice->macroblock_info = VA_INVALID_ID;
1060 vslice->RefPicList0[
i].picture_id = VA_INVALID_ID;
1061 vslice->RefPicList0[
i].flags = VA_PICTURE_H264_INVALID;
1062 vslice->RefPicList1[
i].picture_id = VA_INVALID_ID;
1063 vslice->RefPicList1[
i].flags = VA_PICTURE_H264_INVALID;
1070 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
1075 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
1096 if (
ctx->va_rc_mode == VA_RC_CQP) {
1112 "%d / %d / %d for IDR- / P- / B-frames.\n",
1122 if (!
ctx->rc_mode->hrd) {
1129 const char *vaapi = VA_VERSION_S;
1137 driver = vaQueryVendorString(
ctx->hwctx->display);
1139 driver =
"unknown driver";
1148 "%s / VAAPI %s / %s", lavc, vaapi, driver);
1155 ctx->roi_quant_range = 51 + 6 * (
ctx->profile->depth - 8);
1161 #if VA_CHECK_VERSION(1, 18, 0)
1167 8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
1179 .default_quality = 20,
1185 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferH264),
1188 .picture_params_size =
sizeof(VAEncPictureParameterBufferH264),
1191 .slice_params_size =
sizeof(VAEncSliceParameterBufferH264),
1194 .sequence_header_type = VAEncPackedHeaderSequence,
1197 .slice_header_type = VAEncPackedHeaderH264_Slice,
1221 "supported, using constrained baseline profile instead.\n");
1226 "is not supported.\n");
1230 "is not supported.\n");
1239 "are not supported.\n");
1245 "in 8-bit unsigned integer.\n", avctx->
level);
1249 ctx->desired_packed_headers =
1250 VA_ENC_PACKED_HEADER_SEQUENCE |
1251 VA_ENC_PACKED_HEADER_SLICE |
1252 VA_ENC_PACKED_HEADER_MISC;
1257 ctx->slice_block_height =
ctx->slice_block_width = 16;
1260 ctx->explicit_qp = priv->
qp;
1277 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
1278 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1283 {
"qp",
"Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1285 {
"quality",
"Set encode quality (trades off against speed, higher is faster)",
1287 {
"coder",
"Entropy coder type",
1294 {
"aud",
"Include AUD",
1297 {
"sei",
"Set SEI to include",
1300 0, INT_MAX,
FLAGS,
"sei" },
1301 {
"identifier",
"Include encoder version identifier",
1303 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1304 {
"timing",
"Include timing parameters (buffering_period and pic_timing)",
1306 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1307 {
"recovery_point",
"Include recovery points where appropriate",
1309 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1310 {
"a53_cc",
"Include A/53 caption data",
1312 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1314 {
"profile",
"Set profile (profile_idc and constraint_set*_flag)",
1318 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1319 { .i64 = value }, 0, 0, FLAGS, "profile"
1326 {
"level",
"Set level (level_idc)",
1330 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1331 { .i64 = value }, 0, 0, FLAGS, "level"
1333 {
LEVEL(
"1.1", 11) },
1334 {
LEVEL(
"1.2", 12) },
1335 {
LEVEL(
"1.3", 13) },
1337 {
LEVEL(
"2.1", 21) },
1338 {
LEVEL(
"2.2", 22) },
1340 {
LEVEL(
"3.1", 31) },
1341 {
LEVEL(
"3.2", 32) },
1343 {
LEVEL(
"4.1", 41) },
1344 {
LEVEL(
"4.2", 42) },
1346 {
LEVEL(
"5.1", 51) },
1347 {
LEVEL(
"5.2", 52) },
1349 {
LEVEL(
"6.1", 61) },
1350 {
LEVEL(
"6.2", 62) },
1360 {
"i_qfactor",
"1" },
1361 {
"i_qoffset",
"0" },
1362 {
"b_qfactor",
"6/5" },
1363 {
"b_qoffset",
"0" },
1377 .
p.
name =
"h264_vaapi",
1396 .p.wrapper_name =
"vaapi",