2 * QEMU VNC display driver: tight encoding
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.
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include "config-host.h"
34 #ifdef CONFIG_VNC_JPEG
39 #include "qemu-common.h"
44 #include "vnc-enc-tight.h"
45 #include "vnc-palette.h"
47 /* Compression level stuff. The following array contains various
48 encoder parameters for each of 10 compression levels (0..9).
49 Last three parameters correspond to JPEG quality levels (0..9). */
52 int max_rect_size, max_rect_width;
53 int mono_min_rect_size, gradient_min_rect_size;
54 int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
55 int gradient_threshold, gradient_threshold24;
56 int idx_max_colors_divisor;
57 int jpeg_quality, jpeg_threshold, jpeg_threshold24;
59 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
60 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
61 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
62 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
63 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
64 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
65 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
66 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
67 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
68 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
72 static int tight_send_framebuffer_update(VncState *vs, int x, int y,
75 #ifdef CONFIG_VNC_JPEG
77 double jpeg_freq_min; /* Don't send JPEG if the freq is bellow */
78 double jpeg_freq_threshold; /* Always send JPEG if the freq is above */
79 int jpeg_idx; /* Allow indexed JPEG */
80 int jpeg_full; /* Allow full color JPEG */
81 } tight_jpeg_conf[] = {
97 int png_zlib_level, png_filters;
98 } tight_png_conf[] = {
99 { 0, PNG_NO_FILTERS },
100 { 1, PNG_NO_FILTERS },
101 { 2, PNG_NO_FILTERS },
102 { 3, PNG_NO_FILTERS },
103 { 4, PNG_NO_FILTERS },
104 { 5, PNG_ALL_FILTERS },
105 { 6, PNG_ALL_FILTERS },
106 { 7, PNG_ALL_FILTERS },
107 { 8, PNG_ALL_FILTERS },
108 { 9, PNG_ALL_FILTERS },
111 static int send_png_rect(VncState *vs, int x, int y, int w, int h,
112 VncPalette *palette);
114 static bool tight_can_send_png_rect(VncState *vs, int w, int h)
116 if (vs->tight.type != VNC_ENCODING_TIGHT_PNG) {
120 if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
121 vs->clientds.pf.bytes_per_pixel == 1) {
130 * Code to guess if given rectangle is suitable for smooth image
131 * compression (by applying "gradient" filter or JPEG coder).
135 tight_detect_smooth_image24(VncState *vs, int w, int h)
140 unsigned int stats[256];
144 unsigned char *buf = vs->tight.tight.buffer;
147 * If client is big-endian, color samples begin from the second
148 * byte (offset 1) of a 32-bit pixel value.
150 off = !!(vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG);
152 memset(stats, 0, sizeof (stats));
154 for (y = 0, x = 0; y < h && x < w;) {
155 for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH;
157 for (c = 0; c < 3; c++) {
158 left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF;
160 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) {
161 for (c = 0; c < 3; c++) {
162 pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF;
163 stats[abs(pix - left[c])]++;
178 /* 95% smooth or more ... */
179 if (stats[0] * 33 / pixels >= 95) {
184 for (c = 1; c < 8; c++) {
185 errors += stats[c] * (c * c);
186 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) {
190 for (; c < 256; c++) {
191 errors += stats[c] * (c * c);
193 errors /= (pixels * 3 - stats[0]);
198 #define DEFINE_DETECT_FUNCTION(bpp) \
200 static unsigned int \
201 tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \
204 int max[3], shift[3]; \
207 unsigned int stats[256]; \
209 int sample, sum, left[3]; \
210 unsigned int errors; \
211 unsigned char *buf = vs->tight.tight.buffer; \
213 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
214 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
217 max[0] = vs->clientds.pf.rmax; \
218 max[1] = vs->clientds.pf.gmax; \
219 max[2] = vs->clientds.pf.bmax; \
220 shift[0] = vs->clientds.pf.rshift; \
221 shift[1] = vs->clientds.pf.gshift; \
222 shift[2] = vs->clientds.pf.bshift; \
224 memset(stats, 0, sizeof(stats)); \
227 while (y < h && x < w) { \
228 for (d = 0; d < h - y && \
229 d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \
230 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \
232 pix = bswap##bpp(pix); \
234 for (c = 0; c < 3; c++) { \
235 left[c] = (int)(pix >> shift[c] & max[c]); \
237 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \
239 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \
241 pix = bswap##bpp(pix); \
244 for (c = 0; c < 3; c++) { \
245 sample = (int)(pix >> shift[c] & max[c]); \
246 sum += abs(sample - left[c]); \
265 if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \
270 for (c = 1; c < 8; c++) { \
271 errors += stats[c] * (c * c); \
272 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \
276 for (; c < 256; c++) { \
277 errors += stats[c] * (c * c); \
279 errors /= (pixels - stats[0]); \
284 DEFINE_DETECT_FUNCTION(16)
285 DEFINE_DETECT_FUNCTION(32)
288 tight_detect_smooth_image(VncState *vs, int w, int h)
291 int compression = vs->tight.compression;
292 int quality = vs->tight.quality;
294 if (!vs->vd->lossy) {
298 if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
299 vs->clientds.pf.bytes_per_pixel == 1 ||
300 w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) {
304 if (vs->tight.quality != (uint8_t)-1) {
305 if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) {
309 if (w * h < tight_conf[compression].gradient_min_rect_size) {
314 if (vs->clientds.pf.bytes_per_pixel == 4) {
315 if (vs->tight.pixel24) {
316 errors = tight_detect_smooth_image24(vs, w, h);
317 if (vs->tight.quality != (uint8_t)-1) {
318 return (errors < tight_conf[quality].jpeg_threshold24);
320 return (errors < tight_conf[compression].gradient_threshold24);
322 errors = tight_detect_smooth_image32(vs, w, h);
325 errors = tight_detect_smooth_image16(vs, w, h);
328 return (errors < tight_conf[quality].jpeg_threshold);
330 return (errors < tight_conf[compression].gradient_threshold);
334 * Code to determine how many different colors used in rectangle.
336 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
339 tight_fill_palette##bpp(VncState *vs, int x, int y, \
340 int max, size_t count, \
341 uint32_t *bg, uint32_t *fg, \
342 VncPalette **palette) { \
343 uint##bpp##_t *data; \
344 uint##bpp##_t c0, c1, ci; \
347 data = (uint##bpp##_t *)vs->tight.tight.buffer; \
351 while (i < count && data[i] == c0) \
365 for (i++; i < count; i++) { \
369 } else if (ci == c1) { \
376 *bg = (uint32_t)c0; \
377 *fg = (uint32_t)c1; \
379 *bg = (uint32_t)c1; \
380 *fg = (uint32_t)c0; \
389 *palette = palette_new(max, bpp); \
390 palette_put(*palette, c0); \
391 palette_put(*palette, c1); \
392 palette_put(*palette, ci); \
394 for (i++; i < count; i++) { \
395 if (data[i] == ci) { \
399 if (!palette_put(*palette, (uint32_t)ci)) { \
405 return palette_size(*palette); \
408 DEFINE_FILL_PALETTE_FUNCTION(8)
409 DEFINE_FILL_PALETTE_FUNCTION(16)
410 DEFINE_FILL_PALETTE_FUNCTION(32)
412 static int tight_fill_palette(VncState *vs, int x, int y,
413 size_t count, uint32_t *bg, uint32_t *fg,
414 VncPalette **palette)
418 max = count / tight_conf[vs->tight.compression].idx_max_colors_divisor;
420 count >= tight_conf[vs->tight.compression].mono_min_rect_size) {
427 switch(vs->clientds.pf.bytes_per_pixel) {
429 return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
431 return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
434 return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
440 * Converting truecolor samples into palette indices.
442 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
445 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
446 VncPalette *palette) { \
447 uint##bpp##_t *src; \
452 src = (uint##bpp##_t *) buf; \
454 for (i = 0; i < count; i++) { \
458 while (i < count && *src == rgb) { \
461 idx = palette_idx(palette, rgb); \
463 * Should never happen, but don't break everything \
464 * if it does, use the first color instead \
466 if (idx == (uint8_t)-1) { \
476 DEFINE_IDX_ENCODE_FUNCTION(16)
477 DEFINE_IDX_ENCODE_FUNCTION(32)
479 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
482 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
483 uint##bpp##_t bg, uint##bpp##_t fg) { \
484 uint##bpp##_t *ptr; \
485 unsigned int value, mask; \
489 ptr = (uint##bpp##_t *) buf; \
490 aligned_width = w - w % 8; \
492 for (y = 0; y < h; y++) { \
493 for (x = 0; x < aligned_width; x += 8) { \
494 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
495 if (*ptr++ != bg) { \
499 if (bg_bits == 8) { \
503 mask = 0x80 >> bg_bits; \
505 for (bg_bits++; bg_bits < 8; bg_bits++) { \
507 if (*ptr++ != bg) { \
511 *buf++ = (uint8_t)value; \
520 for (; x < w; x++) { \
521 if (*ptr++ != bg) { \
526 *buf++ = (uint8_t)value; \
530 DEFINE_MONO_ENCODE_FUNCTION(8)
531 DEFINE_MONO_ENCODE_FUNCTION(16)
532 DEFINE_MONO_ENCODE_FUNCTION(32)
535 * ``Gradient'' filter for 24-bit color samples.
536 * Should be called only when redMax, greenMax and blueMax are 255.
537 * Color components assumed to be byte-aligned.
541 tight_filter_gradient24(VncState *vs, uint8_t *buf, int w, int h)
547 int here[3], upper[3], left[3], upperleft[3];
551 buf32 = (uint32_t *)buf;
552 memset(vs->tight.gradient.buffer, 0, w * 3 * sizeof(int));
554 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
555 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
556 shift[0] = vs->clientds.pf.rshift;
557 shift[1] = vs->clientds.pf.gshift;
558 shift[2] = vs->clientds.pf.bshift;
560 shift[0] = 24 - vs->clientds.pf.rshift;
561 shift[1] = 24 - vs->clientds.pf.gshift;
562 shift[2] = 24 - vs->clientds.pf.bshift;
565 for (y = 0; y < h; y++) {
566 for (c = 0; c < 3; c++) {
570 prev = (int *)vs->tight.gradient.buffer;
571 for (x = 0; x < w; x++) {
573 for (c = 0; c < 3; c++) {
574 upperleft[c] = upper[c];
577 here[c] = (int)(pix32 >> shift[c] & 0xFF);
580 prediction = left[c] + upper[c] - upperleft[c];
581 if (prediction < 0) {
583 } else if (prediction > 0xFF) {
586 *buf++ = (char)(here[c] - prediction);
594 * ``Gradient'' filter for other color depths.
597 #define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \
600 tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \
602 uint##bpp##_t pix, diff; \
605 int max[3], shift[3]; \
606 int here[3], upper[3], left[3], upperleft[3]; \
610 memset (vs->tight.gradient.buffer, 0, w * 3 * sizeof(int)); \
612 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
613 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
615 max[0] = vs->clientds.pf.rmax; \
616 max[1] = vs->clientds.pf.gmax; \
617 max[2] = vs->clientds.pf.bmax; \
618 shift[0] = vs->clientds.pf.rshift; \
619 shift[1] = vs->clientds.pf.gshift; \
620 shift[2] = vs->clientds.pf.bshift; \
622 for (y = 0; y < h; y++) { \
623 for (c = 0; c < 3; c++) { \
627 prev = (int *)vs->tight.gradient.buffer; \
628 for (x = 0; x < w; x++) { \
631 pix = bswap##bpp(pix); \
634 for (c = 0; c < 3; c++) { \
635 upperleft[c] = upper[c]; \
638 here[c] = (int)(pix >> shift[c] & max[c]); \
641 prediction = left[c] + upper[c] - upperleft[c]; \
642 if (prediction < 0) { \
644 } else if (prediction > max[c]) { \
645 prediction = max[c]; \
647 diff |= ((here[c] - prediction) & max[c]) \
651 diff = bswap##bpp(diff); \
658 DEFINE_GRADIENT_FILTER_FUNCTION(16)
659 DEFINE_GRADIENT_FILTER_FUNCTION(32)
662 * Check if a rectangle is all of the same color. If needSameColor is
663 * set to non-zero, then also check that its color equals to the
664 * *colorPtr value. The result is 1 if the test is successful, and in
665 * that case new color will be stored in *colorPtr.
668 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
671 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
672 uint32_t* color, bool samecolor) \
674 VncDisplay *vd = vs->vd; \
675 uint##bpp##_t *fbptr; \
679 fbptr = (uint##bpp##_t *) \
680 (vd->server->data + y * ds_get_linesize(vs->ds) + \
681 x * ds_get_bytes_per_pixel(vs->ds)); \
684 if (samecolor && (uint32_t)c != *color) { \
688 for (dy = 0; dy < h; dy++) { \
689 for (dx = 0; dx < w; dx++) { \
690 if (c != fbptr[dx]) { \
694 fbptr = (uint##bpp##_t *) \
695 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
698 *color = (uint32_t)c; \
702 DEFINE_CHECK_SOLID_FUNCTION(32)
703 DEFINE_CHECK_SOLID_FUNCTION(16)
704 DEFINE_CHECK_SOLID_FUNCTION(8)
706 static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
707 uint32_t* color, bool samecolor)
709 VncDisplay *vd = vs->vd;
711 switch(vd->server->pf.bytes_per_pixel) {
713 return check_solid_tile32(vs, x, y, w, h, color, samecolor);
715 return check_solid_tile16(vs, x, y, w, h, color, samecolor);
717 return check_solid_tile8(vs, x, y, w, h, color, samecolor);
721 static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
722 uint32_t color, int *w_ptr, int *h_ptr)
726 int w_best = 0, h_best = 0;
730 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
732 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
733 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
735 if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
739 for (dx = x + dw; dx < x + w_prev;) {
740 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
742 if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
749 if (w_prev * (dy + dh - y) > w_best * h_best) {
751 h_best = dy + dh - y;
759 static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
760 uint32_t color, int *x_ptr, int *y_ptr,
761 int *w_ptr, int *h_ptr)
765 /* Try to extend the area upwards. */
766 for ( cy = *y_ptr - 1;
767 cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
769 *h_ptr += *y_ptr - (cy + 1);
773 for ( cy = *y_ptr + *h_ptr;
775 check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
777 *h_ptr += cy - (*y_ptr + *h_ptr);
779 /* ... to the left. */
780 for ( cx = *x_ptr - 1;
781 cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
783 *w_ptr += *x_ptr - (cx + 1);
786 /* ... to the right. */
787 for ( cx = *x_ptr + *w_ptr;
789 check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
791 *w_ptr += cx - (*x_ptr + *w_ptr);
794 static int tight_init_stream(VncState *vs, int stream_id,
795 int level, int strategy)
797 z_streamp zstream = &vs->tight.stream[stream_id];
799 if (zstream->opaque == NULL) {
802 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
803 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
804 zstream->zalloc = vnc_zlib_zalloc;
805 zstream->zfree = vnc_zlib_zfree;
807 err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
808 MAX_MEM_LEVEL, strategy);
811 fprintf(stderr, "VNC: error initializing zlib\n");
815 vs->tight.levels[stream_id] = level;
816 zstream->opaque = vs;
819 if (vs->tight.levels[stream_id] != level) {
820 if (deflateParams(zstream, level, strategy) != Z_OK) {
823 vs->tight.levels[stream_id] = level;
828 static void tight_send_compact_size(VncState *vs, size_t len)
832 char buf[3] = {0, 0, 0};
834 buf[bytes++] = len & 0x7F;
836 buf[bytes-1] |= 0x80;
837 buf[bytes++] = (len >> 7) & 0x7F;
839 buf[bytes-1] |= 0x80;
840 buf[bytes++] = (len >> 14) & 0xFF;
843 for (lpc = 0; lpc < bytes; lpc++) {
844 vnc_write_u8(vs, buf[lpc]);
848 static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
849 int level, int strategy)
851 z_streamp zstream = &vs->tight.stream[stream_id];
854 if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
855 vnc_write(vs, vs->tight.tight.buffer, vs->tight.tight.offset);
859 if (tight_init_stream(vs, stream_id, level, strategy)) {
863 /* reserve memory in output buffer */
864 buffer_reserve(&vs->tight.zlib, bytes + 64);
867 zstream->next_in = vs->tight.tight.buffer;
868 zstream->avail_in = vs->tight.tight.offset;
869 zstream->next_out = vs->tight.zlib.buffer + vs->tight.zlib.offset;
870 zstream->avail_out = vs->tight.zlib.capacity - vs->tight.zlib.offset;
871 zstream->data_type = Z_BINARY;
872 previous_out = zstream->total_out;
875 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
876 fprintf(stderr, "VNC: error during tight compression\n");
880 vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out;
881 bytes = zstream->total_out - previous_out;
883 tight_send_compact_size(vs, bytes);
884 vnc_write(vs, vs->tight.zlib.buffer, bytes);
886 buffer_reset(&vs->tight.zlib);
892 * Subencoding implementations.
894 static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
898 int rshift, gshift, bshift;
900 buf32 = (uint32_t *)buf;
902 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
903 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
904 rshift = vs->clientds.pf.rshift;
905 gshift = vs->clientds.pf.gshift;
906 bshift = vs->clientds.pf.bshift;
908 rshift = 24 - vs->clientds.pf.rshift;
909 gshift = 24 - vs->clientds.pf.gshift;
910 bshift = 24 - vs->clientds.pf.bshift;
919 *buf++ = (char)(pix >> rshift);
920 *buf++ = (char)(pix >> gshift);
921 *buf++ = (char)(pix >> bshift);
925 static int send_full_color_rect(VncState *vs, int x, int y, int w, int h)
930 #ifdef CONFIG_VNC_PNG
931 if (tight_can_send_png_rect(vs, w, h)) {
932 return send_png_rect(vs, x, y, w, h, NULL);
936 vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
938 if (vs->tight.pixel24) {
939 tight_pack24(vs, vs->tight.tight.buffer, w * h, &vs->tight.tight.offset);
942 bytes = vs->clientds.pf.bytes_per_pixel;
945 bytes = tight_compress_data(vs, stream, w * h * bytes,
946 tight_conf[vs->tight.compression].raw_zlib_level,
952 static int send_solid_rect(VncState *vs)
956 vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
958 if (vs->tight.pixel24) {
959 tight_pack24(vs, vs->tight.tight.buffer, 1, &vs->tight.tight.offset);
962 bytes = vs->clientds.pf.bytes_per_pixel;
965 vnc_write(vs, vs->tight.tight.buffer, bytes);
969 static int send_mono_rect(VncState *vs, int x, int y,
970 int w, int h, uint32_t bg, uint32_t fg)
974 int level = tight_conf[vs->tight.compression].mono_zlib_level;
976 #ifdef CONFIG_VNC_PNG
977 if (tight_can_send_png_rect(vs, w, h)) {
979 int bpp = vs->clientds.pf.bytes_per_pixel * 8;
980 VncPalette *palette = palette_new(2, bpp);
982 palette_put(palette, bg);
983 palette_put(palette, fg);
984 ret = send_png_rect(vs, x, y, w, h, palette);
985 palette_destroy(palette);
990 bytes = ((w + 7) / 8) * h;
992 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
993 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
996 switch(vs->clientds.pf.bytes_per_pixel) {
999 uint32_t buf[2] = {bg, fg};
1000 size_t ret = sizeof (buf);
1002 if (vs->tight.pixel24) {
1003 tight_pack24(vs, (unsigned char*)buf, 2, &ret);
1005 vnc_write(vs, buf, ret);
1007 tight_encode_mono_rect32(vs->tight.tight.buffer, w, h, bg, fg);
1011 vnc_write(vs, &bg, 2);
1012 vnc_write(vs, &fg, 2);
1013 tight_encode_mono_rect16(vs->tight.tight.buffer, w, h, bg, fg);
1016 vnc_write_u8(vs, bg);
1017 vnc_write_u8(vs, fg);
1018 tight_encode_mono_rect8(vs->tight.tight.buffer, w, h, bg, fg);
1021 vs->tight.tight.offset = bytes;
1023 bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
1024 return (bytes >= 0);
1027 struct palette_cb_priv {
1030 #ifdef CONFIG_VNC_PNG
1031 png_colorp png_palette;
1035 static void write_palette(int idx, uint32_t color, void *opaque)
1037 struct palette_cb_priv *priv = opaque;
1038 VncState *vs = priv->vs;
1039 uint32_t bytes = vs->clientds.pf.bytes_per_pixel;
1042 ((uint32_t*)priv->header)[idx] = color;
1044 ((uint16_t*)priv->header)[idx] = color;
1048 static bool send_gradient_rect(VncState *vs, int x, int y, int w, int h)
1051 int level = tight_conf[vs->tight.compression].gradient_zlib_level;
1054 if (vs->clientds.pf.bytes_per_pixel == 1)
1055 return send_full_color_rect(vs, x, y, w, h);
1057 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1058 vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT);
1060 buffer_reserve(&vs->tight.gradient, w * 3 * sizeof (int));
1062 if (vs->tight.pixel24) {
1063 tight_filter_gradient24(vs, vs->tight.tight.buffer, w, h);
1065 } else if (vs->clientds.pf.bytes_per_pixel == 4) {
1066 tight_filter_gradient32(vs, (uint32_t *)vs->tight.tight.buffer, w, h);
1069 tight_filter_gradient16(vs, (uint16_t *)vs->tight.tight.buffer, w, h);
1073 buffer_reset(&vs->tight.gradient);
1075 bytes = w * h * bytes;
1076 vs->tight.tight.offset = bytes;
1078 bytes = tight_compress_data(vs, stream, bytes,
1080 return (bytes >= 0);
1083 static int send_palette_rect(VncState *vs, int x, int y,
1084 int w, int h, VncPalette *palette)
1087 int level = tight_conf[vs->tight.compression].idx_zlib_level;
1091 #ifdef CONFIG_VNC_PNG
1092 if (tight_can_send_png_rect(vs, w, h)) {
1093 return send_png_rect(vs, x, y, w, h, palette);
1097 colors = palette_size(palette);
1099 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1100 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
1101 vnc_write_u8(vs, colors - 1);
1103 switch(vs->clientds.pf.bytes_per_pixel) {
1106 size_t old_offset, offset;
1107 uint32_t header[palette_size(palette)];
1108 struct palette_cb_priv priv = { vs, (uint8_t *)header };
1110 old_offset = vs->output.offset;
1111 palette_iter(palette, write_palette, &priv);
1112 vnc_write(vs, header, sizeof(header));
1114 if (vs->tight.pixel24) {
1115 tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
1116 vs->output.offset = old_offset + offset;
1119 tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
1124 uint16_t header[palette_size(palette)];
1125 struct palette_cb_priv priv = { vs, (uint8_t *)header };
1127 palette_iter(palette, write_palette, &priv);
1128 vnc_write(vs, header, sizeof(header));
1129 tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
1133 return -1; /* No palette for 8bits colors */
1137 vs->tight.tight.offset = bytes;
1139 bytes = tight_compress_data(vs, stream, bytes,
1140 level, Z_DEFAULT_STRATEGY);
1141 return (bytes >= 0);
1144 #if defined(CONFIG_VNC_JPEG) || defined(CONFIG_VNC_PNG)
1145 static void rgb_prepare_row24(VncState *vs, uint8_t *dst, int x, int y,
1148 VncDisplay *vd = vs->vd;
1152 fbptr = (uint32_t *)(vd->server->data + y * ds_get_linesize(vs->ds) +
1153 x * ds_get_bytes_per_pixel(vs->ds));
1157 *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.rshift);
1158 *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.gshift);
1159 *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.bshift);
1163 #define DEFINE_RGB_GET_ROW_FUNCTION(bpp) \
1166 rgb_prepare_row##bpp(VncState *vs, uint8_t *dst, \
1167 int x, int y, int count) \
1169 VncDisplay *vd = vs->vd; \
1170 uint##bpp##_t *fbptr; \
1171 uint##bpp##_t pix; \
1174 fbptr = (uint##bpp##_t *) \
1175 (vd->server->data + y * ds_get_linesize(vs->ds) + \
1176 x * ds_get_bytes_per_pixel(vs->ds)); \
1181 r = (int)((pix >> vs->ds->surface->pf.rshift) \
1182 & vs->ds->surface->pf.rmax); \
1183 g = (int)((pix >> vs->ds->surface->pf.gshift) \
1184 & vs->ds->surface->pf.gmax); \
1185 b = (int)((pix >> vs->ds->surface->pf.bshift) \
1186 & vs->ds->surface->pf.bmax); \
1188 *dst++ = (uint8_t)((r * 255 + vs->ds->surface->pf.rmax / 2) \
1189 / vs->ds->surface->pf.rmax); \
1190 *dst++ = (uint8_t)((g * 255 + vs->ds->surface->pf.gmax / 2) \
1191 / vs->ds->surface->pf.gmax); \
1192 *dst++ = (uint8_t)((b * 255 + vs->ds->surface->pf.bmax / 2) \
1193 / vs->ds->surface->pf.bmax); \
1197 DEFINE_RGB_GET_ROW_FUNCTION(16)
1198 DEFINE_RGB_GET_ROW_FUNCTION(32)
1200 static void rgb_prepare_row(VncState *vs, uint8_t *dst, int x, int y,
1203 if (ds_get_bytes_per_pixel(vs->ds) == 4) {
1204 if (vs->ds->surface->pf.rmax == 0xFF &&
1205 vs->ds->surface->pf.gmax == 0xFF &&
1206 vs->ds->surface->pf.bmax == 0xFF) {
1207 rgb_prepare_row24(vs, dst, x, y, count);
1209 rgb_prepare_row32(vs, dst, x, y, count);
1212 rgb_prepare_row16(vs, dst, x, y, count);
1215 #endif /* CONFIG_VNC_JPEG or CONFIG_VNC_PNG */
1218 * JPEG compression stuff.
1220 #ifdef CONFIG_VNC_JPEG
1222 * Destination manager implementation for JPEG library.
1225 /* This is called once per encoding */
1226 static void jpeg_init_destination(j_compress_ptr cinfo)
1228 VncState *vs = cinfo->client_data;
1229 Buffer *buffer = &vs->tight.jpeg;
1231 cinfo->dest->next_output_byte = (JOCTET *)buffer->buffer + buffer->offset;
1232 cinfo->dest->free_in_buffer = (size_t)(buffer->capacity - buffer->offset);
1235 /* This is called when we ran out of buffer (shouldn't happen!) */
1236 static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo)
1238 VncState *vs = cinfo->client_data;
1239 Buffer *buffer = &vs->tight.jpeg;
1241 buffer->offset = buffer->capacity;
1242 buffer_reserve(buffer, 2048);
1243 jpeg_init_destination(cinfo);
1247 /* This is called when we are done processing data */
1248 static void jpeg_term_destination(j_compress_ptr cinfo)
1250 VncState *vs = cinfo->client_data;
1251 Buffer *buffer = &vs->tight.jpeg;
1253 buffer->offset = buffer->capacity - cinfo->dest->free_in_buffer;
1256 static int send_jpeg_rect(VncState *vs, int x, int y, int w, int h, int quality)
1258 struct jpeg_compress_struct cinfo;
1259 struct jpeg_error_mgr jerr;
1260 struct jpeg_destination_mgr manager;
1265 if (ds_get_bytes_per_pixel(vs->ds) == 1)
1266 return send_full_color_rect(vs, x, y, w, h);
1268 buffer_reserve(&vs->tight.jpeg, 2048);
1270 cinfo.err = jpeg_std_error(&jerr);
1271 jpeg_create_compress(&cinfo);
1273 cinfo.client_data = vs;
1274 cinfo.image_width = w;
1275 cinfo.image_height = h;
1276 cinfo.input_components = 3;
1277 cinfo.in_color_space = JCS_RGB;
1279 jpeg_set_defaults(&cinfo);
1280 jpeg_set_quality(&cinfo, quality, true);
1282 manager.init_destination = jpeg_init_destination;
1283 manager.empty_output_buffer = jpeg_empty_output_buffer;
1284 manager.term_destination = jpeg_term_destination;
1285 cinfo.dest = &manager;
1287 jpeg_start_compress(&cinfo, true);
1289 buf = qemu_malloc(w * 3);
1291 for (dy = 0; dy < h; dy++) {
1292 rgb_prepare_row(vs, buf, x, y + dy, w);
1293 jpeg_write_scanlines(&cinfo, row, 1);
1297 jpeg_finish_compress(&cinfo);
1298 jpeg_destroy_compress(&cinfo);
1300 vnc_write_u8(vs, VNC_TIGHT_JPEG << 4);
1302 tight_send_compact_size(vs, vs->tight.jpeg.offset);
1303 vnc_write(vs, vs->tight.jpeg.buffer, vs->tight.jpeg.offset);
1304 buffer_reset(&vs->tight.jpeg);
1308 #endif /* CONFIG_VNC_JPEG */
1311 * PNG compression stuff.
1313 #ifdef CONFIG_VNC_PNG
1314 static void write_png_palette(int idx, uint32_t pix, void *opaque)
1316 struct palette_cb_priv *priv = opaque;
1317 VncState *vs = priv->vs;
1318 png_colorp color = &priv->png_palette[idx];
1320 if (vs->tight.pixel24)
1322 color->red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
1323 color->green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
1324 color->blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
1328 int red, green, blue;
1330 red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
1331 green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
1332 blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
1333 color->red = ((red * 255 + vs->clientds.pf.rmax / 2) /
1334 vs->clientds.pf.rmax);
1335 color->green = ((green * 255 + vs->clientds.pf.gmax / 2) /
1336 vs->clientds.pf.gmax);
1337 color->blue = ((blue * 255 + vs->clientds.pf.bmax / 2) /
1338 vs->clientds.pf.bmax);
1342 static void png_write_data(png_structp png_ptr, png_bytep data,
1345 VncState *vs = png_get_io_ptr(png_ptr);
1347 buffer_reserve(&vs->tight.png, vs->tight.png.offset + length);
1348 memcpy(vs->tight.png.buffer + vs->tight.png.offset, data, length);
1350 vs->tight.png.offset += length;
1353 static void png_flush_data(png_structp png_ptr)
1357 static void *vnc_png_malloc(png_structp png_ptr, png_size_t size)
1359 return qemu_malloc(size);
1362 static void vnc_png_free(png_structp png_ptr, png_voidp ptr)
1367 static int send_png_rect(VncState *vs, int x, int y, int w, int h,
1368 VncPalette *palette)
1370 png_byte color_type;
1371 png_structp png_ptr;
1373 png_colorp png_palette = NULL;
1374 int level = tight_png_conf[vs->tight.compression].png_zlib_level;
1375 int filters = tight_png_conf[vs->tight.compression].png_filters;
1379 png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL,
1380 NULL, vnc_png_malloc, vnc_png_free);
1382 if (png_ptr == NULL)
1385 info_ptr = png_create_info_struct(png_ptr);
1387 if (info_ptr == NULL) {
1388 png_destroy_write_struct(&png_ptr, NULL);
1392 png_set_write_fn(png_ptr, (void *) vs, png_write_data, png_flush_data);
1393 png_set_compression_level(png_ptr, level);
1394 png_set_filter(png_ptr, PNG_FILTER_TYPE_DEFAULT, filters);
1397 color_type = PNG_COLOR_TYPE_PALETTE;
1399 color_type = PNG_COLOR_TYPE_RGB;
1402 png_set_IHDR(png_ptr, info_ptr, w, h,
1403 8, color_type, PNG_INTERLACE_NONE,
1404 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1406 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1407 struct palette_cb_priv priv;
1409 png_palette = png_malloc(png_ptr, sizeof(*png_palette) *
1410 palette_size(palette));
1413 priv.png_palette = png_palette;
1414 palette_iter(palette, write_png_palette, &priv);
1416 png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size(palette));
1418 if (vs->clientds.pf.bytes_per_pixel == 4) {
1419 tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
1421 tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
1425 png_write_info(png_ptr, info_ptr);
1427 buffer_reserve(&vs->tight.png, 2048);
1428 buf = qemu_malloc(w * 3);
1429 for (dy = 0; dy < h; dy++)
1431 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1432 memcpy(buf, vs->tight.tight.buffer + (dy * w), w);
1434 rgb_prepare_row(vs, buf, x, y + dy, w);
1436 png_write_row(png_ptr, buf);
1440 png_write_end(png_ptr, NULL);
1442 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1443 png_free(png_ptr, png_palette);
1446 png_destroy_write_struct(&png_ptr, &info_ptr);
1448 vnc_write_u8(vs, VNC_TIGHT_PNG << 4);
1450 tight_send_compact_size(vs, vs->tight.png.offset);
1451 vnc_write(vs, vs->tight.png.buffer, vs->tight.png.offset);
1452 buffer_reset(&vs->tight.png);
1455 #endif /* CONFIG_VNC_PNG */
1457 static void vnc_tight_start(VncState *vs)
1459 buffer_reset(&vs->tight.tight);
1461 // make the output buffer be the zlib buffer, so we can compress it later
1462 vs->tight.tmp = vs->output;
1463 vs->output = vs->tight.tight;
1466 static void vnc_tight_stop(VncState *vs)
1468 // switch back to normal output/zlib buffers
1469 vs->tight.tight = vs->output;
1470 vs->output = vs->tight.tmp;
1473 static int send_sub_rect_nojpeg(VncState *vs, int x, int y, int w, int h,
1474 int bg, int fg, int colors, VncPalette *palette)
1479 if (tight_detect_smooth_image(vs, w, h)) {
1480 ret = send_gradient_rect(vs, x, y, w, h);
1482 ret = send_full_color_rect(vs, x, y, w, h);
1484 } else if (colors == 1) {
1485 ret = send_solid_rect(vs);
1486 } else if (colors == 2) {
1487 ret = send_mono_rect(vs, x, y, w, h, bg, fg);
1488 } else if (colors <= 256) {
1489 ret = send_palette_rect(vs, x, y, w, h, palette);
1496 #ifdef CONFIG_VNC_JPEG
1497 static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h,
1498 int bg, int fg, int colors,
1499 VncPalette *palette, bool force)
1504 if (force || (tight_jpeg_conf[vs->tight.quality].jpeg_full &&
1505 tight_detect_smooth_image(vs, w, h))) {
1506 int quality = tight_conf[vs->tight.quality].jpeg_quality;
1508 ret = send_jpeg_rect(vs, x, y, w, h, quality);
1510 ret = send_full_color_rect(vs, x, y, w, h);
1512 } else if (colors == 1) {
1513 ret = send_solid_rect(vs);
1514 } else if (colors == 2) {
1515 ret = send_mono_rect(vs, x, y, w, h, bg, fg);
1516 } else if (colors <= 256) {
1517 if (force || (colors > 96 &&
1518 tight_jpeg_conf[vs->tight.quality].jpeg_idx &&
1519 tight_detect_smooth_image(vs, w, h))) {
1520 int quality = tight_conf[vs->tight.quality].jpeg_quality;
1522 ret = send_jpeg_rect(vs, x, y, w, h, quality);
1524 ret = send_palette_rect(vs, x, y, w, h, palette);
1533 static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
1535 VncPalette *palette = NULL;
1536 uint32_t bg = 0, fg = 0;
1539 bool force_jpeg = false;
1540 bool allow_jpeg = true;
1542 vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
1544 vnc_tight_start(vs);
1545 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1548 #ifdef CONFIG_VNC_JPEG
1549 if (!vs->vd->non_adaptive && vs->tight.quality != (uint8_t)-1) {
1550 double freq = vnc_update_freq(vs, x, y, w, h);
1552 if (freq < tight_jpeg_conf[vs->tight.quality].jpeg_freq_min) {
1555 if (freq >= tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
1557 vnc_sent_lossy_rect(vs, x, y, w, h);
1562 colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
1564 #ifdef CONFIG_VNC_JPEG
1565 if (allow_jpeg && vs->tight.quality != (uint8_t)-1) {
1566 ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, palette,
1569 ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
1572 ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
1575 palette_destroy(palette);
1579 static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
1581 vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
1583 vnc_tight_start(vs);
1584 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1587 return send_solid_rect(vs);
1590 static int send_rect_simple(VncState *vs, int x, int y, int w, int h,
1593 int max_size, max_width;
1594 int max_sub_width, max_sub_height;
1599 max_size = tight_conf[vs->tight.compression].max_rect_size;
1600 max_width = tight_conf[vs->tight.compression].max_rect_width;
1602 if (split && (w > max_width || w * h > max_size)) {
1603 max_sub_width = (w > max_width) ? max_width : w;
1604 max_sub_height = max_size / max_sub_width;
1606 for (dy = 0; dy < h; dy += max_sub_height) {
1607 for (dx = 0; dx < w; dx += max_width) {
1608 rw = MIN(max_sub_width, w - dx);
1609 rh = MIN(max_sub_height, h - dy);
1610 n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
1614 n += send_sub_rect(vs, x, y, w, h);
1620 static int find_large_solid_color_rect(VncState *vs, int x, int y,
1621 int w, int h, int max_rows)
1626 /* Try to find large solid-color areas and send them separately. */
1628 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1630 /* If a rectangle becomes too large, send its upper part now. */
1632 if (dy - y >= max_rows) {
1633 n += send_rect_simple(vs, x, y, w, max_rows, true);
1638 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));
1640 for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1641 uint32_t color_value;
1642 int x_best, y_best, w_best, h_best;
1644 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));
1646 if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
1650 /* Get dimensions of solid-color area. */
1652 find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
1653 color_value, &w_best, &h_best);
1655 /* Make sure a solid rectangle is large enough
1656 (or the whole rectangle is of the same color). */
1658 if (w_best * h_best != w * h &&
1659 w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
1663 /* Try to extend solid rectangle to maximum size. */
1665 x_best = dx; y_best = dy;
1666 extend_solid_area(vs, x, y, w, h, color_value,
1667 &x_best, &y_best, &w_best, &h_best);
1669 /* Send rectangles at top and left to solid-color area. */
1672 n += send_rect_simple(vs, x, y, w, y_best-y, true);
1675 n += tight_send_framebuffer_update(vs, x, y_best,
1679 /* Send solid-color rectangle. */
1680 n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);
1682 /* Send remaining rectangles (at right and bottom). */
1684 if (x_best + w_best != x + w) {
1685 n += tight_send_framebuffer_update(vs, x_best+w_best,
1687 w-(x_best-x)-w_best,
1690 if (y_best + h_best != y + h) {
1691 n += tight_send_framebuffer_update(vs, x, y_best+h_best,
1692 w, h-(y_best-y)-h_best);
1695 /* Return after all recursive calls are done. */
1699 return n + send_rect_simple(vs, x, y, w, h, true);
1702 static int tight_send_framebuffer_update(VncState *vs, int x, int y,
1707 if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF &&
1708 vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) {
1709 vs->tight.pixel24 = true;
1711 vs->tight.pixel24 = false;
1714 if (vs->tight.quality != (uint8_t)-1) {
1715 double freq = vnc_update_freq(vs, x, y, w, h);
1717 if (freq > tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
1718 return send_rect_simple(vs, x, y, w, h, false);
1722 if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE) {
1723 return send_rect_simple(vs, x, y, w, h, true);
1726 /* Calculate maximum number of rows in one non-solid rectangle. */
1728 max_rows = tight_conf[vs->tight.compression].max_rect_size;
1729 max_rows /= MIN(tight_conf[vs->tight.compression].max_rect_width, w);
1731 return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
1734 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
1737 vs->tight.type = VNC_ENCODING_TIGHT;
1738 return tight_send_framebuffer_update(vs, x, y, w, h);
1741 int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y,
1744 vs->tight.type = VNC_ENCODING_TIGHT_PNG;
1745 return tight_send_framebuffer_update(vs, x, y, w, h);
1748 void vnc_tight_clear(VncState *vs)
1751 for (i=0; i<ARRAY_SIZE(vs->tight.stream); i++) {
1752 if (vs->tight.stream[i].opaque) {
1753 deflateEnd(&vs->tight.stream[i]);
1757 buffer_free(&vs->tight.tight);
1758 buffer_free(&vs->tight.zlib);
1759 buffer_free(&vs->tight.gradient);
1760 #ifdef CONFIG_VNC_JPEG
1761 buffer_free(&vs->tight.jpeg);
1763 #ifdef CONFIG_VNC_PNG
1764 buffer_free(&vs->tight.png);