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 /*!\file 12 * \brief Describes the decoder algorithm interface for algorithm 13 * implementations. 14 * 15 * This file defines the private structures and data types that are only 16 * relevant to implementing an algorithm, as opposed to using it. 17 * 18 * To create a decoder algorithm class, an interface structure is put 19 * into the global namespace: 20 * <pre> 21 * my_codec.c: 22 * vpx_codec_iface_t my_codec = { 23 * "My Codec v1.0", 24 * VPX_CODEC_ALG_ABI_VERSION, 25 * ... 26 * }; 27 * </pre> 28 * 29 * An application instantiates a specific decoder instance by using 30 * vpx_codec_dec_init() and a pointer to the algorithm's interface structure: 31 * <pre> 32 * my_app.c: 33 * extern vpx_codec_iface_t my_codec; 34 * { 35 * vpx_codec_ctx_t algo; 36 * int threads = 4; 37 * vpx_codec_dec_cfg_t cfg = { threads, 0, 0 }; 38 * res = vpx_codec_dec_init(&algo, &my_codec, &cfg, 0); 39 * } 40 * </pre> 41 * 42 * Once initialized, the instance is manged using other functions from 43 * the vpx_codec_* family. 44 */ 45 #ifndef VPX_VPX_INTERNAL_VPX_CODEC_INTERNAL_H_ 46 #define VPX_VPX_INTERNAL_VPX_CODEC_INTERNAL_H_ 47 #include "../vpx_decoder.h" 48 #include "../vpx_encoder.h" 49 #include <stdarg.h> 50 51 #ifdef __cplusplus 52 extern "C" { 53 #endif 54 55 /*!\brief Current ABI version number 56 * 57 * \internal 58 * If this file is altered in any way that changes the ABI, this value 59 * must be bumped. Examples include, but are not limited to, changing 60 * types, removing or reassigning enums, adding/removing/rearranging 61 * fields to structures 62 */ 63 #define VPX_CODEC_INTERNAL_ABI_VERSION (5) /**<\hideinitializer*/ 64 65 typedef struct vpx_codec_alg_priv vpx_codec_alg_priv_t; 66 typedef struct vpx_codec_priv_enc_mr_cfg vpx_codec_priv_enc_mr_cfg_t; 67 68 /*!\brief init function pointer prototype 69 * 70 * Performs algorithm-specific initialization of the decoder context. This 71 * function is called by vpx_codec_dec_init() and vpx_codec_enc_init(), so 72 * plugins implementing this interface may trust the input parameters to be 73 * properly initialized. 74 * 75 * \param[in] ctx Pointer to this instance's context 76 * \retval #VPX_CODEC_OK 77 * The input stream was recognized and decoder initialized. 78 * \retval #VPX_CODEC_MEM_ERROR 79 * Memory operation failed. 80 */ 81 typedef vpx_codec_err_t (*vpx_codec_init_fn_t)( 82 vpx_codec_ctx_t *ctx, vpx_codec_priv_enc_mr_cfg_t *data); 83 84 /*!\brief destroy function pointer prototype 85 * 86 * Performs algorithm-specific destruction of the decoder context. This 87 * function is called by the generic vpx_codec_destroy() wrapper function, 88 * so plugins implementing this interface may trust the input parameters 89 * to be properly initialized. 90 * 91 * \param[in] ctx Pointer to this instance's context 92 * \retval #VPX_CODEC_OK 93 * The input stream was recognized and decoder initialized. 94 * \retval #VPX_CODEC_MEM_ERROR 95 * Memory operation failed. 96 */ 97 typedef vpx_codec_err_t (*vpx_codec_destroy_fn_t)(vpx_codec_alg_priv_t *ctx); 98 99 /*!\brief parse stream info function pointer prototype 100 * 101 * Performs high level parsing of the bitstream. This function is called by the 102 * generic vpx_codec_peek_stream_info() wrapper function, so plugins 103 * implementing this interface may trust the input parameters to be properly 104 * initialized. 105 * 106 * \param[in] data Pointer to a block of data to parse 107 * \param[in] data_sz Size of the data buffer 108 * \param[in,out] si Pointer to stream info to update. The size member 109 * \ref MUST be properly initialized, but \ref MAY be 110 * clobbered by the algorithm. This parameter \ref MAY 111 * be NULL. 112 * 113 * \retval #VPX_CODEC_OK 114 * Bitstream is parsable and stream information updated 115 */ 116 typedef vpx_codec_err_t (*vpx_codec_peek_si_fn_t)(const uint8_t *data, 117 unsigned int data_sz, 118 vpx_codec_stream_info_t *si); 119 120 /*!\brief Return information about the current stream. 121 * 122 * Returns information about the stream that has been parsed during decoding. 123 * 124 * \param[in] ctx Pointer to this instance's context 125 * \param[in,out] si Pointer to stream info to update. The size member 126 * \ref MUST be properly initialized, but \ref MAY be 127 * clobbered by the algorithm. This parameter \ref MAY 128 * be NULL. 129 * 130 * \retval #VPX_CODEC_OK 131 * Bitstream is parsable and stream information updated 132 */ 133 typedef vpx_codec_err_t (*vpx_codec_get_si_fn_t)(vpx_codec_alg_priv_t *ctx, 134 vpx_codec_stream_info_t *si); 135 136 /*!\brief control function pointer prototype 137 * 138 * This function is used to exchange algorithm specific data with the decoder 139 * instance. This can be used to implement features specific to a particular 140 * algorithm. 141 * 142 * This function is called by the generic vpx_codec_control() wrapper 143 * function, so plugins implementing this interface may trust the input 144 * parameters to be properly initialized. However, this interface does not 145 * provide type safety for the exchanged data or assign meanings to the 146 * control codes. Those details should be specified in the algorithm's 147 * header file. In particular, the ctrl_id parameter is guaranteed to exist 148 * in the algorithm's control mapping table, and the data parameter may be NULL. 149 * 150 * 151 * \param[in] ctx Pointer to this instance's context 152 * \param[in] ctrl_id Algorithm specific control identifier 153 * \param[in,out] data Data to exchange with algorithm instance. 154 * 155 * \retval #VPX_CODEC_OK 156 * The internal state data was deserialized. 157 */ 158 typedef vpx_codec_err_t (*vpx_codec_control_fn_t)(vpx_codec_alg_priv_t *ctx, 159 va_list ap); 160 161 /*!\brief control function pointer mapping 162 * 163 * This structure stores the mapping between control identifiers and 164 * implementing functions. Each algorithm provides a list of these 165 * mappings. This list is searched by the vpx_codec_control() wrapper 166 * function to determine which function to invoke. The special 167 * value {0, NULL} is used to indicate end-of-list, and must be 168 * present. The special value {0, <non-null>} can be used as a catch-all 169 * mapping. This implies that ctrl_id values chosen by the algorithm 170 * \ref MUST be non-zero. 171 */ 172 typedef const struct vpx_codec_ctrl_fn_map { 173 int ctrl_id; 174 vpx_codec_control_fn_t fn; 175 } vpx_codec_ctrl_fn_map_t; 176 177 /*!\brief decode data function pointer prototype 178 * 179 * Processes a buffer of coded data. If the processing results in a new 180 * decoded frame becoming available, put_slice and put_frame callbacks 181 * are invoked as appropriate. This function is called by the generic 182 * vpx_codec_decode() wrapper function, so plugins implementing this 183 * interface may trust the input parameters to be properly initialized. 184 * 185 * \param[in] ctx Pointer to this instance's context 186 * \param[in] data Pointer to this block of new coded data. If 187 * NULL, the put_frame callback is invoked for 188 * the previously decoded frame. 189 * \param[in] data_sz Size of the coded data, in bytes. 190 * 191 * \return Returns #VPX_CODEC_OK if the coded data was processed completely 192 * and future pictures can be decoded without error. Otherwise, 193 * see the descriptions of the other error codes in ::vpx_codec_err_t 194 * for recoverability capabilities. 195 */ 196 typedef vpx_codec_err_t (*vpx_codec_decode_fn_t)(vpx_codec_alg_priv_t *ctx, 197 const uint8_t *data, 198 unsigned int data_sz, 199 void *user_priv, 200 long deadline); 201 202 /*!\brief Decoded frames iterator 203 * 204 * Iterates over a list of the frames available for display. The iterator 205 * storage should be initialized to NULL to start the iteration. Iteration is 206 * complete when this function returns NULL. 207 * 208 * The list of available frames becomes valid upon completion of the 209 * vpx_codec_decode call, and remains valid until the next call to 210 * vpx_codec_decode. 211 * 212 * \param[in] ctx Pointer to this instance's context 213 * \param[in out] iter Iterator storage, initialized to NULL 214 * 215 * \return Returns a pointer to an image, if one is ready for display. Frames 216 * produced will always be in PTS (presentation time stamp) order. 217 */ 218 typedef vpx_image_t *(*vpx_codec_get_frame_fn_t)(vpx_codec_alg_priv_t *ctx, 219 vpx_codec_iter_t *iter); 220 221 /*!\brief Pass in external frame buffers for the decoder to use. 222 * 223 * Registers functions to be called when libvpx needs a frame buffer 224 * to decode the current frame and a function to be called when libvpx does 225 * not internally reference the frame buffer. This set function must 226 * be called before the first call to decode or libvpx will assume the 227 * default behavior of allocating frame buffers internally. 228 * 229 * \param[in] ctx Pointer to this instance's context 230 * \param[in] cb_get Pointer to the get callback function 231 * \param[in] cb_release Pointer to the release callback function 232 * \param[in] cb_priv Callback's private data 233 * 234 * \retval #VPX_CODEC_OK 235 * External frame buffers will be used by libvpx. 236 * \retval #VPX_CODEC_INVALID_PARAM 237 * One or more of the callbacks were NULL. 238 * \retval #VPX_CODEC_ERROR 239 * Decoder context not initialized, or algorithm not capable of 240 * using external frame buffers. 241 * 242 * \note 243 * When decoding VP9, the application may be required to pass in at least 244 * #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS external frame 245 * buffers. 246 */ 247 typedef vpx_codec_err_t (*vpx_codec_set_fb_fn_t)( 248 vpx_codec_alg_priv_t *ctx, vpx_get_frame_buffer_cb_fn_t cb_get, 249 vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv); 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)( 258 vpx_codec_alg_priv_t *ctx, vpx_codec_iter_t *iter); 259 260 typedef vpx_codec_err_t (*vpx_codec_enc_config_set_fn_t)( 261 vpx_codec_alg_priv_t *ctx, const vpx_codec_enc_cfg_t *cfg); 262 typedef vpx_fixed_buf_t *(*vpx_codec_get_global_headers_fn_t)( 263 vpx_codec_alg_priv_t *ctx); 264 265 typedef vpx_image_t *(*vpx_codec_get_preview_frame_fn_t)( 266 vpx_codec_alg_priv_t *ctx); 267 268 typedef vpx_codec_err_t (*vpx_codec_enc_mr_get_mem_loc_fn_t)( 269 const vpx_codec_enc_cfg_t *cfg, void **mem_loc); 270 271 /*!\brief usage configuration mapping 272 * 273 * This structure stores the mapping between usage identifiers and 274 * configuration structures. Each algorithm provides a list of these 275 * mappings. This list is searched by the vpx_codec_enc_config_default() 276 * wrapper function to determine which config to return. The special value 277 * {-1, {0}} is used to indicate end-of-list, and must be present. At least 278 * one mapping must be present, in addition to the end-of-list. 279 * 280 */ 281 typedef const struct vpx_codec_enc_cfg_map { 282 int usage; 283 vpx_codec_enc_cfg_t cfg; 284 } vpx_codec_enc_cfg_map_t; 285 286 /*!\brief Decoder algorithm interface 287 * 288 * All decoders \ref MUST expose a variable of this type. 289 */ 290 struct vpx_codec_iface { 291 const char *name; /**< Identification String */ 292 int abi_version; /**< Implemented ABI version */ 293 vpx_codec_caps_t caps; /**< Decoder capabilities */ 294 vpx_codec_init_fn_t init; /**< \copydoc ::vpx_codec_init_fn_t */ 295 vpx_codec_destroy_fn_t destroy; /**< \copydoc ::vpx_codec_destroy_fn_t */ 296 vpx_codec_ctrl_fn_map_t *ctrl_maps; /**< \copydoc ::vpx_codec_ctrl_fn_map_t */ 297 struct vpx_codec_dec_iface { 298 vpx_codec_peek_si_fn_t peek_si; /**< \copydoc ::vpx_codec_peek_si_fn_t */ 299 vpx_codec_get_si_fn_t get_si; /**< \copydoc ::vpx_codec_get_si_fn_t */ 300 vpx_codec_decode_fn_t decode; /**< \copydoc ::vpx_codec_decode_fn_t */ 301 vpx_codec_get_frame_fn_t 302 get_frame; /**< \copydoc ::vpx_codec_get_frame_fn_t */ 303 vpx_codec_set_fb_fn_t set_fb_fn; /**< \copydoc ::vpx_codec_set_fb_fn_t */ 304 } dec; 305 struct vpx_codec_enc_iface { 306 int cfg_map_count; 307 vpx_codec_enc_cfg_map_t 308 *cfg_maps; /**< \copydoc ::vpx_codec_enc_cfg_map_t */ 309 vpx_codec_encode_fn_t encode; /**< \copydoc ::vpx_codec_encode_fn_t */ 310 vpx_codec_get_cx_data_fn_t 311 get_cx_data; /**< \copydoc ::vpx_codec_get_cx_data_fn_t */ 312 vpx_codec_enc_config_set_fn_t 313 cfg_set; /**< \copydoc ::vpx_codec_enc_config_set_fn_t */ 314 vpx_codec_get_global_headers_fn_t 315 get_glob_hdrs; /**< \copydoc ::vpx_codec_get_global_headers_fn_t */ 316 vpx_codec_get_preview_frame_fn_t 317 get_preview; /**< \copydoc ::vpx_codec_get_preview_frame_fn_t */ 318 vpx_codec_enc_mr_get_mem_loc_fn_t 319 mr_get_mem_loc; /**< \copydoc ::vpx_codec_enc_mr_get_mem_loc_fn_t */ 320 } enc; 321 }; 322 323 /*!\brief Callback function pointer / user data pair storage */ 324 typedef struct vpx_codec_priv_cb_pair { 325 union { 326 vpx_codec_put_frame_cb_fn_t put_frame; 327 vpx_codec_put_slice_cb_fn_t put_slice; 328 } u; 329 void *user_priv; 330 } vpx_codec_priv_cb_pair_t; 331 332 /*!\brief Instance private storage 333 * 334 * This structure is allocated by the algorithm's init function. It can be 335 * extended in one of two ways. First, a second, algorithm specific structure 336 * can be allocated and the priv member pointed to it. Alternatively, this 337 * structure can be made the first member of the algorithm specific structure, 338 * and the pointer cast to the proper type. 339 */ 340 struct vpx_codec_priv { 341 const char *err_detail; 342 vpx_codec_flags_t init_flags; 343 struct { 344 vpx_codec_priv_cb_pair_t put_frame_cb; 345 vpx_codec_priv_cb_pair_t put_slice_cb; 346 } dec; 347 struct { 348 vpx_fixed_buf_t cx_data_dst_buf; 349 unsigned int cx_data_pad_before; 350 unsigned int cx_data_pad_after; 351 vpx_codec_cx_pkt_t cx_data_pkt; 352 unsigned int total_encoders; 353 } enc; 354 }; 355 356 /* 357 * Multi-resolution encoding internal configuration 358 */ 359 struct vpx_codec_priv_enc_mr_cfg { 360 unsigned int mr_total_resolutions; 361 unsigned int mr_encoder_id; 362 struct vpx_rational mr_down_sampling_factor; 363 void *mr_low_res_mode_info; 364 }; 365 366 #undef VPX_CTRL_USE_TYPE 367 #define VPX_CTRL_USE_TYPE(id, typ) \ 368 static VPX_INLINE typ id##__value(va_list args) { return va_arg(args, typ); } 369 370 #undef VPX_CTRL_USE_TYPE_DEPRECATED 371 #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \ 372 static VPX_INLINE typ id##__value(va_list args) { return va_arg(args, typ); } 373 374 #define CAST(id, arg) id##__value(arg) 375 376 /* CODEC_INTERFACE convenience macro 377 * 378 * By convention, each codec interface is a struct with extern linkage, where 379 * the symbol is suffixed with _algo. A getter function is also defined to 380 * return a pointer to the struct, since in some cases it's easier to work 381 * with text symbols than data symbols (see issue #169). This function has 382 * the same name as the struct, less the _algo suffix. The CODEC_INTERFACE 383 * macro is provided to define this getter function automatically. 384 */ 385 #define CODEC_INTERFACE(id) \ 386 vpx_codec_iface_t *id(void) { return &id##_algo; } \ 387 vpx_codec_iface_t id##_algo 388 389 /* Internal Utility Functions 390 * 391 * The following functions are intended to be used inside algorithms as 392 * utilities for manipulating vpx_codec_* data structures. 393 */ 394 struct vpx_codec_pkt_list { 395 unsigned int cnt; 396 unsigned int max; 397 struct vpx_codec_cx_pkt pkts[1]; 398 }; 399 400 #define vpx_codec_pkt_list_decl(n) \ 401 union { \ 402 struct vpx_codec_pkt_list head; \ 403 struct { \ 404 struct vpx_codec_pkt_list head; \ 405 struct vpx_codec_cx_pkt pkts[n]; \ 406 } alloc; \ 407 } 408 409 #define vpx_codec_pkt_list_init(m) \ 410 (m)->alloc.head.cnt = 0, \ 411 (m)->alloc.head.max = sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0]) 412 413 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *, 414 const struct vpx_codec_cx_pkt *); 415 416 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get( 417 struct vpx_codec_pkt_list *list, vpx_codec_iter_t *iter); 418 419 #include <stdio.h> 420 #include <setjmp.h> 421 422 struct vpx_internal_error_info { 423 vpx_codec_err_t error_code; 424 int has_detail; 425 char detail[80]; 426 int setjmp; 427 jmp_buf jmp; 428 }; 429 430 #define CLANG_ANALYZER_NORETURN 431 #if defined(__has_feature) 432 #if __has_feature(attribute_analyzer_noreturn) 433 #undef CLANG_ANALYZER_NORETURN 434 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn)) 435 #endif 436 #endif 437 438 // Tells the compiler to perform `printf` format string checking if the 439 // compiler supports it; see the 'format' attribute in 440 // <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html>. 441 #define LIBVPX_FORMAT_PRINTF(string_index, first_to_check) 442 #if defined(__has_attribute) 443 #if __has_attribute(format) 444 #undef LIBVPX_FORMAT_PRINTF 445 #define LIBVPX_FORMAT_PRINTF(string_index, first_to_check) \ 446 __attribute__((__format__(__printf__, string_index, first_to_check))) 447 #endif 448 #endif 449 450 void vpx_internal_error(struct vpx_internal_error_info *info, 451 vpx_codec_err_t error, const char *fmt, ...) 452 LIBVPX_FORMAT_PRINTF(3, 4) CLANG_ANALYZER_NORETURN; 453 454 #ifdef __cplusplus 455 } // extern "C" 456 #endif 457 458 #endif // VPX_VPX_INTERNAL_VPX_CODEC_INTERNAL_H_ 459