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 "vnc.h"
  29. #include "vnc-enc-zrle.h"
  30. static const int bits_per_packed_pixel[] = {
  31. 0, 1, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
  32. };
  33. static void vnc_zrle_start(VncState *vs)
  34. {
  35. buffer_reset(&vs->zrle.zrle);
  36. /* make the output buffer be the zlib buffer, so we can compress it later */
  37. vs->zrle.tmp = vs->output;
  38. vs->output = vs->zrle.zrle;
  39. }
  40. static void vnc_zrle_stop(VncState *vs)
  41. {
  42. /* switch back to normal output/zlib buffers */
  43. vs->zrle.zrle = vs->output;
  44. vs->output = vs->zrle.tmp;
  45. }
  46. static void *zrle_convert_fb(VncState *vs, int x, int y, int w, int h,
  47. int bpp)
  48. {
  49. Buffer tmp;
  50. buffer_reset(&vs->zrle.fb);
  51. buffer_reserve(&vs->zrle.fb, w * h * bpp + bpp);
  52. tmp = vs->output;
  53. vs->output = vs->zrle.fb;
  54. vnc_raw_send_framebuffer_update(vs, x, y, w, h);
  55. vs->zrle.fb = vs->output;
  56. vs->output = tmp;
  57. return vs->zrle.fb.buffer;
  58. }
  59. static int zrle_compress_data(VncState *vs, int level)
  60. {
  61. z_streamp zstream = &vs->zrle.stream;
  62. buffer_reset(&vs->zrle.zlib);
  63. if (zstream->opaque != vs) {
  64. int err;
  65. zstream->zalloc = vnc_zlib_zalloc;
  66. zstream->zfree = vnc_zlib_zfree;
  67. err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
  68. MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  69. if (err != Z_OK) {
  70. fprintf(stderr, "VNC: error initializing zlib\n");
  71. return -1;
  72. }
  73. zstream->opaque = vs;
  74. }
  75. /* reserve memory in output buffer */
  76. buffer_reserve(&vs->zrle.zlib, vs->zrle.zrle.offset + 64);
  77. /* set pointers */
  78. zstream->next_in = vs->zrle.zrle.buffer;
  79. zstream->avail_in = vs->zrle.zrle.offset;
  80. zstream->next_out = vs->zrle.zlib.buffer + vs->zrle.zlib.offset;
  81. zstream->avail_out = vs->zrle.zlib.capacity - vs->zrle.zlib.offset;
  82. zstream->data_type = Z_BINARY;
  83. /* start encoding */
  84. if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
  85. fprintf(stderr, "VNC: error during zrle compression\n");
  86. return -1;
  87. }
  88. vs->zrle.zlib.offset = vs->zrle.zlib.capacity - zstream->avail_out;
  89. return vs->zrle.zlib.offset;
  90. }
  91. /* Try to work out whether to use RLE and/or a palette. We do this by
  92. * estimating the number of bytes which will be generated and picking the
  93. * method which results in the fewest bytes. Of course this may not result
  94. * in the fewest bytes after compression... */
  95. static void zrle_choose_palette_rle(VncState *vs, int w, int h,
  96. VncPalette *palette, int bpp_out,
  97. int runs, int single_pixels,
  98. int zywrle_level,
  99. bool *use_rle, bool *use_palette)
  100. {
  101. size_t estimated_bytes;
  102. size_t plain_rle_bytes;
  103. *use_palette = *use_rle = false;
  104. estimated_bytes = w * h * (bpp_out / 8); /* start assuming raw */
  105. if (bpp_out != 8) {
  106. if (zywrle_level > 0 && !(zywrle_level & 0x80))
  107. estimated_bytes >>= zywrle_level;
  108. }
  109. plain_rle_bytes = ((bpp_out / 8) + 1) * (runs + single_pixels);
  110. if (plain_rle_bytes < estimated_bytes) {
  111. *use_rle = true;
  112. estimated_bytes = plain_rle_bytes;
  113. }
  114. if (palette_size(palette) < 128) {
  115. int palette_rle_bytes;
  116. palette_rle_bytes = (bpp_out / 8) * palette_size(palette);
  117. palette_rle_bytes += 2 * runs + single_pixels;
  118. if (palette_rle_bytes < estimated_bytes) {
  119. *use_rle = true;
  120. *use_palette = true;
  121. estimated_bytes = palette_rle_bytes;
  122. }
  123. if (palette_size(palette) < 17) {
  124. int packed_bytes;
  125. packed_bytes = (bpp_out / 8) * palette_size(palette);
  126. packed_bytes += w * h *
  127. bits_per_packed_pixel[palette_size(palette)-1] / 8;
  128. if (packed_bytes < estimated_bytes) {
  129. *use_rle = false;
  130. *use_palette = true;
  131. estimated_bytes = packed_bytes;
  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-template.c"
  162. #undef ZRLE_BPP
  163. #define ZRLE_BPP 15
  164. #undef ZYWRLE_ENDIAN
  165. #define ZYWRLE_ENDIAN ENDIAN_LITTLE
  166. #include "vnc-enc-zrle-template.c"
  167. #undef ZYWRLE_ENDIAN
  168. #define ZYWRLE_ENDIAN ENDIAN_BIG
  169. #include "vnc-enc-zrle-template.c"
  170. #undef ZRLE_BPP
  171. #define ZRLE_BPP 16
  172. #undef ZYWRLE_ENDIAN
  173. #define ZYWRLE_ENDIAN ENDIAN_LITTLE
  174. #include "vnc-enc-zrle-template.c"
  175. #undef ZYWRLE_ENDIAN
  176. #define ZYWRLE_ENDIAN ENDIAN_BIG
  177. #include "vnc-enc-zrle-template.c"
  178. #undef ZRLE_BPP
  179. #define ZRLE_BPP 32
  180. #undef ZYWRLE_ENDIAN
  181. #define ZYWRLE_ENDIAN ENDIAN_LITTLE
  182. #include "vnc-enc-zrle-template.c"
  183. #undef ZYWRLE_ENDIAN
  184. #define ZYWRLE_ENDIAN ENDIAN_BIG
  185. #include "vnc-enc-zrle-template.c"
  186. #define ZRLE_COMPACT_PIXEL 24a
  187. #undef ZYWRLE_ENDIAN
  188. #define ZYWRLE_ENDIAN ENDIAN_LITTLE
  189. #include "vnc-enc-zrle-template.c"
  190. #undef ZYWRLE_ENDIAN
  191. #define ZYWRLE_ENDIAN ENDIAN_BIG
  192. #include "vnc-enc-zrle-template.c"
  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-template.c"
  198. #undef ZYWRLE_ENDIAN
  199. #define ZYWRLE_ENDIAN ENDIAN_BIG
  200. #include "vnc-enc-zrle-template.c"
  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. }