From a2b8eaff37896d0b5e97a203d611ca000f2bcbee Mon Sep 17 00:00:00 2001 From: eisneinechse <42617957+eisneinechse@users.noreply.github.com> Date: Fri, 15 Feb 2019 10:11:45 -0800 Subject: [PATCH] Allow to use nvenc and nvdec in Windows for nVidia cards. nVidia card don't use the DX API like intel or AMD cards. If ffmpeg and the libraries are compiled with nvenc and nvdec support on WIndows this should(!) now work. --- src/FFmpegReader.cpp | 21 +++++++++++++++++++++ src/FFmpegWriter.cpp | 15 ++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index 7439db4d..1c80bb84 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -224,6 +224,23 @@ static enum AVPixelFormat get_hw_dec_format_d3(AVCodecContext *ctx, const enum A ZmqLogger::Instance()->AppendDebugMethod("FFmpegReader::ReadStream (Unable to decode this file using hardware decode.)", "", -1, "", -1, "", -1, "", -1, "", -1, "", -1); return AV_PIX_FMT_NONE; } + +static enum AVPixelFormat get_hw_dec_format_cu(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts) +{ + const enum AVPixelFormat *p; + + for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) { + switch (*p) { + case AV_PIX_FMT_CUDA: + hw_de_av_pix_fmt_global = AV_PIX_FMT_CUDA; + hw_de_av_device_type_global = AV_HWDEVICE_TYPE_CUDA; + return *p; + break; + } + } + ZmqLogger::Instance()->AppendDebugMethod("FFmpegReader::ReadStream (Unable to decode this file using hardware decode.)", "", -1, "", -1, "", -1, "", -1, "", -1, "", -1); + return AV_PIX_FMT_NONE; +} #endif #if defined(__APPLE__) @@ -378,6 +395,10 @@ void FFmpegReader::Open() hw_de_av_device_type = AV_HWDEVICE_TYPE_DXVA2; pCodecCtx->get_format = get_hw_dec_format_dx; break; + case 2: + hw_de_av_device_type = AV_HWDEVICE_TYPE_CUDA; + pCodecCtx->get_format = get_hw_dec_format_cu; + break; case 3: hw_de_av_device_type = AV_HWDEVICE_TYPE_DXVA2; pCodecCtx->get_format = get_hw_dec_format_dx; diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index 41285314..fb8040a8 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -203,9 +203,18 @@ void FFmpegWriter::SetVideoOptions(bool has_video, string codec, Fraction fps, i hw_en_av_device_type = AV_HWDEVICE_TYPE_DXVA2; } else { - new_codec = avcodec_find_encoder_by_name(codec.c_str()); - hw_en_on = 0; - hw_en_supported = 0; + if ( (strcmp(codec.c_str(),"h264_nvenc") == 0)) { + new_codec = avcodec_find_encoder_by_name(codec.c_str()); + hw_en_on = 1; + hw_en_supported = 1; + hw_en_av_pix_fmt = AV_PIX_FMT_CUDA; + hw_en_av_device_type = AV_HWDEVICE_TYPE_CUDA; + } + else { + new_codec = avcodec_find_encoder_by_name(codec.c_str()); + hw_en_on = 0; + hw_en_supported = 0; + } } #elif defined(__APPLE__) if ( (strcmp(codec.c_str(),"h264_qsv") == 0)) {