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