FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vaapi_encode_h265.c
Go to the documentation of this file.
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 <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 
40 typedef struct VAAPIEncodeH265STRPS {
42 
43  unsigned int num_negative_pics;
44  unsigned int num_positive_pics;
45 
48 
52 
53 // This structure contains all possibly-useful per-sequence syntax elements
54 // which are not already contained in the various VAAPI structures.
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;
65  unsigned int vps_max_layer_id;
70 
71  // Profile/tier/level parameters.
78 
79  // Decode/display ordering parameters.
89 
90  // Timing information.
92  unsigned int vps_num_units_in_tick;
93  unsigned int vps_time_scale;
96 
97  // Cropping information.
99  unsigned int conf_win_left_offset;
100  unsigned int conf_win_right_offset;
101  unsigned int conf_win_top_offset;
103 
104  // Short-term reference picture sets.
107 
108  // Long-term reference pictures.
111  struct {
114  } lt_ref_pic;
115 
116  // Deblocking filter control.
122 
123  // Video Usability Information.
126  unsigned int aspect_ratio_idc;
127  unsigned int sar_width;
128  unsigned int sar_height;
130  unsigned int video_format;
133  unsigned int colour_primaries;
135  unsigned int matrix_coeffs;
136 
137  // Oddments.
146 
147 // This structure contains all possibly-useful per-slice syntax elements
148 // which are not already contained in the various VAAPI structures.
150  // Slice segments.
152  unsigned int slice_segment_address;
153 
154  // Short-term reference picture sets.
158 
159  // Deblocking filter.
161 
162  // Oddments.
167 
168 typedef struct VAAPIEncodeH265Slice {
170 
171  int64_t pic_order_cnt;
173 
174 typedef struct VAAPIEncodeH265Context {
176 
177  unsigned int ctu_width;
178  unsigned int ctu_height;
179 
183 
184  int64_t last_idr_frame;
185 
186  // Rate control configuration.
187  struct {
188  VAEncMiscParameterBuffer misc;
189  VAEncMiscParameterRateControl rc;
190  } rc_params;
191  struct {
192  VAEncMiscParameterBuffer misc;
193  VAEncMiscParameterHRD hrd;
194  } hrd_params;
196 
197 typedef struct VAAPIEncodeH265Options {
198  int qp;
200 
201 
202 #define vseq_var(name) vseq->name, name
203 #define vseq_field(name) vseq->seq_fields.bits.name, name
204 #define vpic_var(name) vpic->name, name
205 #define vpic_field(name) vpic->pic_fields.bits.name, name
206 #define vslice_var(name) vslice->name, name
207 #define vslice_field(name) vslice->slice_fields.bits.name, name
208 #define mseq_var(name) mseq->name, name
209 #define mslice_var(name) mslice->name, name
210 #define mstrps_var(name) mstrps->name, name
211 
213  int nal_unit_type)
214 {
215  u(1, 0, forbidden_zero_bit);
216  u(6, nal_unit_type, nal_unit_type);
217  u(6, 0, nuh_layer_id);
218  u(3, 1, nuh_temporal_id_plus1);
219 }
220 
222 {
223  u(1, 1, rbsp_stop_one_bit);
224  while (put_bits_count(pbc) & 7)
225  u(1, 0, rbsp_alignment_zero_bit);
226 }
227 
230 {
231  VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
232  VAAPIEncodeH265Context *priv = ctx->priv_data;
234  int j;
235 
236  if (1) {
237  u(2, 0, general_profile_space);
238  u(1, vseq_var(general_tier_flag));
239  u(5, vseq_var(general_profile_idc));
240 
241  for (j = 0; j < 32; j++) {
242  u(1, mseq_var(general_profile_compatibility_flag[j]));
243  }
244 
245  u(1, mseq_var(general_progressive_source_flag));
246  u(1, mseq_var(general_interlaced_source_flag));
247  u(1, mseq_var(general_non_packed_constraint_flag));
248  u(1, mseq_var(general_frame_only_constraint_flag));
249 
250  if (0) {
251  // Not main profile.
252  // Lots of extra constraint flags.
253  } else {
254  // put_bits only handles up to 31 bits.
255  u(23, 0, general_reserved_zero_43bits);
256  u(20, 0, general_reserved_zero_43bits);
257  }
258 
259  if (vseq->general_profile_idc >= 1 && vseq->general_profile_idc <= 5) {
260  u(1, mseq_var(general_inbld_flag));
261  } else {
262  u(1, 0, general_reserved_zero_bit);
263  }
264  }
265 
266  u(8, vseq_var(general_level_idc));
267 
268  // No sublayers.
269 }
270 
273 {
274  VAAPIEncodeH265Context *priv = ctx->priv_data;
276  int i, j;
277 
279 
280  u(4, mseq->video_parameter_set_id, vps_video_parameter_set_id);
281 
282  u(1, 1, vps_base_layer_internal_flag);
283  u(1, 1, vps_base_layer_available_flag);
284  u(6, mseq_var(vps_max_layers_minus1));
285  u(3, mseq_var(vps_max_sub_layers_minus1));
286  u(1, mseq_var(vps_temporal_id_nesting_flag));
287 
288  u(16, 0xffff, vps_reserved_0xffff_16bits);
289 
291 
292  u(1, mseq_var(vps_sub_layer_ordering_info_present_flag));
294  0 : mseq->vps_max_sub_layers_minus1);
295  i <= mseq->vps_max_sub_layers_minus1; i++) {
296  ue(mseq_var(vps_max_dec_pic_buffering_minus1[i]));
297  ue(mseq_var(vps_max_num_reorder_pics[i]));
298  ue(mseq_var(vps_max_latency_increase_plus1[i]));
299  }
300 
301  u(6, mseq_var(vps_max_layer_id));
302  ue(mseq_var(vps_num_layer_sets_minus1));
303  for (i = 1; i <= mseq->vps_num_layer_sets_minus1; i++) {
304  for (j = 0; j < mseq->vps_max_layer_id; j++)
305  u(1, mseq_var(layer_id_included_flag[i][j]));
306  }
307 
308  u(1, mseq_var(vps_timing_info_present_flag));
309  if (mseq->vps_timing_info_present_flag) {
310  u(1, 0, put_bits_hack_zero_bit);
311  u(31, mseq_var(vps_num_units_in_tick));
312  u(1, 0, put_bits_hack_zero_bit);
313  u(31, mseq_var(vps_time_scale));
314  u(1, mseq_var(vps_poc_proportional_to_timing_flag));
316  ue(mseq_var(vps_num_ticks_poc_diff_minus1));
317  }
318  ue(0, vps_num_hrd_parameters);
319  }
320 
321  u(1, 0, vps_extension_flag);
322 
324 }
325 
327  int st_rps_idx,
328  VAAPIEncodeH265STRPS *mstrps)
329 {
330  int i;
331 
332  if (st_rps_idx != 0)
333  u(1, mstrps_var(inter_ref_pic_set_prediction_flag));
334 
335  if (mstrps->inter_ref_pic_set_prediction_flag) {
336  av_assert0(0 && "inter ref pic set prediction not supported");
337  } else {
338  ue(mstrps_var(num_negative_pics));
339  ue(mstrps_var(num_positive_pics));
340 
341  for (i = 0; i < mstrps->num_negative_pics; i++) {
342  ue(mstrps_var(delta_poc_s0_minus1[i]));
343  u(1, mstrps_var(used_by_curr_pic_s0_flag[i]));
344  }
345  for (i = 0; i < mstrps->num_positive_pics; i++) {
346  ue(mstrps_var(delta_poc_s1_minus1[i]));
347  u(1, mstrps_var(used_by_curr_pic_s1_flag[i]));
348  }
349  }
350 }
351 
354 {
355  VAAPIEncodeH265Context *priv = ctx->priv_data;
357 
358  u(1, mseq_var(aspect_ratio_info_present_flag));
359  if (mseq->aspect_ratio_info_present_flag) {
360  u(8, mseq_var(aspect_ratio_idc));
361  if (mseq->aspect_ratio_idc == 255) {
362  u(16, mseq_var(sar_width));
363  u(16, mseq_var(sar_height));
364  }
365  }
366 
367  u(1, 0, overscan_info_present_flag);
368 
369  u(1, mseq_var(video_signal_type_present_flag));
370  if (mseq->video_signal_type_present_flag) {
371  u(3, mseq_var(video_format));
372  u(1, mseq_var(video_full_range_flag));
373  u(1, mseq_var(colour_description_present_flag));
375  u(8, mseq_var(colour_primaries));
377  u(8, mseq_var(matrix_coeffs));
378  }
379  }
380 
381  u(1, 0, chroma_loc_info_present_flag);
382  u(1, 0, neutral_chroma_indication_flag);
383  u(1, 0, field_seq_flag);
384  u(1, 0, frame_field_info_present_flag);
385  u(1, 0, default_display_window_flag);
386  u(1, 0, vui_timing_info_present_flag);
387  u(1, 0, bitstream_restriction_flag_flag);
388 }
389 
392 {
393  VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
394  VAAPIEncodeH265Context *priv = ctx->priv_data;
396  int i;
397 
399 
400  u(4, mseq->video_parameter_set_id, sps_video_parameter_set_id);
401 
402  u(3, mseq_var(sps_max_sub_layers_minus1));
403  u(1, mseq_var(sps_temporal_id_nesting_flag));
404 
406 
407  ue(mseq->seq_parameter_set_id, sps_seq_parameter_set_id);
408  ue(vseq_field(chroma_format_idc));
409  if (vseq->seq_fields.bits.chroma_format_idc == 3)
410  u(1, 0, separate_colour_plane_flag);
411 
412  ue(vseq_var(pic_width_in_luma_samples));
413  ue(vseq_var(pic_height_in_luma_samples));
414 
415  u(1, mseq_var(conformance_window_flag));
416  if (mseq->conformance_window_flag) {
417  ue(mseq_var(conf_win_left_offset));
418  ue(mseq_var(conf_win_right_offset));
419  ue(mseq_var(conf_win_top_offset));
420  ue(mseq_var(conf_win_bottom_offset));
421  }
422 
423  ue(vseq_field(bit_depth_luma_minus8));
424  ue(vseq_field(bit_depth_chroma_minus8));
425 
426  ue(mseq_var(log2_max_pic_order_cnt_lsb_minus4));
427 
428  u(1, mseq_var(sps_sub_layer_ordering_info_present_flag));
430  0 : mseq->sps_max_sub_layers_minus1);
431  i <= mseq->sps_max_sub_layers_minus1; i++) {
432  ue(mseq_var(sps_max_dec_pic_buffering_minus1[i]));
433  ue(mseq_var(sps_max_num_reorder_pics[i]));
434  ue(mseq_var(sps_max_latency_increase_plus1[i]));
435  }
436 
437  ue(vseq_var(log2_min_luma_coding_block_size_minus3));
438  ue(vseq_var(log2_diff_max_min_luma_coding_block_size));
439  ue(vseq_var(log2_min_transform_block_size_minus2));
440  ue(vseq_var(log2_diff_max_min_transform_block_size));
441  ue(vseq_var(max_transform_hierarchy_depth_inter));
442  ue(vseq_var(max_transform_hierarchy_depth_intra));
443 
444  u(1, vseq_field(scaling_list_enabled_flag));
445  if (vseq->seq_fields.bits.scaling_list_enabled_flag) {
446  u(1, 0, sps_scaling_list_data_present_flag);
447  }
448 
449  u(1, vseq_field(amp_enabled_flag));
450  u(1, vseq_field(sample_adaptive_offset_enabled_flag));
451 
452  u(1, vseq_field(pcm_enabled_flag));
453  if (vseq->seq_fields.bits.pcm_enabled_flag) {
454  u(4, vseq_var(pcm_sample_bit_depth_luma_minus1));
455  u(4, vseq_var(pcm_sample_bit_depth_chroma_minus1));
456  ue(vseq_var(log2_min_pcm_luma_coding_block_size_minus3));
457  ue(vseq->log2_max_pcm_luma_coding_block_size_minus3 -
458  vseq->log2_min_pcm_luma_coding_block_size_minus3,
459  log2_diff_max_min_pcm_luma_coding_block_size);
460  u(1, vseq_field(pcm_loop_filter_disabled_flag));
461  }
462 
463  ue(mseq_var(num_short_term_ref_pic_sets));
464  for (i = 0; i < mseq->num_short_term_ref_pic_sets; i++)
466  &mseq->st_ref_pic_set[i]);
467 
468  u(1, mseq_var(long_term_ref_pics_present_flag));
470  ue(0, num_long_term_ref_pics_sps);
471  }
472 
473  u(1, vseq_field(sps_temporal_mvp_enabled_flag));
474  u(1, vseq_field(strong_intra_smoothing_enabled_flag));
475 
476  u(1, mseq_var(vui_parameters_present_flag));
477  if (mseq->vui_parameters_present_flag) {
479  }
480 
481  u(1, 0, sps_extension_present_flag);
482 
484 }
485 
488 {
489  VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
490  VAAPIEncodeH265Context *priv = ctx->priv_data;
492  int i;
493 
495 
496  ue(vpic->slice_pic_parameter_set_id, pps_pic_parameter_set_id);
497  ue(mseq->seq_parameter_set_id, pps_seq_parameter_set_id);
498 
499  u(1, vpic_field(dependent_slice_segments_enabled_flag));
500  u(1, mseq_var(output_flag_present_flag));
501  u(3, mseq_var(num_extra_slice_header_bits));
502  u(1, vpic_field(sign_data_hiding_enabled_flag));
503  u(1, mseq_var(cabac_init_present_flag));
504 
505  ue(vpic_var(num_ref_idx_l0_default_active_minus1));
506  ue(vpic_var(num_ref_idx_l1_default_active_minus1));
507 
508  se(vpic->pic_init_qp - 26, init_qp_minus26);
509 
510  u(1, vpic_field(constrained_intra_pred_flag));
511  u(1, vpic_field(transform_skip_enabled_flag));
512 
513  u(1, vpic_field(cu_qp_delta_enabled_flag));
514  if (vpic->pic_fields.bits.cu_qp_delta_enabled_flag)
515  ue(vpic_var(diff_cu_qp_delta_depth));
516 
517  se(vpic_var(pps_cb_qp_offset));
518  se(vpic_var(pps_cr_qp_offset));
519 
520  u(1, mseq_var(pps_slice_chroma_qp_offsets_present_flag));
521  u(1, vpic_field(weighted_pred_flag));
522  u(1, vpic_field(weighted_bipred_flag));
523  u(1, vpic_field(transquant_bypass_enabled_flag));
524  u(1, vpic_field(tiles_enabled_flag));
525  u(1, vpic_field(entropy_coding_sync_enabled_flag));
526 
527  if (vpic->pic_fields.bits.tiles_enabled_flag) {
528  ue(vpic_var(num_tile_columns_minus1));
529  ue(vpic_var(num_tile_rows_minus1));
530  u(1, mseq_var(uniform_spacing_flag));
531  if (!mseq->uniform_spacing_flag) {
532  for (i = 0; i < vpic->num_tile_columns_minus1; i++)
533  ue(vpic_var(column_width_minus1[i]));
534  for (i = 0; i < vpic->num_tile_rows_minus1; i++)
535  ue(vpic_var(row_height_minus1[i]));
536  }
537  u(1, vpic_field(loop_filter_across_tiles_enabled_flag));
538  }
539 
540  u(1, vpic_field(pps_loop_filter_across_slices_enabled_flag));
541  u(1, mseq_var(deblocking_filter_control_present_flag));
543  u(1, mseq_var(deblocking_filter_override_enabled_flag));
544  u(1, mseq_var(pps_deblocking_filter_disabled_flag));
546  se(mseq_var(pps_beta_offset_div2));
547  se(mseq_var(pps_tc_offset_div2));
548  }
549  }
550 
551  u(1, 0, pps_scaling_list_data_present_flag);
552  // No scaling list data.
553 
554  u(1, mseq_var(lists_modification_present_flag));
555  ue(vpic_var(log2_parallel_merge_level_minus2));
556  u(1, 0, slice_segment_header_extension_present_flag);
557  u(1, 0, pps_extension_present_flag);
558 
560 }
561 
564  VAAPIEncodePicture *pic,
565  VAAPIEncodeSlice *slice)
566 {
567  VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
568  VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
569  VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
570  VAAPIEncodeH265Context *priv = ctx->priv_data;
572  VAAPIEncodeH265Slice *pslice = slice->priv_data;
574  int i;
575 
576  vaapi_encode_h265_write_nal_unit_header(pbc, vpic->nal_unit_type);
577 
578  u(1, mslice_var(first_slice_segment_in_pic_flag));
579  if (vpic->nal_unit_type >= NAL_BLA_W_LP &&
580  vpic->nal_unit_type <= 23)
581  u(1, mslice_var(no_output_of_prior_pics_flag));
582 
583  ue(vslice_var(slice_pic_parameter_set_id));
584 
585  if (!mslice->first_slice_segment_in_pic_flag) {
586  if (vpic->pic_fields.bits.dependent_slice_segments_enabled_flag)
587  u(1, vslice_field(dependent_slice_segment_flag));
588  u(av_log2((priv->ctu_width * priv->ctu_height) - 1) + 1,
589  mslice_var(slice_segment_address));
590  }
591  if (!vslice->slice_fields.bits.dependent_slice_segment_flag) {
592  for (i = 0; i < mseq->num_extra_slice_header_bits; i++)
593  u(1, mslice_var(slice_reserved_flag[i]));
594 
595  ue(vslice_var(slice_type));
596  if (mseq->output_flag_present_flag)
597  u(1, 1, pic_output_flag);
598  if (vseq->seq_fields.bits.separate_colour_plane_flag)
599  u(2, vslice_field(colour_plane_id));
600  if (vpic->nal_unit_type != NAL_IDR_W_RADL &&
601  vpic->nal_unit_type != NAL_IDR_N_LP) {
603  (pslice->pic_order_cnt &
604  ((1 << (mseq->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1)),
605  slice_pic_order_cnt_lsb);
606 
607  u(1, mslice_var(short_term_ref_pic_set_sps_flag));
608  if (!mslice->short_term_ref_pic_set_sps_flag) {
610  &mslice->st_ref_pic_set);
611  } else if (mseq->num_short_term_ref_pic_sets > 1) {
612  u(av_log2(mseq->num_short_term_ref_pic_sets - 1) + 1,
613  mslice_var(short_term_ref_pic_idx));
614  }
615 
617  av_assert0(0);
618  }
619 
620  if (vseq->seq_fields.bits.sps_temporal_mvp_enabled_flag) {
621  u(1, vslice_field(slice_temporal_mvp_enabled_flag));
622  }
623 
624  if (vseq->seq_fields.bits.sample_adaptive_offset_enabled_flag) {
625  u(1, vslice_field(slice_sao_luma_flag));
626  if (!vseq->seq_fields.bits.separate_colour_plane_flag &&
627  vseq->seq_fields.bits.chroma_format_idc != 0) {
628  u(1, vslice_field(slice_sao_chroma_flag));
629  }
630  }
631 
632  if (vslice->slice_type == P_SLICE || vslice->slice_type == B_SLICE) {
633  u(1, vslice_field(num_ref_idx_active_override_flag));
634  if (vslice->slice_fields.bits.num_ref_idx_active_override_flag) {
635  ue(vslice_var(num_ref_idx_l0_active_minus1));
636  if (vslice->slice_type == B_SLICE) {
637  ue(vslice_var(num_ref_idx_l1_active_minus1));
638  }
639  }
640 
642  av_assert0(0);
643  // ref_pic_lists_modification()
644  }
645  if (vslice->slice_type == B_SLICE) {
646  u(1, vslice_field(mvd_l1_zero_flag));
647  }
648  if (mseq->cabac_init_present_flag) {
649  u(1, vslice_field(cabac_init_flag));
650  }
651  if (vslice->slice_fields.bits.slice_temporal_mvp_enabled_flag) {
652  if (vslice->slice_type == B_SLICE)
653  u(1, vslice_field(collocated_from_l0_flag));
654  ue(vpic->collocated_ref_pic_index, collocated_ref_idx);
655  }
656  if ((vpic->pic_fields.bits.weighted_pred_flag &&
657  vslice->slice_type == P_SLICE) ||
658  (vpic->pic_fields.bits.weighted_bipred_flag &&
659  vslice->slice_type == B_SLICE)) {
660  av_assert0(0);
661  // pred_weight_table()
662  }
663  ue(5 - vslice->max_num_merge_cand, five_minus_max_num_merge_cand);
664  }
665 
666  se(vslice_var(slice_qp_delta));
668  se(vslice_var(slice_cb_qp_offset));
669  se(vslice_var(slice_cr_qp_offset));
670  }
672  u(1, 0, cu_chroma_qp_offset_enabled_flag);
673  }
675  u(1, mslice_var(deblocking_filter_override_flag));
676  }
677  if (mslice->deblocking_filter_override_flag) {
678  u(1, vslice_field(slice_deblocking_filter_disabled_flag));
679  if (!vslice->slice_fields.bits.slice_deblocking_filter_disabled_flag) {
680  se(vslice_var(slice_beta_offset_div2));
681  se(vslice_var(slice_tc_offset_div2));
682  }
683  }
684  if (vpic->pic_fields.bits.pps_loop_filter_across_slices_enabled_flag &&
685  (vslice->slice_fields.bits.slice_sao_luma_flag ||
686  vslice->slice_fields.bits.slice_sao_chroma_flag ||
687  vslice->slice_fields.bits.slice_deblocking_filter_disabled_flag)) {
688  u(1, vslice_field(slice_loop_filter_across_slices_enabled_flag));
689  }
690  }
691 
692  if (vpic->pic_fields.bits.tiles_enabled_flag ||
693  vpic->pic_fields.bits.entropy_coding_sync_enabled_flag) {
694  // num_entry_point_offsets
695  }
696 
697  if (0) {
698  // slice_segment_header_extension_length
699  }
700  }
701 
702  u(1, 1, alignment_bit_equal_to_one);
703  while (put_bits_count(pbc) & 7)
704  u(1, 0, alignment_bit_equal_to_zero);
705 }
706 
708  char *data, size_t *data_len)
709 {
710  VAAPIEncodeContext *ctx = avctx->priv_data;
711  PutBitContext pbc;
712  char tmp[256];
713  int err;
714  size_t nal_len, bit_len, bit_pos, next_len;
715 
716  bit_len = *data_len;
717  bit_pos = 0;
718 
719  init_put_bits(&pbc, tmp, sizeof(tmp));
720  vaapi_encode_h265_write_vps(&pbc, ctx);
721  nal_len = put_bits_count(&pbc);
722  flush_put_bits(&pbc);
723 
724  next_len = bit_len - bit_pos;
725  err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
726  &next_len,
727  tmp, nal_len);
728  if (err < 0)
729  return err;
730  bit_pos += next_len;
731 
732  init_put_bits(&pbc, tmp, sizeof(tmp));
733  vaapi_encode_h265_write_sps(&pbc, ctx);
734  nal_len = put_bits_count(&pbc);
735  flush_put_bits(&pbc);
736 
737  next_len = bit_len - bit_pos;
738  err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
739  &next_len,
740  tmp, nal_len);
741  if (err < 0)
742  return err;
743  bit_pos += next_len;
744 
745  init_put_bits(&pbc, tmp, sizeof(tmp));
746  vaapi_encode_h265_write_pps(&pbc, ctx);
747  nal_len = put_bits_count(&pbc);
748  flush_put_bits(&pbc);
749 
750  next_len = bit_len - bit_pos;
751  err = ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data + bit_pos / 8,
752  &next_len,
753  tmp, nal_len);
754  if (err < 0)
755  return err;
756  bit_pos += next_len;
757 
758  *data_len = bit_pos;
759  return 0;
760 }
761 
763  VAAPIEncodePicture *pic,
764  VAAPIEncodeSlice *slice,
765  char *data, size_t *data_len)
766 {
767  VAAPIEncodeContext *ctx = avctx->priv_data;
768  PutBitContext pbc;
769  char tmp[256];
770  size_t header_len;
771 
772  init_put_bits(&pbc, tmp, sizeof(tmp));
773  vaapi_encode_h265_write_slice_header2(&pbc, ctx, pic, slice);
774  header_len = put_bits_count(&pbc);
775  flush_put_bits(&pbc);
776 
777  return ff_vaapi_encode_h26x_nal_unit_to_byte_stream(data, data_len,
778  tmp, header_len);
779 }
780 
782 {
783  VAAPIEncodeContext *ctx = avctx->priv_data;
784  VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
785  VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
786  VAAPIEncodeH265Context *priv = ctx->priv_data;
788  int i;
789 
790  {
791  // general_profile_space == 0.
792  vseq->general_profile_idc = 1; // Main profile (ctx->codec_profile?)
793  vseq->general_tier_flag = 0;
794 
795  vseq->general_level_idc = avctx->level * 3;
796 
797  vseq->intra_period = 0;
798  vseq->intra_idr_period = 0;
799  vseq->ip_period = 0;
800 
801  vseq->pic_width_in_luma_samples = ctx->aligned_width;
802  vseq->pic_height_in_luma_samples = ctx->aligned_height;
803 
804  vseq->seq_fields.bits.chroma_format_idc = 1; // 4:2:0.
805  vseq->seq_fields.bits.separate_colour_plane_flag = 0;
806  vseq->seq_fields.bits.bit_depth_luma_minus8 = 0; // 8-bit luma.
807  vseq->seq_fields.bits.bit_depth_chroma_minus8 = 0; // 8-bit chroma.
808  // Other misc flags all zero.
809 
810  // These have to come from the capabilities of the encoder. We have
811  // no way to query it, so just hardcode ones which worked for me...
812  // CTB size from 8x8 to 32x32.
813  vseq->log2_min_luma_coding_block_size_minus3 = 0;
814  vseq->log2_diff_max_min_luma_coding_block_size = 2;
815  // Transform size from 4x4 to 32x32.
816  vseq->log2_min_transform_block_size_minus2 = 0;
817  vseq->log2_diff_max_min_transform_block_size = 3;
818  // Full transform hierarchy allowed (2-5).
819  vseq->max_transform_hierarchy_depth_inter = 3;
820  vseq->max_transform_hierarchy_depth_intra = 3;
821 
822  vseq->vui_parameters_present_flag = 0;
823 
824  vseq->bits_per_second = avctx->bit_rate;
825  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
826  vseq->vui_num_units_in_tick = avctx->framerate.num;
827  vseq->vui_time_scale = avctx->framerate.den;
828  } else {
829  vseq->vui_num_units_in_tick = avctx->time_base.num;
830  vseq->vui_time_scale = avctx->time_base.den;
831  }
832 
833  vseq->intra_period = ctx->p_per_i * (ctx->b_per_p + 1);
834  vseq->intra_idr_period = vseq->intra_period;
835  vseq->ip_period = ctx->b_per_p + 1;
836  }
837 
838  {
839  vpic->decoded_curr_pic.picture_id = VA_INVALID_ID;
840  vpic->decoded_curr_pic.flags = VA_PICTURE_HEVC_INVALID;
841 
842  for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
843  vpic->reference_frames[i].picture_id = VA_INVALID_ID;
844  vpic->reference_frames[i].flags = VA_PICTURE_HEVC_INVALID;
845  }
846 
847  vpic->collocated_ref_pic_index = 0xff;
848 
849  vpic->last_picture = 0;
850 
851  vpic->pic_init_qp = priv->fixed_qp_idr;
852 
853  vpic->diff_cu_qp_delta_depth = 0;
854  vpic->pps_cb_qp_offset = 0;
855  vpic->pps_cr_qp_offset = 0;
856 
857  // tiles_enabled_flag == 0, so ignore num_tile_(rows|columns)_minus1.
858 
859  vpic->log2_parallel_merge_level_minus2 = 0;
860 
861  // No limit on size.
862  vpic->ctu_max_bitsize_allowed = 0;
863 
864  vpic->num_ref_idx_l0_default_active_minus1 = 0;
865  vpic->num_ref_idx_l1_default_active_minus1 = 0;
866 
867  vpic->slice_pic_parameter_set_id = 0;
868 
869  vpic->pic_fields.bits.screen_content_flag = 0;
870  vpic->pic_fields.bits.enable_gpu_weighted_prediction = 0;
871 
872  // Per-CU QP changes are required for non-constant-QP modes.
873  vpic->pic_fields.bits.cu_qp_delta_enabled_flag =
874  ctx->va_rc_mode != VA_RC_CQP;
875  }
876 
877  {
878  mseq->video_parameter_set_id = 5;
879  mseq->seq_parameter_set_id = 5;
880 
881  mseq->vps_max_layers_minus1 = 0;
882  mseq->vps_max_sub_layers_minus1 = 0;
884  mseq->sps_max_sub_layers_minus1 = 0;
886 
887  for (i = 0; i < 32; i++) {
889  (i == vseq->general_profile_idc);
890  }
891 
896  mseq->general_inbld_flag = 0;
897 
901  mseq->vps_max_num_reorder_pics[0] = ctx->b_per_p;
902  mseq->vps_max_latency_increase_plus1[0] = 0;
905  mseq->sps_max_num_reorder_pics[0] = ctx->b_per_p;
906  mseq->sps_max_latency_increase_plus1[0] = 0;
907 
909  mseq->vps_num_units_in_tick = avctx->time_base.num;
910  mseq->vps_time_scale = avctx->time_base.den;
913 
914  if (ctx->input_width != ctx->aligned_width ||
915  ctx->input_height != ctx->aligned_height) {
916  mseq->conformance_window_flag = 1;
917  mseq->conf_win_left_offset = 0;
918  mseq->conf_win_right_offset =
919  (ctx->aligned_width - ctx->input_width) / 2;
920  mseq->conf_win_top_offset = 0;
921  mseq->conf_win_bottom_offset =
922  (ctx->aligned_height - ctx->input_height) / 2;
923  } else {
924  mseq->conformance_window_flag = 0;
925  }
926 
927  mseq->num_short_term_ref_pic_sets = 0;
928  // STRPSs should ideally be here rather than repeated in each slice.
929 
930  mseq->vui_parameters_present_flag = 1;
931  if (avctx->sample_aspect_ratio.num != 0) {
933  if (avctx->sample_aspect_ratio.num ==
934  avctx->sample_aspect_ratio.den) {
935  mseq->aspect_ratio_idc = 1;
936  } else {
937  mseq->aspect_ratio_idc = 255; // Extended SAR.
938  mseq->sar_width = avctx->sample_aspect_ratio.num;
939  mseq->sar_height = avctx->sample_aspect_ratio.den;
940  }
941  }
942  if (1) {
943  // Should this be conditional on some of these being set?
945  mseq->video_format = 5; // Unspecified.
946  mseq->video_full_range_flag = 0;
948  mseq->colour_primaries = avctx->color_primaries;
949  mseq->transfer_characteristics = avctx->color_trc;
950  mseq->matrix_coeffs = avctx->colorspace;
951  }
952  }
953 
954  return 0;
955 }
956 
958  VAAPIEncodePicture *pic)
959 {
960  VAAPIEncodeContext *ctx = avctx->priv_data;
961  VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
962  VAAPIEncodeH265Context *priv = ctx->priv_data;
963  int i;
964 
965  if (pic->type == PICTURE_TYPE_IDR) {
966  av_assert0(pic->display_order == pic->encode_order);
967  priv->last_idr_frame = pic->display_order;
968  } else {
969  av_assert0(pic->encode_order > priv->last_idr_frame);
970  // Display order need not be if we have RA[SD]L pictures, though.
971  }
972 
973  vpic->decoded_curr_pic.picture_id = pic->recon_surface;
974  vpic->decoded_curr_pic.pic_order_cnt =
975  pic->display_order - priv->last_idr_frame;
976  vpic->decoded_curr_pic.flags = 0;
977 
978  for (i = 0; i < pic->nb_refs; i++) {
979  VAAPIEncodePicture *ref = pic->refs[i];
980  av_assert0(ref);
981  vpic->reference_frames[i].picture_id = ref->recon_surface;
982  vpic->reference_frames[i].pic_order_cnt =
983  ref->display_order - priv->last_idr_frame;
984  vpic->reference_frames[i].flags =
985  (ref->display_order < pic->display_order ?
986  VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
987  (ref->display_order > pic->display_order ?
988  VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0);
989  }
990  for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
991  vpic->reference_frames[i].picture_id = VA_INVALID_ID;
992  vpic->reference_frames[i].flags = VA_PICTURE_HEVC_INVALID;
993  }
994 
995  vpic->coded_buf = pic->output_buffer;
996 
997  switch (pic->type) {
998  case PICTURE_TYPE_IDR:
999  vpic->nal_unit_type = NAL_IDR_W_RADL;
1000  vpic->pic_fields.bits.idr_pic_flag = 1;
1001  vpic->pic_fields.bits.coding_type = 1;
1002  vpic->pic_fields.bits.reference_pic_flag = 1;
1003  break;
1004  case PICTURE_TYPE_I:
1005  vpic->nal_unit_type = NAL_TRAIL_R;
1006  vpic->pic_fields.bits.idr_pic_flag = 0;
1007  vpic->pic_fields.bits.coding_type = 1;
1008  vpic->pic_fields.bits.reference_pic_flag = 1;
1009  break;
1010  case PICTURE_TYPE_P:
1011  vpic->nal_unit_type = NAL_TRAIL_R;
1012  vpic->pic_fields.bits.idr_pic_flag = 0;
1013  vpic->pic_fields.bits.coding_type = 2;
1014  vpic->pic_fields.bits.reference_pic_flag = 1;
1015  break;
1016  case PICTURE_TYPE_B:
1017  vpic->nal_unit_type = NAL_TRAIL_R;
1018  vpic->pic_fields.bits.idr_pic_flag = 0;
1019  vpic->pic_fields.bits.coding_type = 3;
1020  vpic->pic_fields.bits.reference_pic_flag = 0;
1021  break;
1022  default:
1023  av_assert0(0 && "invalid picture type");
1024  }
1025 
1026  pic->nb_slices = 1;
1027 
1028  return 0;
1029 }
1030 
1032  VAAPIEncodePicture *pic,
1033  VAAPIEncodeSlice *slice)
1034 {
1035  VAAPIEncodeContext *ctx = avctx->priv_data;
1036  VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
1037  VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
1038  VAAPIEncodeH265Context *priv = ctx->priv_data;
1039  VAAPIEncodeH265Slice *pslice;
1041  int i;
1042 
1043  slice->priv_data = av_mallocz(sizeof(*pslice));
1044  if (!slice->priv_data)
1045  return AVERROR(ENOMEM);
1046  pslice = slice->priv_data;
1047  mslice = &pslice->misc_slice_params;
1048 
1049  // Currently we only support one slice per frame.
1050  vslice->slice_segment_address = 0;
1051  vslice->num_ctu_in_slice = priv->ctu_width * priv->ctu_height;
1052 
1053  switch (pic->type) {
1054  case PICTURE_TYPE_IDR:
1055  case PICTURE_TYPE_I:
1056  vslice->slice_type = I_SLICE;
1057  break;
1058  case PICTURE_TYPE_P:
1059  vslice->slice_type = P_SLICE;
1060  break;
1061  case PICTURE_TYPE_B:
1062  vslice->slice_type = B_SLICE;
1063  break;
1064  default:
1065  av_assert0(0 && "invalid picture type");
1066  }
1067 
1068  vslice->slice_pic_parameter_set_id = vpic->slice_pic_parameter_set_id;
1069 
1070  pslice->pic_order_cnt = pic->display_order - priv->last_idr_frame;
1071 
1072  for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
1073  vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
1074  vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
1075  vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
1076  vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
1077  }
1078 
1079  av_assert0(pic->nb_refs <= 2);
1080  if (pic->nb_refs >= 1) {
1081  // Backward reference for P- or B-frame.
1082  av_assert0(pic->type == PICTURE_TYPE_P ||
1083  pic->type == PICTURE_TYPE_B);
1084 
1085  vslice->num_ref_idx_l0_active_minus1 = 0;
1086  vslice->ref_pic_list0[0] = vpic->reference_frames[0];
1087  }
1088  if (pic->nb_refs >= 2) {
1089  // Forward reference for B-frame.
1090  av_assert0(pic->type == PICTURE_TYPE_B);
1091 
1092  vslice->num_ref_idx_l1_active_minus1 = 0;
1093  vslice->ref_pic_list1[0] = vpic->reference_frames[1];
1094  }
1095 
1096  vslice->max_num_merge_cand = 5;
1097 
1098  if (pic->type == PICTURE_TYPE_B)
1099  vslice->slice_qp_delta = priv->fixed_qp_b - vpic->pic_init_qp;
1100  else if (pic->type == PICTURE_TYPE_P)
1101  vslice->slice_qp_delta = priv->fixed_qp_p - vpic->pic_init_qp;
1102  else
1103  vslice->slice_qp_delta = priv->fixed_qp_idr - vpic->pic_init_qp;
1104 
1105  vslice->slice_fields.bits.last_slice_of_pic_flag = 1;
1106 
1107  mslice->first_slice_segment_in_pic_flag = 1;
1108 
1109  if (pic->type == PICTURE_TYPE_IDR) {
1110  // No reference pictures.
1111  } else if (0) {
1112  mslice->short_term_ref_pic_set_sps_flag = 1;
1113  mslice->short_term_ref_pic_idx = 0;
1114  } else {
1115  VAAPIEncodePicture *st;
1116  int used;
1117 
1118  mslice->short_term_ref_pic_set_sps_flag = 0;
1120 
1121  for (st = ctx->pic_start; st; st = st->next) {
1122  if (st->encode_order >= pic->encode_order) {
1123  // Not yet in DPB.
1124  continue;
1125  }
1126  used = 0;
1127  for (i = 0; i < pic->nb_refs; i++) {
1128  if (pic->refs[i] == st)
1129  used = 1;
1130  }
1131  if (!used) {
1132  // Currently true, but need not be.
1133  continue;
1134  }
1135  // This only works for one instance of each (delta_poc_sN_minus1
1136  // is relative to the previous frame in the list, not relative to
1137  // the current frame directly).
1138  if (st->display_order < pic->display_order) {
1139  i = mslice->st_ref_pic_set.num_negative_pics;
1140  mslice->st_ref_pic_set.delta_poc_s0_minus1[i] =
1141  pic->display_order - st->display_order - 1;
1142  mslice->st_ref_pic_set.used_by_curr_pic_s0_flag[i] = used;
1144  } else {
1145  i = mslice->st_ref_pic_set.num_positive_pics;
1146  mslice->st_ref_pic_set.delta_poc_s1_minus1[i] =
1147  st->display_order - pic->display_order - 1;
1148  mslice->st_ref_pic_set.used_by_curr_pic_s1_flag[i] = used;
1150  }
1151  }
1152  }
1153 
1154  return 0;
1155 }
1156 
1158 {
1159  VAAPIEncodeContext *ctx = avctx->priv_data;
1160  VAAPIEncodeH265Context *priv = ctx->priv_data;
1161  int hrd_buffer_size;
1162  int hrd_initial_buffer_fullness;
1163 
1164  if (avctx->bit_rate > INT32_MAX) {
1165  av_log(avctx, AV_LOG_ERROR, "Target bitrate of 2^31 bps or "
1166  "higher is not supported.\n");
1167  return AVERROR(EINVAL);
1168  }
1169 
1170  if (avctx->rc_buffer_size)
1171  hrd_buffer_size = avctx->rc_buffer_size;
1172  else
1173  hrd_buffer_size = avctx->bit_rate;
1174  if (avctx->rc_initial_buffer_occupancy)
1175  hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy;
1176  else
1177  hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1178 
1179  priv->rc_params.misc.type = VAEncMiscParameterTypeRateControl;
1180  priv->rc_params.rc = (VAEncMiscParameterRateControl) {
1181  .bits_per_second = avctx->bit_rate,
1182  .target_percentage = 66,
1183  .window_size = 1000,
1184  .initial_qp = (avctx->qmax >= 0 ? avctx->qmax : 40),
1185  .min_qp = (avctx->qmin >= 0 ? avctx->qmin : 20),
1186  .basic_unit_size = 0,
1187  };
1188  ctx->global_params[ctx->nb_global_params] =
1189  &priv->rc_params.misc;
1190  ctx->global_params_size[ctx->nb_global_params++] =
1191  sizeof(priv->rc_params);
1192 
1193  priv->hrd_params.misc.type = VAEncMiscParameterTypeHRD;
1194  priv->hrd_params.hrd = (VAEncMiscParameterHRD) {
1195  .initial_buffer_fullness = hrd_initial_buffer_fullness,
1196  .buffer_size = hrd_buffer_size,
1197  };
1198  ctx->global_params[ctx->nb_global_params] =
1199  &priv->hrd_params.misc;
1200  ctx->global_params_size[ctx->nb_global_params++] =
1201  sizeof(priv->hrd_params);
1202 
1203  // These still need to be set for pic_init_qp/slice_qp_delta.
1204  priv->fixed_qp_idr = 30;
1205  priv->fixed_qp_p = 30;
1206  priv->fixed_qp_b = 30;
1207 
1208  av_log(avctx, AV_LOG_DEBUG, "Using constant-bitrate = %"PRId64" bps.\n",
1209  avctx->bit_rate);
1210  return 0;
1211 }
1212 
1214 {
1215  VAAPIEncodeContext *ctx = avctx->priv_data;
1216  VAAPIEncodeH265Context *priv = ctx->priv_data;
1218 
1219  priv->fixed_qp_p = opt->qp;
1220  if (avctx->i_quant_factor > 0.0)
1221  priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
1222  avctx->i_quant_offset) + 0.5);
1223  else
1224  priv->fixed_qp_idr = priv->fixed_qp_p;
1225  if (avctx->b_quant_factor > 0.0)
1226  priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
1227  avctx->b_quant_offset) + 0.5);
1228  else
1229  priv->fixed_qp_b = priv->fixed_qp_p;
1230 
1231  av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1232  "%d / %d / %d for IDR- / P- / B-frames.\n",
1233  priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1234  return 0;
1235 }
1236 
1238 {
1239  static const VAConfigAttrib default_config_attributes[] = {
1240  { .type = VAConfigAttribRTFormat,
1241  .value = VA_RT_FORMAT_YUV420 },
1242  { .type = VAConfigAttribEncPackedHeaders,
1243  .value = (VA_ENC_PACKED_HEADER_SEQUENCE |
1244  VA_ENC_PACKED_HEADER_SLICE) },
1245  };
1246 
1247  VAAPIEncodeContext *ctx = avctx->priv_data;
1248  VAAPIEncodeH265Context *priv = ctx->priv_data;
1249  int i, err;
1250 
1251  switch (avctx->profile) {
1252  case FF_PROFILE_HEVC_MAIN:
1253  case FF_PROFILE_UNKNOWN:
1254  ctx->va_profile = VAProfileHEVCMain;
1255  break;
1257  av_log(avctx, AV_LOG_ERROR, "H.265 main 10-bit profile "
1258  "is not supported.\n");
1259  return AVERROR_PATCHWELCOME;
1260  default:
1261  av_log(avctx, AV_LOG_ERROR, "Unknown H.265 profile %d.\n",
1262  avctx->profile);
1263  return AVERROR(EINVAL);
1264  }
1265  ctx->va_entrypoint = VAEntrypointEncSlice;
1266 
1267  ctx->input_width = avctx->width;
1268  ctx->input_height = avctx->height;
1269  ctx->aligned_width = FFALIGN(ctx->input_width, 16);
1270  ctx->aligned_height = FFALIGN(ctx->input_height, 16);
1271  priv->ctu_width = FFALIGN(ctx->aligned_width, 32) / 32;
1272  priv->ctu_height = FFALIGN(ctx->aligned_height, 32) / 32;
1273 
1274  av_log(avctx, AV_LOG_VERBOSE, "Input %ux%u -> Aligned %ux%u -> CTU %ux%u.\n",
1275  ctx->input_width, ctx->input_height, ctx->aligned_width,
1276  ctx->aligned_height, priv->ctu_width, priv->ctu_height);
1277 
1278  for (i = 0; i < FF_ARRAY_ELEMS(default_config_attributes); i++) {
1280  default_config_attributes[i];
1281  }
1282 
1283  if (avctx->bit_rate > 0) {
1284  ctx->va_rc_mode = VA_RC_CBR;
1286  } else {
1287  ctx->va_rc_mode = VA_RC_CQP;
1288  err = vaapi_encode_h265_init_fixed_qp(avctx);
1289  }
1290  if (err < 0)
1291  return err;
1292 
1293  ctx->config_attributes[ctx->nb_config_attributes++] = (VAConfigAttrib) {
1294  .type = VAConfigAttribRateControl,
1295  .value = ctx->va_rc_mode,
1296  };
1297 
1298  ctx->nb_recon_frames = 20;
1299 
1300  return 0;
1301 }
1302 
1305 
1307 
1308  .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
1309  .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
1310 
1311  .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
1312  .init_picture_params = &vaapi_encode_h265_init_picture_params,
1313 
1314  .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
1315  .init_slice_params = &vaapi_encode_h265_init_slice_params,
1316 
1317  .sequence_header_type = VAEncPackedHeaderSequence,
1318  .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
1319 
1320  .slice_header_type = VAEncPackedHeaderHEVC_Slice,
1321  .write_slice_header = &vaapi_encode_h265_write_slice_header,
1322 };
1323 
1325 {
1326  return ff_vaapi_encode_init(avctx, &vaapi_encode_type_h265);
1327 }
1328 
1329 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
1330  offsetof(VAAPIEncodeH265Options, x))
1331 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1333  { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1334  OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, 52, FLAGS },
1335  { NULL },
1336 };
1337 
1339  { "profile", "1" },
1340  { "level", "51" },
1341  { "b", "0" },
1342  { "bf", "2" },
1343  { "g", "120" },
1344  { "i_qfactor", "1" },
1345  { "i_qoffset", "0" },
1346  { "b_qfactor", "6/5" },
1347  { "b_qoffset", "0" },
1348  { NULL },
1349 };
1350 
1352  .class_name = "h265_vaapi",
1353  .item_name = av_default_item_name,
1354  .option = vaapi_encode_h265_options,
1355  .version = LIBAVUTIL_VERSION_INT,
1356 };
1357 
1359  .name = "hevc_vaapi",
1360  .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
1361  .type = AVMEDIA_TYPE_VIDEO,
1362  .id = AV_CODEC_ID_HEVC,
1363  .priv_data_size = (sizeof(VAAPIEncodeContext) +
1364  sizeof(VAAPIEncodeH265Options)),
1366  .encode2 = &ff_vaapi_encode2,
1367  .close = &ff_vaapi_encode_close,
1368  .priv_class = &vaapi_encode_h265_class,
1369  .capabilities = AV_CODEC_CAP_DELAY,
1370  .defaults = vaapi_encode_h265_defaults,
1371  .pix_fmts = (const enum AVPixelFormat[]) {
1374  },
1375 };
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3375
VAProfile va_profile
Definition: vaapi_encode.h:108
unsigned int delta_poc_s0_minus1[MAX_DPB_PICS]
#define MAX_ST_REF_PIC_SETS
static void vaapi_encode_h265_write_vps(PutBitContext *pbc, VAAPIEncodeContext *ctx)
VAEntrypoint va_entrypoint
Definition: vaapi_encode.h:109
#define mstrps_var(name)
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define mseq_var(name)
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1741
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:2715
static void vaapi_encode_h265_write_vui_parameters(PutBitContext *pbc, VAAPIEncodeContext *ctx)
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
int num
Numerator.
Definition: rational.h:59
static const AVClass vaapi_encode_h265_class
int av_log2(unsigned v)
Definition: intmath.c:26
size_t priv_data_size
Definition: vaapi_encode.h:184
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:2087
VAAPIEncodeH265MiscSliceParams misc_slice_params
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
void * codec_sequence_params
Definition: vaapi_encode.h:141
static const AVCodecDefault vaapi_encode_h265_defaults[]
int profile
profile
Definition: avcodec.h:3181
AVCodec.
Definition: avcodec.h:3600
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:2020
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1813
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:984
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define av_cold
Definition: attributes.h:82
AVOptions.
static VAAPIEncodeType vaapi_encode_type_h265
unsigned int delta_poc_s1_minus1[MAX_DPB_PICS]
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition: avcodec.h:1971
#define FF_PROFILE_HEVC_MAIN
Definition: avcodec.h:3269
struct VAAPIEncodeH265Context::@132 rc_params
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx, const VAAPIEncodeType *type)
#define se(...)
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
VASurfaceID recon_surface
Definition: vaapi_encode.h:82
unsigned int vps_max_dec_pic_buffering_minus1[MAX_LAYERS]
#define FF_PROFILE_HEVC_MAIN_10
Definition: avcodec.h:3270
#define mslice_var(name)
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
char layer_id_included_flag[MAX_LAYERS][64]
VAEncMiscParameterHRD hrd
#define ue(...)
Definition: hevc.h:113
VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES]
Definition: vaapi_encode.h:133
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void vaapi_encode_h265_write_slice_header2(PutBitContext *pbc, VAAPIEncodeContext *ctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice)
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
VAEncMiscParameterRateControl rc
int qmax
maximum quantizer
Definition: avcodec.h:2626
int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *input_image, int *got_packet)
Definition: vaapi_encode.c:809
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define MAX_DPB_PICS
VAEncMiscParameterBuffer * global_params[MAX_GLOBAL_PARAMS]
Definition: vaapi_encode.h:136
static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
simple assert() macros that are a bit more flexible than ISO C assert().
VAAPIEncodeH265MiscSequenceParams misc_sequence_params
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
Definition: avcodec.h:2013
static const AVOption vaapi_encode_h265_options[]
#define vslice_field(name)
void * codec_picture_params
Definition: vaapi_encode.h:90
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2653
static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice)
common internal API header
unsigned int vps_max_num_reorder_pics[MAX_LAYERS]
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
int width
picture width / height.
Definition: avcodec.h:1863
#define FLAGS
char used_by_curr_pic_s0_flag[MAX_DPB_PICS]
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:3182
AVCodec ff_hevc_vaapi_encoder
static av_cold int vaapi_encode_h265_init_constant_bitrate(AVCodecContext *avctx)
AVFormatContext * ctx
Definition: movenc.c:48
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2392
#define vpic_field(name)
struct VAAPIEncodePicture * next
Definition: vaapi_encode.h:67
Definition: hevc.h:134
int level
level
Definition: avcodec.h:3279
void * codec_picture_params
Definition: vaapi_encode.h:145
static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
static void vaapi_encode_h265_write_pps(PutBitContext *pbc, VAAPIEncodeContext *ctx)
static void vaapi_encode_h265_write_sps(PutBitContext *pbc, VAAPIEncodeContext *ctx)
static av_cold int vaapi_encode_h265_init_internal(AVCodecContext *avctx)
Definition: hevc.h:112
#define FF_ARRAY_ELEMS(a)
unsigned int sps_max_dec_pic_buffering_minus1[MAX_LAYERS]
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
Definition: hevc.h:133
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
Definition: vaapi_encode.h:93
#define OFFSET(x)
static av_cold int vaapi_encode_h265_init_fixed_qp(AVCodecContext *avctx)
static void vaapi_encode_h265_write_nal_unit_header(PutBitContext *pbc, int nal_unit_type)
static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice, char *data, size_t *data_len)
Libavcodec external API header.
VAAPIEncodePicture * pic_start
Definition: vaapi_encode.h:148
unsigned int num_positive_pics
main external API structure.
Definition: avcodec.h:1676
int qmin
minimum quantizer
Definition: avcodec.h:2619
Definition: hevc.h:111
unsigned int vps_max_latency_increase_plus1[MAX_LAYERS]
char used_by_curr_pic_s1_flag[MAX_DPB_PICS]
Describe the class of an AVClass context structure.
Definition: log.h:67
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2406
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2399
VAEncMiscParameterBuffer misc
unsigned int sps_max_num_reorder_pics[MAX_LAYERS]
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1990
unsigned int num_negative_pics
#define u(width,...)
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
#define vseq_field(name)
static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
static void vaapi_encode_h265_write_st_ref_pic_set(PutBitContext *pbc, int st_rps_idx, VAAPIEncodeH265STRPS *mstrps)
size_t global_params_size[MAX_GLOBAL_PARAMS]
Definition: vaapi_encode.h:137
VAAPIEncodeH265STRPS st_ref_pic_set
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int den
Denominator.
Definition: rational.h:60
static void vaapi_encode_h265_write_profile_tier_level(PutBitContext *pbc, VAAPIEncodeContext *ctx)
struct VAAPIEncodeH265Context::@133 hrd_params
void * priv_data
Definition: avcodec.h:1718
#define vpic_var(name)
unsigned int sps_max_latency_increase_plus1[MAX_LAYERS]
pixel format definitions
static const float matrix_coeffs[8]
Definition: atrac3data.h:137
static uint8_t tmp[8]
Definition: des.c:38
void * codec_slice_params
Definition: vaapi_encode.h:63
VAAPIEncodeH265STRPS st_ref_pic_set[MAX_ST_REF_PIC_SETS]
struct VAAPIEncodeH265MiscSequenceParams::@131 lt_ref_pic
Definition: hevc.h:132
static void vaapi_encode_h265_write_rbsp_trailing_bits(PutBitContext *pbc)
static const AVCodecDefault defaults[]
Definition: dcaenc.c:975
#define MAX_LAYERS
VABufferID output_buffer
Definition: vaapi_encode.h:87
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
#define vseq_var(name)
#define vslice_var(name)
int ff_vaapi_encode_h26x_nal_unit_to_byte_stream(uint8_t *dst, size_t *dst_bit_len, uint8_t *src, size_t src_bit_len)
bitstream writer API