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