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  if (ctx->max_slice_size > 0) {
1170  h264->sliceMode = 1;
1171  h264->sliceModeData = ctx->max_slice_size;
1172  } else {
1173  h264->sliceMode = 3;
1174  h264->sliceModeData = avctx->slices > 0 ? avctx->slices : 1;
1175  }
1176 
1177  if (ctx->intra_refresh) {
1178  h264->enableIntraRefresh = 1;
1179  h264->intraRefreshPeriod = cc->gopLength;
1180  h264->intraRefreshCnt = cc->gopLength - 1;
1181  cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1182 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1183  h264->singleSliceIntraRefresh = ctx->single_slice_intra_refresh;
1184 #endif
1185  }
1186 
1187  if (ctx->constrained_encoding)
1188  h264->enableConstrainedEncoding = 1;
1189 
1190  h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1191  h264->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1192  h264->outputAUD = ctx->aud;
1193 
1194  if (ctx->dpb_size >= 0) {
1195  /* 0 means "let the hardware decide" */
1196  h264->maxNumRefFrames = ctx->dpb_size;
1197  }
1198 
1199  h264->idrPeriod = cc->gopLength;
1200 
1201  if (IS_CBR(cc->rcParams.rateControlMode)) {
1202  h264->outputBufferingPeriodSEI = 1;
1203  }
1204 
1205  h264->outputPictureTimingSEI = 1;
1206 
1207 #ifndef NVENC_NO_DEPRECATED_RC
1208  if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1209  cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1210  cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1211  h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1212  h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1213  }
1214 #endif
1215 
1216  if (ctx->flags & NVENC_LOSSLESS) {
1217  h264->qpPrimeYZeroTransformBypassFlag = 1;
1218  } else {
1219  switch(ctx->profile) {
1221  cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1223  break;
1225  cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1226  avctx->profile = AV_PROFILE_H264_MAIN;
1227  break;
1229  cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1230  avctx->profile = AV_PROFILE_H264_HIGH;
1231  break;
1233  cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1235  break;
1236  }
1237  }
1238 
1239  // force setting profile as high444p if input is AV_PIX_FMT_YUV444P
1240  if (IS_YUV444(ctx->data_pix_fmt)) {
1241  cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1243  }
1244 
1245  h264->chromaFormatIDC = avctx->profile == AV_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1;
1246 
1247  h264->level = ctx->level;
1248 
1249  if (ctx->coder >= 0)
1250  h264->entropyCodingMode = ctx->coder;
1251 
1252 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1253  if (ctx->b_ref_mode >= 0)
1254  h264->useBFramesAsRef = ctx->b_ref_mode;
1255 #endif
1256 
1257 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1258  h264->numRefL0 = avctx->refs;
1259  h264->numRefL1 = avctx->refs;
1260 #endif
1261 
1262  return 0;
1263 }
1264 
1266 {
1267  NvencContext *ctx = avctx->priv_data;
1268  NV_ENC_CONFIG *cc = &ctx->encode_config;
1269  NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1270  NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1271 
1272  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt);
1273 
1274  if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) {
1275  vui->colourMatrix = AVCOL_SPC_BT470BG;
1276  vui->colourPrimaries = avctx->color_primaries;
1277  vui->transferCharacteristics = avctx->color_trc;
1278  vui->videoFullRangeFlag = 0;
1279  } else {
1280  vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace;
1281  vui->colourPrimaries = avctx->color_primaries;
1282  vui->transferCharacteristics = avctx->color_trc;
1283  vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
1284  || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P);
1285  }
1286 
1287  vui->colourDescriptionPresentFlag =
1288  (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1289 
1290  vui->videoSignalTypePresentFlag =
1291  (vui->colourDescriptionPresentFlag
1292  || vui->videoFormat != 5
1293  || vui->videoFullRangeFlag != 0);
1294 
1295  if (ctx->max_slice_size > 0) {
1296  hevc->sliceMode = 1;
1297  hevc->sliceModeData = ctx->max_slice_size;
1298  } else {
1299  hevc->sliceMode = 3;
1300  hevc->sliceModeData = avctx->slices > 0 ? avctx->slices : 1;
1301  }
1302 
1303  if (ctx->intra_refresh) {
1304  hevc->enableIntraRefresh = 1;
1305  hevc->intraRefreshPeriod = cc->gopLength;
1306  hevc->intraRefreshCnt = cc->gopLength - 1;
1307  cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1308 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1309  hevc->singleSliceIntraRefresh = ctx->single_slice_intra_refresh;
1310 #endif
1311  }
1312 
1313 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1314  if (ctx->constrained_encoding)
1315  hevc->enableConstrainedEncoding = 1;
1316 #endif
1317 
1318  hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1319  hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1320  hevc->outputAUD = ctx->aud;
1321 
1322  if (ctx->dpb_size >= 0) {
1323  /* 0 means "let the hardware decide" */
1324  hevc->maxNumRefFramesInDPB = ctx->dpb_size;
1325  }
1326 
1327  hevc->idrPeriod = cc->gopLength;
1328 
1329  if (IS_CBR(cc->rcParams.rateControlMode)) {
1330  hevc->outputBufferingPeriodSEI = 1;
1331  }
1332 
1333  hevc->outputPictureTimingSEI = 1;
1334 
1335  switch (ctx->profile) {
1337  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1338  avctx->profile = AV_PROFILE_HEVC_MAIN;
1339  break;
1341  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1343  break;
1345  cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1346  avctx->profile = AV_PROFILE_HEVC_REXT;
1347  break;
1348  }
1349 
1350  // force setting profile as main10 if input is 10 bit
1351  if (IS_10BIT(ctx->data_pix_fmt)) {
1352  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1354  }
1355 
1356  // force setting profile as rext if input is yuv444
1357  if (IS_YUV444(ctx->data_pix_fmt)) {
1358  cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1359  avctx->profile = AV_PROFILE_HEVC_REXT;
1360  }
1361 
1362  hevc->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1;
1363 
1364  hevc->pixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0;
1365 
1366  hevc->level = ctx->level;
1367 
1368  hevc->tier = ctx->tier;
1369 
1370 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1371  if (ctx->b_ref_mode >= 0)
1372  hevc->useBFramesAsRef = ctx->b_ref_mode;
1373 #endif
1374 
1375 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1376  hevc->numRefL0 = avctx->refs;
1377  hevc->numRefL1 = avctx->refs;
1378 #endif
1379 
1380  return 0;
1381 }
1382 
1383 #if CONFIG_AV1_NVENC_ENCODER
1384 static av_cold int nvenc_setup_av1_config(AVCodecContext *avctx)
1385 {
1386  NvencContext *ctx = avctx->priv_data;
1387  NV_ENC_CONFIG *cc = &ctx->encode_config;
1388  NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1389 
1390  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt);
1391 
1392  if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) {
1393  av1->matrixCoefficients = AVCOL_SPC_BT470BG;
1394  av1->colorPrimaries = avctx->color_primaries;
1395  av1->transferCharacteristics = avctx->color_trc;
1396  av1->colorRange = 0;
1397  } else {
1398  av1->matrixCoefficients = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace;
1399  av1->colorPrimaries = avctx->color_primaries;
1400  av1->transferCharacteristics = avctx->color_trc;
1401  av1->colorRange = (avctx->color_range == AVCOL_RANGE_JPEG
1402  || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P);
1403  }
1404 
1405  if (IS_YUV444(ctx->data_pix_fmt)) {
1406  av_log(avctx, AV_LOG_ERROR, "AV1 High Profile not supported, required for 4:4:4 encoding\n");
1407  return AVERROR(ENOTSUP);
1408  } else {
1409  cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1410  avctx->profile = AV_PROFILE_AV1_MAIN;
1411  }
1412 
1413  if (ctx->dpb_size >= 0) {
1414  /* 0 means "let the hardware decide" */
1415  av1->maxNumRefFramesInDPB = ctx->dpb_size;
1416  }
1417 
1418  if (ctx->intra_refresh) {
1419  av1->enableIntraRefresh = 1;
1420  av1->intraRefreshPeriod = cc->gopLength;
1421  av1->intraRefreshCnt = cc->gopLength - 1;
1422  cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1423  }
1424 
1425  av1->idrPeriod = cc->gopLength;
1426 
1427  if (IS_CBR(cc->rcParams.rateControlMode)) {
1428  av1->enableBitstreamPadding = 1;
1429  }
1430 
1431  if (ctx->tile_cols >= 0)
1432  av1->numTileColumns = ctx->tile_cols;
1433  if (ctx->tile_rows >= 0)
1434  av1->numTileRows = ctx->tile_rows;
1435 
1436  av1->outputAnnexBFormat = 0;
1437 
1438  av1->level = ctx->level;
1439  av1->tier = ctx->tier;
1440 
1441  av1->enableTimingInfo = ctx->timing_info;
1442 
1443  /* mp4 encapsulation requires sequence headers to be present on all keyframes for AV1 */
1444  av1->disableSeqHdr = 0;
1445  av1->repeatSeqHdr = 1;
1446 
1447  av1->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1;
1448 
1449  av1->inputPixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0;
1450  av1->pixelBitDepthMinus8 = (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) ? 2 : 0;
1451 
1452  if (ctx->b_ref_mode >= 0)
1453  av1->useBFramesAsRef = ctx->b_ref_mode;
1454 
1455  av1->numFwdRefs = avctx->refs;
1456  av1->numBwdRefs = avctx->refs;
1457 
1458  return 0;
1459 }
1460 #endif
1461 
1463 {
1464  switch (avctx->codec->id) {
1465  case AV_CODEC_ID_H264:
1466  return nvenc_setup_h264_config(avctx);
1467  case AV_CODEC_ID_HEVC:
1468  return nvenc_setup_hevc_config(avctx);
1469 #if CONFIG_AV1_NVENC_ENCODER
1470  case AV_CODEC_ID_AV1:
1471  return nvenc_setup_av1_config(avctx);
1472 #endif
1473  /* Earlier switch/case will return if unknown codec is passed. */
1474  }
1475 
1476  return 0;
1477 }
1478 
1479 static void compute_dar(AVCodecContext *avctx, int *dw, int *dh) {
1480  int sw, sh;
1481 
1482  sw = avctx->width;
1483  sh = avctx->height;
1484 
1485 #if CONFIG_AV1_NVENC_ENCODER
1486  if (avctx->codec->id == AV_CODEC_ID_AV1) {
1487  /* For AV1 we actually need to calculate the render width/height, not the dar */
1488  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0
1489  && avctx->sample_aspect_ratio.num != avctx->sample_aspect_ratio.den)
1490  {
1491  if (avctx->sample_aspect_ratio.num > avctx->sample_aspect_ratio.den) {
1492  sw = av_rescale(sw, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
1493  } else {
1494  sh = av_rescale(sh, avctx->sample_aspect_ratio.den, avctx->sample_aspect_ratio.num);
1495  }
1496  }
1497 
1498  *dw = sw;
1499  *dh = sh;
1500  return;
1501  }
1502 #endif
1503 
1504  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
1505  sw *= avctx->sample_aspect_ratio.num;
1506  sh *= avctx->sample_aspect_ratio.den;
1507  }
1508 
1509  av_reduce(dw, dh, sw, sh, 1024 * 1024);
1510 }
1511 
1513 {
1514  NvencContext *ctx = avctx->priv_data;
1515  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1516  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1517 
1518  NV_ENC_PRESET_CONFIG preset_config = { 0 };
1519  NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1520  AVCPBProperties *cpb_props;
1521  int res = 0;
1522  int dw, dh;
1523 
1524  ctx->encode_config.version = NV_ENC_CONFIG_VER;
1525  ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1526 
1527  ctx->init_encode_params.encodeHeight = avctx->height;
1528  ctx->init_encode_params.encodeWidth = avctx->width;
1529 
1530  ctx->init_encode_params.encodeConfig = &ctx->encode_config;
1531 
1532  preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1533  preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1534 
1535 #ifdef NVENC_HAVE_NEW_PRESETS
1536  ctx->init_encode_params.tuningInfo = ctx->tuning_info;
1537 
1538  if (ctx->flags & NVENC_LOSSLESS)
1539  ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1540  else if (ctx->flags & NVENC_LOWLATENCY)
1541  ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1542 
1543  nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(ctx->nvencoder,
1544  ctx->init_encode_params.encodeGUID,
1545  ctx->init_encode_params.presetGUID,
1546  ctx->init_encode_params.tuningInfo,
1547  &preset_config);
1548 #else
1549  nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder,
1550  ctx->init_encode_params.encodeGUID,
1551  ctx->init_encode_params.presetGUID,
1552  &preset_config);
1553 #endif
1554  if (nv_status != NV_ENC_SUCCESS)
1555  return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration");
1556 
1557  memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
1558 
1559  ctx->encode_config.version = NV_ENC_CONFIG_VER;
1560 
1561  compute_dar(avctx, &dw, &dh);
1562  ctx->init_encode_params.darHeight = dh;
1563  ctx->init_encode_params.darWidth = dw;
1564 
1565  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
1566  ctx->init_encode_params.frameRateNum = avctx->framerate.num;
1567  ctx->init_encode_params.frameRateDen = avctx->framerate.den;
1568  } else {
1569  ctx->init_encode_params.frameRateNum = avctx->time_base.den;
1571  ctx->init_encode_params.frameRateDen = avctx->time_base.num
1572 #if FF_API_TICKS_PER_FRAME
1573  * avctx->ticks_per_frame
1574 #endif
1575  ;
1577  }
1578 
1579  ctx->init_encode_params.enableEncodeAsync = 0;
1580  ctx->init_encode_params.enablePTD = 1;
1581 
1582 #ifdef NVENC_HAVE_NEW_PRESETS
1583  /* If lookahead isn't set from CLI, use value from preset.
1584  * P6 & P7 presets may enable lookahead for better quality.
1585  * */
1586  if (ctx->rc_lookahead == 0 && ctx->encode_config.rcParams.enableLookahead)
1587  ctx->rc_lookahead = ctx->encode_config.rcParams.lookaheadDepth;
1588 #endif
1589 
1590  if (ctx->weighted_pred == 1)
1591  ctx->init_encode_params.enableWeightedPrediction = 1;
1592 
1593  if (ctx->bluray_compat) {
1594  ctx->aud = 1;
1595  ctx->dpb_size = FFMIN(FFMAX(avctx->refs, 0), 6);
1596  avctx->max_b_frames = FFMIN(avctx->max_b_frames, 3);
1597  switch (avctx->codec->id) {
1598  case AV_CODEC_ID_H264:
1599  /* maximum level depends on used resolution */
1600  break;
1601  case AV_CODEC_ID_HEVC:
1602  ctx->level = NV_ENC_LEVEL_HEVC_51;
1603  ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1604  break;
1605  }
1606  }
1607 
1608  if (avctx->gop_size > 0) {
1609  // only overwrite preset if a GOP size was selected as input
1610  ctx->encode_config.gopLength = avctx->gop_size;
1611  } else if (avctx->gop_size == 0) {
1612  ctx->encode_config.frameIntervalP = 0;
1613  ctx->encode_config.gopLength = 1;
1614  }
1615 
1616  if (avctx->max_b_frames >= 0 && ctx->encode_config.gopLength > 1) {
1617  /* 0 is intra-only, 1 is I/P only, 2 is one B-Frame, 3 two B-frames, and so on. */
1618  ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
1619  }
1620 
1621  /* force to enable intra refresh */
1622  if(ctx->single_slice_intra_refresh)
1623  ctx->intra_refresh = 1;
1624 
1625  nvenc_recalc_surfaces(avctx);
1626 
1627  nvenc_setup_rate_control(avctx);
1628 
1629  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
1630  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1631  } else {
1632  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1633  }
1634 
1635  res = nvenc_setup_codec_config(avctx);
1636  if (res)
1637  return res;
1638 
1639  res = nvenc_push_context(avctx);
1640  if (res < 0)
1641  return res;
1642 
1643  nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
1644  if (nv_status != NV_ENC_SUCCESS) {
1645  nvenc_pop_context(avctx);
1646  return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed");
1647  }
1648 
1649 #ifdef NVENC_HAVE_CUSTREAM_PTR
1650  if (ctx->cu_context) {
1651  nv_status = p_nvenc->nvEncSetIOCudaStreams(ctx->nvencoder, &ctx->cu_stream, &ctx->cu_stream);
1652  if (nv_status != NV_ENC_SUCCESS) {
1653  nvenc_pop_context(avctx);
1654  return nvenc_print_error(avctx, nv_status, "SetIOCudaStreams failed");
1655  }
1656  }
1657 #endif
1658 
1659  res = nvenc_pop_context(avctx);
1660  if (res < 0)
1661  return res;
1662 
1663  if (ctx->encode_config.frameIntervalP > 1)
1664  avctx->has_b_frames = 2;
1665 
1666  if (ctx->encode_config.rcParams.averageBitRate > 0)
1667  avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
1668 
1669  cpb_props = ff_encode_add_cpb_side_data(avctx);
1670  if (!cpb_props)
1671  return AVERROR(ENOMEM);
1672  cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate;
1673  cpb_props->avg_bitrate = avctx->bit_rate;
1674  cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize;
1675 
1676  return 0;
1677 }
1678 
1679 static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt)
1680 {
1681  switch (pix_fmt) {
1682  case AV_PIX_FMT_YUV420P:
1683  return NV_ENC_BUFFER_FORMAT_YV12_PL;
1684  case AV_PIX_FMT_NV12:
1685  return NV_ENC_BUFFER_FORMAT_NV12_PL;
1686  case AV_PIX_FMT_P010:
1687  case AV_PIX_FMT_P016:
1688  return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1689  case AV_PIX_FMT_GBRP:
1690  case AV_PIX_FMT_YUV444P:
1691  return NV_ENC_BUFFER_FORMAT_YUV444_PL;
1692  case AV_PIX_FMT_GBRP16:
1693  case AV_PIX_FMT_YUV444P16:
1694  return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1695  case AV_PIX_FMT_0RGB32:
1696  case AV_PIX_FMT_RGB32:
1697  return NV_ENC_BUFFER_FORMAT_ARGB;
1698  case AV_PIX_FMT_0BGR32:
1699  case AV_PIX_FMT_BGR32:
1700  return NV_ENC_BUFFER_FORMAT_ABGR;
1701  case AV_PIX_FMT_X2RGB10:
1702  return NV_ENC_BUFFER_FORMAT_ARGB10;
1703  case AV_PIX_FMT_X2BGR10:
1704  return NV_ENC_BUFFER_FORMAT_ABGR10;
1705  default:
1706  return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1707  }
1708 }
1709 
1710 static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
1711 {
1712  NvencContext *ctx = avctx->priv_data;
1713  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1714  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1715  NvencSurface* tmp_surface = &ctx->surfaces[idx];
1716 
1717  NVENCSTATUS nv_status;
1718  NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1719  allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1720 
1721  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1722  ctx->surfaces[idx].in_ref = av_frame_alloc();
1723  if (!ctx->surfaces[idx].in_ref)
1724  return AVERROR(ENOMEM);
1725  } else {
1726  NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1727 
1728  ctx->surfaces[idx].format = nvenc_map_buffer_format(ctx->data_pix_fmt);
1729  if (ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1730  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
1731  av_get_pix_fmt_name(ctx->data_pix_fmt));
1732  return AVERROR(EINVAL);
1733  }
1734 
1735  allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1736  allocSurf.width = avctx->width;
1737  allocSurf.height = avctx->height;
1738  allocSurf.bufferFmt = ctx->surfaces[idx].format;
1739 
1740  nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
1741  if (nv_status != NV_ENC_SUCCESS) {
1742  return nvenc_print_error(avctx, nv_status, "CreateInputBuffer failed");
1743  }
1744 
1745  ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1746  ctx->surfaces[idx].width = allocSurf.width;
1747  ctx->surfaces[idx].height = allocSurf.height;
1748  }
1749 
1750  nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
1751  if (nv_status != NV_ENC_SUCCESS) {
1752  int err = nvenc_print_error(avctx, nv_status, "CreateBitstreamBuffer failed");
1753  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1754  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[idx].input_surface);
1755  av_frame_free(&ctx->surfaces[idx].in_ref);
1756  return err;
1757  }
1758 
1759  ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1760 
1761  av_fifo_write(ctx->unused_surface_queue, &tmp_surface, 1);
1762 
1763  return 0;
1764 }
1765 
1767 {
1768  NvencContext *ctx = avctx->priv_data;
1769  int i, res = 0, res2;
1770 
1771  ctx->surfaces = av_calloc(ctx->nb_surfaces, sizeof(*ctx->surfaces));
1772  if (!ctx->surfaces)
1773  return AVERROR(ENOMEM);
1774 
1775  ctx->frame_data_array = av_calloc(ctx->frame_data_array_nb, sizeof(*ctx->frame_data_array));
1776  if (!ctx->frame_data_array)
1777  return AVERROR(ENOMEM);
1778 
1779  ctx->timestamp_list = av_fifo_alloc2(ctx->nb_surfaces, sizeof(int64_t), 0);
1780  if (!ctx->timestamp_list)
1781  return AVERROR(ENOMEM);
1782 
1783  ctx->unused_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
1784  if (!ctx->unused_surface_queue)
1785  return AVERROR(ENOMEM);
1786 
1787  ctx->output_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
1788  if (!ctx->output_surface_queue)
1789  return AVERROR(ENOMEM);
1790  ctx->output_surface_ready_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
1791  if (!ctx->output_surface_ready_queue)
1792  return AVERROR(ENOMEM);
1793 
1794  res = nvenc_push_context(avctx);
1795  if (res < 0)
1796  return res;
1797 
1798  for (i = 0; i < ctx->nb_surfaces; i++) {
1799  if ((res = nvenc_alloc_surface(avctx, i)) < 0)
1800  goto fail;
1801  }
1802 
1803 fail:
1804  res2 = nvenc_pop_context(avctx);
1805  if (res2 < 0)
1806  return res2;
1807 
1808  return res;
1809 }
1810 
1812 {
1813  NvencContext *ctx = avctx->priv_data;
1814  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1815  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1816 
1817  NVENCSTATUS nv_status;
1818  uint32_t outSize = 0;
1819  char tmpHeader[NV_MAX_SEQ_HDR_LEN];
1820 
1821  NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1822  payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1823 
1824  payload.spsppsBuffer = tmpHeader;
1825  payload.inBufferSize = sizeof(tmpHeader);
1826  payload.outSPSPPSPayloadSize = &outSize;
1827 
1828  nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
1829  if (nv_status != NV_ENC_SUCCESS) {
1830  return nvenc_print_error(avctx, nv_status, "GetSequenceParams failed");
1831  }
1832 
1833  avctx->extradata_size = outSize;
1835 
1836  if (!avctx->extradata) {
1837  return AVERROR(ENOMEM);
1838  }
1839 
1840  memcpy(avctx->extradata, tmpHeader, outSize);
1841 
1842  return 0;
1843 }
1844 
1846 {
1847  NvencContext *ctx = avctx->priv_data;
1848  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1849  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1850  int i, res;
1851 
1852  /* the encoder has to be flushed before it can be closed */
1853  if (ctx->nvencoder) {
1854  NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1855  .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1856 
1857  res = nvenc_push_context(avctx);
1858  if (res < 0)
1859  return res;
1860 
1861  p_nvenc->nvEncEncodePicture(ctx->nvencoder, &params);
1862  }
1863 
1864  av_fifo_freep2(&ctx->timestamp_list);
1865  av_fifo_freep2(&ctx->output_surface_ready_queue);
1866  av_fifo_freep2(&ctx->output_surface_queue);
1867  av_fifo_freep2(&ctx->unused_surface_queue);
1868 
1869  if (ctx->frame_data_array) {
1870  for (i = 0; i < ctx->nb_surfaces; i++)
1871  av_buffer_unref(&ctx->frame_data_array[i].frame_opaque_ref);
1872  av_freep(&ctx->frame_data_array);
1873  }
1874 
1875  if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) {
1876  for (i = 0; i < ctx->nb_registered_frames; i++) {
1877  if (ctx->registered_frames[i].mapped)
1878  p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[i].in_map.mappedResource);
1879  if (ctx->registered_frames[i].regptr)
1880  p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr);
1881  }
1882  ctx->nb_registered_frames = 0;
1883  }
1884 
1885  if (ctx->surfaces) {
1886  for (i = 0; i < ctx->nb_surfaces; ++i) {
1887  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1888  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[i].input_surface);
1889  av_frame_free(&ctx->surfaces[i].in_ref);
1890  p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->surfaces[i].output_surface);
1891  }
1892  }
1893  av_freep(&ctx->surfaces);
1894  ctx->nb_surfaces = 0;
1895 
1896  av_frame_free(&ctx->frame);
1897 
1898  av_freep(&ctx->sei_data);
1899 
1900  if (ctx->nvencoder) {
1901  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
1902 
1903  res = nvenc_pop_context(avctx);
1904  if (res < 0)
1905  return res;
1906  }
1907  ctx->nvencoder = NULL;
1908 
1909  if (ctx->cu_context_internal)
1910  CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal));
1911  ctx->cu_context = ctx->cu_context_internal = NULL;
1912 
1913 #if CONFIG_D3D11VA
1914  if (ctx->d3d11_device) {
1915  ID3D11Device_Release(ctx->d3d11_device);
1916  ctx->d3d11_device = NULL;
1917  }
1918 #endif
1919 
1920  nvenc_free_functions(&dl_fn->nvenc_dl);
1921  cuda_free_functions(&dl_fn->cuda_dl);
1922 
1923  dl_fn->nvenc_device_count = 0;
1924 
1925  av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
1926 
1927  return 0;
1928 }
1929 
1931 {
1932  NvencContext *ctx = avctx->priv_data;
1933  int ret;
1934 
1935  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1936  AVHWFramesContext *frames_ctx;
1937  if (!avctx->hw_frames_ctx) {
1938  av_log(avctx, AV_LOG_ERROR,
1939  "hw_frames_ctx must be set when using GPU frames as input\n");
1940  return AVERROR(EINVAL);
1941  }
1942  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1943  if (frames_ctx->format != avctx->pix_fmt) {
1944  av_log(avctx, AV_LOG_ERROR,
1945  "hw_frames_ctx must match the GPU frame type\n");
1946  return AVERROR(EINVAL);
1947  }
1948  ctx->data_pix_fmt = frames_ctx->sw_format;
1949  } else {
1950  ctx->data_pix_fmt = avctx->pix_fmt;
1951  }
1952 
1953  ctx->frame = av_frame_alloc();
1954  if (!ctx->frame)
1955  return AVERROR(ENOMEM);
1956 
1957  if ((ret = nvenc_load_libraries(avctx)) < 0)
1958  return ret;
1959 
1960  if ((ret = nvenc_setup_device(avctx)) < 0)
1961  return ret;
1962 
1963  if ((ret = nvenc_setup_encoder(avctx)) < 0)
1964  return ret;
1965 
1966  if ((ret = nvenc_setup_surfaces(avctx)) < 0)
1967  return ret;
1968 
1969  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1970  if ((ret = nvenc_setup_extradata(avctx)) < 0)
1971  return ret;
1972  }
1973 
1974  return 0;
1975 }
1976 
1978 {
1979  NvencSurface *tmp_surf;
1980 
1981  if (av_fifo_read(ctx->unused_surface_queue, &tmp_surf, 1) < 0)
1982  // queue empty
1983  return NULL;
1984 
1985  return tmp_surf;
1986 }
1987 
1988 static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface,
1989  NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame)
1990 {
1991  int dst_linesize[4] = {
1992  lock_buffer_params->pitch,
1993  lock_buffer_params->pitch,
1994  lock_buffer_params->pitch,
1995  lock_buffer_params->pitch
1996  };
1997  uint8_t *dst_data[4];
1998  int ret;
1999 
2000  if (frame->format == AV_PIX_FMT_YUV420P)
2001  dst_linesize[1] = dst_linesize[2] >>= 1;
2002 
2003  ret = av_image_fill_pointers(dst_data, frame->format, nv_surface->height,
2004  lock_buffer_params->bufferDataPtr, dst_linesize);
2005  if (ret < 0)
2006  return ret;
2007 
2008  if (frame->format == AV_PIX_FMT_YUV420P)
2009  FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2010 
2011  av_image_copy2(dst_data, dst_linesize,
2012  frame->data, frame->linesize, frame->format,
2013  avctx->width, avctx->height);
2014 
2015  return 0;
2016 }
2017 
2019 {
2020  NvencContext *ctx = avctx->priv_data;
2021  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2022  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2023  NVENCSTATUS nv_status;
2024 
2025  int i, first_round;
2026 
2027  if (ctx->nb_registered_frames == FF_ARRAY_ELEMS(ctx->registered_frames)) {
2028  for (first_round = 1; first_round >= 0; first_round--) {
2029  for (i = 0; i < ctx->nb_registered_frames; i++) {
2030  if (!ctx->registered_frames[i].mapped) {
2031  if (ctx->registered_frames[i].regptr) {
2032  if (first_round)
2033  continue;
2034  nv_status = p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr);
2035  if (nv_status != NV_ENC_SUCCESS)
2036  return nvenc_print_error(avctx, nv_status, "Failed unregistering unused input resource");
2037  ctx->registered_frames[i].ptr = NULL;
2038  ctx->registered_frames[i].regptr = NULL;
2039  }
2040  return i;
2041  }
2042  }
2043  }
2044  } else {
2045  return ctx->nb_registered_frames++;
2046  }
2047 
2048  av_log(avctx, AV_LOG_ERROR, "Too many registered CUDA frames\n");
2049  return AVERROR(ENOMEM);
2050 }
2051 
2053 {
2054  NvencContext *ctx = avctx->priv_data;
2055  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2056  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2057 
2058  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frame->hw_frames_ctx->data;
2059  NV_ENC_REGISTER_RESOURCE reg = { 0 };
2060  int i, idx, ret;
2061 
2062  for (i = 0; i < ctx->nb_registered_frames; i++) {
2063  if (avctx->pix_fmt == AV_PIX_FMT_CUDA && ctx->registered_frames[i].ptr == frame->data[0])
2064  return i;
2065  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])
2066  return i;
2067  }
2068 
2069  idx = nvenc_find_free_reg_resource(avctx);
2070  if (idx < 0)
2071  return idx;
2072 
2073  reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2074  reg.width = frames_ctx->width;
2075  reg.height = frames_ctx->height;
2076  reg.pitch = frame->linesize[0];
2077  reg.resourceToRegister = frame->data[0];
2078 
2079  if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
2080  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2081  }
2082  else if (avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2083  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2084  reg.subResourceIndex = (intptr_t)frame->data[1];
2085  }
2086 
2087  reg.bufferFormat = nvenc_map_buffer_format(frames_ctx->sw_format);
2088  if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2089  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
2090  av_get_pix_fmt_name(frames_ctx->sw_format));
2091  return AVERROR(EINVAL);
2092  }
2093 
2094  ret = p_nvenc->nvEncRegisterResource(ctx->nvencoder, &reg);
2095  if (ret != NV_ENC_SUCCESS) {
2096  nvenc_print_error(avctx, ret, "Error registering an input resource");
2097  return AVERROR_UNKNOWN;
2098  }
2099 
2100  ctx->registered_frames[idx].ptr = frame->data[0];
2101  ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2102  ctx->registered_frames[idx].regptr = reg.registeredResource;
2103  return idx;
2104 }
2105 
2107  NvencSurface *nvenc_frame)
2108 {
2109  NvencContext *ctx = avctx->priv_data;
2110  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2111  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2112 
2113  int res;
2114  NVENCSTATUS nv_status;
2115 
2116  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2117  int reg_idx = nvenc_register_frame(avctx, frame);
2118  if (reg_idx < 0) {
2119  av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n");
2120  return reg_idx;
2121  }
2122 
2123  res = av_frame_ref(nvenc_frame->in_ref, frame);
2124  if (res < 0)
2125  return res;
2126 
2127  if (!ctx->registered_frames[reg_idx].mapped) {
2128  ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2129  ctx->registered_frames[reg_idx].in_map.registeredResource = ctx->registered_frames[reg_idx].regptr;
2130  nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &ctx->registered_frames[reg_idx].in_map);
2131  if (nv_status != NV_ENC_SUCCESS) {
2132  av_frame_unref(nvenc_frame->in_ref);
2133  return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");
2134  }
2135  }
2136 
2137  ctx->registered_frames[reg_idx].mapped += 1;
2138 
2139  nvenc_frame->reg_idx = reg_idx;
2140  nvenc_frame->input_surface = ctx->registered_frames[reg_idx].in_map.mappedResource;
2141  nvenc_frame->format = ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2142  nvenc_frame->pitch = frame->linesize[0];
2143 
2144  return 0;
2145  } else {
2146  NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2147 
2148  lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2149  lockBufferParams.inputBuffer = nvenc_frame->input_surface;
2150 
2151  nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
2152  if (nv_status != NV_ENC_SUCCESS) {
2153  return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer");
2154  }
2155 
2156  nvenc_frame->pitch = lockBufferParams.pitch;
2157  res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame);
2158 
2159  nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, nvenc_frame->input_surface);
2160  if (nv_status != NV_ENC_SUCCESS) {
2161  return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!");
2162  }
2163 
2164  return res;
2165  }
2166 }
2167 
2169  NV_ENC_PIC_PARAMS *params,
2170  NV_ENC_SEI_PAYLOAD *sei_data,
2171  int sei_count)
2172 {
2173  NvencContext *ctx = avctx->priv_data;
2174 
2175  switch (avctx->codec->id) {
2176  case AV_CODEC_ID_H264:
2177  params->codecPicParams.h264PicParams.sliceMode =
2178  ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2179  params->codecPicParams.h264PicParams.sliceModeData =
2180  ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2181  if (sei_count > 0) {
2182  params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2183  params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2184  }
2185 
2186  break;
2187  case AV_CODEC_ID_HEVC:
2188  params->codecPicParams.hevcPicParams.sliceMode =
2189  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2190  params->codecPicParams.hevcPicParams.sliceModeData =
2191  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2192  if (sei_count > 0) {
2193  params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2194  params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2195  }
2196 
2197  break;
2198 #if CONFIG_AV1_NVENC_ENCODER
2199  case AV_CODEC_ID_AV1:
2200  params->codecPicParams.av1PicParams.numTileColumns =
2201  ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2202  params->codecPicParams.av1PicParams.numTileRows =
2203  ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2204  if (sei_count > 0) {
2205  params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2206  params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2207  }
2208 
2209  break;
2210 #endif
2211  }
2212 }
2213 
2214 static inline void timestamp_queue_enqueue(AVFifo *queue, int64_t timestamp)
2215 {
2216  av_fifo_write(queue, &timestamp, 1);
2217 }
2218 
2219 static inline int64_t timestamp_queue_dequeue(AVFifo *queue)
2220 {
2221  int64_t timestamp = AV_NOPTS_VALUE;
2222  // The following call might fail if the queue is empty.
2223  av_fifo_read(queue, &timestamp, 1);
2224 
2225  return timestamp;
2226 }
2227 
2229  NV_ENC_LOCK_BITSTREAM *params,
2230  AVPacket *pkt)
2231 {
2232  NvencContext *ctx = avctx->priv_data;
2233 
2234  pkt->pts = params->outputTimeStamp;
2235 
2238  pkt->dts = timestamp_queue_dequeue(ctx->timestamp_list) -
2239 #if FF_API_TICKS_PER_FRAME
2240  FFMAX(avctx->ticks_per_frame, 1) *
2241 #endif
2242  FFMAX(ctx->encode_config.frameIntervalP - 1, 0);
2244  } else {
2245  pkt->dts = pkt->pts;
2246  }
2247 
2248  return 0;
2249 }
2250 
2251 static int nvenc_store_frame_data(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *pic_params, const AVFrame *frame)
2252 {
2253  NvencContext *ctx = avctx->priv_data;
2254  int res = 0;
2255 
2256  int idx = ctx->frame_data_array_pos;
2257  NvencFrameData *frame_data = &ctx->frame_data_array[idx];
2258 
2259  // in case the encoder got reconfigured, there might be leftovers
2261 
2262  if (frame->opaque_ref && avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
2265  return AVERROR(ENOMEM);
2266  }
2267 
2268  frame_data->duration = frame->duration;
2269  frame_data->frame_opaque = frame->opaque;
2270 
2271 #if FF_API_REORDERED_OPAQUE
2273  frame_data->reordered_opaque = frame->reordered_opaque;
2275 #endif
2276 
2277  ctx->frame_data_array_pos = (ctx->frame_data_array_pos + 1) % ctx->frame_data_array_nb;
2278  pic_params->inputDuration = idx;
2279 
2280  return res;
2281 }
2282 
2283 static int nvenc_retrieve_frame_data(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *lock_params, AVPacket *pkt)
2284 {
2285  NvencContext *ctx = avctx->priv_data;
2286  int res = 0;
2287 
2288  int idx = lock_params->outputDuration;
2289  NvencFrameData *frame_data = &ctx->frame_data_array[idx];
2290 
2292 
2293 #if FF_API_REORDERED_OPAQUE
2295  avctx->reordered_opaque = frame_data->reordered_opaque;
2297 #endif
2298 
2299  if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
2303  }
2304 
2306 
2307  return res;
2308 }
2309 
2311 {
2312  NvencContext *ctx = avctx->priv_data;
2313  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2314  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2315 
2316  NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2317  NVENCSTATUS nv_status;
2318  int res = 0;
2319 
2320  enum AVPictureType pict_type;
2321 
2322  lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2323 
2324  lock_params.doNotWait = 0;
2325  lock_params.outputBitstream = tmpoutsurf->output_surface;
2326 
2327  nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
2328  if (nv_status != NV_ENC_SUCCESS) {
2329  res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer");
2330  goto error;
2331  }
2332 
2333  res = ff_get_encode_buffer(avctx, pkt, lock_params.bitstreamSizeInBytes, 0);
2334 
2335  if (res < 0) {
2336  p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
2337  goto error;
2338  }
2339 
2340  memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2341 
2342  nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
2343  if (nv_status != NV_ENC_SUCCESS) {
2344  res = nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");
2345  goto error;
2346  }
2347 
2348 
2349  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
2350  ctx->registered_frames[tmpoutsurf->reg_idx].mapped -= 1;
2351  if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped == 0) {
2352  nv_status = p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[tmpoutsurf->reg_idx].in_map.mappedResource);
2353  if (nv_status != NV_ENC_SUCCESS) {
2354  res = nvenc_print_error(avctx, nv_status, "Failed unmapping input resource");
2355  goto error;
2356  }
2357  } else if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped < 0) {
2358  res = AVERROR_BUG;
2359  goto error;
2360  }
2361 
2362  av_frame_unref(tmpoutsurf->in_ref);
2363 
2364  tmpoutsurf->input_surface = NULL;
2365  }
2366 
2367  switch (lock_params.pictureType) {
2368  case NV_ENC_PIC_TYPE_IDR:
2370  case NV_ENC_PIC_TYPE_I:
2371  pict_type = AV_PICTURE_TYPE_I;
2372  break;
2373  case NV_ENC_PIC_TYPE_P:
2374  pict_type = AV_PICTURE_TYPE_P;
2375  break;
2376  case NV_ENC_PIC_TYPE_B:
2377  pict_type = AV_PICTURE_TYPE_B;
2378  break;
2379  case NV_ENC_PIC_TYPE_BI:
2380  pict_type = AV_PICTURE_TYPE_BI;
2381  break;
2382  default:
2383  av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
2384  av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
2385  res = AVERROR_EXTERNAL;
2386  goto error;
2387  }
2388 
2390  (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type);
2391 
2392  res = nvenc_set_timestamp(avctx, &lock_params, pkt);
2393  if (res < 0)
2394  goto error2;
2395 
2396  res = nvenc_retrieve_frame_data(avctx, &lock_params, pkt);
2397  if (res < 0)
2398  goto error2;
2399 
2400  return 0;
2401 
2402 error:
2403  timestamp_queue_dequeue(ctx->timestamp_list);
2404 
2405 error2:
2406  return res;
2407 }
2408 
2409 static int output_ready(AVCodecContext *avctx, int flush)
2410 {
2411  NvencContext *ctx = avctx->priv_data;
2412  int nb_ready, nb_pending;
2413 
2414  nb_ready = av_fifo_can_read(ctx->output_surface_ready_queue);
2415  nb_pending = av_fifo_can_read(ctx->output_surface_queue);
2416  if (flush)
2417  return nb_ready > 0;
2418  return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth);
2419 }
2420 
2422 {
2423  NvencContext *ctx = avctx->priv_data;
2424  int sei_count = 0;
2425  int i, res;
2426 
2428  void *a53_data = NULL;
2429  size_t a53_size = 0;
2430 
2431  if (ff_alloc_a53_sei(frame, 0, &a53_data, &a53_size) < 0) {
2432  av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2433  }
2434 
2435  if (a53_data) {
2436  void *tmp = av_fast_realloc(ctx->sei_data,
2437  &ctx->sei_data_size,
2438  (sei_count + 1) * sizeof(*ctx->sei_data));
2439  if (!tmp) {
2440  av_free(a53_data);
2441  res = AVERROR(ENOMEM);
2442  goto error;
2443  } else {
2444  ctx->sei_data = tmp;
2445  ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2446  ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2447 
2448 #if CONFIG_AV1_NVENC_ENCODER
2449  if (avctx->codec->id == AV_CODEC_ID_AV1)
2450  ctx->sei_data[sei_count].payloadType = AV1_METADATA_TYPE_ITUT_T35;
2451  else
2452 #endif
2453  ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35;
2454 
2455  sei_count++;
2456  }
2457  }
2458  }
2459 
2461  void *tc_data = NULL;
2462  size_t tc_size = 0;
2463 
2464  if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, &tc_data, &tc_size) < 0) {
2465  av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n");
2466  }
2467 
2468  if (tc_data) {
2469  void *tmp = av_fast_realloc(ctx->sei_data,
2470  &ctx->sei_data_size,
2471  (sei_count + 1) * sizeof(*ctx->sei_data));
2472  if (!tmp) {
2473  av_free(tc_data);
2474  res = AVERROR(ENOMEM);
2475  goto error;
2476  } else {
2477  ctx->sei_data = tmp;
2478  ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2479  ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2480 
2481 #if CONFIG_AV1_NVENC_ENCODER
2482  if (avctx->codec->id == AV_CODEC_ID_AV1)
2483  ctx->sei_data[sei_count].payloadType = AV1_METADATA_TYPE_TIMECODE;
2484  else
2485 #endif
2486  ctx->sei_data[sei_count].payloadType = SEI_TYPE_TIME_CODE;
2487 
2488  sei_count++;
2489  }
2490  }
2491  }
2492 
2493  if (!ctx->udu_sei)
2494  return sei_count;
2495 
2496  for (i = 0; i < frame->nb_side_data; i++) {
2497  AVFrameSideData *side_data = frame->side_data[i];
2498  void *tmp;
2499 
2500  if (side_data->type != AV_FRAME_DATA_SEI_UNREGISTERED)
2501  continue;
2502 
2503  tmp = av_fast_realloc(ctx->sei_data,
2504  &ctx->sei_data_size,
2505  (sei_count + 1) * sizeof(*ctx->sei_data));
2506  if (!tmp) {
2507  res = AVERROR(ENOMEM);
2508  goto error;
2509  } else {
2510  ctx->sei_data = tmp;
2511  ctx->sei_data[sei_count].payloadSize = side_data->size;
2512  ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_UNREGISTERED;
2513  ctx->sei_data[sei_count].payload = av_memdup(side_data->data, side_data->size);
2514 
2515  if (!ctx->sei_data[sei_count].payload) {
2516  res = AVERROR(ENOMEM);
2517  goto error;
2518  }
2519 
2520  sei_count++;
2521  }
2522  }
2523 
2524  return sei_count;
2525 
2526 error:
2527  for (i = 0; i < sei_count; i++)
2528  av_freep(&(ctx->sei_data[i].payload));
2529 
2530  return res;
2531 }
2532 
2533 static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
2534 {
2535  NvencContext *ctx = avctx->priv_data;
2536  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
2537  NVENCSTATUS ret;
2538 
2539  NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2540  int needs_reconfig = 0;
2541  int needs_encode_config = 0;
2542  int reconfig_bitrate = 0, reconfig_dar = 0;
2543  int dw, dh;
2544 
2545  params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2546  params.reInitEncodeParams = ctx->init_encode_params;
2547 
2548  compute_dar(avctx, &dw, &dh);
2549  if (dw != ctx->init_encode_params.darWidth || dh != ctx->init_encode_params.darHeight) {
2550  av_log(avctx, AV_LOG_VERBOSE,
2551  "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2552  ctx->init_encode_params.darWidth,
2553  ctx->init_encode_params.darHeight, dw, dh);
2554 
2555  params.reInitEncodeParams.darHeight = dh;
2556  params.reInitEncodeParams.darWidth = dw;
2557 
2558  needs_reconfig = 1;
2559  reconfig_dar = 1;
2560  }
2561 
2562  if (ctx->rc != NV_ENC_PARAMS_RC_CONSTQP && ctx->support_dyn_bitrate) {
2563  if (avctx->bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->bit_rate) {
2564  av_log(avctx, AV_LOG_VERBOSE,
2565  "avg bitrate change: %d -> %d\n",
2566  params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2567  (uint32_t)avctx->bit_rate);
2568 
2569  params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->bit_rate;
2570  reconfig_bitrate = 1;
2571  }
2572 
2573  if (avctx->rc_max_rate > 0 && ctx->encode_config.rcParams.maxBitRate != avctx->rc_max_rate) {
2574  av_log(avctx, AV_LOG_VERBOSE,
2575  "max bitrate change: %d -> %d\n",
2576  params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2577  (uint32_t)avctx->rc_max_rate);
2578 
2579  params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->rc_max_rate;
2580  reconfig_bitrate = 1;
2581  }
2582 
2583  if (avctx->rc_buffer_size > 0 && ctx->encode_config.rcParams.vbvBufferSize != avctx->rc_buffer_size) {
2584  av_log(avctx, AV_LOG_VERBOSE,
2585  "vbv buffer size change: %d -> %d\n",
2586  params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2587  avctx->rc_buffer_size);
2588 
2589  params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->rc_buffer_size;
2590  reconfig_bitrate = 1;
2591  }
2592 
2593  if (reconfig_bitrate) {
2594  params.resetEncoder = 1;
2595  params.forceIDR = 1;
2596 
2597  needs_encode_config = 1;
2598  needs_reconfig = 1;
2599  }
2600  }
2601 
2602  if (!needs_encode_config)
2603  params.reInitEncodeParams.encodeConfig = NULL;
2604 
2605  if (needs_reconfig) {
2606  ret = p_nvenc->nvEncReconfigureEncoder(ctx->nvencoder, &params);
2607  if (ret != NV_ENC_SUCCESS) {
2608  nvenc_print_error(avctx, ret, "failed to reconfigure nvenc");
2609  } else {
2610  if (reconfig_dar) {
2611  ctx->init_encode_params.darHeight = dh;
2612  ctx->init_encode_params.darWidth = dw;
2613  }
2614 
2615  if (reconfig_bitrate) {
2616  ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2617  ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2618  ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2619  }
2620 
2621  }
2622  }
2623 }
2624 
2625 static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
2626 {
2627  NVENCSTATUS nv_status;
2628  NvencSurface *tmp_out_surf, *in_surf;
2629  int res, res2;
2630  int sei_count = 0;
2631  int i;
2632 
2633  NvencContext *ctx = avctx->priv_data;
2634  NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
2635  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2636 
2637  NV_ENC_PIC_PARAMS pic_params = { 0 };
2638  pic_params.version = NV_ENC_PIC_PARAMS_VER;
2639 
2640  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
2641  return AVERROR(EINVAL);
2642 
2643  if (frame && frame->buf[0]) {
2644  in_surf = get_free_frame(ctx);
2645  if (!in_surf)
2646  return AVERROR(EAGAIN);
2647 
2648  res = nvenc_push_context(avctx);
2649  if (res < 0)
2650  return res;
2651 
2652  reconfig_encoder(avctx, frame);
2653 
2654  res = nvenc_upload_frame(avctx, frame, in_surf);
2655 
2656  res2 = nvenc_pop_context(avctx);
2657  if (res2 < 0)
2658  return res2;
2659 
2660  if (res)
2661  return res;
2662 
2663  pic_params.inputBuffer = in_surf->input_surface;
2664  pic_params.bufferFmt = in_surf->format;
2665  pic_params.inputWidth = in_surf->width;
2666  pic_params.inputHeight = in_surf->height;
2667  pic_params.inputPitch = in_surf->pitch;
2668  pic_params.outputBitstream = in_surf->output_surface;
2669 
2670  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2671  if (frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)
2672  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2673  else
2674  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2675  } else {
2676  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2677  }
2678 
2679  if (ctx->forced_idr >= 0 && frame->pict_type == AV_PICTURE_TYPE_I) {
2680  pic_params.encodePicFlags =
2681  ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2682  } else {
2683  pic_params.encodePicFlags = 0;
2684  }
2685 
2686  pic_params.inputTimeStamp = frame->pts;
2687 
2688  if (ctx->extra_sei) {
2689  res = prepare_sei_data_array(avctx, frame);
2690  if (res < 0)
2691  return res;
2692  sei_count = res;
2693  }
2694 
2695  res = nvenc_store_frame_data(avctx, &pic_params, frame);
2696  if (res < 0)
2697  return res;
2698 
2699  nvenc_codec_specific_pic_params(avctx, &pic_params, ctx->sei_data, sei_count);
2700  } else {
2701  pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2702  }
2703 
2704  res = nvenc_push_context(avctx);
2705  if (res < 0)
2706  return res;
2707 
2708  nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
2709 
2710  for (i = 0; i < sei_count; i++)
2711  av_freep(&(ctx->sei_data[i].payload));
2712 
2713  res = nvenc_pop_context(avctx);
2714  if (res < 0)
2715  return res;
2716 
2717  if (nv_status != NV_ENC_SUCCESS &&
2718  nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2719  return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
2720 
2721  if (frame && frame->buf[0]) {
2722  av_fifo_write(ctx->output_surface_queue, &in_surf, 1);
2723 
2725  timestamp_queue_enqueue(ctx->timestamp_list, frame->pts);
2726  }
2727 
2728  /* all the pending buffers are now ready for output */
2729  if (nv_status == NV_ENC_SUCCESS) {
2730  while (av_fifo_read(ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2731  av_fifo_write(ctx->output_surface_ready_queue, &tmp_out_surf, 1);
2732  }
2733 
2734  return 0;
2735 }
2736 
2738 {
2739  NvencSurface *tmp_out_surf;
2740  int res, res2;
2741 
2742  NvencContext *ctx = avctx->priv_data;
2743 
2744  AVFrame *frame = ctx->frame;
2745 
2746  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
2747  return AVERROR(EINVAL);
2748 
2749  if (!frame->buf[0]) {
2750  res = ff_encode_get_frame(avctx, frame);
2751  if (res < 0 && res != AVERROR_EOF)
2752  return res;
2753  }
2754 
2755  res = nvenc_send_frame(avctx, frame);
2756  if (res < 0) {
2757  if (res != AVERROR(EAGAIN))
2758  return res;
2759  } else
2761 
2762  if (output_ready(avctx, avctx->internal->draining)) {
2763  av_fifo_read(ctx->output_surface_ready_queue, &tmp_out_surf, 1);
2764 
2765  res = nvenc_push_context(avctx);
2766  if (res < 0)
2767  return res;
2768 
2769  res = process_output_surface(avctx, pkt, tmp_out_surf);
2770 
2771  res2 = nvenc_pop_context(avctx);
2772  if (res2 < 0)
2773  return res2;
2774 
2775  if (res)
2776  return res;
2777 
2778  av_fifo_write(ctx->unused_surface_queue, &tmp_out_surf, 1);
2779  } else if (avctx->internal->draining) {
2780  return AVERROR_EOF;
2781  } else {
2782  return AVERROR(EAGAIN);
2783  }
2784 
2785  return 0;
2786 }
2787 
2789 {
2790  NvencContext *ctx = avctx->priv_data;
2791 
2792  nvenc_send_frame(avctx, NULL);
2793  av_fifo_reset2(ctx->timestamp_list);
2794 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
nvenc_errors
static const struct @120 nvenc_errors[]
AVHWDeviceContext::hwctx
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:92
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
ff_alloc_a53_sei
int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len, void **data, size_t *sei_size)
Check AVFrame for A53 side data and allocate and fill SEI message with A53 info.
Definition: atsc_a53.c: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:96
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:1025
P3
#define P3
Definition: hevcdsp_template.c:1497
NV_ENC_HEVC_PROFILE_REXT
@ NV_ENC_HEVC_PROFILE_REXT
Definition: nvenc.h:154
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:824
AV1_METADATA_TYPE_ITUT_T35
@ AV1_METADATA_TYPE_ITUT_T35
Definition: av1.h:47
AV_PIX_FMT_BGR32
#define AV_PIX_FMT_BGR32
Definition: pixfmt.h:443
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:2964
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:603
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AV_FRAME_DATA_A53_CC
@ AV_FRAME_DATA_A53_CC
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:59
AV_PROFILE_H264_MAIN
#define AV_PROFILE_H264_MAIN
Definition: defs.h:111
nvenc_push_context
static int nvenc_push_context(AVCodecContext *avctx)
Definition: nvenc.c:340
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:209
AVPictureType
AVPictureType
Definition: avutil.h:277
output_ready
static int output_ready(AVCodecContext *avctx, int flush)
Definition: nvenc.c:2409
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
AV_PROFILE_HEVC_MAIN
#define AV_PROFILE_HEVC_MAIN
Definition: defs.h:158
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:2251
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
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:1018
nvenc_set_timestamp
static int nvenc_set_timestamp(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *params, AVPacket *pkt)
Definition: nvenc.c:2228
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:673
P1
#define P1
Definition: cavsdsp.c:37
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
encode.h
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:732
NvencFrameData
Definition: nvenc.h:99
reconfig_encoder
static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2533
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:600
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
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
AVCodecContext::qmax
int qmax
maximum quantizer
Definition: avcodec.h:1251
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:2788
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:330
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:641
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:1479
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1797
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:605
nvenc_upload_frame
static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame, NvencSurface *nvenc_frame)
Definition: nvenc.c:2106
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:291
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:749
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:446
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:286
fail
#define fail()
Definition: checkasm.h:138
av_fifo_write
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition: fifo.c:188
dummy
int dummy
Definition: motion.c:66
NvencSurface::format
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.h:96
AVCodecContext::refs
int refs
number of reference frames
Definition: avcodec.h:997
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:517
val
static double val(void *priv, double ch)
Definition: aeval.c:78
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:1988
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:322
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
NV_ENC_H264_PROFILE_HIGH
@ NV_ENC_H264_PROFILE_HIGH
Definition: nvenc.h:147
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:1011
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:249
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:539
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:740
ff_nvenc_encode_init
av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:1930
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:503
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:481
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1223
AV_PIX_FMT_0BGR32
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:446
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:1811
timestamp_queue_enqueue
static void timestamp_queue_enqueue(AVFifo *queue, int64_t timestamp)
Definition: nvenc.c:2214
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:1280
LIST_DEVICES
@ LIST_DEVICES
Definition: nvenc.h:167
timestamp_queue_dequeue
static int64_t timestamp_queue_dequeue(AVFifo *queue)
Definition: nvenc.c:2219
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:261
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:1265
NVENC_CAP
#define NVENC_CAP
Definition: nvenc.c:45
AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:487
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:1035
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
NV_ENC_H264_PROFILE_HIGH_444P
@ NV_ENC_H264_PROFILE_HIGH_444P
Definition: nvenc.h:148
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:472
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:487
ff_nvenc_encode_close
av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:1845
FrameData::duration
int64_t duration
Definition: librav1e.c:59
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
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_PROFILE_HEVC_MAIN_10
#define AV_PROFILE_HEVC_MAIN_10
Definition: defs.h:159
AV_PROFILE_HEVC_REXT
#define AV_PROFILE_HEVC_REXT
Definition: defs.h:161
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:559
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:527
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
HW_CONFIG_ENCODER_DEVICE
#define HW_CONFIG_ENCODER_DEVICE(format, device_type_)
Definition: hwconfig.h:93
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:639
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
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:1030
nvenc_alloc_surface
static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
Definition: nvenc.c:1710
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFrameSideData::data
uint8_t * data
Definition: frame.h:248
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:2052
AVCodecHWConfigInternal
Definition: hwconfig.h:25
frame_data
FrameData * frame_data(AVFrame *frame)
Get our axiliary frame data attached to the frame, allocating it if needed.
Definition: ffmpeg.c:429
NVENC_ONE_PASS
@ NVENC_ONE_PASS
Definition: nvenc.h:160
ff_nvenc_receive_packet
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: nvenc.c:2737
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
height
#define height
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:441
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
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:276
get_free_frame
static NvencSurface * get_free_frame(NvencContext *ctx)
Definition: nvenc.c:1977
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:725
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:96
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:244
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:32
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:538
process_output_surface
static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)
Definition: nvenc.c:2310
NVENC_LOWLATENCY
@ NVENC_LOWLATENCY
Definition: nvenc.h:158
NVENC_LOSSLESS
@ NVENC_LOSSLESS
Definition: nvenc.h:159
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:266
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:583
prepare_sei_data_array
static int prepare_sei_data_array(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2421
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:526
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:1975
NV_ENC_HEVC_PROFILE_MAIN_10
@ NV_ENC_HEVC_PROFILE_MAIN_10
Definition: nvenc.h:153
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:617
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:654
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:1679
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:520
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:1934
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
AVCUDADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_cuda.h:42
AV_PROFILE_H264_HIGH_444_PREDICTIVE
#define AV_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: defs.h:121
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:1512
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:445
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:282
cuda_check.h
atsc_a53.h
AV_PROFILE_H264_BASELINE
#define AV_PROFILE_H264_BASELINE
Definition: defs.h:109
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:2168
AVCodecContext
main external API structure.
Definition: avcodec.h:437
AVCodecContext::codec_descriptor
const AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
Definition: avcodec.h:1818
AV_PROFILE_H264_HIGH
#define AV_PROFILE_H264_HIGH
Definition: defs.h:113
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:281
NVENC_TWO_PASSES
@ NVENC_TWO_PASSES
Definition: nvenc.h:161
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:104
NvencSurface::height
int height
Definition: nvenc.h:92
SEI_TYPE_USER_DATA_UNREGISTERED
@ SEI_TYPE_USER_DATA_UNREGISTERED
Definition: sei.h:35
av_image_copy2
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition: imgutils.h:184
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1244
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:1592
nvenc_setup_surfaces
static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx)
Definition: nvenc.c:1766
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:756
AVFrameSideData::type
enum AVFrameSideDataType type
Definition: frame.h:247
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:575
nvenc_find_free_reg_resource
static int nvenc_find_free_reg_resource(AVCodecContext *avctx)
Definition: nvenc.c:2018
NVENC_DEPRECATED_PRESET
@ NVENC_DEPRECATED_PRESET
Definition: nvenc.h:163
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
NV_ENC_H264_PROFILE_MAIN
@ NV_ENC_H264_PROFILE_MAIN
Definition: nvenc.h:146
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:518
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:72
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
NvencDynLoadFunctions::nvenc_funcs
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.h:116
NV_ENC_H264_PROFILE_BASELINE
@ NV_ENC_H264_PROFILE_BASELINE
Definition: nvenc.h:145
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
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:716
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:208
packet_internal.h
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:246
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:1051
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:464
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AV_PICTURE_TYPE_BI
@ AV_PICTURE_TYPE_BI
BI type.
Definition: avutil.h:285
nvenc_setup_device
static av_cold int nvenc_setup_device(AVCodecContext *avctx)
Definition: nvenc.c:668
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:617
AV1_METADATA_TYPE_TIMECODE
@ AV1_METADATA_TYPE_TIMECODE
Definition: av1.h:48
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:467
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
ff_encode_add_cpb_side_data
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition: encode.c:869
nvenc_setup_codec_config
static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx)
Definition: nvenc.c:1462
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
AV_PROFILE_AV1_MAIN
#define AV_PROFILE_AV1_MAIN
Definition: defs.h:167
NV_ENC_HEVC_PROFILE_MAIN
@ NV_ENC_HEVC_PROFILE_MAIN
Definition: nvenc.h:152
int
int
Definition: ffmpeg_filter.c:331
hevc_sei.h
ANY_DEVICE
@ ANY_DEVICE
Definition: nvenc.h:168
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:818
nvenc_setup_hevc_config
static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
Definition: nvenc.c:1265
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:2625
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:2884
nvenc_retrieve_frame_data
static int nvenc_retrieve_frame_data(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *lock_params, AVPacket *pkt)
Definition: nvenc.c:2283