]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/nvenc.c
Merge commit '7c6eb0a1b7bf1aac7f033a7ec6d8cacc3b5c2615'
[ffmpeg] / libavcodec / nvenc.c
index bfb5856a7fbe307426eafaa79f17a1acfe4c4102..c5b63bf45d981464c1a6ab6471a5d5e9e2aa1004 100644 (file)
 /*
- * NVIDIA NVENC Support
- * Copyright (C) 2015 Luca Barbato
+ * H.264 hardware encoding using nvidia nvenc
+ * Copyright (c) 2014 Timo Rothenpieler <timo@rothenpieler.org>
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "config.h"
-
-#include <cuda.h>
-#include <nvEncodeAPI.h>
-#include <string.h>
-
-#define CUDA_LIBNAME "libcuda.so"
-
-#if HAVE_DLFCN_H
-#include <dlfcn.h>
-
-#define NVENC_LIBNAME "libnvidia-encode.so"
-
-#elif HAVE_WINDOWS_H
+#if defined(_WIN32)
 #include <windows.h>
-
-#if ARCH_X86_64
-#define NVENC_LIBNAME "nvEncodeAPI64.dll"
 #else
-#define NVENC_LIBNAME "nvEncodeAPI.dll"
+#include <dlfcn.h>
 #endif
 
-#define dlopen(filename, flags) LoadLibrary((filename))
-#define dlsym(handle, symbol)   GetProcAddress(handle, symbol)
-#define dlclose(handle)         FreeLibrary(handle)
-#endif
+#include <nvEncodeAPI.h>
 
-#include "libavutil/common.h"
+#include "libavutil/internal.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/avassert.h"
+#include "libavutil/opt.h"
 #include "libavutil/mem.h"
 #include "avcodec.h"
 #include "internal.h"
-#include "nvenc.h"
-
-#define NVENC_CAP 0x30
-#define BITSTREAM_BUFFER_SIZE 1024 * 1024
-
-#define LOAD_LIBRARY(l, path)                   \
-    do {                                        \
-        if (!((l) = dlopen(path, RTLD_LAZY))) { \
-            av_log(avctx, AV_LOG_ERROR,         \
-                   "Cannot load %s\n",          \
-                   path);                       \
-            return AVERROR_UNKNOWN;             \
-        }                                       \
-    } while (0)
-
-#define LOAD_SYMBOL(fun, lib, symbol)        \
-    do {                                     \
-        if (!((fun) = dlsym(lib, symbol))) { \
-            av_log(avctx, AV_LOG_ERROR,      \
-                   "Cannot load %s\n",       \
-                   symbol);                  \
-            return AVERROR_UNKNOWN;          \
-        }                                    \
-    } while (0)
-
-static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
-{
-    NVENCContext *ctx         = avctx->priv_data;
-    NVENCLibraryContext *nvel = &ctx->nvel;
-    PNVENCODEAPICREATEINSTANCE nvenc_create_instance;
+#include "thread.h"
 
-    LOAD_LIBRARY(nvel->cuda, CUDA_LIBNAME);
+#if defined(_WIN32)
+#define CUDAAPI __stdcall
+#else
+#define CUDAAPI
+#endif
 
-    LOAD_SYMBOL(nvel->cu_init, nvel->cuda, "cuInit");
-    LOAD_SYMBOL(nvel->cu_device_get_count, nvel->cuda, "cuDeviceGetCount");
-    LOAD_SYMBOL(nvel->cu_device_get, nvel->cuda, "cuDeviceGet");
-    LOAD_SYMBOL(nvel->cu_device_get_name, nvel->cuda, "cuDeviceGetName");
-    LOAD_SYMBOL(nvel->cu_device_compute_capability, nvel->cuda,
-                "cuDeviceComputeCapability");
-    LOAD_SYMBOL(nvel->cu_ctx_create, nvel->cuda, "cuCtxCreate_v2");
-    LOAD_SYMBOL(nvel->cu_ctx_pop_current, nvel->cuda, "cuCtxPopCurrent_v2");
-    LOAD_SYMBOL(nvel->cu_ctx_destroy, nvel->cuda, "cuCtxDestroy_v2");
+#if defined(_WIN32)
+#define LOAD_FUNC(l, s) GetProcAddress(l, s)
+#define DL_CLOSE_FUNC(l) FreeLibrary(l)
+#else
+#define LOAD_FUNC(l, s) dlsym(l, s)
+#define DL_CLOSE_FUNC(l) dlclose(l)
+#endif
 
-    LOAD_LIBRARY(nvel->nvenc, NVENC_LIBNAME);
+typedef enum cudaError_enum {
+    CUDA_SUCCESS = 0
+} CUresult;
+typedef int CUdevice;
+typedef void* CUcontext;
 
-    LOAD_SYMBOL(nvenc_create_instance, nvel->nvenc,
-                "NvEncodeAPICreateInstance");
+typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
+typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
+typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
+typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
+typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
+typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
+typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
+typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
 
-    nvel->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
+typedef NVENCSTATUS (NVENCAPI* PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
 
-    if ((nvenc_create_instance(&nvel->nvenc_funcs)) != NV_ENC_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR, "Cannot create the NVENC instance");
-        return AVERROR_UNKNOWN;
-    }
+typedef struct NvencInputSurface
+{
+    NV_ENC_INPUT_PTR input_surface;
+    int width;
+    int height;
 
-    return 0;
-}
+    int lockCount;
 
-static int nvenc_open_session(AVCodecContext *avctx)
+    NV_ENC_BUFFER_FORMAT format;
+} NvencInputSurface;
+
+typedef struct NvencOutputSurface
 {
-    NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
-    NVENCContext *ctx                           = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv             = &ctx->nvel.nvenc_funcs;
-    int ret;
-
-    params.version    = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
-    params.apiVersion = NVENCAPI_VERSION;
-    params.device     = ctx->cu_context;
-    params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
-
-    ret = nv->nvEncOpenEncodeSessionEx(&params, &ctx->nvenc_ctx);
-    if (ret != NV_ENC_SUCCESS) {
-        ctx->nvenc_ctx = NULL;
-        av_log(avctx, AV_LOG_ERROR,
-               "Cannot open the NVENC Session\n");
-        return AVERROR_UNKNOWN;
-    }
+    NV_ENC_OUTPUT_PTR output_surface;
+    int size;
 
-    return 0;
-}
+    NvencInputSurface* input_surface;
+
+    int busy;
+} NvencOutputSurface;
 
-static int nvenc_check_codec_support(AVCodecContext *avctx)
+typedef struct NvencData
 {
-    NVENCContext *ctx               = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
-    int i, ret, count = 0;
-    GUID *guids = NULL;
+    union {
+        int64_t timestamp;
+        NvencOutputSurface *surface;
+    } u;
+} NvencData;
 
-    ret = nv->nvEncGetEncodeGUIDCount(ctx->nvenc_ctx, &count);
+typedef struct NvencDataList
+{
+    NvencData* data;
 
-    if (ret != NV_ENC_SUCCESS || !count)
-        return AVERROR(ENOSYS);
+    uint32_t pos;
+    uint32_t count;
+    uint32_t size;
+} NvencDataList;
 
-    guids = av_malloc(count * sizeof(GUID));
-    if (!guids)
-        return AVERROR(ENOMEM);
+typedef struct NvencDynLoadFunctions
+{
+    PCUINIT cu_init;
+    PCUDEVICEGETCOUNT cu_device_get_count;
+    PCUDEVICEGET cu_device_get;
+    PCUDEVICEGETNAME cu_device_get_name;
+    PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
+    PCUCTXCREATE cu_ctx_create;
+    PCUCTXPOPCURRENT cu_ctx_pop_current;
+    PCUCTXDESTROY cu_ctx_destroy;
+
+    NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
+    int nvenc_device_count;
+    CUdevice nvenc_devices[16];
+
+#if defined(_WIN32)
+    HMODULE cuda_lib;
+    HMODULE nvenc_lib;
+#else
+    void* cuda_lib;
+    void* nvenc_lib;
+#endif
+} NvencDynLoadFunctions;
 
-    ret = nv->nvEncGetEncodeGUIDs(ctx->nvenc_ctx, guids, count, &count);
-    if (ret != NV_ENC_SUCCESS) {
-        ret = AVERROR(ENOSYS);
-        goto fail;
-    }
+typedef struct NvencValuePair
+{
+    const char *str;
+    uint32_t num;
+} NvencValuePair;
 
-    ret = AVERROR(ENOSYS);
-    for (i = 0; i < count; i++) {
-        if (!memcmp(&guids[i], &ctx->params.encodeGUID, sizeof(*guids))) {
-            ret = 0;
-            break;
+typedef struct NvencContext
+{
+    AVClass *avclass;
+
+    NvencDynLoadFunctions nvenc_dload_funcs;
+
+    NV_ENC_INITIALIZE_PARAMS init_encode_params;
+    NV_ENC_CONFIG encode_config;
+    CUcontext cu_context;
+
+    int max_surface_count;
+    NvencInputSurface *input_surfaces;
+    NvencOutputSurface *output_surfaces;
+
+    NvencDataList output_surface_queue;
+    NvencDataList output_surface_ready_queue;
+    NvencDataList timestamp_list;
+    int64_t last_dts;
+
+    void *nvencoder;
+
+    char *preset;
+    char *profile;
+    char *level;
+    char *tier;
+    int cbr;
+    int twopass;
+    int gpu;
+    int buffer_delay;
+} NvencContext;
+
+static const NvencValuePair nvenc_h264_level_pairs[] = {
+    { "auto", NV_ENC_LEVEL_AUTOSELECT },
+    { "1"   , NV_ENC_LEVEL_H264_1     },
+    { "1.0" , NV_ENC_LEVEL_H264_1     },
+    { "1b"  , NV_ENC_LEVEL_H264_1b    },
+    { "1.0b", NV_ENC_LEVEL_H264_1b    },
+    { "1.1" , NV_ENC_LEVEL_H264_11    },
+    { "1.2" , NV_ENC_LEVEL_H264_12    },
+    { "1.3" , NV_ENC_LEVEL_H264_13    },
+    { "2"   , NV_ENC_LEVEL_H264_2     },
+    { "2.0" , NV_ENC_LEVEL_H264_2     },
+    { "2.1" , NV_ENC_LEVEL_H264_21    },
+    { "2.2" , NV_ENC_LEVEL_H264_22    },
+    { "3"   , NV_ENC_LEVEL_H264_3     },
+    { "3.0" , NV_ENC_LEVEL_H264_3     },
+    { "3.1" , NV_ENC_LEVEL_H264_31    },
+    { "3.2" , NV_ENC_LEVEL_H264_32    },
+    { "4"   , NV_ENC_LEVEL_H264_4     },
+    { "4.0" , NV_ENC_LEVEL_H264_4     },
+    { "4.1" , NV_ENC_LEVEL_H264_41    },
+    { "4.2" , NV_ENC_LEVEL_H264_42    },
+    { "5"   , NV_ENC_LEVEL_H264_5     },
+    { "5.0" , NV_ENC_LEVEL_H264_5     },
+    { "5.1" , NV_ENC_LEVEL_H264_51    },
+    { NULL }
+};
+
+static const NvencValuePair nvenc_hevc_level_pairs[] = {
+    { "auto", NV_ENC_LEVEL_AUTOSELECT },
+    { "1"   , NV_ENC_LEVEL_HEVC_1     },
+    { "1.0" , NV_ENC_LEVEL_HEVC_1     },
+    { "2"   , NV_ENC_LEVEL_HEVC_2     },
+    { "2.0" , NV_ENC_LEVEL_HEVC_2     },
+    { "2.1" , NV_ENC_LEVEL_HEVC_21    },
+    { "3"   , NV_ENC_LEVEL_HEVC_3     },
+    { "3.0" , NV_ENC_LEVEL_HEVC_3     },
+    { "3.1" , NV_ENC_LEVEL_HEVC_31    },
+    { "4"   , NV_ENC_LEVEL_HEVC_4     },
+    { "4.0" , NV_ENC_LEVEL_HEVC_4     },
+    { "4.1" , NV_ENC_LEVEL_HEVC_41    },
+    { "5"   , NV_ENC_LEVEL_HEVC_5     },
+    { "5.0" , NV_ENC_LEVEL_HEVC_5     },
+    { "5.1" , NV_ENC_LEVEL_HEVC_51    },
+    { "5.2" , NV_ENC_LEVEL_HEVC_52    },
+    { "6"   , NV_ENC_LEVEL_HEVC_6     },
+    { "6.0" , NV_ENC_LEVEL_HEVC_6     },
+    { "6.1" , NV_ENC_LEVEL_HEVC_61    },
+    { "6.2" , NV_ENC_LEVEL_HEVC_62    },
+    { NULL }
+};
+
+static int input_string_to_uint32(AVCodecContext *avctx, const NvencValuePair *pair, const char *input, uint32_t *output)
+{
+    for (; pair->str; ++pair) {
+        if (!strcmp(input, pair->str)) {
+            *output = pair->num;
+            return 0;
         }
     }
 
-fail:
-    av_free(guids);
-
-    return ret;
+    return AVERROR(EINVAL);
 }
 
-static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
+static NvencData* data_queue_dequeue(NvencDataList* queue)
 {
-    NVENCContext *ctx               = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
-    NV_ENC_CAPS_PARAM params        = { 0 };
-    int ret, val = 0;
+    uint32_t mask;
+    uint32_t read_pos;
 
-    params.version     = NV_ENC_CAPS_PARAM_VER;
-    params.capsToQuery = cap;
+    av_assert0(queue);
+    av_assert0(queue->size);
+    av_assert0(queue->data);
 
-    ret = nv->nvEncGetEncodeCaps(ctx->nvenc_ctx, ctx->params.encodeGUID, &params, &val);
+    if (!queue->count)
+        return NULL;
 
-    if (ret == NV_ENC_SUCCESS)
-        return val;
-    return 0;
+    /* Size always is a multiple of two */
+    mask = queue->size - 1;
+    read_pos = (queue->pos - queue->count) & mask;
+    queue->count--;
+
+    return &queue->data[read_pos];
 }
 
