2012-05-26 02:11:02 +02:00
|
|
|
<!--
|
|
|
|
|
|
|
|
Although you may be viewing an alternate representation, this document
|
|
|
|
is sourced in Markdown, a light-duty markup scheme, and is optimized for
|
|
|
|
the [kramdown](http://kramdown.rubyforge.org/) transformer.
|
|
|
|
|
|
|
|
See the accompanying README. External link targets are referenced at the
|
|
|
|
end of this file.
|
|
|
|
|
|
|
|
-->
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
Specification for WebP Lossless Bitstream
|
2012-06-14 21:03:55 +02:00
|
|
|
=========================================
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-07-11 22:46:00 +02:00
|
|
|
_Jyrki Alakuijala, Ph.D., Google, Inc., 2012-06-19_
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
--------
|
|
|
|
|
2012-06-15 00:59:26 +02:00
|
|
|
WebP lossless is an image format for lossless compression of ARGB
|
|
|
|
images. The lossless format stores and restores the pixel values
|
|
|
|
exactly, including the color values for zero alpha pixels. The
|
2012-05-26 02:11:02 +02:00
|
|
|
format uses subresolution images, recursively embedded into the format
|
2012-06-15 00:59:26 +02:00
|
|
|
itself, for storing statistical data about the images, such as the used
|
|
|
|
entropy codes, spatial predictors, color space conversion, and color
|
|
|
|
table. LZ77, Huffman coding, and a color cache are used for compression
|
|
|
|
of the bulk data. Decoding speeds faster than PNG have been
|
2012-06-15 02:13:15 +02:00
|
|
|
demonstrated, as well as 25% denser compression than can be achieved
|
|
|
|
using today's PNG format.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
* TOC placeholder
|
|
|
|
{:toc}
|
|
|
|
|
|
|
|
|
|
|
|
Nomenclature
|
|
|
|
------------
|
|
|
|
|
|
|
|
ARGB
|
|
|
|
: A pixel value consisting of alpha, red, green, and blue values.
|
|
|
|
|
|
|
|
ARGB image
|
|
|
|
: A two-dimensional array containing ARGB pixels.
|
|
|
|
|
|
|
|
color cache
|
2012-06-15 02:13:15 +02:00
|
|
|
: A small hash-addressed array to store recently used colors, to be able
|
|
|
|
to recall them with shorter codes.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
color indexing image
|
2012-06-15 02:13:15 +02:00
|
|
|
: A one-dimensional image of colors that can be indexed using a small
|
|
|
|
integer (up to 256 within WebP lossless).
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
color transform image
|
2012-06-15 02:13:15 +02:00
|
|
|
: A two-dimensional subresolution image containing data about
|
|
|
|
correlations of color components.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
distance mapping
|
2012-06-15 02:13:15 +02:00
|
|
|
: Changes LZ77 distances to have the smallest values for pixels in 2D
|
|
|
|
proximity.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
entropy image
|
2012-06-15 02:13:15 +02:00
|
|
|
: A two-dimensional subresolution image indicating which entropy coding
|
|
|
|
should be used in a respective square in the image, i.e., each pixel
|
|
|
|
is a meta Huffman code.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
Huffman code
|
2012-06-15 02:13:15 +02:00
|
|
|
: A classic way to do entropy coding where a smaller number of bits are
|
|
|
|
used for more frequent codes.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
LZ77
|
|
|
|
: Dictionary-based sliding window compression algorithm that either
|
|
|
|
emits symbols or describes them as sequences of past symbols.
|
|
|
|
|
|
|
|
meta Huffman code
|
2012-06-15 02:13:15 +02:00
|
|
|
: A small integer (up to 16 bits) that indexes an element in the meta
|
|
|
|
Huffman table.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
predictor image
|
2012-06-15 02:13:15 +02:00
|
|
|
: A two-dimensional subresolution image indicating which spatial
|
|
|
|
predictor is used for a particular square in the image.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
prefix coding
|
2012-06-15 02:13:15 +02:00
|
|
|
: A way to entropy code larger integers that codes a few bits of the
|
|
|
|
integer using an entropy code and codifies the remaining bits raw.
|
|
|
|
This allows for the descriptions of the entropy codes to remain
|
2012-05-26 02:11:02 +02:00
|
|
|
relatively small even when the range of symbols is large.
|
|
|
|
|
|
|
|
scan-line order
|
2012-06-15 02:13:15 +02:00
|
|
|
: A processing order of pixels, left-to-right, top-to-bottom, starting
|
|
|
|
from the left-hand-top pixel, proceeding to the right. Once a row is
|
|
|
|
completed, continue from the left-hand column of the next row.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
2012-06-15 00:59:26 +02:00
|
|
|
1 Introduction
|
|
|
|
--------------
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
This document describes the compressed data representation of a WebP
|
|
|
|
lossless image. It is intended as a detailed reference for WebP lossless
|
|
|
|
encoder and decoder implementation.
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
In this document, we extensively use C programming language syntax to
|
|
|
|
describe the bitstream, and assume the existence of a function for
|
|
|
|
reading bits, `ReadBits(n)`. The bytes are read in the natural order of
|
|
|
|
the stream containing them, and bits of each byte are read in
|
|
|
|
least-significant-bit-first order. When multiple bits are read at the
|
|
|
|
same time, the integer is constructed from the original data in the
|
|
|
|
original order. The most significant bits of the returned integer are
|
|
|
|
also the most significant bits of the original data. Thus the statement
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
b = ReadBits(2);
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
is equivalent with the two statements below:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-06-15 02:13:15 +02:00
|
|
|
b = ReadBits(1);
|
2012-05-26 02:11:02 +02:00
|
|
|
b |= ReadBits(1) << 1;
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
We assume that each color component (e.g. alpha, red, blue and green) is
|
2012-06-15 00:14:14 +02:00
|
|
|
represented using an 8-bit byte. We define the corresponding type as
|
|
|
|
uint8. A whole ARGB pixel is represented by a type called uint32, an
|
|
|
|
unsigned integer consisting of 32 bits. In the code showing the behavior
|
|
|
|
of the transformations, alpha value is codified in bits 31..24, red in
|
|
|
|
bits 23..16, green in bits 15..8 and blue in bits 7..0, but
|
|
|
|
implementations of the format are free to use another representation
|
|
|
|
internally.
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
Broadly, a WebP lossless image contains header data, transform
|
2012-06-15 00:14:14 +02:00
|
|
|
information and actual image data. Headers contain width and height of
|
2012-07-02 22:31:09 +02:00
|
|
|
the image. A WebP lossless image can go through four different types of
|
2012-06-15 00:14:14 +02:00
|
|
|
transformation before being entropy encoded. The transform information
|
2012-06-15 02:13:15 +02:00
|
|
|
in the bitstream contains the data required to apply the respective
|
2012-06-15 00:14:14 +02:00
|
|
|
inverse transforms.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
2012-06-15 00:59:26 +02:00
|
|
|
2 RIFF Header
|
|
|
|
-------------
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
The beginning of the header has the RIFF container. This consists of the
|
2012-05-26 02:11:02 +02:00
|
|
|
following 21 bytes:
|
|
|
|
|
2012-05-31 22:04:19 +02:00
|
|
|
1. String "RIFF"
|
2012-06-15 00:14:14 +02:00
|
|
|
2. A little-endian 32 bit value of the block length, the whole size
|
|
|
|
of the block controlled by the RIFF header. Normally this equals
|
2012-06-15 02:13:15 +02:00
|
|
|
the payload size (file size minus 8 bytes: 4 bytes for the 'RIFF'
|
|
|
|
identifier and 4 bytes for storing the value itself).
|
2012-05-31 22:04:19 +02:00
|
|
|
3. String "WEBP" (RIFF container name).
|
|
|
|
4. String "VP8L" (chunk tag for lossless encoded image data).
|
2012-06-15 00:14:14 +02:00
|
|
|
5. A little-endian 32-bit value of the number of bytes in the
|
|
|
|
lossless stream.
|
2012-06-15 02:13:15 +02:00
|
|
|
6. One byte signature 0x2f.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
The first 28 bits of the bitstream specify the width and height of the
|
2012-06-15 00:14:14 +02:00
|
|
|
image. Width and height are decoded as 14-bit integers as follows:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int image_width = ReadBits(14) + 1;
|
|
|
|
int image_height = ReadBits(14) + 1;
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The 14-bit dynamics for image size limit the maximum size of a WebP
|
|
|
|
lossless image to 16384✕16384 pixels.
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
The alpha_is_used bit is a hint only, and should not impact decoding.
|
|
|
|
It should be set to 0 when all alpha values are 255 in the picture, and
|
|
|
|
1 otherwise.
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int alpha_is_used = ReadBits(1);
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The version_number is a 3 bit code that must be discarded by the decoder
|
|
|
|
at this time. Complying encoders write a 3-bit value 0.
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int version_number = ReadBits(3);
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:59:26 +02:00
|
|
|
3 Transformations
|
|
|
|
-----------------
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
Transformations are reversible manipulations of the image data that can
|
|
|
|
reduce the remaining symbolic entropy by modeling spatial and color
|
|
|
|
correlations. Transformations can make the final compression more dense.
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
An image can go through four types of transformation. A 1 bit indicates
|
|
|
|
the presence of a transform. Each transform is allowed to be used only
|
|
|
|
once. The transformations are used only for the main level ARGB image:
|
2012-06-15 00:14:14 +02:00
|
|
|
the subresolution images have no transforms, not even the 0 bit
|
|
|
|
indicating the end-of-transforms.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
Typically an encoder would use these transforms to reduce the Shannon
|
|
|
|
entropy in the residual image. Also, the transform data can be decided
|
|
|
|
based on entropy minimization.
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
while (ReadBits(1)) { // Transform present.
|
|
|
|
// Decode transform type.
|
|
|
|
enum TransformType transform_type = ReadBits(2);
|
|
|
|
// Decode transform data.
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
// Decode actual image data (Section 4).
|
2012-05-26 02:11:02 +02:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
If a transform is present then the next two bits specify the transform
|
|
|
|
type. There are four types of transforms.
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
enum TransformType {
|
|
|
|
PREDICTOR_TRANSFORM = 0,
|
|
|
|
COLOR_TRANSFORM = 1,
|
|
|
|
SUBTRACT_GREEN = 2,
|
|
|
|
COLOR_INDEXING_TRANSFORM = 3,
|
|
|
|
};
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The transform type is followed by the transform data. Transform data
|
2012-06-15 02:13:15 +02:00
|
|
|
contains the information required to apply the inverse transform and
|
2012-05-26 02:11:02 +02:00
|
|
|
depends on the transform type. Next we describe the transform data for
|
|
|
|
different types.
|
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
### Predictor Transform
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-14 21:57:02 +02:00
|
|
|
The predictor transform can be used to reduce entropy by exploiting the
|
|
|
|
fact that neighboring pixels are often correlated. In the predictor
|
|
|
|
transform, the current pixel value is predicted from the pixels already
|
|
|
|
decoded (in scan-line order) and only the residual value (actual -
|
|
|
|
predicted) is encoded. The _prediction mode_ determines the type of
|
|
|
|
prediction to use. We divide the image into squares and all the pixels
|
|
|
|
in a square use same prediction mode.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-12-05 09:01:28 +01:00
|
|
|
The first 3 bits of prediction data define the block width and height in
|
2012-06-15 02:13:15 +02:00
|
|
|
number of bits. The number of block columns, `block_xsize`, is used in
|
2012-05-26 02:11:02 +02:00
|
|
|
indexing two-dimensionally.
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-06-15 02:13:15 +02:00
|
|
|
int size_bits = ReadBits(3) + 2;
|
2012-05-26 02:11:02 +02:00
|
|
|
int block_width = (1 << size_bits);
|
|
|
|
int block_height = (1 << size_bits);
|
|
|
|
#define DIV_ROUND_UP(num, den) ((num) + (den) - 1) / (den))
|
|
|
|
int block_xsize = DIV_ROUND_UP(image_width, 1 << size_bits);
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The transform data contains the prediction mode for each block of the
|
2012-06-14 21:57:02 +02:00
|
|
|
image. All the `block_width * block_height` pixels of a block use same
|
|
|
|
prediction mode. The prediction modes are treated as pixels of an image
|
2012-06-15 02:13:15 +02:00
|
|
|
and encoded using the same techniques described in
|
|
|
|
[Chapter 4](#image-data).
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-14 21:57:02 +02:00
|
|
|
For a pixel _x, y_, one can compute the respective filter block address
|
|
|
|
by:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int block_index = (y >> size_bits) * block_xsize +
|
|
|
|
(x >> size_bits);
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
There are 14 different prediction modes. In each prediction mode, the
|
2012-06-15 00:14:14 +02:00
|
|
|
current pixel value is predicted from one or more neighboring pixels
|
|
|
|
whose values are already known.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
We choose the neighboring pixels (TL, T, TR, and L) of the current pixel
|
|
|
|
(P) as follows:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
O O O O O O O O O O O
|
|
|
|
O O O O O O O O O O O
|
|
|
|
O O O O TL T TR O O O O
|
|
|
|
O O O O L P X X X X X
|
|
|
|
X X X X X X X X X X X
|
|
|
|
X X X X X X X X X X X
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
where TL means top-left, T top, TR top-right, L left pixel.
|
2012-06-15 00:14:14 +02:00
|
|
|
At the time of predicting a value for P, all pixels O, TL, T, TR and L
|
|
|
|
have been already processed, and pixel P and all pixels X are unknown.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
Given the above neighboring pixels, the different prediction modes are
|
|
|
|
defined as follows.
|
|
|
|
|
|
|
|
| Mode | Predicted value of each channel of the current pixel |
|
|
|
|
| ------ | ------------------------------------------------------- |
|
|
|
|
| 0 | 0xff000000 (represents solid black color in ARGB) |
|
|
|
|
| 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) |
|
2012-06-15 02:13:15 +02:00
|
|
|
| 12 | ClampAddSubtractFull(L, T, TL) |
|
|
|
|
| 13 | ClampAddSubtractHalf(Average2(L, T), TL) |
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
2012-06-14 21:57:02 +02:00
|
|
|
`Average2` is defined as follows for each ARGB component:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
uint8 Average2(uint8 a, uint8 b) {
|
|
|
|
return (a + b) / 2;
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The Select predictor is defined as follows:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
uint32 Select(uint32 L, uint32 T, uint32 TL) {
|
|
|
|
// L = left pixel, T = top pixel, TL = top left pixel.
|
|
|
|
|
|
|
|
// ARGB component estimates for prediction.
|
|
|
|
int pAlpha = ALPHA(L) + ALPHA(T) - ALPHA(TL);
|
|
|
|
int pRed = RED(L) + RED(T) - RED(TL);
|
|
|
|
int pGreen = GREEN(L) + GREEN(T) - GREEN(TL);
|
|
|
|
int pBlue = BLUE(L) + BLUE(T) - BLUE(TL);
|
|
|
|
|
|
|
|
// Manhattan distances to estimates for left and top pixels.
|
|
|
|
int pL = abs(pAlpha - ALPHA(L)) + abs(pRed - RED(L)) +
|
|
|
|
abs(pGreen - GREEN(L)) + abs(pBlue - BLUE(L));
|
|
|
|
int pT = abs(pAlpha - ALPHA(T)) + abs(pRed - RED(T)) +
|
|
|
|
abs(pGreen - GREEN(T)) + abs(pBlue - BLUE(T));
|
|
|
|
|
|
|
|
// Return either left or top, the one closer to the prediction.
|
|
|
|
if (pL <= pT) {
|
|
|
|
return L;
|
|
|
|
} else {
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
The functions `ClampAddSubtractFull` and `ClampAddSubtractHalf` are
|
2012-05-26 02:11:02 +02:00
|
|
|
performed for each ARGB component as follows:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
// Clamp the input value between 0 and 255.
|
|
|
|
int Clamp(int a) {
|
|
|
|
return (a < 0) ? 0 : (a > 255) ? 255 : a;
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int ClampAddSubtractFull(int a, int b, int c) {
|
|
|
|
return Clamp(a + b - c);
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int ClampAddSubtractHalf(int a, int b) {
|
|
|
|
return Clamp(a + (a - b) / 2);
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
There are special handling rules for some border pixels. If there is a
|
2012-06-15 00:14:14 +02:00
|
|
|
prediction transform, regardless of the mode [0..13] for these pixels,
|
|
|
|
the predicted value for the left-topmost pixel of the image is
|
|
|
|
0xff000000, L-pixel for all pixels on the top row, and T-pixel for all
|
|
|
|
pixels on the leftmost column.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
Addressing the TR-pixel for pixels on the rightmost column is
|
|
|
|
exceptional. The pixels on the rightmost column are predicted by using
|
|
|
|
the modes [0..13] just like pixels not on border, but by using the
|
|
|
|
leftmost pixel on the same row as the current TR-pixel. The TR-pixel
|
|
|
|
offset in memory is the same for border and non-border pixels.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
### Color Transform
|
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
The goal of the color transform is to decorrelate the R, G and B values
|
|
|
|
of each pixel. Color transform keeps the green (G) value as it is,
|
|
|
|
transforms red (R) based on green and transforms blue (B) based on green
|
|
|
|
and then based on red.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
As is the case for the predictor transform, first the image is divided
|
|
|
|
into blocks and the same transform mode is used for all the pixels in a
|
|
|
|
block. For each block there are three types of color transform elements.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
typedef struct {
|
|
|
|
uint8 green_to_red;
|
|
|
|
uint8 green_to_blue;
|
|
|
|
uint8 red_to_blue;
|
|
|
|
} ColorTransformElement;
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The actual color transformation is done by defining a color transform
|
2012-06-15 02:13:15 +02:00
|
|
|
delta. The color transform delta depends on the `ColorTransformElement`,
|
|
|
|
which is the same for all the pixels in a particular block. The delta is
|
2012-06-14 21:57:02 +02:00
|
|
|
added during color transform. The inverse color transform then is just
|
|
|
|
subtracting those deltas.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
The color transform function is defined as follows:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
void ColorTransform(uint8 red, uint8 blue, uint8 green,
|
|
|
|
ColorTransformElement *trans,
|
|
|
|
uint8 *new_red, uint8 *new_blue) {
|
|
|
|
// Transformed values of red and blue components
|
|
|
|
uint32 tmp_red = red;
|
|
|
|
uint32 tmp_blue = blue;
|
|
|
|
|
|
|
|
// Applying transform is just adding the transform deltas
|
|
|
|
tmp_red += ColorTransformDelta(trans->green_to_red, green);
|
|
|
|
tmp_blue += ColorTransformDelta(trans->green_to_blue, green);
|
|
|
|
tmp_blue += ColorTransformDelta(trans->red_to_blue, red);
|
|
|
|
|
|
|
|
*new_red = tmp_red & 0xff;
|
|
|
|
*new_blue = tmp_blue & 0xff;
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-14 21:57:02 +02:00
|
|
|
`ColorTransformDelta` is computed using a signed 8-bit integer
|
|
|
|
representing a 3.5-fixed-point number, and a signed 8-bit RGB color
|
|
|
|
channel (c) [-128..127] and is defined as follows:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int8 ColorTransformDelta(int8 t, int8 c) {
|
|
|
|
return (t * c) >> 5;
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
The multiplication is to be done using more precision (with at least
|
|
|
|
16-bit dynamics). The sign extension property of the shift operation
|
2012-06-15 00:14:14 +02:00
|
|
|
does not matter here: only the lowest 8 bits are used from the result,
|
|
|
|
and there the sign extension shifting and unsigned shifting are
|
|
|
|
consistent with each other.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
Now we describe the contents of color transform data so that decoding
|
|
|
|
can apply the inverse color transform and recover the original red and
|
2012-12-05 22:37:52 +01:00
|
|
|
blue values. The first 3 bits of the color transform data contain the
|
2012-06-15 00:14:14 +02:00
|
|
|
width and height of the image block in number of bits, just like the
|
|
|
|
predictor transform:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-06-15 02:13:15 +02:00
|
|
|
int size_bits = ReadStream(3) + 2;
|
2012-05-26 02:11:02 +02:00
|
|
|
int block_width = 1 << size_bits;
|
|
|
|
int block_height = 1 << size_bits;
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The remaining part of the color transform data contains
|
2012-06-15 02:13:15 +02:00
|
|
|
`ColorTransformElement` instances corresponding to each block of the
|
|
|
|
image. `ColorTransformElement` instances are treated as pixels of an
|
|
|
|
image and encoded using the methods described in
|
|
|
|
[Chapter 4](#image-data).
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
During decoding, `ColorTransformElement` instances of the blocks are
|
2012-06-15 00:14:14 +02:00
|
|
|
decoded and the inverse color transform is applied on the ARGB values of
|
2012-06-15 02:13:15 +02:00
|
|
|
the pixels. As mentioned earlier, that inverse color transform is just
|
|
|
|
subtracting `ColorTransformElement` values from the red and blue
|
|
|
|
channels.
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-05-26 02:11:02 +02:00
|
|
|
void InverseTransform(uint8 red, uint8 green, uint8 blue,
|
2012-06-15 02:13:15 +02:00
|
|
|
ColorTransformElement *p,
|
2012-05-26 02:11:02 +02:00
|
|
|
uint8 *new_red, uint8 *new_blue) {
|
|
|
|
// Applying inverse transform is just subtracting the
|
|
|
|
// color transform deltas
|
|
|
|
red -= ColorTransformDelta(p->green_to_red_, green);
|
|
|
|
blue -= ColorTransformDelta(p->green_to_blue_, green);
|
|
|
|
blue -= ColorTransformDelta(p->red_to_blue_, red & 0xff);
|
|
|
|
|
|
|
|
*new_red = red & 0xff;
|
|
|
|
*new_blue = blue & 0xff;
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
|
|
### Subtract Green Transform
|
|
|
|
|
|
|
|
The subtract green transform subtracts green values from red and blue
|
2012-06-15 00:14:14 +02:00
|
|
|
values of each pixel. When this transform is present, the decoder needs
|
|
|
|
to add the green value to both red and blue. There is no data associated
|
|
|
|
with this transform. The decoder applies the inverse transform as
|
|
|
|
follows:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
void AddGreenToBlueAndRed(uint8 green, uint8 *red, uint8 *blue) {
|
|
|
|
*red = (*red + green) & 0xff;
|
|
|
|
*blue = (*blue + green) & 0xff;
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
This transform is redundant as it can be modeled using the color
|
2012-06-15 02:13:15 +02:00
|
|
|
transform, but it is still often useful. Since it can extend the
|
|
|
|
dynamics of the color transform and there is no additional data here,
|
|
|
|
the subtract green transform can be coded using fewer bits than a
|
|
|
|
full-blown color transform.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
### Color Indexing Transform
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
If there are not many unique pixel values, it may be more efficient to
|
|
|
|
create a color index array and replace the pixel values by the array's
|
|
|
|
indices. The color indexing transform achieves this. (In the context of
|
|
|
|
WebP lossless, we specifically do not call this a palette transform
|
|
|
|
because a similar but more dynamic concept exists in WebP lossless
|
|
|
|
encoding: color cache.)
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
The color indexing transform checks for the number of unique ARGB values
|
|
|
|
in the image. If that number is below a threshold (256), it creates an
|
2012-06-15 02:13:15 +02:00
|
|
|
array of those ARGB values, which is then used to replace the pixel
|
|
|
|
values with the corresponding index: the green channel of the pixels are
|
|
|
|
replaced with the index; all alpha values are set to 255; all red and
|
2012-06-15 00:14:14 +02:00
|
|
|
blue values to 0.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
The transform data contains color table size and the entries in the
|
|
|
|
color table. The decoder reads the color indexing transform data as
|
2012-06-15 02:13:15 +02:00
|
|
|
follows:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
// 8 bit value for color table size
|
|
|
|
int color_table_size = ReadStream(8) + 1;
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
The color table is stored using the image storage format itself. The
|
|
|
|
color table can be obtained by reading an image, without the RIFF
|
2012-06-15 02:13:15 +02:00
|
|
|
header, image size, and transforms, assuming a height of one pixel and
|
|
|
|
a width of `color_table_size`. The color table is always
|
|
|
|
subtraction-coded to reduce image entropy. The deltas of palette colors
|
|
|
|
contain typically much less entropy than the colors themselves, leading
|
2012-06-15 00:14:14 +02:00
|
|
|
to significant savings for smaller images. In decoding, every final
|
|
|
|
color in the color table can be obtained by adding the previous color
|
2012-06-15 02:13:15 +02:00
|
|
|
component values by each ARGB component separately, and storing the
|
2012-06-15 00:14:14 +02:00
|
|
|
least significant 8 bits of the result.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
The inverse transform for the image is simply replacing the pixel values
|
2012-06-15 00:14:14 +02:00
|
|
|
(which are indices to the color table) with the actual color table
|
|
|
|
values. The indexing is done based on the green component of the ARGB
|
|
|
|
color.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
// Inverse transform
|
|
|
|
argb = color_table[GREEN(argb)];
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
When the color table is small (equal to or less than 16 colors), several
|
|
|
|
pixels are bundled into a single pixel. The pixel bundling packs several
|
|
|
|
(2, 4, or 8) pixels into a single pixel, reducing the image width
|
|
|
|
respectively. Pixel bundling allows for a more efficient joint
|
|
|
|
distribution entropy coding of neighboring pixels, and gives some
|
|
|
|
arithmetic coding-like benefits to the entropy code, but it can only be
|
|
|
|
used when there are a small number of unique values.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
`color_table_size` specifies how many pixels are combined together:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-06-15 02:13:15 +02:00
|
|
|
int width_bits;
|
2012-05-26 02:11:02 +02:00
|
|
|
if (color_table_size <= 2) {
|
|
|
|
width_bits = 3;
|
|
|
|
} else if (color_table_size <= 4) {
|
|
|
|
width_bits = 2;
|
|
|
|
} else if (color_table_size <= 16) {
|
|
|
|
width_bits = 1;
|
2012-06-15 02:13:15 +02:00
|
|
|
} else {
|
|
|
|
width_bits = 0;
|
2012-05-26 02:11:02 +02:00
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
`width_bits` has a value of 0, 1, 2 or 3. A value of 0 indicates no
|
2012-06-14 21:57:02 +02:00
|
|
|
pixel bundling to be done for the image. A value of 1 indicates that two
|
|
|
|
pixels are combined together, and each pixel has a range of [0..15]. A
|
|
|
|
value of 2 indicates that four pixels are combined together, and each
|
|
|
|
pixel has a range of [0..3]. A value of 3 indicates that eight pixels
|
2012-06-15 02:13:15 +02:00
|
|
|
are combined together and each pixel has a range of [0..1], i.e., a
|
2012-06-14 21:57:02 +02:00
|
|
|
binary value.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
The values are packed into the green component as follows:
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
* `width_bits` = 1: for every x value where x ≡ 0 (mod 2), a green
|
2012-06-15 00:14:14 +02:00
|
|
|
value at x is positioned into the 4 least-significant bits of the
|
|
|
|
green value at x / 2, a green value at x + 1 is positioned into the
|
|
|
|
4 most-significant bits of the green value at x / 2.
|
2012-06-15 02:13:15 +02:00
|
|
|
* `width_bits` = 2: for every x value where x ≡ 0 (mod 4), a green
|
2012-06-15 00:14:14 +02:00
|
|
|
value at x is positioned into the 2 least-significant bits of the
|
|
|
|
green value at x / 4, green values at x + 1 to x + 3 in order to the
|
|
|
|
more significant bits of the green value at x / 4.
|
2012-06-15 02:13:15 +02:00
|
|
|
* `width_bits` = 3: for every x value where x ≡ 0 (mod 8), a green
|
2012-06-15 00:14:14 +02:00
|
|
|
value at x is positioned into the least-significant bit of the green
|
|
|
|
value at x / 8, green values at x + 1 to x + 7 in order to the more
|
2012-05-26 02:11:02 +02:00
|
|
|
significant bits of the green value at x / 8.
|
|
|
|
|
|
|
|
|
2012-06-15 00:59:26 +02:00
|
|
|
4 Image Data
|
|
|
|
------------
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
Image data is an array of pixel values in scan-line order. We use image
|
2012-06-15 00:14:14 +02:00
|
|
|
data in five different roles: The main role, an auxiliary role related
|
|
|
|
to entropy coding, and three further roles related to transforms.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
1. ARGB image.
|
|
|
|
2. Entropy image. The red and green components define the meta Huffman
|
|
|
|
code used in a particular area of the image.
|
2012-06-15 00:14:14 +02:00
|
|
|
3. Predictor image. The green component defines which of the 14 values
|
|
|
|
is used within a particular square of the image.
|
2012-06-15 02:13:15 +02:00
|
|
|
4. Color indexing image. An array of up to 256 ARGB colors is used for
|
|
|
|
transforming a green-only image, using the green value as an index
|
|
|
|
to this one-dimensional array.
|
2012-12-12 23:03:44 +01:00
|
|
|
5. Color transform image. It is created by `ColorTransformElement` values
|
|
|
|
(defined in [Color Transform](#color-transform)) for different blocks of
|
|
|
|
the image. Each `ColorTransformElement` `'cte'` is treated as a pixel whose
|
|
|
|
alpha component is `255`, red component is `cte.red_to_blue`, green
|
|
|
|
component is `cte.green_to_blue` and blue component is `cte.green_to_red`.
|
2012-06-15 00:14:14 +02:00
|
|
|
|
|
|
|
To divide the image into multiple regions, the image is first divided
|
|
|
|
into a set of fixed-size blocks (typically 16x16 blocks). Each of these
|
|
|
|
blocks can be modeled using an entropy code, in a way where several
|
|
|
|
blocks can share the same entropy code. There is a cost in transmitting
|
|
|
|
an entropy code, and in order to minimize this cost, statistically
|
|
|
|
similar blocks can share an entropy code. The blocks sharing an entropy
|
|
|
|
code can be found by clustering their statistical properties, or by
|
|
|
|
repeatedly joining two randomly selected clusters when it reduces the
|
2012-06-15 02:13:15 +02:00
|
|
|
overall amount of bits needed to encode the image. See the section
|
|
|
|
[Decoding of Meta Huffman Codes](#decoding-of-meta-huffman-codes) in
|
|
|
|
[Chapter 5](#entropy-code) for an explanation of how this entropy image
|
|
|
|
is stored.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
Each pixel is encoded using one of three possible methods:
|
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
1. Huffman coded literals, where each channel (green, alpha, red,
|
2012-06-15 02:13:15 +02:00
|
|
|
blue) is entropy-coded independently;
|
2012-06-15 00:14:14 +02:00
|
|
|
2. LZ77, a sequence of pixels in scan-line order copied from elsewhere
|
2012-06-15 02:13:15 +02:00
|
|
|
in the image; or
|
2012-05-26 02:11:02 +02:00
|
|
|
3. Color cache, using a short multiplicative hash code (color cache
|
|
|
|
index) of a recently seen color.
|
|
|
|
|
|
|
|
In the following sections we introduce the main concepts in LZ77 prefix
|
2012-06-15 00:14:14 +02:00
|
|
|
coding, LZ77 entropy coding, LZ77 distance mapping, and color cache
|
|
|
|
codes. The actual details of the entropy code are described in more
|
2012-06-15 02:13:15 +02:00
|
|
|
detail in [Chapter 5](#entropy-code).
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
### LZ77 Prefix Coding
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
Prefix coding divides large integer values into two parts: the prefix
|
2012-06-15 00:14:14 +02:00
|
|
|
code and the extra bits. The benefit of this approach is that entropy
|
|
|
|
coding is later used only for the prefix code, reducing the resources
|
|
|
|
needed by the entropy code. The extra bits are stored as they are,
|
|
|
|
without an entropy code.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
This prefix code is used for coding backward reference lengths and
|
2012-06-15 00:14:14 +02:00
|
|
|
distances. The extra bits form an integer that is added to the lower
|
|
|
|
value of the range. Hence the LZ77 lengths and distances are divided
|
2012-06-15 02:13:15 +02:00
|
|
|
into prefix codes and extra bits. Performing the Huffman coding only on
|
2012-06-15 00:14:14 +02:00
|
|
|
the prefixes reduces the size of the Huffman codes to tens of values
|
2012-06-15 02:13:15 +02:00
|
|
|
instead of a million (distance) or several thousands (length).
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
| Prefix code | Value range | Extra bits |
|
|
|
|
| ----------- | --------------- | ---------- |
|
|
|
|
| 0 | 1 | 0 |
|
|
|
|
| 1 | 2 | 0 |
|
|
|
|
| 2 | 3 | 0 |
|
|
|
|
| 3 | 4 | 0 |
|
|
|
|
| 4 | 5..6 | 1 |
|
|
|
|
| 5 | 7..8 | 1 |
|
|
|
|
| 6 | 9..12 | 2 |
|
|
|
|
| 7 | 13..16 | 2 |
|
|
|
|
| ... | ... | ... |
|
2012-07-01 23:10:32 +02:00
|
|
|
| 38 | 262145..524288 | 18 |
|
|
|
|
| 39 | 524289..1048576 | 18 |
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
The code to obtain a value from the prefix code is as follows:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
if (prefix_code < 4) {
|
2012-07-01 23:10:32 +02:00
|
|
|
return prefix_code + 1;
|
2012-05-26 02:11:02 +02:00
|
|
|
}
|
2012-06-15 02:13:15 +02:00
|
|
|
int extra_bits = (prefix_code - 2) >> 1;
|
|
|
|
int offset = (2 + (prefix_code & 1)) << extra_bits;
|
2012-05-26 02:11:02 +02:00
|
|
|
return offset + ReadBits(extra_bits) + 1;
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
### LZ77 Backward Reference Entropy Coding
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
Backward references are tuples of length and distance. Length indicates
|
|
|
|
how many pixels in scan-line order are to be copied. The length is
|
|
|
|
codified in two steps: prefix and extra bits. Only the first 24 prefix
|
|
|
|
codes with their respective extra bits are used for length codes,
|
|
|
|
limiting the maximum length to 4096. For distances, all 40 prefix codes
|
|
|
|
are used.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
### LZ77 Distance Mapping
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
120 smallest distance codes [1..120] are reserved for a close
|
|
|
|
neighborhood within the current pixel. The rest are pure distance codes
|
|
|
|
in scan-line order, just offset by 120. The smallest codes are coded
|
|
|
|
into x and y offsets by the following table. Each tuple shows the x and
|
2012-06-15 02:13:15 +02:00
|
|
|
the y coordinates in 2D offsets -- for example the first tuple (0, 1)
|
2012-06-15 00:14:14 +02:00
|
|
|
means 0 for no difference in x, and 1 pixel difference in y (indicating
|
|
|
|
previous row).
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
(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),
|
2012-06-15 02:13:15 +02:00
|
|
|
(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),
|
2012-05-26 02:11:02 +02:00
|
|
|
(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)
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
The distances codes that map into these tuples are changes into
|
|
|
|
scan-line order distances using the following formula:
|
2012-06-15 02:13:15 +02:00
|
|
|
_dist = x + y * xsize_, where _xsize_ is the width of the image in
|
|
|
|
pixels. If a decoder detects a computed _dist_ value smaller than 1,
|
|
|
|
the value of 1 is used instead.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
### Color Cache Code
|
|
|
|
|
|
|
|
Color cache stores a set of colors that have been recently used in the
|
2012-06-15 00:14:14 +02:00
|
|
|
image. Using the color cache code, the color cache colors can be
|
2012-06-15 02:13:15 +02:00
|
|
|
referred to more efficiently than emitting the respective ARGB values
|
|
|
|
independently or sending them as backward references with a length of
|
2012-06-15 00:14:14 +02:00
|
|
|
one pixel.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
Color cache codes are coded as follows. First, there is a bit that
|
|
|
|
indicates if the color cache is used or not. If this bit is 0, no color
|
|
|
|
cache codes exist, and they are not transmitted in the Huffman code that
|
2012-06-15 00:14:14 +02:00
|
|
|
decodes the green symbols and the length prefix codes. However, if this
|
|
|
|
bit is 1, the color cache size is read:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int color_cache_code_bits = ReadBits(br, 4);
|
|
|
|
int color_cache_size = 1 << color_cache_code_bits;
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
`color_cache_code_bits` defines the size of the color_cache by (1 <<
|
|
|
|
`color_cache_code_bits`). The range of allowed values for
|
|
|
|
`color_cache_code_bits` is [1..11]. Compliant decoders must indicate a
|
|
|
|
corrupted bitstream for other values.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
A color cache is an array of the size `color_cache_size`. Each entry
|
2012-06-15 00:14:14 +02:00
|
|
|
stores one ARGB color. Colors are looked up by indexing them by
|
2012-06-15 02:13:15 +02:00
|
|
|
(0x1e35a7bd * `color`) >> (32 - `color_cache_code_bits`). Only one
|
|
|
|
lookup is done in a color cache; there is no conflict resolution.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
In the beginning of decoding or encoding of an image, all entries in all
|
|
|
|
color cache values are set to zero. The color cache code is converted to
|
2012-06-15 00:14:14 +02:00
|
|
|
this color at decoding time. The state of the color cache is maintained
|
|
|
|
by inserting every pixel, be it produced by backward referencing or as
|
2012-05-26 02:11:02 +02:00
|
|
|
literals, into the cache in the order they appear in the stream.
|
|
|
|
|
|
|
|
|
2012-06-15 00:59:26 +02:00
|
|
|
5 Entropy Code
|
|
|
|
--------------
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
### Huffman Coding
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
Most of the data is coded using a canonical Huffman code. This includes
|
|
|
|
the following:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
* a combined code that defines either the value of the green
|
|
|
|
component, a color cache code, or a prefix of the length codes;
|
|
|
|
* the data for alpha, red and blue components; and
|
2012-05-26 02:11:02 +02:00
|
|
|
* prefixes of the distance codes.
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
The Huffman codes are transmitted by sending the code lengths; the
|
2012-06-15 00:14:14 +02:00
|
|
|
actual symbols are implicit and done in order for each length. The
|
|
|
|
Huffman code lengths are run-length-encoded using three different
|
|
|
|
prefixes, and the result of this coding is further Huffman coded.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
### Spatially-variant Huffman Coding
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
For every pixel (x, y) in the image, there is a definition of which
|
|
|
|
entropy code to use. First, there is an integer called 'meta Huffman
|
2012-07-02 22:31:09 +02:00
|
|
|
code' that can be obtained from the entropy image. This
|
2012-06-15 00:14:14 +02:00
|
|
|
meta Huffman code identifies a set of five Huffman codes, one for green
|
|
|
|
(along with length codes and color cache codes), one for each of red,
|
|
|
|
blue and alpha, and one for distance. The Huffman codes are identified
|
|
|
|
by their position in a table by an integer.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
|
|
|
|
### Decoding Flow of Image Data
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
Read next symbol S
|
|
|
|
|
|
|
|
1. S < 256
|
|
|
|
1. Use S as green component
|
|
|
|
2. read alpha
|
|
|
|
3. read red
|
|
|
|
4. read blue
|
|
|
|
2. S < 256 + 24
|
|
|
|
1. Use S - 256 as a length prefix code
|
|
|
|
2. read length extra bits
|
|
|
|
3. read distance prefix code
|
|
|
|
4. read distance extra bits
|
|
|
|
3. S >= 256 + 24
|
|
|
|
1. Use ARGB color from the color cache, at index S - 256 + 24
|
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
### Decoding the Code Lengths
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
There are two different ways to encode the code lengths of a Huffman
|
|
|
|
code, indicated by the first bit of the code: _simple code length code_
|
|
|
|
(1), and _normal code length code_ (0).
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
#### Simple Code Length Code
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
This variant can codify 1 or 2 non-zero length codes in the range of [0,
|
|
|
|
255]. All other code lengths are implicitly zeros.
|
|
|
|
|
|
|
|
The first bit indicates the number of codes:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int num_symbols = ReadBits(1) + 1;
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
The first symbol is stored either using a 1-bit code for values of 0 and
|
|
|
|
1, or using a 8-bit code for values in range [0, 255]. The second
|
|
|
|
symbol, when present, is coded as an 8-bit code.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int first_symbol_len_code = VP8LReadBits(br, 1);
|
|
|
|
symbols[0] = ReadBits(1 + 7 * first_symbol_len_code);
|
|
|
|
if (num_symbols == 2) {
|
|
|
|
symbols[1] = ReadBits(8);
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Empty trees can be coded as trees that contain one 0 symbol, and can be
|
|
|
|
codified using four bits. For example, a distance tree can be empty if
|
2012-06-15 00:14:14 +02:00
|
|
|
there are no backward references. Similarly, alpha, red, and blue trees
|
|
|
|
can be empty if all pixels within the same meta Huffman code are
|
|
|
|
produced using the color cache.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
#### Normal Code Length Code
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
The code lengths of a Huffman code are read as follows: `num_codes`
|
|
|
|
specifies the number of code lengths; the rest of the code lengths
|
|
|
|
(according to the order in `kCodeLengthCodeOrder`) are zeros.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int kCodeLengthCodes = 19;
|
|
|
|
int kCodeLengthCodeOrder[kCodeLengthCodes] = {
|
|
|
|
17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
|
|
|
|
};
|
|
|
|
int num_codes = 4 + ReadStream(4);
|
|
|
|
for (i = 0; i < num_codes; ++i) {
|
|
|
|
code_lengths[kCodeLengthCodeOrder[i]] = ReadBits(3);
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
* Code length code [0..15] indicates literal code lengths.
|
|
|
|
* Value 0 means no symbols have been coded.
|
|
|
|
* Values [1..15] indicate the bit length of the respective code.
|
2012-06-15 00:14:14 +02:00
|
|
|
* Code 16 repeats the previous non-zero value [3..6] times, i.e.,
|
2012-06-15 02:13:15 +02:00
|
|
|
3 + `ReadStream(2)` times. If code 16 is used before a non-zero
|
|
|
|
value has been emitted, a value of 8 is repeated.
|
|
|
|
* Code 17 emits a streak of zeros [3..10], i.e., 3 + `ReadStream(3)`
|
|
|
|
times.
|
2012-06-15 00:14:14 +02:00
|
|
|
* Code 18 emits a streak of zeros of length [11..138], i.e.,
|
2012-06-15 02:13:15 +02:00
|
|
|
11 + `ReadStream(7)` times.
|
2012-06-15 00:14:14 +02:00
|
|
|
|
|
|
|
The entropy codes for alpha, red and blue have a total of 256 symbols.
|
|
|
|
The entropy code for distance prefix codes has 40 symbols. The entropy
|
2012-06-15 02:13:15 +02:00
|
|
|
code for green has 256 + 24 + `color_cache_size`, 256 symbols for
|
2012-06-15 00:14:14 +02:00
|
|
|
different green symbols, 24 length code prefix symbols, and symbols for
|
|
|
|
the color cache.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
The meta Huffman code, specified in the next section, defines how many
|
|
|
|
Huffman codes there are. There are always 5 times the number of Huffman
|
|
|
|
codes to the number of meta Huffman codes.
|
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
### Decoding of Meta Huffman Codes
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-14 21:03:55 +02:00
|
|
|
There are two ways to code the meta Huffman codes, indicated by one bit
|
|
|
|
for the ARGB image and is an implicit zero, i.e., not present in the
|
2012-07-02 22:31:09 +02:00
|
|
|
stream for all transform images and the entropy image itself.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
If this bit is zero, there is only one meta Huffman code, using Huffman
|
|
|
|
codes 0, 1, 2, 3 and 4 for green, alpha, red, blue and distance,
|
|
|
|
respectively. This meta Huffman code is used everywhere in the image.
|
|
|
|
|
|
|
|
If this bit is one, the meta Huffman codes are controlled by the entropy
|
2012-06-15 00:14:14 +02:00
|
|
|
image, where the index of the meta Huffman code is codified in the red
|
|
|
|
and green components. The index can be obtained from the uint32 value by
|
|
|
|
_((pixel >> 8) & 0xffff)_, thus there can be up to 65536 unique meta
|
|
|
|
Huffman codes. When decoding a Huffman encoded symbol at a pixel x, y,
|
|
|
|
one chooses the meta Huffman code respective to these coordinates.
|
|
|
|
However, not all bits of the coordinates are used for choosing the meta
|
|
|
|
Huffman code, i.e., the entropy image is of subresolution to the real
|
|
|
|
image.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-06-15 02:13:15 +02:00
|
|
|
int huffman_bits = ReadBits(3) + 2;
|
2012-05-26 02:11:02 +02:00
|
|
|
int huffman_xsize = DIV_ROUND_UP(xsize, 1 << huffman_bits);
|
|
|
|
int huffman_ysize = DIV_ROUND_UP(ysize, 1 << huffman_bits);
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
`huffman_bits` gives the amount of subsampling in the entropy image.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
After reading the `huffman_bits`, an entropy image stream of size
|
|
|
|
`huffman_xsize`, `huffman_ysize` is read.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
The meta Huffman code, identifying the five Huffman codes per meta
|
|
|
|
Huffman code, is coded only by the number of codes:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
int num_meta_codes = max(entropy_image) + 1;
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 00:14:14 +02:00
|
|
|
Now, we can obtain the five Huffman codes for green, alpha, red, blue
|
|
|
|
and distance for a given (x, y) by the following expression:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
meta_codes[(entropy_image[(y >> huffman_bits) * huffman_xsize +
|
|
|
|
(x >> huffman_bits)] >> 8) & 0xffff]
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
The `huffman_code[5 * meta_code + k]`, codes with _k_ == 0 are for the
|
2012-06-15 00:14:14 +02:00
|
|
|
green & length code, _k_ == 4 for the distance code, and the codes at
|
|
|
|
_k_ == 1, 2, and 3, are for codes of length 256 for red, blue and alpha,
|
|
|
|
respectively.
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
The value of _k_ for the reference position in `meta_code` determines the
|
2012-05-26 02:11:02 +02:00
|
|
|
length of the Huffman code:
|
|
|
|
|
|
|
|
* k = 0; length = 256 + 24 + cache_size
|
|
|
|
* k = 1, 2, or 3; length = 256
|
|
|
|
* k = 4, length = 40.
|
|
|
|
|
|
|
|
|
2012-06-15 00:59:26 +02:00
|
|
|
6 Overall Structure of the Format
|
|
|
|
---------------------------------
|
2012-05-26 02:11:02 +02:00
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
Below is a view into the format in Backus-Naur form. It does not cover
|
|
|
|
all details. End-of-image (EOI) is only implicitly coded into the number
|
|
|
|
of pixels (xsize * ysize).
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
#### Basic Structure
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
<format> ::= <RIFF header><image size><image stream>
|
2012-07-02 22:31:09 +02:00
|
|
|
<image stream> ::= (<optional-transform><image stream>);
|
|
|
|
<spatially-coded image>
|
2012-05-26 02:11:02 +02:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
#### Structure of Transforms
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
<optional-transform> ::= 1-bit <transform> <optional-transform> | 0-bit
|
|
|
|
<transform> ::= <predictor-tx> | <color-tx> | <subtract-green-tx> |
|
|
|
|
<color-indexing-tx>
|
2012-07-02 22:31:09 +02:00
|
|
|
<predictor-tx> ::= 2-bit value 0; <predictor image>
|
|
|
|
<predictor image> ::= 3-bit sub-pixel code | <entropy-coded image>
|
|
|
|
<color-tx> ::= 2-bit value 1; <color image>
|
|
|
|
<color image> ::= 3-bit sub-pixel code | <entropy-coded image>
|
2012-05-26 02:11:02 +02:00
|
|
|
<subtract-green-tx> ::= 2-bit value 2
|
2012-07-02 22:31:09 +02:00
|
|
|
<color-indexing-tx> ::= 2-bit value 3; <color-indexing image>
|
|
|
|
<color-indexing image> ::= 8-bit color count; <entropy-coded image>
|
2012-05-26 02:11:02 +02:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
#### Structure of the Image Data
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-07-02 22:31:09 +02:00
|
|
|
<spatially-coded image> ::= <meta huffman><entropy-coded image>
|
|
|
|
<entropy-coded image> ::= <color cache info><huffman codes><lz77-coded image>
|
|
|
|
<meta huffman> ::= 1-bit value 0 |
|
|
|
|
(1-bit value 1; <entropy image>)
|
|
|
|
<entropy image> ::= 3-bit subsample value; <entropy-coded image>
|
2012-06-15 02:13:15 +02:00
|
|
|
<color cache info> ::= 1 bit value 0 |
|
2012-05-26 02:11:02 +02:00
|
|
|
(1-bit value 1; 4-bit value for color cache size)
|
2012-06-15 02:13:15 +02:00
|
|
|
<huffman codes> ::= <huffman code> | <huffman code><huffman codes>
|
2012-05-26 02:11:02 +02:00
|
|
|
<huffman code> ::= <simple huffman code> | <normal huffman code>
|
2012-05-31 22:04:19 +02:00
|
|
|
<simple huffman code> ::= see "Simple code length code" for details
|
2012-05-26 02:11:02 +02:00
|
|
|
<normal huffman code> ::= <code length code>; encoded code lengths
|
2012-05-31 22:04:19 +02:00
|
|
|
<code length code> ::= see section "Normal code length code"
|
2012-05-26 02:11:02 +02:00
|
|
|
<lz77-coded image> ::= (<argb-pixel> | <color-cache-code> | <lz77-copy>) |
|
|
|
|
(<lz77-coded image> | "")
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-15 02:13:15 +02:00
|
|
|
A possible example sequence:
|
2012-05-26 02:11:02 +02:00
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-06-14 21:03:55 +02:00
|
|
|
<RIFF header><image size>1-bit value 1<subtract-green-tx>
|
2012-07-02 22:31:09 +02:00
|
|
|
1-bit value 1<predictor-tx>1-bit value 0<meta huffman>
|
|
|
|
<color cache info><huffman codes>
|
2012-05-26 02:11:02 +02:00
|
|
|
<lz77-coded image>
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|