xref: /qemu/ui/vnc-enc-zrle.c (revision 7a4e543d)
1 /*
2  * QEMU VNC display driver: Zlib Run-length Encoding (ZRLE)
3  *
4  * From libvncserver/libvncserver/zrle.c
5  * Copyright (C) 2002 RealVNC Ltd.  All Rights Reserved.
6  * Copyright (C) 2003 Sun Microsystems, Inc.
7  *
8  * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  */
28 
29 #include "qemu/osdep.h"
30 #include "vnc.h"
31 #include "vnc-enc-zrle.h"
32 
33 static const int bits_per_packed_pixel[] = {
34   0, 1, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
35 };
36 
37 
38 static void vnc_zrle_start(VncState *vs)
39 {
40     buffer_reset(&vs->zrle.zrle);
41 
42     /* make the output buffer be the zlib buffer, so we can compress it later */
43     vs->zrle.tmp = vs->output;
44     vs->output = vs->zrle.zrle;
45 }
46 
47 static void vnc_zrle_stop(VncState *vs)
48 {
49     /* switch back to normal output/zlib buffers */
50     vs->zrle.zrle = vs->output;
51     vs->output = vs->zrle.tmp;
52 }
53 
54 static void *zrle_convert_fb(VncState *vs, int x, int y, int w, int h,
55                              int bpp)
56 {
57     Buffer tmp;
58 
59     buffer_reset(&vs->zrle.fb);
60     buffer_reserve(&vs->zrle.fb, w * h * bpp + bpp);
61 
62     tmp = vs->output;
63     vs->output = vs->zrle.fb;
64 
65     vnc_raw_send_framebuffer_update(vs, x, y, w, h);
66 
67     vs->zrle.fb = vs->output;
68     vs->output = tmp;
69     return vs->zrle.fb.buffer;
70 }
71 
72 static int zrle_compress_data(VncState *vs, int level)
73 {
74     z_streamp zstream = &vs->zrle.stream;
75 
76     buffer_reset(&vs->zrle.zlib);
77 
78     if (zstream->opaque != vs) {
79         int err;
80 
81         zstream->zalloc = vnc_zlib_zalloc;
82         zstream->zfree = vnc_zlib_zfree;
83 
84         err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
85                            MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
86 
87         if (err != Z_OK) {
88             fprintf(stderr, "VNC: error initializing zlib\n");
89             return -1;
90         }
91 
92         zstream->opaque = vs;
93     }
94 
95     /* reserve memory in output buffer */
96     buffer_reserve(&vs->zrle.zlib, vs->zrle.zrle.offset + 64);
97 
98     /* set pointers */
99     zstream->next_in = vs->zrle.zrle.buffer;
100     zstream->avail_in = vs->zrle.zrle.offset;
101     zstream->next_out = vs->zrle.zlib.buffer + vs->zrle.zlib.offset;
102     zstream->avail_out = vs->zrle.zlib.capacity - vs->zrle.zlib.offset;
103     zstream->data_type = Z_BINARY;
104 
105     /* start encoding */
106     if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
107         fprintf(stderr, "VNC: error during zrle compression\n");
108         return -1;
109     }
110 
111     vs->zrle.zlib.offset = vs->zrle.zlib.capacity - zstream->avail_out;
112     return vs->zrle.zlib.offset;
113 }
114 
115 /* Try to work out whether to use RLE and/or a palette.  We do this by
116  * estimating the number of bytes which will be generated and picking the
117  * method which results in the fewest bytes.  Of course this may not result
118  * in the fewest bytes after compression... */
119 static void zrle_choose_palette_rle(VncState *vs, int w, int h,
120                                     VncPalette *palette, int bpp_out,
121                                     int runs, int single_pixels,
122                                     int zywrle_level,
123                                     bool *use_rle, bool *use_palette)
124 {
125     size_t estimated_bytes;
126     size_t plain_rle_bytes;
127 
128     *use_palette = *use_rle = false;
129 
130     estimated_bytes = w * h * (bpp_out / 8); /* start assuming raw */
131 
132     if (bpp_out != 8) {
133         if (zywrle_level > 0 && !(zywrle_level & 0x80))
134             estimated_bytes >>= zywrle_level;
135     }
136 
137     plain_rle_bytes = ((bpp_out / 8) + 1) * (runs + single_pixels);
138 
139     if (plain_rle_bytes < estimated_bytes) {
140         *use_rle = true;
141         estimated_bytes = plain_rle_bytes;
142     }
143 
144     if (palette_size(palette) < 128) {
145         int palette_rle_bytes;
146 
147         palette_rle_bytes = (bpp_out / 8) * palette_size(palette);
148         palette_rle_bytes += 2 * runs + single_pixels;
149 
150         if (palette_rle_bytes < estimated_bytes) {
151             *use_rle = true;
152             *use_palette = true;
153             estimated_bytes = palette_rle_bytes;
154         }
155 
156         if (palette_size(palette) < 17) {
157             int packed_bytes;
158 
159             packed_bytes = (bpp_out / 8) * palette_size(palette);
160             packed_bytes += w * h *
161                 bits_per_packed_pixel[palette_size(palette)-1] / 8;
162 
163             if (packed_bytes < estimated_bytes) {
164                 *use_rle = false;
165                 *use_palette = true;
166                 estimated_bytes = packed_bytes;
167             }
168         }
169     }
170 }
171 
172 static void zrle_write_u32(VncState *vs, uint32_t value)
173 {
174     vnc_write(vs, (uint8_t *)&value, 4);
175 }
176 
177 static void zrle_write_u24a(VncState *vs, uint32_t value)
178 {
179     vnc_write(vs, (uint8_t *)&value, 3);
180 }
181 
182 static void zrle_write_u24b(VncState *vs, uint32_t value)
183 {
184     vnc_write(vs, ((uint8_t *)&value) + 1, 3);
185 }
186 
187 static void zrle_write_u16(VncState *vs, uint16_t value)
188 {
189     vnc_write(vs, (uint8_t *)&value, 2);
190 }
191 
192 static void zrle_write_u8(VncState *vs, uint8_t value)
193 {
194     vnc_write_u8(vs, value);
195 }
196 
197 #define ENDIAN_LITTLE 0
198 #define ENDIAN_BIG    1
199 #define ENDIAN_NO     2
200 
201 #define ZRLE_BPP 8
202 #define ZYWRLE_ENDIAN ENDIAN_NO
203 #include "vnc-enc-zrle-template.c"
204 #undef ZRLE_BPP
205 
206 #define ZRLE_BPP 15
207 #undef ZYWRLE_ENDIAN
208 #define ZYWRLE_ENDIAN ENDIAN_LITTLE
209 #include "vnc-enc-zrle-template.c"
210 
211 #undef ZYWRLE_ENDIAN
212 #define ZYWRLE_ENDIAN ENDIAN_BIG
213 #include "vnc-enc-zrle-template.c"
214 
215 #undef ZRLE_BPP
216 #define ZRLE_BPP 16
217 #undef ZYWRLE_ENDIAN
218 #define ZYWRLE_ENDIAN ENDIAN_LITTLE
219 #include "vnc-enc-zrle-template.c"
220 
221 #undef ZYWRLE_ENDIAN
222 #define ZYWRLE_ENDIAN ENDIAN_BIG
223 #include "vnc-enc-zrle-template.c"
224 
225 #undef ZRLE_BPP
226 #define ZRLE_BPP 32
227 #undef ZYWRLE_ENDIAN
228 #define ZYWRLE_ENDIAN ENDIAN_LITTLE
229 #include "vnc-enc-zrle-template.c"
230 
231 #undef ZYWRLE_ENDIAN
232 #define ZYWRLE_ENDIAN ENDIAN_BIG
233 #include "vnc-enc-zrle-template.c"
234 
235 #define ZRLE_COMPACT_PIXEL 24a
236 #undef ZYWRLE_ENDIAN
237 #define ZYWRLE_ENDIAN ENDIAN_LITTLE
238 #include "vnc-enc-zrle-template.c"
239 
240 #undef ZYWRLE_ENDIAN
241 #define ZYWRLE_ENDIAN ENDIAN_BIG
242 #include "vnc-enc-zrle-template.c"
243 
244 #undef ZRLE_COMPACT_PIXEL
245 #define ZRLE_COMPACT_PIXEL 24b
246 #undef ZYWRLE_ENDIAN
247 #define ZYWRLE_ENDIAN ENDIAN_LITTLE
248 #include "vnc-enc-zrle-template.c"
249 
250 #undef ZYWRLE_ENDIAN
251 #define ZYWRLE_ENDIAN ENDIAN_BIG
252 #include "vnc-enc-zrle-template.c"
253 #undef ZRLE_COMPACT_PIXEL
254 #undef ZRLE_BPP
255 
256 static int zrle_send_framebuffer_update(VncState *vs, int x, int y,
257                                         int w, int h)
258 {
259     bool be = vs->client_be;
260     size_t bytes;
261     int zywrle_level;
262 
263     if (vs->zrle.type == VNC_ENCODING_ZYWRLE) {
264         if (!vs->vd->lossy || vs->tight.quality == (uint8_t)-1
265             || vs->tight.quality == 9) {
266             zywrle_level = 0;
267             vs->zrle.type = VNC_ENCODING_ZRLE;
268         } else if (vs->tight.quality < 3) {
269             zywrle_level = 3;
270         } else if (vs->tight.quality < 6) {
271             zywrle_level = 2;
272         } else {
273             zywrle_level = 1;
274         }
275     } else {
276         zywrle_level = 0;
277     }
278 
279     vnc_zrle_start(vs);
280 
281     switch (vs->client_pf.bytes_per_pixel) {
282     case 1:
283         zrle_encode_8ne(vs, x, y, w, h, zywrle_level);
284         break;
285 
286     case 2:
287         if (vs->client_pf.gmax > 0x1F) {
288             if (be) {
289                 zrle_encode_16be(vs, x, y, w, h, zywrle_level);
290             } else {
291                 zrle_encode_16le(vs, x, y, w, h, zywrle_level);
292             }
293         } else {
294             if (be) {
295                 zrle_encode_15be(vs, x, y, w, h, zywrle_level);
296             } else {
297                 zrle_encode_15le(vs, x, y, w, h, zywrle_level);
298             }
299         }
300         break;
301 
302     case 4:
303     {
304         bool fits_in_ls3bytes;
305         bool fits_in_ms3bytes;
306 
307         fits_in_ls3bytes =
308             ((vs->client_pf.rmax << vs->client_pf.rshift) < (1 << 24) &&
309              (vs->client_pf.gmax << vs->client_pf.gshift) < (1 << 24) &&
310              (vs->client_pf.bmax << vs->client_pf.bshift) < (1 << 24));
311 
312         fits_in_ms3bytes = (vs->client_pf.rshift > 7 &&
313                             vs->client_pf.gshift > 7 &&
314                             vs->client_pf.bshift > 7);
315 
316         if ((fits_in_ls3bytes && !be) || (fits_in_ms3bytes && be)) {
317             if (be) {
318                 zrle_encode_24abe(vs, x, y, w, h, zywrle_level);
319             } else {
320                 zrle_encode_24ale(vs, x, y, w, h, zywrle_level);
321           }
322         } else if ((fits_in_ls3bytes && be) || (fits_in_ms3bytes && !be)) {
323             if (be) {
324                 zrle_encode_24bbe(vs, x, y, w, h, zywrle_level);
325             } else {
326                 zrle_encode_24ble(vs, x, y, w, h, zywrle_level);
327             }
328         } else {
329             if (be) {
330                 zrle_encode_32be(vs, x, y, w, h, zywrle_level);
331             } else {
332                 zrle_encode_32le(vs, x, y, w, h, zywrle_level);
333             }
334         }
335     }
336     break;
337     }
338 
339     vnc_zrle_stop(vs);
340     bytes = zrle_compress_data(vs, Z_DEFAULT_COMPRESSION);
341     vnc_framebuffer_update(vs, x, y, w, h, vs->zrle.type);
342     vnc_write_u32(vs, bytes);
343     vnc_write(vs, vs->zrle.zlib.buffer, vs->zrle.zlib.offset);
344     return 1;
345 }
346 
347 int vnc_zrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
348 {
349     vs->zrle.type = VNC_ENCODING_ZRLE;
350     return zrle_send_framebuffer_update(vs, x, y, w, h);
351 }
352 
353 int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
354 {
355     vs->zrle.type = VNC_ENCODING_ZYWRLE;
356     return zrle_send_framebuffer_update(vs, x, y, w, h);
357 }
358 
359 void vnc_zrle_clear(VncState *vs)
360 {
361     if (vs->zrle.stream.opaque) {
362         deflateEnd(&vs->zrle.stream);
363     }
364     buffer_free(&vs->zrle.zrle);
365     buffer_free(&vs->zrle.fb);
366     buffer_free(&vs->zrle.zlib);
367 }
368