Bug 573948 - Part 1: Use libjpeg-turbo instead of libjpeg. r=jmuizelaar
[gecko.git] / jpeg / README-turbo.txt
blobfec34e0cdabbfc1ef49d21dfe694ff0775623623
1 *******************************************************************************
2 **     Background
3 *******************************************************************************
5 libjpeg-turbo is a derivative of libjpeg which uses SIMD instructions (MMX,
6 SSE2, etc.) to accelerate baseline JPEG compression and decompression on x86
7 and x86-64 systems.  On such systems, libjpeg-turbo is generally 2-4x as fast
8 as the unmodified version of libjpeg, all else being equal.
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 in
12 2009, including improved support for Mac OS X, 64-bit support, support for
13 32-bit and big endian pixel formats (RGBX, XBGR, etc.), accelerated Huffman
14 encoding/decoding, and various bug fixes.  The goal was to produce a fully open
15 source codec that could replace the partially closed source TurboJPEG/IPP codec
16 used by VirtualGL and TurboVNC.  libjpeg-turbo generally performs in the range
17 of 80-120% of TurboJPEG/IPP.  It is faster in some areas but slower in others.
19 In early 2010, libjpeg-turbo spun off into its own independent project, with
20 the goal of making high-speed JPEG compression/decompression technology
21 available to a broader range of users and developers.  The libjpeg-turbo shared
22 libraries can be used as drop-in replacements for libjpeg on most systems.
25 *******************************************************************************
26 **     License
27 *******************************************************************************
29 The TurboJPEG/OSS wrapper, as well as some of the optimizations to the Huffman
30 encoder (jchuff.c) and decoder (jdhuff.c), were borrowed from VirtualGL, and
31 thus any distribution of libjpeg-turbo which includes those files must, as a
32 whole, be subject to the terms of the wxWindows Library Licence, Version 3.1.
33 A copy of this license can be found in this directory under LICENSE.txt.  The
34 wxWindows Library License is based on the LGPL but includes provisions which
35 allow the Library to be statically linked into proprietary libraries and
36 applications without requiring the resulting binaries to be distributed under
37 the terms of the LGPL.
39 The rest of the source code, apart from TurboJPEG/OSS and the Huffman codec
40 optimizations, falls under a less restrictive, BSD-style license (see README.)
41 You can choose to distribute libjpeg-turbo, as a whole, under this BSD-style
42 license by simply removing TurboJPEG/OSS and replacing the optimized jchuff.c
43 and jdhuff.c with their unoptimized counterparts from the libjpeg v6b source.
46 *******************************************************************************
47 **     Using libjpeg-turbo
48 *******************************************************************************
50 =============================
51 Replacing libjpeg at Run Time
52 =============================
54 If a Unix application is dynamically linked with libjpeg, then you can replace
55 libjpeg with libjpeg-turbo at run time by manipulating LD_LIBRARY_PATH.
56 For instance:
58   [Using libjpeg]
59   > time cjpeg <vgl_5674_0098.ppm >vgl_5674_0098.jpg
60   real  0m0.392s
61   user  0m0.074s
62   sys   0m0.020s
64   [Using libjpeg-turbo]
65   > export LD_LIBRARY_PATH=/opt/libjpeg-turbo/{lib}:$LD_LIBRARY_PATH
66   > time cjpeg <vgl_5674_0098.ppm >vgl_5674_0098.jpg
67   real  0m0.109s
68   user  0m0.029s
69   sys   0m0.010s
71 NOTE: {lib} can be lib, lib32, lib64, or lib/64, depending on the O/S and
72 architecture.
74 System administrators can also replace the libjpeg sym links in /usr/{lib} with
75 links to the libjpeg dynamic library located in /opt/libjpeg-turbo/{lib}.  This
76 will effectively accelerate every dynamically linked libjpeg application on the
77 system.
79 The libjpeg-turbo SDK for Visual C++ installs the libjpeg-turbo DLL
80 (jpeg62.dll, jpeg7.dll, or jpeg8.dll, depending on whether libjpeg v6b, v7, or
81 v8 emulation is enabled) into c:\libjpeg-turbo[64]\bin, and the PATH
82 environment variable can be modified such that this directory is searched
83 before any others that might contain a libjpeg DLL.  However, if a libjpeg
84 DLL exists in an application's install directory, then Windows will load this
85 DLL first whenever the application is launched.  Thus, if an application ships
86 with jpeg62.dll, jpeg7.dll, or jpeg8.dll, then back up the application's
87 version of this DLL and copy c:\libjpeg-turbo[64]\bin\jpeg*.dll into the
88 application's install directory to accelerate it.
90 The version of the libjpeg-turbo DLL distributed in the libjpeg-turbo SDK for
91 Visual C++ requires the Visual C++ 2008 C run time DLL (msvcr90.dll).
92 msvcr90.dll ships with more recent versions of Windows, but users of older
93 Windows releases can obtain it from the Visual C++ 2008 Redistributable
94 Package, which is available as a free download from Microsoft's web site.
96 NOTE:  Features of libjpeg which require passing a C run time structure, such
97 as a file handle, from an application to libjpeg will probably not work with
98 the version of the libjpeg-turbo DLL distributed in the libjpeg-turbo SDK for
99 Visual C++, unless the application is also built to use the Visual C++ 2008 C
100 run time DLL.  In particular, this affects jpeg_stdio_dest() and
101 jpeg_stdio_src().
103 Mac applications typically embed their own copies of the libjpeg dylib inside
104 the (hidden) application bundle, so it is not possible to globally replace
105 libjpeg on OS X systems.  If an application uses a shared library version of
106 libjpeg, then it may be possible to replace the application's version of it.
107 This would generally involve copying libjpeg.*.dylib from libjpeg-turbo into
108 the appropriate place in the application bundle and using install_name_tool to
109 repoint the dylib to the new directory.  This requires an advanced knowledge of
110 OS X and would not survive an upgrade or a re-install of the application.
111 Thus, it is not recommended for most users.
113 =======================
114 Replacing TurboJPEG/IPP
115 =======================
117 libjpeg-turbo is a drop-in replacement for the TurboJPEG/IPP SDK used by
118 VirtualGL 2.1.x and TurboVNC 0.6 (and prior.)  libjpeg-turbo contains a wrapper
119 library (TurboJPEG/OSS) that emulates the TurboJPEG API using libjpeg-turbo
120 instead of the closed source Intel Performance Primitives.  You can replace the
121 TurboJPEG/IPP package on Linux systems with the libjpeg-turbo package in order
122 to make existing releases of VirtualGL 2.1.x and TurboVNC 0.x use the new codec
123 at run time.  Note that the 64-bit libjpeg-turbo packages contain only 64-bit
124 binaries, whereas the TurboJPEG/IPP 64-bit packages contained both 64-bit and
125 32-bit binaries.  Thus, to replace a TurboJPEG/IPP 64-bit package, install
126 both the 64-bit and 32-bit versions of libjpeg-turbo.
128 You can also build the VirtualGL 2.1.x and TurboVNC 0.6 source code with
129 the libjpeg-turbo SDK instead of TurboJPEG/IPP.  It should work identically.
130 libjpeg-turbo also includes static library versions of TurboJPEG/OSS, which
131 are used to build TurboVNC 1.0 and later.
133 ========================================
134 Using libjpeg-turbo in Your Own Programs
135 ========================================
137 For the most part, libjpeg-turbo should work identically to libjpeg, so in
138 most cases, an application can be built against libjpeg and then run against
139 libjpeg-turbo.  On Unix systems (including Cygwin), you can build against
140 libjpeg-turbo instead of libjpeg by setting
142   CPATH=/opt/libjpeg-turbo/include
143   and
144   LIBRARY_PATH=/opt/libjpeg-turbo/{lib}
146 ({lib} = lib32 or lib64, depending on whether you are building a 32-bit or a
147 64-bit application.)
149 If using MinGW, then set
151   CPATH=/c/libjpeg-turbo-gcc[64]/include
152   and
153   LIBRARY_PATH=/c/libjpeg-turbo-gcc[64]/lib
155 Building against libjpeg-turbo is useful, for instance, if you want to build an
156 application that leverages the libjpeg-turbo colorspace extensions (see below.)
157 On Linux and Solaris systems, you would still need to manipulate
158 LD_LIBRARY_PATH or create appropriate sym links to use libjpeg-turbo at run
159 time.  On such systems, you can pass -R /opt/libjpeg-turbo/{lib} to the linker
160 to force the use of libjpeg-turbo at run time rather than libjpeg (also useful
161 if you want to leverage the colorspace extensions), or you can link against the
162 libjpeg-turbo static library.
164 To force a Linux, Solaris, or MinGW application to link against the static
165 version of libjpeg-turbo, you can use the following linker options:
167   -Wl,-Bstatic -ljpeg -Wl,-Bdynamic
169 On OS X, simply add /opt/libjpeg-turbo/lib/libjpeg.a to the linker command
170 line (this also works on Linux and Solaris.)
172 To build Visual C++ applications using libjpeg-turbo, add
173 c:\libjpeg-turbo[64]\include to the system or user INCLUDE environment
174 variable and c:\libjpeg-turbo[64]\lib to the system or user LIB environment
175 variable, and then link against either jpeg.lib (to use the DLL version of
176 libjpeg-turbo) or jpeg-static.lib (to use the static version of libjpeg-turbo.)
178 =====================
179 Colorspace Extensions
180 =====================
182 libjpeg-turbo includes extensions which allow JPEG images to be compressed
183 directly from (and decompressed directly to) buffers which use BGR, BGRX,
184 RGBX, XBGR, and XRGB pixel ordering.  This is implemented with six new
185 colorspace constants:
187   JCS_EXT_RGB   /* red/green/blue */
188   JCS_EXT_RGBX  /* red/green/blue/x */
189   JCS_EXT_BGR   /* blue/green/red */
190   JCS_EXT_BGRX  /* blue/green/red/x */
191   JCS_EXT_XBGR  /* x/blue/green/red */
192   JCS_EXT_XRGB  /* x/red/green/blue */
194 Setting cinfo.in_color_space (compression) or cinfo.out_color_space
195 (decompression) to one of these values will cause libjpeg-turbo to read the
196 red, green, and blue values from (or write them to) the appropriate position in
197 the pixel when YUV conversion is performed.
199 Your application can check for the existence of these extensions at compile
200 time with:
202   #ifdef JCS_EXTENSIONS
204 At run time, attempting to use these extensions with a version of libjpeg
205 that doesn't support them will result in a "Bogus input colorspace" error.
207 =================================
208 libjpeg v7 and v8 API/ABI support
209 =================================
211 libjpeg v7 and v8 added new features to the API/ABI, and, unfortunately, the
212 compression and decompression structures were extended in a backward-
213 incompatible manner to accommodate these features.  Thus, programs which are
214 built to use libjpeg v7 or v8 did not work with libjpeg-turbo, since it is
215 based on the libjpeg v6b code base.  Although libjpeg v7 and v8 are still not
216 as widely used as v6b, enough programs (including a few Linux distros) have
217 made the switch that it was desirable to provide support for the libjpeg v7/v8
218 API/ABI in libjpeg-turbo.
220 Some of the libjpeg v7 and v8 features -- DCT scaling, to name one -- involve
221 deep modifications to the code which cannot be accommodated by libjpeg-turbo
222 without either breaking compatibility with libjpeg v6b or producing an
223 unsupportable mess.  In order to fully support libjpeg v8 with all of its
224 features, we would have to essentially port the SIMD extensions to the libjpeg
225 v8 code base and maintain two separate code trees.  We are hesitant to do this
226 until/unless the newer libjpeg code bases garner more community support and
227 involvement and until/unless we have some notion of whether future libjpeg
228 releases will also be backward-incompatible.
230 By passing an argument of --with-jpeg7 or --with-jpeg8 to configure, or an
231 argument of -DWITH_JPEG7=1 or -DWITH_JPEG8=1 to cmake, you can build a version
232 of libjpeg-turbo which emulates the libjpeg v7 or v8 API/ABI, so that programs
233 which are built against libjpeg v7 or v8 can be run with libjpeg-turbo.  The
234 following section describes which libjpeg v7+ features are supported and which
235 aren't.
237 libjpeg v7 and v8 Features:
238 ---------------------------
240 Fully supported:
242 -- cjpeg: Separate quality settings for luminance and chrominance
243    Note that the libpjeg v7+ API was extended to accommodate this feature only
244    for convenience purposes.  It has always been possible to implement this
245    feature with libjpeg v6b (see rdswitch.c for an example.)
247 -- cjpeg: 32-bit BMP support
249 -- jpegtran: lossless cropping
251 -- jpegtran: -perfect option
253 -- rdjpgcom: -raw option
255 -- rdjpgcom: locale awareness
258 Fully supported when using libjpeg v7/v8 emulation:
260 -- libjpeg: In-memory source and destination managers
263 Not supported:
265 -- libjpeg: DCT scaling in compressor
266    cinfo.scale_num and cinfo.scale_denom are silently ignored.
268 -- libjpeg: IDCT scaling extensions in decompressor
269    libjpeg-turbo still supports IDCT scaling with scaling factors of 1/2, 1/4,
270    and 1/8 (same as libjpeg v6b.)
272 -- libjpeg: Fancy downsampling in compressor
273    cinfo.do_fancy_downsampling is silently ignored.
275 -- jpegtran: Scaling
276    Seems to depend on the DCT scaling feature, which isn't supported.
279 *******************************************************************************
280 **     Performance pitfalls
281 *******************************************************************************
283 ===============
284 Restart Markers
285 ===============
287 The optimized Huffman decoder in libjpeg-turbo does not handle restart markers
288 in a way that makes libjpeg happy, so it is necessary to use the slow Huffman
289 decoder when decompressing a JPEG image that has restart markers.  This can
290 cause the decompression performance to drop by as much as 20%, but the
291 performance will still be much much greater than that of libjpeg v6b.  Many
292 consumer packages, such as PhotoShop, use restart markers when generating JPEG
293 images, so images generated by those programs will experience this issue.
295 ===============================================
296 Fast Integer Forward DCT at High Quality Levels
297 ===============================================
299 The algorithm used by the SIMD-accelerated quantization function cannot produce
300 correct results whenever the fast integer forward DCT is used along with a JPEG
301 quality of 98-100.  Thus, libjpeg-turbo must use the non-SIMD quantization
302 function in those cases.  This causes performance to drop by as much as 40%.
303 It is therefore strongly advised that you use the slow integer forward DCT
304 whenever encoding images with a JPEG quality of 98 or higher.