xref: /openbmc/qemu/ui/vnc-enc-tight.h (revision 245f7b51)
1*245f7b51SCorentin Chary /*
2*245f7b51SCorentin Chary  * QEMU VNC display driver: tight encoding
3*245f7b51SCorentin Chary  *
4*245f7b51SCorentin Chary  * From libvncserver/rfb/rfbproto.h
5*245f7b51SCorentin Chary  * Copyright (C) 2005 Rohit Kumar, Johannes E. Schindelin
6*245f7b51SCorentin Chary  * Copyright (C) 2000-2002 Constantin Kaplinsky.  All Rights Reserved.
7*245f7b51SCorentin Chary  * Copyright (C) 2000 Tridia Corporation.  All Rights Reserved.
8*245f7b51SCorentin Chary  * Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
9*245f7b51SCorentin Chary  *
10*245f7b51SCorentin Chary  *
11*245f7b51SCorentin Chary  * Permission is hereby granted, free of charge, to any person obtaining a copy
12*245f7b51SCorentin Chary  * of this software and associated documentation files (the "Software"), to deal
13*245f7b51SCorentin Chary  * in the Software without restriction, including without limitation the rights
14*245f7b51SCorentin Chary  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15*245f7b51SCorentin Chary  * copies of the Software, and to permit persons to whom the Software is
16*245f7b51SCorentin Chary  * furnished to do so, subject to the following conditions:
17*245f7b51SCorentin Chary  *
18*245f7b51SCorentin Chary  * The above copyright notice and this permission notice shall be included in
19*245f7b51SCorentin Chary  * all copies or substantial portions of the Software.
20*245f7b51SCorentin Chary  *
21*245f7b51SCorentin Chary  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22*245f7b51SCorentin Chary  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23*245f7b51SCorentin Chary  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24*245f7b51SCorentin Chary  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25*245f7b51SCorentin Chary  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26*245f7b51SCorentin Chary  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27*245f7b51SCorentin Chary  * THE SOFTWARE.
28*245f7b51SCorentin Chary  */
29*245f7b51SCorentin Chary 
30*245f7b51SCorentin Chary #ifndef VNC_ENCODING_TIGHT_H
31*245f7b51SCorentin Chary #define VNC_ENCODING_TIGHT_H
32*245f7b51SCorentin Chary 
33*245f7b51SCorentin Chary /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34*245f7b51SCorentin Chary  * Tight Encoding.
35*245f7b51SCorentin Chary  *
36*245f7b51SCorentin Chary  *-- The first byte of each Tight-encoded rectangle is a "compression control
37*245f7b51SCorentin Chary  *   byte". Its format is as follows (bit 0 is the least significant one):
38*245f7b51SCorentin Chary  *
39*245f7b51SCorentin Chary  *   bit 0:    if 1, then compression stream 0 should be reset;
40*245f7b51SCorentin Chary  *   bit 1:    if 1, then compression stream 1 should be reset;
41*245f7b51SCorentin Chary  *   bit 2:    if 1, then compression stream 2 should be reset;
42*245f7b51SCorentin Chary  *   bit 3:    if 1, then compression stream 3 should be reset;
43*245f7b51SCorentin Chary  *   bits 7-4: if 1000 (0x08), then the compression type is "fill",
44*245f7b51SCorentin Chary  *             if 1001 (0x09), then the compression type is "jpeg",
45*245f7b51SCorentin Chary  *             if 0xxx, then the compression type is "basic",
46*245f7b51SCorentin Chary  *             values greater than 1001 are not valid.
47*245f7b51SCorentin Chary  *
48*245f7b51SCorentin Chary  * If the compression type is "basic", then bits 6..4 of the
49*245f7b51SCorentin Chary  * compression control byte (those xxx in 0xxx) specify the following:
50*245f7b51SCorentin Chary  *
51*245f7b51SCorentin Chary  *   bits 5-4:  decimal representation is the index of a particular zlib
52*245f7b51SCorentin Chary  *              stream which should be used for decompressing the data;
53*245f7b51SCorentin Chary  *   bit 6:     if 1, then a "filter id" byte is following this byte.
54*245f7b51SCorentin Chary  *
55*245f7b51SCorentin Chary  *-- The data that follows after the compression control byte described
56*245f7b51SCorentin Chary  * above depends on the compression type ("fill", "jpeg" or "basic").
57*245f7b51SCorentin Chary  *
58*245f7b51SCorentin Chary  *-- If the compression type is "fill", then the only pixel value follows, in
59*245f7b51SCorentin Chary  * client pixel format (see NOTE 1). This value applies to all pixels of the
60*245f7b51SCorentin Chary  * rectangle.
61*245f7b51SCorentin Chary  *
62*245f7b51SCorentin Chary  *-- If the compression type is "jpeg", the following data stream looks like
63*245f7b51SCorentin Chary  * this:
64*245f7b51SCorentin Chary  *
65*245f7b51SCorentin Chary  *   1..3 bytes:  data size (N) in compact representation;
66*245f7b51SCorentin Chary  *   N bytes:     JPEG image.
67*245f7b51SCorentin Chary  *
68*245f7b51SCorentin Chary  * Data size is compactly represented in one, two or three bytes, according
69*245f7b51SCorentin Chary  * to the following scheme:
70*245f7b51SCorentin Chary  *
71*245f7b51SCorentin Chary  *  0xxxxxxx                    (for values 0..127)
72*245f7b51SCorentin Chary  *  1xxxxxxx 0yyyyyyy           (for values 128..16383)
73*245f7b51SCorentin Chary  *  1xxxxxxx 1yyyyyyy zzzzzzzz  (for values 16384..4194303)
74*245f7b51SCorentin Chary  *
75*245f7b51SCorentin Chary  * Here each character denotes one bit, xxxxxxx are the least significant 7
76*245f7b51SCorentin Chary  * bits of the value (bits 0-6), yyyyyyy are bits 7-13, and zzzzzzzz are the
77*245f7b51SCorentin Chary  * most significant 8 bits (bits 14-21). For example, decimal value 10000
78*245f7b51SCorentin Chary  * should be represented as two bytes: binary 10010000 01001110, or
79*245f7b51SCorentin Chary  * hexadecimal 90 4E.
80*245f7b51SCorentin Chary  *
81*245f7b51SCorentin Chary  *-- If the compression type is "basic" and bit 6 of the compression control
82*245f7b51SCorentin Chary  * byte was set to 1, then the next (second) byte specifies "filter id" which
83*245f7b51SCorentin Chary  * tells the decoder what filter type was used by the encoder to pre-process
84*245f7b51SCorentin Chary  * pixel data before the compression. The "filter id" byte can be one of the
85*245f7b51SCorentin Chary  * following:
86*245f7b51SCorentin Chary  *
87*245f7b51SCorentin Chary  *   0:  no filter ("copy" filter);
88*245f7b51SCorentin Chary  *   1:  "palette" filter;
89*245f7b51SCorentin Chary  *   2:  "gradient" filter.
90*245f7b51SCorentin Chary  *
91*245f7b51SCorentin Chary  *-- If bit 6 of the compression control byte is set to 0 (no "filter id"
92*245f7b51SCorentin Chary  * byte), or if the filter id is 0, then raw pixel values in the client
93*245f7b51SCorentin Chary  * format (see NOTE 1) will be compressed. See below details on the
94*245f7b51SCorentin Chary  * compression.
95*245f7b51SCorentin Chary  *
96*245f7b51SCorentin Chary  *-- The "gradient" filter pre-processes pixel data with a simple algorithm
97*245f7b51SCorentin Chary  * which converts each color component to a difference between a "predicted"
98*245f7b51SCorentin Chary  * intensity and the actual intensity. Such a technique does not affect
99*245f7b51SCorentin Chary  * uncompressed data size, but helps to compress photo-like images better.
100*245f7b51SCorentin Chary  * Pseudo-code for converting intensities to differences is the following:
101*245f7b51SCorentin Chary  *
102*245f7b51SCorentin Chary  *   P[i,j] := V[i-1,j] + V[i,j-1] - V[i-1,j-1];
103*245f7b51SCorentin Chary  *   if (P[i,j] < 0) then P[i,j] := 0;
104*245f7b51SCorentin Chary  *   if (P[i,j] > MAX) then P[i,j] := MAX;
105*245f7b51SCorentin Chary  *   D[i,j] := V[i,j] - P[i,j];
106*245f7b51SCorentin Chary  *
107*245f7b51SCorentin Chary  * Here V[i,j] is the intensity of a color component for a pixel at
108*245f7b51SCorentin Chary  * coordinates (i,j). MAX is the maximum value of intensity for a color
109*245f7b51SCorentin Chary  * component.
110*245f7b51SCorentin Chary  *
111*245f7b51SCorentin Chary  *-- The "palette" filter converts true-color pixel data to indexed colors
112*245f7b51SCorentin Chary  * and a palette which can consist of 2..256 colors. If the number of colors
113*245f7b51SCorentin Chary  * is 2, then each pixel is encoded in 1 bit, otherwise 8 bits is used to
114*245f7b51SCorentin Chary  * encode one pixel. 1-bit encoding is performed such way that the most
115*245f7b51SCorentin Chary  * significant bits correspond to the leftmost pixels, and each raw of pixels
116*245f7b51SCorentin Chary  * is aligned to the byte boundary. When "palette" filter is used, the
117*245f7b51SCorentin Chary  * palette is sent before the pixel data. The palette begins with an unsigned
118*245f7b51SCorentin Chary  * byte which value is the number of colors in the palette minus 1 (i.e. 1
119*245f7b51SCorentin Chary  * means 2 colors, 255 means 256 colors in the palette). Then follows the
120*245f7b51SCorentin Chary  * palette itself which consist of pixel values in client pixel format (see
121*245f7b51SCorentin Chary  * NOTE 1).
122*245f7b51SCorentin Chary  *
123*245f7b51SCorentin Chary  *-- The pixel data is compressed using the zlib library. But if the data
124*245f7b51SCorentin Chary  * size after applying the filter but before the compression is less then 12,
125*245f7b51SCorentin Chary  * then the data is sent as is, uncompressed. Four separate zlib streams
126*245f7b51SCorentin Chary  * (0..3) can be used and the decoder should read the actual stream id from
127*245f7b51SCorentin Chary  * the compression control byte (see NOTE 2).
128*245f7b51SCorentin Chary  *
129*245f7b51SCorentin Chary  * If the compression is not used, then the pixel data is sent as is,
130*245f7b51SCorentin Chary  * otherwise the data stream looks like this:
131*245f7b51SCorentin Chary  *
132*245f7b51SCorentin Chary  *   1..3 bytes:  data size (N) in compact representation;
133*245f7b51SCorentin Chary  *   N bytes:     zlib-compressed data.
134*245f7b51SCorentin Chary  *
135*245f7b51SCorentin Chary  * Data size is compactly represented in one, two or three bytes, just like
136*245f7b51SCorentin Chary  * in the "jpeg" compression method (see above).
137*245f7b51SCorentin Chary  *
138*245f7b51SCorentin Chary  *-- NOTE 1. If the color depth is 24, and all three color components are
139*245f7b51SCorentin Chary  * 8-bit wide, then one pixel in Tight encoding is always represented by
140*245f7b51SCorentin Chary  * three bytes, where the first byte is red component, the second byte is
141*245f7b51SCorentin Chary  * green component, and the third byte is blue component of the pixel color
142*245f7b51SCorentin Chary  * value. This applies to colors in palettes as well.
143*245f7b51SCorentin Chary  *
144*245f7b51SCorentin Chary  *-- NOTE 2. The decoder must reset compression streams' states before
145*245f7b51SCorentin Chary  * decoding the rectangle, if some of bits 0,1,2,3 in the compression control
146*245f7b51SCorentin Chary  * byte are set to 1. Note that the decoder must reset zlib streams even if
147*245f7b51SCorentin Chary  * the compression type is "fill" or "jpeg".
148*245f7b51SCorentin Chary  *
149*245f7b51SCorentin Chary  *-- NOTE 3. The "gradient" filter and "jpeg" compression may be used only
150*245f7b51SCorentin Chary  * when bits-per-pixel value is either 16 or 32, not 8.
151*245f7b51SCorentin Chary  *
152*245f7b51SCorentin Chary  *-- NOTE 4. The width of any Tight-encoded rectangle cannot exceed 2048
153*245f7b51SCorentin Chary  * pixels. If a rectangle is wider, it must be split into several rectangles
154*245f7b51SCorentin Chary  * and each one should be encoded separately.
155*245f7b51SCorentin Chary  *
156*245f7b51SCorentin Chary  */
157*245f7b51SCorentin Chary 
158*245f7b51SCorentin Chary #define VNC_TIGHT_EXPLICIT_FILTER       0x04
159*245f7b51SCorentin Chary #define VNC_TIGHT_FILL                  0x08
160*245f7b51SCorentin Chary #define VNC_TIGHT_JPEG                  0x09
161*245f7b51SCorentin Chary #define VNC_TIGHT_MAX_SUBENCODING       0x09
162*245f7b51SCorentin Chary 
163*245f7b51SCorentin Chary /* Filters to improve compression efficiency */
164*245f7b51SCorentin Chary #define VNC_TIGHT_FILTER_COPY             0x00
165*245f7b51SCorentin Chary #define VNC_TIGHT_FILTER_PALETTE          0x01
166*245f7b51SCorentin Chary #define VNC_TIGHT_FILTER_GRADIENT         0x02
167*245f7b51SCorentin Chary 
168*245f7b51SCorentin Chary /* Note: The following constant should not be changed. */
169*245f7b51SCorentin Chary #define VNC_TIGHT_MIN_TO_COMPRESS 12
170*245f7b51SCorentin Chary 
171*245f7b51SCorentin Chary /* The parameters below may be adjusted. */
172*245f7b51SCorentin Chary #define VNC_TIGHT_MIN_SPLIT_RECT_SIZE     4096
173*245f7b51SCorentin Chary #define VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE  2048
174*245f7b51SCorentin Chary #define VNC_TIGHT_MAX_SPLIT_TILE_SIZE       16
175*245f7b51SCorentin Chary 
176*245f7b51SCorentin Chary #define VNC_TIGHT_JPEG_MIN_RECT_SIZE      4096
177*245f7b51SCorentin Chary #define VNC_TIGHT_DETECT_SUBROW_WIDTH        7
178*245f7b51SCorentin Chary #define VNC_TIGHT_DETECT_MIN_WIDTH           8
179*245f7b51SCorentin Chary #define VNC_TIGHT_DETECT_MIN_HEIGHT          8
180*245f7b51SCorentin Chary 
181*245f7b51SCorentin Chary #endif /* VNC_ENCODING_TIGHT_H */
182