FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vaapi_encode_mpeg2.c
Date: 2024-07-16 12:46:59
Exec Total Coverage
Lines: 0 324 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 VAAPIEncodePicture *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 VAAPIEncodePicture *vaapi_pic)
422 {
423 VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
424 const FFHWBaseEncodePicture *pic = &vaapi_pic->base;
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])->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])->recon_surface;
468 vpic->backward_reference_picture = ((VAAPIEncodePicture *)pic->refs[1][0])->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 VAAPIEncodePicture *vaapi_pic,
485 VAAPIEncodeSlice *slice)
486 {
487 const FFHWBaseEncodePicture *pic = &vaapi_pic->base;
488 VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
489 VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
490 int qp;
491
492 vslice->macroblock_address = slice->block_start;
493 vslice->num_macroblocks = slice->block_size;
494
495 switch (pic->type) {
496 case FF_HW_PICTURE_TYPE_IDR:
497 case FF_HW_PICTURE_TYPE_I:
498 qp = priv->quant_i;
499 break;
500 case FF_HW_PICTURE_TYPE_P:
501 qp = priv->quant_p;
502 break;
503 case FF_HW_PICTURE_TYPE_B:
504 qp = priv->quant_b;
505 break;
506 default:
507 av_assert0(0 && "invalid picture type");
508 }
509
510 vslice->quantiser_scale_code = qp;
511 vslice->is_intra_slice = (pic->type == FF_HW_PICTURE_TYPE_IDR ||
512 pic->type == FF_HW_PICTURE_TYPE_I);
513
514 return 0;
515 }
516
517 static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
518 {
519 VAAPIEncodeContext *ctx = avctx->priv_data;
520 VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
521 int err;
522
523 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
524 if (err < 0)
525 return err;
526
527 if (ctx->va_rc_mode == VA_RC_CQP) {
528 priv->quant_p = av_clip(ctx->rc_quality, 1, 31);
529 if (avctx->i_quant_factor > 0.0)
530 priv->quant_i =
531 av_clip((avctx->i_quant_factor * priv->quant_p +
532 avctx->i_quant_offset) + 0.5, 1, 31);
533 else
534 priv->quant_i = priv->quant_p;
535 if (avctx->b_quant_factor > 0.0)
536 priv->quant_b =
537 av_clip((avctx->b_quant_factor * priv->quant_p +
538 avctx->b_quant_offset) + 0.5, 1, 31);
539 else
540 priv->quant_b = priv->quant_p;
541
542 av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
543 "%d / %d / %d for I- / P- / B-frames.\n",
544 priv->quant_i, priv->quant_p, priv->quant_b);
545
546 } else {
547 priv->quant_i = 16;
548 priv->quant_p = 16;
549 priv->quant_b = 16;
550 }
551
552 ctx->slice_block_rows = FFALIGN(avctx->height, 16) / 16;
553 ctx->slice_block_cols = FFALIGN(avctx->width, 16) / 16;
554
555 ctx->nb_slices = ctx->slice_block_rows;
556 ctx->slice_size = 1;
557
558 ctx->roi_quant_range = 31;
559
560 return 0;
561 }
562
563 static const VAAPIEncodeProfile vaapi_encode_mpeg2_profiles[] = {
564 { AV_PROFILE_MPEG2_MAIN, 8, 3, 1, 1, VAProfileMPEG2Main },
565 { AV_PROFILE_MPEG2_SIMPLE, 8, 3, 1, 1, VAProfileMPEG2Simple },
566 { AV_PROFILE_UNKNOWN }
567 };
568
569 static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
570 .profiles = vaapi_encode_mpeg2_profiles,
571
572 .flags = FF_HW_FLAG_B_PICTURES,
573
574 .configure = &vaapi_encode_mpeg2_configure,
575
576 .default_quality = 10,
577
578 .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
579 .init_sequence_params = &vaapi_encode_mpeg2_init_sequence_params,
580
581 .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
582 .init_picture_params = &vaapi_encode_mpeg2_init_picture_params,
583
584 .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
585 .init_slice_params = &vaapi_encode_mpeg2_init_slice_params,
586
587 .sequence_header_type = VAEncPackedHeaderSequence,
588 .write_sequence_header = &vaapi_encode_mpeg2_write_sequence_header,
589
590 .picture_header_type = VAEncPackedHeaderPicture,
591 .write_picture_header = &vaapi_encode_mpeg2_write_picture_header,
592 };
593
594 static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
595 {
596 VAAPIEncodeContext *ctx = avctx->priv_data;
597 VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
598
599 ctx->codec = &vaapi_encode_type_mpeg2;
600
601 if (avctx->profile == AV_PROFILE_UNKNOWN)
602 avctx->profile = priv->profile;
603 if (avctx->level == AV_LEVEL_UNKNOWN)
604 avctx->level = priv->level;
605
606 // Reject unknown levels (these are required to set f_code for
607 // motion vector encoding).
608 switch (avctx->level) {
609 case 4: // High
610 case 6: // High 1440
611 case 8: // Main
612 case 10: // Low
613 break;
614 default:
615 av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 level %d.\n",
616 avctx->level);
617 return AVERROR(EINVAL);
618 }
619
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);
624 }
625
626 ctx->desired_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE |
627 VA_ENC_PACKED_HEADER_PICTURE;
628
629 return ff_vaapi_encode_init(avctx);
630 }
631
632 static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
633 {
634 VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
635
636 ff_cbs_fragment_free(&priv->current_fragment);
637 ff_cbs_close(&priv->cbc);
638
639 return ff_vaapi_encode_close(avctx);
640 }
641
642 #define OFFSET(x) offsetof(VAAPIEncodeMPEG2Context, x)
643 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
644 static const AVOption vaapi_encode_mpeg2_options[] = {
645 HW_BASE_ENCODE_COMMON_OPTIONS,
646 VAAPI_ENCODE_COMMON_OPTIONS,
647 VAAPI_ENCODE_RC_OPTIONS,
648
649 { "profile", "Set profile (in profile_and_level_indication)",
650 OFFSET(profile), AV_OPT_TYPE_INT,
651 { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 7, FLAGS, .unit = "profile" },
652
653 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
654 { .i64 = value }, 0, 0, FLAGS, .unit = "profile"
655 { PROFILE("simple", AV_PROFILE_MPEG2_SIMPLE) },
656 { PROFILE("main", AV_PROFILE_MPEG2_MAIN) },
657 #undef PROFILE
658
659 { "level", "Set level (in profile_and_level_indication)",
660 OFFSET(level), AV_OPT_TYPE_INT,
661 { .i64 = 4 }, 0, 15, FLAGS, .unit = "level" },
662
663 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
664 { .i64 = value }, 0, 0, FLAGS, .unit = "level"
665 { LEVEL("low", 10) },
666 { LEVEL("main", 8) },
667 { LEVEL("high_1440", 6) },
668 { LEVEL("high", 4) },
669 #undef LEVEL
670
671 { NULL },
672 };
673
674 static const FFCodecDefault vaapi_encode_mpeg2_defaults[] = {
675 { "b", "0" },
676 { "bf", "1" },
677 { "g", "120" },
678 { "i_qfactor", "1" },
679 { "i_qoffset", "0" },
680 { "b_qfactor", "6/5" },
681 { "b_qoffset", "0" },
682 { "qmin", "-1" },
683 { "qmax", "-1" },
684 { NULL },
685 };
686
687 static const AVClass vaapi_encode_mpeg2_class = {
688 .class_name = "mpeg2_vaapi",
689 .item_name = av_default_item_name,
690 .option = vaapi_encode_mpeg2_options,
691 .version = LIBAVUTIL_VERSION_INT,
692 };
693
694 const FFCodec ff_mpeg2_vaapi_encoder = {
695 .p.name = "mpeg2_vaapi",
696 CODEC_LONG_NAME("MPEG-2 (VAAPI)"),
697 .p.type = AVMEDIA_TYPE_VIDEO,
698 .p.id = AV_CODEC_ID_MPEG2VIDEO,
699 .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
700 .init = &vaapi_encode_mpeg2_init,
701 FF_CODEC_RECEIVE_PACKET_CB(&ff_vaapi_encode_receive_packet),
702 .close = &vaapi_encode_mpeg2_close,
703 .p.priv_class = &vaapi_encode_mpeg2_class,
704 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
705 AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
706 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
707 FF_CODEC_CAP_INIT_CLEANUP,
708 .defaults = vaapi_encode_mpeg2_defaults,
709 .p.pix_fmts = (const enum AVPixelFormat[]) {
710 AV_PIX_FMT_VAAPI,
711 AV_PIX_FMT_NONE,
712 },
713 .hw_configs = ff_vaapi_encode_hw_configs,
714 .p.wrapper_name = "vaapi",
715 };
716