22 #include <va/va_enc_h264.h>
50 0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
51 0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
114 char *
data,
size_t *data_len,
128 "%zu < %zu.\n", *data_len,
150 "type = %d.\n",
header->nal_unit_type);
158 char *
data,
size_t *data_len)
188 char *
data,
size_t *data_len)
214 char *
data,
size_t *data_len)
272 *
type = VAEncPackedHeaderRawData;
282 *
type = VAEncPackedHeaderH264_SEI;
302 VAEncSequenceParameterBufferH264 *vseq =
ctx->codec_sequence_params;
303 VAEncPictureParameterBufferH264 *vpic =
ctx->codec_picture_params;
305 memset(
sps, 0,
sizeof(*
sps));
306 memset(
pps, 0,
sizeof(*
pps));
308 sps->nal_unit_header.nal_ref_idc = 3;
315 sps->constraint_set1_flag = 1;
318 sps->constraint_set3_flag =
ctx->gop_size == 1;
322 sps->constraint_set4_flag = 1;
323 sps->constraint_set5_flag =
ctx->b_per_p == 0;
326 if (
ctx->gop_size == 1)
350 if (
level->constraint_set3_flag)
351 sps->constraint_set3_flag = 1;
355 "to any level: using level 6.2.\n");
360 sps->seq_parameter_set_id = 0;
361 sps->chroma_format_idc = 1;
363 sps->log2_max_frame_num_minus4 = 4;
364 sps->pic_order_cnt_type =
ctx->max_b_depth ? 0 : 2;
365 if (
sps->pic_order_cnt_type == 0) {
366 sps->log2_max_pic_order_cnt_lsb_minus4 = 4;
372 sps->pic_height_in_map_units_minus1 = priv->
mb_height - 1;
374 sps->frame_mbs_only_flag = 1;
375 sps->direct_8x8_inference_flag = 1;
379 sps->frame_cropping_flag = 1;
381 sps->frame_crop_left_offset = 0;
382 sps->frame_crop_right_offset =
384 sps->frame_crop_top_offset = 0;
385 sps->frame_crop_bottom_offset =
388 sps->frame_cropping_flag = 0;
391 sps->vui_parameters_present_flag = 1;
401 sps->vui.aspect_ratio_idc =
i;
406 sps->vui.aspect_ratio_idc = 255;
407 sps->vui.sar_width = num;
408 sps->vui.sar_height = den;
410 sps->vui.aspect_ratio_info_present_flag = 1;
414 sps->vui.video_format = 5;
415 sps->vui.video_full_range_flag =
423 sps->vui.colour_description_present_flag = 1;
425 sps->vui.colour_description_present_flag)
426 sps->vui.video_signal_type_present_flag = 1;
429 sps->vui.chroma_loc_info_present_flag = 1;
430 sps->vui.chroma_sample_loc_type_top_field =
431 sps->vui.chroma_sample_loc_type_bottom_field =
435 sps->vui.timing_info_present_flag = 1;
439 sps->vui.fixed_frame_rate_flag = 1;
443 sps->vui.fixed_frame_rate_flag = 0;
450 sps->vui.nal_hrd_parameters_present_flag = 1;
479 (uint64_t)
ctx->hrd_params.initial_buffer_fullness /
480 ctx->hrd_params.buffer_size;
483 sps->vui.nal_hrd_parameters_present_flag = 0;
484 sps->vui.low_delay_hrd_flag = 1 -
sps->vui.fixed_frame_rate_flag;
487 sps->vui.bitstream_restriction_flag = 1;
488 sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
489 sps->vui.log2_max_mv_length_horizontal = 15;
490 sps->vui.log2_max_mv_length_vertical = 15;
491 sps->vui.max_num_reorder_frames =
ctx->max_b_depth;
492 sps->vui.max_dec_frame_buffering =
ctx->max_b_depth + 1;
494 pps->nal_unit_header.nal_ref_idc = 3;
497 pps->pic_parameter_set_id = 0;
498 pps->seq_parameter_set_id = 0;
500 pps->entropy_coding_mode_flag =
504 if (!priv->
coder &&
pps->entropy_coding_mode_flag)
505 pps->entropy_coding_mode_flag = 0;
507 pps->num_ref_idx_l0_default_active_minus1 = 0;
508 pps->num_ref_idx_l1_default_active_minus1 = 0;
515 pps->more_rbsp_data = 0;
517 pps->more_rbsp_data = 1;
519 pps->transform_8x8_mode_flag = 1;
522 *vseq = (VAEncSequenceParameterBufferH264) {
523 .seq_parameter_set_id =
sps->seq_parameter_set_id,
524 .level_idc =
sps->level_idc,
525 .intra_period =
ctx->gop_size,
526 .intra_idr_period =
ctx->gop_size,
527 .ip_period =
ctx->b_per_p + 1,
529 .bits_per_second =
ctx->va_bit_rate,
530 .max_num_ref_frames =
sps->max_num_ref_frames,
531 .picture_width_in_mbs =
sps->pic_width_in_mbs_minus1 + 1,
532 .picture_height_in_mbs =
sps->pic_height_in_map_units_minus1 + 1,
535 .chroma_format_idc =
sps->chroma_format_idc,
536 .frame_mbs_only_flag =
sps->frame_mbs_only_flag,
537 .mb_adaptive_frame_field_flag =
sps->mb_adaptive_frame_field_flag,
538 .seq_scaling_matrix_present_flag =
sps->seq_scaling_matrix_present_flag,
539 .direct_8x8_inference_flag =
sps->direct_8x8_inference_flag,
540 .log2_max_frame_num_minus4 =
sps->log2_max_frame_num_minus4,
541 .pic_order_cnt_type =
sps->pic_order_cnt_type,
542 .log2_max_pic_order_cnt_lsb_minus4 =
sps->log2_max_pic_order_cnt_lsb_minus4,
543 .delta_pic_order_always_zero_flag =
sps->delta_pic_order_always_zero_flag,
546 .bit_depth_luma_minus8 =
sps->bit_depth_luma_minus8,
547 .bit_depth_chroma_minus8 =
sps->bit_depth_chroma_minus8,
549 .frame_cropping_flag =
sps->frame_cropping_flag,
550 .frame_crop_left_offset =
sps->frame_crop_left_offset,
551 .frame_crop_right_offset =
sps->frame_crop_right_offset,
552 .frame_crop_top_offset =
sps->frame_crop_top_offset,
553 .frame_crop_bottom_offset =
sps->frame_crop_bottom_offset,
555 .vui_parameters_present_flag =
sps->vui_parameters_present_flag,
558 .aspect_ratio_info_present_flag =
sps->vui.aspect_ratio_info_present_flag,
559 .timing_info_present_flag =
sps->vui.timing_info_present_flag,
560 .bitstream_restriction_flag =
sps->vui.bitstream_restriction_flag,
561 .log2_max_mv_length_horizontal =
sps->vui.log2_max_mv_length_horizontal,
562 .log2_max_mv_length_vertical =
sps->vui.log2_max_mv_length_vertical,
565 .aspect_ratio_idc =
sps->vui.aspect_ratio_idc,
566 .sar_width =
sps->vui.sar_width,
567 .sar_height =
sps->vui.sar_height,
568 .num_units_in_tick =
sps->vui.num_units_in_tick,
569 .time_scale =
sps->vui.time_scale,
572 *vpic = (VAEncPictureParameterBufferH264) {
574 .picture_id = VA_INVALID_ID,
575 .flags = VA_PICTURE_H264_INVALID,
578 .coded_buf = VA_INVALID_ID,
580 .pic_parameter_set_id =
pps->pic_parameter_set_id,
581 .seq_parameter_set_id =
pps->seq_parameter_set_id,
583 .pic_init_qp =
pps->pic_init_qp_minus26 + 26,
584 .num_ref_idx_l0_active_minus1 =
pps->num_ref_idx_l0_default_active_minus1,
585 .num_ref_idx_l1_active_minus1 =
pps->num_ref_idx_l1_default_active_minus1,
587 .chroma_qp_index_offset =
pps->chroma_qp_index_offset,
588 .second_chroma_qp_index_offset =
pps->second_chroma_qp_index_offset,
591 .entropy_coding_mode_flag =
pps->entropy_coding_mode_flag,
592 .weighted_pred_flag =
pps->weighted_pred_flag,
593 .weighted_bipred_idc =
pps->weighted_bipred_idc,
594 .constrained_intra_pred_flag =
pps->constrained_intra_pred_flag,
595 .transform_8x8_mode_flag =
pps->transform_8x8_mode_flag,
596 .deblocking_filter_control_present_flag =
597 pps->deblocking_filter_control_present_flag,
598 .redundant_pic_cnt_present_flag =
pps->redundant_pic_cnt_present_flag,
599 .pic_order_present_flag =
600 pps->bottom_field_pic_order_in_frame_present_flag,
601 .pic_scaling_matrix_present_flag =
pps->pic_scaling_matrix_present_flag,
672 if (
ctx->va_rc_mode == VA_RC_CBR)
688 .exact_match_flag = 1,
689 .broken_link_flag =
ctx->b_per_p > 0,
697 size_t sei_a53cc_len;
711 vpic->CurrPic = (VAPictureH264) {
724 href =
ref->priv_data;
726 vpic->ReferenceFrames[
i] = (VAPictureH264) {
727 .picture_id =
ref->recon_surface,
729 .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
735 vpic->ReferenceFrames[
i] = (VAPictureH264) {
736 .picture_id = VA_INVALID_ID,
737 .flags = VA_PICTURE_H264_INVALID,
770 for (j = n; j > 0; j--) {
775 rpl0[j] = rpl0[j - 1];
777 rpl0[j] = prev->
dpb[
i];
780 for (j = n; j > 0; j--) {
791 rpl0[j] = rpl0[j - 1];
793 rpl0[j] = prev->
dpb[
i];
795 for (j = n; j > 0; j--) {
806 rpl1[j] = rpl1[j - 1];
808 rpl1[j] = prev->
dpb[
i];
815 for (
i = 0;
i < n;
i++) {
816 if (rpl0[
i] != rpl1[
i])
827 for (
i = 0;
i < n;
i++) {
830 hn->frame_num,
hn->pic_order_cnt);
837 for (
i = 0;
i < n;
i++) {
840 hn->frame_num,
hn->pic_order_cnt);
876 ((1 << (4 +
sps->log2_max_frame_num_minus4)) - 1);
879 ((1 << (4 +
sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
892 int discard = 0, keep = 0;
898 if (prev->
dpb[
i] == pic->
dpb[j])
902 discard_list[discard] = prev->
dpb[
i];
914 for (
i = 0;
i < discard;
i++) {
939 if (pic->
refs[
i] != def_l0[
i])
964 int need_rplm_l0 = 0, need_rplm_l1 = 0;
971 if (pic->
refs[
i] != def_l0[n0])
975 if (pic->
refs[
i] != def_l1[n1])
1034 vslice->macroblock_info = VA_INVALID_ID;
1045 vslice->RefPicList0[
i].picture_id = VA_INVALID_ID;
1046 vslice->RefPicList0[
i].flags = VA_PICTURE_H264_INVALID;
1047 vslice->RefPicList1[
i].picture_id = VA_INVALID_ID;
1048 vslice->RefPicList1[
i].flags = VA_PICTURE_H264_INVALID;
1056 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
1061 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
1082 if (
ctx->va_rc_mode == VA_RC_CQP) {
1098 "%d / %d / %d for IDR- / P- / B-frames.\n",
1108 if (!
ctx->rc_mode->hrd) {
1115 const char *vaapi = VA_VERSION_S;
1123 driver = vaQueryVendorString(
ctx->hwctx->display);
1125 driver =
"unknown driver";
1134 "%s / VAAPI %s / %s", lavc, vaapi, driver);
1141 ctx->roi_quant_range = 51 + 6 * (
ctx->profile->depth - 8);
1150 8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
1162 .default_quality = 20,
1168 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferH264),
1171 .picture_params_size =
sizeof(VAEncPictureParameterBufferH264),
1174 .slice_params_size =
sizeof(VAEncSliceParameterBufferH264),
1177 .sequence_header_type = VAEncPackedHeaderSequence,
1180 .slice_header_type = VAEncPackedHeaderH264_Slice,
1204 "supported, using constrained baseline profile instead.\n");
1209 "is not supported.\n");
1214 "are not supported.\n");
1223 "are not supported.\n");
1229 "in 8-bit unsigned integer.\n", avctx->
level);
1233 ctx->desired_packed_headers =
1234 VA_ENC_PACKED_HEADER_SEQUENCE |
1235 VA_ENC_PACKED_HEADER_SLICE |
1236 VA_ENC_PACKED_HEADER_MISC;
1241 ctx->slice_block_height =
ctx->slice_block_width = 16;
1244 ctx->explicit_qp = priv->
qp;
1261 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
1262 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1267 {
"qp",
"Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1269 {
"quality",
"Set encode quality (trades off against speed, higher is faster)",
1271 {
"coder",
"Entropy coder type",
1278 {
"aud",
"Include AUD",
1281 {
"sei",
"Set SEI to include",
1284 0, INT_MAX,
FLAGS,
"sei" },
1285 {
"identifier",
"Include encoder version identifier",
1287 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1288 {
"timing",
"Include timing parameters (buffering_period and pic_timing)",
1290 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1291 {
"recovery_point",
"Include recovery points where appropriate",
1293 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1294 {
"a53_cc",
"Include A/53 caption data",
1296 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1298 {
"profile",
"Set profile (profile_idc and constraint_set*_flag)",
1302 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1303 { .i64 = value }, 0, 0, FLAGS, "profile"
1309 {
"level",
"Set level (level_idc)",
1313 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1314 { .i64 = value }, 0, 0, FLAGS, "level"
1316 {
LEVEL(
"1.1", 11) },
1317 {
LEVEL(
"1.2", 12) },
1318 {
LEVEL(
"1.3", 13) },
1320 {
LEVEL(
"2.1", 21) },
1321 {
LEVEL(
"2.2", 22) },
1323 {
LEVEL(
"3.1", 31) },
1324 {
LEVEL(
"3.2", 32) },
1326 {
LEVEL(
"4.1", 41) },
1327 {
LEVEL(
"4.2", 42) },
1329 {
LEVEL(
"5.1", 51) },
1330 {
LEVEL(
"5.2", 52) },
1332 {
LEVEL(
"6.1", 61) },
1333 {
LEVEL(
"6.2", 62) },
1343 {
"i_qfactor",
"1" },
1344 {
"i_qoffset",
"0" },
1345 {
"b_qfactor",
"6/5" },
1346 {
"b_qoffset",
"0" },
1360 .
p.
name =
"h264_vaapi",
1379 .p.wrapper_name =
"vaapi",