Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 960 lines (818 sloc) 36.859 kb
380282b0 »
2010-05-19 vnc: add basic tight support
1 /*
2 * QEMU VNC display driver: tight encoding
3 *
4 * From libvncserver/libvncserver/tight.c
5 * Copyright (C) 2000, 2001 Const Kaplinsky. All Rights Reserved.
6 * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
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
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
29 #include "qdict.h"
30 #include "qint.h"
380282b0 »
2010-05-19 vnc: add basic tight support
31 #include "vnc.h"
32 #include "vnc-encoding-tight.h"
33
34 /* Compression level stuff. The following array contains various
35 encoder parameters for each of 10 compression levels (0..9).
36 Last three parameters correspond to JPEG quality levels (0..9). */
37
38 static const struct {
39 int max_rect_size, max_rect_width;
40 int mono_min_rect_size, gradient_min_rect_size;
41 int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
42 int gradient_threshold, gradient_threshold24;
43 int idx_max_colors_divisor;
44 int jpeg_quality, jpeg_threshold, jpeg_threshold24;
45 } tight_conf[] = {
46 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
47 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
48 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
49 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
50 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
51 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
52 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
53 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
54 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
55 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
56 };
57
b4bea3f2 »
2010-05-19 vnc: add support for tight fill encoding
58 /*
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
59 * Code to determine how many different colors used in rectangle.
60 */
61
62 static void tight_palette_rgb2buf(uint32_t rgb, int bpp, uint8_t buf[6])
63 {
64 memset(buf, 0, 6);
65
66 if (bpp == 32) {
67 buf[0] = ((rgb >> 24) & 0xFF);
68 buf[1] = ((rgb >> 16) & 0xFF);
69 buf[2] = ((rgb >> 8) & 0xFF);
70 buf[3] = ((rgb >> 0) & 0xFF);
71 buf[4] = ((buf[0] & 1) == 0) << 3 | ((buf[1] & 1) == 0) << 2;
72 buf[4]|= ((buf[2] & 1) == 0) << 1 | ((buf[3] & 1) == 0) << 0;
73 buf[0] |= 1;
74 buf[1] |= 1;
75 buf[2] |= 1;
76 buf[3] |= 1;
77 }
78 if (bpp == 16) {
79 buf[0] = ((rgb >> 8) & 0xFF);
80 buf[1] = ((rgb >> 0) & 0xFF);
81 buf[2] = ((buf[0] & 1) == 0) << 1 | ((buf[1] & 1) == 0) << 0;
82 buf[0] |= 1;
83 buf[1] |= 1;
84 }
85 }
86
87 static uint32_t tight_palette_buf2rgb(int bpp, const uint8_t *buf)
88 {
89 uint32_t rgb = 0;
90
91 if (bpp == 32) {
92 rgb |= ((buf[0] & ~1) | !((buf[4] >> 3) & 1)) << 24;
93 rgb |= ((buf[1] & ~1) | !((buf[4] >> 2) & 1)) << 16;
94 rgb |= ((buf[2] & ~1) | !((buf[4] >> 1) & 1)) << 8;
95 rgb |= ((buf[3] & ~1) | !((buf[4] >> 0) & 1)) << 0;
96 }
97 if (bpp == 16) {
98 rgb |= ((buf[0] & ~1) | !((buf[2] >> 1) & 1)) << 8;
99 rgb |= ((buf[1] & ~1) | !((buf[2] >> 0) & 1)) << 0;
100 }
101 return rgb;
102 }
103
104
105 static int tight_palette_insert(QDict *palette, uint32_t rgb, int bpp, int max)
106 {
107 uint8_t key[6];
108 int idx = qdict_size(palette);
109 bool present;
110
111 tight_palette_rgb2buf(rgb, bpp, key);
112 present = qdict_haskey(palette, (char *)key);
113 if (idx >= max && !present) {
114 return 0;
115 }
116 if (!present) {
117 qdict_put(palette, (char *)key, qint_from_int(idx));
118 }
119 return qdict_size(palette);
120 }
121
122 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
123 \
124 static int \
125 tight_fill_palette##bpp(VncState *vs, int x, int y, \
126 int max, size_t count, \
127 uint32_t *bg, uint32_t *fg, \
128 struct QDict **palette) { \
129 uint##bpp##_t *data; \
130 uint##bpp##_t c0, c1, ci; \
131 int i, n0, n1; \
132 \
133 data = (uint##bpp##_t *)vs->tight.buffer; \
134 \
135 c0 = data[0]; \
136 i = 1; \
137 while (i < count && data[i] == c0) \
138 i++; \
139 if (i >= count) { \
140 *bg = *fg = c0; \
141 return 1; \
142 } \
143 \
144 if (max < 2) { \
145 return 0; \
146 } \
147 \
148 n0 = i; \
149 c1 = data[i]; \
150 n1 = 0; \
151 for (i++; i < count; i++) { \
152 ci = data[i]; \
153 if (ci == c0) { \
154 n0++; \
155 } else if (ci == c1) { \
156 n1++; \
157 } else \
158 break; \
159 } \
160 if (i >= count) { \
161 if (n0 > n1) { \
162 *bg = (uint32_t)c0; \
163 *fg = (uint32_t)c1; \
164 } else { \
165 *bg = (uint32_t)c1; \
166 *fg = (uint32_t)c0; \
167 } \
168 return 2; \
169 } \
170 \
171 if (max == 2) { \
172 return 0; \
173 } \
174 \
175 *palette = qdict_new(); \
176 tight_palette_insert(*palette, c0, bpp, max); \
177 tight_palette_insert(*palette, c1, bpp, max); \
270ec219 »
2010-06-01 vnc: tight: don't forget the third color
178 tight_palette_insert(*palette, ci, bpp, max); \
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
179 \
180 for (i++; i < count; i++) { \
181 if (data[i] == ci) { \
182 continue; \
183 } else { \
184 if (!tight_palette_insert(*palette, (uint32_t)ci, \
185 bpp, max)) { \
186 return 0; \
187 } \
188 ci = data[i]; \
189 } \
190 } \
191 \
192 return qdict_size(*palette); \
193 }
194
195 DEFINE_FILL_PALETTE_FUNCTION(8)
196 DEFINE_FILL_PALETTE_FUNCTION(16)
197 DEFINE_FILL_PALETTE_FUNCTION(32)
198
199 static int tight_fill_palette(VncState *vs, int x, int y,
200 size_t count, uint32_t *bg, uint32_t *fg,
201 struct QDict **palette)
202 {
203 int max;
204
205 max = count / tight_conf[vs->tight_compression].idx_max_colors_divisor;
206 if (max < 2 &&
207 count >= tight_conf[vs->tight_compression].mono_min_rect_size) {
208 max = 2;
209 }
210 if (max >= 256) {
211 max = 256;
212 }
213
214 switch(vs->clientds.pf.bytes_per_pixel) {
215 case 4:
216 return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
217 case 2:
218 return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
219 default:
220 max = 2;
221 return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
222 }
223 return 0;
224 }
225
226 /* Callback to dump a palette with qdict_iter
227 static void print_palette(const char *key, QObject *obj, void *opaque)
228 {
229 uint8_t idx = qint_get_int(qobject_to_qint(obj));
230 uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
231
232 fprintf(stderr, "%.2x ", (unsigned char)*key);
233 while (*key++)
234 fprintf(stderr, "%.2x ", (unsigned char)*key);
235
236 fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
237 }
238 */
239
240 /*
241 * Converting truecolor samples into palette indices.
242 */
243 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
244 \
245 static void \
246 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
247 struct QDict *palette) { \
248 uint##bpp##_t *src; \
249 uint##bpp##_t rgb; \
250 uint8_t key[6]; \
54d43eac »
2010-06-01 vnc: tight: don't forget last pixel in tight_encode_indexed_rect
251 int i, rep; \
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
252 uint8_t idx; \
253 \
254 src = (uint##bpp##_t *) buf; \
255 \
54d43eac »
2010-06-01 vnc: tight: don't forget last pixel in tight_encode_indexed_rect
256 for (i = 0; i < count; i++) { \
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
257 rgb = *src++; \
258 rep = 0; \
54d43eac »
2010-06-01 vnc: tight: don't forget last pixel in tight_encode_indexed_rect
259 while (i < count && *src == rgb) { \
260 rep++, src++, i++; \
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
261 } \
262 tight_palette_rgb2buf(rgb, bpp, key); \
263 if (!qdict_haskey(palette, (char *)key)) { \
264 /* \
265 * Should never happen, but don't break everything \
266 * if it does, use the first color instead \
267 */ \
268 idx = 0; \
269 } else { \
270 idx = qdict_get_int(palette, (char *)key); \
271 } \
272 while (rep >= 0) { \
273 *buf++ = idx; \
274 rep--; \
275 } \
276 } \
277 }
278
279 DEFINE_IDX_ENCODE_FUNCTION(16)
280 DEFINE_IDX_ENCODE_FUNCTION(32)
281
282 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
283 \
284 static void \
285 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
286 uint##bpp##_t bg, uint##bpp##_t fg) { \
287 uint##bpp##_t *ptr; \
288 unsigned int value, mask; \
289 int aligned_width; \
290 int x, y, bg_bits; \
291 \
292 ptr = (uint##bpp##_t *) buf; \
293 aligned_width = w - w % 8; \
294 \
295 for (y = 0; y < h; y++) { \
296 for (x = 0; x < aligned_width; x += 8) { \
297 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
298 if (*ptr++ != bg) { \
299 break; \
300 } \
301 } \
302 if (bg_bits == 8) { \
303 *buf++ = 0; \
304 continue; \
305 } \
306 mask = 0x80 >> bg_bits; \
307 value = mask; \
308 for (bg_bits++; bg_bits < 8; bg_bits++) { \
309 mask >>= 1; \
310 if (*ptr++ != bg) { \
311 value |= mask; \
312 } \
313 } \
314 *buf++ = (uint8_t)value; \
315 } \
316 \
317 mask = 0x80; \
318 value = 0; \
319 if (x >= w) { \
320 continue; \
321 } \
322 \
323 for (; x < w; x++) { \
324 if (*ptr++ != bg) { \
325 value |= mask; \
326 } \
327 mask >>= 1; \
328 } \
329 *buf++ = (uint8_t)value; \
330 } \
331 }
332
333 DEFINE_MONO_ENCODE_FUNCTION(8)
334 DEFINE_MONO_ENCODE_FUNCTION(16)
335 DEFINE_MONO_ENCODE_FUNCTION(32)
336
337 /*
b4bea3f2 »
2010-05-19 vnc: add support for tight fill encoding
338 * Check if a rectangle is all of the same color. If needSameColor is
339 * set to non-zero, then also check that its color equals to the
340 * *colorPtr value. The result is 1 if the test is successfull, and in
341 * that case new color will be stored in *colorPtr.
342 */
343
344 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
345 \
346 static bool \
347 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
348 uint32_t* color, bool samecolor) \
349 { \
350 VncDisplay *vd = vs->vd; \
351 uint##bpp##_t *fbptr; \
352 uint##bpp##_t c; \
353 int dx, dy; \
354 \
355 fbptr = (uint##bpp##_t *) \
356 (vd->server->data + y * ds_get_linesize(vs->ds) + \
357 x * ds_get_bytes_per_pixel(vs->ds)); \
358 \
359 c = *fbptr; \
360 if (samecolor && (uint32_t)c != *color) { \
361 return false; \
362 } \
363 \
364 for (dy = 0; dy < h; dy++) { \
365 for (dx = 0; dx < w; dx++) { \
366 if (c != fbptr[dx]) { \
367 return false; \
368 } \
369 } \
370 fbptr = (uint##bpp##_t *) \
371 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
372 } \
373 \
374 *color = (uint32_t)c; \
375 return true; \
376 }
377
378 DEFINE_CHECK_SOLID_FUNCTION(32)
379 DEFINE_CHECK_SOLID_FUNCTION(16)
380 DEFINE_CHECK_SOLID_FUNCTION(8)
381
382 static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
383 uint32_t* color, bool samecolor)
384 {
385 VncDisplay *vd = vs->vd;
386
387 switch(vd->server->pf.bytes_per_pixel) {
388 case 4:
389 return check_solid_tile32(vs, x, y, w, h, color, samecolor);
390 case 2:
391 return check_solid_tile16(vs, x, y, w, h, color, samecolor);
392 default:
393 return check_solid_tile8(vs, x, y, w, h, color, samecolor);
394 }
395 }
396
397 static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
398 uint32_t color, int *w_ptr, int *h_ptr)
399 {
400 int dx, dy, dw, dh;
401 int w_prev;
402 int w_best = 0, h_best = 0;
403
404 w_prev = w;
405
406 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
407
408 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
409 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
410
411 if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
412 break;
413 }
414
415 for (dx = x + dw; dx < x + w_prev;) {
416 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
417
418 if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
419 break;
420 }
421 dx += dw;
422 }
423
424 w_prev = dx - x;
425 if (w_prev * (dy + dh - y) > w_best * h_best) {
426 w_best = w_prev;
427 h_best = dy + dh - y;
428 }
429 }
430
431 *w_ptr = w_best;
432 *h_ptr = h_best;
433 }
434
435 static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
436 uint32_t color, int *x_ptr, int *y_ptr,
437 int *w_ptr, int *h_ptr)
438 {
439 int cx, cy;
440
441 /* Try to extend the area upwards. */
442 for ( cy = *y_ptr - 1;
443 cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
444 cy-- );
445 *h_ptr += *y_ptr - (cy + 1);
446 *y_ptr = cy + 1;
447
448 /* ... downwards. */
449 for ( cy = *y_ptr + *h_ptr;
450 cy < y + h &&
451 check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
452 cy++ );
453 *h_ptr += cy - (*y_ptr + *h_ptr);
454
455 /* ... to the left. */
456 for ( cx = *x_ptr - 1;
457 cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
458 cx-- );
459 *w_ptr += *x_ptr - (cx + 1);
460 *x_ptr = cx + 1;
461
462 /* ... to the right. */
463 for ( cx = *x_ptr + *w_ptr;
464 cx < x + w &&
465 check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
466 cx++ );
467 *w_ptr += cx - (*x_ptr + *w_ptr);
468 }
469
380282b0 »
2010-05-19 vnc: add basic tight support
470 static int tight_init_stream(VncState *vs, int stream_id,
471 int level, int strategy)
472 {
473 z_streamp zstream = &vs->tight_stream[stream_id];
474
475 if (zstream->opaque == NULL) {
476 int err;
477
478 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
479 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
480 zstream->zalloc = vnc_zlib_zalloc;
481 zstream->zfree = vnc_zlib_zfree;
482
483 err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
484 MAX_MEM_LEVEL, strategy);
485
486 if (err != Z_OK) {
487 fprintf(stderr, "VNC: error initializing zlib\n");
488 return -1;
489 }
490
491 vs->tight_levels[stream_id] = level;
492 zstream->opaque = vs;
493 }
494
495 if (vs->tight_levels[stream_id] != level) {
496 if (deflateParams(zstream, level, strategy) != Z_OK) {
497 return -1;
498 }
499 vs->tight_levels[stream_id] = level;
500 }
501 return 0;
502 }
503
504 static void tight_send_compact_size(VncState *vs, size_t len)
505 {
506 int lpc = 0;
507 int bytes = 0;
508 char buf[3] = {0, 0, 0};
509
510 buf[bytes++] = len & 0x7F;
511 if (len > 0x7F) {
512 buf[bytes-1] |= 0x80;
513 buf[bytes++] = (len >> 7) & 0x7F;
514 if (len > 0x3FFF) {
515 buf[bytes-1] |= 0x80;
516 buf[bytes++] = (len >> 14) & 0xFF;
517 }
518 }
b4bea3f2 »
2010-05-19 vnc: add support for tight fill encoding
519 for (lpc = 0; lpc < bytes; lpc++) {
380282b0 »
2010-05-19 vnc: add basic tight support
520 vnc_write_u8(vs, buf[lpc]);
521 }
522 }
523
524 static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
525 int level, int strategy)
526 {
527 z_streamp zstream = &vs->tight_stream[stream_id];
528 int previous_out;
529
530 if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
531 vnc_write(vs, vs->tight.buffer, vs->tight.offset);
532 return bytes;
533 }
534
535 if (tight_init_stream(vs, stream_id, level, strategy)) {
536 return -1;
537 }
538
539 /* reserve memory in output buffer */
540 buffer_reserve(&vs->tight_zlib, bytes + 64);
541
542 /* set pointers */
543 zstream->next_in = vs->tight.buffer;
544 zstream->avail_in = vs->tight.offset;
545 zstream->next_out = vs->tight_zlib.buffer + vs->tight_zlib.offset;
546 zstream->avail_out = vs->tight_zlib.capacity - vs->tight_zlib.offset;
547 zstream->data_type = Z_BINARY;
548 previous_out = zstream->total_out;
549
550 /* start encoding */
551 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
552 fprintf(stderr, "VNC: error during tight compression\n");
553 return -1;
554 }
555
556 vs->tight_zlib.offset = vs->tight_zlib.capacity - zstream->avail_out;
557 bytes = zstream->total_out - previous_out;
558
559 tight_send_compact_size(vs, bytes);
560 vnc_write(vs, vs->tight_zlib.buffer, bytes);
561
562 buffer_reset(&vs->tight_zlib);
563
564 return bytes;
565 }
566
567 /*
568 * Subencoding implementations.
569 */
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
570 static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
380282b0 »
2010-05-19 vnc: add basic tight support
571 {
572 uint32_t *buf32;
573 uint32_t pix;
574 int rshift, gshift, bshift;
575
576 buf32 = (uint32_t *)buf;
577
578 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
579 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
580 rshift = vs->clientds.pf.rshift;
581 gshift = vs->clientds.pf.gshift;
582 bshift = vs->clientds.pf.bshift;
583 } else {
584 rshift = 24 - vs->clientds.pf.rshift;
585 gshift = 24 - vs->clientds.pf.gshift;
586 bshift = 24 - vs->clientds.pf.bshift;
587 }
588
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
589 if (ret) {
590 *ret = count * 3;
591 }
380282b0 »
2010-05-19 vnc: add basic tight support
592
593 while (count--) {
594 pix = *buf32++;
595 *buf++ = (char)(pix >> rshift);
596 *buf++ = (char)(pix >> gshift);
597 *buf++ = (char)(pix >> bshift);
598 }
599 }
600
601 static int send_full_color_rect(VncState *vs, int w, int h)
602 {
603 int stream = 0;
604 size_t bytes;
605
606 vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
607
608 if (vs->tight_pixel24) {
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
609 tight_pack24(vs, vs->tight.buffer, w * h, &vs->tight.offset);
380282b0 »
2010-05-19 vnc: add basic tight support
610 bytes = 3;
611 } else {
612 bytes = vs->clientds.pf.bytes_per_pixel;
613 }
614
615 bytes = tight_compress_data(vs, stream, w * h * bytes,
616 tight_conf[vs->tight_compression].raw_zlib_level,
617 Z_DEFAULT_STRATEGY);
618
619 return (bytes >= 0);
620 }
621
b4bea3f2 »
2010-05-19 vnc: add support for tight fill encoding
622 static int send_solid_rect(VncState *vs)
623 {
624 size_t bytes;
625
626 vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
627
628 if (vs->tight_pixel24) {
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
629 tight_pack24(vs, vs->tight.buffer, 1, &vs->tight.offset);
b4bea3f2 »
2010-05-19 vnc: add support for tight fill encoding
630 bytes = 3;
631 } else {
632 bytes = vs->clientds.pf.bytes_per_pixel;
633 }
634
635 vnc_write(vs, vs->tight.buffer, bytes);
636 return 1;
637 }
638
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
639 static int send_mono_rect(VncState *vs, int w, int h, uint32_t bg, uint32_t fg)
640 {
641 size_t bytes;
642 int stream = 1;
643 int level = tight_conf[vs->tight_compression].mono_zlib_level;
644
645 bytes = ((w + 7) / 8) * h;
646
647 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
648 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
649 vnc_write_u8(vs, 1);
650
651 switch(vs->clientds.pf.bytes_per_pixel) {
652 case 4:
653 {
654 uint32_t buf[2] = {bg, fg};
655 size_t ret = sizeof (buf);
656
657 if (vs->tight_pixel24) {
658 tight_pack24(vs, (unsigned char*)buf, 2, &ret);
659 }
660 vnc_write(vs, buf, ret);
661
662 tight_encode_mono_rect32(vs->tight.buffer, w, h, bg, fg);
663 break;
664 }
665 case 2:
666 vnc_write(vs, &bg, 2);
667 vnc_write(vs, &fg, 2);
668 tight_encode_mono_rect16(vs->tight.buffer, w, h, bg, fg);
669 break;
670 default:
671 vnc_write_u8(vs, bg);
672 vnc_write_u8(vs, fg);
673 tight_encode_mono_rect8(vs->tight.buffer, w, h, bg, fg);
674 break;
675 }
676 vs->tight.offset = bytes;
677
678 bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
679 return (bytes >= 0);
680 }
681
682 struct palette_cb_priv {
683 VncState *vs;
684 uint8_t *header;
685 };
686
687 static void write_palette(const char *key, QObject *obj, void *opaque)
688 {
689 struct palette_cb_priv *priv = opaque;
690 VncState *vs = priv->vs;
691 uint32_t bytes = vs->clientds.pf.bytes_per_pixel;
692 uint8_t idx = qint_get_int(qobject_to_qint(obj));
693
694 if (bytes == 4) {
695 uint32_t color = tight_palette_buf2rgb(32, (uint8_t *)key);
696
697 ((uint32_t*)priv->header)[idx] = color;
698 } else {
699 uint16_t color = tight_palette_buf2rgb(16, (uint8_t *)key);
700
701 ((uint16_t*)priv->header)[idx] = color;
702 }
703 }
704
705 static int send_palette_rect(VncState *vs, int w, int h, struct QDict *palette)
706 {
707 int stream = 2;
708 int level = tight_conf[vs->tight_compression].idx_zlib_level;
709 int colors;
710 size_t bytes;
711
712 colors = qdict_size(palette);
713
714 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
715 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
716 vnc_write_u8(vs, colors - 1);
717
718 switch(vs->clientds.pf.bytes_per_pixel) {
719 case 4:
720 {
721 size_t old_offset, offset;
722 uint32_t header[qdict_size(palette)];
723 struct palette_cb_priv priv = { vs, (uint8_t *)header };
724
725 old_offset = vs->output.offset;
726 qdict_iter(palette, write_palette, &priv);
727 vnc_write(vs, header, sizeof(header));
728
729 if (vs->tight_pixel24) {
730 tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
731 vs->output.offset = old_offset + offset;
732 }
733
734 tight_encode_indexed_rect32(vs->tight.buffer, w * h, palette);
735 break;
736 }
737 case 2:
738 {
739 uint16_t header[qdict_size(palette)];
740 struct palette_cb_priv priv = { vs, (uint8_t *)header };
741
742 qdict_iter(palette, write_palette, &priv);
743 vnc_write(vs, header, sizeof(header));
744 tight_encode_indexed_rect16(vs->tight.buffer, w * h, palette);
745 break;
746 }
747 default:
748 return -1; /* No palette for 8bits colors */
749 break;
750 }
751 bytes = w * h;
752 vs->tight.offset = bytes;
753
754 bytes = tight_compress_data(vs, stream, bytes,
755 level, Z_DEFAULT_STRATEGY);
756 return (bytes >= 0);
757 }
758
380282b0 »
2010-05-19 vnc: add basic tight support
759 static void vnc_tight_start(VncState *vs)
760 {
761 buffer_reset(&vs->tight);
762
763 // make the output buffer be the zlib buffer, so we can compress it later
764 vs->tight_tmp = vs->output;
765 vs->output = vs->tight;
766 }
767
768 static void vnc_tight_stop(VncState *vs)
769 {
770 // switch back to normal output/zlib buffers
771 vs->tight = vs->output;
772 vs->output = vs->tight_tmp;
773 }
774
775 static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
776 {
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
777 struct QDict *palette = NULL;
778 uint32_t bg = 0, fg = 0;
779 int colors;
780 int ret = 0;
781
380282b0 »
2010-05-19 vnc: add basic tight support
782 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT);
783
784 vnc_tight_start(vs);
785 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
786 vnc_tight_stop(vs);
787
aa7d73fd »
2010-05-19 vnc: tight: add palette encoding
788 colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
789
790 if (colors == 0) {
791 ret = send_full_color_rect(vs, w, h);
792 } else if (colors == 1) {
793 ret = send_solid_rect(vs);
794 } else if (colors == 2) {
795 ret = send_mono_rect(vs, w, h, bg, fg);
796 } else if (colors <= 256) {
797 ret = send_palette_rect(vs, w, h, palette);
798 }
799 QDECREF(palette);
800 return ret;
380282b0 »
2010-05-19 vnc: add basic tight support
801 }
802
b4bea3f2 »
2010-05-19 vnc: add support for tight fill encoding
803 static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
804 {
805 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT);
806
807 vnc_tight_start(vs);
808 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
809 vnc_tight_stop(vs);
810
811 return send_solid_rect(vs);
812 }
813
380282b0 »
2010-05-19 vnc: add basic tight support
814 static int send_rect_simple(VncState *vs, int x, int y, int w, int h)
815 {
816 int max_size, max_width;
817 int max_sub_width, max_sub_height;
818 int dx, dy;
819 int rw, rh;
820 int n = 0;
821
822 max_size = tight_conf[vs->tight_compression].max_rect_size;
823 max_width = tight_conf[vs->tight_compression].max_rect_width;
824
825 if (w > max_width || w * h > max_size) {
826 max_sub_width = (w > max_width) ? max_width : w;
827 max_sub_height = max_size / max_sub_width;
828
829 for (dy = 0; dy < h; dy += max_sub_height) {
830 for (dx = 0; dx < w; dx += max_width) {
831 rw = MIN(max_sub_width, w - dx);
832 rh = MIN(max_sub_height, h - dy);
833 n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
834 }
835 }
836 } else {
837 n += send_sub_rect(vs, x, y, w, h);
838 }
839
840 return n;
841 }
842
b4bea3f2 »
2010-05-19 vnc: add support for tight fill encoding
843 static int find_large_solid_color_rect(VncState *vs, int x, int y,
844 int w, int h, int max_rows)
845 {
846 int dx, dy, dw, dh;
847 int n = 0;
848
849 /* Try to find large solid-color areas and send them separately. */
850
851 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
852
853 /* If a rectangle becomes too large, send its upper part now. */
854
855 if (dy - y >= max_rows) {
856 n += send_rect_simple(vs, x, y, w, max_rows);
857 y += max_rows;
858 h -= max_rows;
859 }
860
861 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));
862
863 for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
864 uint32_t color_value;
865 int x_best, y_best, w_best, h_best;
866
867 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));
868
869 if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
870 continue ;
871 }
872
873 /* Get dimensions of solid-color area. */
874
875 find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
876 color_value, &w_best, &h_best);
877
878 /* Make sure a solid rectangle is large enough
879 (or the whole rectangle is of the same color). */
880
881 if (w_best * h_best != w * h &&
882 w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
883 continue;
884 }
885
886 /* Try to extend solid rectangle to maximum size. */
887
888 x_best = dx; y_best = dy;
889 extend_solid_area(vs, x, y, w, h, color_value,
890 &x_best, &y_best, &w_best, &h_best);
891
892 /* Send rectangles at top and left to solid-color area. */
893
894 if (y_best != y) {
895 n += send_rect_simple(vs, x, y, w, y_best-y);
896 }
897 if (x_best != x) {
898 n += vnc_tight_send_framebuffer_update(vs, x, y_best,
899 x_best-x, h_best);
900 }
901
902 /* Send solid-color rectangle. */
903 n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);
904
905 /* Send remaining rectangles (at right and bottom). */
906
907 if (x_best + w_best != x + w) {
908 n += vnc_tight_send_framebuffer_update(vs, x_best+w_best,
909 y_best,
910 w-(x_best-x)-w_best,
911 h_best);
912 }
913 if (y_best + h_best != y + h) {
914 n += vnc_tight_send_framebuffer_update(vs, x, y_best+h_best,
915 w, h-(y_best-y)-h_best);
916 }
917
918 /* Return after all recursive calls are done. */
919 return n;
920 }
921 }
922 return n + send_rect_simple(vs, x, y, w, h);
923 }
924
380282b0 »
2010-05-19 vnc: add basic tight support
925 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
926 int w, int h)
927 {
b4bea3f2 »
2010-05-19 vnc: add support for tight fill encoding
928 int max_rows;
929
380282b0 »
2010-05-19 vnc: add basic tight support
930 if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF &&
931 vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) {
932 vs->tight_pixel24 = true;
933 } else {
934 vs->tight_pixel24 = false;
935 }
936
b4bea3f2 »
2010-05-19 vnc: add support for tight fill encoding
937 if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE)
938 return send_rect_simple(vs, x, y, w, h);
939
940 /* Calculate maximum number of rows in one non-solid rectangle. */
941
942 max_rows = tight_conf[vs->tight_compression].max_rect_size;
943 max_rows /= MIN(tight_conf[vs->tight_compression].max_rect_width, w);
944
945 return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
380282b0 »
2010-05-19 vnc: add basic tight support
946 }
947
948 void vnc_tight_clear(VncState *vs)
949 {
950 int i;
951 for (i=0; i<ARRAY_SIZE(vs->tight_stream); i++) {
952 if (vs->tight_stream[i].opaque) {
953 deflateEnd(&vs->tight_stream[i]);
954 }
955 }
956
957 buffer_free(&vs->tight);
958 buffer_free(&vs->tight_zlib);
959 }
Something went wrong with that request. Please try again.