NvEncoderCuda.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2017-2020 NVIDIA Corporation. All rights reserved.
  3. *
  4. * Please refer to the NVIDIA end user license agreement (EULA) associated
  5. * with this source code for terms and conditions that govern your use of
  6. * this software. Any use, reproduction, disclosure, or distribution of
  7. * this software and related documentation outside the terms of the EULA
  8. * is strictly prohibited.
  9. *
  10. */
  11. #pragma once
  12. #include <vector>
  13. #include <stdint.h>
  14. #include <mutex>
  15. #include <cuda.h>
  16. #include "NvEncoder.h"
  17. #define CUDA_DRVAPI_CALL( call ) \
  18. do \
  19. { \
  20. CUresult err__ = call; \
  21. if (err__ != CUDA_SUCCESS) \
  22. { \
  23. const char *szErrName = NULL; \
  24. cuGetErrorName(err__, &szErrName); \
  25. std::ostringstream errorLog; \
  26. errorLog << "CUDA driver API error " << szErrName ; \
  27. throw NVENCException::makeNVENCException(errorLog.str(), NV_ENC_ERR_GENERIC, __FUNCTION__, __FILE__, __LINE__); \
  28. } \
  29. } \
  30. while (0)
  31. /**
  32. * @brief Encoder for CUDA device memory.
  33. */
  34. class NvEncoderCuda : public NvEncoder
  35. {
  36. public:
  37. NvEncoderCuda(CUcontext cuContext, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat,
  38. uint32_t nExtraOutputDelay = 3, bool bMotionEstimationOnly = false, bool bOPInVideoMemory = false);
  39. virtual ~NvEncoderCuda();
  40. /**
  41. * @brief This is a static function to copy input data from host memory to device memory.
  42. * This function assumes YUV plane is a single contiguous memory segment.
  43. */
  44. static void CopyToDeviceFrame(CUcontext device,
  45. void* pSrcFrame,
  46. uint32_t nSrcPitch,
  47. CUdeviceptr pDstFrame,
  48. uint32_t dstPitch,
  49. int width,
  50. int height,
  51. CUmemorytype srcMemoryType,
  52. NV_ENC_BUFFER_FORMAT pixelFormat,
  53. const uint32_t dstChromaOffsets[],
  54. uint32_t numChromaPlanes,
  55. bool bUnAlignedDeviceCopy = false,
  56. CUstream stream = NULL);
  57. /**
  58. * @brief This is a static function to copy input data from host memory to device memory.
  59. * Application must pass a seperate device pointer for each YUV plane.
  60. */
  61. static void CopyToDeviceFrame(CUcontext device,
  62. void* pSrcFrame,
  63. uint32_t nSrcPitch,
  64. CUdeviceptr pDstFrame,
  65. uint32_t dstPitch,
  66. int width,
  67. int height,
  68. CUmemorytype srcMemoryType,
  69. NV_ENC_BUFFER_FORMAT pixelFormat,
  70. CUdeviceptr dstChromaPtr[],
  71. uint32_t dstChromaPitch,
  72. uint32_t numChromaPlanes,
  73. bool bUnAlignedDeviceCopy = false);
  74. /**
  75. * @brief This function sets input and output CUDA streams
  76. */
  77. void SetIOCudaStreams(NV_ENC_CUSTREAM_PTR inputStream, NV_ENC_CUSTREAM_PTR outputStream);
  78. protected:
  79. /**
  80. * @brief This function is used to release the input buffers allocated for encoding.
  81. * This function is an override of virtual function NvEncoder::ReleaseInputBuffers().
  82. */
  83. virtual void ReleaseInputBuffers() override;
  84. private:
  85. /**
  86. * @brief This function is used to allocate input buffers for encoding.
  87. * This function is an override of virtual function NvEncoder::AllocateInputBuffers().
  88. */
  89. virtual void AllocateInputBuffers(int32_t numInputBuffers) override;
  90. private:
  91. /**
  92. * @brief This is a private function to release CUDA device memory used for encoding.
  93. */
  94. void ReleaseCudaResources();
  95. protected:
  96. CUcontext m_cuContext;
  97. private:
  98. size_t m_cudaPitch = 0;
  99. };