diff options
Diffstat (limited to 'libavcodec/nvenc.c')
-rw-r--r-- | libavcodec/nvenc.c | 2005 |
1 files changed, 1186 insertions, 819 deletions
diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c index a7171e4..39c3aa1 100644 --- a/libavcodec/nvenc.c +++ b/libavcodec/nvenc.c @@ -1,98 +1,52 @@ /* - * NVIDIA NVENC Support - * Copyright (C) 2015 Luca Barbato - * Copyright (C) 2015 Philip Langdale <philipl@overt.org> - * Copyright (C) 2014 Timo Rothenpieler <timo@rothenpieler.org> + * H.264/HEVC hardware encoding using nvidia nvenc + * Copyright (c) 2016 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 <nvEncodeAPI.h> -#include <string.h> - -#define CUDA_LIBNAME "libcuda.so" - -#if HAVE_WINDOWS_H -#include <windows.h> - -#if ARCH_X86_64 -#define NVENC_LIBNAME "nvEncodeAPI64.dll" -#else -#define NVENC_LIBNAME "nvEncodeAPI.dll" -#endif - -#define dlopen(filename, flags) LoadLibrary((filename)) -#define dlsym(handle, symbol) GetProcAddress(handle, symbol) -#define dlclose(handle) FreeLibrary(handle) -#else -#include <dlfcn.h> -#define NVENC_LIBNAME "libnvidia-encode.so" -#endif +#include "nvenc.h" -#include "libavutil/common.h" +#include "libavutil/hwcontext_cuda.h" #include "libavutil/hwcontext.h" #include "libavutil/imgutils.h" +#include "libavutil/avassert.h" #include "libavutil/mem.h" -#include "avcodec.h" +#include "libavutil/pixdesc.h" #include "internal.h" -#include "nvenc.h" - -#if CONFIG_CUDA -#include "libavutil/hwcontext_cuda.h" -#endif #define NVENC_CAP 0x30 -#define BITSTREAM_BUFFER_SIZE 1024 * 1024 -#define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \ - rc == NV_ENC_PARAMS_RC_2_PASS_QUALITY || \ - rc == NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP) - -#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) +#define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \ + rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \ + rc == NV_ENC_PARAMS_RC_CBR_HQ) const enum AVPixelFormat ff_nvenc_pix_fmts[] = { - AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, - AV_PIX_FMT_YUV444P, -#if NVENCAPI_MAJOR_VERSION >= 7 + AV_PIX_FMT_NV12, AV_PIX_FMT_P010, + AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P16, -#endif -#if CONFIG_CUDA + AV_PIX_FMT_0RGB32, + AV_PIX_FMT_0BGR32, AV_PIX_FMT_CUDA, +#if CONFIG_D3D11VA + AV_PIX_FMT_D3D11, #endif AV_PIX_FMT_NONE }; @@ -121,14 +75,12 @@ static const struct { { NV_ENC_ERR_OUT_OF_MEMORY, AVERROR(ENOMEM), "out of memory" }, { NV_ENC_ERR_ENCODER_NOT_INITIALIZED, AVERROR(EINVAL), "encoder not initialized" }, { NV_ENC_ERR_UNSUPPORTED_PARAM, AVERROR(ENOSYS), "unsupported param" }, - { NV_ENC_ERR_LOCK_BUSY, AVERROR(EBUSY), "lock busy" }, - { NV_ENC_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOBUFS), "not enough buffer" }, + { NV_ENC_ERR_LOCK_BUSY, AVERROR(EAGAIN), "lock busy" }, + { NV_ENC_ERR_NOT_ENOUGH_BUFFER, AVERROR_BUFFER_TOO_SMALL, "not enough buffer"}, { NV_ENC_ERR_INVALID_VERSION, AVERROR(EINVAL), "invalid version" }, { NV_ENC_ERR_MAP_FAILED, AVERROR(EIO), "map failed" }, - /* this is error should always be treated specially, so this "mapping" - * is for completeness only */ - { NV_ENC_ERR_NEED_MORE_INPUT, AVERROR_UNKNOWN, "need more input" }, - { NV_ENC_ERR_ENCODER_BUSY, AVERROR(EBUSY), "encoder busy" }, + { NV_ENC_ERR_NEED_MORE_INPUT, AVERROR(EAGAIN), "need more input" }, + { NV_ENC_ERR_ENCODER_BUSY, AVERROR(EAGAIN), "encoder busy" }, { NV_ENC_ERR_EVENT_NOT_REGISTERD, AVERROR(EBADF), "event not registered" }, { NV_ENC_ERR_GENERIC, AVERROR_UNKNOWN, "generic error" }, { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, AVERROR(EINVAL), "incompatible client key" }, @@ -163,68 +115,118 @@ static int nvenc_print_error(void *log_ctx, NVENCSTATUS err, return ret; } +static void nvenc_print_driver_requirement(AVCodecContext *avctx, int level) +{ +#if defined(_WIN32) || defined(__CYGWIN__) + const char *minver = "378.66"; +#else + const char *minver = "378.13"; +#endif + av_log(avctx, level, "The minimum required Nvidia driver for nvenc is %s or newer\n", minver); +} + static av_cold int nvenc_load_libraries(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; - NVENCLibraryContext *nvel = &ctx->nvel; - PNVENCODEAPICREATEINSTANCE nvenc_create_instance; + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; NVENCSTATUS err; + uint32_t nvenc_max_ver; + int ret; -#if CONFIG_CUDA - nvel->cu_init = cuInit; - nvel->cu_device_get_count = cuDeviceGetCount; - nvel->cu_device_get = cuDeviceGet; - nvel->cu_device_get_name = cuDeviceGetName; - nvel->cu_device_compute_capability = cuDeviceComputeCapability; - nvel->cu_ctx_create = cuCtxCreate_v2; - nvel->cu_ctx_pop_current = cuCtxPopCurrent_v2; - nvel->cu_ctx_push_current = cuCtxPushCurrent_v2; - nvel->cu_ctx_destroy = cuCtxDestroy_v2; -#else - LOAD_LIBRARY(nvel->cuda, CUDA_LIBNAME); - - 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_push_current, nvel->cuda, "cuCtxPushCurrent_v2"); - LOAD_SYMBOL(nvel->cu_ctx_destroy, nvel->cuda, "cuCtxDestroy_v2"); -#endif + ret = cuda_load_functions(&dl_fn->cuda_dl, avctx); + if (ret < 0) + return ret; + + ret = nvenc_load_functions(&dl_fn->nvenc_dl, avctx); + if (ret < 0) { + nvenc_print_driver_requirement(avctx, AV_LOG_ERROR); + return ret; + } + + err = dl_fn->nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver); + if (err != NV_ENC_SUCCESS) + return nvenc_print_error(avctx, err, "Failed to query nvenc max version"); - LOAD_LIBRARY(nvel->nvenc, NVENC_LIBNAME); + av_log(avctx, AV_LOG_VERBOSE, "Loaded Nvenc version %d.%d\n", nvenc_max_ver >> 4, nvenc_max_ver & 0xf); - LOAD_SYMBOL(nvenc_create_instance, nvel->nvenc, - "NvEncodeAPICreateInstance"); + if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) { + av_log(avctx, AV_LOG_ERROR, "Driver does not support the required nvenc API version. " + "Required: %d.%d Found: %d.%d\n", + NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION, + nvenc_max_ver >> 4, nvenc_max_ver & 0xf); + nvenc_print_driver_requirement(avctx, AV_LOG_ERROR); + return AVERROR(ENOSYS); + } - nvel->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER; + dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER; - err = nvenc_create_instance(&nvel->nvenc_funcs); + err = dl_fn->nvenc_dl->NvEncodeAPICreateInstance(&dl_fn->nvenc_funcs); if (err != NV_ENC_SUCCESS) - return nvenc_print_error(avctx, err, "Cannot create the NVENC instance"); + return nvenc_print_error(avctx, err, "Failed to create nvenc instance"); + + av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n"); + + return 0; +} + +static int nvenc_push_context(AVCodecContext *avctx) +{ + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + CUresult cu_res; + + if (ctx->d3d11_device) + return 0; + + cu_res = dl_fn->cuda_dl->cuCtxPushCurrent(ctx->cu_context); + if (cu_res != CUDA_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "cuCtxPushCurrent failed\n"); + return AVERROR_EXTERNAL; + } return 0; } -static int nvenc_open_session(AVCodecContext *avctx) +static int nvenc_pop_context(AVCodecContext *avctx) +{ + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + CUresult cu_res; + CUcontext dummy; + + if (ctx->d3d11_device) + return 0; + + cu_res = dl_fn->cuda_dl->cuCtxPopCurrent(&dummy); + if (cu_res != CUDA_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "cuCtxPopCurrent failed\n"); + return AVERROR_EXTERNAL; + } + + return 0; +} + +static av_cold int nvenc_open_session(AVCodecContext *avctx) { 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; + NvencContext *ctx = avctx->priv_data; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs; + NVENCSTATUS 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; + if (ctx->d3d11_device) { + params.device = ctx->d3d11_device; + params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX; + } else { + params.device = ctx->cu_context; + params.deviceType = NV_ENC_DEVICE_TYPE_CUDA; + } - ret = nv->nvEncOpenEncodeSessionEx(¶ms, &ctx->nvenc_ctx); + ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &ctx->nvencoder); if (ret != NV_ENC_SUCCESS) { - ctx->nvenc_ctx = NULL; - return nvenc_print_error(avctx, ret, "Cannot open the NVENC Session"); + ctx->nvencoder = NULL; + return nvenc_print_error(avctx, ret, "OpenEncodeSessionEx failed"); } return 0; @@ -232,12 +234,12 @@ static int nvenc_open_session(AVCodecContext *avctx) static int nvenc_check_codec_support(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; - NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs; + NvencContext *ctx = avctx->priv_data; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs; int i, ret, count = 0; GUID *guids = NULL; - ret = nv->nvEncGetEncodeGUIDCount(ctx->nvenc_ctx, &count); + ret = p_nvenc->nvEncGetEncodeGUIDCount(ctx->nvencoder, &count); if (ret != NV_ENC_SUCCESS || !count) return AVERROR(ENOSYS); @@ -246,7 +248,7 @@ static int nvenc_check_codec_support(AVCodecContext *avctx) if (!guids) return AVERROR(ENOMEM); - ret = nv->nvEncGetEncodeGUIDs(ctx->nvenc_ctx, guids, count, &count); + ret = p_nvenc->nvEncGetEncodeGUIDs(ctx->nvencoder, guids, count, &count); if (ret != NV_ENC_SUCCESS) { ret = AVERROR(ENOSYS); goto fail; @@ -254,7 +256,7 @@ static int nvenc_check_codec_support(AVCodecContext *avctx) ret = AVERROR(ENOSYS); for (i = 0; i < count; i++) { - if (!memcmp(&guids[i], &ctx->params.encodeGUID, sizeof(*guids))) { + if (!memcmp(&guids[i], &ctx->init_encode_params.encodeGUID, sizeof(*guids))) { ret = 0; break; } @@ -268,15 +270,15 @@ fail: static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap) { - NVENCContext *ctx = avctx->priv_data; - NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs; + NvencContext *ctx = avctx->priv_data; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs; NV_ENC_CAPS_PARAM params = { 0 }; int ret, val = 0; params.version = NV_ENC_CAPS_PARAM_VER; params.capsToQuery = cap; - ret = nv->nvEncGetEncodeCaps(ctx->nvenc_ctx, ctx->params.encodeGUID, ¶ms, &val); + ret = p_nvenc->nvEncGetEncodeCaps(ctx->nvencoder, ctx->init_encode_params.encodeGUID, ¶ms, &val); if (ret == NV_ENC_SUCCESS) return val; @@ -285,7 +287,7 @@ static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap) static int nvenc_check_capabilities(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; + NvencContext *ctx = avctx->priv_data; int ret; ret = nvenc_check_codec_support(avctx); @@ -295,11 +297,17 @@ static int nvenc_check_capabilities(AVCodecContext *avctx) } ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_YUV444_ENCODE); - if (ctx->data_pix_fmt == AV_PIX_FMT_YUV444P && ret <= 0) { + if (IS_YUV444(ctx->data_pix_fmt) && ret <= 0) { av_log(avctx, AV_LOG_VERBOSE, "YUV444P not supported\n"); return AVERROR(ENOSYS); } + ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE); + if (ctx->preset >= PRESET_LOSSLESS_DEFAULT && ret <= 0) { + av_log(avctx, AV_LOG_VERBOSE, "Lossless encoding not supported\n"); + return AVERROR(ENOSYS); + } + 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", @@ -322,54 +330,99 @@ static int nvenc_check_capabilities(AVCodecContext *avctx) return AVERROR(ENOSYS); } + ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_FIELD_ENCODING); + if (ret < 1 && avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { + av_log(avctx, AV_LOG_VERBOSE, + "Interlaced encoding is not supported. Supported level: %d\n", + ret); + return AVERROR(ENOSYS); + } + + ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_10BIT_ENCODE); + if (IS_10BIT(ctx->data_pix_fmt) && ret <= 0) { + av_log(avctx, AV_LOG_VERBOSE, "10 bit encode not supported\n"); + return AVERROR(ENOSYS); + } + + ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOOKAHEAD); + if (ctx->rc_lookahead > 0 && ret <= 0) { + av_log(avctx, AV_LOG_VERBOSE, "RC lookahead not supported\n"); + return AVERROR(ENOSYS); + } + + ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ); + if (ctx->temporal_aq > 0 && ret <= 0) { + av_log(avctx, AV_LOG_VERBOSE, "Temporal AQ not supported\n"); + return AVERROR(ENOSYS); + } + + ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION); + if (ctx->weighted_pred > 0 && ret <= 0) { + av_log (avctx, AV_LOG_VERBOSE, "Weighted Prediction not supported\n"); + return AVERROR(ENOSYS); + } + + ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_CABAC); + if (ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC && ret <= 0) { + av_log(avctx, AV_LOG_VERBOSE, "CABAC entropy coding not supported\n"); + return AVERROR(ENOSYS); + } + return 0; } -static int nvenc_check_device(AVCodecContext *avctx, int idx) +static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx) { - NVENCContext *ctx = avctx->priv_data; - NVENCLibraryContext *nvel = &ctx->nvel; - char name[128] = { 0 }; + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; + char name[128] = { 0}; int major, minor, ret; + CUresult cu_res; 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) { + cu_res = dl_fn->cuda_dl->cuDeviceGet(&cu_device, idx); + if (cu_res != CUDA_SUCCESS) { av_log(avctx, AV_LOG_ERROR, "Cannot access the CUDA device %d\n", idx); return -1; } - ret = nvel->cu_device_get_name(name, sizeof(name), cu_device); - if (ret != CUDA_SUCCESS) + cu_res = dl_fn->cuda_dl->cuDeviceGetName(name, sizeof(name), cu_device); + if (cu_res != CUDA_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "cuDeviceGetName failed on device %d\n", idx); return -1; + } - ret = nvel->cu_device_compute_capability(&major, &minor, cu_device); - if (ret != CUDA_SUCCESS) + cu_res = dl_fn->cuda_dl->cuDeviceComputeCapability(&major, &minor, cu_device); + if (cu_res != CUDA_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "cuDeviceComputeCapability failed on device %d\n", idx); return -1; + } - av_log(avctx, loglevel, "Device %d [%s] ", cu_device, name); - - if (((major << 4) | minor) < NVENC_CAP) + av_log(avctx, loglevel, "[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx, name, major, minor); + if (((major << 4) | minor) < NVENC_CAP) { + av_log(avctx, loglevel, "does not support NVENC\n"); goto fail; + } if (ctx->device != idx && ctx->device != ANY_DEVICE) return -1; - ret = nvel->cu_ctx_create(&ctx->cu_context_internal, 0, cu_device); - if (ret != CUDA_SUCCESS) + cu_res = dl_fn->cuda_dl->cuCtxCreate(&ctx->cu_context_internal, 0, cu_device); + if (cu_res != CUDA_SUCCESS) { + av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res); goto fail; + } ctx->cu_context = ctx->cu_context_internal; - ret = nvel->cu_ctx_pop_current(&dummy); - if (ret != CUDA_SUCCESS) + if ((ret = nvenc_pop_context(avctx)) < 0) goto fail2; if ((ret = nvenc_open_session(avctx)) < 0) @@ -380,81 +433,120 @@ static int nvenc_check_device(AVCodecContext *avctx, int idx) av_log(avctx, loglevel, "supports NVENC\n"); + dl_fn->nvenc_device_count++; + if (ctx->device == idx || ctx->device == ANY_DEVICE) return 0; fail3: - nvel->nvenc_funcs.nvEncDestroyEncoder(ctx->nvenc_ctx); - ctx->nvenc_ctx = NULL; + if ((ret = nvenc_push_context(avctx)) < 0) + return ret; + + p_nvenc->nvEncDestroyEncoder(ctx->nvencoder); + ctx->nvencoder = NULL; + + if ((ret = nvenc_pop_context(avctx)) < 0) + return ret; fail2: - nvel->cu_ctx_destroy(ctx->cu_context_internal); + dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal); ctx->cu_context_internal = NULL; fail: - if (ret != 0) - av_log(avctx, loglevel, "does not support NVENC (major %d minor %d)\n", - major, minor); - return AVERROR(ENOSYS); } -static int nvenc_setup_device(AVCodecContext *avctx) +static av_cold int nvenc_setup_device(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; - NVENCLibraryContext *nvel = &ctx->nvel; + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; switch (avctx->codec->id) { case AV_CODEC_ID_H264: - ctx->params.encodeGUID = NV_ENC_CODEC_H264_GUID; + ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID; break; case AV_CODEC_ID_HEVC: - ctx->params.encodeGUID = NV_ENC_CODEC_HEVC_GUID; + ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID; break; default: return AVERROR_BUG; } - if (avctx->pix_fmt == AV_PIX_FMT_CUDA) { -#if CONFIG_CUDA + if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11 || avctx->hw_frames_ctx || avctx->hw_device_ctx) { AVHWFramesContext *frames_ctx; - AVCUDADeviceContext *device_hwctx; + AVHWDeviceContext *hwdev_ctx; + AVCUDADeviceContext *cuda_device_hwctx = NULL; +#if CONFIG_D3D11VA + AVD3D11VADeviceContext *d3d11_device_hwctx = NULL; +#endif int ret; - if (!avctx->hw_frames_ctx) + if (avctx->hw_frames_ctx) { + frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; + if (frames_ctx->format == AV_PIX_FMT_CUDA) + cuda_device_hwctx = frames_ctx->device_ctx->hwctx; +#if CONFIG_D3D11VA + else if (frames_ctx->format == AV_PIX_FMT_D3D11) + d3d11_device_hwctx = frames_ctx->device_ctx->hwctx; +#endif + else + return AVERROR(EINVAL); + } else if (avctx->hw_device_ctx) { + hwdev_ctx = (AVHWDeviceContext*)avctx->hw_device_ctx->data; + if (hwdev_ctx->type == AV_HWDEVICE_TYPE_CUDA) + cuda_device_hwctx = hwdev_ctx->hwctx; +#if CONFIG_D3D11VA + else if (hwdev_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) + d3d11_device_hwctx = hwdev_ctx->hwctx; +#endif + else + return AVERROR(EINVAL); + } else { return AVERROR(EINVAL); + } - frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; - device_hwctx = frames_ctx->device_ctx->hwctx; - - ctx->cu_context = device_hwctx->cuda_ctx; + if (cuda_device_hwctx) { + ctx->cu_context = cuda_device_hwctx->cuda_ctx; + } +#if CONFIG_D3D11VA + else if (d3d11_device_hwctx) { + ctx->d3d11_device = d3d11_device_hwctx->device; + ID3D11Device_AddRef(ctx->d3d11_device); + } +#endif ret = nvenc_open_session(avctx); if (ret < 0) return ret; ret = nvenc_check_capabilities(avctx); - if (ret < 0) + if (ret < 0) { + av_log(avctx, AV_LOG_FATAL, "Provided device doesn't support required NVENC features\n"); return ret; -#else - return AVERROR_BUG; -#endif + } } else { int i, nb_devices = 0; - if ((nvel->cu_init(0)) != CUDA_SUCCESS) { + if ((dl_fn->cuda_dl->cuInit(0)) != CUDA_SUCCESS) { av_log(avctx, AV_LOG_ERROR, "Cannot init CUDA\n"); return AVERROR_UNKNOWN; } - if ((nvel->cu_device_get_count(&nb_devices)) != CUDA_SUCCESS) { + if ((dl_fn->cuda_dl->cuDeviceGetCount(&nb_devices)) != CUDA_SUCCESS) { av_log(avctx, AV_LOG_ERROR, "Cannot enumerate the CUDA devices\n"); return AVERROR_UNKNOWN; } + if (!nb_devices) { + av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n"); + return AVERROR_EXTERNAL; + } + + av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", nb_devices); + dl_fn->nvenc_device_count = 0; for (i = 0; i < nb_devices; ++i) { if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES) return 0; @@ -463,7 +555,13 @@ static int nvenc_setup_device(AVCodecContext *avctx) if (ctx->device == LIST_DEVICES) return AVERROR_EXIT; - return AVERROR(ENOSYS); + if (!dl_fn->nvenc_device_count) { + av_log(avctx, AV_LOG_FATAL, "No NVENC capable devices found\n"); + return AVERROR_EXTERNAL; + } + + av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->device, nb_devices); + return AVERROR(EINVAL); } return 0; @@ -479,337 +577,465 @@ typedef struct GUIDTuple { #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__) -static int nvenc_map_preset(NVENCContext *ctx) +static void nvenc_map_preset(NvencContext *ctx) { GUIDTuple presets[] = { PRESET(DEFAULT), PRESET(HP), PRESET(HQ), PRESET(BD), + PRESET_ALIAS(SLOW, HQ, NVENC_TWO_PASSES), + PRESET_ALIAS(MEDIUM, HQ, NVENC_ONE_PASS), + PRESET_ALIAS(FAST, HP, NVENC_ONE_PASS), PRESET(LOW_LATENCY_DEFAULT, NVENC_LOWLATENCY), PRESET(LOW_LATENCY_HP, NVENC_LOWLATENCY), PRESET(LOW_LATENCY_HQ, NVENC_LOWLATENCY), PRESET(LOSSLESS_DEFAULT, NVENC_LOSSLESS), PRESET(LOSSLESS_HP, NVENC_LOSSLESS), - PRESET_ALIAS(SLOW, HQ, NVENC_TWO_PASSES), - PRESET_ALIAS(MEDIUM, HQ, NVENC_ONE_PASS), - PRESET_ALIAS(FAST, HP, NVENC_ONE_PASS) }; GUIDTuple *t = &presets[ctx->preset]; - ctx->params.presetGUID = t->guid; - ctx->flags = t->flags; - - return AVERROR(EINVAL); + ctx->init_encode_params.presetGUID = t->guid; + ctx->flags = t->flags; } #undef PRESET #undef PRESET_ALIAS -static void set_constqp(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc) +static av_cold void set_constqp(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; + NvencContext *ctx = avctx->priv_data; + NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams; + rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; if (ctx->init_qp_p >= 0) { rc->constQP.qpInterP = ctx->init_qp_p; if (ctx->init_qp_i >= 0 && ctx->init_qp_b >= 0) { - rc->constQP.qpIntra = ctx->init_qp_i; + rc->constQP.qpIntra = ctx->init_qp_i; rc->constQP.qpInterB = ctx->init_qp_b; } else if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) { - rc->constQP.qpIntra = av_clip(rc->constQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51); - rc->constQP.qpInterB = av_clip(rc->constQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51); + rc->constQP.qpIntra = av_clip( + rc->constQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51); + rc->constQP.qpInterB = av_clip( + rc->constQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51); } else { - rc->constQP.qpIntra = rc->constQP.qpInterP; + rc->constQP.qpIntra = rc->constQP.qpInterP; rc->constQP.qpInterB = rc->constQP.qpInterP; } - } else if (avctx->global_quality >= 0) { - rc->constQP.qpInterP = avctx->global_quality; - rc->constQP.qpInterB = avctx->global_quality; - rc->constQP.qpIntra = avctx->global_quality; + } else if (ctx->cqp >= 0) { + rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra = ctx->cqp; + if (avctx->b_quant_factor != 0.0) + rc->constQP.qpInterB = av_clip(ctx->cqp * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51); + if (avctx->i_quant_factor != 0.0) + rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51); } + + avctx->qmin = -1; + avctx->qmax = -1; } -static void set_vbr(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc) +static av_cold void set_vbr(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; + NvencContext *ctx = avctx->priv_data; + NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams; + int qp_inter_p; + + if (avctx->qmin >= 0 && avctx->qmax >= 0) { + rc->enableMinQP = 1; + rc->enableMaxQP = 1; - 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; + rc->maxQP.qpIntra = avctx->qmax; + + qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin + } else if (avctx->qmin >= 0) { + rc->enableMinQP = 1; + + rc->minQP.qpInterB = avctx->qmin; + rc->minQP.qpInterP = avctx->qmin; + rc->minQP.qpIntra = avctx->qmin; + + qp_inter_p = avctx->qmin; + } else { + qp_inter_p = 26; // default to 26 } - if (ctx->init_qp_p >= 0) { - rc->enableInitialRCQP = 1; + rc->enableInitialRCQP = 1; + + if (ctx->init_qp_p < 0) { + rc->initialRCQP.qpInterP = qp_inter_p; + } else { rc->initialRCQP.qpInterP = ctx->init_qp_p; - if (ctx->init_qp_i < 0) { - if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) { - rc->initialRCQP.qpIntra = av_clip(rc->initialRCQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51); - } else { - rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP; - } + } + + if (ctx->init_qp_i < 0) { + if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) { + rc->initialRCQP.qpIntra = av_clip( + rc->initialRCQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, 51); } else { - rc->initialRCQP.qpIntra = ctx->init_qp_i; + rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP; } + } else { + rc->initialRCQP.qpIntra = ctx->init_qp_i; + } - if (ctx->init_qp_b < 0) { - if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) { - rc->initialRCQP.qpInterB = av_clip(rc->initialRCQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51); - } else { - rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP; - } + if (ctx->init_qp_b < 0) { + if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) { + rc->initialRCQP.qpInterB = av_clip( + rc->initialRCQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, 51); } else { - rc->initialRCQP.qpInterB = ctx->init_qp_b; + rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP; } + } else { + rc->initialRCQP.qpInterB = ctx->init_qp_b; } } -static void set_lossless(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc) +static av_cold void set_lossless(AVCodecContext *avctx) { - rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; + NvencContext *ctx = avctx->priv_data; + NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams; + + rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; rc->constQP.qpInterB = 0; rc->constQP.qpInterP = 0; rc->constQP.qpIntra = 0; + + avctx->qmin = -1; + avctx->qmax = -1; } -static void nvenc_override_rate_control(AVCodecContext *avctx, - NV_ENC_RC_PARAMS *rc) +static void nvenc_override_rate_control(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; + NvencContext *ctx = avctx->priv_data; + NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams; switch (ctx->rc) { case NV_ENC_PARAMS_RC_CONSTQP: - set_constqp(avctx, rc); + set_constqp(avctx); return; - case NV_ENC_PARAMS_RC_2_PASS_VBR: - case NV_ENC_PARAMS_RC_VBR: - set_vbr(avctx, rc); - break; 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"); + set_vbr(avctx); return; } - set_vbr(avctx, rc); + /* fall through */ + case NV_ENC_PARAMS_RC_VBR_HQ: + case NV_ENC_PARAMS_RC_VBR: + set_vbr(avctx); break; case NV_ENC_PARAMS_RC_CBR: + case NV_ENC_PARAMS_RC_CBR_HQ: + case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ: break; - case NV_ENC_PARAMS_RC_2_PASS_QUALITY: - case NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP: - if (!(ctx->flags & NVENC_LOWLATENCY)) { + } + + rc->rateControlMode = ctx->rc; +} + +static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx) +{ + NvencContext *ctx = avctx->priv_data; + // default minimum of 4 surfaces + // multiply by 2 for number of NVENCs on gpu (hardcode to 2) + // another multiply by 2 to avoid blocking next PBB group + int nb_surfaces = FFMAX(4, ctx->encode_config.frameIntervalP * 2 * 2); + + // lookahead enabled + if (ctx->rc_lookahead > 0) { + // +1 is to account for lkd_bound calculation later + // +4 is to allow sufficient pipelining with lookahead + nb_surfaces = FFMAX(1, FFMAX(nb_surfaces, ctx->rc_lookahead + ctx->encode_config.frameIntervalP + 1 + 4)); + if (nb_surfaces > ctx->nb_surfaces && ctx->nb_surfaces > 0) + { av_log(avctx, AV_LOG_WARNING, - "The multipass rate-control requires " - "a low-latency preset.\n"); - return; + "Defined rc_lookahead requires more surfaces, " + "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces); + } + ctx->nb_surfaces = FFMAX(nb_surfaces, ctx->nb_surfaces); + } else { + if (ctx->encode_config.frameIntervalP > 1 && ctx->nb_surfaces < nb_surfaces && ctx->nb_surfaces > 0) + { + av_log(avctx, AV_LOG_WARNING, + "Defined b-frame requires more surfaces, " + "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces); + ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, nb_surfaces); } + else if (ctx->nb_surfaces <= 0) + ctx->nb_surfaces = nb_surfaces; + // otherwise use user specified value } - rc->rateControlMode = ctx->rc; + ctx->nb_surfaces = FFMAX(1, FFMIN(MAX_REGISTERED_FRAMES, ctx->nb_surfaces)); + ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1); + + return 0; } -static void nvenc_setup_rate_control(AVCodecContext *avctx) +static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; - NV_ENC_RC_PARAMS *rc = &ctx->config.rcParams; + NvencContext *ctx = avctx->priv_data; + + if (avctx->global_quality > 0) + av_log(avctx, AV_LOG_WARNING, "Using global_quality with nvenc is deprecated. Use qp instead.\n"); - if (avctx->bit_rate > 0) - rc->averageBitRate = avctx->bit_rate; + if (ctx->cqp < 0 && avctx->global_quality > 0) + ctx->cqp = avctx->global_quality; + + if (avctx->bit_rate > 0) { + ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate; + } else if (ctx->encode_config.rcParams.averageBitRate > 0) { + ctx->encode_config.rcParams.maxBitRate = ctx->encode_config.rcParams.averageBitRate; + } if (avctx->rc_max_rate > 0) - rc->maxBitRate = avctx->rc_max_rate; - - if (ctx->rc > 0) { - nvenc_override_rate_control(avctx, rc); - } else if (ctx->flags & NVENC_LOSSLESS) { - set_lossless(avctx, rc); - } else if (avctx->global_quality > 0) { - set_constqp(avctx, rc); - } else { + ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate; + + if (ctx->rc < 0) { + if (ctx->flags & NVENC_ONE_PASS) + ctx->twopass = 0; if (ctx->flags & NVENC_TWO_PASSES) - rc->rateControlMode = NV_ENC_PARAMS_RC_2_PASS_VBR; - else - rc->rateControlMode = NV_ENC_PARAMS_RC_VBR; - set_vbr(avctx, rc); + ctx->twopass = 1; + + if (ctx->twopass < 0) + ctx->twopass = (ctx->flags & NVENC_LOWLATENCY) != 0; + + if (ctx->cbr) { + if (ctx->twopass) { + ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ; + } else { + ctx->rc = NV_ENC_PARAMS_RC_CBR; + } + } else if (ctx->cqp >= 0) { + ctx->rc = NV_ENC_PARAMS_RC_CONSTQP; + } else if (ctx->twopass) { + ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ; + } else if (avctx->qmin >= 0 && avctx->qmax >= 0) { + ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP; + } + } + + if (ctx->rc >= 0 && ctx->rc & RC_MODE_DEPRECATED) { + av_log(avctx, AV_LOG_WARNING, "Specified rc mode is deprecated.\n"); + av_log(avctx, AV_LOG_WARNING, "\tll_2pass_quality -> cbr_ld_hq\n"); + av_log(avctx, AV_LOG_WARNING, "\tll_2pass_size -> cbr_hq\n"); + av_log(avctx, AV_LOG_WARNING, "\tvbr_2pass -> vbr_hq\n"); + av_log(avctx, AV_LOG_WARNING, "\tvbr_minqp -> (no replacement)\n"); + + ctx->rc &= ~RC_MODE_DEPRECATED; } - if (avctx->rc_buffer_size > 0) - rc->vbvBufferSize = avctx->rc_buffer_size; + if (ctx->flags & NVENC_LOSSLESS) { + set_lossless(avctx); + } else if (ctx->rc >= 0) { + nvenc_override_rate_control(avctx); + } else { + ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR; + set_vbr(avctx); + } - if (rc->averageBitRate > 0) - avctx->bit_rate = rc->averageBitRate; + if (avctx->rc_buffer_size > 0) { + ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size; + } else if (ctx->encode_config.rcParams.averageBitRate > 0) { + ctx->encode_config.rcParams.vbvBufferSize = 2 * ctx->encode_config.rcParams.averageBitRate; + } -#if NVENCAPI_MAJOR_VERSION >= 7 if (ctx->aq) { - ctx->config.rcParams.enableAQ = 1; - ctx->config.rcParams.aqStrength = ctx->aq_strength; + ctx->encode_config.rcParams.enableAQ = 1; + ctx->encode_config.rcParams.aqStrength = ctx->aq_strength; av_log(avctx, AV_LOG_VERBOSE, "AQ enabled.\n"); } if (ctx->temporal_aq) { - ctx->config.rcParams.enableTemporalAQ = 1; + ctx->encode_config.rcParams.enableTemporalAQ = 1; av_log(avctx, AV_LOG_VERBOSE, "Temporal AQ enabled.\n"); } if (ctx->rc_lookahead > 0) { int lkd_bound = FFMIN(ctx->nb_surfaces, ctx->async_depth) - - ctx->config.frameIntervalP - 4; + ctx->encode_config.frameIntervalP - 4; if (lkd_bound < 0) { av_log(avctx, AV_LOG_WARNING, "Lookahead not enabled. Increase buffer delay (-delay).\n"); } else { - ctx->config.rcParams.enableLookahead = 1; - ctx->config.rcParams.lookaheadDepth = av_clip(ctx->rc_lookahead, 0, lkd_bound); - ctx->config.rcParams.disableIadapt = ctx->no_scenecut; - ctx->config.rcParams.disableBadapt = !ctx->b_adapt; + ctx->encode_config.rcParams.enableLookahead = 1; + ctx->encode_config.rcParams.lookaheadDepth = av_clip(ctx->rc_lookahead, 0, lkd_bound); + ctx->encode_config.rcParams.disableIadapt = ctx->no_scenecut; + ctx->encode_config.rcParams.disableBadapt = !ctx->b_adapt; av_log(avctx, AV_LOG_VERBOSE, "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n", - ctx->config.rcParams.lookaheadDepth, - ctx->config.rcParams.disableIadapt ? "disabled" : "enabled", - ctx->config.rcParams.disableBadapt ? "disabled" : "enabled"); + ctx->encode_config.rcParams.lookaheadDepth, + ctx->encode_config.rcParams.disableIadapt ? "disabled" : "enabled", + ctx->encode_config.rcParams.disableBadapt ? "disabled" : "enabled"); } } if (ctx->strict_gop) { - ctx->config.rcParams.strictGOPTarget = 1; + ctx->encode_config.rcParams.strictGOPTarget = 1; av_log(avctx, AV_LOG_VERBOSE, "Strict GOP target enabled.\n"); } if (ctx->nonref_p) - ctx->config.rcParams.enableNonRefP = 1; + ctx->encode_config.rcParams.enableNonRefP = 1; if (ctx->zerolatency) - ctx->config.rcParams.zeroReorderDelay = 1; + ctx->encode_config.rcParams.zeroReorderDelay = 1; if (ctx->quality) - ctx->config.rcParams.targetQuality = ctx->quality; -#endif /* NVENCAPI_MAJOR_VERSION >= 7 */ + { + //convert from float to fixed point 8.8 + int tmp_quality = (int)(ctx->quality * 256.0f); + ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8); + ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff); + } } -static int nvenc_setup_h264_config(AVCodecContext *avctx) +static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; - NV_ENC_CONFIG *cc = &ctx->config; + NvencContext *ctx = avctx->priv_data; + NV_ENC_CONFIG *cc = &ctx->encode_config; NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config; NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters; - vui->colourDescriptionPresentFlag = avctx->colorspace != AVCOL_SPC_UNSPECIFIED || - avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || - avctx->color_trc != AVCOL_TRC_UNSPECIFIED; - - vui->colourMatrix = avctx->colorspace; - vui->colourPrimaries = avctx->color_primaries; + vui->colourMatrix = avctx->colorspace; + vui->colourPrimaries = avctx->color_primaries; vui->transferCharacteristics = avctx->color_trc; + vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG + || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); - vui->videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG; + vui->colourDescriptionPresentFlag = + (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2); - vui->videoSignalTypePresentFlag = vui->colourDescriptionPresentFlag || - vui->videoFullRangeFlag; + vui->videoSignalTypePresentFlag = + (vui->colourDescriptionPresentFlag + || vui->videoFormat != 5 + || vui->videoFullRangeFlag != 0); + + h264->sliceMode = 3; + h264->sliceModeData = 1; h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0; h264->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1; - h264->outputAUD = 1; - - h264->maxNumRefFrames = avctx->refs; - h264->idrPeriod = cc->gopLength; + h264->outputAUD = ctx->aud; - h264->sliceMode = 3; - h264->sliceModeData = FFMAX(avctx->slices, 1); - - if (ctx->flags & NVENC_LOSSLESS) - h264->qpPrimeYZeroTransformBypassFlag = 1; + if (avctx->refs >= 0) { + /* 0 means "let the hardware decide" */ + h264->maxNumRefFrames = avctx->refs; + } + if (avctx->gop_size >= 0) { + h264->idrPeriod = cc->gopLength; + } if (IS_CBR(cc->rcParams.rateControlMode)) { h264->outputBufferingPeriodSEI = 1; - h264->outputPictureTimingSEI = 1; } - if (ctx->profile) - avctx->profile = ctx->profile; + h264->outputPictureTimingSEI = 1; - if (ctx->data_pix_fmt == AV_PIX_FMT_YUV444P) - h264->chromaFormatIDC = 3; - else - h264->chromaFormatIDC = 1; + if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || + cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ || + cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) { + h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE; + h264->fmoMode = NV_ENC_H264_FMO_DISABLE; + } - 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; + if (ctx->flags & NVENC_LOSSLESS) { + h264->qpPrimeYZeroTransformBypassFlag = 1; + } else { + switch(ctx->profile) { + case NV_ENC_H264_PROFILE_BASELINE: + cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID; + avctx->profile = FF_PROFILE_H264_BASELINE; + break; + case NV_ENC_H264_PROFILE_MAIN: + cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID; + avctx->profile = FF_PROFILE_H264_MAIN; + break; + case NV_ENC_H264_PROFILE_HIGH: + cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID; + avctx->profile = FF_PROFILE_H264_HIGH; + break; + case NV_ENC_H264_PROFILE_HIGH_444P: + cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID; + avctx->profile = FF_PROFILE_H264_HIGH_444_PREDICTIVE; + break; + } } + // force setting profile as high444p if input is AV_PIX_FMT_YUV444P if (ctx->data_pix_fmt == AV_PIX_FMT_YUV444P) { cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID; avctx->profile = FF_PROFILE_H264_HIGH_444_PREDICTIVE; } + h264->chromaFormatIDC = avctx->profile == FF_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1; + h264->level = ctx->level; + if (ctx->coder >= 0) + h264->entropyCodingMode = ctx->coder; + return 0; } -static int nvenc_setup_hevc_config(AVCodecContext *avctx) +static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; - NV_ENC_CONFIG *cc = &ctx->config; + NvencContext *ctx = avctx->priv_data; + NV_ENC_CONFIG *cc = &ctx->encode_config; NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig; NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters; - vui->colourDescriptionPresentFlag = avctx->colorspace != AVCOL_SPC_UNSPECIFIED || - avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || - avctx->color_trc != AVCOL_TRC_UNSPECIFIED; - - vui->colourMatrix = avctx->colorspace; - vui->colourPrimaries = avctx->color_primaries; + vui->colourMatrix = avctx->colorspace; + vui->colourPrimaries = avctx->color_primaries; vui->transferCharacteristics = avctx->color_trc; + vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG + || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); + + vui->colourDescriptionPresentFlag = + (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2); - vui->videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG; + vui->videoSignalTypePresentFlag = + (vui->colourDescriptionPresentFlag + || vui->videoFormat != 5 + || vui->videoFullRangeFlag != 0); - vui->videoSignalTypePresentFlag = vui->colourDescriptionPresentFlag || - vui->videoFullRangeFlag; + hevc->sliceMode = 3; + hevc->sliceModeData = 1; hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0; hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1; - hevc->outputAUD = 1; + hevc->outputAUD = ctx->aud; - hevc->maxNumRefFramesInDPB = avctx->refs; - hevc->idrPeriod = cc->gopLength; + if (avctx->refs >= 0) { + /* 0 means "let the hardware decide" */ + hevc->maxNumRefFramesInDPB = avctx->refs; + } + if (avctx->gop_size >= 0) { + hevc->idrPeriod = cc->gopLength; + } if (IS_CBR(cc->rcParams.rateControlMode)) { hevc->outputBufferingPeriodSEI = 1; - hevc->outputPictureTimingSEI = 1; } + hevc->outputPictureTimingSEI = 1; + switch (ctx->profile) { case NV_ENC_HEVC_PROFILE_MAIN: cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID; avctx->profile = FF_PROFILE_HEVC_MAIN; break; -#if NVENCAPI_MAJOR_VERSION >= 7 case NV_ENC_HEVC_PROFILE_MAIN_10: cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID; avctx->profile = FF_PROFILE_HEVC_MAIN_10; @@ -818,177 +1044,126 @@ static int nvenc_setup_hevc_config(AVCodecContext *avctx) cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID; avctx->profile = FF_PROFILE_HEVC_REXT; break; -#endif /* NVENCAPI_MAJOR_VERSION >= 7 */ } - // force setting profile for various input formats - switch (ctx->data_pix_fmt) { - case AV_PIX_FMT_YUV420P: - case AV_PIX_FMT_NV12: - cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID; - avctx->profile = FF_PROFILE_HEVC_MAIN; - break; -#if NVENCAPI_MAJOR_VERSION >= 7 - case AV_PIX_FMT_P010: + // force setting profile as main10 if input is 10 bit + if (IS_10BIT(ctx->data_pix_fmt)) { cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID; - avctx->profile = FF_PROFILE_HEVC_MAIN_10; - break; - case AV_PIX_FMT_YUV444P: - case AV_PIX_FMT_YUV444P16: + avctx->profile = FF_PROFILE_HEVC_MAIN_10; + } + + // force setting profile as rext if input is yuv444 + if (IS_YUV444(ctx->data_pix_fmt)) { cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID; - avctx->profile = FF_PROFILE_HEVC_REXT; - break; -#endif /* NVENCAPI_MAJOR_VERSION >= 7 */ + avctx->profile = FF_PROFILE_HEVC_REXT; } -#if NVENCAPI_MAJOR_VERSION >= 7 - hevc->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1; - hevc->pixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0; -#endif /* NVENCAPI_MAJOR_VERSION >= 7 */ + hevc->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1; - hevc->sliceMode = 3; - hevc->sliceModeData = FFMAX(avctx->slices, 1); + hevc->pixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0; - if (ctx->level) { - hevc->level = ctx->level; - } else { - hevc->level = NV_ENC_LEVEL_AUTOSELECT; - } + hevc->level = ctx->level; - if (ctx->tier) { - hevc->tier = ctx->tier; - } + hevc->tier = ctx->tier; return 0; } -static int nvenc_setup_codec_config(AVCodecContext *avctx) + +static av_cold 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); - } - return 0; -} - -static int nvenc_recalc_surfaces(AVCodecContext *avctx) -{ - NVENCContext *ctx = avctx->priv_data; - // default minimum of 4 surfaces - // multiply by 2 for number of NVENCs on gpu (hardcode to 2) - // another multiply by 2 to avoid blocking next PBB group - int nb_surfaces = FFMAX(4, ctx->config.frameIntervalP * 2 * 2); - - // lookahead enabled - if (ctx->rc_lookahead > 0) { - // +1 is to account for lkd_bound calculation later - // +4 is to allow sufficient pipelining with lookahead - nb_surfaces = FFMAX(1, FFMAX(nb_surfaces, ctx->rc_lookahead + ctx->config.frameIntervalP + 1 + 4)); - if (nb_surfaces > ctx->nb_surfaces && ctx->nb_surfaces > 0) { - av_log(avctx, AV_LOG_WARNING, - "Defined rc_lookahead requires more surfaces, " - "increasing used surfaces %d -> %d\n", - ctx->nb_surfaces, nb_surfaces); - } - ctx->nb_surfaces = FFMAX(nb_surfaces, ctx->nb_surfaces); - } else { - if (ctx->config.frameIntervalP > 1 && - ctx->nb_surfaces < nb_surfaces && ctx->nb_surfaces > 0) { - av_log(avctx, AV_LOG_WARNING, - "Defined b-frame requires more surfaces, " - "increasing used surfaces %d -> %d\n", - ctx->nb_surfaces, nb_surfaces); - ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, nb_surfaces); - } else if (ctx->nb_surfaces <= 0) - ctx->nb_surfaces = nb_surfaces; - // otherwise use user specified value + /* Earlier switch/case will return if unknown codec is passed. */ } - ctx->nb_surfaces = FFMAX(1, FFMIN(MAX_REGISTERED_FRAMES, ctx->nb_surfaces)); - ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1); return 0; } -static int nvenc_setup_encoder(AVCodecContext *avctx) +static av_cold 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 }; - AVCPBProperties *cpb_props; - int ret; + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; - 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; - } - - // 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(&ctx->params.darWidth, &ctx->params.darHeight, - ctx->params.darWidth * 44, - ctx->params.darHeight * 45, - 1024 * 1024); - } + NV_ENC_PRESET_CONFIG preset_config = { 0 }; + NVENCSTATUS nv_status = NV_ENC_SUCCESS; + AVCPBProperties *cpb_props; + int res = 0; + int dw, dh; - ctx->params.frameRateNum = avctx->time_base.den; - ctx->params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame; + ctx->encode_config.version = NV_ENC_CONFIG_VER; + ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER; - ctx->params.enableEncodeAsync = 0; - ctx->params.enablePTD = 1; + ctx->init_encode_params.encodeHeight = avctx->height; + ctx->init_encode_params.encodeWidth = avctx->width; - ctx->params.encodeConfig = &ctx->config; + ctx->init_encode_params.encodeConfig = &ctx->encode_config; nvenc_map_preset(ctx); - preset_cfg.version = NV_ENC_PRESET_CONFIG_VER; - preset_cfg.presetCfg.version = NV_ENC_CONFIG_VER; + preset_config.version = NV_ENC_PRESET_CONFIG_VER; + preset_config.presetCfg.version = NV_ENC_CONFIG_VER; + + nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, + ctx->init_encode_params.encodeGUID, + ctx->init_encode_params.presetGUID, + &preset_config); + if (nv_status != NV_ENC_SUCCESS) + return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration"); - ret = nv->nvEncGetEncodePresetConfig(ctx->nvenc_ctx, - ctx->params.encodeGUID, - ctx->params.presetGUID, - &preset_cfg); - if (ret != NV_ENC_SUCCESS) - return nvenc_print_error(avctx, ret, "Cannot get the preset configuration"); + memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config)); - memcpy(&ctx->config, &preset_cfg.presetCfg, sizeof(ctx->config)); + ctx->encode_config.version = NV_ENC_CONFIG_VER; - ctx->config.version = NV_ENC_CONFIG_VER; + dw = avctx->width; + dh = avctx->height; + if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) { + dw*= avctx->sample_aspect_ratio.num; + dh*= avctx->sample_aspect_ratio.den; + } + av_reduce(&dw, &dh, dw, dh, 1024 * 1024); + ctx->init_encode_params.darHeight = dh; + ctx->init_encode_params.darWidth = dw; + + ctx->init_encode_params.frameRateNum = avctx->time_base.den; + ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame; + + ctx->init_encode_params.enableEncodeAsync = 0; + ctx->init_encode_params.enablePTD = 1; + + if (ctx->weighted_pred == 1) + ctx->init_encode_params.enableWeightedPrediction = 1; + + if (ctx->bluray_compat) { + ctx->aud = 1; + avctx->refs = FFMIN(FFMAX(avctx->refs, 0), 6); + avctx->max_b_frames = FFMIN(avctx->max_b_frames, 3); + switch (avctx->codec->id) { + case AV_CODEC_ID_H264: + /* maximum level depends on used resolution */ + break; + case AV_CODEC_ID_HEVC: + ctx->level = NV_ENC_LEVEL_HEVC_51; + ctx->tier = NV_ENC_TIER_HEVC_HIGH; + break; + } + } 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->config.frameIntervalP = avctx->max_b_frames + 1; - } else if (avctx->max_b_frames == 0) { - ctx->config.frameIntervalP = 1; + 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; } - ctx->config.gopLength = avctx->gop_size; + + ctx->encode_config.gopLength = avctx->gop_size; } else if (avctx->gop_size == 0) { - ctx->config.frameIntervalP = 0; - ctx->config.gopLength = 1; + ctx->encode_config.frameIntervalP = 0; + ctx->encode_config.gopLength = 1; } - if (ctx->config.frameIntervalP > 1) - avctx->max_b_frames = ctx->config.frameIntervalP - 1; - ctx->initial_pts[0] = AV_NOPTS_VALUE; ctx->initial_pts[1] = AV_NOPTS_VALUE; @@ -997,216 +1172,279 @@ static int nvenc_setup_encoder(AVCodecContext *avctx) nvenc_setup_rate_control(avctx); if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { - ctx->config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD; + ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD; } else { - ctx->config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME; + ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME; } - if ((ret = nvenc_setup_codec_config(avctx)) < 0) - return ret; + res = nvenc_setup_codec_config(avctx); + if (res) + return res; + + res = nvenc_push_context(avctx); + if (res < 0) + return res; + + nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params); + + res = nvenc_pop_context(avctx); + if (res < 0) + return res; - ret = nv->nvEncInitializeEncoder(ctx->nvenc_ctx, &ctx->params); - if (ret != NV_ENC_SUCCESS) - return nvenc_print_error(avctx, ret, "InitializeEncoder failed"); + if (nv_status != NV_ENC_SUCCESS) { + return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed"); + } + + if (ctx->encode_config.frameIntervalP > 1) + avctx->has_b_frames = 2; + + if (ctx->encode_config.rcParams.averageBitRate > 0) + avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate; cpb_props = ff_add_cpb_side_data(avctx); if (!cpb_props) return AVERROR(ENOMEM); - cpb_props->max_bitrate = avctx->rc_max_rate; - cpb_props->min_bitrate = avctx->rc_min_rate; + cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate; cpb_props->avg_bitrate = avctx->bit_rate; - cpb_props->buffer_size = avctx->rc_buffer_size; + cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize; return 0; } -static int nvenc_alloc_surface(AVCodecContext *avctx, int idx) +static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt) { - NVENCContext *ctx = avctx->priv_data; - NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs; - NVENCFrame *tmp_surface = &ctx->frames[idx]; - int ret; - NV_ENC_CREATE_BITSTREAM_BUFFER out_buffer = { 0 }; - - switch (ctx->data_pix_fmt) { + switch (pix_fmt) { case AV_PIX_FMT_YUV420P: - ctx->frames[idx].format = NV_ENC_BUFFER_FORMAT_YV12_PL; - break; + return NV_ENC_BUFFER_FORMAT_YV12_PL; case AV_PIX_FMT_NV12: - ctx->frames[idx].format = NV_ENC_BUFFER_FORMAT_NV12_PL; - break; - case AV_PIX_FMT_YUV444P: - ctx->frames[idx].format = NV_ENC_BUFFER_FORMAT_YUV444_PL; - break; -#if NVENCAPI_MAJOR_VERSION >= 7 + return NV_ENC_BUFFER_FORMAT_NV12_PL; case AV_PIX_FMT_P010: - ctx->frames[idx].format = NV_ENC_BUFFER_FORMAT_YUV420_10BIT; - break; + return NV_ENC_BUFFER_FORMAT_YUV420_10BIT; + case AV_PIX_FMT_YUV444P: + return NV_ENC_BUFFER_FORMAT_YUV444_PL; case AV_PIX_FMT_YUV444P16: - ctx->frames[idx].format = NV_ENC_BUFFER_FORMAT_YUV444_10BIT; - break; -#endif /* NVENCAPI_MAJOR_VERSION >= 7 */ + return NV_ENC_BUFFER_FORMAT_YUV444_10BIT; + case AV_PIX_FMT_0RGB32: + return NV_ENC_BUFFER_FORMAT_ARGB; + case AV_PIX_FMT_0BGR32: + return NV_ENC_BUFFER_FORMAT_ABGR; default: - return AVERROR_BUG; + return NV_ENC_BUFFER_FORMAT_UNDEFINED; } +} - if (avctx->pix_fmt == AV_PIX_FMT_CUDA) { - ctx->frames[idx].in_ref = av_frame_alloc(); - if (!ctx->frames[idx].in_ref) +static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx) +{ + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; + NvencSurface* tmp_surface = &ctx->surfaces[idx]; + + NVENCSTATUS nv_status; + NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 }; + allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER; + + if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) { + ctx->surfaces[idx].in_ref = av_frame_alloc(); + if (!ctx->surfaces[idx].in_ref) return AVERROR(ENOMEM); } else { - NV_ENC_CREATE_INPUT_BUFFER in_buffer = { 0 }; + NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 }; - in_buffer.version = NV_ENC_CREATE_INPUT_BUFFER_VER; - - in_buffer.width = avctx->width; - in_buffer.height = avctx->height; + ctx->surfaces[idx].format = nvenc_map_buffer_format(ctx->data_pix_fmt); + if (ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) { + av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n", + av_get_pix_fmt_name(ctx->data_pix_fmt)); + return AVERROR(EINVAL); + } - in_buffer.bufferFmt = ctx->frames[idx].format; - in_buffer.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED; + allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER; + allocSurf.width = avctx->width; + allocSurf.height = avctx->height; + allocSurf.bufferFmt = ctx->surfaces[idx].format; - ret = nv->nvEncCreateInputBuffer(ctx->nvenc_ctx, &in_buffer); - if (ret != NV_ENC_SUCCESS) - return nvenc_print_error(avctx, ret, "CreateInputBuffer failed"); + nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf); + if (nv_status != NV_ENC_SUCCESS) { + return nvenc_print_error(avctx, nv_status, "CreateInputBuffer failed"); + } - ctx->frames[idx].in = in_buffer.inputBuffer; + ctx->surfaces[idx].input_surface = allocSurf.inputBuffer; + ctx->surfaces[idx].width = allocSurf.width; + ctx->surfaces[idx].height = allocSurf.height; } - out_buffer.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER; - /* 1MB is large enough to hold most output frames. - * NVENC increases this automatically if it is not enough. */ - out_buffer.size = BITSTREAM_BUFFER_SIZE; - - out_buffer.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED; - - ret = nv->nvEncCreateBitstreamBuffer(ctx->nvenc_ctx, &out_buffer); - if (ret != NV_ENC_SUCCESS) - return nvenc_print_error(avctx, ret, "CreateBitstreamBuffer failed"); + nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut); + if (nv_status != NV_ENC_SUCCESS) { + int err = nvenc_print_error(avctx, nv_status, "CreateBitstreamBuffer failed"); + if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11) + p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[idx].input_surface); + av_frame_free(&ctx->surfaces[idx].in_ref); + return err; + } - ctx->frames[idx].out = out_buffer.bitstreamBuffer; + ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer; + ctx->surfaces[idx].size = allocOut.size; av_fifo_generic_write(ctx->unused_surface_queue, &tmp_surface, sizeof(tmp_surface), NULL); return 0; } -static int nvenc_setup_surfaces(AVCodecContext *avctx) +static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; - int i, ret; + NvencContext *ctx = avctx->priv_data; + int i, res = 0, res2; - ctx->frames = av_mallocz_array(ctx->nb_surfaces, sizeof(*ctx->frames)); - if (!ctx->frames) + ctx->surfaces = av_mallocz_array(ctx->nb_surfaces, sizeof(*ctx->surfaces)); + if (!ctx->surfaces) return AVERROR(ENOMEM); - ctx->timestamps = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t)); - if (!ctx->timestamps) + ctx->timestamp_list = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t)); + if (!ctx->timestamp_list) return AVERROR(ENOMEM); - ctx->unused_surface_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NVENCFrame*)); + + ctx->unused_surface_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*)); if (!ctx->unused_surface_queue) return AVERROR(ENOMEM); - ctx->pending = av_fifo_alloc(ctx->nb_surfaces * sizeof(*ctx->frames)); - if (!ctx->pending) + + ctx->output_surface_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*)); + if (!ctx->output_surface_queue) return AVERROR(ENOMEM); - ctx->ready = av_fifo_alloc(ctx->nb_surfaces * sizeof(*ctx->frames)); - if (!ctx->ready) + ctx->output_surface_ready_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*)); + if (!ctx->output_surface_ready_queue) return AVERROR(ENOMEM); + res = nvenc_push_context(avctx); + if (res < 0) + return res; + for (i = 0; i < ctx->nb_surfaces; i++) { - if ((ret = nvenc_alloc_surface(avctx, i)) < 0) - return ret; + if ((res = nvenc_alloc_surface(avctx, i)) < 0) + goto fail; } - return 0; -} +fail: + res2 = nvenc_pop_context(avctx); + if (res2 < 0) + return res2; -#define EXTRADATA_SIZE 512 + return res; +} -static int nvenc_setup_extradata(AVCodecContext *avctx) +static av_cold int nvenc_setup_extradata(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; - NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs; + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; + + NVENCSTATUS nv_status; + uint32_t outSize = 0; + char tmpHeader[256]; NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 }; - int ret; + payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER; - avctx->extradata = av_mallocz(EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE); - if (!avctx->extradata) - return AVERROR(ENOMEM); + payload.spsppsBuffer = tmpHeader; + payload.inBufferSize = sizeof(tmpHeader); + payload.outSPSPPSPayloadSize = &outSize; + + nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload); + if (nv_status != NV_ENC_SUCCESS) { + return nvenc_print_error(avctx, nv_status, "GetSequenceParams failed"); + } + + avctx->extradata_size = outSize; + avctx->extradata = av_mallocz(outSize + AV_INPUT_BUFFER_PADDING_SIZE); - payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER; - payload.spsppsBuffer = avctx->extradata; - payload.inBufferSize = EXTRADATA_SIZE; - payload.outSPSPPSPayloadSize = &avctx->extradata_size; + if (!avctx->extradata) { + return AVERROR(ENOMEM); + } - ret = nv->nvEncGetSequenceParams(ctx->nvenc_ctx, &payload); - if (ret != NV_ENC_SUCCESS) - return nvenc_print_error(avctx, ret, "Cannot get the extradata"); + memcpy(avctx->extradata, tmpHeader, outSize); return 0; } 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; + 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, res; /* the encoder has to be flushed before it can be closed */ - if (ctx->nvenc_ctx) { + if (ctx->nvencoder) { NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER, .encodePicFlags = NV_ENC_PIC_FLAG_EOS }; - nv->nvEncEncodePicture(ctx->nvenc_ctx, ¶ms); - } + res = nvenc_push_context(avctx); + if (res < 0) + return res; - av_fifo_free(ctx->timestamps); - av_fifo_free(ctx->pending); - av_fifo_free(ctx->ready); - av_fifo_free(ctx->unused_surface_queue); + p_nvenc->nvEncEncodePicture(ctx->nvencoder, ¶ms); + } - if (ctx->frames) { - for (i = 0; i < ctx->nb_surfaces; ++i) { - if (avctx->pix_fmt != AV_PIX_FMT_CUDA) { - nv->nvEncDestroyInputBuffer(ctx->nvenc_ctx, ctx->frames[i].in); - } else if (ctx->frames[i].in) { - nv->nvEncUnmapInputResource(ctx->nvenc_ctx, ctx->frames[i].in_map.mappedResource); - } + av_fifo_freep(&ctx->timestamp_list); + av_fifo_freep(&ctx->output_surface_ready_queue); + av_fifo_freep(&ctx->output_surface_queue); + av_fifo_freep(&ctx->unused_surface_queue); - av_frame_free(&ctx->frames[i].in_ref); - nv->nvEncDestroyBitstreamBuffer(ctx->nvenc_ctx, ctx->frames[i].out); + if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) { + for (i = 0; i < ctx->nb_registered_frames; i++) { + if (ctx->registered_frames[i].mapped) + p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[i].in_map.mappedResource); + if (ctx->registered_frames[i].regptr) + p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr); } + ctx->nb_registered_frames = 0; } - for (i = 0; i < ctx->nb_registered_frames; i++) { - if (ctx->registered_frames[i].regptr) - nv->nvEncUnregisterResource(ctx->nvenc_ctx, ctx->registered_frames[i].regptr); + + if (ctx->surfaces) { + for (i = 0; i < ctx->nb_surfaces; ++i) { + if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11) + p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[i].input_surface); + av_frame_free(&ctx->surfaces[i].in_ref); + p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->surfaces[i].output_surface); + } } - ctx->nb_registered_frames = 0; + av_freep(&ctx->surfaces); + ctx->nb_surfaces = 0; - av_freep(&ctx->frames); + if (ctx->nvencoder) { + p_nvenc->nvEncDestroyEncoder(ctx->nvencoder); - if (ctx->nvenc_ctx) - nv->nvEncDestroyEncoder(ctx->nvenc_ctx); + res = nvenc_pop_context(avctx); + if (res < 0) + return res; + } + ctx->nvencoder = NULL; if (ctx->cu_context_internal) - ctx->nvel.cu_ctx_destroy(ctx->cu_context_internal); - - if (ctx->nvel.nvenc) - dlclose(ctx->nvel.nvenc); + dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal); + ctx->cu_context = ctx->cu_context_internal = NULL; -#if !CONFIG_CUDA - if (ctx->nvel.cuda) - dlclose(ctx->nvel.cuda); +#if CONFIG_D3D11VA + if (ctx->d3d11_device) { + ID3D11Device_Release(ctx->d3d11_device); + ctx->d3d11_device = NULL; + } #endif + nvenc_free_functions(&dl_fn->nvenc_dl); + cuda_free_functions(&dl_fn->cuda_dl); + + dl_fn->nvenc_device_count = 0; + + av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n"); + return 0; } av_cold int ff_nvenc_encode_init(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; + NvencContext *ctx = avctx->priv_data; int ret; - if (avctx->pix_fmt == AV_PIX_FMT_CUDA) { + if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) { AVHWFramesContext *frames_ctx; if (!avctx->hw_frames_ctx) { av_log(avctx, AV_LOG_ERROR, @@ -1214,6 +1452,11 @@ av_cold int ff_nvenc_encode_init(AVCodecContext *avctx) return AVERROR(EINVAL); } frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; + if (frames_ctx->format != avctx->pix_fmt) { + av_log(avctx, AV_LOG_ERROR, + "hw_frames_ctx must match the GPU frame type\n"); + return AVERROR(EINVAL); + } ctx->data_pix_fmt = frames_ctx->sw_format; } else { ctx->data_pix_fmt = avctx->pix_fmt; @@ -1239,9 +1482,9 @@ av_cold int ff_nvenc_encode_init(AVCodecContext *avctx) return 0; } -static NVENCFrame *get_free_frame(NVENCContext *ctx) +static NvencSurface *get_free_frame(NvencContext *ctx) { - NVENCFrame *tmp_surf; + NvencSurface *tmp_surf; if (!(av_fifo_size(ctx->unused_surface_queue) > 0)) // queue empty @@ -1251,97 +1494,53 @@ static NVENCFrame *get_free_frame(NVENCContext *ctx) return tmp_surf; } -static int nvenc_copy_frame(NV_ENC_LOCK_INPUT_BUFFER *in, const AVFrame *frame) +static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface, + NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame) { - uint8_t *buf = in->bufferDataPtr; - int off = frame->height * in->pitch; + int dst_linesize[4] = { + lock_buffer_params->pitch, + lock_buffer_params->pitch, + lock_buffer_params->pitch, + lock_buffer_params->pitch + }; + uint8_t *dst_data[4]; + int ret; - 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; + if (frame->format == AV_PIX_FMT_YUV420P) + dst_linesize[1] = dst_linesize[2] >>= 1; - av_image_copy_plane(buf, in->pitch >> 1, - frame->data[2], frame->linesize[2], - frame->width >> 1, frame->height >> 1); + ret = av_image_fill_pointers(dst_data, frame->format, nv_surface->height, + lock_buffer_params->bufferDataPtr, dst_linesize); + if (ret < 0) + return ret; - buf += off >> 2; + if (frame->format == AV_PIX_FMT_YUV420P) + FFSWAP(uint8_t*, dst_data[1], dst_data[2]); - 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_P010: - av_image_copy_plane(buf, in->pitch, - frame->data[0], frame->linesize[0], - frame->width << 1, frame->height); - buf += off; - - av_image_copy_plane(buf, in->pitch, - frame->data[1], frame->linesize[1], - frame->width << 1, 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; - case AV_PIX_FMT_YUV444P16: - av_image_copy_plane(buf, in->pitch, - frame->data[0], frame->linesize[0], - frame->width << 1, frame->height); - buf += off; - - av_image_copy_plane(buf, in->pitch, - frame->data[1], frame->linesize[1], - frame->width << 1, frame->height); - buf += off; - - av_image_copy_plane(buf, in->pitch, - frame->data[2], frame->linesize[2], - frame->width << 1, frame->height); - break; - default: - return AVERROR_BUG; - } + av_image_copy(dst_data, dst_linesize, + (const uint8_t**)frame->data, frame->linesize, frame->format, + avctx->width, avctx->height); return 0; } static int nvenc_find_free_reg_resource(AVCodecContext *avctx) { - NVENCContext *ctx = avctx->priv_data; - NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs; + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; + NVENCSTATUS nv_status; + int i; if (ctx->nb_registered_frames == FF_ARRAY_ELEMS(ctx->registered_frames)) { for (i = 0; i < ctx->nb_registered_frames; i++) { if (!ctx->registered_frames[i].mapped) { if (ctx->registered_frames[i].regptr) { - nv->nvEncUnregisterResource(ctx->nvenc_ctx, - ctx->registered_frames[i].regptr); + nv_status = p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr); + if (nv_status != NV_ENC_SUCCESS) + return nvenc_print_error(avctx, nv_status, "Failed unregistering unused input resource"); + ctx->registered_frames[i].ptr = NULL; ctx->registered_frames[i].regptr = NULL; } return i; @@ -1357,14 +1556,18 @@ static int nvenc_find_free_reg_resource(AVCodecContext *avctx) static int nvenc_register_frame(AVCodecContext *avctx, const AVFrame *frame) { - NVENCContext *ctx = avctx->priv_data; - NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs; - AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; + + AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frame->hw_frames_ctx->data; NV_ENC_REGISTER_RESOURCE reg; int i, idx, ret; for (i = 0; i < ctx->nb_registered_frames; i++) { - if (ctx->registered_frames[i].ptr == (CUdeviceptr)frame->data[0]) + if (avctx->pix_fmt == AV_PIX_FMT_CUDA && ctx->registered_frames[i].ptr == frame->data[0]) + return i; + else if (avctx->pix_fmt == AV_PIX_FMT_D3D11 && ctx->registered_frames[i].ptr == frame->data[0] && ctx->registered_frames[i].ptr_index == (intptr_t)frame->data[1]) return i; } @@ -1373,120 +1576,142 @@ static int nvenc_register_frame(AVCodecContext *avctx, const AVFrame *frame) return idx; reg.version = NV_ENC_REGISTER_RESOURCE_VER; - reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR; reg.width = frames_ctx->width; reg.height = frames_ctx->height; - reg.bufferFormat = ctx->frames[0].format; reg.pitch = frame->linesize[0]; reg.resourceToRegister = frame->data[0]; - ret = nv->nvEncRegisterResource(ctx->nvenc_ctx, ®); + if (avctx->pix_fmt == AV_PIX_FMT_CUDA) { + reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR; + } + else if (avctx->pix_fmt == AV_PIX_FMT_D3D11) { + reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX; + reg.subResourceIndex = (intptr_t)frame->data[1]; + } + + reg.bufferFormat = nvenc_map_buffer_format(frames_ctx->sw_format); + if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) { + av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n", + av_get_pix_fmt_name(frames_ctx->sw_format)); + return AVERROR(EINVAL); + } + + ret = p_nvenc->nvEncRegisterResource(ctx->nvencoder, ®); if (ret != NV_ENC_SUCCESS) { nvenc_print_error(avctx, ret, "Error registering an input resource"); return AVERROR_UNKNOWN; } - ctx->registered_frames[idx].ptr = (CUdeviceptr)frame->data[0]; - ctx->registered_frames[idx].regptr = reg.registeredResource; + ctx->registered_frames[idx].ptr = frame->data[0]; + ctx->registered_frames[idx].ptr_index = reg.subResourceIndex; + ctx->registered_frames[idx].regptr = reg.registeredResource; return idx; } static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame, - NVENCFrame *nvenc_frame) + NvencSurface *nvenc_frame) { - NVENCContext *ctx = avctx->priv_data; - NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs; - int ret; - - if (avctx->pix_fmt == AV_PIX_FMT_CUDA) { - int reg_idx; - - ret = nvenc_register_frame(avctx, frame); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "Could not register an input CUDA frame\n"); - return ret; + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; + + int res; + NVENCSTATUS nv_status; + + if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) { + int reg_idx = nvenc_register_frame(avctx, frame); + if (reg_idx < 0) { + av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n"); + return reg_idx; } - reg_idx = ret; - - ret = av_frame_ref(nvenc_frame->in_ref, frame); - if (ret < 0) - return ret; - nvenc_frame->in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER; - nvenc_frame->in_map.registeredResource = ctx->registered_frames[reg_idx].regptr; - - ret = nv->nvEncMapInputResource(ctx->nvenc_ctx, &nvenc_frame->in_map); - if (ret != NV_ENC_SUCCESS) { - av_frame_unref(nvenc_frame->in_ref); - return nvenc_print_error(avctx, ret, "Error mapping an input resource"); + res = av_frame_ref(nvenc_frame->in_ref, frame); + if (res < 0) + return res; + + if (!ctx->registered_frames[reg_idx].mapped) { + ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER; + ctx->registered_frames[reg_idx].in_map.registeredResource = ctx->registered_frames[reg_idx].regptr; + nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &ctx->registered_frames[reg_idx].in_map); + if (nv_status != NV_ENC_SUCCESS) { + av_frame_unref(nvenc_frame->in_ref); + return nvenc_print_error(avctx, nv_status, "Error mapping an input resource"); + } } - ctx->registered_frames[reg_idx].mapped = 1; + ctx->registered_frames[reg_idx].mapped += 1; + nvenc_frame->reg_idx = reg_idx; - nvenc_frame->in = nvenc_frame->in_map.mappedResource; + nvenc_frame->input_surface = ctx->registered_frames[reg_idx].in_map.mappedResource; + nvenc_frame->format = ctx->registered_frames[reg_idx].in_map.mappedBufferFmt; + nvenc_frame->pitch = frame->linesize[0]; + + return 0; } else { - NV_ENC_LOCK_INPUT_BUFFER params = { 0 }; + NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 }; - params.version = NV_ENC_LOCK_INPUT_BUFFER_VER; - params.inputBuffer = nvenc_frame->in; + lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER; + lockBufferParams.inputBuffer = nvenc_frame->input_surface; - ret = nv->nvEncLockInputBuffer(ctx->nvenc_ctx, ¶ms); - if (ret != NV_ENC_SUCCESS) - return nvenc_print_error(avctx, ret, "Cannot lock the buffer"); + nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams); + if (nv_status != NV_ENC_SUCCESS) { + return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer"); + } - ret = nvenc_copy_frame(¶ms, frame); - if (ret < 0) { - nv->nvEncUnlockInputBuffer(ctx->nvenc_ctx, nvenc_frame->in); - return ret; + nvenc_frame->pitch = lockBufferParams.pitch; + res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame); + + nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, nvenc_frame->input_surface); + if (nv_status != NV_ENC_SUCCESS) { + return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!"); } - ret = nv->nvEncUnlockInputBuffer(ctx->nvenc_ctx, nvenc_frame->in); - if (ret != NV_ENC_SUCCESS) - return nvenc_print_error(avctx, ret, "Cannot unlock the buffer"); + return res; } - - return 0; } static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *params) { - NVENCContext *ctx = avctx->priv_data; + NvencContext *ctx = avctx->priv_data; switch (avctx->codec->id) { case AV_CODEC_ID_H264: params->codecPicParams.h264PicParams.sliceMode = - ctx->config.encodeCodecConfig.h264Config.sliceMode; + ctx->encode_config.encodeCodecConfig.h264Config.sliceMode; params->codecPicParams.h264PicParams.sliceModeData = - ctx->config.encodeCodecConfig.h264Config.sliceModeData; - break; + ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData; + break; case AV_CODEC_ID_HEVC: params->codecPicParams.hevcPicParams.sliceMode = - ctx->config.encodeCodecConfig.hevcConfig.sliceMode; + ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode; params->codecPicParams.hevcPicParams.sliceModeData = - ctx->config.encodeCodecConfig.hevcConfig.sliceModeData; + ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData; break; } } -static inline int nvenc_enqueue_timestamp(AVFifoBuffer *f, int64_t pts) +static inline void timestamp_queue_enqueue(AVFifoBuffer* queue, int64_t timestamp) { - return av_fifo_generic_write(f, &pts, sizeof(pts), NULL); + av_fifo_generic_write(queue, ×tamp, sizeof(timestamp), NULL); } -static inline int nvenc_dequeue_timestamp(AVFifoBuffer *f, int64_t *pts) +static inline int64_t timestamp_queue_dequeue(AVFifoBuffer* queue) { - return av_fifo_generic_read(f, pts, sizeof(*pts), NULL); + int64_t timestamp = AV_NOPTS_VALUE; + if (av_fifo_size(queue) > 0) + av_fifo_generic_read(queue, ×tamp, sizeof(timestamp), NULL); + + return timestamp; } static int nvenc_set_timestamp(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *params, AVPacket *pkt) { - NVENCContext *ctx = avctx->priv_data; + NvencContext *ctx = avctx->priv_data; - pkt->pts = params->outputTimeStamp; - pkt->duration = params->outputDuration; + pkt->pts = params->outputTimeStamp; /* generate the first dts by linearly extrapolating the * first two pts values to the past */ @@ -1508,80 +1733,147 @@ static int nvenc_set_timestamp(AVCodecContext *avctx, ctx->first_packet_output = 1; return 0; } - return nvenc_dequeue_timestamp(ctx->timestamps, &pkt->dts); + + pkt->dts = timestamp_queue_dequeue(ctx->timestamp_list); + + return 0; } -static int nvenc_get_output(AVCodecContext *avctx, AVPacket *pkt) +static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf) { - NVENCContext *ctx = avctx->priv_data; - NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs; - NV_ENC_LOCK_BITSTREAM params = { 0 }; - NVENCFrame *frame; - int ret; + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; - ret = av_fifo_generic_read(ctx->ready, &frame, sizeof(frame), NULL); - if (ret) - return ret; + uint32_t slice_mode_data; + uint32_t *slice_offsets = NULL; + NV_ENC_LOCK_BITSTREAM lock_params = { 0 }; + NVENCSTATUS nv_status; + int res = 0; - params.version = NV_ENC_LOCK_BITSTREAM_VER; - params.outputBitstream = frame->out; + enum AVPictureType pict_type; - ret = nv->nvEncLockBitstream(ctx->nvenc_ctx, ¶ms); - if (ret < 0) - return nvenc_print_error(avctx, ret, "Cannot lock the bitstream"); + 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, "Unknown codec name\n"); + res = AVERROR(EINVAL); + goto error; + } + slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets)); - ret = ff_alloc_packet(pkt, params.bitstreamSizeInBytes); - if (ret < 0) - return ret; + if (!slice_offsets) { + res = AVERROR(ENOMEM); + goto error; + } - memcpy(pkt->data, params.bitstreamBufferPtr, pkt->size); + lock_params.version = NV_ENC_LOCK_BITSTREAM_VER; - ret = nv->nvEncUnlockBitstream(ctx->nvenc_ctx, frame->out); - if (ret < 0) - return nvenc_print_error(avctx, ret, "Cannot unlock the bitstream"); + lock_params.doNotWait = 0; + lock_params.outputBitstream = tmpoutsurf->output_surface; + lock_params.sliceOffsets = slice_offsets; - if (avctx->pix_fmt == AV_PIX_FMT_CUDA) { - nv->nvEncUnmapInputResource(ctx->nvenc_ctx, frame->in_map.mappedResource); - av_frame_unref(frame->in_ref); - ctx->registered_frames[frame->reg_idx].mapped = 0; + nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params); + if (nv_status != NV_ENC_SUCCESS) { + res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer"); + goto error; + } - frame->in = NULL; + if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes,0)) { + p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface); + goto error; } - av_fifo_generic_write(ctx->unused_surface_queue, &frame, sizeof(frame), NULL); + memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes); - ret = nvenc_set_timestamp(avctx, ¶ms, pkt); - if (ret < 0) - return ret; + nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface); + if (nv_status != NV_ENC_SUCCESS) { + res = nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open"); + goto error; + } + + + if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) { + ctx->registered_frames[tmpoutsurf->reg_idx].mapped -= 1; + if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped == 0) { + nv_status = p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[tmpoutsurf->reg_idx].in_map.mappedResource); + if (nv_status != NV_ENC_SUCCESS) { + res = nvenc_print_error(avctx, nv_status, "Failed unmapping input resource"); + goto error; + } + nv_status = p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[tmpoutsurf->reg_idx].regptr); + if (nv_status != NV_ENC_SUCCESS) { + res = nvenc_print_error(avctx, nv_status, "Failed unregistering input resource"); + goto error; + } + ctx->registered_frames[tmpoutsurf->reg_idx].ptr = NULL; + ctx->registered_frames[tmpoutsurf->reg_idx].regptr = NULL; + } else if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped < 0) { + res = AVERROR_BUG; + goto error; + } + + av_frame_unref(tmpoutsurf->in_ref); + + tmpoutsurf->input_surface = NULL; + } - 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; + pict_type = AV_PICTURE_TYPE_I; break; case NV_ENC_PIC_TYPE_P: - avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P; + pict_type = AV_PICTURE_TYPE_P; break; case NV_ENC_PIC_TYPE_B: - avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B; + pict_type = AV_PICTURE_TYPE_B; break; case NV_ENC_PIC_TYPE_BI: - avctx->coded_frame->pict_type = AV_PICTURE_TYPE_BI; + 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; + } + +#if FF_API_CODED_FRAME +FF_DISABLE_DEPRECATION_WARNINGS + avctx->coded_frame->pict_type = pict_type; FF_ENABLE_DEPRECATION_WARNINGS #endif - } + + ff_side_data_set_encoder_stats(pkt, + (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type); + + res = nvenc_set_timestamp(avctx, &lock_params, pkt); + if (res < 0) + goto error2; + + av_free(slice_offsets); return 0; + +error: + timestamp_queue_dequeue(ctx->timestamp_list); + +error2: + av_free(slice_offsets); + + return res; } static int output_ready(AVCodecContext *avctx, int flush) { - NVENCContext *ctx = avctx->priv_data; + NvencContext *ctx = avctx->priv_data; int nb_ready, nb_pending; /* when B-frames are enabled, we wait for two initial timestamps to @@ -1590,96 +1882,171 @@ static int output_ready(AVCodecContext *avctx, int flush) (ctx->initial_pts[0] == AV_NOPTS_VALUE || ctx->initial_pts[1] == AV_NOPTS_VALUE)) return 0; - nb_ready = av_fifo_size(ctx->ready) / sizeof(NVENCFrame*); - nb_pending = av_fifo_size(ctx->pending) / sizeof(NVENCFrame*); + nb_ready = av_fifo_size(ctx->output_surface_ready_queue) / sizeof(NvencSurface*); + nb_pending = av_fifo_size(ctx->output_surface_queue) / sizeof(NvencSurface*); if (flush) return nb_ready > 0; return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth); } -int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, - const AVFrame *frame, int *got_packet) +int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) { - NVENCContext *ctx = avctx->priv_data; - NVENCLibraryContext *nvel = &ctx->nvel; - NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs; - NV_ENC_PIC_PARAMS params = { 0 }; - NVENCFrame *nvenc_frame = NULL; - CUcontext dummy; - int enc_ret, ret; + NVENCSTATUS nv_status; + NvencSurface *tmp_out_surf, *in_surf; + int res, res2; + + 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; - params.version = NV_ENC_PIC_PARAMS_VER; + if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder) + return AVERROR(EINVAL); + + if (ctx->encoder_flushing) + return AVERROR_EOF; if (frame) { - nvenc_frame = get_free_frame(ctx); - if (!nvenc_frame) { - av_log(avctx, AV_LOG_ERROR, "No free surfaces\n"); - return AVERROR_BUG; - } + in_surf = get_free_frame(ctx); + if (!in_surf) + return AVERROR(EAGAIN); - ret = nvenc_upload_frame(avctx, frame, nvenc_frame); - if (ret < 0) - return ret; + res = nvenc_push_context(avctx); + if (res < 0) + return res; - params.inputBuffer = nvenc_frame->in; - params.bufferFmt = nvenc_frame->format; - params.inputWidth = frame->width; - params.inputHeight = frame->height; - params.outputBitstream = nvenc_frame->out; - params.inputTimeStamp = frame->pts; + res = nvenc_upload_frame(avctx, frame, in_surf); + + res2 = nvenc_pop_context(avctx); + if (res2 < 0) + return res2; + + if (res) + return res; + + pic_params.inputBuffer = in_surf->input_surface; + pic_params.bufferFmt = in_surf->format; + pic_params.inputWidth = in_surf->width; + pic_params.inputHeight = in_surf->height; + pic_params.inputPitch = in_surf->pitch; + pic_params.outputBitstream = in_surf->output_surface; if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { if (frame->top_field_first) - params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM; + pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM; else - params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP; + 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, ¶ms); + if (ctx->forced_idr >= 0 && frame->pict_type == AV_PICTURE_TYPE_I) { + pic_params.encodePicFlags = + ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA; + } else { + pic_params.encodePicFlags = 0; + } - ret = nvenc_enqueue_timestamp(ctx->timestamps, frame->pts); - if (ret < 0) - return ret; + pic_params.inputTimeStamp = frame->pts; - if (ctx->initial_pts[0] == AV_NOPTS_VALUE) - ctx->initial_pts[0] = frame->pts; - else if (ctx->initial_pts[1] == AV_NOPTS_VALUE) - ctx->initial_pts[1] = frame->pts; + nvenc_codec_specific_pic_params(avctx, &pic_params); } else { - params.encodePicFlags = NV_ENC_PIC_FLAG_EOS; + pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS; + ctx->encoder_flushing = 1; } - nvel->cu_ctx_push_current(ctx->cu_context); - enc_ret = nv->nvEncEncodePicture(ctx->nvenc_ctx, ¶ms); - nvel->cu_ctx_pop_current(&dummy); + res = nvenc_push_context(avctx); + if (res < 0) + return res; - if (enc_ret != NV_ENC_SUCCESS && - enc_ret != NV_ENC_ERR_NEED_MORE_INPUT) - return nvenc_print_error(avctx, enc_ret, "Error encoding the frame"); + nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params); - if (nvenc_frame) { - ret = av_fifo_generic_write(ctx->pending, &nvenc_frame, sizeof(nvenc_frame), NULL); - if (ret < 0) - return ret; + res = nvenc_pop_context(avctx); + if (res < 0) + return res; + + if (nv_status != NV_ENC_SUCCESS && + nv_status != NV_ENC_ERR_NEED_MORE_INPUT) + return nvenc_print_error(avctx, nv_status, "EncodePicture failed!"); + + if (frame) { + av_fifo_generic_write(ctx->output_surface_queue, &in_surf, sizeof(in_surf), NULL); + timestamp_queue_enqueue(ctx->timestamp_list, frame->pts); + + if (ctx->initial_pts[0] == AV_NOPTS_VALUE) + ctx->initial_pts[0] = frame->pts; + else if (ctx->initial_pts[1] == AV_NOPTS_VALUE) + ctx->initial_pts[1] = frame->pts; } /* all the pending buffers are now ready for output */ - if (enc_ret == NV_ENC_SUCCESS) { - while (av_fifo_size(ctx->pending) > 0) { - av_fifo_generic_read(ctx->pending, &nvenc_frame, sizeof(nvenc_frame), NULL); - av_fifo_generic_write(ctx->ready, &nvenc_frame, sizeof(nvenc_frame), NULL); + if (nv_status == NV_ENC_SUCCESS) { + while (av_fifo_size(ctx->output_surface_queue) > 0) { + av_fifo_generic_read(ctx->output_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); + av_fifo_generic_write(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); } } - if (output_ready(avctx, !frame)) { - ret = nvenc_get_output(avctx, pkt); - if (ret < 0) - return ret; - *got_packet = 1; + return 0; +} + +int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) +{ + NvencSurface *tmp_out_surf; + int res, res2; + + NvencContext *ctx = avctx->priv_data; + + if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder) + return AVERROR(EINVAL); + + if (output_ready(avctx, ctx->encoder_flushing)) { + av_fifo_generic_read(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); + + res = nvenc_push_context(avctx); + if (res < 0) + return res; + + res = process_output_surface(avctx, pkt, tmp_out_surf); + + res2 = nvenc_pop_context(avctx); + if (res2 < 0) + return res2; + + if (res) + return res; + + av_fifo_generic_write(ctx->unused_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); + } else if (ctx->encoder_flushing) { + return AVERROR_EOF; } else { + return AVERROR(EAGAIN); + } + + return 0; +} + +int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, + const AVFrame *frame, int *got_packet) +{ + NvencContext *ctx = avctx->priv_data; + int res; + + if (!ctx->encoder_flushing) { + res = ff_nvenc_send_frame(avctx, frame); + if (res < 0) + return res; + } + + res = ff_nvenc_receive_packet(avctx, pkt); + if (res == AVERROR(EAGAIN) || res == AVERROR_EOF) { *got_packet = 0; + } else if (res < 0) { + return res; + } else { + *got_packet = 1; } return 0; |