Convert Figma logo to code with AI

AOMediaCodec logolibavif

libavif - Library for encoding and decoding .avif files

1,519
194
1,519
128

Top Related Projects

1,683

libheif is an HEIF and AVIF file format decoder and encoder.

3,679

The fastest and safest AV1 encoder.

4,479

Perceptual video quality assessment based on multi-method fusion.

21,699

Make images smaller using best-in-class codecs, right in the browser.

Quick Overview

The AOMediaCodec/libavif repository is a C library that provides an implementation of the AV1 Image File Format (AVIF), a modern image format based on the AV1 video codec. The library aims to provide a simple and efficient way to work with AVIF images, including encoding, decoding, and metadata manipulation.

Pros

  • Efficient Compression: AVIF offers significantly better compression compared to traditional image formats like JPEG, resulting in smaller file sizes without sacrificing image quality.
  • Wide Adoption: AVIF is supported by major web browsers and is gaining traction as a modern image format for the web and other applications.
  • Flexible Metadata: The AVIF format supports a wide range of metadata, including ICC color profiles, EXIF data, and more.
  • Cross-Platform Compatibility: The libavif library is designed to be cross-platform, with support for various operating systems and architectures.

Cons

  • Limited Browser Support: While AVIF is gaining traction, it is not yet universally supported by all web browsers, which can limit its adoption in some web-based applications.
  • Complexity: Implementing AVIF support can be more complex than working with simpler image formats like JPEG or PNG, especially for developers new to the format.
  • Performance Overhead: Encoding and decoding AVIF images can be more computationally intensive than other image formats, which may impact performance in some use cases.
  • Licensing Considerations: The AV1 codec, which is the basis for the AVIF format, has some licensing and patent considerations that developers should be aware of.

Code Examples

Here are a few examples of how to use the libavif library:

  1. Decoding an AVIF Image:
#include <avif/avif.h>

avifImage* image = avifImageCreateEmpty();
avifDecoder* decoder = avifDecoderCreate();
avifResult result = avifDecoderReadImage(decoder, image);
if (result == AVIF_RESULT_OK) {
    // The image has been successfully decoded and can be accessed through the `image` object
}
avifImageDestroy(image);
avifDecoderDestroy(decoder);
  1. Encoding an AVIF Image:
#include <avif/avif.h>

avifImage* image = avifImageCreate(width, height, 8, AVIF_PIXEL_FORMAT_YUV420);
// Set image data and metadata
avifEncoder* encoder = avifEncoderCreate();
encoder->speed = AVIF_SPEED_SLOWEST;
encoder->quality = 50;
avifResult result = avifEncoderWrite(encoder, image, &data, &size);
if (result == AVIF_RESULT_OK) {
    // The AVIF image data is available in the `data` buffer
}
avifImageDestroy(image);
avifEncoderDestroy(encoder);
  1. Extracting Metadata from an AVIF Image:
#include <avif/avif.h>

avifImage* image = avifImageCreateEmpty();
avifDecoder* decoder = avifDecoderCreate();
avifResult result = avifDecoderReadImage(decoder, image);
if (result == AVIF_RESULT_OK) {
    // Access metadata properties of the image, such as:
    printf("Image width: %d\n", image->width);
    printf("Image height: %d\n", image->height);
    printf("Bit depth: %d\n", image->depth);
    printf("Color profile: %s\n", image->colorProfile.data);
}
avifImageDestroy(image);
avifDecoderDestroy(decoder);

Getting Started

To get started with the libavif library, follow these steps:

  1. Clone the repository:
git clone https://github.com/AOMediaCodec/libavif.git
  1. Build the library:
cd libavif
mkdir build
cd build
cmake ..
make
  1. Install the library:
sudo make install

Competitor Comparisons

1,683

libheif is an HEIF and AVIF file format decoder and encoder.

Pros of libheif

  • Supports both HEIF and AVIF formats, offering more flexibility
  • More mature project with longer development history
  • Includes a simple API for encoding and decoding images

Cons of libheif

  • Larger codebase, potentially more complex to maintain
  • May have slower performance for AVIF-specific operations
  • Requires additional dependencies for full functionality

Code Comparison

libheif:

heif_context* ctx = heif_context_alloc();
heif_context_read_from_file(ctx, input_filename, nullptr);
heif_image_handle* handle;
heif_context_get_primary_image_handle(ctx, &handle);
heif_image* img;
heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_RGB, nullptr);

libavif:

