NvEncoderCudaWithCUarray.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include <cuda.h>
  3. #include <mutex>
  4. #include <stdint.h>
  5. #include "NvEncoder/NvEncoder.h"
  6. namespace unity
  7. {
  8. namespace webrtc
  9. {
  10. /**
  11. * @brief Encoder for CUDA device memory.
  12. */
  13. class NvEncoderCudaWithCUarray : public ::NvEncoder
  14. {
  15. public:
  16. NvEncoderCudaWithCUarray(
  17. CUcontext cuContext,
  18. uint32_t nWidth,
  19. uint32_t nHeight,
  20. NV_ENC_BUFFER_FORMAT eBufferFormat,
  21. uint32_t nExtraOutputDelay = 3,
  22. bool bMotionEstimationOnly = false,
  23. bool bOPInVideoMemory = false);
  24. virtual ~NvEncoderCudaWithCUarray() override;
  25. /**
  26. * @brief This is a static function to copy input data from host memory to device memory.
  27. * This function assumes YUV plane is a single contiguous memory segment.
  28. */
  29. static void CopyToDeviceFrame(
  30. CUcontext device,
  31. void* pSrcArray,
  32. uint32_t nSrcPitch,
  33. CUarray pDstArray,
  34. uint32_t dstPitch,
  35. int width,
  36. int height,
  37. CUmemorytype srcMemoryType,
  38. NV_ENC_BUFFER_FORMAT pixelFormat,
  39. const uint32_t dstChromaOffsets[],
  40. uint32_t numChromaPlanes,
  41. bool bUnAlignedDeviceCopy = false,
  42. CUstream stream = nullptr);
  43. protected:
  44. /**
  45. * @brief This function is used to release the input buffers allocated for encoding.
  46. * This function is an override of virtual function NvEncoder::ReleaseInputBuffers().
  47. */
  48. virtual void ReleaseInputBuffers() override;
  49. private:
  50. /**
  51. * @brief This function is used to allocate input buffers for encoding.
  52. * This function is an override of virtual function NvEncoder::AllocateInputBuffers().
  53. */
  54. virtual void AllocateInputBuffers(int32_t numInputBuffers) override;
  55. private:
  56. /**
  57. * @brief This is a private function to release CUDA device memory used for encoding.
  58. */
  59. void ReleaseCudaResources();
  60. protected:
  61. CUcontext m_cuContext;
  62. };
  63. } // end namespace webrtc
  64. } // end namespace unity