blob: 2c47a604f2762b22a7163b30d1056bfb82833411 [file] [log] [blame]
Constantin Kaplinsky0b263232007-08-02 13:51:09 +00001/* Copyright (C) 2007 Constantin Kaplinsky. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
Adam Tkacad1cbd92008-10-06 14:08:00 +000019#ifdef HAVE_COMMON_CONFIG_H
20#include <common-config.h>
21#endif
22
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000023#include <rfb/JpegEncoder.h>
24#include <rdr/OutStream.h>
Constantin Kaplinsky651606d2007-10-17 17:40:23 +000025#include <rdr/Exception.h>
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000026#include <rfb/encodings.h>
Constantin Kaplinsky651606d2007-10-17 17:40:23 +000027#include <rfb/ConnParams.h>
Constantin Kaplinsky48039462007-10-10 04:44:54 +000028#include <rfb/LogWriter.h>
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000029
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000030#ifdef HAVE_CL
31#include <rfb/IrixCLJpegCompressor.h>
32#endif
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000033#ifdef HAVE_DMEDIA
34#include <rfb/IrixDMJpegCompressor.h>
35#endif
36
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000037using namespace rfb;
38
Constantin Kaplinsky48039462007-10-10 04:44:54 +000039static LogWriter vlog("JpegEncoder");
40
41BoolParameter JpegEncoder::useHardwareJPEG
42("UseHardwareJPEG",
43 "Use hardware-accelerated JPEG compressor for video if available",
44 true);
45
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000046const int JpegEncoder::qualityMap[10] = {
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000047 2, 10, 15, 25, 37, 50, 60, 70, 80, 90
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000048};
49
Constantin Kaplinsky48039462007-10-10 04:44:54 +000050JpegEncoder::JpegEncoder(SMsgWriter* writer_) : writer(writer_), jcomp(0)
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000051{
Constantin Kaplinsky1b738582007-12-20 15:30:20 +000052#ifdef HAVE_DMEDIA
53#ifdef DEBUG_FORCE_CL
54 vlog.debug("DEBUG: skipping IRIX DM JPEG compressor");
55#else
56 if (!jcomp && useHardwareJPEG) {
57 vlog.debug("trying IRIX DM JPEG compressor");
58 IrixDMJpegCompressor *dmComp = new IrixDMJpegCompressor;
59 if (dmComp->isValid()) {
60 vlog.debug("initialized IRIX DM JPEG compressor successfully");
61 jcomp = dmComp;
62 } else {
63 vlog.error("warning: could not create IRIX DM JPEG compressor");
64 delete dmComp;
65 }
66 }
67#endif
68#endif
69
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000070#ifdef HAVE_CL
71 if (!jcomp && useHardwareJPEG) {
72 vlog.debug("trying IRIX CL JPEG compressor");
73 IrixCLJpegCompressor *clComp = new IrixCLJpegCompressor;
74 if (clComp->isValid()) {
75 vlog.debug("initialized IRIX CL JPEG compressor successfully");
76 jcomp = clComp;
Constantin Kaplinsky48039462007-10-10 04:44:54 +000077 } else {
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000078 vlog.error("warning: could not create IRIX CL JPEG compressor");
79 delete clComp;
Constantin Kaplinsky48039462007-10-10 04:44:54 +000080 }
81 }
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000082#endif
Constantin Kaplinsky1b738582007-12-20 15:30:20 +000083
Constantin Kaplinsky48039462007-10-10 04:44:54 +000084 if (!jcomp) {
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000085 if (useHardwareJPEG) {
86 vlog.info("no hardware JPEG compressor available");
87 }
Constantin Kaplinsky48039462007-10-10 04:44:54 +000088 vlog.debug("using software JPEG compressor");
89 jcomp = new StandardJpegCompressor;
90 }
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000091 jcomp->setQuality(qualityMap[6]);
92}
93
94JpegEncoder::~JpegEncoder()
95{
96 delete jcomp;
97}
98
99void JpegEncoder::setQualityLevel(int level)
100{
Constantin Kaplinskyc2573702007-10-10 10:03:06 +0000101 if (level < 0) {
102 level = 0;
103 } else if (level > 9) {
104 level = 9;
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000105 }
Constantin Kaplinskyc2573702007-10-10 10:03:06 +0000106 jcomp->setQuality(qualityMap[level]);
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000107}
108
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000109bool JpegEncoder::isPixelFormatSupported(PixelBuffer* pb) const
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000110{
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000111 const PixelFormat &serverPF = pb->getPF();
112 const PixelFormat &clientPF = writer->getConnParams()->pf();
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000113
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000114 // FIXME: Ask encoders if they support given pixel formats.
115
116 if ( serverPF.bpp == 32 && clientPF.bpp >= 16 &&
117 serverPF.depth == 24 && serverPF.redMax == 255 &&
118 serverPF.greenMax == 255 && serverPF.blueMax == 255 ) {
119 return true;
120 }
121
122 return false;
123}
124
125void JpegEncoder::writeRect(PixelBuffer* pb, const Rect& r)
126{
127 if (!isPixelFormatSupported(pb)) {
128 vlog.error("pixel format unsupported by JPEG encoder");
129 throw rdr::Exception("internal error in JpegEncoder");
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000130 }
131
132 writer->startRect(r, encodingTight);
133 rdr::OutStream* os = writer->getOutStream();
134
135 // Get access to pixel data
136 int stride;
137 const rdr::U32* pixels = (const rdr::U32 *)pb->getPixelsR(r, &stride);
138 const PixelFormat& fmt = pb->getPF();
139
Constantin Kaplinsky67ebf002008-06-05 03:05:13 +0000140 // Try to encode data
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000141 jcomp->compress(pixels, &fmt, r.width(), r.height(), stride);
142
Constantin Kaplinsky67ebf002008-06-05 03:05:13 +0000143 // If not successful, switch to StandardJpegCompressor
144 if (jcomp->getDataLength() == 0) {
145 vlog.info("switching to standard software JPEG compressor");
146 delete jcomp;
147 jcomp = new StandardJpegCompressor;
148 jcomp->setQuality(qualityMap[6]);
Constantin Kaplinskye6658f92008-09-03 05:52:35 +0000149 jcomp->compress(pixels, &fmt, r.width(), r.height(), stride);
Constantin Kaplinsky67ebf002008-06-05 03:05:13 +0000150 }
151
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000152 // Write Tight-encoded header and JPEG data.
153 os->writeU8(0x09 << 4);
154 os->writeCompactLength(jcomp->getDataLength());
155 os->writeBytes(jcomp->getDataPtr(), jcomp->getDataLength());
156
157 writer->endRect();
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000158}
159