vpx_decoder.h 15 KB

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