A VP8L chunk holds a lossless image: every pixel comes back exactly as it was, with an 8-bit
alpha channel as well as red, green and blue. The encoder first reshapes the pixels with up to four
reversible transforms so that most values become small and repetitive, then codes them with prefix (Huffman)
codes, copies of earlier pixels (LZ77) and a cache of recent colours. A reader undoes that in reverse: it
reads the header and the transforms, rebuilds the prefix codes, decodes the pixels, then applies the inverse
transforms last one first.
The data is a stream of bits. A multi-bit value is read with its first bit as the least significant bit, and
bits are taken from the least significant bit of each byte first. ReadBits(n) below means n bits
read that way. The same stream, without its header, holds the alpha plane of a lossy image
(ALPH).
The five kinds of image inside the stream
| Role | Holds | Size |
|---|---|---|
| ARGB image | The pixels (after the transforms). | The image, or narrower after colour indexing. |
| Entropy image | Which group of prefix codes each tile of the ARGB image uses. | Tiles of 2prefix_bits pixels square. |
| Predictor image | The predictor mode of each block. | Blocks of 2size_bits pixels square. |
| Colour transform image | The three multipliers of each block. | Blocks of 2size_bits pixels square. |
| Colour indexing image | The palette, delta coded. | The palette size by 1 pixel. |
All five are coded the same way (a colour cache, prefix codes and tokens). Only the ARGB image can have an entropy image, so the others skip that flag. Only the ARGB image has transforms.
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) |
| 14, 15 | Not defined by the specification, which describes 14 predictors. libwebp, and so every browser, treats them as mode 0, solid black 0xff000000; this viewer does the same. |
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 |
Bundled indices are packed into the green channel, the first pixel in the least significant bits: with width_bits 1, pixel x = 2k is in the low 4 bits of stored pixel k and x = 2k + 1 in the high 4 bits; with 2, pixels 4k to 4k + 3 take the 2-bit fields from the least significant upwards; with 3, pixels 8k to 8k + 7 take the bits from the least significant upwards. Transforms read after the colour indexing transform work on the narrower stored image, so their blocks are counted in stored pixels. Undoing them comes first, and colour indexing is undone after those read later, when each index is unpacked and replaced by its palette colour.
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, up to 1,048,576 (prefix 39 has 18 extra bits and starts at 786,433). The extra bits are read straight from the stream, not through a prefix code. Prefix 4 is 5 to 6, 5 is 7 to 8, 6 is 9 to 12, 7 is 13 to 16, and so on, with 2 extra bits more for each two prefixes.
A copy takes its pixels one at a time, so a distance shorter than the length repeats a pattern: a distance of 1 repeats the pixel before it. The copy may not run past the end of the image, and a distance may not reach back before the first pixel.
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, which can be sent either way and takes no bits to read: the symbol is simply that one. A code with no symbol at all is not allowed (an unused code is sent as the single symbol 0). A simple code with the same symbol twice is a single leaf. A decoder fails when max_symbol is larger than the alphabet, when a run of lengths runs past the alphabet, or when the lengths do not make a complete code.
From lengths to codes
The codes are canonical. Symbols are sorted by code length, then by symbol value; the first code of each length is the (last code of the length before + 1) shifted left one bit, starting from 0, and the codes of one length count up in symbol order. The bits of a code are read one at a time, the first bit read being the most significant bit of the code.