2
0

vnc-enc-zrle.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * QEMU VNC display driver: Zlib Run-length Encoding (ZRLE)
  3. *
  4. * From libvncserver/libvncserver/zrle.c
  5. * Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
  6. * Copyright (C) 2003 Sun Microsystems, Inc.
  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. #include "qemu/osdep.h"
  29. #include "vnc.h"
  30. #include "vnc-enc-zrle.h"
  31. static const int bits_per_packed_pixel[] = {
  32. 0, 1, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
  33. };
  34. static void vnc_zrle_start(VncState *vs)
  35. {
  36. buffer_reset(&vs->zrle->zrle);
  37. /* make the output buffer be the zlib buffer, so we can compress it later */
  38. vs->zrle->tmp = vs->output;
  39. vs->output = vs->zrle->zrle;
  40. }
  41. static void vnc_zrle_stop(VncState *vs)
  42. {
  43. /* switch back to normal output/zlib buffers */
  44. vs->zrle->zrle = vs->output;
  45. vs->output = vs->zrle->tmp;
  46. }
  47. static void *zrle_convert_fb(VncState *vs, int x, int y, int w, int h,
  48. int bpp)
  49. {
  50. Buffer tmp;
  51. buffer_reset(&vs->zrle->fb);
  52. buffer_reserve(&vs->zrle->fb, w * h * bpp + bpp);
  53. tmp = vs->output;
  54. vs->output = vs->zrle->fb;
  55. vnc_raw_send_framebuffer_update(vs, x, y, w, h);
  56. vs->zrle->fb = vs->output;
  57. vs->output = tmp;
  58. return vs->zrle->fb.buffer;
  59. }
  60. static int zrle_compress_data(VncState *vs, int level)
  61. {
  62. z_streamp zstream = &vs->zrle->stream;
  63. buffer_reset(&vs->zrle->zlib);
  64. if (zstream->opaque != vs) {
  65. int err;
  66. zstream->zalloc = vnc_zlib_zalloc;
  67. zstream->zfree = vnc_zlib_zfree;
  68. err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
  69. MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  70. if (err != Z_OK) {
  71. fprintf(stderr, "VNC: error initializing zlib\n");
  72. return -1;
  73. }
  74. zstream->opaque = vs;
  75. }
  76. /* reserve memory in output buffer */
  77. buffer_reserve(&vs->zrle->zlib, vs->zrle->zrle.offset + 64);
  78. /* set pointers */
  79. zstream->next_in = vs->zrle->zrle.buffer;
  80. zstream->avail_in = vs->zrle->zrle.offset;
  81. zstream->next_out = vs->zrle->zlib.buffer;
  82. zstream->avail_out = vs->zrle->zlib.capacity;
  83. zstream->data_type = Z_BINARY;
  84. /* start encoding */
  85. if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
  86. fprintf(stderr, "VNC: error during zrle compression\n");
  87. return -1;
  88. }
  89. vs->zrle->zlib.offset = vs->zrle->zlib.capacity - zstream->avail_out;
  90. return vs->zrle->zlib.offset;
  91. }
  92. /* Try to work out whether to use RLE and/or a palette. We do this by
  93. * estimating the number of bytes which will be generated and picking the
  94. * method which results in the fewest bytes. Of course this may not result
  95. * in the fewest bytes after compression... */
  96. static void zrle_choose_palette_rle(VncState *vs, int w, int h,
  97. VncPalette *palette, int bpp_out,
  98. int runs, int single_pixels,
  99. int zywrle_level,
  100. bool *use_rle, bool *use_palette)
  101. {
  102. size_t estimated_bytes;
  103. size_t plain_rle_bytes;
  104. *use_palette = *use_rle = false;
  105. estimated_bytes = w * h * (bpp_out / 8); /* start assuming raw */
  106. if (bpp_out != 8) {
  107. if (zywrle_level > 0 && !(zywrle_level & 0x80))
  108. estimated_bytes >>= zywrle_level;
  109. }
  110. plain_rle_bytes = ((bpp_out / 8) + 1) * (runs + single_pixels);
  111. if (plain_rle_bytes < estimated_bytes) {
  112. *use_rle = true;
  113. estimated_bytes = plain_rle_bytes;
  114. }
  115. if (palette_size(palette) < 128) {
  116. int palette_rle_bytes;
  117. palette_rle_bytes = (bpp_out / 8) * palette_size(palette);
  118. palette_rle_bytes += 2 * runs + single_pixels;
  119. if (palette_rle_bytes < estimated_bytes) {
  120. *use_rle = true;
  121. *use_palette = true;
  122. estimated_bytes = palette_rle_bytes;
  123. }
  124. if (palette_size(palette) < 17) {
  125. int packed_bytes;
  126. packed_bytes = (bpp_out / 8) * palette_size(palette);
  127. packed_bytes += w * h *
  128. bits_per_packed_pixel[palette_size(palette)-1] / 8;
  129. if (packed_bytes < estimated_bytes) {
  130. *use_rle = false;
  131. *use_palette = true;
  132. }
  133. }
  134. }
  135. }
  136. static void zrle_write_u32(VncState *vs, uint32_t value)
  137. {
  138. vnc_write(vs, (uint8_t *)&value, 4);
  139. }
  140. static void zrle_write_u24a(VncState *vs, uint32_t value)
  141. {
  142. vnc_write(vs, (uint8_t *)&value, 3);
  143. }
  144. static void zrle_write_u24b(VncState *vs, uint32_t value)
  145. {
  146. vnc_write(vs, ((uint8_t *)&value) + 1, 3);
  147. }
  148. static void zrle_write_u16(VncState *vs, uint16_t value)
  149. {
  150. vnc_write(vs, (uint8_t *)&value, 2);
  151. }
  152. static void zrle_write_u8(VncState *vs, uint8_t value)
  153. {
  154. vnc_write_u8(vs, value);
  155. }
  156. #define ENDIAN_LITTLE 0
  157. #define ENDIAN_BIG 1
  158. #define ENDIAN_NO 2
  159. #define ZRLE_BPP 8
  160. #define ZYWRLE_ENDIAN ENDIAN_NO
  161. #include "vnc-enc-zrle.c.inc"
  162. #undef ZRLE_BPP
  163. #define ZRLE_BPP 15
  164. #undef ZYWRLE_ENDIAN
  165. #define ZYWRLE_ENDIAN ENDIAN_LITTLE
  166. #include "vnc-enc-zrle.c.inc"
  167. #undef ZYWRLE_ENDIAN
  168. #define ZYWRLE_ENDIAN ENDIAN_BIG
  169. #include "vnc-enc-zrle.c.inc"
  170. #undef ZRLE_BPP
  171. #define ZRLE_BPP 16
  172. #undef ZYWRLE_ENDIAN
  173. #define ZYWRLE_ENDIAN ENDIAN_LITTLE
  174. #include "vnc-enc-zrle.c.inc"
  175. #undef ZYWRLE_ENDIAN
  176. #define ZYWRLE_ENDIAN ENDIAN_BIG
  177. #include "vnc-enc-zrle.c.inc"
  178. #undef ZRLE_BPP
  179. #define ZRLE_BPP 32
  180. #undef ZYWRLE_ENDIAN
  181. #define ZYWRLE_ENDIAN ENDIAN_LITTLE
  182. #include "vnc-enc-zrle.c.inc"
  183. #undef ZYWRLE_ENDIAN
  184. #define ZYWRLE_ENDIAN ENDIAN_BIG
  185. #include "vnc-enc-zrle.c.inc"
  186. #define ZRLE_COMPACT_PIXEL 24a
  187. #undef ZYWRLE_ENDIAN
  188. #define ZYWRLE_ENDIAN ENDIAN_LITTLE
  189. #include "vnc-enc-zrle.c.inc"
  190. #undef ZYWRLE_ENDIAN
  191. #define ZYWRLE_ENDIAN ENDIAN_BIG
  192. #include "vnc-enc-zrle.c.inc"
  193. #undef ZRLE_COMPACT_PIXEL
  194. #define ZRLE_COMPACT_PIXEL 24b
  195. #undef ZYWRLE_ENDIAN
  196. #define ZYWRLE_ENDIAN ENDIAN_LITTLE
  197. #include "vnc-enc-zrle.c.inc"
  198. #undef ZYWRLE_ENDIAN
  199. #define ZYWRLE_ENDIAN ENDIAN_BIG
  200. #include "vnc-enc-zrle.c.inc"
  201. #undef ZRLE_COMPACT_PIXEL
  202. #undef ZRLE_BPP
  203. static int zrle_send_framebuffer_update(VncState *vs, int x, int y,
  204. int w, int h)
  205. {
  206. bool be = vs->client_be;
  207. size_t bytes;
  208. int zywrle_level;
  209. if (vs->zrle->type == VNC_ENCODING_ZYWRLE) {
  210. if (!vs->vd->lossy || vs->tight->quality == (uint8_t)-1
  211. || vs->tight->quality == 9) {
  212. zywrle_level = 0;
  213. vs->zrle->type = VNC_ENCODING_ZRLE;
  214. } else if (vs->tight->quality < 3) {
  215. zywrle_level = 3;
  216. } else if (vs->tight->quality < 6) {
  217. zywrle_level = 2;
  218. } else {
  219. zywrle_level = 1;
  220. }
  221. } else {
  222. zywrle_level = 0;
  223. }
  224. vnc_zrle_start(vs);
  225. switch (vs->client_pf.bytes_per_pixel) {
  226. case 1:
  227. zrle_encode_8ne(vs, x, y, w, h, zywrle_level);
  228. break;
  229. case 2:
  230. if (vs->client_pf.gmax > 0x1F) {
  231. if (be) {
  232. zrle_encode_16be(vs, x, y, w, h, zywrle_level);
  233. } else {
  234. zrle_encode_16le(vs, x, y, w, h, zywrle_level);
  235. }
  236. } else {
  237. if (be) {
  238. zrle_encode_15be(vs, x, y, w, h, zywrle_level);
  239. } else {
  240. zrle_encode_15le(vs, x, y, w, h, zywrle_level);
  241. }
  242. }
  243. break;
  244. case 4:
  245. {
  246. bool fits_in_ls3bytes;
  247. bool fits_in_ms3bytes;
  248. fits_in_ls3bytes =
  249. ((vs->client_pf.rmax << vs->client_pf.rshift) < (1 << 24) &&
  250. (vs->client_pf.gmax << vs->client_pf.gshift) < (1 << 24) &&
  251. (vs->client_pf.bmax << vs->client_pf.bshift) < (1 << 24));
  252. fits_in_ms3bytes = (vs->client_pf.rshift > 7 &&
  253. vs->client_pf.gshift > 7 &&
  254. vs->client_pf.bshift > 7);
  255. if ((fits_in_ls3bytes && !be) || (fits_in_ms3bytes && be)) {
  256. if (be) {
  257. zrle_encode_24abe(vs, x, y, w, h, zywrle_level);
  258. } else {
  259. zrle_encode_24ale(vs, x, y, w, h, zywrle_level);
  260. }
  261. } else if ((fits_in_ls3bytes && be) || (fits_in_ms3bytes && !be)) {
  262. if (be) {
  263. zrle_encode_24bbe(vs, x, y, w, h, zywrle_level);
  264. } else {
  265. zrle_encode_24ble(vs, x, y, w, h, zywrle_level);
  266. }
  267. } else {
  268. if (be) {
  269. zrle_encode_32be(vs, x, y, w, h, zywrle_level);
  270. } else {
  271. zrle_encode_32le(vs, x, y, w, h, zywrle_level);
  272. }
  273. }
  274. }
  275. break;
  276. }
  277. vnc_zrle_stop(vs);
  278. bytes = zrle_compress_data(vs, Z_DEFAULT_COMPRESSION);
  279. vnc_framebuffer_update(vs, x, y, w, h, vs->zrle->type);
  280. vnc_write_u32(vs, bytes);
  281. vnc_write(vs, vs->zrle->zlib.buffer, vs->zrle->zlib.offset);
  282. return 1;
  283. }
  284. int vnc_zrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
  285. {
  286. vs->zrle->type = VNC_ENCODING_ZRLE;
  287. return zrle_send_framebuffer_update(vs, x, y, w, h);
  288. }
  289. int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
  290. {
  291. vs->zrle->type = VNC_ENCODING_ZYWRLE;
  292. return zrle_send_framebuffer_update(vs, x, y, w, h);
  293. }
  294. void vnc_zrle_clear(VncState *vs)
  295. {
  296. if (vs->zrle->stream.opaque) {
  297. deflateEnd(&vs->zrle->stream);
  298. }
  299. buffer_free(&vs->zrle->zrle);
  300. buffer_free(&vs->zrle->fb);
  301. buffer_free(&vs->zrle->zlib);
  302. }