vpx_codec_internal.h 18 KB

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