]> git.proxmox.com Git - mirror_qemu.git/blame - ui/vnc-enc-tight.c
pixman/vnc: remove rgb_prepare_row* functions
[mirror_qemu.git] / ui / vnc-enc-tight.c
CommitLineData
380282b0
CC
1/*
2 * QEMU VNC display driver: tight encoding
3 *
4 * From libvncserver/libvncserver/tight.c
5 * Copyright (C) 2000, 2001 Const Kaplinsky. All Rights Reserved.
6 * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
7 *
8 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
28
efe556ad 29#include "config-host.h"
2f6f5c7a 30
f26e428d
RT
31/* This needs to be before jpeglib.h line because of conflict with
32 INT32 definitions between jmorecfg.h (included by jpeglib.h) and
33 Win32 basetsd.h (included by windows.h). */
34#include "qemu-common.h"
35
efe556ad 36#ifdef CONFIG_VNC_PNG
2fb0c09f
SW
37/* The following define is needed by pngconf.h. Otherwise it won't compile,
38 because setjmp.h was already included by qemu-common.h. */
39#define PNG_SKIP_SETJMP_CHECK
efe556ad
CC
40#include <png.h>
41#endif
2f6f5c7a
CC
42#ifdef CONFIG_VNC_JPEG
43#include <stdio.h>
44#include <jpeglib.h>
45#endif
46
47#include "bswap.h"
aa7d73fd 48#include "qint.h"
380282b0 49#include "vnc.h"
245f7b51 50#include "vnc-enc-tight.h"
5136a052 51#include "vnc-palette.h"
380282b0
CC
52
53/* Compression level stuff. The following array contains various
54 encoder parameters for each of 10 compression levels (0..9).
55 Last three parameters correspond to JPEG quality levels (0..9). */
56
57static const struct {
58 int max_rect_size, max_rect_width;
59 int mono_min_rect_size, gradient_min_rect_size;
60 int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
61 int gradient_threshold, gradient_threshold24;
62 int idx_max_colors_divisor;
63 int jpeg_quality, jpeg_threshold, jpeg_threshold24;
64} tight_conf[] = {
65 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
66 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
67 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
68 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
69 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
70 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
71 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
72 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
73 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
74 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
75};
76
efe556ad
CC
77
78static int tight_send_framebuffer_update(VncState *vs, int x, int y,
79 int w, int h);
80
ce702e93
CC
81#ifdef CONFIG_VNC_JPEG
82static const struct {
83 double jpeg_freq_min; /* Don't send JPEG if the freq is bellow */
84 double jpeg_freq_threshold; /* Always send JPEG if the freq is above */
85 int jpeg_idx; /* Allow indexed JPEG */
86 int jpeg_full; /* Allow full color JPEG */
87} tight_jpeg_conf[] = {
8cb4a6b7
CC
88 { 0, 8, 1, 1 },
89 { 0, 8, 1, 1 },
90 { 0, 8, 1, 1 },
91 { 0, 8, 1, 1 },
92 { 0, 10, 1, 1 },
93 { 0.1, 10, 1, 1 },
94 { 0.2, 10, 1, 1 },
95 { 0.3, 12, 0, 0 },
96 { 0.4, 14, 0, 0 },
97 { 0.5, 16, 0, 0 },
ce702e93
CC
98};
99#endif
100
efe556ad 101#ifdef CONFIG_VNC_PNG
3941bf6f
CC
102static const struct {
103 int png_zlib_level, png_filters;
104} tight_png_conf[] = {
105 { 0, PNG_NO_FILTERS },
106 { 1, PNG_NO_FILTERS },
107 { 2, PNG_NO_FILTERS },
108 { 3, PNG_NO_FILTERS },
109 { 4, PNG_NO_FILTERS },
110 { 5, PNG_ALL_FILTERS },
111 { 6, PNG_ALL_FILTERS },
112 { 7, PNG_ALL_FILTERS },
113 { 8, PNG_ALL_FILTERS },
114 { 9, PNG_ALL_FILTERS },
115};
116
efe556ad 117static int send_png_rect(VncState *vs, int x, int y, int w, int h,
5136a052 118 VncPalette *palette);
efe556ad
CC
119
120static bool tight_can_send_png_rect(VncState *vs, int w, int h)
121{
d1af0e05 122 if (vs->tight.type != VNC_ENCODING_TIGHT_PNG) {
efe556ad
CC
123 return false;
124 }
125
126 if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
9f64916d 127 vs->client_pf.bytes_per_pixel == 1) {
efe556ad
CC
128 return false;
129 }
130
131 return true;
132}
133#endif
134
2f6f5c7a
CC
135/*
136 * Code to guess if given rectangle is suitable for smooth image
137 * compression (by applying "gradient" filter or JPEG coder).
138 */
139
249cdb42 140static unsigned int
2f6f5c7a
CC
141tight_detect_smooth_image24(VncState *vs, int w, int h)
142{
143 int off;
144 int x, y, d, dx;
249cdb42
BS
145 unsigned int c;
146 unsigned int stats[256];
2f6f5c7a
CC
147 int pixels = 0;
148 int pix, left[3];
249cdb42 149 unsigned int errors;
d1af0e05 150 unsigned char *buf = vs->tight.tight.buffer;
2f6f5c7a
CC
151
152 /*
153 * If client is big-endian, color samples begin from the second
154 * byte (offset 1) of a 32-bit pixel value.
155 */
9f64916d 156 off = vs->client_be;
2f6f5c7a
CC
157
158 memset(stats, 0, sizeof (stats));
159
160 for (y = 0, x = 0; y < h && x < w;) {
161 for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH;
162 d++) {
163 for (c = 0; c < 3; c++) {
164 left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF;
165 }
166 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) {
167 for (c = 0; c < 3; c++) {
168 pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF;
169 stats[abs(pix - left[c])]++;
170 left[c] = pix;
171 }
172 pixels++;
173 }
174 }
175 if (w > h) {
176 x += h;
177 y = 0;
178 } else {
179 x = 0;
180 y += w;
181 }
182 }
183
184 /* 95% smooth or more ... */
185 if (stats[0] * 33 / pixels >= 95) {
186 return 0;
187 }
188
189 errors = 0;
190 for (c = 1; c < 8; c++) {
191 errors += stats[c] * (c * c);
192 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) {
193 return 0;
194 }
195 }
196 for (; c < 256; c++) {
197 errors += stats[c] * (c * c);
198 }
199 errors /= (pixels * 3 - stats[0]);
200
201 return errors;
202}
203
204#define DEFINE_DETECT_FUNCTION(bpp) \
205 \
249cdb42 206 static unsigned int \
2f6f5c7a
CC
207 tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \
208 bool endian; \
209 uint##bpp##_t pix; \
210 int max[3], shift[3]; \
211 int x, y, d, dx; \
249cdb42
BS
212 unsigned int c; \
213 unsigned int stats[256]; \
2f6f5c7a
CC
214 int pixels = 0; \
215 int sample, sum, left[3]; \
249cdb42 216 unsigned int errors; \
d1af0e05 217 unsigned char *buf = vs->tight.tight.buffer; \
2f6f5c7a 218 \
9f64916d
GH
219 endian = 0; /* FIXME: ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
220 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); */ \
2f6f5c7a
CC
221 \
222 \
9f64916d
GH
223 max[0] = vs->client_pf.rmax; \
224 max[1] = vs->client_pf.gmax; \
225 max[2] = vs->client_pf.bmax; \
226 shift[0] = vs->client_pf.rshift; \
227 shift[1] = vs->client_pf.gshift; \
228 shift[2] = vs->client_pf.bshift; \
2f6f5c7a
CC
229 \
230 memset(stats, 0, sizeof(stats)); \
231 \
232 y = 0, x = 0; \
233 while (y < h && x < w) { \
234 for (d = 0; d < h - y && \
235 d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \
236 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \
237 if (endian) { \
ba5e7f82 238 pix = bswap##bpp(pix); \
2f6f5c7a
CC
239 } \
240 for (c = 0; c < 3; c++) { \
241 left[c] = (int)(pix >> shift[c] & max[c]); \
242 } \
243 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \
244 dx++) { \
245 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \
246 if (endian) { \
ba5e7f82 247 pix = bswap##bpp(pix); \
2f6f5c7a
CC
248 } \
249 sum = 0; \
250 for (c = 0; c < 3; c++) { \
251 sample = (int)(pix >> shift[c] & max[c]); \
252 sum += abs(sample - left[c]); \
253 left[c] = sample; \
254 } \
255 if (sum > 255) { \
256 sum = 255; \
257 } \
258 stats[sum]++; \
259 pixels++; \
260 } \
261 } \
262 if (w > h) { \
263 x += h; \
264 y = 0; \
265 } else { \
266 x = 0; \
267 y += w; \
268 } \
269 } \
270 \
271 if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \
272 return 0; \
273 } \
274 \
275 errors = 0; \
276 for (c = 1; c < 8; c++) { \
277 errors += stats[c] * (c * c); \
278 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \
279 return 0; \
280 } \
281 } \
282 for (; c < 256; c++) { \
283 errors += stats[c] * (c * c); \
284 } \
285 errors /= (pixels - stats[0]); \
286 \
287 return errors; \
288 }
289
290DEFINE_DETECT_FUNCTION(16)
291DEFINE_DETECT_FUNCTION(32)
292
293static int
294tight_detect_smooth_image(VncState *vs, int w, int h)
295{
249cdb42 296 unsigned int errors;
d1af0e05
CC
297 int compression = vs->tight.compression;
298 int quality = vs->tight.quality;
2f6f5c7a 299
6f9c78c1
CC
300 if (!vs->vd->lossy) {
301 return 0;
302 }
303
2f6f5c7a 304 if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
9f64916d 305 vs->client_pf.bytes_per_pixel == 1 ||
2f6f5c7a
CC
306 w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) {
307 return 0;
308 }
309
7bccf573 310 if (vs->tight.quality != (uint8_t)-1) {
2f6f5c7a
CC
311 if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) {
312 return 0;
313 }
314 } else {
315 if (w * h < tight_conf[compression].gradient_min_rect_size) {
316 return 0;
317 }
318 }
319
9f64916d 320 if (vs->client_pf.bytes_per_pixel == 4) {
d1af0e05 321 if (vs->tight.pixel24) {
2f6f5c7a 322 errors = tight_detect_smooth_image24(vs, w, h);
7bccf573 323 if (vs->tight.quality != (uint8_t)-1) {
2f6f5c7a
CC
324 return (errors < tight_conf[quality].jpeg_threshold24);
325 }
326 return (errors < tight_conf[compression].gradient_threshold24);
327 } else {
328 errors = tight_detect_smooth_image32(vs, w, h);
329 }
330 } else {
331 errors = tight_detect_smooth_image16(vs, w, h);
332 }
333 if (quality != -1) {
334 return (errors < tight_conf[quality].jpeg_threshold);
335 }
336 return (errors < tight_conf[compression].gradient_threshold);
337}
338
aa7d73fd
CC
339/*
340 * Code to determine how many different colors used in rectangle.
341 */
aa7d73fd
CC
342#define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
343 \
344 static int \
345 tight_fill_palette##bpp(VncState *vs, int x, int y, \
346 int max, size_t count, \
347 uint32_t *bg, uint32_t *fg, \
5136a052 348 VncPalette **palette) { \
aa7d73fd
CC
349 uint##bpp##_t *data; \
350 uint##bpp##_t c0, c1, ci; \
351 int i, n0, n1; \
352 \
d1af0e05 353 data = (uint##bpp##_t *)vs->tight.tight.buffer; \
aa7d73fd
CC
354 \
355 c0 = data[0]; \
356 i = 1; \
357 while (i < count && data[i] == c0) \
358 i++; \
359 if (i >= count) { \
360 *bg = *fg = c0; \
361 return 1; \
362 } \
363 \
364 if (max < 2) { \
365 return 0; \
366 } \
367 \
368 n0 = i; \
369 c1 = data[i]; \
370 n1 = 0; \
371 for (i++; i < count; i++) { \
372 ci = data[i]; \
373 if (ci == c0) { \
374 n0++; \
375 } else if (ci == c1) { \
376 n1++; \
377 } else \
378 break; \
379 } \
380 if (i >= count) { \
381 if (n0 > n1) { \
382 *bg = (uint32_t)c0; \
383 *fg = (uint32_t)c1; \
384 } else { \
385 *bg = (uint32_t)c1; \
386 *fg = (uint32_t)c0; \
387 } \
388 return 2; \
389 } \
390 \
391 if (max == 2) { \
392 return 0; \
393 } \
394 \
5136a052
CC
395 *palette = palette_new(max, bpp); \
396 palette_put(*palette, c0); \
397 palette_put(*palette, c1); \
398 palette_put(*palette, ci); \
aa7d73fd
CC
399 \
400 for (i++; i < count; i++) { \
401 if (data[i] == ci) { \
402 continue; \
403 } else { \
5d8efe39 404 ci = data[i]; \
5136a052 405 if (!palette_put(*palette, (uint32_t)ci)) { \
aa7d73fd
CC
406 return 0; \
407 } \
aa7d73fd
CC
408 } \
409 } \
410 \
5136a052 411 return palette_size(*palette); \
aa7d73fd
CC
412 }
413
414DEFINE_FILL_PALETTE_FUNCTION(8)
415DEFINE_FILL_PALETTE_FUNCTION(16)
416DEFINE_FILL_PALETTE_FUNCTION(32)
417
418static int tight_fill_palette(VncState *vs, int x, int y,
419 size_t count, uint32_t *bg, uint32_t *fg,
5136a052 420 VncPalette **palette)
aa7d73fd
CC
421{
422 int max;
423
d1af0e05 424 max = count / tight_conf[vs->tight.compression].idx_max_colors_divisor;
aa7d73fd 425 if (max < 2 &&
d1af0e05 426 count >= tight_conf[vs->tight.compression].mono_min_rect_size) {
aa7d73fd
CC
427 max = 2;
428 }
429 if (max >= 256) {
430 max = 256;
431 }
432
9f64916d 433 switch (vs->client_pf.bytes_per_pixel) {
aa7d73fd
CC
434 case 4:
435 return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
436 case 2:
437 return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
438 default:
439 max = 2;
440 return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
441 }
442 return 0;
443}
444
aa7d73fd
CC
445/*
446 * Converting truecolor samples into palette indices.
447 */
448#define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
449 \
450 static void \
451 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
5136a052 452 VncPalette *palette) { \
aa7d73fd
CC
453 uint##bpp##_t *src; \
454 uint##bpp##_t rgb; \
54d43eac 455 int i, rep; \
aa7d73fd
CC
456 uint8_t idx; \
457 \
458 src = (uint##bpp##_t *) buf; \
459 \
54d43eac 460 for (i = 0; i < count; i++) { \
efe556ad 461 \
aa7d73fd
CC
462 rgb = *src++; \
463 rep = 0; \
54d43eac
CC
464 while (i < count && *src == rgb) { \
465 rep++, src++, i++; \
aa7d73fd 466 } \
5136a052
CC
467 idx = palette_idx(palette, rgb); \
468 /* \
469 * Should never happen, but don't break everything \
470 * if it does, use the first color instead \
471 */ \
7bccf573 472 if (idx == (uint8_t)-1) { \
aa7d73fd 473 idx = 0; \
aa7d73fd
CC
474 } \
475 while (rep >= 0) { \
476 *buf++ = idx; \
477 rep--; \
478 } \
479 } \
480 }
481
482DEFINE_IDX_ENCODE_FUNCTION(16)
483DEFINE_IDX_ENCODE_FUNCTION(32)
484
485#define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
486 \
487 static void \
488 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
489 uint##bpp##_t bg, uint##bpp##_t fg) { \
490 uint##bpp##_t *ptr; \
491 unsigned int value, mask; \
492 int aligned_width; \
493 int x, y, bg_bits; \
494 \
495 ptr = (uint##bpp##_t *) buf; \
496 aligned_width = w - w % 8; \
497 \
498 for (y = 0; y < h; y++) { \
499 for (x = 0; x < aligned_width; x += 8) { \
500 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
501 if (*ptr++ != bg) { \
502 break; \
503 } \
504 } \
505 if (bg_bits == 8) { \
506 *buf++ = 0; \
507 continue; \
508 } \
509 mask = 0x80 >> bg_bits; \
510 value = mask; \
511 for (bg_bits++; bg_bits < 8; bg_bits++) { \
512 mask >>= 1; \
513 if (*ptr++ != bg) { \
514 value |= mask; \
515 } \
516 } \
517 *buf++ = (uint8_t)value; \
518 } \
519 \
520 mask = 0x80; \
521 value = 0; \
522 if (x >= w) { \
523 continue; \
524 } \
525 \
526 for (; x < w; x++) { \
527 if (*ptr++ != bg) { \
528 value |= mask; \
529 } \
530 mask >>= 1; \
531 } \
532 *buf++ = (uint8_t)value; \
533 } \
534 }
535
536DEFINE_MONO_ENCODE_FUNCTION(8)
537DEFINE_MONO_ENCODE_FUNCTION(16)
538DEFINE_MONO_ENCODE_FUNCTION(32)
539
2f6f5c7a
CC
540/*
541 * ``Gradient'' filter for 24-bit color samples.
542 * Should be called only when redMax, greenMax and blueMax are 255.
543 * Color components assumed to be byte-aligned.
544 */
545
546static void
547tight_filter_gradient24(VncState *vs, uint8_t *buf, int w, int h)
548{
549 uint32_t *buf32;
550 uint32_t pix32;
551 int shift[3];
552 int *prev;
553 int here[3], upper[3], left[3], upperleft[3];
554 int prediction;
555 int x, y, c;
556
557 buf32 = (uint32_t *)buf;
d1af0e05 558 memset(vs->tight.gradient.buffer, 0, w * 3 * sizeof(int));
2f6f5c7a 559
9f64916d
GH
560 if (1 /* FIXME: (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
561 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) */) {
562 shift[0] = vs->client_pf.rshift;
563 shift[1] = vs->client_pf.gshift;
564 shift[2] = vs->client_pf.bshift;
2f6f5c7a 565 } else {
9f64916d
GH
566 shift[0] = 24 - vs->client_pf.rshift;
567 shift[1] = 24 - vs->client_pf.gshift;
568 shift[2] = 24 - vs->client_pf.bshift;
2f6f5c7a
CC
569 }
570
571 for (y = 0; y < h; y++) {
572 for (c = 0; c < 3; c++) {
573 upper[c] = 0;
574 here[c] = 0;
575 }
d1af0e05 576 prev = (int *)vs->tight.gradient.buffer;
2f6f5c7a
CC
577 for (x = 0; x < w; x++) {
578 pix32 = *buf32++;
579 for (c = 0; c < 3; c++) {
580 upperleft[c] = upper[c];
581 left[c] = here[c];
582 upper[c] = *prev;
583 here[c] = (int)(pix32 >> shift[c] & 0xFF);
584 *prev++ = here[c];
585
586 prediction = left[c] + upper[c] - upperleft[c];
587 if (prediction < 0) {
588 prediction = 0;
589 } else if (prediction > 0xFF) {
590 prediction = 0xFF;
591 }
592 *buf++ = (char)(here[c] - prediction);
593 }
594 }
595 }
596}
597
598
599/*
600 * ``Gradient'' filter for other color depths.
601 */
602
603#define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \
604 \
605 static void \
606 tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \
607 int w, int h) { \
608 uint##bpp##_t pix, diff; \
609 bool endian; \
610 int *prev; \
611 int max[3], shift[3]; \
612 int here[3], upper[3], left[3], upperleft[3]; \
613 int prediction; \
614 int x, y, c; \
615 \
d1af0e05 616 memset (vs->tight.gradient.buffer, 0, w * 3 * sizeof(int)); \
2f6f5c7a 617 \
9f64916d
GH
618 endian = 0; /* FIXME: ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
619 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); */ \
2f6f5c7a 620 \
9f64916d
GH
621 max[0] = vs->client_pf.rmax; \
622 max[1] = vs->client_pf.gmax; \
623 max[2] = vs->client_pf.bmax; \
624 shift[0] = vs->client_pf.rshift; \
625 shift[1] = vs->client_pf.gshift; \
626 shift[2] = vs->client_pf.bshift; \
2f6f5c7a
CC
627 \
628 for (y = 0; y < h; y++) { \
629 for (c = 0; c < 3; c++) { \
630 upper[c] = 0; \
631 here[c] = 0; \
632 } \
d1af0e05 633 prev = (int *)vs->tight.gradient.buffer; \
2f6f5c7a
CC
634 for (x = 0; x < w; x++) { \
635 pix = *buf; \
636 if (endian) { \
ba5e7f82 637 pix = bswap##bpp(pix); \
2f6f5c7a
CC
638 } \
639 diff = 0; \
640 for (c = 0; c < 3; c++) { \
641 upperleft[c] = upper[c]; \
642 left[c] = here[c]; \
643 upper[c] = *prev; \
644 here[c] = (int)(pix >> shift[c] & max[c]); \
645 *prev++ = here[c]; \
646 \
647 prediction = left[c] + upper[c] - upperleft[c]; \
648 if (prediction < 0) { \
649 prediction = 0; \
650 } else if (prediction > max[c]) { \
651 prediction = max[c]; \
652 } \
653 diff |= ((here[c] - prediction) & max[c]) \
654 << shift[c]; \
655 } \
656 if (endian) { \
ba5e7f82 657 diff = bswap##bpp(diff); \
2f6f5c7a
CC
658 } \
659 *buf++ = diff; \
660 } \
661 } \
662 }
663
664DEFINE_GRADIENT_FILTER_FUNCTION(16)
665DEFINE_GRADIENT_FILTER_FUNCTION(32)
666
b4bea3f2
CC
667/*
668 * Check if a rectangle is all of the same color. If needSameColor is
669 * set to non-zero, then also check that its color equals to the
b0cd712c 670 * *colorPtr value. The result is 1 if the test is successful, and in
b4bea3f2
CC
671 * that case new color will be stored in *colorPtr.
672 */
673
674#define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
675 \
676 static bool \
677 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
678 uint32_t* color, bool samecolor) \
679 { \
680 VncDisplay *vd = vs->vd; \
681 uint##bpp##_t *fbptr; \
682 uint##bpp##_t c; \
683 int dx, dy; \
684 \
9f64916d 685 fbptr = vnc_server_fb_ptr(vd, x, y); \
b4bea3f2
CC
686 \
687 c = *fbptr; \
688 if (samecolor && (uint32_t)c != *color) { \
689 return false; \
690 } \
691 \
692 for (dy = 0; dy < h; dy++) { \
693 for (dx = 0; dx < w; dx++) { \
694 if (c != fbptr[dx]) { \
695 return false; \
696 } \
697 } \
698 fbptr = (uint##bpp##_t *) \
9f64916d 699 ((uint8_t *)fbptr + vnc_server_fb_stride(vd)); \
b4bea3f2
CC
700 } \
701 \
702 *color = (uint32_t)c; \
703 return true; \
704 }
705
706DEFINE_CHECK_SOLID_FUNCTION(32)
707DEFINE_CHECK_SOLID_FUNCTION(16)
708DEFINE_CHECK_SOLID_FUNCTION(8)
709
710static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
711 uint32_t* color, bool samecolor)
712{
9f64916d 713 switch (VNC_SERVER_FB_BYTES) {
b4bea3f2
CC
714 case 4:
715 return check_solid_tile32(vs, x, y, w, h, color, samecolor);
716 case 2:
717 return check_solid_tile16(vs, x, y, w, h, color, samecolor);
718 default:
719 return check_solid_tile8(vs, x, y, w, h, color, samecolor);
720 }
721}
722
723static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
724 uint32_t color, int *w_ptr, int *h_ptr)
725{
726 int dx, dy, dw, dh;
727 int w_prev;
728 int w_best = 0, h_best = 0;
729
730 w_prev = w;
731
732 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
733
734 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
735 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
736
737 if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
738 break;
739 }
740
741 for (dx = x + dw; dx < x + w_prev;) {
742 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
743
744 if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
745 break;
746 }
747 dx += dw;
748 }
749
750 w_prev = dx - x;
751 if (w_prev * (dy + dh - y) > w_best * h_best) {
752 w_best = w_prev;
753 h_best = dy + dh - y;
754 }
755 }
756
757 *w_ptr = w_best;
758 *h_ptr = h_best;
759}
760
761static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
762 uint32_t color, int *x_ptr, int *y_ptr,
763 int *w_ptr, int *h_ptr)
764{
765 int cx, cy;
766
767 /* Try to extend the area upwards. */
768 for ( cy = *y_ptr - 1;
769 cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
770 cy-- );
771 *h_ptr += *y_ptr - (cy + 1);
772 *y_ptr = cy + 1;
773
774 /* ... downwards. */
775 for ( cy = *y_ptr + *h_ptr;
776 cy < y + h &&
777 check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
778 cy++ );
779 *h_ptr += cy - (*y_ptr + *h_ptr);
780
781 /* ... to the left. */
782 for ( cx = *x_ptr - 1;
783 cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
784 cx-- );
785 *w_ptr += *x_ptr - (cx + 1);
786 *x_ptr = cx + 1;
787
788 /* ... to the right. */
789 for ( cx = *x_ptr + *w_ptr;
790 cx < x + w &&
791 check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
792 cx++ );
793 *w_ptr += cx - (*x_ptr + *w_ptr);
794}
795
380282b0
CC
796static int tight_init_stream(VncState *vs, int stream_id,
797 int level, int strategy)
798{
d1af0e05 799 z_streamp zstream = &vs->tight.stream[stream_id];
380282b0
CC
800
801 if (zstream->opaque == NULL) {
802 int err;
803
804 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
805 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
806 zstream->zalloc = vnc_zlib_zalloc;
807 zstream->zfree = vnc_zlib_zfree;
808
809 err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
810 MAX_MEM_LEVEL, strategy);
811
812 if (err != Z_OK) {
813 fprintf(stderr, "VNC: error initializing zlib\n");
814 return -1;
815 }
816
d1af0e05 817 vs->tight.levels[stream_id] = level;
380282b0
CC
818 zstream->opaque = vs;
819 }
820
d1af0e05 821 if (vs->tight.levels[stream_id] != level) {
380282b0
CC
822 if (deflateParams(zstream, level, strategy) != Z_OK) {
823 return -1;
824 }
d1af0e05 825 vs->tight.levels[stream_id] = level;
380282b0
CC
826 }
827 return 0;
828}
829
830static void tight_send_compact_size(VncState *vs, size_t len)
831{
832 int lpc = 0;
833 int bytes = 0;
834 char buf[3] = {0, 0, 0};
835
836 buf[bytes++] = len & 0x7F;
837 if (len > 0x7F) {
838 buf[bytes-1] |= 0x80;
839 buf[bytes++] = (len >> 7) & 0x7F;
840 if (len > 0x3FFF) {
841 buf[bytes-1] |= 0x80;
842 buf[bytes++] = (len >> 14) & 0xFF;
843 }
844 }
b4bea3f2 845 for (lpc = 0; lpc < bytes; lpc++) {
380282b0
CC
846 vnc_write_u8(vs, buf[lpc]);
847 }
848}
849
850static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
851 int level, int strategy)
852{
d1af0e05 853 z_streamp zstream = &vs->tight.stream[stream_id];
380282b0
CC
854 int previous_out;
855
856 if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
d1af0e05 857 vnc_write(vs, vs->tight.tight.buffer, vs->tight.tight.offset);
380282b0
CC
858 return bytes;
859 }
860
861 if (tight_init_stream(vs, stream_id, level, strategy)) {
862 return -1;
863 }
864
865 /* reserve memory in output buffer */
d1af0e05 866 buffer_reserve(&vs->tight.zlib, bytes + 64);
380282b0
CC
867
868 /* set pointers */
d1af0e05
CC
869 zstream->next_in = vs->tight.tight.buffer;
870 zstream->avail_in = vs->tight.tight.offset;
871 zstream->next_out = vs->tight.zlib.buffer + vs->tight.zlib.offset;
872 zstream->avail_out = vs->tight.zlib.capacity - vs->tight.zlib.offset;
2caa9e9d 873 previous_out = zstream->avail_out;
380282b0 874 zstream->data_type = Z_BINARY;
380282b0
CC
875
876 /* start encoding */
877 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
878 fprintf(stderr, "VNC: error during tight compression\n");
879 return -1;
880 }
881
d1af0e05 882 vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out;
2caa9e9d
MT
883 /* ...how much data has actually been produced by deflate() */
884 bytes = previous_out - zstream->avail_out;
380282b0
CC
885
886 tight_send_compact_size(vs, bytes);
d1af0e05 887 vnc_write(vs, vs->tight.zlib.buffer, bytes);
380282b0 888
d1af0e05 889 buffer_reset(&vs->tight.zlib);
380282b0
CC
890
891 return bytes;
892}
893
894/*
895 * Subencoding implementations.
896 */
aa7d73fd 897static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
380282b0 898{
380282b0
CC
899 uint32_t *buf32;
900 uint32_t pix;
901 int rshift, gshift, bshift;
902
380282b0
CC
903 buf32 = (uint32_t *)buf;
904
9f64916d
GH
905 if (1 /* FIXME: (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
906 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) */) {
907 rshift = vs->client_pf.rshift;
908 gshift = vs->client_pf.gshift;
909 bshift = vs->client_pf.bshift;
380282b0 910 } else {
9f64916d
GH
911 rshift = 24 - vs->client_pf.rshift;
912 gshift = 24 - vs->client_pf.gshift;
913 bshift = 24 - vs->client_pf.bshift;
380282b0
CC
914 }
915
aa7d73fd
CC
916 if (ret) {
917 *ret = count * 3;
918 }
380282b0
CC
919
920 while (count--) {
921 pix = *buf32++;
922 *buf++ = (char)(pix >> rshift);
923 *buf++ = (char)(pix >> gshift);
924 *buf++ = (char)(pix >> bshift);
925 }
926}
927
efe556ad 928static int send_full_color_rect(VncState *vs, int x, int y, int w, int h)
380282b0
CC
929{
930 int stream = 0;
2116eff9 931 ssize_t bytes;
380282b0 932
efe556ad
CC
933#ifdef CONFIG_VNC_PNG
934 if (tight_can_send_png_rect(vs, w, h)) {
935 return send_png_rect(vs, x, y, w, h, NULL);
936 }
937#endif
938
380282b0
CC
939 vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
940
d1af0e05
CC
941 if (vs->tight.pixel24) {
942 tight_pack24(vs, vs->tight.tight.buffer, w * h, &vs->tight.tight.offset);
380282b0
CC
943 bytes = 3;
944 } else {
9f64916d 945 bytes = vs->client_pf.bytes_per_pixel;
380282b0
CC
946 }
947
948 bytes = tight_compress_data(vs, stream, w * h * bytes,
d1af0e05 949 tight_conf[vs->tight.compression].raw_zlib_level,
380282b0
CC
950 Z_DEFAULT_STRATEGY);
951
952 return (bytes >= 0);
953}
954
b4bea3f2
CC
955static int send_solid_rect(VncState *vs)
956{
957 size_t bytes;
958
959 vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
960
d1af0e05
CC
961 if (vs->tight.pixel24) {
962 tight_pack24(vs, vs->tight.tight.buffer, 1, &vs->tight.tight.offset);
b4bea3f2
CC
963 bytes = 3;
964 } else {
9f64916d 965 bytes = vs->client_pf.bytes_per_pixel;
b4bea3f2
CC
966 }
967
d1af0e05 968 vnc_write(vs, vs->tight.tight.buffer, bytes);
b4bea3f2
CC
969 return 1;
970}
971
efe556ad
CC
972static int send_mono_rect(VncState *vs, int x, int y,
973 int w, int h, uint32_t bg, uint32_t fg)
aa7d73fd 974{
2116eff9 975 ssize_t bytes;
aa7d73fd 976 int stream = 1;
d1af0e05 977 int level = tight_conf[vs->tight.compression].mono_zlib_level;
aa7d73fd 978
efe556ad
CC
979#ifdef CONFIG_VNC_PNG
980 if (tight_can_send_png_rect(vs, w, h)) {
981 int ret;
9f64916d 982 int bpp = vs->client_pf.bytes_per_pixel * 8;
5136a052 983 VncPalette *palette = palette_new(2, bpp);
efe556ad 984
5136a052
CC
985 palette_put(palette, bg);
986 palette_put(palette, fg);
efe556ad 987 ret = send_png_rect(vs, x, y, w, h, palette);
5136a052 988 palette_destroy(palette);
efe556ad
CC
989 return ret;
990 }
991#endif
992
aa7d73fd
CC
993 bytes = ((w + 7) / 8) * h;
994
995 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
996 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
997 vnc_write_u8(vs, 1);
998
9f64916d 999 switch (vs->client_pf.bytes_per_pixel) {
aa7d73fd
CC
1000 case 4:
1001 {
1002 uint32_t buf[2] = {bg, fg};
1003 size_t ret = sizeof (buf);
1004
d1af0e05 1005 if (vs->tight.pixel24) {
aa7d73fd
CC
1006 tight_pack24(vs, (unsigned char*)buf, 2, &ret);
1007 }
1008 vnc_write(vs, buf, ret);
1009
d1af0e05 1010 tight_encode_mono_rect32(vs->tight.tight.buffer, w, h, bg, fg);
aa7d73fd
CC
1011 break;
1012 }
1013 case 2:
1014 vnc_write(vs, &bg, 2);
1015 vnc_write(vs, &fg, 2);
d1af0e05 1016 tight_encode_mono_rect16(vs->tight.tight.buffer, w, h, bg, fg);
aa7d73fd
CC
1017 break;
1018 default:
1019 vnc_write_u8(vs, bg);
1020 vnc_write_u8(vs, fg);
d1af0e05 1021 tight_encode_mono_rect8(vs->tight.tight.buffer, w, h, bg, fg);
aa7d73fd
CC
1022 break;
1023 }
d1af0e05 1024 vs->tight.tight.offset = bytes;
aa7d73fd
CC
1025
1026 bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
1027 return (bytes >= 0);
1028}
1029
1030struct palette_cb_priv {
1031 VncState *vs;
1032 uint8_t *header;
efe556ad
CC
1033#ifdef CONFIG_VNC_PNG
1034 png_colorp png_palette;
1035#endif
aa7d73fd
CC
1036};
1037
5136a052 1038static void write_palette(int idx, uint32_t color, void *opaque)
aa7d73fd
CC
1039{
1040 struct palette_cb_priv *priv = opaque;
1041 VncState *vs = priv->vs;
9f64916d 1042 uint32_t bytes = vs->client_pf.bytes_per_pixel;
aa7d73fd
CC
1043
1044 if (bytes == 4) {
aa7d73fd
CC
1045 ((uint32_t*)priv->header)[idx] = color;
1046 } else {
aa7d73fd
CC
1047 ((uint16_t*)priv->header)[idx] = color;
1048 }
1049}
1050
efe556ad 1051static bool send_gradient_rect(VncState *vs, int x, int y, int w, int h)
2f6f5c7a
CC
1052{
1053 int stream = 3;
d1af0e05 1054 int level = tight_conf[vs->tight.compression].gradient_zlib_level;
2116eff9 1055 ssize_t bytes;
2f6f5c7a 1056
9f64916d 1057 if (vs->client_pf.bytes_per_pixel == 1) {
efe556ad 1058 return send_full_color_rect(vs, x, y, w, h);
9f64916d 1059 }
2f6f5c7a
CC
1060
1061 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1062 vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT);
1063
d1af0e05 1064 buffer_reserve(&vs->tight.gradient, w * 3 * sizeof (int));
2f6f5c7a 1065
d1af0e05
CC
1066 if (vs->tight.pixel24) {
1067 tight_filter_gradient24(vs, vs->tight.tight.buffer, w, h);
2f6f5c7a 1068 bytes = 3;
9f64916d 1069 } else if (vs->client_pf.bytes_per_pixel == 4) {
d1af0e05 1070 tight_filter_gradient32(vs, (uint32_t *)vs->tight.tight.buffer, w, h);
2f6f5c7a
CC
1071 bytes = 4;
1072 } else {
d1af0e05 1073 tight_filter_gradient16(vs, (uint16_t *)vs->tight.tight.buffer, w, h);
2f6f5c7a
CC
1074 bytes = 2;
1075 }
1076
d1af0e05 1077 buffer_reset(&vs->tight.gradient);
2f6f5c7a
CC
1078
1079 bytes = w * h * bytes;
d1af0e05 1080 vs->tight.tight.offset = bytes;
2f6f5c7a
CC
1081
1082 bytes = tight_compress_data(vs, stream, bytes,
1083 level, Z_FILTERED);
1084 return (bytes >= 0);
1085}
1086
efe556ad 1087static int send_palette_rect(VncState *vs, int x, int y,
5136a052 1088 int w, int h, VncPalette *palette)
aa7d73fd
CC
1089{
1090 int stream = 2;
d1af0e05 1091 int level = tight_conf[vs->tight.compression].idx_zlib_level;
aa7d73fd 1092 int colors;
2116eff9 1093 ssize_t bytes;
aa7d73fd 1094
efe556ad
CC
1095#ifdef CONFIG_VNC_PNG
1096 if (tight_can_send_png_rect(vs, w, h)) {
1097 return send_png_rect(vs, x, y, w, h, palette);
1098 }
1099#endif
1100
5136a052 1101 colors = palette_size(palette);
aa7d73fd
CC
1102
1103 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1104 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
1105 vnc_write_u8(vs, colors - 1);
1106
9f64916d 1107 switch (vs->client_pf.bytes_per_pixel) {
aa7d73fd
CC
1108 case 4:
1109 {
1110 size_t old_offset, offset;
5136a052 1111 uint32_t header[palette_size(palette)];
aa7d73fd
CC
1112 struct palette_cb_priv priv = { vs, (uint8_t *)header };
1113
1114 old_offset = vs->output.offset;
5136a052 1115 palette_iter(palette, write_palette, &priv);
aa7d73fd
CC
1116 vnc_write(vs, header, sizeof(header));
1117
d1af0e05 1118 if (vs->tight.pixel24) {
aa7d73fd
CC
1119 tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
1120 vs->output.offset = old_offset + offset;
1121 }
1122
d1af0e05 1123 tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
aa7d73fd
CC
1124 break;
1125 }
1126 case 2:
1127 {
5136a052 1128 uint16_t header[palette_size(palette)];
aa7d73fd
CC
1129 struct palette_cb_priv priv = { vs, (uint8_t *)header };
1130
5136a052 1131 palette_iter(palette, write_palette, &priv);
aa7d73fd 1132 vnc_write(vs, header, sizeof(header));
d1af0e05 1133 tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
aa7d73fd
CC
1134 break;
1135 }
1136 default:
1137 return -1; /* No palette for 8bits colors */
1138 break;
1139 }
1140 bytes = w * h;
d1af0e05 1141 vs->tight.tight.offset = bytes;
aa7d73fd
CC
1142
1143 bytes = tight_compress_data(vs, stream, bytes,
1144 level, Z_DEFAULT_STRATEGY);
1145 return (bytes >= 0);
1146}
1147
efe556ad
CC
1148/*
1149 * JPEG compression stuff.
1150 */
1151#ifdef CONFIG_VNC_JPEG
2f6f5c7a
CC
1152/*
1153 * Destination manager implementation for JPEG library.
1154 */
1155
1156/* This is called once per encoding */
1157static void jpeg_init_destination(j_compress_ptr cinfo)
1158{
1159 VncState *vs = cinfo->client_data;
d1af0e05 1160 Buffer *buffer = &vs->tight.jpeg;
2f6f5c7a
CC
1161
1162 cinfo->dest->next_output_byte = (JOCTET *)buffer->buffer + buffer->offset;
1163 cinfo->dest->free_in_buffer = (size_t)(buffer->capacity - buffer->offset);
1164}
1165
1166/* This is called when we ran out of buffer (shouldn't happen!) */
1167static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo)
1168{
1169 VncState *vs = cinfo->client_data;
d1af0e05 1170 Buffer *buffer = &vs->tight.jpeg;
2f6f5c7a
CC
1171
1172 buffer->offset = buffer->capacity;
1173 buffer_reserve(buffer, 2048);
1174 jpeg_init_destination(cinfo);
1175 return TRUE;
1176}
1177
1178/* This is called when we are done processing data */
1179static void jpeg_term_destination(j_compress_ptr cinfo)
1180{
1181 VncState *vs = cinfo->client_data;
d1af0e05 1182 Buffer *buffer = &vs->tight.jpeg;
2f6f5c7a
CC
1183
1184 buffer->offset = buffer->capacity - cinfo->dest->free_in_buffer;
1185}
1186
1187static int send_jpeg_rect(VncState *vs, int x, int y, int w, int h, int quality)
1188{
1189 struct jpeg_compress_struct cinfo;
1190 struct jpeg_error_mgr jerr;
1191 struct jpeg_destination_mgr manager;
47683d66 1192 pixman_image_t *linebuf;
2f6f5c7a
CC
1193 JSAMPROW row[1];
1194 uint8_t *buf;
1195 int dy;
1196
1197 if (ds_get_bytes_per_pixel(vs->ds) == 1)
efe556ad 1198 return send_full_color_rect(vs, x, y, w, h);
2f6f5c7a 1199
d1af0e05 1200 buffer_reserve(&vs->tight.jpeg, 2048);
2f6f5c7a
CC
1201
1202 cinfo.err = jpeg_std_error(&jerr);
1203 jpeg_create_compress(&cinfo);
1204
1205 cinfo.client_data = vs;
1206 cinfo.image_width = w;
1207 cinfo.image_height = h;
1208 cinfo.input_components = 3;
1209 cinfo.in_color_space = JCS_RGB;
1210
1211 jpeg_set_defaults(&cinfo);
1212 jpeg_set_quality(&cinfo, quality, true);
1213
1214 manager.init_destination = jpeg_init_destination;
1215 manager.empty_output_buffer = jpeg_empty_output_buffer;
1216 manager.term_destination = jpeg_term_destination;
1217 cinfo.dest = &manager;
1218
1219 jpeg_start_compress(&cinfo, true);
1220
47683d66
GH
1221 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, w);
1222 buf = (uint8_t *)pixman_image_get_data(linebuf);
d9c18c24 1223 row[0] = buf;
2f6f5c7a 1224 for (dy = 0; dy < h; dy++) {
47683d66 1225 qemu_pixman_linebuf_fill(linebuf, vs->vd->server, w, dy);
2f6f5c7a
CC
1226 jpeg_write_scanlines(&cinfo, row, 1);
1227 }
47683d66 1228 qemu_pixman_image_unref(linebuf);
2f6f5c7a
CC
1229
1230 jpeg_finish_compress(&cinfo);
1231 jpeg_destroy_compress(&cinfo);
1232
1233 vnc_write_u8(vs, VNC_TIGHT_JPEG << 4);
1234
d1af0e05
CC
1235 tight_send_compact_size(vs, vs->tight.jpeg.offset);
1236 vnc_write(vs, vs->tight.jpeg.buffer, vs->tight.jpeg.offset);
1237 buffer_reset(&vs->tight.jpeg);
2f6f5c7a
CC
1238
1239 return 1;
1240}
1241#endif /* CONFIG_VNC_JPEG */
1242
efe556ad
CC
1243/*
1244 * PNG compression stuff.
1245 */
1246#ifdef CONFIG_VNC_PNG
5136a052 1247static void write_png_palette(int idx, uint32_t pix, void *opaque)
efe556ad
CC
1248{
1249 struct palette_cb_priv *priv = opaque;
1250 VncState *vs = priv->vs;
efe556ad 1251 png_colorp color = &priv->png_palette[idx];
efe556ad 1252
d1af0e05 1253 if (vs->tight.pixel24)
efe556ad 1254 {
9f64916d
GH
1255 color->red = (pix >> vs->client_pf.rshift) & vs->client_pf.rmax;
1256 color->green = (pix >> vs->client_pf.gshift) & vs->client_pf.gmax;
1257 color->blue = (pix >> vs->client_pf.bshift) & vs->client_pf.bmax;
efe556ad
CC
1258 }
1259 else
1260 {
1261 int red, green, blue;
1262
9f64916d
GH
1263 red = (pix >> vs->client_pf.rshift) & vs->client_pf.rmax;
1264 green = (pix >> vs->client_pf.gshift) & vs->client_pf.gmax;
1265 blue = (pix >> vs->client_pf.bshift) & vs->client_pf.bmax;
1266 color->red = ((red * 255 + vs->client_pf.rmax / 2) /
1267 vs->client_pf.rmax);
1268 color->green = ((green * 255 + vs->client_pf.gmax / 2) /
1269 vs->client_pf.gmax);
1270 color->blue = ((blue * 255 + vs->client_pf.bmax / 2) /
1271 vs->client_pf.bmax);
efe556ad
CC
1272 }
1273}
1274
1275static void png_write_data(png_structp png_ptr, png_bytep data,
1276 png_size_t length)
1277{
1278 VncState *vs = png_get_io_ptr(png_ptr);
1279
d1af0e05
CC
1280 buffer_reserve(&vs->tight.png, vs->tight.png.offset + length);
1281 memcpy(vs->tight.png.buffer + vs->tight.png.offset, data, length);
efe556ad 1282
d1af0e05 1283 vs->tight.png.offset += length;
efe556ad
CC
1284}
1285
1286static void png_flush_data(png_structp png_ptr)
1287{
1288}
1289
1290static void *vnc_png_malloc(png_structp png_ptr, png_size_t size)
1291{
7267c094 1292 return g_malloc(size);
efe556ad
CC
1293}
1294
1295static void vnc_png_free(png_structp png_ptr, png_voidp ptr)
1296{
7267c094 1297 g_free(ptr);
efe556ad
CC
1298}
1299
1300static int send_png_rect(VncState *vs, int x, int y, int w, int h,
5136a052 1301 VncPalette *palette)
efe556ad
CC
1302{
1303 png_byte color_type;
1304 png_structp png_ptr;
1305 png_infop info_ptr;
1306 png_colorp png_palette = NULL;
47683d66 1307 pixman_image_t *linebuf;
d1af0e05
CC
1308 int level = tight_png_conf[vs->tight.compression].png_zlib_level;
1309 int filters = tight_png_conf[vs->tight.compression].png_filters;
efe556ad
CC
1310 uint8_t *buf;
1311 int dy;
1312
1313 png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL,
1314 NULL, vnc_png_malloc, vnc_png_free);
1315
1316 if (png_ptr == NULL)
1317 return -1;
1318
1319 info_ptr = png_create_info_struct(png_ptr);
1320
1321 if (info_ptr == NULL) {
1322 png_destroy_write_struct(&png_ptr, NULL);
1323 return -1;
1324 }
1325
1326 png_set_write_fn(png_ptr, (void *) vs, png_write_data, png_flush_data);
1327 png_set_compression_level(png_ptr, level);
3941bf6f 1328 png_set_filter(png_ptr, PNG_FILTER_TYPE_DEFAULT, filters);
efe556ad
CC
1329
1330 if (palette) {
1331 color_type = PNG_COLOR_TYPE_PALETTE;
1332 } else {
1333 color_type = PNG_COLOR_TYPE_RGB;
1334 }
1335
1336 png_set_IHDR(png_ptr, info_ptr, w, h,
1337 8, color_type, PNG_INTERLACE_NONE,
1338 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1339
1340 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1341 struct palette_cb_priv priv;
1342
1343 png_palette = png_malloc(png_ptr, sizeof(*png_palette) *
5136a052 1344 palette_size(palette));
efe556ad
CC
1345
1346 priv.vs = vs;
1347 priv.png_palette = png_palette;
5136a052 1348 palette_iter(palette, write_png_palette, &priv);
efe556ad 1349
5136a052 1350 png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size(palette));
efe556ad 1351
9f64916d 1352 if (vs->client_pf.bytes_per_pixel == 4) {
d1af0e05 1353 tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
efe556ad 1354 } else {
d1af0e05 1355 tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
efe556ad
CC
1356 }
1357 }
1358
1359 png_write_info(png_ptr, info_ptr);
1360
d1af0e05 1361 buffer_reserve(&vs->tight.png, 2048);
47683d66
GH
1362 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, w);
1363 buf = (uint8_t *)pixman_image_get_data(linebuf);
efe556ad
CC
1364 for (dy = 0; dy < h; dy++)
1365 {
1366 if (color_type == PNG_COLOR_TYPE_PALETTE) {
d1af0e05 1367 memcpy(buf, vs->tight.tight.buffer + (dy * w), w);
efe556ad 1368 } else {
47683d66 1369 qemu_pixman_linebuf_fill(linebuf, vs->vd->server, w, dy);
efe556ad
CC
1370 }
1371 png_write_row(png_ptr, buf);
1372 }
47683d66 1373 qemu_pixman_image_unref(linebuf);
efe556ad
CC
1374
1375 png_write_end(png_ptr, NULL);
1376
1377 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1378 png_free(png_ptr, png_palette);
1379 }
1380
1381 png_destroy_write_struct(&png_ptr, &info_ptr);
1382
1383 vnc_write_u8(vs, VNC_TIGHT_PNG << 4);
1384
d1af0e05
CC
1385 tight_send_compact_size(vs, vs->tight.png.offset);
1386 vnc_write(vs, vs->tight.png.buffer, vs->tight.png.offset);
1387 buffer_reset(&vs->tight.png);
efe556ad
CC
1388 return 1;
1389}
1390#endif /* CONFIG_VNC_PNG */
1391
380282b0
CC
1392static void vnc_tight_start(VncState *vs)
1393{
d1af0e05 1394 buffer_reset(&vs->tight.tight);
380282b0
CC
1395
1396 // make the output buffer be the zlib buffer, so we can compress it later
d1af0e05
CC
1397 vs->tight.tmp = vs->output;
1398 vs->output = vs->tight.tight;
380282b0
CC
1399}
1400
1401static void vnc_tight_stop(VncState *vs)
1402{
1403 // switch back to normal output/zlib buffers
d1af0e05
CC
1404 vs->tight.tight = vs->output;
1405 vs->output = vs->tight.tmp;
380282b0
CC
1406}
1407
03817eb8
CC
1408static int send_sub_rect_nojpeg(VncState *vs, int x, int y, int w, int h,
1409 int bg, int fg, int colors, VncPalette *palette)
380282b0 1410{
03817eb8 1411 int ret;
380282b0 1412
03817eb8
CC
1413 if (colors == 0) {
1414 if (tight_detect_smooth_image(vs, w, h)) {
1415 ret = send_gradient_rect(vs, x, y, w, h);
1416 } else {
1417 ret = send_full_color_rect(vs, x, y, w, h);
1418 }
1419 } else if (colors == 1) {
1420 ret = send_solid_rect(vs);
1421 } else if (colors == 2) {
1422 ret = send_mono_rect(vs, x, y, w, h, bg, fg);
1423 } else if (colors <= 256) {
1424 ret = send_palette_rect(vs, x, y, w, h, palette);
d167f9bc
BS
1425 } else {
1426 ret = 0;
03817eb8
CC
1427 }
1428 return ret;
1429}
380282b0 1430
03817eb8
CC
1431#ifdef CONFIG_VNC_JPEG
1432static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h,
1433 int bg, int fg, int colors,
ce702e93 1434 VncPalette *palette, bool force)
03817eb8
CC
1435{
1436 int ret;
aa7d73fd
CC
1437
1438 if (colors == 0) {
ce702e93
CC
1439 if (force || (tight_jpeg_conf[vs->tight.quality].jpeg_full &&
1440 tight_detect_smooth_image(vs, w, h))) {
03817eb8 1441 int quality = tight_conf[vs->tight.quality].jpeg_quality;
2f6f5c7a 1442
03817eb8 1443 ret = send_jpeg_rect(vs, x, y, w, h, quality);
2f6f5c7a 1444 } else {
efe556ad 1445 ret = send_full_color_rect(vs, x, y, w, h);
2f6f5c7a 1446 }
aa7d73fd
CC
1447 } else if (colors == 1) {
1448 ret = send_solid_rect(vs);
1449 } else if (colors == 2) {
efe556ad 1450 ret = send_mono_rect(vs, x, y, w, h, bg, fg);
aa7d73fd 1451 } else if (colors <= 256) {
ce702e93
CC
1452 if (force || (colors > 96 &&
1453 tight_jpeg_conf[vs->tight.quality].jpeg_idx &&
1454 tight_detect_smooth_image(vs, w, h))) {
d1af0e05 1455 int quality = tight_conf[vs->tight.quality].jpeg_quality;
2f6f5c7a
CC
1456
1457 ret = send_jpeg_rect(vs, x, y, w, h, quality);
1458 } else {
efe556ad 1459 ret = send_palette_rect(vs, x, y, w, h, palette);
2f6f5c7a 1460 }
ad7ee4ad
BS
1461 } else {
1462 ret = 0;
03817eb8
CC
1463 }
1464 return ret;
1465}
2f6f5c7a 1466#endif
03817eb8
CC
1467
1468static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
1469{
1470 VncPalette *palette = NULL;
1471 uint32_t bg = 0, fg = 0;
1472 int colors;
1473 int ret = 0;
cf76a1ce 1474#ifdef CONFIG_VNC_JPEG
ce702e93
CC
1475 bool force_jpeg = false;
1476 bool allow_jpeg = true;
cf76a1ce 1477#endif
03817eb8
CC
1478
1479 vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
1480
1481 vnc_tight_start(vs);
1482 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1483 vnc_tight_stop(vs);
1484
ce702e93 1485#ifdef CONFIG_VNC_JPEG
80e0c8c3 1486 if (!vs->vd->non_adaptive && vs->tight.quality != (uint8_t)-1) {
ce702e93
CC
1487 double freq = vnc_update_freq(vs, x, y, w, h);
1488
1489 if (freq < tight_jpeg_conf[vs->tight.quality].jpeg_freq_min) {
1490 allow_jpeg = false;
1491 }
1492 if (freq >= tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
1493 force_jpeg = true;
1494 vnc_sent_lossy_rect(vs, x, y, w, h);
1495 }
1496 }
1497#endif
1498
03817eb8
CC
1499 colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
1500
1501#ifdef CONFIG_VNC_JPEG
ce702e93
CC
1502 if (allow_jpeg && vs->tight.quality != (uint8_t)-1) {
1503 ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, palette,
1504 force_jpeg);
03817eb8
CC
1505 } else {
1506 ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
aa7d73fd 1507 }
03817eb8
CC
1508#else
1509 ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
1510#endif
1511
5136a052 1512 palette_destroy(palette);
aa7d73fd 1513 return ret;
380282b0
CC
1514}
1515
b4bea3f2
CC
1516static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
1517{
d1af0e05 1518 vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
b4bea3f2
CC
1519
1520 vnc_tight_start(vs);
1521 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1522 vnc_tight_stop(vs);
1523
1524 return send_solid_rect(vs);
1525}
1526
ce702e93
CC
1527static int send_rect_simple(VncState *vs, int x, int y, int w, int h,
1528 bool split)
380282b0
CC
1529{
1530 int max_size, max_width;
1531 int max_sub_width, max_sub_height;
1532 int dx, dy;
1533 int rw, rh;
1534 int n = 0;
1535
d1af0e05
CC
1536 max_size = tight_conf[vs->tight.compression].max_rect_size;
1537 max_width = tight_conf[vs->tight.compression].max_rect_width;
380282b0 1538
ce702e93 1539 if (split && (w > max_width || w * h > max_size)) {
380282b0
CC
1540 max_sub_width = (w > max_width) ? max_width : w;
1541 max_sub_height = max_size / max_sub_width;
1542
1543 for (dy = 0; dy < h; dy += max_sub_height) {
1544 for (dx = 0; dx < w; dx += max_width) {
1545 rw = MIN(max_sub_width, w - dx);
1546 rh = MIN(max_sub_height, h - dy);
1547 n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
1548 }
1549 }
1550 } else {
1551 n += send_sub_rect(vs, x, y, w, h);
1552 }
1553
1554 return n;
1555}
1556
b4bea3f2
CC
1557static int find_large_solid_color_rect(VncState *vs, int x, int y,
1558 int w, int h, int max_rows)
1559{
1560 int dx, dy, dw, dh;
1561 int n = 0;
1562
1563 /* Try to find large solid-color areas and send them separately. */
1564
1565 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1566
1567 /* If a rectangle becomes too large, send its upper part now. */
1568
1569 if (dy - y >= max_rows) {
ce702e93 1570 n += send_rect_simple(vs, x, y, w, max_rows, true);
b4bea3f2
CC
1571 y += max_rows;
1572 h -= max_rows;
1573 }
1574
1575 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));
1576
1577 for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1578 uint32_t color_value;
1579 int x_best, y_best, w_best, h_best;
1580
1581 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));
1582
1583 if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
1584 continue ;
1585 }
1586
1587 /* Get dimensions of solid-color area. */
1588
1589 find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
1590 color_value, &w_best, &h_best);
1591
1592 /* Make sure a solid rectangle is large enough
1593 (or the whole rectangle is of the same color). */
1594
1595 if (w_best * h_best != w * h &&
1596 w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
1597 continue;
1598 }
1599
1600 /* Try to extend solid rectangle to maximum size. */
1601
1602 x_best = dx; y_best = dy;
1603 extend_solid_area(vs, x, y, w, h, color_value,
1604 &x_best, &y_best, &w_best, &h_best);
1605
1606 /* Send rectangles at top and left to solid-color area. */
1607
1608 if (y_best != y) {
ce702e93 1609 n += send_rect_simple(vs, x, y, w, y_best-y, true);
b4bea3f2
CC
1610 }
1611 if (x_best != x) {
efe556ad
CC
1612 n += tight_send_framebuffer_update(vs, x, y_best,
1613 x_best-x, h_best);
b4bea3f2
CC
1614 }
1615
1616 /* Send solid-color rectangle. */
1617 n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);
1618
1619 /* Send remaining rectangles (at right and bottom). */
1620
1621 if (x_best + w_best != x + w) {
efe556ad
CC
1622 n += tight_send_framebuffer_update(vs, x_best+w_best,
1623 y_best,
1624 w-(x_best-x)-w_best,
1625 h_best);
b4bea3f2
CC
1626 }
1627 if (y_best + h_best != y + h) {
efe556ad
CC
1628 n += tight_send_framebuffer_update(vs, x, y_best+h_best,
1629 w, h-(y_best-y)-h_best);
b4bea3f2
CC
1630 }
1631
1632 /* Return after all recursive calls are done. */
1633 return n;
1634 }
1635 }
ce702e93 1636 return n + send_rect_simple(vs, x, y, w, h, true);
b4bea3f2
CC
1637}
1638
efe556ad
CC
1639static int tight_send_framebuffer_update(VncState *vs, int x, int y,
1640 int w, int h)
380282b0 1641{
b4bea3f2
CC
1642 int max_rows;
1643
9f64916d
GH
1644 if (vs->client_pf.bytes_per_pixel == 4 && vs->client_pf.rmax == 0xFF &&
1645 vs->client_pf.bmax == 0xFF && vs->client_pf.gmax == 0xFF) {
d1af0e05 1646 vs->tight.pixel24 = true;
380282b0 1647 } else {
d1af0e05 1648 vs->tight.pixel24 = false;
380282b0
CC
1649 }
1650
cf76a1ce 1651#ifdef CONFIG_VNC_JPEG
368d2588 1652 if (vs->tight.quality != (uint8_t)-1) {
ce702e93
CC
1653 double freq = vnc_update_freq(vs, x, y, w, h);
1654
1655 if (freq > tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
1656 return send_rect_simple(vs, x, y, w, h, false);
1657 }
1658 }
cf76a1ce 1659#endif
ce702e93
CC
1660
1661 if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE) {
1662 return send_rect_simple(vs, x, y, w, h, true);
1663 }
b4bea3f2
CC
1664
1665 /* Calculate maximum number of rows in one non-solid rectangle. */
1666
d1af0e05
CC
1667 max_rows = tight_conf[vs->tight.compression].max_rect_size;
1668 max_rows /= MIN(tight_conf[vs->tight.compression].max_rect_width, w);
b4bea3f2
CC
1669
1670 return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
380282b0
CC
1671}
1672
efe556ad
CC
1673int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
1674 int w, int h)
1675{
d1af0e05 1676 vs->tight.type = VNC_ENCODING_TIGHT;
efe556ad
CC
1677 return tight_send_framebuffer_update(vs, x, y, w, h);
1678}
1679
1680int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y,
1681 int w, int h)
1682{
d1af0e05 1683 vs->tight.type = VNC_ENCODING_TIGHT_PNG;
efe556ad
CC
1684 return tight_send_framebuffer_update(vs, x, y, w, h);
1685}
1686
380282b0
CC
1687void vnc_tight_clear(VncState *vs)
1688{
1689 int i;
d1af0e05
CC
1690 for (i=0; i<ARRAY_SIZE(vs->tight.stream); i++) {
1691 if (vs->tight.stream[i].opaque) {
1692 deflateEnd(&vs->tight.stream[i]);
380282b0
CC
1693 }
1694 }
1695
d1af0e05
CC
1696 buffer_free(&vs->tight.tight);
1697 buffer_free(&vs->tight.zlib);
1698 buffer_free(&vs->tight.gradient);
2f6f5c7a 1699#ifdef CONFIG_VNC_JPEG
d1af0e05 1700 buffer_free(&vs->tight.jpeg);
2f6f5c7a 1701#endif
b5469b11
CC
1702#ifdef CONFIG_VNC_PNG
1703 buffer_free(&vs->tight.png);
1704#endif
380282b0 1705}