easy_encode.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. /**
  21. * @file easy_encode.h
  22. *
  23. * This file contains a declaration of the EasyDecode class and involved structures.
  24. */
  25. #ifndef EASYCODEC_EASY_ENCODE_H_
  26. #define EASYCODEC_EASY_ENCODE_H_
  27. #include <functional>
  28. #include <memory>
  29. #include "cxxutil/edk_attribute.h"
  30. #include "cxxutil/exception.h"
  31. #include "easycodec/vformat.h"
  32. namespace edk {
  33. /**
  34. * @brief Rate control parameters
  35. */
  36. struct RateControl {
  37. /// Using variable bit rate or constant bit rate
  38. bool vbr{false};
  39. /// The interval of ISLICE.
  40. uint32_t gop{0};
  41. /// The numerator of encode frame rate of the venc channel
  42. uint32_t frame_rate_num{0};
  43. /// The denominator of encode frame rate of the venc channel
  44. uint32_t frame_rate_den{0};
  45. /// Average bitrate in unit of kpbs, for cbr only.
  46. uint32_t bit_rate{0};
  47. /// The max bitrate in unit of kbps, for vbr only .
  48. uint32_t max_bit_rate{0};
  49. /// The max qp, range [min_qp, 51]
  50. uint32_t max_qp{0};
  51. /// The min qp, range [0, max_qp]
  52. uint32_t min_qp{0};
  53. };
  54. /**
  55. * @brief Video profile enumaration.
  56. */
  57. enum class VideoProfile {
  58. H264_BASELINE = 0,
  59. H264_MAIN,
  60. H264_HIGH,
  61. H264_HIGH_10,
  62. H265_MAIN,
  63. H265_MAIN_STILL,
  64. H265_MAIN_INTRA,
  65. H265_MAIN_10,
  66. PROFILE_MAX
  67. };
  68. /**
  69. * @brief Video codec level
  70. */
  71. enum class VideoLevel {
  72. H264_1 = 0,
  73. H264_1B,
  74. H264_11,
  75. H264_12,
  76. H264_13,
  77. H264_2,
  78. H264_21,
  79. H264_22,
  80. H264_3,
  81. H264_31,
  82. H264_32,
  83. H264_4,
  84. H264_41,
  85. H264_42,
  86. H264_5,
  87. H264_51,
  88. H265_MAIN_1,
  89. H265_HIGH_1,
  90. H265_MAIN_2,
  91. H265_HIGH_2,
  92. H265_MAIN_21,
  93. H265_HIGH_21,
  94. H265_MAIN_3,
  95. H265_HIGH_3,
  96. H265_MAIN_31,
  97. H265_HIGH_31,
  98. H265_MAIN_4,
  99. H265_HIGH_4,
  100. H265_MAIN_41,
  101. H265_HIGH_41,
  102. H265_MAIN_5,
  103. H265_HIGH_5,
  104. H265_MAIN_51,
  105. H265_HIGH_51,
  106. H265_MAIN_52,
  107. H265_HIGH_52,
  108. H265_MAIN_6,
  109. H265_HIGH_6,
  110. H265_MAIN_61,
  111. H265_HIGH_61,
  112. H265_MAIN_62,
  113. H265_HIGH_62,
  114. LEVEL_MAX
  115. };
  116. /*
  117. * @brief cncodec GOP type, see developer guide
  118. */
  119. enum class GopType { BIDIRECTIONAL, LOW_DELAY, PYRAMID };
  120. /**
  121. * @brief Encode packet callback function type
  122. * @param CnPacket Packet containing encoded frame information
  123. */
  124. using EncodePacketCallback = std::function<void(const CnPacket&)>;
  125. /// Encode EOS callback function type
  126. using EncodeEosCallback = std::function<void()>;
  127. class EncodeHandler;
  128. /**
  129. * @brief Easy encoder class, provide a fast and easy API to encode on MLU platform.
  130. */
  131. class EasyEncode {
  132. public:
  133. friend class EncodeHandler;
  134. /**
  135. * @brief params for creating EasyEncode
  136. */
  137. struct Attr {
  138. /// The maximum resolution that this endecoder can handle.
  139. Geometry frame_geometry;
  140. /**
  141. * @brief Input pixel format
  142. * @note h264/h265 support NV21/NV12/I420/RGBA/BGRA/ARGB/ABGR
  143. * jpeg support NV21/NV12
  144. */
  145. PixelFmt pixel_format;
  146. /**
  147. * @brief output codec type
  148. * @note support h264/h265/jpeg
  149. */
  150. CodecType codec_type = CodecType::H264;
  151. /// Color space standard
  152. ColorStd color_std = ColorStd::ITU_BT_709;
  153. /// Qulity factor for jpeg encoder.
  154. uint32_t jpeg_qfactor = 50;
  155. /// Profile for video encoder.
  156. VideoProfile profile = VideoProfile::H264_MAIN;
  157. /// Video encode level
  158. VideoLevel level = VideoLevel::H264_41;
  159. /// Video rate control parameters.
  160. RateControl rate_control;
  161. /// Input buffer number
  162. uint32_t input_buffer_num = 3;
  163. /// Output buffer number
  164. uint32_t output_buffer_num = 4;
  165. /// P frame number in gop default 0
  166. uint32_t p_frame_num = 0;
  167. /// B frame number in gop when profile is above main, default 0
  168. uint32_t b_frame_num = 0;
  169. /// GOP type, @see edk::GopType
  170. GopType gop_type = GopType::BIDIRECTIONAL;
  171. /// insert SPS/PPS before IDR,1, insert, 0 not
  172. bool insert_spspps_when_idr = true;
  173. /// Whether to print encoder attribute
  174. bool silent = false;
  175. /// Callback for receive packet
  176. EncodePacketCallback packet_callback = NULL;
  177. /// Callback for receive eos
  178. EncodeEosCallback eos_callback = NULL;
  179. /// Indentification to specify device on which create encoder
  180. int dev_id = 0;
  181. };
  182. /**
  183. * @brief Create encoder by attr. Throw a Exception while error encountered.
  184. * @param attr Encoder attribute description
  185. * @return Pointer to new encoder instance
  186. */
  187. static std::unique_ptr<EasyEncode> New(const Attr& attr);
  188. /**
  189. * @brief Abort encoder instance at once
  190. * @note aborted encoder instance cannot be used any more
  191. */
  192. void AbortEncoder();
  193. /**
  194. * @brief Get the encoder instance attribute
  195. * @return Encoder attribute
  196. */
  197. Attr GetAttr() const;
  198. /**
  199. * @brief Destroy the Easy Encode object
  200. */
  201. ~EasyEncode();
  202. /**
  203. * @brief send frame to encoder.
  204. * @param frame CNframe
  205. * @param eos default false
  206. * @return Return false if send data failed.
  207. */
  208. bool SendDataCPU(const CnFrame& frame, bool eos = false);
  209. /**
  210. * @brief Release encoder buffer.
  211. * @note Release encoder buffer each time received packet while packet content will not be used,
  212. * otherwise encoder may be blocked.
  213. * @param buf_id Codec buffer id.
  214. */
  215. void ReleaseBuffer(uint64_t buf_id);
  216. private:
  217. EasyEncode();
  218. EncodeHandler* handler_ = nullptr;
  219. EasyEncode(const EasyEncode&) = delete;
  220. const EasyEncode& operator=(const EasyEncode&) = delete;
  221. }; // class EasyEncode
  222. } // namespace edk
  223. #endif // EASYCODEC_EASY_ENCODE_H_