// Copyright 2019 Joe Drago. All rights reserved. // SPDX-License-Identifier: BSD-2-Clause #ifndef AVIF_AVIF_H #define AVIF_AVIF_H #include <stddef.h> #include <stdint.h> #ifdef __cplusplus extern "C" { #endif // --------------------------------------------------------------------------- // Export macros // AVIF_BUILDING_SHARED_LIBS should only be defined when libavif is being built // as a shared library. // AVIF_DLL should be defined if libavif is a shared library. If you are using // libavif as a CMake dependency, through a CMake package config file or through // pkg-config, this is defined automatically. // // Here's what AVIF_API will be defined as in shared build: // | | Windows | Unix | // | Build | __declspec(dllexport) | __attribute__((visibility("default"))) | // | Use | __declspec(dllimport) | | // // For static build, AVIF_API is always defined as nothing. #if defined(_WIN32) #define AVIF_HELPER_EXPORT … #define AVIF_HELPER_IMPORT … #elif defined(__GNUC__) && __GNUC__ >= 4 #define AVIF_HELPER_EXPORT … #define AVIF_HELPER_IMPORT #else #define AVIF_HELPER_EXPORT #define AVIF_HELPER_IMPORT #endif #if defined(AVIF_DLL) #if defined(AVIF_BUILDING_SHARED_LIBS) #define AVIF_API … #else #define AVIF_API … #endif // defined(AVIF_BUILDING_SHARED_LIBS) #else #define AVIF_API #endif // defined(AVIF_DLL) // [[nodiscard]] requires C++17 and C23. // // If the -std=c2x or -std=gnu2x option is specified, __STDC_VERSION__ is // * 202000L in GCC 13.2.0, Clang 16.0.6, and Apple Clang 15.0.0; or // * 202311L in Clang 19.0.0git. // If the /std:clatest option is specified, __STDC_VERSION__ is // * 202312L in Microsoft Visual Studio 17.10.5. #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L) #define AVIF_NODISCARD … #else // Starting with 3.9, clang allows defining the warn_unused_result attribute for enums. #if defined(__clang__) && defined(__has_attribute) && ((__clang_major__ << 8) | __clang_minor__) >= ((3 << 8) | 9) #if __has_attribute(warn_unused_result) #define AVIF_NODISCARD … #else #define AVIF_NODISCARD #endif #else #define AVIF_NODISCARD #endif #endif // --------------------------------------------------------------------------- // Constants // AVIF_VERSION_DEVEL should always be 0 for official releases / version tags, // and non-zero during development of the next release. This should allow for // downstream projects to do greater-than preprocessor checks on AVIF_VERSION // to leverage in-development code without breaking their stable builds. #define AVIF_VERSION_MAJOR … #define AVIF_VERSION_MINOR … #define AVIF_VERSION_PATCH … #define AVIF_VERSION_DEVEL … #define AVIF_VERSION … avifBool; #define AVIF_TRUE … #define AVIF_FALSE … #define AVIF_DIAGNOSTICS_ERROR_BUFFER_SIZE … // A reasonable default for maximum image size (in pixel count) to avoid out-of-memory errors or // integer overflow in (32-bit) int or unsigned int arithmetic operations. #define AVIF_DEFAULT_IMAGE_SIZE_LIMIT … // A reasonable default for maximum image dimension (width or height). #define AVIF_DEFAULT_IMAGE_DIMENSION_LIMIT … // a 12 hour AVIF image sequence, running at 60 fps (a basic sanity check as this is quite ridiculous) #define AVIF_DEFAULT_IMAGE_COUNT_LIMIT … #define AVIF_QUALITY_DEFAULT … #define AVIF_QUALITY_LOSSLESS … #define AVIF_QUALITY_WORST … #define AVIF_QUALITY_BEST … #define AVIF_QUANTIZER_LOSSLESS … #define AVIF_QUANTIZER_BEST_QUALITY … #define AVIF_QUANTIZER_WORST_QUALITY … #define AVIF_PLANE_COUNT_YUV … #define AVIF_SPEED_DEFAULT … #define AVIF_SPEED_SLOWEST … #define AVIF_SPEED_FASTEST … // This value is used to indicate that an animated AVIF file has to be repeated infinitely. #define AVIF_REPETITION_COUNT_INFINITE … // This value is used if an animated AVIF file does not have repetitions specified using an EditList box. Applications can choose // to handle this case however they want. #define AVIF_REPETITION_COUNT_UNKNOWN … // The number of spatial layers in AV1, with spatial_id = 0..3. #define AVIF_MAX_AV1_LAYER_COUNT … avifPlanesFlag; avifPlanesFlags; avifChannelIndex; // --------------------------------------------------------------------------- // Version AVIF_API const char * avifVersion(void); AVIF_API void avifCodecVersions(char outBuffer[256]); AVIF_API unsigned int avifLibYUVVersion(void); // returns 0 if libavif wasn't compiled with libyuv support // --------------------------------------------------------------------------- // Memory management // Returns NULL on memory allocation failure. AVIF_API void * avifAlloc(size_t size); AVIF_API void avifFree(void * p); // --------------------------------------------------------------------------- // avifResult avifResult; AVIF_API const char * avifResultToString(avifResult result); // --------------------------------------------------------------------------- // avifHeaderFormat avifHeaderFormat; // --------------------------------------------------------------------------- // avifROData/avifRWData: Generic raw memory storage avifROData; // Note: Use avifRWDataFree() if any avif*() function populates one of these. avifRWData; // clang-format off // Initialize avifROData/avifRWData on the stack with this #define AVIF_DATA_EMPTY … // clang-format on // The avifRWData input must be zero-initialized before being manipulated with these functions. // If AVIF_RESULT_OUT_OF_MEMORY is returned, raw is left unchanged. AVIF_API avifResult avifRWDataRealloc(avifRWData * raw, size_t newSize); AVIF_API avifResult avifRWDataSet(avifRWData * raw, const uint8_t * data, size_t len); AVIF_API void avifRWDataFree(avifRWData * raw); // --------------------------------------------------------------------------- // Metadata // Validates the first bytes of the Exif payload and finds the TIFF header offset (up to UINT32_MAX). AVIF_API avifResult avifGetExifTiffHeaderOffset(const uint8_t * exif, size_t exifSize, size_t * offset); // Returns the offset to the Exif 8-bit orientation value and AVIF_RESULT_OK, or an error. // If the offset is set to exifSize, there was no parsing error but no orientation tag was found. AVIF_API avifResult avifGetExifOrientationOffset(const uint8_t * exif, size_t exifSize, size_t * offset); // --------------------------------------------------------------------------- // avifPixelFormat // // Note to libavif maintainers: The lookup tables in avifImageYUVToRGBLibYUV // rely on the ordering of this enum values for their correctness. So changing // the values in this enum will require auditing avifImageYUVToRGBLibYUV for // correctness. avifPixelFormat; AVIF_API const char * avifPixelFormatToString(avifPixelFormat format); avifPixelFormatInfo; // Returns the avifPixelFormatInfo depending on the avifPixelFormat. // When monochrome is AVIF_TRUE, chromaShiftX and chromaShiftY are set to 1 according to the AV1 specification but they should be ignored. // // Note: This function implements the second table on page 119 of the AV1 specification version 1.0.0 with Errata 1. // For monochrome 4:0:0, subsampling_x and subsampling are specified as 1 to allow // an AV1 implementation that only supports profile 0 to hardcode subsampling_x and subsampling_y to 1. AVIF_API void avifGetPixelFormatInfo(avifPixelFormat format, avifPixelFormatInfo * info); // --------------------------------------------------------------------------- // avifChromaSamplePosition avifChromaSamplePosition; // --------------------------------------------------------------------------- // avifRange avifRange; // --------------------------------------------------------------------------- // CICP enums - https://www.itu.int/rec/T-REC-H.273-201612-S/en enum { … }; avifColorPrimaries; // AVIF_COLOR_PRIMARIES_* // outPrimaries: rX, rY, gX, gY, bX, bY, wX, wY AVIF_API void avifColorPrimariesGetValues(avifColorPrimaries acp, float outPrimaries[8]); AVIF_API avifColorPrimaries avifColorPrimariesFind(const float inPrimaries[8], const char ** outName); enum { … }; avifTransferCharacteristics; // AVIF_TRANSFER_CHARACTERISTICS_* // If the given transfer characteristics can be expressed with a simple gamma value, sets 'gamma' // to that value and returns AVIF_RESULT_OK. Returns an error otherwise. AVIF_API avifResult avifTransferCharacteristicsGetGamma(avifTransferCharacteristics atc, float * gamma); AVIF_API avifTransferCharacteristics avifTransferCharacteristicsFindByGamma(float gamma); enum { … }; avifMatrixCoefficients; // AVIF_MATRIX_COEFFICIENTS_* // --------------------------------------------------------------------------- // avifDiagnostics avifDiagnostics; AVIF_API void avifDiagnosticsClearError(avifDiagnostics * diag); // --------------------------------------------------------------------------- // Fraction utility avifFraction; // --------------------------------------------------------------------------- // Optional transformation structs avifTransformFlag; avifTransformFlags; avifPixelAspectRatioBox; avifCleanApertureBox; avifImageRotation; avifImageMirror; // --------------------------------------------------------------------------- // avifCropRect - Helper struct/functions to work with avifCleanApertureBox avifCropRect; // These will return AVIF_FALSE if the resultant values violate any standards, and if so, the output // values are not guaranteed to be complete or correct and should not be used. AVIF_NODISCARD AVIF_API avifBool avifCropRectConvertCleanApertureBox(avifCropRect * cropRect, const avifCleanApertureBox * clap, uint32_t imageW, uint32_t imageH, avifPixelFormat yuvFormat, avifDiagnostics * diag); AVIF_NODISCARD AVIF_API avifBool avifCleanApertureBoxConvertCropRect(avifCleanApertureBox * clap, const avifCropRect * cropRect, uint32_t imageW, uint32_t imageH, avifPixelFormat yuvFormat, avifDiagnostics * diag); // --------------------------------------------------------------------------- // avifContentLightLevelInformationBox avifContentLightLevelInformationBox; #if defined(AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP) // --------------------------------------------------------------------------- // avifGainMap // Gain Maps are a solution for a consistent and adaptive display of HDR images. // Gain Maps are a HIGHLY EXPERIMENTAL FEATURE. The format might still change and // images containing a gain map encoded with the current version of libavif might // not decode with a future version of libavif. The API is not guaranteed // to be stable, and might even be removed in the future. Use at your own risk. // This is based on ISO/IEC JTC 1/SC 29/WG 3 m64379 // This product includes Gain Map technology under license by Adobe. // // Terms: // base image: main image stored in the file, shown by viewers that do not support // gain maps // alternate image: image obtained by combining the base image and the gain map // gain map: data structure that contains pixels and metadata used for conversion // between the base image and the alternate image struct avifImage; // Gain map metadata, to apply the gain map. Fully applying the gain map to the base // image results in the alternate image. // All field pairs ending with 'N' and 'D' are fractional values (numerator and denominator). avifGainMapMetadata; // Gain map image and associated metadata. // Must be allocated by calling avifGainMapCreate(). avifGainMap; // Allocates a gain map. Returns NULL if a memory allocation failed. // The 'image' field is NULL by default and must be allocated separately. AVIF_API avifGainMap * avifGainMapCreate(void); // Frees a gain map, including the 'image' field if non NULL. AVIF_API void avifGainMapDestroy(avifGainMap * gainMap); // Same as avifGainMapMetadata, but with fields of type double instead of uint32_t fractions. // Use avifGainMapMetadataDoubleToFractions() to convert this to a avifGainMapMetadata. // See avifGainMapMetadata for detailed descriptions of fields. avifGainMapMetadataDouble; // Converts a avifGainMapMetadataDouble to avifGainMapMetadata by converting double values // to the closest uint32_t fractions. // Returns AVIF_FALSE if some field values are < 0 or > UINT32_MAX. AVIF_NODISCARD AVIF_API avifBool avifGainMapMetadataDoubleToFractions(avifGainMapMetadata * dst, const avifGainMapMetadataDouble * src); // Converts a avifGainMapMetadata to avifGainMapMetadataDouble by converting fractions to double values. // Returns AVIF_FALSE if some denominators are zero. AVIF_NODISCARD AVIF_API avifBool avifGainMapMetadataFractionsToDouble(avifGainMapMetadataDouble * dst, const avifGainMapMetadata * src); #endif // AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP // --------------------------------------------------------------------------- #if defined(AVIF_ENABLE_EXPERIMENTAL_SAMPLE_TRANSFORM) // Sample Transforms are a HIGHLY EXPERIMENTAL FEATURE. The format might still // change and images containing a sample transform item encoded with the current // version of libavif might not decode with a future version of libavif. // Use at your own risk. // This is based on a proposal from the Alliance for Open Media. typedef enum avifSampleTransformRecipe { AVIF_SAMPLE_TRANSFORM_NONE, // Encode the 8 most significant bits of each input image sample losslessly // into a base image. The remaining 8 least significant bits are encoded in // a separate hidden image item. The two are combined at decoding into one // image with the same bit depth as the original image. It is backward // compatible in the sense that it is possible to decode only the base image // (ignoring the hidden image item), leading to a valid image but with // precision loss (16-bit samples truncated to the 8 most significant bits). AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_8B_8B, // Encode the 12 most significant bits of each input image sample losslessly // into a base image. The remaining 4 least significant bits are encoded in // a separate hidden image item. The two are combined at decoding into one // image with the same bit depth as the original image. It is backward // compatible in the sense that it is possible to decode only the base image // (ignoring the hidden image item), leading to a valid image but with // precision loss (16-bit samples truncated to the 12 most significant // bits). AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_12B_4B, // Encode the 12 most significant bits of each input image sample lossily or // losslessly into a base image. The difference between the original and // decoded values of these samples is encoded as a separate 8-bit hidden // image item. The two are combined at decoding into one image with the same // bit depth as the original image. It is backward compatible in the sense // that it is possible to decode only the base image (ignoring the hidden // image item), leading to a valid image but with loss due to precision // truncation and/or compression. AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_12B_8B_OVERLAP_4B } avifSampleTransformRecipe; #endif // AVIF_ENABLE_EXPERIMENTAL_SAMPLE_TRANSFORM // --------------------------------------------------------------------------- // avifImage // NOTE: The avifImage struct may be extended in a future release. Code outside the libavif library // must allocate avifImage by calling the avifImageCreate() or avifImageCreateEmpty() function. avifImage; // avifImageCreate() and avifImageCreateEmpty() return NULL if arguments are invalid or if a memory allocation failed. AVIF_NODISCARD AVIF_API avifImage * avifImageCreate(uint32_t width, uint32_t height, uint32_t depth, avifPixelFormat yuvFormat); AVIF_NODISCARD AVIF_API avifImage * avifImageCreateEmpty(void); // helper for making an image to decode into // Performs a deep copy of an image, including all metadata and planes, and the gain map metadata/planes if present // and if AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP is defined. AVIF_API avifResult avifImageCopy(avifImage * dstImage, const avifImage * srcImage, avifPlanesFlags planes); // Performs a shallow copy of a rectangular area of an image. 'dstImage' does not own the planes. // Ignores the gainMap field (which exists only if AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP is defined). AVIF_API avifResult avifImageSetViewRect(avifImage * dstImage, const avifImage * srcImage, const avifCropRect * rect); AVIF_API void avifImageDestroy(avifImage * image); AVIF_API avifResult avifImageSetProfileICC(avifImage * image, const uint8_t * icc, size_t iccSize); // Sets Exif metadata. Attempts to parse the Exif metadata for Exif orientation. Sets // image->transformFlags, image->irot and image->imir if the Exif metadata is parsed successfully, // otherwise leaves image->transformFlags, image->irot and image->imir unchanged. // Warning: If the Exif payload is set and invalid, avifEncoderWrite() may return AVIF_RESULT_INVALID_EXIF_PAYLOAD. AVIF_API avifResult avifImageSetMetadataExif(avifImage * image, const uint8_t * exif, size_t exifSize); // Sets XMP metadata. AVIF_API avifResult avifImageSetMetadataXMP(avifImage * image, const uint8_t * xmp, size_t xmpSize); // Allocate/free/steal planes. These functions ignore the gainMap field (which exists only if // AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP is defined). AVIF_API avifResult avifImageAllocatePlanes(avifImage * image, avifPlanesFlags planes); // Ignores any pre-existing planes AVIF_API void avifImageFreePlanes(avifImage * image, avifPlanesFlags planes); // Ignores already-freed planes AVIF_API void avifImageStealPlanes(avifImage * dstImage, avifImage * srcImage, avifPlanesFlags planes); // --------------------------------------------------------------------------- // Understanding maxThreads // // libavif's structures and API use the setting 'maxThreads' in a few places. The intent of this // setting is to limit concurrent thread activity/usage, not necessarily to put a hard ceiling on // how many sleeping threads happen to exist behind the scenes. The goal of this setting is to // ensure that at any given point during libavif's encoding or decoding, no more than *maxThreads* // threads are simultaneously **active and taking CPU time**. // // As an important example, when encoding an image sequence that has an alpha channel, two // long-lived underlying AV1 encoders must simultaneously exist (one for color, one for alpha). For // each additional frame fed into libavif, its YUV planes are fed into one instance of the AV1 // encoder, and its alpha plane is fed into another. These operations happen serially, so only one // of these AV1 encoders is ever active at a time. However, the AV1 encoders might pre-create a // pool of worker threads upon initialization, so during this process, twice the amount of worker // threads actually simultaneously exist on the machine, but half of them are guaranteed to be // sleeping. // // This design ensures that AV1 implementations are given as many threads as possible to ensure a // speedy encode or decode, despite the complexities of occasionally needing two AV1 codec instances // (due to alpha payloads being separate from color payloads). If your system has a hard ceiling on // the number of threads that can ever be in flight at a given time, please account for this // accordingly. // --------------------------------------------------------------------------- // Scaling // Scales the YUV/A planes in-place. dstWidth and dstHeight must both be <= AVIF_DEFAULT_IMAGE_DIMENSION_LIMIT and // dstWidth*dstHeight should be <= AVIF_DEFAULT_IMAGE_SIZE_LIMIT. AVIF_API avifResult avifImageScale(avifImage * image, uint32_t dstWidth, uint32_t dstHeight, avifDiagnostics * diag); // --------------------------------------------------------------------------- // Optional YUV<->RGB support // To convert to/from RGB, create an avifRGBImage on the stack, call avifRGBImageSetDefaults() on // it, and then tweak the values inside of it accordingly. At a minimum, you should populate // ->pixels and ->rowBytes with an appropriately sized pixel buffer, which should be at least // (->rowBytes * ->height) bytes, where ->rowBytes is at least (->width * avifRGBImagePixelSize()). // If you don't want to supply your own pixel buffer, you can use the // avifRGBImageAllocatePixels()/avifRGBImageFreePixels() convenience functions. // avifImageRGBToYUV() and avifImageYUVToRGB() will perform depth rescaling and limited<->full range // conversion, if necessary. Pixels in an avifRGBImage buffer are always full range, and conversion // routines will fail if the width and height don't match the associated avifImage. // If libavif is built with a version of libyuv offering a fast conversion between RGB and YUV for // the given inputs, libavif will use it. See reformat_libyuv.c for the details. // libyuv is faster but may have slightly less precision than built-in conversion, so avoidLibYUV // can be set to AVIF_TRUE when AVIF_CHROMA_UPSAMPLING_BEST_QUALITY or // AVIF_CHROMA_DOWNSAMPLING_BEST_QUALITY is used, to get the most precise but slowest results. // Note to libavif maintainers: The lookup tables in avifImageYUVToRGBLibYUV // rely on the ordering of this enum values for their correctness. So changing // the values in this enum will require auditing avifImageYUVToRGBLibYUV for // correctness. avifRGBFormat; AVIF_API uint32_t avifRGBFormatChannelCount(avifRGBFormat format); AVIF_API avifBool avifRGBFormatHasAlpha(avifRGBFormat format); avifChromaUpsampling; avifChromaDownsampling; // NOTE: avifRGBImage must be initialized with avifRGBImageSetDefaults() (preferred) or memset() // before use. avifRGBImage; // Sets rgb->width, rgb->height, and rgb->depth to image->width, image->height, and image->depth. // Sets rgb->pixels to NULL and rgb->rowBytes to 0. Sets the other fields of 'rgb' to default // values. AVIF_API void avifRGBImageSetDefaults(avifRGBImage * rgb, const avifImage * image); AVIF_API uint32_t avifRGBImagePixelSize(const avifRGBImage * rgb); // Convenience functions. If you supply your own pixels/rowBytes, you do not need to use these. AVIF_API avifResult avifRGBImageAllocatePixels(avifRGBImage * rgb); AVIF_API void avifRGBImageFreePixels(avifRGBImage * rgb); // The main conversion functions AVIF_API avifResult avifImageRGBToYUV(avifImage * image, const avifRGBImage * rgb); AVIF_API avifResult avifImageYUVToRGB(const avifImage * image, avifRGBImage * rgb); // Premultiply handling functions. // (Un)premultiply is automatically done by the main conversion functions above, // so usually you don't need to call these. They are there for convenience. AVIF_API avifResult avifRGBImagePremultiplyAlpha(avifRGBImage * rgb); AVIF_API avifResult avifRGBImageUnpremultiplyAlpha(avifRGBImage * rgb); // --------------------------------------------------------------------------- // YUV Utils AVIF_API int avifFullToLimitedY(uint32_t depth, int v); AVIF_API int avifFullToLimitedUV(uint32_t depth, int v); AVIF_API int avifLimitedToFullY(uint32_t depth, int v); AVIF_API int avifLimitedToFullUV(uint32_t depth, int v); // --------------------------------------------------------------------------- // Codec selection avifCodecChoice; avifCodecFlag; avifCodecFlags; // If this returns NULL, the codec choice/flag combination is unavailable AVIF_API const char * avifCodecName(avifCodecChoice choice, avifCodecFlags requiredFlags); AVIF_API avifCodecChoice avifCodecChoiceFromName(const char * name); // --------------------------------------------------------------------------- // avifIO struct avifIO; // Destroy must completely destroy all child structures *and* free the avifIO object itself. // This function pointer is optional, however, if the avifIO object isn't intended to be owned by // a libavif encoder/decoder. avifIODestroyFunc; // This function should return a block of memory that *must* remain valid until another read call to // this avifIO struct is made (reusing a read buffer is acceptable/expected). // // * If offset exceeds the size of the content (past EOF), return AVIF_RESULT_IO_ERROR. // * If offset is *exactly* at EOF, provide a 0-byte buffer and return AVIF_RESULT_OK. // * If (offset+size) exceeds the contents' size, it must truncate the range to provide all // bytes from the offset to EOF. // * If the range is unavailable yet (due to network conditions or any other reason), // return AVIF_RESULT_WAITING_ON_IO. // * Otherwise, provide the range and return AVIF_RESULT_OK. avifIOReadFunc; avifIOWriteFunc; avifIO; // Returns NULL if the reader cannot be allocated. AVIF_API avifIO * avifIOCreateMemoryReader(const uint8_t * data, size_t size); // Returns NULL if the file cannot be opened or if the reader cannot be allocated. AVIF_API avifIO * avifIOCreateFileReader(const char * filename); AVIF_API void avifIODestroy(avifIO * io); // --------------------------------------------------------------------------- // avifDecoder // Some encoders (including very old versions of avifenc) do not implement the AVIF standard // perfectly, and thus create invalid files. However, these files are likely still recoverable / // decodable, if it wasn't for the strict requirements imposed by libavif's decoder. These flags // allow a user of avifDecoder to decide what level of strictness they want in their project. avifStrictFlag; avifStrictFlags; // Useful stats related to a read/write avifIOStats; struct avifDecoderData; avifDecoderSource; // Information about the timing of a single image in an image sequence avifImageTiming; avifProgressiveState; AVIF_API const char * avifProgressiveStateToString(avifProgressiveState progressiveState); // NOTE: The avifDecoder struct may be extended in a future release. Code outside the libavif // library must allocate avifDecoder by calling the avifDecoderCreate() function. avifDecoder; // Returns NULL in case of memory allocation failure. AVIF_API avifDecoder * avifDecoderCreate(void); AVIF_API void avifDecoderDestroy(avifDecoder * decoder); // Simple interfaces to decode a single image, independent of the decoder afterwards (decoder may be destroyed). AVIF_API avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image); // call avifDecoderSetIO*() first AVIF_API avifResult avifDecoderReadMemory(avifDecoder * decoder, avifImage * image, const uint8_t * data, size_t size); AVIF_API avifResult avifDecoderReadFile(avifDecoder * decoder, avifImage * image, const char * filename); // Multi-function alternative to avifDecoderRead() for image sequences and gaining direct access // to the decoder's YUV buffers (for performance's sake). Data passed into avifDecoderParse() is NOT // copied, so it must continue to exist until the decoder is destroyed. // // Usage / function call order is: // * avifDecoderCreate() // * avifDecoderSetSource() - optional, the default (AVIF_DECODER_SOURCE_AUTO) is usually sufficient // * avifDecoderSetIO*() // * avifDecoderParse() // * avifDecoderNextImage() - in a loop, using decoder->image after each successful call // * avifDecoderDestroy() // // NOTE: Until avifDecoderParse() returns AVIF_RESULT_OK, no data in avifDecoder should // be considered valid, and no queries (such as Keyframe/Timing/MaxExtent) should be made. // // You can use avifDecoderReset() any time after a successful call to avifDecoderParse() // to reset the internal decoder back to before the first frame. Calling either // avifDecoderSetSource() or avifDecoderParse() will automatically Reset the decoder. // // avifDecoderSetSource() allows you not only to choose whether to parse tracks or // items in a file containing both, but switch between sources without having to // Parse again. Normally AVIF_DECODER_SOURCE_AUTO is enough for the common path. AVIF_API avifResult avifDecoderSetSource(avifDecoder * decoder, avifDecoderSource source); // Note: When avifDecoderSetIO() is called, whether 'decoder' takes ownership of 'io' depends on // whether io->destroy is set. avifDecoderDestroy(decoder) calls avifIODestroy(io), which calls // io->destroy(io) if io->destroy is set. Therefore, if io->destroy is not set, then // avifDecoderDestroy(decoder) has no effects on 'io'. AVIF_API void avifDecoderSetIO(avifDecoder * decoder, avifIO * io); AVIF_API avifResult avifDecoderSetIOMemory(avifDecoder * decoder, const uint8_t * data, size_t size); AVIF_API avifResult avifDecoderSetIOFile(avifDecoder * decoder, const char * filename); AVIF_API avifResult avifDecoderParse(avifDecoder * decoder); AVIF_API avifResult avifDecoderNextImage(avifDecoder * decoder); AVIF_API avifResult avifDecoderNthImage(avifDecoder * decoder, uint32_t frameIndex); AVIF_API avifResult avifDecoderReset(avifDecoder * decoder); // Keyframe information // frameIndex - 0-based, matching avifDecoder->imageIndex, bound by avifDecoder->imageCount // "nearest" keyframe means the keyframe prior to this frame index (returns frameIndex if it is a keyframe) // These functions may be used after a successful call (AVIF_RESULT_OK) to avifDecoderParse(). AVIF_NODISCARD AVIF_API avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex); AVIF_API uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex); // Timing helper - This does not change the current image or invoke the codec (safe to call repeatedly) // This function may be used after a successful call (AVIF_RESULT_OK) to avifDecoderParse(). AVIF_API avifResult avifDecoderNthImageTiming(const avifDecoder * decoder, uint32_t frameIndex, avifImageTiming * outTiming); // When avifDecoderNextImage() or avifDecoderNthImage() returns AVIF_RESULT_WAITING_ON_IO, this // function can be called next to retrieve the number of top rows that can be immediately accessed // from the luma plane of decoder->image, and alpha if any. The corresponding rows from the chroma planes, // if any, can also be accessed (half rounded up if subsampled, same number of rows otherwise). // If a gain map is present and AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP is on and enableDecodingGainMap is also on, // the gain map's planes can also be accessed in the same way. If the gain map's height is different from // the main image, then the number of available gain map rows is at least: // roundf((float)decoded_row_count / decoder->image->height * decoder->image->gainMap.image->height) // When gain map scaling is needed, callers might choose to use a few less rows depending on how many rows // are needed by the scaling algorithm, to avoid the last row(s) changing when more data becomes available. // decoder->allowIncremental must be set to true before calling avifDecoderNextImage() or // avifDecoderNthImage(). Returns decoder->image->height when the last call to avifDecoderNextImage() or // avifDecoderNthImage() returned AVIF_RESULT_OK. Returns 0 in all other cases. // WARNING: Experimental feature. AVIF_API uint32_t avifDecoderDecodedRowCount(const avifDecoder * decoder); // --------------------------------------------------------------------------- // avifExtent avifExtent; // Streaming data helper - Use this to calculate the maximal AVIF data extent encompassing all AV1 // sample data needed to decode the Nth image. The offset will be the earliest offset of all // required AV1 extents for this frame, and the size will create a range including the last byte of // the last AV1 sample needed. Note that this extent may include non-sample data, as a frame's // sample data may be broken into multiple extents and interleaved with other data, or in // non-sequential order. This extent will also encompass all AV1 samples that this frame's sample // depends on to decode (such as samples for reference frames), from the nearest keyframe up to this // Nth frame. // // If avifDecoderNthImageMaxExtent() returns AVIF_RESULT_OK and the extent's size is 0 bytes, this // signals that libavif doesn't expect to call avifIO's Read for this frame's decode. This happens if // data for this frame was read as a part of avifDecoderParse() (typically in an idat box inside of // a meta box). // // This function may be used after a successful call (AVIF_RESULT_OK) to avifDecoderParse(). AVIF_API avifResult avifDecoderNthImageMaxExtent(const avifDecoder * decoder, uint32_t frameIndex, avifExtent * outExtent); // --------------------------------------------------------------------------- // avifEncoder struct avifEncoderData; struct avifCodecSpecificOptions; avifScalingMode; // Notes: // * The avifEncoder struct may be extended in a future release. Code outside the libavif library // must allocate avifEncoder by calling the avifEncoderCreate() function. // * If avifEncoderWrite() returns AVIF_RESULT_OK, output must be freed with avifRWDataFree() // * If (maxThreads < 2), multithreading is disabled // * NOTE: Please see the "Understanding maxThreads" comment block above // * Quality range: [AVIF_QUALITY_WORST - AVIF_QUALITY_BEST] // * Quantizer range: [AVIF_QUANTIZER_BEST_QUALITY - AVIF_QUANTIZER_WORST_QUALITY] // * In older versions of libavif, the avifEncoder struct doesn't have the quality and qualityAlpha // fields. For backward compatibility, if the quality field is not set, the default value of // quality is based on the average of minQuantizer and maxQuantizer. Similarly the default value // of qualityAlpha is based on the average of minQuantizerAlpha and maxQuantizerAlpha. New code // should set quality and qualityAlpha and leave minQuantizer, maxQuantizer, minQuantizerAlpha, // and maxQuantizerAlpha initialized to their default values. // * To enable tiling, set tileRowsLog2 > 0 and/or tileColsLog2 > 0. // Tiling values range [0-6], where the value indicates a request for 2^n tiles in that dimension. // If autoTiling is set to AVIF_TRUE, libavif ignores tileRowsLog2 and tileColsLog2 and // automatically chooses suitable tiling values. // * Speed range: [AVIF_SPEED_SLOWEST - AVIF_SPEED_FASTEST]. Slower should make for a better quality // image in less bytes. AVIF_SPEED_DEFAULT means "Leave the AV1 codec to its default speed settings"./ // If avifEncoder uses rav1e, the speed value is directly passed through (0-10). If libaom is used, // a combination of settings are tweaked to simulate this speed range. // * Extra layer count: [0 - (AVIF_MAX_AV1_LAYER_COUNT-1)]. Non-zero value indicates a layered // (progressive) image. // * Some encoder settings can be changed after encoding starts. Changes will take effect in the next // call to avifEncoderAddImage(). avifEncoder; // avifEncoderCreate() returns NULL if a memory allocation failed. AVIF_NODISCARD AVIF_API avifEncoder * avifEncoderCreate(void); AVIF_API avifResult avifEncoderWrite(avifEncoder * encoder, const avifImage * image, avifRWData * output); AVIF_API void avifEncoderDestroy(avifEncoder * encoder); avifAddImageFlag; avifAddImageFlags; // Multi-function alternative to avifEncoderWrite() for advanced features. // // Usage / function call order is: // * avifEncoderCreate() // - Still image: // * avifEncoderAddImage() [exactly once] // - Still image grid: // * avifEncoderAddImageGrid() [exactly once, AVIF_ADD_IMAGE_FLAG_SINGLE is assumed] // - Image sequence: // * Set encoder->timescale (Hz) correctly // * avifEncoderAddImage() ... [repeatedly; at least once] // - Still layered image: // * Set encoder->extraLayerCount correctly // * avifEncoderAddImage() ... [exactly encoder->extraLayerCount+1 times] // - Still layered grid: // * Set encoder->extraLayerCount correctly // * avifEncoderAddImageGrid() ... [exactly encoder->extraLayerCount+1 times] // * avifEncoderFinish() // * avifEncoderDestroy() // // The image passed to avifEncoderAddImage() or avifEncoderAddImageGrid() is encoded during the // call (which may be slow) and can be freed after the function returns. // durationInTimescales is ignored if AVIF_ADD_IMAGE_FLAG_SINGLE is set in addImageFlags, // or if we are encoding a layered image. AVIF_API avifResult avifEncoderAddImage(avifEncoder * encoder, const avifImage * image, uint64_t durationInTimescales, avifAddImageFlags addImageFlags); AVIF_API avifResult avifEncoderAddImageGrid(avifEncoder * encoder, uint32_t gridCols, uint32_t gridRows, const avifImage * const * cellImages, avifAddImageFlags addImageFlags); AVIF_API avifResult avifEncoderFinish(avifEncoder * encoder, avifRWData * output); // Codec-specific, optional "advanced" tuning settings, in the form of string key/value pairs, // to be consumed by the codec in the next avifEncoderAddImage() call. // See the codec documentation to know if a setting is persistent or applied only to the next frame. // key must be non-NULL, but passing a NULL value will delete the pending key, if it exists. // Setting an incorrect or unknown option for the current codec will cause errors of type // AVIF_RESULT_INVALID_CODEC_SPECIFIC_OPTION from avifEncoderWrite() or avifEncoderAddImage(). AVIF_API avifResult avifEncoderSetCodecSpecificOption(avifEncoder * encoder, const char * key, const char * value); #if defined(AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP) // Returns the size in bytes of the AV1 image item containing gain map samples, or 0 if no gain map was encoded. AVIF_API size_t avifEncoderGetGainMapSizeBytes(avifEncoder * encoder); #endif // Helpers AVIF_NODISCARD AVIF_API avifBool avifImageUsesU16(const avifImage * image); AVIF_NODISCARD AVIF_API avifBool avifImageIsOpaque(const avifImage * image); // channel can be an avifChannelIndex. AVIF_API uint8_t * avifImagePlane(const avifImage * image, int channel); AVIF_API uint32_t avifImagePlaneRowBytes(const avifImage * image, int channel); AVIF_API uint32_t avifImagePlaneWidth(const avifImage * image, int channel); AVIF_API uint32_t avifImagePlaneHeight(const avifImage * image, int channel); // Returns AVIF_TRUE if input begins with a valid FileTypeBox (ftyp) that supports // either the brand 'avif' or 'avis' (or both), without performing any allocations. AVIF_NODISCARD AVIF_API avifBool avifPeekCompatibleFileType(const avifROData * input); #if defined(AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP) // --------------------------------------------------------------------------- // Gain Map utilities. // Gain Maps are a HIGHLY EXPERIMENTAL FEATURE, see comments in the avifGainMap // section above. // Performs tone mapping on a base image using the provided gain map. // The HDR headroom is log2 of the ratio of HDR to SDR white brightness of the display to tone map for. // 'toneMappedImage' should have the 'format', 'depth', and 'isFloat' fields set to the desired values. // If non NULL, 'clli' will be filled with the light level information of the tone mapped image. // NOTE: only used in tests for now, might be added to the public API at some point. AVIF_API avifResult avifImageApplyGainMap(const avifImage * baseImage, const avifGainMap * gainMap, float hdrHeadroom, avifColorPrimaries outputColorPrimaries, avifTransferCharacteristics outputTransferCharacteristics, avifRGBImage * toneMappedImage, avifContentLightLevelInformationBox * clli, avifDiagnostics * diag); // Same as above but takes an avifRGBImage as input instead of avifImage. AVIF_API avifResult avifRGBImageApplyGainMap(const avifRGBImage * baseImage, avifColorPrimaries baseColorPrimaries, avifTransferCharacteristics baseTransferCharacteristics, const avifGainMap * gainMap, float hdrHeadroom, avifColorPrimaries outputColorPrimaries, avifTransferCharacteristics outputTransferCharacteristics, avifRGBImage * toneMappedImage, avifContentLightLevelInformationBox * clli, avifDiagnostics * diag); // Computes a gain map between two images: a base image and an alternate image. // Both images should have the same width and height, and use the same color // primaries. TODO(maryla): allow different primaries. // gainMap->image should be initialized with avifImageCreate(), with the width, // height, depth and yuvFormat fields set to the desired output values for the // gain map. All of these fields may differ from the source images. AVIF_API avifResult avifRGBImageComputeGainMap(const avifRGBImage * baseRgbImage, avifColorPrimaries baseColorPrimaries, avifTransferCharacteristics baseTransferCharacteristics, const avifRGBImage * altRgbImage, avifColorPrimaries altColorPrimaries, avifTransferCharacteristics altTransferCharacteristics, avifGainMap * gainMap, avifDiagnostics * diag); // Convenience function. Same as above but takes avifImage images as input // instead of avifRGBImage. Gain map computation is performed in RGB space so // the images are converted to RGB first. AVIF_API avifResult avifImageComputeGainMap(const avifImage * baseImage, const avifImage * altImage, avifGainMap * gainMap, avifDiagnostics * diag); #endif // AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP #ifdef __cplusplus } // extern "C" #endif #endif // ifndef AVIF_AVIF_H