blob: 38cb4eb72dd3128cce7aec47fb8394f870791ef3 [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) :
Pierre Ossmanbeab2b62018-09-20 10:52:15 +020067 Encoder(conn, encodingTight,
68 (EncoderFlags)(EncoderUseNativePF | EncoderLossy), -1, 9),
Pierre Ossmanc0397262014-03-14 15:59:46 +010069 qualityLevel(-1), fineQuality(-1), fineSubsampling(subsampleUndefined)
70{
71}
72
73TightJPEGEncoder::~TightJPEGEncoder()
74{
75}
76
77bool TightJPEGEncoder::isSupported()
78{
79 if (!conn->cp.supportsEncoding(encodingTight))
80 return false;
81
82 // Any one of these indicates support for JPEG
83 if (conn->cp.qualityLevel != -1)
84 return true;
85 if (conn->cp.fineQualityLevel != -1)
86 return true;
87 if (conn->cp.subsampling != -1)
88 return true;
89
90 // Tight support, but not JPEG
91 return false;
92}
93
94void TightJPEGEncoder::setQualityLevel(int level)
95{
96 qualityLevel = level;
97}
98
99void TightJPEGEncoder::setFineQualityLevel(int quality, int subsampling)
100{
101 fineQuality = quality;
102 fineSubsampling = subsampling;
103}
104
Pierre Ossmanab9fd6b2018-09-20 10:51:00 +0200105int TightJPEGEncoder::getQualityLevel()
106{
107 return qualityLevel;
108}
109
Pierre Ossmanc0397262014-03-14 15:59:46 +0100110void TightJPEGEncoder::writeRect(const PixelBuffer* pb, const Palette& palette)
111{
112 const rdr::U8* buffer;
113 int stride;
114
115 int quality, subsampling;
116
117 rdr::OutStream* os;
118
119 buffer = pb->getBuffer(pb->getRect(), &stride);
120
121 if (qualityLevel >= 0 && qualityLevel <= 9) {
122 quality = conf[qualityLevel].quality;
123 subsampling = conf[qualityLevel].subsampling;
124 } else {
125 quality = -1;
126 subsampling = subsampleUndefined;
127 }
128
129 // Fine settings trump level
130 if (fineQuality != -1)
131 quality = fineQuality;
132 if (fineSubsampling != subsampleUndefined)
133 subsampling = fineSubsampling;
134
135 jc.clear();
136 jc.compress(buffer, stride, pb->getRect(),
137 pb->getPF(), quality, subsampling);
138
139 os = conn->getOutStream();
140
141 os->writeU8(tightJpeg << 4);
142
143 writeCompact(jc.length(), os);
144 os->writeBytes(jc.data(), jc.length());
145}
146
147void TightJPEGEncoder::writeSolidRect(int width, int height,
148 const PixelFormat& pf,
149 const rdr::U8* colour)
150{
151 // FIXME: Add a shortcut in the JPEG compressor to handle this case
152 // without having to use the default fallback which is very slow.
153 Encoder::writeSolidRect(width, height, pf, colour);
154}
155
156void TightJPEGEncoder::writeCompact(rdr::U32 value, rdr::OutStream* os)
157{
158 // Copied from TightEncoder as it's overkill to inherit just for this
159 rdr::U8 b;
160
161 b = value & 0x7F;
162 if (value <= 0x7F) {
163 os->writeU8(b);
164 } else {
165 os->writeU8(b | 0x80);
166 b = value >> 7 & 0x7F;
167 if (value <= 0x3FFF) {
168 os->writeU8(b);
169 } else {
170 os->writeU8(b | 0x80);
171 os->writeU8(value >> 14 & 0xFF);
172 }
173 }
174}