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.
8 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
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"
45 #include "vnc-enc-tight.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
,
76 static int send_png_rect(VncState
*vs
, int x
, int y
, int w
, int h
,
79 static bool tight_can_send_png_rect(VncState
*vs
, int w
, int h
)
81 if (vs
->tight_type
!= VNC_ENCODING_TIGHT_PNG
) {
85 if (ds_get_bytes_per_pixel(vs
->ds
) == 1 ||
86 vs
->clientds
.pf
.bytes_per_pixel
== 1) {
95 * Code to guess if given rectangle is suitable for smooth image
96 * compression (by applying "gradient" filter or JPEG coder).
100 tight_detect_smooth_image24(VncState
*vs
, int w
, int h
)
109 unsigned char *buf
= vs
->tight
.buffer
;
112 * If client is big-endian, color samples begin from the second
113 * byte (offset 1) of a 32-bit pixel value.
115 off
= !!(vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
);
117 memset(stats
, 0, sizeof (stats
));
119 for (y
= 0, x
= 0; y
< h
&& x
< w
;) {
120 for (d
= 0; d
< h
- y
&& d
< w
- x
- VNC_TIGHT_DETECT_SUBROW_WIDTH
;
122 for (c
= 0; c
< 3; c
++) {
123 left
[c
] = buf
[((y
+d
)*w
+x
+d
)*4+off
+c
] & 0xFF;
125 for (dx
= 1; dx
<= VNC_TIGHT_DETECT_SUBROW_WIDTH
; dx
++) {
126 for (c
= 0; c
< 3; c
++) {
127 pix
= buf
[((y
+d
)*w
+x
+d
+dx
)*4+off
+c
] & 0xFF;
128 stats
[abs(pix
- left
[c
])]++;
143 /* 95% smooth or more ... */
144 if (stats
[0] * 33 / pixels
>= 95) {
149 for (c
= 1; c
< 8; c
++) {
150 errors
+= stats
[c
] * (c
* c
);
151 if (stats
[c
] == 0 || stats
[c
] > stats
[c
-1] * 2) {
155 for (; c
< 256; c
++) {
156 errors
+= stats
[c
] * (c
* c
);
158 errors
/= (pixels
* 3 - stats
[0]);
163 #define DEFINE_DETECT_FUNCTION(bpp) \
166 tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \
169 int max[3], shift[3]; \
174 int sample, sum, left[3]; \
176 unsigned char *buf = vs->tight.buffer; \
178 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
179 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
182 max[0] = vs->clientds.pf.rmax; \
183 max[1] = vs->clientds.pf.gmax; \
184 max[2] = vs->clientds.pf.bmax; \
185 shift[0] = vs->clientds.pf.rshift; \
186 shift[1] = vs->clientds.pf.gshift; \
187 shift[2] = vs->clientds.pf.bshift; \
189 memset(stats, 0, sizeof(stats)); \
192 while (y < h && x < w) { \
193 for (d = 0; d < h - y && \
194 d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \
195 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \
197 pix = bswap_##bpp(pix); \
199 for (c = 0; c < 3; c++) { \
200 left[c] = (int)(pix >> shift[c] & max[c]); \
202 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \
204 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \
206 pix = bswap_##bpp(pix); \
209 for (c = 0; c < 3; c++) { \
210 sample = (int)(pix >> shift[c] & max[c]); \
211 sum += abs(sample - left[c]); \
230 if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \
235 for (c = 1; c < 8; c++) { \
236 errors += stats[c] * (c * c); \
237 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \
241 for (; c < 256; c++) { \
242 errors += stats[c] * (c * c); \
244 errors /= (pixels - stats[0]); \
249 DEFINE_DETECT_FUNCTION(16)
250 DEFINE_DETECT_FUNCTION(32)
253 tight_detect_smooth_image(VncState
*vs
, int w
, int h
)
256 int compression
= vs
->tight_compression
;
257 int quality
= vs
->tight_quality
;
259 if (!vs
->vd
->lossy
) {
263 if (ds_get_bytes_per_pixel(vs
->ds
) == 1 ||
264 vs
->clientds
.pf
.bytes_per_pixel
== 1 ||
265 w
< VNC_TIGHT_DETECT_MIN_WIDTH
|| h
< VNC_TIGHT_DETECT_MIN_HEIGHT
) {
269 if (vs
->tight_quality
!= -1) {
270 if (w
* h
< VNC_TIGHT_JPEG_MIN_RECT_SIZE
) {
274 if (w
* h
< tight_conf
[compression
].gradient_min_rect_size
) {
279 if (vs
->clientds
.pf
.bytes_per_pixel
== 4) {
280 if (vs
->tight_pixel24
) {
281 errors
= tight_detect_smooth_image24(vs
, w
, h
);
282 if (vs
->tight_quality
!= -1) {
283 return (errors
< tight_conf
[quality
].jpeg_threshold24
);
285 return (errors
< tight_conf
[compression
].gradient_threshold24
);
287 errors
= tight_detect_smooth_image32(vs
, w
, h
);
290 errors
= tight_detect_smooth_image16(vs
, w
, h
);
293 return (errors
< tight_conf
[quality
].jpeg_threshold
);
295 return (errors
< tight_conf
[compression
].gradient_threshold
);
299 * Code to determine how many different colors used in rectangle.
302 static void tight_palette_rgb2buf(uint32_t rgb
, int bpp
, uint8_t buf
[6])
307 buf
[0] = ((rgb
>> 24) & 0xFF);
308 buf
[1] = ((rgb
>> 16) & 0xFF);
309 buf
[2] = ((rgb
>> 8) & 0xFF);
310 buf
[3] = ((rgb
>> 0) & 0xFF);
311 buf
[4] = ((buf
[0] & 1) == 0) << 3 | ((buf
[1] & 1) == 0) << 2;
312 buf
[4]|= ((buf
[2] & 1) == 0) << 1 | ((buf
[3] & 1) == 0) << 0;
319 buf
[0] = ((rgb
>> 8) & 0xFF);
320 buf
[1] = ((rgb
>> 0) & 0xFF);
321 buf
[2] = ((buf
[0] & 1) == 0) << 1 | ((buf
[1] & 1) == 0) << 0;
327 static uint32_t tight_palette_buf2rgb(int bpp
, const uint8_t *buf
)
332 rgb
|= ((buf
[0] & ~1) | !((buf
[4] >> 3) & 1)) << 24;
333 rgb
|= ((buf
[1] & ~1) | !((buf
[4] >> 2) & 1)) << 16;
334 rgb
|= ((buf
[2] & ~1) | !((buf
[4] >> 1) & 1)) << 8;
335 rgb
|= ((buf
[3] & ~1) | !((buf
[4] >> 0) & 1)) << 0;
338 rgb
|= ((buf
[0] & ~1) | !((buf
[2] >> 1) & 1)) << 8;
339 rgb
|= ((buf
[1] & ~1) | !((buf
[2] >> 0) & 1)) << 0;
345 static int tight_palette_insert(QDict
*palette
, uint32_t rgb
, int bpp
, int max
)
348 int idx
= qdict_size(palette
);
351 tight_palette_rgb2buf(rgb
, bpp
, key
);
352 present
= qdict_haskey(palette
, (char *)key
);
353 if (idx
>= max
&& !present
) {
357 qdict_put(palette
, (char *)key
, qint_from_int(idx
));
359 return qdict_size(palette
);
362 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
365 tight_fill_palette##bpp(VncState *vs, int x, int y, \
366 int max, size_t count, \
367 uint32_t *bg, uint32_t *fg, \
368 struct QDict **palette) { \
369 uint##bpp##_t *data; \
370 uint##bpp##_t c0, c1, ci; \
373 data = (uint##bpp##_t *)vs->tight.buffer; \
377 while (i < count && data[i] == c0) \
391 for (i++; i < count; i++) { \
395 } else if (ci == c1) { \
402 *bg = (uint32_t)c0; \
403 *fg = (uint32_t)c1; \
405 *bg = (uint32_t)c1; \
406 *fg = (uint32_t)c0; \
415 *palette = qdict_new(); \
416 tight_palette_insert(*palette, c0, bpp, max); \
417 tight_palette_insert(*palette, c1, bpp, max); \
418 tight_palette_insert(*palette, ci, bpp, max); \
420 for (i++; i < count; i++) { \
421 if (data[i] == ci) { \
425 if (!tight_palette_insert(*palette, (uint32_t)ci, \
432 return qdict_size(*palette); \
435 DEFINE_FILL_PALETTE_FUNCTION(8)
436 DEFINE_FILL_PALETTE_FUNCTION(16)
437 DEFINE_FILL_PALETTE_FUNCTION(32)
439 static int tight_fill_palette(VncState
*vs
, int x
, int y
,
440 size_t count
, uint32_t *bg
, uint32_t *fg
,
441 struct QDict
**palette
)
445 max
= count
/ tight_conf
[vs
->tight_compression
].idx_max_colors_divisor
;
447 count
>= tight_conf
[vs
->tight_compression
].mono_min_rect_size
) {
454 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
456 return tight_fill_palette32(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
458 return tight_fill_palette16(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
461 return tight_fill_palette8(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
466 /* Callback to dump a palette with qdict_iter
467 static void print_palette(const char *key, QObject *obj, void *opaque)
469 uint8_t idx = qint_get_int(qobject_to_qint(obj));
470 uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
472 fprintf(stderr, "%.2x ", (unsigned char)*key);
474 fprintf(stderr, "%.2x ", (unsigned char)*key);
476 fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
481 * Converting truecolor samples into palette indices.
483 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
486 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
487 struct QDict *palette) { \
488 uint##bpp##_t *src; \
494 src = (uint##bpp##_t *) buf; \
496 for (i = 0; i < count; i++) { \
500 while (i < count && *src == rgb) { \
503 tight_palette_rgb2buf(rgb, bpp, key); \
504 if (!qdict_haskey(palette, (char *)key)) { \
506 * Should never happen, but don't break everything \
507 * if it does, use the first color instead \
511 idx = qdict_get_int(palette, (char *)key); \
520 DEFINE_IDX_ENCODE_FUNCTION(16)
521 DEFINE_IDX_ENCODE_FUNCTION(32)
523 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
526 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
527 uint##bpp##_t bg, uint##bpp##_t fg) { \
528 uint##bpp##_t *ptr; \
529 unsigned int value, mask; \
533 ptr = (uint##bpp##_t *) buf; \
534 aligned_width = w - w % 8; \
536 for (y = 0; y < h; y++) { \
537 for (x = 0; x < aligned_width; x += 8) { \
538 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
539 if (*ptr++ != bg) { \
543 if (bg_bits == 8) { \
547 mask = 0x80 >> bg_bits; \
549 for (bg_bits++; bg_bits < 8; bg_bits++) { \
551 if (*ptr++ != bg) { \
555 *buf++ = (uint8_t)value; \
564 for (; x < w; x++) { \
565 if (*ptr++ != bg) { \
570 *buf++ = (uint8_t)value; \
574 DEFINE_MONO_ENCODE_FUNCTION(8)
575 DEFINE_MONO_ENCODE_FUNCTION(16)
576 DEFINE_MONO_ENCODE_FUNCTION(32)
579 * ``Gradient'' filter for 24-bit color samples.
580 * Should be called only when redMax, greenMax and blueMax are 255.
581 * Color components assumed to be byte-aligned.
585 tight_filter_gradient24(VncState
*vs
, uint8_t *buf
, int w
, int h
)
591 int here
[3], upper
[3], left
[3], upperleft
[3];
595 buf32
= (uint32_t *)buf
;
596 memset(vs
->tight_gradient
.buffer
, 0, w
* 3 * sizeof(int));
598 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
599 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
)) {
600 shift
[0] = vs
->clientds
.pf
.rshift
;
601 shift
[1] = vs
->clientds
.pf
.gshift
;
602 shift
[2] = vs
->clientds
.pf
.bshift
;
604 shift
[0] = 24 - vs
->clientds
.pf
.rshift
;
605 shift
[1] = 24 - vs
->clientds
.pf
.gshift
;
606 shift
[2] = 24 - vs
->clientds
.pf
.bshift
;
609 for (y
= 0; y
< h
; y
++) {
610 for (c
= 0; c
< 3; c
++) {
614 prev
= (int *)vs
->tight_gradient
.buffer
;
615 for (x
= 0; x
< w
; x
++) {
617 for (c
= 0; c
< 3; c
++) {
618 upperleft
[c
] = upper
[c
];
621 here
[c
] = (int)(pix32
>> shift
[c
] & 0xFF);
624 prediction
= left
[c
] + upper
[c
] - upperleft
[c
];
625 if (prediction
< 0) {
627 } else if (prediction
> 0xFF) {
630 *buf
++ = (char)(here
[c
] - prediction
);
638 * ``Gradient'' filter for other color depths.
641 #define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \
644 tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \
646 uint##bpp##_t pix, diff; \
649 int max[3], shift[3]; \
650 int here[3], upper[3], left[3], upperleft[3]; \
654 memset (vs->tight_gradient.buffer, 0, w * 3 * sizeof(int)); \
656 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
657 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
659 max[0] = vs->clientds.pf.rmax; \
660 max[1] = vs->clientds.pf.gmax; \
661 max[2] = vs->clientds.pf.bmax; \
662 shift[0] = vs->clientds.pf.rshift; \
663 shift[1] = vs->clientds.pf.gshift; \
664 shift[2] = vs->clientds.pf.bshift; \
666 for (y = 0; y < h; y++) { \
667 for (c = 0; c < 3; c++) { \
671 prev = (int *)vs->tight_gradient.buffer; \
672 for (x = 0; x < w; x++) { \
675 pix = bswap_##bpp(pix); \
678 for (c = 0; c < 3; c++) { \
679 upperleft[c] = upper[c]; \
682 here[c] = (int)(pix >> shift[c] & max[c]); \
685 prediction = left[c] + upper[c] - upperleft[c]; \
686 if (prediction < 0) { \
688 } else if (prediction > max[c]) { \
689 prediction = max[c]; \
691 diff |= ((here[c] - prediction) & max[c]) \
695 diff = bswap_##bpp(diff); \
702 DEFINE_GRADIENT_FILTER_FUNCTION(16)
703 DEFINE_GRADIENT_FILTER_FUNCTION(32)
706 * Check if a rectangle is all of the same color. If needSameColor is
707 * set to non-zero, then also check that its color equals to the
708 * *colorPtr value. The result is 1 if the test is successfull, and in
709 * that case new color will be stored in *colorPtr.
712 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
715 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
716 uint32_t* color, bool samecolor) \
718 VncDisplay *vd = vs->vd; \
719 uint##bpp##_t *fbptr; \
723 fbptr = (uint##bpp##_t *) \
724 (vd->server->data + y * ds_get_linesize(vs->ds) + \
725 x * ds_get_bytes_per_pixel(vs->ds)); \
728 if (samecolor && (uint32_t)c != *color) { \
732 for (dy = 0; dy < h; dy++) { \
733 for (dx = 0; dx < w; dx++) { \
734 if (c != fbptr[dx]) { \
738 fbptr = (uint##bpp##_t *) \
739 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
742 *color = (uint32_t)c; \
746 DEFINE_CHECK_SOLID_FUNCTION(32)
747 DEFINE_CHECK_SOLID_FUNCTION(16)
748 DEFINE_CHECK_SOLID_FUNCTION(8)
750 static bool check_solid_tile(VncState
*vs
, int x
, int y
, int w
, int h
,
751 uint32_t* color
, bool samecolor
)
753 VncDisplay
*vd
= vs
->vd
;
755 switch(vd
->server
->pf
.bytes_per_pixel
) {
757 return check_solid_tile32(vs
, x
, y
, w
, h
, color
, samecolor
);
759 return check_solid_tile16(vs
, x
, y
, w
, h
, color
, samecolor
);
761 return check_solid_tile8(vs
, x
, y
, w
, h
, color
, samecolor
);
765 static void find_best_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
766 uint32_t color
, int *w_ptr
, int *h_ptr
)
770 int w_best
= 0, h_best
= 0;
774 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
776 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, y
+ h
- dy
);
777 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, w_prev
);
779 if (!check_solid_tile(vs
, x
, dy
, dw
, dh
, &color
, true)) {
783 for (dx
= x
+ dw
; dx
< x
+ w_prev
;) {
784 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, x
+ w_prev
- dx
);
786 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color
, true)) {
793 if (w_prev
* (dy
+ dh
- y
) > w_best
* h_best
) {
795 h_best
= dy
+ dh
- y
;
803 static void extend_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
804 uint32_t color
, int *x_ptr
, int *y_ptr
,
805 int *w_ptr
, int *h_ptr
)
809 /* Try to extend the area upwards. */
810 for ( cy
= *y_ptr
- 1;
811 cy
>= y
&& check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
813 *h_ptr
+= *y_ptr
- (cy
+ 1);
817 for ( cy
= *y_ptr
+ *h_ptr
;
819 check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
821 *h_ptr
+= cy
- (*y_ptr
+ *h_ptr
);
823 /* ... to the left. */
824 for ( cx
= *x_ptr
- 1;
825 cx
>= x
&& check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
827 *w_ptr
+= *x_ptr
- (cx
+ 1);
830 /* ... to the right. */
831 for ( cx
= *x_ptr
+ *w_ptr
;
833 check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
835 *w_ptr
+= cx
- (*x_ptr
+ *w_ptr
);
838 static int tight_init_stream(VncState
*vs
, int stream_id
,
839 int level
, int strategy
)
841 z_streamp zstream
= &vs
->tight_stream
[stream_id
];
843 if (zstream
->opaque
== NULL
) {
846 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id
);
847 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream
->opaque
, vs
);
848 zstream
->zalloc
= vnc_zlib_zalloc
;
849 zstream
->zfree
= vnc_zlib_zfree
;
851 err
= deflateInit2(zstream
, level
, Z_DEFLATED
, MAX_WBITS
,
852 MAX_MEM_LEVEL
, strategy
);
855 fprintf(stderr
, "VNC: error initializing zlib\n");
859 vs
->tight_levels
[stream_id
] = level
;
860 zstream
->opaque
= vs
;
863 if (vs
->tight_levels
[stream_id
] != level
) {
864 if (deflateParams(zstream
, level
, strategy
) != Z_OK
) {
867 vs
->tight_levels
[stream_id
] = level
;
872 static void tight_send_compact_size(VncState
*vs
, size_t len
)
876 char buf
[3] = {0, 0, 0};
878 buf
[bytes
++] = len
& 0x7F;
880 buf
[bytes
-1] |= 0x80;
881 buf
[bytes
++] = (len
>> 7) & 0x7F;
883 buf
[bytes
-1] |= 0x80;
884 buf
[bytes
++] = (len
>> 14) & 0xFF;
887 for (lpc
= 0; lpc
< bytes
; lpc
++) {
888 vnc_write_u8(vs
, buf
[lpc
]);
892 static int tight_compress_data(VncState
*vs
, int stream_id
, size_t bytes
,
893 int level
, int strategy
)
895 z_streamp zstream
= &vs
->tight_stream
[stream_id
];
898 if (bytes
< VNC_TIGHT_MIN_TO_COMPRESS
) {
899 vnc_write(vs
, vs
->tight
.buffer
, vs
->tight
.offset
);
903 if (tight_init_stream(vs
, stream_id
, level
, strategy
)) {
907 /* reserve memory in output buffer */
908 buffer_reserve(&vs
->tight_zlib
, bytes
+ 64);
911 zstream
->next_in
= vs
->tight
.buffer
;
912 zstream
->avail_in
= vs
->tight
.offset
;
913 zstream
->next_out
= vs
->tight_zlib
.buffer
+ vs
->tight_zlib
.offset
;
914 zstream
->avail_out
= vs
->tight_zlib
.capacity
- vs
->tight_zlib
.offset
;
915 zstream
->data_type
= Z_BINARY
;
916 previous_out
= zstream
->total_out
;
919 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
920 fprintf(stderr
, "VNC: error during tight compression\n");
924 vs
->tight_zlib
.offset
= vs
->tight_zlib
.capacity
- zstream
->avail_out
;
925 bytes
= zstream
->total_out
- previous_out
;
927 tight_send_compact_size(vs
, bytes
);
928 vnc_write(vs
, vs
->tight_zlib
.buffer
, bytes
);
930 buffer_reset(&vs
->tight_zlib
);
936 * Subencoding implementations.
938 static void tight_pack24(VncState
*vs
, uint8_t *buf
, size_t count
, size_t *ret
)
942 int rshift
, gshift
, bshift
;
944 buf32
= (uint32_t *)buf
;
946 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
947 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
)) {
948 rshift
= vs
->clientds
.pf
.rshift
;
949 gshift
= vs
->clientds
.pf
.gshift
;
950 bshift
= vs
->clientds
.pf
.bshift
;
952 rshift
= 24 - vs
->clientds
.pf
.rshift
;
953 gshift
= 24 - vs
->clientds
.pf
.gshift
;
954 bshift
= 24 - vs
->clientds
.pf
.bshift
;
963 *buf
++ = (char)(pix
>> rshift
);
964 *buf
++ = (char)(pix
>> gshift
);
965 *buf
++ = (char)(pix
>> bshift
);
969 static int send_full_color_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
974 #ifdef CONFIG_VNC_PNG
975 if (tight_can_send_png_rect(vs
, w
, h
)) {
976 return send_png_rect(vs
, x
, y
, w
, h
, NULL
);
980 vnc_write_u8(vs
, stream
<< 4); /* no flushing, no filter */
982 if (vs
->tight_pixel24
) {
983 tight_pack24(vs
, vs
->tight
.buffer
, w
* h
, &vs
->tight
.offset
);
986 bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
989 bytes
= tight_compress_data(vs
, stream
, w
* h
* bytes
,
990 tight_conf
[vs
->tight_compression
].raw_zlib_level
,
996 static int send_solid_rect(VncState
*vs
)
1000 vnc_write_u8(vs
, VNC_TIGHT_FILL
<< 4); /* no flushing, no filter */
1002 if (vs
->tight_pixel24
) {
1003 tight_pack24(vs
, vs
->tight
.buffer
, 1, &vs
->tight
.offset
);
1006 bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
1009 vnc_write(vs
, vs
->tight
.buffer
, bytes
);
1013 static int send_mono_rect(VncState
*vs
, int x
, int y
,
1014 int w
, int h
, uint32_t bg
, uint32_t fg
)
1018 int level
= tight_conf
[vs
->tight_compression
].mono_zlib_level
;
1020 #ifdef CONFIG_VNC_PNG
1021 if (tight_can_send_png_rect(vs
, w
, h
)) {
1023 QDict
*palette
= qdict_new();
1024 int bpp
= vs
->clientds
.pf
.bytes_per_pixel
* 8;
1026 tight_palette_insert(palette
, bg
, bpp
, 2);
1027 tight_palette_insert(palette
, fg
, bpp
, 2);
1028 ret
= send_png_rect(vs
, x
, y
, w
, h
, palette
);
1034 bytes
= ((w
+ 7) / 8) * h
;
1036 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
1037 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
1038 vnc_write_u8(vs
, 1);
1040 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
1043 uint32_t buf
[2] = {bg
, fg
};
1044 size_t ret
= sizeof (buf
);
1046 if (vs
->tight_pixel24
) {
1047 tight_pack24(vs
, (unsigned char*)buf
, 2, &ret
);
1049 vnc_write(vs
, buf
, ret
);
1051 tight_encode_mono_rect32(vs
->tight
.buffer
, w
, h
, bg
, fg
);
1055 vnc_write(vs
, &bg
, 2);
1056 vnc_write(vs
, &fg
, 2);
1057 tight_encode_mono_rect16(vs
->tight
.buffer
, w
, h
, bg
, fg
);
1060 vnc_write_u8(vs
, bg
);
1061 vnc_write_u8(vs
, fg
);
1062 tight_encode_mono_rect8(vs
->tight
.buffer
, w
, h
, bg
, fg
);
1065 vs
->tight
.offset
= bytes
;
1067 bytes
= tight_compress_data(vs
, stream
, bytes
, level
, Z_DEFAULT_STRATEGY
);
1068 return (bytes
>= 0);
1071 struct palette_cb_priv
{
1074 #ifdef CONFIG_VNC_PNG
1075 png_colorp png_palette
;
1079 static void write_palette(const char *key
, QObject
*obj
, void *opaque
)
1081 struct palette_cb_priv
*priv
= opaque
;
1082 VncState
*vs
= priv
->vs
;
1083 uint32_t bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
1084 uint8_t idx
= qint_get_int(qobject_to_qint(obj
));
1087 uint32_t color
= tight_palette_buf2rgb(32, (uint8_t *)key
);
1089 ((uint32_t*)priv
->header
)[idx
] = color
;
1091 uint16_t color
= tight_palette_buf2rgb(16, (uint8_t *)key
);
1093 ((uint16_t*)priv
->header
)[idx
] = color
;
1097 static bool send_gradient_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
1100 int level
= tight_conf
[vs
->tight_compression
].gradient_zlib_level
;
1103 if (vs
->clientds
.pf
.bytes_per_pixel
== 1)
1104 return send_full_color_rect(vs
, x
, y
, w
, h
);
1106 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
1107 vnc_write_u8(vs
, VNC_TIGHT_FILTER_GRADIENT
);
1109 buffer_reserve(&vs
->tight_gradient
, w
* 3 * sizeof (int));
1111 if (vs
->tight_pixel24
) {
1112 tight_filter_gradient24(vs
, vs
->tight
.buffer
, w
, h
);
1114 } else if (vs
->clientds
.pf
.bytes_per_pixel
== 4) {
1115 tight_filter_gradient32(vs
, (uint32_t *)vs
->tight
.buffer
, w
, h
);
1118 tight_filter_gradient16(vs
, (uint16_t *)vs
->tight
.buffer
, w
, h
);
1122 buffer_reset(&vs
->tight_gradient
);
1124 bytes
= w
* h
* bytes
;
1125 vs
->tight
.offset
= bytes
;
1127 bytes
= tight_compress_data(vs
, stream
, bytes
,
1129 return (bytes
>= 0);
1132 static int send_palette_rect(VncState
*vs
, int x
, int y
,
1133 int w
, int h
, struct QDict
*palette
)
1136 int level
= tight_conf
[vs
->tight_compression
].idx_zlib_level
;
1140 #ifdef CONFIG_VNC_PNG
1141 if (tight_can_send_png_rect(vs
, w
, h
)) {
1142 return send_png_rect(vs
, x
, y
, w
, h
, palette
);
1146 colors
= qdict_size(palette
);
1148 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
1149 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
1150 vnc_write_u8(vs
, colors
- 1);
1152 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
1155 size_t old_offset
, offset
;
1156 uint32_t header
[qdict_size(palette
)];
1157 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
1159 old_offset
= vs
->output
.offset
;
1160 qdict_iter(palette
, write_palette
, &priv
);
1161 vnc_write(vs
, header
, sizeof(header
));
1163 if (vs
->tight_pixel24
) {
1164 tight_pack24(vs
, vs
->output
.buffer
+ old_offset
, colors
, &offset
);
1165 vs
->output
.offset
= old_offset
+ offset
;
1168 tight_encode_indexed_rect32(vs
->tight
.buffer
, w
* h
, palette
);
1173 uint16_t header
[qdict_size(palette
)];
1174 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
1176 qdict_iter(palette
, write_palette
, &priv
);
1177 vnc_write(vs
, header
, sizeof(header
));
1178 tight_encode_indexed_rect16(vs
->tight
.buffer
, w
* h
, palette
);
1182 return -1; /* No palette for 8bits colors */
1186 vs
->tight
.offset
= bytes
;
1188 bytes
= tight_compress_data(vs
, stream
, bytes
,
1189 level
, Z_DEFAULT_STRATEGY
);
1190 return (bytes
>= 0);
1193 #if defined(CONFIG_VNC_JPEG) || defined(CONFIG_VNC_PNG)
1194 static void rgb_prepare_row24(VncState
*vs
, uint8_t *dst
, int x
, int y
,
1197 VncDisplay
*vd
= vs
->vd
;
1201 fbptr
= (uint32_t *)(vd
->server
->data
+ y
* ds_get_linesize(vs
->ds
) +
1202 x
* ds_get_bytes_per_pixel(vs
->ds
));
1206 *dst
++ = (uint8_t)(pix
>> vs
->ds
->surface
->pf
.rshift
);
1207 *dst
++ = (uint8_t)(pix
>> vs
->ds
->surface
->pf
.gshift
);
1208 *dst
++ = (uint8_t)(pix
>> vs
->ds
->surface
->pf
.bshift
);
1212 #define DEFINE_RGB_GET_ROW_FUNCTION(bpp) \
1215 rgb_prepare_row##bpp(VncState *vs, uint8_t *dst, \
1216 int x, int y, int count) \
1218 VncDisplay *vd = vs->vd; \
1219 uint##bpp##_t *fbptr; \
1220 uint##bpp##_t pix; \
1223 fbptr = (uint##bpp##_t *) \
1224 (vd->server->data + y * ds_get_linesize(vs->ds) + \
1225 x * ds_get_bytes_per_pixel(vs->ds)); \
1230 r = (int)((pix >> vs->ds->surface->pf.rshift) \
1231 & vs->ds->surface->pf.rmax); \
1232 g = (int)((pix >> vs->ds->surface->pf.gshift) \
1233 & vs->ds->surface->pf.gmax); \
1234 b = (int)((pix >> vs->ds->surface->pf.bshift) \
1235 & vs->ds->surface->pf.bmax); \
1237 *dst++ = (uint8_t)((r * 255 + vs->ds->surface->pf.rmax / 2) \
1238 / vs->ds->surface->pf.rmax); \
1239 *dst++ = (uint8_t)((g * 255 + vs->ds->surface->pf.gmax / 2) \
1240 / vs->ds->surface->pf.gmax); \
1241 *dst++ = (uint8_t)((b * 255 + vs->ds->surface->pf.bmax / 2) \
1242 / vs->ds->surface->pf.bmax); \
1246 DEFINE_RGB_GET_ROW_FUNCTION(16)
1247 DEFINE_RGB_GET_ROW_FUNCTION(32)
1249 static void rgb_prepare_row(VncState
*vs
, uint8_t *dst
, int x
, int y
,
1252 if (vs
->tight_pixel24
)
1253 rgb_prepare_row24(vs
, dst
, x
, y
, count
);
1254 else if (ds_get_bytes_per_pixel(vs
->ds
) == 4)
1255 rgb_prepare_row32(vs
, dst
, x
, y
, count
);
1257 rgb_prepare_row16(vs
, dst
, x
, y
, count
);
1259 #endif /* CONFIG_VNC_JPEG or CONFIG_VNC_PNG */
1262 * JPEG compression stuff.
1264 #ifdef CONFIG_VNC_JPEG
1266 * Destination manager implementation for JPEG library.
1269 /* This is called once per encoding */
1270 static void jpeg_init_destination(j_compress_ptr cinfo
)
1272 VncState
*vs
= cinfo
->client_data
;
1273 Buffer
*buffer
= &vs
->tight_jpeg
;
1275 cinfo
->dest
->next_output_byte
= (JOCTET
*)buffer
->buffer
+ buffer
->offset
;
1276 cinfo
->dest
->free_in_buffer
= (size_t)(buffer
->capacity
- buffer
->offset
);
1279 /* This is called when we ran out of buffer (shouldn't happen!) */
1280 static boolean
jpeg_empty_output_buffer(j_compress_ptr cinfo
)
1282 VncState
*vs
= cinfo
->client_data
;
1283 Buffer
*buffer
= &vs
->tight_jpeg
;
1285 buffer
->offset
= buffer
->capacity
;
1286 buffer_reserve(buffer
, 2048);
1287 jpeg_init_destination(cinfo
);
1291 /* This is called when we are done processing data */
1292 static void jpeg_term_destination(j_compress_ptr cinfo
)
1294 VncState
*vs
= cinfo
->client_data
;
1295 Buffer
*buffer
= &vs
->tight_jpeg
;
1297 buffer
->offset
= buffer
->capacity
- cinfo
->dest
->free_in_buffer
;
1300 static int send_jpeg_rect(VncState
*vs
, int x
, int y
, int w
, int h
, int quality
)
1302 struct jpeg_compress_struct cinfo
;
1303 struct jpeg_error_mgr jerr
;
1304 struct jpeg_destination_mgr manager
;
1309 if (ds_get_bytes_per_pixel(vs
->ds
) == 1)
1310 return send_full_color_rect(vs
, x
, y
, w
, h
);
1312 buffer_reserve(&vs
->tight_jpeg
, 2048);
1314 cinfo
.err
= jpeg_std_error(&jerr
);
1315 jpeg_create_compress(&cinfo
);
1317 cinfo
.client_data
= vs
;
1318 cinfo
.image_width
= w
;
1319 cinfo
.image_height
= h
;
1320 cinfo
.input_components
= 3;
1321 cinfo
.in_color_space
= JCS_RGB
;
1323 jpeg_set_defaults(&cinfo
);
1324 jpeg_set_quality(&cinfo
, quality
, true);
1326 manager
.init_destination
= jpeg_init_destination
;
1327 manager
.empty_output_buffer
= jpeg_empty_output_buffer
;
1328 manager
.term_destination
= jpeg_term_destination
;
1329 cinfo
.dest
= &manager
;
1331 jpeg_start_compress(&cinfo
, true);
1333 buf
= qemu_malloc(w
* 3);
1335 for (dy
= 0; dy
< h
; dy
++) {
1336 rgb_prepare_row(vs
, buf
, x
, y
+ dy
, w
);
1337 jpeg_write_scanlines(&cinfo
, row
, 1);
1341 jpeg_finish_compress(&cinfo
);
1342 jpeg_destroy_compress(&cinfo
);
1344 vnc_write_u8(vs
, VNC_TIGHT_JPEG
<< 4);
1346 tight_send_compact_size(vs
, vs
->tight_jpeg
.offset
);
1347 vnc_write(vs
, vs
->tight_jpeg
.buffer
, vs
->tight_jpeg
.offset
);
1348 buffer_reset(&vs
->tight_jpeg
);
1352 #endif /* CONFIG_VNC_JPEG */
1355 * PNG compression stuff.
1357 #ifdef CONFIG_VNC_PNG
1358 static void write_png_palette(const char *key
, QObject
*obj
, void *opaque
)
1360 struct palette_cb_priv
*priv
= opaque
;
1361 VncState
*vs
= priv
->vs
;
1362 uint32_t bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
1363 uint8_t idx
= qint_get_int(qobject_to_qint(obj
));
1364 png_colorp color
= &priv
->png_palette
[idx
];
1368 pix
= tight_palette_buf2rgb(32, (uint8_t *)key
);
1370 pix
= tight_palette_buf2rgb(16, (uint8_t *)key
);
1373 if (vs
->tight_pixel24
)
1375 color
->red
= (pix
>> vs
->clientds
.pf
.rshift
) & vs
->clientds
.pf
.rmax
;
1376 color
->green
= (pix
>> vs
->clientds
.pf
.gshift
) & vs
->clientds
.pf
.gmax
;
1377 color
->blue
= (pix
>> vs
->clientds
.pf
.bshift
) & vs
->clientds
.pf
.bmax
;
1381 int red
, green
, blue
;
1383 red
= (pix
>> vs
->clientds
.pf
.rshift
) & vs
->clientds
.pf
.rmax
;
1384 green
= (pix
>> vs
->clientds
.pf
.gshift
) & vs
->clientds
.pf
.gmax
;
1385 blue
= (pix
>> vs
->clientds
.pf
.bshift
) & vs
->clientds
.pf
.bmax
;
1386 color
->red
= ((red
* 255 + vs
->clientds
.pf
.rmax
/ 2) /
1387 vs
->clientds
.pf
.rmax
);
1388 color
->green
= ((green
* 255 + vs
->clientds
.pf
.gmax
/ 2) /
1389 vs
->clientds
.pf
.gmax
);
1390 color
->blue
= ((blue
* 255 + vs
->clientds
.pf
.bmax
/ 2) /
1391 vs
->clientds
.pf
.bmax
);
1395 static void png_write_data(png_structp png_ptr
, png_bytep data
,
1398 VncState
*vs
= png_get_io_ptr(png_ptr
);
1400 buffer_reserve(&vs
->tight_png
, vs
->tight_png
.offset
+ length
);
1401 memcpy(vs
->tight_png
.buffer
+ vs
->tight_png
.offset
, data
, length
);
1403 vs
->tight_png
.offset
+= length
;
1406 static void png_flush_data(png_structp png_ptr
)
1410 static void *vnc_png_malloc(png_structp png_ptr
, png_size_t size
)
1412 return qemu_malloc(size
);
1415 static void vnc_png_free(png_structp png_ptr
, png_voidp ptr
)
1420 static int send_png_rect(VncState
*vs
, int x
, int y
, int w
, int h
,
1423 png_byte color_type
;
1424 png_structp png_ptr
;
1426 png_colorp png_palette
= NULL
;
1428 int level
= tight_conf
[vs
->tight_compression
].raw_zlib_level
;
1432 png_ptr
= png_create_write_struct_2(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
,
1433 NULL
, vnc_png_malloc
, vnc_png_free
);
1435 if (png_ptr
== NULL
)
1438 info_ptr
= png_create_info_struct(png_ptr
);
1440 if (info_ptr
== NULL
) {
1441 png_destroy_write_struct(&png_ptr
, NULL
);
1445 png_set_write_fn(png_ptr
, (void *) vs
, png_write_data
, png_flush_data
);
1446 png_set_compression_level(png_ptr
, level
);
1449 color_type
= PNG_COLOR_TYPE_PALETTE
;
1451 color_type
= PNG_COLOR_TYPE_RGB
;
1454 png_set_IHDR(png_ptr
, info_ptr
, w
, h
,
1455 8, color_type
, PNG_INTERLACE_NONE
,
1456 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
1458 if (color_type
== PNG_COLOR_TYPE_PALETTE
) {
1459 struct palette_cb_priv priv
;
1461 png_palette
= png_malloc(png_ptr
, sizeof(*png_palette
) *
1462 qdict_size(palette
));
1465 priv
.png_palette
= png_palette
;
1466 qdict_iter(palette
, write_png_palette
, &priv
);
1468 png_set_PLTE(png_ptr
, info_ptr
, png_palette
, qdict_size(palette
));
1470 offset
= vs
->tight
.offset
;
1471 if (vs
->clientds
.pf
.bytes_per_pixel
== 4) {
1472 tight_encode_indexed_rect32(vs
->tight
.buffer
, w
* h
, palette
);
1474 tight_encode_indexed_rect16(vs
->tight
.buffer
, w
* h
, palette
);
1478 png_write_info(png_ptr
, info_ptr
);
1480 buffer_reserve(&vs
->tight_png
, 2048);
1481 buf
= qemu_malloc(w
* 3);
1482 for (dy
= 0; dy
< h
; dy
++)
1484 if (color_type
== PNG_COLOR_TYPE_PALETTE
) {
1485 memcpy(buf
, vs
->tight
.buffer
+ (dy
* w
), w
);
1487 rgb_prepare_row(vs
, buf
, x
, y
+ dy
, w
);
1489 png_write_row(png_ptr
, buf
);
1493 png_write_end(png_ptr
, NULL
);
1495 if (color_type
== PNG_COLOR_TYPE_PALETTE
) {
1496 png_free(png_ptr
, png_palette
);
1499 png_destroy_write_struct(&png_ptr
, &info_ptr
);
1501 vnc_write_u8(vs
, VNC_TIGHT_PNG
<< 4);
1503 tight_send_compact_size(vs
, vs
->tight_png
.offset
);
1504 vnc_write(vs
, vs
->tight_png
.buffer
, vs
->tight_png
.offset
);
1505 buffer_reset(&vs
->tight_png
);
1508 #endif /* CONFIG_VNC_PNG */
1510 static void vnc_tight_start(VncState
*vs
)
1512 buffer_reset(&vs
->tight
);
1514 // make the output buffer be the zlib buffer, so we can compress it later
1515 vs
->tight_tmp
= vs
->output
;
1516 vs
->output
= vs
->tight
;
1519 static void vnc_tight_stop(VncState
*vs
)
1521 // switch back to normal output/zlib buffers
1522 vs
->tight
= vs
->output
;
1523 vs
->output
= vs
->tight_tmp
;
1526 static int send_sub_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
1528 struct QDict
*palette
= NULL
;
1529 uint32_t bg
= 0, fg
= 0;
1533 vnc_framebuffer_update(vs
, x
, y
, w
, h
, vs
->tight_type
);
1535 vnc_tight_start(vs
);
1536 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
1539 colors
= tight_fill_palette(vs
, x
, y
, w
* h
, &fg
, &bg
, &palette
);
1542 if (tight_detect_smooth_image(vs
, w
, h
)) {
1543 if (vs
->tight_quality
== -1) {
1544 ret
= send_gradient_rect(vs
, x
, y
, w
, h
);
1546 #ifdef CONFIG_VNC_JPEG
1547 int quality
= tight_conf
[vs
->tight_quality
].jpeg_quality
;
1549 ret
= send_jpeg_rect(vs
, x
, y
, w
, h
, quality
);
1551 ret
= send_full_color_rect(vs
, x
, y
, w
, h
);
1555 ret
= send_full_color_rect(vs
, x
, y
, w
, h
);
1557 } else if (colors
== 1) {
1558 ret
= send_solid_rect(vs
);
1559 } else if (colors
== 2) {
1560 ret
= send_mono_rect(vs
, x
, y
, w
, h
, bg
, fg
);
1561 } else if (colors
<= 256) {
1562 #ifdef CONFIG_VNC_JPEG
1563 if (colors
> 96 && vs
->tight_quality
!= -1 && vs
->tight_quality
<= 3 &&
1564 tight_detect_smooth_image(vs
, w
, h
)) {
1565 int quality
= tight_conf
[vs
->tight_quality
].jpeg_quality
;
1567 ret
= send_jpeg_rect(vs
, x
, y
, w
, h
, quality
);
1569 ret
= send_palette_rect(vs
, x
, y
, w
, h
, palette
);
1572 ret
= send_palette_rect(vs
, x
, y
, w
, h
, 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
)
1592 int max_size
, max_width
;
1593 int max_sub_width
, max_sub_height
;
1598 max_size
= tight_conf
[vs
->tight_compression
].max_rect_size
;
1599 max_width
= tight_conf
[vs
->tight_compression
].max_rect_width
;
1601 if (w
> max_width
|| w
* h
> max_size
) {
1602 max_sub_width
= (w
> max_width
) ? max_width
: w
;
1603 max_sub_height
= max_size
/ max_sub_width
;
1605 for (dy
= 0; dy
< h
; dy
+= max_sub_height
) {
1606 for (dx
= 0; dx
< w
; dx
+= max_width
) {
1607 rw
= MIN(max_sub_width
, w
- dx
);
1608 rh
= MIN(max_sub_height
, h
- dy
);
1609 n
+= send_sub_rect(vs
, x
+dx
, y
+dy
, rw
, rh
);
1613 n
+= send_sub_rect(vs
, x
, y
, w
, h
);
1619 static int find_large_solid_color_rect(VncState
*vs
, int x
, int y
,
1620 int w
, int h
, int max_rows
)
1625 /* Try to find large solid-color areas and send them separately. */
1627 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
1629 /* If a rectangle becomes too large, send its upper part now. */
1631 if (dy
- y
>= max_rows
) {
1632 n
+= send_rect_simple(vs
, x
, y
, w
, max_rows
);
1637 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (y
+ h
- dy
));
1639 for (dx
= x
; dx
< x
+ w
; dx
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
1640 uint32_t color_value
;
1641 int x_best
, y_best
, w_best
, h_best
;
1643 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (x
+ w
- dx
));
1645 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color_value
, false)) {
1649 /* Get dimensions of solid-color area. */
1651 find_best_solid_area(vs
, dx
, dy
, w
- (dx
- x
), h
- (dy
- y
),
1652 color_value
, &w_best
, &h_best
);
1654 /* Make sure a solid rectangle is large enough
1655 (or the whole rectangle is of the same color). */
1657 if (w_best
* h_best
!= w
* h
&&
1658 w_best
* h_best
< VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE
) {
1662 /* Try to extend solid rectangle to maximum size. */
1664 x_best
= dx
; y_best
= dy
;
1665 extend_solid_area(vs
, x
, y
, w
, h
, color_value
,
1666 &x_best
, &y_best
, &w_best
, &h_best
);
1668 /* Send rectangles at top and left to solid-color area. */
1671 n
+= send_rect_simple(vs
, x
, y
, w
, y_best
-y
);
1674 n
+= tight_send_framebuffer_update(vs
, x
, y_best
,
1678 /* Send solid-color rectangle. */
1679 n
+= send_sub_rect_solid(vs
, x_best
, y_best
, w_best
, h_best
);
1681 /* Send remaining rectangles (at right and bottom). */
1683 if (x_best
+ w_best
!= x
+ w
) {
1684 n
+= tight_send_framebuffer_update(vs
, x_best
+w_best
,
1686 w
-(x_best
-x
)-w_best
,
1689 if (y_best
+ h_best
!= y
+ h
) {
1690 n
+= tight_send_framebuffer_update(vs
, x
, y_best
+h_best
,
1691 w
, h
-(y_best
-y
)-h_best
);
1694 /* Return after all recursive calls are done. */
1698 return n
+ send_rect_simple(vs
, x
, y
, w
, h
);
1701 static int tight_send_framebuffer_update(VncState
*vs
, int x
, int y
,
1706 if (vs
->clientds
.pf
.bytes_per_pixel
== 4 && vs
->clientds
.pf
.rmax
== 0xFF &&
1707 vs
->clientds
.pf
.bmax
== 0xFF && vs
->clientds
.pf
.gmax
== 0xFF) {
1708 vs
->tight_pixel24
= true;
1710 vs
->tight_pixel24
= false;
1713 if (w
* h
< VNC_TIGHT_MIN_SPLIT_RECT_SIZE
)
1714 return send_rect_simple(vs
, x
, y
, w
, h
);
1716 /* Calculate maximum number of rows in one non-solid rectangle. */
1718 max_rows
= tight_conf
[vs
->tight_compression
].max_rect_size
;
1719 max_rows
/= MIN(tight_conf
[vs
->tight_compression
].max_rect_width
, w
);
1721 return find_large_solid_color_rect(vs
, x
, y
, w
, h
, max_rows
);
1724 int vnc_tight_send_framebuffer_update(VncState
*vs
, int x
, int y
,
1727 vs
->tight_type
= VNC_ENCODING_TIGHT
;
1728 return tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
1731 int vnc_tight_png_send_framebuffer_update(VncState
*vs
, int x
, int y
,
1734 vs
->tight_type
= VNC_ENCODING_TIGHT_PNG
;
1735 return tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
1738 void vnc_tight_clear(VncState
*vs
)
1741 for (i
=0; i
<ARRAY_SIZE(vs
->tight_stream
); i
++) {
1742 if (vs
->tight_stream
[i
].opaque
) {
1743 deflateEnd(&vs
->tight_stream
[i
]);
1747 buffer_free(&vs
->tight
);
1748 buffer_free(&vs
->tight_zlib
);
1749 buffer_free(&vs
->tight_gradient
);
1750 #ifdef CONFIG_VNC_JPEG
1751 buffer_free(&vs
->tight_jpeg
);