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 // 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__) 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__) 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__) 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 (7 + 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. The bits are 227 * allocated as follows: 228 * 0x1 - 0x80: codec (common to decoder and encoder) 229 * 0x100 - 0x8000: decoder 230 * 0x10000 - 0x800000: encoder 231 */ 232 typedef long aom_codec_flags_t; 233 234 // Experimental feature policy 235 // 236 // New features may be marked as experimental. Experimental features are not 237 // part of the stable API and may be modified or removed in a future release. 238 // Experimental features are made available only if you pass the 239 // AOM_CODEC_USE_EXPERIMENTAL flag to the codec init function. 240 // 241 // If you use experimental features, you must rebuild your code whenever you 242 // update to a new libaom release, and you must be prepared to modify your code 243 // when an experimental feature you use is modified or removed. If you are not 244 // sure, DO NOT use experimental features. 245 #define AOM_CODEC_USE_EXPERIMENTAL 0x1 /**< Enables experimental features */ 246 247 /*!\brief Time Stamp Type 248 * 249 * An integer, which when multiplied by the stream's time base, provides 250 * the absolute time of a sample. 251 */ 252 typedef int64_t aom_codec_pts_t; 253 254 /*!\brief Codec interface structure. 255 * 256 * Contains function pointers and other data private to the codec 257 * implementation. This structure is opaque to the application. Common 258 * functions used with this structure: 259 * - aom_codec_iface_name(aom_codec_iface_t *iface): get the 260 * name of the codec 261 * - aom_codec_get_caps(aom_codec_iface_t *iface): returns 262 * the capabilities of the codec 263 * - aom_codec_enc_config_default: generate the default config for 264 * initializing the encoder (see documentation in aom_encoder.h) 265 * - aom_codec_dec_init, aom_codec_enc_init: initialize the codec context 266 * structure (see documentation on aom_codec_ctx). 267 * 268 * To get access to the AV1 encoder and decoder, use aom_codec_av1_cx() and 269 * aom_codec_av1_dx(). 270 */ 271 typedef const struct aom_codec_iface aom_codec_iface_t; 272 273 /*!\brief Codec private data structure. 274 * 275 * Contains data private to the codec implementation. This structure is opaque 276 * to the application. 277 */ 278 typedef struct aom_codec_priv aom_codec_priv_t; 279 280 /*!\brief Compressed Frame Flags 281 * 282 * This type represents a bitfield containing information about a compressed 283 * frame that may be useful to an application. The most significant 16 bits 284 * can be used by an algorithm to provide additional detail, for example to 285 * support frame types that are codec specific (MPEG-1 D-frames for example) 286 */ 287 typedef uint32_t aom_codec_frame_flags_t; 288 #define AOM_FRAME_IS_KEY 0x1u /**< frame is the start of a GOP */ 289 /*!\brief frame can be dropped without affecting the stream (no future frame 290 * depends on this one) */ 291 #define AOM_FRAME_IS_DROPPABLE 0x2u 292 /*!\brief this is an INTRA_ONLY frame */ 293 #define AOM_FRAME_IS_INTRAONLY 0x10u 294 /*!\brief this is an S-frame */ 295 #define AOM_FRAME_IS_SWITCH 0x20u 296 /*!\brief this is an error-resilient frame */ 297 #define AOM_FRAME_IS_ERROR_RESILIENT 0x40u 298 /*!\brief this is a key-frame dependent recovery-point frame */ 299 #define AOM_FRAME_IS_DELAYED_RANDOM_ACCESS_POINT 0x80u 300 301 /*!\brief Iterator 302 * 303 * Opaque storage used for iterating over lists. 304 */ 305 typedef const void *aom_codec_iter_t; 306 307 /*!\brief Codec context structure 308 * 309 * All codecs \ref MUST support this context structure fully. In general, 310 * this data should be considered private to the codec algorithm, and 311 * not be manipulated or examined by the calling application. Applications 312 * may reference the 'name' member to get a printable description of the 313 * algorithm. 314 */ 315 typedef struct aom_codec_ctx { 316 const char *name; /**< Printable interface name */ 317 aom_codec_iface_t *iface; /**< Interface pointers */ 318 aom_codec_err_t err; /**< Last returned error */ 319 const char *err_detail; /**< Detailed info, if available */ 320 aom_codec_flags_t init_flags; /**< Flags passed at init time */ 321 union { 322 /**< Decoder Configuration Pointer */ 323 const struct aom_codec_dec_cfg *dec; 324 /**< Encoder Configuration Pointer */ 325 const struct aom_codec_enc_cfg *enc; 326 const void *raw; 327 } config; /**< Configuration pointer aliasing union */ 328 aom_codec_priv_t *priv; /**< Algorithm private storage */ 329 } aom_codec_ctx_t; 330 331 /*!\brief Bit depth for codec 332 * * 333 * This enumeration determines the bit depth of the codec. 334 */ 335 typedef enum aom_bit_depth { 336 AOM_BITS_8 = 8, /**< 8 bits */ 337 AOM_BITS_10 = 10, /**< 10 bits */ 338 AOM_BITS_12 = 12, /**< 12 bits */ 339 } aom_bit_depth_t; 340 341 /*!\brief Superblock size selection. 342 * 343 * Defines the superblock size used for encoding. The superblock size can 344 * either be fixed at 64x64 or 128x128 pixels, or it can be dynamically 345 * selected by the encoder for each frame. 346 */ 347 typedef enum aom_superblock_size { 348 AOM_SUPERBLOCK_SIZE_64X64, /**< Always use 64x64 superblocks. */ 349 AOM_SUPERBLOCK_SIZE_128X128, /**< Always use 128x128 superblocks. */ 350 AOM_SUPERBLOCK_SIZE_DYNAMIC /**< Select superblock size dynamically. */ 351 } aom_superblock_size_t; 352 353 /* 354 * Library Version Number Interface 355 * 356 * For example, see the following sample return values: 357 * aom_codec_version() (1<<16 | 2<<8 | 3) 358 * aom_codec_version_str() "v1.2.3-rc1-16-gec6a1ba" 359 * aom_codec_version_extra_str() "rc1-16-gec6a1ba" 360 */ 361 362 /*!\brief Return the version information (as an integer) 363 * 364 * Returns a packed encoding of the library version number. This will only 365 * include the major.minor.patch component of the version number. Note that this 366 * encoded value should be accessed through the macros provided, as the encoding 367 * may change in the future. 368 * 369 */ 370 int aom_codec_version(void); 371 372 /*!\brief Return the major version number */ 373 #define aom_codec_version_major() ((aom_codec_version() >> 16) & 0xff) 374 375 /*!\brief Return the minor version number */ 376 #define aom_codec_version_minor() ((aom_codec_version() >> 8) & 0xff) 377 378 /*!\brief Return the patch version number */ 379 #define aom_codec_version_patch() ((aom_codec_version() >> 0) & 0xff) 380 381 /*!\brief Return the version information (as a string) 382 * 383 * Returns a printable string containing the full library version number. This 384 * may contain additional text following the three digit version number, as to 385 * indicate release candidates, pre-release versions, etc. 386 * 387 */ 388 const char *aom_codec_version_str(void); 389 390 /*!\brief Return the version information (as a string) 391 * 392 * Returns a printable "extra string". This is the component of the string 393 * returned by aom_codec_version_str() following the three digit version number. 394 * 395 */ 396 const char *aom_codec_version_extra_str(void); 397 398 /*!\brief Return the build configuration 399 * 400 * Returns a printable string containing an encoded version of the build 401 * configuration. This may be useful to aom support. 402 * 403 */ 404 const char *aom_codec_build_config(void); 405 406 /*!\brief Return the name for a given interface 407 * 408 * Returns a human readable string for name of the given codec interface. 409 * 410 * \param[in] iface Interface pointer 411 * 412 */ 413 const char *aom_codec_iface_name(aom_codec_iface_t *iface); 414 415 /*!\brief Convert error number to printable string 416 * 417 * Returns a human readable string for the last error returned by the 418 * algorithm. The returned error will be one line and will not contain 419 * any newline characters. 420 * 421 * 422 * \param[in] err Error number. 423 * 424 */ 425 const char *aom_codec_err_to_string(aom_codec_err_t err); 426 427 /*!\brief Retrieve error synopsis for codec context 428 * 429 * Returns a human readable string for the last error returned by the 430 * algorithm. The returned error will be one line and will not contain 431 * any newline characters. 432 * 433 * 434 * \param[in] ctx Pointer to this instance's context. 435 * 436 */ 437 const char *aom_codec_error(const aom_codec_ctx_t *ctx); 438 439 /*!\brief Retrieve detailed error information for codec context 440 * 441 * Returns a human readable string providing detailed information about 442 * the last error. The returned string is only valid until the next 443 * aom_codec_* function call (except aom_codec_error and 444 * aom_codec_error_detail) on the codec context. 445 * 446 * \param[in] ctx Pointer to this instance's context. 447 * 448 * \retval NULL 449 * No detailed information is available. 450 */ 451 const char *aom_codec_error_detail(const aom_codec_ctx_t *ctx); 452 453 /* REQUIRED FUNCTIONS 454 * 455 * The following functions are required to be implemented for all codecs. 456 * They represent the base case functionality expected of all codecs. 457 */ 458 459 /*!\brief Destroy a codec instance 460 * 461 * Destroys a codec context, freeing any associated memory buffers. 462 * 463 * \param[in] ctx Pointer to this instance's context 464 * 465 * \retval #AOM_CODEC_OK 466 * The codec instance has been destroyed. 467 * \retval #AOM_CODEC_INVALID_PARAM 468 * ctx is a null pointer. 469 * \retval #AOM_CODEC_ERROR 470 * Codec context not initialized. 471 */ 472 aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx); 473 474 /*!\brief Get the capabilities of an algorithm. 475 * 476 * Retrieves the capabilities bitfield from the algorithm's interface. 477 * 478 * \param[in] iface Pointer to the algorithm interface 479 * 480 */ 481 aom_codec_caps_t aom_codec_get_caps(aom_codec_iface_t *iface); 482 483 /*!\name Codec Control 484 * 485 * The aom_codec_control function exchanges algorithm specific data with the 486 * codec instance. Additionally, the macro AOM_CODEC_CONTROL_TYPECHECKED is 487 * provided, which will type-check the parameter against the control ID before 488 * calling aom_codec_control - note that this macro requires the control ID 489 * to be directly encoded in it, e.g., 490 * AOM_CODEC_CONTROL_TYPECHECKED(&ctx, AOME_SET_CPUUSED, 8). 491 * 492 * The codec control IDs can be found in aom.h, aomcx.h, and aomdx.h 493 * (defined as aom_com_control_id, aome_enc_control_id, and aom_dec_control_id). 494 * @{ 495 */ 496 /*!\brief Algorithm Control 497 * 498 * aom_codec_control takes a context, a control ID, and a third parameter 499 * (with varying type). If the context is non-null and an error occurs, 500 * ctx->err will be set to the same value as the return value. 501 * 502 * \param[in] ctx Pointer to this instance's context 503 * \param[in] ctrl_id Algorithm specific control identifier. 504 * Must be nonzero. 505 * 506 * \retval #AOM_CODEC_OK 507 * The control request was processed. 508 * \retval #AOM_CODEC_ERROR 509 * The control request was not processed. 510 * \retval #AOM_CODEC_INVALID_PARAM 511 * The control ID was zero, or the data was not valid. 512 */ 513 aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id, ...); 514 515 /*!\brief Key & Value API 516 * 517 * aom_codec_set_option() takes a context, a key (option name) and a value. If 518 * the context is non-null and an error occurs, ctx->err will be set to the same 519 * value as the return value. 520 * 521 * \param[in] ctx Pointer to this instance's context 522 * \param[in] name The name of the option (key) 523 * \param[in] value The value of the option 524 * 525 * \retval #AOM_CODEC_OK 526 * The value of the option was set. 527 * \retval #AOM_CODEC_INVALID_PARAM 528 * The data was not valid. 529 * \retval #AOM_CODEC_ERROR 530 * The option was not successfully set. 531 */ 532 aom_codec_err_t aom_codec_set_option(aom_codec_ctx_t *ctx, const char *name, 533 const char *value); 534 535 /*!\brief aom_codec_control wrapper macro (adds type-checking, less flexible) 536 * 537 * This macro allows for type safe conversions across the variadic parameter 538 * to aom_codec_control(). However, it requires the explicit control ID 539 * be passed in (it cannot be passed in via a variable) -- otherwise a compiler 540 * error will occur. After the type checking, it calls aom_codec_control. 541 */ 542 #define AOM_CODEC_CONTROL_TYPECHECKED(ctx, id, data) \ 543 aom_codec_control_typechecked_##id(ctx, id, data) /**<\hideinitializer*/ 544 545 /*!\brief Creates type checking mechanisms for aom_codec_control 546 * 547 * It defines a static function with the correctly typed arguments as a wrapper 548 * to the type-unsafe aom_codec_control function. It also creates a typedef 549 * for each type. 550 */ 551 #define AOM_CTRL_USE_TYPE(id, typ) \ 552 static aom_codec_err_t aom_codec_control_typechecked_##id( \ 553 aom_codec_ctx_t *, int, typ) AOM_UNUSED; \ 554 static aom_codec_err_t aom_codec_control_typechecked_##id( \ 555 aom_codec_ctx_t *ctx, int ctrl, typ data) { \ 556 return aom_codec_control(ctx, ctrl, data); \ 557 } /**<\hideinitializer*/ \ 558 typedef typ aom_codec_control_type_##id; 559 /*!@} end Codec Control group */ 560 561 /*!\brief OBU types. */ 562 typedef enum ATTRIBUTE_PACKED { 563 OBU_SEQUENCE_HEADER = 1, 564 OBU_TEMPORAL_DELIMITER = 2, 565 OBU_FRAME_HEADER = 3, 566 OBU_TILE_GROUP = 4, 567 OBU_METADATA = 5, 568 OBU_FRAME = 6, 569 OBU_REDUNDANT_FRAME_HEADER = 7, 570 OBU_TILE_LIST = 8, 571 OBU_PADDING = 15, 572 } OBU_TYPE; 573 574 /*!\brief OBU metadata types. */ 575 typedef enum { 576 OBU_METADATA_TYPE_AOM_RESERVED_0 = 0, 577 OBU_METADATA_TYPE_HDR_CLL = 1, 578 OBU_METADATA_TYPE_HDR_MDCV = 2, 579 OBU_METADATA_TYPE_SCALABILITY = 3, 580 OBU_METADATA_TYPE_ITUT_T35 = 4, 581 OBU_METADATA_TYPE_TIMECODE = 5, 582 } OBU_METADATA_TYPE; 583 584 /*!\brief Returns string representation of OBU_TYPE. 585 * 586 * \param[in] type The OBU_TYPE to convert to string. 587 */ 588 const char *aom_obu_type_to_string(OBU_TYPE type); 589 590 /*!@} - end defgroup codec*/ 591 #ifdef __cplusplus 592 } 593 #endif 594 #endif // AOM_AOM_AOM_CODEC_H_ 595