-static int nvenc_check_capabilities(AVCodecContext *avctx)
+static int data_queue_enqueue(NvencDataList* queue, NvencData *data)
 {
-    int ret;
+    NvencDataList new_queue;
+    NvencData* tmp_data;
+    uint32_t mask;
 
-    ret = nvenc_check_codec_support(avctx);
-    if (ret < 0) {
-        av_log(avctx, AV_LOG_VERBOSE, "Codec not supported\n");
-        return ret;
-    }
-
-    ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_YUV444_ENCODE);
-    if (avctx->pix_fmt == AV_PIX_FMT_YUV444P && ret <= 0) {
-        av_log(avctx, AV_LOG_VERBOSE, "YUV444P not supported\n");
-        return AVERROR(ENOSYS);
-    }
+    if (!queue->size) {
+        /* size always has to be a multiple of two */
+        queue->size = 4;
+        queue->pos = 0;
+        queue->count = 0;
 
-    ret = nvenc_check_cap(avctx, NV_ENC_CAPS_WIDTH_MAX);
-    if (ret < avctx->width) {
-        av_log(avctx, AV_LOG_VERBOSE, "Width %d exceeds %d\n",
-               avctx->width, ret);
-        return AVERROR(ENOSYS);
-    }
+        queue->data = av_malloc(queue->size * sizeof(*(queue->data)));
 
-    ret = nvenc_check_cap(avctx, NV_ENC_CAPS_HEIGHT_MAX);
-    if (ret < avctx->height) {
-        av_log(avctx, AV_LOG_VERBOSE, "Height %d exceeds %d\n",
-               avctx->height, ret);
-        return AVERROR(ENOSYS);
+        if (!queue->data) {
+            queue->size = 0;
+            return AVERROR(ENOMEM);
+        }
     }
 
-    ret = nvenc_check_cap(avctx, NV_ENC_CAPS_NUM_MAX_BFRAMES);
-    if (ret < avctx->max_b_frames) {
-        av_log(avctx, AV_LOG_VERBOSE, "Max b-frames %d exceed %d\n",
-               avctx->max_b_frames, ret);
+    if (queue->count == queue->size) {
+        new_queue.size = queue->size << 1;
+        new_queue.pos = 0;
+        new_queue.count = 0;
+        new_queue.data = av_malloc(new_queue.size * sizeof(*(queue->data)));
 
-        return AVERROR(ENOSYS);
-    }
+        if (!new_queue.data)
+            return AVERROR(ENOMEM);
 
-    return 0;
-}
+        while (tmp_data = data_queue_dequeue(queue))
+            data_queue_enqueue(&new_queue, tmp_data);
 
-static int nvenc_check_device(AVCodecContext *avctx, int idx)
-{
-    NVENCContext *ctx               = avctx->priv_data;
-    NVENCLibraryContext *nvel       = &ctx->nvel;
-    char name[128]                  = { 0 };
-    int major, minor, ret;
-    CUdevice cu_device;
-    CUcontext dummy;
-    int loglevel = AV_LOG_VERBOSE;
-
-    if (ctx->device == LIST_DEVICES)
-        loglevel = AV_LOG_INFO;
-
-    ret = nvel->cu_device_get(&cu_device, idx);
-    if (ret != CUDA_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Cannot access the CUDA device %d\n",
-               idx);
-        return -1;
+        av_free(queue->data);
+        *queue = new_queue;
     }
 
-    ret = nvel->cu_device_get_name(name, sizeof(name), cu_device);
-    if (ret != CUDA_SUCCESS)
-        return -1;
+    mask = queue->size - 1;
 
-    ret = nvel->cu_device_compute_capability(&major, &minor, cu_device);
-    if (ret != CUDA_SUCCESS)
-        return -1;
+    queue->data[queue->pos] = *data;
+    queue->pos = (queue->pos + 1) & mask;
+    queue->count++;
 
-    av_log(avctx, loglevel, "Device %d [%s] ", cu_device, name);
-
-    if (((major << 4) | minor) < NVENC_CAP)
-        goto fail;
+    return 0;
+}
 
-    ret = nvel->cu_ctx_create(&ctx->cu_context, 0, cu_device);
-    if (ret != CUDA_SUCCESS)
-        goto fail;
+static int out_surf_queue_enqueue(NvencDataList* queue, NvencOutputSurface* surface)
+{
+    NvencData data;
+    data.u.surface = surface;
 
-    ret = nvel->cu_ctx_pop_current(&dummy);
-    if (ret != CUDA_SUCCESS)
-        goto fail2;
+    return data_queue_enqueue(queue, &data);
+}
 
-    if ((ret = nvenc_open_session(avctx)) < 0)
-        goto fail2;
+static NvencOutputSurface* out_surf_queue_dequeue(NvencDataList* queue)
+{
+    NvencData* res = data_queue_dequeue(queue);
 
-    if ((ret = nvenc_check_capabilities(avctx)) < 0)
-        goto fail3;
+    if (!res)
+        return NULL;
 
-    av_log(avctx, loglevel, "supports NVENC\n");
+    return res->u.surface;
+}
 
-    if (ctx->device == cu_device || ctx->device == ANY_DEVICE)
-        return 0;
+static int timestamp_queue_enqueue(NvencDataList* queue, int64_t timestamp)
+{
+    NvencData data;
+    data.u.timestamp = timestamp;
 
-fail3:
-    nvel->nvenc_funcs.nvEncDestroyEncoder(ctx->nvenc_ctx);
-    ctx->nvenc_ctx = NULL;
+    return data_queue_enqueue(queue, &data);
+}
 
-fail2:
-    nvel->cu_ctx_destroy(ctx->cu_context);
-    ctx->cu_context = NULL;
+static int64_t timestamp_queue_dequeue(NvencDataList* queue)
+{
+    NvencData* res = data_queue_dequeue(queue);
 
-fail:
-    if (ret != 0)
-        av_log(avctx, loglevel, "does not support NVENC (major %d minor %d)\n",
-               major, minor);
+    if (!res)
+        return AV_NOPTS_VALUE;
 
-    return AVERROR(ENOSYS);
+    return res->u.timestamp;
 }
 
-static int nvenc_setup_device(AVCodecContext *avctx)
+#define CHECK_LOAD_FUNC(t, f, s) \
+do { \
+    (f) = (t)LOAD_FUNC(dl_fn->cuda_lib, s); \
+    if (!(f)) { \
+        av_log(avctx, AV_LOG_FATAL, "Failed loading %s from CUDA library\n", s); \
+        goto error; \
+    } \
+} while (0)
+
+static av_cold int nvenc_dyload_cuda(AVCodecContext *avctx)
 {
-    NVENCContext *ctx         = avctx->priv_data;
-    NVENCLibraryContext *nvel = &ctx->nvel;
-    int i, nb_devices = 0;
-
-    if ((nvel->cu_init(0)) != CUDA_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Cannot init CUDA\n");
-        return AVERROR_UNKNOWN;
-    }
+    NvencContext *ctx = avctx->priv_data;
+    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
 
-    if ((nvel->cu_device_get_count(&nb_devices)) != CUDA_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Cannot enumerate the CUDA devices\n");
-        return AVERROR_UNKNOWN;
-    }
+    if (dl_fn->cuda_lib)
+        return 1;
 
-    switch (avctx->codec->id) {
-    case AV_CODEC_ID_H264:
-        ctx->params.encodeGUID = NV_ENC_CODEC_H264_GUID;
-        break;
-    case AV_CODEC_ID_HEVC:
-        ctx->params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
-        break;
-    default:
-        return AVERROR_BUG;
-    }
+#if defined(_WIN32)
+    dl_fn->cuda_lib = LoadLibrary(TEXT("nvcuda.dll"));
+#else
+    dl_fn->cuda_lib = dlopen("libcuda.so", RTLD_LAZY);
+#endif
 
-    for (i = 0; i < nb_devices; ++i) {
-        if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES)
-            return 0;
+    if (!dl_fn->cuda_lib) {
+        av_log(avctx, AV_LOG_FATAL, "Failed loading CUDA library\n");
+        goto error;
     }
 
-    if (ctx->device == LIST_DEVICES)
-        return AVERROR_EXIT;
+    CHECK_LOAD_FUNC(PCUINIT, dl_fn->cu_init, "cuInit");
+    CHECK_LOAD_FUNC(PCUDEVICEGETCOUNT, dl_fn->cu_device_get_count, "cuDeviceGetCount");
+    CHECK_LOAD_FUNC(PCUDEVICEGET, dl_fn->cu_device_get, "cuDeviceGet");
+    CHECK_LOAD_FUNC(PCUDEVICEGETNAME, dl_fn->cu_device_get_name, "cuDeviceGetName");
+    CHECK_LOAD_FUNC(PCUDEVICECOMPUTECAPABILITY, dl_fn->cu_device_compute_capability, "cuDeviceComputeCapability");
+    CHECK_LOAD_FUNC(PCUCTXCREATE, dl_fn->cu_ctx_create, "cuCtxCreate_v2");
+    CHECK_LOAD_FUNC(PCUCTXPOPCURRENT, dl_fn->cu_ctx_pop_current, "cuCtxPopCurrent_v2");
+    CHECK_LOAD_FUNC(PCUCTXDESTROY, dl_fn->cu_ctx_destroy, "cuCtxDestroy_v2");
 
-    return AVERROR(ENOSYS);
-}
+    return 1;
 
