vpx_decoder.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. * \param[in] ctx Pointer to this instance's context.
  113. * \param[in] iface Pointer to the algorithm interface to use.
  114. * \param[in] cfg Configuration to use, if known. May be NULL.
  115. * \param[in] flags Bitfield of VPX_CODEC_USE_* flags
  116. * \param[in] ver ABI version number. Must be set to
  117. * VPX_DECODER_ABI_VERSION
  118. * \retval #VPX_CODEC_OK
  119. * The decoder algorithm initialized.
  120. * \retval #VPX_CODEC_MEM_ERROR
  121. * Memory allocation failed.
  122. */
  123. vpx_codec_err_t vpx_codec_dec_init_ver(vpx_codec_ctx_t *ctx,
  124. vpx_codec_iface_t *iface,
  125. const vpx_codec_dec_cfg_t *cfg,
  126. vpx_codec_flags_t flags,
  127. 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,
  201. const uint8_t *data,
  202. unsigned int data_sz,
  203. void *user_priv,
  204. long deadline);
  205. /*!\brief Decoded frames iterator
  206. *
  207. * Iterates over a list of the frames available for display. The iterator
  208. * storage should be initialized to NULL to start the iteration. Iteration is
  209. * complete when this function returns NULL.
  210. *
  211. * The list of available frames becomes valid upon completion of the
  212. * vpx_codec_decode call, and remains valid until the next call to vpx_codec_decode.
  213. *
  214. * \param[in] ctx Pointer to this instance's context
  215. * \param[in,out] iter Iterator storage, initialized to NULL
  216. *
  217. * \return Returns a pointer to an image, if one is ready for display. Frames
  218. * produced will always be in PTS (presentation time stamp) order.
  219. */
  220. vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx,
  221. vpx_codec_iter_t *iter);
  222. /*!\defgroup cap_put_frame Frame-Based Decoding Functions
  223. *
  224. * The following functions are required to be implemented for all decoders
  225. * that advertise the VPX_CODEC_CAP_PUT_FRAME capability. Calling these functions
  226. * for codecs that don't advertise this capability will result in an error
  227. * code being returned, usually VPX_CODEC_ERROR
  228. * @{
  229. */
  230. /*!\brief put frame callback prototype
  231. *
  232. * This callback is invoked by the decoder to notify the application of
  233. * the availability of decoded image data.
  234. */
  235. typedef void (*vpx_codec_put_frame_cb_fn_t)(void *user_priv,
  236. const vpx_image_t *img);
  237. /*!\brief Register for notification of frame completion.
  238. *
  239. * Registers a given function to be called when a decoded frame is
  240. * available.
  241. *
  242. * \param[in] ctx Pointer to this instance's context
  243. * \param[in] cb Pointer to the callback function
  244. * \param[in] user_priv User's private data
  245. *
  246. * \retval #VPX_CODEC_OK
  247. * Callback successfully registered.
  248. * \retval #VPX_CODEC_ERROR
  249. * Decoder context not initialized, or algorithm not capable of
  250. * posting slice completion.
  251. */
  252. vpx_codec_err_t vpx_codec_register_put_frame_cb(vpx_codec_ctx_t *ctx,
  253. vpx_codec_put_frame_cb_fn_t cb,
  254. void *user_priv);
  255. /*!@} - end defgroup cap_put_frame */
  256. /*!\defgroup cap_put_slice Slice-Based Decoding Functions
  257. *
  258. * The following functions are required to be implemented for all decoders
  259. * that advertise the VPX_CODEC_CAP_PUT_SLICE capability. Calling these 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,
  331. vpx_get_frame_buffer_cb_fn_t cb_get,
  332. vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv);
  333. /*!@} - end defgroup cap_external_frame_buffer */
  334. /*!@} - end defgroup decoder*/
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #endif // VPX_VPX_DECODER_H_