aboutsummaryrefslogtreecommitdiffstats
path: root/common/rfb/TightJPEGEncoder.cxx
blob: de8fd77f1f0d048ad448dea28b16c7f9e6b481e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright (C) 2000-2003 Constantin Kaplinsky.  All Rights Reserved.
 * Copyright (C) 2011 D. R. Commander.  All Rights Reserved.
 * Copyright 2014 Pierre Ossman for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <rdr/OutStream.h>
#include <rfb/encodings.h>
#include <rfb/SConnection.h>
#include <rfb/PixelBuffer.h>
#include <rfb/TightJPEGEncoder.h>
#include <rfb/TightConstants.h>

using namespace rfb;

struct TightJPEGConfiguration {
    int quality;
    int subsampling;
};

// NOTE:  The JPEG quality and subsampling levels below were obtained
// experimentally by the VirtualGL Project.  They represent the approximate
// average compression ratios listed below, as measured across the set of
// every 10th frame in the SPECviewperf 9 benchmark suite.
//
// 9 = JPEG quality 100, no subsampling (ratio ~= 10:1)
//     [this should be lossless, except for round-off error]
// 8 = JPEG quality 92,  no subsampling (ratio ~= 20:1)
//     [this should be perceptually lossless, based on current research]
// 7 = JPEG quality 86,  no subsampling (ratio ~= 25:1)
// 6 = JPEG quality 79,  no subsampling (ratio ~= 30:1)
// 5 = JPEG quality 77,  4:2:2 subsampling (ratio ~= 40:1)
// 4 = JPEG quality 62,  4:2:2 subsampling (ratio ~= 50:1)
// 3 = JPEG quality 42,  4:2:2 subsampling (ratio ~= 60:1)
// 2 = JPEG quality 41,  4:2:0 subsampling (ratio ~= 70:1)
// 1 = JPEG quality 29,  4:2:0 subsampling (ratio ~= 80:1)
// 0 = JPEG quality 15,  4:2:0 subsampling (ratio ~= 100:1)

static const struct TightJPEGConfiguration conf[10] = {
  {  15, subsample4X }, // 0
  {  29, subsample4X }, // 1
  {  41, subsample4X }, // 2
  {  42, subsample2X }, // 3
  {  62, subsample2X }, // 4
  {  77, subsample2X }, // 5
  {  79, subsampleNone }, // 6
  {  86, subsampleNone }, // 7
  {  92, subsampleNone }, // 8
  { 100, subsampleNone }  // 9
};


TightJPEGEncoder::TightJPEGEncoder(SConnection* conn_) :
  Encoder(conn_, encodingTight,
          (EncoderFlags)(EncoderUseNativePF | EncoderLossy), -1, 9),
  qualityLevel(-1), fineQuality(-1), fineSubsampling(subsampleUndefined)
{
}

TightJPEGEncoder::~TightJPEGEncoder()
{
}

bool TightJPEGEncoder::isSupported()
{
  if (!conn->client.supportsEncoding(encodingTight))
    return false;

  // Any one of these indicates support for JPEG
  if (conn->client.qualityLevel != -1)
    return true;
  if (conn->client.fineQualityLevel != -1)
    return true;
  if (conn->client.subsampling != -1)
    return true;

  // Tight support, but not JPEG
  return false;
}

void TightJPEGEncoder::setQualityLevel(int level)
{
  qualityLevel = level;
}

void TightJPEGEncoder::setFineQualityLevel(int quality, int subsampling)
{
  fineQuality = quality;
  fineSubsampling = subsampling;
}

int TightJPEGEncoder::getQualityLevel()
{
  return qualityLevel;
}

void TightJPEGEncoder::writeRect(const PixelBuffer* pb,
                                 const Palette& /*palette*/)
{
  const uint8_t* buffer;
  int stride;

  int quality, subsampling;

  rdr::OutStream* os;

  buffer = pb->getBuffer(pb->getRect(), &stride);

  if (qualityLevel >= 0 && qualityLevel <= 9) {
    quality = conf[qualityLevel].quality;
    subsampling = conf[qualityLevel].subsampling;
  } else {
    quality = -1;
    subsampling = subsampleUndefined;
  }

  // Fine settings trump level
  if (fineQuality != -1)
    quality = fineQuality;
  if (fineSubsampling != subsampleUndefined)
    subsampling = fineSubsampling;

  jc.clear();
  jc.compress(buffer, stride, pb->getRect(),
              pb->getPF(), quality, subsampling);

  os = conn->getOutStream();

  os->writeU8(tightJpeg << 4);

  writeCompact(jc.length(), os);
  os->writeBytes(jc.data(), jc.length());
}

void TightJPEGEncoder::writeSolidRect(int width, int height,
                                      const PixelFormat& pf,
                                      const uint8_t* colour)
{
  // FIXME: Add a shortcut in the JPEG compressor to handle this case
  //        without having to use the default fallback which is very slow.
  Encoder::writeSolidRect(width, height, pf, colour);
}

void TightJPEGEncoder::writeCompact(uint32_t value, rdr::OutStream* os)
{
  // Copied from TightEncoder as it's overkill to inherit just for this
  uint8_t b;

  b = value & 0x7F;
  if (value <= 0x7F) {
    os->writeU8(b);
  } else {
    os->writeU8(b | 0x80);
    b = value >> 7 & 0x7F;
    if (value <= 0x3FFF) {
      os->writeU8(b);
    } else {
      os->writeU8(b | 0x80);
      os->writeU8(value >> 14 & 0xFF);
    }
  }
}