]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_encode_h264.c
vaapi_encode_h264: Reduce SAR to valid range
[ffmpeg] / libavcodec / vaapi_encode_h264.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 <string.h>
20
21 #include <va/va.h>
22 #include <va/va_enc_h264.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
28
29 #include "avcodec.h"
30 #include "cbs.h"
31 #include "cbs_h264.h"
32 #include "h264.h"
33 #include "h264_levels.h"
34 #include "h264_sei.h"
35 #include "internal.h"
36 #include "vaapi_encode.h"
37
38 enum {
39     SEI_TIMING         = 0x01,
40     SEI_IDENTIFIER     = 0x02,
41     SEI_RECOVERY_POINT = 0x04,
42 };
43
44 // Random (version 4) ISO 11578 UUID.
45 static const uint8_t vaapi_encode_h264_sei_identifier_uuid[16] = {
46     0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
47     0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
48 };
49
50 typedef struct VAAPIEncodeH264Context {
51     VAAPIEncodeContext common;
52
53     // User options.
54     int qp;
55     int quality;
56     int coder;
57     int aud;
58     int sei;
59     int profile;
60     int level;
61
62     // Derived settings.
63     int mb_width;
64     int mb_height;
65
66     int fixed_qp_idr;
67     int fixed_qp_p;
68     int fixed_qp_b;
69
70     // Stream state.
71     int frame_num;
72     int pic_order_cnt;
73     int next_frame_num;
74     int64_t last_idr_frame;
75     int64_t idr_pic_count;
76
77     int primary_pic_type;
78     int slice_type;
79
80     int cpb_delay;
81     int dpb_delay;
82
83     // Writer structures.
84     CodedBitstreamContext *cbc;
85     CodedBitstreamFragment current_access_unit;
86
87     H264RawAUD   raw_aud;
88     H264RawSPS   raw_sps;
89     H264RawPPS   raw_pps;
90     H264RawSEI   raw_sei;
91     H264RawSlice raw_slice;
92
93     H264RawSEIBufferingPeriod      sei_buffering_period;
94     H264RawSEIPicTiming            sei_pic_timing;
95     H264RawSEIRecoveryPoint        sei_recovery_point;
96     H264RawSEIUserDataUnregistered sei_identifier;
97     char                          *sei_identifier_string;
98
99     int aud_needed;
100     int sei_needed;
101     int sei_cbr_workaround_needed;
102 } VAAPIEncodeH264Context;
103
104
105 static int vaapi_encode_h264_write_access_unit(AVCodecContext *avctx,
106                                                char *data, size_t *data_len,
107                                                CodedBitstreamFragment *au)
108 {
109     VAAPIEncodeH264Context *priv = avctx->priv_data;
110     int err;
111
112     err = ff_cbs_write_fragment_data(priv->cbc, au);
113     if (err < 0) {
114         av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
115         return err;
116     }
117
118     if (*data_len < 8 * au->data_size - au->data_bit_padding) {
119         av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
120                "%zu < %zu.\n", *data_len,
121                8 * au->data_size - au->data_bit_padding);
122         return AVERROR(ENOSPC);
123     }
124
125     memcpy(data, au->data, au->data_size);
126     *data_len = 8 * au->data_size - au->data_bit_padding;
127
128     return 0;
129 }
130
131 static int vaapi_encode_h264_add_nal(AVCodecContext *avctx,
132                                      CodedBitstreamFragment *au,
133                                      void *nal_unit)
134 {
135     VAAPIEncodeH264Context *priv = avctx->priv_data;
136     H264RawNALUnitHeader *header = nal_unit;
137     int err;
138
139     err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
140                                      header->nal_unit_type, nal_unit, NULL);
141     if (err < 0) {
142         av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
143                "type = %d.\n", header->nal_unit_type);
144         return err;
145     }
146
147     return 0;
148 }
149
150 static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
151                                                    char *data, size_t *data_len)
152 {
153     VAAPIEncodeH264Context *priv = avctx->priv_data;
154     CodedBitstreamFragment   *au = &priv->current_access_unit;
155     int err;
156
157     if (priv->aud_needed) {
158         err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
159         if (err < 0)
160             goto fail;
161         priv->aud_needed = 0;
162     }
163
164     err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_sps);
165     if (err < 0)
166         goto fail;
167
168     err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_pps);
169     if (err < 0)
170         goto fail;
171
172     err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
173 fail:
174     ff_cbs_fragment_uninit(priv->cbc, au);
175     return err;
176 }
177
178 static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
179                                                 VAAPIEncodePicture *pic,
180                                                 VAAPIEncodeSlice *slice,
181                                                 char *data, size_t *data_len)
182 {
183     VAAPIEncodeH264Context *priv = avctx->priv_data;
184     CodedBitstreamFragment   *au = &priv->current_access_unit;
185     int err;
186
187     if (priv->aud_needed) {
188         err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
189         if (err < 0)
190             goto fail;
191         priv->aud_needed = 0;
192     }
193
194     err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_slice);
195     if (err < 0)
196         goto fail;
197
198     err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
199 fail:
200     ff_cbs_fragment_uninit(priv->cbc, au);
201     return err;
202 }
203
204 static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
205                                                 VAAPIEncodePicture *pic,
206                                                 int index, int *type,
207                                                 char *data, size_t *data_len)
208 {
209     VAAPIEncodeH264Context *priv = avctx->priv_data;
210     CodedBitstreamFragment   *au = &priv->current_access_unit;
211     int err, i;
212
213     if (priv->sei_needed) {
214         H264RawSEI *sei = &priv->raw_sei;
215
216         if (priv->aud_needed) {
217             err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
218             if (err < 0)
219                 goto fail;
220             priv->aud_needed = 0;
221         }
222
223         *sei = (H264RawSEI) {
224             .nal_unit_header = {
225                 .nal_unit_type = H264_NAL_SEI,
226             },
227         };
228
229         i = 0;
230
231         if (priv->sei_needed & SEI_IDENTIFIER) {
232             sei->payload[i].payload_type = H264_SEI_TYPE_USER_DATA_UNREGISTERED;
233             sei->payload[i].payload.user_data_unregistered = priv->sei_identifier;
234             ++i;
235         }
236         if (priv->sei_needed & SEI_TIMING) {
237             if (pic->type == PICTURE_TYPE_IDR) {
238                 sei->payload[i].payload_type = H264_SEI_TYPE_BUFFERING_PERIOD;
239                 sei->payload[i].payload.buffering_period = priv->sei_buffering_period;
240                 ++i;
241             }
242             sei->payload[i].payload_type = H264_SEI_TYPE_PIC_TIMING;
243             sei->payload[i].payload.pic_timing = priv->sei_pic_timing;
244             ++i;
245         }
246         if (priv->sei_needed & SEI_RECOVERY_POINT) {
247             sei->payload[i].payload_type = H264_SEI_TYPE_RECOVERY_POINT;
248             sei->payload[i].payload.recovery_point = priv->sei_recovery_point;
249             ++i;
250         }
251
252         sei->payload_count = i;
253         av_assert0(sei->payload_count > 0);
254
255         err = vaapi_encode_h264_add_nal(avctx, au, sei);
256         if (err < 0)
257             goto fail;
258         priv->sei_needed = 0;
259
260         err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
261         if (err < 0)
262             goto fail;
263
264         ff_cbs_fragment_uninit(priv->cbc, au);
265
266         *type = VAEncPackedHeaderRawData;
267         return 0;
268
269 #if !CONFIG_VAAPI_1
270     } else if (priv->sei_cbr_workaround_needed) {
271         // Insert a zero-length header using the old SEI type.  This is
272         // required to avoid triggering broken behaviour on Intel platforms
273         // in CBR mode where an invalid SEI message is generated by the
274         // driver and inserted into the stream.
275         *data_len = 0;
276         *type = VAEncPackedHeaderH264_SEI;
277         priv->sei_cbr_workaround_needed = 0;
278         return 0;
279 #endif
280
281     } else {
282         return AVERROR_EOF;
283     }
284
285 fail:
286     ff_cbs_fragment_uninit(priv->cbc, au);
287     return err;
288 }
289
290 static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
291 {
292     VAAPIEncodeContext                *ctx = avctx->priv_data;
293     VAAPIEncodeH264Context           *priv = avctx->priv_data;
294     H264RawSPS                        *sps = &priv->raw_sps;
295     H264RawPPS                        *pps = &priv->raw_pps;
296     VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
297     VAEncPictureParameterBufferH264  *vpic = ctx->codec_picture_params;
298     int dpb_frames;
299
300     memset(&priv->current_access_unit, 0,
301            sizeof(priv->current_access_unit));
302
303     memset(sps, 0, sizeof(*sps));
304     memset(pps, 0, sizeof(*pps));
305
306     sps->nal_unit_header.nal_ref_idc   = 3;
307     sps->nal_unit_header.nal_unit_type = H264_NAL_SPS;
308
309     sps->profile_idc = avctx->profile & 0xff;
310
311     if (avctx->profile == FF_PROFILE_H264_CONSTRAINED_BASELINE ||
312         avctx->profile == FF_PROFILE_H264_MAIN)
313         sps->constraint_set1_flag = 1;
314
315     if (avctx->profile == FF_PROFILE_H264_HIGH)
316         sps->constraint_set3_flag = ctx->gop_size == 1;
317
318     if (avctx->profile == FF_PROFILE_H264_MAIN ||
319         avctx->profile == FF_PROFILE_H264_HIGH) {
320         sps->constraint_set4_flag = 1;
321         sps->constraint_set5_flag = ctx->b_per_p == 0;
322     }
323
324     if (ctx->gop_size == 1)
325         dpb_frames = 0;
326     else
327         dpb_frames = 1 + (ctx->b_per_p > 0);
328
329     if (avctx->level != FF_LEVEL_UNKNOWN) {
330         sps->level_idc = avctx->level;
331     } else {
332         const H264LevelDescriptor *level;
333
334         level = ff_h264_guess_level(sps->profile_idc,
335                                     avctx->bit_rate,
336                                     priv->mb_width  * 16,
337                                     priv->mb_height * 16,
338                                     dpb_frames);
339         if (level) {
340             av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
341             if (level->constraint_set3_flag)
342                 sps->constraint_set3_flag = 1;
343             sps->level_idc = level->level_idc;
344         } else {
345             av_log(avctx, AV_LOG_WARNING, "Stream will not conform "
346                    "to any level: using level 6.2.\n");
347             sps->level_idc = 62;
348         }
349     }
350
351     sps->seq_parameter_set_id = 0;
352     sps->chroma_format_idc    = 1;
353
354     sps->log2_max_frame_num_minus4 = 4;
355     sps->pic_order_cnt_type        = 0;
356     sps->log2_max_pic_order_cnt_lsb_minus4 =
357         av_clip(av_log2(ctx->b_per_p + 1) - 2, 0, 12);
358
359     sps->max_num_ref_frames = dpb_frames;
360
361     sps->pic_width_in_mbs_minus1        = priv->mb_width  - 1;
362     sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
363
364     sps->frame_mbs_only_flag = 1;
365     sps->direct_8x8_inference_flag = 1;
366
367     if (avctx->width  != 16 * priv->mb_width ||
368         avctx->height != 16 * priv->mb_height) {
369         sps->frame_cropping_flag = 1;
370
371         sps->frame_crop_left_offset   = 0;
372         sps->frame_crop_right_offset  =
373             (16 * priv->mb_width - avctx->width) / 2;
374         sps->frame_crop_top_offset    = 0;
375         sps->frame_crop_bottom_offset =
376             (16 * priv->mb_height - avctx->height) / 2;
377     } else {
378         sps->frame_cropping_flag = 0;
379     }
380
381     sps->vui_parameters_present_flag = 1;
382
383     if (avctx->sample_aspect_ratio.num != 0 &&
384         avctx->sample_aspect_ratio.den != 0) {
385         static const AVRational sar_idc[] = {
386             {   0,  0 },
387             {   1,  1 }, {  12, 11 }, {  10, 11 }, {  16, 11 },
388             {  40, 33 }, {  24, 11 }, {  20, 11 }, {  32, 11 },
389             {  80, 33 }, {  18, 11 }, {  15, 11 }, {  64, 33 },
390             { 160, 99 }, {   4,  3 }, {   3,  2 }, {   2,  1 },
391         };
392         int num, den, i;
393         av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
394                   avctx->sample_aspect_ratio.den, 65535);
395         for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
396             if (num == sar_idc[i].num &&
397                 den == sar_idc[i].den) {
398                 sps->vui.aspect_ratio_idc = i;
399                 break;
400             }
401         }
402         if (i >= FF_ARRAY_ELEMS(sar_idc)) {
403             sps->vui.aspect_ratio_idc = 255;
404             sps->vui.sar_width  = num;
405             sps->vui.sar_height = den;
406         }
407         sps->vui.aspect_ratio_info_present_flag = 1;
408     }
409
410     if (avctx->color_range     != AVCOL_RANGE_UNSPECIFIED ||
411         avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
412         avctx->color_trc       != AVCOL_TRC_UNSPECIFIED ||
413         avctx->colorspace      != AVCOL_SPC_UNSPECIFIED) {
414         sps->vui.video_signal_type_present_flag = 1;
415         sps->vui.video_format      = 5; // Unspecified.
416         sps->vui.video_full_range_flag =
417             avctx->color_range == AVCOL_RANGE_JPEG;
418
419         if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
420             avctx->color_trc       != AVCOL_TRC_UNSPECIFIED ||
421             avctx->colorspace      != AVCOL_SPC_UNSPECIFIED) {
422             sps->vui.colour_description_present_flag = 1;
423             sps->vui.colour_primaries         = avctx->color_primaries;
424             sps->vui.transfer_characteristics = avctx->color_trc;
425             sps->vui.matrix_coefficients      = avctx->colorspace;
426         }
427     } else {
428         sps->vui.video_format             = 5;
429         sps->vui.video_full_range_flag    = 0;
430         sps->vui.colour_primaries         = avctx->color_primaries;
431         sps->vui.transfer_characteristics = avctx->color_trc;
432         sps->vui.matrix_coefficients      = avctx->colorspace;
433     }
434
435     if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
436         sps->vui.chroma_loc_info_present_flag = 1;
437         sps->vui.chroma_sample_loc_type_top_field    =
438         sps->vui.chroma_sample_loc_type_bottom_field =
439             avctx->chroma_sample_location - 1;
440     }
441
442     sps->vui.timing_info_present_flag = 1;
443     if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
444         sps->vui.num_units_in_tick = avctx->framerate.den;
445         sps->vui.time_scale        = 2 * avctx->framerate.num;
446         sps->vui.fixed_frame_rate_flag = 1;
447     } else {
448         sps->vui.num_units_in_tick = avctx->time_base.num;
449         sps->vui.time_scale        = 2 * avctx->time_base.den;
450         sps->vui.fixed_frame_rate_flag = 0;
451     }
452
453     if (priv->sei & SEI_TIMING) {
454         H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
455         H264RawSEIBufferingPeriod *bp = &priv->sei_buffering_period;
456
457         sps->vui.nal_hrd_parameters_present_flag = 1;
458
459         hrd->cpb_cnt_minus1 = 0;
460
461         // Try to scale these to a sensible range so that the
462         // golomb encode of the value is not overlong.
463         hrd->bit_rate_scale =
464             av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4);
465         hrd->bit_rate_value_minus1[0] =
466             (ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1;
467
468         hrd->cpb_size_scale =
469             av_clip_uintp2(av_log2(ctx->hrd_params.hrd.buffer_size) - 15 - 4, 4);
470         hrd->cpb_size_value_minus1[0] =
471             (ctx->hrd_params.hrd.buffer_size >> hrd->cpb_size_scale + 4) - 1;
472
473         // CBR mode as defined for the HRD cannot be achieved without filler
474         // data, so this flag cannot be set even with VAAPI CBR modes.
475         hrd->cbr_flag[0] = 0;
476
477         hrd->initial_cpb_removal_delay_length_minus1 = 23;
478         hrd->cpb_removal_delay_length_minus1         = 23;
479         hrd->dpb_output_delay_length_minus1          = 7;
480         hrd->time_offset_length                      = 0;
481
482         bp->seq_parameter_set_id = sps->seq_parameter_set_id;
483
484         // This calculation can easily overflow 32 bits.
485         bp->nal.initial_cpb_removal_delay[0] = 90000 *
486             (uint64_t)ctx->hrd_params.hrd.initial_buffer_fullness /
487             ctx->hrd_params.hrd.buffer_size;
488         bp->nal.initial_cpb_removal_delay_offset[0] = 0;
489     } else {
490         sps->vui.nal_hrd_parameters_present_flag = 0;
491         sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag;
492     }
493
494     sps->vui.bitstream_restriction_flag    = 1;
495     sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
496     sps->vui.log2_max_mv_length_horizontal = 15;
497     sps->vui.log2_max_mv_length_vertical   = 15;
498     sps->vui.max_num_reorder_frames        = (ctx->b_per_p > 0);
499     sps->vui.max_dec_frame_buffering       = sps->max_num_ref_frames;
500
501     pps->nal_unit_header.nal_ref_idc = 3;
502     pps->nal_unit_header.nal_unit_type = H264_NAL_PPS;
503
504     pps->pic_parameter_set_id = 0;
505     pps->seq_parameter_set_id = 0;
506
507     pps->entropy_coding_mode_flag =
508         !(sps->profile_idc == FF_PROFILE_H264_BASELINE ||
509           sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
510           sps->profile_idc == FF_PROFILE_H264_CAVLC_444);
511     if (!priv->coder && pps->entropy_coding_mode_flag)
512         pps->entropy_coding_mode_flag = 0;
513
514     pps->num_ref_idx_l0_default_active_minus1 = 0;
515     pps->num_ref_idx_l1_default_active_minus1 = 0;
516
517     pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
518
519     if (sps->profile_idc == FF_PROFILE_H264_BASELINE ||
520         sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
521         sps->profile_idc == FF_PROFILE_H264_MAIN) {
522         pps->more_rbsp_data = 0;
523     } else {
524         pps->more_rbsp_data = 1;
525
526         pps->transform_8x8_mode_flag = 1;
527     }
528
529     *vseq = (VAEncSequenceParameterBufferH264) {
530         .seq_parameter_set_id = sps->seq_parameter_set_id,
531         .level_idc        = sps->level_idc,
532         .intra_period     = ctx->gop_size,
533         .intra_idr_period = ctx->gop_size,
534         .ip_period        = ctx->b_per_p + 1,
535
536         .bits_per_second       = ctx->va_bit_rate,
537         .max_num_ref_frames    = sps->max_num_ref_frames,
538         .picture_width_in_mbs  = sps->pic_width_in_mbs_minus1 + 1,
539         .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
540
541         .seq_fields.bits = {
542             .chroma_format_idc                 = sps->chroma_format_idc,
543             .frame_mbs_only_flag               = sps->frame_mbs_only_flag,
544             .mb_adaptive_frame_field_flag      = sps->mb_adaptive_frame_field_flag,
545             .seq_scaling_matrix_present_flag   = sps->seq_scaling_matrix_present_flag,
546             .direct_8x8_inference_flag         = sps->direct_8x8_inference_flag,
547             .log2_max_frame_num_minus4         = sps->log2_max_frame_num_minus4,
548             .pic_order_cnt_type                = sps->pic_order_cnt_type,
549             .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
550             .delta_pic_order_always_zero_flag  = sps->delta_pic_order_always_zero_flag,
551         },
552
553         .bit_depth_luma_minus8   = sps->bit_depth_luma_minus8,
554         .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
555
556         .frame_cropping_flag      = sps->frame_cropping_flag,
557         .frame_crop_left_offset   = sps->frame_crop_left_offset,
558         .frame_crop_right_offset  = sps->frame_crop_right_offset,
559         .frame_crop_top_offset    = sps->frame_crop_top_offset,
560         .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
561
562         .vui_parameters_present_flag = sps->vui_parameters_present_flag,
563
564         .vui_fields.bits = {
565             .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
566             .timing_info_present_flag       = sps->vui.timing_info_present_flag,
567             .bitstream_restriction_flag     = sps->vui.bitstream_restriction_flag,
568             .log2_max_mv_length_horizontal  = sps->vui.log2_max_mv_length_horizontal,
569             .log2_max_mv_length_vertical    = sps->vui.log2_max_mv_length_vertical,
570         },
571
572         .aspect_ratio_idc  = sps->vui.aspect_ratio_idc,
573         .sar_width         = sps->vui.sar_width,
574         .sar_height        = sps->vui.sar_height,
575         .num_units_in_tick = sps->vui.num_units_in_tick,
576         .time_scale        = sps->vui.time_scale,
577     };
578
579     *vpic = (VAEncPictureParameterBufferH264) {
580         .CurrPic = {
581             .picture_id = VA_INVALID_ID,
582             .flags      = VA_PICTURE_H264_INVALID,
583         },
584
585         .coded_buf = VA_INVALID_ID,
586
587         .pic_parameter_set_id = pps->pic_parameter_set_id,
588         .seq_parameter_set_id = pps->seq_parameter_set_id,
589
590         .pic_init_qp                  = pps->pic_init_qp_minus26 + 26,
591         .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
592         .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
593
594         .chroma_qp_index_offset        = pps->chroma_qp_index_offset,
595         .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
596
597         .pic_fields.bits = {
598             .entropy_coding_mode_flag        = pps->entropy_coding_mode_flag,
599             .weighted_pred_flag              = pps->weighted_pred_flag,
600             .weighted_bipred_idc             = pps->weighted_bipred_idc,
601             .constrained_intra_pred_flag     = pps->constrained_intra_pred_flag,
602             .transform_8x8_mode_flag         = pps->transform_8x8_mode_flag,
603             .deblocking_filter_control_present_flag =
604                 pps->deblocking_filter_control_present_flag,
605             .redundant_pic_cnt_present_flag  = pps->redundant_pic_cnt_present_flag,
606             .pic_order_present_flag          =
607                 pps->bottom_field_pic_order_in_frame_present_flag,
608             .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
609         },
610     };
611
612     return 0;
613 }
614
615 static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
616                                                  VAAPIEncodePicture *pic)
617 {
618     VAAPIEncodeContext               *ctx = avctx->priv_data;
619     VAAPIEncodeH264Context          *priv = avctx->priv_data;
620     H264RawSPS                       *sps = &priv->raw_sps;
621     VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
622     int i;
623
624     memset(&priv->current_access_unit, 0,
625            sizeof(priv->current_access_unit));
626
627     if (pic->type == PICTURE_TYPE_IDR) {
628         av_assert0(pic->display_order == pic->encode_order);
629         priv->frame_num      = 0;
630         priv->next_frame_num = 1;
631         priv->cpb_delay      = 0;
632         priv->last_idr_frame = pic->display_order;
633         ++priv->idr_pic_count;
634
635         priv->slice_type       = 7;
636         priv->primary_pic_type = 0;
637     } else {
638         priv->frame_num      = priv->next_frame_num;
639
640         if (pic->type != PICTURE_TYPE_B) {
641             // Reference picture, so frame_num advances.
642             priv->next_frame_num = (priv->frame_num + 1) &
643                 ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
644         }
645         ++priv->cpb_delay;
646
647         if (pic->type == PICTURE_TYPE_I) {
648             priv->slice_type       = 7;
649             priv->primary_pic_type = 0;
650         } else if (pic->type == PICTURE_TYPE_P) {
651             priv->slice_type       = 5;
652             priv->primary_pic_type = 1;
653         } else {
654             priv->slice_type       = 6;
655             priv->primary_pic_type = 2;
656         }
657     }
658     priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
659     priv->dpb_delay     = pic->display_order - pic->encode_order + 1;
660
661     if (priv->aud) {
662         priv->aud_needed = 1;
663         priv->raw_aud = (H264RawAUD) {
664             .nal_unit_header = {
665                 .nal_unit_type = H264_NAL_AUD,
666             },
667             .primary_pic_type  = priv->primary_pic_type,
668         };
669     } else {
670         priv->aud_needed = 0;
671     }
672
673     priv->sei_needed = 0;
674
675     if (priv->sei & SEI_IDENTIFIER && pic->encode_order == 0)
676         priv->sei_needed |= SEI_IDENTIFIER;
677 #if !CONFIG_VAAPI_1
678     if (ctx->va_rc_mode == VA_RC_CBR)
679         priv->sei_cbr_workaround_needed = 1;
680 #endif
681
682     if (priv->sei & SEI_TIMING) {
683         priv->sei_pic_timing = (H264RawSEIPicTiming) {
684             .cpb_removal_delay = 2 * priv->cpb_delay,
685             .dpb_output_delay  = 2 * priv->dpb_delay,
686         };
687
688         priv->sei_needed |= SEI_TIMING;
689     }
690
691     if (priv->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
692         priv->sei_recovery_point = (H264RawSEIRecoveryPoint) {
693             .recovery_frame_cnt = 0,
694             .exact_match_flag   = 1,
695             .broken_link_flag   = ctx->b_per_p > 0,
696         };
697
698         priv->sei_needed |= SEI_RECOVERY_POINT;
699     }
700
701     vpic->CurrPic = (VAPictureH264) {
702         .picture_id          = pic->recon_surface,
703         .frame_idx           = priv->frame_num,
704         .flags               = 0,
705         .TopFieldOrderCnt    = priv->pic_order_cnt,
706         .BottomFieldOrderCnt = priv->pic_order_cnt,
707     };
708
709     for (i = 0; i < pic->nb_refs; i++) {
710         VAAPIEncodePicture *ref = pic->refs[i];
711         unsigned int frame_num = (ref->encode_order - priv->last_idr_frame) &
712             ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
713         unsigned int pic_order_cnt = ref->display_order - priv->last_idr_frame;
714
715         av_assert0(ref && ref->encode_order < pic->encode_order);
716         vpic->ReferenceFrames[i] = (VAPictureH264) {
717             .picture_id          = ref->recon_surface,
718             .frame_idx           = frame_num,
719             .flags               = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
720             .TopFieldOrderCnt    = pic_order_cnt,
721             .BottomFieldOrderCnt = pic_order_cnt,
722         };
723     }
724     for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
725         vpic->ReferenceFrames[i] = (VAPictureH264) {
726             .picture_id = VA_INVALID_ID,
727             .flags      = VA_PICTURE_H264_INVALID,
728         };
729     }
730
731     vpic->coded_buf = pic->output_buffer;
732
733     vpic->frame_num = priv->frame_num;
734
735     vpic->pic_fields.bits.idr_pic_flag       = (pic->type == PICTURE_TYPE_IDR);
736     vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
737
738     return 0;
739 }
740
741 static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
742                                                VAAPIEncodePicture *pic,
743                                                VAAPIEncodeSlice *slice)
744 {
745     VAAPIEncodeH264Context          *priv = avctx->priv_data;
746     H264RawSPS                       *sps = &priv->raw_sps;
747     H264RawPPS                       *pps = &priv->raw_pps;
748     H264RawSliceHeader                *sh = &priv->raw_slice.header;
749     VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
750     VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
751     int i;
752
753     if (pic->type == PICTURE_TYPE_IDR) {
754         sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE;
755         sh->nal_unit_header.nal_ref_idc   = 3;
756     } else {
757         sh->nal_unit_header.nal_unit_type = H264_NAL_SLICE;
758         sh->nal_unit_header.nal_ref_idc   = pic->type != PICTURE_TYPE_B;
759     }
760
761     sh->first_mb_in_slice = slice->block_start;
762     sh->slice_type        = priv->slice_type;
763
764     sh->pic_parameter_set_id = pps->pic_parameter_set_id;
765
766     sh->frame_num  = priv->frame_num;
767     sh->idr_pic_id = priv->idr_pic_count;
768
769     sh->pic_order_cnt_lsb = priv->pic_order_cnt &
770         ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
771
772     sh->direct_spatial_mv_pred_flag = 1;
773
774     if (pic->type == PICTURE_TYPE_B)
775         sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
776     else if (pic->type == PICTURE_TYPE_P)
777         sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
778     else
779         sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
780
781
782     vslice->macroblock_address = slice->block_start;
783     vslice->num_macroblocks    = slice->block_size;
784
785     vslice->macroblock_info = VA_INVALID_ID;
786
787     vslice->slice_type           = sh->slice_type % 5;
788     vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
789     vslice->idr_pic_id           = sh->idr_pic_id;
790
791     vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
792
793     vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
794
795     for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
796         vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
797         vslice->RefPicList0[i].flags      = VA_PICTURE_H264_INVALID;
798         vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
799         vslice->RefPicList1[i].flags      = VA_PICTURE_H264_INVALID;
800     }
801
802     av_assert0(pic->nb_refs <= 2);
803     if (pic->nb_refs >= 1) {
804         // Backward reference for P- or B-frame.
805         av_assert0(pic->type == PICTURE_TYPE_P ||
806                    pic->type == PICTURE_TYPE_B);
807         vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
808     }
809     if (pic->nb_refs >= 2) {
810         // Forward reference for B-frame.
811         av_assert0(pic->type == PICTURE_TYPE_B);
812         vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
813     }
814
815     vslice->slice_qp_delta = sh->slice_qp_delta;
816
817     return 0;
818 }
819
820 static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
821 {
822     VAAPIEncodeContext      *ctx = avctx->priv_data;
823     VAAPIEncodeH264Context *priv = avctx->priv_data;
824     int err;
825
826     err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
827     if (err < 0)
828         return err;
829
830     priv->mb_width  = FFALIGN(avctx->width,  16) / 16;
831     priv->mb_height = FFALIGN(avctx->height, 16) / 16;
832
833     if (ctx->va_rc_mode == VA_RC_CQP) {
834         priv->fixed_qp_p = priv->qp;
835         if (avctx->i_quant_factor > 0.0)
836             priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
837                                         avctx->i_quant_offset) + 0.5);
838         else
839             priv->fixed_qp_idr = priv->fixed_qp_p;
840         if (avctx->b_quant_factor > 0.0)
841             priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
842                                       avctx->b_quant_offset) + 0.5);
843         else
844             priv->fixed_qp_b = priv->fixed_qp_p;
845
846         priv->sei &= ~SEI_TIMING;
847
848         av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
849                "%d / %d / %d for IDR- / P- / B-frames.\n",
850                priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
851
852     } else if (ctx->va_rc_mode == VA_RC_CBR ||
853                ctx->va_rc_mode == VA_RC_VBR) {
854         // These still need to be  set for pic_init_qp/slice_qp_delta.
855         priv->fixed_qp_idr = 26;
856         priv->fixed_qp_p   = 26;
857         priv->fixed_qp_b   = 26;
858
859     } else {
860         av_assert0(0 && "Invalid RC mode.");
861     }
862
863     if (priv->sei & SEI_IDENTIFIER) {
864         const char *lavc  = LIBAVCODEC_IDENT;
865         const char *vaapi = VA_VERSION_S;
866         const char *driver;
867         int len;
868
869         memcpy(priv->sei_identifier.uuid_iso_iec_11578,
870                vaapi_encode_h264_sei_identifier_uuid,
871                sizeof(priv->sei_identifier.uuid_iso_iec_11578));
872
873         driver = vaQueryVendorString(ctx->hwctx->display);
874         if (!driver)
875             driver = "unknown driver";
876
877         len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
878         if (len >= 0) {
879             priv->sei_identifier_string = av_malloc(len + 1);
880             if (!priv->sei_identifier_string)
881                 return AVERROR(ENOMEM);
882
883             snprintf(priv->sei_identifier_string, len + 1,
884                      "%s / VAAPI %s / %s", lavc, vaapi, driver);
885
886             priv->sei_identifier.data        = priv->sei_identifier_string;
887             priv->sei_identifier.data_length = len + 1;
888         }
889     }
890
891     return 0;
892 }
893
894 static const VAAPIEncodeProfile vaapi_encode_h264_profiles[] = {
895     { FF_PROFILE_H264_HIGH, 8, 3, 1, 1, VAProfileH264High },
896     { FF_PROFILE_H264_MAIN, 8, 3, 1, 1, VAProfileH264Main },
897     { FF_PROFILE_H264_CONSTRAINED_BASELINE,
898                             8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
899     { FF_PROFILE_UNKNOWN }
900 };
901
902 static const VAAPIEncodeType vaapi_encode_type_h264 = {
903     .profiles              = vaapi_encode_h264_profiles,
904
905     .flags                 = FLAG_SLICE_CONTROL,
906
907     .configure             = &vaapi_encode_h264_configure,
908
909     .sequence_params_size  = sizeof(VAEncSequenceParameterBufferH264),
910     .init_sequence_params  = &vaapi_encode_h264_init_sequence_params,
911
912     .picture_params_size   = sizeof(VAEncPictureParameterBufferH264),
913     .init_picture_params   = &vaapi_encode_h264_init_picture_params,
914
915     .slice_params_size     = sizeof(VAEncSliceParameterBufferH264),
916     .init_slice_params     = &vaapi_encode_h264_init_slice_params,
917
918     .sequence_header_type  = VAEncPackedHeaderSequence,
919     .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
920
921     .slice_header_type     = VAEncPackedHeaderH264_Slice,
922     .write_slice_header    = &vaapi_encode_h264_write_slice_header,
923
924     .write_extra_header    = &vaapi_encode_h264_write_extra_header,
925 };
926
927 static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
928 {
929     VAAPIEncodeContext      *ctx = avctx->priv_data;
930     VAAPIEncodeH264Context *priv = avctx->priv_data;
931
932     ctx->codec = &vaapi_encode_type_h264;
933
934     if (avctx->profile == FF_PROFILE_UNKNOWN)
935         avctx->profile = priv->profile;
936     if (avctx->level == FF_LEVEL_UNKNOWN)
937         avctx->level = priv->level;
938     if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
939         avctx->compression_level = priv->quality;
940
941     // Reject unsupported profiles.
942     switch (avctx->profile) {
943     case FF_PROFILE_H264_BASELINE:
944         av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
945                "supported, using constrained baseline profile instead.\n");
946         avctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
947         break;
948     case FF_PROFILE_H264_EXTENDED:
949         av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
950                "is not supported.\n");
951         return AVERROR_PATCHWELCOME;
952     case FF_PROFILE_H264_HIGH_10:
953     case FF_PROFILE_H264_HIGH_10_INTRA:
954         av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
955                "are not supported.\n");
956         return AVERROR_PATCHWELCOME;
957     case FF_PROFILE_H264_HIGH_422:
958     case FF_PROFILE_H264_HIGH_422_INTRA:
959     case FF_PROFILE_H264_HIGH_444:
960     case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
961     case FF_PROFILE_H264_HIGH_444_INTRA:
962     case FF_PROFILE_H264_CAVLC_444:
963         av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
964                "are not supported.\n");
965         return AVERROR_PATCHWELCOME;
966     }
967
968     if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
969         av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
970                "in 8-bit unsigned integer.\n", avctx->level);
971         return AVERROR(EINVAL);
972     }
973
974     ctx->desired_packed_headers =
975         VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
976         VA_ENC_PACKED_HEADER_SLICE    | // Slice headers.
977         VA_ENC_PACKED_HEADER_MISC;      // SEI.
978
979     ctx->surface_width  = FFALIGN(avctx->width,  16);
980     ctx->surface_height = FFALIGN(avctx->height, 16);
981
982     ctx->slice_block_height = ctx->slice_block_width = 16;
983
984     return ff_vaapi_encode_init(avctx);
985 }
986
987 static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
988 {
989     VAAPIEncodeH264Context *priv = avctx->priv_data;
990
991     ff_cbs_close(&priv->cbc);
992     av_freep(&priv->sei_identifier_string);
993
994     return ff_vaapi_encode_close(avctx);
995 }
996
997 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
998 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
999 static const AVOption vaapi_encode_h264_options[] = {
1000     VAAPI_ENCODE_COMMON_OPTIONS,
1001
1002     { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1003       OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
1004     { "quality", "Set encode quality (trades off against speed, higher is faster)",
1005       OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
1006     { "coder", "Entropy coder type",
1007       OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
1008         { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1009         { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1010         { "vlc",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1011         { "ac",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1012
1013     { "aud", "Include AUD",
1014       OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1015
1016     { "sei", "Set SEI to include",
1017       OFFSET(sei), AV_OPT_TYPE_FLAGS,
1018       { .i64 = SEI_IDENTIFIER | SEI_TIMING | SEI_RECOVERY_POINT },
1019       0, INT_MAX, FLAGS, "sei" },
1020     { "identifier", "Include encoder version identifier",
1021       0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
1022       INT_MIN, INT_MAX, FLAGS, "sei" },
1023     { "timing", "Include timing parameters (buffering_period and pic_timing)",
1024       0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
1025       INT_MIN, INT_MAX, FLAGS, "sei" },
1026     { "recovery_point", "Include recovery points where appropriate",
1027       0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
1028       INT_MIN, INT_MAX, FLAGS, "sei" },
1029
1030     { "profile", "Set profile (profile_idc and constraint_set*_flag)",
1031       OFFSET(profile), AV_OPT_TYPE_INT,
1032       { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, FLAGS, "profile" },
1033
1034 #define PROFILE(name, value)  name, NULL, 0, AV_OPT_TYPE_CONST, \
1035       { .i64 = value }, 0, 0, FLAGS, "profile"
1036     { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
1037     { PROFILE("main",                 FF_PROFILE_H264_MAIN) },
1038     { PROFILE("high",                 FF_PROFILE_H264_HIGH) },
1039 #undef PROFILE
1040
1041     { "level", "Set level (level_idc)",
1042       OFFSET(level), AV_OPT_TYPE_INT,
1043       { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
1044
1045 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1046       { .i64 = value }, 0, 0, FLAGS, "level"
1047     { LEVEL("1",   10) },
1048     { LEVEL("1.1", 11) },
1049     { LEVEL("1.2", 12) },
1050     { LEVEL("1.3", 13) },
1051     { LEVEL("2",   20) },
1052     { LEVEL("2.1", 21) },
1053     { LEVEL("2.2", 22) },
1054     { LEVEL("3",   30) },
1055     { LEVEL("3.1", 31) },
1056     { LEVEL("3.2", 32) },
1057     { LEVEL("4",   40) },
1058     { LEVEL("4.1", 41) },
1059     { LEVEL("4.2", 42) },
1060     { LEVEL("5",   50) },
1061     { LEVEL("5.1", 51) },
1062     { LEVEL("5.2", 52) },
1063     { LEVEL("6",   60) },
1064     { LEVEL("6.1", 61) },
1065     { LEVEL("6.2", 62) },
1066 #undef LEVEL
1067
1068     { NULL },
1069 };
1070
1071 static const AVCodecDefault vaapi_encode_h264_defaults[] = {
1072     { "b",              "0"   },
1073     { "bf",             "2"   },
1074     { "g",              "120" },
1075     { "i_qfactor",      "1"   },
1076     { "i_qoffset",      "0"   },
1077     { "b_qfactor",      "6/5" },
1078     { "b_qoffset",      "0"   },
1079     { "qmin",           "-1"  },
1080     { "qmax",           "-1"  },
1081     { NULL },
1082 };
1083
1084 static const AVClass vaapi_encode_h264_class = {
1085     .class_name = "h264_vaapi",
1086     .item_name  = av_default_item_name,
1087     .option     = vaapi_encode_h264_options,
1088     .version    = LIBAVUTIL_VERSION_INT,
1089 };
1090
1091 AVCodec ff_h264_vaapi_encoder = {
1092     .name           = "h264_vaapi",
1093     .long_name      = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1094     .type           = AVMEDIA_TYPE_VIDEO,
1095     .id             = AV_CODEC_ID_H264,
1096     .priv_data_size = sizeof(VAAPIEncodeH264Context),
1097     .init           = &vaapi_encode_h264_init,
1098     .encode2        = &ff_vaapi_encode2,
1099     .close          = &vaapi_encode_h264_close,
1100     .priv_class     = &vaapi_encode_h264_class,
1101     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
1102     .defaults       = vaapi_encode_h264_defaults,
1103     .pix_fmts = (const enum AVPixelFormat[]) {
1104         AV_PIX_FMT_VAAPI,
1105         AV_PIX_FMT_NONE,
1106     },
1107     .wrapper_name   = "vaapi",
1108 };