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

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