-typedef struct GUIDTuple {
-    const GUID guid;
-    int flags;
-} GUIDTuple;
+error:
 
-static int nvec_map_preset(NVENCContext *ctx)
-{
-    GUIDTuple presets[] = {
-        { NV_ENC_PRESET_DEFAULT_GUID },
-        { NV_ENC_PRESET_HP_GUID },
-        { NV_ENC_PRESET_HQ_GUID },
-        { NV_ENC_PRESET_BD_GUID },
-        { NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID, NVENC_LOWLATENCY },
-        { NV_ENC_PRESET_LOW_LATENCY_HP_GUID,      NVENC_LOWLATENCY },
-        { NV_ENC_PRESET_LOW_LATENCY_HQ_GUID,      NVENC_LOWLATENCY },
-        { NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID,    NVENC_LOSSLESS },
-        { NV_ENC_PRESET_LOSSLESS_HP_GUID,         NVENC_LOSSLESS },
-        { { 0 } }
-    };
-
-    GUIDTuple *t = &presets[ctx->preset];
-
-    ctx->params.presetGUID = t->guid;
-    ctx->flags             = t->flags;
+    if (dl_fn->cuda_lib)
+        DL_CLOSE_FUNC(dl_fn->cuda_lib);
 
-    return AVERROR(EINVAL);
-}
+    dl_fn->cuda_lib = NULL;
 
