22 #include <va/va_enc_h264.h>
46 0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
47 0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
109 char *
data,
size_t *data_len,
123 "%zu < %zu.\n", *data_len,
146 "type = %d.\n",
header->nal_unit_type);
154 char *
data,
size_t *data_len)
184 char *
data,
size_t *data_len)
210 char *
data,
size_t *data_len)
255 sei->payload_count =
i;
269 *
type = VAEncPackedHeaderRawData;
279 *
type = VAEncPackedHeaderH264_SEI;
299 VAEncSequenceParameterBufferH264 *vseq =
ctx->codec_sequence_params;
300 VAEncPictureParameterBufferH264 *vpic =
ctx->codec_picture_params;
302 memset(
sps, 0,
sizeof(*
sps));
303 memset(
pps, 0,
sizeof(*
pps));
305 sps->nal_unit_header.nal_ref_idc = 3;
312 sps->constraint_set1_flag = 1;
315 sps->constraint_set3_flag =
ctx->gop_size == 1;
319 sps->constraint_set4_flag = 1;
320 sps->constraint_set5_flag =
ctx->b_per_p == 0;
323 if (
ctx->gop_size == 1)
347 if (
level->constraint_set3_flag)
348 sps->constraint_set3_flag = 1;
352 "to any level: using level 6.2.\n");
357 sps->seq_parameter_set_id = 0;
358 sps->chroma_format_idc = 1;
360 sps->log2_max_frame_num_minus4 = 4;
361 sps->pic_order_cnt_type = 0;
362 sps->log2_max_pic_order_cnt_lsb_minus4 = 4;
367 sps->pic_height_in_map_units_minus1 = priv->
mb_height - 1;
369 sps->frame_mbs_only_flag = 1;
370 sps->direct_8x8_inference_flag = 1;
374 sps->frame_cropping_flag = 1;
376 sps->frame_crop_left_offset = 0;
377 sps->frame_crop_right_offset =
379 sps->frame_crop_top_offset = 0;
380 sps->frame_crop_bottom_offset =
383 sps->frame_cropping_flag = 0;
386 sps->vui_parameters_present_flag = 1;
392 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
393 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
394 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
395 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
401 if (num == sar_idc[
i].num &&
402 den == sar_idc[
i].den) {
403 sps->vui.aspect_ratio_idc =
i;
408 sps->vui.aspect_ratio_idc = 255;
409 sps->vui.sar_width = num;
410 sps->vui.sar_height = den;
412 sps->vui.aspect_ratio_info_present_flag = 1;
419 sps->vui.video_signal_type_present_flag = 1;
420 sps->vui.video_format = 5;
421 sps->vui.video_full_range_flag =
427 sps->vui.colour_description_present_flag = 1;
433 sps->vui.video_format = 5;
434 sps->vui.video_full_range_flag = 0;
441 sps->vui.chroma_loc_info_present_flag = 1;
442 sps->vui.chroma_sample_loc_type_top_field =
443 sps->vui.chroma_sample_loc_type_bottom_field =
447 sps->vui.timing_info_present_flag = 1;
451 sps->vui.fixed_frame_rate_flag = 1;
455 sps->vui.fixed_frame_rate_flag = 0;
462 sps->vui.nal_hrd_parameters_present_flag = 1;
469 av_clip_uintp2(
av_log2(
ctx->va_bit_rate) - 15 - 6, 4);
474 av_clip_uintp2(
av_log2(
ctx->hrd_params.buffer_size) - 15 - 4, 4);
491 (uint64_t)
ctx->hrd_params.initial_buffer_fullness /
492 ctx->hrd_params.buffer_size;
495 sps->vui.nal_hrd_parameters_present_flag = 0;
496 sps->vui.low_delay_hrd_flag = 1 -
sps->vui.fixed_frame_rate_flag;
499 sps->vui.bitstream_restriction_flag = 1;
500 sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
501 sps->vui.log2_max_mv_length_horizontal = 15;
502 sps->vui.log2_max_mv_length_vertical = 15;
503 sps->vui.max_num_reorder_frames =
ctx->max_b_depth;
504 sps->vui.max_dec_frame_buffering =
ctx->max_b_depth + 1;
506 pps->nal_unit_header.nal_ref_idc = 3;
509 pps->pic_parameter_set_id = 0;
510 pps->seq_parameter_set_id = 0;
512 pps->entropy_coding_mode_flag =
516 if (!priv->
coder &&
pps->entropy_coding_mode_flag)
517 pps->entropy_coding_mode_flag = 0;
519 pps->num_ref_idx_l0_default_active_minus1 = 0;
520 pps->num_ref_idx_l1_default_active_minus1 = 0;
527 pps->more_rbsp_data = 0;
529 pps->more_rbsp_data = 1;
531 pps->transform_8x8_mode_flag = 1;
534 *vseq = (VAEncSequenceParameterBufferH264) {
535 .seq_parameter_set_id =
sps->seq_parameter_set_id,
536 .level_idc =
sps->level_idc,
537 .intra_period =
ctx->gop_size,
538 .intra_idr_period =
ctx->gop_size,
539 .ip_period =
ctx->b_per_p + 1,
541 .bits_per_second =
ctx->va_bit_rate,
542 .max_num_ref_frames =
sps->max_num_ref_frames,
543 .picture_width_in_mbs =
sps->pic_width_in_mbs_minus1 + 1,
544 .picture_height_in_mbs =
sps->pic_height_in_map_units_minus1 + 1,
547 .chroma_format_idc =
sps->chroma_format_idc,
548 .frame_mbs_only_flag =
sps->frame_mbs_only_flag,
549 .mb_adaptive_frame_field_flag =
sps->mb_adaptive_frame_field_flag,
550 .seq_scaling_matrix_present_flag =
sps->seq_scaling_matrix_present_flag,
551 .direct_8x8_inference_flag =
sps->direct_8x8_inference_flag,
552 .log2_max_frame_num_minus4 =
sps->log2_max_frame_num_minus4,
553 .pic_order_cnt_type =
sps->pic_order_cnt_type,
554 .log2_max_pic_order_cnt_lsb_minus4 =
sps->log2_max_pic_order_cnt_lsb_minus4,
555 .delta_pic_order_always_zero_flag =
sps->delta_pic_order_always_zero_flag,
558 .bit_depth_luma_minus8 =
sps->bit_depth_luma_minus8,
559 .bit_depth_chroma_minus8 =
sps->bit_depth_chroma_minus8,
561 .frame_cropping_flag =
sps->frame_cropping_flag,
562 .frame_crop_left_offset =
sps->frame_crop_left_offset,
563 .frame_crop_right_offset =
sps->frame_crop_right_offset,
564 .frame_crop_top_offset =
sps->frame_crop_top_offset,
565 .frame_crop_bottom_offset =
sps->frame_crop_bottom_offset,
567 .vui_parameters_present_flag =
sps->vui_parameters_present_flag,
570 .aspect_ratio_info_present_flag =
sps->vui.aspect_ratio_info_present_flag,
571 .timing_info_present_flag =
sps->vui.timing_info_present_flag,
572 .bitstream_restriction_flag =
sps->vui.bitstream_restriction_flag,
573 .log2_max_mv_length_horizontal =
sps->vui.log2_max_mv_length_horizontal,
574 .log2_max_mv_length_vertical =
sps->vui.log2_max_mv_length_vertical,
577 .aspect_ratio_idc =
sps->vui.aspect_ratio_idc,
578 .sar_width =
sps->vui.sar_width,
579 .sar_height =
sps->vui.sar_height,
580 .num_units_in_tick =
sps->vui.num_units_in_tick,
581 .time_scale =
sps->vui.time_scale,
584 *vpic = (VAEncPictureParameterBufferH264) {
586 .picture_id = VA_INVALID_ID,
587 .flags = VA_PICTURE_H264_INVALID,
590 .coded_buf = VA_INVALID_ID,
592 .pic_parameter_set_id =
pps->pic_parameter_set_id,
593 .seq_parameter_set_id =
pps->seq_parameter_set_id,
595 .pic_init_qp =
pps->pic_init_qp_minus26 + 26,
596 .num_ref_idx_l0_active_minus1 =
pps->num_ref_idx_l0_default_active_minus1,
597 .num_ref_idx_l1_active_minus1 =
pps->num_ref_idx_l1_default_active_minus1,
599 .chroma_qp_index_offset =
pps->chroma_qp_index_offset,
600 .second_chroma_qp_index_offset =
pps->second_chroma_qp_index_offset,
603 .entropy_coding_mode_flag =
pps->entropy_coding_mode_flag,
604 .weighted_pred_flag =
pps->weighted_pred_flag,
605 .weighted_bipred_idc =
pps->weighted_bipred_idc,
606 .constrained_intra_pred_flag =
pps->constrained_intra_pred_flag,
607 .transform_8x8_mode_flag =
pps->transform_8x8_mode_flag,
608 .deblocking_filter_control_present_flag =
609 pps->deblocking_filter_control_present_flag,
610 .redundant_pic_cnt_present_flag =
pps->redundant_pic_cnt_present_flag,
611 .pic_order_present_flag =
612 pps->bottom_field_pic_order_in_frame_present_flag,
613 .pic_scaling_matrix_present_flag =
pps->pic_scaling_matrix_present_flag,
680 if (
ctx->va_rc_mode == VA_RC_CBR)
696 .exact_match_flag = 1,
697 .broken_link_flag =
ctx->b_per_p > 0,
703 vpic->CurrPic = (VAPictureH264) {
716 href =
ref->priv_data;
718 vpic->ReferenceFrames[
i] = (VAPictureH264) {
719 .picture_id =
ref->recon_surface,
721 .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
727 vpic->ReferenceFrames[
i] = (VAPictureH264) {
728 .picture_id = VA_INVALID_ID,
729 .flags = VA_PICTURE_H264_INVALID,
762 for (j = n; j > 0; j--) {
767 rpl0[j] = rpl0[j - 1];
769 rpl0[j] = prev->
dpb[
i];
772 for (j = n; j > 0; j--) {
783 rpl0[j] = rpl0[j - 1];
785 rpl0[j] = prev->
dpb[
i];
787 for (j = n; j > 0; j--) {
798 rpl1[j] = rpl1[j - 1];
800 rpl1[j] = prev->
dpb[
i];
807 for (
i = 0;
i < n;
i++) {
808 if (rpl0[
i] != rpl1[
i])
819 for (
i = 0;
i < n;
i++) {
822 hn->frame_num,
hn->pic_order_cnt);
829 for (
i = 0;
i < n;
i++) {
832 hn->frame_num,
hn->pic_order_cnt);
868 ((1 << (4 +
sps->log2_max_frame_num_minus4)) - 1);
871 ((1 << (4 +
sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
884 int discard = 0, keep = 0;
890 if (prev->
dpb[
i] == pic->
dpb[j])
894 discard_list[discard] = prev->
dpb[
i];
906 for (
i = 0;
i < discard;
i++) {
931 if (pic->
refs[
i] != def_l0[
i])
956 int need_rplm_l0 = 0, need_rplm_l1 = 0;
963 if (pic->
refs[
i] != def_l0[n0])
967 if (pic->
refs[
i] != def_l1[n1])
1026 vslice->macroblock_info = VA_INVALID_ID;
1037 vslice->RefPicList0[
i].picture_id = VA_INVALID_ID;
1038 vslice->RefPicList0[
i].flags = VA_PICTURE_H264_INVALID;
1039 vslice->RefPicList1[
i].picture_id = VA_INVALID_ID;
1040 vslice->RefPicList1[
i].flags = VA_PICTURE_H264_INVALID;
1048 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
1053 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
1074 if (
ctx->va_rc_mode == VA_RC_CQP) {
1090 "%d / %d / %d for IDR- / P- / B-frames.\n",
1100 if (!
ctx->rc_mode->hrd) {
1107 const char *vaapi = VA_VERSION_S;
1115 driver = vaQueryVendorString(
ctx->hwctx->display);
1117 driver =
"unknown driver";
1126 "%s / VAAPI %s / %s", lavc, vaapi, driver);
1133 ctx->roi_quant_range = 51 + 6 * (
ctx->profile->depth - 8);
1142 8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
1154 .default_quality = 20,
1160 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferH264),
1163 .picture_params_size =
sizeof(VAEncPictureParameterBufferH264),
1166 .slice_params_size =
sizeof(VAEncSliceParameterBufferH264),
1169 .sequence_header_type = VAEncPackedHeaderSequence,
1172 .slice_header_type = VAEncPackedHeaderH264_Slice,
1196 "supported, using constrained baseline profile instead.\n");
1201 "is not supported.\n");
1206 "are not supported.\n");
1215 "are not supported.\n");
1221 "in 8-bit unsigned integer.\n", avctx->
level);
1225 ctx->desired_packed_headers =
1226 VA_ENC_PACKED_HEADER_SEQUENCE |
1227 VA_ENC_PACKED_HEADER_SLICE |
1228 VA_ENC_PACKED_HEADER_MISC;
1233 ctx->slice_block_height =
ctx->slice_block_width = 16;
1236 ctx->explicit_qp = priv->
qp;
1252 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
1253 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1258 {
"qp",
"Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1260 {
"quality",
"Set encode quality (trades off against speed, higher is faster)",
1262 {
"coder",
"Entropy coder type",
1269 {
"aud",
"Include AUD",
1272 {
"sei",
"Set SEI to include",
1275 0, INT_MAX,
FLAGS,
"sei" },
1276 {
"identifier",
"Include encoder version identifier",
1278 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1279 {
"timing",
"Include timing parameters (buffering_period and pic_timing)",
1281 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1282 {
"recovery_point",
"Include recovery points where appropriate",
1284 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1286 {
"profile",
"Set profile (profile_idc and constraint_set*_flag)",
1290 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1291 { .i64 = value }, 0, 0, FLAGS, "profile"
1297 {
"level",
"Set level (level_idc)",
1301 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1302 { .i64 = value }, 0, 0, FLAGS, "level"
1304 {
LEVEL(
"1.1", 11) },
1305 {
LEVEL(
"1.2", 12) },
1306 {
LEVEL(
"1.3", 13) },
1308 {
LEVEL(
"2.1", 21) },
1309 {
LEVEL(
"2.2", 22) },
1311 {
LEVEL(
"3.1", 31) },
1312 {
LEVEL(
"3.2", 32) },
1314 {
LEVEL(
"4.1", 41) },
1315 {
LEVEL(
"4.2", 42) },
1317 {
LEVEL(
"5.1", 51) },
1318 {
LEVEL(
"5.2", 52) },
1320 {
LEVEL(
"6.1", 61) },
1321 {
LEVEL(
"6.2", 62) },
1331 {
"i_qfactor",
"1" },
1332 {
"i_qoffset",
"0" },
1333 {
"b_qfactor",
"6/5" },
1334 {
"b_qoffset",
"0" },
1348 .
name =
"h264_vaapi",
1366 .wrapper_name =
"vaapi",