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
29e16f4c87SPeter Maydell #include "qemu/osdep.h"
30148954faSCorentin Chary #include "vnc.h"
31148954faSCorentin Chary #include "vnc-enc-zrle.h"
32148954faSCorentin Chary
33148954faSCorentin Chary static const int bits_per_packed_pixel[] = {
34148954faSCorentin Chary 0, 1, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
35148954faSCorentin Chary };
36148954faSCorentin Chary
37148954faSCorentin Chary
vnc_zrle_start(VncState * vs)38148954faSCorentin Chary static void vnc_zrle_start(VncState *vs)
39148954faSCorentin Chary {
406bf21f3dSLi Qiang buffer_reset(&vs->zrle->zrle);
41148954faSCorentin Chary
42148954faSCorentin Chary /* make the output buffer be the zlib buffer, so we can compress it later */
436bf21f3dSLi Qiang vs->zrle->tmp = vs->output;
446bf21f3dSLi Qiang vs->output = vs->zrle->zrle;
45148954faSCorentin Chary }
46148954faSCorentin Chary
vnc_zrle_stop(VncState * vs)47148954faSCorentin Chary static void vnc_zrle_stop(VncState *vs)
48148954faSCorentin Chary {
49148954faSCorentin Chary /* switch back to normal output/zlib buffers */
506bf21f3dSLi Qiang vs->zrle->zrle = vs->output;
516bf21f3dSLi Qiang vs->output = vs->zrle->tmp;
52148954faSCorentin Chary }
53148954faSCorentin Chary
zrle_convert_fb(VncState * vs,int x,int y,int w,int h,int bpp)54148954faSCorentin Chary static void *zrle_convert_fb(VncState *vs, int x, int y, int w, int h,
55148954faSCorentin Chary int bpp)
56148954faSCorentin Chary {
57148954faSCorentin Chary Buffer tmp;
58148954faSCorentin Chary
596bf21f3dSLi Qiang buffer_reset(&vs->zrle->fb);
606bf21f3dSLi Qiang buffer_reserve(&vs->zrle->fb, w * h * bpp + bpp);
61148954faSCorentin Chary
62148954faSCorentin Chary tmp = vs->output;
636bf21f3dSLi Qiang vs->output = vs->zrle->fb;
64148954faSCorentin Chary
65148954faSCorentin Chary vnc_raw_send_framebuffer_update(vs, x, y, w, h);
66148954faSCorentin Chary
676bf21f3dSLi Qiang vs->zrle->fb = vs->output;
68148954faSCorentin Chary vs->output = tmp;
696bf21f3dSLi Qiang return vs->zrle->fb.buffer;
70148954faSCorentin Chary }
71148954faSCorentin Chary
zrle_compress_data(VncState * vs,int level)72148954faSCorentin Chary static int zrle_compress_data(VncState *vs, int level)
73148954faSCorentin Chary {
746bf21f3dSLi Qiang z_streamp zstream = &vs->zrle->stream;
75148954faSCorentin Chary
766bf21f3dSLi Qiang buffer_reset(&vs->zrle->zlib);
77148954faSCorentin Chary
78148954faSCorentin Chary if (zstream->opaque != vs) {
79148954faSCorentin Chary int err;
80148954faSCorentin Chary
81148954faSCorentin Chary zstream->zalloc = vnc_zlib_zalloc;
82148954faSCorentin Chary zstream->zfree = vnc_zlib_zfree;
83148954faSCorentin Chary
84148954faSCorentin Chary err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
85148954faSCorentin Chary MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
86148954faSCorentin Chary
87148954faSCorentin Chary if (err != Z_OK) {
88148954faSCorentin Chary fprintf(stderr, "VNC: error initializing zlib\n");
89148954faSCorentin Chary return -1;
90148954faSCorentin Chary }
91148954faSCorentin Chary
92148954faSCorentin Chary zstream->opaque = vs;
93148954faSCorentin Chary }
94148954faSCorentin Chary
95148954faSCorentin Chary /* reserve memory in output buffer */
966bf21f3dSLi Qiang buffer_reserve(&vs->zrle->zlib, vs->zrle->zrle.offset + 64);
97148954faSCorentin Chary
98148954faSCorentin Chary /* set pointers */
996bf21f3dSLi Qiang zstream->next_in = vs->zrle->zrle.buffer;
1006bf21f3dSLi Qiang zstream->avail_in = vs->zrle->zrle.offset;
101557ba0e5SCameron Esfahani zstream->next_out = vs->zrle->zlib.buffer;
102557ba0e5SCameron Esfahani zstream->avail_out = vs->zrle->zlib.capacity;
103148954faSCorentin Chary zstream->data_type = Z_BINARY;
104148954faSCorentin Chary
105148954faSCorentin Chary /* start encoding */
106148954faSCorentin Chary if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
107148954faSCorentin Chary fprintf(stderr, "VNC: error during zrle compression\n");
108148954faSCorentin Chary return -1;
109148954faSCorentin Chary }
110148954faSCorentin Chary
1116bf21f3dSLi Qiang vs->zrle->zlib.offset = vs->zrle->zlib.capacity - zstream->avail_out;
1126bf21f3dSLi Qiang return vs->zrle->zlib.offset;
113148954faSCorentin Chary }
114148954faSCorentin Chary
115148954faSCorentin Chary /* Try to work out whether to use RLE and/or a palette. We do this by
116148954faSCorentin Chary * estimating the number of bytes which will be generated and picking the
117148954faSCorentin Chary * method which results in the fewest bytes. Of course this may not result
118148954faSCorentin Chary * in the fewest bytes after compression... */
zrle_choose_palette_rle(VncState * vs,int w,int h,VncPalette * palette,int bpp_out,int runs,int single_pixels,int zywrle_level,bool * use_rle,bool * use_palette)119148954faSCorentin Chary static void zrle_choose_palette_rle(VncState *vs, int w, int h,
120148954faSCorentin Chary VncPalette *palette, int bpp_out,
121148954faSCorentin Chary int runs, int single_pixels,
122148954faSCorentin Chary int zywrle_level,
123148954faSCorentin Chary bool *use_rle, bool *use_palette)
124148954faSCorentin Chary {
125148954faSCorentin Chary size_t estimated_bytes;
126148954faSCorentin Chary size_t plain_rle_bytes;
127148954faSCorentin Chary
128148954faSCorentin Chary *use_palette = *use_rle = false;
129148954faSCorentin Chary
130148954faSCorentin Chary estimated_bytes = w * h * (bpp_out / 8); /* start assuming raw */
131148954faSCorentin Chary
132148954faSCorentin Chary if (bpp_out != 8) {
133148954faSCorentin Chary if (zywrle_level > 0 && !(zywrle_level & 0x80))
134148954faSCorentin Chary estimated_bytes >>= zywrle_level;
135148954faSCorentin Chary }
136148954faSCorentin Chary
137148954faSCorentin Chary plain_rle_bytes = ((bpp_out / 8) + 1) * (runs + single_pixels);
138148954faSCorentin Chary
139148954faSCorentin Chary if (plain_rle_bytes < estimated_bytes) {
140148954faSCorentin Chary *use_rle = true;
141148954faSCorentin Chary estimated_bytes = plain_rle_bytes;
142148954faSCorentin Chary }
143148954faSCorentin Chary
144148954faSCorentin Chary if (palette_size(palette) < 128) {
145148954faSCorentin Chary int palette_rle_bytes;
146148954faSCorentin Chary
147148954faSCorentin Chary palette_rle_bytes = (bpp_out / 8) * palette_size(palette);
148148954faSCorentin Chary palette_rle_bytes += 2 * runs + single_pixels;
149148954faSCorentin Chary
150148954faSCorentin Chary if (palette_rle_bytes < estimated_bytes) {
151148954faSCorentin Chary *use_rle = true;
152148954faSCorentin Chary *use_palette = true;
153148954faSCorentin Chary estimated_bytes = palette_rle_bytes;
154148954faSCorentin Chary }
155148954faSCorentin Chary
156148954faSCorentin Chary if (palette_size(palette) < 17) {
157148954faSCorentin Chary int packed_bytes;
158148954faSCorentin Chary
159148954faSCorentin Chary packed_bytes = (bpp_out / 8) * palette_size(palette);
160148954faSCorentin Chary packed_bytes += w * h *
161148954faSCorentin Chary bits_per_packed_pixel[palette_size(palette)-1] / 8;
162148954faSCorentin Chary
163148954faSCorentin Chary if (packed_bytes < estimated_bytes) {
164148954faSCorentin Chary *use_rle = false;
165148954faSCorentin Chary *use_palette = true;
166148954faSCorentin Chary }
167148954faSCorentin Chary }
168148954faSCorentin Chary }
169148954faSCorentin Chary }
170148954faSCorentin Chary
zrle_write_u32(VncState * vs,uint32_t value)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
zrle_write_u24a(VncState * vs,uint32_t value)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
zrle_write_u24b(VncState * vs,uint32_t value)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
zrle_write_u16(VncState * vs,uint16_t value)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
zrle_write_u8(VncState * vs,uint8_t value)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
202*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
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
208*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
209148954faSCorentin Chary
210148954faSCorentin Chary #undef ZYWRLE_ENDIAN
211148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
212*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
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
218*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
219148954faSCorentin Chary
220148954faSCorentin Chary #undef ZYWRLE_ENDIAN
221148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
222*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
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
228*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
229148954faSCorentin Chary
230148954faSCorentin Chary #undef ZYWRLE_ENDIAN
231148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
232*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
233148954faSCorentin Chary
234148954faSCorentin Chary #define ZRLE_COMPACT_PIXEL 24a
235148954faSCorentin Chary #undef ZYWRLE_ENDIAN
236148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_LITTLE
237*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
238148954faSCorentin Chary
239148954faSCorentin Chary #undef ZYWRLE_ENDIAN
240148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
241*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
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
247*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
248148954faSCorentin Chary
249148954faSCorentin Chary #undef ZYWRLE_ENDIAN
250148954faSCorentin Chary #define ZYWRLE_ENDIAN ENDIAN_BIG
251*139c1837SPaolo Bonzini #include "vnc-enc-zrle.c.inc"
252148954faSCorentin Chary #undef ZRLE_COMPACT_PIXEL
253148954faSCorentin Chary #undef ZRLE_BPP
254148954faSCorentin Chary
zrle_send_framebuffer_update(VncState * vs,int x,int y,int w,int h)255148954faSCorentin Chary static int zrle_send_framebuffer_update(VncState *vs, int x, int y,
256148954faSCorentin Chary int w, int h)
257148954faSCorentin Chary {
2589f64916dSGerd Hoffmann bool be = vs->client_be;
259148954faSCorentin Chary size_t bytes;
260148954faSCorentin Chary int zywrle_level;
261148954faSCorentin Chary
2626bf21f3dSLi Qiang if (vs->zrle->type == VNC_ENCODING_ZYWRLE) {
2636bf21f3dSLi Qiang if (!vs->vd->lossy || vs->tight->quality == (uint8_t)-1
2646bf21f3dSLi Qiang || vs->tight->quality == 9) {
265148954faSCorentin Chary zywrle_level = 0;
2666bf21f3dSLi Qiang vs->zrle->type = VNC_ENCODING_ZRLE;
2676bf21f3dSLi Qiang } else if (vs->tight->quality < 3) {
268148954faSCorentin Chary zywrle_level = 3;
2696bf21f3dSLi Qiang } 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
2809f64916dSGerd 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:
2869f64916dSGerd 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 =
3079f64916dSGerd Hoffmann ((vs->client_pf.rmax << vs->client_pf.rshift) < (1 << 24) &&
3089f64916dSGerd Hoffmann (vs->client_pf.gmax << vs->client_pf.gshift) < (1 << 24) &&
3099f64916dSGerd Hoffmann (vs->client_pf.bmax << vs->client_pf.bshift) < (1 << 24));
310148954faSCorentin Chary
3119f64916dSGerd Hoffmann fits_in_ms3bytes = (vs->client_pf.rshift > 7 &&
3129f64916dSGerd Hoffmann vs->client_pf.gshift > 7 &&
3139f64916dSGerd 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);
3406bf21f3dSLi Qiang vnc_framebuffer_update(vs, x, y, w, h, vs->zrle->type);
341148954faSCorentin Chary vnc_write_u32(vs, bytes);
3426bf21f3dSLi Qiang vnc_write(vs, vs->zrle->zlib.buffer, vs->zrle->zlib.offset);
343148954faSCorentin Chary return 1;
344148954faSCorentin Chary }
345148954faSCorentin Chary
vnc_zrle_send_framebuffer_update(VncState * vs,int x,int y,int w,int h)346148954faSCorentin Chary int vnc_zrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
347148954faSCorentin Chary {
3486bf21f3dSLi Qiang vs->zrle->type = VNC_ENCODING_ZRLE;
349148954faSCorentin Chary return zrle_send_framebuffer_update(vs, x, y, w, h);
350148954faSCorentin Chary }
351148954faSCorentin Chary
vnc_zywrle_send_framebuffer_update(VncState * vs,int x,int y,int w,int h)352148954faSCorentin Chary int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
353148954faSCorentin Chary {
3546bf21f3dSLi Qiang vs->zrle->type = VNC_ENCODING_ZYWRLE;
355148954faSCorentin Chary return zrle_send_framebuffer_update(vs, x, y, w, h);
356148954faSCorentin Chary }
357148954faSCorentin Chary
vnc_zrle_clear(VncState * vs)358148954faSCorentin Chary void vnc_zrle_clear(VncState *vs)
359148954faSCorentin Chary {
3606bf21f3dSLi Qiang if (vs->zrle->stream.opaque) {
3616bf21f3dSLi Qiang deflateEnd(&vs->zrle->stream);
362148954faSCorentin Chary }
3636bf21f3dSLi Qiang buffer_free(&vs->zrle->zrle);
3646bf21f3dSLi Qiang buffer_free(&vs->zrle->fb);
3656bf21f3dSLi Qiang buffer_free(&vs->zrle->zlib);
366148954faSCorentin Chary }
367