-static void set_constqp(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
-{
-    rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
-    rc->constQP.qpInterB = avctx->global_quality;
-    rc->constQP.qpInterP = avctx->global_quality;
-    rc->constQP.qpIntra  = avctx->global_quality;
+    return 0;
 }
 
-static void set_vbr(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
+static av_cold int check_cuda_errors(AVCodecContext *avctx, CUresult err, const char *func)
 {
-    if (avctx->qmin >= 0) {
-        rc->enableMinQP    = 1;
-        rc->minQP.qpInterB = avctx->qmin;
-        rc->minQP.qpInterP = avctx->qmin;
-        rc->minQP.qpIntra  = avctx->qmin;
-    }
-
-    if (avctx->qmax >= 0) {
-        rc->enableMaxQP = 1;
-        rc->maxQP.qpInterB = avctx->qmax;
-        rc->maxQP.qpInterP = avctx->qmax;
-        rc->maxQP.qpIntra  = avctx->qmax;
+    if (err != CUDA_SUCCESS) {
+        av_log(avctx, AV_LOG_FATAL, ">> %s - failed with error code 0x%x\n", func, err);
+        return 0;
     }
+    return 1;
 }
+#define check_cuda_errors(f) if (!check_cuda_errors(avctx, f, #f)) goto error
 
-static void nvenc_override_rate_control(AVCodecContext *avctx,
-                                        NV_ENC_RC_PARAMS *rc)
+static av_cold int nvenc_check_cuda(AVCodecContext *avctx)
 {
-    NVENCContext *ctx    = avctx->priv_data;
-
-    switch (ctx->rc) {
-    case NV_ENC_PARAMS_RC_CONSTQP:
-        if (avctx->global_quality < 0) {
-            av_log(avctx, AV_LOG_WARNING,
-                   "The constant quality rate-control requires "
-                   "the 'global_quality' option set.\n");
-            return;
-        }
-        set_constqp(avctx, rc);
-        return;
-    case NV_ENC_PARAMS_RC_2_PASS_VBR:
-    case NV_ENC_PARAMS_RC_VBR:
-        if (avctx->qmin < 0 && avctx->qmax < 0) {
-            av_log(avctx, AV_LOG_WARNING,
-                   "The variable bitrate rate-control requires "
-                   "the 'qmin' and/or 'qmax' option set.\n");
-            return;
-        }
-    case NV_ENC_PARAMS_RC_VBR_MINQP:
-        if (avctx->qmin < 0) {
-            av_log(avctx, AV_LOG_WARNING,
-                   "The variable bitrate rate-control requires "
-                   "the 'qmin' option set.\n");
-            return;
-        }
-        set_vbr(avctx, rc);
+    int device_count = 0;
+    CUdevice cu_device = 0;
+    char gpu_name[128];
+    int smminor = 0, smmajor = 0;
+    int i, smver, target_smver;
+
+    NvencContext *ctx = avctx->priv_data;
+    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+
+    switch (avctx->codec->id) {
+    case AV_CODEC_ID_H264:
+        target_smver = avctx->pix_fmt == AV_PIX_FMT_YUV444P ? 0x52 : 0x30;
         break;
-    case NV_ENC_PARAMS_RC_CBR:
+    case AV_CODEC_ID_H265:
+        target_smver = 0x52;
         break;
-    case NV_ENC_PARAMS_RC_2_PASS_QUALITY:
-    case NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP:
-        if (!(ctx->flags & NVENC_LOWLATENCY)) {
-            av_log(avctx, AV_LOG_WARNING,
-                   "The multipass rate-control requires "
-                   "a low-latency preset.\n");
-            return;
-        }
+    default:
+        av_log(avctx, AV_LOG_FATAL, "nvenc: Unknown codec name\n");
+        goto error;
     }
 
-    rc->rateControlMode = ctx->rc;
-}
+    if (!nvenc_dyload_cuda(avctx))
+        return 0;
 
-static void nvenc_setup_rate_control(AVCodecContext *avctx)
-{
-    NVENCContext *ctx    = avctx->priv_data;
-    NV_ENC_RC_PARAMS *rc = &ctx->config.rcParams;
+    if (dl_fn->nvenc_device_count > 0)
+        return 1;
 
-    if (avctx->bit_rate > 0)
-        rc->averageBitRate = avctx->bit_rate;
+    check_cuda_errors(dl_fn->cu_init(0));
 
-    if (avctx->rc_max_rate > 0)
-        rc->maxBitRate = avctx->rc_max_rate;
+    check_cuda_errors(dl_fn->cu_device_get_count(&device_count));
 
-    if (ctx->rc > 0) {
-        nvenc_override_rate_control(avctx, rc);
-    } else if (avctx->global_quality > 0) {
-        set_constqp(avctx, rc);
-    } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
-        rc->rateControlMode = NV_ENC_PARAMS_RC_VBR;
-        set_vbr(avctx, rc);
+    if (!device_count) {
+        av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
+        goto error;
     }
 
-    if (avctx->rc_buffer_size > 0)
-        rc->vbvBufferSize = avctx->rc_buffer_size;
+    av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", device_count);
 
-    if (rc->averageBitRate > 0)
-        avctx->bit_rate = rc->averageBitRate;
-}
+    dl_fn->nvenc_device_count = 0;
 
-static int nvenc_setup_h264_config(AVCodecContext *avctx)
-{
-    NVENCContext *ctx                      = avctx->priv_data;
-    NV_ENC_CONFIG *cc                      = &ctx->config;
-    NV_ENC_CONFIG_H264 *h264               = &cc->encodeCodecConfig.h264Config;
-    NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
+    for (i = 0; i < device_count; ++i) {
+        check_cuda_errors(dl_fn->cu_device_get(&cu_device, i));
+        check_cuda_errors(dl_fn->cu_device_get_name(gpu_name, sizeof(gpu_name), cu_device));
+        check_cuda_errors(dl_fn->cu_device_compute_capability(&smmajor, &smminor, cu_device));
 
-    vui->colourDescriptionPresentFlag = 1;
-    vui->videoSignalTypePresentFlag   = 1;
+        smver = (smmajor << 4) | smminor;
 
-    vui->colourMatrix            = avctx->colorspace;
-    vui->colourPrimaries         = avctx->color_primaries;
-    vui->transferCharacteristics = avctx->color_trc;
+        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");
 
-    vui->videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG;
+        if (smver >= target_smver)
+            dl_fn->nvenc_devices[dl_fn->nvenc_device_count++] = cu_device;
+    }
 
-    h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
-    h264->repeatSPSPPS  = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
+    if (!dl_fn->nvenc_device_count) {
+        av_log(avctx, AV_LOG_FATAL, "No NVENC capable devices found\n");
+        goto error;
+    }
 
-    h264->maxNumRefFrames = avctx->refs;
-    h264->idrPeriod       = cc->gopLength;
+    return 1;
 
-    if (ctx->profile)
-        avctx->profile = ctx->profile;
+error:
 
-    if (avctx->pix_fmt == AV_PIX_FMT_YUV444P)
-        h264->chromaFormatIDC = 3;
-    else
-        h264->chromaFormatIDC = 1;
-
-    switch (ctx->profile) {
-    case NV_ENC_H264_PROFILE_BASELINE:
-        cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
-        break;
-    case NV_ENC_H264_PROFILE_MAIN:
-        cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
-        break;
-    case NV_ENC_H264_PROFILE_HIGH:
-        cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
-        break;
-    case NV_ENC_H264_PROFILE_HIGH_444:
-        cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
-        break;
-    case NV_ENC_H264_PROFILE_CONSTRAINED_HIGH:
-        cc->profileGUID = NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID;
-        break;
-    }
-
-    h264->level = ctx->level;
+    dl_fn->nvenc_device_count = 0;
 
     return 0;
 }
 
-static int nvenc_setup_hevc_config(AVCodecContext *avctx)
+static av_cold int nvenc_dyload_nvenc(AVCodecContext *avctx)
 {
-    NVENCContext *ctx                      = avctx->priv_data;
-    NV_ENC_CONFIG *cc                      = &ctx->config;
-    NV_ENC_CONFIG_HEVC *hevc               = &cc->encodeCodecConfig.hevcConfig;
+    PNVENCODEAPICREATEINSTANCE nvEncodeAPICreateInstance = 0;
+    NVENCSTATUS nvstatus;
 
-    hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
-    hevc->repeatSPSPPS  = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
+    NvencContext *ctx = avctx->priv_data;
+    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
 
-    hevc->maxNumRefFramesInDPB = avctx->refs;
-    hevc->idrPeriod            = cc->gopLength;
+    if (!nvenc_check_cuda(avctx))
+        return 0;
 
-    /* No other profile is supported in the current SDK version 5 */
-    cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
-    avctx->profile  = FF_PROFILE_HEVC_MAIN;
+    if (dl_fn->nvenc_lib)
+        return 1;
 
-    if (ctx->level) {
-        hevc->level = ctx->level;
+#if defined(_WIN32)
+    if (sizeof(void*) == 8) {
+        dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI64.dll"));
     } else {
-        hevc->level = NV_ENC_LEVEL_AUTOSELECT;
+        dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI.dll"));
     }
+#else
+    dl_fn->nvenc_lib = dlopen("libnvidia-encode.so.1", RTLD_LAZY);
+#endif
 
-    if (ctx->tier) {
-        hevc->tier = ctx->tier;
+    if (!dl_fn->nvenc_lib) {
+        av_log(avctx, AV_LOG_FATAL, "Failed loading the nvenc library\n");
+        goto error;
     }
 
-    return 0;
-}
-static int nvenc_setup_codec_config(AVCodecContext *avctx)
-{
-    switch (avctx->codec->id) {
-    case AV_CODEC_ID_H264:
-        return nvenc_setup_h264_config(avctx);
-    case AV_CODEC_ID_HEVC:
-        return nvenc_setup_hevc_config(avctx);
+    nvEncodeAPICreateInstance = (PNVENCODEAPICREATEINSTANCE)LOAD_FUNC(dl_fn->nvenc_lib, "NvEncodeAPICreateInstance");
+
+    if (!nvEncodeAPICreateInstance) {
+        av_log(avctx, AV_LOG_FATAL, "Failed to load nvenc entrypoint\n");
+        goto error;
     }
-    return 0;
-}
 
-static int nvenc_setup_encoder(AVCodecContext *avctx)
-{
-    NVENCContext *ctx               = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
-    NV_ENC_PRESET_CONFIG preset_cfg = { 0 };
-    int ret;
-
-    ctx->params.version = NV_ENC_INITIALIZE_PARAMS_VER;
-
-    ctx->params.encodeHeight = avctx->height;
-    ctx->params.encodeWidth  = avctx->width;
-
-    if (avctx->sample_aspect_ratio.num &&
-        avctx->sample_aspect_ratio.den &&
-        (avctx->sample_aspect_ratio.num != 1 ||
-         avctx->sample_aspect_ratio.den != 1)) {
-        av_reduce(&ctx->params.darWidth,
-                  &ctx->params.darHeight,
-                  avctx->width * avctx->sample_aspect_ratio.num,
-                  avctx->height * avctx->sample_aspect_ratio.den,
-                  INT_MAX / 8);
-    } else {
-        ctx->params.darHeight = avctx->height;
-        ctx->params.darWidth  = avctx->width;
+    dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
+
+    nvstatus = nvEncodeAPICreateInstance(&dl_fn->nvenc_funcs);
+
+    if (nvstatus != NV_ENC_SUCCESS) {
+        av_log(avctx, AV_LOG_FATAL, "Failed to create nvenc instance\n");
+        goto error;
     }
 
-    ctx->params.frameRateNum = avctx->time_base.den;
-    ctx->params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
+    av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
 
-    ctx->params.enableEncodeAsync = 0;
-    ctx->params.enablePTD         = 1;
+    return 1;
 
-    ctx->params.encodeConfig = &ctx->config;
+error:
+    if (dl_fn->nvenc_lib)
+        DL_CLOSE_FUNC(dl_fn->nvenc_lib);
 
-    nvec_map_preset(ctx);
+    dl_fn->nvenc_lib = NULL;
 
-    preset_cfg.version           = NV_ENC_PRESET_CONFIG_VER;
-    preset_cfg.presetCfg.version = NV_ENC_CONFIG_VER;
+    return 0;
+}
 
-    ret = nv->nvEncGetEncodePresetConfig(ctx->nvenc_ctx,
-                                         ctx->params.encodeGUID,
-                                         ctx->params.presetGUID,
-                                         &preset_cfg);
-    if (ret != NV_ENC_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Cannot get the preset configuration\n");
-        return AVERROR_UNKNOWN;
-    }
+static av_cold void nvenc_unload_nvenc(AVCodecContext *avctx)
+{
+    NvencContext *ctx = avctx->priv_data;
+    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
 
-    memcpy(&ctx->config, &preset_cfg.presetCfg, sizeof(ctx->config));
+    DL_CLOSE_FUNC(dl_fn->nvenc_lib);
+    dl_fn->nvenc_lib = NULL;
 
-    ctx->config.version = NV_ENC_CONFIG_VER;
+    dl_fn->nvenc_device_count = 0;
 
-    if (avctx->gop_size > 0) {
-        if (avctx->max_b_frames > 0) {
-            ctx->last_dts = -2;
-            /* 0 is intra-only,
-             * 1 is I/P only,
-             * 2 is one B Frame,
-             * 3 two B frames, and so on. */
-            ctx->config.frameIntervalP = avctx->max_b_frames + 1;
-        } else if (avctx->max_b_frames == 0) {
-            ctx->config.frameIntervalP = 1;
-        }
-        ctx->config.gopLength = avctx->gop_size;
-    } else if (avctx->gop_size == 0) {
-        ctx->config.frameIntervalP = 0;
-        ctx->config.gopLength      = 1;
-    }
+    DL_CLOSE_FUNC(dl_fn->cuda_lib);
+    dl_fn->cuda_lib = NULL;
 
-    if (ctx->config.frameIntervalP > 1)
-        avctx->max_b_frames = ctx->config.frameIntervalP - 1;
+    dl_fn->cu_init = NULL;
+    dl_fn->cu_device_get_count = NULL;
+    dl_fn->cu_device_get = NULL;
+    dl_fn->cu_device_get_name = NULL;
+    dl_fn->cu_device_compute_capability = NULL;
+    dl_fn->cu_ctx_create = NULL;
+    dl_fn->cu_ctx_pop_current = NULL;
+    dl_fn->cu_ctx_destroy = NULL;
 
-    nvenc_setup_rate_control(avctx);
+    av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
+}
 
-    if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
-        ctx->config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
-    } else {
-        ctx->config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
+static av_cold int nvenc_encode_init(AVCodecContext *avctx)
+{
+    NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encode_session_params = { 0 };
+    NV_ENC_PRESET_CONFIG preset_config = { 0 };
+    CUcontext cu_context_curr;
+    CUresult cu_res;
+    GUID encoder_preset = NV_ENC_PRESET_HQ_GUID;
+    GUID codec;
+    NVENCSTATUS nv_status = NV_ENC_SUCCESS;
+    int surfaceCount = 0;
+    int i, num_mbs;
+    int isLL = 0;
+    int lossless = 0;
+    int res = 0;
+    int dw, dh;
+
+    NvencContext *ctx = avctx->priv_data;
+    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+    NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
+
+    if (!nvenc_dyload_nvenc(avctx))
+        return AVERROR_EXTERNAL;
+
+    ctx->last_dts = AV_NOPTS_VALUE;
+
+    ctx->encode_config.version = NV_ENC_CONFIG_VER;
+    ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
+    preset_config.version = NV_ENC_PRESET_CONFIG_VER;
+    preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
+    encode_session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
+    encode_session_params.apiVersion = NVENCAPI_VERSION;
+
+    if (ctx->gpu >= dl_fn->nvenc_device_count) {
+        av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->gpu, dl_fn->nvenc_device_count);
+        res = AVERROR(EINVAL);
+        goto error;
     }
 
-    if ((ret = nvenc_setup_codec_config(avctx)) < 0)
-        return ret;
+    ctx->cu_context = NULL;
+    cu_res = dl_fn->cu_ctx_create(&ctx->cu_context, 0, dl_fn->nvenc_devices[ctx->gpu]);
 
-    ret = nv->nvEncInitializeEncoder(ctx->nvenc_ctx, &ctx->params);
-    if (ret != NV_ENC_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR, "Cannot initialize the decoder");
-        return AVERROR_UNKNOWN;
+    if (cu_res != CUDA_SUCCESS) {
+        av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res);
+        res = AVERROR_EXTERNAL;
+        goto error;
     }
 
-    return 0;
-}
+    cu_res = dl_fn->cu_ctx_pop_current(&cu_context_curr);
 
-static int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
-{
-    NVENCContext *ctx               = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
-    int ret;
-    NV_ENC_CREATE_INPUT_BUFFER in_buffer      = { 0 };
-    NV_ENC_CREATE_BITSTREAM_BUFFER out_buffer = { 0 };
+    if (cu_res != CUDA_SUCCESS) {
+        av_log(avctx, AV_LOG_FATAL, "Failed popping CUDA context: 0x%x\n", (int)cu_res);
+        res = AVERROR_EXTERNAL;
+        goto error;
+    }
 
-    in_buffer.version  = NV_ENC_CREATE_INPUT_BUFFER_VER;
-    out_buffer.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
+    encode_session_params.device = ctx->cu_context;
+    encode_session_params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
 
-    in_buffer.width  = avctx->width;
-    in_buffer.height = avctx->height;
+    nv_status = p_nvenc->nvEncOpenEncodeSessionEx(&encode_session_params, &ctx->nvencoder);
+    if (nv_status != NV_ENC_SUCCESS) {
+        ctx->nvencoder = NULL;
+        av_log(avctx, AV_LOG_FATAL, "OpenEncodeSessionEx failed: 0x%x - invalid license key?\n", (int)nv_status);
+        res = AVERROR_EXTERNAL;
+        goto error;
+    }
 
-    in_buffer.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED;
+    if (ctx->preset) {
+        if (!strcmp(ctx->preset, "hp")) {
+            encoder_preset = NV_ENC_PRESET_HP_GUID;
+        } else if (!strcmp(ctx->preset, "hq")) {
+            encoder_preset = NV_ENC_PRESET_HQ_GUID;
+        } else if (!strcmp(ctx->preset, "bd")) {
+            encoder_preset = NV_ENC_PRESET_BD_GUID;
+        } else if (!strcmp(ctx->preset, "ll")) {
+            encoder_preset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID;
+            isLL = 1;
+        } else if (!strcmp(ctx->preset, "llhp")) {
+            encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
+            isLL = 1;
+        } else if (!strcmp(ctx->preset, "llhq")) {
+            encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
+            isLL = 1;
+        } else if (!strcmp(ctx->preset, "lossless")) {
+            encoder_preset = NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID;
+            lossless = 1;
+        } else if (!strcmp(ctx->preset, "losslesshp")) {
+            encoder_preset = NV_ENC_PRESET_LOSSLESS_HP_GUID;
+            lossless = 1;
+        } else if (!strcmp(ctx->preset, "default")) {
+            encoder_preset = NV_ENC_PRESET_DEFAULT_GUID;
+        } else {
+            av_log(avctx, AV_LOG_FATAL, "Preset \"%s\" is unknown! Supported presets: hp, hq, bd, ll, llhp, llhq, lossless, losslesshp, default\n", ctx->preset);
+            res = AVERROR(EINVAL);
+            goto error;
+        }
+    }
 
-    switch (avctx->pix_fmt) {
-    case AV_PIX_FMT_YUV420P:
-        in_buffer.bufferFmt = NV_ENC_BUFFER_FORMAT_YV12_PL;
-        break;
-    case AV_PIX_FMT_NV12:
-        in_buffer.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
+    switch (avctx->codec->id) {
+    case AV_CODEC_ID_H264:
+        codec = NV_ENC_CODEC_H264_GUID;
         break;
-    case AV_PIX_FMT_YUV444P:
-        in_buffer.bufferFmt = NV_ENC_BUFFER_FORMAT_YUV444_PL;
+    case AV_CODEC_ID_H265:
+        codec = NV_ENC_CODEC_HEVC_GUID;
         break;
     default:
-        return AVERROR_BUG;
+        av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
+        res = AVERROR(EINVAL);
+        goto error;
     }
 
-    ret = nv->nvEncCreateInputBuffer(ctx->nvenc_ctx, &in_buffer);
-    if (ret != NV_ENC_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR, "CreateInputBuffer failed\n");
-        return AVERROR_UNKNOWN;
+    nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, codec, encoder_preset, &preset_config);
+    if (nv_status != NV_ENC_SUCCESS) {
+        av_log(avctx, AV_LOG_FATAL, "GetEncodePresetConfig failed: 0x%x\n", (int)nv_status);
+        res = AVERROR_EXTERNAL;
+        goto error;
     }
 
-    ctx->in[idx].in        = in_buffer.inputBuffer;
-    ctx->in[idx].format    = in_buffer.bufferFmt;
-
-    /* 1MB is large enough to hold most output frames.
-     * NVENC increases this automaticaly if it's not enough. */
-    out_buffer.size = BITSTREAM_BUFFER_SIZE;
+    ctx->init_encode_params.encodeGUID = codec;
+    ctx->init_encode_params.encodeHeight = avctx->height;
+    ctx->init_encode_params.encodeWidth = avctx->width;
 
-    out_buffer.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED;
+    if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den &&
+        (avctx->sample_aspect_ratio.num != 1 || avctx->sample_aspect_ratio.num != 1)) {
+        av_reduce(&dw, &dh,
+                  avctx->width * avctx->sample_aspect_ratio.num,
+                  avctx->height * avctx->sample_aspect_ratio.den,
+                  1024 * 1024);
+        ctx->init_encode_params.darHeight = dh;
+        ctx->init_encode_params.darWidth = dw;
+    } else {
+        ctx->init_encode_params.darHeight = avctx->height;
+        ctx->init_encode_params.darWidth = avctx->width;
+    }
 
-    ret = nv->nvEncCreateBitstreamBuffer(ctx->nvenc_ctx, &out_buffer);
-    if (ret != NV_ENC_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR, "CreateBitstreamBuffer failed\n");
-        return AVERROR_UNKNOWN;
+    // De-compensate for hardware, dubiously, trying to compensate for
+    // playback at 704 pixel width.
+    if (avctx->width == 720 &&
+        (avctx->height == 480 || avctx->height == 576)) {
+        av_reduce(&dw, &dh,
+                  ctx->init_encode_params.darWidth * 44,
+                  ctx->init_encode_params.darHeight * 45,
+                  1024 * 1024);
+        ctx->init_encode_params.darHeight = dh;
+        ctx->init_encode_params.darWidth = dw;
     }
 
-    ctx->out[idx].out  = out_buffer.bitstreamBuffer;
-    ctx->out[idx].busy = 0;
+    ctx->init_encode_params.frameRateNum = avctx->time_base.den;
+    ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
 
-    return 0;
-}
+    num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4);
+    ctx->max_surface_count = (num_mbs >= 8160) ? 32 : 48;
 
-static int nvenc_setup_surfaces(AVCodecContext *avctx)
-{
-    NVENCContext *ctx = avctx->priv_data;
-    int i, ret;
+    if (ctx->buffer_delay >= ctx->max_surface_count)
+        ctx->buffer_delay = ctx->max_surface_count - 1;
 
-    ctx->nb_surfaces = FFMAX(4 + avctx->max_b_frames,
-                             ctx->nb_surfaces);
+    ctx->init_encode_params.enableEncodeAsync = 0;
+    ctx->init_encode_params.enablePTD = 1;
 
-    ctx->in = av_mallocz(ctx->nb_surfaces * sizeof(*ctx->in));
-    if (!ctx->in)
-        return AVERROR(ENOMEM);
+    ctx->init_encode_params.presetGUID = encoder_preset;
 
-    ctx->out = av_mallocz(ctx->nb_surfaces * sizeof(*ctx->out));
-    if (!ctx->out)
-        return AVERROR(ENOMEM);
+    ctx->init_encode_params.encodeConfig = &ctx->encode_config;
+    memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
+    ctx->encode_config.version = NV_ENC_CONFIG_VER;
 
-    ctx->timestamps = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t));
-    if (!ctx->timestamps)
-        return AVERROR(ENOMEM);
-    ctx->pending = av_fifo_alloc(ctx->nb_surfaces * sizeof(ctx->out));
-    if (!ctx->pending)
-        return AVERROR(ENOMEM);
-    ctx->ready = av_fifo_alloc(ctx->nb_surfaces * sizeof(ctx->out));
-    if (!ctx->ready)
-        return AVERROR(ENOMEM);
+    if (avctx->refs >= 0) {
+        /* 0 means "let the hardware decide" */
+        switch (avctx->codec->id) {
+        case AV_CODEC_ID_H264:
+            ctx->encode_config.encodeCodecConfig.h264Config.maxNumRefFrames = avctx->refs;
+            break;
+        case AV_CODEC_ID_H265:
+            ctx->encode_config.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = avctx->refs;
+            break;
+        /* Earlier switch/case will return if unknown codec is passed. */
+        }
+    }
+
+    if (avctx->gop_size > 0) {
+        if (avctx->max_b_frames >= 0) {
+            /* 0 is intra-only, 1 is I/P only, 2 is one B Frame, 3 two B frames, and so on. */
+            ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
+        }
 
-    for (i = 0; i < ctx->nb_surfaces; i++) {
-        if ((ret = nvenc_alloc_surface(avctx, i)) < 0)
-            return ret;
+        ctx->encode_config.gopLength = avctx->gop_size;
+        switch (avctx->codec->id) {
+        case AV_CODEC_ID_H264:
+            ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = avctx->gop_size;
+            break;
+        case AV_CODEC_ID_H265:
+            ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = avctx->gop_size;
+            break;
+        /* Earlier switch/case will return if unknown codec is passed. */
+        }
+    } else if (avctx->gop_size == 0) {
+        ctx->encode_config.frameIntervalP = 0;
+        ctx->encode_config.gopLength = 1;
+        switch (avctx->codec->id) {
+        case AV_CODEC_ID_H264:
+            ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = 1;
+            break;
+        case AV_CODEC_ID_H265:
+            ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = 1;
+            break;
+        /* Earlier switch/case will return if unknown codec is passed. */
+        }
     }
 
-    return 0;
-}
+    /* when there're b frames, set dts offset */
+    if (ctx->encode_config.frameIntervalP >= 2)
+        ctx->last_dts = -2;
 
-#define EXTRADATA_SIZE 512
+    if (avctx->bit_rate > 0)
+        ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
 
-static int nvenc_setup_extradata(AVCodecContext *avctx)
-{
-    NVENCContext *ctx                     = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv       = &ctx->nvel.nvenc_funcs;
-    NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
-    int ret;
+    if (avctx->rc_max_rate > 0)
+        ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
+
+    if (lossless) {
+      ctx->encode_config.encodeCodecConfig.h264Config.qpPrimeYZeroTransformBypassFlag = 1;
+      ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
+      ctx->encode_config.rcParams.constQP.qpInterB = 0;
+      ctx->encode_config.rcParams.constQP.qpInterP = 0;
+      ctx->encode_config.rcParams.constQP.qpIntra = 0;
+
+      avctx->qmin = -1;
+      avctx->qmax = -1;
+    } else if (ctx->cbr) {
+        if (!ctx->twopass) {
+            ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
+        } else if (ctx->twopass == 1 || isLL) {
+            ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_QUALITY;
+
+            if (avctx->codec->id == AV_CODEC_ID_H264) {
+                ctx->encode_config.encodeCodecConfig.h264Config.adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
+                ctx->encode_config.encodeCodecConfig.h264Config.fmoMode = NV_ENC_H264_FMO_DISABLE;
+            }
+        } else {
+            ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
+        }
+    } else if (avctx->global_quality > 0) {
+        ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
+        ctx->encode_config.rcParams.constQP.qpInterB = avctx->global_quality;
+        ctx->encode_config.rcParams.constQP.qpInterP = avctx->global_quality;
+        ctx->encode_config.rcParams.constQP.qpIntra = avctx->global_quality;
 
-    avctx->extradata = av_mallocz(EXTRADATA_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
-    if (!avctx->extradata)
-        return AVERROR(ENOMEM);
+        avctx->qmin = -1;
+        avctx->qmax = -1;
+    } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
+        ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
 
-    payload.version              = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
-    payload.spsppsBuffer         = avctx->extradata;
-    payload.inBufferSize         = EXTRADATA_SIZE;
-    payload.outSPSPPSPayloadSize = &avctx->extradata_size;
+        ctx->encode_config.rcParams.enableMinQP = 1;
+        ctx->encode_config.rcParams.enableMaxQP = 1;
 
-    ret = nv->nvEncGetSequenceParams(ctx->nvenc_ctx, &payload);
-    if (ret != NV_ENC_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR, "Cannot get the extradata\n");
-        return AVERROR_UNKNOWN;
+        ctx->encode_config.rcParams.minQP.qpInterB = avctx->qmin;
+        ctx->encode_config.rcParams.minQP.qpInterP = avctx->qmin;
+        ctx->encode_config.rcParams.minQP.qpIntra = avctx->qmin;
+
+        ctx->encode_config.rcParams.maxQP.qpInterB = avctx->qmax;
+        ctx->encode_config.rcParams.maxQP.qpInterP = avctx->qmax;
+        ctx->encode_config.rcParams.maxQP.qpIntra = avctx->qmax;
     }
 
-    return 0;
-}
+    if (avctx->rc_buffer_size > 0)
+        ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size;
 
-av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
-{
-    NVENCContext *ctx               = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
-    int i;
+    if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
+        ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
+    } else {
+        ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
+    }
 
-    if (ctx->in) {
-        for (i = 0; i < ctx->nb_surfaces; ++i) {
-            nv->nvEncDestroyInputBuffer(ctx->nvenc_ctx, ctx->in[i].in);
-            nv->nvEncDestroyBitstreamBuffer(ctx->nvenc_ctx, ctx->out[i].out);
+    switch (avctx->codec->id) {
+    case AV_CODEC_ID_H264:
+        ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourDescriptionPresentFlag = 1;
+        ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag = 1;
+
+        ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourMatrix = avctx->colorspace;
+        ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourPrimaries = avctx->color_primaries;
+        ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.transferCharacteristics = avctx->color_trc;
+
+        ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG;
+
+        ctx->encode_config.encodeCodecConfig.h264Config.disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
+        ctx->encode_config.encodeCodecConfig.h264Config.repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
+
+        if (!ctx->profile) {
+            switch (avctx->profile) {
+            case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
+                ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
+                break;
+            case FF_PROFILE_H264_BASELINE:
+                ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
+                break;
+            case FF_PROFILE_H264_MAIN:
+                ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
+                break;
+            case FF_PROFILE_H264_HIGH:
+            case FF_PROFILE_UNKNOWN:
+                ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
+                break;
+            default:
+                av_log(avctx, AV_LOG_WARNING, "Unsupported profile requested, falling back to high\n");
+                ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
+                break;
+            }
+        } else {
+            if (!strcmp(ctx->profile, "high")) {
+                ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
+                avctx->profile = FF_PROFILE_H264_HIGH;
+            } else if (!strcmp(ctx->profile, "main")) {
+                ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
+                avctx->profile = FF_PROFILE_H264_MAIN;
+            } else if (!strcmp(ctx->profile, "baseline")) {
+                ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
+                avctx->profile = FF_PROFILE_H264_BASELINE;
+            } else if (!strcmp(ctx->profile, "high444p")) {
+                ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
+                avctx->profile = FF_PROFILE_H264_HIGH_444_PREDICTIVE;
+            } else {
+                av_log(avctx, AV_LOG_FATAL, "Profile \"%s\" is unknown! Supported profiles: high, main, baseline\n", ctx->profile);
+                res = AVERROR(EINVAL);
+                goto error;
+            }
         }
-    }
 
-    av_freep(&ctx->in);
-    av_freep(&ctx->out);
+        ctx->encode_config.encodeCodecConfig.h264Config.chromaFormatIDC = avctx->profile == FF_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1;
 
-    if (ctx->nvenc_ctx)
-        nv->nvEncDestroyEncoder(ctx->nvenc_ctx);
+        if (ctx->level) {
+            res = input_string_to_uint32(avctx, nvenc_h264_level_pairs, ctx->level, &ctx->encode_config.encodeCodecConfig.h264Config.level);
 
-    if (ctx->cu_context)
-        ctx->nvel.cu_ctx_destroy(ctx->cu_context);
+            if (res) {
+                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);
+                goto error;
+            }
+        } else {
+            ctx->encode_config.encodeCodecConfig.h264Config.level = NV_ENC_LEVEL_AUTOSELECT;
+        }
 
-    if (ctx->nvel.nvenc)
-        dlclose(ctx->nvel.nvenc);
+        break;
+    case AV_CODEC_ID_H265:
+        ctx->encode_config.encodeCodecConfig.hevcConfig.disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
+        ctx->encode_config.encodeCodecConfig.hevcConfig.repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
 
-    if (ctx->nvel.cuda)
-        dlclose(ctx->nvel.cuda);
+        /* No other profile is supported in the current SDK version 5 */
+        ctx->encode_config.profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
+        avctx->profile = FF_PROFILE_HEVC_MAIN;
 
-    return 0;
-}
+        if (ctx->level) {
+            res = input_string_to_uint32(avctx, nvenc_hevc_level_pairs, ctx->level, &ctx->encode_config.encodeCodecConfig.hevcConfig.level);
 
-av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
-{
-    int ret;
+            if (res) {
+                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);
+                goto error;
+            }
+        } else {
+            ctx->encode_config.encodeCodecConfig.hevcConfig.level = NV_ENC_LEVEL_AUTOSELECT;
+        }
+
+        if (ctx->tier) {
+            if (!strcmp(ctx->tier, "main")) {
+                ctx->encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_MAIN;
+            } else if (!strcmp(ctx->tier, "high")) {
+                ctx->encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_HIGH;
+            } else {
+                av_log(avctx, AV_LOG_FATAL, "Tier \"%s\" is unknown! Supported tiers: main, high\n", ctx->tier);
+                res = AVERROR(EINVAL);
+                goto error;
+            }
+        }
+
+        break;
+    /* Earlier switch/case will return if unknown codec is passed. */
+    }
 
-    if ((ret = nvenc_load_libraries(avctx)) < 0)
-        return ret;
+    nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
+    if (nv_status != NV_ENC_SUCCESS) {
+        av_log(avctx, AV_LOG_FATAL, "InitializeEncoder failed: 0x%x\n", (int)nv_status);
+        res = AVERROR_EXTERNAL;
+        goto error;
+    }
 
-    if ((ret = nvenc_setup_device(avctx)) < 0)
-        return ret;
+    ctx->input_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->input_surfaces));
 
