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