vpx_codec.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. /*!\defgroup codec Common Algorithm Interface
  11. * This abstraction allows applications to easily support multiple video
  12. * formats with minimal code duplication. This section describes the interface
  13. * common to all codecs (both encoders and decoders).
  14. * @{
  15. */
  16. /*!\file
  17. * \brief Describes the codec algorithm interface to applications.
  18. *
  19. * This file describes the interface between an application and a
  20. * video codec algorithm.
  21. *
  22. * An application instantiates a specific codec instance by using
  23. * vpx_codec_init() and a pointer to the algorithm's interface structure:
  24. * <pre>
  25. * my_app.c:
  26. * extern vpx_codec_iface_t my_codec;
  27. * {
  28. * vpx_codec_ctx_t algo;
  29. * res = vpx_codec_init(&algo, &my_codec);
  30. * }
  31. * </pre>
  32. *
  33. * Once initialized, the instance is manged using other functions from
  34. * the vpx_codec_* family.
  35. */
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #ifndef VPX_CODEC_H
  40. #define VPX_CODEC_H
  41. #include "vpx_integer.h"
  42. #include "vpx_image.h"
  43. /*!\brief Decorator indicating a function is deprecated */
  44. #ifndef DEPRECATED
  45. #if defined(__GNUC__) && __GNUC__
  46. #define DEPRECATED __attribute__ ((deprecated))
  47. #define DECLSPEC_DEPRECATED /**< \copydoc #DEPRECATED */
  48. #elif defined(_MSC_VER)
  49. #define DEPRECATED
  50. #define DECLSPEC_DEPRECATED __declspec(deprecated) /**< \copydoc #DEPRECATED */
  51. #else
  52. #define DEPRECATED
  53. #define DECLSPEC_DEPRECATED /**< \copydoc #DEPRECATED */
  54. #endif
  55. #endif
  56. /*!\brief Decorator indicating a function is potentially unused */
  57. #ifdef UNUSED
  58. #elif __GNUC__
  59. #define UNUSED __attribute__ ((unused))
  60. #else
  61. #define UNUSED
  62. #endif
  63. /*!\brief Current ABI version number
  64. *
  65. * \internal
  66. * If this file is altered in any way that changes the ABI, this value
  67. * must be bumped. Examples include, but are not limited to, changing
  68. * types, removing or reassigning enums, adding/removing/rearranging
  69. * fields to structures
  70. */
  71. #define VPX_CODEC_ABI_VERSION (2 + VPX_IMAGE_ABI_VERSION) /**<\hideinitializer*/
  72. /*!\brief Algorithm return codes */
  73. typedef enum {
  74. /*!\brief Operation completed without error */
  75. VPX_CODEC_OK,
  76. /*!\brief Unspecified error */
  77. VPX_CODEC_ERROR,
  78. /*!\brief Memory operation failed */
  79. VPX_CODEC_MEM_ERROR,
  80. /*!\brief ABI version mismatch */
  81. VPX_CODEC_ABI_MISMATCH,
  82. /*!\brief Algorithm does not have required capability */
  83. VPX_CODEC_INCAPABLE,
  84. /*!\brief The given bitstream is not supported.
  85. *
  86. * The bitstream was unable to be parsed at the highest level. The decoder
  87. * is unable to proceed. This error \ref SHOULD be treated as fatal to the
  88. * stream. */
  89. VPX_CODEC_UNSUP_BITSTREAM,
  90. /*!\brief Encoded bitstream uses an unsupported feature
  91. *
  92. * The decoder does not implement a feature required by the encoder. This
  93. * return code should only be used for features that prevent future
  94. * pictures from being properly decoded. This error \ref MAY be treated as
  95. * fatal to the stream or \ref MAY be treated as fatal to the current GOP.
  96. */
  97. VPX_CODEC_UNSUP_FEATURE,
  98. /*!\brief The coded data for this stream is corrupt or incomplete
  99. *
  100. * There was a problem decoding the current frame. This return code
  101. * should only be used for failures that prevent future pictures from
  102. * being properly decoded. This error \ref MAY be treated as fatal to the
  103. * stream or \ref MAY be treated as fatal to the current GOP. If decoding
  104. * is continued for the current GOP, artifacts may be present.
  105. */
  106. VPX_CODEC_CORRUPT_FRAME,
  107. /*!\brief An application-supplied parameter is not valid.
  108. *
  109. */
  110. VPX_CODEC_INVALID_PARAM,
  111. /*!\brief An iterator reached the end of list.
  112. *
  113. */
  114. VPX_CODEC_LIST_END
  115. }
  116. vpx_codec_err_t;
  117. /*! \brief Codec capabilities bitfield
  118. *
  119. * Each codec advertises the capabilities it supports as part of its
  120. * ::vpx_codec_iface_t interface structure. Capabilities are extra interfaces
  121. * or functionality, and are not required to be supported.
  122. *
  123. * The available flags are specified by VPX_CODEC_CAP_* defines.
  124. */
  125. typedef long vpx_codec_caps_t;
  126. #define VPX_CODEC_CAP_DECODER 0x1 /**< Is a decoder */
  127. #define VPX_CODEC_CAP_ENCODER 0x2 /**< Is an encoder */
  128. #define VPX_CODEC_CAP_XMA 0x4 /**< Supports eXternal Memory Allocation */
  129. /*! \brief Initialization-time Feature Enabling
  130. *
  131. * Certain codec features must be known at initialization time, to allow for
  132. * proper memory allocation.
  133. *
  134. * The available flags are specified by VPX_CODEC_USE_* defines.
  135. */
  136. typedef long vpx_codec_flags_t;
  137. #define VPX_CODEC_USE_XMA 0x00000001 /**< Use eXternal Memory Allocation mode */
  138. /*!\brief Codec interface structure.
  139. *
  140. * Contains function pointers and other data private to the codec
  141. * implementation. This structure is opaque to the application.
  142. */
  143. typedef const struct vpx_codec_iface vpx_codec_iface_t;
  144. /*!\brief Codec private data structure.
  145. *
  146. * Contains data private to the codec implementation. This structure is opaque
  147. * to the application.
  148. */
  149. typedef struct vpx_codec_priv vpx_codec_priv_t;
  150. /*!\brief Iterator
  151. *
  152. * Opaque storage used for iterating over lists.
  153. */
  154. typedef const void *vpx_codec_iter_t;
  155. /*!\brief Codec context structure
  156. *
  157. * All codecs \ref MUST support this context structure fully. In general,
  158. * this data should be considered private to the codec algorithm, and
  159. * not be manipulated or examined by the calling application. Applications
  160. * may reference the 'name' member to get a printable description of the
  161. * algorithm.
  162. */
  163. typedef struct vpx_codec_ctx {
  164. const char *name; /**< Printable interface name */
  165. vpx_codec_iface_t *iface; /**< Interface pointers */
  166. vpx_codec_err_t err; /**< Last returned error */
  167. const char *err_detail; /**< Detailed info, if available */
  168. vpx_codec_flags_t init_flags; /**< Flags passed at init time */
  169. union {
  170. struct vpx_codec_dec_cfg *dec; /**< Decoder Configuration Pointer */
  171. struct vpx_codec_enc_cfg *enc; /**< Encoder Configuration Pointer */
  172. void *raw;
  173. } config; /**< Configuration pointer aliasing union */
  174. vpx_codec_priv_t *priv; /**< Algorithm private storage */
  175. } vpx_codec_ctx_t;
  176. /*
  177. * Library Version Number Interface
  178. *
  179. * For example, see the following sample return values:
  180. * vpx_codec_version() (1<<16 | 2<<8 | 3)
  181. * vpx_codec_version_str() "v1.2.3-rc1-16-gec6a1ba"
  182. * vpx_codec_version_extra_str() "rc1-16-gec6a1ba"
  183. */
  184. /*!\brief Return the version information (as an integer)
  185. *
  186. * Returns a packed encoding of the library version number. This will only include
  187. * the major.minor.patch component of the version number. Note that this encoded
  188. * value should be accessed through the macros provided, as the encoding may change
  189. * in the future.
  190. *
  191. */
  192. int vpx_codec_version(void);
  193. #define VPX_VERSION_MAJOR(v) ((v>>16)&0xff) /**< extract major from packed version */
  194. #define VPX_VERSION_MINOR(v) ((v>>8)&0xff) /**< extract minor from packed version */
  195. #define VPX_VERSION_PATCH(v) ((v>>0)&0xff) /**< extract patch from packed version */
  196. /*!\brief Return the version major number */
  197. #define vpx_codec_version_major() ((vpx_codec_version()>>16)&0xff)
  198. /*!\brief Return the version minor number */
  199. #define vpx_codec_version_minor() ((vpx_codec_version()>>8)&0xff)
  200. /*!\brief Return the version patch number */
  201. #define vpx_codec_version_patch() ((vpx_codec_version()>>0)&0xff)
  202. /*!\brief Return the version information (as a string)
  203. *
  204. * Returns a printable string containing the full library version number. This may
  205. * contain additional text following the three digit version number, as to indicate
  206. * release candidates, prerelease versions, etc.
  207. *
  208. */
  209. const char *vpx_codec_version_str(void);
  210. /*!\brief Return the version information (as a string)
  211. *
  212. * Returns a printable "extra string". This is the component of the string returned
  213. * by vpx_codec_version_str() following the three digit version number.
  214. *
  215. */
  216. const char *vpx_codec_version_extra_str(void);
  217. /*!\brief Return the build configuration
  218. *
  219. * Returns a printable string containing an encoded version of the build
  220. * configuration. This may be useful to vpx support.
  221. *
  222. */
  223. const char *vpx_codec_build_config(void);
  224. /*!\brief Return the name for a given interface
  225. *
  226. * Returns a human readable string for name of the given codec interface.
  227. *
  228. * \param[in] iface Interface pointer
  229. *
  230. */
  231. const char *vpx_codec_iface_name(vpx_codec_iface_t *iface);
  232. /*!\brief Convert error number to printable string
  233. *
  234. * Returns a human readable string for the last error returned by the
  235. * algorithm. The returned error will be one line and will not contain
  236. * any newline characters.
  237. *
  238. *
  239. * \param[in] err Error number.
  240. *
  241. */
  242. const char *vpx_codec_err_to_string(vpx_codec_err_t err);
  243. /*!\brief Retrieve error synopsis for codec context
  244. *
  245. * Returns a human readable string for the last error returned by the
  246. * algorithm. The returned error will be one line and will not contain
  247. * any newline characters.
  248. *
  249. *
  250. * \param[in] ctx Pointer to this instance's context.
  251. *
  252. */
  253. const char *vpx_codec_error(vpx_codec_ctx_t *ctx);
  254. /*!\brief Retrieve detailed error information for codec context
  255. *
  256. * Returns a human readable string providing detailed information about
  257. * the last error.
  258. *
  259. * \param[in] ctx Pointer to this instance's context.
  260. *
  261. * \retval NULL
  262. * No detailed information is available.
  263. */
  264. const char *vpx_codec_error_detail(vpx_codec_ctx_t *ctx);
  265. /* REQUIRED FUNCTIONS
  266. *
  267. * The following functions are required to be implemented for all codecs.
  268. * They represent the base case functionality expected of all codecs.
  269. */
  270. /*!\brief Destroy a codec instance
  271. *
  272. * Destroys a codec context, freeing any associated memory buffers.
  273. *
  274. * \param[in] ctx Pointer to this instance's context
  275. *
  276. * \retval #VPX_CODEC_OK
  277. * The codec algorithm initialized.
  278. * \retval #VPX_CODEC_MEM_ERROR
  279. * Memory allocation failed.
  280. */
  281. vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx);
  282. /*!\brief Get the capabilities of an algorithm.
  283. *
  284. * Retrieves the capabilities bitfield from the algorithm's interface.
  285. *
  286. * \param[in] iface Pointer to the algorithm interface
  287. *
  288. */
  289. vpx_codec_caps_t vpx_codec_get_caps(vpx_codec_iface_t *iface);
  290. /*!\brief Control algorithm
  291. *
  292. * This function is used to exchange algorithm specific data with the codec
  293. * instance. This can be used to implement features specific to a particular
  294. * algorithm.
  295. *
  296. * This wrapper function dispatches the request to the helper function
  297. * associated with the given ctrl_id. It tries to call this function
  298. * transparently, but will return #VPX_CODEC_ERROR if the request could not
  299. * be dispatched.
  300. *
  301. * Note that this function should not be used directly. Call the
  302. * #vpx_codec_control wrapper macro instead.
  303. *
  304. * \param[in] ctx Pointer to this instance's context
  305. * \param[in] ctrl_id Algorithm specific control identifier
  306. *
  307. * \retval #VPX_CODEC_OK
  308. * The control request was processed.
  309. * \retval #VPX_CODEC_ERROR
  310. * The control request was not processed.
  311. * \retval #VPX_CODEC_INVALID_PARAM
  312. * The data was not valid.
  313. */
  314. vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx,
  315. int ctrl_id,
  316. ...);
  317. #if defined(VPX_DISABLE_CTRL_TYPECHECKS) && VPX_DISABLE_CTRL_TYPECHECKS
  318. # define vpx_codec_control(ctx,id,data) vpx_codec_control_(ctx,id,data)
  319. # define VPX_CTRL_USE_TYPE(id, typ)
  320. # define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ)
  321. # define VPX_CTRL_VOID(id, typ)
  322. #else
  323. /*!\brief vpx_codec_control wrapper macro
  324. *
  325. * This macro allows for type safe conversions across the variadic parameter
  326. * to vpx_codec_control_().
  327. *
  328. * \internal
  329. * It works by dispatching the call to the control function through a wrapper
  330. * function named with the id parameter.
  331. */
  332. # define vpx_codec_control(ctx,id,data) vpx_codec_control_##id(ctx,id,data)\
  333. /**<\hideinitializer*/
  334. /*!\brief vpx_codec_control type definition macro
  335. *
  336. * This macro allows for type safe conversions across the variadic parameter
  337. * to vpx_codec_control_(). It defines the type of the argument for a given
  338. * control identifier.
  339. *
  340. * \internal
  341. * It defines a static function with
  342. * the correctly typed arguments as a wrapper to the type-unsafe internal
  343. * function.
  344. */
  345. # define VPX_CTRL_USE_TYPE(id, typ) \
  346. static vpx_codec_err_t \
  347. vpx_codec_control_##id(vpx_codec_ctx_t*, int, typ) UNUSED;\
  348. \
  349. static vpx_codec_err_t \
  350. vpx_codec_control_##id(vpx_codec_ctx_t *ctx, int ctrl_id, typ data) {\
  351. return vpx_codec_control_(ctx, ctrl_id, data);\
  352. } /**<\hideinitializer*/
  353. /*!\brief vpx_codec_control deprecated type definition macro
  354. *
  355. * Like #VPX_CTRL_USE_TYPE, but indicates that the specified control is
  356. * deprecated and should not be used. Consult the documentation for your
  357. * codec for more information.
  358. *
  359. * \internal
  360. * It defines a static function with the correctly typed arguments as a
  361. * wrapper to the type-unsafe internal function.
  362. */
  363. # define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \
  364. DECLSPEC_DEPRECATED static vpx_codec_err_t \
  365. vpx_codec_control_##id(vpx_codec_ctx_t*, int, typ) DEPRECATED UNUSED;\
  366. \
  367. DECLSPEC_DEPRECATED static vpx_codec_err_t \
  368. vpx_codec_control_##id(vpx_codec_ctx_t *ctx, int ctrl_id, typ data) {\
  369. return vpx_codec_control_(ctx, ctrl_id, data);\
  370. } /**<\hideinitializer*/
  371. /*!\brief vpx_codec_control void type definition macro
  372. *
  373. * This macro allows for type safe conversions across the variadic parameter
  374. * to vpx_codec_control_(). It indicates that a given control identifier takes
  375. * no argument.
  376. *
  377. * \internal
  378. * It defines a static function without a data argument as a wrapper to the
  379. * type-unsafe internal function.
  380. */
  381. # define VPX_CTRL_VOID(id) \
  382. static vpx_codec_err_t \
  383. vpx_codec_control_##id(vpx_codec_ctx_t*, int) UNUSED;\
  384. \
  385. static vpx_codec_err_t \
  386. vpx_codec_control_##id(vpx_codec_ctx_t *ctx, int ctrl_id) {\
  387. return vpx_codec_control_(ctx, ctrl_id);\
  388. } /**<\hideinitializer*/
  389. #endif
  390. /*!\defgroup cap_xma External Memory Allocation Functions
  391. *
  392. * The following functions are required to be implemented for all codecs
  393. * that advertise the VPX_CODEC_CAP_XMA capability. Calling these functions
  394. * for codecs that don't advertise this capability will result in an error
  395. * code being returned, usually VPX_CODEC_INCAPABLE
  396. * @{
  397. */
  398. /*!\brief Memory Map Entry
  399. *
  400. * This structure is used to contain the properties of a memory segment. It
  401. * is populated by the codec in the request phase, and by the calling
  402. * application once the requested allocation has been performed.
  403. */
  404. typedef struct vpx_codec_mmap {
  405. /*
  406. * The following members are set by the codec when requesting a segment
  407. */
  408. unsigned int id; /**< identifier for the segment's contents */
  409. unsigned long sz; /**< size of the segment, in bytes */
  410. unsigned int align; /**< required alignment of the segment, in bytes */
  411. unsigned int flags; /**< bitfield containing segment properties */
  412. #define VPX_CODEC_MEM_ZERO 0x1 /**< Segment must be zeroed by allocation */
  413. #define VPX_CODEC_MEM_WRONLY 0x2 /**< Segment need not be readable */
  414. #define VPX_CODEC_MEM_FAST 0x4 /**< Place in fast memory, if available */
  415. /* The following members are to be filled in by the allocation function */
  416. void *base; /**< pointer to the allocated segment */
  417. void (*dtor)(struct vpx_codec_mmap *map); /**< destructor to call */
  418. void *priv; /**< allocator private storage */
  419. } vpx_codec_mmap_t; /**< alias for struct vpx_codec_mmap */
  420. /*!\brief Iterate over the list of segments to allocate.
  421. *
  422. * Iterates over a list of the segments to allocate. The iterator storage
  423. * should be initialized to NULL to start the iteration. Iteration is complete
  424. * when this function returns VPX_CODEC_LIST_END. The amount of memory needed to
  425. * allocate is dependent upon the size of the encoded stream. In cases where the
  426. * stream is not available at allocation time, a fixed size must be requested.
  427. * The codec will not be able to operate on streams larger than the size used at
  428. * allocation time.
  429. *
  430. * \param[in] ctx Pointer to this instance's context.
  431. * \param[out] mmap Pointer to the memory map entry to populate.
  432. * \param[in,out] iter Iterator storage, initialized to NULL
  433. *
  434. * \retval #VPX_CODEC_OK
  435. * The memory map entry was populated.
  436. * \retval #VPX_CODEC_ERROR
  437. * Codec does not support XMA mode.
  438. * \retval #VPX_CODEC_MEM_ERROR
  439. * Unable to determine segment size from stream info.
  440. */
  441. vpx_codec_err_t vpx_codec_get_mem_map(vpx_codec_ctx_t *ctx,
  442. vpx_codec_mmap_t *mmap,
  443. vpx_codec_iter_t *iter);
  444. /*!\brief Identify allocated segments to codec instance
  445. *
  446. * Stores a list of allocated segments in the codec. Segments \ref MUST be
  447. * passed in the order they are read from vpx_codec_get_mem_map(), but may be
  448. * passed in groups of any size. Segments \ref MUST be set only once. The
  449. * allocation function \ref MUST ensure that the vpx_codec_mmap_t::base member
  450. * is non-NULL. If the segment requires cleanup handling (e.g., calling free()
  451. * or close()) then the vpx_codec_mmap_t::dtor member \ref MUST be populated.
  452. *
  453. * \param[in] ctx Pointer to this instance's context.
  454. * \param[in] mmaps Pointer to the first memory map entry in the list.
  455. * \param[in] num_maps Number of entries being set at this time
  456. *
  457. * \retval #VPX_CODEC_OK
  458. * The segment was stored in the codec context.
  459. * \retval #VPX_CODEC_INCAPABLE
  460. * Codec does not support XMA mode.
  461. * \retval #VPX_CODEC_MEM_ERROR
  462. * Segment base address was not set, or segment was already stored.
  463. */
  464. vpx_codec_err_t vpx_codec_set_mem_map(vpx_codec_ctx_t *ctx,
  465. vpx_codec_mmap_t *mmaps,
  466. unsigned int num_maps);
  467. /*!@} - end defgroup cap_xma*/
  468. /*!@} - end defgroup codec*/
  469. #endif
  470. #ifdef __cplusplus
  471. }
  472. #endif