2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <va/va_enc_mpeg2.h>
22 #include "libavutil/avassert.h"
26 #include "cbs_mpeg2.h"
28 #include "vaapi_encode.h"
30 typedef struct VAAPIEncodeMPEG2Context {
38 MPEG2RawSequenceHeader sequence_header;
39 MPEG2RawExtensionData sequence_extension;
40 MPEG2RawExtensionData sequence_display_extension;
41 MPEG2RawGroupOfPicturesHeader gop_header;
42 MPEG2RawPictureHeader picture_header;
43 MPEG2RawExtensionData picture_coding_extension;
47 unsigned int bit_rate;
48 unsigned int vbv_buffer_size;
50 AVRational frame_rate;
52 unsigned int f_code_horizontal;
53 unsigned int f_code_vertical;
55 CodedBitstreamContext *cbc;
56 CodedBitstreamFragment current_fragment;
57 } VAAPIEncodeMPEG2Context;
60 static int vaapi_encode_mpeg2_write_fragment(AVCodecContext *avctx,
61 char *data, size_t *data_len,
62 CodedBitstreamFragment *frag)
64 VAAPIEncodeContext *ctx = avctx->priv_data;
65 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
68 err = ff_cbs_write_fragment_data(priv->cbc, frag);
70 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
74 if (*data_len < 8 * frag->data_size - frag->data_bit_padding) {
75 av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
76 "%zu < %zu.\n", *data_len,
77 8 * frag->data_size - frag->data_bit_padding);
78 return AVERROR(ENOSPC);
81 memcpy(data, frag->data, frag->data_size);
82 *data_len = 8 * frag->data_size - frag->data_bit_padding;
87 static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx,
88 CodedBitstreamFragment *frag,
89 int type, void *header)
91 VAAPIEncodeContext *ctx = avctx->priv_data;
92 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
95 err = ff_cbs_insert_unit_content(priv->cbc, frag, -1, type, header);
97 av_log(avctx, AV_LOG_ERROR, "Failed to add header: "
98 "type = %d.\n", type);
105 static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx,
106 char *data, size_t *data_len)
108 VAAPIEncodeContext *ctx = avctx->priv_data;
109 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
110 CodedBitstreamFragment *frag = &priv->current_fragment;
113 err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_SEQUENCE_HEADER,
114 &priv->sequence_header);
118 err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
119 &priv->sequence_extension);
123 err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
124 &priv->sequence_display_extension);
128 err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_GROUP,
133 err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
135 ff_cbs_fragment_uninit(priv->cbc, frag);
139 static int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx,
140 VAAPIEncodePicture *pic,
141 char *data, size_t *data_len)
143 VAAPIEncodeContext *ctx = avctx->priv_data;
144 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
145 CodedBitstreamFragment *frag = &priv->current_fragment;
148 err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_PICTURE,
149 &priv->picture_header);
153 err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
154 &priv->picture_coding_extension);
158 err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
160 ff_cbs_fragment_uninit(priv->cbc, frag);
164 static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)
166 VAAPIEncodeContext *ctx = avctx->priv_data;
167 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
168 MPEG2RawSequenceHeader *sh = &priv->sequence_header;
169 MPEG2RawSequenceExtension *se = &priv->sequence_extension.data.sequence;
170 MPEG2RawSequenceDisplayExtension *sde = &priv->sequence_display_extension.data.sequence_display;
171 MPEG2RawGroupOfPicturesHeader *goph = &priv->gop_header;
172 MPEG2RawPictureHeader *ph = &priv->picture_header;
173 MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
174 VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params;
175 VAEncPictureParameterBufferMPEG2 *vpic = ctx->codec_picture_params;
176 int code, ext_n, ext_d;
178 memset(sh, 0, sizeof(*sh));
179 memset(se, 0, sizeof(*se));
180 memset(sde, 0, sizeof(*sde));
181 memset(goph, 0, sizeof(*goph));
182 memset(ph, 0, sizeof(*ph));
183 memset(pce, 0, sizeof(*pce));
186 if (avctx->bit_rate > 0) {
187 priv->bit_rate = (avctx->bit_rate + 399) / 400;
189 // Unknown (not a bitrate-targetting mode), so just use the
191 priv->bit_rate = 0x3fffffff;
193 if (avctx->rc_buffer_size > 0) {
194 priv->vbv_buffer_size = (avctx->rc_buffer_size + (1 << 14) - 1) >> 14;
196 // Unknown, so guess a value from the bitrate.
197 priv->vbv_buffer_size = priv->bit_rate >> 14;
200 switch (avctx->level) {
202 case 6: // High 1440.
203 priv->f_code_horizontal = 9;
204 priv->f_code_vertical = 5;
207 priv->f_code_horizontal = 8;
208 priv->f_code_vertical = 5;
212 priv->f_code_horizontal = 7;
213 priv->f_code_vertical = 4;
220 sh->sequence_header_code = MPEG2_START_SEQUENCE_HEADER;
222 sh->horizontal_size_value = avctx->width & 0xfff;
223 sh->vertical_size_value = avctx->height & 0xfff;
225 if (avctx->sample_aspect_ratio.num != 0 &&
226 avctx->sample_aspect_ratio.den != 0) {
227 AVRational dar = av_div_q(avctx->sample_aspect_ratio,
228 (AVRational) { avctx->width, avctx->height });
230 if (av_cmp_q(avctx->sample_aspect_ratio, (AVRational) { 1, 1 }) == 0) {
231 sh->aspect_ratio_information = 1;
232 } else if (av_cmp_q(dar, (AVRational) { 3, 4 }) == 0) {
233 sh->aspect_ratio_information = 2;
234 } else if (av_cmp_q(dar, (AVRational) { 9, 16 }) == 0) {
235 sh->aspect_ratio_information = 3;
236 } else if (av_cmp_q(dar, (AVRational) { 100, 221 }) == 0) {
237 sh->aspect_ratio_information = 4;
239 av_log(avctx, AV_LOG_WARNING, "Sample aspect ratio %d:%d is not "
240 "representable, signalling square pixels instead.\n",
241 avctx->sample_aspect_ratio.num,
242 avctx->sample_aspect_ratio.den);
243 sh->aspect_ratio_information = 1;
246 // Unknown - assume square pixels.
247 sh->aspect_ratio_information = 1;
250 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
251 priv->frame_rate = avctx->framerate;
253 priv->frame_rate = av_inv_q(avctx->time_base);
254 ff_mpeg12_find_best_frame_rate(priv->frame_rate,
255 &code, &ext_n, &ext_d, 0);
256 sh->frame_rate_code = code;
258 sh->bit_rate_value = priv->bit_rate & 0x3ffff;
259 sh->vbv_buffer_size_value = priv->vbv_buffer_size & 0x3ff;
261 sh->constrained_parameters_flag = 0;
262 sh->load_intra_quantiser_matrix = 0;
263 sh->load_non_intra_quantiser_matrix = 0;
266 // Sequence extension
268 priv->sequence_extension.extension_start_code = MPEG2_START_EXTENSION;
269 priv->sequence_extension.extension_start_code_identifier =
270 MPEG2_EXTENSION_SEQUENCE;
272 se->profile_and_level_indication = avctx->profile << 4 | avctx->level;
273 se->progressive_sequence = 1;
274 se->chroma_format = 1;
276 se->horizontal_size_extension = avctx->width >> 12;
277 se->vertical_size_extension = avctx->height >> 12;
279 se->bit_rate_extension = priv->bit_rate >> 18;
280 se->vbv_buffer_size_extension = priv->vbv_buffer_size >> 10;
281 se->low_delay = ctx->b_per_p == 0;
283 se->frame_rate_extension_n = ext_n;
284 se->frame_rate_extension_d = ext_d;
287 // Sequence display extension
289 priv->sequence_display_extension.extension_start_code =
290 MPEG2_START_EXTENSION;
291 priv->sequence_display_extension.extension_start_code_identifier =
292 MPEG2_EXTENSION_SEQUENCE_DISPLAY;
294 sde->video_format = 5;
295 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
296 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
297 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
298 sde->colour_description = 1;
299 sde->colour_primaries = avctx->color_primaries;
300 sde->transfer_characteristics = avctx->color_trc;
301 sde->matrix_coefficients = avctx->colorspace;
303 sde->colour_description = 0;
306 sde->display_horizontal_size = avctx->width;
307 sde->display_vertical_size = avctx->height;
312 goph->group_start_code = MPEG2_START_GROUP;
315 goph->closed_gop = 1;
316 goph->broken_link = 0;
319 // Defaults for picture header
321 ph->picture_start_code = MPEG2_START_PICTURE;
323 ph->vbv_delay = 0xffff; // Not currently calculated.
325 ph->full_pel_forward_vector = 0;
326 ph->forward_f_code = 7;
327 ph->full_pel_backward_vector = 0;
328 ph->forward_f_code = 7;
331 // Defaults for picture coding extension
333 priv->picture_coding_extension.extension_start_code =
334 MPEG2_START_EXTENSION;
335 priv->picture_coding_extension.extension_start_code_identifier =
336 MPEG2_EXTENSION_PICTURE_CODING;
338 pce->intra_dc_precision = 0;
339 pce->picture_structure = 3;
340 pce->top_field_first = 0;
341 pce->frame_pred_frame_dct = 1;
342 pce->concealment_motion_vectors = 0;
343 pce->q_scale_type = 0;
344 pce->intra_vlc_format = 0;
345 pce->alternate_scan = 0;
346 pce->repeat_first_field = 0;
347 pce->progressive_frame = 1;
348 pce->composite_display_flag = 0;
352 *vseq = (VAEncSequenceParameterBufferMPEG2) {
353 .intra_period = avctx->gop_size,
354 .ip_period = ctx->b_per_p + 1,
356 .picture_width = avctx->width,
357 .picture_height = avctx->height,
359 .bits_per_second = avctx->bit_rate,
360 .frame_rate = av_q2d(priv->frame_rate),
361 .aspect_ratio_information = sh->aspect_ratio_information,
362 .vbv_buffer_size = priv->vbv_buffer_size,
364 .sequence_extension.bits = {
365 .profile_and_level_indication = se->profile_and_level_indication,
366 .progressive_sequence = se->progressive_sequence,
367 .chroma_format = se->chroma_format,
368 .low_delay = se->low_delay,
369 .frame_rate_extension_n = se->frame_rate_extension_n,
370 .frame_rate_extension_d = se->frame_rate_extension_d,
375 .time_code = goph->time_code,
376 .closed_gop = goph->closed_gop,
377 .broken_link = goph->broken_link,
381 *vpic = (VAEncPictureParameterBufferMPEG2) {
382 .forward_reference_picture = VA_INVALID_ID,
383 .backward_reference_picture = VA_INVALID_ID,
384 .reconstructed_picture = VA_INVALID_ID,
385 .coded_buf = VA_INVALID_ID,
388 .f_code = { { 15, 15 }, { 15, 15 } },
390 .picture_coding_extension.bits = {
391 .intra_dc_precision = pce->intra_dc_precision,
392 .picture_structure = pce->picture_structure,
393 .top_field_first = pce->top_field_first,
394 .frame_pred_frame_dct = pce->frame_pred_frame_dct,
395 .concealment_motion_vectors = pce->concealment_motion_vectors,
396 .q_scale_type = pce->q_scale_type,
397 .intra_vlc_format = pce->intra_vlc_format,
398 .alternate_scan = pce->alternate_scan,
399 .repeat_first_field = pce->repeat_first_field,
400 .progressive_frame = pce->progressive_frame,
401 .composite_display_flag = pce->composite_display_flag,
404 .composite_display.bits = {
405 .v_axis = pce->v_axis,
406 .field_sequence = pce->field_sequence,
407 .sub_carrier = pce->sub_carrier,
408 .burst_amplitude = pce->burst_amplitude,
409 .sub_carrier_phase = pce->sub_carrier_phase,
416 static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,
417 VAAPIEncodePicture *pic)
419 VAAPIEncodeContext *ctx = avctx->priv_data;
420 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
421 MPEG2RawPictureHeader *ph = &priv->picture_header;
422 MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
423 VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params;
425 if (pic->type == PICTURE_TYPE_IDR || pic->type == PICTURE_TYPE_I) {
426 ph->temporal_reference = 0;
427 ph->picture_coding_type = 1;
428 priv->last_i_frame = pic->display_order;
430 ph->temporal_reference = pic->display_order - priv->last_i_frame;
431 ph->picture_coding_type = pic->type == PICTURE_TYPE_B ? 3 : 2;
434 if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) {
435 pce->f_code[0][0] = priv->f_code_horizontal;
436 pce->f_code[0][1] = priv->f_code_vertical;
438 pce->f_code[0][0] = 15;
439 pce->f_code[0][1] = 15;
441 if (pic->type == PICTURE_TYPE_B) {
442 pce->f_code[1][0] = priv->f_code_horizontal;
443 pce->f_code[1][1] = priv->f_code_vertical;
445 pce->f_code[1][0] = 15;
446 pce->f_code[1][1] = 15;
449 vpic->reconstructed_picture = pic->recon_surface;
450 vpic->coded_buf = pic->output_buffer;
453 case PICTURE_TYPE_IDR:
455 vpic->picture_type = VAEncPictureTypeIntra;
458 vpic->picture_type = VAEncPictureTypePredictive;
459 vpic->forward_reference_picture = pic->refs[0]->recon_surface;
462 vpic->picture_type = VAEncPictureTypeBidirectional;
463 vpic->forward_reference_picture = pic->refs[0]->recon_surface;
464 vpic->backward_reference_picture = pic->refs[1]->recon_surface;
467 av_assert0(0 && "invalid picture type");
470 vpic->temporal_reference = ph->temporal_reference;
471 vpic->f_code[0][0] = pce->f_code[0][0];
472 vpic->f_code[0][1] = pce->f_code[0][1];
473 vpic->f_code[1][0] = pce->f_code[1][0];
474 vpic->f_code[1][1] = pce->f_code[1][1];
476 pic->nb_slices = priv->mb_height;
481 static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx,
482 VAAPIEncodePicture *pic,
483 VAAPIEncodeSlice *slice)
485 VAAPIEncodeContext *ctx = avctx->priv_data;
486 VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
487 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
490 vslice->macroblock_address = priv->mb_width * slice->index;
491 vslice->num_macroblocks = priv->mb_width;
494 case PICTURE_TYPE_IDR:
505 av_assert0(0 && "invalid picture type");
508 vslice->quantiser_scale_code = qp;
509 vslice->is_intra_slice = (pic->type == PICTURE_TYPE_IDR ||
510 pic->type == PICTURE_TYPE_I);
515 static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
517 VAAPIEncodeContext *ctx = avctx->priv_data;
518 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
521 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
525 priv->mb_width = FFALIGN(avctx->width, 16) / 16;
526 priv->mb_height = FFALIGN(avctx->height, 16) / 16;
528 if (ctx->va_rc_mode == VA_RC_CQP) {
529 priv->quant_p = av_clip(avctx->global_quality, 1, 31);
530 if (avctx->i_quant_factor > 0.0)
531 priv->quant_i = av_clip((avctx->global_quality *
532 avctx->i_quant_factor +
533 avctx->i_quant_offset) + 0.5,
536 priv->quant_i = priv->quant_p;
537 if (avctx->b_quant_factor > 0.0)
538 priv->quant_b = av_clip((avctx->global_quality *
539 avctx->b_quant_factor +
540 avctx->b_quant_offset) + 0.5,
543 priv->quant_b = priv->quant_p;
545 av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
546 "%d / %d / %d for I- / P- / B-frames.\n",
547 priv->quant_i, priv->quant_p, priv->quant_b);
550 av_assert0(0 && "Invalid RC mode.");
556 static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
557 .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
559 .configure = &vaapi_encode_mpeg2_configure,
561 .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
562 .init_sequence_params = &vaapi_encode_mpeg2_init_sequence_params,
564 .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
565 .init_picture_params = &vaapi_encode_mpeg2_init_picture_params,
567 .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
568 .init_slice_params = &vaapi_encode_mpeg2_init_slice_params,
570 .sequence_header_type = VAEncPackedHeaderSequence,
571 .write_sequence_header = &vaapi_encode_mpeg2_write_sequence_header,
573 .picture_header_type = VAEncPackedHeaderPicture,
574 .write_picture_header = &vaapi_encode_mpeg2_write_picture_header,
577 static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
579 VAAPIEncodeContext *ctx = avctx->priv_data;
581 ctx->codec = &vaapi_encode_type_mpeg2;
583 switch (avctx->profile) {
584 case FF_PROFILE_MPEG2_SIMPLE:
585 ctx->va_profile = VAProfileMPEG2Simple;
587 case FF_PROFILE_MPEG2_MAIN:
588 ctx->va_profile = VAProfileMPEG2Main;
590 case FF_PROFILE_MPEG2_422:
591 av_log(avctx, AV_LOG_ERROR, "MPEG-2 4:2:2 profile "
592 "is not supported.\n");
593 return AVERROR_PATCHWELCOME;
594 case FF_PROFILE_MPEG2_HIGH:
595 av_log(avctx, AV_LOG_ERROR, "MPEG-2 high profile "
596 "is not supported.\n");
597 return AVERROR_PATCHWELCOME;
598 case FF_PROFILE_MPEG2_SS:
599 case FF_PROFILE_MPEG2_SNR_SCALABLE:
600 av_log(avctx, AV_LOG_ERROR, "MPEG-2 scalable profiles "
601 "are not supported.\n");
602 return AVERROR_PATCHWELCOME;
604 av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 profile %d.\n",
606 return AVERROR(EINVAL);
608 switch (avctx->level) {
615 av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 level %d.\n",
617 return AVERROR(EINVAL);
620 if (avctx->height % 4096 == 0 || avctx->width % 4096 == 0) {
621 av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support picture "
622 "height or width divisible by 4096.\n");
623 return AVERROR(EINVAL);
626 ctx->va_entrypoint = VAEntrypointEncSlice;
627 ctx->va_rt_format = VA_RT_FORMAT_YUV420;
628 ctx->va_rc_mode = VA_RC_CQP;
630 ctx->va_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE |
631 VA_ENC_PACKED_HEADER_PICTURE;
633 ctx->surface_width = FFALIGN(avctx->width, 16);
634 ctx->surface_height = FFALIGN(avctx->height, 16);
636 return ff_vaapi_encode_init(avctx);
639 static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
641 VAAPIEncodeContext *ctx = avctx->priv_data;
642 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
645 ff_cbs_close(&priv->cbc);
647 return ff_vaapi_encode_close(avctx);
650 static const AVCodecDefault vaapi_encode_mpeg2_defaults[] = {
655 { "i_qfactor", "1" },
656 { "i_qoffset", "0" },
657 { "b_qfactor", "6/5" },
658 { "b_qoffset", "0" },
659 { "global_quality", "10" },
663 AVCodec ff_mpeg2_vaapi_encoder = {
664 .name = "mpeg2_vaapi",
665 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 (VAAPI)"),
666 .type = AVMEDIA_TYPE_VIDEO,
667 .id = AV_CODEC_ID_MPEG2VIDEO,
668 .priv_data_size = sizeof(VAAPIEncodeContext),
669 .init = &vaapi_encode_mpeg2_init,
670 .encode2 = &ff_vaapi_encode2,
671 .close = &vaapi_encode_mpeg2_close,
672 .capabilities = AV_CODEC_CAP_DELAY,
673 .defaults = vaapi_encode_mpeg2_defaults,
674 .pix_fmts = (const enum AVPixelFormat[]) {