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;
77 H265RawSlice raw_slice;
79 H265RawSEIMasteringDisplayColourVolume sei_mastering_display;
80 H265RawSEIContentLightLevelInfo sei_content_light_level;
82 CodedBitstreamContext *cbc;
83 CodedBitstreamFragment current_access_unit;
86 } VAAPIEncodeH265Context;
89 static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx,
90 char *data, size_t *data_len,
91 CodedBitstreamFragment *au)
93 VAAPIEncodeH265Context *priv = avctx->priv_data;
96 err = ff_cbs_write_fragment_data(priv->cbc, au);
98 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
102 if (*data_len < 8 * au->data_size - au->data_bit_padding) {
103 av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
104 "%zu < %zu.\n", *data_len,
105 8 * au->data_size - au->data_bit_padding);
106 return AVERROR(ENOSPC);
109 memcpy(data, au->data, au->data_size);
110 *data_len = 8 * au->data_size - au->data_bit_padding;
115 static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
116 CodedBitstreamFragment *au,
119 H265RawNALUnitHeader *header = nal_unit;
122 err = ff_cbs_insert_unit_content(au, -1,
123 header->nal_unit_type, nal_unit, NULL);
125 av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
126 "type = %d.\n", header->nal_unit_type);
133 static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
134 char *data, size_t *data_len)
136 VAAPIEncodeH265Context *priv = avctx->priv_data;
137 CodedBitstreamFragment *au = &priv->current_access_unit;
140 if (priv->aud_needed) {
141 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
144 priv->aud_needed = 0;
147 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_vps);
151 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_sps);
155 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_pps);
159 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
161 ff_cbs_fragment_reset(au);
165 static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
166 VAAPIEncodePicture *pic,
167 VAAPIEncodeSlice *slice,
168 char *data, size_t *data_len)
170 VAAPIEncodeH265Context *priv = avctx->priv_data;
171 CodedBitstreamFragment *au = &priv->current_access_unit;
174 if (priv->aud_needed) {
175 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
178 priv->aud_needed = 0;
181 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_slice);
185 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
187 ff_cbs_fragment_reset(au);
191 static int vaapi_encode_h265_write_extra_header(AVCodecContext *avctx,
192 VAAPIEncodePicture *pic,
193 int index, int *type,
194 char *data, size_t *data_len)
196 VAAPIEncodeH265Context *priv = avctx->priv_data;
197 CodedBitstreamFragment *au = &priv->current_access_unit;
200 if (priv->sei_needed) {
201 H265RawSEI *sei = &priv->raw_sei;
203 if (priv->aud_needed) {
204 err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
207 priv->aud_needed = 0;
210 *sei = (H265RawSEI) {
212 .nal_unit_type = HEVC_NAL_SEI_PREFIX,
214 .nuh_temporal_id_plus1 = 1,
220 if (priv->sei_needed & SEI_MASTERING_DISPLAY) {
221 sei->payload[i].payload_type = HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO;
222 sei->payload[i].payload.mastering_display = priv->sei_mastering_display;
226 if (priv->sei_needed & SEI_CONTENT_LIGHT_LEVEL) {
227 sei->payload[i].payload_type = HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO;
228 sei->payload[i].payload.content_light_level = priv->sei_content_light_level;
232 sei->payload_count = i;
233 av_assert0(sei->payload_count > 0);
235 err = vaapi_encode_h265_add_nal(avctx, au, sei);
238 priv->sei_needed = 0;
240 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
244 ff_cbs_fragment_reset(au);
246 *type = VAEncPackedHeaderRawData;
253 ff_cbs_fragment_reset(au);
257 static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
259 VAAPIEncodeContext *ctx = avctx->priv_data;
260 VAAPIEncodeH265Context *priv = avctx->priv_data;
261 H265RawVPS *vps = &priv->raw_vps;
262 H265RawSPS *sps = &priv->raw_sps;
263 H265RawPPS *pps = &priv->raw_pps;
264 H265RawProfileTierLevel *ptl = &vps->profile_tier_level;
265 H265RawVUI *vui = &sps->vui;
266 VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
267 VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
268 const AVPixFmtDescriptor *desc;
269 int chroma_format, bit_depth;
272 memset(vps, 0, sizeof(*vps));
273 memset(sps, 0, sizeof(*sps));
274 memset(pps, 0, sizeof(*pps));
277 desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format);
279 if (desc->nb_components == 1) {
282 if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1) {
284 } else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0) {
286 } else if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
289 av_log(avctx, AV_LOG_ERROR, "Chroma format of input pixel format "
290 "%s is not supported.\n", desc->name);
291 return AVERROR(EINVAL);
294 bit_depth = desc->comp[0].depth;
299 vps->nal_unit_header = (H265RawNALUnitHeader) {
300 .nal_unit_type = HEVC_NAL_VPS,
302 .nuh_temporal_id_plus1 = 1,
305 vps->vps_video_parameter_set_id = 0;
307 vps->vps_base_layer_internal_flag = 1;
308 vps->vps_base_layer_available_flag = 1;
309 vps->vps_max_layers_minus1 = 0;
310 vps->vps_max_sub_layers_minus1 = 0;
311 vps->vps_temporal_id_nesting_flag = 1;
313 ptl->general_profile_space = 0;
314 ptl->general_profile_idc = avctx->profile;
315 ptl->general_tier_flag = priv->tier;
317 if (chroma_format == 1) {
318 ptl->general_profile_compatibility_flag[1] = bit_depth == 8;
319 ptl->general_profile_compatibility_flag[2] = bit_depth <= 10;
321 ptl->general_profile_compatibility_flag[4] = 1;
323 ptl->general_progressive_source_flag = 1;
324 ptl->general_interlaced_source_flag = 0;
325 ptl->general_non_packed_constraint_flag = 1;
326 ptl->general_frame_only_constraint_flag = 1;
328 ptl->general_max_12bit_constraint_flag = bit_depth <= 12;
329 ptl->general_max_10bit_constraint_flag = bit_depth <= 10;
330 ptl->general_max_8bit_constraint_flag = bit_depth == 8;
332 ptl->general_max_422chroma_constraint_flag = chroma_format <= 2;
333 ptl->general_max_420chroma_constraint_flag = chroma_format <= 1;
334 ptl->general_max_monochrome_constraint_flag = chroma_format == 0;
336 ptl->general_intra_constraint_flag = ctx->gop_size == 1;
338 ptl->general_lower_bit_rate_constraint_flag = 1;
340 if (avctx->level != FF_LEVEL_UNKNOWN) {
341 ptl->general_level_idc = avctx->level;
343 const H265LevelDescriptor *level;
345 level = ff_h265_guess_level(ptl, avctx->bit_rate,
346 ctx->surface_width, ctx->surface_height,
347 ctx->nb_slices, ctx->tile_rows, ctx->tile_cols,
348 (ctx->b_per_p > 0) + 1);
350 av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
351 ptl->general_level_idc = level->level_idc;
353 av_log(avctx, AV_LOG_VERBOSE, "Stream will not conform to "
354 "any normal level; using level 8.5.\n");
355 ptl->general_level_idc = 255;
356 // The tier flag must be set in level 8.5.
357 ptl->general_tier_flag = 1;
361 vps->vps_sub_layer_ordering_info_present_flag = 0;
362 vps->vps_max_dec_pic_buffering_minus1[0] = ctx->max_b_depth + 1;
363 vps->vps_max_num_reorder_pics[0] = ctx->max_b_depth;
364 vps->vps_max_latency_increase_plus1[0] = 0;
366 vps->vps_max_layer_id = 0;
367 vps->vps_num_layer_sets_minus1 = 0;
368 vps->layer_id_included_flag[0][0] = 1;
370 vps->vps_timing_info_present_flag = 1;
371 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
372 vps->vps_num_units_in_tick = avctx->framerate.den;
373 vps->vps_time_scale = avctx->framerate.num;
374 vps->vps_poc_proportional_to_timing_flag = 1;
375 vps->vps_num_ticks_poc_diff_one_minus1 = 0;
377 vps->vps_num_units_in_tick = avctx->time_base.num;
378 vps->vps_time_scale = avctx->time_base.den;
379 vps->vps_poc_proportional_to_timing_flag = 0;
381 vps->vps_num_hrd_parameters = 0;
386 sps->nal_unit_header = (H265RawNALUnitHeader) {
387 .nal_unit_type = HEVC_NAL_SPS,
389 .nuh_temporal_id_plus1 = 1,
392 sps->sps_video_parameter_set_id = vps->vps_video_parameter_set_id;
394 sps->sps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1;
395 sps->sps_temporal_id_nesting_flag = vps->vps_temporal_id_nesting_flag;
397 sps->profile_tier_level = vps->profile_tier_level;
399 sps->sps_seq_parameter_set_id = 0;
401 sps->chroma_format_idc = chroma_format;
402 sps->separate_colour_plane_flag = 0;
404 sps->pic_width_in_luma_samples = ctx->surface_width;
405 sps->pic_height_in_luma_samples = ctx->surface_height;
407 if (avctx->width != ctx->surface_width ||
408 avctx->height != ctx->surface_height) {
409 sps->conformance_window_flag = 1;
410 sps->conf_win_left_offset = 0;
411 sps->conf_win_right_offset =
412 (ctx->surface_width - avctx->width) >> desc->log2_chroma_w;
413 sps->conf_win_top_offset = 0;
414 sps->conf_win_bottom_offset =
415 (ctx->surface_height - avctx->height) >> desc->log2_chroma_h;
417 sps->conformance_window_flag = 0;
420 sps->bit_depth_luma_minus8 = bit_depth - 8;
421 sps->bit_depth_chroma_minus8 = bit_depth - 8;
423 sps->log2_max_pic_order_cnt_lsb_minus4 = 8;
425 sps->sps_sub_layer_ordering_info_present_flag =
426 vps->vps_sub_layer_ordering_info_present_flag;
427 for (i = 0; i <= sps->sps_max_sub_layers_minus1; i++) {
428 sps->sps_max_dec_pic_buffering_minus1[i] =
429 vps->vps_max_dec_pic_buffering_minus1[i];
430 sps->sps_max_num_reorder_pics[i] =
431 vps->vps_max_num_reorder_pics[i];
432 sps->sps_max_latency_increase_plus1[i] =
433 vps->vps_max_latency_increase_plus1[i];
436 // These have to come from the capabilities of the encoder. We have no
437 // way to query them, so just hardcode parameters which work on the Intel
439 // CTB size from 8x8 to 32x32.
440 sps->log2_min_luma_coding_block_size_minus3 = 0;
441 sps->log2_diff_max_min_luma_coding_block_size = 2;
442 // Transform size from 4x4 to 32x32.
443 sps->log2_min_luma_transform_block_size_minus2 = 0;
444 sps->log2_diff_max_min_luma_transform_block_size = 3;
445 // Full transform hierarchy allowed (2-5).
446 sps->max_transform_hierarchy_depth_inter = 3;
447 sps->max_transform_hierarchy_depth_intra = 3;
449 sps->amp_enabled_flag = 1;
450 // SAO and temporal MVP do not work.
451 sps->sample_adaptive_offset_enabled_flag = 0;
452 sps->sps_temporal_mvp_enabled_flag = 0;
454 sps->pcm_enabled_flag = 0;
456 // STRPSs should ideally be here rather than defined individually in
457 // each slice, but the structure isn't completely fixed so for now
459 sps->num_short_term_ref_pic_sets = 0;
460 sps->long_term_ref_pics_present_flag = 0;
462 sps->vui_parameters_present_flag = 1;
464 if (avctx->sample_aspect_ratio.num != 0 &&
465 avctx->sample_aspect_ratio.den != 0) {
466 static const AVRational sar_idc[] = {
468 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
469 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
470 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
471 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
474 av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
475 avctx->sample_aspect_ratio.den, 65535);
476 for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
477 if (num == sar_idc[i].num &&
478 den == sar_idc[i].den) {
479 vui->aspect_ratio_idc = i;
483 if (i >= FF_ARRAY_ELEMS(sar_idc)) {
484 vui->aspect_ratio_idc = 255;
485 vui->sar_width = num;
486 vui->sar_height = den;
488 vui->aspect_ratio_info_present_flag = 1;
491 // Unspecified video format, from table E-2.
492 vui->video_format = 5;
493 vui->video_full_range_flag =
494 avctx->color_range == AVCOL_RANGE_JPEG;
495 vui->colour_primaries = avctx->color_primaries;
496 vui->transfer_characteristics = avctx->color_trc;
497 vui->matrix_coefficients = avctx->colorspace;
498 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
499 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
500 avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
501 vui->colour_description_present_flag = 1;
502 if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
503 vui->colour_description_present_flag)
504 vui->video_signal_type_present_flag = 1;
506 if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
507 vui->chroma_loc_info_present_flag = 1;
508 vui->chroma_sample_loc_type_top_field =
509 vui->chroma_sample_loc_type_bottom_field =
510 avctx->chroma_sample_location - 1;
513 vui->vui_timing_info_present_flag = 1;
514 vui->vui_num_units_in_tick = vps->vps_num_units_in_tick;
515 vui->vui_time_scale = vps->vps_time_scale;
516 vui->vui_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag;
517 vui->vui_num_ticks_poc_diff_one_minus1 = vps->vps_num_ticks_poc_diff_one_minus1;
518 vui->vui_hrd_parameters_present_flag = 0;
520 vui->bitstream_restriction_flag = 1;
521 vui->motion_vectors_over_pic_boundaries_flag = 1;
522 vui->restricted_ref_pic_lists_flag = 1;
523 vui->max_bytes_per_pic_denom = 0;
524 vui->max_bits_per_min_cu_denom = 0;
525 vui->log2_max_mv_length_horizontal = 15;
526 vui->log2_max_mv_length_vertical = 15;
531 pps->nal_unit_header = (H265RawNALUnitHeader) {
532 .nal_unit_type = HEVC_NAL_PPS,
534 .nuh_temporal_id_plus1 = 1,
537 pps->pps_pic_parameter_set_id = 0;
538 pps->pps_seq_parameter_set_id = sps->sps_seq_parameter_set_id;
540 pps->num_ref_idx_l0_default_active_minus1 = 0;
541 pps->num_ref_idx_l1_default_active_minus1 = 0;
543 pps->init_qp_minus26 = priv->fixed_qp_idr - 26;
545 pps->cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP);
546 pps->diff_cu_qp_delta_depth = 0;
548 if (ctx->tile_rows && ctx->tile_cols) {
551 pps->tiles_enabled_flag = 1;
552 pps->num_tile_columns_minus1 = ctx->tile_cols - 1;
553 pps->num_tile_rows_minus1 = ctx->tile_rows - 1;
555 // Test whether the spacing provided matches the H.265 uniform
556 // spacing, and set the flag if it does.
558 for (i = 0; i <= pps->num_tile_columns_minus1 &&
559 uniform_spacing; i++) {
560 if (ctx->col_width[i] !=
561 (i + 1) * ctx->slice_block_cols / ctx->tile_cols -
562 i * ctx->slice_block_cols / ctx->tile_cols)
565 for (i = 0; i <= pps->num_tile_rows_minus1 &&
566 uniform_spacing; i++) {
567 if (ctx->row_height[i] !=
568 (i + 1) * ctx->slice_block_rows / ctx->tile_rows -
569 i * ctx->slice_block_rows / ctx->tile_rows)
572 pps->uniform_spacing_flag = uniform_spacing;
574 for (i = 0; i <= pps->num_tile_columns_minus1; i++)
575 pps->column_width_minus1[i] = ctx->col_width[i] - 1;
576 for (i = 0; i <= pps->num_tile_rows_minus1; i++)
577 pps->row_height_minus1[i] = ctx->row_height[i] - 1;
579 pps->loop_filter_across_tiles_enabled_flag = 1;
582 pps->pps_loop_filter_across_slices_enabled_flag = 1;
584 // Fill VAAPI parameter buffers.
586 *vseq = (VAEncSequenceParameterBufferHEVC) {
587 .general_profile_idc = vps->profile_tier_level.general_profile_idc,
588 .general_level_idc = vps->profile_tier_level.general_level_idc,
589 .general_tier_flag = vps->profile_tier_level.general_tier_flag,
591 .intra_period = ctx->gop_size,
592 .intra_idr_period = ctx->gop_size,
593 .ip_period = ctx->b_per_p + 1,
594 .bits_per_second = ctx->va_bit_rate,
596 .pic_width_in_luma_samples = sps->pic_width_in_luma_samples,
597 .pic_height_in_luma_samples = sps->pic_height_in_luma_samples,
600 .chroma_format_idc = sps->chroma_format_idc,
601 .separate_colour_plane_flag = sps->separate_colour_plane_flag,
602 .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
603 .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
604 .scaling_list_enabled_flag = sps->scaling_list_enabled_flag,
605 .strong_intra_smoothing_enabled_flag =
606 sps->strong_intra_smoothing_enabled_flag,
607 .amp_enabled_flag = sps->amp_enabled_flag,
608 .sample_adaptive_offset_enabled_flag =
609 sps->sample_adaptive_offset_enabled_flag,
610 .pcm_enabled_flag = sps->pcm_enabled_flag,
611 .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag,
612 .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
615 .log2_min_luma_coding_block_size_minus3 =
616 sps->log2_min_luma_coding_block_size_minus3,
617 .log2_diff_max_min_luma_coding_block_size =
618 sps->log2_diff_max_min_luma_coding_block_size,
619 .log2_min_transform_block_size_minus2 =
620 sps->log2_min_luma_transform_block_size_minus2,
621 .log2_diff_max_min_transform_block_size =
622 sps->log2_diff_max_min_luma_transform_block_size,
623 .max_transform_hierarchy_depth_inter =
624 sps->max_transform_hierarchy_depth_inter,
625 .max_transform_hierarchy_depth_intra =
626 sps->max_transform_hierarchy_depth_intra,
628 .pcm_sample_bit_depth_luma_minus1 =
629 sps->pcm_sample_bit_depth_luma_minus1,
630 .pcm_sample_bit_depth_chroma_minus1 =
631 sps->pcm_sample_bit_depth_chroma_minus1,
632 .log2_min_pcm_luma_coding_block_size_minus3 =
633 sps->log2_min_pcm_luma_coding_block_size_minus3,
634 .log2_max_pcm_luma_coding_block_size_minus3 =
635 sps->log2_min_pcm_luma_coding_block_size_minus3 +
636 sps->log2_diff_max_min_pcm_luma_coding_block_size,
638 .vui_parameters_present_flag = 0,
641 *vpic = (VAEncPictureParameterBufferHEVC) {
642 .decoded_curr_pic = {
643 .picture_id = VA_INVALID_ID,
644 .flags = VA_PICTURE_HEVC_INVALID,
647 .coded_buf = VA_INVALID_ID,
649 .collocated_ref_pic_index = 0xff,
653 .pic_init_qp = pps->init_qp_minus26 + 26,
654 .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
655 .pps_cb_qp_offset = pps->pps_cb_qp_offset,
656 .pps_cr_qp_offset = pps->pps_cr_qp_offset,
658 .num_tile_columns_minus1 = pps->num_tile_columns_minus1,
659 .num_tile_rows_minus1 = pps->num_tile_rows_minus1,
661 .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2,
662 .ctu_max_bitsize_allowed = 0,
664 .num_ref_idx_l0_default_active_minus1 =
665 pps->num_ref_idx_l0_default_active_minus1,
666 .num_ref_idx_l1_default_active_minus1 =
667 pps->num_ref_idx_l1_default_active_minus1,
669 .slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id,
672 .sign_data_hiding_enabled_flag = pps->sign_data_hiding_enabled_flag,
673 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
674 .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
675 .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
676 .weighted_pred_flag = pps->weighted_pred_flag,
677 .weighted_bipred_flag = pps->weighted_bipred_flag,
678 .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag,
679 .tiles_enabled_flag = pps->tiles_enabled_flag,
680 .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
681 .loop_filter_across_tiles_enabled_flag =
682 pps->loop_filter_across_tiles_enabled_flag,
683 .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag |
684 pps->pps_scaling_list_data_present_flag),
685 .screen_content_flag = 0,
686 .enable_gpu_weighted_prediction = 0,
687 .no_output_of_prior_pics_flag = 0,
691 if (pps->tiles_enabled_flag) {
692 for (i = 0; i <= vpic->num_tile_rows_minus1; i++)
693 vpic->row_height_minus1[i] = pps->row_height_minus1[i];
694 for (i = 0; i <= vpic->num_tile_columns_minus1; i++)
695 vpic->column_width_minus1[i] = pps->column_width_minus1[i];
701 static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
702 VAAPIEncodePicture *pic)
704 VAAPIEncodeContext *ctx = avctx->priv_data;
705 VAAPIEncodeH265Context *priv = avctx->priv_data;
706 VAAPIEncodeH265Picture *hpic = pic->priv_data;
707 VAAPIEncodePicture *prev = pic->prev;
708 VAAPIEncodeH265Picture *hprev = prev ? prev->priv_data : NULL;
709 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
712 if (pic->type == PICTURE_TYPE_IDR) {
713 av_assert0(pic->display_order == pic->encode_order);
715 hpic->last_idr_frame = pic->display_order;
717 hpic->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
718 hpic->slice_type = HEVC_SLICE_I;
722 hpic->last_idr_frame = hprev->last_idr_frame;
724 if (pic->type == PICTURE_TYPE_I) {
725 hpic->slice_nal_unit = HEVC_NAL_CRA_NUT;
726 hpic->slice_type = HEVC_SLICE_I;
728 } else if (pic->type == PICTURE_TYPE_P) {
729 av_assert0(pic->refs[0]);
730 hpic->slice_nal_unit = HEVC_NAL_TRAIL_R;
731 hpic->slice_type = HEVC_SLICE_P;
734 VAAPIEncodePicture *irap_ref;
735 av_assert0(pic->refs[0] && pic->refs[1]);
736 for (irap_ref = pic; irap_ref; irap_ref = irap_ref->refs[1]) {
737 if (irap_ref->type == PICTURE_TYPE_I)
740 if (pic->b_depth == ctx->max_b_depth) {
741 hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_N
744 hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_R
747 hpic->slice_type = HEVC_SLICE_B;
751 hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame;
754 priv->aud_needed = 1;
755 priv->raw_aud = (H265RawAUD) {
757 .nal_unit_type = HEVC_NAL_AUD,
759 .nuh_temporal_id_plus1 = 1,
761 .pic_type = hpic->pic_type,
764 priv->aud_needed = 0;
767 priv->sei_needed = 0;
769 // Only look for the metadata on I/IDR frame on the output. We
770 // may force an IDR frame on the output where the medadata gets
771 // changed on the input frame.
772 if ((priv->sei & SEI_MASTERING_DISPLAY) &&
773 (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
774 AVFrameSideData *sd =
775 av_frame_get_side_data(pic->input_image,
776 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
779 AVMasteringDisplayMetadata *mdm =
780 (AVMasteringDisplayMetadata *)sd->data;
782 // SEI is needed when both the primaries and luminance are set
783 if (mdm->has_primaries && mdm->has_luminance) {
784 H265RawSEIMasteringDisplayColourVolume *mdcv =
785 &priv->sei_mastering_display;
786 const int mapping[3] = {1, 2, 0};
787 const int chroma_den = 50000;
788 const int luma_den = 10000;
790 for (i = 0; i < 3; i++) {
791 const int j = mapping[i];
792 mdcv->display_primaries_x[i] =
793 FFMIN(lrint(chroma_den *
794 av_q2d(mdm->display_primaries[j][0])),
796 mdcv->display_primaries_y[i] =
797 FFMIN(lrint(chroma_den *
798 av_q2d(mdm->display_primaries[j][1])),
802 mdcv->white_point_x =
803 FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
805 mdcv->white_point_y =
806 FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
809 mdcv->max_display_mastering_luminance =
810 lrint(luma_den * av_q2d(mdm->max_luminance));
811 mdcv->min_display_mastering_luminance =
812 FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
813 mdcv->max_display_mastering_luminance);
815 priv->sei_needed |= SEI_MASTERING_DISPLAY;
820 if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
821 (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
822 AVFrameSideData *sd =
823 av_frame_get_side_data(pic->input_image,
824 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
827 AVContentLightMetadata *clm =
828 (AVContentLightMetadata *)sd->data;
829 H265RawSEIContentLightLevelInfo *clli =
830 &priv->sei_content_light_level;
832 clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535);
833 clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
835 priv->sei_needed |= SEI_CONTENT_LIGHT_LEVEL;
839 vpic->decoded_curr_pic = (VAPictureHEVC) {
840 .picture_id = pic->recon_surface,
841 .pic_order_cnt = hpic->pic_order_cnt,
845 for (i = 0; i < pic->nb_refs; i++) {
846 VAAPIEncodePicture *ref = pic->refs[i];
847 VAAPIEncodeH265Picture *href;
849 av_assert0(ref && ref->encode_order < pic->encode_order);
850 href = ref->priv_data;
852 vpic->reference_frames[i] = (VAPictureHEVC) {
853 .picture_id = ref->recon_surface,
854 .pic_order_cnt = href->pic_order_cnt,
855 .flags = (ref->display_order < pic->display_order ?
856 VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
857 (ref->display_order > pic->display_order ?
858 VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
861 for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
862 vpic->reference_frames[i] = (VAPictureHEVC) {
863 .picture_id = VA_INVALID_ID,
864 .flags = VA_PICTURE_HEVC_INVALID,
868 vpic->coded_buf = pic->output_buffer;
870 vpic->nal_unit_type = hpic->slice_nal_unit;
873 case PICTURE_TYPE_IDR:
874 vpic->pic_fields.bits.idr_pic_flag = 1;
875 vpic->pic_fields.bits.coding_type = 1;
876 vpic->pic_fields.bits.reference_pic_flag = 1;
879 vpic->pic_fields.bits.idr_pic_flag = 0;
880 vpic->pic_fields.bits.coding_type = 1;
881 vpic->pic_fields.bits.reference_pic_flag = 1;
884 vpic->pic_fields.bits.idr_pic_flag = 0;
885 vpic->pic_fields.bits.coding_type = 2;
886 vpic->pic_fields.bits.reference_pic_flag = 1;
889 vpic->pic_fields.bits.idr_pic_flag = 0;
890 vpic->pic_fields.bits.coding_type = 3;
891 vpic->pic_fields.bits.reference_pic_flag = 0;
894 av_assert0(0 && "invalid picture type");
900 static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
901 VAAPIEncodePicture *pic,
902 VAAPIEncodeSlice *slice)
904 VAAPIEncodeH265Context *priv = avctx->priv_data;
905 VAAPIEncodeH265Picture *hpic = pic->priv_data;
906 const H265RawSPS *sps = &priv->raw_sps;
907 const H265RawPPS *pps = &priv->raw_pps;
908 H265RawSliceHeader *sh = &priv->raw_slice.header;
909 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
910 VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
913 sh->nal_unit_header = (H265RawNALUnitHeader) {
914 .nal_unit_type = hpic->slice_nal_unit,
916 .nuh_temporal_id_plus1 = 1,
919 sh->slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id;
921 sh->first_slice_segment_in_pic_flag = slice->index == 0;
922 sh->slice_segment_address = slice->block_start;
924 sh->slice_type = hpic->slice_type;
926 sh->slice_pic_order_cnt_lsb = hpic->pic_order_cnt &
927 (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
929 if (pic->type != PICTURE_TYPE_IDR) {
930 H265RawSTRefPicSet *rps;
931 const VAAPIEncodeH265Picture *strp;
932 int rps_poc[MAX_DPB_SIZE];
933 int rps_used[MAX_DPB_SIZE];
934 int i, j, poc, rps_pics;
936 sh->short_term_ref_pic_set_sps_flag = 0;
938 rps = &sh->short_term_ref_pic_set;
939 memset(rps, 0, sizeof(*rps));
942 for (i = 0; i < pic->nb_refs; i++) {
943 strp = pic->refs[i]->priv_data;
944 rps_poc[rps_pics] = strp->pic_order_cnt;
945 rps_used[rps_pics] = 1;
948 for (i = 0; i < pic->nb_dpb_pics; i++) {
949 if (pic->dpb[i] == pic)
951 for (j = 0; j < pic->nb_refs; j++) {
952 if (pic->dpb[i] == pic->refs[j])
955 if (j < pic->nb_refs)
957 strp = pic->dpb[i]->priv_data;
958 rps_poc[rps_pics] = strp->pic_order_cnt;
959 rps_used[rps_pics] = 0;
963 for (i = 1; i < rps_pics; i++) {
964 for (j = i; j > 0; j--) {
965 if (rps_poc[j] > rps_poc[j - 1])
967 av_assert0(rps_poc[j] != rps_poc[j - 1]);
968 FFSWAP(int, rps_poc[j], rps_poc[j - 1]);
969 FFSWAP(int, rps_used[j], rps_used[j - 1]);
973 av_log(avctx, AV_LOG_DEBUG, "RPS for POC %d:",
974 hpic->pic_order_cnt);
975 for (i = 0; i < rps_pics; i++) {
976 av_log(avctx, AV_LOG_DEBUG, " (%d,%d)",
977 rps_poc[i], rps_used[i]);
979 av_log(avctx, AV_LOG_DEBUG, "\n");
981 for (i = 0; i < rps_pics; i++) {
982 av_assert0(rps_poc[i] != hpic->pic_order_cnt);
983 if (rps_poc[i] > hpic->pic_order_cnt)
987 rps->num_negative_pics = i;
988 poc = hpic->pic_order_cnt;
989 for (j = i - 1; j >= 0; j--) {
990 rps->delta_poc_s0_minus1[i - 1 - j] = poc - rps_poc[j] - 1;
991 rps->used_by_curr_pic_s0_flag[i - 1 - j] = rps_used[j];
995 rps->num_positive_pics = rps_pics - i;
996 poc = hpic->pic_order_cnt;
997 for (j = i; j < rps_pics; j++) {
998 rps->delta_poc_s1_minus1[j - i] = rps_poc[j] - poc - 1;
999 rps->used_by_curr_pic_s1_flag[j - i] = rps_used[j];
1003 sh->num_long_term_sps = 0;
1004 sh->num_long_term_pics = 0;
1006 sh->slice_temporal_mvp_enabled_flag =
1007 sps->sps_temporal_mvp_enabled_flag;
1008 if (sh->slice_temporal_mvp_enabled_flag) {
1009 sh->collocated_from_l0_flag = sh->slice_type == HEVC_SLICE_B;
1010 sh->collocated_ref_idx = 0;
1013 sh->num_ref_idx_active_override_flag = 0;
1014 sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
1015 sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
1018 sh->slice_sao_luma_flag = sh->slice_sao_chroma_flag =
1019 sps->sample_adaptive_offset_enabled_flag;
1021 if (pic->type == PICTURE_TYPE_B)
1022 sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
1023 else if (pic->type == PICTURE_TYPE_P)
1024 sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
1026 sh->slice_qp_delta = priv->fixed_qp_idr - (pps->init_qp_minus26 + 26);
1029 *vslice = (VAEncSliceParameterBufferHEVC) {
1030 .slice_segment_address = sh->slice_segment_address,
1031 .num_ctu_in_slice = slice->block_size,
1033 .slice_type = sh->slice_type,
1034 .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
1036 .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
1037 .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
1039 .luma_log2_weight_denom = sh->luma_log2_weight_denom,
1040 .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
1042 .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
1044 .slice_qp_delta = sh->slice_qp_delta,
1045 .slice_cb_qp_offset = sh->slice_cb_qp_offset,
1046 .slice_cr_qp_offset = sh->slice_cr_qp_offset,
1048 .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
1049 .slice_tc_offset_div2 = sh->slice_tc_offset_div2,
1051 .slice_fields.bits = {
1052 .last_slice_of_pic_flag = slice->index == pic->nb_slices - 1,
1053 .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
1054 .colour_plane_id = sh->colour_plane_id,
1055 .slice_temporal_mvp_enabled_flag =
1056 sh->slice_temporal_mvp_enabled_flag,
1057 .slice_sao_luma_flag = sh->slice_sao_luma_flag,
1058 .slice_sao_chroma_flag = sh->slice_sao_chroma_flag,
1059 .num_ref_idx_active_override_flag =
1060 sh->num_ref_idx_active_override_flag,
1061 .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
1062 .cabac_init_flag = sh->cabac_init_flag,
1063 .slice_deblocking_filter_disabled_flag =
1064 sh->slice_deblocking_filter_disabled_flag,
1065 .slice_loop_filter_across_slices_enabled_flag =
1066 sh->slice_loop_filter_across_slices_enabled_flag,
1067 .collocated_from_l0_flag = sh->collocated_from_l0_flag,
1071 for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
1072 vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
1073 vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
1074 vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
1075 vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
1078 av_assert0(pic->nb_refs <= 2);
1079 if (pic->nb_refs >= 1) {
1080 // Backward reference for P- or B-frame.
1081 av_assert0(pic->type == PICTURE_TYPE_P ||
1082 pic->type == PICTURE_TYPE_B);
1083 vslice->ref_pic_list0[0] = vpic->reference_frames[0];
1085 if (pic->nb_refs >= 2) {
1086 // Forward reference for B-frame.
1087 av_assert0(pic->type == PICTURE_TYPE_B);
1088 vslice->ref_pic_list1[0] = vpic->reference_frames[1];
1094 static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
1096 VAAPIEncodeContext *ctx = avctx->priv_data;
1097 VAAPIEncodeH265Context *priv = avctx->priv_data;
1100 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
1104 if (ctx->va_rc_mode == VA_RC_CQP) {
1105 // Note that VAAPI only supports positive QP values - the range is
1106 // therefore always bounded below by 1, even in 10-bit mode where
1107 // it should go down to -12.
1109 priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
1110 if (avctx->i_quant_factor > 0.0)
1111 priv->fixed_qp_idr =
1112 av_clip((avctx->i_quant_factor * priv->fixed_qp_p +
1113 avctx->i_quant_offset) + 0.5, 1, 51);
1115 priv->fixed_qp_idr = priv->fixed_qp_p;
1116 if (avctx->b_quant_factor > 0.0)
1118 av_clip((avctx->b_quant_factor * priv->fixed_qp_p +
1119 avctx->b_quant_offset) + 0.5, 1, 51);
1121 priv->fixed_qp_b = priv->fixed_qp_p;
1123 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1124 "%d / %d / %d for IDR- / P- / B-frames.\n",
1125 priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1128 // These still need to be set for init_qp/slice_qp_delta.
1129 priv->fixed_qp_idr = 30;
1130 priv->fixed_qp_p = 30;
1131 priv->fixed_qp_b = 30;
1134 ctx->roi_quant_range = 51 + 6 * (ctx->profile->depth - 8);
1139 static const VAAPIEncodeProfile vaapi_encode_h265_profiles[] = {
1140 { FF_PROFILE_HEVC_MAIN, 8, 3, 1, 1, VAProfileHEVCMain },
1141 { FF_PROFILE_HEVC_REXT, 8, 3, 1, 1, VAProfileHEVCMain },
1142 #if VA_CHECK_VERSION(0, 37, 0)
1143 { FF_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10 },
1144 { FF_PROFILE_HEVC_REXT, 10, 3, 1, 1, VAProfileHEVCMain10 },
1146 #if VA_CHECK_VERSION(1, 2, 0)
1147 { FF_PROFILE_HEVC_REXT, 8, 3, 1, 0, VAProfileHEVCMain422_10 },
1148 { FF_PROFILE_HEVC_REXT, 10, 3, 1, 0, VAProfileHEVCMain422_10 },
1150 { FF_PROFILE_UNKNOWN }
1153 static const VAAPIEncodeType vaapi_encode_type_h265 = {
1154 .profiles = vaapi_encode_h265_profiles,
1156 .flags = FLAG_SLICE_CONTROL |
1158 FLAG_B_PICTURE_REFERENCES |
1159 FLAG_NON_IDR_KEY_PICTURES,
1161 .default_quality = 25,
1163 .configure = &vaapi_encode_h265_configure,
1165 .picture_priv_data_size = sizeof(VAAPIEncodeH265Picture),
1167 .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
1168 .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
1170 .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
1171 .init_picture_params = &vaapi_encode_h265_init_picture_params,
1173 .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
1174 .init_slice_params = &vaapi_encode_h265_init_slice_params,
1176 .sequence_header_type = VAEncPackedHeaderSequence,
1177 .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
1179 .slice_header_type = VAEncPackedHeaderHEVC_Slice,
1180 .write_slice_header = &vaapi_encode_h265_write_slice_header,
1182 .write_extra_header = &vaapi_encode_h265_write_extra_header,
1185 static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
1187 VAAPIEncodeContext *ctx = avctx->priv_data;
1188 VAAPIEncodeH265Context *priv = avctx->priv_data;
1190 ctx->codec = &vaapi_encode_type_h265;
1192 if (avctx->profile == FF_PROFILE_UNKNOWN)
1193 avctx->profile = priv->profile;
1194 if (avctx->level == FF_LEVEL_UNKNOWN)
1195 avctx->level = priv->level;
1197 if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
1198 av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
1199 "in 8-bit unsigned integer.\n", avctx->level);
1200 return AVERROR(EINVAL);
1203 ctx->desired_packed_headers =
1204 VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
1205 VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
1206 VA_ENC_PACKED_HEADER_MISC; // SEI
1208 ctx->surface_width = FFALIGN(avctx->width, 16);
1209 ctx->surface_height = FFALIGN(avctx->height, 16);
1211 // CTU size is currently hard-coded to 32.
1212 ctx->slice_block_width = ctx->slice_block_height = 32;
1215 ctx->explicit_qp = priv->qp;
1217 return ff_vaapi_encode_init(avctx);
1220 static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
1222 VAAPIEncodeH265Context *priv = avctx->priv_data;
1224 ff_cbs_fragment_free(&priv->current_access_unit);
1225 ff_cbs_close(&priv->cbc);
1227 return ff_vaapi_encode_close(avctx);
1230 #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
1231 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1232 static const AVOption vaapi_encode_h265_options[] = {
1233 VAAPI_ENCODE_COMMON_OPTIONS,
1234 VAAPI_ENCODE_RC_OPTIONS,
1236 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1237 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
1239 { "aud", "Include AUD",
1240 OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1242 { "profile", "Set profile (general_profile_idc)",
1243 OFFSET(profile), AV_OPT_TYPE_INT,
1244 { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
1246 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1247 { .i64 = value }, 0, 0, FLAGS, "profile"
1248 { PROFILE("main", FF_PROFILE_HEVC_MAIN) },
1249 { PROFILE("main10", FF_PROFILE_HEVC_MAIN_10) },
1250 { PROFILE("rext", FF_PROFILE_HEVC_REXT) },
1253 { "tier", "Set tier (general_tier_flag)",
1254 OFFSET(tier), AV_OPT_TYPE_INT,
1255 { .i64 = 0 }, 0, 1, FLAGS, "tier" },
1256 { "main", NULL, 0, AV_OPT_TYPE_CONST,
1257 { .i64 = 0 }, 0, 0, FLAGS, "tier" },
1258 { "high", NULL, 0, AV_OPT_TYPE_CONST,
1259 { .i64 = 1 }, 0, 0, FLAGS, "tier" },
1261 { "level", "Set level (general_level_idc)",
1262 OFFSET(level), AV_OPT_TYPE_INT,
1263 { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
1265 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1266 { .i64 = value }, 0, 0, FLAGS, "level"
1269 { LEVEL("2.1", 63) },
1271 { LEVEL("3.1", 93) },
1272 { LEVEL("4", 120) },
1273 { LEVEL("4.1", 123) },
1274 { LEVEL("5", 150) },
1275 { LEVEL("5.1", 153) },
1276 { LEVEL("5.2", 156) },
1277 { LEVEL("6", 180) },
1278 { LEVEL("6.1", 183) },
1279 { LEVEL("6.2", 186) },
1282 { "sei", "Set SEI to include",
1283 OFFSET(sei), AV_OPT_TYPE_FLAGS,
1284 { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
1285 0, INT_MAX, FLAGS, "sei" },
1287 "Include HDR metadata for mastering display colour volume "
1288 "and content light level information",
1289 0, AV_OPT_TYPE_CONST,
1290 { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
1291 INT_MIN, INT_MAX, FLAGS, "sei" },
1293 { "tiles", "Tile columns x rows",
1294 OFFSET(common.tile_cols), AV_OPT_TYPE_IMAGE_SIZE,
1295 { .str = NULL }, 0, 0, FLAGS },
1300 static const AVCodecDefault vaapi_encode_h265_defaults[] = {
1304 { "i_qfactor", "1" },
1305 { "i_qoffset", "0" },
1306 { "b_qfactor", "6/5" },
1307 { "b_qoffset", "0" },
1313 static const AVClass vaapi_encode_h265_class = {
1314 .class_name = "h265_vaapi",
1315 .item_name = av_default_item_name,
1316 .option = vaapi_encode_h265_options,
1317 .version = LIBAVUTIL_VERSION_INT,
1320 AVCodec ff_hevc_vaapi_encoder = {
1321 .name = "hevc_vaapi",
1322 .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
1323 .type = AVMEDIA_TYPE_VIDEO,
1324 .id = AV_CODEC_ID_HEVC,
1325 .priv_data_size = sizeof(VAAPIEncodeH265Context),
1326 .init = &vaapi_encode_h265_init,
1327 .receive_packet = &ff_vaapi_encode_receive_packet,
1328 .close = &vaapi_encode_h265_close,
1329 .priv_class = &vaapi_encode_h265_class,
1330 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
1331 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1332 .defaults = vaapi_encode_h265_defaults,
1333 .pix_fmts = (const enum AVPixelFormat[]) {
1337 .hw_configs = ff_vaapi_encode_hw_configs,
1338 .wrapper_name = "vaapi",