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 <string.h>
20 
21 #include <va/va.h>
22 #include <va/va_enc_hevc.h>
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/opt.h"
29 
30 #include "avcodec.h"
31 #include "cbs.h"
32 #include "cbs_h265.h"
33 #include "h265_profile_level.h"
34 #include "hevc.h"
35 #include "hevc_sei.h"
36 #include "internal.h"
37 #include "put_bits.h"
38 #include "vaapi_encode.h"
39 
40 enum {
43 };
44 
45 typedef struct VAAPIEncodeH265Context {
47 
48  // User options.
49  int qp;
50  int aud;
51  int profile;
52  int tier;
53  int level;
54  int sei;
55 
56  // Derived settings.
60 
61  // Stream state.
62  int64_t last_idr_frame;
64 
67  int pic_type;
68 
69  // Writer structures.
76 
79 
85 
86 
88  char *data, size_t *data_len,
90 {
91  VAAPIEncodeH265Context *priv = avctx->priv_data;
92  int err;
93 
94  err = ff_cbs_write_fragment_data(priv->cbc, au);
95  if (err < 0) {
96  av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
97  return err;
98  }
99 
100  if (*data_len < 8 * au->data_size - au->data_bit_padding) {
101  av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
102  "%zu < %zu.\n", *data_len,
103  8 * au->data_size - au->data_bit_padding);
104  return AVERROR(ENOSPC);
105  }
106 
107  memcpy(data, au->data, au->data_size);
108  *data_len = 8 * au->data_size - au->data_bit_padding;
109 
110  return 0;
111 }
112 
115  void *nal_unit)
116 {
117  VAAPIEncodeH265Context *priv = avctx->priv_data;
118  H265RawNALUnitHeader *header = nal_unit;
119  int err;
120 
121  err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
122  header->nal_unit_type, nal_unit, NULL);
123  if (err < 0) {
124  av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
125  "type = %d.\n", header->nal_unit_type);
126  return err;
127  }
128 
129  return 0;
130 }
131 
133  char *data, size_t *data_len)
134 {
135  VAAPIEncodeH265Context *priv = avctx->priv_data;
137  int err;
138 
139  if (priv->aud_needed) {
140  err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
141  if (err < 0)
142  goto fail;
143  priv->aud_needed = 0;
144  }
145 
146  err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_vps);
147  if (err < 0)
148  goto fail;
149 
150  err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_sps);
151  if (err < 0)
152  goto fail;
153 
154  err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_pps);
155  if (err < 0)
156  goto fail;
157 
158  err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
159 fail:
160  ff_cbs_fragment_uninit(priv->cbc, au);
161  return err;
162 }
163 
165  VAAPIEncodePicture *pic,
166  VAAPIEncodeSlice *slice,
167  char *data, size_t *data_len)
168 {
169  VAAPIEncodeH265Context *priv = avctx->priv_data;
171  int err;
172 
173  if (priv->aud_needed) {
174  err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
175  if (err < 0)
176  goto fail;
177  priv->aud_needed = 0;
178  }
179 
180  err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_slice);
181  if (err < 0)
182  goto fail;
183 
184  err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
185 fail:
186  ff_cbs_fragment_uninit(priv->cbc, au);
187  return err;
188 }
189 
191  VAAPIEncodePicture *pic,
192  int index, int *type,
193  char *data, size_t *data_len)
194 {
195  VAAPIEncodeH265Context *priv = avctx->priv_data;
197  int err, i;
198 
199  if (priv->sei_needed) {
200  H265RawSEI *sei = &priv->raw_sei;
201 
202  if (priv->aud_needed) {
203  err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
204  if (err < 0)
205  goto fail;
206  priv->aud_needed = 0;
207  }
208 
209  *sei = (H265RawSEI) {
210  .nal_unit_header = {
212  .nuh_layer_id = 0,
213  .nuh_temporal_id_plus1 = 1,
214  },
215  };
216 
217  i = 0;
218 
219  if (priv->sei_needed & SEI_MASTERING_DISPLAY) {
222  ++i;
223  }
224 
225  if (priv->sei_needed & SEI_CONTENT_LIGHT_LEVEL) {
228  ++i;
229  }
230 
231  sei->payload_count = i;
232  av_assert0(sei->payload_count > 0);
233 
234  err = vaapi_encode_h265_add_nal(avctx, au, sei);
235  if (err < 0)
236  goto fail;
237  priv->sei_needed = 0;
238 
239  err = vaapi_encode_h265_write_access_unit(avctx, data, data_len, au);
240  if (err < 0)
241  goto fail;
242 
243  ff_cbs_fragment_uninit(priv->cbc, au);
244 
245  *type = VAEncPackedHeaderRawData;
246  return 0;
247  } else {
248  return AVERROR_EOF;
249  }
250 
251 fail:
252  ff_cbs_fragment_uninit(priv->cbc, au);
253  return err;
254 }
255 
257 {
258  VAAPIEncodeContext *ctx = avctx->priv_data;
259  VAAPIEncodeH265Context *priv = avctx->priv_data;
260  H265RawVPS *vps = &priv->raw_vps;
261  H265RawSPS *sps = &priv->raw_sps;
262  H265RawPPS *pps = &priv->raw_pps;
264  H265RawVUI *vui = &sps->vui;
265  VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
266  VAEncPictureParameterBufferHEVC *vpic = ctx->codec_picture_params;
267  const AVPixFmtDescriptor *desc;
268  int chroma_format, bit_depth;
269  int i;
270 
271  memset(&priv->current_access_unit, 0,
272  sizeof(priv->current_access_unit));
273 
274  memset(vps, 0, sizeof(*vps));
275  memset(sps, 0, sizeof(*sps));
276  memset(pps, 0, sizeof(*pps));
277 
278 
280  av_assert0(desc);
281  if (desc->nb_components == 1) {
282  chroma_format = 0;
283  } else {
284  if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1) {
285  chroma_format = 1;
286  } else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0) {
287  chroma_format = 2;
288  } else if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
289  chroma_format = 3;
290  } else {
291  av_log(avctx, AV_LOG_ERROR, "Chroma format of input pixel format "
292  "%s is not supported.\n", desc->name);
293  return AVERROR(EINVAL);
294  }
295  }
296  bit_depth = desc->comp[0].depth;
297 
298 
299  // VPS
300 
303  .nuh_layer_id = 0,
304  .nuh_temporal_id_plus1 = 1,
305  };
306 
308 
311  vps->vps_max_layers_minus1 = 0;
312  vps->vps_max_sub_layers_minus1 = 0;
314 
315  ptl->general_profile_space = 0;
316  ptl->general_profile_idc = avctx->profile;
317  ptl->general_tier_flag = priv->tier;
318 
319  if (chroma_format == 1) {
320  ptl->general_profile_compatibility_flag[1] = bit_depth == 8;
321  ptl->general_profile_compatibility_flag[2] = bit_depth <= 10;
322  }
324 
329 
330  ptl->general_max_12bit_constraint_flag = bit_depth <= 12;
331  ptl->general_max_10bit_constraint_flag = bit_depth <= 10;
332  ptl->general_max_8bit_constraint_flag = bit_depth == 8;
333 
334  ptl->general_max_422chroma_constraint_flag = chroma_format <= 2;
335  ptl->general_max_420chroma_constraint_flag = chroma_format <= 1;
336  ptl->general_max_monochrome_constraint_flag = chroma_format == 0;
337 
338  ptl->general_intra_constraint_flag = ctx->gop_size == 1;
339 
341 
342  if (avctx->level != FF_LEVEL_UNKNOWN) {
343  ptl->general_level_idc = avctx->level;
344  } else {
345  const H265LevelDescriptor *level;
346 
347  level = ff_h265_guess_level(ptl, avctx->bit_rate,
348  ctx->surface_width, ctx->surface_height,
349  ctx->nb_slices, 1, 1,
350  (ctx->b_per_p > 0) + 1);
351  if (level) {
352  av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
353  ptl->general_level_idc = level->level_idc;
354  } else {
355  av_log(avctx, AV_LOG_VERBOSE, "Stream will not conform to "
356  "any normal level; using level 8.5.\n");
357  ptl->general_level_idc = 255;
358  // The tier flag must be set in level 8.5.
359  ptl->general_tier_flag = 1;
360  }
361  }
362 
364  vps->vps_max_dec_pic_buffering_minus1[0] = (ctx->b_per_p > 0) + 1;
365  vps->vps_max_num_reorder_pics[0] = (ctx->b_per_p > 0);
366  vps->vps_max_latency_increase_plus1[0] = 0;
367 
368  vps->vps_max_layer_id = 0;
369  vps->vps_num_layer_sets_minus1 = 0;
370  vps->layer_id_included_flag[0][0] = 1;
371 
373  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
374  vps->vps_num_units_in_tick = avctx->framerate.den;
375  vps->vps_time_scale = avctx->framerate.num;
378  } else {
379  vps->vps_num_units_in_tick = avctx->time_base.num;
380  vps->vps_time_scale = avctx->time_base.den;
382  }
383  vps->vps_num_hrd_parameters = 0;
384 
385 
386  // SPS
387 
390  .nuh_layer_id = 0,
391  .nuh_temporal_id_plus1 = 1,
392  };
393 
395 
398 
400 
401  sps->sps_seq_parameter_set_id = 0;
402 
403  sps->chroma_format_idc = chroma_format;
405 
408 
409  if (avctx->width != ctx->surface_width ||
410  avctx->height != ctx->surface_height) {
411  sps->conformance_window_flag = 1;
412  sps->conf_win_left_offset = 0;
413  sps->conf_win_right_offset =
414  (ctx->surface_width - avctx->width) / 2;
415  sps->conf_win_top_offset = 0;
417  (ctx->surface_height - avctx->height) / 2;
418  } else {
419  sps->conformance_window_flag = 0;
420  }
421 
422  sps->bit_depth_luma_minus8 = bit_depth - 8;
423  sps->bit_depth_chroma_minus8 = bit_depth - 8;
424 
426 
429  for (i = 0; i <= sps->sps_max_sub_layers_minus1; i++) {
432  sps->sps_max_num_reorder_pics[i] =
433  vps->vps_max_num_reorder_pics[i];
436  }
437 
438  // These have to come from the capabilities of the encoder. We have no
439  // way to query them, so just hardcode parameters which work on the Intel
440  // driver.
441  // CTB size from 8x8 to 32x32.
444  // Transform size from 4x4 to 32x32.
447  // Full transform hierarchy allowed (2-5).
450  // AMP works.
451  sps->amp_enabled_flag = 1;
452  // SAO and temporal MVP do not work.
455 
456  sps->pcm_enabled_flag = 0;
457 
458  // STRPSs should ideally be here rather than defined individually in
459  // each slice, but the structure isn't completely fixed so for now
460  // don't bother.
463 
465 
466  if (avctx->sample_aspect_ratio.num != 0 &&
467  avctx->sample_aspect_ratio.den != 0) {
468  static const AVRational sar_idc[] = {
469  { 0, 0 },
470  { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
471  { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
472  { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
473  { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
474  };
475  int num, den, i;
476  av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
477  avctx->sample_aspect_ratio.den, 65535);
478  for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
479  if (num == sar_idc[i].num &&
480  den == sar_idc[i].den) {
481  vui->aspect_ratio_idc = i;
482  break;
483  }
484  }
485  if (i >= FF_ARRAY_ELEMS(sar_idc)) {
486  vui->aspect_ratio_idc = 255;
487  vui->sar_width = num;
488  vui->sar_height = den;
489  }
491  }
492 
493  if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
495  avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
496  avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
498  vui->video_format = 5; // Unspecified.
499  vui->video_full_range_flag =
500  avctx->color_range == AVCOL_RANGE_JPEG;
501 
502  if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
503  avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
504  avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
506  vui->colour_primaries = avctx->color_primaries;
507  vui->transfer_characteristics = avctx->color_trc;
508  vui->matrix_coefficients = avctx->colorspace;
509  }
510  } else {
511  vui->video_format = 5;
512  vui->video_full_range_flag = 0;
513  vui->colour_primaries = avctx->color_primaries;
514  vui->transfer_characteristics = avctx->color_trc;
515  vui->matrix_coefficients = avctx->colorspace;
516  }
517 
522  avctx->chroma_sample_location - 1;
523  }
524 
527  vui->vui_time_scale = vps->vps_time_scale;
531 
535  vui->max_bytes_per_pic_denom = 0;
536  vui->max_bits_per_min_cu_denom = 0;
538  vui->log2_max_mv_length_vertical = 15;
539 
540 
541  // PPS
542 
545  .nuh_layer_id = 0,
546  .nuh_temporal_id_plus1 = 1,
547  };
548 
549  pps->pps_pic_parameter_set_id = 0;
551 
554 
555  pps->init_qp_minus26 = priv->fixed_qp_idr - 26;
556 
557  pps->cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP);
558  pps->diff_cu_qp_delta_depth = 0;
559 
561 
562 
563  // Fill VAAPI parameter buffers.
564 
565  *vseq = (VAEncSequenceParameterBufferHEVC) {
566  .general_profile_idc = vps->profile_tier_level.general_profile_idc,
567  .general_level_idc = vps->profile_tier_level.general_level_idc,
568  .general_tier_flag = vps->profile_tier_level.general_tier_flag,
569 
570  .intra_period = ctx->gop_size,
571  .intra_idr_period = ctx->gop_size,
572  .ip_period = ctx->b_per_p + 1,
573  .bits_per_second = ctx->va_bit_rate,
574 
575  .pic_width_in_luma_samples = sps->pic_width_in_luma_samples,
576  .pic_height_in_luma_samples = sps->pic_height_in_luma_samples,
577 
578  .seq_fields.bits = {
579  .chroma_format_idc = sps->chroma_format_idc,
580  .separate_colour_plane_flag = sps->separate_colour_plane_flag,
581  .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
582  .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
583  .scaling_list_enabled_flag = sps->scaling_list_enabled_flag,
584  .strong_intra_smoothing_enabled_flag =
586  .amp_enabled_flag = sps->amp_enabled_flag,
587  .sample_adaptive_offset_enabled_flag =
589  .pcm_enabled_flag = sps->pcm_enabled_flag,
590  .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled_flag,
591  .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
592  },
593 
594  .log2_min_luma_coding_block_size_minus3 =
596  .log2_diff_max_min_luma_coding_block_size =
598  .log2_min_transform_block_size_minus2 =
600  .log2_diff_max_min_transform_block_size =
602  .max_transform_hierarchy_depth_inter =
604  .max_transform_hierarchy_depth_intra =
606 
607  .pcm_sample_bit_depth_luma_minus1 =
609  .pcm_sample_bit_depth_chroma_minus1 =
611  .log2_min_pcm_luma_coding_block_size_minus3 =
613  .log2_max_pcm_luma_coding_block_size_minus3 =
616 
617  .vui_parameters_present_flag = 0,
618  };
619 
620  *vpic = (VAEncPictureParameterBufferHEVC) {
621  .decoded_curr_pic = {
622  .picture_id = VA_INVALID_ID,
623  .flags = VA_PICTURE_HEVC_INVALID,
624  },
625 
626  .coded_buf = VA_INVALID_ID,
627 
628  .collocated_ref_pic_index = 0xff,
629 
630  .last_picture = 0,
631 
632  .pic_init_qp = pps->init_qp_minus26 + 26,
633  .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
634  .pps_cb_qp_offset = pps->pps_cb_qp_offset,
635  .pps_cr_qp_offset = pps->pps_cr_qp_offset,
636 
637  .num_tile_columns_minus1 = pps->num_tile_columns_minus1,
638  .num_tile_rows_minus1 = pps->num_tile_rows_minus1,
639 
640  .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level_minus2,
641  .ctu_max_bitsize_allowed = 0,
642 
643  .num_ref_idx_l0_default_active_minus1 =
645  .num_ref_idx_l1_default_active_minus1 =
647 
648  .slice_pic_parameter_set_id = pps->pps_pic_parameter_set_id,
649 
650  .pic_fields.bits = {
651  .sign_data_hiding_enabled_flag = pps->sign_data_hiding_enabled_flag,
652  .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
653  .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
654  .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
655  .weighted_pred_flag = pps->weighted_pred_flag,
656  .weighted_bipred_flag = pps->weighted_bipred_flag,
657  .transquant_bypass_enabled_flag = pps->transquant_bypass_enabled_flag,
658  .tiles_enabled_flag = pps->tiles_enabled_flag,
659  .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
660  .loop_filter_across_tiles_enabled_flag =
662  .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag |
664  .screen_content_flag = 0,
665  .enable_gpu_weighted_prediction = 0,
666  .no_output_of_prior_pics_flag = 0,
667  },
668  };
669 
670  return 0;
671 }
672 
674  VAAPIEncodePicture *pic)
675 {
676  VAAPIEncodeH265Context *priv = avctx->priv_data;
677  VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
678  int i;
679 
680  if (pic->type == PICTURE_TYPE_IDR) {
681  av_assert0(pic->display_order == pic->encode_order);
682 
683  priv->last_idr_frame = pic->display_order;
684 
686  priv->slice_type = HEVC_SLICE_I;
687  priv->pic_type = 0;
688  } else {
689  av_assert0(pic->encode_order > priv->last_idr_frame);
690 
691  if (pic->type == PICTURE_TYPE_I) {
693  priv->slice_type = HEVC_SLICE_I;
694  priv->pic_type = 0;
695  } else if (pic->type == PICTURE_TYPE_P) {
696  av_assert0(pic->refs[0]);
698  priv->slice_type = HEVC_SLICE_P;
699  priv->pic_type = 1;
700  } else {
701  av_assert0(pic->refs[0] && pic->refs[1]);
702  if (pic->refs[1]->type == PICTURE_TYPE_I)
704  else
706  priv->slice_type = HEVC_SLICE_B;
707  priv->pic_type = 2;
708  }
709  }
710  priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
711 
712  if (priv->aud) {
713  priv->aud_needed = 1;
714  priv->raw_aud = (H265RawAUD) {
715  .nal_unit_header = {
717  .nuh_layer_id = 0,
718  .nuh_temporal_id_plus1 = 1,
719  },
720  .pic_type = priv->pic_type,
721  };
722  } else {
723  priv->aud_needed = 0;
724  }
725 
726  priv->sei_needed = 0;
727 
728  // Only look for the metadata on I/IDR frame on the output. We
729  // may force an IDR frame on the output where the medadata gets
730  // changed on the input frame.
731  if ((priv->sei & SEI_MASTERING_DISPLAY) &&
732  (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
733  AVFrameSideData *sd =
736 
737  if (sd) {
740 
741  // SEI is needed when both the primaries and luminance are set
742  if (mdm->has_primaries && mdm->has_luminance) {
744  &priv->sei_mastering_display;
745  const int mapping[3] = {1, 2, 0};
746  const int chroma_den = 50000;
747  const int luma_den = 10000;
748 
749  for (i = 0; i < 3; i++) {
750  const int j = mapping[i];
751  mdcv->display_primaries_x[i] =
752  FFMIN(lrint(chroma_den *
753  av_q2d(mdm->display_primaries[j][0])),
754  chroma_den);
755  mdcv->display_primaries_y[i] =
756  FFMIN(lrint(chroma_den *
757  av_q2d(mdm->display_primaries[j][1])),
758  chroma_den);
759  }
760 
761  mdcv->white_point_x =
762  FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
763  chroma_den);
764  mdcv->white_point_y =
765  FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
766  chroma_den);
767 
769  lrint(luma_den * av_q2d(mdm->max_luminance));
771  FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
773 
775  }
776  }
777  }
778 
779  if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
780  (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
781  AVFrameSideData *sd =
784 
785  if (sd) {
790 
791  clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535);
792  clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
793 
795  }
796  }
797 
798  vpic->decoded_curr_pic = (VAPictureHEVC) {
799  .picture_id = pic->recon_surface,
800  .pic_order_cnt = priv->pic_order_cnt,
801  .flags = 0,
802  };
803 
804  for (i = 0; i < pic->nb_refs; i++) {
805  VAAPIEncodePicture *ref = pic->refs[i];
806  av_assert0(ref && ref->encode_order < pic->encode_order);
807 
808  vpic->reference_frames[i] = (VAPictureHEVC) {
809  .picture_id = ref->recon_surface,
810  .pic_order_cnt = ref->display_order - priv->last_idr_frame,
811  .flags = (ref->display_order < pic->display_order ?
812  VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE : 0) |
813  (ref->display_order > pic->display_order ?
814  VA_PICTURE_HEVC_RPS_ST_CURR_AFTER : 0),
815  };
816  }
817  for (; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) {
818  vpic->reference_frames[i] = (VAPictureHEVC) {
819  .picture_id = VA_INVALID_ID,
820  .flags = VA_PICTURE_HEVC_INVALID,
821  };
822  }
823 
824  vpic->coded_buf = pic->output_buffer;
825 
826  vpic->nal_unit_type = priv->slice_nal_unit;
827 
828  switch (pic->type) {
829  case PICTURE_TYPE_IDR:
830  vpic->pic_fields.bits.idr_pic_flag = 1;
831  vpic->pic_fields.bits.coding_type = 1;
832  vpic->pic_fields.bits.reference_pic_flag = 1;
833  break;
834  case PICTURE_TYPE_I:
835  vpic->pic_fields.bits.idr_pic_flag = 0;
836  vpic->pic_fields.bits.coding_type = 1;
837  vpic->pic_fields.bits.reference_pic_flag = 1;
838  break;
839  case PICTURE_TYPE_P:
840  vpic->pic_fields.bits.idr_pic_flag = 0;
841  vpic->pic_fields.bits.coding_type = 2;
842  vpic->pic_fields.bits.reference_pic_flag = 1;
843  break;
844  case PICTURE_TYPE_B:
845  vpic->pic_fields.bits.idr_pic_flag = 0;
846  vpic->pic_fields.bits.coding_type = 3;
847  vpic->pic_fields.bits.reference_pic_flag = 0;
848  break;
849  default:
850  av_assert0(0 && "invalid picture type");
851  }
852 
853  return 0;
854 }
855 
857  VAAPIEncodePicture *pic,
858  VAAPIEncodeSlice *slice)
859 {
860  VAAPIEncodeContext *ctx = avctx->priv_data;
861  VAAPIEncodeH265Context *priv = avctx->priv_data;
862  const H265RawSPS *sps = &priv->raw_sps;
863  const H265RawPPS *pps = &priv->raw_pps;
864  H265RawSliceHeader *sh = &priv->raw_slice.header;
865  VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
866  VAEncSliceParameterBufferHEVC *vslice = slice->codec_slice_params;
867  int i;
868 
870  .nal_unit_type = priv->slice_nal_unit,
871  .nuh_layer_id = 0,
872  .nuh_temporal_id_plus1 = 1,
873  };
874 
876 
877  sh->first_slice_segment_in_pic_flag = slice->index == 0;
878  sh->slice_segment_address = slice->block_start;
879 
880  sh->slice_type = priv->slice_type;
881 
883  (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1;
884 
885  if (pic->type != PICTURE_TYPE_IDR) {
886  H265RawSTRefPicSet *rps;
887  VAAPIEncodePicture *st;
888  int used;
889 
891 
892  rps = &sh->short_term_ref_pic_set;
893  memset(rps, 0, sizeof(*rps));
894 
895  for (st = ctx->pic_start; st; st = st->next) {
896  if (st->encode_order >= pic->encode_order) {
897  // Not yet in DPB.
898  continue;
899  }
900  used = 0;
901  for (i = 0; i < pic->nb_refs; i++) {
902  if (pic->refs[i] == st)
903  used = 1;
904  }
905  if (!used) {
906  // Usually each picture always uses all of the others in the
907  // DPB as references. The one case we have to treat here is
908  // a non-IDR IRAP picture, which may need to hold unused
909  // references across itself to be used for the decoding of
910  // following RASL pictures. This looks for such an RASL
911  // picture, and keeps the reference if there is one.
912  VAAPIEncodePicture *rp;
913  for (rp = ctx->pic_start; rp; rp = rp->next) {
914  if (rp->encode_order < pic->encode_order)
915  continue;
916  if (rp->type != PICTURE_TYPE_B)
917  continue;
918  if (rp->refs[0] == st && rp->refs[1] == pic)
919  break;
920  }
921  if (!rp)
922  continue;
923  }
924  // This only works for one instance of each (delta_poc_sN_minus1
925  // is relative to the previous frame in the list, not relative to
926  // the current frame directly).
927  if (st->display_order < pic->display_order) {
929  pic->display_order - st->display_order - 1;
931  ++rps->num_negative_pics;
932  } else {
934  st->display_order - pic->display_order - 1;
936  ++rps->num_positive_pics;
937  }
938  }
939 
940  sh->num_long_term_sps = 0;
941  sh->num_long_term_pics = 0;
942 
947  sh->collocated_ref_idx = 0;
948  }
949 
953  }
954 
957 
958  if (pic->type == PICTURE_TYPE_B)
959  sh->slice_qp_delta = priv->fixed_qp_b - (pps->init_qp_minus26 + 26);
960  else if (pic->type == PICTURE_TYPE_P)
961  sh->slice_qp_delta = priv->fixed_qp_p - (pps->init_qp_minus26 + 26);
962  else
963  sh->slice_qp_delta = priv->fixed_qp_idr - (pps->init_qp_minus26 + 26);
964 
965 
966  *vslice = (VAEncSliceParameterBufferHEVC) {
967  .slice_segment_address = sh->slice_segment_address,
968  .num_ctu_in_slice = slice->block_size,
969 
970  .slice_type = sh->slice_type,
971  .slice_pic_parameter_set_id = sh->slice_pic_parameter_set_id,
972 
973  .num_ref_idx_l0_active_minus1 = sh->num_ref_idx_l0_active_minus1,
974  .num_ref_idx_l1_active_minus1 = sh->num_ref_idx_l1_active_minus1,
975 
976  .luma_log2_weight_denom = sh->luma_log2_weight_denom,
977  .delta_chroma_log2_weight_denom = sh->delta_chroma_log2_weight_denom,
978 
979  .max_num_merge_cand = 5 - sh->five_minus_max_num_merge_cand,
980 
981  .slice_qp_delta = sh->slice_qp_delta,
982  .slice_cb_qp_offset = sh->slice_cb_qp_offset,
983  .slice_cr_qp_offset = sh->slice_cr_qp_offset,
984 
985  .slice_beta_offset_div2 = sh->slice_beta_offset_div2,
986  .slice_tc_offset_div2 = sh->slice_tc_offset_div2,
987 
988  .slice_fields.bits = {
989  .last_slice_of_pic_flag = slice->index == pic->nb_slices - 1,
990  .dependent_slice_segment_flag = sh->dependent_slice_segment_flag,
991  .colour_plane_id = sh->colour_plane_id,
992  .slice_temporal_mvp_enabled_flag =
994  .slice_sao_luma_flag = sh->slice_sao_luma_flag,
995  .slice_sao_chroma_flag = sh->slice_sao_chroma_flag,
996  .num_ref_idx_active_override_flag =
998  .mvd_l1_zero_flag = sh->mvd_l1_zero_flag,
999  .cabac_init_flag = sh->cabac_init_flag,
1000  .slice_deblocking_filter_disabled_flag =
1002  .slice_loop_filter_across_slices_enabled_flag =
1004  .collocated_from_l0_flag = sh->collocated_from_l0_flag,
1005  },
1006  };
1007 
1008  for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) {
1009  vslice->ref_pic_list0[i].picture_id = VA_INVALID_ID;
1010  vslice->ref_pic_list0[i].flags = VA_PICTURE_HEVC_INVALID;
1011  vslice->ref_pic_list1[i].picture_id = VA_INVALID_ID;
1012  vslice->ref_pic_list1[i].flags = VA_PICTURE_HEVC_INVALID;
1013  }
1014 
1015  av_assert0(pic->nb_refs <= 2);
1016  if (pic->nb_refs >= 1) {
1017  // Backward reference for P- or B-frame.
1018  av_assert0(pic->type == PICTURE_TYPE_P ||
1019  pic->type == PICTURE_TYPE_B);
1020  vslice->ref_pic_list0[0] = vpic->reference_frames[0];
1021  }
1022  if (pic->nb_refs >= 2) {
1023  // Forward reference for B-frame.
1024  av_assert0(pic->type == PICTURE_TYPE_B);
1025  vslice->ref_pic_list1[0] = vpic->reference_frames[1];
1026  }
1027 
1028  return 0;
1029 }
1030 
1032 {
1033  VAAPIEncodeContext *ctx = avctx->priv_data;
1034  VAAPIEncodeH265Context *priv = avctx->priv_data;
1035  int err;
1036 
1037  err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
1038  if (err < 0)
1039  return err;
1040 
1041  if (ctx->va_rc_mode == VA_RC_CQP) {
1042  priv->fixed_qp_p = priv->qp;
1043  if (avctx->i_quant_factor > 0.0)
1044  priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
1045  avctx->i_quant_offset) + 0.5);
1046  else
1047  priv->fixed_qp_idr = priv->fixed_qp_p;
1048  if (avctx->b_quant_factor > 0.0)
1049  priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
1050  avctx->b_quant_offset) + 0.5);
1051  else
1052  priv->fixed_qp_b = priv->fixed_qp_p;
1053 
1054  av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1055  "%d / %d / %d for IDR- / P- / B-frames.\n",
1056  priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1057 
1058  } else if (ctx->va_rc_mode == VA_RC_CBR ||
1059  ctx->va_rc_mode == VA_RC_VBR) {
1060  // These still need to be set for pic_init_qp/slice_qp_delta.
1061  priv->fixed_qp_idr = 30;
1062  priv->fixed_qp_p = 30;
1063  priv->fixed_qp_b = 30;
1064 
1065  } else {
1066  av_assert0(0 && "Invalid RC mode.");
1067  }
1068 
1069  return 0;
1070 }
1071 
1073  { FF_PROFILE_HEVC_MAIN, 8, 3, 1, 1, VAProfileHEVCMain },
1074  { FF_PROFILE_HEVC_REXT, 8, 3, 1, 1, VAProfileHEVCMain },
1075 #if VA_CHECK_VERSION(0, 37, 0)
1076  { FF_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10 },
1077  { FF_PROFILE_HEVC_REXT, 10, 3, 1, 1, VAProfileHEVCMain10 },
1078 #endif
1079  { FF_PROFILE_UNKNOWN }
1080 };
1081 
1084 
1085  .flags = FLAG_SLICE_CONTROL,
1086 
1087  .configure = &vaapi_encode_h265_configure,
1088 
1089  .sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
1090  .init_sequence_params = &vaapi_encode_h265_init_sequence_params,
1091 
1092  .picture_params_size = sizeof(VAEncPictureParameterBufferHEVC),
1093  .init_picture_params = &vaapi_encode_h265_init_picture_params,
1094 
1095  .slice_params_size = sizeof(VAEncSliceParameterBufferHEVC),
1096  .init_slice_params = &vaapi_encode_h265_init_slice_params,
1097 
1098  .sequence_header_type = VAEncPackedHeaderSequence,
1099  .write_sequence_header = &vaapi_encode_h265_write_sequence_header,
1100 
1101  .slice_header_type = VAEncPackedHeaderHEVC_Slice,
1102  .write_slice_header = &vaapi_encode_h265_write_slice_header,
1103 
1104  .write_extra_header = &vaapi_encode_h265_write_extra_header,
1105 };
1106 
1108 {
1109  VAAPIEncodeContext *ctx = avctx->priv_data;
1110  VAAPIEncodeH265Context *priv = avctx->priv_data;
1111 
1112  ctx->codec = &vaapi_encode_type_h265;
1113 
1114  if (avctx->profile == FF_PROFILE_UNKNOWN)
1115  avctx->profile = priv->profile;
1116  if (avctx->level == FF_LEVEL_UNKNOWN)
1117  avctx->level = priv->level;
1118 
1119  if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
1120  av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
1121  "in 8-bit unsigned integer.\n", avctx->level);
1122  return AVERROR(EINVAL);
1123  }
1124 
1125  ctx->desired_packed_headers =
1126  VA_ENC_PACKED_HEADER_SEQUENCE | // VPS, SPS and PPS.
1127  VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
1128  VA_ENC_PACKED_HEADER_MISC; // SEI
1129 
1130  ctx->surface_width = FFALIGN(avctx->width, 16);
1131  ctx->surface_height = FFALIGN(avctx->height, 16);
1132 
1133  // CTU size is currently hard-coded to 32.
1134  ctx->slice_block_width = ctx->slice_block_height = 32;
1135 
1136  return ff_vaapi_encode_init(avctx);
1137 }
1138 
1140 {
1141  VAAPIEncodeH265Context *priv = avctx->priv_data;
1142 
1143  ff_cbs_close(&priv->cbc);
1144 
1145  return ff_vaapi_encode_close(avctx);
1146 }
1147 
1148 #define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
1149 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1152 
1153  { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1154  OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, 52, FLAGS },
1155 
1156  { "aud", "Include AUD",
1157  OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1158 
1159  { "profile", "Set profile (general_profile_idc)",
1161  { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
1162 
1163 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1164  { .i64 = value }, 0, 0, FLAGS, "profile"
1165  { PROFILE("main", FF_PROFILE_HEVC_MAIN) },
1166  { PROFILE("main10", FF_PROFILE_HEVC_MAIN_10) },
1167  { PROFILE("rext", FF_PROFILE_HEVC_REXT) },
1168 #undef PROFILE
1169 
1170  { "tier", "Set tier (general_tier_flag)",
1171  OFFSET(tier), AV_OPT_TYPE_INT,
1172  { .i64 = 0 }, 0, 1, FLAGS, "tier" },
1173  { "main", NULL, 0, AV_OPT_TYPE_CONST,
1174  { .i64 = 0 }, 0, 0, FLAGS, "tier" },
1175  { "high", NULL, 0, AV_OPT_TYPE_CONST,
1176  { .i64 = 1 }, 0, 0, FLAGS, "tier" },
1177 
1178  { "level", "Set level (general_level_idc)",
1180  { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
1181 
1182 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1183  { .i64 = value }, 0, 0, FLAGS, "level"
1184  { LEVEL("1", 30) },
1185  { LEVEL("2", 60) },
1186  { LEVEL("2.1", 63) },
1187  { LEVEL("3", 90) },
1188  { LEVEL("3.1", 93) },
1189  { LEVEL("4", 120) },
1190  { LEVEL("4.1", 123) },
1191  { LEVEL("5", 150) },
1192  { LEVEL("5.1", 153) },
1193  { LEVEL("5.2", 156) },
1194  { LEVEL("6", 180) },
1195  { LEVEL("6.1", 183) },
1196  { LEVEL("6.2", 186) },
1197 #undef LEVEL
1198 
1199  { "sei", "Set SEI to include",
1202  0, INT_MAX, FLAGS, "sei" },
1203  { "hdr",
1204  "Include HDR metadata for mastering display colour volume "
1205  "and content light level information",
1206  0, AV_OPT_TYPE_CONST,
1208  INT_MIN, INT_MAX, FLAGS, "sei" },
1209 
1210  { NULL },
1211 };
1212 
1214  { "b", "0" },
1215  { "bf", "2" },
1216  { "g", "120" },
1217  { "i_qfactor", "1" },
1218  { "i_qoffset", "0" },
1219  { "b_qfactor", "6/5" },
1220  { "b_qoffset", "0" },
1221  { "qmin", "-1" },
1222  { "qmax", "-1" },
1223  { NULL },
1224 };
1225 
1227  .class_name = "h265_vaapi",
1228  .item_name = av_default_item_name,
1229  .option = vaapi_encode_h265_options,
1230  .version = LIBAVUTIL_VERSION_INT,
1231 };
1232 
1234  .name = "hevc_vaapi",
1235  .long_name = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
1236  .type = AVMEDIA_TYPE_VIDEO,
1237  .id = AV_CODEC_ID_HEVC,
1238  .priv_data_size = sizeof(VAAPIEncodeH265Context),
1240  .encode2 = &ff_vaapi_encode2,
1241  .close = &vaapi_encode_h265_close,
1242  .priv_class = &vaapi_encode_h265_class,
1243  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
1244  .defaults = vaapi_encode_h265_defaults,
1245  .pix_fmts = (const enum AVPixelFormat[]) {
1248  },
1249  .wrapper_name = "vaapi",
1250 };
uint8_t sps_video_parameter_set_id
Definition: cbs_h265.h:256
uint8_t loop_filter_across_tiles_enabled_flag
Definition: cbs_h265.h:393
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3087
uint8_t transfer_characteristics
Definition: cbs_h265.h:150
uint8_t pps_pic_parameter_set_id
Definition: cbs_h265.h:358
uint8_t vps_poc_proportional_to_timing_flag
Definition: cbs_h265.h:217
uint8_t num_long_term_pics
Definition: cbs_h265.h:475
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2498
unsigned MaxCLL
Max content light level (cd/m^2).
uint8_t num_ref_idx_l0_default_active_minus1
Definition: cbs_h265.h:367
uint8_t log2_max_mv_length_horizontal
Definition: cbs_h265.h:182
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
uint8_t motion_vectors_over_pic_boundaries_flag
Definition: cbs_h265.h:177
uint8_t sps_sub_layer_ordering_info_present_flag
Definition: cbs_h265.h:282
uint8_t slice_temporal_mvp_enabled_flag
Definition: cbs_h265.h:482
uint8_t general_max_8bit_constraint_flag
Definition: cbs_h265.h:58
static int vaapi_encode_h265_add_nal(AVCodecContext *avctx, CodedBitstreamFragment *au, void *nal_unit)
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1604
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
uint8_t bitstream_restriction_flag
Definition: cbs_h265.h:175
const char * desc
Definition: nvenc.c:68
uint8_t bit_depth_luma_minus8
Definition: cbs_h265.h:277
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: avcodec.h:1067
H265RawSEIContentLightLevelInfo content_light_level
Definition: cbs_h265.h:698
uint8_t vui_timing_info_present_flag
Definition: cbs_h265.h:167
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:74
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2185
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
Content light level (based on CTA-861.3).
Definition: frame.h:136
uint8_t sps_max_sub_layers_minus1
Definition: cbs_h265.h:258
uint16_t slice_pic_order_cnt_lsb
Definition: cbs_h265.h:468
int num
Numerator.
Definition: rational.h:59
uint8_t sps_max_num_reorder_pics[HEVC_MAX_SUB_LAYERS]
Definition: cbs_h265.h:284
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static const AVClass vaapi_encode_h265_class
uint8_t tiles_enabled_flag
Definition: cbs_h265.h:385
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:570
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:1933
int8_t slice_qp_delta
Definition: cbs_h265.h:519
uint8_t restricted_ref_pic_lists_flag
Definition: cbs_h265.h:178
Mastering display metadata associated with a video frame.
Definition: frame.h:119
H265RawProfileTierLevel profile_tier_level
Definition: cbs_h265.h:261
int8_t slice_beta_offset_div2
Definition: cbs_h265.h:529
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
uint8_t collocated_from_l0_flag
Definition: cbs_h265.h:498
CodedBitstreamContext * cbc
void * codec_sequence_params
Definition: vaapi_encode.h:202
uint8_t sample_adaptive_offset_enabled_flag
Definition: cbs_h265.h:299
uint8_t vui_parameters_present_flag
Definition: cbs_h265.h:319
uint8_t pcm_sample_bit_depth_luma_minus1
Definition: cbs_h265.h:302
static const AVCodecDefault vaapi_encode_h265_defaults[]
uint8_t bit_depth_chroma_minus8
Definition: cbs_h265.h:278
int profile
profile
Definition: avcodec.h:2883
AVCodec.
Definition: avcodec.h:3455
int8_t delta_chroma_log2_weight_denom
Definition: cbs_h265.h:502
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1866
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
uint8_t general_profile_idc
Definition: cbs_h265.h:47
uint8_t used_by_curr_pic_s0_flag[HEVC_MAX_REFS]
Definition: cbs_h265.h:241
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1677
uint16_t slice_segment_address
Definition: cbs_h265.h:460
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:734
uint32_t vps_num_units_in_tick
Definition: cbs_h265.h:215
uint8_t sign_data_hiding_enabled_flag
Definition: cbs_h265.h:364
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:3001
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
uint16_t conf_win_right_offset
Definition: cbs_h265.h:273
#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:995
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
H265RawSEIContentLightLevelInfo sei_content_light_level
uint8_t slice_deblocking_filter_disabled_flag
Definition: cbs_h265.h:528
uint16_t pic_height_in_luma_samples
Definition: cbs_h265.h:269
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
#define av_cold
Definition: attributes.h:82
int8_t slice_tc_offset_div2
Definition: cbs_h265.h:530
AVOptions.
int8_t slice_cb_qp_offset
Definition: cbs_h265.h:520
uint8_t colour_primaries
Definition: cbs_h265.h:149
uint8_t vps_sub_layer_ordering_info_present_flag
Definition: cbs_h265.h:205
uint8_t general_max_12bit_constraint_flag
Definition: cbs_h265.h:56
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:1823
uint8_t five_minus_max_num_merge_cand
Definition: cbs_h265.h:516
#define FF_PROFILE_HEVC_MAIN
Definition: avcodec.h:2971
uint8_t video_format
Definition: cbs_h265.h:146
H265RawSTRefPicSet short_term_ref_pic_set
Definition: cbs_h265.h:471
uint8_t vps_timing_info_present_flag
Definition: cbs_h265.h:214
uint8_t log2_max_pic_order_cnt_lsb_minus4
Definition: cbs_h265.h:280
uint8_t constrained_intra_pred_flag
Definition: cbs_h265.h:372
uint32_t sps_max_latency_increase_plus1[HEVC_MAX_SUB_LAYERS]
Definition: cbs_h265.h:285
Structure to hold side data for an AVFrame.
Definition: frame.h:201
uint8_t matrix_coefficients
Definition: cbs_h265.h:151
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
uint8_t aspect_ratio_info_present_flag
Definition: cbs_h265.h:137
uint8_t vps_max_sub_layers_minus1
Definition: cbs_h265.h:200
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
H265RawSEIPayload payload[H265_MAX_SEI_PAYLOADS]
Definition: cbs_h265.h:712
uint8_t general_max_monochrome_constraint_flag
Definition: cbs_h265.h:61
VASurfaceID recon_surface
Definition: vaapi_encode.h:79
uint8_t log2_min_luma_transform_block_size_minus2
Definition: cbs_h265.h:289
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
static const uint8_t header[24]
Definition: sdr2.c:67
uint8_t sps_scaling_list_data_present_flag
Definition: cbs_h265.h:295
uint8_t log2_min_pcm_luma_coding_block_size_minus3
Definition: cbs_h265.h:304
static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx, char *data, size_t *data_len, CodedBitstreamFragment *au)
#define FF_PROFILE_HEVC_MAIN_10
Definition: avcodec.h:2972
uint8_t video_full_range_flag
Definition: cbs_h265.h:147
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2192
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
uint8_t chroma_sample_loc_type_bottom_field
Definition: cbs_h265.h:155
uint16_t vps_num_layer_sets_minus1
Definition: cbs_h265.h:211
uint8_t video_signal_type_present_flag
Definition: cbs_h265.h:145
const char * name
Definition: pixdesc.h:82
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
H265RawVUI vui
Definition: cbs_h265.h:320
unsigned int va_rc_mode
Definition: vaapi_encode.h:147
uint32_t vui_num_ticks_poc_diff_one_minus1
Definition: cbs_h265.h:171
uint8_t vps_max_dec_pic_buffering_minus1[HEVC_MAX_SUB_LAYERS]
Definition: cbs_h265.h:206
uint8_t log2_max_mv_length_vertical
Definition: cbs_h265.h:183
AVHWFramesContext * input_frames
Definition: vaapi_encode.h:166
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
uint8_t vps_video_parameter_set_id
Definition: cbs_h265.h:195
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:139
uint8_t conformance_window_flag
Definition: cbs_h265.h:271
uint8_t max_bits_per_min_cu_denom
Definition: cbs_h265.h:181
#define AVERROR(e)
Definition: error.h:43
static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
uint8_t dependent_slice_segment_flag
Definition: cbs_h265.h:459
union H265RawSEIPayload::@55 payload
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
uint8_t chroma_loc_info_present_flag
Definition: cbs_h265.h:153
#define PROFILE(name, value)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
uint8_t num_negative_pics
Definition: cbs_h265.h:238
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3462
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:1859
static const AVOption vaapi_encode_h265_options[]
uint8_t general_interlaced_source_flag
Definition: cbs_h265.h:52
static const AVCodecDefault defaults[]
Definition: amfenc_h264.c:361
unsigned int va_bit_rate
Definition: vaapi_encode.h:149
uint8_t general_tier_flag
Definition: cbs_h265.h:46
uint8_t transform_skip_enabled_flag
Definition: cbs_h265.h:373
H265RawNALUnitHeader nal_unit_header
Definition: cbs_h265.h:193
uint8_t pcm_sample_bit_depth_chroma_minus1
Definition: cbs_h265.h:303
uint16_t sar_height
Definition: cbs_h265.h:140
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
void * codec_picture_params
Definition: vaapi_encode.h:88
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:133
#define fail()
Definition: checkasm.h:118
uint8_t chroma_sample_loc_type_top_field
Definition: cbs_h265.h:154
uint8_t vps_max_layers_minus1
Definition: cbs_h265.h:199
uint32_t vui_time_scale
Definition: cbs_h265.h:169
static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice)
uint16_t conf_win_bottom_offset
Definition: cbs_h265.h:275
H265RawNALUnitHeader nal_unit_header
Definition: cbs_h265.h:447
static const VAAPIEncodeType vaapi_encode_type_h265
uint8_t slice_type
Definition: cbs_h265.h:463
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
uint8_t scaling_list_enabled_flag
Definition: cbs_h265.h:294
uint32_t payload_type
Definition: cbs_h265.h:684
H265RawProfileTierLevel profile_tier_level
Definition: cbs_h265.h:203
#define FFMIN(a, b)
Definition: common.h:96
uint8_t general_max_422chroma_constraint_flag
Definition: cbs_h265.h:59
uint16_t sar_width
Definition: cbs_h265.h:139
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
uint8_t transquant_bypass_enabled_flag
Definition: cbs_h265.h:384
int width
picture width / height.
Definition: avcodec.h:1727
uint8_t general_intra_constraint_flag
Definition: cbs_h265.h:62
uint32_t vps_max_latency_increase_plus1[HEVC_MAX_SUB_LAYERS]
Definition: cbs_h265.h:208
uint8_t vui_hrd_parameters_present_flag
Definition: cbs_h265.h:172
#define FLAGS
uint16_t delta_poc_s1_minus1[HEVC_MAX_REFS]
Definition: cbs_h265.h:242
uint8_t diff_cu_qp_delta_depth
Definition: cbs_h265.h:375
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2884
const VAAPIEncodeProfile * profiles
Definition: vaapi_encode.h:261
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
AVCodec ff_hevc_vaapi_encoder
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:155
uint8_t general_non_packed_constraint_flag
Definition: cbs_h265.h:53
AVFormatContext * ctx
Definition: movenc.c:48
uint8_t colour_plane_id
Definition: cbs_h265.h:466
uint8_t sps_seq_parameter_set_id
Definition: cbs_h265.h:263
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2164
struct VAAPIEncodePicture * next
Definition: vaapi_encode.h:64
int level
level
Definition: avcodec.h:3000
AVFrame * input_image
Definition: vaapi_encode.h:75
uint16_t pic_width_in_luma_samples
Definition: cbs_h265.h:268
void * codec_picture_params
Definition: vaapi_encode.h:206
static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
uint8_t vps_base_layer_available_flag
Definition: cbs_h265.h:198
uint8_t log2_min_luma_coding_block_size_minus3
Definition: cbs_h265.h:287
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
uint8_t pps_seq_parameter_set_id
Definition: cbs_h265.h:359
#define FF_ARRAY_ELEMS(a)
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:519
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:282
uint16_t vps_num_hrd_parameters
Definition: cbs_h265.h:219
uint8_t num_ref_idx_l1_active_minus1
Definition: cbs_h265.h:489
uint8_t general_max_420chroma_constraint_flag
Definition: cbs_h265.h:60
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
Definition: vaapi_encode.h:91
uint8_t used_by_curr_pic_s1_flag[HEVC_MAX_REFS]
Definition: cbs_h265.h:243
#define OFFSET(x)
const H265LevelDescriptor * ff_h265_guess_level(const H265RawProfileTierLevel *ptl, int64_t bitrate, int width, int height, int slice_segments, int tile_rows, int tile_cols, int max_dec_pic_buffering)
Guess the level of a stream from some parameters.
uint8_t max_bytes_per_pic_denom
Definition: cbs_h265.h:180
uint8_t num_ref_idx_l0_active_minus1
Definition: cbs_h265.h:488
uint8_t payload_count
Definition: cbs_h265.h:713
H265RawNALUnitHeader nal_unit_header
Definition: cbs_h265.h:254
static int vaapi_encode_h265_write_slice_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice, char *data, size_t *data_len)
const struct VAAPIEncodeType * codec
Definition: vaapi_encode.h:116
uint8_t log2_diff_max_min_pcm_luma_coding_block_size
Definition: cbs_h265.h:305
Libavcodec external API header.
uint8_t entropy_coding_sync_enabled_flag
Definition: cbs_h265.h:386
uint8_t weighted_bipred_flag
Definition: cbs_h265.h:382
uint16_t conf_win_top_offset
Definition: cbs_h265.h:274
uint8_t sps_temporal_mvp_enabled_flag
Definition: cbs_h265.h:316
VAAPIEncodePicture * pic_start
Definition: vaapi_encode.h:209
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
uint8_t general_level_idc
Definition: cbs_h265.h:69
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
main external API structure.
Definition: avcodec.h:1554
int8_t slice_cr_qp_offset
Definition: cbs_h265.h:521
uint32_t vui_num_units_in_tick
Definition: cbs_h265.h:168
uint8_t short_term_ref_pic_set_sps_flag
Definition: cbs_h265.h:470
uint8_t slice_loop_filter_across_slices_enabled_flag
Definition: cbs_h265.h:531
uint8_t luma_log2_weight_denom
Definition: cbs_h265.h:501
uint8_t max_transform_hierarchy_depth_intra
Definition: cbs_h265.h:292
uint8_t mvd_l1_zero_flag
Definition: cbs_h265.h:496
uint8_t * data
Definition: frame.h:203
uint8_t pps_loop_filter_across_slices_enabled_flag
Definition: cbs_h265.h:395
uint8_t weighted_pred_flag
Definition: cbs_h265.h:381
uint8_t sps_temporal_id_nesting_flag
Definition: cbs_h265.h:259
GLint GLenum type
Definition: opengl_enc.c:105
int8_t pps_cb_qp_offset
Definition: cbs_h265.h:377
H265RawSEIMasteringDisplayColourVolume mastering_display
Definition: cbs_h265.h:697
static int FUNC() aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
uint8_t vps_max_layer_id
Definition: cbs_h265.h:210
uint16_t conf_win_left_offset
Definition: cbs_h265.h:272
Describe the class of an AVClass context structure.
Definition: log.h:67
uint8_t collocated_ref_idx
Definition: cbs_h265.h:499
uint8_t amp_enabled_flag
Definition: cbs_h265.h:298
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Context structure for coded bitstream operations.
Definition: cbs.h:159
int index
Definition: gxfenc.c:89
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2178
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2171
Mastering display metadata capable of representing the color volume of the display used to master the...
uint8_t general_max_10bit_constraint_flag
Definition: cbs_h265.h:57
static int vaapi_encode_h265_write_extra_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, int index, int *type, char *data, size_t *data_len)
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
uint8_t chroma_format_idc
Definition: cbs_h265.h:265
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:113
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1836
uint8_t general_lower_bit_rate_constraint_flag
Definition: cbs_h265.h:64
uint8_t general_profile_compatibility_flag[32]
Definition: cbs_h265.h:49
uint8_t vps_base_layer_internal_flag
Definition: cbs_h265.h:197
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
mfxU16 profile
Definition: qsvenc.c:44
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
uint8_t pcm_loop_filter_disabled_flag
Definition: cbs_h265.h:306
H265RawNALUnitHeader nal_unit_header
Definition: cbs_h265.h:356
#define VAAPI_ENCODE_COMMON_OPTIONS
Definition: vaapi_encode.h:330
static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
uint8_t num_positive_pics
Definition: cbs_h265.h:239
uint8_t general_frame_only_constraint_flag
Definition: cbs_h265.h:54
uint8_t level
Definition: svq3.c:207
int8_t pps_cr_qp_offset
Definition: cbs_h265.h:378
static const VAAPIEncodeProfile vaapi_encode_h265_profiles[]
uint8_t slice_pic_parameter_set_id
Definition: cbs_h265.h:457
uint8_t vui_poc_proportional_to_timing_flag
Definition: cbs_h265.h:170
uint8_t general_progressive_source_flag
Definition: cbs_h265.h:51
int
uint8_t max_transform_hierarchy_depth_inter
Definition: cbs_h265.h:291
static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
VAAPIEncodeContext common
common internal api header.
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:279
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
uint8_t pps_scaling_list_data_present_flag
Definition: cbs_h265.h:402
uint8_t cabac_init_flag
Definition: cbs_h265.h:497
uint8_t num_tile_rows_minus1
Definition: cbs_h265.h:389
uint8_t pcm_enabled_flag
Definition: cbs_h265.h:301
uint8_t slice_sao_luma_flag
Definition: cbs_h265.h:484
int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *input_image, int *got_packet)
Definition: vaapi_encode.c:895
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
uint8_t num_tile_columns_minus1
Definition: cbs_h265.h:388
int den
Denominator.
Definition: rational.h:60
uint16_t delta_poc_s0_minus1[HEVC_MAX_REFS]
Definition: cbs_h265.h:240
uint8_t log2_diff_max_min_luma_coding_block_size
Definition: cbs_h265.h:288
CodedBitstreamFragment current_access_unit
uint8_t slice_sao_chroma_flag
Definition: cbs_h265.h:485
#define FF_PROFILE_HEVC_REXT
Definition: avcodec.h:2974
void * priv_data
Definition: avcodec.h:1581
uint8_t vps_max_num_reorder_pics[HEVC_MAX_SUB_LAYERS]
Definition: cbs_h265.h:207
H265RawSliceHeader header
Definition: cbs_h265.h:543
H265RawSEIMasteringDisplayColourVolume sei_mastering_display
uint32_t vps_time_scale
Definition: cbs_h265.h:216
uint8_t num_short_term_ref_pic_sets
Definition: cbs_h265.h:308
void * codec_slice_params
Definition: vaapi_encode.h:60
uint8_t log2_parallel_merge_level_minus2
Definition: cbs_h265.h:406
uint8_t vps_temporal_id_nesting_flag
Definition: cbs_h265.h:201
#define lrint
Definition: tablegen.h:53
uint8_t aspect_ratio_idc
Definition: cbs_h265.h:138
unsigned int desired_packed_headers
Definition: vaapi_encode.h:124
#define LEVEL(name, value)
H265RawNALUnitHeader nal_unit_header
Definition: cbs_h265.h:453
uint8_t num_long_term_sps
Definition: cbs_h265.h:474
uint8_t general_profile_space
Definition: cbs_h265.h:45
uint8_t sps_max_dec_pic_buffering_minus1[HEVC_MAX_SUB_LAYERS]
Definition: cbs_h265.h:283
int8_t init_qp_minus26
Definition: cbs_h265.h:370
uint32_t vps_num_ticks_poc_diff_one_minus1
Definition: cbs_h265.h:218
int depth
Number of bits in the component.
Definition: pixdesc.h:58
uint8_t separate_colour_plane_flag
Definition: cbs_h265.h:266
VABufferID output_buffer
Definition: vaapi_encode.h:85
uint8_t strong_intra_smoothing_enabled_flag
Definition: cbs_h265.h:317
uint8_t colour_description_present_flag
Definition: cbs_h265.h:148
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:221
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
unsigned MaxFALL
Max average light level per frame (cd/m^2).
uint8_t log2_diff_max_min_luma_transform_block_size
Definition: cbs_h265.h:290
uint8_t long_term_ref_pics_present_flag
Definition: cbs_h265.h:311
uint8_t num_ref_idx_active_override_flag
Definition: cbs_h265.h:487
uint8_t layer_id_included_flag[HEVC_MAX_LAYER_SETS][HEVC_MAX_LAYERS]
Definition: cbs_h265.h:212
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
uint8_t nal_unit_type
Definition: cbs_h265.h:39
uint8_t num_ref_idx_l1_default_active_minus1
Definition: cbs_h265.h:368
H265RawNALUnitHeader nal_unit_header
Definition: cbs_h265.h:710
uint8_t first_slice_segment_in_pic_flag
Definition: cbs_h265.h:455
uint8_t cu_qp_delta_enabled_flag
Definition: cbs_h265.h:374
bitstream writer API