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