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