NvEncoderOutputInVidMemCuda.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2019-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 "nvEncodeAPI.h"
  14. #include <stdint.h>
  15. #include <mutex>
  16. #include <string>
  17. #include <iostream>
  18. #include <sstream>
  19. #include <string.h>
  20. #include "NvEncoderCuda.h"
  21. #define ALIGN_UP(s,a) (((s) + (a) - 1) & ~((a) - 1))
  22. /**
  23. * @brief Class for encode or ME only output in video memory feature for Cuda interfaces.
  24. */
  25. class NvEncoderOutputInVidMemCuda : public NvEncoderCuda
  26. {
  27. public:
  28. /**
  29. * @brief NvEncoderOutputInVidMem class constructor.
  30. */
  31. NvEncoderOutputInVidMemCuda(CUcontext cuContext, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat,
  32. bool bMotionEstimationOnly = false);
  33. /**
  34. * @brief NvEncoder class virtual destructor.
  35. */
  36. virtual ~NvEncoderOutputInVidMemCuda();
  37. /**
  38. * @brief This function is used to initialize the encoder session.
  39. * Application must call this function to initialize the encoder, before
  40. * starting to encode or motion estimate any frames.
  41. */
  42. void CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams);
  43. /**
  44. * @brief This function is used to encode a frame.
  45. * Applications must call EncodeFrame() function to encode the uncompressed
  46. * data, which has been copied to an input buffer obtained from the
  47. * GetNextInputFrame() function.
  48. * This function returns video memory buffer pointers containing compressed data
  49. * in pOutputBuffer. If there is buffering enabled, this may return without
  50. * any data in pOutputBuffer.
  51. */
  52. void EncodeFrame(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer, NV_ENC_PIC_PARAMS *pPicParams = nullptr);
  53. /**
  54. * @brief This function to flush the encoder queue.
  55. * The encoder might be queuing frames for B picture encoding or lookahead;
  56. * the application must call EndEncode() to get all the queued encoded frames
  57. * from the encoder. The application must call this function before destroying
  58. * an encoder session. Video memory buffer pointer containing compressed data
  59. * is returned in pOutputBuffer.
  60. */
  61. void EndEncode(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer);
  62. /**
  63. * @brief This function is used to run motion estimation.
  64. * This is used to run motion estimation on a a pair of frames. The
  65. * application must copy the reference frame data to the buffer obtained
  66. * by calling GetNextReferenceFrame(), and copy the input frame data to
  67. * the buffer obtained by calling GetNextInputFrame() before calling the
  68. * RunMotionEstimation() function.
  69. * This function returns video memory buffer pointers containing
  70. * motion vector data in pOutputBuffer.
  71. */
  72. void RunMotionEstimation(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer);
  73. /**
  74. * @brief This function is used to destroy the encoder session.
  75. * Application must call this function to destroy the encoder session and
  76. * clean up any allocated resources. The application must call EndEncode()
  77. * function to get any queued encoded frames before calling DestroyEncoder().
  78. */
  79. void DestroyEncoder();
  80. /**
  81. * @brief This function is used to get the size of output buffer required to be
  82. * allocated in order to store the output.
  83. */
  84. uint32_t GetOutputBufferSize();
  85. private:
  86. /**
  87. * @brief This function is used to allocate output buffers in video memory for storing
  88. * encode or motion estimation output.
  89. */
  90. void AllocateOutputBuffers(uint32_t numOutputBuffers);
  91. /**
  92. * @brief This function is used to release output buffers.
  93. */
  94. void ReleaseOutputBuffers();
  95. /**
  96. * @brief This function is used to register output buffers with NvEncodeAPI.
  97. */
  98. void RegisterOutputResources(uint32_t bfrSize);
  99. /**
  100. * @brief This function is used to unregister output resources which had been previously registered for encoding
  101. * using RegisterOutputResources() function.
  102. */
  103. void UnregisterOutputResources();
  104. /**
  105. * @brief This function is used to map the input and output buffers to NvEncodeAPI.
  106. */
  107. void MapResources(uint32_t bfrIdx);
  108. /**
  109. * @brief This is a private function which is used to get video memory buffer pointer containing compressed data
  110. * or motion estimation output from the encoder HW.
  111. * This is called by EncodeFrame() function. If there is buffering enabled,
  112. * this may return without any output data.
  113. */
  114. void GetEncodedPacket(std::vector<NV_ENC_OUTPUT_PTR> &pOutputBuffer, bool bOutputDelay);
  115. /**
  116. * @brief This function is used to flush the encoder queue.
  117. */
  118. void FlushEncoder();
  119. private:
  120. std::vector<NV_ENC_OUTPUT_PTR> m_vMappedOutputBuffers;
  121. std::vector<NV_ENC_OUTPUT_PTR> m_pOutputBuffers;
  122. std::vector<NV_ENC_REGISTERED_PTR> m_vRegisteredResourcesOutputBuffer;
  123. };