FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vaapi_encode_h265.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 0 741 0.0%
Functions: 0 12 0.0%
Branches: 0 331 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 "codec_internal.h"
36 #include "h2645data.h"
37 #include "h265_profile_level.h"
38 #include "hevc.h"
39 #include "vaapi_encode.h"
40
41 enum {
42 SEI_MASTERING_DISPLAY = 0x08,
43 SEI_CONTENT_LIGHT_LEVEL = 0x10,
44 SEI_A53_CC = 0x20,
45 };
46
47 typedef struct VAAPIEncodeH265Picture {
48 int pic_order_cnt;
49
50 int64_t last_idr_frame;
51
52 int slice_nal_unit;
53 int slice_type;
54 int pic_type;
55 } VAAPIEncodeH265Picture;
56
57 typedef struct VAAPIEncodeH265Context {
58 VAAPIEncodeContext common;
59
60 // Encoder features.
61 uint32_t va_features;
62 // Block size info.
63 uint32_t va_bs;
64 uint32_t ctu_size;
65 uint32_t min_cb_size;
66
67 // User options.
68 int qp;
69 int aud;
70 int profile;
71 int tier;
72 int level;
73 int sei;
74
75 // Derived settings.
76 int fixed_qp_idr;
77 int fixed_qp_p;
78 int fixed_qp_b;
79
80 // Writer structures.
81 H265RawAUD raw_aud;
82 H265RawVPS raw_vps;
83 H265RawSPS raw_sps;
84 H265RawPPS raw_pps;
85 H265RawSlice raw_slice;
86
87 SEIRawMasteringDisplayColourVolume sei_mastering_display;
88 SEIRawContentLightLevelInfo sei_content_light_level;
89 SEIRawUserDataRegistered sei_a53cc;
90 void *sei_a53cc_data;
91
92 CodedBitstreamContext *cbc;
93 CodedBitstreamFragment current_access_unit;
94 int aud_needed;
95 int sei_needed;
96 } VAAPIEncodeH265Context;
97
98
99 static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx,
100 char *data, size_t *data_len,
101 CodedBitstreamFragment *au)
102 {
103 VAAPIEncodeH265Context *priv = avctx->priv_data;
104 int err;
105
106 err = ff_cbs_write_fragment_data(priv->cbc, au);
107 if (err < 0) {
108 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
109 return err;
110 }
111
112 if (*data_len < 8 * au->data_size - au->data_bit_padding) {
113 av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
114 "%zu < %zu.\n", *data_len,
115 8 * au->data_size - au->data_bit_padding);
116 return AVERROR(ENOSPC);
117 }
118
119 memcpy(data, au->data, au->data_size);
120 *data_len = 8 * au->data_size - au->data_bit_padding;
121
122 return 0;
123 }
124
125 static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
126 CodedBitstreamFragment *au,
127 void *nal_unit)
128 {
129 H265RawNALUnitHeader *header = nal_unit;
130 int err;
131
132 err = ff_cbs_insert_unit_content(au, -1,
133 header->nal_unit_type, nal_unit, NULL);
134 if (err < 0) {
135 av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
136 "type = %d.\n", header->nal_unit_type);
137 return err;
138 }
139
140 return 0;
141 }
142
143 static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
144 char *data, size_t *data_len)
145 {
146 VAAPIEncodeH265Context *priv = avctx->priv_data;
147 CodedBitstreamFragment *au = &priv->current_access_unit;
148 int err;
149
150 if (priv->aud_needed) {
151 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
152 if (err < 0)
153 goto fail;
154 priv->aud_needed = 0;
155 }
156
157 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_vps);
158 if (err < 0)
159 goto fail;
160
161 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_sps);
162 if (err < 0)
163 goto fail;
164
165 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_pps);
166 if (err < 0)
167 goto fail;
168
169 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
170 fail:
171 ff_cbs_fragment_reset(au);
172 return err;
173 }
174
175 static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
176 VAAPIEncodePicture *pic,
177 VAAPIEncodeSlice *slice,
178 char *data, size_t *data_len)
179 {
180 VAAPIEncodeH265Context *priv = avctx->priv_data;
181 CodedBitstreamFragment *au = &priv->current_access_unit;
182 int err;
183
184 if (priv->aud_needed) {
185 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
186 if (err < 0)
187 goto fail;
188 priv->aud_needed = 0;
189 }
190
191 err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_slice);
192 if (err < 0)
193 goto fail;
194
195 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
196 fail:
197 ff_cbs_fragment_reset(au);
198 return err;
199 }
200
201 static int vaapi_encode_h265_write_extra_header(AVCodecContext *avctx,
202 VAAPIEncodePicture *pic,
203 int index, int *type,
204 char *data, size_t *data_len)
205 {
206 VAAPIEncodeH265Context *priv = avctx->priv_data;
207 CodedBitstreamFragment *au = &priv->current_access_unit;
208 int err;
209
210 if (priv->sei_needed) {
211 if (priv->aud_needed) {
212 err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
213 if (err < 0)
214 goto fail;
215 priv->aud_needed = 0;
216 }
217
218 if (priv->sei_needed & SEI_MASTERING_DISPLAY) {
219 err = ff_cbs_sei_add_message(priv->cbc, au, 1,
220 SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME,
221 &priv->sei_mastering_display, NULL);
222 if (err < 0)
223 goto fail;
224 }
225
226 if (priv->sei_needed & SEI_CONTENT_LIGHT_LEVEL) {
227 err = ff_cbs_sei_add_message(priv->cbc, au, 1,
228 SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO,
229 &priv->sei_content_light_level, NULL);
230 if (err < 0)
231 goto fail;
232 }
233 if (priv->sei_needed & SEI_A53_CC) {
234 err = ff_cbs_sei_add_message(priv->cbc, au, 1,
235 SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35,
236 &priv->sei_a53cc, NULL);
237 if (err < 0)
238 goto fail;
239 }
240
241 priv->sei_needed = 0;
242
243 err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
244 if (err < 0)
245 goto fail;
246
247 ff_cbs_fragment_reset(au);
248
249 *type = VAEncPackedHeaderRawData;
250 return 0;
251 } else {
252 return AVERROR_EOF;
253 }
254
255 fail:
256 ff_cbs_fragment_reset(au);
257 return err;
258 }
259
260 static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
261 {
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;
273 int i;
274
275 memset(vps, 0, sizeof(*vps));
276 memset(sps, 0, sizeof(*sps));
277 memset(pps, 0, sizeof(*pps));
278
279
280 desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format);
281 av_assert0(desc);
282 if (desc->nb_components == 1) {
283 chroma_format = 0;
284 } else {
285 if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1) {
286 chroma_format = 1;
287 } else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0) {
288 chroma_format = 2;
289 } else if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
290 chroma_format = 3;
291 } else {
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);
295 }
296 }
297 bit_depth = desc->comp[0].depth;
298
299
300 // VPS
301
302 vps->nal_unit_header = (H265RawNALUnitHeader) {
303 .nal_unit_type = HEVC_NAL_VPS,
304 .nuh_layer_id = 0,
305 .nuh_temporal_id_plus1 = 1,
306 };
307
308 vps->vps_video_parameter_set_id = 0;
309
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;
315
316 ptl->general_profile_space = 0;
317 ptl->general_profile_idc = avctx->profile;
318 ptl->general_tier_flag = priv->tier;
319
320 ptl->general_profile_compatibility_flag[ptl->general_profile_idc] = 1;
321
322 if (ptl->general_profile_compatibility_flag[1])
323 ptl->general_profile_compatibility_flag[2] = 1;
324 if (ptl->general_profile_compatibility_flag[3]) {
325 ptl->general_profile_compatibility_flag[1] = 1;
326 ptl->general_profile_compatibility_flag[2] = 1;
327 }
328
329 ptl->general_progressive_source_flag = 1;
330 ptl->general_interlaced_source_flag = 0;
331 ptl->general_non_packed_constraint_flag = 1;
332 ptl->general_frame_only_constraint_flag = 1;
333
334 ptl->general_max_14bit_constraint_flag = bit_depth <= 14;
335 ptl->general_max_12bit_constraint_flag = bit_depth <= 12;
336 ptl->general_max_10bit_constraint_flag = bit_depth <= 10;
337 ptl->general_max_8bit_constraint_flag = bit_depth == 8;
338
339 ptl->general_max_422chroma_constraint_flag = chroma_format <= 2;
340 ptl->general_max_420chroma_constraint_flag = chroma_format <= 1;
341 ptl->general_max_monochrome_constraint_flag = chroma_format == 0;
342
343 ptl->general_intra_constraint_flag = ctx->gop_size == 1;
344 ptl->general_one_picture_only_constraint_flag = 0;
345
346 ptl->general_lower_bit_rate_constraint_flag = 1;
347
348 if (avctx->level != AV_LEVEL_UNKNOWN) {
349 ptl->general_level_idc = avctx->level;
350 } else {
351 const H265LevelDescriptor *level;
352
353 level = ff_h265_guess_level(ptl, avctx->bit_rate,
354 ctx->surface_width, ctx->surface_height,
355 ctx->nb_slices, ctx->tile_rows, ctx->tile_cols,
356 (ctx->b_per_p > 0) + 1);
357 if (level) {
358 av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
359 ptl->general_level_idc = level->level_idc;
360 } else {
361 av_log(avctx, AV_LOG_VERBOSE, "Stream will not conform to "
362 "any normal level; using level 8.5.\n");
363 ptl->general_level_idc = 255;
364 // The tier flag must be set in level 8.5.
365 ptl->general_tier_flag = 1;
366 }
367 }
368
369 vps->vps_sub_layer_ordering_info_present_flag = 0;
370 vps->vps_max_dec_pic_buffering_minus1[0] = ctx->max_b_depth + 1;
371 vps->vps_max_num_reorder_pics[0] = ctx->max_b_depth;
372 vps->vps_max_latency_increase_plus1[0] = 0;
373
374 vps->vps_max_layer_id = 0;
375 vps->vps_num_layer_sets_minus1 = 0;
376 vps->layer_id_included_flag[0][0] = 1;
377
378 vps->vps_timing_info_present_flag = 1;
379 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
380 vps->vps_num_units_in_tick = avctx->framerate.den;
381 vps->vps_time_scale = avctx->framerate.num;
382 vps->vps_poc_proportional_to_timing_flag = 1;
383 vps->vps_num_ticks_poc_diff_one_minus1 = 0;
384 } else {
385 vps->vps_num_units_in_tick = avctx->time_base.num;
386 vps->vps_time_scale = avctx->time_base.den;
387 vps->vps_poc_proportional_to_timing_flag = 0;
388 }
389 vps->vps_num_hrd_parameters = 0;
390
391
392 // SPS
393
394 sps->nal_unit_header = (H265RawNALUnitHeader) {
395 .nal_unit_type = HEVC_NAL_SPS,
396 .nuh_layer_id = 0,
397 .nuh_temporal_id_plus1 = 1,
398 };
399
400 sps->sps_video_parameter_set_id = vps->vps_video_parameter_set_id;
401
402 sps->sps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1;
403 sps->sps_temporal_id_nesting_flag = vps->vps_temporal_id_nesting_flag;
404
405 sps->profile_tier_level = vps->profile_tier_level;
406
407 sps->sps_seq_parameter_set_id = 0;
408
409 sps->chroma_format_idc = chroma_format;
410 sps->separate_colour_plane_flag = 0;
411
412 sps->pic_width_in_luma_samples = ctx->surface_width;
413 sps->pic_height_in_luma_samples = ctx->surface_height;
414
415 if (avctx->width != ctx->surface_width ||
416 avctx->height != ctx->surface_height) {
417 sps->conformance_window_flag = 1;
418 sps->conf_win_left_offset = 0;
419 sps->conf_win_right_offset =
420 (ctx->surface_width - avctx->width) >> desc->log2_chroma_w;
421 sps->conf_win_top_offset = 0;
422 sps->conf_win_bottom_offset =
423 (ctx->surface_height - avctx->height) >> desc->log2_chroma_h;
424 } else {
425 sps->conformance_window_flag = 0;
426 }
427
428 sps->bit_depth_luma_minus8 = bit_depth - 8;
429 sps->bit_depth_chroma_minus8 = bit_depth - 8;
430
431 sps->log2_max_pic_order_cnt_lsb_minus4 = 8;
432
433 sps->sps_sub_layer_ordering_info_present_flag =
434 vps->vps_sub_layer_ordering_info_present_flag;
435 for (i = 0; i <= sps->sps_max_sub_layers_minus1; i++) {
436 sps->sps_max_dec_pic_buffering_minus1[i] =
437 vps->vps_max_dec_pic_buffering_minus1[i];
438 sps->sps_max_num_reorder_pics[i] =
439 vps->vps_max_num_reorder_pics[i];
440 sps->sps_max_latency_increase_plus1[i] =
441 vps->vps_max_latency_increase_plus1[i];
442 }
443
444 // These values come from the capabilities of the first encoder
445 // implementation in the i965 driver on Intel Skylake. They may
446 // fail badly with other platforms or drivers.
447 // CTB size from 8x8 to 32x32.
448 sps->log2_min_luma_coding_block_size_minus3 = 0;
449 sps->log2_diff_max_min_luma_coding_block_size = 2;
450 // Transform size from 4x4 to 32x32.
451 sps->log2_min_luma_transform_block_size_minus2 = 0;
452 sps->log2_diff_max_min_luma_transform_block_size = 3;
453 // Full transform hierarchy allowed (2-5).
454 sps->max_transform_hierarchy_depth_inter = 3;
455 sps->max_transform_hierarchy_depth_intra = 3;
456 // AMP works.
457 sps->amp_enabled_flag = 1;
458 // SAO and temporal MVP do not work.
459 sps->sample_adaptive_offset_enabled_flag = 0;
460 sps->sps_temporal_mvp_enabled_flag = 0;
461
462 sps->pcm_enabled_flag = 0;
463
464 // update sps setting according to queried result
465 #if VA_CHECK_VERSION(1, 13, 0)
466 if (priv->va_features) {
467 VAConfigAttribValEncHEVCFeatures features = { .value = priv->va_features };
468
469 // Enable feature if get queried result is VA_FEATURE_SUPPORTED | VA_FEATURE_REQUIRED
470 sps->amp_enabled_flag =
471 !!features.bits.amp;
472 sps->sample_adaptive_offset_enabled_flag =
473 !!features.bits.sao;
474 sps->sps_temporal_mvp_enabled_flag =
475 !!features.bits.temporal_mvp;
476 sps->pcm_enabled_flag =
477 !!features.bits.pcm;
478 }
479
480 if (priv->va_bs) {
481 VAConfigAttribValEncHEVCBlockSizes bs = { .value = priv->va_bs };
482 sps->log2_min_luma_coding_block_size_minus3 =
483 ff_ctz(priv->min_cb_size) - 3;
484 sps->log2_diff_max_min_luma_coding_block_size =
485 ff_ctz(priv->ctu_size) - ff_ctz(priv->min_cb_size);
486
487 sps->log2_min_luma_transform_block_size_minus2 =
488 bs.bits.log2_min_luma_transform_block_size_minus2;
489 sps->log2_diff_max_min_luma_transform_block_size =
490 bs.bits.log2_max_luma_transform_block_size_minus2 -
491 bs.bits.log2_min_luma_transform_block_size_minus2;
492
493 sps->max_transform_hierarchy_depth_inter =
494 bs.bits.max_max_transform_hierarchy_depth_inter;
495 sps->max_transform_hierarchy_depth_intra =
496 bs.bits.max_max_transform_hierarchy_depth_intra;
497 }
498 #endif
499
500 // STRPSs should ideally be here rather than defined individually in
501 // each slice, but the structure isn't completely fixed so for now
502 // don't bother.
503 sps->num_short_term_ref_pic_sets = 0;
504 sps->long_term_ref_pics_present_flag = 0;
505
506 sps->vui_parameters_present_flag = 1;
507
508 if (avctx->sample_aspect_ratio.num != 0 &&
509 avctx->sample_aspect_ratio.den != 0) {
510 int num, den, i;
511 av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
512 avctx->sample_aspect_ratio.den, 65535);
513 for (i = 0; i < FF_ARRAY_ELEMS(ff_h2645_pixel_aspect); i++) {
514 if (num == ff_h2645_pixel_aspect[i].num &&
515 den == ff_h2645_pixel_aspect[i].den) {
516 vui->aspect_ratio_idc = i;
517 break;
518 }
519 }
520 if (i >= FF_ARRAY_ELEMS(ff_h2645_pixel_aspect)) {
521 vui->aspect_ratio_idc = 255;
522 vui->sar_width = num;
523 vui->sar_height = den;
524 }
525 vui->aspect_ratio_info_present_flag = 1;
526 }
527
528 // Unspecified video format, from table E-2.
529 vui->video_format = 5;
530 vui->video_full_range_flag =
531 avctx->color_range == AVCOL_RANGE_JPEG;
532 vui->colour_primaries = avctx->color_primaries;
533 vui->transfer_characteristics = avctx->color_trc;
534 vui->matrix_coefficients = avctx->colorspace;
535 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
536 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
537 avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
538 vui->colour_description_present_flag = 1;
539 if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
540 vui->colour_description_present_flag)
541 vui->video_signal_type_present_flag = 1;
542
543 if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
544 vui->chroma_loc_info_present_flag = 1;
545 vui->chroma_sample_loc_type_top_field =
546 vui->chroma_sample_loc_type_bottom_field =
547 avctx->chroma_sample_location - 1;
548 }
549
550 vui->vui_timing_info_present_flag = 1;
551 vui->vui_num_units_in_tick = vps->vps_num_units_in_tick;
552 vui->vui_time_scale = vps->vps_time_scale;
553 vui->vui_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag;
554 vui->vui_num_ticks_poc_diff_one_minus1 = vps->vps_num_ticks_poc_diff_one_minus1;
555 vui->vui_hrd_parameters_present_flag = 0;
556
557 vui->bitstream_restriction_flag = 1;
558 vui->motion_vectors_over_pic_boundaries_flag = 1;
559 vui->restricted_ref_pic_lists_flag = 1;
560 vui->max_bytes_per_pic_denom = 0;
561 vui->max_bits_per_min_cu_denom = 0;
562 vui->log2_max_mv_length_horizontal = 15;
563 vui->log2_max_mv_length_vertical = 15;
564
565
566 // PPS
567
568 pps->nal_unit_header = (H265RawNALUnitHeader) {
569 .nal_unit_type = HEVC_NAL_PPS,
570 .nuh_layer_id = 0,
571 .nuh_temporal_id_plus1 = 1,
572 };
573
574 pps->pps_pic_parameter_set_id = 0;
575 pps->pps_seq_parameter_set_id = sps->sps_seq_parameter_set_id;
576
577 pps->num_ref_idx_l0_default_active_minus1 = 0;
578 pps->num_ref_idx_l1_default_active_minus1 = 0;
579
580 pps->init_qp_minus26 = priv->fixed_qp_idr - 26;
581
582 pps->cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP);
583 pps->diff_cu_qp_delta_depth = 0;
584
585 // update pps setting according to queried result
586 #if VA_CHECK_VERSION(1, 13, 0)
587 if (priv->va_features) {
588 VAConfigAttribValEncHEVCFeatures features = { .value = priv->va_features };
589 if (ctx->va_rc_mode != VA_RC_CQP)
590 pps->cu_qp_delta_enabled_flag =
591 !!features.bits.cu_qp_delta;
592
593 pps->transform_skip_enabled_flag =
594 !!features.bits.transform_skip;
595 // set diff_cu_qp_delta_depth as its max value if cu_qp_delta enabled. Otherwise
596 // 0 will make cu_qp_delta invalid.
597 if (pps->cu_qp_delta_enabled_flag)
598 pps->diff_cu_qp_delta_depth = sps->log2_diff_max_min_luma_coding_block_size;
599 }
600 #endif
601
602 if (ctx->tile_rows && ctx->tile_cols) {
603 int uniform_spacing;
604
605 pps->tiles_enabled_flag = 1;
606 pps->num_tile_columns_minus1 = ctx->tile_cols - 1;
607 pps->num_tile_rows_minus1 = ctx->tile_rows - 1;
608
609 // Test whether the spacing provided matches the H.265 uniform
610 // spacing, and set the flag if it does.
611 uniform_spacing = 1;
612 for (i = 0; i <= pps->num_tile_columns_minus1 &&
613 uniform_spacing; i++) {
614 if (ctx->col_width[i] !=
615 (i + 1) * ctx->slice_block_cols / ctx->tile_cols -
616 i * ctx->slice_block_cols / ctx->tile_cols)
617 uniform_spacing = 0;
618 }
619 for (i = 0; i <= pps->num_tile_rows_minus1 &&
620 uniform_spacing; i++) {
621 if (ctx->row_height[i] !=
622 (i + 1) * ctx->slice_block_rows / ctx->tile_rows -
623 i * ctx->slice_block_rows / ctx->tile_rows)
624 uniform_spacing = 0;
625 }
626 pps->uniform_spacing_flag = uniform_spacing;
627
628 for (i = 0; i <= pps->num_tile_columns_minus1; i++)
629 pps->column_width_minus1[i] = ctx->col_width[i] - 1;
630 for (i = 0; i <= pps->num_tile_rows_minus1; i++)
631 pps->row_height_minus1[i] = ctx->row_height[i] - 1;
632
633 pps->loop_filter_across_tiles_enabled_flag = 1;
634 }
635
636 pps->pps_loop_filter_across_slices_enabled_flag = 1;
637
638 // Fill VAAPI parameter buffers.
639
640 *vseq = (VAEncSequenceParameterBufferHEVC) {
641 .general_profile_idc = vps->profile_tier_level.general_profile_idc,
642 .general_level_idc = vps->profile_tier_level.general_level_idc,
643 .general_tier_flag = vps->profile_tier_level.general_tier_flag,
644
645 .intra_period = ctx->gop_size,
646 .intra_idr_period = ctx->gop_size,
647 .ip_period = ctx->b_per_p + 1,
648 .bits_per_second = ctx->va_bit_rate,
649
650 .pic_width_in_luma_samples = sps->pic_width_in_luma_samples,
651 .pic_height_in_luma_samples = sps->pic_height_in_luma_samples,
652
653 .seq_fields.bits = {
654 .chroma_format_idc = sps->chroma_format_idc,
655 .separate_colour_plane_flag = sps->separate_colour_plane_flag,
656 .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
657 .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
658 .scaling_list_enabled_flag = sps->scaling_list_enabled_flag,
659 .strong_intra_smoothing_enabled_flag =
660 sps->strong_intra_smoothing_enabled_flag,
661 .amp_enabled_flag = sps->amp_enabled_flag,
662 .sample_adaptive_offset_enabled_flag =
663 sps->sample_adaptive_offset_enabled_flag,
664 .pcm_enabled_flag = sps->pcm_enabled_flag,
665 .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag,
666 .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
667 },
668
669 .log2_min_luma_coding_block_size_minus3 =
670 sps->log2_min_luma_coding_block_size_minus3,
671 .log2_diff_max_min_luma_coding_block_size =
672 sps->log2_diff_max_min_luma_coding_block_size,
673 .log2_min_transform_block_size_minus2 =
674 sps->log2_min_luma_transform_block_size_minus2,
675 .log2_diff_max_min_transform_block_size =
676 sps->log2_diff_max_min_luma_transform_block_size,
677 .max_transform_hierarchy_depth_inter =
678 sps->max_transform_hierarchy_depth_inter,
679 .max_transform_hierarchy_depth_intra =
680 sps->max_transform_hierarchy_depth_intra,
681
682 .pcm_sample_bit_depth_luma_minus1 =
683 sps->pcm_sample_bit_depth_luma_minus1,
684 .pcm_sample_bit_depth_chroma_minus1 =
685 sps->pcm_sample_bit_depth_chroma_minus1,
686 .log2_min_pcm_luma_coding_block_size_minus3 =
687 sps->log2_min_pcm_luma_coding_block_size_minus3,
688 .log2_max_pcm_luma_coding_block_size_minus3 =
689 sps->log2_min_pcm_luma_coding_block_size_minus3 +
690 sps->log2_diff_max_min_pcm_luma_coding_block_size,
691
692 .vui_parameters_present_flag = 0,
693 };
694
695 *vpic = (VAEncPictureParameterBufferHEVC) {
696 .decoded_curr_pic = {
697 .picture_id = VA_INVALID_ID,
698 .flags = VA_PICTURE_HEVC_INVALID,
699 },
700
701 .coded_buf = VA_INVALID_ID,
702
703 .collocated_ref_pic_index = sps->sps_temporal_mvp_enabled_flag ?
704 0 : 0xff,
705 .last_picture = 0,
706
707 .pic_init_qp = pps->init_qp_minus26 + 26,
708 .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
709 .pps_cb_qp_offset = pps->pps_cb_qp_offset,
710 .pps_cr_qp_offset = pps->pps_cr_qp_offset,
711
712 .num_tile_columns_minus1 = pps->num_tile_columns_minus1,
713 .num_tile_rows_minus1 = pps->num_tile_rows_minus1,
714
715 .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2,
716 .ctu_max_bitsize_allowed = 0,
717
718 .num_ref_idx_l0_default_active_minus1 =
719 pps->num_ref_idx_l0_default_active_minus1,
720 .num_ref_idx_l1_default_active_minus1 =
721 pps->num_ref_idx_l1_default_active_minus1,
722
723 .slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id,
724
725 .pic_fields.bits = {
726 .sign_data_hiding_enabled_flag = pps->sign_data_hiding_enabled_flag,
727 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
728 .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
729 .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
730 .weighted_pred_flag = pps->weighted_pred_flag,
731 .weighted_bipred_flag = pps->weighted_bipred_flag,
732 .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag,
733 .tiles_enabled_flag = pps->tiles_enabled_flag,
734 .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
735 .loop_filter_across_tiles_enabled_flag =
736 pps->loop_filter_across_tiles_enabled_flag,
737 .pps_loop_filter_across_slices_enabled_flag =
738 pps->pps_loop_filter_across_slices_enabled_flag,
739 .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag |
740 pps->pps_scaling_list_data_present_flag),
741 .screen_content_flag = 0,
742 .enable_gpu_weighted_prediction = 0,
743 .no_output_of_prior_pics_flag = 0,
744 },
745 };
746
747 if (pps->tiles_enabled_flag) {
748 for (i = 0; i <= vpic->num_tile_rows_minus1; i++)
749 vpic->row_height_minus1[i] = pps->row_height_minus1[i];
750 for (i = 0; i <= vpic->num_tile_columns_minus1; i++)
751 vpic->column_width_minus1[i] = pps->column_width_minus1[i];
752 }
753
754 return 0;
755 }
756
757 static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
758 VAAPIEncodePicture *pic)
759 {
760 VAAPIEncodeContext *ctx = avctx->priv_data;
761 VAAPIEncodeH265Context *priv = avctx->priv_data;
762 VAAPIEncodeH265Picture *hpic = pic->priv_data;
763 VAAPIEncodePicture *prev = pic->prev;
764 VAAPIEncodeH265Picture *hprev = prev ? prev->priv_data : NULL;
765 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
766 int i, j = 0;
767
768 if (pic->type == PICTURE_TYPE_IDR) {
769 av_assert0(pic->display_order == pic->encode_order);
770
771 hpic->last_idr_frame = pic->display_order;
772
773 hpic->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
774 hpic->slice_type = HEVC_SLICE_I;
775 hpic->pic_type = 0;
776 } else {
777 av_assert0(prev);
778 hpic->last_idr_frame = hprev->last_idr_frame;
779
780 if (pic->type == PICTURE_TYPE_I) {
781 hpic->slice_nal_unit = HEVC_NAL_CRA_NUT;
782 hpic->slice_type = HEVC_SLICE_I;
783 hpic->pic_type = 0;
784 } else if (pic->type == PICTURE_TYPE_P) {
785 av_assert0(pic->refs[0]);
786 hpic->slice_nal_unit = HEVC_NAL_TRAIL_R;
787 hpic->slice_type = HEVC_SLICE_P;
788 hpic->pic_type = 1;
789 } else {
790 VAAPIEncodePicture *irap_ref;
791 av_assert0(pic->refs[0][0] && pic->refs[1][0]);
792 for (irap_ref = pic; irap_ref; irap_ref = irap_ref->refs[1][0]) {
793 if (irap_ref->type == PICTURE_TYPE_I)
794 break;
795 }
796 if (pic->b_depth == ctx->max_b_depth) {
797 hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_N
798 : HEVC_NAL_TRAIL_N;
799 } else {
800 hpic->slice_nal_unit = irap_ref ? HEVC_NAL_RASL_R
801 : HEVC_NAL_TRAIL_R;
802 }
803 hpic->slice_type = HEVC_SLICE_B;
804 hpic->pic_type = 2;
805 }
806 }
807 hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame;
808
809 if (priv->aud) {
810 priv->aud_needed = 1;
811 priv->raw_aud = (H265RawAUD) {
812 .nal_unit_header = {
813 .nal_unit_type = HEVC_NAL_AUD,
814 .nuh_layer_id = 0,
815 .nuh_temporal_id_plus1 = 1,
816 },
817 .pic_type = hpic->pic_type,
818 };
819 } else {
820 priv->aud_needed = 0;
821 }
822
823 priv->sei_needed = 0;
824
825 // Only look for the metadata on I/IDR frame on the output. We
826 // may force an IDR frame on the output where the medadata gets
827 // changed on the input frame.
828 if ((priv->sei & SEI_MASTERING_DISPLAY) &&
829 (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
830 AVFrameSideData *sd =
831 av_frame_get_side_data(pic->input_image,
832 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
833
834 if (sd) {
835 AVMasteringDisplayMetadata *mdm =
836 (AVMasteringDisplayMetadata *)sd->data;
837
838 // SEI is needed when both the primaries and luminance are set
839 if (mdm->has_primaries && mdm->has_luminance) {
840 SEIRawMasteringDisplayColourVolume *mdcv =
841 &priv->sei_mastering_display;
842 const int mapping[3] = {1, 2, 0};
843 const int chroma_den = 50000;
844 const int luma_den = 10000;
845
846 for (i = 0; i < 3; i++) {
847 const int j = mapping[i];
848 mdcv->display_primaries_x[i] =
849 FFMIN(lrint(chroma_den *
850 av_q2d(mdm->display_primaries[j][0])),
851 chroma_den);
852 mdcv->display_primaries_y[i] =
853 FFMIN(lrint(chroma_den *
854 av_q2d(mdm->display_primaries[j][1])),
855 chroma_den);
856 }
857
858 mdcv->white_point_x =
859 FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
860 chroma_den);
861 mdcv->white_point_y =
862 FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
863 chroma_den);
864
865 mdcv->max_display_mastering_luminance =
866 lrint(luma_den * av_q2d(mdm->max_luminance));
867 mdcv->min_display_mastering_luminance =
868 FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
869 mdcv->max_display_mastering_luminance);
870
871 priv->sei_needed |= SEI_MASTERING_DISPLAY;
872 }
873 }
874 }
875
876 if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
877 (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
878 AVFrameSideData *sd =
879 av_frame_get_side_data(pic->input_image,
880 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
881
882 if (sd) {
883 AVContentLightMetadata *clm =
884 (AVContentLightMetadata *)sd->data;
885 SEIRawContentLightLevelInfo *clli =
886 &priv->sei_content_light_level;
887
888 clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535);
889 clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
890
891 priv->sei_needed |= SEI_CONTENT_LIGHT_LEVEL;
892 }
893 }
894
895 if (priv->sei & SEI_A53_CC) {
896 int err;
897 size_t sei_a53cc_len;
898 av_freep(&priv->sei_a53cc_data);
899 err = ff_alloc_a53_sei(pic->input_image, 0, &priv->sei_a53cc_data, &sei_a53cc_len);
900 if (err < 0)
901 return err;
902 if (priv->sei_a53cc_data != NULL) {
903 priv->sei_a53cc.itu_t_t35_country_code = 181;
904 priv->sei_a53cc.data = (uint8_t *)priv->sei_a53cc_data + 1;
905 priv->sei_a53cc.data_length = sei_a53cc_len - 1;
906
907 priv->sei_needed |= SEI_A53_CC;
908 }
909 }
910
911 vpic->decoded_curr_pic = (VAPictureHEVC) {
912 .picture_id = pic->recon_surface,
913 .pic_order_cnt = hpic->pic_order_cnt,
914 .flags = 0,
915 };
916
917 for (int k = 0; k < MAX_REFERENCE_LIST_NUM; k++) {
918 for (i = 0; i < pic->nb_refs[k]; i++) {
919 VAAPIEncodePicture *ref = pic->refs[k][i];
920 VAAPIEncodeH265Picture *href;
921
922 av_assert0(ref && ref->encode_order < pic->encode_order);
923 href = ref->priv_data;
924
925 vpic->reference_frames[j++] = (VAPictureHEVC) {
926 .picture_id = ref->recon_surface,
927 .pic_order_cnt = href->pic_order_cnt,
928 .flags = (ref->display_order < pic->display_order ?
929 VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
930 (ref->display_order > pic->display_order ?
931 VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
932 };
933 }
934 }
935
936 for (; j < FF_ARRAY_ELEMS(vpic->reference_frames); j++) {
937 vpic->reference_frames[j] = (VAPictureHEVC) {
938 .picture_id = VA_INVALID_ID,
939 .flags = VA_PICTURE_HEVC_INVALID,
940 };
941 }
942
943 vpic->coded_buf = pic->output_buffer;
944
945 vpic->nal_unit_type = hpic->slice_nal_unit;
946
947 switch (pic->type) {
948 case PICTURE_TYPE_IDR:
949 vpic->pic_fields.bits.idr_pic_flag = 1;
950 vpic->pic_fields.bits.coding_type = 1;
951 vpic->pic_fields.bits.reference_pic_flag = 1;
952 break;
953 case PICTURE_TYPE_I:
954 vpic->pic_fields.bits.idr_pic_flag = 0;
955 vpic->pic_fields.bits.coding_type = 1;
956 vpic->pic_fields.bits.reference_pic_flag = 1;
957 break;
958 case PICTURE_TYPE_P:
959 vpic->pic_fields.bits.idr_pic_flag = 0;
960 vpic->pic_fields.bits.coding_type = 2;
961 vpic->pic_fields.bits.reference_pic_flag = 1;
962 break;
963 case PICTURE_TYPE_B:
964 vpic->pic_fields.bits.idr_pic_flag = 0;
965 vpic->pic_fields.bits.coding_type = 3;
966 vpic->pic_fields.bits.reference_pic_flag = 0;
967 break;
968 default:
969 av_assert0(0 && "invalid picture type");
970 }
971
972 return 0;
973 }
974
975 static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
976 VAAPIEncodePicture *pic,
977 VAAPIEncodeSlice *slice)
978 {
979 VAAPIEncodeContext *ctx = avctx->priv_data;
980 VAAPIEncodeH265Context *priv = avctx->priv_data;
981 VAAPIEncodeH265Picture *hpic = pic->priv_data;
982 const H265RawSPS *sps = &priv->raw_sps;
983 const H265RawPPS *pps = &priv->raw_pps;
984 H265RawSliceHeader *sh = &priv->raw_slice.header;
985 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
986 VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
987 int i;
988
989 sh->nal_unit_header = (H265RawNALUnitHeader) {
990 .nal_unit_type = hpic->slice_nal_unit,
991 .nuh_layer_id = 0,
992 .nuh_temporal_id_plus1 = 1,
993 };
994
995 sh->slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id;
996
997 sh->first_slice_segment_in_pic_flag = slice->index == 0;
998 sh->slice_segment_address = slice->block_start;
999
1000 sh->slice_type = hpic->slice_type;
1001
1002 if (sh->slice_type == HEVC_SLICE_P && ctx->p_to_gpb)
1003 sh->slice_type = HEVC_SLICE_B;
1004
1005 sh->slice_pic_order_cnt_lsb = hpic->pic_order_cnt &
1006 (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
1007
1008 if (pic->type != PICTURE_TYPE_IDR) {
1009 H265RawSTRefPicSet *rps;
1010 const VAAPIEncodeH265Picture *strp;
1011 int rps_poc[MAX_DPB_SIZE];
1012 int rps_used[MAX_DPB_SIZE];
1013 int i, j, poc, rps_pics;
1014
1015 sh->short_term_ref_pic_set_sps_flag = 0;
1016
1017 rps = &sh->short_term_ref_pic_set;
1018 memset(rps, 0, sizeof(*rps));
1019
1020 rps_pics = 0;
1021 for (i = 0; i < MAX_REFERENCE_LIST_NUM; i++) {
1022 for (j = 0; j < pic->nb_refs[i]; j++) {
1023 strp = pic->refs[i][j]->priv_data;
1024 rps_poc[rps_pics] = strp->pic_order_cnt;
1025 rps_used[rps_pics] = 1;
1026 ++rps_pics;
1027 }
1028 }
1029
1030 for (i = 0; i < pic->nb_dpb_pics; i++) {
1031 if (pic->dpb[i] == pic)
1032 continue;
1033
1034 for (j = 0; j < pic->nb_refs[0]; j++) {
1035 if (pic->dpb[i] == pic->refs[0][j])
1036 break;
1037 }
1038 if (j < pic->nb_refs[0])
1039 continue;
1040
1041 for (j = 0; j < pic->nb_refs[1]; j++) {
1042 if (pic->dpb[i] == pic->refs[1][j])
1043 break;
1044 }
1045 if (j < pic->nb_refs[1])
1046 continue;
1047
1048 strp = pic->dpb[i]->priv_data;
1049 rps_poc[rps_pics] = strp->pic_order_cnt;
1050 rps_used[rps_pics] = 0;
1051 ++rps_pics;
1052 }
1053
1054 for (i = 1; i < rps_pics; i++) {
1055 for (j = i; j > 0; j--) {
1056 if (rps_poc[j] > rps_poc[j - 1])
1057 break;
1058 av_assert0(rps_poc[j] != rps_poc[j - 1]);
1059 FFSWAP(int, rps_poc[j], rps_poc[j - 1]);
1060 FFSWAP(int, rps_used[j], rps_used[j - 1]);
1061 }
1062 }
1063
1064 av_log(avctx, AV_LOG_DEBUG, "RPS for POC %d:",
1065 hpic->pic_order_cnt);
1066 for (i = 0; i < rps_pics; i++) {
1067 av_log(avctx, AV_LOG_DEBUG, " (%d,%d)",
1068 rps_poc[i], rps_used[i]);
1069 }
1070 av_log(avctx, AV_LOG_DEBUG, "\n");
1071
1072 for (i = 0; i < rps_pics; i++) {
1073 av_assert0(rps_poc[i] != hpic->pic_order_cnt);
1074 if (rps_poc[i] > hpic->pic_order_cnt)
1075 break;
1076 }
1077
1078 rps->num_negative_pics = i;
1079 poc = hpic->pic_order_cnt;
1080 for (j = i - 1; j >= 0; j--) {
1081 rps->delta_poc_s0_minus1[i - 1 - j] = poc - rps_poc[j] - 1;
1082 rps->used_by_curr_pic_s0_flag[i - 1 - j] = rps_used[j];
1083 poc = rps_poc[j];
1084 }
1085
1086 rps->num_positive_pics = rps_pics - i;
1087 poc = hpic->pic_order_cnt;
1088 for (j = i; j < rps_pics; j++) {
1089 rps->delta_poc_s1_minus1[j - i] = rps_poc[j] - poc - 1;
1090 rps->used_by_curr_pic_s1_flag[j - i] = rps_used[j];
1091 poc = rps_poc[j];
1092 }
1093
1094 sh->num_long_term_sps = 0;
1095 sh->num_long_term_pics = 0;
1096
1097 // when this flag is not present, it is inerred to 1.
1098 sh->collocated_from_l0_flag = 1;
1099 sh->slice_temporal_mvp_enabled_flag =
1100 sps->sps_temporal_mvp_enabled_flag;
1101 if (sh->slice_temporal_mvp_enabled_flag) {
1102 if (sh->slice_type == HEVC_SLICE_B)
1103 sh->collocated_from_l0_flag = 1;
1104 sh->collocated_ref_idx = 0;
1105 }
1106
1107 sh->num_ref_idx_active_override_flag = 0;
1108 sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
1109 sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
1110 }
1111
1112 sh->slice_sao_luma_flag = sh->slice_sao_chroma_flag =
1113 sps->sample_adaptive_offset_enabled_flag;
1114
1115 if (pic->type == PICTURE_TYPE_B)
1116 sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
1117 else if (pic->type == PICTURE_TYPE_P)
1118 sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
1119 else
1120 sh->slice_qp_delta = priv->fixed_qp_idr - (pps->init_qp_minus26 + 26);
1121
1122
1123 *vslice = (VAEncSliceParameterBufferHEVC) {
1124 .slice_segment_address = sh->slice_segment_address,
1125 .num_ctu_in_slice = slice->block_size,
1126
1127 .slice_type = sh->slice_type,
1128 .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
1129
1130 .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
1131 .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
1132
1133 .luma_log2_weight_denom = sh->luma_log2_weight_denom,
1134 .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
1135
1136 .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
1137
1138 .slice_qp_delta = sh->slice_qp_delta,
1139 .slice_cb_qp_offset = sh->slice_cb_qp_offset,
1140 .slice_cr_qp_offset = sh->slice_cr_qp_offset,
1141
1142 .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
1143 .slice_tc_offset_div2 = sh->slice_tc_offset_div2,
1144
1145 .slice_fields.bits = {
1146 .last_slice_of_pic_flag = slice->index == pic->nb_slices - 1,
1147 .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
1148 .colour_plane_id = sh->colour_plane_id,
1149 .slice_temporal_mvp_enabled_flag =
1150 sh->slice_temporal_mvp_enabled_flag,
1151 .slice_sao_luma_flag = sh->slice_sao_luma_flag,
1152 .slice_sao_chroma_flag = sh->slice_sao_chroma_flag,
1153 .num_ref_idx_active_override_flag =
1154 sh->num_ref_idx_active_override_flag,
1155 .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
1156 .cabac_init_flag = sh->cabac_init_flag,
1157 .slice_deblocking_filter_disabled_flag =
1158 sh->slice_deblocking_filter_disabled_flag,
1159 .slice_loop_filter_across_slices_enabled_flag =
1160 sh->slice_loop_filter_across_slices_enabled_flag,
1161 .collocated_from_l0_flag = sh->collocated_from_l0_flag,
1162 },
1163 };
1164
1165 for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
1166 vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
1167 vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
1168 vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
1169 vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
1170 }
1171
1172 if (pic->nb_refs[0]) {
1173 // Backward reference for P- or B-frame.
1174 av_assert0(pic->type == PICTURE_TYPE_P ||
1175 pic->type == PICTURE_TYPE_B);
1176 vslice->ref_pic_list0[0] = vpic->reference_frames[0];
1177 if (ctx->p_to_gpb && pic->type == PICTURE_TYPE_P)
1178 // Reference for GPB B-frame, L0 == L1
1179 vslice->ref_pic_list1[0] = vpic->reference_frames[0];
1180 }
1181 if (pic->nb_refs[1]) {
1182 // Forward reference for B-frame.
1183 av_assert0(pic->type == PICTURE_TYPE_B);
1184 vslice->ref_pic_list1[0] = vpic->reference_frames[1];
1185 }
1186
1187 if (pic->type == PICTURE_TYPE_P && ctx->p_to_gpb) {
1188 vslice->slice_type = HEVC_SLICE_B;
1189 for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
1190 vslice->ref_pic_list1[i].picture_id = vslice->ref_pic_list0[i].picture_id;
1191 vslice->ref_pic_list1[i].flags = vslice->ref_pic_list0[i].flags;
1192 }
1193 }
1194
1195 return 0;
1196 }
1197
1198 static av_cold int vaapi_encode_h265_get_encoder_caps(AVCodecContext *avctx)
1199 {
1200 VAAPIEncodeContext *ctx = avctx->priv_data;
1201 VAAPIEncodeH265Context *priv = avctx->priv_data;
1202
1203 #if VA_CHECK_VERSION(1, 13, 0)
1204 {
1205 VAConfigAttribValEncHEVCBlockSizes block_size;
1206 VAConfigAttrib attr;
1207 VAStatus vas;
1208
1209 attr.type = VAConfigAttribEncHEVCFeatures;
1210 vas = vaGetConfigAttributes(ctx->hwctx->display, ctx->va_profile,
1211 ctx->va_entrypoint, &attr, 1);
1212 if (vas != VA_STATUS_SUCCESS) {
1213 av_log(avctx, AV_LOG_ERROR, "Failed to query encoder "
1214 "features, using guessed defaults.\n");
1215 return AVERROR_EXTERNAL;
1216 } else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1217 av_log(avctx, AV_LOG_WARNING, "Driver does not advertise "
1218 "encoder features, using guessed defaults.\n");
1219 } else {
1220 priv->va_features = attr.value;
1221 }
1222
1223 attr.type = VAConfigAttribEncHEVCBlockSizes;
1224 vas = vaGetConfigAttributes(ctx->hwctx->display, ctx->va_profile,
1225 ctx->va_entrypoint, &attr, 1);
1226 if (vas != VA_STATUS_SUCCESS) {
1227 av_log(avctx, AV_LOG_ERROR, "Failed to query encoder "
1228 "block size, using guessed defaults.\n");
1229 return AVERROR_EXTERNAL;
1230 } else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1231 av_log(avctx, AV_LOG_WARNING, "Driver does not advertise "
1232 "encoder block size, using guessed defaults.\n");
1233 } else {
1234 priv->va_bs = block_size.value = attr.value;
1235
1236 priv->ctu_size =
1237 1 << block_size.bits.log2_max_coding_tree_block_size_minus3 + 3;
1238 priv->min_cb_size =
1239 1 << block_size.bits.log2_min_luma_coding_block_size_minus3 + 3;
1240 }
1241 }
1242 #endif
1243
1244 if (!priv->ctu_size) {
1245 priv->ctu_size = 32;
1246 priv->min_cb_size = 16;
1247 }
1248 av_log(avctx, AV_LOG_VERBOSE, "Using CTU size %dx%d, "
1249 "min CB size %dx%d.\n", priv->ctu_size, priv->ctu_size,
1250 priv->min_cb_size, priv->min_cb_size);
1251
1252 ctx->surface_width = FFALIGN(avctx->width, priv->min_cb_size);
1253 ctx->surface_height = FFALIGN(avctx->height, priv->min_cb_size);
1254
1255 ctx->slice_block_width = ctx->slice_block_height = priv->ctu_size;
1256
1257 return 0;
1258 }
1259
1260 static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
1261 {
1262 VAAPIEncodeContext *ctx = avctx->priv_data;
1263 VAAPIEncodeH265Context *priv = avctx->priv_data;
1264 int err;
1265
1266 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
1267 if (err < 0)
1268 return err;
1269
1270 if (ctx->va_rc_mode == VA_RC_CQP) {
1271 // Note that VAAPI only supports positive QP values - the range is
1272 // therefore always bounded below by 1, even in 10-bit mode where
1273 // it should go down to -12.
1274
1275 priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
1276 if (avctx->i_quant_factor > 0.0)
1277 priv->fixed_qp_idr =
1278 av_clip((avctx->i_quant_factor * priv->fixed_qp_p +
1279 avctx->i_quant_offset) + 0.5, 1, 51);
1280 else
1281 priv->fixed_qp_idr = priv->fixed_qp_p;
1282 if (avctx->b_quant_factor > 0.0)
1283 priv->fixed_qp_b =
1284 av_clip((avctx->b_quant_factor * priv->fixed_qp_p +
1285 avctx->b_quant_offset) + 0.5, 1, 51);
1286 else
1287 priv->fixed_qp_b = priv->fixed_qp_p;
1288
1289 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1290 "%d / %d / %d for IDR- / P- / B-frames.\n",
1291 priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1292
1293 } else {
1294 // These still need to be set for init_qp/slice_qp_delta.
1295 priv->fixed_qp_idr = 30;
1296 priv->fixed_qp_p = 30;
1297 priv->fixed_qp_b = 30;
1298 }
1299
1300 ctx->roi_quant_range = 51 + 6 * (ctx->profile->depth - 8);
1301
1302 return 0;
1303 }
1304
1305 static const VAAPIEncodeProfile vaapi_encode_h265_profiles[] = {
1306 { AV_PROFILE_HEVC_MAIN, 8, 3, 1, 1, VAProfileHEVCMain },
1307 { AV_PROFILE_HEVC_REXT, 8, 3, 1, 1, VAProfileHEVCMain },
1308 #if VA_CHECK_VERSION(0, 37, 0)
1309 { AV_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10 },
1310 { AV_PROFILE_HEVC_REXT, 10, 3, 1, 1, VAProfileHEVCMain10 },
1311 #endif
1312 #if VA_CHECK_VERSION(1, 2, 0)
1313 { AV_PROFILE_HEVC_REXT, 12, 3, 1, 1, VAProfileHEVCMain12 },
1314 { AV_PROFILE_HEVC_REXT, 8, 3, 1, 0, VAProfileHEVCMain422_10 },
1315 { AV_PROFILE_HEVC_REXT, 10, 3, 1, 0, VAProfileHEVCMain422_10 },
1316 { AV_PROFILE_HEVC_REXT, 12, 3, 1, 0, VAProfileHEVCMain422_12 },
1317 { AV_PROFILE_HEVC_REXT, 8, 3, 0, 0, VAProfileHEVCMain444 },
1318 { AV_PROFILE_HEVC_REXT, 10, 3, 0, 0, VAProfileHEVCMain444_10 },
1319 { AV_PROFILE_HEVC_REXT, 12, 3, 0, 0, VAProfileHEVCMain444_12 },
1320 #endif
1321 { AV_PROFILE_UNKNOWN }
1322 };
1323
1324 static const VAAPIEncodeType vaapi_encode_type_h265 = {
1325 .profiles = vaapi_encode_h265_profiles,
1326
1327 .flags = FLAG_SLICE_CONTROL |
1328 FLAG_B_PICTURES |
1329 FLAG_B_PICTURE_REFERENCES |
1330 FLAG_NON_IDR_KEY_PICTURES,
1331
1332 .default_quality = 25,
1333
1334 .get_encoder_caps = &vaapi_encode_h265_get_encoder_caps,
1335 .configure = &vaapi_encode_h265_configure,
1336
1337 .picture_priv_data_size = sizeof(VAAPIEncodeH265Picture),
1338
1339 .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
1340 .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
1341
1342 .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
1343 .init_picture_params = &vaapi_encode_h265_init_picture_params,
1344
1345 .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
1346 .init_slice_params = &vaapi_encode_h265_init_slice_params,
1347
1348 .sequence_header_type = VAEncPackedHeaderSequence,
1349 .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
1350
1351 .slice_header_type = VAEncPackedHeaderHEVC_Slice,
1352 .write_slice_header = &vaapi_encode_h265_write_slice_header,
1353
1354 .write_extra_header = &vaapi_encode_h265_write_extra_header,
1355 };
1356
1357 static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
1358 {
1359 VAAPIEncodeContext *ctx = avctx->priv_data;
1360 VAAPIEncodeH265Context *priv = avctx->priv_data;
1361
1362 ctx->codec = &vaapi_encode_type_h265;
1363
1364 if (avctx->profile == AV_PROFILE_UNKNOWN)
1365 avctx->profile = priv->profile;
1366 if (avctx->level == AV_LEVEL_UNKNOWN)
1367 avctx->level = priv->level;
1368
1369 if (avctx->level != AV_LEVEL_UNKNOWN && avctx->level & ~0xff) {
1370 av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
1371 "in 8-bit unsigned integer.\n", avctx->level);
1372 return AVERROR(EINVAL);
1373 }
1374
1375 ctx->desired_packed_headers =
1376 VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
1377 VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
1378 VA_ENC_PACKED_HEADER_MISC; // SEI
1379
1380 if (priv->qp > 0)
1381 ctx->explicit_qp = priv->qp;
1382
1383 return ff_vaapi_encode_init(avctx);
1384 }
1385
1386 static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
1387 {
1388 VAAPIEncodeH265Context *priv = avctx->priv_data;
1389
1390 ff_cbs_fragment_free(&priv->current_access_unit);
1391 ff_cbs_close(&priv->cbc);
1392 av_freep(&priv->sei_a53cc_data);
1393
1394 return ff_vaapi_encode_close(avctx);
1395 }
1396
1397 #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
1398 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1399 static const AVOption vaapi_encode_h265_options[] = {
1400 VAAPI_ENCODE_COMMON_OPTIONS,
1401 VAAPI_ENCODE_RC_OPTIONS,
1402
1403 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1404 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
1405
1406 { "aud", "Include AUD",
1407 OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1408
1409 { "profile", "Set profile (general_profile_idc)",
1410 OFFSET(profile), AV_OPT_TYPE_INT,
1411 { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xff, FLAGS, .unit = "profile" },
1412
1413 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1414 { .i64 = value }, 0, 0, FLAGS, .unit = "profile"
1415 { PROFILE("main", AV_PROFILE_HEVC_MAIN) },
1416 { PROFILE("main10", AV_PROFILE_HEVC_MAIN_10) },
1417 { PROFILE("rext", AV_PROFILE_HEVC_REXT) },
1418 #undef PROFILE
1419
1420 { "tier", "Set tier (general_tier_flag)",
1421 OFFSET(tier), AV_OPT_TYPE_INT,
1422 { .i64 = 0 }, 0, 1, FLAGS, .unit = "tier" },
1423 { "main", NULL, 0, AV_OPT_TYPE_CONST,
1424 { .i64 = 0 }, 0, 0, FLAGS, .unit = "tier" },
1425 { "high", NULL, 0, AV_OPT_TYPE_CONST,
1426 { .i64 = 1 }, 0, 0, FLAGS, .unit = "tier" },
1427
1428 { "level", "Set level (general_level_idc)",
1429 OFFSET(level), AV_OPT_TYPE_INT,
1430 { .i64 = AV_LEVEL_UNKNOWN }, AV_LEVEL_UNKNOWN, 0xff, FLAGS, .unit = "level" },
1431
1432 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1433 { .i64 = value }, 0, 0, FLAGS, .unit = "level"
1434 { LEVEL("1", 30) },
1435 { LEVEL("2", 60) },
1436 { LEVEL("2.1", 63) },
1437 { LEVEL("3", 90) },
1438 { LEVEL("3.1", 93) },
1439 { LEVEL("4", 120) },
1440 { LEVEL("4.1", 123) },
1441 { LEVEL("5", 150) },
1442 { LEVEL("5.1", 153) },
1443 { LEVEL("5.2", 156) },
1444 { LEVEL("6", 180) },
1445 { LEVEL("6.1", 183) },
1446 { LEVEL("6.2", 186) },
1447 #undef LEVEL
1448
1449 { "sei", "Set SEI to include",
1450 OFFSET(sei), AV_OPT_TYPE_FLAGS,
1451 { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL | SEI_A53_CC },
1452 0, INT_MAX, FLAGS, .unit = "sei" },
1453 { "hdr",
1454 "Include HDR metadata for mastering display colour volume "
1455 "and content light level information",
1456 0, AV_OPT_TYPE_CONST,
1457 { .i64 = SEI_MASTERING_DISPLAY | SEI_CONTENT_LIGHT_LEVEL },
1458 INT_MIN, INT_MAX, FLAGS, .unit = "sei" },
1459 { "a53_cc",
1460 "Include A/53 caption data",
1461 0, AV_OPT_TYPE_CONST,
1462 { .i64 = SEI_A53_CC },
1463 INT_MIN, INT_MAX, FLAGS, .unit = "sei" },
1464
1465 { "tiles", "Tile columns x rows",
1466 OFFSET(common.tile_cols), AV_OPT_TYPE_IMAGE_SIZE,
1467 { .str = NULL }, 0, 0, FLAGS },
1468
1469 { NULL },
1470 };
1471
1472 static const FFCodecDefault vaapi_encode_h265_defaults[] = {
1473 { "b", "0" },
1474 { "bf", "2" },
1475 { "g", "120" },
1476 { "i_qfactor", "1" },
1477 { "i_qoffset", "0" },
1478 { "b_qfactor", "6/5" },
1479 { "b_qoffset", "0" },
1480 { "qmin", "-1" },
1481 { "qmax", "-1" },
1482 { NULL },
1483 };
1484
1485 static const AVClass vaapi_encode_h265_class = {
1486 .class_name = "h265_vaapi",
1487 .item_name = av_default_item_name,
1488 .option = vaapi_encode_h265_options,
1489 .version = LIBAVUTIL_VERSION_INT,
1490 };
1491
1492 const FFCodec ff_hevc_vaapi_encoder = {
1493 .p.name = "hevc_vaapi",
1494 CODEC_LONG_NAME("H.265/HEVC (VAAPI)"),
1495 .p.type = AVMEDIA_TYPE_VIDEO,
1496 .p.id = AV_CODEC_ID_HEVC,
1497 .priv_data_size = sizeof(VAAPIEncodeH265Context),
1498 .init = &vaapi_encode_h265_init,
1499 FF_CODEC_RECEIVE_PACKET_CB(&ff_vaapi_encode_receive_packet),
1500 .close = &vaapi_encode_h265_close,
1501 .p.priv_class = &vaapi_encode_h265_class,
1502 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
1503 AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1504 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
1505 FF_CODEC_CAP_INIT_CLEANUP,
1506 .defaults = vaapi_encode_h265_defaults,
1507 .p.pix_fmts = (const enum AVPixelFormat[]) {
1508 AV_PIX_FMT_VAAPI,
1509 AV_PIX_FMT_NONE,
1510 },
1511 .hw_configs = ff_vaapi_encode_hw_configs,
1512 .p.wrapper_name = "vaapi",
1513 };
1514