]>
Commit | Line | Data |
---|---|---|
380282b0 CC |
1 | /* |
2 | * QEMU VNC display driver: tight encoding | |
3 | * | |
4 | * From libvncserver/libvncserver/tight.c | |
5 | * Copyright (C) 2000, 2001 Const Kaplinsky. All Rights Reserved. | |
6 | * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. | |
7 | * | |
8 | * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
26 | * THE SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <stdbool.h> | |
30 | ||
aa7d73fd CC |
31 | #include "qdict.h" |
32 | #include "qint.h" | |
380282b0 CC |
33 | #include "vnc.h" |
34 | #include "vnc-encoding-tight.h" | |
35 | ||
36 | /* Compression level stuff. The following array contains various | |
37 | encoder parameters for each of 10 compression levels (0..9). | |
38 | Last three parameters correspond to JPEG quality levels (0..9). */ | |
39 | ||
40 | static const struct { | |
41 | int max_rect_size, max_rect_width; | |
42 | int mono_min_rect_size, gradient_min_rect_size; | |
43 | int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level; | |
44 | int gradient_threshold, gradient_threshold24; | |
45 | int idx_max_colors_divisor; | |
46 | int jpeg_quality, jpeg_threshold, jpeg_threshold24; | |
47 | } tight_conf[] = { | |
48 | { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 }, | |
49 | { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 }, | |
50 | { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 }, | |
51 | { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 }, | |
52 | { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 }, | |
53 | { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 }, | |
54 | { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 }, | |
55 | { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 }, | |
56 | { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 }, | |
57 | { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 } | |
58 | }; | |
59 | ||
aa7d73fd CC |
60 | /* |
61 | * Code to determine how many different colors used in rectangle. | |
62 | */ | |
63 | ||
64 | static void tight_palette_rgb2buf(uint32_t rgb, int bpp, uint8_t buf[6]) | |
65 | { | |
66 | memset(buf, 0, 6); | |
67 | ||
68 | if (bpp == 32) { | |
69 | buf[0] = ((rgb >> 24) & 0xFF); | |
70 | buf[1] = ((rgb >> 16) & 0xFF); | |
71 | buf[2] = ((rgb >> 8) & 0xFF); | |
72 | buf[3] = ((rgb >> 0) & 0xFF); | |
73 | buf[4] = ((buf[0] & 1) == 0) << 3 | ((buf[1] & 1) == 0) << 2; | |
74 | buf[4]|= ((buf[2] & 1) == 0) << 1 | ((buf[3] & 1) == 0) << 0; | |
75 | buf[0] |= 1; | |
76 | buf[1] |= 1; | |
77 | buf[2] |= 1; | |
78 | buf[3] |= 1; | |
79 | } | |
80 | if (bpp == 16) { | |
81 | buf[0] = ((rgb >> 8) & 0xFF); | |
82 | buf[1] = ((rgb >> 0) & 0xFF); | |
83 | buf[2] = ((buf[0] & 1) == 0) << 1 | ((buf[1] & 1) == 0) << 0; | |
84 | buf[0] |= 1; | |
85 | buf[1] |= 1; | |
86 | } | |
87 | } | |
88 | ||
89 | static uint32_t tight_palette_buf2rgb(int bpp, const uint8_t *buf) | |
90 | { | |
91 | uint32_t rgb = 0; | |
92 | ||
93 | if (bpp == 32) { | |
94 | rgb |= ((buf[0] & ~1) | !((buf[4] >> 3) & 1)) << 24; | |
95 | rgb |= ((buf[1] & ~1) | !((buf[4] >> 2) & 1)) << 16; | |
96 | rgb |= ((buf[2] & ~1) | !((buf[4] >> 1) & 1)) << 8; | |
97 | rgb |= ((buf[3] & ~1) | !((buf[4] >> 0) & 1)) << 0; | |
98 | } | |
99 | if (bpp == 16) { | |
100 | rgb |= ((buf[0] & ~1) | !((buf[2] >> 1) & 1)) << 8; | |
101 | rgb |= ((buf[1] & ~1) | !((buf[2] >> 0) & 1)) << 0; | |
102 | } | |
103 | return rgb; | |
104 | } | |
105 | ||
106 | ||
107 | static int tight_palette_insert(QDict *palette, uint32_t rgb, int bpp, int max) | |
108 | { | |
109 | uint8_t key[6]; | |
110 | int idx = qdict_size(palette); | |
111 | bool present; | |
112 | ||
113 | tight_palette_rgb2buf(rgb, bpp, key); | |
114 | present = qdict_haskey(palette, (char *)key); | |
115 | if (idx >= max && !present) { | |
116 | return 0; | |
117 | } | |
118 | if (!present) { | |
119 | qdict_put(palette, (char *)key, qint_from_int(idx)); | |
120 | } | |
121 | return qdict_size(palette); | |
122 | } | |
123 | ||
124 | #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \ | |
125 | \ | |
126 | static int \ | |
127 | tight_fill_palette##bpp(VncState *vs, int x, int y, \ | |
128 | int max, size_t count, \ | |
129 | uint32_t *bg, uint32_t *fg, \ | |
130 | struct QDict **palette) { \ | |
131 | uint##bpp##_t *data; \ | |
132 | uint##bpp##_t c0, c1, ci; \ | |
133 | int i, n0, n1; \ | |
134 | \ | |
135 | data = (uint##bpp##_t *)vs->tight.buffer; \ | |
136 | \ | |
137 | c0 = data[0]; \ | |
138 | i = 1; \ | |
139 | while (i < count && data[i] == c0) \ | |
140 | i++; \ | |
141 | if (i >= count) { \ | |
142 | *bg = *fg = c0; \ | |
143 | return 1; \ | |
144 | } \ | |
145 | \ | |
146 | if (max < 2) { \ | |
147 | return 0; \ | |
148 | } \ | |
149 | \ | |
150 | n0 = i; \ | |
151 | c1 = data[i]; \ | |
152 | n1 = 0; \ | |
153 | for (i++; i < count; i++) { \ | |
154 | ci = data[i]; \ | |
155 | if (ci == c0) { \ | |
156 | n0++; \ | |
157 | } else if (ci == c1) { \ | |
158 | n1++; \ | |
159 | } else \ | |
160 | break; \ | |
161 | } \ | |
162 | if (i >= count) { \ | |
163 | if (n0 > n1) { \ | |
164 | *bg = (uint32_t)c0; \ | |
165 | *fg = (uint32_t)c1; \ | |
166 | } else { \ | |
167 | *bg = (uint32_t)c1; \ | |
168 | *fg = (uint32_t)c0; \ | |
169 | } \ | |
170 | return 2; \ | |
171 | } \ | |
172 | \ | |
173 | if (max == 2) { \ | |
174 | return 0; \ | |
175 | } \ | |
176 | \ | |
177 | *palette = qdict_new(); \ | |
178 | tight_palette_insert(*palette, c0, bpp, max); \ | |
179 | tight_palette_insert(*palette, c1, bpp, max); \ | |
270ec219 | 180 | tight_palette_insert(*palette, ci, bpp, max); \ |
aa7d73fd CC |
181 | \ |
182 | for (i++; i < count; i++) { \ | |
183 | if (data[i] == ci) { \ | |
184 | continue; \ | |
185 | } else { \ | |
186 | if (!tight_palette_insert(*palette, (uint32_t)ci, \ | |
187 | bpp, max)) { \ | |
188 | return 0; \ | |
189 | } \ | |
190 | ci = data[i]; \ | |
191 | } \ | |
192 | } \ | |
193 | \ | |
194 | return qdict_size(*palette); \ | |
195 | } | |
196 | ||
197 | DEFINE_FILL_PALETTE_FUNCTION(8) | |
198 | DEFINE_FILL_PALETTE_FUNCTION(16) | |
199 | DEFINE_FILL_PALETTE_FUNCTION(32) | |
200 | ||
201 | static int tight_fill_palette(VncState *vs, int x, int y, | |
202 | size_t count, uint32_t *bg, uint32_t *fg, | |
203 | struct QDict **palette) | |
204 | { | |
205 | int max; | |
206 | ||
207 | max = count / tight_conf[vs->tight_compression].idx_max_colors_divisor; | |
208 | if (max < 2 && | |
209 | count >= tight_conf[vs->tight_compression].mono_min_rect_size) { | |
210 | max = 2; | |
211 | } | |
212 | if (max >= 256) { | |
213 | max = 256; | |
214 | } | |
215 | ||
216 | switch(vs->clientds.pf.bytes_per_pixel) { | |
217 | case 4: | |
218 | return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette); | |
219 | case 2: | |
220 | return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette); | |
221 | default: | |
222 | max = 2; | |
223 | return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette); | |
224 | } | |
225 | return 0; | |
226 | } | |
227 | ||
228 | /* Callback to dump a palette with qdict_iter | |
229 | static void print_palette(const char *key, QObject *obj, void *opaque) | |
230 | { | |
231 | uint8_t idx = qint_get_int(qobject_to_qint(obj)); | |
232 | uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key); | |
233 | ||
234 | fprintf(stderr, "%.2x ", (unsigned char)*key); | |
235 | while (*key++) | |
236 | fprintf(stderr, "%.2x ", (unsigned char)*key); | |
237 | ||
238 | fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb); | |
239 | } | |
240 | */ | |
241 | ||
242 | /* | |
243 | * Converting truecolor samples into palette indices. | |
244 | */ | |
245 | #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \ | |
246 | \ | |
247 | static void \ | |
248 | tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \ | |
249 | struct QDict *palette) { \ | |
250 | uint##bpp##_t *src; \ | |
251 | uint##bpp##_t rgb; \ | |
252 | uint8_t key[6]; \ | |
54d43eac | 253 | int i, rep; \ |
aa7d73fd CC |
254 | uint8_t idx; \ |
255 | \ | |
256 | src = (uint##bpp##_t *) buf; \ | |
257 | \ | |
54d43eac | 258 | for (i = 0; i < count; i++) { \ |
aa7d73fd CC |
259 | rgb = *src++; \ |
260 | rep = 0; \ | |
54d43eac CC |
261 | while (i < count && *src == rgb) { \ |
262 | rep++, src++, i++; \ | |
aa7d73fd CC |
263 | } \ |
264 | tight_palette_rgb2buf(rgb, bpp, key); \ | |
265 | if (!qdict_haskey(palette, (char *)key)) { \ | |
266 | /* \ | |
267 | * Should never happen, but don't break everything \ | |
268 | * if it does, use the first color instead \ | |
269 | */ \ | |
270 | idx = 0; \ | |
271 | } else { \ | |
272 | idx = qdict_get_int(palette, (char *)key); \ | |
273 | } \ | |
274 | while (rep >= 0) { \ | |
275 | *buf++ = idx; \ | |
276 | rep--; \ | |
277 | } \ | |
278 | } \ | |
279 | } | |
280 | ||
281 | DEFINE_IDX_ENCODE_FUNCTION(16) | |
282 | DEFINE_IDX_ENCODE_FUNCTION(32) | |
283 | ||
284 | #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \ | |
285 | \ | |
286 | static void \ | |
287 | tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \ | |
288 | uint##bpp##_t bg, uint##bpp##_t fg) { \ | |
289 | uint##bpp##_t *ptr; \ | |
290 | unsigned int value, mask; \ | |
291 | int aligned_width; \ | |
292 | int x, y, bg_bits; \ | |
293 | \ | |
294 | ptr = (uint##bpp##_t *) buf; \ | |
295 | aligned_width = w - w % 8; \ | |
296 | \ | |
297 | for (y = 0; y < h; y++) { \ | |
298 | for (x = 0; x < aligned_width; x += 8) { \ | |
299 | for (bg_bits = 0; bg_bits < 8; bg_bits++) { \ | |
300 | if (*ptr++ != bg) { \ | |
301 | break; \ | |
302 | } \ | |
303 | } \ | |
304 | if (bg_bits == 8) { \ | |
305 | *buf++ = 0; \ | |
306 | continue; \ | |
307 | } \ | |
308 | mask = 0x80 >> bg_bits; \ | |
309 | value = mask; \ | |
310 | for (bg_bits++; bg_bits < 8; bg_bits++) { \ | |
311 | mask >>= 1; \ | |
312 | if (*ptr++ != bg) { \ | |
313 | value |= mask; \ | |
314 | } \ | |
315 | } \ | |
316 | *buf++ = (uint8_t)value; \ | |
317 | } \ | |
318 | \ | |
319 | mask = 0x80; \ | |
320 | value = 0; \ | |
321 | if (x >= w) { \ | |
322 | continue; \ | |
323 | } \ | |
324 | \ | |
325 | for (; x < w; x++) { \ | |
326 | if (*ptr++ != bg) { \ | |
327 | value |= mask; \ | |
328 | } \ | |
329 | mask >>= 1; \ | |
330 | } \ | |
331 | *buf++ = (uint8_t)value; \ | |
332 | } \ | |
333 | } | |
334 | ||
335 | DEFINE_MONO_ENCODE_FUNCTION(8) | |
336 | DEFINE_MONO_ENCODE_FUNCTION(16) | |
337 | DEFINE_MONO_ENCODE_FUNCTION(32) | |
338 | ||
b4bea3f2 CC |
339 | /* |
340 | * Check if a rectangle is all of the same color. If needSameColor is | |
341 | * set to non-zero, then also check that its color equals to the | |
342 | * *colorPtr value. The result is 1 if the test is successfull, and in | |
343 | * that case new color will be stored in *colorPtr. | |
344 | */ | |
345 | ||
346 | #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \ | |
347 | \ | |
348 | static bool \ | |
349 | check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \ | |
350 | uint32_t* color, bool samecolor) \ | |
351 | { \ | |
352 | VncDisplay *vd = vs->vd; \ | |
353 | uint##bpp##_t *fbptr; \ | |
354 | uint##bpp##_t c; \ | |
355 | int dx, dy; \ | |
356 | \ | |
357 | fbptr = (uint##bpp##_t *) \ | |
358 | (vd->server->data + y * ds_get_linesize(vs->ds) + \ | |
359 | x * ds_get_bytes_per_pixel(vs->ds)); \ | |
360 | \ | |
361 | c = *fbptr; \ | |
362 | if (samecolor && (uint32_t)c != *color) { \ | |
363 | return false; \ | |
364 | } \ | |
365 | \ | |
366 | for (dy = 0; dy < h; dy++) { \ | |
367 | for (dx = 0; dx < w; dx++) { \ | |
368 | if (c != fbptr[dx]) { \ | |
369 | return false; \ | |
370 | } \ | |
371 | } \ | |
372 | fbptr = (uint##bpp##_t *) \ | |
373 | ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \ | |
374 | } \ | |
375 | \ | |
376 | *color = (uint32_t)c; \ | |
377 | return true; \ | |
378 | } | |
379 | ||
380 | DEFINE_CHECK_SOLID_FUNCTION(32) | |
381 | DEFINE_CHECK_SOLID_FUNCTION(16) | |
382 | DEFINE_CHECK_SOLID_FUNCTION(8) | |
383 | ||
384 | static bool check_solid_tile(VncState *vs, int x, int y, int w, int h, | |
385 | uint32_t* color, bool samecolor) | |
386 | { | |
387 | VncDisplay *vd = vs->vd; | |
388 | ||
389 | switch(vd->server->pf.bytes_per_pixel) { | |
390 | case 4: | |
391 | return check_solid_tile32(vs, x, y, w, h, color, samecolor); | |
392 | case 2: | |
393 | return check_solid_tile16(vs, x, y, w, h, color, samecolor); | |
394 | default: | |
395 | return check_solid_tile8(vs, x, y, w, h, color, samecolor); | |
396 | } | |
397 | } | |
398 | ||
399 | static void find_best_solid_area(VncState *vs, int x, int y, int w, int h, | |
400 | uint32_t color, int *w_ptr, int *h_ptr) | |
401 | { | |
402 | int dx, dy, dw, dh; | |
403 | int w_prev; | |
404 | int w_best = 0, h_best = 0; | |
405 | ||
406 | w_prev = w; | |
407 | ||
408 | for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) { | |
409 | ||
410 | dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy); | |
411 | dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev); | |
412 | ||
413 | if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) { | |
414 | break; | |
415 | } | |
416 | ||
417 | for (dx = x + dw; dx < x + w_prev;) { | |
418 | dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx); | |
419 | ||
420 | if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) { | |
421 | break; | |
422 | } | |
423 | dx += dw; | |
424 | } | |
425 | ||
426 | w_prev = dx - x; | |
427 | if (w_prev * (dy + dh - y) > w_best * h_best) { | |
428 | w_best = w_prev; | |
429 | h_best = dy + dh - y; | |
430 | } | |
431 | } | |
432 | ||
433 | *w_ptr = w_best; | |
434 | *h_ptr = h_best; | |
435 | } | |
436 | ||
437 | static void extend_solid_area(VncState *vs, int x, int y, int w, int h, | |
438 | uint32_t color, int *x_ptr, int *y_ptr, | |
439 | int *w_ptr, int *h_ptr) | |
440 | { | |
441 | int cx, cy; | |
442 | ||
443 | /* Try to extend the area upwards. */ | |
444 | for ( cy = *y_ptr - 1; | |
445 | cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true); | |
446 | cy-- ); | |
447 | *h_ptr += *y_ptr - (cy + 1); | |
448 | *y_ptr = cy + 1; | |
449 | ||
450 | /* ... downwards. */ | |
451 | for ( cy = *y_ptr + *h_ptr; | |
452 | cy < y + h && | |
453 | check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true); | |
454 | cy++ ); | |
455 | *h_ptr += cy - (*y_ptr + *h_ptr); | |
456 | ||
457 | /* ... to the left. */ | |
458 | for ( cx = *x_ptr - 1; | |
459 | cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true); | |
460 | cx-- ); | |
461 | *w_ptr += *x_ptr - (cx + 1); | |
462 | *x_ptr = cx + 1; | |
463 | ||
464 | /* ... to the right. */ | |
465 | for ( cx = *x_ptr + *w_ptr; | |
466 | cx < x + w && | |
467 | check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true); | |
468 | cx++ ); | |
469 | *w_ptr += cx - (*x_ptr + *w_ptr); | |
470 | } | |
471 | ||
380282b0 CC |
472 | static int tight_init_stream(VncState *vs, int stream_id, |
473 | int level, int strategy) | |
474 | { | |
475 | z_streamp zstream = &vs->tight_stream[stream_id]; | |
476 | ||
477 | if (zstream->opaque == NULL) { | |
478 | int err; | |
479 | ||
480 | VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id); | |
481 | VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs); | |
482 | zstream->zalloc = vnc_zlib_zalloc; | |
483 | zstream->zfree = vnc_zlib_zfree; | |
484 | ||
485 | err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS, | |
486 | MAX_MEM_LEVEL, strategy); | |
487 | ||
488 | if (err != Z_OK) { | |
489 | fprintf(stderr, "VNC: error initializing zlib\n"); | |
490 | return -1; | |
491 | } | |
492 | ||
493 | vs->tight_levels[stream_id] = level; | |
494 | zstream->opaque = vs; | |
495 | } | |
496 | ||
497 | if (vs->tight_levels[stream_id] != level) { | |
498 | if (deflateParams(zstream, level, strategy) != Z_OK) { | |
499 | return -1; | |
500 | } | |
501 | vs->tight_levels[stream_id] = level; | |
502 | } | |
503 | return 0; | |
504 | } | |
505 | ||
506 | static void tight_send_compact_size(VncState *vs, size_t len) | |
507 | { | |
508 | int lpc = 0; | |
509 | int bytes = 0; | |
510 | char buf[3] = {0, 0, 0}; | |
511 | ||
512 | buf[bytes++] = len & 0x7F; | |
513 | if (len > 0x7F) { | |
514 | buf[bytes-1] |= 0x80; | |
515 | buf[bytes++] = (len >> 7) & 0x7F; | |
516 | if (len > 0x3FFF) { | |
517 | buf[bytes-1] |= 0x80; | |
518 | buf[bytes++] = (len >> 14) & 0xFF; | |
519 | } | |
520 | } | |
b4bea3f2 | 521 | for (lpc = 0; lpc < bytes; lpc++) { |
380282b0 CC |
522 | vnc_write_u8(vs, buf[lpc]); |
523 | } | |
524 | } | |
525 | ||
526 | static int tight_compress_data(VncState *vs, int stream_id, size_t bytes, | |
527 | int level, int strategy) | |
528 | { | |
529 | z_streamp zstream = &vs->tight_stream[stream_id]; | |
530 | int previous_out; | |
531 | ||
532 | if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) { | |
533 | vnc_write(vs, vs->tight.buffer, vs->tight.offset); | |
534 | return bytes; | |
535 | } | |
536 | ||
537 | if (tight_init_stream(vs, stream_id, level, strategy)) { | |
538 | return -1; | |
539 | } | |
540 | ||
541 | /* reserve memory in output buffer */ | |
542 | buffer_reserve(&vs->tight_zlib, bytes + 64); | |
543 | ||
544 | /* set pointers */ | |
545 | zstream->next_in = vs->tight.buffer; | |
546 | zstream->avail_in = vs->tight.offset; | |
547 | zstream->next_out = vs->tight_zlib.buffer + vs->tight_zlib.offset; | |
548 | zstream->avail_out = vs->tight_zlib.capacity - vs->tight_zlib.offset; | |
549 | zstream->data_type = Z_BINARY; | |
550 | previous_out = zstream->total_out; | |
551 | ||
552 | /* start encoding */ | |
553 | if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) { | |
554 | fprintf(stderr, "VNC: error during tight compression\n"); | |
555 | return -1; | |
556 | } | |
557 | ||
558 | vs->tight_zlib.offset = vs->tight_zlib.capacity - zstream->avail_out; | |
559 | bytes = zstream->total_out - previous_out; | |
560 | ||
561 | tight_send_compact_size(vs, bytes); | |
562 | vnc_write(vs, vs->tight_zlib.buffer, bytes); | |
563 | ||
564 | buffer_reset(&vs->tight_zlib); | |
565 | ||
566 | return bytes; | |
567 | } | |
568 | ||
569 | /* | |
570 | * Subencoding implementations. | |
571 | */ | |
aa7d73fd | 572 | static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret) |
380282b0 | 573 | { |
380282b0 CC |
574 | uint32_t *buf32; |
575 | uint32_t pix; | |
576 | int rshift, gshift, bshift; | |
577 | ||
380282b0 CC |
578 | buf32 = (uint32_t *)buf; |
579 | ||
580 | if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) == | |
581 | (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) { | |
582 | rshift = vs->clientds.pf.rshift; | |
583 | gshift = vs->clientds.pf.gshift; | |
584 | bshift = vs->clientds.pf.bshift; | |
585 | } else { | |
586 | rshift = 24 - vs->clientds.pf.rshift; | |
587 | gshift = 24 - vs->clientds.pf.gshift; | |
588 | bshift = 24 - vs->clientds.pf.bshift; | |
589 | } | |
590 | ||
aa7d73fd CC |
591 | if (ret) { |
592 | *ret = count * 3; | |
593 | } | |
380282b0 CC |
594 | |
595 | while (count--) { | |
596 | pix = *buf32++; | |
597 | *buf++ = (char)(pix >> rshift); | |
598 | *buf++ = (char)(pix >> gshift); | |
599 | *buf++ = (char)(pix >> bshift); | |
600 | } | |
601 | } | |
602 | ||
603 | static int send_full_color_rect(VncState *vs, int w, int h) | |
604 | { | |
605 | int stream = 0; | |
606 | size_t bytes; | |
607 | ||
608 | vnc_write_u8(vs, stream << 4); /* no flushing, no filter */ | |
609 | ||
610 | if (vs->tight_pixel24) { | |
aa7d73fd | 611 | tight_pack24(vs, vs->tight.buffer, w * h, &vs->tight.offset); |
380282b0 CC |
612 | bytes = 3; |
613 | } else { | |
614 | bytes = vs->clientds.pf.bytes_per_pixel; | |
615 | } | |
616 | ||
617 | bytes = tight_compress_data(vs, stream, w * h * bytes, | |
618 | tight_conf[vs->tight_compression].raw_zlib_level, | |
619 | Z_DEFAULT_STRATEGY); | |
620 | ||
621 | return (bytes >= 0); | |
622 | } | |
623 | ||
b4bea3f2 CC |
624 | static int send_solid_rect(VncState *vs) |
625 | { | |
626 | size_t bytes; | |
627 | ||
628 | vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */ | |
629 | ||
630 | if (vs->tight_pixel24) { | |
aa7d73fd | 631 | tight_pack24(vs, vs->tight.buffer, 1, &vs->tight.offset); |
b4bea3f2 CC |
632 | bytes = 3; |
633 | } else { | |
634 | bytes = vs->clientds.pf.bytes_per_pixel; | |
635 | } | |
636 | ||
637 | vnc_write(vs, vs->tight.buffer, bytes); | |
638 | return 1; | |
639 | } | |
640 | ||
aa7d73fd CC |
641 | static int send_mono_rect(VncState *vs, int w, int h, uint32_t bg, uint32_t fg) |
642 | { | |
643 | size_t bytes; | |
644 | int stream = 1; | |
645 | int level = tight_conf[vs->tight_compression].mono_zlib_level; | |
646 | ||
647 | bytes = ((w + 7) / 8) * h; | |
648 | ||
649 | vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4); | |
650 | vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE); | |
651 | vnc_write_u8(vs, 1); | |
652 | ||
653 | switch(vs->clientds.pf.bytes_per_pixel) { | |
654 | case 4: | |
655 | { | |
656 | uint32_t buf[2] = {bg, fg}; | |
657 | size_t ret = sizeof (buf); | |
658 | ||
659 | if (vs->tight_pixel24) { | |
660 | tight_pack24(vs, (unsigned char*)buf, 2, &ret); | |
661 | } | |
662 | vnc_write(vs, buf, ret); | |
663 | ||
664 | tight_encode_mono_rect32(vs->tight.buffer, w, h, bg, fg); | |
665 | break; | |
666 | } | |
667 | case 2: | |
668 | vnc_write(vs, &bg, 2); | |
669 | vnc_write(vs, &fg, 2); | |
670 | tight_encode_mono_rect16(vs->tight.buffer, w, h, bg, fg); | |
671 | break; | |
672 | default: | |
673 | vnc_write_u8(vs, bg); | |
674 | vnc_write_u8(vs, fg); | |
675 | tight_encode_mono_rect8(vs->tight.buffer, w, h, bg, fg); | |
676 | break; | |
677 | } | |
678 | vs->tight.offset = bytes; | |
679 | ||
680 | bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY); | |
681 | return (bytes >= 0); | |
682 | } | |
683 | ||
684 | struct palette_cb_priv { | |
685 | VncState *vs; | |
686 | uint8_t *header; | |
687 | }; | |
688 | ||
689 | static void write_palette(const char *key, QObject *obj, void *opaque) | |
690 | { | |
691 | struct palette_cb_priv *priv = opaque; | |
692 | VncState *vs = priv->vs; | |
693 | uint32_t bytes = vs->clientds.pf.bytes_per_pixel; | |
694 | uint8_t idx = qint_get_int(qobject_to_qint(obj)); | |
695 | ||
696 | if (bytes == 4) { | |
697 | uint32_t color = tight_palette_buf2rgb(32, (uint8_t *)key); | |
698 | ||
699 | ((uint32_t*)priv->header)[idx] = color; | |
700 | } else { | |
701 | uint16_t color = tight_palette_buf2rgb(16, (uint8_t *)key); | |
702 | ||
703 | ((uint16_t*)priv->header)[idx] = color; | |
704 | } | |
705 | } | |
706 | ||
707 | static int send_palette_rect(VncState *vs, int w, int h, struct QDict *palette) | |
708 | { | |
709 | int stream = 2; | |
710 | int level = tight_conf[vs->tight_compression].idx_zlib_level; | |
711 | int colors; | |
712 | size_t bytes; | |
713 | ||
714 | colors = qdict_size(palette); | |
715 | ||
716 | vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4); | |
717 | vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE); | |
718 | vnc_write_u8(vs, colors - 1); | |
719 | ||
720 | switch(vs->clientds.pf.bytes_per_pixel) { | |
721 | case 4: | |
722 | { | |
723 | size_t old_offset, offset; | |
724 | uint32_t header[qdict_size(palette)]; | |
725 | struct palette_cb_priv priv = { vs, (uint8_t *)header }; | |
726 | ||
727 | old_offset = vs->output.offset; | |
728 | qdict_iter(palette, write_palette, &priv); | |
729 | vnc_write(vs, header, sizeof(header)); | |
730 | ||
731 | if (vs->tight_pixel24) { | |
732 | tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset); | |
733 | vs->output.offset = old_offset + offset; | |
734 | } | |
735 | ||
736 | tight_encode_indexed_rect32(vs->tight.buffer, w * h, palette); | |
737 | break; | |
738 | } | |
739 | case 2: | |
740 | { | |
741 | uint16_t header[qdict_size(palette)]; | |
742 | struct palette_cb_priv priv = { vs, (uint8_t *)header }; | |
743 | ||
744 | qdict_iter(palette, write_palette, &priv); | |
745 | vnc_write(vs, header, sizeof(header)); | |
746 | tight_encode_indexed_rect16(vs->tight.buffer, w * h, palette); | |
747 | break; | |
748 | } | |
749 | default: | |
750 | return -1; /* No palette for 8bits colors */ | |
751 | break; | |
752 | } | |
753 | bytes = w * h; | |
754 | vs->tight.offset = bytes; | |
755 | ||
756 | bytes = tight_compress_data(vs, stream, bytes, | |
757 | level, Z_DEFAULT_STRATEGY); | |
758 | return (bytes >= 0); | |
759 | } | |
760 | ||
380282b0 CC |
761 | static void vnc_tight_start(VncState *vs) |
762 | { | |
763 | buffer_reset(&vs->tight); | |
764 | ||
765 | // make the output buffer be the zlib buffer, so we can compress it later | |
766 | vs->tight_tmp = vs->output; | |
767 | vs->output = vs->tight; | |
768 | } | |
769 | ||
770 | static void vnc_tight_stop(VncState *vs) | |
771 | { | |
772 | // switch back to normal output/zlib buffers | |
773 | vs->tight = vs->output; | |
774 | vs->output = vs->tight_tmp; | |
775 | } | |
776 | ||
777 | static int send_sub_rect(VncState *vs, int x, int y, int w, int h) | |
778 | { | |
aa7d73fd CC |
779 | struct QDict *palette = NULL; |
780 | uint32_t bg = 0, fg = 0; | |
781 | int colors; | |
782 | int ret = 0; | |
783 | ||
380282b0 CC |
784 | vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT); |
785 | ||
380282b0 CC |
786 | vnc_tight_start(vs); |
787 | vnc_raw_send_framebuffer_update(vs, x, y, w, h); | |
788 | vnc_tight_stop(vs); | |
789 | ||
aa7d73fd CC |
790 | colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette); |
791 | ||
792 | if (colors == 0) { | |
793 | ret = send_full_color_rect(vs, w, h); | |
794 | } else if (colors == 1) { | |
795 | ret = send_solid_rect(vs); | |
796 | } else if (colors == 2) { | |
797 | ret = send_mono_rect(vs, w, h, bg, fg); | |
798 | } else if (colors <= 256) { | |
799 | ret = send_palette_rect(vs, w, h, palette); | |
800 | } | |
801 | QDECREF(palette); | |
802 | return ret; | |
380282b0 CC |
803 | } |
804 | ||
b4bea3f2 CC |
805 | static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h) |
806 | { | |
807 | vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT); | |
808 | ||
809 | vnc_tight_start(vs); | |
810 | vnc_raw_send_framebuffer_update(vs, x, y, w, h); | |
811 | vnc_tight_stop(vs); | |
812 | ||
813 | return send_solid_rect(vs); | |
814 | } | |
815 | ||
380282b0 CC |
816 | static int send_rect_simple(VncState *vs, int x, int y, int w, int h) |
817 | { | |
818 | int max_size, max_width; | |
819 | int max_sub_width, max_sub_height; | |
820 | int dx, dy; | |
821 | int rw, rh; | |
822 | int n = 0; | |
823 | ||
824 | max_size = tight_conf[vs->tight_compression].max_rect_size; | |
825 | max_width = tight_conf[vs->tight_compression].max_rect_width; | |
826 | ||
827 | if (w > max_width || w * h > max_size) { | |
828 | max_sub_width = (w > max_width) ? max_width : w; | |
829 | max_sub_height = max_size / max_sub_width; | |
830 | ||
831 | for (dy = 0; dy < h; dy += max_sub_height) { | |
832 | for (dx = 0; dx < w; dx += max_width) { | |
833 | rw = MIN(max_sub_width, w - dx); | |
834 | rh = MIN(max_sub_height, h - dy); | |
835 | n += send_sub_rect(vs, x+dx, y+dy, rw, rh); | |
836 | } | |
837 | } | |
838 | } else { | |
839 | n += send_sub_rect(vs, x, y, w, h); | |
840 | } | |
841 | ||
842 | return n; | |
843 | } | |
844 | ||
b4bea3f2 CC |
845 | static int find_large_solid_color_rect(VncState *vs, int x, int y, |
846 | int w, int h, int max_rows) | |
847 | { | |
848 | int dx, dy, dw, dh; | |
849 | int n = 0; | |
850 | ||
851 | /* Try to find large solid-color areas and send them separately. */ | |
852 | ||
853 | for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) { | |
854 | ||
855 | /* If a rectangle becomes too large, send its upper part now. */ | |
856 | ||
857 | if (dy - y >= max_rows) { | |
858 | n += send_rect_simple(vs, x, y, w, max_rows); | |
859 | y += max_rows; | |
860 | h -= max_rows; | |
861 | } | |
862 | ||
863 | dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy)); | |
864 | ||
865 | for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) { | |
866 | uint32_t color_value; | |
867 | int x_best, y_best, w_best, h_best; | |
868 | ||
869 | dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx)); | |
870 | ||
871 | if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) { | |
872 | continue ; | |
873 | } | |
874 | ||
875 | /* Get dimensions of solid-color area. */ | |
876 | ||
877 | find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y), | |
878 | color_value, &w_best, &h_best); | |
879 | ||
880 | /* Make sure a solid rectangle is large enough | |
881 | (or the whole rectangle is of the same color). */ | |
882 | ||
883 | if (w_best * h_best != w * h && | |
884 | w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) { | |
885 | continue; | |
886 | } | |
887 | ||
888 | /* Try to extend solid rectangle to maximum size. */ | |
889 | ||
890 | x_best = dx; y_best = dy; | |
891 | extend_solid_area(vs, x, y, w, h, color_value, | |
892 | &x_best, &y_best, &w_best, &h_best); | |
893 | ||
894 | /* Send rectangles at top and left to solid-color area. */ | |
895 | ||
896 | if (y_best != y) { | |
897 | n += send_rect_simple(vs, x, y, w, y_best-y); | |
898 | } | |
899 | if (x_best != x) { | |
900 | n += vnc_tight_send_framebuffer_update(vs, x, y_best, | |
901 | x_best-x, h_best); | |
902 | } | |
903 | ||
904 | /* Send solid-color rectangle. */ | |
905 | n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best); | |
906 | ||
907 | /* Send remaining rectangles (at right and bottom). */ | |
908 | ||
909 | if (x_best + w_best != x + w) { | |
910 | n += vnc_tight_send_framebuffer_update(vs, x_best+w_best, | |
911 | y_best, | |
912 | w-(x_best-x)-w_best, | |
913 | h_best); | |
914 | } | |
915 | if (y_best + h_best != y + h) { | |
916 | n += vnc_tight_send_framebuffer_update(vs, x, y_best+h_best, | |
917 | w, h-(y_best-y)-h_best); | |
918 | } | |
919 | ||
920 | /* Return after all recursive calls are done. */ | |
921 | return n; | |
922 | } | |
923 | } | |
924 | return n + send_rect_simple(vs, x, y, w, h); | |
925 | } | |
926 | ||
380282b0 CC |
927 | int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, |
928 | int w, int h) | |
929 | { | |
b4bea3f2 CC |
930 | int max_rows; |
931 | ||
380282b0 CC |
932 | if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF && |
933 | vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) { | |
934 | vs->tight_pixel24 = true; | |
935 | } else { | |
936 | vs->tight_pixel24 = false; | |
937 | } | |
938 | ||
b4bea3f2 CC |
939 | if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE) |
940 | return send_rect_simple(vs, x, y, w, h); | |
941 | ||
942 | /* Calculate maximum number of rows in one non-solid rectangle. */ | |
943 | ||
944 | max_rows = tight_conf[vs->tight_compression].max_rect_size; | |
945 | max_rows /= MIN(tight_conf[vs->tight_compression].max_rect_width, w); | |
946 | ||
947 | return find_large_solid_color_rect(vs, x, y, w, h, max_rows); | |
380282b0 CC |
948 | } |
949 | ||
950 | void vnc_tight_clear(VncState *vs) | |
951 | { | |
952 | int i; | |
953 | for (i=0; i<ARRAY_SIZE(vs->tight_stream); i++) { | |
954 | if (vs->tight_stream[i].opaque) { | |
955 | deflateEnd(&vs->tight_stream[i]); | |
956 | } | |
957 | } | |
958 | ||
959 | buffer_free(&vs->tight); | |
960 | buffer_free(&vs->tight_zlib); | |
961 | } |