blob: b7ef2236ef0c9a1f362b8eb0b1453ba6bfb9b548 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
DRCcd2c5d42011-08-11 11:18:34 +00002 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19#include <rdr/OutStream.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000020#include <rfb/encodings.h>
21#include <rfb/ConnParams.h>
22#include <rfb/SMsgWriter.h>
23#include <rfb/TightEncoder.h>
24
25using namespace rfb;
26
27// Minimum amount of data to be compressed. This value should not be
28// changed, doing so will break compatibility with existing clients.
29#define TIGHT_MIN_TO_COMPRESS 12
30
31// Adjustable parameters.
32// FIXME: Get rid of #defines
DRCcd2c5d42011-08-11 11:18:34 +000033#define TIGHT_MAX_SPLIT_TILE_SIZE 16
34#define TIGHT_MIN_SPLIT_RECT_SIZE 4096
35#define TIGHT_MIN_SOLID_SUBRECT_SIZE 2048
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036
37//
38// Compression level stuff. The following array contains various
39// encoder parameters for each of 10 compression levels (0..9).
40// Last three parameters correspond to JPEG quality levels (0..9).
41//
DRCcd2c5d42011-08-11 11:18:34 +000042// NOTE: The parameters used in this encoder are the result of painstaking
43// research by The VirtualGL Project using RFB session captures from a variety
44// of both 2D and 3D applications. See http://www.VirtualGL.org for the full
45// reports.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046
DRC773cf3c2009-03-12 19:26:44 +000047// NOTE: The JPEG quality and subsampling levels below were obtained
48// experimentally by the VirtualGL Project. They represent the approximate
49// average compression ratios listed below, as measured across the set of
50// every 10th frame in the SPECviewperf 9 benchmark suite.
51//
52// 9 = JPEG quality 100, no subsampling (ratio ~= 10:1)
53// [this should be lossless, except for round-off error]
54// 8 = JPEG quality 92, no subsampling (ratio ~= 20:1)
55// [this should be perceptually lossless, based on current research]
56// 7 = JPEG quality 86, no subsampling (ratio ~= 25:1)
57// 6 = JPEG quality 79, no subsampling (ratio ~= 30:1)
58// 5 = JPEG quality 77, 4:2:2 subsampling (ratio ~= 40:1)
59// 4 = JPEG quality 62, 4:2:2 subsampling (ratio ~= 50:1)
60// 3 = JPEG quality 42, 4:2:2 subsampling (ratio ~= 60:1)
61// 2 = JPEG quality 41, 4:2:0 subsampling (ratio ~= 70:1)
62// 1 = JPEG quality 29, 4:2:0 subsampling (ratio ~= 80:1)
63// 0 = JPEG quality 15, 4:2:0 subsampling (ratio ~= 100:1)
64
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000065const TIGHT_CONF TightEncoder::conf[10] = {
DRCcd2c5d42011-08-11 11:18:34 +000066 { 65536, 2048, 6, 0, 0, 0, 4, 24, 15, SUBSAMP_420 }, // 0
67 { 65536, 2048, 6, 1, 1, 1, 8, 24, 29, SUBSAMP_420 }, // 1
68 { 65536, 2048, 8, 3, 3, 2, 24, 96, 41, SUBSAMP_420 }, // 2
69 { 65536, 2048, 12, 5, 5, 2, 32, 96, 42, SUBSAMP_422 }, // 3
70 { 65536, 2048, 12, 6, 7, 3, 32, 96, 62, SUBSAMP_422 }, // 4
71 { 65536, 2048, 12, 7, 8, 4, 32, 96, 77, SUBSAMP_422 }, // 5
72 { 65536, 2048, 16, 7, 8, 5, 32, 96, 79, SUBSAMP_NONE }, // 6
73 { 65536, 2048, 16, 8, 9, 6, 64, 96, 86, SUBSAMP_NONE }, // 7
74 { 65536, 2048, 24, 9, 9, 7, 64, 96, 92, SUBSAMP_NONE }, // 8
75 { 65536, 2048, 32, 9, 9, 9, 96, 96,100, SUBSAMP_NONE } // 9
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000076};
DRCcd2c5d42011-08-11 11:18:34 +000077const int TightEncoder::defaultCompressLevel = 1;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000078
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079//
80// Including BPP-dependent implementation of the encoder.
81//
82
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083#define BPP 8
84#include <rfb/tightEncode.h>
85#undef BPP
86#define BPP 16
87#include <rfb/tightEncode.h>
88#undef BPP
89#define BPP 32
90#include <rfb/tightEncode.h>
91#undef BPP
92
93Encoder* TightEncoder::create(SMsgWriter* writer)
94{
95 return new TightEncoder(writer);
96}
97
98TightEncoder::TightEncoder(SMsgWriter* writer_) : writer(writer_)
99{
100 setCompressLevel(defaultCompressLevel);
101 setQualityLevel(-1);
102}
103
104TightEncoder::~TightEncoder()
105{
106}
107
108void TightEncoder::setCompressLevel(int level)
109{
110 if (level >= 0 && level <= 9) {
111 pconf = &conf[level];
112 } else {
113 pconf = &conf[defaultCompressLevel];
114 }
115}
116
117void TightEncoder::setQualityLevel(int level)
118{
119 if (level >= 0 && level <= 9) {
DRCb4a83232011-08-19 04:57:18 +0000120 jpegQuality = conf[level].jpegQuality;
121 jpegSubsampling = conf[level].jpegSubsampling;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122 } else {
DRCb4a83232011-08-19 04:57:18 +0000123 jpegQuality = -1;
124 jpegSubsampling = SUBSAMP_UNDEFINED;
125 }
126}
127
128void TightEncoder::setFineQualityLevel(int quality, JPEG_SUBSAMP subsampling)
129{
130 if (quality >= 1 && quality <= 100) {
131 jpegQuality = quality;
132 }
133 if (subsampling >= SUBSAMP_NONE && subsampling <= SUBSAMP_GRAY) {
134 jpegSubsampling = subsampling;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135 }
136}
137
DRCffe09d62011-08-17 02:27:59 +0000138bool TightEncoder::checkSolidTile(Rect& r, rdr::U32* colorPtr,
DRCcd2c5d42011-08-11 11:18:34 +0000139 bool needSameColor)
140{
DRCffe09d62011-08-17 02:27:59 +0000141 switch (serverpf.bpp) {
DRCcd2c5d42011-08-11 11:18:34 +0000142 case 32:
DRCffe09d62011-08-17 02:27:59 +0000143 return checkSolidTile32(r, colorPtr, needSameColor);
DRCcd2c5d42011-08-11 11:18:34 +0000144 case 16:
DRCffe09d62011-08-17 02:27:59 +0000145 return checkSolidTile16(r, colorPtr, needSameColor);
DRCcd2c5d42011-08-11 11:18:34 +0000146 default:
DRCffe09d62011-08-17 02:27:59 +0000147 return checkSolidTile8(r, colorPtr, needSameColor);
DRCcd2c5d42011-08-11 11:18:34 +0000148 }
149}
150
DRCffe09d62011-08-17 02:27:59 +0000151void TightEncoder::findBestSolidArea(Rect& r, rdr::U32 colorValue, Rect& bestr)
DRCcd2c5d42011-08-11 11:18:34 +0000152{
153 int dx, dy, dw, dh;
154 int w_prev;
155 Rect sr;
156 int w_best = 0, h_best = 0;
157
158 bestr.tl.x = bestr.br.x = r.tl.x;
159 bestr.tl.y = bestr.br.y = r.tl.y;
160
161 w_prev = r.width();
162
163 for (dy = r.tl.y; dy < r.br.y; dy += TIGHT_MAX_SPLIT_TILE_SIZE) {
164
165 dh = (dy + TIGHT_MAX_SPLIT_TILE_SIZE <= r.br.y) ?
166 TIGHT_MAX_SPLIT_TILE_SIZE : (r.br.y - dy);
167 dw = (w_prev > TIGHT_MAX_SPLIT_TILE_SIZE) ?
168 TIGHT_MAX_SPLIT_TILE_SIZE : w_prev;
169
170 sr.setXYWH(r.tl.x, dy, dw, dh);
DRCffe09d62011-08-17 02:27:59 +0000171 if (!checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000172 break;
173
174 for (dx = r.tl.x + dw; dx < r.tl.x + w_prev;) {
175 dw = (dx + TIGHT_MAX_SPLIT_TILE_SIZE <= r.tl.x + w_prev) ?
176 TIGHT_MAX_SPLIT_TILE_SIZE : (r.tl.x + w_prev - dx);
177 sr.setXYWH(dx, dy, dw, dh);
DRCffe09d62011-08-17 02:27:59 +0000178 if (!checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000179 break;
DRCffe09d62011-08-17 02:27:59 +0000180 dx += dw;
DRCcd2c5d42011-08-11 11:18:34 +0000181 }
182
183 w_prev = dx - r.tl.x;
184 if (w_prev * (dy + dh - r.tl.y) > w_best * h_best) {
185 w_best = w_prev;
186 h_best = dy + dh - r.tl.y;
187 }
188 }
189
190 bestr.br.x = bestr.tl.x + w_best;
191 bestr.br.y = bestr.tl.y + h_best;
192}
193
DRCffe09d62011-08-17 02:27:59 +0000194void TightEncoder::extendSolidArea(const Rect& r, rdr::U32 colorValue,
195 Rect& er)
DRCcd2c5d42011-08-11 11:18:34 +0000196{
197 int cx, cy;
198 Rect sr;
199
200 // Try to extend the area upwards.
201 for (cy = er.tl.y - 1; ; cy--) {
202 sr.setXYWH(er.tl.x, cy, er.width(), 1);
DRCffe09d62011-08-17 02:27:59 +0000203 if (cy < r.tl.y || !checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000204 break;
205 }
206 er.tl.y = cy + 1;
207
208 // ... downwards.
209 for (cy = er.br.y; ; cy++) {
210 sr.setXYWH(er.tl.x, cy, er.width(), 1);
DRCffe09d62011-08-17 02:27:59 +0000211 if (cy >= r.br.y || !checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000212 break;
213 }
214 er.br.y = cy;
215
216 // ... to the left.
217 for (cx = er.tl.x - 1; ; cx--) {
218 sr.setXYWH(cx, er.tl.y, 1, er.height());
DRCffe09d62011-08-17 02:27:59 +0000219 if (cx < r.tl.x || !checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000220 break;
221 }
222 er.tl.x = cx + 1;
223
224 // ... to the right.
225 for (cx = er.br.x; ; cx++) {
226 sr.setXYWH(cx, er.tl.y, 1, er.height());
DRCffe09d62011-08-17 02:27:59 +0000227 if (cx >= r.br.x || !checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000228 break;
229 }
230 er.br.x = cx;
231}
232
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000233int TightEncoder::getNumRects(const Rect &r)
234{
DRCcd2c5d42011-08-11 11:18:34 +0000235 ConnParams* cp = writer->getConnParams();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 const unsigned int w = r.width();
237 const unsigned int h = r.height();
238
DRCcd2c5d42011-08-11 11:18:34 +0000239 // If last rect. encoding is enabled, we can use the higher-performance
240 // code that pre-computes solid rectangles. In that case, we don't care
241 // about the rectangle count.
242 if (cp->supportsLastRect && w * h >= TIGHT_MIN_SPLIT_RECT_SIZE)
243 return 0;
244
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000245 // Will this rectangle split into subrects?
246 bool rectTooBig = w > pconf->maxRectWidth || w * h > pconf->maxRectSize;
247 if (!rectTooBig)
248 return 1;
249
250 // Compute max sub-rectangle size.
251 const unsigned int subrectMaxWidth =
252 (w > pconf->maxRectWidth) ? pconf->maxRectWidth : w;
253 const unsigned int subrectMaxHeight =
254 pconf->maxRectSize / subrectMaxWidth;
255
256 // Return the number of subrects.
257 return (((w - 1) / pconf->maxRectWidth + 1) *
258 ((h - 1) / subrectMaxHeight + 1));
259}
260
DRCffe09d62011-08-17 02:27:59 +0000261void TightEncoder::sendRectSimple(const Rect& r)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000262{
263 // Shortcuts to rectangle coordinates and dimensions.
264 const int x = r.tl.x;
265 const int y = r.tl.y;
266 const unsigned int w = r.width();
267 const unsigned int h = r.height();
268
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000269 // Encode small rects as is.
270 bool rectTooBig = w > pconf->maxRectWidth || w * h > pconf->maxRectSize;
271 if (!rectTooBig) {
DRCffe09d62011-08-17 02:27:59 +0000272 writeSubrect(r);
DRCcd2c5d42011-08-11 11:18:34 +0000273 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000274 }
275
276 // Compute max sub-rectangle size.
277 const unsigned int subrectMaxWidth =
278 (w > pconf->maxRectWidth) ? pconf->maxRectWidth : w;
279 const unsigned int subrectMaxHeight =
280 pconf->maxRectSize / subrectMaxWidth;
281
282 // Split big rects into separately encoded subrects.
283 Rect sr;
284 unsigned int dx, dy, sw, sh;
285 for (dy = 0; dy < h; dy += subrectMaxHeight) {
286 for (dx = 0; dx < w; dx += pconf->maxRectWidth) {
287 sw = (dx + pconf->maxRectWidth < w) ? pconf->maxRectWidth : w - dx;
288 sh = (dy + subrectMaxHeight < h) ? subrectMaxHeight : h - dy;
289 sr.setXYWH(x + dx, y + dy, sw, sh);
DRCffe09d62011-08-17 02:27:59 +0000290 writeSubrect(sr);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000291 }
292 }
DRCcd2c5d42011-08-11 11:18:34 +0000293}
294
DRCffe09d62011-08-17 02:27:59 +0000295bool TightEncoder::writeRect(const Rect& _r, TransImageGetter* _ig,
296 Rect* actual)
DRCcd2c5d42011-08-11 11:18:34 +0000297{
DRCffe09d62011-08-17 02:27:59 +0000298 ig = _ig;
299 serverpf = ig->getPixelBuffer()->getPF();
DRCcd2c5d42011-08-11 11:18:34 +0000300 ConnParams* cp = writer->getConnParams();
DRCffe09d62011-08-17 02:27:59 +0000301 clientpf = cp->pf();
DRCcd2c5d42011-08-11 11:18:34 +0000302
303 // Shortcuts to rectangle coordinates and dimensions.
304 Rect r = _r;
305 int x = r.tl.x;
306 int y = r.tl.y;
DRCe3ffcf72011-08-19 03:11:32 +0000307 int w = r.width();
308 int h = r.height();
DRCcd2c5d42011-08-11 11:18:34 +0000309
DRCcd2c5d42011-08-11 11:18:34 +0000310 // Encode small rects as is.
311 if (!cp->supportsLastRect || w * h < TIGHT_MIN_SPLIT_RECT_SIZE) {
DRCffe09d62011-08-17 02:27:59 +0000312 sendRectSimple(r);
DRCcd2c5d42011-08-11 11:18:34 +0000313 return true;
314 }
315
316 // Split big rects into separately encoded subrects.
317 Rect sr, bestr;
DRCe3ffcf72011-08-19 03:11:32 +0000318 int dx, dy, dw, dh;
DRCcd2c5d42011-08-11 11:18:34 +0000319 rdr::U32 colorValue;
DRCffe09d62011-08-17 02:27:59 +0000320 int maxRectSize = pconf->maxRectSize;
321 int maxRectWidth = pconf->maxRectWidth;
DRCcd2c5d42011-08-11 11:18:34 +0000322 int nMaxWidth = (w > maxRectWidth) ? maxRectWidth : w;
DRCffe09d62011-08-17 02:27:59 +0000323 int nMaxRows = pconf->maxRectSize / nMaxWidth;
DRCcd2c5d42011-08-11 11:18:34 +0000324
325 // Try to find large solid-color areas and send them separately.
326 for (dy = y; dy < y + h; dy += TIGHT_MAX_SPLIT_TILE_SIZE) {
327
328 // If a rectangle becomes too large, send its upper part now.
329 if (dy - y >= nMaxRows) {
330 sr.setXYWH(x, y, w, nMaxRows);
DRCffe09d62011-08-17 02:27:59 +0000331 sendRectSimple(sr);
DRCcd2c5d42011-08-11 11:18:34 +0000332 r.tl.y += nMaxRows;
333 y = r.tl.y;
334 h = r.height();
335 }
336
337 dh = (dy + TIGHT_MAX_SPLIT_TILE_SIZE <= y + h) ?
338 TIGHT_MAX_SPLIT_TILE_SIZE : (y + h - dy);
339
340 for (dx = x; dx < x + w; dx += TIGHT_MAX_SPLIT_TILE_SIZE) {
341
342 dw = (dx + TIGHT_MAX_SPLIT_TILE_SIZE <= x + w) ?
343 TIGHT_MAX_SPLIT_TILE_SIZE : (x + w - dx);
344
345 sr.setXYWH(dx, dy, dw, dh);
DRCffe09d62011-08-17 02:27:59 +0000346 if (checkSolidTile(sr, &colorValue, false)) {
DRCcd2c5d42011-08-11 11:18:34 +0000347
DRCb4a83232011-08-19 04:57:18 +0000348 if (jpegSubsampling == SUBSAMP_GRAY && jpegQuality != -1) {
349 Colour rgb;
350 serverpf.rgbFromPixel(colorValue, NULL, &rgb);
351 rdr::U32 lum = ((257 * rgb.r) + (504 * rgb.g) + (98 * rgb.b)
352 + 16500) / 1000;
353 colorValue = lum + (lum << 8) + (lum << 16);
354 }
355
DRCcd2c5d42011-08-11 11:18:34 +0000356 // Get dimensions of solid-color area.
357 sr.setXYWH(dx, dy, r.br.x - dx, r.br.y - dy);
DRCffe09d62011-08-17 02:27:59 +0000358 findBestSolidArea(sr, colorValue, bestr);
DRCcd2c5d42011-08-11 11:18:34 +0000359
360 // Make sure a solid rectangle is large enough
361 // (or the whole rectangle is of the same color).
362 if (bestr.area() != r.area()
363 && bestr.area() < TIGHT_MIN_SOLID_SUBRECT_SIZE)
364 continue;
365
366 // Try to extend solid rectangle to maximum size.
DRCffe09d62011-08-17 02:27:59 +0000367 extendSolidArea(r, colorValue, bestr);
DRCcd2c5d42011-08-11 11:18:34 +0000368
369 // Send rectangles at top and left to solid-color area.
370 if (bestr.tl.y != y) {
371 sr.setXYWH(x, y, w, bestr.tl.y - y);
DRCffe09d62011-08-17 02:27:59 +0000372 sendRectSimple(sr);
DRCcd2c5d42011-08-11 11:18:34 +0000373 }
374 if (bestr.tl.x != x) {
375 sr.setXYWH(x, bestr.tl.y, bestr.tl.x - x, bestr.height());
DRCffe09d62011-08-17 02:27:59 +0000376 writeRect(sr, _ig, NULL);
DRCcd2c5d42011-08-11 11:18:34 +0000377 }
378
379 // Send solid-color rectangle.
DRCffe09d62011-08-17 02:27:59 +0000380 writeSubrect(bestr, true);
DRCcd2c5d42011-08-11 11:18:34 +0000381
382 // Send remaining rectangles (at right and bottom).
383 if (bestr.br.x != r.br.x) {
384 sr.setXYWH(bestr.br.x, bestr.tl.y, r.br.x - bestr.br.x,
385 bestr.height());
DRCffe09d62011-08-17 02:27:59 +0000386 writeRect(sr, _ig, NULL);
DRCcd2c5d42011-08-11 11:18:34 +0000387 }
388 if (bestr.br.y != r.br.y) {
389 sr.setXYWH(x, bestr.br.y, w, r.br.y - bestr.br.y);
DRCffe09d62011-08-17 02:27:59 +0000390 writeRect(sr, _ig, NULL);
DRCcd2c5d42011-08-11 11:18:34 +0000391 }
392
393 return true;
394 }
395 }
396 }
397
398 // No suitable solid-color rectangles found.
DRCffe09d62011-08-17 02:27:59 +0000399 sendRectSimple(r);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000400 return true;
401}
402
DRCffe09d62011-08-17 02:27:59 +0000403void TightEncoder::writeSubrect(const Rect& r, bool forceSolid)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000404{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000405 mos.clear();
406
DRCffe09d62011-08-17 02:27:59 +0000407 switch (clientpf.bpp) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000408 case 8:
DRCffe09d62011-08-17 02:27:59 +0000409 tightEncode8(r, &mos, forceSolid); break;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000410 case 16:
DRCffe09d62011-08-17 02:27:59 +0000411 tightEncode16(r, &mos, forceSolid); break;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000412 case 32:
DRCffe09d62011-08-17 02:27:59 +0000413 tightEncode32(r, &mos, forceSolid); break;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000414 }
415
416 writer->startRect(r, encodingTight);
417 rdr::OutStream* os = writer->getOutStream();
418 os->writeBytes(mos.data(), mos.length());
419 writer->endRect();
420}