blob: 1d9b3c79f0337c649693fcabd25023a8f2bf41eb [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
19#include <rfb/JpegEncoder.h>
20#include <rdr/OutStream.h>
Constantin Kaplinsky651606d2007-10-17 17:40:23 +000021#include <rdr/Exception.h>
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000022#include <rfb/encodings.h>
Constantin Kaplinsky651606d2007-10-17 17:40:23 +000023#include <rfb/ConnParams.h>
Constantin Kaplinsky48039462007-10-10 04:44:54 +000024#include <rfb/LogWriter.h>
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000025
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000026#ifdef HAVE_CL
27#include <rfb/IrixCLJpegCompressor.h>
28#endif
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000029#ifdef HAVE_DMEDIA
30#include <rfb/IrixDMJpegCompressor.h>
31#endif
32
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000033using namespace rfb;
34
Constantin Kaplinsky48039462007-10-10 04:44:54 +000035static LogWriter vlog("JpegEncoder");
36
37BoolParameter JpegEncoder::useHardwareJPEG
38("UseHardwareJPEG",
39 "Use hardware-accelerated JPEG compressor for video if available",
40 true);
41
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000042const int JpegEncoder::qualityMap[10] = {
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000043 2, 10, 15, 25, 37, 50, 60, 70, 80, 90
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000044};
45
Constantin Kaplinsky48039462007-10-10 04:44:54 +000046JpegEncoder::JpegEncoder(SMsgWriter* writer_) : writer(writer_), jcomp(0)
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000047{
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000048 // FIXME: DM should be preferred over CL.
49#ifdef HAVE_CL
50 if (!jcomp && useHardwareJPEG) {
51 vlog.debug("trying IRIX CL JPEG compressor");
52 IrixCLJpegCompressor *clComp = new IrixCLJpegCompressor;
53 if (clComp->isValid()) {
54 vlog.debug("initialized IRIX CL JPEG compressor successfully");
55 jcomp = clComp;
Constantin Kaplinsky48039462007-10-10 04:44:54 +000056 } else {
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000057 vlog.error("warning: could not create IRIX CL JPEG compressor");
58 delete clComp;
Constantin Kaplinsky48039462007-10-10 04:44:54 +000059 }
60 }
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000061#endif
62#ifdef HAVE_DMEDIA
63 if (!jcomp && useHardwareJPEG) {
64 vlog.debug("trying IRIX DM JPEG compressor");
65 IrixDMJpegCompressor *dmComp = new IrixDMJpegCompressor;
66 if (dmComp->isValid()) {
67 vlog.debug("initialized IRIX DM JPEG compressor successfully");
68 jcomp = dmComp;
69 } else {
70 vlog.error("warning: could not create IRIX DM JPEG compressor");
71 delete dmComp;
72 }
Constantin Kaplinsky48039462007-10-10 04:44:54 +000073 }
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000074#endif
Constantin Kaplinsky48039462007-10-10 04:44:54 +000075 if (!jcomp) {
Constantin Kaplinsky51e66522007-12-19 06:25:06 +000076 if (useHardwareJPEG) {
77 vlog.info("no hardware JPEG compressor available");
78 }
Constantin Kaplinsky48039462007-10-10 04:44:54 +000079 vlog.debug("using software JPEG compressor");
80 jcomp = new StandardJpegCompressor;
81 }
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000082 jcomp->setQuality(qualityMap[6]);
83}
84
85JpegEncoder::~JpegEncoder()
86{
87 delete jcomp;
88}
89
90void JpegEncoder::setQualityLevel(int level)
91{
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000092 if (level < 0) {
93 level = 0;
94 } else if (level > 9) {
95 level = 9;
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000096 }
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000097 jcomp->setQuality(qualityMap[level]);
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000098}
99
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000100bool JpegEncoder::isPixelFormatSupported(PixelBuffer* pb) const
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000101{
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000102 const PixelFormat &serverPF = pb->getPF();
103 const PixelFormat &clientPF = writer->getConnParams()->pf();
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000104
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000105 // FIXME: Ask encoders if they support given pixel formats.
106
107 if ( serverPF.bpp == 32 && clientPF.bpp >= 16 &&
108 serverPF.depth == 24 && serverPF.redMax == 255 &&
109 serverPF.greenMax == 255 && serverPF.blueMax == 255 ) {
110 return true;
111 }
112
113 return false;
114}
115
116void JpegEncoder::writeRect(PixelBuffer* pb, const Rect& r)
117{
118 if (!isPixelFormatSupported(pb)) {
119 vlog.error("pixel format unsupported by JPEG encoder");
120 throw rdr::Exception("internal error in JpegEncoder");
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000121 }
122
123 writer->startRect(r, encodingTight);
124 rdr::OutStream* os = writer->getOutStream();
125
126 // Get access to pixel data
127 int stride;
128 const rdr::U32* pixels = (const rdr::U32 *)pb->getPixelsR(r, &stride);
129 const PixelFormat& fmt = pb->getPF();
130
131 // Encode data
132 jcomp->compress(pixels, &fmt, r.width(), r.height(), stride);
133
134 // Write Tight-encoded header and JPEG data.
135 os->writeU8(0x09 << 4);
136 os->writeCompactLength(jcomp->getDataLength());
137 os->writeBytes(jcomp->getDataPtr(), jcomp->getDataLength());
138
139 writer->endRect();
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000140}
141