Home | History | Annotate | Download | only in internal
      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 
     11 
     12 /*!\file
     13  * \brief Describes the decoder algorithm interface for algorithm
     14  *        implementations.
     15  *
     16  * This file defines the private structures and data types that are only
     17  * relevant to implementing an algorithm, as opposed to using it.
     18  *
     19  * To create a decoder algorithm class, an interface structure is put
     20  * into the global namespace:
     21  *     <pre>
     22  *     my_codec.c:
     23  *       vpx_codec_iface_t my_codec = {
     24  *           "My Codec v1.0",
     25  *           VPX_CODEC_ALG_ABI_VERSION,
     26  *           ...
     27  *       };
     28  *     </pre>
     29  *
     30  * An application instantiates a specific decoder instance by using
     31  * vpx_codec_init() and a pointer to the algorithm's interface structure:
     32  *     <pre>
     33  *     my_app.c:
     34  *       extern vpx_codec_iface_t my_codec;
     35  *       {
     36  *           vpx_codec_ctx_t algo;
     37  *           res = vpx_codec_init(&algo, &my_codec);
     38  *       }
     39  *     </pre>
     40  *
     41  * Once initialized, the instance is manged using other functions from
     42  * the vpx_codec_* family.
     43  */
     44 #ifndef VPX_CODEC_INTERNAL_H
     45 #define VPX_CODEC_INTERNAL_H
     46 #include "../vpx_decoder.h"
     47 #include "../vpx_encoder.h"
     48 #include <stdarg.h>
     49 
     50 
     51 /*!\brief Current ABI version number
     52  *
     53  * \internal
     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
     58  */
     59 #define VPX_CODEC_INTERNAL_ABI_VERSION (4) /**<\hideinitializer*/
     60 
     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;
     63 
     64 /*!\brief init function pointer prototype
     65  *
     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.
     70  *
     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.
     76  */
     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);
     79 
     80 /*!\brief destroy function pointer prototype
     81  *
     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.
     86  *
     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.
     92  */
     93 typedef vpx_codec_err_t (*vpx_codec_destroy_fn_t)(vpx_codec_alg_priv_t *ctx);
     94 
     95 /*!\brief parse stream info function pointer prototype
     96  *
     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.
    100  *
    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
    106  *                         be NULL.
    107  *
    108  * \retval #VPX_CODEC_OK
    109  *     Bitstream is parsable and stream information updated
    110  */
    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);
    114 
    115 /*!\brief Return information about the current stream.
    116  *
    117  * Returns information about the stream that has been parsed during decoding.
    118  *
    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
    123  *                         be NULL.
    124  *
    125  * \retval #VPX_CODEC_OK
    126  *     Bitstream is parsable and stream information updated
    127  */
    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);
    130 
    131 /*!\brief control function pointer prototype
    132  *
    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
    135  * algorithm.
    136  *
    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.
    144  *
    145  *
    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.
    149  *
    150  * \retval #VPX_CODEC_OK
    151  *     The internal state data was deserialized.
    152  */
    153 typedef vpx_codec_err_t (*vpx_codec_control_fn_t)(vpx_codec_alg_priv_t  *ctx,
    154         int                  ctrl_id,
    155         va_list              ap);
    156 
    157 /*!\brief control function pointer mapping
    158  *
    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.
    167  */
    168 typedef const struct vpx_codec_ctrl_fn_map
    169 {
    170     int                    ctrl_id;
    171     vpx_codec_control_fn_t   fn;
    172 } vpx_codec_ctrl_fn_map_t;
    173 
    174 /*!\brief decode data function pointer prototype
    175  *
    176  * Processes a buffer of coded data. If the processing results in a new
    177  * decoded frame becoming available, #VPX_CODEC_CB_PUT_SLICE and
    178  * #VPX_CODEC_CB_PUT_FRAME events are generated as appropriate. This
    179  * function is called by the generic vpx_codec_decode() wrapper function,
    180  * so plugins implementing this interface may trust the input parameters
    181  * to be properly initialized.
    182  *
    183  * \param[in] ctx          Pointer to this instance's context
    184  * \param[in] data         Pointer to this block of new coded data. If
    185  *                         NULL, a #VPX_CODEC_CB_PUT_FRAME event is posted
    186  *                         for the previously decoded frame.
    187  * \param[in] data_sz      Size of the coded data, in bytes.
    188  *
    189  * \return Returns #VPX_CODEC_OK if the coded data was processed completely
    190  *         and future pictures can be decoded without error. Otherwise,
    191  *         see the descriptions of the other error codes in ::vpx_codec_err_t
    192  *         for recoverability capabilities.
    193  */
    194 typedef vpx_codec_err_t (*vpx_codec_decode_fn_t)(vpx_codec_alg_priv_t  *ctx,
    195         const uint8_t         *data,
    196         unsigned int     data_sz,
    197         void        *user_priv,
    198         long         deadline);
    199 
    200 /*!\brief Decoded frames iterator
    201  *
    202  * Iterates over a list of the frames available for display. The iterator
    203  * storage should be initialized to NULL to start the iteration. Iteration is
    204  * complete when this function returns NULL.
    205  *
    206  * The list of available frames becomes valid upon completion of the
    207  * vpx_codec_decode call, and remains valid until the next call to vpx_codec_decode.
    208  *
    209  * \param[in]     ctx      Pointer to this instance's context
    210  * \param[in out] iter     Iterator storage, initialized to NULL
    211  *
    212  * \return Returns a pointer to an image, if one is ready for display. Frames
    213  *         produced will always be in PTS (presentation time stamp) order.
    214  */
    215 typedef vpx_image_t*(*vpx_codec_get_frame_fn_t)(vpx_codec_alg_priv_t *ctx,
    216         vpx_codec_iter_t     *iter);
    217 
    218 
    219 /*\brief eXternal Memory Allocation memory map get iterator
    220  *
    221  * Iterates over a list of the memory maps requested by the decoder. The
    222  * iterator storage should be initialized to NULL to start the iteration.
    223  * Iteration is complete when this function returns NULL.
    224  *
    225  * \param[in out] iter     Iterator storage, initialized to NULL
    226  *
    227  * \return Returns a pointer to an memory segment descriptor, or NULL to
    228  *         indicate end-of-list.
    229  */
    230 typedef vpx_codec_err_t (*vpx_codec_get_mmap_fn_t)(const vpx_codec_ctx_t      *ctx,
    231         vpx_codec_mmap_t           *mmap,
    232         vpx_codec_iter_t           *iter);
    233 
    234 
    235 /*\brief eXternal Memory Allocation memory map set iterator
    236  *
    237  * Sets a memory descriptor inside the decoder instance.
    238  *
    239  * \param[in] ctx      Pointer to this instance's context
    240  * \param[in] mmap     Memory map to store.
    241  *
    242  * \retval #VPX_CODEC_OK
    243  *     The memory map was accepted and stored.
    244  * \retval #VPX_CODEC_MEM_ERROR
    245  *     The memory map was rejected.
    246  */
    247 typedef vpx_codec_err_t (*vpx_codec_set_mmap_fn_t)(vpx_codec_ctx_t         *ctx,
    248         const vpx_codec_mmap_t  *mmap);
    249 
    250 
    251 typedef vpx_codec_err_t (*vpx_codec_encode_fn_t)(vpx_codec_alg_priv_t  *ctx,
    252         const vpx_image_t     *img,
    253         vpx_codec_pts_t        pts,
    254         unsigned long          duration,
    255         vpx_enc_frame_flags_t  flags,
    256         unsigned long          deadline);
    257 typedef const vpx_codec_cx_pkt_t*(*vpx_codec_get_cx_data_fn_t)(vpx_codec_alg_priv_t *ctx,
    258         vpx_codec_iter_t     *iter);
    259 
    260 typedef vpx_codec_err_t
    261 (*vpx_codec_enc_config_set_fn_t)(vpx_codec_alg_priv_t       *ctx,
    262                                  const vpx_codec_enc_cfg_t  *cfg);
    263 typedef vpx_fixed_buf_t *
    264 (*vpx_codec_get_global_headers_fn_t)(vpx_codec_alg_priv_t   *ctx);
    265 
    266 typedef vpx_image_t *
    267 (*vpx_codec_get_preview_frame_fn_t)(vpx_codec_alg_priv_t   *ctx);
    268 
    269 typedef vpx_codec_err_t
    270 (*vpx_codec_enc_mr_get_mem_loc_fn_t)(const vpx_codec_enc_cfg_t     *cfg,
    271                                    void **mem_loc);
    272 
    273 /*!\brief usage configuration mapping
    274  *
    275  * This structure stores the mapping between usage identifiers and
    276  * configuration structures. Each algorithm provides a list of these
    277  * mappings. This list is searched by the vpx_codec_enc_config_default()
    278  * wrapper function to determine which config to return. The special value
    279  * {-1, {0}} is used to indicate end-of-list, and must be present. At least
    280  * one mapping must be present, in addition to the end-of-list.
    281  *
    282  */
    283 typedef const struct vpx_codec_enc_cfg_map
    284 {
    285     int                 usage;
    286     vpx_codec_enc_cfg_t cfg;
    287 } vpx_codec_enc_cfg_map_t;
    288 
    289 #define NOT_IMPLEMENTED 0
    290 
    291 /*!\brief Decoder algorithm interface interface
    292  *
    293  * All decoders \ref MUST expose a variable of this type.
    294  */
    295 struct vpx_codec_iface
    296 {
    297     const char               *name;        /**< Identification String  */
    298     int                       abi_version; /**< Implemented ABI version */
    299     vpx_codec_caps_t          caps;    /**< Decoder capabilities */
    300     vpx_codec_init_fn_t       init;    /**< \copydoc ::vpx_codec_init_fn_t */
    301     vpx_codec_destroy_fn_t    destroy;     /**< \copydoc ::vpx_codec_destroy_fn_t */
    302     vpx_codec_ctrl_fn_map_t  *ctrl_maps;   /**< \copydoc ::vpx_codec_ctrl_fn_map_t */
    303     vpx_codec_get_mmap_fn_t   get_mmap;    /**< \copydoc ::vpx_codec_get_mmap_fn_t */
    304     vpx_codec_set_mmap_fn_t   set_mmap;    /**< \copydoc ::vpx_codec_set_mmap_fn_t */
    305     struct vpx_codec_dec_iface
    306     {
    307         vpx_codec_peek_si_fn_t    peek_si;     /**< \copydoc ::vpx_codec_peek_si_fn_t */
    308         vpx_codec_get_si_fn_t     get_si;      /**< \copydoc ::vpx_codec_peek_si_fn_t */
    309         vpx_codec_decode_fn_t     decode;      /**< \copydoc ::vpx_codec_decode_fn_t */
    310         vpx_codec_get_frame_fn_t  get_frame;   /**< \copydoc ::vpx_codec_get_frame_fn_t */
    311     } dec;
    312     struct vpx_codec_enc_iface
    313     {
    314         vpx_codec_enc_cfg_map_t           *cfg_maps;      /**< \copydoc ::vpx_codec_enc_cfg_map_t */
    315         vpx_codec_encode_fn_t              encode;        /**< \copydoc ::vpx_codec_encode_fn_t */
    316         vpx_codec_get_cx_data_fn_t         get_cx_data;   /**< \copydoc ::vpx_codec_get_cx_data_fn_t */
    317         vpx_codec_enc_config_set_fn_t      cfg_set;       /**< \copydoc ::vpx_codec_enc_config_set_fn_t */
    318         vpx_codec_get_global_headers_fn_t  get_glob_hdrs; /**< \copydoc ::vpx_codec_get_global_headers_fn_t */
    319         vpx_codec_get_preview_frame_fn_t   get_preview;   /**< \copydoc ::vpx_codec_get_preview_frame_fn_t */
    320         vpx_codec_enc_mr_get_mem_loc_fn_t  mr_get_mem_loc;   /**< \copydoc ::vpx_codec_enc_mr_get_mem_loc_fn_t */
    321     } enc;
    322 };
    323 
    324 /*!\brief Callback function pointer / user data pair storage */
    325 typedef struct vpx_codec_priv_cb_pair
    326 {
    327     union
    328     {
    329         vpx_codec_put_frame_cb_fn_t    put_frame;
    330         vpx_codec_put_slice_cb_fn_t    put_slice;
    331     } u;
    332     void                            *user_priv;
    333 } vpx_codec_priv_cb_pair_t;
    334 
    335 
    336 /*!\brief Instance private storage
    337  *
    338  * This structure is allocated by the algorithm's init function. It can be
    339  * extended in one of two ways. First, a second, algorithm specific structure
    340  * can be allocated and the priv member pointed to it. Alternatively, this
    341  * structure can be made the first member of the algorithm specific structure,
    342  * and the pointer cast to the proper type.
    343  */
    344 struct vpx_codec_priv
    345 {
    346     unsigned int                    sz;
    347     vpx_codec_iface_t              *iface;
    348     struct vpx_codec_alg_priv      *alg_priv;
    349     const char                     *err_detail;
    350     vpx_codec_flags_t               init_flags;
    351     struct
    352     {
    353         vpx_codec_priv_cb_pair_t    put_frame_cb;
    354         vpx_codec_priv_cb_pair_t    put_slice_cb;
    355     } dec;
    356     struct
    357     {
    358         int                         tbd;
    359         struct vpx_fixed_buf        cx_data_dst_buf;
    360         unsigned int                cx_data_pad_before;
    361         unsigned int                cx_data_pad_after;
    362         vpx_codec_cx_pkt_t          cx_data_pkt;
    363         unsigned int                total_encoders;
    364     } enc;
    365 };
    366 
    367 /*
    368  * Multi-resolution encoding internal configuration
    369  */
    370 struct vpx_codec_priv_enc_mr_cfg
    371 {
    372     unsigned int           mr_total_resolutions;
    373     unsigned int           mr_encoder_id;
    374     struct vpx_rational    mr_down_sampling_factor;
    375     void*                  mr_low_res_mode_info;
    376 };
    377 
    378 #undef VPX_CTRL_USE_TYPE
    379 #define VPX_CTRL_USE_TYPE(id, typ) \
    380     static typ id##__value(va_list args) {return va_arg(args, typ);} \
    381     static typ id##__convert(void *x)\
    382     {\
    383         union\
    384         {\
    385             void *x;\
    386             typ   d;\
    387         } u;\
    388         u.x = x;\
    389         return u.d;\
    390     }
    391 
    392 
    393 #undef VPX_CTRL_USE_TYPE_DEPRECATED
    394 #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \
    395     static typ id##__value(va_list args) {return va_arg(args, typ);} \
    396     static typ id##__convert(void *x)\
    397     {\
    398         union\
    399         {\
    400             void *x;\
    401             typ   d;\
    402         } u;\
    403         u.x = x;\
    404         return u.d;\
    405     }
    406 
    407 #define CAST(id, arg) id##__value(arg)
    408 #define RECAST(id, x) id##__convert(x)
    409 
    410 
    411 /* CODEC_INTERFACE convenience macro
    412  *
    413  * By convention, each codec interface is a struct with extern linkage, where
    414  * the symbol is suffixed with _algo. A getter function is also defined to
    415  * return a pointer to the struct, since in some cases it's easier to work
    416  * with text symbols than data symbols (see issue #169). This function has
    417  * the same name as the struct, less the _algo suffix. The CODEC_INTERFACE
    418  * macro is provided to define this getter function automatically.
    419  */
    420 #define CODEC_INTERFACE(id)\
    421 vpx_codec_iface_t* id(void) { return &id##_algo; }\
    422 vpx_codec_iface_t  id##_algo
    423 
    424 
    425 /* Internal Utility Functions
    426  *
    427  * The following functions are intended to be used inside algorithms as
    428  * utilities for manipulating vpx_codec_* data structures.
    429  */
    430 struct vpx_codec_pkt_list
    431 {
    432     unsigned int            cnt;
    433     unsigned int            max;
    434     struct vpx_codec_cx_pkt pkts[1];
    435 };
    436 
    437 #define vpx_codec_pkt_list_decl(n)\
    438     union {struct vpx_codec_pkt_list head;\
    439         struct {struct vpx_codec_pkt_list head;\
    440             struct vpx_codec_cx_pkt    pkts[n];} alloc;}
    441 
    442 #define vpx_codec_pkt_list_init(m)\
    443     (m)->alloc.head.cnt = 0,\
    444                           (m)->alloc.head.max = sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0])
    445 
    446 int
    447 vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *,
    448                        const struct vpx_codec_cx_pkt *);
    449 
    450 const vpx_codec_cx_pkt_t*
    451 vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
    452                        vpx_codec_iter_t           *iter);
    453 
    454 
    455 #include <stdio.h>
    456 #include <setjmp.h>
    457 struct vpx_internal_error_info
    458 {
    459     vpx_codec_err_t  error_code;
    460     int              has_detail;
    461     char             detail[80];
    462     int              setjmp;
    463     jmp_buf          jmp;
    464 };
    465 
    466 static void vpx_internal_error(struct vpx_internal_error_info *info,
    467                                vpx_codec_err_t                 error,
    468                                const char                     *fmt,
    469                                ...)
    470 {
    471     va_list ap;
    472 
    473     info->error_code = error;
    474     info->has_detail = 0;
    475 
    476     if (fmt)
    477     {
    478         size_t  sz = sizeof(info->detail);
    479 
    480         info->has_detail = 1;
    481         va_start(ap, fmt);
    482         vsnprintf(info->detail, sz - 1, fmt, ap);
    483         va_end(ap);
    484         info->detail[sz-1] = '\0';
    485     }
    486 
    487     if (info->setjmp)
    488         longjmp(info->jmp, info->error_code);
    489 }
    490 #endif
    491