| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| #pragma once
|
|
|
| #include <assert.h>
|
| #include <stdint.h>
|
| #include <mutex>
|
| #include <vector>
|
| #include <string>
|
| #include <iostream>
|
| #include <sstream>
|
| #include <string.h>
|
| #include "../../../Interface/nvcuvid.h"
|
| #include "../Utils/NvCodecUtils.h"
|
| #include <map>
|
|
|
| #define MAX_FRM_CNT 32
|
|
|
| typedef enum{
|
| SEI_TYPE_TIME_CODE_H264 = 1,
|
| SEI_TYPE_USER_DATA_REGISTERED = 4,
|
| SEI_TYPE_USER_DATA_UNREGISTERED = 5,
|
| SEI_TYPE_TIME_CODE = 136,
|
| SEI_TYPE_MASTERING_DISPLAY_COLOR_VOLUME = 137,
|
| SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO = 144,
|
| SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS = 147
|
| } SEI_H264_HEVC_MPEG2_PAYLOAD_TYPE;
|
|
|
| |
| |
|
|
| 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;
|
| };
|
|
|
| |
| |
|
|
| class NvDecoder {
|
|
|
| public:
|
| |
| |
| |
| |
|
|
| NvDecoder(CUcontext cuContext, bool bUseDeviceFrame, cudaVideoCodec eCodec, bool bLowLatency = false,
|
| bool bDeviceFramePitched = false, const Rect *pCropRect = NULL, const Dim *pResizeDim = NULL,
|
| bool extract_user_SEI_Message = false, int maxWidth = 0, int maxHeight = 0, unsigned int clkRate = 1000,
|
| bool force_zero_latency = false, unsigned int initial_dec_surfaces = 0, CUstream custream=NULL);
|
| ~NvDecoder();
|
|
|
| |
| |
|
|
| CUcontext GetContext() { return m_cuContext; }
|
|
|
| |
| |
| |
|
|
| int GetWidth() { assert(m_nWidth); return (m_eOutputFormat == cudaVideoSurfaceFormat_NV12 || m_eOutputFormat == cudaVideoSurfaceFormat_P016
|
| || m_eOutputFormat == cudaVideoSurfaceFormat_NV16 || m_eOutputFormat == cudaVideoSurfaceFormat_P216)
|
| ? (m_nWidth + 1) & ~1 : m_nWidth; }
|
|
|
| |
| |
|
|
| int GetDecodeWidth() { assert(m_nWidth); return m_nWidth; }
|
|
|
| |
| |
|
|
| int GetHeight() { assert(m_nLumaHeight); return m_nLumaHeight; }
|
|
|
| |
| |
|
|
| int GetChromaHeight() { assert(m_nChromaHeight); return m_nChromaHeight; }
|
|
|
| |
| |
|
|
| int GetNumChromaPlanes() { assert(m_nNumChromaPlanes); return m_nNumChromaPlanes; }
|
|
|
| |
| |
|
|
| int GetFrameSize() { assert(m_nWidth); return GetWidth() * (m_nLumaHeight + (m_nChromaHeight * m_nNumChromaPlanes)) * m_nBPP; }
|
|
|
| |
| |
|
|
| int GetLumaPlaneSize() { assert(m_nWidth); return GetWidth() * m_nLumaHeight * m_nBPP; }
|
|
|
| |
| |
|
|
| int GetChromaPlaneSize() { assert(m_nWidth); return GetWidth() * (m_nChromaHeight * m_nNumChromaPlanes) * m_nBPP; }
|
|
|
| |
| |
|
|
| int GetDeviceFramePitch() { assert(m_nWidth); return m_nDeviceFramePitch ? (int)m_nDeviceFramePitch : GetWidth() * m_nBPP; }
|
|
|
| |
| |
|
|
| int GetBitDepth() { assert(m_nWidth); return m_nBitDepthMinus8 + 8; }
|
|
|
| |
| |
|
|
| int GetBPP() { assert(m_nWidth); return m_nBPP; }
|
|
|
| |
| |
|
|
| cudaVideoSurfaceFormat GetOutputFormat() { return m_eOutputFormat; }
|
|
|
| |
| |
|
|
| cudaVideoChromaFormat GetOutputChromaFormat() { return GetChromaFormat(m_eOutputFormat); }
|
|
|
| |
| |
|
|
| CUVIDEOFORMAT GetVideoFormatInfo() { assert(m_nWidth); return m_videoFormat; }
|
|
|
| |
| |
|
|
| cudaVideoChromaFormat GetChromaFormat(cudaVideoSurfaceFormat);
|
|
|
| |
| |
|
|
| const char *GetCodecString(cudaVideoCodec eCodec);
|
|
|
| |
| |
|
|
| std::string GetVideoInfo() const { return m_videoInfo.str(); }
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| int Decode(const uint8_t *pData, int nSize, int nFlags = 0, int64_t nTimestamp = 0);
|
|
|
| |
| |
| |
|
|
| uint8_t* GetFrame(int64_t* pTimestamp = nullptr);
|
|
|
|
|
| |
| |
| |
| |
| |
|
|
| uint8_t* GetLockedFrame(int64_t* pTimestamp = nullptr);
|
|
|
| |
| |
| |
| |
|
|
| void UnlockFrame(uint8_t **pFrame);
|
|
|
| |
| |
| |
| |
|
|
| int setReconfigParams(const Rect * pCropRect, const Dim * pResizeDim);
|
|
|
| |
| |
| |
| |
|
|
| void SetOperatingPoint(const uint32_t opPoint, const bool bDispAllLayers) { m_nOperatingPoint = opPoint; m_bDispAllLayers = bDispAllLayers; }
|
|
|
|
|
| void startTimer() { m_stDecode_time.Start(); }
|
|
|
|
|
| double stopTimer() { return m_stDecode_time.Stop(); }
|
|
|
|
|
| protected:
|
| |
| |
| |
|
|
| virtual int HandleVideoSequence(CUVIDEOFORMAT *pVideoFormat);
|
|
|
|
|
| private:
|
| |
| |
|
|
| static int CUDAAPI HandleVideoSequenceProc(void *pUserData, CUVIDEOFORMAT *pVideoFormat) { return ((NvDecoder *)pUserData)->HandleVideoSequence(pVideoFormat); }
|
|
|
| |
| |
|
|
| static int CUDAAPI HandlePictureDecodeProc(void *pUserData, CUVIDPICPARAMS *pPicParams) { return ((NvDecoder *)pUserData)->HandlePictureDecode(pPicParams); }
|
|
|
| |
| |
|
|
| static int CUDAAPI HandlePictureDisplayProc(void *pUserData, CUVIDPARSERDISPINFO *pDispInfo) { return ((NvDecoder *)pUserData)->HandlePictureDisplay(pDispInfo); }
|
|
|
| |
| |
|
|
| static int CUDAAPI HandleOperatingPointProc(void *pUserData, CUVIDOPERATINGPOINTINFO *pOPInfo) { return ((NvDecoder *)pUserData)->GetOperatingPoint(pOPInfo); }
|
|
|
| |
| |
|
|
| static int CUDAAPI HandleSEIMessagesProc(void *pUserData, CUVIDSEIMESSAGEINFO *pSEIMessageInfo) { return ((NvDecoder *)pUserData)->GetSEIMessage(pSEIMessageInfo); }
|
|
|
| |
| |
| |
|
|
| int HandlePictureDecode(CUVIDPICPARAMS *pPicParams);
|
|
|
| |
| |
| |
|
|
| int HandlePictureDisplay(CUVIDPARSERDISPINFO *pDispInfo);
|
|
|
| |
| |
|
|
| int GetOperatingPoint(CUVIDOPERATINGPOINTINFO *pOPInfo);
|
|
|
| |
| |
|
|
| int GetSEIMessage(CUVIDSEIMESSAGEINFO *pSEIMessageInfo);
|
|
|
| |
| |
|
|
| int ReconfigureDecoder(CUVIDEOFORMAT *pVideoFormat);
|
|
|
| private:
|
| CUcontext m_cuContext = NULL;
|
| CUvideoctxlock m_ctxLock;
|
| CUvideoparser m_hParser = NULL;
|
| CUvideodecoder m_hDecoder = NULL;
|
| bool m_bUseDeviceFrame;
|
|
|
| unsigned int m_nWidth = 0, m_nLumaHeight = 0, m_nChromaHeight = 0;
|
| unsigned int m_nNumChromaPlanes = 0;
|
|
|
| int m_nSurfaceHeight = 0;
|
| int m_nSurfaceWidth = 0;
|
| cudaVideoCodec m_eCodec = cudaVideoCodec_NumCodecs;
|
| cudaVideoChromaFormat m_eChromaFormat = cudaVideoChromaFormat_420;
|
| cudaVideoSurfaceFormat m_eOutputFormat = cudaVideoSurfaceFormat_NV12;
|
| int m_nBitDepthMinus8 = 0;
|
| int m_nBPP = 1;
|
| CUVIDEOFORMAT m_videoFormat = {};
|
| Rect m_displayRect = {};
|
|
|
| std::vector<uint8_t *> m_vpFrame;
|
|
|
| std::vector<int64_t> m_vTimestamp;
|
| int m_nDecodedFrame = 0, m_nDecodedFrameReturned = 0;
|
| int m_nDecodePicCnt = 0, m_nPicNumInDecodeOrder[MAX_FRM_CNT];
|
| CUVIDSEIMESSAGEINFO *m_pCurrSEIMessage = NULL;
|
| CUVIDSEIMESSAGEINFO m_SEIMessagesDisplayOrder[MAX_FRM_CNT][2];
|
| FILE *m_fpSEI = NULL;
|
| bool m_bEndDecodeDone = false;
|
| std::mutex m_mtxVPFrame;
|
| int m_nFrameAlloc = 0;
|
| CUstream m_cuvidStream = 0;
|
| bool m_bExternalStream = 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;
|
| bool m_bNumSurfacesChange = false;
|
| StopWatch m_stDecode_time;
|
|
|
| unsigned int m_nOperatingPoint = 0;
|
| bool m_bDispAllLayers = false;
|
|
|
|
|
|
|
|
|
|
|
| bool m_bForce_zero_latency = false;
|
| bool m_bExtractSEIMessage = false;
|
|
|
|
|
|
|
| bool m_bMemoryOptimize = false;
|
| int m_nNumDecSurfaces = 0;
|
| };
|
|
|