vpx_codec.h 19 KB

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