@@ -61,6 +61,26 @@ class CuvidParserException : public std::runtime_error {
6161 CuvidParserException () : std::runtime_error(" HW reset" ) {}
6262};
6363
64+ class CudaResMgr
65+ {
66+ private:
67+ CudaResMgr ();
68+
69+ public:
70+ CUcontext GetCtx (size_t idx);
71+ CUstream GetStream (size_t idx);
72+ ~CudaResMgr ();
73+ static CudaResMgr& Instance ();
74+ static size_t GetNumGpus ();
75+
76+ std::vector<std::pair<CUdevice, CUcontext>> g_Contexts;
77+ std::vector<CUstream> g_Streams;
78+
79+ static std::mutex gInsMutex ;
80+ static std::mutex gCtxMutex ;
81+ static std::mutex gStrMutex ;
82+ };
83+
6484class PyFrameUploader {
6585 std::unique_ptr<CudaUploadFrame> uploader;
6686 uint32_t surfaceWidth, surfaceHeight;
@@ -95,7 +115,7 @@ class PyBufferUploader {
95115 CUstream str);
96116
97117 PyBufferUploader (uint32_t elemSize, uint32_t numElems,
98- size_t ctx, size_t str) :
118+ size_t ctx, size_t str) :
99119 PyBufferUploader (elemSize, numElems, (CUcontext)ctx, (CUstream)str) {}
100120
101121 std::shared_ptr<CudaBuffer> UploadSingleBuffer (py::array_t <uint8_t > &buffer);
@@ -136,7 +156,7 @@ class PyCudaBufferDownloader {
136156 CUstream str);
137157
138158 PyCudaBufferDownloader (uint32_t elemSize, uint32_t numElems,
139- size_t ctx, size_t str) :
159+ size_t ctx, size_t str) :
140160 PyCudaBufferDownloader (elemSize, numElems, (CUcontext)ctx, (CUstream)str) {}
141161
142162 bool DownloadSingleCudaBuffer (std::shared_ptr<CudaBuffer> buffer,
@@ -275,11 +295,11 @@ class PyNvDecoder {
275295 PyNvDecoder (pathToFile, (CUcontext)ctx, (CUstream)str, ffmpeg_options){}
276296
277297 static Buffer *getElementaryVideo (DemuxFrame *demuxer,
278- SeekContext & seek_ctx, bool needSEI);
298+ SeekContext * seek_ctx, bool needSEI);
279299
280300 static Surface *getDecodedSurface (NvdecDecodeFrame *decoder,
281301 DemuxFrame *demuxer,
282- SeekContext & seek_ctx, bool needSEI);
302+ SeekContext * seek_ctx, bool needSEI);
283303
284304 uint32_t Width () const ;
285305
@@ -305,148 +325,15 @@ class PyNvDecoder {
305325
306326 Pixel_Format GetPixelFormat () const ;
307327
308- std::shared_ptr<Surface> DecodeSurfaceFromPacket (py::array_t <uint8_t > &packet,
309- py::array_t <uint8_t > &sei);
310-
311- std::shared_ptr<Surface> DecodeSurfaceFromPacket (PacketData &enc_packet_data,
312- py::array_t <uint8_t > &packet,
313- py::array_t <uint8_t > &sei);
314-
315- std::shared_ptr<Surface> DecodeSurfaceFromPacket (py::array_t <uint8_t > &packet,
316- py::array_t <uint8_t > &sei,
317- PacketData &pkt_data);
318-
319- std::shared_ptr<Surface> DecodeSurfaceFromPacket (PacketData &enc_packet_data,
320- py::array_t <uint8_t > &packet,
321- py::array_t <uint8_t > &sei,
322- PacketData &pkt_data);
323-
324- std::shared_ptr<Surface> DecodeSurfaceFromPacket (py::array_t <uint8_t > &packet);
325-
326- std::shared_ptr<Surface> DecodeSurfaceFromPacket (PacketData &enc_packet_data,
327- py::array_t <uint8_t > &packet);
328-
329- std::shared_ptr<Surface> DecodeSurfaceFromPacket (py::array_t <uint8_t > &packet,
330- PacketData &pkt_data);
331-
332- std::shared_ptr<Surface> DecodeSurfaceFromPacket (PacketData &enc_packet_data,
333- py::array_t <uint8_t > &packet,
334- PacketData &pkt_data);
335-
336- std::shared_ptr<Surface> DecodeSingleSurface (py::array_t <uint8_t > &sei);
337-
338- std::shared_ptr<Surface> DecodeSingleSurface (py::array_t <uint8_t > &sei,
339- PacketData &pkt_data);
340-
341- std::shared_ptr<Surface> DecodeSingleSurface (py::array_t <uint8_t > &sei,
342- SeekContext &ctx);
343-
344- std::shared_ptr<Surface> DecodeSingleSurface (py::array_t <uint8_t > &sei,
345- SeekContext &ctx,
346- PacketData &pkt_data);
347-
348- std::shared_ptr<Surface> DecodeSingleSurface ();
349-
350- std::shared_ptr<Surface> DecodeSingleSurface (PacketData &pkt_data);
351-
352- std::shared_ptr<Surface> DecodeSingleSurface (SeekContext &ctx);
353-
354- std::shared_ptr<Surface> DecodeSingleSurface (SeekContext &ctx,
355- PacketData &pkt_data);
356-
357- bool DecodeSingleFrame (py::array_t <uint8_t > &frame,
358- py::array_t <uint8_t > &sei);
359-
360- bool DecodeSingleFrame (py::array_t <uint8_t > &frame,
361- py::array_t <uint8_t > &sei,
362- PacketData &pkt_data);
363-
364- bool DecodeSingleFrame (py::array_t <uint8_t > &frame,
365- py::array_t <uint8_t > &sei,
366- SeekContext &ctx);
328+ bool DecodeSurface (class DecodeContext &ctx);
367329
368- bool DecodeSingleFrame (py::array_t <uint8_t > &frame,
369- py::array_t <uint8_t > &sei,
370- SeekContext &ctx,
371- PacketData &pkt_data);
330+ bool DecodeFrame (class DecodeContext &ctx, py::array_t <uint8_t >& frame);
372331
373- bool DecodeSingleFrame (py::array_t <uint8_t > &frame);
374-
375- bool DecodeSingleFrame (py::array_t <uint8_t > &frame,
376- PacketData &pkt_data);
377-
378- bool DecodeSingleFrame (py::array_t <uint8_t > &frame,
379- SeekContext &ctx);
380-
381- bool DecodeSingleFrame (py::array_t <uint8_t > &frame,
382- SeekContext &ctx,
383- PacketData &pkt_data);
384-
385- bool DecodeFrameFromPacket (py::array_t <uint8_t > &frame,
386- py::array_t <uint8_t > &packet,
387- py::array_t <uint8_t > &sei);
388-
389- bool DecodeFrameFromPacket (py::array_t <uint8_t > &frame,
390- PacketData &enc_packet_data,
391- py::array_t <uint8_t > &packet,
392- py::array_t <uint8_t > &sei);
393-
394- bool DecodeFrameFromPacket (py::array_t <uint8_t > &frame,
395- py::array_t <uint8_t > &packet,
396- py::array_t <uint8_t > &sei,
397- PacketData &pkt_data);
398-
399- bool DecodeFrameFromPacket (py::array_t <uint8_t > &frame,
400- PacketData &enc_packet_data,
401- py::array_t <uint8_t > &packet,
402- py::array_t <uint8_t > &sei,
403- PacketData &pkt_data);
404-
405- bool DecodeFrameFromPacket (py::array_t <uint8_t > &frame,
406- py::array_t <uint8_t > &packet);
407-
408- bool DecodeFrameFromPacket (py::array_t <uint8_t > &frame,
409- PacketData &enc_packet_data,
410- py::array_t <uint8_t > &packet);
411-
412- bool DecodeFrameFromPacket (py::array_t <uint8_t > &frame,
413- py::array_t <uint8_t > &packet,
414- PacketData &pkt_data);
415-
416- bool DecodeFrameFromPacket (py::array_t <uint8_t > &frame,
417- PacketData &enc_packet_data,
418- py::array_t <uint8_t > &packet,
419- PacketData &pkt_data);
420-
421- bool FlushSingleFrame (py::array_t <uint8_t > &frame);
422-
423- bool FlushSingleFrame (py::array_t <uint8_t > &frame, PacketData &pkt_data);
424-
425- std::shared_ptr<Surface> FlushSingleSurface ();
426-
427- std::shared_ptr<Surface> FlushSingleSurface (PacketData &pkt_data);
428-
429- private:
430- bool DecodeSurface (struct DecodeContext &ctx);
431-
432- Surface *getDecodedSurfaceFromPacket (py::array_t <uint8_t > *pPacket,
433- PacketData *p_packet_data = nullptr ,
332+ Surface *getDecodedSurfaceFromPacket (const py::array_t <uint8_t > *pPacket,
333+ const PacketData *p_packet_data = nullptr ,
434334 bool no_eos = false );
435- };
436335
437- struct EncodeContext {
438- std::shared_ptr<Surface> rawSurface;
439- py::array_t <uint8_t > *pPacket;
440- const py::array_t <uint8_t > *pMessageSEI;
441- bool sync;
442- bool append;
443-
444- EncodeContext (std::shared_ptr<Surface> spRawSurface,
445- py::array_t <uint8_t > *packet,
446- const py::array_t <uint8_t > *messageSEI, bool is_sync,
447- bool is_append)
448- : rawSurface(spRawSurface), pPacket(packet), pMessageSEI(messageSEI),
449- sync (is_sync), append(is_append) {}
336+ void DownloaderLazyInit ();
450337};
451338
452339class PyNvEncoder {
@@ -523,5 +410,5 @@ class PyNvEncoder {
523410 bool FlushSinglePacket (py::array_t <uint8_t > &packet);
524411
525412private:
526- bool EncodeSingleSurface (EncodeContext &ctx);
413+ bool EncodeSingleSurface (struct EncodeContext &ctx);
527414};
0 commit comments