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"
23 #include "libavutil/common.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixfmt.h"
30 #include "mpegvideo.h"
32 #include "vaapi_encode.h"
34 typedef struct VAAPIEncodeMPEG2Context {
44 unsigned int bit_rate;
45 unsigned int vbv_buffer_size;
46 } VAAPIEncodeMPEG2Context;
49 #define vseq_var(name) vseq->name, name
50 #define vseqext_field(name) vseq->sequence_extension.bits.name, name
51 #define vgop_field(name) vseq->gop_header.bits.name, name
52 #define vpic_var(name) vpic->name, name
53 #define vpcext_field(name) vpic->picture_coding_extension.bits.name, name
54 #define vcomp_field(name) vpic->composite_display.bits.name, name
56 #define u2(width, value, name) put_bits(&pbc, width, value)
57 #define u(width, ...) u2(width, __VA_ARGS__)
59 static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx,
60 char *data, size_t *data_len)
62 VAAPIEncodeContext *ctx = avctx->priv_data;
63 VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params;
64 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
67 init_put_bits(&pbc, data, 8 * *data_len);
69 u(32, SEQ_START_CODE, sequence_header_code);
71 u(12, vseq->picture_width, horizontal_size_value);
72 u(12, vseq->picture_height, vertical_size_value);
73 u(4, vseq_var(aspect_ratio_information));
74 u(4, 8, frame_rate_code);
75 u(18, priv->bit_rate & 0x3fff, bit_rate_value);
77 u(10, priv->vbv_buffer_size & 0x3ff, vbv_buffer_size_value);
78 u(1, 0, constrained_parameters_flag);
79 u(1, 0, load_intra_quantiser_matrix);
80 // intra_quantiser_matrix[64]
81 u(1, 0, load_non_intra_quantiser_matrix);
82 // non_intra_quantiser_matrix[64]
84 while (put_bits_count(&pbc) % 8)
87 u(32, EXT_START_CODE, extension_start_code);
88 u(4, 1, extension_start_code_identifier);
89 u(8, vseqext_field(profile_and_level_indication));
90 u(1, vseqext_field(progressive_sequence));
91 u(2, vseqext_field(chroma_format));
92 u(2, 0, horizontal_size_extension);
93 u(2, 0, vertical_size_extension);
94 u(12, priv->bit_rate >> 18, bit_rate_extension);
96 u(8, priv->vbv_buffer_size >> 10, vbv_buffer_size_extension);
97 u(1, vseqext_field(low_delay));
98 u(2, vseqext_field(frame_rate_extension_n));
99 u(2, vseqext_field(frame_rate_extension_d));
101 while (put_bits_count(&pbc) % 8)
104 u(32, GOP_START_CODE, group_start_code);
105 u(25, vgop_field(time_code));
106 u(1, vgop_field(closed_gop));
107 u(1, vgop_field(broken_link));
109 while (put_bits_count(&pbc) % 8)
112 *data_len = put_bits_count(&pbc);
113 flush_put_bits(&pbc);
118 static int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx,
119 VAAPIEncodePicture *pic,
120 char *data, size_t *data_len)
122 VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params;
123 int picture_coding_type;
126 init_put_bits(&pbc, data, 8 * *data_len);
128 u(32, PICTURE_START_CODE, picture_start_code);
129 u(10, vpic_var(temporal_reference));
131 switch (vpic->picture_type) {
132 case VAEncPictureTypeIntra:
133 picture_coding_type = AV_PICTURE_TYPE_I;
135 case VAEncPictureTypePredictive:
136 picture_coding_type = AV_PICTURE_TYPE_P;
138 case VAEncPictureTypeBidirectional:
139 picture_coding_type = AV_PICTURE_TYPE_B;
142 av_assert0(0 && "invalid picture_coding_type");
144 u(3, picture_coding_type, picture_coding_type);
145 u(16, 0xffff, vbv_delay);
146 if (picture_coding_type == 2 || picture_coding_type == 3) {
147 u(1, 0, full_pel_forward_vector);
148 u(3, 7, forward_f_code);
150 if (picture_coding_type == 3) {
151 u(1, 0, full_pel_backward_vector);
152 u(3, 7, backward_f_code);
154 u(1, 0, extra_bit_picture);
156 while (put_bits_count(&pbc) % 8)
159 u(32, EXT_START_CODE, extension_start_code);
160 u(4, 8, extension_start_code_identifier);
161 u(4, vpic_var(f_code[0][0]));
162 u(4, vpic_var(f_code[0][1]));
163 u(4, vpic_var(f_code[1][0]));
164 u(4, vpic_var(f_code[1][1]));
165 u(2, vpcext_field(intra_dc_precision));
166 u(2, vpcext_field(picture_structure));
167 u(1, vpcext_field(top_field_first));
168 u(1, vpcext_field(frame_pred_frame_dct));
169 u(1, vpcext_field(concealment_motion_vectors));
170 u(1, vpcext_field(q_scale_type));
171 u(1, vpcext_field(intra_vlc_format));
172 u(1, vpcext_field(alternate_scan));
173 u(1, vpcext_field(repeat_first_field));
174 u(1, 1, chroma_420_type);
175 u(1, vpcext_field(progressive_frame));
176 u(1, vpcext_field(composite_display_flag));
177 if (vpic->picture_coding_extension.bits.composite_display_flag) {
178 u(1, vcomp_field(v_axis));
179 u(3, vcomp_field(field_sequence));
180 u(1, vcomp_field(sub_carrier));
181 u(7, vcomp_field(burst_amplitude));
182 u(8, vcomp_field(sub_carrier_phase));
185 while (put_bits_count(&pbc) % 8)
188 *data_len = put_bits_count(&pbc);
189 flush_put_bits(&pbc);
194 static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)
196 VAAPIEncodeContext *ctx = avctx->priv_data;
197 VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params;
198 VAEncPictureParameterBufferMPEG2 *vpic = ctx->codec_picture_params;
199 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
201 vseq->intra_period = avctx->gop_size;
202 vseq->ip_period = ctx->b_per_p + 1;
204 vseq->picture_width = avctx->width;
205 vseq->picture_height = avctx->height;
207 vseq->bits_per_second = avctx->bit_rate;
208 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
209 vseq->frame_rate = (float)avctx->framerate.num / avctx->framerate.den;
211 vseq->frame_rate = (float)avctx->time_base.num / avctx->time_base.den;
213 vseq->aspect_ratio_information = 1;
214 vseq->vbv_buffer_size = avctx->rc_buffer_size / (16 * 1024);
216 vseq->sequence_extension.bits.profile_and_level_indication =
217 avctx->profile << 4 | avctx->level;
218 vseq->sequence_extension.bits.progressive_sequence = 1;
219 vseq->sequence_extension.bits.chroma_format = 1;
220 vseq->sequence_extension.bits.low_delay = 0;
221 vseq->sequence_extension.bits.frame_rate_extension_n = 0;
222 vseq->sequence_extension.bits.frame_rate_extension_d = 0;
224 vseq->new_gop_header = 0;
225 vseq->gop_header.bits.time_code = 0;
226 vseq->gop_header.bits.closed_gop = 1;
227 vseq->gop_header.bits.broken_link = 0;
229 vpic->forward_reference_picture = VA_INVALID_ID;
230 vpic->backward_reference_picture = VA_INVALID_ID;
231 vpic->reconstructed_picture = VA_INVALID_ID;
233 vpic->coded_buf = VA_INVALID_ID;
235 vpic->temporal_reference = 0;
236 vpic->f_code[0][0] = 15;
237 vpic->f_code[0][1] = 15;
238 vpic->f_code[1][0] = 15;
239 vpic->f_code[1][1] = 15;
241 vpic->picture_coding_extension.bits.intra_dc_precision = 0;
242 vpic->picture_coding_extension.bits.picture_structure = 3;
243 vpic->picture_coding_extension.bits.top_field_first = 0;
244 vpic->picture_coding_extension.bits.frame_pred_frame_dct = 1;
245 vpic->picture_coding_extension.bits.concealment_motion_vectors = 0;
246 vpic->picture_coding_extension.bits.q_scale_type = 0;
247 vpic->picture_coding_extension.bits.intra_vlc_format = 0;
248 vpic->picture_coding_extension.bits.alternate_scan = 0;
249 vpic->picture_coding_extension.bits.repeat_first_field = 0;
250 vpic->picture_coding_extension.bits.progressive_frame = 1;
251 vpic->picture_coding_extension.bits.composite_display_flag = 0;
253 priv->bit_rate = (avctx->bit_rate + 399) / 400;
254 priv->vbv_buffer_size = avctx->rc_buffer_size / (16 * 1024);
259 static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,
260 VAAPIEncodePicture *pic)
262 VAAPIEncodeContext *ctx = avctx->priv_data;
263 VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params;
264 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
267 switch (avctx->level) {
269 case 6: // High 1440.
285 case PICTURE_TYPE_IDR:
287 vpic->picture_type = VAEncPictureTypeIntra;
288 priv->last_i_frame = pic->display_order;
291 vpic->picture_type = VAEncPictureTypePredictive;
292 vpic->forward_reference_picture = pic->refs[0]->recon_surface;
293 vpic->f_code[0][0] = fch;
294 vpic->f_code[0][1] = fcv;
297 vpic->picture_type = VAEncPictureTypeBidirectional;
298 vpic->forward_reference_picture = pic->refs[0]->recon_surface;
299 vpic->backward_reference_picture = pic->refs[1]->recon_surface;
300 vpic->f_code[0][0] = fch;
301 vpic->f_code[0][1] = fcv;
302 vpic->f_code[1][0] = fch;
303 vpic->f_code[1][1] = fcv;
306 av_assert0(0 && "invalid picture type");
309 vpic->reconstructed_picture = pic->recon_surface;
310 vpic->coded_buf = pic->output_buffer;
312 vpic->temporal_reference = pic->display_order - priv->last_i_frame;
314 pic->nb_slices = priv->mb_height;
319 static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx,
320 VAAPIEncodePicture *pic,
321 VAAPIEncodeSlice *slice)
323 VAAPIEncodeContext *ctx = avctx->priv_data;
324 VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
325 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
328 vslice->macroblock_address = priv->mb_width * slice->index;
329 vslice->num_macroblocks = priv->mb_width;
332 case PICTURE_TYPE_IDR:
343 av_assert0(0 && "invalid picture type");
346 vslice->quantiser_scale_code = qp;
347 vslice->is_intra_slice = (pic->type == PICTURE_TYPE_IDR ||
348 pic->type == PICTURE_TYPE_I);
353 static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
355 VAAPIEncodeContext *ctx = avctx->priv_data;
356 VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
358 priv->mb_width = FFALIGN(avctx->width, 16) / 16;
359 priv->mb_height = FFALIGN(avctx->height, 16) / 16;
361 if (ctx->va_rc_mode == VA_RC_CQP) {
362 priv->quant_p = av_clip(avctx->global_quality, 1, 31);
363 if (avctx->i_quant_factor > 0.0)
364 priv->quant_i = av_clip((avctx->global_quality *
365 avctx->i_quant_factor +
366 avctx->i_quant_offset) + 0.5,
369 priv->quant_i = priv->quant_p;
370 if (avctx->b_quant_factor > 0.0)
371 priv->quant_b = av_clip((avctx->global_quality *
372 avctx->b_quant_factor +
373 avctx->b_quant_offset) + 0.5,
376 priv->quant_b = priv->quant_p;
378 av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
379 "%d / %d / %d for I- / P- / B-frames.\n",
380 priv->quant_i, priv->quant_p, priv->quant_b);
383 av_assert0(0 && "Invalid RC mode.");
389 static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
390 .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
392 .configure = &vaapi_encode_mpeg2_configure,
394 .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
395 .init_sequence_params = &vaapi_encode_mpeg2_init_sequence_params,
397 .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
398 .init_picture_params = &vaapi_encode_mpeg2_init_picture_params,
400 .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
401 .init_slice_params = &vaapi_encode_mpeg2_init_slice_params,
403 .sequence_header_type = VAEncPackedHeaderSequence,
404 .write_sequence_header = &vaapi_encode_mpeg2_write_sequence_header,
406 .picture_header_type = VAEncPackedHeaderPicture,
407 .write_picture_header = &vaapi_encode_mpeg2_write_picture_header,
410 static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
412 VAAPIEncodeContext *ctx = avctx->priv_data;
414 ctx->codec = &vaapi_encode_type_mpeg2;
416 switch (avctx->profile) {
417 case FF_PROFILE_MPEG2_SIMPLE:
418 ctx->va_profile = VAProfileMPEG2Simple;
420 case FF_PROFILE_MPEG2_MAIN:
421 ctx->va_profile = VAProfileMPEG2Main;
424 av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 profile %d.\n",
426 return AVERROR(EINVAL);
429 ctx->va_entrypoint = VAEntrypointEncSlice;
430 ctx->va_rt_format = VA_RT_FORMAT_YUV420;
431 ctx->va_rc_mode = VA_RC_CQP;
433 ctx->va_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE |
434 VA_ENC_PACKED_HEADER_PICTURE;
436 ctx->surface_width = FFALIGN(avctx->width, 16);
437 ctx->surface_height = FFALIGN(avctx->height, 16);
439 return ff_vaapi_encode_init(avctx);
442 static const AVCodecDefault vaapi_encode_mpeg2_defaults[] = {
447 { "i_qfactor", "1" },
448 { "i_qoffset", "0" },
449 { "b_qfactor", "6/5" },
450 { "b_qoffset", "0" },
451 { "global_quality", "10" },
455 AVCodec ff_mpeg2_vaapi_encoder = {
456 .name = "mpeg2_vaapi",
457 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 (VAAPI)"),
458 .type = AVMEDIA_TYPE_VIDEO,
459 .id = AV_CODEC_ID_MPEG2VIDEO,
460 .priv_data_size = sizeof(VAAPIEncodeContext),
461 .init = &vaapi_encode_mpeg2_init,
462 .encode2 = &ff_vaapi_encode2,
463 .close = &ff_vaapi_encode_close,
464 .capabilities = AV_CODEC_CAP_DELAY,
465 .defaults = vaapi_encode_mpeg2_defaults,
466 .pix_fmts = (const enum AVPixelFormat[]) {