lavc: VAAPI H.265 encoder
[libav.git] / libavcodec / vaapi_encode_h265.c
CommitLineData
31fe1f25
MT
1/*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <va/va.h>
20#include <va/va_enc_hevc.h>
21
22#include "libavutil/avassert.h"
23#include "libavutil/internal.h"
24#include "libavutil/opt.h"
25#include "libavutil/pixfmt.h"
26
27#include "avcodec.h"
28#include "hevc.h"
29#include "internal.h"
30#include "put_bits.h"
31#include "vaapi_encode.h"
32#include "vaapi_encode_h26x.h"
33
34
35#define MAX_ST_REF_PIC_SETS 32
36#define MAX_DPB_PICS 16
37#define MAX_LAYERS 1
38
39
40typedef struct VAAPIEncodeH265STRPS {
41 char inter_ref_pic_set_prediction_flag;
42
43 unsigned int num_negative_pics;
44 unsigned int num_positive_pics;
45
46 unsigned int delta_poc_s0_minus1[MAX_DPB_PICS];
47 char used_by_curr_pic_s0_flag[MAX_DPB_PICS];
48
49 unsigned int delta_poc_s1_minus1[MAX_DPB_PICS];
50 char used_by_curr_pic_s1_flag[MAX_DPB_PICS];
51} VAAPIEncodeH265STRPS;
52
53// This structure contains all possibly-useful per-sequence syntax elements
54// which are not already contained in the various VAAPI structures.
55typedef struct VAAPIEncodeH265MiscSequenceParams {
56
57 // Parameter set IDs.
58 unsigned int video_parameter_set_id;
59 unsigned int seq_parameter_set_id;
60
61 // Layering.
62 unsigned int vps_max_layers_minus1;
63 unsigned int vps_max_sub_layers_minus1;
64 char vps_temporal_id_nesting_flag;
65 unsigned int vps_max_layer_id;
66 unsigned int vps_num_layer_sets_minus1;
67 unsigned int sps_max_sub_layers_minus1;
68 char sps_temporal_id_nesting_flag;
69 char layer_id_included_flag[MAX_LAYERS][64];
70
71 // Profile/tier/level parameters.
72 char general_profile_compatibility_flag[32];
73 char general_progressive_source_flag;
74 char general_interlaced_source_flag;
75 char general_non_packed_constraint_flag;
76 char general_frame_only_constraint_flag;
77 char general_inbld_flag;
78
79 // Decode/display ordering parameters.
80 unsigned int log2_max_pic_order_cnt_lsb_minus4;
81 char vps_sub_layer_ordering_info_present_flag;
82 unsigned int vps_max_dec_pic_buffering_minus1[MAX_LAYERS];
83 unsigned int vps_max_num_reorder_pics[MAX_LAYERS];
84 unsigned int vps_max_latency_increase_plus1[MAX_LAYERS];
85 char sps_sub_layer_ordering_info_present_flag;
86 unsigned int sps_max_dec_pic_buffering_minus1[MAX_LAYERS];
87 unsigned int sps_max_num_reorder_pics[MAX_LAYERS];
88 unsigned int sps_max_latency_increase_plus1[MAX_LAYERS];
89
90 // Timing information.
91 char vps_timing_info_present_flag;
92 unsigned int vps_num_units_in_tick;
93 unsigned int vps_time_scale;
94 char vps_poc_proportional_to_timing_flag;
95 unsigned int vps_num_ticks_poc_diff_minus1;
96
97 // Cropping information.
98 char conformance_window_flag;
99 unsigned int conf_win_left_offset;
100 unsigned int conf_win_right_offset;
101 unsigned int conf_win_top_offset;
102 unsigned int conf_win_bottom_offset;
103
104 // Short-term reference picture sets.
105 unsigned int num_short_term_ref_pic_sets;
106 VAAPIEncodeH265STRPS st_ref_pic_set[MAX_ST_REF_PIC_SETS];
107
108 // Long-term reference pictures.
109 char long_term_ref_pics_present_flag;
110 unsigned int num_long_term_ref_pics_sps;
111 struct {
112 unsigned int lt_ref_pic_poc_lsb_sps;
113 char used_by_curr_pic_lt_sps_flag;
114 } lt_ref_pic;
115
116 // Deblocking filter control.
117 char deblocking_filter_control_present_flag;
118 char deblocking_filter_override_enabled_flag;
119 char pps_deblocking_filter_disabled_flag;
120 int pps_beta_offset_div2;
121 int pps_tc_offset_div2;
122
123 // Video Usability Information.
124 char vui_parameters_present_flag;
125 char aspect_ratio_info_present_flag;
126 unsigned int aspect_ratio_idc;
127 unsigned int sar_width;
128 unsigned int sar_height;
129 char video_signal_type_present_flag;
130 unsigned int video_format;
131 char video_full_range_flag;
132 char colour_description_present_flag;
133 unsigned int colour_primaries;
134 unsigned int transfer_characteristics;
135 unsigned int matrix_coeffs;
136
137 // Oddments.
138 char uniform_spacing_flag;
139 char output_flag_present_flag;
140 char cabac_init_present_flag;
141 unsigned int num_extra_slice_header_bits;
142 char lists_modification_present_flag;
143 char pps_slice_chroma_qp_offsets_present_flag;
144 char pps_slice_chroma_offset_list_enabled_flag;
145} VAAPIEncodeH265MiscSequenceParams;
146
147// This structure contains all possibly-useful per-slice syntax elements
148// which are not already contained in the various VAAPI structures.
149typedef struct VAAPIEncodeH265MiscSliceParams {
150 // Slice segments.
151 char first_slice_segment_in_pic_flag;
152 unsigned int slice_segment_address;
153
154 // Short-term reference picture sets.
155 char short_term_ref_pic_set_sps_flag;
156 unsigned int short_term_ref_pic_idx;
157 VAAPIEncodeH265STRPS st_ref_pic_set;
158
159 // Deblocking filter.
160 char deblocking_filter_override_flag;
161
162 // Oddments.
163 char slice_reserved_flag[8];
164 char no_output_of_prior_pics_flag;
165 char pic_output_flag;
166} VAAPIEncodeH265MiscSliceParams;
167
168typedef struct VAAPIEncodeH265Slice {
169 VAAPIEncodeH265MiscSliceParams misc_slice_params;
170
171 int64_t pic_order_cnt;
172} VAAPIEncodeH265Slice;
173
174typedef struct VAAPIEncodeH265Context {
175 VAAPIEncodeH265MiscSequenceParams misc_sequence_params;
176
177 unsigned int ctu_width;
178 unsigned int ctu_height;
179
180 int fixed_qp_idr;
181 int fixed_qp_p;
182 int fixed_qp_b;
183
184 int64_t last_idr_frame;
185} VAAPIEncodeH265Context;
186
187
188#define vseq_var(name) vseq->name, name
189#define vseq_field(name) vseq->seq_fields.bits.name, name
190#define vpic_var(name) vpic->name, name
191#define vpic_field(name) vpic->pic_fields.bits.name, name
192#define vslice_var(name) vslice->name, name
193#define vslice_field(name) vslice->slice_fields.bits.name, name
194#define mseq_var(name) mseq->name, name
195#define mslice_var(name) mslice->name, name
196#define mstrps_var(name) mstrps->name, name
197
198static void vaapi_encode_h265_write_nal_unit_header(PutBitContext *pbc,
199 int nal_unit_type)
200{
201 u(1, 0, forbidden_zero_bit);
202 u(6, nal_unit_type, nal_unit_type);
203 u(6, 0, nuh_layer_id);
204 u(3, 1, nuh_temporal_id_plus1);
205}
206
207static void vaapi_encode_h265_write_rbsp_trailing_bits(PutBitContext *pbc)
208{
209 u(1, 1, rbsp_stop_one_bit);
210 while (put_bits_count(pbc) & 7)
211 u(1, 0, rbsp_alignment_zero_bit);
212}
213
214static void vaapi_encode_h265_write_profile_tier_level(PutBitContext *pbc,
215 VAAPIEncodeContext *ctx)
216{
217 VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
218 VAAPIEncodeH265Context *priv = ctx->priv_data;
219 VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
220 int j;
221
222 if (1) {
223 u(2, 0, general_profile_space);
224 u(1, vseq_var(general_tier_flag));
225 u(5, vseq_var(general_profile_idc));
226
227 for (j = 0; j < 32; j++) {
228 u(1, mseq_var(general_profile_compatibility_flag[j]));
229 }
230
231 u(1, mseq_var(general_progressive_source_flag));
232 u(1, mseq_var(general_interlaced_source_flag));
233 u(1, mseq_var(general_non_packed_constraint_flag));
234 u(1, mseq_var(general_frame_only_constraint_flag));
235
236 if (0) {
237 // Not main profile.
238 // Lots of extra constraint flags.
239 } else {
240 // put_bits only handles up to 31 bits.
241 u(23, 0, general_reserved_zero_43bits);
242 u(20, 0, general_reserved_zero_43bits);
243 }
244
245 if (vseq->general_profile_idc >= 1 && vseq->general_profile_idc <= 5) {
246 u(1, mseq_var(general_inbld_flag));
247 } else {
248 u(1, 0, general_reserved_zero_bit);
249 }
250 }
251
252 u(8, vseq_var(general_level_idc));
253
254 // No sublayers.
255}
256
257static void vaapi_encode_h265_write_vps(PutBitContext *pbc,
258 VAAPIEncodeContext *ctx)
259{
260 VAAPIEncodeH265Context *priv = ctx->priv_data;
261 VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
262 int i, j;
263
264 vaapi_encode_h265_write_nal_unit_header(pbc, NAL_VPS);
265
266 u(4, mseq->video_parameter_set_id, vps_video_parameter_set_id);
267
268 u(1, 1, vps_base_layer_internal_flag);
269 u(1, 1, vps_base_layer_available_flag);
270 u(6, mseq_var(vps_max_layers_minus1));
271 u(3, mseq_var(vps_max_sub_layers_minus1));
272 u(1, mseq_var(vps_temporal_id_nesting_flag));
273
274 u(16, 0xffff, vps_reserved_0xffff_16bits);
275
276 vaapi_encode_h265_write_profile_tier_level(pbc, ctx);
277
278 u(1, mseq_var(vps_sub_layer_ordering_info_present_flag));
279 for (i = (mseq->vps_sub_layer_ordering_info_present_flag ?
280 0 : mseq->vps_max_sub_layers_minus1);
281 i <= mseq->vps_max_sub_layers_minus1; i++) {
282 ue(mseq_var(vps_max_dec_pic_buffering_minus1[i]));
283 ue(mseq_var(vps_max_num_reorder_pics[i]));
284 ue(mseq_var(vps_max_latency_increase_plus1[i]));
285 }
286
287 u(6, mseq_var(vps_max_layer_id));
288 ue(mseq_var(vps_num_layer_sets_minus1));
289 for (i = 1; i <= mseq->vps_num_layer_sets_minus1; i++) {
290 for (j = 0; j < mseq->vps_max_layer_id; j++)
291 u(1, mseq_var(layer_id_included_flag[i][j]));
292 }
293
294 u(1, mseq_var(vps_timing_info_present_flag));
295 if (mseq->vps_timing_info_present_flag) {
296 u(1, 0, put_bits_hack_zero_bit);
297 u(31, mseq_var(vps_num_units_in_tick));
298 u(1, 0, put_bits_hack_zero_bit);
299 u(31, mseq_var(vps_time_scale));
300 u(1, mseq_var(vps_poc_proportional_to_timing_flag));
301 if (mseq->vps_poc_proportional_to_timing_flag) {
302 ue(mseq_var(vps_num_ticks_poc_diff_minus1));
303 }
304 ue(0, vps_num_hrd_parameters);
305 }
306
307 u(1, 0, vps_extension_flag);
308
309 vaapi_encode_h265_write_rbsp_trailing_bits(pbc);
310}
311
312static void vaapi_encode_h265_write_st_ref_pic_set(PutBitContext *pbc,
313 int st_rps_idx,
314 VAAPIEncodeH265STRPS *mstrps)
315{
316 int i;
317
318 if (st_rps_idx != 0)
319 u(1, mstrps_var(inter_ref_pic_set_prediction_flag));
320
321 if (mstrps->inter_ref_pic_set_prediction_flag) {
322 av_assert0(0 && "inter ref pic set prediction not supported");
323 } else {
324 ue(mstrps_var(num_negative_pics));
325 ue(mstrps_var(num_positive_pics));
326
327 for (i = 0; i < mstrps->num_negative_pics; i++) {
328 ue(mstrps_var(delta_poc_s0_minus1[i]));
329 u(1, mstrps_var(used_by_curr_pic_s0_flag[i]));
330 }
331 for (i = 0; i < mstrps->num_positive_pics; i++) {
332 ue(mstrps_var(delta_poc_s1_minus1[i]));
333 u(1, mstrps_var(used_by_curr_pic_s1_flag[i]));
334 }
335 }
336}
337
338static void vaapi_encode_h265_write_vui_parameters(PutBitContext *pbc,
339 VAAPIEncodeContext *ctx)
340{
341 VAAPIEncodeH265Context *priv = ctx->priv_data;
342 VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
343
344 u(1, mseq_var(aspect_ratio_info_present_flag));
345 if (mseq->aspect_ratio_info_present_flag) {
346 u(8, mseq_var(aspect_ratio_idc));
347 if (mseq->aspect_ratio_idc == 255) {
348 u(16, mseq_var(sar_width));
349 u(16, mseq_var(sar_height));
350 }
351 }
352
353 u(1, 0, overscan_info_present_flag);
354
355 u(1, mseq_var(video_signal_type_present_flag));
356 if (mseq->video_signal_type_present_flag) {
357 u(3, mseq_var(video_format));
358 u(1, mseq_var(video_full_range_flag));
359 u(1, mseq_var(colour_description_present_flag));
360 if (mseq->colour_description_present_flag) {
361 u(8, mseq_var(colour_primaries));
362 u(8, mseq_var(transfer_characteristics));
363 u(8, mseq_var(matrix_coeffs));
364 }
365 }
366
367 u(1, 0, chroma_loc_info_present_flag);
368 u(1, 0, neutral_chroma_indication_flag);
369 u(1, 0, field_seq_flag);
370 u(1, 0, frame_field_info_present_flag);
371 u(1, 0, default_display_window_flag);
372 u(1, 0, vui_timing_info_present_flag);
373 u(1, 0, bitstream_restriction_flag_flag);
374}
375
376static void vaapi_encode_h265_write_sps(PutBitContext *pbc,
377 VAAPIEncodeContext *ctx)
378{
379 VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
380 VAAPIEncodeH265Context *priv = ctx->priv_data;
381 VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
382 int i;
383
384 vaapi_encode_h265_write_nal_unit_header(pbc, NAL_SPS);
385
386 u(4, mseq->video_parameter_set_id, sps_video_parameter_set_id);
387
388 u(3, mseq_var(sps_max_sub_layers_minus1));
389 u(1, mseq_var(sps_temporal_id_nesting_flag));
390
391 vaapi_encode_h265_write_profile_tier_level(pbc, ctx);
392
393 ue(mseq->seq_parameter_set_id, sps_seq_parameter_set_id);
394 ue(vseq_field(chroma_format_idc));
395 if (vseq->seq_fields.bits.chroma_format_idc == 3)
396 u(1, 0, separate_colour_plane_flag);
397
398 ue(vseq_var(pic_width_in_luma_samples));
399 ue(vseq_var(pic_height_in_luma_samples));
400
401 u(1, mseq_var(conformance_window_flag));
402 if (mseq->conformance_window_flag) {
403 ue(mseq_var(conf_win_left_offset));
404 ue(mseq_var(conf_win_right_offset));
405 ue(mseq_var(conf_win_top_offset));
406 ue(mseq_var(conf_win_bottom_offset));
407 }
408
409 ue(vseq_field(bit_depth_luma_minus8));
410 ue(vseq_field(bit_depth_chroma_minus8));
411
412 ue(mseq_var(log2_max_pic_order_cnt_lsb_minus4));
413
414 u(1, mseq_var(sps_sub_layer_ordering_info_present_flag));
415 for (i = (mseq->sps_sub_layer_ordering_info_present_flag ?
416 0 : mseq->sps_max_sub_layers_minus1);
417 i <= mseq->sps_max_sub_layers_minus1; i++) {
418 ue(mseq_var(sps_max_dec_pic_buffering_minus1[i]));
419 ue(mseq_var(sps_max_num_reorder_pics[i]));
420 ue(mseq_var(sps_max_latency_increase_plus1[i]));
421 }
422
423 ue(vseq_var(log2_min_luma_coding_block_size_minus3));
424 ue(vseq_var(log2_diff_max_min_luma_coding_block_size));
425 ue(vseq_var(log2_min_transform_block_size_minus2));
426 ue(vseq_var(log2_diff_max_min_transform_block_size));
427 ue(vseq_var(max_transform_hierarchy_depth_inter));
428 ue(vseq_var(max_transform_hierarchy_depth_intra));
429
430 u(1, vseq_field(scaling_list_enabled_flag));
431 if (vseq->seq_fields.bits.scaling_list_enabled_flag) {
432 u(1, 0, sps_scaling_list_data_present_flag);
433 }
434
435 u(1, vseq_field(amp_enabled_flag));
436 u(1, vseq_field(sample_adaptive_offset_enabled_flag));
437
438 u(1, vseq_field(pcm_enabled_flag));
439 if (vseq->seq_fields.bits.pcm_enabled_flag) {
440 u(4, vseq_var(pcm_sample_bit_depth_luma_minus1));
441 u(4, vseq_var(pcm_sample_bit_depth_chroma_minus1));
442 ue(vseq_var(log2_min_pcm_luma_coding_block_size_minus3));
443 ue(vseq->log2_max_pcm_luma_coding_block_size_minus3 -
444 vseq->log2_min_pcm_luma_coding_block_size_minus3,
445 log2_diff_max_min_pcm_luma_coding_block_size);
446 u(1, vseq_field(pcm_loop_filter_disabled_flag));
447 }
448
449 ue(mseq_var(num_short_term_ref_pic_sets));
450 for (i = 0; i < mseq->num_short_term_ref_pic_sets; i++)
451 vaapi_encode_h265_write_st_ref_pic_set(pbc, i,
452 &mseq->st_ref_pic_set[i]);
453
454 u(1, mseq_var(long_term_ref_pics_present_flag));
455 if (mseq->long_term_ref_pics_present_flag) {
456 ue(0, num_long_term_ref_pics_sps);
457 }
458
459 u(1, vseq_field(sps_temporal_mvp_enabled_flag));
460 u(1, vseq_field(strong_intra_smoothing_enabled_flag));
461
462 u(1, mseq_var(vui_parameters_present_flag));
463 if (mseq->vui_parameters_present_flag) {
464 vaapi_encode_h265_write_vui_parameters(pbc, ctx);
465 }
466
467 u(1, 0, sps_extension_present_flag);
468
469 vaapi_encode_h265_write_rbsp_trailing_bits(pbc);
470}
471
472static void vaapi_encode_h265_write_pps(PutBitContext *pbc,
473 VAAPIEncodeContext *ctx)
474{
475 VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
476 VAAPIEncodeH265Context *priv = ctx->priv_data;
477 VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
478 int i;
479
480 vaapi_encode_h265_write_nal_unit_header(pbc, NAL_PPS);
481
482 ue(vpic->slice_pic_parameter_set_id, pps_pic_parameter_set_id);
483 ue(mseq->seq_parameter_set_id, pps_seq_parameter_set_id);
484
485 u(1, vpic_field(dependent_slice_segments_enabled_flag));
486 u(1, mseq_var(output_flag_present_flag));
487 u(3, mseq_var(num_extra_slice_header_bits));
488 u(1, vpic_field(sign_data_hiding_enabled_flag));
489 u(1, mseq_var(cabac_init_present_flag));
490
491 ue(vpic_var(num_ref_idx_l0_default_active_minus1));
492 ue(vpic_var(num_ref_idx_l1_default_active_minus1));
493
494 se(vpic->pic_init_qp - 26, init_qp_minus26);
495
496 u(1, vpic_field(constrained_intra_pred_flag));
497 u(1, vpic_field(transform_skip_enabled_flag));
498
499 u(1, vpic_field(cu_qp_delta_enabled_flag));
500 if (vpic->pic_fields.bits.cu_qp_delta_enabled_flag)
501 ue(vpic_var(diff_cu_qp_delta_depth));
502
503 se(vpic_var(pps_cb_qp_offset));
504 se(vpic_var(pps_cr_qp_offset));
505
506 u(1, mseq_var(pps_slice_chroma_qp_offsets_present_flag));
507 u(1, vpic_field(weighted_pred_flag));
508 u(1, vpic_field(weighted_bipred_flag));
509 u(1, vpic_field(transquant_bypass_enabled_flag));
510 u(1, vpic_field(tiles_enabled_flag));
511 u(1, vpic_field(entropy_coding_sync_enabled_flag));
512
513 if (vpic->pic_fields.bits.tiles_enabled_flag) {
514 ue(vpic_var(num_tile_columns_minus1));
515 ue(vpic_var(num_tile_rows_minus1));
516 u(1, mseq_var(uniform_spacing_flag));
517 if (!mseq->uniform_spacing_flag) {
518 for (i = 0; i < vpic->num_tile_columns_minus1; i++)
519 ue(vpic_var(column_width_minus1[i]));
520 for (i = 0; i < vpic->num_tile_rows_minus1; i++)
521 ue(vpic_var(row_height_minus1[i]));
522 }
523 u(1, vpic_field(loop_filter_across_tiles_enabled_flag));
524 }
525
526 u(1, vpic_field(pps_loop_filter_across_slices_enabled_flag));
527 u(1, mseq_var(deblocking_filter_control_present_flag));
528 if (mseq->deblocking_filter_control_present_flag) {
529 u(1, mseq_var(deblocking_filter_override_enabled_flag));
530 u(1, mseq_var(pps_deblocking_filter_disabled_flag));
531 if (!mseq->pps_deblocking_filter_disabled_flag) {
532 se(mseq_var(pps_beta_offset_div2));
533 se(mseq_var(pps_tc_offset_div2));
534 }
535 }
536
537 u(1, 0, pps_scaling_list_data_present_flag);
538 // No scaling list data.
539
540 u(1, mseq_var(lists_modification_present_flag));
541 ue(vpic_var(log2_parallel_merge_level_minus2));
542 u(1, 0, slice_segment_header_extension_present_flag);
543 u(1, 0, pps_extension_present_flag);
544
545 vaapi_encode_h265_write_rbsp_trailing_bits(pbc);
546}
547
548static void vaapi_encode_h265_write_slice_header2(PutBitContext *pbc,
549 VAAPIEncodeContext *ctx,
550 VAAPIEncodePicture *pic,
551 VAAPIEncodeSlice *slice)
552{
553 VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
554 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
555 VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
556 VAAPIEncodeH265Context *priv = ctx->priv_data;
557 VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
558 VAAPIEncodeH265Slice *pslice = slice->priv_data;
559 VAAPIEncodeH265MiscSliceParams *mslice = &pslice->misc_slice_params;
560 int i;
561
562 vaapi_encode_h265_write_nal_unit_header(pbc, vpic->nal_unit_type);
563
564 u(1, mslice_var(first_slice_segment_in_pic_flag));
565 if (vpic->nal_unit_type >= NAL_BLA_W_LP &&
566 vpic->nal_unit_type <= 23)
567 u(1, mslice_var(no_output_of_prior_pics_flag));
568
569 ue(vslice_var(slice_pic_parameter_set_id));
570
571 if (!mslice->first_slice_segment_in_pic_flag) {
572 if (vpic->pic_fields.bits.dependent_slice_segments_enabled_flag)
573 u(1, vslice_field(dependent_slice_segment_flag));
574 u(av_log2((priv->ctu_width * priv->ctu_height) - 1) + 1,
575 mslice_var(slice_segment_address));
576 }
577 if (!vslice->slice_fields.bits.dependent_slice_segment_flag) {
578 for (i = 0; i < mseq->num_extra_slice_header_bits; i++)
579 u(1, mslice_var(slice_reserved_flag[i]));
580
581 ue(vslice_var(slice_type));
582 if (mseq->output_flag_present_flag)
583 u(1, 1, pic_output_flag);
584 if (vseq->seq_fields.bits.separate_colour_plane_flag)
585 u(2, vslice_field(colour_plane_id));
586 if (vpic->nal_unit_type != NAL_IDR_W_RADL &&
587 vpic->nal_unit_type != NAL_IDR_N_LP) {
588 u(4 + mseq->log2_max_pic_order_cnt_lsb_minus4,
589 (pslice->pic_order_cnt &
590 ((1 << (mseq->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1)),
591 slice_pic_order_cnt_lsb);
592
593 u(1, mslice_var(short_term_ref_pic_set_sps_flag));
594 if (!mslice->short_term_ref_pic_set_sps_flag) {
595 vaapi_encode_h265_write_st_ref_pic_set(pbc, mseq->num_short_term_ref_pic_sets,
596 &mslice->st_ref_pic_set);
597 } else if (mseq->num_short_term_ref_pic_sets > 1) {
598 u(av_log2(mseq->num_short_term_ref_pic_sets - 1) + 1,
599 mslice_var(short_term_ref_pic_idx));
600 }
601
602 if (mseq->long_term_ref_pics_present_flag) {
603 av_assert0(0);
604 }
605
606 if (vseq->seq_fields.bits.sps_temporal_mvp_enabled_flag) {
607 u(1, vslice_field(slice_temporal_mvp_enabled_flag));
608 }
609
610 if (vseq->seq_fields.bits.sample_adaptive_offset_enabled_flag) {
611 u(1, vslice_field(slice_sao_luma_flag));
612 if (!vseq->seq_fields.bits.separate_colour_plane_flag &&
613 vseq->seq_fields.bits.chroma_format_idc != 0) {
614 u(1, vslice_field(slice_sao_chroma_flag));
615 }
616 }
617
618 if (vslice->slice_type == P_SLICE || vslice->slice_type == B_SLICE) {
619 u(1, vslice_field(num_ref_idx_active_override_flag));
620 if (vslice->slice_fields.bits.num_ref_idx_active_override_flag) {
621 ue(vslice_var(num_ref_idx_l0_active_minus1));
622 if (vslice->slice_type == B_SLICE) {
623 ue(vslice_var(num_ref_idx_l1_active_minus1));
624 }
625 }
626
627 if (mseq->lists_modification_present_flag) {
628 av_assert0(0);
629 // ref_pic_lists_modification()
630 }
631 if (vslice->slice_type == B_SLICE) {
632 u(1, vslice_field(mvd_l1_zero_flag));
633 }
634 if (mseq->cabac_init_present_flag) {
635 u(1, vslice_field(cabac_init_flag));
636 }
637 if (vslice->slice_fields.bits.slice_temporal_mvp_enabled_flag) {
638 if (vslice->slice_type == B_SLICE)
639 u(1, vslice_field(collocated_from_l0_flag));
640 ue(vpic->collocated_ref_pic_index, collocated_ref_idx);
641 }
642 if ((vpic->pic_fields.bits.weighted_pred_flag &&
643 vslice->slice_type == P_SLICE) ||
644 (vpic->pic_fields.bits.weighted_bipred_flag &&
645 vslice->slice_type == B_SLICE)) {
646 av_assert0(0);
647 // pred_weight_table()
648 }
649 ue(5 - vslice->max_num_merge_cand, five_minus_max_num_merge_cand);
650 }
651
652 se(vslice_var(slice_qp_delta));
653 if (mseq->pps_slice_chroma_qp_offsets_present_flag) {
654 se(vslice_var(slice_cb_qp_offset));
655 se(vslice_var(slice_cr_qp_offset));
656 }
657 if (mseq->pps_slice_chroma_offset_list_enabled_flag) {
658 u(1, 0, cu_chroma_qp_offset_enabled_flag);
659 }
660 if (mseq->deblocking_filter_override_enabled_flag) {
661 u(1, mslice_var(deblocking_filter_override_flag));
662 }
663 if (mslice->deblocking_filter_override_flag) {
664 u(1, vslice_field(slice_deblocking_filter_disabled_flag));
665 if (!vslice->slice_fields.bits.slice_deblocking_filter_disabled_flag) {
666 se(vslice_var(slice_beta_offset_div2));
667 se(vslice_var(slice_tc_offset_div2));
668 }
669 }
670 if (vpic->pic_fields.bits.pps_loop_filter_across_slices_enabled_flag &&
671 (vslice->slice_fields.bits.slice_sao_luma_flag ||
672 vslice->slice_fields.bits.slice_sao_chroma_flag ||
673 vslice->slice_fields.bits.slice_deblocking_filter_disabled_flag)) {
674 u(1, vslice_field(slice_loop_filter_across_slices_enabled_flag));
675 }
676 }
677
678 if (vpic->pic_fields.bits.tiles_enabled_flag ||
679 vpic->pic_fields.bits.entropy_coding_sync_enabled_flag) {
680 // num_entry_point_offsets
681 }
682
683 if (0) {
684 // slice_segment_header_extension_length
685 }
686 }
687
688 u(1, 1, alignment_bit_equal_to_one);
689 while (put_bits_count(pbc) & 7)
690 u(1, 0, alignment_bit_equal_to_zero);
691}
692
693static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
694 char *data, size_t *data_len)
695{
696 VAAPIEncodeContext *ctx = avctx->priv_data;
697 PutBitContext pbc;
698 char tmp[256];
699 int err;
700 size_t nal_len, bit_len, bit_pos, next_len;
701
702 bit_len = *data_len;
703 bit_pos = 0;
704
705 init_put_bits(&pbc, tmp, sizeof(tmp));
706 vaapi_encode_h265_write_vps(&pbc, ctx);
707 nal_len = put_bits_count(&pbc);
708 flush_put_bits(&pbc);
709
710 next_len = bit_len - bit_pos;
711 err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
712 &next_len,
713 tmp, nal_len);
714 if (err < 0)
715 return err;
716 bit_pos += next_len;
717
718 init_put_bits(&pbc, tmp, sizeof(tmp));
719 vaapi_encode_h265_write_sps(&pbc, ctx);
720 nal_len = put_bits_count(&pbc);
721 flush_put_bits(&pbc);
722
723 next_len = bit_len - bit_pos;
724 err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
725 &next_len,
726 tmp, nal_len);
727 if (err < 0)
728 return err;
729 bit_pos += next_len;
730
731 init_put_bits(&pbc, tmp, sizeof(tmp));
732 vaapi_encode_h265_write_pps(&pbc, ctx);
733 nal_len = put_bits_count(&pbc);
734 flush_put_bits(&pbc);
735
736 next_len = bit_len - bit_pos;
737 err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
738 &next_len,
739 tmp, nal_len);
740 if (err < 0)
741 return err;
742 bit_pos += next_len;
743
744 *data_len = bit_pos;
745 return 0;
746}
747
748static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
749 VAAPIEncodePicture *pic,
750 VAAPIEncodeSlice *slice,
751 char *data, size_t *data_len)
752{
753 VAAPIEncodeContext *ctx = avctx->priv_data;
754 PutBitContext pbc;
755 char tmp[256];
756 size_t header_len;
757
758 init_put_bits(&pbc, tmp, sizeof(tmp));
759 vaapi_encode_h265_write_slice_header2(&pbc, ctx, pic, slice);
760 header_len = put_bits_count(&pbc);
761 flush_put_bits(&pbc);
762
763 return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
764 tmp, header_len);
765}
766
767static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
768{
769 VAAPIEncodeContext *ctx = avctx->priv_data;
770 VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
771 VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
772 VAAPIEncodeH265Context *priv = ctx->priv_data;
773 VAAPIEncodeH265MiscSequenceParams *mseq = &priv->misc_sequence_params;
774 int i;
775
776 {
777 // general_profile_space == 0.
778 vseq->general_profile_idc = 1; // Main profile (ctx->codec_profile?)
779 vseq->general_tier_flag = 0;
780
781 vseq->general_level_idc = avctx->level * 3;
782
783 vseq->intra_period = 0;
784 vseq->intra_idr_period = 0;
785 vseq->ip_period = 0;
786
787 vseq->pic_width_in_luma_samples = ctx->aligned_width;
788 vseq->pic_height_in_luma_samples = ctx->aligned_height;
789
790 vseq->seq_fields.bits.chroma_format_idc = 1; // 4:2:0.
791 vseq->seq_fields.bits.separate_colour_plane_flag = 0;
792 vseq->seq_fields.bits.bit_depth_luma_minus8 = 0; // 8-bit luma.
793 vseq->seq_fields.bits.bit_depth_chroma_minus8 = 0; // 8-bit chroma.
794 // Other misc flags all zero.
795
796 // These have to come from the capabilities of the encoder. We have
797 // no way to query it, so just hardcode ones which worked for me...
798 // CTB size from 8x8 to 32x32.
799 vseq->log2_min_luma_coding_block_size_minus3 = 0;
800 vseq->log2_diff_max_min_luma_coding_block_size = 2;
801 // Transform size from 4x4 to 32x32.
802 vseq->log2_min_transform_block_size_minus2 = 0;
803 vseq->log2_diff_max_min_transform_block_size = 3;
804 // Full transform hierarchy allowed (2-5).
805 vseq->max_transform_hierarchy_depth_inter = 3;
806 vseq->max_transform_hierarchy_depth_intra = 3;
807
808 vseq->vui_parameters_present_flag = 0;
809 }
810
811 {
812 vpic->decoded_curr_pic.picture_id = VA_INVALID_ID;
813 vpic->decoded_curr_pic.flags = VA_PICTURE_HEVC_INVALID;
814
815 for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
816 vpic->reference_frames[i].picture_id = VA_INVALID_ID;
817 vpic->reference_frames[i].flags = VA_PICTURE_HEVC_INVALID;
818 }
819
820 vpic->collocated_ref_pic_index = 0xff;
821
822 vpic->last_picture = 0;
823
824 vpic->pic_init_qp = priv->fixed_qp_idr;
825
826 vpic->diff_cu_qp_delta_depth = 0;
827 vpic->pps_cb_qp_offset = 0;
828 vpic->pps_cr_qp_offset = 0;
829
830 // tiles_enabled_flag == 0, so ignore num_tile_(rows|columns)_minus1.
831
832 vpic->log2_parallel_merge_level_minus2 = 0;
833
834 // No limit on size.
835 vpic->ctu_max_bitsize_allowed = 0;
836
837 vpic->num_ref_idx_l0_default_active_minus1 = 0;
838 vpic->num_ref_idx_l1_default_active_minus1 = 0;
839
840 vpic->slice_pic_parameter_set_id = 0;
841
842 vpic->pic_fields.bits.screen_content_flag = 0;
843 vpic->pic_fields.bits.enable_gpu_weighted_prediction = 0;
844
845 //vpic->pic_fields.bits.cu_qp_delta_enabled_flag = 1;
846 }
847
848 {
849 mseq->video_parameter_set_id = 5;
850 mseq->seq_parameter_set_id = 5;
851
852 mseq->vps_max_layers_minus1 = 0;
853 mseq->vps_max_sub_layers_minus1 = 0;
854 mseq->vps_temporal_id_nesting_flag = 1;
855 mseq->sps_max_sub_layers_minus1 = 0;
856 mseq->sps_temporal_id_nesting_flag = 1;
857
858 for (i = 0; i < 32; i++) {
859 mseq->general_profile_compatibility_flag[i] =
860 (i == vseq->general_profile_idc);
861 }
862
863 mseq->general_progressive_source_flag = 1;
864 mseq->general_interlaced_source_flag = 0;
865 mseq->general_non_packed_constraint_flag = 0;
866 mseq->general_frame_only_constraint_flag = 1;
867 mseq->general_inbld_flag = 0;
868
869 mseq->log2_max_pic_order_cnt_lsb_minus4 = 8;
870 mseq->vps_sub_layer_ordering_info_present_flag = 0;
871 mseq->vps_max_dec_pic_buffering_minus1[0] = 1;
872 mseq->vps_max_num_reorder_pics[0] = ctx->b_per_p;
873 mseq->vps_max_latency_increase_plus1[0] = 0;
874 mseq->sps_sub_layer_ordering_info_present_flag = 0;
875 mseq->sps_max_dec_pic_buffering_minus1[0] = 1;
876 mseq->sps_max_num_reorder_pics[0] = ctx->b_per_p;
877 mseq->sps_max_latency_increase_plus1[0] = 0;
878
879 mseq->vps_timing_info_present_flag = 1;
880 mseq->vps_num_units_in_tick = avctx->time_base.num;
881 mseq->vps_time_scale = avctx->time_base.den;
882 mseq->vps_poc_proportional_to_timing_flag = 1;
883 mseq->vps_num_ticks_poc_diff_minus1 = 0;
884
885 if (ctx->input_width != ctx->aligned_width ||
886 ctx->input_height != ctx->aligned_height) {
887 mseq->conformance_window_flag = 1;
888 mseq->conf_win_left_offset = 0;
889 mseq->conf_win_right_offset =
890 (ctx->aligned_width - ctx->input_width) / 2;
891 mseq->conf_win_top_offset = 0;
892 mseq->conf_win_bottom_offset =
893 (ctx->aligned_height - ctx->input_height) / 2;
894 } else {
895 mseq->conformance_window_flag = 0;
896 }
897
898 mseq->num_short_term_ref_pic_sets = 0;
899 // STRPSs should ideally be here rather than repeated in each slice.
900
901 mseq->vui_parameters_present_flag = 1;
902 if (avctx->sample_aspect_ratio.num != 0) {
903 mseq->aspect_ratio_info_present_flag = 1;
904 if (avctx->sample_aspect_ratio.num ==
905 avctx->sample_aspect_ratio.den) {
906 mseq->aspect_ratio_idc = 1;
907 } else {
908 mseq->aspect_ratio_idc = 255; // Extended SAR.
909 mseq->sar_width = avctx->sample_aspect_ratio.num;
910 mseq->sar_height = avctx->sample_aspect_ratio.den;
911 }
912 }
913 if (1) {
914 // Should this be conditional on some of these being set?
915 mseq->video_signal_type_present_flag = 1;
916 mseq->video_format = 5; // Unspecified.
917 mseq->video_full_range_flag = 0;
918 mseq->colour_description_present_flag = 1;
919 mseq->colour_primaries = avctx->color_primaries;
920 mseq->transfer_characteristics = avctx->color_trc;
921 mseq->matrix_coeffs = avctx->colorspace;
922 }
923 }
924
925 return 0;
926}
927
928static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
929 VAAPIEncodePicture *pic)
930{
931 VAAPIEncodeContext *ctx = avctx->priv_data;
932 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
933 VAAPIEncodeH265Context *priv = ctx->priv_data;
934 int i;
935
936 if (pic->type == PICTURE_TYPE_IDR) {
937 av_assert0(pic->display_order == pic->encode_order);
938 priv->last_idr_frame = pic->display_order;
939 } else {
940 av_assert0(pic->encode_order > priv->last_idr_frame);
941 // Display order need not be if we have RA[SD]L pictures, though.
942 }
943
944 vpic->decoded_curr_pic.picture_id = pic->recon_surface;
945 vpic->decoded_curr_pic.pic_order_cnt =
946 pic->display_order - priv->last_idr_frame;
947 vpic->decoded_curr_pic.flags = 0;
948
949 for (i = 0; i < pic->nb_refs; i++) {
950 VAAPIEncodePicture *ref = pic->refs[i];
951 av_assert0(ref);
952 vpic->reference_frames[i].picture_id = ref->recon_surface;
953 vpic->reference_frames[i].pic_order_cnt =
954 ref->display_order - priv->last_idr_frame;
955 vpic->reference_frames[i].flags =
956 (ref->display_order < pic->display_order ?
957 VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
958 (ref->display_order > pic->display_order ?
959 VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0);
960 }
961 for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
962 vpic->reference_frames[i].picture_id = VA_INVALID_ID;
963 vpic->reference_frames[i].flags = VA_PICTURE_HEVC_INVALID;
964 }
965
966 vpic->coded_buf = pic->output_buffer;
967
968 switch (pic->type) {
969 case PICTURE_TYPE_IDR:
970 vpic->nal_unit_type = NAL_IDR_W_RADL;
971 vpic->pic_fields.bits.idr_pic_flag = 1;
972 vpic->pic_fields.bits.coding_type = 1;
973 vpic->pic_fields.bits.reference_pic_flag = 1;
974 break;
975 case PICTURE_TYPE_I:
976 vpic->nal_unit_type = NAL_TRAIL_R;
977 vpic->pic_fields.bits.idr_pic_flag = 0;
978 vpic->pic_fields.bits.coding_type = 1;
979 vpic->pic_fields.bits.reference_pic_flag = 1;
980 break;
981 case PICTURE_TYPE_P:
982 vpic->nal_unit_type = NAL_TRAIL_R;
983 vpic->pic_fields.bits.idr_pic_flag = 0;
984 vpic->pic_fields.bits.coding_type = 2;
985 vpic->pic_fields.bits.reference_pic_flag = 1;
986 break;
987 case PICTURE_TYPE_B:
988 vpic->nal_unit_type = NAL_TRAIL_R;
989 vpic->pic_fields.bits.idr_pic_flag = 0;
990 vpic->pic_fields.bits.coding_type = 3;
991 vpic->pic_fields.bits.reference_pic_flag = 0;
992 break;
993 default:
994 av_assert0(0 && "invalid picture type");
995 }
996
997 pic->nb_slices = 1;
998
999 return 0;
1000}
1001
1002static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
1003 VAAPIEncodePicture *pic,
1004 VAAPIEncodeSlice *slice)
1005{
1006 VAAPIEncodeContext *ctx = avctx->priv_data;
1007 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
1008 VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
1009 VAAPIEncodeH265Context *priv = ctx->priv_data;
1010 VAAPIEncodeH265Slice *pslice;
1011 VAAPIEncodeH265MiscSliceParams *mslice;
1012 int i;
1013
1014 slice->priv_data = av_mallocz(sizeof(*pslice));
1015 if (!slice->priv_data)
1016 return AVERROR(ENOMEM);
1017 pslice = slice->priv_data;
1018 mslice = &pslice->misc_slice_params;
1019
1020 // Currently we only support one slice per frame.
1021 vslice->slice_segment_address = 0;
1022 vslice->num_ctu_in_slice = priv->ctu_width * priv->ctu_height;
1023
1024 switch (pic->type) {
1025 case PICTURE_TYPE_IDR:
1026 case PICTURE_TYPE_I:
1027 vslice->slice_type = I_SLICE;
1028 break;
1029 case PICTURE_TYPE_P:
1030 vslice->slice_type = P_SLICE;
1031 break;
1032 case PICTURE_TYPE_B:
1033 vslice->slice_type = B_SLICE;
1034 break;
1035 default:
1036 av_assert0(0 && "invalid picture type");
1037 }
1038
1039 vslice->slice_pic_parameter_set_id = vpic->slice_pic_parameter_set_id;
1040
1041 pslice->pic_order_cnt = pic->display_order - priv->last_idr_frame;
1042
1043 for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
1044 vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
1045 vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
1046 vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
1047 vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
1048 }
1049
1050 av_assert0(pic->nb_refs <= 2);
1051 if (pic->nb_refs >= 1) {
1052 // Backward reference for P or B frame.
1053 av_assert0(pic->type == PICTURE_TYPE_P ||
1054 pic->type == PICTURE_TYPE_B);
1055
1056 vslice->num_ref_idx_l0_active_minus1 = 0;
1057 vslice->ref_pic_list0[0] = vpic->reference_frames[0];
1058 }
1059 if (pic->nb_refs >= 2) {
1060 // Forward reference for B frame.
1061 av_assert0(pic->type == PICTURE_TYPE_B);
1062
1063 vslice->num_ref_idx_l1_active_minus1 = 0;
1064 vslice->ref_pic_list1[0] = vpic->reference_frames[1];
1065 }
1066
1067 vslice->max_num_merge_cand = 5;
1068
1069 if (pic->type == PICTURE_TYPE_B)
1070 vslice->slice_qp_delta = priv->fixed_qp_b - vpic->pic_init_qp;
1071 else if (pic->type == PICTURE_TYPE_P)
1072 vslice->slice_qp_delta = priv->fixed_qp_p - vpic->pic_init_qp;
1073 else
1074 vslice->slice_qp_delta = priv->fixed_qp_idr - vpic->pic_init_qp;
1075
1076 vslice->slice_fields.bits.last_slice_of_pic_flag = 1;
1077
1078 mslice->first_slice_segment_in_pic_flag = 1;
1079
1080 if (pic->type == PICTURE_TYPE_IDR) {
1081 // No reference pictures.
1082 } else if (0) {
1083 mslice->short_term_ref_pic_set_sps_flag = 1;
1084 mslice->short_term_ref_pic_idx = 0;
1085 } else {
1086 VAAPIEncodePicture *st;
1087 int used;
1088
1089 mslice->short_term_ref_pic_set_sps_flag = 0;
1090 mslice->st_ref_pic_set.inter_ref_pic_set_prediction_flag = 0;
1091
1092 for (st = ctx->pic_start; st; st = st->next) {
1093 if (st->encode_order >= pic->encode_order) {
1094 // Not yet in DPB.
1095 continue;
1096 }
1097 used = 0;
1098 for (i = 0; i < pic->nb_refs; i++) {
1099 if (pic->refs[i] == st)
1100 used = 1;
1101 }
1102 if (!used) {
1103 // Currently true, but need not be.
1104 continue;
1105 }
1106 // This only works for one instance of each (delta_poc_sN_minus1
1107 // is relative to the previous frame in the list, not relative to
1108 // the current frame directly).
1109 if (st->display_order < pic->display_order) {
1110 i = mslice->st_ref_pic_set.num_negative_pics;
1111 mslice->st_ref_pic_set.delta_poc_s0_minus1[i] =
1112 pic->display_order - st->display_order - 1;
1113 mslice->st_ref_pic_set.used_by_curr_pic_s0_flag[i] = used;
1114 ++mslice->st_ref_pic_set.num_negative_pics;
1115 } else {
1116 i = mslice->st_ref_pic_set.num_positive_pics;
1117 mslice->st_ref_pic_set.delta_poc_s1_minus1[i] =
1118 st->display_order - pic->display_order - 1;
1119 mslice->st_ref_pic_set.used_by_curr_pic_s1_flag[i] = used;
1120 ++mslice->st_ref_pic_set.num_positive_pics;
1121 }
1122 }
1123 }
1124
1125 return 0;
1126}
1127
1128static VAConfigAttrib vaapi_encode_h265_config_attributes[] = {
1129 { .type = VAConfigAttribRTFormat,
1130 .value = VA_RT_FORMAT_YUV420 },
1131 { .type = VAConfigAttribRateControl,
1132 .value = VA_RC_CQP },
1133 { .type = VAConfigAttribEncPackedHeaders,
1134 .value = (VA_ENC_PACKED_HEADER_SEQUENCE |
1135 VA_ENC_PACKED_HEADER_SLICE) },
1136};
1137
1138static av_cold int vaapi_encode_h265_init_internal(AVCodecContext *avctx)
1139{
1140 VAAPIEncodeContext *ctx = avctx->priv_data;
1141 VAAPIEncodeH265Context *priv = ctx->priv_data;
1142
1143 switch (avctx->profile) {
1144 case FF_PROFILE_HEVC_MAIN:
1145 case FF_PROFILE_UNKNOWN:
1146 ctx->va_profile = VAProfileHEVCMain;
1147 break;
1148 case FF_PROFILE_HEVC_MAIN_10:
1149 av_log(avctx, AV_LOG_ERROR, "H.265 main 10-bit profile "
1150 "is not supported.\n");
1151 return AVERROR_PATCHWELCOME;
1152 default:
1153 av_log(avctx, AV_LOG_ERROR, "Unknown H.265 profile %d.\n",
1154 avctx->profile);
1155 return AVERROR(EINVAL);
1156 }
1157 ctx->va_entrypoint = VAEntrypointEncSlice;
1158
1159 ctx->va_rc_mode = VA_RC_CQP;
1160
1161 ctx->input_width = avctx->width;
1162 ctx->input_height = avctx->height;
1163 ctx->aligned_width = FFALIGN(ctx->input_width, 16);
1164 ctx->aligned_height = FFALIGN(ctx->input_height, 16);
1165 priv->ctu_width = FFALIGN(ctx->aligned_width, 32) / 32;
1166 priv->ctu_height = FFALIGN(ctx->aligned_height, 32) / 32;
1167
1168 av_log(avctx, AV_LOG_VERBOSE, "Input %ux%u -> Aligned %ux%u -> CTU %ux%u.\n",
1169 ctx->input_width, ctx->input_height, ctx->aligned_width,
1170 ctx->aligned_height, priv->ctu_width, priv->ctu_height);
1171
1172 priv->fixed_qp_p = avctx->global_quality;
1173 if (avctx->i_quant_factor > 0.0)
1174 priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
1175 avctx->i_quant_offset) + 0.5);
1176 else
1177 priv->fixed_qp_idr = priv->fixed_qp_p;
1178 if (avctx->b_quant_factor > 0.0)
1179 priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
1180 avctx->b_quant_offset) + 0.5);
1181 else
1182 priv->fixed_qp_b = priv->fixed_qp_p;
1183 av_log(avctx, AV_LOG_DEBUG, "QP = %d / %d / %d for IDR / P / B frames.\n",
1184 priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1185
1186 ctx->config_attributes = vaapi_encode_h265_config_attributes;
1187 ctx->nb_config_attributes =
1188 FF_ARRAY_ELEMS(vaapi_encode_h265_config_attributes);
1189
1190 ctx->nb_recon_frames = 20;
1191
1192 return 0;
1193}
1194
1195static VAAPIEncodeType vaapi_encode_type_h265 = {
1196 .priv_data_size = sizeof(VAAPIEncodeH265Context),
1197
1198 .init = &vaapi_encode_h265_init_internal,
1199
1200 .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
1201 .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
1202
1203 .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
1204 .init_picture_params = &vaapi_encode_h265_init_picture_params,
1205
1206 .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
1207 .init_slice_params = &vaapi_encode_h265_init_slice_params,
1208
1209 .sequence_header_type = VAEncPackedHeaderSequence,
1210 .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
1211
1212 .slice_header_type = VAEncPackedHeaderHEVC_Slice,
1213 .write_slice_header = &vaapi_encode_h265_write_slice_header,
1214};
1215
1216static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
1217{
1218 return ff_vaapi_encode_init(avctx, &vaapi_encode_type_h265);
1219}
1220
1221static const AVCodecDefault vaapi_encode_h265_defaults[] = {
1222 { "profile", "1" },
1223 { "level", "51" },
1224 { "b", "0" },
1225 { "bf", "2" },
1226 { "g", "120" },
1227 { "global_quality", "25" },
1228 { "i_qfactor", "1.0" },
1229 { "i_qoffset", "0.0" },
1230 { "b_qfactor", "1.2" },
1231 { "b_qoffset", "0.0" },
1232 { NULL },
1233};
1234
1235static const AVClass vaapi_encode_h265_class = {
1236 .class_name = "h265_vaapi",
1237 .item_name = av_default_item_name,
1238 .version = LIBAVUTIL_VERSION_INT,
1239};
1240
1241AVCodec ff_hevc_vaapi_encoder = {
1242 .name = "hevc_vaapi",
1243 .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
1244 .type = AVMEDIA_TYPE_VIDEO,
1245 .id = AV_CODEC_ID_HEVC,
1246 .priv_data_size = sizeof(VAAPIEncodeContext),
1247 .init = &vaapi_encode_h265_init,
1248 .encode2 = &ff_vaapi_encode2,
1249 .close = &ff_vaapi_encode_close,
1250 .priv_class = &vaapi_encode_h265_class,
1251 .capabilities = AV_CODEC_CAP_DELAY,
1252 .defaults = vaapi_encode_h265_defaults,
1253 .pix_fmts = (const enum AVPixelFormat[]) {
1254 AV_PIX_FMT_VAAPI,
1255 AV_PIX_FMT_NONE,
1256 },
1257};