-    if ((ret = nvenc_setup_encoder(avctx)) < 0)
-        return ret;
+    if (!ctx->input_surfaces) {
+        res = AVERROR(ENOMEM);
+        goto error;
+    }
 
-    if ((ret = nvenc_setup_surfaces(avctx)) < 0)
-        return ret;
+    ctx->output_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->output_surfaces));
 
-    if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
-        if ((ret = nvenc_setup_extradata(avctx)) < 0)
-            return ret;
+    if (!ctx->output_surfaces) {
+        res = AVERROR(ENOMEM);
+        goto error;
     }
 
-    return 0;
-}
+    for (surfaceCount = 0; surfaceCount < ctx->max_surface_count; ++surfaceCount) {
+        NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
+        NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
+        allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
+        allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
 
-static NVENCInputSurface *get_input_surface(NVENCContext *ctx)
-{
-    int i;
+        allocSurf.width = (avctx->width + 31) & ~31;
+        allocSurf.height = (avctx->height + 31) & ~31;
 
-    for (i = 0; i < ctx->nb_surfaces; i++) {
-        if (!ctx->in[i].locked) {
-            ctx->in[i].locked = 1;
-            return &ctx->in[i];
-        }
-    }
+        allocSurf.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
 
-    return NULL;
-}
+        switch (avctx->pix_fmt) {
+        case AV_PIX_FMT_YUV420P:
+            allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YV12_PL;
+            break;
 
-static NVENCOutputSurface *get_output_surface(NVENCContext *ctx)
-{
-    int i;
+        case AV_PIX_FMT_NV12:
+            allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
+            break;
+
+        case AV_PIX_FMT_YUV444P:
+            allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YUV444_PL;
+            break;
 
-    for (i = 0; i < ctx->nb_surfaces; i++) {
-        if (!ctx->out[i].busy) {
-            return &ctx->out[i];
+        default:
+            av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format\n");
+            res = AVERROR(EINVAL);
+            goto error;
         }
-    }
 
-    return NULL;
-}
+        nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
+        if (nv_status != NV_ENC_SUCCESS) {
+            av_log(avctx, AV_LOG_FATAL, "CreateInputBuffer failed\n");
+            res = AVERROR_EXTERNAL;
+            goto error;
+        }
 
-static int nvenc_copy_frame(NV_ENC_LOCK_INPUT_BUFFER *in, const AVFrame *frame)
-{
-    uint8_t *buf = in->bufferDataPtr;
-    int off      = frame->height * in->pitch;
+        ctx->input_surfaces[surfaceCount].lockCount = 0;
+        ctx->input_surfaces[surfaceCount].input_surface = allocSurf.inputBuffer;
+        ctx->input_surfaces[surfaceCount].format = allocSurf.bufferFmt;
+        ctx->input_surfaces[surfaceCount].width = allocSurf.width;
+        ctx->input_surfaces[surfaceCount].height = allocSurf.height;
 
-    switch (frame->format) {
-    case AV_PIX_FMT_YUV420P:
-        av_image_copy_plane(buf, in->pitch,
-                            frame->data[0], frame->linesize[0],
-                            frame->width, frame->height);
-        buf += off;
+        /* 1MB is large enough to hold most output frames. NVENC increases this automaticaly if it's not enough. */
+        allocOut.size = 1024 * 1024;
 
-        av_image_copy_plane(buf, in->pitch >> 1,
-                            frame->data[2], frame->linesize[2],
-                            frame->width >> 1, frame->height >> 1);
+        allocOut.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
 
-        buf += off >> 2;
+        nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
+        if (nv_status != NV_ENC_SUCCESS) {
+            av_log(avctx, AV_LOG_FATAL, "CreateBitstreamBuffer failed\n");
+            ctx->output_surfaces[surfaceCount++].output_surface = NULL;
+            res = AVERROR_EXTERNAL;
+            goto error;
+        }
 
-        av_image_copy_plane(buf, in->pitch >> 1,
-                            frame->data[1], frame->linesize[1],
-                            frame->width >> 1, frame->height >> 1);
-        break;
-    case AV_PIX_FMT_NV12:
-        av_image_copy_plane(buf, in->pitch,
-                            frame->data[0], frame->linesize[0],
-                            frame->width, frame->height);
-        buf += off;
-
-        av_image_copy_plane(buf, in->pitch,
-                            frame->data[1], frame->linesize[1],
-                            frame->width, frame->height >> 1);
-        break;
-    case AV_PIX_FMT_YUV444P:
-        av_image_copy_plane(buf, in->pitch,
-                            frame->data[0], frame->linesize[0],
-                            frame->width, frame->height);
-        buf += off;
-
-        av_image_copy_plane(buf, in->pitch,
-                            frame->data[1], frame->linesize[1],
-                            frame->width, frame->height);
-        buf += off;
-
-        av_image_copy_plane(buf, in->pitch,
-                            frame->data[2], frame->linesize[2],
-                            frame->width, frame->height);
-        break;
-    default:
-        return AVERROR_BUG;
+        ctx->output_surfaces[surfaceCount].output_surface = allocOut.bitstreamBuffer;
+        ctx->output_surfaces[surfaceCount].size = allocOut.size;
+        ctx->output_surfaces[surfaceCount].busy = 0;
     }
 
-    return 0;
-}
+    if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
+        uint32_t outSize = 0;
+        char tmpHeader[256];
+        NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
+        payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
+
+        payload.spsppsBuffer = tmpHeader;
+        payload.inBufferSize = sizeof(tmpHeader);
+        payload.outSPSPPSPayloadSize = &outSize;
+
+        nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
+        if (nv_status != NV_ENC_SUCCESS) {
+            av_log(avctx, AV_LOG_FATAL, "GetSequenceParams failed\n");
+            goto error;
+        }
 
-static int nvenc_enqueue_frame(AVCodecContext *avctx, const AVFrame *frame,
-                               NVENCInputSurface **in_surf)
-{
-    NVENCContext *ctx               = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
-    NV_ENC_LOCK_INPUT_BUFFER params = { 0 };
-    NVENCInputSurface *in           = get_input_surface(ctx);
-    int ret;
+        avctx->extradata_size = outSize;
+        avctx->extradata = av_mallocz(outSize + FF_INPUT_BUFFER_PADDING_SIZE);
 
-    if (!in)
-        return AVERROR_BUG;
+        if (!avctx->extradata) {
+            res = AVERROR(ENOMEM);
+            goto error;
+        }
 
-    params.version     = NV_ENC_LOCK_INPUT_BUFFER_VER;
-    params.inputBuffer = in->in;
+        memcpy(avctx->extradata, tmpHeader, outSize);
+    }
 
+    if (ctx->encode_config.frameIntervalP > 1)
+        avctx->has_b_frames = 2;
 
-    ret = nv->nvEncLockInputBuffer(ctx->nvenc_ctx, &params);
-    if (ret != NV_ENC_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR, "Cannot lock the buffer %p.\n",
-               in);
-        return AVERROR_UNKNOWN;
-    }
+    if (ctx->encode_config.rcParams.averageBitRate > 0)
+        avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
+
+    return 0;
 
-    ret = nvenc_copy_frame(&params, frame);
-    if (ret < 0)
-        goto fail;
+error:
 
-    ret = nv->nvEncUnlockInputBuffer(ctx->nvenc_ctx, in->in);
-    if (ret != NV_ENC_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR, "Cannot unlock the buffer %p.\n",
-               in);
-        return AVERROR_UNKNOWN;
+    for (i = 0; i < surfaceCount; ++i) {
+        p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface);
+        if (ctx->output_surfaces[i].output_surface)
+            p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface);
     }
 
