Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 1 | /* 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 Kaplinsky | 651606d | 2007-10-17 17:40:23 +0000 | [diff] [blame] | 21 | #include <rdr/Exception.h> |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 22 | #include <rfb/encodings.h> |
Constantin Kaplinsky | 651606d | 2007-10-17 17:40:23 +0000 | [diff] [blame] | 23 | #include <rfb/ConnParams.h> |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 24 | #include <rfb/LogWriter.h> |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 25 | |
Constantin Kaplinsky | d37420a | 2007-09-04 09:08:10 +0000 | [diff] [blame] | 26 | #ifdef HAVE_DMEDIA |
| 27 | #include <rfb/IrixDMJpegCompressor.h> |
| 28 | #endif |
| 29 | |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 30 | using namespace rfb; |
| 31 | |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 32 | static LogWriter vlog("JpegEncoder"); |
| 33 | |
| 34 | BoolParameter JpegEncoder::useHardwareJPEG |
| 35 | ("UseHardwareJPEG", |
| 36 | "Use hardware-accelerated JPEG compressor for video if available", |
| 37 | true); |
| 38 | |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 39 | const int JpegEncoder::qualityMap[10] = { |
Constantin Kaplinsky | c257370 | 2007-10-10 10:03:06 +0000 | [diff] [blame] | 40 | 2, 10, 15, 25, 37, 50, 60, 70, 80, 90 |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 43 | JpegEncoder::JpegEncoder(SMsgWriter* writer_) : writer(writer_), jcomp(0) |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 44 | { |
Constantin Kaplinsky | d37420a | 2007-09-04 09:08:10 +0000 | [diff] [blame] | 45 | #ifdef HAVE_DMEDIA |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 46 | 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 Kaplinsky | d37420a | 2007-09-04 09:08:10 +0000 | [diff] [blame] | 57 | #else |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 58 | if (useHardwareJPEG) { |
| 59 | vlog.info("no hardware JPEG compressor available"); |
| 60 | } |
Constantin Kaplinsky | d37420a | 2007-09-04 09:08:10 +0000 | [diff] [blame] | 61 | #endif |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 62 | if (!jcomp) { |
| 63 | vlog.debug("using software JPEG compressor"); |
| 64 | jcomp = new StandardJpegCompressor; |
| 65 | } |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 66 | jcomp->setQuality(qualityMap[6]); |
| 67 | } |
| 68 | |
| 69 | JpegEncoder::~JpegEncoder() |
| 70 | { |
| 71 | delete jcomp; |
| 72 | } |
| 73 | |
| 74 | void JpegEncoder::setQualityLevel(int level) |
| 75 | { |
Constantin Kaplinsky | c257370 | 2007-10-10 10:03:06 +0000 | [diff] [blame] | 76 | if (level < 0) { |
| 77 | level = 0; |
| 78 | } else if (level > 9) { |
| 79 | level = 9; |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 80 | } |
Constantin Kaplinsky | c257370 | 2007-10-10 10:03:06 +0000 | [diff] [blame] | 81 | jcomp->setQuality(qualityMap[level]); |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Constantin Kaplinsky | 651606d | 2007-10-17 17:40:23 +0000 | [diff] [blame] | 84 | bool JpegEncoder::isPixelFormatSupported(PixelBuffer* pb) const |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 85 | { |
Constantin Kaplinsky | 651606d | 2007-10-17 17:40:23 +0000 | [diff] [blame] | 86 | const PixelFormat &serverPF = pb->getPF(); |
| 87 | const PixelFormat &clientPF = writer->getConnParams()->pf(); |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 88 | |
Constantin Kaplinsky | 651606d | 2007-10-17 17:40:23 +0000 | [diff] [blame] | 89 | // 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 | |
| 100 | void 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 Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 105 | } |
| 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 Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 124 | } |
| 125 | |