diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c771cb97f3b906fd2b90be23ca4826236b943ea2..93966ccf5d060acde6e8445b46d8ad940fbeb19a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,7 +6,7 @@ variables: GIT_SUBMODULE_STRATEGY: recursive - CMAKE_ARGS_WINDOWS: '-DCMAKE_GENERATOR_PLATFORM=x64 -DPORTAUDIO_DIR="D:/Build/portaudio" -DNVPIPE_DIR="D:/Build/NvPipe" -DEigen3_DIR="C:/Program Files (x86)/Eigen3/share/eigen3/cmake" -DOpenCV_DIR="D:/Build/opencv-4.1.1" -DCUDA_TOOLKIT_ROOT_DIR="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1" -DWITH_OPENVR=TRUE -DOPENVR_DIR="D:/Build/OpenVRSDK" -DWITH_CERES=FALSE' + CMAKE_ARGS_WINDOWS: '-DCMAKE_GENERATOR_PLATFORM=x64 -DPORTAUDIO_DIR="D:/Build/portaudio" -DEigen3_DIR="C:/Program Files (x86)/Eigen3/share/eigen3/cmake" -DOpenCV_DIR="D:/Build/opencv-4.1.1" -DCUDA_TOOLKIT_ROOT_DIR="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1" -DWITH_OPENVR=TRUE -DOPENVR_DIR="D:/Build/OpenVRSDK" -DWITH_CERES=FALSE' stages: - all diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cf826bff89e5cabc09c5f28dbeba09c3089e574..90be9db69602b1ba5c3bb3633e6e0d3400a31f12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,6 @@ include(GNUInstallDirs) include(CTest) enable_testing() -option(WITH_NVPIPE "Use NvPipe for compression if available" ON) option(WITH_OPTFLOW "Use NVIDIA Optical Flow if available" OFF) option(WITH_OPENVR "Build with OpenVR support" OFF) option(WITH_OPUS "Use Opus audio compression" ON) @@ -399,7 +398,7 @@ set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) # ==== NvPipe ================================================================== -add_subdirectory(lib/nvpipe) +#add_subdirectory(lib/nvpipe) #find_library( NVPIPE_LIBRARY NAMES NvPipe libNvPipe PATHS ${NVPIPE_DIR} PATH_SUFFIXES lib) #if (NVPIPE_LIBRARY) diff --git a/SDK/Python/ftl/codecs.py b/SDK/Python/ftl/codecs.py index dd14c27d802a27374d685785705f3b151945898a..306e9cd6ad1836de7e2082b6f53c8529af75e6cb 100644 --- a/SDK/Python/ftl/codecs.py +++ b/SDK/Python/ftl/codecs.py @@ -14,8 +14,8 @@ from enum import IntEnum ################################################################################ # components/codecs/include/ftl/codecs/packet.hpp -Packet = namedtuple("Packet", ["codec", "definition", "block_total", - "block_number", "flags", "data"]) +Packet = namedtuple("Packet", ["codec", "definition", "frame_count", + "bitrate", "flags", "data"]) StreamPacket = namedtuple("StreamPacket", ["timestamp", "frameset_id", "frame_number", "channel"]) @@ -33,7 +33,10 @@ class codec_t(IntEnum): PNG = 1 H264 = 2 HEVC = 3 - WAV = 4 + H264_LOSSLESS = 4 + HEVC_LOSSLESS = 5 + WAV = 32 + OPUS = 33 JSON = 100 CALIBRATION = 101 POSE = 102 @@ -127,10 +130,10 @@ class FTLDecoder: ################################################################################ def split_images(packet, im): - if packet.block_total == 1: + if packet.frame_count == 1: return im - n = packet.block_total + n = packet.frame_count height, width = definition_t[packet.definition] cols = im.shape[1] // width @@ -145,7 +148,7 @@ def split_images(packet, im): return imgs def decode_codec_opencv(packet): - if packet.block_total != 1 or packet.block_number != 0: + if packet.frame_count != 1: warn("Unsupported block format (todo)") # is this relevant? im = _int_to_float(cv.imdecode(np.frombuffer(packet.data, dtype=np.uint8), @@ -154,7 +157,7 @@ def decode_codec_opencv(packet): return split_images(packet, im) def decode_codec_opencv_float(packet): - if packet.block_total != 1 or packet.block_number != 0: + if packet.frame_count != 1: warn("Unsupported block format (todo)") # is this relevant? im = cv.imdecode(np.frombuffer(packet.data, dtype=np.uint8), diff --git a/SDK/Python/ftl/streamwriter.py b/SDK/Python/ftl/streamwriter.py index b4703ade2236a93b118e68b62c7bd6fe1c2cb654..fdf673d702d7f79d9730c2b665b6bbaee1597299 100644 --- a/SDK/Python/ftl/streamwriter.py +++ b/SDK/Python/ftl/streamwriter.py @@ -151,9 +151,9 @@ class FTLStreamWriter: elif data.dtype in [np.int8, np.uint8]: if nchans == 3: - ftl_dtype = _imageformat_t.RGB + ftl_dtype = _imageformat_t.BGR elif nchans == 4: - ftl_dtype = _imageformat_t.RGBA + ftl_dtype = _imageformat_t.BGRA else: raise ValueError("Unsupported number of channels: %i" % nchans) diff --git a/components/codecs/CMakeLists.txt b/components/codecs/CMakeLists.txt index 052bbe8f079d2a5d92b139dfb99f26480b808059..6e7b42f3cd5295d0e9c4acfa8a85385a13bb0302 100644 --- a/components/codecs/CMakeLists.txt +++ b/components/codecs/CMakeLists.txt @@ -13,7 +13,6 @@ target_include_directories(BaseCodec PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/Video_Codec_SDK_9.1.23/include ${CMAKE_CURRENT_SOURCE_DIR}/src/Video_Codec_SDK_9.1.23/Samples/NvCodec $<TARGET_PROPERTY:ftlcommon,INTERFACE_INCLUDE_DIRECTORIES> - $<TARGET_PROPERTY:NvPipe,INTERFACE_INCLUDE_DIRECTORIES> ) add_library(OpenCVCodec OBJECT @@ -30,21 +29,20 @@ $<TARGET_OBJECTS:BaseCodec> $<TARGET_OBJECTS:OpenCVCodec> ) -if (HAVE_NVPIPE) - add_library(NvPipeCodec OBJECT - src/nvpipe_encoder.cpp - src/nvpipe_decoder.cpp - src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvDecoder/NvDecoder.cpp - ) - target_include_directories(NvPipeCodec PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/include - ${CMAKE_CURRENT_SOURCE_DIR}/src/Video_Codec_SDK_9.1.23/include - ${CMAKE_CURRENT_SOURCE_DIR}/src/Video_Codec_SDK_9.1.23/Samples/NvCodec - $<TARGET_PROPERTY:ftlcommon,INTERFACE_INCLUDE_DIRECTORIES> - $<TARGET_PROPERTY:NvPipe,INTERFACE_INCLUDE_DIRECTORIES> - ) - list(APPEND CODECSRC $<TARGET_OBJECTS:NvPipeCodec>) -endif() +add_library(NvidiaCodec OBJECT + src/nvidia_encoder.cpp + src/nvidia_decoder.cpp + src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvDecoder/NvDecoder.cpp + src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoder.cpp + src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoderCuda.cpp +) +target_include_directories(NvidiaCodec PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR}/src/Video_Codec_SDK_9.1.23/include + ${CMAKE_CURRENT_SOURCE_DIR}/src/Video_Codec_SDK_9.1.23/Samples/NvCodec + $<TARGET_PROPERTY:ftlcommon,INTERFACE_INCLUDE_DIRECTORIES> +) +list(APPEND CODECSRC $<TARGET_OBJECTS:NvidiaCodec>) add_library(ftlcodecs ${CODECSRC}) @@ -62,7 +60,7 @@ target_include_directories(ftlcodecs PUBLIC PRIVATE src) #target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include) -target_link_libraries(ftlcodecs ftlcommon ${OpenCV_LIBS} ${CUDA_LIBRARIES} Eigen3::Eigen NvPipe nvcuvid) +target_link_libraries(ftlcodecs ftlcommon ${OpenCV_LIBS} ${CUDA_LIBRARIES} Eigen3::Eigen nvcuvid cuda) if (BUILD_TESTS) add_subdirectory(test) diff --git a/components/codecs/include/ftl/codecs/depth_convert_cuda.hpp b/components/codecs/include/ftl/codecs/depth_convert_cuda.hpp index 73417e11fb72940c8f129e932c55e5d5624fb21a..7fca125dbf5ec87bd9745dabbcf67801cc25ecb4 100644 --- a/components/codecs/include/ftl/codecs/depth_convert_cuda.hpp +++ b/components/codecs/include/ftl/codecs/depth_convert_cuda.hpp @@ -8,6 +8,8 @@ namespace cuda { void depth_to_vuya(const cv::cuda::PtrStepSz<float> &depth, const cv::cuda::PtrStepSz<uchar4> &rgba, float maxdepth, cv::cuda::Stream &stream); +void depth_to_nv12_10(const cv::cuda::PtrStepSz<float> &depth, ushort* luminance, ushort* chroma, int pitch, float maxdepth, cv::cuda::Stream &stream); + void vuya_to_depth(const cv::cuda::PtrStepSz<float> &depth, const cv::cuda::PtrStepSz<ushort4> &rgba, float maxdepth, cv::cuda::Stream &stream); void vuya_to_depth(const cv::cuda::PtrStepSz<float> &depth, const cv::cuda::PtrStepSz<ushort> &luminance, const cv::cuda::PtrStepSz<ushort> &chroma, float maxdepth, cv::cuda::Stream &stream); @@ -16,6 +18,8 @@ void smooth_y(const cv::cuda::PtrStepSz<ushort4> &rgba, cv::cuda::Stream &stream void nv12_to_float(const uint8_t* src, uint32_t srcPitch, float* dst, uint32_t dstPitch, uint32_t width, uint32_t height, cudaStream_t s); +void float_to_nv12_16bit(const float* src, uint32_t srcPitch, uchar* dst, uint32_t dstPitch, uint32_t width, uint32_t height, cudaStream_t s); + } } diff --git a/components/codecs/include/ftl/codecs/nvpipe_decoder.hpp b/components/codecs/include/ftl/codecs/nvidia_decoder.hpp similarity index 75% rename from components/codecs/include/ftl/codecs/nvpipe_decoder.hpp rename to components/codecs/include/ftl/codecs/nvidia_decoder.hpp index e663c8551304069ca4d023c064afa16629095320..2c3ad8efaa7be45eedd831eb6d1341c3963576a6 100644 --- a/components/codecs/include/ftl/codecs/nvpipe_decoder.hpp +++ b/components/codecs/include/ftl/codecs/nvidia_decoder.hpp @@ -1,20 +1,18 @@ -#ifndef _FTL_CODECS_NVPIPE_DECODER_HPP_ -#define _FTL_CODECS_NVPIPE_DECODER_HPP_ +#ifndef _FTL_CODECS_NVIDIA_DECODER_HPP_ +#define _FTL_CODECS_NVIDIA_DECODER_HPP_ #include <ftl/codecs/decoder.hpp> #include <ftl/threads.hpp> -//#include <NvPipe.h> - class NvDecoder; namespace ftl { namespace codecs { -class NvPipeDecoder : public ftl::codecs::Decoder { +class NvidiaDecoder : public ftl::codecs::Decoder { public: - NvPipeDecoder(); - ~NvPipeDecoder(); + NvidiaDecoder(); + ~NvidiaDecoder(); bool decode(const ftl::codecs::Packet &pkt, cv::cuda::GpuMat &out) override; @@ -42,4 +40,4 @@ class NvPipeDecoder : public ftl::codecs::Decoder { } } -#endif // _FTL_CODECS_NVPIPE_DECODER_HPP_ +#endif // _FTL_CODECS_NVIDIA_DECODER_HPP_ diff --git a/components/codecs/include/ftl/codecs/nvidia_encoder.hpp b/components/codecs/include/ftl/codecs/nvidia_encoder.hpp new file mode 100644 index 0000000000000000000000000000000000000000..db3f3d4dfa3855efd72cbd0b129d27728db33c5b --- /dev/null +++ b/components/codecs/include/ftl/codecs/nvidia_encoder.hpp @@ -0,0 +1,55 @@ +#ifndef _FTL_CODECS_NVIDIA_ENCODER_HPP_ +#define _FTL_CODECS_NVIDIA_ENCODER_HPP_ + +#include <ftl/codecs/encoder.hpp> + +class NvEncoderCuda; + +namespace ftl { +namespace codecs { + +class NvidiaEncoder : public ftl::codecs::Encoder { + public: + NvidiaEncoder(ftl::codecs::definition_t maxdef, + ftl::codecs::definition_t mindef); + ~NvidiaEncoder(); + + bool encode(const cv::cuda::GpuMat &in, ftl::codecs::Packet &pkt) override; + + void reset(); + + bool supports(ftl::codecs::codec_t codec) override; + + struct Parameters { + ftl::codecs::codec_t codec; + bool is_float; + uint32_t width; + uint32_t height; + uint8_t bitrate; + + inline uint32_t encodeWidth() const { return (is_float && !isLossy()) ? width*2 : width; } + inline uint32_t encodeHeight() const { return height; } + inline bool isLossy() const { return codec == ftl::codecs::codec_t::HEVC || codec == ftl::codecs::codec_t::H264; } + + inline bool operator==(const Parameters &p) const { + return codec == p.codec && is_float == p.is_float && width == p.width && + height == p.height && bitrate == p.bitrate; + } + }; + + private: + NvEncoderCuda *nvenc_; + ftl::codecs::codec_t codec_; + Parameters params_; + + bool was_reset_; + + bool _createEncoder(const cv::cuda::GpuMat &in, const ftl::codecs::Packet &pkt); + ftl::codecs::definition_t _verifiedDefinition(ftl::codecs::definition_t def, const cv::cuda::GpuMat &in); + uint64_t _encode(uint8_t* dst, uint64_t dstSize, bool forceIFrame); +}; + +} +} + +#endif // _FTL_CODECS_NVIDIA_ENCODER_HPP_ diff --git a/components/codecs/include/ftl/codecs/nvpipe_encoder.hpp b/components/codecs/include/ftl/codecs/nvpipe_encoder.hpp deleted file mode 100644 index 607d8d40f134ec044e5cdf2c335bf4050fa51373..0000000000000000000000000000000000000000 --- a/components/codecs/include/ftl/codecs/nvpipe_encoder.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef _FTL_CODECS_NVPIPE_ENCODER_HPP_ -#define _FTL_CODECS_NVPIPE_ENCODER_HPP_ - -#include <ftl/codecs/encoder.hpp> -#include <NvPipe.h> - -namespace ftl { -namespace codecs { - -class NvPipeEncoder : public ftl::codecs::Encoder { - public: - NvPipeEncoder(ftl::codecs::definition_t maxdef, - ftl::codecs::definition_t mindef); - ~NvPipeEncoder(); - - bool encode(const cv::cuda::GpuMat &in, ftl::codecs::Packet &pkt) override; - - //bool encode(const cv::cuda::GpuMat &in, std::vector<uint8_t> &out, bitrate_t bix, bool); - - void reset(); - - bool supports(ftl::codecs::codec_t codec) override; - - static constexpr int kFlagRGB = 0x00000001; - static constexpr int kFlagMappedDepth = 0x00000002; - - private: - NvPipe *nvenc_; - NvPipe_Codec codec_; - NvPipe_Format format_; - NvPipe_Compression compression_; - uint8_t last_bitrate_; - - bool was_reset_; - cv::cuda::GpuMat tmp_; - cv::cuda::GpuMat tmp2_; - - bool _encoderMatch(const ftl::codecs::Packet &pkt, format_t fmt); - bool _createEncoder(const ftl::codecs::Packet &pkt, format_t fmt); - ftl::codecs::definition_t _verifiedDefinition(ftl::codecs::definition_t def, const cv::cuda::GpuMat &in); -}; - -} -} - -#endif // _FTL_CODECS_NVPIPE_ENCODER_HPP_ diff --git a/components/codecs/include/ftl/codecs/packet.hpp b/components/codecs/include/ftl/codecs/packet.hpp index 546da9ac9e654757730284d269d6c56e305a3175..97bda6e826a52627e546f4537df6df30e12818d9 100644 --- a/components/codecs/include/ftl/codecs/packet.hpp +++ b/components/codecs/include/ftl/codecs/packet.hpp @@ -36,22 +36,18 @@ struct IndexHeader { */ struct Packet { ftl::codecs::codec_t codec; - ftl::codecs::definition_t definition; // Data resolution union { - [[deprecated]] uint8_t block_total; // v1-3 Packets expected per frame - uint8_t frame_count; // v4+ Frames included in this packet + [[deprecated]] ftl::codecs::definition_t definition; // Data resolution + uint8_t reserved=7; }; - union { - [[deprecated]] uint8_t block_number; // v1-3 This packets number within a frame + uint8_t frame_count; // v4+ Frames included in this packet uint8_t bitrate=0; // v4+ For multi-bitrate encoding, 0=highest - }; - uint8_t flags; // Codec dependent flags (eg. I-Frame or P-Frame) std::vector<uint8_t> data; - MSGPACK_DEFINE(codec, definition, frame_count, bitrate, flags, data); + MSGPACK_DEFINE(codec, reserved, frame_count, bitrate, flags, data); }; static constexpr unsigned int kStreamCap_Static = 0x01; diff --git a/components/codecs/src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoder.cpp b/components/codecs/src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoder.cpp index 0628f03261adebc3cf807675c54427555c7974b0..fd50ab3fc7bc6dbe2610a1cc02d11e32d0fc57fe 100644 --- a/components/codecs/src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoder.cpp +++ b/components/codecs/src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoder.cpp @@ -9,6 +9,10 @@ * */ +// Nick: Add dlfcn.h +#ifndef WIN32 +#include <dlfcn.h> +#endif #include "NvEncoder/NvEncoder.h" #ifndef _WIN32 @@ -55,6 +59,31 @@ NvEncoder::NvEncoder(NV_ENC_DEVICE_TYPE eDeviceType, void *pDevice, uint32_t nWi void NvEncoder::LoadNvEncApi() { + // Nick: Patched, as this is missing +#if defined(_WIN32) +#if defined(_WIN64) + HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI64.dll")); +#else + HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI.dll")); +#endif +#else + void *hModule = dlopen("libnvidia-encode.so.1", RTLD_LAZY); +#endif + + if (hModule == NULL) + { + NVENC_THROW_ERROR("NVENC library file is not found. Please ensure NV driver is installed", NV_ENC_ERR_NO_ENCODE_DEVICE); + } + + m_hModule = hModule; + + typedef NVENCSTATUS(NVENCAPI *NvEncodeAPIGetMaxSupportedVersion_Type)(uint32_t*); +#if defined(_WIN32) + NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = (NvEncodeAPIGetMaxSupportedVersion_Type)GetProcAddress(hModule, "NvEncodeAPIGetMaxSupportedVersion"); +#else + NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = (NvEncodeAPIGetMaxSupportedVersion_Type)dlsym(hModule, "NvEncodeAPIGetMaxSupportedVersion"); +#endif + // Nick: End patch uint32_t version = 0; uint32_t currentVersion = (NVENCAPI_MAJOR_VERSION << 4) | NVENCAPI_MINOR_VERSION; @@ -64,6 +93,18 @@ void NvEncoder::LoadNvEncApi() NVENC_THROW_ERROR("Current Driver Version does not support this NvEncodeAPI version, please upgrade driver", NV_ENC_ERR_INVALID_VERSION); } + // Nick: Patch + typedef NVENCSTATUS(NVENCAPI *NvEncodeAPICreateInstance_Type)(NV_ENCODE_API_FUNCTION_LIST*); +#if defined(_WIN32) + NvEncodeAPICreateInstance_Type NvEncodeAPICreateInstance = (NvEncodeAPICreateInstance_Type)GetProcAddress(hModule, "NvEncodeAPICreateInstance"); +#else + NvEncodeAPICreateInstance_Type NvEncodeAPICreateInstance = (NvEncodeAPICreateInstance_Type)dlsym(hModule, "NvEncodeAPICreateInstance"); +#endif + + if (!NvEncodeAPICreateInstance) + { + NVENC_THROW_ERROR("Cannot find NvEncodeAPICreateInstance() entry in NVENC library", NV_ENC_ERR_NO_ENCODE_DEVICE); + } m_nvenc = { NV_ENCODE_API_FUNCTION_LIST_VER }; NVENC_API_CALL(NvEncodeAPICreateInstance(&m_nvenc)); @@ -72,6 +113,17 @@ void NvEncoder::LoadNvEncApi() NvEncoder::~NvEncoder() { DestroyHWEncoder(); + + // Nick: Patch + if (m_hModule) + { +#if defined(_WIN32) + FreeLibrary((HMODULE)m_hModule); +#else + dlclose(m_hModule); +#endif + m_hModule = nullptr; + } } void NvEncoder::CreateDefaultEncoderParams(NV_ENC_INITIALIZE_PARAMS* pIntializeParams, GUID codecGuid, GUID presetGuid) diff --git a/components/codecs/src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoder.h b/components/codecs/src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoder.h index 7ce0af20626f4019c8e95efce8969731c9379d4f..a9e283321755e4f2036f2eebbb85113bc5d69c2d 100644 --- a/components/codecs/src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoder.h +++ b/components/codecs/src/Video_Codec_SDK_9.1.23/Samples/NvCodec/NvEncoder/NvEncoder.h @@ -433,4 +433,5 @@ private: std::vector<NV_ENC_OUTPUT_PTR> m_vMVDataOutputBuffer; uint32_t m_nMaxEncodeWidth = 0; uint32_t m_nMaxEncodeHeight = 0; + void* m_hModule = nullptr; }; diff --git a/components/codecs/src/decoder.cpp b/components/codecs/src/decoder.cpp index af02fd7f1cd6a301d0c33c1054f897cbed516e0a..dcaf17a78f6bb07eea871eb876ccb2e7ff96ed32 100644 --- a/components/codecs/src/decoder.cpp +++ b/components/codecs/src/decoder.cpp @@ -1,7 +1,7 @@ #include <ftl/codecs/decoder.hpp> #include <ftl/codecs/opencv_decoder.hpp> -#include <ftl/codecs/nvpipe_decoder.hpp> +#include <ftl/codecs/nvidia_decoder.hpp> using ftl::codecs::Decoder; using ftl::codecs::codec_t; @@ -21,7 +21,7 @@ Decoder *ftl::codecs::allocateDecoder(const ftl::codecs::Packet &pkt) { case codec_t::HEVC_LOSSLESS: case codec_t::H264_LOSSLESS: case codec_t::H264 : - case codec_t::HEVC : return new ftl::codecs::NvPipeDecoder; + case codec_t::HEVC : return new ftl::codecs::NvidiaDecoder; default : return nullptr; } } diff --git a/components/codecs/src/depth_convert.cu b/components/codecs/src/depth_convert.cu index ba09efc9f17dc2dfe26e68feededebfcf0fd3b95..8b6bef8b5bf78fa8691a4ec0710b036fd3313554 100644 --- a/components/codecs/src/depth_convert.cu +++ b/components/codecs/src/depth_convert.cu @@ -22,27 +22,31 @@ __device__ inline float clampC(float v, float t=255.0f) { * */ - // Assumes 8 bit output channels and 14bit depth - static constexpr float P = (2.0f * 256.0f) / 16384.0f; + // Assumes 8 bit output channels and 14bit depth + static constexpr float P = (2.0f * 256.0f) / 16384.0f; + + __device__ inline float3 depth2yuv(float depth, float maxdepth) { + float d = max(0.0f,depth); + if (d >= maxdepth) d = 0.0f; + float L = d / maxdepth; + const float p = P; + + float Ha1 = fmodf((L / (p/2.0f)), 2.0f); + float Ha = (Ha1 <= 1.0f) ? Ha1 : 2.0f - Ha1; + + float Hb1 = fmodf(((L - (p/4.0f)) / (p/2.0f)), 2.0f); + float Hb = (Hb1 <= 1.0f) ? Hb1 : 2.0f - Hb1; + + return {L, Ha, Hb}; + } __global__ void depth_to_vuya_kernel(cv::cuda::PtrStepSz<float> depth, cv::cuda::PtrStepSz<uchar4> rgba, float maxdepth) { const unsigned int x = blockIdx.x*blockDim.x + threadIdx.x; const unsigned int y = blockIdx.y*blockDim.y + threadIdx.y; if (x < depth.cols && y < depth.rows) { - //float d = max(0.0f,min(maxdepth,depth(y,x))); - float d = max(0.0f,depth(y,x)); - if (d >= maxdepth) d = 0.0f; - float L = d / maxdepth; - const float p = P; - - float Ha1 = fmodf((L / (p/2.0f)), 2.0f); - float Ha = (Ha1 <= 1.0f) ? Ha1 : 2.0f - Ha1; - - float Hb1 = fmodf(((L - (p/4.0f)) / (p/2.0f)), 2.0f); - float Hb = (Hb1 <= 1.0f) ? Hb1 : 2.0f - Hb1; - - rgba(y,x) = make_uchar4(Hb*255.0f,Ha*255.0f,L*255.0f, 0.0f); + float3 yuv = depth2yuv(depth(y,x), maxdepth); + rgba(y,x) = make_uchar4(yuv.z*255.0f,yuv.y*255.0f,yuv.x*255.0f, 0.0f); } } @@ -54,6 +58,44 @@ void ftl::cuda::depth_to_vuya(const cv::cuda::PtrStepSz<float> &depth, const cv: cudaSafeCall( cudaGetLastError() ); } +// Planar 10bit version + +__global__ void depth_to_nv12_10_kernel(cv::cuda::PtrStepSz<float> depth, ushort* luminance, ushort* chroma, int pitch, float maxdepth) { + const unsigned int x = (blockIdx.x*blockDim.x + threadIdx.x) * 2; + const unsigned int y = (blockIdx.y*blockDim.y + threadIdx.y) * 2; + + if (x < depth.cols && y < depth.rows) { + float3 yuv1 = depth2yuv(depth(y,x), maxdepth); + float3 yuv2 = depth2yuv(depth(y,x+1), maxdepth); + float3 yuv3 = depth2yuv(depth(y+1,x), maxdepth); + float3 yuv4 = depth2yuv(depth(y+1,x+1), maxdepth); + + // TODO: Something better than just average! + // Bad ones are discarded anyway... + float Ha = (yuv1.y+yuv2.y+yuv3.y+yuv4.y) / 4.0f * 255.0f; + float Hb = (yuv1.z+yuv2.z+yuv3.z+yuv4.z) / 4.0f * 255.0f; + + luminance[y*pitch+x] = ushort(yuv1.x*255.0f) << 8; + luminance[y*pitch+x+1] = ushort(yuv2.x*255.0f) << 8; + luminance[(y+1)*pitch+x] = ushort(yuv3.x*255.0f) << 8; + luminance[(y+1)*pitch+x+1] = ushort(yuv4.x*255.0f) << 8; + + chroma[(y/2)*pitch+x] = ushort(Hb) << 8; + chroma[(y/2)*pitch+x+1] = ushort(Ha) << 8; + } +} + +void ftl::cuda::depth_to_nv12_10(const cv::cuda::PtrStepSz<float> &depth, ushort* luminance, ushort* chroma, int pitch, float maxdepth, cv::cuda::Stream &stream) { + const dim3 gridSize((depth.cols/2 + T_PER_BLOCK - 1)/T_PER_BLOCK, (depth.rows/2 + T_PER_BLOCK - 1)/T_PER_BLOCK); + const dim3 blockSize(T_PER_BLOCK, T_PER_BLOCK); + + depth_to_nv12_10_kernel<<<gridSize, blockSize, 0, cv::cuda::StreamAccessor::getStream(stream)>>>(depth, luminance, chroma, pitch, maxdepth); + cudaSafeCall( cudaGetLastError() ); +} + + +// ============================================================================= + // Decoding /* @@ -361,7 +403,7 @@ static void SetMatYuv2Rgb(int iMatrix) { cudaMemcpyToSymbol(matYuv2Rgb, mat, sizeof(mat)); } -static void SetMatRgb2Yuv(int iMatrix) { +/*static void SetMatRgb2Yuv(int iMatrix) { float wr, wb; int black, white, max; GetConstants(iMatrix, wr, wb, black, white, max); @@ -376,7 +418,7 @@ static void SetMatRgb2Yuv(int iMatrix) { } } cudaMemcpyToSymbol(matRgb2Yuv, mat, sizeof(mat)); -} +}*/ template<class T> __device__ static T Clamp(T x, T lower, T upper) { @@ -474,3 +516,42 @@ void ftl::cuda::nv12_to_float(const uint8_t* src, uint32_t srcPitch, float* dst, ::nv12_to_float << <gridSize, blockSize, 0, s >> > (src, srcPitch, dst, dstPitch, width, height); } + +__global__ +void float_to_nv12_16bit(const float* __restrict__ src, uint32_t srcPitch, uint8_t* dst, uint32_t dstPitch, uint32_t width, uint32_t height) +{ + const uint32_t x = blockIdx.x * blockDim.x + threadIdx.x; + const uint32_t y = blockIdx.y * blockDim.y + threadIdx.y; + + if (x < width && y < height) + { + const uint32_t i = y * srcPitch + x; + const uint32_t j = y * dstPitch + x; + + float d = src[i]; + ushort ds = ushort(d*1000.0f); + + // Copy higher byte to left half of Y channel + dst[j] = ds & 0xFF; + + // Copy lower byte to right half of Y channel + dst[j + width] = ds >> 8; + + // Blank UV channel + if (y < height / 2) + { + uint8_t* UV = dst + dstPitch * (height + y); + UV[2 * x + 0] = 0; + UV[2 * x + 1] = 0; + } + } +} + +void ftl::cuda::float_to_nv12_16bit(const float* src, uint32_t srcPitch, uchar* dst, uint32_t dstPitch, uint32_t width, uint32_t height, cudaStream_t s) { + static const int THREADS_X = 16; + static const int THREADS_Y = 16; + dim3 gridSize(width / THREADS_X + 1, height / THREADS_Y + 1); + dim3 blockSize(THREADS_X, THREADS_Y); + + ::float_to_nv12_16bit << <gridSize, blockSize, 0, s >> > (src, srcPitch, dst, dstPitch, width, height); +} diff --git a/components/codecs/src/generate.cpp b/components/codecs/src/generate.cpp index 64dd2a042a42b6e5dbf272a01f4a922ed00168fe..f6a021fbc0fc893a1d19916dee657a2952939d7e 100644 --- a/components/codecs/src/generate.cpp +++ b/components/codecs/src/generate.cpp @@ -5,9 +5,7 @@ #include <ftl/config.h> #include <loguru.hpp> -#ifdef HAVE_NVPIPE -#include <ftl/codecs/nvpipe_encoder.hpp> -#endif +#include <ftl/codecs/nvidia_encoder.hpp> namespace ftl { namespace codecs { @@ -22,11 +20,8 @@ void fin_encoders() { } void init_encoders() { - #ifdef HAVE_NVPIPE - LOG(INFO) << "Adding NvPipe Encoders"; - encoders.push_back(new ftl::codecs::NvPipeEncoder(definition_t::UHD4k, definition_t::HD720)); - encoders.push_back(new ftl::codecs::NvPipeEncoder(definition_t::UHD4k, definition_t::HD720)); - #endif + encoders.push_back(new ftl::codecs::NvidiaEncoder(definition_t::UHD4k, definition_t::HD720)); + encoders.push_back(new ftl::codecs::NvidiaEncoder(definition_t::UHD4k, definition_t::HD720)); encoders.push_back(new ftl::codecs::OpenCVEncoder(definition_t::HD1080, definition_t::HD720)); encoders.push_back(new ftl::codecs::OpenCVEncoder(definition_t::HD1080, definition_t::HD720)); diff --git a/components/codecs/src/nvpipe_decoder.cpp b/components/codecs/src/nvidia_decoder.cpp similarity index 61% rename from components/codecs/src/nvpipe_decoder.cpp rename to components/codecs/src/nvidia_decoder.cpp index 3317a0c00c7e56c0c5e963c06b01b0018f8be22d..3bda2c25b253f402d900956a089e6ffab2b49875 100644 --- a/components/codecs/src/nvpipe_decoder.cpp +++ b/components/codecs/src/nvidia_decoder.cpp @@ -1,5 +1,5 @@ -#include <ftl/codecs/nvpipe_decoder.hpp> -#include <ftl/codecs/nvpipe_encoder.hpp> +#include <ftl/codecs/nvidia_decoder.hpp> +#include <ftl/codecs/nvidia_encoder.hpp> #include <ftl/exception.hpp> #include <loguru.hpp> @@ -18,16 +18,16 @@ #include "Video_Codec_SDK_9.1.23/Samples/NvCodec/NvDecoder/NvDecoder.h" #include "../Utils/ColorSpace.h" -using ftl::codecs::NvPipeDecoder; +using ftl::codecs::NvidiaDecoder; using ftl::codecs::codec_t; -NvPipeDecoder::NvPipeDecoder() { +NvidiaDecoder::NvidiaDecoder() { nv_decoder_ = nullptr; seen_iframe_ = false; } -NvPipeDecoder::~NvPipeDecoder() { +NvidiaDecoder::~NvidiaDecoder() { if (nv_decoder_ != nullptr) { delete nv_decoder_; } @@ -40,7 +40,7 @@ static T readValue(const unsigned char **data) { return *ptr; } -bool NvPipeDecoder::_checkIFrame(ftl::codecs::codec_t codec, const unsigned char *data, size_t size) { +bool NvidiaDecoder::_checkIFrame(ftl::codecs::codec_t codec, const unsigned char *data, size_t size) { if (!seen_iframe_) { if (codec == ftl::codecs::codec_t::HEVC || codec == ftl::codecs::codec_t::HEVC_LOSSLESS) { if (ftl::codecs::hevc::isIFrame(data, size)) seen_iframe_ = true; @@ -62,22 +62,19 @@ static inline std::string DecErrorCodeToString(CUresult code) return "Unknown error code"; } -bool NvPipeDecoder::_create(const ftl::codecs::Packet &pkt) { +bool NvidiaDecoder::_create(const ftl::codecs::Packet &pkt) { bool is_float_frame = pkt.flags & ftl::codecs::kFlagFloat; // Check existing decoder is valid first and remove if not - if (nv_decoder_ != nullptr && (last_definition_ != pkt.definition || - last_codec_ != pkt.codec || is_float_channel_ != is_float_frame || - width_ != last_width_ || height_ != last_height_)) { + if (nv_decoder_ != nullptr && (last_codec_ != pkt.codec || is_float_channel_ != is_float_frame)) { + //width_ != last_width_ || height_ != last_height_)) { delete nv_decoder_; nv_decoder_ = nullptr; } if (!nv_decoder_) { // Ensure we have a CUDA context - LOG(INFO) << "Getting cuda context..."; cudaSafeCall(cudaDeviceSynchronize()); - LOG(INFO) << "Have cuda context."; CUcontext cudaContext; cuCtxGetCurrent(&cudaContext); @@ -93,7 +90,7 @@ bool NvPipeDecoder::_create(const ftl::codecs::Packet &pkt) { return true; } -uint8_t* NvPipeDecoder::_decode(const uint8_t* src, uint64_t srcSize) { +uint8_t* NvidiaDecoder::_decode(const uint8_t* src, uint64_t srcSize) { int numFramesDecoded = 0; uint8_t **decodedFrames; int64_t *timeStamps; @@ -115,7 +112,7 @@ uint8_t* NvPipeDecoder::_decode(const uint8_t* src, uint64_t srcSize) { return decodedFrames[numFramesDecoded - 1]; } -bool NvPipeDecoder::decode(const ftl::codecs::Packet &pkt, cv::cuda::GpuMat &out) { +bool NvidiaDecoder::decode(const ftl::codecs::Packet &pkt, cv::cuda::GpuMat &out) { //cudaSetDevice(0); UNIQUE_LOCK(mutex_,lk); if (pkt.codec != codec_t::HEVC && pkt.codec != codec_t::H264 && pkt.codec != codec_t::HEVC_LOSSLESS && pkt.codec != codec_t::H264_LOSSLESS) return false; @@ -124,11 +121,6 @@ bool NvPipeDecoder::decode(const ftl::codecs::Packet &pkt, cv::cuda::GpuMat &out bool islossless = ((pkt.codec == ftl::codecs::codec_t::HEVC || pkt.codec == ftl::codecs::codec_t::H264) && is_float_frame && !(pkt.flags & 0x2)) || pkt.codec == ftl::codecs::codec_t::HEVC_LOSSLESS || pkt.codec == ftl::codecs::codec_t::H264_LOSSLESS; - /*if (is_float_frame && !islossless && out.type() != CV_16UC4) { - LOG(ERROR) << "Invalid buffer for lossy float frame"; - return false; - }*/ - if (is_float_frame && out.type() != CV_32F) { LOG(ERROR) << "Invalid buffer for float frame"; return false; @@ -139,51 +131,10 @@ bool NvPipeDecoder::decode(const ftl::codecs::Packet &pkt, cv::cuda::GpuMat &out return false; } - int width = ftl::codecs::getWidth(pkt.definition); - int height = ftl::codecs::getHeight(pkt.definition); - auto [tx,ty] = ftl::codecs::chooseTileConfig(pkt.frame_count); - - //int exp_height = (is_float_frame && !islossless) ? ty*height+((ty*height)/2) : ty*height; - - if (tx*width != out.cols || ty*height != out.rows) { - LOG(ERROR) << "Received frame whose size does not match buffer"; - return false; - } - - // Is the previous decoder still valid for current resolution and type? - //if (nv_decoder_ != nullptr && (last_definition_ != pkt.definition || last_codec_ != pkt.codec || is_float_channel_ != is_float_frame)) { - // NvPipe_Destroy(nv_decoder_); - // nv_decoder_ = nullptr; - //} - - // Create an appropriate NvDecoder instance - width_ = tx*width; - height_ = ty*height; - //if (islossless && is_float_frame) width_ *= 2; // 16bit = double width 8 bit _create(pkt); is_float_channel_ = is_float_frame; - last_definition_ = pkt.definition; last_codec_ = pkt.codec; - last_width_ = width_; - last_height_ = height_; - - // Build a decoder instance of the correct kind - /*if (nv_decoder_ == nullptr) { - nv_decoder_ = NvPipe_CreateDecoder( - (is_float_frame) ? (islossless) ? NVPIPE_UINT16 : NVPIPE_NV12_10bit : NVPIPE_RGBA32, - (pkt.codec == codec_t::HEVC || pkt.codec == ftl::codecs::codec_t::HEVC_LOSSLESS) ? NVPIPE_HEVC : NVPIPE_H264, - out.cols, - out.rows); - if (!nv_decoder_) { - //LOG(INFO) << "Bitrate=" << (int)bitrate << " width=" << ABRController::getColourWidth(bitrate); - LOG(FATAL) << "Could not create decoder: " << NvPipe_GetError(NULL); - } - - seen_iframe_ = false; - }*/ - - //tmp_.create(cv::Size(ftl::codecs::getWidth(pkt.definition),ftl::codecs::getHeight(pkt.definition)), (!is_float_frame) ? CV_8UC4 : (islossless) ? CV_16U : CV_16UC4); // Final checks for validity if (pkt.data.size() == 0) { // || !ftl::codecs::hevc::validNAL(pkt.data)) { @@ -212,27 +163,28 @@ bool NvPipeDecoder::decode(const ftl::codecs::Packet &pkt, cv::cuda::GpuMat &out } decodedPtr = _decode(ptr, size); - //rc = NvPipe_Decode(nv_decoder_, ptr, size, out.data, tx*width, ty*height, out.step); - //if (rc == 0) LOG(ERROR) << "NvPipe decode error: " << NvPipe_GetError(nv_decoder_); ptr += size; } - - //LOG(WARNING) << "Decode of multiple frames: " << count; } else { if (!_checkIFrame(pkt.codec, pkt.data.data(), pkt.data.size())) { LOG(WARNING) << "P-Frame without I-Frame in decoder: " << pkt.data.size(); return false; } decodedPtr = _decode(pkt.data.data(), pkt.data.size()); - //LOG(INFO) << "Decoded size = " << nv_decoder_->GetWidth() << "x" << nv_decoder_->GetHeight() << " - " << nv_decoder_->GetBPP(); - //rc = NvPipe_Decode(nv_decoder_, pkt.data.data(), pkt.data.size(), out.data, tx*width, ty*height, out.step); - //if (rc == 0) LOG(ERROR) << "NvPipe decode error: " << NvPipe_GetError(nv_decoder_); + } + + width_ = nv_decoder_->GetWidth(); + height_ = nv_decoder_->GetHeight(); + + if (out.cols != ((is_float_frame && islossless) ? width_/2 : width_) || out.rows != height_) { + LOG(ERROR) << "Decoded frame not same size as buffer"; + return false; } // OpenCV GpuMat for YCbCr 4:2:0 cv::cuda::GpuMat surface; if (is_float_frame && !islossless) surface = cv::cuda::GpuMat(height_+height_/2, width_, CV_16U, decodedPtr, width_*2); - else if (is_float_frame && islossless) surface = cv::cuda::GpuMat(height_+height_/2, width_*2, CV_8U, decodedPtr, width_*2); + else if (is_float_frame && islossless) surface = cv::cuda::GpuMat(height_+height_/2, width_, CV_8U, decodedPtr, width_); else surface = cv::cuda::GpuMat(height_+height_/2, width_, CV_8U, decodedPtr, width_); auto cvstream = cv::cuda::StreamAccessor::wrapStream(stream_); @@ -241,21 +193,16 @@ bool NvPipeDecoder::decode(const ftl::codecs::Packet &pkt, cv::cuda::GpuMat &out if (!islossless) { cv::cuda::GpuMat sroi = surface(cv::Rect(0,0,width_, height_)); cv::cuda::GpuMat csroi = surface(cv::Rect(0,height_,width_, height_/2)); - //cv::cuda::cvtColor(tmp_, tmp_, cv::COLOR_RGB2YUV, 4, stream_); - //LOG(INFO) << "Depth convert: " << out.cols << ", " << out.rows << ", " << out.type(); - //ftl::cuda::vuya_to_depth(out, tmp_, 16.0f, stream_); + ftl::cuda::vuya_to_depth(out, sroi, csroi, 16.0f, cvstream); } else { - //tmp_.convertTo(out, CV_32FC1, 1.0f/1000.0f, stream_); - ftl::cuda::nv12_to_float(decodedPtr, width_*2, (float*)out.data, out.step1(), width_, height_, stream_); + ftl::cuda::nv12_to_float(decodedPtr, width_, (float*)out.data, out.step1(), width_/2, height_, stream_); } } else { // Flag 0x1 means frame is in RGB so needs conversion to BGR if (pkt.flags & 0x1) { - //cv::cuda::cvtColor(surface, out, cv::COLOR_YUV2BGRA_NV12, 0, cvstream); Nv12ToColor32<BGRA32>(decodedPtr, width_, out.data, out.step1(), width_, height_, 0, stream_); } else { - //cv::cuda::cvtColor(surface, out, cv::COLOR_YUV2RGBA_NV12, 0, cvstream); Nv12ToColor32<RGBA32>(decodedPtr, width_, out.data, out.step1(), width_, height_, 0, stream_); } } @@ -265,6 +212,6 @@ bool NvPipeDecoder::decode(const ftl::codecs::Packet &pkt, cv::cuda::GpuMat &out return true; } -bool NvPipeDecoder::accepts(const ftl::codecs::Packet &pkt) { +bool NvidiaDecoder::accepts(const ftl::codecs::Packet &pkt) { return pkt.codec == codec_t::HEVC || pkt.codec == codec_t::H264 || pkt.codec == codec_t::H264_LOSSLESS || pkt.codec == codec_t::HEVC_LOSSLESS; } diff --git a/components/codecs/src/nvidia_encoder.cpp b/components/codecs/src/nvidia_encoder.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3aaad2e7ca0f6bc1325a8e93ec99ab0bcf5692d2 --- /dev/null +++ b/components/codecs/src/nvidia_encoder.cpp @@ -0,0 +1,366 @@ +#include <ftl/codecs/nvidia_encoder.hpp> +#include <loguru.hpp> +#include <ftl/timer.hpp> +#include <ftl/codecs/codecs.hpp> +#include <ftl/cuda_util.hpp> +#include <ftl/exception.hpp> + +#include <opencv2/core/cuda/common.hpp> +#include <opencv2/core/cuda_stream_accessor.hpp> +#include <opencv2/cudaimgproc.hpp> + +#include <ftl/codecs/depth_convert_cuda.hpp> + +#include "NvEncoder/NvEncoderCuda.h" + +using ftl::codecs::NvidiaEncoder; +using ftl::codecs::bitrate_t; +using ftl::codecs::codec_t; +using ftl::codecs::definition_t; +using ftl::codecs::format_t; +using ftl::codecs::Packet; +using ftl::codecs::kFlagFloat; +using ftl::codecs::kFlagFlipRGB; +using ftl::codecs::kFlagMappedDepth; + +static inline std::string EncErrorCodeToString(NVENCSTATUS code) +{ + std::vector<std::string> errors = { + "NV_ENC_SUCCESS", + "NV_ENC_ERR_NO_ENCODE_DEVICE", + "NV_ENC_ERR_UNSUPPORTED_DEVICE", + "NV_ENC_ERR_INVALID_ENCODERDEVICE", + "NV_ENC_ERR_INVALID_DEVICE", + "NV_ENC_ERR_DEVICE_NOT_EXIST", + "NV_ENC_ERR_INVALID_PTR", + "NV_ENC_ERR_INVALID_EVENT", + "NV_ENC_ERR_INVALID_PARAM", + "NV_ENC_ERR_INVALID_CALL", + "NV_ENC_ERR_OUT_OF_MEMORY", + "NV_ENC_ERR_ENCODER_NOT_INITIALIZED", + "NV_ENC_ERR_UNSUPPORTED_PARAM", + "NV_ENC_ERR_LOCK_BUSY", + "NV_ENC_ERR_NOT_ENOUGH_BUFFER", + "NV_ENC_ERR_INVALID_VERSION", + "NV_ENC_ERR_MAP_FAILED", + "NV_ENC_ERR_NEED_MORE_INPUT", + "NV_ENC_ERR_ENCODER_BUSY", + "NV_ENC_ERR_EVENT_NOT_REGISTERD", + "NV_ENC_ERR_GENERIC", + "NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY", + "NV_ENC_ERR_UNIMPLEMENTED", + "NV_ENC_ERR_RESOURCE_REGISTER_FAILED", + "NV_ENC_ERR_RESOURCE_NOT_REGISTERED", + "NV_ENC_ERR_RESOURCE_NOT_MAPPED" + }; + + if (code >= 0 && code < errors.size()) + return errors[code]; + + return "Unknown error code"; +} + +NvidiaEncoder::NvidiaEncoder(definition_t maxdef, + definition_t mindef) : Encoder(maxdef, mindef, ftl::codecs::device_t::Hardware) { + nvenc_ = nullptr; + was_reset_ = false; +} + +NvidiaEncoder::~NvidiaEncoder() { + if (nvenc_) { + std::vector<std::vector<uint8_t>> tmp; + nvenc_->EndEncode(tmp); + nvenc_->DestroyEncoder(); + delete nvenc_; + nvenc_ = nullptr; + } +} + +void NvidiaEncoder::reset() { + was_reset_ = true; +} + +bool NvidiaEncoder::supports(ftl::codecs::codec_t codec) { + switch (codec) { + case codec_t::H264_LOSSLESS: + case codec_t::HEVC_LOSSLESS: + case codec_t::H264: + case codec_t::HEVC: return true; + default: return false; + } +} + +/* + * Create encoder params structure from packet and surface. Changes to these + * require a recreation of the encoder. + */ +static ftl::codecs::NvidiaEncoder::Parameters generateParams(const cv::cuda::GpuMat &in, const ftl::codecs::Packet &pkt) { + ftl::codecs::NvidiaEncoder::Parameters params; + params.bitrate = pkt.bitrate; + params.codec = pkt.codec; + params.width = in.cols; + params.height = in.rows; + params.is_float = in.type() == CV_32F; + return params; +} + +static uint64_t calculateBitrate(int64_t pixels, float ratescale) { + /*float bitrate = 1.0f; // Megabits + switch (def) { + case definition_t::UHD4k : bitrate = 40.0f; break; + case definition_t::HTC_VIVE : bitrate = 32.0f; break; + case definition_t::HD1080 : bitrate = 12.0f; break; + case definition_t::HD720 : bitrate = 8.0f; break; + case definition_t::SD576 : + case definition_t::SD480 : bitrate = 4.0f; break; + case definition_t::LD360 : bitrate = 2.0f; break; + default : bitrate = 16.0f; + }*/ + + float bitrate = 8.0f * float(pixels); + + //bitrate *= 1000.0f*1000.0f; + float minrate = 0.05f * bitrate; + return uint64_t((bitrate - minrate)*ratescale + minrate); +} + +/* + * Check that codec configuration and surface data are all valid. + */ +static bool validate(const cv::cuda::GpuMat &in, ftl::codecs::Packet &pkt) { + if (in.type() == CV_32F) pkt.flags |= kFlagFloat; + else pkt.flags |= kFlagFlipRGB; + + // Remove unwanted flags + if (in.type() == CV_32F && (pkt.flags & kFlagFlipRGB)) pkt.flags &= ~kFlagFlipRGB; + if (in.type() == CV_8UC4 && (pkt.flags & kFlagFloat)) pkt.flags &= ~kFlagFloat; + if (pkt.codec == codec_t::HEVC_LOSSLESS && (pkt.flags & kFlagMappedDepth)) pkt.flags &= ~kFlagMappedDepth; + + if (pkt.codec == codec_t::Any) pkt.codec = codec_t::HEVC; + + // Correct for mising flag + if (pkt.codec == codec_t::HEVC && in.type() == CV_32F) { + pkt.flags |= ftl::codecs::kFlagMappedDepth; + } + + if (pkt.codec == codec_t::H264 && in.type() == CV_32F) { + LOG(ERROR) << "Lossy compression not supported with H264 currently"; + return false; + } + + if (pkt.frame_count == 0) { + //pkt.definition = definition_t::Invalid; + return false; + } + + /*auto [tx,ty] = ftl::codecs::chooseTileConfig(pkt.frame_count); + pkt.definition = (pkt.definition == definition_t::Any) ? ftl::codecs::findDefinition(in.cols/tx, in.rows/ty) : pkt.definition; + if (pkt.definition == definition_t::Invalid || pkt.definition == definition_t::Any) { + LOG(ERROR) << "Could not find appropriate definition"; + return false; + }*/ + + auto width = in.cols; //ftl::codecs::getWidth(pkt.definition)*tx; + auto height = in.rows; //ftl::codecs::getHeight(pkt.definition)*ty; + + if (in.empty()) { + LOG(WARNING) << "No data"; + return false; + } + + if (in.type() != CV_32F && in.type() != CV_8UC4) { + LOG(ERROR) << "Input type does not match given format"; + pkt.flags = 0; + return false; + } + + /*if (width != in.cols || height != in.rows) { + // TODO: Resize if lower definition requested... + LOG(ERROR) << "Input size does not match expected: " << in.cols << " != " << width; + pkt.definition = definition_t::Invalid; + return false; + }*/ + + return true; +} + +bool NvidiaEncoder::encode(const cv::cuda::GpuMat &in, ftl::codecs::Packet &pkt) { + //cudaSetDevice(0); + + if (pkt.codec != codec_t::Any && !supports(pkt.codec)) { + pkt.codec = codec_t::Invalid; + return false; + } + + if (!validate(in, pkt)) return false; + if (!_createEncoder(in, pkt)) return false; + + const NvEncInputFrame* f = nvenc_->GetNextInputFrame(); + + if (!params_.is_float) { + cv::cuda::GpuMat surface(nvenc_->GetEncodeHeight(), nvenc_->GetEncodeWidth(), CV_8UC4, f->inputPtr, f->pitch); + cv::cuda::cvtColor(in, surface, cv::COLOR_BGRA2RGBA, 0, stream_); + } else if (params_.isLossy()) { + ftl::cuda::depth_to_nv12_10(in, (ushort*)f->inputPtr, (ushort*)(((uchar*)f->inputPtr)+(nvenc_->GetEncodeHeight()*f->pitch)), f->pitch/2, 16.0f, stream_); + } else { + ftl::cuda::float_to_nv12_16bit((float*)in.data, in.step1(), (uchar*)f->inputPtr, f->pitch, nvenc_->GetEncodeWidth()/2, nvenc_->GetEncodeHeight(), cv::cuda::StreamAccessor::getStream(stream_)); + } + + // TODO: Use page locked memory? + pkt.data.resize(ftl::codecs::kVideoBufferSize); + + // Make sure conversions complete... + stream_.waitForCompletion(); + + uint64_t cs = _encode(pkt.data.data(), pkt.data.size(), was_reset_); + pkt.data.resize(cs); + was_reset_ = false; + + if (cs == 0 || cs >= ftl::codecs::kVideoBufferSize) { + LOG(ERROR) << "Could not encode video frame"; + return false; + } else { + return true; + } +} + +bool NvidiaEncoder::_createEncoder(const cv::cuda::GpuMat &in, const ftl::codecs::Packet &pkt) { + Parameters params = generateParams(in, pkt); + if (nvenc_ && (params == params_)) return true; + + uint64_t bitrate = calculateBitrate(in.cols*in.rows, float(pkt.bitrate)/255.0f) * pkt.frame_count; + LOG(INFO) << "Calculated bitrate " << ((params.is_float) ? "(float)" : "(rgb)") << ": " << bitrate; + + params_ = params; + + const int fps = 1000/ftl::timer::getInterval(); + + bool ish264 = pkt.codec == codec_t::H264 || pkt.codec == codec_t::H264_LOSSLESS; + bool ishevc = !ish264; + + // Ensure we have a CUDA context + cudaSafeCall(cudaDeviceSynchronize()); + CUcontext cudaContext; + cuCtxGetCurrent(&cudaContext); + + if (nvenc_) { + LOG(INFO) << "Destroying old NVENC encoder"; + std::vector<std::vector<uint8_t>> tmp; + nvenc_->EndEncode(tmp); + nvenc_->DestroyEncoder(); + delete nvenc_; + nvenc_ = nullptr; + } + + // Create encoder + try + { + NV_ENC_BUFFER_FORMAT bufferFormat; + if (!params.is_float) bufferFormat = NV_ENC_BUFFER_FORMAT_ABGR; + else if (!params.isLossy()) bufferFormat = NV_ENC_BUFFER_FORMAT_NV12; + else bufferFormat = NV_ENC_BUFFER_FORMAT_YUV420_10BIT; + + nvenc_ = new NvEncoderCuda(cudaContext, params_.encodeWidth(), params_.encodeHeight(), bufferFormat, 0); + + NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER }; + NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER }; + initializeParams.encodeConfig = &encodeConfig; + + GUID codecGUID = (ishevc) ? NV_ENC_CODEC_HEVC_GUID : NV_ENC_CODEC_H264_GUID; + + GUID presetGUID = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID; + if (!params.isLossy()) + presetGUID = NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID; // NV_ENC_PRESET_LOSSLESS_HP_GUID + + nvenc_->CreateDefaultEncoderParams(&initializeParams, codecGUID, presetGUID); + + initializeParams.encodeWidth = params.encodeWidth(); + initializeParams.encodeHeight = params.encodeHeight(); + initializeParams.frameRateNum = fps; + initializeParams.frameRateDen = 1; + initializeParams.enablePTD = 1; + + encodeConfig.gopLength = NVENC_INFINITE_GOPLENGTH; // No B-frames + encodeConfig.frameIntervalP = 1; + + if (ish264) + encodeConfig.encodeCodecConfig.h264Config.idrPeriod = NVENC_INFINITE_GOPLENGTH; + else { + encodeConfig.encodeCodecConfig.hevcConfig.idrPeriod = NVENC_INFINITE_GOPLENGTH; + + if (params.is_float && params.isLossy()) { + encodeConfig.encodeCodecConfig.hevcConfig.pixelBitDepthMinus8 = 2; // For 10-bit colour + } + + //if (this->compression == NVPIPE_LOSSY_10BIT_444 || this->compression == NVPIPE_LOSSY_8BIT_444) { + // encodeConfig.encodeCodecConfig.hevcConfig.chromaFormatIDC = 3; // For Yuv444 (1 for 420) + //} + } + + if (params.isLossy()) + { + encodeConfig.rcParams.averageBitRate = bitrate; + encodeConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ; + encodeConfig.rcParams.vbvBufferSize = encodeConfig.rcParams.averageBitRate * initializeParams.frameRateDen / initializeParams.frameRateNum; // bitrate / framerate = one frame + encodeConfig.rcParams.maxBitRate = encodeConfig.rcParams.averageBitRate; + encodeConfig.rcParams.vbvInitialDelay = encodeConfig.rcParams.vbvBufferSize; + } + + nvenc_->CreateEncoder(&initializeParams); + } + catch (NVENCException& e) + { + throw FTL_Error("Failed to create encoder (" << e.getErrorString() << ", error " + std::to_string(e.getErrorCode()) << " = " + EncErrorCodeToString(e.getErrorCode()) << ")"); + } + + if (!nvenc_) { + //LOG(ERROR) << "Could not create video encoder"; + return false; + } else { + LOG(INFO) << "NVENC encoder created"; + + //nvenc_->SetIOCudaStreams(cv::cuda::StreamAccessor::getStream(stream_), cv::cuda::StreamAccessor::getStream(stream_)); + + return true; + } +} + +uint64_t NvidiaEncoder::_encode(uint8_t* dst, uint64_t dstSize, bool forceIFrame) { + std::vector<std::vector<uint8_t>> packets; + + try + { + if (forceIFrame) + { + NV_ENC_PIC_PARAMS params = {}; + params.encodePicFlags = NV_ENC_PIC_FLAG_FORCEIDR | NV_ENC_PIC_FLAG_OUTPUT_SPSPPS; + + nvenc_->EncodeFrame(packets, ¶ms); + } + else + { + nvenc_->EncodeFrame(packets); + } + } + catch (NVENCException& e) + { + throw FTL_Error("Encode failed (" << e.getErrorString() << ", error " << std::to_string(e.getErrorCode()) << " = " << EncErrorCodeToString(e.getErrorCode()) << ")"); + } + + // Copy output + uint64_t size = 0; + for (auto& p : packets) + { + if (size + p.size() <= dstSize) + { + memcpy(dst + size, p.data(), p.size()); + size += p.size(); + } + else + { + throw FTL_Error("Encode output buffer overflow"); + } + } + + return size; +} diff --git a/components/codecs/src/nvpipe_encoder.cpp b/components/codecs/src/nvpipe_encoder.cpp deleted file mode 100644 index c9f662183cd1d007914e7ada1baeb5c913aef763..0000000000000000000000000000000000000000 --- a/components/codecs/src/nvpipe_encoder.cpp +++ /dev/null @@ -1,300 +0,0 @@ -#include <ftl/codecs/nvpipe_encoder.hpp> -#include <loguru.hpp> -#include <ftl/timer.hpp> -#include <ftl/codecs/codecs.hpp> -#include <ftl/cuda_util.hpp> - -#include <opencv2/core/cuda/common.hpp> - -#include <ftl/codecs/depth_convert_cuda.hpp> - -using ftl::codecs::NvPipeEncoder; -using ftl::codecs::bitrate_t; -using ftl::codecs::codec_t; -using ftl::codecs::definition_t; -using ftl::codecs::format_t; -using ftl::codecs::Packet; - -NvPipeEncoder::NvPipeEncoder(definition_t maxdef, - definition_t mindef) : Encoder(maxdef, mindef, ftl::codecs::device_t::Hardware) { - nvenc_ = nullptr; - was_reset_ = false; -} - -NvPipeEncoder::~NvPipeEncoder() { - if (nvenc_) NvPipe_Destroy(nvenc_); -} - -void NvPipeEncoder::reset() { - was_reset_ = true; -} - -bool NvPipeEncoder::supports(ftl::codecs::codec_t codec) { - switch (codec) { - case codec_t::H264_LOSSLESS: - case codec_t::HEVC_LOSSLESS: - case codec_t::H264: - case codec_t::HEVC: return true; - default: return false; - } -} - -/* Check preset resolution is not better than actual resolution. */ -/*definition_t NvPipeEncoder::_verifiedDefinition(definition_t def, const cv::cuda::GpuMat &in) { - int height = ftl::codecs::getHeight(def); - - while (height > in.rows) { - def = static_cast<definition_t>(int(def)+1); - height = ftl::codecs::getHeight(def); - } - - return def; -}*/ - -static bool isLossy(codec_t c) { - return !(c == codec_t::HEVC_LOSSLESS || c == codec_t::H264_LOSSLESS); -} - -static bool sanityFormat(int type, ftl::codecs::format_t fmt) { - switch(fmt) { - case format_t::BGRA8 : - case format_t::RGBA8 : return type == CV_8UC4; - case format_t::VUYA16 : return type == CV_8UC4; - case format_t::F32 : return type == CV_32F; - case format_t::U16 : return type == CV_16U; - } - return false; -} - -static ftl::codecs::format_t formatFromPacket(const ftl::codecs::Packet &pkt) { - if (pkt.flags & ftl::codecs::kFlagFloat) { - return (pkt.flags & ftl::codecs::kFlagMappedDepth) ? format_t::VUYA16 : format_t::U16; - } else { - return (pkt.flags & ftl::codecs::kFlagFlipRGB) ? format_t::BGRA8 : format_t::RGBA8; - } -} - -static uint64_t calculateBitrate(definition_t def, float ratescale) { - float bitrate = 1.0f; // Megabits - switch (def) { - case definition_t::UHD4k : bitrate = 40.0f; break; - case definition_t::HTC_VIVE : bitrate = 32.0f; break; - case definition_t::HD1080 : bitrate = 12.0f; break; - case definition_t::HD720 : bitrate = 8.0f; break; - case definition_t::SD576 : - case definition_t::SD480 : bitrate = 4.0f; break; - case definition_t::LD360 : bitrate = 2.0f; break; - default : bitrate = 16.0f; - } - - bitrate *= 1000.0f*1000.0f; - float minrate = 0.05f * bitrate; - return uint64_t((bitrate - minrate)*ratescale + minrate); -} - -bool NvPipeEncoder::encode(const cv::cuda::GpuMat &in, ftl::codecs::Packet &pkt) { - //cudaSetDevice(0); - - if (pkt.codec != codec_t::Any && !supports(pkt.codec)) { - pkt.codec = codec_t::Invalid; - return false; - } - - // Correct for mising flag - if (pkt.codec == codec_t::HEVC && (pkt.flags & ftl::codecs::kFlagFloat) && in.type() == CV_8UC4) { - pkt.flags |= ftl::codecs::kFlagMappedDepth; - } - - ftl::codecs::format_t fmt = formatFromPacket(pkt); - - if (pkt.frame_count == 0) { - pkt.definition = definition_t::Invalid; - return false; - } - - //bool is_stereo = pkt.flags & ftl::codecs::kFlagStereo; - - auto [tx,ty] = ftl::codecs::chooseTileConfig(pkt.frame_count); - pkt.definition = (pkt.definition == definition_t::Any) ? ftl::codecs::findDefinition(in.cols/tx, in.rows/ty) : pkt.definition; - if (pkt.definition == definition_t::Invalid || pkt.definition == definition_t::Any) { - LOG(ERROR) << "Could not find appropriate definition"; - return false; - } - - auto width = ftl::codecs::getWidth(pkt.definition); - auto height = ftl::codecs::getHeight(pkt.definition); - - if (in.empty()) { - LOG(WARNING) << "No data"; - return false; - } - - if (!sanityFormat(in.type(), fmt)) { - LOG(ERROR) << "Input type does not match given format"; - pkt.flags = 0; - return false; - } - - if (tx*width != in.cols || ty*height != in.rows) { - // TODO: Resize if lower definition requested... - LOG(ERROR) << "Input size does not match expected: " << in.cols << " != " << tx*width; - pkt.definition = definition_t::Invalid; - return false; - } - - cv::cuda::GpuMat tmp; - /*if (width != in.cols || height != in.rows) { - LOG(WARNING) << "Mismatch resolution with encoding resolution"; - if (in.type() == CV_32F) { - cv::cuda::resize(in, tmp_, cv::Size(width,height), 0.0, 0.0, cv::INTER_NEAREST, stream_); - } else { - cv::cuda::resize(in, tmp_, cv::Size(width,height), 0.0, 0.0, cv::INTER_LINEAR, stream_); - } - tmp = tmp_; - } else {*/ - tmp = in; - //} - - //LOG(INFO) << "Definition: " << ftl::codecs::getWidth(pkt.definition) << "x" << ftl::codecs::getHeight(pkt.definition); - - if (in.empty()) { - LOG(ERROR) << "Missing data for Nvidia encoder"; - return false; - } - - if (pkt.codec == codec_t::Any) - pkt.codec = ((pkt.flags & ftl::codecs::kFlagFloat) && !(pkt.flags & ftl::codecs::kFlagMappedDepth)) ? codec_t::HEVC_LOSSLESS : codec_t::HEVC; - - if (!_createEncoder(pkt, fmt)) return false; - - // Doesn't seem to work - /*if (isLossy(pkt.codec) && pkt.bitrate != last_bitrate_) { - uint64_t bitrate = calculateBitrate(pkt.definition, float(pkt.bitrate)/255.0f) * pkt.frame_count; - const int fps = 1000/ftl::timer::getInterval(); - LOG(INFO) << "Changing bitrate: " << bitrate; - NvPipe_SetBitrate(nvenc_, bitrate, fps); - last_bitrate_ = pkt.bitrate; - }*/ - - //LOG(INFO) << "NvPipe Encode: " << int(definition) << " " << in.cols; - - //pkt.flags = 0; - - //cv::Mat tmp; - /*if (tmp.type() == CV_32F) { - if (isLossy(pkt.codec)) { - // Use special encoding transform - tmp2_.create(tmp.size(), CV_8UC4); - ftl::cuda::depth_to_vuya(tmp, tmp2_, 16.0f, stream_); - pkt.flags |= NvPipeEncoder::kFlagMappedDepth; - } else { - tmp.convertTo(tmp2_, CV_16UC1, 1000, stream_); - } - } else if (tmp.type() == CV_8UC3) { - cv::cuda::cvtColor(tmp, tmp2_, cv::COLOR_BGR2RGBA, 0, stream_); - } else if (tmp.type() == CV_8UC4) { - if (fmt == format_t::BGRA8) { - cv::cuda::cvtColor(tmp, tmp2_, cv::COLOR_BGRA2RGBA, 0, stream_); - pkt.flags |= NvPipeEncoder::kFlagRGB; - } else if (fmt == format_t::VUYA16) { - tmp2_ = tmp; - } - //} else if (tmp.type() == CV_16UC4) { - - } else { - LOG(ERROR) << "Unsupported cv::Mat type in Nvidia encoder"; - return false; - }*/ - - // Make sure conversions complete... - //stream_.waitForCompletion(); - - //pkt.flags = NvPipeEncoder::kFlagRGB | NvPipeEncoder::kFlagMappedDepth; - - // TODO: Use page locked memory? - pkt.data.resize(ftl::codecs::kVideoBufferSize); - uint64_t cs = NvPipe_Encode( - nvenc_, - in.data, - in.step, - pkt.data.data(), - ftl::codecs::kVideoBufferSize, - in.cols, - in.rows, - was_reset_ // Force IFrame! - ); - pkt.data.resize(cs); - was_reset_ = false; - - if (cs == 0 || cs >= ftl::codecs::kVideoBufferSize) { - LOG(ERROR) << "Could not encode video frame: " << NvPipe_GetError(nvenc_); - return false; - } else { - return true; - } -} - -static NvPipe_Codec selectCodec(const Packet &pkt) { - return (pkt.codec == codec_t::HEVC || pkt.codec == codec_t::HEVC_LOSSLESS) ? NVPIPE_HEVC : NVPIPE_H264; -} - -static NvPipe_Compression selectCompression(const Packet &pkt, format_t fmt) { - switch (fmt) { - case format_t::BGRA8 : - case format_t::RGBA8 : return NVPIPE_LOSSY; - case format_t::F32 : return (isLossy(pkt.codec)) ? NVPIPE_LOSSY_10BIT_420 : NVPIPE_LOSSLESS; - case format_t::VUYA16 : return NVPIPE_LOSSY_10BIT_420; // FIXME: Check codec. - case format_t::U16 : return NVPIPE_LOSSLESS; - } - return NVPIPE_LOSSY; -} - -static NvPipe_Format selectFormat(const Packet &pkt, format_t fmt) { - switch (fmt) { - case format_t::BGRA8 : - case format_t::RGBA8 : return NVPIPE_RGBA32; - case format_t::F32 : return (isLossy(pkt.codec)) ? NVPIPE_YUV32 : NVPIPE_UINT16; - case format_t::U16 : return NVPIPE_UINT16; - case format_t::VUYA16 : return NVPIPE_YUV32; - } - return NVPIPE_RGBA32; -} - -bool NvPipeEncoder::_encoderMatch(const ftl::codecs::Packet &pkt, format_t fmt) { - return compression_ == selectCompression(pkt, fmt) && - format_ == selectFormat(pkt, fmt) && - codec_ == selectCodec(pkt) && last_bitrate_ == pkt.bitrate; -} - -bool NvPipeEncoder::_createEncoder(const ftl::codecs::Packet &pkt, format_t fmt) { - if (_encoderMatch(pkt, fmt) && nvenc_) return true; - - uint64_t bitrate = calculateBitrate(pkt.definition, float(pkt.bitrate)/255.0f) * pkt.frame_count; - //if (is_float_channel_) bitrate *= 2.0f; - //LOG(INFO) << "Calculated bitrate: " << bitrate; - - format_ = selectFormat(pkt, fmt); - compression_ = selectCompression(pkt, fmt); - codec_ = selectCodec(pkt); - last_bitrate_ = pkt.bitrate; - - if (nvenc_) NvPipe_Destroy(nvenc_); - const int fps = 1000/ftl::timer::getInterval(); - nvenc_ = NvPipe_CreateEncoder( - format_, - codec_, - compression_, - bitrate, - fps, // FPS - ftl::codecs::getWidth(pkt.definition), // Output Width - ftl::codecs::getHeight(pkt.definition) // Output Height - ); - - if (!nvenc_) { - LOG(ERROR) << "Could not create video encoder: " << NvPipe_GetError(NULL); - return false; - } else { - LOG(INFO) << "NvPipe encoder created"; - return true; - } -} diff --git a/components/codecs/src/opencv_encoder.cpp b/components/codecs/src/opencv_encoder.cpp index 6cf2a3183ded4e3090dbcd9cb89f4b3574b4fe90..75f1a0553d9d8996fc4de710401c1b124bd6e411 100644 --- a/components/codecs/src/opencv_encoder.cpp +++ b/components/codecs/src/opencv_encoder.cpp @@ -28,10 +28,12 @@ bool OpenCVEncoder::supports(ftl::codecs::codec_t codec) { } bool OpenCVEncoder::encode(const cv::cuda::GpuMat &in, ftl::codecs::Packet &pkt) { - bool is_colour = !(pkt.flags & ftl::codecs::kFlagFloat); + bool is_colour = in.type() == CV_8UC4; - if (is_colour && in.type() != CV_8UC4 && in.type() != CV_8UC1) return false; - if (!is_colour && in.type() == CV_8UC4) { + if (pkt.codec == codec_t::Any) pkt.codec = (is_colour) ? codec_t::JPG : codec_t::PNG; + if (!supports(pkt.codec)) return false; + + if (!is_colour && pkt.codec == codec_t::JPG) { LOG(ERROR) << "OpenCV Encoder doesn't support lossy depth"; return false; } @@ -43,39 +45,23 @@ bool OpenCVEncoder::encode(const cv::cuda::GpuMat &in, ftl::codecs::Packet &pkt) return false; } - /*pkt.definition = (pkt.definition == definition_t::Any) ? ftl::codecs::findDefinition(in.cols, in.rows) : pkt.definition; - - if (pkt.definition == definition_t::Invalid || pkt.definition == definition_t::Any) { - LOG(ERROR) << "Invalid definition"; - return false; - }*/ - // Ensure definition does not exceed max current_definition_ = pkt.definition; //((int)pkt.definition < (int)max_definition) ? max_definition : pkt.definition; in.download(tmp_); - //CHECK(cv::Size(ftl::codecs::getWidth(definition), ftl::codecs::getHeight(definition)) == in.size()); - //if (!is_colour) { - //tmp_.convertTo(tmp_, CV_16U, 1000.0f); - //} + if (!is_colour && in.type() == CV_32F) { + tmp_.convertTo(tmp_, CV_16U, 1000.0f); + } int width = ftl::codecs::getWidth(current_definition_); int height = ftl::codecs::getHeight(current_definition_); - // Scale down image to match requested definition... - /*if (ftl::codecs::getHeight(current_definition_) < in.rows) { - cv::resize(tmp_, tmp_, cv::Size(ftl::codecs::getWidth(current_definition_), ftl::codecs::getHeight(current_definition_)), 0, 0, (is_colour) ? 1 : cv::INTER_NEAREST); - } else { - - }*/ if (tx*width != in.cols || ty*height != in.rows) { LOG(ERROR) << "Input does not match requested definition"; return false; } - if (pkt.codec == codec_t::Any) pkt.codec = (is_colour && in.type() != CV_8UC1) ? codec_t::JPG : codec_t::PNG; - //for (int i=0; i<chunk_count_; ++i) { // Add chunk job to thread pool //ftl::pool.push([this,i,cb,is_colour,bitrate](int id) { diff --git a/components/codecs/test/CMakeLists.txt b/components/codecs/test/CMakeLists.txt index 9646e2c3f969babe1f006bf106fb0f4542422e56..7a64f1b08f58c85356ff87a0840a75517322b148 100644 --- a/components/codecs/test/CMakeLists.txt +++ b/components/codecs/test/CMakeLists.txt @@ -15,32 +15,32 @@ target_link_libraries(opencv_codec_unit add_test(OpenCVCodecUnitTest opencv_codec_unit) -### NvPipe Codec Unit ################################################################ -add_executable(nvpipe_codec_unit +### Nvidia Codec Unit ################################################################ +add_executable(nvidia_codec_unit $<TARGET_OBJECTS:CatchTest> ../src/bitrates.cpp ../src/encoder.cpp #../src/nvpipe_encoder.cpp #../src/nvpipe_decoder.cpp - $<TARGET_OBJECTS:NvPipeCodec> + $<TARGET_OBJECTS:NvidiaCodec> ../src/depth_convert.cu - ./nvpipe_codec_unit.cpp + ./nvidia_codec_unit.cpp ) if (WIN32) if (CMAKE_SIZEOF_VOID_P EQUAL 8) - target_link_directories(nvpipe_codec_unit PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../src/Video_Codec_SDK_9.1.23/Lib/x64) + target_link_directories(nvidia_codec_unit PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../src/Video_Codec_SDK_9.1.23/Lib/x64) elseif (CMAKE_SIZEOF_VOID_P EQUAL 4) - target_link_directories(nvpipe_codec_unit PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../src/Video_Codec_SDK_9.1.23/Lib/Win32) + target_link_directories(nvidia_codec_unit PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../src/Video_Codec_SDK_9.1.23/Lib/Win32) endif() endif() -target_include_directories(nvpipe_codec_unit PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include" +target_include_directories(nvidia_codec_unit PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include" ${CMAKE_CURRENT_SOURCE_DIR}/../src/Video_Codec_SDK_9.1.23/include ${CMAKE_CURRENT_SOURCE_DIR}/../src/Video_Codec_SDK_9.1.23/Samples/NvCodec) -target_link_libraries(nvpipe_codec_unit - Threads::Threads ${OS_LIBS} ${OpenCV_LIBS} ${CUDA_LIBRARIES} NvPipe ftlcommon nvcuvid cuda) +target_link_libraries(nvidia_codec_unit + Threads::Threads ${OS_LIBS} ${OpenCV_LIBS} ${CUDA_LIBRARIES} ftlcommon nvcuvid cuda) -add_test(NvPipeCodecUnitTest nvpipe_codec_unit) +add_test(NvidiaCodecUnitTest nvidia_codec_unit) ### Reader Writer Unit ################################################################ #add_executable(rw_unit diff --git a/components/codecs/test/nvpipe_codec_unit.cpp b/components/codecs/test/nvidia_codec_unit.cpp similarity index 59% rename from components/codecs/test/nvpipe_codec_unit.cpp rename to components/codecs/test/nvidia_codec_unit.cpp index 74bd9cf10bf0831c1aa6a48da7791353f3de5c1d..67928ea76b1980d6a298180a730999f9c334d37f 100644 --- a/components/codecs/test/nvpipe_codec_unit.cpp +++ b/components/codecs/test/nvidia_codec_unit.cpp @@ -1,6 +1,6 @@ #include "catch.hpp" -#include <ftl/codecs/nvpipe_encoder.hpp> -#include <ftl/codecs/nvpipe_decoder.hpp> +#include <ftl/codecs/nvidia_encoder.hpp> +#include <ftl/codecs/nvidia_decoder.hpp> #include <ftl/codecs/hevc.hpp> #include <ftl/threads.hpp> @@ -25,14 +25,13 @@ namespace ftl { } -TEST_CASE( "NvPipeEncoder::encode() - A valid colour image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); +TEST_CASE( "NvidiaEncoder::encode() - A valid colour image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); cv::cuda::GpuMat m(cv::Size(1920,1080), CV_8UC4, cv::Scalar(0,0,0,0)); ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.flags = 0; pkt.frame_count = 1; @@ -41,8 +40,7 @@ TEST_CASE( "NvPipeEncoder::encode() - A valid colour image" ) { REQUIRE( r ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD1080 ); - REQUIRE( pkt.flags == 0 ); + REQUIRE( pkt.flags == ftl::codecs::kFlagFlipRGB ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); } @@ -58,23 +56,12 @@ TEST_CASE( "NvPipeEncoder::encode() - A valid colour image" ) { REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); } - SECTION("invalid frame count of 2") { - pkt.frame_count = 2; - - bool r = encoder.encode(m, pkt); - - REQUIRE( !r ); - REQUIRE( pkt.definition == definition_t::Invalid ); - REQUIRE( pkt.data.size() == 0 ); - } - SECTION("invalid frame count of 0") { pkt.frame_count = 0; bool r = encoder.encode(m, pkt); REQUIRE( !r ); - REQUIRE( pkt.definition == definition_t::Invalid ); REQUIRE( pkt.data.size() == 0 ); } @@ -83,9 +70,9 @@ TEST_CASE( "NvPipeEncoder::encode() - A valid colour image" ) { bool r = encoder.encode(m, pkt); - REQUIRE( !r ); - REQUIRE( pkt.flags == 0 ); - REQUIRE( pkt.data.size() == 0 ); + REQUIRE( r ); + REQUIRE( pkt.flags == ftl::codecs::kFlagFlipRGB ); + REQUIRE( pkt.data.size() != 0 ); } SECTION("invalid codec") { @@ -97,27 +84,16 @@ TEST_CASE( "NvPipeEncoder::encode() - A valid colour image" ) { REQUIRE( pkt.codec == codec_t::Invalid ); REQUIRE( pkt.data.size() == 0 ); } - - SECTION("invalid definition") { - pkt.definition = definition_t::HD720; - - bool r = encoder.encode(m, pkt); - - REQUIRE( !r ); - REQUIRE( pkt.definition == definition_t::Invalid ); - REQUIRE( pkt.data.size() == 0 ); - } } -TEST_CASE( "NvPipeEncoder::encode() - A tiled colour image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); +TEST_CASE( "NvidiaEncoder::encode() - A tiled colour image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); cv::cuda::GpuMat m(cv::Size(2560,720), CV_8UC4, cv::Scalar(0,0,0,0)); SECTION("auto codec and definition, 2x1 frames") { ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.flags = 0; pkt.frame_count = 2; @@ -125,114 +101,76 @@ TEST_CASE( "NvPipeEncoder::encode() - A tiled colour image" ) { REQUIRE( r ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); - REQUIRE( pkt.flags == 0 ); + REQUIRE( pkt.flags == ftl::codecs::kFlagFlipRGB ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); } } -TEST_CASE( "NvPipeEncoder::encode() - A valid lossless float image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); - cv::cuda::GpuMat m(cv::Size(1280,720), CV_16U, cv::Scalar(0)); +TEST_CASE( "NvidiaEncoder::encode() - A valid lossless float image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); + cv::cuda::GpuMat m(cv::Size(1280,720), CV_32F, cv::Scalar(0.0f)); SECTION("auto codec and definition, single frame") { ftl::codecs::Packet pkt; - pkt.codec = codec_t::Any; + pkt.codec = codec_t::HEVC_LOSSLESS; pkt.bitrate = 255; - pkt.definition = definition_t::Any; - pkt.flags = ftl::codecs::kFlagFloat; + pkt.flags = 0; pkt.frame_count = 1; bool r = encoder.encode(m, pkt); REQUIRE( r ); REQUIRE( pkt.codec == codec_t::HEVC_LOSSLESS ); - REQUIRE( pkt.definition == definition_t::HD720 ); REQUIRE( pkt.flags == ftl::codecs::kFlagFloat ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); } - SECTION("missing float flag") { - ftl::codecs::Packet pkt; - pkt.codec = codec_t::Any; - pkt.bitrate = 255; - pkt.definition = definition_t::Any; - pkt.flags = 0; - pkt.frame_count = 1; - - bool r = encoder.encode(m, pkt); - - REQUIRE( !r ); - REQUIRE( pkt.data.size() == 0 ); - } - SECTION("invalid lossy flag") { ftl::codecs::Packet pkt; - pkt.codec = codec_t::Any; + pkt.codec = codec_t::HEVC_LOSSLESS; pkt.bitrate = 255; - pkt.definition = definition_t::Any; - pkt.flags = ftl::codecs::kFlagFloat & ftl::codecs::kFlagMappedDepth; + pkt.flags = ftl::codecs::kFlagMappedDepth; pkt.frame_count = 1; bool r = encoder.encode(m, pkt); - REQUIRE( !r ); - REQUIRE( pkt.data.size() == 0 ); + REQUIRE( r ); + REQUIRE( pkt.flags == ftl::codecs::kFlagFloat ); + REQUIRE( pkt.data.size() != 0 ); } } -TEST_CASE( "NvPipeEncoder::encode() - A valid lossy float image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); - cv::cuda::GpuMat m(cv::Size(1280,720), CV_8UC4, cv::Scalar(0)); +TEST_CASE( "NvidiaEncoder::encode() - A valid lossy float image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); + cv::cuda::GpuMat m(cv::Size(1280,720), CV_32F, cv::Scalar(0.0f)); SECTION("auto codec and definition, single frame") { ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; - pkt.flags = ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth; - pkt.frame_count = 1; - - bool r = encoder.encode(m, pkt); - - REQUIRE( r ); - REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); - REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth) ); - REQUIRE( pkt.data.size() > 0 ); - REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); - } - - SECTION("correct codec, missing flag") { - ftl::codecs::Packet pkt; - pkt.codec = codec_t::HEVC; - pkt.bitrate = 255; - pkt.definition = definition_t::Any; - pkt.flags = ftl::codecs::kFlagFloat; + pkt.flags = 0; pkt.frame_count = 1; bool r = encoder.encode(m, pkt); REQUIRE( r ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth) ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); } } -TEST_CASE( "NvPipeEncoder::encode() - A tiled lossy float image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); - cv::cuda::GpuMat m(cv::Size(2560,720), CV_8UC4, cv::Scalar(0)); +TEST_CASE( "NvidiaEncoder::encode() - A tiled lossy float image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); + cv::cuda::GpuMat m(cv::Size(2560,720), CV_32F, cv::Scalar(0)); SECTION("auto codec and definition, 2x1 frame") { ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.flags = ftl::codecs::kFlagFloat & ftl::codecs::kFlagMappedDepth; pkt.frame_count = 2; @@ -240,39 +178,36 @@ TEST_CASE( "NvPipeEncoder::encode() - A tiled lossy float image" ) { REQUIRE( r ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); - REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat & ftl::codecs::kFlagMappedDepth) ); + REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth) ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); } } -TEST_CASE( "NvPipeEncoder::encode() - A large tiled lossy float image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); - cv::cuda::GpuMat m(cv::Size(5120,1440), CV_8UC4, cv::Scalar(0)); +TEST_CASE( "NvidiaEncoder::encode() - A large tiled lossy float image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); + cv::cuda::GpuMat m(cv::Size(5120,1440), CV_32F, cv::Scalar(0)); SECTION("auto codec and definition, 4x2 frame") { ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; - pkt.flags = ftl::codecs::kFlagFloat & ftl::codecs::kFlagMappedDepth; + pkt.flags = 0; pkt.frame_count = 7; bool r = encoder.encode(m, pkt); REQUIRE( r ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); - REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat & ftl::codecs::kFlagMappedDepth) ); + REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth) ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); } } -TEST_CASE( "NvPipeDecoder::decode() - A colour test image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); - ftl::codecs::NvPipeDecoder decoder; +TEST_CASE( "NvidiaDecoder::decode() - A colour test image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); + ftl::codecs::NvidiaDecoder decoder; cv::cuda::GpuMat in; cv::cuda::GpuMat out; @@ -284,7 +219,6 @@ TEST_CASE( "NvPipeDecoder::decode() - A colour test image" ) { ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 1; pkt.flags = 0; bool r = encoder.encode(in, pkt); @@ -298,9 +232,9 @@ TEST_CASE( "NvPipeDecoder::decode() - A colour test image" ) { REQUIRE( (cv::cuda::sum(out) != cv::Scalar(0,0,0)) ); } -TEST_CASE( "NvPipeDecoder::decode() - A tiled colour image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); - ftl::codecs::NvPipeDecoder decoder; +TEST_CASE( "NvidiaDecoder::decode() - A tiled colour image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); + ftl::codecs::NvidiaDecoder decoder; cv::cuda::GpuMat in; cv::cuda::GpuMat out; @@ -312,7 +246,6 @@ TEST_CASE( "NvPipeDecoder::decode() - A tiled colour image" ) { ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 2; pkt.flags = 0; bool r = encoder.encode(in, pkt); @@ -321,87 +254,79 @@ TEST_CASE( "NvPipeDecoder::decode() - A tiled colour image" ) { REQUIRE( decoder.decode(pkt, out) ); REQUIRE( (out.cols == 2560) ); REQUIRE( (out.type() == CV_8UC4) ); - REQUIRE( (pkt.definition == definition_t::HD720) ); //} REQUIRE( (cv::cuda::sum(out) != cv::Scalar(0,0,0)) ); } -TEST_CASE( "NvPipeDecoder::decode() - A lossless depth image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); - ftl::codecs::NvPipeDecoder decoder; +TEST_CASE( "NvidiaDecoder::decode() - A lossless depth image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); + ftl::codecs::NvidiaDecoder decoder; cv::cuda::GpuMat in; cv::cuda::GpuMat out; //SECTION("FHD in and out, FHD encoding") { - in = cv::cuda::GpuMat(cv::Size(1280,720), CV_16U, cv::Scalar(255)); - out = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(0)); + in = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(10.0f)); + out = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(0.0f)); ftl::codecs::Packet pkt; - pkt.codec = codec_t::Any; + pkt.codec = codec_t::HEVC_LOSSLESS; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 1; - pkt.flags = ftl::codecs::kFlagFloat; + pkt.flags = 0; bool r = encoder.encode(in, pkt); REQUIRE( r ); REQUIRE( decoder.decode(pkt, out) ); - REQUIRE( (pkt.definition == definition_t::HD720) ); //} REQUIRE( (cv::cuda::sum(out) != cv::Scalar(0)) ); } -TEST_CASE( "NvPipeDecoder::decode() - A lossy depth image" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); - ftl::codecs::NvPipeDecoder decoder; +TEST_CASE( "NvidiaDecoder::decode() - A lossy depth image" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); + ftl::codecs::NvidiaDecoder decoder; cv::cuda::GpuMat in; cv::cuda::GpuMat out; //SECTION("FHD in and out, FHD encoding") { - in = cv::cuda::GpuMat(cv::Size(1280,720), CV_8UC4, cv::Scalar(255)); + in = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(10.0f)); out = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(0)); ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 1; - pkt.flags = ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth; + pkt.flags = 0; bool r = encoder.encode(in, pkt); REQUIRE( r ); REQUIRE( decoder.decode(pkt, out) ); - REQUIRE( (pkt.definition == definition_t::HD720) ); //} REQUIRE( (cv::cuda::sum(out) != cv::Scalar(0)) ); } -TEST_CASE( "NvPipeDecoder::decode() - corrupted packet" ) { - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); - ftl::codecs::NvPipeDecoder decoder; +TEST_CASE( "NvidiaDecoder::decode() - corrupted packet" ) { + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); + ftl::codecs::NvidiaDecoder decoder; cv::cuda::GpuMat in; cv::cuda::GpuMat out; - SECTION("Corrupted definition") { + SECTION("Bad output size") { in = cv::cuda::GpuMat(cv::Size(2560,720), CV_8UC4, cv::Scalar(255,0,0,0)); - out = cv::cuda::GpuMat(cv::Size(2560,720), CV_8UC4, cv::Scalar(0,0,0,0)); + out = cv::cuda::GpuMat(cv::Size(2500,720), CV_8UC4, cv::Scalar(0,0,0,0)); ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 2; pkt.flags = 0; bool r = encoder.encode(in, pkt); - pkt.definition = definition_t::HD1080; - REQUIRE( r ); REQUIRE( !decoder.decode(pkt, out) ); } @@ -413,7 +338,6 @@ TEST_CASE( "NvPipeDecoder::decode() - corrupted packet" ) { ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 2; pkt.flags = 0; bool r = encoder.encode(in, pkt); @@ -431,7 +355,6 @@ TEST_CASE( "NvPipeDecoder::decode() - corrupted packet" ) { ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 2; pkt.flags = 0; bool r = encoder.encode(in, pkt); @@ -449,7 +372,6 @@ TEST_CASE( "NvPipeDecoder::decode() - corrupted packet" ) { ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 2; pkt.flags = 0; bool r = encoder.encode(in, pkt); @@ -461,32 +383,30 @@ TEST_CASE( "NvPipeDecoder::decode() - corrupted packet" ) { } SECTION("Corrupted float mapped flags") { - in = cv::cuda::GpuMat(cv::Size(1280,720), CV_16U, cv::Scalar(255)); - out = cv::cuda::GpuMat(cv::Size(1280,720), CV_16U, cv::Scalar(0)); + in = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(10.0f)); + out = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(0)); ftl::codecs::Packet pkt; - pkt.codec = codec_t::Any; + pkt.codec = codec_t::HEVC_LOSSLESS; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 1; pkt.flags = ftl::codecs::kFlagFloat; bool r = encoder.encode(in, pkt); - pkt.codec = codec_t::HEVC; + //pkt.codec = codec_t::HEVC; pkt.flags = ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth; REQUIRE( r ); - REQUIRE( !decoder.decode(pkt, out) ); + REQUIRE( decoder.decode(pkt, out) ); } SECTION("Missing float flag - lossless") { - in = cv::cuda::GpuMat(cv::Size(1280,720), CV_16U, cv::Scalar(255)); - out = cv::cuda::GpuMat(cv::Size(1280,720), CV_16U, cv::Scalar(0)); + in = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(255)); + out = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(0)); ftl::codecs::Packet pkt; - pkt.codec = codec_t::Any; + pkt.codec = codec_t::HEVC_LOSSLESS; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 1; pkt.flags = ftl::codecs::kFlagFloat; bool r = encoder.encode(in, pkt); @@ -498,15 +418,14 @@ TEST_CASE( "NvPipeDecoder::decode() - corrupted packet" ) { } SECTION("Missing data") { - in = cv::cuda::GpuMat(cv::Size(1280,720), CV_16U, cv::Scalar(255)); - out = cv::cuda::GpuMat(cv::Size(1280,720), CV_16U, cv::Scalar(0)); + in = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(255)); + out = cv::cuda::GpuMat(cv::Size(1280,720), CV_32F, cv::Scalar(0)); ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; pkt.bitrate = 255; - pkt.definition = definition_t::Any; pkt.frame_count = 1; - pkt.flags = ftl::codecs::kFlagFloat; + pkt.flags = 0; bool r = encoder.encode(in, pkt); pkt.data.resize(0); diff --git a/components/rgbd-sources/include/ftl/rgbd/source.hpp b/components/rgbd-sources/include/ftl/rgbd/source.hpp index 41d9e88b0a811ae487a308cbf15668da2734498f..5676c25d34dcc3dbbacd0460dbb6c993e4df3cc4 100644 --- a/components/rgbd-sources/include/ftl/rgbd/source.hpp +++ b/components/rgbd-sources/include/ftl/rgbd/source.hpp @@ -274,7 +274,7 @@ void ftl::rgbd::Source::inject(ftl::codecs::Channel c, ARGS... args) { pkt.codec = ftl::codecs::codec_t::MSGPACK; pkt.bitrate = 0; pkt.frame_count = 1; - pkt.definition = ftl::codecs::definition_t::Any; + //pkt.definition = ftl::codecs::definition_t::Any; pkt.flags = 0; VectorBuffer buf(pkt.data); diff --git a/components/rgbd-sources/src/source.cpp b/components/rgbd-sources/src/source.cpp index 06470b21d513003fdc1d4b8accdcfcd6cb869c4c..d39ea51d587cc95d26727f0c63c7e207c6823aef 100644 --- a/components/rgbd-sources/src/source.cpp +++ b/components/rgbd-sources/src/source.cpp @@ -323,7 +323,7 @@ void Source::inject(const Eigen::Matrix4d &pose) { spkt.channel = Channel::Pose; spkt.streamID = 0; pkt.codec = ftl::codecs::codec_t::MSGPACK; - pkt.definition = ftl::codecs::definition_t::Any; + //pkt.definition = ftl::codecs::definition_t::Any; pkt.bitrate = 0; pkt.frame_count = 1; pkt.flags = 0; diff --git a/components/streams/src/injectors.cpp b/components/streams/src/injectors.cpp index 01dcbef368a8b642abbdf91b25aa31e3c8ee857c..539c9d3765d36a9970137265b4ebc96dab359a6b 100644 --- a/components/streams/src/injectors.cpp +++ b/components/streams/src/injectors.cpp @@ -23,7 +23,7 @@ void ftl::stream::injectConfig(ftl::stream::Stream *stream, const ftl::rgbd::Fra ftl::codecs::Packet pkt; pkt.codec = ftl::codecs::codec_t::MSGPACK; - pkt.definition = ftl::codecs::definition_t::Any; + //pkt.definition = ftl::codecs::definition_t::Any; pkt.bitrate = 0; pkt.frame_count = 1; pkt.flags = 0; @@ -45,7 +45,7 @@ void ftl::stream::injectPose(ftl::stream::Stream *stream, const ftl::rgbd::Frame ftl::codecs::Packet pkt; pkt.codec = ftl::codecs::codec_t::MSGPACK; - pkt.definition = ftl::codecs::definition_t::Any; + //pkt.definition = ftl::codecs::definition_t::Any; pkt.bitrate = 0; pkt.frame_count = 1; pkt.flags = 0; @@ -73,7 +73,7 @@ void ftl::stream::injectCalibration(ftl::stream::Stream *stream, const ftl::rgbd ftl::codecs::Packet pkt; pkt.codec = ftl::codecs::codec_t::MSGPACK; - pkt.definition = ftl::codecs::definition_t::Any; + //pkt.definition = ftl::codecs::definition_t::Any; pkt.bitrate = 0; pkt.frame_count = 1; pkt.flags = 0; diff --git a/components/streams/src/receiver.cpp b/components/streams/src/receiver.cpp index da2a97577ec357ebba436c206fa2a65f1a1fe697..a319d60e3b14718b67fdf7b07436eecb95c9d8e6 100644 --- a/components/streams/src/receiver.cpp +++ b/components/streams/src/receiver.cpp @@ -222,9 +222,14 @@ void Receiver::_processVideo(const StreamPacket &spkt, const Packet &pkt) { InternalVideoStates &ividstate = _getVideoFrame(spkt); auto [tx,ty] = ftl::codecs::chooseTileConfig(pkt.frame_count); - int width = ftl::codecs::getWidth(pkt.definition); - int height = ftl::codecs::getHeight(pkt.definition); - int sheight = height; + + int width = ividstate.state.getLeft().width; + int height = ividstate.state.getLeft().height; + + if (width == 0 || height == 0) { + LOG(WARNING) << "No calibration, skipping frame"; + return; + } //LOG(INFO) << " CODEC = " << (int)pkt.codec << " " << (int)pkt.flags << " " << (int)spkt.channel; //LOG(INFO) << "Decode surface: " << (width*tx) << "x" << (height*ty); @@ -274,8 +279,6 @@ void Receiver::_processVideo(const StreamPacket &spkt, const Packet &pkt) { cv::waitKey(1); }*/ - bool apply_Y_filter = value("apply_Y_filter", true); - // Mark a frameset as being partial if (pkt.flags & ftl::codecs::kFlagPartial) { builder_[spkt.streamID].markPartial(spkt.timestamp); @@ -296,18 +299,6 @@ void Receiver::_processVideo(const StreamPacket &spkt, const Packet &pkt) { LOG(WARNING) << "Previous frame not complete: " << spkt.timestamp; } - { - // This ensures that if previous frames are unfinished then they - // are discarded. - /*UNIQUE_LOCK(vidstate.mutex, lk); - if (frame.timestamp != spkt.timestamp && frame.timestamp != -1) { - frame.frame.reset(); - frame.completed.clear(); - LOG(WARNING) << "Frames out-of-phase by: " << spkt.timestamp - frame.timestamp; - } - frame.timestamp = spkt.timestamp;*/ - } - // Add channel to frame and allocate memory if required const cv::Size size = cv::Size(width, height); frame.getBuffer<cv::cuda::GpuMat>(spkt.channel).create(size, ftl::codecs::type(spkt.channel)); //(isFloatChannel(rchan) ? CV_32FC1 : CV_8UC4)); @@ -315,29 +306,6 @@ void Receiver::_processVideo(const StreamPacket &spkt, const Packet &pkt) { cv::Rect roi((i % tx)*width, (i / tx)*height, width, height); cv::cuda::GpuMat sroi = surface(roi); sroi.copyTo(frame.getBuffer<cv::cuda::GpuMat>(spkt.channel), cvstream); - - // Do colour conversion - /*if (isFloatChannel(rchan) && (pkt.flags & 0x2)) { - cv::Rect croi((i % tx)*width, ty*height+(i / tx)*height/2, width, height/2); - cv::cuda::GpuMat csroi = surface(croi); - // Smooth Y channel around discontinuities - // Lerp the uv channels / smooth over a small kernal size. - - //if (value("apply_bilateral", true)) { - // cv::cuda::split - // Apply disparity bilateral to the luminance channel - // cv::cuda::merge or overload vuya_to_depth - //} - - //if (apply_Y_filter) ftl::cuda::smooth_y(sroi, cvstream); - ftl::cuda::vuya_to_depth(frame.getBuffer<cv::cuda::GpuMat>(spkt.channel), sroi, csroi, 16.0f, cvstream); - } else if (isFloatChannel(rchan)) { - sroi.convertTo(frame.getBuffer<cv::cuda::GpuMat>(spkt.channel), CV_32FC1, 1.0f/1000.0f, cvstream); - } else if (sroi.type() == CV_8UC1) { - sroi.copyTo(frame.getBuffer<cv::cuda::GpuMat>(spkt.channel), cvstream); - } else { - cv::cuda::cvtColor(sroi, frame.getBuffer<cv::cuda::GpuMat>(spkt.channel), cv::COLOR_RGBA2BGRA, 0, cvstream); - }*/ } // Must ensure all processing is finished before completing a frame. diff --git a/components/streams/src/sender.cpp b/components/streams/src/sender.cpp index 34cbf255ffc8d5cd36430e38d076c9aa802d83c9..8667adb5f6ba162af18f4e61c59eb823fddf5355 100644 --- a/components/streams/src/sender.cpp +++ b/components/streams/src/sender.cpp @@ -348,7 +348,7 @@ void Sender::_encodeChannel(ftl::rgbd::FrameSet &fs, Channel c, bool reset) { } //cudaSafeCall(cudaStreamSynchronize(enc->stream())); - enc->stream().waitForCompletion(); + //enc->stream().waitForCompletion(); if (enc) { if (reset) enc->reset(); @@ -361,11 +361,6 @@ void Sender::_encodeChannel(ftl::rgbd::FrameSet &fs, Channel c, bool reset) { pkt.bitrate = (!lossless && ftl::codecs::isFloatChannel(cc)) ? max_bitrate : max_bitrate/2; pkt.flags = 0; - if (!lossless && ftl::codecs::isFloatChannel(cc)) pkt.flags = ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth; - else if (lossless && ftl::codecs::isFloatChannel(cc)) pkt.flags = ftl::codecs::kFlagFloat; - else pkt.flags = ftl::codecs::kFlagFlipRGB; - if (is_stereo) pkt.flags |= ftl::codecs::kFlagStereo; - // In the event of partial frames, add a flag to indicate that if (static_cast<size_t>(fs.count) < fs.frames.size()) pkt.flags |= ftl::codecs::kFlagPartial; @@ -449,12 +444,7 @@ int Sender::_generateTiles(const ftl::rgbd::FrameSet &fs, int offset, Channel c, int tilecount = tx*ty; uint32_t count = 0; - int cvtype = CV_8UC4; - switch (m.type()) { - case CV_32F : cvtype = (lossless && m.type() == CV_32F) ? CV_16U : CV_8UC4; break; - case CV_8UC1 : cvtype = CV_8UC1; break; - default : cvtype = CV_8UC4; - } + int cvtype = m.type(); surface.surface.create(height, width, cvtype); @@ -466,46 +456,7 @@ int Sender::_generateTiles(const ftl::rgbd::FrameSet &fs, int offset, Channel c, cv::Rect roi((count % tx)*rwidth, (count / tx)*rheight, (stereo) ? rwidth/2 : rwidth, rheight); cv::cuda::GpuMat sroi = surface.surface(roi); - if (m.type() == CV_32F) { - if (lossless) { - m.convertTo(sroi, CV_16UC1, 1000, stream); - } else { - ftl::cuda::depth_to_vuya(m, sroi, _selectFloatMax(c), stream); - } - } else if (m.type() == CV_8UC4) { - cv::cuda::cvtColor(m, sroi, cv::COLOR_BGRA2RGBA, 0, stream); - } else if (m.type() == CV_8UC3) { - cv::cuda::cvtColor(m, sroi, cv::COLOR_BGR2RGBA, 0, stream); - } else if (m.type() == CV_8UC1) { - m.copyTo(sroi, stream); - } else { - LOG(ERROR) << "Unsupported colour format: " << m.type(); - return 0; - } - - // Do the right channel - if (stereo) { - auto &m = cframe->get<cv::cuda::GpuMat>((c == Channel::Colour) ? Channel::Colour2 : Channel::Colour2HighRes); - cv::Rect roi((count % tx)*rwidth + (rwidth/2), (count / tx)*rheight, rwidth/2, rheight); - cv::cuda::GpuMat sroi = surface.surface(roi); - - if (m.type() == CV_32F) { - if (lossless) { - m.convertTo(sroi, CV_16UC1, 1000, stream); - } else { - ftl::cuda::depth_to_vuya(m, sroi, _selectFloatMax(c), stream); - } - } else if (m.type() == CV_8UC4) { - cv::cuda::cvtColor(m, sroi, cv::COLOR_BGRA2RGBA, 0, stream); - } else if (m.type() == CV_8UC3) { - cv::cuda::cvtColor(m, sroi, cv::COLOR_BGR2RGBA, 0, stream); - } else if (m.type() == CV_8UC1) { - m.copyTo(sroi, stream); - } else { - LOG(ERROR) << "Unsupported colour format: " << m.type(); - return 0; - } - } + m.copyTo(sroi, stream); } else { cv::Rect roi((count % tx)*rwidth, (count / tx)*rheight, rwidth, rheight); cv::cuda::GpuMat sroi = surface.surface(roi); diff --git a/components/streams/test/receiver_unit.cpp b/components/streams/test/receiver_unit.cpp index 755c55c2293717c3490171813f08abebec735343..7ff3df42d4eb7631074fd99801f0a291b4ea8d1e 100644 --- a/components/streams/test/receiver_unit.cpp +++ b/components/streams/test/receiver_unit.cpp @@ -1,7 +1,7 @@ #include "catch.hpp" #include <ftl/streams/receiver.hpp> -#include <ftl/codecs/nvpipe_encoder.hpp> +#include <ftl/codecs/nvidia_encoder.hpp> #include <ftl/streams/injectors.hpp> #include <nlohmann/json.hpp> @@ -65,7 +65,7 @@ TEST_CASE( "Receiver generating onFrameSet" ) { receiver->setStream(&stream); receiver->set("frameset_buffer_size", 0); - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; @@ -119,7 +119,7 @@ TEST_CASE( "Receiver generating onFrameSet" ) { SECTION("multi-frameset") { cv::cuda::GpuMat m(cv::Size(1280,720), CV_8UC4, cv::Scalar(0)); - ftl::stream::injectCalibration(&stream, dummy, 1, 0, 0); + ftl::stream::injectCalibration(&stream, dummy, 1, 1, 0); bool r = encoder.encode(m, pkt); REQUIRE( r ); @@ -174,12 +174,12 @@ TEST_CASE( "Receiver generating onFrameSet" ) { } SECTION("a tiled lossy depth frame") { - cv::cuda::GpuMat m(cv::Size(2560,720), CV_8UC4, cv::Scalar(0)); + cv::cuda::GpuMat m(cv::Size(2560,720), CV_32F, cv::Scalar(0)); ftl::stream::injectCalibration(&stream, dummy, 0, 0, 1); spkt.channel = Channel::Depth; pkt.frame_count = 2; - pkt.flags = ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth; + pkt.flags = 0; bool r = encoder.encode(m, pkt); REQUIRE( r ); @@ -208,12 +208,13 @@ TEST_CASE( "Receiver generating onFrameSet" ) { } SECTION("a tiled lossless depth frame") { - cv::cuda::GpuMat m(cv::Size(2560,720), CV_16U, cv::Scalar(0)); + cv::cuda::GpuMat m(cv::Size(2560,720), CV_32F, cv::Scalar(0)); ftl::stream::injectCalibration(&stream, dummy, 0, 0, 1); spkt.channel = Channel::Depth; pkt.frame_count = 2; - pkt.flags = ftl::codecs::kFlagFloat; + pkt.flags = 0; + pkt.codec = codec_t::HEVC_LOSSLESS; bool r = encoder.encode(m, pkt); REQUIRE( r ); @@ -263,7 +264,7 @@ TEST_CASE( "Receiver sync bugs" ) { receiver->setStream(&stream); receiver->set("frameset_buffer_size", 0); - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; @@ -348,7 +349,7 @@ TEST_CASE( "Receiver non zero buffer" ) { receiver->setStream(&stream); receiver->set("frameset_buffer_size", 1); - ftl::codecs::NvPipeEncoder encoder(definition_t::HD1080, definition_t::SD480); + ftl::codecs::NvidiaEncoder encoder(definition_t::HD1080, definition_t::SD480); ftl::codecs::Packet pkt; pkt.codec = codec_t::Any; diff --git a/components/streams/test/sender_unit.cpp b/components/streams/test/sender_unit.cpp index d5a08732807a8b8f4ed0aa30dcd1fadbd0a98ac7..8e646957d9e88c5538e44fdd94cc36444a13b681 100644 --- a/components/streams/test/sender_unit.cpp +++ b/components/streams/test/sender_unit.cpp @@ -101,7 +101,6 @@ TEST_CASE( "Sender::post() video frames" ) { REQUIRE( spkt.streamID == 0 ); REQUIRE( spkt.channel == Channel::Colour ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( pkt.frame_count == 1 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); @@ -127,7 +126,6 @@ TEST_CASE( "Sender::post() video frames" ) { REQUIRE( spkt.streamID == 0 ); REQUIRE( spkt.channel == Channel::Colour ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( pkt.frame_count == 2 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); @@ -153,7 +151,6 @@ TEST_CASE( "Sender::post() video frames" ) { REQUIRE( spkt.streamID == 0 ); REQUIRE( spkt.channel == Channel::Depth ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth) ); REQUIRE( pkt.frame_count == 2 ); @@ -183,7 +180,6 @@ TEST_CASE( "Sender::post() video frames" ) { REQUIRE( spkt.streamID == 0 ); REQUIRE( spkt.channel == Channel::Depth ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat | ftl::codecs::kFlagMappedDepth) ); REQUIRE( pkt.frame_count == 1 ); @@ -201,7 +197,7 @@ TEST_CASE( "Sender::post() video frames" ) { fs.frames[1].create<cv::cuda::GpuMat>(Channel::Depth).create(cv::Size(1280,720), CV_32F); fs.frames[1].get<cv::cuda::GpuMat>(Channel::Depth).setTo(cv::Scalar(0.0f)); - sender->set("lossless", true); + sender->set("codec", (int)codec_t::HEVC_LOSSLESS); sender->post(fs); REQUIRE( count == 1 ); @@ -211,7 +207,6 @@ TEST_CASE( "Sender::post() video frames" ) { REQUIRE( spkt.streamID == 0 ); REQUIRE( spkt.channel == Channel::Depth ); REQUIRE( pkt.codec == codec_t::HEVC_LOSSLESS ); - REQUIRE( pkt.definition == definition_t::HD720 ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( pkt.flags == (ftl::codecs::kFlagFloat) ); REQUIRE( pkt.frame_count == 2 ); @@ -237,7 +232,6 @@ TEST_CASE( "Sender::post() video frames" ) { REQUIRE( spkt.streamID == 0 ); REQUIRE( spkt.channel == Channel::Depth ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( pkt.frame_count == 1 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); @@ -300,7 +294,6 @@ TEST_CASE( "Sender request to control encoding" ) { REQUIRE( spkt.streamID == 0 ); REQUIRE( spkt.channel == Channel::Colour ); REQUIRE( pkt.codec == codec_t::HEVC ); - REQUIRE( pkt.definition == definition_t::HD720 ); REQUIRE( pkt.data.size() > 0 ); REQUIRE( pkt.frame_count == 1 ); REQUIRE( ftl::codecs::hevc::validNAL(pkt.data.data(), pkt.data.size()) ); diff --git a/lib/nvpipe/CMakeLists.txt b/lib/nvpipe/CMakeLists.txt deleted file mode 100644 index 426a00dc7fa44de4f299e79de0f6c97a33178bd0..0000000000000000000000000000000000000000 --- a/lib/nvpipe/CMakeLists.txt +++ /dev/null @@ -1,93 +0,0 @@ -# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# * Neither the name of NVIDIA CORPORATION nor the names of its -# contributors may be used to endorse or promote products derived -# from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY -# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - - -# Construct path to CUDA driver API lib (not provided by FindCUDA) -#get_filename_component(CUDA_LIB_DIR ${CUDA_cudart_static_LIBRARY} DIRECTORY) -#find_library(CUDA_LIB NAMES cuda HINTS ${CUDA_LIB_DIR}) - -# Options -option(NVPIPE_WITH_ENCODER "Enables the NvPipe encoding interface." ON) -option(NVPIPE_WITH_DECODER "Enables the NvPipe decoding interface." OFF) -option(NVPIPE_WITH_OPENGL "Enables the NvPipe OpenGL interface." OFF) -option(NVPIPE_BUILD_EXAMPLES "Builds the NvPipe example applications (requires both encoder and decoder)." OFF) - -# Header -configure_file(src/NvPipe.h.in include/NvPipe.h @ONLY) -#include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) - -# NvPipe shared library -list(APPEND NVPIPE_SOURCES - src/NvPipe.cu - src/Video_Codec_SDK_9.0.20/Samples/Utils/ColorSpace.cu - ) -list(APPEND NVPIPE_LIBRARIES - #${CUDA_LIB} - cuda - ) - -if (NVPIPE_WITH_ENCODER) - list(APPEND NVPIPE_SOURCES - src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoder.cpp - src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderCuda.cpp - ) -endif() - -#if (NVPIPE_WITH_DECODER) -# list(APPEND NVPIPE_SOURCES -# src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvDecoder/NvDecoder.cpp -# ) -# list(APPEND NVPIPE_LIBRARIES -# nvcuvid -# ) - - #if (WIN32) - # if (CMAKE_SIZEOF_VOID_P EQUAL 8) - # link_directories(src/Video_Codec_SDK_9.0.20/Lib/x64) - # elseif (CMAKE_SIZEOF_VOID_P EQUAL 4) - # link_directories(src/Video_Codec_SDK_9.0.20/Lib/Win32) - # endif() - #endif() -#endif() - -#set(CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION ON) - -add_library(NvPipe STATIC ${NVPIPE_SOURCES}) -#set_property(TARGET NvPipe PROPERTY CUDA_SEPARABLE_COMPILATION ON) -#set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON) -target_include_directories(NvPipe PUBLIC - $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include> - $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include> - ) -target_include_directories(NvPipe PRIVATE - $<BUILD_INTERFACE:src/Video_Codec_SDK_9.0.20/Samples src/Video_Codec_SDK_9.0.20/Samples/NvCodec src/Video_Codec_SDK_9.0.20/include ${CUDA_INCLUDE_DIRS}> - ) -target_link_libraries(NvPipe ${NVPIPE_LIBRARIES}) - -set_target_properties(NvPipe PROPERTIES - VERSION ${PROJECT_VERSION} - SOVERSION 1) diff --git a/lib/nvpipe/LICENSE b/lib/nvpipe/LICENSE deleted file mode 100644 index a8c9c37f440aebd523307d3a6b448e3e88e27f1c..0000000000000000000000000000000000000000 --- a/lib/nvpipe/LICENSE +++ /dev/null @@ -1,29 +0,0 @@ -Copyright (c) 2018 NVIDIA CORPORATION. All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of NVIDIA CORPORATION nor the names of its - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY -EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ------ - -The Video Codec SDK has its own license agreement that can be found in src/Video_Codec_SDK. \ No newline at end of file diff --git a/lib/nvpipe/README.md b/lib/nvpipe/README.md deleted file mode 100644 index 75a90ace74150af895ee6635c99bddb2779ac16d..0000000000000000000000000000000000000000 --- a/lib/nvpipe/README.md +++ /dev/null @@ -1,258 +0,0 @@ -Introduction -============ - -NvPipe is a simple and lightweight C API library for low-latency video compression. -It provides easy-to-use access to NVIDIA's hardware-accelerated H.264 and HEVC video codecs and is a great choice to drastically lower the bandwidth required for your -networked interactive server/client application. - -Designed for both remote rendering solutions and general compression of arbitrary image data, NvPipe accepts frames in various formats and supports access to host memory, CUDA device memory, OpenGL textures and OpenGL pixel buffer objects. - -Supported formats are 32 bit RGBA frames (8 bit per channel; alpha is not supported by the underlying video codecs and is ignored) and unsigned integer grayscale frames with 4 bit, 8 bit, 16 bit or 32 bit per pixel. - -Besides conventional lossy video compression based on target bitrate and framerate, also fully lossless compression is available enabling exact bit pattern reconstruction. - -Please note that NvPipe acts as a lightweight synchronous convenience layer around the [NVIDIA Video Codec SDK](https://developer.nvidia.com/nvidia-video-codec-sdk) and doesn't offer all high-performance capabilities. -If you're looking for ultimate encode/decode performance, you may want to consider using NvCodec directly. - - -Usage -============ - -The library is specifically designed to be easily integratable into existing -low-latency streaming applications. NvPipe does not take over any of the -network communication aspects, allowing your application to dictate the -server/client scenario it is used in. - -A sample encoding scenario: - -```c++ -#include <NvPipe.h> -... - -uint32_t width = ..., height = ...; // Image resolution -uint8_t* rgba = ...; // Image data in device or host memory -uint8_t* buffer = ...; // Buffer for compressed output in host memory - -// Create encoder -NvPipe* encoder = NvPipe_CreateEncoder(NVPIPE_RGBA32, NVPIPE_H264, NVPIPE_LOSSY, 32 * 1000 * 1000, 90, width, height); // 32 Mbps @ 90 Hz - -while (frameAvailable) -{ - // Encode next frame - uint64_t compressedSize = NvPipe_Encode(encoder, rgba, width * 4, buffer, bufferSize, width, height, false); - - // Send the frame size and compressed stream to the consuming side - send(socket, &compressedSize, sizeof(uint64_t), ...); - send(socket, buffer, compressedSize, ...); - send(socket, ...) // Other meta data -} - -// Destroy encode session -NvPipe_Destroy(encoder); -``` - -The corresponding decoding scenario: - -```c++ -#include <NvPipe.h> -... - -uint32_t width = ..., height = ...; // Image resolution -uint8_t* rgba = ...; // Image destination in device or host memory -uint8_t* buffer = ...; // Buffer for incoming packets - -// Create decoder -NvPipe* decoder = NvPipe_CreateDecoder(NVPIPE_RGBA32, NVPIPE_H264, width, height); - -while (frameAvailable) -{ - // Receive data - uint64_t compressedSize; - receive(socket, &compressedSize, sizeof(uint64_t), ...); - receive(socket, buffer, compressedSize, ...); - receive(socket, ...); - - // Decode frame - NvPipe_Decode(decoder, buffer, compressedSize, rgba, width, height); - - // Use frame (blit/save/...) - ... -} - -// Destroy decode session -NvPipe_Destroy(decoder); -``` - - - -Installation -============ -NvPipe requires a C++ compiler and uses the standard CMake compilation and installation procedure. -```bash -mkdir build && cd build -cmake PATH_TO_NVPIPE -make -make install -``` - -It is possible to compile the encoding or decoding interface only using the `NVPIPE_WITH_ENCODER` and `NVPIPE_WITH_DECODER` options (default: `ON`). - -The OpenGL interface is optional and can be disabled using the `NVPIPE_WITH_OPENGL` option (default: `ON`). - -The compilation of the included sample applications can be controlled via the `NVPIPE_BUILD_EXAMPLES` CMake option (default: `ON`). - -Only shared libraries are supported. - - -##### Compiling on Windows using Visual Studio 2017 ##### - -On Windows, NvPipe can be compiled using Visual Studio's built-in CMake support. - -Just place a `CMakeSettings.json` file with the following contents (adjust paths accordingly) next to NvPipe's `CMakeLists.txt`: - -``` -{ - "configurations": [ - { - "name": "x64-Release", - "generator": "Ninja", - "configurationType": "Release", - "inheritEnvironments": [ - "msvc_x64_x64" - ], - "buildRoot": "C:\\.build\\NvPipe\\${name}", - "installRoot": "C:\\.install\\NvPipe\\${name}", - "cmakeCommandArgs": "", - "buildCommandArgs": "-v", - "ctestCommandArgs": "", - "variables": [ - { - "name": "GLEW_INCLUDE_DIR", - "value": "C:\\PATH\\TO\\glew-2.1.0\\include" - }, - { - "name": "GLEW_LIBRARY_RELEASE", - "value": "C:\\PATH\\TO\\glew-2.1.0\\lib\\Release\\x64\\glew32.lib" - } - ] - } - ] -} -``` - - -Examples -===== - -Two example applications are included that perform encoding and decoding of a sequence of frames, measure performance, and dump image files before and after compression. -Additionally, an example of integer frame compression verifies the bitwise correctness of lossless encoding and decoding. - -The `memory` example compares the performance of input and output based on host memory vs. CUDA device memory. -As illustrated in the following example output, device memory can be directly accessed by the video codec hardware and is thus faster, whereas host memory entails additional bus transfers. -```bash -$ ./nvpExampleMemory -NvPipe example application: Comparison of using host/device memory. - -Resolution: 3840 x 2160 -Codec: H.264 -Bitrate: 32 Mbps @ 90 Hz -Resolution: 3840 x 2160 - ---- Encode from host memory / Decode to host memory --- -Frame | Encode (ms) | Decode (ms) | Size (KB) - 0 | 57.8 | 42.2 | 31.0 - 1 | 15.4 | 13.3 | 12.1 - 2 | 16.6 | 13.5 | 5.5 - 3 | 16.6 | 13.6 | 8.3 - 4 | 16.9 | 13.8 | 3.9 - 5 | 17.1 | 13.8 | 3.5 - 6 | 16.9 | 13.8 | 3.5 - 7 | 17.0 | 13.8 | 3.5 - 8 | 17.0 | 13.8 | 3.5 - 9 | 16.9 | 14.3 | 3.5 - ---- Encode from device memory / Decode to device memory --- -Frame | Encode (ms) | Decode (ms) | Size (KB) - 0 | 45.9 | 35.0 | 31.0 - 1 | 10.5 | 6.9 | 12.1 - 2 | 10.2 | 6.8 | 5.5 - 3 | 10.1 | 6.8 | 8.3 - 4 | 10.2 | 6.8 | 3.9 - 5 | 10.2 | 6.8 | 3.5 - 6 | 10.1 | 6.9 | 3.5 - 7 | 10.2 | 6.8 | 3.5 - 8 | 10.1 | 6.9 | 3.5 - 9 | 10.1 | 6.8 | 3.5 -``` - -As indicated by the size column, the first frame is an I-frame and thus requires more bandwidth. The subsequent frames however are more lightweight P-frames, which only describe differences to previous frames. - - -The `egl` example application demonstrates the usage of NvPipe in a server/client remote rendering scenario. An offscreen OpenGL framebuffer is created through EGL which is [ideally suited for remote rendering on headless nodes without X server](https://devblogs.nvidia.com/egl-eye-opengl-visualization-without-x-server/). The rendered frame is encoded by directly accessing the framebuffer's color attachment. After decoding, a fullscreen texture is used to draw the frame to the default framebuffer. -The following example output shows that performance is similar to CUDA device memory access as illustrated above. -```bash -$ ./nvpExampleEGL -NvPipe example application: Render to offscreen framebuffer using EGL, -encode framebuffer, decode to display texture. - -Resolution: 3840 x 2160 -Codec: H.264 -Bitrate: 32 Mbps @ 90 Hz - -Frame | Encode (ms) | Decode (ms) | Size (KB) - 0 | 54.4 | 36.4 | 38.5 - 1 | 11.8 | 7.5 | 29.4 - 2 | 11.9 | 7.4 | 28.5 - 3 | 11.8 | 7.5 | 28.0 - 4 | 11.9 | 7.4 | 27.8 - 5 | 11.9 | 7.5 | 27.8 - 6 | 11.8 | 7.5 | 27.7 - 7 | 11.9 | 7.5 | 27.4 - 8 | 11.9 | 7.4 | 27.4 - 9 | 11.8 | 7.5 | 27.6 -``` - -Note that the overall compressed sizes differ in the two examples as they use different images for input. - -The `lossless` example demonstrates the usage of NvPipe for lossless integer data compression. An 8 bit unsigned integer frame is created, compressed, and the result verified for bitwise correctness after decompression. -The following example output shows that while correctness is always guaranteed due to lossless compression, the chosen pixel format has crucial impact on the compressed output size. -```bash -$ ./nvpExampleLossless -NvPipe example application: Tests lossless compression of a grayscale integer frame. - -Input: 1024 x 1024 UINT8 (Raw size: 1048.6 KB) - - [as UINT4] Size: 181.3 KB, Encode: 14.8 ms, Decode: 17.1 ms - OK - - [as UINT8] Size: 45.2 KB, Encode: 14.8 ms, Decode: 15.5 ms - OK - - [as UINT16] Size: 57.7 KB, Encode: 16.4 ms, Decode: 11.4 ms - OK -``` - -The ideal pixel format is highly dependent on the structure of your input data. Keep in mind that video codecs are optimized for spatial and temporal coherence. For instance, the 8 bit pixel data in the example above interpreted as 4 bit pixels results in poor compression due to high frequency noise from the encoder's perspective. - - - -Supported Platforms -=================== - -NvPipe is supported on both Linux and Windows. OS X support is not plausible in the short term. - -Please refer to the hardware capability matrices of the [NVIDIA Video Codec SDK](https://developer.nvidia.com/nvidia-video-codec-sdk) for more details on feature availability and driver requirements. - -Note that NvPipe does not support the Jetson platform, on which the video hardware should be accessed through the [NvMedia API](https://docs.nvidia.com/drive/nvvib_docs/NVIDIA%20DRIVE%20Linux%20SDK%20Development%20Guide/baggage/group__nvmedia__top.html). - - - -Publication -=================== - -NvPipe was successfully used in the EGPGV 2018 best paper *Hardware-Accelerated Multi-Tile Streaming for Realtime Remote Visualization*, which is available [here](https://hpcvis.org/publications/Streaming-EGPGV2018.pdf). - - - -Feedback -=================== - -Feedback and pull requests welcome! After starting a PR, remember to sign the CLA. - -We would love to hear more about your use cases of NvPipe! -If you have any questions, feature requests, issues, or ideas for improvement, please feel free to reach out at [tbiedert@nvidia.com](mailto:tbiedert@nvidia.com). -The more I know about your use cases of NvPipe, the better I can justify spending time on your project! diff --git a/lib/nvpipe/examples/cmake/FindEGL.cmake b/lib/nvpipe/examples/cmake/FindEGL.cmake deleted file mode 100644 index ca295e3ffbfbf721a3626a2cdd8e5f04bd93ced2..0000000000000000000000000000000000000000 --- a/lib/nvpipe/examples/cmake/FindEGL.cmake +++ /dev/null @@ -1,74 +0,0 @@ -# -# Source: VTK (https://github.com/Kitware/VTK/blob/master/CMake/FindEGL.cmake) -# Copied and adapted 07/17/2017 (GLdispatch is not needed anymore). -# -# This is a temporary solution. -# - -# Try to find EGL library and include dir. -# Once done this will define -# -# EGL_FOUND - true if EGL has been found -# EGL_INCLUDE_DIR - where the EGL/egl.h and KHR/khrplatform.h can be found -# EGL_LIBRARY - link this to use libEGL.so.1 -# EGL_opengl_LIBRARY - link with these two libraries instead of the gl library -# EGL_LIBRARIES - all EGL related libraries: EGL, OpenGL - - -if(NOT EGL_INCLUDE_DIR) - - # If we have a root defined look there first - if(EGL_ROOT) - find_path(EGL_INCLUDE_DIR EGL/egl.h PATHS ${EGL_ROOT}/include - NO_DEFAULT_PATH - ) - endif() - - if(NOT EGL_INCLUDE_DIR) - find_path(EGL_INCLUDE_DIR EGL/egl.h PATHS - /usr/local/include - /usr/include - ) - endif() -endif() - -if(NOT EGL_LIBRARY) - # If we have a root defined look there first - if(EGL_ROOT) - find_library(EGL_LIBRARY EGL PATHS ${EGL_ROOT}/lib - NO_DEFAULT_PATH - ) - endif() - - if(NOT EGL_LIBRARY) - find_library(EGL_LIBRARY EGL PATHS - /usr/local/lib - /usr/lib - ) - endif() -endif() - -if(NOT EGL_opengl_LIBRARY) - # If we have a root defined look there first - if(EGL_ROOT) - find_library(EGL_opengl_LIBRARY OpenGL PATHS ${EGL_ROOT}/lib - NO_DEFAULT_PATH - ) - endif() - - if(NOT EGL_opengl_LIBRARY) - find_library(EGL_opengl_LIBRARY OpenGL PATHS - /usr/local/lib - /usr/lib - ) - endif() -endif() - - -set(EGL_LIBRARIES ${EGL_LIBRARY} ${EGL_opengl_LIBRARY}) - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(EGL DEFAULT_MSG - EGL_LIBRARY EGL_opengl_LIBRARY EGL_INCLUDE_DIR) - -mark_as_advanced(EGL_DIR EGL_INCLUDE_DIR EGL_LIBRARY EGL_opengl_LIBRARY) diff --git a/lib/nvpipe/examples/concurrent.cpp b/lib/nvpipe/examples/concurrent.cpp deleted file mode 100644 index 6fcb081cdd95dfeb71953c112790a1ab8a9fe630..0000000000000000000000000000000000000000 --- a/lib/nvpipe/examples/concurrent.cpp +++ /dev/null @@ -1,161 +0,0 @@ -/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NVIDIA CORPORATION nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - - -#include <thread> -#include <mutex> -#include <string> -#include <iostream> -#include <vector> - -#include <cuda_runtime_api.h> - -#include <NvPipe.h> - -#include "utils.h" - - -void run(void* rgba, uint32_t width, uint32_t height, uint32_t index, std::mutex* printMutex) -{ - const NvPipe_Codec codec = NVPIPE_H264; - const NvPipe_Compression compression = NVPIPE_LOSSY; - const float bitrateMbps = 4; - const uint32_t targetFPS = 30; - const uint32_t numFrames = 500; - - std::vector<uint8_t> compressed(width * height * 4); - - void* decompressedDevice; - cudaMalloc(&decompressedDevice, width * height * 4); - - // Create encoder - NvPipe* encoder = NvPipe_CreateEncoder(NVPIPE_RGBA32, codec, compression, bitrateMbps * 1000 * 1000, targetFPS, width, height); - if (!encoder) - { - std::lock_guard<std::mutex> lock(*printMutex); - std::cerr << "[" << index << "] Failed to create encoder: " << NvPipe_GetError(NULL) << std::endl; - return; - } - - // Create decoder - NvPipe* decoder = NvPipe_CreateDecoder(NVPIPE_RGBA32, codec, width, height); - if (!decoder) - { - std::lock_guard<std::mutex> lock(*printMutex); - std::cerr << "[" << index << "] Failed to create decoder: " << NvPipe_GetError(NULL) << std::endl; - return; - } - - Timer timer; - double encodeMs = 0.0; - double decodeMs = 0.0; - - for (uint32_t i = 0; i < numFrames; ++i) - { - // Encode - timer.reset(); - uint64_t size = NvPipe_Encode(encoder, rgba, width * 4, compressed.data(), compressed.size(), width, height, false); - encodeMs += timer.getElapsedMilliseconds(); - - if (0 == size) - { - std::lock_guard<std::mutex> lock(*printMutex); - std::cerr << "[" << index << "] Encode error: " << NvPipe_GetError(encoder) << std::endl; - return; - } - - // Decode - timer.reset(); - uint64_t r = NvPipe_Decode(decoder, compressed.data(), size, decompressedDevice, width, height); - decodeMs += timer.getElapsedMilliseconds(); - - if (0 == r) - { - std::lock_guard<std::mutex> lock(*printMutex); - std::cerr << "[" << index << "] Decode error: " << NvPipe_GetError(decoder) << std::endl; - return; - } - } - - encodeMs /= numFrames; - decodeMs /= numFrames; - - // Clean up - NvPipe_Destroy(encoder); - NvPipe_Destroy(decoder); - - cudaFree(decompressedDevice); - - // Print results - std::lock_guard<std::mutex> lock(*printMutex); - std::cout << std::fixed << std::setprecision(1) << "[" << index << "] Average encode: " << encodeMs << " ms / Average decode: " << decodeMs << " ms" << std::endl; -} - - -int main(int argc, char* argv[]) -{ - std::cout << "NvPipe example application: Concurrent encode/decode sessions." << std::endl; - - const uint32_t width = 1920; - const uint32_t height = 1080; - - const uint32_t numWorkersMax = 4; - - - // Allocate dummy frame - std::vector<uint8_t> rgba(width * height * 4); - for (uint32_t y = 0; y < height; ++y) - for (uint32_t x = 0; x < width; ++x) - rgba[4 * (y * width + x) + 0] = (255.0f * x * y) / (width * height) * (y % 100 < 50); - - // Allocate device memory and copy input - void* rgbaDevice; - cudaMalloc(&rgbaDevice, rgba.size()); - cudaMemcpy(rgbaDevice, rgba.data(), rgba.size(), cudaMemcpyHostToDevice); - - // Multiple runs with increasing worker count - for (uint32_t numWorkers = 1; numWorkers <= numWorkersMax; ++numWorkers) - { - std::cout << std::endl; - std::cout << "--- " << numWorkers << " threads ---" << std::endl; - - std::mutex printMutex; - std::vector<std::thread> workers; - - for (uint32_t i = 0; i < numWorkers; ++i) - workers.push_back(std::thread(run, rgbaDevice, width, height, i, &printMutex)); - - for (std::thread& t : workers) - if (t.joinable()) - t.join(); - } - - // Clean up - cudaFree(rgbaDevice); - - return 0; -} diff --git a/lib/nvpipe/examples/egl.cpp b/lib/nvpipe/examples/egl.cpp deleted file mode 100644 index 530dc9e564fb3ac59e9e891ddbce2a5aba9fcdea..0000000000000000000000000000000000000000 --- a/lib/nvpipe/examples/egl.cpp +++ /dev/null @@ -1,273 +0,0 @@ -/* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NVIDIA CORPORATION nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include <NvPipe.h> - -#include "utils.h" - -#include <iostream> -#include <vector> - -#include <EGL/egl.h> -#include <GL/glew.h> - - -void captureFramebufferPPM(GLuint framebuffer, uint32_t width, uint32_t height, const std::string& path) -{ - std::vector<uint8_t> rgba(width * height * 4); - - glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer); - glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, rgba.data()); - - savePPM(rgba.data(), width, height, path); -} - - -int main(int argc, char* argv[]) -{ - std::cout << "NvPipe example application: Render to offscreen framebuffer using EGL," << std::endl << "encode framebuffer, decode to display texture." << std::endl << std::endl; - - const uint32_t width = 3840; - const uint32_t height = 2160; - - const NvPipe_Codec codec = NVPIPE_H264; - const float bitrateMbps = 32; - const uint32_t targetFPS = 90; - - - std::cout << "Resolution: " << width << " x " << height << std::endl; - std::cout << "Codec: " << (codec == NVPIPE_H264 ? "H.264" : "HEVC") << std::endl; - std::cout << "Bitrate: " << bitrateMbps << " Mbps @ " << targetFPS << " Hz" << std::endl; - - - - // Init EGL/OpenGL - EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); - - EGLint major, minor; - eglInitialize(display, &major, &minor); - - const EGLint configAttribs[] = { - EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, - EGL_BLUE_SIZE, 8, - EGL_GREEN_SIZE, 8, - EGL_RED_SIZE, 8, - EGL_DEPTH_SIZE, 8, - EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, - EGL_NONE - }; - - EGLint numConfigs; - EGLConfig config; - eglChooseConfig(display, configAttribs, &config, 1, &numConfigs); - - const EGLint pbufferAttribs[] = { - EGL_WIDTH, width, - EGL_HEIGHT, height, - EGL_NONE, - }; - - EGLSurface surface = eglCreatePbufferSurface(display, config, pbufferAttribs); - - eglBindAPI(EGL_OPENGL_API); - EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL); - eglMakeCurrent(display, surface, surface, context); - - glewInit(); - - - // Create framebuffer for offscreen rendering - GLuint serverColorTex; - glGenTextures(1, &serverColorTex); - glBindTexture(GL_TEXTURE_2D, serverColorTex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); - - GLuint serverDepthTex; - glGenTextures(1, &serverDepthTex); - glBindTexture(GL_TEXTURE_2D, serverDepthTex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); - - GLuint serverFBO; - glGenFramebuffers(1, &serverFBO); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, serverFBO); - glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, serverColorTex, 0); - glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, serverDepthTex, 0); - - if (glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - { - std::cerr << "Failed to create framebuffer" << std::endl; - return 1; - } - - - // Create shader and texture for fullscreen display of decompressed frame - const GLchar* clientVertexShader = - "#version 330\n" - "void main() {}"; - - const GLchar* clientGeometryShader = - "#version 330 core\n" - "layout(points) in;" - "layout(triangle_strip, max_vertices = 4) out;" - "out vec2 texcoord;" - "void main() {" - "gl_Position = vec4( 1.0, 1.0, 0.0, 1.0 ); texcoord = vec2( 1.0, 1.0 ); EmitVertex();" - "gl_Position = vec4(-1.0, 1.0, 0.0, 1.0 ); texcoord = vec2( 0.0, 1.0 ); EmitVertex();" - "gl_Position = vec4( 1.0,-1.0, 0.0, 1.0 ); texcoord = vec2( 1.0, 0.0 ); EmitVertex();" - "gl_Position = vec4(-1.0,-1.0, 0.0, 1.0 ); texcoord = vec2( 0.0, 0.0 ); EmitVertex();" - "EndPrimitive();" - "}"; - - const GLchar* clientFragmentShader = - "#version 330\n" - "uniform sampler2D tex;" - "in vec2 texcoord;" - "out vec4 color;" - "void main() {" - " color = texture(tex, texcoord);" - "}"; - - GLuint clientVertexShaderHandle = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(clientVertexShaderHandle, 1, &clientVertexShader, 0); - glCompileShader(clientVertexShaderHandle); - - GLuint clientGeometryShaderHandle = glCreateShader(GL_GEOMETRY_SHADER); - glShaderSource(clientGeometryShaderHandle, 1, &clientGeometryShader, 0); - glCompileShader(clientGeometryShaderHandle); - - GLuint clientFragmentShaderHandle = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(clientFragmentShaderHandle, 1, &clientFragmentShader, 0); - glCompileShader(clientFragmentShaderHandle); - - GLuint clientFullscreenQuadProgram = glCreateProgram(); - glAttachShader(clientFullscreenQuadProgram, clientVertexShaderHandle); - glAttachShader(clientFullscreenQuadProgram, clientGeometryShaderHandle); - glAttachShader(clientFullscreenQuadProgram, clientFragmentShaderHandle); - glLinkProgram(clientFullscreenQuadProgram); - - GLuint clientFullscreenTextureLocation = glGetUniformLocation(clientFullscreenQuadProgram, "tex"); - - GLuint clientFullscreenVAO; - glGenVertexArrays(1, &clientFullscreenVAO); - - GLuint clientColorTex; - glGenTextures(1, &clientColorTex); - glBindTexture(GL_TEXTURE_2D, clientColorTex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); // must use RGBA(8) here for CUDA-GL interop - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - - // Create encoder - NvPipe* encoder = NvPipe_CreateEncoder(NVPIPE_RGBA32, codec, NVPIPE_LOSSY, bitrateMbps * 1000 * 1000, targetFPS, width, height); - if (!encoder) - std::cerr << "Failed to create encoder: " << NvPipe_GetError(NULL) << std::endl; - - // Create decoder - NvPipe* decoder = NvPipe_CreateDecoder(NVPIPE_RGBA32, codec, width, height); - if (!decoder) - std::cerr << "Failed to create decoder: " << NvPipe_GetError(NULL) << std::endl; - - - Timer timer; - std::cout << std::endl << "Frame | Encode (ms) | Decode (ms) | Size (KB)" << std::endl; - - for (uint32_t i = 0; i < 10; ++i) - { - // Render dummy scene (Nothing to see here; just some oldschool immediate mode.. urgh) - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, serverFBO); - glUseProgram(0); - - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glViewport(0, 0, width, height); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - glRotatef((float) i, 0.0f, 0.0f, 1.0f); - - glBegin(GL_TRIANGLES); - glColor3f(1.0f, 0.0f, 0.0f); - glVertex3f(0.0f, -0.9f, 0.0f); - glColor3f(0.0f, 1.0f, 0.0f); - glVertex3f(-0.9f,0.9f, 0.0f); - glColor3f(0.0f, 0.0f, 1.0f); - glVertex3f(0.9f, 0.9f, 0.0f); - glEnd(); - - glFinish(); // Make sure rendering is complete before grabbing frame - - captureFramebufferPPM(serverFBO, width, height, "egl-input" + std::to_string(i) + ".ppm"); - - // Encode - std::vector<uint8_t> compressed(width * height * 4); - - timer.reset(); - uint64_t size = NvPipe_EncodeTexture(encoder, serverColorTex, GL_TEXTURE_2D, compressed.data(), compressed.size(), width, height, false); - double encodeMs = timer.getElapsedMilliseconds(); - - if (0 == size) - std::cerr << "Encode error: " << NvPipe_GetError(encoder) << std::endl; - - - // Decode - timer.reset(); - uint64_t r = NvPipe_DecodeTexture(decoder, compressed.data(), size, clientColorTex, GL_TEXTURE_2D, width, height); - double decodeMs = timer.getElapsedMilliseconds(); - - if (0 == r) - std::cerr << "Decode error: " << NvPipe_GetError(decoder) << std::endl; - - double sizeKB = size / 1000.0; - std::cout << std::fixed << std::setprecision(1) << std::setw(5) << i << " | " << std::setw(11) << encodeMs << " | " << std::setw(11) << decodeMs << " | " << std::setw(8) << sizeKB << std::endl; - - - - // Display decoded frame - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - glUseProgram(clientFullscreenQuadProgram); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, clientColorTex); - glUniform1i(clientFullscreenTextureLocation, 0); - glBindVertexArray(clientFullscreenVAO); - glDrawArrays(GL_POINTS, 0, 1); - - captureFramebufferPPM(0, width, height, "egl-output" + std::to_string(i) + ".ppm"); - } - - // Clean up - NvPipe_Destroy(encoder); - NvPipe_Destroy(decoder); - - eglTerminate(display); - - return 0; -} diff --git a/lib/nvpipe/examples/file.cpp b/lib/nvpipe/examples/file.cpp deleted file mode 100644 index 76fafcf7648e585a9d802a223adacd6fbd11912d..0000000000000000000000000000000000000000 --- a/lib/nvpipe/examples/file.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NVIDIA CORPORATION nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include <NvPipe.h> - -#include "utils.h" - -#include <iostream> -#include <vector> -#include <fstream> - -int main(int argc, char* argv[]) -{ - std::cout << "NvPipe example application: Encodes to a file / decodes from a file." << std::endl << "Useful for testing if only encoding or decoding is enabled." << std::endl << std::endl; - - const uint32_t width = 3840; - const uint32_t height = 2160; - - const NvPipe_Codec codec = NVPIPE_H264; - const float bitrateMbps = 32; - const uint32_t targetFPS = 90; - - - std::cout << "Resolution: " << width << " x " << height << std::endl; - std::cout << "Codec: " << (codec == NVPIPE_H264 ? "H.264" : "HEVC") << std::endl; - std::cout << "Bitrate: " << bitrateMbps << " Mbps @ " << targetFPS << " Hz" << std::endl; - - std::cout << "Resolution: " << width << " x " << height << std::endl; - - Timer timer; - - std::vector<uint8_t> rgba(width * height * 4); - std::vector<uint8_t> compressed(rgba.size()); - - - // Encoding -#ifdef NVPIPE_WITH_ENCODER - // Construct dummy frame - for (uint32_t y = 0; y < height; ++y) - for (uint32_t x = 0; x < width; ++x) - rgba[4 * (y * width + x) + 1] = (255.0f * x* y) / (width * height) * (y % 100 < 50); - - savePPM(rgba.data(), width, height, "file-input.ppm"); - - - NvPipe* encoder = NvPipe_CreateEncoder(NVPIPE_RGBA32, codec, NVPIPE_LOSSY, bitrateMbps * 1000 * 1000, targetFPS, width, height); - if (!encoder) - std::cerr << "Failed to create encoder: " << NvPipe_GetError(NULL) << std::endl; - - std::ofstream out("stream.bin", std::ios::out | std::ios::binary); - - std::cout << std::endl << "Encoding..." << std::endl; - - for (uint32_t i = 0; i < 10; ++i) - { - timer.reset(); - uint64_t size = NvPipe_Encode(encoder, rgba.data(), width * 4, compressed.data(), compressed.size(), width, height, false); - double encodeMs = timer.getElapsedMilliseconds(); - - if (0 == size) - std::cerr << "Encode error: " << NvPipe_GetError(encoder) << std::endl; - - out.write((char*) &size, sizeof(uint64_t)); - out.write((char*) compressed.data(), size); - - std::cout << i << ": " << encodeMs << " ms" << std::endl; - } - - out.close(); - - NvPipe_Destroy(encoder); -#endif - - - // Decoding -#ifdef NVPIPE_WITH_DECODER - NvPipe* decoder = NvPipe_CreateDecoder(NVPIPE_RGBA32, codec, width, height); - if (!decoder) - std::cerr << "Failed to create decoder: " << NvPipe_GetError(NULL) << std::endl; - - std::ifstream in("stream.bin", std::ios::in | std::ios::binary); - if (!in) - { - std::cerr << std::endl; - std::cerr << "Error: Failed to open input file \"stream.bin\"." << std::endl; - std::cerr << "The file can be created using this example with" << std::endl; - std::cerr << "NvPipe encoding enabled." << std::endl; - return 1; - } - - std::cout << std::endl << "Decoding..." << std::endl; - - for (uint32_t i = 0; i < 10; ++i) - { - uint64_t size; - in.read((char*) &size, sizeof(uint64_t)); - in.read((char*) compressed.data(), size); - - - // Decode - timer.reset(); - uint64_t r = NvPipe_Decode(decoder, compressed.data(), size, rgba.data(), width, height); - double decodeMs = timer.getElapsedMilliseconds(); - - if (r == size) - std::cerr << "Decode error: " << NvPipe_GetError(decoder) << std::endl; - - std::cout << i << ": " << decodeMs << " ms" << std::endl; - - if (i == 0) - savePPM(rgba.data(), width, height, "file-output.ppm"); - } - - in.close(); - - NvPipe_Destroy(decoder); -#endif - - return 0; -} diff --git a/lib/nvpipe/examples/lossless.cpp b/lib/nvpipe/examples/lossless.cpp deleted file mode 100644 index 4d089ef9deea8c8e16bb78ff07e883426f7b792b..0000000000000000000000000000000000000000 --- a/lib/nvpipe/examples/lossless.cpp +++ /dev/null @@ -1,194 +0,0 @@ -/* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NVIDIA CORPORATION nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include <NvPipe.h> - -#include <iostream> -#include <iomanip> -#include <vector> - -#include "utils.h" - - - - -void test(const uint8_t* data, NvPipe_Format format, uint32_t width, uint32_t height) -{ - uint64_t dataSize = width * height; - uint64_t dataPitch = width; - if (format == NVPIPE_UINT4) - { - dataSize /= 2; - dataPitch /= 2; - } - else if (format == NVPIPE_UINT16) - { - dataSize *= 2; - dataPitch *= 2; - } - else if (format == NVPIPE_UINT32) - { - dataSize *= 4; - dataPitch *= 4; - } - - - Timer timer; - - // Encode - NvPipe* encoder = NvPipe_CreateEncoder(format, NVPIPE_H264, NVPIPE_LOSSLESS, 0, 0, width, height); - if (!encoder) - { - std::cerr << "Failed to create encoder: " << NvPipe_GetError(NULL) << std::endl; - return; - } - - std::vector<uint8_t> buffer(dataSize * 2); - timer.reset(); - uint64_t size = NvPipe_Encode(encoder, data, dataPitch, buffer.data(), buffer.size(), width, height, false); - double encodeMs = timer.getElapsedMilliseconds(); - if (0 == size) - { - std::cerr << "Encode error: " << NvPipe_GetError(encoder) << std::endl; - return; - } - - NvPipe_Destroy(encoder); - - // Decode - NvPipe* decoder = NvPipe_CreateDecoder(format, NVPIPE_H264, width, height); - if (!decoder) - { - std::cerr << "Failed to create decoder: " << NvPipe_GetError(NULL) << std::endl; - return; - } - - std::vector<uint8_t> result(dataSize); - timer.reset(); - uint64_t r = NvPipe_Decode(decoder, buffer.data(), size, result.data(), width, height); - double decodeMs = timer.getElapsedMilliseconds(); - if (0 == r) - { - std::cerr << "Decode error: " << NvPipe_GetError(decoder) << std::endl; - return; - } - - NvPipe_Destroy(decoder); - - - // Check - auto bitpattern = [](uint8_t v) -> std::string - { - std::string s; - - for (uint32_t i = 1; i <= 128; i *= 2) - { - if (v & i) - s = "1" + s; - else - s = "0" + s; - - if (i == 8) - s = " " + s; - } - - return s; - }; - - bool ok = true; - std::string mismatch; - - for (uint32_t i = 0; i < dataSize; ++i) - { - if (data[i] != result[i]) - { - mismatch = "Byte " + std::to_string(i) + ": " + bitpattern(data[i]) + " -> " + bitpattern(result[i]); - ok = false; - break; - } - } - - // Print result - if (format == NVPIPE_UINT4) - std::cout << " - [as UINT4] "; - else if (format == NVPIPE_UINT8) - std::cout << " - [as UINT8] "; - else if (format == NVPIPE_UINT16) - std::cout << " - [as UINT16] "; - else if (format == NVPIPE_UINT32) - std::cout << " - [as UINT32] "; - - std::cout << std::fixed << std::setprecision(1) << " Size: " << size * 0.001 << " KB, Encode: " << encodeMs << " ms, Decode: " << decodeMs << " ms - "; - - if (ok) - std::cout << "OK" << std::endl; - else - std::cout << "MISMATCH [" << mismatch << "]" << std::endl; -} - - -int main(int argc, char* argv[]) -{ - std::cout << "NvPipe example application: Tests lossless compression of a grayscale integer frame." << std::endl << std::endl; - - uint32_t width = 1024; - uint32_t height = 1024; - - // UINT 8 test - { - std::vector<uint8_t> image(width * height); - for (uint32_t y = 0; y < height; ++y) - for (uint32_t x = 0; x < width; ++x) - image[y * width + x] = (255.0f * x * y) / (width * height) * (y % 100 < 50); - - std::cout << std::fixed << std::setprecision(1) << "Input: " << width << " x " << height << " UINT8 (Raw size: " << (width * height) * 0.001 << " KB)" << std::endl; - test(image.data(), NVPIPE_UINT4, width * 2, height); - test(image.data(), NVPIPE_UINT8, width, height); - test(image.data(), NVPIPE_UINT16, width / 2, height); - test(image.data(), NVPIPE_UINT32, width / 4, height); - } - - std::cout << std::endl; - - - // UINT32 test - { - std::vector<uint32_t> image(width * height); - for (uint32_t y = 0; y < height; ++y) - for (uint32_t x = 0; x < width; ++x) - image[y * width + x] = (4294967295.0f * x * y) / (width * height) * (y % 100 < 50); - - std::cout << std::fixed << std::setprecision(1) << "Input: " << width << " x " << height << " UINT32 (Raw size: " << (width * height * 4) * 0.001 << " KB)" << std::endl; -// test((uint8_t*) image.data(), NVPIPE_UINT4, width * 8, height); - test((uint8_t*) image.data(), NVPIPE_UINT8, width * 4, height); - test((uint8_t*) image.data(), NVPIPE_UINT16, width * 2, height); - test((uint8_t*) image.data(), NVPIPE_UINT32, width, height); - } - - - return 0; -} diff --git a/lib/nvpipe/examples/memory.cpp b/lib/nvpipe/examples/memory.cpp deleted file mode 100644 index 8d0b654ea0decf0fa05f0e1859855b2b038ab0a6..0000000000000000000000000000000000000000 --- a/lib/nvpipe/examples/memory.cpp +++ /dev/null @@ -1,174 +0,0 @@ -/* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NVIDIA CORPORATION nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include <NvPipe.h> - -#include "utils.h" - -#include <iostream> -#include <vector> - -#include <cuda_runtime_api.h> - - -int main(int argc, char* argv[]) -{ - std::cout << "NvPipe example application: Comparison of using host/device memory." << std::endl << std::endl; - - const uint32_t width = 3840; - const uint32_t height = 2160; - - const NvPipe_Codec codec = NVPIPE_H264; - const NvPipe_Compression compression = NVPIPE_LOSSY; - const float bitrateMbps = 32; - const uint32_t targetFPS = 90; - - - std::cout << "Resolution: " << width << " x " << height << std::endl; - std::cout << "Codec: " << (codec == NVPIPE_H264 ? "H.264" : "HEVC") << std::endl; - if (compression == NVPIPE_LOSSY) - std::cout << "Bitrate: " << bitrateMbps << " Mbps @ " << targetFPS << " Hz" << std::endl; - - - // Construct dummy frame - std::vector<uint8_t> rgba(width * height * 4); - for (uint32_t y = 0; y < height; ++y) - for (uint32_t x = 0; x < width; ++x) - rgba[4 * (y * width + x) + 0] = (255.0f * x * y) / (width * height) * (y % 100 < 50); - - savePPM(rgba.data(), width, height, "memory-input.ppm"); - - std::cout << "Resolution: " << width << " x " << height << std::endl; - - - std::vector<uint8_t> compressed(rgba.size()); - std::vector<uint8_t> decompressed(rgba.size()); - - Timer timer; - - - // Host memory benchmark - { - std::cout << std::endl << "--- Encode from host memory / Decode to host memory ---" << std::endl; - std::cout << "Frame | Encode (ms) | Decode (ms) | Size (KB)" << std::endl; - - // Create encoder - NvPipe* encoder = NvPipe_CreateEncoder(NVPIPE_RGBA32, codec, compression, bitrateMbps * 1000 * 1000, targetFPS, width, height); - if (!encoder) - std::cerr << "Failed to create encoder: " << NvPipe_GetError(NULL) << std::endl; - - // Create decoder - NvPipe* decoder = NvPipe_CreateDecoder(NVPIPE_RGBA32, codec, width, height); - if (!decoder) - std::cerr << "Failed to create decoder: " << NvPipe_GetError(NULL) << std::endl; - - // A few frames ... - for (uint32_t i = 0; i < 10; ++i) - { - // Encode - timer.reset(); - uint64_t size = NvPipe_Encode(encoder, rgba.data(), width * 4, compressed.data(), compressed.size(), width, height, false); - double encodeMs = timer.getElapsedMilliseconds(); - - if (0 == size) - std::cerr << "Encode error: " << NvPipe_GetError(encoder) << std::endl; - - // Decode - timer.reset(); - uint64_t r = NvPipe_Decode(decoder, compressed.data(), size, decompressed.data(), width, height); - double decodeMs = timer.getElapsedMilliseconds(); - - if (0 == r) - std::cerr << "Decode error: " << NvPipe_GetError(decoder) << std::endl; - - double sizeKB = size / 1000.0; - std::cout << std::fixed << std::setprecision(1) << std::setw(5) << i << " | " << std::setw(11) << encodeMs << " | " << std::setw(11) << decodeMs << " | " << std::setw(8) << sizeKB << std::endl; - - if (i == 9) - savePPM(decompressed.data(), width, height, "memory-output.ppm"); - } - - // Clean up - NvPipe_Destroy(encoder); - NvPipe_Destroy(decoder); - } - - // Device memory benchmark - { - std::cout << std::endl << "--- Encode from device memory / Decode to device memory ---" << std::endl; - std::cout << "Frame | Encode (ms) | Decode (ms) | Size (KB)" << std::endl; - - // Create encoder - NvPipe* encoder = NvPipe_CreateEncoder(NVPIPE_RGBA32, codec, compression, bitrateMbps * 1000 * 1000, targetFPS, width, height); - if (!encoder) - std::cerr << "Failed to create encoder: " << NvPipe_GetError(NULL) << std::endl; - - // Create decoder - NvPipe* decoder = NvPipe_CreateDecoder(NVPIPE_RGBA32, codec, width, height); - if (!decoder) - std::cerr << "Failed to create decoder: " << NvPipe_GetError(NULL) << std::endl; - - // Allocate device memory and copy input - void* rgbaDevice; - cudaMalloc(&rgbaDevice, rgba.size()); - cudaMemcpy(rgbaDevice, rgba.data(), rgba.size(), cudaMemcpyHostToDevice); - - void* decompressedDevice; - cudaMalloc(&decompressedDevice, rgba.size()); - - for (uint32_t i = 0; i < 10; ++i) - { - // Encode - timer.reset(); - uint64_t size = NvPipe_Encode(encoder, rgbaDevice, width * 4, compressed.data(), compressed.size(), width, height, false); - double encodeMs = timer.getElapsedMilliseconds(); - - if (0 == size) - std::cerr << "Encode error: " << NvPipe_GetError(encoder) << std::endl; - - // Decode - timer.reset(); - uint64_t r = NvPipe_Decode(decoder, compressed.data(), size, decompressedDevice, width, height); - double decodeMs = timer.getElapsedMilliseconds(); - - if (0 == r) - std::cerr << "Decode error: " << NvPipe_GetError(decoder) << std::endl; - - double sizeKB = size / 1000.0; - std::cout << std::fixed << std::setprecision(1) << std::setw(5) << i << " | " << std::setw(11) << encodeMs << " | " << std::setw(11) << decodeMs << " | " << std::setw(8) << sizeKB << std::endl; - } - - cudaFree(rgbaDevice); - cudaFree(decompressedDevice); - - // Clean up - NvPipe_Destroy(encoder); - NvPipe_Destroy(decoder); - } - - return 0; -} diff --git a/lib/nvpipe/examples/utils.h b/lib/nvpipe/examples/utils.h deleted file mode 100644 index 0d1eb2f9fa62c108af29dc1cc07c0dbb949f139c..0000000000000000000000000000000000000000 --- a/lib/nvpipe/examples/utils.h +++ /dev/null @@ -1,86 +0,0 @@ -/* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NVIDIA CORPORATION nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include <string> -#include <fstream> -#include <vector> -#include <chrono> -#include <iostream> -#include <iomanip> - - -void savePPM(uint8_t* rgba, uint32_t width, uint32_t height, const std::string& path) -{ - // Convert to RGB - std::vector<uint8_t> rgb(width * height * 3); - for (uint32_t i = 0; i < width * height; ++i) - for (uint32_t j = 0; j < 3; ++j) - rgb[3 * i + j] = rgba[4 * i + j]; - - // Write PPM - std::ofstream outFile; - outFile.open(path.c_str(), std::ios::binary); - - outFile << "P6" << "\n" - << width << " " << height << "\n" - << "255\n"; - - outFile.write((char*) rgb.data(), rgb.size()); -} - - -class Timer -{ -public: - Timer() - { - this->reset(); - } - - void reset() - { - this->time = std::chrono::high_resolution_clock::now(); - } - - double getElapsedSeconds() const - { - return 1.0e-6 * std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - this->time).count(); - } - - double getElapsedMilliseconds() const - { - return 1.0e-3 * std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - this->time).count(); - } - -private: - std::chrono::high_resolution_clock::time_point time; -}; - - - diff --git a/lib/nvpipe/src/NvPipe.cu b/lib/nvpipe/src/NvPipe.cu deleted file mode 100644 index 37cdf31861ce4ad290000c5e6441e712f13ce95c..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/NvPipe.cu +++ /dev/null @@ -1,1316 +0,0 @@ -/* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NVIDIA CORPORATION nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "NvPipe.h" - -#ifdef NVPIPE_WITH_ENCODER -#include "NvCodec/NvEncoder/NvEncoderCuda.h" -#endif - -#ifdef NVPIPE_WITH_DECODER -#include "NvCodec/NvDecoder/NvDecoder.h" -#endif - -#include "Utils/ColorSpace.h" -#include "Utils/NvCodecUtils.h" - -#include <memory> -#include <iostream> -#include <string> -#include <sstream> -#include <unordered_map> -#include <mutex> - -#include <cuda.h> -#include <cuda_runtime_api.h> - -#ifdef NVPIPE_WITH_OPENGL -#include <cuda_gl_interop.h> -#endif - - -class Exception -{ -public: - Exception(const std::string& msg) : message(msg) {} - std::string getErrorString() const { return message; } -public: - std::string message; -}; - - -inline void CUDA_THROW(cudaError_t code, const std::string& errorMessage) -{ - if (cudaSuccess != code) { - throw Exception(errorMessage + " (Error " + std::to_string(code) + ": " + std::string(cudaGetErrorString(code)) + ")"); - } -} - -inline bool isDevicePointer(const void* ptr) -{ - struct cudaPointerAttributes attr; - const cudaError_t perr = cudaPointerGetAttributes(&attr, ptr); - -#if (CUDA_VERSION >= 10000) - return (perr == cudaSuccess) && (attr.type != cudaMemoryTypeHost); -#else - return (perr == cudaSuccess) && (attr.memoryType != cudaMemoryTypeHost); -#endif -} - -inline uint64_t getFrameSize(NvPipe_Format format, uint32_t width, uint32_t height) -{ - if (format == NVPIPE_RGBA32 || format == NVPIPE_YUV32) - return width * height * 4; - else if (format == NVPIPE_YUV64) - return width * height * 8; - else if (format == NVPIPE_UINT4) - return width * height / 2; - else if (format == NVPIPE_UINT8) - return width * height; - else if (format == NVPIPE_UINT16) - return width * height * 2; - else if (format == NVPIPE_UINT32) - return width * height * 4; - else if (format == NVPIPE_NV12_10bit) - return width * 2 * (height + height/2); - - return 0; -} - - -__global__ -void uint4_to_nv12(const uint8_t* src, uint32_t srcPitch, uint8_t* dst, uint32_t dstPitch, uint32_t width, uint32_t height) -{ - // one thread per pixel - const uint32_t x = blockIdx.x * blockDim.x + threadIdx.x; - const uint32_t y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x < width && y < height) - { - const uint32_t i = y * srcPitch + x / 2; - const uint32_t j = y * dstPitch + x; - - // Extend 4 bit to 8 bits - // Even thread: higher 4 bits, odd thread: lower 4 bits - dst[j] = (x & 1 == 1) ? (src[i] & 0xF) : ((src[i] & 0xF0) >> 4); - - // Blank UV channel - if (y < height / 2) - { - uint8_t* UV = dst + dstPitch * (height + y); - UV[x] = 0; - } - } -} - -__global__ -void nv12_to_uint4(const uint8_t* src, uint32_t srcPitch, uint8_t* dst, uint32_t dstPitch, uint32_t width, uint32_t height) -{ - // one thread per TWO pixels - const uint32_t x = blockIdx.x * blockDim.x + threadIdx.x; - const uint32_t y = blockIdx.y * blockDim.y + threadIdx.y; - - if (2 * x < width && y < height) - { - const uint32_t i = y * srcPitch + 2 * x; - const uint32_t j = y * dstPitch + x; - - // Merge lower 4 bits of two Y bytes to one output byte - uint8_t v = (src[i] & 0xF) << 4; - - if (2 * x + 1 < width) - v = v | (src[i + 1] & 0xF); - - dst[j] = v; - } -} - -__global__ -void uint8_to_nv12(const uint8_t* src, uint32_t srcPitch, uint8_t* dst, uint32_t dstPitch, uint32_t width, uint32_t height) -{ - const uint32_t x = blockIdx.x * blockDim.x + threadIdx.x; - const uint32_t y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x < width && y < height) - { - const uint32_t i = y * srcPitch + x; - const uint32_t j = y * dstPitch + x; - - // Copy grayscale image to Y channel - dst[j] = src[i]; - - // Blank UV channel - if (y < height / 2) - { - uint8_t* UV = dst + dstPitch * (height + y); - UV[x] = 0; - } - } -} - -__global__ -void nv12_to_uint8(const uint8_t* src, uint32_t srcPitch, uint8_t* dst, uint32_t dstPitch, uint32_t width, uint32_t height) -{ - const uint32_t x = blockIdx.x * blockDim.x + threadIdx.x; - const uint32_t y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x < width && y < height) - { - const uint32_t i = y * srcPitch + x; - const uint32_t j = y * dstPitch + x; - - // Copy Y channel to grayscale image - dst[j] = src[i]; - - } -} - -__global__ -void uint16_to_nv12(const uint8_t* src, uint32_t srcPitch, uint8_t* dst, uint32_t dstPitch, uint32_t width, uint32_t height) -{ - const uint32_t x = blockIdx.x * blockDim.x + threadIdx.x; - const uint32_t y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x < width && y < height) - { - const uint32_t i = y * srcPitch + 2 * x; - const uint32_t j = y * dstPitch + x; - - // Copy higher byte to left half of Y channel - dst[j] = src[i]; - - // Copy lower byte to right half of Y channel - dst[j + width] = src[i + 1]; - - // Blank UV channel - if (y < height / 2) - { - uint8_t* UV = dst + dstPitch * (height + y); - UV[2 * x + 0] = 0; - UV[2 * x + 1] = 0; - } - } -} - -__global__ -void nv12_to_uint16(const uint8_t* src, uint32_t srcPitch, uint8_t* dst, uint32_t dstPitch, uint32_t width, uint32_t height) -{ - const uint32_t x = blockIdx.x * blockDim.x + threadIdx.x; - const uint32_t y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x < width && y < height) - { - const uint32_t i = y * srcPitch + x; - const uint32_t j = y * dstPitch + 2 * x; - - // Copy higher byte from left half of Y channel - dst[j] = src[i]; - - // Copy lower byte from right half of Y channel - dst[j + 1] = src[i + width]; - } -} - -__global__ -void uint32_to_nv12(const uint8_t* src, uint32_t srcPitch, uint8_t* dst, uint32_t dstPitch, uint32_t width, uint32_t height) -{ - const uint32_t x = blockIdx.x * blockDim.x + threadIdx.x; - const uint32_t y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x < width && y < height) - { - const uint32_t i = y * srcPitch + 4 * x; - const uint32_t j = y * dstPitch + x; - - // Copy highest byte to left quarter of Y channel, - // ... - // Copy lowest byte to right quarter of Y channel - dst[j] = src[i]; - dst[j + width] = src[i + 1]; - dst[j + 2 * width] = src[i + 2]; - dst[j + 3 * width] = src[i + 3]; - - // Blank UV channel - if (y < height / 2) - { - uint8_t* UV = dst + dstPitch * (height + y); - UV[4 * x + 0] = 0; - UV[4 * x + 1] = 0; - UV[4 * x + 2] = 0; - UV[4 * x + 3] = 0; - } - } -} - -__global__ -void nv12_to_uint32(const uint8_t* src, uint32_t srcPitch, uint8_t* dst, uint32_t dstPitch, uint32_t width, uint32_t height) -{ - const uint32_t x = blockIdx.x * blockDim.x + threadIdx.x; - const uint32_t y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x < width && y < height) - { - const uint32_t i = y * srcPitch + x; - const uint32_t j = y * dstPitch + 4 * x; - - // Copy highest byte from left quarter of Y channel - // ... - // Copy lowest byte from right quarter of Y channel - dst[j] = src[i]; - dst[j + 1] = src[i + width]; - dst[j + 2] = src[i + 2 * width]; - dst[j + 3] = src[i + 3 * width]; - - } -} - -#ifdef NVPIPE_WITH_OPENGL -/** - * @brief Utility class for managing CUDA-GL interop graphics resources. - */ -class GraphicsResourceRegistry -{ -public: - virtual ~GraphicsResourceRegistry() - { - // Unregister all - for (auto& r : this->registeredPBOs) - CUDA_THROW(cudaGraphicsUnregisterResource(r.second.graphicsResource), - "Failed to unregister PBO graphics resource"); - - for (auto& r : this->registeredTextures) - CUDA_THROW(cudaGraphicsUnregisterResource(r.second.graphicsResource), - "Failed to unregister texture graphics resource"); - } - - cudaGraphicsResource_t getTextureGraphicsResource(uint32_t texture, uint32_t target, uint32_t width, uint32_t height, uint32_t flags) - { - // Check if texture needs to be (re)registered - RegisteredTexture& reg = this->registeredTextures[texture]; - - if (reg.width != width || reg.height != height || reg.target != target) { - if (reg.graphicsResource) { - CUDA_THROW(cudaGraphicsUnregisterResource(reg.graphicsResource), - "Failed to unregister texture graphics resource"); - - reg.graphicsResource = nullptr; - } - - CUDA_THROW(cudaGraphicsGLRegisterImage(®.graphicsResource, texture, target, flags), - "Failed to register texture as graphics resource"); - - reg.width = width; - reg.height = height; - reg.target = target; - } - - return reg.graphicsResource; - } - - cudaGraphicsResource_t getPBOGraphicsResource(uint32_t pbo, uint32_t width, uint32_t height, uint32_t flags) - { - // Check if PBO needs to be (re)registered - RegisteredPBO& reg = this->registeredPBOs[pbo]; - - if (reg.width != width || reg.height != height) { - if (reg.graphicsResource) { - CUDA_THROW(cudaGraphicsUnregisterResource(reg.graphicsResource), - "Failed to unregister PBO graphics resource"); - - reg.graphicsResource = nullptr; - } - - CUDA_THROW(cudaGraphicsGLRegisterBuffer(®.graphicsResource, pbo, flags), - "Failed to register PBO as graphics resource"); - - reg.width = width; - reg.height = height; - } - - return reg.graphicsResource; - } - -private: - struct RegisteredTexture - { - cudaGraphicsResource_t graphicsResource = nullptr; - uint32_t width = 0; - uint32_t height = 0; - uint32_t target = 0; - }; - std::unordered_map<uint32_t, RegisteredTexture> registeredTextures; - - struct RegisteredPBO - { - cudaGraphicsResource_t graphicsResource = nullptr; - uint32_t width = 0; - uint32_t height = 0; - }; - std::unordered_map<uint32_t, RegisteredPBO> registeredPBOs; -}; -#endif - - -#ifdef NVPIPE_WITH_ENCODER - -inline std::string EncErrorCodeToString(NVENCSTATUS code) -{ - std::vector<std::string> errors = { - "NV_ENC_SUCCESS", - "NV_ENC_ERR_NO_ENCODE_DEVICE", - "NV_ENC_ERR_UNSUPPORTED_DEVICE", - "NV_ENC_ERR_INVALID_ENCODERDEVICE", - "NV_ENC_ERR_INVALID_DEVICE", - "NV_ENC_ERR_DEVICE_NOT_EXIST", - "NV_ENC_ERR_INVALID_PTR", - "NV_ENC_ERR_INVALID_EVENT", - "NV_ENC_ERR_INVALID_PARAM", - "NV_ENC_ERR_INVALID_CALL", - "NV_ENC_ERR_OUT_OF_MEMORY", - "NV_ENC_ERR_ENCODER_NOT_INITIALIZED", - "NV_ENC_ERR_UNSUPPORTED_PARAM", - "NV_ENC_ERR_LOCK_BUSY", - "NV_ENC_ERR_NOT_ENOUGH_BUFFER", - "NV_ENC_ERR_INVALID_VERSION", - "NV_ENC_ERR_MAP_FAILED", - "NV_ENC_ERR_NEED_MORE_INPUT", - "NV_ENC_ERR_ENCODER_BUSY", - "NV_ENC_ERR_EVENT_NOT_REGISTERD", - "NV_ENC_ERR_GENERIC", - "NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY", - "NV_ENC_ERR_UNIMPLEMENTED", - "NV_ENC_ERR_RESOURCE_REGISTER_FAILED", - "NV_ENC_ERR_RESOURCE_NOT_REGISTERED", - "NV_ENC_ERR_RESOURCE_NOT_MAPPED" - }; - - if (code >= 0 && code < errors.size()) - return errors[code]; - - return "Unknown error code"; -} - -/** - * @brief Encoder implementation. - */ -class Encoder -{ -public: - Encoder(NvPipe_Format format, NvPipe_Codec codec, NvPipe_Compression compression, uint64_t bitrate, uint32_t targetFrameRate, uint32_t width, uint32_t height) - { - this->format = format; - this->codec = codec; - this->compression = compression; - this->bitrate = bitrate; - this->targetFrameRate = targetFrameRate; - - cudaStreamCreate(&stream); - - this->recreate(width, height); - } - - ~Encoder() - { - cudaStreamDestroy(stream); - // Destroy encoder - if (this->encoder) - { - std::vector<std::vector<uint8_t>> tmp; - this->encoder->EndEncode(tmp); - this->encoder->DestroyEncoder(); - this->encoder.reset(); - } - - // Free temporary device memory - if (this->deviceBuffer) - cudaFree(this->deviceBuffer); - } - - void setBitrate(uint64_t bitrate, uint32_t targetFrameRate) - { - NV_ENC_CONFIG config; - memset(&config, 0, sizeof(config)); - config.version = NV_ENC_CONFIG_VER; - config.rcParams.averageBitRate = bitrate; - - NV_ENC_RECONFIGURE_PARAMS reconfigureParams; - memset(&reconfigureParams, 0, sizeof(reconfigureParams)); - reconfigureParams.version = NV_ENC_RECONFIGURE_PARAMS_VER; - reconfigureParams.resetEncoder = 1; - reconfigureParams.forceIDR = 1; - reconfigureParams.reInitEncodeParams.encodeConfig = &config; - - encoder->GetInitializeParams(&reconfigureParams.reInitEncodeParams); - reconfigureParams.reInitEncodeParams.frameRateNum = targetFrameRate; - reconfigureParams.reInitEncodeParams.frameRateDen = 1; - - encoder->Reconfigure(&reconfigureParams); - - this->bitrate = bitrate; - this->targetFrameRate = targetFrameRate; - } - - uint64_t encode(const void* src, uint64_t srcPitch, uint8_t *dst, uint64_t dstSize, uint32_t width, uint32_t height, bool forceIFrame) - { - // Recreate encoder if size changed - if (this->format == NVPIPE_UINT16) - this->recreate(width * 2, height); // split into two adjecent tiles in Y channel - else if (this->format == NVPIPE_UINT32) - this->recreate(width * 4, height); // split into four adjecent tiles in Y channel - else - this->recreate(width, height); - - // RGBA can be directly copied from host or device - if (this->format == NVPIPE_RGBA32 || this->format == NVPIPE_YUV32) - { - const NvEncInputFrame* f = this->encoder->GetNextInputFrame(); - CUDA_THROW(cudaMemcpy2D(f->inputPtr, f->pitch, src, srcPitch, width * 4, height, isDevicePointer(src) ? cudaMemcpyDeviceToDevice : cudaMemcpyHostToDevice), - "Failed to copy input frame"); - } - // Other formats need to be copied to the device and converted - else - { - // Copy to device if necessary - bool copyToDevice = !isDevicePointer(src); - if (copyToDevice) - { - this->recreateDeviceBuffer(width, height); - CUDA_THROW(cudaMemcpy(this->deviceBuffer, src, getFrameSize(this->format, width, height), cudaMemcpyHostToDevice), - "Failed to copy input frame"); - } - - // Convert - const NvEncInputFrame* f = this->encoder->GetNextInputFrame(); - - if (this->format == NVPIPE_UINT4) - { - // one thread per pixel (extract 4 bit and copy to 8 bit) - dim3 gridSize(width / 16 + 1, height / 2 + 1); - dim3 blockSize(16, 2); - - uint4_to_nv12 << <gridSize, blockSize, 0, stream >> > ((uint8_t*)(copyToDevice ? this->deviceBuffer : src), srcPitch, (uint8_t*)f->inputPtr, f->pitch, width, height); - cudaStreamSynchronize(stream); - } - else if (this->format == NVPIPE_UINT8) - { - // one thread per pixel (copy 8 bit) - dim3 gridSize(width / 16 + 1, height / 2 + 1); - dim3 blockSize(16, 2); - - uint8_to_nv12 << <gridSize, blockSize, 0, stream >> > ((uint8_t*)(copyToDevice ? this->deviceBuffer : src), srcPitch, (uint8_t*)f->inputPtr, f->pitch, width, height); - cudaStreamSynchronize(stream); - } - else if (this->format == NVPIPE_UINT16) - { - // one thread per pixel (split 16 bit into 2x 8 bit) - dim3 gridSize(width / 16 + 1, height / 2 + 1); - dim3 blockSize(16, 2); - - uint16_to_nv12 << <gridSize, blockSize, 0, stream >> > ((uint8_t*)(copyToDevice ? this->deviceBuffer : src), srcPitch, (uint8_t*)f->inputPtr, f->pitch, width, height); - cudaStreamSynchronize(stream); - } - else if (this->format == NVPIPE_UINT32) - { - // one thread per pixel (split 32 bit into 4x 8 bit) - dim3 gridSize(width / 16 + 1, height / 2 + 1); - dim3 blockSize(16, 2); - - uint32_to_nv12 << <gridSize, blockSize, 0, stream >> > ((uint8_t*)(copyToDevice ? this->deviceBuffer : src), srcPitch, (uint8_t*)f->inputPtr, f->pitch, width, height); - cudaStreamSynchronize(stream); - } - } - - // Encode - return this->encode(dst, dstSize, forceIFrame); - } - -#ifdef NVPIPE_WITH_OPENGL - - uint64_t encodeTexture(uint32_t texture, uint32_t target, uint8_t* dst, uint64_t dstSize, uint32_t width, uint32_t height, bool forceIFrame) - { - if (this->format != NVPIPE_RGBA32) - throw Exception("The OpenGL interface only supports the RGBA32 format"); - - // Recreate encoder if size changed - this->recreate(width, height); - - // Map texture and copy input to encoder - cudaGraphicsResource_t resource = this->registry.getTextureGraphicsResource(texture, target, width, height, cudaGraphicsRegisterFlagsReadOnly); - CUDA_THROW(cudaGraphicsMapResources(1, &resource), - "Failed to map texture graphics resource"); - cudaArray_t array; - CUDA_THROW(cudaGraphicsSubResourceGetMappedArray(&array, resource, 0, 0), - "Failed get texture graphics resource array"); - - const NvEncInputFrame* f = this->encoder->GetNextInputFrame(); - CUDA_THROW(cudaMemcpy2DFromArray(f->inputPtr, f->pitch, array, 0, 0, width * 4, height, cudaMemcpyDeviceToDevice), - "Failed to copy from texture array"); - - // Encode - uint64_t size = this->encode(dst, dstSize, forceIFrame); - - // Unmap texture - CUDA_THROW(cudaGraphicsUnmapResources(1, &resource), - "Failed to unmap texture graphics resource"); - - return size; - } - - uint64_t encodePBO(uint32_t pbo, uint8_t* dst, uint64_t dstSize, uint32_t width, uint32_t height, bool forceIFrame) - { - if (this->format != NVPIPE_RGBA32) - throw Exception("The OpenGL interface only supports the RGBA32 format"); - - // Map PBO and copy input to encoder - cudaGraphicsResource_t resource = this->registry.getPBOGraphicsResource(pbo, width, height, cudaGraphicsRegisterFlagsReadOnly); - CUDA_THROW(cudaGraphicsMapResources(1, &resource), - "Failed to map PBO graphics resource"); - void* pboPointer; - size_t pboSize; - CUDA_THROW(cudaGraphicsResourceGetMappedPointer(&pboPointer, &pboSize, resource), - "Failed to get mapped PBO pointer"); - - // Encode - uint64_t size = this->encode(pboPointer, width * 4, dst, dstSize, width, height, forceIFrame); - - // Unmap PBO - CUDA_THROW(cudaGraphicsUnmapResources(1, &resource), - "Failed to unmap PBO graphics resource"); - - return size; - } - -#endif - -private: - void recreate(uint32_t width, uint32_t height) - { - std::lock_guard<std::mutex> lock(Encoder::mutex); - - // Only recreate if necessary - if (width == this->width && height == this->height) - return; - - this->width = width; - this->height = height; - - // Ensure we have a CUDA context - CUDA_THROW(cudaDeviceSynchronize(), - "Failed to synchronize device"); - CUcontext cudaContext; - cuCtxGetCurrent(&cudaContext); - - // Create encoder - try - { - // Destroy previous encoder - if (this->encoder) - { - std::vector<std::vector<uint8_t>> tmp; - this->encoder->EndEncode(tmp); - this->encoder->DestroyEncoder(); - this->encoder.reset(); - } - - NV_ENC_BUFFER_FORMAT bufferFormat; // = (this->format == NVPIPE_RGBA32) ? NV_ENC_BUFFER_FORMAT_ABGR : NV_ENC_BUFFER_FORMAT_NV12; - switch(this->format) { - case NVPIPE_RGBA32 : bufferFormat = NV_ENC_BUFFER_FORMAT_ABGR; break; - case NVPIPE_YUV32 : bufferFormat = NV_ENC_BUFFER_FORMAT_AYUV; break; - default : bufferFormat = NV_ENC_BUFFER_FORMAT_NV12; - } - this->encoder = std::unique_ptr<NvEncoderCuda>(new NvEncoderCuda(cudaContext, width, height, bufferFormat, 0)); - - NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER }; - NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER }; - initializeParams.encodeConfig = &encodeConfig; - - GUID codecGUID = (this->codec == NVPIPE_HEVC) ? NV_ENC_CODEC_HEVC_GUID : NV_ENC_CODEC_H264_GUID; - - GUID presetGUID = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID; - if (this->compression == NVPIPE_LOSSLESS) - presetGUID = NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID; // NV_ENC_PRESET_LOSSLESS_HP_GUID - - encoder->CreateDefaultEncoderParams(&initializeParams, codecGUID, presetGUID); - - initializeParams.encodeWidth = width; - initializeParams.encodeHeight = height; - initializeParams.frameRateNum = this->targetFrameRate; - initializeParams.frameRateDen = 1; - initializeParams.enablePTD = 1; - - encodeConfig.gopLength = NVENC_INFINITE_GOPLENGTH; // No B-frames - encodeConfig.frameIntervalP = 1; - - if (this->codec == NVPIPE_H264) - encodeConfig.encodeCodecConfig.h264Config.idrPeriod = NVENC_INFINITE_GOPLENGTH; - else if (this->codec == NVPIPE_HEVC) { - encodeConfig.encodeCodecConfig.hevcConfig.idrPeriod = NVENC_INFINITE_GOPLENGTH; - - if (this->compression == NVPIPE_LOSSY_10BIT_420 || this->compression == NVPIPE_LOSSY_10BIT_444) { - encodeConfig.encodeCodecConfig.hevcConfig.pixelBitDepthMinus8 = 2; // For 10-bit colour - } - - if (this->compression == NVPIPE_LOSSY_10BIT_444 || this->compression == NVPIPE_LOSSY_8BIT_444) { - encodeConfig.encodeCodecConfig.hevcConfig.chromaFormatIDC = 3; // For Yuv444 (1 for 420) - } - } - - if (this->compression != NVPIPE_LOSSLESS) - { - encodeConfig.rcParams.averageBitRate = this->bitrate; - encodeConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ; - encodeConfig.rcParams.vbvBufferSize = encodeConfig.rcParams.averageBitRate * initializeParams.frameRateDen / initializeParams.frameRateNum; // bitrate / framerate = one frame - encodeConfig.rcParams.maxBitRate = encodeConfig.rcParams.averageBitRate; - encodeConfig.rcParams.vbvInitialDelay = encodeConfig.rcParams.vbvBufferSize; - } - - encoder->CreateEncoder(&initializeParams); - } - catch (NVENCException& e) - { - throw Exception("Failed to create encoder (" + e.getErrorString() + ", error " + std::to_string(e.getErrorCode()) + " = " + EncErrorCodeToString(e.getErrorCode()) + ")"); - } - } - - uint64_t encode(uint8_t* dst, uint64_t dstSize, bool forceIFrame) - { - std::vector<std::vector<uint8_t>> packets; - - try - { - if (forceIFrame) - { - NV_ENC_PIC_PARAMS params = {}; - params.encodePicFlags = NV_ENC_PIC_FLAG_FORCEIDR | NV_ENC_PIC_FLAG_OUTPUT_SPSPPS; - - this->encoder->EncodeFrame(packets, ¶ms); - } - else - { - this->encoder->EncodeFrame(packets); - } - } - catch (NVENCException& e) - { - throw Exception("Encode failed (" + e.getErrorString() + ", error " + std::to_string(e.getErrorCode()) + " = " + EncErrorCodeToString(e.getErrorCode()) + ")"); - } - - // Copy output - uint64_t size = 0; - for (auto& p : packets) - { - if (size + p.size() <= dstSize) - { - memcpy(dst + size, p.data(), p.size()); - size += p.size(); - } - else - { - throw Exception("Encode output buffer overflow"); - } - } - - return size; - } - - void recreateDeviceBuffer(uint32_t width, uint32_t height) - { - // (Re)allocate temporary device memory if necessary - uint64_t requiredSize = getFrameSize(this->format, width, height); - - if (this->deviceBufferSize < requiredSize) - { - if (this->deviceBuffer) - cudaFree(this->deviceBuffer); - - this->deviceBufferSize = requiredSize; - CUDA_THROW(cudaMalloc(&this->deviceBuffer, this->deviceBufferSize), - "Failed to allocate temporary device memory"); - } - } - -private: - NvPipe_Format format; - NvPipe_Codec codec; - NvPipe_Compression compression; - uint64_t bitrate; - uint32_t targetFrameRate; - uint32_t width = 0; - uint32_t height = 0; - - std::unique_ptr<NvEncoderCuda> encoder; - - void* deviceBuffer = nullptr; - uint64_t deviceBufferSize = 0; - cudaStream_t stream; - - static std::mutex mutex; - -#ifdef NVPIPE_WITH_OPENGL - GraphicsResourceRegistry registry; -#endif -}; - -std::mutex Encoder::mutex; - -#endif - - -#ifdef NVPIPE_WITH_DECODER - -inline std::string DecErrorCodeToString(CUresult code) -{ - const char* str = nullptr; - cuGetErrorName(code, &str); - - if (str) - return std::string(str); - - return "Unknown error code"; -} - -/** - * @brief Decoder implementation. - */ -class Decoder -{ -public: - Decoder(NvPipe_Format format, NvPipe_Codec codec, uint32_t width, uint32_t height) - { - this->format = format; - this->codec = codec; - cudaStreamCreate(&stream); - - this->recreate(width, height); - } - - ~Decoder() - { - cudaStreamDestroy(stream); - // Free temporary device memory - if (this->deviceBuffer) - cudaFree(this->deviceBuffer); - } - - uint64_t decode(const uint8_t* src, uint64_t srcSize, void* dst, uint32_t width, uint32_t height, uint32_t pitch) - { - // Recreate decoder if size changed - if (this->format == NVPIPE_UINT16) - this->recreate(width * 2, height); // split into two adjecent tiles in Y channel - else if (this->format == NVPIPE_UINT32) - this->recreate(width * 4, height); // split into four adjecent tiles in Y channel - else - this->recreate(width, height); - - // Decode - uint8_t* decoded = this->decode(src, srcSize); - - if (nullptr != decoded) - { - // Allocate temporary device buffer if we need to copy to the host eventually - bool copyToHost = !isDevicePointer(dst); - if (copyToHost) - this->recreateDeviceBuffer(width, height); - - // Convert to output format - uint8_t* dstDevice = (uint8_t*)(copyToHost ? this->deviceBuffer : dst); - - if (this->format == NVPIPE_RGBA32) - { - Nv12ToColor32<RGBA32>(decoded, width, dstDevice, pitch, width, height, 0, stream); - //cudaStreamSynchronize(stream); - } - else if (this->format == NVPIPE_YUV32) - { - Nv12ToYuv32<YUVA32>(decoded, width, dstDevice, pitch, width, height, 0, stream); - } - else if (this->format == NVPIPE_YUV64) - { - Yuv420_10bitToYuv<YUVA64>(decoded, width*2, dstDevice, pitch, width, height, 0, stream); - } - else if (this->format == NVPIPE_NV12_10bit) - { - cudaMemcpy2DAsync(dstDevice, pitch, decoded, width*2, width*2, height+(height/2), cudaMemcpyDeviceToDevice, stream); - } - else if (this->format == NVPIPE_UINT4) - { - // one thread per TWO pixels (merge 2x4 bit to one byte per thread) - dim3 gridSize(width / 16 / 2 + 1, height / 2 + 1); - dim3 blockSize(16, 2); - - nv12_to_uint4 << <gridSize, blockSize, 0, this->stream >> > (decoded, this->decoder->GetDeviceFramePitch(), dstDevice, pitch, width, height); - //cudaStreamSynchronize(this->stream); - } - else if (this->format == NVPIPE_UINT8) - { - // one thread per pixel (copy 8 bit) - dim3 gridSize(width / 16 + 1, height / 2 + 1); - dim3 blockSize(16, 2); - - nv12_to_uint8 << <gridSize, blockSize, 0, this->stream >> > (decoded, this->decoder->GetDeviceFramePitch(), dstDevice, pitch, width, height); - //cudaStreamSynchronize(this->stream); - } - else if (this->format == NVPIPE_UINT16) - { - // one thread per pixel (merge 2x8 bit into 16 bit pixels) - dim3 gridSize(width / 16 + 1, height / 2 + 1); - dim3 blockSize(16, 2); - - nv12_to_uint16 << <gridSize, blockSize, 0, this->stream >> > (decoded, this->decoder->GetDeviceFramePitch(), dstDevice, pitch, width, height); - //cudaStreamSynchronize(this->stream); - } - else if (this->format == NVPIPE_UINT32) - { - // one thread per pixel (merge 4x8 bit into 32 bit pixels) - dim3 gridSize(width / 16 + 1, height / 2 + 1); - dim3 blockSize(16, 2); - - nv12_to_uint32 << <gridSize, blockSize, 0, this->stream >> > (decoded, this->decoder->GetDeviceFramePitch(), dstDevice, pitch, width, height); - //cudaStreamSynchronize(this->stream); - } - - // Copy to host if necessary - if (copyToHost) - CUDA_THROW(cudaMemcpyAsync(dst, this->deviceBuffer, getFrameSize(this->format, width, height), cudaMemcpyDeviceToHost, stream), - "Failed to copy output to host memory"); - - cudaStreamSynchronize(this->stream); - return getFrameSize(this->format, width, height); - } - - return 0; - } - -#ifdef NVPIPE_WITH_OPENGL - - uint64_t decodeTexture(const uint8_t* src, uint64_t srcSize, uint32_t texture, uint32_t target, uint32_t width, uint32_t height) - { - if (this->format != NVPIPE_RGBA32) - throw Exception("The OpenGL interface only supports the RGBA32 format"); - - // Recreate decoder if size changed - this->recreate(width, height); - - // Decode - uint8_t* decoded = this->decode(src, srcSize); - - if (nullptr != decoded) - { - // Convert to RGBA - this->recreateDeviceBuffer(width, height); - Nv12ToColor32<RGBA32>(decoded, width, (uint8_t*)this->deviceBuffer, width * 4, width, height); - - // Copy output to texture - cudaGraphicsResource_t resource = this->registry.getTextureGraphicsResource(texture, target, width, height, cudaGraphicsRegisterFlagsWriteDiscard); - CUDA_THROW(cudaGraphicsMapResources(1, &resource), - "Failed to map texture graphics resource"); - cudaArray_t array; - CUDA_THROW(cudaGraphicsSubResourceGetMappedArray(&array, resource, 0, 0), - "Failed get texture graphics resource array"); - CUDA_THROW(cudaMemcpy2DToArray(array, 0, 0, this->deviceBuffer, width * 4, width * 4, height, cudaMemcpyDeviceToDevice), - "Failed to copy to texture array"); - CUDA_THROW(cudaGraphicsUnmapResources(1, &resource), - "Failed to unmap texture graphics resource"); - - return width * height * 4; - } - - return 0; - } - - uint64_t decodePBO(const uint8_t* src, uint64_t srcSize, uint32_t pbo, uint32_t width, uint32_t height, uint32_t pitch) - { - if (this->format != NVPIPE_RGBA32) - throw Exception("The OpenGL interface only supports the RGBA32 format"); - - // Map PBO for output - cudaGraphicsResource_t resource = this->registry.getPBOGraphicsResource(pbo, width, height, cudaGraphicsRegisterFlagsWriteDiscard); - CUDA_THROW(cudaGraphicsMapResources(1, &resource), - "Failed to map PBO graphics resource"); - void* pboPointer; - size_t pboSize; - CUDA_THROW(cudaGraphicsResourceGetMappedPointer(&pboPointer, &pboSize, resource), - "Failed to get mapped PBO pointer"); - - // Decode - uint64_t size = this->decode(src, srcSize, pboPointer, width, height, pitch); - - // Unmap PBO - CUDA_THROW(cudaGraphicsUnmapResources(1, &resource), - "Failed to unmap PBO graphics resource"); - - return size; - } - -#endif - -private: - void recreate(uint32_t width, uint32_t height) - { - std::lock_guard<std::mutex> lock(Decoder::mutex); - - // Only recreate if necessary - if (width == this->width && height == this->height) - return; - - this->width = width; - this->height = height; - - // Ensure we have a CUDA context - CUDA_THROW(cudaDeviceSynchronize(), - "Failed to synchronize device"); - CUcontext cudaContext; - cuCtxGetCurrent(&cudaContext); - - // Create decoder - try - { - // Destroy previous decoder - if (this->decoder) - { - this->decoder.reset(); - } - - this->decoder = std::unique_ptr<NvDecoder>(new NvDecoder(cudaContext, width, height, true, (this->codec == NVPIPE_HEVC) ? cudaVideoCodec_HEVC : cudaVideoCodec_H264,/* &Decoder::mutex*/ nullptr, true)); - } - catch (NVDECException& e) - { - throw Exception("Failed to create decoder (" + e.getErrorString() + ", error " + std::to_string(e.getErrorCode()) + " = " + DecErrorCodeToString(e.getErrorCode()) + ")"); - } - } - - uint8_t* decode(const uint8_t* src, uint64_t srcSize) - { - int numFramesDecoded = 0; - uint8_t **decodedFrames; - int64_t *timeStamps; - - try - { - // Some cuvid implementations have one frame latency. Refeed frame into pipeline in this case. - const uint32_t DECODE_TRIES = 3; - for (uint32_t i = 0; (i < DECODE_TRIES) && (numFramesDecoded <= 0); ++i) - this->decoder->Decode(src, srcSize, &decodedFrames, &numFramesDecoded, CUVID_PKT_ENDOFPICTURE, &timeStamps, this->n++); - } - catch (NVDECException& e) - { - throw Exception("Decode failed (" + e.getErrorString() + ", error " + std::to_string(e.getErrorCode()) + " = " + DecErrorCodeToString(e.getErrorCode()) + ")"); - } - - if (numFramesDecoded <= 0) - { - throw Exception("No frame decoded (Decoder expects encoded bitstream for a single complete frame. Accumulating partial data or combining multiple frames is not supported.)"); - } - - return decodedFrames[numFramesDecoded - 1]; - } - - void recreateDeviceBuffer(uint32_t width, uint32_t height) - { - // (Re)allocate temporary device memory if necessary - uint64_t requiredSize = getFrameSize(this->format, width, height); - - if (this->deviceBufferSize < requiredSize) - { - if (this->deviceBuffer) - cudaFree(this->deviceBuffer); - - this->deviceBufferSize = requiredSize; - CUDA_THROW(cudaMalloc(&this->deviceBuffer, this->deviceBufferSize), - "Failed to allocate temporary device memory"); - } - } - -private: - NvPipe_Format format; - NvPipe_Codec codec; - uint32_t width = 0; - uint32_t height = 0; - - std::unique_ptr<NvDecoder> decoder; - int64_t n = 0; - - void* deviceBuffer = nullptr; - uint64_t deviceBufferSize = 0; - cudaStream_t stream; - - static std::mutex mutex; - -#ifdef NVPIPE_WITH_OPENGL - GraphicsResourceRegistry registry; -#endif -}; - -std::mutex Decoder::mutex; - -#endif - - - - -// --------- Exported C API --------- - -// NvPipe was originally developed as a C++ library. -// However, for compatibility reasons its functionality is now exposed as a plain C API. - -struct Instance -{ -#ifdef NVPIPE_WITH_ENCODER - std::unique_ptr<Encoder> encoder; -#endif - -#ifdef NVPIPE_WITH_DECODER - std::unique_ptr<Decoder> decoder; -#endif - - std::string error; -}; - -std::string sharedError; // shared error code for create functions (NOT threadsafe) - - -#ifdef NVPIPE_WITH_ENCODER - -NVPIPE_EXPORT NvPipe* NvPipe_CreateEncoder(NvPipe_Format format, NvPipe_Codec codec, NvPipe_Compression compression, uint64_t bitrate, uint32_t targetFrameRate, uint32_t width, uint32_t height) -{ - Instance* instance = new Instance(); - - try - { - instance->encoder = std::unique_ptr<Encoder>(new Encoder(format, codec, compression, bitrate, targetFrameRate, width, height)); - } - catch (Exception& e) - { - sharedError = e.getErrorString(); - delete instance; - return nullptr; - } - - return instance; -} - -NVPIPE_EXPORT void NvPipe_SetBitrate(NvPipe* nvp, uint64_t bitrate, uint32_t targetFrameRate) -{ - Instance* instance = static_cast<Instance*>(nvp); - if (!instance->encoder) - { - instance->error = "Invalid NvPipe encoder."; - return; - } - - try - { - return instance->encoder->setBitrate(bitrate, targetFrameRate); - } - catch (Exception& e) - { - instance->error = e.getErrorString(); - } -} - -NVPIPE_EXPORT uint64_t NvPipe_Encode(NvPipe* nvp, const void* src, uint64_t srcPitch, uint8_t* dst, uint64_t dstSize, uint32_t width, uint32_t height, bool forceIFrame) -{ - Instance* instance = static_cast<Instance*>(nvp); - if (!instance->encoder) - { - instance->error = "Invalid NvPipe encoder."; - return 0; - } - - try - { - return instance->encoder->encode(src, srcPitch, dst, dstSize, width, height, forceIFrame); - } - catch (Exception& e) - { - instance->error = e.getErrorString(); - return 0; - } -} - -#ifdef NVPIPE_WITH_OPENGL - -NVPIPE_EXPORT uint64_t NvPipe_EncodeTexture(NvPipe* nvp, uint32_t texture, uint32_t target, uint8_t* dst, uint64_t dstSize, uint32_t width, uint32_t height, bool forceIFrame) -{ - Instance* instance = static_cast<Instance*>(nvp); - if (!instance->encoder) - { - instance->error = "Invalid NvPipe encoder."; - return 0; - } - - try - { - return instance->encoder->encodeTexture(texture, target, dst, dstSize, width, height, forceIFrame); - } - catch (Exception& e) - { - instance->error = e.getErrorString(); - return 0; - } -} - -NVPIPE_EXPORT uint64_t NvPipe_EncodePBO(NvPipe* nvp, uint32_t pbo, uint8_t* dst, uint64_t dstSize, uint32_t width, uint32_t height, bool forceIFrame) -{ - Instance* instance = static_cast<Instance*>(nvp); - if (!instance->encoder) - { - instance->error = "Invalid NvPipe encoder."; - return 0; - } - - try - { - return instance->encoder->encodePBO(pbo, dst, dstSize, width, height, forceIFrame); - } - catch (Exception& e) - { - instance->error = e.getErrorString(); - return 0; - } -} - -#endif - -#endif - -#ifdef NVPIPE_WITH_DECODER - -NVPIPE_EXPORT NvPipe* NvPipe_CreateDecoder(NvPipe_Format format, NvPipe_Codec codec, uint32_t width, uint32_t height) -{ - Instance* instance = new Instance(); - - try - { - instance->decoder = std::unique_ptr<Decoder>(new Decoder(format, codec, width, height)); - } - catch (Exception& e) - { - sharedError = e.getErrorString(); - delete instance; - return nullptr; - } - - return instance; -} - -NVPIPE_EXPORT uint64_t NvPipe_Decode(NvPipe* nvp, const uint8_t* src, uint64_t srcSize, void* dst, uint32_t width, uint32_t height, uint32_t pitch) -{ - Instance* instance = static_cast<Instance*>(nvp); - if (!instance->decoder) - { - instance->error = "Invalid NvPipe decoder."; - return 0; - } - - try - { - return instance->decoder->decode(src, srcSize, dst, width, height, pitch); - } - catch (Exception& e) - { - instance->error = e.getErrorString(); - return 0; - } -} - -#ifdef NVPIPE_WITH_OPENGL - -NVPIPE_EXPORT uint64_t NvPipe_DecodeTexture(NvPipe* nvp, const uint8_t* src, uint64_t srcSize, uint32_t texture, uint32_t target, uint32_t width, uint32_t height) -{ - Instance* instance = static_cast<Instance*>(nvp); - if (!instance->decoder) - { - instance->error = "Invalid NvPipe decoder."; - return 0; - } - - try - { - return instance->decoder->decodeTexture(src, srcSize, texture, target, width, height); - } - catch (Exception& e) - { - instance->error = e.getErrorString(); - return 0; - } -} - -NVPIPE_EXPORT uint64_t NvPipe_DecodePBO(NvPipe* nvp, const uint8_t* src, uint64_t srcSize, uint32_t pbo, uint32_t width, uint32_t height, uint32_t pitch) -{ - Instance* instance = static_cast<Instance*>(nvp); - if (!instance->decoder) - { - instance->error = "Invalid NvPipe decoder."; - return 0; - } - - try - { - return instance->decoder->decodePBO(src, srcSize, pbo, width, height, pitch); - } - catch (Exception& e) - { - instance->error = e.getErrorString(); - return 0; - } -} - -#endif - -#endif - -NVPIPE_EXPORT void NvPipe_Destroy(NvPipe* nvp) -{ - Instance* instance = static_cast<Instance*>(nvp); - delete instance; -} - -NVPIPE_EXPORT const char* NvPipe_GetError(NvPipe* nvp) -{ - if (nullptr == nvp) - return sharedError.c_str(); - - Instance* instance = static_cast<Instance*>(nvp); - return instance->error.c_str(); -} - - - - - - - - - - - - - - - diff --git a/lib/nvpipe/src/NvPipe.h.in b/lib/nvpipe/src/NvPipe.h.in deleted file mode 100644 index b6e229915b752006ce5d4dcc90e7b87b158a33ff..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/NvPipe.h.in +++ /dev/null @@ -1,241 +0,0 @@ -/* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NVIDIA CORPORATION nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef NVPIPE_H -#define NVPIPE_H - -#include <stdlib.h> -#include <stdint.h> - -#include <cuda_runtime_api.h> - -#cmakedefine NVPIPE_WITH_ENCODER -#cmakedefine NVPIPE_WITH_DECODER -#cmakedefine NVPIPE_WITH_OPENGL - -#ifdef _WIN32 -# define NVPIPE_EXPORT __declspec(dllexport) -#else -# define NVPIPE_EXPORT __attribute__((visibility("default"))) -#endif - -extern "C" -{ - -typedef void NvPipe; - - -/** - * Available video codecs in NvPipe. - */ -typedef enum { - NVPIPE_H264, - NVPIPE_HEVC -} NvPipe_Codec; - - -/** - * Compression type used for encoding. Lossless produces larger output. - */ -typedef enum { - NVPIPE_LOSSY, - NVPIPE_LOSSLESS, - NVPIPE_LOSSY_10BIT_420, - NVPIPE_LOSSY_10BIT_444, - NVPIPE_LOSSY_8BIT_444 -} NvPipe_Compression; - - -/** - * Format of the input frame. - */ -typedef enum { - NVPIPE_RGBA32, - NVPIPE_UINT4, - NVPIPE_UINT8, - NVPIPE_UINT16, - NVPIPE_UINT32, - NVPIPE_YUV32, // Added by Nick - NVPIPE_YUV64, // Added by Nick - NVPIPE_NV12, // Added by Nick - NVPIPE_NV12_10bit // Added by Nick -} NvPipe_Format; - - -#ifdef NVPIPE_WITH_ENCODER - -/** - * @brief Creates a new encoder instance. - * @param format Format of input frame. - * @param codec Possible codecs are H.264 and HEVC if available. - * @param compression Lossy or lossless compression. - * @param bitrate Bitrate in bit per second, e.g., 32 * 1000 * 1000 = 32 Mbps (for lossy compression only). - * @param targetFrameRate At this frame rate the effective data rate approximately equals the bitrate (for lossy compression only). - * @param width Initial width of the encoder. - * @param height Initial height of the encoder. - * @return NULL on error. - */ -NVPIPE_EXPORT NvPipe* NvPipe_CreateEncoder(NvPipe_Format format, NvPipe_Codec codec, NvPipe_Compression compression, uint64_t bitrate, uint32_t targetFrameRate, uint32_t width, uint32_t height); - - -/** - * @brief Reconfigures the encoder with a new bitrate and target frame rate. - * @param nvp Encoder instance. - * @param bitrate Bitrate in bit per second, e.g., 32 * 1000 * 1000 = 32 Mbps (for lossy compression only). - * @param targetFrameRate At this frame rate the effective data rate approximately equals the bitrate (for lossy compression only). - */ -NVPIPE_EXPORT void NvPipe_SetBitrate(NvPipe* nvp, uint64_t bitrate, uint32_t targetFrameRate); - - -/** - * @brief Encodes a single frame from device or host memory. - * @param nvp Encoder instance. - * @param src Device or host memory pointer. - * @param srcPitch Pitch of source memory. - * @param dst Host memory pointer for compressed output. - * @param dstSize Available space for compressed output. - * @param width Width of input frame in pixels. - * @param height Height of input frame in pixels. - * @param forceIFrame Enforces an I-frame instead of a P-frame. - * @param stream Cuda stream, default to no stream - * @return Size of encoded data in bytes or 0 on error. - */ -NVPIPE_EXPORT uint64_t NvPipe_Encode(NvPipe* nvp, const void* src, uint64_t srcPitch, uint8_t* dst, uint64_t dstSize, uint32_t width, uint32_t height, bool forceIFrame); - - -#ifdef NVPIPE_WITH_OPENGL - -/** - * @brief encodeTexture Encodes a single frame from an OpenGL texture. - * @param nvp Encoder instance. - * @param texture OpenGL texture ID. - * @param target OpenGL texture target. - * @param dst Host memory pointer for compressed output. - * @param dstSize Available space for compressed output. Will be overridden by effective compressed output size. - * @param width Width of frame in pixels. - * @param height Height of frame in pixels. - * @param forceIFrame Enforces an I-frame instead of a P-frame. - * @return Size of encoded data in bytes or 0 on error. - */ -NVPIPE_EXPORT uint64_t NvPipe_EncodeTexture(NvPipe* nvp, uint32_t texture, uint32_t target, uint8_t* dst, uint64_t dstSize, uint32_t width, uint32_t height, bool forceIFrame); - - -/** - * @brief encodePBO Encodes a single frame from an OpenGL pixel buffer object (PBO). - * @param nvp Encoder instance. - * @param pbo OpenGL PBO ID. - * @param dst Host memory pointer for compressed output. - * @param dstSize Available space for compressed output. Will be overridden by effective compressed output size. - * @param width Width of frame in pixels. - * @param height Height of frame in pixels. - * @param forceIFrame Enforces an I-frame instead of a P-frame. - * @return Size of encoded data in bytes or 0 on error. - */ -NVPIPE_EXPORT uint64_t NvPipe_EncodePBO(NvPipe* nvp, uint32_t pbo, uint8_t* dst, uint64_t dstSize, uint32_t width, uint32_t height, bool forceIFrame); - -#endif - -#endif - -#ifdef NVPIPE_WITH_DECODER - -/** - * @brief Creates a new decoder instance. - * @param format Format of output frame. - * @param codec Possible codecs are H.264 and HEVC if available. - * @param width Initial width of the decoder. - * @param height Initial height of the decoder. - * @return NULL on error. - */ -NVPIPE_EXPORT NvPipe* NvPipe_CreateDecoder(NvPipe_Format format, NvPipe_Codec codec, uint32_t width, uint32_t height); - - -/** - * @brief Decodes a single frame to device or host memory. - * @param nvp Decoder instance. - * @param src Compressed frame data in host memory. - * @param srcSize Size of compressed data. - * @param dst Device or host memory pointer. - * @param width Width of frame in pixels. - * @param height Height of frame in pixels. - * @param stream Cuda stream - * @return Size of decoded data in bytes or 0 on error. - */ -NVPIPE_EXPORT uint64_t NvPipe_Decode(NvPipe* nvp, const uint8_t* src, uint64_t srcSize, void* dst, uint32_t width, uint32_t height, uint32_t pitch); - - -#ifdef NVPIPE_WITH_OPENGL - -/** - * @brief Decodes a single frame to an OpenGL texture. - * @param nvp Decoder instance. - * @param src Compressed frame data in host memory. - * @param srcSize Size of compressed data. - * @param texture OpenGL texture ID. - * @param target OpenGL texture target. - * @param width Width of frame in pixels. - * @param height Height of frame in pixels. - * @return Size of decoded data in bytes or 0 on error. - */ -NVPIPE_EXPORT uint64_t NvPipe_DecodeTexture(NvPipe* nvp, const uint8_t* src, uint64_t srcSize, uint32_t texture, uint32_t target, uint32_t width, uint32_t height); - - -/** - * @brief Decodes a single frame to an OpenGL pixel buffer object (PBO). - * @param nvp Decoder instance. - * @param src Compressed frame data in host memory. - * @param srcSize Size of compressed data. - * @param pbo OpenGL PBO ID. - * @param width Width of frame in pixels. - * @param height Height of frame in pixels. - * @return Size of decoded data in bytes or 0 on error. - */ -NVPIPE_EXPORT uint64_t NvPipe_DecodePBO(NvPipe* nvp, const uint8_t* src, uint64_t srcSize, uint32_t pbo, uint32_t width, uint32_t height, uint32_t pitch); - -#endif - -#endif - - -/** - * @brief Cleans up an encoder or decoder instance. - * @param nvp The encoder or decoder instance to destroy. - */ -NVPIPE_EXPORT void NvPipe_Destroy(NvPipe* nvp); - - -/** - * @brief Returns an error message for the last error that occured. - * @param nvp Encoder or decoder. Use NULL to get error message if encoder or decoder creation failed. - * @return Returned string must not be deleted. - */ -NVPIPE_EXPORT const char* NvPipe_GetError(NvPipe* nvp); - -} - -#endif - diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/Win32/nvcuvid.lib b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/Win32/nvcuvid.lib deleted file mode 100644 index 0f6c15b7ef33a242554a5de31914f74261a661f6..0000000000000000000000000000000000000000 Binary files a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/Win32/nvcuvid.lib and /dev/null differ diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/linux/stubs/x86_64/libnvcuvid.so b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/linux/stubs/x86_64/libnvcuvid.so deleted file mode 100644 index f08a209545e076a835d11dcc24bd20d22088b1c5..0000000000000000000000000000000000000000 Binary files a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/linux/stubs/x86_64/libnvcuvid.so and /dev/null differ diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/linux/stubs/x86_64/libnvidia-encode.so b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/linux/stubs/x86_64/libnvidia-encode.so deleted file mode 100644 index 99934c7b22357afacb382aef554075081ee787f6..0000000000000000000000000000000000000000 Binary files a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/linux/stubs/x86_64/libnvidia-encode.so and /dev/null differ diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/x64/nvcuvid.lib b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/x64/nvcuvid.lib deleted file mode 100644 index 3c186902f03a03c19b724d6af8b19ba866b6edbf..0000000000000000000000000000000000000000 Binary files a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Lib/x64/nvcuvid.lib and /dev/null differ diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/LicenseAgreement.pdf b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/LicenseAgreement.pdf deleted file mode 100644 index 0e44fcdba0dda1858c321e26695f10b399e84af9..0000000000000000000000000000000000000000 Binary files a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/LicenseAgreement.pdf and /dev/null differ diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/ReadMe.txt b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/ReadMe.txt deleted file mode 100644 index 0f507150ad04ec57b486c809eec989affdf0897e..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/ReadMe.txt +++ /dev/null @@ -1,68 +0,0 @@ -NVIDIA Video Codec SDK 9.0 Readme and Getting Started Guide - -System Requirements - -* NVIDIA Kepler/Maxwell/Pascal/Volta/Turing GPU with hardware video accelerators - Refer to the NVIDIA Video SDK developer zone web page - (https://developer.nvidia.com/nvidia-video-codec-sdk) for GPUs which support - video encoding and decoding acceleration. - -* Windows: Driver version 418.81 or higher -* Linux: Driver version 418.30 or higher -* CUDA 10.0 Toolkit -* NVIDIA Video Codec SDK is now supported on IBM Power9 class server with - NVIDIA Tesla V100 (SXM2) GPU. - -[Windows Configuration Requirements] -- DirectX SDK is needed. You can download the latest SDK from Microsoft's DirectX - website. -- The CUDA 10.0 tool kit is needed to compile the decode samples in SDK 9.0 - and above. -- CUDA tool kit is also used for building CUDA kernels that can interop with - NVENC. - -In Windows, the following environment variables must be set to build the sample -applications included with the SDK - - DXSDK_DIR: pointing to the DirectX SDK root directory. - - The CUDA 10.0 Toolkit is optional to install if the client has - Video Codec SDK 8.0. However it is mandatory if client has - Video Codec SDK 8.1 or above on his/her machine. - -[Linux Configuration Requirements] - - X11 and OpenGL, GLUT, GLEW libraries for video playback and display - - The CUDA 10.0 Toolkit is optional to install if the client has Video Codec - SDK 8.0. - - CUDA 10.0 Toolkit is mandatory if client has Video Codec SDK 8.1 or above - on his/her machine. - - CUDA toolkit is used for building CUDA kernels that can interop with NVENC. - - Libraries and headers from the FFmpeg project which can be downloaded and - installed using the distribution's package manager or compiled from source. - The sample applications have been compiled and tested against the - libraries and headers from FFmpeg- 4.1. The source code of FFmpeg- 4.1 - has been included in this SDK package. While configuring FFmpeg on Linux, - it is recommended not to use 'disable-decoders' option. This configuration - is known to have a channel error (XID 31) while executing sample - applications with certain clips and/or result in an unexpected behavior. - - To build/use sample applications that depend on FFmpeg, users may need to - * Add the directory (/usr/local/lib/pkgconfig by default) to the - PKG_CONFIG_PATH environment variable. This is required by the Makefile - to determine the include paths for the FFmpeg headers. - * Add the directory where the FFmpeg libraries are installed, to the - LD_LIBRARY_PATH environment variable. This is required for resolving - runtime dependencies on FFmpeg libraries. - - Stub libraries (libnvcuvid.so and libnvidia-encode.so) have been included - as part of the SDK package, in order to aid development of applications on - systems where the NVIDIA driver has not been installed. The sample - applications in the SDK will link against these stub libraries as part of - the build process. However, users need to ensure that the stub libraries - are not referenced when running the sample applications. A driver - compatible with this SDK needs to be installed in order for the sample - applications to work correctly. - - The Vulkan SDK needs to be installed in order to build and run the - AppMotionEstimationVkCuda sample application. Vulkan SDK can be downloaded - from https://vulkan.lunarg.com/sdk/home. Alternatively, it can be - installed by using the distribution's package manager. - -[Common to all OS platforms] -* To download the CUDA 10.0 toolkit, please go to the following web site: - http://developer.nvidia.com/cuda/cuda-toolkit \ No newline at end of file diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Release_notes.txt b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Release_notes.txt deleted file mode 100644 index d1f7c782911cead1e82d875105beb38eaf5c0a1c..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Release_notes.txt +++ /dev/null @@ -1,66 +0,0 @@ -What's new in Video Codec SDK 9.0: ----------------------------------- -In NVIDIA Video Codec SDK release 9.0, following features have been added: - -Encode Features:: -1. Improved encoded quality for Turing GPUs -2. HEVC B-frame support (Turing GPUs only) -3. Encoded output in video memory -4. H.264 ME only mode output in video memory. -5. Non-reference P frames -6. Support for accepting CUArray as input - -Decode Features:: -1. HEVC YUV 444 decoding (Turing GPUs only) -2. Multiple NVDEC engines(Turing GPUs only) - - -Please refer to .\doc\NVENC_Application_Note.pdf and -.\doc\NVDEC_Application_Note.pdf to get more details about the available -encoding and decoding functionalities in the SDK. - -For system, software and build environment requirements, please refer to the -file ReadMe.txt included in the SDK package. - -Package Contents ----------------- -This package contains the following: - -1) Sample applications demonstrating various encoding/decoding/transcoding - capabilities - - [.\Samples\] - -2) NVIDIA video encoder API header - - [.\include\nvEncodeAPI.h] - -3) NVIDIA video encoder application note - - [.\doc\NVENC_Application_Note.pdf] - -4) NVIDIA video encoder programming guide - - [.\doc\NVENC_VideoEncoder_API_ProgGuide.pdf] - -5) NVIDIA video decoder API headers - - [.\include\cuviddec.h] - - [.\include\nvcuvid.h] - -6) NVIDIA video decoder application note - - [.\doc\NVDEC_Application_Note.pdf] - -7) NVIDIA video decoder programming guide - - [.\doc\NVDEC_VideoDecoder_API_ProgGuide.pdf] - -8) Application note on how to use NVIDIA video acceleration in FFmpeg - - [.\doc\Using FFmpeg with NVIDIA GPU Hardware Acceleration.pdf] - -9) NVIDIA video decoder and encoder stub libraries - - [.\Lib\linux\stubs\x86_64\libnvcuvid.so] - - [.\Lib\linux\stubs\x86_64\libnvidia-encode.so] - - [.\Lib\linux\stubs\ppc64le\libnvcuvid.so] - - [.\Lib\linux\stubs\ppc64le\libnvidia-encode.so] - - [.\Lib\Win32\nvcuvid.lib] - - [.\Lib\x64\nvcuvid.lib] - -The sample applications provided in the package are for demonstration purposes -only and may not be fully tuned for quality and performance. Hence the users -are advised to do their independent evaluation for quality and/or performance. - diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvDecoder/NvDecoder.cpp b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvDecoder/NvDecoder.cpp deleted file mode 100644 index f3186776b4941fb9f7bf19540b92e922ece60e60..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvDecoder/NvDecoder.cpp +++ /dev/null @@ -1,704 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include <iostream> -#include <algorithm> -#include <chrono> - -#include "nvcuvid.h" -#include "../Utils/NvCodecUtils.h" -#include "NvDecoder/NvDecoder.h" - -//#define START_TIMER auto start = std::chrono::high_resolution_clock::now(); -//#define STOP_TIMER(print_message) std::cout << print_message << \ -// std::chrono::duration_cast<std::chrono::milliseconds>( \ -// std::chrono::high_resolution_clock::now() - start).count() \ -// << " ms " << std::endl; - -// tb: Remove console output for NvPipe -#define START_TIMER -#define STOP_TIMER(x) - -#define CUDA_DRVAPI_CALL( call ) \ - do \ - { \ - CUresult err__ = call; \ - if (err__ != CUDA_SUCCESS) \ - { \ - const char *szErrName = NULL; \ - cuGetErrorName(err__, &szErrName); \ - std::ostringstream errorLog; \ - errorLog << "CUDA driver API error " << szErrName ; \ - throw NVDECException::makeNVDECException(errorLog.str(), err__, __FUNCTION__, __FILE__, __LINE__); \ - } \ - } \ - while (0) - -static const char * GetVideoCodecString(cudaVideoCodec eCodec) { - static struct { - cudaVideoCodec eCodec; - const char *name; - } aCodecName [] = { - { cudaVideoCodec_MPEG1, "MPEG-1" }, - { cudaVideoCodec_MPEG2, "MPEG-2" }, - { cudaVideoCodec_MPEG4, "MPEG-4 (ASP)" }, - { cudaVideoCodec_VC1, "VC-1/WMV" }, - { cudaVideoCodec_H264, "AVC/H.264" }, - { cudaVideoCodec_JPEG, "M-JPEG" }, - { cudaVideoCodec_H264_SVC, "H.264/SVC" }, - { cudaVideoCodec_H264_MVC, "H.264/MVC" }, - { cudaVideoCodec_HEVC, "H.265/HEVC" }, - { cudaVideoCodec_VP8, "VP8" }, - { cudaVideoCodec_VP9, "VP9" }, - { cudaVideoCodec_NumCodecs, "Invalid" }, - { cudaVideoCodec_YUV420, "YUV 4:2:0" }, - { cudaVideoCodec_YV12, "YV12 4:2:0" }, - { cudaVideoCodec_NV12, "NV12 4:2:0" }, - { cudaVideoCodec_YUYV, "YUYV 4:2:2" }, - { cudaVideoCodec_UYVY, "UYVY 4:2:2" }, - }; - - if (eCodec >= 0 && eCodec <= cudaVideoCodec_NumCodecs) { - return aCodecName[eCodec].name; - } - for (int i = cudaVideoCodec_NumCodecs + 1; i < sizeof(aCodecName) / sizeof(aCodecName[0]); i++) { - if (eCodec == aCodecName[i].eCodec) { - return aCodecName[eCodec].name; - } - } - return "Unknown"; -} - -static const char * GetVideoChromaFormatString(cudaVideoChromaFormat eChromaFormat) { - static struct { - cudaVideoChromaFormat eChromaFormat; - const char *name; - } aChromaFormatName[] = { - { cudaVideoChromaFormat_Monochrome, "YUV 400 (Monochrome)" }, - { cudaVideoChromaFormat_420, "YUV 420" }, - { cudaVideoChromaFormat_422, "YUV 422" }, - { cudaVideoChromaFormat_444, "YUV 444" }, - }; - - if (eChromaFormat >= 0 && eChromaFormat < sizeof(aChromaFormatName) / sizeof(aChromaFormatName[0])) { - return aChromaFormatName[eChromaFormat].name; - } - return "Unknown"; -} - -static float GetChromaHeightFactor(cudaVideoChromaFormat eChromaFormat) -{ - float factor = 0.5; - switch (eChromaFormat) - { - case cudaVideoChromaFormat_Monochrome: - factor = 0.0; - break; - case cudaVideoChromaFormat_420: - factor = 0.5; - break; - case cudaVideoChromaFormat_422: - factor = 1.0; - break; - case cudaVideoChromaFormat_444: - factor = 1.0; - break; - } - - return factor; -} - -static int GetChromaPlaneCount(cudaVideoChromaFormat eChromaFormat) -{ - int numPlane = 1; - switch (eChromaFormat) - { - case cudaVideoChromaFormat_Monochrome: - numPlane = 0; - break; - case cudaVideoChromaFormat_420: - numPlane = 1; - break; - case cudaVideoChromaFormat_444: - numPlane = 2; - break; - } - - return numPlane; -} - -static unsigned long GetNumDecodeSurfaces(cudaVideoCodec eCodec, unsigned int nWidth, unsigned int nHeight) { - if (eCodec == cudaVideoCodec_VP9) { - return 12; - } - - if (eCodec == cudaVideoCodec_H264 || eCodec == cudaVideoCodec_H264_SVC || eCodec == cudaVideoCodec_H264_MVC) { - // assume worst-case of 20 decode surfaces for H264 - return 20; - } - - if (eCodec == cudaVideoCodec_HEVC) { - // ref HEVC spec: A.4.1 General tier and level limits - // currently assuming level 6.2, 8Kx4K - int MaxLumaPS = 35651584; - int MaxDpbPicBuf = 6; - int PicSizeInSamplesY = (int)(nWidth * nHeight); - int MaxDpbSize; - if (PicSizeInSamplesY <= (MaxLumaPS>>2)) - MaxDpbSize = MaxDpbPicBuf * 4; - else if (PicSizeInSamplesY <= (MaxLumaPS>>1)) - MaxDpbSize = MaxDpbPicBuf * 2; - else if (PicSizeInSamplesY <= ((3*MaxLumaPS)>>2)) - MaxDpbSize = (MaxDpbPicBuf * 4) / 3; - else - MaxDpbSize = MaxDpbPicBuf; - return (std::min)(MaxDpbSize, 16) + 4; - } - - return 8; -} - -/* Return value from HandleVideoSequence() are interpreted as : -* 0: fail, 1: suceeded, > 1: override dpb size of parser (set by CUVIDPARSERPARAMS::ulMaxNumDecodeSurfaces while creating parser) -*/ -int NvDecoder::HandleVideoSequence(CUVIDEOFORMAT *pVideoFormat) -{ - START_TIMER - m_videoInfo.str(""); - m_videoInfo.clear(); - m_videoInfo << "Video Input Information" << std::endl - << "\tCodec : " << GetVideoCodecString(pVideoFormat->codec) << std::endl - << "\tFrame rate : " << pVideoFormat->frame_rate.numerator << "/" << pVideoFormat->frame_rate.denominator - << " = " << 1.0 * pVideoFormat->frame_rate.numerator / pVideoFormat->frame_rate.denominator << " fps" << std::endl - << "\tSequence : " << (pVideoFormat->progressive_sequence ? "Progressive" : "Interlaced") << std::endl - << "\tCoded size : [" << pVideoFormat->coded_width << ", " << pVideoFormat->coded_height << "]" << std::endl - << "\tDisplay area : [" << pVideoFormat->display_area.left << ", " << pVideoFormat->display_area.top << ", " - << pVideoFormat->display_area.right << ", " << pVideoFormat->display_area.bottom << "]" << std::endl - << "\tChroma : " << GetVideoChromaFormatString(pVideoFormat->chroma_format) << std::endl - << "\tBit depth : " << pVideoFormat->bit_depth_luma_minus8 + 8 - ; - m_videoInfo << std::endl; - - int nDecodeSurface = GetNumDecodeSurfaces(pVideoFormat->codec, pVideoFormat->coded_width, pVideoFormat->coded_height); - - CUVIDDECODECAPS decodecaps; - memset(&decodecaps, 0, sizeof(decodecaps)); - - decodecaps.eCodecType = pVideoFormat->codec; - decodecaps.eChromaFormat = pVideoFormat->chroma_format; - decodecaps.nBitDepthMinus8 = pVideoFormat->bit_depth_luma_minus8; - - CUDA_DRVAPI_CALL(cuCtxPushCurrent(m_cuContext)); - NVDEC_API_CALL(cuvidGetDecoderCaps(&decodecaps)); - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); - - if(!decodecaps.bIsSupported){ - NVDEC_THROW_ERROR("Codec not supported on this GPU", CUDA_ERROR_NOT_SUPPORTED); - return nDecodeSurface; - } - - if ((pVideoFormat->coded_width > decodecaps.nMaxWidth) || - (pVideoFormat->coded_height > decodecaps.nMaxHeight)){ - - std::ostringstream errorString; - errorString << std::endl - << "Resolution : " << pVideoFormat->coded_width << "x" << pVideoFormat->coded_height << std::endl - << "Max Supported (wxh) : " << decodecaps.nMaxWidth << "x" << decodecaps.nMaxHeight << std::endl - << "Resolution not supported on this GPU"; - - const std::string cErr = errorString.str(); - NVDEC_THROW_ERROR(cErr, CUDA_ERROR_NOT_SUPPORTED); - return nDecodeSurface; - } - - if ((pVideoFormat->coded_width>>4)*(pVideoFormat->coded_height>>4) > decodecaps.nMaxMBCount){ - - std::ostringstream errorString; - errorString << std::endl - << "MBCount : " << (pVideoFormat->coded_width >> 4)*(pVideoFormat->coded_height >> 4) << std::endl - << "Max Supported mbcnt : " << decodecaps.nMaxMBCount << std::endl - << "MBCount not supported on this GPU"; - - const std::string cErr = errorString.str(); - NVDEC_THROW_ERROR(cErr, CUDA_ERROR_NOT_SUPPORTED); - return nDecodeSurface; - } - - if (m_nWidth && m_nLumaHeight && m_nChromaHeight) { - - // cuvidCreateDecoder() has been called before, and now there's possible config change - return ReconfigureDecoder(pVideoFormat); - } - - // eCodec has been set in the constructor (for parser). Here it's set again for potential correction - m_eCodec = pVideoFormat->codec; - m_eChromaFormat = pVideoFormat->chroma_format; - m_nBitDepthMinus8 = pVideoFormat->bit_depth_luma_minus8; - m_nBPP = m_nBitDepthMinus8 > 0 ? 2 : 1; - - if (m_eChromaFormat == cudaVideoChromaFormat_420) - m_eOutputFormat = pVideoFormat->bit_depth_luma_minus8 ? cudaVideoSurfaceFormat_P016 : cudaVideoSurfaceFormat_NV12; - else if (m_eChromaFormat == cudaVideoChromaFormat_444) - m_eOutputFormat = pVideoFormat->bit_depth_luma_minus8 ? cudaVideoSurfaceFormat_YUV444_16Bit : cudaVideoSurfaceFormat_YUV444; - - m_videoFormat = *pVideoFormat; - - CUVIDDECODECREATEINFO videoDecodeCreateInfo = { 0 }; - videoDecodeCreateInfo.CodecType = pVideoFormat->codec; - videoDecodeCreateInfo.ChromaFormat = pVideoFormat->chroma_format; - videoDecodeCreateInfo.OutputFormat = m_eOutputFormat; - videoDecodeCreateInfo.bitDepthMinus8 = pVideoFormat->bit_depth_luma_minus8; - videoDecodeCreateInfo.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave; - videoDecodeCreateInfo.ulNumOutputSurfaces = 2; - // With PreferCUVID, JPEG is still decoded by CUDA while video is decoded by NVDEC hardware - videoDecodeCreateInfo.ulCreationFlags = cudaVideoCreate_PreferCUVID; - videoDecodeCreateInfo.ulNumDecodeSurfaces = nDecodeSurface; - videoDecodeCreateInfo.vidLock = m_ctxLock; - videoDecodeCreateInfo.ulWidth = pVideoFormat->coded_width; - videoDecodeCreateInfo.ulHeight = pVideoFormat->coded_height; - if (m_nMaxWidth < (int)pVideoFormat->coded_width) - m_nMaxWidth = pVideoFormat->coded_width; - if (m_nMaxHeight < (int)pVideoFormat->coded_height) - m_nMaxHeight = pVideoFormat->coded_height; - videoDecodeCreateInfo.ulMaxWidth = m_nMaxWidth; - videoDecodeCreateInfo.ulMaxHeight = m_nMaxHeight; - - if (!(m_cropRect.r && m_cropRect.b) && !(m_resizeDim.w && m_resizeDim.h)) { - m_nWidth = pVideoFormat->display_area.right - pVideoFormat->display_area.left; - m_nLumaHeight = pVideoFormat->display_area.bottom - pVideoFormat->display_area.top; - videoDecodeCreateInfo.ulTargetWidth = pVideoFormat->coded_width; - videoDecodeCreateInfo.ulTargetHeight = pVideoFormat->coded_height; - } else { - if (m_resizeDim.w && m_resizeDim.h) { - videoDecodeCreateInfo.display_area.left = pVideoFormat->display_area.left; - videoDecodeCreateInfo.display_area.top = pVideoFormat->display_area.top; - videoDecodeCreateInfo.display_area.right = pVideoFormat->display_area.right; - videoDecodeCreateInfo.display_area.bottom = pVideoFormat->display_area.bottom; - m_nWidth = m_resizeDim.w; - m_nLumaHeight = m_resizeDim.h; - } - - if (m_cropRect.r && m_cropRect.b) { - videoDecodeCreateInfo.display_area.left = m_cropRect.l; - videoDecodeCreateInfo.display_area.top = m_cropRect.t; - videoDecodeCreateInfo.display_area.right = m_cropRect.r; - videoDecodeCreateInfo.display_area.bottom = m_cropRect.b; - m_nWidth = m_cropRect.r - m_cropRect.l; - m_nLumaHeight = m_cropRect.b - m_cropRect.t; - } - videoDecodeCreateInfo.ulTargetWidth = m_nWidth; - videoDecodeCreateInfo.ulTargetHeight = m_nLumaHeight; - } - - m_nChromaHeight = (int)(m_nLumaHeight * GetChromaHeightFactor(videoDecodeCreateInfo.ChromaFormat)); - m_nNumChromaPlanes = GetChromaPlaneCount(videoDecodeCreateInfo.ChromaFormat); - m_nSurfaceHeight = videoDecodeCreateInfo.ulTargetHeight; - m_nSurfaceWidth = videoDecodeCreateInfo.ulTargetWidth; - m_displayRect.b = videoDecodeCreateInfo.display_area.bottom; - m_displayRect.t = videoDecodeCreateInfo.display_area.top; - m_displayRect.l = videoDecodeCreateInfo.display_area.left; - m_displayRect.r = videoDecodeCreateInfo.display_area.right; - - m_videoInfo << "Video Decoding Params:" << std::endl - << "\tNum Surfaces : " << videoDecodeCreateInfo.ulNumDecodeSurfaces << std::endl - << "\tCrop : [" << videoDecodeCreateInfo.display_area.left << ", " << videoDecodeCreateInfo.display_area.top << ", " - << videoDecodeCreateInfo.display_area.right << ", " << videoDecodeCreateInfo.display_area.bottom << "]" << std::endl - << "\tResize : " << videoDecodeCreateInfo.ulTargetWidth << "x" << videoDecodeCreateInfo.ulTargetHeight << std::endl - << "\tDeinterlace : " << std::vector<const char *>{"Weave", "Bob", "Adaptive"}[videoDecodeCreateInfo.DeinterlaceMode] - ; - m_videoInfo << std::endl; - - CUDA_DRVAPI_CALL(cuCtxPushCurrent(m_cuContext)); - NVDEC_API_CALL(cuvidCreateDecoder(&m_hDecoder, &videoDecodeCreateInfo)); - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); - STOP_TIMER("Session Initialization Time: "); - return nDecodeSurface; -} - -int NvDecoder::ReconfigureDecoder(CUVIDEOFORMAT *pVideoFormat) -{ - if (pVideoFormat->bit_depth_luma_minus8 != m_videoFormat.bit_depth_luma_minus8 || pVideoFormat->bit_depth_chroma_minus8 != m_videoFormat.bit_depth_chroma_minus8){ - - NVDEC_THROW_ERROR("Reconfigure Not supported for bit depth change", CUDA_ERROR_NOT_SUPPORTED); - } - - if (pVideoFormat->chroma_format != m_videoFormat.chroma_format) { - - NVDEC_THROW_ERROR("Reconfigure Not supported for chroma format change", CUDA_ERROR_NOT_SUPPORTED); - } - - bool bDecodeResChange = !(pVideoFormat->coded_width == m_videoFormat.coded_width && pVideoFormat->coded_height == m_videoFormat.coded_height); - bool bDisplayRectChange = !(pVideoFormat->display_area.bottom == m_videoFormat.display_area.bottom && pVideoFormat->display_area.top == m_videoFormat.display_area.top \ - && pVideoFormat->display_area.left == m_videoFormat.display_area.left && pVideoFormat->display_area.right == m_videoFormat.display_area.right); - - int nDecodeSurface = GetNumDecodeSurfaces(pVideoFormat->codec, pVideoFormat->coded_width, pVideoFormat->coded_height); - - if ((pVideoFormat->coded_width > m_nMaxWidth) || (pVideoFormat->coded_height > m_nMaxHeight)) { - // For VP9, let driver handle the change if new width/height > maxwidth/maxheight - if ((m_eCodec != cudaVideoCodec_VP9) || m_bReconfigExternal) - { - NVDEC_THROW_ERROR("Reconfigure Not supported when width/height > maxwidth/maxheight", CUDA_ERROR_NOT_SUPPORTED); - } - return 1; - } - - if (!bDecodeResChange && !m_bReconfigExtPPChange) { - // if the coded_width/coded_height hasn't changed but display resolution has changed, then need to update width/height for - // correct output without cropping. Example : 1920x1080 vs 1920x1088 - if (bDisplayRectChange) - { - m_nWidth = pVideoFormat->display_area.right - pVideoFormat->display_area.left; - m_nLumaHeight = pVideoFormat->display_area.bottom - pVideoFormat->display_area.top; - m_nChromaHeight = int(m_nLumaHeight * GetChromaHeightFactor(pVideoFormat->chroma_format)); - m_nNumChromaPlanes = GetChromaPlaneCount(pVideoFormat->chroma_format); - } - - // no need for reconfigureDecoder(). Just return - return 1; - } - - CUVIDRECONFIGUREDECODERINFO reconfigParams = { 0 }; - - reconfigParams.ulWidth = m_videoFormat.coded_width = pVideoFormat->coded_width; - reconfigParams.ulHeight = m_videoFormat.coded_height = pVideoFormat->coded_height; - - // Dont change display rect and get scaled output from decoder. This will help display app to present apps smoothly - reconfigParams.display_area.bottom = m_displayRect.b; - reconfigParams.display_area.top = m_displayRect.t; - reconfigParams.display_area.left = m_displayRect.l; - reconfigParams.display_area.right = m_displayRect.r; - reconfigParams.ulTargetWidth = m_nSurfaceWidth; - reconfigParams.ulTargetHeight = m_nSurfaceHeight; - - // If external reconfigure is called along with resolution change even if post processing params is not changed, - // do full reconfigure params update - if ((m_bReconfigExternal && bDecodeResChange) || m_bReconfigExtPPChange) { - // update display rect and target resolution if requested explicitely - m_bReconfigExternal = false; - m_bReconfigExtPPChange = false; - m_videoFormat = *pVideoFormat; - if (!(m_cropRect.r && m_cropRect.b) && !(m_resizeDim.w && m_resizeDim.h)) { - m_nWidth = pVideoFormat->display_area.right - pVideoFormat->display_area.left; - m_nLumaHeight = pVideoFormat->display_area.bottom - pVideoFormat->display_area.top; - reconfigParams.ulTargetWidth = pVideoFormat->coded_width; - reconfigParams.ulTargetHeight = pVideoFormat->coded_height; - } - else { - if (m_resizeDim.w && m_resizeDim.h) { - reconfigParams.display_area.left = pVideoFormat->display_area.left; - reconfigParams.display_area.top = pVideoFormat->display_area.top; - reconfigParams.display_area.right = pVideoFormat->display_area.right; - reconfigParams.display_area.bottom = pVideoFormat->display_area.bottom; - m_nWidth = m_resizeDim.w; - m_nLumaHeight = m_resizeDim.h; - } - - if (m_cropRect.r && m_cropRect.b) { - reconfigParams.display_area.left = m_cropRect.l; - reconfigParams.display_area.top = m_cropRect.t; - reconfigParams.display_area.right = m_cropRect.r; - reconfigParams.display_area.bottom = m_cropRect.b; - m_nWidth = m_cropRect.r - m_cropRect.l; - m_nLumaHeight = m_cropRect.b - m_cropRect.t; - } - reconfigParams.ulTargetWidth = m_nWidth; - reconfigParams.ulTargetHeight = m_nLumaHeight; - } - - m_nChromaHeight = int(m_nLumaHeight * GetChromaHeightFactor(pVideoFormat->chroma_format)); - m_nNumChromaPlanes = GetChromaPlaneCount(pVideoFormat->chroma_format); - m_nSurfaceHeight = reconfigParams.ulTargetHeight; - m_nSurfaceWidth = reconfigParams.ulTargetWidth; - m_displayRect.b = reconfigParams.display_area.bottom; - m_displayRect.t = reconfigParams.display_area.top; - m_displayRect.l = reconfigParams.display_area.left; - m_displayRect.r = reconfigParams.display_area.right; - } - - reconfigParams.ulNumDecodeSurfaces = nDecodeSurface; - - START_TIMER - CUDA_DRVAPI_CALL(cuCtxPushCurrent(m_cuContext)); - NVDEC_API_CALL(cuvidReconfigureDecoder(m_hDecoder, &reconfigParams)); - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); - STOP_TIMER("Session Reconfigure Time: "); - - return nDecodeSurface; -} - -int NvDecoder::setReconfigParams(const Rect *pCropRect, const Dim *pResizeDim) -{ - m_bReconfigExternal = true; - m_bReconfigExtPPChange = false; - if (pCropRect) - { - if (!((pCropRect->t == m_cropRect.t) && (pCropRect->l == m_cropRect.l) && - (pCropRect->b == m_cropRect.b) && (pCropRect->r == m_cropRect.r))) - { - m_bReconfigExtPPChange = true; - m_cropRect = *pCropRect; - } - } - if (pResizeDim) - { - if (!((pResizeDim->w == m_resizeDim.w) && (pResizeDim->h == m_resizeDim.h))) - { - m_bReconfigExtPPChange = true; - m_resizeDim = *pResizeDim; - } - } - - // Clear existing output buffers of different size - uint8_t *pFrame = NULL; - while (!m_vpFrame.empty()) - { - pFrame = m_vpFrame.back(); - m_vpFrame.pop_back(); - if (m_bUseDeviceFrame) - { - CUDA_DRVAPI_CALL(cuCtxPushCurrent(m_cuContext)); - CUDA_DRVAPI_CALL(cuMemFree((CUdeviceptr)pFrame)); - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); - } - else - { - delete pFrame; - } - } - m_vpFrameRet.clear(); - - return 1; -} - -/* Return value from HandlePictureDecode() are interpreted as: -* 0: fail, >=1: suceeded -*/ -int NvDecoder::HandlePictureDecode(CUVIDPICPARAMS *pPicParams) { - if (!m_hDecoder) - { - NVDEC_THROW_ERROR("Decoder not initialized.", CUDA_ERROR_NOT_INITIALIZED); - return false; - } - m_nPicNumInDecodeOrder[pPicParams->CurrPicIdx] = m_nDecodePicCnt++; - NVDEC_API_CALL(cuvidDecodePicture(m_hDecoder, pPicParams)); - return 1; -} - -/* Return value from HandlePictureDisplay() are interpreted as: -* 0: fail, >=1: suceeded -*/ -int NvDecoder::HandlePictureDisplay(CUVIDPARSERDISPINFO *pDispInfo) { - CUVIDPROCPARAMS videoProcessingParameters = {}; - videoProcessingParameters.progressive_frame = pDispInfo->progressive_frame; - videoProcessingParameters.second_field = pDispInfo->repeat_first_field + 1; - videoProcessingParameters.top_field_first = pDispInfo->top_field_first; - videoProcessingParameters.unpaired_field = pDispInfo->repeat_first_field < 0; - videoProcessingParameters.output_stream = m_cuvidStream; - - CUdeviceptr dpSrcFrame = 0; - unsigned int nSrcPitch = 0; - NVDEC_API_CALL(cuvidMapVideoFrame(m_hDecoder, pDispInfo->picture_index, &dpSrcFrame, - &nSrcPitch, &videoProcessingParameters)); - - CUVIDGETDECODESTATUS DecodeStatus; - memset(&DecodeStatus, 0, sizeof(DecodeStatus)); - CUresult result = cuvidGetDecodeStatus(m_hDecoder, pDispInfo->picture_index, &DecodeStatus); - if (result == CUDA_SUCCESS && (DecodeStatus.decodeStatus == cuvidDecodeStatus_Error || DecodeStatus.decodeStatus == cuvidDecodeStatus_Error_Concealed)) - { - printf("Decode Error occurred for picture %d\n", m_nPicNumInDecodeOrder[pDispInfo->picture_index]); - } - - uint8_t *pDecodedFrame = nullptr; - { - std::lock_guard<std::mutex> lock(m_mtxVPFrame); - if ((unsigned)++m_nDecodedFrame > m_vpFrame.size()) - { - // Not enough frames in stock - m_nFrameAlloc++; - uint8_t *pFrame = NULL; - if (m_bUseDeviceFrame) - { - CUDA_DRVAPI_CALL(cuCtxPushCurrent(m_cuContext)); - if (m_bDeviceFramePitched) - { - CUDA_DRVAPI_CALL(cuMemAllocPitch((CUdeviceptr *)&pFrame, &m_nDeviceFramePitch, m_nWidth * m_nBPP, m_nLumaHeight + (m_nChromaHeight * m_nNumChromaPlanes), 16)); - } - else - { - CUDA_DRVAPI_CALL(cuMemAlloc((CUdeviceptr *)&pFrame, GetFrameSize())); - } - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); - } - else - { - pFrame = new uint8_t[GetFrameSize()]; - } - m_vpFrame.push_back(pFrame); - } - pDecodedFrame = m_vpFrame[m_nDecodedFrame - 1]; - } - - CUDA_DRVAPI_CALL(cuCtxPushCurrent(m_cuContext)); - CUDA_MEMCPY2D m = { 0 }; - m.srcMemoryType = CU_MEMORYTYPE_DEVICE; - m.srcDevice = dpSrcFrame; - m.srcPitch = nSrcPitch; - m.dstMemoryType = m_bUseDeviceFrame ? CU_MEMORYTYPE_DEVICE : CU_MEMORYTYPE_HOST; - m.dstDevice = (CUdeviceptr)(m.dstHost = pDecodedFrame); - m.dstPitch = m_nDeviceFramePitch ? m_nDeviceFramePitch : m_nWidth * m_nBPP; - m.WidthInBytes = m_nWidth * m_nBPP; - m.Height = m_nLumaHeight; - CUDA_DRVAPI_CALL(cuMemcpy2DAsync(&m, m_cuvidStream)); - - m.srcDevice = (CUdeviceptr)((uint8_t *)dpSrcFrame + m.srcPitch * m_nSurfaceHeight); - m.dstDevice = (CUdeviceptr)(m.dstHost = pDecodedFrame + m.dstPitch * m_nLumaHeight); - m.Height = m_nChromaHeight; - CUDA_DRVAPI_CALL(cuMemcpy2DAsync(&m, m_cuvidStream)); - - if (m_nNumChromaPlanes == 2) - { - m.srcDevice = (CUdeviceptr)((uint8_t *)dpSrcFrame + m.srcPitch * m_nSurfaceHeight * 2); - m.dstDevice = (CUdeviceptr)(m.dstHost = pDecodedFrame + m.dstPitch * m_nLumaHeight * 2); - m.Height = m_nChromaHeight; - CUDA_DRVAPI_CALL(cuMemcpy2DAsync(&m, m_cuvidStream)); - } - CUDA_DRVAPI_CALL(cuStreamSynchronize(m_cuvidStream)); - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); - - if ((int)m_vTimestamp.size() < m_nDecodedFrame) { - m_vTimestamp.resize(m_vpFrame.size()); - } - m_vTimestamp[m_nDecodedFrame - 1] = pDispInfo->timestamp; - - NVDEC_API_CALL(cuvidUnmapVideoFrame(m_hDecoder, dpSrcFrame)); - return 1; -} - -NvDecoder::NvDecoder(CUcontext cuContext, int nWidth, int nHeight, bool bUseDeviceFrame, cudaVideoCodec eCodec, std::mutex *pMutex, - bool bLowLatency, bool bDeviceFramePitched, const Rect *pCropRect, const Dim *pResizeDim, int maxWidth, int maxHeight) : - m_cuContext(cuContext), m_bUseDeviceFrame(bUseDeviceFrame), m_eCodec(eCodec), m_pMutex(pMutex), m_bDeviceFramePitched(bDeviceFramePitched), - m_nMaxWidth (maxWidth), m_nMaxHeight(maxHeight) -{ - if (pCropRect) m_cropRect = *pCropRect; - if (pResizeDim) m_resizeDim = *pResizeDim; - - NVDEC_API_CALL(cuvidCtxLockCreate(&m_ctxLock, cuContext)); - - CUVIDPARSERPARAMS videoParserParameters = {}; - videoParserParameters.CodecType = eCodec; - videoParserParameters.ulMaxNumDecodeSurfaces = 1; - videoParserParameters.ulMaxDisplayDelay = bLowLatency ? 0 : 1; - videoParserParameters.pUserData = this; - videoParserParameters.pfnSequenceCallback = HandleVideoSequenceProc; - videoParserParameters.pfnDecodePicture = HandlePictureDecodeProc; - videoParserParameters.pfnDisplayPicture = HandlePictureDisplayProc; - if (m_pMutex) m_pMutex->lock(); - NVDEC_API_CALL(cuvidCreateVideoParser(&m_hParser, &videoParserParameters)); - if (m_pMutex) m_pMutex->unlock(); -} - -NvDecoder::~NvDecoder() { - - START_TIMER - cuCtxPushCurrent(m_cuContext); - cuCtxPopCurrent(NULL); - - if (m_hParser) { - cuvidDestroyVideoParser(m_hParser); - } - - if (m_hDecoder) { - if (m_pMutex) m_pMutex->lock(); - cuvidDestroyDecoder(m_hDecoder); - if (m_pMutex) m_pMutex->unlock(); - } - - std::lock_guard<std::mutex> lock(m_mtxVPFrame); - if (m_vpFrame.size() != m_nFrameAlloc) - { - //LOG(WARNING) << "nFrameAlloc(" << m_nFrameAlloc << ") != m_vpFrame.size()(" << m_vpFrame.size() << ")"; - } - for (uint8_t *pFrame : m_vpFrame) - { - if (m_bUseDeviceFrame) - { - if (m_pMutex) m_pMutex->lock(); - cuCtxPushCurrent(m_cuContext); - cuMemFree((CUdeviceptr)pFrame); - cuCtxPopCurrent(NULL); - if (m_pMutex) m_pMutex->unlock(); - } - else - { - delete[] pFrame; - } - } - cuvidCtxLockDestroy(m_ctxLock); - STOP_TIMER("Session Deinitialization Time: "); -} - -bool NvDecoder::Decode(const uint8_t *pData, int nSize, uint8_t ***pppFrame, int *pnFrameReturned, uint32_t flags, int64_t **ppTimestamp, int64_t timestamp, CUstream stream) -{ - if (!m_hParser) - { - NVDEC_THROW_ERROR("Parser not initialized.", CUDA_ERROR_NOT_INITIALIZED); - return false; - } - - m_nDecodedFrame = 0; - CUVIDSOURCEDATAPACKET packet = {0}; - packet.payload = pData; - packet.payload_size = nSize; - packet.flags = flags | CUVID_PKT_TIMESTAMP; - packet.timestamp = timestamp; - if (!pData || nSize == 0) { - packet.flags |= CUVID_PKT_ENDOFSTREAM; - } - m_cuvidStream = stream; - if (m_pMutex) m_pMutex->lock(); - NVDEC_API_CALL(cuvidParseVideoData(m_hParser, &packet)); - if (m_pMutex) m_pMutex->unlock(); - m_cuvidStream = 0; - - if (m_nDecodedFrame > 0) - { - if (pppFrame) - { - m_vpFrameRet.clear(); - std::lock_guard<std::mutex> lock(m_mtxVPFrame); - m_vpFrameRet.insert(m_vpFrameRet.begin(), m_vpFrame.begin(), m_vpFrame.begin() + m_nDecodedFrame); - *pppFrame = &m_vpFrameRet[0]; - } - if (ppTimestamp) - { - *ppTimestamp = &m_vTimestamp[0]; - } - } - if (pnFrameReturned) - { - *pnFrameReturned = m_nDecodedFrame; - } - return true; -} - -bool NvDecoder::DecodeLockFrame(const uint8_t *pData, int nSize, uint8_t ***pppFrame, int *pnFrameReturned, uint32_t flags, int64_t **ppTimestamp, int64_t timestamp, CUstream stream) -{ - bool ret = Decode(pData, nSize, pppFrame, pnFrameReturned, flags, ppTimestamp, timestamp, stream); - std::lock_guard<std::mutex> lock(m_mtxVPFrame); - m_vpFrame.erase(m_vpFrame.begin(), m_vpFrame.begin() + m_nDecodedFrame); - return true; -} - -void NvDecoder::UnlockFrame(uint8_t **ppFrame, int nFrame) -{ - std::lock_guard<std::mutex> lock(m_mtxVPFrame); - m_vpFrame.insert(m_vpFrame.end(), &ppFrame[0], &ppFrame[nFrame]); -} diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvDecoder/NvDecoder.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvDecoder/NvDecoder.h deleted file mode 100644 index 05771a5cd8a992b2c9eea8992ab1cce3e1f6a087..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvDecoder/NvDecoder.h +++ /dev/null @@ -1,279 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include <assert.h> -#include <stdint.h> -#include <mutex> -#include <vector> -#include <string> -#include <iostream> -#include <sstream> -#include <string.h> -#include "nvcuvid.h" - -/** -* @brief Exception class for error reporting from the decode API. -*/ -class NVDECException : public std::exception -{ -public: - NVDECException(const std::string& errorStr, const CUresult errorCode) - : m_errorString(errorStr), m_errorCode(errorCode) {} - - virtual ~NVDECException() throw() {} - virtual const char* what() const throw() { return m_errorString.c_str(); } - CUresult getErrorCode() const { return m_errorCode; } - const std::string& getErrorString() const { return m_errorString; } - static NVDECException makeNVDECException(const std::string& errorStr, const CUresult errorCode, - const std::string& functionName, const std::string& fileName, int lineNo); -private: - std::string m_errorString; - CUresult m_errorCode; -}; - -inline NVDECException NVDECException::makeNVDECException(const std::string& errorStr, const CUresult errorCode, const std::string& functionName, - const std::string& fileName, int lineNo) -{ - std::ostringstream errorLog; - errorLog << functionName << " : " << errorStr << " at " << fileName << ":" << lineNo << std::endl; - NVDECException exception(errorLog.str(), errorCode); - return exception; -} - -#define NVDEC_THROW_ERROR( errorStr, errorCode ) \ - do \ - { \ - throw NVDECException::makeNVDECException(errorStr, errorCode, __FUNCTION__, __FILE__, __LINE__); \ - } while (0) - - -#define NVDEC_API_CALL( cuvidAPI ) \ - do \ - { \ - CUresult errorCode = cuvidAPI; \ - if( errorCode != CUDA_SUCCESS) \ - { \ - std::ostringstream errorLog; \ - errorLog << #cuvidAPI << " returned error " << errorCode; \ - throw NVDECException::makeNVDECException(errorLog.str(), errorCode, __FUNCTION__, __FILE__, __LINE__); \ - } \ - } while (0) - -struct Rect { - int l, t, r, b; -}; - -struct Dim { - int w, h; -}; - -/** -* @brief Base class for decoder interface. -*/ -class NvDecoder { - -public: - /** - * @brief This function is used to initialize the decoder session. - * Application must call this function to initialize the decoder, before - * starting to decode any frames. - */ - NvDecoder(CUcontext cuContext, int nWidth, int nHeight, bool bUseDeviceFrame, cudaVideoCodec eCodec, std::mutex *pMutex = NULL, - bool bLowLatency = false, bool bDeviceFramePitched = false, const Rect *pCropRect = NULL, const Dim *pResizeDim = NULL, int maxWidth = 0, int maxHeight = 0); - ~NvDecoder(); - - /** - * @brief This function is used to get the current CUDA context. - */ - CUcontext GetContext() { return m_cuContext; } - - /** - * @brief This function is used to get the current decode width. - */ - int GetWidth() { assert(m_nWidth); return m_nWidth; } - - /** - * @brief This function is used to get the current decode height (Luma height). - */ - int GetHeight() { assert(m_nLumaHeight); return m_nLumaHeight; } - - /** - * @brief This function is used to get the current chroma height. - */ - int GetChromaHeight() { assert(m_nChromaHeight); return m_nChromaHeight; } - - /** - * @brief This function is used to get the number of chroma planes. - */ - int GetNumChromaPlanes() { assert(m_nNumChromaPlanes); return m_nNumChromaPlanes; } - - /** - * @brief This function is used to get the current frame size based on pixel format. - */ - int GetFrameSize() { assert(m_nWidth); return m_nWidth * (m_nLumaHeight + m_nChromaHeight * m_nNumChromaPlanes) * m_nBPP; } - - /** - * @brief This function is used to get the pitch of the device buffer holding the decoded frame. - */ - int GetDeviceFramePitch() { assert(m_nWidth); return m_nDeviceFramePitch ? (int)m_nDeviceFramePitch : m_nWidth * m_nBPP; } - - /** - * @brief This function is used to get the bit depth associated with the pixel format. - */ - int GetBitDepth() { assert(m_nWidth); return m_nBitDepthMinus8 + 8; } - - /** - * @brief This function is used to get the bytes used per pixel. - */ - int GetBPP() { assert(m_nWidth); return m_nBPP; } - - /** - * @brief This function is used to get the YUV chroma format - */ - cudaVideoSurfaceFormat GetOutputFormat() { return m_eOutputFormat; } - - /** - * @brief This function is used to get information about the video stream (codec, display parameters etc) - */ - CUVIDEOFORMAT GetVideoFormatInfo() { assert(m_nWidth); return m_videoFormat; } - - /** - * @brief This function is used to print information about the video stream - */ - std::string GetVideoInfo() const { return m_videoInfo.str(); } - - /** - * @brief This function decodes a frame and returns frames that are available for display. - The frames should be used or buffered before making subsequent calls to the Decode function again - * @param pData - pointer to the data buffer that is to be decoded - * @param nSize - size of the data buffer in bytes - * @param pppFrame - CUvideopacketflags for setting decode options - * @param pnFrameReturned - pointer to array of decoded frames that are returned - * @param flags - CUvideopacketflags for setting decode options - * @param ppTimestamp - pointer to array of timestamps for decoded frames that are returned - * @param timestamp - presentation timestamp - * @param stream - CUstream to be used for post-processing operations - */ - bool Decode(const uint8_t *pData, int nSize, uint8_t ***pppFrame, int *pnFrameReturned, uint32_t flags = 0, int64_t **ppTimestamp = NULL, int64_t timestamp = 0, CUstream stream = 0); - - /** - * @brief This function decodes a frame and returns the locked frame buffers - * This makes the buffers available for use by the application without the buffers - * getting overwritten, even if subsequent decode calls are made. The frame buffers - * remain locked, until ::UnlockFrame() is called - * @param pData - pointer to the data buffer that is to be decoded - * @param nSize - size of the data buffer in bytes - * @param pppFrame - CUvideopacketflags for setting decode options - * @param pnFrameReturned - pointer to array of decoded frames that are returned - * @param flags - CUvideopacketflags for setting decode options - * @param ppTimestamp - pointer to array of timestamps for decoded frames that are returned - * @param timestamp - presentation timestamp - * @param stream - CUstream to be used for post-processing operations - */ - bool DecodeLockFrame(const uint8_t *pData, int nSize, uint8_t ***pppFrame, int *pnFrameReturned, uint32_t flags = 0, int64_t **ppTimestamp = NULL, int64_t timestamp = 0, CUstream stream = 0); - - /** - * @brief This function unlocks the frame buffer and makes the frame buffers available for write again - * @param ppFrame - pointer to array of frames that are to be unlocked - * @param nFrame - number of frames to be unlocked - */ - void UnlockFrame(uint8_t **ppFrame, int nFrame); - - /** - * @brief This function allow app to set decoder reconfig params - * @param pCropRect - cropping rectangle coordinates - * @param pResizeDim - width and height of resized output - */ - int setReconfigParams(const Rect * pCropRect, const Dim * pResizeDim); - -private: - /** - * @brief Callback function to be registered for getting a callback when decoding of sequence starts - */ - static int CUDAAPI HandleVideoSequenceProc(void *pUserData, CUVIDEOFORMAT *pVideoFormat) { return ((NvDecoder *)pUserData)->HandleVideoSequence(pVideoFormat); } - - /** - * @brief Callback function to be registered for getting a callback when a decoded frame is ready to be decoded - */ - static int CUDAAPI HandlePictureDecodeProc(void *pUserData, CUVIDPICPARAMS *pPicParams) { return ((NvDecoder *)pUserData)->HandlePictureDecode(pPicParams); } - - /** - * @brief Callback function to be registered for getting a callback when a decoded frame is available for display - */ - static int CUDAAPI HandlePictureDisplayProc(void *pUserData, CUVIDPARSERDISPINFO *pDispInfo) { return ((NvDecoder *)pUserData)->HandlePictureDisplay(pDispInfo); } - - /** - * @brief This function gets called when a sequence is ready to be decoded. The function also gets called - when there is format change - */ - int HandleVideoSequence(CUVIDEOFORMAT *pVideoFormat); - - /** - * @brief This function gets called when a picture is ready to be decoded. cuvidDecodePicture is called from this function - * to decode the picture - */ - int HandlePictureDecode(CUVIDPICPARAMS *pPicParams); - - /** - * @brief This function gets called after a picture is decoded and available for display. Frames are fetched and stored in - internal buffer - */ - int HandlePictureDisplay(CUVIDPARSERDISPINFO *pDispInfo); - - /** - * @brief This function reconfigure decoder if there is a change in sequence params. - */ - int ReconfigureDecoder(CUVIDEOFORMAT *pVideoFormat); - -private: - CUcontext m_cuContext = NULL; - CUvideoctxlock m_ctxLock; - std::mutex *m_pMutex; - CUvideoparser m_hParser = NULL; - CUvideodecoder m_hDecoder = NULL; - bool m_bUseDeviceFrame; - // dimension of the output - unsigned int m_nWidth = 0, m_nLumaHeight = 0, m_nChromaHeight = 0; - unsigned int m_nNumChromaPlanes = 0; - // height of the mapped surface - int m_nSurfaceHeight = 0; - int m_nSurfaceWidth = 0; - cudaVideoCodec m_eCodec = cudaVideoCodec_NumCodecs; - cudaVideoChromaFormat m_eChromaFormat; - cudaVideoSurfaceFormat m_eOutputFormat; - int m_nBitDepthMinus8 = 0; - int m_nBPP = 1; - CUVIDEOFORMAT m_videoFormat = {}; - Rect m_displayRect = {}; - // stock of frames - std::vector<uint8_t *> m_vpFrame; - // decoded frames for return - std::vector<uint8_t *> m_vpFrameRet; - // timestamps of decoded frames - std::vector<int64_t> m_vTimestamp; - int m_nDecodedFrame = 0, m_nDecodedFrameReturned = 0; - int m_nDecodePicCnt = 0, m_nPicNumInDecodeOrder[32]; - bool m_bEndDecodeDone = false; - std::mutex m_mtxVPFrame; - int m_nFrameAlloc = 0; - CUstream m_cuvidStream = 0; - bool m_bDeviceFramePitched = false; - size_t m_nDeviceFramePitch = 0; - Rect m_cropRect = {}; - Dim m_resizeDim = {}; - - std::ostringstream m_videoInfo; - unsigned int m_nMaxWidth = 0, m_nMaxHeight = 0; - bool m_bReconfigExternal = false; - bool m_bReconfigExtPPChange = false; -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoder.cpp b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoder.cpp deleted file mode 100644 index e6827eb60d0b24f036ea3da1d8a2906d99a2996b..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoder.cpp +++ /dev/null @@ -1,976 +0,0 @@ -/* -* Copyright 2017-2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#ifndef WIN32 -#include <dlfcn.h> -#endif -#include "NvEncoder/NvEncoder.h" - -#ifndef _WIN32 -#include <cstring> -static inline bool operator==(const GUID &guid1, const GUID &guid2) { - return !memcmp(&guid1, &guid2, sizeof(GUID)); -} - -static inline bool operator!=(const GUID &guid1, const GUID &guid2) { - return !(guid1 == guid2); -} -#endif - -NvEncoder::NvEncoder(NV_ENC_DEVICE_TYPE eDeviceType, void *pDevice, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay, bool bMotionEstimationOnly, bool bOutputInVideoMemory) : - m_pDevice(pDevice), - m_eDeviceType(eDeviceType), - m_nWidth(nWidth), - m_nHeight(nHeight), - m_nMaxEncodeWidth(nWidth), - m_nMaxEncodeHeight(nHeight), - m_eBufferFormat(eBufferFormat), - m_bMotionEstimationOnly(bMotionEstimationOnly), - m_bOutputInVideoMemory(bOutputInVideoMemory), - m_nExtraOutputDelay(nExtraOutputDelay), - m_hEncoder(nullptr) -{ - LoadNvEncApi(); - - if (!m_nvenc.nvEncOpenEncodeSession) - { - m_nEncoderBuffer = 0; - NVENC_THROW_ERROR("EncodeAPI not found", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encodeSessionExParams = { NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER }; - encodeSessionExParams.device = m_pDevice; - encodeSessionExParams.deviceType = m_eDeviceType; - encodeSessionExParams.apiVersion = NVENCAPI_VERSION; - void *hEncoder = NULL; - NVENC_API_CALL(m_nvenc.nvEncOpenEncodeSessionEx(&encodeSessionExParams, &hEncoder)); - m_hEncoder = hEncoder; -} - -void NvEncoder::LoadNvEncApi() -{ -#if defined(_WIN32) -#if defined(_WIN64) - HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI64.dll")); -#else - HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI.dll")); -#endif -#else - void *hModule = dlopen("libnvidia-encode.so.1", RTLD_LAZY); -#endif - - if (hModule == NULL) - { - NVENC_THROW_ERROR("NVENC library file is not found. Please ensure NV driver is installed", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - m_hModule = hModule; - - typedef NVENCSTATUS(NVENCAPI *NvEncodeAPIGetMaxSupportedVersion_Type)(uint32_t*); -#if defined(_WIN32) - NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = (NvEncodeAPIGetMaxSupportedVersion_Type)GetProcAddress(hModule, "NvEncodeAPIGetMaxSupportedVersion"); -#else - NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = (NvEncodeAPIGetMaxSupportedVersion_Type)dlsym(hModule, "NvEncodeAPIGetMaxSupportedVersion"); -#endif - - uint32_t version = 0; - uint32_t currentVersion = (NVENCAPI_MAJOR_VERSION << 4) | NVENCAPI_MINOR_VERSION; - NVENC_API_CALL(NvEncodeAPIGetMaxSupportedVersion(&version)); - if (currentVersion > version) - { - NVENC_THROW_ERROR("Current Driver Version does not support this NvEncodeAPI version, please upgrade driver", NV_ENC_ERR_INVALID_VERSION); - } - - typedef NVENCSTATUS(NVENCAPI *NvEncodeAPICreateInstance_Type)(NV_ENCODE_API_FUNCTION_LIST*); -#if defined(_WIN32) - NvEncodeAPICreateInstance_Type NvEncodeAPICreateInstance = (NvEncodeAPICreateInstance_Type)GetProcAddress(hModule, "NvEncodeAPICreateInstance"); -#else - NvEncodeAPICreateInstance_Type NvEncodeAPICreateInstance = (NvEncodeAPICreateInstance_Type)dlsym(hModule, "NvEncodeAPICreateInstance"); -#endif - - if (!NvEncodeAPICreateInstance) - { - NVENC_THROW_ERROR("Cannot find NvEncodeAPICreateInstance() entry in NVENC library", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - m_nvenc = { NV_ENCODE_API_FUNCTION_LIST_VER }; - NVENC_API_CALL(NvEncodeAPICreateInstance(&m_nvenc)); -} - -NvEncoder::~NvEncoder() -{ - DestroyHWEncoder(); - - if (m_hModule) - { -#if defined(_WIN32) - FreeLibrary((HMODULE)m_hModule); -#else - dlclose(m_hModule); -#endif - m_hModule = nullptr; - } -} - -void NvEncoder::CreateDefaultEncoderParams(NV_ENC_INITIALIZE_PARAMS* pIntializeParams, GUID codecGuid, GUID presetGuid) -{ - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_NO_ENCODE_DEVICE); - return; - } - - if (pIntializeParams == nullptr || pIntializeParams->encodeConfig == nullptr) - { - NVENC_THROW_ERROR("pInitializeParams and pInitializeParams->encodeConfig can't be NULL", NV_ENC_ERR_INVALID_PTR); - } - - memset(pIntializeParams->encodeConfig, 0, sizeof(NV_ENC_CONFIG)); - auto pEncodeConfig = pIntializeParams->encodeConfig; - memset(pIntializeParams, 0, sizeof(NV_ENC_INITIALIZE_PARAMS)); - pIntializeParams->encodeConfig = pEncodeConfig; - - - pIntializeParams->encodeConfig->version = NV_ENC_CONFIG_VER; - pIntializeParams->version = NV_ENC_INITIALIZE_PARAMS_VER; - - pIntializeParams->encodeGUID = codecGuid; - pIntializeParams->presetGUID = presetGuid; - pIntializeParams->encodeWidth = m_nWidth; - pIntializeParams->encodeHeight = m_nHeight; - pIntializeParams->darWidth = m_nWidth; - pIntializeParams->darHeight = m_nHeight; - pIntializeParams->frameRateNum = 30; - pIntializeParams->frameRateDen = 1; - pIntializeParams->enablePTD = 1; - pIntializeParams->reportSliceOffsets = 0; - pIntializeParams->enableSubFrameWrite = 0; - pIntializeParams->maxEncodeWidth = m_nWidth; - pIntializeParams->maxEncodeHeight = m_nHeight; - pIntializeParams->enableMEOnlyMode = m_bMotionEstimationOnly; - pIntializeParams->enableOutputInVidmem = m_bOutputInVideoMemory; -#if defined(_WIN32) - if (!m_bOutputInVideoMemory) - { - pIntializeParams->enableEncodeAsync = GetCapabilityValue(codecGuid, NV_ENC_CAPS_ASYNC_ENCODE_SUPPORT); - } -#endif - - NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } }; - m_nvenc.nvEncGetEncodePresetConfig(m_hEncoder, codecGuid, presetGuid, &presetConfig); - memcpy(pIntializeParams->encodeConfig, &presetConfig.presetCfg, sizeof(NV_ENC_CONFIG)); - pIntializeParams->encodeConfig->frameIntervalP = 1; - pIntializeParams->encodeConfig->gopLength = NVENC_INFINITE_GOPLENGTH; - - pIntializeParams->encodeConfig->rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; - - if (pIntializeParams->presetGUID != NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID - && pIntializeParams->presetGUID != NV_ENC_PRESET_LOSSLESS_HP_GUID) - { - pIntializeParams->encodeConfig->rcParams.constQP = { 28, 31, 25 }; - } - - if (pIntializeParams->encodeGUID == NV_ENC_CODEC_H264_GUID) - { - if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) - { - pIntializeParams->encodeConfig->encodeCodecConfig.h264Config.chromaFormatIDC = 3; - } - pIntializeParams->encodeConfig->encodeCodecConfig.h264Config.idrPeriod = pIntializeParams->encodeConfig->gopLength; - } - else if (pIntializeParams->encodeGUID == NV_ENC_CODEC_HEVC_GUID) - { - pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig.pixelBitDepthMinus8 = - (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT ) ? 2 : 0; - if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) - { - pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig.chromaFormatIDC = 3; - } - pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig.idrPeriod = pIntializeParams->encodeConfig->gopLength; - } - - return; -} - -void NvEncoder::CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams) -{ - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - if (!pEncoderParams) - { - NVENC_THROW_ERROR("Invalid NV_ENC_INITIALIZE_PARAMS ptr", NV_ENC_ERR_INVALID_PTR); - } - - if (pEncoderParams->encodeWidth == 0 || pEncoderParams->encodeHeight == 0) - { - NVENC_THROW_ERROR("Invalid encoder width and height", NV_ENC_ERR_INVALID_PARAM); - } - - if (pEncoderParams->encodeGUID != NV_ENC_CODEC_H264_GUID && pEncoderParams->encodeGUID != NV_ENC_CODEC_HEVC_GUID) - { - NVENC_THROW_ERROR("Invalid codec guid", NV_ENC_ERR_INVALID_PARAM); - } - - if (pEncoderParams->encodeGUID == NV_ENC_CODEC_H264_GUID) - { - if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) - { - NVENC_THROW_ERROR("10-bit format isn't supported by H264 encoder", NV_ENC_ERR_INVALID_PARAM); - } - } - - // set other necessary params if not set yet - if (pEncoderParams->encodeGUID == NV_ENC_CODEC_H264_GUID) - { - if ((m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444) && - (pEncoderParams->encodeConfig->encodeCodecConfig.h264Config.chromaFormatIDC != 3)) - { - NVENC_THROW_ERROR("Invalid ChromaFormatIDC", NV_ENC_ERR_INVALID_PARAM); - } - } - - if (pEncoderParams->encodeGUID == NV_ENC_CODEC_HEVC_GUID) - { - bool yuv10BitFormat = (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) ? true : false; - if (yuv10BitFormat && pEncoderParams->encodeConfig->encodeCodecConfig.hevcConfig.pixelBitDepthMinus8 != 2) - { - NVENC_THROW_ERROR("Invalid PixelBitdepth", NV_ENC_ERR_INVALID_PARAM); - } - - if ((m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) && - (pEncoderParams->encodeConfig->encodeCodecConfig.hevcConfig.chromaFormatIDC != 3)) - { - NVENC_THROW_ERROR("Invalid ChromaFormatIDC", NV_ENC_ERR_INVALID_PARAM); - } - } - - memcpy(&m_initializeParams, pEncoderParams, sizeof(m_initializeParams)); - m_initializeParams.version = NV_ENC_INITIALIZE_PARAMS_VER; - - if (pEncoderParams->encodeConfig) - { - memcpy(&m_encodeConfig, pEncoderParams->encodeConfig, sizeof(m_encodeConfig)); - m_encodeConfig.version = NV_ENC_CONFIG_VER; - } - else - { - NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } }; - m_nvenc.nvEncGetEncodePresetConfig(m_hEncoder, pEncoderParams->encodeGUID, NV_ENC_PRESET_DEFAULT_GUID, &presetConfig); - memcpy(&m_encodeConfig, &presetConfig.presetCfg, sizeof(NV_ENC_CONFIG)); - m_encodeConfig.version = NV_ENC_CONFIG_VER; - m_encodeConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; - m_encodeConfig.rcParams.constQP = { 28, 31, 25 }; - } - m_initializeParams.encodeConfig = &m_encodeConfig; - - NVENC_API_CALL(m_nvenc.nvEncInitializeEncoder(m_hEncoder, &m_initializeParams)); - - m_bEncoderInitialized = true; - m_nWidth = m_initializeParams.encodeWidth; - m_nHeight = m_initializeParams.encodeHeight; - m_nMaxEncodeWidth = m_initializeParams.maxEncodeWidth; - m_nMaxEncodeHeight = m_initializeParams.maxEncodeHeight; - - m_nEncoderBuffer = m_encodeConfig.frameIntervalP + m_encodeConfig.rcParams.lookaheadDepth + m_nExtraOutputDelay; - m_nOutputDelay = m_nEncoderBuffer - 1; - m_vMappedInputBuffers.resize(m_nEncoderBuffer, nullptr); - - if (!m_bOutputInVideoMemory) - { - m_vpCompletionEvent.resize(m_nEncoderBuffer, nullptr); - } - -#if defined(_WIN32) - for (int i = 0; i < m_vpCompletionEvent.size(); i++) - { - m_vpCompletionEvent[i] = CreateEvent(NULL, FALSE, FALSE, NULL); - NV_ENC_EVENT_PARAMS eventParams = { NV_ENC_EVENT_PARAMS_VER }; - eventParams.completionEvent = m_vpCompletionEvent[i]; - m_nvenc.nvEncRegisterAsyncEvent(m_hEncoder, &eventParams); - } -#endif - - if (m_bMotionEstimationOnly) - { - m_vMappedRefBuffers.resize(m_nEncoderBuffer, nullptr); - - if (!m_bOutputInVideoMemory) - { - InitializeMVOutputBuffer(); - } - } - else - { - if (!m_bOutputInVideoMemory) - { - m_vBitstreamOutputBuffer.resize(m_nEncoderBuffer, nullptr); - InitializeBitstreamBuffer(); - } - } - - AllocateInputBuffers(m_nEncoderBuffer); -} - -void NvEncoder::DestroyEncoder() -{ - if (!m_hEncoder) - { - return; - } - - ReleaseInputBuffers(); - - DestroyHWEncoder(); -} - -void NvEncoder::DestroyHWEncoder() -{ - if (!m_hEncoder) - { - return; - } - -#if defined(_WIN32) - for (uint32_t i = 0; i < m_vpCompletionEvent.size(); i++) - { - if (m_vpCompletionEvent[i]) - { - NV_ENC_EVENT_PARAMS eventParams = { NV_ENC_EVENT_PARAMS_VER }; - eventParams.completionEvent = m_vpCompletionEvent[i]; - m_nvenc.nvEncUnregisterAsyncEvent(m_hEncoder, &eventParams); - CloseHandle(m_vpCompletionEvent[i]); - } - } - m_vpCompletionEvent.clear(); -#endif - - if (m_bMotionEstimationOnly) - { - DestroyMVOutputBuffer(); - } - else - { - DestroyBitstreamBuffer(); - } - - m_nvenc.nvEncDestroyEncoder(m_hEncoder); - - m_hEncoder = nullptr; - - m_bEncoderInitialized = false; -} - -const NvEncInputFrame* NvEncoder::GetNextInputFrame() -{ - int i = m_iToSend % m_nEncoderBuffer; - return &m_vInputFrames[i]; -} - -const NvEncInputFrame* NvEncoder::GetNextReferenceFrame() -{ - int i = m_iToSend % m_nEncoderBuffer; - return &m_vReferenceFrames[i]; -} - -void NvEncoder::MapResources(uint32_t bfrIdx) -{ - NV_ENC_MAP_INPUT_RESOURCE mapInputResource = { NV_ENC_MAP_INPUT_RESOURCE_VER }; - - mapInputResource.registeredResource = m_vRegisteredResources[bfrIdx]; - NVENC_API_CALL(m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResource)); - m_vMappedInputBuffers[bfrIdx] = mapInputResource.mappedResource; - - if (m_bMotionEstimationOnly) - { - mapInputResource.registeredResource = m_vRegisteredResourcesForReference[bfrIdx]; - NVENC_API_CALL(m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResource)); - m_vMappedRefBuffers[bfrIdx] = mapInputResource.mappedResource; - } -} - -void NvEncoder::EncodeFrame(std::vector<std::vector<uint8_t>> &vPacket, NV_ENC_PIC_PARAMS *pPicParams) -{ - vPacket.clear(); - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder device not found", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - int bfrIdx = m_iToSend % m_nEncoderBuffer; - - MapResources(bfrIdx); - - NVENCSTATUS nvStatus = DoEncode(m_vMappedInputBuffers[bfrIdx], m_vBitstreamOutputBuffer[bfrIdx], pPicParams); - - if (nvStatus == NV_ENC_SUCCESS || nvStatus == NV_ENC_ERR_NEED_MORE_INPUT) - { - m_iToSend++; - GetEncodedPacket(m_vBitstreamOutputBuffer, vPacket, true); - } - else - { - NVENC_THROW_ERROR("nvEncEncodePicture API failed", nvStatus); - } -} - -void NvEncoder::RunMotionEstimation(std::vector<uint8_t> &mvData) -{ - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_NO_ENCODE_DEVICE); - return; - } - - const uint32_t bfrIdx = m_iToSend % m_nEncoderBuffer; - - MapResources(bfrIdx); - - NVENCSTATUS nvStatus = DoMotionEstimation(m_vMappedInputBuffers[bfrIdx], m_vMappedRefBuffers[bfrIdx], m_vMVDataOutputBuffer[bfrIdx]); - - if (nvStatus == NV_ENC_SUCCESS) - { - m_iToSend++; - std::vector<std::vector<uint8_t>> vPacket; - GetEncodedPacket(m_vMVDataOutputBuffer, vPacket, true); - if (vPacket.size() != 1) - { - NVENC_THROW_ERROR("GetEncodedPacket() doesn't return one (and only one) MVData", NV_ENC_ERR_GENERIC); - } - mvData = vPacket[0]; - } - else - { - NVENC_THROW_ERROR("nvEncEncodePicture API failed", nvStatus); - } -} - - -void NvEncoder::GetSequenceParams(std::vector<uint8_t> &seqParams) -{ - uint8_t spsppsData[1024]; // Assume maximum spspps data is 1KB or less - memset(spsppsData, 0, sizeof(spsppsData)); - NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER }; - uint32_t spsppsSize = 0; - - payload.spsppsBuffer = spsppsData; - payload.inBufferSize = sizeof(spsppsData); - payload.outSPSPPSPayloadSize = &spsppsSize; - NVENC_API_CALL(m_nvenc.nvEncGetSequenceParams(m_hEncoder, &payload)); - seqParams.clear(); - seqParams.insert(seqParams.end(), &spsppsData[0], &spsppsData[spsppsSize]); -} - -NVENCSTATUS NvEncoder::DoEncode(NV_ENC_INPUT_PTR inputBuffer, NV_ENC_OUTPUT_PTR outputBuffer, NV_ENC_PIC_PARAMS *pPicParams) -{ - NV_ENC_PIC_PARAMS picParams = {}; - if (pPicParams) - { - picParams = *pPicParams; - } - picParams.version = NV_ENC_PIC_PARAMS_VER; - picParams.pictureStruct = NV_ENC_PIC_STRUCT_FRAME; - picParams.inputBuffer = inputBuffer; - picParams.bufferFmt = GetPixelFormat(); - picParams.inputWidth = GetEncodeWidth(); - picParams.inputHeight = GetEncodeHeight(); - picParams.outputBitstream = outputBuffer; - picParams.completionEvent = GetCompletionEvent(m_iToSend % m_nEncoderBuffer); - NVENCSTATUS nvStatus = m_nvenc.nvEncEncodePicture(m_hEncoder, &picParams); - - return nvStatus; -} - -void NvEncoder::SendEOS() -{ - NV_ENC_PIC_PARAMS picParams = { NV_ENC_PIC_PARAMS_VER }; - picParams.encodePicFlags = NV_ENC_PIC_FLAG_EOS; - picParams.completionEvent = GetCompletionEvent(m_iToSend % m_nEncoderBuffer); - NVENC_API_CALL(m_nvenc.nvEncEncodePicture(m_hEncoder, &picParams)); -} - -void NvEncoder::EndEncode(std::vector<std::vector<uint8_t>> &vPacket) -{ - vPacket.clear(); - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder device not initialized", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - - SendEOS(); - - GetEncodedPacket(m_vBitstreamOutputBuffer, vPacket, false); -} - -void NvEncoder::GetEncodedPacket(std::vector<NV_ENC_OUTPUT_PTR> &vOutputBuffer, std::vector<std::vector<uint8_t>> &vPacket, bool bOutputDelay) -{ - unsigned i = 0; - int iEnd = bOutputDelay ? m_iToSend - m_nOutputDelay : m_iToSend; - for (; m_iGot < iEnd; m_iGot++) - { - WaitForCompletionEvent(m_iGot % m_nEncoderBuffer); - NV_ENC_LOCK_BITSTREAM lockBitstreamData = { NV_ENC_LOCK_BITSTREAM_VER }; - lockBitstreamData.outputBitstream = vOutputBuffer[m_iGot % m_nEncoderBuffer]; - lockBitstreamData.doNotWait = false; - NVENC_API_CALL(m_nvenc.nvEncLockBitstream(m_hEncoder, &lockBitstreamData)); - - uint8_t *pData = (uint8_t *)lockBitstreamData.bitstreamBufferPtr; - if (vPacket.size() < i + 1) - { - vPacket.push_back(std::vector<uint8_t>()); - } - vPacket[i].clear(); - vPacket[i].insert(vPacket[i].end(), &pData[0], &pData[lockBitstreamData.bitstreamSizeInBytes]); - i++; - - NVENC_API_CALL(m_nvenc.nvEncUnlockBitstream(m_hEncoder, lockBitstreamData.outputBitstream)); - - if (m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - - if (m_bMotionEstimationOnly && m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - } -} - -bool NvEncoder::Reconfigure(const NV_ENC_RECONFIGURE_PARAMS *pReconfigureParams) -{ - NVENC_API_CALL(m_nvenc.nvEncReconfigureEncoder(m_hEncoder, const_cast<NV_ENC_RECONFIGURE_PARAMS*>(pReconfigureParams))); - - memcpy(&m_initializeParams, &(pReconfigureParams->reInitEncodeParams), sizeof(m_initializeParams)); - if (pReconfigureParams->reInitEncodeParams.encodeConfig) - { - memcpy(&m_encodeConfig, pReconfigureParams->reInitEncodeParams.encodeConfig, sizeof(m_encodeConfig)); - } - - m_nWidth = m_initializeParams.encodeWidth; - m_nHeight = m_initializeParams.encodeHeight; - m_nMaxEncodeWidth = m_initializeParams.maxEncodeWidth; - m_nMaxEncodeHeight = m_initializeParams.maxEncodeHeight; - - return true; -} - -NV_ENC_REGISTERED_PTR NvEncoder::RegisterResource(void *pBuffer, NV_ENC_INPUT_RESOURCE_TYPE eResourceType, - int width, int height, int pitch, NV_ENC_BUFFER_FORMAT bufferFormat, NV_ENC_BUFFER_USAGE bufferUsage) -{ - NV_ENC_REGISTER_RESOURCE registerResource = { NV_ENC_REGISTER_RESOURCE_VER }; - registerResource.resourceType = eResourceType; - registerResource.resourceToRegister = pBuffer; - registerResource.width = width; - registerResource.height = height; - registerResource.pitch = pitch; - registerResource.bufferFormat = bufferFormat; - registerResource.bufferUsage = bufferUsage; - NVENC_API_CALL(m_nvenc.nvEncRegisterResource(m_hEncoder, ®isterResource)); - - return registerResource.registeredResource; -} - -void NvEncoder::RegisterInputResources(std::vector<void*> inputframes, NV_ENC_INPUT_RESOURCE_TYPE eResourceType, - int width, int height, int pitch, NV_ENC_BUFFER_FORMAT bufferFormat, bool bReferenceFrame) -{ - for (uint32_t i = 0; i < inputframes.size(); ++i) - { - NV_ENC_REGISTERED_PTR registeredPtr = RegisterResource(inputframes[i], eResourceType, width, height, pitch, bufferFormat, NV_ENC_INPUT_IMAGE); - - std::vector<uint32_t> _chromaOffsets; - NvEncoder::GetChromaSubPlaneOffsets(bufferFormat, pitch, height, _chromaOffsets); - NvEncInputFrame inputframe = {}; - inputframe.inputPtr = (void *)inputframes[i]; - inputframe.chromaOffsets[0] = 0; - inputframe.chromaOffsets[1] = 0; - for (uint32_t ch = 0; ch < _chromaOffsets.size(); ch++) - { - inputframe.chromaOffsets[ch] = _chromaOffsets[ch]; - } - inputframe.numChromaPlanes = NvEncoder::GetNumChromaPlanes(bufferFormat); - inputframe.pitch = pitch; - inputframe.chromaPitch = NvEncoder::GetChromaPitch(bufferFormat, pitch); - inputframe.bufferFormat = bufferFormat; - inputframe.resourceType = eResourceType; - - if (bReferenceFrame) - { - m_vRegisteredResourcesForReference.push_back(registeredPtr); - m_vReferenceFrames.push_back(inputframe); - } - else - { - m_vRegisteredResources.push_back(registeredPtr); - m_vInputFrames.push_back(inputframe); - } - } -} - -void NvEncoder::FlushEncoder() -{ - if (!m_bMotionEstimationOnly && !m_bOutputInVideoMemory) - { - // Incase of error it is possible for buffers still mapped to encoder. - // flush the encoder queue and then unmapped it if any surface is still mapped - try - { - std::vector<std::vector<uint8_t>> vPacket; - EndEncode(vPacket); - } - catch (...) - { - - } - } -} - -void NvEncoder::UnregisterInputResources() -{ - FlushEncoder(); - - if (m_bMotionEstimationOnly) - { - for (uint32_t i = 0; i < m_vMappedRefBuffers.size(); ++i) - { - if (m_vMappedRefBuffers[i]) - { - m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedRefBuffers[i]); - } - } - } - m_vMappedRefBuffers.clear(); - - for (uint32_t i = 0; i < m_vMappedInputBuffers.size(); ++i) - { - if (m_vMappedInputBuffers[i]) - { - m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedInputBuffers[i]); - } - } - m_vMappedInputBuffers.clear(); - - for (uint32_t i = 0; i < m_vRegisteredResources.size(); ++i) - { - if (m_vRegisteredResources[i]) - { - m_nvenc.nvEncUnregisterResource(m_hEncoder, m_vRegisteredResources[i]); - } - } - m_vRegisteredResources.clear(); - - - for (uint32_t i = 0; i < m_vRegisteredResourcesForReference.size(); ++i) - { - if (m_vRegisteredResourcesForReference[i]) - { - m_nvenc.nvEncUnregisterResource(m_hEncoder, m_vRegisteredResourcesForReference[i]); - } - } - m_vRegisteredResourcesForReference.clear(); - -} - - -void NvEncoder::WaitForCompletionEvent(int iEvent) -{ -#if defined(_WIN32) - // Check if we are in async mode. If not, don't wait for event; - NV_ENC_CONFIG sEncodeConfig = { 0 }; - NV_ENC_INITIALIZE_PARAMS sInitializeParams = { 0 }; - sInitializeParams.encodeConfig = &sEncodeConfig; - GetInitializeParams(&sInitializeParams); - - if (0U == sInitializeParams.enableEncodeAsync) - { - return; - } -#ifdef DEBUG - WaitForSingleObject(m_vpCompletionEvent[iEvent], INFINITE); -#else - // wait for 20s which is infinite on terms of gpu time - if (WaitForSingleObject(m_vpCompletionEvent[iEvent], 20000) == WAIT_FAILED) - { - NVENC_THROW_ERROR("Failed to encode frame", NV_ENC_ERR_GENERIC); - } -#endif -#endif -} - -uint32_t NvEncoder::GetWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t width) -{ - switch (bufferFormat) { - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - case NV_ENC_BUFFER_FORMAT_YUV444: - return width; - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return width * 2; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return width * 4; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return 0; - } -} - -uint32_t NvEncoder::GetNumChromaPlanes(const NV_ENC_BUFFER_FORMAT bufferFormat) -{ - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - return 1; - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - case NV_ENC_BUFFER_FORMAT_YUV444: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return 2; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 0; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return -1; - } -} - -uint32_t NvEncoder::GetChromaPitch(const NV_ENC_BUFFER_FORMAT bufferFormat,const uint32_t lumaPitch) -{ - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - case NV_ENC_BUFFER_FORMAT_YUV444: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return lumaPitch; - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - return (lumaPitch + 1)/2; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 0; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return -1; - } -} - -void NvEncoder::GetChromaSubPlaneOffsets(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t pitch, const uint32_t height, std::vector<uint32_t>& chromaOffsets) -{ - chromaOffsets.clear(); - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - chromaOffsets.push_back(pitch * height); - return; - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - chromaOffsets.push_back(pitch * height); - chromaOffsets.push_back(chromaOffsets[0] + (NvEncoder::GetChromaPitch(bufferFormat, pitch) * GetChromaHeight(bufferFormat, height))); - return; - case NV_ENC_BUFFER_FORMAT_YUV444: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - chromaOffsets.push_back(pitch * height); - chromaOffsets.push_back(chromaOffsets[0] + (pitch * height)); - return; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return; - } -} - -uint32_t NvEncoder::GetChromaHeight(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaHeight) -{ - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - return (lumaHeight + 1)/2; - case NV_ENC_BUFFER_FORMAT_YUV444: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return lumaHeight; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 0; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return 0; - } -} - -uint32_t NvEncoder::GetChromaWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaWidth) -{ - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - return (lumaWidth + 1) / 2; - case NV_ENC_BUFFER_FORMAT_NV12: - return lumaWidth; - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - return 2 * lumaWidth; - case NV_ENC_BUFFER_FORMAT_YUV444: - return lumaWidth; - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return 2 * lumaWidth; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 0; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return 0; - } -} - - -int NvEncoder::GetCapabilityValue(GUID guidCodec, NV_ENC_CAPS capsToQuery) -{ - if (!m_hEncoder) - { - return 0; - } - NV_ENC_CAPS_PARAM capsParam = { NV_ENC_CAPS_PARAM_VER }; - capsParam.capsToQuery = capsToQuery; - int v; - m_nvenc.nvEncGetEncodeCaps(m_hEncoder, guidCodec, &capsParam, &v); - return v; -} - -int NvEncoder::GetFrameSize() const -{ - switch (GetPixelFormat()) - { - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - case NV_ENC_BUFFER_FORMAT_NV12: - return GetEncodeWidth() * (GetEncodeHeight() + (GetEncodeHeight() + 1) / 2); - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - return 2 * GetEncodeWidth() * (GetEncodeHeight() + (GetEncodeHeight() + 1) / 2); - case NV_ENC_BUFFER_FORMAT_YUV444: - return GetEncodeWidth() * GetEncodeHeight() * 3; - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return 2 * GetEncodeWidth() * GetEncodeHeight() * 3; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 4 * GetEncodeWidth() * GetEncodeHeight(); - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return 0; - } -} - -void NvEncoder::GetInitializeParams(NV_ENC_INITIALIZE_PARAMS *pInitializeParams) -{ - if (!pInitializeParams || !pInitializeParams->encodeConfig) - { - NVENC_THROW_ERROR("Both pInitializeParams and pInitializeParams->encodeConfig can't be NULL", NV_ENC_ERR_INVALID_PTR); - } - NV_ENC_CONFIG *pEncodeConfig = pInitializeParams->encodeConfig; - *pEncodeConfig = m_encodeConfig; - *pInitializeParams = m_initializeParams; - pInitializeParams->encodeConfig = pEncodeConfig; -} - -void NvEncoder::InitializeBitstreamBuffer() -{ - for (int i = 0; i < m_nEncoderBuffer; i++) - { - NV_ENC_CREATE_BITSTREAM_BUFFER createBitstreamBuffer = { NV_ENC_CREATE_BITSTREAM_BUFFER_VER }; - NVENC_API_CALL(m_nvenc.nvEncCreateBitstreamBuffer(m_hEncoder, &createBitstreamBuffer)); - m_vBitstreamOutputBuffer[i] = createBitstreamBuffer.bitstreamBuffer; - } -} - -void NvEncoder::DestroyBitstreamBuffer() -{ - for (uint32_t i = 0; i < m_vBitstreamOutputBuffer.size(); i++) - { - if (m_vBitstreamOutputBuffer[i]) - { - m_nvenc.nvEncDestroyBitstreamBuffer(m_hEncoder, m_vBitstreamOutputBuffer[i]); - } - } - - m_vBitstreamOutputBuffer.clear(); -} - -void NvEncoder::InitializeMVOutputBuffer() -{ - for (int i = 0; i < m_nEncoderBuffer; i++) - { - NV_ENC_CREATE_MV_BUFFER createMVBuffer = { NV_ENC_CREATE_MV_BUFFER_VER }; - NVENC_API_CALL(m_nvenc.nvEncCreateMVBuffer(m_hEncoder, &createMVBuffer)); - m_vMVDataOutputBuffer.push_back(createMVBuffer.mvBuffer); - } -} - -void NvEncoder::DestroyMVOutputBuffer() -{ - for (uint32_t i = 0; i < m_vMVDataOutputBuffer.size(); i++) - { - if (m_vMVDataOutputBuffer[i]) - { - m_nvenc.nvEncDestroyMVBuffer(m_hEncoder, m_vMVDataOutputBuffer[i]); - } - } - - m_vMVDataOutputBuffer.clear(); -} - -NVENCSTATUS NvEncoder::DoMotionEstimation(NV_ENC_INPUT_PTR inputBuffer, NV_ENC_INPUT_PTR inputBufferForReference, NV_ENC_OUTPUT_PTR outputBuffer) -{ - NV_ENC_MEONLY_PARAMS meParams = { NV_ENC_MEONLY_PARAMS_VER }; - meParams.inputBuffer = inputBuffer; - meParams.referenceFrame = inputBufferForReference; - meParams.inputWidth = GetEncodeWidth(); - meParams.inputHeight = GetEncodeHeight(); - meParams.mvBuffer = outputBuffer; - meParams.completionEvent = GetCompletionEvent(m_iToSend % m_nEncoderBuffer); - NVENCSTATUS nvStatus = m_nvenc.nvEncRunMotionEstimationOnly(m_hEncoder, &meParams); - - return nvStatus; -} diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoder.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoder.h deleted file mode 100644 index e740b71bb6aebd6555b13a8d766d8f2f3aa6a8f8..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoder.h +++ /dev/null @@ -1,437 +0,0 @@ -/* -* Copyright 2017-2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include <vector> -#include "nvEncodeAPI.h" -#include <stdint.h> -#include <mutex> -#include <string> -#include <iostream> -#include <sstream> -#include <string.h> - -/** -* @brief Exception class for error reporting from NvEncodeAPI calls. -*/ -class NVENCException : public std::exception -{ -public: - NVENCException(const std::string& errorStr, const NVENCSTATUS errorCode) - : m_errorString(errorStr), m_errorCode(errorCode) {} - - virtual ~NVENCException() throw() {} - virtual const char* what() const throw() { return m_errorString.c_str(); } - NVENCSTATUS getErrorCode() const { return m_errorCode; } - const std::string& getErrorString() const { return m_errorString; } - static NVENCException makeNVENCException(const std::string& errorStr, const NVENCSTATUS errorCode, - const std::string& functionName, const std::string& fileName, int lineNo); -private: - std::string m_errorString; - NVENCSTATUS m_errorCode; -}; - -inline NVENCException NVENCException::makeNVENCException(const std::string& errorStr, const NVENCSTATUS errorCode, const std::string& functionName, - const std::string& fileName, int lineNo) -{ - std::ostringstream errorLog; - errorLog << functionName << " : " << errorStr << " at " << fileName << ":" << lineNo << std::endl; - NVENCException exception(errorLog.str(), errorCode); - return exception; -} - -#define NVENC_THROW_ERROR( errorStr, errorCode ) \ - do \ - { \ - throw NVENCException::makeNVENCException(errorStr, errorCode, __FUNCTION__, __FILE__, __LINE__); \ - } while (0) - - -#define NVENC_API_CALL( nvencAPI ) \ - do \ - { \ - NVENCSTATUS errorCode = nvencAPI; \ - if( errorCode != NV_ENC_SUCCESS) \ - { \ - std::ostringstream errorLog; \ - errorLog << #nvencAPI << " returned error " << errorCode; \ - throw NVENCException::makeNVENCException(errorLog.str(), errorCode, __FUNCTION__, __FILE__, __LINE__); \ - } \ - } while (0) - -struct NvEncInputFrame -{ - void* inputPtr = nullptr; - uint32_t chromaOffsets[2]; - uint32_t numChromaPlanes; - uint32_t pitch; - uint32_t chromaPitch; - NV_ENC_BUFFER_FORMAT bufferFormat; - NV_ENC_INPUT_RESOURCE_TYPE resourceType; -}; - -/** -* @brief Shared base class for different encoder interfaces. -*/ -class NvEncoder -{ -public: - /** - * @brief This function is used to initialize the encoder session. - * Application must call this function to initialize the encoder, before - * starting to encode any frames. - */ - void CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncodeParams); - - /** - * @brief This function is used to destroy the encoder session. - * Application must call this function to destroy the encoder session and - * clean up any allocated resources. The application must call EndEncode() - * function to get any queued encoded frames before calling DestroyEncoder(). - */ - void DestroyEncoder(); - - /** - * @brief This function is used to reconfigure an existing encoder session. - * Application can use this function to dynamically change the bitrate, - * resolution and other QOS parameters. If the application changes the - * resolution, it must set NV_ENC_RECONFIGURE_PARAMS::forceIDR. - */ - bool Reconfigure(const NV_ENC_RECONFIGURE_PARAMS *pReconfigureParams); - - /** - * @brief This function is used to get the next available input buffer. - * Applications must call this function to obtain a pointer to the next - * input buffer. The application must copy the uncompressed data to the - * input buffer and then call EncodeFrame() function to encode it. - */ - const NvEncInputFrame* GetNextInputFrame(); - - - /** - * @brief This function is used to encode a frame. - * Applications must call EncodeFrame() function to encode the uncompressed - * data, which has been copied to an input buffer obtained from the - * GetNextInputFrame() function. - */ - void EncodeFrame(std::vector<std::vector<uint8_t>> &vPacket, NV_ENC_PIC_PARAMS *pPicParams = nullptr); - - /** - * @brief This function to flush the encoder queue. - * The encoder might be queuing frames for B picture encoding or lookahead; - * the application must call EndEncode() to get all the queued encoded frames - * from the encoder. The application must call this function before destroying - * an encoder session. - */ - void EndEncode(std::vector<std::vector<uint8_t>> &vPacket); - - /** - * @brief This function is used to query hardware encoder capabilities. - * Applications can call this function to query capabilities like maximum encode - * dimensions, support for lookahead or the ME-only mode etc. - */ - int GetCapabilityValue(GUID guidCodec, NV_ENC_CAPS capsToQuery); - - /** - * @brief This function is used to get the current device on which encoder is running. - */ - void *GetDevice() const { return m_pDevice; } - - /** - * @brief This function is used to get the current device type which encoder is running. - */ - NV_ENC_DEVICE_TYPE GetDeviceType() const { return m_eDeviceType; } - - /** - * @brief This function is used to get the current encode width. - * The encode width can be modified by Reconfigure() function. - */ - int GetEncodeWidth() const { return m_nWidth; } - - /** - * @brief This function is used to get the current encode height. - * The encode height can be modified by Reconfigure() function. - */ - int GetEncodeHeight() const { return m_nHeight; } - - /** - * @brief This function is used to get the current frame size based on pixel format. - */ - int GetFrameSize() const; - - /** - * @brief This function is used to initialize config parameters based on - * given codec and preset guids. - * The application can call this function to get the default configuration - * for a certain preset. The application can either use these parameters - * directly or override them with application-specific settings before - * using them in CreateEncoder() function. - */ - void CreateDefaultEncoderParams(NV_ENC_INITIALIZE_PARAMS* pIntializeParams, GUID codecGuid, GUID presetGuid); - - /** - * @brief This function is used to get the current initialization parameters, - * which had been used to configure the encoder session. - * The initialization parameters are modified if the application calls - * Reconfigure() function. - */ - void GetInitializeParams(NV_ENC_INITIALIZE_PARAMS *pInitializeParams); - - /** - * @brief This function is used to run motion estimation - * This is used to run motion estimation on a a pair of frames. The - * application must copy the reference frame data to the buffer obtained - * by calling GetNextReferenceFrame(), and copy the input frame data to - * the buffer obtained by calling GetNextInputFrame() before calling the - * RunMotionEstimation() function. - */ - void RunMotionEstimation(std::vector<uint8_t> &mvData); - - /** - * @brief This function is used to get an available reference frame. - * Application must call this function to get a pointer to reference buffer, - * to be used in the subsequent RunMotionEstimation() function. - */ - const NvEncInputFrame* GetNextReferenceFrame(); - - /** - * @brief This function is used to get sequence and picture parameter headers. - * Application can call this function after encoder is initialized to get SPS and PPS - * nalus for the current encoder instance. The sequence header data might change when - * application calls Reconfigure() function. - */ - void GetSequenceParams(std::vector<uint8_t> &seqParams); - - /** - * @brief NvEncoder class virtual destructor. - */ - virtual ~NvEncoder(); - -public: - /** - * @brief This a static function to get chroma offsets for YUV planar formats. - */ - static void GetChromaSubPlaneOffsets(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t pitch, - const uint32_t height, std::vector<uint32_t>& chromaOffsets); - /** - * @brief This a static function to get the chroma plane pitch for YUV planar formats. - */ - static uint32_t GetChromaPitch(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaPitch); - - /** - * @brief This a static function to get the number of chroma planes for YUV planar formats. - */ - static uint32_t GetNumChromaPlanes(const NV_ENC_BUFFER_FORMAT bufferFormat); - - /** - * @brief This a static function to get the chroma plane width in bytes for YUV planar formats. - */ - static uint32_t GetChromaWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaWidth); - - /** - * @brief This a static function to get the chroma planes height in bytes for YUV planar formats. - */ - static uint32_t GetChromaHeight(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaHeight); - - - /** - * @brief This a static function to get the width in bytes for the frame. - * For YUV planar format this is the width in bytes of the luma plane. - */ - static uint32_t GetWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t width); - -protected: - - /** - * @brief NvEncoder class constructor. - * NvEncoder class constructor cannot be called directly by the application. - */ - NvEncoder(NV_ENC_DEVICE_TYPE eDeviceType, void *pDevice, uint32_t nWidth, uint32_t nHeight, - NV_ENC_BUFFER_FORMAT eBufferFormat, uint32_t nOutputDelay, bool bMotionEstimationOnly, bool bOutputInVideoMemory = false); - - /** - * @brief This function is used to check if hardware encoder is properly initialized. - */ - bool IsHWEncoderInitialized() const { return m_hEncoder != NULL && m_bEncoderInitialized; } - - /** - * @brief This function is used to register CUDA, D3D or OpenGL input buffers with NvEncodeAPI. - * This is non public function and is called by derived class for allocating - * and registering input buffers. - */ - void RegisterInputResources(std::vector<void*> inputframes, NV_ENC_INPUT_RESOURCE_TYPE eResourceType, - int width, int height, int pitch, NV_ENC_BUFFER_FORMAT bufferFormat, bool bReferenceFrame = false); - - /** - * @brief This function is used to unregister resources which had been previously registered for encoding - * using RegisterInputResources() function. - */ - void UnregisterInputResources(); - - /** - * @brief This function is used to register CUDA, D3D or OpenGL input or output buffers with NvEncodeAPI. - */ - NV_ENC_REGISTERED_PTR RegisterResource(void *pBuffer, NV_ENC_INPUT_RESOURCE_TYPE eResourceType, - int width, int height, int pitch, NV_ENC_BUFFER_FORMAT bufferFormat, NV_ENC_BUFFER_USAGE bufferUsage = NV_ENC_INPUT_IMAGE); - - /** - * @brief This function returns maximum width used to open the encoder session. - * All encode input buffers are allocated using maximum dimensions. - */ - uint32_t GetMaxEncodeWidth() const { return m_nMaxEncodeWidth; } - - /** - * @brief This function returns maximum height used to open the encoder session. - * All encode input buffers are allocated using maximum dimensions. - */ - uint32_t GetMaxEncodeHeight() const { return m_nMaxEncodeHeight; } - - /** - * @brief This function returns the completion event. - */ - void* GetCompletionEvent(uint32_t eventIdx) { return (m_vpCompletionEvent.size() == m_nEncoderBuffer) ? m_vpCompletionEvent[eventIdx] : nullptr; } - - /** - * @brief This function returns the current pixel format. - */ - NV_ENC_BUFFER_FORMAT GetPixelFormat() const { return m_eBufferFormat; } - - /** - * @brief This function is used to submit the encode commands to the - * NVENC hardware. - */ - NVENCSTATUS DoEncode(NV_ENC_INPUT_PTR inputBuffer, NV_ENC_OUTPUT_PTR outputBuffer, NV_ENC_PIC_PARAMS *pPicParams); - - /** - * @brief This function is used to submit the encode commands to the - * NVENC hardware for ME only mode. - */ - NVENCSTATUS DoMotionEstimation(NV_ENC_INPUT_PTR inputBuffer, NV_ENC_INPUT_PTR inputBufferForReference, NV_ENC_OUTPUT_PTR outputBuffer); - - /** - * @brief This function is used to map the input buffers to NvEncodeAPI. - */ - void MapResources(uint32_t bfrIdx); - - /** - * @brief This function is used to wait for completion of encode command. - */ - void WaitForCompletionEvent(int iEvent); - - /** - * @brief This function is used to send EOS to HW encoder. - */ - void SendEOS(); - -private: - /** - * @brief This is a private function which is used to check if there is any - buffering done by encoder. - * The encoder generally buffers data to encode B frames or for lookahead - * or pipelining. - */ - bool IsZeroDelay() { return m_nOutputDelay == 0; } - - /** - * @brief This is a private function which is used to load the encode api shared library. - */ - void LoadNvEncApi(); - - /** - * @brief This is a private function which is used to get the output packets - * from the encoder HW. - * This is called by DoEncode() function. If there is buffering enabled, - * this may return without any output data. - */ - void GetEncodedPacket(std::vector<NV_ENC_OUTPUT_PTR> &vOutputBuffer, std::vector<std::vector<uint8_t>> &vPacket, bool bOutputDelay); - - /** - * @brief This is a private function which is used to initialize the bitstream buffers. - * This is only used in the encoding mode. - */ - void InitializeBitstreamBuffer(); - - /** - * @brief This is a private function which is used to destroy the bitstream buffers. - * This is only used in the encoding mode. - */ - void DestroyBitstreamBuffer(); - - /** - * @brief This is a private function which is used to initialize MV output buffers. - * This is only used in ME-only Mode. - */ - void InitializeMVOutputBuffer(); - - /** - * @brief This is a private function which is used to destroy MV output buffers. - * This is only used in ME-only Mode. - */ - void DestroyMVOutputBuffer(); - - /** - * @brief This is a private function which is used to destroy HW encoder. - */ - void DestroyHWEncoder(); - - /** - * @brief This function is used to flush the encoder queue. - */ - void FlushEncoder(); - -private: - /** - * @brief This is a pure virtual function which is used to allocate input buffers. - * The derived classes must implement this function. - */ - virtual void AllocateInputBuffers(int32_t numInputBuffers) = 0; - - /** - * @brief This is a pure virtual function which is used to destroy input buffers. - * The derived classes must implement this function. - */ - virtual void ReleaseInputBuffers() = 0; - -protected: - bool m_bMotionEstimationOnly = false; - bool m_bOutputInVideoMemory = false; - void *m_hEncoder = nullptr; - NV_ENCODE_API_FUNCTION_LIST m_nvenc; - std::vector<NvEncInputFrame> m_vInputFrames; - std::vector<NV_ENC_REGISTERED_PTR> m_vRegisteredResources; - std::vector<NvEncInputFrame> m_vReferenceFrames; - std::vector<NV_ENC_REGISTERED_PTR> m_vRegisteredResourcesForReference; - std::vector<NV_ENC_INPUT_PTR> m_vMappedInputBuffers; - std::vector<NV_ENC_INPUT_PTR> m_vMappedRefBuffers; - std::vector<void *> m_vpCompletionEvent; - - int32_t m_iToSend = 0; - int32_t m_iGot = 0; - int32_t m_nEncoderBuffer = 0; - int32_t m_nOutputDelay = 0; - -private: - uint32_t m_nWidth; - uint32_t m_nHeight; - NV_ENC_BUFFER_FORMAT m_eBufferFormat; - void *m_pDevice; - NV_ENC_DEVICE_TYPE m_eDeviceType; - NV_ENC_INITIALIZE_PARAMS m_initializeParams = {}; - NV_ENC_CONFIG m_encodeConfig = {}; - bool m_bEncoderInitialized = false; - uint32_t m_nExtraOutputDelay = 3; - std::vector<NV_ENC_OUTPUT_PTR> m_vBitstreamOutputBuffer; - std::vector<NV_ENC_OUTPUT_PTR> m_vMVDataOutputBuffer; - uint32_t m_nMaxEncodeWidth = 0; - uint32_t m_nMaxEncodeHeight = 0; - void* m_hModule = nullptr; -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderCuda.cpp b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderCuda.cpp deleted file mode 100644 index 3a1e234d99805107358f3b1ae024089e6dfdd0d6..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderCuda.cpp +++ /dev/null @@ -1,283 +0,0 @@ -/* -* Copyright 2017-2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include "NvEncoder/NvEncoderCuda.h" - - -NvEncoderCuda::NvEncoderCuda(CUcontext cuContext, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay, bool bMotionEstimationOnly, bool bOutputInVideoMemory): - NvEncoder(NV_ENC_DEVICE_TYPE_CUDA, cuContext, nWidth, nHeight, eBufferFormat, nExtraOutputDelay, bMotionEstimationOnly, bOutputInVideoMemory), - m_cuContext(cuContext) -{ - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_INVALID_DEVICE); - } - - if (!m_cuContext) - { - NVENC_THROW_ERROR("Invalid Cuda Context", NV_ENC_ERR_INVALID_DEVICE); - } -} - -NvEncoderCuda::~NvEncoderCuda() -{ - ReleaseCudaResources(); -} - -void NvEncoderCuda::AllocateInputBuffers(int32_t numInputBuffers) -{ - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder intialization failed", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - - // for MEOnly mode we need to allocate seperate set of buffers for reference frame - int numCount = m_bMotionEstimationOnly ? 2 : 1; - - for (int count = 0; count < numCount; count++) - { - CUDA_DRVAPI_CALL(cuCtxPushCurrent(m_cuContext)); - std::vector<void*> inputFrames; - for (int i = 0; i < numInputBuffers; i++) - { - CUdeviceptr pDeviceFrame; - uint32_t chromaHeight = GetNumChromaPlanes(GetPixelFormat()) * GetChromaHeight(GetPixelFormat(), GetMaxEncodeHeight()); - if (GetPixelFormat() == NV_ENC_BUFFER_FORMAT_YV12 || GetPixelFormat() == NV_ENC_BUFFER_FORMAT_IYUV) - chromaHeight = GetChromaHeight(GetPixelFormat(), GetMaxEncodeHeight()); - CUDA_DRVAPI_CALL(cuMemAllocPitch((CUdeviceptr *)&pDeviceFrame, - &m_cudaPitch, - GetWidthInBytes(GetPixelFormat(), GetMaxEncodeWidth()), - GetMaxEncodeHeight() + chromaHeight, 16)); - inputFrames.push_back((void*)pDeviceFrame); - } - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); - - RegisterInputResources(inputFrames, - NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, - GetMaxEncodeWidth(), - GetMaxEncodeHeight(), - (int)m_cudaPitch, - GetPixelFormat(), - (count == 1) ? true : false); - } -} - -void NvEncoderCuda::ReleaseInputBuffers() -{ - ReleaseCudaResources(); -} - -void NvEncoderCuda::ReleaseCudaResources() -{ - if (!m_hEncoder) - { - return; - } - - if (!m_cuContext) - { - return; - } - - UnregisterInputResources(); - - cuCtxPushCurrent(m_cuContext); - - for (uint32_t i = 0; i < m_vInputFrames.size(); ++i) - { - if (m_vInputFrames[i].inputPtr) - { - cuMemFree(reinterpret_cast<CUdeviceptr>(m_vInputFrames[i].inputPtr)); - } - } - m_vInputFrames.clear(); - - for (uint32_t i = 0; i < m_vReferenceFrames.size(); ++i) - { - if (m_vReferenceFrames[i].inputPtr) - { - cuMemFree(reinterpret_cast<CUdeviceptr>(m_vReferenceFrames[i].inputPtr)); - } - } - m_vReferenceFrames.clear(); - - cuCtxPopCurrent(NULL); - m_cuContext = nullptr; -} - -void NvEncoderCuda::CopyToDeviceFrame(CUcontext device, - void* pSrcFrame, - uint32_t nSrcPitch, - CUdeviceptr pDstFrame, - uint32_t dstPitch, - int width, - int height, - CUmemorytype srcMemoryType, - NV_ENC_BUFFER_FORMAT pixelFormat, - const uint32_t dstChromaOffsets[], - uint32_t numChromaPlanes, - bool bUnAlignedDeviceCopy) -{ - if (srcMemoryType != CU_MEMORYTYPE_HOST && srcMemoryType != CU_MEMORYTYPE_DEVICE) - { - NVENC_THROW_ERROR("Invalid source memory type for copy", NV_ENC_ERR_INVALID_PARAM); - } - - CUDA_DRVAPI_CALL(cuCtxPushCurrent(device)); - - uint32_t srcPitch = nSrcPitch ? nSrcPitch : NvEncoder::GetWidthInBytes(pixelFormat, width); - CUDA_MEMCPY2D m = { 0 }; - m.srcMemoryType = srcMemoryType; - if (srcMemoryType == CU_MEMORYTYPE_HOST) - { - m.srcHost = pSrcFrame; - } - else - { - m.srcDevice = (CUdeviceptr)pSrcFrame; - } - m.srcPitch = srcPitch; - m.dstMemoryType = CU_MEMORYTYPE_DEVICE; - m.dstDevice = pDstFrame; - m.dstPitch = dstPitch; - m.WidthInBytes = NvEncoder::GetWidthInBytes(pixelFormat, width); - m.Height = height; - if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) - { - CUDA_DRVAPI_CALL(cuMemcpy2DUnaligned(&m)); - } - else - { - CUDA_DRVAPI_CALL(cuMemcpy2D(&m)); - } - - std::vector<uint32_t> srcChromaOffsets; - NvEncoder::GetChromaSubPlaneOffsets(pixelFormat, srcPitch, height, srcChromaOffsets); - uint32_t chromaHeight = NvEncoder::GetChromaHeight(pixelFormat, height); - uint32_t destChromaPitch = NvEncoder::GetChromaPitch(pixelFormat, dstPitch); - uint32_t srcChromaPitch = NvEncoder::GetChromaPitch(pixelFormat, srcPitch); - uint32_t chromaWidthInBytes = NvEncoder::GetChromaWidthInBytes(pixelFormat, width); - - for (uint32_t i = 0; i < numChromaPlanes; ++i) - { - if (chromaHeight) - { - if (srcMemoryType == CU_MEMORYTYPE_HOST) - { - m.srcHost = ((uint8_t *)pSrcFrame + srcChromaOffsets[i]); - } - else - { - m.srcDevice = (CUdeviceptr)((uint8_t *)pSrcFrame + srcChromaOffsets[i]); - } - m.srcPitch = srcChromaPitch; - - m.dstDevice = (CUdeviceptr)((uint8_t *)pDstFrame + dstChromaOffsets[i]); - m.dstPitch = destChromaPitch; - m.WidthInBytes = chromaWidthInBytes; - m.Height = chromaHeight; - if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) - { - CUDA_DRVAPI_CALL(cuMemcpy2DUnaligned(&m)); - } - else - { - CUDA_DRVAPI_CALL(cuMemcpy2D(&m)); - } - } - } - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); -} - -void NvEncoderCuda::CopyToDeviceFrame(CUcontext device, - void* pSrcFrame, - uint32_t nSrcPitch, - CUdeviceptr pDstFrame, - uint32_t dstPitch, - int width, - int height, - CUmemorytype srcMemoryType, - NV_ENC_BUFFER_FORMAT pixelFormat, - CUdeviceptr dstChromaDevicePtrs[], - uint32_t dstChromaPitch, - uint32_t numChromaPlanes, - bool bUnAlignedDeviceCopy) -{ - if (srcMemoryType != CU_MEMORYTYPE_HOST && srcMemoryType != CU_MEMORYTYPE_DEVICE) - { - NVENC_THROW_ERROR("Invalid source memory type for copy", NV_ENC_ERR_INVALID_PARAM); - } - - CUDA_DRVAPI_CALL(cuCtxPushCurrent(device)); - - uint32_t srcPitch = nSrcPitch ? nSrcPitch : NvEncoder::GetWidthInBytes(pixelFormat, width); - CUDA_MEMCPY2D m = { 0 }; - m.srcMemoryType = srcMemoryType; - if (srcMemoryType == CU_MEMORYTYPE_HOST) - { - m.srcHost = pSrcFrame; - } - else - { - m.srcDevice = (CUdeviceptr)pSrcFrame; - } - m.srcPitch = srcPitch; - m.dstMemoryType = CU_MEMORYTYPE_DEVICE; - m.dstDevice = pDstFrame; - m.dstPitch = dstPitch; - m.WidthInBytes = NvEncoder::GetWidthInBytes(pixelFormat, width); - m.Height = height; - if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) - { - CUDA_DRVAPI_CALL(cuMemcpy2DUnaligned(&m)); - } - else - { - CUDA_DRVAPI_CALL(cuMemcpy2D(&m)); - } - - std::vector<uint32_t> srcChromaOffsets; - NvEncoder::GetChromaSubPlaneOffsets(pixelFormat, srcPitch, height, srcChromaOffsets); - uint32_t chromaHeight = NvEncoder::GetChromaHeight(pixelFormat, height); - uint32_t srcChromaPitch = NvEncoder::GetChromaPitch(pixelFormat, srcPitch); - uint32_t chromaWidthInBytes = NvEncoder::GetChromaWidthInBytes(pixelFormat, width); - - for (uint32_t i = 0; i < numChromaPlanes; ++i) - { - if (chromaHeight) - { - if (srcMemoryType == CU_MEMORYTYPE_HOST) - { - m.srcHost = ((uint8_t *)pSrcFrame + srcChromaOffsets[i]); - } - else - { - m.srcDevice = (CUdeviceptr)((uint8_t *)pSrcFrame + srcChromaOffsets[i]); - } - m.srcPitch = srcChromaPitch; - - m.dstDevice = dstChromaDevicePtrs[i]; - m.dstPitch = dstChromaPitch; - m.WidthInBytes = chromaWidthInBytes; - m.Height = chromaHeight; - if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) - { - CUDA_DRVAPI_CALL(cuMemcpy2DUnaligned(&m)); - } - else - { - CUDA_DRVAPI_CALL(cuMemcpy2D(&m)); - } - } - } - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); -} diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderCuda.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderCuda.h deleted file mode 100644 index acfcfac20e83438ebba99f22a26084a17aa36622..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderCuda.h +++ /dev/null @@ -1,106 +0,0 @@ -/* -* Copyright 2017-2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include <vector> -#include <stdint.h> -#include <mutex> -#include <cuda.h> -#include "NvEncoder.h" - -#define CUDA_DRVAPI_CALL( call ) \ - do \ - { \ - CUresult err__ = call; \ - if (err__ != CUDA_SUCCESS) \ - { \ - const char *szErrName = NULL; \ - cuGetErrorName(err__, &szErrName); \ - std::ostringstream errorLog; \ - errorLog << "CUDA driver API error " << szErrName ; \ - throw NVENCException::makeNVENCException(errorLog.str(), NV_ENC_ERR_GENERIC, __FUNCTION__, __FILE__, __LINE__); \ - } \ - } \ - while (0) - -/** -* @brief Encoder for CUDA device memory. -*/ -class NvEncoderCuda : public NvEncoder -{ -public: - NvEncoderCuda(CUcontext cuContext, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay = 3, bool bMotionEstimationOnly = false, bool bOPInVideoMemory = false); - virtual ~NvEncoderCuda(); - - /** - * @brief This is a static function to copy input data from host memory to device memory. - * This function assumes YUV plane is a single contiguous memory segment. - */ - static void CopyToDeviceFrame(CUcontext device, - void* pSrcFrame, - uint32_t nSrcPitch, - CUdeviceptr pDstFrame, - uint32_t dstPitch, - int width, - int height, - CUmemorytype srcMemoryType, - NV_ENC_BUFFER_FORMAT pixelFormat, - const uint32_t dstChromaOffsets[], - uint32_t numChromaPlanes, - bool bUnAlignedDeviceCopy = false); - - - /** - * @brief This is a static function to copy input data from host memory to device memory. - * Application must pass a seperate device pointer for each YUV plane. - */ - static void CopyToDeviceFrame(CUcontext device, - void* pSrcFrame, - uint32_t nSrcPitch, - CUdeviceptr pDstFrame, - uint32_t dstPitch, - int width, - int height, - CUmemorytype srcMemoryType, - NV_ENC_BUFFER_FORMAT pixelFormat, - CUdeviceptr dstChromaPtr[], - uint32_t dstChromaPitch, - uint32_t numChromaPlanes, - bool bUnAlignedDeviceCopy = false); - -protected: - /** - * @brief This function is used to release the input buffers allocated for encoding. - * This function is an override of virtual function NvEncoder::ReleaseInputBuffers(). - */ - virtual void ReleaseInputBuffers() override; - -private: - /** - * @brief This function is used to allocate input buffers for encoding. - * This function is an override of virtual function NvEncoder::AllocateInputBuffers(). - */ - virtual void AllocateInputBuffers(int32_t numInputBuffers) override; - -private: - /** - * @brief This is a private function to release CUDA device memory used for encoding. - */ - void ReleaseCudaResources(); - -protected: - CUcontext m_cuContext; - -private: - size_t m_cudaPitch = 0; -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D11.cpp b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D11.cpp deleted file mode 100644 index 029bee1deb4901c4d7b7b8ac8243975a994f9e5b..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D11.cpp +++ /dev/null @@ -1,147 +0,0 @@ -/* -* Copyright 2017-2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - - -#ifndef WIN32 -#include <dlfcn.h> -#endif -#include "NvEncoder/NvEncoderD3D11.h" -#include <D3D9Types.h> - -#ifndef MAKEFOURCC -#define MAKEFOURCC(a,b,c,d) (((unsigned int)a) | (((unsigned int)b)<< 8) | (((unsigned int)c)<<16) | (((unsigned int)d)<<24) ) -#endif - -DXGI_FORMAT GetD3D11Format(NV_ENC_BUFFER_FORMAT eBufferFormat) -{ - switch (eBufferFormat) - { - case NV_ENC_BUFFER_FORMAT_NV12: - return DXGI_FORMAT_NV12; - case NV_ENC_BUFFER_FORMAT_ARGB: - return DXGI_FORMAT_B8G8R8A8_UNORM; - default: - return DXGI_FORMAT_UNKNOWN; - } -} - -NvEncoderD3D11::NvEncoderD3D11(ID3D11Device* pD3D11Device, uint32_t nWidth, uint32_t nHeight, - NV_ENC_BUFFER_FORMAT eBufferFormat, uint32_t nExtraOutputDelay, bool bMotionEstimationOnly, bool bOutputInVideoMemory) : - NvEncoder(NV_ENC_DEVICE_TYPE_DIRECTX, pD3D11Device, nWidth, nHeight, eBufferFormat, nExtraOutputDelay, bMotionEstimationOnly, bOutputInVideoMemory) -{ - if (!pD3D11Device) - { - NVENC_THROW_ERROR("Bad d3d11device ptr", NV_ENC_ERR_INVALID_PTR); - return; - } - - if (GetD3D11Format(GetPixelFormat()) == DXGI_FORMAT_UNKNOWN) - { - NVENC_THROW_ERROR("Unsupported Buffer format", NV_ENC_ERR_INVALID_PARAM); - } - - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_INVALID_DEVICE); - } - - m_pD3D11Device = pD3D11Device; - m_pD3D11Device->AddRef(); - m_pD3D11Device->GetImmediateContext(&m_pD3D11DeviceContext); -} - -NvEncoderD3D11::~NvEncoderD3D11() -{ - ReleaseD3D11Resources(); -} - -void NvEncoderD3D11::AllocateInputBuffers(int32_t numInputBuffers) -{ - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder intialization failed", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - - // for MEOnly mode we need to allocate seperate set of buffers for reference frame - int numCount = m_bMotionEstimationOnly ? 2 : 1; - for (int count = 0; count < numCount; count++) - { - std::vector<void*> inputFrames; - for (int i = 0; i < numInputBuffers; i++) - { - ID3D11Texture2D *pInputTextures = NULL; - D3D11_TEXTURE2D_DESC desc; - ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC)); - desc.Width = GetMaxEncodeWidth(); - desc.Height = GetMaxEncodeHeight(); - desc.MipLevels = 1; - desc.ArraySize = 1; - desc.Format = GetD3D11Format(GetPixelFormat()); - desc.SampleDesc.Count = 1; - desc.Usage = D3D11_USAGE_DEFAULT; - desc.BindFlags = D3D11_BIND_RENDER_TARGET; - desc.CPUAccessFlags = 0; - if (m_pD3D11Device->CreateTexture2D(&desc, NULL, &pInputTextures) != S_OK) - { - NVENC_THROW_ERROR("Failed to create d3d11textures", NV_ENC_ERR_OUT_OF_MEMORY); - } - inputFrames.push_back(pInputTextures); - } - RegisterInputResources(inputFrames, NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX, - GetMaxEncodeWidth(), GetMaxEncodeHeight(), 0, GetPixelFormat(), count == 1 ? true : false); - } -} - -void NvEncoderD3D11::ReleaseInputBuffers() -{ - ReleaseD3D11Resources(); -} - -void NvEncoderD3D11::ReleaseD3D11Resources() -{ - if (!m_hEncoder) - { - return; - } - - UnregisterInputResources(); - - for (uint32_t i = 0; i < m_vInputFrames.size(); ++i) - { - if (m_vInputFrames[i].inputPtr) - { - reinterpret_cast<ID3D11Texture2D*>(m_vInputFrames[i].inputPtr)->Release(); - } - } - m_vInputFrames.clear(); - - for (uint32_t i = 0; i < m_vReferenceFrames.size(); ++i) - { - if (m_vReferenceFrames[i].inputPtr) - { - reinterpret_cast<ID3D11Texture2D*>(m_vReferenceFrames[i].inputPtr)->Release(); - } - } - m_vReferenceFrames.clear(); - - if (m_pD3D11DeviceContext) - { - m_pD3D11DeviceContext->Release(); - m_pD3D11DeviceContext = nullptr; - } - - if (m_pD3D11Device) - { - m_pD3D11Device->Release(); - m_pD3D11Device = nullptr; - } -} - diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D11.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D11.h deleted file mode 100644 index 68b83a4a5bc8be0a36876e27298a95c6611ee965..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D11.h +++ /dev/null @@ -1,55 +0,0 @@ -/* -* Copyright 2017-2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include <vector> -#include <stdint.h> -#include <mutex> -#include <unordered_map> -#include <d3d11.h> -#include "NvEncoder.h" - -class NvEncoderD3D11 : public NvEncoder -{ -public: - NvEncoderD3D11(ID3D11Device* pD3D11Device, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay = 3, bool bMotionEstimationOnly = false, bool bOPInVideoMemory = false); - virtual ~NvEncoderD3D11(); - -protected: - /** - * @brief This function is used to release the input buffers allocated for encoding. - * This function is an override of virtual function NvEncoder::ReleaseInputBuffers(). - */ - virtual void ReleaseInputBuffers() override; - -private: - /** - * @brief This function is used to allocate input buffers for encoding. - * This function is an override of virtual function NvEncoder::AllocateInputBuffers(). - * This function creates ID3D11Texture2D textures which is used to accept input data. - * To obtain handle to input buffers application must call NvEncoder::GetNextInputFrame() - */ - virtual void AllocateInputBuffers(int32_t numInputBuffers) override; - -private: - /** - * @brief This is a private function to release ID3D11Texture2D textures used for encoding. - */ - void ReleaseD3D11Resources(); - -protected: - ID3D11Device *m_pD3D11Device = nullptr; - -private: - ID3D11DeviceContext* m_pD3D11DeviceContext = nullptr; -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D9.cpp b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D9.cpp deleted file mode 100644 index 0814995c596d24fef3b048095b5e01880a8bd144..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D9.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/* -* Copyright 2017-2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - - -#ifndef WIN32 -#include <dlfcn.h> -#endif -#include "NvEncoder/NvEncoderD3D9.h" -#include <D3D9Types.h> -#include <utility> - -#ifndef MAKEFOURCC -#define MAKEFOURCC(a,b,c,d) (((unsigned int)a) | (((unsigned int)b)<< 8) | (((unsigned int)c)<<16) | (((unsigned int)d)<<24) ) -#endif - -D3DFORMAT GetD3D9Format(NV_ENC_BUFFER_FORMAT eBufferFormat) -{ - switch (eBufferFormat) - { - case NV_ENC_BUFFER_FORMAT_NV12: - return (D3DFORMAT)MAKEFOURCC('N', 'V', '1', '2'); - case NV_ENC_BUFFER_FORMAT_ARGB: - return D3DFMT_A8R8G8B8; - default: - return D3DFMT_UNKNOWN; - } -} - -NvEncoderD3D9::NvEncoderD3D9(IDirect3DDevice9* pD3D9Device, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - IDirectXVideoAccelerationService* pDXVAService, uint32_t nExtraOutputDelay, bool bMotionEstimationOnly) : - NvEncoder(NV_ENC_DEVICE_TYPE_DIRECTX, pD3D9Device, nWidth, nHeight, eBufferFormat, nExtraOutputDelay, bMotionEstimationOnly) -{ - if (!pD3D9Device) - { - NVENC_THROW_ERROR("Bad d3d9device ptr", NV_ENC_ERR_INVALID_PTR); - } - - if (GetD3D9Format(GetPixelFormat()) == D3DFMT_UNKNOWN) - { - NVENC_THROW_ERROR("Unsupported Buffer format", NV_ENC_ERR_INVALID_PARAM); - } - - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_INVALID_DEVICE); - } - - m_pD3D9Device = pD3D9Device; - m_pD3D9Device->AddRef(); - - m_pDXVAService = pDXVAService; - if (m_pDXVAService) - { - m_pDXVAService->AddRef(); - } -} - -NvEncoderD3D9::~NvEncoderD3D9() -{ - ReleaseD3D9Resources(); -} - -void NvEncoderD3D9::AllocateInputBuffers(int32_t numInputBuffers) -{ - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder intialization failed", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - - - // for MEOnly mode we need to allocate seperate set of buffers for reference frame - int numCount = m_bMotionEstimationOnly ? 2 : 1; - - for (int count = 0; count < numCount; count++) - { - std::vector<void*> inputFrames; - for (int i = 0; i < numInputBuffers; i++) - { - IDirect3DSurface9* pD3D9Surface; - HRESULT res = S_OK; - if (m_pDXVAService) - { - res = m_pDXVAService->CreateSurface(GetMaxEncodeWidth(), GetMaxEncodeHeight(), 0, GetD3D9Format(GetPixelFormat()), D3DPOOL_DEFAULT, 0, DXVA2_VideoProcessorRenderTarget, &pD3D9Surface, nullptr); - } - else - { - res = m_pD3D9Device->CreateOffscreenPlainSurface(GetMaxEncodeWidth(), GetMaxEncodeHeight(), GetD3D9Format(GetPixelFormat()), D3DPOOL_DEFAULT, &pD3D9Surface, nullptr); - } - if (res != S_OK) - { - NVENC_THROW_ERROR("Failed to create d3d9Surfaces", NV_ENC_ERR_OUT_OF_MEMORY); - } - inputFrames.push_back(pD3D9Surface); - } - RegisterInputResources(inputFrames, NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX, GetMaxEncodeWidth(), GetMaxEncodeHeight(), 0, GetPixelFormat(), count == 1 ? true : false); - } -} - -void NvEncoderD3D9::ReleaseInputBuffers() -{ - ReleaseD3D9Resources(); -} - -void NvEncoderD3D9::ReleaseD3D9Resources() -{ - if (!m_hEncoder) - { - return; - } - - UnregisterInputResources(); - - for (uint32_t i = 0; i < m_vInputFrames.size(); ++i) - { - if (m_vInputFrames[i].inputPtr) - { - reinterpret_cast<IDirect3DSurface9*>(m_vInputFrames[i].inputPtr)->Release(); - } - } - m_vInputFrames.clear(); - - for (uint32_t i = 0; i < m_vReferenceFrames.size(); ++i) - { - if (m_vReferenceFrames[i].inputPtr) - { - reinterpret_cast<IDirect3DSurface9*>(m_vReferenceFrames[i].inputPtr)->Release(); - } - } - m_vReferenceFrames.clear(); - - if (m_pDXVAService) - { - m_pDXVAService->Release(); - m_pDXVAService = nullptr; - } - - if (m_pD3D9Device) - { - m_pD3D9Device->Release(); - m_pD3D9Device = nullptr; - } -} diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D9.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D9.h deleted file mode 100644 index 68c448003dc0ee8744f60e6f7f337bfe5864c6ab..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderD3D9.h +++ /dev/null @@ -1,52 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include <vector> -#include <stdint.h> -#include <mutex> -#include <unordered_map> -#include <d3d9.h> -#include <dxva2api.h> -#include "NvEncoder.h" - - -class NvEncoderD3D9 : public NvEncoder -{ -public: - NvEncoderD3D9(IDirect3DDevice9* pD3D9Device, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - IDirectXVideoAccelerationService* pDXVAService = nullptr, uint32_t nExtraOutputDelay = 3, bool bMotionEstimationOnly = false); - virtual ~NvEncoderD3D9(); -private: - - /** - * @brief This function is used to allocate input buffers for encoding. - * This function is an override of virtual function NvEncoder::AllocateInputBuffers(). - * This function creates IDirect3DSurface9* which is used to accept input data. - * To obtain handle to input buffers application must call NvEncoder::GetNextInputFrame() - */ - virtual void AllocateInputBuffers(int32_t numInputBuffers) override; - - /** - * @brief This function is used to release the input buffers allocated for encoding. - * This function is an override of virtual function NvEncoder::ReleaseInputBuffers(). - */ - virtual void ReleaseInputBuffers() override; -private: - /** - * @brief This is a private function to release IDirect3DSurface9 surfaces used for encoding. - */ - void ReleaseD3D9Resources(); -private: - IDirect3DDevice9* m_pD3D9Device = nullptr; - IDirectXVideoAccelerationService* m_pDXVAService = nullptr; -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderGL.cpp b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderGL.cpp deleted file mode 100644 index b82c8b6b93ab476a26233f2938403a3ef8f4195c..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderGL.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* -* Copyright 2017-2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include "NvEncoder/NvEncoderGL.h" - -NvEncoderGL::NvEncoderGL(uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay, bool bMotionEstimationOnly) : - NvEncoder(NV_ENC_DEVICE_TYPE_OPENGL, nullptr, nWidth, nHeight, eBufferFormat, - nExtraOutputDelay, bMotionEstimationOnly) -{ - if (!m_hEncoder) - { - return; - } -} - -NvEncoderGL::~NvEncoderGL() -{ - ReleaseGLResources(); -} - -void NvEncoderGL::ReleaseInputBuffers() -{ - ReleaseGLResources(); -} - -void NvEncoderGL::AllocateInputBuffers(int32_t numInputBuffers) -{ - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder device not initialized", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - int numCount = m_bMotionEstimationOnly ? 2 : 1; - - for (int count = 0; count < numCount; count++) - { - std::vector<void*> inputFrames; - for (int i = 0; i < numInputBuffers; i++) - { - NV_ENC_INPUT_RESOURCE_OPENGL_TEX *pResource = new NV_ENC_INPUT_RESOURCE_OPENGL_TEX; - uint32_t tex; - - glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_RECTANGLE, tex); - - uint32_t chromaHeight = GetNumChromaPlanes(GetPixelFormat()) * GetChromaHeight(GetPixelFormat(), GetMaxEncodeHeight()); - if (GetPixelFormat() == NV_ENC_BUFFER_FORMAT_YV12 || GetPixelFormat() == NV_ENC_BUFFER_FORMAT_IYUV) - chromaHeight = GetChromaHeight(GetPixelFormat(), GetMaxEncodeHeight()); - - glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R8, - GetWidthInBytes(GetPixelFormat(), GetMaxEncodeWidth()), - GetMaxEncodeHeight() + chromaHeight, - 0, GL_RED, GL_UNSIGNED_BYTE, NULL); - - glBindTexture(GL_TEXTURE_RECTANGLE, 0); - - pResource->texture = tex; - pResource->target = GL_TEXTURE_RECTANGLE; - inputFrames.push_back(pResource); - } - RegisterInputResources(inputFrames, NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX, - GetMaxEncodeWidth(), - GetMaxEncodeHeight(), - GetWidthInBytes(GetPixelFormat(), GetMaxEncodeWidth()), - GetPixelFormat(), count == 1 ? true : false); - } -} - -void NvEncoderGL::ReleaseGLResources() -{ - if (!m_hEncoder) - { - return; - } - - UnregisterInputResources(); - - for (int i = 0; i < m_vInputFrames.size(); ++i) - { - if (m_vInputFrames[i].inputPtr) - { - NV_ENC_INPUT_RESOURCE_OPENGL_TEX *pResource = (NV_ENC_INPUT_RESOURCE_OPENGL_TEX *)m_vInputFrames[i].inputPtr; - if (pResource) - { - glDeleteTextures(1, &pResource->texture); - delete pResource; - } - } - } - m_vInputFrames.clear(); - - for (int i = 0; i < m_vReferenceFrames.size(); ++i) - { - if (m_vReferenceFrames[i].inputPtr) - { - NV_ENC_INPUT_RESOURCE_OPENGL_TEX *pResource = (NV_ENC_INPUT_RESOURCE_OPENGL_TEX *)m_vReferenceFrames[i].inputPtr; - if (pResource) - { - glDeleteTextures(1, &pResource->texture); - delete pResource; - } - } - } - m_vReferenceFrames.clear(); -} diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderGL.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderGL.h deleted file mode 100644 index f3cdf1305b4681f490bdcfddeadc0518a6a25729..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderGL.h +++ /dev/null @@ -1,45 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include <iostream> -#include "NvEncoder/NvEncoder.h" -#include <unordered_map> -#include <GL/glew.h> - -class NvEncoderGL : public NvEncoder -{ -public: - /** - * @brief The constructor for the NvEncoderGL class - * An OpenGL context must be current to the calling thread/process when - * creating an instance of this class. - */ - NvEncoderGL(uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay = 3, bool bMotionEstimationOnly = false); - - virtual ~NvEncoderGL(); -private: - /** - * @brief This function is used to allocate input buffers for encoding. - * This function is an override of virtual function NvEncoder::AllocateInputBuffers(). - * This function creates OpenGL textures which are used to hold input data. - * To obtain handle to input buffers, the application must call NvEncoder::GetNextInputFrame() - */ - virtual void AllocateInputBuffers(int32_t numInputBuffers) override; - - /** - * @brief This function is used to release the input buffers allocated for encoding. - * This function is an override of virtual function NvEncoder::ReleaseInputBuffers(). - */ - virtual void ReleaseInputBuffers() override; -private: - void ReleaseGLResources(); -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemCuda.cpp b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemCuda.cpp deleted file mode 100644 index 947e62cea9cfa2ab5b83cfafef2a3163309ffccd..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemCuda.cpp +++ /dev/null @@ -1,282 +0,0 @@ -/* -* Copyright 2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include "NvEncoder/NvEncoderOutputInVidMemCuda.h" - - -NvEncoderOutputInVidMemCuda::NvEncoderOutputInVidMemCuda(CUcontext cuContext, - uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - bool bMotionEstimationOnly) - : NvEncoderCuda(cuContext, nWidth, nHeight, eBufferFormat, 0, bMotionEstimationOnly, true) -{ -} - -NvEncoderOutputInVidMemCuda::~NvEncoderOutputInVidMemCuda() -{ - try - { - FlushEncoder(); - ReleaseOutputBuffers(); - } - catch (...) - { - - } -} - -uint32_t NvEncoderOutputInVidMemCuda::GetOutputBufferSize() -{ - uint32_t bufferSize = 0; - - if (m_bMotionEstimationOnly) - { - uint32_t encodeWidthInMbs = (GetEncodeWidth() + 15) >> 4; - uint32_t encodeHeightInMbs = (GetEncodeHeight() + 15) >> 4; - - bufferSize = encodeWidthInMbs * encodeHeightInMbs * sizeof(NV_ENC_H264_MV_DATA); - } - else - { - // 2-times the input size - bufferSize = GetFrameSize() * 2; - - bufferSize += sizeof(NV_ENC_ENCODE_OUT_PARAMS); - } - - bufferSize = ALIGN_UP(bufferSize, 4); - - return bufferSize; -} - -void NvEncoderOutputInVidMemCuda::AllocateOutputBuffers(uint32_t numOutputBuffers) -{ - uint32_t size = GetOutputBufferSize(); - - CUDA_DRVAPI_CALL(cuCtxPushCurrent(m_cuContext)); - - for (uint32_t i = 0; i < numOutputBuffers; i++) - { - CUdeviceptr pDeviceFrame; - - CUresult cuResult = cuMemAlloc(&pDeviceFrame, size); - if (cuResult != CUDA_SUCCESS) - { - NVENC_THROW_ERROR("cuMemAlloc Failed", NV_ENC_ERR_OUT_OF_MEMORY); - } - - m_pOutputBuffers.push_back((NV_ENC_OUTPUT_PTR)pDeviceFrame); - } - - CUDA_DRVAPI_CALL(cuCtxPopCurrent(NULL)); - - RegisterOutputResources(size); -} - -void NvEncoderOutputInVidMemCuda::ReleaseOutputBuffers() -{ - if (!m_hEncoder) - { - return; - } - - UnregisterOutputResources(); - - for (uint32_t i = 0; i < m_pOutputBuffers.size(); ++i) - { - cuMemFree(reinterpret_cast<CUdeviceptr>(m_pOutputBuffers[i])); - } - - m_pOutputBuffers.clear(); -} - -void NvEncoderOutputInVidMemCuda::RegisterOutputResources(uint32_t bfrSize) -{ - NV_ENC_BUFFER_USAGE bufferUsage = m_bMotionEstimationOnly? NV_ENC_OUTPUT_MOTION_VECTOR : NV_ENC_OUTPUT_BITSTREAM; - - for (uint32_t i = 0; i < m_pOutputBuffers.size(); ++i) - { - if (m_pOutputBuffers[i]) - { - NV_ENC_REGISTERED_PTR registeredPtr = RegisterResource((void *)m_pOutputBuffers[i], - NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, - bfrSize, 1, bfrSize, NV_ENC_BUFFER_FORMAT_U8, bufferUsage); - - m_vRegisteredResourcesOutputBuffer.push_back(registeredPtr); - } - } -} - -void NvEncoderOutputInVidMemCuda::UnregisterOutputResources() -{ - for (uint32_t i = 0; i < m_vMappedOutputBuffers.size(); ++i) - { - if (m_vMappedOutputBuffers[i]) - { - m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedOutputBuffers[i]); - } - } - m_vMappedOutputBuffers.clear(); - - for (uint32_t i = 0; i < m_vRegisteredResourcesOutputBuffer.size(); ++i) - { - if (m_vRegisteredResourcesOutputBuffer[i]) - { - m_nvenc.nvEncUnregisterResource(m_hEncoder, m_vRegisteredResourcesOutputBuffer[i]); - } - } - - m_vRegisteredResourcesOutputBuffer.clear(); -} - -void NvEncoderOutputInVidMemCuda::CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams) -{ - NvEncoder::CreateEncoder(pEncoderParams); - - AllocateOutputBuffers(m_nEncoderBuffer); - - m_vMappedOutputBuffers.resize(m_nEncoderBuffer, nullptr); -} - -void NvEncoderOutputInVidMemCuda::MapResources(uint32_t bfrIdx) -{ - NvEncoder::MapResources(bfrIdx); - - //map output surface - NV_ENC_MAP_INPUT_RESOURCE mapInputResourceBitstreamBuffer = { NV_ENC_MAP_INPUT_RESOURCE_VER }; - mapInputResourceBitstreamBuffer.registeredResource = m_vRegisteredResourcesOutputBuffer[bfrIdx]; - NVENC_API_CALL(m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResourceBitstreamBuffer)); - m_vMappedOutputBuffers[bfrIdx] = mapInputResourceBitstreamBuffer.mappedResource; -} - -void NvEncoderOutputInVidMemCuda::EncodeFrame(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer, NV_ENC_PIC_PARAMS *pPicParams) -{ - pOutputBuffer.clear(); - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder device not found", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - int bfrIdx = m_iToSend % m_nEncoderBuffer; - - MapResources(bfrIdx); - - NVENCSTATUS nvStatus = DoEncode(m_vMappedInputBuffers[bfrIdx], m_vMappedOutputBuffers[bfrIdx], pPicParams); - - if (nvStatus == NV_ENC_SUCCESS || nvStatus == NV_ENC_ERR_NEED_MORE_INPUT) - { - m_iToSend++; - GetEncodedPacket(pOutputBuffer, true); - } - else - { - NVENC_THROW_ERROR("nvEncEncodePicture API failed", nvStatus); - } -} - -void NvEncoderOutputInVidMemCuda::EndEncode(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer) -{ - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder device not initialized", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - - SendEOS(); - - GetEncodedPacket(pOutputBuffer, false); -} - -void NvEncoderOutputInVidMemCuda::RunMotionEstimation(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer) -{ - pOutputBuffer.clear(); - - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_NO_ENCODE_DEVICE); - return; - } - - const uint32_t bfrIdx = m_iToSend % m_nEncoderBuffer; - - MapResources(bfrIdx); - - NVENCSTATUS nvStatus = DoMotionEstimation(m_vMappedInputBuffers[bfrIdx], m_vMappedRefBuffers[bfrIdx], m_vMappedOutputBuffers[bfrIdx]); - - if (nvStatus == NV_ENC_SUCCESS) - { - m_iToSend++; - GetEncodedPacket(pOutputBuffer, true); - } - else - { - NVENC_THROW_ERROR("nvEncRunMotionEstimationOnly API failed", nvStatus); - } -} - -void NvEncoderOutputInVidMemCuda::GetEncodedPacket(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer, bool bOutputDelay) -{ - unsigned i = 0; - int iEnd = bOutputDelay ? m_iToSend - m_nOutputDelay : m_iToSend; - - for (; m_iGot < iEnd; m_iGot++) - { - if (m_vMappedOutputBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedOutputBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedOutputBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - - if (m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - - if (m_bMotionEstimationOnly && m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - - pOutputBuffer.push_back(m_pOutputBuffers[(m_iGot % m_nEncoderBuffer)]); - - i++; - } -} - -void NvEncoderOutputInVidMemCuda::FlushEncoder() -{ - if (!m_hEncoder) - { - return; - } - - if (!m_bMotionEstimationOnly) - { - std::vector<NV_ENC_OUTPUT_PTR> pOutputBuffer; - EndEncode(pOutputBuffer); - } -} - -void NvEncoderOutputInVidMemCuda::DestroyEncoder() -{ - if (!m_hEncoder) - { - return; - } - - // Incase of error it is possible for buffers still mapped to encoder. - // flush the encoder queue and then unmapped it if any surface is still mapped - FlushEncoder(); - - ReleaseOutputBuffers(); - - NvEncoder::DestroyEncoder(); -} \ No newline at end of file diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemCuda.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemCuda.h deleted file mode 100644 index 79f0b816f8d05e046efc4af1f0c33e9e8c9b2d43..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemCuda.h +++ /dev/null @@ -1,144 +0,0 @@ -/* -* Copyright 2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include <vector> -#include "nvEncodeAPI.h" -#include <stdint.h> -#include <mutex> -#include <string> -#include <iostream> -#include <sstream> -#include <string.h> -#include "NvEncoder/NvEncoderCuda.h" - -#define ALIGN_UP(s,a) (((s) + (a) - 1) & ~((a) - 1)) - - -/** -* @brief Class for encode or ME only output in video memory feature for Cuda interfaces. -*/ -class NvEncoderOutputInVidMemCuda : public NvEncoderCuda -{ -public: - /** - * @brief NvEncoderOutputInVidMem class constructor. - */ - NvEncoderOutputInVidMemCuda(CUcontext cuContext, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - bool bMotionEstimationOnly = false); - - /** - * @brief NvEncoder class virtual destructor. - */ - virtual ~NvEncoderOutputInVidMemCuda(); - - /** - * @brief This function is used to initialize the encoder session. - * Application must call this function to initialize the encoder, before - * starting to encode or motion estimate any frames. - */ - void CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams); - - /** - * @brief This function is used to encode a frame. - * Applications must call EncodeFrame() function to encode the uncompressed - * data, which has been copied to an input buffer obtained from the - * GetNextInputFrame() function. - * This function returns video memory buffer pointers containing compressed data - * in pOutputBuffer. If there is buffering enabled, this may return without - * any data in pOutputBuffer. - */ - void EncodeFrame(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer, NV_ENC_PIC_PARAMS *pPicParams = nullptr); - - /** - * @brief This function to flush the encoder queue. - * The encoder might be queuing frames for B picture encoding or lookahead; - * the application must call EndEncode() to get all the queued encoded frames - * from the encoder. The application must call this function before destroying - * an encoder session. Video memory buffer pointer containing compressed data - * is returned in pOutputBuffer. - */ - void EndEncode(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer); - - /** - * @brief This function is used to run motion estimation. - * This is used to run motion estimation on a a pair of frames. The - * application must copy the reference frame data to the buffer obtained - * by calling GetNextReferenceFrame(), and copy the input frame data to - * the buffer obtained by calling GetNextInputFrame() before calling the - * RunMotionEstimation() function. - * This function returns video memory buffer pointers containing - * motion vector data in pOutputBuffer. - */ - void RunMotionEstimation(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer); - - /** - * @brief This function is used to destroy the encoder session. - * Application must call this function to destroy the encoder session and - * clean up any allocated resources. The application must call EndEncode() - * function to get any queued encoded frames before calling DestroyEncoder(). - */ - void DestroyEncoder(); - - /** - * @brief This function is used to get the size of output buffer required to be - * allocated in order to store the output. - */ - uint32_t GetOutputBufferSize(); - -private: - - /** - * @brief This function is used to allocate output buffers in video memory for storing - * encode or motion estimation output. - */ - void AllocateOutputBuffers(uint32_t numOutputBuffers); - - /** - * @brief This function is used to release output buffers. - */ - void ReleaseOutputBuffers(); - - /** - * @brief This function is used to register output buffers with NvEncodeAPI. - */ - void RegisterOutputResources(uint32_t bfrSize); - - /** - * @brief This function is used to unregister output resources which had been previously registered for encoding - * using RegisterOutputResources() function. - */ - void UnregisterOutputResources(); - - /** - * @brief This function is used to map the input and output buffers to NvEncodeAPI. - */ - void MapResources(uint32_t bfrIdx); - - /** - * @brief This is a private function which is used to get video memory buffer pointer containing compressed data - * or motion estimation output from the encoder HW. - * This is called by EncodeFrame() function. If there is buffering enabled, - * this may return without any output data. - */ - void GetEncodedPacket(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer, bool bOutputDelay); - - /** - * @brief This function is used to flush the encoder queue. - */ - void FlushEncoder(); - -private: - std::vector<NV_ENC_OUTPUT_PTR> m_vMappedOutputBuffers; - std::vector<NV_ENC_OUTPUT_PTR> m_pOutputBuffers; - std::vector<NV_ENC_REGISTERED_PTR> m_vRegisteredResourcesOutputBuffer; -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemD3D11.cpp b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemD3D11.cpp deleted file mode 100644 index 216fe2b746a1d61da043e789b2444b2a79a2733d..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemD3D11.cpp +++ /dev/null @@ -1,291 +0,0 @@ -/* -* Copyright 2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include "NvEncoder/NvEncoder.h" -#include "NvEncoder/NvEncoderOutputInVidMemD3D11.h" - - -NvEncoderOutputInVidMemD3D11::NvEncoderOutputInVidMemD3D11(ID3D11Device* pD3D11Device, - uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - bool bMotionEstimationOnly) - : NvEncoderD3D11(pD3D11Device, nWidth, nHeight, eBufferFormat, 0, - bMotionEstimationOnly, true) -{ -} - -NvEncoderOutputInVidMemD3D11::~NvEncoderOutputInVidMemD3D11() -{ - try - { - FlushEncoder(); - ReleaseOutputBuffers(); - } - catch (...) - { - - } -} - -uint32_t NvEncoderOutputInVidMemD3D11::GetOutputBufferSize() -{ - uint32_t bufferSize = 0; - - if (m_bMotionEstimationOnly) - { - uint32_t encodeWidthInMbs = (GetEncodeWidth() + 15) >> 4; - uint32_t encodeHeightInMbs = (GetEncodeHeight() + 15) >> 4; - - bufferSize = encodeWidthInMbs * encodeHeightInMbs * sizeof(NV_ENC_H264_MV_DATA); - } - else - { - // 2-times the input size - bufferSize = GetFrameSize() * 2; - - bufferSize += sizeof(NV_ENC_ENCODE_OUT_PARAMS); - } - - bufferSize = ALIGN_UP(bufferSize, 4); - - return bufferSize; -} - -void NvEncoderOutputInVidMemD3D11::AllocateOutputBuffers(uint32_t numOutputBuffers) -{ - uint32_t size = GetOutputBufferSize(); - D3D11_BUFFER_DESC desc; - - ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC)); - - desc.ByteWidth = size; - desc.Usage = D3D11_USAGE_DEFAULT; - desc.BindFlags = D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_SHADER_RESOURCE; - desc.CPUAccessFlags = 0; - - for (uint32_t i = 0; i < numOutputBuffers; i++) - { - ID3D11Buffer *dx11bfr = NULL; - - if (m_pD3D11Device->CreateBuffer(&desc, NULL, (ID3D11Buffer **)&dx11bfr) != S_OK) - { - NVENC_THROW_ERROR("Failed to create ID3D11Buffer", NV_ENC_ERR_OUT_OF_MEMORY); - } - - m_pOutputBuffers.push_back((NV_ENC_OUTPUT_PTR)dx11bfr); - } - - RegisterOutputResources(size); -} - -void NvEncoderOutputInVidMemD3D11::ReleaseOutputBuffers() -{ - if (!m_hEncoder) - { - return; - } - - UnregisterOutputResources(); - - for (uint32_t i = 0; i < m_pOutputBuffers.size(); ++i) - { - reinterpret_cast<ID3D11Buffer *>(m_pOutputBuffers[i])->Release(); - } - m_pOutputBuffers.clear(); -} - -void NvEncoderOutputInVidMemD3D11::RegisterOutputResources(uint32_t bfrSize) -{ - NV_ENC_BUFFER_USAGE bufferUsage = m_bMotionEstimationOnly? NV_ENC_OUTPUT_MOTION_VECTOR : NV_ENC_OUTPUT_BITSTREAM; - - for (uint32_t i = 0; i < m_pOutputBuffers.size(); ++i) - { - if (m_pOutputBuffers[i]) - { - NV_ENC_REGISTERED_PTR registeredPtr = RegisterResource(m_pOutputBuffers[i], - NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX, bfrSize, 1, 0, - NV_ENC_BUFFER_FORMAT_U8, bufferUsage); - - m_vRegisteredResourcesOutputBuffer.push_back(registeredPtr); - } - } -} - -void NvEncoderOutputInVidMemD3D11::UnregisterOutputResources() -{ - for (uint32_t i = 0; i < m_vMappedOutputBuffers.size(); ++i) - { - if (m_vMappedOutputBuffers[i]) - { - m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedOutputBuffers[i]); - } - } - m_vMappedOutputBuffers.clear(); - - for (uint32_t i = 0; i < m_vRegisteredResourcesOutputBuffer.size(); ++i) - { - if (m_vRegisteredResourcesOutputBuffer[i]) - { - m_nvenc.nvEncUnregisterResource(m_hEncoder, m_vRegisteredResourcesOutputBuffer[i]); - } - } - m_vRegisteredResourcesOutputBuffer.clear(); -} - -void NvEncoderOutputInVidMemD3D11::CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams) -{ - NvEncoder::CreateEncoder(pEncoderParams); - - AllocateOutputBuffers(m_nEncoderBuffer); - - m_vMappedOutputBuffers.resize(m_nEncoderBuffer, nullptr); -} - -void NvEncoderOutputInVidMemD3D11::MapResources(uint32_t bfrIdx) -{ - NvEncoder::MapResources(bfrIdx); - - //map output surface - NV_ENC_MAP_INPUT_RESOURCE mapInputResourceBitstreamBuffer = { NV_ENC_MAP_INPUT_RESOURCE_VER }; - mapInputResourceBitstreamBuffer.registeredResource = m_vRegisteredResourcesOutputBuffer[bfrIdx]; - NVENC_API_CALL(m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResourceBitstreamBuffer)); - m_vMappedOutputBuffers[bfrIdx] = mapInputResourceBitstreamBuffer.mappedResource; -} - -void NvEncoderOutputInVidMemD3D11::EncodeFrame(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer, NV_ENC_PIC_PARAMS *pPicParams) -{ - pOutputBuffer.clear(); - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder device not found", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - int bfrIdx = m_iToSend % m_nEncoderBuffer; - - MapResources(bfrIdx); - - NVENCSTATUS nvStatus = DoEncode(m_vMappedInputBuffers[bfrIdx], m_vMappedOutputBuffers[bfrIdx], pPicParams); - - if (nvStatus == NV_ENC_SUCCESS || nvStatus == NV_ENC_ERR_NEED_MORE_INPUT) - { - m_iToSend++; - GetEncodedPacket(pOutputBuffer, true); - } - else - { - NVENC_THROW_ERROR("nvEncEncodePicture API failed", nvStatus); - } -} - -void NvEncoderOutputInVidMemD3D11::EndEncode(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer) -{ - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder device not initialized", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - - SendEOS(); - - GetEncodedPacket(pOutputBuffer, false); -} - -void NvEncoderOutputInVidMemD3D11::RunMotionEstimation(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer) -{ - pOutputBuffer.clear(); - - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_NO_ENCODE_DEVICE); - return; - } - - const uint32_t bfrIdx = m_iToSend % m_nEncoderBuffer; - - MapResources(bfrIdx); - - NVENCSTATUS nvStatus = DoMotionEstimation(m_vMappedInputBuffers[bfrIdx], m_vMappedRefBuffers[bfrIdx], m_vMappedOutputBuffers[bfrIdx]); - - if (nvStatus == NV_ENC_SUCCESS) - { - m_iToSend++; - GetEncodedPacket(pOutputBuffer, true); - } - else - { - NVENC_THROW_ERROR("nvEncRunMotionEstimationOnly API failed", nvStatus); - } -} - -void NvEncoderOutputInVidMemD3D11::GetEncodedPacket(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer , bool bOutputDelay) -{ - unsigned i = 0; - int iEnd = bOutputDelay ? m_iToSend - m_nOutputDelay : m_iToSend; - - for (; m_iGot < iEnd; m_iGot++) - { - if (m_vMappedOutputBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedOutputBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedOutputBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - - if (m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - - if (m_bMotionEstimationOnly && m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - - pOutputBuffer.push_back(m_pOutputBuffers[(m_iGot % m_nEncoderBuffer)]); - - i++; - } -} - -void NvEncoderOutputInVidMemD3D11::FlushEncoder() -{ - if (!m_hEncoder) - { - return; - } - - if (!m_bMotionEstimationOnly) - { - try - { - std::vector<NV_ENC_OUTPUT_PTR> pOutputBuffer; - EndEncode(pOutputBuffer); - } - catch (...) - { - - } - } -} - -void NvEncoderOutputInVidMemD3D11::DestroyEncoder() -{ - if (!m_hEncoder) - { - return; - } - - // Incase of error it is possible for buffers still mapped to encoder. - // flush the encoder queue and then unmapped it if any surface is still mapped - FlushEncoder(); - ReleaseOutputBuffers(); - - NvEncoder::DestroyEncoder(); -} diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemD3D11.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemD3D11.h deleted file mode 100644 index 224f1db9350dd5d766ccda12306e1928cd61f6d0..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/NvCodec/NvEncoder/NvEncoderOutputInVidMemD3D11.h +++ /dev/null @@ -1,144 +0,0 @@ -/* -* Copyright 2019 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include <vector> -#include "nvEncodeAPI.h" -#include <stdint.h> -#include <mutex> -#include <string> -#include <iostream> -#include <sstream> -#include <string.h> -#include "NvEncoder/NvEncoderD3D11.h" - -#define ALIGN_UP(s,a) (((s) + (a) - 1) & ~((a) - 1)) - - -/** -* @brief Class for encode or ME only output in video memory feature for D3D11 interfaces. -*/ -class NvEncoderOutputInVidMemD3D11 : public NvEncoderD3D11 -{ -public: - /** - * @brief NvEncoderOutputInVidMemD3D11 class constructor. - */ - NvEncoderOutputInVidMemD3D11(ID3D11Device* pD3D11Device, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - bool bMotionEstimationOnly = false); - - /** - * @brief NvEncoder class virtual destructor. - */ - virtual ~NvEncoderOutputInVidMemD3D11(); - - /** - * @brief This function is used to initialize the encoder session. - * Application must call this function to initialize the encoder, before - * starting to encode or motion estimate any frames. - */ - void CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams); - - /** - * @brief This function is used to encode a frame. - * Applications must call EncodeFrame() function to encode the uncompressed - * data, which has been copied to an input buffer obtained from the - * GetNextInputFrame() function. - * This function returns video memory buffer pointers containing compressed data - * in pOutputBuffer. If there is buffering enabled, this may return without - * any data in pOutputBuffer. - */ - void EncodeFrame(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer, NV_ENC_PIC_PARAMS *pPicParams = nullptr); - - /** - * @brief This function to flush the encoder queue. - * The encoder might be queuing frames for B picture encoding or lookahead; - * the application must call EndEncode() to get all the queued encoded frames - * from the encoder. The application must call this function before destroying - * an encoder session. Video memory buffer pointer containing compressed data - * is returned in pOutputBuffer. - */ - void EndEncode(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer); - - /** - * @brief This function is used to run motion estimation. - * This is used to run motion estimation on a a pair of frames. The - * application must copy the reference frame data to the buffer obtained - * by calling GetNextReferenceFrame(), and copy the input frame data to - * the buffer obtained by calling GetNextInputFrame() before calling the - * RunMotionEstimation() function. - * This function returns video memory buffer pointers containing - * motion vector data in pOutputBuffer. - */ - void RunMotionEstimation(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer); - - /** - * @brief This function is used to destroy the encoder session. - * Application must call this function to destroy the encoder session and - * clean up any allocated resources. The application must call EndEncode() - * function to get any queued encoded frames before calling DestroyEncoder(). - */ - void DestroyEncoder(); - - /** - * @brief This function is used to get the size of output buffer required to be - * allocated in order to store the output. - */ - uint32_t GetOutputBufferSize(); - -private: - - /** - * @brief This function is used to allocate output buffers in video memory for storing - * encode or motion estimation output. - */ - void AllocateOutputBuffers(uint32_t numOutputBuffers); - - /** - * @brief This function is used to release output buffers. - */ - void ReleaseOutputBuffers(); - - /** - * @brief This function is used to register output buffers with NvEncodeAPI. - */ - void RegisterOutputResources(uint32_t bfrSize); - - /** - * @brief This function is used to unregister output resources which had been previously registered for encoding - * using RegisterOutputResources() function. - */ - void UnregisterOutputResources(); - - /** - * @brief This function is used to map the input and output buffers to NvEncodeAPI. - */ - void MapResources(uint32_t bfrIdx); - - /** - * @brief This is a private function which is used to get video memory buffer pointer containing compressed data - * or motion estimation output from the encoder HW. - * This is called by EncodeFrame() function. If there is buffering enabled, - * this may return without any output data. - */ - void GetEncodedPacket(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer, bool bOutputDelay); - - /** - * @brief This function is used to flush the encoder queue. - */ - void FlushEncoder(); - -private: - std::vector<NV_ENC_OUTPUT_PTR> m_vMappedOutputBuffers; - std::vector<NV_ENC_OUTPUT_PTR> m_pOutputBuffers; - std::vector<NV_ENC_REGISTERED_PTR> m_vRegisteredResourcesOutputBuffer; -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/BitDepth.cu b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/BitDepth.cu deleted file mode 100644 index 9fcc64f5a0952a71e87628f1538376abcf7e0285..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/BitDepth.cu +++ /dev/null @@ -1,54 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include <cuda_runtime.h> -#include <stdint.h> -#include <stdio.h> - -static __global__ void ConvertUInt8ToUInt16Kernel(uint8_t *dpUInt8, uint16_t *dpUInt16, int nSrcPitch, int nDestPitch, int nWidth, int nHeight) -{ - int x = blockIdx.x * blockDim.x + threadIdx.x, - y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x >= nWidth || y >= nHeight) - { - return; - } - int destStrideInPixels = nDestPitch / (sizeof(uint16_t)); - *(uchar2 *)&dpUInt16[y * destStrideInPixels + x] = uchar2{ 0, dpUInt8[y * nSrcPitch + x] }; -} - -static __global__ void ConvertUInt16ToUInt8Kernel(uint16_t *dpUInt16, uint8_t *dpUInt8, int nSrcPitch, int nDestPitch, int nWidth, int nHeight) -{ - int x = blockIdx.x * blockDim.x + threadIdx.x, - y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x >= nWidth || y >= nHeight) - { - return; - } - int srcStrideInPixels = nSrcPitch / (sizeof(uint16_t)); - dpUInt8[y * nDestPitch + x] = ((uchar2 *)&dpUInt16[y * srcStrideInPixels + x])->y; -} - -void ConvertUInt8ToUInt16(uint8_t *dpUInt8, uint16_t *dpUInt16, int nSrcPitch, int nDestPitch, int nWidth, int nHeight) -{ - dim3 blockSize(16, 16, 1); - dim3 gridSize(((uint32_t)nWidth + blockSize.x - 1) / blockSize.x, ((uint32_t)nHeight + blockSize.y - 1) / blockSize.y, 1); - ConvertUInt8ToUInt16Kernel <<< gridSize, blockSize >>>(dpUInt8, dpUInt16, nSrcPitch, nDestPitch, nWidth, nHeight); -} - -void ConvertUInt16ToUInt8(uint16_t *dpUInt16, uint8_t *dpUInt8, int nSrcPitch, int nDestPitch, int nWidth, int nHeight) -{ - dim3 blockSize(16, 16, 1); - dim3 gridSize(((uint32_t)nWidth + blockSize.x - 1) / blockSize.x, ((uint32_t)nHeight + blockSize.y - 1) / blockSize.y, 1); - ConvertUInt16ToUInt8Kernel <<<gridSize, blockSize >>>(dpUInt16, dpUInt8, nSrcPitch, nDestPitch, nWidth, nHeight); -} diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/ColorSpace.cu b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/ColorSpace.cu deleted file mode 100644 index b44bf529ce6c14a507fa6936c163be558d4fef60..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/ColorSpace.cu +++ /dev/null @@ -1,442 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include "ColorSpace.h" - -__constant__ float matYuv2Rgb[3][3]; -__constant__ float matRgb2Yuv[3][3]; - -void inline GetConstants(int iMatrix, float &wr, float &wb, int &black, int &white, int &max) { - // Default is BT709 - wr = 0.2126f; wb = 0.0722f; - black = 16; white = 235; - max = 255; - if (iMatrix == ColorSpaceStandard_BT601) { - wr = 0.2990f; wb = 0.1140f; - } else if (iMatrix == ColorSpaceStandard_BT2020) { - wr = 0.2627f; wb = 0.0593f; - // 10-bit only - black = 64 << 6; white = 940 << 6; - max = (1 << 16) - 1; - } -} - -void SetMatYuv2Rgb(int iMatrix) { - float wr, wb; - int black, white, max; - GetConstants(iMatrix, wr, wb, black, white, max); - float mat[3][3] = { - 1.0f, 0.0f, (1.0f - wr) / 0.5f, - 1.0f, -wb * (1.0f - wb) / 0.5f / (1 - wb - wr), -wr * (1 - wr) / 0.5f / (1 - wb - wr), - 1.0f, (1.0f - wb) / 0.5f, 0.0f, - }; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - mat[i][j] = (float)(1.0 * max / (white - black) * mat[i][j]); - } - } - cudaMemcpyToSymbol(matYuv2Rgb, mat, sizeof(mat)); -} - -void SetMatRgb2Yuv(int iMatrix) { - float wr, wb; - int black, white, max; - GetConstants(iMatrix, wr, wb, black, white, max); - float mat[3][3] = { - wr, 1.0f - wb - wr, wb, - -0.5f * wr / (1.0f - wb), -0.5f * (1 - wb - wr) / (1.0f - wb), 0.5f, - 0.5f, -0.5f * (1.0f - wb - wr) / (1.0f - wr), -0.5f * wb / (1.0f - wr), - }; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - mat[i][j] = (float)(1.0 * (white - black) / max * mat[i][j]); - } - } - cudaMemcpyToSymbol(matRgb2Yuv, mat, sizeof(mat)); -} - -template<class T> -__device__ static T Clamp(T x, T lower, T upper) { - return x < lower ? lower : (x > upper ? upper : x); -} - -template<class Rgb, class YuvUnit> -__device__ inline Rgb YuvToRgbForPixel(YuvUnit y, YuvUnit u, YuvUnit v) { - const int - low = 1 << (sizeof(YuvUnit) * 8 - 4), - mid = 1 << (sizeof(YuvUnit) * 8 - 1); - float fy = (int)y - low, fu = (int)u - mid, fv = (int)v - mid; - const float maxf = (1 << sizeof(YuvUnit) * 8) - 1.0f; - YuvUnit - r = (YuvUnit)Clamp(matYuv2Rgb[0][0] * fy + matYuv2Rgb[0][1] * fu + matYuv2Rgb[0][2] * fv, 0.0f, maxf), - g = (YuvUnit)Clamp(matYuv2Rgb[1][0] * fy + matYuv2Rgb[1][1] * fu + matYuv2Rgb[1][2] * fv, 0.0f, maxf), - b = (YuvUnit)Clamp(matYuv2Rgb[2][0] * fy + matYuv2Rgb[2][1] * fu + matYuv2Rgb[2][2] * fv, 0.0f, maxf); - - Rgb rgb{}; - const int nShift = abs((int)sizeof(YuvUnit) - (int)sizeof(rgb.c.r)) * 8; - if (sizeof(YuvUnit) >= sizeof(rgb.c.r)) { - rgb.c.r = r >> nShift; - rgb.c.g = g >> nShift; - rgb.c.b = b >> nShift; - } else { - rgb.c.r = r << nShift; - rgb.c.g = g << nShift; - rgb.c.b = b << nShift; - } - return rgb; -} - -template<class Rgb, class YuvUnit> -__device__ inline Rgb YuvToYuvForPixel(YuvUnit y, YuvUnit u, YuvUnit v) { - Rgb rgb{}; - /*const int nShift = abs((int)sizeof(YuvUnit) - (int)sizeof(rgb.c.y)) * 8; - if (sizeof(YuvUnit) >= sizeof(rgb.c.y)) { - rgb.c.y = y >> nShift; - rgb.c.u = u >> nShift; - rgb.c.v = v >> nShift; - } else { - rgb.c.y = y << nShift; - rgb.c.u = u << nShift; - rgb.c.v = v << nShift; - }*/ - - rgb.c.y = y; - rgb.c.u = u; - rgb.c.v = v; - return rgb; -} - -template<class YuvUnitx2, class Rgb, class RgbIntx2> -__global__ static void YuvToRgbKernel(uint8_t *pYuv, int nYuvPitch, uint8_t *pRgb, int nRgbPitch, int nWidth, int nHeight) { - int x = (threadIdx.x + blockIdx.x * blockDim.x) * 2; - int y = (threadIdx.y + blockIdx.y * blockDim.y) * 2; - if (x + 1 >= nWidth || y + 1 >= nHeight) { - return; - } - - uint8_t *pSrc = pYuv + x * sizeof(YuvUnitx2) / 2 + y * nYuvPitch; - uint8_t *pDst = pRgb + x * sizeof(Rgb) + y * nRgbPitch; - - YuvUnitx2 l0 = *(YuvUnitx2 *)pSrc; - YuvUnitx2 l1 = *(YuvUnitx2 *)(pSrc + nYuvPitch); - YuvUnitx2 ch = *(YuvUnitx2 *)(pSrc + (nHeight - y / 2) * nYuvPitch); - - *(RgbIntx2 *)pDst = RgbIntx2 { - YuvToRgbForPixel<Rgb>(l0.x, ch.x, ch.y).d, - YuvToRgbForPixel<Rgb>(l0.y, ch.x, ch.y).d, - }; - *(RgbIntx2 *)(pDst + nRgbPitch) = RgbIntx2 { - YuvToRgbForPixel<Rgb>(l1.x, ch.x, ch.y).d, - YuvToRgbForPixel<Rgb>(l1.y, ch.x, ch.y).d, - }; -} - -template<class YuvUnitx2, class Rgb, class RgbIntx2> -__global__ static void YuvToYuvKernel(uint8_t *pYuv, int nYuvPitch, uint8_t *pRgb, int nRgbPitch, int nWidth, int nHeight) { - int x = (threadIdx.x + blockIdx.x * blockDim.x) * 2; - int y = (threadIdx.y + blockIdx.y * blockDim.y) * 2; - if (x + 1 >= nWidth || y + 1 >= nHeight) { - return; - } - - uint8_t *pSrc = pYuv + x * sizeof(YuvUnitx2) / 2 + y * nYuvPitch; - uint8_t *pDst = pRgb + x * sizeof(Rgb) + y * nRgbPitch; - - YuvUnitx2 l0 = *(YuvUnitx2 *)pSrc; - YuvUnitx2 l1 = *(YuvUnitx2 *)(pSrc + nYuvPitch); - YuvUnitx2 ch = *(YuvUnitx2 *)(pSrc + (nHeight - y / 2) * nYuvPitch); - - *(RgbIntx2 *)pDst = RgbIntx2 { - YuvToYuvForPixel<Rgb>(l0.x, ch.x, ch.y).d, - YuvToYuvForPixel<Rgb>(l0.y, ch.x, ch.y).d, - }; - *(RgbIntx2 *)(pDst + nRgbPitch) = RgbIntx2 { - YuvToYuvForPixel<Rgb>(l1.x, ch.x, ch.y).d, - YuvToYuvForPixel<Rgb>(l1.y, ch.x, ch.y).d, - }; -} - -template<class YuvUnitx2, class Rgb, class RgbIntx2> -__global__ static void Yuv444ToRgbKernel(uint8_t *pYuv, int nYuvPitch, uint8_t *pRgb, int nRgbPitch, int nWidth, int nHeight) { - int x = (threadIdx.x + blockIdx.x * blockDim.x) * 2; - int y = (threadIdx.y + blockIdx.y * blockDim.y); - if (x + 1 >= nWidth || y >= nHeight) { - return; - } - - uint8_t *pSrc = pYuv + x * sizeof(YuvUnitx2) / 2 + y * nYuvPitch; - uint8_t *pDst = pRgb + x * sizeof(Rgb) + y * nRgbPitch; - - YuvUnitx2 l0 = *(YuvUnitx2 *)pSrc; - YuvUnitx2 ch1 = *(YuvUnitx2 *)(pSrc + (nHeight * nYuvPitch)); - YuvUnitx2 ch2 = *(YuvUnitx2 *)(pSrc + (2 * nHeight * nYuvPitch)); - - *(RgbIntx2 *)pDst = RgbIntx2{ - YuvToRgbForPixel<Rgb>(l0.x, ch1.x, ch2.x).d, - YuvToRgbForPixel<Rgb>(l0.y, ch1.y, ch2.y).d, - }; -} - -template<class YuvUnitx2, class Rgb, class RgbUnitx2> -__global__ static void YuvToRgbPlanarKernel(uint8_t *pYuv, int nYuvPitch, uint8_t *pRgbp, int nRgbpPitch, int nWidth, int nHeight) { - int x = (threadIdx.x + blockIdx.x * blockDim.x) * 2; - int y = (threadIdx.y + blockIdx.y * blockDim.y) * 2; - if (x + 1 >= nWidth || y + 1 >= nHeight) { - return; - } - - uint8_t *pSrc = pYuv + x * sizeof(YuvUnitx2) / 2 + y * nYuvPitch; - - YuvUnitx2 l0 = *(YuvUnitx2 *)pSrc; - YuvUnitx2 l1 = *(YuvUnitx2 *)(pSrc + nYuvPitch); - YuvUnitx2 ch = *(YuvUnitx2 *)(pSrc + (nHeight - y / 2) * nYuvPitch); - - Rgb rgb0 = YuvToRgbForPixel<Rgb>(l0.x, ch.x, ch.y), - rgb1 = YuvToRgbForPixel<Rgb>(l0.y, ch.x, ch.y), - rgb2 = YuvToRgbForPixel<Rgb>(l1.x, ch.x, ch.y), - rgb3 = YuvToRgbForPixel<Rgb>(l1.y, ch.x, ch.y); - - uint8_t *pDst = pRgbp + x * sizeof(RgbUnitx2) / 2 + y * nRgbpPitch; - *(RgbUnitx2 *)pDst = RgbUnitx2 {rgb0.v.x, rgb1.v.x}; - *(RgbUnitx2 *)(pDst + nRgbpPitch) = RgbUnitx2 {rgb2.v.x, rgb3.v.x}; - pDst += nRgbpPitch * nHeight; - *(RgbUnitx2 *)pDst = RgbUnitx2 {rgb0.v.y, rgb1.v.y}; - *(RgbUnitx2 *)(pDst + nRgbpPitch) = RgbUnitx2 {rgb2.v.y, rgb3.v.y}; - pDst += nRgbpPitch * nHeight; - *(RgbUnitx2 *)pDst = RgbUnitx2 {rgb0.v.z, rgb1.v.z}; - *(RgbUnitx2 *)(pDst + nRgbpPitch) = RgbUnitx2 {rgb2.v.z, rgb3.v.z}; -} - -template<class YuvUnitx2, class Rgb, class RgbUnitx2> -__global__ static void Yuv444ToRgbPlanarKernel(uint8_t *pYuv, int nYuvPitch, uint8_t *pRgbp, int nRgbpPitch, int nWidth, int nHeight) { - int x = (threadIdx.x + blockIdx.x * blockDim.x) * 2; - int y = (threadIdx.y + blockIdx.y * blockDim.y); - if (x + 1 >= nWidth || y >= nHeight) { - return; - } - - uint8_t *pSrc = pYuv + x * sizeof(YuvUnitx2) / 2 + y * nYuvPitch; - - YuvUnitx2 l0 = *(YuvUnitx2 *)pSrc; - YuvUnitx2 ch1 = *(YuvUnitx2 *)(pSrc + (nHeight * nYuvPitch)); - YuvUnitx2 ch2 = *(YuvUnitx2 *)(pSrc + (2 * nHeight * nYuvPitch)); - - Rgb rgb0 = YuvToRgbForPixel<Rgb>(l0.x, ch1.x, ch2.x), - rgb1 = YuvToRgbForPixel<Rgb>(l0.y, ch1.y, ch2.y); - - - uint8_t *pDst = pRgbp + x * sizeof(RgbUnitx2) / 2 + y * nRgbpPitch; - *(RgbUnitx2 *)pDst = RgbUnitx2{ rgb0.v.x, rgb1.v.x }; - - pDst += nRgbpPitch * nHeight; - *(RgbUnitx2 *)pDst = RgbUnitx2{ rgb0.v.y, rgb1.v.y }; - - pDst += nRgbpPitch * nHeight; - *(RgbUnitx2 *)pDst = RgbUnitx2{ rgb0.v.z, rgb1.v.z }; -} - -template <class COLOR32> -void Nv12ToColor32(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix, cudaStream_t s) { - SetMatYuv2Rgb(iMatrix); - YuvToRgbKernel<uchar2, COLOR32, uint2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2 / 2), dim3(32, 2), 0, s>>> - (dpNv12, nNv12Pitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR32> -void Nv12ToYuv32(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix, cudaStream_t s) { - //SetMatYuv2Rgb(iMatrix); - YuvToYuvKernel<uchar2, COLOR32, uint2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2 / 2), dim3(32, 2), 0, s>>> - (dpNv12, nNv12Pitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR> -void Yuv420_10bitToYuv(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix, cudaStream_t s) { - //SetMatYuv2Rgb(iMatrix); - YuvToYuvKernel<ushort2, COLOR, ulonglong2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2 / 2), dim3(32, 2), 0, s>>> - (dpNv12, nNv12Pitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR64> -void Nv12ToColor64(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - YuvToRgbKernel<uchar2, COLOR64, ulonglong2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2 / 2), dim3(32, 2)>>> - (dpNv12, nNv12Pitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR32> -void YUV444ToColor32(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - Yuv444ToRgbKernel<uchar2, COLOR32, uint2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2), dim3(32, 2) >>> - (dpYUV444, nPitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR64> -void YUV444ToColor64(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - Yuv444ToRgbKernel<uchar2, COLOR64, ulonglong2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2), dim3(32, 2) >>> - (dpYUV444, nPitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR32> -void P016ToColor32(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - YuvToRgbKernel<ushort2, COLOR32, uint2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2 / 2), dim3(32, 2)>>> - (dpP016, nP016Pitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR64> -void P016ToColor64(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - YuvToRgbKernel<ushort2, COLOR64, ulonglong2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2 / 2), dim3(32, 2)>>> - (dpP016, nP016Pitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR32> -void YUV444P16ToColor32(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - Yuv444ToRgbKernel<ushort2, COLOR32, uint2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2), dim3(32, 2) >>> - (dpYUV444, nPitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR64> -void YUV444P16ToColor64(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - Yuv444ToRgbKernel<ushort2, COLOR64, ulonglong2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2), dim3(32, 2) >>> - (dpYUV444, nPitch, dpBgra, nBgraPitch, nWidth, nHeight); -} - -template <class COLOR32> -void Nv12ToColorPlanar(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - YuvToRgbPlanarKernel<uchar2, COLOR32, uchar2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2 / 2), dim3(32, 2)>>> - (dpNv12, nNv12Pitch, dpBgrp, nBgrpPitch, nWidth, nHeight); -} - -template <class COLOR32> -void P016ToColorPlanar(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - YuvToRgbPlanarKernel<ushort2, COLOR32, uchar2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2 / 2), dim3(32, 2)>>> - (dpP016, nP016Pitch, dpBgrp, nBgrpPitch, nWidth, nHeight); -} - -template <class COLOR32> -void YUV444ToColorPlanar(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - Yuv444ToRgbPlanarKernel<uchar2, COLOR32, uchar2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2), dim3(32, 2) >>> - (dpYUV444, nPitch, dpBgrp, nBgrpPitch, nWidth, nHeight); -} - -template <class COLOR32> -void YUV444P16ToColorPlanar(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix) { - SetMatYuv2Rgb(iMatrix); - Yuv444ToRgbPlanarKernel<ushort2, COLOR32, uchar2> - << <dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2), dim3(32, 2) >> > - (dpYUV444, nPitch, dpBgrp, nBgrpPitch, nWidth, nHeight); -} - -// Explicit Instantiation -template void Nv12ToYuv32<YUVA32>(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix, cudaStream_t); -template void Yuv420_10bitToYuv<YUVA64>(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix, cudaStream_t); -template void Nv12ToColor32<BGRA32>(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix, cudaStream_t); -template void Nv12ToColor32<RGBA32>(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix, cudaStream_t); -template void Nv12ToColor64<BGRA64>(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void Nv12ToColor64<RGBA64>(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444ToColor32<BGRA32>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444ToColor32<RGBA32>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444ToColor64<BGRA64>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444ToColor64<RGBA64>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void P016ToColor32<BGRA32>(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void P016ToColor32<RGBA32>(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void P016ToColor64<BGRA64>(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void P016ToColor64<RGBA64>(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444P16ToColor32<BGRA32>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444P16ToColor32<RGBA32>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444P16ToColor64<BGRA64>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444P16ToColor64<RGBA64>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix); -template void Nv12ToColorPlanar<BGRA32>(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix); -template void Nv12ToColorPlanar<RGBA32>(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix); -template void P016ToColorPlanar<BGRA32>(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix); -template void P016ToColorPlanar<RGBA32>(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444ToColorPlanar<BGRA32>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444ToColorPlanar<RGBA32>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444P16ToColorPlanar<BGRA32>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix); -template void YUV444P16ToColorPlanar<RGBA32>(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix); - -template<class YuvUnit, class RgbUnit> -__device__ inline YuvUnit RgbToY(RgbUnit r, RgbUnit g, RgbUnit b) { - const YuvUnit low = 1 << (sizeof(YuvUnit) * 8 - 4); - return matRgb2Yuv[0][0] * r + matRgb2Yuv[0][1] * g + matRgb2Yuv[0][2] * b + low; -} - -template<class YuvUnit, class RgbUnit> -__device__ inline YuvUnit RgbToU(RgbUnit r, RgbUnit g, RgbUnit b) { - const YuvUnit mid = 1 << (sizeof(YuvUnit) * 8 - 1); - return matRgb2Yuv[1][0] * r + matRgb2Yuv[1][1] * g + matRgb2Yuv[1][2] * b + mid; -} - -template<class YuvUnit, class RgbUnit> -__device__ inline YuvUnit RgbToV(RgbUnit r, RgbUnit g, RgbUnit b) { - const YuvUnit mid = 1 << (sizeof(YuvUnit) * 8 - 1); - return matRgb2Yuv[2][0] * r + matRgb2Yuv[2][1] * g + matRgb2Yuv[2][2] * b + mid; -} - -template<class YuvUnitx2, class Rgb, class RgbIntx2> -__global__ static void RgbToYuvKernel(uint8_t *pRgb, int nRgbPitch, uint8_t *pYuv, int nYuvPitch, int nWidth, int nHeight) { - int x = (threadIdx.x + blockIdx.x * blockDim.x) * 2; - int y = (threadIdx.y + blockIdx.y * blockDim.y) * 2; - if (x + 1 >= nWidth || y + 1 >= nHeight) { - return; - } - - uint8_t *pSrc = pRgb + x * sizeof(Rgb) + y * nRgbPitch; - RgbIntx2 int2a = *(RgbIntx2 *)pSrc; - RgbIntx2 int2b = *(RgbIntx2 *)(pSrc + nRgbPitch); - - Rgb rgb[4] = {int2a.x, int2a.y, int2b.x, int2b.y}; - decltype(Rgb::c.r) - r = (rgb[0].c.r + rgb[1].c.r + rgb[2].c.r + rgb[3].c.r) / 4, - g = (rgb[0].c.g + rgb[1].c.g + rgb[2].c.g + rgb[3].c.g) / 4, - b = (rgb[0].c.b + rgb[1].c.b + rgb[2].c.b + rgb[3].c.b) / 4; - - uint8_t *pDst = pYuv + x * sizeof(YuvUnitx2) / 2 + y * nYuvPitch; - *(YuvUnitx2 *)pDst = YuvUnitx2 { - RgbToY<decltype(YuvUnitx2::x)>(rgb[0].c.r, rgb[0].c.g, rgb[0].c.b), - RgbToY<decltype(YuvUnitx2::x)>(rgb[1].c.r, rgb[1].c.g, rgb[1].c.b), - }; - *(YuvUnitx2 *)(pDst + nYuvPitch) = YuvUnitx2 { - RgbToY<decltype(YuvUnitx2::x)>(rgb[2].c.r, rgb[2].c.g, rgb[2].c.b), - RgbToY<decltype(YuvUnitx2::x)>(rgb[3].c.r, rgb[3].c.g, rgb[3].c.b), - }; - *(YuvUnitx2 *)(pDst + (nHeight - y / 2) * nYuvPitch) = YuvUnitx2 { - RgbToU<decltype(YuvUnitx2::x)>(r, g, b), - RgbToV<decltype(YuvUnitx2::x)>(r, g, b), - }; -} - -void Bgra64ToP016(uint8_t *dpBgra, int nBgraPitch, uint8_t *dpP016, int nP016Pitch, int nWidth, int nHeight, int iMatrix) { - SetMatRgb2Yuv(iMatrix); - RgbToYuvKernel<ushort2, BGRA64, ulonglong2> - <<<dim3((nWidth + 63) / 32 / 2, (nHeight + 3) / 2 / 2), dim3(32, 2)>>> - (dpBgra, nBgraPitch, dpP016, nP016Pitch, nWidth, nHeight); -} diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/ColorSpace.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/ColorSpace.h deleted file mode 100644 index c46e275772af63c1143c28f9a6e4e8d5c9041515..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/ColorSpace.h +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once -#include <stdint.h> -#include <cuda_runtime.h> - -typedef enum ColorSpaceStandard { - ColorSpaceStandard_BT709 = 0, - ColorSpaceStandard_BT601 = 2, - ColorSpaceStandard_BT2020 = 4 -} ColorSpaceStandard; - -union BGRA32 { - uint32_t d; - uchar4 v; - struct { - uint8_t b, g, r, a; - } c; -}; - -union RGBA32 { - uint32_t d; - uchar4 v; - struct { - uint8_t r, g, b, a; - } c; -}; - -union YUVA32 { - uint32_t d; - uchar4 v; - struct { - uint8_t v, u, y, a; - } c; -}; - -union YUVA64 { - uint64_t d; - ushort4 v; - struct { - uint16_t v, u, y, a; - } c; -}; - -union BGRA64 { - uint64_t d; - ushort4 v; - struct { - uint16_t b, g, r, a; - } c; -}; - -union RGBA64 { - uint64_t d; - ushort4 v; - struct { - uint16_t r, g, b, a; - } c; -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/FFmpegDemuxer.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/FFmpegDemuxer.h deleted file mode 100644 index abe3699a59de6e06d6c36408f4fdb0cb1f47a061..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/FFmpegDemuxer.h +++ /dev/null @@ -1,273 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ -#pragma once - -extern "C" { -#include <libavformat/avformat.h> -#include <libavformat/avio.h> -#include <libavcodec/avcodec.h> -} -#include "NvCodecUtils.h" - -class FFmpegDemuxer { -private: - AVFormatContext *fmtc = NULL; - AVIOContext *avioc = NULL; - AVPacket pkt, pktFiltered; - AVBSFContext *bsfc = NULL; - - int iVideoStream; - bool bMp4H264, bMp4HEVC; - AVCodecID eVideoCodec; - AVPixelFormat eChromaFormat; - int nWidth, nHeight, nBitDepth, nBPP, nChromaHeight; - -public: - class DataProvider { - public: - virtual ~DataProvider() {} - virtual int GetData(uint8_t *pBuf, int nBuf) = 0; - }; - -private: - FFmpegDemuxer(AVFormatContext *fmtc) : fmtc(fmtc) { - if (!fmtc) { - LOG(ERROR) << "No AVFormatContext provided."; - return; - } - - LOG(INFO) << "Media format: " << fmtc->iformat->long_name << " (" << fmtc->iformat->name << ")"; - - ck(avformat_find_stream_info(fmtc, NULL)); - iVideoStream = av_find_best_stream(fmtc, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); - if (iVideoStream < 0) { - LOG(ERROR) << "FFmpeg error: " << __FILE__ << " " << __LINE__ << " " << "Could not find stream in input file"; - return; - } - - //fmtc->streams[iVideoStream]->need_parsing = AVSTREAM_PARSE_NONE; - eVideoCodec = fmtc->streams[iVideoStream]->codecpar->codec_id; - nWidth = fmtc->streams[iVideoStream]->codecpar->width; - nHeight = fmtc->streams[iVideoStream]->codecpar->height; - eChromaFormat = (AVPixelFormat)fmtc->streams[iVideoStream]->codecpar->format; - - switch (eChromaFormat) - { - case AV_PIX_FMT_YUV420P10LE: - nBitDepth = 10; - nChromaHeight = (nHeight + 1) >> 1; - nBPP = 2; - break; - case AV_PIX_FMT_YUV420P12LE: - nBitDepth = 12; - nChromaHeight = (nHeight + 1) >> 1; - nBPP = 2; - break; - case AV_PIX_FMT_YUV444P10LE: - nBitDepth = 10; - nChromaHeight = nHeight << 1; - nBPP = 2; - break; - case AV_PIX_FMT_YUV444P12LE: - nBitDepth = 12; - nChromaHeight = nHeight << 1; - nBPP = 2; - break; - case AV_PIX_FMT_YUV444P: - nBitDepth = 8; - nChromaHeight = nHeight << 1; - nBPP = 1; - break; - case AV_PIX_FMT_YUV420P: - case AV_PIX_FMT_YUVJ420P: - case AV_PIX_FMT_YUVJ422P: // jpeg decoder output is subsampled to NV12 for 422/444 so treat it as 420 - case AV_PIX_FMT_YUVJ444P: // jpeg decoder output is subsampled to NV12 for 422/444 so treat it as 420 - nBitDepth = 8; - nChromaHeight = (nHeight + 1) >> 1; - nBPP = 1; - break; - default: - LOG(WARNING) << "ChromaFormat not recognized. Assuming 420"; - nBitDepth = 8; - nChromaHeight = (nHeight + 1) >> 1; - nBPP = 1; - } - - bMp4H264 = eVideoCodec == AV_CODEC_ID_H264 && ( - !strcmp(fmtc->iformat->long_name, "QuickTime / MOV") - || !strcmp(fmtc->iformat->long_name, "FLV (Flash Video)") - || !strcmp(fmtc->iformat->long_name, "Matroska / WebM") - ); - bMp4HEVC = eVideoCodec == AV_CODEC_ID_HEVC && ( - !strcmp(fmtc->iformat->long_name, "QuickTime / MOV") - || !strcmp(fmtc->iformat->long_name, "FLV (Flash Video)") - || !strcmp(fmtc->iformat->long_name, "Matroska / WebM") - ); - av_init_packet(&pkt); - pkt.data = NULL; - pkt.size = 0; - av_init_packet(&pktFiltered); - pktFiltered.data = NULL; - pktFiltered.size = 0; - - if (bMp4H264) { - const AVBitStreamFilter *bsf = av_bsf_get_by_name("h264_mp4toannexb"); - if (!bsf) { - LOG(ERROR) << "FFmpeg error: " << __FILE__ << " " << __LINE__ << " " << "av_bsf_get_by_name() failed"; - return; - } - ck(av_bsf_alloc(bsf, &bsfc)); - avcodec_parameters_copy(bsfc->par_in, fmtc->streams[iVideoStream]->codecpar); - ck(av_bsf_init(bsfc)); - } - if (bMp4HEVC) { - const AVBitStreamFilter *bsf = av_bsf_get_by_name("hevc_mp4toannexb"); - if (!bsf) { - LOG(ERROR) << "FFmpeg error: " << __FILE__ << " " << __LINE__ << " " << "av_bsf_get_by_name() failed"; - return; - } - ck(av_bsf_alloc(bsf, &bsfc)); - avcodec_parameters_copy(bsfc->par_in, fmtc->streams[iVideoStream]->codecpar); - ck(av_bsf_init(bsfc)); - } - } - - AVFormatContext *CreateFormatContext(DataProvider *pDataProvider) { - - AVFormatContext *ctx = NULL; - if (!(ctx = avformat_alloc_context())) { - LOG(ERROR) << "FFmpeg error: " << __FILE__ << " " << __LINE__; - return NULL; - } - - uint8_t *avioc_buffer = NULL; - int avioc_buffer_size = 8 * 1024 * 1024; - avioc_buffer = (uint8_t *)av_malloc(avioc_buffer_size); - if (!avioc_buffer) { - LOG(ERROR) << "FFmpeg error: " << __FILE__ << " " << __LINE__; - return NULL; - } - avioc = avio_alloc_context(avioc_buffer, avioc_buffer_size, - 0, pDataProvider, &ReadPacket, NULL, NULL); - if (!avioc) { - LOG(ERROR) << "FFmpeg error: " << __FILE__ << " " << __LINE__; - return NULL; - } - ctx->pb = avioc; - - ck(avformat_open_input(&ctx, NULL, NULL, NULL)); - return ctx; - } - - AVFormatContext *CreateFormatContext(const char *szFilePath) { - avformat_network_init(); - - AVFormatContext *ctx = NULL; - ck(avformat_open_input(&ctx, szFilePath, NULL, NULL)); - return ctx; - } - -public: - FFmpegDemuxer(const char *szFilePath) : FFmpegDemuxer(CreateFormatContext(szFilePath)) {} - FFmpegDemuxer(DataProvider *pDataProvider) : FFmpegDemuxer(CreateFormatContext(pDataProvider)) {avioc = fmtc->pb;} - ~FFmpegDemuxer() { - if (pkt.data) { - av_packet_unref(&pkt); - } - if (pktFiltered.data) { - av_packet_unref(&pktFiltered); - } - - if (bsfc) { - av_bsf_free(&bsfc); - } - - avformat_close_input(&fmtc); - - if (avioc) { - av_freep(&avioc->buffer); - av_freep(&avioc); - } - } - AVCodecID GetVideoCodec() { - return eVideoCodec; - } - AVPixelFormat GetChromaFormat() { - return eChromaFormat; - } - int GetWidth() { - return nWidth; - } - int GetHeight() { - return nHeight; - } - int GetBitDepth() { - return nBitDepth; - } - int GetFrameSize() { - return nWidth * (nHeight + nChromaHeight) * nBPP; - } - bool Demux(uint8_t **ppVideo, int *pnVideoBytes) { - if (!fmtc) { - return false; - } - - *pnVideoBytes = 0; - - if (pkt.data) { - av_packet_unref(&pkt); - } - - int e = 0; - while ((e = av_read_frame(fmtc, &pkt)) >= 0 && pkt.stream_index != iVideoStream) { - av_packet_unref(&pkt); - } - if (e < 0) { - return false; - } - - if (bMp4H264 || bMp4HEVC) { - if (pktFiltered.data) { - av_packet_unref(&pktFiltered); - } - ck(av_bsf_send_packet(bsfc, &pkt)); - ck(av_bsf_receive_packet(bsfc, &pktFiltered)); - *ppVideo = pktFiltered.data; - *pnVideoBytes = pktFiltered.size; - } else { - *ppVideo = pkt.data; - *pnVideoBytes = pkt.size; - } - - return true; - } - - static int ReadPacket(void *opaque, uint8_t *pBuf, int nBuf) { - return ((DataProvider *)opaque)->GetData(pBuf, nBuf); - } -}; - -inline cudaVideoCodec FFmpeg2NvCodecId(AVCodecID id) { - switch (id) { - case AV_CODEC_ID_MPEG1VIDEO : return cudaVideoCodec_MPEG1; - case AV_CODEC_ID_MPEG2VIDEO : return cudaVideoCodec_MPEG2; - case AV_CODEC_ID_MPEG4 : return cudaVideoCodec_MPEG4; - case AV_CODEC_ID_VC1 : return cudaVideoCodec_VC1; - case AV_CODEC_ID_H264 : return cudaVideoCodec_H264; - case AV_CODEC_ID_HEVC : return cudaVideoCodec_HEVC; - case AV_CODEC_ID_VP8 : return cudaVideoCodec_VP8; - case AV_CODEC_ID_VP9 : return cudaVideoCodec_VP9; - case AV_CODEC_ID_MJPEG : return cudaVideoCodec_JPEG; - default : return cudaVideoCodec_NumCodecs; - } -} - - diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/FFmpegStreamer.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/FFmpegStreamer.h deleted file mode 100644 index d407cbe2483ca1cc8b9e8dd3f872a0d87a7298ed..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/FFmpegStreamer.h +++ /dev/null @@ -1,109 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ -#pragma once - -#include <thread> -#include <mutex> -extern "C" { -#include <libavformat/avformat.h> -#include <libavutil/opt.h> -#include <libswresample/swresample.h> -}; -#include "Logger.h" - -extern simplelogger::Logger *logger; - -class FFmpegStreamer { -private: - AVFormatContext *oc = NULL; - AVStream *vs = NULL; - int nFps = 0; - -public: - FFmpegStreamer(AVCodecID eCodecId, int nWidth, int nHeight, int nFps, const char *szInFilePath) : nFps(nFps) { - avformat_network_init(); - oc = avformat_alloc_context(); - if (!oc) { - LOG(ERROR) << "FFMPEG: avformat_alloc_context error"; - return; - } - - // Set format on oc - AVOutputFormat *fmt = av_guess_format("mpegts", NULL, NULL); - if (!fmt) { - LOG(ERROR) << "Invalid format"; - return; - } - fmt->video_codec = eCodecId; - - oc->oformat = fmt; - oc->url = av_strdup(szInFilePath); - LOG(INFO) << "Streaming destination: " << oc->url; - - // Add video stream to oc - vs = avformat_new_stream(oc, NULL); - if (!vs) { - LOG(ERROR) << "FFMPEG: Could not alloc video stream"; - return; - } - vs->id = 0; - - // Set video parameters - AVCodecParameters *vpar = vs->codecpar; - vpar->codec_id = fmt->video_codec; - vpar->codec_type = AVMEDIA_TYPE_VIDEO; - vpar->width = nWidth; - vpar->height = nHeight; - - // Everything is ready. Now open the output stream. - if (avio_open(&oc->pb, oc->url, AVIO_FLAG_WRITE) < 0) { - LOG(ERROR) << "FFMPEG: Could not open " << oc->url; - return ; - } - - // Write the container header - if (avformat_write_header(oc, NULL)) { - LOG(ERROR) << "FFMPEG: avformat_write_header error!"; - return; - } - } - ~FFmpegStreamer() { - if (oc) { - av_write_trailer(oc); - avio_close(oc->pb); - avformat_free_context(oc); - } - } - - bool Stream(uint8_t *pData, int nBytes, int nPts) { - AVPacket pkt = {0}; - av_init_packet(&pkt); - pkt.pts = av_rescale_q(nPts++, AVRational {1, nFps}, vs->time_base); - // No B-frames - pkt.dts = pkt.pts; - pkt.stream_index = vs->index; - pkt.data = pData; - pkt.size = nBytes; - - if(!memcmp(pData, "\x00\x00\x00\x01\x67", 5)) { - pkt.flags |= AV_PKT_FLAG_KEY; - } - - // Write the compressed frame into the output - int ret = av_write_frame(oc, &pkt); - av_write_frame(oc, NULL); - if (ret < 0) { - LOG(ERROR) << "FFMPEG: Error while writing video frame"; - } - - return true; - } -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/Logger.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/Logger.h deleted file mode 100644 index acffa978be260dc345d4581309af413c0c54367d..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/Logger.h +++ /dev/null @@ -1,240 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include <iostream> -#include <fstream> -#include <string> -#include <sstream> -#include <mutex> -#include <time.h> - -#ifdef _WIN32 -#include <winsock.h> -#include <windows.h> - -#pragma comment(lib, "ws2_32.lib") -#undef ERROR -#else -#include <unistd.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#define SOCKET int -#define INVALID_SOCKET -1 -#endif - -enum LogLevel { - TRACE, - INFO, - WARNING, - ERROR, - FATAL -}; - -namespace simplelogger{ -class Logger { -public: - Logger(LogLevel level, bool bPrintTimeStamp) : level(level), bPrintTimeStamp(bPrintTimeStamp) {} - virtual ~Logger() {} - virtual std::ostream& GetStream() = 0; - virtual void FlushStream() {} - bool ShouldLogFor(LogLevel l) { - return l >= level; - } - char* GetLead(LogLevel l, const char *szFile, int nLine, const char *szFunc) { - if (l < TRACE || l > FATAL) { - sprintf(szLead, "[?????] "); - return szLead; - } - const char *szLevels[] = {"TRACE", "INFO", "WARN", "ERROR", "FATAL"}; - if (bPrintTimeStamp) { - time_t t = time(NULL); - struct tm *ptm = localtime(&t); - sprintf(szLead, "[%-5s][%02d:%02d:%02d] ", - szLevels[l], ptm->tm_hour, ptm->tm_min, ptm->tm_sec); - } else { - sprintf(szLead, "[%-5s] ", szLevels[l]); - } - return szLead; - } - void EnterCriticalSection() { - mtx.lock(); - } - void LeaveCriticalSection() { - mtx.unlock(); - } -private: - LogLevel level; - char szLead[80]; - bool bPrintTimeStamp; - std::mutex mtx; -}; - -class LoggerFactory { -public: - static Logger* CreateFileLogger(std::string strFilePath, - LogLevel level = INFO, bool bPrintTimeStamp = true) { - return new FileLogger(strFilePath, level, bPrintTimeStamp); - } - static Logger* CreateConsoleLogger(LogLevel level = INFO, - bool bPrintTimeStamp = true) { - return new ConsoleLogger(level, bPrintTimeStamp); - } - static Logger* CreateUdpLogger(char *szHost, unsigned uPort, LogLevel level = INFO, - bool bPrintTimeStamp = true) { - return new UdpLogger(szHost, uPort, level, bPrintTimeStamp); - } -private: - LoggerFactory() {} - - class FileLogger : public Logger { - public: - FileLogger(std::string strFilePath, LogLevel level, bool bPrintTimeStamp) - : Logger(level, bPrintTimeStamp) { - pFileOut = new std::ofstream(); - pFileOut->open(strFilePath.c_str()); - } - ~FileLogger() { - pFileOut->close(); - } - std::ostream& GetStream() { - return *pFileOut; - } - private: - std::ofstream *pFileOut; - }; - - class ConsoleLogger : public Logger { - public: - ConsoleLogger(LogLevel level, bool bPrintTimeStamp) - : Logger(level, bPrintTimeStamp) {} - std::ostream& GetStream() { - return std::cout; - } - }; - - class UdpLogger : public Logger { - private: - class UdpOstream : public std::ostream { - public: - UdpOstream(char *szHost, unsigned short uPort) : std::ostream(&sb), socket(INVALID_SOCKET){ -#ifdef _WIN32 - WSADATA w; - if (WSAStartup(0x0101, &w) != 0) { - fprintf(stderr, "WSAStartup() failed.\n"); - return; - } -#endif - socket = ::socket(AF_INET, SOCK_DGRAM, 0); - if (socket == INVALID_SOCKET) { -#ifdef _WIN32 - WSACleanup(); -#endif - fprintf(stderr, "socket() failed.\n"); - return; - } -#ifdef _WIN32 - unsigned int b1, b2, b3, b4; - sscanf(szHost, "%u.%u.%u.%u", &b1, &b2, &b3, &b4); - struct in_addr addr = {(unsigned char)b1, (unsigned char)b2, (unsigned char)b3, (unsigned char)b4}; -#else - struct in_addr addr = {inet_addr(szHost)}; -#endif - struct sockaddr_in s = {AF_INET, htons(uPort), addr}; - server = s; - } - ~UdpOstream() throw() { - if (socket == INVALID_SOCKET) { - return; - } -#ifdef _WIN32 - closesocket(socket); - WSACleanup(); -#else - close(socket); -#endif - } - void Flush() { - if (sendto(socket, sb.str().c_str(), (int)sb.str().length() + 1, - 0, (struct sockaddr *)&server, (int)sizeof(sockaddr_in)) == -1) { - fprintf(stderr, "sendto() failed.\n"); - } - sb.str(""); - } - - private: - std::stringbuf sb; - SOCKET socket; - struct sockaddr_in server; - }; - public: - UdpLogger(char *szHost, unsigned uPort, LogLevel level, bool bPrintTimeStamp) - : Logger(level, bPrintTimeStamp), udpOut(szHost, (unsigned short)uPort) {} - UdpOstream& GetStream() { - return udpOut; - } - virtual void FlushStream() { - udpOut.Flush(); - } - private: - UdpOstream udpOut; - }; -}; - -class LogTransaction { -public: - LogTransaction(Logger *pLogger, LogLevel level, const char *szFile, const int nLine, const char *szFunc) : pLogger(pLogger), level(level) { - if (!pLogger) { - std::cout << "[-----] "; - return; - } - if (!pLogger->ShouldLogFor(level)) { - return; - } - pLogger->EnterCriticalSection(); - pLogger->GetStream() << pLogger->GetLead(level, szFile, nLine, szFunc); - } - ~LogTransaction() { - if (!pLogger) { - std::cout << std::endl; - return; - } - if (!pLogger->ShouldLogFor(level)) { - return; - } - pLogger->GetStream() << std::endl; - pLogger->FlushStream(); - pLogger->LeaveCriticalSection(); - if (level == FATAL) { - exit(1); - } - } - std::ostream& GetStream() { - if (!pLogger) { - return std::cout; - } - if (!pLogger->ShouldLogFor(level)) { - return ossNull; - } - return pLogger->GetStream(); - } -private: - Logger *pLogger; - LogLevel level; - std::ostringstream ossNull; -}; - -} - -extern simplelogger::Logger *logger; -#define LOG(level) simplelogger::LogTransaction(logger, level, __FILE__, __LINE__, __FUNCTION__).GetStream() diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/NvCodecUtils.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/NvCodecUtils.h deleted file mode 100644 index 7c06d710d975699f6289eaa92debd59992477347..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/NvCodecUtils.h +++ /dev/null @@ -1,347 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once -#include <iomanip> -#include <chrono> -#include <sys/stat.h> -#include <assert.h> -#include <stdint.h> -#include <string.h> -#include "Logger.h" -#include <thread> - -extern simplelogger::Logger *logger; - -#include <cuda_runtime_api.h> - -#ifdef __cuda_cuda_h__ -inline bool check(CUresult e, int iLine, const char *szFile) { - if (e != CUDA_SUCCESS) { - const char *szErrName = NULL; - cuGetErrorName(e, &szErrName); - LOG(FATAL) << "CUDA driver API error " << szErrName << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -#ifdef __CUDA_RUNTIME_H__ -inline bool check(cudaError_t e, int iLine, const char *szFile) { - if (e != cudaSuccess) { - LOG(FATAL) << "CUDA runtime API error " << cudaGetErrorName(e) << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -#ifdef _NV_ENCODEAPI_H_ -inline bool check(NVENCSTATUS e, int iLine, const char *szFile) { - const char *aszErrName[] = { - "NV_ENC_SUCCESS", - "NV_ENC_ERR_NO_ENCODE_DEVICE", - "NV_ENC_ERR_UNSUPPORTED_DEVICE", - "NV_ENC_ERR_INVALID_ENCODERDEVICE", - "NV_ENC_ERR_INVALID_DEVICE", - "NV_ENC_ERR_DEVICE_NOT_EXIST", - "NV_ENC_ERR_INVALID_PTR", - "NV_ENC_ERR_INVALID_EVENT", - "NV_ENC_ERR_INVALID_PARAM", - "NV_ENC_ERR_INVALID_CALL", - "NV_ENC_ERR_OUT_OF_MEMORY", - "NV_ENC_ERR_ENCODER_NOT_INITIALIZED", - "NV_ENC_ERR_UNSUPPORTED_PARAM", - "NV_ENC_ERR_LOCK_BUSY", - "NV_ENC_ERR_NOT_ENOUGH_BUFFER", - "NV_ENC_ERR_INVALID_VERSION", - "NV_ENC_ERR_MAP_FAILED", - "NV_ENC_ERR_NEED_MORE_INPUT", - "NV_ENC_ERR_ENCODER_BUSY", - "NV_ENC_ERR_EVENT_NOT_REGISTERD", - "NV_ENC_ERR_GENERIC", - "NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY", - "NV_ENC_ERR_UNIMPLEMENTED", - "NV_ENC_ERR_RESOURCE_REGISTER_FAILED", - "NV_ENC_ERR_RESOURCE_NOT_REGISTERED", - "NV_ENC_ERR_RESOURCE_NOT_MAPPED", - }; - if (e != NV_ENC_SUCCESS) { - LOG(FATAL) << "NVENC error " << aszErrName[e] << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -#ifdef _WINERROR_ -inline bool check(HRESULT e, int iLine, const char *szFile) { - if (e != S_OK) { - LOG(FATAL) << "HRESULT error 0x" << (void *)e << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -#if defined(__gl_h_) || defined(__GL_H__) -inline bool check(GLenum e, int iLine, const char *szFile) { - if (e != 0) { - LOG(ERROR) << "GLenum error " << e << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -inline bool check(int e, int iLine, const char *szFile) { - if (e < 0) { - LOG(ERROR) << "General error " << e << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} - -#define ck(call) check(call, __LINE__, __FILE__) - -class NvThread -{ -public: - NvThread() = default; - NvThread(const NvThread&) = delete; - NvThread& operator=(const NvThread& other) = delete; - - NvThread(std::thread&& thread) : t(std::move(thread)) - { - - } - - NvThread(NvThread&& thread) : t(std::move(thread.t)) - { - - } - - NvThread& operator=(NvThread&& other) - { - t = std::move(other.t); - return *this; - } - - ~NvThread() - { - join(); - } - - void join() - { - if (t.joinable()) - { - t.join(); - } - } -private: - std::thread t; -}; - -#ifndef _WIN32 -#define _stricmp strcasecmp -#endif - -class BufferedFileReader { -public: - BufferedFileReader(const char *szFileName, bool bPartial = false) { - struct stat st; - - if (stat(szFileName, &st) != 0) { - return; - } - - nSize = st.st_size; - while (nSize) { - try { - pBuf = new uint8_t[nSize]; - if (nSize != st.st_size) { - LOG(WARNING) << "File is too large - only " << std::setprecision(4) << 100.0 * nSize / (uint32_t)st.st_size << "% is loaded"; - } - break; - } catch(std::bad_alloc) { - if (!bPartial) { - LOG(ERROR) << "Failed to allocate memory in BufferedReader"; - return; - } - nSize = (uint32_t)(nSize * 0.9); - } - } - - std::ifstream fpIn(szFileName, std::ifstream::in | std::ifstream::binary); - if (!fpIn) - { - LOG(ERROR) << "Unable to open input file: " << szFileName; - return; - } - - std::streamsize nRead = fpIn.read(reinterpret_cast<char*>(pBuf), nSize).gcount(); - fpIn.close(); - - assert(nRead == nSize); - } - ~BufferedFileReader() { - if (pBuf) { - delete[] pBuf; - } - } - bool GetBuffer(uint8_t **ppBuf, uint32_t *pnSize) { - if (!pBuf) { - return false; - } - - *ppBuf = pBuf; - *pnSize = nSize; - return true; - } - -private: - uint8_t *pBuf = NULL; - uint32_t nSize = 0; -}; - -template<typename T> -class YuvConverter { -public: - YuvConverter(int nWidth, int nHeight) : nWidth(nWidth), nHeight(nHeight) { - pQuad = new T[nWidth * nHeight / 4]; - } - ~YuvConverter() { - delete pQuad; - } - void PlanarToUVInterleaved(T *pFrame, int nPitch = 0) { - if (nPitch == 0) { - nPitch = nWidth; - } - T *puv = pFrame + nPitch * nHeight; - if (nPitch == nWidth) { - memcpy(pQuad, puv, nWidth * nHeight / 4 * sizeof(T)); - } else { - for (int i = 0; i < nHeight / 2; i++) { - memcpy(pQuad + nWidth / 2 * i, puv + nPitch / 2 * i, nWidth / 2 * sizeof(T)); - } - } - T *pv = puv + (nPitch / 2) * (nHeight / 2); - for (int y = 0; y < nHeight / 2; y++) { - for (int x = 0; x < nWidth / 2; x++) { - puv[y * nPitch + x * 2] = pQuad[y * nWidth / 2 + x]; - puv[y * nPitch + x * 2 + 1] = pv[y * nPitch / 2 + x]; - } - } - } - void UVInterleavedToPlanar(T *pFrame, int nPitch = 0) { - if (nPitch == 0) { - nPitch = nWidth; - } - T *puv = pFrame + nPitch * nHeight, - *pu = puv, - *pv = puv + nPitch * nHeight / 4; - for (int y = 0; y < nHeight / 2; y++) { - for (int x = 0; x < nWidth / 2; x++) { - pu[y * nPitch / 2 + x] = puv[y * nPitch + x * 2]; - pQuad[y * nWidth / 2 + x] = puv[y * nPitch + x * 2 + 1]; - } - } - if (nPitch == nWidth) { - memcpy(pv, pQuad, nWidth * nHeight / 4 * sizeof(T)); - } else { - for (int i = 0; i < nHeight / 2; i++) { - memcpy(pv + nPitch / 2 * i, pQuad + nWidth / 2 * i, nWidth / 2 * sizeof(T)); - } - } - } - -private: - T *pQuad; - int nWidth, nHeight; -}; - -class StopWatch { -public: - void Start() { - t0 = std::chrono::high_resolution_clock::now(); - } - double Stop() { - return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch() - t0.time_since_epoch()).count() / 1.0e9; - } - -private: - std::chrono::high_resolution_clock::time_point t0; -}; - -inline void CheckInputFile(const char *szInFilePath) { - std::ifstream fpIn(szInFilePath, std::ios::in | std::ios::binary); - if (fpIn.fail()) { - std::ostringstream err; - err << "Unable to open input file: " << szInFilePath << std::endl; - throw std::invalid_argument(err.str()); - } -} - -inline void ValidateResolution(int nWidth, int nHeight) { - - if (nWidth <= 0 || nHeight <= 0) { - std::ostringstream err; - err << "Please specify positive non zero resolution as -s WxH. Current resolution is " << nWidth << "x" << nHeight << std::endl; - throw std::invalid_argument(err.str()); - } -} - -template <class COLOR32> -void Nv12ToYuv32(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0, cudaStream_t s=0); -template <class COLOR> -void Yuv420_10bitToYuv(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0, cudaStream_t s=0); -template <class COLOR32> -void Nv12ToColor32(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0, cudaStream_t s=0); -template <class COLOR64> -void Nv12ToColor64(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0); - -template <class COLOR32> -void P016ToColor32(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 4); -template <class COLOR64> -void P016ToColor64(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 4); - -template <class COLOR32> -void YUV444ToColor32(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0); -template <class COLOR64> -void YUV444ToColor64(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0); - -template <class COLOR32> -void YUV444P16ToColor32(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 4); -template <class COLOR64> -void YUV444P16ToColor64(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 4); - -template <class COLOR32> -void Nv12ToColorPlanar(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix = 0); -template <class COLOR32> -void P016ToColorPlanar(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix = 4); - -template <class COLOR32> -void YUV444ToColorPlanar(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix = 0); -template <class COLOR32> -void YUV444P16ToColorPlanar(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix = 4); - -void Bgra64ToP016(uint8_t *dpBgra, int nBgraPitch, uint8_t *dpP016, int nP016Pitch, int nWidth, int nHeight, int iMatrix = 4); - -void ConvertUInt8ToUInt16(uint8_t *dpUInt8, uint16_t *dpUInt16, int nSrcPitch, int nDestPitch, int nWidth, int nHeight); -void ConvertUInt16ToUInt8(uint16_t *dpUInt16, uint8_t *dpUInt8, int nSrcPitch, int nDestPitch, int nWidth, int nHeight); - -void ResizeNv12(unsigned char *dpDstNv12, int nDstPitch, int nDstWidth, int nDstHeight, unsigned char *dpSrcNv12, int nSrcPitch, int nSrcWidth, int nSrcHeight, unsigned char *dpDstNv12UV = nullptr); -void ResizeP016(unsigned char *dpDstP016, int nDstPitch, int nDstWidth, int nDstHeight, unsigned char *dpSrcP016, int nSrcPitch, int nSrcWidth, int nSrcHeight, unsigned char *dpDstP016UV = nullptr); - -void ScaleYUV420(unsigned char *dpDstY, unsigned char* dpDstU, unsigned char* dpDstV, int nDstPitch, int nDstChromaPitch, int nDstWidth, int nDstHeight, - unsigned char *dpSrcY, unsigned char* dpSrcU, unsigned char* dpSrcV, int nSrcPitch, int nSrcChromaPitch, int nSrcWidth, int nSrcHeight, bool bSemiplanar); diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/NvEncoderCLIOptions.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/NvEncoderCLIOptions.h deleted file mode 100644 index 13866fc78d19eb12a720e33e2b0a5592b0ff5327..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/NvEncoderCLIOptions.h +++ /dev/null @@ -1,597 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once -#include <vector> -#include <string> -#include <algorithm> -#include <stdexcept> -#include <sstream> -#include <iterator> -#include <cstring> -#include <functional> -#include "../Utils/Logger.h" - -extern simplelogger::Logger *logger; - -#ifndef _WIN32 -inline bool operator==(const GUID &guid1, const GUID &guid2) { - return !memcmp(&guid1, &guid2, sizeof(GUID)); -} - -inline bool operator!=(const GUID &guid1, const GUID &guid2) { - return !(guid1 == guid2); -} -#endif - -class NvEncoderInitParam { -public: - NvEncoderInitParam(const char *szParam = "", - std::function<void(NV_ENC_INITIALIZE_PARAMS *pParams)> *pfuncInit = NULL, bool _bLowLatency = false) - : strParam(szParam), bLowLatency(_bLowLatency) - { - if (pfuncInit) { - funcInit = *pfuncInit; - } - - std::transform(strParam.begin(), strParam.end(), strParam.begin(), tolower); - std::istringstream ss(strParam); - tokens = std::vector<std::string> { - std::istream_iterator<std::string>(ss), - std::istream_iterator<std::string>() - }; - - for (unsigned i = 0; i < tokens.size(); i++) - { - if (tokens[i] == "-codec" && ++i != tokens.size()) - { - ParseString("-codec", tokens[i], vCodec, szCodecNames, &guidCodec); - continue; - } - if (bLowLatency) - { - if (tokens[i] == "-preset" && ++i != tokens.size()) { - ParseString("-preset", tokens[i], vLowLatencyPreset, szLowLatencyPresetNames, &guidPreset); - continue; - } - } - else - { - if (tokens[i] == "-preset" && ++i != tokens.size()) { - ParseString("-preset", tokens[i], vPreset, szPresetNames, &guidPreset); - continue; - } - } - } - - if (bLowLatency) guidPreset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID; - } - virtual ~NvEncoderInitParam() {} - virtual bool IsCodecH264() { - return GetEncodeGUID() == NV_ENC_CODEC_H264_GUID; - } - - virtual bool IsCodecHEVC() { - return GetEncodeGUID() == NV_ENC_CODEC_HEVC_GUID; - } - std::string GetHelpMessage(bool bMeOnly = false, bool bUnbuffered = false, bool bHide444 = false, bool bOutputInVidMem = false) - { - std::ostringstream oss; - - if (bOutputInVidMem && bMeOnly) - { - oss << "-codec Codec: " << "h264" << std::endl; - } - else - { - oss << "-codec Codec: " << szCodecNames << std::endl; - } - - oss << "-preset Preset: " << (bLowLatency ? szLowLatencyPresetNames : szPresetNames) << std::endl - << "-profile H264: " << szH264ProfileNames; - - if (bOutputInVidMem && bMeOnly) - { - oss << std::endl; - } - else - { - oss << "; HEVC: " << szHevcProfileNames << std::endl; - } - - if (!bHide444 && !bLowLatency) - { - oss << "-444 (Only for RGB input) YUV444 encode" << std::endl; - } - if (bMeOnly) return oss.str(); - oss << "-rc Rate control mode: " << szRcModeNames << std::endl - << "-fps Frame rate" << std::endl - << "-gop Length of GOP (Group of Pictures)" << std::endl; - if (!bUnbuffered && !bLowLatency) - { - oss << "-bf Number of consecutive B-frames" << std::endl; - } - oss << "-bitrate Average bit rate, can be in unit of 1, K, M" << std::endl - << "-maxbitrate Max bit rate, can be in unit of 1, K, M" << std::endl - << "-vbvbufsize VBV buffer size in bits, can be in unit of 1, K, M" << std::endl - << "-vbvinit VBV initial delay in bits, can be in unit of 1, K, M" << std::endl; - if (!bLowLatency) - { - oss << "-aq Enable spatial AQ and set its stength (range 1-15, 0-auto)" << std::endl - << "-temporalaq (No value) Enable temporal AQ" << std::endl; - } - if (!bUnbuffered && !bLowLatency) - { - oss << "-lookahead Maximum depth of lookahead (range 0-32)" << std::endl; - } - oss << "-cq Target constant quality level for VBR mode (range 1-51, 0-auto)" << std::endl - << "-qmin Min QP value" << std::endl - << "-qmax Max QP value" << std::endl - << "-initqp Initial QP value" << std::endl; - if (!bLowLatency) - { - oss << "-constqp QP value for constqp rate control mode" << std::endl - << "Note: QP value can be in the form of qp_of_P_B_I or qp_P,qp_B,qp_I (no space)" << std::endl; - } - if (bUnbuffered && !bLowLatency) - { - oss << "Note: Options -bf and -lookahead are unavailable for this app" << std::endl; - } - return oss.str(); - } - - std::string MainParamToString(const NV_ENC_INITIALIZE_PARAMS *pParams) { - std::ostringstream os; - os - << "Encoding Parameters:" - << std::endl << "\tcodec : " << ConvertValueToString(vCodec, szCodecNames, pParams->encodeGUID) - << std::endl << "\tpreset : " << ConvertValueToString(vPreset, szPresetNames, pParams->presetGUID) - << std::endl << "\tprofile : " << ConvertValueToString(vProfile, szProfileNames, pParams->encodeConfig->profileGUID) - << std::endl << "\tchroma : " << ConvertValueToString(vChroma, szChromaNames, (pParams->encodeGUID == NV_ENC_CODEC_H264_GUID) ? pParams->encodeConfig->encodeCodecConfig.h264Config.chromaFormatIDC : pParams->encodeConfig->encodeCodecConfig.hevcConfig.chromaFormatIDC) - << std::endl << "\tbitdepth : " << ((pParams->encodeGUID == NV_ENC_CODEC_H264_GUID) ? 0 : pParams->encodeConfig->encodeCodecConfig.hevcConfig.pixelBitDepthMinus8) + 8 - << std::endl << "\trc : " << ConvertValueToString(vRcMode, szRcModeNames, pParams->encodeConfig->rcParams.rateControlMode) - ; - if (pParams->encodeConfig->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CONSTQP) { - os << " (P,B,I=" << pParams->encodeConfig->rcParams.constQP.qpInterP << "," << pParams->encodeConfig->rcParams.constQP.qpInterB << "," << pParams->encodeConfig->rcParams.constQP.qpIntra << ")"; - } - os - << std::endl << "\tfps : " << pParams->frameRateNum << "/" << pParams->frameRateDen - << std::endl << "\tgop : " << (pParams->encodeConfig->gopLength == NVENC_INFINITE_GOPLENGTH ? "INF" : std::to_string(pParams->encodeConfig->gopLength)) - << std::endl << "\tbf : " << pParams->encodeConfig->frameIntervalP - 1 - << std::endl << "\tsize : " << pParams->encodeWidth << "x" << pParams->encodeHeight - << std::endl << "\tbitrate : " << pParams->encodeConfig->rcParams.averageBitRate - << std::endl << "\tmaxbitrate : " << pParams->encodeConfig->rcParams.maxBitRate - << std::endl << "\tvbvbufsize : " << pParams->encodeConfig->rcParams.vbvBufferSize - << std::endl << "\tvbvinit : " << pParams->encodeConfig->rcParams.vbvInitialDelay - << std::endl << "\taq : " << (pParams->encodeConfig->rcParams.enableAQ ? (pParams->encodeConfig->rcParams.aqStrength ? std::to_string(pParams->encodeConfig->rcParams.aqStrength) : "auto") : "disabled") - << std::endl << "\ttemporalaq : " << (pParams->encodeConfig->rcParams.enableTemporalAQ ? "enabled" : "disabled") - << std::endl << "\tlookahead : " << (pParams->encodeConfig->rcParams.enableLookahead ? std::to_string(pParams->encodeConfig->rcParams.lookaheadDepth) : "disabled") - << std::endl << "\tcq : " << pParams->encodeConfig->rcParams.targetQuality - << std::endl << "\tqmin : P,B,I=" << pParams->encodeConfig->rcParams.minQP.qpInterP << "," << pParams->encodeConfig->rcParams.minQP.qpInterB << "," << pParams->encodeConfig->rcParams.minQP.qpIntra - << std::endl << "\tqmax : P,B,I=" << pParams->encodeConfig->rcParams.maxQP.qpInterP << "," << pParams->encodeConfig->rcParams.maxQP.qpInterB << "," << pParams->encodeConfig->rcParams.maxQP.qpIntra - << std::endl << "\tinitqp : P,B,I=" << pParams->encodeConfig->rcParams.initialRCQP.qpInterP << "," << pParams->encodeConfig->rcParams.initialRCQP.qpInterB << "," << pParams->encodeConfig->rcParams.initialRCQP.qpIntra - ; - return os.str(); - } - -public: - virtual GUID GetEncodeGUID() { return guidCodec; } - virtual GUID GetPresetGUID() { return guidPreset; } - virtual void SetInitParams(NV_ENC_INITIALIZE_PARAMS *pParams, NV_ENC_BUFFER_FORMAT eBufferFormat) - { - NV_ENC_CONFIG &config = *pParams->encodeConfig; - for (unsigned i = 0; i < tokens.size(); i++) - { - if ( - tokens[i] == "-codec" && ++i || - tokens[i] == "-preset" && ++i || - tokens[i] == "-profile" && ++i != tokens.size() && (IsCodecH264() ? - ParseString("-profile", tokens[i], vH264Profile, szH264ProfileNames, &config.profileGUID) : - ParseString("-profile", tokens[i], vHevcProfile, szHevcProfileNames, &config.profileGUID)) || - tokens[i] == "-rc" && ++i != tokens.size() && ParseString("-rc", tokens[i], vRcMode, szRcModeNames, &config.rcParams.rateControlMode) || - tokens[i] == "-fps" && ++i != tokens.size() && ParseInt("-fps", tokens[i], &pParams->frameRateNum) || - tokens[i] == "-bf" && ++i != tokens.size() && ParseInt("-bf", tokens[i], &config.frameIntervalP) && ++config.frameIntervalP || - tokens[i] == "-bitrate" && ++i != tokens.size() && ParseBitRate("-bitrate", tokens[i], &config.rcParams.averageBitRate) || - tokens[i] == "-maxbitrate" && ++i != tokens.size() && ParseBitRate("-maxbitrate", tokens[i], &config.rcParams.maxBitRate) || - tokens[i] == "-vbvbufsize" && ++i != tokens.size() && ParseBitRate("-vbvbufsize", tokens[i], &config.rcParams.vbvBufferSize) || - tokens[i] == "-vbvinit" && ++i != tokens.size() && ParseBitRate("-vbvinit", tokens[i], &config.rcParams.vbvInitialDelay) || - tokens[i] == "-cq" && ++i != tokens.size() && ParseInt("-cq", tokens[i], &config.rcParams.targetQuality) || - tokens[i] == "-initqp" && ++i != tokens.size() && ParseQp("-initqp", tokens[i], &config.rcParams.initialRCQP) && (config.rcParams.enableInitialRCQP = true) || - tokens[i] == "-qmin" && ++i != tokens.size() && ParseQp("-qmin", tokens[i], &config.rcParams.minQP) && (config.rcParams.enableMinQP = true) || - tokens[i] == "-qmax" && ++i != tokens.size() && ParseQp("-qmax", tokens[i], &config.rcParams.maxQP) && (config.rcParams.enableMaxQP = true) || - tokens[i] == "-constqp" && ++i != tokens.size() && ParseQp("-constqp", tokens[i], &config.rcParams.constQP) || - tokens[i] == "-temporalaq" && (config.rcParams.enableTemporalAQ = true) - ) - { - continue; - } - if (tokens[i] == "-lookahead" && ++i != tokens.size() && ParseInt("-lookahead", tokens[i], &config.rcParams.lookaheadDepth)) - { - config.rcParams.enableLookahead = config.rcParams.lookaheadDepth > 0; - continue; - } - int aqStrength; - if (tokens[i] == "-aq" && ++i != tokens.size() && ParseInt("-aq", tokens[i], &aqStrength)) { - config.rcParams.enableAQ = true; - config.rcParams.aqStrength = aqStrength; - continue; - } - - if (tokens[i] == "-gop" && ++i != tokens.size() && ParseInt("-gop", tokens[i], &config.gopLength)) - { - if (IsCodecH264()) - { - config.encodeCodecConfig.h264Config.idrPeriod = config.gopLength; - } - else - { - config.encodeCodecConfig.hevcConfig.idrPeriod = config.gopLength; - } - continue; - } - - if (tokens[i] == "-444") - { - if (IsCodecH264()) - { - config.encodeCodecConfig.h264Config.chromaFormatIDC = 3; - } else - { - config.encodeCodecConfig.hevcConfig.chromaFormatIDC = 3; - } - continue; - } - - std::ostringstream errmessage; - errmessage << "Incorrect parameter: " << tokens[i] << std::endl; - errmessage << "Re-run the application with the -h option to get a list of the supported options."; - errmessage << std::endl; - - throw std::invalid_argument(errmessage.str()); - } - - if (IsCodecHEVC()) - { - if (eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) - { - config.encodeCodecConfig.hevcConfig.pixelBitDepthMinus8 = 2; - } - } - - funcInit(pParams); - LOG(INFO) << NvEncoderInitParam().MainParamToString(pParams); - LOG(TRACE) << NvEncoderInitParam().FullParamToString(pParams); - } - -private: - template<typename T> - bool ParseString(const std::string &strName, const std::string &strValue, const std::vector<T> &vValue, const std::string &strValueNames, T *pValue) { - std::vector<std::string> vstrValueName = split(strValueNames, ' '); - auto it = std::find(vstrValueName.begin(), vstrValueName.end(), strValue); - if (it == vstrValueName.end()) { - LOG(ERROR) << strName << " options: " << strValueNames; - return false; - } - *pValue = vValue[it - vstrValueName.begin()]; - return true; - } - template<typename T> - std::string ConvertValueToString(const std::vector<T> &vValue, const std::string &strValueNames, T value) { - auto it = std::find(vValue.begin(), vValue.end(), value); - if (it == vValue.end()) { - LOG(ERROR) << "Invalid value. Can't convert to one of " << strValueNames; - return std::string(); - } - return split(strValueNames, ' ')[it - vValue.begin()]; - } - bool ParseBitRate(const std::string &strName, const std::string &strValue, unsigned *pBitRate) { - try { - size_t l; - double r = std::stod(strValue, &l); - char c = strValue[l]; - if (c != 0 && c != 'k' && c != 'm') { - LOG(ERROR) << strName << " units: 1, K, M (lower case also allowed)"; - } - *pBitRate = (unsigned)((c == 'm' ? 1000000 : (c == 'k' ? 1000 : 1)) * r); - } catch (std::invalid_argument) { - return false; - } - return true; - } - template<typename T> - bool ParseInt(const std::string &strName, const std::string &strValue, T *pInt) { - try { - *pInt = std::stoi(strValue); - } catch (std::invalid_argument) { - LOG(ERROR) << strName << " need a value of positive number"; - return false; - } - return true; - } - bool ParseQp(const std::string &strName, const std::string &strValue, NV_ENC_QP *pQp) { - std::vector<std::string> vQp = split(strValue, ','); - try { - if (vQp.size() == 1) { - unsigned qp = (unsigned)std::stoi(vQp[0]); - *pQp = {qp, qp, qp}; - } else if (vQp.size() == 3) { - *pQp = {(unsigned)std::stoi(vQp[0]), (unsigned)std::stoi(vQp[1]), (unsigned)std::stoi(vQp[2])}; - } else { - LOG(ERROR) << strName << " qp_for_P_B_I or qp_P,qp_B,qp_I (no space is allowed)"; - return false; - } - } catch (std::invalid_argument) { - return false; - } - return true; - } - std::vector<std::string> split(const std::string &s, char delim) { - std::stringstream ss(s); - std::string token; - std::vector<std::string> tokens; - while (getline(ss, token, delim)) { - tokens.push_back(token); - } - return tokens; - } - -private: - std::string strParam; - std::function<void(NV_ENC_INITIALIZE_PARAMS *pParams)> funcInit = [](NV_ENC_INITIALIZE_PARAMS *pParams){}; - std::vector<std::string> tokens; - GUID guidCodec = NV_ENC_CODEC_H264_GUID; - GUID guidPreset = NV_ENC_PRESET_DEFAULT_GUID; - bool bLowLatency = false; - - const char *szCodecNames = "h264 hevc"; - std::vector<GUID> vCodec = std::vector<GUID> { - NV_ENC_CODEC_H264_GUID, - NV_ENC_CODEC_HEVC_GUID - }; - - const char *szChromaNames = "yuv420 yuv444"; - std::vector<uint32_t> vChroma = std::vector<uint32_t> - { - 1, 3 - }; - - const char *szPresetNames = "default hp hq bd ll ll_hp ll_hq lossless lossless_hp"; - const char *szLowLatencyPresetNames = "ll ll_hp ll_hq"; - std::vector<GUID> vPreset = std::vector<GUID> { - NV_ENC_PRESET_DEFAULT_GUID, - NV_ENC_PRESET_HP_GUID, - NV_ENC_PRESET_HQ_GUID, - NV_ENC_PRESET_BD_GUID, - NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID, - NV_ENC_PRESET_LOW_LATENCY_HP_GUID, - NV_ENC_PRESET_LOW_LATENCY_HQ_GUID, - NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID, - NV_ENC_PRESET_LOSSLESS_HP_GUID - }; - - std::vector<GUID> vLowLatencyPreset = std::vector<GUID> { - NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID, - NV_ENC_PRESET_LOW_LATENCY_HP_GUID, - NV_ENC_PRESET_LOW_LATENCY_HQ_GUID, - }; - - const char *szH264ProfileNames = "baseline main high high444"; - std::vector<GUID> vH264Profile = std::vector<GUID> { - NV_ENC_H264_PROFILE_BASELINE_GUID, - NV_ENC_H264_PROFILE_MAIN_GUID, - NV_ENC_H264_PROFILE_HIGH_GUID, - NV_ENC_H264_PROFILE_HIGH_444_GUID, - }; - const char *szHevcProfileNames = "main main10 frext"; - std::vector<GUID> vHevcProfile = std::vector<GUID> { - NV_ENC_HEVC_PROFILE_MAIN_GUID, - NV_ENC_HEVC_PROFILE_MAIN10_GUID, - NV_ENC_HEVC_PROFILE_FREXT_GUID, - }; - const char *szProfileNames = "(default) auto baseline(h264) main(h264) high(h264) high444(h264)" - " stereo(h264) svc_temporal_scalability(h264) progressiv_high(h264) constrained_high(h264)" - " main(hevc) main10(hevc) frext(hevc)"; - std::vector<GUID> vProfile = std::vector<GUID> { - GUID{}, - NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID, - NV_ENC_H264_PROFILE_BASELINE_GUID, - NV_ENC_H264_PROFILE_MAIN_GUID, - NV_ENC_H264_PROFILE_HIGH_GUID, - NV_ENC_H264_PROFILE_HIGH_444_GUID, - NV_ENC_H264_PROFILE_STEREO_GUID, - NV_ENC_H264_PROFILE_SVC_TEMPORAL_SCALABILTY, - NV_ENC_H264_PROFILE_PROGRESSIVE_HIGH_GUID, - NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID, - NV_ENC_HEVC_PROFILE_MAIN_GUID, - NV_ENC_HEVC_PROFILE_MAIN10_GUID, - NV_ENC_HEVC_PROFILE_FREXT_GUID, - }; - - const char *szRcModeNames = "constqp vbr cbr cbr_ll_hq cbr_hq vbr_hq"; - std::vector<NV_ENC_PARAMS_RC_MODE> vRcMode = std::vector<NV_ENC_PARAMS_RC_MODE> { - NV_ENC_PARAMS_RC_CONSTQP, - NV_ENC_PARAMS_RC_VBR, - NV_ENC_PARAMS_RC_CBR, - NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ, - NV_ENC_PARAMS_RC_CBR_HQ, - NV_ENC_PARAMS_RC_VBR_HQ, - }; - - const char *szQpMapModeNames = "disabled emphasis_level_map delta_qp_map qp_map"; - std::vector<NV_ENC_QP_MAP_MODE> vQpMapMode = std::vector<NV_ENC_QP_MAP_MODE> { - NV_ENC_QP_MAP_DISABLED, - NV_ENC_QP_MAP_EMPHASIS, - NV_ENC_QP_MAP_DELTA, - NV_ENC_QP_MAP, - }; - - -public: - std::string FullParamToString(const NV_ENC_INITIALIZE_PARAMS *pInitializeParams) { - std::ostringstream os; - os << "NV_ENC_INITIALIZE_PARAMS:" << std::endl - << "encodeGUID: " << ConvertValueToString(vCodec, szCodecNames, pInitializeParams->encodeGUID) << std::endl - << "presetGUID: " << ConvertValueToString(vPreset, szPresetNames, pInitializeParams->presetGUID) << std::endl - << "encodeWidth: " << pInitializeParams->encodeWidth << std::endl - << "encodeHeight: " << pInitializeParams->encodeHeight << std::endl - << "darWidth: " << pInitializeParams->darWidth << std::endl - << "darHeight: " << pInitializeParams->darHeight << std::endl - << "frameRateNum: " << pInitializeParams->frameRateNum << std::endl - << "frameRateDen: " << pInitializeParams->frameRateDen << std::endl - << "enableEncodeAsync: " << pInitializeParams->enableEncodeAsync << std::endl - << "reportSliceOffsets: " << pInitializeParams->reportSliceOffsets << std::endl - << "enableSubFrameWrite: " << pInitializeParams->enableSubFrameWrite << std::endl - << "enableExternalMEHints: " << pInitializeParams->enableExternalMEHints << std::endl - << "enableMEOnlyMode: " << pInitializeParams->enableMEOnlyMode << std::endl - << "enableWeightedPrediction: " << pInitializeParams->enableWeightedPrediction << std::endl - << "maxEncodeWidth: " << pInitializeParams->maxEncodeWidth << std::endl - << "maxEncodeHeight: " << pInitializeParams->maxEncodeHeight << std::endl - << "maxMEHintCountsPerBlock: " << pInitializeParams->maxMEHintCountsPerBlock << std::endl - ; - NV_ENC_CONFIG *pConfig = pInitializeParams->encodeConfig; - os << "NV_ENC_CONFIG:" << std::endl - << "profile: " << ConvertValueToString(vProfile, szProfileNames, pConfig->profileGUID) << std::endl - << "gopLength: " << pConfig->gopLength << std::endl - << "frameIntervalP: " << pConfig->frameIntervalP << std::endl - << "monoChromeEncoding: " << pConfig->monoChromeEncoding << std::endl - << "frameFieldMode: " << pConfig->frameFieldMode << std::endl - << "mvPrecision: " << pConfig->mvPrecision << std::endl - << "NV_ENC_RC_PARAMS:" << std::endl - << " rateControlMode: 0x" << std::hex << pConfig->rcParams.rateControlMode << std::dec << std::endl - << " constQP: " << pConfig->rcParams.constQP.qpInterP << ", " << pConfig->rcParams.constQP.qpInterB << ", " << pConfig->rcParams.constQP.qpIntra << std::endl - << " averageBitRate: " << pConfig->rcParams.averageBitRate << std::endl - << " maxBitRate: " << pConfig->rcParams.maxBitRate << std::endl - << " vbvBufferSize: " << pConfig->rcParams.vbvBufferSize << std::endl - << " vbvInitialDelay: " << pConfig->rcParams.vbvInitialDelay << std::endl - << " enableMinQP: " << pConfig->rcParams.enableMinQP << std::endl - << " enableMaxQP: " << pConfig->rcParams.enableMaxQP << std::endl - << " enableInitialRCQP: " << pConfig->rcParams.enableInitialRCQP << std::endl - << " enableAQ: " << pConfig->rcParams.enableAQ << std::endl - << " qpMapMode: " << ConvertValueToString(vQpMapMode, szQpMapModeNames, pConfig->rcParams.qpMapMode) << std::endl - << " enableLookahead: " << pConfig->rcParams.enableLookahead << std::endl - << " disableIadapt: " << pConfig->rcParams.disableIadapt << std::endl - << " disableBadapt: " << pConfig->rcParams.disableBadapt << std::endl - << " enableTemporalAQ: " << pConfig->rcParams.enableTemporalAQ << std::endl - << " zeroReorderDelay: " << pConfig->rcParams.zeroReorderDelay << std::endl - << " enableNonRefP: " << pConfig->rcParams.enableNonRefP << std::endl - << " strictGOPTarget: " << pConfig->rcParams.strictGOPTarget << std::endl - << " aqStrength: " << pConfig->rcParams.aqStrength << std::endl - << " minQP: " << pConfig->rcParams.minQP.qpInterP << ", " << pConfig->rcParams.minQP.qpInterB << ", " << pConfig->rcParams.minQP.qpIntra << std::endl - << " maxQP: " << pConfig->rcParams.maxQP.qpInterP << ", " << pConfig->rcParams.maxQP.qpInterB << ", " << pConfig->rcParams.maxQP.qpIntra << std::endl - << " initialRCQP: " << pConfig->rcParams.initialRCQP.qpInterP << ", " << pConfig->rcParams.initialRCQP.qpInterB << ", " << pConfig->rcParams.initialRCQP.qpIntra << std::endl - << " temporallayerIdxMask: " << pConfig->rcParams.temporallayerIdxMask << std::endl - << " temporalLayerQP: " << (int)pConfig->rcParams.temporalLayerQP[0] << ", " << (int)pConfig->rcParams.temporalLayerQP[1] << ", " << (int)pConfig->rcParams.temporalLayerQP[2] << ", " << (int)pConfig->rcParams.temporalLayerQP[3] << ", " << (int)pConfig->rcParams.temporalLayerQP[4] << ", " << (int)pConfig->rcParams.temporalLayerQP[5] << ", " << (int)pConfig->rcParams.temporalLayerQP[6] << ", " << (int)pConfig->rcParams.temporalLayerQP[7] << std::endl - << " targetQuality: " << pConfig->rcParams.targetQuality << std::endl - << " lookaheadDepth: " << pConfig->rcParams.lookaheadDepth << std::endl; - if (pInitializeParams->encodeGUID == NV_ENC_CODEC_H264_GUID) { - os - << "NV_ENC_CODEC_CONFIG (H264):" << std::endl - << " enableTemporalSVC: " << pConfig->encodeCodecConfig.h264Config.enableTemporalSVC << std::endl - << " enableStereoMVC: " << pConfig->encodeCodecConfig.h264Config.enableStereoMVC << std::endl - << " hierarchicalPFrames: " << pConfig->encodeCodecConfig.h264Config.hierarchicalPFrames << std::endl - << " hierarchicalBFrames: " << pConfig->encodeCodecConfig.h264Config.hierarchicalBFrames << std::endl - << " outputBufferingPeriodSEI: " << pConfig->encodeCodecConfig.h264Config.outputBufferingPeriodSEI << std::endl - << " outputPictureTimingSEI: " << pConfig->encodeCodecConfig.h264Config.outputPictureTimingSEI << std::endl - << " outputAUD: " << pConfig->encodeCodecConfig.h264Config.outputAUD << std::endl - << " disableSPSPPS: " << pConfig->encodeCodecConfig.h264Config.disableSPSPPS << std::endl - << " outputFramePackingSEI: " << pConfig->encodeCodecConfig.h264Config.outputFramePackingSEI << std::endl - << " outputRecoveryPointSEI: " << pConfig->encodeCodecConfig.h264Config.outputRecoveryPointSEI << std::endl - << " enableIntraRefresh: " << pConfig->encodeCodecConfig.h264Config.enableIntraRefresh << std::endl - << " enableConstrainedEncoding: " << pConfig->encodeCodecConfig.h264Config.enableConstrainedEncoding << std::endl - << " repeatSPSPPS: " << pConfig->encodeCodecConfig.h264Config.repeatSPSPPS << std::endl - << " enableVFR: " << pConfig->encodeCodecConfig.h264Config.enableVFR << std::endl - << " enableLTR: " << pConfig->encodeCodecConfig.h264Config.enableLTR << std::endl - << " qpPrimeYZeroTransformBypassFlag: " << pConfig->encodeCodecConfig.h264Config.qpPrimeYZeroTransformBypassFlag << std::endl - << " useConstrainedIntraPred: " << pConfig->encodeCodecConfig.h264Config.useConstrainedIntraPred << std::endl - << " level: " << pConfig->encodeCodecConfig.h264Config.level << std::endl - << " idrPeriod: " << pConfig->encodeCodecConfig.h264Config.idrPeriod << std::endl - << " separateColourPlaneFlag: " << pConfig->encodeCodecConfig.h264Config.separateColourPlaneFlag << std::endl - << " disableDeblockingFilterIDC: " << pConfig->encodeCodecConfig.h264Config.disableDeblockingFilterIDC << std::endl - << " numTemporalLayers: " << pConfig->encodeCodecConfig.h264Config.numTemporalLayers << std::endl - << " enableTemporalSVC: " << pConfig->encodeCodecConfig.h264Config.enableTemporalSVC << std::endl - << " spsId: " << pConfig->encodeCodecConfig.h264Config.spsId << std::endl - << " ppsId: " << pConfig->encodeCodecConfig.h264Config.ppsId << std::endl - << " adaptiveTransformMode: " << pConfig->encodeCodecConfig.h264Config.adaptiveTransformMode << std::endl - << " fmoMode: " << pConfig->encodeCodecConfig.h264Config.fmoMode << std::endl - << " bdirectMode: " << pConfig->encodeCodecConfig.h264Config.bdirectMode << std::endl - << " entropyCodingMode: " << pConfig->encodeCodecConfig.h264Config.entropyCodingMode << std::endl - << " stereoMode: " << pConfig->encodeCodecConfig.h264Config.stereoMode << std::endl - << " intraRefreshPeriod: " << pConfig->encodeCodecConfig.h264Config.intraRefreshPeriod << std::endl - << " intraRefreshCnt: " << pConfig->encodeCodecConfig.h264Config.intraRefreshCnt << std::endl - << " maxNumRefFrames: " << pConfig->encodeCodecConfig.h264Config.maxNumRefFrames << std::endl - << " sliceMode: " << pConfig->encodeCodecConfig.h264Config.sliceMode << std::endl - << " sliceModeData: " << pConfig->encodeCodecConfig.h264Config.sliceModeData << std::endl - << " NV_ENC_CONFIG_H264_VUI_PARAMETERS:" << std::endl - << " overscanInfoPresentFlag: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.overscanInfoPresentFlag << std::endl - << " overscanInfo: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.overscanInfo << std::endl - << " videoSignalTypePresentFlag: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag << std::endl - << " videoFormat: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.videoFormat << std::endl - << " videoFullRangeFlag: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.videoFullRangeFlag << std::endl - << " colourDescriptionPresentFlag: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.colourDescriptionPresentFlag << std::endl - << " colourPrimaries: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.colourPrimaries << std::endl - << " transferCharacteristics: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.transferCharacteristics << std::endl - << " colourMatrix: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.colourMatrix << std::endl - << " chromaSampleLocationFlag: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.chromaSampleLocationFlag << std::endl - << " chromaSampleLocationTop: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.chromaSampleLocationTop << std::endl - << " chromaSampleLocationBot: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.chromaSampleLocationBot << std::endl - << " bitstreamRestrictionFlag: " << pConfig->encodeCodecConfig.h264Config.h264VUIParameters.bitstreamRestrictionFlag << std::endl - << " ltrNumFrames: " << pConfig->encodeCodecConfig.h264Config.ltrNumFrames << std::endl - << " ltrTrustMode: " << pConfig->encodeCodecConfig.h264Config.ltrTrustMode << std::endl - << " chromaFormatIDC: " << pConfig->encodeCodecConfig.h264Config.chromaFormatIDC << std::endl - << " maxTemporalLayers: " << pConfig->encodeCodecConfig.h264Config.maxTemporalLayers << std::endl; - } else if (pInitializeParams->encodeGUID == NV_ENC_CODEC_HEVC_GUID) { - os - << "NV_ENC_CODEC_CONFIG (HEVC):" << std::endl - << " level: " << pConfig->encodeCodecConfig.hevcConfig.level << std::endl - << " tier: " << pConfig->encodeCodecConfig.hevcConfig.tier << std::endl - << " minCUSize: " << pConfig->encodeCodecConfig.hevcConfig.minCUSize << std::endl - << " maxCUSize: " << pConfig->encodeCodecConfig.hevcConfig.maxCUSize << std::endl - << " useConstrainedIntraPred: " << pConfig->encodeCodecConfig.hevcConfig.useConstrainedIntraPred << std::endl - << " disableDeblockAcrossSliceBoundary: " << pConfig->encodeCodecConfig.hevcConfig.disableDeblockAcrossSliceBoundary << std::endl - << " outputBufferingPeriodSEI: " << pConfig->encodeCodecConfig.hevcConfig.outputBufferingPeriodSEI << std::endl - << " outputPictureTimingSEI: " << pConfig->encodeCodecConfig.hevcConfig.outputPictureTimingSEI << std::endl - << " outputAUD: " << pConfig->encodeCodecConfig.hevcConfig.outputAUD << std::endl - << " enableLTR: " << pConfig->encodeCodecConfig.hevcConfig.enableLTR << std::endl - << " disableSPSPPS: " << pConfig->encodeCodecConfig.hevcConfig.disableSPSPPS << std::endl - << " repeatSPSPPS: " << pConfig->encodeCodecConfig.hevcConfig.repeatSPSPPS << std::endl - << " enableIntraRefresh: " << pConfig->encodeCodecConfig.hevcConfig.enableIntraRefresh << std::endl - << " chromaFormatIDC: " << pConfig->encodeCodecConfig.hevcConfig.chromaFormatIDC << std::endl - << " pixelBitDepthMinus8: " << pConfig->encodeCodecConfig.hevcConfig.pixelBitDepthMinus8 << std::endl - << " idrPeriod: " << pConfig->encodeCodecConfig.hevcConfig.idrPeriod << std::endl - << " intraRefreshPeriod: " << pConfig->encodeCodecConfig.hevcConfig.intraRefreshPeriod << std::endl - << " intraRefreshCnt: " << pConfig->encodeCodecConfig.hevcConfig.intraRefreshCnt << std::endl - << " maxNumRefFramesInDPB: " << pConfig->encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB << std::endl - << " ltrNumFrames: " << pConfig->encodeCodecConfig.hevcConfig.ltrNumFrames << std::endl - << " vpsId: " << pConfig->encodeCodecConfig.hevcConfig.vpsId << std::endl - << " spsId: " << pConfig->encodeCodecConfig.hevcConfig.spsId << std::endl - << " ppsId: " << pConfig->encodeCodecConfig.hevcConfig.ppsId << std::endl - << " sliceMode: " << pConfig->encodeCodecConfig.hevcConfig.sliceMode << std::endl - << " sliceModeData: " << pConfig->encodeCodecConfig.hevcConfig.sliceModeData << std::endl - << " maxTemporalLayersMinus1: " << pConfig->encodeCodecConfig.hevcConfig.maxTemporalLayersMinus1 << std::endl - << " NV_ENC_CONFIG_HEVC_VUI_PARAMETERS:" << std::endl - << " overscanInfoPresentFlag: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.overscanInfoPresentFlag << std::endl - << " overscanInfo: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.overscanInfo << std::endl - << " videoSignalTypePresentFlag: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.videoSignalTypePresentFlag << std::endl - << " videoFormat: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.videoFormat << std::endl - << " videoFullRangeFlag: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.videoFullRangeFlag << std::endl - << " colourDescriptionPresentFlag: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.colourDescriptionPresentFlag << std::endl - << " colourPrimaries: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.colourPrimaries << std::endl - << " transferCharacteristics: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.transferCharacteristics << std::endl - << " colourMatrix: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.colourMatrix << std::endl - << " chromaSampleLocationFlag: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.chromaSampleLocationFlag << std::endl - << " chromaSampleLocationTop: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.chromaSampleLocationTop << std::endl - << " chromaSampleLocationBot: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.chromaSampleLocationBot << std::endl - << " bitstreamRestrictionFlag: " << pConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.bitstreamRestrictionFlag << std::endl - << " ltrTrustMode: " << pConfig->encodeCodecConfig.hevcConfig.ltrTrustMode << std::endl; - } - return os.str(); - } -}; diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/Resize.cu b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/Resize.cu deleted file mode 100644 index c928e4ff3de70bea4469c9dd2ad66229786741b9..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/Samples/Utils/Resize.cu +++ /dev/null @@ -1,192 +0,0 @@ -/* -* Copyright 2017-2018 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include <cuda_runtime.h> -#include "NvCodecUtils.h" - -template<typename YuvUnitx2> -static __global__ void Resize(cudaTextureObject_t texY, cudaTextureObject_t texUv, - uint8_t *pDst, uint8_t *pDstUV, int nPitch, int nWidth, int nHeight, - float fxScale, float fyScale) -{ - int ix = blockIdx.x * blockDim.x + threadIdx.x, - iy = blockIdx.y * blockDim.y + threadIdx.y; - - if (ix >= nWidth / 2 || iy >= nHeight / 2) { - return; - } - - int x = ix * 2, y = iy * 2; - typedef decltype(YuvUnitx2::x) YuvUnit; - const int MAX = 1 << (sizeof(YuvUnit) * 8); - *(YuvUnitx2 *)(pDst + y * nPitch + x * sizeof(YuvUnit)) = YuvUnitx2 { - (YuvUnit)(tex2D<float>(texY, x / fxScale, y / fyScale) * MAX), - (YuvUnit)(tex2D<float>(texY, (x + 1) / fxScale, y / fyScale) * MAX) - }; - y++; - *(YuvUnitx2 *)(pDst + y * nPitch + x * sizeof(YuvUnit)) = YuvUnitx2 { - (YuvUnit)(tex2D<float>(texY, x / fxScale, y / fyScale) * MAX), - (YuvUnit)(tex2D<float>(texY, (x + 1) / fxScale, y / fyScale) * MAX) - }; - float2 uv = tex2D<float2>(texUv, ix / fxScale, (nHeight + iy) / fyScale + 0.5f); - *(YuvUnitx2 *)(pDstUV + iy * nPitch + ix * 2 * sizeof(YuvUnit)) = YuvUnitx2{ (YuvUnit)(uv.x * MAX), (YuvUnit)(uv.y * MAX) }; -} - -template <typename YuvUnitx2> -static void Resize(unsigned char *dpDst, unsigned char* dpDstUV, int nDstPitch, int nDstWidth, int nDstHeight, unsigned char *dpSrc, int nSrcPitch, int nSrcWidth, int nSrcHeight) { - cudaResourceDesc resDesc = {}; - resDesc.resType = cudaResourceTypePitch2D; - resDesc.res.pitch2D.devPtr = dpSrc; - resDesc.res.pitch2D.desc = cudaCreateChannelDesc<decltype(YuvUnitx2::x)>(); - resDesc.res.pitch2D.width = nSrcWidth; - resDesc.res.pitch2D.height = nSrcHeight; - resDesc.res.pitch2D.pitchInBytes = nSrcPitch; - - cudaTextureDesc texDesc = {}; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeNormalizedFloat; - - cudaTextureObject_t texY=0; - ck(cudaCreateTextureObject(&texY, &resDesc, &texDesc, NULL)); - - resDesc.res.pitch2D.desc = cudaCreateChannelDesc<YuvUnitx2>(); - resDesc.res.pitch2D.width = nSrcWidth / 2; - resDesc.res.pitch2D.height = nSrcHeight * 3 / 2; - - cudaTextureObject_t texUv=0; - ck(cudaCreateTextureObject(&texUv, &resDesc, &texDesc, NULL)); - - Resize<YuvUnitx2> << <dim3((nDstWidth + 31) / 32, (nDstHeight + 31) / 32), dim3(16, 16) >> >(texY, texUv, dpDst, dpDstUV, - nDstPitch, nDstWidth, nDstHeight, 1.0f * nDstWidth / nSrcWidth, 1.0f * nDstHeight / nSrcHeight); - - ck(cudaDestroyTextureObject(texY)); - ck(cudaDestroyTextureObject(texUv)); -} - -void ResizeNv12(unsigned char *dpDstNv12, int nDstPitch, int nDstWidth, int nDstHeight, unsigned char *dpSrcNv12, int nSrcPitch, int nSrcWidth, int nSrcHeight, unsigned char* dpDstNv12UV) -{ - unsigned char* dpDstUV = dpDstNv12UV ? dpDstNv12UV : dpDstNv12 + (nDstPitch*nDstHeight); - return Resize<uchar2>(dpDstNv12, dpDstUV, nDstPitch, nDstWidth, nDstHeight, dpSrcNv12, nSrcPitch, nSrcWidth, nSrcHeight); -} - - -void ResizeP016(unsigned char *dpDstP016, int nDstPitch, int nDstWidth, int nDstHeight, unsigned char *dpSrcP016, int nSrcPitch, int nSrcWidth, int nSrcHeight, unsigned char* dpDstP016UV) -{ - unsigned char* dpDstUV = dpDstP016UV ? dpDstP016UV : dpDstP016 + (nDstPitch*nDstHeight); - return Resize<ushort2>(dpDstP016, dpDstUV, nDstPitch, nDstWidth, nDstHeight, dpSrcP016, nSrcPitch, nSrcWidth, nSrcHeight); -} - -static __global__ void Scale(cudaTextureObject_t texSrc, - uint8_t *pDst, int nPitch, int nWidth, int nHeight, - float fxScale, float fyScale) -{ - int x = blockIdx.x * blockDim.x + threadIdx.x, - y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x >= nWidth || y >= nHeight) - { - return; - } - - *(unsigned char*)(pDst + (y * nPitch) + x) = (unsigned char)(fminf((tex2D<float>(texSrc, x * fxScale, y * fyScale)) * 255.0f, 255.0f)); -} - -static __global__ void Scale_uv(cudaTextureObject_t texSrc, - uint8_t *pDst, int nPitch, int nWidth, int nHeight, - float fxScale, float fyScale) -{ - int x = blockIdx.x * blockDim.x + threadIdx.x, - y = blockIdx.y * blockDim.y + threadIdx.y; - - if (x >= nWidth || y >= nHeight) - { - return; - } - - float2 uv = tex2D<float2>(texSrc, x * fxScale, y * fyScale); - uchar2 uvOut = uchar2{ (unsigned char)(fminf(uv.x * 255.0f, 255.0f)), (unsigned char)(fminf(uv.y * 255.0f, 255.0f)) }; - - *(uchar2*)(pDst + (y * nPitch) + 2 * x) = uvOut; -} - -void ScaleKernelLaunch(unsigned char *dpDst, int nDstPitch, int nDstWidth, int nDstHeight, unsigned char *dpSrc, int nSrcPitch, int nSrcWidth, int nSrcHeight, bool bUVPlane = false) -{ - cudaResourceDesc resDesc = {}; - resDesc.resType = cudaResourceTypePitch2D; - resDesc.res.pitch2D.devPtr = dpSrc; - resDesc.res.pitch2D.desc = bUVPlane ? cudaCreateChannelDesc<uchar2>() : cudaCreateChannelDesc<unsigned char>(); - resDesc.res.pitch2D.width = nSrcWidth; - resDesc.res.pitch2D.height = nSrcHeight; - resDesc.res.pitch2D.pitchInBytes = nSrcPitch; - - cudaTextureDesc texDesc = {}; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeNormalizedFloat; - - texDesc.addressMode[0] = cudaAddressModeClamp; - texDesc.addressMode[1] = cudaAddressModeClamp; - texDesc.addressMode[2] = cudaAddressModeClamp; - - cudaTextureObject_t texSrc = 0; - ck(cudaCreateTextureObject(&texSrc, &resDesc, &texDesc, NULL)); - - dim3 blockSize(16, 16, 1); - dim3 gridSize(((uint32_t)nDstWidth + blockSize.x - 1) / blockSize.x, ((uint32_t)nDstHeight + blockSize.y - 1) / blockSize.y, 1); - - if (bUVPlane) - { - Scale_uv << <gridSize, blockSize >> >(texSrc, dpDst, - nDstPitch, nDstWidth, nDstHeight, 1.0f * nSrcWidth / nDstWidth, 1.0f * nSrcHeight / nDstHeight); - } - else - { - Scale << <gridSize, blockSize >> >(texSrc, dpDst, - nDstPitch, nDstWidth, nDstHeight, 1.0f * nSrcWidth / nDstWidth, 1.0f * nSrcHeight / nDstHeight); - } - - ck(cudaGetLastError()); - ck(cudaDestroyTextureObject(texSrc)); -} - -void ScaleYUV420(unsigned char *dpDstY, - unsigned char* dpDstU, - unsigned char* dpDstV, - int nDstPitch, - int nDstChromaPitch, - int nDstWidth, - int nDstHeight, - unsigned char *dpSrcY, - unsigned char* dpSrcU, - unsigned char* dpSrcV, - int nSrcPitch, - int nSrcChromaPitch, - int nSrcWidth, - int nSrcHeight, - bool bSemiplanar) -{ - int chromaWidthDst = (nDstWidth + 1) / 2; - int chromaHeightDst = (nDstHeight + 1) / 2; - - int chromaWidthSrc = (nSrcWidth + 1) / 2; - int chromaHeightSrc = (nSrcHeight + 1) / 2; - - ScaleKernelLaunch(dpDstY, nDstPitch, nDstWidth, nDstHeight, dpSrcY, nSrcPitch, nSrcWidth, nSrcHeight); - - if (bSemiplanar) - { - ScaleKernelLaunch(dpDstU, nDstChromaPitch, chromaWidthDst, chromaHeightDst, dpSrcU, nSrcChromaPitch, chromaWidthSrc, chromaHeightSrc, true); - } - else - { - ScaleKernelLaunch(dpDstU, nDstChromaPitch, chromaWidthDst, chromaHeightDst, dpSrcU, nSrcChromaPitch, chromaWidthSrc, chromaHeightSrc); - ScaleKernelLaunch(dpDstV, nDstChromaPitch, chromaWidthDst, chromaHeightDst, dpSrcV, nSrcChromaPitch, chromaWidthSrc, chromaHeightSrc); - } -} diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/include/cuviddec.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/include/cuviddec.h deleted file mode 100644 index b24d2850f133613f2ef7185123b6c30f701d4aa3..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/include/cuviddec.h +++ /dev/null @@ -1,1001 +0,0 @@ -/* - * This copyright notice applies to this header file only: - * - * Copyright (c) 2010-2019 NVIDIA Corporation - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the software, and to permit persons to whom the - * software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -/*****************************************************************************************************/ -//! \file cuviddec.h -//! NVDECODE API provides video decoding interface to NVIDIA GPU devices. -//! \date 2015-2019 -//! This file contains constants, structure definitions and function prototypes used for decoding. -/*****************************************************************************************************/ - -#if !defined(__CUDA_VIDEO_H__) -#define __CUDA_VIDEO_H__ - -#ifndef __cuda_cuda_h__ -#include <cuda.h> -#endif // __cuda_cuda_h__ - -#if defined(_WIN64) || defined(__LP64__) || defined(__x86_64) || defined(AMD64) || defined(_M_AMD64) -#if (CUDA_VERSION >= 3020) && (!defined(CUDA_FORCE_API_VERSION) || (CUDA_FORCE_API_VERSION >= 3020)) -#define __CUVID_DEVPTR64 -#endif -#endif - -#if defined(__cplusplus) -extern "C" { -#endif /* __cplusplus */ - -typedef void *CUvideodecoder; -typedef struct _CUcontextlock_st *CUvideoctxlock; - -/*********************************************************************************/ -//! \enum cudaVideoCodec -//! Video codec enums -//! These enums are used in CUVIDDECODECREATEINFO and CUVIDDECODECAPS structures -/*********************************************************************************/ -typedef enum cudaVideoCodec_enum { - cudaVideoCodec_MPEG1=0, /**< MPEG1 */ - cudaVideoCodec_MPEG2, /**< MPEG2 */ - cudaVideoCodec_MPEG4, /**< MPEG4 */ - cudaVideoCodec_VC1, /**< VC1 */ - cudaVideoCodec_H264, /**< H264 */ - cudaVideoCodec_JPEG, /**< JPEG */ - cudaVideoCodec_H264_SVC, /**< H264-SVC */ - cudaVideoCodec_H264_MVC, /**< H264-MVC */ - cudaVideoCodec_HEVC, /**< HEVC */ - cudaVideoCodec_VP8, /**< VP8 */ - cudaVideoCodec_VP9, /**< VP9 */ - cudaVideoCodec_NumCodecs, /**< Max codecs */ - // Uncompressed YUV - cudaVideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), /**< Y,U,V (4:2:0) */ - cudaVideoCodec_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), /**< Y,V,U (4:2:0) */ - cudaVideoCodec_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), /**< Y,UV (4:2:0) */ - cudaVideoCodec_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), /**< YUYV/YUY2 (4:2:2) */ - cudaVideoCodec_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')) /**< UYVY (4:2:2) */ -} cudaVideoCodec; - -/*********************************************************************************/ -//! \enum cudaVideoSurfaceFormat -//! Video surface format enums used for output format of decoded output -//! These enums are used in CUVIDDECODECREATEINFO structure -/*********************************************************************************/ -typedef enum cudaVideoSurfaceFormat_enum { - cudaVideoSurfaceFormat_NV12=0, /**< Semi-Planar YUV [Y plane followed by interleaved UV plane] */ - cudaVideoSurfaceFormat_P016=1, /**< 16 bit Semi-Planar YUV [Y plane followed by interleaved UV plane]. - Can be used for 10 bit(6LSB bits 0), 12 bit (4LSB bits 0) */ - cudaVideoSurfaceFormat_YUV444=2, /**< Planar YUV [Y plane followed by U and V planes] */ - cudaVideoSurfaceFormat_YUV444_16Bit=3, /**< 16 bit Planar YUV [Y plane followed by U and V planes]. - Can be used for 10 bit(6LSB bits 0), 12 bit (4LSB bits 0) */ -} cudaVideoSurfaceFormat; - -/******************************************************************************************************************/ -//! \enum cudaVideoDeinterlaceMode -//! Deinterlacing mode enums -//! These enums are used in CUVIDDECODECREATEINFO structure -//! Use cudaVideoDeinterlaceMode_Weave for progressive content and for content that doesn't need deinterlacing -//! cudaVideoDeinterlaceMode_Adaptive needs more video memory than other DImodes -/******************************************************************************************************************/ -typedef enum cudaVideoDeinterlaceMode_enum { - cudaVideoDeinterlaceMode_Weave=0, /**< Weave both fields (no deinterlacing) */ - cudaVideoDeinterlaceMode_Bob, /**< Drop one field */ - cudaVideoDeinterlaceMode_Adaptive /**< Adaptive deinterlacing */ -} cudaVideoDeinterlaceMode; - -/**************************************************************************************************************/ -//! \enum cudaVideoChromaFormat -//! Chroma format enums -//! These enums are used in CUVIDDECODECREATEINFO and CUVIDDECODECAPS structures -/**************************************************************************************************************/ -typedef enum cudaVideoChromaFormat_enum { - cudaVideoChromaFormat_Monochrome=0, /**< MonoChrome */ - cudaVideoChromaFormat_420, /**< YUV 4:2:0 */ - cudaVideoChromaFormat_422, /**< YUV 4:2:2 */ - cudaVideoChromaFormat_444 /**< YUV 4:4:4 */ -} cudaVideoChromaFormat; - -/*************************************************************************************************************/ -//! \enum cudaVideoCreateFlags -//! Decoder flag enums to select preferred decode path -//! cudaVideoCreate_Default and cudaVideoCreate_PreferCUVID are most optimized, use these whenever possible -/*************************************************************************************************************/ -typedef enum cudaVideoCreateFlags_enum { - cudaVideoCreate_Default = 0x00, /**< Default operation mode: use dedicated video engines */ - cudaVideoCreate_PreferCUDA = 0x01, /**< Use CUDA-based decoder (requires valid vidLock object for multi-threading) */ - cudaVideoCreate_PreferDXVA = 0x02, /**< Go through DXVA internally if possible (requires D3D9 interop) */ - cudaVideoCreate_PreferCUVID = 0x04 /**< Use dedicated video engines directly */ -} cudaVideoCreateFlags; - - -/*************************************************************************/ -//! \enum cuvidDecodeStatus -//! Decode status enums -//! These enums are used in CUVIDGETDECODESTATUS structure -/*************************************************************************/ -typedef enum cuvidDecodeStatus_enum -{ - cuvidDecodeStatus_Invalid = 0, // Decode status is not valid - cuvidDecodeStatus_InProgress = 1, // Decode is in progress - cuvidDecodeStatus_Success = 2, // Decode is completed without any errors - // 3 to 7 enums are reserved for future use - cuvidDecodeStatus_Error = 8, // Decode is completed with an error (error is not concealed) - cuvidDecodeStatus_Error_Concealed = 9, // Decode is completed with an error and error is concealed -} cuvidDecodeStatus; - -/**************************************************************************************************************/ -//! \struct CUVIDDECODECAPS; -//! This structure is used in cuvidGetDecoderCaps API -/**************************************************************************************************************/ -typedef struct _CUVIDDECODECAPS -{ - cudaVideoCodec eCodecType; /**< IN: cudaVideoCodec_XXX */ - cudaVideoChromaFormat eChromaFormat; /**< IN: cudaVideoChromaFormat_XXX */ - unsigned int nBitDepthMinus8; /**< IN: The Value "BitDepth minus 8" */ - unsigned int reserved1[3]; /**< Reserved for future use - set to zero */ - - unsigned char bIsSupported; /**< OUT: 1 if codec supported, 0 if not supported */ - unsigned char reserved2[3]; /**< Reserved for future use - set to zero */ - unsigned int nMaxWidth; /**< OUT: Max supported coded width in pixels */ - unsigned int nMaxHeight; /**< OUT: Max supported coded height in pixels */ - unsigned int nMaxMBCount; /**< OUT: Max supported macroblock count - CodedWidth*CodedHeight/256 must be <= nMaxMBCount */ - unsigned short nMinWidth; /**< OUT: Min supported coded width in pixels */ - unsigned short nMinHeight; /**< OUT: Min supported coded height in pixels */ - unsigned int reserved3[11]; /**< Reserved for future use - set to zero */ -} CUVIDDECODECAPS; - -/**************************************************************************************************************/ -//! \struct CUVIDDECODECREATEINFO -//! This structure is used in cuvidCreateDecoder API -/**************************************************************************************************************/ -typedef struct _CUVIDDECODECREATEINFO -{ - unsigned long ulWidth; /**< IN: Coded sequence width in pixels */ - unsigned long ulHeight; /**< IN: Coded sequence height in pixels */ - unsigned long ulNumDecodeSurfaces; /**< IN: Maximum number of internal decode surfaces */ - cudaVideoCodec CodecType; /**< IN: cudaVideoCodec_XXX */ - cudaVideoChromaFormat ChromaFormat; /**< IN: cudaVideoChromaFormat_XXX */ - unsigned long ulCreationFlags; /**< IN: Decoder creation flags (cudaVideoCreateFlags_XXX) */ - unsigned long bitDepthMinus8; /**< IN: The value "BitDepth minus 8" */ - unsigned long ulIntraDecodeOnly; /**< IN: Set 1 only if video has all intra frames (default value is 0). This will - optimize video memory for Intra frames only decoding. The support is limited - to specific codecs - H264, HEVC, VP9, the flag will be ignored for codecs which - are not supported. However decoding might fail if the flag is enabled in case - of supported codecs for regular bit streams having P and/or B frames. */ - unsigned long ulMaxWidth; /**< IN: Coded sequence max width in pixels used with reconfigure Decoder */ - unsigned long ulMaxHeight; /**< IN: Coded sequence max height in pixels used with reconfigure Decoder */ - unsigned long Reserved1; /**< Reserved for future use - set to zero */ - /** - * IN: area of the frame that should be displayed - */ - struct { - short left; - short top; - short right; - short bottom; - } display_area; - - cudaVideoSurfaceFormat OutputFormat; /**< IN: cudaVideoSurfaceFormat_XXX */ - cudaVideoDeinterlaceMode DeinterlaceMode; /**< IN: cudaVideoDeinterlaceMode_XXX */ - unsigned long ulTargetWidth; /**< IN: Post-processed output width (Should be aligned to 2) */ - unsigned long ulTargetHeight; /**< IN: Post-processed output height (Should be aligned to 2) */ - unsigned long ulNumOutputSurfaces; /**< IN: Maximum number of output surfaces simultaneously mapped */ - CUvideoctxlock vidLock; /**< IN: If non-NULL, context lock used for synchronizing ownership of - the cuda context. Needed for cudaVideoCreate_PreferCUDA decode */ - /** - * IN: target rectangle in the output frame (for aspect ratio conversion) - * if a null rectangle is specified, {0,0,ulTargetWidth,ulTargetHeight} will be used - */ - struct { - short left; - short top; - short right; - short bottom; - } target_rect; - unsigned long Reserved2[5]; /**< Reserved for future use - set to zero */ -} CUVIDDECODECREATEINFO; - -/*********************************************************/ -//! \struct CUVIDH264DPBENTRY -//! H.264 DPB entry -//! This structure is used in CUVIDH264PICPARAMS structure -/*********************************************************/ -typedef struct _CUVIDH264DPBENTRY -{ - int PicIdx; /**< picture index of reference frame */ - int FrameIdx; /**< frame_num(short-term) or LongTermFrameIdx(long-term) */ - int is_long_term; /**< 0=short term reference, 1=long term reference */ - int not_existing; /**< non-existing reference frame (corresponding PicIdx should be set to -1) */ - int used_for_reference; /**< 0=unused, 1=top_field, 2=bottom_field, 3=both_fields */ - int FieldOrderCnt[2]; /**< field order count of top and bottom fields */ -} CUVIDH264DPBENTRY; - -/************************************************************/ -//! \struct CUVIDH264MVCEXT -//! H.264 MVC picture parameters ext -//! This structure is used in CUVIDH264PICPARAMS structure -/************************************************************/ -typedef struct _CUVIDH264MVCEXT -{ - int num_views_minus1; /**< Max number of coded views minus 1 in video : Range - 0 to 1023 */ - int view_id; /**< view identifier */ - unsigned char inter_view_flag; /**< 1 if used for inter-view prediction, 0 if not */ - unsigned char num_inter_view_refs_l0; /**< number of inter-view ref pics in RefPicList0 */ - unsigned char num_inter_view_refs_l1; /**< number of inter-view ref pics in RefPicList1 */ - unsigned char MVCReserved8Bits; /**< Reserved bits */ - int InterViewRefsL0[16]; /**< view id of the i-th view component for inter-view prediction in RefPicList0 */ - int InterViewRefsL1[16]; /**< view id of the i-th view component for inter-view prediction in RefPicList1 */ -} CUVIDH264MVCEXT; - -/*********************************************************/ -//! \struct CUVIDH264SVCEXT -//! H.264 SVC picture parameters ext -//! This structure is used in CUVIDH264PICPARAMS structure -/*********************************************************/ -typedef struct _CUVIDH264SVCEXT -{ - unsigned char profile_idc; - unsigned char level_idc; - unsigned char DQId; - unsigned char DQIdMax; - unsigned char disable_inter_layer_deblocking_filter_idc; - unsigned char ref_layer_chroma_phase_y_plus1; - signed char inter_layer_slice_alpha_c0_offset_div2; - signed char inter_layer_slice_beta_offset_div2; - - unsigned short DPBEntryValidFlag; - unsigned char inter_layer_deblocking_filter_control_present_flag; - unsigned char extended_spatial_scalability_idc; - unsigned char adaptive_tcoeff_level_prediction_flag; - unsigned char slice_header_restriction_flag; - unsigned char chroma_phase_x_plus1_flag; - unsigned char chroma_phase_y_plus1; - - unsigned char tcoeff_level_prediction_flag; - unsigned char constrained_intra_resampling_flag; - unsigned char ref_layer_chroma_phase_x_plus1_flag; - unsigned char store_ref_base_pic_flag; - unsigned char Reserved8BitsA; - unsigned char Reserved8BitsB; - - short scaled_ref_layer_left_offset; - short scaled_ref_layer_top_offset; - short scaled_ref_layer_right_offset; - short scaled_ref_layer_bottom_offset; - unsigned short Reserved16Bits; - struct _CUVIDPICPARAMS *pNextLayer; /**< Points to the picparams for the next layer to be decoded. - Linked list ends at the target layer. */ - int bRefBaseLayer; /**< whether to store ref base pic */ -} CUVIDH264SVCEXT; - -/******************************************************/ -//! \struct CUVIDH264PICPARAMS -//! H.264 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/******************************************************/ -typedef struct _CUVIDH264PICPARAMS -{ - // SPS - int log2_max_frame_num_minus4; - int pic_order_cnt_type; - int log2_max_pic_order_cnt_lsb_minus4; - int delta_pic_order_always_zero_flag; - int frame_mbs_only_flag; - int direct_8x8_inference_flag; - int num_ref_frames; // NOTE: shall meet level 4.1 restrictions - unsigned char residual_colour_transform_flag; - unsigned char bit_depth_luma_minus8; // Must be 0 (only 8-bit supported) - unsigned char bit_depth_chroma_minus8; // Must be 0 (only 8-bit supported) - unsigned char qpprime_y_zero_transform_bypass_flag; - // PPS - int entropy_coding_mode_flag; - int pic_order_present_flag; - int num_ref_idx_l0_active_minus1; - int num_ref_idx_l1_active_minus1; - int weighted_pred_flag; - int weighted_bipred_idc; - int pic_init_qp_minus26; - int deblocking_filter_control_present_flag; - int redundant_pic_cnt_present_flag; - int transform_8x8_mode_flag; - int MbaffFrameFlag; - int constrained_intra_pred_flag; - int chroma_qp_index_offset; - int second_chroma_qp_index_offset; - int ref_pic_flag; - int frame_num; - int CurrFieldOrderCnt[2]; - // DPB - CUVIDH264DPBENTRY dpb[16]; // List of reference frames within the DPB - // Quantization Matrices (raster-order) - unsigned char WeightScale4x4[6][16]; - unsigned char WeightScale8x8[2][64]; - // FMO/ASO - unsigned char fmo_aso_enable; - unsigned char num_slice_groups_minus1; - unsigned char slice_group_map_type; - signed char pic_init_qs_minus26; - unsigned int slice_group_change_rate_minus1; - union - { - unsigned long long slice_group_map_addr; - const unsigned char *pMb2SliceGroupMap; - } fmo; - unsigned int Reserved[12]; - // SVC/MVC - union - { - CUVIDH264MVCEXT mvcext; - CUVIDH264SVCEXT svcext; - }; -} CUVIDH264PICPARAMS; - - -/********************************************************/ -//! \struct CUVIDMPEG2PICPARAMS -//! MPEG-2 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/********************************************************/ -typedef struct _CUVIDMPEG2PICPARAMS -{ - int ForwardRefIdx; // Picture index of forward reference (P/B-frames) - int BackwardRefIdx; // Picture index of backward reference (B-frames) - int picture_coding_type; - int full_pel_forward_vector; - int full_pel_backward_vector; - int f_code[2][2]; - int intra_dc_precision; - int frame_pred_frame_dct; - int concealment_motion_vectors; - int q_scale_type; - int intra_vlc_format; - int alternate_scan; - int top_field_first; - // Quantization matrices (raster order) - unsigned char QuantMatrixIntra[64]; - unsigned char QuantMatrixInter[64]; -} CUVIDMPEG2PICPARAMS; - -// MPEG-4 has VOP types instead of Picture types -#define I_VOP 0 -#define P_VOP 1 -#define B_VOP 2 -#define S_VOP 3 - -/*******************************************************/ -//! \struct CUVIDMPEG4PICPARAMS -//! MPEG-4 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/*******************************************************/ -typedef struct _CUVIDMPEG4PICPARAMS -{ - int ForwardRefIdx; // Picture index of forward reference (P/B-frames) - int BackwardRefIdx; // Picture index of backward reference (B-frames) - // VOL - int video_object_layer_width; - int video_object_layer_height; - int vop_time_increment_bitcount; - int top_field_first; - int resync_marker_disable; - int quant_type; - int quarter_sample; - int short_video_header; - int divx_flags; - // VOP - int vop_coding_type; - int vop_coded; - int vop_rounding_type; - int alternate_vertical_scan_flag; - int interlaced; - int vop_fcode_forward; - int vop_fcode_backward; - int trd[2]; - int trb[2]; - // Quantization matrices (raster order) - unsigned char QuantMatrixIntra[64]; - unsigned char QuantMatrixInter[64]; - int gmc_enabled; -} CUVIDMPEG4PICPARAMS; - -/********************************************************/ -//! \struct CUVIDVC1PICPARAMS -//! VC1 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/********************************************************/ -typedef struct _CUVIDVC1PICPARAMS -{ - int ForwardRefIdx; /**< Picture index of forward reference (P/B-frames) */ - int BackwardRefIdx; /**< Picture index of backward reference (B-frames) */ - int FrameWidth; /**< Actual frame width */ - int FrameHeight; /**< Actual frame height */ - // PICTURE - int intra_pic_flag; /**< Set to 1 for I,BI frames */ - int ref_pic_flag; /**< Set to 1 for I,P frames */ - int progressive_fcm; /**< Progressive frame */ - // SEQUENCE - int profile; - int postprocflag; - int pulldown; - int interlace; - int tfcntrflag; - int finterpflag; - int psf; - int multires; - int syncmarker; - int rangered; - int maxbframes; - // ENTRYPOINT - int panscan_flag; - int refdist_flag; - int extended_mv; - int dquant; - int vstransform; - int loopfilter; - int fastuvmc; - int overlap; - int quantizer; - int extended_dmv; - int range_mapy_flag; - int range_mapy; - int range_mapuv_flag; - int range_mapuv; - int rangeredfrm; // range reduction state -} CUVIDVC1PICPARAMS; - -/***********************************************************/ -//! \struct CUVIDJPEGPICPARAMS -//! JPEG picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/***********************************************************/ -typedef struct _CUVIDJPEGPICPARAMS -{ - int Reserved; -} CUVIDJPEGPICPARAMS; - - -/*******************************************************/ -//! \struct CUVIDHEVCPICPARAMS -//! HEVC picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/*******************************************************/ -typedef struct _CUVIDHEVCPICPARAMS -{ - // sps - int pic_width_in_luma_samples; - int pic_height_in_luma_samples; - unsigned char log2_min_luma_coding_block_size_minus3; - unsigned char log2_diff_max_min_luma_coding_block_size; - unsigned char log2_min_transform_block_size_minus2; - unsigned char log2_diff_max_min_transform_block_size; - unsigned char pcm_enabled_flag; - unsigned char log2_min_pcm_luma_coding_block_size_minus3; - unsigned char log2_diff_max_min_pcm_luma_coding_block_size; - unsigned char pcm_sample_bit_depth_luma_minus1; - - unsigned char pcm_sample_bit_depth_chroma_minus1; - unsigned char pcm_loop_filter_disabled_flag; - unsigned char strong_intra_smoothing_enabled_flag; - unsigned char max_transform_hierarchy_depth_intra; - unsigned char max_transform_hierarchy_depth_inter; - unsigned char amp_enabled_flag; - unsigned char separate_colour_plane_flag; - unsigned char log2_max_pic_order_cnt_lsb_minus4; - - unsigned char num_short_term_ref_pic_sets; - unsigned char long_term_ref_pics_present_flag; - unsigned char num_long_term_ref_pics_sps; - unsigned char sps_temporal_mvp_enabled_flag; - unsigned char sample_adaptive_offset_enabled_flag; - unsigned char scaling_list_enable_flag; - unsigned char IrapPicFlag; - unsigned char IdrPicFlag; - - unsigned char bit_depth_luma_minus8; - unsigned char bit_depth_chroma_minus8; - //sps/pps extension fields - unsigned char log2_max_transform_skip_block_size_minus2; - unsigned char log2_sao_offset_scale_luma; - unsigned char log2_sao_offset_scale_chroma; - unsigned char high_precision_offsets_enabled_flag; - unsigned char reserved1[10]; - - // pps - unsigned char dependent_slice_segments_enabled_flag; - unsigned char slice_segment_header_extension_present_flag; - unsigned char sign_data_hiding_enabled_flag; - unsigned char cu_qp_delta_enabled_flag; - unsigned char diff_cu_qp_delta_depth; - signed char init_qp_minus26; - signed char pps_cb_qp_offset; - signed char pps_cr_qp_offset; - - unsigned char constrained_intra_pred_flag; - unsigned char weighted_pred_flag; - unsigned char weighted_bipred_flag; - unsigned char transform_skip_enabled_flag; - unsigned char transquant_bypass_enabled_flag; - unsigned char entropy_coding_sync_enabled_flag; - unsigned char log2_parallel_merge_level_minus2; - unsigned char num_extra_slice_header_bits; - - unsigned char loop_filter_across_tiles_enabled_flag; - unsigned char loop_filter_across_slices_enabled_flag; - unsigned char output_flag_present_flag; - unsigned char num_ref_idx_l0_default_active_minus1; - unsigned char num_ref_idx_l1_default_active_minus1; - unsigned char lists_modification_present_flag; - unsigned char cabac_init_present_flag; - unsigned char pps_slice_chroma_qp_offsets_present_flag; - - unsigned char deblocking_filter_override_enabled_flag; - unsigned char pps_deblocking_filter_disabled_flag; - signed char pps_beta_offset_div2; - signed char pps_tc_offset_div2; - unsigned char tiles_enabled_flag; - unsigned char uniform_spacing_flag; - unsigned char num_tile_columns_minus1; - unsigned char num_tile_rows_minus1; - - unsigned short column_width_minus1[21]; - unsigned short row_height_minus1[21]; - - // sps and pps extension HEVC-main 444 - unsigned char sps_range_extension_flag; - unsigned char transform_skip_rotation_enabled_flag; - unsigned char transform_skip_context_enabled_flag; - unsigned char implicit_rdpcm_enabled_flag; - - unsigned char explicit_rdpcm_enabled_flag; - unsigned char extended_precision_processing_flag; - unsigned char intra_smoothing_disabled_flag; - unsigned char persistent_rice_adaptation_enabled_flag; - - unsigned char cabac_bypass_alignment_enabled_flag; - unsigned char pps_range_extension_flag; - unsigned char cross_component_prediction_enabled_flag; - unsigned char chroma_qp_offset_list_enabled_flag; - - unsigned char diff_cu_chroma_qp_offset_depth; - unsigned char chroma_qp_offset_list_len_minus1; - signed char cb_qp_offset_list[6]; - - signed char cr_qp_offset_list[6]; - unsigned char reserved2[2]; - - unsigned int reserved3[8]; - - // RefPicSets - int NumBitsForShortTermRPSInSlice; - int NumDeltaPocsOfRefRpsIdx; - int NumPocTotalCurr; - int NumPocStCurrBefore; - int NumPocStCurrAfter; - int NumPocLtCurr; - int CurrPicOrderCntVal; - int RefPicIdx[16]; // [refpic] Indices of valid reference pictures (-1 if unused for reference) - int PicOrderCntVal[16]; // [refpic] - unsigned char IsLongTerm[16]; // [refpic] 0=not a long-term reference, 1=long-term reference - unsigned char RefPicSetStCurrBefore[8]; // [0..NumPocStCurrBefore-1] -> refpic (0..15) - unsigned char RefPicSetStCurrAfter[8]; // [0..NumPocStCurrAfter-1] -> refpic (0..15) - unsigned char RefPicSetLtCurr[8]; // [0..NumPocLtCurr-1] -> refpic (0..15) - unsigned char RefPicSetInterLayer0[8]; - unsigned char RefPicSetInterLayer1[8]; - unsigned int reserved4[12]; - - // scaling lists (diag order) - unsigned char ScalingList4x4[6][16]; // [matrixId][i] - unsigned char ScalingList8x8[6][64]; // [matrixId][i] - unsigned char ScalingList16x16[6][64]; // [matrixId][i] - unsigned char ScalingList32x32[2][64]; // [matrixId][i] - unsigned char ScalingListDCCoeff16x16[6]; // [matrixId] - unsigned char ScalingListDCCoeff32x32[2]; // [matrixId] -} CUVIDHEVCPICPARAMS; - - -/***********************************************************/ -//! \struct CUVIDVP8PICPARAMS -//! VP8 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/***********************************************************/ -typedef struct _CUVIDVP8PICPARAMS -{ - int width; - int height; - unsigned int first_partition_size; - //Frame Indexes - unsigned char LastRefIdx; - unsigned char GoldenRefIdx; - unsigned char AltRefIdx; - union { - struct { - unsigned char frame_type : 1; /**< 0 = KEYFRAME, 1 = INTERFRAME */ - unsigned char version : 3; - unsigned char show_frame : 1; - unsigned char update_mb_segmentation_data : 1; /**< Must be 0 if segmentation is not enabled */ - unsigned char Reserved2Bits : 2; - }vp8_frame_tag; - unsigned char wFrameTagFlags; - }; - unsigned char Reserved1[4]; - unsigned int Reserved2[3]; -} CUVIDVP8PICPARAMS; - -/***********************************************************/ -//! \struct CUVIDVP9PICPARAMS -//! VP9 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/***********************************************************/ -typedef struct _CUVIDVP9PICPARAMS -{ - unsigned int width; - unsigned int height; - - //Frame Indices - unsigned char LastRefIdx; - unsigned char GoldenRefIdx; - unsigned char AltRefIdx; - unsigned char colorSpace; - - unsigned short profile : 3; - unsigned short frameContextIdx : 2; - unsigned short frameType : 1; - unsigned short showFrame : 1; - unsigned short errorResilient : 1; - unsigned short frameParallelDecoding : 1; - unsigned short subSamplingX : 1; - unsigned short subSamplingY : 1; - unsigned short intraOnly : 1; - unsigned short allow_high_precision_mv : 1; - unsigned short refreshEntropyProbs : 1; - unsigned short reserved2Bits : 2; - - unsigned short reserved16Bits; - - unsigned char refFrameSignBias[4]; - - unsigned char bitDepthMinus8Luma; - unsigned char bitDepthMinus8Chroma; - unsigned char loopFilterLevel; - unsigned char loopFilterSharpness; - - unsigned char modeRefLfEnabled; - unsigned char log2_tile_columns; - unsigned char log2_tile_rows; - - unsigned char segmentEnabled : 1; - unsigned char segmentMapUpdate : 1; - unsigned char segmentMapTemporalUpdate : 1; - unsigned char segmentFeatureMode : 1; - unsigned char reserved4Bits : 4; - - - unsigned char segmentFeatureEnable[8][4]; - short segmentFeatureData[8][4]; - unsigned char mb_segment_tree_probs[7]; - unsigned char segment_pred_probs[3]; - unsigned char reservedSegment16Bits[2]; - - int qpYAc; - int qpYDc; - int qpChDc; - int qpChAc; - - unsigned int activeRefIdx[3]; - unsigned int resetFrameContext; - unsigned int mcomp_filter_type; - unsigned int mbRefLfDelta[4]; - unsigned int mbModeLfDelta[2]; - unsigned int frameTagSize; - unsigned int offsetToDctParts; - unsigned int reserved128Bits[4]; - -} CUVIDVP9PICPARAMS; - - -/******************************************************************************************/ -//! \struct CUVIDPICPARAMS -//! Picture parameters for decoding -//! This structure is used in cuvidDecodePicture API -//! IN for cuvidDecodePicture -/******************************************************************************************/ -typedef struct _CUVIDPICPARAMS -{ - int PicWidthInMbs; /**< IN: Coded frame size in macroblocks */ - int FrameHeightInMbs; /**< IN: Coded frame height in macroblocks */ - int CurrPicIdx; /**< IN: Output index of the current picture */ - int field_pic_flag; /**< IN: 0=frame picture, 1=field picture */ - int bottom_field_flag; /**< IN: 0=top field, 1=bottom field (ignored if field_pic_flag=0) */ - int second_field; /**< IN: Second field of a complementary field pair */ - // Bitstream data - unsigned int nBitstreamDataLen; /**< IN: Number of bytes in bitstream data buffer */ - const unsigned char *pBitstreamData; /**< IN: Ptr to bitstream data for this picture (slice-layer) */ - unsigned int nNumSlices; /**< IN: Number of slices in this picture */ - const unsigned int *pSliceDataOffsets; /**< IN: nNumSlices entries, contains offset of each slice within - the bitstream data buffer */ - int ref_pic_flag; /**< IN: This picture is a reference picture */ - int intra_pic_flag; /**< IN: This picture is entirely intra coded */ - unsigned int Reserved[30]; /**< Reserved for future use */ - // IN: Codec-specific data - union { - CUVIDMPEG2PICPARAMS mpeg2; /**< Also used for MPEG-1 */ - CUVIDH264PICPARAMS h264; - CUVIDVC1PICPARAMS vc1; - CUVIDMPEG4PICPARAMS mpeg4; - CUVIDJPEGPICPARAMS jpeg; - CUVIDHEVCPICPARAMS hevc; - CUVIDVP8PICPARAMS vp8; - CUVIDVP9PICPARAMS vp9; - unsigned int CodecReserved[1024]; - } CodecSpecific; -} CUVIDPICPARAMS; - - -/******************************************************/ -//! \struct CUVIDPROCPARAMS -//! Picture parameters for postprocessing -//! This structure is used in cuvidMapVideoFrame API -/******************************************************/ -typedef struct _CUVIDPROCPARAMS -{ - int progressive_frame; /**< IN: Input is progressive (deinterlace_mode will be ignored) */ - int second_field; /**< IN: Output the second field (ignored if deinterlace mode is Weave) */ - int top_field_first; /**< IN: Input frame is top field first (1st field is top, 2nd field is bottom) */ - int unpaired_field; /**< IN: Input only contains one field (2nd field is invalid) */ - // The fields below are used for raw YUV input - unsigned int reserved_flags; /**< Reserved for future use (set to zero) */ - unsigned int reserved_zero; /**< Reserved (set to zero) */ - unsigned long long raw_input_dptr; /**< IN: Input CUdeviceptr for raw YUV extensions */ - unsigned int raw_input_pitch; /**< IN: pitch in bytes of raw YUV input (should be aligned appropriately) */ - unsigned int raw_input_format; /**< IN: Input YUV format (cudaVideoCodec_enum) */ - unsigned long long raw_output_dptr; /**< IN: Output CUdeviceptr for raw YUV extensions */ - unsigned int raw_output_pitch; /**< IN: pitch in bytes of raw YUV output (should be aligned appropriately) */ - unsigned int Reserved1; /**< Reserved for future use (set to zero) */ - CUstream output_stream; /**< IN: stream object used by cuvidMapVideoFrame */ - unsigned int Reserved[46]; /**< Reserved for future use (set to zero) */ - void *Reserved2[2]; /**< Reserved for future use (set to zero) */ -} CUVIDPROCPARAMS; - -/*********************************************************************************************************/ -//! \struct CUVIDGETDECODESTATUS -//! Struct for reporting decode status. -//! This structure is used in cuvidGetDecodeStatus API. -/*********************************************************************************************************/ -typedef struct _CUVIDGETDECODESTATUS -{ - cuvidDecodeStatus decodeStatus; - unsigned int reserved[31]; - void *pReserved[8]; -} CUVIDGETDECODESTATUS; - -/****************************************************/ -//! \struct CUVIDRECONFIGUREDECODERINFO -//! Struct for decoder reset -//! This structure is used in cuvidReconfigureDecoder() API -/****************************************************/ -typedef struct _CUVIDRECONFIGUREDECODERINFO -{ - unsigned int ulWidth; /**< IN: Coded sequence width in pixels, MUST be < = ulMaxWidth defined at CUVIDDECODECREATEINFO */ - unsigned int ulHeight; /**< IN: Coded sequence height in pixels, MUST be < = ulMaxHeight defined at CUVIDDECODECREATEINFO */ - unsigned int ulTargetWidth; /**< IN: Post processed output width */ - unsigned int ulTargetHeight; /**< IN: Post Processed output height */ - unsigned int ulNumDecodeSurfaces; /**< IN: Maximum number of internal decode surfaces */ - unsigned int reserved1[12]; /**< Reserved for future use. Set to Zero */ - /** - * IN: Area of frame to be displayed. Use-case : Source Cropping - */ - struct { - short left; - short top; - short right; - short bottom; - } display_area; - /** - * IN: Target Rectangle in the OutputFrame. Use-case : Aspect ratio Conversion - */ - struct { - short left; - short top; - short right; - short bottom; - } target_rect; - unsigned int reserved2[11]; /**< Reserved for future use. Set to Zero */ -} CUVIDRECONFIGUREDECODERINFO; - - -/***********************************************************************************************************/ -//! VIDEO_DECODER -//! -//! In order to minimize decode latencies, there should be always at least 2 pictures in the decode -//! queue at any time, in order to make sure that all decode engines are always busy. -//! -//! Overall data flow: -//! - cuvidGetDecoderCaps(...) -//! - cuvidCreateDecoder(...) -//! - For each picture: -//! + cuvidDecodePicture(N) -//! + cuvidMapVideoFrame(N-4) -//! + do some processing in cuda -//! + cuvidUnmapVideoFrame(N-4) -//! + cuvidDecodePicture(N+1) -//! + cuvidMapVideoFrame(N-3) -//! + ... -//! - cuvidDestroyDecoder(...) -//! -//! NOTE: -//! - When the cuda context is created from a D3D device, the D3D device must also be created -//! with the D3DCREATE_MULTITHREADED flag. -//! - There is a limit to how many pictures can be mapped simultaneously (ulNumOutputSurfaces) -//! - cuvidDecodePicture may block the calling thread if there are too many pictures pending -//! in the decode queue -/***********************************************************************************************************/ - - -/**********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidGetDecoderCaps(CUVIDDECODECAPS *pdc) -//! Queries decode capabilities of NVDEC-HW based on CodecType, ChromaFormat and BitDepthMinus8 parameters. -//! 1. Application fills IN parameters CodecType, ChromaFormat and BitDepthMinus8 of CUVIDDECODECAPS structure -//! 2. On calling cuvidGetDecoderCaps, driver fills OUT parameters if the IN parameters are supported -//! If IN parameters passed to the driver are not supported by NVDEC-HW, then all OUT params are set to 0. -//! E.g. on Geforce GTX 960: -//! App fills - eCodecType = cudaVideoCodec_H264; eChromaFormat = cudaVideoChromaFormat_420; nBitDepthMinus8 = 0; -//! Given IN parameters are supported, hence driver fills: bIsSupported = 1; nMinWidth = 48; nMinHeight = 16; -//! nMaxWidth = 4096; nMaxHeight = 4096; nMaxMBCount = 65536; -//! CodedWidth*CodedHeight/256 must be less than or equal to nMaxMBCount -/**********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidGetDecoderCaps(CUVIDDECODECAPS *pdc); - -/*****************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCreateDecoder(CUvideodecoder *phDecoder, CUVIDDECODECREATEINFO *pdci) -//! Create the decoder object based on pdci. A handle to the created decoder is returned -/*****************************************************************************************************/ -extern CUresult CUDAAPI cuvidCreateDecoder(CUvideodecoder *phDecoder, CUVIDDECODECREATEINFO *pdci); - -/*****************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder hDecoder) -//! Destroy the decoder object -/*****************************************************************************************************/ -extern CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder hDecoder); - -/*****************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidDecodePicture(CUvideodecoder hDecoder, CUVIDPICPARAMS *pPicParams) -//! Decode a single picture (field or frame) -//! Kicks off HW decoding -/*****************************************************************************************************/ -extern CUresult CUDAAPI cuvidDecodePicture(CUvideodecoder hDecoder, CUVIDPICPARAMS *pPicParams); - -/************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidGetDecodeStatus(CUvideodecoder hDecoder, int nPicIdx); -//! Get the decode status for frame corresponding to nPicIdx -/************************************************************************************************************/ -extern CUresult CUDAAPI cuvidGetDecodeStatus(CUvideodecoder hDecoder, int nPicIdx, CUVIDGETDECODESTATUS* pDecodeStatus); - -/*********************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidReconfigureDecoder(CUvideodecoder hDecoder, CUVIDRECONFIGUREDECODERINFO *pDecReconfigParams) -//! Used to reuse single decoder for multiple clips. Currently supports resolution change, resize params, display area -//! params, target area params change for same codec. Must be called during CUVIDPARSERPARAMS::pfnSequenceCallback -/*********************************************************************************************************/ -extern CUresult CUDAAPI cuvidReconfigureDecoder(CUvideodecoder hDecoder, CUVIDRECONFIGUREDECODERINFO *pDecReconfigParams); - - -#if !defined(__CUVID_DEVPTR64) || defined(__CUVID_INTERNAL) -/************************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidMapVideoFrame(CUvideodecoder hDecoder, int nPicIdx, unsigned int *pDevPtr, -//! unsigned int *pPitch, CUVIDPROCPARAMS *pVPP); -//! Post-process and map video frame corresponding to nPicIdx for use in cuda. Returns cuda device pointer and associated -//! pitch of the video frame -/************************************************************************************************************************/ -extern CUresult CUDAAPI cuvidMapVideoFrame(CUvideodecoder hDecoder, int nPicIdx, - unsigned int *pDevPtr, unsigned int *pPitch, - CUVIDPROCPARAMS *pVPP); - -/*****************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidUnmapVideoFrame(CUvideodecoder hDecoder, unsigned int DevPtr) -//! Unmap a previously mapped video frame -/*****************************************************************************************************/ -extern CUresult CUDAAPI cuvidUnmapVideoFrame(CUvideodecoder hDecoder, unsigned int DevPtr); -#endif - -#if defined(_WIN64) || defined(__LP64__) || defined(__x86_64) || defined(AMD64) || defined(_M_AMD64) -/****************************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidMapVideoFrame64(CUvideodecoder hDecoder, int nPicIdx, unsigned long long *pDevPtr, -//! unsigned int * pPitch, CUVIDPROCPARAMS *pVPP); -//! Post-process and map video frame corresponding to nPicIdx for use in cuda. Returns cuda device pointer and associated -//! pitch of the video frame -/****************************************************************************************************************************/ -extern CUresult CUDAAPI cuvidMapVideoFrame64(CUvideodecoder hDecoder, int nPicIdx, unsigned long long *pDevPtr, - unsigned int *pPitch, CUVIDPROCPARAMS *pVPP); - -/**************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidUnmapVideoFrame64(CUvideodecoder hDecoder, unsigned long long DevPtr); -//! Unmap a previously mapped video frame -/**************************************************************************************************/ -extern CUresult CUDAAPI cuvidUnmapVideoFrame64(CUvideodecoder hDecoder, unsigned long long DevPtr); - -#if defined(__CUVID_DEVPTR64) && !defined(__CUVID_INTERNAL) -#define cuvidMapVideoFrame cuvidMapVideoFrame64 -#define cuvidUnmapVideoFrame cuvidUnmapVideoFrame64 -#endif -#endif - - -/********************************************************************************************************************/ -//! -//! Context-locking: to facilitate multi-threaded implementations, the following 4 functions -//! provide a simple mutex-style host synchronization. If a non-NULL context is specified -//! in CUVIDDECODECREATEINFO, the codec library will acquire the mutex associated with the given -//! context before making any cuda calls. -//! A multi-threaded application could create a lock associated with a context handle so that -//! multiple threads can safely share the same cuda context: -//! - use cuCtxPopCurrent immediately after context creation in order to create a 'floating' context -//! that can be passed to cuvidCtxLockCreate. -//! - When using a floating context, all cuda calls should only be made within a cuvidCtxLock/cuvidCtxUnlock section. -//! -//! NOTE: This is a safer alternative to cuCtxPushCurrent and cuCtxPopCurrent, and is not related to video -//! decoder in any way (implemented as a critical section associated with cuCtx{Push|Pop}Current calls). -/********************************************************************************************************************/ - -/********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCtxLockCreate(CUvideoctxlock *pLock, CUcontext ctx) -//! This API is used to create CtxLock object -/********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidCtxLockCreate(CUvideoctxlock *pLock, CUcontext ctx); - -/********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCtxLockDestroy(CUvideoctxlock lck) -//! This API is used to free CtxLock object -/********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidCtxLockDestroy(CUvideoctxlock lck); - -/********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCtxLock(CUvideoctxlock lck, unsigned int reserved_flags) -//! This API is used to acquire ctxlock -/********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidCtxLock(CUvideoctxlock lck, unsigned int reserved_flags); - -/********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCtxUnlock(CUvideoctxlock lck, unsigned int reserved_flags) -//! This API is used to release ctxlock -/********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidCtxUnlock(CUvideoctxlock lck, unsigned int reserved_flags); - -/**********************************************************************************************/ - - -#if defined(__cplusplus) -} -// Auto-lock helper for C++ applications -class CCtxAutoLock -{ -private: - CUvideoctxlock m_ctx; -public: - CCtxAutoLock(CUvideoctxlock ctx):m_ctx(ctx) { cuvidCtxLock(m_ctx,0); } - ~CCtxAutoLock() { cuvidCtxUnlock(m_ctx,0); } -}; -#endif /* __cplusplus */ - -#endif // __CUDA_VIDEO_H__ - diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/include/nvEncodeAPI.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/include/nvEncodeAPI.h deleted file mode 100644 index 7073f1d135f132a9a14321fddf5253e49c4a7215..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/include/nvEncodeAPI.h +++ /dev/null @@ -1,3484 +0,0 @@ -/* - * This copyright notice applies to this header file only: - * - * Copyright (c) 2010-2019 NVIDIA Corporation - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the software, and to permit persons to whom the - * software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -/** - * \file nvEncodeAPI.h - * NVIDIA GPUs - beginning with the Kepler generation - contain a hardware-based encoder - * (referred to as NVENC) which provides fully-accelerated hardware-based video encoding. - * NvEncodeAPI provides the interface for NVIDIA video encoder (NVENC). - * \date 2011-2018 - * This file contains the interface constants, structure definitions and function prototypes. - */ - -#ifndef _NV_ENCODEAPI_H_ -#define _NV_ENCODEAPI_H_ - -#include <stdlib.h> - -#ifdef _WIN32 -#include <windows.h> -#endif - -#ifdef _MSC_VER -#ifndef _STDINT -typedef __int32 int32_t; -typedef unsigned __int32 uint32_t; -typedef __int64 int64_t; -typedef unsigned __int64 uint64_t; -typedef signed char int8_t; -typedef unsigned char uint8_t; -typedef short int16_t; -typedef unsigned short uint16_t; -#endif -#else -#include <stdint.h> -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * \addtogroup ENCODER_STRUCTURE NvEncodeAPI Data structures - * @{ - */ - -#ifdef _WIN32 -#define NVENCAPI __stdcall -typedef RECT NVENC_RECT; -#else -#define NVENCAPI -// ========================================================================================= -#ifndef GUID -/*! - * \struct GUID - * Abstracts the GUID structure for non-windows platforms. - */ -// ========================================================================================= -typedef struct -{ - uint32_t Data1; /**< [in]: Specifies the first 8 hexadecimal digits of the GUID. */ - uint16_t Data2; /**< [in]: Specifies the first group of 4 hexadecimal digits. */ - uint16_t Data3; /**< [in]: Specifies the second group of 4 hexadecimal digits. */ - uint8_t Data4[8]; /**< [in]: Array of 8 bytes. The first 2 bytes contain the third group of 4 hexadecimal digits. - The remaining 6 bytes contain the final 12 hexadecimal digits. */ -} GUID; -#endif // GUID - -/** - * \struct _NVENC_RECT - * Defines a Rectangle. Used in ::NV_ENC_PREPROCESS_FRAME. - */ -typedef struct _NVENC_RECT -{ - uint32_t left; /**< [in]: X coordinate of the upper left corner of rectangular area to be specified. */ - uint32_t top; /**< [in]: Y coordinate of the upper left corner of the rectangular area to be specified. */ - uint32_t right; /**< [in]: X coordinate of the bottom right corner of the rectangular area to be specified. */ - uint32_t bottom; /**< [in]: Y coordinate of the bottom right corner of the rectangular area to be specified. */ -} NVENC_RECT; - -#endif // _WIN32 - -/** @} */ /* End of GUID and NVENC_RECT structure grouping*/ - -typedef void* NV_ENC_INPUT_PTR; /**< NVENCODE API input buffer */ -typedef void* NV_ENC_OUTPUT_PTR; /**< NVENCODE API output buffer*/ -typedef void* NV_ENC_REGISTERED_PTR; /**< A Resource that has been registered with NVENCODE API*/ - -#define NVENCAPI_MAJOR_VERSION 9 -#define NVENCAPI_MINOR_VERSION 0 - -#define NVENCAPI_VERSION (NVENCAPI_MAJOR_VERSION | (NVENCAPI_MINOR_VERSION << 24)) - -/** - * Macro to generate per-structure version for use with API. - */ -#define NVENCAPI_STRUCT_VERSION(ver) ((uint32_t)NVENCAPI_VERSION | ((ver)<<16) | (0x7 << 28)) - - -#define NVENC_INFINITE_GOPLENGTH 0xffffffff - -#define NV_MAX_SEQ_HDR_LEN (512) - -// ========================================================================================= -// Encode Codec GUIDS supported by the NvEncodeAPI interface. -// ========================================================================================= - -// {6BC82762-4E63-4ca4-AA85-1E50F321F6BF} -static const GUID NV_ENC_CODEC_H264_GUID = -{ 0x6bc82762, 0x4e63, 0x4ca4, { 0xaa, 0x85, 0x1e, 0x50, 0xf3, 0x21, 0xf6, 0xbf } }; - -// {790CDC88-4522-4d7b-9425-BDA9975F7603} -static const GUID NV_ENC_CODEC_HEVC_GUID = -{ 0x790cdc88, 0x4522, 0x4d7b, { 0x94, 0x25, 0xbd, 0xa9, 0x97, 0x5f, 0x76, 0x3 } }; - - - -// ========================================================================================= -// * Encode Profile GUIDS supported by the NvEncodeAPI interface. -// ========================================================================================= - -// {BFD6F8E7-233C-4341-8B3E-4818523803F4} -static const GUID NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID = -{ 0xbfd6f8e7, 0x233c, 0x4341, { 0x8b, 0x3e, 0x48, 0x18, 0x52, 0x38, 0x3, 0xf4 } }; - -// {0727BCAA-78C4-4c83-8C2F-EF3DFF267C6A} -static const GUID NV_ENC_H264_PROFILE_BASELINE_GUID = -{ 0x727bcaa, 0x78c4, 0x4c83, { 0x8c, 0x2f, 0xef, 0x3d, 0xff, 0x26, 0x7c, 0x6a } }; - -// {60B5C1D4-67FE-4790-94D5-C4726D7B6E6D} -static const GUID NV_ENC_H264_PROFILE_MAIN_GUID = -{ 0x60b5c1d4, 0x67fe, 0x4790, { 0x94, 0xd5, 0xc4, 0x72, 0x6d, 0x7b, 0x6e, 0x6d } }; - -// {E7CBC309-4F7A-4b89-AF2A-D537C92BE310} -static const GUID NV_ENC_H264_PROFILE_HIGH_GUID = -{ 0xe7cbc309, 0x4f7a, 0x4b89, { 0xaf, 0x2a, 0xd5, 0x37, 0xc9, 0x2b, 0xe3, 0x10 } }; - -// {7AC663CB-A598-4960-B844-339B261A7D52} -static const GUID NV_ENC_H264_PROFILE_HIGH_444_GUID = -{ 0x7ac663cb, 0xa598, 0x4960, { 0xb8, 0x44, 0x33, 0x9b, 0x26, 0x1a, 0x7d, 0x52 } }; - -// {40847BF5-33F7-4601-9084-E8FE3C1DB8B7} -static const GUID NV_ENC_H264_PROFILE_STEREO_GUID = -{ 0x40847bf5, 0x33f7, 0x4601, { 0x90, 0x84, 0xe8, 0xfe, 0x3c, 0x1d, 0xb8, 0xb7 } }; - -// {CE788D20-AAA9-4318-92BB-AC7E858C8D36} -static const GUID NV_ENC_H264_PROFILE_SVC_TEMPORAL_SCALABILTY = -{ 0xce788d20, 0xaaa9, 0x4318, { 0x92, 0xbb, 0xac, 0x7e, 0x85, 0x8c, 0x8d, 0x36 } }; - -// {B405AFAC-F32B-417B-89C4-9ABEED3E5978} -static const GUID NV_ENC_H264_PROFILE_PROGRESSIVE_HIGH_GUID = -{ 0xb405afac, 0xf32b, 0x417b, { 0x89, 0xc4, 0x9a, 0xbe, 0xed, 0x3e, 0x59, 0x78 } }; - -// {AEC1BD87-E85B-48f2-84C3-98BCA6285072} -static const GUID NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID = -{ 0xaec1bd87, 0xe85b, 0x48f2, { 0x84, 0xc3, 0x98, 0xbc, 0xa6, 0x28, 0x50, 0x72 } }; - -// {B514C39A-B55B-40fa-878F-F1253B4DFDEC} -static const GUID NV_ENC_HEVC_PROFILE_MAIN_GUID = -{ 0xb514c39a, 0xb55b, 0x40fa, { 0x87, 0x8f, 0xf1, 0x25, 0x3b, 0x4d, 0xfd, 0xec } }; - -// {fa4d2b6c-3a5b-411a-8018-0a3f5e3c9be5} -static const GUID NV_ENC_HEVC_PROFILE_MAIN10_GUID = -{ 0xfa4d2b6c, 0x3a5b, 0x411a, { 0x80, 0x18, 0x0a, 0x3f, 0x5e, 0x3c, 0x9b, 0xe5 } }; - -// For HEVC Main 444 8 bit and HEVC Main 444 10 bit profiles only -// {51ec32b5-1b4c-453c-9cbd-b616bd621341} -static const GUID NV_ENC_HEVC_PROFILE_FREXT_GUID = -{ 0x51ec32b5, 0x1b4c, 0x453c, { 0x9c, 0xbd, 0xb6, 0x16, 0xbd, 0x62, 0x13, 0x41 } }; - -// ========================================================================================= -// * Preset GUIDS supported by the NvEncodeAPI interface. -// ========================================================================================= -// {B2DFB705-4EBD-4C49-9B5F-24A777D3E587} -static const GUID NV_ENC_PRESET_DEFAULT_GUID = -{ 0xb2dfb705, 0x4ebd, 0x4c49, { 0x9b, 0x5f, 0x24, 0xa7, 0x77, 0xd3, 0xe5, 0x87 } }; - -// {60E4C59F-E846-4484-A56D-CD45BE9FDDF6} -static const GUID NV_ENC_PRESET_HP_GUID = -{ 0x60e4c59f, 0xe846, 0x4484, { 0xa5, 0x6d, 0xcd, 0x45, 0xbe, 0x9f, 0xdd, 0xf6 } }; - -// {34DBA71D-A77B-4B8F-9C3E-B6D5DA24C012} -static const GUID NV_ENC_PRESET_HQ_GUID = -{ 0x34dba71d, 0xa77b, 0x4b8f, { 0x9c, 0x3e, 0xb6, 0xd5, 0xda, 0x24, 0xc0, 0x12 } }; - -// {82E3E450-BDBB-4e40-989C-82A90DF9EF32} -static const GUID NV_ENC_PRESET_BD_GUID = -{ 0x82e3e450, 0xbdbb, 0x4e40, { 0x98, 0x9c, 0x82, 0xa9, 0xd, 0xf9, 0xef, 0x32 } }; - -// {49DF21C5-6DFA-4feb-9787-6ACC9EFFB726} -static const GUID NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID = -{ 0x49df21c5, 0x6dfa, 0x4feb, { 0x97, 0x87, 0x6a, 0xcc, 0x9e, 0xff, 0xb7, 0x26 } }; - -// {C5F733B9-EA97-4cf9-BEC2-BF78A74FD105} -static const GUID NV_ENC_PRESET_LOW_LATENCY_HQ_GUID = -{ 0xc5f733b9, 0xea97, 0x4cf9, { 0xbe, 0xc2, 0xbf, 0x78, 0xa7, 0x4f, 0xd1, 0x5 } }; - -// {67082A44-4BAD-48FA-98EA-93056D150A58} -static const GUID NV_ENC_PRESET_LOW_LATENCY_HP_GUID = -{ 0x67082a44, 0x4bad, 0x48fa, { 0x98, 0xea, 0x93, 0x5, 0x6d, 0x15, 0xa, 0x58 } }; - -// {D5BFB716-C604-44e7-9BB8-DEA5510FC3AC} -static const GUID NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID = -{ 0xd5bfb716, 0xc604, 0x44e7, { 0x9b, 0xb8, 0xde, 0xa5, 0x51, 0xf, 0xc3, 0xac } }; - -// {149998E7-2364-411d-82EF-179888093409} -static const GUID NV_ENC_PRESET_LOSSLESS_HP_GUID = -{ 0x149998e7, 0x2364, 0x411d, { 0x82, 0xef, 0x17, 0x98, 0x88, 0x9, 0x34, 0x9 } }; - -/** - * \addtogroup ENCODER_STRUCTURE NvEncodeAPI Data structures - * @{ - */ - -/** - * Input frame encode modes - */ -typedef enum _NV_ENC_PARAMS_FRAME_FIELD_MODE -{ - NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME = 0x01, /**< Frame mode */ - NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD = 0x02, /**< Field mode */ - NV_ENC_PARAMS_FRAME_FIELD_MODE_MBAFF = 0x03 /**< MB adaptive frame/field */ -} NV_ENC_PARAMS_FRAME_FIELD_MODE; - -/** - * Rate Control Modes - */ -typedef enum _NV_ENC_PARAMS_RC_MODE -{ - NV_ENC_PARAMS_RC_CONSTQP = 0x0, /**< Constant QP mode */ - NV_ENC_PARAMS_RC_VBR = 0x1, /**< Variable bitrate mode */ - NV_ENC_PARAMS_RC_CBR = 0x2, /**< Constant bitrate mode */ - NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ = 0x8, /**< low-delay CBR, high quality */ - NV_ENC_PARAMS_RC_CBR_HQ = 0x10, /**< CBR, high quality (slower) */ - NV_ENC_PARAMS_RC_VBR_HQ = 0x20 /**< VBR, high quality (slower) */ -} NV_ENC_PARAMS_RC_MODE; - -/** - * Emphasis Levels - */ -typedef enum _NV_ENC_EMPHASIS_MAP_LEVEL -{ - NV_ENC_EMPHASIS_MAP_LEVEL_0 = 0x0, /**< Emphasis Map Level 0, for zero Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_1 = 0x1, /**< Emphasis Map Level 1, for very low Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_2 = 0x2, /**< Emphasis Map Level 2, for low Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_3 = 0x3, /**< Emphasis Map Level 3, for medium Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_4 = 0x4, /**< Emphasis Map Level 4, for high Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_5 = 0x5 /**< Emphasis Map Level 5, for very high Delta QP value */ -} NV_ENC_EMPHASIS_MAP_LEVEL; - -/** - * QP MAP MODE - */ -typedef enum _NV_ENC_QP_MAP_MODE -{ - NV_ENC_QP_MAP_DISABLED = 0x0, /**< Value in NV_ENC_PIC_PARAMS::qpDeltaMap have no effect. */ - NV_ENC_QP_MAP_EMPHASIS = 0x1, /**< Value in NV_ENC_PIC_PARAMS::qpDeltaMap will be treated as Empasis level. Currently this is only supported for H264 */ - NV_ENC_QP_MAP_DELTA = 0x2, /**< Value in NV_ENC_PIC_PARAMS::qpDeltaMap will be treated as QP delta map. */ - NV_ENC_QP_MAP = 0x3, /**< Currently This is not supported. Value in NV_ENC_PIC_PARAMS::qpDeltaMap will be treated as QP value. */ -} NV_ENC_QP_MAP_MODE; - -#define NV_ENC_PARAMS_RC_VBR_MINQP (NV_ENC_PARAMS_RC_MODE)0x4 /**< Deprecated */ -#define NV_ENC_PARAMS_RC_2_PASS_QUALITY NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ /**< Deprecated */ -#define NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP NV_ENC_PARAMS_RC_CBR_HQ /**< Deprecated */ -#define NV_ENC_PARAMS_RC_2_PASS_VBR NV_ENC_PARAMS_RC_VBR_HQ /**< Deprecated */ -#define NV_ENC_PARAMS_RC_CBR2 NV_ENC_PARAMS_RC_CBR /**< Deprecated */ - -/** - * Input picture structure - */ -typedef enum _NV_ENC_PIC_STRUCT -{ - NV_ENC_PIC_STRUCT_FRAME = 0x01, /**< Progressive frame */ - NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM = 0x02, /**< Field encoding top field first */ - NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP = 0x03 /**< Field encoding bottom field first */ -} NV_ENC_PIC_STRUCT; - -/** - * Input picture type - */ -typedef enum _NV_ENC_PIC_TYPE -{ - NV_ENC_PIC_TYPE_P = 0x0, /**< Forward predicted */ - NV_ENC_PIC_TYPE_B = 0x01, /**< Bi-directionally predicted picture */ - NV_ENC_PIC_TYPE_I = 0x02, /**< Intra predicted picture */ - NV_ENC_PIC_TYPE_IDR = 0x03, /**< IDR picture */ - NV_ENC_PIC_TYPE_BI = 0x04, /**< Bi-directionally predicted with only Intra MBs */ - NV_ENC_PIC_TYPE_SKIPPED = 0x05, /**< Picture is skipped */ - NV_ENC_PIC_TYPE_INTRA_REFRESH = 0x06, /**< First picture in intra refresh cycle */ - NV_ENC_PIC_TYPE_NONREF_P = 0x07, /**< Non reference P picture */ - NV_ENC_PIC_TYPE_UNKNOWN = 0xFF /**< Picture type unknown */ -} NV_ENC_PIC_TYPE; - -/** - * Motion vector precisions - */ -typedef enum _NV_ENC_MV_PRECISION -{ - NV_ENC_MV_PRECISION_DEFAULT = 0x0, /**<Driver selects QuarterPel motion vector precision by default*/ - NV_ENC_MV_PRECISION_FULL_PEL = 0x01, /**< FullPel motion vector precision */ - NV_ENC_MV_PRECISION_HALF_PEL = 0x02, /**< HalfPel motion vector precision */ - NV_ENC_MV_PRECISION_QUARTER_PEL = 0x03 /**< QuarterPel motion vector precision */ -} NV_ENC_MV_PRECISION; - - -/** - * Input buffer formats - */ -typedef enum _NV_ENC_BUFFER_FORMAT -{ - NV_ENC_BUFFER_FORMAT_UNDEFINED = 0x00000000, /**< Undefined buffer format */ - - NV_ENC_BUFFER_FORMAT_NV12 = 0x00000001, /**< Semi-Planar YUV [Y plane followed by interleaved UV plane] */ - NV_ENC_BUFFER_FORMAT_YV12 = 0x00000010, /**< Planar YUV [Y plane followed by V and U planes] */ - NV_ENC_BUFFER_FORMAT_IYUV = 0x00000100, /**< Planar YUV [Y plane followed by U and V planes] */ - NV_ENC_BUFFER_FORMAT_YUV444 = 0x00001000, /**< Planar YUV [Y plane followed by U and V planes] */ - NV_ENC_BUFFER_FORMAT_YUV420_10BIT = 0x00010000, /**< 10 bit Semi-Planar YUV [Y plane followed by interleaved UV plane]. Each pixel of size 2 bytes. Most Significant 10 bits contain pixel data. */ - NV_ENC_BUFFER_FORMAT_YUV444_10BIT = 0x00100000, /**< 10 bit Planar YUV444 [Y plane followed by U and V planes]. Each pixel of size 2 bytes. Most Significant 10 bits contain pixel data. */ - NV_ENC_BUFFER_FORMAT_ARGB = 0x01000000, /**< 8 bit Packed A8R8G8B8. This is a word-ordered format - where a pixel is represented by a 32-bit word with B - in the lowest 8 bits, G in the next 8 bits, R in the - 8 bits after that and A in the highest 8 bits. */ - NV_ENC_BUFFER_FORMAT_ARGB10 = 0x02000000, /**< 10 bit Packed A2R10G10B10. This is a word-ordered format - where a pixel is represented by a 32-bit word with B - in the lowest 10 bits, G in the next 10 bits, R in the - 10 bits after that and A in the highest 2 bits. */ - NV_ENC_BUFFER_FORMAT_AYUV = 0x04000000, /**< 8 bit Packed A8Y8U8V8. This is a word-ordered format - where a pixel is represented by a 32-bit word with V - in the lowest 8 bits, U in the next 8 bits, Y in the - 8 bits after that and A in the highest 8 bits. */ - NV_ENC_BUFFER_FORMAT_ABGR = 0x10000000, /**< 8 bit Packed A8B8G8R8. This is a word-ordered format - where a pixel is represented by a 32-bit word with R - in the lowest 8 bits, G in the next 8 bits, B in the - 8 bits after that and A in the highest 8 bits. */ - NV_ENC_BUFFER_FORMAT_ABGR10 = 0x20000000, /**< 10 bit Packed A2B10G10R10. This is a word-ordered format - where a pixel is represented by a 32-bit word with R - in the lowest 10 bits, G in the next 10 bits, B in the - 10 bits after that and A in the highest 2 bits. */ - NV_ENC_BUFFER_FORMAT_U8 = 0x40000000, /**< Buffer format representing one-dimensional buffer. - This format should be used only when registering the - resource as output buffer, which will be used to write - the encoded bit stream or H.264 ME only mode output. */ -} NV_ENC_BUFFER_FORMAT; - -#define NV_ENC_BUFFER_FORMAT_NV12_PL NV_ENC_BUFFER_FORMAT_NV12 -#define NV_ENC_BUFFER_FORMAT_YV12_PL NV_ENC_BUFFER_FORMAT_YV12 -#define NV_ENC_BUFFER_FORMAT_IYUV_PL NV_ENC_BUFFER_FORMAT_IYUV -#define NV_ENC_BUFFER_FORMAT_YUV444_PL NV_ENC_BUFFER_FORMAT_YUV444 - -/** - * Encoding levels - */ -typedef enum _NV_ENC_LEVEL -{ - NV_ENC_LEVEL_AUTOSELECT = 0, - - NV_ENC_LEVEL_H264_1 = 10, - NV_ENC_LEVEL_H264_1b = 9, - NV_ENC_LEVEL_H264_11 = 11, - NV_ENC_LEVEL_H264_12 = 12, - NV_ENC_LEVEL_H264_13 = 13, - NV_ENC_LEVEL_H264_2 = 20, - NV_ENC_LEVEL_H264_21 = 21, - NV_ENC_LEVEL_H264_22 = 22, - NV_ENC_LEVEL_H264_3 = 30, - NV_ENC_LEVEL_H264_31 = 31, - NV_ENC_LEVEL_H264_32 = 32, - NV_ENC_LEVEL_H264_4 = 40, - NV_ENC_LEVEL_H264_41 = 41, - NV_ENC_LEVEL_H264_42 = 42, - NV_ENC_LEVEL_H264_5 = 50, - NV_ENC_LEVEL_H264_51 = 51, - NV_ENC_LEVEL_H264_52 = 52, - - - NV_ENC_LEVEL_HEVC_1 = 30, - NV_ENC_LEVEL_HEVC_2 = 60, - NV_ENC_LEVEL_HEVC_21 = 63, - NV_ENC_LEVEL_HEVC_3 = 90, - NV_ENC_LEVEL_HEVC_31 = 93, - NV_ENC_LEVEL_HEVC_4 = 120, - NV_ENC_LEVEL_HEVC_41 = 123, - NV_ENC_LEVEL_HEVC_5 = 150, - NV_ENC_LEVEL_HEVC_51 = 153, - NV_ENC_LEVEL_HEVC_52 = 156, - NV_ENC_LEVEL_HEVC_6 = 180, - NV_ENC_LEVEL_HEVC_61 = 183, - NV_ENC_LEVEL_HEVC_62 = 186, - - NV_ENC_TIER_HEVC_MAIN = 0, - NV_ENC_TIER_HEVC_HIGH = 1 -} NV_ENC_LEVEL; - -/** - * Error Codes - */ -typedef enum _NVENCSTATUS -{ - /** - * This indicates that API call returned with no errors. - */ - NV_ENC_SUCCESS, - - /** - * This indicates that no encode capable devices were detected. - */ - NV_ENC_ERR_NO_ENCODE_DEVICE, - - /** - * This indicates that devices pass by the client is not supported. - */ - NV_ENC_ERR_UNSUPPORTED_DEVICE, - - /** - * This indicates that the encoder device supplied by the client is not - * valid. - */ - NV_ENC_ERR_INVALID_ENCODERDEVICE, - - /** - * This indicates that device passed to the API call is invalid. - */ - NV_ENC_ERR_INVALID_DEVICE, - - /** - * This indicates that device passed to the API call is no longer available and - * needs to be reinitialized. The clients need to destroy the current encoder - * session by freeing the allocated input output buffers and destroying the device - * and create a new encoding session. - */ - NV_ENC_ERR_DEVICE_NOT_EXIST, - - /** - * This indicates that one or more of the pointers passed to the API call - * is invalid. - */ - NV_ENC_ERR_INVALID_PTR, - - /** - * This indicates that completion event passed in ::NvEncEncodePicture() call - * is invalid. - */ - NV_ENC_ERR_INVALID_EVENT, - - /** - * This indicates that one or more of the parameter passed to the API call - * is invalid. - */ - NV_ENC_ERR_INVALID_PARAM, - - /** - * This indicates that an API call was made in wrong sequence/order. - */ - NV_ENC_ERR_INVALID_CALL, - - /** - * This indicates that the API call failed because it was unable to allocate - * enough memory to perform the requested operation. - */ - NV_ENC_ERR_OUT_OF_MEMORY, - - /** - * This indicates that the encoder has not been initialized with - * ::NvEncInitializeEncoder() or that initialization has failed. - * The client cannot allocate input or output buffers or do any encoding - * related operation before successfully initializing the encoder. - */ - NV_ENC_ERR_ENCODER_NOT_INITIALIZED, - - /** - * This indicates that an unsupported parameter was passed by the client. - */ - NV_ENC_ERR_UNSUPPORTED_PARAM, - - /** - * This indicates that the ::NvEncLockBitstream() failed to lock the output - * buffer. This happens when the client makes a non blocking lock call to - * access the output bitstream by passing NV_ENC_LOCK_BITSTREAM::doNotWait flag. - * This is not a fatal error and client should retry the same operation after - * few milliseconds. - */ - NV_ENC_ERR_LOCK_BUSY, - - /** - * This indicates that the size of the user buffer passed by the client is - * insufficient for the requested operation. - */ - NV_ENC_ERR_NOT_ENOUGH_BUFFER, - - /** - * This indicates that an invalid struct version was used by the client. - */ - NV_ENC_ERR_INVALID_VERSION, - - /** - * This indicates that ::NvEncMapInputResource() API failed to map the client - * provided input resource. - */ - NV_ENC_ERR_MAP_FAILED, - - /** - * This indicates encode driver requires more input buffers to produce an output - * bitstream. If this error is returned from ::NvEncEncodePicture() API, this - * is not a fatal error. If the client is encoding with B frames then, - * ::NvEncEncodePicture() API might be buffering the input frame for re-ordering. - * - * A client operating in synchronous mode cannot call ::NvEncLockBitstream() - * API on the output bitstream buffer if ::NvEncEncodePicture() returned the - * ::NV_ENC_ERR_NEED_MORE_INPUT error code. - * The client must continue providing input frames until encode driver returns - * ::NV_ENC_SUCCESS. After receiving ::NV_ENC_SUCCESS status the client can call - * ::NvEncLockBitstream() API on the output buffers in the same order in which - * it has called ::NvEncEncodePicture(). - */ - NV_ENC_ERR_NEED_MORE_INPUT, - - /** - * This indicates that the HW encoder is busy encoding and is unable to encode - * the input. The client should call ::NvEncEncodePicture() again after few - * milliseconds. - */ - NV_ENC_ERR_ENCODER_BUSY, - - /** - * This indicates that the completion event passed in ::NvEncEncodePicture() - * API has not been registered with encoder driver using ::NvEncRegisterAsyncEvent(). - */ - NV_ENC_ERR_EVENT_NOT_REGISTERD, - - /** - * This indicates that an unknown internal error has occurred. - */ - NV_ENC_ERR_GENERIC, - - /** - * This indicates that the client is attempting to use a feature - * that is not available for the license type for the current system. - */ - NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, - - /** - * This indicates that the client is attempting to use a feature - * that is not implemented for the current version. - */ - NV_ENC_ERR_UNIMPLEMENTED, - - /** - * This indicates that the ::NvEncRegisterResource API failed to register the resource. - */ - NV_ENC_ERR_RESOURCE_REGISTER_FAILED, - - /** - * This indicates that the client is attempting to unregister a resource - * that has not been successfully registered. - */ - NV_ENC_ERR_RESOURCE_NOT_REGISTERED, - - /** - * This indicates that the client is attempting to unmap a resource - * that has not been successfully mapped. - */ - NV_ENC_ERR_RESOURCE_NOT_MAPPED, - -} NVENCSTATUS; - -/** - * Encode Picture encode flags. - */ -typedef enum _NV_ENC_PIC_FLAGS -{ - NV_ENC_PIC_FLAG_FORCEINTRA = 0x1, /**< Encode the current picture as an Intra picture */ - NV_ENC_PIC_FLAG_FORCEIDR = 0x2, /**< Encode the current picture as an IDR picture. - This flag is only valid when Picture type decision is taken by the Encoder - [_NV_ENC_INITIALIZE_PARAMS::enablePTD == 1]. */ - NV_ENC_PIC_FLAG_OUTPUT_SPSPPS = 0x4, /**< Write the sequence and picture header in encoded bitstream of the current picture */ - NV_ENC_PIC_FLAG_EOS = 0x8, /**< Indicates end of the input stream */ -} NV_ENC_PIC_FLAGS; - -/** - * Memory heap to allocate input and output buffers. - */ -typedef enum _NV_ENC_MEMORY_HEAP -{ - NV_ENC_MEMORY_HEAP_AUTOSELECT = 0, /**< Memory heap to be decided by the encoder driver based on the usage */ - NV_ENC_MEMORY_HEAP_VID = 1, /**< Memory heap is in local video memory */ - NV_ENC_MEMORY_HEAP_SYSMEM_CACHED = 2, /**< Memory heap is in cached system memory */ - NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED = 3 /**< Memory heap is in uncached system memory */ -} NV_ENC_MEMORY_HEAP; - -/** - * B-frame used as reference modes - */ -typedef enum _NV_ENC_BFRAME_REF_MODE -{ - NV_ENC_BFRAME_REF_MODE_DISABLED = 0x0, /**< B frame is not used for reference */ - NV_ENC_BFRAME_REF_MODE_EACH = 0x1, /**< Each B-frame will be used for reference. currently not supported for H.264 */ - NV_ENC_BFRAME_REF_MODE_MIDDLE = 0x2, /**< Only(Number of B-frame)/2 th B-frame will be used for reference */ -} NV_ENC_BFRAME_REF_MODE; - -/** - * H.264 entropy coding modes. - */ -typedef enum _NV_ENC_H264_ENTROPY_CODING_MODE -{ - NV_ENC_H264_ENTROPY_CODING_MODE_AUTOSELECT = 0x0, /**< Entropy coding mode is auto selected by the encoder driver */ - NV_ENC_H264_ENTROPY_CODING_MODE_CABAC = 0x1, /**< Entropy coding mode is CABAC */ - NV_ENC_H264_ENTROPY_CODING_MODE_CAVLC = 0x2 /**< Entropy coding mode is CAVLC */ -} NV_ENC_H264_ENTROPY_CODING_MODE; - -/** - * H.264 specific Bdirect modes - */ -typedef enum _NV_ENC_H264_BDIRECT_MODE -{ - NV_ENC_H264_BDIRECT_MODE_AUTOSELECT = 0x0, /**< BDirect mode is auto selected by the encoder driver */ - NV_ENC_H264_BDIRECT_MODE_DISABLE = 0x1, /**< Disable BDirect mode */ - NV_ENC_H264_BDIRECT_MODE_TEMPORAL = 0x2, /**< Temporal BDirect mode */ - NV_ENC_H264_BDIRECT_MODE_SPATIAL = 0x3 /**< Spatial BDirect mode */ -} NV_ENC_H264_BDIRECT_MODE; - -/** - * H.264 specific FMO usage - */ -typedef enum _NV_ENC_H264_FMO_MODE -{ - NV_ENC_H264_FMO_AUTOSELECT = 0x0, /**< FMO usage is auto selected by the encoder driver */ - NV_ENC_H264_FMO_ENABLE = 0x1, /**< Enable FMO */ - NV_ENC_H264_FMO_DISABLE = 0x2, /**< Disble FMO */ -} NV_ENC_H264_FMO_MODE; - -/** - * H.264 specific Adaptive Transform modes - */ -typedef enum _NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE -{ - NV_ENC_H264_ADAPTIVE_TRANSFORM_AUTOSELECT = 0x0, /**< Adaptive Transform 8x8 mode is auto selected by the encoder driver*/ - NV_ENC_H264_ADAPTIVE_TRANSFORM_DISABLE = 0x1, /**< Adaptive Transform 8x8 mode disabled */ - NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE = 0x2, /**< Adaptive Transform 8x8 mode should be used */ -} NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE; - -/** - * Stereo frame packing modes. - */ -typedef enum _NV_ENC_STEREO_PACKING_MODE -{ - NV_ENC_STEREO_PACKING_MODE_NONE = 0x0, /**< No Stereo packing required */ - NV_ENC_STEREO_PACKING_MODE_CHECKERBOARD = 0x1, /**< Checkerboard mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_COLINTERLEAVE = 0x2, /**< Column Interleave mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_ROWINTERLEAVE = 0x3, /**< Row Interleave mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_SIDEBYSIDE = 0x4, /**< Side-by-side mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_TOPBOTTOM = 0x5, /**< Top-Bottom mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_FRAMESEQ = 0x6 /**< Frame Sequential mode for packing stereo frames */ -} NV_ENC_STEREO_PACKING_MODE; - -/** - * Input Resource type - */ -typedef enum _NV_ENC_INPUT_RESOURCE_TYPE -{ - NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX = 0x0, /**< input resource type is a directx9 surface*/ - NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR = 0x1, /**< input resource type is a cuda device pointer surface*/ - NV_ENC_INPUT_RESOURCE_TYPE_CUDAARRAY = 0x2, /**< input resource type is a cuda array surface. - This array must be a 2D array and the CUDA_ARRAY3D_SURFACE_LDST - flag must have been specified when creating it. */ - NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX = 0x3 /**< input resource type is an OpenGL texture */ -} NV_ENC_INPUT_RESOURCE_TYPE; - -/** - * Buffer usage - */ -typedef enum _NV_ENC_BUFFER_USAGE -{ - NV_ENC_INPUT_IMAGE = 0x0, /**< Registered surface will be used for input image */ - NV_ENC_OUTPUT_MOTION_VECTOR = 0x1, /**< Registered surface will be used for output of H.264 ME only mode. - This buffer usage type is not supported for HEVC ME only mode. */ - NV_ENC_OUTPUT_BITSTREAM = 0x2 /**< Registered surface will be used for output bitstream in encoding */ -} NV_ENC_BUFFER_USAGE; - -/** - * Encoder Device type - */ -typedef enum _NV_ENC_DEVICE_TYPE -{ - NV_ENC_DEVICE_TYPE_DIRECTX = 0x0, /**< encode device type is a directx9 device */ - NV_ENC_DEVICE_TYPE_CUDA = 0x1, /**< encode device type is a cuda device */ - NV_ENC_DEVICE_TYPE_OPENGL = 0x2 /**< encode device type is an OpenGL device. - Use of this device type is supported only on Linux */ -} NV_ENC_DEVICE_TYPE; - -/** - * Encoder capabilities enumeration. - */ -typedef enum _NV_ENC_CAPS -{ - /** - * Maximum number of B-Frames supported. - */ - NV_ENC_CAPS_NUM_MAX_BFRAMES, - - /** - * Rate control modes supported. - * \n The API return value is a bitmask of the values in NV_ENC_PARAMS_RC_MODE. - */ - NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES, - - /** - * Indicates HW support for field mode encoding. - * \n 0 : Interlaced mode encoding is not supported. - * \n 1 : Interlaced field mode encoding is supported. - * \n 2 : Interlaced frame encoding and field mode encoding are both supported. - */ - NV_ENC_CAPS_SUPPORT_FIELD_ENCODING, - - /** - * Indicates HW support for monochrome mode encoding. - * \n 0 : Monochrome mode not supported. - * \n 1 : Monochrome mode supported. - */ - NV_ENC_CAPS_SUPPORT_MONOCHROME, - - /** - * Indicates HW support for FMO. - * \n 0 : FMO not supported. - * \n 1 : FMO supported. - */ - NV_ENC_CAPS_SUPPORT_FMO, - - /** - * Indicates HW capability for Quarter pel motion estimation. - * \n 0 : QuarterPel Motion Estimation not supported. - * \n 1 : QuarterPel Motion Estimation supported. - */ - NV_ENC_CAPS_SUPPORT_QPELMV, - - /** - * H.264 specific. Indicates HW support for BDirect modes. - * \n 0 : BDirect mode encoding not supported. - * \n 1 : BDirect mode encoding supported. - */ - NV_ENC_CAPS_SUPPORT_BDIRECT_MODE, - - /** - * H264 specific. Indicates HW support for CABAC entropy coding mode. - * \n 0 : CABAC entropy coding not supported. - * \n 1 : CABAC entropy coding supported. - */ - NV_ENC_CAPS_SUPPORT_CABAC, - - /** - * Indicates HW support for Adaptive Transform. - * \n 0 : Adaptive Transform not supported. - * \n 1 : Adaptive Transform supported. - */ - NV_ENC_CAPS_SUPPORT_ADAPTIVE_TRANSFORM, - - /** - * Reserved enum field. - */ - NV_ENC_CAPS_SUPPORT_RESERVED, - - /** - * Indicates HW support for encoding Temporal layers. - * \n 0 : Encoding Temporal layers not supported. - * \n 1 : Encoding Temporal layers supported. - */ - NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS, - - /** - * Indicates HW support for Hierarchical P frames. - * \n 0 : Hierarchical P frames not supported. - * \n 1 : Hierarchical P frames supported. - */ - NV_ENC_CAPS_SUPPORT_HIERARCHICAL_PFRAMES, - - /** - * Indicates HW support for Hierarchical B frames. - * \n 0 : Hierarchical B frames not supported. - * \n 1 : Hierarchical B frames supported. - */ - NV_ENC_CAPS_SUPPORT_HIERARCHICAL_BFRAMES, - - /** - * Maximum Encoding level supported (See ::NV_ENC_LEVEL for details). - */ - NV_ENC_CAPS_LEVEL_MAX, - - /** - * Minimum Encoding level supported (See ::NV_ENC_LEVEL for details). - */ - NV_ENC_CAPS_LEVEL_MIN, - - /** - * Indicates HW support for separate colour plane encoding. - * \n 0 : Separate colour plane encoding not supported. - * \n 1 : Separate colour plane encoding supported. - */ - NV_ENC_CAPS_SEPARATE_COLOUR_PLANE, - - /** - * Maximum output width supported. - */ - NV_ENC_CAPS_WIDTH_MAX, - - /** - * Maximum output height supported. - */ - NV_ENC_CAPS_HEIGHT_MAX, - - /** - * Indicates Temporal Scalability Support. - * \n 0 : Temporal SVC encoding not supported. - * \n 1 : Temporal SVC encoding supported. - */ - NV_ENC_CAPS_SUPPORT_TEMPORAL_SVC, - - /** - * Indicates Dynamic Encode Resolution Change Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Dynamic Encode Resolution Change not supported. - * \n 1 : Dynamic Encode Resolution Change supported. - */ - NV_ENC_CAPS_SUPPORT_DYN_RES_CHANGE, - - /** - * Indicates Dynamic Encode Bitrate Change Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Dynamic Encode bitrate change not supported. - * \n 1 : Dynamic Encode bitrate change supported. - */ - NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE, - - /** - * Indicates Forcing Constant QP On The Fly Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Forcing constant QP on the fly not supported. - * \n 1 : Forcing constant QP on the fly supported. - */ - NV_ENC_CAPS_SUPPORT_DYN_FORCE_CONSTQP, - - /** - * Indicates Dynamic rate control mode Change Support. - * \n 0 : Dynamic rate control mode change not supported. - * \n 1 : Dynamic rate control mode change supported. - */ - NV_ENC_CAPS_SUPPORT_DYN_RCMODE_CHANGE, - - /** - * Indicates Subframe readback support for slice-based encoding. - * \n 0 : Subframe readback not supported. - * \n 1 : Subframe readback supported. - */ - NV_ENC_CAPS_SUPPORT_SUBFRAME_READBACK, - - /** - * Indicates Constrained Encoding mode support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Constrained encoding mode not supported. - * \n 1 : Constarined encoding mode supported. - * If this mode is supported client can enable this during initialisation. - * Client can then force a picture to be coded as constrained picture where - * each slice in a constrained picture will have constrained_intra_pred_flag set to 1 - * and disable_deblocking_filter_idc will be set to 2 and prediction vectors for inter - * macroblocks in each slice will be restricted to the slice region. - */ - NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING, - - /** - * Indicates Intra Refresh Mode Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Intra Refresh Mode not supported. - * \n 1 : Intra Refresh Mode supported. - */ - NV_ENC_CAPS_SUPPORT_INTRA_REFRESH, - - /** - * Indicates Custom VBV Bufer Size support. It can be used for capping frame size. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Custom VBV buffer size specification from client, not supported. - * \n 1 : Custom VBV buffer size specification from client, supported. - */ - NV_ENC_CAPS_SUPPORT_CUSTOM_VBV_BUF_SIZE, - - /** - * Indicates Dynamic Slice Mode Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Dynamic Slice Mode not supported. - * \n 1 : Dynamic Slice Mode supported. - */ - NV_ENC_CAPS_SUPPORT_DYNAMIC_SLICE_MODE, - - /** - * Indicates Reference Picture Invalidation Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Reference Picture Invalidation not supported. - * \n 1 : Reference Picture Invalidation supported. - */ - NV_ENC_CAPS_SUPPORT_REF_PIC_INVALIDATION, - - /** - * Indicates support for PreProcessing. - * The API return value is a bitmask of the values defined in ::NV_ENC_PREPROC_FLAGS - */ - NV_ENC_CAPS_PREPROC_SUPPORT, - - /** - * Indicates support Async mode. - * \n 0 : Async Encode mode not supported. - * \n 1 : Async Encode mode supported. - */ - NV_ENC_CAPS_ASYNC_ENCODE_SUPPORT, - - /** - * Maximum MBs per frame supported. - */ - NV_ENC_CAPS_MB_NUM_MAX, - - /** - * Maximum aggregate throughput in MBs per sec. - */ - NV_ENC_CAPS_MB_PER_SEC_MAX, - - /** - * Indicates HW support for YUV444 mode encoding. - * \n 0 : YUV444 mode encoding not supported. - * \n 1 : YUV444 mode encoding supported. - */ - NV_ENC_CAPS_SUPPORT_YUV444_ENCODE, - - /** - * Indicates HW support for lossless encoding. - * \n 0 : lossless encoding not supported. - * \n 1 : lossless encoding supported. - */ - NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE, - - /** - * Indicates HW support for Sample Adaptive Offset. - * \n 0 : SAO not supported. - * \n 1 : SAO encoding supported. - */ - NV_ENC_CAPS_SUPPORT_SAO, - - /** - * Indicates HW support for MEOnly Mode. - * \n 0 : MEOnly Mode not supported. - * \n 1 : MEOnly Mode supported for I and P frames. - * \n 2 : MEOnly Mode supported for I, P and B frames. - */ - NV_ENC_CAPS_SUPPORT_MEONLY_MODE, - - /** - * Indicates HW support for lookahead encoding (enableLookahead=1). - * \n 0 : Lookahead not supported. - * \n 1 : Lookahead supported. - */ - NV_ENC_CAPS_SUPPORT_LOOKAHEAD, - - /** - * Indicates HW support for temporal AQ encoding (enableTemporalAQ=1). - * \n 0 : Temporal AQ not supported. - * \n 1 : Temporal AQ supported. - */ - NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ, - /** - * Indicates HW support for 10 bit encoding. - * \n 0 : 10 bit encoding not supported. - * \n 1 : 10 bit encoding supported. - */ - NV_ENC_CAPS_SUPPORT_10BIT_ENCODE, - /** - * Maximum number of Long Term Reference frames supported - */ - NV_ENC_CAPS_NUM_MAX_LTR_FRAMES, - - /** - * Indicates HW support for Weighted Predicition. - * \n 0 : Weighted Predicition not supported. - * \n 1 : Weighted Predicition supported. - */ - NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION, - - - /** - * On managed (vGPU) platforms (Windows only), this API, in conjunction with other GRID Management APIs, can be used - * to estimate the residual capacity of the hardware encoder on the GPU as a percentage of the total available encoder capacity. - * This API can be called at any time; i.e. during the encode session or before opening the encode session. - * If the available encoder capacity is returned as zero, applications may choose to switch to software encoding - * and continue to call this API (e.g. polling once per second) until capacity becomes available. - * - * On baremetal (non-virtualized GPU) and linux platforms, this API always returns 100. - */ - NV_ENC_CAPS_DYNAMIC_QUERY_ENCODER_CAPACITY, - - /** - * Indicates B as refererence support. - * \n 0 : B as reference is not supported. - * \n 1 : each B-Frame as reference is supported. - * \n 2 : only Middle B-frame as reference is supported. - */ - NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE, - - /** - * Indicates HW support for Emphasis Level Map based delta QP computation. - * \n 0 : Emphasis Level Map based delta QP not supported. - * \n 1 : Emphasis Level Map based delta QP is supported. - */ - NV_ENC_CAPS_SUPPORT_EMPHASIS_LEVEL_MAP, - - /** - * Reserved - Not to be used by clients. - */ - NV_ENC_CAPS_EXPOSED_COUNT -} NV_ENC_CAPS; - -/** - * HEVC CU SIZE - */ -typedef enum _NV_ENC_HEVC_CUSIZE -{ - NV_ENC_HEVC_CUSIZE_AUTOSELECT = 0, - NV_ENC_HEVC_CUSIZE_8x8 = 1, - NV_ENC_HEVC_CUSIZE_16x16 = 2, - NV_ENC_HEVC_CUSIZE_32x32 = 3, - NV_ENC_HEVC_CUSIZE_64x64 = 4, -}NV_ENC_HEVC_CUSIZE; - -/** - * Input struct for querying Encoding capabilities. - */ -typedef struct _NV_ENC_CAPS_PARAM -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CAPS_PARAM_VER */ - NV_ENC_CAPS capsToQuery; /**< [in]: Specifies the encode capability to be queried. Client should pass a member for ::NV_ENC_CAPS enum. */ - uint32_t reserved[62]; /**< [in]: Reserved and must be set to 0 */ -} NV_ENC_CAPS_PARAM; - -/** NV_ENC_CAPS_PARAM struct version. */ -#define NV_ENC_CAPS_PARAM_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * Encoder Output parameters - */ -typedef struct _NV_ENC_ENCODE_OUT_PARAMS -{ - uint32_t version; /**< [out]: Struct version. */ - uint32_t bitstreamSizeInBytes; /**< [out]: Encoded bitstream size in bytes */ - uint32_t reserved[62]; /**< [out]: Reserved and must be set to 0 */ -} NV_ENC_ENCODE_OUT_PARAMS; - -/** NV_ENC_ENCODE_OUT_PARAMS struct version. */ -#define NV_ENC_ENCODE_OUT_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * Creation parameters for input buffer. - */ -typedef struct _NV_ENC_CREATE_INPUT_BUFFER -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CREATE_INPUT_BUFFER_VER */ - uint32_t width; /**< [in]: Input buffer width */ - uint32_t height; /**< [in]: Input buffer width */ - NV_ENC_MEMORY_HEAP memoryHeap; /**< [in]: Deprecated. Do not use */ - NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Input buffer format */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ - NV_ENC_INPUT_PTR inputBuffer; /**< [out]: Pointer to input buffer */ - void* pSysMemBuffer; /**< [in]: Pointer to existing sysmem buffer */ - uint32_t reserved1[57]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[63]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CREATE_INPUT_BUFFER; - -/** NV_ENC_CREATE_INPUT_BUFFER struct version. */ -#define NV_ENC_CREATE_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * Creation parameters for output bitstream buffer. - */ -typedef struct _NV_ENC_CREATE_BITSTREAM_BUFFER -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CREATE_BITSTREAM_BUFFER_VER */ - uint32_t size; /**< [in]: Deprecated. Do not use */ - NV_ENC_MEMORY_HEAP memoryHeap; /**< [in]: Deprecated. Do not use */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ - NV_ENC_OUTPUT_PTR bitstreamBuffer; /**< [out]: Pointer to the output bitstream buffer */ - void* bitstreamBufferPtr; /**< [out]: Reserved and should not be used */ - uint32_t reserved1[58]; /**< [in]: Reserved and should be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and should be set to NULL */ -} NV_ENC_CREATE_BITSTREAM_BUFFER; - -/** NV_ENC_CREATE_BITSTREAM_BUFFER struct version. */ -#define NV_ENC_CREATE_BITSTREAM_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * Structs needed for ME only mode. - */ -typedef struct _NV_ENC_MVECTOR -{ - int16_t mvx; /**< the x component of MV in qpel units */ - int16_t mvy; /**< the y component of MV in qpel units */ -} NV_ENC_MVECTOR; - -/** - * Motion vector structure per macroblock for H264 motion estimation. - */ -typedef struct _NV_ENC_H264_MV_DATA -{ - NV_ENC_MVECTOR mv[4]; /**< up to 4 vectors for 8x8 partition */ - uint8_t mbType; /**< 0 (I), 1 (P), 2 (IPCM), 3 (B) */ - uint8_t partitionType; /**< Specifies the block partition type. 0:16x16, 1:8x8, 2:16x8, 3:8x16 */ - uint16_t reserved; /**< reserved padding for alignment */ - uint32_t mbCost; -} NV_ENC_H264_MV_DATA; - -/** - * Motion vector structure per CU for HEVC motion estimation. - */ -typedef struct _NV_ENC_HEVC_MV_DATA -{ - NV_ENC_MVECTOR mv[4]; /**< up to 4 vectors within a CU */ - uint8_t cuType; /**< 0 (I), 1(P) */ - uint8_t cuSize; /**< 0: 8x8, 1: 16x16, 2: 32x32, 3: 64x64 */ - uint8_t partitionMode; /**< The CU partition mode - 0 (2Nx2N), 1 (2NxN), 2(Nx2N), 3 (NxN), - 4 (2NxnU), 5 (2NxnD), 6(nLx2N), 7 (nRx2N) */ - uint8_t lastCUInCTB; /**< Marker to separate CUs in the current CTB from CUs in the next CTB */ -} NV_ENC_HEVC_MV_DATA; - -/** - * Creation parameters for output motion vector buffer for ME only mode. - */ -typedef struct _NV_ENC_CREATE_MV_BUFFER -{ - uint32_t version; /**< [in]: Struct version. Must be set to NV_ENC_CREATE_MV_BUFFER_VER */ - NV_ENC_OUTPUT_PTR mvBuffer; /**< [out]: Pointer to the output motion vector buffer */ - uint32_t reserved1[255]; /**< [in]: Reserved and should be set to 0 */ - void* reserved2[63]; /**< [in]: Reserved and should be set to NULL */ -} NV_ENC_CREATE_MV_BUFFER; - -/** NV_ENC_CREATE_MV_BUFFER struct version*/ -#define NV_ENC_CREATE_MV_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * QP value for frames - */ -typedef struct _NV_ENC_QP -{ - uint32_t qpInterP; - uint32_t qpInterB; - uint32_t qpIntra; -} NV_ENC_QP; - -/** - * Rate Control Configuration Paramters - */ - typedef struct _NV_ENC_RC_PARAMS - { - uint32_t version; - NV_ENC_PARAMS_RC_MODE rateControlMode; /**< [in]: Specifies the rate control mode. Check support for various rate control modes using ::NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES caps. */ - NV_ENC_QP constQP; /**< [in]: Specifies the initial QP to be used for encoding, these values would be used for all frames if in Constant QP mode. */ - uint32_t averageBitRate; /**< [in]: Specifies the average bitrate(in bits/sec) used for encoding. */ - uint32_t maxBitRate; /**< [in]: Specifies the maximum bitrate for the encoded output. This is used for VBR and ignored for CBR mode. */ - uint32_t vbvBufferSize; /**< [in]: Specifies the VBV(HRD) buffer size. in bits. Set 0 to use the default VBV buffer size. */ - uint32_t vbvInitialDelay; /**< [in]: Specifies the VBV(HRD) initial delay in bits. Set 0 to use the default VBV initial delay .*/ - uint32_t enableMinQP :1; /**< [in]: Set this to 1 if minimum QP used for rate control. */ - uint32_t enableMaxQP :1; /**< [in]: Set this to 1 if maximum QP used for rate control. */ - uint32_t enableInitialRCQP :1; /**< [in]: Set this to 1 if user suppplied initial QP is used for rate control. */ - uint32_t enableAQ :1; /**< [in]: Set this to 1 to enable adaptive quantization (Spatial). */ - uint32_t reservedBitField1 :1; /**< [in]: Reserved bitfields and must be set to 0. */ - uint32_t enableLookahead :1; /**< [in]: Set this to 1 to enable lookahead with depth <lookaheadDepth> (if lookahead is enabled, input frames must remain available to the encoder until encode completion) */ - uint32_t disableIadapt :1; /**< [in]: Set this to 1 to disable adaptive I-frame insertion at scene cuts (only has an effect when lookahead is enabled) */ - uint32_t disableBadapt :1; /**< [in]: Set this to 1 to disable adaptive B-frame decision (only has an effect when lookahead is enabled) */ - uint32_t enableTemporalAQ :1; /**< [in]: Set this to 1 to enable temporal AQ for H.264 */ - uint32_t zeroReorderDelay :1; /**< [in]: Set this to 1 to indicate zero latency operation (no reordering delay, num_reorder_frames=0) */ - uint32_t enableNonRefP :1; /**< [in]: Set this to 1 to enable automatic insertion of non-reference P-frames (no effect if enablePTD=0) */ - uint32_t strictGOPTarget :1; /**< [in]: Set this to 1 to minimize GOP-to-GOP rate fluctuations */ - uint32_t aqStrength :4; /**< [in]: When AQ (Spatial) is enabled (i.e. NV_ENC_RC_PARAMS::enableAQ is set), this field is used to specify AQ strength. AQ strength scale is from 1 (low) - 15 (aggressive). If not set, strength is autoselected by driver. */ - uint32_t reservedBitFields :16; /**< [in]: Reserved bitfields and must be set to 0 */ - NV_ENC_QP minQP; /**< [in]: Specifies the minimum QP used for rate control. Client must set NV_ENC_CONFIG::enableMinQP to 1. */ - NV_ENC_QP maxQP; /**< [in]: Specifies the maximum QP used for rate control. Client must set NV_ENC_CONFIG::enableMaxQP to 1. */ - NV_ENC_QP initialRCQP; /**< [in]: Specifies the initial QP used for rate control. Client must set NV_ENC_CONFIG::enableInitialRCQP to 1. */ - uint32_t temporallayerIdxMask; /**< [in]: Specifies the temporal layers (as a bitmask) whose QPs have changed. Valid max bitmask is [2^NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS - 1] */ - uint8_t temporalLayerQP[8]; /**< [in]: Specifies the temporal layer QPs used for rate control. Temporal layer index is used as as the array index */ - uint8_t targetQuality; /**< [in]: Target CQ (Constant Quality) level for VBR mode (range 0-51 with 0-automatic) */ - uint8_t targetQualityLSB; /**< [in]: Fractional part of target quality (as 8.8 fixed point format) */ - uint16_t lookaheadDepth; /**< [in]: Maximum depth of lookahead with range 0-32 (only used if enableLookahead=1) */ - uint32_t reserved1; - NV_ENC_QP_MAP_MODE qpMapMode; /**< [in]: This flag is used to interpret values in array pecified by NV_ENC_PIC_PARAMS::qpDeltaMap. - Set this to NV_ENC_QP_MAP_EMPHASIS to treat values specified by NV_ENC_PIC_PARAMS::qpDeltaMap as Emphasis level Map. - Emphasis Level can be assigned any value specified in enum NV_ENC_EMPHASIS_MAP_LEVEL. - Emphasis Level Map is used to specify regions to be encoded at varying levels of quality. - The hardware encoder adjusts the quantization within the image as per the provided emphasis map, - by adjusting the quantization parameter (QP) assigned to each macroblock. This adjustment is commonly called “Delta QPâ€. - The adjustment depends on the absolute QP decided by the rate control algorithm, and is applied after the rate control has decided each macroblock’s QP. - Since the Delta QP overrides rate control, enabling emphasis level map may violate bitrate and VBV buffersize constraints. - Emphasis level map is useful in situations when client has a priori knowledge of the image complexity (e.g. via use of NVFBC's Classification feature) and encoding those high-complexity areas at higher quality (lower QP) is important, even at the possible cost of violating bitrate/VBV buffersize constraints - This feature is not supported when AQ( Spatial/Temporal) is enabled. - This feature is only supported for H264 codec currently. - - Set this to NV_ENC_QP_MAP_DELTA to treat values specified by NV_ENC_PIC_PARAMS::qpDeltaMap as QPDelta. This specify QP modifier to be applied on top of the QP chosen by rate control - - Set this to NV_ENC_QP_MAP_DISABLED to ignore NV_ENC_PIC_PARAMS::qpDeltaMap values. In this case, qpDeltaMap should be set to NULL. - - Other values are reserved for future use.*/ - uint32_t reserved[7]; - } NV_ENC_RC_PARAMS; - -/** macro for constructing the version field of ::_NV_ENC_RC_PARAMS */ -#define NV_ENC_RC_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) - - - -/** - * \struct _NV_ENC_CONFIG_H264_VUI_PARAMETERS - * H264 Video Usability Info parameters - */ -typedef struct _NV_ENC_CONFIG_H264_VUI_PARAMETERS -{ - uint32_t overscanInfoPresentFlag; /**< [in]: if set to 1 , it specifies that the overscanInfo is present */ - uint32_t overscanInfo; /**< [in]: Specifies the overscan info(as defined in Annex E of the ITU-T Specification). */ - uint32_t videoSignalTypePresentFlag; /**< [in]: If set to 1, it specifies that the videoFormat, videoFullRangeFlag and colourDescriptionPresentFlag are present. */ - uint32_t videoFormat; /**< [in]: Specifies the source video format(as defined in Annex E of the ITU-T Specification).*/ - uint32_t videoFullRangeFlag; /**< [in]: Specifies the output range of the luma and chroma samples(as defined in Annex E of the ITU-T Specification). */ - uint32_t colourDescriptionPresentFlag; /**< [in]: If set to 1, it specifies that the colourPrimaries, transferCharacteristics and colourMatrix are present. */ - uint32_t colourPrimaries; /**< [in]: Specifies color primaries for converting to RGB(as defined in Annex E of the ITU-T Specification) */ - uint32_t transferCharacteristics; /**< [in]: Specifies the opto-electronic transfer characteristics to use (as defined in Annex E of the ITU-T Specification) */ - uint32_t colourMatrix; /**< [in]: Specifies the matrix coefficients used in deriving the luma and chroma from the RGB primaries (as defined in Annex E of the ITU-T Specification). */ - uint32_t chromaSampleLocationFlag; /**< [in]: if set to 1 , it specifies that the chromaSampleLocationTop and chromaSampleLocationBot are present.*/ - uint32_t chromaSampleLocationTop; /**< [in]: Specifies the chroma sample location for top field(as defined in Annex E of the ITU-T Specification) */ - uint32_t chromaSampleLocationBot; /**< [in]: Specifies the chroma sample location for bottom field(as defined in Annex E of the ITU-T Specification) */ - uint32_t bitstreamRestrictionFlag; /**< [in]: if set to 1, it specifies the bitstream restriction parameters are present in the bitstream.*/ - uint32_t reserved[15]; -}NV_ENC_CONFIG_H264_VUI_PARAMETERS; - -typedef NV_ENC_CONFIG_H264_VUI_PARAMETERS NV_ENC_CONFIG_HEVC_VUI_PARAMETERS; - -/** - * \struct _NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE - * External motion vector hint counts per block type. - * H264 supports multiple hint while HEVC supports one hint for each valid candidate. - */ -typedef struct _NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE -{ - uint32_t numCandsPerBlk16x16 : 4; /**< [in]: Supported for H264,HEVC.It Specifies the number of candidates per 16x16 block. */ - uint32_t numCandsPerBlk16x8 : 4; /**< [in]: Supported for H264 only.Specifies the number of candidates per 16x8 block. */ - uint32_t numCandsPerBlk8x16 : 4; /**< [in]: Supported for H264 only.Specifies the number of candidates per 8x16 block. */ - uint32_t numCandsPerBlk8x8 : 4; /**< [in]: Supported for H264,HEVC.Specifies the number of candidates per 8x8 block. */ - uint32_t reserved : 16; /**< [in]: Reserved for padding. */ - uint32_t reserved1[3]; /**< [in]: Reserved for future use. */ -} NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE; - - -/** - * \struct _NVENC_EXTERNAL_ME_HINT - * External Motion Vector hint structure. - */ -typedef struct _NVENC_EXTERNAL_ME_HINT -{ - int32_t mvx : 12; /**< [in]: Specifies the x component of integer pixel MV (relative to current MB) S12.0. */ - int32_t mvy : 10; /**< [in]: Specifies the y component of integer pixel MV (relative to current MB) S10.0 .*/ - int32_t refidx : 5; /**< [in]: Specifies the reference index (31=invalid). Current we support only 1 reference frame per direction for external hints, so \p refidx must be 0. */ - int32_t dir : 1; /**< [in]: Specifies the direction of motion estimation . 0=L0 1=L1.*/ - int32_t partType : 2; /**< [in]: Specifies the block partition type.0=16x16 1=16x8 2=8x16 3=8x8 (blocks in partition must be consecutive).*/ - int32_t lastofPart : 1; /**< [in]: Set to 1 for the last MV of (sub) partition */ - int32_t lastOfMB : 1; /**< [in]: Set to 1 for the last MV of macroblock. */ -} NVENC_EXTERNAL_ME_HINT; - - -/** - * \struct _NV_ENC_CONFIG_H264 - * H264 encoder configuration parameters - */ -typedef struct _NV_ENC_CONFIG_H264 -{ - uint32_t enableTemporalSVC :1; /**< [in]: Set to 1 to enable SVC temporal*/ - uint32_t enableStereoMVC :1; /**< [in]: Set to 1 to enable stereo MVC*/ - uint32_t hierarchicalPFrames :1; /**< [in]: Set to 1 to enable hierarchical PFrames */ - uint32_t hierarchicalBFrames :1; /**< [in]: Set to 1 to enable hierarchical BFrames */ - uint32_t outputBufferingPeriodSEI :1; /**< [in]: Set to 1 to write SEI buffering period syntax in the bitstream */ - uint32_t outputPictureTimingSEI :1; /**< [in]: Set to 1 to write SEI picture timing syntax in the bitstream. When set for following rateControlMode : NV_ENC_PARAMS_RC_CBR, NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ, - NV_ENC_PARAMS_RC_CBR_HQ, filler data is inserted if needed to achieve hrd bitrate */ - uint32_t outputAUD :1; /**< [in]: Set to 1 to write access unit delimiter syntax in bitstream */ - uint32_t disableSPSPPS :1; /**< [in]: Set to 1 to disable writing of Sequence and Picture parameter info in bitstream */ - uint32_t outputFramePackingSEI :1; /**< [in]: Set to 1 to enable writing of frame packing arrangement SEI messages to bitstream */ - uint32_t outputRecoveryPointSEI :1; /**< [in]: Set to 1 to enable writing of recovery point SEI message */ - uint32_t enableIntraRefresh :1; /**< [in]: Set to 1 to enable gradual decoder refresh or intra refresh. If the GOP structure uses B frames this will be ignored */ - uint32_t enableConstrainedEncoding :1; /**< [in]: Set this to 1 to enable constrainedFrame encoding where each slice in the constarined picture is independent of other slices - Check support for constrained encoding using ::NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING caps. */ - uint32_t repeatSPSPPS :1; /**< [in]: Set to 1 to enable writing of Sequence and Picture parameter for every IDR frame */ - uint32_t enableVFR :1; /**< [in]: Set to 1 to enable variable frame rate. */ - uint32_t enableLTR :1; /**< [in]: Set to 1 to enable LTR (Long Term Reference) frame support. LTR can be used in two modes: "LTR Trust" mode and "LTR Per Picture" mode. - LTR Trust mode: In this mode, ltrNumFrames pictures after IDR are automatically marked as LTR. This mode is enabled by setting ltrTrustMode = 1. - Use of LTR Trust mode is strongly discouraged as this mode may be deprecated in future. - LTR Per Picture mode: In this mode, client can control whether the current picture should be marked as LTR. Enable this mode by setting - ltrTrustMode = 0 and ltrMarkFrame = 1 for the picture to be marked as LTR. This is the preferred mode - for using LTR. - Note that LTRs are not supported if encoding session is configured with B-frames */ - uint32_t qpPrimeYZeroTransformBypassFlag :1; /**< [in]: To enable lossless encode set this to 1, set QP to 0 and RC_mode to NV_ENC_PARAMS_RC_CONSTQP and profile to HIGH_444_PREDICTIVE_PROFILE. - Check support for lossless encoding using ::NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE caps. */ - uint32_t useConstrainedIntraPred :1; /**< [in]: Set 1 to enable constrained intra prediction. */ - uint32_t reservedBitFields :15; /**< [in]: Reserved bitfields and must be set to 0 */ - uint32_t level; /**< [in]: Specifies the encoding level. Client is recommended to set this to NV_ENC_LEVEL_AUTOSELECT in order to enable the NvEncodeAPI interface to select the correct level. */ - uint32_t idrPeriod; /**< [in]: Specifies the IDR interval. If not set, this is made equal to gopLength in NV_ENC_CONFIG.Low latency application client can set IDR interval to NVENC_INFINITE_GOPLENGTH so that IDR frames are not inserted automatically. */ - uint32_t separateColourPlaneFlag; /**< [in]: Set to 1 to enable 4:4:4 separate colour planes */ - uint32_t disableDeblockingFilterIDC; /**< [in]: Specifies the deblocking filter mode. Permissible value range: [0,2] */ - uint32_t numTemporalLayers; /**< [in]: Specifies max temporal layers to be used for hierarchical coding. Valid value range is [1,::NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS] */ - uint32_t spsId; /**< [in]: Specifies the SPS id of the sequence header */ - uint32_t ppsId; /**< [in]: Specifies the PPS id of the picture header */ - NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE adaptiveTransformMode; /**< [in]: Specifies the AdaptiveTransform Mode. Check support for AdaptiveTransform mode using ::NV_ENC_CAPS_SUPPORT_ADAPTIVE_TRANSFORM caps. */ - NV_ENC_H264_FMO_MODE fmoMode; /**< [in]: Specified the FMO Mode. Check support for FMO using ::NV_ENC_CAPS_SUPPORT_FMO caps. */ - NV_ENC_H264_BDIRECT_MODE bdirectMode; /**< [in]: Specifies the BDirect mode. Check support for BDirect mode using ::NV_ENC_CAPS_SUPPORT_BDIRECT_MODE caps.*/ - NV_ENC_H264_ENTROPY_CODING_MODE entropyCodingMode; /**< [in]: Specifies the entropy coding mode. Check support for CABAC mode using ::NV_ENC_CAPS_SUPPORT_CABAC caps. */ - NV_ENC_STEREO_PACKING_MODE stereoMode; /**< [in]: Specifies the stereo frame packing mode which is to be signalled in frame packing arrangement SEI */ - uint32_t intraRefreshPeriod; /**< [in]: Specifies the interval between successive intra refresh if enableIntrarefresh is set. Requires enableIntraRefresh to be set. - Will be disabled if NV_ENC_CONFIG::gopLength is not set to NVENC_INFINITE_GOPLENGTH. */ - uint32_t intraRefreshCnt; /**< [in]: Specifies the length of intra refresh in number of frames for periodic intra refresh. This value should be smaller than intraRefreshPeriod */ - uint32_t maxNumRefFrames; /**< [in]: Specifies the DPB size used for encoding. Setting it to 0 will let driver use the default dpb size. - The low latency application which wants to invalidate reference frame as an error resilience tool - is recommended to use a large DPB size so that the encoder can keep old reference frames which can be used if recent - frames are invalidated. */ - uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices - sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3 numSlices in Picture. - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting - When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ - uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: - sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice) - sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) - sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice) - sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ - NV_ENC_CONFIG_H264_VUI_PARAMETERS h264VUIParameters; /**< [in]: Specifies the H264 video usability info pamameters */ - uint32_t ltrNumFrames; /**< [in]: Specifies the number of LTR frames. This parameter has different meaning in two LTR modes. - In "LTR Trust" mode (ltrTrustMode = 1), encoder will mark the first ltrNumFrames base layer reference frames within each IDR interval as LTR. - In "LTR Per Picture" mode (ltrTrustMode = 0 and ltrMarkFrame = 1), ltrNumFrames specifies maximum number of LTR frames in DPB. */ - uint32_t ltrTrustMode; /**< [in]: Specifies the LTR operating mode. See comments near NV_ENC_CONFIG_H264::enableLTR for description of the two modes. - Set to 1 to use "LTR Trust" mode of LTR operation. Clients are discouraged to use "LTR Trust" mode as this mode may - be deprecated in future releases. - Set to 0 when using "LTR Per Picture" mode of LTR operation. */ - uint32_t chromaFormatIDC; /**< [in]: Specifies the chroma format. Should be set to 1 for yuv420 input, 3 for yuv444 input. - Check support for YUV444 encoding using ::NV_ENC_CAPS_SUPPORT_YUV444_ENCODE caps.*/ - uint32_t maxTemporalLayers; /**< [in]: Specifies the max temporal layer used for hierarchical coding. */ - NV_ENC_BFRAME_REF_MODE useBFramesAsRef; /**< [in]: Specifies the B-Frame as reference mode. Check support for useBFramesAsRef mode using ::NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE caps.*/ - uint32_t reserved1[269]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG_H264; - -/** - * \struct _NV_ENC_CONFIG_HEVC - * HEVC encoder configuration parameters to be set during initialization. - */ -typedef struct _NV_ENC_CONFIG_HEVC -{ - uint32_t level; /**< [in]: Specifies the level of the encoded bitstream.*/ - uint32_t tier; /**< [in]: Specifies the level tier of the encoded bitstream.*/ - NV_ENC_HEVC_CUSIZE minCUSize; /**< [in]: Specifies the minimum size of luma coding unit.*/ - NV_ENC_HEVC_CUSIZE maxCUSize; /**< [in]: Specifies the maximum size of luma coding unit. Currently NVENC SDK only supports maxCUSize equal to NV_ENC_HEVC_CUSIZE_32x32.*/ - uint32_t useConstrainedIntraPred :1; /**< [in]: Set 1 to enable constrained intra prediction. */ - uint32_t disableDeblockAcrossSliceBoundary :1; /**< [in]: Set 1 to disable in loop filtering across slice boundary.*/ - uint32_t outputBufferingPeriodSEI :1; /**< [in]: Set 1 to write SEI buffering period syntax in the bitstream */ - uint32_t outputPictureTimingSEI :1; /**< [in]: Set 1 to write SEI picture timing syntax in the bitstream */ - uint32_t outputAUD :1; /**< [in]: Set 1 to write Access Unit Delimiter syntax. */ - uint32_t enableLTR :1; /**< [in]: Set to 1 to enable LTR (Long Term Reference) frame support. LTR can be used in two modes: "LTR Trust" mode and "LTR Per Picture" mode. - LTR Trust mode: In this mode, ltrNumFrames pictures after IDR are automatically marked as LTR. This mode is enabled by setting ltrTrustMode = 1. - Use of LTR Trust mode is strongly discouraged as this mode may be deprecated in future releases. - LTR Per Picture mode: In this mode, client can control whether the current picture should be marked as LTR. Enable this mode by setting - ltrTrustMode = 0 and ltrMarkFrame = 1 for the picture to be marked as LTR. This is the preferred mode - for using LTR. - Note that LTRs are not supported if encoding session is configured with B-frames */ - uint32_t disableSPSPPS :1; /**< [in]: Set 1 to disable VPS,SPS and PPS signalling in the bitstream. */ - uint32_t repeatSPSPPS :1; /**< [in]: Set 1 to output VPS,SPS and PPS for every IDR frame.*/ - uint32_t enableIntraRefresh :1; /**< [in]: Set 1 to enable gradual decoder refresh or intra refresh. If the GOP structure uses B frames this will be ignored */ - uint32_t chromaFormatIDC :2; /**< [in]: Specifies the chroma format. Should be set to 1 for yuv420 input, 3 for yuv444 input.*/ - uint32_t pixelBitDepthMinus8 :3; /**< [in]: Specifies pixel bit depth minus 8. Should be set to 0 for 8 bit input, 2 for 10 bit input.*/ - uint32_t reserved :18; /**< [in]: Reserved bitfields.*/ - uint32_t idrPeriod; /**< [in]: Specifies the IDR interval. If not set, this is made equal to gopLength in NV_ENC_CONFIG.Low latency application client can set IDR interval to NVENC_INFINITE_GOPLENGTH so that IDR frames are not inserted automatically. */ - uint32_t intraRefreshPeriod; /**< [in]: Specifies the interval between successive intra refresh if enableIntrarefresh is set. Requires enableIntraRefresh to be set. - Will be disabled if NV_ENC_CONFIG::gopLength is not set to NVENC_INFINITE_GOPLENGTH. */ - uint32_t intraRefreshCnt; /**< [in]: Specifies the length of intra refresh in number of frames for periodic intra refresh. This value should be smaller than intraRefreshPeriod */ - uint32_t maxNumRefFramesInDPB; /**< [in]: Specifies the maximum number of references frames in the DPB.*/ - uint32_t ltrNumFrames; /**< [in]: This parameter has different meaning in two LTR modes. - In "LTR Trust" mode (ltrTrustMode = 1), encoder will mark the first ltrNumFrames base layer reference frames within each IDR interval as LTR. - In "LTR Per Picture" mode (ltrTrustMode = 0 and ltrMarkFrame = 1), ltrNumFrames specifies maximum number of LTR frames in DPB. */ - uint32_t vpsId; /**< [in]: Specifies the VPS id of the video parameter set */ - uint32_t spsId; /**< [in]: Specifies the SPS id of the sequence header */ - uint32_t ppsId; /**< [in]: Specifies the PPS id of the picture header */ - uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices - sliceMode = 0 CTU based slices, sliceMode = 1 Byte based slices, sliceMode = 2 CTU row based slices, sliceMode = 3, numSlices in Picture - When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ - uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: - sliceMode = 0, sliceModeData specifies # of CTUs in each slice (except last slice) - sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) - sliceMode = 2, sliceModeData specifies # of CTU rows in each slice (except last slice) - sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ - uint32_t maxTemporalLayersMinus1; /**< [in]: Specifies the max temporal layer used for hierarchical coding. */ - NV_ENC_CONFIG_HEVC_VUI_PARAMETERS hevcVUIParameters; /**< [in]: Specifies the HEVC video usability info pamameters */ - uint32_t ltrTrustMode; /**< [in]: Specifies the LTR operating mode. See comments near NV_ENC_CONFIG_HEVC::enableLTR for description of the two modes. - Set to 1 to use "LTR Trust" mode of LTR operation. Clients are discouraged to use "LTR Trust" mode as this mode may - be deprecated in future releases. - Set to 0 when using "LTR Per Picture" mode of LTR operation. */ - NV_ENC_BFRAME_REF_MODE useBFramesAsRef; /**< [in]: Specifies the B-Frame as reference mode. Check support for useBFramesAsRef mode using ::NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE caps.*/ - uint32_t reserved1[216]; /**< [in]: Reserved and must be set to 0.*/ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG_HEVC; - -/** - * \struct _NV_ENC_CONFIG_H264_MEONLY - * H264 encoder configuration parameters for ME only Mode - * - */ -typedef struct _NV_ENC_CONFIG_H264_MEONLY -{ - uint32_t disablePartition16x16 :1; /**< [in]: Disable MotionEstimation on 16x16 blocks*/ - uint32_t disablePartition8x16 :1; /**< [in]: Disable MotionEstimation on 8x16 blocks*/ - uint32_t disablePartition16x8 :1; /**< [in]: Disable MotionEstimation on 16x8 blocks*/ - uint32_t disablePartition8x8 :1; /**< [in]: Disable MotionEstimation on 8x8 blocks*/ - uint32_t disableIntraSearch :1; /**< [in]: Disable Intra search during MotionEstimation*/ - uint32_t bStereoEnable :1; /**< [in]: Enable Stereo Mode for Motion Estimation where each view is independently executed*/ - uint32_t reserved :26; /**< [in]: Reserved and must be set to 0 */ - uint32_t reserved1 [255]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG_H264_MEONLY; - - -/** - * \struct _NV_ENC_CONFIG_HEVC_MEONLY - * HEVC encoder configuration parameters for ME only Mode - * - */ -typedef struct _NV_ENC_CONFIG_HEVC_MEONLY -{ - uint32_t reserved [256]; /**< [in]: Reserved and must be set to 0 */ - void* reserved1[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG_HEVC_MEONLY; - -/** - * \struct _NV_ENC_CODEC_CONFIG - * Codec-specific encoder configuration parameters to be set during initialization. - */ -typedef union _NV_ENC_CODEC_CONFIG -{ - NV_ENC_CONFIG_H264 h264Config; /**< [in]: Specifies the H.264-specific encoder configuration. */ - NV_ENC_CONFIG_HEVC hevcConfig; /**< [in]: Specifies the HEVC-specific encoder configuration. */ - NV_ENC_CONFIG_H264_MEONLY h264MeOnlyConfig; /**< [in]: Specifies the H.264-specific ME only encoder configuration. */ - NV_ENC_CONFIG_HEVC_MEONLY hevcMeOnlyConfig; /**< [in]: Specifies the HEVC-specific ME only encoder configuration. */ - uint32_t reserved[320]; /**< [in]: Reserved and must be set to 0 */ -} NV_ENC_CODEC_CONFIG; - - -/** - * \struct _NV_ENC_CONFIG - * Encoder configuration parameters to be set during initialization. - */ -typedef struct _NV_ENC_CONFIG -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CONFIG_VER. */ - GUID profileGUID; /**< [in]: Specifies the codec profile guid. If client specifies \p NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID the NvEncodeAPI interface will select the appropriate codec profile. */ - uint32_t gopLength; /**< [in]: Specifies the number of pictures in one GOP. Low latency application client can set goplength to NVENC_INFINITE_GOPLENGTH so that keyframes are not inserted automatically. */ - int32_t frameIntervalP; /**< [in]: Specifies the GOP pattern as follows: \p frameIntervalP = 0: I, 1: IPP, 2: IBP, 3: IBBP If goplength is set to NVENC_INFINITE_GOPLENGTH \p frameIntervalP should be set to 1. */ - uint32_t monoChromeEncoding; /**< [in]: Set this to 1 to enable monochrome encoding for this session. */ - NV_ENC_PARAMS_FRAME_FIELD_MODE frameFieldMode; /**< [in]: Specifies the frame/field mode. - Check support for field encoding using ::NV_ENC_CAPS_SUPPORT_FIELD_ENCODING caps. - Using a frameFieldMode other than NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME for RGB input is not supported. */ - NV_ENC_MV_PRECISION mvPrecision; /**< [in]: Specifies the desired motion vector prediction precision. */ - NV_ENC_RC_PARAMS rcParams; /**< [in]: Specifies the rate control parameters for the current encoding session. */ - NV_ENC_CODEC_CONFIG encodeCodecConfig; /**< [in]: Specifies the codec specific config parameters through this union. */ - uint32_t reserved [278]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG; - -/** macro for constructing the version field of ::_NV_ENC_CONFIG */ -#define NV_ENC_CONFIG_VER (NVENCAPI_STRUCT_VERSION(7) | ( 1<<31 )) - - -/** - * \struct _NV_ENC_INITIALIZE_PARAMS - * Encode Session Initialization parameters. - */ -typedef struct _NV_ENC_INITIALIZE_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_INITIALIZE_PARAMS_VER. */ - GUID encodeGUID; /**< [in]: Specifies the Encode GUID for which the encoder is being created. ::NvEncInitializeEncoder() API will fail if this is not set, or set to unsupported value. */ - GUID presetGUID; /**< [in]: Specifies the preset for encoding. If the preset GUID is set then , the preset configuration will be applied before any other parameter. */ - uint32_t encodeWidth; /**< [in]: Specifies the encode width. If not set ::NvEncInitializeEncoder() API will fail. */ - uint32_t encodeHeight; /**< [in]: Specifies the encode height. If not set ::NvEncInitializeEncoder() API will fail. */ - uint32_t darWidth; /**< [in]: Specifies the display aspect ratio Width. */ - uint32_t darHeight; /**< [in]: Specifies the display aspect ratio height. */ - uint32_t frameRateNum; /**< [in]: Specifies the numerator for frame rate used for encoding in frames per second ( Frame rate = frameRateNum / frameRateDen ). */ - uint32_t frameRateDen; /**< [in]: Specifies the denominator for frame rate used for encoding in frames per second ( Frame rate = frameRateNum / frameRateDen ). */ - uint32_t enableEncodeAsync; /**< [in]: Set this to 1 to enable asynchronous mode and is expected to use events to get picture completion notification. */ - uint32_t enablePTD; /**< [in]: Set this to 1 to enable the Picture Type Decision is be taken by the NvEncodeAPI interface. */ - uint32_t reportSliceOffsets :1; /**< [in]: Set this to 1 to enable reporting slice offsets in ::_NV_ENC_LOCK_BITSTREAM. NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync must be set to 0 to use this feature. Client must set this to 0 if NV_ENC_CONFIG_H264::sliceMode is 1 on Kepler GPUs */ - uint32_t enableSubFrameWrite :1; /**< [in]: Set this to 1 to write out available bitstream to memory at subframe intervals */ - uint32_t enableExternalMEHints :1; /**< [in]: Set to 1 to enable external ME hints for the current frame. For NV_ENC_INITIALIZE_PARAMS::enablePTD=1 with B frames, programming L1 hints is optional for B frames since Client doesn't know internal GOP structure. - NV_ENC_PIC_PARAMS::meHintRefPicDist should preferably be set with enablePTD=1. */ - uint32_t enableMEOnlyMode :1; /**< [in]: Set to 1 to enable ME Only Mode .*/ - uint32_t enableWeightedPrediction :1; /**< [in]: Set this to 1 to enable weighted prediction. Not supported if encode session is configured for B-Frames( 'frameIntervalP' in NV_ENC_CONFIG is greater than 1).*/ - uint32_t enableOutputInVidmem :1; /**< [in]: Set this to 1 to enable output of NVENC in video memory buffer created by application. This feature is not supported for HEVC ME only mode. */ - uint32_t reservedBitFields :26; /**< [in]: Reserved bitfields and must be set to 0 */ - uint32_t privDataSize; /**< [in]: Reserved private data buffer size and must be set to 0 */ - void* privData; /**< [in]: Reserved private data buffer and must be set to NULL */ - NV_ENC_CONFIG* encodeConfig; /**< [in]: Specifies the advanced codec specific structure. If client has sent a valid codec config structure, it will override parameters set by the NV_ENC_INITIALIZE_PARAMS::presetGUID parameter. If set to NULL the NvEncodeAPI interface will use the NV_ENC_INITIALIZE_PARAMS::presetGUID to set the codec specific parameters. - Client can also optionally query the NvEncodeAPI interface to get codec specific parameters for a presetGUID using ::NvEncGetEncodePresetConfig() API. It can then modify (if required) some of the codec config parameters and send down a custom config structure as part of ::_NV_ENC_INITIALIZE_PARAMS. - Even in this case client is recommended to pass the same preset guid it has used in ::NvEncGetEncodePresetConfig() API to query the config structure; as NV_ENC_INITIALIZE_PARAMS::presetGUID. This will not override the custom config structure but will be used to determine other Encoder HW specific parameters not exposed in the API. */ - uint32_t maxEncodeWidth; /**< [in]: Maximum encode width to be used for current Encode session. - Client should allocate output buffers according to this dimension for dynamic resolution change. If set to 0, Encoder will not allow dynamic resolution change. */ - uint32_t maxEncodeHeight; /**< [in]: Maximum encode height to be allowed for current Encode session. - Client should allocate output buffers according to this dimension for dynamic resolution change. If set to 0, Encode will not allow dynamic resolution change. */ - NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE maxMEHintCountsPerBlock[2]; /**< [in]: If Client wants to pass external motion vectors in NV_ENC_PIC_PARAMS::meExternalHints buffer it must specify the maximum number of hint candidates per block per direction for the encode session. - The NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[0] is for L0 predictors and NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[1] is for L1 predictors. - This client must also set NV_ENC_INITIALIZE_PARAMS::enableExternalMEHints to 1. */ - uint32_t reserved [289]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_INITIALIZE_PARAMS; - -/** macro for constructing the version field of ::_NV_ENC_INITIALIZE_PARAMS */ -#define NV_ENC_INITIALIZE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(5) | ( 1<<31 )) - - -/** - * \struct _NV_ENC_RECONFIGURE_PARAMS - * Encode Session Reconfigured parameters. - */ -typedef struct _NV_ENC_RECONFIGURE_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_RECONFIGURE_PARAMS_VER. */ - NV_ENC_INITIALIZE_PARAMS reInitEncodeParams; /**< [in]: Encoder session re-initialization parameters. - If reInitEncodeParams.encodeConfig is NULL and - reInitEncodeParams.presetGUID is the same as the preset - GUID specified on the call to NvEncInitializeEncoder(), - EncodeAPI will continue to use the existing encode - configuration. - If reInitEncodeParams.encodeConfig is NULL and - reInitEncodeParams.presetGUID is different from the preset - GUID specified on the call to NvEncInitializeEncoder(), - EncodeAPI will try to use the default configuration for - the preset specified by reInitEncodeParams.presetGUID. - In this case, reconfiguration may fail if the new - configuration is incompatible with the existing - configuration (e.g. the new configuration results in - a change in the GOP structure). */ - uint32_t resetEncoder :1; /**< [in]: This resets the rate control states and other internal encoder states. This should be used only with an IDR frame. - If NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1, encoder will force the frame type to IDR */ - uint32_t forceIDR :1; /**< [in]: Encode the current picture as an IDR picture. This flag is only valid when Picture type decision is taken by the Encoder - [_NV_ENC_INITIALIZE_PARAMS::enablePTD == 1]. */ - uint32_t reserved :30; - -}NV_ENC_RECONFIGURE_PARAMS; - -/** macro for constructing the version field of ::_NV_ENC_RECONFIGURE_PARAMS */ -#define NV_ENC_RECONFIGURE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(1) | ( 1<<31 )) - -/** - * \struct _NV_ENC_PRESET_CONFIG - * Encoder preset config - */ -typedef struct _NV_ENC_PRESET_CONFIG -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_PRESET_CONFIG_VER. */ - NV_ENC_CONFIG presetCfg; /**< [out]: preset config returned by the Nvidia Video Encoder interface. */ - uint32_t reserved1[255]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -}NV_ENC_PRESET_CONFIG; - -/** macro for constructing the version field of ::_NV_ENC_PRESET_CONFIG */ -#define NV_ENC_PRESET_CONFIG_VER (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 )) - - -/** - * \struct _NV_ENC_SEI_PAYLOAD - * User SEI message - */ -typedef struct _NV_ENC_SEI_PAYLOAD -{ - uint32_t payloadSize; /**< [in] SEI payload size in bytes. SEI payload must be byte aligned, as described in Annex D */ - uint32_t payloadType; /**< [in] SEI payload types and syntax can be found in Annex D of the H.264 Specification. */ - uint8_t *payload; /**< [in] pointer to user data */ -} NV_ENC_SEI_PAYLOAD; - -#define NV_ENC_H264_SEI_PAYLOAD NV_ENC_SEI_PAYLOAD - -/** - * \struct _NV_ENC_PIC_PARAMS_H264 - * H264 specific enc pic params. sent on a per frame basis. - */ -typedef struct _NV_ENC_PIC_PARAMS_H264 -{ - uint32_t displayPOCSyntax; /**< [in]: Specifies the display POC syntax This is required to be set if client is handling the picture type decision. */ - uint32_t reserved3; /**< [in]: Reserved and must be set to 0 */ - uint32_t refPicFlag; /**< [in]: Set to 1 for a reference picture. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ - uint32_t colourPlaneId; /**< [in]: Specifies the colour plane ID associated with the current input. */ - uint32_t forceIntraRefreshWithFrameCnt; /**< [in]: Forces an intra refresh with duration equal to intraRefreshFrameCnt. - When outputRecoveryPointSEI is set this is value is used for recovery_frame_cnt in recovery point SEI message - forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified */ - uint32_t constrainedFrame :1; /**< [in]: Set to 1 if client wants to encode this frame with each slice completely independent of other slices in the frame. - NV_ENC_INITIALIZE_PARAMS::enableConstrainedEncoding should be set to 1 */ - uint32_t sliceModeDataUpdate :1; /**< [in]: Set to 1 if client wants to change the sliceModeData field to specify new sliceSize Parameter - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting */ - uint32_t ltrMarkFrame :1; /**< [in]: Set to 1 if client wants to mark this frame as LTR */ - uint32_t ltrUseFrames :1; /**< [in]: Set to 1 if client allows encoding this frame using the LTR frames specified in ltrFrameBitmap */ - uint32_t reservedBitFields :28; /**< [in]: Reserved bit fields and must be set to 0 */ - uint8_t* sliceTypeData; /**< [in]: Deprecated. */ - uint32_t sliceTypeArrayCnt; /**< [in]: Deprecated. */ - uint32_t seiPayloadArrayCnt; /**< [in]: Specifies the number of elements allocated in seiPayloadArray array. */ - NV_ENC_SEI_PAYLOAD* seiPayloadArray; /**< [in]: Array of SEI payloads which will be inserted for this frame. */ - uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices - sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3, numSlices in Picture - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting - When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ - uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: - sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice) - sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) - sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice) - sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ - uint32_t ltrMarkFrameIdx; /**< [in]: Specifies the long term referenceframe index to use for marking this frame as LTR.*/ - uint32_t ltrUseFrameBitmap; /**< [in]: Specifies the the associated bitmap of LTR frame indices to use when encoding this frame. */ - uint32_t ltrUsageMode; /**< [in]: Not supported. Reserved for future use and must be set to 0. */ - uint32_t forceIntraSliceCount; /**< [in]: Specfies the number of slices to be forced to Intra in the current picture. - This option along with forceIntraSliceIdx[] array needs to be used with sliceMode = 3 only */ - uint32_t *forceIntraSliceIdx; /**< [in]: Slice indices to be forced to intra in the current picture. Each slice index should be <= num_slices_in_picture -1. Index starts from 0 for first slice. - The number of entries in this array should be equal to forceIntraSliceCount */ - uint32_t reserved [242]; /**< [in]: Reserved and must be set to 0. */ - void* reserved2[61]; /**< [in]: Reserved and must be set to NULL. */ -} NV_ENC_PIC_PARAMS_H264; - -/** - * \struct _NV_ENC_PIC_PARAMS_HEVC - * HEVC specific enc pic params. sent on a per frame basis. - */ -typedef struct _NV_ENC_PIC_PARAMS_HEVC -{ - uint32_t displayPOCSyntax; /**< [in]: Specifies the display POC syntax This is required to be set if client is handling the picture type decision. */ - uint32_t refPicFlag; /**< [in]: Set to 1 for a reference picture. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ - uint32_t temporalId; /**< [in]: Specifies the temporal id of the picture */ - uint32_t forceIntraRefreshWithFrameCnt; /**< [in]: Forces an intra refresh with duration equal to intraRefreshFrameCnt. - When outputRecoveryPointSEI is set this is value is used for recovery_frame_cnt in recovery point SEI message - forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified */ - uint32_t constrainedFrame :1; /**< [in]: Set to 1 if client wants to encode this frame with each slice completely independent of other slices in the frame. - NV_ENC_INITIALIZE_PARAMS::enableConstrainedEncoding should be set to 1 */ - uint32_t sliceModeDataUpdate :1; /**< [in]: Set to 1 if client wants to change the sliceModeData field to specify new sliceSize Parameter - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting */ - uint32_t ltrMarkFrame :1; /**< [in]: Set to 1 if client wants to mark this frame as LTR */ - uint32_t ltrUseFrames :1; /**< [in]: Set to 1 if client allows encoding this frame using the LTR frames specified in ltrFrameBitmap */ - uint32_t reservedBitFields :28; /**< [in]: Reserved bit fields and must be set to 0 */ - uint8_t* sliceTypeData; /**< [in]: Array which specifies the slice type used to force intra slice for a particular slice. Currently supported only for NV_ENC_CONFIG_H264::sliceMode == 3. - Client should allocate array of size sliceModeData where sliceModeData is specified in field of ::_NV_ENC_CONFIG_H264 - Array element with index n corresponds to nth slice. To force a particular slice to intra client should set corresponding array element to NV_ENC_SLICE_TYPE_I - all other array elements should be set to NV_ENC_SLICE_TYPE_DEFAULT */ - uint32_t sliceTypeArrayCnt; /**< [in]: Client should set this to the number of elements allocated in sliceTypeData array. If sliceTypeData is NULL then this should be set to 0 */ - uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices - sliceMode = 0 CTU based slices, sliceMode = 1 Byte based slices, sliceMode = 2 CTU row based slices, sliceMode = 3, numSlices in Picture - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting - When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ - uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: - sliceMode = 0, sliceModeData specifies # of CTUs in each slice (except last slice) - sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) - sliceMode = 2, sliceModeData specifies # of CTU rows in each slice (except last slice) - sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ - uint32_t ltrMarkFrameIdx; /**< [in]: Specifies the long term reference frame index to use for marking this frame as LTR.*/ - uint32_t ltrUseFrameBitmap; /**< [in]: Specifies the associated bitmap of LTR frame indices to use when encoding this frame. */ - uint32_t ltrUsageMode; /**< [in]: Not supported. Reserved for future use and must be set to 0. */ - uint32_t seiPayloadArrayCnt; /**< [in]: Specifies the number of elements allocated in seiPayloadArray array. */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0. */ - NV_ENC_SEI_PAYLOAD* seiPayloadArray; /**< [in]: Array of SEI payloads which will be inserted for this frame. */ - uint32_t reserved2 [244]; /**< [in]: Reserved and must be set to 0. */ - void* reserved3[61]; /**< [in]: Reserved and must be set to NULL. */ -} NV_ENC_PIC_PARAMS_HEVC; - -/** - * Codec specific per-picture encoding parameters. - */ -typedef union _NV_ENC_CODEC_PIC_PARAMS -{ - NV_ENC_PIC_PARAMS_H264 h264PicParams; /**< [in]: H264 encode picture params. */ - NV_ENC_PIC_PARAMS_HEVC hevcPicParams; /**< [in]: HEVC encode picture params. */ - uint32_t reserved[256]; /**< [in]: Reserved and must be set to 0. */ -} NV_ENC_CODEC_PIC_PARAMS; - -/** - * \struct _NV_ENC_PIC_PARAMS - * Encoding parameters that need to be sent on a per frame basis. - */ -typedef struct _NV_ENC_PIC_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_PIC_PARAMS_VER. */ - uint32_t inputWidth; /**< [in]: Specifies the input buffer width */ - uint32_t inputHeight; /**< [in]: Specifies the input buffer height */ - uint32_t inputPitch; /**< [in]: Specifies the input buffer pitch. If pitch value is not known, set this to inputWidth. */ - uint32_t encodePicFlags; /**< [in]: Specifies bit-wise OR`ed encode pic flags. See ::NV_ENC_PIC_FLAGS enum. */ - uint32_t frameIdx; /**< [in]: Specifies the frame index associated with the input frame [optional]. */ - uint64_t inputTimeStamp; /**< [in]: Specifies presentation timestamp associated with the input picture. */ - uint64_t inputDuration; /**< [in]: Specifies duration of the input picture */ - NV_ENC_INPUT_PTR inputBuffer; /**< [in]: Specifies the input buffer pointer. Client must use a pointer obtained from ::NvEncCreateInputBuffer() or ::NvEncMapInputResource() APIs.*/ - NV_ENC_OUTPUT_PTR outputBitstream; /**< [in]: Specifies the output buffer pointer. - If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 0, specifies the pointer to output buffer. Client should use a pointer obtained from ::NvEncCreateBitstreamBuffer() API. - If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 1, client should allocate buffer in video memory for NV_ENC_ENCODE_OUT_PARAMS struct and encoded bitstream data. Client - should use a pointer obtained from ::NvEncMapInputResource() API, when mapping this output buffer and assign it to NV_ENC_PIC_PARAMS::outputBitstream. - First 256 bytes of this buffer should be interpreted as NV_ENC_ENCODE_OUT_PARAMS struct followed by encoded bitstream data. Recommended size for output buffer is sum of size of - NV_ENC_ENCODE_OUT_PARAMS struct and twice the input frame size for lower resolution eg. CIF and 1.5 times the input frame size for higher resolutions. If encoded bitstream size is - greater than the allocated buffer size for encoded bitstream, then the output buffer will have encoded bitstream data equal to buffer size. All CUDA operations on this buffer must use - the default stream. */ - void* completionEvent; /**< [in]: Specifies an event to be signalled on completion of encoding of this Frame [only if operating in Asynchronous mode]. Each output buffer should be associated with a distinct event pointer. */ - NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Specifies the input buffer format. */ - NV_ENC_PIC_STRUCT pictureStruct; /**< [in]: Specifies structure of the input picture. */ - NV_ENC_PIC_TYPE pictureType; /**< [in]: Specifies input picture type. Client required to be set explicitly by the client if the client has not set NV_ENC_INITALIZE_PARAMS::enablePTD to 1 while calling NvInitializeEncoder. */ - NV_ENC_CODEC_PIC_PARAMS codecPicParams; /**< [in]: Specifies the codec specific per-picture encoding parameters. */ - NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE meHintCountsPerBlock[2]; /**< [in]: Specifies the number of hint candidates per block per direction for the current frame. meHintCountsPerBlock[0] is for L0 predictors and meHintCountsPerBlock[1] is for L1 predictors. - The candidate count in NV_ENC_PIC_PARAMS::meHintCountsPerBlock[lx] must never exceed NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[lx] provided during encoder intialization. */ - NVENC_EXTERNAL_ME_HINT *meExternalHints; /**< [in]: Specifies the pointer to ME external hints for the current frame. The size of ME hint buffer should be equal to number of macroblocks * the total number of candidates per macroblock. - The total number of candidates per MB per direction = 1*meHintCountsPerBlock[Lx].numCandsPerBlk16x16 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk16x8 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk8x8 - + 4*meHintCountsPerBlock[Lx].numCandsPerBlk8x8. For frames using bidirectional ME , the total number of candidates for single macroblock is sum of total number of candidates per MB for each direction (L0 and L1) */ - uint32_t reserved1[6]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[2]; /**< [in]: Reserved and must be set to NULL */ - int8_t *qpDeltaMap; /**< [in]: Specifies the pointer to signed byte array containing value per MB in raster scan order for the current picture, which will be Interperated depending on NV_ENC_RC_PARAMS::qpMapMode. - If NV_ENC_RC_PARAMS::qpMapMode is NV_ENC_QP_MAP_DELTA , This specify QP modifier to be applied on top of the QP chosen by rate control. - If NV_ENC_RC_PARAMS::qpMapMode is NV_ENC_QP_MAP_EMPHASIS, it specifies emphasis level map per MB. This level value along with QP chosen by rate control is used to compute the QP modifier, - which in turn is applied on top of QP chosen by rate control. - If NV_ENC_RC_PARAMS::qpMapMode is NV_ENC_QP_MAP_DISABLED value in qpDeltaMap will be ignored.*/ - uint32_t qpDeltaMapSize; /**< [in]: Specifies the size in bytes of qpDeltaMap surface allocated by client and pointed to by NV_ENC_PIC_PARAMS::qpDeltaMap. Surface (array) should be picWidthInMbs * picHeightInMbs */ - uint32_t reservedBitFields; /**< [in]: Reserved bitfields and must be set to 0 */ - uint16_t meHintRefPicDist[2]; /**< [in]: Specifies temporal distance for reference picture (NVENC_EXTERNAL_ME_HINT::refidx = 0) used during external ME with NV_ENC_INITALIZE_PARAMS::enablePTD = 1 . meHintRefPicDist[0] is for L0 hints and meHintRefPicDist[1] is for L1 hints. - If not set, will internally infer distance of 1. Ignored for NV_ENC_INITALIZE_PARAMS::enablePTD = 0 */ - uint32_t reserved3[286]; /**< [in]: Reserved and must be set to 0 */ - void* reserved4[60]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_PIC_PARAMS; - -/** Macro for constructing the version field of ::_NV_ENC_PIC_PARAMS */ -#define NV_ENC_PIC_PARAMS_VER (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 )) - - -/** - * \struct _NV_ENC_MEONLY_PARAMS - * MEOnly parameters that need to be sent on a per motion estimation basis. - * NV_ENC_MEONLY_PARAMS::meExternalHints is supported for H264 only. - */ -typedef struct _NV_ENC_MEONLY_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to NV_ENC_MEONLY_PARAMS_VER.*/ - uint32_t inputWidth; /**< [in]: Specifies the input buffer width */ - uint32_t inputHeight; /**< [in]: Specifies the input buffer height */ - NV_ENC_INPUT_PTR inputBuffer; /**< [in]: Specifies the input buffer pointer. Client must use a pointer obtained from NvEncCreateInputBuffer() or NvEncMapInputResource() APIs. */ - NV_ENC_INPUT_PTR referenceFrame; /**< [in]: Specifies the reference frame pointer */ - NV_ENC_OUTPUT_PTR mvBuffer; /**< [in]: Specifies the output buffer pointer. - If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 0, specifies the pointer to motion vector data buffer allocated by NvEncCreateMVBuffer. - Client must lock mvBuffer using ::NvEncLockBitstream() API to get the motion vector data. - If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 1, client should allocate buffer in video memory for storing the motion vector data. The size of this buffer must - be equal to total number of macroblocks multiplied by size of NV_ENC_H264_MV_DATA struct. Client should use a pointer obtained from ::NvEncMapInputResource() API, when mapping this - output buffer and assign it to NV_ENC_MEONLY_PARAMS::mvBuffer. All CUDA operations on this buffer must use the default stream. */ - NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Specifies the input buffer format. */ - void* completionEvent; /**< [in]: Specifies an event to be signalled on completion of motion estimation - of this Frame [only if operating in Asynchronous mode]. - Each output buffer should be associated with a distinct event pointer. */ - uint32_t viewID; /**< [in]: Specifies left,right viewID if NV_ENC_CONFIG_H264_MEONLY::bStereoEnable is set. - viewID can be 0,1 if bStereoEnable is set, 0 otherwise. */ - NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE - meHintCountsPerBlock[2]; /**< [in]: Specifies the number of hint candidates per block for the current frame. meHintCountsPerBlock[0] is for L0 predictors. - The candidate count in NV_ENC_PIC_PARAMS::meHintCountsPerBlock[lx] must never exceed NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[lx] provided during encoder intialization. */ - NVENC_EXTERNAL_ME_HINT *meExternalHints; /**< [in]: Specifies the pointer to ME external hints for the current frame. The size of ME hint buffer should be equal to number of macroblocks * the total number of candidates per macroblock. - The total number of candidates per MB per direction = 1*meHintCountsPerBlock[Lx].numCandsPerBlk16x16 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk16x8 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk8x8 - + 4*meHintCountsPerBlock[Lx].numCandsPerBlk8x8. For frames using bidirectional ME , the total number of candidates for single macroblock is sum of total number of candidates per MB for each direction (L0 and L1) */ - uint32_t reserved1[243]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[59]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_MEONLY_PARAMS; - -/** NV_ENC_MEONLY_PARAMS struct version*/ -#define NV_ENC_MEONLY_PARAMS_VER NVENCAPI_STRUCT_VERSION(3) - - -/** - * \struct _NV_ENC_LOCK_BITSTREAM - * Bitstream buffer lock parameters. - */ -typedef struct _NV_ENC_LOCK_BITSTREAM -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_LOCK_BITSTREAM_VER. */ - uint32_t doNotWait :1; /**< [in]: If this flag is set, the NvEncodeAPI interface will return buffer pointer even if operation is not completed. If not set, the call will block until operation completes. */ - uint32_t ltrFrame :1; /**< [out]: Flag indicating this frame is marked as LTR frame */ - uint32_t getRCStats :1; /**< [in]: If this flag is set then lockBitstream call will add additional intra-inter MB count and average MVX, MVY */ - uint32_t reservedBitFields :29; /**< [in]: Reserved bit fields and must be set to 0 */ - void* outputBitstream; /**< [in]: Pointer to the bitstream buffer being locked. */ - uint32_t* sliceOffsets; /**< [in,out]: Array which receives the slice offsets. This is not supported if NV_ENC_CONFIG_H264::sliceMode is 1 on Kepler GPUs. Array size must be equal to size of frame in MBs. */ - uint32_t frameIdx; /**< [out]: Frame no. for which the bitstream is being retrieved. */ - uint32_t hwEncodeStatus; /**< [out]: The NvEncodeAPI interface status for the locked picture. */ - uint32_t numSlices; /**< [out]: Number of slices in the encoded picture. Will be reported only if NV_ENC_INITIALIZE_PARAMS::reportSliceOffsets set to 1. */ - uint32_t bitstreamSizeInBytes; /**< [out]: Actual number of bytes generated and copied to the memory pointed by bitstreamBufferPtr. */ - uint64_t outputTimeStamp; /**< [out]: Presentation timestamp associated with the encoded output. */ - uint64_t outputDuration; /**< [out]: Presentation duration associates with the encoded output. */ - void* bitstreamBufferPtr; /**< [out]: Pointer to the generated output bitstream. - For MEOnly mode _NV_ENC_LOCK_BITSTREAM::bitstreamBufferPtr should be typecast to - NV_ENC_H264_MV_DATA/NV_ENC_HEVC_MV_DATA pointer respectively for H264/HEVC */ - NV_ENC_PIC_TYPE pictureType; /**< [out]: Picture type of the encoded picture. */ - NV_ENC_PIC_STRUCT pictureStruct; /**< [out]: Structure of the generated output picture. */ - uint32_t frameAvgQP; /**< [out]: Average QP of the frame. */ - uint32_t frameSatd; /**< [out]: Total SATD cost for whole frame. */ - uint32_t ltrFrameIdx; /**< [out]: Frame index associated with this LTR frame. */ - uint32_t ltrFrameBitmap; /**< [out]: Bitmap of LTR frames indices which were used for encoding this frame. Value of 0 if no LTR frames were used. */ - uint32_t reserved[13]; /**< [in]: Reserved and must be set to 0 */ - uint32_t intraMBCount; /**< [out]: For H264, Number of Intra MBs in the encoded frame. For HEVC, Number of Intra CTBs in the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ - uint32_t interMBCount; /**< [out]: For H264, Number of Inter MBs in the encoded frame, includes skip MBs. For HEVC, Number of Inter CTBs in the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ - int32_t averageMVX; /**< [out]: Average Motion Vector in X direction for the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ - int32_t averageMVY; /**< [out]: Average Motion Vector in y direction for the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ - uint32_t reserved1[219]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_LOCK_BITSTREAM; - -/** Macro for constructing the version field of ::_NV_ENC_LOCK_BITSTREAM */ -#define NV_ENC_LOCK_BITSTREAM_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * \struct _NV_ENC_LOCK_INPUT_BUFFER - * Uncompressed Input Buffer lock parameters. - */ -typedef struct _NV_ENC_LOCK_INPUT_BUFFER -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_LOCK_INPUT_BUFFER_VER. */ - uint32_t doNotWait :1; /**< [in]: Set to 1 to make ::NvEncLockInputBuffer() a unblocking call. If the encoding is not completed, driver will return ::NV_ENC_ERR_ENCODER_BUSY error code. */ - uint32_t reservedBitFields :31; /**< [in]: Reserved bitfields and must be set to 0 */ - NV_ENC_INPUT_PTR inputBuffer; /**< [in]: Pointer to the input buffer to be locked, client should pass the pointer obtained from ::NvEncCreateInputBuffer() or ::NvEncMapInputResource API. */ - void* bufferDataPtr; /**< [out]: Pointed to the locked input buffer data. Client can only access input buffer using the \p bufferDataPtr. */ - uint32_t pitch; /**< [out]: Pitch of the locked input buffer. */ - uint32_t reserved1[251]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_LOCK_INPUT_BUFFER; - -/** Macro for constructing the version field of ::_NV_ENC_LOCK_INPUT_BUFFER */ -#define NV_ENC_LOCK_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * \struct _NV_ENC_MAP_INPUT_RESOURCE - * Map an input resource to a Nvidia Encoder Input Buffer - */ -typedef struct _NV_ENC_MAP_INPUT_RESOURCE -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_MAP_INPUT_RESOURCE_VER. */ - uint32_t subResourceIndex; /**< [in]: Deprecated. Do not use. */ - void* inputResource; /**< [in]: Deprecated. Do not use. */ - NV_ENC_REGISTERED_PTR registeredResource; /**< [in]: The Registered resource handle obtained by calling NvEncRegisterInputResource. */ - NV_ENC_INPUT_PTR mappedResource; /**< [out]: Mapped pointer corresponding to the registeredResource. This pointer must be used in NV_ENC_PIC_PARAMS::inputBuffer parameter in ::NvEncEncodePicture() API. */ - NV_ENC_BUFFER_FORMAT mappedBufferFmt; /**< [out]: Buffer format of the outputResource. This buffer format must be used in NV_ENC_PIC_PARAMS::bufferFmt if client using the above mapped resource pointer. */ - uint32_t reserved1[251]; /**< [in]: Reserved and must be set to 0. */ - void* reserved2[63]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_MAP_INPUT_RESOURCE; - -/** Macro for constructing the version field of ::_NV_ENC_MAP_INPUT_RESOURCE */ -#define NV_ENC_MAP_INPUT_RESOURCE_VER NVENCAPI_STRUCT_VERSION(4) - -/** - * \struct _NV_ENC_INPUT_RESOURCE_OPENGL_TEX - * NV_ENC_REGISTER_RESOURCE::resourceToRegister must be a pointer to a variable of this type, - * when NV_ENC_REGISTER_RESOURCE::resourceType is NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX - */ -typedef struct _NV_ENC_INPUT_RESOURCE_OPENGL_TEX -{ - uint32_t texture; /**< [in]: The name of the texture to be used. */ - uint32_t target; /**< [in]: Accepted values are GL_TEXTURE_RECTANGLE and GL_TEXTURE_2D. */ -} NV_ENC_INPUT_RESOURCE_OPENGL_TEX; - -/** - * \struct _NV_ENC_REGISTER_RESOURCE - * Register a resource for future use with the Nvidia Video Encoder Interface. - */ -typedef struct _NV_ENC_REGISTER_RESOURCE -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_REGISTER_RESOURCE_VER. */ - NV_ENC_INPUT_RESOURCE_TYPE resourceType; /**< [in]: Specifies the type of resource to be registered. - Supported values are - ::NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX, - ::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, - ::NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX */ - uint32_t width; /**< [in]: Input buffer Width. */ - uint32_t height; /**< [in]: Input buffer Height. */ - uint32_t pitch; /**< [in]: Input buffer Pitch. - For ::NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX resources, set this to 0. - For ::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR resources, set this to - the pitch as obtained from cuMemAllocPitch(), or to the width in - bytes (if this resource was created by using cuMemAlloc()). This - value must be a multiple of 4. - For ::NV_ENC_INPUT_RESOURCE_TYPE_CUDAARRAY resources, set this to the - width of the allocation in bytes (i.e. - CUDA_ARRAY3D_DESCRIPTOR::Width * CUDA_ARRAY3D_DESCRIPTOR::NumChannels). - For ::NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX resources, set this to the - texture width multiplied by the number of components in the texture - format. */ - uint32_t subResourceIndex; /**< [in]: Subresource Index of the DirectX resource to be registered. Should be set to 0 for other interfaces. */ - void* resourceToRegister; /**< [in]: Handle to the resource that is being registered. */ - NV_ENC_REGISTERED_PTR registeredResource; /**< [out]: Registered resource handle. This should be used in future interactions with the Nvidia Video Encoder Interface. */ - NV_ENC_BUFFER_FORMAT bufferFormat; /**< [in]: Buffer format of resource to be registered. */ - NV_ENC_BUFFER_USAGE bufferUsage; /**< [in]: Usage of resource to be registered. */ - uint32_t reserved1[247]; /**< [in]: Reserved and must be set to 0. */ - void* reserved2[62]; /**< [in]: Reserved and must be set to NULL. */ -} NV_ENC_REGISTER_RESOURCE; - -/** Macro for constructing the version field of ::_NV_ENC_REGISTER_RESOURCE */ -#define NV_ENC_REGISTER_RESOURCE_VER NVENCAPI_STRUCT_VERSION(3) - -/** - * \struct _NV_ENC_STAT - * Encode Stats structure. - */ -typedef struct _NV_ENC_STAT -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_STAT_VER. */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ - NV_ENC_OUTPUT_PTR outputBitStream; /**< [out]: Specifies the pointer to output bitstream. */ - uint32_t bitStreamSize; /**< [out]: Size of generated bitstream in bytes. */ - uint32_t picType; /**< [out]: Picture type of encoded picture. See ::NV_ENC_PIC_TYPE. */ - uint32_t lastValidByteOffset; /**< [out]: Offset of last valid bytes of completed bitstream */ - uint32_t sliceOffsets[16]; /**< [out]: Offsets of each slice */ - uint32_t picIdx; /**< [out]: Picture number */ - uint32_t reserved1[233]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_STAT; - -/** Macro for constructing the version field of ::_NV_ENC_STAT */ -#define NV_ENC_STAT_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * \struct _NV_ENC_SEQUENCE_PARAM_PAYLOAD - * Sequence and picture paramaters payload. - */ -typedef struct _NV_ENC_SEQUENCE_PARAM_PAYLOAD -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_INITIALIZE_PARAMS_VER. */ - uint32_t inBufferSize; /**< [in]: Specifies the size of the spsppsBuffer provied by the client */ - uint32_t spsId; /**< [in]: Specifies the SPS id to be used in sequence header. Default value is 0. */ - uint32_t ppsId; /**< [in]: Specifies the PPS id to be used in picture header. Default value is 0. */ - void* spsppsBuffer; /**< [in]: Specifies bitstream header pointer of size NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize. It is the client's responsibility to manage this memory. */ - uint32_t* outSPSPPSPayloadSize; /**< [out]: Size of the sequence and picture header in bytes written by the NvEncodeAPI interface to the SPSPPSBuffer. */ - uint32_t reserved [250]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_SEQUENCE_PARAM_PAYLOAD; - -/** Macro for constructing the version field of ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD */ -#define NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * Event registration/unregistration parameters. - */ -typedef struct _NV_ENC_EVENT_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_EVENT_PARAMS_VER. */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ - void* completionEvent; /**< [in]: Handle to event to be registered/unregistered with the NvEncodeAPI interface. */ - uint32_t reserved1[253]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_EVENT_PARAMS; - -/** Macro for constructing the version field of ::_NV_ENC_EVENT_PARAMS */ -#define NV_ENC_EVENT_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * Encoder Session Creation parameters - */ -typedef struct _NV_ENC_OPEN_ENCODE_SESSIONEX_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER. */ - NV_ENC_DEVICE_TYPE deviceType; /**< [in]: Specified the device Type */ - void* device; /**< [in]: Pointer to client device. */ - void* reserved; /**< [in]: Reserved and must be set to 0. */ - uint32_t apiVersion; /**< [in]: API version. Should be set to NVENCAPI_VERSION. */ - uint32_t reserved1[253]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS; -/** Macro for constructing the version field of ::_NV_ENC_OPEN_ENCODE_SESSIONEX_PARAMS */ -#define NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) - -/** @} */ /* END ENCODER_STRUCTURE */ - - -/** - * \addtogroup ENCODE_FUNC NvEncodeAPI Functions - * @{ - */ - -// NvEncOpenEncodeSession -/** - * \brief Opens an encoding session. - * - * Deprecated. - * - * \return - * ::NV_ENC_ERR_INVALID_CALL\n - * - */ -NVENCSTATUS NVENCAPI NvEncOpenEncodeSession (void* device, uint32_t deviceType, void** encoder); - -// NvEncGetEncodeGuidCount -/** - * \brief Retrieves the number of supported encode GUIDs. - * - * The function returns the number of codec guids supported by the NvEncodeAPI - * interface. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [out] encodeGUIDCount - * Number of supported encode GUIDs. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeGUIDCount (void* encoder, uint32_t* encodeGUIDCount); - - -// NvEncGetEncodeGUIDs -/** - * \brief Retrieves an array of supported encoder codec GUIDs. - * - * The function returns an array of codec guids supported by the NvEncodeAPI interface. - * The client must allocate an array where the NvEncodeAPI interface can - * fill the supported guids and pass the pointer in \p *GUIDs parameter. - * The size of the array can be determined by using ::NvEncGetEncodeGUIDCount() API. - * The Nvidia Encoding interface returns the number of codec guids it has actually - * filled in the guid array in the \p GUIDCount parameter. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] guidArraySize - * Number of GUIDs to retrieved. Should be set to the number retrieved using - * ::NvEncGetEncodeGUIDCount. - * \param [out] GUIDs - * Array of supported Encode GUIDs. - * \param [out] GUIDCount - * Number of supported Encode GUIDs. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeGUIDs (void* encoder, GUID* GUIDs, uint32_t guidArraySize, uint32_t* GUIDCount); - - -// NvEncGetEncodeProfileGuidCount -/** - * \brief Retrieves the number of supported profile GUIDs. - * - * The function returns the number of profile GUIDs supported for a given codec. - * The client must first enumerate the codec guids supported by the NvEncodeAPI - * interface. After determining the codec guid, it can query the NvEncodeAPI - * interface to determine the number of profile guids supported for a particular - * codec guid. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * The codec guid for which the profile guids are being enumerated. - * \param [out] encodeProfileGUIDCount - * Number of encode profiles supported for the given encodeGUID. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeProfileGUIDCount (void* encoder, GUID encodeGUID, uint32_t* encodeProfileGUIDCount); - - -// NvEncGetEncodeProfileGUIDs -/** - * \brief Retrieves an array of supported encode profile GUIDs. - * - * The function returns an array of supported profile guids for a particular - * codec guid. The client must allocate an array where the NvEncodeAPI interface - * can populate the profile guids. The client can determine the array size using - * ::NvEncGetEncodeProfileGUIDCount() API. The client must also validiate that the - * NvEncodeAPI interface supports the GUID the client wants to pass as \p encodeGUID - * parameter. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * The encode guid whose profile guids are being enumerated. - * \param [in] guidArraySize - * Number of GUIDs to be retrieved. Should be set to the number retrieved using - * ::NvEncGetEncodeProfileGUIDCount. - * \param [out] profileGUIDs - * Array of supported Encode Profile GUIDs - * \param [out] GUIDCount - * Number of valid encode profile GUIDs in \p profileGUIDs array. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeProfileGUIDs (void* encoder, GUID encodeGUID, GUID* profileGUIDs, uint32_t guidArraySize, uint32_t* GUIDCount); - -// NvEncGetInputFormatCount -/** - * \brief Retrieve the number of supported Input formats. - * - * The function returns the number of supported input formats. The client must - * query the NvEncodeAPI interface to determine the supported input formats - * before creating the input surfaces. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the number of supported input formats - * is to be retrieved. - * \param [out] inputFmtCount - * Number of input formats supported for specified Encode GUID. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncGetInputFormatCount (void* encoder, GUID encodeGUID, uint32_t* inputFmtCount); - - -// NvEncGetInputFormats -/** - * \brief Retrieves an array of supported Input formats - * - * Returns an array of supported input formats The client must use the input - * format to create input surface using ::NvEncCreateInputBuffer() API. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the number of supported input formats - * is to be retrieved. - *\param [in] inputFmtArraySize - * Size input format count array passed in \p inputFmts. - *\param [out] inputFmts - * Array of input formats supported for this Encode GUID. - *\param [out] inputFmtCount - * The number of valid input format types returned by the NvEncodeAPI - * interface in \p inputFmts array. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetInputFormats (void* encoder, GUID encodeGUID, NV_ENC_BUFFER_FORMAT* inputFmts, uint32_t inputFmtArraySize, uint32_t* inputFmtCount); - - -// NvEncGetEncodeCaps -/** - * \brief Retrieves the capability value for a specified encoder attribute. - * - * The function returns the capability value for a given encoder attribute. The - * client must validate the encodeGUID using ::NvEncGetEncodeGUIDs() API before - * calling this function. The encoder attribute being queried are enumerated in - * ::NV_ENC_CAPS_PARAM enum. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the capability attribute is to be retrieved. - * \param [in] capsParam - * Used to specify attribute being queried. Refer ::NV_ENC_CAPS_PARAM for more - * details. - * \param [out] capsVal - * The value corresponding to the capability attribute being queried. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeCaps (void* encoder, GUID encodeGUID, NV_ENC_CAPS_PARAM* capsParam, int* capsVal); - - -// NvEncGetEncodePresetCount -/** - * \brief Retrieves the number of supported preset GUIDs. - * - * The function returns the number of preset GUIDs available for a given codec. - * The client must validate the codec guid using ::NvEncGetEncodeGUIDs() API - * before calling this function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the number of supported presets is to - * be retrieved. - * \param [out] encodePresetGUIDCount - * Receives the number of supported preset GUIDs. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodePresetCount (void* encoder, GUID encodeGUID, uint32_t* encodePresetGUIDCount); - - -// NvEncGetEncodePresetGUIDs -/** - * \brief Receives an array of supported encoder preset GUIDs. - * - * The function returns an array of encode preset guids available for a given codec. - * The client can directly use one of the preset guids based upon the use case - * or target device. The preset guid chosen can be directly used in - * NV_ENC_INITIALIZE_PARAMS::presetGUID parameter to ::NvEncEncodePicture() API. - * Alternately client can also use the preset guid to retrieve the encoding config - * parameters being used by NvEncodeAPI interface for that given preset, using - * ::NvEncGetEncodePresetConfig() API. It can then modify preset config parameters - * as per its use case and send it to NvEncodeAPI interface as part of - * NV_ENC_INITIALIZE_PARAMS::encodeConfig parameter for NvEncInitializeEncoder() - * API. - * - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the list of supported presets is to be - * retrieved. - * \param [in] guidArraySize - * Size of array of preset guids passed in \p preset GUIDs - * \param [out] presetGUIDs - * Array of supported Encode preset GUIDs from the NvEncodeAPI interface - * to client. - * \param [out] encodePresetGUIDCount - * Receives the number of preset GUIDs returned by the NvEncodeAPI - * interface. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodePresetGUIDs (void* encoder, GUID encodeGUID, GUID* presetGUIDs, uint32_t guidArraySize, uint32_t* encodePresetGUIDCount); - - -// NvEncGetEncodePresetConfig -/** - * \brief Returns a preset config structure supported for given preset GUID. - * - * The function returns a preset config structure for a given preset guid. Before - * using this function the client must enumerate the preset guids available for - * a given codec. The preset config structure can be modified by the client depending - * upon its use case and can be then used to initialize the encoder using - * ::NvEncInitializeEncoder() API. The client can use this function only if it - * wants to modify the NvEncodeAPI preset configuration, otherwise it can - * directly use the preset guid. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the list of supported presets is to be - * retrieved. - * \param [in] presetGUID - * Preset GUID, corresponding to which the Encoding configurations is to be - * retrieved. - * \param [out] presetConfig - * The requested Preset Encoder Attribute set. Refer ::_NV_ENC_CONFIG for -* more details. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodePresetConfig (void* encoder, GUID encodeGUID, GUID presetGUID, NV_ENC_PRESET_CONFIG* presetConfig); - -// NvEncInitializeEncoder -/** - * \brief Initialize the encoder. - * - * This API must be used to initialize the encoder. The initialization parameter - * is passed using \p *createEncodeParams The client must send the following - * fields of the _NV_ENC_INITIALIZE_PARAMS structure with a valid value. - * - NV_ENC_INITIALIZE_PARAMS::encodeGUID - * - NV_ENC_INITIALIZE_PARAMS::encodeWidth - * - NV_ENC_INITIALIZE_PARAMS::encodeHeight - * - * The client can pass a preset guid directly to the NvEncodeAPI interface using - * NV_ENC_INITIALIZE_PARAMS::presetGUID field. If the client doesn't pass - * NV_ENC_INITIALIZE_PARAMS::encodeConfig structure, the codec specific parameters - * will be selected based on the preset guid. The preset guid must have been - * validated by the client using ::NvEncGetEncodePresetGUIDs() API. - * If the client passes a custom ::_NV_ENC_CONFIG structure through - * NV_ENC_INITIALIZE_PARAMS::encodeConfig , it will override the codec specific parameters - * based on the preset guid. It is recommended that even if the client passes a custom config, - * it should also send a preset guid. In this case, the preset guid passed by the client - * will not override any of the custom config parameters programmed by the client, - * it is only used as a hint by the NvEncodeAPI interface to determine certain encoder parameters - * which are not exposed to the client. - * - * There are two modes of operation for the encoder namely: - * - Asynchronous mode - * - Synchronous mode - * - * The client can select asynchronous or synchronous mode by setting the \p - * enableEncodeAsync field in ::_NV_ENC_INITIALIZE_PARAMS to 1 or 0 respectively. - *\par Asynchronous mode of operation: - * The Asynchronous mode can be enabled by setting NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 1. - * The client operating in asynchronous mode must allocate completion event object - * for each output buffer and pass the completion event object in the - * ::NvEncEncodePicture() API. The client can create another thread and wait on - * the event object to be signalled by NvEncodeAPI interface on completion of the - * encoding process for the output frame. This should unblock the main thread from - * submitting work to the encoder. When the event is signalled the client can call - * NvEncodeAPI interfaces to copy the bitstream data using ::NvEncLockBitstream() - * API. This is the preferred mode of operation. - * - * NOTE: Asynchronous mode is not supported on Linux. - * - *\par Synchronous mode of operation: - * The client can select synchronous mode by setting NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 0. - * The client working in synchronous mode can work in a single threaded or multi - * threaded mode. The client need not allocate any event objects. The client can - * only lock the bitstream data after NvEncodeAPI interface has returned - * ::NV_ENC_SUCCESS from encode picture. The NvEncodeAPI interface can return - * ::NV_ENC_ERR_NEED_MORE_INPUT error code from ::NvEncEncodePicture() API. The - * client must not lock the output buffer in such case but should send the next - * frame for encoding. The client must keep on calling ::NvEncEncodePicture() API - * until it returns ::NV_ENC_SUCCESS. \n - * The client must always lock the bitstream data in order in which it has submitted. - * This is true for both asynchronous and synchronous mode. - * - *\par Picture type decision: - * If the client is taking the picture type decision and it must disable the picture - * type decision module in NvEncodeAPI by setting NV_ENC_INITIALIZE_PARAMS::enablePTD - * to 0. In this case the client is required to send the picture in encoding - * order to NvEncodeAPI by doing the re-ordering for B frames. \n - * If the client doesn't want to take the picture type decision it can enable - * picture type decision module in the NvEncodeAPI interface by setting - * NV_ENC_INITIALIZE_PARAMS::enablePTD to 1 and send the input pictures in display - * order. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] createEncodeParams - * Refer ::_NV_ENC_INITIALIZE_PARAMS for details. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncInitializeEncoder (void* encoder, NV_ENC_INITIALIZE_PARAMS* createEncodeParams); - - -// NvEncCreateInputBuffer -/** - * \brief Allocates Input buffer. - * - * This function is used to allocate an input buffer. The client must enumerate - * the input buffer format before allocating the input buffer resources. The - * NV_ENC_INPUT_PTR returned by the NvEncodeAPI interface in the - * NV_ENC_CREATE_INPUT_BUFFER::inputBuffer field can be directly used in - * ::NvEncEncodePicture() API. The number of input buffers to be allocated by the - * client must be at least 4 more than the number of B frames being used for encoding. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] createInputBufferParams - * Pointer to the ::NV_ENC_CREATE_INPUT_BUFFER structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncCreateInputBuffer (void* encoder, NV_ENC_CREATE_INPUT_BUFFER* createInputBufferParams); - - -// NvEncDestroyInputBuffer -/** - * \brief Release an input buffers. - * - * This function is used to free an input buffer. If the client has allocated - * any input buffer using ::NvEncCreateInputBuffer() API, it must free those - * input buffers by calling this function. The client must release the input - * buffers before destroying the encoder using ::NvEncDestroyEncoder() API. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] inputBuffer - * Pointer to the input buffer to be released. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncDestroyInputBuffer (void* encoder, NV_ENC_INPUT_PTR inputBuffer); - - -// NvEncCreateBitstreamBuffer -/** - * \brief Allocates an output bitstream buffer - * - * This function is used to allocate an output bitstream buffer and returns a - * NV_ENC_OUTPUT_PTR to bitstream buffer to the client in the - * NV_ENC_CREATE_BITSTREAM_BUFFER::bitstreamBuffer field. - * The client can only call this function after the encoder session has been - * initialized using ::NvEncInitializeEncoder() API. The minimum number of output - * buffers allocated by the client must be at least 4 more than the number of B - * B frames being used for encoding. The client can only access the output - * bitsteam data by locking the \p bitstreamBuffer using the ::NvEncLockBitstream() - * function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] createBitstreamBufferParams - * Pointer ::NV_ENC_CREATE_BITSTREAM_BUFFER for details. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncCreateBitstreamBuffer (void* encoder, NV_ENC_CREATE_BITSTREAM_BUFFER* createBitstreamBufferParams); - - -// NvEncDestroyBitstreamBuffer -/** - * \brief Release a bitstream buffer. - * - * This function is used to release the output bitstream buffer allocated using - * the ::NvEncCreateBitstreamBuffer() function. The client must release the output - * bitstreamBuffer using this function before destroying the encoder session. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] bitstreamBuffer - * Pointer to the bitstream buffer being released. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncDestroyBitstreamBuffer (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer); - -// NvEncEncodePicture -/** - * \brief Submit an input picture for encoding. - * - * This function is used to submit an input picture buffer for encoding. The - * encoding parameters are passed using \p *encodePicParams which is a pointer - * to the ::_NV_ENC_PIC_PARAMS structure. - * - * If the client has set NV_ENC_INITIALIZE_PARAMS::enablePTD to 0, then it must - * send a valid value for the following fields. - * - NV_ENC_PIC_PARAMS::pictureType - * - NV_ENC_PIC_PARAMS_H264::displayPOCSyntax (H264 only) - * - NV_ENC_PIC_PARAMS_H264::frameNumSyntax(H264 only) - * - NV_ENC_PIC_PARAMS_H264::refPicFlag(H264 only) - * - * - *\par Asynchronous Encoding - * If the client has enabled asynchronous mode of encoding by setting - * NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 1 in the ::NvEncInitializeEncoder() - * API ,then the client must send a valid NV_ENC_PIC_PARAMS::completionEvent. - * Incase of asynchronous mode of operation, client can queue the ::NvEncEncodePicture() - * API commands from the main thread and then queue output buffers to be processed - * to a secondary worker thread. Before the locking the output buffers in the - * secondary thread , the client must wait on NV_ENC_PIC_PARAMS::completionEvent - * it has queued in ::NvEncEncodePicture() API call. The client must always process - * completion event and the output buffer in the same order in which they have been - * submitted for encoding. The NvEncodeAPI interface is responsible for any - * re-ordering required for B frames and will always ensure that encoded bitstream - * data is written in the same order in which output buffer is submitted. - *\code - The below example shows how asynchronous encoding in case of 1 B frames - ------------------------------------------------------------------------ - Suppose the client allocated 4 input buffers(I1,I2..), 4 output buffers(O1,O2..) - and 4 completion events(E1, E2, ...). The NvEncodeAPI interface will need to - keep a copy of the input buffers for re-ordering and it allocates following - internal buffers (NvI1, NvI2...). These internal buffers are managed by NvEncodeAPI - and the client is not responsible for the allocating or freeing the memory of - the internal buffers. - - a) The client main thread will queue the following encode frame calls. - Note the picture type is unknown to the client, the decision is being taken by - NvEncodeAPI interface. The client should pass ::_NV_ENC_PIC_PARAMS parameter - consisting of allocated input buffer, output buffer and output events in successive - ::NvEncEncodePicture() API calls along with other required encode picture params. - For example: - 1st EncodePicture parameters - (I1, O1, E1) - 2nd EncodePicture parameters - (I2, O2, E2) - 3rd EncodePicture parameters - (I3, O3, E3) - - b) NvEncodeAPI SW will receive the following encode Commands from the client. - The left side shows input from client in the form (Input buffer, Output Buffer, - Output Event). The right hand side shows a possible picture type decision take by - the NvEncodeAPI interface. - (I1, O1, E1) ---P1 Frame - (I2, O2, E2) ---B2 Frame - (I3, O3, E3) ---P3 Frame - - c) NvEncodeAPI interface will make a copy of the input buffers to its internal - buffersfor re-ordering. These copies are done as part of nvEncEncodePicture - function call from the client and NvEncodeAPI interface is responsible for - synchronization of copy operation with the actual encoding operation. - I1 --> NvI1 - I2 --> NvI2 - I3 --> NvI3 - - d) After returning from ::NvEncEncodePicture() call , the client must queue the output - bitstream processing work to the secondary thread. The output bitstream processing - for asynchronous mode consist of first waiting on completion event(E1, E2..) - and then locking the output bitstream buffer(O1, O2..) for reading the encoded - data. The work queued to the secondary thread by the client is in the following order - (I1, O1, E1) - (I2, O2, E2) - (I3, O3, E3) - Note they are in the same order in which client calls ::NvEncEncodePicture() API - in \p step a). - - e) NvEncodeAPI interface will do the re-ordering such that Encoder HW will receive - the following encode commands: - (NvI1, O1, E1) ---P1 Frame - (NvI3, O2, E2) ---P3 Frame - (NvI2, O3, E3) ---B2 frame - - f) After the encoding operations are completed, the events will be signalled - by NvEncodeAPI interface in the following order : - (O1, E1) ---P1 Frame ,output bitstream copied to O1 and event E1 signalled. - (O2, E2) ---P3 Frame ,output bitstream copied to O2 and event E2 signalled. - (O3, E3) ---B2 Frame ,output bitstream copied to O3 and event E3 signalled. - - g) The client must lock the bitstream data using ::NvEncLockBitstream() API in - the order O1,O2,O3 to read the encoded data, after waiting for the events - to be signalled in the same order i.e E1, E2 and E3.The output processing is - done in the secondary thread in the following order: - Waits on E1, copies encoded bitstream from O1 - Waits on E2, copies encoded bitstream from O2 - Waits on E3, copies encoded bitstream from O3 - - -Note the client will receive the events signalling and output buffer in the - same order in which they have submitted for encoding. - -Note the LockBitstream will have picture type field which will notify the - output picture type to the clients. - -Note the input, output buffer and the output completion event are free to be - reused once NvEncodeAPI interfaced has signalled the event and the client has - copied the data from the output buffer. - - * \endcode - * - *\par Synchronous Encoding - * The client can enable synchronous mode of encoding by setting - * NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 0 in ::NvEncInitializeEncoder() API. - * The NvEncodeAPI interface may return ::NV_ENC_ERR_NEED_MORE_INPUT error code for - * some ::NvEncEncodePicture() API calls when NV_ENC_INITIALIZE_PARAMS::enablePTD - * is set to 1, but the client must not treat it as a fatal error. The NvEncodeAPI - * interface might not be able to submit an input picture buffer for encoding - * immediately due to re-ordering for B frames. The NvEncodeAPI interface cannot - * submit the input picture which is decided to be encoded as B frame as it waits - * for backward reference from temporally subsequent frames. This input picture - * is buffered internally and waits for more input picture to arrive. The client - * must not call ::NvEncLockBitstream() API on the output buffers whose - * ::NvEncEncodePicture() API returns ::NV_ENC_ERR_NEED_MORE_INPUT. The client must - * wait for the NvEncodeAPI interface to return ::NV_ENC_SUCCESS before locking the - * output bitstreams to read the encoded bitstream data. The following example - * explains the scenario with synchronous encoding with 2 B frames. - *\code - The below example shows how synchronous encoding works in case of 1 B frames - ----------------------------------------------------------------------------- - Suppose the client allocated 4 input buffers(I1,I2..), 4 output buffers(O1,O2..) - and 4 completion events(E1, E2, ...). The NvEncodeAPI interface will need to - keep a copy of the input buffers for re-ordering and it allocates following - internal buffers (NvI1, NvI2...). These internal buffers are managed by NvEncodeAPI - and the client is not responsible for the allocating or freeing the memory of - the internal buffers. - - The client calls ::NvEncEncodePicture() API with input buffer I1 and output buffer O1. - The NvEncodeAPI decides to encode I1 as P frame and submits it to encoder - HW and returns ::NV_ENC_SUCCESS. - The client can now read the encoded data by locking the output O1 by calling - NvEncLockBitstream API. - - The client calls ::NvEncEncodePicture() API with input buffer I2 and output buffer O2. - The NvEncodeAPI decides to encode I2 as B frame and buffers I2 by copying it - to internal buffer and returns ::NV_ENC_ERR_NEED_MORE_INPUT. - The error is not fatal and it notifies client that it cannot read the encoded - data by locking the output O2 by calling ::NvEncLockBitstream() API without submitting - more work to the NvEncodeAPI interface. - - The client calls ::NvEncEncodePicture() with input buffer I3 and output buffer O3. - The NvEncodeAPI decides to encode I3 as P frame and it first submits I3 for - encoding which will be used as backward reference frame for I2. - The NvEncodeAPI then submits I2 for encoding and returns ::NV_ENC_SUCESS. Both - the submission are part of the same ::NvEncEncodePicture() function call. - The client can now read the encoded data for both the frames by locking the output - O2 followed by O3 ,by calling ::NvEncLockBitstream() API. - - The client must always lock the output in the same order in which it has submitted - to receive the encoded bitstream in correct encoding order. - - * \endcode - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] encodePicParams - * Pointer to the ::_NV_ENC_PIC_PARAMS structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_ENCODER_BUSY \n - * ::NV_ENC_ERR_NEED_MORE_INPUT \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncEncodePicture (void* encoder, NV_ENC_PIC_PARAMS* encodePicParams); - - -// NvEncLockBitstream -/** - * \brief Lock output bitstream buffer - * - * This function is used to lock the bitstream buffer to read the encoded data. - * The client can only access the encoded data by calling this function. - * The pointer to client accessible encoded data is returned in the - * NV_ENC_LOCK_BITSTREAM::bitstreamBufferPtr field. The size of the encoded data - * in the output buffer is returned in the NV_ENC_LOCK_BITSTREAM::bitstreamSizeInBytes - * The NvEncodeAPI interface also returns the output picture type and picture structure - * of the encoded frame in NV_ENC_LOCK_BITSTREAM::pictureType and - * NV_ENC_LOCK_BITSTREAM::pictureStruct fields respectively. If the client has - * set NV_ENC_LOCK_BITSTREAM::doNotWait to 1, the function might return - * ::NV_ENC_ERR_LOCK_BUSY if client is operating in synchronous mode. This is not - * a fatal failure if NV_ENC_LOCK_BITSTREAM::doNotWait is set to 1. In the above case the client can - * retry the function after few milliseconds. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] lockBitstreamBufferParams - * Pointer to the ::_NV_ENC_LOCK_BITSTREAM structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_LOCK_BUSY \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncLockBitstream (void* encoder, NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams); - - -// NvEncUnlockBitstream -/** - * \brief Unlock the output bitstream buffer - * - * This function is used to unlock the output bitstream buffer after the client - * has read the encoded data from output buffer. The client must call this function - * to unlock the output buffer which it has previously locked using ::NvEncLockBitstream() - * function. Using a locked bitstream buffer in ::NvEncEncodePicture() API will cause - * the function to fail. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] bitstreamBuffer - * bitstream buffer pointer being unlocked - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncUnlockBitstream (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer); - - -// NvLockInputBuffer -/** - * \brief Locks an input buffer - * - * This function is used to lock the input buffer to load the uncompressed YUV - * pixel data into input buffer memory. The client must pass the NV_ENC_INPUT_PTR - * it had previously allocated using ::NvEncCreateInputBuffer()in the - * NV_ENC_LOCK_INPUT_BUFFER::inputBuffer field. - * The NvEncodeAPI interface returns pointer to client accessible input buffer - * memory in NV_ENC_LOCK_INPUT_BUFFER::bufferDataPtr field. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] lockInputBufferParams - * Pointer to the ::_NV_ENC_LOCK_INPUT_BUFFER structure - * - * \return - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_LOCK_BUSY \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncLockInputBuffer (void* encoder, NV_ENC_LOCK_INPUT_BUFFER* lockInputBufferParams); - - -// NvUnlockInputBuffer -/** - * \brief Unlocks the input buffer - * - * This function is used to unlock the input buffer memory previously locked for - * uploading YUV pixel data. The input buffer must be unlocked before being used - * again for encoding, otherwise NvEncodeAPI will fail the ::NvEncEncodePicture() - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] inputBuffer - * Pointer to the input buffer that is being unlocked. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - * - */ -NVENCSTATUS NVENCAPI NvEncUnlockInputBuffer (void* encoder, NV_ENC_INPUT_PTR inputBuffer); - - -// NvEncGetEncodeStats -/** - * \brief Get encoding statistics. - * - * This function is used to retrieve the encoding statistics. - * This API is not supported when encode device type is CUDA. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] encodeStats - * Pointer to the ::_NV_ENC_STAT structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeStats (void* encoder, NV_ENC_STAT* encodeStats); - - -// NvEncGetSequenceParams -/** - * \brief Get encoded sequence and picture header. - * - * This function can be used to retrieve the sequence and picture header out of - * band. The client must call this function only after the encoder has been - * initialized using ::NvEncInitializeEncoder() function. The client must - * allocate the memory where the NvEncodeAPI interface can copy the bitstream - * header and pass the pointer to the memory in NV_ENC_SEQUENCE_PARAM_PAYLOAD::spsppsBuffer. - * The size of buffer is passed in the field NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize. - * The NvEncodeAPI interface will copy the bitstream header payload and returns - * the actual size of the bitstream header in the field - * NV_ENC_SEQUENCE_PARAM_PAYLOAD::outSPSPPSPayloadSize. - * The client must call ::NvEncGetSequenceParams() function from the same thread which is - * being used to call ::NvEncEncodePicture() function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] sequenceParamPayload - * Pointer to the ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetSequenceParams (void* encoder, NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); - - -// NvEncRegisterAsyncEvent -/** - * \brief Register event for notification to encoding completion. - * - * This function is used to register the completion event with NvEncodeAPI - * interface. The event is required when the client has configured the encoder to - * work in asynchronous mode. In this mode the client needs to send a completion - * event with every output buffer. The NvEncodeAPI interface will signal the - * completion of the encoding process using this event. Only after the event is - * signalled the client can get the encoded data using ::NvEncLockBitstream() function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] eventParams - * Pointer to the ::_NV_ENC_EVENT_PARAMS structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncRegisterAsyncEvent (void* encoder, NV_ENC_EVENT_PARAMS* eventParams); - - -// NvEncUnregisterAsyncEvent -/** - * \brief Unregister completion event. - * - * This function is used to unregister completion event which has been previously - * registered using ::NvEncRegisterAsyncEvent() function. The client must unregister - * all events before destroying the encoder using ::NvEncDestroyEncoder() function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] eventParams - * Pointer to the ::_NV_ENC_EVENT_PARAMS structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncUnregisterAsyncEvent (void* encoder, NV_ENC_EVENT_PARAMS* eventParams); - - -// NvEncMapInputResource -/** - * \brief Map an externally created input resource pointer for encoding. - * - * Maps an externally allocated input resource [using and returns a NV_ENC_INPUT_PTR - * which can be used for encoding in the ::NvEncEncodePicture() function. The - * mapped resource is returned in the field NV_ENC_MAP_INPUT_RESOURCE::outputResourcePtr. - * The NvEncodeAPI interface also returns the buffer format of the mapped resource - * in the field NV_ENC_MAP_INPUT_RESOURCE::outbufferFmt. - * This function provides synchronization guarantee that any graphics work submitted - * on the input buffer is completed before the buffer is used for encoding. This is - * also true for compute (i.e. CUDA) work, provided that the previous workload using - * the input resource was submitted to the default stream. - * The client should not access any input buffer while they are mapped by the encoder. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] mapInputResParams - * Pointer to the ::_NV_ENC_MAP_INPUT_RESOURCE structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n - * ::NV_ENC_ERR_MAP_FAILED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncMapInputResource (void* encoder, NV_ENC_MAP_INPUT_RESOURCE* mapInputResParams); - - -// NvEncUnmapInputResource -/** - * \brief UnMaps a NV_ENC_INPUT_PTR which was mapped for encoding - * - * - * UnMaps an input buffer which was previously mapped using ::NvEncMapInputResource() - * API. The mapping created using ::NvEncMapInputResource() should be invalidated - * using this API before the external resource is destroyed by the client. The client - * must unmap the buffer after ::NvEncLockBitstream() API returns succuessfully for encode - * work submitted using the mapped input buffer. - * - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] mappedInputBuffer - * Pointer to the NV_ENC_INPUT_PTR - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n - * ::NV_ENC_ERR_RESOURCE_NOT_MAPPED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncUnmapInputResource (void* encoder, NV_ENC_INPUT_PTR mappedInputBuffer); - -// NvEncDestroyEncoder -/** - * \brief Destroy Encoding Session - * - * Destroys the encoder session previously created using ::NvEncOpenEncodeSession() - * function. The client must flush the encoder before freeing any resources. In order - * to flush the encoder the client must pass a NULL encode picture packet and either - * wait for the ::NvEncEncodePicture() function to return in synchronous mode or wait - * for the flush event to be signaled by the encoder in asynchronous mode. - * The client must free all the input and output resources created using the - * NvEncodeAPI interface before destroying the encoder. If the client is operating - * in asynchronous mode, it must also unregister the completion events previously - * registered. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncDestroyEncoder (void* encoder); - -// NvEncInvalidateRefFrames -/** - * \brief Invalidate reference frames - * - * Invalidates reference frame based on the time stamp provided by the client. - * The encoder marks any reference frames or any frames which have been reconstructed - * using the corrupt frame as invalid for motion estimation and uses older reference - * frames for motion estimation. The encoded forces the current frame to be encoded - * as an intra frame if no reference frames are left after invalidation process. - * This is useful for low latency application for error resiliency. The client - * is recommended to set NV_ENC_CONFIG_H264::maxNumRefFrames to a large value so - * that encoder can keep a backup of older reference frames in the DPB and can use them - * for motion estimation when the newer reference frames have been invalidated. - * This API can be called multiple times. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] invalidRefFrameTimeStamp - * Timestamp of the invalid reference frames which needs to be invalidated. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncInvalidateRefFrames(void* encoder, uint64_t invalidRefFrameTimeStamp); - -// NvEncOpenEncodeSessionEx -/** - * \brief Opens an encoding session. - * - * Opens an encoding session and returns a pointer to the encoder interface in - * the \p **encoder parameter. The client should start encoding process by calling - * this API first. - * The client must pass a pointer to IDirect3DDevice9 device or CUDA context in the \p *device parameter. - * For the OpenGL interface, \p device must be NULL. An OpenGL context must be current when - * calling all NvEncodeAPI functions. - * If the creation of encoder session fails, the client must call ::NvEncDestroyEncoder API - * before exiting. - * - * \param [in] openSessionExParams - * Pointer to a ::NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS structure. - * \param [out] encoder - * Encode Session pointer to the NvEncodeAPI interface. - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_NO_ENCODE_DEVICE \n - * ::NV_ENC_ERR_UNSUPPORTED_DEVICE \n - * ::NV_ENC_ERR_INVALID_DEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncOpenEncodeSessionEx (NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS *openSessionExParams, void** encoder); - -// NvEncRegisterResource -/** - * \brief Registers a resource with the Nvidia Video Encoder Interface. - * - * Registers a resource with the Nvidia Video Encoder Interface for book keeping. - * The client is expected to pass the registered resource handle as well, while calling ::NvEncMapInputResource API. - * - * \param [in] encoder - * Pointer to the NVEncodeAPI interface. - * - * \param [in] registerResParams - * Pointer to a ::_NV_ENC_REGISTER_RESOURCE structure - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_RESOURCE_REGISTER_FAILED \n - * ::NV_ENC_ERR_GENERIC \n - * ::NV_ENC_ERR_UNIMPLEMENTED \n - * - */ -NVENCSTATUS NVENCAPI NvEncRegisterResource (void* encoder, NV_ENC_REGISTER_RESOURCE* registerResParams); - -// NvEncUnregisterResource -/** - * \brief Unregisters a resource previously registered with the Nvidia Video Encoder Interface. - * - * Unregisters a resource previously registered with the Nvidia Video Encoder Interface. - * The client is expected to unregister any resource that it has registered with the - * Nvidia Video Encoder Interface before destroying the resource. - * - * \param [in] encoder - * Pointer to the NVEncodeAPI interface. - * - * \param [in] registeredResource - * The registered resource pointer that was returned in ::NvEncRegisterResource. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n - * ::NV_ENC_ERR_GENERIC \n - * ::NV_ENC_ERR_UNIMPLEMENTED \n - * - */ -NVENCSTATUS NVENCAPI NvEncUnregisterResource (void* encoder, NV_ENC_REGISTERED_PTR registeredResource); - -// NvEncReconfigureEncoder -/** - * \brief Reconfigure an existing encoding session. - * - * Reconfigure an existing encoding session. - * The client should call this API to change/reconfigure the parameter passed during - * NvEncInitializeEncoder API call. - * Currently Reconfiguration of following are not supported. - * Change in GOP structure. - * Change in sync-Async mode. - * Change in MaxWidth & MaxHeight. - * Change in PTDmode. - * - * Resolution change is possible only if maxEncodeWidth & maxEncodeHeight of NV_ENC_INITIALIZE_PARAMS - * is set while creating encoder session. - * - * \param [in] encoder - * Pointer to the NVEncodeAPI interface. - * - * \param [in] reInitEncodeParams - * Pointer to a ::NV_ENC_RECONFIGURE_PARAMS structure. - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_NO_ENCODE_DEVICE \n - * ::NV_ENC_ERR_UNSUPPORTED_DEVICE \n - * ::NV_ENC_ERR_INVALID_DEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncReconfigureEncoder (void *encoder, NV_ENC_RECONFIGURE_PARAMS* reInitEncodeParams); - - - -// NvEncCreateMVBuffer -/** - * \brief Allocates output MV buffer for ME only mode. - * - * This function is used to allocate an output MV buffer. The size of the mvBuffer is - * dependent on the frame height and width of the last ::NvEncCreateInputBuffer() call. - * The NV_ENC_OUTPUT_PTR returned by the NvEncodeAPI interface in the - * ::NV_ENC_CREATE_MV_BUFFER::mvBuffer field should be used in - * ::NvEncRunMotionEstimationOnly() API. - * Client must lock ::NV_ENC_CREATE_MV_BUFFER::mvBuffer using ::NvEncLockBitstream() API to get the motion vector data. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] createMVBufferParams - * Pointer to the ::NV_ENC_CREATE_MV_BUFFER structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncCreateMVBuffer (void* encoder, NV_ENC_CREATE_MV_BUFFER* createMVBufferParams); - - -// NvEncDestroyMVBuffer -/** - * \brief Release an output MV buffer for ME only mode. - * - * This function is used to release the output MV buffer allocated using - * the ::NvEncCreateMVBuffer() function. The client must release the output - * mvBuffer using this function before destroying the encoder session. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] mvBuffer - * Pointer to the mvBuffer being released. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncDestroyMVBuffer (void* encoder, NV_ENC_OUTPUT_PTR mvBuffer); - - -// NvEncRunMotionEstimationOnly -/** - * \brief Submit an input picture and reference frame for motion estimation in ME only mode. - * - * This function is used to submit the input frame and reference frame for motion - * estimation. The ME parameters are passed using *meOnlyParams which is a pointer - * to ::_NV_ENC_MEONLY_PARAMS structure. - * Client must lock ::NV_ENC_CREATE_MV_BUFFER::mvBuffer using ::NvEncLockBitstream() API to get the motion vector data. - * to get motion vector data. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] meOnlyParams - * Pointer to the ::_NV_ENC_MEONLY_PARAMS structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_NEED_MORE_INPUT \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncRunMotionEstimationOnly (void* encoder, NV_ENC_MEONLY_PARAMS* meOnlyParams); - -// NvEncodeAPIGetMaxSupportedVersion -/** - * \brief Get the largest NvEncodeAPI version supported by the driver. - * - * This function can be used by clients to determine if the driver supports - * the NvEncodeAPI header the application was compiled with. - * - * \param [out] version - * Pointer to the requested value. The 4 least significant bits in the returned - * indicate the minor version and the rest of the bits indicate the major - * version of the largest supported version. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - */ -NVENCSTATUS NVENCAPI NvEncodeAPIGetMaxSupportedVersion (uint32_t* version); - - -/// \cond API PFN -/* - * Defines API function pointers - */ -typedef NVENCSTATUS (NVENCAPI* PNVENCOPENENCODESESSION) (void* device, uint32_t deviceType, void** encoder); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEGUIDCOUNT) (void* encoder, uint32_t* encodeGUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEGUIDS) (void* encoder, GUID* GUIDs, uint32_t guidArraySize, uint32_t* GUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPROFILEGUIDCOUNT) (void* encoder, GUID encodeGUID, uint32_t* encodeProfileGUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPROFILEGUIDS) (void* encoder, GUID encodeGUID, GUID* profileGUIDs, uint32_t guidArraySize, uint32_t* GUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETINPUTFORMATCOUNT) (void* encoder, GUID encodeGUID, uint32_t* inputFmtCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETINPUTFORMATS) (void* encoder, GUID encodeGUID, NV_ENC_BUFFER_FORMAT* inputFmts, uint32_t inputFmtArraySize, uint32_t* inputFmtCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODECAPS) (void* encoder, GUID encodeGUID, NV_ENC_CAPS_PARAM* capsParam, int* capsVal); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETCOUNT) (void* encoder, GUID encodeGUID, uint32_t* encodePresetGUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETGUIDS) (void* encoder, GUID encodeGUID, GUID* presetGUIDs, uint32_t guidArraySize, uint32_t* encodePresetGUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETCONFIG) (void* encoder, GUID encodeGUID, GUID presetGUID, NV_ENC_PRESET_CONFIG* presetConfig); -typedef NVENCSTATUS (NVENCAPI* PNVENCINITIALIZEENCODER) (void* encoder, NV_ENC_INITIALIZE_PARAMS* createEncodeParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCCREATEINPUTBUFFER) (void* encoder, NV_ENC_CREATE_INPUT_BUFFER* createInputBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYINPUTBUFFER) (void* encoder, NV_ENC_INPUT_PTR inputBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCCREATEBITSTREAMBUFFER) (void* encoder, NV_ENC_CREATE_BITSTREAM_BUFFER* createBitstreamBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYBITSTREAMBUFFER) (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCENCODEPICTURE) (void* encoder, NV_ENC_PIC_PARAMS* encodePicParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCLOCKBITSTREAM) (void* encoder, NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNLOCKBITSTREAM) (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCLOCKINPUTBUFFER) (void* encoder, NV_ENC_LOCK_INPUT_BUFFER* lockInputBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNLOCKINPUTBUFFER) (void* encoder, NV_ENC_INPUT_PTR inputBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODESTATS) (void* encoder, NV_ENC_STAT* encodeStats); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETSEQUENCEPARAMS) (void* encoder, NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); -typedef NVENCSTATUS (NVENCAPI* PNVENCREGISTERASYNCEVENT) (void* encoder, NV_ENC_EVENT_PARAMS* eventParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNREGISTERASYNCEVENT) (void* encoder, NV_ENC_EVENT_PARAMS* eventParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCMAPINPUTRESOURCE) (void* encoder, NV_ENC_MAP_INPUT_RESOURCE* mapInputResParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNMAPINPUTRESOURCE) (void* encoder, NV_ENC_INPUT_PTR mappedInputBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYENCODER) (void* encoder); -typedef NVENCSTATUS (NVENCAPI* PNVENCINVALIDATEREFFRAMES) (void* encoder, uint64_t invalidRefFrameTimeStamp); -typedef NVENCSTATUS (NVENCAPI* PNVENCOPENENCODESESSIONEX) (NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS *openSessionExParams, void** encoder); -typedef NVENCSTATUS (NVENCAPI* PNVENCREGISTERRESOURCE) (void* encoder, NV_ENC_REGISTER_RESOURCE* registerResParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNREGISTERRESOURCE) (void* encoder, NV_ENC_REGISTERED_PTR registeredRes); -typedef NVENCSTATUS (NVENCAPI* PNVENCRECONFIGUREENCODER) (void* encoder, NV_ENC_RECONFIGURE_PARAMS* reInitEncodeParams); - -typedef NVENCSTATUS (NVENCAPI* PNVENCCREATEMVBUFFER) (void* encoder, NV_ENC_CREATE_MV_BUFFER* createMVBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYMVBUFFER) (void* encoder, NV_ENC_OUTPUT_PTR mvBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCRUNMOTIONESTIMATIONONLY) (void* encoder, NV_ENC_MEONLY_PARAMS* meOnlyParams); - - -/// \endcond - - -/** @} */ /* END ENCODE_FUNC */ - -/** - * \ingroup ENCODER_STRUCTURE - * NV_ENCODE_API_FUNCTION_LIST - */ -typedef struct _NV_ENCODE_API_FUNCTION_LIST -{ - uint32_t version; /**< [in]: Client should pass NV_ENCODE_API_FUNCTION_LIST_VER. */ - uint32_t reserved; /**< [in]: Reserved and should be set to 0. */ - PNVENCOPENENCODESESSION nvEncOpenEncodeSession; /**< [out]: Client should access ::NvEncOpenEncodeSession() API through this pointer. */ - PNVENCGETENCODEGUIDCOUNT nvEncGetEncodeGUIDCount; /**< [out]: Client should access ::NvEncGetEncodeGUIDCount() API through this pointer. */ - PNVENCGETENCODEPRESETCOUNT nvEncGetEncodeProfileGUIDCount; /**< [out]: Client should access ::NvEncGetEncodeProfileGUIDCount() API through this pointer.*/ - PNVENCGETENCODEPRESETGUIDS nvEncGetEncodeProfileGUIDs; /**< [out]: Client should access ::NvEncGetEncodeProfileGUIDs() API through this pointer. */ - PNVENCGETENCODEGUIDS nvEncGetEncodeGUIDs; /**< [out]: Client should access ::NvEncGetEncodeGUIDs() API through this pointer. */ - PNVENCGETINPUTFORMATCOUNT nvEncGetInputFormatCount; /**< [out]: Client should access ::NvEncGetInputFormatCount() API through this pointer. */ - PNVENCGETINPUTFORMATS nvEncGetInputFormats; /**< [out]: Client should access ::NvEncGetInputFormats() API through this pointer. */ - PNVENCGETENCODECAPS nvEncGetEncodeCaps; /**< [out]: Client should access ::NvEncGetEncodeCaps() API through this pointer. */ - PNVENCGETENCODEPRESETCOUNT nvEncGetEncodePresetCount; /**< [out]: Client should access ::NvEncGetEncodePresetCount() API through this pointer. */ - PNVENCGETENCODEPRESETGUIDS nvEncGetEncodePresetGUIDs; /**< [out]: Client should access ::NvEncGetEncodePresetGUIDs() API through this pointer. */ - PNVENCGETENCODEPRESETCONFIG nvEncGetEncodePresetConfig; /**< [out]: Client should access ::NvEncGetEncodePresetConfig() API through this pointer. */ - PNVENCINITIALIZEENCODER nvEncInitializeEncoder; /**< [out]: Client should access ::NvEncInitializeEncoder() API through this pointer. */ - PNVENCCREATEINPUTBUFFER nvEncCreateInputBuffer; /**< [out]: Client should access ::NvEncCreateInputBuffer() API through this pointer. */ - PNVENCDESTROYINPUTBUFFER nvEncDestroyInputBuffer; /**< [out]: Client should access ::NvEncDestroyInputBuffer() API through this pointer. */ - PNVENCCREATEBITSTREAMBUFFER nvEncCreateBitstreamBuffer; /**< [out]: Client should access ::NvEncCreateBitstreamBuffer() API through this pointer. */ - PNVENCDESTROYBITSTREAMBUFFER nvEncDestroyBitstreamBuffer; /**< [out]: Client should access ::NvEncDestroyBitstreamBuffer() API through this pointer. */ - PNVENCENCODEPICTURE nvEncEncodePicture; /**< [out]: Client should access ::NvEncEncodePicture() API through this pointer. */ - PNVENCLOCKBITSTREAM nvEncLockBitstream; /**< [out]: Client should access ::NvEncLockBitstream() API through this pointer. */ - PNVENCUNLOCKBITSTREAM nvEncUnlockBitstream; /**< [out]: Client should access ::NvEncUnlockBitstream() API through this pointer. */ - PNVENCLOCKINPUTBUFFER nvEncLockInputBuffer; /**< [out]: Client should access ::NvEncLockInputBuffer() API through this pointer. */ - PNVENCUNLOCKINPUTBUFFER nvEncUnlockInputBuffer; /**< [out]: Client should access ::NvEncUnlockInputBuffer() API through this pointer. */ - PNVENCGETENCODESTATS nvEncGetEncodeStats; /**< [out]: Client should access ::NvEncGetEncodeStats() API through this pointer. */ - PNVENCGETSEQUENCEPARAMS nvEncGetSequenceParams; /**< [out]: Client should access ::NvEncGetSequenceParams() API through this pointer. */ - PNVENCREGISTERASYNCEVENT nvEncRegisterAsyncEvent; /**< [out]: Client should access ::NvEncRegisterAsyncEvent() API through this pointer. */ - PNVENCUNREGISTERASYNCEVENT nvEncUnregisterAsyncEvent; /**< [out]: Client should access ::NvEncUnregisterAsyncEvent() API through this pointer. */ - PNVENCMAPINPUTRESOURCE nvEncMapInputResource; /**< [out]: Client should access ::NvEncMapInputResource() API through this pointer. */ - PNVENCUNMAPINPUTRESOURCE nvEncUnmapInputResource; /**< [out]: Client should access ::NvEncUnmapInputResource() API through this pointer. */ - PNVENCDESTROYENCODER nvEncDestroyEncoder; /**< [out]: Client should access ::NvEncDestroyEncoder() API through this pointer. */ - PNVENCINVALIDATEREFFRAMES nvEncInvalidateRefFrames; /**< [out]: Client should access ::NvEncInvalidateRefFrames() API through this pointer. */ - PNVENCOPENENCODESESSIONEX nvEncOpenEncodeSessionEx; /**< [out]: Client should access ::NvEncOpenEncodeSession() API through this pointer. */ - PNVENCREGISTERRESOURCE nvEncRegisterResource; /**< [out]: Client should access ::NvEncRegisterResource() API through this pointer. */ - PNVENCUNREGISTERRESOURCE nvEncUnregisterResource; /**< [out]: Client should access ::NvEncUnregisterResource() API through this pointer. */ - PNVENCRECONFIGUREENCODER nvEncReconfigureEncoder; /**< [out]: Client should access ::NvEncReconfigureEncoder() API through this pointer. */ - void* reserved1; - PNVENCCREATEMVBUFFER nvEncCreateMVBuffer; /**< [out]: Client should access ::NvEncCreateMVBuffer API through this pointer. */ - PNVENCDESTROYMVBUFFER nvEncDestroyMVBuffer; /**< [out]: Client should access ::NvEncDestroyMVBuffer API through this pointer. */ - PNVENCRUNMOTIONESTIMATIONONLY nvEncRunMotionEstimationOnly; /**< [out]: Client should access ::NvEncRunMotionEstimationOnly API through this pointer. */ - void* reserved2[281]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENCODE_API_FUNCTION_LIST; - -/** Macro for constructing the version field of ::_NV_ENCODEAPI_FUNCTION_LIST. */ -#define NV_ENCODE_API_FUNCTION_LIST_VER NVENCAPI_STRUCT_VERSION(2) - -// NvEncodeAPICreateInstance -/** - * \ingroup ENCODE_FUNC - * Entry Point to the NvEncodeAPI interface. - * - * Creates an instance of the NvEncodeAPI interface, and populates the - * pFunctionList with function pointers to the API routines implemented by the - * NvEncodeAPI interface. - * - * \param [out] functionList - * - * \return - * ::NV_ENC_SUCCESS - * ::NV_ENC_ERR_INVALID_PTR - */ -NVENCSTATUS NVENCAPI NvEncodeAPICreateInstance(NV_ENCODE_API_FUNCTION_LIST *functionList); - -#ifdef __cplusplus -} -#endif - - -#endif - diff --git a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/include/nvcuvid.h b/lib/nvpipe/src/Video_Codec_SDK_9.0.20/include/nvcuvid.h deleted file mode 100644 index c15040646ef73f68988f363c8eb821bc022f4e44..0000000000000000000000000000000000000000 --- a/lib/nvpipe/src/Video_Codec_SDK_9.0.20/include/nvcuvid.h +++ /dev/null @@ -1,385 +0,0 @@ -/* - * This copyright notice applies to this header file only: - * - * Copyright (c) 2010-2019 NVIDIA Corporation - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the software, and to permit persons to whom the - * software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -/********************************************************************************************************************/ -//! \file nvcuvid.h -//! NVDECODE API provides video decoding interface to NVIDIA GPU devices. -//! \date 2015-2019 -//! This file contains the interface constants, structure definitions and function prototypes. -/********************************************************************************************************************/ - -#if !defined(__NVCUVID_H__) -#define __NVCUVID_H__ - -#include "cuviddec.h" - -#if defined(__cplusplus) -extern "C" { -#endif /* __cplusplus */ - - -/***********************************************/ -//! -//! High-level helper APIs for video sources -//! -/***********************************************/ - -typedef void *CUvideosource; -typedef void *CUvideoparser; -typedef long long CUvideotimestamp; - - -/************************************************************************/ -//! \enum cudaVideoState -//! Video source state enums -//! Used in cuvidSetVideoSourceState and cuvidGetVideoSourceState APIs -/************************************************************************/ -typedef enum { - cudaVideoState_Error = -1, /**< Error state (invalid source) */ - cudaVideoState_Stopped = 0, /**< Source is stopped (or reached end-of-stream) */ - cudaVideoState_Started = 1 /**< Source is running and delivering data */ -} cudaVideoState; - -/************************************************************************/ -//! \enum cudaAudioCodec -//! Audio compression enums -//! Used in CUAUDIOFORMAT structure -/************************************************************************/ -typedef enum { - cudaAudioCodec_MPEG1=0, /**< MPEG-1 Audio */ - cudaAudioCodec_MPEG2, /**< MPEG-2 Audio */ - cudaAudioCodec_MP3, /**< MPEG-1 Layer III Audio */ - cudaAudioCodec_AC3, /**< Dolby Digital (AC3) Audio */ - cudaAudioCodec_LPCM, /**< PCM Audio */ - cudaAudioCodec_AAC, /**< AAC Audio */ -} cudaAudioCodec; - -/************************************************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDEOFORMAT -//! Video format -//! Used in cuvidGetSourceVideoFormat API -/************************************************************************************************/ -typedef struct -{ - cudaVideoCodec codec; /**< OUT: Compression format */ - /** - * OUT: frame rate = numerator / denominator (for example: 30000/1001) - */ - struct { - /**< OUT: frame rate numerator (0 = unspecified or variable frame rate) */ - unsigned int numerator; - /**< OUT: frame rate denominator (0 = unspecified or variable frame rate) */ - unsigned int denominator; - } frame_rate; - unsigned char progressive_sequence; /**< OUT: 0=interlaced, 1=progressive */ - unsigned char bit_depth_luma_minus8; /**< OUT: high bit depth luma. E.g, 2 for 10-bitdepth, 4 for 12-bitdepth */ - unsigned char bit_depth_chroma_minus8; /**< OUT: high bit depth chroma. E.g, 2 for 10-bitdepth, 4 for 12-bitdepth */ - unsigned char min_num_decode_surfaces; /**< OUT: Minimum number of decode surfaces to be allocated for correct - decoding. The client can send this value in - ulNumDecodeSurfaces (in CUVIDDECODECREATEINFO strcuture). - If this value is used for ulNumDecodeSurfaces then it has to - also be returned to parser during sequence callback. */ - unsigned int coded_width; /**< OUT: coded frame width in pixels */ - unsigned int coded_height; /**< OUT: coded frame height in pixels */ - /** - * area of the frame that should be displayed - * typical example: - * coded_width = 1920, coded_height = 1088 - * display_area = { 0,0,1920,1080 } - */ - struct { - int left; /**< OUT: left position of display rect */ - int top; /**< OUT: top position of display rect */ - int right; /**< OUT: right position of display rect */ - int bottom; /**< OUT: bottom position of display rect */ - } display_area; - cudaVideoChromaFormat chroma_format; /**< OUT: Chroma format */ - unsigned int bitrate; /**< OUT: video bitrate (bps, 0=unknown) */ - /** - * OUT: Display Aspect Ratio = x:y (4:3, 16:9, etc) - */ - struct { - int x; - int y; - } display_aspect_ratio; - /** - * Video Signal Description - * Refer section E.2.1 (VUI parameters semantics) of H264 spec file - */ - struct { - unsigned char video_format : 3; /**< OUT: 0-Component, 1-PAL, 2-NTSC, 3-SECAM, 4-MAC, 5-Unspecified */ - unsigned char video_full_range_flag : 1; /**< OUT: indicates the black level and luma and chroma range */ - unsigned char reserved_zero_bits : 4; /**< Reserved bits */ - unsigned char color_primaries; /**< OUT: chromaticity coordinates of source primaries */ - unsigned char transfer_characteristics; /**< OUT: opto-electronic transfer characteristic of the source picture */ - unsigned char matrix_coefficients; /**< OUT: used in deriving luma and chroma signals from RGB primaries */ - } video_signal_description; - unsigned int seqhdr_data_length; /**< OUT: Additional bytes following (CUVIDEOFORMATEX) */ -} CUVIDEOFORMAT; - -/****************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDEOFORMATEX -//! Video format including raw sequence header information -//! Used in cuvidGetSourceVideoFormat API -/****************************************************************/ -typedef struct -{ - CUVIDEOFORMAT format; /**< OUT: CUVIDEOFORMAT structure */ - unsigned char raw_seqhdr_data[1024]; /**< OUT: Sequence header data */ -} CUVIDEOFORMATEX; - -/****************************************************************/ -//! \ingroup STRUCTS -//! \struct CUAUDIOFORMAT -//! Audio formats -//! Used in cuvidGetSourceAudioFormat API -/****************************************************************/ -typedef struct -{ - cudaAudioCodec codec; /**< OUT: Compression format */ - unsigned int channels; /**< OUT: number of audio channels */ - unsigned int samplespersec; /**< OUT: sampling frequency */ - unsigned int bitrate; /**< OUT: For uncompressed, can also be used to determine bits per sample */ - unsigned int reserved1; /**< Reserved for future use */ - unsigned int reserved2; /**< Reserved for future use */ -} CUAUDIOFORMAT; - - -/***************************************************************/ -//! \enum CUvideopacketflags -//! Data packet flags -//! Used in CUVIDSOURCEDATAPACKET structure -/***************************************************************/ -typedef enum { - CUVID_PKT_ENDOFSTREAM = 0x01, /**< Set when this is the last packet for this stream */ - CUVID_PKT_TIMESTAMP = 0x02, /**< Timestamp is valid */ - CUVID_PKT_DISCONTINUITY = 0x04, /**< Set when a discontinuity has to be signalled */ - CUVID_PKT_ENDOFPICTURE = 0x08, /**< Set when the packet contains exactly one frame or one field */ - CUVID_PKT_NOTIFY_EOS = 0x10, /**< If this flag is set along with CUVID_PKT_ENDOFSTREAM, an additional (dummy) - display callback will be invoked with null value of CUVIDPARSERDISPINFO which - should be interpreted as end of the stream. */ -} CUvideopacketflags; - -/*****************************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDSOURCEDATAPACKET -//! Data Packet -//! Used in cuvidParseVideoData API -//! IN for cuvidParseVideoData -/*****************************************************************************/ -typedef struct _CUVIDSOURCEDATAPACKET -{ - unsigned long flags; /**< IN: Combination of CUVID_PKT_XXX flags */ - unsigned long payload_size; /**< IN: number of bytes in the payload (may be zero if EOS flag is set) */ - const unsigned char *payload; /**< IN: Pointer to packet payload data (may be NULL if EOS flag is set) */ - CUvideotimestamp timestamp; /**< IN: Presentation time stamp (10MHz clock), only valid if - CUVID_PKT_TIMESTAMP flag is set */ -} CUVIDSOURCEDATAPACKET; - -// Callback for packet delivery -typedef int (CUDAAPI *PFNVIDSOURCECALLBACK)(void *, CUVIDSOURCEDATAPACKET *); - -/**************************************************************************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDSOURCEPARAMS -//! Describes parameters needed in cuvidCreateVideoSource API -//! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported -//! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed. -/**************************************************************************************************************************/ -typedef struct _CUVIDSOURCEPARAMS -{ - unsigned int ulClockRate; /**< IN: Time stamp units in Hz (0=default=10000000Hz) */ - unsigned int uReserved1[7]; /**< Reserved for future use - set to zero */ - void *pUserData; /**< IN: User private data passed in to the data handlers */ - PFNVIDSOURCECALLBACK pfnVideoDataHandler; /**< IN: Called to deliver video packets */ - PFNVIDSOURCECALLBACK pfnAudioDataHandler; /**< IN: Called to deliver audio packets. */ - void *pvReserved2[8]; /**< Reserved for future use - set to NULL */ -} CUVIDSOURCEPARAMS; - - -/**********************************************/ -//! \ingroup ENUMS -//! \enum CUvideosourceformat_flags -//! CUvideosourceformat_flags -//! Used in cuvidGetSourceVideoFormat API -/**********************************************/ -typedef enum { - CUVID_FMT_EXTFORMATINFO = 0x100 /**< Return extended format structure (CUVIDEOFORMATEX) */ -} CUvideosourceformat_flags; - -#if !defined(__APPLE__) -/***************************************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidCreateVideoSource(CUvideosource *pObj, const char *pszFileName, CUVIDSOURCEPARAMS *pParams) -//! Create CUvideosource object. CUvideosource spawns demultiplexer thread that provides two callbacks: -//! pfnVideoDataHandler() and pfnAudioDataHandler() -//! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported -//! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed. -/***************************************************************************************************************************/ -CUresult CUDAAPI cuvidCreateVideoSource(CUvideosource *pObj, const char *pszFileName, CUVIDSOURCEPARAMS *pParams); - -/***************************************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidCreateVideoSourceW(CUvideosource *pObj, const wchar_t *pwszFileName, CUVIDSOURCEPARAMS *pParams) -//! Create video source -/***************************************************************************************************************************/ -CUresult CUDAAPI cuvidCreateVideoSourceW(CUvideosource *pObj, const wchar_t *pwszFileName, CUVIDSOURCEPARAMS *pParams); - -/********************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidDestroyVideoSource(CUvideosource obj) -//! Destroy video source -/********************************************************************/ -CUresult CUDAAPI cuvidDestroyVideoSource(CUvideosource obj); - -/******************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidSetVideoSourceState(CUvideosource obj, cudaVideoState state) -//! Set video source state to: -//! cudaVideoState_Started - to signal the source to run and deliver data -//! cudaVideoState_Stopped - to stop the source from delivering the data -//! cudaVideoState_Error - invalid source -/******************************************************************************************/ -CUresult CUDAAPI cuvidSetVideoSourceState(CUvideosource obj, cudaVideoState state); - -/******************************************************************************************/ -//! \ingroup FUNCTS -//! \fn cudaVideoState CUDAAPI cuvidGetVideoSourceState(CUvideosource obj) -//! Get video source state -//! Returns: -//! cudaVideoState_Started - if Source is running and delivering data -//! cudaVideoState_Stopped - if Source is stopped or reached end-of-stream -//! cudaVideoState_Error - if Source is in error state -/******************************************************************************************/ -cudaVideoState CUDAAPI cuvidGetVideoSourceState(CUvideosource obj); - -/******************************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidGetSourceVideoFormat(CUvideosource obj, CUVIDEOFORMAT *pvidfmt, unsigned int flags) -//! Gets video source format in pvidfmt, flags is set to combination of CUvideosourceformat_flags as per requirement -/******************************************************************************************************************/ -CUresult CUDAAPI cuvidGetSourceVideoFormat(CUvideosource obj, CUVIDEOFORMAT *pvidfmt, unsigned int flags); - -/**************************************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidGetSourceAudioFormat(CUvideosource obj, CUAUDIOFORMAT *paudfmt, unsigned int flags) -//! Get audio source format -//! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported -//! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed. -/**************************************************************************************************************************/ -CUresult CUDAAPI cuvidGetSourceAudioFormat(CUvideosource obj, CUAUDIOFORMAT *paudfmt, unsigned int flags); - -#endif -/**********************************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDPARSERDISPINFO -//! Used in cuvidParseVideoData API with PFNVIDDISPLAYCALLBACK pfnDisplayPicture -/**********************************************************************************/ -typedef struct _CUVIDPARSERDISPINFO -{ - int picture_index; /**< OUT: Index of the current picture */ - int progressive_frame; /**< OUT: 1 if progressive frame; 0 otherwise */ - int top_field_first; /**< OUT: 1 if top field is displayed first; 0 otherwise */ - int repeat_first_field; /**< OUT: Number of additional fields (1=ivtc, 2=frame doubling, 4=frame tripling, - -1=unpaired field) */ - CUvideotimestamp timestamp; /**< OUT: Presentation time stamp */ -} CUVIDPARSERDISPINFO; - -/***********************************************************************************************************************/ -//! Parser callbacks -//! The parser will call these synchronously from within cuvidParseVideoData(), whenever there is seqeuence change or a picture -//! is ready to be decoded and/or displayed. First argument in functions is "void *pUserData" member of structure CUVIDSOURCEPARAMS -//! Return values from these callbacks are interpreted as: -//! PFNVIDSEQUENCECALLBACK : 0: fail, 1: suceeded, > 1: override dpb size of parser (set by CUVIDPARSERPARAMS::ulMaxNumDecodeSurfaces -//! while creating parser) -//! PFNVIDDECODECALLBACK : 0: fail, >=1: suceeded -//! PFNVIDDISPLAYCALLBACK : 0: fail, >=1: suceeded -/***********************************************************************************************************************/ -typedef int (CUDAAPI *PFNVIDSEQUENCECALLBACK)(void *, CUVIDEOFORMAT *); -typedef int (CUDAAPI *PFNVIDDECODECALLBACK)(void *, CUVIDPICPARAMS *); -typedef int (CUDAAPI *PFNVIDDISPLAYCALLBACK)(void *, CUVIDPARSERDISPINFO *); - -/**************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDPARSERPARAMS -//! Used in cuvidCreateVideoParser API -/**************************************/ -typedef struct _CUVIDPARSERPARAMS -{ - cudaVideoCodec CodecType; /**< IN: cudaVideoCodec_XXX */ - unsigned int ulMaxNumDecodeSurfaces; /**< IN: Max # of decode surfaces (parser will cycle through these) */ - unsigned int ulClockRate; /**< IN: Timestamp units in Hz (0=default=10000000Hz) */ - unsigned int ulErrorThreshold; /**< IN: % Error threshold (0-100) for calling pfnDecodePicture (100=always - IN: call pfnDecodePicture even if picture bitstream is fully corrupted) */ - unsigned int ulMaxDisplayDelay; /**< IN: Max display queue delay (improves pipelining of decode with display) - 0=no delay (recommended values: 2..4) */ - unsigned int uReserved1[5]; /**< IN: Reserved for future use - set to 0 */ - void *pUserData; /**< IN: User data for callbacks */ - PFNVIDSEQUENCECALLBACK pfnSequenceCallback; /**< IN: Called before decoding frames and/or whenever there is a fmt change */ - PFNVIDDECODECALLBACK pfnDecodePicture; /**< IN: Called when a picture is ready to be decoded (decode order) */ - PFNVIDDISPLAYCALLBACK pfnDisplayPicture; /**< IN: Called whenever a picture is ready to be displayed (display order) */ - void *pvReserved2[7]; /**< Reserved for future use - set to NULL */ - CUVIDEOFORMATEX *pExtVideoInfo; /**< IN: [Optional] sequence header data from system layer */ -} CUVIDPARSERPARAMS; - -/************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser *pObj, CUVIDPARSERPARAMS *pParams) -//! Create video parser object and initialize -/************************************************************************************************/ -CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser *pObj, CUVIDPARSERPARAMS *pParams); - -/************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET *pPacket) -//! Parse the video data from source data packet in pPacket -//! Extracts parameter sets like SPS, PPS, bitstream etc. from pPacket and -//! calls back pfnDecodePicture with CUVIDPICPARAMS data for kicking of HW decoding -//! calls back pfnSequenceCallback with CUVIDEOFORMAT data for initial sequence header or when -//! the decoder encounters a video format change -//! calls back pfnDisplayPicture with CUVIDPARSERDISPINFO data to display a video frame -/************************************************************************************************/ -CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET *pPacket); - -/************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj) -//! Destroy the video parser -/************************************************************************************************/ -CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj); - -/**********************************************************************************************/ - -#if defined(__cplusplus) -} -#endif /* __cplusplus */ - -#endif // __NVCUVID_H__ - -