-    *in_surf = in;
+    if (ctx->nvencoder)
+        p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
 
-    return 0;
+    if (ctx->cu_context)
+        dl_fn->cu_ctx_destroy(ctx->cu_context);
 
-fail:
-    nv->nvEncUnlockInputBuffer(ctx->nvenc_ctx, in->in);
+    nvenc_unload_nvenc(avctx);
 
-    return ret;
+    ctx->nvencoder = NULL;
+    ctx->cu_context = NULL;
+
+    return res;
 }
 
-static void nvenc_codec_specific_pic_params(AVCodecContext *avctx,
-                                            NV_ENC_PIC_PARAMS *params)
+static av_cold int nvenc_encode_close(AVCodecContext *avctx)
 {
-    NVENCContext *ctx = avctx->priv_data;
+    NvencContext *ctx = avctx->priv_data;
+    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+    NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
+    int i;
 
-    switch (avctx->codec->id) {
-    case AV_CODEC_ID_H264:
-        params->codecPicParams.h264PicParams.sliceMode =
-            ctx->config.encodeCodecConfig.h264Config.sliceMode;
-        params->codecPicParams.h264PicParams.sliceModeData =
-            ctx->config.encodeCodecConfig.h264Config.sliceModeData;
-        break;
-    case AV_CODEC_ID_HEVC:
-        params->codecPicParams.hevcPicParams.sliceMode =
-            ctx->config.encodeCodecConfig.hevcConfig.sliceMode;
-        params->codecPicParams.hevcPicParams.sliceModeData =
-            ctx->config.encodeCodecConfig.hevcConfig.sliceModeData;
-        break;
+    av_freep(&ctx->timestamp_list.data);
+    av_freep(&ctx->output_surface_ready_queue.data);
+    av_freep(&ctx->output_surface_queue.data);
+
+    for (i = 0; i < ctx->max_surface_count; ++i) {
+        p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface);
+        p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface);
     }
