xref: /openbmc/qemu/ui/vnc-enc-zrle.c (revision 9f64916da20eea67121d544698676295bbb105a7)
1148954faSCorentin Chary /*
2148954faSCorentin Chary  * QEMU VNC display driver: Zlib Run-length Encoding (ZRLE)
3148954faSCorentin Chary  *
4148954faSCorentin Chary  * From libvncserver/libvncserver/zrle.c
5148954faSCorentin Chary  * Copyright (C) 2002 RealVNC Ltd.  All Rights Reserved.
6148954faSCorentin Chary  * Copyright (C) 2003 Sun Microsystems, Inc.
7148954faSCorentin Chary  *
8148954faSCorentin Chary  * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
9148954faSCorentin Chary  *
10148954faSCorentin Chary  * Permission is hereby granted, free of charge, to any person obtaining a copy
11148954faSCorentin Chary  * of this software and associated documentation files (the "Software"), to deal
12148954faSCorentin Chary  * in the Software without restriction, including without limitation the rights
13148954faSCorentin Chary  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14148954faSCorentin Chary  * copies of the Software, and to permit persons to whom the Software is
15148954faSCorentin Chary  * furnished to do so, subject to the following conditions:
16148954faSCorentin Chary  *
17148954faSCorentin Chary  * The above copyright notice and this permission notice shall be included in
18148954faSCorentin Chary  * all copies or substantial portions of the Software.
19148954faSCorentin Chary  *
20148954faSCorentin Chary  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21148954faSCorentin Chary  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22148954faSCorentin Chary  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23148954faSCorentin Chary  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24148954faSCorentin Chary  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25148954faSCorentin Chary  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26148954faSCorentin Chary  * THE SOFTWARE.
27148954faSCorentin Chary  */
28148954faSCorentin Chary 
29148954faSCorentin Chary #include "vnc.h"
30148954faSCorentin Chary #include "vnc-enc-zrle.h"
31148954faSCorentin Chary 
32148954faSCorentin Chary static const int bits_per_packed_pixel[] = {
33148954faSCorentin Chary   0, 1, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
34148954faSCorentin Chary };
35148954faSCorentin Chary 
36148954faSCorentin Chary 
37148954faSCorentin Chary static void vnc_zrle_start(VncState *vs)
38148954faSCorentin Chary {
39148954faSCorentin Chary     buffer_reset(&vs->zrle.zrle);
40148954faSCorentin Chary 
41148954faSCorentin Chary     /* make the output buffer be the zlib buffer, so we can compress it later */
42148954faSCorentin Chary     vs->zrle.tmp = vs->output;
43148954faSCorentin Chary     vs->output = vs->zrle.zrle;
44148954faSCorentin Chary }
45148954faSCorentin Chary 
46148954faSCorentin Chary static void vnc_zrle_stop(VncState *vs)
47148954faSCorentin Chary {
48148954faSCorentin Chary     /* switch back to normal output/zlib buffers */
49148954faSCorentin Chary     vs->zrle.zrle = vs->output;
50148954faSCorentin Chary     vs->output = vs->zrle.tmp;
51148954faSCorentin Chary }
52148954faSCorentin Chary 
53148954faSCorentin Chary static void *zrle_convert_fb(VncState *vs, int x, int y, int w, int h,
54148954faSCorentin Chary                              int bpp)
55148954faSCorentin Chary {
56148954faSCorentin Chary     Buffer tmp;
57148954faSCorentin Chary 
58148954faSCorentin Chary     buffer_reset(&vs->zrle.fb);
59148954faSCorentin Chary     buffer_reserve(&vs->zrle.fb, w * h * bpp + bpp);
60148954faSCorentin Chary 
61148954faSCorentin Chary     tmp = vs->output;
62148954faSCorentin Chary     vs->output = vs->zrle.fb;
63148954faSCorentin Chary 
64148954faSCorentin Chary     vnc_raw_send_framebuffer_update(vs, x, y, w, h);
65148954faSCorentin Chary 
66148954faSCorentin Chary     vs->zrle.fb = vs->output;
67148954faSCorentin Chary     vs->output = tmp;
68148954faSCorentin Chary     return vs->zrle.fb.buffer;
69148954faSCorentin Chary }
70148954faSCorentin Chary 
71148954faSCorentin Chary static int zrle_compress_data(VncState *vs, int level)
72148954faSCorentin Chary {
73148954faSCorentin Chary     z_streamp zstream = &vs->zrle.stream;
74148954faSCorentin Chary 
75148954faSCorentin Chary     buffer_reset(&vs->zrle.zlib);
76148954faSCorentin Chary 
77148954faSCorentin Chary     if (zstream->opaque != vs) {
78148954faSCorentin Chary         int err;
79148954faSCorentin Chary 
80148954faSCorentin Chary         zstream->zalloc = vnc_zlib_zalloc;
81148954faSCorentin Chary         zstream->zfree = vnc_zlib_zfree;
82148954faSCorentin Chary 
83148954faSCorentin Chary         err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
84148954faSCorentin Chary                            MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
85148954faSCorentin Chary 
86148954faSCorentin Chary         if (err != Z_OK) {
87148954faSCorentin Chary             fprintf(stderr, "VNC: error initializing zlib\n");
88148954faSCorentin Chary             return -1;
89148954faSCorentin Chary         }
90148954faSCorentin Chary 
91148954faSCorentin Chary         zstream->opaque = vs;
92148954faSCorentin Chary     }
93148954faSCorentin Chary 
94148954faSCorentin Chary     /* reserve memory in output buffer */
95148954faSCorentin Chary     buffer_reserve(&vs->zrle.zlib, vs->zrle.zrle.offset + 64);
96148954faSCorentin Chary 
97148954faSCorentin Chary     /* set pointers */
98148954faSCorentin Chary     zstream->next_in = vs->zrle.zrle.buffer;
99148954faSCorentin Chary     zstream->avail_in = vs->zrle.zrle.offset;
100148954faSCorentin Chary     zstream->next_out = vs->zrle.zlib.buffer + vs->zrle.zlib.offset;
101148954faSCorentin Chary     zstream->avail_out = vs->zrle.zlib.capacity - vs->zrle.zlib.offset;
102148954faSCorentin Chary     zstream->data_type = Z_BINARY;
103148954faSCorentin Chary 
104148954faSCorentin Chary     /* start encoding */
105148954faSCorentin Chary     if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
106148954faSCorentin Chary         fprintf(stderr, "VNC: error during zrle compression\n");
107148954faSCorentin Chary         return -1;
108148954faSCorentin Chary     }
109148954faSCorentin Chary 
110148954faSCorentin Chary     vs->zrle.zlib.offset = vs->zrle.zlib.capacity - zstream->avail_out;
111148954faSCorentin Chary     return vs->zrle.zlib.offset;
112148954faSCorentin Chary }
113148954faSCorentin Chary 
114148954faSCorentin Chary /* Try to work out whether to use RLE and/or a palette.  We do this by
115148954faSCorentin Chary  * estimating the number of bytes which will be generated and picking the
116148954faSCorentin Chary  * method which results in the fewest bytes.  Of course this may not result
117148954faSCorentin Chary  * in the fewest bytes after compression... */
118148954faSCorentin Chary static void zrle_choose_palette_rle(VncState *vs, int w, int h,
119148954faSCorentin Chary                                     VncPalette *palette, int bpp_out,
120148954faSCorentin Chary                                     int runs, int single_pixels,
121148954faSCorentin Chary                                     int zywrle_level,
122148954faSCorentin Chary                                     bool *use_rle, bool *use_palette)
123148954faSCorentin Chary {
124148954faSCorentin Chary     size_t estimated_bytes;
125148954faSCorentin Chary     size_t plain_rle_bytes;
126148954faSCorentin Chary 
127148954faSCorentin Chary     *use_palette = *use_rle = false;
128148954faSCorentin Chary 
129148954faSCorentin Chary     estimated_bytes = w * h * (bpp_out / 8); /* start assuming raw */
130148954faSCorentin Chary 
131148954faSCorentin Chary     if (bpp_out != 8) {
132148954faSCorentin Chary         if (zywrle_level > 0 && !(zywrle_level & 0x80))
133148954faSCorentin Chary             estimated_bytes >>= zywrle_level;
134148954faSCorentin Chary     }
135148954faSCorentin Chary 
136148954faSCorentin Chary     plain_rle_bytes = ((bpp_out / 8) + 1) * (runs + single_pixels);
137148954faSCorentin Chary 
138148954faSCorentin Chary     if (plain_rle_bytes < estimated_bytes) {
139148954faSCorentin Chary         *use_rle = true;
140148954faSCorentin Chary         estimated_bytes = plain_rle_bytes;
141148954faSCorentin Chary     }
142148954faSCorentin Chary 
143148954faSCorentin Chary     if (palette_size(palette) < 128) {
144148954faSCorentin Chary         int palette_rle_bytes;
145148954faSCorentin Chary 
146148954faSCorentin Chary         palette_rle_bytes = (bpp_out / 8) * palette_size(palette);
147148954faSCorentin Chary         palette_rle_bytes += 2 * runs + single_pixels;
148148954faSCorentin Chary 
149148954faSCorentin Chary         if (palette_rle_bytes < estimated_bytes) {
150148954faSCorentin Chary             *use_rle = true;
151148954faSCorentin Chary             *use_palette = true;
152148954faSCorentin Chary             estimated_bytes = palette_rle_bytes;
153148954faSCorentin Chary         }
154148954faSCorentin Chary 
155148954faSCorentin Chary         if (palette_size(palette) < 17) {
156148954faSCorentin Chary             int packed_bytes;
157148954faSCorentin Chary 
158148954faSCorentin Chary             packed_bytes = (bpp_out / 8) * palette_size(palette);
159148954faSCorentin Chary             packed_bytes += w * h *
160148954faSCorentin Chary                 bits_per_packed_pixel[palette_size(palette)-1] / 8;
161148954faSCorentin Chary 
162148954faSCorentin Chary             if (packed_bytes < estimated_bytes) {
163148954faSCorentin Chary                 *use_rle = false;
164148954faSCorentin Chary                 *use_palette = true;
165148954faSCorentin Chary                 estimated_bytes = packed_bytes;
166148954faSCorentin Chary             }
167148954faSCorentin Chary         }
168148954faSCorentin Chary     }
169148954faSCorentin Chary }
170148954faSCorentin Chary 
171148954faSCorentin Chary static void zrle_write_u32(VncState *vs, uint32_t value)
172148954faSCorentin Chary {
173148954faSCorentin Chary     vnc_write(vs, (uint8_t *)&value, 4);
174148954faSCorentin Chary }
175148954faSCorentin Chary 
176148954faSCorentin Chary static void zrle_write_u24a(VncState *vs, uint32_t value)
177148954faSCorentin Chary {
178148954faSCorentin Chary     vnc_write(vs, (uint8_t *)&value, 3);
179148954faSCorentin Chary }
180148954faSCorentin Chary 
181148954faSCorentin Chary static void zrle_write_u24b(VncState *vs, uint32_t value)
182148954faSCorentin Chary {
183148954faSCorentin Chary     vnc_write(vs, ((uint8_t *)&value) + 1, 3);
184148954faSCorentin Chary }
185148954faSCorentin Chary 
186148954faSCorentin Chary static void zrle_write_u16(VncState *vs, uint16_t value)
187148954faSCorentin Chary {
188148954faSCorentin Chary     vnc_write(vs, (uint8_t *)&value, 2);
189148954faSCorentin Chary }
190148954faSCorentin Chary 
191148954faSCorentin Chary static void zrle_write_u8(VncState *vs, uint8_t value)
192148954faSCorentin Chary {
193148954faSCorentin Chary     vnc_write_u8(vs, value);
194148954faSCorentin Chary }
195148954faSCorentin Chary 
196148954faSCorentin Chary #define ENDIAN_LITTLE 0
197148954faSCorentin Chary #define ENDIAN_BIG    1
198148954faSCorentin Chary #define ENDIAN_NO     2
199148954faSCorentin Chary 
200148954faSCorentin Chary #define ZRLE_BPP 8
201148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_NO
202148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
203148954faSCorentin Chary #undef ZRLE_BPP
204148954faSCorentin Chary 
205148954faSCorentin Chary #define ZRLE_BPP 15
206148954faSCorentin Chary #undef ZYWRLE_ENDIAN
207148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_LITTLE
208148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
209148954faSCorentin Chary 
210148954faSCorentin Chary #undef ZYWRLE_ENDIAN
211148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
212148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
213148954faSCorentin Chary 
214148954faSCorentin Chary #undef ZRLE_BPP
215148954faSCorentin Chary #define ZRLE_BPP 16
216148954faSCorentin Chary #undef ZYWRLE_ENDIAN
217148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_LITTLE
218148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
219148954faSCorentin Chary 
220148954faSCorentin Chary #undef ZYWRLE_ENDIAN
221148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
222148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
223148954faSCorentin Chary 
224148954faSCorentin Chary #undef ZRLE_BPP
225148954faSCorentin Chary #define ZRLE_BPP 32
226148954faSCorentin Chary #undef ZYWRLE_ENDIAN
227148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_LITTLE
228148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
229148954faSCorentin Chary 
230148954faSCorentin Chary #undef ZYWRLE_ENDIAN
231148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
232148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
233148954faSCorentin Chary 
234148954faSCorentin Chary #define ZRLE_COMPACT_PIXEL 24a
235148954faSCorentin Chary #undef ZYWRLE_ENDIAN
236148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_LITTLE
237148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
238148954faSCorentin Chary 
239148954faSCorentin Chary #undef ZYWRLE_ENDIAN
240148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
241148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
242148954faSCorentin Chary 
243148954faSCorentin Chary #undef ZRLE_COMPACT_PIXEL
244148954faSCorentin Chary #define ZRLE_COMPACT_PIXEL 24b
245148954faSCorentin Chary #undef ZYWRLE_ENDIAN
246148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_LITTLE
247148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
248148954faSCorentin Chary 
249148954faSCorentin Chary #undef ZYWRLE_ENDIAN
250148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
251148954faSCorentin Chary #include "vnc-enc-zrle-template.c"
252148954faSCorentin Chary #undef ZRLE_COMPACT_PIXEL
253148954faSCorentin Chary #undef ZRLE_BPP
254148954faSCorentin Chary 
255148954faSCorentin Chary static int zrle_send_framebuffer_update(VncState *vs, int x, int y,
256148954faSCorentin Chary                                         int w, int h)
257148954faSCorentin Chary {
258*9f64916dSGerd Hoffmann     bool be = vs->client_be;
259148954faSCorentin Chary     size_t bytes;
260148954faSCorentin Chary     int zywrle_level;
261148954faSCorentin Chary 
262148954faSCorentin Chary     if (vs->zrle.type == VNC_ENCODING_ZYWRLE) {
263368d2588SCorentin Chary         if (!vs->vd->lossy || vs->tight.quality == (uint8_t)-1
264368d2588SCorentin Chary             || vs->tight.quality == 9) {
265148954faSCorentin Chary             zywrle_level = 0;
266148954faSCorentin Chary             vs->zrle.type = VNC_ENCODING_ZRLE;
267148954faSCorentin Chary         } else if (vs->tight.quality < 3) {
268148954faSCorentin Chary             zywrle_level = 3;
269148954faSCorentin Chary         } else if (vs->tight.quality < 6) {
270148954faSCorentin Chary             zywrle_level = 2;
271148954faSCorentin Chary         } else {
272148954faSCorentin Chary             zywrle_level = 1;
273148954faSCorentin Chary         }
274148954faSCorentin Chary     } else {
275148954faSCorentin Chary         zywrle_level = 0;
276148954faSCorentin Chary     }
277148954faSCorentin Chary 
278148954faSCorentin Chary     vnc_zrle_start(vs);
279148954faSCorentin Chary 
280*9f64916dSGerd Hoffmann     switch (vs->client_pf.bytes_per_pixel) {
281148954faSCorentin Chary     case 1:
282148954faSCorentin Chary         zrle_encode_8ne(vs, x, y, w, h, zywrle_level);
283148954faSCorentin Chary         break;
284148954faSCorentin Chary 
285148954faSCorentin Chary     case 2:
286*9f64916dSGerd Hoffmann         if (vs->client_pf.gmax > 0x1F) {
287148954faSCorentin Chary             if (be) {
288148954faSCorentin Chary                 zrle_encode_16be(vs, x, y, w, h, zywrle_level);
289148954faSCorentin Chary             } else {
290148954faSCorentin Chary                 zrle_encode_16le(vs, x, y, w, h, zywrle_level);
291148954faSCorentin Chary             }
292148954faSCorentin Chary         } else {
293148954faSCorentin Chary             if (be) {
294148954faSCorentin Chary                 zrle_encode_15be(vs, x, y, w, h, zywrle_level);
295148954faSCorentin Chary             } else {
296148954faSCorentin Chary                 zrle_encode_15le(vs, x, y, w, h, zywrle_level);
297148954faSCorentin Chary             }
298148954faSCorentin Chary         }
299148954faSCorentin Chary         break;
300148954faSCorentin Chary 
301148954faSCorentin Chary     case 4:
302148954faSCorentin Chary     {
303148954faSCorentin Chary         bool fits_in_ls3bytes;
304148954faSCorentin Chary         bool fits_in_ms3bytes;
305148954faSCorentin Chary 
306148954faSCorentin Chary         fits_in_ls3bytes =
307*9f64916dSGerd Hoffmann             ((vs->client_pf.rmax << vs->client_pf.rshift) < (1 << 24) &&
308*9f64916dSGerd Hoffmann              (vs->client_pf.gmax << vs->client_pf.gshift) < (1 << 24) &&
309*9f64916dSGerd Hoffmann              (vs->client_pf.bmax << vs->client_pf.bshift) < (1 << 24));
310148954faSCorentin Chary 
311*9f64916dSGerd Hoffmann         fits_in_ms3bytes = (vs->client_pf.rshift > 7 &&
312*9f64916dSGerd Hoffmann                             vs->client_pf.gshift > 7 &&
313*9f64916dSGerd Hoffmann                             vs->client_pf.bshift > 7);
314148954faSCorentin Chary 
315148954faSCorentin Chary         if ((fits_in_ls3bytes && !be) || (fits_in_ms3bytes && be)) {
316148954faSCorentin Chary             if (be) {
317148954faSCorentin Chary                 zrle_encode_24abe(vs, x, y, w, h, zywrle_level);
318148954faSCorentin Chary             } else {
319148954faSCorentin Chary                 zrle_encode_24ale(vs, x, y, w, h, zywrle_level);
320148954faSCorentin Chary           }
321148954faSCorentin Chary         } else if ((fits_in_ls3bytes && be) || (fits_in_ms3bytes && !be)) {
322148954faSCorentin Chary             if (be) {
323148954faSCorentin Chary                 zrle_encode_24bbe(vs, x, y, w, h, zywrle_level);
324148954faSCorentin Chary             } else {
325148954faSCorentin Chary                 zrle_encode_24ble(vs, x, y, w, h, zywrle_level);
326148954faSCorentin Chary             }
327148954faSCorentin Chary         } else {
328148954faSCorentin Chary             if (be) {
329148954faSCorentin Chary                 zrle_encode_32be(vs, x, y, w, h, zywrle_level);
330148954faSCorentin Chary             } else {
331148954faSCorentin Chary                 zrle_encode_32le(vs, x, y, w, h, zywrle_level);
332148954faSCorentin Chary             }
333148954faSCorentin Chary         }
334148954faSCorentin Chary     }
335148954faSCorentin Chary     break;
336148954faSCorentin Chary     }
337148954faSCorentin Chary 
338148954faSCorentin Chary     vnc_zrle_stop(vs);
339148954faSCorentin Chary     bytes = zrle_compress_data(vs, Z_DEFAULT_COMPRESSION);
340148954faSCorentin Chary     vnc_framebuffer_update(vs, x, y, w, h, vs->zrle.type);
341148954faSCorentin Chary     vnc_write_u32(vs, bytes);
342148954faSCorentin Chary     vnc_write(vs, vs->zrle.zlib.buffer, vs->zrle.zlib.offset);
343148954faSCorentin Chary     return 1;
344148954faSCorentin Chary }
345148954faSCorentin Chary 
346148954faSCorentin Chary int vnc_zrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
347148954faSCorentin Chary {
348148954faSCorentin Chary     vs->zrle.type = VNC_ENCODING_ZRLE;
349148954faSCorentin Chary     return zrle_send_framebuffer_update(vs, x, y, w, h);
350148954faSCorentin Chary }
351148954faSCorentin Chary 
352148954faSCorentin Chary int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
353148954faSCorentin Chary {
354148954faSCorentin Chary     vs->zrle.type = VNC_ENCODING_ZYWRLE;
355148954faSCorentin Chary     return zrle_send_framebuffer_update(vs, x, y, w, h);
356148954faSCorentin Chary }
357148954faSCorentin Chary 
358148954faSCorentin Chary void vnc_zrle_clear(VncState *vs)
359148954faSCorentin Chary {
360148954faSCorentin Chary     if (vs->zrle.stream.opaque) {
361148954faSCorentin Chary         deflateEnd(&vs->zrle.stream);
362148954faSCorentin Chary     }
363148954faSCorentin Chary     buffer_free(&vs->zrle.zrle);
364148954faSCorentin Chary     buffer_free(&vs->zrle.fb);
365148954faSCorentin Chary     buffer_free(&vs->zrle.zlib);
366148954faSCorentin Chary }
367