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