vpx_codec_internal.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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_VPX_INTERNAL_VPX_CODEC_INTERNAL_H_
  43. #define VPX_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)(
  75. vpx_codec_ctx_t *ctx, 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
  197. * vpx_codec_decode.
  198. *
  199. * \param[in] ctx Pointer to this instance's context
  200. * \param[in out] iter Iterator storage, initialized to NULL
  201. *
  202. * \return Returns a pointer to an image, if one is ready for display. Frames
  203. * produced will always be in PTS (presentation time stamp) order.
  204. */
  205. typedef vpx_image_t *(*vpx_codec_get_frame_fn_t)(vpx_codec_alg_priv_t *ctx,
  206. vpx_codec_iter_t *iter);
  207. /*!\brief Pass in external frame buffers for the decoder to use.
  208. *
  209. * Registers functions to be called when libvpx needs a frame buffer
  210. * to decode the current frame and a function to be called when libvpx does
  211. * not internally reference the frame buffer. This set function must
  212. * be called before the first call to decode or libvpx will assume the
  213. * default behavior of allocating frame buffers internally.
  214. *
  215. * \param[in] ctx Pointer to this instance's context
  216. * \param[in] cb_get Pointer to the get callback function
  217. * \param[in] cb_release Pointer to the release callback function
  218. * \param[in] cb_priv Callback's private data
  219. *
  220. * \retval #VPX_CODEC_OK
  221. * External frame buffers will be used by libvpx.
  222. * \retval #VPX_CODEC_INVALID_PARAM
  223. * One or more of the callbacks were NULL.
  224. * \retval #VPX_CODEC_ERROR
  225. * Decoder context not initialized, or algorithm not capable of
  226. * using external frame buffers.
  227. *
  228. * \note
  229. * When decoding VP9, the application may be required to pass in at least
  230. * #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS external frame
  231. * buffers.
  232. */
  233. typedef vpx_codec_err_t (*vpx_codec_set_fb_fn_t)(
  234. vpx_codec_alg_priv_t *ctx, 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)(
  243. vpx_codec_alg_priv_t *ctx, vpx_codec_iter_t *iter);
  244. typedef vpx_codec_err_t (*vpx_codec_enc_config_set_fn_t)(
  245. vpx_codec_alg_priv_t *ctx, const vpx_codec_enc_cfg_t *cfg);
  246. typedef vpx_fixed_buf_t *(*vpx_codec_get_global_headers_fn_t)(
  247. vpx_codec_alg_priv_t *ctx);
  248. typedef vpx_image_t *(*vpx_codec_get_preview_frame_fn_t)(
  249. vpx_codec_alg_priv_t *ctx);
  250. typedef vpx_codec_err_t (*vpx_codec_enc_mr_get_mem_loc_fn_t)(
  251. const vpx_codec_enc_cfg_t *cfg, void **mem_loc);
  252. /*!\brief usage configuration mapping
  253. *
  254. * This structure stores the mapping between usage identifiers and
  255. * configuration structures. Each algorithm provides a list of these
  256. * mappings. This list is searched by the vpx_codec_enc_config_default()
  257. * wrapper function to determine which config to return. The special value
  258. * {-1, {0}} is used to indicate end-of-list, and must be present. At least
  259. * one mapping must be present, in addition to the end-of-list.
  260. *
  261. */
  262. typedef const struct vpx_codec_enc_cfg_map {
  263. int usage;
  264. vpx_codec_enc_cfg_t cfg;
  265. } vpx_codec_enc_cfg_map_t;
  266. /*!\brief Decoder algorithm interface interface
  267. *
  268. * All decoders \ref MUST expose a variable of this type.
  269. */
  270. struct vpx_codec_iface {
  271. const char *name; /**< Identification String */
  272. int abi_version; /**< Implemented ABI version */
  273. vpx_codec_caps_t caps; /**< Decoder capabilities */
  274. vpx_codec_init_fn_t init; /**< \copydoc ::vpx_codec_init_fn_t */
  275. vpx_codec_destroy_fn_t destroy; /**< \copydoc ::vpx_codec_destroy_fn_t */
  276. vpx_codec_ctrl_fn_map_t *ctrl_maps; /**< \copydoc ::vpx_codec_ctrl_fn_map_t */
  277. struct vpx_codec_dec_iface {
  278. vpx_codec_peek_si_fn_t peek_si; /**< \copydoc ::vpx_codec_peek_si_fn_t */
  279. vpx_codec_get_si_fn_t get_si; /**< \copydoc ::vpx_codec_get_si_fn_t */
  280. vpx_codec_decode_fn_t decode; /**< \copydoc ::vpx_codec_decode_fn_t */
  281. vpx_codec_get_frame_fn_t
  282. get_frame; /**< \copydoc ::vpx_codec_get_frame_fn_t */
  283. vpx_codec_set_fb_fn_t set_fb_fn; /**< \copydoc ::vpx_codec_set_fb_fn_t */
  284. } dec;
  285. struct vpx_codec_enc_iface {
  286. int cfg_map_count;
  287. vpx_codec_enc_cfg_map_t
  288. *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
  291. get_cx_data; /**< \copydoc ::vpx_codec_get_cx_data_fn_t */
  292. vpx_codec_enc_config_set_fn_t
  293. cfg_set; /**< \copydoc ::vpx_codec_enc_config_set_fn_t */
  294. vpx_codec_get_global_headers_fn_t
  295. get_glob_hdrs; /**< \copydoc ::vpx_codec_get_global_headers_fn_t */
  296. vpx_codec_get_preview_frame_fn_t
  297. get_preview; /**< \copydoc ::vpx_codec_get_preview_frame_fn_t */
  298. vpx_codec_enc_mr_get_mem_loc_fn_t
  299. mr_get_mem_loc; /**< \copydoc ::vpx_codec_enc_mr_get_mem_loc_fn_t */
  300. } enc;
  301. };
  302. /*!\brief Callback function pointer / user data pair storage */
  303. typedef struct vpx_codec_priv_cb_pair {
  304. union {
  305. vpx_codec_put_frame_cb_fn_t put_frame;
  306. vpx_codec_put_slice_cb_fn_t put_slice;
  307. } u;
  308. void *user_priv;
  309. } vpx_codec_priv_cb_pair_t;
  310. /*!\brief Instance private storage
  311. *
  312. * This structure is allocated by the algorithm's init function. It can be
  313. * extended in one of two ways. First, a second, algorithm specific structure
  314. * can be allocated and the priv member pointed to it. Alternatively, this
  315. * structure can be made the first member of the algorithm specific structure,
  316. * and the pointer cast to the proper type.
  317. */
  318. struct vpx_codec_priv {
  319. const char *err_detail;
  320. vpx_codec_flags_t init_flags;
  321. struct {
  322. vpx_codec_priv_cb_pair_t put_frame_cb;
  323. vpx_codec_priv_cb_pair_t put_slice_cb;
  324. } dec;
  325. struct {
  326. vpx_fixed_buf_t cx_data_dst_buf;
  327. unsigned int cx_data_pad_before;
  328. unsigned int cx_data_pad_after;
  329. vpx_codec_cx_pkt_t cx_data_pkt;
  330. unsigned int total_encoders;
  331. } enc;
  332. };
  333. /*
  334. * Multi-resolution encoding internal configuration
  335. */
  336. struct vpx_codec_priv_enc_mr_cfg {
  337. unsigned int mr_total_resolutions;
  338. unsigned int mr_encoder_id;
  339. struct vpx_rational mr_down_sampling_factor;
  340. void *mr_low_res_mode_info;
  341. };
  342. #undef VPX_CTRL_USE_TYPE
  343. #define VPX_CTRL_USE_TYPE(id, typ) \
  344. static VPX_INLINE typ id##__value(va_list args) { return va_arg(args, typ); }
  345. #undef VPX_CTRL_USE_TYPE_DEPRECATED
  346. #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \
  347. static VPX_INLINE typ id##__value(va_list args) { return va_arg(args, typ); }
  348. #define CAST(id, arg) id##__value(arg)
  349. /* CODEC_INTERFACE convenience macro
  350. *
  351. * By convention, each codec interface is a struct with extern linkage, where
  352. * the symbol is suffixed with _algo. A getter function is also defined to
  353. * return a pointer to the struct, since in some cases it's easier to work
  354. * with text symbols than data symbols (see issue #169). This function has
  355. * the same name as the struct, less the _algo suffix. The CODEC_INTERFACE
  356. * macro is provided to define this getter function automatically.
  357. */
  358. #define CODEC_INTERFACE(id) \
  359. vpx_codec_iface_t *id(void) { return &id##_algo; } \
  360. vpx_codec_iface_t id##_algo
  361. /* Internal Utility Functions
  362. *
  363. * The following functions are intended to be used inside algorithms as
  364. * utilities for manipulating vpx_codec_* data structures.
  365. */
  366. struct vpx_codec_pkt_list {
  367. unsigned int cnt;
  368. unsigned int max;
  369. struct vpx_codec_cx_pkt pkts[1];
  370. };
  371. #define vpx_codec_pkt_list_decl(n) \
  372. union { \
  373. struct vpx_codec_pkt_list head; \
  374. struct { \
  375. struct vpx_codec_pkt_list head; \
  376. struct vpx_codec_cx_pkt pkts[n]; \
  377. } alloc; \
  378. }
  379. #define vpx_codec_pkt_list_init(m) \
  380. (m)->alloc.head.cnt = 0, \
  381. (m)->alloc.head.max = sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0])
  382. int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *,
  383. const struct vpx_codec_cx_pkt *);
  384. const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(
  385. struct vpx_codec_pkt_list *list, vpx_codec_iter_t *iter);
  386. #include <stdio.h>
  387. #include <setjmp.h>
  388. struct vpx_internal_error_info {
  389. vpx_codec_err_t error_code;
  390. int has_detail;
  391. char detail[80];
  392. int setjmp;
  393. jmp_buf jmp;
  394. };
  395. #define CLANG_ANALYZER_NORETURN
  396. #if defined(__has_feature)
  397. #if __has_feature(attribute_analyzer_noreturn)
  398. #undef CLANG_ANALYZER_NORETURN
  399. #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
  400. #endif
  401. #endif
  402. void vpx_internal_error(struct vpx_internal_error_info *info,
  403. vpx_codec_err_t error, const char *fmt,
  404. ...) CLANG_ANALYZER_NORETURN;
  405. #ifdef __cplusplus
  406. } // extern "C"
  407. #endif
  408. #endif // VPX_VPX_INTERNAL_VPX_CODEC_INTERNAL_H_