13 releases (6 stable)

new 1.3.2 Nov 7, 2024
1.3.0 Oct 17, 2024
1.0.0 Jan 25, 2022
0.13.2 Jun 2, 2021
0.12.3 Aug 29, 2020

#769 in Images

Download history 225/week @ 2024-07-18 223/week @ 2024-07-25 267/week @ 2024-08-01 203/week @ 2024-08-08 170/week @ 2024-08-15 162/week @ 2024-08-22 311/week @ 2024-08-29 207/week @ 2024-09-05 196/week @ 2024-09-12 260/week @ 2024-09-19 190/week @ 2024-09-26 209/week @ 2024-10-03 526/week @ 2024-10-10 571/week @ 2024-10-17 284/week @ 2024-10-24 163/week @ 2024-10-31

1,596 downloads per month
Used in 7 crates (5 directly)

MPL-2.0 license

89KB
2K SLoC

AVIF file structure parser (demuxer)

Get AV1 payload and the alpha channel metadata out of AVIF image files.

This is a minimal ISOBMFF/MIAF and AV1 OBU parser. It is a fork of Mozilla's MP4 parser used in Firefox, so it's designed to be robust and safely handle untrusted data. This crate is written entirely in safe Rust code.

The parser is compatible with files supported by libavif, Chrome 85 and Firefox 81a. It parses all files in the AOM test suite.

API documentation

This crate doesn't include an AV1 decoder. To display the pixels you will additionally need dav1d or libaom (full decoder example).

Usage from Rust

It takes io::Read, so you can use any readable input, such as a byte slice (vec.as_slice()), or a File wrapped in BufReader, etc.

let data = read_avif(&mut slice)?;
av1_decode(&data.primary_item)?;
if let Some(alpha) = &data.alpha_item {
    av1_decode(alpha)?;
}
if data.premultiplied_alpha {
    // after decoding, remember to divide R,G,B values by A
}

Usage from C

Install Rust 1.68 or later, preferably via rustup, and run:

cargo build --release

It will build ./target/release/libavif_parse.a (or avif_parse.lib on Windows). Link it with your project.

Cargo supports cross-compilation, so you can easily build it for other platforms (e.g. iOS).

#include "avif_parse.h"
avif_data_t data = avif_parse(file_data, file_length);

if (data) {
    av1_decode(data.primary_data, data.primary_size);
    if (data.alpha_data) {
        av1_decode(data.alpha_data, data.alpha_size);
    }
    if (data.premultiplied_alpha) {
        // after decoding, remember to divide R,G,B values by A
    }
}

avif_data_free(data);

Dependencies

~565KB
~10K SLoC