vpx_decoder.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_VPX_VPX_DECODER_H_
  11. #define VPX_VPX_VPX_DECODER_H_
  12. /*!\defgroup decoder Decoder Algorithm Interface
  13. * \ingroup codec
  14. * This abstraction allows applications using this decoder to easily support
  15. * multiple video formats with minimal code duplication. This section describes
  16. * the interface common to all decoders.
  17. * @{
  18. */
  19. /*!\file
  20. * \brief Describes the decoder algorithm interface to applications.
  21. *
  22. * This file describes the interface between an application and a
  23. * video decoder algorithm.
  24. *
  25. */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include "./vpx_codec.h"
  30. #include "./vpx_frame_buffer.h"
  31. /*!\brief Current ABI version number
  32. *
  33. * \internal
  34. * If this file is altered in any way that changes the ABI, this value
  35. * must be bumped. Examples include, but are not limited to, changing
  36. * types, removing or reassigning enums, adding/removing/rearranging
  37. * fields to structures
  38. */
  39. #define VPX_DECODER_ABI_VERSION \
  40. (3 + VPX_CODEC_ABI_VERSION) /**<\hideinitializer*/
  41. /*! \brief Decoder capabilities bitfield
  42. *
  43. * Each decoder advertises the capabilities it supports as part of its
  44. * ::vpx_codec_iface_t interface structure. Capabilities are extra interfaces
  45. * or functionality, and are not required to be supported by a decoder.
  46. *
  47. * The available flags are specified by VPX_CODEC_CAP_* defines.
  48. */
  49. #define VPX_CODEC_CAP_PUT_SLICE 0x10000 /**< Will issue put_slice callbacks */
  50. #define VPX_CODEC_CAP_PUT_FRAME 0x20000 /**< Will issue put_frame callbacks */
  51. #define VPX_CODEC_CAP_POSTPROC 0x40000 /**< Can postprocess decoded frame */
  52. /*!\brief Can conceal errors due to packet loss */
  53. #define VPX_CODEC_CAP_ERROR_CONCEALMENT 0x80000
  54. /*!\brief Can receive encoded frames one fragment at a time */
  55. #define VPX_CODEC_CAP_INPUT_FRAGMENTS 0x100000
  56. /*! \brief Initialization-time Feature Enabling
  57. *
  58. * Certain codec features must be known at initialization time, to allow for
  59. * proper memory allocation.
  60. *
  61. * The available flags are specified by VPX_CODEC_USE_* defines.
  62. */
  63. /*!\brief Can support frame-based multi-threading */
  64. #define VPX_CODEC_CAP_FRAME_THREADING 0x200000
  65. /*!brief Can support external frame buffers */
  66. #define VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER 0x400000
  67. #define VPX_CODEC_USE_POSTPROC 0x10000 /**< Postprocess decoded frame */
  68. /*!\brief Conceal errors in decoded frames */
  69. #define VPX_CODEC_USE_ERROR_CONCEALMENT 0x20000
  70. /*!\brief The input frame should be passed to the decoder one fragment at a
  71. * time */
  72. #define VPX_CODEC_USE_INPUT_FRAGMENTS 0x40000
  73. /*!\brief Enable frame-based multi-threading */
  74. #define VPX_CODEC_USE_FRAME_THREADING 0x80000
  75. /*!\brief Stream properties
  76. *
  77. * This structure is used to query or set properties of the decoded
  78. * stream. Algorithms may extend this structure with data specific
  79. * to their bitstream by setting the sz member appropriately.
  80. */
  81. typedef struct vpx_codec_stream_info {
  82. unsigned int sz; /**< Size of this structure */
  83. unsigned int w; /**< Width (or 0 for unknown/default) */
  84. unsigned int h; /**< Height (or 0 for unknown/default) */
  85. unsigned int is_kf; /**< Current frame is a keyframe */
  86. } vpx_codec_stream_info_t;
  87. /* REQUIRED FUNCTIONS
  88. *
  89. * The following functions are required to be implemented for all decoders.
  90. * They represent the base case functionality expected of all decoders.
  91. */
  92. /*!\brief Initialization Configurations
  93. *
  94. * This structure is used to pass init time configuration options to the
  95. * decoder.
  96. */
  97. typedef struct vpx_codec_dec_cfg {
  98. unsigned int threads; /**< Maximum number of threads to use, default 1 */
  99. unsigned int w; /**< Width */
  100. unsigned int h; /**< Height */
  101. } vpx_codec_dec_cfg_t; /**< alias for struct vpx_codec_dec_cfg */
  102. /*!\brief Initialize a decoder instance
  103. *
  104. * Initializes a decoder context using the given interface. Applications
  105. * should call the vpx_codec_dec_init convenience macro instead of this
  106. * function directly, to ensure that the ABI version number parameter
  107. * is properly initialized.
  108. *
  109. * If the library was configured with --disable-multithread, this call
  110. * is not thread safe and should be guarded with a lock if being used
  111. * in a multithreaded context.
  112. *
  113. * \param[in] ctx Pointer to this instance's context.
  114. * \param[in] iface Pointer to the algorithm interface to use.
  115. * \param[in] cfg Configuration to use, if known. May be NULL.
  116. * \param[in] flags Bitfield of VPX_CODEC_USE_* flags
  117. * \param[in] ver ABI version number. Must be set to
  118. * VPX_DECODER_ABI_VERSION
  119. * \retval #VPX_CODEC_OK
  120. * The decoder algorithm initialized.
  121. * \retval #VPX_CODEC_MEM_ERROR
  122. * Memory allocation failed.
  123. */
  124. vpx_codec_err_t vpx_codec_dec_init_ver(vpx_codec_ctx_t *ctx,
  125. vpx_codec_iface_t *iface,
  126. const vpx_codec_dec_cfg_t *cfg,
  127. vpx_codec_flags_t flags, int ver);
  128. /*!\brief Convenience macro for vpx_codec_dec_init_ver()
  129. *
  130. * Ensures the ABI version parameter is properly set.
  131. */
  132. #define vpx_codec_dec_init(ctx, iface, cfg, flags) \
  133. vpx_codec_dec_init_ver(ctx, iface, cfg, flags, VPX_DECODER_ABI_VERSION)
  134. /*!\brief Parse stream info from a buffer
  135. *
  136. * Performs high level parsing of the bitstream. Construction of a decoder
  137. * context is not necessary. Can be used to determine if the bitstream is
  138. * of the proper format, and to extract information from the stream.
  139. *
  140. * \param[in] iface Pointer to the algorithm interface
  141. * \param[in] data Pointer to a block of data to parse
  142. * \param[in] data_sz Size of the data buffer
  143. * \param[in,out] si Pointer to stream info to update. The size member
  144. * \ref MUST be properly initialized, but \ref MAY be
  145. * clobbered by the algorithm. This parameter \ref MAY
  146. * be NULL.
  147. *
  148. * \retval #VPX_CODEC_OK
  149. * Bitstream is parsable and stream information updated
  150. */
  151. vpx_codec_err_t vpx_codec_peek_stream_info(vpx_codec_iface_t *iface,
  152. const uint8_t *data,
  153. unsigned int data_sz,
  154. vpx_codec_stream_info_t *si);
  155. /*!\brief Return information about the current stream.
  156. *
  157. * Returns information about the stream that has been parsed during decoding.
  158. *
  159. * \param[in] ctx Pointer to this instance's context
  160. * \param[in,out] si Pointer to stream info to update. The size member
  161. * \ref MUST be properly initialized, but \ref MAY be
  162. * clobbered by the algorithm. This parameter \ref MAY
  163. * be NULL.
  164. *
  165. * \retval #VPX_CODEC_OK
  166. * Bitstream is parsable and stream information updated
  167. */
  168. vpx_codec_err_t vpx_codec_get_stream_info(vpx_codec_ctx_t *ctx,
  169. vpx_codec_stream_info_t *si);
  170. /*!\brief Decode data
  171. *
  172. * Processes a buffer of coded data. If the processing results in a new
  173. * decoded frame becoming available, PUT_SLICE and PUT_FRAME events may be
  174. * generated, as appropriate. Encoded data \ref MUST be passed in DTS (decode
  175. * time stamp) order. Frames produced will always be in PTS (presentation
  176. * time stamp) order.
  177. * If the decoder is configured with VPX_CODEC_USE_INPUT_FRAGMENTS enabled,
  178. * data and data_sz can contain a fragment of the encoded frame. Fragment
  179. * \#n must contain at least partition \#n, but can also contain subsequent
  180. * partitions (\#n+1 - \#n+i), and if so, fragments \#n+1, .., \#n+i must
  181. * be empty. When no more data is available, this function should be called
  182. * with NULL as data and 0 as data_sz. The memory passed to this function
  183. * must be available until the frame has been decoded.
  184. *
  185. * \param[in] ctx Pointer to this instance's context
  186. * \param[in] data Pointer to this block of new coded data. If
  187. * NULL, a VPX_CODEC_CB_PUT_FRAME event is posted
  188. * for the previously decoded frame.
  189. * \param[in] data_sz Size of the coded data, in bytes.
  190. * \param[in] user_priv Application specific data to associate with
  191. * this frame.
  192. * \param[in] deadline Soft deadline the decoder should attempt to meet,
  193. * in us. Set to zero for unlimited.
  194. *
  195. * \return Returns #VPX_CODEC_OK if the coded data was processed completely
  196. * and future pictures can be decoded without error. Otherwise,
  197. * see the descriptions of the other error codes in ::vpx_codec_err_t
  198. * for recoverability capabilities.
  199. */
  200. vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, const uint8_t *data,
  201. unsigned int data_sz, void *user_priv,
  202. long deadline);
  203. /*!\brief Decoded frames iterator
  204. *
  205. * Iterates over a list of the frames available for display. The iterator
  206. * storage should be initialized to NULL to start the iteration. Iteration is
  207. * complete when this function returns NULL.
  208. *
  209. * The list of available frames becomes valid upon completion of the
  210. * vpx_codec_decode call, and remains valid until the next call to
  211. * vpx_codec_decode.
  212. *
  213. * \param[in] ctx Pointer to this instance's context
  214. * \param[in,out] iter Iterator storage, initialized to NULL
  215. *
  216. * \return Returns a pointer to an image, if one is ready for display. Frames
  217. * produced will always be in PTS (presentation time stamp) order.
  218. */
  219. vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter);
  220. /*!\defgroup cap_put_frame Frame-Based Decoding Functions
  221. *
  222. * The following functions are required to be implemented for all decoders
  223. * that advertise the VPX_CODEC_CAP_PUT_FRAME capability. Calling these
  224. * functions
  225. * for codecs that don't advertise this capability will result in an error
  226. * code being returned, usually VPX_CODEC_ERROR
  227. * @{
  228. */
  229. /*!\brief put frame callback prototype
  230. *
  231. * This callback is invoked by the decoder to notify the application of
  232. * the availability of decoded image data.
  233. */
  234. typedef void (*vpx_codec_put_frame_cb_fn_t)(void *user_priv,
  235. const vpx_image_t *img);
  236. /*!\brief Register for notification of frame completion.
  237. *
  238. * Registers a given function to be called when a decoded frame is
  239. * available.
  240. *
  241. * \param[in] ctx Pointer to this instance's context
  242. * \param[in] cb Pointer to the callback function
  243. * \param[in] user_priv User's private data
  244. *
  245. * \retval #VPX_CODEC_OK
  246. * Callback successfully registered.
  247. * \retval #VPX_CODEC_ERROR
  248. * Decoder context not initialized, or algorithm not capable of
  249. * posting slice completion.
  250. */
  251. vpx_codec_err_t vpx_codec_register_put_frame_cb(vpx_codec_ctx_t *ctx,
  252. vpx_codec_put_frame_cb_fn_t cb,
  253. void *user_priv);
  254. /*!@} - end defgroup cap_put_frame */
  255. /*!\defgroup cap_put_slice Slice-Based Decoding Functions
  256. *
  257. * The following functions are required to be implemented for all decoders
  258. * that advertise the VPX_CODEC_CAP_PUT_SLICE capability. Calling these
  259. * functions
  260. * for codecs that don't advertise this capability will result in an error
  261. * code being returned, usually VPX_CODEC_ERROR
  262. * @{
  263. */
  264. /*!\brief put slice callback prototype
  265. *
  266. * This callback is invoked by the decoder to notify the application of
  267. * the availability of partially decoded image data. The
  268. */
  269. typedef void (*vpx_codec_put_slice_cb_fn_t)(void *user_priv,
  270. const vpx_image_t *img,
  271. const vpx_image_rect_t *valid,
  272. const vpx_image_rect_t *update);
  273. /*!\brief Register for notification of slice completion.
  274. *
  275. * Registers a given function to be called when a decoded slice is
  276. * available.
  277. *
  278. * \param[in] ctx Pointer to this instance's context
  279. * \param[in] cb Pointer to the callback function
  280. * \param[in] user_priv User's private data
  281. *
  282. * \retval #VPX_CODEC_OK
  283. * Callback successfully registered.
  284. * \retval #VPX_CODEC_ERROR
  285. * Decoder context not initialized, or algorithm not capable of
  286. * posting slice completion.
  287. */
  288. vpx_codec_err_t vpx_codec_register_put_slice_cb(vpx_codec_ctx_t *ctx,
  289. vpx_codec_put_slice_cb_fn_t cb,
  290. void *user_priv);
  291. /*!@} - end defgroup cap_put_slice*/
  292. /*!\defgroup cap_external_frame_buffer External Frame Buffer Functions
  293. *
  294. * The following section is required to be implemented for all decoders
  295. * that advertise the VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER capability.
  296. * Calling this function for codecs that don't advertise this capability
  297. * will result in an error code being returned, usually VPX_CODEC_ERROR.
  298. *
  299. * \note
  300. * Currently this only works with VP9.
  301. * @{
  302. */
  303. /*!\brief Pass in external frame buffers for the decoder to use.
  304. *
  305. * Registers functions to be called when libvpx needs a frame buffer
  306. * to decode the current frame and a function to be called when libvpx does
  307. * not internally reference the frame buffer. This set function must
  308. * be called before the first call to decode or libvpx will assume the
  309. * default behavior of allocating frame buffers internally.
  310. *
  311. * \param[in] ctx Pointer to this instance's context
  312. * \param[in] cb_get Pointer to the get callback function
  313. * \param[in] cb_release Pointer to the release callback function
  314. * \param[in] cb_priv Callback's private data
  315. *
  316. * \retval #VPX_CODEC_OK
  317. * External frame buffers will be used by libvpx.
  318. * \retval #VPX_CODEC_INVALID_PARAM
  319. * One or more of the callbacks were NULL.
  320. * \retval #VPX_CODEC_ERROR
  321. * Decoder context not initialized, or algorithm not capable of
  322. * using external frame buffers.
  323. *
  324. * \note
  325. * When decoding VP9, the application may be required to pass in at least
  326. * #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS external frame
  327. * buffers.
  328. */
  329. vpx_codec_err_t vpx_codec_set_frame_buffer_functions(
  330. vpx_codec_ctx_t *ctx, vpx_get_frame_buffer_cb_fn_t cb_get,
  331. vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv);
  332. /*!@} - end defgroup cap_external_frame_buffer */
  333. /*!@} - end defgroup decoder*/
  334. #ifdef __cplusplus
  335. }
  336. #endif
  337. #endif // VPX_VPX_VPX_DECODER_H_