vnc-enc-tight.c 62 KB

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