A VP8L chunk holds a lossless image as ARGB pixels. The data is a stream of
bits, read starting from the least significant bit of each byte.
Header
| Field | Size | Description |
|---|---|---|
| signature | 8 bits | 2F |
| width minus one | 14 bits | Width is at most 16384. |
| height minus one | 14 bits | Height is at most 16384. |
| alpha_is_used | 1 bit | A hint only; it does not affect decoding. |
| version_number | 3 bits | Must be 0. Any other value is an error. |
A 2 by 3 image without alpha starts 2F 01 80 00 00.
Overall structure
image-stream = optional-transform spatially-coded-image
optional-transform = (%b1 transform optional-transform) / %b0
spatially-coded-image = color-cache-info meta-prefix data
entropy-coded-image = color-cache-info data
data = prefix-codes lz77-coded-image
The end of the image is implicit: decoding stops after width × height pixels. Entropy-coded images, used for the sub-resolution data of the transforms and the entropy image, have no transforms and no meta prefix.
Transforms
Each transform is announced by a 1 bit, then a 2-bit type, then its data; a 0 bit ends the list. Each type may appear at most once. Decoders undo them in the reverse of the order they were read.
| Value | Name | Data |
|---|---|---|
| 0 | PREDICTOR_TRANSFORM | 3 bits: size_bits = value + 2, then an entropy-coded image. |
| 1 | COLOR_TRANSFORM | 3 bits: size_bits = value + 2, then an entropy-coded image. |
| 2 | SUBTRACT_GREEN_TRANSFORM | None. |
| 3 | COLOR_INDEXING_TRANSFORM | 8 bits: palette size minus 1, then the palette as an entropy-coded image. |
A sub-resolution image has a width of ⌈image width ÷ 2size_bits⌉, and the height likewise.
Predictor transform
The green channel of each pixel in the sub-resolution image selects a mode for that block. L is the pixel to the left, T above, TL above-left and TR above-right.
| Mode | Prediction per channel |
|---|---|
| 0 | Solid black, 0xff000000 |
| 1 | L |
| 2 | T |
| 3 | TR |
| 4 | TL |
| 5 | Average2(Average2(L, TR), T) |
| 6 | Average2(L, TL) |
| 7 | Average2(L, T) |
| 8 | Average2(TL, T) |
| 9 | Average2(T, TR) |
| 10 | Average2(Average2(L, TL), Average2(T, TR)) |
| 11 | Select(L, T, TL) |
| 12 | ClampAddSubtractFull(L, T, TL) |
| 13 | ClampAddSubtractHalf(Average2(L, T), TL) |
Average2(a, b) = (a + b) / 2 integer division, per channel
ClampAddSubtractFull(a,b,c) = clamp(a + b - c)
ClampAddSubtractHalf(a,b) = clamp(a + (a - b) / 2)
clamp(x) = x limited to 0..255
Select(L, T, TL):
e = L + T - TL per channel
dL = sum over A, R, G, B of |e - L|
dT = sum over A, R, G, B of |e - T|
return L if dL < dT, otherwise T
| Position | Rule |
|---|---|
| Top-left pixel | Uses mode 0. |
| Top row | Uses L. |
| Left column | Uses T. |
| Rightmost column | TR is the leftmost pixel of the current row. |
The final pixel is the stored residual plus the prediction, per channel, modulo 256.
Colour transform
Each block stores three signed 8-bit multipliers, packed into a pixel with alpha 255, red = red_to_blue, green = green_to_blue and blue = green_to_red.
delta(t, c) = (t * c) >> 5
red = red + delta(green_to_red, green)
blue = blue + delta(green_to_blue, green)
blue = blue + delta(red_to_blue, red) using the restored red
Subtract green
No data. The decoder adds green to red and to blue, modulo 256.
Colour indexing
The palette size is the 8-bit value plus 1, so 1 to 256 colours. The palette is stored
as an image one pixel high, each entry delta-coded from the previous one per channel
modulo 256. Pixels then hold indices in their green channel. An index at or past the
end of the palette gives 0x00000000.
Small palettes bundle several pixels into one, and the image width becomes ⌈width ÷ 2width_bits⌉.
| Palette size | Bits per pixel | Pixels per stored pixel | width_bits |
|---|---|---|---|
| 1 or 2 | 1 | 8 | 3 |
| 3 or 4 | 2 | 4 | 2 |
| 5 to 16 | 4 | 2 | 1 |
| 17 to 256 | 8 | 1 | 0 |
Coding each pixel
Pixels are stored in scan-line order. The green prefix code gives a symbol S, which says what kind of pixel comes next.
| Symbol | Meaning |
|---|---|
| 0 to 255 | A literal: S is green, then red, blue and alpha are each read with their own prefix code. |
| 256 to 279 | An LZ77 backward reference: S − 256 is the length prefix code. |
| 280 and up | A colour cache index: S − 280. |
LZ77 prefix coding
if prefix < 4:
value = prefix + 1
else:
extra = (prefix - 2) >> 1
offset = (2 + (prefix & 1)) << extra
value = offset + ReadBits(extra) + 1
Lengths use the first 24 prefix codes, for a maximum length of 4096. Distances use all 40.
A distance code above 120 is a scan-line distance of code − 120. Codes 1 to 120 are a table of nearby positions (xi, yi), and the distance is xi + yi × width, with a minimum of 1:
(0,1) (1,0) (1,1) (-1,1) (0,2) (2,0) (1,2) (-1,2) (2,1) (-2,1)
(2,2) (-2,2) (0,3) (3,0) (1,3) (-1,3) (3,1) (-3,1) (2,3) (-2,3)
(3,2) (-3,2) (0,4) (4,0) (1,4) (-1,4) (4,1) (-4,1) (3,3) (-3,3)
(2,4) (-2,4) (4,2) (-4,2) (0,5) (3,4) (-3,4) (4,3) (-4,3) (5,0)
(1,5) (-1,5) (5,1) (-5,1) (2,5) (-2,5) (5,2) (-5,2) (4,4) (-4,4)
(3,5) (-3,5) (5,3) (-5,3) (0,6) (6,0) (1,6) (-1,6) (6,1) (-6,1)
(2,6) (-2,6) (6,2) (-6,2) (4,5) (-4,5) (5,4) (-5,4) (3,6) (-3,6)
(6,3) (-6,3) (0,7) (7,0) (1,7) (-1,7) (5,5) (-5,5) (7,1) (-7,1)
(4,6) (-4,6) (6,4) (-6,4) (2,7) (-2,7) (7,2) (-7,2) (3,7) (-3,7)
(7,3) (-7,3) (5,6) (-5,6) (6,5) (-6,5) (8,0) (4,7) (-4,7) (7,4)
(-7,4) (8,1) (8,2) (6,6) (-6,6) (8,3) (5,7) (-5,7) (7,5) (-7,5)
(8,4) (6,7) (-6,7) (7,6) (-7,6) (8,5) (7,7) (-7,7) (8,6) (8,7)
The list reads left to right, so code 1 is (0,1), the pixel directly above; code 2 is (1,0), the pixel to the left; and code 3 is (1,1), the pixel above and to the left.
Colour cache
| Field | Size | Description |
|---|---|---|
| color_cache_flag | 1 bit | Whether a cache is used. |
| color_cache_code_bits | 4 bits | When the flag is set: must be 1 to 11. The cache has 2bits entries. |
index = (0x1e35a7bd * color) >> (32 - color_cache_code_bits) 32-bit arithmetic
The cache starts zeroed and receives every pixel in stream order. There is a single lookup and no collision handling.
Prefix code groups
Each group has five canonical prefix codes.
| Code | Symbols | Alphabet size |
|---|---|---|
| 1 | Green, length prefixes and colour cache indices | 256 + 24 + cache size |
| 2 | Red | 256 |
| 3 | Blue | 256 |
| 4 | Alpha | 256 |
| 5 | Distance prefixes | 40 |
The main ARGB image can use several groups. One bit says whether it does: 0 for one
group, 1 for an entropy image. With an entropy image, 3 bits give
prefix_bits = value + 2, and each pixel’s group is
(entropy_pixel >> 8) & 0xffff. The number of groups is the largest
index plus one.
Reading a prefix code
One bit selects the kind of code.
Simple code lengths (bit 1): one bit gives the number of symbols minus one, so one or two. One bit says whether the first symbol has 1 or 8 bits. The second symbol is always 8 bits. Each symbol gets length 1.
Normal code lengths (bit 0):
- Read 4 bits and add 4 for the number of code-length code lengths. Each is 3 bits, in this order: 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.
- One bit selects the symbol count: 0 means the alphabet size. With 1, read 3 bits x, then length_nbits = 2 + 2x, then max_symbol = 2 + ReadBits(length_nbits).
- Decode the lengths with the table below.
| Code | Extra bits | Meaning |
|---|---|---|
| 0 to 15 | 0 | A literal length; 0 means the symbol is not used. |
| 16 | 2 | Repeat the previous non-zero length 3 to 6 times; 8 is used if there is none. |
| 17 | 3 | 3 to 10 zeros. |
| 18 | 7 | 11 to 138 zeros. |
The code must be complete, with a Kraft sum of exactly 1. The one exception is a single leaf with length 1.