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> |
| 21 | #include <rfb/encodings.h> |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 22 | #include <rfb/LogWriter.h> |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 23 | |
Constantin Kaplinsky | d37420a | 2007-09-04 09:08:10 +0000 | [diff] [blame] | 24 | #ifdef HAVE_DMEDIA |
| 25 | #include <rfb/IrixDMJpegCompressor.h> |
| 26 | #endif |
| 27 | |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 28 | using namespace rfb; |
| 29 | |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 30 | static LogWriter vlog("JpegEncoder"); |
| 31 | |
| 32 | BoolParameter JpegEncoder::useHardwareJPEG |
| 33 | ("UseHardwareJPEG", |
| 34 | "Use hardware-accelerated JPEG compressor for video if available", |
| 35 | true); |
| 36 | |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 37 | const int JpegEncoder::qualityMap[10] = { |
| 38 | 5, 10, 15, 25, 37, 50, 60, 70, 75, 80 |
| 39 | }; |
| 40 | |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 41 | JpegEncoder::JpegEncoder(SMsgWriter* writer_) : writer(writer_), jcomp(0) |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 42 | { |
Constantin Kaplinsky | d37420a | 2007-09-04 09:08:10 +0000 | [diff] [blame] | 43 | #ifdef HAVE_DMEDIA |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 44 | 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 Kaplinsky | d37420a | 2007-09-04 09:08:10 +0000 | [diff] [blame] | 55 | #else |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 56 | if (useHardwareJPEG) { |
| 57 | vlog.info("no hardware JPEG compressor available"); |
| 58 | } |
Constantin Kaplinsky | d37420a | 2007-09-04 09:08:10 +0000 | [diff] [blame] | 59 | #endif |
Constantin Kaplinsky | 4803946 | 2007-10-10 04:44:54 +0000 | [diff] [blame] | 60 | if (!jcomp) { |
| 61 | vlog.debug("using software JPEG compressor"); |
| 62 | jcomp = new StandardJpegCompressor; |
| 63 | } |
Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 64 | jcomp->setQuality(qualityMap[6]); |
| 65 | } |
| 66 | |
| 67 | JpegEncoder::~JpegEncoder() |
| 68 | { |
| 69 | delete jcomp; |
| 70 | } |
| 71 | |
| 72 | void JpegEncoder::setQualityLevel(int level) |
| 73 | { |
| 74 | if (level >= 0 && level <= 9) { |
| 75 | jcomp->setQuality(qualityMap[level]); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | bool 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 | |