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