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.

TightEncoder.cxx 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
  2. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  3. * Copyright 2014 Pierre Ossman for Cendio AB
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <assert.h>
  24. #include <rdr/OutStream.h>
  25. #include <rfb/PixelBuffer.h>
  26. #include <rfb/Palette.h>
  27. #include <rfb/encodings.h>
  28. #include <rfb/SConnection.h>
  29. #include <rfb/TightEncoder.h>
  30. #include <rfb/TightConstants.h>
  31. using namespace rfb;
  32. struct TightConf {
  33. int idxZlibLevel, monoZlibLevel, rawZlibLevel;
  34. };
  35. //
  36. // Compression level stuff. The following array contains zlib
  37. // settings for each of 10 compression levels (0..9).
  38. //
  39. // NOTE: The parameters used in this encoder are the result of painstaking
  40. // research by The VirtualGL Project using RFB session captures from a variety
  41. // of both 2D and 3D applications. See http://www.VirtualGL.org for the full
  42. // reports.
  43. static const TightConf conf[10] = {
  44. { 0, 0, 0 }, // 0
  45. { 1, 1, 1 }, // 1
  46. { 3, 3, 2 }, // 2
  47. { 5, 5, 2 }, // 3
  48. { 6, 7, 3 }, // 4
  49. { 7, 8, 4 }, // 5
  50. { 7, 8, 5 }, // 6
  51. { 8, 9, 6 }, // 7
  52. { 9, 9, 7 }, // 8
  53. { 9, 9, 9 } // 9
  54. };
  55. TightEncoder::TightEncoder(SConnection* conn) :
  56. Encoder(conn, encodingTight, EncoderPlain, 256)
  57. {
  58. setCompressLevel(-1);
  59. }
  60. TightEncoder::~TightEncoder()
  61. {
  62. }
  63. bool TightEncoder::isSupported()
  64. {
  65. return conn->client.supportsEncoding(encodingTight);
  66. }
  67. void TightEncoder::setCompressLevel(int level)
  68. {
  69. if (level < 0 || level > 9)
  70. level = 2;
  71. idxZlibLevel = conf[level].idxZlibLevel;
  72. monoZlibLevel = conf[level].monoZlibLevel;
  73. rawZlibLevel = conf[level].rawZlibLevel;
  74. }
  75. void TightEncoder::writeRect(const PixelBuffer* pb, const Palette& palette)
  76. {
  77. switch (palette.size()) {
  78. case 0:
  79. writeFullColourRect(pb);
  80. break;
  81. case 1:
  82. Encoder::writeSolidRect(pb, palette);
  83. break;
  84. case 2:
  85. writeMonoRect(pb, palette);
  86. break;
  87. default:
  88. writeIndexedRect(pb, palette);
  89. }
  90. }
  91. void TightEncoder::writeSolidRect(int /*width*/, int /*height*/,
  92. const PixelFormat& pf,
  93. const rdr::U8* colour)
  94. {
  95. rdr::OutStream* os;
  96. os = conn->getOutStream();
  97. os->writeU8(tightFill << 4);
  98. writePixels(colour, pf, 1, os);
  99. }
  100. void TightEncoder::writeMonoRect(const PixelBuffer* pb, const Palette& palette)
  101. {
  102. const rdr::U8* buffer;
  103. int stride;
  104. buffer = pb->getBuffer(pb->getRect(), &stride);
  105. switch (pb->getPF().bpp) {
  106. case 32:
  107. writeMonoRect(pb->width(), pb->height(), (rdr::U32*)buffer, stride,
  108. pb->getPF(), palette);
  109. break;
  110. case 16:
  111. writeMonoRect(pb->width(), pb->height(), (rdr::U16*)buffer, stride,
  112. pb->getPF(), palette);
  113. break;
  114. default:
  115. writeMonoRect(pb->width(), pb->height(), (rdr::U8*)buffer, stride,
  116. pb->getPF(), palette);
  117. }
  118. }
  119. void TightEncoder::writeIndexedRect(const PixelBuffer* pb, const Palette& palette)
  120. {
  121. const rdr::U8* buffer;
  122. int stride;
  123. buffer = pb->getBuffer(pb->getRect(), &stride);
  124. switch (pb->getPF().bpp) {
  125. case 32:
  126. writeIndexedRect(pb->width(), pb->height(), (rdr::U32*)buffer, stride,
  127. pb->getPF(), palette);
  128. break;
  129. case 16:
  130. writeIndexedRect(pb->width(), pb->height(), (rdr::U16*)buffer, stride,
  131. pb->getPF(), palette);
  132. break;
  133. default:
  134. // It's more efficient to just do raw pixels
  135. writeFullColourRect(pb);
  136. }
  137. }
  138. void TightEncoder::writeFullColourRect(const PixelBuffer* pb)
  139. {
  140. const int streamId = 0;
  141. rdr::OutStream* os;
  142. rdr::OutStream* zos;
  143. int length;
  144. const rdr::U8* buffer;
  145. int stride, h;
  146. os = conn->getOutStream();
  147. os->writeU8(streamId << 4);
  148. // Set up compression
  149. if ((pb->getPF().bpp != 32) || !pb->getPF().is888())
  150. length = pb->getRect().area() * pb->getPF().bpp/8;
  151. else
  152. length = pb->getRect().area() * 3;
  153. zos = getZlibOutStream(streamId, rawZlibLevel, length);
  154. // And then just dump all the raw pixels
  155. buffer = pb->getBuffer(pb->getRect(), &stride);
  156. h = pb->height();
  157. while (h--) {
  158. writePixels(buffer, pb->getPF(), pb->width(), zos);
  159. buffer += stride * pb->getPF().bpp/8;
  160. }
  161. // Finish the zlib stream
  162. flushZlibOutStream(zos);
  163. }
  164. void TightEncoder::writePixels(const rdr::U8* buffer, const PixelFormat& pf,
  165. unsigned int count, rdr::OutStream* os)
  166. {
  167. rdr::U8 rgb[2048];
  168. if ((pf.bpp != 32) || !pf.is888()) {
  169. os->writeBytes(buffer, count * pf.bpp/8);
  170. return;
  171. }
  172. while (count) {
  173. unsigned int iter_count;
  174. iter_count = sizeof(rgb)/3;
  175. if (iter_count > count)
  176. iter_count = count;
  177. pf.rgbFromBuffer(rgb, buffer, iter_count);
  178. os->writeBytes(rgb, iter_count * 3);
  179. buffer += iter_count * pf.bpp/8;
  180. count -= iter_count;
  181. }
  182. }
  183. void TightEncoder::writeCompact(rdr::OutStream* os, rdr::U32 value)
  184. {
  185. rdr::U8 b;
  186. b = value & 0x7F;
  187. if (value <= 0x7F) {
  188. os->writeU8(b);
  189. } else {
  190. os->writeU8(b | 0x80);
  191. b = value >> 7 & 0x7F;
  192. if (value <= 0x3FFF) {
  193. os->writeU8(b);
  194. } else {
  195. os->writeU8(b | 0x80);
  196. os->writeU8(value >> 14 & 0xFF);
  197. }
  198. }
  199. }
  200. rdr::OutStream* TightEncoder::getZlibOutStream(int streamId, int level, size_t length)
  201. {
  202. // Minimum amount of data to be compressed. This value should not be
  203. // changed, doing so will break compatibility with existing clients.
  204. if (length < 12)
  205. return conn->getOutStream();
  206. assert(streamId >= 0);
  207. assert(streamId < 4);
  208. zlibStreams[streamId].setUnderlying(&memStream);
  209. zlibStreams[streamId].setCompressionLevel(level);
  210. zlibStreams[streamId].cork(true);
  211. return &zlibStreams[streamId];
  212. }
  213. void TightEncoder::flushZlibOutStream(rdr::OutStream* os_)
  214. {
  215. rdr::OutStream* os;
  216. rdr::ZlibOutStream* zos;
  217. zos = dynamic_cast<rdr::ZlibOutStream*>(os_);
  218. if (zos == NULL)
  219. return;
  220. zos->cork(false);
  221. zos->flush();
  222. zos->setUnderlying(NULL);
  223. os = conn->getOutStream();
  224. writeCompact(os, memStream.length());
  225. os->writeBytes(memStream.data(), memStream.length());
  226. memStream.clear();
  227. }
  228. //
  229. // Including BPP-dependent implementation of the encoder.
  230. //
  231. #define BPP 8
  232. #include <rfb/TightEncoderBPP.cxx>
  233. #undef BPP
  234. #define BPP 16
  235. #include <rfb/TightEncoderBPP.cxx>
  236. #undef BPP
  237. #define BPP 32
  238. #include <rfb/TightEncoderBPP.cxx>
  239. #undef BPP