blob: 054a5ef32138a5ab3eb5a5d09944dd554dc7bf86 [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 Kaplinskyd37420a2007-09-04 09:08:10 +000026#ifdef HAVE_DMEDIA
27#include <rfb/IrixDMJpegCompressor.h>
28#endif
29
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000030using namespace rfb;
31
Constantin Kaplinsky48039462007-10-10 04:44:54 +000032static LogWriter vlog("JpegEncoder");
33
34BoolParameter JpegEncoder::useHardwareJPEG
35("UseHardwareJPEG",
36 "Use hardware-accelerated JPEG compressor for video if available",
37 true);
38
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000039const int JpegEncoder::qualityMap[10] = {
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000040 2, 10, 15, 25, 37, 50, 60, 70, 80, 90
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000041};
42
Constantin Kaplinsky48039462007-10-10 04:44:54 +000043JpegEncoder::JpegEncoder(SMsgWriter* writer_) : writer(writer_), jcomp(0)
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000044{
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000045#ifdef HAVE_DMEDIA
Constantin Kaplinsky48039462007-10-10 04:44:54 +000046 if (useHardwareJPEG) {
47 vlog.debug("trying IRIX DM JPEG compressor");
48 IrixDMJpegCompressor *irixComp = new IrixDMJpegCompressor;
49 if (irixComp->isValid()) {
50 vlog.debug("initialized IRIX DM JPEG compressor successfully");
51 jcomp = irixComp;
52 } else {
53 vlog.error("warning: could not create IRIX DM JPEG compressor");
54 delete irixComp;
55 }
56 }
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000057#else
Constantin Kaplinsky48039462007-10-10 04:44:54 +000058 if (useHardwareJPEG) {
59 vlog.info("no hardware JPEG compressor available");
60 }
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000061#endif
Constantin Kaplinsky48039462007-10-10 04:44:54 +000062 if (!jcomp) {
63 vlog.debug("using software JPEG compressor");
64 jcomp = new StandardJpegCompressor;
65 }
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000066 jcomp->setQuality(qualityMap[6]);
67}
68
69JpegEncoder::~JpegEncoder()
70{
71 delete jcomp;
72}
73
74void JpegEncoder::setQualityLevel(int level)
75{
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000076 if (level < 0) {
77 level = 0;
78 } else if (level > 9) {
79 level = 9;
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000080 }
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000081 jcomp->setQuality(qualityMap[level]);
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000082}
83
Constantin Kaplinsky651606d2007-10-17 17:40:23 +000084bool JpegEncoder::isPixelFormatSupported(PixelBuffer* pb) const
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000085{
Constantin Kaplinsky651606d2007-10-17 17:40:23 +000086 const PixelFormat &serverPF = pb->getPF();
87 const PixelFormat &clientPF = writer->getConnParams()->pf();
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000088
Constantin Kaplinsky651606d2007-10-17 17:40:23 +000089 // FIXME: Ask encoders if they support given pixel formats.
90
91 if ( serverPF.bpp == 32 && clientPF.bpp >= 16 &&
92 serverPF.depth == 24 && serverPF.redMax == 255 &&
93 serverPF.greenMax == 255 && serverPF.blueMax == 255 ) {
94 return true;
95 }
96
97 return false;
98}
99
100void JpegEncoder::writeRect(PixelBuffer* pb, const Rect& r)
101{
102 if (!isPixelFormatSupported(pb)) {
103 vlog.error("pixel format unsupported by JPEG encoder");
104 throw rdr::Exception("internal error in JpegEncoder");
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000105 }
106
107 writer->startRect(r, encodingTight);
108 rdr::OutStream* os = writer->getOutStream();
109
110 // Get access to pixel data
111 int stride;
112 const rdr::U32* pixels = (const rdr::U32 *)pb->getPixelsR(r, &stride);
113 const PixelFormat& fmt = pb->getPF();
114
115 // Encode data
116 jcomp->compress(pixels, &fmt, r.width(), r.height(), stride);
117
118 // Write Tight-encoded header and JPEG data.
119 os->writeU8(0x09 << 4);
120 os->writeCompactLength(jcomp->getDataLength());
121 os->writeBytes(jcomp->getDataPtr(), jcomp->getDataLength());
122
123 writer->endRect();
Constantin Kaplinsky0b263232007-08-02 13:51:09 +0000124}
125