Skip to main content

otf_pixels_codec_avif/
lib.rs

1//! AVIF codec for `otf-pixels`, implemented from scratch.
2//!
3//! AVIF is two specifications stacked: an ISOBMFF/HEIF container (ISO/IEC
4//! 23008-12) holding one or more items, and an AV1 bitstream (AOM AV1) coding
5//! the pixels. This crate owns both — see ADR-0013, which reverses ADR-0004's
6//! decision to wrap the dav1d/rav1e family.
7//!
8//! # Scope
9//!
10//! Still images only: an AVIF still is an AV1 **key frame**, which removes
11//! inter prediction, reference frame management, motion vectors and compound
12//! modes — well over half of AV1's decoder surface. AVIF image *sequences*
13//! (the `avis` brand, carrying `moov`/`trak`) are animation and therefore v2
14//! per ROADMAP §v2; a sequence decodes its primary item and nothing else,
15//! matching what GIF and WebP already do.
16//!
17//! # Encoding
18//!
19//! [`AvifEncoder`] writes an 8-bit 4:2:0 (or monochrome) key frame, with an
20//! auxiliary alpha item for transparency. It is the tile decoder driven by
21//! decisions: every symbol site asks a coder, which either reads the stream
22//! or decides and writes it, so prediction, reconstruction and probability
23//! adaptation are the decoder's own and cannot drift from it.
24//!
25//! # Memory
26//!
27//! Internally buffered, as SPEC §Formats says. The container addresses its
28//! payload by absolute file offset through `iloc`, so the bytes must be
29//! resident before any of them can be interpreted — there is no prefix of an
30//! AVIF that yields a finished row. The external contract stays streaming
31//! (ADR-0005): the codec buffers, the caller does not.
32//!
33//! # Safety
34//!
35//! Every parser here reads attacker-controlled bytes. Malformed input is a
36//! value, never a panic: `unsafe_code = "forbid"` and the workspace ban on
37//! `unwrap`/`expect`/`panic!` mean the classic container failures — a box
38//! declaring more bytes than its parent holds, an item extent pointing outside
39//! the file, a `grid` whose tiles do not tile — are rejected rather than
40//! trusted.
41
42mod av1;
43mod boxes;
44mod decoder;
45mod encoder;
46mod meta;
47mod props;
48mod yuv;
49
50pub use av1::cdf;
51pub use av1::{
52    BLOCK_4X4, BitReader, Cdef, CoeffBlock, CoeffCdfs, ColorConfig, DecodedFrame, Dequant,
53    FilmGrain, FrameHeader, IntraMode, IntraTxSet, IntraTxTypeCdfs, LoopFilter, LoopRestoration,
54    Neighbours, Obu, ObuHeader, ObuType, OperatingPoint, Plane, PredBlock, Quantization, Residual,
55    Segmentation, SequenceHeader, StillPicture, SymbolDecoder, TileInfo, TxDepthCdfs, TxMode,
56    TxSize, TxSizeParams, TxType, TxTypeCtx, ac_q, add_residual, add_residual_4x4,
57    block_size_from_4x4, chroma_tx_type, dc_q, decode_coeffs, decode_still, dequantize,
58    dequantize_with_matrix, floor_log2, intra_dir, intra_tx_set, inverse_transform_2d,
59    is_tx_type_in_set_intra, max_tx_depth, max_tx_size_rect, mode_to_txfm, predict_intra_4x4,
60    predict_intra_block, quantizer_matrix, read_transform_type, read_tx_size,
61    sequence_header_from_config, split_tx_size, tx_depth_ctx,
62};
63pub use boxes::{BoxHeader, FourCc, Reader};
64pub use decoder::{AvifCodec, AvifDecoder, AvifInfo, probe};
65pub use encoder::AvifEncoder;
66pub use meta::{Construction, Extent, Item, Meta, Reference, URN_ALPHA, URN_ALPHA_LEGACY};
67pub use props::{
68    Association, Av1Config, Colour, Extents, PixelInfo, Properties, Property, Subsampling,
69};
70
71/// The box type that identifies a file's brands, at offset 4 of every ISOBMFF
72/// file.
73pub const SIGNATURE_FTYP: [u8; 4] = *b"ftyp";
74
75/// Brands that mean "this file holds an AVIF still image".
76///
77/// A file is claimed if any of these appears as the major brand or among the
78/// compatible brands. `avif` is the still-image brand proper; `mif1` is the
79/// HEIF image-file brand that many encoders write as the major brand with
80/// `avif` only in the compatible list; `miaf` is the MIAF profile brand; and
81/// `MA1A`/`MA1B` are the MIAF AVIF baseline and advanced profiles.
82pub const BRANDS_STILL: [[u8; 4]; 5] = [*b"avif", *b"mif1", *b"miaf", *b"MA1A", *b"MA1B"];
83
84/// The brand for an AVIF image sequence.
85///
86/// Recognised so that sniffing claims the file and the decoder can report what
87/// it found, rather than leaving an animation to be mis-sniffed as something
88/// else entirely.
89pub const BRAND_SEQUENCE: [u8; 4] = *b"avis";