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