2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
13 * \brief Describes the decoder algorithm interface for algorithm
16 * This file defines the private structures and data types that are only
17 * relevant to implementing an algorithm, as opposed to using it.
19 * To create a decoder algorithm class, an interface structure is put
20 * into the global namespace:
23 * vpx_codec_iface_t my_codec = {
25 * VPX_CODEC_ALG_ABI_VERSION,
30 * An application instantiates a specific decoder instance by using
31 * vpx_codec_init() and a pointer to the algorithm's interface structure:
34 * extern vpx_codec_iface_t my_codec;
36 * vpx_codec_ctx_t algo;
37 * res = vpx_codec_init(&algo, &my_codec);
41 * Once initialized, the instance is manged using other functions from
42 * the vpx_codec_* family.
44 #ifndef VPX_CODEC_INTERNAL_H
45 #define VPX_CODEC_INTERNAL_H
46 #include "../vpx_decoder.h"
47 #include "../vpx_encoder.h"
51 /*!\brief Current ABI version number
54 * If this file is altered in any way that changes the ABI, this value
55 * must be bumped. Examples include, but are not limited to, changing
56 * types, removing or reassigning enums, adding/removing/rearranging
57 * fields to structures
59 #define VPX_CODEC_INTERNAL_ABI_VERSION (4) /**<\hideinitializer*/
61 typedef struct vpx_codec_alg_priv vpx_codec_alg_priv_t;
62 typedef struct vpx_codec_priv_enc_mr_cfg vpx_codec_priv_enc_mr_cfg_t;
64 /*!\brief init function pointer prototype
66 * Performs algorithm-specific initialization of the decoder context. This
67 * function is called by the generic vpx_codec_init() wrapper function, so
68 * plugins implementing this interface may trust the input parameters to be
69 * properly initialized.
71 * \param[in] ctx Pointer to this instance's context
72 * \retval #VPX_CODEC_OK
73 * The input stream was recognized and decoder initialized.
74 * \retval #VPX_CODEC_MEM_ERROR
75 * Memory operation failed.
77 typedef vpx_codec_err_t (*vpx_codec_init_fn_t)(vpx_codec_ctx_t *ctx,
78 vpx_codec_priv_enc_mr_cfg_t *data);
80 /*!\brief destroy function pointer prototype
82 * Performs algorithm-specific destruction of the decoder context. This
83 * function is called by the generic vpx_codec_destroy() wrapper function,
84 * so plugins implementing this interface may trust the input parameters
85 * to be properly initialized.
87 * \param[in] ctx Pointer to this instance's context
88 * \retval #VPX_CODEC_OK
89 * The input stream was recognized and decoder initialized.
90 * \retval #VPX_CODEC_MEM_ERROR
91 * Memory operation failed.
93 typedef vpx_codec_err_t (*vpx_codec_destroy_fn_t)(vpx_codec_alg_priv_t *ctx);
95 /*!\brief parse stream info function pointer prototype
97 * Performs high level parsing of the bitstream. This function is called by
98 * the generic vpx_codec_parse_stream() wrapper function, so plugins implementing
99 * this interface may trust the input parameters to be properly initialized.
101 * \param[in] data Pointer to a block of data to parse
102 * \param[in] data_sz Size of the data buffer
103 * \param[in,out] si Pointer to stream info to update. The size member
104 * \ref MUST be properly initialized, but \ref MAY be
105 * clobbered by the algorithm. This parameter \ref MAY
108 * \retval #VPX_CODEC_OK
109 * Bitstream is parsable and stream information updated
111 typedef vpx_codec_err_t (*vpx_codec_peek_si_fn_t)(const uint8_t *data,
112 unsigned int data_sz,
113 vpx_codec_stream_info_t *si);
115 /*!\brief Return information about the current stream.
117 * Returns information about the stream that has been parsed during decoding.
119 * \param[in] ctx Pointer to this instance's context
120 * \param[in,out] si Pointer to stream info to update. The size member
121 * \ref MUST be properly initialized, but \ref MAY be
122 * clobbered by the algorithm. This parameter \ref MAY
125 * \retval #VPX_CODEC_OK
126 * Bitstream is parsable and stream information updated
128 typedef vpx_codec_err_t (*vpx_codec_get_si_fn_t)(vpx_codec_alg_priv_t *ctx,
129 vpx_codec_stream_info_t *si);
131 /*!\brief control function pointer prototype
133 * This function is used to exchange algorithm specific data with the decoder
134 * instance. This can be used to implement features specific to a particular
137 * This function is called by the generic vpx_codec_control() wrapper
138 * function, so plugins implementing this interface may trust the input
139 * parameters to be properly initialized. However, this interface does not
140 * provide type safety for the exchanged data or assign meanings to the
141 * control codes. Those details should be specified in the algorithm's
142 * header file. In particular, the ctrl_id parameter is guaranteed to exist
143 * in the algorithm's control mapping table, and the data parameter may be NULL.
146 * \param[in] ctx Pointer to this instance's context
147 * \param[in] ctrl_id Algorithm specific control identifier
148 * \param[in,out] data Data to exchange with algorithm instance.
150 * \retval #VPX_CODEC_OK
151 * The internal state data was deserialized.
153 typedef vpx_codec_err_t (*vpx_codec_control_fn_t)(vpx_codec_alg_priv_t *ctx,
157 /*!\brief control function pointer mapping
159 * This structure stores the mapping between control identifiers and
160 * implementing functions. Each algorithm provides a list of these
161 * mappings. This list is searched by the vpx_codec_control() wrapper
162 * function to determine which function to invoke. The special
163 * value {0, NULL} is used to indicate end-of-list, and must be
164 * present. The special value {0, <non-null>} can be used as a catch-all
165 * mapping. This implies that ctrl_id values chosen by the algorithm
166 * \ref MUST be non-zero.
168 typedef const struct vpx_codec_ctrl_fn_map {
170 vpx_codec_control_fn_t fn;
171 } vpx_codec_ctrl_fn_map_t;
173 /*!\brief decode data function pointer prototype
175 * Processes a buffer of coded data. If the processing results in a new
176 * decoded frame becoming available, #VPX_CODEC_CB_PUT_SLICE and
177 * #VPX_CODEC_CB_PUT_FRAME events are generated as appropriate. This
178 * function is called by the generic vpx_codec_decode() wrapper function,
179 * so plugins implementing this interface may trust the input parameters
180 * to be properly initialized.
182 * \param[in] ctx Pointer to this instance's context
183 * \param[in] data Pointer to this block of new coded data. If
184 * NULL, a #VPX_CODEC_CB_PUT_FRAME event is posted
185 * for the previously decoded frame.
186 * \param[in] data_sz Size of the coded data, in bytes.
188 * \return Returns #VPX_CODEC_OK if the coded data was processed completely
189 * and future pictures can be decoded without error. Otherwise,
190 * see the descriptions of the other error codes in ::vpx_codec_err_t
191 * for recoverability capabilities.
193 typedef vpx_codec_err_t (*vpx_codec_decode_fn_t)(vpx_codec_alg_priv_t *ctx,
195 unsigned int data_sz,
199 /*!\brief Decoded frames iterator
201 * Iterates over a list of the frames available for display. The iterator
202 * storage should be initialized to NULL to start the iteration. Iteration is
203 * complete when this function returns NULL.
205 * The list of available frames becomes valid upon completion of the
206 * vpx_codec_decode call, and remains valid until the next call to vpx_codec_decode.
208 * \param[in] ctx Pointer to this instance's context
209 * \param[in out] iter Iterator storage, initialized to NULL
211 * \return Returns a pointer to an image, if one is ready for display. Frames
212 * produced will always be in PTS (presentation time stamp) order.
214 typedef vpx_image_t *(*vpx_codec_get_frame_fn_t)(vpx_codec_alg_priv_t *ctx,
215 vpx_codec_iter_t *iter);
218 /*\brief eXternal Memory Allocation memory map get iterator
220 * Iterates over a list of the memory maps requested by the decoder. The
221 * iterator storage should be initialized to NULL to start the iteration.
222 * Iteration is complete when this function returns NULL.
224 * \param[in out] iter Iterator storage, initialized to NULL
226 * \return Returns a pointer to an memory segment descriptor, or NULL to
227 * indicate end-of-list.
229 typedef vpx_codec_err_t (*vpx_codec_get_mmap_fn_t)(const vpx_codec_ctx_t *ctx,
230 vpx_codec_mmap_t *mmap,
231 vpx_codec_iter_t *iter);
234 /*\brief eXternal Memory Allocation memory map set iterator
236 * Sets a memory descriptor inside the decoder instance.
238 * \param[in] ctx Pointer to this instance's context
239 * \param[in] mmap Memory map to store.
241 * \retval #VPX_CODEC_OK
242 * The memory map was accepted and stored.
243 * \retval #VPX_CODEC_MEM_ERROR
244 * The memory map was rejected.
246 typedef vpx_codec_err_t (*vpx_codec_set_mmap_fn_t)(vpx_codec_ctx_t *ctx,
247 const vpx_codec_mmap_t *mmap);
250 typedef vpx_codec_err_t (*vpx_codec_encode_fn_t)(vpx_codec_alg_priv_t *ctx,
251 const vpx_image_t *img,
253 unsigned long duration,
254 vpx_enc_frame_flags_t flags,
255 unsigned long deadline);
256 typedef const vpx_codec_cx_pkt_t *(*vpx_codec_get_cx_data_fn_t)(vpx_codec_alg_priv_t *ctx,
257 vpx_codec_iter_t *iter);
259 typedef vpx_codec_err_t
260 (*vpx_codec_enc_config_set_fn_t)(vpx_codec_alg_priv_t *ctx,
261 const vpx_codec_enc_cfg_t *cfg);
262 typedef vpx_fixed_buf_t *
263 (*vpx_codec_get_global_headers_fn_t)(vpx_codec_alg_priv_t *ctx);
265 typedef vpx_image_t *
266 (*vpx_codec_get_preview_frame_fn_t)(vpx_codec_alg_priv_t *ctx);
268 typedef vpx_codec_err_t
269 (*vpx_codec_enc_mr_get_mem_loc_fn_t)(const vpx_codec_enc_cfg_t *cfg,
272 /*!\brief usage configuration mapping
274 * This structure stores the mapping between usage identifiers and
275 * configuration structures. Each algorithm provides a list of these
276 * mappings. This list is searched by the vpx_codec_enc_config_default()
277 * wrapper function to determine which config to return. The special value
278 * {-1, {0}} is used to indicate end-of-list, and must be present. At least
279 * one mapping must be present, in addition to the end-of-list.
282 typedef const struct vpx_codec_enc_cfg_map {
284 vpx_codec_enc_cfg_t cfg;
285 } vpx_codec_enc_cfg_map_t;
287 #define NOT_IMPLEMENTED 0
289 /*!\brief Decoder algorithm interface interface
291 * All decoders \ref MUST expose a variable of this type.
293 struct vpx_codec_iface {
294 const char *name; /**< Identification String */
295 int abi_version; /**< Implemented ABI version */
296 vpx_codec_caps_t caps; /**< Decoder capabilities */
297 vpx_codec_init_fn_t init; /**< \copydoc ::vpx_codec_init_fn_t */
298 vpx_codec_destroy_fn_t destroy; /**< \copydoc ::vpx_codec_destroy_fn_t */
299 vpx_codec_ctrl_fn_map_t *ctrl_maps; /**< \copydoc ::vpx_codec_ctrl_fn_map_t */
300 vpx_codec_get_mmap_fn_t get_mmap; /**< \copydoc ::vpx_codec_get_mmap_fn_t */
301 vpx_codec_set_mmap_fn_t set_mmap; /**< \copydoc ::vpx_codec_set_mmap_fn_t */
302 struct vpx_codec_dec_iface {
303 vpx_codec_peek_si_fn_t peek_si; /**< \copydoc ::vpx_codec_peek_si_fn_t */
304 vpx_codec_get_si_fn_t get_si; /**< \copydoc ::vpx_codec_peek_si_fn_t */
305 vpx_codec_decode_fn_t decode; /**< \copydoc ::vpx_codec_decode_fn_t */
306 vpx_codec_get_frame_fn_t get_frame; /**< \copydoc ::vpx_codec_get_frame_fn_t */
308 struct vpx_codec_enc_iface {
309 vpx_codec_enc_cfg_map_t *cfg_maps; /**< \copydoc ::vpx_codec_enc_cfg_map_t */
310 vpx_codec_encode_fn_t encode; /**< \copydoc ::vpx_codec_encode_fn_t */
311 vpx_codec_get_cx_data_fn_t get_cx_data; /**< \copydoc ::vpx_codec_get_cx_data_fn_t */
312 vpx_codec_enc_config_set_fn_t cfg_set; /**< \copydoc ::vpx_codec_enc_config_set_fn_t */
313 vpx_codec_get_global_headers_fn_t get_glob_hdrs; /**< \copydoc ::vpx_codec_get_global_headers_fn_t */
314 vpx_codec_get_preview_frame_fn_t get_preview; /**< \copydoc ::vpx_codec_get_preview_frame_fn_t */
315 vpx_codec_enc_mr_get_mem_loc_fn_t mr_get_mem_loc; /**< \copydoc ::vpx_codec_enc_mr_get_mem_loc_fn_t */
319 /*!\brief Callback function pointer / user data pair storage */
320 typedef struct vpx_codec_priv_cb_pair {
322 vpx_codec_put_frame_cb_fn_t put_frame;
323 vpx_codec_put_slice_cb_fn_t put_slice;
326 } vpx_codec_priv_cb_pair_t;
329 /*!\brief Instance private storage
331 * This structure is allocated by the algorithm's init function. It can be
332 * extended in one of two ways. First, a second, algorithm specific structure
333 * can be allocated and the priv member pointed to it. Alternatively, this
334 * structure can be made the first member of the algorithm specific structure,
335 * and the pointer cast to the proper type.
337 struct vpx_codec_priv {
339 vpx_codec_iface_t *iface;
340 struct vpx_codec_alg_priv *alg_priv;
341 const char *err_detail;
342 vpx_codec_flags_t init_flags;
344 vpx_codec_priv_cb_pair_t put_frame_cb;
345 vpx_codec_priv_cb_pair_t put_slice_cb;
349 struct vpx_fixed_buf cx_data_dst_buf;
350 unsigned int cx_data_pad_before;
351 unsigned int cx_data_pad_after;
352 vpx_codec_cx_pkt_t cx_data_pkt;
353 unsigned int total_encoders;
358 * Multi-resolution encoding internal configuration
360 struct vpx_codec_priv_enc_mr_cfg
362 unsigned int mr_total_resolutions;
363 unsigned int mr_encoder_id;
364 struct vpx_rational mr_down_sampling_factor;
365 void* mr_low_res_mode_info;
368 #undef VPX_CTRL_USE_TYPE
369 #define VPX_CTRL_USE_TYPE(id, typ) \
370 static typ id##__value(va_list args) {return va_arg(args, typ);} \
371 static typ id##__convert(void *x)\
383 #undef VPX_CTRL_USE_TYPE_DEPRECATED
384 #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \
385 static typ id##__value(va_list args) {return va_arg(args, typ);} \
386 static typ id##__convert(void *x)\
397 #define CAST(id, arg) id##__value(arg)
398 #define RECAST(id, x) id##__convert(x)
401 /* CODEC_INTERFACE convenience macro
403 * By convention, each codec interface is a struct with extern linkage, where
404 * the symbol is suffixed with _algo. A getter function is also defined to
405 * return a pointer to the struct, since in some cases it's easier to work
406 * with text symbols than data symbols (see issue #169). This function has
407 * the same name as the struct, less the _algo suffix. The CODEC_INTERFACE
408 * macro is provided to define this getter function automatically.
410 #define CODEC_INTERFACE(id)\
411 vpx_codec_iface_t* id(void) { return &id##_algo; }\
412 vpx_codec_iface_t id##_algo
415 /* Internal Utility Functions
417 * The following functions are intended to be used inside algorithms as
418 * utilities for manipulating vpx_codec_* data structures.
420 struct vpx_codec_pkt_list {
423 struct vpx_codec_cx_pkt pkts[1];
426 #define vpx_codec_pkt_list_decl(n)\
427 union {struct vpx_codec_pkt_list head;\
428 struct {struct vpx_codec_pkt_list head;\
429 struct vpx_codec_cx_pkt pkts[n];} alloc;}
431 #define vpx_codec_pkt_list_init(m)\
432 (m)->alloc.head.cnt = 0,\
433 (m)->alloc.head.max = sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0])
436 vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *,
437 const struct vpx_codec_cx_pkt *);
439 const vpx_codec_cx_pkt_t *
440 vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
441 vpx_codec_iter_t *iter);
446 struct vpx_internal_error_info {
447 vpx_codec_err_t error_code;
454 static void vpx_internal_error(struct vpx_internal_error_info *info,
455 vpx_codec_err_t error,
460 info->error_code = error;
461 info->has_detail = 0;
464 size_t sz = sizeof(info->detail);
466 info->has_detail = 1;
468 vsnprintf(info->detail, sz - 1, fmt, ap);
470 info->detail[sz - 1] = '\0';
474 longjmp(info->jmp, info->error_code);