1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 /*!\defgroup codec Common Algorithm Interface
13  * This abstraction allows applications to easily support multiple video
14  * formats with minimal code duplication. This section describes the interface
15  * common to all codecs (both encoders and decoders).
16  * @{
17  */
18 
19 /*!\file
20  * \brief Describes the codec algorithm interface to applications.
21  *
22  * This file describes the interface between an application and a
23  * video codec algorithm.
24  *
25  * An application instantiates a specific codec instance by using
26  * aom_codec_init() and a pointer to the algorithm's interface structure:
27  *     <pre>
28  *     my_app.c:
29  *       extern aom_codec_iface_t my_codec;
30  *       {
31  *           aom_codec_ctx_t algo;
32  *           res = aom_codec_init(&algo, &my_codec);
33  *       }
34  *     </pre>
35  *
36  * Once initialized, the instance is managed using other functions from
37  * the aom_codec_* family.
38  */
39 #ifndef AOM_AOM_AOM_CODEC_H_
40 #define AOM_AOM_AOM_CODEC_H_
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #include "aom/aom_image.h"
47 #include "aom/aom_integer.h"
48 
49 /*!\brief Decorator indicating a function is deprecated */
50 #ifndef AOM_DEPRECATED
51 #if defined(__GNUC__) && __GNUC__
52 #define AOM_DEPRECATED __attribute__((deprecated))
53 #elif defined(_MSC_VER)
54 #define AOM_DEPRECATED
55 #else
56 #define AOM_DEPRECATED
57 #endif
58 #endif /* AOM_DEPRECATED */
59 
60 #ifndef AOM_DECLSPEC_DEPRECATED
61 #if defined(__GNUC__) && __GNUC__
62 #define AOM_DECLSPEC_DEPRECATED /**< \copydoc #AOM_DEPRECATED */
63 #elif defined(_MSC_VER)
64 /*!\brief \copydoc #AOM_DEPRECATED */
65 #define AOM_DECLSPEC_DEPRECATED __declspec(deprecated)
66 #else
67 #define AOM_DECLSPEC_DEPRECATED /**< \copydoc #AOM_DEPRECATED */
68 #endif
69 #endif /* AOM_DECLSPEC_DEPRECATED */
70 
71 /*!\brief Decorator indicating a function is potentially unused */
72 #ifdef AOM_UNUSED
73 #elif defined(__GNUC__) || defined(__clang__)
74 #define AOM_UNUSED __attribute__((unused))
75 #else
76 #define AOM_UNUSED
77 #endif
78 
79 /*!\brief Decorator indicating that given struct/union/enum is packed */
80 #ifndef ATTRIBUTE_PACKED
81 #if defined(__GNUC__) && __GNUC__
82 #define ATTRIBUTE_PACKED __attribute__((packed))
83 #elif defined(_MSC_VER)
84 #define ATTRIBUTE_PACKED
85 #else
86 #define ATTRIBUTE_PACKED
87 #endif
88 #endif /* ATTRIBUTE_PACKED */
89 
90 /*!\brief Current ABI version number
91  *
92  * \internal
93  * If this file is altered in any way that changes the ABI, this value
94  * must be bumped.  Examples include, but are not limited to, changing
95  * types, removing or reassigning enums, adding/removing/rearranging
96  * fields to structures
97  */
98 #define AOM_CODEC_ABI_VERSION (5 + AOM_IMAGE_ABI_VERSION) /**<\hideinitializer*/
99 
100 /*!\brief Algorithm return codes */
101 typedef enum {
102   /*!\brief Operation completed without error */
103   AOM_CODEC_OK,
104 
105   /*!\brief Unspecified error */
106   AOM_CODEC_ERROR,
107 
108   /*!\brief Memory operation failed */
109   AOM_CODEC_MEM_ERROR,
110 
111   /*!\brief ABI version mismatch */
112   AOM_CODEC_ABI_MISMATCH,
113 
114   /*!\brief Algorithm does not have required capability */
115   AOM_CODEC_INCAPABLE,
116 
117   /*!\brief The given bitstream is not supported.
118    *
119    * The bitstream was unable to be parsed at the highest level. The decoder
120    * is unable to proceed. This error \ref SHOULD be treated as fatal to the
121    * stream. */
122   AOM_CODEC_UNSUP_BITSTREAM,
123 
124   /*!\brief Encoded bitstream uses an unsupported feature
125    *
126    * The decoder does not implement a feature required by the encoder. This
127    * return code should only be used for features that prevent future
128    * pictures from being properly decoded. This error \ref MAY be treated as
129    * fatal to the stream or \ref MAY be treated as fatal to the current GOP.
130    */
131   AOM_CODEC_UNSUP_FEATURE,
132 
133   /*!\brief The coded data for this stream is corrupt or incomplete
134    *
135    * There was a problem decoding the current frame.  This return code
136    * should only be used for failures that prevent future pictures from
137    * being properly decoded. This error \ref MAY be treated as fatal to the
138    * stream or \ref MAY be treated as fatal to the current GOP. If decoding
139    * is continued for the current GOP, artifacts may be present.
140    */
141   AOM_CODEC_CORRUPT_FRAME,
142 
143   /*!\brief An application-supplied parameter is not valid.
144    *
145    */
146   AOM_CODEC_INVALID_PARAM,
147 
148   /*!\brief An iterator reached the end of list.
149    *
150    */
151   AOM_CODEC_LIST_END
152 
153 } aom_codec_err_t;
154 
155 /*! \brief Codec capabilities bitfield
156  *
157  *  Each codec advertises the capabilities it supports as part of its
158  *  ::aom_codec_iface_t interface structure. Capabilities are extra interfaces
159  *  or functionality, and are not required to be supported.
160  *
161  *  The available flags are specified by AOM_CODEC_CAP_* defines.
162  */
163 typedef long aom_codec_caps_t;
164 #define AOM_CODEC_CAP_DECODER 0x1 /**< Is a decoder */
165 #define AOM_CODEC_CAP_ENCODER 0x2 /**< Is an encoder */
166 
167 /*! \brief Initialization-time Feature Enabling
168  *
169  *  Certain codec features must be known at initialization time, to allow for
170  *  proper memory allocation.
171  *
172  *  The available flags are specified by AOM_CODEC_USE_* defines.
173  */
174 typedef long aom_codec_flags_t;
175 
176 /*!\brief Time Stamp Type
177  *
178  * An integer, which when multiplied by the stream's time base, provides
179  * the absolute time of a sample.
180  */
181 typedef int64_t aom_codec_pts_t;
182 
183 /*!\brief Codec interface structure.
184  *
185  * Contains function pointers and other data private to the codec
186  * implementation. This structure is opaque to the application. Common
187  * functions used with this structure:
188  *   - aom_codec_iface_name: get the name of the codec
189  *   - aom_codec_get_caps: returns the capabilities of the codec (see
190  *     aom_encoder.h for more details)
191  *   - aom_codec_enc_config_default: generate the default config to use
192  *     when initializing the encoder
193  *   - aom_codec_dec_init, aom_codec_enc_init: initialize the codec context
194  *     structure (see documentation on aom_codec_ctx for more information).
195  */
196 typedef const struct aom_codec_iface aom_codec_iface_t;
197 
198 /*!\brief Codec private data structure.
199  *
200  * Contains data private to the codec implementation. This structure is opaque
201  * to the application.
202  */
203 typedef struct aom_codec_priv aom_codec_priv_t;
204 
205 /*!\brief Iterator
206  *
207  * Opaque storage used for iterating over lists.
208  */
209 typedef const void *aom_codec_iter_t;
210 
211 /*!\brief Codec context structure
212  *
213  * All codecs \ref MUST support this context structure fully. In general,
214  * this data should be considered private to the codec algorithm, and
215  * not be manipulated or examined by the calling application. Applications
216  * may reference the 'name' member to get a printable description of the
217  * algorithm.
218  */
219 typedef struct aom_codec_ctx {
220   const char *name;             /**< Printable interface name */
221   aom_codec_iface_t *iface;     /**< Interface pointers */
222   aom_codec_err_t err;          /**< Last returned error */
223   const char *err_detail;       /**< Detailed info, if available */
224   aom_codec_flags_t init_flags; /**< Flags passed at init time */
225   union {
226     /**< Decoder Configuration Pointer */
227     const struct aom_codec_dec_cfg *dec;
228     /**< Encoder Configuration Pointer */
229     const struct aom_codec_enc_cfg *enc;
230     const void *raw;
231   } config;               /**< Configuration pointer aliasing union */
232   aom_codec_priv_t *priv; /**< Algorithm private storage */
233 } aom_codec_ctx_t;
234 
235 /*!\brief Bit depth for codec
236  * *
237  * This enumeration determines the bit depth of the codec.
238  */
239 typedef enum aom_bit_depth {
240   AOM_BITS_8 = 8,   /**<  8 bits */
241   AOM_BITS_10 = 10, /**< 10 bits */
242   AOM_BITS_12 = 12, /**< 12 bits */
243 } aom_bit_depth_t;
244 
245 /*!\brief Superblock size selection.
246  *
247  * Defines the superblock size used for encoding. The superblock size can
248  * either be fixed at 64x64 or 128x128 pixels, or it can be dynamically
249  * selected by the encoder for each frame.
250  */
251 typedef enum aom_superblock_size {
252   AOM_SUPERBLOCK_SIZE_64X64,   /**< Always use 64x64 superblocks. */
253   AOM_SUPERBLOCK_SIZE_128X128, /**< Always use 128x128 superblocks. */
254   AOM_SUPERBLOCK_SIZE_DYNAMIC  /**< Select superblock size dynamically. */
255 } aom_superblock_size_t;
256 
257 /*
258  * Library Version Number Interface
259  *
260  * For example, see the following sample return values:
261  *     aom_codec_version()           (1<<16 | 2<<8 | 3)
262  *     aom_codec_version_str()       "v1.2.3-rc1-16-gec6a1ba"
263  *     aom_codec_version_extra_str() "rc1-16-gec6a1ba"
264  */
265 
266 /*!\brief Return the version information (as an integer)
267  *
268  * Returns a packed encoding of the library version number. This will only
269  * include
270  * the major.minor.patch component of the version number. Note that this encoded
271  * value should be accessed through the macros provided, as the encoding may
272  * change
273  * in the future.
274  *
275  */
276 int aom_codec_version(void);
277 
278 /*!\brief Return the version major number */
279 #define aom_codec_version_major() ((aom_codec_version() >> 16) & 0xff)
280 
281 /*!\brief Return the version minor number */
282 #define aom_codec_version_minor() ((aom_codec_version() >> 8) & 0xff)
283 
284 /*!\brief Return the version patch number */
285 #define aom_codec_version_patch() ((aom_codec_version() >> 0) & 0xff)
286 
287 /*!\brief Return the version information (as a string)
288  *
289  * Returns a printable string containing the full library version number. This
290  * may
291  * contain additional text following the three digit version number, as to
292  * indicate
293  * release candidates, prerelease versions, etc.
294  *
295  */
296 const char *aom_codec_version_str(void);
297 
298 /*!\brief Return the version information (as a string)
299  *
300  * Returns a printable "extra string". This is the component of the string
301  * returned
302  * by aom_codec_version_str() following the three digit version number.
303  *
304  */
305 const char *aom_codec_version_extra_str(void);
306 
307 /*!\brief Return the build configuration
308  *
309  * Returns a printable string containing an encoded version of the build
310  * configuration. This may be useful to aom support.
311  *
312  */
313 const char *aom_codec_build_config(void);
314 
315 /*!\brief Return the name for a given interface
316  *
317  * Returns a human readable string for name of the given codec interface.
318  *
319  * \param[in]    iface     Interface pointer
320  *
321  */
322 const char *aom_codec_iface_name(aom_codec_iface_t *iface);
323 
324 /*!\brief Convert error number to printable string
325  *
326  * Returns a human readable string for the last error returned by the
327  * algorithm. The returned error will be one line and will not contain
328  * any newline characters.
329  *
330  *
331  * \param[in]    err     Error number.
332  *
333  */
334 const char *aom_codec_err_to_string(aom_codec_err_t err);
335 
336 /*!\brief Retrieve error synopsis for codec context
337  *
338  * Returns a human readable string for the last error returned by the
339  * algorithm. The returned error will be one line and will not contain
340  * any newline characters.
341  *
342  *
343  * \param[in]    ctx     Pointer to this instance's context.
344  *
345  */
346 const char *aom_codec_error(aom_codec_ctx_t *ctx);
347 
348 /*!\brief Retrieve detailed error information for codec context
349  *
350  * Returns a human readable string providing detailed information about
351  * the last error.
352  *
353  * \param[in]    ctx     Pointer to this instance's context.
354  *
355  * \retval NULL
356  *     No detailed information is available.
357  */
358 const char *aom_codec_error_detail(aom_codec_ctx_t *ctx);
359 
360 /* REQUIRED FUNCTIONS
361  *
362  * The following functions are required to be implemented for all codecs.
363  * They represent the base case functionality expected of all codecs.
364  */
365 
366 /*!\brief Destroy a codec instance
367  *
368  * Destroys a codec context, freeing any associated memory buffers.
369  *
370  * \param[in] ctx   Pointer to this instance's context
371  *
372  * \retval #AOM_CODEC_OK
373  *     The codec algorithm initialized.
374  * \retval #AOM_CODEC_MEM_ERROR
375  *     Memory allocation failed.
376  */
377 aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx);
378 
379 /*!\brief Get the capabilities of an algorithm.
380  *
381  * Retrieves the capabilities bitfield from the algorithm's interface.
382  *
383  * \param[in] iface   Pointer to the algorithm interface
384  *
385  */
386 aom_codec_caps_t aom_codec_get_caps(aom_codec_iface_t *iface);
387 
388 /*!\name Codec Control
389  *
390  * The aom_codec_control function exchanges algorithm specific data with the
391  * codec instance. Additionally, the macro AOM_CODEC_CONTROL_TYPECHECKED is
392  * provided, which will type-check the parameter against the control ID before
393  * calling aom_codec_control - note that this macro requires the control ID
394  * to be directly encoded in it, e.g.,
395  * AOM_CODEC_CONTROL_TYPECHECKED(&ctx, AOME_SET_CPUUSED, 8).
396  *
397  * The codec control IDs can be found in aom.h, aomcx.h, and aomdx.h
398  * (defined as aom_com_control_id, aome_enc_control_id, and aom_dec_control_id).
399  * @{
400  */
401 /*!\brief Algorithm Control
402  *
403  * aom_codec_control takes a context, a control ID, and a third parameter
404  * (with varying type). If the context is non-null and an error occurs,
405  * ctx->err will be set to the same value as the return value.
406  *
407  * \param[in]     ctx              Pointer to this instance's context
408  * \param[in]     ctrl_id          Algorithm specific control identifier
409  *
410  * \retval #AOM_CODEC_OK
411  *     The control request was processed.
412  * \retval #AOM_CODEC_ERROR
413  *     The control request was not processed.
414  * \retval #AOM_CODEC_INVALID_PARAM
415  *     The data was not valid.
416  */
417 aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id, ...);
418 
419 /*!\brief aom_codec_control wrapper macro (adds type-checking, less flexible)
420  *
421  * This macro allows for type safe conversions across the variadic parameter
422  * to aom_codec_control(). However, it requires the explicit control ID
423  * be passed in (it cannot be passed in via a variable) -- otherwise a compiler
424  * error will occur. After the type checking, it calls aom_codec_control.
425  */
426 #define AOM_CODEC_CONTROL_TYPECHECKED(ctx, id, data) \
427   aom_codec_control_typechecked_##id(ctx, id, data) /**<\hideinitializer*/
428 
429 /*!\brief Creates typechecking mechanisms for aom_codec_control
430  *
431  * It defines a static function with the correctly typed arguments as a wrapper
432  * to the type-unsafe aom_codec_control function. It also creates a typedef
433  * for each type.
434  */
435 #define AOM_CTRL_USE_TYPE(id, typ)                           \
436   static aom_codec_err_t aom_codec_control_typechecked_##id( \
437       aom_codec_ctx_t *, int, typ) AOM_UNUSED;               \
438   static aom_codec_err_t aom_codec_control_typechecked_##id( \
439       aom_codec_ctx_t *ctx, int ctrl, typ data) {            \
440     return aom_codec_control(ctx, ctrl, data);               \
441   } /**<\hideinitializer*/                                   \
442   typedef typ aom_codec_control_type_##id;
443 /*!@} end Codec Control group */
444 
445 /*!\brief OBU types. */
446 typedef enum ATTRIBUTE_PACKED {
447   OBU_SEQUENCE_HEADER = 1,
448   OBU_TEMPORAL_DELIMITER = 2,
449   OBU_FRAME_HEADER = 3,
450   OBU_TILE_GROUP = 4,
451   OBU_METADATA = 5,
452   OBU_FRAME = 6,
453   OBU_REDUNDANT_FRAME_HEADER = 7,
454   OBU_TILE_LIST = 8,
455   OBU_PADDING = 15,
456 } OBU_TYPE;
457 
458 /*!\brief OBU metadata types. */
459 typedef enum {
460   OBU_METADATA_TYPE_AOM_RESERVED_0 = 0,
461   OBU_METADATA_TYPE_HDR_CLL = 1,
462   OBU_METADATA_TYPE_HDR_MDCV = 2,
463   OBU_METADATA_TYPE_SCALABILITY = 3,
464   OBU_METADATA_TYPE_ITUT_T35 = 4,
465   OBU_METADATA_TYPE_TIMECODE = 5,
466 } OBU_METADATA_TYPE;
467 
468 /*!\brief Returns string representation of OBU_TYPE.
469  *
470  * \param[in]     type            The OBU_TYPE to convert to string.
471  */
472 const char *aom_obu_type_to_string(OBU_TYPE type);
473 
474 /*!@} - end defgroup codec*/
475 #ifdef __cplusplus
476 }
477 #endif
478 #endif  // AOM_AOM_AOM_CODEC_H_
479