DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 1 | ******************************************************************************* |
| 2 | ** Background |
| 3 | ******************************************************************************* |
| 4 | |
| 5 | libjpeg-turbo is a high-speed version of libjpeg for x86 and x86-64 processors |
| 6 | which uses SIMD instructions (MMX, SSE2, etc.) to accelerate baseline JPEG |
| 7 | compression and decompression. libjpeg-turbo is generally 2-4x as fast |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 8 | as the unmodified version of libjpeg v6b, all else being equal. |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 9 | |
| 10 | libjpeg-turbo was originally based on libjpeg/SIMD by Miyasaka Masaru, but |
| 11 | the TigerVNC and VirtualGL projects made numerous enhancements to the codec, |
| 12 | including improved support for Mac OS X, 64-bit support, support for 32-bit |
| 13 | and big endian pixel formats, accelerated Huffman encoding/decoding, and |
| 14 | various bug fixes. The goal was to produce a fully open source codec that |
| 15 | could replace the partially closed source TurboJPEG/IPP codec used by VirtualGL |
| 16 | and TurboVNC. libjpeg-turbo generally performs in the range of 80-120% of |
| 17 | TurboJPEG/IPP. It is faster in some areas but slower in others. |
| 18 | |
| 19 | It was decided to split libjpeg-turbo into a separate SDK so that other |
| 20 | projects could take advantage of this technology. The libjpeg-turbo shared |
| 21 | libraries can be used as drop-in replacements for libjpeg on most systems. |
| 22 | |
| 23 | |
| 24 | ******************************************************************************* |
| 25 | ** License |
| 26 | ******************************************************************************* |
| 27 | |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 28 | The TurboJPEG/OSS wrapper, as well as some of the optimizations to the Huffman |
| 29 | encoder (jchuff.c) and decoder (jdhuff.c), were borrowed from VirtualGL, and |
| 30 | thus any distribution of libjpeg-turbo which includes those files must, as a |
| 31 | whole, be subject to the terms of the wxWindows Library Licence, Version 3.1. |
| 32 | A copy of this license can be found in this directory under LICENSE.txt. The |
| 33 | wxWindows Library License is based on the LGPL but includes provisions which |
| 34 | allow the Library to be statically linked into proprietary libraries and |
| 35 | applications without requiring the resulting binaries to be distributed under |
| 36 | the terms of the LGPL. |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 37 | |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 38 | The rest of the source code, apart from TurboJPEG/OSS and the Huffman codec |
| 39 | optimizations, falls under a less restrictive, BSD-style license (see README.) |
| 40 | You can choose to distribute libjpeg-turbo, as a whole, under this BSD-style |
| 41 | license by simply removing TurboJPEG/OSS and replacing the optimized jchuff.c |
| 42 | and jdhuff.c with their unoptimized counterparts from the libjpeg v6b source. |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | ******************************************************************************* |
| 46 | ** Using libjpeg-turbo |
| 47 | ******************************************************************************* |
| 48 | |
| 49 | ============================= |
| 50 | Replacing libjpeg at Run Time |
| 51 | ============================= |
| 52 | |
| 53 | If a Unix application is dynamically linked with libjpeg, then you can replace |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 54 | libjpeg with libjpeg-turbo at run time by manipulating LD_LIBRARY_PATH. |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 55 | For instance: |
| 56 | |
| 57 | [Using libjpeg] |
| 58 | > time cjpeg <vgl_5674_0098.ppm >vgl_5674_0098.jpg |
| 59 | real 0m0.392s |
| 60 | user 0m0.074s |
| 61 | sys 0m0.020s |
| 62 | |
| 63 | [Using libjpeg-turbo] |
| 64 | > export LD_LIBRARY_PATH=/opt/libjpeg-turbo/{lib}:$LD_LIBRARY_PATH |
| 65 | > time cjpeg <vgl_5674_0098.ppm >vgl_5674_0098.jpg |
| 66 | real 0m0.109s |
| 67 | user 0m0.029s |
| 68 | sys 0m0.010s |
| 69 | |
| 70 | NOTE: {lib} can be lib, lib32, lib64, or lib/64, depending on the O/S and |
| 71 | architecture. |
| 72 | |
| 73 | System administrators can also replace the libjpeg sym links in /usr/{lib} with |
| 74 | links to the libjpeg dynamic library located in /opt/libjpeg-turbo/{lib}. This |
| 75 | will effectively accelerate every dynamically linked libjpeg application on the |
| 76 | system. |
| 77 | |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 78 | The libjpeg-turbo SDK for Visual C++ installs jpeg62.dll into |
| 79 | c:\libjpeg-turbo[64]\bin, and the PATH environment variable can be modified |
| 80 | such that this directory is searched before any others that might contain |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 81 | jpeg62.dll. However, if jpeg62.dll also exists in an application's install |
| 82 | directory, then Windows will load the application's version of it first. Thus, |
| 83 | if an application ships with jpeg62.dll, then back up the application's version |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 84 | of jpeg62.dll and copy c:\libjpeg-turbo[64]\bin\jpeg62.dll into the |
| 85 | application's install directory to accelerate it. |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 86 | |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 87 | The version of jpeg62.dll distributed in the libjpeg-turbo SDK for Visual C++ |
| 88 | requires the Visual C++ 2008 C run time DLL (msvcr90.dll). msvcr90.dll ships |
| 89 | with more recent versions of Windows, but users of older Windows releases can |
| 90 | obtain it from the Visual C++ 2008 Redistributable Package, which is available |
| 91 | as a free download from Microsoft's web site. |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 92 | |
| 93 | NOTE: Features of libjpeg which require passing a C run time structure, such |
| 94 | as a file handle, from an application to libjpeg will probably not work with |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 95 | the version of jpeg62.dll distributed in the libjpeg-turbo SDK for Visual C++, |
| 96 | unless the application is also built to use the Visual C++ 2008 C run time DLL. |
| 97 | In particular, this affects jpeg_stdio_dest() and jpeg_stdio_src(). |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 98 | |
| 99 | Mac applications typically embed their own copies of libjpeg.62.dylib inside |
| 100 | the (hidden) application bundle, so it is not possible to globally replace |
| 101 | libjpeg on OS X systems. If an application uses a shared library version of |
| 102 | libjpeg, then it may be possible to replace the application's version of it. |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 103 | This would generally involve copying libjpeg.62.dylib from libjpeg-turbo into |
| 104 | the appropriate place in the application bundle and using install_name_tool to |
| 105 | repoint the dylib to the new directory. This requires an advanced knowledge of |
| 106 | OS X and would not survive an upgrade or a re-install of the application. |
| 107 | Thus, it is not recommended for most users. |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 108 | |
| 109 | ======================= |
| 110 | Replacing TurboJPEG/IPP |
| 111 | ======================= |
| 112 | |
| 113 | libjpeg-turbo is a drop-in replacement for the TurboJPEG/IPP SDK used by |
| 114 | VirtualGL 2.1.x and TurboVNC 0.6 (and prior.) libjpeg-turbo contains a wrapper |
| 115 | library (TurboJPEG/OSS) that emulates the TurboJPEG API using libjpeg-turbo |
| 116 | instead of the closed source Intel Performance Primitives. You can replace the |
| 117 | TurboJPEG/IPP package on Linux systems with the libjpeg-turbo package in order |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 118 | to make existing releases of VirtualGL 2.1.x and TurboVNC 0.x use the new codec |
| 119 | at run time. Note that the 64-bit libjpeg-turbo packages contain only 64-bit |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 120 | binaries, whereas the TurboJPEG/IPP 64-bit packages contained both 64-bit and |
| 121 | 32-bit binaries. Thus, to replace a TurboJPEG/IPP 64-bit package, install |
| 122 | both the 64-bit and 32-bit versions of libjpeg-turbo. |
| 123 | |
| 124 | You can also build the VirtualGL 2.1.x and TurboVNC 0.6 source code with |
| 125 | the libjpeg-turbo SDK instead of TurboJPEG/IPP. It should work identically. |
| 126 | libjpeg-turbo also includes static library versions of TurboJPEG/OSS, which |
| 127 | are used to build TurboVNC 1.0 and later. |
| 128 | |
| 129 | ======================================== |
| 130 | Using libjpeg-turbo in Your Own Programs |
| 131 | ======================================== |
| 132 | |
| 133 | For the most part, libjpeg-turbo should work identically to libjpeg, so in |
| 134 | most cases, an application can be built against libjpeg and then run against |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 135 | libjpeg-turbo. On Unix systems (including Cygwin), you can build against |
| 136 | libjpeg-turbo instead of libjpeg by setting |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 137 | |
| 138 | CPATH=/opt/libjpeg-turbo/include |
| 139 | and |
| 140 | LIBRARY_PATH=/opt/libjpeg-turbo/{lib} |
| 141 | |
| 142 | ({lib} = lib32 or lib64, depending on whether you are building a 32-bit or a |
| 143 | 64-bit application.) |
| 144 | |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 145 | If using MinGW, then set |
| 146 | |
| 147 | CPATH=/c/libjpeg-turbo-gcc[64]/include |
| 148 | and |
| 149 | LIBRARY_PATH=/c/libjpeg-turbo-gcc[64]/lib |
| 150 | |
| 151 | Building against libjpeg-turbo is useful, for instance, if you want to build an |
| 152 | application that leverages the libjpeg-turbo colorspace extensions (see below.) |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 153 | On Linux and Solaris systems, you would still need to manipulate |
| 154 | LD_LIBRARY_PATH or create appropriate sym links to use libjpeg-turbo at run |
| 155 | time. On such systems, you can pass -R /opt/libjpeg-turbo/{lib} to the linker |
| 156 | to force the use of libjpeg-turbo at run time rather than libjpeg (also useful |
| 157 | if you want to leverage the colorspace extensions), or you can link against the |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 158 | libjpeg-turbo static library. |
| 159 | |
| 160 | To force a Linux, Solaris, or MinGW application to link against the static |
| 161 | version of libjpeg-turbo, you can use the following linker options: |
| 162 | |
| 163 | -Wl,-Bstatic -ljpeg -Wl,-Bdynamic |
| 164 | |
| 165 | On OS X, simply add /opt/libjpeg-turbo/lib/libjpeg.a to the linker command |
| 166 | line (this also works on Linux and Solaris.) |
| 167 | |
| 168 | To build Visual C++ applications using libjpeg-turbo, add |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 169 | c:\libjpeg-turbo[64]\include to the system or user INCLUDE environment |
| 170 | variable and c:\libjpeg-turbo[64]\lib to the system or user LIB environment |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 171 | variable, and then link against either jpeg.lib (to use jpeg62.dll) or |
| 172 | jpeg-static.lib (to use the static version of libjpeg-turbo.) |
| 173 | |
| 174 | ===================== |
| 175 | Colorspace Extensions |
| 176 | ===================== |
| 177 | |
| 178 | libjpeg-turbo includes extensions which allow JPEG images to be compressed |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 179 | directly from (and decompressed directly to) buffers which use BGR, BGRX, |
| 180 | RGBX, XBGR, and XRGB pixel ordering. This is implemented with six new |
DRC | e74b086 | 2010-10-25 22:07:40 +0000 | [diff] [blame] | 181 | colorspace constants: |
| 182 | |
| 183 | JCS_EXT_RGB /* red/green/blue */ |
| 184 | JCS_EXT_RGBX /* red/green/blue/x */ |
| 185 | JCS_EXT_BGR /* blue/green/red */ |
| 186 | JCS_EXT_BGRX /* blue/green/red/x */ |
| 187 | JCS_EXT_XBGR /* x/blue/green/red */ |
| 188 | JCS_EXT_XRGB /* x/red/green/blue */ |
| 189 | |
| 190 | Setting cinfo.in_color_space (compression) or cinfo.out_color_space |
| 191 | (decompression) to one of these values will cause libjpeg-turbo to read the |
| 192 | red, green, and blue values from (or write them to) the appropriate position in |
| 193 | the pixel when YUV conversion is performed. |
| 194 | |
| 195 | Your application can check for the existence of these extensions at compile |
| 196 | time with: |
| 197 | |
| 198 | #ifdef JCS_EXTENSIONS |
| 199 | |
| 200 | At run time, attempting to use these extensions with a version of libjpeg |
| 201 | that doesn't support them will result in a "Bogus input colorspace" error. |
DRC | fc97cfe | 2011-02-18 05:42:10 +0000 | [diff] [blame] | 202 | |
| 203 | |
| 204 | ******************************************************************************* |
| 205 | ** Performance pitfalls |
| 206 | ******************************************************************************* |
| 207 | |
| 208 | =============== |
| 209 | Restart Markers |
| 210 | =============== |
| 211 | |
| 212 | The optimized Huffman decoder in libjpeg-turbo does not handle restart markers |
| 213 | in a way that makes libjpeg happy, so it is necessary to use the slow Huffman |
| 214 | decoder when decompressing a JPEG image that has restart markers. This can |
| 215 | cause the decompression performance to drop by as much as 20%, but the |
| 216 | performance will still be much much greater than that of libjpeg v6b. Many |
| 217 | consumer packages, such as PhotoShop, use restart markers when generating JPEG |
| 218 | images, so images generated by those programs will experience this issue. |
| 219 | |
| 220 | =============================================== |
| 221 | Fast Integer Forward DCT at High Quality Levels |
| 222 | =============================================== |
| 223 | |
| 224 | The algorithm used by the SIMD-accelerated quantization function cannot produce |
| 225 | correct results whenever the fast integer forward DCT is used along with a JPEG |
| 226 | quality of 98-100. Thus, libjpeg-turbo must use the non-SIMD quantization |
| 227 | function in those cases. This causes performance to drop by as much as 40%. |
| 228 | It is therefore strongly advised that you use the slow integer forward DCT |
| 229 | whenever encoding images with a JPEG quality of 98 or higher. |