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