root / vnc-encoding-tight.c @ 2f6f5c7a
History | View | Annotate | Download (58.9 kB)
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 |
|
29 |
#include "qemu-common.h" |
30 |
|
31 |
#ifdef CONFIG_VNC_JPEG
|
32 |
#include <stdio.h> |
33 |
#include <jpeglib.h> |
34 |
#endif
|
35 |
|
36 |
#include "bswap.h" |
37 |
#include "qdict.h" |
38 |
#include "qint.h" |
39 |
#include "vnc.h" |
40 |
#include "vnc-encoding-tight.h" |
41 |
|
42 |
/* Compression level stuff. The following array contains various
|
43 |
encoder parameters for each of 10 compression levels (0..9).
|
44 |
Last three parameters correspond to JPEG quality levels (0..9). */
|
45 |
|
46 |
static const struct { |
47 |
int max_rect_size, max_rect_width;
|
48 |
int mono_min_rect_size, gradient_min_rect_size;
|
49 |
int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
|
50 |
int gradient_threshold, gradient_threshold24;
|
51 |
int idx_max_colors_divisor;
|
52 |
int jpeg_quality, jpeg_threshold, jpeg_threshold24;
|
53 |
} tight_conf[] = { |
54 |
{ 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 }, |
55 |
{ 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 }, |
56 |
{ 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 }, |
57 |
{ 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 }, |
58 |
{ 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 }, |
59 |
{ 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 }, |
60 |
{ 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 }, |
61 |
{ 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 }, |
62 |
{ 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 }, |
63 |
{ 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 } |
64 |
}; |
65 |
|
66 |
/*
|
67 |
* Code to guess if given rectangle is suitable for smooth image
|
68 |
* compression (by applying "gradient" filter or JPEG coder).
|
69 |
*/
|
70 |
|
71 |
static uint
|
72 |
tight_detect_smooth_image24(VncState *vs, int w, int h) |
73 |
{ |
74 |
int off;
|
75 |
int x, y, d, dx;
|
76 |
uint c; |
77 |
uint stats[256];
|
78 |
int pixels = 0; |
79 |
int pix, left[3]; |
80 |
uint errors; |
81 |
unsigned char *buf = vs->tight.buffer; |
82 |
|
83 |
/*
|
84 |
* If client is big-endian, color samples begin from the second
|
85 |
* byte (offset 1) of a 32-bit pixel value.
|
86 |
*/
|
87 |
off = !!(vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG); |
88 |
|
89 |
memset(stats, 0, sizeof (stats)); |
90 |
|
91 |
for (y = 0, x = 0; y < h && x < w;) { |
92 |
for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; |
93 |
d++) { |
94 |
for (c = 0; c < 3; c++) { |
95 |
left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF; |
96 |
} |
97 |
for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) { |
98 |
for (c = 0; c < 3; c++) { |
99 |
pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF; |
100 |
stats[abs(pix - left[c])]++; |
101 |
left[c] = pix; |
102 |
} |
103 |
pixels++; |
104 |
} |
105 |
} |
106 |
if (w > h) {
|
107 |
x += h; |
108 |
y = 0;
|
109 |
} else {
|
110 |
x = 0;
|
111 |
y += w; |
112 |
} |
113 |
} |
114 |
|
115 |
/* 95% smooth or more ... */
|
116 |
if (stats[0] * 33 / pixels >= 95) { |
117 |
return 0; |
118 |
} |
119 |
|
120 |
errors = 0;
|
121 |
for (c = 1; c < 8; c++) { |
122 |
errors += stats[c] * (c * c); |
123 |
if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { |
124 |
return 0; |
125 |
} |
126 |
} |
127 |
for (; c < 256; c++) { |
128 |
errors += stats[c] * (c * c); |
129 |
} |
130 |
errors /= (pixels * 3 - stats[0]); |
131 |
|
132 |
return errors;
|
133 |
} |
134 |
|
135 |
#define DEFINE_DETECT_FUNCTION(bpp) \
|
136 |
\ |
137 |
static uint \
|
138 |
tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \ |
139 |
bool endian; \
|
140 |
uint##bpp##_t pix; \ |
141 |
int max[3], shift[3]; \ |
142 |
int x, y, d, dx; \
|
143 |
uint c; \ |
144 |
uint stats[256]; \
|
145 |
int pixels = 0; \ |
146 |
int sample, sum, left[3]; \ |
147 |
uint errors; \ |
148 |
unsigned char *buf = vs->tight.buffer; \ |
149 |
\ |
150 |
endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \ |
151 |
(vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \ |
152 |
\ |
153 |
\ |
154 |
max[0] = vs->clientds.pf.rmax; \
|
155 |
max[1] = vs->clientds.pf.gmax; \
|
156 |
max[2] = vs->clientds.pf.bmax; \
|
157 |
shift[0] = vs->clientds.pf.rshift; \
|
158 |
shift[1] = vs->clientds.pf.gshift; \
|
159 |
shift[2] = vs->clientds.pf.bshift; \
|
160 |
\ |
161 |
memset(stats, 0, sizeof(stats)); \ |
162 |
\ |
163 |
y = 0, x = 0; \ |
164 |
while (y < h && x < w) { \
|
165 |
for (d = 0; d < h - y && \ |
166 |
d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \ |
167 |
pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \ |
168 |
if (endian) { \
|
169 |
pix = bswap_##bpp(pix); \ |
170 |
} \ |
171 |
for (c = 0; c < 3; c++) { \ |
172 |
left[c] = (int)(pix >> shift[c] & max[c]); \
|
173 |
} \ |
174 |
for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \ |
175 |
dx++) { \ |
176 |
pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \ |
177 |
if (endian) { \
|
178 |
pix = bswap_##bpp(pix); \ |
179 |
} \ |
180 |
sum = 0; \
|
181 |
for (c = 0; c < 3; c++) { \ |
182 |
sample = (int)(pix >> shift[c] & max[c]); \
|
183 |
sum += abs(sample - left[c]); \ |
184 |
left[c] = sample; \ |
185 |
} \ |
186 |
if (sum > 255) { \ |
187 |
sum = 255; \
|
188 |
} \ |
189 |
stats[sum]++; \ |
190 |
pixels++; \ |
191 |
} \ |
192 |
} \ |
193 |
if (w > h) { \
|
194 |
x += h; \ |
195 |
y = 0; \
|
196 |
} else { \
|
197 |
x = 0; \
|
198 |
y += w; \ |
199 |
} \ |
200 |
} \ |
201 |
\ |
202 |
if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \ |
203 |
return 0; \ |
204 |
} \ |
205 |
\ |
206 |
errors = 0; \
|
207 |
for (c = 1; c < 8; c++) { \ |
208 |
errors += stats[c] * (c * c); \ |
209 |
if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \ |
210 |
return 0; \ |
211 |
} \ |
212 |
} \ |
213 |
for (; c < 256; c++) { \ |
214 |
errors += stats[c] * (c * c); \ |
215 |
} \ |
216 |
errors /= (pixels - stats[0]); \
|
217 |
\ |
218 |
return errors; \
|
219 |
} |
220 |
|
221 |
DEFINE_DETECT_FUNCTION(16)
|
222 |
DEFINE_DETECT_FUNCTION(32)
|
223 |
|
224 |
static int |
225 |
tight_detect_smooth_image(VncState *vs, int w, int h) |
226 |
{ |
227 |
uint errors; |
228 |
int compression = vs->tight_compression;
|
229 |
int quality = vs->tight_quality;
|
230 |
|
231 |
if (ds_get_bytes_per_pixel(vs->ds) == 1 || |
232 |
vs->clientds.pf.bytes_per_pixel == 1 ||
|
233 |
w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) { |
234 |
return 0; |
235 |
} |
236 |
|
237 |
if (vs->tight_quality != -1) { |
238 |
if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) {
|
239 |
return 0; |
240 |
} |
241 |
} else {
|
242 |
if (w * h < tight_conf[compression].gradient_min_rect_size) {
|
243 |
return 0; |
244 |
} |
245 |
} |
246 |
|
247 |
if (vs->clientds.pf.bytes_per_pixel == 4) { |
248 |
if (vs->tight_pixel24) {
|
249 |
errors = tight_detect_smooth_image24(vs, w, h); |
250 |
if (vs->tight_quality != -1) { |
251 |
return (errors < tight_conf[quality].jpeg_threshold24);
|
252 |
} |
253 |
return (errors < tight_conf[compression].gradient_threshold24);
|
254 |
} else {
|
255 |
errors = tight_detect_smooth_image32(vs, w, h); |
256 |
} |
257 |
} else {
|
258 |
errors = tight_detect_smooth_image16(vs, w, h); |
259 |
} |
260 |
if (quality != -1) { |
261 |
return (errors < tight_conf[quality].jpeg_threshold);
|
262 |
} |
263 |
return (errors < tight_conf[compression].gradient_threshold);
|
264 |
} |
265 |
|
266 |
/*
|
267 |
* Code to determine how many different colors used in rectangle.
|
268 |
*/
|
269 |
|
270 |
static void tight_palette_rgb2buf(uint32_t rgb, int bpp, uint8_t buf[6]) |
271 |
{ |
272 |
memset(buf, 0, 6); |
273 |
|
274 |
if (bpp == 32) { |
275 |
buf[0] = ((rgb >> 24) & 0xFF); |
276 |
buf[1] = ((rgb >> 16) & 0xFF); |
277 |
buf[2] = ((rgb >> 8) & 0xFF); |
278 |
buf[3] = ((rgb >> 0) & 0xFF); |
279 |
buf[4] = ((buf[0] & 1) == 0) << 3 | ((buf[1] & 1) == 0) << 2; |
280 |
buf[4]|= ((buf[2] & 1) == 0) << 1 | ((buf[3] & 1) == 0) << 0; |
281 |
buf[0] |= 1; |
282 |
buf[1] |= 1; |
283 |
buf[2] |= 1; |
284 |
buf[3] |= 1; |
285 |
} |
286 |
if (bpp == 16) { |
287 |
buf[0] = ((rgb >> 8) & 0xFF); |
288 |
buf[1] = ((rgb >> 0) & 0xFF); |
289 |
buf[2] = ((buf[0] & 1) == 0) << 1 | ((buf[1] & 1) == 0) << 0; |
290 |
buf[0] |= 1; |
291 |
buf[1] |= 1; |
292 |
} |
293 |
} |
294 |
|
295 |
static uint32_t tight_palette_buf2rgb(int bpp, const uint8_t *buf) |
296 |
{ |
297 |
uint32_t rgb = 0;
|
298 |
|
299 |
if (bpp == 32) { |
300 |
rgb |= ((buf[0] & ~1) | !((buf[4] >> 3) & 1)) << 24; |
301 |
rgb |= ((buf[1] & ~1) | !((buf[4] >> 2) & 1)) << 16; |
302 |
rgb |= ((buf[2] & ~1) | !((buf[4] >> 1) & 1)) << 8; |
303 |
rgb |= ((buf[3] & ~1) | !((buf[4] >> 0) & 1)) << 0; |
304 |
} |
305 |
if (bpp == 16) { |
306 |
rgb |= ((buf[0] & ~1) | !((buf[2] >> 1) & 1)) << 8; |
307 |
rgb |= ((buf[1] & ~1) | !((buf[2] >> 0) & 1)) << 0; |
308 |
} |
309 |
return rgb;
|
310 |
} |
311 |
|
312 |
|
313 |
static int tight_palette_insert(QDict *palette, uint32_t rgb, int bpp, int max) |
314 |
{ |
315 |
uint8_t key[6];
|
316 |
int idx = qdict_size(palette);
|
317 |
bool present;
|
318 |
|
319 |
tight_palette_rgb2buf(rgb, bpp, key); |
320 |
present = qdict_haskey(palette, (char *)key);
|
321 |
if (idx >= max && !present) {
|
322 |
return 0; |
323 |
} |
324 |
if (!present) {
|
325 |
qdict_put(palette, (char *)key, qint_from_int(idx));
|
326 |
} |
327 |
return qdict_size(palette);
|
328 |
} |
329 |
|
330 |
#define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
|
331 |
\ |
332 |
static int \ |
333 |
tight_fill_palette##bpp(VncState *vs, int x, int y, \ |
334 |
int max, size_t count, \
|
335 |
uint32_t *bg, uint32_t *fg, \ |
336 |
struct QDict **palette) { \
|
337 |
uint##bpp##_t *data; \ |
338 |
uint##bpp##_t c0, c1, ci; \ |
339 |
int i, n0, n1; \
|
340 |
\ |
341 |
data = (uint##bpp##_t *)vs->tight.buffer; \ |
342 |
\ |
343 |
c0 = data[0]; \
|
344 |
i = 1; \
|
345 |
while (i < count && data[i] == c0) \
|
346 |
i++; \ |
347 |
if (i >= count) { \
|
348 |
*bg = *fg = c0; \ |
349 |
return 1; \ |
350 |
} \ |
351 |
\ |
352 |
if (max < 2) { \ |
353 |
return 0; \ |
354 |
} \ |
355 |
\ |
356 |
n0 = i; \ |
357 |
c1 = data[i]; \ |
358 |
n1 = 0; \
|
359 |
for (i++; i < count; i++) { \
|
360 |
ci = data[i]; \ |
361 |
if (ci == c0) { \
|
362 |
n0++; \ |
363 |
} else if (ci == c1) { \ |
364 |
n1++; \ |
365 |
} else \
|
366 |
break; \
|
367 |
} \ |
368 |
if (i >= count) { \
|
369 |
if (n0 > n1) { \
|
370 |
*bg = (uint32_t)c0; \ |
371 |
*fg = (uint32_t)c1; \ |
372 |
} else { \
|
373 |
*bg = (uint32_t)c1; \ |
374 |
*fg = (uint32_t)c0; \ |
375 |
} \ |
376 |
return 2; \ |
377 |
} \ |
378 |
\ |
379 |
if (max == 2) { \ |
380 |
return 0; \ |
381 |
} \ |
382 |
\ |
383 |
*palette = qdict_new(); \ |
384 |
tight_palette_insert(*palette, c0, bpp, max); \ |
385 |
tight_palette_insert(*palette, c1, bpp, max); \ |
386 |
tight_palette_insert(*palette, ci, bpp, max); \ |
387 |
\ |
388 |
for (i++; i < count; i++) { \
|
389 |
if (data[i] == ci) { \
|
390 |
continue; \
|
391 |
} else { \
|
392 |
if (!tight_palette_insert(*palette, (uint32_t)ci, \
|
393 |
bpp, max)) { \ |
394 |
return 0; \ |
395 |
} \ |
396 |
ci = data[i]; \ |
397 |
} \ |
398 |
} \ |
399 |
\ |
400 |
return qdict_size(*palette); \
|
401 |
} |
402 |
|
403 |
DEFINE_FILL_PALETTE_FUNCTION(8)
|
404 |
DEFINE_FILL_PALETTE_FUNCTION(16)
|
405 |
DEFINE_FILL_PALETTE_FUNCTION(32)
|
406 |
|
407 |
static int tight_fill_palette(VncState *vs, int x, int y, |
408 |
size_t count, uint32_t *bg, uint32_t *fg, |
409 |
struct QDict **palette)
|
410 |
{ |
411 |
int max;
|
412 |
|
413 |
max = count / tight_conf[vs->tight_compression].idx_max_colors_divisor; |
414 |
if (max < 2 && |
415 |
count >= tight_conf[vs->tight_compression].mono_min_rect_size) { |
416 |
max = 2;
|
417 |
} |
418 |
if (max >= 256) { |
419 |
max = 256;
|
420 |
} |
421 |
|
422 |
switch(vs->clientds.pf.bytes_per_pixel) {
|
423 |
case 4: |
424 |
return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
|
425 |
case 2: |
426 |
return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
|
427 |
default:
|
428 |
max = 2;
|
429 |
return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
|
430 |
} |
431 |
return 0; |
432 |
} |
433 |
|
434 |
/* Callback to dump a palette with qdict_iter
|
435 |
static void print_palette(const char *key, QObject *obj, void *opaque)
|
436 |
{
|
437 |
uint8_t idx = qint_get_int(qobject_to_qint(obj));
|
438 |
uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
|
439 |
|
440 |
fprintf(stderr, "%.2x ", (unsigned char)*key);
|
441 |
while (*key++)
|
442 |
fprintf(stderr, "%.2x ", (unsigned char)*key);
|
443 |
|
444 |
fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
|
445 |
}
|
446 |
*/
|
447 |
|
448 |
/*
|
449 |
* Converting truecolor samples into palette indices.
|
450 |
*/
|
451 |
#define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
|
452 |
\ |
453 |
static void \ |
454 |
tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \ |
455 |
struct QDict *palette) { \
|
456 |
uint##bpp##_t *src; \ |
457 |
uint##bpp##_t rgb; \ |
458 |
uint8_t key[6]; \
|
459 |
int i, rep; \
|
460 |
uint8_t idx; \ |
461 |
\ |
462 |
src = (uint##bpp##_t *) buf; \ |
463 |
\ |
464 |
for (i = 0; i < count; i++) { \ |
465 |
rgb = *src++; \ |
466 |
rep = 0; \
|
467 |
while (i < count && *src == rgb) { \
|
468 |
rep++, src++, i++; \ |
469 |
} \ |
470 |
tight_palette_rgb2buf(rgb, bpp, key); \ |
471 |
if (!qdict_haskey(palette, (char *)key)) { \ |
472 |
/* \
|
473 |
* Should never happen, but don't break everything \
|
474 |
* if it does, use the first color instead \
|
475 |
*/ \
|
476 |
idx = 0; \
|
477 |
} else { \
|
478 |
idx = qdict_get_int(palette, (char *)key); \
|
479 |
} \ |
480 |
while (rep >= 0) { \ |
481 |
*buf++ = idx; \ |
482 |
rep--; \ |
483 |
} \ |
484 |
} \ |
485 |
} |
486 |
|
487 |
DEFINE_IDX_ENCODE_FUNCTION(16)
|
488 |
DEFINE_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 |
|
541 |
DEFINE_MONO_ENCODE_FUNCTION(8)
|
542 |
DEFINE_MONO_ENCODE_FUNCTION(16)
|
543 |
DEFINE_MONO_ENCODE_FUNCTION(32)
|
544 |
|
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 |
|
551 |
static void |
552 |
tight_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; |
563 |
memset(vs->tight_gradient.buffer, 0, w * 3 * sizeof(int)); |
564 |
|
565 |
if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
|
566 |
(vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) { |
567 |
shift[0] = vs->clientds.pf.rshift;
|
568 |
shift[1] = vs->clientds.pf.gshift;
|
569 |
shift[2] = vs->clientds.pf.bshift;
|
570 |
} else {
|
571 |
shift[0] = 24 - vs->clientds.pf.rshift; |
572 |
shift[1] = 24 - vs->clientds.pf.gshift; |
573 |
shift[2] = 24 - vs->clientds.pf.bshift; |
574 |
} |
575 |
|
576 |
for (y = 0; y < h; y++) { |
577 |
for (c = 0; c < 3; c++) { |
578 |
upper[c] = 0;
|
579 |
here[c] = 0;
|
580 |
} |
581 |
prev = (int *)vs->tight_gradient.buffer;
|
582 |
for (x = 0; x < w; x++) { |
583 |
pix32 = *buf32++; |
584 |
for (c = 0; c < 3; c++) { |
585 |
upperleft[c] = upper[c]; |
586 |
left[c] = here[c]; |
587 |
upper[c] = *prev; |
588 |
here[c] = (int)(pix32 >> shift[c] & 0xFF); |
589 |
*prev++ = here[c]; |
590 |
|
591 |
prediction = left[c] + upper[c] - upperleft[c]; |
592 |
if (prediction < 0) { |
593 |
prediction = 0;
|
594 |
} else if (prediction > 0xFF) { |
595 |
prediction = 0xFF;
|
596 |
} |
597 |
*buf++ = (char)(here[c] - prediction);
|
598 |
} |
599 |
} |
600 |
} |
601 |
} |
602 |
|
603 |
|
604 |
/*
|
605 |
* ``Gradient'' filter for other color depths.
|
606 |
*/
|
607 |
|
608 |
#define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \
|
609 |
\ |
610 |
static void \ |
611 |
tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \ |
612 |
int w, int h) { \ |
613 |
uint##bpp##_t pix, diff; \ |
614 |
bool endian; \
|
615 |
int *prev; \
|
616 |
int max[3], shift[3]; \ |
617 |
int here[3], upper[3], left[3], upperleft[3]; \ |
618 |
int prediction; \
|
619 |
int x, y, c; \
|
620 |
\ |
621 |
memset (vs->tight_gradient.buffer, 0, w * 3 * sizeof(int)); \ |
622 |
\ |
623 |
endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \ |
624 |
(vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \ |
625 |
\ |
626 |
max[0] = vs->clientds.pf.rmax; \
|
627 |
max[1] = vs->clientds.pf.gmax; \
|
628 |
max[2] = vs->clientds.pf.bmax; \
|
629 |
shift[0] = vs->clientds.pf.rshift; \
|
630 |
shift[1] = vs->clientds.pf.gshift; \
|
631 |
shift[2] = vs->clientds.pf.bshift; \
|
632 |
\ |
633 |
for (y = 0; y < h; y++) { \ |
634 |
for (c = 0; c < 3; c++) { \ |
635 |
upper[c] = 0; \
|
636 |
here[c] = 0; \
|
637 |
} \ |
638 |
prev = (int *)vs->tight_gradient.buffer; \
|
639 |
for (x = 0; x < w; x++) { \ |
640 |
pix = *buf; \ |
641 |
if (endian) { \
|
642 |
pix = bswap_##bpp(pix); \ |
643 |
} \ |
644 |
diff = 0; \
|
645 |
for (c = 0; c < 3; c++) { \ |
646 |
upperleft[c] = upper[c]; \ |
647 |
left[c] = here[c]; \ |
648 |
upper[c] = *prev; \ |
649 |
here[c] = (int)(pix >> shift[c] & max[c]); \
|
650 |
*prev++ = here[c]; \ |
651 |
\ |
652 |
prediction = left[c] + upper[c] - upperleft[c]; \ |
653 |
if (prediction < 0) { \ |
654 |
prediction = 0; \
|
655 |
} else if (prediction > max[c]) { \ |
656 |
prediction = max[c]; \ |
657 |
} \ |
658 |
diff |= ((here[c] - prediction) & max[c]) \ |
659 |
<< shift[c]; \ |
660 |
} \ |
661 |
if (endian) { \
|
662 |
diff = bswap_##bpp(diff); \ |
663 |
} \ |
664 |
*buf++ = diff; \ |
665 |
} \ |
666 |
} \ |
667 |
} |
668 |
|
669 |
DEFINE_GRADIENT_FILTER_FUNCTION(16)
|
670 |
DEFINE_GRADIENT_FILTER_FUNCTION(32)
|
671 |
|
672 |
/*
|
673 |
* Check if a rectangle is all of the same color. If needSameColor is
|
674 |
* set to non-zero, then also check that its color equals to the
|
675 |
* *colorPtr value. The result is 1 if the test is successfull, and in
|
676 |
* that case new color will be stored in *colorPtr.
|
677 |
*/
|
678 |
|
679 |
#define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
|
680 |
\ |
681 |
static bool \ |
682 |
check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \ |
683 |
uint32_t* color, bool samecolor) \
|
684 |
{ \ |
685 |
VncDisplay *vd = vs->vd; \ |
686 |
uint##bpp##_t *fbptr; \ |
687 |
uint##bpp##_t c; \ |
688 |
int dx, dy; \
|
689 |
\ |
690 |
fbptr = (uint##bpp##_t *) \ |
691 |
(vd->server->data + y * ds_get_linesize(vs->ds) + \ |
692 |
x * ds_get_bytes_per_pixel(vs->ds)); \ |
693 |
\ |
694 |
c = *fbptr; \ |
695 |
if (samecolor && (uint32_t)c != *color) { \
|
696 |
return false; \ |
697 |
} \ |
698 |
\ |
699 |
for (dy = 0; dy < h; dy++) { \ |
700 |
for (dx = 0; dx < w; dx++) { \ |
701 |
if (c != fbptr[dx]) { \
|
702 |
return false; \ |
703 |
} \ |
704 |
} \ |
705 |
fbptr = (uint##bpp##_t *) \ |
706 |
((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \ |
707 |
} \ |
708 |
\ |
709 |
*color = (uint32_t)c; \ |
710 |
return true; \ |
711 |
} |
712 |
|
713 |
DEFINE_CHECK_SOLID_FUNCTION(32)
|
714 |
DEFINE_CHECK_SOLID_FUNCTION(16)
|
715 |
DEFINE_CHECK_SOLID_FUNCTION(8)
|
716 |
|
717 |
static bool check_solid_tile(VncState *vs, int x, int y, int w, int h, |
718 |
uint32_t* color, bool samecolor)
|
719 |
{ |
720 |
VncDisplay *vd = vs->vd; |
721 |
|
722 |
switch(vd->server->pf.bytes_per_pixel) {
|
723 |
case 4: |
724 |
return check_solid_tile32(vs, x, y, w, h, color, samecolor);
|
725 |
case 2: |
726 |
return check_solid_tile16(vs, x, y, w, h, color, samecolor);
|
727 |
default:
|
728 |
return check_solid_tile8(vs, x, y, w, h, color, samecolor);
|
729 |
} |
730 |
} |
731 |
|
732 |
static void find_best_solid_area(VncState *vs, int x, int y, int w, int h, |
733 |
uint32_t color, int *w_ptr, int *h_ptr) |
734 |
{ |
735 |
int dx, dy, dw, dh;
|
736 |
int w_prev;
|
737 |
int w_best = 0, h_best = 0; |
738 |
|
739 |
w_prev = w; |
740 |
|
741 |
for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
|
742 |
|
743 |
dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy); |
744 |
dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev); |
745 |
|
746 |
if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) { |
747 |
break;
|
748 |
} |
749 |
|
750 |
for (dx = x + dw; dx < x + w_prev;) {
|
751 |
dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx); |
752 |
|
753 |
if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) { |
754 |
break;
|
755 |
} |
756 |
dx += dw; |
757 |
} |
758 |
|
759 |
w_prev = dx - x; |
760 |
if (w_prev * (dy + dh - y) > w_best * h_best) {
|
761 |
w_best = w_prev; |
762 |
h_best = dy + dh - y; |
763 |
} |
764 |
} |
765 |
|
766 |
*w_ptr = w_best; |
767 |
*h_ptr = h_best; |
768 |
} |
769 |
|
770 |
static void extend_solid_area(VncState *vs, int x, int y, int w, int h, |
771 |
uint32_t color, int *x_ptr, int *y_ptr, |
772 |
int *w_ptr, int *h_ptr) |
773 |
{ |
774 |
int cx, cy;
|
775 |
|
776 |
/* Try to extend the area upwards. */
|
777 |
for ( cy = *y_ptr - 1; |
778 |
cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true); |
779 |
cy-- ); |
780 |
*h_ptr += *y_ptr - (cy + 1);
|
781 |
*y_ptr = cy + 1;
|
782 |
|
783 |
/* ... downwards. */
|
784 |
for ( cy = *y_ptr + *h_ptr;
|
785 |
cy < y + h && |
786 |
check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true); |
787 |
cy++ ); |
788 |
*h_ptr += cy - (*y_ptr + *h_ptr); |
789 |
|
790 |
/* ... to the left. */
|
791 |
for ( cx = *x_ptr - 1; |
792 |
cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true); |
793 |
cx-- ); |
794 |
*w_ptr += *x_ptr - (cx + 1);
|
795 |
*x_ptr = cx + 1;
|
796 |
|
797 |
/* ... to the right. */
|
798 |
for ( cx = *x_ptr + *w_ptr;
|
799 |
cx < x + w && |
800 |
check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true); |
801 |
cx++ ); |
802 |
*w_ptr += cx - (*x_ptr + *w_ptr); |
803 |
} |
804 |
|
805 |
static int tight_init_stream(VncState *vs, int stream_id, |
806 |
int level, int strategy) |
807 |
{ |
808 |
z_streamp zstream = &vs->tight_stream[stream_id]; |
809 |
|
810 |
if (zstream->opaque == NULL) { |
811 |
int err;
|
812 |
|
813 |
VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
|
814 |
VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
|
815 |
zstream->zalloc = vnc_zlib_zalloc; |
816 |
zstream->zfree = vnc_zlib_zfree; |
817 |
|
818 |
err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS, |
819 |
MAX_MEM_LEVEL, strategy); |
820 |
|
821 |
if (err != Z_OK) {
|
822 |
fprintf(stderr, "VNC: error initializing zlib\n");
|
823 |
return -1; |
824 |
} |
825 |
|
826 |
vs->tight_levels[stream_id] = level; |
827 |
zstream->opaque = vs; |
828 |
} |
829 |
|
830 |
if (vs->tight_levels[stream_id] != level) {
|
831 |
if (deflateParams(zstream, level, strategy) != Z_OK) {
|
832 |
return -1; |
833 |
} |
834 |
vs->tight_levels[stream_id] = level; |
835 |
} |
836 |
return 0; |
837 |
} |
838 |
|
839 |
static void tight_send_compact_size(VncState *vs, size_t len) |
840 |
{ |
841 |
int lpc = 0; |
842 |
int bytes = 0; |
843 |
char buf[3] = {0, 0, 0}; |
844 |
|
845 |
buf[bytes++] = len & 0x7F;
|
846 |
if (len > 0x7F) { |
847 |
buf[bytes-1] |= 0x80; |
848 |
buf[bytes++] = (len >> 7) & 0x7F; |
849 |
if (len > 0x3FFF) { |
850 |
buf[bytes-1] |= 0x80; |
851 |
buf[bytes++] = (len >> 14) & 0xFF; |
852 |
} |
853 |
} |
854 |
for (lpc = 0; lpc < bytes; lpc++) { |
855 |
vnc_write_u8(vs, buf[lpc]); |
856 |
} |
857 |
} |
858 |
|
859 |
static int tight_compress_data(VncState *vs, int stream_id, size_t bytes, |
860 |
int level, int strategy) |
861 |
{ |
862 |
z_streamp zstream = &vs->tight_stream[stream_id]; |
863 |
int previous_out;
|
864 |
|
865 |
if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
|
866 |
vnc_write(vs, vs->tight.buffer, vs->tight.offset); |
867 |
return bytes;
|
868 |
} |
869 |
|
870 |
if (tight_init_stream(vs, stream_id, level, strategy)) {
|
871 |
return -1; |
872 |
} |
873 |
|
874 |
/* reserve memory in output buffer */
|
875 |
buffer_reserve(&vs->tight_zlib, bytes + 64);
|
876 |
|
877 |
/* set pointers */
|
878 |
zstream->next_in = vs->tight.buffer; |
879 |
zstream->avail_in = vs->tight.offset; |
880 |
zstream->next_out = vs->tight_zlib.buffer + vs->tight_zlib.offset; |
881 |
zstream->avail_out = vs->tight_zlib.capacity - vs->tight_zlib.offset; |
882 |
zstream->data_type = Z_BINARY; |
883 |
previous_out = zstream->total_out; |
884 |
|
885 |
/* start encoding */
|
886 |
if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
|
887 |
fprintf(stderr, "VNC: error during tight compression\n");
|
888 |
return -1; |
889 |
} |
890 |
|
891 |
vs->tight_zlib.offset = vs->tight_zlib.capacity - zstream->avail_out; |
892 |
bytes = zstream->total_out - previous_out; |
893 |
|
894 |
tight_send_compact_size(vs, bytes); |
895 |
vnc_write(vs, vs->tight_zlib.buffer, bytes); |
896 |
|
897 |
buffer_reset(&vs->tight_zlib); |
898 |
|
899 |
return bytes;
|
900 |
} |
901 |
|
902 |
/*
|
903 |
* Subencoding implementations.
|
904 |
*/
|
905 |
static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret) |
906 |
{ |
907 |
uint32_t *buf32; |
908 |
uint32_t pix; |
909 |
int rshift, gshift, bshift;
|
910 |
|
911 |
buf32 = (uint32_t *)buf; |
912 |
|
913 |
if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
|
914 |
(vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) { |
915 |
rshift = vs->clientds.pf.rshift; |
916 |
gshift = vs->clientds.pf.gshift; |
917 |
bshift = vs->clientds.pf.bshift; |
918 |
} else {
|
919 |
rshift = 24 - vs->clientds.pf.rshift;
|
920 |
gshift = 24 - vs->clientds.pf.gshift;
|
921 |
bshift = 24 - vs->clientds.pf.bshift;
|
922 |
} |
923 |
|
924 |
if (ret) {
|
925 |
*ret = count * 3;
|
926 |
} |
927 |
|
928 |
while (count--) {
|
929 |
pix = *buf32++; |
930 |
*buf++ = (char)(pix >> rshift);
|
931 |
*buf++ = (char)(pix >> gshift);
|
932 |
*buf++ = (char)(pix >> bshift);
|
933 |
} |
934 |
} |
935 |
|
936 |
static int send_full_color_rect(VncState *vs, int w, int h) |
937 |
{ |
938 |
int stream = 0; |
939 |
size_t bytes; |
940 |
|
941 |
vnc_write_u8(vs, stream << 4); /* no flushing, no filter */ |
942 |
|
943 |
if (vs->tight_pixel24) {
|
944 |
tight_pack24(vs, vs->tight.buffer, w * h, &vs->tight.offset); |
945 |
bytes = 3;
|
946 |
} else {
|
947 |
bytes = vs->clientds.pf.bytes_per_pixel; |
948 |
} |
949 |
|
950 |
bytes = tight_compress_data(vs, stream, w * h * bytes, |
951 |
tight_conf[vs->tight_compression].raw_zlib_level, |
952 |
Z_DEFAULT_STRATEGY); |
953 |
|
954 |
return (bytes >= 0); |
955 |
} |
956 |
|
957 |
static int send_solid_rect(VncState *vs) |
958 |
{ |
959 |
size_t bytes; |
960 |
|
961 |
vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */ |
962 |
|
963 |
if (vs->tight_pixel24) {
|
964 |
tight_pack24(vs, vs->tight.buffer, 1, &vs->tight.offset);
|
965 |
bytes = 3;
|
966 |
} else {
|
967 |
bytes = vs->clientds.pf.bytes_per_pixel; |
968 |
} |
969 |
|
970 |
vnc_write(vs, vs->tight.buffer, bytes); |
971 |
return 1; |
972 |
} |
973 |
|
974 |
static int send_mono_rect(VncState *vs, int w, int h, uint32_t bg, uint32_t fg) |
975 |
{ |
976 |
size_t bytes; |
977 |
int stream = 1; |
978 |
int level = tight_conf[vs->tight_compression].mono_zlib_level;
|
979 |
|
980 |
bytes = ((w + 7) / 8) * h; |
981 |
|
982 |
vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
|
983 |
vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE); |
984 |
vnc_write_u8(vs, 1);
|
985 |
|
986 |
switch(vs->clientds.pf.bytes_per_pixel) {
|
987 |
case 4: |
988 |
{ |
989 |
uint32_t buf[2] = {bg, fg};
|
990 |
size_t ret = sizeof (buf);
|
991 |
|
992 |
if (vs->tight_pixel24) {
|
993 |
tight_pack24(vs, (unsigned char*)buf, 2, &ret); |
994 |
} |
995 |
vnc_write(vs, buf, ret); |
996 |
|
997 |
tight_encode_mono_rect32(vs->tight.buffer, w, h, bg, fg); |
998 |
break;
|
999 |
} |
1000 |
case 2: |
1001 |
vnc_write(vs, &bg, 2);
|
1002 |
vnc_write(vs, &fg, 2);
|
1003 |
tight_encode_mono_rect16(vs->tight.buffer, w, h, bg, fg); |
1004 |
break;
|
1005 |
default:
|
1006 |
vnc_write_u8(vs, bg); |
1007 |
vnc_write_u8(vs, fg); |
1008 |
tight_encode_mono_rect8(vs->tight.buffer, w, h, bg, fg); |
1009 |
break;
|
1010 |
} |
1011 |
vs->tight.offset = bytes; |
1012 |
|
1013 |
bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY); |
1014 |
return (bytes >= 0); |
1015 |
} |
1016 |
|
1017 |
struct palette_cb_priv {
|
1018 |
VncState *vs; |
1019 |
uint8_t *header; |
1020 |
}; |
1021 |
|
1022 |
static void write_palette(const char *key, QObject *obj, void *opaque) |
1023 |
{ |
1024 |
struct palette_cb_priv *priv = opaque;
|
1025 |
VncState *vs = priv->vs; |
1026 |
uint32_t bytes = vs->clientds.pf.bytes_per_pixel; |
1027 |
uint8_t idx = qint_get_int(qobject_to_qint(obj)); |
1028 |
|
1029 |
if (bytes == 4) { |
1030 |
uint32_t color = tight_palette_buf2rgb(32, (uint8_t *)key);
|
1031 |
|
1032 |
((uint32_t*)priv->header)[idx] = color; |
1033 |
} else {
|
1034 |
uint16_t color = tight_palette_buf2rgb(16, (uint8_t *)key);
|
1035 |
|
1036 |
((uint16_t*)priv->header)[idx] = color; |
1037 |
} |
1038 |
} |
1039 |
|
1040 |
static bool send_gradient_rect(VncState *vs, int w, int h) |
1041 |
{ |
1042 |
int stream = 3; |
1043 |
int level = tight_conf[vs->tight_compression].gradient_zlib_level;
|
1044 |
size_t bytes; |
1045 |
|
1046 |
if (vs->clientds.pf.bytes_per_pixel == 1) |
1047 |
return send_full_color_rect(vs, w, h);
|
1048 |
|
1049 |
vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
|
1050 |
vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT); |
1051 |
|
1052 |
buffer_reserve(&vs->tight_gradient, w * 3 * sizeof (int)); |
1053 |
|
1054 |
if (vs->tight_pixel24) {
|
1055 |
tight_filter_gradient24(vs, vs->tight.buffer, w, h); |
1056 |
bytes = 3;
|
1057 |
} else if (vs->clientds.pf.bytes_per_pixel == 4) { |
1058 |
tight_filter_gradient32(vs, (uint32_t *)vs->tight.buffer, w, h); |
1059 |
bytes = 4;
|
1060 |
} else {
|
1061 |
tight_filter_gradient16(vs, (uint16_t *)vs->tight.buffer, w, h); |
1062 |
bytes = 2;
|
1063 |
} |
1064 |
|
1065 |
buffer_reset(&vs->tight_gradient); |
1066 |
|
1067 |
bytes = w * h * bytes; |
1068 |
vs->tight.offset = bytes; |
1069 |
|
1070 |
bytes = tight_compress_data(vs, stream, bytes, |
1071 |
level, Z_FILTERED); |
1072 |
return (bytes >= 0); |
1073 |
} |
1074 |
|
1075 |
static int send_palette_rect(VncState *vs, int w, int h, struct QDict *palette) |
1076 |
{ |
1077 |
int stream = 2; |
1078 |
int level = tight_conf[vs->tight_compression].idx_zlib_level;
|
1079 |
int colors;
|
1080 |
size_t bytes; |
1081 |
|
1082 |
colors = qdict_size(palette); |
1083 |
|
1084 |
vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
|
1085 |
vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE); |
1086 |
vnc_write_u8(vs, colors - 1);
|
1087 |
|
1088 |
switch(vs->clientds.pf.bytes_per_pixel) {
|
1089 |
case 4: |
1090 |
{ |
1091 |
size_t old_offset, offset; |
1092 |
uint32_t header[qdict_size(palette)]; |
1093 |
struct palette_cb_priv priv = { vs, (uint8_t *)header };
|
1094 |
|
1095 |
old_offset = vs->output.offset; |
1096 |
qdict_iter(palette, write_palette, &priv); |
1097 |
vnc_write(vs, header, sizeof(header));
|
1098 |
|
1099 |
if (vs->tight_pixel24) {
|
1100 |
tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset); |
1101 |
vs->output.offset = old_offset + offset; |
1102 |
} |
1103 |
|
1104 |
tight_encode_indexed_rect32(vs->tight.buffer, w * h, palette); |
1105 |
break;
|
1106 |
} |
1107 |
case 2: |
1108 |
{ |
1109 |
uint16_t header[qdict_size(palette)]; |
1110 |
struct palette_cb_priv priv = { vs, (uint8_t *)header };
|
1111 |
|
1112 |
qdict_iter(palette, write_palette, &priv); |
1113 |
vnc_write(vs, header, sizeof(header));
|
1114 |
tight_encode_indexed_rect16(vs->tight.buffer, w * h, palette); |
1115 |
break;
|
1116 |
} |
1117 |
default:
|
1118 |
return -1; /* No palette for 8bits colors */ |
1119 |
break;
|
1120 |
} |
1121 |
bytes = w * h; |
1122 |
vs->tight.offset = bytes; |
1123 |
|
1124 |
bytes = tight_compress_data(vs, stream, bytes, |
1125 |
level, Z_DEFAULT_STRATEGY); |
1126 |
return (bytes >= 0); |
1127 |
} |
1128 |
|
1129 |
/*
|
1130 |
* JPEG compression stuff.
|
1131 |
*/
|
1132 |
#ifdef CONFIG_VNC_JPEG
|
1133 |
static void jpeg_prepare_row24(VncState *vs, uint8_t *dst, int x, int y, |
1134 |
int count)
|
1135 |
{ |
1136 |
VncDisplay *vd = vs->vd; |
1137 |
uint32_t *fbptr; |
1138 |
uint32_t pix; |
1139 |
|
1140 |
fbptr = (uint32_t *)(vd->server->data + y * ds_get_linesize(vs->ds) + |
1141 |
x * ds_get_bytes_per_pixel(vs->ds)); |
1142 |
|
1143 |
while (count--) {
|
1144 |
pix = *fbptr++; |
1145 |
*dst++ = (uint8_t)(pix >> vs->ds->surface->pf.rshift); |
1146 |
*dst++ = (uint8_t)(pix >> vs->ds->surface->pf.gshift); |
1147 |
*dst++ = (uint8_t)(pix >> vs->ds->surface->pf.bshift); |
1148 |
} |
1149 |
} |
1150 |
|
1151 |
#define DEFINE_JPEG_GET_ROW_FUNCTION(bpp) \
|
1152 |
\ |
1153 |
static void \ |
1154 |
jpeg_prepare_row##bpp(VncState *vs, uint8_t *dst, \ |
1155 |
int x, int y, int count) \ |
1156 |
{ \ |
1157 |
VncDisplay *vd = vs->vd; \ |
1158 |
uint##bpp##_t *fbptr; \ |
1159 |
uint##bpp##_t pix; \ |
1160 |
int r, g, b; \
|
1161 |
\ |
1162 |
fbptr = (uint##bpp##_t *) \ |
1163 |
(vd->server->data + y * ds_get_linesize(vs->ds) + \ |
1164 |
x * ds_get_bytes_per_pixel(vs->ds)); \ |
1165 |
\ |
1166 |
while (count--) { \
|
1167 |
pix = *fbptr++; \ |
1168 |
\ |
1169 |
r = (int)((pix >> vs->ds->surface->pf.rshift) \
|
1170 |
& vs->ds->surface->pf.rmax); \ |
1171 |
g = (int)((pix >> vs->ds->surface->pf.gshift) \
|
1172 |
& vs->ds->surface->pf.gmax); \ |
1173 |
b = (int)((pix >> vs->ds->surface->pf.bshift) \
|
1174 |
& vs->ds->surface->pf.bmax); \ |
1175 |
\ |
1176 |
*dst++ = (uint8_t)((r * 255 + vs->ds->surface->pf.rmax / 2) \ |
1177 |
/ vs->ds->surface->pf.rmax); \ |
1178 |
*dst++ = (uint8_t)((g * 255 + vs->ds->surface->pf.gmax / 2) \ |
1179 |
/ vs->ds->surface->pf.gmax); \ |
1180 |
*dst++ = (uint8_t)((b * 255 + vs->ds->surface->pf.bmax / 2) \ |
1181 |
/ vs->ds->surface->pf.bmax); \ |
1182 |
} \ |
1183 |
} |
1184 |
|
1185 |
DEFINE_JPEG_GET_ROW_FUNCTION(16)
|
1186 |
DEFINE_JPEG_GET_ROW_FUNCTION(32)
|
1187 |
|
1188 |
static void jpeg_prepare_row(VncState *vs, uint8_t *dst, int x, int y, |
1189 |
int count)
|
1190 |
{ |
1191 |
if (vs->tight_pixel24)
|
1192 |
jpeg_prepare_row24(vs, dst, x, y, count); |
1193 |
else if (ds_get_bytes_per_pixel(vs->ds) == 4) |
1194 |
jpeg_prepare_row32(vs, dst, x, y, count); |
1195 |
else
|
1196 |
jpeg_prepare_row16(vs, dst, x, y, count); |
1197 |
} |
1198 |
|
1199 |
/*
|
1200 |
* Destination manager implementation for JPEG library.
|
1201 |
*/
|
1202 |
|
1203 |
/* This is called once per encoding */
|
1204 |
static void jpeg_init_destination(j_compress_ptr cinfo) |
1205 |
{ |
1206 |
VncState *vs = cinfo->client_data; |
1207 |
Buffer *buffer = &vs->tight_jpeg; |
1208 |
|
1209 |
cinfo->dest->next_output_byte = (JOCTET *)buffer->buffer + buffer->offset; |
1210 |
cinfo->dest->free_in_buffer = (size_t)(buffer->capacity - buffer->offset); |
1211 |
} |
1212 |
|
1213 |
/* This is called when we ran out of buffer (shouldn't happen!) */
|
1214 |
static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo)
|
1215 |
{ |
1216 |
VncState *vs = cinfo->client_data; |
1217 |
Buffer *buffer = &vs->tight_jpeg; |
1218 |
|
1219 |
buffer->offset = buffer->capacity; |
1220 |
buffer_reserve(buffer, 2048);
|
1221 |
jpeg_init_destination(cinfo); |
1222 |
return TRUE;
|
1223 |
} |
1224 |
|
1225 |
/* This is called when we are done processing data */
|
1226 |
static void jpeg_term_destination(j_compress_ptr cinfo) |
1227 |
{ |
1228 |
VncState *vs = cinfo->client_data; |
1229 |
Buffer *buffer = &vs->tight_jpeg; |
1230 |
|
1231 |
buffer->offset = buffer->capacity - cinfo->dest->free_in_buffer; |
1232 |
} |
1233 |
|
1234 |
static int send_jpeg_rect(VncState *vs, int x, int y, int w, int h, int quality) |
1235 |
{ |
1236 |
struct jpeg_compress_struct cinfo;
|
1237 |
struct jpeg_error_mgr jerr;
|
1238 |
struct jpeg_destination_mgr manager;
|
1239 |
JSAMPROW row[1];
|
1240 |
uint8_t *buf; |
1241 |
int dy;
|
1242 |
|
1243 |
if (ds_get_bytes_per_pixel(vs->ds) == 1) |
1244 |
return send_full_color_rect(vs, w, h);
|
1245 |
|
1246 |
buf = qemu_malloc(w * 3);
|
1247 |
row[0] = buf;
|
1248 |
buffer_reserve(&vs->tight_jpeg, 2048);
|
1249 |
|
1250 |
cinfo.err = jpeg_std_error(&jerr); |
1251 |
jpeg_create_compress(&cinfo); |
1252 |
|
1253 |
cinfo.client_data = vs; |
1254 |
cinfo.image_width = w; |
1255 |
cinfo.image_height = h; |
1256 |
cinfo.input_components = 3;
|
1257 |
cinfo.in_color_space = JCS_RGB; |
1258 |
|
1259 |
jpeg_set_defaults(&cinfo); |
1260 |
jpeg_set_quality(&cinfo, quality, true);
|
1261 |
|
1262 |
manager.init_destination = jpeg_init_destination; |
1263 |
manager.empty_output_buffer = jpeg_empty_output_buffer; |
1264 |
manager.term_destination = jpeg_term_destination; |
1265 |
cinfo.dest = &manager; |
1266 |
|
1267 |
jpeg_start_compress(&cinfo, true);
|
1268 |
|
1269 |
for (dy = 0; dy < h; dy++) { |
1270 |
jpeg_prepare_row(vs, buf, x, y + dy, w); |
1271 |
jpeg_write_scanlines(&cinfo, row, 1);
|
1272 |
} |
1273 |
|
1274 |
jpeg_finish_compress(&cinfo); |
1275 |
jpeg_destroy_compress(&cinfo); |
1276 |
|
1277 |
vnc_write_u8(vs, VNC_TIGHT_JPEG << 4);
|
1278 |
|
1279 |
tight_send_compact_size(vs, vs->tight_jpeg.offset); |
1280 |
vnc_write(vs, vs->tight_jpeg.buffer, vs->tight_jpeg.offset); |
1281 |
buffer_reset(&vs->tight_jpeg); |
1282 |
|
1283 |
return 1; |
1284 |
} |
1285 |
#endif /* CONFIG_VNC_JPEG */ |
1286 |
|
1287 |
static void vnc_tight_start(VncState *vs) |
1288 |
{ |
1289 |
buffer_reset(&vs->tight); |
1290 |
|
1291 |
// make the output buffer be the zlib buffer, so we can compress it later
|
1292 |
vs->tight_tmp = vs->output; |
1293 |
vs->output = vs->tight; |
1294 |
} |
1295 |
|
1296 |
static void vnc_tight_stop(VncState *vs) |
1297 |
{ |
1298 |
// switch back to normal output/zlib buffers
|
1299 |
vs->tight = vs->output; |
1300 |
vs->output = vs->tight_tmp; |
1301 |
} |
1302 |
|
1303 |
static int send_sub_rect(VncState *vs, int x, int y, int w, int h) |
1304 |
{ |
1305 |
struct QDict *palette = NULL; |
1306 |
uint32_t bg = 0, fg = 0; |
1307 |
int colors;
|
1308 |
int ret = 0; |
1309 |
|
1310 |
vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT); |
1311 |
|
1312 |
vnc_tight_start(vs); |
1313 |
vnc_raw_send_framebuffer_update(vs, x, y, w, h); |
1314 |
vnc_tight_stop(vs); |
1315 |
|
1316 |
colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette); |
1317 |
|
1318 |
if (colors == 0) { |
1319 |
if (tight_detect_smooth_image(vs, w, h)) {
|
1320 |
if (vs->tight_quality == -1) { |
1321 |
ret = send_gradient_rect(vs, w, h); |
1322 |
} else {
|
1323 |
#ifdef CONFIG_VNC_JPEG
|
1324 |
int quality = tight_conf[vs->tight_quality].jpeg_quality;
|
1325 |
|
1326 |
ret = send_jpeg_rect(vs, x, y, w, h, quality); |
1327 |
#else
|
1328 |
ret = send_full_color_rect(vs, w, h); |
1329 |
#endif
|
1330 |
} |
1331 |
} else {
|
1332 |
ret = send_full_color_rect(vs, w, h); |
1333 |
} |
1334 |
} else if (colors == 1) { |
1335 |
ret = send_solid_rect(vs); |
1336 |
} else if (colors == 2) { |
1337 |
ret = send_mono_rect(vs, w, h, bg, fg); |
1338 |
} else if (colors <= 256) { |
1339 |
#ifdef CONFIG_VNC_JPEG
|
1340 |
if (colors > 96 && vs->tight_quality != -1 && vs->tight_quality <= 3 && |
1341 |
tight_detect_smooth_image(vs, w, h)) { |
1342 |
int quality = tight_conf[vs->tight_quality].jpeg_quality;
|
1343 |
|
1344 |
ret = send_jpeg_rect(vs, x, y, w, h, quality); |
1345 |
} else {
|
1346 |
ret = send_palette_rect(vs, w, h, palette); |
1347 |
} |
1348 |
#else
|
1349 |
ret = send_palette_rect(vs, w, h, palette); |
1350 |
#endif
|
1351 |
} |
1352 |
QDECREF(palette); |
1353 |
return ret;
|
1354 |
} |
1355 |
|
1356 |
static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h) |
1357 |
{ |
1358 |
vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT); |
1359 |
|
1360 |
vnc_tight_start(vs); |
1361 |
vnc_raw_send_framebuffer_update(vs, x, y, w, h); |
1362 |
vnc_tight_stop(vs); |
1363 |
|
1364 |
return send_solid_rect(vs);
|
1365 |
} |
1366 |
|
1367 |
static int send_rect_simple(VncState *vs, int x, int y, int w, int h) |
1368 |
{ |
1369 |
int max_size, max_width;
|
1370 |
int max_sub_width, max_sub_height;
|
1371 |
int dx, dy;
|
1372 |
int rw, rh;
|
1373 |
int n = 0; |
1374 |
|
1375 |
max_size = tight_conf[vs->tight_compression].max_rect_size; |
1376 |
max_width = tight_conf[vs->tight_compression].max_rect_width; |
1377 |
|
1378 |
if (w > max_width || w * h > max_size) {
|
1379 |
max_sub_width = (w > max_width) ? max_width : w; |
1380 |
max_sub_height = max_size / max_sub_width; |
1381 |
|
1382 |
for (dy = 0; dy < h; dy += max_sub_height) { |
1383 |
for (dx = 0; dx < w; dx += max_width) { |
1384 |
rw = MIN(max_sub_width, w - dx); |
1385 |
rh = MIN(max_sub_height, h - dy); |
1386 |
n += send_sub_rect(vs, x+dx, y+dy, rw, rh); |
1387 |
} |
1388 |
} |
1389 |
} else {
|
1390 |
n += send_sub_rect(vs, x, y, w, h); |
1391 |
} |
1392 |
|
1393 |
return n;
|
1394 |
} |
1395 |
|
1396 |
static int find_large_solid_color_rect(VncState *vs, int x, int y, |
1397 |
int w, int h, int max_rows) |
1398 |
{ |
1399 |
int dx, dy, dw, dh;
|
1400 |
int n = 0; |
1401 |
|
1402 |
/* Try to find large solid-color areas and send them separately. */
|
1403 |
|
1404 |
for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
|
1405 |
|
1406 |
/* If a rectangle becomes too large, send its upper part now. */
|
1407 |
|
1408 |
if (dy - y >= max_rows) {
|
1409 |
n += send_rect_simple(vs, x, y, w, max_rows); |
1410 |
y += max_rows; |
1411 |
h -= max_rows; |
1412 |
} |
1413 |
|
1414 |
dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy)); |
1415 |
|
1416 |
for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
|
1417 |
uint32_t color_value; |
1418 |
int x_best, y_best, w_best, h_best;
|
1419 |
|
1420 |
dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx)); |
1421 |
|
1422 |
if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) { |
1423 |
continue ;
|
1424 |
} |
1425 |
|
1426 |
/* Get dimensions of solid-color area. */
|
1427 |
|
1428 |
find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y), |
1429 |
color_value, &w_best, &h_best); |
1430 |
|
1431 |
/* Make sure a solid rectangle is large enough
|
1432 |
(or the whole rectangle is of the same color). */
|
1433 |
|
1434 |
if (w_best * h_best != w * h &&
|
1435 |
w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) { |
1436 |
continue;
|
1437 |
} |
1438 |
|
1439 |
/* Try to extend solid rectangle to maximum size. */
|
1440 |
|
1441 |
x_best = dx; y_best = dy; |
1442 |
extend_solid_area(vs, x, y, w, h, color_value, |
1443 |
&x_best, &y_best, &w_best, &h_best); |
1444 |
|
1445 |
/* Send rectangles at top and left to solid-color area. */
|
1446 |
|
1447 |
if (y_best != y) {
|
1448 |
n += send_rect_simple(vs, x, y, w, y_best-y); |
1449 |
} |
1450 |
if (x_best != x) {
|
1451 |
n += vnc_tight_send_framebuffer_update(vs, x, y_best, |
1452 |
x_best-x, h_best); |
1453 |
} |
1454 |
|
1455 |
/* Send solid-color rectangle. */
|
1456 |
n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best); |
1457 |
|
1458 |
/* Send remaining rectangles (at right and bottom). */
|
1459 |
|
1460 |
if (x_best + w_best != x + w) {
|
1461 |
n += vnc_tight_send_framebuffer_update(vs, x_best+w_best, |
1462 |
y_best, |
1463 |
w-(x_best-x)-w_best, |
1464 |
h_best); |
1465 |
} |
1466 |
if (y_best + h_best != y + h) {
|
1467 |
n += vnc_tight_send_framebuffer_update(vs, x, y_best+h_best, |
1468 |
w, h-(y_best-y)-h_best); |
1469 |
} |
1470 |
|
1471 |
/* Return after all recursive calls are done. */
|
1472 |
return n;
|
1473 |
} |
1474 |
} |
1475 |
return n + send_rect_simple(vs, x, y, w, h);
|
1476 |
} |
1477 |
|
1478 |
int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, |
1479 |
int w, int h) |
1480 |
{ |
1481 |
int max_rows;
|
1482 |
|
1483 |
if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF && |
1484 |
vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) { |
1485 |
vs->tight_pixel24 = true;
|
1486 |
} else {
|
1487 |
vs->tight_pixel24 = false;
|
1488 |
} |
1489 |
|
1490 |
if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE)
|
1491 |
return send_rect_simple(vs, x, y, w, h);
|
1492 |
|
1493 |
/* Calculate maximum number of rows in one non-solid rectangle. */
|
1494 |
|
1495 |
max_rows = tight_conf[vs->tight_compression].max_rect_size; |
1496 |
max_rows /= MIN(tight_conf[vs->tight_compression].max_rect_width, w); |
1497 |
|
1498 |
return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
|
1499 |
} |
1500 |
|
1501 |
void vnc_tight_clear(VncState *vs)
|
1502 |
{ |
1503 |
int i;
|
1504 |
for (i=0; i<ARRAY_SIZE(vs->tight_stream); i++) { |
1505 |
if (vs->tight_stream[i].opaque) {
|
1506 |
deflateEnd(&vs->tight_stream[i]); |
1507 |
} |
1508 |
} |
1509 |
|
1510 |
buffer_free(&vs->tight); |
1511 |
buffer_free(&vs->tight_zlib); |
1512 |
buffer_free(&vs->tight_gradient); |
1513 |
#ifdef CONFIG_VNC_JPEG
|
1514 |
buffer_free(&vs->tight_jpeg); |
1515 |
#endif
|
1516 |
} |