blob: 6903c2cc919fd8567cd05fc4da06b5dae9ded570 [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>
21#include <rfb/encodings.h>
Constantin Kaplinsky48039462007-10-10 04:44:54 +000022#include <rfb/LogWriter.h>
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000023
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000024#ifdef HAVE_DMEDIA
25#include <rfb/IrixDMJpegCompressor.h>
26#endif
27
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000028using namespace rfb;
29
Constantin Kaplinsky48039462007-10-10 04:44:54 +000030static LogWriter vlog("JpegEncoder");
31
32BoolParameter JpegEncoder::useHardwareJPEG
33("UseHardwareJPEG",
34 "Use hardware-accelerated JPEG compressor for video if available",
35 true);
36
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000037const int JpegEncoder::qualityMap[10] = {
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000038 2, 10, 15, 25, 37, 50, 60, 70, 80, 90
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000039};
40
Constantin Kaplinsky48039462007-10-10 04:44:54 +000041JpegEncoder::JpegEncoder(SMsgWriter* writer_) : writer(writer_), jcomp(0)
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000042{
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000043#ifdef HAVE_DMEDIA
Constantin Kaplinsky48039462007-10-10 04:44:54 +000044 if (useHardwareJPEG) {
45 vlog.debug("trying IRIX DM JPEG compressor");
46 IrixDMJpegCompressor *irixComp = new IrixDMJpegCompressor;
47 if (irixComp->isValid()) {
48 vlog.debug("initialized IRIX DM JPEG compressor successfully");
49 jcomp = irixComp;
50 } else {
51 vlog.error("warning: could not create IRIX DM JPEG compressor");
52 delete irixComp;
53 }
54 }
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000055#else
Constantin Kaplinsky48039462007-10-10 04:44:54 +000056 if (useHardwareJPEG) {
57 vlog.info("no hardware JPEG compressor available");
58 }
Constantin Kaplinskyd37420a2007-09-04 09:08:10 +000059#endif
Constantin Kaplinsky48039462007-10-10 04:44:54 +000060 if (!jcomp) {
61 vlog.debug("using software JPEG compressor");
62 jcomp = new StandardJpegCompressor;
63 }
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000064 jcomp->setQuality(qualityMap[6]);
65}
66
67JpegEncoder::~JpegEncoder()
68{
69 delete jcomp;
70}
71
72void JpegEncoder::setQualityLevel(int level)
73{
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000074 if (level < 0) {
75 level = 0;
76 } else if (level > 9) {
77 level = 9;
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000078 }
Constantin Kaplinskyc2573702007-10-10 10:03:06 +000079 jcomp->setQuality(qualityMap[level]);
Constantin Kaplinsky0b263232007-08-02 13:51:09 +000080}
81
82bool JpegEncoder::writeRect(PixelBuffer* pb, const Rect& r)
83{
84 int serverBitsPerPixel = pb->getPF().bpp;
85 int clientBitsPerPixel = writer->bpp();
86
87 // FIXME: Implement JPEG compression for (serverBitsPerPixel == 16).
88 // FIXME: Check that all color components are actually 8 bits wide.
89 if (serverBitsPerPixel != 32 || clientBitsPerPixel < 16) {
90 // FIXME: Make sure this return value is checked properly.
91 return false;
92 }
93
94 writer->startRect(r, encodingTight);
95 rdr::OutStream* os = writer->getOutStream();
96
97 // Get access to pixel data
98 int stride;
99 const rdr::U32* pixels = (const rdr::U32 *)pb->getPixelsR(r, &stride);
100 const PixelFormat& fmt = pb->getPF();
101
102 // Encode data
103 jcomp->compress(pixels, &fmt, r.width(), r.height(), stride);
104
105 // Write Tight-encoded header and JPEG data.
106 os->writeU8(0x09 << 4);
107 os->writeCompactLength(jcomp->getDataLength());
108 os->writeBytes(jcomp->getDataPtr(), jcomp->getDataLength());
109
110 writer->endRect();
111
112 return true;
113}
114