Premultiplied Alpha in AVIF: Mathematical Process

This article explains the mathematical framework behind premultiplied alpha storage in the AV1 Image File Format (AVIF). It details the equations used to convert between straight and premultiplied color channels, demonstrates how the alpha compositing formula simplifies through premultiplication, and explores the mathematical rationale for storing transparency this way to optimize compression and eliminate edge artifacts.

Straight Alpha vs. Premultiplied Alpha

In standard unassociated (straight) alpha, color channels (\(R, G, B\)) and the transparency channel (\(A\)) are stored independently:

\[\text{Pixel}_{\text{straight}} = (R, G, B, A)\]

In associated (premultiplied) alpha, each color component is scaled by the normalized alpha value prior to encoding and storage:

\[\text{Pixel}_{\text{premultiplied}} = (R', G', B', A)\]

The Forward Conversion Formula

To convert a pixel from straight alpha to premultiplied alpha, the color components are multiplied by the alpha factor.

1. Normalized Space (\([0.0, 1.0]\))

Let \(R, G, B \in [0.0, 1.0]\) be the color values, and \(A \in [0.0, 1.0]\) be the opacity, where \(0.0\) represents fully transparent and \(1.0\) represents fully opaque:

\[R' = R \times A\] \[G' = G \times A\] \[B' = B \times A\] \[A' = A\]

2. Discrete Integer Space (\(N\)-bit Depth)

AVIF commonly encodes images at 8-bit, 10-bit, or 12-bit depths. For an \(N\)-bit channel, the maximum integer value is:

\[MAX = 2^N - 1\]

Given integer inputs \(R_{int}, G_{int}, B_{int}, A_{int} \in [0, MAX]\):

  1. Normalize the alpha channel: \[\alpha = \frac{A_{int}}{MAX}\]

  2. Multiply each color channel and round to the nearest integer: \[R'_{int} = \text{round}\left( R_{int} \times \frac{A_{int}}{MAX} \right)\] \[G'_{int} = \text{round}\left( G_{int} \times \frac{A_{int}}{MAX} \right)\] \[B'_{int} = \text{round}\left( B_{int} \times \frac{A_{int}}{MAX} \right)\] \[A'_{int} = A_{int}\]

The Inverse Conversion Formula (Un-premultiplication)

When an application requires original straight RGB values (for editing or color manipulation), it must invert the process:

\[R = \begin{cases} \text{round}\left( \frac{R'_{int} \times MAX}{A_{int}} \right) & \text{if } A_{int} > 0 \\ 0 & \text{if } A_{int} = 0 \end{cases}\]

(The same formula applies to \(G\) and \(B\).)

When \(A_{int} = 0\), original RGB values cannot be mathematically recovered because any value multiplied by zero yields zero. Additionally, rounding errors during the forward multiplication can cause slight precision loss when un-premultiplying low-alpha values at 8-bit depth.

Mathematical Simplification of Compositing

The primary mathematical benefit of premultiplied alpha appears during the Porter-Duff "Source Over" compositing operation.

Standard Straight Alpha Blend

Compositing a source pixel (\(S\)) over a destination pixel (\(D\)):

\[A_{out} = A_S + A_D \times (1 - A_S)\] \[C_{out} = \frac{C_S \times A_S + C_D \times A_D \times (1 - A_S)}{A_{out}}\]

(Where \(C\) represents any color channel \(R, G,\) or \(B\).)

This requires two multiplications, one addition, and an expensive floating-point division per color channel.

Premultiplied Alpha Blend

Using premultiplied values (\(C' = C \times A\)):

\[A_{out} = A_S + A_D \times (1 - A_S)\] \[C'_{out} = C'_S + C'_D \times (1 - A_S)\]

The division is eliminated, reducing the computation to a single multiply-accumulate operation per channel:

\[C'_{out} = C'_S + C'_D - C'_D \times A_S\]

AVIF Structural Handling and Compression Mathematics

AVIF is built on the ISO Base Media File Format (ISOBMFF) and the AV1 video codec. In AVIF:

  1. Auxiliary Items: The alpha channel is stored as a separate, monochrome auxiliary image item (auxl) linked to the primary color image item.
  2. Signaling: The presence of premultiplied alpha is signaled in the container via the Alpha Premultiplication Box (alpr) defined by the Multi-Image Application Format (MIAF) standard.

Impact on Discrete Cosine Transform (DCT)

AV1 uses transform coding (DCT and Asymmetric Discrete Sine Transforms) to compress spatial blocks.

In straight alpha, transparent pixels retain arbitrary RGB values. At transparent boundaries, this creates sharp, high-frequency step discontinuities between visible and invisible pixels:

\[f(x) = \begin{cases} C_{visible} & x \le x_0 \\ C_{arbitrary} & x > x_0 \end{cases}\]

High-frequency edges require more transform coefficients, leading to higher bitrates and "ringing" compression artifacts.

In premultiplied alpha:

\[\lim_{A \to 0} C' = \lim_{A \to 0} (C \times A) = 0\]

As opacity drops to zero, the color values smoothly converge to zero. This mathematical continuity removes artificial step functions, significantly reducing high-frequency energy in the spatial frequency domain:

\[F(u, v) = \sum_{x} \sum_{y} f(x, y) \cos\left[\frac{\pi}{N}\left(x + \frac{1}{2}\right)u\right] \cos\left[\frac{\pi}{N}\left(y + \frac{1}{2}\right)v\right]\]

Because \(f(x, y)\) transitions smoothly to zero along transparent edges, fewer high-order AC coefficients are needed, directly improving AV1 encoding efficiency and preventing dark or fringed halos during reconstruction.