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