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