FFmpeg
d3d12va_encode_hevc.c
Go to the documentation of this file.
1 /*
2  * Direct3D 12 HW acceleration video encoder
3  *
4  * Copyright (c) 2024 Intel Corporation
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #include "libavutil/opt.h"
23 #include "libavutil/common.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/pixdesc.h"
27 
28 #include "avcodec.h"
29 #include "cbs.h"
30 #include "cbs_h265.h"
31 #include "hw_base_encode_h265.h"
32 #include "h2645data.h"
33 #include "h265_profile_level.h"
34 #include "codec_internal.h"
35 #include "d3d12va_encode.h"
36 
37 typedef struct D3D12VAEncodeHEVCPicture {
41 
42 typedef struct D3D12VAEncodeHEVCContext {
44 
45  // User options.
46  int qp;
47  int profile;
48  int level;
49 
50  // Writer structures.
53 
57 
58 typedef struct D3D12VAEncodeHEVCLevel {
59  int level;
60  D3D12_VIDEO_ENCODER_LEVELS_HEVC d3d12_level;
62 
63 static const D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC hevc_config_support_sets[] =
64 {
65  {
66  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_NONE,
67  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_8x8,
68  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_32x32,
69  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_4x4,
70  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_32x32,
71  3,
72  3,
73  },
74  {
75  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_NONE,
76  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_8x8,
77  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_32x32,
78  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_4x4,
79  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_32x32,
80  0,
81  0,
82  },
83  {
84  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_NONE,
85  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_8x8,
86  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_32x32,
87  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_4x4,
88  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_32x32,
89  2,
90  2,
91  },
92  {
93  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_NONE,
94  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_8x8,
95  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_64x64,
96  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_4x4,
97  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_32x32,
98  2,
99  2,
100  },
101  {
102  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_NONE,
103  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_8x8,
104  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_64x64,
105  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_4x4,
106  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_32x32,
107  4,
108  4,
109  },
110 };
111 
113  { 30, D3D12_VIDEO_ENCODER_LEVELS_HEVC_1 },
114  { 60, D3D12_VIDEO_ENCODER_LEVELS_HEVC_2 },
115  { 63, D3D12_VIDEO_ENCODER_LEVELS_HEVC_21 },
116  { 90, D3D12_VIDEO_ENCODER_LEVELS_HEVC_3 },
117  { 93, D3D12_VIDEO_ENCODER_LEVELS_HEVC_31 },
118  { 120, D3D12_VIDEO_ENCODER_LEVELS_HEVC_4 },
119  { 123, D3D12_VIDEO_ENCODER_LEVELS_HEVC_41 },
120  { 150, D3D12_VIDEO_ENCODER_LEVELS_HEVC_5 },
121  { 153, D3D12_VIDEO_ENCODER_LEVELS_HEVC_51 },
122  { 156, D3D12_VIDEO_ENCODER_LEVELS_HEVC_52 },
123  { 180, D3D12_VIDEO_ENCODER_LEVELS_HEVC_6 },
124  { 183, D3D12_VIDEO_ENCODER_LEVELS_HEVC_61 },
125  { 186, D3D12_VIDEO_ENCODER_LEVELS_HEVC_62 },
126 };
127 
128 static const D3D12_VIDEO_ENCODER_PROFILE_HEVC profile_main = D3D12_VIDEO_ENCODER_PROFILE_HEVC_MAIN;
129 static const D3D12_VIDEO_ENCODER_PROFILE_HEVC profile_main10 = D3D12_VIDEO_ENCODER_PROFILE_HEVC_MAIN10;
130 
131 #define D3D_PROFILE_DESC(name) \
132  { sizeof(D3D12_VIDEO_ENCODER_PROFILE_HEVC), { .pHEVCProfile = (D3D12_VIDEO_ENCODER_PROFILE_HEVC *)&profile_ ## name } }
134  { AV_PROFILE_HEVC_MAIN, 8, 3, 1, 1, D3D_PROFILE_DESC(main) },
135  { AV_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, D3D_PROFILE_DESC(main10) },
136  { AV_PROFILE_UNKNOWN },
137 };
138 
139 static uint8_t d3d12va_encode_hevc_map_cusize(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE cusize)
140 {
141  switch (cusize) {
142  case D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_8x8: return 8;
143  case D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_16x16: return 16;
144  case D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_32x32: return 32;
145  case D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE_64x64: return 64;
146  default: av_assert0(0);
147  }
148  return 0;
149 }
150 
151 static uint8_t d3d12va_encode_hevc_map_tusize(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE tusize)
152 {
153  switch (tusize) {
154  case D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_4x4: return 4;
155  case D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_8x8: return 8;
156  case D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_16x16: return 16;
157  case D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE_32x32: return 32;
158  default: av_assert0(0);
159  }
160  return 0;
161 }
162 
164  char *data, size_t *data_len,
166 {
167  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
168  int err;
169 
170  err = ff_cbs_write_fragment_data(priv->cbc, au);
171  if (err < 0) {
172  av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
173  return err;
174  }
175 
176  if (*data_len < 8 * au->data_size - au->data_bit_padding) {
177  av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
178  "%zu < %zu.\n", *data_len,
179  8 * au->data_size - au->data_bit_padding);
180  return AVERROR(ENOSPC);
181  }
182 
183  memcpy(data, au->data, au->data_size);
184  *data_len = 8 * au->data_size - au->data_bit_padding;
185 
186  return 0;
187 }
188 
191  void *nal_unit)
192 {
193  H265RawNALUnitHeader *header = nal_unit;
194  int err;
195 
196  err = ff_cbs_insert_unit_content(au, -1,
197  header->nal_unit_type, nal_unit, NULL);
198  if (err < 0) {
199  av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
200  "type = %d.\n", header->nal_unit_type);
201  return err;
202  }
203 
204  return 0;
205 }
206 
208  char *data, size_t *data_len)
209 {
210  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
212  int err;
213 
214  err = d3d12va_encode_hevc_add_nal(avctx, au, &priv->units.raw_vps);
215  if (err < 0)
216  goto fail;
217 
218  err = d3d12va_encode_hevc_add_nal(avctx, au, &priv->units.raw_sps);
219  if (err < 0)
220  goto fail;
221 
222  err = d3d12va_encode_hevc_add_nal(avctx, au, &priv->units.raw_pps);
223  if (err < 0)
224  goto fail;
225 
226  err = d3d12va_encode_hevc_write_access_unit(avctx, data, data_len, au);
227 fail:
228  ff_cbs_fragment_reset(au);
229  return err;
230 
231 }
232 
234 {
235  FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
237  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
238  AVD3D12VAFramesContext *hwctx = base_ctx->input_frames->hwctx;
239  H265RawSPS *sps = &priv->units.raw_sps;
240  H265RawPPS *pps = &priv->units.raw_pps;
241  D3D12_VIDEO_ENCODER_PROFILE_HEVC profile = D3D12_VIDEO_ENCODER_PROFILE_HEVC_MAIN;
242  D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC level = { 0 };
243  const AVPixFmtDescriptor *desc;
244  uint8_t min_cu_size, max_cu_size, min_tu_size, max_tu_size;
245  HRESULT hr;
246  int err;
247 
248  D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT support = {
249  .NodeIndex = 0,
250  .Codec = D3D12_VIDEO_ENCODER_CODEC_HEVC,
251  .InputFormat = hwctx->format,
252  .RateControl = ctx->rc,
253  .IntraRefresh = D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE,
254  .SubregionFrameEncoding = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME,
255  .ResolutionsListCount = 1,
256  .pResolutionList = &ctx->resolution,
257  .CodecGopSequence = ctx->gop,
258  .MaxReferenceFramesInDPB = MAX_DPB_SIZE - 1,
259  .CodecConfiguration = ctx->codec_conf,
260  .SuggestedProfile.DataSize = sizeof(D3D12_VIDEO_ENCODER_PROFILE_HEVC),
261  .SuggestedProfile.pHEVCProfile = &profile,
262  .SuggestedLevel.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC),
263  .SuggestedLevel.pHEVCLevelSetting = &level,
264  .pResolutionDependentSupport = &ctx->res_limits,
265  };
266 
267  hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_SUPPORT,
268  &support, sizeof(support));
269 
270  if (FAILED(hr)) {
271  av_log(avctx, AV_LOG_ERROR, "Failed to check encoder support(%lx).\n", (long)hr);
272  return AVERROR(EINVAL);
273  }
274 
275  if (!(support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_GENERAL_SUPPORT_OK)) {
276  av_log(avctx, AV_LOG_ERROR, "Driver does not support some request features. %#x\n",
277  support.ValidationFlags);
278  return AVERROR(EINVAL);
279  }
280 
281  if (support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_RECONSTRUCTED_FRAMES_REQUIRE_TEXTURE_ARRAYS) {
282  ctx->is_texture_array = 1;
283  av_log(avctx, AV_LOG_DEBUG, "D3D12 video encode on this device uses texture array mode.\n");
284  }
285 
287  av_assert0(desc);
288 
289  min_cu_size = d3d12va_encode_hevc_map_cusize(ctx->codec_conf.pHEVCConfig->MinLumaCodingUnitSize);
290  max_cu_size = d3d12va_encode_hevc_map_cusize(ctx->codec_conf.pHEVCConfig->MaxLumaCodingUnitSize);
291  min_tu_size = d3d12va_encode_hevc_map_tusize(ctx->codec_conf.pHEVCConfig->MinLumaTransformUnitSize);
292  max_tu_size = d3d12va_encode_hevc_map_tusize(ctx->codec_conf.pHEVCConfig->MaxLumaTransformUnitSize);
293 
294  // cu_qp_delta always required to be 1 in https://github.com/microsoft/DirectX-Specs/blob/master/d3d/D3D12VideoEncoding.md
296  priv->unit_opts.nb_slices = 1;
297 
298  err = ff_hw_base_encode_init_params_h265(base_ctx, avctx,
299  &priv->units, &priv->unit_opts);
300  if (err < 0)
301  return err;
302 
304 
305  av_assert0(ctx->res_limits.SubregionBlockPixelsSize % min_cu_size == 0);
306 
307  sps->pic_width_in_luma_samples = FFALIGN(base_ctx->surface_width,
308  ctx->res_limits.SubregionBlockPixelsSize);
309  sps->pic_height_in_luma_samples = FFALIGN(base_ctx->surface_height,
310  ctx->res_limits.SubregionBlockPixelsSize);
311 
312  if (avctx->width != sps->pic_width_in_luma_samples ||
313  avctx->height != sps->pic_height_in_luma_samples) {
314  sps->conformance_window_flag = 1;
315  sps->conf_win_left_offset = 0;
316  sps->conf_win_right_offset =
317  (sps->pic_width_in_luma_samples - avctx->width) >> desc->log2_chroma_w;
318  sps->conf_win_top_offset = 0;
319  sps->conf_win_bottom_offset =
320  (sps->pic_height_in_luma_samples - avctx->height) >> desc->log2_chroma_h;
321  } else {
322  sps->conformance_window_flag = 0;
323  }
324 
325  sps->log2_max_pic_order_cnt_lsb_minus4 = ctx->gop.pHEVCGroupOfPictures->log2_max_pic_order_cnt_lsb_minus4;
326 
327  sps->log2_min_luma_coding_block_size_minus3 = (uint8_t)(av_log2(min_cu_size) - 3);
328  sps->log2_diff_max_min_luma_coding_block_size = (uint8_t)(av_log2(max_cu_size) - av_log2(min_cu_size));
329  sps->log2_min_luma_transform_block_size_minus2 = (uint8_t)(av_log2(min_tu_size) - 2);
330  sps->log2_diff_max_min_luma_transform_block_size = (uint8_t)(av_log2(max_tu_size) - av_log2(min_tu_size));
331 
332  sps->max_transform_hierarchy_depth_inter = ctx->codec_conf.pHEVCConfig->max_transform_hierarchy_depth_inter;
333  sps->max_transform_hierarchy_depth_intra = ctx->codec_conf.pHEVCConfig->max_transform_hierarchy_depth_intra;
334 
335  sps->amp_enabled_flag = !!(ctx->codec_conf.pHEVCConfig->ConfigurationFlags &
336  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_USE_ASYMETRIC_MOTION_PARTITION);
337  sps->sample_adaptive_offset_enabled_flag = !!(ctx->codec_conf.pHEVCConfig->ConfigurationFlags &
338  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_ENABLE_SAO_FILTER);
339 
340  pps->cabac_init_present_flag = 1;
341 
342  pps->init_qp_minus26 = 0;
343 
344  pps->transform_skip_enabled_flag = !!(ctx->codec_conf.pHEVCConfig->ConfigurationFlags &
345  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_ENABLE_TRANSFORM_SKIPPING);
346 
347  pps->pps_slice_chroma_qp_offsets_present_flag = 1;
348 
349  pps->tiles_enabled_flag = 0; // no tiling in D3D12
350 
351  pps->pps_loop_filter_across_slices_enabled_flag = !(ctx->codec_conf.pHEVCConfig->ConfigurationFlags &
352  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_DISABLE_LOOP_FILTER_ACROSS_SLICES);
353 
354  pps->deblocking_filter_control_present_flag = 1;
355 
356  return 0;
357 }
358 
360 {
361  int i;
362  HRESULT hr;
363  uint8_t min_cu_size, max_cu_size;
364  FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
366  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC *config;
367  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC hevc_caps;
368 
369  D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT codec_caps = {
370  .NodeIndex = 0,
371  .Codec = D3D12_VIDEO_ENCODER_CODEC_HEVC,
372  .Profile = ctx->profile->d3d12_profile,
373  .CodecSupportLimits.DataSize = sizeof(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC),
374  };
375 
376  for (i = 0; i < FF_ARRAY_ELEMS(hevc_config_support_sets); i++) {
377  hevc_caps = hevc_config_support_sets[i];
378  codec_caps.CodecSupportLimits.pHEVCSupport = &hevc_caps;
379  hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT,
380  &codec_caps, sizeof(codec_caps));
381  if (SUCCEEDED(hr) && codec_caps.IsSupported)
382  break;
383  }
384 
386  av_log(avctx, AV_LOG_ERROR, "Unsupported codec configuration\n");
387  return AVERROR(EINVAL);
388  }
389 
390  ctx->codec_conf.DataSize = sizeof(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC);
391  ctx->codec_conf.pHEVCConfig = av_mallocz(ctx->codec_conf.DataSize);
392  if (!ctx->codec_conf.pHEVCConfig)
393  return AVERROR(ENOMEM);
394 
395  config = ctx->codec_conf.pHEVCConfig;
396 
397  config->ConfigurationFlags = D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_NONE;
398  config->MinLumaCodingUnitSize = hevc_caps.MinLumaCodingUnitSize;
399  config->MaxLumaCodingUnitSize = hevc_caps.MaxLumaCodingUnitSize;
400  config->MinLumaTransformUnitSize = hevc_caps.MinLumaTransformUnitSize;
401  config->MaxLumaTransformUnitSize = hevc_caps.MaxLumaTransformUnitSize;
402  config->max_transform_hierarchy_depth_inter = hevc_caps.max_transform_hierarchy_depth_inter;
403  config->max_transform_hierarchy_depth_intra = hevc_caps.max_transform_hierarchy_depth_intra;
404 
405  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_ASYMETRIC_MOTION_PARTITION_SUPPORT ||
406  hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_ASYMETRIC_MOTION_PARTITION_REQUIRED)
407  config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_USE_ASYMETRIC_MOTION_PARTITION;
408 
409  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_SAO_FILTER_SUPPORT)
410  config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_ENABLE_SAO_FILTER;
411 
412  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_DISABLING_LOOP_FILTER_ACROSS_SLICES_SUPPORT)
413  config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_DISABLE_LOOP_FILTER_ACROSS_SLICES;
414 
415  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_TRANSFORM_SKIP_SUPPORT)
416  config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_ENABLE_TRANSFORM_SKIPPING;
417 
418  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_P_FRAMES_IMPLEMENTED_AS_LOW_DELAY_B_FRAMES)
419  ctx->bi_not_empty = 1;
420 
421  // block sizes
422  min_cu_size = d3d12va_encode_hevc_map_cusize(hevc_caps.MinLumaCodingUnitSize);
423  max_cu_size = d3d12va_encode_hevc_map_cusize(hevc_caps.MaxLumaCodingUnitSize);
424 
425  av_log(avctx, AV_LOG_VERBOSE, "Using CTU size %dx%d, "
426  "min CB size %dx%d.\n", max_cu_size, max_cu_size,
427  min_cu_size, min_cu_size);
428 
429  base_ctx->surface_width = FFALIGN(avctx->width, min_cu_size);
430  base_ctx->surface_height = FFALIGN(avctx->height, min_cu_size);
431 
432  return 0;
433 }
434 
436 {
437  FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
439  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
440  int fixed_qp_idr, fixed_qp_p, fixed_qp_b;
441  int err;
442 
443  err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
444  if (err < 0)
445  return err;
446 
447  // Rate control
448  if (ctx->rc.Mode == D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP) {
449  D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP *cqp_ctl;
450  fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
451  if (avctx->i_quant_factor > 0.0)
452  fixed_qp_idr = av_clip((avctx->i_quant_factor * fixed_qp_p +
453  avctx->i_quant_offset) + 0.5, 1, 51);
454  else
455  fixed_qp_idr = fixed_qp_p;
456  if (avctx->b_quant_factor > 0.0)
457  fixed_qp_b = av_clip((avctx->b_quant_factor * fixed_qp_p +
458  avctx->b_quant_offset) + 0.5, 1, 51);
459  else
460  fixed_qp_b = fixed_qp_p;
461 
462  av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
463  "%d / %d / %d for IDR- / P- / B-frames.\n",
464  fixed_qp_idr, fixed_qp_p, fixed_qp_b);
465 
466  ctx->rc.ConfigParams.DataSize = sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP);
467  cqp_ctl = av_mallocz(ctx->rc.ConfigParams.DataSize);
468  if (!cqp_ctl)
469  return AVERROR(ENOMEM);
470 
471  cqp_ctl->ConstantQP_FullIntracodedFrame = fixed_qp_idr;
472  cqp_ctl->ConstantQP_InterPredictedFrame_PrevRefOnly = fixed_qp_p;
473  cqp_ctl->ConstantQP_InterPredictedFrame_BiDirectionalRef = fixed_qp_b;
474 
475  ctx->rc.ConfigParams.pConfiguration_CQP = cqp_ctl;
476  }
477 
478  // GOP
479  ctx->gop.DataSize = sizeof(D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE_HEVC);
480  ctx->gop.pHEVCGroupOfPictures = av_mallocz(ctx->gop.DataSize);
481  if (!ctx->gop.pHEVCGroupOfPictures)
482  return AVERROR(ENOMEM);
483 
484  ctx->gop.pHEVCGroupOfPictures->GOPLength = base_ctx->gop_size;
485  ctx->gop.pHEVCGroupOfPictures->PPicturePeriod = base_ctx->b_per_p + 1;
486  // Power of 2
487  if (base_ctx->gop_size & base_ctx->gop_size - 1 == 0)
488  ctx->gop.pHEVCGroupOfPictures->log2_max_pic_order_cnt_lsb_minus4 =
489  FFMAX(av_log2(base_ctx->gop_size) - 4, 0);
490  else
491  ctx->gop.pHEVCGroupOfPictures->log2_max_pic_order_cnt_lsb_minus4 =
492  FFMAX(av_log2(base_ctx->gop_size) - 3, 0);
493 
494  return 0;
495 }
496 
498 {
500  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
501  int i;
502 
503  ctx->level.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC);
504  ctx->level.pHEVCLevelSetting = av_mallocz(ctx->level.DataSize);
505  if (!ctx->level.pHEVCLevelSetting)
506  return AVERROR(ENOMEM);
507 
508  for (i = 0; i < FF_ARRAY_ELEMS(hevc_levels); i++) {
509  if (avctx->level == hevc_levels[i].level) {
510  ctx->level.pHEVCLevelSetting->Level = hevc_levels[i].d3d12_level;
511  break;
512  }
513  }
514 
515  if (i == FF_ARRAY_ELEMS(hevc_levels)) {
516  av_log(avctx, AV_LOG_ERROR, "Invalid level %d.\n", avctx->level);
517  return AVERROR(EINVAL);
518  }
519 
520  ctx->level.pHEVCLevelSetting->Tier = priv->units.raw_vps.profile_tier_level.general_tier_flag == 0 ?
521  D3D12_VIDEO_ENCODER_TIER_HEVC_MAIN :
522  D3D12_VIDEO_ENCODER_TIER_HEVC_HIGH;
523 
524  return 0;
525 }
526 
528 {
529  if (!pic->pic_ctl.pHEVCPicData)
530  return;
531 
532  av_freep(&pic->pic_ctl.pHEVCPicData->pList0ReferenceFrames);
533  av_freep(&pic->pic_ctl.pHEVCPicData->pList1ReferenceFrames);
534  av_freep(&pic->pic_ctl.pHEVCPicData->pReferenceFramesReconPictureDescriptors);
535  av_freep(&pic->pic_ctl.pHEVCPicData);
536 }
537 
539  FFHWBaseEncodePicture *base_pic)
540 {
541  D3D12VAEncodePicture *pic = base_pic->priv;
542  D3D12VAEncodeHEVCPicture *hpic = base_pic->codec_priv;
543  FFHWBaseEncodePicture *prev = base_pic->prev;
544  D3D12VAEncodeHEVCPicture *hprev = prev ? prev->codec_priv : NULL;
545  D3D12_VIDEO_ENCODER_REFERENCE_PICTURE_DESCRIPTOR_HEVC *pd = NULL;
546  UINT *ref_list0 = NULL, *ref_list1 = NULL;
547  int i, idx = 0;
548 
549  pic->pic_ctl.DataSize = sizeof(D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA_HEVC);
550  pic->pic_ctl.pHEVCPicData = av_mallocz(pic->pic_ctl.DataSize);
551  if (!pic->pic_ctl.pHEVCPicData)
552  return AVERROR(ENOMEM);
553 
554  if (base_pic->type == FF_HW_PICTURE_TYPE_IDR) {
555  av_assert0(base_pic->display_order == base_pic->encode_order);
556  hpic->last_idr_frame = base_pic->display_order;
557  } else {
558  av_assert0(prev);
559  hpic->last_idr_frame = hprev->last_idr_frame;
560  }
561  hpic->pic_order_cnt = base_pic->display_order - hpic->last_idr_frame;
562 
563  switch(base_pic->type) {
565  pic->pic_ctl.pHEVCPicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_HEVC_IDR_FRAME;
566  break;
568  pic->pic_ctl.pHEVCPicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_HEVC_I_FRAME;
569  break;
571  pic->pic_ctl.pHEVCPicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_HEVC_P_FRAME;
572  break;
574  pic->pic_ctl.pHEVCPicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_HEVC_B_FRAME;
575  break;
576  default:
577  av_assert0(0 && "invalid picture type");
578  }
579 
580  pic->pic_ctl.pHEVCPicData->slice_pic_parameter_set_id = 0;
581  pic->pic_ctl.pHEVCPicData->PictureOrderCountNumber = hpic->pic_order_cnt;
582 
583  if (base_pic->type == FF_HW_PICTURE_TYPE_P || base_pic->type == FF_HW_PICTURE_TYPE_B) {
584  pd = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*pd));
585  if (!pd)
586  return AVERROR(ENOMEM);
587 
588  ref_list0 = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*ref_list0));
589  if (!ref_list0)
590  return AVERROR(ENOMEM);
591 
592  pic->pic_ctl.pHEVCPicData->List0ReferenceFramesCount = base_pic->nb_refs[0];
593  for (i = 0; i < base_pic->nb_refs[0]; i++) {
594  FFHWBaseEncodePicture *ref = base_pic->refs[0][i];
596 
597  av_assert0(ref && ref->encode_order < base_pic->encode_order);
598  href = ref->codec_priv;
599 
600  ref_list0[i] = idx;
601  pd[idx].ReconstructedPictureResourceIndex = idx;
602  pd[idx].IsRefUsedByCurrentPic = TRUE;
603  pd[idx].PictureOrderCountNumber = href->pic_order_cnt;
604  idx++;
605  }
606  }
607 
608  if (base_pic->type == FF_HW_PICTURE_TYPE_B) {
609  ref_list1 = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*ref_list1));
610  if (!ref_list1)
611  return AVERROR(ENOMEM);
612 
613  pic->pic_ctl.pHEVCPicData->List1ReferenceFramesCount = base_pic->nb_refs[1];
614  for (i = 0; i < base_pic->nb_refs[1]; i++) {
615  FFHWBaseEncodePicture *ref = base_pic->refs[1][i];
617 
618  av_assert0(ref && ref->encode_order < base_pic->encode_order);
619  href = ref->codec_priv;
620 
621  ref_list1[i] = idx;
622  pd[idx].ReconstructedPictureResourceIndex = idx;
623  pd[idx].IsRefUsedByCurrentPic = TRUE;
624  pd[idx].PictureOrderCountNumber = href->pic_order_cnt;
625  idx++;
626  }
627  }
628 
629  pic->pic_ctl.pHEVCPicData->pList0ReferenceFrames = ref_list0;
630  pic->pic_ctl.pHEVCPicData->pList1ReferenceFrames = ref_list1;
631  pic->pic_ctl.pHEVCPicData->ReferenceFramesReconPictureDescriptorsCount = idx;
632  pic->pic_ctl.pHEVCPicData->pReferenceFramesReconPictureDescriptors = pd;
633 
634  return 0;
635 }
636 
639 
640  .d3d12_codec = D3D12_VIDEO_ENCODER_CODEC_HEVC,
641 
642  .flags = FF_HW_FLAG_B_PICTURES |
645 
646  .default_quality = 25,
647 
648  .get_encoder_caps = &d3d12va_encode_hevc_get_encoder_caps,
649 
650  .configure = &d3d12va_encode_hevc_configure,
651 
652  .set_level = &d3d12va_encode_hevc_set_level,
653 
654  .picture_priv_data_size = sizeof(D3D12VAEncodeHEVCPicture),
655 
656  .init_sequence_params = &d3d12va_encode_hevc_init_sequence_params,
657 
658  .init_picture_params = &d3d12va_encode_hevc_init_picture_params,
659 
660  .free_picture_params = &d3d12va_encode_hevc_free_picture_params,
661 
663 };
664 
666 {
668  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
669 
670  ctx->codec = &d3d12va_encode_type_hevc;
671 
672  if (avctx->profile == AV_PROFILE_UNKNOWN)
673  avctx->profile = priv->profile;
674  if (avctx->level == AV_LEVEL_UNKNOWN)
675  avctx->level = priv->level;
676 
677  if (avctx->level != AV_LEVEL_UNKNOWN && avctx->level & ~0xff) {
678  av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
679  "in 8-bit unsigned integer.\n", avctx->level);
680  return AVERROR(EINVAL);
681  }
682 
683  if (priv->qp > 0)
684  ctx->explicit_qp = priv->qp;
685 
686  return ff_d3d12va_encode_init(avctx);
687 }
688 
690 {
691  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
692 
693  ff_cbs_fragment_free(&priv->current_access_unit);
694  ff_cbs_close(&priv->cbc);
695 
696  av_freep(&priv->common.codec_conf.pHEVCConfig);
697  av_freep(&priv->common.gop.pHEVCGroupOfPictures);
698  av_freep(&priv->common.level.pHEVCLevelSetting);
699 
700  return ff_d3d12va_encode_close(avctx);
701 }
702 
703 #define OFFSET(x) offsetof(D3D12VAEncodeHEVCContext, x)
704 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
709 
710  { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
711  OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
712 
713  { "profile", "Set profile (general_profile_idc)",
715  { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
716 
717 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
718  { .i64 = value }, 0, 0, FLAGS, "profile"
719  { PROFILE("main", AV_PROFILE_HEVC_MAIN) },
720  { PROFILE("main10", AV_PROFILE_HEVC_MAIN_10) },
721 #undef PROFILE
722 
723  { "tier", "Set tier (general_tier_flag)",
724  OFFSET(unit_opts.tier), AV_OPT_TYPE_INT,
725  { .i64 = 0 }, 0, 1, FLAGS, "tier" },
726  { "main", NULL, 0, AV_OPT_TYPE_CONST,
727  { .i64 = 0 }, 0, 0, FLAGS, "tier" },
728  { "high", NULL, 0, AV_OPT_TYPE_CONST,
729  { .i64 = 1 }, 0, 0, FLAGS, "tier" },
730 
731  { "level", "Set level (general_level_idc)",
733  { .i64 = AV_LEVEL_UNKNOWN }, AV_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
734 
735 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
736  { .i64 = value }, 0, 0, FLAGS, "level"
737  { LEVEL("1", 30) },
738  { LEVEL("2", 60) },
739  { LEVEL("2.1", 63) },
740  { LEVEL("3", 90) },
741  { LEVEL("3.1", 93) },
742  { LEVEL("4", 120) },
743  { LEVEL("4.1", 123) },
744  { LEVEL("5", 150) },
745  { LEVEL("5.1", 153) },
746  { LEVEL("5.2", 156) },
747  { LEVEL("6", 180) },
748  { LEVEL("6.1", 183) },
749  { LEVEL("6.2", 186) },
750 #undef LEVEL
751 
752  { NULL },
753 };
754 
756  { "b", "0" },
757  { "bf", "2" },
758  { "g", "120" },
759  { "i_qfactor", "1" },
760  { "i_qoffset", "0" },
761  { "b_qfactor", "1" },
762  { "b_qoffset", "0" },
763  { "qmin", "-1" },
764  { "qmax", "-1" },
765  { "refs", "0" },
766  { NULL },
767 };
768 
770  .class_name = "hevc_d3d12va",
771  .item_name = av_default_item_name,
772  .option = d3d12va_encode_hevc_options,
773  .version = LIBAVUTIL_VERSION_INT,
774 };
775 
777  .p.name = "hevc_d3d12va",
778  CODEC_LONG_NAME("D3D12VA hevc encoder"),
779  .p.type = AVMEDIA_TYPE_VIDEO,
780  .p.id = AV_CODEC_ID_HEVC,
781  .priv_data_size = sizeof(D3D12VAEncodeHEVCContext),
784  .close = &d3d12va_encode_hevc_close,
785  .p.priv_class = &d3d12va_encode_hevc_class,
786  .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
788  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
790  .defaults = d3d12va_encode_hevc_defaults,
792  .hw_configs = ff_d3d12va_encode_hw_configs,
793  .p.wrapper_name = "d3d12va",
794 };
cbs.h
CODEC_PIXFMTS
#define CODEC_PIXFMTS(...)
Definition: codec_internal.h:391
level
uint8_t level
Definition: svq3.c:208
av_clip
#define av_clip
Definition: common.h:100
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
d3d12va_encode_hevc_write_sequence_header
static int d3d12va_encode_hevc_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
Definition: d3d12va_encode_hevc.c:207
H265RawProfileTierLevel::general_level_idc
uint8_t general_level_idc
Definition: cbs_h265.h:61
FFHWBaseEncodePicture::priv
void * priv
Definition: hw_base_encode.h:63
d3d12va_encode_hevc_init_sequence_params
static int d3d12va_encode_hevc_init_sequence_params(AVCodecContext *avctx)
Definition: d3d12va_encode_hevc.c:233
FFHWBaseEncodePicture::codec_priv
void * codec_priv
Definition: hw_base_encode.h:65
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3447
AV_CODEC_CAP_HARDWARE
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: codec.h:130
FF_HW_PICTURE_TYPE_P
@ FF_HW_PICTURE_TYPE_P
Definition: hw_base_encode.h:41
int64_t
long long int64_t
Definition: coverity.c:34
D3D12VAEncodeHEVCLevel::d3d12_level
D3D12_VIDEO_ENCODER_LEVELS_HEVC d3d12_level
Definition: d3d12va_encode_hevc.c:60
FFHWBaseEncodeH265::raw_vps
H265RawVPS raw_vps
Definition: hw_base_encode_h265.h:26
AV_PROFILE_HEVC_MAIN
#define AV_PROFILE_HEVC_MAIN
Definition: defs.h:159
pixdesc.h
d3d12va_encode_hevc_class
static const AVClass d3d12va_encode_hevc_class
Definition: d3d12va_encode_hevc.c:769
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:226
AVOption
AVOption.
Definition: opt.h:429
D3D12VAEncodeHEVCPicture::last_idr_frame
int64_t last_idr_frame
Definition: d3d12va_encode_hevc.c:39
d3d12va_encode.h
data
const char data[16]
Definition: mxf.c:149
d3d12va_encode_hevc_add_nal
static int d3d12va_encode_hevc_add_nal(AVCodecContext *avctx, CodedBitstreamFragment *au, void *nal_unit)
Definition: d3d12va_encode_hevc.c:189
d3d12va_encode_hevc_map_cusize
static uint8_t d3d12va_encode_hevc_map_cusize(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE cusize)
Definition: d3d12va_encode_hevc.c:139
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:785
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:34
FFCodec
Definition: codec_internal.h:127
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
d3d12va_encode_hevc_write_access_unit
static int d3d12va_encode_hevc_write_access_unit(AVCodecContext *avctx, char *data, size_t *data_len, CodedBitstreamFragment *au)
Definition: d3d12va_encode_hevc.c:163
FF_HW_PICTURE_TYPE_I
@ FF_HW_PICTURE_TYPE_I
Definition: hw_base_encode.h:40
cbs_h265.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
D3D12VAEncodeHEVCContext
Definition: d3d12va_encode_hevc.c:42
FF_HW_FLAG_B_PICTURE_REFERENCES
@ FF_HW_FLAG_B_PICTURE_REFERENCES
Definition: hw_base_encode.h:55
tf_sess_config.config
config
Definition: tf_sess_config.py:33
H265RawSPS
Definition: cbs_h265.h:245
H265RawPPS
Definition: cbs_h265.h:356
FFHWBaseEncodeContext
Definition: hw_base_encode.h:122
d3d12va_encode_hevc_map_tusize
static uint8_t d3d12va_encode_hevc_map_tusize(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE tusize)
Definition: d3d12va_encode_hevc.c:151
D3D12VAEncodeHEVCContext::current_access_unit
CodedBitstreamFragment current_access_unit
Definition: d3d12va_encode_hevc.c:55
FF_HW_PICTURE_TYPE_B
@ FF_HW_PICTURE_TYPE_B
Definition: hw_base_encode.h:42
ff_d3d12va_encode_hw_configs
const AVCodecHWConfigInternal *const ff_d3d12va_encode_hw_configs[]
Definition: d3d12va_encode.c:36
AVCodecContext::i_quant_factor
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:794
FFCodecDefault
Definition: codec_internal.h:96
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FFHWBaseEncodePicture::type
int type
Definition: hw_base_encode.h:78
fail
#define fail()
Definition: checkasm.h:200
profile_main10
static const D3D12_VIDEO_ENCODER_PROFILE_HEVC profile_main10
Definition: d3d12va_encode_hevc.c:129
FFHWBaseEncodeH265::raw_pps
H265RawPPS raw_pps
Definition: hw_base_encode_h265.h:28
ff_hw_base_encode_init_params_h265
int ff_hw_base_encode_init_params_h265(FFHWBaseEncodeContext *base_ctx, AVCodecContext *avctx, FFHWBaseEncodeH265 *common, FFHWBaseEncodeH265Opts *opts)
Definition: hw_base_encode_h265.c:26
D3D12VAEncodeHEVCContext::qp
int qp
Definition: d3d12va_encode_hevc.c:46
D3D12VA_ENCODE_RC_OPTIONS
#define D3D12VA_ENCODE_RC_OPTIONS
Definition: d3d12va_encode.h:346
D3D12VAEncodeHEVCContext::level
int level
Definition: d3d12va_encode_hevc.c:48
d3d12va_encode_hevc_free_picture_params
static void d3d12va_encode_hevc_free_picture_params(D3D12VAEncodePicture *pic)
Definition: d3d12va_encode_hevc.c:527
d3d12va_encode_hevc_close
static int d3d12va_encode_hevc_close(AVCodecContext *avctx)
Definition: d3d12va_encode_hevc.c:689
D3D12VAEncodeHEVCContext::common
D3D12VAEncodeContext common
Definition: d3d12va_encode_hevc.c:43
FFHWBaseEncodePicture::prev
struct FFHWBaseEncodePicture * prev
Definition: hw_base_encode.h:101
d3d12va_encode_hevc_get_encoder_caps
static int d3d12va_encode_hevc_get_encoder_caps(AVCodecContext *avctx)
Definition: d3d12va_encode_hevc.c:359
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
main
int main
Definition: dovi_rpuenc.c:38
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
D3D12VA_ENCODE_COMMON_OPTIONS
#define D3D12VA_ENCODE_COMMON_OPTIONS
Definition: d3d12va_encode.h:337
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:129
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:142
H265RawVPS::profile_tier_level
H265RawProfileTierLevel profile_tier_level
Definition: cbs_h265.h:195
hevc_config_support_sets
static const D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC hevc_config_support_sets[]
Definition: d3d12va_encode_hevc.c:63
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:144
D3D12VAEncodeHEVCPicture
Definition: d3d12va_encode_hevc.c:37
FLAGS
#define FLAGS
Definition: d3d12va_encode_hevc.c:704
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
D3D12VAEncodeHEVCContext::cbc
CodedBitstreamContext * cbc
Definition: d3d12va_encode_hevc.c:54
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
ctx
AVFormatContext * ctx
Definition: movenc.c:49
PROFILE
#define PROFILE(name, value)
D3D12VAEncodePicture::pic_ctl
D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA pic_ctl
Definition: d3d12va_encode.h:57
CodedBitstreamFragment::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:146
h2645data.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:331
FFHWBaseEncodeContext::b_per_p
int b_per_p
Definition: hw_base_encode.h:189
hw_base_encode_h265.h
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:213
FFHWBaseEncodeH265Opts
Definition: hw_base_encode_h265.h:33
AV_LEVEL_UNKNOWN
#define AV_LEVEL_UNKNOWN
Definition: defs.h:209
d3d12va_encode_hevc_set_level
static int d3d12va_encode_hevc_set_level(AVCodecContext *avctx)
Definition: d3d12va_encode_hevc.c:497
FF_CODEC_RECEIVE_PACKET_CB
#define FF_CODEC_RECEIVE_PACKET_CB(func)
Definition: codec_internal.h:366
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:241
AVD3D12VAFramesContext
This struct is allocated as AVHWFramesContext.hwctx.
Definition: hwcontext_d3d12va.h:172
AV_PIX_FMT_D3D12
@ AV_PIX_FMT_D3D12
Hardware surfaces for Direct3D 12.
Definition: pixfmt.h:440
H265RawProfileTierLevel::general_tier_flag
uint8_t general_tier_flag
Definition: cbs_h265.h:38
D3D12VAEncodeHEVCContext::unit_opts
FFHWBaseEncodeH265Opts unit_opts
Definition: d3d12va_encode_hevc.c:52
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1628
AV_PROFILE_HEVC_MAIN_10
#define AV_PROFILE_HEVC_MAIN_10
Definition: defs.h:160
H265RawNALUnitHeader
Definition: cbs_h265.h:30
D3D12VAEncodeContext::codec_conf
D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION codec_conf
Definition: d3d12va_encode.h:260
D3D12VAEncodeHEVCLevel
Definition: d3d12va_encode_hevc.c:58
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
codec_internal.h
ff_d3d12va_encode_init
int ff_d3d12va_encode_init(AVCodecContext *avctx)
Definition: d3d12va_encode.c:1479
FFHWBaseEncodePicture::nb_refs
int nb_refs[MAX_REFERENCE_LIST_NUM]
Definition: hw_base_encode.h:97
MAX_DPB_SIZE
#define MAX_DPB_SIZE
Definition: hw_base_encode.h:26
D3D12VAEncodeProfile
Definition: d3d12va_encode.h:62
D3D12VAEncodeHEVCContext::units
FFHWBaseEncodeH265 units
Definition: d3d12va_encode_hevc.c:51
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:135
D3D12VAEncodeHEVCContext::profile
int profile
Definition: d3d12va_encode_hevc.c:47
FFHWBaseEncodeH265
Definition: hw_base_encode_h265.h:25
d3d12va_encode_hevc_init
static int d3d12va_encode_hevc_init(AVCodecContext *avctx)
Definition: d3d12va_encode_hevc.c:665
h265_profile_level.h
d3d12va_encode_hevc_options
static const AVOption d3d12va_encode_hevc_options[]
Definition: d3d12va_encode_hevc.c:705
header
static const uint8_t header[24]
Definition: sdr2.c:68
FFHWBaseEncodePicture::encode_order
int64_t encode_order
Definition: hw_base_encode.h:70
ff_hevc_d3d12va_encoder
const FFCodec ff_hevc_d3d12va_encoder
Definition: d3d12va_encode_hevc.c:776
FFHWBaseEncodeH265Opts::cu_qp_delta_enabled_flag
int cu_qp_delta_enabled_flag
Definition: hw_base_encode_h265.h:36
d3d12va_encode_hevc_defaults
static const FFCodecDefault d3d12va_encode_hevc_defaults[]
Definition: d3d12va_encode_hevc.c:755
D3D12VAEncodePicture
Definition: d3d12va_encode.h:42
AVCodecContext::b_quant_factor
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:778
D3D12VAEncodeContext::level
D3D12_VIDEO_ENCODER_LEVEL_SETTING level
Definition: d3d12va_encode.h:266
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
FFHWBaseEncodeH265Opts::nb_slices
int nb_slices
Definition: hw_base_encode_h265.h:41
d3d12va_encode_hevc_init_picture_params
static int d3d12va_encode_hevc_init_picture_params(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic)
Definition: d3d12va_encode_hevc.c:538
common.h
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:228
FF_HW_FLAG_B_PICTURES
@ FF_HW_FLAG_B_PICTURES
Definition: hw_base_encode.h:53
FFHWBaseEncodePicture::refs
struct FFHWBaseEncodePicture * refs[MAX_REFERENCE_LIST_NUM][MAX_PICTURE_REFERENCES]
Definition: hw_base_encode.h:98
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
profile
int profile
Definition: mxfenc.c:2278
AVCodecContext::height
int height
Definition: avcodec.h:592
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
avcodec.h
AVD3D12VAFramesContext::format
DXGI_FORMAT format
DXGI_FORMAT format.
Definition: hwcontext_d3d12va.h:177
FFHWBaseEncodeContext::gop_size
int gop_size
Definition: hw_base_encode.h:184
FFHWBaseEncodePicture
Definition: hw_base_encode.h:61
AVClass::class_name
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:81
AVHWFramesContext::hwctx
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:153
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
D3D12VAEncodeContext
Definition: d3d12va_encode.h:146
d3d12va_encode_type_hevc
static const D3D12VAEncodeType d3d12va_encode_type_hevc
Definition: d3d12va_encode_hevc.c:637
profile_main
static const D3D12_VIDEO_ENCODER_PROFILE_HEVC profile_main
Definition: d3d12va_encode_hevc.c:128
D3D12VAEncodeHEVCLevel::level
int level
Definition: d3d12va_encode_hevc.c:59
OFFSET
#define OFFSET(x)
Definition: d3d12va_encode_hevc.c:703
FFHWBaseEncodeContext::surface_height
int surface_height
Definition: hw_base_encode.h:141
AVCodecContext
main external API structure.
Definition: avcodec.h:431
write_sequence_header
static int write_sequence_header(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic, uint8_t *data, size_t *data_len)
Definition: vulkan_encode_av1.c:1055
FFHWBaseEncodeH265::raw_sps
H265RawSPS raw_sps
Definition: hw_base_encode_h265.h:27
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1618
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:801
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
D3D_PROFILE_DESC
#define D3D_PROFILE_DESC(name)
Definition: d3d12va_encode_hevc.c:131
pps
uint64_t pps
Definition: dovi_rpuenc.c:36
FFHWBaseEncodeContext::input_frames
AVHWFramesContext * input_frames
Definition: hw_base_encode.h:153
AV_CODEC_CAP_DELAY
#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: codec.h:76
FFHWBaseEncodeContext::surface_width
int surface_width
Definition: hw_base_encode.h:140
D3D12VAEncodeContext::gop
D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE gop
Definition: d3d12va_encode.h:264
FF_HW_FLAG_NON_IDR_KEY_PICTURES
@ FF_HW_FLAG_NON_IDR_KEY_PICTURES
Definition: hw_base_encode.h:58
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FF_HW_PICTURE_TYPE_IDR
@ FF_HW_PICTURE_TYPE_IDR
Definition: hw_base_encode.h:39
mem.h
D3D12VAEncodeHEVCPicture::pic_order_cnt
int pic_order_cnt
Definition: d3d12va_encode_hevc.c:38
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
LEVEL
#define LEVEL(name, value)
ff_d3d12va_encode_close
int ff_d3d12va_encode_close(AVCodecContext *avctx)
Definition: d3d12va_encode.c:1608
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
ff_d3d12va_encode_receive_packet
int ff_d3d12va_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: d3d12va_encode.c:1474
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
D3D12VAEncodeType::profiles
const D3D12VAEncodeProfile * profiles
List of supported profiles.
Definition: d3d12va_encode.h:273
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:592
d3d12va_encode_hevc_configure
static int d3d12va_encode_hevc_configure(AVCodecContext *avctx)
Definition: d3d12va_encode_hevc.c:435
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
HW_BASE_ENCODE_COMMON_OPTIONS
#define HW_BASE_ENCODE_COMMON_OPTIONS
Definition: hw_base_encode.h:243
D3D12VAEncodeType
Definition: d3d12va_encode.h:269
d3d12va_encode_hevc_profiles
static const D3D12VAEncodeProfile d3d12va_encode_hevc_profiles[]
Definition: d3d12va_encode_hevc.c:133
hwcontext_d3d12va_internal.h
FFHWBaseEncodePicture::display_order
int64_t display_order
Definition: hw_base_encode.h:69
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
hevc_levels
static const D3D12VAEncodeHEVCLevel hevc_levels[]
Definition: d3d12va_encode_hevc.c:112
MAX_PICTURE_REFERENCES
#define MAX_PICTURE_REFERENCES
Definition: hw_base_encode.h:27