VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtEncoderIO.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2022 VT-MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 #pragma once
10 
12 
13 #include <osg/Image>
14 
15 
16 //PPP: This is need to avoid these errors: WinSock2.h(109) : error C2011 : 'fd_set' : 'struct' type redefinition
17 #ifdef WIN32
18 #include <winsock2.h>
19 #endif
20 #include <nvEncodeAPI.h>
21 
22 // CUDA utilities and system includes
23 #include <cuda.h>
24 #include <cuda_gl_interop.h>
25 #include <cuda_runtime_api.h>
26 #include <cuda_runtime.h>
27 
28 
29 #include <unordered_set>
30 #include <unordered_map>
31 
32 #include <tbb/concurrent_queue.h>
33 #include <thread>
34 
35 
36 
37 namespace makVrv
38 {
39  class DtDe;
40 
43  {
45 
47  CUdeviceptr pNV12devPtr = 0;
49  uint32_t uNV12Stride = 0;
50 
51  void* nvRegisteredResource = 0;
52  NV_ENC_INPUT_PTR hInputSurface = 0;
53  };
54 
57  {
59  NV_ENC_OUTPUT_PTR hBitstreamBuffer = 0;
60  };
61 
64  {
66  unsigned int myPboId = 0;
67 
69  cudaGraphicsResource* myCudaGraphicsResource = nullptr;
70 
72  void* myCudaGraphicsResourceMappedPointer = nullptr;
73  };
74 
77  {
78  public:
79  virtual ~DtEncoderIOCallback() = default;
80 
81  virtual void process(const std::vector<std::vector<uint8_t>>& encodedPackets) = 0;
82  };
83 
89  {
90  unsigned int myPboId;
91  int myWidth;
92  int myHeight;
94  unsigned int myFrameRate;
96  };
97 
102  {
103  public:
105  {
107  , H264 = 1
108  , H265 = 2
109  };
110  public:
112  DtEncoderIO(DtDe& de);
113 
115  virtual ~DtEncoderIO();
116  public:
117 
119  virtual void deinitialize();
120 
122  virtual bool initialize(const osg::ref_ptr<osg::Image>& image, unsigned int frameRate, const std::string& encodingFormat);
123 
125  virtual void submitPBO(unsigned int pboId
126  , int width, int height, int rowSizeInBytes
127  , unsigned int frameRate
128  , DtEncoderIOCallback* callback);
129 
131  virtual bool registerGlResourceIfNecessary(unsigned int pboId);
132 
133  protected:
134 
136  virtual void deinitCUDA();
137 
139  virtual bool initCUDA(const osg::ref_ptr<osg::Image>& image, unsigned int frameRate);
140 
142  virtual void deinitNVidiaEncoder();
143 
145  virtual bool initNVidiaEncoder();
146 
148  virtual void unmapDeviceBuffer();
149 
151  virtual bool flipVertically(DtEncodeInputBuffer* inputBuffer, DtRegisteredCudaGlResource* val, int rowSizeInBytes, int imageHeight);
152 
153  virtual bool registerInputAndOutputResourcesIfNecessary(int width, int height, int rowSizeInBytes);
154 
156  virtual DtHwEncoderFormat toFormat(const std::string& format) const;
157 
159  virtual void getEncodedPackets();
160 
162  virtual void doWork();
163  protected:
166 
169 
171  CUcontext myHcuContext;
172 
173  std::vector<DtEncodeOutputBuffer> myOutputBuffer;
174  std::vector<DtEncodeInputBuffer> myInputBuffer;
175 
177  void * myNVEncoder;
178 
180  uint64_t myEncodeIdx;
181 
183  CUdeviceptr myCUDATempDevicePtr;
184 
186  unsigned int myCUDATempDeviceSize;
187 
189  uint32_t myTempStride;
190 
192  unsigned int myFrameRate;
193 
196 
198 
199  int myWidth = 0;
200  int myHeight = 0;
201 
202  int myToSendFrames = 0;
203  int myGotFrames = 0;
204 
205  friend class DtEncoderIOGetEncodedPacketsTask;
206  std::vector<std::vector<uint8_t>> myHwEncodedPackets;
207 
211  std::unordered_map<unsigned int, DtRegisteredCudaGlResource*> myRegisteredPbos;
212 
214  tbb::concurrent_queue<DtEncoderSubmitPboWork> myWork;
215 
217  std::thread myWorkerThread;
218 
220  bool myWorkerInitialized = false;
221 
224  bool myWorkerToQuit = false;
225 
227  bool myInputOutputResourcesRegistered = false;
228 
229  int myId = 0;
230 
231  static int theRunningId;
232 
233 #define DT_ENCODER_DEBUG_WRITE_TO_FILE 0
234 #if DT_ENCODER_DEBUG_WRITE_TO_FILE
235  std::ofstream* myOuputFile;
236 #endif
237  static const int theDebugLevel;
238  };
239 }
This is the OpenGL resource registered with cuda (pbo)
Definition: DtEncoderIO.h:63
int myCudaDeviceId
CUDA device ID use for the encoding.
Definition: DtEncoderIO.h:165
int myWidth
Definition: DtEncoderIO.h:91
This represents the unit of work that is submitted to the encoder via the submit pbo call...
Definition: DtEncoderIO.h:88
CUcontext myHcuContext
CUDA device context.
Definition: DtEncoderIO.h:171
DtHwEncoderFormat
Definition: DtEncoderIO.h:104
uint64_t myEncodeIdx
encoding index
Definition: DtEncoderIO.h:180
Encoding input buffer.
Definition: DtEncoderIO.h:42
std::vector< DtEncodeInputBuffer > myInputBuffer
Definition: DtEncoderIO.h:174
static int theRunningId
Definition: DtEncoderIO.h:231
int myRowSizeInBytes
Definition: DtEncoderIO.h:93
int myHcuDevice
CUDA device handle.
Definition: DtEncoderIO.h:168
std::unordered_map< unsigned int, DtRegisteredCudaGlResource * > myRegisteredPbos
This is the list of pbos registered with this encoder. It corresponds to a ring buffer of pbos on the...
Definition: DtEncoderIO.h:211
std::vector< std::vector< uint8_t > > myHwEncodedPackets
Definition: DtEncoderIO.h:206
#define DT_DLL_VRVVIDEOSTREAM
Definition: vrvVideoStream.h:20
std::thread myWorkerThread
This is the worker thread which processes the work queue.
Definition: DtEncoderIO.h:217
int myHeight
Definition: DtEncoderIO.h:92
char * inputBuffer
Definition: lz4.h:460
std::vector< DtEncodeOutputBuffer > myOutputBuffer
Definition: DtEncoderIO.h:173
DtEncoderIOCallback * myCallback
Definition: DtEncoderIO.h:95
CUdeviceptr myCUDATempDevicePtr
Temp buffer on CUDA device.
Definition: DtEncoderIO.h:183
This is an abstract call back to process encoded packets.
Definition: DtEncoderIO.h:76
void * myNVEncoder
encoding pointer
Definition: DtEncoderIO.h:177
virtual ~DtEncoderIOCallback()=default
virtual void process(const std::vector< std::vector< uint8_t >> &encodedPackets)=0
unsigned int myFrameRate
Definition: DtEncoderIO.h:94
The EncoderIO class is responsible to initialize CUDA and NVidia encoder then will be responsible to ...
Definition: DtEncoderIO.h:101
static const int theDebugLevel
Definition: DtEncoderIO.h:237
tbb::concurrent_queue< DtEncoderSubmitPboWork > myWork
This is the work queue.
Definition: DtEncoderIO.h:214
Definition: DtIndirectGlTextureObjectManager.h:39
DtHwEncoderFormat myEncodingFormat
Encoding Data format.
Definition: DtEncoderIO.h:195
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer, DtDeCommunicator, as well as a DtDisplay and other things.
Definition: DtDe.h:72
Encoding output buffer.
Definition: DtEncoderIO.h:56
DtDe & myDe
Definition: DtEncoderIO.h:197
unsigned int myPboId
Definition: DtEncoderIO.h:90
Contains DLL export macros.
uint32_t myTempStride
Temp buffer on CUDA device.
Definition: DtEncoderIO.h:189
unsigned int myFrameRate
Frame Rate.
Definition: DtEncoderIO.h:192
unsigned int myCUDATempDeviceSize
CUDATempDevicePtr size.
Definition: DtEncoderIO.h:186

Document ID: Generated on Wed Mar 27 22:49:11 EDT 2024 from SVN revision 264633
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)