blob: 7bb61265db7531c7e7a559c499ff572cb56a67d0 [file] [log] [blame]
Pierre Ossmanc0397262014-03-14 15:59:46 +01001/* 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 <rdr/OutStream.h>
21#include <rfb/encodings.h>
22#include <rfb/SConnection.h>
23#include <rfb/PixelBuffer.h>
24#include <rfb/TightJPEGEncoder.h>
25#include <rfb/TightConstants.h>
26
27using namespace rfb;
28
29struct TightJPEGConfiguration {
30 int quality;
31 int subsampling;
32};
33
34// NOTE: The JPEG quality and subsampling levels below were obtained
35// experimentally by the VirtualGL Project. They represent the approximate
36// average compression ratios listed below, as measured across the set of
37// every 10th frame in the SPECviewperf 9 benchmark suite.
38//
39// 9 = JPEG quality 100, no subsampling (ratio ~= 10:1)
40// [this should be lossless, except for round-off error]
41// 8 = JPEG quality 92, no subsampling (ratio ~= 20:1)
42// [this should be perceptually lossless, based on current research]
43// 7 = JPEG quality 86, no subsampling (ratio ~= 25:1)
44// 6 = JPEG quality 79, no subsampling (ratio ~= 30:1)
45// 5 = JPEG quality 77, 4:2:2 subsampling (ratio ~= 40:1)
46// 4 = JPEG quality 62, 4:2:2 subsampling (ratio ~= 50:1)
47// 3 = JPEG quality 42, 4:2:2 subsampling (ratio ~= 60:1)
48// 2 = JPEG quality 41, 4:2:0 subsampling (ratio ~= 70:1)
49// 1 = JPEG quality 29, 4:2:0 subsampling (ratio ~= 80:1)
50// 0 = JPEG quality 15, 4:2:0 subsampling (ratio ~= 100:1)
51
52static const struct TightJPEGConfiguration conf[10] = {
53 { 15, subsample4X }, // 0
54 { 29, subsample4X }, // 1
55 { 41, subsample4X }, // 2
56 { 42, subsample2X }, // 3
57 { 62, subsample2X }, // 4
58 { 77, subsample2X }, // 5
59 { 79, subsampleNone }, // 6
60 { 86, subsampleNone }, // 7
61 { 92, subsampleNone }, // 8
62 { 100, subsampleNone } // 9
63};
64
65
66TightJPEGEncoder::TightJPEGEncoder(SConnection* conn) :
67 Encoder(conn, encodingTight, EncoderUseNativePF, -1),
68 qualityLevel(-1), fineQuality(-1), fineSubsampling(subsampleUndefined)
69{
70}
71
72TightJPEGEncoder::~TightJPEGEncoder()
73{
74}
75
76bool TightJPEGEncoder::isSupported()
77{
78 if (!conn->cp.supportsEncoding(encodingTight))
79 return false;
80
81 // Any one of these indicates support for JPEG
82 if (conn->cp.qualityLevel != -1)
83 return true;
84 if (conn->cp.fineQualityLevel != -1)
85 return true;
86 if (conn->cp.subsampling != -1)
87 return true;
88
89 // Tight support, but not JPEG
90 return false;
91}
92
93void TightJPEGEncoder::setQualityLevel(int level)
94{
95 qualityLevel = level;
96}
97
98void TightJPEGEncoder::setFineQualityLevel(int quality, int subsampling)
99{
100 fineQuality = quality;
101 fineSubsampling = subsampling;
102}
103
104void TightJPEGEncoder::writeRect(const PixelBuffer* pb, const Palette& palette)
105{
106 const rdr::U8* buffer;
107 int stride;
108
109 int quality, subsampling;
110
111 rdr::OutStream* os;
112
113 buffer = pb->getBuffer(pb->getRect(), &stride);
114
115 if (qualityLevel >= 0 && qualityLevel <= 9) {
116 quality = conf[qualityLevel].quality;
117 subsampling = conf[qualityLevel].subsampling;
118 } else {
119 quality = -1;
120 subsampling = subsampleUndefined;
121 }
122
123 // Fine settings trump level
124 if (fineQuality != -1)
125 quality = fineQuality;
126 if (fineSubsampling != subsampleUndefined)
127 subsampling = fineSubsampling;
128
129 jc.clear();
130 jc.compress(buffer, stride, pb->getRect(),
131 pb->getPF(), quality, subsampling);
132
133 os = conn->getOutStream();
134
135 os->writeU8(tightJpeg << 4);
136
137 writeCompact(jc.length(), os);
138 os->writeBytes(jc.data(), jc.length());
139}
140
141void TightJPEGEncoder::writeSolidRect(int width, int height,
142 const PixelFormat& pf,
143 const rdr::U8* colour)
144{
145 // FIXME: Add a shortcut in the JPEG compressor to handle this case
146 // without having to use the default fallback which is very slow.
147 Encoder::writeSolidRect(width, height, pf, colour);
148}
149
150void TightJPEGEncoder::writeCompact(rdr::U32 value, rdr::OutStream* os)
151{
152 // Copied from TightEncoder as it's overkill to inherit just for this
153 rdr::U8 b;
154
155 b = value & 0x7F;
156 if (value <= 0x7F) {
157 os->writeU8(b);
158 } else {
159 os->writeU8(b | 0x80);
160 b = value >> 7 & 0x7F;
161 if (value <= 0x3FFF) {
162 os->writeU8(b);
163 } else {
164 os->writeU8(b | 0x80);
165 os->writeU8(value >> 14 & 0xFF);
166 }
167 }
168}