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