avifDecoder * decoder = avifDecoderCreate();
avifResult result = avifDecoderSetIOFile(decoder, input_filename);
result = avifDecoderParse(decoder);
avifImage * image = avifImageCreateEmpty();
result = avifDecoderNextImage(decoder);
avifRGBImage rgb;
avifRGBImageSetDefaults(&rgb, image);
avifImageYUVToRGB(image, &rgb);

Both libraries provide similar functionality for decoding images, but libavif focuses specifically on AVIF format, while libheif supports multiple formats. The APIs differ in structure and naming conventions, but the overall process is comparable.

3,679

The fastest and safest AV1 encoder.

Pros of rav1e

  • Written in Rust, offering memory safety and performance benefits
  • Focused specifically on AV1 encoding, potentially leading to optimized performance
  • Active development with frequent updates and improvements

Cons of rav1e

  • More limited in scope compared to libavif's broader AVIF support
  • May have a steeper learning curve for developers not familiar with Rust
  • Potentially less mature ecosystem and tooling compared to libavif

Code Comparison

rav1e (Rust):

use rav1e::prelude::*;

let cfg = Config::default();
let mut ctx: Context<u8> = cfg.new_context().unwrap();
let frame = ctx.new_frame();
ctx.send_frame(frame).unwrap();
let packet = ctx.receive_packet().unwrap();

libavif (C):

#include "avif/avif.h"

avifEncoder * encoder = avifEncoderCreate();
avifRWData avifOutput = AVIF_DATA_EMPTY;
avifImage * image = avifImageCreate(width, height, depth, format);
avifResult result = avifEncoderWrite(encoder, image, &avifOutput);

Both libraries provide encoding capabilities for AV1-based formats, but rav1e focuses on AV1 encoding while libavif offers broader AVIF support. rav1e's Rust implementation may provide better memory safety, while libavif's C implementation might be more widely compatible with existing codebases.

4,479

Perceptual video quality assessment based on multi-method fusion.

Pros of VMAF

  • Focused on video quality assessment, providing a perceptual metric
  • Includes pre-trained models for different use cases
  • Widely adopted in the streaming industry, particularly by Netflix

Cons of VMAF

  • Limited to quality assessment, not an image/video codec like libavif
  • Requires more computational resources for analysis
  • May not be as suitable for real-time applications

Code Comparison

VMAF (Python):

import vmaf

model = vmaf.VmafModel()
score = model.compute_score(reference_video, distorted_video)

libavif (C):

avifImage *image = avifImageCreate(width, height, depth, format);
avifEncoder *encoder = avifEncoderCreate();
avifResult result = avifEncoderWrite(encoder, image, &raw);

Key Differences

  • Purpose: VMAF is for video quality assessment, while libavif is an AV1 Image File Format library
  • Language: VMAF is primarily Python-based, libavif is C-based
  • Functionality: VMAF analyzes video quality, libavif encodes/decodes AVIF images
  • Use cases: VMAF is used for streaming optimization, libavif for next-gen image compression

Both projects serve different purposes in the multimedia ecosystem, with VMAF focusing on quality metrics and libavif on image format implementation.

21,699

Make images smaller using best-in-class codecs, right in the browser.

Pros of Squoosh

  • User-friendly web interface for image compression and conversion
  • Supports multiple image formats and compression algorithms
  • Provides real-time preview and comparison of compressed images

Cons of Squoosh

  • Limited to browser-based usage, not suitable for server-side or command-line operations
  • Fewer advanced options and customization compared to libavif
  • Primarily focused on image compression, while libavif specializes in AVIF format

Code Comparison

libavif (C):

avifResult result = avifEncoderWrite(encoder, image, &raw);
if (result != AVIF_RESULT_OK) {
    fprintf(stderr, "Failed to encode: %s\n", avifResultToString(result));
    return 1;
}

Squoosh (JavaScript):

const compressedImage = await imagePool.ingestImage(imageFile);
await compressedImage.encode({
  webp: {},
  mozjpeg: {},
});

The code snippets demonstrate the core functionality of each project. libavif focuses on AVIF encoding, while Squoosh provides a more general-purpose image compression interface supporting multiple formats.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

libavif AppVeyor Build Status Travis Build Status

This library aims to be a friendly, portable C implementation of the AV1 Image File Format, as described here:

https://aomediacodec.github.io/av1-avif/

It can encode and decode all AV1 supported YUV formats and bit depths (with alpha).

It is recommended that you check out/use tagged releases instead of just using the main branch. We will regularly create new versions as bug fixes and features are added.

Usage

Please see the examples in the "examples" directory. If you're already building libavif, enable the CMake option AVIF_BUILD_EXAMPLES in order to build and run the examples too.

Installation

libavif is a package in most major OSs.

Windows

