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 hardware encoding using nvidia nvenc
3  * Copyright (c) 2014 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 #if defined(_WIN32)
23 #include <windows.h>
24 #else
25 #include <dlfcn.h>
26 #endif
27 
28 #include <nvEncodeAPI.h>
29 
30 #include "libavutil/internal.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/mem.h"
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "thread.h"
38 
39 #if defined(_WIN32)
40 #define CUDAAPI __stdcall
41 #else
42 #define CUDAAPI
43 #endif
44 
45 #if defined(_WIN32)
46 #define LOAD_FUNC(l, s) GetProcAddress(l, s)
47 #define DL_CLOSE_FUNC(l) FreeLibrary(l)
48 #else
49 #define LOAD_FUNC(l, s) dlsym(l, s)
50 #define DL_CLOSE_FUNC(l) dlclose(l)
51 #endif
52 
53 typedef enum cudaError_enum {
55 } CUresult;
56 typedef int CUdevice;
57 typedef void* CUcontext;
58 
59 typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
61 typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
62 typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
63 typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
64 typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
67 
68 typedef NVENCSTATUS (NVENCAPI* PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
69 
70 typedef struct NvencInputSurface
71 {
72  NV_ENC_INPUT_PTR input_surface;
73  int width;
74  int height;
75 
76  int lockCount;
77 
78  NV_ENC_BUFFER_FORMAT format;
80 
81 typedef struct NvencOutputSurface
82 {
83  NV_ENC_OUTPUT_PTR output_surface;
84  int size;
85 
87 
88  int busy;
90 
91 typedef struct NvencData
92 {
93  union {
94  int64_t timestamp;
96  } u;
97 } NvencData;
98 
99 typedef struct NvencDataList
100 {
102 
103  uint32_t pos;
104  uint32_t count;
105  uint32_t size;
106 } NvencDataList;
107 
108 typedef struct NvencDynLoadFunctions
109 {
118 
119  NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
122 
123 #if defined(_WIN32)
126 #else
127  void* cuda_lib;
128  void* nvenc_lib;
129 #endif
131 
132 typedef struct NvencValuePair
133 {
134  const char *str;
135  uint32_t num;
137 
138 typedef struct NvencContext
139 {
141 
143 
144  NV_ENC_INITIALIZE_PARAMS init_encode_params;
145  NV_ENC_CONFIG encode_config;
147 
151 
155  int64_t last_dts;
156 
157  void *nvencoder;
158 
159  char *preset;
160  char *profile;
161  char *level;
162  char *tier;
163  int cbr;
164  int twopass;
165  int gpu;
167 } NvencContext;
168 
170  { "auto", NV_ENC_LEVEL_AUTOSELECT },
171  { "1" , NV_ENC_LEVEL_H264_1 },
172  { "1.0" , NV_ENC_LEVEL_H264_1 },
173  { "1b" , NV_ENC_LEVEL_H264_1b },
174  { "1.0b", NV_ENC_LEVEL_H264_1b },
175  { "1.1" , NV_ENC_LEVEL_H264_11 },
176  { "1.2" , NV_ENC_LEVEL_H264_12 },
177  { "1.3" , NV_ENC_LEVEL_H264_13 },
178  { "2" , NV_ENC_LEVEL_H264_2 },
179  { "2.0" , NV_ENC_LEVEL_H264_2 },
180  { "2.1" , NV_ENC_LEVEL_H264_21 },
181  { "2.2" , NV_ENC_LEVEL_H264_22 },
182  { "3" , NV_ENC_LEVEL_H264_3 },
183  { "3.0" , NV_ENC_LEVEL_H264_3 },
184  { "3.1" , NV_ENC_LEVEL_H264_31 },
185  { "3.2" , NV_ENC_LEVEL_H264_32 },
186  { "4" , NV_ENC_LEVEL_H264_4 },
187  { "4.0" , NV_ENC_LEVEL_H264_4 },
188  { "4.1" , NV_ENC_LEVEL_H264_41 },
189  { "4.2" , NV_ENC_LEVEL_H264_42 },
190  { "5" , NV_ENC_LEVEL_H264_5 },
191  { "5.0" , NV_ENC_LEVEL_H264_5 },
192  { "5.1" , NV_ENC_LEVEL_H264_51 },
193  { NULL }
194 };
195 
197  { "auto", NV_ENC_LEVEL_AUTOSELECT },
198  { "1" , NV_ENC_LEVEL_HEVC_1 },
199  { "1.0" , NV_ENC_LEVEL_HEVC_1 },
200  { "2" , NV_ENC_LEVEL_HEVC_2 },
201  { "2.0" , NV_ENC_LEVEL_HEVC_2 },
202  { "2.1" , NV_ENC_LEVEL_HEVC_21 },
203  { "3" , NV_ENC_LEVEL_HEVC_3 },
204  { "3.0" , NV_ENC_LEVEL_HEVC_3 },
205  { "3.1" , NV_ENC_LEVEL_HEVC_31 },
206  { "4" , NV_ENC_LEVEL_HEVC_4 },
207  { "4.0" , NV_ENC_LEVEL_HEVC_4 },
208  { "4.1" , NV_ENC_LEVEL_HEVC_41 },
209  { "5" , NV_ENC_LEVEL_HEVC_5 },
210  { "5.0" , NV_ENC_LEVEL_HEVC_5 },
211  { "5.1" , NV_ENC_LEVEL_HEVC_51 },
212  { "5.2" , NV_ENC_LEVEL_HEVC_52 },
213  { "6" , NV_ENC_LEVEL_HEVC_6 },
214  { "6.0" , NV_ENC_LEVEL_HEVC_6 },
215  { "6.1" , NV_ENC_LEVEL_HEVC_61 },
216  { "6.2" , NV_ENC_LEVEL_HEVC_62 },
217  { NULL }
218 };
219 
220 static int input_string_to_uint32(AVCodecContext *avctx, const NvencValuePair *pair, const char *input, uint32_t *output)
221 {
222  for (; pair->str; ++pair) {
223  if (!strcmp(input, pair->str)) {
224  *output = pair->num;
225  return 0;
226  }
227  }
228 
229  return AVERROR(EINVAL);
230 }
231 
233 {
234  uint32_t mask;
235  uint32_t read_pos;
236 
237  av_assert0(queue);
238  av_assert0(queue->size);
239  av_assert0(queue->data);
240 
241  if (!queue->count)
242  return NULL;
243 
244  /* Size always is a multiple of two */
245  mask = queue->size - 1;
246  read_pos = (queue->pos - queue->count) & mask;
247  queue->count--;
248 
249  return &queue->data[read_pos];
250 }
251 
253 {
254  NvencDataList new_queue;
255  NvencData* tmp_data;
256  uint32_t mask;
257 
258  if (!queue->size) {
259  /* size always has to be a multiple of two */
260  queue->size = 4;
261  queue->pos = 0;
262  queue->count = 0;
263 
264  queue->data = av_malloc(queue->size * sizeof(*(queue->data)));
265 
266  if (!queue->data) {
267  queue->size = 0;
268  return AVERROR(ENOMEM);
269  }
270  }
271 
272  if (queue->count == queue->size) {
273  new_queue.size = queue->size << 1;
274  new_queue.pos = 0;
275  new_queue.count = 0;
276  new_queue.data = av_malloc(new_queue.size * sizeof(*(queue->data)));
277 
278  if (!new_queue.data)
279  return AVERROR(ENOMEM);
280 
281  while (tmp_data = data_queue_dequeue(queue))
282  data_queue_enqueue(&new_queue, tmp_data);
283 
284  av_free(queue->data);
285  *queue = new_queue;
286  }
287 
288  mask = queue->size - 1;
289 
290  queue->data[queue->pos] = *data;
291  queue->pos = (queue->pos + 1) & mask;
292  queue->count++;
293 
294  return 0;
295 }
296 
298 {
299  NvencData data;
300  data.u.surface = surface;
301 
302  return data_queue_enqueue(queue, &data);
303 }
304 
306 {
307  NvencData* res = data_queue_dequeue(queue);
308 
309  if (!res)
310  return NULL;
311 
312  return res->u.surface;
313 }
314 
315 static int timestamp_queue_enqueue(NvencDataList* queue, int64_t timestamp)
316 {
317  NvencData data;
318  data.u.timestamp = timestamp;
319 
320  return data_queue_enqueue(queue, &data);
321 }
322 
324 {
325  NvencData* res = data_queue_dequeue(queue);
326 
327  if (!res)
328  return AV_NOPTS_VALUE;
329 
330  return res->u.timestamp;
331 }
332 
333 #define CHECK_LOAD_FUNC(t, f, s) \
334 do { \
335  (f) = (t)LOAD_FUNC(dl_fn->cuda_lib, s); \
336  if (!(f)) { \
337  av_log(avctx, AV_LOG_FATAL, "Failed loading %s from CUDA library\n", s); \
338  goto error; \
339  } \
340 } while (0)
341 
343 {
344  NvencContext *ctx = avctx->priv_data;
346 
347  if (dl_fn->cuda_lib)
348  return 1;
349 
350 #if defined(_WIN32)
351  dl_fn->cuda_lib = LoadLibrary(TEXT("nvcuda.dll"));
352 #else
353  dl_fn->cuda_lib = dlopen("libcuda.so", RTLD_LAZY);
354 #endif
355 
356  if (!dl_fn->cuda_lib) {
357  av_log(avctx, AV_LOG_FATAL, "Failed loading CUDA library\n");
358  goto error;
359  }
360 
361  CHECK_LOAD_FUNC(PCUINIT, dl_fn->cu_init, "cuInit");
362  CHECK_LOAD_FUNC(PCUDEVICEGETCOUNT, dl_fn->cu_device_get_count, "cuDeviceGetCount");
363  CHECK_LOAD_FUNC(PCUDEVICEGET, dl_fn->cu_device_get, "cuDeviceGet");
364  CHECK_LOAD_FUNC(PCUDEVICEGETNAME, dl_fn->cu_device_get_name, "cuDeviceGetName");
365  CHECK_LOAD_FUNC(PCUDEVICECOMPUTECAPABILITY, dl_fn->cu_device_compute_capability, "cuDeviceComputeCapability");
366  CHECK_LOAD_FUNC(PCUCTXCREATE, dl_fn->cu_ctx_create, "cuCtxCreate_v2");
367  CHECK_LOAD_FUNC(PCUCTXPOPCURRENT, dl_fn->cu_ctx_pop_current, "cuCtxPopCurrent_v2");
368  CHECK_LOAD_FUNC(PCUCTXDESTROY, dl_fn->cu_ctx_destroy, "cuCtxDestroy_v2");
369 
370  return 1;
371 
372 error:
373 
374  if (dl_fn->cuda_lib)
375  DL_CLOSE_FUNC(dl_fn->cuda_lib);
376 
377  dl_fn->cuda_lib = NULL;
378 
379  return 0;
380 }
381 
382 static av_cold int check_cuda_errors(AVCodecContext *avctx, CUresult err, const char *func)
383 {
384  if (err != CUDA_SUCCESS) {
385  av_log(avctx, AV_LOG_FATAL, ">> %s - failed with error code 0x%x\n", func, err);
386  return 0;
387  }
388  return 1;
389 }
390 #define check_cuda_errors(f) if (!check_cuda_errors(avctx, f, #f)) goto error
391 
393 {
394  int device_count = 0;
395  CUdevice cu_device = 0;
396  char gpu_name[128];
397  int smminor = 0, smmajor = 0;
398  int i, smver, target_smver;
399 
400  NvencContext *ctx = avctx->priv_data;
402 
403  switch (avctx->codec->id) {
404  case AV_CODEC_ID_H264:
405  target_smver = avctx->pix_fmt == AV_PIX_FMT_YUV444P ? 0x52 : 0x30;
406  break;
407  case AV_CODEC_ID_H265:
408  target_smver = 0x52;
409  break;
410  default:
411  av_log(avctx, AV_LOG_FATAL, "Unknown codec name\n");
412  goto error;
413  }
414 
415  if (!nvenc_dyload_cuda(avctx))
416  return 0;
417 
418  if (dl_fn->nvenc_device_count > 0)
419  return 1;
420 
421  check_cuda_errors(dl_fn->cu_init(0));
422 
423  check_cuda_errors(dl_fn->cu_device_get_count(&device_count));
424 
425  if (!device_count) {
426  av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
427  goto error;
428  }
429 
430  av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", device_count);
431 
432  dl_fn->nvenc_device_count = 0;
433 
434  for (i = 0; i < device_count; ++i) {
435  check_cuda_errors(dl_fn->cu_device_get(&cu_device, i));
436  check_cuda_errors(dl_fn->cu_device_get_name(gpu_name, sizeof(gpu_name), cu_device));
437  check_cuda_errors(dl_fn->cu_device_compute_capability(&smmajor, &smminor, cu_device));
438 
439  smver = (smmajor << 4) | smminor;
440 
441  av_log(avctx, AV_LOG_VERBOSE, "[ GPU #%d - < %s > has Compute SM %d.%d, NVENC %s ]\n", i, gpu_name, smmajor, smminor, (smver >= target_smver) ? "Available" : "Not Available");
442 
443  if (smver >= target_smver)
444  dl_fn->nvenc_devices[dl_fn->nvenc_device_count++] = cu_device;
445  }
446 
447  if (!dl_fn->nvenc_device_count) {
448  av_log(avctx, AV_LOG_FATAL, "No NVENC capable devices found\n");
449  goto error;
450  }
451 
452  return 1;
453 
454 error:
455 
456  dl_fn->nvenc_device_count = 0;
457 
458  return 0;
459 }
460 
462 {
463  PNVENCODEAPICREATEINSTANCE nvEncodeAPICreateInstance = 0;
464  NVENCSTATUS nvstatus;
465 
466  NvencContext *ctx = avctx->priv_data;
468 
469  if (!nvenc_check_cuda(avctx))
470  return 0;
471 
472  if (dl_fn->nvenc_lib)
473  return 1;
474 
475 #if defined(_WIN32)
476  if (sizeof(void*) == 8) {
477  dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI64.dll"));
478  } else {
479  dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI.dll"));
480  }
481 #else
482  dl_fn->nvenc_lib = dlopen("libnvidia-encode.so.1", RTLD_LAZY);
483 #endif
484 
485  if (!dl_fn->nvenc_lib) {
486  av_log(avctx, AV_LOG_FATAL, "Failed loading the nvenc library\n");
487  goto error;
488  }
489 
490  nvEncodeAPICreateInstance = (PNVENCODEAPICREATEINSTANCE)LOAD_FUNC(dl_fn->nvenc_lib, "NvEncodeAPICreateInstance");
491 
492  if (!nvEncodeAPICreateInstance) {
493  av_log(avctx, AV_LOG_FATAL, "Failed to load nvenc entrypoint\n");
494  goto error;
495  }
496 
497  dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
498 
499  nvstatus = nvEncodeAPICreateInstance(&dl_fn->nvenc_funcs);
500 
501  if (nvstatus != NV_ENC_SUCCESS) {
502  av_log(avctx, AV_LOG_FATAL, "Failed to create nvenc instance\n");
503  goto error;
504  }
505 
506  av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
507 
508  return 1;
509 
510 error:
511  if (dl_fn->nvenc_lib)
512  DL_CLOSE_FUNC(dl_fn->nvenc_lib);
513 
514  dl_fn->nvenc_lib = NULL;
515 
516  return 0;
517 }
518 
520 {
521  NvencContext *ctx = avctx->priv_data;
523 
524  DL_CLOSE_FUNC(dl_fn->nvenc_lib);
525  dl_fn->nvenc_lib = NULL;
526 
527  dl_fn->nvenc_device_count = 0;
528 
529  DL_CLOSE_FUNC(dl_fn->cuda_lib);
530  dl_fn->cuda_lib = NULL;
531 
532  dl_fn->cu_init = NULL;
533  dl_fn->cu_device_get_count = NULL;
534  dl_fn->cu_device_get = NULL;
535  dl_fn->cu_device_get_name = NULL;
537  dl_fn->cu_ctx_create = NULL;
538  dl_fn->cu_ctx_pop_current = NULL;
539  dl_fn->cu_ctx_destroy = NULL;
540 
541  av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
542 }
543 
545 {
546  NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encode_session_params = { 0 };
547  NV_ENC_PRESET_CONFIG preset_config = { 0 };
548  CUcontext cu_context_curr;
549  CUresult cu_res;
550  GUID encoder_preset = NV_ENC_PRESET_HQ_GUID;
551  GUID codec;
552  NVENCSTATUS nv_status = NV_ENC_SUCCESS;
553  AVCPBProperties *cpb_props;
554  int surfaceCount = 0;
555  int i, num_mbs;
556  int isLL = 0;
557  int lossless = 0;
558  int res = 0;
559  int dw, dh;
560  int qp_inter_p;
561 
562  NvencContext *ctx = avctx->priv_data;
564  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
565 
566  if (!nvenc_dyload_nvenc(avctx))
567  return AVERROR_EXTERNAL;
568 
569  ctx->last_dts = AV_NOPTS_VALUE;
570 
571  ctx->encode_config.version = NV_ENC_CONFIG_VER;
572  ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
573  preset_config.version = NV_ENC_PRESET_CONFIG_VER;
574  preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
575  encode_session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
576  encode_session_params.apiVersion = NVENCAPI_VERSION;
577 
578  if (ctx->gpu >= dl_fn->nvenc_device_count) {
579  av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->gpu, dl_fn->nvenc_device_count);
580  res = AVERROR(EINVAL);
581  goto error;
582  }
583 
584  ctx->cu_context = NULL;
585  cu_res = dl_fn->cu_ctx_create(&ctx->cu_context, 4, dl_fn->nvenc_devices[ctx->gpu]); // CU_CTX_SCHED_BLOCKING_SYNC=4, avoid CPU spins
586 
587  if (cu_res != CUDA_SUCCESS) {
588  av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res);
589  res = AVERROR_EXTERNAL;
590  goto error;
591  }
592 
593  cu_res = dl_fn->cu_ctx_pop_current(&cu_context_curr);
594 
595  if (cu_res != CUDA_SUCCESS) {
596  av_log(avctx, AV_LOG_FATAL, "Failed popping CUDA context: 0x%x\n", (int)cu_res);
597  res = AVERROR_EXTERNAL;
598  goto error;
599  }
600 
601  encode_session_params.device = ctx->cu_context;
602  encode_session_params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
603 
604  nv_status = p_nvenc->nvEncOpenEncodeSessionEx(&encode_session_params, &ctx->nvencoder);
605  if (nv_status != NV_ENC_SUCCESS) {
606  ctx->nvencoder = NULL;
607  av_log(avctx, AV_LOG_FATAL, "OpenEncodeSessionEx failed: 0x%x\n", (int)nv_status);
608  res = AVERROR_EXTERNAL;
609  goto error;
610  }
611 
612  if (ctx->preset) {
613  if (!strcmp(ctx->preset, "slow")) {
614  encoder_preset = NV_ENC_PRESET_HQ_GUID;
615  ctx->twopass = 1;
616  } else if (!strcmp(ctx->preset, "medium")) {
617  encoder_preset = NV_ENC_PRESET_HQ_GUID;
618  ctx->twopass = 0;
619  } else if (!strcmp(ctx->preset, "fast")) {
620  encoder_preset = NV_ENC_PRESET_HP_GUID;
621  ctx->twopass = 0;
622  } else if (!strcmp(ctx->preset, "hq")) {
623  encoder_preset = NV_ENC_PRESET_HQ_GUID;
624  } else if (!strcmp(ctx->preset, "hp")) {
625  encoder_preset = NV_ENC_PRESET_HP_GUID;
626  } else if (!strcmp(ctx->preset, "bd")) {
627  encoder_preset = NV_ENC_PRESET_BD_GUID;
628  } else if (!strcmp(ctx->preset, "ll")) {
629  encoder_preset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID;
630  isLL = 1;
631  } else if (!strcmp(ctx->preset, "llhp")) {
632  encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
633  isLL = 1;
634  } else if (!strcmp(ctx->preset, "llhq")) {
635  encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
636  isLL = 1;
637  } else if (!strcmp(ctx->preset, "lossless")) {
638  encoder_preset = NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID;
639  lossless = 1;
640  } else if (!strcmp(ctx->preset, "losslesshp")) {
641  encoder_preset = NV_ENC_PRESET_LOSSLESS_HP_GUID;
642  lossless = 1;
643  } else if (!strcmp(ctx->preset, "default")) {
644  encoder_preset = NV_ENC_PRESET_DEFAULT_GUID;
645  } else {
646  av_log(avctx, AV_LOG_FATAL, "Preset \"%s\" is unknown! Supported presets: slow, medium, high, hp, hq, bd, ll, llhp, llhq, lossless, losslesshp, default\n", ctx->preset);
647  res = AVERROR(EINVAL);
648  goto error;
649  }
650  }
651 
652  if (ctx->twopass < 0) {
653  ctx->twopass = isLL;
654  }
655 
656  switch (avctx->codec->id) {
657  case AV_CODEC_ID_H264:
658  codec = NV_ENC_CODEC_H264_GUID;
659  break;
660  case AV_CODEC_ID_H265:
661  codec = NV_ENC_CODEC_HEVC_GUID;
662  break;
663  default:
664  av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
665  res = AVERROR(EINVAL);
666  goto error;
667  }
668 
669  nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, codec, encoder_preset, &preset_config);
670  if (nv_status != NV_ENC_SUCCESS) {
671  av_log(avctx, AV_LOG_FATAL, "GetEncodePresetConfig failed: 0x%x\n", (int)nv_status);
672  res = AVERROR_EXTERNAL;
673  goto error;
674  }
675 
676  ctx->init_encode_params.encodeGUID = codec;
677  ctx->init_encode_params.encodeHeight = avctx->height;
678  ctx->init_encode_params.encodeWidth = avctx->width;
679 
680  if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den &&
681  (avctx->sample_aspect_ratio.num != 1 || avctx->sample_aspect_ratio.num != 1)) {
682  av_reduce(&dw, &dh,
683  avctx->width * avctx->sample_aspect_ratio.num,
684  avctx->height * avctx->sample_aspect_ratio.den,
685  1024 * 1024);
686  ctx->init_encode_params.darHeight = dh;
687  ctx->init_encode_params.darWidth = dw;
688  } else {
689  ctx->init_encode_params.darHeight = avctx->height;
690  ctx->init_encode_params.darWidth = avctx->width;
691  }
692 
693  // De-compensate for hardware, dubiously, trying to compensate for
694  // playback at 704 pixel width.
695  if (avctx->width == 720 &&
696  (avctx->height == 480 || avctx->height == 576)) {
697  av_reduce(&dw, &dh,
698  ctx->init_encode_params.darWidth * 44,
699  ctx->init_encode_params.darHeight * 45,
700  1024 * 1024);
701  ctx->init_encode_params.darHeight = dh;
702  ctx->init_encode_params.darWidth = dw;
703  }
704 
705  ctx->init_encode_params.frameRateNum = avctx->time_base.den;
706  ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
707 
708  num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4);
709  ctx->max_surface_count = (num_mbs >= 8160) ? 32 : 48;
710 
711  if (ctx->buffer_delay >= ctx->max_surface_count)
712  ctx->buffer_delay = ctx->max_surface_count - 1;
713 
714  ctx->init_encode_params.enableEncodeAsync = 0;
715  ctx->init_encode_params.enablePTD = 1;
716 
717  ctx->init_encode_params.presetGUID = encoder_preset;
718 
719  ctx->init_encode_params.encodeConfig = &ctx->encode_config;
720  memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
721  ctx->encode_config.version = NV_ENC_CONFIG_VER;
722 
723  if (avctx->refs >= 0) {
724  /* 0 means "let the hardware decide" */
725  switch (avctx->codec->id) {
726  case AV_CODEC_ID_H264:
727  ctx->encode_config.encodeCodecConfig.h264Config.maxNumRefFrames = avctx->refs;
728  break;
729  case AV_CODEC_ID_H265:
730  ctx->encode_config.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = avctx->refs;
731  break;
732  /* Earlier switch/case will return if unknown codec is passed. */
733  }
734  }
735 
736  if (avctx->gop_size > 0) {
737  if (avctx->max_b_frames >= 0) {
738  /* 0 is intra-only, 1 is I/P only, 2 is one B Frame, 3 two B frames, and so on. */
739  ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
740  }
741 
742  ctx->encode_config.gopLength = avctx->gop_size;
743  switch (avctx->codec->id) {
744  case AV_CODEC_ID_H264:
745  ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = avctx->gop_size;
746  break;
747  case AV_CODEC_ID_H265:
748  ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = avctx->gop_size;
749  break;
750  /* Earlier switch/case will return if unknown codec is passed. */
751  }
752  } else if (avctx->gop_size == 0) {
753  ctx->encode_config.frameIntervalP = 0;
754  ctx->encode_config.gopLength = 1;
755  switch (avctx->codec->id) {
756  case AV_CODEC_ID_H264:
757  ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = 1;
758  break;
759  case AV_CODEC_ID_H265:
760  ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = 1;
761  break;
762  /* Earlier switch/case will return if unknown codec is passed. */
763  }
764  }
765 
766  /* when there're b frames, set dts offset */
767  if (ctx->encode_config.frameIntervalP >= 2)
768  ctx->last_dts = -2;
769 
770  if (avctx->bit_rate > 0) {
771  ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
772  } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
773  ctx->encode_config.rcParams.maxBitRate = ctx->encode_config.rcParams.averageBitRate;
774  }
775 
776  if (avctx->rc_max_rate > 0)
777  ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
778 
779  if (lossless) {
780  if (avctx->codec->id == AV_CODEC_ID_H264)
781  ctx->encode_config.encodeCodecConfig.h264Config.qpPrimeYZeroTransformBypassFlag = 1;
782 
783  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
784  ctx->encode_config.rcParams.constQP.qpInterB = 0;
785  ctx->encode_config.rcParams.constQP.qpInterP = 0;
786  ctx->encode_config.rcParams.constQP.qpIntra = 0;
787 
788  avctx->qmin = -1;
789  avctx->qmax = -1;
790  } else if (ctx->cbr) {
791  if (!ctx->twopass) {
792  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
793  } else {
794  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_QUALITY;
795 
796  if (avctx->codec->id == AV_CODEC_ID_H264) {
797  ctx->encode_config.encodeCodecConfig.h264Config.adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
798  ctx->encode_config.encodeCodecConfig.h264Config.fmoMode = NV_ENC_H264_FMO_DISABLE;
799  }
800  }
801  } else if (avctx->global_quality > 0) {
802  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
803  ctx->encode_config.rcParams.constQP.qpInterB = avctx->global_quality;
804  ctx->encode_config.rcParams.constQP.qpInterP = avctx->global_quality;
805  ctx->encode_config.rcParams.constQP.qpIntra = avctx->global_quality;
806 
807  avctx->qmin = -1;
808  avctx->qmax = -1;
809  } else {
810  if (avctx->qmin >= 0 && avctx->qmax >= 0) {
811  ctx->encode_config.rcParams.enableMinQP = 1;
812  ctx->encode_config.rcParams.enableMaxQP = 1;
813 
814  ctx->encode_config.rcParams.minQP.qpInterB = avctx->qmin;
815  ctx->encode_config.rcParams.minQP.qpInterP = avctx->qmin;
816  ctx->encode_config.rcParams.minQP.qpIntra = avctx->qmin;
817 
818  ctx->encode_config.rcParams.maxQP.qpInterB = avctx->qmax;
819  ctx->encode_config.rcParams.maxQP.qpInterP = avctx->qmax;
820  ctx->encode_config.rcParams.maxQP.qpIntra = avctx->qmax;
821 
822  qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin
823 
824  if (ctx->twopass) {
825  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_VBR;
826  if (avctx->codec->id == AV_CODEC_ID_H264) {
827  ctx->encode_config.encodeCodecConfig.h264Config.adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
828  ctx->encode_config.encodeCodecConfig.h264Config.fmoMode = NV_ENC_H264_FMO_DISABLE;
829  }
830  } else {
831  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR_MINQP;
832  }
833  } else {
834  qp_inter_p = 26; // default to 26
835 
836  if (ctx->twopass) {
837  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_VBR;
838  } else {
839  ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
840  }
841  }
842 
843  ctx->encode_config.rcParams.enableInitialRCQP = 1;
844  ctx->encode_config.rcParams.initialRCQP.qpInterP = qp_inter_p;
845 
846  if(avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
847  ctx->encode_config.rcParams.initialRCQP.qpIntra = av_clip(
848  qp_inter_p * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
849  ctx->encode_config.rcParams.initialRCQP.qpInterB = av_clip(
850  qp_inter_p * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51);
851  } else {
852  ctx->encode_config.rcParams.initialRCQP.qpIntra = qp_inter_p;
853  ctx->encode_config.rcParams.initialRCQP.qpInterB = qp_inter_p;
854  }
855  }
856 
857  if (avctx->rc_buffer_size > 0) {
858  ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size;
859  } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
860  ctx->encode_config.rcParams.vbvBufferSize = 2 * ctx->encode_config.rcParams.averageBitRate;
861  }
862 
863  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
864  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
865  } else {
866  ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
867  }
868 
869  switch (avctx->codec->id) {
870  case AV_CODEC_ID_H264:
871  ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourDescriptionPresentFlag = 1;
872  ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag = 1;
873 
874  ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourMatrix = avctx->colorspace;
875  ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourPrimaries = avctx->color_primaries;
876  ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.transferCharacteristics = avctx->color_trc;
877 
878  ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG;
879 
880  ctx->encode_config.encodeCodecConfig.h264Config.sliceMode = 3;
881  ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData = 1;
882 
883  ctx->encode_config.encodeCodecConfig.h264Config.disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
884  ctx->encode_config.encodeCodecConfig.h264Config.repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
885 
886  if (!ctx->profile) {
887  switch (avctx->profile) {
889  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
890  break;
892  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
893  break;
895  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
896  break;
898  case FF_PROFILE_UNKNOWN:
899  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
900  break;
901  default:
902  av_log(avctx, AV_LOG_WARNING, "Unsupported profile requested, falling back to high\n");
903  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
904  break;
905  }
906  } else {
907  if (!strcmp(ctx->profile, "high")) {
908  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
909  avctx->profile = FF_PROFILE_H264_HIGH;
910  } else if (!strcmp(ctx->profile, "main")) {
911  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
912  avctx->profile = FF_PROFILE_H264_MAIN;
913  } else if (!strcmp(ctx->profile, "baseline")) {
914  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
916  } else if (!strcmp(ctx->profile, "high444p")) {
917  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
919  } else {
920  av_log(avctx, AV_LOG_FATAL, "Profile \"%s\" is unknown! Supported profiles: high, main, baseline\n", ctx->profile);
921  res = AVERROR(EINVAL);
922  goto error;
923  }
924  }
925 
926  // force setting profile as high444p if input is AV_PIX_FMT_YUV444P
927  if (avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
928  ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
930  }
931 
932  ctx->encode_config.encodeCodecConfig.h264Config.chromaFormatIDC = avctx->profile == FF_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1;
933 
934  if (ctx->level) {
935  res = input_string_to_uint32(avctx, nvenc_h264_level_pairs, ctx->level, &ctx->encode_config.encodeCodecConfig.h264Config.level);
936 
937  if (res) {
938  av_log(avctx, AV_LOG_FATAL, "Level \"%s\" is unknown! Supported levels: auto, 1, 1b, 1.1, 1.2, 1.3, 2, 2.1, 2.2, 3, 3.1, 3.2, 4, 4.1, 4.2, 5, 5.1\n", ctx->level);
939  goto error;
940  }
941  } else {
942  ctx->encode_config.encodeCodecConfig.h264Config.level = NV_ENC_LEVEL_AUTOSELECT;
943  }
944 
945  break;
946  case AV_CODEC_ID_H265:
947  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode = 3;
948  ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData = 1;
949 
950  ctx->encode_config.encodeCodecConfig.hevcConfig.disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
951  ctx->encode_config.encodeCodecConfig.hevcConfig.repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
952 
953  /* No other profile is supported in the current SDK version 5 */
954  ctx->encode_config.profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
955  avctx->profile = FF_PROFILE_HEVC_MAIN;
956 
957  if (ctx->level) {
958  res = input_string_to_uint32(avctx, nvenc_hevc_level_pairs, ctx->level, &ctx->encode_config.encodeCodecConfig.hevcConfig.level);
959 
960  if (res) {
961  av_log(avctx, AV_LOG_FATAL, "Level \"%s\" is unknown! Supported levels: auto, 1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 5.1, 5.2, 6, 6.1, 6.2\n", ctx->level);
962  goto error;
963  }
964  } else {
965  ctx->encode_config.encodeCodecConfig.hevcConfig.level = NV_ENC_LEVEL_AUTOSELECT;
966  }
967 
968  if (ctx->tier) {
969  if (!strcmp(ctx->tier, "main")) {
970  ctx->encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_MAIN;
971  } else if (!strcmp(ctx->tier, "high")) {
972  ctx->encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_HIGH;
973  } else {
974  av_log(avctx, AV_LOG_FATAL, "Tier \"%s\" is unknown! Supported tiers: main, high\n", ctx->tier);
975  res = AVERROR(EINVAL);
976  goto error;
977  }
978  }
979 
980  break;
981  /* Earlier switch/case will return if unknown codec is passed. */
982  }
983 
984  nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
985  if (nv_status != NV_ENC_SUCCESS) {
986  av_log(avctx, AV_LOG_FATAL, "InitializeEncoder failed: 0x%x\n", (int)nv_status);
987  res = AVERROR_EXTERNAL;
988  goto error;
989  }
990 
991  ctx->input_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->input_surfaces));
992 
993  if (!ctx->input_surfaces) {
994  res = AVERROR(ENOMEM);
995  goto error;
996  }
997 
998  ctx->output_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->output_surfaces));
999 
1000  if (!ctx->output_surfaces) {
1001  res = AVERROR(ENOMEM);
1002  goto error;
1003  }
1004 
1005  for (surfaceCount = 0; surfaceCount < ctx->max_surface_count; ++surfaceCount) {
1006  NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1007  NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1008  allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1009  allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1010 
1011  allocSurf.width = (avctx->width + 31) & ~31;
1012  allocSurf.height = (avctx->height + 31) & ~31;
1013 
1014  allocSurf.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
1015 
1016  switch (avctx->pix_fmt) {
1017  case AV_PIX_FMT_YUV420P:
1018  allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YV12_PL;
1019  break;
1020 
1021  case AV_PIX_FMT_NV12:
1022  allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
1023  break;
1024 
1025  case AV_PIX_FMT_YUV444P:
1026  allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YUV444_PL;
1027  break;
1028 
1029  default:
1030  av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format\n");
1031  res = AVERROR(EINVAL);
1032  goto error;
1033  }
1034 
1035  nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
1036  if (nv_status != NV_ENC_SUCCESS) {
1037  av_log(avctx, AV_LOG_FATAL, "CreateInputBuffer failed\n");
1038  res = AVERROR_EXTERNAL;
1039  goto error;
1040  }
1041 
1042  ctx->input_surfaces[surfaceCount].lockCount = 0;
1043  ctx->input_surfaces[surfaceCount].input_surface = allocSurf.inputBuffer;
1044  ctx->input_surfaces[surfaceCount].format = allocSurf.bufferFmt;
1045  ctx->input_surfaces[surfaceCount].width = allocSurf.width;
1046  ctx->input_surfaces[surfaceCount].height = allocSurf.height;
1047 
1048  /* 1MB is large enough to hold most output frames. NVENC increases this automaticaly if it's not enough. */
1049  allocOut.size = 1024 * 1024;
1050 
1051  allocOut.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
1052 
1053  nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
1054  if (nv_status != NV_ENC_SUCCESS) {
1055  av_log(avctx, AV_LOG_FATAL, "CreateBitstreamBuffer failed\n");
1056  ctx->output_surfaces[surfaceCount++].output_surface = NULL;
1057  res = AVERROR_EXTERNAL;
1058  goto error;
1059  }
1060 
1061  ctx->output_surfaces[surfaceCount].output_surface = allocOut.bitstreamBuffer;
1062  ctx->output_surfaces[surfaceCount].size = allocOut.size;
1063  ctx->output_surfaces[surfaceCount].busy = 0;
1064  }
1065 
1066  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1067  uint32_t outSize = 0;
1068  char tmpHeader[256];
1069  NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1070  payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1071 
1072  payload.spsppsBuffer = tmpHeader;
1073  payload.inBufferSize = sizeof(tmpHeader);
1074  payload.outSPSPPSPayloadSize = &outSize;
1075 
1076  nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
1077  if (nv_status != NV_ENC_SUCCESS) {
1078  av_log(avctx, AV_LOG_FATAL, "GetSequenceParams failed\n");
1079  goto error;
1080  }
1081 
1082  avctx->extradata_size = outSize;
1084 
1085  if (!avctx->extradata) {
1086  res = AVERROR(ENOMEM);
1087  goto error;
1088  }
1089 
1090  memcpy(avctx->extradata, tmpHeader, outSize);
1091  }
1092 
1093  if (ctx->encode_config.frameIntervalP > 1)
1094  avctx->has_b_frames = 2;
1095 
1096  if (ctx->encode_config.rcParams.averageBitRate > 0)
1097  avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
1098 
1099  cpb_props = ff_add_cpb_side_data(avctx);
1100  if (!cpb_props)
1101  return AVERROR(ENOMEM);
1102  cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate;
1103  cpb_props->avg_bitrate = avctx->bit_rate;
1104  cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize;
1105 
1106  return 0;
1107 
1108 error:
1109 
1110  for (i = 0; i < surfaceCount; ++i) {
1111  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface);
1112  if (ctx->output_surfaces[i].output_surface)
1113  p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface);
1114  }
1115 
1116  if (ctx->nvencoder)
1117  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
1118 
1119  if (ctx->cu_context)
1120  dl_fn->cu_ctx_destroy(ctx->cu_context);
1121 
1122  nvenc_unload_nvenc(avctx);
1123 
1124  ctx->nvencoder = NULL;
1125  ctx->cu_context = NULL;
1126 
1127  return res;
1128 }
1129 
1131 {
1132  NvencContext *ctx = avctx->priv_data;
1134  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1135  int i;
1136 
1137  av_freep(&ctx->timestamp_list.data);
1140 
1141  for (i = 0; i < ctx->max_surface_count; ++i) {
1142  p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface);
1143  p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface);
1144  }
1145  ctx->max_surface_count = 0;
1146 
1147  p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
1148  ctx->nvencoder = NULL;
1149 
1150  dl_fn->cu_ctx_destroy(ctx->cu_context);
1151  ctx->cu_context = NULL;
1152 
1153  nvenc_unload_nvenc(avctx);
1154 
1155  return 0;
1156 }
1157 
1159 {
1160  NvencContext *ctx = avctx->priv_data;
1162  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1163 
1164  uint32_t slice_mode_data;
1165  uint32_t *slice_offsets;
1166  NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
1167  NVENCSTATUS nv_status;
1168  int res = 0;
1169 
1170  switch (avctx->codec->id) {
1171  case AV_CODEC_ID_H264:
1172  slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1173  break;
1174  case AV_CODEC_ID_H265:
1175  slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1176  break;
1177  default:
1178  av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
1179  res = AVERROR(EINVAL);
1180  goto error;
1181  }
1182  slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));
1183 
1184  if (!slice_offsets)
1185  return AVERROR(ENOMEM);
1186 
1187  lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
1188 
1189  lock_params.doNotWait = 0;
1190  lock_params.outputBitstream = tmpoutsurf->output_surface;
1191  lock_params.sliceOffsets = slice_offsets;
1192 
1193  nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
1194  if (nv_status != NV_ENC_SUCCESS) {
1195  av_log(avctx, AV_LOG_ERROR, "Failed locking bitstream buffer\n");
1196  res = AVERROR_EXTERNAL;
1197  goto error;
1198  }
1199 
1200  if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes,0)) {
1201  p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1202  goto error;
1203  }
1204 
1205  memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
1206 
1207  nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1208  if (nv_status != NV_ENC_SUCCESS)
1209  av_log(avctx, AV_LOG_ERROR, "Failed unlocking bitstream buffer, expect the gates of mordor to open\n");
1210 
1211  switch (lock_params.pictureType) {
1212  case NV_ENC_PIC_TYPE_IDR:
1213  pkt->flags |= AV_PKT_FLAG_KEY;
1214 #if FF_API_CODED_FRAME
1216  case NV_ENC_PIC_TYPE_I:
1218  break;
1219  case NV_ENC_PIC_TYPE_P:
1221  break;
1222  case NV_ENC_PIC_TYPE_B:
1224  break;
1225  case NV_ENC_PIC_TYPE_BI:
1227  break;
1228  default:
1229  av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
1230  av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
1231  res = AVERROR_EXTERNAL;
1232  goto error;
1234 #endif
1235  }
1236 
1237  pkt->pts = lock_params.outputTimeStamp;
1239 
1240  /* when there're b frame(s), set dts offset */
1241  if (ctx->encode_config.frameIntervalP >= 2)
1242  pkt->dts -= 1;
1243 
1244  if (pkt->dts > pkt->pts)
1245  pkt->dts = pkt->pts;
1246 
1247  if (ctx->last_dts != AV_NOPTS_VALUE && pkt->dts <= ctx->last_dts)
1248  pkt->dts = ctx->last_dts + 1;
1249 
1250  ctx->last_dts = pkt->dts;
1251 
1252  av_free(slice_offsets);
1253 
1254  return 0;
1255 
1256 error:
1257 
1258  av_free(slice_offsets);
1260 
1261  return res;
1262 }
1263 
1265  const AVFrame *frame, int *got_packet)
1266 {
1267  NVENCSTATUS nv_status;
1268  NvencOutputSurface *tmpoutsurf;
1269  int res, i = 0;
1270 
1271  NvencContext *ctx = avctx->priv_data;
1273  NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1274 
1275  NV_ENC_PIC_PARAMS pic_params = { 0 };
1276  pic_params.version = NV_ENC_PIC_PARAMS_VER;
1277 
1278  if (frame) {
1279  NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1280  NvencInputSurface *inSurf = NULL;
1281 
1282  for (i = 0; i < ctx->max_surface_count; ++i) {
1283  if (!ctx->input_surfaces[i].lockCount) {
1284  inSurf = &ctx->input_surfaces[i];
1285  break;
1286  }
1287  }
1288 
1289  av_assert0(inSurf);
1290 
1291  inSurf->lockCount = 1;
1292 
1293  lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1294  lockBufferParams.inputBuffer = inSurf->input_surface;
1295 
1296  nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
1297  if (nv_status != NV_ENC_SUCCESS) {
1298  av_log(avctx, AV_LOG_ERROR, "Failed locking nvenc input buffer\n");
1299  return 0;
1300  }
1301 
1302  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
1303  uint8_t *buf = lockBufferParams.bufferDataPtr;
1304 
1305  av_image_copy_plane(buf, lockBufferParams.pitch,
1306  frame->data[0], frame->linesize[0],
1307  avctx->width, avctx->height);
1308 
1309  buf += inSurf->height * lockBufferParams.pitch;
1310 
1311  av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
1312  frame->data[2], frame->linesize[2],
1313  avctx->width >> 1, avctx->height >> 1);
1314 
1315  buf += (inSurf->height * lockBufferParams.pitch) >> 2;
1316 
1317  av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
1318  frame->data[1], frame->linesize[1],
1319  avctx->width >> 1, avctx->height >> 1);
1320  } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
1321  uint8_t *buf = lockBufferParams.bufferDataPtr;
1322 
1323  av_image_copy_plane(buf, lockBufferParams.pitch,
1324  frame->data[0], frame->linesize[0],
1325  avctx->width, avctx->height);
1326 
1327  buf += inSurf->height * lockBufferParams.pitch;
1328 
1329  av_image_copy_plane(buf, lockBufferParams.pitch,
1330  frame->data[1], frame->linesize[1],
1331  avctx->width, avctx->height >> 1);
1332  } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
1333  uint8_t *buf = lockBufferParams.bufferDataPtr;
1334 
1335  av_image_copy_plane(buf, lockBufferParams.pitch,
1336  frame->data[0], frame->linesize[0],
1337  avctx->width, avctx->height);
1338 
1339  buf += inSurf->height * lockBufferParams.pitch;
1340 
1341  av_image_copy_plane(buf, lockBufferParams.pitch,
1342  frame->data[1], frame->linesize[1],
1343  avctx->width, avctx->height);
1344 
1345  buf += inSurf->height * lockBufferParams.pitch;
1346 
1347  av_image_copy_plane(buf, lockBufferParams.pitch,
1348  frame->data[2], frame->linesize[2],
1349  avctx->width, avctx->height);
1350  } else {
1351  av_log(avctx, AV_LOG_FATAL, "Invalid pixel format!\n");
1352  return AVERROR(EINVAL);
1353  }
1354 
1355  nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, inSurf->input_surface);
1356  if (nv_status != NV_ENC_SUCCESS) {
1357  av_log(avctx, AV_LOG_FATAL, "Failed unlocking input buffer!\n");
1358  return AVERROR_EXTERNAL;
1359  }
1360 
1361  for (i = 0; i < ctx->max_surface_count; ++i)
1362  if (!ctx->output_surfaces[i].busy)
1363  break;
1364 
1365  if (i == ctx->max_surface_count) {
1366  inSurf->lockCount = 0;
1367  av_log(avctx, AV_LOG_FATAL, "No free output surface found!\n");
1368  return AVERROR_EXTERNAL;
1369  }
1370 
1371  ctx->output_surfaces[i].input_surface = inSurf;
1372 
1373  pic_params.inputBuffer = inSurf->input_surface;
1374  pic_params.bufferFmt = inSurf->format;
1375  pic_params.inputWidth = avctx->width;
1376  pic_params.inputHeight = avctx->height;
1377  pic_params.outputBitstream = ctx->output_surfaces[i].output_surface;
1378  pic_params.completionEvent = 0;
1379 
1380  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
1381  if (frame->top_field_first) {
1382  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
1383  } else {
1384  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
1385  }
1386  } else {
1387  pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
1388  }
1389 
1390  pic_params.encodePicFlags = 0;
1391  pic_params.inputTimeStamp = frame->pts;
1392  pic_params.inputDuration = 0;
1393  switch (avctx->codec->id) {
1394  case AV_CODEC_ID_H264:
1395  pic_params.codecPicParams.h264PicParams.sliceMode = ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
1396  pic_params.codecPicParams.h264PicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1397  break;
1398  case AV_CODEC_ID_H265:
1399  pic_params.codecPicParams.hevcPicParams.sliceMode = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
1400  pic_params.codecPicParams.hevcPicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1401  break;
1402  default:
1403  av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
1404  return AVERROR(EINVAL);
1405  }
1406 
1407  res = timestamp_queue_enqueue(&ctx->timestamp_list, frame->pts);
1408 
1409  if (res)
1410  return res;
1411  } else {
1412  pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
1413  }
1414 
1415  nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
1416 
1417  if (frame && nv_status == NV_ENC_ERR_NEED_MORE_INPUT) {
1419 
1420  if (res)
1421  return res;
1422 
1423  ctx->output_surfaces[i].busy = 1;
1424  }
1425 
1426  if (nv_status != NV_ENC_SUCCESS && nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
1427  av_log(avctx, AV_LOG_ERROR, "EncodePicture failed!\n");
1428  return AVERROR_EXTERNAL;
1429  }
1430 
1431  if (nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
1432  while (ctx->output_surface_queue.count) {
1433  tmpoutsurf = out_surf_queue_dequeue(&ctx->output_surface_queue);
1434  res = out_surf_queue_enqueue(&ctx->output_surface_ready_queue, tmpoutsurf);
1435 
1436  if (res)
1437  return res;
1438  }
1439 
1440  if (frame) {
1442 
1443  if (res)
1444  return res;
1445 
1446  ctx->output_surfaces[i].busy = 1;
1447  }
1448  }
1449 
1452 
1453  res = process_output_surface(avctx, pkt, tmpoutsurf);
1454 
1455  if (res)
1456  return res;
1457 
1458  tmpoutsurf->busy = 0;
1459  av_assert0(tmpoutsurf->input_surface->lockCount);
1460  tmpoutsurf->input_surface->lockCount--;
1461 
1462  *got_packet = 1;
1463  } else {
1464  *got_packet = 0;
1465  }
1466 
1467  return 0;
1468 }
1469 
1470 static const enum AVPixelFormat pix_fmts_nvenc[] = {
1475 };
1476 
1477 #define OFFSET(x) offsetof(NvencContext, x)
1478 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1479 static const AVOption options[] = {
1480  { "preset", "Set the encoding preset (one of slow = hq 2pass, medium = hq, fast = hp, hq, hp, bd, ll, llhq, llhp, default)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE },
1481  { "profile", "Set the encoding profile (high, main, baseline or high444p)", OFFSET(profile), AV_OPT_TYPE_STRING, { .str = "main" }, 0, 0, VE },
1482  { "level", "Set the encoding level restriction (auto, 1.0, 1.0b, 1.1, 1.2, ..., 4.2, 5.0, 5.1)", OFFSET(level), AV_OPT_TYPE_STRING, { .str = "auto" }, 0, 0, VE },
1483  { "tier", "Set the encoding tier (main or high)", OFFSET(tier), AV_OPT_TYPE_STRING, { .str = "main" }, 0, 0, VE },
1484  { "cbr", "Use cbr encoding mode", OFFSET(cbr), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1485  { "2pass", "Use 2pass encoding mode", OFFSET(twopass), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
1486  { "gpu", "Selects which NVENC capable GPU to use. First GPU is 0, second is 1, and so on.", OFFSET(gpu), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
1487  { "delay", "Delays frame output by the given amount of frames.", OFFSET(buffer_delay), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 0, INT_MAX, VE },
1488  { NULL }
1489 };
1490 
1491 static const AVCodecDefault nvenc_defaults[] = {
1492  { "b", "2M" },
1493  { "qmin", "-1" },
1494  { "qmax", "-1" },
1495  { "qdiff", "-1" },
1496  { "qblur", "-1" },
1497  { "qcomp", "-1" },
1498  { "g", "250" },
1499  { "bf", "0" },
1500  { NULL },
1501 };
1502 
1503 #if CONFIG_NVENC_ENCODER
1504 static const AVClass nvenc_class = {
1505  .class_name = "nvenc",
1506  .item_name = av_default_item_name,
1507  .option = options,
1508  .version = LIBAVUTIL_VERSION_INT,
1509 };
1510 
1511 AVCodec ff_nvenc_encoder = {
1512  .name = "nvenc",
1513  .long_name = NULL_IF_CONFIG_SMALL("NVIDIA NVENC h264 encoder"),
1514  .type = AVMEDIA_TYPE_VIDEO,
1515  .id = AV_CODEC_ID_H264,
1516  .priv_data_size = sizeof(NvencContext),
1518  .encode2 = nvenc_encode_frame,
1519  .close = nvenc_encode_close,
1520  .capabilities = AV_CODEC_CAP_DELAY,
1521  .priv_class = &nvenc_class,
1522  .defaults = nvenc_defaults,
1524 };
1525 #endif
1526 
1527 /* Add an alias for nvenc_h264 */
1528 #if CONFIG_NVENC_H264_ENCODER
1529 static const AVClass nvenc_h264_class = {
1530  .class_name = "nvenc_h264",
1531  .item_name = av_default_item_name,
1532  .option = options,
1533  .version = LIBAVUTIL_VERSION_INT,
1534 };
1535 
1536 AVCodec ff_nvenc_h264_encoder = {
1537  .name = "nvenc_h264",
1538  .long_name = NULL_IF_CONFIG_SMALL("NVIDIA NVENC h264 encoder"),
1539  .type = AVMEDIA_TYPE_VIDEO,
1540  .id = AV_CODEC_ID_H264,
1541  .priv_data_size = sizeof(NvencContext),
1543  .encode2 = nvenc_encode_frame,
1544  .close = nvenc_encode_close,
1545  .capabilities = AV_CODEC_CAP_DELAY,
1546  .priv_class = &nvenc_h264_class,
1547  .defaults = nvenc_defaults,
1549 };
1550 #endif
1551 
1552 #if CONFIG_NVENC_HEVC_ENCODER
1553 static const AVClass nvenc_hevc_class = {
1554  .class_name = "nvenc_hevc",
1555  .item_name = av_default_item_name,
1556  .option = options,
1557  .version = LIBAVUTIL_VERSION_INT,
1558 };
1559 
1560 AVCodec ff_nvenc_hevc_encoder = {
1561  .name = "nvenc_hevc",
1562  .long_name = NULL_IF_CONFIG_SMALL("NVIDIA NVENC hevc encoder"),
1563  .type = AVMEDIA_TYPE_VIDEO,
1564  .id = AV_CODEC_ID_H265,
1565  .priv_data_size = sizeof(NvencContext),
1567  .encode2 = nvenc_encode_frame,
1568  .close = nvenc_encode_close,
1569  .capabilities = AV_CODEC_CAP_DELAY,
1570  .priv_class = &nvenc_hevc_class,
1571  .defaults = nvenc_defaults,
1573 };
1574 #endif
#define FF_PROFILE_H264_MAIN
Definition: avcodec.h:3062
#define NULL
Definition: coverity.c:32
AVClass * avclass
Definition: nvenc.c:140
const struct AVCodec * codec
Definition: avcodec.h:1541
BI type.
Definition: avutil.h:272
void * nvencoder
Definition: nvenc.c:157
static av_cold int nvenc_check_cuda(AVCodecContext *avctx)
Definition: nvenc.c:392
uint32_t pos
Definition: nvenc.c:103
uint32_t count
Definition: nvenc.c:104
int twopass
Definition: nvenc.c:164
NvencOutputSurface * surface
Definition: nvenc.c:95
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
NvencData * data
Definition: nvenc.c:101
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:775
AVFormatContext * ctx
Definition: movenc-test.c:48
#define OFFSET(x)
Definition: nvenc.c:1477
NvencDataList output_surface_queue
Definition: nvenc.c:152
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
NvencInputSurface * input_surfaces
Definition: nvenc.c:149
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:1597
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
memory handling functions
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: avcodec.h:1203
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:1810
NVENCSTATUS(NVENCAPI * PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList)
Definition: nvenc.c:68
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2262
int num
numerator
Definition: rational.h:44
PCUCTXDESTROY cu_ctx_destroy
Definition: nvenc.c:117
int gpu
Definition: nvenc.c:165
char * profile
Definition: nvenc.c:160
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.c:119
NvencDynLoadFunctions nvenc_dload_funcs
Definition: nvenc.c:142
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:1935
const char * str
Definition: nvenc.c:134
int64_t last_dts
Definition: nvenc.c:155
PCUDEVICEGETNAME cu_device_get_name
Definition: nvenc.c:113
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
char * level
Definition: nvenc.c:161
CUresult(CUDAAPI * PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev)
Definition: nvenc.c:63
static const AVOption options[]
Definition: nvenc.c:1479
CUresult
Definition: nvenc.c:53
NvencDataList timestamp_list
Definition: nvenc.c:154
static AVPacket pkt
int profile
profile
Definition: avcodec.h:3028
AVCodec.
Definition: avcodec.h:3392
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1868
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:3070
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1661
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:881
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
CUresult(CUDAAPI * PCUDEVICEGET)(CUdevice *device, int ordinal)
Definition: nvenc.c:61
CUcontext cu_context
Definition: nvenc.c:146
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:3060
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
#define CHECK_LOAD_FUNC(t, f, s)
Definition: nvenc.c:333
AVOptions.
PCUDEVICEGETCOUNT cu_device_get_count
Definition: nvenc.c:111
int max_surface_count
Definition: nvenc.c:148
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:1819
Multithreading support functions.
#define FF_PROFILE_HEVC_MAIN
Definition: avcodec.h:3107
PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability
Definition: nvenc.c:114
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:262
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
uint32_t size
Definition: nvenc.c:105
NV_ENC_INITIALIZE_PARAMS init_encode_params
Definition: nvenc.c:144
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1467
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:1219
#define check_cuda_errors(f)
Definition: nvenc.c:390
NvencOutputSurface * output_surfaces
Definition: nvenc.c:150
static av_cold int nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:1130
#define LOAD_FUNC(l, s)
Definition: nvenc.c:49
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
NvencDataList output_surface_ready_queue
Definition: nvenc.c:153
enum AVCodecID id
Definition: avcodec.h:3406
static int input_string_to_uint32(AVCodecContext *avctx, const NvencValuePair *pair, const char *input, uint32_t *output)
Definition: nvenc.c:220
#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:1846
static const uint16_t mask[17]
Definition: lzw.c:38
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
char * tier
Definition: nvenc.c:162
int qmax
maximum quantizer
Definition: avcodec.h:2473
void * HMODULE
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
PCUDEVICEGET cu_device_get
Definition: nvenc.c:112
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:3064
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1627
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:91
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
float i_quant_factor
qscale factor between P and I-frames If > 0 then the last p frame quantizer will be used (q= lastp_q*...
Definition: avcodec.h:1861
char * preset
Definition: nvenc.c:159
GLsizei count
Definition: opengl_enc.c:109
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.c:78
#define CUDAAPI
Definition: nvenc.c:42
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2500
static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencOutputSurface *tmpoutsurf)
Definition: nvenc.c:1158
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
common internal API header
int refs
number of reference frames
Definition: avcodec.h:2205
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:252
#define VE
Definition: nvenc.c:1478
int width
picture width / height.
Definition: avcodec.h:1711
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:3029
void * CUcontext
Definition: nvenc.c:57
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2241
static const NvencValuePair nvenc_h264_level_pairs[]
Definition: nvenc.c:169
CUresult(CUDAAPI * PCUDEVICEGETCOUNT)(int *count)
Definition: nvenc.c:60
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1670
static int out_surf_queue_enqueue(NvencDataList *queue, NvencOutputSurface *surface)
Definition: nvenc.c:297
CUresult(CUDAAPI * PCUDEVICEGETNAME)(char *name, int len, CUdevice dev)
Definition: nvenc.c:62
#define LoadLibrary(x)
Definition: avisynth.c:43
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:443
static av_cold int nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:544
static NvencOutputSurface * out_surf_queue_dequeue(NvencDataList *queue)
Definition: nvenc.c:305
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:1198
int buffer_delay
Definition: nvenc.c:166
PCUCTXCREATE cu_ctx_create
Definition: nvenc.c:115
NV_ENC_CONFIG encode_config
Definition: nvenc.c:145
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
static av_cold void nvenc_unload_nvenc(AVCodecContext *avctx)
Definition: nvenc.c:519
uint32_t num
Definition: nvenc.c:135
main external API structure.
Definition: avcodec.h:1532
int qmin
minimum quantizer
Definition: avcodec.h:2466
static int timestamp_queue_enqueue(NvencDataList *queue, int64_t timestamp)
Definition: nvenc.c:315
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1648
static av_cold int nvenc_dyload_cuda(AVCodecContext *avctx)
Definition: nvenc.c:342
Describe the class of an AVClass context structure.
Definition: log.h:67
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2255
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2248
static const NvencValuePair nvenc_hevc_level_pairs[]
Definition: nvenc.c:196
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:67
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1838
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1621
static int64_t timestamp_queue_dequeue(NvencDataList *queue)
Definition: nvenc.c:323
int cbr
Definition: nvenc.c:163
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
mfxU16 profile
Definition: qsvenc.c:42
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1613
static av_cold int nvenc_dyload_nvenc(AVCodecContext *avctx)
Definition: nvenc.c:461
static int flags
Definition: cpu.c:47
static const AVCodecDefault nvenc_defaults[]
Definition: nvenc.c:1491
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
uint8_t level
Definition: svq3.c:150
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:783
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1737
preset
Definition: vf_curves.c:46
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
common internal api header.
CUresult(CUDAAPI * PCUCTXDESTROY)(CUcontext ctx)
Definition: nvenc.c:66
PCUCTXPOPCURRENT cu_ctx_pop_current
Definition: nvenc.c:116
#define DL_CLOSE_FUNC(l)
Definition: nvenc.c:50
Bi-dir predicted.
Definition: avutil.h:268
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2945
CUdevice nvenc_devices[16]
Definition: nvenc.c:121
static enum AVPixelFormat pix_fmts_nvenc[]
Definition: nvenc.c:1470
int den
denominator
Definition: rational.h:45
AVCPBProperties * ff_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition: utils.c:3533
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:635
void * priv_data
Definition: avcodec.h:1574
CUresult(CUDAAPI * PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev)
Definition: nvenc.c:64
CUresult(CUDAAPI * PCUINIT)(unsigned int Flags)
Definition: nvenc.c:59
#define av_free(p)
union NvencData::@78 u
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:317
int CUdevice
Definition: nvenc.c:56
int len
int avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: avcodec.h:1213
NV_ENC_INPUT_PTR input_surface
Definition: nvenc.c:72
NvencInputSurface * input_surface
Definition: nvenc.c:86
static int nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: nvenc.c:1264
NV_ENC_OUTPUT_PTR output_surface
Definition: nvenc.c:83
int64_t timestamp
Definition: nvenc.c:94
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1466
#define av_freep(p)
#define AV_CODEC_ID_H265
Definition: avcodec.h:282
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
static NvencData * data_queue_dequeue(NvencDataList *queue)
Definition: nvenc.c:232
static const AVCodecDefault defaults[]
Definition: dcaenc.c:975
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:287
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1444
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460
CUresult(CUDAAPI * PCUCTXPOPCURRENT)(CUcontext *pctx)
Definition: nvenc.c:65
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
Predicted.
Definition: avutil.h:267
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2523
const char * name
Definition: opengl_enc.c:103
static int data_queue_enqueue(NvencDataList *queue, NvencData *data)
Definition: nvenc.c:252