You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TightDecoder.cxx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
  2. * Copyright 2004-2005 Cendio AB.
  3. * Copyright 2009-2015 Pierre Ossman for Cendio AB
  4. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This software is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this software; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. * USA.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <assert.h>
  25. #include <rdr/InStream.h>
  26. #include <rdr/MemInStream.h>
  27. #include <rdr/OutStream.h>
  28. #include <rfb/ServerParams.h>
  29. #include <rfb/Exception.h>
  30. #include <rfb/PixelBuffer.h>
  31. #include <rfb/TightConstants.h>
  32. #include <rfb/TightDecoder.h>
  33. using namespace rfb;
  34. static const int TIGHT_MAX_WIDTH = 2048;
  35. static const int TIGHT_MIN_TO_COMPRESS = 12;
  36. #define BPP 8
  37. #include <rfb/tightDecode.h>
  38. #undef BPP
  39. #define BPP 16
  40. #include <rfb/tightDecode.h>
  41. #undef BPP
  42. #define BPP 32
  43. #include <rfb/tightDecode.h>
  44. #undef BPP
  45. TightDecoder::TightDecoder() : Decoder(DecoderPartiallyOrdered)
  46. {
  47. }
  48. TightDecoder::~TightDecoder()
  49. {
  50. }
  51. bool TightDecoder::readRect(const Rect& r, rdr::InStream* is,
  52. const ServerParams& server, rdr::OutStream* os)
  53. {
  54. rdr::U8 comp_ctl;
  55. if (!is->hasData(1))
  56. return false;
  57. is->setRestorePoint();
  58. comp_ctl = is->readU8();
  59. os->writeU8(comp_ctl);
  60. comp_ctl >>= 4;
  61. // "Fill" compression type.
  62. if (comp_ctl == tightFill) {
  63. if (server.pf().is888()) {
  64. if (!is->hasDataOrRestore(3))
  65. return false;
  66. os->copyBytes(is, 3);
  67. } else {
  68. if (!is->hasDataOrRestore(server.pf().bpp/8))
  69. return false;
  70. os->copyBytes(is, server.pf().bpp/8);
  71. }
  72. is->clearRestorePoint();
  73. return true;
  74. }
  75. // "JPEG" compression type.
  76. if (comp_ctl == tightJpeg) {
  77. rdr::U32 len;
  78. // FIXME: Might be less than 3 bytes
  79. if (!is->hasDataOrRestore(3))
  80. return false;
  81. len = readCompact(is);
  82. os->writeOpaque32(len);
  83. if (!is->hasDataOrRestore(len))
  84. return false;
  85. os->copyBytes(is, len);
  86. is->clearRestorePoint();
  87. return true;
  88. }
  89. // Quit on unsupported compression type.
  90. if (comp_ctl > tightMaxSubencoding)
  91. throw Exception("TightDecoder: bad subencoding value received");
  92. // "Basic" compression type.
  93. int palSize = 0;
  94. if (r.width() > TIGHT_MAX_WIDTH)
  95. throw Exception("TightDecoder: too large rectangle (%d pixels)", r.width());
  96. // Possible palette
  97. if ((comp_ctl & tightExplicitFilter) != 0) {
  98. rdr::U8 filterId;
  99. if (!is->hasDataOrRestore(1))
  100. return false;
  101. filterId = is->readU8();
  102. os->writeU8(filterId);
  103. switch (filterId) {
  104. case tightFilterPalette:
  105. if (!is->hasDataOrRestore(1))
  106. return false;
  107. palSize = is->readU8() + 1;
  108. os->writeU8(palSize - 1);
  109. if (server.pf().is888()) {
  110. if (!is->hasDataOrRestore(palSize * 3))
  111. return false;
  112. os->copyBytes(is, palSize * 3);
  113. } else {
  114. if (!is->hasDataOrRestore(palSize * server.pf().bpp/8))
  115. return false;
  116. os->copyBytes(is, palSize * server.pf().bpp/8);
  117. }
  118. break;
  119. case tightFilterGradient:
  120. if (server.pf().bpp == 8)
  121. throw Exception("TightDecoder: invalid BPP for gradient filter");
  122. break;
  123. case tightFilterCopy:
  124. break;
  125. default:
  126. throw Exception("TightDecoder: unknown filter code received");
  127. }
  128. }
  129. size_t rowSize, dataSize;
  130. if (palSize != 0) {
  131. if (palSize <= 2)
  132. rowSize = (r.width() + 7) / 8;
  133. else
  134. rowSize = r.width();
  135. } else if (server.pf().is888()) {
  136. rowSize = r.width() * 3;
  137. } else {
  138. rowSize = r.width() * server.pf().bpp/8;
  139. }
  140. dataSize = r.height() * rowSize;
  141. if (dataSize < TIGHT_MIN_TO_COMPRESS) {
  142. if (!is->hasDataOrRestore(dataSize))
  143. return false;
  144. os->copyBytes(is, dataSize);
  145. } else {
  146. rdr::U32 len;
  147. // FIXME: Might be less than 3 bytes
  148. if (!is->hasDataOrRestore(3))
  149. return false;
  150. len = readCompact(is);
  151. os->writeOpaque32(len);
  152. if (!is->hasDataOrRestore(len))
  153. return false;
  154. os->copyBytes(is, len);
  155. }
  156. is->clearRestorePoint();
  157. return true;
  158. }
  159. bool TightDecoder::doRectsConflict(const Rect& /*rectA*/,
  160. const void* bufferA,
  161. size_t buflenA,
  162. const Rect& /*rectB*/,
  163. const void* bufferB,
  164. size_t buflenB,
  165. const ServerParams& /*server*/)
  166. {
  167. rdr::U8 comp_ctl_a, comp_ctl_b;
  168. assert(buflenA >= 1);
  169. assert(buflenB >= 1);
  170. comp_ctl_a = *(const rdr::U8*)bufferA;
  171. comp_ctl_b = *(const rdr::U8*)bufferB;
  172. // Resets or use of zlib pose the same problem, so merge them
  173. if ((comp_ctl_a & 0x80) == 0x00)
  174. comp_ctl_a |= 1 << ((comp_ctl_a >> 4) & 0x03);
  175. if ((comp_ctl_b & 0x80) == 0x00)
  176. comp_ctl_b |= 1 << ((comp_ctl_b >> 4) & 0x03);
  177. if (((comp_ctl_a & 0x0f) & (comp_ctl_b & 0x0f)) != 0)
  178. return true;
  179. return false;
  180. }
  181. void TightDecoder::decodeRect(const Rect& r, const void* buffer,
  182. size_t buflen, const ServerParams& server,
  183. ModifiablePixelBuffer* pb)
  184. {
  185. const rdr::U8* bufptr;
  186. const PixelFormat& pf = server.pf();
  187. rdr::U8 comp_ctl;
  188. bufptr = (const rdr::U8*)buffer;
  189. assert(buflen >= 1);
  190. comp_ctl = *bufptr;
  191. bufptr += 1;
  192. buflen -= 1;
  193. // Reset zlib streams if we are told by the server to do so.
  194. for (int i = 0; i < 4; i++) {
  195. if (comp_ctl & 1) {
  196. zis[i].reset();
  197. }
  198. comp_ctl >>= 1;
  199. }
  200. // "Fill" compression type.
  201. if (comp_ctl == tightFill) {
  202. if (pf.is888()) {
  203. rdr::U8 pix[4];
  204. assert(buflen >= 3);
  205. pf.bufferFromRGB(pix, bufptr, 1);
  206. pb->fillRect(pf, r, pix);
  207. } else {
  208. assert(buflen >= (size_t)pf.bpp/8);
  209. pb->fillRect(pf, r, bufptr);
  210. }
  211. return;
  212. }
  213. // "JPEG" compression type.
  214. if (comp_ctl == tightJpeg) {
  215. rdr::U32 len;
  216. int stride;
  217. rdr::U8 *buf;
  218. JpegDecompressor jd;
  219. assert(buflen >= 4);
  220. memcpy(&len, bufptr, 4);
  221. bufptr += 4;
  222. buflen -= 4;
  223. // We always use direct decoding with JPEG images
  224. buf = pb->getBufferRW(r, &stride);
  225. jd.decompress(bufptr, len, buf, stride, r, pb->getPF());
  226. pb->commitBufferRW(r);
  227. return;
  228. }
  229. // Quit on unsupported compression type.
  230. assert(comp_ctl <= tightMaxSubencoding);
  231. // "Basic" compression type.
  232. int palSize = 0;
  233. rdr::U8 palette[256 * 4];
  234. bool useGradient = false;
  235. if ((comp_ctl & tightExplicitFilter) != 0) {
  236. rdr::U8 filterId;
  237. assert(buflen >= 1);
  238. filterId = *bufptr;
  239. bufptr += 1;
  240. buflen -= 1;
  241. switch (filterId) {
  242. case tightFilterPalette:
  243. assert(buflen >= 1);
  244. palSize = *bufptr + 1;
  245. bufptr += 1;
  246. buflen -= 1;
  247. if (pf.is888()) {
  248. size_t len = palSize * 3;
  249. rdr::U8Array tightPalette(len);
  250. assert(buflen >= len);
  251. memcpy(tightPalette.buf, bufptr, len);
  252. bufptr += len;
  253. buflen -= len;
  254. pf.bufferFromRGB(palette, tightPalette.buf, palSize);
  255. } else {
  256. size_t len;
  257. len = palSize * pf.bpp/8;
  258. assert(buflen >= len);
  259. memcpy(palette, bufptr, len);
  260. bufptr += len;
  261. buflen -= len;
  262. }
  263. break;
  264. case tightFilterGradient:
  265. useGradient = true;
  266. break;
  267. case tightFilterCopy:
  268. break;
  269. default:
  270. assert(false);
  271. }
  272. }
  273. // Determine if the data should be decompressed or just copied.
  274. size_t rowSize, dataSize;
  275. rdr::U8* netbuf;
  276. netbuf = NULL;
  277. if (palSize != 0) {
  278. if (palSize <= 2)
  279. rowSize = (r.width() + 7) / 8;
  280. else
  281. rowSize = r.width();
  282. } else if (pf.is888()) {
  283. rowSize = r.width() * 3;
  284. } else {
  285. rowSize = r.width() * pf.bpp/8;
  286. }
  287. dataSize = r.height() * rowSize;
  288. if (dataSize < TIGHT_MIN_TO_COMPRESS)
  289. assert(buflen >= dataSize);
  290. else {
  291. rdr::U32 len;
  292. int streamId;
  293. rdr::MemInStream* ms;
  294. assert(buflen >= 4);
  295. memcpy(&len, bufptr, 4);
  296. bufptr += 4;
  297. buflen -= 4;
  298. assert(buflen >= len);
  299. streamId = comp_ctl & 0x03;
  300. ms = new rdr::MemInStream(bufptr, len);
  301. zis[streamId].setUnderlying(ms, len);
  302. // Allocate buffer and decompress the data
  303. netbuf = new rdr::U8[dataSize];
  304. if (!zis[streamId].hasData(dataSize))
  305. throw Exception("Tight decode error");
  306. zis[streamId].readBytes(netbuf, dataSize);
  307. zis[streamId].flushUnderlying();
  308. zis[streamId].setUnderlying(NULL, 0);
  309. delete ms;
  310. bufptr = netbuf;
  311. buflen = dataSize;
  312. }
  313. // Time to decode the actual data
  314. bool directDecode;
  315. rdr::U8* outbuf;
  316. int stride;
  317. if (pb->getPF().equal(pf)) {
  318. // Decode directly into the framebuffer (fast path)
  319. directDecode = true;
  320. } else {
  321. // Decode into an intermediate buffer and use pixel translation
  322. directDecode = false;
  323. }
  324. if (directDecode)
  325. outbuf = pb->getBufferRW(r, &stride);
  326. else {
  327. outbuf = new rdr::U8[r.area() * (pf.bpp/8)];
  328. stride = r.width();
  329. }
  330. if (palSize == 0) {
  331. // Truecolor data
  332. if (useGradient) {
  333. if (pf.is888())
  334. FilterGradient24(bufptr, pf, (rdr::U32*)outbuf, stride, r);
  335. else {
  336. switch (pf.bpp) {
  337. case 8:
  338. assert(false);
  339. break;
  340. case 16:
  341. FilterGradient(bufptr, pf, (rdr::U16*)outbuf, stride, r);
  342. break;
  343. case 32:
  344. FilterGradient(bufptr, pf, (rdr::U32*)outbuf, stride, r);
  345. break;
  346. }
  347. }
  348. } else {
  349. // Copy
  350. rdr::U8* ptr = outbuf;
  351. const rdr::U8* srcPtr = bufptr;
  352. int w = r.width();
  353. int h = r.height();
  354. if (pf.is888()) {
  355. while (h > 0) {
  356. pf.bufferFromRGB(ptr, srcPtr, w);
  357. ptr += stride * pf.bpp/8;
  358. srcPtr += w * 3;
  359. h--;
  360. }
  361. } else {
  362. while (h > 0) {
  363. memcpy(ptr, srcPtr, w * pf.bpp/8);
  364. ptr += stride * pf.bpp/8;
  365. srcPtr += w * pf.bpp/8;
  366. h--;
  367. }
  368. }
  369. }
  370. } else {
  371. // Indexed color
  372. switch (pf.bpp) {
  373. case 8:
  374. FilterPalette((const rdr::U8*)palette, palSize,
  375. bufptr, (rdr::U8*)outbuf, stride, r);
  376. break;
  377. case 16:
  378. FilterPalette((const rdr::U16*)palette, palSize,
  379. bufptr, (rdr::U16*)outbuf, stride, r);
  380. break;
  381. case 32:
  382. FilterPalette((const rdr::U32*)palette, palSize,
  383. bufptr, (rdr::U32*)outbuf, stride, r);
  384. break;
  385. }
  386. }
  387. if (directDecode)
  388. pb->commitBufferRW(r);
  389. else {
  390. pb->imageRect(pf, r, outbuf);
  391. delete [] outbuf;
  392. }
  393. delete [] netbuf;
  394. }
  395. rdr::U32 TightDecoder::readCompact(rdr::InStream* is)
  396. {
  397. rdr::U8 b;
  398. rdr::U32 result;
  399. b = is->readU8();
  400. result = (int)b & 0x7F;
  401. if (b & 0x80) {
  402. b = is->readU8();
  403. result |= ((int)b & 0x7F) << 7;
  404. if (b & 0x80) {
  405. b = is->readU8();
  406. result |= ((int)b & 0xFF) << 14;
  407. }
  408. }
  409. return result;
  410. }