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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
  2. * Copyright 2004-2005 Cendio AB.
  3. * Copyright 2009-2022 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 <vector>
  26. #include <rdr/InStream.h>
  27. #include <rdr/MemInStream.h>
  28. #include <rdr/OutStream.h>
  29. #include <rfb/ServerParams.h>
  30. #include <rfb/Exception.h>
  31. #include <rfb/PixelBuffer.h>
  32. #include <rfb/TightConstants.h>
  33. #include <rfb/TightDecoder.h>
  34. using namespace rfb;
  35. static const int TIGHT_MAX_WIDTH = 2048;
  36. static const int TIGHT_MIN_TO_COMPRESS = 12;
  37. TightDecoder::TightDecoder() : Decoder(DecoderPartiallyOrdered)
  38. {
  39. }
  40. TightDecoder::~TightDecoder()
  41. {
  42. }
  43. bool TightDecoder::readRect(const Rect& r, rdr::InStream* is,
  44. const ServerParams& server, rdr::OutStream* os)
  45. {
  46. uint8_t comp_ctl;
  47. if (!is->hasData(1))
  48. return false;
  49. is->setRestorePoint();
  50. comp_ctl = is->readU8();
  51. os->writeU8(comp_ctl);
  52. comp_ctl >>= 4;
  53. // "Fill" compression type.
  54. if (comp_ctl == tightFill) {
  55. if (server.pf().is888()) {
  56. if (!is->hasDataOrRestore(3))
  57. return false;
  58. os->copyBytes(is, 3);
  59. } else {
  60. if (!is->hasDataOrRestore(server.pf().bpp/8))
  61. return false;
  62. os->copyBytes(is, server.pf().bpp/8);
  63. }
  64. is->clearRestorePoint();
  65. return true;
  66. }
  67. // "JPEG" compression type.
  68. if (comp_ctl == tightJpeg) {
  69. uint32_t len;
  70. // FIXME: Might be less than 3 bytes
  71. if (!is->hasDataOrRestore(3))
  72. return false;
  73. len = readCompact(is);
  74. os->writeOpaque32(len);
  75. if (!is->hasDataOrRestore(len))
  76. return false;
  77. os->copyBytes(is, len);
  78. is->clearRestorePoint();
  79. return true;
  80. }
  81. // Quit on unsupported compression type.
  82. if (comp_ctl > tightMaxSubencoding)
  83. throw Exception("TightDecoder: bad subencoding value received");
  84. // "Basic" compression type.
  85. int palSize = 0;
  86. if (r.width() > TIGHT_MAX_WIDTH)
  87. throw Exception("TightDecoder: too large rectangle (%d pixels)", r.width());
  88. // Possible palette
  89. if ((comp_ctl & tightExplicitFilter) != 0) {
  90. uint8_t filterId;
  91. if (!is->hasDataOrRestore(1))
  92. return false;
  93. filterId = is->readU8();
  94. os->writeU8(filterId);
  95. switch (filterId) {
  96. case tightFilterPalette:
  97. if (!is->hasDataOrRestore(1))
  98. return false;
  99. palSize = is->readU8() + 1;
  100. os->writeU8(palSize - 1);
  101. if (server.pf().is888()) {
  102. if (!is->hasDataOrRestore(palSize * 3))
  103. return false;
  104. os->copyBytes(is, palSize * 3);
  105. } else {
  106. if (!is->hasDataOrRestore(palSize * server.pf().bpp/8))
  107. return false;
  108. os->copyBytes(is, palSize * server.pf().bpp/8);
  109. }
  110. break;
  111. case tightFilterGradient:
  112. if (server.pf().bpp == 8)
  113. throw Exception("TightDecoder: invalid BPP for gradient filter");
  114. break;
  115. case tightFilterCopy:
  116. break;
  117. default:
  118. throw Exception("TightDecoder: unknown filter code received");
  119. }
  120. }
  121. size_t rowSize, dataSize;
  122. if (palSize != 0) {
  123. if (palSize <= 2)
  124. rowSize = (r.width() + 7) / 8;
  125. else
  126. rowSize = r.width();
  127. } else if (server.pf().is888()) {
  128. rowSize = r.width() * 3;
  129. } else {
  130. rowSize = r.width() * server.pf().bpp/8;
  131. }
  132. dataSize = r.height() * rowSize;
  133. if (dataSize < TIGHT_MIN_TO_COMPRESS) {
  134. if (!is->hasDataOrRestore(dataSize))
  135. return false;
  136. os->copyBytes(is, dataSize);
  137. } else {
  138. uint32_t len;
  139. // FIXME: Might be less than 3 bytes
  140. if (!is->hasDataOrRestore(3))
  141. return false;
  142. len = readCompact(is);
  143. os->writeOpaque32(len);
  144. if (!is->hasDataOrRestore(len))
  145. return false;
  146. os->copyBytes(is, len);
  147. }
  148. is->clearRestorePoint();
  149. return true;
  150. }
  151. bool TightDecoder::doRectsConflict(const Rect& /*rectA*/,
  152. const uint8_t* bufferA,
  153. size_t buflenA,
  154. const Rect& /*rectB*/,
  155. const uint8_t* bufferB,
  156. size_t buflenB,
  157. const ServerParams& /*server*/)
  158. {
  159. uint8_t comp_ctl_a, comp_ctl_b;
  160. assert(buflenA >= 1);
  161. assert(buflenB >= 1);
  162. comp_ctl_a = *(const uint8_t*)bufferA;
  163. comp_ctl_b = *(const uint8_t*)bufferB;
  164. // Resets or use of zlib pose the same problem, so merge them
  165. if ((comp_ctl_a & 0x80) == 0x00)
  166. comp_ctl_a |= 1 << ((comp_ctl_a >> 4) & 0x03);
  167. if ((comp_ctl_b & 0x80) == 0x00)
  168. comp_ctl_b |= 1 << ((comp_ctl_b >> 4) & 0x03);
  169. if (((comp_ctl_a & 0x0f) & (comp_ctl_b & 0x0f)) != 0)
  170. return true;
  171. return false;
  172. }
  173. void TightDecoder::decodeRect(const Rect& r, const uint8_t* buffer,
  174. size_t buflen, const ServerParams& server,
  175. ModifiablePixelBuffer* pb)
  176. {
  177. const uint8_t* bufptr;
  178. const PixelFormat& pf = server.pf();
  179. uint8_t comp_ctl;
  180. bufptr = (const uint8_t*)buffer;
  181. assert(buflen >= 1);
  182. comp_ctl = *bufptr;
  183. bufptr += 1;
  184. buflen -= 1;
  185. // Reset zlib streams if we are told by the server to do so.
  186. for (int i = 0; i < 4; i++) {
  187. if (comp_ctl & 1) {
  188. zis[i].reset();
  189. }
  190. comp_ctl >>= 1;
  191. }
  192. // "Fill" compression type.
  193. if (comp_ctl == tightFill) {
  194. if (pf.is888()) {
  195. uint8_t pix[4];
  196. assert(buflen >= 3);
  197. pf.bufferFromRGB(pix, bufptr, 1);
  198. pb->fillRect(pf, r, pix);
  199. } else {
  200. assert(buflen >= (size_t)pf.bpp/8);
  201. pb->fillRect(pf, r, bufptr);
  202. }
  203. return;
  204. }
  205. // "JPEG" compression type.
  206. if (comp_ctl == tightJpeg) {
  207. uint32_t len;
  208. int stride;
  209. uint8_t *buf;
  210. JpegDecompressor jd;
  211. assert(buflen >= 4);
  212. memcpy(&len, bufptr, 4);
  213. bufptr += 4;
  214. buflen -= 4;
  215. // We always use direct decoding with JPEG images
  216. buf = pb->getBufferRW(r, &stride);
  217. jd.decompress(bufptr, len, buf, stride, r, pb->getPF());
  218. pb->commitBufferRW(r);
  219. return;
  220. }
  221. // Quit on unsupported compression type.
  222. assert(comp_ctl <= tightMaxSubencoding);
  223. // "Basic" compression type.
  224. int palSize = 0;
  225. uint8_t palette[256 * 4];
  226. bool useGradient = false;
  227. if ((comp_ctl & tightExplicitFilter) != 0) {
  228. uint8_t filterId;
  229. assert(buflen >= 1);
  230. filterId = *bufptr;
  231. bufptr += 1;
  232. buflen -= 1;
  233. switch (filterId) {
  234. case tightFilterPalette:
  235. assert(buflen >= 1);
  236. palSize = *bufptr + 1;
  237. bufptr += 1;
  238. buflen -= 1;
  239. if (pf.is888()) {
  240. size_t len = palSize * 3;
  241. std::vector<uint8_t> tightPalette(len);
  242. assert(buflen >= len);
  243. memcpy(tightPalette.data(), bufptr, len);
  244. bufptr += len;
  245. buflen -= len;
  246. pf.bufferFromRGB(palette, tightPalette.data(), palSize);
  247. } else {
  248. size_t len;
  249. len = palSize * pf.bpp/8;
  250. assert(buflen >= len);
  251. memcpy(palette, bufptr, len);
  252. bufptr += len;
  253. buflen -= len;
  254. }
  255. break;
  256. case tightFilterGradient:
  257. useGradient = true;
  258. break;
  259. case tightFilterCopy:
  260. break;
  261. default:
  262. assert(false);
  263. }
  264. }
  265. // Determine if the data should be decompressed or just copied.
  266. size_t rowSize, dataSize;
  267. uint8_t* netbuf;
  268. netbuf = NULL;
  269. if (palSize != 0) {
  270. if (palSize <= 2)
  271. rowSize = (r.width() + 7) / 8;
  272. else
  273. rowSize = r.width();
  274. } else if (pf.is888()) {
  275. rowSize = r.width() * 3;
  276. } else {
  277. rowSize = r.width() * pf.bpp/8;
  278. }
  279. dataSize = r.height() * rowSize;
  280. if (dataSize < TIGHT_MIN_TO_COMPRESS)
  281. assert(buflen >= dataSize);
  282. else {
  283. uint32_t len;
  284. int streamId;
  285. rdr::MemInStream* ms;
  286. assert(buflen >= 4);
  287. memcpy(&len, bufptr, 4);
  288. bufptr += 4;
  289. buflen -= 4;
  290. assert(buflen >= len);
  291. streamId = comp_ctl & 0x03;
  292. ms = new rdr::MemInStream(bufptr, len);
  293. zis[streamId].setUnderlying(ms, len);
  294. // Allocate buffer and decompress the data
  295. netbuf = new uint8_t[dataSize];
  296. if (!zis[streamId].hasData(dataSize))
  297. throw Exception("Tight decode error");
  298. zis[streamId].readBytes(netbuf, dataSize);
  299. zis[streamId].flushUnderlying();
  300. zis[streamId].setUnderlying(NULL, 0);
  301. delete ms;
  302. bufptr = netbuf;
  303. buflen = dataSize;
  304. }
  305. // Time to decode the actual data
  306. bool directDecode;
  307. uint8_t* outbuf;
  308. int stride;
  309. if (pb->getPF() == pf) {
  310. // Decode directly into the framebuffer (fast path)
  311. directDecode = true;
  312. } else {
  313. // Decode into an intermediate buffer and use pixel translation
  314. directDecode = false;
  315. }
  316. if (directDecode)
  317. outbuf = pb->getBufferRW(r, &stride);
  318. else {
  319. outbuf = new uint8_t[r.area() * (pf.bpp/8)];
  320. stride = r.width();
  321. }
  322. if (palSize == 0) {
  323. // Truecolor data
  324. if (useGradient) {
  325. if (pf.is888())
  326. FilterGradient24(bufptr, pf, (uint32_t*)outbuf, stride, r);
  327. else {
  328. switch (pf.bpp) {
  329. case 8:
  330. assert(false);
  331. break;
  332. case 16:
  333. FilterGradient(bufptr, pf, (uint16_t*)outbuf, stride, r);
  334. break;
  335. case 32:
  336. FilterGradient(bufptr, pf, (uint32_t*)outbuf, stride, r);
  337. break;
  338. }
  339. }
  340. } else {
  341. // Copy
  342. uint8_t* ptr = outbuf;
  343. const uint8_t* srcPtr = bufptr;
  344. int w = r.width();
  345. int h = r.height();
  346. if (pf.is888()) {
  347. while (h > 0) {
  348. pf.bufferFromRGB(ptr, srcPtr, w);
  349. ptr += stride * pf.bpp/8;
  350. srcPtr += w * 3;
  351. h--;
  352. }
  353. } else {
  354. while (h > 0) {
  355. memcpy(ptr, srcPtr, w * pf.bpp/8);
  356. ptr += stride * pf.bpp/8;
  357. srcPtr += w * pf.bpp/8;
  358. h--;
  359. }
  360. }
  361. }
  362. } else {
  363. // Indexed color
  364. switch (pf.bpp) {
  365. case 8:
  366. FilterPalette((const uint8_t*)palette, palSize,
  367. bufptr, (uint8_t*)outbuf, stride, r);
  368. break;
  369. case 16:
  370. FilterPalette((const uint16_t*)palette, palSize,
  371. bufptr, (uint16_t*)outbuf, stride, r);
  372. break;
  373. case 32:
  374. FilterPalette((const uint32_t*)palette, palSize,
  375. bufptr, (uint32_t*)outbuf, stride, r);
  376. break;
  377. }
  378. }
  379. if (directDecode)
  380. pb->commitBufferRW(r);
  381. else {
  382. pb->imageRect(pf, r, outbuf);
  383. delete [] outbuf;
  384. }
  385. delete [] netbuf;
  386. }
  387. uint32_t TightDecoder::readCompact(rdr::InStream* is)
  388. {
  389. uint8_t b;
  390. uint32_t result;
  391. b = is->readU8();
  392. result = (int)b & 0x7F;
  393. if (b & 0x80) {
  394. b = is->readU8();
  395. result |= ((int)b & 0x7F) << 7;
  396. if (b & 0x80) {
  397. b = is->readU8();
  398. result |= ((int)b & 0xFF) << 14;
  399. }
  400. }
  401. return result;
  402. }
  403. void
  404. TightDecoder::FilterGradient24(const uint8_t *inbuf,
  405. const PixelFormat& pf, uint32_t* outbuf,
  406. int stride, const Rect& r)
  407. {
  408. int x, y, c;
  409. uint8_t prevRow[TIGHT_MAX_WIDTH*3];
  410. uint8_t thisRow[TIGHT_MAX_WIDTH*3];
  411. uint8_t pix[3];
  412. int est[3];
  413. memset(prevRow, 0, sizeof(prevRow));
  414. // Set up shortcut variables
  415. int rectHeight = r.height();
  416. int rectWidth = r.width();
  417. for (y = 0; y < rectHeight; y++) {
  418. for (x = 0; x < rectWidth; x++) {
  419. /* First pixel in a row */
  420. if (x == 0) {
  421. for (c = 0; c < 3; c++) {
  422. pix[c] = inbuf[y*rectWidth*3+c] + prevRow[c];
  423. thisRow[c] = pix[c];
  424. }
  425. pf.bufferFromRGB((uint8_t*)&outbuf[y*stride], pix, 1);
  426. continue;
  427. }
  428. for (c = 0; c < 3; c++) {
  429. est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
  430. if (est[c] > 0xff) {
  431. est[c] = 0xff;
  432. } else if (est[c] < 0) {
  433. est[c] = 0;
  434. }
  435. pix[c] = inbuf[(y*rectWidth+x)*3+c] + est[c];
  436. thisRow[x*3+c] = pix[c];
  437. }
  438. pf.bufferFromRGB((uint8_t*)&outbuf[y*stride+x], pix, 1);
  439. }
  440. memcpy(prevRow, thisRow, sizeof(prevRow));
  441. }
  442. }
  443. template<class T>
  444. void TightDecoder::FilterGradient(const uint8_t* inbuf,
  445. const PixelFormat& pf, T* outbuf,
  446. int stride, const Rect& r)
  447. {
  448. int x, y, c;
  449. static uint8_t prevRow[TIGHT_MAX_WIDTH*3];
  450. static uint8_t thisRow[TIGHT_MAX_WIDTH*3];
  451. uint8_t pix[3];
  452. int est[3];
  453. memset(prevRow, 0, sizeof(prevRow));
  454. // Set up shortcut variables
  455. int rectHeight = r.height();
  456. int rectWidth = r.width();
  457. for (y = 0; y < rectHeight; y++) {
  458. for (x = 0; x < rectWidth; x++) {
  459. /* First pixel in a row */
  460. if (x == 0) {
  461. pf.rgbFromBuffer(pix, &inbuf[y*rectWidth], 1);
  462. for (c = 0; c < 3; c++)
  463. pix[c] += prevRow[c];
  464. memcpy(thisRow, pix, sizeof(pix));
  465. pf.bufferFromRGB((uint8_t*)&outbuf[y*stride], pix, 1);
  466. continue;
  467. }
  468. for (c = 0; c < 3; c++) {
  469. est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
  470. if (est[c] > 255) {
  471. est[c] = 255;
  472. } else if (est[c] < 0) {
  473. est[c] = 0;
  474. }
  475. }
  476. pf.rgbFromBuffer(pix, &inbuf[y*rectWidth+x], 1);
  477. for (c = 0; c < 3; c++)
  478. pix[c] += est[c];
  479. memcpy(&thisRow[x*3], pix, sizeof(pix));
  480. pf.bufferFromRGB((uint8_t*)&outbuf[y*stride+x], pix, 1);
  481. }
  482. memcpy(prevRow, thisRow, sizeof(prevRow));
  483. }
  484. }
  485. template<class T>
  486. void TightDecoder::FilterPalette(const T* palette, int palSize,
  487. const uint8_t* inbuf, T* outbuf,
  488. int stride, const Rect& r)
  489. {
  490. // Indexed color
  491. int x, h = r.height(), w = r.width(), b, pad = stride - w;
  492. T* ptr = outbuf;
  493. uint8_t bits;
  494. const uint8_t* srcPtr = inbuf;
  495. if (palSize <= 2) {
  496. // 2-color palette
  497. while (h > 0) {
  498. for (x = 0; x < w / 8; x++) {
  499. bits = *srcPtr++;
  500. for (b = 7; b >= 0; b--) {
  501. *ptr++ = palette[bits >> b & 1];
  502. }
  503. }
  504. if (w % 8 != 0) {
  505. bits = *srcPtr++;
  506. for (b = 7; b >= 8 - w % 8; b--) {
  507. *ptr++ = palette[bits >> b & 1];
  508. }
  509. }
  510. ptr += pad;
  511. h--;
  512. }
  513. } else {
  514. // 256-color palette
  515. while (h > 0) {
  516. T *endOfRow = ptr + w;
  517. while (ptr < endOfRow) {
  518. *ptr++ = palette[*srcPtr++];
  519. }
  520. ptr += pad;
  521. h--;
  522. }
  523. }
  524. }