easy_decode.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_decode.h
  22. *
  23. * This file contains a declaration of the EasyEncode class and involved structures.
  24. */
  25. #ifndef EASYCODEC_EASY_DECODE_H_
  26. #define EASYCODEC_EASY_DECODE_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 Decode packet callback function type
  35. * @param CnFrame Frame containing decoded frame information
  36. */
  37. using DecodeFrameCallback = std::function<void(const CnFrame&)>;
  38. /// Decode EOS callback function type
  39. using DecodeEOSCallback = std::function<void()>;
  40. class DecodeHandler;
  41. /**
  42. * @brief Easy decode class, provide a fast and easy API to decode on MLU platform
  43. */
  44. class EasyDecode {
  45. public:
  46. /**
  47. * @brief params for creating EasyDecode
  48. */
  49. struct Attr {
  50. /// The frame resolution that this decoder can handle.
  51. Geometry frame_geometry;
  52. /// Video codec type
  53. CodecType codec_type;
  54. /// The pixel format of output frames.
  55. PixelFmt pixel_format;
  56. /// Color space standard
  57. ColorStd color_std = ColorStd::ITU_BT_709;
  58. /// The input buffer count
  59. uint32_t input_buffer_num = 2;
  60. /// The output buffer count.
  61. uint32_t output_buffer_num = 3;
  62. /// Interlaced data or progressive data
  63. bool interlaced = false;
  64. /// Frame callback
  65. DecodeFrameCallback frame_callback = NULL;
  66. /// EOS callback
  67. DecodeEOSCallback eos_callback = NULL;
  68. /// whether to print useful messages.
  69. bool silent = false;
  70. /// Create decoder on which device
  71. int dev_id = 0;
  72. /// Set align value (2^n = 1,4,8...), MLU270: 1 -- MLU220 scalar: 128
  73. /// @note only work on video decode, JPEG stride align is fixed to 64
  74. int stride_align = 1;
  75. }; // struct Attr
  76. /**
  77. * @brief Decoder status enumeration
  78. */
  79. enum class Status {
  80. RUNNING, ///< running, SendData and Callback are active.
  81. PAUSED, ///< pause, SendData and Callback are blocked.
  82. STOP, ///< stopped, decoder was destroied.
  83. EOS ///< received eos.
  84. }; // Enum Status
  85. /**
  86. * @brief Create decoder by attr. Throw a Exception while error encountered.
  87. * @param attr Decoder attribute description
  88. * @attention status is RUNNING after object be constructed.
  89. * @return Pointer to new decoder instance
  90. */
  91. static std::unique_ptr<EasyDecode> New(const Attr& attr) noexcept(false);
  92. /**
  93. * @brief Get the decoder instance attribute
  94. * @return Decoder attribute
  95. */
  96. Attr GetAttr() const;
  97. /**
  98. * @brief Get current state of decoder
  99. * @return Decoder status
  100. */
  101. Status GetStatus() const;
  102. /**
  103. * @brief Pause the decode process
  104. */
  105. bool Pause();
  106. /**
  107. * @brief Resume the decode process
  108. */
  109. bool Resume();
  110. /**
  111. * @brief Abort decoder instance at once
  112. * @note aborted decoder instance cannot be used any more
  113. */
  114. void AbortDecoder();
  115. /**
  116. * @brief Send data to decoder, block when STATUS is pause.
  117. * An Exception is thrown when send data failed.
  118. *
  119. * @param packet bytestream data
  120. * @param integral_frame indicate whether packet contain an integral frame
  121. *
  122. * @retval true Feed data succeed
  123. * @retval false otherwise
  124. */
  125. bool FeedData(const CnPacket& packet, bool integral_frame = true) noexcept(false);
  126. /**
  127. * @brief Send EOS to decoder, block when STATUS is pause.
  128. * An Exception is thrown when send EOS failed.
  129. *
  130. * @retval false if an EOS has been sent
  131. * @retval true otherwise.
  132. */
  133. bool FeedEos() noexcept(false);
  134. /**
  135. * @brief Release decoder's buffer.
  136. * @note Release decoder buffer While buffer content will not be used, or decoder may be blocked.
  137. * @param buf_id Codec buffer id.
  138. */
  139. void ReleaseBuffer(uint64_t buf_id);
  140. /**
  141. * @brief copy frame from device to host.
  142. * @param dst copy destination
  143. * @param frame Frame you want to copy
  144. * @return when error occurs, return false.
  145. */
  146. static bool CopyFrameD2H(void* dst, const CnFrame& frame);
  147. /**
  148. * @brief Gets the minimum decode output buffer count.
  149. * @return Return the minimum decode output buffer count.
  150. *
  151. * @note It only returns right value after received the first frame.
  152. **/
  153. int GetMinimumOutputBufferCount() const;
  154. /**
  155. * @brief Destroy the Easy Decode object
  156. */
  157. ~EasyDecode();
  158. friend class DecodeHandler;
  159. private:
  160. explicit EasyDecode(const Attr& attr);
  161. EasyDecode(const EasyDecode&) = delete;
  162. EasyDecode& operator=(const EasyDecode&) = delete;
  163. EasyDecode(EasyDecode&&) = delete;
  164. EasyDecode& operator=(EasyDecode&&) = delete;
  165. DecodeHandler* handler_ = nullptr;
  166. }; // class EasyDecode
  167. } // namespace edk
  168. #endif // EASYCODEC_EASY_DECODE_H_