FFmpeg
nvenc.c
Go to the documentation of this file.
1 /*
2  * H.264/HEVC 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 
24 #include "nvenc.h"
25 #include "hevc_sei.h"
26 
28 #include "libavutil/hwcontext.h"
29 #include "libavutil/cuda_check.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/pixdesc.h"
34 #include "atsc_a53.h"
35 #include "encode.h"
36 #include "internal.h"
37 #include "packet_internal.h"
38 
39 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
40 
41 #define NVENC_CAP 0x30
42 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
43  rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
44  rc == NV_ENC_PARAMS_RC_CBR_HQ)
45 
51  AV_PIX_FMT_P016, // Truncated to 10bits
52  AV_PIX_FMT_YUV444P16, // Truncated to 10bits
56 #if CONFIG_D3D11VA
58 #endif
60 };
61 
63  HW_CONFIG_ENCODER_FRAMES(CUDA, CUDA),
65 #if CONFIG_D3D11VA
66  HW_CONFIG_ENCODER_FRAMES(D3D11, D3D11VA),
68 #endif
69  NULL,
70 };
71 
72 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
73  pix_fmt == AV_PIX_FMT_P016 || \
74  pix_fmt == AV_PIX_FMT_YUV444P16)
75 
76 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
77  pix_fmt == AV_PIX_FMT_YUV444P16)
78 
79 static const struct {
80  NVENCSTATUS nverr;
81  int averr;
82  const char *desc;
83 } nvenc_errors[] = {
84  { NV_ENC_SUCCESS, 0, "success" },
85  { NV_ENC_ERR_NO_ENCODE_DEVICE, AVERROR(ENOENT), "no encode device" },
86  { NV_ENC_ERR_UNSUPPORTED_DEVICE, AVERROR(ENOSYS), "unsupported device" },
87  { NV_ENC_ERR_INVALID_ENCODERDEVICE, AVERROR(EINVAL), "invalid encoder device" },
88  { NV_ENC_ERR_INVALID_DEVICE, AVERROR(EINVAL), "invalid device" },
89  { NV_ENC_ERR_DEVICE_NOT_EXIST, AVERROR(EIO), "device does not exist" },
90  { NV_ENC_ERR_INVALID_PTR, AVERROR(EFAULT), "invalid ptr" },
91  { NV_ENC_ERR_INVALID_EVENT, AVERROR(EINVAL), "invalid event" },
92  { NV_ENC_ERR_INVALID_PARAM, AVERROR(EINVAL), "invalid param" },
93  { NV_ENC_ERR_INVALID_CALL, AVERROR(EINVAL), "invalid call" },
94  { NV_ENC_ERR_OUT_OF_MEMORY, AVERROR(ENOMEM), "out of memory" },
95  { NV_ENC_ERR_ENCODER_NOT_INITIALIZED, AVERROR(EINVAL), "encoder not initialized" },
96  { NV_ENC_ERR_UNSUPPORTED_PARAM, AVERROR(ENOSYS), "unsupported param" },
97  { NV_ENC_ERR_LOCK_BUSY, AVERROR(EAGAIN), "lock busy" },
98  { NV_ENC_ERR_NOT_ENOUGH_BUFFER, AVERROR_BUFFER_TOO_SMALL, "not enough buffer"},
99  { NV_ENC_ERR_INVALID_VERSION, AVERROR(EINVAL), "invalid version" },
100  { NV_ENC_ERR_MAP_FAILED, AVERROR(EIO), "map failed" },
101  { NV_ENC_ERR_NEED_MORE_INPUT, AVERROR(EAGAIN), "need more input" },
102  { NV_ENC_ERR_ENCODER_BUSY, AVERROR(EAGAIN), "encoder busy" },
103  { NV_ENC_ERR_EVENT_NOT_REGISTERD, AVERROR(EBADF), "event not registered" },
104  { NV_ENC_ERR_GENERIC, AVERROR_UNKNOWN, "generic error" },
105  { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, AVERROR(EINVAL), "incompatible client key" },
106  { NV_ENC_ERR_UNIMPLEMENTED, AVERROR(ENOSYS), "unimplemented" },
107  { NV_ENC_ERR_RESOURCE_REGISTER_FAILED, AVERROR(EIO), "resource register failed" },
108  { NV_ENC_ERR_RESOURCE_NOT_REGISTERED, AVERROR(EBADF), "resource not registered" },
109  { NV_ENC_ERR_RESOURCE_NOT_MAPPED, AVERROR(EBADF), "resource not mapped" },
110 };
111 
112 static int nvenc_map_error(NVENCSTATUS err, const char **desc)
113 {
114  int i;
115  for (i = 0; i < FF_ARRAY_ELEMS(nvenc_errors); i++) {
116  if (nvenc_errors[i].nverr == err) {
117  if (desc)
118  *desc = nvenc_errors[i].desc;
119  return nvenc_errors[i].averr;
120  }
121  }
122  if (desc)
123  *desc = "unknown error";
124  return AVERROR_UNKNOWN;
125 }
126 
127 static int nvenc_print_error(AVCodecContext *avctx, NVENCSTATUS err,
128  const char *error_string)
129 {
130  const char *desc;
131  const char *details = "(no details)";
132  int ret = nvenc_map_error(err, &desc);
133 
134 #ifdef NVENC_HAVE_GETLASTERRORSTRING
135  NvencContext *ctx = avctx->priv_data;
136  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
137 
138  if (p_nvenc && ctx->nvencoder)
139  details = p_nvenc->nvEncGetLastErrorString(ctx->nvencoder);
140 #endif
141 
142  av_log(avctx, AV_LOG_ERROR, "%s: %s (%d): %s\n", error_string, desc, err, details);
143 
144  return ret;
145 }
146 
148 {
149 #if NVENCAPI_CHECK_VERSION(11, 1)
150  const char *minver = "(unknown)";
151 #elif NVENCAPI_CHECK_VERSION(11, 0)
152 # if defined(_WIN32) || defined(__CYGWIN__)
153  const char *minver = "456.71";
154 # else
155  const char *minver = "455.28";
156 # endif
157 #elif NVENCAPI_CHECK_VERSION(10, 0)
158 # if defined(_WIN32) || defined(__CYGWIN__)
159  const char *minver = "450.51";
160 # else
161  const char *minver = "445.87";
162 # endif
163 #elif NVENCAPI_CHECK_VERSION(9, 1)
164 # if defined(_WIN32) || defined(__CYGWIN__)
165  const char *minver = "436.15";
166 # else
167  const char *minver = "435.21";
168 # endif
169 #elif NVENCAPI_CHECK_VERSION(9, 0)
170 # if defined(_WIN32) || defined(__CYGWIN__)
171  const char *minver = "418.81";
172 # else
173  const char *minver = "418.30";
174 # endif
175 #elif NVENCAPI_CHECK_VERSION(8, 2)
176 # if defined(_WIN32) || defined(__CYGWIN__)
177  const char *minver = "397.93";
178 # else
179  const char *minver = "396.24";
180 #endif
181 #elif NVENCAPI_CHECK_VERSION(8, 1)
182 # if defined(_WIN32) || defined(__CYGWIN__)
183  const char *minver = "390.77";
184 # else
185  const char *minver = "390.25";
186 # endif
187 #else
188 # if defined(_WIN32) || defined(__CYGWIN__)
189  const char *minver = "378.66";
190 # else
191  const char *minver = "378.13";
192 # endif
193 #endif
194  av_log(avctx, level, "The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
195 }
196 
198 {
199  NvencContext *ctx = avctx->priv_data;
201  NVENCSTATUS err;
202  uint32_t nvenc_max_ver;
203  int ret;
204 
205  ret = cuda_load_functions(&dl_fn->cuda_dl, avctx);
206  if (ret < 0)
207  return ret;
208 
209  ret = nvenc_load_functions(&dl_fn->nvenc_dl, avctx);
210  if (ret < 0) {
212  return ret;
213  }
214 
215  err = dl_fn->nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
216  if (err != NV_ENC_SUCCESS)
217  return nvenc_print_error(avctx, err, "Failed to query nvenc max version");
218 
219  av_log(avctx, AV_LOG_VERBOSE, "Loaded Nvenc version %d.%d\n", nvenc_max_ver >> 4, nvenc_max_ver & 0xf);
220 
221  if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
222  av_log(avctx, AV_LOG_ERROR, "Driver does not support the required nvenc API version. "
223  "Required: %d.%d Found: %d.%d\n",
224  NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
225  nvenc_max_ver >> 4, nvenc_max_ver & 0xf);
227  return AVERROR(ENOSYS);
228  }
229 
230  dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
231 
232  err = dl_fn->nvenc_dl->NvEncodeAPICreateInstance(&dl_fn->nvenc_funcs);
233  if (err != NV_ENC_SUCCESS)
234  return nvenc_print_error(avctx, err, "Failed to create nvenc instance");
235 
236  av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
237 
238  return 0;
239 }
240 
242 {
243  NvencContext *ctx = avctx->priv_data;
245 
246  if (ctx->d3d11_device)
247  return 0;
248 
249  return CHECK_CU(dl_fn->cuda_dl->cuCtxPushCurrent(ctx->cu_context));
250 }
251 
253 {
254  NvencContext *ctx = avctx->priv_data;
256  CUcontext dummy;
257 
258  if (ctx->d3d11_device)
259  return 0;
260 
261  return CHECK_CU(dl_fn->cuda_dl->cuCtxPopCurrent(&dummy));
262 }
263 
265 {
266  NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
267  NvencContext *ctx = avctx->priv_data;
268  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
269  NVENCSTATUS ret;
270 
271  params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
272  params.apiVersion = NVENCAPI_VERSION;
273  if (ctx->d3d11_device) {
274  params.device = ctx->d3d11_device;
275  params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
276  } else {
277  params.device = ctx->cu_context;
278  params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
279  }
280 
281  ret = p_nvenc->nvEncOpenEncodeSessionEx(&params, &ctx->nvencoder);
282  if (ret != NV_ENC_SUCCESS) {
283  ctx->nvencoder = NULL;
284  return nvenc_print_error(avctx, ret, "OpenEncodeSessionEx failed");
285  }
286 
287  return 0;
288 }
289 
291 {
292  NvencContext *ctx = avctx->priv_data;
293  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
294  int i, ret, count = 0;
295  GUID *guids = NULL;
296 
297  ret = p_nvenc->nvEncGetEncodeGUIDCount(ctx->nvencoder, &count);
298 
299  if (ret != NV_ENC_SUCCESS || !count)
300  return AVERROR(ENOSYS);
301 
302  guids = av_malloc(count * sizeof(GUID));
303  if (!guids)
304  return AVERROR(ENOMEM);
305 
306  ret = p_nvenc->nvEncGetEncodeGUIDs(ctx->nvencoder, guids, count, &count);
307  if (ret != NV_ENC_SUCCESS) {
308  ret = AVERROR(ENOSYS);
309  goto fail;
310  }
311 
312  ret = AVERROR(ENOSYS);
313  for (i = 0; i < count; i++) {
314  if (!memcmp(&guids[i], &ctx->init_encode_params.encodeGUID, sizeof(*guids))) {
315  ret = 0;
316  break;
317  }
318  }
319 
320 fail:
321  av_free(guids);
322 
323  return ret;
324 }
325 
326 static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
327 {
328  NvencContext *ctx = avctx->priv_data;
329  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
330  NV_ENC_CAPS_PARAM params = { 0 };
331  int ret, val = 0;
332 
333  params.version = NV_ENC_CAPS_PARAM_VER;
334  params.capsToQuery = cap;
335 
336  ret = p_nvenc->nvEncGetEncodeCaps(ctx->nvencoder, ctx->init_encode_params.encodeGUID, &params, &val);
337 
338  if (ret == NV_ENC_SUCCESS)
339  return val;
340  return 0;
341 }
342 
344 {
345  NvencContext *ctx = avctx->priv_data;
346  int ret;
347 
348  ret = nvenc_check_codec_support(avctx);
349  if (ret < 0) {
350  av_log(avctx, AV_LOG_WARNING, "Codec not supported\n");
351  return ret;
352  }
353 
354  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_YUV444_ENCODE);
355  if (IS_YUV444(ctx->data_pix_fmt) && ret <= 0) {
356  av_log(avctx, AV_LOG_WARNING, "YUV444P not supported\n");
357  return AVERROR(ENOSYS);
358  }
359 
360  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE);
361  if (ctx->preset >= PRESET_LOSSLESS_DEFAULT && ret <= 0) {
362  av_log(avctx, AV_LOG_WARNING, "Lossless encoding not supported\n");
363  return AVERROR(ENOSYS);
364  }
365 
366  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_WIDTH_MAX);
367  if (ret < avctx->width) {
368  av_log(avctx, AV_LOG_WARNING, "Width %d exceeds %d\n",
369  avctx->width, ret);
370  return AVERROR(ENOSYS);
371  }
372 
373  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_HEIGHT_MAX);
374  if (ret < avctx->height) {
375  av_log(avctx, AV_LOG_WARNING, "Height %d exceeds %d\n",
376  avctx->height, ret);
377  return AVERROR(ENOSYS);
378  }
379 
380  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_NUM_MAX_BFRAMES);
381  if (ret < avctx->max_b_frames) {
382  av_log(avctx, AV_LOG_WARNING, "Max B-frames %d exceed %d\n",
383  avctx->max_b_frames, ret);
384 
385  return AVERROR(ENOSYS);
386  }
387 
388  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_FIELD_ENCODING);
389  if (ret < 1 && avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
390  av_log(avctx, AV_LOG_WARNING,
391  "Interlaced encoding is not supported. Supported level: %d\n",
392  ret);
393  return AVERROR(ENOSYS);
394  }
395 
396  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_10BIT_ENCODE);
397  if (IS_10BIT(ctx->data_pix_fmt) && ret <= 0) {
398  av_log(avctx, AV_LOG_WARNING, "10 bit encode not supported\n");
399  return AVERROR(ENOSYS);
400  }
401 
402  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOOKAHEAD);
403  if (ctx->rc_lookahead > 0 && ret <= 0) {
404  av_log(avctx, AV_LOG_WARNING, "RC lookahead not supported\n");
405  return AVERROR(ENOSYS);
406  }
407 
408  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ);
409  if (ctx->temporal_aq > 0 && ret <= 0) {
410  av_log(avctx, AV_LOG_WARNING, "Temporal AQ not supported\n");
411  return AVERROR(ENOSYS);
412  }
413 
414  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION);
415  if (ctx->weighted_pred > 0 && ret <= 0) {
416  av_log (avctx, AV_LOG_WARNING, "Weighted Prediction not supported\n");
417  return AVERROR(ENOSYS);
418  }
419 
420  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_CABAC);
421  if (ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC && ret <= 0) {
422  av_log(avctx, AV_LOG_WARNING, "CABAC entropy coding not supported\n");
423  return AVERROR(ENOSYS);
424  }
425 
426 #ifdef NVENC_HAVE_BFRAME_REF_MODE
427  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE);
428  if (ctx->b_ref_mode == NV_ENC_BFRAME_REF_MODE_EACH && ret != 1 && ret != 3) {
429  av_log(avctx, AV_LOG_WARNING, "Each B frame as reference is not supported\n");
430  return AVERROR(ENOSYS);
431  } else if (ctx->b_ref_mode != NV_ENC_BFRAME_REF_MODE_DISABLED && ret == 0) {
432  av_log(avctx, AV_LOG_WARNING, "B frames as references are not supported\n");
433  return AVERROR(ENOSYS);
434  }
435 #else
436  if (ctx->b_ref_mode != 0) {
437  av_log(avctx, AV_LOG_WARNING, "B frames as references need SDK 8.1 at build time\n");
438  return AVERROR(ENOSYS);
439  }
440 #endif
441 
442 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
443  ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES);
444  if(avctx->refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT && ret <= 0) {
445  av_log(avctx, AV_LOG_WARNING, "Multiple reference frames are not supported by the device\n");
446  return AVERROR(ENOSYS);
447  }
448 #else
449  if(avctx->refs != 0) {
450  av_log(avctx, AV_LOG_WARNING, "Multiple reference frames need SDK 9.1 at build time\n");
451  return AVERROR(ENOSYS);
452  }
453 #endif
454 
455  ctx->support_dyn_bitrate = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
456 
457  return 0;
458 }
459 
460 static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
461 {
462  NvencContext *ctx = avctx->priv_data;
464  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
465  char name[128] = { 0};
466  int major, minor, ret;
467  CUdevice cu_device;
468  int loglevel = AV_LOG_VERBOSE;
469 
470  if (ctx->device == LIST_DEVICES)
471  loglevel = AV_LOG_INFO;
472 
473  ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceGet(&cu_device, idx));
474  if (ret < 0)
475  return ret;
476 
477  ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceGetName(name, sizeof(name), cu_device));
478  if (ret < 0)
479  return ret;
480 
481  ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceComputeCapability(&major, &minor, cu_device));
482  if (ret < 0)
483  return ret;
484 
485  av_log(avctx, loglevel, "[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx, name, major, minor);
486  if (((major << 4) | minor) < NVENC_CAP) {
487  av_log(avctx, loglevel, "does not support NVENC\n");
488  goto fail;
489  }
490 
491  if (ctx->device != idx && ctx->device != ANY_DEVICE)
492  return -1;
493 
494  ret = CHECK_CU(dl_fn->cuda_dl->cuCtxCreate(&ctx->cu_context_internal, 0, cu_device));
495  if (ret < 0)
496  goto fail;
497 
498  ctx->cu_context = ctx->cu_context_internal;
499  ctx->cu_stream = NULL;
500 
501  if ((ret = nvenc_pop_context(avctx)) < 0)
502  goto fail2;
503 
504  if ((ret = nvenc_open_session(avctx)) < 0)
505  goto fail2;
506 
507  if ((ret = nvenc_check_capabilities(avctx)) < 0)
508  goto fail3;
509 
510  av_log(avctx, loglevel, "supports NVENC\n");
511 
512  dl_fn->nvenc_device_count++;
513 
514  if (ctx->device == idx || ctx->device == ANY_DEVICE)
515  return 0;
516 
517 fail3:
518  if ((ret = nvenc_push_context(avctx)) < 0)
519  return ret;
520 
521  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
522  ctx->nvencoder = NULL;
523 
524  if ((ret = nvenc_pop_context(avctx)) < 0)
525  return ret;
526 
527 fail2:
528  CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal));
529  ctx->cu_context_internal = NULL;
530 
531 fail:
532  return AVERROR(ENOSYS);
533 }
534 
536 {
537  NvencContext *ctx = avctx->priv_data;
539 
540  switch (avctx->codec->id) {
541  case AV_CODEC_ID_H264:
542  ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
543  break;
544  case AV_CODEC_ID_HEVC:
545  ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
546  break;
547  default:
548  return AVERROR_BUG;
549  }
550 
551  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11 || avctx->hw_frames_ctx || avctx->hw_device_ctx) {
552  AVHWFramesContext *frames_ctx;
553  AVHWDeviceContext *hwdev_ctx;
554  AVCUDADeviceContext *cuda_device_hwctx = NULL;
555 #if CONFIG_D3D11VA
556  AVD3D11VADeviceContext *d3d11_device_hwctx = NULL;
557 #endif
558  int ret;
559 
560  if (avctx->hw_frames_ctx) {
561  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
562  if (frames_ctx->format == AV_PIX_FMT_CUDA)
563  cuda_device_hwctx = frames_ctx->device_ctx->hwctx;
564 #if CONFIG_D3D11VA
565  else if (frames_ctx->format == AV_PIX_FMT_D3D11)
566  d3d11_device_hwctx = frames_ctx->device_ctx->hwctx;
567 #endif
568  else
569  return AVERROR(EINVAL);
570  } else if (avctx->hw_device_ctx) {
571  hwdev_ctx = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
572  if (hwdev_ctx->type == AV_HWDEVICE_TYPE_CUDA)
573  cuda_device_hwctx = hwdev_ctx->hwctx;
574 #if CONFIG_D3D11VA
575  else if (hwdev_ctx->type == AV_HWDEVICE_TYPE_D3D11VA)
576  d3d11_device_hwctx = hwdev_ctx->hwctx;
577 #endif
578  else
579  return AVERROR(EINVAL);
580  } else {
581  return AVERROR(EINVAL);
582  }
583 
584  if (cuda_device_hwctx) {
585  ctx->cu_context = cuda_device_hwctx->cuda_ctx;
586  ctx->cu_stream = cuda_device_hwctx->stream;
587  }
588 #if CONFIG_D3D11VA
589  else if (d3d11_device_hwctx) {
590  ctx->d3d11_device = d3d11_device_hwctx->device;
591  ID3D11Device_AddRef(ctx->d3d11_device);
592  }
593 #endif
594 
595  ret = nvenc_open_session(avctx);
596  if (ret < 0)
597  return ret;
598 
599  ret = nvenc_check_capabilities(avctx);
600  if (ret < 0) {
601  av_log(avctx, AV_LOG_FATAL, "Provided device doesn't support required NVENC features\n");
602  return ret;
603  }
604  } else {
605  int i, nb_devices = 0;
606 
607  if (CHECK_CU(dl_fn->cuda_dl->cuInit(0)) < 0)
608  return AVERROR_UNKNOWN;
609 
610  if (CHECK_CU(dl_fn->cuda_dl->cuDeviceGetCount(&nb_devices)) < 0)
611  return AVERROR_UNKNOWN;
612 
613  if (!nb_devices) {
614  av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
615  return AVERROR_EXTERNAL;
616  }
617 
618  av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", nb_devices);
619 
620  dl_fn->nvenc_device_count = 0;
621  for (i = 0; i < nb_devices; ++i) {
622  if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES)
623  return 0;
624  }
625 
626  if (ctx->device == LIST_DEVICES)
627  return AVERROR_EXIT;
628 
629  if (!dl_fn->nvenc_device_count) {
630  av_log(avctx, AV_LOG_FATAL, "No capable devices found\n");
631  return AVERROR_EXTERNAL;
632  }
633 
634  av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->device, nb_devices);
635  return AVERROR(EINVAL);
636  }
637 
638  return 0;
639 }
640 
641 typedef struct GUIDTuple {
642  const GUID guid;
643  int flags;
644 } GUIDTuple;
645 
646 #define PRESET_ALIAS(alias, name, ...) \
647  [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
648 
649 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
650 
652 {
653  GUIDTuple presets[] = {
654 #ifdef NVENC_HAVE_NEW_PRESETS
655  PRESET(P1),
656  PRESET(P2),
657  PRESET(P3),
658  PRESET(P4),
659  PRESET(P5),
660  PRESET(P6),
661  PRESET(P7),
662  PRESET_ALIAS(SLOW, P7, NVENC_TWO_PASSES),
663  PRESET_ALIAS(MEDIUM, P4, NVENC_ONE_PASS),
665  // Compat aliases
670  PRESET_ALIAS(LOW_LATENCY_DEFAULT, P4, NVENC_DEPRECATED_PRESET | NVENC_LOWLATENCY),
673  PRESET_ALIAS(LOSSLESS_DEFAULT, P4, NVENC_DEPRECATED_PRESET | NVENC_LOSSLESS),
675 #else
676  PRESET(DEFAULT),
677  PRESET(HP),
678  PRESET(HQ),
679  PRESET(BD),
680  PRESET_ALIAS(SLOW, HQ, NVENC_TWO_PASSES),
681  PRESET_ALIAS(MEDIUM, HQ, NVENC_ONE_PASS),
682  PRESET_ALIAS(FAST, HP, NVENC_ONE_PASS),
683  PRESET(LOW_LATENCY_DEFAULT, NVENC_LOWLATENCY),
684  PRESET(LOW_LATENCY_HP, NVENC_LOWLATENCY),
685  PRESET(LOW_LATENCY_HQ, NVENC_LOWLATENCY),
686  PRESET(LOSSLESS_DEFAULT, NVENC_LOSSLESS),
687  PRESET(LOSSLESS_HP, NVENC_LOSSLESS),
688 #endif
689  };
690 
691  GUIDTuple *t = &presets[ctx->preset];
692 
693  ctx->init_encode_params.presetGUID = t->guid;
694  ctx->flags = t->flags;
695 }
696 
697 #undef PRESET
698 #undef PRESET_ALIAS
699 
700 static av_cold void set_constqp(AVCodecContext *avctx)
701 {
702  NvencContext *ctx = avctx->priv_data;
703  NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
704 
705  rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
706 
707  if (ctx->init_qp_p >= 0) {
708  rc->constQP.qpInterP = ctx->init_qp_p;
709  if (ctx->init_qp_i >= 0 && ctx->init_qp_b >= 0) {
710  rc->constQP.qpIntra = ctx->init_qp_i;
711  rc->constQP.qpInterB = ctx->init_qp_b;
712  } else if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
713  rc->constQP.qpIntra = av_clip(
714  rc->constQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51);
715  rc->constQP.qpInterB = av_clip(
716  rc->constQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51);
717  } else {
718  rc->constQP.qpIntra = rc->constQP.qpInterP;
719  rc->constQP.qpInterB = rc->constQP.qpInterP;
720  }
721  } else if (ctx->cqp >= 0) {
722  rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra = ctx->cqp;
723  if (avctx->b_quant_factor != 0.0)
724  rc->constQP.qpInterB = av_clip(ctx->cqp * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51);
725  if (avctx->i_quant_factor != 0.0)
726  rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51);
727  }
728 
729  avctx->qmin = -1;
730  avctx->qmax = -1;
731 }
732 
733 static av_cold void set_vbr(AVCodecContext *avctx)
734 {
735  NvencContext *ctx = avctx->priv_data;
736  NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
737  int qp_inter_p;
738 
739  if (avctx->qmin >= 0 && avctx->qmax >= 0) {
740  rc->enableMinQP = 1;
741  rc->enableMaxQP = 1;
742 
743  rc->minQP.qpInterB = avctx->qmin;
744  rc->minQP.qpInterP = avctx->qmin;
745  rc->minQP.qpIntra = avctx->qmin;
746 
747  rc->maxQP.qpInterB = avctx->qmax;
748  rc->maxQP.qpInterP = avctx->qmax;
749  rc->maxQP.qpIntra = avctx->qmax;
750 
751  qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin
752  } else if (avctx->qmin >= 0) {
753  rc->enableMinQP = 1;
754 
755  rc->minQP.qpInterB = avctx->qmin;
756  rc->minQP.qpInterP = avctx->qmin;
757  rc->minQP.qpIntra = avctx->qmin;
758 
759  qp_inter_p = avctx->qmin;
760  } else {
761  qp_inter_p = 26; // default to 26
762  }
763 
764  rc->enableInitialRCQP = 1;
765 
766  if (ctx->init_qp_p < 0) {
767  rc->initialRCQP.qpInterP = qp_inter_p;
768  } else {
769  rc->initialRCQP.qpInterP = ctx->init_qp_p;
770  }
771 
772  if (ctx->init_qp_i < 0) {
773  if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
774  rc->initialRCQP.qpIntra = av_clip(
775  rc->initialRCQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51);
776  } else {
777  rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
778  }
779  } else {
780  rc->initialRCQP.qpIntra = ctx->init_qp_i;
781  }
782 
783  if (ctx->init_qp_b < 0) {
784  if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
785  rc->initialRCQP.qpInterB = av_clip(
786  rc->initialRCQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51);
787  } else {
788  rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
789  }
790  } else {
791  rc->initialRCQP.qpInterB = ctx->init_qp_b;
792  }
793 }
794 
796 {
797  NvencContext *ctx = avctx->priv_data;
798  NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
799 
800  rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
801  rc->constQP.qpInterB = 0;
802  rc->constQP.qpInterP = 0;
803  rc->constQP.qpIntra = 0;
804 
805  avctx->qmin = -1;
806  avctx->qmax = -1;
807 }
808 
810 {
811  NvencContext *ctx = avctx->priv_data;
812  NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
813 
814  switch (ctx->rc) {
815  case NV_ENC_PARAMS_RC_CONSTQP:
816  set_constqp(avctx);
817  return;
818  case NV_ENC_PARAMS_RC_VBR_MINQP:
819  if (avctx->qmin < 0) {
820  av_log(avctx, AV_LOG_WARNING,
821  "The variable bitrate rate-control requires "
822  "the 'qmin' option set.\n");
823  set_vbr(avctx);
824  return;
825  }
826  /* fall through */
827  case NV_ENC_PARAMS_RC_VBR_HQ:
828  case NV_ENC_PARAMS_RC_VBR:
829  set_vbr(avctx);
830  break;
831  case NV_ENC_PARAMS_RC_CBR:
832  case NV_ENC_PARAMS_RC_CBR_HQ:
833  case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
834  break;
835  }
836 
837  rc->rateControlMode = ctx->rc;
838 }
839 
841 {
842  NvencContext *ctx = avctx->priv_data;
843  // default minimum of 4 surfaces
844  // multiply by 2 for number of NVENCs on gpu (hardcode to 2)
845  // another multiply by 2 to avoid blocking next PBB group
846  int nb_surfaces = FFMAX(4, ctx->encode_config.frameIntervalP * 2 * 2);
847 
848  // lookahead enabled
849  if (ctx->rc_lookahead > 0) {
850  // +1 is to account for lkd_bound calculation later
851  // +4 is to allow sufficient pipelining with lookahead
852  nb_surfaces = FFMAX(1, FFMAX(nb_surfaces, ctx->rc_lookahead + ctx->encode_config.frameIntervalP + 1 + 4));
853  if (nb_surfaces > ctx->nb_surfaces && ctx->nb_surfaces > 0)
854  {
855  av_log(avctx, AV_LOG_WARNING,
856  "Defined rc_lookahead requires more surfaces, "
857  "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces);
858  }
859  ctx->nb_surfaces = FFMAX(nb_surfaces, ctx->nb_surfaces);
860  } else {
861  if (ctx->encode_config.frameIntervalP > 1 && ctx->nb_surfaces < nb_surfaces && ctx->nb_surfaces > 0)
862  {
863  av_log(avctx, AV_LOG_WARNING,
864  "Defined b-frame requires more surfaces, "
865  "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces);
866  ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, nb_surfaces);
867  }
868  else if (ctx->nb_surfaces <= 0)
869  ctx->nb_surfaces = nb_surfaces;
870  // otherwise use user specified value
871  }
872 
874  ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1);
875 
876  return 0;
877 }
878 
880 {
881  NvencContext *ctx = avctx->priv_data;
882 
883  if (avctx->global_quality > 0)
884  av_log(avctx, AV_LOG_WARNING, "Using global_quality with nvenc is deprecated. Use qp instead.\n");
885 
886  if (ctx->cqp < 0 && avctx->global_quality > 0)
887  ctx->cqp = avctx->global_quality;
888 
889  if (avctx->bit_rate > 0) {
890  ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
891  } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
892  ctx->encode_config.rcParams.maxBitRate = ctx->encode_config.rcParams.averageBitRate;
893  }
894 
895  if (avctx->rc_max_rate > 0)
896  ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
897 
898 #ifdef NVENC_HAVE_MULTIPASS
899  ctx->encode_config.rcParams.multiPass = ctx->multipass;
900 
901  if (ctx->flags & NVENC_ONE_PASS)
902  ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
903  if (ctx->flags & NVENC_TWO_PASSES || ctx->twopass > 0)
904  ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
905 
906  if (ctx->rc < 0) {
907  if (ctx->cbr) {
908  ctx->rc = NV_ENC_PARAMS_RC_CBR;
909  } else if (ctx->cqp >= 0) {
910  ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
911  } else if (ctx->quality >= 0.0f) {
912  ctx->rc = NV_ENC_PARAMS_RC_VBR;
913  }
914  }
915 #else
916  if (ctx->rc < 0) {
917  if (ctx->flags & NVENC_ONE_PASS)
918  ctx->twopass = 0;
919  if (ctx->flags & NVENC_TWO_PASSES)
920  ctx->twopass = 1;
921 
922  if (ctx->twopass < 0)
923  ctx->twopass = (ctx->flags & NVENC_LOWLATENCY) != 0;
924 
925  if (ctx->cbr) {
926  if (ctx->twopass) {
927  ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
928  } else {
929  ctx->rc = NV_ENC_PARAMS_RC_CBR;
930  }
931  } else if (ctx->cqp >= 0) {
932  ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
933  } else if (ctx->twopass) {
934  ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
935  } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
936  ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
937  }
938  }
939 #endif
940 
941  if (ctx->rc >= 0 && ctx->rc & RC_MODE_DEPRECATED) {
942  av_log(avctx, AV_LOG_WARNING, "Specified rc mode is deprecated.\n");
943  av_log(avctx, AV_LOG_WARNING, "Use -rc constqp/cbr/vbr, -tune and -multipass instead.\n");
944 
945  ctx->rc &= ~RC_MODE_DEPRECATED;
946  }
947 
948 #ifdef NVENC_HAVE_LDKFS
949  if (ctx->ldkfs)
950  ctx->encode_config.rcParams.lowDelayKeyFrameScale = ctx->ldkfs;
951 #endif
952 
953  if (ctx->flags & NVENC_LOSSLESS) {
954  set_lossless(avctx);
955  } else if (ctx->rc >= 0) {
957  } else {
958  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
959  set_vbr(avctx);
960  }
961 
962  if (avctx->rc_buffer_size > 0) {
963  ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size;
964  } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
965  avctx->rc_buffer_size = ctx->encode_config.rcParams.vbvBufferSize = 2 * ctx->encode_config.rcParams.averageBitRate;
966  }
967 
968  if (ctx->aq) {
969  ctx->encode_config.rcParams.enableAQ = 1;
970  ctx->encode_config.rcParams.aqStrength = ctx->aq_strength;
971  av_log(avctx, AV_LOG_VERBOSE, "AQ enabled.\n");
972  }
973 
974  if (ctx->temporal_aq) {
975  ctx->encode_config.rcParams.enableTemporalAQ = 1;
976  av_log(avctx, AV_LOG_VERBOSE, "Temporal AQ enabled.\n");
977  }
978 
979  if (ctx->rc_lookahead > 0) {
980  int lkd_bound = FFMIN(ctx->nb_surfaces, ctx->async_depth) -
981  ctx->encode_config.frameIntervalP - 4;
982 
983  if (lkd_bound < 0) {
984  av_log(avctx, AV_LOG_WARNING,
985  "Lookahead not enabled. Increase buffer delay (-delay).\n");
986  } else {
987  ctx->encode_config.rcParams.enableLookahead = 1;
988  ctx->encode_config.rcParams.lookaheadDepth = av_clip(ctx->rc_lookahead, 0, lkd_bound);
989  ctx->encode_config.rcParams.disableIadapt = ctx->no_scenecut;
990  ctx->encode_config.rcParams.disableBadapt = !ctx->b_adapt;
991  av_log(avctx, AV_LOG_VERBOSE,
992  "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
993  ctx->encode_config.rcParams.lookaheadDepth,
994  ctx->encode_config.rcParams.disableIadapt ? "disabled" : "enabled",
995  ctx->encode_config.rcParams.disableBadapt ? "disabled" : "enabled");
996  }
997  }
998 
999  if (ctx->strict_gop) {
1000  ctx->encode_config.rcParams.strictGOPTarget = 1;
1001  av_log(avctx, AV_LOG_VERBOSE, "Strict GOP target enabled.\n");
1002  }
1003 
1004  if (ctx->nonref_p)
1005  ctx->encode_config.rcParams.enableNonRefP = 1;
1006 
1007  if (ctx->zerolatency)
1008  ctx->encode_config.rcParams.zeroReorderDelay = 1;
1009 
1010  if (ctx->quality) {
1011  //convert from float to fixed point 8.8
1012  int tmp_quality = (int)(ctx->quality * 256.0f);
1013  ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1014  ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1015 
1016  av_log(avctx, AV_LOG_VERBOSE, "CQ(%d) mode enabled.\n", tmp_quality);
1017 
1018  //CQ mode shall discard avg bitrate & honor max bitrate;
1019  ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate = 0;
1020  ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
1021  }
1022 }
1023 
1025 {
1026  NvencContext *ctx = avctx->priv_data;
1027  NV_ENC_CONFIG *cc = &ctx->encode_config;
1028  NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1029  NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1030 
1031  vui->colourMatrix = avctx->colorspace;
1032  vui->colourPrimaries = avctx->color_primaries;
1033  vui->transferCharacteristics = avctx->color_trc;
1034  vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
1036 
1037  vui->colourDescriptionPresentFlag =
1038  (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2);
1039 
1040  vui->videoSignalTypePresentFlag =
1041  (vui->colourDescriptionPresentFlag
1042  || vui->videoFormat != 5
1043  || vui->videoFullRangeFlag != 0);
1044 
1045  h264->sliceMode = 3;
1046  h264->sliceModeData = 1;
1047 
1048  h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1049  h264->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1050  h264->outputAUD = ctx->aud;
1051 
1052  if (ctx->dpb_size >= 0) {
1053  /* 0 means "let the hardware decide" */
1054  h264->maxNumRefFrames = ctx->dpb_size;
1055  }
1056  if (avctx->gop_size >= 0) {
1057  h264->idrPeriod = cc->gopLength;
1058  }
1059 
1060  if (IS_CBR(cc->rcParams.rateControlMode)) {
1061  h264->outputBufferingPeriodSEI = 1;
1062  }
1063 
1064  h264->outputPictureTimingSEI = 1;
1065 
1066  if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1067  cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1068  cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1069  h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1070  h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1071  }
1072 
1073  if (ctx->flags & NVENC_LOSSLESS) {
1074  h264->qpPrimeYZeroTransformBypassFlag = 1;
1075  } else {
1076  switch(ctx->profile) {
1078  cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1080  break;
1082  cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1083  avctx->profile = FF_PROFILE_H264_MAIN;
1084  break;
1086  cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1087  avctx->profile = FF_PROFILE_H264_HIGH;
1088  break;
1090  cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1092  break;
1093  }
1094  }
1095 
1096  // force setting profile as high444p if input is AV_PIX_FMT_YUV444P
1097  if (ctx->data_pix_fmt == AV_PIX_FMT_YUV444P) {
1098  cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1100  }
1101 
1102  h264->chromaFormatIDC = avctx->profile == FF_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1;
1103 
1104  h264->level = ctx->level;
1105 
1106  if (ctx->coder >= 0)
1107  h264->entropyCodingMode = ctx->coder;
1108 
1109 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1110  h264->useBFramesAsRef = ctx->b_ref_mode;
1111 #endif
1112 
1113 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1114  h264->numRefL0 = avctx->refs;
1115  h264->numRefL1 = avctx->refs;
1116 #endif
1117 
1118  return 0;
1119 }
1120 
1122 {
1123  NvencContext *ctx = avctx->priv_data;
1124  NV_ENC_CONFIG *cc = &ctx->encode_config;
1125  NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1126  NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1127 
1128  vui->colourMatrix = avctx->colorspace;
1129  vui->colourPrimaries = avctx->color_primaries;
1130  vui->transferCharacteristics = avctx->color_trc;
1131  vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
1133 
1134  vui->colourDescriptionPresentFlag =
1135  (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2);
1136 
1137  vui->videoSignalTypePresentFlag =
1138  (vui->colourDescriptionPresentFlag
1139  || vui->videoFormat != 5
1140  || vui->videoFullRangeFlag != 0);
1141 
1142  hevc->sliceMode = 3;
1143  hevc->sliceModeData = 1;
1144 
1145  hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1146  hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1147  hevc->outputAUD = ctx->aud;
1148 
1149  if (ctx->dpb_size >= 0) {
1150  /* 0 means "let the hardware decide" */
1151  hevc->maxNumRefFramesInDPB = ctx->dpb_size;
1152  }
1153  if (avctx->gop_size >= 0) {
1154  hevc->idrPeriod = cc->gopLength;
1155  }
1156 
1157  if (IS_CBR(cc->rcParams.rateControlMode)) {
1158  hevc->outputBufferingPeriodSEI = 1;
1159  }
1160 
1161  hevc->outputPictureTimingSEI = 1;
1162 
1163  switch (ctx->profile) {
1165  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1166  avctx->profile = FF_PROFILE_HEVC_MAIN;
1167  break;
1169  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1171  break;
1173  cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1174  avctx->profile = FF_PROFILE_HEVC_REXT;
1175  break;
1176  }
1177 
1178  // force setting profile as main10 if input is 10 bit
1179  if (IS_10BIT(ctx->data_pix_fmt)) {
1180  cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1182  }
1183 
1184  // force setting profile as rext if input is yuv444
1185  if (IS_YUV444(ctx->data_pix_fmt)) {
1186  cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1187  avctx->profile = FF_PROFILE_HEVC_REXT;
1188  }
1189 
1190  hevc->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1;
1191 
1192  hevc->pixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0;
1193 
1194  hevc->level = ctx->level;
1195 
1196  hevc->tier = ctx->tier;
1197 
1198 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1199  hevc->useBFramesAsRef = ctx->b_ref_mode;
1200 #endif
1201 
1202 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1203  hevc->numRefL0 = avctx->refs;
1204  hevc->numRefL1 = avctx->refs;
1205 #endif
1206 
1207  return 0;
1208 }
1209 
1211 {
1212  switch (avctx->codec->id) {
1213  case AV_CODEC_ID_H264:
1214  return nvenc_setup_h264_config(avctx);
1215  case AV_CODEC_ID_HEVC:
1216  return nvenc_setup_hevc_config(avctx);
1217  /* Earlier switch/case will return if unknown codec is passed. */
1218  }
1219 
1220  return 0;
1221 }
1222 
1223 static void compute_dar(AVCodecContext *avctx, int *dw, int *dh) {
1224  int sw, sh;
1225 
1226  sw = avctx->width;
1227  sh = avctx->height;
1228 
1229  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
1230  sw *= avctx->sample_aspect_ratio.num;
1231  sh *= avctx->sample_aspect_ratio.den;
1232  }
1233 
1234  av_reduce(dw, dh, sw, sh, 1024 * 1024);
1235 }
1236 
1238 {
1239  NvencContext *ctx = avctx->priv_data;
1241  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1242 
1243  NV_ENC_PRESET_CONFIG preset_config = { 0 };
1244  NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1245  AVCPBProperties *cpb_props;
1246  int res = 0;
1247  int dw, dh;
1248 
1249  ctx->encode_config.version = NV_ENC_CONFIG_VER;
1250  ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1251 
1252  ctx->init_encode_params.encodeHeight = avctx->height;
1253  ctx->init_encode_params.encodeWidth = avctx->width;
1254 
1255  ctx->init_encode_params.encodeConfig = &ctx->encode_config;
1256 
1257  nvenc_map_preset(ctx);
1258 
1259  if (ctx->flags & NVENC_DEPRECATED_PRESET)
1260  av_log(avctx, AV_LOG_WARNING, "The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
1261 
1262  preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1263  preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1264 
1265 #ifdef NVENC_HAVE_NEW_PRESETS
1266  ctx->init_encode_params.tuningInfo = ctx->tuning_info;
1267 
1268  if (ctx->flags & NVENC_LOWLATENCY)
1269  ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1270 
1271  nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(ctx->nvencoder,
1272  ctx->init_encode_params.encodeGUID,
1273  ctx->init_encode_params.presetGUID,
1274  ctx->init_encode_params.tuningInfo,
1275  &preset_config);
1276 #else
1277  // Turn off tuning info parameter if older presets are on
1278  ctx->init_encode_params.tuningInfo = 0;
1279 
1280  nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder,
1281  ctx->init_encode_params.encodeGUID,
1282  ctx->init_encode_params.presetGUID,
1283  &preset_config);
1284 #endif
1285  if (nv_status != NV_ENC_SUCCESS)
1286  return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration");
1287 
1288  memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
1289 
1290  ctx->encode_config.version = NV_ENC_CONFIG_VER;
1291 
1292  compute_dar(avctx, &dw, &dh);
1293  ctx->init_encode_params.darHeight = dh;
1294  ctx->init_encode_params.darWidth = dw;
1295 
1296  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
1297  ctx->init_encode_params.frameRateNum = avctx->framerate.num;
1298  ctx->init_encode_params.frameRateDen = avctx->framerate.den;
1299  } else {
1300  ctx->init_encode_params.frameRateNum = avctx->time_base.den;
1301  ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
1302  }
1303 
1304  ctx->init_encode_params.enableEncodeAsync = 0;
1305  ctx->init_encode_params.enablePTD = 1;
1306 
1307 #ifdef NVENC_HAVE_NEW_PRESETS
1308  /* If lookahead isn't set from CLI, use value from preset.
1309  * P6 & P7 presets may enable lookahead for better quality.
1310  * */
1311  if (ctx->rc_lookahead == 0 && ctx->encode_config.rcParams.enableLookahead)
1312  ctx->rc_lookahead = ctx->encode_config.rcParams.lookaheadDepth;
1313 
1314  if (ctx->init_encode_params.tuningInfo == NV_ENC_TUNING_INFO_LOSSLESS)
1315  ctx->flags |= NVENC_LOSSLESS;
1316 #endif
1317 
1318  if (ctx->weighted_pred == 1)
1319  ctx->init_encode_params.enableWeightedPrediction = 1;
1320 
1321  if (ctx->bluray_compat) {
1322  ctx->aud = 1;
1323  ctx->dpb_size = FFMIN(FFMAX(avctx->refs, 0), 6);
1324  avctx->max_b_frames = FFMIN(avctx->max_b_frames, 3);
1325  switch (avctx->codec->id) {
1326  case AV_CODEC_ID_H264:
1327  /* maximum level depends on used resolution */
1328  break;
1329  case AV_CODEC_ID_HEVC:
1330  ctx->level = NV_ENC_LEVEL_HEVC_51;
1331  ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1332  break;
1333  }
1334  }
1335 
1336  if (avctx->gop_size > 0) {
1337  if (avctx->max_b_frames >= 0) {
1338  /* 0 is intra-only, 1 is I/P only, 2 is one B-Frame, 3 two B-frames, and so on. */
1339  ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
1340  }
1341 
1342  ctx->encode_config.gopLength = avctx->gop_size;
1343  } else if (avctx->gop_size == 0) {
1344  ctx->encode_config.frameIntervalP = 0;
1345  ctx->encode_config.gopLength = 1;
1346  }
1347 
1348  nvenc_recalc_surfaces(avctx);
1349 
1350  nvenc_setup_rate_control(avctx);
1351 
1352  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
1353  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1354  } else {
1355  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1356  }
1357 
1358  res = nvenc_setup_codec_config(avctx);
1359  if (res)
1360  return res;
1361 
1362  res = nvenc_push_context(avctx);
1363  if (res < 0)
1364  return res;
1365 
1366  nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
1367  if (nv_status != NV_ENC_SUCCESS) {
1368  nvenc_pop_context(avctx);
1369  return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed");
1370  }
1371 
1372 #ifdef NVENC_HAVE_CUSTREAM_PTR
1373  if (ctx->cu_context) {
1374  nv_status = p_nvenc->nvEncSetIOCudaStreams(ctx->nvencoder, &ctx->cu_stream, &ctx->cu_stream);
1375  if (nv_status != NV_ENC_SUCCESS) {
1376  nvenc_pop_context(avctx);
1377  return nvenc_print_error(avctx, nv_status, "SetIOCudaStreams failed");
1378  }
1379  }
1380 #endif
1381 
1382  res = nvenc_pop_context(avctx);
1383  if (res < 0)
1384  return res;
1385 
1386  if (ctx->encode_config.frameIntervalP > 1)
1387  avctx->has_b_frames = 2;
1388 
1389  if (ctx->encode_config.rcParams.averageBitRate > 0)
1390  avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
1391 
1392  cpb_props = ff_add_cpb_side_data(avctx);
1393  if (!cpb_props)
1394  return AVERROR(ENOMEM);
1395  cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate;
1396  cpb_props->avg_bitrate = avctx->bit_rate;
1397  cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize;
1398 
1399  return 0;
1400 }
1401 
1402 static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt)
1403 {
1404  switch (pix_fmt) {
1405  case AV_PIX_FMT_YUV420P:
1406  return NV_ENC_BUFFER_FORMAT_YV12_PL;
1407  case AV_PIX_FMT_NV12:
1408  return NV_ENC_BUFFER_FORMAT_NV12_PL;
1409  case AV_PIX_FMT_P010:
1410  case AV_PIX_FMT_P016:
1411  return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1412  case AV_PIX_FMT_YUV444P:
1413  return NV_ENC_BUFFER_FORMAT_YUV444_PL;
1414  case AV_PIX_FMT_YUV444P16:
1415  return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1416  case AV_PIX_FMT_0RGB32:
1417  return NV_ENC_BUFFER_FORMAT_ARGB;
1418  case AV_PIX_FMT_0BGR32:
1419  return NV_ENC_BUFFER_FORMAT_ABGR;
1420  default:
1421  return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1422  }
1423 }
1424 
1425 static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
1426 {
1427  NvencContext *ctx = avctx->priv_data;
1429  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1430  NvencSurface* tmp_surface = &ctx->surfaces[idx];
1431 
1432  NVENCSTATUS nv_status;
1433  NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1434  allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1435 
1436  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1437  ctx->surfaces[idx].in_ref = av_frame_alloc();
1438  if (!ctx->surfaces[idx].in_ref)
1439  return AVERROR(ENOMEM);
1440  } else {
1441  NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1442 
1444  if (ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1445  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
1447  return AVERROR(EINVAL);
1448  }
1449 
1450  allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1451  allocSurf.width = avctx->width;
1452  allocSurf.height = avctx->height;
1453  allocSurf.bufferFmt = ctx->surfaces[idx].format;
1454 
1455  nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
1456  if (nv_status != NV_ENC_SUCCESS) {
1457  return nvenc_print_error(avctx, nv_status, "CreateInputBuffer failed");
1458  }
1459 
1460  ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1461  ctx->surfaces[idx].width = allocSurf.width;
1462  ctx->surfaces[idx].height = allocSurf.height;
1463  }
1464 
1465  nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
1466  if (nv_status != NV_ENC_SUCCESS) {
1467  int err = nvenc_print_error(avctx, nv_status, "CreateBitstreamBuffer failed");
1468  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1469  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[idx].input_surface);
1470  av_frame_free(&ctx->surfaces[idx].in_ref);
1471  return err;
1472  }
1473 
1474  ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1475 
1476  av_fifo_generic_write(ctx->unused_surface_queue, &tmp_surface, sizeof(tmp_surface), NULL);
1477 
1478  return 0;
1479 }
1480 
1482 {
1483  NvencContext *ctx = avctx->priv_data;
1484  int i, res = 0, res2;
1485 
1486  ctx->surfaces = av_mallocz_array(ctx->nb_surfaces, sizeof(*ctx->surfaces));
1487  if (!ctx->surfaces)
1488  return AVERROR(ENOMEM);
1489 
1490  ctx->timestamp_list = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t));
1491  if (!ctx->timestamp_list)
1492  return AVERROR(ENOMEM);
1493 
1495  if (!ctx->unused_surface_queue)
1496  return AVERROR(ENOMEM);
1497 
1499  if (!ctx->output_surface_queue)
1500  return AVERROR(ENOMEM);
1502  if (!ctx->output_surface_ready_queue)
1503  return AVERROR(ENOMEM);
1504 
1505  res = nvenc_push_context(avctx);
1506  if (res < 0)
1507  return res;
1508 
1509  for (i = 0; i < ctx->nb_surfaces; i++) {
1510  if ((res = nvenc_alloc_surface(avctx, i)) < 0)
1511  goto fail;
1512  }
1513 
1514 fail:
1515  res2 = nvenc_pop_context(avctx);
1516  if (res2 < 0)
1517  return res2;
1518 
1519  return res;
1520 }
1521 
1523 {
1524  NvencContext *ctx = avctx->priv_data;
1526  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1527 
1528  NVENCSTATUS nv_status;
1529  uint32_t outSize = 0;
1530  char tmpHeader[256];
1531  NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1532  payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1533 
1534  payload.spsppsBuffer = tmpHeader;
1535  payload.inBufferSize = sizeof(tmpHeader);
1536  payload.outSPSPPSPayloadSize = &outSize;
1537 
1538  nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
1539  if (nv_status != NV_ENC_SUCCESS) {
1540  return nvenc_print_error(avctx, nv_status, "GetSequenceParams failed");
1541  }
1542 
1543  avctx->extradata_size = outSize;
1545 
1546  if (!avctx->extradata) {
1547  return AVERROR(ENOMEM);
1548  }
1549 
1550  memcpy(avctx->extradata, tmpHeader, outSize);
1551 
1552  return 0;
1553 }
1554 
1556 {
1557  NvencContext *ctx = avctx->priv_data;
1559  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1560  int i, res;
1561 
1562  /* the encoder has to be flushed before it can be closed */
1563  if (ctx->nvencoder) {
1564  NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1565  .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1566 
1567  res = nvenc_push_context(avctx);
1568  if (res < 0)
1569  return res;
1570 
1571  p_nvenc->nvEncEncodePicture(ctx->nvencoder, &params);
1572  }
1573 
1578 
1579  if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) {
1580  for (i = 0; i < ctx->nb_registered_frames; i++) {
1581  if (ctx->registered_frames[i].mapped)
1582  p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[i].in_map.mappedResource);
1583  if (ctx->registered_frames[i].regptr)
1584  p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr);
1585  }
1586  ctx->nb_registered_frames = 0;
1587  }
1588 
1589  if (ctx->surfaces) {
1590  for (i = 0; i < ctx->nb_surfaces; ++i) {
1591  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1592  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[i].input_surface);
1593  av_frame_free(&ctx->surfaces[i].in_ref);
1594  p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->surfaces[i].output_surface);
1595  }
1596  }
1597  av_freep(&ctx->surfaces);
1598  ctx->nb_surfaces = 0;
1599 
1600  av_frame_free(&ctx->frame);
1601 
1602  if (ctx->nvencoder) {
1603  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
1604 
1605  res = nvenc_pop_context(avctx);
1606  if (res < 0)
1607  return res;
1608  }
1609  ctx->nvencoder = NULL;
1610 
1611  if (ctx->cu_context_internal)
1612  CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal));
1613  ctx->cu_context = ctx->cu_context_internal = NULL;
1614 
1615 #if CONFIG_D3D11VA
1616  if (ctx->d3d11_device) {
1617  ID3D11Device_Release(ctx->d3d11_device);
1618  ctx->d3d11_device = NULL;
1619  }
1620 #endif
1621 
1622  nvenc_free_functions(&dl_fn->nvenc_dl);
1623  cuda_free_functions(&dl_fn->cuda_dl);
1624 
1625  dl_fn->nvenc_device_count = 0;
1626 
1627  av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
1628 
1629  return 0;
1630 }
1631 
1633 {
1634  NvencContext *ctx = avctx->priv_data;
1635  int ret;
1636 
1637  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1638  AVHWFramesContext *frames_ctx;
1639  if (!avctx->hw_frames_ctx) {
1640  av_log(avctx, AV_LOG_ERROR,
1641  "hw_frames_ctx must be set when using GPU frames as input\n");
1642  return AVERROR(EINVAL);
1643  }
1644  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1645  if (frames_ctx->format != avctx->pix_fmt) {
1646  av_log(avctx, AV_LOG_ERROR,
1647  "hw_frames_ctx must match the GPU frame type\n");
1648  return AVERROR(EINVAL);
1649  }
1650  ctx->data_pix_fmt = frames_ctx->sw_format;
1651  } else {
1652  ctx->data_pix_fmt = avctx->pix_fmt;
1653  }
1654 
1655  ctx->frame = av_frame_alloc();
1656  if (!ctx->frame)
1657  return AVERROR(ENOMEM);
1658 
1659  if ((ret = nvenc_load_libraries(avctx)) < 0)
1660  return ret;
1661 
1662  if ((ret = nvenc_setup_device(avctx)) < 0)
1663  return ret;
1664 
1665  if ((ret = nvenc_setup_encoder(avctx)) < 0)
1666  return ret;
1667 
1668  if ((ret = nvenc_setup_surfaces(avctx)) < 0)
1669  return ret;
1670 
1671  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1672  if ((ret = nvenc_setup_extradata(avctx)) < 0)
1673  return ret;
1674  }
1675 
1676  return 0;
1677 }
1678 
1680 {
1681  NvencSurface *tmp_surf;
1682 
1683  if (!(av_fifo_size(ctx->unused_surface_queue) > 0))
1684  // queue empty
1685  return NULL;
1686 
1687  av_fifo_generic_read(ctx->unused_surface_queue, &tmp_surf, sizeof(tmp_surf), NULL);
1688  return tmp_surf;
1689 }
1690 
1691 static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface,
1692  NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame)
1693 {
1694  int dst_linesize[4] = {
1695  lock_buffer_params->pitch,
1696  lock_buffer_params->pitch,
1697  lock_buffer_params->pitch,
1698  lock_buffer_params->pitch
1699  };
1700  uint8_t *dst_data[4];
1701  int ret;
1702 
1703  if (frame->format == AV_PIX_FMT_YUV420P)
1704  dst_linesize[1] = dst_linesize[2] >>= 1;
1705 
1706  ret = av_image_fill_pointers(dst_data, frame->format, nv_surface->height,
1707  lock_buffer_params->bufferDataPtr, dst_linesize);
1708  if (ret < 0)
1709  return ret;
1710 
1711  if (frame->format == AV_PIX_FMT_YUV420P)
1712  FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
1713 
1714  av_image_copy(dst_data, dst_linesize,
1715  (const uint8_t**)frame->data, frame->linesize, frame->format,
1716  avctx->width, avctx->height);
1717 
1718  return 0;
1719 }
1720 
1722 {
1723  NvencContext *ctx = avctx->priv_data;
1725  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1726  NVENCSTATUS nv_status;
1727 
1728  int i, first_round;
1729 
1731  for (first_round = 1; first_round >= 0; first_round--) {
1732  for (i = 0; i < ctx->nb_registered_frames; i++) {
1733  if (!ctx->registered_frames[i].mapped) {
1734  if (ctx->registered_frames[i].regptr) {
1735  if (first_round)
1736  continue;
1737  nv_status = p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr);
1738  if (nv_status != NV_ENC_SUCCESS)
1739  return nvenc_print_error(avctx, nv_status, "Failed unregistering unused input resource");
1740  ctx->registered_frames[i].ptr = NULL;
1741  ctx->registered_frames[i].regptr = NULL;
1742  }
1743  return i;
1744  }
1745  }
1746  }
1747  } else {
1748  return ctx->nb_registered_frames++;
1749  }
1750 
1751  av_log(avctx, AV_LOG_ERROR, "Too many registered CUDA frames\n");
1752  return AVERROR(ENOMEM);
1753 }
1754 
1756 {
1757  NvencContext *ctx = avctx->priv_data;
1759  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1760 
1761  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frame->hw_frames_ctx->data;
1762  NV_ENC_REGISTER_RESOURCE reg;
1763  int i, idx, ret;
1764 
1765  for (i = 0; i < ctx->nb_registered_frames; i++) {
1766  if (avctx->pix_fmt == AV_PIX_FMT_CUDA && ctx->registered_frames[i].ptr == frame->data[0])
1767  return i;
1768  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])
1769  return i;
1770  }
1771 
1772  idx = nvenc_find_free_reg_resource(avctx);
1773  if (idx < 0)
1774  return idx;
1775 
1776  reg.version = NV_ENC_REGISTER_RESOURCE_VER;
1777  reg.width = frames_ctx->width;
1778  reg.height = frames_ctx->height;
1779  reg.pitch = frame->linesize[0];
1780  reg.resourceToRegister = frame->data[0];
1781 
1782  if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
1783  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
1784  }
1785  else if (avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1786  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
1787  reg.subResourceIndex = (intptr_t)frame->data[1];
1788  }
1789 
1790  reg.bufferFormat = nvenc_map_buffer_format(frames_ctx->sw_format);
1791  if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1792  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
1793  av_get_pix_fmt_name(frames_ctx->sw_format));
1794  return AVERROR(EINVAL);
1795  }
1796 
1797  ret = p_nvenc->nvEncRegisterResource(ctx->nvencoder, &reg);
1798  if (ret != NV_ENC_SUCCESS) {
1799  nvenc_print_error(avctx, ret, "Error registering an input resource");
1800  return AVERROR_UNKNOWN;
1801  }
1802 
1803  ctx->registered_frames[idx].ptr = frame->data[0];
1804  ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
1805  ctx->registered_frames[idx].regptr = reg.registeredResource;
1806  return idx;
1807 }
1808 
1810  NvencSurface *nvenc_frame)
1811 {
1812  NvencContext *ctx = avctx->priv_data;
1814  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1815 
1816  int res;
1817  NVENCSTATUS nv_status;
1818 
1819  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1820  int reg_idx = nvenc_register_frame(avctx, frame);
1821  if (reg_idx < 0) {
1822  av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n");
1823  return reg_idx;
1824  }
1825 
1826  res = av_frame_ref(nvenc_frame->in_ref, frame);
1827  if (res < 0)
1828  return res;
1829 
1830  if (!ctx->registered_frames[reg_idx].mapped) {
1831  ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
1832  ctx->registered_frames[reg_idx].in_map.registeredResource = ctx->registered_frames[reg_idx].regptr;
1833  nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &ctx->registered_frames[reg_idx].in_map);
1834  if (nv_status != NV_ENC_SUCCESS) {
1835  av_frame_unref(nvenc_frame->in_ref);
1836  return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");
1837  }
1838  }
1839 
1840  ctx->registered_frames[reg_idx].mapped += 1;
1841 
1842  nvenc_frame->reg_idx = reg_idx;
1843  nvenc_frame->input_surface = ctx->registered_frames[reg_idx].in_map.mappedResource;
1844  nvenc_frame->format = ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
1845  nvenc_frame->pitch = frame->linesize[0];
1846 
1847  return 0;
1848  } else {
1849  NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1850 
1851  lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1852  lockBufferParams.inputBuffer = nvenc_frame->input_surface;
1853 
1854  nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
1855  if (nv_status != NV_ENC_SUCCESS) {
1856  return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer");
1857  }
1858 
1859  nvenc_frame->pitch = lockBufferParams.pitch;
1860  res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame);
1861 
1862  nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, nvenc_frame->input_surface);
1863  if (nv_status != NV_ENC_SUCCESS) {
1864  return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!");
1865  }
1866 
1867  return res;
1868  }
1869 }
1870 
1872  NV_ENC_PIC_PARAMS *params,
1873  NV_ENC_SEI_PAYLOAD *sei_data,
1874  int sei_count)
1875 {
1876  NvencContext *ctx = avctx->priv_data;
1877 
1878  switch (avctx->codec->id) {
1879  case AV_CODEC_ID_H264:
1880  params->codecPicParams.h264PicParams.sliceMode =
1881  ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
1882  params->codecPicParams.h264PicParams.sliceModeData =
1883  ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1884  if (sei_count > 0) {
1885  params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
1886  params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
1887  }
1888 
1889  break;
1890  case AV_CODEC_ID_HEVC:
1891  params->codecPicParams.hevcPicParams.sliceMode =
1892  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
1893  params->codecPicParams.hevcPicParams.sliceModeData =
1894  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1895  if (sei_count > 0) {
1896  params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
1897  params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
1898  }
1899 
1900  break;
1901  }
1902 }
1903 
1904 static inline void timestamp_queue_enqueue(AVFifoBuffer* queue, int64_t timestamp)
1905 {
1906  av_fifo_generic_write(queue, &timestamp, sizeof(timestamp), NULL);
1907 }
1908 
1909 static inline int64_t timestamp_queue_dequeue(AVFifoBuffer* queue)
1910 {
1911  int64_t timestamp = AV_NOPTS_VALUE;
1912  if (av_fifo_size(queue) > 0)
1913  av_fifo_generic_read(queue, &timestamp, sizeof(timestamp), NULL);
1914 
1915  return timestamp;
1916 }
1917 
1919  NV_ENC_LOCK_BITSTREAM *params,
1920  AVPacket *pkt)
1921 {
1922  NvencContext *ctx = avctx->priv_data;
1923 
1924  pkt->pts = params->outputTimeStamp;
1926 
1927  pkt->dts -= FFMAX(avctx->max_b_frames, 0) * FFMIN(avctx->ticks_per_frame, 1);
1928 
1929  return 0;
1930 }
1931 
1933 {
1934  NvencContext *ctx = avctx->priv_data;
1936  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1937 
1938  uint32_t slice_mode_data;
1939  uint32_t *slice_offsets = NULL;
1940  NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
1941  NVENCSTATUS nv_status;
1942  int res = 0;
1943 
1944  enum AVPictureType pict_type;
1945 
1946  switch (avctx->codec->id) {
1947  case AV_CODEC_ID_H264:
1948  slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1949  break;
1950  case AV_CODEC_ID_H265:
1951  slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1952  break;
1953  default:
1954  av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
1955  res = AVERROR(EINVAL);
1956  goto error;
1957  }
1958  slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));
1959 
1960  if (!slice_offsets) {
1961  res = AVERROR(ENOMEM);
1962  goto error;
1963  }
1964 
1965  lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
1966 
1967  lock_params.doNotWait = 0;
1968  lock_params.outputBitstream = tmpoutsurf->output_surface;
1969  lock_params.sliceOffsets = slice_offsets;
1970 
1971  nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
1972  if (nv_status != NV_ENC_SUCCESS) {
1973  res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer");
1974  goto error;
1975  }
1976 
1977  res = av_new_packet(pkt, lock_params.bitstreamSizeInBytes);
1978 
1979  if (res < 0) {
1980  p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1981  goto error;
1982  }
1983 
1984  memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
1985 
1986  nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1987  if (nv_status != NV_ENC_SUCCESS) {
1988  res = nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");
1989  goto error;
1990  }
1991 
1992 
1993  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1994  ctx->registered_frames[tmpoutsurf->reg_idx].mapped -= 1;
1995  if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped == 0) {
1996  nv_status = p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[tmpoutsurf->reg_idx].in_map.mappedResource);
1997  if (nv_status != NV_ENC_SUCCESS) {
1998  res = nvenc_print_error(avctx, nv_status, "Failed unmapping input resource");
1999  goto error;
2000  }
2001  } else if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped < 0) {
2002  res = AVERROR_BUG;
2003  goto error;
2004  }
2005 
2006  av_frame_unref(tmpoutsurf->in_ref);
2007 
2008  tmpoutsurf->input_surface = NULL;
2009  }
2010 
2011  switch (lock_params.pictureType) {
2012  case NV_ENC_PIC_TYPE_IDR:
2013  pkt->flags |= AV_PKT_FLAG_KEY;
2014  case NV_ENC_PIC_TYPE_I:
2015  pict_type = AV_PICTURE_TYPE_I;
2016  break;
2017  case NV_ENC_PIC_TYPE_P:
2018  pict_type = AV_PICTURE_TYPE_P;
2019  break;
2020  case NV_ENC_PIC_TYPE_B:
2021  pict_type = AV_PICTURE_TYPE_B;
2022  break;
2023  case NV_ENC_PIC_TYPE_BI:
2024  pict_type = AV_PICTURE_TYPE_BI;
2025  break;
2026  default:
2027  av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
2028  av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
2029  res = AVERROR_EXTERNAL;
2030  goto error;
2031  }
2032 
2033 #if FF_API_CODED_FRAME
2035  avctx->coded_frame->pict_type = pict_type;
2037 #endif
2038 
2040  (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type);
2041 
2042  res = nvenc_set_timestamp(avctx, &lock_params, pkt);
2043  if (res < 0)
2044  goto error2;
2045 
2046  av_free(slice_offsets);
2047 
2048  return 0;
2049 
2050 error:
2052 
2053 error2:
2054  av_free(slice_offsets);
2055 
2056  return res;
2057 }
2058 
2059 static int output_ready(AVCodecContext *avctx, int flush)
2060 {
2061  NvencContext *ctx = avctx->priv_data;
2062  int nb_ready, nb_pending;
2063 
2064  nb_ready = av_fifo_size(ctx->output_surface_ready_queue) / sizeof(NvencSurface*);
2065  nb_pending = av_fifo_size(ctx->output_surface_queue) / sizeof(NvencSurface*);
2066  if (flush)
2067  return nb_ready > 0;
2068  return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth);
2069 }
2070 
2071 static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
2072 {
2073  NvencContext *ctx = avctx->priv_data;
2074  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
2075  NVENCSTATUS ret;
2076 
2077  NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2078  int needs_reconfig = 0;
2079  int needs_encode_config = 0;
2080  int reconfig_bitrate = 0, reconfig_dar = 0;
2081  int dw, dh;
2082 
2083  params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2084  params.reInitEncodeParams = ctx->init_encode_params;
2085 
2086  compute_dar(avctx, &dw, &dh);
2087  if (dw != ctx->init_encode_params.darWidth || dh != ctx->init_encode_params.darHeight) {
2088  av_log(avctx, AV_LOG_VERBOSE,
2089  "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2090  ctx->init_encode_params.darWidth,
2091  ctx->init_encode_params.darHeight, dw, dh);
2092 
2093  params.reInitEncodeParams.darHeight = dh;
2094  params.reInitEncodeParams.darWidth = dw;
2095 
2096  needs_reconfig = 1;
2097  reconfig_dar = 1;
2098  }
2099 
2100  if (ctx->rc != NV_ENC_PARAMS_RC_CONSTQP && ctx->support_dyn_bitrate) {
2101  if (avctx->bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->bit_rate) {
2102  av_log(avctx, AV_LOG_VERBOSE,
2103  "avg bitrate change: %d -> %d\n",
2104  params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2105  (uint32_t)avctx->bit_rate);
2106 
2107  params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->bit_rate;
2108  reconfig_bitrate = 1;
2109  }
2110 
2111  if (avctx->rc_max_rate > 0 && ctx->encode_config.rcParams.maxBitRate != avctx->rc_max_rate) {
2112  av_log(avctx, AV_LOG_VERBOSE,
2113  "max bitrate change: %d -> %d\n",
2114  params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2115  (uint32_t)avctx->rc_max_rate);
2116 
2117  params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->rc_max_rate;
2118  reconfig_bitrate = 1;
2119  }
2120 
2121  if (avctx->rc_buffer_size > 0 && ctx->encode_config.rcParams.vbvBufferSize != avctx->rc_buffer_size) {
2122  av_log(avctx, AV_LOG_VERBOSE,
2123  "vbv buffer size change: %d -> %d\n",
2124  params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2125  avctx->rc_buffer_size);
2126 
2127  params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->rc_buffer_size;
2128  reconfig_bitrate = 1;
2129  }
2130 
2131  if (reconfig_bitrate) {
2132  params.resetEncoder = 1;
2133  params.forceIDR = 1;
2134 
2135  needs_encode_config = 1;
2136  needs_reconfig = 1;
2137  }
2138  }
2139 
2140  if (!needs_encode_config)
2141  params.reInitEncodeParams.encodeConfig = NULL;
2142 
2143  if (needs_reconfig) {
2144  ret = p_nvenc->nvEncReconfigureEncoder(ctx->nvencoder, &params);
2145  if (ret != NV_ENC_SUCCESS) {
2146  nvenc_print_error(avctx, ret, "failed to reconfigure nvenc");
2147  } else {
2148  if (reconfig_dar) {
2149  ctx->init_encode_params.darHeight = dh;
2150  ctx->init_encode_params.darWidth = dw;
2151  }
2152 
2153  if (reconfig_bitrate) {
2154  ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2155  ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2156  ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2157  }
2158 
2159  }
2160  }
2161 }
2162 
2163 static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
2164 {
2165  NVENCSTATUS nv_status;
2166  NvencSurface *tmp_out_surf, *in_surf;
2167  int res, res2;
2168  NV_ENC_SEI_PAYLOAD sei_data[8];
2169  int sei_count = 0;
2170  int i;
2171 
2172  NvencContext *ctx = avctx->priv_data;
2174  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
2175 
2176  NV_ENC_PIC_PARAMS pic_params = { 0 };
2177  pic_params.version = NV_ENC_PIC_PARAMS_VER;
2178 
2179  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
2180  return AVERROR(EINVAL);
2181 
2182  if (frame && frame->buf[0]) {
2183  in_surf = get_free_frame(ctx);
2184  if (!in_surf)
2185  return AVERROR(EAGAIN);
2186 
2187  res = nvenc_push_context(avctx);
2188  if (res < 0)
2189  return res;
2190 
2191  reconfig_encoder(avctx, frame);
2192 
2193  res = nvenc_upload_frame(avctx, frame, in_surf);
2194 
2195  res2 = nvenc_pop_context(avctx);
2196  if (res2 < 0)
2197  return res2;
2198 
2199  if (res)
2200  return res;
2201 
2202  pic_params.inputBuffer = in_surf->input_surface;
2203  pic_params.bufferFmt = in_surf->format;
2204  pic_params.inputWidth = in_surf->width;
2205  pic_params.inputHeight = in_surf->height;
2206  pic_params.inputPitch = in_surf->pitch;
2207  pic_params.outputBitstream = in_surf->output_surface;
2208 
2209  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2210  if (frame->top_field_first)
2211  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2212  else
2213  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2214  } else {
2215  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2216  }
2217 
2218  if (ctx->forced_idr >= 0 && frame->pict_type == AV_PICTURE_TYPE_I) {
2219  pic_params.encodePicFlags =
2220  ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2221  } else {
2222  pic_params.encodePicFlags = 0;
2223  }
2224 
2225  pic_params.inputTimeStamp = frame->pts;
2226 
2227  if (ctx->a53_cc && av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC)) {
2228  void *a53_data = NULL;
2229  size_t a53_size = 0;
2230 
2231  if (ff_alloc_a53_sei(frame, 0, (void**)&a53_data, &a53_size) < 0) {
2232  av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2233  }
2234 
2235  if (a53_data) {
2236  sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2237  sei_data[sei_count].payloadType = 4;
2238  sei_data[sei_count].payload = (uint8_t*)a53_data;
2239  sei_count ++;
2240  }
2241  }
2242 
2244  void *tc_data = NULL;
2245  size_t tc_size = 0;
2246 
2247  if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, (void**)&tc_data, &tc_size) < 0) {
2248  av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n");
2249  }
2250 
2251  if (tc_data) {
2252  sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2253  sei_data[sei_count].payloadType = HEVC_SEI_TYPE_TIME_CODE;
2254  sei_data[sei_count].payload = (uint8_t*)tc_data;
2255  sei_count ++;
2256  }
2257  }
2258 
2259  nvenc_codec_specific_pic_params(avctx, &pic_params, sei_data, sei_count);
2260  } else {
2261  pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2262  }
2263 
2264  res = nvenc_push_context(avctx);
2265  if (res < 0)
2266  return res;
2267 
2268  nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
2269 
2270  for ( i = 0; i < sei_count; i++)
2271  av_freep(&sei_data[i].payload);
2272 
2273  res = nvenc_pop_context(avctx);
2274  if (res < 0)
2275  return res;
2276 
2277  if (nv_status != NV_ENC_SUCCESS &&
2278  nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2279  return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
2280 
2281  if (frame && frame->buf[0]) {
2282  av_fifo_generic_write(ctx->output_surface_queue, &in_surf, sizeof(in_surf), NULL);
2284  }
2285 
2286  /* all the pending buffers are now ready for output */
2287  if (nv_status == NV_ENC_SUCCESS) {
2288  while (av_fifo_size(ctx->output_surface_queue) > 0) {
2289  av_fifo_generic_read(ctx->output_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
2290  av_fifo_generic_write(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
2291  }
2292  }
2293 
2294  return 0;
2295 }
2296 
2298 {
2299  NvencSurface *tmp_out_surf;
2300  int res, res2;
2301 
2302  NvencContext *ctx = avctx->priv_data;
2303 
2304  AVFrame *frame = ctx->frame;
2305 
2306  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
2307  return AVERROR(EINVAL);
2308 
2309  if (!frame->buf[0]) {
2310  res = ff_encode_get_frame(avctx, frame);
2311  if (res < 0 && res != AVERROR_EOF)
2312  return res;
2313  }
2314 
2315  res = nvenc_send_frame(avctx, frame);
2316  if (res < 0) {
2317  if (res != AVERROR(EAGAIN))
2318  return res;
2319  } else
2320  av_frame_unref(frame);
2321 
2322  if (output_ready(avctx, avctx->internal->draining)) {
2323  av_fifo_generic_read(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
2324 
2325  res = nvenc_push_context(avctx);
2326  if (res < 0)
2327  return res;
2328 
2329  res = process_output_surface(avctx, pkt, tmp_out_surf);
2330 
2331  res2 = nvenc_pop_context(avctx);
2332  if (res2 < 0)
2333  return res2;
2334 
2335  if (res)
2336  return res;
2337 
2338  av_fifo_generic_write(ctx->unused_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
2339  } else if (avctx->internal->draining) {
2340  return AVERROR_EOF;
2341  } else {
2342  return AVERROR(EAGAIN);
2343  }
2344 
2345  return 0;
2346 }
2347 
2349 {
2350  NvencContext *ctx = avctx->priv_data;
2351 
2352  nvenc_send_frame(avctx, NULL);
2354 }
const GUID guid
Definition: nvenc.c:642
#define FF_PROFILE_H264_MAIN
Definition: avcodec.h:1900
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
int no_scenecut
Definition: nvenc.h:198
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:535
AVRational framerate
Definition: avcodec.h:2069
BI type.
Definition: avutil.h:280
void * nvencoder
Definition: nvenc.h:184
int support_dyn_bitrate
Definition: nvenc.h:182
av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:1555
int twopass
Definition: nvenc.h:192
#define CHECK_CU(x)
Definition: nvenc.c:39
static enum AVPixelFormat pix_fmt
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.h:83
int height
Definition: nvenc.h:79
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx)
Definition: nvenc.c:1210
AVFifoBuffer * timestamp_list
Definition: nvenc.h:167
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:796
static void flush(AVCodecContext *avctx)
NvencFunctions * nvenc_dl
Definition: nvenc.h:89
int mapped
Definition: nvenc.h:173
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:321
static av_cold void set_vbr(AVCodecContext *avctx)
Definition: nvenc.c:733
AVFrame * in_ref
Definition: nvenc.h:76
#define P1
Definition: cavsdsp.c:39
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
int64_t bit_rate
the average bitrate
Definition: avcodec.h:576
#define RC_MODE_DEPRECATED
Definition: nvenc.h:41
Memory handling functions.
static av_cold int nvenc_setup_device(AVCodecContext *avctx)
Definition: nvenc.c:535
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:499
static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2163
#define HW_CONFIG_ENCODER_FRAMES(format, device_type_)
Definition: hwconfig.h:99
const char * desc
Definition: nvenc.c:82
int max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: avcodec.h:454
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:786
int forced_idr
Definition: nvenc.h:199
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1161
int num
Numerator.
Definition: rational.h:59
Timecode which conforms to SMPTE ST 12-1.
Definition: frame.h:168
#define PRESET_ALIAS(alias, name,...)
Definition: nvenc.c:646
static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx)
Definition: nvenc.c:1481
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.h:91
NvencDynLoadFunctions nvenc_dload_funcs
Definition: nvenc.h:150
ID3D11Device * d3d11_device
Definition: nvenc.h:157
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:905
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:229
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:209
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: nvenc.c:2297
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx)
Definition: nvenc.c:840
static AVPacket pkt
int init_qp_b
Definition: nvenc.h:210
static void error(const char *err)
#define PRESET(name,...)
Definition: nvenc.c:649
int profile
profile
Definition: avcodec.h:1859
int preset
Definition: nvenc.h:186
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:838
static void nvenc_override_rate_control(AVCodecContext *avctx)
Definition: nvenc.c:809
static NvencSurface * get_free_frame(NvencContext *ctx)
Definition: nvenc.c:1679
static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx)
Definition: nvenc.c:879
int pitch
Definition: nvenc.h:80
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
Definition: fifo.c:122
int nvenc_device_count
Definition: nvenc.h:92
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:1910
NV_ENC_INPUT_PTR input_surface
Definition: nvenc.h:75
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:649
NVENCSTATUS nverr
Definition: nvenc.c:80
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:751
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:647
int aq
Definition: nvenc.h:197
#define AV_PIX_FMT_P016
Definition: pixfmt.h:449
int b_ref_mode
Definition: nvenc.h:215
#define AV_PIX_FMT_P010
Definition: pixfmt.h:448
CUcontext cu_context
Definition: nvenc.h:154
struct NvencContext::@116 registered_frames[MAX_REGISTERED_FRAMES]
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:1898
#define DEFAULT
Definition: avdct.c:28
uint8_t
AVFifoBuffer * unused_surface_queue
Definition: nvenc.h:164
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
enum AVPixelFormat ff_nvenc_pix_fmts[]
Definition: nvenc.c:46
static const struct @110 nvenc_errors[]
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:795
int init_qp_p
Definition: nvenc.h:209
static int nvenc_pop_context(AVCodecContext *avctx)
Definition: nvenc.c:252
#define FF_PROFILE_HEVC_MAIN
Definition: avcodec.h:1947
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:456
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:401
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
float quality
Definition: nvenc.h:206
NV_ENC_INITIALIZE_PARAMS init_encode_params
Definition: nvenc.h:152
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:92
static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
Definition: nvenc.c:1425
#define height
ID3D11Device * device
Device used for texture creation and access.
#define MAX_REGISTERED_FRAMES
Definition: nvenc.h:40
uint8_t * data
Definition: packet.h:363
static av_cold int nvenc_setup_extradata(AVCodecContext *avctx)
Definition: nvenc.c:1522
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
static int nvenc_check_capabilities(AVCodecContext *avctx)
Definition: nvenc.c:343
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
static int nvenc_register_frame(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:1755
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:481
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
#define HW_CONFIG_ENCODER_DEVICE(format, device_type_)
Definition: hwconfig.h:96
int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len, void **data, size_t *sei_size)
Check AVFrame for A53 side data and allocate and fill SEI message with A53 info.
Definition: atsc_a53.c:25
#define FF_PROFILE_HEVC_MAIN_10
Definition: avcodec.h:1948
AVFifoBuffer * output_surface_ready_queue
Definition: nvenc.h:166
#define av_log(a,...)
CUcontext cu_context_internal
Definition: nvenc.h:155
An API-specific header for AV_HWDEVICE_TYPE_CUDA.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:401
int ptr_index
Definition: nvenc.h:171
#define P3
int async_depth
Definition: nvenc.h:195
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:88
enum AVCodecID id
Definition: codec.h:204
static av_cold int nvenc_open_session(AVCodecContext *avctx)
Definition: nvenc.c:264
void * ptr
Definition: nvenc.h:170
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:816
int coder
Definition: nvenc.h:214
static void timestamp_queue_enqueue(AVFifoBuffer *queue, int64_t timestamp)
Definition: nvenc.c:1904
int rc
Definition: nvenc.h:190
int nb_registered_frames
Definition: nvenc.h:176
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
int qmax
maximum quantizer
Definition: avcodec.h:1375
static int nvenc_map_error(NVENCSTATUS err, const char **desc)
Definition: nvenc.c:112
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:58
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:213
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:1902
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:606
GLenum GLint * params
Definition: opengl_enc.c:113
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:377
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:831
const AVCodecHWConfigInternal * ff_nvenc_hw_configs[]
Definition: nvenc.c:62
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:79
static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
Definition: nvenc.c:1121
GLsizei count
Definition: opengl_enc.c:108
static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt)
Definition: nvenc.c:1402
#define FFMAX(a, b)
Definition: common.h:94
static av_cold void set_constqp(AVCodecContext *avctx)
Definition: nvenc.c:700
#define fail()
Definition: checkasm.h:123
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:422
int level
Definition: nvenc.h:188
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:369
int bluray_compat
Definition: nvenc.h:208
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1389
int aq_strength
Definition: nvenc.h:205
static int nvenc_check_codec_support(AVCodecContext *avctx)
Definition: nvenc.c:290
int refs
number of reference frames
Definition: avcodec.h:1114
int flags
Definition: nvenc.h:194
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:391
static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame, NvencSurface *nvenc_frame)
Definition: nvenc.c:1809
NV_ENC_REGISTERED_PTR regptr
Definition: nvenc.h:172
#define FFMIN(a, b)
Definition: common.h:96
static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
Definition: nvenc.c:1237
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:51
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:149
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:1632
#define width
Definition: af_afade.c:54
static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)
Definition: nvenc.c:1932
int width
picture width / height.
Definition: avcodec.h:699
int dpb_size
Definition: nvenc.h:218
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:2226
#define NVENC_CAP
Definition: nvenc.c:41
AVFormatContext * ctx
Definition: movenc.c:48
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1140
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
static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface, NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame)
Definition: nvenc.c:1691
#define IS_YUV444(pix_fmt)
Definition: nvenc.c:76
int dummy
Definition: motion.c:64
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:658
int profile
Definition: nvenc.h:187
AVFifoBuffer * output_surface_queue
Definition: nvenc.h:165
static void nvenc_print_driver_requirement(AVCodecContext *avctx, int level)
Definition: nvenc.c:147
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
HW acceleration through CUDA.
Definition: pixfmt.h:235
#define AV_CODEC_ID_H265
Definition: codec_id.h:224
static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx)
Definition: nvenc.c:1024
int tuning_info
Definition: nvenc.h:219
int draining
checks API usage: after codec draining, flush is required to resume operation
Definition: internal.h:174
#define FF_ARRAY_ELEMS(a)
Full range content.
Definition: pixfmt.h:586
if(ret)
CudaFunctions * cuda_dl
Definition: nvenc.h:88
enum AVPixelFormat data_pix_fmt
Definition: nvenc.h:180
#define IS_10BIT(pix_fmt)
Definition: nvenc.c:72
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:381
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:448
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
NV_ENC_CONFIG encode_config
Definition: nvenc.h:153
static int nvenc_push_context(AVCodecContext *avctx)
Definition: nvenc.c:241
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:146
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
Definition: fifo.c:77
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:339
int strict_gop
Definition: nvenc.h:204
int temporal_aq
Definition: nvenc.h:201
main external API structure.
Definition: avcodec.h:526
uint8_t * data
The data buffer.
Definition: buffer.h:89
int qmin
minimum quantizer
Definition: avcodec.h:1368
#define BD
int init_qp_i
Definition: nvenc.h:211
int extradata_size
Definition: avcodec.h:628
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
This struct is allocated as AVHWDeviceContext.hwctx.
static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
Definition: nvenc.c:326
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1154
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1147
int width
Definition: nvenc.h:78
int multipass
Definition: nvenc.h:220
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
#define IS_CBR(rc)
Definition: nvenc.c:42
AVPictureType
Definition: avutil.h:272
av_cold void ff_nvenc_encode_flush(AVCodecContext *avctx)
Definition: nvenc.c:2348
int flags
Definition: nvenc.c:643
int nonref_p
Definition: nvenc.h:203
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:808
int cbr
Definition: nvenc.h:191
static int nvenc_find_free_reg_resource(AVCodecContext *avctx)
Definition: nvenc.c:1721
int averr
Definition: nvenc.c:81
int s12m_tc
Definition: nvenc.h:217
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:566
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:592
#define flags(name, subs,...)
Definition: cbs_av1.c:560
static void compute_dar(AVCodecContext *avctx, int *dw, int *dh)
Definition: nvenc.c:1223
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
int reg_idx
Definition: nvenc.h:77
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:1871
uint8_t level
Definition: svq3.c:205
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:329
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:313
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:721
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:2212
int
int b_adapt
Definition: nvenc.h:200
static int64_t timestamp_queue_dequeue(AVFifoBuffer *queue)
Definition: nvenc.c:1909
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
int weighted_pred
Definition: nvenc.h:213
int rc_lookahead
Definition: nvenc.h:196
static int output_ready(AVCodecContext *avctx, int flush)
Definition: nvenc.c:2059
Bi-dir predicted.
Definition: avutil.h:276
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1776
NvencSurface * surfaces
Definition: nvenc.h:162
int den
Denominator.
Definition: rational.h:60
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
NV_ENC_MAP_INPUT_RESOURCE in_map
Definition: nvenc.h:174
AVCPBProperties * ff_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition: utils.c:1999
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2071
#define FF_PROFILE_HEVC_REXT
Definition: avcodec.h:1950
void * priv_data
Definition: avcodec.h:553
static int nvenc_set_timestamp(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *params, AVPacket *pkt)
Definition: nvenc.c:1918
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:663
#define av_free(p)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:460
static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
Definition: nvenc.c:197
static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
Definition: nvenc.c:460
int avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: avcodec.h:472
CUstream cu_stream
Definition: nvenc.h:156
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:561
#define P2
Definition: cavsdsp.c:38
int device
Definition: nvenc.h:193
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:362
This struct is allocated as AVHWDeviceContext.hwctx.
int nb_surfaces
Definition: nvenc.h:161
static int nvenc_print_error(AVCodecContext *avctx, NVENCSTATUS err, const char *error_string)
Definition: nvenc.c:127
int aud
Definition: nvenc.h:207
int cqp
Definition: nvenc.h:212
#define av_freep(p)
static av_cold void set_lossless(AVCodecContext *avctx)
Definition: nvenc.c:795
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
Definition: fifo.c:63
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:188
#define FFSWAP(type, a, b)
Definition: common.h:99
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:2489
int tier
Definition: nvenc.h:189
int ldkfs
Definition: nvenc.h:221
int a53_cc
Definition: nvenc.h:216
void av_fifo_reset(AVFifoBuffer *f)
Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied...
Definition: fifo.c:71
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
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:2278
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:222
AVFrame * frame
Definition: nvenc.h:159
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:340
NV_ENC_OUTPUT_PTR output_surface
Definition: nvenc.h:82
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356
for(j=16;j >0;--j)
int i
Definition: input.c:407
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
Predicted.
Definition: avutil.h:275
static void nvenc_map_preset(NvencContext *ctx)
Definition: nvenc.c:651
int zerolatency
Definition: nvenc.h:202
#define AV_PIX_FMT_0RGB32
Definition: pixfmt.h:376
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1404
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition: encode.c:107
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
const char * name
Definition: opengl_enc.c:102