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,
111 char *
data,
size_t *data_len,
125 "%zu < %zu.\n", *data_len,
147 "type = %d.\n",
header->nal_unit_type);
155 char *
data,
size_t *data_len)
185 char *
data,
size_t *data_len)
211 char *
data,
size_t *data_len)
269 *
type = VAEncPackedHeaderRawData;
279 *
type = VAEncPackedHeaderH264_SEI;
300 VAEncSequenceParameterBufferH264 *vseq =
ctx->codec_sequence_params;
301 VAEncPictureParameterBufferH264 *vpic =
ctx->codec_picture_params;
307 .cabac = priv->
coder,
308 .hrd_buffer_size =
ctx->hrd_params.buffer_size,
310 .initial_buffer_fullness =
ctx->hrd_params.initial_buffer_fullness,
315 &priv->
units, &unit_opts);
319 *vseq = (VAEncSequenceParameterBufferH264) {
320 .seq_parameter_set_id =
sps->seq_parameter_set_id,
321 .level_idc =
sps->level_idc,
323 .intra_idr_period = base_ctx->
gop_size,
324 .ip_period = base_ctx->
b_per_p + 1,
326 .bits_per_second =
ctx->va_bit_rate,
327 .max_num_ref_frames =
sps->max_num_ref_frames,
328 .picture_width_in_mbs =
sps->pic_width_in_mbs_minus1 + 1,
329 .picture_height_in_mbs =
sps->pic_height_in_map_units_minus1 + 1,
332 .chroma_format_idc =
sps->chroma_format_idc,
333 .frame_mbs_only_flag =
sps->frame_mbs_only_flag,
334 .mb_adaptive_frame_field_flag =
sps->mb_adaptive_frame_field_flag,
335 .seq_scaling_matrix_present_flag =
sps->seq_scaling_matrix_present_flag,
336 .direct_8x8_inference_flag =
sps->direct_8x8_inference_flag,
337 .log2_max_frame_num_minus4 =
sps->log2_max_frame_num_minus4,
338 .pic_order_cnt_type =
sps->pic_order_cnt_type,
339 .log2_max_pic_order_cnt_lsb_minus4 =
sps->log2_max_pic_order_cnt_lsb_minus4,
340 .delta_pic_order_always_zero_flag =
sps->delta_pic_order_always_zero_flag,
343 .bit_depth_luma_minus8 =
sps->bit_depth_luma_minus8,
344 .bit_depth_chroma_minus8 =
sps->bit_depth_chroma_minus8,
346 .frame_cropping_flag =
sps->frame_cropping_flag,
347 .frame_crop_left_offset =
sps->frame_crop_left_offset,
348 .frame_crop_right_offset =
sps->frame_crop_right_offset,
349 .frame_crop_top_offset =
sps->frame_crop_top_offset,
350 .frame_crop_bottom_offset =
sps->frame_crop_bottom_offset,
352 .vui_parameters_present_flag =
sps->vui_parameters_present_flag,
355 .aspect_ratio_info_present_flag =
sps->vui.aspect_ratio_info_present_flag,
356 .timing_info_present_flag =
sps->vui.timing_info_present_flag,
357 .bitstream_restriction_flag =
sps->vui.bitstream_restriction_flag,
358 .log2_max_mv_length_horizontal =
sps->vui.log2_max_mv_length_horizontal,
359 .log2_max_mv_length_vertical =
sps->vui.log2_max_mv_length_vertical,
362 .aspect_ratio_idc =
sps->vui.aspect_ratio_idc,
363 .sar_width =
sps->vui.sar_width,
364 .sar_height =
sps->vui.sar_height,
365 .num_units_in_tick =
sps->vui.num_units_in_tick,
366 .time_scale =
sps->vui.time_scale,
369 *vpic = (VAEncPictureParameterBufferH264) {
371 .picture_id = VA_INVALID_ID,
372 .flags = VA_PICTURE_H264_INVALID,
375 .coded_buf = VA_INVALID_ID,
377 .pic_parameter_set_id =
pps->pic_parameter_set_id,
378 .seq_parameter_set_id =
pps->seq_parameter_set_id,
380 .pic_init_qp =
pps->pic_init_qp_minus26 + 26,
381 .num_ref_idx_l0_active_minus1 =
pps->num_ref_idx_l0_default_active_minus1,
382 .num_ref_idx_l1_active_minus1 =
pps->num_ref_idx_l1_default_active_minus1,
384 .chroma_qp_index_offset =
pps->chroma_qp_index_offset,
385 .second_chroma_qp_index_offset =
pps->second_chroma_qp_index_offset,
388 .entropy_coding_mode_flag =
pps->entropy_coding_mode_flag,
389 .weighted_pred_flag =
pps->weighted_pred_flag,
390 .weighted_bipred_idc =
pps->weighted_bipred_idc,
391 .constrained_intra_pred_flag =
pps->constrained_intra_pred_flag,
392 .transform_8x8_mode_flag =
pps->transform_8x8_mode_flag,
393 .deblocking_filter_control_present_flag =
394 pps->deblocking_filter_control_present_flag,
395 .redundant_pic_cnt_present_flag =
pps->redundant_pic_cnt_present_flag,
396 .pic_order_present_flag =
397 pps->bottom_field_pic_order_in_frame_present_flag,
398 .pic_scaling_matrix_present_flag =
pps->pic_scaling_matrix_present_flag,
473 if (
ctx->va_rc_mode == VA_RC_CBR)
489 .exact_match_flag = 1,
490 .broken_link_flag = base_ctx->
b_per_p > 0,
498 size_t sei_a53cc_len;
512 vpic->CurrPic = (VAPictureH264) {
525 href =
ref->codec_priv;
527 vpic->ReferenceFrames[j++] = (VAPictureH264) {
530 .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
538 vpic->ReferenceFrames[j] = (VAPictureH264) {
539 .picture_id = VA_INVALID_ID,
540 .flags = VA_PICTURE_H264_INVALID,
549 vpic->pic_fields.bits.reference_pic_flag = pic->
is_reference;
573 for (j = n; j > 0; j--) {
578 rpl0[j] = rpl0[j - 1];
580 rpl0[j] = prev->
dpb[
i];
583 for (j = n; j > 0; j--) {
594 rpl0[j] = rpl0[j - 1];
596 rpl0[j] = prev->
dpb[
i];
598 for (j = n; j > 0; j--) {
609 rpl1[j] = rpl1[j - 1];
611 rpl1[j] = prev->
dpb[
i];
618 for (
i = 0;
i < n;
i++) {
619 if (rpl0[
i] != rpl1[
i])
630 for (
i = 0;
i < n;
i++) {
633 hn->frame_num,
hn->pic_order_cnt);
640 for (
i = 0;
i < n;
i++) {
643 hn->frame_num,
hn->pic_order_cnt);
680 ((1 << (4 +
sps->log2_max_frame_num_minus4)) - 1);
683 ((1 << (4 +
sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
696 int discard = 0, keep = 0;
702 if (prev->
dpb[
i] == pic->
dpb[j])
706 discard_list[discard] = prev->
dpb[
i];
718 for (
i = 0;
i < discard;
i++) {
768 int need_rplm_l0 = 0, need_rplm_l1 = 0;
837 vslice->macroblock_info = VA_INVALID_ID;
848 vslice->RefPicList0[
i].picture_id = VA_INVALID_ID;
849 vslice->RefPicList0[
i].flags = VA_PICTURE_H264_INVALID;
850 vslice->RefPicList1[
i].picture_id = VA_INVALID_ID;
851 vslice->RefPicList1[
i].flags = VA_PICTURE_H264_INVALID;
858 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
863 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
884 if (
ctx->va_rc_mode == VA_RC_CQP) {
900 "%d / %d / %d for IDR- / P- / B-frames.\n",
910 if (!
ctx->rc_mode->hrd) {
917 const char *vaapi = VA_VERSION_S;
925 driver = vaQueryVendorString(
ctx->hwctx->display);
927 driver =
"unknown driver";
936 "%s / VAAPI %s / %s", lavc, vaapi, driver);
943 ctx->roi_quant_range = 51 + 6 * (
ctx->profile->depth - 8);
949 #if VA_CHECK_VERSION(1, 18, 0)
955 8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
967 .default_quality = 20,
973 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferH264),
976 .picture_params_size =
sizeof(VAEncPictureParameterBufferH264),
979 .slice_params_size =
sizeof(VAEncSliceParameterBufferH264),
982 .sequence_header_type = VAEncPackedHeaderSequence,
985 .slice_header_type = VAEncPackedHeaderH264_Slice,
1010 "supported, using constrained baseline profile instead.\n");
1015 "is not supported.\n");
1019 "is not supported.\n");
1028 "are not supported.\n");
1034 "in 8-bit unsigned integer.\n", avctx->
level);
1038 ctx->desired_packed_headers =
1039 VA_ENC_PACKED_HEADER_SEQUENCE |
1040 VA_ENC_PACKED_HEADER_SLICE |
1041 VA_ENC_PACKED_HEADER_MISC;
1049 ctx->explicit_qp = priv->
qp;
1066 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
1067 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1073 {
"qp",
"Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1075 {
"quality",
"Set encode quality (trades off against speed, higher is faster)",
1077 {
"coder",
"Entropy coder type",
1084 {
"aud",
"Include AUD",
1087 {
"sei",
"Set SEI to include",
1090 0, INT_MAX,
FLAGS, .unit =
"sei" },
1091 {
"identifier",
"Include encoder version identifier",
1093 INT_MIN, INT_MAX,
FLAGS, .unit =
"sei" },
1094 {
"timing",
"Include timing parameters (buffering_period and pic_timing)",
1096 INT_MIN, INT_MAX,
FLAGS, .unit =
"sei" },
1097 {
"recovery_point",
"Include recovery points where appropriate",
1099 INT_MIN, INT_MAX,
FLAGS, .unit =
"sei" },
1100 {
"a53_cc",
"Include A/53 caption data",
1102 INT_MIN, INT_MAX,
FLAGS, .unit =
"sei" },
1104 {
"profile",
"Set profile (profile_idc and constraint_set*_flag)",
1108 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1109 { .i64 = value }, 0, 0, FLAGS, .unit = "profile"
1116 {
"level",
"Set level (level_idc)",
1120 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1121 { .i64 = value }, 0, 0, FLAGS, .unit = "level"
1123 {
LEVEL(
"1.1", 11) },
1124 {
LEVEL(
"1.2", 12) },
1125 {
LEVEL(
"1.3", 13) },
1127 {
LEVEL(
"2.1", 21) },
1128 {
LEVEL(
"2.2", 22) },
1130 {
LEVEL(
"3.1", 31) },
1131 {
LEVEL(
"3.2", 32) },
1133 {
LEVEL(
"4.1", 41) },
1134 {
LEVEL(
"4.2", 42) },
1136 {
LEVEL(
"5.1", 51) },
1137 {
LEVEL(
"5.2", 52) },
1139 {
LEVEL(
"6.1", 61) },
1140 {
LEVEL(
"6.2", 62) },
1150 {
"i_qfactor",
"1" },
1151 {
"i_qoffset",
"0" },
1152 {
"b_qfactor",
"6/5" },
1153 {
"b_qoffset",
"0" },
1167 .
p.
name =
"h264_vaapi",
1187 .p.wrapper_name =
"vaapi",