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