20 #include <va/va_enc_h264.h>
110 VAEncMiscParameterRateControl
rc;
113 VAEncMiscParameterBuffer
misc;
114 VAEncMiscParameterHRD
hrd;
117 #if VA_CHECK_VERSION(0, 36, 0)
120 VAEncMiscParameterBuffer
misc;
121 VAEncMiscParameterBufferQualityLevel quality;
133 #define vseq_var(name) vseq->name, name
134 #define vseq_field(name) vseq->seq_fields.bits.name, name
135 #define vpic_var(name) vpic->name, name
136 #define vpic_field(name) vpic->pic_fields.bits.name, name
137 #define vslice_var(name) vslice->name, name
138 #define vslice_field(name) vslice->slice_fields.bits.name, name
139 #define mseq_var(name) mseq->name, name
140 #define mslice_var(name) mslice->name, name
143 int nal_unit_type,
int nal_ref_idc)
145 u(1, 0, forbidden_zero_bit);
146 u(2, nal_ref_idc, nal_ref_idc);
147 u(5, nal_unit_type, nal_unit_type);
152 u(1, 1, rbsp_stop_one_bit);
154 u(1, 0, rbsp_alignment_zero_bit);
174 u(2, 0, reserved_zero_2bits);
187 if (vseq->seq_fields.bits.chroma_format_idc == 3)
188 u(1,
mseq_var(separate_colour_plane_flag));
193 u(1,
mseq_var(qpprime_y_zero_transform_bypass_flag));
196 if (vseq->seq_fields.bits.seq_scaling_matrix_present_flag) {
197 av_assert0(0 &&
"scaling matrices not supported");
204 if (vseq->seq_fields.bits.pic_order_cnt_type == 0) {
206 }
else if (vseq->seq_fields.bits.pic_order_cnt_type == 1) {
207 u(1,
mseq_var(delta_pic_order_always_zero_flag));
210 ue(
vseq_var(num_ref_frames_in_pic_order_cnt_cycle));
212 for (i = 0; i < vseq->num_ref_frames_in_pic_order_cnt_cycle; i++)
217 u(1,
mseq_var(gaps_in_frame_num_allowed_flag));
219 ue(vseq->picture_width_in_mbs - 1, pic_width_in_mbs_minus1);
220 ue(vseq->picture_height_in_mbs - 1, pic_height_in_mbs_minus1);
223 if (!vseq->seq_fields.bits.frame_mbs_only_flag)
229 if (vseq->frame_cropping_flag) {
236 u(1,
mseq_var(vui_parameters_present_flag));
254 u(1,
mseq_var(bottom_field_pic_order_in_frame_present_flag));
259 av_assert0(0 &&
"slice groups not supported");
268 se(vpic->pic_init_qp - 26, pic_init_qp_minus26);
272 u(1,
vpic_field(deblocking_filter_control_present_flag));
278 if (vpic->pic_fields.bits.pic_scaling_matrix_present_flag) {
279 av_assert0(0 &&
"scaling matrices not supported");
303 ue(vslice->macroblock_address, first_mb_in_slice);
311 u(4 + vseq->seq_fields.bits.log2_max_frame_num_minus4,
313 ((1 << (4 + vseq->seq_fields.bits.log2_max_frame_num_minus4)) - 1)),
316 if (!vseq->seq_fields.bits.frame_mbs_only_flag) {
322 if (vpic->pic_fields.bits.idr_pic_flag) {
326 if (vseq->seq_fields.bits.pic_order_cnt_type == 0) {
327 u(4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4,
335 if (vseq->seq_fields.bits.pic_order_cnt_type == 1 &&
336 !vseq->seq_fields.bits.delta_pic_order_always_zero_flag) {
344 if (vpic->pic_fields.bits.redundant_pic_cnt_present_flag) {
355 u(1,
vslice_var(num_ref_idx_active_override_flag));
356 if (vslice->num_ref_idx_active_override_flag) {
366 if (vslice->slice_type % 5 != 2 && vslice->slice_type % 5 != 4) {
367 u(1,
mslice_var(ref_pic_list_modification_flag_l0));
372 if (vslice->slice_type % 5 == 1) {
373 u(1,
mslice_var(ref_pic_list_modification_flag_l1));
380 if ((vpic->pic_fields.bits.weighted_pred_flag &&
383 (vpic->pic_fields.bits.weighted_bipred_idc == 1 &&
385 av_assert0(0 &&
"prediction weights not supported");
389 vpic->pic_fields.bits.reference_pic_flag);
391 if (vpic->pic_fields.bits.idr_pic_flag) {
395 u(1,
mslice_var(adaptive_ref_pic_marking_mode_flag));
402 if (vpic->pic_fields.bits.entropy_coding_mode_flag &&
416 if (vpic->pic_fields.bits.deblocking_filter_control_present_flag) {
418 if (vslice->disable_deblocking_filter_idc != 1) {
426 av_assert0(0 &&
"slice groups not supported");
433 char *
data,
size_t *data_len)
439 size_t nal_len, bit_len, bit_pos, next_len;
449 next_len = bit_len - bit_pos;
462 next_len = bit_len - bit_pos;
477 char *
data,
size_t *data_len)
503 vseq->seq_parameter_set_id = 0;
505 vseq->level_idc = avctx->
level;
507 vseq->max_num_ref_frames = 2;
509 vseq->picture_width_in_mbs = priv->
mb_width;
510 vseq->picture_height_in_mbs = priv->
mb_height;
512 vseq->seq_fields.bits.chroma_format_idc = 1;
513 vseq->seq_fields.bits.frame_mbs_only_flag = 1;
514 vseq->seq_fields.bits.direct_8x8_inference_flag = 1;
515 vseq->seq_fields.bits.log2_max_frame_num_minus4 = 4;
516 vseq->seq_fields.bits.pic_order_cnt_type = 0;
520 vseq->frame_cropping_flag = 1;
522 vseq->frame_crop_left_offset = 0;
523 vseq->frame_crop_right_offset =
525 vseq->frame_crop_top_offset = 0;
526 vseq->frame_crop_bottom_offset =
529 vseq->frame_cropping_flag = 0;
532 vseq->bits_per_second = avctx->
bit_rate;
542 vseq->intra_idr_period = vseq->intra_period;
543 vseq->ip_period = ctx->
b_per_p + 1;
547 vpic->CurrPic.picture_id = VA_INVALID_ID;
548 vpic->CurrPic.flags = VA_PICTURE_H264_INVALID;
551 vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
552 vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
555 vpic->coded_buf = VA_INVALID_ID;
557 vpic->pic_parameter_set_id = 0;
558 vpic->seq_parameter_set_id = 0;
560 vpic->num_ref_idx_l0_active_minus1 = 0;
561 vpic->num_ref_idx_l1_active_minus1 = 0;
563 vpic->pic_fields.bits.entropy_coding_mode_flag =
564 ((avctx->
profile & 0xff) != 66);
565 vpic->pic_fields.bits.weighted_pred_flag = 0;
566 vpic->pic_fields.bits.weighted_bipred_idc = 0;
567 vpic->pic_fields.bits.transform_8x8_mode_flag =
568 ((avctx->
profile & 0xff) >= 100);
606 vpic->frame_num = vpic->frame_num &
607 ((1 << (4 + vseq->seq_fields.bits.log2_max_frame_num_minus4)) - 1);
610 vpic->CurrPic.frame_idx = vpic->frame_num;
611 vpic->CurrPic.flags = 0;
615 for (i = 0; i < pic->
nb_refs; i++) {
620 vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
621 vpic->ReferenceFrames[i].TopFieldOrderCnt = ref->
display_order;
622 vpic->ReferenceFrames[i].BottomFieldOrderCnt = ref->
display_order;
625 vpic->ReferenceFrames[i].picture_id = VA_INVALID_ID;
626 vpic->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
685 vslice->macroblock_address = 0;
688 vslice->macroblock_info = VA_INVALID_ID;
690 vslice->pic_parameter_set_id = vpic->pic_parameter_set_id;
694 ((1 << (4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4)) - 1);
697 vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
698 vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
699 vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
700 vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
709 vslice->num_ref_idx_l0_active_minus1 = 0;
710 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
716 vslice->num_ref_idx_l1_active_minus1 = 0;
717 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
721 vslice->slice_qp_delta = priv->
fixed_qp_b - vpic->pic_init_qp;
723 vslice->slice_qp_delta = priv->
fixed_qp_p - vpic->pic_init_qp;
725 vslice->slice_qp_delta = priv->
fixed_qp_idr - vpic->pic_init_qp;
727 vslice->direct_spatial_mv_pred_flag = 1;
737 int hrd_initial_buffer_fullness;
741 "higher is not supported.\n");
752 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
754 priv->
rc_params.
misc.type = VAEncMiscParameterTypeRateControl;
755 priv->
rc_params.
rc = (VAEncMiscParameterRateControl) {
757 .target_percentage = 66,
759 .initial_qp = (avctx->
qmax >= 0 ? avctx->
qmax : 40),
760 .min_qp = (avctx->
qmin >= 0 ? avctx->
qmin : 18),
761 .basic_unit_size = 0,
770 .initial_buffer_fullness = hrd_initial_buffer_fullness,
771 .buffer_size = hrd_buffer_size,
807 "%d / %d / %d for IDR- / P- / B-frames.\n",
814 static const VAConfigAttrib default_config_attributes[] = {
815 { .type = VAConfigAttribRTFormat,
816 .value = VA_RT_FORMAT_YUV420 },
817 { .type = VAConfigAttribEncPackedHeaders,
818 .value = (VA_ENC_PACKED_HEADER_SEQUENCE |
819 VA_ENC_PACKED_HEADER_SLICE) },
829 ctx->
va_profile = VAProfileH264ConstrainedBaseline;
839 "is not supported.\n");
848 "are not supported.\n");
857 "are not supported.\n");
865 #if VA_CHECK_VERSION(0, 39, 1)
869 "supported with this VAAPI version.\n");
885 default_config_attributes[i];
899 .type = VAConfigAttribRateControl,
904 #if VA_CHECK_VERSION(0, 36, 0)
905 priv->quality_params.
misc.type =
906 VAEncMiscParameterTypeQualityLevel;
907 priv->quality_params.quality.quality_level = opt->
quality;
910 &priv->quality_params.
misc;
912 sizeof(priv->quality_params);
915 "supported with this VAAPI version.\n");
929 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferH264),
932 .picture_params_size =
sizeof(VAEncPictureParameterBufferH264),
935 .slice_params_size =
sizeof(VAEncSliceParameterBufferH264),
938 .sequence_header_type = VAEncPackedHeaderSequence,
941 .slice_header_type = VAEncPackedHeaderH264_Slice,
950 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
951 offsetof(VAAPIEncodeH264Options, x))
952 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
954 {
"qp",
"Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
956 {
"quality",
"Set encode quality (trades off against speed, higher is faster)",
958 {
"low_power",
"Use low-power encoding mode (experimental: only supported "
959 "on some platforms, does not support all features)",
965 {
"profile",
"100" },
970 {
"i_qfactor",
"1.0" },
971 {
"i_qoffset",
"0.0" },
972 {
"b_qfactor",
"1.2" },
973 {
"b_qoffset",
"0.0" },
985 .
name =
"h264_vaapi",
994 .priv_class = &vaapi_encode_h264_class,
996 .
defaults = vaapi_encode_h264_defaults,
#define FF_PROFILE_H264_MAIN
#define FF_PROFILE_H264_CAVLC_444
VAEntrypoint va_entrypoint
ptrdiff_t const GLvoid * data
static void vaapi_encode_h264_write_slice_header2(PutBitContext *pbc, VAAPIEncodeContext *ctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice)
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
#define FF_PROFILE_H264_INTRA
char adaptive_ref_pic_marking_mode_flag
void * codec_sequence_params
VAEncMiscParameterHRD hrd
VAEncMiscParameterBuffer misc
float i_quant_offset
qscale offset between P and I-frames
static const AVClass vaapi_encode_h264_class
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
static void vaapi_encode_h264_write_nal_header(PutBitContext *pbc, int nal_unit_type, int nal_ref_idc)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#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_assert0(cond)
assert() equivalent, that is always enabled.
static VAAPIEncodeType vaapi_encode_type_h264
static const AVOption vaapi_encode_h264_options[]
#define FF_PROFILE_H264_BASELINE
static const AVCodecDefault vaapi_encode_h264_defaults[]
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...
char delta_pic_order_always_zero_flag
char constraint_set3_flag
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx, const VAAPIEncodeType *type)
static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
char constraint_set5_flag
static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
#define FF_PROFILE_H264_EXTENDED
static void vaapi_encode_h264_write_pps(PutBitContext *pbc, VAAPIEncodeContext *ctx)
VASurfaceID recon_surface
H.264 / AVC / MPEG-4 part10 codec.
VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
char vui_parameters_present_flag
int qmax
maximum quantizer
int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *input_image, int *got_packet)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
char constraint_set2_flag
VAEncMiscParameterBuffer * global_params[MAX_GLOBAL_PARAMS]
#define FF_PROFILE_H264_HIGH_422
#define FF_PROFILE_H264_HIGH
static av_cold int vaapi_encode_h264_init_fixed_qp(AVCodecContext *avctx)
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
char ref_pic_list_modification_flag_l0
struct VAAPIEncodeH264Context::@113 rc_params
static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
void * codec_picture_params
char constraint_set1_flag
static int put_bits_count(PutBitContext *s)
int rc_buffer_size
decoder bitstream buffer size
char gaps_in_frame_num_allowed_flag
common internal API header
AVCodec ff_h264_vaapi_encoder
static void vaapi_encode_h264_write_sps(PutBitContext *pbc, VAAPIEncodeContext *ctx)
char constraint_set0_flag
static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
int width
picture width / height.
#define FF_PROFILE_UNKNOWN
char separate_colour_plane_flag
unsigned int num_slice_groups_minus1
void * codec_picture_params
static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice, char *data, size_t *data_len)
#define FF_ARRAY_ELEMS(a)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
unsigned int colour_plane_id
Libavcodec external API header.
unsigned int slice_group_map_type
#define FF_PROFILE_H264_HIGH_422_INTRA
main external API structure.
char long_term_reference_flag
int qmin
minimum quantizer
char ref_pic_list_modification_flag_l1
char no_output_of_prior_pics_flag
#define FF_PROFILE_H264_HIGH_10_INTRA
Describe the class of an AVClass context structure.
char bottom_field_pic_order_in_frame_present_flag
unsigned int redundant_pic_cnt
struct VAAPIEncodeH264Context::@114 hrd_params
#define FF_PROFILE_H264_HIGH_444
float b_quant_offset
qscale offset between IP and B-frames
static enum AVPixelFormat pix_fmts[]
unsigned int nal_unit_type
VAAPIEncodeH264MiscSliceParams misc_slice_params
static av_cold int vaapi_encode_h264_init_internal(AVCodecContext *avctx)
size_t global_params_size[MAX_GLOBAL_PARAMS]
static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice)
VAEncMiscParameterRateControl rc
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static int ref[MAX_W *MAX_W]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static av_cold int vaapi_encode_h264_init_constant_bitrate(AVCodecContext *avctx)
char qpprime_y_zero_transform_bypass_flag
void * codec_slice_params
#define FF_PROFILE_H264_HIGH_444_INTRA
static void vaapi_encode_h264_write_trailing_rbsp(PutBitContext *pbc)
static const AVCodecDefault defaults[]
#define FF_PROFILE_H264_CONSTRAINED
#define FF_PROFILE_H264_CONSTRAINED_BASELINE
#define FF_PROFILE_H264_HIGH_10
AVPixelFormat
Pixel format.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
VAAPIEncodeH264MiscSequenceParams misc_sequence_params
int ff_vaapi_encode_h26x_nal_unit_to_byte_stream(uint8_t *dst, size_t *dst_bit_len, uint8_t *src, size_t src_bit_len)
char constraint_set4_flag