2 * This file is part of FFmpeg.
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.
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.
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
22 #include <va/va_enc_hevc.h>
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"
33 #include "h265_profile_level.h"
38 #include "vaapi_encode.h"
41 SEI_MASTERING_DISPLAY = 0x08,
42 SEI_CONTENT_LIGHT_LEVEL = 0x10,
45 typedef struct VAAPIEncodeH265Picture {
48 int64_t last_idr_frame;
53 } VAAPIEncodeH265Picture;
55 typedef struct VAAPIEncodeH265Context {
56 VAAPIEncodeContext common;
80 H265RawSlice raw_slice;
82 H265RawSEIMasteringDisplayColourVolume sei_mastering_display;
83 H265RawSEIContentLightLevelInfo sei_content_light_level;
85 CodedBitstreamContext *cbc;
86 CodedBitstreamFragment current_access_unit;
89 } VAAPIEncodeH265Context;
92 static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx,
93 char *data, size_t *data_len,
94 CodedBitstreamFragment *au)
96 VAAPIEncodeH265Context *priv = avctx->priv_data;
99 err = ff_cbs_write_fragment_data(priv->cbc, au);
101 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
105 if (*data_len < 8 * au->data_size - au->data_bit_padding) {
106 av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
107 "%zu < %zu.\n", *data_len,
108 8 * au->data_size - au->data_bit_padding);
109 return AVERROR(ENOSPC);
112 memcpy(data, au->data, au->data_size);
113 *data_len = 8 * au->data_size - au->data_bit_padding;
118 static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
119 CodedBitstreamFragment *au,
122 H265RawNALUnitHeader *header = nal_unit;
125 err = ff_cbs_insert_unit_content(au, -1,
126 header->nal_unit_type, nal_unit, NULL);
128 av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
129 "type = %d.\n", header->nal_unit_type);
136 static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
137 char *data, size_t *data_len)
139 VAAPIEncodeH265Context *priv = avctx->priv_data;
140 CodedBitstreamFragment *au = &priv->current_access_unit;
143 if (priv->aud_needed) {
144 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
147 priv->aud_needed = 0;
150 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_vps);
154 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_sps);
158 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_pps);
162 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
164 ff_cbs_fragment_reset(au);
168 static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
169 VAAPIEncodePicture *pic,
170 VAAPIEncodeSlice *slice,
171 char *data, size_t *data_len)
173 VAAPIEncodeH265Context *priv = avctx->priv_data;
174 CodedBitstreamFragment *au = &priv->current_access_unit;
177 if (priv->aud_needed) {
178 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
181 priv->aud_needed = 0;
184 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_slice);
188 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
190 ff_cbs_fragment_reset(au);
194 static int vaapi_encode_h265_write_extra_header(AVCodecContext *avctx,
195 VAAPIEncodePicture *pic,
196 int index, int *type,
197 char *data, size_t *data_len)
199 VAAPIEncodeH265Context *priv = avctx->priv_data;
200 CodedBitstreamFragment *au = &priv->current_access_unit;
203 if (priv->sei_needed) {
204 H265RawSEI *sei = &priv->raw_sei;
206 if (priv->aud_needed) {
207 err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
210 priv->aud_needed = 0;
213 *sei = (H265RawSEI) {
215 .nal_unit_type = HEVC_NAL_SEI_PREFIX,
217 .nuh_temporal_id_plus1 = 1,
223 if (priv->sei_needed & SEI_MASTERING_DISPLAY) {
224 sei->payload[i].payload_type = HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO;
225 sei->payload[i].payload.mastering_display = priv->sei_mastering_display;
229 if (priv->sei_needed & SEI_CONTENT_LIGHT_LEVEL) {
230 sei->payload[i].payload_type = HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO;
231 sei->payload[i].payload.content_light_level = priv->sei_content_light_level;
235 sei->payload_count = i;
236 av_assert0(sei->payload_count > 0);
238 err = vaapi_encode_h265_add_nal(avctx, au, sei);
241 priv->sei_needed = 0;
243 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
247 ff_cbs_fragment_reset(au);
249 *type = VAEncPackedHeaderRawData;
256 ff_cbs_fragment_reset(au);
260 static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
262 VAAPIEncodeContext *ctx = avctx->priv_data;
263 VAAPIEncodeH265Context *priv = avctx->priv_data;
264 H265RawVPS *vps = &priv->raw_vps;
265 H265RawSPS *sps = &priv->raw_sps;
266 H265RawPPS *pps = &priv->raw_pps;
267 H265RawProfileTierLevel *ptl = &vps->profile_tier_level;
268 H265RawVUI *vui = &sps->vui;
269 VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
270 VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
271 const AVPixFmtDescriptor *desc;
272 int chroma_format, bit_depth;
275 memset(vps, 0, sizeof(*vps));
276 memset(sps, 0, sizeof(*sps));
277 memset(pps, 0, sizeof(*pps));
280 desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format);
282 if (desc->nb_components == 1) {
285 if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1) {
287 } else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0) {
289 } else if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
292 av_log(avctx, AV_LOG_ERROR, "Chroma format of input pixel format "
293 "%s is not supported.\n", desc->name);
294 return AVERROR(EINVAL);
297 bit_depth = desc->comp[0].depth;
302 vps->nal_unit_header = (H265RawNALUnitHeader) {
303 .nal_unit_type = HEVC_NAL_VPS,
305 .nuh_temporal_id_plus1 = 1,
308 vps->vps_video_parameter_set_id = 0;
310 vps->vps_base_layer_internal_flag = 1;
311 vps->vps_base_layer_available_flag = 1;
312 vps->vps_max_layers_minus1 = 0;
313 vps->vps_max_sub_layers_minus1 = 0;
314 vps->vps_temporal_id_nesting_flag = 1;
316 ptl->general_profile_space = 0;
317 ptl->general_profile_idc = avctx->profile;
318 ptl->general_tier_flag = priv->tier;
320 if (chroma_format == 1) {
321 ptl->general_profile_compatibility_flag[1] = bit_depth == 8;
322 ptl->general_profile_compatibility_flag[2] = bit_depth <= 10;
324 ptl->general_profile_compatibility_flag[4] = 1;
326 ptl->general_progressive_source_flag = 1;
327 ptl->general_interlaced_source_flag = 0;
328 ptl->general_non_packed_constraint_flag = 1;
329 ptl->general_frame_only_constraint_flag = 1;
331 ptl->general_max_12bit_constraint_flag = bit_depth <= 12;
332 ptl->general_max_10bit_constraint_flag = bit_depth <= 10;
333 ptl->general_max_8bit_constraint_flag = bit_depth == 8;
335 ptl->general_max_422chroma_constraint_flag = chroma_format <= 2;
336 ptl->general_max_420chroma_constraint_flag = chroma_format <= 1;
337 ptl->general_max_monochrome_constraint_flag = chroma_format == 0;
339 ptl->general_intra_constraint_flag = ctx->gop_size == 1;
341 ptl->general_lower_bit_rate_constraint_flag = 1;
343 if (avctx->level != FF_LEVEL_UNKNOWN) {
344 ptl->general_level_idc = avctx->level;
346 const H265LevelDescriptor *level;
348 level = ff_h265_guess_level(ptl, avctx->bit_rate,
349 ctx->surface_width, ctx->surface_height,
350 ctx->nb_slices, ctx->tile_rows, ctx->tile_cols,
351 (ctx->b_per_p > 0) + 1);
353 av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
354 ptl->general_level_idc = level->level_idc;
356 av_log(avctx, AV_LOG_VERBOSE, "Stream will not conform to "
357 "any normal level; using level 8.5.\n");
358 ptl->general_level_idc = 255;
359 // The tier flag must be set in level 8.5.
360 ptl->general_tier_flag = 1;
364 vps->vps_sub_layer_ordering_info_present_flag = 0;
365 vps->vps_max_dec_pic_buffering_minus1[0] = ctx->max_b_depth + 1;
366 vps->vps_max_num_reorder_pics[0] = ctx->max_b_depth;
367 vps->vps_max_latency_increase_plus1[0] = 0;
369 vps->vps_max_layer_id = 0;
370 vps->vps_num_layer_sets_minus1 = 0;
371 vps->layer_id_included_flag[0][0] = 1;
373 vps->vps_timing_info_present_flag = 1;
374 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
375 vps->vps_num_units_in_tick = avctx->framerate.den;
376 vps->vps_time_scale = avctx->framerate.num;
377 vps->vps_poc_proportional_to_timing_flag = 1;
378 vps->vps_num_ticks_poc_diff_one_minus1 = 0;
380 vps->vps_num_units_in_tick = avctx->time_base.num;
381 vps->vps_time_scale = avctx->time_base.den;
382 vps->vps_poc_proportional_to_timing_flag = 0;
384 vps->vps_num_hrd_parameters = 0;
389 sps->nal_unit_header = (H265RawNALUnitHeader) {
390 .nal_unit_type = HEVC_NAL_SPS,
392 .nuh_temporal_id_plus1 = 1,
395 sps->sps_video_parameter_set_id = vps->vps_video_parameter_set_id;
397 sps->sps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1;
398 sps->sps_temporal_id_nesting_flag = vps->vps_temporal_id_nesting_flag;
400 sps->profile_tier_level = vps->profile_tier_level;
402 sps->sps_seq_parameter_set_id = 0;
404 sps->chroma_format_idc = chroma_format;
405 sps->separate_colour_plane_flag = 0;
407 sps->pic_width_in_luma_samples = ctx->surface_width;
408 sps->pic_height_in_luma_samples = ctx->surface_height;
410 if (avctx->width != ctx->surface_width ||
411 avctx->height != ctx->surface_height) {
412 sps->conformance_window_flag = 1;
413 sps->conf_win_left_offset = 0;
414 sps->conf_win_right_offset =
415 (ctx->surface_width - avctx->width) >> desc->log2_chroma_w;
416 sps->conf_win_top_offset = 0;
417 sps->conf_win_bottom_offset =
418 (ctx->surface_height - avctx->height) >> desc->log2_chroma_h;
420 sps->conformance_window_flag = 0;
423 sps->bit_depth_luma_minus8 = bit_depth - 8;
424 sps->bit_depth_chroma_minus8 = bit_depth - 8;
426 sps->log2_max_pic_order_cnt_lsb_minus4 = 8;
428 sps->sps_sub_layer_ordering_info_present_flag =
429 vps->vps_sub_layer_ordering_info_present_flag;
430 for (i = 0; i <= sps->sps_max_sub_layers_minus1; i++) {
431 sps->sps_max_dec_pic_buffering_minus1[i] =
432 vps->vps_max_dec_pic_buffering_minus1[i];
433 sps->sps_max_num_reorder_pics[i] =
434 vps->vps_max_num_reorder_pics[i];
435 sps->sps_max_latency_increase_plus1[i] =
436 vps->vps_max_latency_increase_plus1[i];
439 // These have to come from the capabilities of the encoder. We have no
440 // way to query them, so just hardcode parameters which work on the Intel
442 // CTB size from 8x8 to 32x32.
443 sps->log2_min_luma_coding_block_size_minus3 = 0;
444 sps->log2_diff_max_min_luma_coding_block_size = 2;
445 // Transform size from 4x4 to 32x32.
446 sps->log2_min_luma_transform_block_size_minus2 = 0;
447 sps->log2_diff_max_min_luma_transform_block_size = 3;
448 // Full transform hierarchy allowed (2-5).
449 sps->max_transform_hierarchy_depth_inter = 3;
450 sps->max_transform_hierarchy_depth_intra = 3;
452 sps->amp_enabled_flag = 1;
453 // SAO and temporal MVP do not work.
454 sps->sample_adaptive_offset_enabled_flag = 0;
455 sps->sps_temporal_mvp_enabled_flag = 0;
457 sps->pcm_enabled_flag = 0;
459 // STRPSs should ideally be here rather than defined individually in
460 // each slice, but the structure isn't completely fixed so for now
462 sps->num_short_term_ref_pic_sets = 0;
463 sps->long_term_ref_pics_present_flag = 0;
465 sps->vui_parameters_present_flag = 1;
467 if (avctx->sample_aspect_ratio.num != 0 &&
468 avctx->sample_aspect_ratio.den != 0) {
469 static const AVRational sar_idc[] = {
471 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
472 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
473 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
474 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
477 av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
478 avctx->sample_aspect_ratio.den, 65535);
479 for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
480 if (num == sar_idc[i].num &&
481 den == sar_idc[i].den) {
482 vui->aspect_ratio_idc = i;
486 if (i >= FF_ARRAY_ELEMS(sar_idc)) {
487 vui->aspect_ratio_idc = 255;
488 vui->sar_width = num;
489 vui->sar_height = den;
491 vui->aspect_ratio_info_present_flag = 1;
494 if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
495 avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
496 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
497 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
498 vui->video_signal_type_present_flag = 1;
499 vui->video_format = 5; // Unspecified.
500 vui->video_full_range_flag =
501 avctx->color_range == AVCOL_RANGE_JPEG;
503 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
504 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
505 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
506 vui->colour_description_present_flag = 1;
507 vui->colour_primaries = avctx->color_primaries;
508 vui->transfer_characteristics = avctx->color_trc;
509 vui->matrix_coefficients = avctx->colorspace;
512 vui->video_format = 5;
513 vui->video_full_range_flag = 0;
514 vui->colour_primaries = avctx->color_primaries;
515 vui->transfer_characteristics = avctx->color_trc;
516 vui->matrix_coefficients = avctx->colorspace;
519 if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
520 vui->chroma_loc_info_present_flag = 1;
521 vui->chroma_sample_loc_type_top_field =
522 vui->chroma_sample_loc_type_bottom_field =
523 avctx->chroma_sample_location - 1;
526 vui->vui_timing_info_present_flag = 1;
527 vui->vui_num_units_in_tick = vps->vps_num_units_in_tick;
528 vui->vui_time_scale = vps->vps_time_scale;
529 vui->vui_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag;
530 vui->vui_num_ticks_poc_diff_one_minus1 = vps->vps_num_ticks_poc_diff_one_minus1;
531 vui->vui_hrd_parameters_present_flag = 0;
533 vui->bitstream_restriction_flag = 1;
534 vui->motion_vectors_over_pic_boundaries_flag = 1;
535 vui->restricted_ref_pic_lists_flag = 1;
536 vui->max_bytes_per_pic_denom = 0;
537 vui->max_bits_per_min_cu_denom = 0;
538 vui->log2_max_mv_length_horizontal = 15;
539 vui->log2_max_mv_length_vertical = 15;
544 pps->nal_unit_header = (H265RawNALUnitHeader) {
545 .nal_unit_type = HEVC_NAL_PPS,
547 .nuh_temporal_id_plus1 = 1,
550 pps->pps_pic_parameter_set_id = 0;
551 pps->pps_seq_parameter_set_id = sps->sps_seq_parameter_set_id;
553 pps->num_ref_idx_l0_default_active_minus1 = 0;
554 pps->num_ref_idx_l1_default_active_minus1 = 0;
556 pps->init_qp_minus26 = priv->fixed_qp_idr - 26;
558 pps->cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP);
559 pps->diff_cu_qp_delta_depth = 0;
561 pps->pps_loop_filter_across_slices_enabled_flag = 1;
563 if (ctx->tile_rows && ctx->tile_cols) {
564 pps->tiles_enabled_flag = 1;
565 pps->uniform_spacing_flag = 1;
567 pps->num_tile_rows_minus1 = ctx->tile_rows - 1;
568 pps->num_tile_columns_minus1 = ctx->tile_cols - 1;
570 pps->loop_filter_across_tiles_enabled_flag = 1;
572 for (i = 0; i <= pps->num_tile_rows_minus1; i++)
573 pps->row_height_minus1[i] = ctx->row_height[i] - 1;
574 for (i = 0; i <= pps->num_tile_columns_minus1; i++)
575 pps->column_width_minus1[i] = ctx->col_width[i] - 1;
578 // Fill VAAPI parameter buffers.
580 *vseq = (VAEncSequenceParameterBufferHEVC) {
581 .general_profile_idc = vps->profile_tier_level.general_profile_idc,
582 .general_level_idc = vps->profile_tier_level.general_level_idc,
583 .general_tier_flag = vps->profile_tier_level.general_tier_flag,
585 .intra_period = ctx->gop_size,
586 .intra_idr_period = ctx->gop_size,
587 .ip_period = ctx->b_per_p + 1,
588 .bits_per_second = ctx->va_bit_rate,
590 .pic_width_in_luma_samples = sps->pic_width_in_luma_samples,
591 .pic_height_in_luma_samples = sps->pic_height_in_luma_samples,
594 .chroma_format_idc = sps->chroma_format_idc,
595 .separate_colour_plane_flag = sps->separate_colour_plane_flag,
596 .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
597 .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
598 .scaling_list_enabled_flag = sps->scaling_list_enabled_flag,
599 .strong_intra_smoothing_enabled_flag =
600 sps->strong_intra_smoothing_enabled_flag,
601 .amp_enabled_flag = sps->amp_enabled_flag,
602 .sample_adaptive_offset_enabled_flag =
603 sps->sample_adaptive_offset_enabled_flag,
604 .pcm_enabled_flag = sps->pcm_enabled_flag,
605 .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag,
606 .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
609 .log2_min_luma_coding_block_size_minus3 =
610 sps->log2_min_luma_coding_block_size_minus3,
611 .log2_diff_max_min_luma_coding_block_size =
612 sps->log2_diff_max_min_luma_coding_block_size,
613 .log2_min_transform_block_size_minus2 =
614 sps->log2_min_luma_transform_block_size_minus2,
615 .log2_diff_max_min_transform_block_size =
616 sps->log2_diff_max_min_luma_transform_block_size,
617 .max_transform_hierarchy_depth_inter =
618 sps->max_transform_hierarchy_depth_inter,
619 .max_transform_hierarchy_depth_intra =
620 sps->max_transform_hierarchy_depth_intra,
622 .pcm_sample_bit_depth_luma_minus1 =
623 sps->pcm_sample_bit_depth_luma_minus1,
624 .pcm_sample_bit_depth_chroma_minus1 =
625 sps->pcm_sample_bit_depth_chroma_minus1,
626 .log2_min_pcm_luma_coding_block_size_minus3 =
627 sps->log2_min_pcm_luma_coding_block_size_minus3,
628 .log2_max_pcm_luma_coding_block_size_minus3 =
629 sps->log2_min_pcm_luma_coding_block_size_minus3 +
630 sps->log2_diff_max_min_pcm_luma_coding_block_size,
632 .vui_parameters_present_flag = 0,
635 *vpic = (VAEncPictureParameterBufferHEVC) {
636 .decoded_curr_pic = {
637 .picture_id = VA_INVALID_ID,
638 .flags = VA_PICTURE_HEVC_INVALID,
641 .coded_buf = VA_INVALID_ID,
643 .collocated_ref_pic_index = 0xff,
647 .pic_init_qp = pps->init_qp_minus26 + 26,
648 .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
649 .pps_cb_qp_offset = pps->pps_cb_qp_offset,
650 .pps_cr_qp_offset = pps->pps_cr_qp_offset,
652 .num_tile_columns_minus1 = pps->num_tile_columns_minus1,
653 .num_tile_rows_minus1 = pps->num_tile_rows_minus1,
655 .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2,
656 .ctu_max_bitsize_allowed = 0,
658 .num_ref_idx_l0_default_active_minus1 =
659 pps->num_ref_idx_l0_default_active_minus1,
660 .num_ref_idx_l1_default_active_minus1 =
661 pps->num_ref_idx_l1_default_active_minus1,
663 .slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id,
666 .sign_data_hiding_enabled_flag = pps->sign_data_hiding_enabled_flag,
667 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
668 .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
669 .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
670 .weighted_pred_flag = pps->weighted_pred_flag,
671 .weighted_bipred_flag = pps->weighted_bipred_flag,
672 .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag,
673 .tiles_enabled_flag = pps->tiles_enabled_flag,
674 .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
675 .loop_filter_across_tiles_enabled_flag =
676 pps->loop_filter_across_tiles_enabled_flag,
677 .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag |
678 pps->pps_scaling_list_data_present_flag),
679 .screen_content_flag = 0,
680 .enable_gpu_weighted_prediction = 0,
681 .no_output_of_prior_pics_flag = 0,
685 if (pps->tiles_enabled_flag) {
686 for (i = 0; i <= vpic->num_tile_rows_minus1; i++)
687 vpic->row_height_minus1[i] = pps->row_height_minus1[i];
688 for (i = 0; i <= vpic->num_tile_columns_minus1; i++)
689 vpic->column_width_minus1[i] = pps->column_width_minus1[i];
695 static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
696 VAAPIEncodePicture *pic)
698 VAAPIEncodeContext *ctx = avctx->priv_data;
699 VAAPIEncodeH265Context *priv = avctx->priv_data;
700 VAAPIEncodeH265Picture *hpic = pic->priv_data;
701 VAAPIEncodePicture *prev = pic->prev;
702 VAAPIEncodeH265Picture *hprev = prev ? prev->priv_data : NULL;
703 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
706 if (pic->type == PICTURE_TYPE_IDR) {
707 av_assert0(pic->display_order == pic->encode_order);
709 hpic->last_idr_frame = pic->display_order;
711 hpic->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
712 hpic->slice_type = HEVC_SLICE_I;
716 hpic->last_idr_frame = hprev->last_idr_frame;
718 if (pic->type == PICTURE_TYPE_I) {
719 hpic->slice_nal_unit = HEVC_NAL_CRA_NUT;
720 hpic->slice_type = HEVC_SLICE_I;
722 } else if (pic->type == PICTURE_TYPE_P) {
723 av_assert0(pic->refs[0]);
724 hpic->slice_nal_unit = HEVC_NAL_TRAIL_R;
725 hpic->slice_type = HEVC_SLICE_P;
728 VAAPIEncodePicture *irap_ref;
729 av_assert0(pic->refs[0] && pic->refs[1]);
730 for (irap_ref = pic; irap_ref; irap_ref = irap_ref->refs[1]) {
731 if (irap_ref->type == PICTURE_TYPE_I)
734 if (pic->b_depth == ctx->max_b_depth) {
735 hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_N
738 hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_R
741 hpic->slice_type = HEVC_SLICE_B;
745 hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame;
748 priv->aud_needed = 1;
749 priv->raw_aud = (H265RawAUD) {
751 .nal_unit_type = HEVC_NAL_AUD,
753 .nuh_temporal_id_plus1 = 1,
755 .pic_type = hpic->pic_type,
758 priv->aud_needed = 0;
761 priv->sei_needed = 0;
763 // Only look for the metadata on I/IDR frame on the output. We
764 // may force an IDR frame on the output where the medadata gets
765 // changed on the input frame.
766 if ((priv->sei & SEI_MASTERING_DISPLAY) &&
767 (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
768 AVFrameSideData *sd =
769 av_frame_get_side_data(pic->input_image,
770 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
773 AVMasteringDisplayMetadata *mdm =
774 (AVMasteringDisplayMetadata *)sd->data;
776 // SEI is needed when both the primaries and luminance are set
777 if (mdm->has_primaries && mdm->has_luminance) {
778 H265RawSEIMasteringDisplayColourVolume *mdcv =
779 &priv->sei_mastering_display;
780 const int mapping[3] = {1, 2, 0};
781 const int chroma_den = 50000;
782 const int luma_den = 10000;
784 for (i = 0; i < 3; i++) {
785 const int j = mapping[i];
786 mdcv->display_primaries_x[i] =
787 FFMIN(lrint(chroma_den *
788 av_q2d(mdm->display_primaries[j][0])),
790 mdcv->display_primaries_y[i] =
791 FFMIN(lrint(chroma_den *
792 av_q2d(mdm->display_primaries[j][1])),
796 mdcv->white_point_x =
797 FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
799 mdcv->white_point_y =
800 FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
803 mdcv->max_display_mastering_luminance =
804 lrint(luma_den * av_q2d(mdm->max_luminance));
805 mdcv->min_display_mastering_luminance =
806 FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
807 mdcv->max_display_mastering_luminance);
809 priv->sei_needed |= SEI_MASTERING_DISPLAY;
814 if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
815 (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
816 AVFrameSideData *sd =
817 av_frame_get_side_data(pic->input_image,
818 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
821 AVContentLightMetadata *clm =
822 (AVContentLightMetadata *)sd->data;
823 H265RawSEIContentLightLevelInfo *clli =
824 &priv->sei_content_light_level;
826 clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535);
827 clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
829 priv->sei_needed |= SEI_CONTENT_LIGHT_LEVEL;
833 vpic->decoded_curr_pic = (VAPictureHEVC) {
834 .picture_id = pic->recon_surface,
835 .pic_order_cnt = hpic->pic_order_cnt,
839 for (i = 0; i < pic->nb_refs; i++) {
840 VAAPIEncodePicture *ref = pic->refs[i];
841 VAAPIEncodeH265Picture *href;
843 av_assert0(ref && ref->encode_order < pic->encode_order);
844 href = ref->priv_data;
846 vpic->reference_frames[i] = (VAPictureHEVC) {
847 .picture_id = ref->recon_surface,
848 .pic_order_cnt = href->pic_order_cnt,
849 .flags = (ref->display_order < pic->display_order ?
850 VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
851 (ref->display_order > pic->display_order ?
852 VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
855 for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
856 vpic->reference_frames[i] = (VAPictureHEVC) {
857 .picture_id = VA_INVALID_ID,
858 .flags = VA_PICTURE_HEVC_INVALID,
862 vpic->coded_buf = pic->output_buffer;
864 vpic->nal_unit_type = hpic->slice_nal_unit;
867 case PICTURE_TYPE_IDR:
868 vpic->pic_fields.bits.idr_pic_flag = 1;
869 vpic->pic_fields.bits.coding_type = 1;
870 vpic->pic_fields.bits.reference_pic_flag = 1;
873 vpic->pic_fields.bits.idr_pic_flag = 0;
874 vpic->pic_fields.bits.coding_type = 1;
875 vpic->pic_fields.bits.reference_pic_flag = 1;
878 vpic->pic_fields.bits.idr_pic_flag = 0;
879 vpic->pic_fields.bits.coding_type = 2;
880 vpic->pic_fields.bits.reference_pic_flag = 1;
883 vpic->pic_fields.bits.idr_pic_flag = 0;
884 vpic->pic_fields.bits.coding_type = 3;
885 vpic->pic_fields.bits.reference_pic_flag = 0;
888 av_assert0(0 && "invalid picture type");
894 static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
895 VAAPIEncodePicture *pic,
896 VAAPIEncodeSlice *slice)
898 VAAPIEncodeH265Context *priv = avctx->priv_data;
899 VAAPIEncodeH265Picture *hpic = pic->priv_data;
900 const H265RawSPS *sps = &priv->raw_sps;
901 const H265RawPPS *pps = &priv->raw_pps;
902 H265RawSliceHeader *sh = &priv->raw_slice.header;
903 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
904 VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
907 sh->nal_unit_header = (H265RawNALUnitHeader) {
908 .nal_unit_type = hpic->slice_nal_unit,
910 .nuh_temporal_id_plus1 = 1,
913 sh->slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id;
915 sh->first_slice_segment_in_pic_flag = slice->index == 0;
916 sh->slice_segment_address = slice->block_start;
918 sh->slice_type = hpic->slice_type;
920 sh->slice_pic_order_cnt_lsb = hpic->pic_order_cnt &
921 (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
923 if (pic->type != PICTURE_TYPE_IDR) {
924 H265RawSTRefPicSet *rps;
925 const VAAPIEncodeH265Picture *strp;
926 int rps_poc[MAX_DPB_SIZE];
927 int rps_used[MAX_DPB_SIZE];
928 int i, j, poc, rps_pics;
930 sh->short_term_ref_pic_set_sps_flag = 0;
932 rps = &sh->short_term_ref_pic_set;
933 memset(rps, 0, sizeof(*rps));
936 for (i = 0; i < pic->nb_refs; i++) {
937 strp = pic->refs[i]->priv_data;
938 rps_poc[rps_pics] = strp->pic_order_cnt;
939 rps_used[rps_pics] = 1;
942 for (i = 0; i < pic->nb_dpb_pics; i++) {
943 if (pic->dpb[i] == pic)
945 for (j = 0; j < pic->nb_refs; j++) {
946 if (pic->dpb[i] == pic->refs[j])
949 if (j < pic->nb_refs)
951 strp = pic->dpb[i]->priv_data;
952 rps_poc[rps_pics] = strp->pic_order_cnt;
953 rps_used[rps_pics] = 0;
957 for (i = 1; i < rps_pics; i++) {
958 for (j = i; j > 0; j--) {
959 if (rps_poc[j] > rps_poc[j - 1])
961 av_assert0(rps_poc[j] != rps_poc[j - 1]);
962 FFSWAP(int, rps_poc[j], rps_poc[j - 1]);
963 FFSWAP(int, rps_used[j], rps_used[j - 1]);
967 av_log(avctx, AV_LOG_DEBUG, "RPS for POC %d:",
968 hpic->pic_order_cnt);
969 for (i = 0; i < rps_pics; i++) {
970 av_log(avctx, AV_LOG_DEBUG, " (%d,%d)",
971 rps_poc[i], rps_used[i]);
973 av_log(avctx, AV_LOG_DEBUG, "\n");
975 for (i = 0; i < rps_pics; i++) {
976 av_assert0(rps_poc[i] != hpic->pic_order_cnt);
977 if (rps_poc[i] > hpic->pic_order_cnt)
981 rps->num_negative_pics = i;
982 poc = hpic->pic_order_cnt;
983 for (j = i - 1; j >= 0; j--) {
984 rps->delta_poc_s0_minus1[i - 1 - j] = poc - rps_poc[j] - 1;
985 rps->used_by_curr_pic_s0_flag[i - 1 - j] = rps_used[j];
989 rps->num_positive_pics = rps_pics - i;
990 poc = hpic->pic_order_cnt;
991 for (j = i; j < rps_pics; j++) {
992 rps->delta_poc_s1_minus1[j - i] = rps_poc[j] - poc - 1;
993 rps->used_by_curr_pic_s1_flag[j - i] = rps_used[j];
997 sh->num_long_term_sps = 0;
998 sh->num_long_term_pics = 0;
1000 sh->slice_temporal_mvp_enabled_flag =
1001 sps->sps_temporal_mvp_enabled_flag;
1002 if (sh->slice_temporal_mvp_enabled_flag) {
1003 sh->collocated_from_l0_flag = sh->slice_type == HEVC_SLICE_B;
1004 sh->collocated_ref_idx = 0;
1007 sh->num_ref_idx_active_override_flag = 0;
1008 sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
1009 sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
1012 sh->slice_sao_luma_flag = sh->slice_sao_chroma_flag =
1013 sps->sample_adaptive_offset_enabled_flag;
1015 if (pic->type == PICTURE_TYPE_B)
1016 sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
1017 else if (pic->type == PICTURE_TYPE_P)
1018 sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
1020 sh->slice_qp_delta = priv->fixed_qp_idr - (pps->init_qp_minus26 + 26);
1023 *vslice = (VAEncSliceParameterBufferHEVC) {
1024 .slice_segment_address = sh->slice_segment_address,
1025 .num_ctu_in_slice = slice->block_size,
1027 .slice_type = sh->slice_type,
1028 .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
1030 .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
1031 .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
1033 .luma_log2_weight_denom = sh->luma_log2_weight_denom,
1034 .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
1036 .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
1038 .slice_qp_delta = sh->slice_qp_delta,
1039 .slice_cb_qp_offset = sh->slice_cb_qp_offset,
1040 .slice_cr_qp_offset = sh->slice_cr_qp_offset,
1042 .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
1043 .slice_tc_offset_div2 = sh->slice_tc_offset_div2,
1045 .slice_fields.bits = {
1046 .last_slice_of_pic_flag = slice->index == pic->nb_slices - 1,
1047 .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
1048 .colour_plane_id = sh->colour_plane_id,
1049 .slice_temporal_mvp_enabled_flag =
1050 sh->slice_temporal_mvp_enabled_flag,
1051 .slice_sao_luma_flag = sh->slice_sao_luma_flag,
1052 .slice_sao_chroma_flag = sh->slice_sao_chroma_flag,
1053 .num_ref_idx_active_override_flag =
1054 sh->num_ref_idx_active_override_flag,
1055 .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
1056 .cabac_init_flag = sh->cabac_init_flag,
1057 .slice_deblocking_filter_disabled_flag =
1058 sh->slice_deblocking_filter_disabled_flag,
1059 .slice_loop_filter_across_slices_enabled_flag =
1060 sh->slice_loop_filter_across_slices_enabled_flag,
1061 .collocated_from_l0_flag = sh->collocated_from_l0_flag,
1065 for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
1066 vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
1067 vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
1068 vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
1069 vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
1072 av_assert0(pic->nb_refs <= 2);
1073 if (pic->nb_refs >= 1) {
1074 // Backward reference for P- or B-frame.
1075 av_assert0(pic->type == PICTURE_TYPE_P ||
1076 pic->type == PICTURE_TYPE_B);
1077 vslice->ref_pic_list0[0] = vpic->reference_frames[0];
1079 if (pic->nb_refs >= 2) {
1080 // Forward reference for B-frame.
1081 av_assert0(pic->type == PICTURE_TYPE_B);
1082 vslice->ref_pic_list1[0] = vpic->reference_frames[1];
1088 static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
1090 VAAPIEncodeContext *ctx = avctx->priv_data;
1091 VAAPIEncodeH265Context *priv = avctx->priv_data;
1094 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
1098 if (ctx->va_rc_mode == VA_RC_CQP) {
1099 // Note that VAAPI only supports positive QP values - the range is
1100 // therefore always bounded below by 1, even in 10-bit mode where
1101 // it should go down to -12.
1103 priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
1104 if (avctx->i_quant_factor > 0.0)
1105 priv->fixed_qp_idr =
1106 av_clip((avctx->i_quant_factor * priv->fixed_qp_p +
1107 avctx->i_quant_offset) + 0.5, 1, 51);
1109 priv->fixed_qp_idr = priv->fixed_qp_p;
1110 if (avctx->b_quant_factor > 0.0)
1112 av_clip((avctx->b_quant_factor * priv->fixed_qp_p +
1113 avctx->b_quant_offset) + 0.5, 1, 51);
1115 priv->fixed_qp_b = priv->fixed_qp_p;
1117 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1118 "%d / %d / %d for IDR- / P- / B-frames.\n",
1119 priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1122 // These still need to be set for init_qp/slice_qp_delta.
1123 priv->fixed_qp_idr = 30;
1124 priv->fixed_qp_p = 30;
1125 priv->fixed_qp_b = 30;
1128 ctx->roi_quant_range = 51 + 6 * (ctx->profile->depth - 8);
1133 static const VAAPIEncodeProfile vaapi_encode_h265_profiles[] = {
1134 { FF_PROFILE_HEVC_MAIN, 8, 3, 1, 1, VAProfileHEVCMain },
1135 { FF_PROFILE_HEVC_REXT, 8, 3, 1, 1, VAProfileHEVCMain },
1136 #if VA_CHECK_VERSION(0, 37, 0)
1137 { FF_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10 },
1138 { FF_PROFILE_HEVC_REXT, 10, 3, 1, 1, VAProfileHEVCMain10 },
1140 #if VA_CHECK_VERSION(1, 2, 0)
1141 { FF_PROFILE_HEVC_REXT, 8, 3, 1, 0, VAProfileHEVCMain422_10 },
1142 { FF_PROFILE_HEVC_REXT, 10, 3, 1, 0, VAProfileHEVCMain422_10 },
1144 { FF_PROFILE_UNKNOWN }
1147 static const VAAPIEncodeType vaapi_encode_type_h265 = {
1148 .profiles = vaapi_encode_h265_profiles,
1150 .flags = FLAG_SLICE_CONTROL |
1152 FLAG_B_PICTURE_REFERENCES |
1153 FLAG_NON_IDR_KEY_PICTURES,
1155 .default_quality = 25,
1157 .configure = &vaapi_encode_h265_configure,
1159 .picture_priv_data_size = sizeof(VAAPIEncodeH265Picture),
1161 .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
1162 .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
1164 .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
1165 .init_picture_params = &vaapi_encode_h265_init_picture_params,
1167 .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
1168 .init_slice_params = &vaapi_encode_h265_init_slice_params,
1170 .sequence_header_type = VAEncPackedHeaderSequence,
1171 .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
1173 .slice_header_type = VAEncPackedHeaderHEVC_Slice,
1174 .write_slice_header = &vaapi_encode_h265_write_slice_header,
1176 .write_extra_header = &vaapi_encode_h265_write_extra_header,
1179 static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
1181 VAAPIEncodeContext *ctx = avctx->priv_data;
1182 VAAPIEncodeH265Context *priv = avctx->priv_data;
1184 ctx->codec = &vaapi_encode_type_h265;
1186 if (avctx->profile == FF_PROFILE_UNKNOWN)
1187 avctx->profile = priv->profile;
1188 if (avctx->level == FF_LEVEL_UNKNOWN)
1189 avctx->level = priv->level;
1191 if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
1192 av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
1193 "in 8-bit unsigned integer.\n", avctx->level);
1194 return AVERROR(EINVAL);
1197 ctx->desired_packed_headers =
1198 VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
1199 VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
1200 VA_ENC_PACKED_HEADER_MISC; // SEI
1202 ctx->surface_width = FFALIGN(avctx->width, 16);
1203 ctx->surface_height = FFALIGN(avctx->height, 16);
1205 // CTU size is currently hard-coded to 32.
1206 ctx->slice_block_width = ctx->slice_block_height = 32;
1209 ctx->explicit_qp = priv->qp;
1211 if (priv->trows && priv->tcols) {
1212 ctx->tile_rows = priv->trows;
1213 ctx->tile_cols = priv->tcols;
1216 return ff_vaapi_encode_init(avctx);
1219 static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
1221 VAAPIEncodeH265Context *priv = avctx->priv_data;
1223 ff_cbs_fragment_free(&priv->current_access_unit);
1224 ff_cbs_close(&priv->cbc);
1226 return ff_vaapi_encode_close(avctx);
1229 #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
1230 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1231 static const AVOption vaapi_encode_h265_options[] = {
1232 VAAPI_ENCODE_COMMON_OPTIONS,
1233 VAAPI_ENCODE_RC_OPTIONS,
1235 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1236 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
1238 { "aud", "Include AUD",
1239 OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1241 { "profile", "Set profile (general_profile_idc)",
1242 OFFSET(profile), AV_OPT_TYPE_INT,
1243 { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
1245 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1246 { .i64 = value }, 0, 0, FLAGS, "profile"
1247 { PROFILE("main", FF_PROFILE_HEVC_MAIN) },
1248 { PROFILE("main10", FF_PROFILE_HEVC_MAIN_10) },
1249 { PROFILE("rext", FF_PROFILE_HEVC_REXT) },
1252 { "tier", "Set tier (general_tier_flag)",
1253 OFFSET(tier), AV_OPT_TYPE_INT,
1254 { .i64 = 0 }, 0, 1, FLAGS, "tier" },
1255 { "main", NULL, 0, AV_OPT_TYPE_CONST,
1256 { .i64 = 0 }, 0, 0, FLAGS, "tier" },
1257 { "high", NULL, 0, AV_OPT_TYPE_CONST,
1258 { .i64 = 1 }, 0, 0, FLAGS, "tier" },
1260 { "level", "Set level (general_level_idc)",
1261 OFFSET(level), AV_OPT_TYPE_INT,
1262 { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
1264 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1265 { .i64 = value }, 0, 0, FLAGS, "level"
1268 { LEVEL("2.1", 63) },
1270 { LEVEL("3.1", 93) },
1271 { LEVEL("4", 120) },
1272 { LEVEL("4.1", 123) },
1273 { LEVEL("5", 150) },
1274 { LEVEL("5.1", 153) },
1275 { LEVEL("5.2", 156) },
1276 { LEVEL("6", 180) },
1277 { LEVEL("6.1", 183) },
1278 { LEVEL("6.2", 186) },
1281 { "sei", "Set SEI to include",
1282 OFFSET(sei), AV_OPT_TYPE_FLAGS,
1283 { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
1284 0, INT_MAX, FLAGS, "sei" },
1286 "Include HDR metadata for mastering display colour volume "
1287 "and content light level information",
1288 0, AV_OPT_TYPE_CONST,
1289 { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
1290 INT_MIN, INT_MAX, FLAGS, "sei" },
1292 { "tiles", "Tile rows x cols",
1293 OFFSET(trows), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, FLAGS },
1298 static const AVCodecDefault vaapi_encode_h265_defaults[] = {
1302 { "i_qfactor", "1" },
1303 { "i_qoffset", "0" },
1304 { "b_qfactor", "6/5" },
1305 { "b_qoffset", "0" },
1311 static const AVClass vaapi_encode_h265_class = {
1312 .class_name = "h265_vaapi",
1313 .item_name = av_default_item_name,
1314 .option = vaapi_encode_h265_options,
1315 .version = LIBAVUTIL_VERSION_INT,
1318 AVCodec ff_hevc_vaapi_encoder = {
1319 .name = "hevc_vaapi",
1320 .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
1321 .type = AVMEDIA_TYPE_VIDEO,
1322 .id = AV_CODEC_ID_HEVC,
1323 .priv_data_size = sizeof(VAAPIEncodeH265Context),
1324 .init = &vaapi_encode_h265_init,
1325 .receive_packet = &ff_vaapi_encode_receive_packet,
1326 .close = &vaapi_encode_h265_close,
1327 .priv_class = &vaapi_encode_h265_class,
1328 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
1329 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1330 .defaults = vaapi_encode_h265_defaults,
1331 .pix_fmts = (const enum AVPixelFormat[]) {
1335 .hw_configs = ff_vaapi_encode_hw_configs,
1336 .wrapper_name = "vaapi",