8 releases (1 stable)

1.0.0 Jan 25, 2022
0.13.2 Jun 2, 2021
0.13.1 Jan 29, 2021
0.13.0 Sep 6, 2020
0.12.3 Aug 29, 2020

#898 in Images

Download history 236/week @ 2023-12-15 128/week @ 2023-12-22 74/week @ 2023-12-29 117/week @ 2024-01-05 154/week @ 2024-01-12 126/week @ 2024-01-19 155/week @ 2024-01-26 87/week @ 2024-02-02 133/week @ 2024-02-09 202/week @ 2024-02-16 261/week @ 2024-02-23 208/week @ 2024-03-01 168/week @ 2024-03-08 108/week @ 2024-03-15 166/week @ 2024-03-22 210/week @ 2024-03-29

672 downloads per month
Used in 7 crates (5 directly)

MPL-2.0 license

58KB
1.5K SLoC

AVIF file structure parser (demuxer)

Get AV1 payload and alpha channel metadata out of AVIF image files. The parser is a fork of Mozilla's MP4 parser used in Firefox, so it's designed to be robust and safely handle untrusted data.

The parser is compatible with files supported by libavif, Chrome 85 and Firefox 81a.

API documentation

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

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.45 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

~520KB