20 #include <va/va_enc_mpeg2.h>
67 char *
data,
size_t *data_len,
81 "%zu < %zu.\n", *data_len,
102 "type = %d.\n", type);
110 char *
data,
size_t *data_len)
144 char *
data,
size_t *data_len)
178 int code, ext_n, ext_d;
180 memset(sh, 0,
sizeof(*sh));
181 memset(se, 0,
sizeof(*se));
182 memset(sde, 0,
sizeof(*sde));
183 memset(goph, 0,
sizeof(*goph));
184 memset(ph, 0,
sizeof(*ph));
185 memset(pce, 0,
sizeof(*pce));
202 switch (avctx->
level) {
233 sh->aspect_ratio_information = 1;
235 sh->aspect_ratio_information = 2;
237 sh->aspect_ratio_information = 3;
239 sh->aspect_ratio_information = 4;
242 "representable, signalling square pixels instead.\n",
243 avctx->sample_aspect_ratio.num,
244 avctx->sample_aspect_ratio.den);
245 sh->aspect_ratio_information = 1;
249 sh->aspect_ratio_information = 1;
252 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
253 priv->frame_rate = avctx->framerate;
255 priv->frame_rate =
av_inv_q(avctx->time_base);
257 &code, &ext_n, &ext_d, 0);
258 sh->frame_rate_code = code;
260 sh->bit_rate_value = priv->bit_rate & 0x3ffff;
261 sh->vbv_buffer_size_value = priv->vbv_buffer_size & 0x3ff;
263 sh->constrained_parameters_flag = 0;
264 sh->load_intra_quantiser_matrix = 0;
265 sh->load_non_intra_quantiser_matrix = 0;
271 priv->sequence_extension.extension_start_code_identifier =
274 se->profile_and_level_indication = avctx->profile << 4 | avctx->level;
275 se->progressive_sequence = 1;
276 se->chroma_format = 1;
278 se->horizontal_size_extension = avctx->width >> 12;
279 se->vertical_size_extension = avctx->height >> 12;
281 se->bit_rate_extension = priv->bit_rate >> 18;
282 se->vbv_buffer_size_extension = priv->vbv_buffer_size >> 10;
283 se->low_delay =
ctx->b_per_p == 0;
285 se->frame_rate_extension_n = ext_n;
286 se->frame_rate_extension_d = ext_d;
291 priv->sequence_display_extension.extension_start_code =
293 priv->sequence_display_extension.extension_start_code_identifier =
296 sde->video_format = 5;
300 sde->colour_description = 1;
301 sde->colour_primaries = avctx->color_primaries;
302 sde->transfer_characteristics = avctx->color_trc;
303 sde->matrix_coefficients = avctx->colorspace;
305 sde->colour_description = 0;
308 sde->display_horizontal_size = avctx->width;
309 sde->display_vertical_size = avctx->height;
317 goph->closed_gop = 1;
318 goph->broken_link = 0;
325 ph->vbv_delay = 0xffff;
327 ph->full_pel_forward_vector = 0;
328 ph->forward_f_code = 7;
329 ph->full_pel_backward_vector = 0;
330 ph->forward_f_code = 7;
335 priv->picture_coding_extension.extension_start_code =
337 priv->picture_coding_extension.extension_start_code_identifier =
340 pce->intra_dc_precision = 0;
341 pce->picture_structure = 3;
342 pce->top_field_first = 0;
343 pce->frame_pred_frame_dct = 1;
344 pce->concealment_motion_vectors = 0;
345 pce->q_scale_type = 0;
346 pce->intra_vlc_format = 0;
347 pce->alternate_scan = 0;
348 pce->repeat_first_field = 0;
349 pce->progressive_frame = 1;
350 pce->composite_display_flag = 0;
354 *vseq = (VAEncSequenceParameterBufferMPEG2) {
355 .intra_period =
ctx->gop_size,
356 .ip_period =
ctx->b_per_p + 1,
358 .picture_width = avctx->width,
359 .picture_height = avctx->height,
361 .bits_per_second =
ctx->va_bit_rate,
362 .frame_rate =
av_q2d(priv->frame_rate),
363 .aspect_ratio_information = sh->aspect_ratio_information,
364 .vbv_buffer_size = priv->vbv_buffer_size,
366 .sequence_extension.bits = {
367 .profile_and_level_indication =
se->profile_and_level_indication,
368 .progressive_sequence =
se->progressive_sequence,
369 .chroma_format =
se->chroma_format,
370 .low_delay =
se->low_delay,
371 .frame_rate_extension_n =
se->frame_rate_extension_n,
372 .frame_rate_extension_d =
se->frame_rate_extension_d,
377 .time_code = goph->time_code,
378 .closed_gop = goph->closed_gop,
379 .broken_link = goph->broken_link,
383 *vpic = (VAEncPictureParameterBufferMPEG2) {
384 .forward_reference_picture = VA_INVALID_ID,
385 .backward_reference_picture = VA_INVALID_ID,
386 .reconstructed_picture = VA_INVALID_ID,
387 .coded_buf = VA_INVALID_ID,
390 .f_code = { { 15, 15 }, { 15, 15 } },
392 .picture_coding_extension.bits = {
393 .intra_dc_precision = pce->intra_dc_precision,
394 .picture_structure = pce->picture_structure,
395 .top_field_first = pce->top_field_first,
396 .frame_pred_frame_dct = pce->frame_pred_frame_dct,
397 .concealment_motion_vectors = pce->concealment_motion_vectors,
398 .q_scale_type = pce->q_scale_type,
399 .intra_vlc_format = pce->intra_vlc_format,
400 .alternate_scan = pce->alternate_scan,
401 .repeat_first_field = pce->repeat_first_field,
402 .progressive_frame = pce->progressive_frame,
403 .composite_display_flag = pce->composite_display_flag,
406 .composite_display.bits = {
407 .v_axis = pce->v_axis,
408 .field_sequence = pce->field_sequence,
409 .sub_carrier = pce->sub_carrier,
410 .burst_amplitude = pce->burst_amplitude,
411 .sub_carrier_phase = pce->sub_carrier_phase,
456 vpic->picture_type = VAEncPictureTypeIntra;
459 vpic->picture_type = VAEncPictureTypePredictive;
463 vpic->picture_type = VAEncPictureTypeBidirectional;
472 vpic->f_code[0][0] = pce->
f_code[0][0];
473 vpic->f_code[0][1] = pce->
f_code[0][1];
474 vpic->f_code[1][0] = pce->
f_code[1][0];
475 vpic->f_code[1][1] = pce->
f_code[1][1];
506 vslice->quantiser_scale_code = qp;
541 "%d / %d / %d for I- / P- / B-frames.\n",
568 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferMPEG2),
571 .picture_params_size =
sizeof(VAEncPictureParameterBufferMPEG2),
574 .slice_params_size =
sizeof(VAEncSliceParameterBufferMPEG2),
577 .sequence_header_type = VAEncPackedHeaderSequence,
580 .picture_header_type = VAEncPackedHeaderPicture,
598 switch (avctx->
level) {
610 if (avctx->
height % 4096 == 0 || avctx->
width % 4096 == 0) {
612 "height or width divisible by 4096.\n");
617 VA_ENC_PACKED_HEADER_PICTURE;
634 #define OFFSET(x) offsetof(VAAPIEncodeMPEG2Context, x)
635 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
639 {
"profile",
"Set profile (in profile_and_level_indication)",
643 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
644 { .i64 = value }, 0, 0, FLAGS, "profile"
649 {
"level",
"Set level (in profile_and_level_indication)",
651 { .i64 = 4 }, 0, 15,
FLAGS,
"level" },
653 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
654 { .i64 = value }, 0, 0, FLAGS, "level"
655 {
LEVEL(
"low", 10) },
656 {
LEVEL(
"main", 8) },
657 {
LEVEL(
"high_1440", 6) },
658 {
LEVEL(
"high", 4) },
668 {
"i_qfactor",
"1" },
669 {
"i_qoffset",
"0" },
670 {
"b_qfactor",
"6/5" },
671 {
"b_qoffset",
"0" },
672 {
"global_quality",
"10" },
686 .
name =
"mpeg2_vaapi",
694 .priv_class = &vaapi_encode_mpeg2_class,
696 .
defaults = vaapi_encode_mpeg2_defaults,
701 .wrapper_name =
"vaapi",
static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
MPEG2RawExtensionData sequence_extension
#define FF_PROFILE_MPEG2_MAIN
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
#define se(name, range_min, range_max)
ptrdiff_t const GLvoid * data
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static const VAAPIEncodeProfile vaapi_encode_mpeg2_profiles[]
static av_cold int init(AVCodecContext *avctx)
MPEG2RawSequenceExtension sequence
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
const char * av_default_item_name(void *ptr)
Return the context name.
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
MPEG2RawExtensionData sequence_display_extension
void * codec_sequence_params
static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
unsigned int vbv_buffer_size
float i_quant_offset
qscale offset between P and I-frames
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 int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, char *data, size_t *data_len)
static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx, CodedBitstreamFragment *frag, int type, void *header)
static const AVClass vaapi_encode_mpeg2_class
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...
static double av_q2d(AVRational a)
Convert an AVRational to a double.
VASurfaceID recon_surface
static const uint8_t header[24]
MPEG2RawSequenceHeader sequence_header
static int vaapi_encode_mpeg2_write_fragment(AVCodecContext *avctx, char *data, size_t *data_len, CodedBitstreamFragment *frag)
MPEG2RawPictureCodingExtension picture_coding
size_t data_size
The number of bytes in the bitstream.
AVCodec ff_mpeg2_vaapi_encoder
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
#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.
static const AVCodecDefault vaapi_encode_mpeg2_defaults[]
MPEG2RawPictureHeader picture_header
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_...
static const AVCodecDefault defaults[]
void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, int *code, int *ext_n, int *ext_d, int nonstandard)
void * codec_picture_params
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
int rc_buffer_size
decoder bitstream buffer size
CodedBitstreamContext * cbc
#define PROFILE(name, value)
static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)
int width
picture width / height.
unsigned int f_code_vertical
#define FF_PROFILE_UNKNOWN
const VAAPIEncodeProfile * profiles
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
VAAPIEncodeContext common
void * codec_picture_params
preferred ID for MPEG-1/2 video decoding
uint8_t * data
Pointer to the bitstream form of this fragment.
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice)
const struct VAAPIEncodeType * codec
Libavcodec external API header.
MPEG2RawSequenceDisplayExtension sequence_display
Coded bitstream fragment structure, combining one or more units.
main external API structure.
static const AVOption vaapi_encode_mpeg2_options[]
CodedBitstreamFragment current_fragment
Describe the class of an AVClass context structure.
Context structure for coded bitstream operations.
#define LEVEL(name, value)
Rational number (pair of numerator and denominator).
MPEG2RawExtensionData picture_coding_extension
static const VAAPIEncodeType vaapi_encode_type_mpeg2
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
float b_quant_offset
qscale offset between IP and B-frames
unsigned int f_code_horizontal
static enum AVPixelFormat pix_fmts[]
int global_quality
Global quality for codecs which cannot change it per frame.
#define VAAPI_ENCODE_COMMON_OPTIONS
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *input_image, int *got_packet)
#define FF_PROFILE_MPEG2_SIMPLE
void * codec_slice_params
MPEG2RawGroupOfPicturesHeader gop_header
unsigned int desired_packed_headers
static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
AVPixelFormat
Pixel format.
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
union MPEG2RawExtensionData::@61 data