avcodec/cavsdec: Propagate error codes inside decode_mb_i()
[ffmpeg.git] / libavcodec / vaapi_encode_h265.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_hevc.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/mastering_display_metadata.h"
29
30 #include "avcodec.h"
31 #include "cbs.h"
32 #include "cbs_h265.h"
33 #include "h265_profile_level.h"
34 #include "hevc.h"
35 #include "hevc_sei.h"
36 #include "internal.h"
37 #include "put_bits.h"
38 #include "vaapi_encode.h"
39
40 enum {
41     SEI_MASTERING_DISPLAY       = 0x08,
42     SEI_CONTENT_LIGHT_LEVEL     = 0x10,
43 };
44
45 typedef struct VAAPIEncodeH265Context {
46     VAAPIEncodeContext common;
47
48     // User options.
49     int qp;
50     int aud;
51     int profile;
52     int tier;
53     int level;
54     int sei;
55
56     // Derived settings.
57     int fixed_qp_idr;
58     int fixed_qp_p;
59     int fixed_qp_b;
60
61     // Stream state.
62     int64_t last_idr_frame;
63     int pic_order_cnt;
64
65     int slice_nal_unit;
66     int slice_type;
67     int pic_type;
68
69     // Writer structures.
70     H265RawAUD   raw_aud;
71     H265RawVPS   raw_vps;
72     H265RawSPS   raw_sps;
73     H265RawPPS   raw_pps;
74     H265RawSEI   raw_sei;
75     H265RawSlice raw_slice;
76
77     H265RawSEIMasteringDisplayColourVolume sei_mastering_display;
78     H265RawSEIContentLightLevelInfo        sei_content_light_level;
79
80     CodedBitstreamContext *cbc;
81     CodedBitstreamFragment current_access_unit;
82     int aud_needed;
83     int sei_needed;
84 } VAAPIEncodeH265Context;
85
86
87 static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx,
88                                                char *data, size_t *data_len,
89                                                CodedBitstreamFragment *au)
90 {
91     VAAPIEncodeH265Context *priv = avctx->priv_data;
92     int err;
93
94     err = ff_cbs_write_fragment_data(priv->cbc, au);
95     if (err < 0) {
96         av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
97         return err;
98     }
99
100     if (*data_len < 8 * au->data_size - au->data_bit_padding) {
101         av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
102                "%zu < %zu.\n", *data_len,
103                8 * au->data_size - au->data_bit_padding);
104         return AVERROR(ENOSPC);
105     }
106
107     memcpy(data, au->data, au->data_size);
108     *data_len = 8 * au->data_size - au->data_bit_padding;
109
110     return 0;
111 }
112
113 static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
114                                      CodedBitstreamFragment *au,
115                                      void *nal_unit)
116 {
117     VAAPIEncodeH265Context *priv = avctx->priv_data;
118     H265RawNALUnitHeader *header = nal_unit;
119     int err;
120
121     err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
122                                      header->nal_unit_type, nal_unit, NULL);
123     if (err < 0) {
124         av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
125                "type = %d.\n", header->nal_unit_type);
126         return err;
127     }
128
129     return 0;
130 }
131
132 static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
133                                                    char *data, size_t *data_len)
134 {
135     VAAPIEncodeH265Context *priv = avctx->priv_data;
136     CodedBitstreamFragment   *au = &priv->current_access_unit;
137     int err;
138
139     if (priv->aud_needed) {
140         err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
141         if (err < 0)
142             goto fail;
143         priv->aud_needed = 0;
144     }
145
146     err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_vps);
147     if (err < 0)
148         goto fail;
149
150     err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_sps);
151     if (err < 0)
152         goto fail;
153
154     err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_pps);
155     if (err < 0)
156         goto fail;
157
158     err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
159 fail:
160     ff_cbs_fragment_uninit(priv->cbc, au);
161     return err;
162 }
163
164 static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
165                                                 VAAPIEncodePicture *pic,
166                                                 VAAPIEncodeSlice *slice,
167                                                 char *data, size_t *data_len)
168 {
169     VAAPIEncodeH265Context *priv = avctx->priv_data;
170     CodedBitstreamFragment   *au = &priv->current_access_unit;
171     int err;
172
173     if (priv->aud_needed) {
174         err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
175         if (err < 0)
176             goto fail;
177         priv->aud_needed = 0;
178     }
179
180     err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_slice);
181     if (err < 0)
182         goto fail;
183
184     err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
185 fail:
186     ff_cbs_fragment_uninit(priv->cbc, au);
187     return err;
188 }
189
190 static int vaapi_encode_h265_write_extra_header(AVCodecContext *avctx,
191                                                 VAAPIEncodePicture *pic,
192                                                 int index, int *type,
193                                                 char *data, size_t *data_len)
194 {
195     VAAPIEncodeH265Context *priv = avctx->priv_data;
196     CodedBitstreamFragment   *au = &priv->current_access_unit;
197     int err, i;
198
199     if (priv->sei_needed) {
200         H265RawSEI *sei = &priv->raw_sei;
201
202         if (priv->aud_needed) {
203             err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
204             if (err < 0)
205                 goto fail;
206             priv->aud_needed = 0;
207         }
208
209         *sei = (H265RawSEI) {
210             .nal_unit_header = {
211                 .nal_unit_type         = HEVC_NAL_SEI_PREFIX,
212                 .nuh_layer_id          = 0,
213                 .nuh_temporal_id_plus1 = 1,
214             },
215         };
216
217         i = 0;
218
219         if (priv->sei_needed & SEI_MASTERING_DISPLAY) {
220             sei->payload[i].payload_type = HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO;
221             sei->payload[i].payload.mastering_display = priv->sei_mastering_display;
222             ++i;
223         }
224
225         if (priv->sei_needed & SEI_CONTENT_LIGHT_LEVEL) {
226             sei->payload[i].payload_type = HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO;
227             sei->payload[i].payload.content_light_level = priv->sei_content_light_level;
228             ++i;
229         }
230
231         sei->payload_count = i;
232         av_assert0(sei->payload_count > 0);
233
234         err = vaapi_encode_h265_add_nal(avctx, au, sei);
235         if (err < 0)
236             goto fail;
237         priv->sei_needed = 0;
238
239         err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
240         if (err < 0)
241             goto fail;
242
243         ff_cbs_fragment_uninit(priv->cbc, au);
244
245         *type = VAEncPackedHeaderRawData;
246         return 0;
247     } else {
248         return AVERROR_EOF;
249     }
250
251 fail:
252     ff_cbs_fragment_uninit(priv->cbc, au);
253     return err;
254 }
255
256 static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
257 {
258     VAAPIEncodeContext                *ctx = avctx->priv_data;
259     VAAPIEncodeH265Context           *priv = avctx->priv_data;
260     H265RawVPS                        *vps = &priv->raw_vps;
261     H265RawSPS                        *sps = &priv->raw_sps;
262     H265RawPPS                        *pps = &priv->raw_pps;
263     H265RawProfileTierLevel           *ptl = &vps->profile_tier_level;
264     H265RawVUI                        *vui = &sps->vui;
265     VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
266     VAEncPictureParameterBufferHEVC  *vpic = ctx->codec_picture_params;
267     const AVPixFmtDescriptor *desc;
268     int chroma_format, bit_depth;
269     int i;
270
271     memset(&priv->current_access_unit, 0,
272            sizeof(priv->current_access_unit));
273
274     memset(vps, 0, sizeof(*vps));
275     memset(sps, 0, sizeof(*sps));
276     memset(pps, 0, sizeof(*pps));
277
278
279     desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format);
280     av_assert0(desc);
281     if (desc->nb_components == 1) {
282         chroma_format = 0;
283     } else {
284         if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1) {
285             chroma_format = 1;
286         } else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0) {
287             chroma_format = 2;
288         } else if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
289             chroma_format = 3;
290         } else {
291             av_log(avctx, AV_LOG_ERROR, "Chroma format of input pixel format "
292                    "%s is not supported.\n", desc->name);
293             return AVERROR(EINVAL);
294         }
295     }
296     bit_depth = desc->comp[0].depth;
297
298
299     // VPS
300
301     vps->nal_unit_header = (H265RawNALUnitHeader) {
302         .nal_unit_type         = HEVC_NAL_VPS,
303         .nuh_layer_id          = 0,
304         .nuh_temporal_id_plus1 = 1,
305     };
306
307     vps->vps_video_parameter_set_id = 0;
308
309     vps->vps_base_layer_internal_flag  = 1;
310     vps->vps_base_layer_available_flag = 1;
311     vps->vps_max_layers_minus1         = 0;
312     vps->vps_max_sub_layers_minus1     = 0;
313     vps->vps_temporal_id_nesting_flag  = 1;
314
315     ptl->general_profile_space = 0;
316     ptl->general_profile_idc   = avctx->profile;
317     ptl->general_tier_flag     = priv->tier;
318
319     if (chroma_format == 1) {
320         ptl->general_profile_compatibility_flag[1] = bit_depth ==  8;
321         ptl->general_profile_compatibility_flag[2] = bit_depth <= 10;
322     }
323     ptl->general_profile_compatibility_flag[4] = 1;
324
325     ptl->general_progressive_source_flag    = 1;
326     ptl->general_interlaced_source_flag     = 0;
327     ptl->general_non_packed_constraint_flag = 1;
328     ptl->general_frame_only_constraint_flag = 1;
329
330     ptl->general_max_12bit_constraint_flag = bit_depth <= 12;
331     ptl->general_max_10bit_constraint_flag = bit_depth <= 10;
332     ptl->general_max_8bit_constraint_flag  = bit_depth ==  8;
333
334     ptl->general_max_422chroma_constraint_flag  = chroma_format <= 2;
335     ptl->general_max_420chroma_constraint_flag  = chroma_format <= 1;
336     ptl->general_max_monochrome_constraint_flag = chroma_format == 0;
337
338     ptl->general_intra_constraint_flag = ctx->gop_size == 1;
339
340     ptl->general_lower_bit_rate_constraint_flag = 1;
341
342     if (avctx->level != FF_LEVEL_UNKNOWN) {
343         ptl->general_level_idc = avctx->level;
344     } else {
345         const H265LevelDescriptor *level;
346
347         level = ff_h265_guess_level(ptl, avctx->bit_rate,
348                                     ctx->surface_width, ctx->surface_height,
349                                     ctx->nb_slices, 1, 1,
350                                     (ctx->b_per_p > 0) + 1);
351         if (level) {
352             av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
353             ptl->general_level_idc = level->level_idc;
354         } else {
355             av_log(avctx, AV_LOG_VERBOSE, "Stream will not conform to "
356                    "any normal level; using level 8.5.\n");
357             ptl->general_level_idc = 255;
358             // The tier flag must be set in level 8.5.
359             ptl->general_tier_flag = 1;
360         }
361     }
362
363     vps->vps_sub_layer_ordering_info_present_flag = 0;
364     vps->vps_max_dec_pic_buffering_minus1[0]      = (ctx->b_per_p > 0) + 1;
365     vps->vps_max_num_reorder_pics[0]              = (ctx->b_per_p > 0);
366     vps->vps_max_latency_increase_plus1[0]        = 0;
367
368     vps->vps_max_layer_id             = 0;
369     vps->vps_num_layer_sets_minus1    = 0;
370     vps->layer_id_included_flag[0][0] = 1;
371
372     vps->vps_timing_info_present_flag = 1;
373     if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
374         vps->vps_num_units_in_tick  = avctx->framerate.den;
375         vps->vps_time_scale         = avctx->framerate.num;
376         vps->vps_poc_proportional_to_timing_flag = 1;
377         vps->vps_num_ticks_poc_diff_one_minus1   = 0;
378     } else {
379         vps->vps_num_units_in_tick  = avctx->time_base.num;
380         vps->vps_time_scale         = avctx->time_base.den;
381         vps->vps_poc_proportional_to_timing_flag = 0;
382     }
383     vps->vps_num_hrd_parameters = 0;
384
385
386     // SPS
387
388     sps->nal_unit_header = (H265RawNALUnitHeader) {
389         .nal_unit_type         = HEVC_NAL_SPS,
390         .nuh_layer_id          = 0,
391         .nuh_temporal_id_plus1 = 1,
392     };
393
394     sps->sps_video_parameter_set_id = vps->vps_video_parameter_set_id;
395
396     sps->sps_max_sub_layers_minus1    = vps->vps_max_sub_layers_minus1;
397     sps->sps_temporal_id_nesting_flag = vps->vps_temporal_id_nesting_flag;
398
399     sps->profile_tier_level = vps->profile_tier_level;
400
401     sps->sps_seq_parameter_set_id = 0;
402
403     sps->chroma_format_idc          = chroma_format;
404     sps->separate_colour_plane_flag = 0;
405
406     sps->pic_width_in_luma_samples  = ctx->surface_width;
407     sps->pic_height_in_luma_samples = ctx->surface_height;
408
409     if (avctx->width  != ctx->surface_width ||
410         avctx->height != ctx->surface_height) {
411         sps->conformance_window_flag = 1;
412         sps->conf_win_left_offset   = 0;
413         sps->conf_win_right_offset  =
414             (ctx->surface_width - avctx->width) / 2;
415         sps->conf_win_top_offset    = 0;
416         sps->conf_win_bottom_offset =
417             (ctx->surface_height - avctx->height) / 2;
418     } else {
419         sps->conformance_window_flag = 0;
420     }
421
422     sps->bit_depth_luma_minus8   = bit_depth - 8;
423     sps->bit_depth_chroma_minus8 = bit_depth - 8;
424
425     sps->log2_max_pic_order_cnt_lsb_minus4 = 8;
426
427     sps->sps_sub_layer_ordering_info_present_flag =
428         vps->vps_sub_layer_ordering_info_present_flag;
429     for (i = 0; i <= sps->sps_max_sub_layers_minus1; i++) {
430         sps->sps_max_dec_pic_buffering_minus1[i] =
431             vps->vps_max_dec_pic_buffering_minus1[i];
432         sps->sps_max_num_reorder_pics[i] =
433             vps->vps_max_num_reorder_pics[i];
434         sps->sps_max_latency_increase_plus1[i] =
435             vps->vps_max_latency_increase_plus1[i];
436     }
437
438     // These have to come from the capabilities of the encoder.  We have no
439     // way to query them, so just hardcode parameters which work on the Intel
440     // driver.
441     // CTB size from 8x8 to 32x32.
442     sps->log2_min_luma_coding_block_size_minus3   = 0;
443     sps->log2_diff_max_min_luma_coding_block_size = 2;
444     // Transform size from 4x4 to 32x32.
445     sps->log2_min_luma_transform_block_size_minus2   = 0;
446     sps->log2_diff_max_min_luma_transform_block_size = 3;
447     // Full transform hierarchy allowed (2-5).
448     sps->max_transform_hierarchy_depth_inter = 3;
449     sps->max_transform_hierarchy_depth_intra = 3;
450     // AMP works.
451     sps->amp_enabled_flag = 1;
452     // SAO and temporal MVP do not work.
453     sps->sample_adaptive_offset_enabled_flag = 0;
454     sps->sps_temporal_mvp_enabled_flag       = 0;
455
456     sps->pcm_enabled_flag = 0;
457
458     // STRPSs should ideally be here rather than defined individually in
459     // each slice, but the structure isn't completely fixed so for now
460     // don't bother.
461     sps->num_short_term_ref_pic_sets     = 0;
462     sps->long_term_ref_pics_present_flag = 0;
463
464     sps->vui_parameters_present_flag = 1;
465
466     if (avctx->sample_aspect_ratio.num != 0 &&
467         avctx->sample_aspect_ratio.den != 0) {
468         static const AVRational sar_idc[] = {
469             {   0,  0 },
470             {   1,  1 }, {  12, 11 }, {  10, 11 }, {  16, 11 },
471             {  40, 33 }, {  24, 11 }, {  20, 11 }, {  32, 11 },
472             {  80, 33 }, {  18, 11 }, {  15, 11 }, {  64, 33 },
473             { 160, 99 }, {   4,  3 }, {   3,  2 }, {   2,  1 },
474         };
475         int i;
476         for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
477             if (avctx->sample_aspect_ratio.num == sar_idc[i].num &&
478                 avctx->sample_aspect_ratio.den == sar_idc[i].den) {
479                 vui->aspect_ratio_idc = i;
480                 break;
481             }
482         }
483         if (i >= FF_ARRAY_ELEMS(sar_idc)) {
484             vui->aspect_ratio_idc = 255;
485             vui->sar_width  = avctx->sample_aspect_ratio.num;
486             vui->sar_height = avctx->sample_aspect_ratio.den;
487         }
488         vui->aspect_ratio_info_present_flag = 1;
489     }
490
491     if (avctx->color_range     != AVCOL_RANGE_UNSPECIFIED ||
492         avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
493         avctx->color_trc       != AVCOL_TRC_UNSPECIFIED ||
494         avctx->colorspace      != AVCOL_SPC_UNSPECIFIED) {
495         vui->video_signal_type_present_flag = 1;
496         vui->video_format      = 5; // Unspecified.
497         vui->video_full_range_flag =
498             avctx->color_range == AVCOL_RANGE_JPEG;
499
500         if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
501             avctx->color_trc       != AVCOL_TRC_UNSPECIFIED ||
502             avctx->colorspace      != AVCOL_SPC_UNSPECIFIED) {
503             vui->colour_description_present_flag = 1;
504             vui->colour_primaries         = avctx->color_primaries;
505             vui->transfer_characteristics = avctx->color_trc;
506             vui->matrix_coefficients      = avctx->colorspace;
507         }
508     } else {
509         vui->video_format             = 5;
510         vui->video_full_range_flag    = 0;
511         vui->colour_primaries         = avctx->color_primaries;
512         vui->transfer_characteristics = avctx->color_trc;
513         vui->matrix_coefficients      = avctx->colorspace;
514     }
515
516     if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
517         vui->chroma_loc_info_present_flag = 1;
518         vui->chroma_sample_loc_type_top_field    =
519         vui->chroma_sample_loc_type_bottom_field =
520             avctx->chroma_sample_location - 1;
521     }
522
523     vui->vui_timing_info_present_flag        = 1;
524     vui->vui_num_units_in_tick               = vps->vps_num_units_in_tick;
525     vui->vui_time_scale                      = vps->vps_time_scale;
526     vui->vui_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag;
527     vui->vui_num_ticks_poc_diff_one_minus1   = vps->vps_num_ticks_poc_diff_one_minus1;
528     vui->vui_hrd_parameters_present_flag     = 0;
529
530     vui->bitstream_restriction_flag    = 1;
531     vui->motion_vectors_over_pic_boundaries_flag = 1;
532     vui->restricted_ref_pic_lists_flag = 1;
533     vui->max_bytes_per_pic_denom       = 0;
534     vui->max_bits_per_min_cu_denom     = 0;
535     vui->log2_max_mv_length_horizontal = 15;
536     vui->log2_max_mv_length_vertical   = 15;
537
538
539     // PPS
540
541     pps->nal_unit_header = (H265RawNALUnitHeader) {
542         .nal_unit_type         = HEVC_NAL_PPS,
543         .nuh_layer_id          = 0,
544         .nuh_temporal_id_plus1 = 1,
545     };
546
547     pps->pps_pic_parameter_set_id = 0;
548     pps->pps_seq_parameter_set_id = sps->sps_seq_parameter_set_id;
549
550     pps->num_ref_idx_l0_default_active_minus1 = 0;
551     pps->num_ref_idx_l1_default_active_minus1 = 0;
552
553     pps->init_qp_minus26 = priv->fixed_qp_idr - 26;
554
555     pps->cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP);
556     pps->diff_cu_qp_delta_depth   = 0;
557
558     pps->pps_loop_filter_across_slices_enabled_flag = 1;
559
560
561     // Fill VAAPI parameter buffers.
562
563     *vseq = (VAEncSequenceParameterBufferHEVC) {
564         .general_profile_idc = vps->profile_tier_level.general_profile_idc,
565         .general_level_idc   = vps->profile_tier_level.general_level_idc,
566         .general_tier_flag   = vps->profile_tier_level.general_tier_flag,
567
568         .intra_period     = ctx->gop_size,
569         .intra_idr_period = ctx->gop_size,
570         .ip_period        = ctx->b_per_p + 1,
571         .bits_per_second  = ctx->va_bit_rate,
572
573         .pic_width_in_luma_samples  = sps->pic_width_in_luma_samples,
574         .pic_height_in_luma_samples = sps->pic_height_in_luma_samples,
575
576         .seq_fields.bits = {
577             .chroma_format_idc             = sps->chroma_format_idc,
578             .separate_colour_plane_flag    = sps->separate_colour_plane_flag,
579             .bit_depth_luma_minus8         = sps->bit_depth_luma_minus8,
580             .bit_depth_chroma_minus8       = sps->bit_depth_chroma_minus8,
581             .scaling_list_enabled_flag     = sps->scaling_list_enabled_flag,
582             .strong_intra_smoothing_enabled_flag =
583                 sps->strong_intra_smoothing_enabled_flag,
584             .amp_enabled_flag              = sps->amp_enabled_flag,
585             .sample_adaptive_offset_enabled_flag =
586                 sps->sample_adaptive_offset_enabled_flag,
587             .pcm_enabled_flag              = sps->pcm_enabled_flag,
588             .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag,
589             .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
590         },
591
592         .log2_min_luma_coding_block_size_minus3 =
593             sps->log2_min_luma_coding_block_size_minus3,
594         .log2_diff_max_min_luma_coding_block_size =
595             sps->log2_diff_max_min_luma_coding_block_size,
596         .log2_min_transform_block_size_minus2 =
597             sps->log2_min_luma_transform_block_size_minus2,
598         .log2_diff_max_min_transform_block_size =
599             sps->log2_diff_max_min_luma_transform_block_size,
600         .max_transform_hierarchy_depth_inter =
601             sps->max_transform_hierarchy_depth_inter,
602         .max_transform_hierarchy_depth_intra =
603             sps->max_transform_hierarchy_depth_intra,
604
605         .pcm_sample_bit_depth_luma_minus1 =
606             sps->pcm_sample_bit_depth_luma_minus1,
607         .pcm_sample_bit_depth_chroma_minus1 =
608             sps->pcm_sample_bit_depth_chroma_minus1,
609         .log2_min_pcm_luma_coding_block_size_minus3 =
610             sps->log2_min_pcm_luma_coding_block_size_minus3,
611         .log2_max_pcm_luma_coding_block_size_minus3 =
612             sps->log2_min_pcm_luma_coding_block_size_minus3 +
613             sps->log2_diff_max_min_pcm_luma_coding_block_size,
614
615         .vui_parameters_present_flag = 0,
616     };
617
618     *vpic = (VAEncPictureParameterBufferHEVC) {
619         .decoded_curr_pic = {
620             .picture_id = VA_INVALID_ID,
621             .flags      = VA_PICTURE_HEVC_INVALID,
622         },
623
624         .coded_buf = VA_INVALID_ID,
625
626         .collocated_ref_pic_index = 0xff,
627
628         .last_picture = 0,
629
630         .pic_init_qp            = pps->init_qp_minus26 + 26,
631         .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
632         .pps_cb_qp_offset       = pps->pps_cb_qp_offset,
633         .pps_cr_qp_offset       = pps->pps_cr_qp_offset,
634
635         .num_tile_columns_minus1 = pps->num_tile_columns_minus1,
636         .num_tile_rows_minus1    = pps->num_tile_rows_minus1,
637
638         .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2,
639         .ctu_max_bitsize_allowed          = 0,
640
641         .num_ref_idx_l0_default_active_minus1 =
642             pps->num_ref_idx_l0_default_active_minus1,
643         .num_ref_idx_l1_default_active_minus1 =
644             pps->num_ref_idx_l1_default_active_minus1,
645
646         .slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id,
647
648         .pic_fields.bits = {
649             .sign_data_hiding_enabled_flag  = pps->sign_data_hiding_enabled_flag,
650             .constrained_intra_pred_flag    = pps->constrained_intra_pred_flag,
651             .transform_skip_enabled_flag    = pps->transform_skip_enabled_flag,
652             .cu_qp_delta_enabled_flag       = pps->cu_qp_delta_enabled_flag,
653             .weighted_pred_flag             = pps->weighted_pred_flag,
654             .weighted_bipred_flag           = pps->weighted_bipred_flag,
655             .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag,
656             .tiles_enabled_flag             = pps->tiles_enabled_flag,
657             .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
658             .loop_filter_across_tiles_enabled_flag =
659                 pps->loop_filter_across_tiles_enabled_flag,
660             .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag |
661                                                pps->pps_scaling_list_data_present_flag),
662             .screen_content_flag            = 0,
663             .enable_gpu_weighted_prediction = 0,
664             .no_output_of_prior_pics_flag   = 0,
665         },
666     };
667
668     return 0;
669 }
670
671 static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
672                                                  VAAPIEncodePicture *pic)
673 {
674     VAAPIEncodeH265Context          *priv = avctx->priv_data;
675     VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
676     int i;
677
678     if (pic->type == PICTURE_TYPE_IDR) {
679         av_assert0(pic->display_order == pic->encode_order);
680
681         priv->last_idr_frame = pic->display_order;
682
683         priv->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
684         priv->slice_type     = HEVC_SLICE_I;
685         priv->pic_type       = 0;
686     } else {
687         av_assert0(pic->encode_order > priv->last_idr_frame);
688
689         if (pic->type == PICTURE_TYPE_I) {
690             priv->slice_nal_unit = HEVC_NAL_CRA_NUT;
691             priv->slice_type     = HEVC_SLICE_I;
692             priv->pic_type       = 0;
693         } else if (pic->type == PICTURE_TYPE_P) {
694             av_assert0(pic->refs[0]);
695             priv->slice_nal_unit = HEVC_NAL_TRAIL_R;
696             priv->slice_type     = HEVC_SLICE_P;
697             priv->pic_type       = 1;
698         } else {
699             av_assert0(pic->refs[0] && pic->refs[1]);
700             if (pic->refs[1]->type == PICTURE_TYPE_I)
701                 priv->slice_nal_unit = HEVC_NAL_RASL_N;
702             else
703                 priv->slice_nal_unit = HEVC_NAL_TRAIL_N;
704             priv->slice_type = HEVC_SLICE_B;
705             priv->pic_type   = 2;
706         }
707     }
708     priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
709
710     if (priv->aud) {
711         priv->aud_needed = 1;
712         priv->raw_aud = (H265RawAUD) {
713             .nal_unit_header = {
714                 .nal_unit_type         = HEVC_NAL_AUD,
715                 .nuh_layer_id          = 0,
716                 .nuh_temporal_id_plus1 = 1,
717             },
718             .pic_type = priv->pic_type,
719         };
720     } else {
721         priv->aud_needed = 0;
722     }
723
724     priv->sei_needed = 0;
725
726     // Only look for the metadata on I/IDR frame on the output. We
727     // may force an IDR frame on the output where the medadata gets
728     // changed on the input frame.
729     if ((priv->sei & SEI_MASTERING_DISPLAY) &&
730         (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
731         AVFrameSideData *sd =
732             av_frame_get_side_data(pic->input_image,
733                                    AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
734
735         if (sd) {
736             AVMasteringDisplayMetadata *mdm =
737                 (AVMasteringDisplayMetadata *)sd->data;
738
739             // SEI is needed when both the primaries and luminance are set
740             if (mdm->has_primaries && mdm->has_luminance) {
741                 H265RawSEIMasteringDisplayColourVolume *mdcv =
742                     &priv->sei_mastering_display;
743                 const int mapping[3] = {1, 2, 0};
744                 const int chroma_den = 50000;
745                 const int luma_den   = 10000;
746
747                 for (i = 0; i < 3; i++) {
748                     const int j = mapping[i];
749                     mdcv->display_primaries_x[i] =
750                         FFMIN(lrint(chroma_den *
751                                     av_q2d(mdm->display_primaries[j][0])),
752                               chroma_den);
753                     mdcv->display_primaries_y[i] =
754                         FFMIN(lrint(chroma_den *
755                                     av_q2d(mdm->display_primaries[j][1])),
756                               chroma_den);
757                 }
758
759                 mdcv->white_point_x =
760                     FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
761                           chroma_den);
762                 mdcv->white_point_y =
763                     FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
764                           chroma_den);
765
766                 mdcv->max_display_mastering_luminance =
767                     lrint(luma_den * av_q2d(mdm->max_luminance));
768                 mdcv->min_display_mastering_luminance =
769                     FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
770                           mdcv->max_display_mastering_luminance);
771
772                 priv->sei_needed |= SEI_MASTERING_DISPLAY;
773             }
774         }
775     }
776
777     if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
778         (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
779         AVFrameSideData *sd =
780             av_frame_get_side_data(pic->input_image,
781                                    AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
782
783         if (sd) {
784             AVContentLightMetadata *clm =
785                 (AVContentLightMetadata *)sd->data;
786             H265RawSEIContentLightLevelInfo *clli =
787                 &priv->sei_content_light_level;
788
789             clli->max_content_light_level     = FFMIN(clm->MaxCLL,  65535);
790             clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
791
792             priv->sei_needed |= SEI_CONTENT_LIGHT_LEVEL;
793         }
794     }
795
796     vpic->decoded_curr_pic = (VAPictureHEVC) {
797         .picture_id    = pic->recon_surface,
798         .pic_order_cnt = priv->pic_order_cnt,
799         .flags         = 0,
800     };
801
802     for (i = 0; i < pic->nb_refs; i++) {
803         VAAPIEncodePicture *ref = pic->refs[i];
804         av_assert0(ref && ref->encode_order < pic->encode_order);
805
806         vpic->reference_frames[i] = (VAPictureHEVC) {
807             .picture_id    = ref->recon_surface,
808             .pic_order_cnt = ref->display_order - priv->last_idr_frame,
809             .flags = (ref->display_order < pic->display_order ?
810                       VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
811                      (ref->display_order > pic->display_order ?
812                       VA_PICTURE_HEVC_RPS_ST_CURR_AFTER  : 0),
813         };
814     }
815     for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
816         vpic->reference_frames[i] = (VAPictureHEVC) {
817             .picture_id = VA_INVALID_ID,
818             .flags      = VA_PICTURE_HEVC_INVALID,
819         };
820     }
821
822     vpic->coded_buf = pic->output_buffer;
823
824     vpic->nal_unit_type = priv->slice_nal_unit;
825
826     switch (pic->type) {
827     case PICTURE_TYPE_IDR:
828         vpic->pic_fields.bits.idr_pic_flag       = 1;
829         vpic->pic_fields.bits.coding_type        = 1;
830         vpic->pic_fields.bits.reference_pic_flag = 1;
831         break;
832     case PICTURE_TYPE_I:
833         vpic->pic_fields.bits.idr_pic_flag       = 0;
834         vpic->pic_fields.bits.coding_type        = 1;
835         vpic->pic_fields.bits.reference_pic_flag = 1;
836         break;
837     case PICTURE_TYPE_P:
838         vpic->pic_fields.bits.idr_pic_flag       = 0;
839         vpic->pic_fields.bits.coding_type        = 2;
840         vpic->pic_fields.bits.reference_pic_flag = 1;
841         break;
842     case PICTURE_TYPE_B:
843         vpic->pic_fields.bits.idr_pic_flag       = 0;
844         vpic->pic_fields.bits.coding_type        = 3;
845         vpic->pic_fields.bits.reference_pic_flag = 0;
846         break;
847     default:
848         av_assert0(0 && "invalid picture type");
849     }
850
851     return 0;
852 }
853
854 static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
855                                                VAAPIEncodePicture *pic,
856                                                VAAPIEncodeSlice *slice)
857 {
858     VAAPIEncodeContext                *ctx = avctx->priv_data;
859     VAAPIEncodeH265Context           *priv = avctx->priv_data;
860     const H265RawSPS                  *sps = &priv->raw_sps;
861     const H265RawPPS                  *pps = &priv->raw_pps;
862     H265RawSliceHeader                 *sh = &priv->raw_slice.header;
863     VAEncPictureParameterBufferHEVC  *vpic = pic->codec_picture_params;
864     VAEncSliceParameterBufferHEVC  *vslice = slice->codec_slice_params;
865     int i;
866
867     sh->nal_unit_header = (H265RawNALUnitHeader) {
868         .nal_unit_type         = priv->slice_nal_unit,
869         .nuh_layer_id          = 0,
870         .nuh_temporal_id_plus1 = 1,
871     };
872
873     sh->slice_pic_parameter_set_id      = pps->pps_pic_parameter_set_id;
874
875     sh->first_slice_segment_in_pic_flag = slice->index == 0;
876     sh->slice_segment_address           = slice->block_start;
877
878     sh->slice_type = priv->slice_type;
879
880     sh->slice_pic_order_cnt_lsb = priv->pic_order_cnt &
881         (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
882
883     if (pic->type != PICTURE_TYPE_IDR) {
884         H265RawSTRefPicSet *rps;
885         VAAPIEncodePicture *st;
886         int used;
887
888         sh->short_term_ref_pic_set_sps_flag = 0;
889
890         rps = &sh->short_term_ref_pic_set;
891         memset(rps, 0, sizeof(*rps));
892
893         for (st = ctx->pic_start; st; st = st->next) {
894             if (st->encode_order >= pic->encode_order) {
895                 // Not yet in DPB.
896                 continue;
897             }
898             used = 0;
899             for (i = 0; i < pic->nb_refs; i++) {
900                 if (pic->refs[i] == st)
901                     used = 1;
902             }
903             if (!used) {
904                 // Usually each picture always uses all of the others in the
905                 // DPB as references.  The one case we have to treat here is
906                 // a non-IDR IRAP picture, which may need to hold unused
907                 // references across itself to be used for the decoding of
908                 // following RASL pictures.  This looks for such an RASL
909                 // picture, and keeps the reference if there is one.
910                 VAAPIEncodePicture *rp;
911                 for (rp = ctx->pic_start; rp; rp = rp->next) {
912                     if (rp->encode_order < pic->encode_order)
913                         continue;
914                     if (rp->type != PICTURE_TYPE_B)
915                         continue;
916                     if (rp->refs[0] == st && rp->refs[1] == pic)
917                         break;
918                 }
919                 if (!rp)
920                     continue;
921             }
922             // This only works for one instance of each (delta_poc_sN_minus1
923             // is relative to the previous frame in the list, not relative to
924             // the current frame directly).
925             if (st->display_order < pic->display_order) {
926                 rps->delta_poc_s0_minus1[rps->num_negative_pics] =
927                     pic->display_order - st->display_order - 1;
928                 rps->used_by_curr_pic_s0_flag[rps->num_negative_pics] = used;
929                 ++rps->num_negative_pics;
930             } else {
931                 rps->delta_poc_s1_minus1[rps->num_positive_pics] =
932                     st->display_order - pic->display_order - 1;
933                 rps->used_by_curr_pic_s1_flag[rps->num_positive_pics] = used;
934                 ++rps->num_positive_pics;
935             }
936         }
937
938         sh->num_long_term_sps  = 0;
939         sh->num_long_term_pics = 0;
940
941         sh->slice_temporal_mvp_enabled_flag =
942             sps->sps_temporal_mvp_enabled_flag;
943         if (sh->slice_temporal_mvp_enabled_flag) {
944             sh->collocated_from_l0_flag = sh->slice_type == HEVC_SLICE_B;
945             sh->collocated_ref_idx      = 0;
946         }
947
948         sh->num_ref_idx_active_override_flag = 0;
949         sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
950         sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
951     }
952
953     sh->slice_sao_luma_flag = sh->slice_sao_chroma_flag =
954         sps->sample_adaptive_offset_enabled_flag;
955
956     if (pic->type == PICTURE_TYPE_B)
957         sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
958     else if (pic->type == PICTURE_TYPE_P)
959         sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
960     else
961         sh->slice_qp_delta = priv->fixed_qp_idr - (pps->init_qp_minus26 + 26);
962
963
964     *vslice = (VAEncSliceParameterBufferHEVC) {
965         .slice_segment_address = sh->slice_segment_address,
966         .num_ctu_in_slice      = slice->block_size,
967
968         .slice_type                 = sh->slice_type,
969         .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
970
971         .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
972         .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
973
974         .luma_log2_weight_denom         = sh->luma_log2_weight_denom,
975         .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
976
977         .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
978
979         .slice_qp_delta     = sh->slice_qp_delta,
980         .slice_cb_qp_offset = sh->slice_cb_qp_offset,
981         .slice_cr_qp_offset = sh->slice_cr_qp_offset,
982
983         .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
984         .slice_tc_offset_div2   = sh->slice_tc_offset_div2,
985
986         .slice_fields.bits = {
987             .last_slice_of_pic_flag       = slice->index == pic->nb_slices - 1,
988             .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
989             .colour_plane_id              = sh->colour_plane_id,
990             .slice_temporal_mvp_enabled_flag =
991                 sh->slice_temporal_mvp_enabled_flag,
992             .slice_sao_luma_flag          = sh->slice_sao_luma_flag,
993             .slice_sao_chroma_flag        = sh->slice_sao_chroma_flag,
994             .num_ref_idx_active_override_flag =
995                 sh->num_ref_idx_active_override_flag,
996             .mvd_l1_zero_flag             = sh->mvd_l1_zero_flag,
997             .cabac_init_flag              = sh->cabac_init_flag,
998             .slice_deblocking_filter_disabled_flag =
999                 sh->slice_deblocking_filter_disabled_flag,
1000             .slice_loop_filter_across_slices_enabled_flag =
1001                 sh->slice_loop_filter_across_slices_enabled_flag,
1002             .collocated_from_l0_flag      = sh->collocated_from_l0_flag,
1003         },
1004     };
1005
1006     for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
1007         vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
1008         vslice->ref_pic_list0[i].flags      = VA_PICTURE_HEVC_INVALID;
1009         vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
1010         vslice->ref_pic_list1[i].flags      = VA_PICTURE_HEVC_INVALID;
1011     }
1012
1013     av_assert0(pic->nb_refs <= 2);
1014     if (pic->nb_refs >= 1) {
1015         // Backward reference for P- or B-frame.
1016         av_assert0(pic->type == PICTURE_TYPE_P ||
1017                    pic->type == PICTURE_TYPE_B);
1018         vslice->ref_pic_list0[0] = vpic->reference_frames[0];
1019     }
1020     if (pic->nb_refs >= 2) {
1021         // Forward reference for B-frame.
1022         av_assert0(pic->type == PICTURE_TYPE_B);
1023         vslice->ref_pic_list1[0] = vpic->reference_frames[1];
1024     }
1025
1026     return 0;
1027 }
1028
1029 static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
1030 {
1031     VAAPIEncodeContext      *ctx = avctx->priv_data;
1032     VAAPIEncodeH265Context *priv = avctx->priv_data;
1033     int err;
1034
1035     err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
1036     if (err < 0)
1037         return err;
1038
1039     if (ctx->va_rc_mode == VA_RC_CQP) {
1040         priv->fixed_qp_p = priv->qp;
1041         if (avctx->i_quant_factor > 0.0)
1042             priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
1043                                         avctx->i_quant_offset) + 0.5);
1044         else
1045             priv->fixed_qp_idr = priv->fixed_qp_p;
1046         if (avctx->b_quant_factor > 0.0)
1047             priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
1048                                       avctx->b_quant_offset) + 0.5);
1049         else
1050             priv->fixed_qp_b = priv->fixed_qp_p;
1051
1052         av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1053                "%d / %d / %d for IDR- / P- / B-frames.\n",
1054                priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1055
1056     } else if (ctx->va_rc_mode == VA_RC_CBR ||
1057                ctx->va_rc_mode == VA_RC_VBR) {
1058         // These still need to be  set for pic_init_qp/slice_qp_delta.
1059         priv->fixed_qp_idr = 30;
1060         priv->fixed_qp_p   = 30;
1061         priv->fixed_qp_b   = 30;
1062
1063     } else {
1064         av_assert0(0 && "Invalid RC mode.");
1065     }
1066
1067     return 0;
1068 }
1069
1070 static const VAAPIEncodeProfile vaapi_encode_h265_profiles[] = {
1071     { FF_PROFILE_HEVC_MAIN,     8, 3, 1, 1, VAProfileHEVCMain       },
1072     { FF_PROFILE_HEVC_REXT,     8, 3, 1, 1, VAProfileHEVCMain       },
1073 #if VA_CHECK_VERSION(0, 37, 0)
1074     { FF_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10     },
1075     { FF_PROFILE_HEVC_REXT,    10, 3, 1, 1, VAProfileHEVCMain10     },
1076 #endif
1077     { FF_PROFILE_UNKNOWN }
1078 };
1079
1080 static const VAAPIEncodeType vaapi_encode_type_h265 = {
1081     .profiles              = vaapi_encode_h265_profiles,
1082
1083     .flags                 = FLAG_SLICE_CONTROL,
1084
1085     .configure             = &vaapi_encode_h265_configure,
1086
1087     .sequence_params_size  = sizeof(VAEncSequenceParameterBufferHEVC),
1088     .init_sequence_params  = &vaapi_encode_h265_init_sequence_params,
1089
1090     .picture_params_size   = sizeof(VAEncPictureParameterBufferHEVC),
1091     .init_picture_params   = &vaapi_encode_h265_init_picture_params,
1092
1093     .slice_params_size     = sizeof(VAEncSliceParameterBufferHEVC),
1094     .init_slice_params     = &vaapi_encode_h265_init_slice_params,
1095
1096     .sequence_header_type  = VAEncPackedHeaderSequence,
1097     .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
1098
1099     .slice_header_type     = VAEncPackedHeaderHEVC_Slice,
1100     .write_slice_header    = &vaapi_encode_h265_write_slice_header,
1101
1102     .write_extra_header    = &vaapi_encode_h265_write_extra_header,
1103 };
1104
1105 static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
1106 {
1107     VAAPIEncodeContext      *ctx = avctx->priv_data;
1108     VAAPIEncodeH265Context *priv = avctx->priv_data;
1109
1110     ctx->codec = &vaapi_encode_type_h265;
1111
1112     if (avctx->profile == FF_PROFILE_UNKNOWN)
1113         avctx->profile = priv->profile;
1114     if (avctx->level == FF_LEVEL_UNKNOWN)
1115         avctx->level = priv->level;
1116
1117     if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
1118         av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
1119                "in 8-bit unsigned integer.\n", avctx->level);
1120         return AVERROR(EINVAL);
1121     }
1122
1123     ctx->desired_packed_headers =
1124         VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
1125         VA_ENC_PACKED_HEADER_SLICE    | // Slice headers.
1126         VA_ENC_PACKED_HEADER_MISC;      // SEI
1127
1128     ctx->surface_width  = FFALIGN(avctx->width,  16);
1129     ctx->surface_height = FFALIGN(avctx->height, 16);
1130
1131     // CTU size is currently hard-coded to 32.
1132     ctx->slice_block_width = ctx->slice_block_height = 32;
1133
1134     return ff_vaapi_encode_init(avctx);
1135 }
1136
1137 static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
1138 {
1139     VAAPIEncodeH265Context *priv = avctx->priv_data;
1140
1141     ff_cbs_close(&priv->cbc);
1142
1143     return ff_vaapi_encode_close(avctx);
1144 }
1145
1146 #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
1147 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1148 static const AVOption vaapi_encode_h265_options[] = {
1149     VAAPI_ENCODE_COMMON_OPTIONS,
1150
1151     { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1152       OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, 52, FLAGS },
1153
1154     { "aud", "Include AUD",
1155       OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1156
1157     { "profile", "Set profile (general_profile_idc)",
1158       OFFSET(profile), AV_OPT_TYPE_INT,
1159       { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
1160
1161 #define PROFILE(name, value)  name, NULL, 0, AV_OPT_TYPE_CONST, \
1162       { .i64 = value }, 0, 0, FLAGS, "profile"
1163     { PROFILE("main",               FF_PROFILE_HEVC_MAIN) },
1164     { PROFILE("main10",             FF_PROFILE_HEVC_MAIN_10) },
1165     { PROFILE("rext",               FF_PROFILE_HEVC_REXT) },
1166 #undef PROFILE
1167
1168     { "tier", "Set tier (general_tier_flag)",
1169       OFFSET(tier), AV_OPT_TYPE_INT,
1170       { .i64 = 0 }, 0, 1, FLAGS, "tier" },
1171     { "main", NULL, 0, AV_OPT_TYPE_CONST,
1172       { .i64 = 0 }, 0, 0, FLAGS, "tier" },
1173     { "high", NULL, 0, AV_OPT_TYPE_CONST,
1174       { .i64 = 1 }, 0, 0, FLAGS, "tier" },
1175
1176     { "level", "Set level (general_level_idc)",
1177       OFFSET(level), AV_OPT_TYPE_INT,
1178       { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
1179
1180 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1181       { .i64 = value }, 0, 0, FLAGS, "level"
1182     { LEVEL("1",    30) },
1183     { LEVEL("2",    60) },
1184     { LEVEL("2.1",  63) },
1185     { LEVEL("3",    90) },
1186     { LEVEL("3.1",  93) },
1187     { LEVEL("4",   120) },
1188     { LEVEL("4.1", 123) },
1189     { LEVEL("5",   150) },
1190     { LEVEL("5.1", 153) },
1191     { LEVEL("5.2", 156) },
1192     { LEVEL("6",   180) },
1193     { LEVEL("6.1", 183) },
1194     { LEVEL("6.2", 186) },
1195 #undef LEVEL
1196
1197     { "sei", "Set SEI to include",
1198       OFFSET(sei), AV_OPT_TYPE_FLAGS,
1199       { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
1200       0, INT_MAX, FLAGS, "sei" },
1201     { "hdr",
1202       "Include HDR metadata for mastering display colour volume "
1203       "and content light level information",
1204       0, AV_OPT_TYPE_CONST,
1205       { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
1206       INT_MIN, INT_MAX, FLAGS, "sei" },
1207
1208     { NULL },
1209 };
1210
1211 static const AVCodecDefault vaapi_encode_h265_defaults[] = {
1212     { "b",              "0"   },
1213     { "bf",             "2"   },
1214     { "g",              "120" },
1215     { "i_qfactor",      "1"   },
1216     { "i_qoffset",      "0"   },
1217     { "b_qfactor",      "6/5" },
1218     { "b_qoffset",      "0"   },
1219     { "qmin",           "-1"  },
1220     { "qmax",           "-1"  },
1221     { NULL },
1222 };
1223
1224 static const AVClass vaapi_encode_h265_class = {
1225     .class_name = "h265_vaapi",
1226     .item_name  = av_default_item_name,
1227     .option     = vaapi_encode_h265_options,
1228     .version    = LIBAVUTIL_VERSION_INT,
1229 };
1230
1231 AVCodec ff_hevc_vaapi_encoder = {
1232     .name           = "hevc_vaapi",
1233     .long_name      = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
1234     .type           = AVMEDIA_TYPE_VIDEO,
1235     .id             = AV_CODEC_ID_HEVC,
1236     .priv_data_size = sizeof(VAAPIEncodeH265Context),
1237     .init           = &vaapi_encode_h265_init,
1238     .encode2        = &ff_vaapi_encode2,
1239     .close          = &vaapi_encode_h265_close,
1240     .priv_class     = &vaapi_encode_h265_class,
1241     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
1242     .defaults       = vaapi_encode_h265_defaults,
1243     .pix_fmts = (const enum AVPixelFormat[]) {
1244         AV_PIX_FMT_VAAPI,
1245         AV_PIX_FMT_NONE,
1246     },
1247     .wrapper_name   = "vaapi",
1248 };