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  h264->chromaFormatIDC = avctx->profile == AV_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1;
1314 
1315  h264->level = ctx->level;
1316 
1317 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1318  h264->inputBitDepth = h264->outputBitDepth =
1319  IS_10BIT(ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1320 #endif
1321 
1322  if (ctx->coder >= 0)
1323  h264->entropyCodingMode = ctx->coder;
1324 
1325 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1326  if (ctx->b_ref_mode >= 0)
1327  h264->useBFramesAsRef = ctx->b_ref_mode;
1328 #endif
1329 
1330 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1331  h264->numRefL0 = avctx->refs;
1332  h264->numRefL1 = avctx->refs;
1333 #endif
1334 
1335  return 0;
1336 }
1337 
1339 {
1340  NvencContext *ctx = avctx->priv_data;
1341  NV_ENC_CONFIG *cc = &ctx->encode_config;
1342  NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1343  NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1344 
1345  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt);
1346 
1347  if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) {
1348  vui->colourMatrix = AVCOL_SPC_BT470BG;
1349  vui->colourPrimaries = avctx->color_primaries;
1350  vui->transferCharacteristics = avctx->color_trc;
1351  vui->videoFullRangeFlag = 0;
1352  } else {
1353  vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace;
1354  vui->colourPrimaries = avctx->color_primaries;
1355  vui->transferCharacteristics = avctx->color_trc;
1356  vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
1357  || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P);
1358  }
1359 
1360  vui->colourDescriptionPresentFlag =
1361  (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1362 
1363  vui->videoSignalTypePresentFlag =
1364  (vui->colourDescriptionPresentFlag
1365  || vui->videoFormat != 5
1366  || vui->videoFullRangeFlag != 0);
1367 
1368  if (ctx->max_slice_size > 0) {
1369  hevc->sliceMode = 1;
1370  hevc->sliceModeData = ctx->max_slice_size;
1371  } else {
1372  hevc->sliceMode = 3;
1373  hevc->sliceModeData = avctx->slices > 0 ? avctx->slices : 1;
1374  }
1375 
1376  if (ctx->intra_refresh) {
1377  hevc->enableIntraRefresh = 1;
1378  hevc->intraRefreshPeriod = cc->gopLength;
1379  hevc->intraRefreshCnt = cc->gopLength - 1;
1380  cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1381 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1382  hevc->singleSliceIntraRefresh = ctx->single_slice_intra_refresh;
1383 #endif
1384  }
1385 
1386 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1387  if (ctx->constrained_encoding)
1388  hevc->enableConstrainedEncoding = 1;
1389 #endif
1390 
1391  hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1392  hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1393  hevc->outputAUD = ctx->aud;
1394 
1395  if (ctx->dpb_size >= 0) {
1396  /* 0 means "let the hardware decide" */
1397  hevc->maxNumRefFramesInDPB = ctx->dpb_size;
1398  }
1399 
1400  hevc->idrPeriod = cc->gopLength;
1401 
1402  if (IS_CBR(cc->rcParams.rateControlMode)) {
1403  hevc->outputBufferingPeriodSEI = 1;
1404  }
1405 
1406  hevc->outputPictureTimingSEI = 1;
1407 
1408  switch (ctx->profile) {
1410  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1411  avctx->profile = AV_PROFILE_HEVC_MAIN;
1412  break;
1414  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1416  break;
1418  cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1419  avctx->profile = AV_PROFILE_HEVC_REXT;
1420  break;
1421  }
1422 
1423  // force setting profile as main10 if input is 10 bit or if it should be encoded as 10 bit
1424  if (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) {
1425  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1427  }
1428 
1429  // force setting profile as rext if input is yuv444
1430  if (IS_YUV444(ctx->data_pix_fmt)) {
1431  cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1432  avctx->profile = AV_PROFILE_HEVC_REXT;
1433  }
1434 
1435  hevc->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1;
1436 
1437 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1438  hevc->inputBitDepth = IS_10BIT(ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1439  hevc->outputBitDepth = (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1440 #else
1441  hevc->pixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0;
1442 #endif
1443 
1444  hevc->level = ctx->level;
1445 
1446  hevc->tier = ctx->tier;
1447 
1448 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1449  if (ctx->b_ref_mode >= 0)
1450  hevc->useBFramesAsRef = ctx->b_ref_mode;
1451 #endif
1452 
1453 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1454  hevc->numRefL0 = avctx->refs;
1455  hevc->numRefL1 = avctx->refs;
1456 #endif
1457 
1458 #ifdef NVENC_HAVE_TEMPORAL_FILTER
1459  if (ctx->tf_level >= 0) {
1460  hevc->tfLevel = ctx->tf_level;
1461 
1462  switch (ctx->tf_level)
1463  {
1464  case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1465  case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1466  break;
1467  default:
1468  av_log(avctx, AV_LOG_ERROR, "Invalid temporal filtering level.\n");
1469  return AVERROR(EINVAL);
1470  }
1471 
1472  if (ctx->encode_config.frameIntervalP < 5)
1473  av_log(avctx, AV_LOG_WARNING, "Temporal filtering needs at least 4 B-Frames (-bf 4).\n");
1474  }
1475 #endif
1476 
1477  return 0;
1478 }
1479 
1480 #if CONFIG_AV1_NVENC_ENCODER
1481 static av_cold int nvenc_setup_av1_config(AVCodecContext *avctx)
1482 {
1483  NvencContext *ctx = avctx->priv_data;
1484  NV_ENC_CONFIG *cc = &ctx->encode_config;
1485  NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1486 
1487  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt);
1488 
1489  if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) {
1490  av1->matrixCoefficients = AVCOL_SPC_BT470BG;
1491  av1->colorPrimaries = avctx->color_primaries;
1492  av1->transferCharacteristics = avctx->color_trc;
1493  av1->colorRange = 0;
1494  } else {
1495  av1->matrixCoefficients = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace;
1496  av1->colorPrimaries = avctx->color_primaries;
1497  av1->transferCharacteristics = avctx->color_trc;
1498  av1->colorRange = (avctx->color_range == AVCOL_RANGE_JPEG
1499  || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P);
1500  }
1501 
1502  if (IS_YUV444(ctx->data_pix_fmt)) {
1503  av_log(avctx, AV_LOG_ERROR, "AV1 High Profile not supported, required for 4:4:4 encoding\n");
1504  return AVERROR(ENOTSUP);
1505  } else {
1506  cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1507  avctx->profile = AV_PROFILE_AV1_MAIN;
1508  }
1509 
1510  if (ctx->dpb_size >= 0) {
1511  /* 0 means "let the hardware decide" */
1512  av1->maxNumRefFramesInDPB = ctx->dpb_size;
1513  }
1514 
1515  if (ctx->intra_refresh) {
1516  av1->enableIntraRefresh = 1;
1517  av1->intraRefreshPeriod = cc->gopLength;
1518  av1->intraRefreshCnt = cc->gopLength - 1;
1519  cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1520  }
1521 
1522  av1->idrPeriod = cc->gopLength;
1523 
1524  if (IS_CBR(cc->rcParams.rateControlMode)) {
1525  av1->enableBitstreamPadding = 1;
1526  }
1527 
1528  if (ctx->tile_cols >= 0)
1529  av1->numTileColumns = ctx->tile_cols;
1530  if (ctx->tile_rows >= 0)
1531  av1->numTileRows = ctx->tile_rows;
1532 
1533  av1->outputAnnexBFormat = 0;
1534 
1535  av1->level = ctx->level;
1536  av1->tier = ctx->tier;
1537 
1538  av1->enableTimingInfo = ctx->timing_info;
1539 
1540  /* mp4 encapsulation requires sequence headers to be present on all keyframes for AV1 */
1541  av1->disableSeqHdr = 0;
1542  av1->repeatSeqHdr = 1;
1543 
1544  av1->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1;
1545 
1546 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1547  av1->inputBitDepth = IS_10BIT(ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1548  av1->outputBitDepth = (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1549 #else
1550  av1->inputPixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0;
1551  av1->pixelBitDepthMinus8 = (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) ? 2 : 0;
1552 #endif
1553 
1554  if (ctx->b_ref_mode >= 0)
1555  av1->useBFramesAsRef = ctx->b_ref_mode;
1556 
1557  av1->numFwdRefs = avctx->refs;
1558  av1->numBwdRefs = avctx->refs;
1559 
1560  return 0;
1561 }
1562 #endif
1563 
1565 {
1566  switch (avctx->codec->id) {
1567  case AV_CODEC_ID_H264:
1568  return nvenc_setup_h264_config(avctx);
1569  case AV_CODEC_ID_HEVC:
1570  return nvenc_setup_hevc_config(avctx);
1571 #if CONFIG_AV1_NVENC_ENCODER
1572  case AV_CODEC_ID_AV1:
1573  return nvenc_setup_av1_config(avctx);
1574 #endif
1575  /* Earlier switch/case will return if unknown codec is passed. */
1576  }
1577 
1578  return 0;
1579 }
1580 
1581 static void compute_dar(AVCodecContext *avctx, int *dw, int *dh) {
1582  int sw, sh;
1583 
1584  sw = avctx->width;
1585  sh = avctx->height;
1586 
1587 #if CONFIG_AV1_NVENC_ENCODER
1588  if (avctx->codec->id == AV_CODEC_ID_AV1) {
1589  /* For AV1 we actually need to calculate the render width/height, not the dar */
1590  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0
1591  && avctx->sample_aspect_ratio.num != avctx->sample_aspect_ratio.den)
1592  {
1593  if (avctx->sample_aspect_ratio.num > avctx->sample_aspect_ratio.den) {
1594  sw = av_rescale(sw, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
1595  } else {
1596  sh = av_rescale(sh, avctx->sample_aspect_ratio.den, avctx->sample_aspect_ratio.num);
1597  }
1598  }
1599 
1600  *dw = sw;
1601  *dh = sh;
1602  return;
1603  }
1604 #endif
1605 
1606  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
1607  sw *= avctx->sample_aspect_ratio.num;
1608  sh *= avctx->sample_aspect_ratio.den;
1609  }
1610 
1611  av_reduce(dw, dh, sw, sh, 1024 * 1024);
1612 }
1613 
1615 {
1616  NvencContext *ctx = avctx->priv_data;
1617  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1618  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1619 
1620  NV_ENC_PRESET_CONFIG preset_config = { 0 };
1621  NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1622  AVCPBProperties *cpb_props;
1623  int res = 0;
1624  int dw, dh;
1625 
1626  ctx->encode_config.version = NV_ENC_CONFIG_VER;
1627  ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1628 
1629  ctx->init_encode_params.encodeHeight = avctx->height;
1630  ctx->init_encode_params.encodeWidth = avctx->width;
1631 
1632  ctx->init_encode_params.encodeConfig = &ctx->encode_config;
1633 
1634  preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1635  preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1636 
1637 #ifdef NVENC_HAVE_NEW_PRESETS
1638  ctx->init_encode_params.tuningInfo = ctx->tuning_info;
1639 
1640  if (ctx->flags & NVENC_LOSSLESS)
1641  ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1642  else if (ctx->flags & NVENC_LOWLATENCY)
1643  ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1644 
1645  nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(ctx->nvencoder,
1646  ctx->init_encode_params.encodeGUID,
1647  ctx->init_encode_params.presetGUID,
1648  ctx->init_encode_params.tuningInfo,
1649  &preset_config);
1650 #else
1651  nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder,
1652  ctx->init_encode_params.encodeGUID,
1653  ctx->init_encode_params.presetGUID,
1654  &preset_config);
1655 #endif
1656  if (nv_status != NV_ENC_SUCCESS)
1657  return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration");
1658 
1659  memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
1660 
1661  ctx->encode_config.version = NV_ENC_CONFIG_VER;
1662 
1663  compute_dar(avctx, &dw, &dh);
1664  ctx->init_encode_params.darHeight = dh;
1665  ctx->init_encode_params.darWidth = dw;
1666 
1667  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
1668  ctx->init_encode_params.frameRateNum = avctx->framerate.num;
1669  ctx->init_encode_params.frameRateDen = avctx->framerate.den;
1670  } else {
1671  ctx->init_encode_params.frameRateNum = avctx->time_base.den;
1673  ctx->init_encode_params.frameRateDen = avctx->time_base.num
1674 #if FF_API_TICKS_PER_FRAME
1675  * avctx->ticks_per_frame
1676 #endif
1677  ;
1679  }
1680 
1681 #ifdef NVENC_HAVE_UNIDIR_B
1682  ctx->init_encode_params.enableUniDirectionalB = ctx->unidir_b;
1683 #endif
1684 
1685  ctx->init_encode_params.enableEncodeAsync = 0;
1686  ctx->init_encode_params.enablePTD = 1;
1687 
1688 #ifdef NVENC_HAVE_NEW_PRESETS
1689  /* If lookahead isn't set from CLI, use value from preset.
1690  * P6 & P7 presets may enable lookahead for better quality.
1691  * */
1692  if (ctx->rc_lookahead == 0 && ctx->encode_config.rcParams.enableLookahead)
1693  ctx->rc_lookahead = ctx->encode_config.rcParams.lookaheadDepth;
1694 #endif
1695 
1696  if (ctx->weighted_pred == 1)
1697  ctx->init_encode_params.enableWeightedPrediction = 1;
1698 
1699 #ifdef NVENC_HAVE_SPLIT_FRAME_ENCODING
1700  ctx->init_encode_params.splitEncodeMode = ctx->split_encode_mode;
1701 
1702  if (ctx->split_encode_mode != NV_ENC_SPLIT_DISABLE_MODE) {
1703  if (avctx->codec->id == AV_CODEC_ID_HEVC && ctx->weighted_pred == 1)
1704  av_log(avctx, AV_LOG_WARNING, "Split encoding not supported with weighted prediction enabled.\n");
1705  }
1706 #endif
1707 
1708  if (ctx->bluray_compat) {
1709  ctx->aud = 1;
1710  ctx->dpb_size = FFMIN(FFMAX(avctx->refs, 0), 6);
1711  avctx->max_b_frames = FFMIN(avctx->max_b_frames, 3);
1712  switch (avctx->codec->id) {
1713  case AV_CODEC_ID_H264:
1714  /* maximum level depends on used resolution */
1715  break;
1716  case AV_CODEC_ID_HEVC:
1717  ctx->level = NV_ENC_LEVEL_HEVC_51;
1718  ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1719  break;
1720  }
1721  }
1722 
1723  if (avctx->gop_size > 0) {
1724  // only overwrite preset if a GOP size was selected as input
1725  ctx->encode_config.gopLength = avctx->gop_size;
1726  } else if (avctx->gop_size == 0) {
1727  ctx->encode_config.frameIntervalP = 0;
1728  ctx->encode_config.gopLength = 1;
1729  }
1730 
1731  if (avctx->max_b_frames >= 0 && ctx->encode_config.gopLength > 1) {
1732  /* 0 is intra-only, 1 is I/P only, 2 is one B-Frame, 3 two B-frames, and so on. */
1733  ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
1734  }
1735 
1736  /* force to enable intra refresh */
1737  if(ctx->single_slice_intra_refresh)
1738  ctx->intra_refresh = 1;
1739 
1740  nvenc_recalc_surfaces(avctx);
1741 
1742  res = nvenc_setup_rate_control(avctx);
1743  if (res < 0)
1744  return res;
1745 
1746  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
1747  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1748  } else {
1749  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1750  }
1751 
1752  res = nvenc_setup_codec_config(avctx);
1753  if (res)
1754  return res;
1755 
1756  res = nvenc_push_context(avctx);
1757  if (res < 0)
1758  return res;
1759 
1760  nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
1761  if (nv_status != NV_ENC_SUCCESS) {
1762  nvenc_pop_context(avctx);
1763  return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed");
1764  }
1765 
1766 #ifdef NVENC_HAVE_CUSTREAM_PTR
1767  if (ctx->cu_context) {
1768  nv_status = p_nvenc->nvEncSetIOCudaStreams(ctx->nvencoder, &ctx->cu_stream, &ctx->cu_stream);
1769  if (nv_status != NV_ENC_SUCCESS) {
1770  nvenc_pop_context(avctx);
1771  return nvenc_print_error(avctx, nv_status, "SetIOCudaStreams failed");
1772  }
1773  }
1774 #endif
1775 
1776  res = nvenc_pop_context(avctx);
1777  if (res < 0)
1778  return res;
1779 
1780  if (ctx->encode_config.frameIntervalP > 1)
1781  avctx->has_b_frames = 2;
1782 
1783  if (ctx->encode_config.rcParams.averageBitRate > 0)
1784  avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
1785 
1786  cpb_props = ff_encode_add_cpb_side_data(avctx);
1787  if (!cpb_props)
1788  return AVERROR(ENOMEM);
1789  cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate;
1790  cpb_props->avg_bitrate = avctx->bit_rate;
1791  cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize;
1792 
1793  return 0;
1794 }
1795 
1796 static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt)
1797 {
1798  switch (pix_fmt) {
1799  case AV_PIX_FMT_YUV420P:
1800  return NV_ENC_BUFFER_FORMAT_YV12;
1801  case AV_PIX_FMT_NV12:
1802  return NV_ENC_BUFFER_FORMAT_NV12;
1803  case AV_PIX_FMT_P010:
1804  case AV_PIX_FMT_P016:
1805  return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1806  case AV_PIX_FMT_GBRP:
1807  case AV_PIX_FMT_YUV444P:
1808  return NV_ENC_BUFFER_FORMAT_YUV444;
1809  case AV_PIX_FMT_GBRP16:
1810  case AV_PIX_FMT_YUV444P16:
1811  return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1812  case AV_PIX_FMT_0RGB32:
1813  case AV_PIX_FMT_RGB32:
1814  return NV_ENC_BUFFER_FORMAT_ARGB;
1815  case AV_PIX_FMT_0BGR32:
1816  case AV_PIX_FMT_BGR32:
1817  return NV_ENC_BUFFER_FORMAT_ABGR;
1818  case AV_PIX_FMT_X2RGB10:
1819  return NV_ENC_BUFFER_FORMAT_ARGB10;
1820  case AV_PIX_FMT_X2BGR10:
1821  return NV_ENC_BUFFER_FORMAT_ABGR10;
1822  default:
1823  return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1824  }
1825 }
1826 
1827 static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
1828 {
1829  NvencContext *ctx = avctx->priv_data;
1830  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1831  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1832  NvencSurface* tmp_surface = &ctx->surfaces[idx];
1833 
1834  NVENCSTATUS nv_status;
1835  NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1836  allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1837 
1838  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1839  ctx->surfaces[idx].in_ref = av_frame_alloc();
1840  if (!ctx->surfaces[idx].in_ref)
1841  return AVERROR(ENOMEM);
1842  } else {
1843  NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1844 
1845  ctx->surfaces[idx].format = nvenc_map_buffer_format(ctx->data_pix_fmt);
1846  if (ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1847  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
1848  av_get_pix_fmt_name(ctx->data_pix_fmt));
1849  return AVERROR(EINVAL);
1850  }
1851 
1852  allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1853  allocSurf.width = avctx->width;
1854  allocSurf.height = avctx->height;
1855  allocSurf.bufferFmt = ctx->surfaces[idx].format;
1856 
1857  nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
1858  if (nv_status != NV_ENC_SUCCESS) {
1859  return nvenc_print_error(avctx, nv_status, "CreateInputBuffer failed");
1860  }
1861 
1862  ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1863  ctx->surfaces[idx].width = allocSurf.width;
1864  ctx->surfaces[idx].height = allocSurf.height;
1865  }
1866 
1867  nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
1868  if (nv_status != NV_ENC_SUCCESS) {
1869  int err = nvenc_print_error(avctx, nv_status, "CreateBitstreamBuffer failed");
1870  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1871  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[idx].input_surface);
1872  av_frame_free(&ctx->surfaces[idx].in_ref);
1873  return err;
1874  }
1875 
1876  ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1877 
1878  av_fifo_write(ctx->unused_surface_queue, &tmp_surface, 1);
1879 
1880  return 0;
1881 }
1882 
1884 {
1885  NvencContext *ctx = avctx->priv_data;
1886  int i, res = 0, res2;
1887 
1888  ctx->surfaces = av_calloc(ctx->nb_surfaces, sizeof(*ctx->surfaces));
1889  if (!ctx->surfaces)
1890  return AVERROR(ENOMEM);
1891 
1892  ctx->frame_data_array = av_calloc(ctx->frame_data_array_nb, sizeof(*ctx->frame_data_array));
1893  if (!ctx->frame_data_array)
1894  return AVERROR(ENOMEM);
1895 
1896  ctx->timestamp_list = av_fifo_alloc2(ctx->nb_surfaces + ctx->encode_config.frameIntervalP,
1897  sizeof(int64_t), 0);
1898  if (!ctx->timestamp_list)
1899  return AVERROR(ENOMEM);
1900 
1901  ctx->unused_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
1902  if (!ctx->unused_surface_queue)
1903  return AVERROR(ENOMEM);
1904 
1905  ctx->output_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
1906  if (!ctx->output_surface_queue)
1907  return AVERROR(ENOMEM);
1908  ctx->output_surface_ready_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
1909  if (!ctx->output_surface_ready_queue)
1910  return AVERROR(ENOMEM);
1911 
1912  res = nvenc_push_context(avctx);
1913  if (res < 0)
1914  return res;
1915 
1916  for (i = 0; i < ctx->nb_surfaces; i++) {
1917  if ((res = nvenc_alloc_surface(avctx, i)) < 0)
1918  goto fail;
1919  }
1920 
1921 fail:
1922  res2 = nvenc_pop_context(avctx);
1923  if (res2 < 0)
1924  return res2;
1925 
1926  return res;
1927 }
1928 
1930 {
1931  NvencContext *ctx = avctx->priv_data;
1932  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1933  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1934 
1935  NVENCSTATUS nv_status;
1936  uint32_t outSize = 0;
1937  char tmpHeader[NV_MAX_SEQ_HDR_LEN];
1938 
1939  NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1940  payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1941 
1942  payload.spsppsBuffer = tmpHeader;
1943  payload.inBufferSize = sizeof(tmpHeader);
1944  payload.outSPSPPSPayloadSize = &outSize;
1945 
1946  nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
1947  if (nv_status != NV_ENC_SUCCESS) {
1948  return nvenc_print_error(avctx, nv_status, "GetSequenceParams failed");
1949  }
1950 
1951  avctx->extradata_size = outSize;
1953 
1954  if (!avctx->extradata) {
1955  return AVERROR(ENOMEM);
1956  }
1957 
1958  memcpy(avctx->extradata, tmpHeader, outSize);
1959 
1960  return 0;
1961 }
1962 
1964 {
1965  NvencContext *ctx = avctx->priv_data;
1966  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1967  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1968  int i, res;
1969 
1970  /* the encoder has to be flushed before it can be closed */
1971  if (ctx->nvencoder) {
1972  NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1973  .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1974 
1975  res = nvenc_push_context(avctx);
1976  if (res < 0)
1977  return res;
1978 
1979  p_nvenc->nvEncEncodePicture(ctx->nvencoder, &params);
1980  }
1981 
1982  av_fifo_freep2(&ctx->timestamp_list);
1983  av_fifo_freep2(&ctx->output_surface_ready_queue);
1984  av_fifo_freep2(&ctx->output_surface_queue);
1985  av_fifo_freep2(&ctx->unused_surface_queue);
1986 
1987  if (ctx->frame_data_array) {
1988  for (i = 0; i < ctx->frame_data_array_nb; i++)
1989  av_buffer_unref(&ctx->frame_data_array[i].frame_opaque_ref);
1990  av_freep(&ctx->frame_data_array);
1991  }
1992 
1993  if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) {
1994  for (i = 0; i < ctx->nb_registered_frames; i++) {
1995  if (ctx->registered_frames[i].mapped)
1996  p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[i].in_map.mappedResource);
1997  if (ctx->registered_frames[i].regptr)
1998  p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr);
1999  }
2000  ctx->nb_registered_frames = 0;
2001  }
2002 
2003  if (ctx->surfaces) {
2004  for (i = 0; i < ctx->nb_surfaces; ++i) {
2005  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
2006  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[i].input_surface);
2007  av_frame_free(&ctx->surfaces[i].in_ref);
2008  p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->surfaces[i].output_surface);
2009  }
2010  }
2011  av_freep(&ctx->surfaces);
2012  ctx->nb_surfaces = 0;
2013 
2014  av_frame_free(&ctx->frame);
2015 
2016  av_freep(&ctx->sei_data);
2017 
2018  if (ctx->nvencoder) {
2019  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
2020 
2021  res = nvenc_pop_context(avctx);
2022  if (res < 0)
2023  return res;
2024  }
2025  ctx->nvencoder = NULL;
2026 
2027  if (ctx->cu_context_internal)
2028  CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal));
2029  ctx->cu_context = ctx->cu_context_internal = NULL;
2030 
2031 #if CONFIG_D3D11VA
2032  if (ctx->d3d11_device) {
2033  ID3D11Device_Release(ctx->d3d11_device);
2034  ctx->d3d11_device = NULL;
2035  }
2036 #endif
2037 
2038  nvenc_free_functions(&dl_fn->nvenc_dl);
2039  cuda_free_functions(&dl_fn->cuda_dl);
2040 
2041  dl_fn->nvenc_device_count = 0;
2042 
2043  av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
2044 
2045  return 0;
2046 }
2047 
2049 {
2050  NvencContext *ctx = avctx->priv_data;
2051  int ret;
2052 
2053  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2054  AVHWFramesContext *frames_ctx;
2055  if (!avctx->hw_frames_ctx) {
2056  av_log(avctx, AV_LOG_ERROR,
2057  "hw_frames_ctx must be set when using GPU frames as input\n");
2058  return AVERROR(EINVAL);
2059  }
2060  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
2061  if (frames_ctx->format != avctx->pix_fmt) {
2062  av_log(avctx, AV_LOG_ERROR,
2063  "hw_frames_ctx must match the GPU frame type\n");
2064  return AVERROR(EINVAL);
2065  }
2066  ctx->data_pix_fmt = frames_ctx->sw_format;
2067  } else {
2068  ctx->data_pix_fmt = avctx->pix_fmt;
2069  }
2070 
2071  if (ctx->rgb_mode == NVENC_RGB_MODE_DISABLED && IS_RGB(ctx->data_pix_fmt)) {
2072  av_log(avctx, AV_LOG_ERROR, "Packed RGB input, but RGB support is disabled.\n");
2073  return AVERROR(EINVAL);
2074  }
2075 
2076  ctx->frame = av_frame_alloc();
2077  if (!ctx->frame)
2078  return AVERROR(ENOMEM);
2079 
2080  if ((ret = nvenc_load_libraries(avctx)) < 0)
2081  return ret;
2082 
2083  if ((ret = nvenc_setup_device(avctx)) < 0)
2084  return ret;
2085 
2086  if ((ret = nvenc_setup_encoder(avctx)) < 0)
2087  return ret;
2088 
2089  if ((ret = nvenc_setup_surfaces(avctx)) < 0)
2090  return ret;
2091 
2092  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
2093  if ((ret = nvenc_setup_extradata(avctx)) < 0)
2094  return ret;
2095  }
2096 
2097  return 0;
2098 }
2099 
2101 {
2102  NvencSurface *tmp_surf;
2103 
2104  if (av_fifo_read(ctx->unused_surface_queue, &tmp_surf, 1) < 0)
2105  // queue empty
2106  return NULL;
2107 
2108  return tmp_surf;
2109 }
2110 
2111 static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface,
2112  NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame)
2113 {
2114  int dst_linesize[4] = {
2115  lock_buffer_params->pitch,
2116  lock_buffer_params->pitch,
2117  lock_buffer_params->pitch,
2118  lock_buffer_params->pitch
2119  };
2120  uint8_t *dst_data[4];
2121  int ret;
2122 
2123  if (frame->format == AV_PIX_FMT_YUV420P)
2124  dst_linesize[1] = dst_linesize[2] >>= 1;
2125 
2126  ret = av_image_fill_pointers(dst_data, frame->format, nv_surface->height,
2127  lock_buffer_params->bufferDataPtr, dst_linesize);
2128  if (ret < 0)
2129  return ret;
2130 
2131  if (frame->format == AV_PIX_FMT_YUV420P)
2132  FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2133 
2134  av_image_copy2(dst_data, dst_linesize,
2135  frame->data, frame->linesize, frame->format,
2136  avctx->width, avctx->height);
2137 
2138  return 0;
2139 }
2140 
2142 {
2143  NvencContext *ctx = avctx->priv_data;
2144  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2145  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2146  NVENCSTATUS nv_status;
2147 
2148  int i, first_round;
2149 
2150  if (ctx->nb_registered_frames == FF_ARRAY_ELEMS(ctx->registered_frames)) {
2151  for (first_round = 1; first_round >= 0; first_round--) {
2152  for (i = 0; i < ctx->nb_registered_frames; i++) {
2153  if (!ctx->registered_frames[i].mapped) {
2154  if (ctx->registered_frames[i].regptr) {
2155  if (first_round)
2156  continue;
2157  nv_status = p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr);
2158  if (nv_status != NV_ENC_SUCCESS)
2159  return nvenc_print_error(avctx, nv_status, "Failed unregistering unused input resource");
2160  ctx->registered_frames[i].ptr = NULL;
2161  ctx->registered_frames[i].regptr = NULL;
2162  }
2163  return i;
2164  }
2165  }
2166  }
2167  } else {
2168  return ctx->nb_registered_frames++;
2169  }
2170 
2171  av_log(avctx, AV_LOG_ERROR, "Too many registered CUDA frames\n");
2172  return AVERROR(ENOMEM);
2173 }
2174 
2176 {
2177  NvencContext *ctx = avctx->priv_data;
2178  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2179  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2180 
2181  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frame->hw_frames_ctx->data;
2182  NV_ENC_REGISTER_RESOURCE reg = { 0 };
2183  int i, idx, ret;
2184 
2185  for (i = 0; i < ctx->nb_registered_frames; i++) {
2186  if (avctx->pix_fmt == AV_PIX_FMT_CUDA && ctx->registered_frames[i].ptr == frame->data[0])
2187  return i;
2188  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])
2189  return i;
2190  }
2191 
2192  idx = nvenc_find_free_reg_resource(avctx);
2193  if (idx < 0)
2194  return idx;
2195 
2196  reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2197  reg.width = frames_ctx->width;
2198  reg.height = frames_ctx->height;
2199  reg.pitch = frame->linesize[0];
2200  reg.resourceToRegister = frame->data[0];
2201 
2202  if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
2203  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2204  }
2205  else if (avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2206  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2207  reg.subResourceIndex = (intptr_t)frame->data[1];
2208  }
2209 
2210  reg.bufferFormat = nvenc_map_buffer_format(frames_ctx->sw_format);
2211  if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2212  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
2213  av_get_pix_fmt_name(frames_ctx->sw_format));
2214  return AVERROR(EINVAL);
2215  }
2216 
2217  ret = p_nvenc->nvEncRegisterResource(ctx->nvencoder, &reg);
2218  if (ret != NV_ENC_SUCCESS) {
2219  nvenc_print_error(avctx, ret, "Error registering an input resource");
2220  return AVERROR_UNKNOWN;
2221  }
2222 
2223  ctx->registered_frames[idx].ptr = frame->data[0];
2224  ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2225  ctx->registered_frames[idx].regptr = reg.registeredResource;
2226  return idx;
2227 }
2228 
2230  NvencSurface *nvenc_frame)
2231 {
2232  NvencContext *ctx = avctx->priv_data;
2233  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2234  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2235 
2236  int res;
2237  NVENCSTATUS nv_status;
2238 
2239  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2240  int reg_idx = nvenc_register_frame(avctx, frame);
2241  if (reg_idx < 0) {
2242  av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n");
2243  return reg_idx;
2244  }
2245 
2246  res = av_frame_ref(nvenc_frame->in_ref, frame);
2247  if (res < 0)
2248  return res;
2249 
2250  if (!ctx->registered_frames[reg_idx].mapped) {
2251  ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2252  ctx->registered_frames[reg_idx].in_map.registeredResource = ctx->registered_frames[reg_idx].regptr;
2253  nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &ctx->registered_frames[reg_idx].in_map);
2254  if (nv_status != NV_ENC_SUCCESS) {
2255  av_frame_unref(nvenc_frame->in_ref);
2256  return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");
2257  }
2258  }
2259 
2260  ctx->registered_frames[reg_idx].mapped += 1;
2261 
2262  nvenc_frame->reg_idx = reg_idx;
2263  nvenc_frame->input_surface = ctx->registered_frames[reg_idx].in_map.mappedResource;
2264  nvenc_frame->format = ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2265  nvenc_frame->pitch = frame->linesize[0];
2266 
2267  return 0;
2268  } else {
2269  NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2270 
2271  lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2272  lockBufferParams.inputBuffer = nvenc_frame->input_surface;
2273 
2274  nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
2275  if (nv_status != NV_ENC_SUCCESS) {
2276  return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer");
2277  }
2278 
2279  nvenc_frame->pitch = lockBufferParams.pitch;
2280  res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame);
2281 
2282  nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, nvenc_frame->input_surface);
2283  if (nv_status != NV_ENC_SUCCESS) {
2284  return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!");
2285  }
2286 
2287  return res;
2288  }
2289 }
2290 
2292  NV_ENC_PIC_PARAMS *params,
2293  NV_ENC_SEI_PAYLOAD *sei_data,
2294  int sei_count)
2295 {
2296  NvencContext *ctx = avctx->priv_data;
2297 
2298  switch (avctx->codec->id) {
2299  case AV_CODEC_ID_H264:
2300  params->codecPicParams.h264PicParams.sliceMode =
2301  ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2302  params->codecPicParams.h264PicParams.sliceModeData =
2303  ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2304  if (sei_count > 0) {
2305  params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2306  params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2307  }
2308 
2309  break;
2310  case AV_CODEC_ID_HEVC:
2311  params->codecPicParams.hevcPicParams.sliceMode =
2312  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2313  params->codecPicParams.hevcPicParams.sliceModeData =
2314  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2315  if (sei_count > 0) {
2316  params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2317  params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2318  }
2319 
2320  break;
2321 #if CONFIG_AV1_NVENC_ENCODER
2322  case AV_CODEC_ID_AV1:
2323  params->codecPicParams.av1PicParams.numTileColumns =
2324  ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2325  params->codecPicParams.av1PicParams.numTileRows =
2326  ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2327  if (sei_count > 0) {
2328  params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2329  params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2330  }
2331 
2332  break;
2333 #endif
2334  }
2335 }
2336 
2337 static inline void timestamp_queue_enqueue(AVFifo *queue, int64_t timestamp)
2338 {
2339  av_fifo_write(queue, &timestamp, 1);
2340 }
2341 
2343 {
2344  int64_t timestamp = AV_NOPTS_VALUE;
2345  // The following call might fail if the queue is empty.
2346  av_fifo_read(queue, &timestamp, 1);
2347 
2348  return timestamp;
2349 }
2350 
2351 static inline int64_t timestamp_queue_peek(AVFifo *queue, size_t index)
2352 {
2353  int64_t timestamp = AV_NOPTS_VALUE;
2354  av_fifo_peek(queue, &timestamp, 1, index);
2355 
2356  return timestamp;
2357 }
2358 
2360  NV_ENC_LOCK_BITSTREAM *params,
2361  AVPacket *pkt)
2362 {
2363  NvencContext *ctx = avctx->priv_data;
2364  unsigned int delay;
2365  int64_t delay_time;
2366 
2367  pkt->pts = params->outputTimeStamp;
2368 
2369  if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER)) {
2370  pkt->dts = pkt->pts;
2371  return 0;
2372  }
2373 
2374  // This can be more than necessary, but we don't know the real reorder delay.
2375  delay = FFMAX(ctx->encode_config.frameIntervalP - 1, 0);
2376  if (ctx->output_frame_num >= delay) {
2377  pkt->dts = timestamp_queue_dequeue(ctx->timestamp_list);
2378  ctx->output_frame_num++;
2379  return 0;
2380  }
2381 
2382  delay_time = ctx->initial_delay_time;
2383  if (!delay_time) {
2384  int64_t t1, t2, t3;
2385  t1 = timestamp_queue_peek(ctx->timestamp_list, delay);
2386  t2 = timestamp_queue_peek(ctx->timestamp_list, 0);
2387  t3 = (delay > 1) ? timestamp_queue_peek(ctx->timestamp_list, 1) : t1;
2388 
2389  if (t1 != AV_NOPTS_VALUE) {
2390  delay_time = t1 - t2;
2391  } else if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
2392  delay_time = av_rescale_q(delay, (AVRational) {avctx->framerate.den, avctx->framerate.num},
2393  avctx->time_base);
2394  } else if (t3 != AV_NOPTS_VALUE) {
2395  delay_time = delay * (t3 - t2);
2396  } else {
2397  delay_time = delay;
2398  }
2399  ctx->initial_delay_time = delay_time;
2400  }
2401 
2402  /* The following method is simple, but doesn't guarantee monotonic with VFR
2403  * when delay_time isn't accurate (that is, t1 == AV_NOPTS_VALUE)
2404  *
2405  * dts = timestamp_queue_peek(ctx->timestamp_list, ctx->output_frame_num) - delay_time
2406  */
2407  pkt->dts = timestamp_queue_peek(ctx->timestamp_list, 0) - delay_time * (delay - ctx->output_frame_num) / delay;
2408  ctx->output_frame_num++;
2409 
2410  return 0;
2411 }
2412 
2413 static int nvenc_store_frame_data(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *pic_params, const AVFrame *frame)
2414 {
2415  NvencContext *ctx = avctx->priv_data;
2416  int res = 0;
2417 
2418  int idx = ctx->frame_data_array_pos;
2419  NvencFrameData *frame_data = &ctx->frame_data_array[idx];
2420 
2421  // in case the encoder got reconfigured, there might be leftovers
2423 
2424  if (frame->opaque_ref && avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
2427  return AVERROR(ENOMEM);
2428  }
2429 
2430  frame_data->duration = frame->duration;
2431  frame_data->frame_opaque = frame->opaque;
2432 
2433  ctx->frame_data_array_pos = (ctx->frame_data_array_pos + 1) % ctx->frame_data_array_nb;
2434  pic_params->inputDuration = idx;
2435 
2436  return res;
2437 }
2438 
2439 static int nvenc_retrieve_frame_data(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *lock_params, AVPacket *pkt)
2440 {
2441  NvencContext *ctx = avctx->priv_data;
2442  int res = 0;
2443 
2444  int idx = lock_params->outputDuration;
2445  NvencFrameData *frame_data = &ctx->frame_data_array[idx];
2446 
2448 
2449  if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
2453  }
2454 
2456 
2457  return res;
2458 }
2459 
2461 {
2462  NvencContext *ctx = avctx->priv_data;
2463  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2464  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2465 
2466  NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2467  NVENCSTATUS nv_status;
2468  int res = 0;
2469 
2470  enum AVPictureType pict_type;
2471 
2472  lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2473 
2474  lock_params.doNotWait = 0;
2475  lock_params.outputBitstream = tmpoutsurf->output_surface;
2476 
2477  nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
2478  if (nv_status != NV_ENC_SUCCESS) {
2479  res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer");
2480  goto error;
2481  }
2482 
2483  res = ff_get_encode_buffer(avctx, pkt, lock_params.bitstreamSizeInBytes, 0);
2484 
2485  if (res < 0) {
2486  p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
2487  goto error;
2488  }
2489 
2490  memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2491 
2492  nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
2493  if (nv_status != NV_ENC_SUCCESS) {
2494  res = nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");
2495  goto error;
2496  }
2497 
2498 
2499  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2500  ctx->registered_frames[tmpoutsurf->reg_idx].mapped -= 1;
2501  if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped == 0) {
2502  nv_status = p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[tmpoutsurf->reg_idx].in_map.mappedResource);
2503  if (nv_status != NV_ENC_SUCCESS) {
2504  res = nvenc_print_error(avctx, nv_status, "Failed unmapping input resource");
2505  goto error;
2506  }
2507  } else if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped < 0) {
2508  res = AVERROR_BUG;
2509  goto error;
2510  }
2511 
2512  av_frame_unref(tmpoutsurf->in_ref);
2513 
2514  tmpoutsurf->input_surface = NULL;
2515  }
2516 
2517  switch (lock_params.pictureType) {
2518  case NV_ENC_PIC_TYPE_IDR:
2520  case NV_ENC_PIC_TYPE_I:
2521  pict_type = AV_PICTURE_TYPE_I;
2522  break;
2523  case NV_ENC_PIC_TYPE_P:
2524  pict_type = AV_PICTURE_TYPE_P;
2525  break;
2526  case NV_ENC_PIC_TYPE_B:
2527  pict_type = AV_PICTURE_TYPE_B;
2528  break;
2529  case NV_ENC_PIC_TYPE_BI:
2530  pict_type = AV_PICTURE_TYPE_BI;
2531  break;
2532  default:
2533  av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
2534  av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
2535  res = AVERROR_EXTERNAL;
2536  goto error;
2537  }
2538 
2540  (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type);
2541 
2542  res = nvenc_set_timestamp(avctx, &lock_params, pkt);
2543  if (res < 0)
2544  goto error2;
2545 
2546  res = nvenc_retrieve_frame_data(avctx, &lock_params, pkt);
2547  if (res < 0)
2548  goto error2;
2549 
2550  return 0;
2551 
2552 error:
2553  timestamp_queue_dequeue(ctx->timestamp_list);
2554 
2555 error2:
2556  return res;
2557 }
2558 
2559 static int output_ready(AVCodecContext *avctx, int flush)
2560 {
2561  NvencContext *ctx = avctx->priv_data;
2562  int nb_ready, nb_pending;
2563 
2564  nb_ready = av_fifo_can_read(ctx->output_surface_ready_queue);
2565  nb_pending = av_fifo_can_read(ctx->output_surface_queue);
2566  if (flush)
2567  return nb_ready > 0;
2568  return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth);
2569 }
2570 
2572 {
2573  NvencContext *ctx = avctx->priv_data;
2574  int sei_count = 0;
2575  int i, res;
2576 
2578  void *a53_data = NULL;
2579  size_t a53_size = 0;
2580 
2581  if (ff_alloc_a53_sei(frame, 0, &a53_data, &a53_size) < 0) {
2582  av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2583  }
2584 
2585  if (a53_data) {
2586  void *tmp = av_fast_realloc(ctx->sei_data,
2587  &ctx->sei_data_size,
2588  (sei_count + 1) * sizeof(*ctx->sei_data));
2589  if (!tmp) {
2590  av_free(a53_data);
2591  res = AVERROR(ENOMEM);
2592  goto error;
2593  } else {
2594  ctx->sei_data = tmp;
2595  ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2596  ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2597 
2598 #if CONFIG_AV1_NVENC_ENCODER
2599  if (avctx->codec->id == AV_CODEC_ID_AV1)
2600  ctx->sei_data[sei_count].payloadType = AV1_METADATA_TYPE_ITUT_T35;
2601  else
2602 #endif
2603  ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35;
2604 
2605  sei_count++;
2606  }
2607  }
2608  }
2609 
2611  void *tc_data = NULL;
2612  size_t tc_size = 0;
2613 
2614  if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, &tc_data, &tc_size) < 0) {
2615  av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n");
2616  }
2617 
2618  if (tc_data) {
2619  void *tmp = av_fast_realloc(ctx->sei_data,
2620  &ctx->sei_data_size,
2621  (sei_count + 1) * sizeof(*ctx->sei_data));
2622  if (!tmp) {
2623  av_free(tc_data);
2624  res = AVERROR(ENOMEM);
2625  goto error;
2626  } else {
2627  ctx->sei_data = tmp;
2628  ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2629  ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2630 
2631 #if CONFIG_AV1_NVENC_ENCODER
2632  if (avctx->codec->id == AV_CODEC_ID_AV1)
2633  ctx->sei_data[sei_count].payloadType = AV1_METADATA_TYPE_TIMECODE;
2634  else
2635 #endif
2636  ctx->sei_data[sei_count].payloadType = SEI_TYPE_TIME_CODE;
2637 
2638  sei_count++;
2639  }
2640  }
2641  }
2642 
2643  if (!ctx->udu_sei)
2644  return sei_count;
2645 
2646  for (i = 0; i < frame->nb_side_data; i++) {
2647  AVFrameSideData *side_data = frame->side_data[i];
2648  void *tmp;
2649 
2650  if (side_data->type != AV_FRAME_DATA_SEI_UNREGISTERED)
2651  continue;
2652 
2653  tmp = av_fast_realloc(ctx->sei_data,
2654  &ctx->sei_data_size,
2655  (sei_count + 1) * sizeof(*ctx->sei_data));
2656  if (!tmp) {
2657  res = AVERROR(ENOMEM);
2658  goto error;
2659  } else {
2660  ctx->sei_data = tmp;
2661  ctx->sei_data[sei_count].payloadSize = side_data->size;
2662  ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_UNREGISTERED;
2663  ctx->sei_data[sei_count].payload = av_memdup(side_data->data, side_data->size);
2664 
2665  if (!ctx->sei_data[sei_count].payload) {
2666  res = AVERROR(ENOMEM);
2667  goto error;
2668  }
2669 
2670  sei_count++;
2671  }
2672  }
2673 
2674  return sei_count;
2675 
2676 error:
2677  for (i = 0; i < sei_count; i++)
2678  av_freep(&(ctx->sei_data[i].payload));
2679 
2680  return res;
2681 }
2682 
2683 static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
2684 {
2685  NvencContext *ctx = avctx->priv_data;
2686  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
2687  NVENCSTATUS ret;
2688 
2689  NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2690  int needs_reconfig = 0;
2691  int needs_encode_config = 0;
2692  int reconfig_bitrate = 0, reconfig_dar = 0;
2693  int dw, dh;
2694 
2695  params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2696  params.reInitEncodeParams = ctx->init_encode_params;
2697 
2698  compute_dar(avctx, &dw, &dh);
2699  if (dw != ctx->init_encode_params.darWidth || dh != ctx->init_encode_params.darHeight) {
2700  av_log(avctx, AV_LOG_VERBOSE,
2701  "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2702  ctx->init_encode_params.darWidth,
2703  ctx->init_encode_params.darHeight, dw, dh);
2704 
2705  params.reInitEncodeParams.darHeight = dh;
2706  params.reInitEncodeParams.darWidth = dw;
2707 
2708  needs_reconfig = 1;
2709  reconfig_dar = 1;
2710  }
2711 
2712  if (ctx->rc != NV_ENC_PARAMS_RC_CONSTQP && ctx->support_dyn_bitrate) {
2713  if (avctx->bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->bit_rate) {
2714  av_log(avctx, AV_LOG_VERBOSE,
2715  "avg bitrate change: %d -> %d\n",
2716  params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2717  (uint32_t)avctx->bit_rate);
2718 
2719  params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->bit_rate;
2720  reconfig_bitrate = 1;
2721  }
2722 
2723  if (avctx->rc_max_rate > 0 && ctx->encode_config.rcParams.maxBitRate != avctx->rc_max_rate) {
2724  av_log(avctx, AV_LOG_VERBOSE,
2725  "max bitrate change: %d -> %d\n",
2726  params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2727  (uint32_t)avctx->rc_max_rate);
2728 
2729  params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->rc_max_rate;
2730  reconfig_bitrate = 1;
2731  }
2732 
2733  if (avctx->rc_buffer_size > 0 && ctx->encode_config.rcParams.vbvBufferSize != avctx->rc_buffer_size) {
2734  av_log(avctx, AV_LOG_VERBOSE,
2735  "vbv buffer size change: %d -> %d\n",
2736  params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2737  avctx->rc_buffer_size);
2738 
2739  params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->rc_buffer_size;
2740  reconfig_bitrate = 1;
2741  }
2742 
2743  if (reconfig_bitrate) {
2744  params.resetEncoder = 1;
2745  params.forceIDR = 1;
2746 
2747  needs_encode_config = 1;
2748  needs_reconfig = 1;
2749  }
2750  }
2751 
2752  if (!needs_encode_config)
2753  params.reInitEncodeParams.encodeConfig = NULL;
2754 
2755  if (needs_reconfig) {
2756  ret = p_nvenc->nvEncReconfigureEncoder(ctx->nvencoder, &params);
2757  if (ret != NV_ENC_SUCCESS) {
2758  nvenc_print_error(avctx, ret, "failed to reconfigure nvenc");
2759  } else {
2760  if (reconfig_dar) {
2761  ctx->init_encode_params.darHeight = dh;
2762  ctx->init_encode_params.darWidth = dw;
2763  }
2764 
2765  if (reconfig_bitrate) {
2766  ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2767  ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2768  ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2769  }
2770 
2771  }
2772  }
2773 }
2774 
2775 static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
2776 {
2777  NVENCSTATUS nv_status;
2778  NvencSurface *tmp_out_surf, *in_surf;
2779  int res, res2;
2780  int sei_count = 0;
2781  int i;
2782 
2783  NvencContext *ctx = avctx->priv_data;
2784  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2785  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2786 
2787  NV_ENC_PIC_PARAMS pic_params = { 0 };
2788  pic_params.version = NV_ENC_PIC_PARAMS_VER;
2789 
2790  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
2791  return AVERROR(EINVAL);
2792 
2793  if (frame && frame->buf[0]) {
2794  in_surf = get_free_frame(ctx);
2795  if (!in_surf)
2796  return AVERROR(EAGAIN);
2797 
2798  res = nvenc_push_context(avctx);
2799  if (res < 0)
2800  return res;
2801 
2802  reconfig_encoder(avctx, frame);
2803 
2804  res = nvenc_upload_frame(avctx, frame, in_surf);
2805 
2806  res2 = nvenc_pop_context(avctx);
2807  if (res2 < 0)
2808  return res2;
2809 
2810  if (res)
2811  return res;
2812 
2813  pic_params.inputBuffer = in_surf->input_surface;
2814  pic_params.bufferFmt = in_surf->format;
2815  pic_params.inputWidth = in_surf->width;
2816  pic_params.inputHeight = in_surf->height;
2817  pic_params.inputPitch = in_surf->pitch;
2818  pic_params.outputBitstream = in_surf->output_surface;
2819 
2820  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2821  if (frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)
2822  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2823  else
2824  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2825  } else {
2826  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2827  }
2828 
2829  if (ctx->forced_idr >= 0 && frame->pict_type == AV_PICTURE_TYPE_I) {
2830  pic_params.encodePicFlags =
2831  ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2832  } else {
2833  pic_params.encodePicFlags = 0;
2834  }
2835 
2836  pic_params.frameIdx = ctx->frame_idx_counter++;
2837  pic_params.inputTimeStamp = frame->pts;
2838 
2839  if (ctx->extra_sei) {
2840  res = prepare_sei_data_array(avctx, frame);
2841  if (res < 0)
2842  return res;
2843  sei_count = res;
2844  }
2845 
2846  res = nvenc_store_frame_data(avctx, &pic_params, frame);
2847  if (res < 0)
2848  return res;
2849 
2850  nvenc_codec_specific_pic_params(avctx, &pic_params, ctx->sei_data, sei_count);
2851  } else {
2852  pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2853  }
2854 
2855  res = nvenc_push_context(avctx);
2856  if (res < 0)
2857  return res;
2858 
2859  nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
2860 
2861  for (i = 0; i < sei_count; i++)
2862  av_freep(&(ctx->sei_data[i].payload));
2863 
2864  res = nvenc_pop_context(avctx);
2865  if (res < 0)
2866  return res;
2867 
2868  if (nv_status != NV_ENC_SUCCESS &&
2869  nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2870  return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
2871 
2872  if (frame && frame->buf[0]) {
2873  av_fifo_write(ctx->output_surface_queue, &in_surf, 1);
2874 
2876  timestamp_queue_enqueue(ctx->timestamp_list, frame->pts);
2877  }
2878 
2879  /* all the pending buffers are now ready for output */
2880  if (nv_status == NV_ENC_SUCCESS) {
2881  while (av_fifo_read(ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2882  av_fifo_write(ctx->output_surface_ready_queue, &tmp_out_surf, 1);
2883  }
2884 
2885  return 0;
2886 }
2887 
2889 {
2890  NvencSurface *tmp_out_surf;
2891  int res, res2;
2892 
2893  NvencContext *ctx = avctx->priv_data;
2894 
2895  AVFrame *frame = ctx->frame;
2896 
2897  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
2898  return AVERROR(EINVAL);
2899 
2900  if (!frame->buf[0]) {
2901  res = ff_encode_get_frame(avctx, frame);
2902  if (res < 0 && res != AVERROR_EOF)
2903  return res;
2904  }
2905 
2906  res = nvenc_send_frame(avctx, frame);
2907  if (res < 0) {
2908  if (res != AVERROR(EAGAIN))
2909  return res;
2910  } else
2912 
2913  if (output_ready(avctx, avctx->internal->draining)) {
2914  av_fifo_read(ctx->output_surface_ready_queue, &tmp_out_surf, 1);
2915 
2916  res = nvenc_push_context(avctx);
2917  if (res < 0)
2918  return res;
2919 
2920  res = process_output_surface(avctx, pkt, tmp_out_surf);
2921 
2922  res2 = nvenc_pop_context(avctx);
2923  if (res2 < 0)
2924  return res2;
2925 
2926  if (res)
2927  return res;
2928 
2929  av_fifo_write(ctx->unused_surface_queue, &tmp_out_surf, 1);
2930  } else if (avctx->internal->draining) {
2931  return AVERROR_EOF;
2932  } else {
2933  return AVERROR(EAGAIN);
2934  }
2935 
2936  return 0;
2937 }
2938 
2940 {
2941  NvencContext *ctx = avctx->priv_data;
2942 
2943  nvenc_send_frame(avctx, NULL);
2944  av_fifo_reset2(ctx->timestamp_list);
2945  ctx->output_frame_num = 0;
2946  ctx->initial_delay_time = 0;
2947 }
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
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:186
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
NV_ENC_H264_PROFILE_HIGH
@ NV_ENC_H264_PROFILE_HIGH
Definition: nvenc.h:153
NVENC_RGB_MODE_DISABLED
@ NVENC_RGB_MODE_DISABLED
Definition: nvenc.h:178
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
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:685
NV_ENC_H264_PROFILE_BASELINE
@ NV_ENC_H264_PROFILE_BASELINE
Definition: nvenc.h:151
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:949
AV_PIX_FMT_BGR32
#define AV_PIX_FMT_BGR32
Definition: pixfmt.h:453
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:2965
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
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:2559
NvencContext
Definition: nvenc.h:183
AVCodecContext::codec_descriptor
const struct AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
Definition: avcodec.h:1866
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:160
nvenc_store_frame_data
static int nvenc_store_frame_data(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *pic_params, const AVFrame *frame)
Definition: nvenc.c:2413
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:374
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:678
nvenc_set_timestamp
static int nvenc_set_timestamp(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *params, AVPacket *pkt)
Definition: nvenc.c:2359
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:686
P1
#define P1
Definition: cavsdsp.c:37
NV_ENC_HEVC_PROFILE_MAIN_10
@ NV_ENC_HEVC_PROFILE_MAIN_10
Definition: nvenc.h:159
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:533
encode.h
ANY_DEVICE
@ ANY_DEVICE
Definition: nvenc.h:174
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:811
NvencFrameData
Definition: nvenc.h:109
reconfig_encoder
static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2683
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
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:610
timestamp_queue_peek
static int64_t timestamp_queue_peek(AVFifo *queue, size_t index)
Definition: nvenc.c:2351
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:551
mathematics.h
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
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:1267
NV_ENC_HEVC_PROFILE_REXT
@ NV_ENC_HEVC_PROFILE_REXT
Definition: nvenc.h:160
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:588
ff_nvenc_encode_flush
av_cold void ff_nvenc_encode_flush(AVCodecContext *avctx)
Definition: nvenc.c:2939
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:638
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:1581
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:560
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:615
nvenc_upload_frame
static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame, NvencSurface *nvenc_frame)
Definition: nvenc.c:2229
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
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:820
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:454
AVPacket::opaque_ref
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
Definition: packet.h:569
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:188
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:715
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:502
NVENC_TWO_PASSES
@ NVENC_TWO_PASSES
Definition: nvenc.h:167
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:2111
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
nvenc_errors
static const struct @182 nvenc_errors[]
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:148
NVENC_ONE_PASS
@ NVENC_ONE_PASS
Definition: nvenc.h:166
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:671
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:180
AVFrameSideData::size
size_t size
Definition: frame.h:253
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
NVENC_DEPRECATED_PRESET
@ NVENC_DEPRECATED_PRESET
Definition: nvenc.h:169
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:524
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:723
ff_nvenc_encode_init
av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:2048
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
width
#define width
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1243
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:491
AV1_METADATA_TYPE_TIMECODE
@ AV1_METADATA_TYPE_TIMECODE
Definition: av1.h:48
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1411
AV_PIX_FMT_0BGR32
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:456
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:1929
timestamp_queue_enqueue
static void timestamp_queue_enqueue(AVFifo *queue, int64_t timestamp)
Definition: nvenc.c:2337
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:1296
timestamp_queue_dequeue
static int64_t timestamp_queue_dequeue(AVFifo *queue)
Definition: nvenc.c:2342
AVPacket::opaque
void * opaque
for some private data of the user
Definition: packet.h:558
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:1281
NVENC_CAP
#define NVENC_CAP
Definition: nvenc.c:46
AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:497
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:695
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:280
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:480
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:495
ff_nvenc_encode_close
av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:1963
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:368
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:544
NV_ENC_HEVC_PROFILE_MAIN
@ NV_ENC_HEVC_PROFILE_MAIN
Definition: nvenc.h:158
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:537
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:1031
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:386
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
AV1_METADATA_TYPE_ITUT_T35
@ AV1_METADATA_TYPE_ITUT_T35
Definition: av1.h:47
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:993
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:1827
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFrameSideData::data
uint8_t * data
Definition: frame.h:252
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:2175
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:2888
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:532
height
#define height
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:451
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:539
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:2100
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
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:804
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
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:526
FAST
@ FAST
Definition: vf_guided.c:32
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
process_output_surface
static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)
Definition: nvenc.c:2460
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:226
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:2571
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:608
AV_PIX_FMT_X2RGB10
#define AV_PIX_FMT_X2RGB10
Definition: pixfmt.h:536
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:1501
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:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
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:1796
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
AV_PIX_FMT_P016
#define AV_PIX_FMT_P016
Definition: pixfmt.h:530
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:1479
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:174
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:1614
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:455
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:2291
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AV_PROFILE_H264_HIGH
#define AV_PROFILE_H264_HIGH
Definition: defs.h:114
NVENC_LOWLATENCY
@ NVENC_LOWLATENCY
Definition: nvenc.h:164
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
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1260
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:1644
nvenc_setup_surfaces
static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx)
Definition: nvenc.c:1883
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:827
AVFrameSideData::type
enum AVFrameSideDataType type
Definition: frame.h:251
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:576
nvenc_find_free_reg_resource
static int nvenc_find_free_reg_resource(AVCodecContext *avctx)
Definition: nvenc.c:2141
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:528
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
LIST_DEVICES
@ LIST_DEVICES
Definition: nvenc.h:173
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:795
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:250
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
NVENC_LOSSLESS
@ NVENC_LOSSLESS
Definition: nvenc.h:165
AVCodecContext::slices
int slices
Number of slices.
Definition: avcodec.h:1047
DEFAULT
#define DEFAULT
Definition: avdct.c:29
AVPacket
This structure stores compressed data.
Definition: packet.h:510
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
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:618
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:608
NV_ENC_H264_PROFILE_MAIN
@ NV_ENC_H264_PROFILE_MAIN
Definition: nvenc.h:152
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:904
nvenc_setup_codec_config
static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx)
Definition: nvenc.c:1564
NV_ENC_H264_PROFILE_HIGH_444P
@ NV_ENC_H264_PROFILE_HIGH_444P
Definition: nvenc.h:154
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:642
nvenc_setup_hevc_config
static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
Definition: nvenc.c:1338
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:2775
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:2885
nvenc_retrieve_frame_data
static int nvenc_retrieve_frame_data(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *lock_params, AVPacket *pkt)
Definition: nvenc.c:2439