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