22 #include <va/va_enc_h264.h>
47 0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
48 0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
109 char *
data,
size_t *data_len,
123 "%zu < %zu.\n", *data_len,
145 "type = %d.\n",
header->nal_unit_type);
153 char *
data,
size_t *data_len)
183 char *
data,
size_t *data_len)
209 char *
data,
size_t *data_len)
260 *
type = VAEncPackedHeaderRawData;
270 *
type = VAEncPackedHeaderH264_SEI;
290 VAEncSequenceParameterBufferH264 *vseq =
ctx->codec_sequence_params;
291 VAEncPictureParameterBufferH264 *vpic =
ctx->codec_picture_params;
293 memset(
sps, 0,
sizeof(*
sps));
294 memset(
pps, 0,
sizeof(*
pps));
296 sps->nal_unit_header.nal_ref_idc = 3;
303 sps->constraint_set1_flag = 1;
306 sps->constraint_set3_flag =
ctx->gop_size == 1;
310 sps->constraint_set4_flag = 1;
311 sps->constraint_set5_flag =
ctx->b_per_p == 0;
314 if (
ctx->gop_size == 1)
338 if (
level->constraint_set3_flag)
339 sps->constraint_set3_flag = 1;
343 "to any level: using level 6.2.\n");
348 sps->seq_parameter_set_id = 0;
349 sps->chroma_format_idc = 1;
351 sps->log2_max_frame_num_minus4 = 4;
352 sps->pic_order_cnt_type = 0;
353 sps->log2_max_pic_order_cnt_lsb_minus4 = 4;
358 sps->pic_height_in_map_units_minus1 = priv->
mb_height - 1;
360 sps->frame_mbs_only_flag = 1;
361 sps->direct_8x8_inference_flag = 1;
365 sps->frame_cropping_flag = 1;
367 sps->frame_crop_left_offset = 0;
368 sps->frame_crop_right_offset =
370 sps->frame_crop_top_offset = 0;
371 sps->frame_crop_bottom_offset =
374 sps->frame_cropping_flag = 0;
377 sps->vui_parameters_present_flag = 1;
383 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
384 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
385 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
386 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
392 if (num == sar_idc[
i].num &&
393 den == sar_idc[
i].den) {
394 sps->vui.aspect_ratio_idc =
i;
399 sps->vui.aspect_ratio_idc = 255;
400 sps->vui.sar_width = num;
401 sps->vui.sar_height = den;
403 sps->vui.aspect_ratio_info_present_flag = 1;
407 sps->vui.video_format = 5;
408 sps->vui.video_full_range_flag =
416 sps->vui.colour_description_present_flag = 1;
418 sps->vui.colour_description_present_flag)
419 sps->vui.video_signal_type_present_flag = 1;
422 sps->vui.chroma_loc_info_present_flag = 1;
423 sps->vui.chroma_sample_loc_type_top_field =
424 sps->vui.chroma_sample_loc_type_bottom_field =
428 sps->vui.timing_info_present_flag = 1;
432 sps->vui.fixed_frame_rate_flag = 1;
436 sps->vui.fixed_frame_rate_flag = 0;
443 sps->vui.nal_hrd_parameters_present_flag = 1;
472 (uint64_t)
ctx->hrd_params.initial_buffer_fullness /
473 ctx->hrd_params.buffer_size;
476 sps->vui.nal_hrd_parameters_present_flag = 0;
477 sps->vui.low_delay_hrd_flag = 1 -
sps->vui.fixed_frame_rate_flag;
480 sps->vui.bitstream_restriction_flag = 1;
481 sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
482 sps->vui.log2_max_mv_length_horizontal = 15;
483 sps->vui.log2_max_mv_length_vertical = 15;
484 sps->vui.max_num_reorder_frames =
ctx->max_b_depth;
485 sps->vui.max_dec_frame_buffering =
ctx->max_b_depth + 1;
487 pps->nal_unit_header.nal_ref_idc = 3;
490 pps->pic_parameter_set_id = 0;
491 pps->seq_parameter_set_id = 0;
493 pps->entropy_coding_mode_flag =
497 if (!priv->
coder &&
pps->entropy_coding_mode_flag)
498 pps->entropy_coding_mode_flag = 0;
500 pps->num_ref_idx_l0_default_active_minus1 = 0;
501 pps->num_ref_idx_l1_default_active_minus1 = 0;
508 pps->more_rbsp_data = 0;
510 pps->more_rbsp_data = 1;
512 pps->transform_8x8_mode_flag = 1;
515 *vseq = (VAEncSequenceParameterBufferH264) {
516 .seq_parameter_set_id =
sps->seq_parameter_set_id,
517 .level_idc =
sps->level_idc,
518 .intra_period =
ctx->gop_size,
519 .intra_idr_period =
ctx->gop_size,
520 .ip_period =
ctx->b_per_p + 1,
522 .bits_per_second =
ctx->va_bit_rate,
523 .max_num_ref_frames =
sps->max_num_ref_frames,
524 .picture_width_in_mbs =
sps->pic_width_in_mbs_minus1 + 1,
525 .picture_height_in_mbs =
sps->pic_height_in_map_units_minus1 + 1,
528 .chroma_format_idc =
sps->chroma_format_idc,
529 .frame_mbs_only_flag =
sps->frame_mbs_only_flag,
530 .mb_adaptive_frame_field_flag =
sps->mb_adaptive_frame_field_flag,
531 .seq_scaling_matrix_present_flag =
sps->seq_scaling_matrix_present_flag,
532 .direct_8x8_inference_flag =
sps->direct_8x8_inference_flag,
533 .log2_max_frame_num_minus4 =
sps->log2_max_frame_num_minus4,
534 .pic_order_cnt_type =
sps->pic_order_cnt_type,
535 .log2_max_pic_order_cnt_lsb_minus4 =
sps->log2_max_pic_order_cnt_lsb_minus4,
536 .delta_pic_order_always_zero_flag =
sps->delta_pic_order_always_zero_flag,
539 .bit_depth_luma_minus8 =
sps->bit_depth_luma_minus8,
540 .bit_depth_chroma_minus8 =
sps->bit_depth_chroma_minus8,
542 .frame_cropping_flag =
sps->frame_cropping_flag,
543 .frame_crop_left_offset =
sps->frame_crop_left_offset,
544 .frame_crop_right_offset =
sps->frame_crop_right_offset,
545 .frame_crop_top_offset =
sps->frame_crop_top_offset,
546 .frame_crop_bottom_offset =
sps->frame_crop_bottom_offset,
548 .vui_parameters_present_flag =
sps->vui_parameters_present_flag,
551 .aspect_ratio_info_present_flag =
sps->vui.aspect_ratio_info_present_flag,
552 .timing_info_present_flag =
sps->vui.timing_info_present_flag,
553 .bitstream_restriction_flag =
sps->vui.bitstream_restriction_flag,
554 .log2_max_mv_length_horizontal =
sps->vui.log2_max_mv_length_horizontal,
555 .log2_max_mv_length_vertical =
sps->vui.log2_max_mv_length_vertical,
558 .aspect_ratio_idc =
sps->vui.aspect_ratio_idc,
559 .sar_width =
sps->vui.sar_width,
560 .sar_height =
sps->vui.sar_height,
561 .num_units_in_tick =
sps->vui.num_units_in_tick,
562 .time_scale =
sps->vui.time_scale,
565 *vpic = (VAEncPictureParameterBufferH264) {
567 .picture_id = VA_INVALID_ID,
568 .flags = VA_PICTURE_H264_INVALID,
571 .coded_buf = VA_INVALID_ID,
573 .pic_parameter_set_id =
pps->pic_parameter_set_id,
574 .seq_parameter_set_id =
pps->seq_parameter_set_id,
576 .pic_init_qp =
pps->pic_init_qp_minus26 + 26,
577 .num_ref_idx_l0_active_minus1 =
pps->num_ref_idx_l0_default_active_minus1,
578 .num_ref_idx_l1_active_minus1 =
pps->num_ref_idx_l1_default_active_minus1,
580 .chroma_qp_index_offset =
pps->chroma_qp_index_offset,
581 .second_chroma_qp_index_offset =
pps->second_chroma_qp_index_offset,
584 .entropy_coding_mode_flag =
pps->entropy_coding_mode_flag,
585 .weighted_pred_flag =
pps->weighted_pred_flag,
586 .weighted_bipred_idc =
pps->weighted_bipred_idc,
587 .constrained_intra_pred_flag =
pps->constrained_intra_pred_flag,
588 .transform_8x8_mode_flag =
pps->transform_8x8_mode_flag,
589 .deblocking_filter_control_present_flag =
590 pps->deblocking_filter_control_present_flag,
591 .redundant_pic_cnt_present_flag =
pps->redundant_pic_cnt_present_flag,
592 .pic_order_present_flag =
593 pps->bottom_field_pic_order_in_frame_present_flag,
594 .pic_scaling_matrix_present_flag =
pps->pic_scaling_matrix_present_flag,
661 if (
ctx->va_rc_mode == VA_RC_CBR)
677 .exact_match_flag = 1,
678 .broken_link_flag =
ctx->b_per_p > 0,
684 vpic->CurrPic = (VAPictureH264) {
697 href =
ref->priv_data;
699 vpic->ReferenceFrames[
i] = (VAPictureH264) {
700 .picture_id =
ref->recon_surface,
702 .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
708 vpic->ReferenceFrames[
i] = (VAPictureH264) {
709 .picture_id = VA_INVALID_ID,
710 .flags = VA_PICTURE_H264_INVALID,
743 for (j = n; j > 0; j--) {
748 rpl0[j] = rpl0[j - 1];
750 rpl0[j] = prev->
dpb[
i];
753 for (j = n; j > 0; j--) {
764 rpl0[j] = rpl0[j - 1];
766 rpl0[j] = prev->
dpb[
i];
768 for (j = n; j > 0; j--) {
779 rpl1[j] = rpl1[j - 1];
781 rpl1[j] = prev->
dpb[
i];
788 for (
i = 0;
i < n;
i++) {
789 if (rpl0[
i] != rpl1[
i])
800 for (
i = 0;
i < n;
i++) {
803 hn->frame_num,
hn->pic_order_cnt);
810 for (
i = 0;
i < n;
i++) {
813 hn->frame_num,
hn->pic_order_cnt);
849 ((1 << (4 +
sps->log2_max_frame_num_minus4)) - 1);
852 ((1 << (4 +
sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
865 int discard = 0, keep = 0;
871 if (prev->
dpb[
i] == pic->
dpb[j])
875 discard_list[discard] = prev->
dpb[
i];
887 for (
i = 0;
i < discard;
i++) {
912 if (pic->
refs[
i] != def_l0[
i])
937 int need_rplm_l0 = 0, need_rplm_l1 = 0;
944 if (pic->
refs[
i] != def_l0[n0])
948 if (pic->
refs[
i] != def_l1[n1])
1007 vslice->macroblock_info = VA_INVALID_ID;
1018 vslice->RefPicList0[
i].picture_id = VA_INVALID_ID;
1019 vslice->RefPicList0[
i].flags = VA_PICTURE_H264_INVALID;
1020 vslice->RefPicList1[
i].picture_id = VA_INVALID_ID;
1021 vslice->RefPicList1[
i].flags = VA_PICTURE_H264_INVALID;
1029 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
1034 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
1055 if (
ctx->va_rc_mode == VA_RC_CQP) {
1071 "%d / %d / %d for IDR- / P- / B-frames.\n",
1081 if (!
ctx->rc_mode->hrd) {
1088 const char *vaapi = VA_VERSION_S;
1096 driver = vaQueryVendorString(
ctx->hwctx->display);
1098 driver =
"unknown driver";
1107 "%s / VAAPI %s / %s", lavc, vaapi, driver);
1114 ctx->roi_quant_range = 51 + 6 * (
ctx->profile->depth - 8);
1123 8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
1135 .default_quality = 20,
1141 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferH264),
1144 .picture_params_size =
sizeof(VAEncPictureParameterBufferH264),
1147 .slice_params_size =
sizeof(VAEncSliceParameterBufferH264),
1150 .sequence_header_type = VAEncPackedHeaderSequence,
1153 .slice_header_type = VAEncPackedHeaderH264_Slice,
1177 "supported, using constrained baseline profile instead.\n");
1182 "is not supported.\n");
1187 "are not supported.\n");
1196 "are not supported.\n");
1202 "in 8-bit unsigned integer.\n", avctx->
level);
1206 ctx->desired_packed_headers =
1207 VA_ENC_PACKED_HEADER_SEQUENCE |
1208 VA_ENC_PACKED_HEADER_SLICE |
1209 VA_ENC_PACKED_HEADER_MISC;
1214 ctx->slice_block_height =
ctx->slice_block_width = 16;
1217 ctx->explicit_qp = priv->
qp;
1233 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
1234 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1239 {
"qp",
"Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1241 {
"quality",
"Set encode quality (trades off against speed, higher is faster)",
1243 {
"coder",
"Entropy coder type",
1250 {
"aud",
"Include AUD",
1253 {
"sei",
"Set SEI to include",
1256 0, INT_MAX,
FLAGS,
"sei" },
1257 {
"identifier",
"Include encoder version identifier",
1259 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1260 {
"timing",
"Include timing parameters (buffering_period and pic_timing)",
1262 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1263 {
"recovery_point",
"Include recovery points where appropriate",
1265 INT_MIN, INT_MAX,
FLAGS,
"sei" },
1267 {
"profile",
"Set profile (profile_idc and constraint_set*_flag)",
1271 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1272 { .i64 = value }, 0, 0, FLAGS, "profile"
1278 {
"level",
"Set level (level_idc)",
1282 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1283 { .i64 = value }, 0, 0, FLAGS, "level"
1285 {
LEVEL(
"1.1", 11) },
1286 {
LEVEL(
"1.2", 12) },
1287 {
LEVEL(
"1.3", 13) },
1289 {
LEVEL(
"2.1", 21) },
1290 {
LEVEL(
"2.2", 22) },
1292 {
LEVEL(
"3.1", 31) },
1293 {
LEVEL(
"3.2", 32) },
1295 {
LEVEL(
"4.1", 41) },
1296 {
LEVEL(
"4.2", 42) },
1298 {
LEVEL(
"5.1", 51) },
1299 {
LEVEL(
"5.2", 52) },
1301 {
LEVEL(
"6.1", 61) },
1302 {
LEVEL(
"6.2", 62) },
1312 {
"i_qfactor",
"1" },
1313 {
"i_qoffset",
"0" },
1314 {
"b_qfactor",
"6/5" },
1315 {
"b_qoffset",
"0" },
1329 .
p.
name =
"h264_vaapi",
1347 .p.wrapper_name =
"vaapi",