blob: 51d48f16bd39ed390f3fe0fb173f6a39195f463a [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] = {
38 5, 10, 15, 25, 37, 50, 60, 70, 75, 80
39};
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{
74 if (level >= 0 && level <= 9) {
75 jcomp->setQuality(qualityMap[level]);
76 }
77}
78
79bool JpegEncoder::writeRect(PixelBuffer* pb, const Rect& r)
80{
81 int serverBitsPerPixel = pb->getPF().bpp;
82 int clientBitsPerPixel = writer->bpp();
83
84 // FIXME: Implement JPEG compression for (serverBitsPerPixel == 16).
85 // FIXME: Check that all color components are actually 8 bits wide.
86 if (serverBitsPerPixel != 32 || clientBitsPerPixel < 16) {
87 // FIXME: Make sure this return value is checked properly.
88 return false;
89 }
90
91 writer->startRect(r, encodingTight);
92 rdr::OutStream* os = writer->getOutStream();
93
94 // Get access to pixel data
95 int stride;
96 const rdr::U32* pixels = (const rdr::U32 *)pb->getPixelsR(r, &stride);
97 const PixelFormat& fmt = pb->getPF();
98
99 // Encode data
100 jcomp->compress(pixels, &fmt, r.width(), r.height(), stride);
101
102 // Write Tight-encoded header and JPEG data.
103 os->writeU8(0x09 << 4);
104 os->writeCompactLength(jcomp->getDataLength());
105 os->writeBytes(jcomp->getDataPtr(), jcomp->getDataLength());
106
107 writer->endRect();
108
109 return true;
110}
111