blob: bbe4f8fa4de44cbb84a9f68c48e7d475af3f2213 [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 Ossman4694d882018-09-20 10:49:40 +020067 Encoder(conn, encodingTight, (EncoderFlags)(EncoderUseNativePF | EncoderLossy)),
Pierre Ossmanc0397262014-03-14 15:59:46 +010068 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
Pierre Ossmanab9fd6b2018-09-20 10:51:00 +0200104int TightJPEGEncoder::getQualityLevel()
105{
106 return qualityLevel;
107}
108
Pierre Ossmanc0397262014-03-14 15:59:46 +0100109void TightJPEGEncoder::writeRect(const PixelBuffer* pb, const Palette& palette)
110{
111 const rdr::U8* buffer;
112 int stride;
113
114 int quality, subsampling;
115
116 rdr::OutStream* os;
117
118 buffer = pb->getBuffer(pb->getRect(), &stride);
119
120 if (qualityLevel >= 0 && qualityLevel <= 9) {
121 quality = conf[qualityLevel].quality;
122 subsampling = conf[qualityLevel].subsampling;
123 } else {
124 quality = -1;
125 subsampling = subsampleUndefined;
126 }
127
128 // Fine settings trump level
129 if (fineQuality != -1)
130 quality = fineQuality;
131 if (fineSubsampling != subsampleUndefined)
132 subsampling = fineSubsampling;
133
134 jc.clear();
135 jc.compress(buffer, stride, pb->getRect(),
136 pb->getPF(), quality, subsampling);
137
138 os = conn->getOutStream();
139
140 os->writeU8(tightJpeg << 4);
141
142 writeCompact(jc.length(), os);
143 os->writeBytes(jc.data(), jc.length());
144}
145
146void TightJPEGEncoder::writeSolidRect(int width, int height,
147 const PixelFormat& pf,
148 const rdr::U8* colour)
149{
150 // FIXME: Add a shortcut in the JPEG compressor to handle this case
151 // without having to use the default fallback which is very slow.
152 Encoder::writeSolidRect(width, height, pf, colour);
153}
154
155void TightJPEGEncoder::writeCompact(rdr::U32 value, rdr::OutStream* os)
156{
157 // Copied from TightEncoder as it's overkill to inherit just for this
158 rdr::U8 b;
159
160 b = value & 0x7F;
161 if (value <= 0x7F) {
162 os->writeU8(b);
163 } else {
164 os->writeU8(b | 0x80);
165 b = value >> 7 & 0x7F;
166 if (value <= 0x3FFF) {
167 os->writeU8(b);
168 } else {
169 os->writeU8(b | 0x80);
170 os->writeU8(value >> 14 & 0xFF);
171 }
172 }
173}