FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vaapi_encode_mpeg2.c
Date: 2026-09-28 04:46:15
Exec Total Coverage
Lines: 0 323 0.0%
Functions: 0 10 0.0%
Branches: 0 89 0.0%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include <va/va.h>
20 #include <va/va_enc_mpeg2.h>
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/opt.h"
24
25 #include "avcodec.h"
26 #include "cbs.h"
27 #include "cbs_mpeg2.h"
28 #include "codec_internal.h"
29 #include "mpeg12.h"
30 #include "vaapi_encode.h"
31
32 typedef struct VAAPIEncodeMPEG2Context {
33 VAAPIEncodeContext common;
34
35 // User options.
36 int profile;
37 int level;
38
39 // Derived settings.
40 int quant_i;
41 int quant_p;
42 int quant_b;
43
44 unsigned int bit_rate;
45 unsigned int vbv_buffer_size;
46
47 AVRational frame_rate;
48
49 unsigned int f_code_horizontal;
50 unsigned int f_code_vertical;
51
52 // Stream state.
53 int64_t last_i_frame;
54
55 // Writer structures.
56 MPEG2RawSequenceHeader sequence_header;
57 MPEG2RawExtensionData sequence_extension;
58 MPEG2RawExtensionData sequence_display_extension;
59 MPEG2RawGroupOfPicturesHeader gop_header;
60 MPEG2RawPictureHeader picture_header;
61 MPEG2RawExtensionData picture_coding_extension;
62
63 CodedBitstreamContext *cbc;
64 CodedBitstreamFragment current_fragment;
65 } VAAPIEncodeMPEG2Context;
66
67
68 ✗ static int vaapi_encode_mpeg2_write_fragment(AVCodecContext *avctx,
69 char *data, size_t *data_len,
70 CodedBitstreamFragment *frag)
71 {
72 ✗ VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
73 int err;
74
75 ✗ err = ff_cbs_write_fragment_data(priv->cbc, frag);
76 ✗ if (err < 0) {
77 ✗ av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
78 ✗ return err;
79 }
80
81 ✗ if (*data_len < 8 * frag->data_size - frag->data_bit_padding) {
82 ✗ av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
83 "%zu < %zu.\n", *data_len,
84 ✗ 8 * frag->data_size - frag->data_bit_padding);
85 ✗ return AVERROR(ENOSPC);
86 }
87
88 ✗ memcpy(data, frag->data, frag->data_size);
89 ✗ *data_len = 8 * frag->data_size - frag->data_bit_padding;
90
91 ✗ return 0;
92 }
93
94 ✗ static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx,
95 CodedBitstreamFragment *frag,
96 int type, void *header)
97 {
98 int err;
99
100 ✗ err = ff_cbs_insert_unit_content(frag, -1, type, header, NULL);
101 ✗ if (err < 0) {
102 ✗ av_log(avctx, AV_LOG_ERROR, "Failed to add header: "
103 "type = %d.\n", type);
104 ✗ return err;
105 }
106
107 ✗ return 0;
108 }
109
110 ✗ static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx,
111 char *data, size_t *data_len)
112 {
113 ✗ VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
114 ✗ CodedBitstreamFragment *frag = &priv->current_fragment;
115 int err;
116
117 ✗ err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_SEQUENCE_HEADER,
118 ✗ &priv->sequence_header);
119 ✗ if (err < 0)
120 ✗ goto fail;
121
122 ✗ err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
123 ✗ &priv->sequence_extension);
124 ✗ if (err < 0)
125 ✗ goto fail;
126
127 ✗ err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
128 ✗ &priv->sequence_display_extension);
129 ✗ if (err < 0)
130 ✗ goto fail;
131
132 ✗ err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_GROUP,
133 ✗ &priv->gop_header);
134 ✗ if (err < 0)
135 ✗ goto fail;
136
137 ✗ err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
138 ✗ fail:
139 ✗ ff_cbs_fragment_reset(frag);
140 ✗ return 0;
141 }
142
143 ✗ static int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx,
144 FFHWBaseEncodePicture *pic,
145 char *data, size_t *data_len)
146 {
147 ✗ VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
148 ✗ CodedBitstreamFragment *frag = &priv->current_fragment;
149 int err;
150
151 ✗ err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_PICTURE,
152 ✗ &priv->picture_header);
153 ✗ if (err < 0)
154 ✗ goto fail;
155
156 ✗ err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
157 ✗ &priv->picture_coding_extension);
158 ✗ if (err < 0)
159 ✗ goto fail;
160
161 ✗ err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
162 ✗ fail:
163 ✗ ff_cbs_fragment_reset(frag);
164 ✗ return 0;
165 }
166
167 ✗ static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)
168 {
169 ✗ FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
170 ✗ VAAPIEncodeContext *ctx = avctx->priv_data;
171 ✗ VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
172 ✗ MPEG2RawSequenceHeader *sh = &priv->sequence_header;
173 ✗ MPEG2RawSequenceExtension *se = &priv->sequence_extension.data.sequence;
174 ✗ MPEG2RawSequenceDisplayExtension *sde = &priv->sequence_display_extension.data.sequence_display;
175 ✗ MPEG2RawGroupOfPicturesHeader *goph = &priv->gop_header;
176 ✗ MPEG2RawPictureHeader *ph = &priv->picture_header;
177 ✗ MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
178 ✗ VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params;
179 ✗ VAEncPictureParameterBufferMPEG2 *vpic = ctx->codec_picture_params;
180 int code, ext_n, ext_d;
181
182 ✗ memset(sh, 0, sizeof(*sh));
183 ✗ memset(se, 0, sizeof(*se));
184 ✗ memset(sde, 0, sizeof(*sde));
185 ✗ memset(goph, 0, sizeof(*goph));
186 ✗ memset(ph, 0, sizeof(*ph));
187 ✗ memset(pce, 0, sizeof(*pce));
188
189
190 ✗ if (ctx->va_bit_rate > 0) {
191 ✗ priv->bit_rate = (ctx->va_bit_rate + 399) / 400;
192 } else {
193 // Unknown (not a bitrate-targetting mode), so just use the
194 // highest value.
195 ✗ priv->bit_rate = 0x3fffffff;
196 }
197 ✗ if (avctx->rc_buffer_size > 0) {
198 ✗ priv->vbv_buffer_size = (avctx->rc_buffer_size + (1 << 14) - 1) >> 14;
199 } else {
200 // Unknown, so guess a value from the bitrate.
201 ✗ priv->vbv_buffer_size = priv->bit_rate >> 14;
202 }
203
204 ✗ switch (avctx->level) {
205 ✗ case 4: // High.
206 case 6: // High 1440.
207 ✗ priv->f_code_horizontal = 9;
208 ✗ priv->f_code_vertical = 5;
209 ✗ break;
210 ✗ case 8: // Main.
211 ✗ priv->f_code_horizontal = 8;
212 ✗ priv->f_code_vertical = 5;
213 ✗ break;
214 ✗ case 10: // Low.
215 default:
216 ✗ priv->f_code_horizontal = 7;
217 ✗ priv->f_code_vertical = 4;
218 ✗ break;
219 }
220
221
222 // Sequence header
223
224 ✗ sh->sequence_header_code = MPEG2_START_SEQUENCE_HEADER;
225
226 ✗ sh->horizontal_size_value = avctx->width & 0xfff;
227 ✗ sh->vertical_size_value = avctx->height & 0xfff;
228
229 ✗ if (avctx->sample_aspect_ratio.num != 0 &&
230 ✗ avctx->sample_aspect_ratio.den != 0) {
231 ✗ AVRational dar = av_div_q(avctx->sample_aspect_ratio,
232 ✗ (AVRational) { avctx->width, avctx->height });
233
234 ✗ if (av_cmp_q(avctx->sample_aspect_ratio, (AVRational) { 1, 1 }) == 0) {
235 ✗ sh->aspect_ratio_information = 1;
236 ✗ } else if (av_cmp_q(dar, (AVRational) { 3, 4 }) == 0) {
237 ✗ sh->aspect_ratio_information = 2;
238 ✗ } else if (av_cmp_q(dar, (AVRational) { 9, 16 }) == 0) {
239 ✗ sh->aspect_ratio_information = 3;
240 ✗ } else if (av_cmp_q(dar, (AVRational) { 100, 221 }) == 0) {
241 ✗ sh->aspect_ratio_information = 4;
242 } else {
243 ✗ av_log(avctx, AV_LOG_WARNING, "Sample aspect ratio %d:%d is not "
244 "representable, signalling square pixels instead.\n",
245 avctx->sample_aspect_ratio.num,
246 avctx->sample_aspect_ratio.den);
247 ✗ sh->aspect_ratio_information = 1;
248 }
249 } else {
250 // Unknown - assume square pixels.
251 ✗ sh->aspect_ratio_information = 1;
252 }
253
254 ✗ if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
255 ✗ priv->frame_rate = avctx->framerate;
256 else
257 ✗ priv->frame_rate = av_inv_q(avctx->time_base);
258 ✗ ff_mpeg12_find_best_frame_rate(priv->frame_rate,
259 &code, &ext_n, &ext_d, 0);
260 ✗ sh->frame_rate_code = code;
261
262 ✗ sh->bit_rate_value = priv->bit_rate & 0x3ffff;
263 ✗ sh->vbv_buffer_size_value = priv->vbv_buffer_size & 0x3ff;
264
265 ✗ sh->constrained_parameters_flag = 0;
266 ✗ sh->load_intra_quantiser_matrix = 0;
267 ✗ sh->load_non_intra_quantiser_matrix = 0;
268
269
270 // Sequence extension
271
272 ✗ priv->sequence_extension.extension_start_code = MPEG2_START_EXTENSION;
273 ✗ priv->sequence_extension.extension_start_code_identifier =
274 MPEG2_EXTENSION_SEQUENCE;
275
276 ✗ se->profile_and_level_indication = avctx->profile << 4 | avctx->level;
277 ✗ se->progressive_sequence = 1;
278 ✗ se->chroma_format = 1;
279
280 ✗ se->horizontal_size_extension = avctx->width >> 12;
281 ✗ se->vertical_size_extension = avctx->height >> 12;
282
283 ✗ se->bit_rate_extension = priv->bit_rate >> 18;
284 ✗ se->vbv_buffer_size_extension = priv->vbv_buffer_size >> 10;
285 ✗ se->low_delay = base_ctx->b_per_p == 0;
286
287 ✗ se->frame_rate_extension_n = ext_n;
288 ✗ se->frame_rate_extension_d = ext_d;
289
290
291 // Sequence display extension
292
293 ✗ priv->sequence_display_extension.extension_start_code =
294 MPEG2_START_EXTENSION;
295 ✗ priv->sequence_display_extension.extension_start_code_identifier =
296 MPEG2_EXTENSION_SEQUENCE_DISPLAY;
297
298 // Unspecified video format, from table 6-6.
299 ✗ sde->video_format = 5;
300
301 ✗ sde->colour_primaries = avctx->color_primaries;
302 ✗ sde->transfer_characteristics = avctx->color_trc;
303 ✗ sde->matrix_coefficients = avctx->colorspace;
304 ✗ sde->colour_description =
305 ✗ avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
306 ✗ avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
307 ✗ avctx->colorspace != AVCOL_SPC_UNSPECIFIED;
308
309 ✗ sde->display_horizontal_size = avctx->width;
310 ✗ sde->display_vertical_size = avctx->height;
311
312
313 // GOP header
314
315 ✗ goph->group_start_code = MPEG2_START_GROUP;
316
317 // Marker bit in the middle of time_code.
318 ✗ goph->time_code = 1 << 12;
319 ✗ goph->closed_gop = 1;
320 ✗ goph->broken_link = 0;
321
322
323 // Defaults for picture header
324
325 ✗ ph->picture_start_code = MPEG2_START_PICTURE;
326
327 ✗ ph->vbv_delay = 0xffff; // Not currently calculated.
328
329 ✗ ph->full_pel_forward_vector = 0;
330 ✗ ph->forward_f_code = 7;
331 ✗ ph->full_pel_backward_vector = 0;
332 ✗ ph->forward_f_code = 7;
333
334
335 // Defaults for picture coding extension
336
337 ✗ priv->picture_coding_extension.extension_start_code =
338 MPEG2_START_EXTENSION;
339 ✗ priv->picture_coding_extension.extension_start_code_identifier =
340 MPEG2_EXTENSION_PICTURE_CODING;
341
342 ✗ pce->intra_dc_precision = 0;
343 ✗ pce->picture_structure = 3;
344 ✗ pce->top_field_first = 0;
345 ✗ pce->frame_pred_frame_dct = 1;
346 ✗ pce->concealment_motion_vectors = 0;
347 ✗ pce->q_scale_type = 0;
348 ✗ pce->intra_vlc_format = 0;
349 ✗ pce->alternate_scan = 0;
350 ✗ pce->repeat_first_field = 0;
351 ✗ pce->progressive_frame = 1;
352 ✗ pce->composite_display_flag = 0;
353
354
355
356 ✗ *vseq = (VAEncSequenceParameterBufferMPEG2) {
357 ✗ .intra_period = base_ctx->gop_size,
358 ✗ .ip_period = base_ctx->b_per_p + 1,
359
360 ✗ .picture_width = avctx->width,
361 ✗ .picture_height = avctx->height,
362
363 ✗ .bits_per_second = ctx->va_bit_rate,
364 ✗ .frame_rate = av_q2d(priv->frame_rate),
365 ✗ .aspect_ratio_information = sh->aspect_ratio_information,
366 ✗ .vbv_buffer_size = priv->vbv_buffer_size,
367
368 .sequence_extension.bits = {
369 ✗ .profile_and_level_indication = se->profile_and_level_indication,
370 ✗ .progressive_sequence = se->progressive_sequence,
371 ✗ .chroma_format = se->chroma_format,
372 ✗ .low_delay = se->low_delay,
373 ✗ .frame_rate_extension_n = se->frame_rate_extension_n,
374 ✗ .frame_rate_extension_d = se->frame_rate_extension_d,
375 },
376
377 .new_gop_header = 1,
378 .gop_header.bits = {
379 ✗ .time_code = goph->time_code,
380 ✗ .closed_gop = goph->closed_gop,
381 ✗ .broken_link = goph->broken_link,
382 },
383 };
384
385 ✗ *vpic = (VAEncPictureParameterBufferMPEG2) {
386 .forward_reference_picture = VA_INVALID_ID,
387 .backward_reference_picture = VA_INVALID_ID,
388 .reconstructed_picture = VA_INVALID_ID,
389 .coded_buf = VA_INVALID_ID,
390
391 .vbv_delay = 0xffff,
392 .f_code = { { 15, 15 }, { 15, 15 } },
393
394 .picture_coding_extension.bits = {
395 ✗ .intra_dc_precision = pce->intra_dc_precision,
396 ✗ .picture_structure = pce->picture_structure,
397 ✗ .top_field_first = pce->top_field_first,
398 ✗ .frame_pred_frame_dct = pce->frame_pred_frame_dct,
399 ✗ .concealment_motion_vectors = pce->concealment_motion_vectors,
400 ✗ .q_scale_type = pce->q_scale_type,
401 ✗ .intra_vlc_format = pce->intra_vlc_format,
402 ✗ .alternate_scan = pce->alternate_scan,
403 ✗ .repeat_first_field = pce->repeat_first_field,
404 ✗ .progressive_frame = pce->progressive_frame,
405 ✗ .composite_display_flag = pce->composite_display_flag,
406 },
407
408 .composite_display.bits = {
409 ✗ .v_axis = pce->v_axis,
410 ✗ .field_sequence = pce->field_sequence,
411 ✗ .sub_carrier = pce->sub_carrier,
412 ✗ .burst_amplitude = pce->burst_amplitude,
413 ✗ .sub_carrier_phase = pce->sub_carrier_phase,
414 },
415 };
416
417 ✗ return 0;
418 }
419
420 ✗ static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,
421 FFHWBaseEncodePicture *pic)
422 {
423 ✗ VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
424 ✗ VAAPIEncodePicture *vaapi_pic = pic->priv;
425 ✗ MPEG2RawPictureHeader *ph = &priv->picture_header;
426 ✗ MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
427 ✗ VAEncPictureParameterBufferMPEG2 *vpic = vaapi_pic->codec_picture_params;
428
429 ✗ if (pic->type == FF_HW_PICTURE_TYPE_IDR || pic->type == FF_HW_PICTURE_TYPE_I) {
430 ✗ ph->temporal_reference = 0;
431 ✗ ph->picture_coding_type = 1;
432 ✗ priv->last_i_frame = pic->display_order;
433 } else {
434 ✗ ph->temporal_reference = pic->display_order - priv->last_i_frame;
435 ✗ ph->picture_coding_type = pic->type == FF_HW_PICTURE_TYPE_B ? 3 : 2;
436 }
437
438 ✗ if (pic->type == FF_HW_PICTURE_TYPE_P || pic->type == FF_HW_PICTURE_TYPE_B) {
439 ✗ pce->f_code[0][0] = priv->f_code_horizontal;
440 ✗ pce->f_code[0][1] = priv->f_code_vertical;
441 } else {
442 ✗ pce->f_code[0][0] = 15;
443 ✗ pce->f_code[0][1] = 15;
444 }
445 ✗ if (pic->type == FF_HW_PICTURE_TYPE_B) {
446 ✗ pce->f_code[1][0] = priv->f_code_horizontal;
447 ✗ pce->f_code[1][1] = priv->f_code_vertical;
448 } else {
449 ✗ pce->f_code[1][0] = 15;
450 ✗ pce->f_code[1][1] = 15;
451 }
452
453 ✗ vpic->reconstructed_picture = vaapi_pic->recon_surface;
454 ✗ vpic->coded_buf = vaapi_pic->output_buffer;
455
456 ✗ switch (pic->type) {
457 ✗ case FF_HW_PICTURE_TYPE_IDR:
458 case FF_HW_PICTURE_TYPE_I:
459 ✗ vpic->picture_type = VAEncPictureTypeIntra;
460 ✗ break;
461 ✗ case FF_HW_PICTURE_TYPE_P:
462 ✗ vpic->picture_type = VAEncPictureTypePredictive;
463 ✗ vpic->forward_reference_picture = ((VAAPIEncodePicture *)pic->refs[0][0]->priv)->recon_surface;
464 ✗ break;
465 ✗ case FF_HW_PICTURE_TYPE_B:
466 ✗ vpic->picture_type = VAEncPictureTypeBidirectional;
467 ✗ vpic->forward_reference_picture = ((VAAPIEncodePicture *)pic->refs[0][0]->priv)->recon_surface;
468 ✗ vpic->backward_reference_picture = ((VAAPIEncodePicture *)pic->refs[1][0]->priv)->recon_surface;
469 ✗ break;
470 ✗ default:
471 ✗ av_assert0(0 && "invalid picture type");
472 }
473
474 ✗ vpic->temporal_reference = ph->temporal_reference;
475 ✗ vpic->f_code[0][0] = pce->f_code[0][0];
476 ✗ vpic->f_code[0][1] = pce->f_code[0][1];
477 ✗ vpic->f_code[1][0] = pce->f_code[1][0];
478 ✗ vpic->f_code[1][1] = pce->f_code[1][1];
479
480 ✗ return 0;
481 }
482
483 ✗ static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx,
484 FFHWBaseEncodePicture *pic,
485 VAAPIEncodeSlice *slice)
486 {
487 ✗ VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
488 ✗ VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
489 int qp;
490
491 ✗ vslice->macroblock_address = slice->block_start;
492 ✗ vslice->num_macroblocks = slice->block_size;
493
494 ✗ switch (pic->type) {
495 ✗ case FF_HW_PICTURE_TYPE_IDR:
496 case FF_HW_PICTURE_TYPE_I:
497 ✗ qp = priv->quant_i;
498 ✗ break;
499 ✗ case FF_HW_PICTURE_TYPE_P:
500 ✗ qp = priv->quant_p;
501 ✗ break;
502 ✗ case FF_HW_PICTURE_TYPE_B:
503 ✗ qp = priv->quant_b;
504 ✗ break;
505 ✗ default:
506 ✗ av_assert0(0 && "invalid picture type");
507 }
508
509 ✗ vslice->quantiser_scale_code = qp;
510 ✗ vslice->is_intra_slice = (pic->type == FF_HW_PICTURE_TYPE_IDR ||
511 ✗ pic->type == FF_HW_PICTURE_TYPE_I);
512
513 ✗ return 0;
514 }
515
516 ✗ static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
517 {
518 ✗ VAAPIEncodeContext *ctx = avctx->priv_data;
519 ✗ VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
520 int err;
521
522 ✗ err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
523 ✗ if (err < 0)
524 ✗ return err;
525
526 ✗ if (ctx->va_rc_mode == VA_RC_CQP) {
527 ✗ priv->quant_p = av_clip(ctx->rc_quality, 1, 31);
528 ✗ if (avctx->i_quant_factor > 0.0)
529 ✗ priv->quant_i =
530 ✗ av_clip((avctx->i_quant_factor * priv->quant_p +
531 ✗ avctx->i_quant_offset) + 0.5, 1, 31);
532 else
533 ✗ priv->quant_i = priv->quant_p;
534 ✗ if (avctx->b_quant_factor > 0.0)
535 ✗ priv->quant_b =
536 ✗ av_clip((avctx->b_quant_factor * priv->quant_p +
537 ✗ avctx->b_quant_offset) + 0.5, 1, 31);
538 else
539 ✗ priv->quant_b = priv->quant_p;
540
541 ✗ av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
542 "%d / %d / %d for I- / P- / B-frames.\n",
543 priv->quant_i, priv->quant_p, priv->quant_b);
544
545 } else {
546 ✗ priv->quant_i = 16;
547 ✗ priv->quant_p = 16;
548 ✗ priv->quant_b = 16;
549 }
550
551 ✗ ctx->slice_block_rows = FFALIGN(avctx->height, 16) / 16;
552 ✗ ctx->slice_block_cols = FFALIGN(avctx->width, 16) / 16;
553
554 ✗ ctx->nb_slices = ctx->slice_block_rows;
555 ✗ ctx->slice_size = 1;
556
557 ✗ ctx->roi_quant_range = 31;
558
559 ✗ return 0;
560 }
561
562 static const VAAPIEncodeProfile vaapi_encode_mpeg2_profiles[] = {
563 { AV_PROFILE_MPEG2_MAIN, 8, 3, 1, 1, VAProfileMPEG2Main },
564 { AV_PROFILE_MPEG2_SIMPLE, 8, 3, 1, 1, VAProfileMPEG2Simple },
565 { AV_PROFILE_UNKNOWN }
566 };
567
568 static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
569 .profiles = vaapi_encode_mpeg2_profiles,
570
571 .flags = FF_HW_FLAG_B_PICTURES,
572
573 .configure = &vaapi_encode_mpeg2_configure,
574
575 .default_quality = 10,
576
577 .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
578 .init_sequence_params = &vaapi_encode_mpeg2_init_sequence_params,
579
580 .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
581 .init_picture_params = &vaapi_encode_mpeg2_init_picture_params,
582
583 .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
584 .init_slice_params = &vaapi_encode_mpeg2_init_slice_params,
585
586 .sequence_header_type = VAEncPackedHeaderSequence,
587 .write_sequence_header = &vaapi_encode_mpeg2_write_sequence_header,
588
589 .picture_header_type = VAEncPackedHeaderPicture,
590 .write_picture_header = &vaapi_encode_mpeg2_write_picture_header,
591 };
592
593 ✗ static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
594 {
595 ✗ VAAPIEncodeContext *ctx = avctx->priv_data;
596 ✗ VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
597
598 ✗ ctx->codec = &vaapi_encode_type_mpeg2;
599
600 ✗ if (avctx->profile == AV_PROFILE_UNKNOWN)
601 ✗ avctx->profile = priv->profile;
602 ✗ if (avctx->level == AV_LEVEL_UNKNOWN)
603 ✗ avctx->level = priv->level;
604
605 // Reject unknown levels (these are required to set f_code for
606 // motion vector encoding).
607 ✗ switch (avctx->level) {
608 ✗ case 4: // High
609 case 6: // High 1440
610 case 8: // Main
611 case 10: // Low
612 ✗ break;
613 ✗ default:
614 ✗ av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 level %d.\n",
615 avctx->level);
616 ✗ return AVERROR(EINVAL);
617 }
618
619 ✗ if (avctx->height % 4096 == 0 || avctx->width % 4096 == 0) {
620 ✗ av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support picture "
621 "height or width divisible by 4096.\n");
622 ✗ return AVERROR(EINVAL);
623 }
624
625 ✗ ctx->desired_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE |
626 VA_ENC_PACKED_HEADER_PICTURE;
627
628 ✗ return ff_vaapi_encode_init(avctx);
629 }
630
631 ✗ static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
632 {
633 ✗ VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
634
635 ✗ ff_cbs_fragment_free(&priv->current_fragment);
636 ✗ ff_cbs_close(&priv->cbc);
637
638 ✗ return ff_vaapi_encode_close(avctx);
639 }
640
641 #define OFFSET(x) offsetof(VAAPIEncodeMPEG2Context, x)
642 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
643 static const AVOption vaapi_encode_mpeg2_options[] = {
644 HW_BASE_ENCODE_COMMON_OPTIONS,
645 VAAPI_ENCODE_COMMON_OPTIONS,
646 VAAPI_ENCODE_RC_OPTIONS,
647
648 { "profile", "Set profile (in profile_and_level_indication)",
649 OFFSET(profile), AV_OPT_TYPE_INT,
650 { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 7, FLAGS, .unit = "profile" },
651
652 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
653 { .i64 = value }, 0, 0, FLAGS, .unit = "profile"
654 { PROFILE("simple", AV_PROFILE_MPEG2_SIMPLE) },
655 { PROFILE("main", AV_PROFILE_MPEG2_MAIN) },
656 #undef PROFILE
657
658 { "level", "Set level (in profile_and_level_indication)",
659 OFFSET(level), AV_OPT_TYPE_INT,
660 { .i64 = 4 }, 0, 15, FLAGS, .unit = "level" },
661
662 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
663 { .i64 = value }, 0, 0, FLAGS, .unit = "level"
664 { LEVEL("low", 10) },
665 { LEVEL("main", 8) },
666 { LEVEL("high_1440", 6) },
667 { LEVEL("high", 4) },
668 #undef LEVEL
669
670 { NULL },
671 };
672
673 static const FFCodecDefault vaapi_encode_mpeg2_defaults[] = {
674 { "b", "0" },
675 { "bf", "1" },
676 { "g", "120" },
677 { "i_qfactor", "1" },
678 { "i_qoffset", "0" },
679 { "b_qfactor", "6/5" },
680 { "b_qoffset", "0" },
681 { "qmin", "-1" },
682 { "qmax", "-1" },
683 { NULL },
684 };
685
686 static const AVClass vaapi_encode_mpeg2_class = {
687 .class_name = "mpeg2_vaapi",
688 .item_name = av_default_item_name,
689 .option = vaapi_encode_mpeg2_options,
690 .version = LIBAVUTIL_VERSION_INT,
691 };
692
693 const FFCodec ff_mpeg2_vaapi_encoder = {
694 .p.name = "mpeg2_vaapi",
695 CODEC_LONG_NAME("MPEG-2 (VAAPI)"),
696 .p.type = AVMEDIA_TYPE_VIDEO,
697 .p.id = AV_CODEC_ID_MPEG2VIDEO,
698 .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
699 .init = &vaapi_encode_mpeg2_init,
700 FF_CODEC_RECEIVE_PACKET_CB(&ff_vaapi_encode_receive_packet),
701 .close = &vaapi_encode_mpeg2_close,
702 .p.priv_class = &vaapi_encode_mpeg2_class,
703 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
704 AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
705 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
706 FF_CODEC_CAP_INIT_CLEANUP,
707 .defaults = vaapi_encode_mpeg2_defaults,
708 CODEC_PIXFMTS(AV_PIX_FMT_VAAPI),
709 .color_ranges = AVCOL_RANGE_MPEG,
710 .hw_configs = ff_vaapi_encode_hw_configs,
711 .p.wrapper_name = "vaapi",
712 };
713