vnc-enc-zrle.c (9ba1733a7639243500fc71a31e7b86fdfeb58e7b) vnc-enc-zrle.c (6bf21f3d83e95bcc4ba35a7a07cc6655e8b010b0)
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>

--- 23 unchanged lines hidden (view full) ---

32
33static 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
38static void vnc_zrle_start(VncState *vs)
39{
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>

--- 23 unchanged lines hidden (view full) ---

32
33static 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
38static void vnc_zrle_start(VncState *vs)
39{
40 buffer_reset(&vs->zrle.zrle);
40 buffer_reset(&vs->zrle->zrle);
41
42 /* make the output buffer be the zlib buffer, so we can compress it later */
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;
43 vs->zrle->tmp = vs->output;
44 vs->output = vs->zrle->zrle;
45}
46
47static void vnc_zrle_stop(VncState *vs)
48{
49 /* switch back to normal output/zlib buffers */
45}
46
47static 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;
50 vs->zrle->zrle = vs->output;
51 vs->output = vs->zrle->tmp;
52}
53
54static void *zrle_convert_fb(VncState *vs, int x, int y, int w, int h,
55 int bpp)
56{
57 Buffer tmp;
58
52}
53
54static 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);
59 buffer_reset(&vs->zrle->fb);
60 buffer_reserve(&vs->zrle->fb, w * h * bpp + bpp);
61
62 tmp = vs->output;
61
62 tmp = vs->output;
63 vs->output = vs->zrle.fb;
63 vs->output = vs->zrle->fb;
64
65 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
66
64
65 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
66
67 vs->zrle.fb = vs->output;
67 vs->zrle->fb = vs->output;
68 vs->output = tmp;
68 vs->output = tmp;
69 return vs->zrle.fb.buffer;
69 return vs->zrle->fb.buffer;
70}
71
72static int zrle_compress_data(VncState *vs, int level)
73{
70}
71
72static int zrle_compress_data(VncState *vs, int level)
73{
74 z_streamp zstream = &vs->zrle.stream;
74 z_streamp zstream = &vs->zrle->stream;
75
75
76 buffer_reset(&vs->zrle.zlib);
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 */
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);
96 buffer_reserve(&vs->zrle->zlib, vs->zrle->zrle.offset + 64);
97
98 /* set pointers */
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;
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
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;
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... */
119static void zrle_choose_palette_rle(VncState *vs, int w, int h,
120 VncPalette *palette, int bpp_out,

--- 133 unchanged lines hidden (view full) ---

254
255static int zrle_send_framebuffer_update(VncState *vs, int x, int y,
256 int w, int h)
257{
258 bool be = vs->client_be;
259 size_t bytes;
260 int zywrle_level;
261
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... */
119static void zrle_choose_palette_rle(VncState *vs, int w, int h,
120 VncPalette *palette, int bpp_out,

--- 133 unchanged lines hidden (view full) ---

254
255static int zrle_send_framebuffer_update(VncState *vs, int x, int y,
256 int w, int h)
257{
258 bool be = vs->client_be;
259 size_t bytes;
260 int zywrle_level;
261
262 if (vs->zrle.type == VNC_ENCODING_ZYWRLE) {
263 if (!vs->vd->lossy || vs->tight.quality == (uint8_t)-1
264 || vs->tight.quality == 9) {
262 if (vs->zrle->type == VNC_ENCODING_ZYWRLE) {
263 if (!vs->vd->lossy || vs->tight->quality == (uint8_t)-1
264 || vs->tight->quality == 9) {
265 zywrle_level = 0;
265 zywrle_level = 0;
266 vs->zrle.type = VNC_ENCODING_ZRLE;
267 } else if (vs->tight.quality < 3) {
266 vs->zrle->type = VNC_ENCODING_ZRLE;
267 } else if (vs->tight->quality < 3) {
268 zywrle_level = 3;
268 zywrle_level = 3;
269 } else if (vs->tight.quality < 6) {
269 } else if (vs->tight->quality < 6) {
270 zywrle_level = 2;
271 } else {
272 zywrle_level = 1;
273 }
274 } else {
275 zywrle_level = 0;
276 }
277

--- 54 unchanged lines hidden (view full) ---

332 }
333 }
334 }
335 break;
336 }
337
338 vnc_zrle_stop(vs);
339 bytes = zrle_compress_data(vs, Z_DEFAULT_COMPRESSION);
270 zywrle_level = 2;
271 } else {
272 zywrle_level = 1;
273 }
274 } else {
275 zywrle_level = 0;
276 }
277

--- 54 unchanged lines hidden (view full) ---

332 }
333 }
334 }
335 break;
336 }
337
338 vnc_zrle_stop(vs);
339 bytes = zrle_compress_data(vs, Z_DEFAULT_COMPRESSION);
340 vnc_framebuffer_update(vs, x, y, w, h, vs->zrle.type);
340 vnc_framebuffer_update(vs, x, y, w, h, vs->zrle->type);
341 vnc_write_u32(vs, bytes);
341 vnc_write_u32(vs, bytes);
342 vnc_write(vs, vs->zrle.zlib.buffer, vs->zrle.zlib.offset);
342 vnc_write(vs, vs->zrle->zlib.buffer, vs->zrle->zlib.offset);
343 return 1;
344}
345
346int vnc_zrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
347{
343 return 1;
344}
345
346int vnc_zrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
347{
348 vs->zrle.type = VNC_ENCODING_ZRLE;
348 vs->zrle->type = VNC_ENCODING_ZRLE;
349 return zrle_send_framebuffer_update(vs, x, y, w, h);
350}
351
352int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
353{
349 return zrle_send_framebuffer_update(vs, x, y, w, h);
350}
351
352int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
353{
354 vs->zrle.type = VNC_ENCODING_ZYWRLE;
354 vs->zrle->type = VNC_ENCODING_ZYWRLE;
355 return zrle_send_framebuffer_update(vs, x, y, w, h);
356}
357
358void vnc_zrle_clear(VncState *vs)
359{
355 return zrle_send_framebuffer_update(vs, x, y, w, h);
356}
357
358void vnc_zrle_clear(VncState *vs)
359{
360 if (vs->zrle.stream.opaque) {
361 deflateEnd(&vs->zrle.stream);
360 if (vs->zrle->stream.opaque) {
361 deflateEnd(&vs->zrle->stream);
362 }
362 }
363 buffer_free(&vs->zrle.zrle);
364 buffer_free(&vs->zrle.fb);
365 buffer_free(&vs->zrle.zlib);
363 buffer_free(&vs->zrle->zrle);
364 buffer_free(&vs->zrle->fb);
365 buffer_free(&vs->zrle->zlib);
366}
366}