FFmpeg
nvenc.c
Go to the documentation of this file.
1 /*
2  * H.264/HEVC/AV1 hardware encoding using nvidia nvenc
3  * Copyright (c) 2016 Timo Rothenpieler <timo@rothenpieler.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 #include "config_components.h"
24 
25 #include "nvenc.h"
26 #include "hevc/sei.h"
27 #if CONFIG_AV1_NVENC_ENCODER
28 #include "av1.h"
29 #endif
30 
32 #include "libavutil/hwcontext.h"
33 #include "libavutil/cuda_check.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/mathematics.h"
38 #include "atsc_a53.h"
39 #include "codec_desc.h"
40 #include "encode.h"
41 #include "internal.h"
42 #include "packet_internal.h"
43 
44 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
45 
46 #define NVENC_CAP 0x30
47 
48 #ifndef NVENC_NO_DEPRECATED_RC
49 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
50  rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
51  rc == NV_ENC_PARAMS_RC_CBR_HQ)
52 #else
53 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR)
54 #endif
55 
61  AV_PIX_FMT_P016, // Truncated to 10bits
62  AV_PIX_FMT_YUV444P16, // Truncated to 10bits
70  AV_PIX_FMT_GBRP16, // Truncated to 10bits
72 #if CONFIG_D3D11VA
74 #endif
76 };
77 
79  HW_CONFIG_ENCODER_FRAMES(CUDA, CUDA),
81 #if CONFIG_D3D11VA
82  HW_CONFIG_ENCODER_FRAMES(D3D11, D3D11VA),
84 #endif
85  NULL,
86 };
87 
88 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
89  pix_fmt == AV_PIX_FMT_P016 || \
90  pix_fmt == AV_PIX_FMT_YUV444P16 || \
91  pix_fmt == AV_PIX_FMT_X2RGB10 || \
92  pix_fmt == AV_PIX_FMT_X2BGR10 || \
93  pix_fmt == AV_PIX_FMT_GBRP16)
94 
95 #define IS_RGB(pix_fmt) (pix_fmt == AV_PIX_FMT_0RGB32 || \
96  pix_fmt == AV_PIX_FMT_RGB32 || \
97  pix_fmt == AV_PIX_FMT_0BGR32 || \
98  pix_fmt == AV_PIX_FMT_BGR32 || \
99  pix_fmt == AV_PIX_FMT_X2RGB10 || \
100  pix_fmt == AV_PIX_FMT_X2BGR10)
101 
102 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
103  pix_fmt == AV_PIX_FMT_YUV444P16 || \
104  pix_fmt == AV_PIX_FMT_GBRP || \
105  pix_fmt == AV_PIX_FMT_GBRP16 || \
106  (ctx->rgb_mode == NVENC_RGB_MODE_444 && IS_RGB(pix_fmt)))
107 
108 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
109  pix_fmt == AV_PIX_FMT_GBRP16)
110 
111 static const struct {
112  NVENCSTATUS nverr;
113  int averr;
114  const char *desc;
115 } nvenc_errors[] = {
116  { NV_ENC_SUCCESS, 0, "success" },
117  { NV_ENC_ERR_NO_ENCODE_DEVICE, AVERROR(ENOENT), "no encode device" },
118  { NV_ENC_ERR_UNSUPPORTED_DEVICE, AVERROR(ENOSYS), "unsupported device" },
119  { NV_ENC_ERR_INVALID_ENCODERDEVICE, AVERROR(EINVAL), "invalid encoder device" },
120  { NV_ENC_ERR_INVALID_DEVICE, AVERROR(EINVAL), "invalid device" },
121  { NV_ENC_ERR_DEVICE_NOT_EXIST, AVERROR(EIO), "device does not exist" },
122  { NV_ENC_ERR_INVALID_PTR, AVERROR(EFAULT), "invalid ptr" },
123  { NV_ENC_ERR_INVALID_EVENT, AVERROR(EINVAL), "invalid event" },
124  { NV_ENC_ERR_INVALID_PARAM, AVERROR(EINVAL), "invalid param" },
125  { NV_ENC_ERR_INVALID_CALL, AVERROR(EINVAL), "invalid call" },
126  { NV_ENC_ERR_OUT_OF_MEMORY, AVERROR(ENOMEM), "out of memory" },
127  { NV_ENC_ERR_ENCODER_NOT_INITIALIZED, AVERROR(EINVAL), "encoder not initialized" },
128  { NV_ENC_ERR_UNSUPPORTED_PARAM, AVERROR(ENOSYS), "unsupported param" },
129  { NV_ENC_ERR_LOCK_BUSY, AVERROR(EAGAIN), "lock busy" },
130  { NV_ENC_ERR_NOT_ENOUGH_BUFFER, AVERROR_BUFFER_TOO_SMALL, "not enough buffer"},
131  { NV_ENC_ERR_INVALID_VERSION, AVERROR(EINVAL), "invalid version" },
132  { NV_ENC_ERR_MAP_FAILED, AVERROR(EIO), "map failed" },
133  { NV_ENC_ERR_NEED_MORE_INPUT, AVERROR(EAGAIN), "need more input" },
134  { NV_ENC_ERR_ENCODER_BUSY, AVERROR(EAGAIN), "encoder busy" },
135  { NV_ENC_ERR_EVENT_NOT_REGISTERD, AVERROR(EBADF), "event not registered" },
136  { NV_ENC_ERR_GENERIC, AVERROR_UNKNOWN, "generic error" },
137  { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, AVERROR(EINVAL), "incompatible client key" },
138  { NV_ENC_ERR_UNIMPLEMENTED, AVERROR(ENOSYS), "unimplemented" },
139  { NV_ENC_ERR_RESOURCE_REGISTER_FAILED, AVERROR(EIO), "resource register failed" },
140  { NV_ENC_ERR_RESOURCE_NOT_REGISTERED, AVERROR(EBADF), "resource not registered" },
141  { NV_ENC_ERR_RESOURCE_NOT_MAPPED, AVERROR(EBADF), "resource not mapped" },
142 };
143 
144 static int nvenc_map_error(NVENCSTATUS err, const char **desc)
145 {
146  int i;
147  for (i = 0; i < FF_ARRAY_ELEMS(nvenc_errors); i++) {
148  if (nvenc_errors[i].nverr == err) {
149  if (desc)
150  *desc = nvenc_errors[i].desc;
151  return nvenc_errors[i].averr;
152  }
153  }
154  if (desc)
155  *desc = "unknown error";
156  return AVERROR_UNKNOWN;
157 }
158 
159 static int nvenc_print_error(AVCodecContext *avctx, NVENCSTATUS err,
160  const char *error_string)
161 {
162  const char *desc;
163  const char *details = "(no details)";
164  int ret = nvenc_map_error(err, &desc);
165 
166 #ifdef NVENC_HAVE_GETLASTERRORSTRING
167  NvencContext *ctx = avctx->priv_data;
168  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
169 
170  if (p_nvenc && ctx->nvencoder)
171  details = p_nvenc->nvEncGetLastErrorString(ctx->nvencoder);
172 #endif
173 
174  av_log(avctx, AV_LOG_ERROR, "%s: %s (%d): %s\n", error_string, desc, err, details);
175 
176  return ret;
177 }
178 
179 typedef struct GUIDTuple {
180  const GUID guid;
181  int flags;
182 } GUIDTuple;
183 
184 #define PRESET_ALIAS(alias, name, ...) \
185  [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
186 
187 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
188 
190 {
191  GUIDTuple presets[] = {
192 #ifdef NVENC_HAVE_NEW_PRESETS
193  PRESET(P1),
194  PRESET(P2),
195  PRESET(P3),
196  PRESET(P4),
197  PRESET(P5),
198  PRESET(P6),
199  PRESET(P7),
201  PRESET_ALIAS(MEDIUM, P4, NVENC_ONE_PASS),
203  // Compat aliases
208  PRESET_ALIAS(LOW_LATENCY_DEFAULT, P4, NVENC_DEPRECATED_PRESET | NVENC_LOWLATENCY),
213 #else
214  PRESET(DEFAULT),
215  PRESET(HP),
216  PRESET(HQ),
217  PRESET(BD),
218  PRESET_ALIAS(SLOW, HQ, NVENC_TWO_PASSES),
219  PRESET_ALIAS(MEDIUM, HQ, NVENC_ONE_PASS),
221  PRESET(LOW_LATENCY_DEFAULT, NVENC_LOWLATENCY),
222  PRESET(LOW_LATENCY_HP, NVENC_LOWLATENCY),
223  PRESET(LOW_LATENCY_HQ, NVENC_LOWLATENCY),
224  PRESET(LOSSLESS_DEFAULT, NVENC_LOSSLESS),
225  PRESET(LOSSLESS_HP, NVENC_LOSSLESS),
226 #endif
227  };
228 
229  GUIDTuple *t = &presets[ctx->preset];
230 
231  ctx->init_encode_params.presetGUID = t->guid;
232  ctx->flags = t->flags;
233 
234 #ifdef NVENC_HAVE_NEW_PRESETS
235  if (ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
237 #endif
238 }
239 
240 #undef PRESET
241 #undef PRESET_ALIAS
242 
244 {
245 #if NVENCAPI_CHECK_VERSION(12, 3)
246  const char *minver = "(unknown)";
247 #elif NVENCAPI_CHECK_VERSION(12, 2)
248 # if defined(_WIN32) || defined(__CYGWIN__)
249  const char *minver = "551.76";
250 # else
251  const char *minver = "550.54.14";
252 # endif
253 #elif NVENCAPI_CHECK_VERSION(12, 1)
254 # if defined(_WIN32) || defined(__CYGWIN__)
255  const char *minver = "531.61";
256 # else
257  const char *minver = "530.41.03";
258 # endif
259 #elif NVENCAPI_CHECK_VERSION(12, 0)
260 # if defined(_WIN32) || defined(__CYGWIN__)
261  const char *minver = "522.25";
262 # else
263  const char *minver = "520.56.06";
264 # endif
265 #elif NVENCAPI_CHECK_VERSION(11, 1)
266 # if defined(_WIN32) || defined(__CYGWIN__)
267  const char *minver = "471.41";
268 # else
269  const char *minver = "470.57.02";
270 # endif
271 #elif NVENCAPI_CHECK_VERSION(11, 0)
272 # if defined(_WIN32) || defined(__CYGWIN__)
273  const char *minver = "456.71";
274 # else
275  const char *minver = "455.28";
276 # endif
277 #elif NVENCAPI_CHECK_VERSION(10, 0)
278 # if defined(_WIN32) || defined(__CYGWIN__)
279  const char *minver = "450.51";
280 # else
281  const char *minver = "445.87";
282 # endif
283 #elif NVENCAPI_CHECK_VERSION(9, 1)
284 # if defined(_WIN32) || defined(__CYGWIN__)
285  const char *minver = "436.15";
286 # else
287  const char *minver = "435.21";
288 # endif
289 #elif NVENCAPI_CHECK_VERSION(9, 0)
290 # if defined(_WIN32) || defined(__CYGWIN__)
291  const char *minver = "418.81";
292 # else
293  const char *minver = "418.30";
294 # endif
295 #elif NVENCAPI_CHECK_VERSION(8, 2)
296 # if defined(_WIN32) || defined(__CYGWIN__)
297  const char *minver = "397.93";
298 # else
299  const char *minver = "396.24";
300 #endif
301 #elif NVENCAPI_CHECK_VERSION(8, 1)
302 # if defined(_WIN32) || defined(__CYGWIN__)
303  const char *minver = "390.77";
304 # else
305  const char *minver = "390.25";
306 # endif
307 #else
308 # if defined(_WIN32) || defined(__CYGWIN__)
309  const char *minver = "378.66";
310 # else
311  const char *minver = "378.13";
312 # endif
313 #endif
314  av_log(avctx, level, "The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
315 }
316 
318 {
319  NvencContext *ctx = avctx->priv_data;
320  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
321  NVENCSTATUS err;
322  uint32_t nvenc_max_ver;
323  int ret;
324 
325  ret = cuda_load_functions(&dl_fn->cuda_dl, avctx);
326  if (ret < 0)
327  return ret;
328 
329  ret = nvenc_load_functions(&dl_fn->nvenc_dl, avctx);
330  if (ret < 0) {
332  return ret;
333  }
334 
335  err = dl_fn->nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
336  if (err != NV_ENC_SUCCESS)
337  return nvenc_print_error(avctx, err, "Failed to query nvenc max version");
338 
339  av_log(avctx, AV_LOG_VERBOSE, "Loaded Nvenc version %d.%d\n", nvenc_max_ver >> 4, nvenc_max_ver & 0xf);
340 
341  if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
342  av_log(avctx, AV_LOG_ERROR, "Driver does not support the required nvenc API version. "
343  "Required: %d.%d Found: %d.%d\n",
344  NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
345  nvenc_max_ver >> 4, nvenc_max_ver & 0xf);
347  return AVERROR(ENOSYS);
348  }
349 
350  dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
351 
352  err = dl_fn->nvenc_dl->NvEncodeAPICreateInstance(&dl_fn->nvenc_funcs);
353  if (err != NV_ENC_SUCCESS)
354  return nvenc_print_error(avctx, err, "Failed to create nvenc instance");
355 
356  av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
357 
358  return 0;
359 }
360 
362 {
363  NvencContext *ctx = avctx->priv_data;
364  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
365 
366  if (ctx->d3d11_device)
367  return 0;
368 
369  return CHECK_CU(dl_fn->cuda_dl->cuCtxPushCurrent(ctx->cu_context));
370 }
371 
373 {
374  NvencContext *ctx = avctx->priv_data;
375  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
376  CUcontext dummy;
377 
378  if (ctx->d3d11_device)
379  return 0;
380 
381  return CHECK_CU(dl_fn->cuda_dl->cuCtxPopCurrent(&dummy));
382 }
383 
385 {
386  NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
387  NvencContext *ctx = avctx->priv_data;
388  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
389  NVENCSTATUS ret;
390 
391  params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
392  params.apiVersion = NVENCAPI_VERSION;
393  if (ctx->d3d11_device) {
394  params.device = ctx->d3d11_device;
395  params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
396  } else {
397  params.device = ctx->cu_context;
398  params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
399  }
400 
401  ret = p_nvenc->nvEncOpenEncodeSessionEx(&params, &ctx->nvencoder);
402  if (ret != NV_ENC_SUCCESS) {
403  ctx->nvencoder = NULL;
404  return nvenc_print_error(avctx, ret, "OpenEncodeSessionEx failed");
405  }
406 
407  return 0;
408 }
409 
411 {
412  NvencContext *ctx = avctx->priv_data;
413  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
414  int i, ret, count = 0;
415  GUID *guids = NULL;
416 
417  ret = p_nvenc->nvEncGetEncodeGUIDCount(ctx->nvencoder, &count);
418 
419  if (ret != NV_ENC_SUCCESS || !count)
420  return AVERROR(ENOSYS);
421 
422  guids = av_malloc(count * sizeof(GUID));
423  if (!guids)
424  return AVERROR(ENOMEM);
425 
426  ret = p_nvenc->nvEncGetEncodeGUIDs(ctx->nvencoder, guids, count, &count);
427  if (ret != NV_ENC_SUCCESS) {
428  ret = AVERROR(ENOSYS);
429  goto fail;
430  }
431 
432  ret = AVERROR(ENOSYS);
433  for (i = 0; i < count; i++) {
434  if (!memcmp(&guids[i], &ctx->init_encode_params.encodeGUID, sizeof(*guids))) {
435  ret = 0;
436  break;
437  }
438  }
439 
440 fail:
441  av_free(guids);
442 
443  return ret;
444 }
445 
446 static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
447 {
448  NvencContext *ctx = avctx->priv_data;
449  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
450  NV_ENC_CAPS_PARAM params = { 0 };
451  int ret, val = 0;
452 
453  params.version = NV_ENC_CAPS_PARAM_VER;
454  params.capsToQuery = cap;
455 
456  ret = p_nvenc->nvEncGetEncodeCaps(ctx->nvencoder, ctx->init_encode_params.encodeGUID, &params, &val);
457 
458  if (ret == NV_ENC_SUCCESS)
459  return val;
460  return 0;
461 }
462 
464 {
465  NvencContext *ctx = avctx->priv_data;
466  int tmp, ret;
467 
469  if (ret < 0) {
470  av_log(avctx, AV_LOG_WARNING, "Codec not supported\n");
471  return ret;
472  }
473 
474  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_YUV444_ENCODE);
475  if (IS_YUV444(ctx->data_pix_fmt) && ret <= 0) {
476  av_log(avctx, AV_LOG_WARNING, "YUV444P not supported\n");
477  return AVERROR(ENOSYS);
478  }
479 
480  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE);
481  if (ctx->flags & NVENC_LOSSLESS && ret <= 0) {
482  av_log(avctx, AV_LOG_WARNING, "Lossless encoding not supported\n");
483  return AVERROR(ENOSYS);
484  }
485 
486  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_WIDTH_MAX);
487  if (ret < avctx->width) {
488  av_log(avctx, AV_LOG_WARNING, "Width %d exceeds %d\n",
489  avctx->width, ret);
490  return AVERROR(ENOSYS);
491  }
492 
493  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_HEIGHT_MAX);
494  if (ret < avctx->height) {
495  av_log(avctx, AV_LOG_WARNING, "Height %d exceeds %d\n",
496  avctx->height, ret);
497  return AVERROR(ENOSYS);
498  }
499 
500  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_NUM_MAX_BFRAMES);
501  if (ret < avctx->max_b_frames) {
502  av_log(avctx, AV_LOG_WARNING, "Max B-frames %d exceed %d\n",
503  avctx->max_b_frames, ret);
504 
505  return AVERROR(ENOSYS);
506  }
507 
508  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_FIELD_ENCODING);
509  if (ret < 1 && avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
510  av_log(avctx, AV_LOG_WARNING,
511  "Interlaced encoding is not supported. Supported level: %d\n",
512  ret);
513  return AVERROR(ENOSYS);
514  }
515 
516  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_10BIT_ENCODE);
517  if ((IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) && ret <= 0) {
518  av_log(avctx, AV_LOG_WARNING, "10 bit encode not supported\n");
519  return AVERROR(ENOSYS);
520  }
521 
522  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOOKAHEAD);
523  if (ctx->rc_lookahead > 0 && ret <= 0) {
524  av_log(avctx, AV_LOG_WARNING, "RC lookahead not supported\n");
525  return AVERROR(ENOSYS);
526  }
527 
528  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ);
529  if (ctx->temporal_aq > 0 && ret <= 0) {
530  av_log(avctx, AV_LOG_WARNING, "Temporal AQ not supported\n");
531  return AVERROR(ENOSYS);
532  }
533 
534  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION);
535  if (ctx->weighted_pred > 0 && ret <= 0) {
536  av_log (avctx, AV_LOG_WARNING, "Weighted Prediction not supported\n");
537  return AVERROR(ENOSYS);
538  }
539 
540  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_CABAC);
541  if (ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC && ret <= 0) {
542  av_log(avctx, AV_LOG_WARNING, "CABAC entropy coding not supported\n");
543  return AVERROR(ENOSYS);
544  }
545 
546 #ifdef NVENC_HAVE_BFRAME_REF_MODE
547  tmp = (ctx->b_ref_mode >= 0) ? ctx->b_ref_mode : NV_ENC_BFRAME_REF_MODE_DISABLED;
548  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE);
549  if (tmp == NV_ENC_BFRAME_REF_MODE_EACH && ret != 1 && ret != 3) {
550  av_log(avctx, AV_LOG_WARNING, "Each B frame as reference is not supported\n");
551  return AVERROR(ENOSYS);
552  } else if (tmp != NV_ENC_BFRAME_REF_MODE_DISABLED && ret == 0) {
553  av_log(avctx, AV_LOG_WARNING, "B frames as references are not supported\n");
554  return AVERROR(ENOSYS);
555  }
556 #else
557  tmp = (ctx->b_ref_mode >= 0) ? ctx->b_ref_mode : 0;
558  if (tmp > 0) {
559  av_log(avctx, AV_LOG_WARNING, "B frames as references need SDK 8.1 at build time\n");
560  return AVERROR(ENOSYS);
561  }
562 #endif
563 
564 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
565  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES);
566  if(avctx->refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT && ret <= 0) {
567  av_log(avctx, AV_LOG_WARNING, "Multiple reference frames are not supported by the device\n");
568  return AVERROR(ENOSYS);
569  }
570 #else
571  if(avctx->refs != 0) {
572  av_log(avctx, AV_LOG_WARNING, "Multiple reference frames need SDK 9.1 at build time\n");
573  return AVERROR(ENOSYS);
574  }
575 #endif
576 
577 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
578  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SINGLE_SLICE_INTRA_REFRESH);
579  if(ctx->single_slice_intra_refresh && ret <= 0) {
580  av_log(avctx, AV_LOG_WARNING, "Single slice intra refresh not supported by the device\n");
581  return AVERROR(ENOSYS);
582  }
583 #else
584  if(ctx->single_slice_intra_refresh) {
585  av_log(avctx, AV_LOG_WARNING, "Single slice intra refresh needs SDK 11.1 at build time\n");
586  return AVERROR(ENOSYS);
587  }
588 #endif
589 
590  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_INTRA_REFRESH);
591  if((ctx->intra_refresh || ctx->single_slice_intra_refresh) && ret <= 0) {
592  av_log(avctx, AV_LOG_WARNING, "Intra refresh not supported by the device\n");
593  return AVERROR(ENOSYS);
594  }
595 
596 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
597  if (ctx->constrained_encoding && avctx->codec->id == AV_CODEC_ID_HEVC) {
598  av_log(avctx, AV_LOG_WARNING, "HEVC constrained encoding needs SDK 10.0 at build time\n");
599  return AVERROR(ENOSYS);
600  }
601 #endif
602 
603  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING);
604  if(ctx->constrained_encoding && ret <= 0) {
605  av_log(avctx, AV_LOG_WARNING, "Constrained encoding not supported by the device\n");
606  return AVERROR(ENOSYS);
607  }
608 
609 #ifdef NVENC_HAVE_TEMPORAL_FILTER
610  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_TEMPORAL_FILTER);
611  if(ctx->tf_level > 0 && ret <= 0) {
612  av_log(avctx, AV_LOG_WARNING, "Temporal filtering not supported by the device\n");
613  return AVERROR(ENOSYS);
614  }
615 #endif
616 
617 #ifdef NVENC_HAVE_LOOKAHEAD_LEVEL
618  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOOKAHEAD_LEVEL);
619  if(ctx->rc_lookahead > 0 && ctx->lookahead_level > 0 &&
620  ctx->lookahead_level != NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT &&
621  ctx->lookahead_level > ret)
622  {
623  av_log(avctx, AV_LOG_WARNING, "Lookahead level not supported. Maximum level: %d\n", ret);
624  return AVERROR(ENOSYS);
625  }
626 #endif
627 
628 #ifdef NVENC_HAVE_UNIDIR_B
629  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_UNIDIRECTIONAL_B);
630  if(ctx->unidir_b && ret <= 0) {
631  av_log(avctx, AV_LOG_WARNING, "Unidirectional B-Frames not supported by the device\n");
632  return AVERROR(ENOSYS);
633  }
634 #endif
635 
636  ctx->support_dyn_bitrate = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
637 
638  return 0;
639 }
640 
641 static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
642 {
643  NvencContext *ctx = avctx->priv_data;
644  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
645  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
646  char name[128] = { 0};
647  int major, minor, ret;
648  CUdevice cu_device;
649  int loglevel = AV_LOG_VERBOSE;
650 
651  if (ctx->device == LIST_DEVICES)
652  loglevel = AV_LOG_INFO;
653 
654  ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceGet(&cu_device, idx));
655  if (ret < 0)
656  return ret;
657 
658  ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceGetName(name, sizeof(name), cu_device));
659  if (ret < 0)
660  return ret;
661 
662  ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceComputeCapability(&major, &minor, cu_device));
663  if (ret < 0)
664  return ret;
665 
666  av_log(avctx, loglevel, "[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx, name, major, minor);
667  if (((major << 4) | minor) < NVENC_CAP) {
668  av_log(avctx, loglevel, "does not support NVENC\n");
669  goto fail;
670  }
671 
672  if (ctx->device != idx && ctx->device != ANY_DEVICE)
673  return -1;
674 
675  ret = CHECK_CU(dl_fn->cuda_dl->cuCtxCreate(&ctx->cu_context_internal, 0, cu_device));
676  if (ret < 0)
677  goto fail;
678 
679  ctx->cu_context = ctx->cu_context_internal;
680  ctx->cu_stream = NULL;
681 
682  if ((ret = nvenc_pop_context(avctx)) < 0)
683  goto fail2;
684 
685  if ((ret = nvenc_open_session(avctx)) < 0)
686  goto fail2;
687 
688  if ((ret = nvenc_check_capabilities(avctx)) < 0)
689  goto fail3;
690 
691  av_log(avctx, loglevel, "supports NVENC\n");
692 
693  dl_fn->nvenc_device_count++;
694 
695  if (ctx->device == idx || ctx->device == ANY_DEVICE)
696  return 0;
697 
698 fail3:
699  if ((ret = nvenc_push_context(avctx)) < 0)
700  return ret;
701 
702  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
703  ctx->nvencoder = NULL;
704 
705  if ((ret = nvenc_pop_context(avctx)) < 0)
706  return ret;
707 
708 fail2:
709  CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal));
710  ctx->cu_context_internal = NULL;
711 
712 fail:
713  return AVERROR(ENOSYS);
714 }
715 
717 {
718  NvencContext *ctx = avctx->priv_data;
719  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
720 
721  switch (avctx->codec->id) {
722  case AV_CODEC_ID_H264:
723  ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
724  break;
725  case AV_CODEC_ID_HEVC:
726  ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
727  break;
728 #if CONFIG_AV1_NVENC_ENCODER
729  case AV_CODEC_ID_AV1:
730  ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID;
731  break;
732 #endif
733  default:
734  return AVERROR_BUG;
735  }
736 
738 
740  av_log(avctx, AV_LOG_WARNING, "The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
741 
742  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11 || avctx->hw_frames_ctx || avctx->hw_device_ctx) {
743  AVHWFramesContext *frames_ctx;
744  AVHWDeviceContext *hwdev_ctx;
745  AVCUDADeviceContext *cuda_device_hwctx = NULL;
746 #if CONFIG_D3D11VA
747  AVD3D11VADeviceContext *d3d11_device_hwctx = NULL;
748 #endif
749  int ret;
750 
751  if (avctx->hw_frames_ctx) {
752  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
753  if (frames_ctx->format == AV_PIX_FMT_CUDA)
754  cuda_device_hwctx = frames_ctx->device_ctx->hwctx;
755 #if CONFIG_D3D11VA
756  else if (frames_ctx->format == AV_PIX_FMT_D3D11)
757  d3d11_device_hwctx = frames_ctx->device_ctx->hwctx;
758 #endif
759  else
760  return AVERROR(EINVAL);
761  } else if (avctx->hw_device_ctx) {
762  hwdev_ctx = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
763  if (hwdev_ctx->type == AV_HWDEVICE_TYPE_CUDA)
764  cuda_device_hwctx = hwdev_ctx->hwctx;
765 #if CONFIG_D3D11VA
766  else if (hwdev_ctx->type == AV_HWDEVICE_TYPE_D3D11VA)
767  d3d11_device_hwctx = hwdev_ctx->hwctx;
768 #endif
769  else
770  return AVERROR(EINVAL);
771  } else {
772  return AVERROR(EINVAL);
773  }
774 
775  if (cuda_device_hwctx) {
776  ctx->cu_context = cuda_device_hwctx->cuda_ctx;
777  ctx->cu_stream = cuda_device_hwctx->stream;
778  }
779 #if CONFIG_D3D11VA
780  else if (d3d11_device_hwctx) {
781  ctx->d3d11_device = d3d11_device_hwctx->device;
782  ID3D11Device_AddRef(ctx->d3d11_device);
783  }
784 #endif
785 
786  ret = nvenc_open_session(avctx);
787  if (ret < 0)
788  return ret;
789 
790  ret = nvenc_check_capabilities(avctx);
791  if (ret < 0) {
792  av_log(avctx, AV_LOG_FATAL, "Provided device doesn't support required NVENC features\n");
793  return ret;
794  }
795  } else {
796  int i, nb_devices = 0;
797 
798  if (CHECK_CU(dl_fn->cuda_dl->cuInit(0)) < 0)
799  return AVERROR_UNKNOWN;
800 
801  if (CHECK_CU(dl_fn->cuda_dl->cuDeviceGetCount(&nb_devices)) < 0)
802  return AVERROR_UNKNOWN;
803 
804  if (!nb_devices) {
805  av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
806  return AVERROR_EXTERNAL;
807  }
808 
809  av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", nb_devices);
810 
811  dl_fn->nvenc_device_count = 0;
812  for (i = 0; i < nb_devices; ++i) {
813  if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES)
814  return 0;
815  }
816 
817  if (ctx->device == LIST_DEVICES)
818  return AVERROR_EXIT;
819 
820  if (!dl_fn->nvenc_device_count) {
821  av_log(avctx, AV_LOG_FATAL, "No capable devices found\n");
822  return AVERROR_EXTERNAL;
823  }
824 
825  av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->device, nb_devices);
826  return AVERROR(EINVAL);
827  }
828 
829  return 0;
830 }
831 
832 static av_cold void set_constqp(AVCodecContext *avctx)
833 {
834  NvencContext *ctx = avctx->priv_data;
835  NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
836 #if CONFIG_AV1_NVENC_ENCODER
837  int qmax = avctx->codec->id == AV_CODEC_ID_AV1 ? 255 : 51;
838 #else
839  int qmax = 51;
840 #endif
841 
842  rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
843 
844  if (ctx->init_qp_p >= 0) {
845  rc->constQP.qpInterP = ctx->init_qp_p;
846  if (ctx->init_qp_i >= 0 && ctx->init_qp_b >= 0) {
847  rc->constQP.qpIntra = ctx->init_qp_i;
848  rc->constQP.qpInterB = ctx->init_qp_b;
849  } else if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
850  rc->constQP.qpIntra = av_clip(
851  rc->constQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax);
852  rc->constQP.qpInterB = av_clip(
853  rc->constQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, qmax);
854  } else {
855  rc->constQP.qpIntra = rc->constQP.qpInterP;
856  rc->constQP.qpInterB = rc->constQP.qpInterP;
857  }
858  } else if (ctx->cqp >= 0) {
859  rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra = ctx->cqp;
860  if (avctx->b_quant_factor != 0.0)
861  rc->constQP.qpInterB = av_clip(ctx->cqp * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, qmax);
862  if (avctx->i_quant_factor != 0.0)
863  rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax);
864  }
865 
866  avctx->qmin = -1;
867  avctx->qmax = -1;
868 }
869 
870 static av_cold void set_vbr(AVCodecContext *avctx)
871 {
872  NvencContext *ctx = avctx->priv_data;
873  NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
874  int qp_inter_p;
875 #if CONFIG_AV1_NVENC_ENCODER
876  int qmax = avctx->codec->id == AV_CODEC_ID_AV1 ? 255 : 51;
877 #else
878  int qmax = 51;
879 #endif
880 
881  if (avctx->qmin >= 0 && avctx->qmax >= 0) {
882  rc->enableMinQP = 1;
883  rc->enableMaxQP = 1;
884 
885  rc->minQP.qpInterB = avctx->qmin;
886  rc->minQP.qpInterP = avctx->qmin;
887  rc->minQP.qpIntra = avctx->qmin;
888 
889  rc->maxQP.qpInterB = avctx->qmax;
890  rc->maxQP.qpInterP = avctx->qmax;
891  rc->maxQP.qpIntra = avctx->qmax;
892 
893  qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin
894  } else if (avctx->qmin >= 0) {
895  rc->enableMinQP = 1;
896 
897  rc->minQP.qpInterB = avctx->qmin;
898  rc->minQP.qpInterP = avctx->qmin;
899  rc->minQP.qpIntra = avctx->qmin;
900 
901  qp_inter_p = avctx->qmin;
902  } else {
903  qp_inter_p = 26; // default to 26
904  }
905 
906  rc->enableInitialRCQP = 1;
907 
908  if (ctx->init_qp_p < 0) {
909  rc->initialRCQP.qpInterP = qp_inter_p;
910  } else {
911  rc->initialRCQP.qpInterP = ctx->init_qp_p;
912  }
913 
914  if (ctx->init_qp_i < 0) {
915  if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
916  rc->initialRCQP.qpIntra = av_clip(
917  rc->initialRCQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax);
918  } else {
919  rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
920  }
921  } else {
922  rc->initialRCQP.qpIntra = ctx->init_qp_i;
923  }
924 
925  if (ctx->init_qp_b < 0) {
926  if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
927  rc->initialRCQP.qpInterB = av_clip(
928  rc->initialRCQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, qmax);
929  } else {
930  rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
931  }
932  } else {
933  rc->initialRCQP.qpInterB = ctx->init_qp_b;
934  }
935 }
936 
938 {
939  NvencContext *ctx = avctx->priv_data;
940  NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
941 
942  rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
943  rc->constQP.qpInterB = 0;
944  rc->constQP.qpInterP = 0;
945  rc->constQP.qpIntra = 0;
946 
947  avctx->qmin = -1;
948  avctx->qmax = -1;
949 }
950 
952 {
953  NvencContext *ctx = avctx->priv_data;
954  NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
955 
956  switch (ctx->rc) {
957  case NV_ENC_PARAMS_RC_CONSTQP:
958  set_constqp(avctx);
959  return;
960 #ifndef NVENC_NO_DEPRECATED_RC
961  case NV_ENC_PARAMS_RC_VBR_MINQP:
962  if (avctx->qmin < 0) {
963  av_log(avctx, AV_LOG_WARNING,
964  "The variable bitrate rate-control requires "
965  "the 'qmin' option set.\n");
966  set_vbr(avctx);
967  return;
968  }
969  /* fall through */
970  case NV_ENC_PARAMS_RC_VBR_HQ:
971 #endif
972  case NV_ENC_PARAMS_RC_VBR:
973  set_vbr(avctx);
974  break;
975  case NV_ENC_PARAMS_RC_CBR:
976 #ifndef NVENC_NO_DEPRECATED_RC
977  case NV_ENC_PARAMS_RC_CBR_HQ:
978  case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
979 #endif
980  break;
981  }
982 
983  rc->rateControlMode = ctx->rc;
984 }
985 
987 {
988  NvencContext *ctx = avctx->priv_data;
989  // default minimum of 4 surfaces
990  // multiply by 2 for number of NVENCs on gpu (hardcode to 2)
991  // another multiply by 2 to avoid blocking next PBB group
992  int nb_surfaces = FFMAX(4, ctx->encode_config.frameIntervalP * 2 * 2);
993 
994  // lookahead enabled
995  if (ctx->rc_lookahead > 0) {
996  // +1 is to account for lkd_bound calculation later
997  // +4 is to allow sufficient pipelining with lookahead
998  nb_surfaces = FFMAX(1, FFMAX(nb_surfaces, ctx->rc_lookahead + ctx->encode_config.frameIntervalP + 1 + 4));
999  if (nb_surfaces > ctx->nb_surfaces && ctx->nb_surfaces > 0)
1000  {
1001  av_log(avctx, AV_LOG_WARNING,
1002  "Defined rc_lookahead requires more surfaces, "
1003  "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces);
1004  }
1005  ctx->nb_surfaces = FFMAX(nb_surfaces, ctx->nb_surfaces);
1006  } else {
1007  if (ctx->encode_config.frameIntervalP > 1 && ctx->nb_surfaces < nb_surfaces && ctx->nb_surfaces > 0)
1008  {
1009  av_log(avctx, AV_LOG_WARNING,
1010  "Defined b-frame requires more surfaces, "
1011  "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces);
1012  ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, nb_surfaces);
1013  }
1014  else if (ctx->nb_surfaces <= 0)
1015  ctx->nb_surfaces = nb_surfaces;
1016  // otherwise use user specified value
1017  }
1018 
1019  ctx->nb_surfaces = FFMAX(1, FFMIN(MAX_REGISTERED_FRAMES, ctx->nb_surfaces));
1020  ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1);
1021 
1022  // Output in the worst case will only start when the surface buffer is completely full.
1023  // Hence we need to keep at least the max amount of surfaces plus the max reorder delay around.
1024  ctx->frame_data_array_nb = FFMAX(ctx->nb_surfaces, ctx->nb_surfaces + ctx->encode_config.frameIntervalP - 1);
1025 
1026  return 0;
1027 }
1028 
1030 {
1031  NvencContext *ctx = avctx->priv_data;
1032 
1033  if (avctx->global_quality > 0)
1034  av_log(avctx, AV_LOG_WARNING, "Using global_quality with nvenc is deprecated. Use qp instead.\n");
1035 
1036  if (ctx->cqp < 0 && avctx->global_quality > 0)
1037  ctx->cqp = avctx->global_quality;
1038 
1039  if (avctx->bit_rate > 0) {
1040  ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
1041  } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
1042  ctx->encode_config.rcParams.maxBitRate = ctx->encode_config.rcParams.averageBitRate;
1043  }
1044 
1045  if (avctx->rc_max_rate > 0)
1046  ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
1047 
1048 #ifdef NVENC_HAVE_MULTIPASS
1049  ctx->encode_config.rcParams.multiPass = ctx->multipass;
1050 
1051  if (ctx->flags & NVENC_ONE_PASS)
1052  ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
1053  if (ctx->flags & NVENC_TWO_PASSES || ctx->twopass > 0)
1054  ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
1055 
1056  if (ctx->rc < 0) {
1057  if (ctx->cbr) {
1058  ctx->rc = NV_ENC_PARAMS_RC_CBR;
1059  } else if (ctx->cqp >= 0) {
1060  ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1061  } else if (ctx->quality >= 0.0f) {
1062  ctx->rc = NV_ENC_PARAMS_RC_VBR;
1063  }
1064  }
1065 #else
1066  if (ctx->rc < 0) {
1067  if (ctx->flags & NVENC_ONE_PASS)
1068  ctx->twopass = 0;
1069  if (ctx->flags & NVENC_TWO_PASSES)
1070  ctx->twopass = 1;
1071 
1072  if (ctx->twopass < 0)
1073  ctx->twopass = (ctx->flags & NVENC_LOWLATENCY) != 0;
1074 
1075  if (ctx->cbr) {
1076  if (ctx->twopass) {
1077  ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
1078  } else {
1079  ctx->rc = NV_ENC_PARAMS_RC_CBR;
1080  }
1081  } else if (ctx->cqp >= 0) {
1082  ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1083  } else if (ctx->twopass) {
1084  ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1085  } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
1086  ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1087  }
1088  }
1089 #endif
1090 
1091  if (ctx->rc >= 0 && ctx->rc & RC_MODE_DEPRECATED) {
1092  av_log(avctx, AV_LOG_WARNING, "Specified rc mode is deprecated.\n");
1093  av_log(avctx, AV_LOG_WARNING, "Use -rc constqp/cbr/vbr, -tune and -multipass instead.\n");
1094 
1095  ctx->rc &= ~RC_MODE_DEPRECATED;
1096  }
1097 
1098 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1099  ctx->encode_config.rcParams.cbQPIndexOffset = ctx->qp_cb_offset;
1100  ctx->encode_config.rcParams.crQPIndexOffset = ctx->qp_cr_offset;
1101 #else
1102  if (ctx->qp_cb_offset || ctx->qp_cr_offset)
1103  av_log(avctx, AV_LOG_WARNING, "Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1104 #endif
1105 
1106 #ifdef NVENC_HAVE_LDKFS
1107  if (ctx->ldkfs)
1108  ctx->encode_config.rcParams.lowDelayKeyFrameScale = ctx->ldkfs;
1109 #endif
1110 
1111  if (ctx->flags & NVENC_LOSSLESS) {
1112  set_lossless(avctx);
1113  } else if (ctx->rc >= 0) {
1115  } else {
1116  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1117  set_vbr(avctx);
1118  }
1119 
1120  if (avctx->rc_buffer_size > 0) {
1121  ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size;
1122  } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
1123  avctx->rc_buffer_size = ctx->encode_config.rcParams.vbvBufferSize = 2 * ctx->encode_config.rcParams.averageBitRate;
1124  }
1125 
1126  if (ctx->aq) {
1127  ctx->encode_config.rcParams.enableAQ = 1;
1128  ctx->encode_config.rcParams.aqStrength = ctx->aq_strength;
1129  av_log(avctx, AV_LOG_VERBOSE, "AQ enabled.\n");
1130  }
1131 
1132  if (ctx->temporal_aq) {
1133  ctx->encode_config.rcParams.enableTemporalAQ = 1;
1134  av_log(avctx, AV_LOG_VERBOSE, "Temporal AQ enabled.\n");
1135  }
1136 
1137  if (ctx->rc_lookahead > 0) {
1138  int lkd_bound = FFMIN(ctx->nb_surfaces, ctx->async_depth) -
1139  ctx->encode_config.frameIntervalP - 4;
1140 
1141  if (lkd_bound < 0) {
1142  ctx->encode_config.rcParams.enableLookahead = 0;
1143  av_log(avctx, AV_LOG_WARNING,
1144  "Lookahead not enabled. Increase buffer delay (-delay).\n");
1145  } else {
1146  ctx->encode_config.rcParams.enableLookahead = 1;
1147  ctx->encode_config.rcParams.lookaheadDepth = av_clip(ctx->rc_lookahead, 0, lkd_bound);
1148  ctx->encode_config.rcParams.disableIadapt = ctx->no_scenecut;
1149  ctx->encode_config.rcParams.disableBadapt = !ctx->b_adapt;
1150  av_log(avctx, AV_LOG_VERBOSE,
1151  "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1152  ctx->encode_config.rcParams.lookaheadDepth,
1153  ctx->encode_config.rcParams.disableIadapt ? "disabled" : "enabled",
1154  ctx->encode_config.rcParams.disableBadapt ? "disabled" : "enabled");
1155  if (ctx->encode_config.rcParams.lookaheadDepth < ctx->rc_lookahead)
1156  av_log(avctx, AV_LOG_WARNING, "Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay",
1157  ctx->encode_config.rcParams.lookaheadDepth, ctx->rc_lookahead);
1158 
1159 #ifdef NVENC_HAVE_LOOKAHEAD_LEVEL
1160  if (ctx->lookahead_level >= 0) {
1161  switch (ctx->lookahead_level) {
1162  case NV_ENC_LOOKAHEAD_LEVEL_0:
1163  case NV_ENC_LOOKAHEAD_LEVEL_1:
1164  case NV_ENC_LOOKAHEAD_LEVEL_2:
1165  case NV_ENC_LOOKAHEAD_LEVEL_3:
1166  case NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT:
1167  break;
1168  default:
1169  av_log(avctx, AV_LOG_ERROR, "Invalid lookahead level.\n");
1170  return AVERROR(EINVAL);
1171  }
1172 
1173  ctx->encode_config.rcParams.lookaheadLevel = ctx->lookahead_level;
1174  }
1175 #endif
1176  }
1177  }
1178 
1179  if (ctx->strict_gop) {
1180  ctx->encode_config.rcParams.strictGOPTarget = 1;
1181  av_log(avctx, AV_LOG_VERBOSE, "Strict GOP target enabled.\n");
1182  }
1183 
1184  if (ctx->nonref_p)
1185  ctx->encode_config.rcParams.enableNonRefP = 1;
1186 
1187  if (ctx->zerolatency)
1188  ctx->encode_config.rcParams.zeroReorderDelay = 1;
1189 
1190  if (ctx->quality) {
1191  //convert from float to fixed point 8.8
1192  int tmp_quality = (int)(ctx->quality * 256.0f);
1193  ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1194  ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1195 
1196  av_log(avctx, AV_LOG_VERBOSE, "CQ(%d) mode enabled.\n", tmp_quality);
1197 
1198  // CQ mode shall discard avg bitrate/vbv buffer size and honor only max bitrate
1199  ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate = 0;
1200  ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size = 0;
1201  ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
1202  }
1203 
1204  return 0;
1205 }
1206 
1208 {
1209  NvencContext *ctx = avctx->priv_data;
1210  NV_ENC_CONFIG *cc = &ctx->encode_config;
1211  NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1212  NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1213 
1214  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt);
1215 
1216  if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) {
1217  vui->colourMatrix = AVCOL_SPC_BT470BG;
1218  vui->colourPrimaries = avctx->color_primaries;
1219  vui->transferCharacteristics = avctx->color_trc;
1220  vui->videoFullRangeFlag = 0;
1221  } else {
1222  vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace;
1223  vui->colourPrimaries = avctx->color_primaries;
1224  vui->transferCharacteristics = avctx->color_trc;
1225  vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
1226  || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P);
1227  }
1228 
1229  vui->colourDescriptionPresentFlag =
1230  (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1231 
1232  vui->videoSignalTypePresentFlag =
1233  (vui->colourDescriptionPresentFlag
1234  || vui->videoFormat != 5
1235  || vui->videoFullRangeFlag != 0);
1236 
1237  if (ctx->max_slice_size > 0) {
1238  h264->sliceMode = 1;
1239  h264->sliceModeData = ctx->max_slice_size;
1240  } else {
1241  h264->sliceMode = 3;
1242  h264->sliceModeData = avctx->slices > 0 ? avctx->slices : 1;
1243  }
1244 
1245  if (ctx->intra_refresh) {
1246  h264->enableIntraRefresh = 1;
1247  h264->intraRefreshPeriod = cc->gopLength;
1248  h264->intraRefreshCnt = cc->gopLength - 1;
1249  cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1250 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1251  h264->singleSliceIntraRefresh = ctx->single_slice_intra_refresh;
1252 #endif
1253  }
1254 
1255  if (ctx->constrained_encoding)
1256  h264->enableConstrainedEncoding = 1;
1257 
1258  h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1259  h264->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1260  h264->outputAUD = ctx->aud;
1261 
1262  if (ctx->dpb_size >= 0) {
1263  /* 0 means "let the hardware decide" */
1264  h264->maxNumRefFrames = ctx->dpb_size;
1265  }
1266 
1267  h264->idrPeriod = cc->gopLength;
1268 
1269  if (IS_CBR(cc->rcParams.rateControlMode)) {
1270  h264->outputBufferingPeriodSEI = 1;
1271  }
1272 
1273  h264->outputPictureTimingSEI = 1;
1274 
1275 #ifndef NVENC_NO_DEPRECATED_RC
1276  if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1277  cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1278  cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1279  h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1280  h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1281  }
1282 #endif
1283 
1284  if (ctx->flags & NVENC_LOSSLESS) {
1285  h264->qpPrimeYZeroTransformBypassFlag = 1;
1286  } else {
1287  switch(ctx->profile) {
1289  cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1291  break;
1293  cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1294  avctx->profile = AV_PROFILE_H264_MAIN;
1295  break;
1297  cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1298  avctx->profile = AV_PROFILE_H264_HIGH;
1299  break;
1301  cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1303  break;
1304  }
1305  }
1306 
1307  // force setting profile as high444p if input is AV_PIX_FMT_YUV444P
1308  if (IS_YUV444(ctx->data_pix_fmt)) {
1309  cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1311  }
1312 
1313  vui->bitstreamRestrictionFlag = cc->gopLength != 1 || avctx->profile < AV_PROFILE_H264_HIGH;
1314 
1315  h264->chromaFormatIDC = avctx->profile == AV_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1;
1316 
1317  h264->level = ctx->level;
1318 
1319 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1320  h264->inputBitDepth = h264->outputBitDepth =
1321  IS_10BIT(ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1322 #endif
1323 
1324  if (ctx->coder >= 0)
1325  h264->entropyCodingMode = ctx->coder;
1326 
1327 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1328  if (ctx->b_ref_mode >= 0)
1329  h264->useBFramesAsRef = ctx->b_ref_mode;
1330 #endif
1331 
1332 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1333  h264->numRefL0 = avctx->refs;
1334  h264->numRefL1 = avctx->refs;
1335 #endif
1336 
1337  return 0;
1338 }
1339 
1341 {
1342  NvencContext *ctx = avctx->priv_data;
1343  NV_ENC_CONFIG *cc = &ctx->encode_config;
1344  NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1345  NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1346 
1347  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt);
1348 
1349  if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) {
1350  vui->colourMatrix = AVCOL_SPC_BT470BG;
1351  vui->colourPrimaries = avctx->color_primaries;
1352  vui->transferCharacteristics = avctx->color_trc;
1353  vui->videoFullRangeFlag = 0;
1354  } else {
1355  vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace;
1356  vui->colourPrimaries = avctx->color_primaries;
1357  vui->transferCharacteristics = avctx->color_trc;
1358  vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
1359  || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P);
1360  }
1361 
1362  vui->colourDescriptionPresentFlag =
1363  (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1364 
1365  vui->videoSignalTypePresentFlag =
1366  (vui->colourDescriptionPresentFlag
1367  || vui->videoFormat != 5
1368  || vui->videoFullRangeFlag != 0);
1369 
1370  if (ctx->max_slice_size > 0) {
1371  hevc->sliceMode = 1;
1372  hevc->sliceModeData = ctx->max_slice_size;
1373  } else {
1374  hevc->sliceMode = 3;
1375  hevc->sliceModeData = avctx->slices > 0 ? avctx->slices : 1;
1376  }
1377 
1378  if (ctx->intra_refresh) {
1379  hevc->enableIntraRefresh = 1;
1380  hevc->intraRefreshPeriod = cc->gopLength;
1381  hevc->intraRefreshCnt = cc->gopLength - 1;
1382  cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1383 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1384  hevc->singleSliceIntraRefresh = ctx->single_slice_intra_refresh;
1385 #endif
1386  }
1387 
1388 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1389  if (ctx->constrained_encoding)
1390  hevc->enableConstrainedEncoding = 1;
1391 #endif
1392 
1393  hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1394  hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1395  hevc->outputAUD = ctx->aud;
1396 
1397  if (ctx->dpb_size >= 0) {
1398  /* 0 means "let the hardware decide" */
1399  hevc->maxNumRefFramesInDPB = ctx->dpb_size;
1400  }
1401 
1402  hevc->idrPeriod = cc->gopLength;
1403 
1404  if (IS_CBR(cc->rcParams.rateControlMode)) {
1405  hevc->outputBufferingPeriodSEI = 1;
1406  }
1407 
1408  hevc->outputPictureTimingSEI = 1;
1409 
1410  switch (ctx->profile) {
1412  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1413  avctx->profile = AV_PROFILE_HEVC_MAIN;
1414  break;
1416  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1418  break;
1420  cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1421  avctx->profile = AV_PROFILE_HEVC_REXT;
1422  break;
1423  }
1424 
1425  // force setting profile as main10 if input is 10 bit or if it should be encoded as 10 bit
1426  if (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) {
1427  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1429  }
1430 
1431  // force setting profile as rext if input is yuv444
1432  if (IS_YUV444(ctx->data_pix_fmt)) {
1433  cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1434  avctx->profile = AV_PROFILE_HEVC_REXT;
1435  }
1436 
1437  hevc->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1;
1438 
1439 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1440  hevc->inputBitDepth = IS_10BIT(ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1441  hevc->outputBitDepth = (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1442 #else
1443  hevc->pixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0;
1444 #endif
1445 
1446  hevc->level = ctx->level;
1447 
1448  hevc->tier = ctx->tier;
1449 
1450 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1451  if (ctx->b_ref_mode >= 0)
1452  hevc->useBFramesAsRef = ctx->b_ref_mode;
1453 #endif
1454 
1455 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1456  hevc->numRefL0 = avctx->refs;
1457  hevc->numRefL1 = avctx->refs;
1458 #endif
1459 
1460 #ifdef NVENC_HAVE_TEMPORAL_FILTER
1461  if (ctx->tf_level >= 0) {
1462  hevc->tfLevel = ctx->tf_level;
1463 
1464  switch (ctx->tf_level)
1465  {
1466  case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1467  case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1468  break;
1469  default:
1470  av_log(avctx, AV_LOG_ERROR, "Invalid temporal filtering level.\n");
1471  return AVERROR(EINVAL);
1472  }
1473 
1474  if (ctx->encode_config.frameIntervalP < 5)
1475  av_log(avctx, AV_LOG_WARNING, "Temporal filtering needs at least 4 B-Frames (-bf 4).\n");
1476  }
1477 #endif
1478 
1479  return 0;
1480 }
1481 
1482 #if CONFIG_AV1_NVENC_ENCODER
1483 static av_cold int nvenc_setup_av1_config(AVCodecContext *avctx)
1484 {
1485  NvencContext *ctx = avctx->priv_data;
1486  NV_ENC_CONFIG *cc = &ctx->encode_config;
1487  NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1488 
1489  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt);
1490 
1491  if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) {
1492  av1->matrixCoefficients = AVCOL_SPC_BT470BG;
1493  av1->colorPrimaries = avctx->color_primaries;
1494  av1->transferCharacteristics = avctx->color_trc;
1495  av1->colorRange = 0;
1496  } else {
1497  av1->matrixCoefficients = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace;
1498  av1->colorPrimaries = avctx->color_primaries;
1499  av1->transferCharacteristics = avctx->color_trc;
1500  av1->colorRange = (avctx->color_range == AVCOL_RANGE_JPEG
1501  || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P);
1502  }
1503 
1504  if (IS_YUV444(ctx->data_pix_fmt)) {
1505  av_log(avctx, AV_LOG_ERROR, "AV1 High Profile not supported, required for 4:4:4 encoding\n");
1506  return AVERROR(ENOTSUP);
1507  } else {
1508  cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1509  avctx->profile = AV_PROFILE_AV1_MAIN;
1510  }
1511 
1512  if (ctx->dpb_size >= 0) {
1513  /* 0 means "let the hardware decide" */
1514  av1->maxNumRefFramesInDPB = ctx->dpb_size;
1515  }
1516 
1517  if (ctx->intra_refresh) {
1518  av1->enableIntraRefresh = 1;
1519  av1->intraRefreshPeriod = cc->gopLength;
1520  av1->intraRefreshCnt = cc->gopLength - 1;
1521  cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1522  }
1523 
1524  av1->idrPeriod = cc->gopLength;
1525 
1526  if (IS_CBR(cc->rcParams.rateControlMode)) {
1527  av1->enableBitstreamPadding = 1;
1528  }
1529 
1530  if (ctx->tile_cols >= 0)
1531  av1->numTileColumns = ctx->tile_cols;
1532  if (ctx->tile_rows >= 0)
1533  av1->numTileRows = ctx->tile_rows;
1534 
1535  av1->outputAnnexBFormat = 0;
1536 
1537  av1->level = ctx->level;
1538  av1->tier = ctx->tier;
1539 
1540  av1->enableTimingInfo = ctx->timing_info;
1541 
1542  /* mp4 encapsulation requires sequence headers to be present on all keyframes for AV1 */
1543  av1->disableSeqHdr = 0;
1544  av1->repeatSeqHdr = 1;
1545 
1546  av1->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1;
1547 
1548 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1549  av1->inputBitDepth = IS_10BIT(ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1550  av1->outputBitDepth = (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1551 #else
1552  av1->inputPixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0;
1553  av1->pixelBitDepthMinus8 = (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) ? 2 : 0;
1554 #endif
1555 
1556  if (ctx->b_ref_mode >= 0)
1557  av1->useBFramesAsRef = ctx->b_ref_mode;
1558 
1559  av1->numFwdRefs = avctx->refs;
1560  av1->numBwdRefs = avctx->refs;
1561 
1562  return 0;
1563 }
1564 #endif
1565 
1567 {
1568  switch (avctx->codec->id) {
1569  case AV_CODEC_ID_H264:
1570  return nvenc_setup_h264_config(avctx);
1571  case AV_CODEC_ID_HEVC:
1572  return nvenc_setup_hevc_config(avctx);
1573 #if CONFIG_AV1_NVENC_ENCODER
1574  case AV_CODEC_ID_AV1:
1575  return nvenc_setup_av1_config(avctx);
1576 #endif
1577  /* Earlier switch/case will return if unknown codec is passed. */
1578  }
1579 
1580  return 0;
1581 }
1582 
1583 static void compute_dar(AVCodecContext *avctx, int *dw, int *dh) {
1584  int sw, sh;
1585 
1586  sw = avctx->width;
1587  sh = avctx->height;
1588 
1589 #if CONFIG_AV1_NVENC_ENCODER
1590  if (avctx->codec->id == AV_CODEC_ID_AV1) {
1591  /* For AV1 we actually need to calculate the render width/height, not the dar */
1592  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0
1593  && avctx->sample_aspect_ratio.num != avctx->sample_aspect_ratio.den)
1594  {
1595  if (avctx->sample_aspect_ratio.num > avctx->sample_aspect_ratio.den) {
1596  sw = av_rescale(sw, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
1597  } else {
1598  sh = av_rescale(sh, avctx->sample_aspect_ratio.den, avctx->sample_aspect_ratio.num);
1599  }
1600  }
1601 
1602  *dw = sw;
1603  *dh = sh;
1604  return;
1605  }
1606 #endif
1607 
1608  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
1609  sw *= avctx->sample_aspect_ratio.num;
1610  sh *= avctx->sample_aspect_ratio.den;
1611  }
1612 
1613  av_reduce(dw, dh, sw, sh, 1024 * 1024);
1614 }
1615 
1617 {
1618  NvencContext *ctx = avctx->priv_data;
1619  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1620  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1621 
1622  NV_ENC_PRESET_CONFIG preset_config = { 0 };
1623  NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1624  AVCPBProperties *cpb_props;
1625  int res = 0;
1626  int dw, dh;
1627 
1628  ctx->encode_config.version = NV_ENC_CONFIG_VER;
1629  ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1630 
1631  ctx->init_encode_params.encodeHeight = avctx->height;
1632  ctx->init_encode_params.encodeWidth = avctx->width;
1633 
1634  ctx->init_encode_params.encodeConfig = &ctx->encode_config;
1635 
1636  preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1637  preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1638 
1639 #ifdef NVENC_HAVE_NEW_PRESETS
1640  ctx->init_encode_params.tuningInfo = ctx->tuning_info;
1641 
1642  if (ctx->flags & NVENC_LOSSLESS)
1643  ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1644  else if (ctx->flags & NVENC_LOWLATENCY)
1645  ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1646 
1647  nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(ctx->nvencoder,
1648  ctx->init_encode_params.encodeGUID,
1649  ctx->init_encode_params.presetGUID,
1650  ctx->init_encode_params.tuningInfo,
1651  &preset_config);
1652 #else
1653  nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder,
1654  ctx->init_encode_params.encodeGUID,
1655  ctx->init_encode_params.presetGUID,
1656  &preset_config);
1657 #endif
1658  if (nv_status != NV_ENC_SUCCESS)
1659  return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration");
1660 
1661  memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
1662 
1663  ctx->encode_config.version = NV_ENC_CONFIG_VER;
1664 
1665  compute_dar(avctx, &dw, &dh);
1666  ctx->init_encode_params.darHeight = dh;
1667  ctx->init_encode_params.darWidth = dw;
1668 
1669  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
1670  ctx->init_encode_params.frameRateNum = avctx->framerate.num;
1671  ctx->init_encode_params.frameRateDen = avctx->framerate.den;
1672  } else {
1673  ctx->init_encode_params.frameRateNum = avctx->time_base.den;
1675  ctx->init_encode_params.frameRateDen = avctx->time_base.num
1676 #if FF_API_TICKS_PER_FRAME
1677  * avctx->ticks_per_frame
1678 #endif
1679  ;
1681  }
1682 
1683 #ifdef NVENC_HAVE_UNIDIR_B
1684  ctx->init_encode_params.enableUniDirectionalB = ctx->unidir_b;
1685 #endif
1686 
1687  ctx->init_encode_params.enableEncodeAsync = 0;
1688  ctx->init_encode_params.enablePTD = 1;
1689 
1690 #ifdef NVENC_HAVE_NEW_PRESETS
1691  /* If lookahead isn't set from CLI, use value from preset.
1692  * P6 & P7 presets may enable lookahead for better quality.
1693  * */
1694  if (ctx->rc_lookahead == 0 && ctx->encode_config.rcParams.enableLookahead)
1695  ctx->rc_lookahead = ctx->encode_config.rcParams.lookaheadDepth;
1696 #endif
1697 
1698  if (ctx->weighted_pred == 1)
1699  ctx->init_encode_params.enableWeightedPrediction = 1;
1700 
1701 #ifdef NVENC_HAVE_SPLIT_FRAME_ENCODING
1702  ctx->init_encode_params.splitEncodeMode = ctx->split_encode_mode;
1703 
1704  if (ctx->split_encode_mode != NV_ENC_SPLIT_DISABLE_MODE) {
1705  if (avctx->codec->id == AV_CODEC_ID_HEVC && ctx->weighted_pred == 1)
1706  av_log(avctx, AV_LOG_WARNING, "Split encoding not supported with weighted prediction enabled.\n");
1707  }
1708 #endif
1709 
1710  if (ctx->bluray_compat) {
1711  ctx->aud = 1;
1712  ctx->dpb_size = FFMIN(FFMAX(avctx->refs, 0), 6);
1713  avctx->max_b_frames = FFMIN(avctx->max_b_frames, 3);
1714  switch (avctx->codec->id) {
1715  case AV_CODEC_ID_H264:
1716  /* maximum level depends on used resolution */
1717  break;
1718  case AV_CODEC_ID_HEVC:
1719  ctx->level = NV_ENC_LEVEL_HEVC_51;
1720  ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1721  break;
1722  }
1723  }
1724 
1725  if (avctx->gop_size > 0) {
1726  // only overwrite preset if a GOP size was selected as input
1727  ctx->encode_config.gopLength = avctx->gop_size;
1728  } else if (avctx->gop_size == 0) {
1729  ctx->encode_config.frameIntervalP = 0;
1730  ctx->encode_config.gopLength = 1;
1731  }
1732 
1733  if (avctx->max_b_frames >= 0 && ctx->encode_config.gopLength > 1) {
1734  /* 0 is intra-only, 1 is I/P only, 2 is one B-Frame, 3 two B-frames, and so on. */
1735  ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
1736  }
1737 
1738  /* force to enable intra refresh */
1739  if(ctx->single_slice_intra_refresh)
1740  ctx->intra_refresh = 1;
1741 
1742  nvenc_recalc_surfaces(avctx);
1743 
1744  res = nvenc_setup_rate_control(avctx);
1745  if (res < 0)
1746  return res;
1747 
1748  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
1749  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1750  } else {
1751  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1752  }
1753 
1754  res = nvenc_setup_codec_config(avctx);
1755  if (res)
1756  return res;
1757 
1758  res = nvenc_push_context(avctx);
1759  if (res < 0)
1760  return res;
1761 
1762  nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
1763  if (nv_status != NV_ENC_SUCCESS) {
1764  nvenc_pop_context(avctx);
1765  return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed");
1766  }
1767 
1768 #ifdef NVENC_HAVE_CUSTREAM_PTR
1769  if (ctx->cu_context) {
1770  nv_status = p_nvenc->nvEncSetIOCudaStreams(ctx->nvencoder, &ctx->cu_stream, &ctx->cu_stream);
1771  if (nv_status != NV_ENC_SUCCESS) {
1772  nvenc_pop_context(avctx);
1773  return nvenc_print_error(avctx, nv_status, "SetIOCudaStreams failed");
1774  }
1775  }
1776 #endif
1777 
1778  res = nvenc_pop_context(avctx);
1779  if (res < 0)
1780  return res;
1781 
1782  if (ctx->encode_config.frameIntervalP > 1)
1783  avctx->has_b_frames = 2;
1784 
1785  if (ctx->encode_config.rcParams.averageBitRate > 0)
1786  avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
1787 
1788  cpb_props = ff_encode_add_cpb_side_data(avctx);
1789  if (!cpb_props)
1790  return AVERROR(ENOMEM);
1791  cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate;
1792  cpb_props->avg_bitrate = avctx->bit_rate;
1793  cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize;
1794 
1795  return 0;
1796 }
1797 
1798 static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt)
1799 {
1800  switch (pix_fmt) {
1801  case AV_PIX_FMT_YUV420P:
1802  return NV_ENC_BUFFER_FORMAT_YV12;
1803  case AV_PIX_FMT_NV12:
1804  return NV_ENC_BUFFER_FORMAT_NV12;
1805  case AV_PIX_FMT_P010:
1806  case AV_PIX_FMT_P016:
1807  return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1808  case AV_PIX_FMT_GBRP:
1809  case AV_PIX_FMT_YUV444P:
1810  return NV_ENC_BUFFER_FORMAT_YUV444;
1811  case AV_PIX_FMT_GBRP16:
1812  case AV_PIX_FMT_YUV444P16:
1813  return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1814  case AV_PIX_FMT_0RGB32:
1815  case AV_PIX_FMT_RGB32:
1816  return NV_ENC_BUFFER_FORMAT_ARGB;
1817  case AV_PIX_FMT_0BGR32:
1818  case AV_PIX_FMT_BGR32:
1819  return NV_ENC_BUFFER_FORMAT_ABGR;
1820  case AV_PIX_FMT_X2RGB10:
1821  return NV_ENC_BUFFER_FORMAT_ARGB10;
1822  case AV_PIX_FMT_X2BGR10:
1823  return NV_ENC_BUFFER_FORMAT_ABGR10;
1824  default:
1825  return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1826  }
1827 }
1828 
1829 static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
1830 {
1831  NvencContext *ctx = avctx->priv_data;
1832  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1833  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1834  NvencSurface* tmp_surface = &ctx->surfaces[idx];
1835 
1836  NVENCSTATUS nv_status;
1837  NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1838  allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1839 
1840  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1841  ctx->surfaces[idx].in_ref = av_frame_alloc();
1842  if (!ctx->surfaces[idx].in_ref)
1843  return AVERROR(ENOMEM);
1844  } else {
1845  NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1846 
1847  ctx->surfaces[idx].format = nvenc_map_buffer_format(ctx->data_pix_fmt);
1848  if (ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1849  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
1850  av_get_pix_fmt_name(ctx->data_pix_fmt));
1851  return AVERROR(EINVAL);
1852  }
1853 
1854  allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1855  allocSurf.width = avctx->width;
1856  allocSurf.height = avctx->height;
1857  allocSurf.bufferFmt = ctx->surfaces[idx].format;
1858 
1859  nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
1860  if (nv_status != NV_ENC_SUCCESS) {
1861  return nvenc_print_error(avctx, nv_status, "CreateInputBuffer failed");
1862  }
1863 
1864  ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1865  ctx->surfaces[idx].width = allocSurf.width;
1866  ctx->surfaces[idx].height = allocSurf.height;
1867  }
1868 
1869  nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
1870  if (nv_status != NV_ENC_SUCCESS) {
1871  int err = nvenc_print_error(avctx, nv_status, "CreateBitstreamBuffer failed");
1872  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1873  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[idx].input_surface);
1874  av_frame_free(&ctx->surfaces[idx].in_ref);
1875  return err;
1876  }
1877 
1878  ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1879 
1880  av_fifo_write(ctx->unused_surface_queue, &tmp_surface, 1);
1881 
1882  return 0;
1883 }
1884 
1886 {
1887  NvencContext *ctx = avctx->priv_data;
1888  int i, res = 0, res2;
1889 
1890  ctx->surfaces = av_calloc(ctx->nb_surfaces, sizeof(*ctx->surfaces));
1891  if (!ctx->surfaces)
1892  return AVERROR(ENOMEM);
1893 
1894  ctx->frame_data_array = av_calloc(ctx->frame_data_array_nb, sizeof(*ctx->frame_data_array));
1895  if (!ctx->frame_data_array)
1896  return AVERROR(ENOMEM);
1897 
1898  ctx->timestamp_list = av_fifo_alloc2(ctx->nb_surfaces + ctx->encode_config.frameIntervalP,
1899  sizeof(int64_t), 0);
1900  if (!ctx->timestamp_list)
1901  return AVERROR(ENOMEM);
1902 
1903  ctx->unused_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
1904  if (!ctx->unused_surface_queue)
1905  return AVERROR(ENOMEM);
1906 
1907  ctx->output_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
1908  if (!ctx->output_surface_queue)
1909  return AVERROR(ENOMEM);
1910  ctx->output_surface_ready_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
1911  if (!ctx->output_surface_ready_queue)
1912  return AVERROR(ENOMEM);
1913 
1914  res = nvenc_push_context(avctx);
1915  if (res < 0)
1916  return res;
1917 
1918  for (i = 0; i < ctx->nb_surfaces; i++) {
1919  if ((res = nvenc_alloc_surface(avctx, i)) < 0)
1920  goto fail;
1921  }
1922 
1923 fail:
1924  res2 = nvenc_pop_context(avctx);
1925  if (res2 < 0)
1926  return res2;
1927 
1928  return res;
1929 }
1930 
1932 {
1933  NvencContext *ctx = avctx->priv_data;
1934  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1935  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1936 
1937  NVENCSTATUS nv_status;
1938  uint32_t outSize = 0;
1939  char tmpHeader[NV_MAX_SEQ_HDR_LEN];
1940 
1941  NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1942  payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1943 
1944  payload.spsppsBuffer = tmpHeader;
1945  payload.inBufferSize = sizeof(tmpHeader);
1946  payload.outSPSPPSPayloadSize = &outSize;
1947 
1948  nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
1949  if (nv_status != NV_ENC_SUCCESS) {
1950  return nvenc_print_error(avctx, nv_status, "GetSequenceParams failed");
1951  }
1952 
1953  avctx->extradata_size = outSize;
1955 
1956  if (!avctx->extradata) {
1957  return AVERROR(ENOMEM);
1958  }
1959 
1960  memcpy(avctx->extradata, tmpHeader, outSize);
1961 
1962  return 0;
1963 }
1964 
1966 {
1967  NvencContext *ctx = avctx->priv_data;
1968  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1969  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1970  int i, res;
1971 
1972  /* the encoder has to be flushed before it can be closed */
1973  if (ctx->nvencoder) {
1974  NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1975  .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1976 
1977  res = nvenc_push_context(avctx);
1978  if (res < 0)
1979  return res;
1980 
1981  p_nvenc->nvEncEncodePicture(ctx->nvencoder, &params);
1982  }
1983 
1984  av_fifo_freep2(&ctx->timestamp_list);
1985  av_fifo_freep2(&ctx->output_surface_ready_queue);
1986  av_fifo_freep2(&ctx->output_surface_queue);
1987  av_fifo_freep2(&ctx->unused_surface_queue);
1988 
1989  if (ctx->frame_data_array) {
1990  for (i = 0; i < ctx->frame_data_array_nb; i++)
1991  av_buffer_unref(&ctx->frame_data_array[i].frame_opaque_ref);
1992  av_freep(&ctx->frame_data_array);
1993  }
1994 
1995  if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) {
1996  for (i = 0; i < ctx->nb_registered_frames; i++) {
1997  if (ctx->registered_frames[i].mapped)
1998  p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[i].in_map.mappedResource);
1999  if (ctx->registered_frames[i].regptr)
2000  p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr);
2001  }
2002  ctx->nb_registered_frames = 0;
2003  }
2004 
2005  if (ctx->surfaces) {
2006  for (i = 0; i < ctx->nb_surfaces; ++i) {
2007  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
2008  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[i].input_surface);
2009  av_frame_free(&ctx->surfaces[i].in_ref);
2010  p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->surfaces[i].output_surface);
2011  }
2012  }
2013  av_freep(&ctx->surfaces);
2014  ctx->nb_surfaces = 0;
2015 
2016  av_frame_free(&ctx->frame);
2017 
2018  av_freep(&ctx->sei_data);
2019 
2020  if (ctx->nvencoder) {
2021  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
2022 
2023  res = nvenc_pop_context(avctx);
2024  if (res < 0)
2025  return res;
2026  }
2027  ctx->nvencoder = NULL;
2028 
2029  if (ctx->cu_context_internal)
2030  CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal));
2031  ctx->cu_context = ctx->cu_context_internal = NULL;
2032 
2033 #if CONFIG_D3D11VA
2034  if (ctx->d3d11_device) {
2035  ID3D11Device_Release(ctx->d3d11_device);
2036  ctx->d3d11_device = NULL;
2037  }
2038 #endif
2039 
2040  nvenc_free_functions(&dl_fn->nvenc_dl);
2041  cuda_free_functions(&dl_fn->cuda_dl);
2042 
2043  dl_fn->nvenc_device_count = 0;
2044 
2045  av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
2046 
2047  return 0;
2048 }
2049 
2051 {
2052  NvencContext *ctx = avctx->priv_data;
2053  int ret;
2054 
2055  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2056  AVHWFramesContext *frames_ctx;
2057  if (!avctx->hw_frames_ctx) {
2058  av_log(avctx, AV_LOG_ERROR,
2059  "hw_frames_ctx must be set when using GPU frames as input\n");
2060  return AVERROR(EINVAL);
2061  }
2062  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
2063  if (frames_ctx->format != avctx->pix_fmt) {
2064  av_log(avctx, AV_LOG_ERROR,
2065  "hw_frames_ctx must match the GPU frame type\n");
2066  return AVERROR(EINVAL);
2067  }
2068  ctx->data_pix_fmt = frames_ctx->sw_format;
2069  } else {
2070  ctx->data_pix_fmt = avctx->pix_fmt;
2071  }
2072 
2073  if (ctx->rgb_mode == NVENC_RGB_MODE_DISABLED && IS_RGB(ctx->data_pix_fmt)) {
2074  av_log(avctx, AV_LOG_ERROR, "Packed RGB input, but RGB support is disabled.\n");
2075  return AVERROR(EINVAL);
2076  }
2077 
2078  ctx->frame = av_frame_alloc();
2079  if (!ctx->frame)
2080  return AVERROR(ENOMEM);
2081 
2082  if ((ret = nvenc_load_libraries(avctx)) < 0)
2083  return ret;
2084 
2085  if ((ret = nvenc_setup_device(avctx)) < 0)
2086  return ret;
2087 
2088  if ((ret = nvenc_setup_encoder(avctx)) < 0)
2089  return ret;
2090 
2091  if ((ret = nvenc_setup_surfaces(avctx)) < 0)
2092  return ret;
2093 
2094  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
2095  if ((ret = nvenc_setup_extradata(avctx)) < 0)
2096  return ret;
2097  }
2098 
2099  return 0;
2100 }
2101 
2103 {
2104  NvencSurface *tmp_surf;
2105 
2106  if (av_fifo_read(ctx->unused_surface_queue, &tmp_surf, 1) < 0)
2107  // queue empty
2108  return NULL;
2109 
2110  return tmp_surf;
2111 }
2112 
2113 static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface,
2114  NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame)
2115 {
2116  int dst_linesize[4] = {
2117  lock_buffer_params->pitch,
2118  lock_buffer_params->pitch,
2119  lock_buffer_params->pitch,
2120  lock_buffer_params->pitch
2121  };
2122  uint8_t *dst_data[4];
2123  int ret;
2124 
2125  if (frame->format == AV_PIX_FMT_YUV420P)
2126  dst_linesize[1] = dst_linesize[2] >>= 1;
2127 
2128  ret = av_image_fill_pointers(dst_data, frame->format, nv_surface->height,
2129  lock_buffer_params->bufferDataPtr, dst_linesize);
2130  if (ret < 0)
2131  return ret;
2132 
2133  if (frame->format == AV_PIX_FMT_YUV420P)
2134  FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2135 
2136  av_image_copy2(dst_data, dst_linesize,
2137  frame->data, frame->linesize, frame->format,
2138  avctx->width, avctx->height);
2139 
2140  return 0;
2141 }
2142 
2144 {
2145  NvencContext *ctx = avctx->priv_data;
2146  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2147  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2148  NVENCSTATUS nv_status;
2149 
2150  int i, first_round;
2151 
2152  if (ctx->nb_registered_frames == FF_ARRAY_ELEMS(ctx->registered_frames)) {
2153  for (first_round = 1; first_round >= 0; first_round--) {
2154  for (i = 0; i < ctx->nb_registered_frames; i++) {
2155  if (!ctx->registered_frames[i].mapped) {
2156  if (ctx->registered_frames[i].regptr) {
2157  if (first_round)
2158  continue;
2159  nv_status = p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr);
2160  if (nv_status != NV_ENC_SUCCESS)
2161  return nvenc_print_error(avctx, nv_status, "Failed unregistering unused input resource");
2162  ctx->registered_frames[i].ptr = NULL;
2163  ctx->registered_frames[i].regptr = NULL;
2164  }
2165  return i;
2166  }
2167  }
2168  }
2169  } else {
2170  return ctx->nb_registered_frames++;
2171  }
2172 
2173  av_log(avctx, AV_LOG_ERROR, "Too many registered CUDA frames\n");
2174  return AVERROR(ENOMEM);
2175 }
2176 
2178 {
2179  NvencContext *ctx = avctx->priv_data;
2180  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2181  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2182 
2183  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frame->hw_frames_ctx->data;
2184  NV_ENC_REGISTER_RESOURCE reg = { 0 };
2185  int i, idx, ret;
2186 
2187  for (i = 0; i < ctx->nb_registered_frames; i++) {
2188  if (avctx->pix_fmt == AV_PIX_FMT_CUDA && ctx->registered_frames[i].ptr == frame->data[0])
2189  return i;
2190  else if (avctx->pix_fmt == AV_PIX_FMT_D3D11 && ctx->registered_frames[i].ptr == frame->data[0] && ctx->registered_frames[i].ptr_index == (intptr_t)frame->data[1])
2191  return i;
2192  }
2193 
2194  idx = nvenc_find_free_reg_resource(avctx);
2195  if (idx < 0)
2196  return idx;
2197 
2198  reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2199  reg.width = frames_ctx->width;
2200  reg.height = frames_ctx->height;
2201  reg.pitch = frame->linesize[0];
2202  reg.resourceToRegister = frame->data[0];
2203 
2204  if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
2205  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2206  }
2207  else if (avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2208  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2209  reg.subResourceIndex = (intptr_t)frame->data[1];
2210  }
2211 
2212  reg.bufferFormat = nvenc_map_buffer_format(frames_ctx->sw_format);
2213  if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2214  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
2215  av_get_pix_fmt_name(frames_ctx->sw_format));
2216  return AVERROR(EINVAL);
2217  }
2218 
2219  ret = p_nvenc->nvEncRegisterResource(ctx->nvencoder, &reg);
2220  if (ret != NV_ENC_SUCCESS) {
2221  nvenc_print_error(avctx, ret, "Error registering an input resource");
2222  return AVERROR_UNKNOWN;
2223  }
2224 
2225  ctx->registered_frames[idx].ptr = frame->data[0];
2226  ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2227  ctx->registered_frames[idx].regptr = reg.registeredResource;
2228  return idx;
2229 }
2230 
2232  NvencSurface *nvenc_frame)
2233 {
2234  NvencContext *ctx = avctx->priv_data;
2235  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2236  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2237 
2238  int res;
2239  NVENCSTATUS nv_status;
2240 
2241  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2242  int reg_idx = nvenc_register_frame(avctx, frame);
2243  if (reg_idx < 0) {
2244  av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n");
2245  return reg_idx;
2246  }
2247 
2248  res = av_frame_ref(nvenc_frame->in_ref, frame);
2249  if (res < 0)
2250  return res;
2251 
2252  if (!ctx->registered_frames[reg_idx].mapped) {
2253  ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2254  ctx->registered_frames[reg_idx].in_map.registeredResource = ctx->registered_frames[reg_idx].regptr;
2255  nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &ctx->registered_frames[reg_idx].in_map);
2256  if (nv_status != NV_ENC_SUCCESS) {
2257  av_frame_unref(nvenc_frame->in_ref);
2258  return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");
2259  }
2260  }
2261 
2262  ctx->registered_frames[reg_idx].mapped += 1;
2263 
2264  nvenc_frame->reg_idx = reg_idx;
2265  nvenc_frame->input_surface = ctx->registered_frames[reg_idx].in_map.mappedResource;
2266  nvenc_frame->format = ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2267  nvenc_frame->pitch = frame->linesize[0];
2268 
2269  return 0;
2270  } else {
2271  NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2272 
2273  lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2274  lockBufferParams.inputBuffer = nvenc_frame->input_surface;
2275 
2276  nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
2277  if (nv_status != NV_ENC_SUCCESS) {
2278  return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer");
2279  }
2280 
2281  nvenc_frame->pitch = lockBufferParams.pitch;
2282  res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame);
2283 
2284  nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, nvenc_frame->input_surface);
2285  if (nv_status != NV_ENC_SUCCESS) {
2286  return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!");
2287  }
2288 
2289  return res;
2290  }
2291 }
2292 
2294  NV_ENC_PIC_PARAMS *params,
2295  NV_ENC_SEI_PAYLOAD *sei_data,
2296  int sei_count)
2297 {
2298  NvencContext *ctx = avctx->priv_data;
2299 
2300  switch (avctx->codec->id) {
2301  case AV_CODEC_ID_H264:
2302  params->codecPicParams.h264PicParams.sliceMode =
2303  ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2304  params->codecPicParams.h264PicParams.sliceModeData =
2305  ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2306  if (sei_count > 0) {
2307  params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2308  params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2309  }
2310 
2311  break;
2312  case AV_CODEC_ID_HEVC:
2313  params->codecPicParams.hevcPicParams.sliceMode =
2314  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2315  params->codecPicParams.hevcPicParams.sliceModeData =
2316  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2317  if (sei_count > 0) {
2318  params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2319  params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2320  }
2321 
2322  break;
2323 #if CONFIG_AV1_NVENC_ENCODER
2324  case AV_CODEC_ID_AV1:
2325  params->codecPicParams.av1PicParams.numTileColumns =
2326  ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2327  params->codecPicParams.av1PicParams.numTileRows =
2328  ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2329  if (sei_count > 0) {
2330  params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2331  params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2332  }
2333 
2334  break;
2335 #endif
2336  }
2337 }
2338 
2339 static inline void timestamp_queue_enqueue(AVFifo *queue, int64_t timestamp)
2340 {
2341  av_fifo_write(queue, &timestamp, 1);
2342 }
2343 
2345 {
2346  int64_t timestamp = AV_NOPTS_VALUE;
2347  // The following call might fail if the queue is empty.
2348  av_fifo_read(queue, &timestamp, 1);
2349 
2350  return timestamp;
2351 }
2352 
2353 static inline int64_t timestamp_queue_peek(AVFifo *queue, size_t index)
2354 {
2355  int64_t timestamp = AV_NOPTS_VALUE;
2356  av_fifo_peek(queue, &timestamp, 1, index);
2357 
2358  return timestamp;
2359 }
2360 
2362  NV_ENC_LOCK_BITSTREAM *params,
2363  AVPacket *pkt)
2364 {
2365  NvencContext *ctx = avctx->priv_data;
2366  unsigned int delay;
2367  int64_t delay_time;
2368 
2369  pkt->pts = params->outputTimeStamp;
2370 
2371  if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER)) {
2372  pkt->dts = pkt->pts;
2373  return 0;
2374  }
2375 
2376  // This can be more than necessary, but we don't know the real reorder delay.
2377  delay = FFMAX(ctx->encode_config.frameIntervalP - 1, 0);
2378  if (ctx->output_frame_num >= delay) {
2379  pkt->dts = timestamp_queue_dequeue(ctx->timestamp_list);
2380  ctx->output_frame_num++;
2381  return 0;
2382  }
2383 
2384  delay_time = ctx->initial_delay_time;
2385  if (!delay_time) {
2386  int64_t t1, t2, t3;
2387  t1 = timestamp_queue_peek(ctx->timestamp_list, delay);
2388  t2 = timestamp_queue_peek(ctx->timestamp_list, 0);
2389  t3 = (delay > 1) ? timestamp_queue_peek(ctx->timestamp_list, 1) : t1;
2390 
2391  if (t1 != AV_NOPTS_VALUE) {
2392  delay_time = t1 - t2;
2393  } else if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
2394  delay_time = av_rescale_q(delay, (AVRational) {avctx->framerate.den, avctx->framerate.num},
2395  avctx->time_base);
2396  } else if (t3 != AV_NOPTS_VALUE) {
2397  delay_time = delay * (t3 - t2);
2398  } else {
2399  delay_time = delay;
2400  }
2401  ctx->initial_delay_time = delay_time;
2402  }
2403 
2404  /* The following method is simple, but doesn't guarantee monotonic with VFR
2405  * when delay_time isn't accurate (that is, t1 == AV_NOPTS_VALUE)
2406  *
2407  * dts = timestamp_queue_peek(ctx->timestamp_list, ctx->output_frame_num) - delay_time
2408  */
2409  pkt->dts = timestamp_queue_peek(ctx->timestamp_list, 0) - delay_time * (delay - ctx->output_frame_num) / delay;
2410  ctx->output_frame_num++;
2411 
2412  return 0;
2413 }
2414 
2415 static int nvenc_store_frame_data(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *pic_params, const AVFrame *frame)
2416 {
2417  NvencContext *ctx = avctx->priv_data;
2418  int res = 0;
2419 
2420  int idx = ctx->frame_data_array_pos;
2421  NvencFrameData *frame_data = &ctx->frame_data_array[idx];
2422 
2423  // in case the encoder got reconfigured, there might be leftovers
2425 
2426  if (frame->opaque_ref && avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
2429  return AVERROR(ENOMEM);
2430  }
2431 
2432  frame_data->duration = frame->duration;
2433  frame_data->frame_opaque = frame->opaque;
2434 
2435  ctx->frame_data_array_pos = (ctx->frame_data_array_pos + 1) % ctx->frame_data_array_nb;
2436  pic_params->inputDuration = idx;
2437 
2438  return res;
2439 }
2440 
2441 static int nvenc_retrieve_frame_data(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *lock_params, AVPacket *pkt)
2442 {
2443  NvencContext *ctx = avctx->priv_data;
2444  int res = 0;
2445 
2446  int idx = lock_params->outputDuration;
2447  NvencFrameData *frame_data = &ctx->frame_data_array[idx];
2448 
2450 
2451  if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
2455  }
2456 
2458 
2459  return res;
2460 }
2461 
2463 {
2464  NvencContext *ctx = avctx->priv_data;
2465  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2466  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2467 
2468  NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2469  NVENCSTATUS nv_status;
2470  int res = 0;
2471 
2472  enum AVPictureType pict_type;
2473 
2474  lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2475 
2476  lock_params.doNotWait = 0;
2477  lock_params.outputBitstream = tmpoutsurf->output_surface;
2478 
2479  nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
2480  if (nv_status != NV_ENC_SUCCESS) {
2481  res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer");
2482  goto error;
2483  }
2484 
2485  res = ff_get_encode_buffer(avctx, pkt, lock_params.bitstreamSizeInBytes, 0);
2486 
2487  if (res < 0) {
2488  p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
2489  goto error;
2490  }
2491 
2492  memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2493 
2494  nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
2495  if (nv_status != NV_ENC_SUCCESS) {
2496  res = nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");
2497  goto error;
2498  }
2499 
2500 
2501  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2502  ctx->registered_frames[tmpoutsurf->reg_idx].mapped -= 1;
2503  if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped == 0) {
2504  nv_status = p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[tmpoutsurf->reg_idx].in_map.mappedResource);
2505  if (nv_status != NV_ENC_SUCCESS) {
2506  res = nvenc_print_error(avctx, nv_status, "Failed unmapping input resource");
2507  goto error;
2508  }
2509  } else if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped < 0) {
2510  res = AVERROR_BUG;
2511  goto error;
2512  }
2513 
2514  av_frame_unref(tmpoutsurf->in_ref);
2515 
2516  tmpoutsurf->input_surface = NULL;
2517  }
2518 
2519  switch (lock_params.pictureType) {
2520  case NV_ENC_PIC_TYPE_IDR:
2522  case NV_ENC_PIC_TYPE_I:
2523  pict_type = AV_PICTURE_TYPE_I;
2524  break;
2525  case NV_ENC_PIC_TYPE_P:
2526  pict_type = AV_PICTURE_TYPE_P;
2527  break;
2528  case NV_ENC_PIC_TYPE_B:
2529  pict_type = AV_PICTURE_TYPE_B;
2530  break;
2531  case NV_ENC_PIC_TYPE_BI:
2532  pict_type = AV_PICTURE_TYPE_BI;
2533  break;
2534  default:
2535  av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
2536  av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
2537  res = AVERROR_EXTERNAL;
2538  goto error;
2539  }
2540 
2542  (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type);
2543 
2544  res = nvenc_set_timestamp(avctx, &lock_params, pkt);
2545  if (res < 0)
2546  goto error2;
2547 
2548  res = nvenc_retrieve_frame_data(avctx, &lock_params, pkt);
2549  if (res < 0)
2550  goto error2;
2551 
2552  return 0;
2553 
2554 error:
2555  timestamp_queue_dequeue(ctx->timestamp_list);
2556 
2557 error2:
2558  return res;
2559 }
2560 
2561 static int output_ready(AVCodecContext *avctx, int flush)
2562 {
2563  NvencContext *ctx = avctx->priv_data;
2564  int nb_ready, nb_pending;
2565 
2566  nb_ready = av_fifo_can_read(ctx->output_surface_ready_queue);
2567  nb_pending = av_fifo_can_read(ctx->output_surface_queue);
2568  if (flush)
2569  return nb_ready > 0;
2570  return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth);
2571 }
2572 
2574 {
2575  NvencContext *ctx = avctx->priv_data;
2576  int sei_count = 0;
2577  int i, res;
2578 
2580  void *a53_data = NULL;
2581  size_t a53_size = 0;
2582 
2583  if (ff_alloc_a53_sei(frame, 0, &a53_data, &a53_size) < 0) {
2584  av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2585  }
2586 
2587  if (a53_data) {
2588  void *tmp = av_fast_realloc(ctx->sei_data,
2589  &ctx->sei_data_size,
2590  (sei_count + 1) * sizeof(*ctx->sei_data));
2591  if (!tmp) {
2592  av_free(a53_data);
2593  res = AVERROR(ENOMEM);
2594  goto error;
2595  } else {
2596  ctx->sei_data = tmp;
2597  ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2598  ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2599 
2600 #if CONFIG_AV1_NVENC_ENCODER
2601  if (avctx->codec->id == AV_CODEC_ID_AV1)
2602  ctx->sei_data[sei_count].payloadType = AV1_METADATA_TYPE_ITUT_T35;
2603  else
2604 #endif
2605  ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35;
2606 
2607  sei_count++;
2608  }
2609  }
2610  }
2611 
2613  void *tc_data = NULL;
2614  size_t tc_size = 0;
2615 
2616  if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, &tc_data, &tc_size) < 0) {
2617  av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n");
2618  }
2619 
2620  if (tc_data) {
2621  void *tmp = av_fast_realloc(ctx->sei_data,
2622  &ctx->sei_data_size,
2623  (sei_count + 1) * sizeof(*ctx->sei_data));
2624  if (!tmp) {
2625  av_free(tc_data);
2626  res = AVERROR(ENOMEM);
2627  goto error;
2628  } else {
2629  ctx->sei_data = tmp;
2630  ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2631  ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2632 
2633 #if CONFIG_AV1_NVENC_ENCODER
2634  if (avctx->codec->id == AV_CODEC_ID_AV1)
2635  ctx->sei_data[sei_count].payloadType = AV1_METADATA_TYPE_TIMECODE;
2636  else
2637 #endif
2638  ctx->sei_data[sei_count].payloadType = SEI_TYPE_TIME_CODE;
2639 
2640  sei_count++;
2641  }
2642  }
2643  }
2644 
2645  if (!ctx->udu_sei)
2646  return sei_count;
2647 
2648  for (i = 0; i < frame->nb_side_data; i++) {
2649  AVFrameSideData *side_data = frame->side_data[i];
2650  void *tmp;
2651 
2652  if (side_data->type != AV_FRAME_DATA_SEI_UNREGISTERED)
2653  continue;
2654 
2655  tmp = av_fast_realloc(ctx->sei_data,
2656  &ctx->sei_data_size,
2657  (sei_count + 1) * sizeof(*ctx->sei_data));
2658  if (!tmp) {
2659  res = AVERROR(ENOMEM);
2660  goto error;
2661  } else {
2662  ctx->sei_data = tmp;
2663  ctx->sei_data[sei_count].payloadSize = side_data->size;
2664  ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_UNREGISTERED;
2665  ctx->sei_data[sei_count].payload = av_memdup(side_data->data, side_data->size);
2666 
2667  if (!ctx->sei_data[sei_count].payload) {
2668  res = AVERROR(ENOMEM);
2669  goto error;
2670  }
2671 
2672  sei_count++;
2673  }
2674  }
2675 
2676  return sei_count;
2677 
2678 error:
2679  for (i = 0; i < sei_count; i++)
2680  av_freep(&(ctx->sei_data[i].payload));
2681 
2682  return res;
2683 }
2684 
2685 static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
2686 {
2687  NvencContext *ctx = avctx->priv_data;
2688  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
2689  NVENCSTATUS ret;
2690 
2691  NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2692  int needs_reconfig = 0;
2693  int needs_encode_config = 0;
2694  int reconfig_bitrate = 0, reconfig_dar = 0;
2695  int dw, dh;
2696 
2697  params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2698  params.reInitEncodeParams = ctx->init_encode_params;
2699 
2700  compute_dar(avctx, &dw, &dh);
2701  if (dw != ctx->init_encode_params.darWidth || dh != ctx->init_encode_params.darHeight) {
2702  av_log(avctx, AV_LOG_VERBOSE,
2703  "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2704  ctx->init_encode_params.darWidth,
2705  ctx->init_encode_params.darHeight, dw, dh);
2706 
2707  params.reInitEncodeParams.darHeight = dh;
2708  params.reInitEncodeParams.darWidth = dw;
2709 
2710  needs_reconfig = 1;
2711  reconfig_dar = 1;
2712  }
2713 
2714  if (ctx->rc != NV_ENC_PARAMS_RC_CONSTQP && ctx->support_dyn_bitrate) {
2715  if (avctx->bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->bit_rate) {
2716  av_log(avctx, AV_LOG_VERBOSE,
2717  "avg bitrate change: %d -> %d\n",
2718  params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2719  (uint32_t)avctx->bit_rate);
2720 
2721  params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->bit_rate;
2722  reconfig_bitrate = 1;
2723  }
2724 
2725  if (avctx->rc_max_rate > 0 && ctx->encode_config.rcParams.maxBitRate != avctx->rc_max_rate) {
2726  av_log(avctx, AV_LOG_VERBOSE,
2727  "max bitrate change: %d -> %d\n",
2728  params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2729  (uint32_t)avctx->rc_max_rate);
2730 
2731  params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->rc_max_rate;
2732  reconfig_bitrate = 1;
2733  }
2734 
2735  if (avctx->rc_buffer_size > 0 && ctx->encode_config.rcParams.vbvBufferSize != avctx->rc_buffer_size) {
2736  av_log(avctx, AV_LOG_VERBOSE,
2737  "vbv buffer size change: %d -> %d\n",
2738  params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2739  avctx->rc_buffer_size);
2740 
2741  params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->rc_buffer_size;
2742  reconfig_bitrate = 1;
2743  }
2744 
2745  if (reconfig_bitrate) {
2746  params.resetEncoder = 1;
2747  params.forceIDR = 1;
2748 
2749  needs_encode_config = 1;
2750  needs_reconfig = 1;
2751  }
2752  }
2753 
2754  if (!needs_encode_config)
2755  params.reInitEncodeParams.encodeConfig = NULL;
2756 
2757  if (needs_reconfig) {
2758  ret = p_nvenc->nvEncReconfigureEncoder(ctx->nvencoder, &params);
2759  if (ret != NV_ENC_SUCCESS) {
2760  nvenc_print_error(avctx, ret, "failed to reconfigure nvenc");
2761  } else {
2762  if (reconfig_dar) {
2763  ctx->init_encode_params.darHeight = dh;
2764  ctx->init_encode_params.darWidth = dw;
2765  }
2766 
2767  if (reconfig_bitrate) {
2768  ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2769  ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2770  ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2771  }
2772 
2773  }
2774  }
2775 }
2776 
2777 static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
2778 {
2779  NVENCSTATUS nv_status;
2780  NvencSurface *tmp_out_surf, *in_surf;
2781  int res, res2;
2782  int sei_count = 0;
2783  int i;
2784 
2785  NvencContext *ctx = avctx->priv_data;
2786  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2787  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2788 
2789  NV_ENC_PIC_PARAMS pic_params = { 0 };
2790  pic_params.version = NV_ENC_PIC_PARAMS_VER;
2791 
2792  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
2793  return AVERROR(EINVAL);
2794 
2795  if (frame && frame->buf[0]) {
2796  in_surf = get_free_frame(ctx);
2797  if (!in_surf)
2798  return AVERROR(EAGAIN);
2799 
2800  res = nvenc_push_context(avctx);
2801  if (res < 0)
2802  return res;
2803 
2804  reconfig_encoder(avctx, frame);
2805 
2806  res = nvenc_upload_frame(avctx, frame, in_surf);
2807 
2808  res2 = nvenc_pop_context(avctx);
2809  if (res2 < 0)
2810  return res2;
2811 
2812  if (res)
2813  return res;
2814 
2815  pic_params.inputBuffer = in_surf->input_surface;
2816  pic_params.bufferFmt = in_surf->format;
2817  pic_params.inputWidth = in_surf->width;
2818  pic_params.inputHeight = in_surf->height;
2819  pic_params.inputPitch = in_surf->pitch;
2820  pic_params.outputBitstream = in_surf->output_surface;
2821 
2822  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2823  if (frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)
2824  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2825  else
2826  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2827  } else {
2828  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2829  }
2830 
2831  if (ctx->forced_idr >= 0 && frame->pict_type == AV_PICTURE_TYPE_I) {
2832  pic_params.encodePicFlags =
2833  ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2834  } else {
2835  pic_params.encodePicFlags = 0;
2836  }
2837 
2838  pic_params.frameIdx = ctx->frame_idx_counter++;
2839  pic_params.inputTimeStamp = frame->pts;
2840 
2841  if (ctx->extra_sei) {
2842  res = prepare_sei_data_array(avctx, frame);
2843  if (res < 0)
2844  return res;
2845  sei_count = res;
2846  }
2847 
2848  res = nvenc_store_frame_data(avctx, &pic_params, frame);
2849  if (res < 0)
2850  return res;
2851 
2852  nvenc_codec_specific_pic_params(avctx, &pic_params, ctx->sei_data, sei_count);
2853  } else {
2854  pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2855  }
2856 
2857  res = nvenc_push_context(avctx);
2858  if (res < 0)
2859  return res;
2860 
2861  nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
2862 
2863  for (i = 0; i < sei_count; i++)
2864  av_freep(&(ctx->sei_data[i].payload));
2865 
2866  res = nvenc_pop_context(avctx);
2867  if (res < 0)
2868  return res;
2869 
2870  if (nv_status != NV_ENC_SUCCESS &&
2871  nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2872  return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
2873 
2874  if (frame && frame->buf[0]) {
2875  av_fifo_write(ctx->output_surface_queue, &in_surf, 1);
2876 
2878  timestamp_queue_enqueue(ctx->timestamp_list, frame->pts);
2879  }
2880 
2881  /* all the pending buffers are now ready for output */
2882  if (nv_status == NV_ENC_SUCCESS) {
2883  while (av_fifo_read(ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2884  av_fifo_write(ctx->output_surface_ready_queue, &tmp_out_surf, 1);
2885  }
2886 
2887  return 0;
2888 }
2889 
2891 {
2892  NvencSurface *tmp_out_surf;
2893  int res, res2;
2894 
2895  NvencContext *ctx = avctx->priv_data;
2896 
2897  AVFrame *frame = ctx->frame;
2898 
2899  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
2900  return AVERROR(EINVAL);
2901 
2902  if (!frame->buf[0]) {
2903  res = ff_encode_get_frame(avctx, frame);
2904  if (res < 0 && res != AVERROR_EOF)
2905  return res;
2906  }
2907 
2908  res = nvenc_send_frame(avctx, frame);
2909  if (res < 0) {
2910  if (res != AVERROR(EAGAIN))
2911  return res;
2912  } else
2914 
2915  if (output_ready(avctx, avctx->internal->draining)) {
2916  av_fifo_read(ctx->output_surface_ready_queue, &tmp_out_surf, 1);
2917 
2918  res = nvenc_push_context(avctx);
2919  if (res < 0)
2920  return res;
2921 
2922  res = process_output_surface(avctx, pkt, tmp_out_surf);
2923 
2924  res2 = nvenc_pop_context(avctx);
2925  if (res2 < 0)
2926  return res2;
2927 
2928  if (res)
2929  return res;
2930 
2931  av_fifo_write(ctx->unused_surface_queue, &tmp_out_surf, 1);
2932  } else if (avctx->internal->draining) {
2933  return AVERROR_EOF;
2934  } else {
2935  return AVERROR(EAGAIN);
2936  }
2937 
2938  return 0;
2939 }
2940 
2942 {
2943  NvencContext *ctx = avctx->priv_data;
2944 
2945  nvenc_send_frame(avctx, NULL);
2946  av_fifo_reset2(ctx->timestamp_list);
2947  ctx->output_frame_num = 0;
2948  ctx->initial_delay_time = 0;
2949 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
AVHWDeviceContext::hwctx
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:85
ANY_DEVICE
@ ANY_DEVICE
Definition: nvenc.h:174
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
ff_alloc_a53_sei
int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len, void **data, size_t *sei_size)
Check AVFrame for A53 side data and allocate and fill SEI message with A53 info.
Definition: atsc_a53.c:26
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
PRESET_ALIAS
#define PRESET_ALIAS(alias, name,...)
Definition: nvenc.c:184
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:260
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
GUIDTuple::guid
const GUID guid
Definition: nvenc.c:180
level
uint8_t level
Definition: svq3.c:205
av_clip
#define av_clip
Definition: common.h:100
NVENC_DEPRECATED_PRESET
@ NVENC_DEPRECATED_PRESET
Definition: nvenc.h:169
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
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:691
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:963
AV_PIX_FMT_BGR32
#define AV_PIX_FMT_BGR32
Definition: pixfmt.h:477
GUIDTuple
Definition: nvenc.c:179
NONE
@ NONE
Definition: af_afade.c:60
GUIDTuple::flags
int flags
Definition: nvenc.c:181
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AV_FRAME_DATA_A53_CC
@ AV_FRAME_DATA_A53_CC
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:59
AV_PROFILE_H264_MAIN
#define AV_PROFILE_H264_MAIN
Definition: defs.h:112
nvenc_push_context
static int nvenc_push_context(AVCodecContext *avctx)
Definition: nvenc.c:361
NV_ENC_H264_PROFILE_HIGH
@ NV_ENC_H264_PROFILE_HIGH
Definition: nvenc.h:153
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:197
AVPictureType
AVPictureType
Definition: avutil.h:277
output_ready
static int output_ready(AVCodecContext *avctx, int flush)
Definition: nvenc.c:2561
NvencContext
Definition: nvenc.h:183
AVCodecContext::codec_descriptor
const struct AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
Definition: avcodec.h:1872
int64_t
long long int64_t
Definition: coverity.c:34
AV_FRAME_DATA_S12M_TIMECODE
@ AV_FRAME_DATA_S12M_TIMECODE
Timecode which conforms to SMPTE ST 12-1.
Definition: frame.h:152
AV_PROFILE_HEVC_MAIN
#define AV_PROFILE_HEVC_MAIN
Definition: defs.h:159
NvencSurface::in_ref
AVFrame * in_ref
Definition: nvenc.h:99
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
NVENC_TWO_PASSES
@ NVENC_TWO_PASSES
Definition: nvenc.h:167
nvenc_store_frame_data
static int nvenc_store_frame_data(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *pic_params, const AVFrame *frame)
Definition: nvenc.c:2415
nvenc_errors
static const struct @185 nvenc_errors[]
av_fifo_peek
int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
Read data from a FIFO without modifying FIFO state.
Definition: fifo.c:255
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
pixdesc.h
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:684
nvenc_set_timestamp
static int nvenc_set_timestamp(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *params, AVPacket *pkt)
Definition: nvenc.c:2361
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:717
P1
#define P1
Definition: cavsdsp.c:37
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:539
encode.h
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:817
NvencFrameData
Definition: nvenc.h:109
reconfig_encoder
static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2685
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:641
timestamp_queue_peek
static int64_t timestamp_queue_peek(AVFifo *queue, size_t index)
Definition: nvenc.c:2353
ff_nvenc_pix_fmts
enum AVPixelFormat ff_nvenc_pix_fmts[]
Definition: nvenc.c:56
set_constqp
static av_cold void set_constqp(AVCodecContext *avctx)
Definition: nvenc.c:832
NvencSurface
Definition: nvenc.h:96
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
mathematics.h
AV1_METADATA_TYPE_ITUT_T35
@ AV1_METADATA_TYPE_ITUT_T35
Definition: av1.h:47
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
NV_ENC_HEVC_PROFILE_REXT
@ NV_ENC_HEVC_PROFILE_REXT
Definition: nvenc.h:160
nvenc_print_error
static int nvenc_print_error(AVCodecContext *avctx, NVENCSTATUS err, const char *error_string)
Definition: nvenc.c:159
BD
#define BD
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AVCodecContext::qmax
int qmax
maximum quantizer
Definition: avcodec.h:1273
nverr
NVENCSTATUS nverr
Definition: nvenc.c:112
set_lossless
static av_cold void set_lossless(AVCodecContext *avctx)
Definition: nvenc.c:937
PRESET
#define PRESET(name,...)
Definition: nvenc.c:187
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
ff_nvenc_encode_flush
av_cold void ff_nvenc_encode_flush(AVCodecContext *avctx)
Definition: nvenc.c:2941
NV_ENC_H264_PROFILE_MAIN
@ NV_ENC_H264_PROFILE_MAIN
Definition: nvenc.h:152
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:338
nvenc.h
AV_FRAME_FLAG_TOP_FIELD_FIRST
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition: frame.h:653
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:304
AV_HWDEVICE_TYPE_CUDA
@ AV_HWDEVICE_TYPE_CUDA
Definition: hwcontext.h:30
compute_dar
static void compute_dar(AVCodecContext *avctx, int *dw, int *dh)
Definition: nvenc.c:1583
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:566
AVCOL_SPC_BT470BG
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:646
nvenc_upload_frame
static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame, NvencSurface *nvenc_frame)
Definition: nvenc.c:2231
NvencDynLoadFunctions::nvenc_device_count
int nvenc_device_count
Definition: nvenc.h:123
AV_CODEC_FLAG_COPY_OPAQUE
#define AV_CODEC_FLAG_COPY_OPAQUE
Definition: avcodec.h:299
NV_ENC_H264_PROFILE_HIGH_444P
@ NV_ENC_H264_PROFILE_HIGH_444P
Definition: nvenc.h:154
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:826
set_vbr
static av_cold void set_vbr(AVCodecContext *avctx)
Definition: nvenc.c:870
nvenc_map_error
static int nvenc_map_error(NVENCSTATUS err, const char **desc)
Definition: nvenc.c:144
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:460
AVPacket::opaque_ref
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
Definition: packet.h:575
NVENC_RGB_MODE_DISABLED
@ NVENC_RGB_MODE_DISABLED
Definition: nvenc.h:178
nvenc_check_cap
static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
Definition: nvenc.c:446
presets
static const Preset presets[]
Definition: vf_pseudocolor.c:286
fail
#define fail()
Definition: checkasm.h:189
av_fifo_write
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition: fifo.c:188
dummy
int dummy
Definition: motion.c:66
NvencSurface::format
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.h:106
nvenc_setup_rate_control
static av_cold int nvenc_setup_rate_control(AVCodecContext *avctx)
Definition: nvenc.c:1029
sei.h
AVCodecContext::refs
int refs
number of reference frames
Definition: avcodec.h:721
AV_HWDEVICE_TYPE_D3D11VA
@ AV_HWDEVICE_TYPE_D3D11VA
Definition: hwcontext.h:35
nvenc_map_preset
static void nvenc_map_preset(NvencContext *ctx)
Definition: nvenc.c:189
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:508
val
static double val(void *priv, double ch)
Definition: aeval.c:77
nvenc_copy_frame
static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface, NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame)
Definition: nvenc.c:2113
AVERROR_BUFFER_TOO_SMALL
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:53
hwcontext_cuda.h
av_image_fill_pointers
int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height, uint8_t *ptr, const int linesizes[4])
Fill plane data pointers for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:145
IS_GBRP
#define IS_GBRP(pix_fmt)
Definition: nvenc.c:108
AVCUDADeviceContext::cuda_ctx
CUcontext cuda_ctx
Definition: hwcontext_cuda.h:43
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
nvenc_print_driver_requirement
static void nvenc_print_driver_requirement(AVCodecContext *avctx, int level)
Definition: nvenc.c:243
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_CODEC_FLAG_INTERLACED_DCT
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:330
nvenc_check_capabilities
static int nvenc_check_capabilities(AVCodecContext *avctx)
Definition: nvenc.c:463
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:677
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
AVFrameSideData::size
size_t size
Definition: frame.h:268
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:86
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:530
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:729
ff_nvenc_encode_init
av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:2050
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1249
AVD3D11VADeviceContext::device
ID3D11Device * device
Device used for texture creation and access.
Definition: hwcontext_d3d11va.h:56
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:515
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1451
AV_PIX_FMT_0BGR32
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:480
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
NvencDynLoadFunctions
Definition: nvenc.h:117
ctx
AVFormatContext * ctx
Definition: movenc.c:49
SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
@ SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
Definition: sei.h:34
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
nvenc_setup_extradata
static av_cold int nvenc_setup_extradata(AVCodecContext *avctx)
Definition: nvenc.c:1931
timestamp_queue_enqueue
static void timestamp_queue_enqueue(AVFifo *queue, int64_t timestamp)
Definition: nvenc.c:2339
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AVCodecContext::rc_max_rate
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1302
timestamp_queue_dequeue
static int64_t timestamp_queue_dequeue(AVFifo *queue)
Definition: nvenc.c:2344
AVPacket::opaque
void * opaque
for some private data of the user
Definition: packet.h:564
NvencDynLoadFunctions::nvenc_dl
NvencFunctions * nvenc_dl
Definition: nvenc.h:120
AVCPBProperties
This structure describes the bitrate properties of an encoded bitstream.
Definition: defs.h:271
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
NvencSurface::pitch
int pitch
Definition: nvenc.h:103
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:87
NvencSurface::input_surface
NV_ENC_INPUT_PTR input_surface
Definition: nvenc.h:98
AVCodecDescriptor::props
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition: codec_desc.h:54
if
if(ret)
Definition: filter_design.txt:179
AVCodecContext::rc_buffer_size
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1287
NVENC_CAP
#define NVENC_CAP
Definition: nvenc.c:46
AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:521
IS_10BIT
#define IS_10BIT(pix_fmt)
Definition: nvenc.c:88
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
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
NvencSurface::reg_idx
int reg_idx
Definition: nvenc.h:100
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:701
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
SEI_TYPE_TIME_CODE
@ SEI_TYPE_TIME_CODE
Definition: sei.h:95
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:284
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:85
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:501
ff_nvenc_encode_close
av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:1965
FrameData::duration
int64_t duration
Definition: librav1e.c:60
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
P3
#define P3
Definition: dsp_template.c:801
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:370
FrameData::frame_opaque
void * frame_opaque
Definition: librav1e.c:62
NvencDynLoadFunctions::cuda_dl
CudaFunctions * cuda_dl
Definition: nvenc.h:119
nvenc_setup_h264_config
static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx)
Definition: nvenc.c:1207
AVPixFmtDescriptor::flags
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:94
AV_PROFILE_HEVC_MAIN_10
#define AV_PROFILE_HEVC_MAIN_10
Definition: defs.h:160
AV_PROFILE_HEVC_REXT
#define AV_PROFILE_HEVC_REXT
Definition: defs.h:162
index
int index
Definition: gxfenc.c:90
AV_FRAME_DATA_SEI_UNREGISTERED
@ AV_FRAME_DATA_SEI_UNREGISTERED
User data unregistered metadata associated with a video frame.
Definition: frame.h:178
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:550
av_fifo_reset2
void av_fifo_reset2(AVFifo *f)
Definition: fifo.c:280
AV_PIX_FMT_X2BGR10
#define AV_PIX_FMT_X2BGR10
Definition: pixfmt.h:564
AVCUDADeviceContext::stream
CUstream stream
Definition: hwcontext_cuda.h:44
desc
const char * desc
Definition: nvenc.c:114
nvenc_pop_context
static int nvenc_pop_context(AVCodecContext *avctx)
Definition: nvenc.c:372
HW_CONFIG_ENCODER_DEVICE
#define HW_CONFIG_ENCODER_DEVICE(format, device_type_)
Definition: hwconfig.h:95
AVFifo
Definition: fifo.c:35
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1037
height
#define height
Definition: dsp.h:85
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:400
nvenc_check_codec_support
static int nvenc_check_codec_support(AVCodecContext *avctx)
Definition: nvenc.c:410
AV_PIX_FMT_FLAG_RGB
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:136
AV_CODEC_PROP_REORDER
#define AV_CODEC_PROP_REORDER
Codec supports frame reordering.
Definition: codec_desc.h:92
ff_nvenc_hw_configs
const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[]
Definition: nvenc.c:78
MAX_REGISTERED_FRAMES
#define MAX_REGISTERED_FRAMES
Definition: nvenc.h:41
ff_alloc_timecode_sei
int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len, void **data, size_t *sei_size)
Check AVFrame for S12M timecode side data and allocate and fill TC SEI message with timecode info.
Definition: utils.c:974
P6
#define P6
Definition: filter_template.c:407
nvenc_alloc_surface
static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
Definition: nvenc.c:1829
NVENC_LOWLATENCY
@ NVENC_LOWLATENCY
Definition: nvenc.h:164
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFrameSideData::data
uint8_t * data
Definition: frame.h:267
nvenc_check_device
static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
Definition: nvenc.c:641
nvenc_register_frame
static int nvenc_register_frame(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2177
AVCodecHWConfigInternal
Definition: hwconfig.h:25
frame_data
FrameData * frame_data(AVFrame *frame)
Get our axiliary frame data attached to the frame, allocating it if needed.
Definition: ffmpeg.c:453
ff_nvenc_receive_packet
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: nvenc.c:2890
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:538
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:475
nvenc_override_rate_control
static void nvenc_override_rate_control(AVCodecContext *avctx)
Definition: nvenc.c:951
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
AV_PIX_FMT_D3D11
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:336
FrameData::frame_opaque_ref
AVBufferRef * frame_opaque_ref
Definition: librav1e.c:63
AVCPBProperties::avg_bitrate
int64_t avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: defs.h:286
get_free_frame
static NvencSurface * get_free_frame(NvencContext *ctx)
Definition: nvenc.c:2102
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
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:810
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
nvenc_open_session
static av_cold int nvenc_open_session(AVCodecContext *avctx)
Definition: nvenc.c:384
HW_CONFIG_ENCODER_FRAMES
#define HW_CONFIG_ENCODER_FRAMES(format, device_type_)
Definition: hwconfig.h:98
NVENC_ONE_PASS
@ NVENC_ONE_PASS
Definition: nvenc.h:166
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
FAST
@ FAST
Definition: vf_guided.c:32
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:529
process_output_surface
static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)
Definition: nvenc.c:2462
nvenc_load_libraries
static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
Definition: nvenc.c:317
nvenc_recalc_surfaces
static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx)
Definition: nvenc.c:986
AVD3D11VADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_d3d11va.h:45
IS_RGB
#define IS_RGB(pix_fmt)
Definition: nvenc.c:95
AVCPBProperties::max_bitrate
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: defs.h:276
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:228
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:598
prepare_sei_data_array
static int prepare_sei_data_array(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2573
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
NV_ENC_H264_PROFILE_BASELINE
@ NV_ENC_H264_PROFILE_BASELINE
Definition: nvenc.h:151
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
AV_PIX_FMT_X2RGB10
#define AV_PIX_FMT_X2RGB10
Definition: pixfmt.h:563
NV_ENC_HEVC_PROFILE_MAIN
@ NV_ENC_HEVC_PROFILE_MAIN
Definition: nvenc.h:158
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
AVCodecContext::hw_device_ctx
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:1507
IS_YUV444
#define IS_YUV444(pix_fmt)
Definition: nvenc.c:102
P2
#define P2
Definition: cavsdsp.c:36
IS_CBR
#define IS_CBR(rc)
Definition: nvenc.c:49
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
CHECK_CU
#define CHECK_CU(x)
Definition: nvenc.c:44
nvenc_map_buffer_format
static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt)
Definition: nvenc.c:1798
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
NVENC_LOSSLESS
@ NVENC_LOSSLESS
Definition: nvenc.h:165
AV_PIX_FMT_P016
#define AV_PIX_FMT_P016
Definition: pixfmt.h:554
AVCodecContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1485
NvencSurface::width
int width
Definition: nvenc.h:101
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
AVCUDADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_cuda.h:42
AV_PROFILE_H264_HIGH_444_PREDICTIVE
#define AV_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: defs.h:122
ret
ret
Definition: filter_design.txt:187
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:203
AVHWDeviceContext::type
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:72
nvenc_setup_encoder
static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
Definition: nvenc.c:1616
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
averr
int averr
Definition: nvenc.c:113
AV_PIX_FMT_0RGB32
#define AV_PIX_FMT_0RGB32
Definition: pixfmt.h:479
AVHWFramesContext::device_ctx
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:134
AVCPBProperties::buffer_size
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: defs.h:292
cuda_check.h
atsc_a53.h
AV_PROFILE_H264_BASELINE
#define AV_PROFILE_H264_BASELINE
Definition: defs.h:110
av_fifo_alloc2
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition: fifo.c:47
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
nvenc_codec_specific_pic_params
static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *params, NV_ENC_SEI_PAYLOAD *sei_data, int sei_count)
Definition: nvenc.c:2293
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AV_PROFILE_H264_HIGH
#define AV_PROFILE_H264_HIGH
Definition: defs.h:114
AV1_METADATA_TYPE_TIMECODE
@ AV1_METADATA_TYPE_TIMECODE
Definition: av1.h:48
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:281
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
NvencSurface::height
int height
Definition: nvenc.h:102
SEI_TYPE_USER_DATA_UNREGISTERED
@ SEI_TYPE_USER_DATA_UNREGISTERED
Definition: sei.h:35
av_image_copy2
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition: imgutils.h:184
LIST_DEVICES
@ LIST_DEVICES
Definition: nvenc.h:173
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1266
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1650
nvenc_setup_surfaces
static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx)
Definition: nvenc.c:1885
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:833
AVFrameSideData::type
enum AVFrameSideDataType type
Definition: frame.h:266
NvencSurface::output_surface
NV_ENC_OUTPUT_PTR output_surface
Definition: nvenc.h:105
AVCodecContext::ticks_per_frame
attribute_deprecated int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:582
nvenc_find_free_reg_resource
static int nvenc_find_free_reg_resource(AVCodecContext *avctx)
Definition: nvenc.c:2143
NV_ENC_HEVC_PROFILE_MAIN_10
@ NV_ENC_HEVC_PROFILE_MAIN_10
Definition: nvenc.h:159
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
P7
#define P7
Definition: filter_template.c:406
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:552
AVCodecInternal::draining
int draining
decoding: AVERROR_EOF has been returned from ff_decode_get_packet(); must not be used by decoders tha...
Definition: internal.h:139
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:165
NvencDynLoadFunctions::nvenc_funcs
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.h:122
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
mem.h
AVCodecContext::max_b_frames
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:801
ff_encode_get_frame
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition: encode.c:205
packet_internal.h
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:265
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
P4
#define P4
Definition: filter_template.c:409
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecContext::slices
int slices
Number of slices.
Definition: avcodec.h:1053
DEFAULT
#define DEFAULT
Definition: avdct.c:29
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AV_PICTURE_TYPE_BI
@ AV_PICTURE_TYPE_BI
BI type.
Definition: avutil.h:285
nvenc_setup_device
static av_cold int nvenc_setup_device(AVCodecContext *avctx)
Definition: nvenc.c:716
P5
#define P5
Definition: filter_template.c:408
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
hwcontext.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_fifo_freep2
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition: fifo.c:286
ff_side_data_set_encoder_stats
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: packet.c:609
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:58
ff_encode_add_cpb_side_data
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition: encode.c:909
nvenc_setup_codec_config
static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx)
Definition: nvenc.c:1566
width
#define width
Definition: dsp.h:85
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
AV_PROFILE_AV1_MAIN
#define AV_PROFILE_AV1_MAIN
Definition: defs.h:169
codec_desc.h
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:648
nvenc_setup_hevc_config
static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
Definition: nvenc.c:1340
RC_MODE_DEPRECATED
#define RC_MODE_DEPRECATED
Definition: nvenc.h:42
nvenc_send_frame
static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2777
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:3090
nvenc_retrieve_frame_data
static int nvenc_retrieve_frame_data(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *lock_params, AVPacket *pkt)
Definition: nvenc.c:2441