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