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