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