-}
+    ctx->max_surface_count = 0;
 
-static inline int nvenc_enqueue_timestamp(AVFifoBuffer *f, int64_t pts)
-{
-    return av_fifo_generic_write(f, &pts, sizeof(pts), NULL);
-}
+    p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
+    ctx->nvencoder = NULL;
 
-static inline int nvenc_dequeue_timestamp(AVFifoBuffer *f, int64_t *pts)
-{
-    return av_fifo_generic_read(f, pts, sizeof(*pts), NULL);
-}
+    dl_fn->cu_ctx_destroy(ctx->cu_context);
+    ctx->cu_context = NULL;
 
-static inline int nvenc_enqueue_surface(AVFifoBuffer *f,
-                                        NVENCOutputSurface *surf)
-{
-    surf->busy = 1;
-    return av_fifo_generic_write(f, &surf, sizeof(surf), NULL);
-}
+    nvenc_unload_nvenc(avctx);
 
-static inline int nvenc_dequeue_surface(AVFifoBuffer *f,
-                                        NVENCOutputSurface **surf)
-{
-    return av_fifo_generic_read(f, surf, sizeof(*surf), NULL);
+    return 0;
 }
 
-static int nvenc_set_timestamp(NVENCContext *ctx,
-                               NV_ENC_LOCK_BITSTREAM *params,
-                               AVPacket *pkt)
+static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencOutputSurface *tmpoutsurf)
 {
-    pkt->pts      = params->outputTimeStamp;
-    pkt->duration = params->outputDuration;
-
-    return nvenc_dequeue_timestamp(ctx->timestamps, &pkt->dts);
-}
+    NvencContext *ctx = avctx->priv_data;
+    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+    NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
 
-static int nvenc_get_frame(AVCodecContext *avctx, AVPacket *pkt)
-{
-    NVENCContext *ctx               = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
-    NV_ENC_LOCK_BITSTREAM params    = { 0 };
-    NVENCOutputSurface *out         = NULL;
-    int ret;
+    uint32_t slice_mode_data;
+    uint32_t *slice_offsets;
+    NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
+    NVENCSTATUS nv_status;
+    int res = 0;
 
-    ret = nvenc_dequeue_surface(ctx->pending, &out);
-    if (ret)
-        return ret;
+    switch (avctx->codec->id) {
+    case AV_CODEC_ID_H264:
+      slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
+      break;
+    case AV_CODEC_ID_H265:
+      slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
+      break;
+    default:
+      av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
+      res = AVERROR(EINVAL);
+      goto error;
+    }
+    slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));
 
-    params.version         = NV_ENC_LOCK_BITSTREAM_VER;
-    params.outputBitstream = out->out;
+    if (!slice_offsets)
+        return AVERROR(ENOMEM);
 
-    ret = nv->nvEncLockBitstream(ctx->nvenc_ctx, &params);
-    if (ret < 0)
-        return AVERROR_UNKNOWN;
+    lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
 
-    ret = ff_alloc_packet(pkt, params.bitstreamSizeInBytes);
-    if (ret < 0)
-        return ret;
+    lock_params.doNotWait = 0;
+    lock_params.outputBitstream = tmpoutsurf->output_surface;
+    lock_params.sliceOffsets = slice_offsets;
 
-    memcpy(pkt->data, params.bitstreamBufferPtr, pkt->size);
+    nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
+    if (nv_status != NV_ENC_SUCCESS) {
+        av_log(avctx, AV_LOG_ERROR, "Failed locking bitstream buffer\n");
+        res = AVERROR_EXTERNAL;
+        goto error;
+    }
 
-    ret = nv->nvEncUnlockBitstream(ctx->nvenc_ctx, out->out);
-    if (ret < 0)
-        return AVERROR_UNKNOWN;
+    if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes, 0)) {
+        p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
+        goto error;
+    }
 
-    out->busy = out->in->locked = 0;
+    memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
 
-    ret = nvenc_set_timestamp(ctx, &params, pkt);
-    if (ret < 0)
-        return ret;
+    nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
+    if (nv_status != NV_ENC_SUCCESS)
+        av_log(avctx, AV_LOG_ERROR, "Failed unlocking bitstream buffer, expect the gates of mordor to open\n");
 
-    switch (params.pictureType) {
+    switch (lock_params.pictureType) {
     case NV_ENC_PIC_TYPE_IDR:
         pkt->flags |= AV_PKT_FLAG_KEY;
 #if FF_API_CODED_FRAME
 FF_DISABLE_DEPRECATION_WARNINGS
-    case NV_ENC_PIC_TYPE_INTRA_REFRESH:
     case NV_ENC_PIC_TYPE_I:
         avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
         break;
@@ -1052,79 +1152,240 @@ FF_DISABLE_DEPRECATION_WARNINGS
     case NV_ENC_PIC_TYPE_BI:
         avctx->coded_frame->pict_type = AV_PICTURE_TYPE_BI;
         break;
+    default:
+        av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
+        av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
+        res = AVERROR_EXTERNAL;
+        goto error;
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
     }
 
+    pkt->pts = lock_params.outputTimeStamp;
+    pkt->dts = timestamp_queue_dequeue(&ctx->timestamp_list);
+
+    /* when there're b frame(s), set dts offset */
+    if (ctx->encode_config.frameIntervalP >= 2)
+        pkt->dts -= 1;
+
+    if (pkt->dts > pkt->pts)
+        pkt->dts = pkt->pts;
+
+    if (ctx->last_dts != AV_NOPTS_VALUE && pkt->dts <= ctx->last_dts)
+        pkt->dts = ctx->last_dts + 1;
+
+    ctx->last_dts = pkt->dts;
+
+    av_free(slice_offsets);
+
     return 0;
+
+error:
+
+    av_free(slice_offsets);
+    timestamp_queue_dequeue(&ctx->timestamp_list);
+
+    return res;
 }
 
