AV1 Film Grain Synthesis in librav1e Explained
Film grain synthesis is a crucial feature of the AV1 video codec that
preserves the cinematic texture of video while significantly reducing
the required bitrate. This article explains how the
librav1e encoder manages film grain synthesis according to
the AV1 standard, detailing the process of grain analysis, parameter
modeling, and metadata integration into the compressed bitstream.
The Challenge of Film Grain in Video Compression
In traditional video encoding, film grain is treated as high-frequency noise. Because grain is random and changes completely from frame to frame, motion estimation algorithms cannot compress it effectively. Attempting to encode actual grain leads to massive bitrate inflation or unsightly compression artifacts like “smudging” and blocking.
The AV1 standard solves this through Film Grain Synthesis (FGS). Instead of compressing the grain itself, the encoder removes the noise, analyzes its characteristics, transmits a mathematical model of the grain as metadata, and instructs the decoder to regenerate and overlay the grain onto the final image.
How librav1e Implements Film Grain Synthesis
The librav1e encoder (the library interface of the
Rust-based rav1e encoder) implements film grain synthesis
by conforming to the strict syntax and mathematical models defined in
the AV1 specification. The process operates in several distinct
stages:
1. Denoising and Analysis (Pre-processing)
Before the encoding process begins, the source video must be
denoised. librav1e relies on a denoising step—either
internally or through external pre-processors—to separate the “clean”
underlying video from the film grain.
By comparing the original grainy source with the denoised version, the system calculates the statistical properties of the noise. This includes measuring how grain intensity varies across different brightness levels (luma) and color channels (chroma).
2. Parameter Modeling
AV1 uses an autoregressive (AR) model to define film grain.
librav1e maps the analyzed noise into the specific
parameters required by this model. These parameters include: *
Scaling Factors: Dictate the strength/intensity of the
grain based on the local pixel values (e.g., grain is often less visible
in very dark or very bright areas). * Autoregressive
Coefficients: Define the spatial correlation of the grain,
determining whether the grain pattern looks fine, coarse, or clumpy. *
Cross-channel Correlation: Controls how the grain in
the luma (Y) channel affects the grain in the chroma (U and V) channels,
ensuring realistic color noise.
3. Bitstream Integration
Once the mathematical representation of the grain is generated,
librav1e packs these variables into the
film_grain_params() syntax structure.
According to the AV1 standard, these parameters are sent as metadata within the frame headers. Because this metadata only consumes a few dozen bytes per frame—compared to the megabytes required to compress actual grain—this process yields massive savings in bandwidth.
4. Decoder-Side Reconstruction
It is important to note that librav1e does not actually
apply the grain to the video it outputs. Instead, it writes the
instructions into the AV1 bitstream. When an AV1-compliant decoder (such
as dav1d) plays the file, it reads the
film_grain_params, runs the synthesis algorithm, and blends
the mathematically generated grain back onto the decoded, clean frames
in real-time.
Configuring Film Grain in librav1e
Users can leverage film grain synthesis in librav1e by
passing a table of grain parameters (typically generated via tools like
photon noise table or external analysis scripts) to the
encoder configuration. This is done through the API using the
FilmGrainParams struct, allowing developers to fine-tune
the visual appearance of the grain to match the artistic intent of the
original source material.