vcpkg install libavif

You can also download the official windows binaries on the release page.

macOS

Homebrew:

brew install libavif

MacPorts:

sudo port install libavif

Linux

Debian-based distributions:

sudo apt install libavif-dev

Red Hat-based distributions:

sudo yum -y install libavif

MinGW

For the "default" MSYS2 UCRT64 environment:

pacman -S mingw-w64-ucrt-x86_64-libavif

Build Notes

Building libavif requires CMake.

No AV1 codecs are enabled by default. Enable them by setting any of the following CMake options to LOCAL or SYSTEM whether you want to use a locally built or a system installed version (e.g. -DAVIF_CODEC_AOM=LOCAL):

  • AVIF_CODEC_AOM for libaom (encoder and decoder)
  • AVIF_CODEC_DAV1D for dav1d (decoder)
  • AVIF_CODEC_LIBGAV1 for libgav1 (decoder)
  • AVIF_CODEC_RAV1E for rav1e (encoder)
  • AVIF_CODEC_SVT for SVT-AV1 (encoder)

When set to SYSTEM, these libraries (in their C API form) must be externally available (discoverable via CMake's FIND_LIBRARY) to use them, or if libavif is a child CMake project, the appropriate CMake target must already exist by the time libavif's CMake scripts are executed.

Local / Static Builds

The ext/ subdirectory contains a handful of basic scripts which each pull down a known-good copy of an AV1 codec and make a local static library build. Most scripts require CMake, Ninja and NASM. dav1d uses Meson instead of CMake, and rav1e uses cargo (Rust). Check each library's documentation for an exact list of requirements.

If you want to statically link any codec into your local (static) build of libavif, building using one of these scripts and then setting the associated AVIF_CODEC_* to LOCAL is a convenient method, but you must make sure to disable BUILD_SHARED_LIBS in CMake to instruct it to make a static libavif library.

If you want to build/install shared libraries for AV1 codecs, you can still peek inside of each script to see where the current known-good SHA is for each codec.

Tests

A few tests written in C can be built by enabling the AVIF_BUILD_TESTS CMake option.

The remaining tests can be built by enabling the AVIF_BUILD_TESTS and AVIF_ENABLE_GTEST CMake options. They require GoogleTest to be built locally with ext/googletest.cmd or installed on the system.

Command Lines

The following instructions can be used to build the libavif library and the avifenc and avifdec tools.

Build using installed dependencies

To link against the already installed aom, libjpeg and libpng dependency libraries (recommended):

git clone -b v1.0.3 https://github.com/AOMediaCodec/libavif.git
cd libavif
cmake -S . -B build -DAVIF_CODEC_AOM=SYSTEM -DAVIF_BUILD_APPS=ON
cmake --build build --parallel

Build everything from scratch

For development and debugging purposes, or to generate fully static binaries:

git clone -b v1.0.3 https://github.com/AOMediaCodec/libavif.git
cd libavif/ext
./aom.cmd
./libyuv.cmd
./libsharpyuv.cmd
./libjpeg.cmd
./zlibpng.cmd
cd ..
cmake -S . -B build -DBUILD_SHARED_LIBS=OFF -DAVIF_CODEC_AOM=LOCAL -DAVIF_LIBYUV=LOCAL -DAVIF_LIBSHARPYUV=LOCAL -DAVIF_JPEG=LOCAL -DAVIF_ZLIBPNG=LOCAL -DAVIF_BUILD_APPS=ON
cmake --build build --parallel

Prebuilt Library (Windows)

If you're building on Windows with Visual Studio 2022 and want to try out libavif without going through the build process, static library builds for both Debug and Release are available on AppVeyor.

Development Notes

Please check the wiki for extra resources on libavif, such as the Release Checklist.

The libavif library is written in C99. Most of the tests are written in C++14.

Formatting

Use clang-format to format the C sources from the top-level folder (clang-format-16 preferred):

clang-format -style=file -i \
  apps/*.c apps/*/*.c apps/*/*.cc apps/*/*.h examples/*.c \
  include/avif/*.h src/*.c src/*.cc \
  tests/*.c tests/*/*.cc tests/*/*.h

Use cmake-format to format the CMakeLists.txt files from the top-level folder:

cmake-format -i \
  CMakeLists.txt \
  tests/CMakeLists.txt \
  cmake/Modules/*.cmake \
  contrib/CMakeLists.txt \
  contrib/gdk-pixbuf/CMakeLists.txt \
  android_jni/avifandroidjni/src/main/jni/CMakeLists.txt

License

Released under the BSD License.

Copyright 2019 Joe Drago. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.