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:
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  H265RawVUI *vui = &sps->vui;
242  D3D12_VIDEO_ENCODER_PROFILE_HEVC profile = D3D12_VIDEO_ENCODER_PROFILE_HEVC_MAIN;
243  D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC level = { 0 };
244  const AVPixFmtDescriptor *desc;
245  uint8_t min_cu_size, max_cu_size, min_tu_size, max_tu_size;
246  HRESULT hr;
247  int err;
248 
249  D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT support = {
250  .NodeIndex = 0,
251  .Codec = D3D12_VIDEO_ENCODER_CODEC_HEVC,
252  .InputFormat = hwctx->format,
253  .RateControl = ctx->rc,
254  .IntraRefresh = D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE,
255  .SubregionFrameEncoding = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME,
256  .ResolutionsListCount = 1,
257  .pResolutionList = &ctx->resolution,
258  .CodecGopSequence = ctx->gop,
259  .MaxReferenceFramesInDPB = MAX_DPB_SIZE - 1,
260  .CodecConfiguration = ctx->codec_conf,
261  .SuggestedProfile.DataSize = sizeof(D3D12_VIDEO_ENCODER_PROFILE_HEVC),
262  .SuggestedProfile.pHEVCProfile = &profile,
263  .SuggestedLevel.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC),
264  .SuggestedLevel.pHEVCLevelSetting = &level,
265  .pResolutionDependentSupport = &ctx->res_limits,
266  };
267 
268  hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_SUPPORT,
269  &support, sizeof(support));
270 
271  if (FAILED(hr)) {
272  av_log(avctx, AV_LOG_ERROR, "Failed to check encoder support(%lx).\n", (long)hr);
273  return AVERROR(EINVAL);
274  }
275 
276  if (!(support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_GENERAL_SUPPORT_OK)) {
277  av_log(avctx, AV_LOG_ERROR, "Driver does not support some request features. %#x\n",
278  support.ValidationFlags);
279  return AVERROR(EINVAL);
280  }
281 
282  if (support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_RECONSTRUCTED_FRAMES_REQUIRE_TEXTURE_ARRAYS) {
283  av_log(avctx, AV_LOG_ERROR, "D3D12 video encode on this device requires texture array support, "
284  "but it's not implemented.\n");
285  return AVERROR_PATCHWELCOME;
286  }
287 
289  av_assert0(desc);
290 
291  min_cu_size = d3d12va_encode_hevc_map_cusize(ctx->codec_conf.pHEVCConfig->MinLumaCodingUnitSize);
292  max_cu_size = d3d12va_encode_hevc_map_cusize(ctx->codec_conf.pHEVCConfig->MaxLumaCodingUnitSize);
293  min_tu_size = d3d12va_encode_hevc_map_tusize(ctx->codec_conf.pHEVCConfig->MinLumaTransformUnitSize);
294  max_tu_size = d3d12va_encode_hevc_map_tusize(ctx->codec_conf.pHEVCConfig->MaxLumaTransformUnitSize);
295 
296  // cu_qp_delta always required to be 1 in https://github.com/microsoft/DirectX-Specs/blob/master/d3d/D3D12VideoEncoding.md
298  priv->unit_opts.nb_slices = 1;
299 
300  err = ff_hw_base_encode_init_params_h265(base_ctx, avctx,
301  &priv->units, &priv->unit_opts);
302  if (err < 0)
303  return err;
304 
306 
307  av_assert0(ctx->res_limits.SubregionBlockPixelsSize % min_cu_size == 0);
308 
309  sps->pic_width_in_luma_samples = FFALIGN(base_ctx->surface_width,
310  ctx->res_limits.SubregionBlockPixelsSize);
311  sps->pic_height_in_luma_samples = FFALIGN(base_ctx->surface_height,
312  ctx->res_limits.SubregionBlockPixelsSize);
313 
314  if (avctx->width != sps->pic_width_in_luma_samples ||
315  avctx->height != sps->pic_height_in_luma_samples) {
316  sps->conformance_window_flag = 1;
317  sps->conf_win_left_offset = 0;
318  sps->conf_win_right_offset =
319  (sps->pic_width_in_luma_samples - avctx->width) >> desc->log2_chroma_w;
320  sps->conf_win_top_offset = 0;
321  sps->conf_win_bottom_offset =
322  (sps->pic_height_in_luma_samples - avctx->height) >> desc->log2_chroma_h;
323  } else {
324  sps->conformance_window_flag = 0;
325  }
326 
327  sps->log2_max_pic_order_cnt_lsb_minus4 = ctx->gop.pHEVCGroupOfPictures->log2_max_pic_order_cnt_lsb_minus4;
328 
329  sps->log2_min_luma_coding_block_size_minus3 = (uint8_t)(av_log2(min_cu_size) - 3);
330  sps->log2_diff_max_min_luma_coding_block_size = (uint8_t)(av_log2(max_cu_size) - av_log2(min_cu_size));
331  sps->log2_min_luma_transform_block_size_minus2 = (uint8_t)(av_log2(min_tu_size) - 2);
332  sps->log2_diff_max_min_luma_transform_block_size = (uint8_t)(av_log2(max_tu_size) - av_log2(min_tu_size));
333 
334  sps->max_transform_hierarchy_depth_inter = ctx->codec_conf.pHEVCConfig->max_transform_hierarchy_depth_inter;
335  sps->max_transform_hierarchy_depth_intra = ctx->codec_conf.pHEVCConfig->max_transform_hierarchy_depth_intra;
336 
337  sps->amp_enabled_flag = !!(ctx->codec_conf.pHEVCConfig->ConfigurationFlags &
338  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_USE_ASYMETRIC_MOTION_PARTITION);
339  sps->sample_adaptive_offset_enabled_flag = !!(ctx->codec_conf.pHEVCConfig->ConfigurationFlags &
340  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_ENABLE_SAO_FILTER);
341 
342  pps->cabac_init_present_flag = 1;
343 
344  pps->init_qp_minus26 = 0;
345 
346  pps->transform_skip_enabled_flag = !!(ctx->codec_conf.pHEVCConfig->ConfigurationFlags &
347  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_ENABLE_TRANSFORM_SKIPPING);
348 
349  pps->pps_slice_chroma_qp_offsets_present_flag = 1;
350 
351  pps->tiles_enabled_flag = 0; // no tiling in D3D12
352 
353  pps->pps_loop_filter_across_slices_enabled_flag = !(ctx->codec_conf.pHEVCConfig->ConfigurationFlags &
354  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_DISABLE_LOOP_FILTER_ACROSS_SLICES);
355 
356  pps->deblocking_filter_control_present_flag = 1;
357 
358  return 0;
359 }
360 
362 {
363  int i;
364  HRESULT hr;
365  uint8_t min_cu_size, max_cu_size;
366  FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
368  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC *config;
369  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC hevc_caps;
370 
371  D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT codec_caps = {
372  .NodeIndex = 0,
373  .Codec = D3D12_VIDEO_ENCODER_CODEC_HEVC,
374  .Profile = ctx->profile->d3d12_profile,
375  .CodecSupportLimits.DataSize = sizeof(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC),
376  };
377 
378  for (i = 0; i < FF_ARRAY_ELEMS(hevc_config_support_sets); i++) {
379  hevc_caps = hevc_config_support_sets[i];
380  codec_caps.CodecSupportLimits.pHEVCSupport = &hevc_caps;
381  hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT,
382  &codec_caps, sizeof(codec_caps));
383  if (SUCCEEDED(hr) && codec_caps.IsSupported)
384  break;
385  }
386 
388  av_log(avctx, AV_LOG_ERROR, "Unsupported codec configuration\n");
389  return AVERROR(EINVAL);
390  }
391 
392  ctx->codec_conf.DataSize = sizeof(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC);
393  ctx->codec_conf.pHEVCConfig = av_mallocz(ctx->codec_conf.DataSize);
394  if (!ctx->codec_conf.pHEVCConfig)
395  return AVERROR(ENOMEM);
396 
397  config = ctx->codec_conf.pHEVCConfig;
398 
399  config->ConfigurationFlags = D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_NONE;
400  config->MinLumaCodingUnitSize = hevc_caps.MinLumaCodingUnitSize;
401  config->MaxLumaCodingUnitSize = hevc_caps.MaxLumaCodingUnitSize;
402  config->MinLumaTransformUnitSize = hevc_caps.MinLumaTransformUnitSize;
403  config->MaxLumaTransformUnitSize = hevc_caps.MaxLumaTransformUnitSize;
404  config->max_transform_hierarchy_depth_inter = hevc_caps.max_transform_hierarchy_depth_inter;
405  config->max_transform_hierarchy_depth_intra = hevc_caps.max_transform_hierarchy_depth_intra;
406 
407  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_ASYMETRIC_MOTION_PARTITION_SUPPORT ||
408  hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_ASYMETRIC_MOTION_PARTITION_REQUIRED)
409  config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_USE_ASYMETRIC_MOTION_PARTITION;
410 
411  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_SAO_FILTER_SUPPORT)
412  config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_ENABLE_SAO_FILTER;
413 
414  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_DISABLING_LOOP_FILTER_ACROSS_SLICES_SUPPORT)
415  config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_DISABLE_LOOP_FILTER_ACROSS_SLICES;
416 
417  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_TRANSFORM_SKIP_SUPPORT)
418  config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_FLAG_ENABLE_TRANSFORM_SKIPPING;
419 
420  if (hevc_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC_FLAG_P_FRAMES_IMPLEMENTED_AS_LOW_DELAY_B_FRAMES)
421  ctx->bi_not_empty = 1;
422 
423  // block sizes
424  min_cu_size = d3d12va_encode_hevc_map_cusize(hevc_caps.MinLumaCodingUnitSize);
425  max_cu_size = d3d12va_encode_hevc_map_cusize(hevc_caps.MaxLumaCodingUnitSize);
426 
427  av_log(avctx, AV_LOG_VERBOSE, "Using CTU size %dx%d, "
428  "min CB size %dx%d.\n", max_cu_size, max_cu_size,
429  min_cu_size, min_cu_size);
430 
431  base_ctx->surface_width = FFALIGN(avctx->width, min_cu_size);
432  base_ctx->surface_height = FFALIGN(avctx->height, min_cu_size);
433 
434  return 0;
435 }
436 
438 {
439  FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
441  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
442  int fixed_qp_idr, fixed_qp_p, fixed_qp_b;
443  int err;
444 
445  err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
446  if (err < 0)
447  return err;
448 
449  // Rate control
450  if (ctx->rc.Mode == D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP) {
451  D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP *cqp_ctl;
452  fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
453  if (avctx->i_quant_factor > 0.0)
454  fixed_qp_idr = av_clip((avctx->i_quant_factor * fixed_qp_p +
455  avctx->i_quant_offset) + 0.5, 1, 51);
456  else
457  fixed_qp_idr = fixed_qp_p;
458  if (avctx->b_quant_factor > 0.0)
459  fixed_qp_b = av_clip((avctx->b_quant_factor * fixed_qp_p +
460  avctx->b_quant_offset) + 0.5, 1, 51);
461  else
462  fixed_qp_b = fixed_qp_p;
463 
464  av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
465  "%d / %d / %d for IDR- / P- / B-frames.\n",
466  fixed_qp_idr, fixed_qp_p, fixed_qp_b);
467 
468  ctx->rc.ConfigParams.DataSize = sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP);
469  cqp_ctl = av_mallocz(ctx->rc.ConfigParams.DataSize);
470  if (!cqp_ctl)
471  return AVERROR(ENOMEM);
472 
473  cqp_ctl->ConstantQP_FullIntracodedFrame = fixed_qp_idr;
474  cqp_ctl->ConstantQP_InterPredictedFrame_PrevRefOnly = fixed_qp_p;
475  cqp_ctl->ConstantQP_InterPredictedFrame_BiDirectionalRef = fixed_qp_b;
476 
477  ctx->rc.ConfigParams.pConfiguration_CQP = cqp_ctl;
478  }
479 
480  // GOP
481  ctx->gop.DataSize = sizeof(D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE_HEVC);
482  ctx->gop.pHEVCGroupOfPictures = av_mallocz(ctx->gop.DataSize);
483  if (!ctx->gop.pHEVCGroupOfPictures)
484  return AVERROR(ENOMEM);
485 
486  ctx->gop.pHEVCGroupOfPictures->GOPLength = base_ctx->gop_size;
487  ctx->gop.pHEVCGroupOfPictures->PPicturePeriod = base_ctx->b_per_p + 1;
488  // Power of 2
489  if (base_ctx->gop_size & base_ctx->gop_size - 1 == 0)
490  ctx->gop.pHEVCGroupOfPictures->log2_max_pic_order_cnt_lsb_minus4 =
491  FFMAX(av_log2(base_ctx->gop_size) - 4, 0);
492  else
493  ctx->gop.pHEVCGroupOfPictures->log2_max_pic_order_cnt_lsb_minus4 =
494  FFMAX(av_log2(base_ctx->gop_size) - 3, 0);
495 
496  return 0;
497 }
498 
500 {
502  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
503  int i;
504 
505  ctx->level.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC);
506  ctx->level.pHEVCLevelSetting = av_mallocz(ctx->level.DataSize);
507  if (!ctx->level.pHEVCLevelSetting)
508  return AVERROR(ENOMEM);
509 
510  for (i = 0; i < FF_ARRAY_ELEMS(hevc_levels); i++) {
511  if (avctx->level == hevc_levels[i].level) {
512  ctx->level.pHEVCLevelSetting->Level = hevc_levels[i].d3d12_level;
513  break;
514  }
515  }
516 
517  if (i == FF_ARRAY_ELEMS(hevc_levels)) {
518  av_log(avctx, AV_LOG_ERROR, "Invalid level %d.\n", avctx->level);
519  return AVERROR(EINVAL);
520  }
521 
522  ctx->level.pHEVCLevelSetting->Tier = priv->units.raw_vps.profile_tier_level.general_tier_flag == 0 ?
523  D3D12_VIDEO_ENCODER_TIER_HEVC_MAIN :
524  D3D12_VIDEO_ENCODER_TIER_HEVC_HIGH;
525 
526  return 0;
527 }
528 
530 {
531  if (!pic->pic_ctl.pHEVCPicData)
532  return;
533 
534  av_freep(&pic->pic_ctl.pHEVCPicData->pList0ReferenceFrames);
535  av_freep(&pic->pic_ctl.pHEVCPicData->pList1ReferenceFrames);
536  av_freep(&pic->pic_ctl.pHEVCPicData->pReferenceFramesReconPictureDescriptors);
537  av_freep(&pic->pic_ctl.pHEVCPicData);
538 }
539 
541  FFHWBaseEncodePicture *base_pic)
542 {
543  D3D12VAEncodePicture *pic = base_pic->priv;
544  D3D12VAEncodeHEVCPicture *hpic = base_pic->codec_priv;
545  FFHWBaseEncodePicture *prev = base_pic->prev;
546  D3D12VAEncodeHEVCPicture *hprev = prev ? prev->codec_priv : NULL;
547  D3D12_VIDEO_ENCODER_REFERENCE_PICTURE_DESCRIPTOR_HEVC *pd = NULL;
548  UINT *ref_list0 = NULL, *ref_list1 = NULL;
549  int i, idx = 0;
550 
551  pic->pic_ctl.DataSize = sizeof(D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA_HEVC);
552  pic->pic_ctl.pHEVCPicData = av_mallocz(pic->pic_ctl.DataSize);
553  if (!pic->pic_ctl.pHEVCPicData)
554  return AVERROR(ENOMEM);
555 
556  if (base_pic->type == FF_HW_PICTURE_TYPE_IDR) {
557  av_assert0(base_pic->display_order == base_pic->encode_order);
558  hpic->last_idr_frame = base_pic->display_order;
559  } else {
560  av_assert0(prev);
561  hpic->last_idr_frame = hprev->last_idr_frame;
562  }
563  hpic->pic_order_cnt = base_pic->display_order - hpic->last_idr_frame;
564 
565  switch(base_pic->type) {
567  pic->pic_ctl.pHEVCPicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_HEVC_IDR_FRAME;
568  break;
570  pic->pic_ctl.pHEVCPicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_HEVC_I_FRAME;
571  break;
573  pic->pic_ctl.pHEVCPicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_HEVC_P_FRAME;
574  break;
576  pic->pic_ctl.pHEVCPicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_HEVC_B_FRAME;
577  break;
578  default:
579  av_assert0(0 && "invalid picture type");
580  }
581 
582  pic->pic_ctl.pHEVCPicData->slice_pic_parameter_set_id = 0;
583  pic->pic_ctl.pHEVCPicData->PictureOrderCountNumber = hpic->pic_order_cnt;
584 
585  if (base_pic->type == FF_HW_PICTURE_TYPE_P || base_pic->type == FF_HW_PICTURE_TYPE_B) {
586  pd = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*pd));
587  if (!pd)
588  return AVERROR(ENOMEM);
589 
590  ref_list0 = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*ref_list0));
591  if (!ref_list0)
592  return AVERROR(ENOMEM);
593 
594  pic->pic_ctl.pHEVCPicData->List0ReferenceFramesCount = base_pic->nb_refs[0];
595  for (i = 0; i < base_pic->nb_refs[0]; i++) {
596  FFHWBaseEncodePicture *ref = base_pic->refs[0][i];
598 
599  av_assert0(ref && ref->encode_order < base_pic->encode_order);
600  href = ref->codec_priv;
601 
602  ref_list0[i] = idx;
603  pd[idx].ReconstructedPictureResourceIndex = idx;
604  pd[idx].IsRefUsedByCurrentPic = TRUE;
605  pd[idx].PictureOrderCountNumber = href->pic_order_cnt;
606  idx++;
607  }
608  }
609 
610  if (base_pic->type == FF_HW_PICTURE_TYPE_B) {
611  ref_list1 = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*ref_list1));
612  if (!ref_list1)
613  return AVERROR(ENOMEM);
614 
615  pic->pic_ctl.pHEVCPicData->List1ReferenceFramesCount = base_pic->nb_refs[1];
616  for (i = 0; i < base_pic->nb_refs[1]; i++) {
617  FFHWBaseEncodePicture *ref = base_pic->refs[1][i];
619 
620  av_assert0(ref && ref->encode_order < base_pic->encode_order);
621  href = ref->codec_priv;
622 
623  ref_list1[i] = idx;
624  pd[idx].ReconstructedPictureResourceIndex = idx;
625  pd[idx].IsRefUsedByCurrentPic = TRUE;
626  pd[idx].PictureOrderCountNumber = href->pic_order_cnt;
627  idx++;
628  }
629  }
630 
631  pic->pic_ctl.pHEVCPicData->pList0ReferenceFrames = ref_list0;
632  pic->pic_ctl.pHEVCPicData->pList1ReferenceFrames = ref_list1;
633  pic->pic_ctl.pHEVCPicData->ReferenceFramesReconPictureDescriptorsCount = idx;
634  pic->pic_ctl.pHEVCPicData->pReferenceFramesReconPictureDescriptors = pd;
635 
636  return 0;
637 }
638 
641 
642  .d3d12_codec = D3D12_VIDEO_ENCODER_CODEC_HEVC,
643 
644  .flags = FF_HW_FLAG_B_PICTURES |
647 
648  .default_quality = 25,
649 
650  .get_encoder_caps = &d3d12va_encode_hevc_get_encoder_caps,
651 
652  .configure = &d3d12va_encode_hevc_configure,
653 
654  .set_level = &d3d12va_encode_hevc_set_level,
655 
656  .picture_priv_data_size = sizeof(D3D12VAEncodeHEVCPicture),
657 
658  .init_sequence_params = &d3d12va_encode_hevc_init_sequence_params,
659 
660  .init_picture_params = &d3d12va_encode_hevc_init_picture_params,
661 
662  .free_picture_params = &d3d12va_encode_hevc_free_picture_params,
663 
664  .write_sequence_header = &d3d12va_encode_hevc_write_sequence_header,
665 };
666 
668 {
670  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
671 
672  ctx->codec = &d3d12va_encode_type_hevc;
673 
674  if (avctx->profile == AV_PROFILE_UNKNOWN)
675  avctx->profile = priv->profile;
676  if (avctx->level == FF_LEVEL_UNKNOWN)
677  avctx->level = priv->level;
678 
679  if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
680  av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
681  "in 8-bit unsigned integer.\n", avctx->level);
682  return AVERROR(EINVAL);
683  }
684 
685  if (priv->qp > 0)
686  ctx->explicit_qp = priv->qp;
687 
688  return ff_d3d12va_encode_init(avctx);
689 }
690 
692 {
693  D3D12VAEncodeHEVCContext *priv = avctx->priv_data;
694 
696  ff_cbs_close(&priv->cbc);
697 
698  av_freep(&priv->common.codec_conf.pHEVCConfig);
699  av_freep(&priv->common.gop.pHEVCGroupOfPictures);
700  av_freep(&priv->common.level.pHEVCLevelSetting);
701 
702  return ff_d3d12va_encode_close(avctx);
703 }
704 
705 #define OFFSET(x) offsetof(D3D12VAEncodeHEVCContext, x)
706 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
710 
711  { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
712  OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
713 
714  { "profile", "Set profile (general_profile_idc)",
716  { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
717 
718 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
719  { .i64 = value }, 0, 0, FLAGS, "profile"
720  { PROFILE("main", AV_PROFILE_HEVC_MAIN) },
721  { PROFILE("main10", AV_PROFILE_HEVC_MAIN_10) },
722 #undef PROFILE
723 
724  { "tier", "Set tier (general_tier_flag)",
725  OFFSET(unit_opts.tier), AV_OPT_TYPE_INT,
726  { .i64 = 0 }, 0, 1, FLAGS, "tier" },
727  { "main", NULL, 0, AV_OPT_TYPE_CONST,
728  { .i64 = 0 }, 0, 0, FLAGS, "tier" },
729  { "high", NULL, 0, AV_OPT_TYPE_CONST,
730  { .i64 = 1 }, 0, 0, FLAGS, "tier" },
731 
732  { "level", "Set level (general_level_idc)",
734  { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
735 
736 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
737  { .i64 = value }, 0, 0, FLAGS, "level"
738  { LEVEL("1", 30) },
739  { LEVEL("2", 60) },
740  { LEVEL("2.1", 63) },
741  { LEVEL("3", 90) },
742  { LEVEL("3.1", 93) },
743  { LEVEL("4", 120) },
744  { LEVEL("4.1", 123) },
745  { LEVEL("5", 150) },
746  { LEVEL("5.1", 153) },
747  { LEVEL("5.2", 156) },
748  { LEVEL("6", 180) },
749  { LEVEL("6.1", 183) },
750  { LEVEL("6.2", 186) },
751 #undef LEVEL
752 
753  { NULL },
754 };
755 
757  { "b", "0" },
758  { "bf", "2" },
759  { "g", "120" },
760  { "i_qfactor", "1" },
761  { "i_qoffset", "0" },
762  { "b_qfactor", "1" },
763  { "b_qoffset", "0" },
764  { "qmin", "-1" },
765  { "qmax", "-1" },
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,
791  .p.pix_fmts = (const enum AVPixelFormat[]) {
794  },
795  .hw_configs = ff_d3d12va_encode_hw_configs,
796  .p.wrapper_name = "d3d12va",
797 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
level
uint8_t level
Definition: svq3.c:205
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:43
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
FF_HW_PICTURE_TYPE_P
@ FF_HW_PICTURE_TYPE_P
Definition: hw_base_encode.h:41
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
ff_cbs_fragment_free
av_cold void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:186
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3244
AV_CODEC_CAP_HARDWARE
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: codec.h:145
ff_cbs_insert_unit_content
int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, void *content_ref)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:783
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
H265RawVUI
Definition: cbs_h265.h:128
pixdesc.h
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition: cbs.c:172
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:219
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:825
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:35
FFCodec
Definition: codec_internal.h:127
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
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
cbs.h
FF_HW_FLAG_B_PICTURE_REFERENCES
@ FF_HW_FLAG_B_PICTURE_REFERENCES
Definition: hw_base_encode.h:55
cbs_h265.h
FF_LEVEL_UNKNOWN
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:1806
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
D3D12VAEncodeHEVCContext
Definition: d3d12va_encode_hevc.c:42
tf_sess_config.config
config
Definition: tf_sess_config.py:33
ff_cbs_close
av_cold void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:142
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_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:834
FFCodecDefault
Definition: codec_internal.h:97
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:193
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:323
FF_HW_FLAG_B_PICTURES
@ FF_HW_FLAG_B_PICTURES
Definition: hw_base_encode.h:53
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:529
d3d12va_encode_hevc_close
static int d3d12va_encode_hevc_close(AVCodecContext *avctx)
Definition: d3d12va_encode_hevc.c:691
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:361
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
main
int main
Definition: dovi_rpuenc.c:37
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:122
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:135
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:159
D3D12VAEncodeHEVCPicture
Definition: d3d12va_encode_hevc.c:37
FLAGS
#define FLAGS
Definition: d3d12va_encode_hevc.c:706
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
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:230
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FF_HW_PICTURE_TYPE_IDR
@ FF_HW_PICTURE_TYPE_IDR
Definition: hw_base_encode.h:39
PROFILE
#define PROFILE(name, value)
D3D12VAEncodePicture::pic_ctl
D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA pic_ctl
Definition: d3d12va_encode.h:55
CodedBitstreamFragment::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:139
h2645data.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
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:75
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:210
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
FFHWBaseEncodeH265Opts
Definition: hw_base_encode_h265.h:33
d3d12va_encode_hevc_set_level
static int d3d12va_encode_hevc_set_level(AVCodecContext *avctx)
Definition: d3d12va_encode_hevc.c:499
FF_CODEC_RECEIVE_PACKET_CB
#define FF_CODEC_RECEIVE_PACKET_CB(func)
Definition: codec_internal.h:326
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AVD3D12VAFramesContext
This struct is allocated as AVHWFramesContext.hwctx.
Definition: hwcontext_d3d12va.h:126
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:1802
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:243
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:1392
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:60
FF_HW_FLAG_NON_IDR_KEY_PICTURES
@ FF_HW_FLAG_NON_IDR_KEY_PICTURES
Definition: hw_base_encode.h:58
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:128
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:667
h265_profile_level.h
d3d12va_encode_hevc_options
static const AVOption d3d12va_encode_hevc_options[]
Definition: d3d12va_encode_hevc.c:707
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:756
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:818
D3D12VAEncodeContext::level
D3D12_VIDEO_ENCODER_LEVEL_SETTING level
Definition: d3d12va_encode.h:249
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:540
common.h
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:228
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:194
profile
int profile
Definition: mxfenc.c:2233
AVCodecContext::height
int height
Definition: avcodec.h:632
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
ff_cbs_write_fragment_data
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:409
avcodec.h
AVD3D12VAFramesContext::format
DXGI_FORMAT format
DXGI_FORMAT format.
Definition: hwcontext_d3d12va.h:131
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:80
AVHWFramesContext::hwctx
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:150
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
D3D12VAEncodeContext
Definition: d3d12va_encode.h:144
d3d12va_encode_type_hevc
static const D3D12VAEncodeType d3d12va_encode_type_hevc
Definition: d3d12va_encode_hevc.c:639
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:705
FFHWBaseEncodeContext::surface_height
int surface_height
Definition: hw_base_encode.h:141
AVCodecContext
main external API structure.
Definition: avcodec.h:451
FFHWBaseEncodeH265::raw_sps
H265RawSPS raw_sps
Definition: hw_base_encode_h265.h:27
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
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:1658
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:841
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:35
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:247
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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
ff_cbs_init
av_cold 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:90
LEVEL
#define LEVEL(name, value)
ff_d3d12va_encode_close
int ff_d3d12va_encode_close(AVCodecContext *avctx)
Definition: d3d12va_encode.c:1511
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:1387
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
D3D12VAEncodeType::profiles
const D3D12VAEncodeProfile * profiles
List of supported profiles.
Definition: d3d12va_encode.h:256
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:632
FF_HW_PICTURE_TYPE_B
@ FF_HW_PICTURE_TYPE_B
Definition: hw_base_encode.h:42
d3d12va_encode_hevc_configure
static int d3d12va_encode_hevc_configure(AVCodecContext *avctx)
Definition: d3d12va_encode_hevc.c:437
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:252
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
FF_HW_PICTURE_TYPE_I
@ FF_HW_PICTURE_TYPE_I
Definition: hw_base_encode.h:40
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