FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vaapi_encode_h265.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 557 0.0%
Functions: 0 12 0.0%
Branches: 0 253 0.0%

Line Branch Exec Source
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/mem.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/mastering_display_metadata.h"
30
31 #include "atsc_a53.h"
32 #include "avcodec.h"
33 #include "cbs.h"
34 #include "cbs_h265.h"
35 #include "hw_base_encode_h265.h"
36 #include "codec_internal.h"
37 #include "h2645data.h"
38 #include "h265_profile_level.h"
39 #include "vaapi_encode.h"
40
41 #include "hevc/hevc.h"
42
43 enum {
44 SEI_MASTERING_DISPLAY = 0x08,
45 SEI_CONTENT_LIGHT_LEVEL = 0x10,
46 SEI_A53_CC = 0x20,
47 };
48
49 typedef struct VAAPIEncodeH265Picture {
50 int pic_order_cnt;
51
52 int64_t last_idr_frame;
53
54 int slice_nal_unit;
55 int slice_type;
56 int pic_type;
57 } VAAPIEncodeH265Picture;
58
59 typedef struct VAAPIEncodeH265Context {
60 VAAPIEncodeContext common;
61
62 // Encoder features.
63 uint32_t va_features;
64 // Block size info.
65 uint32_t va_bs;
66 uint32_t ctu_size;
67 uint32_t min_cb_size;
68
69 // User options.
70 int qp;
71 int aud;
72 int profile;
73 int level;
74 int sei;
75
76 // Derived settings.
77 int fixed_qp_p;
78 int fixed_qp_b;
79
80 // Writer structures.
81 FFHWBaseEncodeH265 units;
82 FFHWBaseEncodeH265Opts unit_opts;
83 H265RawAUD raw_aud;
84 H265RawSlice raw_slice;
85
86 SEIRawMasteringDisplayColourVolume sei_mastering_display;
87 SEIRawContentLightLevelInfo sei_content_light_level;
88 SEIRawUserDataRegistered sei_a53cc;
89 void *sei_a53cc_data;
90
91 CodedBitstreamContext *cbc;
92 CodedBitstreamFragment current_access_unit;
93 int aud_needed;
94 int sei_needed;
95 } VAAPIEncodeH265Context;
96
97
98 static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx,
99 char *data, size_t *data_len,
100 CodedBitstreamFragment *au)
101 {
102 VAAPIEncodeH265Context *priv = avctx->priv_data;
103 int err;
104
105 err = ff_cbs_write_fragment_data(priv->cbc, au);
106 if (err < 0) {
107 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
108 return err;
109 }
110
111 if (*data_len < 8 * au->data_size - au->data_bit_padding) {
112 av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
113 "%zu < %zu.\n", *data_len,
114 8 * au->data_size - au->data_bit_padding);
115 return AVERROR(ENOSPC);
116 }
117
118 memcpy(data, au->data, au->data_size);
119 *data_len = 8 * au->data_size - au->data_bit_padding;
120
121 return 0;
122 }
123
124 static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
125 CodedBitstreamFragment *au,
126 void *nal_unit)
127 {
128 H265RawNALUnitHeader *header = nal_unit;
129 int err;
130
131 err = ff_cbs_insert_unit_content(au, -1,
132 header->nal_unit_type, nal_unit, NULL);
133 if (err < 0) {
134 av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
135 "type = %d.\n", header->nal_unit_type);
136 return err;
137 }
138
139 return 0;
140 }
141
142 static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
143 char *data, size_t *data_len)
144 {
145 VAAPIEncodeH265Context *priv = avctx->priv_data;
146 CodedBitstreamFragment *au = &priv->current_access_unit;
147 int err;
148
149 if (priv->aud_needed) {
150 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
151 if (err < 0)
152 goto fail;
153 priv->aud_needed = 0;
154 }
155
156 err = vaapi_encode_h265_add_nal(avctx, au, &priv->units.raw_vps);
157 if (err < 0)
158 goto fail;
159
160 err = vaapi_encode_h265_add_nal(avctx, au, &priv->units.raw_sps);
161 if (err < 0)
162 goto fail;
163
164 err = vaapi_encode_h265_add_nal(avctx, au, &priv->units.raw_pps);
165 if (err < 0)
166 goto fail;
167
168 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
169 fail:
170 ff_cbs_fragment_reset(au);
171 return err;
172 }
173
174 static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
175 VAAPIEncodePicture *pic,
176 VAAPIEncodeSlice *slice,
177 char *data, size_t *data_len)
178 {
179 VAAPIEncodeH265Context *priv = avctx->priv_data;
180 CodedBitstreamFragment *au = &priv->current_access_unit;
181 int err;
182
183 if (priv->aud_needed) {
184 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
185 if (err < 0)
186 goto fail;
187 priv->aud_needed = 0;
188 }
189
190 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_slice);
191 if (err < 0)
192 goto fail;
193
194 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
195 fail:
196 ff_cbs_fragment_reset(au);
197 return err;
198 }
199
200 static int vaapi_encode_h265_write_extra_header(AVCodecContext *avctx,
201 FFHWBaseEncodePicture *base,
202 int index, int *type,
203 char *data, size_t *data_len)
204 {
205 VAAPIEncodeH265Context *priv = avctx->priv_data;
206 CodedBitstreamFragment *au = &priv->current_access_unit;
207 int err;
208
209 if (priv->sei_needed) {
210 if (priv->aud_needed) {
211 err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
212 if (err < 0)
213 goto fail;
214 priv->aud_needed = 0;
215 }
216
217 if (priv->sei_needed & SEI_MASTERING_DISPLAY) {
218 err = ff_cbs_sei_add_message(priv->cbc, au, 1,
219 SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME,
220 &priv->sei_mastering_display, NULL);
221 if (err < 0)
222 goto fail;
223 }
224
225 if (priv->sei_needed & SEI_CONTENT_LIGHT_LEVEL) {
226 err = ff_cbs_sei_add_message(priv->cbc, au, 1,
227 SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO,
228 &priv->sei_content_light_level, NULL);
229 if (err < 0)
230 goto fail;
231 }
232 if (priv->sei_needed & SEI_A53_CC) {
233 err = ff_cbs_sei_add_message(priv->cbc, au, 1,
234 SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35,
235 &priv->sei_a53cc, NULL);
236 if (err < 0)
237 goto fail;
238 }
239
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_reset(au);
247
248 *type = VAEncPackedHeaderRawData;
249 return 0;
250 } else {
251 return AVERROR_EOF;
252 }
253
254 fail:
255 ff_cbs_fragment_reset(au);
256 return err;
257 }
258
259 static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
260 {
261 FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
262 VAAPIEncodeContext *ctx = avctx->priv_data;
263 VAAPIEncodeH265Context *priv = avctx->priv_data;
264 H265RawVPS *vps = &priv->units.raw_vps;
265 H265RawSPS *sps = &priv->units.raw_sps;
266 H265RawPPS *pps = &priv->units.raw_pps;
267 VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
268 VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
269 int i, err;
270
271 // priv->unit_opts.tier already set
272 // priv->unit_opts.fixed_qp_idr already set
273 priv->unit_opts.cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP);
274 priv->unit_opts.tile_rows = ctx->tile_rows;
275 priv->unit_opts.tile_cols = ctx->tile_cols;
276 priv->unit_opts.nb_slices = ctx->nb_slices;
277 priv->unit_opts.slice_block_rows = ctx->slice_block_rows;
278 priv->unit_opts.slice_block_cols = ctx->slice_block_cols;
279 memcpy(priv->unit_opts.col_width, ctx->col_width,
280 ctx->tile_rows*sizeof(*priv->unit_opts.col_width));
281 memcpy(priv->unit_opts.row_height, ctx->row_height,
282 ctx->tile_cols*sizeof(*priv->unit_opts.row_height));
283
284 err = ff_hw_base_encode_init_params_h265(base_ctx, avctx,
285 &priv->units, &priv->unit_opts);
286 if (err < 0)
287 return err;
288
289 #if VA_CHECK_VERSION(1, 13, 0)
290 // update sps setting according to queried result
291 if (priv->va_features) {
292 VAConfigAttribValEncHEVCFeatures features = { .value = priv->va_features };
293
294 // Enable feature if get queried result is VA_FEATURE_SUPPORTED | VA_FEATURE_REQUIRED
295 sps->amp_enabled_flag =
296 !!features.bits.amp;
297 sps->sample_adaptive_offset_enabled_flag =
298 !!features.bits.sao;
299 sps->sps_temporal_mvp_enabled_flag =
300 !!features.bits.temporal_mvp;
301 sps->pcm_enabled_flag =
302 !!features.bits.pcm;
303 }
304
305 if (priv->va_bs) {
306 VAConfigAttribValEncHEVCBlockSizes bs = { .value = priv->va_bs };
307 sps->log2_min_luma_coding_block_size_minus3 =
308 ff_ctz(priv->min_cb_size) - 3;
309 sps->log2_diff_max_min_luma_coding_block_size =
310 ff_ctz(priv->ctu_size) - ff_ctz(priv->min_cb_size);
311
312 sps->log2_min_luma_transform_block_size_minus2 =
313 bs.bits.log2_min_luma_transform_block_size_minus2;
314 sps->log2_diff_max_min_luma_transform_block_size =
315 bs.bits.log2_max_luma_transform_block_size_minus2 -
316 bs.bits.log2_min_luma_transform_block_size_minus2;
317
318 sps->max_transform_hierarchy_depth_inter =
319 bs.bits.max_max_transform_hierarchy_depth_inter;
320 sps->max_transform_hierarchy_depth_intra =
321 bs.bits.max_max_transform_hierarchy_depth_intra;
322 }
323
324 // update pps setting according to queried result
325 if (priv->va_features) {
326 VAConfigAttribValEncHEVCFeatures features = { .value = priv->va_features };
327 if (ctx->va_rc_mode != VA_RC_CQP)
328 pps->cu_qp_delta_enabled_flag =
329 !!features.bits.cu_qp_delta;
330
331 pps->transform_skip_enabled_flag =
332 !!features.bits.transform_skip;
333 // set diff_cu_qp_delta_depth as its max value if cu_qp_delta enabled. Otherwise
334 // 0 will make cu_qp_delta invalid.
335 if (pps->cu_qp_delta_enabled_flag)
336 pps->diff_cu_qp_delta_depth = sps->log2_diff_max_min_luma_coding_block_size;
337 }
338 #endif
339
340 // Fill VAAPI parameter buffers.
341
342 *vseq = (VAEncSequenceParameterBufferHEVC) {
343 .general_profile_idc = vps->profile_tier_level.general_profile_idc,
344 .general_level_idc = vps->profile_tier_level.general_level_idc,
345 .general_tier_flag = vps->profile_tier_level.general_tier_flag,
346
347 .intra_period = base_ctx->gop_size,
348 .intra_idr_period = base_ctx->gop_size,
349 .ip_period = base_ctx->b_per_p + 1,
350 .bits_per_second = ctx->va_bit_rate,
351
352 .pic_width_in_luma_samples = sps->pic_width_in_luma_samples,
353 .pic_height_in_luma_samples = sps->pic_height_in_luma_samples,
354
355 .seq_fields.bits = {
356 .chroma_format_idc = sps->chroma_format_idc,
357 .separate_colour_plane_flag = sps->separate_colour_plane_flag,
358 .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
359 .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
360 .scaling_list_enabled_flag = sps->scaling_list_enabled_flag,
361 .strong_intra_smoothing_enabled_flag =
362 sps->strong_intra_smoothing_enabled_flag,
363 .amp_enabled_flag = sps->amp_enabled_flag,
364 .sample_adaptive_offset_enabled_flag =
365 sps->sample_adaptive_offset_enabled_flag,
366 .pcm_enabled_flag = sps->pcm_enabled_flag,
367 .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag,
368 .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
369 },
370
371 .log2_min_luma_coding_block_size_minus3 =
372 sps->log2_min_luma_coding_block_size_minus3,
373 .log2_diff_max_min_luma_coding_block_size =
374 sps->log2_diff_max_min_luma_coding_block_size,
375 .log2_min_transform_block_size_minus2 =
376 sps->log2_min_luma_transform_block_size_minus2,
377 .log2_diff_max_min_transform_block_size =
378 sps->log2_diff_max_min_luma_transform_block_size,
379 .max_transform_hierarchy_depth_inter =
380 sps->max_transform_hierarchy_depth_inter,
381 .max_transform_hierarchy_depth_intra =
382 sps->max_transform_hierarchy_depth_intra,
383
384 .pcm_sample_bit_depth_luma_minus1 =
385 sps->pcm_sample_bit_depth_luma_minus1,
386 .pcm_sample_bit_depth_chroma_minus1 =
387 sps->pcm_sample_bit_depth_chroma_minus1,
388 .log2_min_pcm_luma_coding_block_size_minus3 =
389 sps->log2_min_pcm_luma_coding_block_size_minus3,
390 .log2_max_pcm_luma_coding_block_size_minus3 =
391 sps->log2_min_pcm_luma_coding_block_size_minus3 +
392 sps->log2_diff_max_min_pcm_luma_coding_block_size,
393
394 .vui_parameters_present_flag = 0,
395 };
396
397 *vpic = (VAEncPictureParameterBufferHEVC) {
398 .decoded_curr_pic = {
399 .picture_id = VA_INVALID_ID,
400 .flags = VA_PICTURE_HEVC_INVALID,
401 },
402
403 .coded_buf = VA_INVALID_ID,
404
405 .collocated_ref_pic_index = sps->sps_temporal_mvp_enabled_flag ?
406 0 : 0xff,
407 .last_picture = 0,
408
409 .pic_init_qp = pps->init_qp_minus26 + 26,
410 .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
411 .pps_cb_qp_offset = pps->pps_cb_qp_offset,
412 .pps_cr_qp_offset = pps->pps_cr_qp_offset,
413
414 .num_tile_columns_minus1 = pps->num_tile_columns_minus1,
415 .num_tile_rows_minus1 = pps->num_tile_rows_minus1,
416
417 .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2,
418 .ctu_max_bitsize_allowed = 0,
419
420 .num_ref_idx_l0_default_active_minus1 =
421 pps->num_ref_idx_l0_default_active_minus1,
422 .num_ref_idx_l1_default_active_minus1 =
423 pps->num_ref_idx_l1_default_active_minus1,
424
425 .slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id,
426
427 .pic_fields.bits = {
428 .sign_data_hiding_enabled_flag = pps->sign_data_hiding_enabled_flag,
429 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
430 .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
431 .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
432 .weighted_pred_flag = pps->weighted_pred_flag,
433 .weighted_bipred_flag = pps->weighted_bipred_flag,
434 .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag,
435 .tiles_enabled_flag = pps->tiles_enabled_flag,
436 .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
437 .loop_filter_across_tiles_enabled_flag =
438 pps->loop_filter_across_tiles_enabled_flag,
439 .pps_loop_filter_across_slices_enabled_flag =
440 pps->pps_loop_filter_across_slices_enabled_flag,
441 .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag |
442 pps->pps_scaling_list_data_present_flag),
443 .screen_content_flag = 0,
444 .enable_gpu_weighted_prediction = 0,
445 .no_output_of_prior_pics_flag = 0,
446 },
447 };
448
449 if (pps->tiles_enabled_flag) {
450 for (i = 0; i <= vpic->num_tile_rows_minus1; i++)
451 vpic->row_height_minus1[i] = pps->row_height_minus1[i];
452 for (i = 0; i <= vpic->num_tile_columns_minus1; i++)
453 vpic->column_width_minus1[i] = pps->column_width_minus1[i];
454 }
455
456 return 0;
457 }
458
459 static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
460 FFHWBaseEncodePicture *pic)
461 {
462 FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
463 VAAPIEncodeH265Context *priv = avctx->priv_data;
464 VAAPIEncodePicture *vaapi_pic = pic->priv;
465 VAAPIEncodeH265Picture *hpic = pic->codec_priv;
466 FFHWBaseEncodePicture *prev = pic->prev;
467 VAAPIEncodeH265Picture *hprev = prev ? prev->codec_priv : NULL;
468 VAEncPictureParameterBufferHEVC *vpic = vaapi_pic->codec_picture_params;
469 int i, j = 0;
470
471 if (pic->type == FF_HW_PICTURE_TYPE_IDR) {
472 av_assert0(pic->display_order == pic->encode_order);
473
474 hpic->last_idr_frame = pic->display_order;
475
476 hpic->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
477 hpic->slice_type = HEVC_SLICE_I;
478 hpic->pic_type = 0;
479 } else {
480 av_assert0(prev);
481 hpic->last_idr_frame = hprev->last_idr_frame;
482
483 if (pic->type == FF_HW_PICTURE_TYPE_I) {
484 hpic->slice_nal_unit = HEVC_NAL_CRA_NUT;
485 hpic->slice_type = HEVC_SLICE_I;
486 hpic->pic_type = 0;
487 } else if (pic->type == FF_HW_PICTURE_TYPE_P) {
488 av_assert0(pic->refs[0]);
489 hpic->slice_nal_unit = HEVC_NAL_TRAIL_R;
490 hpic->slice_type = HEVC_SLICE_P;
491 hpic->pic_type = 1;
492 } else {
493 FFHWBaseEncodePicture *irap_ref;
494 av_assert0(pic->refs[0][0] && pic->refs[1][0]);
495 for (irap_ref = pic; irap_ref; irap_ref = irap_ref->refs[1][0]) {
496 if (irap_ref->type == FF_HW_PICTURE_TYPE_I)
497 break;
498 }
499 if (pic->b_depth == base_ctx->max_b_depth) {
500 hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_N
501 : HEVC_NAL_TRAIL_N;
502 } else {
503 hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_R
504 : HEVC_NAL_TRAIL_R;
505 }
506 hpic->slice_type = HEVC_SLICE_B;
507 hpic->pic_type = 2;
508 }
509 }
510 hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame;
511
512 if (priv->aud) {
513 priv->aud_needed = 1;
514 priv->raw_aud = (H265RawAUD) {
515 .nal_unit_header = {
516 .nal_unit_type = HEVC_NAL_AUD,
517 .nuh_layer_id = 0,
518 .nuh_temporal_id_plus1 = 1,
519 },
520 .pic_type = hpic->pic_type,
521 };
522 } else {
523 priv->aud_needed = 0;
524 }
525
526 priv->sei_needed = 0;
527
528 // Only look for the metadata on I/IDR frame on the output. We
529 // may force an IDR frame on the output where the medadata gets
530 // changed on the input frame.
531 if ((priv->sei & SEI_MASTERING_DISPLAY) &&
532 (pic->type == FF_HW_PICTURE_TYPE_I || pic->type == FF_HW_PICTURE_TYPE_IDR)) {
533 AVFrameSideData *sd =
534 av_frame_get_side_data(pic->input_image,
535 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
536
537 if (sd) {
538 AVMasteringDisplayMetadata *mdm =
539 (AVMasteringDisplayMetadata *)sd->data;
540
541 // SEI is needed when both the primaries and luminance are set
542 if (mdm->has_primaries && mdm->has_luminance) {
543 SEIRawMasteringDisplayColourVolume *mdcv =
544 &priv->sei_mastering_display;
545 const int mapping[3] = {1, 2, 0};
546 const int chroma_den = 50000;
547 const int luma_den = 10000;
548
549 for (i = 0; i < 3; i++) {
550 const int j = mapping[i];
551 mdcv->display_primaries_x[i] =
552 FFMIN(lrint(chroma_den *
553 av_q2d(mdm->display_primaries[j][0])),
554 chroma_den);
555 mdcv->display_primaries_y[i] =
556 FFMIN(lrint(chroma_den *
557 av_q2d(mdm->display_primaries[j][1])),
558 chroma_den);
559 }
560
561 mdcv->white_point_x =
562 FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
563 chroma_den);
564 mdcv->white_point_y =
565 FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
566 chroma_den);
567
568 mdcv->max_display_mastering_luminance =
569 lrint(luma_den * av_q2d(mdm->max_luminance));
570 mdcv->min_display_mastering_luminance =
571 FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
572 mdcv->max_display_mastering_luminance);
573
574 priv->sei_needed |= SEI_MASTERING_DISPLAY;
575 }
576 }
577 }
578
579 if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
580 (pic->type == FF_HW_PICTURE_TYPE_I || pic->type == FF_HW_PICTURE_TYPE_IDR)) {
581 AVFrameSideData *sd =
582 av_frame_get_side_data(pic->input_image,
583 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
584
585 if (sd) {
586 AVContentLightMetadata *clm =
587 (AVContentLightMetadata *)sd->data;
588 SEIRawContentLightLevelInfo *clli =
589 &priv->sei_content_light_level;
590
591 clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535);
592 clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
593
594 priv->sei_needed |= SEI_CONTENT_LIGHT_LEVEL;
595 }
596 }
597
598 if (priv->sei & SEI_A53_CC) {
599 int err;
600 size_t sei_a53cc_len;
601 av_freep(&priv->sei_a53cc_data);
602 err = ff_alloc_a53_sei(pic->input_image, 0, &priv->sei_a53cc_data, &sei_a53cc_len);
603 if (err < 0)
604 return err;
605 if (priv->sei_a53cc_data != NULL) {
606 priv->sei_a53cc.itu_t_t35_country_code = 181;
607 priv->sei_a53cc.data = (uint8_t *)priv->sei_a53cc_data + 1;
608 priv->sei_a53cc.data_length = sei_a53cc_len - 1;
609
610 priv->sei_needed |= SEI_A53_CC;
611 }
612 }
613
614 vpic->decoded_curr_pic = (VAPictureHEVC) {
615 .picture_id = vaapi_pic->recon_surface,
616 .pic_order_cnt = hpic->pic_order_cnt,
617 .flags = 0,
618 };
619
620 for (int k = 0; k < MAX_REFERENCE_LIST_NUM; k++) {
621 for (i = 0; i < pic->nb_refs[k]; i++) {
622 FFHWBaseEncodePicture *ref = pic->refs[k][i];
623 VAAPIEncodeH265Picture *href;
624
625 av_assert0(ref && ref->encode_order < pic->encode_order);
626 href = ref->codec_priv;
627
628 vpic->reference_frames[j++] = (VAPictureHEVC) {
629 .picture_id = ((VAAPIEncodePicture *)ref->priv)->recon_surface,
630 .pic_order_cnt = href->pic_order_cnt,
631 .flags = (ref->display_order < pic->display_order ?
632 VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
633 (ref->display_order > pic->display_order ?
634 VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
635 };
636 }
637 }
638
639 for (; j < FF_ARRAY_ELEMS(vpic->reference_frames); j++) {
640 vpic->reference_frames[j] = (VAPictureHEVC) {
641 .picture_id = VA_INVALID_ID,
642 .flags = VA_PICTURE_HEVC_INVALID,
643 };
644 }
645
646 vpic->coded_buf = vaapi_pic->output_buffer;
647
648 vpic->nal_unit_type = hpic->slice_nal_unit;
649
650 vpic->pic_fields.bits.reference_pic_flag = pic->is_reference;
651 switch (pic->type) {
652 case FF_HW_PICTURE_TYPE_IDR:
653 vpic->pic_fields.bits.idr_pic_flag = 1;
654 vpic->pic_fields.bits.coding_type = 1;
655 break;
656 case FF_HW_PICTURE_TYPE_I:
657 vpic->pic_fields.bits.idr_pic_flag = 0;
658 vpic->pic_fields.bits.coding_type = 1;
659 break;
660 case FF_HW_PICTURE_TYPE_P:
661 vpic->pic_fields.bits.idr_pic_flag = 0;
662 vpic->pic_fields.bits.coding_type = 2;
663 break;
664 case FF_HW_PICTURE_TYPE_B:
665 vpic->pic_fields.bits.idr_pic_flag = 0;
666 vpic->pic_fields.bits.coding_type = 3;
667 break;
668 default:
669 av_assert0(0 && "invalid picture type");
670 }
671
672 return 0;
673 }
674
675 static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
676 FFHWBaseEncodePicture *pic,
677 VAAPIEncodeSlice *slice)
678 {
679 FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
680 VAAPIEncodeH265Context *priv = avctx->priv_data;
681 VAAPIEncodePicture *vaapi_pic = pic->priv;
682 VAAPIEncodeH265Picture *hpic = pic->codec_priv;
683 const H265RawSPS *sps = &priv->units.raw_sps;
684 const H265RawPPS *pps = &priv->units.raw_pps;
685 H265RawSliceHeader *sh = &priv->raw_slice.header;
686 VAEncPictureParameterBufferHEVC *vpic = vaapi_pic->codec_picture_params;
687 VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
688 int i;
689
690 sh->nal_unit_header = (H265RawNALUnitHeader) {
691 .nal_unit_type = hpic->slice_nal_unit,
692 .nuh_layer_id = 0,
693 .nuh_temporal_id_plus1 = 1,
694 };
695
696 sh->slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id;
697
698 sh->first_slice_segment_in_pic_flag = slice->index == 0;
699 sh->slice_segment_address = slice->block_start;
700
701 sh->slice_type = hpic->slice_type;
702
703 if (sh->slice_type == HEVC_SLICE_P && base_ctx->p_to_gpb)
704 sh->slice_type = HEVC_SLICE_B;
705
706 sh->slice_pic_order_cnt_lsb = hpic->pic_order_cnt &
707 (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
708
709 if (pic->type != FF_HW_PICTURE_TYPE_IDR) {
710 H265RawSTRefPicSet *rps;
711 const VAAPIEncodeH265Picture *strp;
712 int rps_poc[MAX_DPB_SIZE];
713 int rps_used[MAX_DPB_SIZE];
714 int i, j, poc, rps_pics;
715
716 sh->short_term_ref_pic_set_sps_flag = 0;
717
718 rps = &sh->short_term_ref_pic_set;
719 memset(rps, 0, sizeof(*rps));
720
721 rps_pics = 0;
722 for (i = 0; i < MAX_REFERENCE_LIST_NUM; i++) {
723 for (j = 0; j < pic->nb_refs[i]; j++) {
724 strp = pic->refs[i][j]->codec_priv;
725 rps_poc[rps_pics] = strp->pic_order_cnt;
726 rps_used[rps_pics] = 1;
727 ++rps_pics;
728 }
729 }
730
731 for (i = 0; i < pic->nb_dpb_pics; i++) {
732 if (pic->dpb[i] == pic)
733 continue;
734
735 for (j = 0; j < pic->nb_refs[0]; j++) {
736 if (pic->dpb[i] == pic->refs[0][j])
737 break;
738 }
739 if (j < pic->nb_refs[0])
740 continue;
741
742 for (j = 0; j < pic->nb_refs[1]; j++) {
743 if (pic->dpb[i] == pic->refs[1][j])
744 break;
745 }
746 if (j < pic->nb_refs[1])
747 continue;
748
749 strp = pic->dpb[i]->codec_priv;
750 rps_poc[rps_pics] = strp->pic_order_cnt;
751 rps_used[rps_pics] = 0;
752 ++rps_pics;
753 }
754
755 for (i = 1; i < rps_pics; i++) {
756 for (j = i; j > 0; j--) {
757 if (rps_poc[j] > rps_poc[j - 1])
758 break;
759 av_assert0(rps_poc[j] != rps_poc[j - 1]);
760 FFSWAP(int, rps_poc[j], rps_poc[j - 1]);
761 FFSWAP(int, rps_used[j], rps_used[j - 1]);
762 }
763 }
764
765 av_log(avctx, AV_LOG_DEBUG, "RPS for POC %d:",
766 hpic->pic_order_cnt);
767 for (i = 0; i < rps_pics; i++) {
768 av_log(avctx, AV_LOG_DEBUG, " (%d,%d)",
769 rps_poc[i], rps_used[i]);
770 }
771 av_log(avctx, AV_LOG_DEBUG, "\n");
772
773 for (i = 0; i < rps_pics; i++) {
774 av_assert0(rps_poc[i] != hpic->pic_order_cnt);
775 if (rps_poc[i] > hpic->pic_order_cnt)
776 break;
777 }
778
779 rps->num_negative_pics = i;
780 poc = hpic->pic_order_cnt;
781 for (j = i - 1; j >= 0; j--) {
782 rps->delta_poc_s0_minus1[i - 1 - j] = poc - rps_poc[j] - 1;
783 rps->used_by_curr_pic_s0_flag[i - 1 - j] = rps_used[j];
784 poc = rps_poc[j];
785 }
786
787 rps->num_positive_pics = rps_pics - i;
788 poc = hpic->pic_order_cnt;
789 for (j = i; j < rps_pics; j++) {
790 rps->delta_poc_s1_minus1[j - i] = rps_poc[j] - poc - 1;
791 rps->used_by_curr_pic_s1_flag[j - i] = rps_used[j];
792 poc = rps_poc[j];
793 }
794
795 sh->num_long_term_sps = 0;
796 sh->num_long_term_pics = 0;
797
798 // when this flag is not present, it is inerred to 1.
799 sh->collocated_from_l0_flag = 1;
800 sh->slice_temporal_mvp_enabled_flag =
801 sps->sps_temporal_mvp_enabled_flag;
802 if (sh->slice_temporal_mvp_enabled_flag) {
803 if (sh->slice_type == HEVC_SLICE_B)
804 sh->collocated_from_l0_flag = 1;
805 sh->collocated_ref_idx = 0;
806 }
807
808 sh->num_ref_idx_active_override_flag = 0;
809 sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
810 sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
811 }
812
813 sh->slice_sao_luma_flag = sh->slice_sao_chroma_flag =
814 sps->sample_adaptive_offset_enabled_flag;
815
816 if (pic->type == FF_HW_PICTURE_TYPE_B)
817 sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
818 else if (pic->type == FF_HW_PICTURE_TYPE_P)
819 sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
820 else
821 sh->slice_qp_delta = priv->unit_opts.fixed_qp_idr - (pps->init_qp_minus26 + 26);
822
823
824 *vslice = (VAEncSliceParameterBufferHEVC) {
825 .slice_segment_address = sh->slice_segment_address,
826 .num_ctu_in_slice = slice->block_size,
827
828 .slice_type = sh->slice_type,
829 .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
830
831 .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
832 .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
833
834 .luma_log2_weight_denom = sh->luma_log2_weight_denom,
835 .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
836
837 .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
838
839 .slice_qp_delta = sh->slice_qp_delta,
840 .slice_cb_qp_offset = sh->slice_cb_qp_offset,
841 .slice_cr_qp_offset = sh->slice_cr_qp_offset,
842
843 .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
844 .slice_tc_offset_div2 = sh->slice_tc_offset_div2,
845
846 .slice_fields.bits = {
847 .last_slice_of_pic_flag = slice->index == vaapi_pic->nb_slices - 1,
848 .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
849 .colour_plane_id = sh->colour_plane_id,
850 .slice_temporal_mvp_enabled_flag =
851 sh->slice_temporal_mvp_enabled_flag,
852 .slice_sao_luma_flag = sh->slice_sao_luma_flag,
853 .slice_sao_chroma_flag = sh->slice_sao_chroma_flag,
854 .num_ref_idx_active_override_flag =
855 sh->num_ref_idx_active_override_flag,
856 .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
857 .cabac_init_flag = sh->cabac_init_flag,
858 .slice_deblocking_filter_disabled_flag =
859 sh->slice_deblocking_filter_disabled_flag,
860 .slice_loop_filter_across_slices_enabled_flag =
861 sh->slice_loop_filter_across_slices_enabled_flag,
862 .collocated_from_l0_flag = sh->collocated_from_l0_flag,
863 },
864 };
865
866 for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
867 vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
868 vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
869 vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
870 vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
871 }
872
873 if (pic->nb_refs[0]) {
874 // Backward reference for P- or B-frame.
875 av_assert0(pic->type == FF_HW_PICTURE_TYPE_P ||
876 pic->type == FF_HW_PICTURE_TYPE_B);
877 vslice->ref_pic_list0[0] = vpic->reference_frames[0];
878 if (base_ctx->p_to_gpb && pic->type == FF_HW_PICTURE_TYPE_P)
879 // Reference for GPB B-frame, L0 == L1
880 vslice->ref_pic_list1[0] = vpic->reference_frames[0];
881 }
882 if (pic->nb_refs[1]) {
883 // Forward reference for B-frame.
884 av_assert0(pic->type == FF_HW_PICTURE_TYPE_B);
885 vslice->ref_pic_list1[0] = vpic->reference_frames[1];
886 }
887
888 if (pic->type == FF_HW_PICTURE_TYPE_P && base_ctx->p_to_gpb) {
889 vslice->slice_type = HEVC_SLICE_B;
890 for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
891 vslice->ref_pic_list1[i].picture_id = vslice->ref_pic_list0[i].picture_id;
892 vslice->ref_pic_list1[i].flags = vslice->ref_pic_list0[i].flags;
893 }
894 }
895
896 return 0;
897 }
898
899 static av_cold int vaapi_encode_h265_get_encoder_caps(AVCodecContext *avctx)
900 {
901 FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
902 VAAPIEncodeH265Context *priv = avctx->priv_data;
903
904 #if VA_CHECK_VERSION(1, 13, 0)
905 {
906 VAAPIEncodeContext *ctx = avctx->priv_data;
907 VAConfigAttribValEncHEVCBlockSizes block_size;
908 VAConfigAttrib attr;
909 VAStatus vas;
910
911 attr.type = VAConfigAttribEncHEVCFeatures;
912 vas = vaGetConfigAttributes(ctx->hwctx->display, ctx->va_profile,
913 ctx->va_entrypoint, &attr, 1);
914 if (vas != VA_STATUS_SUCCESS) {
915 av_log(avctx, AV_LOG_ERROR, "Failed to query encoder "
916 "features, using guessed defaults.\n");
917 return AVERROR_EXTERNAL;
918 } else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
919 av_log(avctx, AV_LOG_WARNING, "Driver does not advertise "
920 "encoder features, using guessed defaults.\n");
921 } else {
922 priv->va_features = attr.value;
923 }
924
925 attr.type = VAConfigAttribEncHEVCBlockSizes;
926 vas = vaGetConfigAttributes(ctx->hwctx->display, ctx->va_profile,
927 ctx->va_entrypoint, &attr, 1);
928 if (vas != VA_STATUS_SUCCESS) {
929 av_log(avctx, AV_LOG_ERROR, "Failed to query encoder "
930 "block size, using guessed defaults.\n");
931 return AVERROR_EXTERNAL;
932 } else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
933 av_log(avctx, AV_LOG_WARNING, "Driver does not advertise "
934 "encoder block size, using guessed defaults.\n");
935 } else {
936 priv->va_bs = block_size.value = attr.value;
937
938 priv->ctu_size =
939 1 << block_size.bits.log2_max_coding_tree_block_size_minus3 + 3;
940 priv->min_cb_size =
941 1 << block_size.bits.log2_min_luma_coding_block_size_minus3 + 3;
942 }
943 }
944 #endif
945
946 if (!priv->ctu_size) {
947 priv->ctu_size = 32;
948 priv->min_cb_size = 16;
949 }
950 av_log(avctx, AV_LOG_VERBOSE, "Using CTU size %dx%d, "
951 "min CB size %dx%d.\n", priv->ctu_size, priv->ctu_size,
952 priv->min_cb_size, priv->min_cb_size);
953
954 base_ctx->surface_width = FFALIGN(avctx->width, priv->min_cb_size);
955 base_ctx->surface_height = FFALIGN(avctx->height, priv->min_cb_size);
956
957 base_ctx->slice_block_width = base_ctx->slice_block_height = priv->ctu_size;
958
959 return 0;
960 }
961
962 static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
963 {
964 VAAPIEncodeContext *ctx = avctx->priv_data;
965 VAAPIEncodeH265Context *priv = avctx->priv_data;
966 int err;
967
968 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
969 if (err < 0)
970 return err;
971
972 if (ctx->va_rc_mode == VA_RC_CQP) {
973 // Note that VAAPI only supports positive QP values - the range is
974 // therefore always bounded below by 1, even in 10-bit mode where
975 // it should go down to -12.
976
977 priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
978 if (avctx->i_quant_factor > 0.0)
979 priv->unit_opts.fixed_qp_idr =
980 av_clip((avctx->i_quant_factor * priv->fixed_qp_p +
981 avctx->i_quant_offset) + 0.5, 1, 51);
982 else
983 priv->unit_opts.fixed_qp_idr = priv->fixed_qp_p;
984 if (avctx->b_quant_factor > 0.0)
985 priv->fixed_qp_b =
986 av_clip((avctx->b_quant_factor * priv->fixed_qp_p +
987 avctx->b_quant_offset) + 0.5, 1, 51);
988 else
989 priv->fixed_qp_b = priv->fixed_qp_p;
990
991 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
992 "%d / %d / %d for IDR- / P- / B-frames.\n",
993 priv->unit_opts.fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
994
995 } else {
996 // These still need to be set for init_qp/slice_qp_delta.
997 priv->unit_opts.fixed_qp_idr = 30;
998 priv->fixed_qp_p = 30;
999 priv->fixed_qp_b = 30;
1000 }
1001
1002 ctx->roi_quant_range = 51 + 6 * (ctx->profile->depth - 8);
1003
1004 return 0;
1005 }
1006
1007 static const VAAPIEncodeProfile vaapi_encode_h265_profiles[] = {
1008 { AV_PROFILE_HEVC_MAIN, 8, 3, 1, 1, VAProfileHEVCMain },
1009 { AV_PROFILE_HEVC_REXT, 8, 3, 1, 1, VAProfileHEVCMain },
1010 #if VA_CHECK_VERSION(0, 37, 0)
1011 { AV_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10 },
1012 { AV_PROFILE_HEVC_REXT, 10, 3, 1, 1, VAProfileHEVCMain10 },
1013 #endif
1014 #if VA_CHECK_VERSION(1, 2, 0)
1015 { AV_PROFILE_HEVC_REXT, 12, 3, 1, 1, VAProfileHEVCMain12 },
1016 { AV_PROFILE_HEVC_REXT, 8, 3, 1, 0, VAProfileHEVCMain422_10 },
1017 { AV_PROFILE_HEVC_REXT, 10, 3, 1, 0, VAProfileHEVCMain422_10 },
1018 { AV_PROFILE_HEVC_REXT, 12, 3, 1, 0, VAProfileHEVCMain422_12 },
1019 { AV_PROFILE_HEVC_REXT, 8, 3, 0, 0, VAProfileHEVCMain444 },
1020 { AV_PROFILE_HEVC_REXT, 10, 3, 0, 0, VAProfileHEVCMain444_10 },
1021 { AV_PROFILE_HEVC_REXT, 12, 3, 0, 0, VAProfileHEVCMain444_12 },
1022 #endif
1023 { AV_PROFILE_UNKNOWN }
1024 };
1025
1026 static const VAAPIEncodeType vaapi_encode_type_h265 = {
1027 .profiles = vaapi_encode_h265_profiles,
1028
1029 .flags = FF_HW_FLAG_SLICE_CONTROL |
1030 FF_HW_FLAG_B_PICTURES |
1031 FF_HW_FLAG_B_PICTURE_REFERENCES |
1032 FF_HW_FLAG_NON_IDR_KEY_PICTURES,
1033
1034 .default_quality = 25,
1035
1036 .get_encoder_caps = &vaapi_encode_h265_get_encoder_caps,
1037 .configure = &vaapi_encode_h265_configure,
1038
1039 .picture_priv_data_size = sizeof(VAAPIEncodeH265Picture),
1040
1041 .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
1042 .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
1043
1044 .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
1045 .init_picture_params = &vaapi_encode_h265_init_picture_params,
1046
1047 .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
1048 .init_slice_params = &vaapi_encode_h265_init_slice_params,
1049
1050 .sequence_header_type = VAEncPackedHeaderSequence,
1051 .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
1052
1053 .slice_header_type = VAEncPackedHeaderHEVC_Slice,
1054 .write_slice_header = &vaapi_encode_h265_write_slice_header,
1055
1056 .write_extra_header = &vaapi_encode_h265_write_extra_header,
1057 };
1058
1059 static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
1060 {
1061 VAAPIEncodeContext *ctx = avctx->priv_data;
1062 VAAPIEncodeH265Context *priv = avctx->priv_data;
1063
1064 ctx->codec = &vaapi_encode_type_h265;
1065
1066 if (avctx->profile == AV_PROFILE_UNKNOWN)
1067 avctx->profile = priv->profile;
1068 if (avctx->level == AV_LEVEL_UNKNOWN)
1069 avctx->level = priv->level;
1070
1071 if (avctx->level != AV_LEVEL_UNKNOWN && avctx->level & ~0xff) {
1072 av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
1073 "in 8-bit unsigned integer.\n", avctx->level);
1074 return AVERROR(EINVAL);
1075 }
1076
1077 ctx->desired_packed_headers =
1078 VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
1079 VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
1080 VA_ENC_PACKED_HEADER_MISC; // SEI
1081
1082 if (priv->qp > 0)
1083 ctx->explicit_qp = priv->qp;
1084
1085 return ff_vaapi_encode_init(avctx);
1086 }
1087
1088 static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
1089 {
1090 VAAPIEncodeH265Context *priv = avctx->priv_data;
1091
1092 ff_cbs_fragment_free(&priv->current_access_unit);
1093 ff_cbs_close(&priv->cbc);
1094 av_freep(&priv->sei_a53cc_data);
1095
1096 return ff_vaapi_encode_close(avctx);
1097 }
1098
1099 #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
1100 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1101 static const AVOption vaapi_encode_h265_options[] = {
1102 HW_BASE_ENCODE_COMMON_OPTIONS,
1103 VAAPI_ENCODE_COMMON_OPTIONS,
1104 VAAPI_ENCODE_RC_OPTIONS,
1105
1106 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1107 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
1108
1109 { "aud", "Include AUD",
1110 OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1111
1112 { "profile", "Set profile (general_profile_idc)",
1113 OFFSET(profile), AV_OPT_TYPE_INT,
1114 { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xff, FLAGS, .unit = "profile" },
1115
1116 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1117 { .i64 = value }, 0, 0, FLAGS, .unit = "profile"
1118 { PROFILE("main", AV_PROFILE_HEVC_MAIN) },
1119 { PROFILE("main10", AV_PROFILE_HEVC_MAIN_10) },
1120 { PROFILE("rext", AV_PROFILE_HEVC_REXT) },
1121 #undef PROFILE
1122
1123 { "tier", "Set tier (general_tier_flag)",
1124 OFFSET(unit_opts.tier), AV_OPT_TYPE_INT,
1125 { .i64 = 0 }, 0, 1, FLAGS, .unit = "tier" },
1126 { "main", NULL, 0, AV_OPT_TYPE_CONST,
1127 { .i64 = 0 }, 0, 0, FLAGS, .unit = "tier" },
1128 { "high", NULL, 0, AV_OPT_TYPE_CONST,
1129 { .i64 = 1 }, 0, 0, FLAGS, .unit = "tier" },
1130
1131 { "level", "Set level (general_level_idc)",
1132 OFFSET(level), AV_OPT_TYPE_INT,
1133 { .i64 = AV_LEVEL_UNKNOWN }, AV_LEVEL_UNKNOWN, 0xff, FLAGS, .unit = "level" },
1134
1135 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1136 { .i64 = value }, 0, 0, FLAGS, .unit = "level"
1137 { LEVEL("1", 30) },
1138 { LEVEL("2", 60) },
1139 { LEVEL("2.1", 63) },
1140 { LEVEL("3", 90) },
1141 { LEVEL("3.1", 93) },
1142 { LEVEL("4", 120) },
1143 { LEVEL("4.1", 123) },
1144 { LEVEL("5", 150) },
1145 { LEVEL("5.1", 153) },
1146 { LEVEL("5.2", 156) },
1147 { LEVEL("6", 180) },
1148 { LEVEL("6.1", 183) },
1149 { LEVEL("6.2", 186) },
1150 #undef LEVEL
1151
1152 { "sei", "Set SEI to include",
1153 OFFSET(sei), AV_OPT_TYPE_FLAGS,
1154 { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL | SEI_A53_CC },
1155 0, INT_MAX, FLAGS, .unit = "sei" },
1156 { "hdr",
1157 "Include HDR metadata for mastering display colour volume "
1158 "and content light level information",
1159 0, AV_OPT_TYPE_CONST,
1160 { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
1161 INT_MIN, INT_MAX, FLAGS, .unit = "sei" },
1162 { "a53_cc",
1163 "Include A/53 caption data",
1164 0, AV_OPT_TYPE_CONST,
1165 { .i64 = SEI_A53_CC },
1166 INT_MIN, INT_MAX, FLAGS, .unit = "sei" },
1167
1168 { "tiles", "Tile columns x rows",
1169 OFFSET(common.tile_cols), AV_OPT_TYPE_IMAGE_SIZE,
1170 { .str = NULL }, 0, 0, FLAGS },
1171
1172 { NULL },
1173 };
1174
1175 static const FFCodecDefault vaapi_encode_h265_defaults[] = {
1176 { "b", "0" },
1177 { "bf", "2" },
1178 { "g", "120" },
1179 { "i_qfactor", "1" },
1180 { "i_qoffset", "0" },
1181 { "b_qfactor", "6/5" },
1182 { "b_qoffset", "0" },
1183 { "qmin", "-1" },
1184 { "qmax", "-1" },
1185 { NULL },
1186 };
1187
1188 static const AVClass vaapi_encode_h265_class = {
1189 .class_name = "h265_vaapi",
1190 .item_name = av_default_item_name,
1191 .option = vaapi_encode_h265_options,
1192 .version = LIBAVUTIL_VERSION_INT,
1193 };
1194
1195 const FFCodec ff_hevc_vaapi_encoder = {
1196 .p.name = "hevc_vaapi",
1197 CODEC_LONG_NAME("H.265/HEVC (VAAPI)"),
1198 .p.type = AVMEDIA_TYPE_VIDEO,
1199 .p.id = AV_CODEC_ID_HEVC,
1200 .priv_data_size = sizeof(VAAPIEncodeH265Context),
1201 .init = &vaapi_encode_h265_init,
1202 FF_CODEC_RECEIVE_PACKET_CB(&ff_vaapi_encode_receive_packet),
1203 .close = &vaapi_encode_h265_close,
1204 .p.priv_class = &vaapi_encode_h265_class,
1205 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
1206 AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1207 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
1208 FF_CODEC_CAP_INIT_CLEANUP,
1209 .defaults = vaapi_encode_h265_defaults,
1210 .p.pix_fmts = (const enum AVPixelFormat[]) {
1211 AV_PIX_FMT_VAAPI,
1212 AV_PIX_FMT_NONE,
1213 },
1214 .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG,
1215 .hw_configs = ff_vaapi_encode_hw_configs,
1216 .p.wrapper_name = "vaapi",
1217 };
1218