xref: /qemu/ui/vnc-enc-tight.h (revision 121d07125bb6d7079c7ebafdd3efe8c3a01cc440)
1380282b0SCorentin Chary /*
2380282b0SCorentin Chary  * QEMU VNC display driver: tight encoding
3380282b0SCorentin Chary  *
4380282b0SCorentin Chary  * From libvncserver/rfb/rfbproto.h
5380282b0SCorentin Chary  * Copyright (C) 2005 Rohit Kumar, Johannes E. Schindelin
6380282b0SCorentin Chary  * Copyright (C) 2000-2002 Constantin Kaplinsky.  All Rights Reserved.
7380282b0SCorentin Chary  * Copyright (C) 2000 Tridia Corporation.  All Rights Reserved.
8380282b0SCorentin Chary  * Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
9380282b0SCorentin Chary  *
10380282b0SCorentin Chary  *
11380282b0SCorentin Chary  * Permission is hereby granted, free of charge, to any person obtaining a copy
12380282b0SCorentin Chary  * of this software and associated documentation files (the "Software"), to deal
13380282b0SCorentin Chary  * in the Software without restriction, including without limitation the rights
14380282b0SCorentin Chary  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15380282b0SCorentin Chary  * copies of the Software, and to permit persons to whom the Software is
16380282b0SCorentin Chary  * furnished to do so, subject to the following conditions:
17380282b0SCorentin Chary  *
18380282b0SCorentin Chary  * The above copyright notice and this permission notice shall be included in
19380282b0SCorentin Chary  * all copies or substantial portions of the Software.
20380282b0SCorentin Chary  *
21380282b0SCorentin Chary  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22380282b0SCorentin Chary  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23380282b0SCorentin Chary  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24380282b0SCorentin Chary  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25380282b0SCorentin Chary  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26380282b0SCorentin Chary  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27380282b0SCorentin Chary  * THE SOFTWARE.
28380282b0SCorentin Chary  */
29380282b0SCorentin Chary 
30*121d0712SMarkus Armbruster #ifndef VNC_ENC_TIGHT_H
31*121d0712SMarkus Armbruster #define VNC_ENC_TIGHT_H
32380282b0SCorentin Chary 
33380282b0SCorentin Chary /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34380282b0SCorentin Chary  * Tight Encoding.
35380282b0SCorentin Chary  *
36380282b0SCorentin Chary  *-- The first byte of each Tight-encoded rectangle is a "compression control
37380282b0SCorentin Chary  *   byte". Its format is as follows (bit 0 is the least significant one):
38380282b0SCorentin Chary  *
39380282b0SCorentin Chary  *   bit 0:    if 1, then compression stream 0 should be reset;
40380282b0SCorentin Chary  *   bit 1:    if 1, then compression stream 1 should be reset;
41380282b0SCorentin Chary  *   bit 2:    if 1, then compression stream 2 should be reset;
42380282b0SCorentin Chary  *   bit 3:    if 1, then compression stream 3 should be reset;
43380282b0SCorentin Chary  *   bits 7-4: if 1000 (0x08), then the compression type is "fill",
44380282b0SCorentin Chary  *             if 1001 (0x09), then the compression type is "jpeg",
45efe556adSCorentin Chary  *             if 1010 (0x0A), then the compression type is "png",
46380282b0SCorentin Chary  *             if 0xxx, then the compression type is "basic",
47efe556adSCorentin Chary  *             values greater than 1010 are not valid.
48380282b0SCorentin Chary  *
49380282b0SCorentin Chary  * If the compression type is "basic", then bits 6..4 of the
50380282b0SCorentin Chary  * compression control byte (those xxx in 0xxx) specify the following:
51380282b0SCorentin Chary  *
52380282b0SCorentin Chary  *   bits 5-4:  decimal representation is the index of a particular zlib
53380282b0SCorentin Chary  *              stream which should be used for decompressing the data;
54380282b0SCorentin Chary  *   bit 6:     if 1, then a "filter id" byte is following this byte.
55380282b0SCorentin Chary  *
56380282b0SCorentin Chary  *-- The data that follows after the compression control byte described
57efe556adSCorentin Chary  * above depends on the compression type ("fill", "jpeg", "png" or "basic").
58380282b0SCorentin Chary  *
59380282b0SCorentin Chary  *-- If the compression type is "fill", then the only pixel value follows, in
60380282b0SCorentin Chary  * client pixel format (see NOTE 1). This value applies to all pixels of the
61380282b0SCorentin Chary  * rectangle.
62380282b0SCorentin Chary  *
63efe556adSCorentin Chary  *-- If the compression type is "jpeg" or "png", the following data stream
64efe556adSCorentin Chary  * looks like this:
65380282b0SCorentin Chary  *
66380282b0SCorentin Chary  *   1..3 bytes:  data size (N) in compact representation;
67efe556adSCorentin Chary  *   N bytes:     JPEG or PNG image.
68380282b0SCorentin Chary  *
69380282b0SCorentin Chary  * Data size is compactly represented in one, two or three bytes, according
70380282b0SCorentin Chary  * to the following scheme:
71380282b0SCorentin Chary  *
72380282b0SCorentin Chary  *  0xxxxxxx                    (for values 0..127)
73380282b0SCorentin Chary  *  1xxxxxxx 0yyyyyyy           (for values 128..16383)
74380282b0SCorentin Chary  *  1xxxxxxx 1yyyyyyy zzzzzzzz  (for values 16384..4194303)
75380282b0SCorentin Chary  *
76380282b0SCorentin Chary  * Here each character denotes one bit, xxxxxxx are the least significant 7
77380282b0SCorentin Chary  * bits of the value (bits 0-6), yyyyyyy are bits 7-13, and zzzzzzzz are the
78380282b0SCorentin Chary  * most significant 8 bits (bits 14-21). For example, decimal value 10000
79380282b0SCorentin Chary  * should be represented as two bytes: binary 10010000 01001110, or
80380282b0SCorentin Chary  * hexadecimal 90 4E.
81380282b0SCorentin Chary  *
82380282b0SCorentin Chary  *-- If the compression type is "basic" and bit 6 of the compression control
83380282b0SCorentin Chary  * byte was set to 1, then the next (second) byte specifies "filter id" which
84380282b0SCorentin Chary  * tells the decoder what filter type was used by the encoder to pre-process
85380282b0SCorentin Chary  * pixel data before the compression. The "filter id" byte can be one of the
86380282b0SCorentin Chary  * following:
87380282b0SCorentin Chary  *
88380282b0SCorentin Chary  *   0:  no filter ("copy" filter);
89380282b0SCorentin Chary  *   1:  "palette" filter;
90380282b0SCorentin Chary  *   2:  "gradient" filter.
91380282b0SCorentin Chary  *
92380282b0SCorentin Chary  *-- If bit 6 of the compression control byte is set to 0 (no "filter id"
93380282b0SCorentin Chary  * byte), or if the filter id is 0, then raw pixel values in the client
94380282b0SCorentin Chary  * format (see NOTE 1) will be compressed. See below details on the
95380282b0SCorentin Chary  * compression.
96380282b0SCorentin Chary  *
97380282b0SCorentin Chary  *-- The "gradient" filter pre-processes pixel data with a simple algorithm
98380282b0SCorentin Chary  * which converts each color component to a difference between a "predicted"
99380282b0SCorentin Chary  * intensity and the actual intensity. Such a technique does not affect
100380282b0SCorentin Chary  * uncompressed data size, but helps to compress photo-like images better.
101380282b0SCorentin Chary  * Pseudo-code for converting intensities to differences is the following:
102380282b0SCorentin Chary  *
103380282b0SCorentin Chary  *   P[i,j] := V[i-1,j] + V[i,j-1] - V[i-1,j-1];
104380282b0SCorentin Chary  *   if (P[i,j] < 0) then P[i,j] := 0;
105380282b0SCorentin Chary  *   if (P[i,j] > MAX) then P[i,j] := MAX;
106380282b0SCorentin Chary  *   D[i,j] := V[i,j] - P[i,j];
107380282b0SCorentin Chary  *
108380282b0SCorentin Chary  * Here V[i,j] is the intensity of a color component for a pixel at
109380282b0SCorentin Chary  * coordinates (i,j). MAX is the maximum value of intensity for a color
110380282b0SCorentin Chary  * component.
111380282b0SCorentin Chary  *
112380282b0SCorentin Chary  *-- The "palette" filter converts true-color pixel data to indexed colors
113380282b0SCorentin Chary  * and a palette which can consist of 2..256 colors. If the number of colors
114380282b0SCorentin Chary  * is 2, then each pixel is encoded in 1 bit, otherwise 8 bits is used to
115380282b0SCorentin Chary  * encode one pixel. 1-bit encoding is performed such way that the most
116380282b0SCorentin Chary  * significant bits correspond to the leftmost pixels, and each raw of pixels
117380282b0SCorentin Chary  * is aligned to the byte boundary. When "palette" filter is used, the
118380282b0SCorentin Chary  * palette is sent before the pixel data. The palette begins with an unsigned
119380282b0SCorentin Chary  * byte which value is the number of colors in the palette minus 1 (i.e. 1
120380282b0SCorentin Chary  * means 2 colors, 255 means 256 colors in the palette). Then follows the
121380282b0SCorentin Chary  * palette itself which consist of pixel values in client pixel format (see
122380282b0SCorentin Chary  * NOTE 1).
123380282b0SCorentin Chary  *
124380282b0SCorentin Chary  *-- The pixel data is compressed using the zlib library. But if the data
125380282b0SCorentin Chary  * size after applying the filter but before the compression is less then 12,
126380282b0SCorentin Chary  * then the data is sent as is, uncompressed. Four separate zlib streams
127380282b0SCorentin Chary  * (0..3) can be used and the decoder should read the actual stream id from
128380282b0SCorentin Chary  * the compression control byte (see NOTE 2).
129380282b0SCorentin Chary  *
130380282b0SCorentin Chary  * If the compression is not used, then the pixel data is sent as is,
131380282b0SCorentin Chary  * otherwise the data stream looks like this:
132380282b0SCorentin Chary  *
133380282b0SCorentin Chary  *   1..3 bytes:  data size (N) in compact representation;
134380282b0SCorentin Chary  *   N bytes:     zlib-compressed data.
135380282b0SCorentin Chary  *
136380282b0SCorentin Chary  * Data size is compactly represented in one, two or three bytes, just like
137380282b0SCorentin Chary  * in the "jpeg" compression method (see above).
138380282b0SCorentin Chary  *
139380282b0SCorentin Chary  *-- NOTE 1. If the color depth is 24, and all three color components are
140380282b0SCorentin Chary  * 8-bit wide, then one pixel in Tight encoding is always represented by
141380282b0SCorentin Chary  * three bytes, where the first byte is red component, the second byte is
142380282b0SCorentin Chary  * green component, and the third byte is blue component of the pixel color
143380282b0SCorentin Chary  * value. This applies to colors in palettes as well.
144380282b0SCorentin Chary  *
145380282b0SCorentin Chary  *-- NOTE 2. The decoder must reset compression streams' states before
146380282b0SCorentin Chary  * decoding the rectangle, if some of bits 0,1,2,3 in the compression control
147380282b0SCorentin Chary  * byte are set to 1. Note that the decoder must reset zlib streams even if
148efe556adSCorentin Chary  * the compression type is "fill", "jpeg" or "png".
149380282b0SCorentin Chary  *
150380282b0SCorentin Chary  *-- NOTE 3. The "gradient" filter and "jpeg" compression may be used only
151380282b0SCorentin Chary  * when bits-per-pixel value is either 16 or 32, not 8.
152380282b0SCorentin Chary  *
153380282b0SCorentin Chary  *-- NOTE 4. The width of any Tight-encoded rectangle cannot exceed 2048
154380282b0SCorentin Chary  * pixels. If a rectangle is wider, it must be split into several rectangles
155380282b0SCorentin Chary  * and each one should be encoded separately.
156380282b0SCorentin Chary  *
157380282b0SCorentin Chary  */
158380282b0SCorentin Chary 
159380282b0SCorentin Chary #define VNC_TIGHT_EXPLICIT_FILTER       0x04
160380282b0SCorentin Chary #define VNC_TIGHT_FILL                  0x08
161380282b0SCorentin Chary #define VNC_TIGHT_JPEG                  0x09
162efe556adSCorentin Chary #define VNC_TIGHT_PNG                   0x0A
163efe556adSCorentin Chary #define VNC_TIGHT_MAX_SUBENCODING       0x0A
164380282b0SCorentin Chary 
165380282b0SCorentin Chary /* Filters to improve compression efficiency */
166380282b0SCorentin Chary #define VNC_TIGHT_FILTER_COPY             0x00
167380282b0SCorentin Chary #define VNC_TIGHT_FILTER_PALETTE          0x01
168380282b0SCorentin Chary #define VNC_TIGHT_FILTER_GRADIENT         0x02
169380282b0SCorentin Chary 
170380282b0SCorentin Chary /* Note: The following constant should not be changed. */
171380282b0SCorentin Chary #define VNC_TIGHT_MIN_TO_COMPRESS 12
172380282b0SCorentin Chary 
173380282b0SCorentin Chary /* The parameters below may be adjusted. */
174380282b0SCorentin Chary #define VNC_TIGHT_MIN_SPLIT_RECT_SIZE     4096
175380282b0SCorentin Chary #define VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE  2048
176380282b0SCorentin Chary #define VNC_TIGHT_MAX_SPLIT_TILE_SIZE       16
177380282b0SCorentin Chary 
1782f6f5c7aSCorentin Chary #define VNC_TIGHT_JPEG_MIN_RECT_SIZE      4096
1792f6f5c7aSCorentin Chary #define VNC_TIGHT_DETECT_SUBROW_WIDTH        7
1802f6f5c7aSCorentin Chary #define VNC_TIGHT_DETECT_MIN_WIDTH           8
1812f6f5c7aSCorentin Chary #define VNC_TIGHT_DETECT_MIN_HEIGHT          8
1822f6f5c7aSCorentin Chary 
183*121d0712SMarkus Armbruster #endif /* VNC_ENC_TIGHT_H */
184