FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
27 #include "libavutil/hwcontext.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/pixdesc.h"
32 #include "internal.h"
33 
34 #define NVENC_CAP 0x30
35 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
36  rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
37  rc == NV_ENC_PARAMS_RC_CBR_HQ)
38 
48 #if CONFIG_D3D11VA
50 #endif
52 };
53 
54 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
55  pix_fmt == AV_PIX_FMT_YUV444P16)
56 
57 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
58  pix_fmt == AV_PIX_FMT_YUV444P16)
59 
60 static const struct {
62  int averr;
63  const char *desc;
64 } nvenc_errors[] = {
65  { NV_ENC_SUCCESS, 0, "success" },
66  { NV_ENC_ERR_NO_ENCODE_DEVICE, AVERROR(ENOENT), "no encode device" },
67  { NV_ENC_ERR_UNSUPPORTED_DEVICE, AVERROR(ENOSYS), "unsupported device" },
68  { NV_ENC_ERR_INVALID_ENCODERDEVICE, AVERROR(EINVAL), "invalid encoder device" },
69  { NV_ENC_ERR_INVALID_DEVICE, AVERROR(EINVAL), "invalid device" },
70  { NV_ENC_ERR_DEVICE_NOT_EXIST, AVERROR(EIO), "device does not exist" },
71  { NV_ENC_ERR_INVALID_PTR, AVERROR(EFAULT), "invalid ptr" },
72  { NV_ENC_ERR_INVALID_EVENT, AVERROR(EINVAL), "invalid event" },
73  { NV_ENC_ERR_INVALID_PARAM, AVERROR(EINVAL), "invalid param" },
74  { NV_ENC_ERR_INVALID_CALL, AVERROR(EINVAL), "invalid call" },
75  { NV_ENC_ERR_OUT_OF_MEMORY, AVERROR(ENOMEM), "out of memory" },
76  { NV_ENC_ERR_ENCODER_NOT_INITIALIZED, AVERROR(EINVAL), "encoder not initialized" },
77  { NV_ENC_ERR_UNSUPPORTED_PARAM, AVERROR(ENOSYS), "unsupported param" },
78  { NV_ENC_ERR_LOCK_BUSY, AVERROR(EAGAIN), "lock busy" },
80  { NV_ENC_ERR_INVALID_VERSION, AVERROR(EINVAL), "invalid version" },
81  { NV_ENC_ERR_MAP_FAILED, AVERROR(EIO), "map failed" },
82  { NV_ENC_ERR_NEED_MORE_INPUT, AVERROR(EAGAIN), "need more input" },
83  { NV_ENC_ERR_ENCODER_BUSY, AVERROR(EAGAIN), "encoder busy" },
84  { NV_ENC_ERR_EVENT_NOT_REGISTERD, AVERROR(EBADF), "event not registered" },
85  { NV_ENC_ERR_GENERIC, AVERROR_UNKNOWN, "generic error" },
86  { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, AVERROR(EINVAL), "incompatible client key" },
87  { NV_ENC_ERR_UNIMPLEMENTED, AVERROR(ENOSYS), "unimplemented" },
88  { NV_ENC_ERR_RESOURCE_REGISTER_FAILED, AVERROR(EIO), "resource register failed" },
89  { NV_ENC_ERR_RESOURCE_NOT_REGISTERED, AVERROR(EBADF), "resource not registered" },
90  { NV_ENC_ERR_RESOURCE_NOT_MAPPED, AVERROR(EBADF), "resource not mapped" },
91 };
92 
93 static int nvenc_map_error(NVENCSTATUS err, const char **desc)
94 {
95  int i;
96  for (i = 0; i < FF_ARRAY_ELEMS(nvenc_errors); i++) {
97  if (nvenc_errors[i].nverr == err) {
98  if (desc)
99  *desc = nvenc_errors[i].desc;
100  return nvenc_errors[i].averr;
101  }
102  }
103  if (desc)
104  *desc = "unknown error";
105  return AVERROR_UNKNOWN;
106 }
107 
108 static int nvenc_print_error(void *log_ctx, NVENCSTATUS err,
109  const char *error_string)
110 {
111  const char *desc;
112  int ret;
113  ret = nvenc_map_error(err, &desc);
114  av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
115  return ret;
116 }
117 
119 {
120 #if defined(_WIN32) || defined(__CYGWIN__)
121  const char *minver = "378.66";
122 #else
123  const char *minver = "378.13";
124 #endif
125  av_log(avctx, level, "The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
126 }
127 
129 {
130  NvencContext *ctx = avctx->priv_data;
132  NVENCSTATUS err;
133  uint32_t nvenc_max_ver;
134  int ret;
135 
136  ret = cuda_load_functions(&dl_fn->cuda_dl, avctx);
137  if (ret < 0)
138  return ret;
139 
140  ret = nvenc_load_functions(&dl_fn->nvenc_dl, avctx);
141  if (ret < 0) {
143  return ret;
144  }
145 
146  err = dl_fn->nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
147  if (err != NV_ENC_SUCCESS)
148  return nvenc_print_error(avctx, err, "Failed to query nvenc max version");
149 
150  av_log(avctx, AV_LOG_VERBOSE, "Loaded Nvenc version %d.%d\n", nvenc_max_ver >> 4, nvenc_max_ver & 0xf);
151 
152  if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
153  av_log(avctx, AV_LOG_ERROR, "Driver does not support the required nvenc API version. "
154  "Required: %d.%d Found: %d.%d\n",
156  nvenc_max_ver >> 4, nvenc_max_ver & 0xf);
158  return AVERROR(ENOSYS);
159  }
160 
162 
163  err = dl_fn->nvenc_dl->NvEncodeAPICreateInstance(&dl_fn->nvenc_funcs);
164  if (err != NV_ENC_SUCCESS)
165  return nvenc_print_error(avctx, err, "Failed to create nvenc instance");
166 
167  av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
168 
169  return 0;
170 }
171 
173 {
174  NvencContext *ctx = avctx->priv_data;
176  CUresult cu_res;
177 
178  if (ctx->d3d11_device)
179  return 0;
180 
181  cu_res = dl_fn->cuda_dl->cuCtxPushCurrent(ctx->cu_context);
182  if (cu_res != CUDA_SUCCESS) {
183  av_log(avctx, AV_LOG_ERROR, "cuCtxPushCurrent failed\n");
184  return AVERROR_EXTERNAL;
185  }
186 
187  return 0;
188 }
189 
191 {
192  NvencContext *ctx = avctx->priv_data;
194  CUresult cu_res;
196 
197  if (ctx->d3d11_device)
198  return 0;
199 
200  cu_res = dl_fn->cuda_dl->cuCtxPopCurrent(&dummy);
201  if (cu_res != CUDA_SUCCESS) {
202  av_log(avctx, AV_LOG_ERROR, "cuCtxPopCurrent failed\n");
203  return AVERROR_EXTERNAL;
204  }
205 
206  return 0;
207 }
208 
210 {
212  NvencContext *ctx = avctx->priv_data;
214  NVENCSTATUS ret;
215 
217  params.apiVersion = NVENCAPI_VERSION;
218  if (ctx->d3d11_device) {
219  params.device = ctx->d3d11_device;
221  } else {
222  params.device = ctx->cu_context;
224  }
225 
226  ret = p_nvenc->nvEncOpenEncodeSessionEx(&params, &ctx->nvencoder);
227  if (ret != NV_ENC_SUCCESS) {
228  ctx->nvencoder = NULL;
229  return nvenc_print_error(avctx, ret, "OpenEncodeSessionEx failed");
230  }
231 
232  return 0;
233 }
234 
236 {
237  NvencContext *ctx = avctx->priv_data;
239  int i, ret, count = 0;
240  GUID *guids = NULL;
241 
242  ret = p_nvenc->nvEncGetEncodeGUIDCount(ctx->nvencoder, &count);
243 
244  if (ret != NV_ENC_SUCCESS || !count)
245  return AVERROR(ENOSYS);
246 
247  guids = av_malloc(count * sizeof(GUID));
248  if (!guids)
249  return AVERROR(ENOMEM);
250 
251  ret = p_nvenc->nvEncGetEncodeGUIDs(ctx->nvencoder, guids, count, &count);
252  if (ret != NV_ENC_SUCCESS) {
253  ret = AVERROR(ENOSYS);
254  goto fail;
255  }
256 
257  ret = AVERROR(ENOSYS);
258  for (i = 0; i < count; i++) {
259  if (!memcmp(&guids[i], &ctx->init_encode_params.encodeGUID, sizeof(*guids))) {
260  ret = 0;
261  break;
262  }
263  }
264 
265 fail:
266  av_free(guids);
267 
268  return ret;
269 }
270 
272 {
273  NvencContext *ctx = avctx->priv_data;
275  NV_ENC_CAPS_PARAM params = { 0 };
276  int ret, val = 0;
277 
279  params.capsToQuery = cap;
280 
281  ret = p_nvenc->nvEncGetEncodeCaps(ctx->nvencoder, ctx->init_encode_params.encodeGUID, &params, &val);
282 
283  if (ret == NV_ENC_SUCCESS)
284  return val;
285  return 0;
286 }
287 
289 {
290  NvencContext *ctx = avctx->priv_data;
291  int ret;
292 
293  ret = nvenc_check_codec_support(avctx);
294  if (ret < 0) {
295  av_log(avctx, AV_LOG_VERBOSE, "Codec not supported\n");
296  return ret;
297  }
298 
300  if (IS_YUV444(ctx->data_pix_fmt) && ret <= 0) {
301  av_log(avctx, AV_LOG_VERBOSE, "YUV444P not supported\n");
302  return AVERROR(ENOSYS);
303  }
304 
306  if (ctx->preset >= PRESET_LOSSLESS_DEFAULT && ret <= 0) {
307  av_log(avctx, AV_LOG_VERBOSE, "Lossless encoding not supported\n");
308  return AVERROR(ENOSYS);
309  }
310 
312  if (ret < avctx->width) {
313  av_log(avctx, AV_LOG_VERBOSE, "Width %d exceeds %d\n",
314  avctx->width, ret);
315  return AVERROR(ENOSYS);
316  }
317 
319  if (ret < avctx->height) {
320  av_log(avctx, AV_LOG_VERBOSE, "Height %d exceeds %d\n",
321  avctx->height, ret);
322  return AVERROR(ENOSYS);
323  }
324 
326  if (ret < avctx->max_b_frames) {
327  av_log(avctx, AV_LOG_VERBOSE, "Max B-frames %d exceed %d\n",
328  avctx->max_b_frames, ret);
329 
330  return AVERROR(ENOSYS);
331  }
332 
334  if (ret < 1 && avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
335  av_log(avctx, AV_LOG_VERBOSE,
336  "Interlaced encoding is not supported. Supported level: %d\n",
337  ret);
338  return AVERROR(ENOSYS);
339  }
340 
342  if (IS_10BIT(ctx->data_pix_fmt) && ret <= 0) {
343  av_log(avctx, AV_LOG_VERBOSE, "10 bit encode not supported\n");
344  return AVERROR(ENOSYS);
345  }
346 
348  if (ctx->rc_lookahead > 0 && ret <= 0) {
349  av_log(avctx, AV_LOG_VERBOSE, "RC lookahead not supported\n");
350  return AVERROR(ENOSYS);
351  }
352 
354  if (ctx->temporal_aq > 0 && ret <= 0) {
355  av_log(avctx, AV_LOG_VERBOSE, "Temporal AQ not supported\n");
356  return AVERROR(ENOSYS);
357  }
358 
360  if (ctx->weighted_pred > 0 && ret <= 0) {
361  av_log (avctx, AV_LOG_VERBOSE, "Weighted Prediction not supported\n");
362  return AVERROR(ENOSYS);
363  }
364 
366  if (ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC && ret <= 0) {
367  av_log(avctx, AV_LOG_VERBOSE, "CABAC entropy coding not supported\n");
368  return AVERROR(ENOSYS);
369  }
370 
371  return 0;
372 }
373 
374 static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
375 {
376  NvencContext *ctx = avctx->priv_data;
378  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
379  char name[128] = { 0};
380  int major, minor, ret;
381  CUresult cu_res;
382  CUdevice cu_device;
383  int loglevel = AV_LOG_VERBOSE;
384 
385  if (ctx->device == LIST_DEVICES)
386  loglevel = AV_LOG_INFO;
387 
388  cu_res = dl_fn->cuda_dl->cuDeviceGet(&cu_device, idx);
389  if (cu_res != CUDA_SUCCESS) {
390  av_log(avctx, AV_LOG_ERROR,
391  "Cannot access the CUDA device %d\n",
392  idx);
393  return -1;
394  }
395 
396  cu_res = dl_fn->cuda_dl->cuDeviceGetName(name, sizeof(name), cu_device);
397  if (cu_res != CUDA_SUCCESS) {
398  av_log(avctx, AV_LOG_ERROR, "cuDeviceGetName failed on device %d\n", idx);
399  return -1;
400  }
401 
402  cu_res = dl_fn->cuda_dl->cuDeviceComputeCapability(&major, &minor, cu_device);
403  if (cu_res != CUDA_SUCCESS) {
404  av_log(avctx, AV_LOG_ERROR, "cuDeviceComputeCapability failed on device %d\n", idx);
405  return -1;
406  }
407 
408  av_log(avctx, loglevel, "[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx, name, major, minor);
409  if (((major << 4) | minor) < NVENC_CAP) {
410  av_log(avctx, loglevel, "does not support NVENC\n");
411  goto fail;
412  }
413 
414  if (ctx->device != idx && ctx->device != ANY_DEVICE)
415  return -1;
416 
417  cu_res = dl_fn->cuda_dl->cuCtxCreate(&ctx->cu_context_internal, 0, cu_device);
418  if (cu_res != CUDA_SUCCESS) {
419  av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res);
420  goto fail;
421  }
422 
423  ctx->cu_context = ctx->cu_context_internal;
424 
425  if ((ret = nvenc_pop_context(avctx)) < 0)
426  goto fail2;
427 
428  if ((ret = nvenc_open_session(avctx)) < 0)
429  goto fail2;
430 
431  if ((ret = nvenc_check_capabilities(avctx)) < 0)
432  goto fail3;
433 
434  av_log(avctx, loglevel, "supports NVENC\n");
435 
436  dl_fn->nvenc_device_count++;
437 
438  if (ctx->device == idx || ctx->device == ANY_DEVICE)
439  return 0;
440 
441 fail3:
442  if ((ret = nvenc_push_context(avctx)) < 0)
443  return ret;
444 
445  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
446  ctx->nvencoder = NULL;
447 
448  if ((ret = nvenc_pop_context(avctx)) < 0)
449  return ret;
450 
451 fail2:
453  ctx->cu_context_internal = NULL;
454 
455 fail:
456  return AVERROR(ENOSYS);
457 }
458 
460 {
461  NvencContext *ctx = avctx->priv_data;
463 
464  switch (avctx->codec->id) {
465  case AV_CODEC_ID_H264:
467  break;
468  case AV_CODEC_ID_HEVC:
470  break;
471  default:
472  return AVERROR_BUG;
473  }
474 
475  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11 || avctx->hw_frames_ctx || avctx->hw_device_ctx) {
476  AVHWFramesContext *frames_ctx;
477  AVHWDeviceContext *hwdev_ctx;
478  AVCUDADeviceContext *cuda_device_hwctx = NULL;
479 #if CONFIG_D3D11VA
480  AVD3D11VADeviceContext *d3d11_device_hwctx = NULL;
481 #endif
482  int ret;
483 
484  if (avctx->hw_frames_ctx) {
485  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
486  if (frames_ctx->format == AV_PIX_FMT_CUDA)
487  cuda_device_hwctx = frames_ctx->device_ctx->hwctx;
488 #if CONFIG_D3D11VA
489  else if (frames_ctx->format == AV_PIX_FMT_D3D11)
490  d3d11_device_hwctx = frames_ctx->device_ctx->hwctx;
491 #endif
492  else
493  return AVERROR(EINVAL);
494  } else if (avctx->hw_device_ctx) {
495  hwdev_ctx = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
496  if (hwdev_ctx->type == AV_HWDEVICE_TYPE_CUDA)
497  cuda_device_hwctx = hwdev_ctx->hwctx;
498 #if CONFIG_D3D11VA
499  else if (hwdev_ctx->type == AV_HWDEVICE_TYPE_D3D11VA)
500  d3d11_device_hwctx = hwdev_ctx->hwctx;
501 #endif
502  else
503  return AVERROR(EINVAL);
504  } else {
505  return AVERROR(EINVAL);
506  }
507 
508  if (cuda_device_hwctx) {
509  ctx->cu_context = cuda_device_hwctx->cuda_ctx;
510  }
511 #if CONFIG_D3D11VA
512  else if (d3d11_device_hwctx) {
513  ctx->d3d11_device = d3d11_device_hwctx->device;
514  ID3D11Device_AddRef(ctx->d3d11_device);
515  }
516 #endif
517 
518  ret = nvenc_open_session(avctx);
519  if (ret < 0)
520  return ret;
521 
522  ret = nvenc_check_capabilities(avctx);
523  if (ret < 0) {
524  av_log(avctx, AV_LOG_FATAL, "Provided device doesn't support required NVENC features\n");
525  return ret;
526  }
527  } else {
528  int i, nb_devices = 0;
529 
530  if ((dl_fn->cuda_dl->cuInit(0)) != CUDA_SUCCESS) {
531  av_log(avctx, AV_LOG_ERROR,
532  "Cannot init CUDA\n");
533  return AVERROR_UNKNOWN;
534  }
535 
536  if ((dl_fn->cuda_dl->cuDeviceGetCount(&nb_devices)) != CUDA_SUCCESS) {
537  av_log(avctx, AV_LOG_ERROR,
538  "Cannot enumerate the CUDA devices\n");
539  return AVERROR_UNKNOWN;
540  }
541 
542  if (!nb_devices) {
543  av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
544  return AVERROR_EXTERNAL;
545  }
546 
547  av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", nb_devices);
548 
549  dl_fn->nvenc_device_count = 0;
550  for (i = 0; i < nb_devices; ++i) {
551  if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES)
552  return 0;
553  }
554 
555  if (ctx->device == LIST_DEVICES)
556  return AVERROR_EXIT;
557 
558  if (!dl_fn->nvenc_device_count) {
559  av_log(avctx, AV_LOG_FATAL, "No NVENC capable devices found\n");
560  return AVERROR_EXTERNAL;
561  }
562 
563  av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->device, nb_devices);
564  return AVERROR(EINVAL);
565  }
566 
567  return 0;
568 }
569 
570 typedef struct GUIDTuple {
571  const GUID guid;
572  int flags;
573 } GUIDTuple;
574 
575 #define PRESET_ALIAS(alias, name, ...) \
576  [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
577 
578 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
579 
581 {
582  GUIDTuple presets[] = {
583  PRESET(DEFAULT),
584  PRESET(HP),
585  PRESET(HQ),
586  PRESET(BD),
587  PRESET_ALIAS(SLOW, HQ, NVENC_TWO_PASSES),
588  PRESET_ALIAS(MEDIUM, HQ, NVENC_ONE_PASS),
589  PRESET_ALIAS(FAST, HP, NVENC_ONE_PASS),
590  PRESET(LOW_LATENCY_DEFAULT, NVENC_LOWLATENCY),
591  PRESET(LOW_LATENCY_HP, NVENC_LOWLATENCY),
592  PRESET(LOW_LATENCY_HQ, NVENC_LOWLATENCY),
593  PRESET(LOSSLESS_DEFAULT, NVENC_LOSSLESS),
594  PRESET(LOSSLESS_HP, NVENC_LOSSLESS),
595  };
596 
597  GUIDTuple *t = &presets[ctx->preset];
598 
600  ctx->flags = t->flags;
601 }
602 
603 #undef PRESET
604 #undef PRESET_ALIAS
605 
606 static av_cold void set_constqp(AVCodecContext *avctx)
607 {
608  NvencContext *ctx = avctx->priv_data;
610 
612 
613  if (ctx->init_qp_p >= 0) {
614  rc->constQP.qpInterP = ctx->init_qp_p;
615  if (ctx->init_qp_i >= 0 && ctx->init_qp_b >= 0) {
616  rc->constQP.qpIntra = ctx->init_qp_i;
617  rc->constQP.qpInterB = ctx->init_qp_b;
618  } else if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
619  rc->constQP.qpIntra = av_clip(
620  rc->constQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51);
621  rc->constQP.qpInterB = av_clip(
622  rc->constQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51);
623  } else {
624  rc->constQP.qpIntra = rc->constQP.qpInterP;
625  rc->constQP.qpInterB = rc->constQP.qpInterP;
626  }
627  } else if (ctx->cqp >= 0) {
628  rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra = ctx->cqp;
629  if (avctx->b_quant_factor != 0.0)
630  rc->constQP.qpInterB = av_clip(ctx->cqp * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51);
631  if (avctx->i_quant_factor != 0.0)
632  rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51);
633  }
634 
635  avctx->qmin = -1;
636  avctx->qmax = -1;
637 }
638 
639 static av_cold void set_vbr(AVCodecContext *avctx)
640 {
641  NvencContext *ctx = avctx->priv_data;
643  int qp_inter_p;
644 
645  if (avctx->qmin >= 0 && avctx->qmax >= 0) {
646  rc->enableMinQP = 1;
647  rc->enableMaxQP = 1;
648 
649  rc->minQP.qpInterB = avctx->qmin;
650  rc->minQP.qpInterP = avctx->qmin;
651  rc->minQP.qpIntra = avctx->qmin;
652 
653  rc->maxQP.qpInterB = avctx->qmax;
654  rc->maxQP.qpInterP = avctx->qmax;
655  rc->maxQP.qpIntra = avctx->qmax;
656 
657  qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin
658  } else if (avctx->qmin >= 0) {
659  rc->enableMinQP = 1;
660 
661  rc->minQP.qpInterB = avctx->qmin;
662  rc->minQP.qpInterP = avctx->qmin;
663  rc->minQP.qpIntra = avctx->qmin;
664 
665  qp_inter_p = avctx->qmin;
666  } else {
667  qp_inter_p = 26; // default to 26
668  }
669 
670  rc->enableInitialRCQP = 1;
671 
672  if (ctx->init_qp_p < 0) {
673  rc->initialRCQP.qpInterP = qp_inter_p;
674  } else {
675  rc->initialRCQP.qpInterP = ctx->init_qp_p;
676  }
677 
678  if (ctx->init_qp_i < 0) {
679  if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
680  rc->initialRCQP.qpIntra = av_clip(
681  rc->initialRCQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51);
682  } else {
684  }
685  } else {
686  rc->initialRCQP.qpIntra = ctx->init_qp_i;
687  }
688 
689  if (ctx->init_qp_b < 0) {
690  if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
691  rc->initialRCQP.qpInterB = av_clip(
692  rc->initialRCQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51);
693  } else {
695  }
696  } else {
697  rc->initialRCQP.qpInterB = ctx->init_qp_b;
698  }
699 }
700 
702 {
703  NvencContext *ctx = avctx->priv_data;
705 
707  rc->constQP.qpInterB = 0;
708  rc->constQP.qpInterP = 0;
709  rc->constQP.qpIntra = 0;
710 
711  avctx->qmin = -1;
712  avctx->qmax = -1;
713 }
714 
716 {
717  NvencContext *ctx = avctx->priv_data;
719 
720  switch (ctx->rc) {
722  set_constqp(avctx);
723  return;
725  if (avctx->qmin < 0) {
726  av_log(avctx, AV_LOG_WARNING,
727  "The variable bitrate rate-control requires "
728  "the 'qmin' option set.\n");
729  set_vbr(avctx);
730  return;
731  }
732  /* fall through */
735  set_vbr(avctx);
736  break;
740  break;
741  }
742 
743  rc->rateControlMode = ctx->rc;
744 }
745 
747 {
748  NvencContext *ctx = avctx->priv_data;
749  // default minimum of 4 surfaces
750  // multiply by 2 for number of NVENCs on gpu (hardcode to 2)
751  // another multiply by 2 to avoid blocking next PBB group
752  int nb_surfaces = FFMAX(4, ctx->encode_config.frameIntervalP * 2 * 2);
753 
754  // lookahead enabled
755  if (ctx->rc_lookahead > 0) {
756  // +1 is to account for lkd_bound calculation later
757  // +4 is to allow sufficient pipelining with lookahead
758  nb_surfaces = FFMAX(1, FFMAX(nb_surfaces, ctx->rc_lookahead + ctx->encode_config.frameIntervalP + 1 + 4));
759  if (nb_surfaces > ctx->nb_surfaces && ctx->nb_surfaces > 0)
760  {
761  av_log(avctx, AV_LOG_WARNING,
762  "Defined rc_lookahead requires more surfaces, "
763  "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces);
764  }
765  ctx->nb_surfaces = FFMAX(nb_surfaces, ctx->nb_surfaces);
766  } else {
767  if (ctx->encode_config.frameIntervalP > 1 && ctx->nb_surfaces < nb_surfaces && ctx->nb_surfaces > 0)
768  {
769  av_log(avctx, AV_LOG_WARNING,
770  "Defined b-frame requires more surfaces, "
771  "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces);
772  ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, nb_surfaces);
773  }
774  else if (ctx->nb_surfaces <= 0)
775  ctx->nb_surfaces = nb_surfaces;
776  // otherwise use user specified value
777  }
778 
780  ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1);
781 
782  return 0;
783 }
784 
786 {
787  NvencContext *ctx = avctx->priv_data;
788 
789  if (avctx->global_quality > 0)
790  av_log(avctx, AV_LOG_WARNING, "Using global_quality with nvenc is deprecated. Use qp instead.\n");
791 
792  if (ctx->cqp < 0 && avctx->global_quality > 0)
793  ctx->cqp = avctx->global_quality;
794 
795  if (avctx->bit_rate > 0) {
797  } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
799  }
800 
801  if (avctx->rc_max_rate > 0)
803 
804  if (ctx->rc < 0) {
805  if (ctx->flags & NVENC_ONE_PASS)
806  ctx->twopass = 0;
807  if (ctx->flags & NVENC_TWO_PASSES)
808  ctx->twopass = 1;
809 
810  if (ctx->twopass < 0)
811  ctx->twopass = (ctx->flags & NVENC_LOWLATENCY) != 0;
812 
813  if (ctx->cbr) {
814  if (ctx->twopass) {
816  } else {
817  ctx->rc = NV_ENC_PARAMS_RC_CBR;
818  }
819  } else if (ctx->cqp >= 0) {
821  } else if (ctx->twopass) {
823  } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
825  }
826  }
827 
828  if (ctx->rc >= 0 && ctx->rc & RC_MODE_DEPRECATED) {
829  av_log(avctx, AV_LOG_WARNING, "Specified rc mode is deprecated.\n");
830  av_log(avctx, AV_LOG_WARNING, "\tll_2pass_quality -> cbr_ld_hq\n");
831  av_log(avctx, AV_LOG_WARNING, "\tll_2pass_size -> cbr_hq\n");
832  av_log(avctx, AV_LOG_WARNING, "\tvbr_2pass -> vbr_hq\n");
833  av_log(avctx, AV_LOG_WARNING, "\tvbr_minqp -> (no replacement)\n");
834 
835  ctx->rc &= ~RC_MODE_DEPRECATED;
836  }
837 
838  if (ctx->flags & NVENC_LOSSLESS) {
839  set_lossless(avctx);
840  } else if (ctx->rc >= 0) {
842  } else {
844  set_vbr(avctx);
845  }
846 
847  if (avctx->rc_buffer_size > 0) {
849  } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
851  }
852 
853  if (ctx->aq) {
856  av_log(avctx, AV_LOG_VERBOSE, "AQ enabled.\n");
857  }
858 
859  if (ctx->temporal_aq) {
861  av_log(avctx, AV_LOG_VERBOSE, "Temporal AQ enabled.\n");
862  }
863 
864  if (ctx->rc_lookahead > 0) {
865  int lkd_bound = FFMIN(ctx->nb_surfaces, ctx->async_depth) -
866  ctx->encode_config.frameIntervalP - 4;
867 
868  if (lkd_bound < 0) {
869  av_log(avctx, AV_LOG_WARNING,
870  "Lookahead not enabled. Increase buffer delay (-delay).\n");
871  } else {
873  ctx->encode_config.rcParams.lookaheadDepth = av_clip(ctx->rc_lookahead, 0, lkd_bound);
876  av_log(avctx, AV_LOG_VERBOSE,
877  "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
879  ctx->encode_config.rcParams.disableIadapt ? "disabled" : "enabled",
880  ctx->encode_config.rcParams.disableBadapt ? "disabled" : "enabled");
881  }
882  }
883 
884  if (ctx->strict_gop) {
886  av_log(avctx, AV_LOG_VERBOSE, "Strict GOP target enabled.\n");
887  }
888 
889  if (ctx->nonref_p)
891 
892  if (ctx->zerolatency)
894 
895  if (ctx->quality)
896  {
897  //convert from float to fixed point 8.8
898  int tmp_quality = (int)(ctx->quality * 256.0f);
899  ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
900  ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
901  }
902 }
903 
905 {
906  NvencContext *ctx = avctx->priv_data;
907  NV_ENC_CONFIG *cc = &ctx->encode_config;
910 
911  vui->colourMatrix = avctx->colorspace;
912  vui->colourPrimaries = avctx->color_primaries;
913  vui->transferCharacteristics = avctx->color_trc;
916 
918  (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2);
919 
922  || vui->videoFormat != 5
923  || vui->videoFullRangeFlag != 0);
924 
925  h264->sliceMode = 3;
926  h264->sliceModeData = 1;
927 
928  h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
929  h264->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
930  h264->outputAUD = ctx->aud;
931 
932  if (avctx->refs >= 0) {
933  /* 0 means "let the hardware decide" */
934  h264->maxNumRefFrames = avctx->refs;
935  }
936  if (avctx->gop_size >= 0) {
937  h264->idrPeriod = cc->gopLength;
938  }
939 
940  if (IS_CBR(cc->rcParams.rateControlMode)) {
941  h264->outputBufferingPeriodSEI = 1;
942  }
943 
944  h264->outputPictureTimingSEI = 1;
945 
951  }
952 
953  if (ctx->flags & NVENC_LOSSLESS) {
955  } else {
956  switch(ctx->profile) {
960  break;
963  avctx->profile = FF_PROFILE_H264_MAIN;
964  break;
967  avctx->profile = FF_PROFILE_H264_HIGH;
968  break;
972  break;
973  }
974  }
975 
976  // force setting profile as high444p if input is AV_PIX_FMT_YUV444P
977  if (ctx->data_pix_fmt == AV_PIX_FMT_YUV444P) {
980  }
981 
983 
984  h264->level = ctx->level;
985 
986  if (ctx->coder >= 0)
987  h264->entropyCodingMode = ctx->coder;
988 
989  return 0;
990 }
991 
993 {
994  NvencContext *ctx = avctx->priv_data;
995  NV_ENC_CONFIG *cc = &ctx->encode_config;
998 
999  vui->colourMatrix = avctx->colorspace;
1000  vui->colourPrimaries = avctx->color_primaries;
1001  vui->transferCharacteristics = avctx->color_trc;
1004 
1006  (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2);
1007 
1010  || vui->videoFormat != 5
1011  || vui->videoFullRangeFlag != 0);
1012 
1013  hevc->sliceMode = 3;
1014  hevc->sliceModeData = 1;
1015 
1016  hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
1017  hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
1018  hevc->outputAUD = ctx->aud;
1019 
1020  if (avctx->refs >= 0) {
1021  /* 0 means "let the hardware decide" */
1022  hevc->maxNumRefFramesInDPB = avctx->refs;
1023  }
1024  if (avctx->gop_size >= 0) {
1025  hevc->idrPeriod = cc->gopLength;
1026  }
1027 
1028  if (IS_CBR(cc->rcParams.rateControlMode)) {
1029  hevc->outputBufferingPeriodSEI = 1;
1030  }
1031 
1032  hevc->outputPictureTimingSEI = 1;
1033 
1034  switch (ctx->profile) {
1037  avctx->profile = FF_PROFILE_HEVC_MAIN;
1038  break;
1042  break;
1045  avctx->profile = FF_PROFILE_HEVC_REXT;
1046  break;
1047  }
1048 
1049  // force setting profile as main10 if input is 10 bit
1050  if (IS_10BIT(ctx->data_pix_fmt)) {
1053  }
1054 
1055  // force setting profile as rext if input is yuv444
1056  if (IS_YUV444(ctx->data_pix_fmt)) {
1058  avctx->profile = FF_PROFILE_HEVC_REXT;
1059  }
1060 
1061  hevc->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1;
1062 
1063  hevc->pixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0;
1064 
1065  hevc->level = ctx->level;
1066 
1067  hevc->tier = ctx->tier;
1068 
1069  return 0;
1070 }
1071 
1073 {
1074  switch (avctx->codec->id) {
1075  case AV_CODEC_ID_H264:
1076  return nvenc_setup_h264_config(avctx);
1077  case AV_CODEC_ID_HEVC:
1078  return nvenc_setup_hevc_config(avctx);
1079  /* Earlier switch/case will return if unknown codec is passed. */
1080  }
1081 
1082  return 0;
1083 }
1084 
1086 {
1087  NvencContext *ctx = avctx->priv_data;
1089  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1090 
1091  NV_ENC_PRESET_CONFIG preset_config = { 0 };
1092  NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1093  AVCPBProperties *cpb_props;
1094  int res = 0;
1095  int dw, dh;
1096 
1099 
1100  ctx->init_encode_params.encodeHeight = avctx->height;
1101  ctx->init_encode_params.encodeWidth = avctx->width;
1102 
1104 
1105  nvenc_map_preset(ctx);
1106 
1107  preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1108  preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1109 
1110  nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder,
1113  &preset_config);
1114  if (nv_status != NV_ENC_SUCCESS)
1115  return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration");
1116 
1117  memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
1118 
1120 
1121  dw = avctx->width;
1122  dh = avctx->height;
1123  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
1124  dw*= avctx->sample_aspect_ratio.num;
1125  dh*= avctx->sample_aspect_ratio.den;
1126  }
1127  av_reduce(&dw, &dh, dw, dh, 1024 * 1024);
1128  ctx->init_encode_params.darHeight = dh;
1129  ctx->init_encode_params.darWidth = dw;
1130 
1133 
1135  ctx->init_encode_params.enablePTD = 1;
1136 
1137  if (ctx->weighted_pred == 1)
1139 
1140  if (ctx->bluray_compat) {
1141  ctx->aud = 1;
1142  avctx->refs = FFMIN(FFMAX(avctx->refs, 0), 6);
1143  avctx->max_b_frames = FFMIN(avctx->max_b_frames, 3);
1144  switch (avctx->codec->id) {
1145  case AV_CODEC_ID_H264:
1146  /* maximum level depends on used resolution */
1147  break;
1148  case AV_CODEC_ID_HEVC:
1149  ctx->level = NV_ENC_LEVEL_HEVC_51;
1150  ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1151  break;
1152  }
1153  }
1154 
1155  if (avctx->gop_size > 0) {
1156  if (avctx->max_b_frames >= 0) {
1157  /* 0 is intra-only, 1 is I/P only, 2 is one B-Frame, 3 two B-frames, and so on. */
1158  ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
1159  }
1160 
1161  ctx->encode_config.gopLength = avctx->gop_size;
1162  } else if (avctx->gop_size == 0) {
1163  ctx->encode_config.frameIntervalP = 0;
1164  ctx->encode_config.gopLength = 1;
1165  }
1166 
1167  ctx->initial_pts[0] = AV_NOPTS_VALUE;
1168  ctx->initial_pts[1] = AV_NOPTS_VALUE;
1169 
1170  nvenc_recalc_surfaces(avctx);
1171 
1172  nvenc_setup_rate_control(avctx);
1173 
1174  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
1176  } else {
1178  }
1179 
1180  res = nvenc_setup_codec_config(avctx);
1181  if (res)
1182  return res;
1183 
1184  res = nvenc_push_context(avctx);
1185  if (res < 0)
1186  return res;
1187 
1188  nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
1189 
1190  res = nvenc_pop_context(avctx);
1191  if (res < 0)
1192  return res;
1193 
1194  if (nv_status != NV_ENC_SUCCESS) {
1195  return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed");
1196  }
1197 
1198  if (ctx->encode_config.frameIntervalP > 1)
1199  avctx->has_b_frames = 2;
1200 
1201  if (ctx->encode_config.rcParams.averageBitRate > 0)
1203 
1204  cpb_props = ff_add_cpb_side_data(avctx);
1205  if (!cpb_props)
1206  return AVERROR(ENOMEM);
1207  cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate;
1208  cpb_props->avg_bitrate = avctx->bit_rate;
1209  cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize;
1210 
1211  return 0;
1212 }
1213 
1215 {
1216  switch (pix_fmt) {
1217  case AV_PIX_FMT_YUV420P:
1219  case AV_PIX_FMT_NV12:
1221  case AV_PIX_FMT_P010:
1223  case AV_PIX_FMT_YUV444P:
1225  case AV_PIX_FMT_YUV444P16:
1227  case AV_PIX_FMT_0RGB32:
1229  case AV_PIX_FMT_0BGR32:
1231  default:
1233  }
1234 }
1235 
1236 static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
1237 {
1238  NvencContext *ctx = avctx->priv_data;
1240  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1241  NvencSurface* tmp_surface = &ctx->surfaces[idx];
1242 
1243  NVENCSTATUS nv_status;
1244  NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1246 
1247  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1248  ctx->surfaces[idx].in_ref = av_frame_alloc();
1249  if (!ctx->surfaces[idx].in_ref)
1250  return AVERROR(ENOMEM);
1251  } else {
1252  NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1253 
1255  if (ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1256  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
1258  return AVERROR(EINVAL);
1259  }
1260 
1262  allocSurf.width = avctx->width;
1263  allocSurf.height = avctx->height;
1264  allocSurf.bufferFmt = ctx->surfaces[idx].format;
1265 
1266  nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
1267  if (nv_status != NV_ENC_SUCCESS) {
1268  return nvenc_print_error(avctx, nv_status, "CreateInputBuffer failed");
1269  }
1270 
1271  ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1272  ctx->surfaces[idx].width = allocSurf.width;
1273  ctx->surfaces[idx].height = allocSurf.height;
1274  }
1275 
1276  nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
1277  if (nv_status != NV_ENC_SUCCESS) {
1278  int err = nvenc_print_error(avctx, nv_status, "CreateBitstreamBuffer failed");
1279  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1280  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[idx].input_surface);
1281  av_frame_free(&ctx->surfaces[idx].in_ref);
1282  return err;
1283  }
1284 
1285  ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1286  ctx->surfaces[idx].size = allocOut.size;
1287 
1288  av_fifo_generic_write(ctx->unused_surface_queue, &tmp_surface, sizeof(tmp_surface), NULL);
1289 
1290  return 0;
1291 }
1292 
1294 {
1295  NvencContext *ctx = avctx->priv_data;
1296  int i, res = 0, res2;
1297 
1298  ctx->surfaces = av_mallocz_array(ctx->nb_surfaces, sizeof(*ctx->surfaces));
1299  if (!ctx->surfaces)
1300  return AVERROR(ENOMEM);
1301 
1302  ctx->timestamp_list = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t));
1303  if (!ctx->timestamp_list)
1304  return AVERROR(ENOMEM);
1305 
1307  if (!ctx->unused_surface_queue)
1308  return AVERROR(ENOMEM);
1309 
1311  if (!ctx->output_surface_queue)
1312  return AVERROR(ENOMEM);
1314  if (!ctx->output_surface_ready_queue)
1315  return AVERROR(ENOMEM);
1316 
1317  res = nvenc_push_context(avctx);
1318  if (res < 0)
1319  return res;
1320 
1321  for (i = 0; i < ctx->nb_surfaces; i++) {
1322  if ((res = nvenc_alloc_surface(avctx, i)) < 0)
1323  goto fail;
1324  }
1325 
1326 fail:
1327  res2 = nvenc_pop_context(avctx);
1328  if (res2 < 0)
1329  return res2;
1330 
1331  return res;
1332 }
1333 
1335 {
1336  NvencContext *ctx = avctx->priv_data;
1338  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1339 
1340  NVENCSTATUS nv_status;
1341  uint32_t outSize = 0;
1342  char tmpHeader[256];
1343  NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1345 
1346  payload.spsppsBuffer = tmpHeader;
1347  payload.inBufferSize = sizeof(tmpHeader);
1348  payload.outSPSPPSPayloadSize = &outSize;
1349 
1350  nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
1351  if (nv_status != NV_ENC_SUCCESS) {
1352  return nvenc_print_error(avctx, nv_status, "GetSequenceParams failed");
1353  }
1354 
1355  avctx->extradata_size = outSize;
1357 
1358  if (!avctx->extradata) {
1359  return AVERROR(ENOMEM);
1360  }
1361 
1362  memcpy(avctx->extradata, tmpHeader, outSize);
1363 
1364  return 0;
1365 }
1366 
1368 {
1369  NvencContext *ctx = avctx->priv_data;
1371  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1372  int i, res;
1373 
1374  /* the encoder has to be flushed before it can be closed */
1375  if (ctx->nvencoder) {
1377  .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1378 
1379  res = nvenc_push_context(avctx);
1380  if (res < 0)
1381  return res;
1382 
1383  p_nvenc->nvEncEncodePicture(ctx->nvencoder, &params);
1384  }
1385 
1390 
1391  if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) {
1392  for (i = 0; i < ctx->nb_surfaces; ++i) {
1393  if (ctx->surfaces[i].input_surface) {
1395  }
1396  }
1397  for (i = 0; i < ctx->nb_registered_frames; i++) {
1398  if (ctx->registered_frames[i].regptr)
1400  }
1401  ctx->nb_registered_frames = 0;
1402  }
1403 
1404  if (ctx->surfaces) {
1405  for (i = 0; i < ctx->nb_surfaces; ++i) {
1406  if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11)
1407  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[i].input_surface);
1408  av_frame_free(&ctx->surfaces[i].in_ref);
1410  }
1411  }
1412  av_freep(&ctx->surfaces);
1413  ctx->nb_surfaces = 0;
1414 
1415  if (ctx->nvencoder) {
1416  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
1417 
1418  res = nvenc_pop_context(avctx);
1419  if (res < 0)
1420  return res;
1421  }
1422  ctx->nvencoder = NULL;
1423 
1424  if (ctx->cu_context_internal)
1426  ctx->cu_context = ctx->cu_context_internal = NULL;
1427 
1428 #if CONFIG_D3D11VA
1429  if (ctx->d3d11_device) {
1430  ID3D11Device_Release(ctx->d3d11_device);
1431  ctx->d3d11_device = NULL;
1432  }
1433 #endif
1434 
1435  nvenc_free_functions(&dl_fn->nvenc_dl);
1436  cuda_free_functions(&dl_fn->cuda_dl);
1437 
1438  dl_fn->nvenc_device_count = 0;
1439 
1440  av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
1441 
1442  return 0;
1443 }
1444 
1446 {
1447  NvencContext *ctx = avctx->priv_data;
1448  int ret;
1449 
1450  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1451  AVHWFramesContext *frames_ctx;
1452  if (!avctx->hw_frames_ctx) {
1453  av_log(avctx, AV_LOG_ERROR,
1454  "hw_frames_ctx must be set when using GPU frames as input\n");
1455  return AVERROR(EINVAL);
1456  }
1457  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1458  if (frames_ctx->format != avctx->pix_fmt) {
1459  av_log(avctx, AV_LOG_ERROR,
1460  "hw_frames_ctx must match the GPU frame type\n");
1461  return AVERROR(EINVAL);
1462  }
1463  ctx->data_pix_fmt = frames_ctx->sw_format;
1464  } else {
1465  ctx->data_pix_fmt = avctx->pix_fmt;
1466  }
1467 
1468  if ((ret = nvenc_load_libraries(avctx)) < 0)
1469  return ret;
1470 
1471  if ((ret = nvenc_setup_device(avctx)) < 0)
1472  return ret;
1473 
1474  if ((ret = nvenc_setup_encoder(avctx)) < 0)
1475  return ret;
1476 
1477  if ((ret = nvenc_setup_surfaces(avctx)) < 0)
1478  return ret;
1479 
1480  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1481  if ((ret = nvenc_setup_extradata(avctx)) < 0)
1482  return ret;
1483  }
1484 
1485  return 0;
1486 }
1487 
1489 {
1490  NvencSurface *tmp_surf;
1491 
1492  if (!(av_fifo_size(ctx->unused_surface_queue) > 0))
1493  // queue empty
1494  return NULL;
1495 
1496  av_fifo_generic_read(ctx->unused_surface_queue, &tmp_surf, sizeof(tmp_surf), NULL);
1497  return tmp_surf;
1498 }
1499 
1500 static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface,
1501  NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame)
1502 {
1503  int dst_linesize[4] = {
1504  lock_buffer_params->pitch,
1505  lock_buffer_params->pitch,
1506  lock_buffer_params->pitch,
1507  lock_buffer_params->pitch
1508  };
1509  uint8_t *dst_data[4];
1510  int ret;
1511 
1512  if (frame->format == AV_PIX_FMT_YUV420P)
1513  dst_linesize[1] = dst_linesize[2] >>= 1;
1514 
1515  ret = av_image_fill_pointers(dst_data, frame->format, nv_surface->height,
1516  lock_buffer_params->bufferDataPtr, dst_linesize);
1517  if (ret < 0)
1518  return ret;
1519 
1520  if (frame->format == AV_PIX_FMT_YUV420P)
1521  FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
1522 
1523  av_image_copy(dst_data, dst_linesize,
1524  (const uint8_t**)frame->data, frame->linesize, frame->format,
1525  avctx->width, avctx->height);
1526 
1527  return 0;
1528 }
1529 
1531 {
1532  NvencContext *ctx = avctx->priv_data;
1534  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1535 
1536  int i;
1537 
1539  for (i = 0; i < ctx->nb_registered_frames; i++) {
1540  if (!ctx->registered_frames[i].mapped) {
1541  if (ctx->registered_frames[i].regptr) {
1542  p_nvenc->nvEncUnregisterResource(ctx->nvencoder,
1543  ctx->registered_frames[i].regptr);
1544  ctx->registered_frames[i].regptr = NULL;
1545  }
1546  return i;
1547  }
1548  }
1549  } else {
1550  return ctx->nb_registered_frames++;
1551  }
1552 
1553  av_log(avctx, AV_LOG_ERROR, "Too many registered CUDA frames\n");
1554  return AVERROR(ENOMEM);
1555 }
1556 
1558 {
1559  NvencContext *ctx = avctx->priv_data;
1561  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1562 
1563  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frame->hw_frames_ctx->data;
1565  int i, idx, ret;
1566 
1567  for (i = 0; i < ctx->nb_registered_frames; i++) {
1568  if (avctx->pix_fmt == AV_PIX_FMT_CUDA && ctx->registered_frames[i].ptr == frame->data[0])
1569  return i;
1570  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])
1571  return i;
1572  }
1573 
1574  idx = nvenc_find_free_reg_resource(avctx);
1575  if (idx < 0)
1576  return idx;
1577 
1578  reg.version = NV_ENC_REGISTER_RESOURCE_VER;
1579  reg.width = frames_ctx->width;
1580  reg.height = frames_ctx->height;
1581  reg.pitch = frame->linesize[0];
1582  reg.resourceToRegister = frame->data[0];
1583 
1584  if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
1585  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
1586  }
1587  else if (avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1588  reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
1589  reg.subResourceIndex = (intptr_t)frame->data[1];
1590  }
1591 
1592  reg.bufferFormat = nvenc_map_buffer_format(frames_ctx->sw_format);
1593  if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1594  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n",
1595  av_get_pix_fmt_name(frames_ctx->sw_format));
1596  return AVERROR(EINVAL);
1597  }
1598 
1599  ret = p_nvenc->nvEncRegisterResource(ctx->nvencoder, &reg);
1600  if (ret != NV_ENC_SUCCESS) {
1601  nvenc_print_error(avctx, ret, "Error registering an input resource");
1602  return AVERROR_UNKNOWN;
1603  }
1604 
1605  ctx->registered_frames[idx].ptr = frame->data[0];
1606  ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
1607  ctx->registered_frames[idx].regptr = reg.registeredResource;
1608  return idx;
1609 }
1610 
1612  NvencSurface *nvenc_frame)
1613 {
1614  NvencContext *ctx = avctx->priv_data;
1616  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1617 
1618  int res;
1619  NVENCSTATUS nv_status;
1620 
1621  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1622  int reg_idx = nvenc_register_frame(avctx, frame);
1623  if (reg_idx < 0) {
1624  av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n");
1625  return reg_idx;
1626  }
1627 
1628  res = av_frame_ref(nvenc_frame->in_ref, frame);
1629  if (res < 0)
1630  return res;
1631 
1633  nvenc_frame->in_map.registeredResource = ctx->registered_frames[reg_idx].regptr;
1634  nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &nvenc_frame->in_map);
1635  if (nv_status != NV_ENC_SUCCESS) {
1636  av_frame_unref(nvenc_frame->in_ref);
1637  return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");
1638  }
1639 
1640  ctx->registered_frames[reg_idx].mapped = 1;
1641  nvenc_frame->reg_idx = reg_idx;
1642  nvenc_frame->input_surface = nvenc_frame->in_map.mappedResource;
1643  nvenc_frame->format = nvenc_frame->in_map.mappedBufferFmt;
1644  nvenc_frame->pitch = frame->linesize[0];
1645  return 0;
1646  } else {
1647  NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1648 
1649  lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1650  lockBufferParams.inputBuffer = nvenc_frame->input_surface;
1651 
1652  nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
1653  if (nv_status != NV_ENC_SUCCESS) {
1654  return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer");
1655  }
1656 
1657  nvenc_frame->pitch = lockBufferParams.pitch;
1658  res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame);
1659 
1660  nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, nvenc_frame->input_surface);
1661  if (nv_status != NV_ENC_SUCCESS) {
1662  return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!");
1663  }
1664 
1665  return res;
1666  }
1667 }
1668 
1671 {
1672  NvencContext *ctx = avctx->priv_data;
1673 
1674  switch (avctx->codec->id) {
1675  case AV_CODEC_ID_H264:
1680  break;
1681  case AV_CODEC_ID_HEVC:
1686  break;
1687  }
1688 }
1689 
1690 static inline void timestamp_queue_enqueue(AVFifoBuffer* queue, int64_t timestamp)
1691 {
1692  av_fifo_generic_write(queue, &timestamp, sizeof(timestamp), NULL);
1693 }
1694 
1695 static inline int64_t timestamp_queue_dequeue(AVFifoBuffer* queue)
1696 {
1697  int64_t timestamp = AV_NOPTS_VALUE;
1698  if (av_fifo_size(queue) > 0)
1699  av_fifo_generic_read(queue, &timestamp, sizeof(timestamp), NULL);
1700 
1701  return timestamp;
1702 }
1703 
1706  AVPacket *pkt)
1707 {
1708  NvencContext *ctx = avctx->priv_data;
1709 
1710  pkt->pts = params->outputTimeStamp;
1711 
1712  /* generate the first dts by linearly extrapolating the
1713  * first two pts values to the past */
1714  if (avctx->max_b_frames > 0 && !ctx->first_packet_output &&
1715  ctx->initial_pts[1] != AV_NOPTS_VALUE) {
1716  int64_t ts0 = ctx->initial_pts[0], ts1 = ctx->initial_pts[1];
1717  int64_t delta;
1718 
1719  if ((ts0 < 0 && ts1 > INT64_MAX + ts0) ||
1720  (ts0 > 0 && ts1 < INT64_MIN + ts0))
1721  return AVERROR(ERANGE);
1722  delta = ts1 - ts0;
1723 
1724  if ((delta < 0 && ts0 > INT64_MAX + delta) ||
1725  (delta > 0 && ts0 < INT64_MIN + delta))
1726  return AVERROR(ERANGE);
1727  pkt->dts = ts0 - delta;
1728 
1729  ctx->first_packet_output = 1;
1730  return 0;
1731  }
1732 
1734 
1735  return 0;
1736 }
1737 
1739 {
1740  NvencContext *ctx = avctx->priv_data;
1742  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1743 
1744  uint32_t slice_mode_data;
1745  uint32_t *slice_offsets = NULL;
1746  NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
1747  NVENCSTATUS nv_status;
1748  int res = 0;
1749 
1750  enum AVPictureType pict_type;
1751 
1752  switch (avctx->codec->id) {
1753  case AV_CODEC_ID_H264:
1755  break;
1756  case AV_CODEC_ID_H265:
1758  break;
1759  default:
1760  av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
1761  res = AVERROR(EINVAL);
1762  goto error;
1763  }
1764  slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));
1765 
1766  if (!slice_offsets) {
1767  res = AVERROR(ENOMEM);
1768  goto error;
1769  }
1770 
1771  lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
1772 
1773  lock_params.doNotWait = 0;
1774  lock_params.outputBitstream = tmpoutsurf->output_surface;
1775  lock_params.sliceOffsets = slice_offsets;
1776 
1777  nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
1778  if (nv_status != NV_ENC_SUCCESS) {
1779  res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer");
1780  goto error;
1781  }
1782 
1783  if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes,0)) {
1784  p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1785  goto error;
1786  }
1787 
1788  memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
1789 
1790  nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1791  if (nv_status != NV_ENC_SUCCESS)
1792  nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");
1793 
1794 
1795  if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) {
1796  p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, tmpoutsurf->in_map.mappedResource);
1797  av_frame_unref(tmpoutsurf->in_ref);
1798  ctx->registered_frames[tmpoutsurf->reg_idx].mapped = 0;
1799 
1800  tmpoutsurf->input_surface = NULL;
1801  }
1802 
1803  switch (lock_params.pictureType) {
1804  case NV_ENC_PIC_TYPE_IDR:
1805  pkt->flags |= AV_PKT_FLAG_KEY;
1806  case NV_ENC_PIC_TYPE_I:
1807  pict_type = AV_PICTURE_TYPE_I;
1808  break;
1809  case NV_ENC_PIC_TYPE_P:
1810  pict_type = AV_PICTURE_TYPE_P;
1811  break;
1812  case NV_ENC_PIC_TYPE_B:
1813  pict_type = AV_PICTURE_TYPE_B;
1814  break;
1815  case NV_ENC_PIC_TYPE_BI:
1816  pict_type = AV_PICTURE_TYPE_BI;
1817  break;
1818  default:
1819  av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
1820  av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
1821  res = AVERROR_EXTERNAL;
1822  goto error;
1823  }
1824 
1825 #if FF_API_CODED_FRAME
1827  avctx->coded_frame->pict_type = pict_type;
1829 #endif
1830 
1832  (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type);
1833 
1834  res = nvenc_set_timestamp(avctx, &lock_params, pkt);
1835  if (res < 0)
1836  goto error2;
1837 
1838  av_free(slice_offsets);
1839 
1840  return 0;
1841 
1842 error:
1844 
1845 error2:
1846  av_free(slice_offsets);
1847 
1848  return res;
1849 }
1850 
1851 static int output_ready(AVCodecContext *avctx, int flush)
1852 {
1853  NvencContext *ctx = avctx->priv_data;
1854  int nb_ready, nb_pending;
1855 
1856  /* when B-frames are enabled, we wait for two initial timestamps to
1857  * calculate the first dts */
1858  if (!flush && avctx->max_b_frames > 0 &&
1859  (ctx->initial_pts[0] == AV_NOPTS_VALUE || ctx->initial_pts[1] == AV_NOPTS_VALUE))
1860  return 0;
1861 
1862  nb_ready = av_fifo_size(ctx->output_surface_ready_queue) / sizeof(NvencSurface*);
1863  nb_pending = av_fifo_size(ctx->output_surface_queue) / sizeof(NvencSurface*);
1864  if (flush)
1865  return nb_ready > 0;
1866  return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth);
1867 }
1868 
1870 {
1871  NVENCSTATUS nv_status;
1872  NvencSurface *tmp_out_surf, *in_surf;
1873  int res, res2;
1874 
1875  NvencContext *ctx = avctx->priv_data;
1877  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1878 
1879  NV_ENC_PIC_PARAMS pic_params = { 0 };
1880  pic_params.version = NV_ENC_PIC_PARAMS_VER;
1881 
1882  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
1883  return AVERROR(EINVAL);
1884 
1885  if (ctx->encoder_flushing)
1886  return AVERROR_EOF;
1887 
1888  if (frame) {
1889  in_surf = get_free_frame(ctx);
1890  if (!in_surf)
1891  return AVERROR(EAGAIN);
1892 
1893  res = nvenc_push_context(avctx);
1894  if (res < 0)
1895  return res;
1896 
1897  res = nvenc_upload_frame(avctx, frame, in_surf);
1898 
1899  res2 = nvenc_pop_context(avctx);
1900  if (res2 < 0)
1901  return res2;
1902 
1903  if (res)
1904  return res;
1905 
1906  pic_params.inputBuffer = in_surf->input_surface;
1907  pic_params.bufferFmt = in_surf->format;
1908  pic_params.inputWidth = in_surf->width;
1909  pic_params.inputHeight = in_surf->height;
1910  pic_params.inputPitch = in_surf->pitch;
1911  pic_params.outputBitstream = in_surf->output_surface;
1912 
1913  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
1914  if (frame->top_field_first)
1916  else
1918  } else {
1920  }
1921 
1922  if (ctx->forced_idr >= 0 && frame->pict_type == AV_PICTURE_TYPE_I) {
1923  pic_params.encodePicFlags =
1925  } else {
1926  pic_params.encodePicFlags = 0;
1927  }
1928 
1929  pic_params.inputTimeStamp = frame->pts;
1930 
1931  nvenc_codec_specific_pic_params(avctx, &pic_params);
1932  } else {
1933  pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
1934  ctx->encoder_flushing = 1;
1935  }
1936 
1937  res = nvenc_push_context(avctx);
1938  if (res < 0)
1939  return res;
1940 
1941  nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
1942 
1943  res = nvenc_pop_context(avctx);
1944  if (res < 0)
1945  return res;
1946 
1947  if (nv_status != NV_ENC_SUCCESS &&
1948  nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
1949  return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
1950 
1951  if (frame) {
1952  av_fifo_generic_write(ctx->output_surface_queue, &in_surf, sizeof(in_surf), NULL);
1954 
1955  if (ctx->initial_pts[0] == AV_NOPTS_VALUE)
1956  ctx->initial_pts[0] = frame->pts;
1957  else if (ctx->initial_pts[1] == AV_NOPTS_VALUE)
1958  ctx->initial_pts[1] = frame->pts;
1959  }
1960 
1961  /* all the pending buffers are now ready for output */
1962  if (nv_status == NV_ENC_SUCCESS) {
1963  while (av_fifo_size(ctx->output_surface_queue) > 0) {
1964  av_fifo_generic_read(ctx->output_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
1965  av_fifo_generic_write(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
1966  }
1967  }
1968 
1969  return 0;
1970 }
1971 
1973 {
1974  NvencSurface *tmp_out_surf;
1975  int res, res2;
1976 
1977  NvencContext *ctx = avctx->priv_data;
1978 
1979  if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder)
1980  return AVERROR(EINVAL);
1981 
1982  if (output_ready(avctx, ctx->encoder_flushing)) {
1983  av_fifo_generic_read(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
1984 
1985  res = nvenc_push_context(avctx);
1986  if (res < 0)
1987  return res;
1988 
1989  res = process_output_surface(avctx, pkt, tmp_out_surf);
1990 
1991  res2 = nvenc_pop_context(avctx);
1992  if (res2 < 0)
1993  return res2;
1994 
1995  if (res)
1996  return res;
1997 
1998  av_fifo_generic_write(ctx->unused_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
1999  } else if (ctx->encoder_flushing) {
2000  return AVERROR_EOF;
2001  } else {
2002  return AVERROR(EAGAIN);
2003  }
2004 
2005  return 0;
2006 }
2007 
2009  const AVFrame *frame, int *got_packet)
2010 {
2011  NvencContext *ctx = avctx->priv_data;
2012  int res;
2013 
2014  if (!ctx->encoder_flushing) {
2015  res = ff_nvenc_send_frame(avctx, frame);
2016  if (res < 0)
2017  return res;
2018  }
2019 
2020  res = ff_nvenc_receive_packet(avctx, pkt);
2021  if (res == AVERROR(EAGAIN) || res == AVERROR_EOF) {
2022  *got_packet = 0;
2023  } else if (res < 0) {
2024  return res;
2025  } else {
2026  *got_packet = 1;
2027  }
2028 
2029  return 0;
2030 }
const GUID guid
Definition: nvenc.c:571
#define FF_PROFILE_H264_MAIN
Definition: avcodec.h:2868
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1369
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
int no_scenecut
Definition: nvenc.h:160
PNVENCGETENCODEGUIDS nvEncGetEncodeGUIDs
[out]: Client should access NvEncGetEncodeGUIDs() API through this pointer.
Definition: nvEncodeAPI.h:3262
uint32_t idrPeriod
[in]: Specifies the IDR interval.
Definition: nvEncodeAPI.h:1219
Progressive frame.
Definition: nvEncodeAPI.h:276
#define NULL
Definition: coverity.c:32
tcuDeviceGetName * cuDeviceGetName
const struct AVCodec * codec
Definition: avcodec.h:1511
const char const char void * val
Definition: avisynth_c.h:771
PNVENCCREATEBITSTREAMBUFFER nvEncCreateBitstreamBuffer
[out]: Client should access NvEncCreateBitstreamBuffer() API through this pointer.
Definition: nvEncodeAPI.h:3272
BI type.
Definition: avutil.h:280
void * nvencoder
Definition: nvenc.h:146
av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:1367
int twopass
Definition: nvenc.h:154
static enum AVPixelFormat pix_fmt
uint32_t vbvBufferSize
[in]: Specifies the VBV(HRD) buffer size.
Definition: nvEncodeAPI.h:1097
Field encoding bottom field first.
Definition: nvEncodeAPI.h:278
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.h:54
NV_ENC_QP constQP
[in]: Specifies the initial QP to be used for encoding, these values would be used for all frames if ...
Definition: nvEncodeAPI.h:1094
uint32_t sliceModeData
[in]: Specifies the parameter needed for sliceMode.
Definition: nvEncodeAPI.h:1508
int height
Definition: nvenc.h:50
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx)
Definition: nvenc.c:1072
AVFifoBuffer * timestamp_list
Definition: nvenc.h:125
uint32_t encodeWidth
[in]: Specifies the encode width.
Definition: nvEncodeAPI.h:1397
uint32_t enableTemporalAQ
[in]: Set this to 1 to enable temporal AQ for H.264
Definition: nvEncodeAPI.h:1107
uint32_t pitch
[out]: Pitch of the locked input buffer.
Definition: nvEncodeAPI.h:1694
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:669
static void flush(AVCodecContext *avctx)
This indicates that the HW encoder is busy encoding and is unable to encode the input.
Definition: nvEncodeAPI.h:520
static const GUID NV_ENC_H264_PROFILE_HIGH_444_GUID
Definition: nvEncodeAPI.h:166
NvencFunctions * nvenc_dl
Definition: nvenc.h:61
static const GUID NV_ENC_HEVC_PROFILE_MAIN10_GUID
Definition: nvEncodeAPI.h:190
int mapped
Definition: nvenc.h:133
NV_ENC_REGISTERED_PTR registeredResource
[in]: The Registered resource handle obtained by calling NvEncRegisterInputResource.
Definition: nvEncodeAPI.h:1712
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:871
static av_cold void set_vbr(AVCodecContext *avctx)
Definition: nvenc.c:639
Indicates end of the input stream.
Definition: nvEncodeAPI.h:574
AVFrame * in_ref
Definition: nvenc.h:46
This indicates that the NvEncRegisterResource API failed to register the resource.
Definition: nvEncodeAPI.h:548
GUID presetGUID
[in]: Specifies the preset for encoding.
Definition: nvEncodeAPI.h:1396
void * device
[in]: Pointer to client device.
Definition: nvEncodeAPI.h:1823
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
Indicates HW support for Weighted Predicition.
Definition: nvEncodeAPI.h:956
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1552
#define RC_MODE_DEPRECATED
Definition: nvenc.h:40
Memory handling functions.
#define NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER
Macro for constructing the version field of _NV_ENC_SEQUENCE_PARAM_PAYLOAD.
Definition: nvEncodeAPI.h:1798
static av_cold int nvenc_setup_device(AVCodecContext *avctx)
Definition: nvenc.c:459
const char * desc
Definition: nvenc.c:63
void * outputBitstream
[in]: Pointer to the bitstream buffer being locked.
Definition: nvEncodeAPI.h:1658
uint32_t * sliceOffsets
[in,out]: Array which receives the slice offsets.
Definition: nvEncodeAPI.h:1659
uint32_t inBufferSize
[in]: Specifies the size of the spsppsBuffer provied by the client
Definition: nvEncodeAPI.h:1788
int max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: avcodec.h:1101
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:1761
NV_ENC_INPUT_PTR inputBuffer
[in]: Pointer to the input buffer to be locked, client should pass the pointer obtained from NvEncCre...
Definition: nvEncodeAPI.h:1692
int encoder_flushing
Definition: nvenc.h:127
NV_ENC_CONFIG_H264 h264Config
[in]: Specifies the H.264-specific encoder configuration.
Definition: nvEncodeAPI.h:1355
NV_ENC_MAP_INPUT_RESOURCE in_map
Definition: nvenc.h:47
int forced_idr
Definition: nvenc.h:161
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2132
PNVENCGETENCODECAPS nvEncGetEncodeCaps
[out]: Client should access NvEncGetEncodeCaps() API through this pointer.
Definition: nvEncodeAPI.h:3265
struct NvencContext::@115 registered_frames[MAX_REGISTERED_FRAMES]
int num
Numerator.
Definition: rational.h:59
#define NV_ENC_CREATE_INPUT_BUFFER_VER
NV_ENC_CREATE_INPUT_BUFFER struct version.
Definition: nvEncodeAPI.h:1008
uint32_t chromaFormatIDC
[in]: Specifies the chroma format.
Definition: nvEncodeAPI.h:1254
Forward predicted.
Definition: nvEncodeAPI.h:286
#define PRESET_ALIAS(alias, name,...)
Definition: nvenc.c:575
static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx)
Definition: nvenc.c:1293
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.h:63
NV_ENC_OUTPUT_PTR bitstreamBuffer
[out]: Pointer to the output bitstream buffer
Definition: nvEncodeAPI.h:1019
VBR, high quality (slower)
Definition: nvEncodeAPI.h:262
NvencDynLoadFunctions nvenc_dload_funcs
Definition: nvenc.h:111
ID3D11Device * d3d11_device
Definition: nvenc.h:117
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:1880
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:228
int first_packet_output
Definition: nvenc.h:144
NV_ENCODE_API_FUNCTION_LIST.
Definition: nvEncodeAPI.h:3254
This indicates that one or more of the parameter passed to the API call is invalid.
Definition: nvEncodeAPI.h:447
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1711
uint32_t qpPrimeYZeroTransformBypassFlag
[in]: To enable lossless encode set this to 1, set QP to 0 and RC_mode to NV_ENC_PARAMS_RC_CONSTQP an...
Definition: nvEncodeAPI.h:1214
This indicates that the encoder device supplied by the client is not valid.
Definition: nvEncodeAPI.h:416
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:208
NV_ENC_INPUT_PTR mappedResource
[out]: Mapped pointer corresponding to the registeredResource.
Definition: nvEncodeAPI.h:1713
This indicates that an unknown internal error has occurred.
Definition: nvEncodeAPI.h:531
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: nvenc.c:1972
static const GUID NV_ENC_HEVC_PROFILE_FREXT_GUID
Definition: nvEncodeAPI.h:195
NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE adaptiveTransformMode
[in]: Specifies the AdaptiveTransform Mode.
Definition: nvEncodeAPI.h:1225
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx)
Definition: nvenc.c:746
static AVPacket pkt
int init_qp_b
Definition: nvenc.h:172
uint32_t height
[in]: Input buffer width
Definition: nvEncodeAPI.h:997
#define PRESET(name,...)
Definition: nvenc.c:578
NV_ENC_CODEC_PIC_PARAMS codecPicParams
[in]: Specifies the codec specific per-picture encoding parameters.
Definition: nvEncodeAPI.h:1594
int profile
profile
Definition: avcodec.h:2827
uint32_t enableAQ
[in]: Set this to 1 to enable adaptive quantization (Spatial).
Definition: nvEncodeAPI.h:1102
NV_ENC_PARAMS_FRAME_FIELD_MODE frameFieldMode
[in]: Specifies the frame/field mode.
Definition: nvEncodeAPI.h:1374
int preset
Definition: nvenc.h:148
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1813
static void nvenc_override_rate_control(AVCodecContext *avctx)
Definition: nvenc.c:715
static NvencSurface * get_free_frame(NvencContext *ctx)
Definition: nvenc.c:1488
static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx)
Definition: nvenc.c:785
NV_ENC_QP maxQP
[in]: Specifies the maximum QP used for rate control.
Definition: nvEncodeAPI.h:1114
PNVENCUNLOCKINPUTBUFFER nvEncUnlockInputBuffer
[out]: Client should access NvEncUnlockInputBuffer() API through this pointer.
Definition: nvEncodeAPI.h:3278
10 bit Planar YUV444 [Y plane followed by U and V planes].
Definition: nvEncodeAPI.h:320
int pitch
Definition: nvenc.h:51
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
Constant QP mode.
Definition: nvEncodeAPI.h:257
int nvenc_device_count
Definition: nvenc.h:64
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:2878
NV_ENC_INPUT_PTR input_surface
Definition: nvenc.h:45
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1624
NVENCSTATUS nverr
Definition: nvenc.c:61
static const GUID NV_ENC_H264_PROFILE_MAIN_GUID
Definition: nvEncodeAPI.h:158
NV_ENC_QP initialRCQP
[in]: Specifies the initial QP used for rate control.
Definition: nvEncodeAPI.h:1115
Bi-directionally predicted with only Intra MBs.
Definition: nvEncodeAPI.h:290
NV_ENC_PARAMS_RC_MODE rateControlMode
[in]: Specifies the rate control mode.
Definition: nvEncodeAPI.h:1093
This indicates that an invalid struct version was used by the client.
Definition: nvEncodeAPI.h:491
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:538
int aq
Definition: nvenc.h:159
PNVENCDESTROYINPUTBUFFER nvEncDestroyInputBuffer
[out]: Client should access NvEncDestroyInputBuffer() API through this pointer.
Definition: nvEncodeAPI.h:3271
#define AV_PIX_FMT_P010
Definition: pixfmt.h:413
Creation parameters for input buffer.
Definition: nvEncodeAPI.h:993
CUcontext cu_context
Definition: nvenc.h:115
This indicates that API call returned with no errors.
Definition: nvEncodeAPI.h:400
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
uint32_t strictGOPTarget
[in]: Set this to 1 to minimize GOP-to-GOP rate fluctuations
Definition: nvEncodeAPI.h:1110
NV_ENC_CONFIG_H264_VUI_PARAMETERS h264VUIParameters
[in]: Specifies the H264 video usability info pamameters
Definition: nvEncodeAPI.h:1246
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:2866
PNVENCCREATEINPUTBUFFER nvEncCreateInputBuffer
[out]: Client should access NvEncCreateInputBuffer() API through this pointer.
Definition: nvEncodeAPI.h:3270
NV_ENC_CONFIG * encodeConfig
[in]: Specifies the advanced codec specific structure.
Definition: nvEncodeAPI.h:1414
#define DEFAULT
Definition: avdct.c:28
uint8_t
AVFifoBuffer * unused_surface_queue
Definition: nvenc.h:122
uint32_t inputWidth
[in]: Specifies the input buffer width
Definition: nvEncodeAPI.h:1581
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
Indicates HW support for field mode encoding.
Definition: nvEncodeAPI.h:688
PNVENCGETENCODEGUIDCOUNT nvEncGetEncodeGUIDCount
[out]: Client should access NvEncGetEncodeGUIDCount() API through this pointer.
Definition: nvEncodeAPI.h:3259
float delta
enum AVPixelFormat ff_nvenc_pix_fmts[]
Definition: nvenc.c:39
NV_ENC_PIC_PARAMS_HEVC hevcPicParams
[in]: HEVC encode picture params.
Definition: nvEncodeAPI.h:1570
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:1770
PNVENCLOCKBITSTREAM nvEncLockBitstream
[out]: Client should access NvEncLockBitstream() API through this pointer.
Definition: nvEncodeAPI.h:3275
int init_qp_p
Definition: nvenc.h:171
Indicates HW support for 10 bit encoding.
Definition: nvEncodeAPI.h:945
static int nvenc_pop_context(AVCodecContext *avctx)
Definition: nvenc.c:190
uint32_t colourMatrix
[in]: Specifies the matrix coefficients used in deriving the luma and chroma from the RGB primaries (...
Definition: nvEncodeAPI.h:1143
#define FF_PROFILE_HEVC_MAIN
Definition: avcodec.h:2915
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:400
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:294
Input struct for querying Encoding capabilities.
Definition: nvEncodeAPI.h:979
Field encoding top field first.
Definition: nvEncodeAPI.h:277
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1602
#define NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER
Macro for constructing the version field of ::_NV_ENC_OPEN_ENCODE_SESSIONEX_PARAMS.
Definition: nvEncodeAPI.h:1830
float quality
Definition: nvenc.h:168
NV_ENC_INITIALIZE_PARAMS init_encode_params
Definition: nvenc.h:113
static AVFrame * frame
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:91
uint32_t maxNumRefFrames
[in]: Specifies the DPB size used for encoding.
Definition: nvEncodeAPI.h:1233
static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
Definition: nvenc.c:1236
NV_ENC_OUTPUT_PTR outputBitstream
[in]: Specifies the pointer to output buffer.
Definition: nvEncodeAPI.h:1589
uint32_t outputBufferingPeriodSEI
[in]: Set to 1 to write SEI buffering period syntax in the bitstream
Definition: nvEncodeAPI.h:1195
#define height
ID3D11Device * device
Device used for texture creation and access.
This indicates encode driver requires more input buffers to produce an output bitstream.
Definition: nvEncodeAPI.h:513
#define MAX_REGISTERED_FRAMES
Definition: nvenc.h:39
Indicates HW support for lossless encoding.
Definition: nvEncodeAPI.h:910
uint8_t * data
Definition: avcodec.h:1414
static av_cold int nvenc_setup_extradata(AVCodecContext *avctx)
Definition: nvenc.c:1334
static int flags
Definition: log.c:57
This indicates that the client is attempting to use a feature that is not available for the license t...
Definition: nvEncodeAPI.h:537
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:75
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
Encode the current picture as an Intra picture.
Definition: nvEncodeAPI.h:569
Encode the current picture as an IDR picture.
Definition: nvEncodeAPI.h:570
NV_ENC_BUFFER_FORMAT
Input buffer formats.
Definition: nvEncodeAPI.h:311
static int nvenc_check_capabilities(AVCodecContext *avctx)
Definition: nvenc.c:288
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:381
static int nvenc_register_frame(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:1557
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:1117
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
uint8_t targetQuality
[in]: Target CQ (Constant Quality) level for VBR mode (range 0-51 with 0-automatic) ...
Definition: nvEncodeAPI.h:1118
Undefined buffer format.
Definition: nvEncodeAPI.h:313
#define FF_PROFILE_HEVC_MAIN_10
Definition: avcodec.h:2916
tcuCtxPushCurrent_v2 * cuCtxPushCurrent
This indicates that the completion event passed in NvEncEncodePicture() API has not been registered w...
Definition: nvEncodeAPI.h:526
#define NV_ENC_MAP_INPUT_RESOURCE_VER
Macro for constructing the version field of _NV_ENC_MAP_INPUT_RESOURCE.
Definition: nvEncodeAPI.h:1720
uint32_t * outSPSPPSPayloadSize
[out]: Size of the sequence and picture header in bytes written by the NvEncodeAPI interface to the S...
Definition: nvEncodeAPI.h:1792
AVFifoBuffer * output_surface_ready_queue
Definition: nvenc.h:124
uint32_t apiVersion
[in]: API version.
Definition: nvEncodeAPI.h:1825
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1394
This indicates that the NvEncLockBitstream() failed to lock the output buffer.
Definition: nvEncodeAPI.h:480
Variable bitrate mode.
Definition: nvEncodeAPI.h:258
#define av_log(a,...)
uint32_t enableInitialRCQP
[in]: Set this to 1 if user suppplied initial QP is used for rate control.
Definition: nvEncodeAPI.h:1101
PNVENCLOCKINPUTBUFFER nvEncLockInputBuffer
[out]: Client should access NvEncLockInputBuffer() API through this pointer.
Definition: nvEncodeAPI.h:3277
CUcontext cu_context_internal
Definition: nvenc.h:116
An API-specific header for AV_HWDEVICE_TYPE_CUDA.
uint8_t targetQualityLSB
[in]: Fractional part of target quality (as 8.8 fixed point format)
Definition: nvEncodeAPI.h:1119
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1446
int ptr_index
Definition: nvenc.h:131
uint32_t outputBufferingPeriodSEI
[in]: Set 1 to write SEI buffering period syntax in the bitstream
Definition: nvEncodeAPI.h:1274
uint32_t inputPitch
[in]: Specifies the input buffer pitch.
Definition: nvEncodeAPI.h:1583
int async_depth
Definition: nvenc.h:157
uint32_t outputPictureTimingSEI
[in]: Set 1 to write SEI picture timing syntax in the bitstream
Definition: nvEncodeAPI.h:1275
enum AVCodecID id
Definition: avcodec.h:3379
uint32_t disableIadapt
[in]: Set this to 1 to disable adaptive I-frame insertion at scene cuts (only has an effect when look...
Definition: nvEncodeAPI.h:1105
PNVENCGETSEQUENCEPARAMS nvEncGetSequenceParams
[out]: Client should access NvEncGetSequenceParams() API through this pointer.
Definition: nvEncodeAPI.h:3280
NV_ENC_H264_ENTROPY_CODING_MODE entropyCodingMode
[in]: Specifies the entropy coding mode.
Definition: nvEncodeAPI.h:1228
uint32_t colourPrimaries
[in]: Specifies color primaries for converting to RGB(as defined in Annex E of the ITU-T Specificatio...
Definition: nvEncodeAPI.h:1141
static av_cold int nvenc_open_session(AVCodecContext *avctx)
Definition: nvenc.c:209
void * ptr
Definition: nvenc.h:130
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1791
static const GUID NV_ENC_CODEC_H264_GUID
Definition: nvEncodeAPI.h:136
Indicates HW support for lookahead encoding (enableLookahead=1).
Definition: nvEncodeAPI.h:932
input resource type is a cuda device pointer surface
Definition: nvEncodeAPI.h:650
int coder
Definition: nvenc.h:176
static void timestamp_queue_enqueue(AVFifoBuffer *queue, int64_t timestamp)
Definition: nvenc.c:1690
int rc
Definition: nvenc.h:152
NV_ENC_CONFIG presetCfg
[out]: preset config returned by the Nvidia Video Encoder interface.
Definition: nvEncodeAPI.h:1458
uint32_t chromaFormatIDC
[in]: Specifies the chroma format.
Definition: nvEncodeAPI.h:1287
#define NV_ENC_LOCK_BITSTREAM_VER
Macro for constructing the version field of _NV_ENC_LOCK_BITSTREAM.
Definition: nvEncodeAPI.h:1680
uint32_t colourDescriptionPresentFlag
[in]: If set to 1, it specifies that the colourPrimaries, transferCharacteristics and colourMatrix ar...
Definition: nvEncodeAPI.h:1140
#define AVERROR(e)
Definition: error.h:43
int nb_registered_frames
Definition: nvenc.h:135
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
int qmax
maximum quantizer
Definition: avcodec.h:2346
This indicates that the size of the user buffer passed by the client is insufficient for the requeste...
Definition: nvEncodeAPI.h:486
uint64_t inputTimeStamp
[in]: Specifies presentation timestamp associated with the input picture.
Definition: nvEncodeAPI.h:1586
uint32_t disableBadapt
[in]: Set this to 1 to disable adaptive B-frame decision (only has an effect when lookahead is enable...
Definition: nvEncodeAPI.h:1106
static int nvenc_map_error(NVENCSTATUS err, const char **desc)
Definition: nvenc.c:93
NV_ENC_CAPS capsToQuery
[in]: Specifies the encode capability to be queried.
Definition: nvEncodeAPI.h:982
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
static const GUID NV_ENC_H264_PROFILE_BASELINE_GUID
Definition: nvEncodeAPI.h:154
uint32_t videoFullRangeFlag
[in]: Specifies the output range of the luma and chroma samples(as defined in Annex E of the ITU-T Sp...
Definition: nvEncodeAPI.h:1139
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:2870
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1582
GLenum GLint * params
Definition: opengl_enc.c:114
uint32_t bitstreamSizeInBytes
[out]: Actual number of bytes generated and copied to the memory pointed by bitstreamBufferPtr.
Definition: nvEncodeAPI.h:1663
NV_ENC_INPUT_PTR inputBuffer
[out]: Pointer to input buffer
Definition: nvEncodeAPI.h:1001
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:85
uint16_t width
Definition: gdv.c:47
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:347
PNVENCGETENCODEPRESETCONFIG nvEncGetEncodePresetConfig
[out]: Client should access NvEncGetEncodePresetConfig() API through this pointer.
Definition: nvEncodeAPI.h:3268
tcuInit * cuInit
This indicates that the client is attempting to unregister a resource that has not been successfully ...
Definition: nvEncodeAPI.h:554
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:1806
uint32_t maxNumRefFramesInDPB
[in]: Specifies the maximum number of references frames in the DPB.
Definition: nvEncodeAPI.h:1294
uint32_t averageBitRate
[in]: Specifies the average bitrate(in bits/sec) used for encoding.
Definition: nvEncodeAPI.h:1095
int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:1869
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:78
static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
Definition: nvenc.c:992
GLsizei count
Definition: opengl_enc.c:109
static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt)
Definition: nvenc.c:1214
#define FFMAX(a, b)
Definition: common.h:94
NV_ENC_PIC_PARAMS_H264 h264PicParams
[in]: H264 encode picture params.
Definition: nvEncodeAPI.h:1569
static av_cold void set_constqp(AVCodecContext *avctx)
Definition: nvenc.c:606
PNVENCREGISTERRESOURCE nvEncRegisterResource
[out]: Client should access NvEncRegisterResource() API through this pointer.
Definition: nvEncodeAPI.h:3288
#define fail()
Definition: checkasm.h:113
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:386
int level
Definition: nvenc.h:150
uint32_t videoSignalTypePresentFlag
[in]: If set to 1, it specifies that the videoFormat, videoFullRangeFlag and colourDescriptionPresent...
Definition: nvEncodeAPI.h:1137
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1420
Encoder Session Creation parameters.
Definition: nvEncodeAPI.h:1819
int bluray_compat
Definition: nvenc.h:170
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2360
int aq_strength
Definition: nvenc.h:167
static int nvenc_check_codec_support(AVCodecContext *avctx)
Definition: nvenc.c:235
8 bit Packed A8B8G8R8.
Definition: nvEncodeAPI.h:333
uint32_t enableWeightedPrediction
[in]: Set this to 1 to enable weighted prediction.
Definition: nvEncodeAPI.h:1410
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1457
GUID profileGUID
[in]: Specifies the codec profile guid.
Definition: nvEncodeAPI.h:1370
uint32_t pixelBitDepthMinus8
[in]: Specifies pixel bit depth minus 8.
Definition: nvEncodeAPI.h:1288
uint32_t sliceMode
[in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divi...
Definition: nvEncodeAPI.h:1237
PNVENCENCODEPICTURE nvEncEncodePicture
[out]: Client should access NvEncEncodePicture() API through this pointer.
Definition: nvEncodeAPI.h:3274
int refs
number of reference frames
Definition: avcodec.h:2085
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1787
int flags
Definition: nvenc.h:156
#define NVENCAPI_MAJOR_VERSION
Definition: nvEncodeAPI.h:116
uint32_t sliceMode
[in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divi...
Definition: nvEncodeAPI.h:1544
This indicates that the client is attempting to use a feature that is not implemented for the current...
Definition: nvEncodeAPI.h:543
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:284
10 bit Semi-Planar YUV [Y plane followed by interleaved UV plane].
Definition: nvEncodeAPI.h:319
static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame, NvencSurface *nvenc_frame)
Definition: nvenc.c:1611
tcuCtxCreate_v2 * cuCtxCreate
uint32_t repeatSPSPPS
[in]: Set to 1 to enable writing of Sequence and Picture parameter for every IDR frame ...
Definition: nvEncodeAPI.h:1205
NV_ENC_REGISTERED_PTR regptr
Definition: nvenc.h:132
#define FFMIN(a, b)
Definition: common.h:96
static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
Definition: nvenc.c:1085
uint32_t qpInterB
Definition: nvEncodeAPI.h:1083
NV_ENC_BUFFER_FORMAT mappedBufferFmt
[out]: Buffer format of the outputResource.
Definition: nvEncodeAPI.h:1714
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:51
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:148
PNVENCOPENENCODESESSIONEX nvEncOpenEncodeSessionEx
[out]: Client should access NvEncOpenEncodeSession() API through this pointer.
Definition: nvEncodeAPI.h:3287
NV_ENC_QP minQP
[in]: Specifies the minimum QP used for rate control.
Definition: nvEncodeAPI.h:1113
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:74
uint32_t encodePicFlags
[in]: Specifies bit-wise OR`ed encode pic flags.
Definition: nvEncodeAPI.h:1584
This indicates that device passed to the API call is invalid.
Definition: nvEncodeAPI.h:421
av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:1445
This indicates that the client is attempting to unmap a resource that has not been successfully mappe...
Definition: nvEncodeAPI.h:560
static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)
Definition: nvenc.c:1738
Maximum output height supported.
Definition: nvEncodeAPI.h:783
int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: nvenc.c:2008
Entropy coding mode is CABAC.
Definition: nvEncodeAPI.h:595
int width
picture width / height.
Definition: avcodec.h:1674
uint32_t idrPeriod
[in]: Specifies the IDR interval.
Definition: nvEncodeAPI.h:1290
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:3168
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1580
#define NVENC_CAP
Definition: nvenc.c:34
uint16_t lookaheadDepth
[in]: Maximum depth of lookahead with range 0-32 (only used if enableLookahead=1) ...
Definition: nvEncodeAPI.h:1120
tNvEncodeAPICreateInstance * NvEncodeAPICreateInstance
uint32_t aqStrength
[in]: When AQ (Spatial) is enabled (i.e.
Definition: nvEncodeAPI.h:1111
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1015
AVFormatContext * ctx
Definition: movenc.c:48
#define NV_ENCODE_API_FUNCTION_LIST_VER
Macro for constructing the version field of ::_NV_ENCODEAPI_FUNCTION_LIST.
Definition: nvEncodeAPI.h:3299
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2111
uint32_t zeroReorderDelay
[in]: Set this to 1 to indicate zero latency operation (no reordering delay, num_reorder_frames=0) ...
Definition: nvEncodeAPI.h:1108
Intra predicted picture.
Definition: nvEncodeAPI.h:288
Maximum output width supported.
Definition: nvEncodeAPI.h:778
static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface, NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame)
Definition: nvenc.c:1500
#define IS_YUV444(pix_fmt)
Definition: nvenc.c:57
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:1633
int profile
Definition: nvenc.h:149
PNVENCDESTROYBITSTREAMBUFFER nvEncDestroyBitstreamBuffer
[out]: Client should access NvEncDestroyBitstreamBuffer() API through this pointer.
Definition: nvEncodeAPI.h:3273
AVFifoBuffer * output_surface_queue
Definition: nvenc.h:123
#define NV_ENC_INITIALIZE_PARAMS_VER
macro for constructing the version field of _NV_ENC_INITIALIZE_PARAMS
Definition: nvEncodeAPI.h:1429
#define NV_ENC_PRESET_CONFIG_VER
macro for constructing the version field of _NV_ENC_PRESET_CONFIG
Definition: nvEncodeAPI.h:1464
8 bit Packed A8R8G8B8.
Definition: nvEncodeAPI.h:321
uint32_t maxBitRate
[in]: Specifies the maximum bitrate for the encoded output.
Definition: nvEncodeAPI.h:1096
static void nvenc_print_driver_requirement(AVCodecContext *avctx, int level)
Definition: nvenc.c:118
uint32_t enableLookahead
[in]: Set this to 1 to enable lookahead with depth <lookaheadDepth> (if lookahead is enabled...
Definition: nvEncodeAPI.h:1104
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
Constant bitrate mode.
Definition: nvEncodeAPI.h:259
HW acceleration through CUDA.
Definition: pixfmt.h:231
static void error(const char *err)
static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx)
Definition: nvenc.c:904
#define FF_ARRAY_ELEMS(a)
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:499
uint32_t level
[in]: Specifies the encoding level.
Definition: nvEncodeAPI.h:1218
static int nvenc_print_error(void *log_ctx, NVENCSTATUS err, const char *error_string)
Definition: nvenc.c:108
NV_ENC_BUFFER_FORMAT bufferFmt
[in]: Input buffer format
Definition: nvEncodeAPI.h:999
CudaFunctions * cuda_dl
Definition: nvenc.h:60
enum AVPixelFormat data_pix_fmt
Definition: nvenc.h:139
#define IS_10BIT(pix_fmt)
Definition: nvenc.c:54
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:274
#define NV_ENC_CAPS_PARAM_VER
NV_ENC_CAPS_PARAM struct version.
Definition: nvEncodeAPI.h:987
PNVENCUNREGISTERRESOURCE nvEncUnregisterResource
[out]: Client should access NvEncUnregisterResource() API through this pointer.
Definition: nvEncodeAPI.h:3289
uint32_t inputHeight
[in]: Specifies the input buffer height
Definition: nvEncodeAPI.h:1582
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:1096
uint32_t qpInterP
Definition: nvEncodeAPI.h:1082
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
NV_ENC_CONFIG encode_config
Definition: nvenc.h:114
static const struct @109 nvenc_errors[]
static int nvenc_push_context(AVCodecContext *avctx)
Definition: nvenc.c:172
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:111
uint32_t outputPictureTimingSEI
[in]: Set to 1 to write SEI picture timing syntax in the bitstream.
Definition: nvEncodeAPI.h:1196
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:232
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:995
uint32_t doNotWait
[in]: If this flag is set, the NvEncodeAPI interface will return buffer pointer even if operation is ...
Definition: nvEncodeAPI.h:1655
int strict_gop
Definition: nvenc.h:166
PNVENCMAPINPUTRESOURCE nvEncMapInputResource
[out]: Client should access NvEncMapInputResource() API through this pointer.
Definition: nvEncodeAPI.h:3283
encode device type is a cuda device
Definition: nvEncodeAPI.h:661
#define NVENCAPI_VERSION
Definition: nvEncodeAPI.h:119
void * bitstreamBufferPtr
[out]: Pointer to the generated output bitstream.
Definition: nvEncodeAPI.h:1666
int temporal_aq
Definition: nvenc.h:163
int64_t initial_pts[2]
Definition: nvenc.h:143
Creation parameters for output bitstream buffer.
Definition: nvEncodeAPI.h:1013
NV_ENC_H264_FMO_MODE fmoMode
[in]: Specified the FMO Mode.
Definition: nvEncodeAPI.h:1226
main external API structure.
Definition: avcodec.h:1502
uint32_t frameRateNum
[in]: Specifies the numerator for frame rate used for encoding in frames per second ( Frame rate = fr...
Definition: nvEncodeAPI.h:1401
uint8_t * data
The data buffer.
Definition: buffer.h:89
int qmin
minimum quantizer
Definition: avcodec.h:2339
#define BD
uint32_t frameAvgQP
[out]: Average QP of the frame.
Definition: nvEncodeAPI.h:1671
uint32_t disableSPSPPS
[in]: Set 1 to disable VPS,SPS and PPS signalling in the bitstream.
Definition: nvEncodeAPI.h:1284
Rate Control Configuration Paramters.
Definition: nvEncodeAPI.h:1090
int init_qp_i
Definition: nvenc.h:173
Indicates HW support for temporal AQ encoding (enableTemporalAQ=1).
Definition: nvEncodeAPI.h:939
int extradata_size
Definition: avcodec.h:1603
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
NV_ENC_RC_PARAMS rcParams
[in]: Specifies the rate control parameters for the current encoding session.
Definition: nvEncodeAPI.h:1378
This struct is allocated as AVHWDeviceContext.hwctx.
static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
Definition: nvenc.c:271
This indicates that one or more of the pointers passed to the API call is invalid.
Definition: nvEncodeAPI.h:435
GUID encodeGUID
[in]: Specifies the Encode GUID for which the encoder is being created.
Definition: nvEncodeAPI.h:1395
NV_ENC_INPUT_PTR inputBuffer
[in]: Specifies the input buffer pointer.
Definition: nvEncodeAPI.h:1588
uint32_t level
[in]: Specifies the level of the encoded bitstream.
Definition: nvEncodeAPI.h:1268
void * bufferDataPtr
[out]: Pointed to the locked input buffer data.
Definition: nvEncodeAPI.h:1693
uint32_t transferCharacteristics
[in]: Specifies the opto-electronic transfer characteristics to use (as defined in Annex E of the ITU...
Definition: nvEncodeAPI.h:1142
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2125
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2118
int width
Definition: nvenc.h:49
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:123
This indicates that an API call was made in wrong sequence/order.
Definition: nvEncodeAPI.h:452
Indicates HW support for YUV444 mode encoding.
Definition: nvEncodeAPI.h:903
#define NV_ENC_PIC_PARAMS_VER
Macro for constructing the version field of _NV_ENC_PIC_PARAMS.
Definition: nvEncodeAPI.h:1612
static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *params)
Definition: nvenc.c:1669
#define IS_CBR(rc)
Definition: nvenc.c:35
AVPictureType
Definition: avutil.h:272
int flags
Definition: nvenc.c:572
int nonref_p
Definition: nvenc.h:165
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1783
uint32_t sliceModeData
[in]: Specifies the parameter needed for sliceMode.
Definition: nvEncodeAPI.h:1548
PNVENCUNMAPINPUTRESOURCE nvEncUnmapInputResource
[out]: Client should access NvEncUnmapInputResource() API through this pointer.
Definition: nvEncodeAPI.h:3284
int cbr
Definition: nvenc.h:153
static int nvenc_find_free_reg_resource(AVCodecContext *avctx)
Definition: nvenc.c:1530
uint32_t frameRateDen
[in]: Specifies the denominator for frame rate used for encoding in frames per second ( Frame rate = ...
Definition: nvEncodeAPI.h:1402
int averr
Definition: nvenc.c:62
NV_ENC_CONFIG_HEVC_VUI_PARAMETERS hevcVUIParameters
[in]: Specifies the HEVC video usability info pamameters
Definition: nvEncodeAPI.h:1310
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:510
NV_ENC_CONFIG_HEVC hevcConfig
[in]: Specifies the HEVC-specific encoder configuration.
Definition: nvEncodeAPI.h:1356
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1568
uint32_t gopLength
[in]: Specifies the number of pictures in one GOP.
Definition: nvEncodeAPI.h:1371
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
int reg_idx
Definition: nvenc.h:48
uint8_t level
Definition: svq3.c:207
uint32_t sliceModeData
[in]: Specifies the parameter needed for sliceMode.
Definition: nvEncodeAPI.h:1304
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:879
uint64_t outputTimeStamp
[out]: Presentation timestamp associated with the encoded output.
Definition: nvEncodeAPI.h:1664
This indicates that devices pass by the client is not supported.
Definition: nvEncodeAPI.h:410
IDR picture.
Definition: nvEncodeAPI.h:289
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:309
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1696
tcuDeviceGetCount * cuDeviceGetCount
#define NVENCAPI_MINOR_VERSION
Definition: nvEncodeAPI.h:117
static const GUID NV_ENC_HEVC_PROFILE_MAIN_GUID
Definition: nvEncodeAPI.h:186
uint32_t sliceMode
[in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divi...
Definition: nvEncodeAPI.h:1504
NV_ENC_CAPS
Encoder capabilities enumeration.
Definition: nvEncodeAPI.h:669
uint32_t sliceMode
[in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divi...
Definition: nvEncodeAPI.h:1301
Bi-directionally predicted picture.
Definition: nvEncodeAPI.h:287
int
This indicates that completion event passed in NvEncEncodePicture() call is invalid.
Definition: nvEncodeAPI.h:441
NV_ENC_PIC_TYPE pictureType
[out]: Picture type of the encoded picture.
Definition: nvEncodeAPI.h:1669
int b_adapt
Definition: nvenc.h:162
#define NV_ENC_REGISTER_RESOURCE_VER
Macro for constructing the version field of _NV_ENC_REGISTER_RESOURCE.
Definition: nvEncodeAPI.h:1757
static int64_t timestamp_queue_dequeue(AVFifoBuffer *queue)
Definition: nvenc.c:1695
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:279
uint32_t tier
[in]: Specifies the level tier of the encoded bitstream.
Definition: nvEncodeAPI.h:1269
This indicates that an unsupported parameter was passed by the client.
Definition: nvEncodeAPI.h:471
NV_ENC_DEVICE_TYPE deviceType
[in]: Specified the device Type
Definition: nvEncodeAPI.h:1822
int weighted_pred
Definition: nvenc.h:175
int rc_lookahead
Definition: nvenc.h:158
uint32_t width
[in]: Input buffer width
Definition: nvEncodeAPI.h:996
static int output_ready(AVCodecContext *avctx, int flush)
Definition: nvenc.c:1851
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1821
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:76
PNVENCDESTROYENCODER nvEncDestroyEncoder
[out]: Client should access NvEncDestroyEncoder() API through this pointer.
Definition: nvEncodeAPI.h:3285
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2744
NV_ENC_PIC_STRUCT pictureStruct
[in]: Specifies structure of the input picture.
Definition: nvEncodeAPI.h:1592
#define NV_ENC_PARAMS_RC_VBR_MINQP
Deprecated.
Definition: nvEncodeAPI.h:265
tcuCtxPopCurrent_v2 * cuCtxPopCurrent
static const GUID NV_ENC_CODEC_HEVC_GUID
Definition: nvEncodeAPI.h:140
int size
Definition: nvenc.h:55
NvencSurface * surfaces
Definition: nvenc.h:120
int den
Denominator.
Definition: rational.h:60
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
AVCPBProperties * ff_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition: utils.c:2164
uint32_t outputAUD
[in]: Set 1 to write Access Unit Delimiter syntax.
Definition: nvEncodeAPI.h:1276
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:770
#define NV_ENC_BUFFER_FORMAT_YV12_PL
Definition: nvEncodeAPI.h:344
uint32_t darWidth
[in]: Specifies the display aspect ratio Width.
Definition: nvEncodeAPI.h:1399
#define FF_PROFILE_HEVC_REXT
Definition: avcodec.h:2918
void * priv_data
Definition: avcodec.h:1529
tcuDeviceComputeCapability * cuDeviceComputeCapability
static int nvenc_set_timestamp(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *params, AVPacket *pkt)
Definition: nvenc.c:1704
#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:353
uint32_t repeatSPSPPS
[in]: Set 1 to output VPS,SPS and PPS for every IDR frame.
Definition: nvEncodeAPI.h:1285
static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
Definition: nvenc.c:128
static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
Definition: nvenc.c:374
int avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: avcodec.h:1111
#define NV_ENC_BUFFER_FORMAT_NV12_PL
Definition: nvEncodeAPI.h:343
uint32_t enableMaxQP
[in]: Set this to 1 if maximum QP used for rate control.
Definition: nvEncodeAPI.h:1100
uint32_t sliceModeData
[in]: Specifies the parameter needed for sliceMode.
Definition: nvEncodeAPI.h:1241
low-delay CBR, high quality
Definition: nvEncodeAPI.h:260
int device
Definition: nvenc.h:155
uint32_t darHeight
[in]: Specifies the display aspect ratio height.
Definition: nvEncodeAPI.h:1400
uint32_t enableEncodeAsync
[in]: Set this to 1 to enable asynchronous mode and is expected to use events to get picture completi...
Definition: nvEncodeAPI.h:1403
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
#define NV_ENC_CREATE_BITSTREAM_BUFFER_VER
NV_ENC_CREATE_BITSTREAM_BUFFER struct version.
Definition: nvEncodeAPI.h:1026
uint32_t qpIntra
Definition: nvEncodeAPI.h:1084
input resource type is a directx9 surface
Definition: nvEncodeAPI.h:649
tcuCtxDestroy_v2 * cuCtxDestroy
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1709
static const GUID NV_ENC_H264_PROFILE_HIGH_GUID
Definition: nvEncodeAPI.h:162
int32_t frameIntervalP
[in]: Specifies the GOP pattern as follows: frameIntervalP = 0: I, 1: IPP, 2: IBP, 3: IBBP If goplength is set to NVENC_INFINITE_GOPLENGTH frameIntervalP should be set to 1.
Definition: nvEncodeAPI.h:1372
NV_ENC_BUFFER_FORMAT bufferFmt
[in]: Specifies the input buffer format.
Definition: nvEncodeAPI.h:1591
#define NV_ENC_CONFIG_VER
macro for constructing the version field of _NV_ENC_CONFIG
Definition: nvEncodeAPI.h:1385
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1413
PNVENCINITIALIZEENCODER nvEncInitializeEncoder
[out]: Client should access NvEncInitializeEncoder() API through this pointer.
Definition: nvEncodeAPI.h:3269
This struct is allocated as AVHWDeviceContext.hwctx.
int nb_surfaces
Definition: nvenc.h:119
int aud
Definition: nvenc.h:169
uint32_t enablePTD
[in]: Set this to 1 to enable the Picture Type Decision is be taken by the NvEncodeAPI interface...
Definition: nvEncodeAPI.h:1404
int cqp
Definition: nvenc.h:174
#define av_freep(p)
#define AV_CODEC_ID_H265
Definition: avcodec.h:393
static av_cold void set_lossless(AVCodecContext *avctx)
Definition: nvenc.c:701
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1654
uint32_t outputAUD
[in]: Set to 1 to write access unit delimiter syntax in bitstream
Definition: nvEncodeAPI.h:1198
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:170
uint32_t enableMinQP
[in]: Set this to 1 if minimum QP used for rate control.
Definition: nvEncodeAPI.h:1099
CBR, high quality (slower)
Definition: nvEncodeAPI.h:261
PNVENCUNLOCKBITSTREAM nvEncUnlockBitstream
[out]: Client should access NvEncUnlockBitstream() API through this pointer.
Definition: nvEncodeAPI.h:3276
#define FFSWAP(type, a, b)
Definition: common.h:99
#define NV_ENC_BUFFER_FORMAT_YUV444_PL
Definition: nvEncodeAPI.h:346
uint32_t encodeHeight
[in]: Specifies the encode height.
Definition: nvEncodeAPI.h:1398
uint32_t enableNonRefP
[in]: Set this to 1 to enable automatic insertion of non-reference P-frames (no effect if enablePTD=0...
Definition: nvEncodeAPI.h:1109
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:2279
int tier
Definition: nvenc.h:151
#define NV_ENC_LOCK_INPUT_BUFFER_VER
Macro for constructing the version field of _NV_ENC_LOCK_INPUT_BUFFER.
Definition: nvEncodeAPI.h:1700
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:3220
void * spsppsBuffer
[in]: Specifies bitstream header pointer of size NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize.
Definition: nvEncodeAPI.h:1791
tcuDeviceGet * cuDeviceGet
uint32_t videoFormat
[in]: Specifies the source video format(as defined in Annex E of the ITU-T Specification).
Definition: nvEncodeAPI.h:1138
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:221
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1391
tNvEncodeAPIGetMaxSupportedVersion * NvEncodeAPIGetMaxSupportedVersion
uint32_t version
[in]: Client should pass NV_ENCODE_API_FUNCTION_LIST_VER.
Definition: nvEncodeAPI.h:3256
encode device type is a directx9 device
Definition: nvEncodeAPI.h:660
This indicates that the encoder has not been initialized with NvEncInitializeEncoder() or that initia...
Definition: nvEncodeAPI.h:466
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:981
NV_ENC_OUTPUT_PTR output_surface
Definition: nvenc.h:53
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1407
uint32_t disableSPSPPS
[in]: Set to 1 to disable writing of Sequence and Picture parameter info in bitstream ...
Definition: nvEncodeAPI.h:1199
for(j=16;j >0;--j)
#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:580
This indicates that device passed to the API call is no longer available and needs to be reinitialize...
Definition: nvEncodeAPI.h:429
int zerolatency
Definition: nvenc.h:164
This indicates that no encode capable devices were detected.
Definition: nvEncodeAPI.h:405
#define AV_PIX_FMT_0RGB32
Definition: pixfmt.h:346
NVENCSTATUS
Error Codes.
Definition: nvEncodeAPI.h:395
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2375
Maximum number of B-Frames supported.
Definition: nvEncodeAPI.h:674
This indicates that NvEncMapInputResource() API failed to map the client provided input resource...
Definition: nvEncodeAPI.h:497
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191
const char * name
Definition: opengl_enc.c:103
NV_ENC_CODEC_CONFIG encodeCodecConfig
[in]: Specifies the codec specific config parameters through this union.
Definition: nvEncodeAPI.h:1379
This indicates that the API call failed because it was unable to allocate enough memory to perform th...
Definition: nvEncodeAPI.h:458
uint32_t version
[in]: Struct version.
Definition: nvEncodeAPI.h:1689
Adaptive Transform 8x8 mode should be used.
Definition: nvEncodeAPI.h:627