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 "qemu-common.h"
31 #ifdef CONFIG_VNC_JPEG
40 #include "vnc-encoding-tight.h"
42 /* Compression level stuff. The following array contains various
43 encoder parameters for each of 10 compression levels (0..9).
44 Last three parameters correspond to JPEG quality levels (0..9). */
47 int max_rect_size
, max_rect_width
;
48 int mono_min_rect_size
, gradient_min_rect_size
;
49 int idx_zlib_level
, mono_zlib_level
, raw_zlib_level
, gradient_zlib_level
;
50 int gradient_threshold
, gradient_threshold24
;
51 int idx_max_colors_divisor
;
52 int jpeg_quality
, jpeg_threshold
, jpeg_threshold24
;
54 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
55 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
56 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
57 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
58 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
59 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
60 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
61 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
62 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
63 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
67 * Code to guess if given rectangle is suitable for smooth image
68 * compression (by applying "gradient" filter or JPEG coder).
72 tight_detect_smooth_image24(VncState
*vs
, int w
, int h
)
81 unsigned char *buf
= vs
->tight
.buffer
;
84 * If client is big-endian, color samples begin from the second
85 * byte (offset 1) of a 32-bit pixel value.
87 off
= !!(vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
);
89 memset(stats
, 0, sizeof (stats
));
91 for (y
= 0, x
= 0; y
< h
&& x
< w
;) {
92 for (d
= 0; d
< h
- y
&& d
< w
- x
- VNC_TIGHT_DETECT_SUBROW_WIDTH
;
94 for (c
= 0; c
< 3; c
++) {
95 left
[c
] = buf
[((y
+d
)*w
+x
+d
)*4+off
+c
] & 0xFF;
97 for (dx
= 1; dx
<= VNC_TIGHT_DETECT_SUBROW_WIDTH
; dx
++) {
98 for (c
= 0; c
< 3; c
++) {
99 pix
= buf
[((y
+d
)*w
+x
+d
+dx
)*4+off
+c
] & 0xFF;
100 stats
[abs(pix
- left
[c
])]++;
115 /* 95% smooth or more ... */
116 if (stats
[0] * 33 / pixels
>= 95) {
121 for (c
= 1; c
< 8; c
++) {
122 errors
+= stats
[c
] * (c
* c
);
123 if (stats
[c
] == 0 || stats
[c
] > stats
[c
-1] * 2) {
127 for (; c
< 256; c
++) {
128 errors
+= stats
[c
] * (c
* c
);
130 errors
/= (pixels
* 3 - stats
[0]);
135 #define DEFINE_DETECT_FUNCTION(bpp) \
138 tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \
141 int max[3], shift[3]; \
146 int sample, sum, left[3]; \
148 unsigned char *buf = vs->tight.buffer; \
150 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
151 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
154 max[0] = vs->clientds.pf.rmax; \
155 max[1] = vs->clientds.pf.gmax; \
156 max[2] = vs->clientds.pf.bmax; \
157 shift[0] = vs->clientds.pf.rshift; \
158 shift[1] = vs->clientds.pf.gshift; \
159 shift[2] = vs->clientds.pf.bshift; \
161 memset(stats, 0, sizeof(stats)); \
164 while (y < h && x < w) { \
165 for (d = 0; d < h - y && \
166 d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \
167 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \
169 pix = bswap_##bpp(pix); \
171 for (c = 0; c < 3; c++) { \
172 left[c] = (int)(pix >> shift[c] & max[c]); \
174 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \
176 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \
178 pix = bswap_##bpp(pix); \
181 for (c = 0; c < 3; c++) { \
182 sample = (int)(pix >> shift[c] & max[c]); \
183 sum += abs(sample - left[c]); \
202 if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \
207 for (c = 1; c < 8; c++) { \
208 errors += stats[c] * (c * c); \
209 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \
213 for (; c < 256; c++) { \
214 errors += stats[c] * (c * c); \
216 errors /= (pixels - stats[0]); \
221 DEFINE_DETECT_FUNCTION(16)
222 DEFINE_DETECT_FUNCTION(32)
225 tight_detect_smooth_image(VncState
*vs
, int w
, int h
)
228 int compression
= vs
->tight_compression
;
229 int quality
= vs
->tight_quality
;
231 if (ds_get_bytes_per_pixel(vs
->ds
) == 1 ||
232 vs
->clientds
.pf
.bytes_per_pixel
== 1 ||
233 w
< VNC_TIGHT_DETECT_MIN_WIDTH
|| h
< VNC_TIGHT_DETECT_MIN_HEIGHT
) {
237 if (vs
->tight_quality
!= -1) {
238 if (w
* h
< VNC_TIGHT_JPEG_MIN_RECT_SIZE
) {
242 if (w
* h
< tight_conf
[compression
].gradient_min_rect_size
) {
247 if (vs
->clientds
.pf
.bytes_per_pixel
== 4) {
248 if (vs
->tight_pixel24
) {
249 errors
= tight_detect_smooth_image24(vs
, w
, h
);
250 if (vs
->tight_quality
!= -1) {
251 return (errors
< tight_conf
[quality
].jpeg_threshold24
);
253 return (errors
< tight_conf
[compression
].gradient_threshold24
);
255 errors
= tight_detect_smooth_image32(vs
, w
, h
);
258 errors
= tight_detect_smooth_image16(vs
, w
, h
);
261 return (errors
< tight_conf
[quality
].jpeg_threshold
);
263 return (errors
< tight_conf
[compression
].gradient_threshold
);
267 * Code to determine how many different colors used in rectangle.
270 static void tight_palette_rgb2buf(uint32_t rgb
, int bpp
, uint8_t buf
[6])
275 buf
[0] = ((rgb
>> 24) & 0xFF);
276 buf
[1] = ((rgb
>> 16) & 0xFF);
277 buf
[2] = ((rgb
>> 8) & 0xFF);
278 buf
[3] = ((rgb
>> 0) & 0xFF);
279 buf
[4] = ((buf
[0] & 1) == 0) << 3 | ((buf
[1] & 1) == 0) << 2;
280 buf
[4]|= ((buf
[2] & 1) == 0) << 1 | ((buf
[3] & 1) == 0) << 0;
287 buf
[0] = ((rgb
>> 8) & 0xFF);
288 buf
[1] = ((rgb
>> 0) & 0xFF);
289 buf
[2] = ((buf
[0] & 1) == 0) << 1 | ((buf
[1] & 1) == 0) << 0;
295 static uint32_t tight_palette_buf2rgb(int bpp
, const uint8_t *buf
)
300 rgb
|= ((buf
[0] & ~1) | !((buf
[4] >> 3) & 1)) << 24;
301 rgb
|= ((buf
[1] & ~1) | !((buf
[4] >> 2) & 1)) << 16;
302 rgb
|= ((buf
[2] & ~1) | !((buf
[4] >> 1) & 1)) << 8;
303 rgb
|= ((buf
[3] & ~1) | !((buf
[4] >> 0) & 1)) << 0;
306 rgb
|= ((buf
[0] & ~1) | !((buf
[2] >> 1) & 1)) << 8;
307 rgb
|= ((buf
[1] & ~1) | !((buf
[2] >> 0) & 1)) << 0;
313 static int tight_palette_insert(QDict
*palette
, uint32_t rgb
, int bpp
, int max
)
316 int idx
= qdict_size(palette
);
319 tight_palette_rgb2buf(rgb
, bpp
, key
);
320 present
= qdict_haskey(palette
, (char *)key
);
321 if (idx
>= max
&& !present
) {
325 qdict_put(palette
, (char *)key
, qint_from_int(idx
));
327 return qdict_size(palette
);
330 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
333 tight_fill_palette##bpp(VncState *vs, int x, int y, \
334 int max, size_t count, \
335 uint32_t *bg, uint32_t *fg, \
336 struct QDict **palette) { \
337 uint##bpp##_t *data; \
338 uint##bpp##_t c0, c1, ci; \
341 data = (uint##bpp##_t *)vs->tight.buffer; \
345 while (i < count && data[i] == c0) \
359 for (i++; i < count; i++) { \
363 } else if (ci == c1) { \
370 *bg = (uint32_t)c0; \
371 *fg = (uint32_t)c1; \
373 *bg = (uint32_t)c1; \
374 *fg = (uint32_t)c0; \
383 *palette = qdict_new(); \
384 tight_palette_insert(*palette, c0, bpp, max); \
385 tight_palette_insert(*palette, c1, bpp, max); \
386 tight_palette_insert(*palette, ci, bpp, max); \
388 for (i++; i < count; i++) { \
389 if (data[i] == ci) { \
392 if (!tight_palette_insert(*palette, (uint32_t)ci, \
400 return qdict_size(*palette); \
403 DEFINE_FILL_PALETTE_FUNCTION(8)
404 DEFINE_FILL_PALETTE_FUNCTION(16)
405 DEFINE_FILL_PALETTE_FUNCTION(32)
407 static int tight_fill_palette(VncState
*vs
, int x
, int y
,
408 size_t count
, uint32_t *bg
, uint32_t *fg
,
409 struct QDict
**palette
)
413 max
= count
/ tight_conf
[vs
->tight_compression
].idx_max_colors_divisor
;
415 count
>= tight_conf
[vs
->tight_compression
].mono_min_rect_size
) {
422 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
424 return tight_fill_palette32(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
426 return tight_fill_palette16(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
429 return tight_fill_palette8(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
434 /* Callback to dump a palette with qdict_iter
435 static void print_palette(const char *key, QObject *obj, void *opaque)
437 uint8_t idx = qint_get_int(qobject_to_qint(obj));
438 uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
440 fprintf(stderr, "%.2x ", (unsigned char)*key);
442 fprintf(stderr, "%.2x ", (unsigned char)*key);
444 fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
449 * Converting truecolor samples into palette indices.
451 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
454 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
455 struct QDict *palette) { \
456 uint##bpp##_t *src; \
462 src = (uint##bpp##_t *) buf; \
464 for (i = 0; i < count; i++) { \
467 while (i < count && *src == rgb) { \
470 tight_palette_rgb2buf(rgb, bpp, key); \
471 if (!qdict_haskey(palette, (char *)key)) { \
473 * Should never happen, but don't break everything \
474 * if it does, use the first color instead \
478 idx = qdict_get_int(palette, (char *)key); \
487 DEFINE_IDX_ENCODE_FUNCTION(16)
488 DEFINE_IDX_ENCODE_FUNCTION(32)
490 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
493 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
494 uint##bpp##_t bg, uint##bpp##_t fg) { \
495 uint##bpp##_t *ptr; \
496 unsigned int value, mask; \
500 ptr = (uint##bpp##_t *) buf; \
501 aligned_width = w - w % 8; \
503 for (y = 0; y < h; y++) { \
504 for (x = 0; x < aligned_width; x += 8) { \
505 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
506 if (*ptr++ != bg) { \
510 if (bg_bits == 8) { \
514 mask = 0x80 >> bg_bits; \
516 for (bg_bits++; bg_bits < 8; bg_bits++) { \
518 if (*ptr++ != bg) { \
522 *buf++ = (uint8_t)value; \
531 for (; x < w; x++) { \
532 if (*ptr++ != bg) { \
537 *buf++ = (uint8_t)value; \
541 DEFINE_MONO_ENCODE_FUNCTION(8)
542 DEFINE_MONO_ENCODE_FUNCTION(16)
543 DEFINE_MONO_ENCODE_FUNCTION(32)
546 * ``Gradient'' filter for 24-bit color samples.
547 * Should be called only when redMax, greenMax and blueMax are 255.
548 * Color components assumed to be byte-aligned.
552 tight_filter_gradient24(VncState
*vs
, uint8_t *buf
, int w
, int h
)
558 int here
[3], upper
[3], left
[3], upperleft
[3];
562 buf32
= (uint32_t *)buf
;
563 memset(vs
->tight_gradient
.buffer
, 0, w
* 3 * sizeof(int));
565 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
566 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
)) {
567 shift
[0] = vs
->clientds
.pf
.rshift
;
568 shift
[1] = vs
->clientds
.pf
.gshift
;
569 shift
[2] = vs
->clientds
.pf
.bshift
;
571 shift
[0] = 24 - vs
->clientds
.pf
.rshift
;
572 shift
[1] = 24 - vs
->clientds
.pf
.gshift
;
573 shift
[2] = 24 - vs
->clientds
.pf
.bshift
;
576 for (y
= 0; y
< h
; y
++) {
577 for (c
= 0; c
< 3; c
++) {
581 prev
= (int *)vs
->tight_gradient
.buffer
;
582 for (x
= 0; x
< w
; x
++) {
584 for (c
= 0; c
< 3; c
++) {
585 upperleft
[c
] = upper
[c
];
588 here
[c
] = (int)(pix32
>> shift
[c
] & 0xFF);
591 prediction
= left
[c
] + upper
[c
] - upperleft
[c
];
592 if (prediction
< 0) {
594 } else if (prediction
> 0xFF) {
597 *buf
++ = (char)(here
[c
] - prediction
);
605 * ``Gradient'' filter for other color depths.
608 #define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \
611 tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \
613 uint##bpp##_t pix, diff; \
616 int max[3], shift[3]; \
617 int here[3], upper[3], left[3], upperleft[3]; \
621 memset (vs->tight_gradient.buffer, 0, w * 3 * sizeof(int)); \
623 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
624 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
626 max[0] = vs->clientds.pf.rmax; \
627 max[1] = vs->clientds.pf.gmax; \
628 max[2] = vs->clientds.pf.bmax; \
629 shift[0] = vs->clientds.pf.rshift; \
630 shift[1] = vs->clientds.pf.gshift; \
631 shift[2] = vs->clientds.pf.bshift; \
633 for (y = 0; y < h; y++) { \
634 for (c = 0; c < 3; c++) { \
638 prev = (int *)vs->tight_gradient.buffer; \
639 for (x = 0; x < w; x++) { \
642 pix = bswap_##bpp(pix); \
645 for (c = 0; c < 3; c++) { \
646 upperleft[c] = upper[c]; \
649 here[c] = (int)(pix >> shift[c] & max[c]); \
652 prediction = left[c] + upper[c] - upperleft[c]; \
653 if (prediction < 0) { \
655 } else if (prediction > max[c]) { \
656 prediction = max[c]; \
658 diff |= ((here[c] - prediction) & max[c]) \
662 diff = bswap_##bpp(diff); \
669 DEFINE_GRADIENT_FILTER_FUNCTION(16)
670 DEFINE_GRADIENT_FILTER_FUNCTION(32)
673 * Check if a rectangle is all of the same color. If needSameColor is
674 * set to non-zero, then also check that its color equals to the
675 * *colorPtr value. The result is 1 if the test is successfull, and in
676 * that case new color will be stored in *colorPtr.
679 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
682 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
683 uint32_t* color, bool samecolor) \
685 VncDisplay *vd = vs->vd; \
686 uint##bpp##_t *fbptr; \
690 fbptr = (uint##bpp##_t *) \
691 (vd->server->data + y * ds_get_linesize(vs->ds) + \
692 x * ds_get_bytes_per_pixel(vs->ds)); \
695 if (samecolor && (uint32_t)c != *color) { \
699 for (dy = 0; dy < h; dy++) { \
700 for (dx = 0; dx < w; dx++) { \
701 if (c != fbptr[dx]) { \
705 fbptr = (uint##bpp##_t *) \
706 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
709 *color = (uint32_t)c; \
713 DEFINE_CHECK_SOLID_FUNCTION(32)
714 DEFINE_CHECK_SOLID_FUNCTION(16)
715 DEFINE_CHECK_SOLID_FUNCTION(8)
717 static bool check_solid_tile(VncState
*vs
, int x
, int y
, int w
, int h
,
718 uint32_t* color
, bool samecolor
)
720 VncDisplay
*vd
= vs
->vd
;
722 switch(vd
->server
->pf
.bytes_per_pixel
) {
724 return check_solid_tile32(vs
, x
, y
, w
, h
, color
, samecolor
);
726 return check_solid_tile16(vs
, x
, y
, w
, h
, color
, samecolor
);
728 return check_solid_tile8(vs
, x
, y
, w
, h
, color
, samecolor
);
732 static void find_best_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
733 uint32_t color
, int *w_ptr
, int *h_ptr
)
737 int w_best
= 0, h_best
= 0;
741 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
743 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, y
+ h
- dy
);
744 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, w_prev
);
746 if (!check_solid_tile(vs
, x
, dy
, dw
, dh
, &color
, true)) {
750 for (dx
= x
+ dw
; dx
< x
+ w_prev
;) {
751 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, x
+ w_prev
- dx
);
753 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color
, true)) {
760 if (w_prev
* (dy
+ dh
- y
) > w_best
* h_best
) {
762 h_best
= dy
+ dh
- y
;
770 static void extend_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
771 uint32_t color
, int *x_ptr
, int *y_ptr
,
772 int *w_ptr
, int *h_ptr
)
776 /* Try to extend the area upwards. */
777 for ( cy
= *y_ptr
- 1;
778 cy
>= y
&& check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
780 *h_ptr
+= *y_ptr
- (cy
+ 1);
784 for ( cy
= *y_ptr
+ *h_ptr
;
786 check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
788 *h_ptr
+= cy
- (*y_ptr
+ *h_ptr
);
790 /* ... to the left. */
791 for ( cx
= *x_ptr
- 1;
792 cx
>= x
&& check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
794 *w_ptr
+= *x_ptr
- (cx
+ 1);
797 /* ... to the right. */
798 for ( cx
= *x_ptr
+ *w_ptr
;
800 check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
802 *w_ptr
+= cx
- (*x_ptr
+ *w_ptr
);
805 static int tight_init_stream(VncState
*vs
, int stream_id
,
806 int level
, int strategy
)
808 z_streamp zstream
= &vs
->tight_stream
[stream_id
];
810 if (zstream
->opaque
== NULL
) {
813 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id
);
814 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream
->opaque
, vs
);
815 zstream
->zalloc
= vnc_zlib_zalloc
;
816 zstream
->zfree
= vnc_zlib_zfree
;
818 err
= deflateInit2(zstream
, level
, Z_DEFLATED
, MAX_WBITS
,
819 MAX_MEM_LEVEL
, strategy
);
822 fprintf(stderr
, "VNC: error initializing zlib\n");
826 vs
->tight_levels
[stream_id
] = level
;
827 zstream
->opaque
= vs
;
830 if (vs
->tight_levels
[stream_id
] != level
) {
831 if (deflateParams(zstream
, level
, strategy
) != Z_OK
) {
834 vs
->tight_levels
[stream_id
] = level
;
839 static void tight_send_compact_size(VncState
*vs
, size_t len
)
843 char buf
[3] = {0, 0, 0};
845 buf
[bytes
++] = len
& 0x7F;
847 buf
[bytes
-1] |= 0x80;
848 buf
[bytes
++] = (len
>> 7) & 0x7F;
850 buf
[bytes
-1] |= 0x80;
851 buf
[bytes
++] = (len
>> 14) & 0xFF;
854 for (lpc
= 0; lpc
< bytes
; lpc
++) {
855 vnc_write_u8(vs
, buf
[lpc
]);
859 static int tight_compress_data(VncState
*vs
, int stream_id
, size_t bytes
,
860 int level
, int strategy
)
862 z_streamp zstream
= &vs
->tight_stream
[stream_id
];
865 if (bytes
< VNC_TIGHT_MIN_TO_COMPRESS
) {
866 vnc_write(vs
, vs
->tight
.buffer
, vs
->tight
.offset
);
870 if (tight_init_stream(vs
, stream_id
, level
, strategy
)) {
874 /* reserve memory in output buffer */
875 buffer_reserve(&vs
->tight_zlib
, bytes
+ 64);
878 zstream
->next_in
= vs
->tight
.buffer
;
879 zstream
->avail_in
= vs
->tight
.offset
;
880 zstream
->next_out
= vs
->tight_zlib
.buffer
+ vs
->tight_zlib
.offset
;
881 zstream
->avail_out
= vs
->tight_zlib
.capacity
- vs
->tight_zlib
.offset
;
882 zstream
->data_type
= Z_BINARY
;
883 previous_out
= zstream
->total_out
;
886 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
887 fprintf(stderr
, "VNC: error during tight compression\n");
891 vs
->tight_zlib
.offset
= vs
->tight_zlib
.capacity
- zstream
->avail_out
;
892 bytes
= zstream
->total_out
- previous_out
;
894 tight_send_compact_size(vs
, bytes
);
895 vnc_write(vs
, vs
->tight_zlib
.buffer
, bytes
);
897 buffer_reset(&vs
->tight_zlib
);
903 * Subencoding implementations.
905 static void tight_pack24(VncState
*vs
, uint8_t *buf
, size_t count
, size_t *ret
)
909 int rshift
, gshift
, bshift
;
911 buf32
= (uint32_t *)buf
;
913 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
914 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
)) {
915 rshift
= vs
->clientds
.pf
.rshift
;
916 gshift
= vs
->clientds
.pf
.gshift
;
917 bshift
= vs
->clientds
.pf
.bshift
;
919 rshift
= 24 - vs
->clientds
.pf
.rshift
;
920 gshift
= 24 - vs
->clientds
.pf
.gshift
;
921 bshift
= 24 - vs
->clientds
.pf
.bshift
;
930 *buf
++ = (char)(pix
>> rshift
);
931 *buf
++ = (char)(pix
>> gshift
);
932 *buf
++ = (char)(pix
>> bshift
);
936 static int send_full_color_rect(VncState
*vs
, int w
, int h
)
941 vnc_write_u8(vs
, stream
<< 4); /* no flushing, no filter */
943 if (vs
->tight_pixel24
) {
944 tight_pack24(vs
, vs
->tight
.buffer
, w
* h
, &vs
->tight
.offset
);
947 bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
950 bytes
= tight_compress_data(vs
, stream
, w
* h
* bytes
,
951 tight_conf
[vs
->tight_compression
].raw_zlib_level
,
957 static int send_solid_rect(VncState
*vs
)
961 vnc_write_u8(vs
, VNC_TIGHT_FILL
<< 4); /* no flushing, no filter */
963 if (vs
->tight_pixel24
) {
964 tight_pack24(vs
, vs
->tight
.buffer
, 1, &vs
->tight
.offset
);
967 bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
970 vnc_write(vs
, vs
->tight
.buffer
, bytes
);
974 static int send_mono_rect(VncState
*vs
, int w
, int h
, uint32_t bg
, uint32_t fg
)
978 int level
= tight_conf
[vs
->tight_compression
].mono_zlib_level
;
980 bytes
= ((w
+ 7) / 8) * h
;
982 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
983 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
986 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
989 uint32_t buf
[2] = {bg
, fg
};
990 size_t ret
= sizeof (buf
);
992 if (vs
->tight_pixel24
) {
993 tight_pack24(vs
, (unsigned char*)buf
, 2, &ret
);
995 vnc_write(vs
, buf
, ret
);
997 tight_encode_mono_rect32(vs
->tight
.buffer
, w
, h
, bg
, fg
);
1001 vnc_write(vs
, &bg
, 2);
1002 vnc_write(vs
, &fg
, 2);
1003 tight_encode_mono_rect16(vs
->tight
.buffer
, w
, h
, bg
, fg
);
1006 vnc_write_u8(vs
, bg
);
1007 vnc_write_u8(vs
, fg
);
1008 tight_encode_mono_rect8(vs
->tight
.buffer
, w
, h
, bg
, fg
);
1011 vs
->tight
.offset
= bytes
;
1013 bytes
= tight_compress_data(vs
, stream
, bytes
, level
, Z_DEFAULT_STRATEGY
);
1014 return (bytes
>= 0);
1017 struct palette_cb_priv
{
1022 static void write_palette(const char *key
, QObject
*obj
, void *opaque
)
1024 struct palette_cb_priv
*priv
= opaque
;
1025 VncState
*vs
= priv
->vs
;
1026 uint32_t bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
1027 uint8_t idx
= qint_get_int(qobject_to_qint(obj
));
1030 uint32_t color
= tight_palette_buf2rgb(32, (uint8_t *)key
);
1032 ((uint32_t*)priv
->header
)[idx
] = color
;
1034 uint16_t color
= tight_palette_buf2rgb(16, (uint8_t *)key
);
1036 ((uint16_t*)priv
->header
)[idx
] = color
;
1040 static bool send_gradient_rect(VncState
*vs
, int w
, int h
)
1043 int level
= tight_conf
[vs
->tight_compression
].gradient_zlib_level
;
1046 if (vs
->clientds
.pf
.bytes_per_pixel
== 1)
1047 return send_full_color_rect(vs
, w
, h
);
1049 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
1050 vnc_write_u8(vs
, VNC_TIGHT_FILTER_GRADIENT
);
1052 buffer_reserve(&vs
->tight_gradient
, w
* 3 * sizeof (int));
1054 if (vs
->tight_pixel24
) {
1055 tight_filter_gradient24(vs
, vs
->tight
.buffer
, w
, h
);
1057 } else if (vs
->clientds
.pf
.bytes_per_pixel
== 4) {
1058 tight_filter_gradient32(vs
, (uint32_t *)vs
->tight
.buffer
, w
, h
);
1061 tight_filter_gradient16(vs
, (uint16_t *)vs
->tight
.buffer
, w
, h
);
1065 buffer_reset(&vs
->tight_gradient
);
1067 bytes
= w
* h
* bytes
;
1068 vs
->tight
.offset
= bytes
;
1070 bytes
= tight_compress_data(vs
, stream
, bytes
,
1072 return (bytes
>= 0);
1075 static int send_palette_rect(VncState
*vs
, int w
, int h
, struct QDict
*palette
)
1078 int level
= tight_conf
[vs
->tight_compression
].idx_zlib_level
;
1082 colors
= qdict_size(palette
);
1084 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
1085 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
1086 vnc_write_u8(vs
, colors
- 1);
1088 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
1091 size_t old_offset
, offset
;
1092 uint32_t header
[qdict_size(palette
)];
1093 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
1095 old_offset
= vs
->output
.offset
;
1096 qdict_iter(palette
, write_palette
, &priv
);
1097 vnc_write(vs
, header
, sizeof(header
));
1099 if (vs
->tight_pixel24
) {
1100 tight_pack24(vs
, vs
->output
.buffer
+ old_offset
, colors
, &offset
);
1101 vs
->output
.offset
= old_offset
+ offset
;
1104 tight_encode_indexed_rect32(vs
->tight
.buffer
, w
* h
, palette
);
1109 uint16_t header
[qdict_size(palette
)];
1110 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
1112 qdict_iter(palette
, write_palette
, &priv
);
1113 vnc_write(vs
, header
, sizeof(header
));
1114 tight_encode_indexed_rect16(vs
->tight
.buffer
, w
* h
, palette
);
1118 return -1; /* No palette for 8bits colors */
1122 vs
->tight
.offset
= bytes
;
1124 bytes
= tight_compress_data(vs
, stream
, bytes
,
1125 level
, Z_DEFAULT_STRATEGY
);
1126 return (bytes
>= 0);
1130 * JPEG compression stuff.
1132 #ifdef CONFIG_VNC_JPEG
1133 static void jpeg_prepare_row24(VncState
*vs
, uint8_t *dst
, int x
, int y
,
1136 VncDisplay
*vd
= vs
->vd
;
1140 fbptr
= (uint32_t *)(vd
->server
->data
+ y
* ds_get_linesize(vs
->ds
) +
1141 x
* ds_get_bytes_per_pixel(vs
->ds
));
1145 *dst
++ = (uint8_t)(pix
>> vs
->ds
->surface
->pf
.rshift
);
1146 *dst
++ = (uint8_t)(pix
>> vs
->ds
->surface
->pf
.gshift
);
1147 *dst
++ = (uint8_t)(pix
>> vs
->ds
->surface
->pf
.bshift
);
1151 #define DEFINE_JPEG_GET_ROW_FUNCTION(bpp) \
1154 jpeg_prepare_row##bpp(VncState *vs, uint8_t *dst, \
1155 int x, int y, int count) \
1157 VncDisplay *vd = vs->vd; \
1158 uint##bpp##_t *fbptr; \
1159 uint##bpp##_t pix; \
1162 fbptr = (uint##bpp##_t *) \
1163 (vd->server->data + y * ds_get_linesize(vs->ds) + \
1164 x * ds_get_bytes_per_pixel(vs->ds)); \
1169 r = (int)((pix >> vs->ds->surface->pf.rshift) \
1170 & vs->ds->surface->pf.rmax); \
1171 g = (int)((pix >> vs->ds->surface->pf.gshift) \
1172 & vs->ds->surface->pf.gmax); \
1173 b = (int)((pix >> vs->ds->surface->pf.bshift) \
1174 & vs->ds->surface->pf.bmax); \
1176 *dst++ = (uint8_t)((r * 255 + vs->ds->surface->pf.rmax / 2) \
1177 / vs->ds->surface->pf.rmax); \
1178 *dst++ = (uint8_t)((g * 255 + vs->ds->surface->pf.gmax / 2) \
1179 / vs->ds->surface->pf.gmax); \
1180 *dst++ = (uint8_t)((b * 255 + vs->ds->surface->pf.bmax / 2) \
1181 / vs->ds->surface->pf.bmax); \
1185 DEFINE_JPEG_GET_ROW_FUNCTION(16)
1186 DEFINE_JPEG_GET_ROW_FUNCTION(32)
1188 static void jpeg_prepare_row(VncState
*vs
, uint8_t *dst
, int x
, int y
,
1191 if (vs
->tight_pixel24
)
1192 jpeg_prepare_row24(vs
, dst
, x
, y
, count
);
1193 else if (ds_get_bytes_per_pixel(vs
->ds
) == 4)
1194 jpeg_prepare_row32(vs
, dst
, x
, y
, count
);
1196 jpeg_prepare_row16(vs
, dst
, x
, y
, count
);
1200 * Destination manager implementation for JPEG library.
1203 /* This is called once per encoding */
1204 static void jpeg_init_destination(j_compress_ptr cinfo
)
1206 VncState
*vs
= cinfo
->client_data
;
1207 Buffer
*buffer
= &vs
->tight_jpeg
;
1209 cinfo
->dest
->next_output_byte
= (JOCTET
*)buffer
->buffer
+ buffer
->offset
;
1210 cinfo
->dest
->free_in_buffer
= (size_t)(buffer
->capacity
- buffer
->offset
);
1213 /* This is called when we ran out of buffer (shouldn't happen!) */
1214 static boolean
jpeg_empty_output_buffer(j_compress_ptr cinfo
)
1216 VncState
*vs
= cinfo
->client_data
;
1217 Buffer
*buffer
= &vs
->tight_jpeg
;
1219 buffer
->offset
= buffer
->capacity
;
1220 buffer_reserve(buffer
, 2048);
1221 jpeg_init_destination(cinfo
);
1225 /* This is called when we are done processing data */
1226 static void jpeg_term_destination(j_compress_ptr cinfo
)
1228 VncState
*vs
= cinfo
->client_data
;
1229 Buffer
*buffer
= &vs
->tight_jpeg
;
1231 buffer
->offset
= buffer
->capacity
- cinfo
->dest
->free_in_buffer
;
1234 static int send_jpeg_rect(VncState
*vs
, int x
, int y
, int w
, int h
, int quality
)
1236 struct jpeg_compress_struct cinfo
;
1237 struct jpeg_error_mgr jerr
;
1238 struct jpeg_destination_mgr manager
;
1243 if (ds_get_bytes_per_pixel(vs
->ds
) == 1)
1244 return send_full_color_rect(vs
, w
, h
);
1246 buf
= qemu_malloc(w
* 3);
1248 buffer_reserve(&vs
->tight_jpeg
, 2048);
1250 cinfo
.err
= jpeg_std_error(&jerr
);
1251 jpeg_create_compress(&cinfo
);
1253 cinfo
.client_data
= vs
;
1254 cinfo
.image_width
= w
;
1255 cinfo
.image_height
= h
;
1256 cinfo
.input_components
= 3;
1257 cinfo
.in_color_space
= JCS_RGB
;
1259 jpeg_set_defaults(&cinfo
);
1260 jpeg_set_quality(&cinfo
, quality
, true);
1262 manager
.init_destination
= jpeg_init_destination
;
1263 manager
.empty_output_buffer
= jpeg_empty_output_buffer
;
1264 manager
.term_destination
= jpeg_term_destination
;
1265 cinfo
.dest
= &manager
;
1267 jpeg_start_compress(&cinfo
, true);
1269 for (dy
= 0; dy
< h
; dy
++) {
1270 jpeg_prepare_row(vs
, buf
, x
, y
+ dy
, w
);
1271 jpeg_write_scanlines(&cinfo
, row
, 1);
1274 jpeg_finish_compress(&cinfo
);
1275 jpeg_destroy_compress(&cinfo
);
1277 vnc_write_u8(vs
, VNC_TIGHT_JPEG
<< 4);
1279 tight_send_compact_size(vs
, vs
->tight_jpeg
.offset
);
1280 vnc_write(vs
, vs
->tight_jpeg
.buffer
, vs
->tight_jpeg
.offset
);
1281 buffer_reset(&vs
->tight_jpeg
);
1285 #endif /* CONFIG_VNC_JPEG */
1287 static void vnc_tight_start(VncState
*vs
)
1289 buffer_reset(&vs
->tight
);
1291 // make the output buffer be the zlib buffer, so we can compress it later
1292 vs
->tight_tmp
= vs
->output
;
1293 vs
->output
= vs
->tight
;
1296 static void vnc_tight_stop(VncState
*vs
)
1298 // switch back to normal output/zlib buffers
1299 vs
->tight
= vs
->output
;
1300 vs
->output
= vs
->tight_tmp
;
1303 static int send_sub_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
1305 struct QDict
*palette
= NULL
;
1306 uint32_t bg
= 0, fg
= 0;
1310 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_TIGHT
);
1312 vnc_tight_start(vs
);
1313 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
1316 colors
= tight_fill_palette(vs
, x
, y
, w
* h
, &fg
, &bg
, &palette
);
1319 if (tight_detect_smooth_image(vs
, w
, h
)) {
1320 if (vs
->tight_quality
== -1) {
1321 ret
= send_gradient_rect(vs
, w
, h
);
1323 #ifdef CONFIG_VNC_JPEG
1324 int quality
= tight_conf
[vs
->tight_quality
].jpeg_quality
;
1326 ret
= send_jpeg_rect(vs
, x
, y
, w
, h
, quality
);
1328 ret
= send_full_color_rect(vs
, w
, h
);
1332 ret
= send_full_color_rect(vs
, w
, h
);
1334 } else if (colors
== 1) {
1335 ret
= send_solid_rect(vs
);
1336 } else if (colors
== 2) {
1337 ret
= send_mono_rect(vs
, w
, h
, bg
, fg
);
1338 } else if (colors
<= 256) {
1339 #ifdef CONFIG_VNC_JPEG
1340 if (colors
> 96 && vs
->tight_quality
!= -1 && vs
->tight_quality
<= 3 &&
1341 tight_detect_smooth_image(vs
, w
, h
)) {
1342 int quality
= tight_conf
[vs
->tight_quality
].jpeg_quality
;
1344 ret
= send_jpeg_rect(vs
, x
, y
, w
, h
, quality
);
1346 ret
= send_palette_rect(vs
, w
, h
, palette
);
1349 ret
= send_palette_rect(vs
, w
, h
, palette
);
1356 static int send_sub_rect_solid(VncState
*vs
, int x
, int y
, int w
, int h
)
1358 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_TIGHT
);
1360 vnc_tight_start(vs
);
1361 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
1364 return send_solid_rect(vs
);
1367 static int send_rect_simple(VncState
*vs
, int x
, int y
, int w
, int h
)
1369 int max_size
, max_width
;
1370 int max_sub_width
, max_sub_height
;
1375 max_size
= tight_conf
[vs
->tight_compression
].max_rect_size
;
1376 max_width
= tight_conf
[vs
->tight_compression
].max_rect_width
;
1378 if (w
> max_width
|| w
* h
> max_size
) {
1379 max_sub_width
= (w
> max_width
) ? max_width
: w
;
1380 max_sub_height
= max_size
/ max_sub_width
;
1382 for (dy
= 0; dy
< h
; dy
+= max_sub_height
) {
1383 for (dx
= 0; dx
< w
; dx
+= max_width
) {
1384 rw
= MIN(max_sub_width
, w
- dx
);
1385 rh
= MIN(max_sub_height
, h
- dy
);
1386 n
+= send_sub_rect(vs
, x
+dx
, y
+dy
, rw
, rh
);
1390 n
+= send_sub_rect(vs
, x
, y
, w
, h
);
1396 static int find_large_solid_color_rect(VncState
*vs
, int x
, int y
,
1397 int w
, int h
, int max_rows
)
1402 /* Try to find large solid-color areas and send them separately. */
1404 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
1406 /* If a rectangle becomes too large, send its upper part now. */
1408 if (dy
- y
>= max_rows
) {
1409 n
+= send_rect_simple(vs
, x
, y
, w
, max_rows
);
1414 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (y
+ h
- dy
));
1416 for (dx
= x
; dx
< x
+ w
; dx
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
1417 uint32_t color_value
;
1418 int x_best
, y_best
, w_best
, h_best
;
1420 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (x
+ w
- dx
));
1422 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color_value
, false)) {
1426 /* Get dimensions of solid-color area. */
1428 find_best_solid_area(vs
, dx
, dy
, w
- (dx
- x
), h
- (dy
- y
),
1429 color_value
, &w_best
, &h_best
);
1431 /* Make sure a solid rectangle is large enough
1432 (or the whole rectangle is of the same color). */
1434 if (w_best
* h_best
!= w
* h
&&
1435 w_best
* h_best
< VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE
) {
1439 /* Try to extend solid rectangle to maximum size. */
1441 x_best
= dx
; y_best
= dy
;
1442 extend_solid_area(vs
, x
, y
, w
, h
, color_value
,
1443 &x_best
, &y_best
, &w_best
, &h_best
);
1445 /* Send rectangles at top and left to solid-color area. */
1448 n
+= send_rect_simple(vs
, x
, y
, w
, y_best
-y
);
1451 n
+= vnc_tight_send_framebuffer_update(vs
, x
, y_best
,
1455 /* Send solid-color rectangle. */
1456 n
+= send_sub_rect_solid(vs
, x_best
, y_best
, w_best
, h_best
);
1458 /* Send remaining rectangles (at right and bottom). */
1460 if (x_best
+ w_best
!= x
+ w
) {
1461 n
+= vnc_tight_send_framebuffer_update(vs
, x_best
+w_best
,
1463 w
-(x_best
-x
)-w_best
,
1466 if (y_best
+ h_best
!= y
+ h
) {
1467 n
+= vnc_tight_send_framebuffer_update(vs
, x
, y_best
+h_best
,
1468 w
, h
-(y_best
-y
)-h_best
);
1471 /* Return after all recursive calls are done. */
1475 return n
+ send_rect_simple(vs
, x
, y
, w
, h
);
1478 int vnc_tight_send_framebuffer_update(VncState
*vs
, int x
, int y
,
1483 if (vs
->clientds
.pf
.bytes_per_pixel
== 4 && vs
->clientds
.pf
.rmax
== 0xFF &&
1484 vs
->clientds
.pf
.bmax
== 0xFF && vs
->clientds
.pf
.gmax
== 0xFF) {
1485 vs
->tight_pixel24
= true;
1487 vs
->tight_pixel24
= false;
1490 if (w
* h
< VNC_TIGHT_MIN_SPLIT_RECT_SIZE
)
1491 return send_rect_simple(vs
, x
, y
, w
, h
);
1493 /* Calculate maximum number of rows in one non-solid rectangle. */
1495 max_rows
= tight_conf
[vs
->tight_compression
].max_rect_size
;
1496 max_rows
/= MIN(tight_conf
[vs
->tight_compression
].max_rect_width
, w
);
1498 return find_large_solid_color_rect(vs
, x
, y
, w
, h
, max_rows
);
1501 void vnc_tight_clear(VncState
*vs
)
1504 for (i
=0; i
<ARRAY_SIZE(vs
->tight_stream
); i
++) {
1505 if (vs
->tight_stream
[i
].opaque
) {
1506 deflateEnd(&vs
->tight_stream
[i
]);
1510 buffer_free(&vs
->tight
);
1511 buffer_free(&vs
->tight_zlib
);
1512 buffer_free(&vs
->tight_gradient
);
1513 #ifdef CONFIG_VNC_JPEG
1514 buffer_free(&vs
->tight_jpeg
);