Logo
ApraPipes 1.0
Loading...
Searching...
No Matches
H264DecoderNvCodecHelper.h
1#pragma once
2
3#include <assert.h>
4#include <stdint.h>
5#include <mutex>
6#include <vector>
7#include <string>
8#include <iostream>
9#include <sstream>
10#include <string.h>
11#include "nvcuvid.h"
12#include <boost/shared_ptr.hpp>
13#include "CommonDefs.h"
14#include "CudaCommon.h"
15#include <queue>
16
20class NVDECException : public std::exception
21{
22public:
23 NVDECException(const std::string& errorStr, const CUresult errorCode)
24 : m_errorString(errorStr), m_errorCode(errorCode) {}
25
26 virtual ~NVDECException() throw() {}
27 virtual const char* what() const throw() { return m_errorString.c_str(); }
28 CUresult getErrorCode() const { return m_errorCode; }
29 const std::string& getErrorString() const { return m_errorString; }
30 static NVDECException makeNVDECException(const std::string& errorStr, const CUresult errorCode,
31 const std::string& functionName, const std::string& fileName, int lineNo);
32private:
33 std::string m_errorString;
34 CUresult m_errorCode;
35};
36
37inline NVDECException NVDECException::makeNVDECException(const std::string& errorStr, const CUresult errorCode, const std::string& functionName,
38 const std::string& fileName, int lineNo)
39{
40 std::ostringstream errorLog;
41 errorLog << functionName << " : " << errorStr << " at " << fileName << ":" << lineNo << std::endl;
42 NVDECException exception(errorLog.str(), errorCode);
43 return exception;
44}
45
46#define NVDEC_THROW_ERROR( errorStr, errorCode ) \
47 do \
48 { \
49 throw NVDECException::makeNVDECException(errorStr, errorCode, __FUNCTION__, __FILE__, __LINE__); \
50 } while (0)
51
52
53#define NVDEC_API_CALL( cuvidAPI ) \
54 do \
55 { \
56 CUresult errorCode = cuvidAPI; \
57 if( errorCode != CUDA_SUCCESS) \
58 { \
59 std::ostringstream errorLog; \
60 errorLog << #cuvidAPI << " returned error " << errorCode; \
61 throw NVDECException::makeNVDECException(errorLog.str(), errorCode, __FUNCTION__, __FILE__, __LINE__); \
62 } \
63 } while (0)
64
65struct Rect {
66 int l, t, r, b;
67};
68
69struct Dim {
70 int w, h;
71};
72
76class NvDecoder {
77
78public:
84 NvDecoder(CUcontext cuContext, int nWidth, int nHeight, bool bUseDeviceFrame, cudaVideoCodec eCodec, std::mutex* pMutex = NULL,
85 bool bLowLatency = false, bool bDeviceFramePitched = false, const Rect* pCropRect = NULL, const Dim* pResizeDim = NULL, int maxWidth = 0, int maxHeight = 0);
87 ~NvDecoder() ;
88
92 CUcontext GetContext() { return m_cuContext; }
93
97 int GetWidth() { assert(m_nWidth); return m_nWidth; }
98
102 int GetHeight() { assert(m_nHeight); return m_nHeight; }
103
107 int GetFrameSize() { assert(m_nWidth); return m_nWidth * m_nHeight * 3 / (m_nBitDepthMinus8 ? 1 : 2); }
108
113
117 int GetBitDepth() { assert(m_nWidth); return m_nBitDepthMinus8 + 8; }
118
122 CUVIDEOFORMAT GetVideoFormatInfo() { assert(m_nWidth); return m_videoFormat; }
123
127 std::string GetVideoInfo() const { return m_videoInfo.str(); }
128
133 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);
134
141 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);
142
146 void UnlockFrame(uint8_t** ppFrame, int nFrame);
147
151 int setReconfigParams(const Rect* pCropRect, const Dim* pResizeDim);
152
153private:
157 static int CUDAAPI HandleVideoSequenceProc(void* pUserData, CUVIDEOFORMAT* pVideoFormat) { return ((NvDecoder*)pUserData)->HandleVideoSequence(pVideoFormat); }
158
162 static int CUDAAPI HandlePictureDecodeProc(void* pUserData, CUVIDPICPARAMS* pPicParams) { return ((NvDecoder*)pUserData)->HandlePictureDecode(pPicParams); }
163
167 static int CUDAAPI HandlePictureDisplayProc(void* pUserData, CUVIDPARSERDISPINFO* pDispInfo) { return ((NvDecoder*)pUserData)->HandlePictureDisplay(pDispInfo); }
168
173 int HandleVideoSequence(CUVIDEOFORMAT* pVideoFormat);
174
179 int HandlePictureDecode(CUVIDPICPARAMS* pPicParams);
180
185 int HandlePictureDisplay(CUVIDPARSERDISPINFO* pDispInfo);
186
190 int ReconfigureDecoder(CUVIDEOFORMAT* pVideoFormat);
191
192private:
193 CUcontext m_cuContext = NULL;
194 CUvideoctxlock m_ctxLock;
195 std::mutex* m_pMutex;
196 CUvideoparser m_hParser = NULL;
197 CUvideodecoder m_hDecoder = NULL;
199 // dimension of the output
200 unsigned int m_nWidth = 0, m_nHeight = 0;
201 // height of the mapped surface
204 cudaVideoCodec m_eCodec = cudaVideoCodec_NumCodecs;
205 cudaVideoChromaFormat m_eChromaFormat;
207 CUVIDEOFORMAT m_videoFormat = {};
209 // stock of frames
210 std::vector<uint8_t*> m_vpFrame;
211 // decoded frames for return
212 std::vector<uint8_t*> m_vpFrameRet;
213 // timestamps of decoded frames
214 std::vector<int64_t> m_vTimestamp;
217 bool m_bEndDecodeDone = false;
218 std::mutex m_mtxVPFrame;
220 CUstream m_cuvidStream = 0;
225
226 std::ostringstream m_videoInfo;
227 unsigned int m_nMaxWidth = 0, m_nMaxHeight = 0;
230};
231
233{
234public:
235 H264DecoderNvCodecHelper(int mWidth, int mHieght);
238
239 bool init(std::function<void(frame_sp&)> send, std::function<frame_sp()> makeFrame);
240 void ConvertToPlanar(uint8_t* pHostFrame, int nWidth, int nHeight, int nBitDepth);
241 void process(void* inputFrameBuffer, size_t inputFrameSize, uint64_t inputFrameTS);
242 std::function<void( frame_sp&)> send;
243 std::function<frame_sp()> makeFrame;
244private:
245 boost::shared_ptr<NvDecoder> helper;
246 std::queue<uint64_t> framesTimestampEntry;
247};
Definition H264DecoderNvCodecHelper.h:233
void process(void *inputFrameBuffer, size_t inputFrameSize, uint64_t inputFrameTS)
Definition H264DecoderNvCodecHelper.cpp:733
H264DecoderNvCodecHelper()
Definition H264DecoderNvCodecHelper.h:236
boost::shared_ptr< NvDecoder > helper
Definition H264DecoderNvCodecHelper.h:245
bool init(std::function< void(frame_sp &)> send, std::function< frame_sp()> makeFrame)
Definition H264DecoderNvCodecHelper.cpp:709
std::function< void(frame_sp &)> send
Definition H264DecoderNvCodecHelper.h:242
std::function< frame_sp()> makeFrame
Definition H264DecoderNvCodecHelper.h:243
void ConvertToPlanar(uint8_t *pHostFrame, int nWidth, int nHeight, int nBitDepth)
Definition H264DecoderNvCodecHelper.cpp:716
~H264DecoderNvCodecHelper()
Definition H264DecoderNvCodecHelper.h:237
std::queue< uint64_t > framesTimestampEntry
Definition H264DecoderNvCodecHelper.h:246
Exception class for error reporting from the decode API.
Definition H264DecoderNvCodecHelper.h:21
CUresult m_errorCode
Definition H264DecoderNvCodecHelper.h:34
NVDECException(const std::string &errorStr, const CUresult errorCode)
Definition H264DecoderNvCodecHelper.h:23
virtual const char * what() const
Definition H264DecoderNvCodecHelper.h:27
virtual ~NVDECException()
Definition H264DecoderNvCodecHelper.h:26
std::string m_errorString
Definition H264DecoderNvCodecHelper.h:33
CUresult getErrorCode() const
Definition H264DecoderNvCodecHelper.h:28
const std::string & getErrorString() const
Definition H264DecoderNvCodecHelper.h:29
static NVDECException makeNVDECException(const std::string &errorStr, const CUresult errorCode, const std::string &functionName, const std::string &fileName, int lineNo)
Definition H264DecoderNvCodecHelper.h:37
Base class for decoder interface.
Definition H264DecoderNvCodecHelper.h:76
CUcontext m_cuContext
Definition H264DecoderNvCodecHelper.h:193
int m_nBitDepthMinus8
Definition H264DecoderNvCodecHelper.h:206
std::vector< uint8_t * > m_vpFrame
Definition H264DecoderNvCodecHelper.h:210
bool m_bReconfigExtPPChange
Definition H264DecoderNvCodecHelper.h:229
int m_nPicNumInDecodeOrder[32]
Definition H264DecoderNvCodecHelper.h:216
int m_nDecodedFrame
Definition H264DecoderNvCodecHelper.h:215
int HandleVideoSequence(CUVIDEOFORMAT *pVideoFormat)
This function gets called when a sequence is ready to be decoded. The function also gets called when ...
Definition H264DecoderNvCodecHelper.cpp:117
Dim m_resizeDim
Definition H264DecoderNvCodecHelper.h:224
CUvideoparser m_hParser
Definition H264DecoderNvCodecHelper.h:196
int GetFrameSize()
This function is used to get the current frame size based on pixel format.
Definition H264DecoderNvCodecHelper.h:107
cudaVideoChromaFormat m_eChromaFormat
Definition H264DecoderNvCodecHelper.h:205
Rect m_cropRect
Definition H264DecoderNvCodecHelper.h:223
unsigned int m_nMaxHeight
Definition H264DecoderNvCodecHelper.h:227
int m_nSurfaceWidth
Definition H264DecoderNvCodecHelper.h:203
int GetDeviceFramePitch()
This function is used to get the pitch of the device buffer holding the decoded frame.
Definition H264DecoderNvCodecHelper.h:112
int HandlePictureDisplay(CUVIDPARSERDISPINFO *pDispInfo)
This function gets called after a picture is decoded and available for display. Frames are fetched an...
Definition H264DecoderNvCodecHelper.cpp:430
int ReconfigureDecoder(CUVIDEOFORMAT *pVideoFormat)
This function reconfigure decoder if there is a change in sequence params.
Definition H264DecoderNvCodecHelper.cpp:262
CUvideodecoder m_hDecoder
Definition H264DecoderNvCodecHelper.h:197
~NvDecoder()
Definition H264DecoderNvCodecHelper.cpp:530
int HandlePictureDecode(CUVIDPICPARAMS *pPicParams)
This function gets called when a picture is ready to be decoded. cuvidDecodePicture is called from th...
Definition H264DecoderNvCodecHelper.cpp:416
bool m_bDeviceFramePitched
Definition H264DecoderNvCodecHelper.h:221
std::string GetVideoInfo() const
This function is used to print information about the video stream.
Definition H264DecoderNvCodecHelper.h:127
CUstream m_cuvidStream
Definition H264DecoderNvCodecHelper.h:220
Rect m_displayRect
Definition H264DecoderNvCodecHelper.h:208
int setReconfigParams(const Rect *pCropRect, const Dim *pResizeDim)
This function allow app to set decoder reconfig params.
Definition H264DecoderNvCodecHelper.cpp:369
std::vector< uint8_t * > m_vpFrameRet
Definition H264DecoderNvCodecHelper.h:212
int GetHeight()
This function is used to get the current decode height.
Definition H264DecoderNvCodecHelper.h:102
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)
This function decodes a frame and returns the locked frame buffers This makes the buffers available f...
Definition H264DecoderNvCodecHelper.cpp:613
unsigned int m_nMaxWidth
Definition H264DecoderNvCodecHelper.h:227
std::ostringstream m_videoInfo
Definition H264DecoderNvCodecHelper.h:226
int GetWidth()
This function is used to get the current decode width.
Definition H264DecoderNvCodecHelper.h:97
unsigned int m_nWidth
Definition H264DecoderNvCodecHelper.h:200
NvDecoder()
Definition H264DecoderNvCodecHelper.h:86
CUvideoctxlock m_ctxLock
Definition H264DecoderNvCodecHelper.h:194
int GetBitDepth()
This function is used to get the bit depth associated with the pixel format.
Definition H264DecoderNvCodecHelper.h:117
CUVIDEOFORMAT GetVideoFormatInfo()
This function is used to get information about the video stream (codec, display parameters etc)
Definition H264DecoderNvCodecHelper.h:122
static int CUDAAPI HandleVideoSequenceProc(void *pUserData, CUVIDEOFORMAT *pVideoFormat)
Callback function to be registered for getting a callback when decoding of sequence starts.
Definition H264DecoderNvCodecHelper.h:157
cudaVideoCodec m_eCodec
Definition H264DecoderNvCodecHelper.h:204
void UnlockFrame(uint8_t **ppFrame, int nFrame)
This function unlocks the frame buffer and makes the frame buffers available for write again.
Definition H264DecoderNvCodecHelper.cpp:621
std::mutex m_mtxVPFrame
Definition H264DecoderNvCodecHelper.h:218
CUVIDEOFORMAT m_videoFormat
Definition H264DecoderNvCodecHelper.h:207
int m_nFrameAlloc
Definition H264DecoderNvCodecHelper.h:219
int m_nSurfaceHeight
Definition H264DecoderNvCodecHelper.h:202
int m_nDecodedFrameReturned
Definition H264DecoderNvCodecHelper.h:215
static int CUDAAPI HandlePictureDisplayProc(void *pUserData, CUVIDPARSERDISPINFO *pDispInfo)
Callback function to be registered for getting a callback when a decoded frame is available for displ...
Definition H264DecoderNvCodecHelper.h:167
static int CUDAAPI HandlePictureDecodeProc(void *pUserData, CUVIDPICPARAMS *pPicParams)
Callback function to be registered for getting a callback when a decoded frame is ready to be decoded...
Definition H264DecoderNvCodecHelper.h:162
std::mutex * m_pMutex
Definition H264DecoderNvCodecHelper.h:195
bool m_bUseDeviceFrame
Definition H264DecoderNvCodecHelper.h:198
CUcontext GetContext()
This function is used to get the current CUDA context.
Definition H264DecoderNvCodecHelper.h:92
std::vector< int64_t > m_vTimestamp
Definition H264DecoderNvCodecHelper.h:214
bool m_bEndDecodeDone
Definition H264DecoderNvCodecHelper.h:217
size_t m_nDeviceFramePitch
Definition H264DecoderNvCodecHelper.h:222
int m_nDecodePicCnt
Definition H264DecoderNvCodecHelper.h:216
unsigned int m_nHeight
Definition H264DecoderNvCodecHelper.h:200
bool m_bReconfigExternal
Definition H264DecoderNvCodecHelper.h:228
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)
This function decodes a frame and returns frames that are available for display. The frames should be...
Definition H264DecoderNvCodecHelper.cpp:569
Definition H264DecoderNvCodecHelper.h:69
int w
Definition H264DecoderNvCodecHelper.h:70
int h
Definition H264DecoderNvCodecHelper.h:70
Definition H264DecoderNvCodecHelper.h:65
int t
Definition H264DecoderNvCodecHelper.h:66
int b
Definition H264DecoderNvCodecHelper.h:66
int r
Definition H264DecoderNvCodecHelper.h:66
int l
Definition H264DecoderNvCodecHelper.h:66