FFmpeg coverage


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