blob: 622dc3d40126fa9078d4611f9ab7aab25ecb94e7 [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>
Pierre Ossman456b2c22014-01-15 13:22:03 +010020#include <rfb/TransImageGetter.h>
Pierre Ossman7638e9c2014-01-16 13:12:40 +010021#include <rfb/PixelBuffer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000022#include <rfb/encodings.h>
23#include <rfb/ConnParams.h>
24#include <rfb/SMsgWriter.h>
25#include <rfb/TightEncoder.h>
26
27using namespace rfb;
28
29// Minimum amount of data to be compressed. This value should not be
30// changed, doing so will break compatibility with existing clients.
31#define TIGHT_MIN_TO_COMPRESS 12
32
33// Adjustable parameters.
34// FIXME: Get rid of #defines
DRCcd2c5d42011-08-11 11:18:34 +000035#define TIGHT_MAX_SPLIT_TILE_SIZE 16
36#define TIGHT_MIN_SPLIT_RECT_SIZE 4096
37#define TIGHT_MIN_SOLID_SUBRECT_SIZE 2048
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000038
39//
40// Compression level stuff. The following array contains various
41// encoder parameters for each of 10 compression levels (0..9).
42// Last three parameters correspond to JPEG quality levels (0..9).
43//
DRCcd2c5d42011-08-11 11:18:34 +000044// NOTE: The parameters used in this encoder are the result of painstaking
45// research by The VirtualGL Project using RFB session captures from a variety
46// of both 2D and 3D applications. See http://www.VirtualGL.org for the full
47// reports.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000048
DRC773cf3c2009-03-12 19:26:44 +000049// NOTE: The JPEG quality and subsampling levels below were obtained
50// experimentally by the VirtualGL Project. They represent the approximate
51// average compression ratios listed below, as measured across the set of
52// every 10th frame in the SPECviewperf 9 benchmark suite.
53//
54// 9 = JPEG quality 100, no subsampling (ratio ~= 10:1)
55// [this should be lossless, except for round-off error]
56// 8 = JPEG quality 92, no subsampling (ratio ~= 20:1)
57// [this should be perceptually lossless, based on current research]
58// 7 = JPEG quality 86, no subsampling (ratio ~= 25:1)
59// 6 = JPEG quality 79, no subsampling (ratio ~= 30:1)
60// 5 = JPEG quality 77, 4:2:2 subsampling (ratio ~= 40:1)
61// 4 = JPEG quality 62, 4:2:2 subsampling (ratio ~= 50:1)
62// 3 = JPEG quality 42, 4:2:2 subsampling (ratio ~= 60:1)
63// 2 = JPEG quality 41, 4:2:0 subsampling (ratio ~= 70:1)
64// 1 = JPEG quality 29, 4:2:0 subsampling (ratio ~= 80:1)
65// 0 = JPEG quality 15, 4:2:0 subsampling (ratio ~= 100:1)
66
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067const TIGHT_CONF TightEncoder::conf[10] = {
Pierre Ossmanb948a912014-01-15 13:23:43 +010068 { 65536, 2048, 6, 0, 0, 0, 4, 24, 15, subsample4X }, // 0
69 { 65536, 2048, 6, 1, 1, 1, 8, 24, 29, subsample4X }, // 1
70 { 65536, 2048, 8, 3, 3, 2, 24, 96, 41, subsample4X }, // 2
71 { 65536, 2048, 12, 5, 5, 2, 32, 96, 42, subsample2X }, // 3
72 { 65536, 2048, 12, 6, 7, 3, 32, 96, 62, subsample2X }, // 4
73 { 65536, 2048, 12, 7, 8, 4, 32, 96, 77, subsample2X }, // 5
74 { 65536, 2048, 16, 7, 8, 5, 32, 96, 79, subsampleNone }, // 6
75 { 65536, 2048, 16, 8, 9, 6, 64, 96, 86, subsampleNone }, // 7
76 { 65536, 2048, 24, 9, 9, 7, 64, 96, 92, subsampleNone }, // 8
77 { 65536, 2048, 32, 9, 9, 9, 96, 96,100, subsampleNone } // 9
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000078};
Pierre Ossman701ad682011-11-20 15:39:17 +000079
80const int TightEncoder::defaultCompressLevel = 2;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082//
83// Including BPP-dependent implementation of the encoder.
84//
85
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086#define BPP 8
87#include <rfb/tightEncode.h>
88#undef BPP
89#define BPP 16
90#include <rfb/tightEncode.h>
91#undef BPP
92#define BPP 32
93#include <rfb/tightEncode.h>
94#undef BPP
95
Pierre Ossman4aba19e2014-01-29 16:42:48 +010096TightEncoder::TightEncoder(SMsgWriter* writer) : Encoder(writer)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097{
98 setCompressLevel(defaultCompressLevel);
99 setQualityLevel(-1);
100}
101
102TightEncoder::~TightEncoder()
103{
104}
105
106void TightEncoder::setCompressLevel(int level)
107{
108 if (level >= 0 && level <= 9) {
109 pconf = &conf[level];
110 } else {
111 pconf = &conf[defaultCompressLevel];
112 }
113}
114
115void TightEncoder::setQualityLevel(int level)
116{
117 if (level >= 0 && level <= 9) {
DRCb4a83232011-08-19 04:57:18 +0000118 jpegQuality = conf[level].jpegQuality;
119 jpegSubsampling = conf[level].jpegSubsampling;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000120 } else {
DRCb4a83232011-08-19 04:57:18 +0000121 jpegQuality = -1;
Pierre Ossmanb948a912014-01-15 13:23:43 +0100122 jpegSubsampling = subsampleUndefined;
DRCb4a83232011-08-19 04:57:18 +0000123 }
124}
125
Pierre Ossmanb948a912014-01-15 13:23:43 +0100126void TightEncoder::setFineQualityLevel(int quality, int subsampling)
DRCb4a83232011-08-19 04:57:18 +0000127{
Pierre Ossmanb948a912014-01-15 13:23:43 +0100128 jpegQuality = quality;
129 jpegSubsampling = subsampling;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130}
131
DRCffe09d62011-08-17 02:27:59 +0000132bool TightEncoder::checkSolidTile(Rect& r, rdr::U32* colorPtr,
DRCcd2c5d42011-08-11 11:18:34 +0000133 bool needSameColor)
134{
DRCffe09d62011-08-17 02:27:59 +0000135 switch (serverpf.bpp) {
DRCcd2c5d42011-08-11 11:18:34 +0000136 case 32:
DRCffe09d62011-08-17 02:27:59 +0000137 return checkSolidTile32(r, colorPtr, needSameColor);
DRCcd2c5d42011-08-11 11:18:34 +0000138 case 16:
DRCffe09d62011-08-17 02:27:59 +0000139 return checkSolidTile16(r, colorPtr, needSameColor);
DRCcd2c5d42011-08-11 11:18:34 +0000140 default:
DRCffe09d62011-08-17 02:27:59 +0000141 return checkSolidTile8(r, colorPtr, needSameColor);
DRCcd2c5d42011-08-11 11:18:34 +0000142 }
143}
144
DRCffe09d62011-08-17 02:27:59 +0000145void TightEncoder::findBestSolidArea(Rect& r, rdr::U32 colorValue, Rect& bestr)
DRCcd2c5d42011-08-11 11:18:34 +0000146{
147 int dx, dy, dw, dh;
148 int w_prev;
149 Rect sr;
150 int w_best = 0, h_best = 0;
151
152 bestr.tl.x = bestr.br.x = r.tl.x;
153 bestr.tl.y = bestr.br.y = r.tl.y;
154
155 w_prev = r.width();
156
157 for (dy = r.tl.y; dy < r.br.y; dy += TIGHT_MAX_SPLIT_TILE_SIZE) {
158
159 dh = (dy + TIGHT_MAX_SPLIT_TILE_SIZE <= r.br.y) ?
160 TIGHT_MAX_SPLIT_TILE_SIZE : (r.br.y - dy);
161 dw = (w_prev > TIGHT_MAX_SPLIT_TILE_SIZE) ?
162 TIGHT_MAX_SPLIT_TILE_SIZE : w_prev;
163
164 sr.setXYWH(r.tl.x, dy, dw, dh);
DRCffe09d62011-08-17 02:27:59 +0000165 if (!checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000166 break;
167
168 for (dx = r.tl.x + dw; dx < r.tl.x + w_prev;) {
169 dw = (dx + TIGHT_MAX_SPLIT_TILE_SIZE <= r.tl.x + w_prev) ?
170 TIGHT_MAX_SPLIT_TILE_SIZE : (r.tl.x + w_prev - dx);
171 sr.setXYWH(dx, dy, dw, dh);
DRCffe09d62011-08-17 02:27:59 +0000172 if (!checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000173 break;
DRCffe09d62011-08-17 02:27:59 +0000174 dx += dw;
DRCcd2c5d42011-08-11 11:18:34 +0000175 }
176
177 w_prev = dx - r.tl.x;
178 if (w_prev * (dy + dh - r.tl.y) > w_best * h_best) {
179 w_best = w_prev;
180 h_best = dy + dh - r.tl.y;
181 }
182 }
183
184 bestr.br.x = bestr.tl.x + w_best;
185 bestr.br.y = bestr.tl.y + h_best;
186}
187
DRCffe09d62011-08-17 02:27:59 +0000188void TightEncoder::extendSolidArea(const Rect& r, rdr::U32 colorValue,
189 Rect& er)
DRCcd2c5d42011-08-11 11:18:34 +0000190{
191 int cx, cy;
192 Rect sr;
193
194 // Try to extend the area upwards.
195 for (cy = er.tl.y - 1; ; cy--) {
196 sr.setXYWH(er.tl.x, cy, er.width(), 1);
DRCffe09d62011-08-17 02:27:59 +0000197 if (cy < r.tl.y || !checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000198 break;
199 }
200 er.tl.y = cy + 1;
201
202 // ... downwards.
203 for (cy = er.br.y; ; cy++) {
204 sr.setXYWH(er.tl.x, cy, er.width(), 1);
DRCffe09d62011-08-17 02:27:59 +0000205 if (cy >= r.br.y || !checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000206 break;
207 }
208 er.br.y = cy;
209
210 // ... to the left.
211 for (cx = er.tl.x - 1; ; cx--) {
212 sr.setXYWH(cx, er.tl.y, 1, er.height());
DRCffe09d62011-08-17 02:27:59 +0000213 if (cx < r.tl.x || !checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000214 break;
215 }
216 er.tl.x = cx + 1;
217
218 // ... to the right.
219 for (cx = er.br.x; ; cx++) {
220 sr.setXYWH(cx, er.tl.y, 1, er.height());
DRCffe09d62011-08-17 02:27:59 +0000221 if (cx >= r.br.x || !checkSolidTile(sr, &colorValue, true))
DRCcd2c5d42011-08-11 11:18:34 +0000222 break;
223 }
224 er.br.x = cx;
225}
226
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000227int TightEncoder::getNumRects(const Rect &r)
228{
DRCcd2c5d42011-08-11 11:18:34 +0000229 ConnParams* cp = writer->getConnParams();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000230 const unsigned int w = r.width();
231 const unsigned int h = r.height();
232
DRCcd2c5d42011-08-11 11:18:34 +0000233 // If last rect. encoding is enabled, we can use the higher-performance
234 // code that pre-computes solid rectangles. In that case, we don't care
235 // about the rectangle count.
236 if (cp->supportsLastRect && w * h >= TIGHT_MIN_SPLIT_RECT_SIZE)
237 return 0;
238
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239 // Will this rectangle split into subrects?
240 bool rectTooBig = w > pconf->maxRectWidth || w * h > pconf->maxRectSize;
241 if (!rectTooBig)
242 return 1;
243
244 // Compute max sub-rectangle size.
245 const unsigned int subrectMaxWidth =
246 (w > pconf->maxRectWidth) ? pconf->maxRectWidth : w;
247 const unsigned int subrectMaxHeight =
248 pconf->maxRectSize / subrectMaxWidth;
249
250 // Return the number of subrects.
251 return (((w - 1) / pconf->maxRectWidth + 1) *
252 ((h - 1) / subrectMaxHeight + 1));
253}
254
DRCffe09d62011-08-17 02:27:59 +0000255void TightEncoder::sendRectSimple(const Rect& r)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000256{
257 // Shortcuts to rectangle coordinates and dimensions.
258 const int x = r.tl.x;
259 const int y = r.tl.y;
260 const unsigned int w = r.width();
261 const unsigned int h = r.height();
262
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000263 // Encode small rects as is.
264 bool rectTooBig = w > pconf->maxRectWidth || w * h > pconf->maxRectSize;
265 if (!rectTooBig) {
DRCffe09d62011-08-17 02:27:59 +0000266 writeSubrect(r);
DRCcd2c5d42011-08-11 11:18:34 +0000267 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000268 }
269
270 // Compute max sub-rectangle size.
271 const unsigned int subrectMaxWidth =
272 (w > pconf->maxRectWidth) ? pconf->maxRectWidth : w;
273 const unsigned int subrectMaxHeight =
274 pconf->maxRectSize / subrectMaxWidth;
275
276 // Split big rects into separately encoded subrects.
277 Rect sr;
278 unsigned int dx, dy, sw, sh;
279 for (dy = 0; dy < h; dy += subrectMaxHeight) {
280 for (dx = 0; dx < w; dx += pconf->maxRectWidth) {
281 sw = (dx + pconf->maxRectWidth < w) ? pconf->maxRectWidth : w - dx;
282 sh = (dy + subrectMaxHeight < h) ? subrectMaxHeight : h - dy;
283 sr.setXYWH(x + dx, y + dy, sw, sh);
DRCffe09d62011-08-17 02:27:59 +0000284 writeSubrect(sr);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000285 }
286 }
DRCcd2c5d42011-08-11 11:18:34 +0000287}
288
Pierre Ossman717c07b2014-01-21 14:45:10 +0100289void TightEncoder::writeRect(const Rect& _r, TransImageGetter* _ig)
DRCcd2c5d42011-08-11 11:18:34 +0000290{
DRCffe09d62011-08-17 02:27:59 +0000291 ig = _ig;
292 serverpf = ig->getPixelBuffer()->getPF();
DRCcd2c5d42011-08-11 11:18:34 +0000293 ConnParams* cp = writer->getConnParams();
DRCffe09d62011-08-17 02:27:59 +0000294 clientpf = cp->pf();
DRCcd2c5d42011-08-11 11:18:34 +0000295
296 // Shortcuts to rectangle coordinates and dimensions.
297 Rect r = _r;
298 int x = r.tl.x;
299 int y = r.tl.y;
DRCe3ffcf72011-08-19 03:11:32 +0000300 int w = r.width();
301 int h = r.height();
DRCcd2c5d42011-08-11 11:18:34 +0000302
DRCcd2c5d42011-08-11 11:18:34 +0000303 // Encode small rects as is.
304 if (!cp->supportsLastRect || w * h < TIGHT_MIN_SPLIT_RECT_SIZE) {
DRCffe09d62011-08-17 02:27:59 +0000305 sendRectSimple(r);
Pierre Ossman717c07b2014-01-21 14:45:10 +0100306 return;
DRCcd2c5d42011-08-11 11:18:34 +0000307 }
308
309 // Split big rects into separately encoded subrects.
310 Rect sr, bestr;
DRCe3ffcf72011-08-19 03:11:32 +0000311 int dx, dy, dw, dh;
DRCcd2c5d42011-08-11 11:18:34 +0000312 rdr::U32 colorValue;
DRCffe09d62011-08-17 02:27:59 +0000313 int maxRectWidth = pconf->maxRectWidth;
DRCcd2c5d42011-08-11 11:18:34 +0000314 int nMaxWidth = (w > maxRectWidth) ? maxRectWidth : w;
DRCffe09d62011-08-17 02:27:59 +0000315 int nMaxRows = pconf->maxRectSize / nMaxWidth;
DRCcd2c5d42011-08-11 11:18:34 +0000316
317 // Try to find large solid-color areas and send them separately.
318 for (dy = y; dy < y + h; dy += TIGHT_MAX_SPLIT_TILE_SIZE) {
319
320 // If a rectangle becomes too large, send its upper part now.
321 if (dy - y >= nMaxRows) {
322 sr.setXYWH(x, y, w, nMaxRows);
DRCffe09d62011-08-17 02:27:59 +0000323 sendRectSimple(sr);
DRCcd2c5d42011-08-11 11:18:34 +0000324 r.tl.y += nMaxRows;
325 y = r.tl.y;
326 h = r.height();
327 }
328
329 dh = (dy + TIGHT_MAX_SPLIT_TILE_SIZE <= y + h) ?
330 TIGHT_MAX_SPLIT_TILE_SIZE : (y + h - dy);
331
332 for (dx = x; dx < x + w; dx += TIGHT_MAX_SPLIT_TILE_SIZE) {
333
334 dw = (dx + TIGHT_MAX_SPLIT_TILE_SIZE <= x + w) ?
335 TIGHT_MAX_SPLIT_TILE_SIZE : (x + w - dx);
336
337 sr.setXYWH(dx, dy, dw, dh);
DRCffe09d62011-08-17 02:27:59 +0000338 if (checkSolidTile(sr, &colorValue, false)) {
DRCcd2c5d42011-08-11 11:18:34 +0000339
Pierre Ossmanb948a912014-01-15 13:23:43 +0100340 if (jpegSubsampling == subsampleGray && jpegQuality != -1) {
DRCb4a83232011-08-19 04:57:18 +0000341 Colour rgb;
342 serverpf.rgbFromPixel(colorValue, NULL, &rgb);
343 rdr::U32 lum = ((257 * rgb.r) + (504 * rgb.g) + (98 * rgb.b)
344 + 16500) / 1000;
345 colorValue = lum + (lum << 8) + (lum << 16);
346 }
347
DRCcd2c5d42011-08-11 11:18:34 +0000348 // Get dimensions of solid-color area.
349 sr.setXYWH(dx, dy, r.br.x - dx, r.br.y - dy);
DRCffe09d62011-08-17 02:27:59 +0000350 findBestSolidArea(sr, colorValue, bestr);
DRCcd2c5d42011-08-11 11:18:34 +0000351
352 // Make sure a solid rectangle is large enough
353 // (or the whole rectangle is of the same color).
354 if (bestr.area() != r.area()
355 && bestr.area() < TIGHT_MIN_SOLID_SUBRECT_SIZE)
356 continue;
357
358 // Try to extend solid rectangle to maximum size.
DRCffe09d62011-08-17 02:27:59 +0000359 extendSolidArea(r, colorValue, bestr);
DRCcd2c5d42011-08-11 11:18:34 +0000360
361 // Send rectangles at top and left to solid-color area.
362 if (bestr.tl.y != y) {
363 sr.setXYWH(x, y, w, bestr.tl.y - y);
DRCffe09d62011-08-17 02:27:59 +0000364 sendRectSimple(sr);
DRCcd2c5d42011-08-11 11:18:34 +0000365 }
366 if (bestr.tl.x != x) {
367 sr.setXYWH(x, bestr.tl.y, bestr.tl.x - x, bestr.height());
Pierre Ossman717c07b2014-01-21 14:45:10 +0100368 writeRect(sr, _ig);
DRCcd2c5d42011-08-11 11:18:34 +0000369 }
370
371 // Send solid-color rectangle.
DRCffe09d62011-08-17 02:27:59 +0000372 writeSubrect(bestr, true);
DRCcd2c5d42011-08-11 11:18:34 +0000373
374 // Send remaining rectangles (at right and bottom).
375 if (bestr.br.x != r.br.x) {
376 sr.setXYWH(bestr.br.x, bestr.tl.y, r.br.x - bestr.br.x,
377 bestr.height());
Pierre Ossman717c07b2014-01-21 14:45:10 +0100378 writeRect(sr, _ig);
DRCcd2c5d42011-08-11 11:18:34 +0000379 }
380 if (bestr.br.y != r.br.y) {
381 sr.setXYWH(x, bestr.br.y, w, r.br.y - bestr.br.y);
Pierre Ossman717c07b2014-01-21 14:45:10 +0100382 writeRect(sr, _ig);
DRCcd2c5d42011-08-11 11:18:34 +0000383 }
384
Pierre Ossman717c07b2014-01-21 14:45:10 +0100385 return;
DRCcd2c5d42011-08-11 11:18:34 +0000386 }
387 }
388 }
389
390 // No suitable solid-color rectangles found.
DRCffe09d62011-08-17 02:27:59 +0000391 sendRectSimple(r);
Pierre Ossman717c07b2014-01-21 14:45:10 +0100392 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000393}
394
DRCffe09d62011-08-17 02:27:59 +0000395void TightEncoder::writeSubrect(const Rect& r, bool forceSolid)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000396{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000397 mos.clear();
398
DRCffe09d62011-08-17 02:27:59 +0000399 switch (clientpf.bpp) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000400 case 8:
DRCffe09d62011-08-17 02:27:59 +0000401 tightEncode8(r, &mos, forceSolid); break;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000402 case 16:
DRCffe09d62011-08-17 02:27:59 +0000403 tightEncode16(r, &mos, forceSolid); break;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000404 case 32:
DRCffe09d62011-08-17 02:27:59 +0000405 tightEncode32(r, &mos, forceSolid); break;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000406 }
407
408 writer->startRect(r, encodingTight);
409 rdr::OutStream* os = writer->getOutStream();
410 os->writeBytes(mos.data(), mos.length());
411 writer->endRect();
412}