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.8KB

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