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,
117 err = ff_cbs_write_fragment_data(priv->
cbc, au);
125 "%zu < %zu.\n", *data_len,
143 err = ff_cbs_insert_unit_content(au, -1,
147 "type = %d.\n",
header->nal_unit_type);
155 char *
data,
size_t *data_len)
178 ff_cbs_fragment_reset(au);
185 char *
data,
size_t *data_len)
204 ff_cbs_fragment_reset(au);
211 char *
data,
size_t *data_len)
267 ff_cbs_fragment_reset(au);
269 *
type = VAEncPackedHeaderRawData;
279 *
type = VAEncPackedHeaderH264_SEI;
289 ff_cbs_fragment_reset(au);
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,
311 .bit_rate =
ctx->va_bit_rate,
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)
479 .cpb_removal_delay = 2 * hpic->
cpb_delay,
488 .recovery_frame_cnt = 0,
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;
1059 ff_cbs_close(&priv->
cbc);
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" },
1161 .class_name =
"h264_vaapi",
1168 .p.name =
"h264_vaapi",
1185 .p.wrapper_name =
"vaapi",
static float hn(int n, EqParameter *param, float fs)
const FFCodec ff_h264_vaapi_encoder
int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len, void **data, size_t *sei_size)
Check AVFrame for A53 side data and allocate and fill SEI message with A53 info.
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define FF_COMPRESSION_DEFAULT
#define i(width, name, range_min, range_max)
static int FUNC sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
static int FUNC aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
int ff_cbs_sei_add_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, int prefix, uint32_t payload_type, void *payload_data, void *payload_ref)
Add an SEI message to an access unit.
#define CODEC_PIXFMTS(...)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_RECEIVE_PACKET_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal and external API header
#define AV_PROFILE_H264_MAIN
#define AV_PROFILE_H264_EXTENDED
#define AV_PROFILE_UNKNOWN
#define AV_PROFILE_H264_HIGH_444_PREDICTIVE
#define AV_PROFILE_H264_HIGH_444
#define AV_PROFILE_H264_CAVLC_444
#define AV_PROFILE_H264_HIGH_422_INTRA
#define AV_PROFILE_H264_HIGH_444_INTRA
#define AV_PROFILE_H264_HIGH
#define AV_PROFILE_H264_CONSTRAINED_BASELINE
#define AV_PROFILE_H264_HIGH_10
#define AV_PROFILE_H264_HIGH_422
#define AV_PROFILE_H264_HIGH_10_INTRA
#define AV_PROFILE_H264_BASELINE
int(* init)(AVBSFContext *ctx)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_EOF
End of file.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
#define LIBAVUTIL_VERSION_INT
H.264 common definitions.
#define HW_BASE_ENCODE_COMMON_OPTIONS
@ FF_HW_FLAG_SLICE_CONTROL
@ FF_HW_FLAG_NON_IDR_KEY_PICTURES
@ FF_HW_FLAG_B_PICTURE_REFERENCES
#define MAX_REFERENCE_LIST_NUM
int ff_hw_base_encode_init_params_h264(FFHWBaseEncodeContext *base_ctx, AVCodecContext *avctx, FFHWBaseEncodeH264 *common, FFHWBaseEncodeH264Opts *opts)
#define FF_HW_H264_SEI_TIMING
const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[]
int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
Libavcodec version macros.
#define FFSWAP(type, a, b)
Memory handling functions.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AVCOL_RANGE_JPEG
Full range content.
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
static const uint8_t header[24]
@ SEI_TYPE_RECOVERY_POINT
@ SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
@ SEI_TYPE_USER_DATA_UNREGISTERED
@ SEI_TYPE_BUFFERING_PERIOD
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
main external API structure.
int width
picture width / height.
float b_quant_offset
qscale offset between IP and B-frames
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
int level
Encoding level descriptor.
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
float i_quant_offset
qscale offset between P and I-frames
Context structure for coded bitstream operations.
Coded bitstream fragment structure, combining one or more units.
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
size_t data_size
The number of bytes in the bitstream.
uint8_t * data
Pointer to the bitstream form of this fragment.
H264RawSEIBufferingPeriod sei_buffering_period
struct FFHWBaseEncodePicture * dpb[MAX_DPB_SIZE]
struct FFHWBaseEncodePicture * prev
int nb_refs[MAX_REFERENCE_LIST_NUM]
struct FFHWBaseEncodePicture * refs[MAX_REFERENCE_LIST_NUM][MAX_PICTURE_REFERENCES]
uint8_t pic_order_cnt_type
H264RawSliceHeader header
uint8_t itu_t_t35_country_code
uint8_t uuid_iso_iec_11578[16]
char * sei_identifier_string
VAAPIEncodeContext common
H264RawSEIPicTiming sei_pic_timing
H264RawSEIRecoveryPoint sei_recovery_point
SEIRawUserDataRegistered sei_a53cc
SEIRawUserDataUnregistered sei_identifier
CodedBitstreamFragment current_access_unit
int sei_cbr_workaround_needed
CodedBitstreamContext * cbc
void * codec_picture_params
VASurfaceID recon_surface
void * codec_slice_params
static int ref[MAX_W *MAX_W]
static AVFormatContext * ctx
#define VAAPI_ENCODE_COMMON_OPTIONS
#define VAAPI_ENCODE_RC_OPTIONS
static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, VAAPIEncodeSlice *slice)
static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
static const FFCodecDefault vaapi_encode_h264_defaults[]
static const VAAPIEncodeType vaapi_encode_type_h264
static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice, char *data, size_t *data_len)
static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
static void vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, FFHWBaseEncodePicture **rpl0, FFHWBaseEncodePicture **rpl1, int *rpl_size)
static const VAAPIEncodeProfile vaapi_encode_h264_profiles[]
static const AVOption vaapi_encode_h264_options[]
static const uint8_t vaapi_encode_h264_sei_identifier_uuid[16]
#define LEVEL(name, value)
static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx, FFHWBaseEncodePicture *base, int index, int *type, char *data, size_t *data_len)
#define PROFILE(name, value)
static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx, FFHWBaseEncodePicture *pic)
static const AVClass vaapi_encode_h264_class
static int vaapi_encode_h264_add_nal(AVCodecContext *avctx, CodedBitstreamFragment *au, void *nal_unit)
static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
static int vaapi_encode_h264_write_access_unit(AVCodecContext *avctx, char *data, size_t *data_len, CodedBitstreamFragment *au)
static const uint8_t quality[]