-int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
-                          const AVFrame *frame, int *got_packet)
+static int nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
+    const AVFrame *frame, int *got_packet)
 {
-    NVENCContext *ctx               = avctx->priv_data;
-    NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
-    NV_ENC_PIC_PARAMS params        = { 0 };
-    NVENCInputSurface *in           = NULL;
-    NVENCOutputSurface *out         = NULL;
-    int ret;
+    NVENCSTATUS nv_status;
+    NvencOutputSurface *tmpoutsurf;
+    int res, i = 0;
 
-    params.version = NV_ENC_PIC_PARAMS_VER;
+    NvencContext *ctx = avctx->priv_data;
+    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+    NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
+
+    NV_ENC_PIC_PARAMS pic_params = { 0 };
+    pic_params.version = NV_ENC_PIC_PARAMS_VER;
 
     if (frame) {
-        ret = nvenc_enqueue_frame(avctx, frame, &in);
-        if (ret < 0)
-            return ret;
-        out = get_output_surface(ctx);
-        if (!out)
-            return AVERROR_BUG;
-
-        out->in = in;
-
-        params.inputBuffer     = in->in;
-        params.bufferFmt       = in->format;
-        params.inputWidth      = frame->width;
-        params.inputHeight     = frame->height;
-        params.outputBitstream = out->out;
-        params.inputTimeStamp  = frame->pts;
+        NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
+        NvencInputSurface *inSurf = NULL;
+
+        for (i = 0; i < ctx->max_surface_count; ++i) {
+            if (!ctx->input_surfaces[i].lockCount) {
+                inSurf = &ctx->input_surfaces[i];
+                break;
+            }
+        }
+
+        av_assert0(inSurf);
+
+        inSurf->lockCount = 1;
+
+        lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
+        lockBufferParams.inputBuffer = inSurf->input_surface;
+
+        nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
+        if (nv_status != NV_ENC_SUCCESS) {
+            av_log(avctx, AV_LOG_ERROR, "Failed locking nvenc input buffer\n");
+            return 0;
+        }
+
+        if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
+            uint8_t *buf = lockBufferParams.bufferDataPtr;
+
+            av_image_copy_plane(buf, lockBufferParams.pitch,
+                frame->data[0], frame->linesize[0],
+                avctx->width, avctx->height);
+
+            buf += inSurf->height * lockBufferParams.pitch;
+
+            av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
+                frame->data[2], frame->linesize[2],
+                avctx->width >> 1, avctx->height >> 1);
+
+            buf += (inSurf->height * lockBufferParams.pitch) >> 2;
+
+            av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
+                frame->data[1], frame->linesize[1],
+                avctx->width >> 1, avctx->height >> 1);
+        } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
+            uint8_t *buf = lockBufferParams.bufferDataPtr;
+
+            av_image_copy_plane(buf, lockBufferParams.pitch,
+                frame->data[0], frame->linesize[0],
+                avctx->width, avctx->height);
+
+            buf += inSurf->height * lockBufferParams.pitch;
+
+            av_image_copy_plane(buf, lockBufferParams.pitch,
+                frame->data[1], frame->linesize[1],
+                avctx->width, avctx->height >> 1);
+        } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
+            uint8_t *buf = lockBufferParams.bufferDataPtr;
+
+            av_image_copy_plane(buf, lockBufferParams.pitch,
+                frame->data[0], frame->linesize[0],
+                avctx->width, avctx->height);
+
+            buf += inSurf->height * lockBufferParams.pitch;
+
+            av_image_copy_plane(buf, lockBufferParams.pitch,
+                frame->data[1], frame->linesize[1],
+                avctx->width, avctx->height);
+
+            buf += inSurf->height * lockBufferParams.pitch;
+
+            av_image_copy_plane(buf, lockBufferParams.pitch,
+                frame->data[2], frame->linesize[2],
+                avctx->width, avctx->height);
+        } else {
+            av_log(avctx, AV_LOG_FATAL, "Invalid pixel format!\n");
+            return AVERROR(EINVAL);
+        }
+
+        nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, inSurf->input_surface);
+        if (nv_status != NV_ENC_SUCCESS) {
+            av_log(avctx, AV_LOG_FATAL, "Failed unlocking input buffer!\n");
+            return AVERROR_EXTERNAL;
+        }
+
+        for (i = 0; i < ctx->max_surface_count; ++i)
+            if (!ctx->output_surfaces[i].busy)
+                break;
+
+        if (i == ctx->max_surface_count) {
+            inSurf->lockCount = 0;
+            av_log(avctx, AV_LOG_FATAL, "No free output surface found!\n");
+            return AVERROR_EXTERNAL;
+        }
+
+        ctx->output_surfaces[i].input_surface = inSurf;
+
+        pic_params.inputBuffer = inSurf->input_surface;
+        pic_params.bufferFmt = inSurf->format;
+        pic_params.inputWidth = avctx->width;
+        pic_params.inputHeight = avctx->height;
+        pic_params.outputBitstream = ctx->output_surfaces[i].output_surface;
+        pic_params.completionEvent = 0;
 
         if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
-            if (frame->top_field_first)
-                params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
-            else
-                params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
+            if (frame->top_field_first) {
+                pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
+            } else {
+                pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
+            }
         } else {
-            params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
+            pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
         }
 
-        nvenc_codec_specific_pic_params(avctx, &params);
+        pic_params.encodePicFlags = 0;
+        pic_params.inputTimeStamp = frame->pts;
+        pic_params.inputDuration = 0;
+        switch (avctx->codec->id) {
+        case AV_CODEC_ID_H264:
+          pic_params.codecPicParams.h264PicParams.sliceMode = ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
+          pic_params.codecPicParams.h264PicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
+          break;
+        case AV_CODEC_ID_H265:
+          pic_params.codecPicParams.hevcPicParams.sliceMode = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
+          pic_params.codecPicParams.hevcPicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
+          break;
+        default:
+          av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
+          return AVERROR(EINVAL);
+        }
+
+        res = timestamp_queue_enqueue(&ctx->timestamp_list, frame->pts);
 
-        ret = nvenc_enqueue_timestamp(ctx->timestamps, frame->pts);
-        if (ret < 0)
-            return ret;
+        if (res)
+            return res;
     } else {
-        params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
+        pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
     }
 
-    ret = nv->nvEncEncodePicture(ctx->nvenc_ctx, &params);
+    nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
+
+    if (frame && nv_status == NV_ENC_ERR_NEED_MORE_INPUT) {
+        res = out_surf_queue_enqueue(&ctx->output_surface_queue, &ctx->output_surfaces[i]);
 
-    if (ret != NV_ENC_SUCCESS &&
-        ret != NV_ENC_ERR_NEED_MORE_INPUT) {
+        if (res)
+            return res;
 
-        return AVERROR_UNKNOWN;
+        ctx->output_surfaces[i].busy = 1;
     }
 
-    if (out) {
-        ret = nvenc_enqueue_surface(ctx->pending, out);
-        if (ret < 0)
-            return ret;
+    if (nv_status != NV_ENC_SUCCESS && nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
+        av_log(avctx, AV_LOG_ERROR, "EncodePicture failed!\n");
+        return AVERROR_EXTERNAL;
+    }
+
+    if (nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
+        while (ctx->output_surface_queue.count) {
+            tmpoutsurf = out_surf_queue_dequeue(&ctx->output_surface_queue);
+            res = out_surf_queue_enqueue(&ctx->output_surface_ready_queue, tmpoutsurf);
+
+            if (res)
+                return res;
+        }
+
+        if (frame) {
+            res = out_surf_queue_enqueue(&ctx->output_surface_ready_queue, &ctx->output_surfaces[i]);
+
+            if (res)
+                return res;
+
+            ctx->output_surfaces[i].busy = 1;
+        }
     }
 
-    if (ret != NV_ENC_ERR_NEED_MORE_INPUT &&
-        av_fifo_size(ctx->pending)) {
-        ret = nvenc_get_frame(avctx, pkt);
-        if (ret < 0)
-            return ret;
+    if (ctx->output_surface_ready_queue.count && (!frame || ctx->output_surface_ready_queue.count + ctx->output_surface_queue.count >= ctx->buffer_delay)) {
+        tmpoutsurf = out_surf_queue_dequeue(&ctx->output_surface_ready_queue);
+
+        res = process_output_surface(avctx, pkt, tmpoutsurf);
+
+        if (res)
+            return res;
+
+        tmpoutsurf->busy = 0;
+        av_assert0(tmpoutsurf->input_surface->lockCount);
+        tmpoutsurf->input_surface->lockCount--;
+
         *got_packet = 1;
     } else {
         *got_packet = 0;
@@ -1132,3 +1393,107 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 
     return 0;
 }
+
+static const enum AVPixelFormat pix_fmts_nvenc[] = {
+    AV_PIX_FMT_YUV420P,
+    AV_PIX_FMT_NV12,
+    AV_PIX_FMT_YUV444P,
+    AV_PIX_FMT_NONE
+};
+
+#define OFFSET(x) offsetof(NvencContext, x)
+#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "preset", "Set the encoding preset (one of hq, hp, bd, ll, llhq, llhp, default)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "hq" }, 0, 0, VE },
+    { "profile", "Set the encoding profile (high, main or baseline)", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
+    { "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, { 0 }, 0, 0, VE },
+    { "tier", "Set the encoding tier (main or high)", OFFSET(tier), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
+    { "cbr", "Use cbr encoding mode", OFFSET(cbr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
+    { "2pass", "Use 2pass cbr encoding mode", OFFSET(twopass), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
+    { "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 },
+    { "delay", "Delays frame output by the given amount of frames.", OFFSET(buffer_delay), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 0, INT_MAX, VE },
+    { NULL }
+};
+
+static const AVCodecDefault nvenc_defaults[] = {
+    { "b", "0" },
+    { "qmin", "-1" },
+    { "qmax", "-1" },
+    { "qdiff", "-1" },
+    { "qblur", "-1" },
+    { "qcomp", "-1" },
+    { NULL },
+};
+
+#if CONFIG_NVENC_ENCODER
+static const AVClass nvenc_class = {
+    .class_name = "nvenc",
+    .item_name = av_default_item_name,
+    .option = options,
+    .version = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_nvenc_encoder = {
+    .name = "nvenc",
+    .long_name = NULL_IF_CONFIG_SMALL("Nvidia NVENC h264 encoder"),
+    .type = AVMEDIA_TYPE_VIDEO,
+    .id = AV_CODEC_ID_H264,
+    .priv_data_size = sizeof(NvencContext),
+    .init = nvenc_encode_init,
+    .encode2 = nvenc_encode_frame,
+    .close = nvenc_encode_close,
+    .capabilities = CODEC_CAP_DELAY,
+    .priv_class = &nvenc_class,
+    .defaults = nvenc_defaults,
+    .pix_fmts = pix_fmts_nvenc,
+};
+#endif
+
+/* Add an alias for nvenc_h264 */
+#if CONFIG_NVENC_H264_ENCODER
+static const AVClass nvenc_h264_class = {
+    .class_name = "nvenc_h264",
+    .item_name = av_default_item_name,
+    .option = options,
+    .version = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_nvenc_h264_encoder = {
+    .name = "nvenc_h264",
+    .long_name = NULL_IF_CONFIG_SMALL("Nvidia NVENC h264 encoder"),
+    .type = AVMEDIA_TYPE_VIDEO,
+    .id = AV_CODEC_ID_H264,
+    .priv_data_size = sizeof(NvencContext),
+    .init = nvenc_encode_init,
+    .encode2 = nvenc_encode_frame,
+    .close = nvenc_encode_close,
+    .capabilities = CODEC_CAP_DELAY,
+    .priv_class = &nvenc_h264_class,
+    .defaults = nvenc_defaults,
+    .pix_fmts = pix_fmts_nvenc,
+};
+#endif
+
+#if CONFIG_NVENC_HEVC_ENCODER
+static const AVClass nvenc_hevc_class = {
+    .class_name = "nvenc_hevc",
+    .item_name = av_default_item_name,
+    .option = options,
+    .version = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_nvenc_hevc_encoder = {
+    .name = "nvenc_hevc",
+    .long_name = NULL_IF_CONFIG_SMALL("Nvidia NVENC hevc encoder"),
+    .type = AVMEDIA_TYPE_VIDEO,
+    .id = AV_CODEC_ID_H265,
+    .priv_data_size = sizeof(NvencContext),
+    .init = nvenc_encode_init,
+    .encode2 = nvenc_encode_frame,
+    .close = nvenc_encode_close,
+    .capabilities = CODEC_CAP_DELAY,
+    .priv_class = &nvenc_hevc_class,
+    .defaults = nvenc_defaults,
+    .pix_fmts = pix_fmts_nvenc,
+};
+#endif