blob: fec34e0cdabbfc1ef49d21dfe694ff0775623623 [file] [log] [blame]
DRC101f09a2010-02-12 22:52:37 +00001*******************************************************************************
2** Background
3*******************************************************************************
4
DRC0d2d9652011-02-18 22:29:45 +00005libjpeg-turbo is a derivative of libjpeg which uses SIMD instructions (MMX,
6SSE2, etc.) to accelerate baseline JPEG compression and decompression on x86
7and x86-64 systems. On such systems, libjpeg-turbo is generally 2-4x as fast
8as the unmodified version of libjpeg, all else being equal.
DRC101f09a2010-02-12 22:52:37 +00009
10libjpeg-turbo was originally based on libjpeg/SIMD by Miyasaka Masaru, but
DRC0d2d9652011-02-18 22:29:45 +000011the TigerVNC and VirtualGL projects made numerous enhancements to the codec in
122009, including improved support for Mac OS X, 64-bit support, support for
1332-bit and big endian pixel formats (RGBX, XBGR, etc.), accelerated Huffman
14encoding/decoding, and various bug fixes. The goal was to produce a fully open
15source codec that could replace the partially closed source TurboJPEG/IPP codec
16used by VirtualGL and TurboVNC. libjpeg-turbo generally performs in the range
17of 80-120% of TurboJPEG/IPP. It is faster in some areas but slower in others.
DRC101f09a2010-02-12 22:52:37 +000018
DRC0d2d9652011-02-18 22:29:45 +000019In early 2010, libjpeg-turbo spun off into its own independent project, with
20the goal of making high-speed JPEG compression/decompression technology
21available to a broader range of users and developers. The libjpeg-turbo shared
DRC68fef832010-02-16 05:29:10 +000022libraries can be used as drop-in replacements for libjpeg on most systems.
DRC101f09a2010-02-12 22:52:37 +000023
24
25*******************************************************************************
DRCce1546e2010-02-13 23:06:03 +000026** License
27*******************************************************************************
28
DRCab4db652011-02-18 04:55:08 +000029The TurboJPEG/OSS wrapper, as well as some of the optimizations to the Huffman
30encoder (jchuff.c) and decoder (jdhuff.c), were borrowed from VirtualGL, and
31thus any distribution of libjpeg-turbo which includes those files must, as a
32whole, be subject to the terms of the wxWindows Library Licence, Version 3.1.
33A copy of this license can be found in this directory under LICENSE.txt. The
34wxWindows Library License is based on the LGPL but includes provisions which
35allow the Library to be statically linked into proprietary libraries and
36applications without requiring the resulting binaries to be distributed under
37the terms of the LGPL.
DRC1e6b5b42010-03-20 20:00:51 +000038
DRCab4db652011-02-18 04:55:08 +000039The rest of the source code, apart from TurboJPEG/OSS and the Huffman codec
40optimizations, falls under a less restrictive, BSD-style license (see README.)
41You can choose to distribute libjpeg-turbo, as a whole, under this BSD-style
42license by simply removing TurboJPEG/OSS and replacing the optimized jchuff.c
43and jdhuff.c with their unoptimized counterparts from the libjpeg v6b source.
DRC7c1df0a2011-02-18 02:45:24 +000044
45
46*******************************************************************************
DRC68fef832010-02-16 05:29:10 +000047** Using libjpeg-turbo
DRC101f09a2010-02-12 22:52:37 +000048*******************************************************************************
49
DRC68fef832010-02-16 05:29:10 +000050=============================
51Replacing libjpeg at Run Time
52=============================
DRC101f09a2010-02-12 22:52:37 +000053
DRC68fef832010-02-16 05:29:10 +000054If a Unix application is dynamically linked with libjpeg, then you can replace
DRCab4db652011-02-18 04:55:08 +000055libjpeg with libjpeg-turbo at run time by manipulating LD_LIBRARY_PATH.
DRC68fef832010-02-16 05:29:10 +000056For instance:
DRC101f09a2010-02-12 22:52:37 +000057
DRC68fef832010-02-16 05:29:10 +000058 [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
DRC101f09a2010-02-12 22:52:37 +000063
DRC68fef832010-02-16 05:29:10 +000064 [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
DRC101f09a2010-02-12 22:52:37 +000070
DRCff95aa62010-06-05 00:22:32 +000071NOTE: {lib} can be lib, lib32, lib64, or lib/64, depending on the O/S and
DRC68fef832010-02-16 05:29:10 +000072architecture.
DRC101f09a2010-02-12 22:52:37 +000073
DRC68fef832010-02-16 05:29:10 +000074System administrators can also replace the libjpeg sym links in /usr/{lib} with
75links to the libjpeg dynamic library located in /opt/libjpeg-turbo/{lib}. This
76will effectively accelerate every dynamically linked libjpeg application on the
77system.
DRC101f09a2010-02-12 22:52:37 +000078
DRCab4db652011-02-18 04:55:08 +000079The 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
81v8 emulation is enabled) into c:\libjpeg-turbo[64]\bin, and the PATH
DRC5559c902010-10-18 02:21:10 +000082environment variable can be modified such that this directory is searched
83before any others that might contain a libjpeg DLL. However, if a libjpeg
84DLL exists in an application's install directory, then Windows will load this
85DLL first whenever the application is launched. Thus, if an application ships
86with jpeg62.dll, jpeg7.dll, or jpeg8.dll, then back up the application's
DRCab4db652011-02-18 04:55:08 +000087version of this DLL and copy c:\libjpeg-turbo[64]\bin\jpeg*.dll into the
DRC5559c902010-10-18 02:21:10 +000088application's install directory to accelerate it.
DRC7e0b4992010-02-25 05:52:44 +000089
DRCab4db652011-02-18 04:55:08 +000090The version of the libjpeg-turbo DLL distributed in the libjpeg-turbo SDK for
91Visual C++ requires the Visual C++ 2008 C run time DLL (msvcr90.dll).
DRC5559c902010-10-18 02:21:10 +000092msvcr90.dll ships with more recent versions of Windows, but users of older
93Windows releases can obtain it from the Visual C++ 2008 Redistributable
94Package, which is available as a free download from Microsoft's web site.
DRC0248dd92010-02-25 06:21:12 +000095
96NOTE: Features of libjpeg which require passing a C run time structure, such
97as a file handle, from an application to libjpeg will probably not work with
DRCab4db652011-02-18 04:55:08 +000098the version of the libjpeg-turbo DLL distributed in the libjpeg-turbo SDK for
99Visual C++, unless the application is also built to use the Visual C++ 2008 C
100run time DLL. In particular, this affects jpeg_stdio_dest() and
101jpeg_stdio_src().
DRC101f09a2010-02-12 22:52:37 +0000102
DRC5559c902010-10-18 02:21:10 +0000103Mac applications typically embed their own copies of the libjpeg dylib inside
DRC68fef832010-02-16 05:29:10 +0000104the (hidden) application bundle, so it is not possible to globally replace
105libjpeg on OS X systems. If an application uses a shared library version of
106libjpeg, then it may be possible to replace the application's version of it.
DRC5559c902010-10-18 02:21:10 +0000107This would generally involve copying libjpeg.*.dylib from libjpeg-turbo into
108the appropriate place in the application bundle and using install_name_tool to
109repoint the dylib to the new directory. This requires an advanced knowledge of
110OS X and would not survive an upgrade or a re-install of the application.
111Thus, it is not recommended for most users.
DRC101f09a2010-02-12 22:52:37 +0000112
DRC68fef832010-02-16 05:29:10 +0000113=======================
114Replacing TurboJPEG/IPP
115=======================
DRC101f09a2010-02-12 22:52:37 +0000116
DRC68fef832010-02-16 05:29:10 +0000117libjpeg-turbo is a drop-in replacement for the TurboJPEG/IPP SDK used by
DRCff95aa62010-06-05 00:22:32 +0000118VirtualGL 2.1.x and TurboVNC 0.6 (and prior.) libjpeg-turbo contains a wrapper
DRC68fef832010-02-16 05:29:10 +0000119library (TurboJPEG/OSS) that emulates the TurboJPEG API using libjpeg-turbo
120instead of the closed source Intel Performance Primitives. You can replace the
121TurboJPEG/IPP package on Linux systems with the libjpeg-turbo package in order
DRCab4db652011-02-18 04:55:08 +0000122to make existing releases of VirtualGL 2.1.x and TurboVNC 0.x use the new codec
123at run time. Note that the 64-bit libjpeg-turbo packages contain only 64-bit
DRCff95aa62010-06-05 00:22:32 +0000124binaries, whereas the TurboJPEG/IPP 64-bit packages contained both 64-bit and
DRC68fef832010-02-16 05:29:10 +000012532-bit binaries. Thus, to replace a TurboJPEG/IPP 64-bit package, install
126both the 64-bit and 32-bit versions of libjpeg-turbo.
DRC101f09a2010-02-12 22:52:37 +0000127
DRCff95aa62010-06-05 00:22:32 +0000128You can also build the VirtualGL 2.1.x and TurboVNC 0.6 source code with
DRC68fef832010-02-16 05:29:10 +0000129the libjpeg-turbo SDK instead of TurboJPEG/IPP. It should work identically.
130libjpeg-turbo also includes static library versions of TurboJPEG/OSS, which
DRCff95aa62010-06-05 00:22:32 +0000131are used to build TurboVNC 1.0 and later.
DRC101f09a2010-02-12 22:52:37 +0000132
DRC68fef832010-02-16 05:29:10 +0000133========================================
134Using libjpeg-turbo in Your Own Programs
135========================================
DRC101f09a2010-02-12 22:52:37 +0000136
DRC68fef832010-02-16 05:29:10 +0000137For the most part, libjpeg-turbo should work identically to libjpeg, so in
138most cases, an application can be built against libjpeg and then run against
DRC5559c902010-10-18 02:21:10 +0000139libjpeg-turbo. On Unix systems (including Cygwin), you can build against
140libjpeg-turbo instead of libjpeg by setting
DRC101f09a2010-02-12 22:52:37 +0000141
DRC68fef832010-02-16 05:29:10 +0000142 CPATH=/opt/libjpeg-turbo/include
143 and
144 LIBRARY_PATH=/opt/libjpeg-turbo/{lib}
DRC101f09a2010-02-12 22:52:37 +0000145
DRCff95aa62010-06-05 00:22:32 +0000146({lib} = lib32 or lib64, depending on whether you are building a 32-bit or a
14764-bit application.)
DRC101f09a2010-02-12 22:52:37 +0000148
DRC0a1f68e2010-02-24 07:24:26 +0000149If using MinGW, then set
150
DRC3dc1bc22010-05-10 22:18:10 +0000151 CPATH=/c/libjpeg-turbo-gcc[64]/include
DRC0a1f68e2010-02-24 07:24:26 +0000152 and
DRC3dc1bc22010-05-10 22:18:10 +0000153 LIBRARY_PATH=/c/libjpeg-turbo-gcc[64]/lib
DRC0a1f68e2010-02-24 07:24:26 +0000154
155Building against libjpeg-turbo is useful, for instance, if you want to build an
156application that leverages the libjpeg-turbo colorspace extensions (see below.)
DRCab4db652011-02-18 04:55:08 +0000157On Linux and Solaris systems, you would still need to manipulate
158LD_LIBRARY_PATH or create appropriate sym links to use libjpeg-turbo at run
159time. On such systems, you can pass -R /opt/libjpeg-turbo/{lib} to the linker
160to force the use of libjpeg-turbo at run time rather than libjpeg (also useful
161if you want to leverage the colorspace extensions), or you can link against the
DRC0a1f68e2010-02-24 07:24:26 +0000162libjpeg-turbo static library.
163
164To force a Linux, Solaris, or MinGW application to link against the static
165version of libjpeg-turbo, you can use the following linker options:
DRC101f09a2010-02-12 22:52:37 +0000166
DRC68fef832010-02-16 05:29:10 +0000167 -Wl,-Bstatic -ljpeg -Wl,-Bdynamic
DRC101f09a2010-02-12 22:52:37 +0000168
DRCff95aa62010-06-05 00:22:32 +0000169On OS X, simply add /opt/libjpeg-turbo/lib/libjpeg.a to the linker command
DRC0a1f68e2010-02-24 07:24:26 +0000170line (this also works on Linux and Solaris.)
DRC101f09a2010-02-12 22:52:37 +0000171
DRC68fef832010-02-16 05:29:10 +0000172To build Visual C++ applications using libjpeg-turbo, add
DRCab4db652011-02-18 04:55:08 +0000173c:\libjpeg-turbo[64]\include to the system or user INCLUDE environment
174variable and c:\libjpeg-turbo[64]\lib to the system or user LIB environment
DRC5559c902010-10-18 02:21:10 +0000175variable, and then link against either jpeg.lib (to use the DLL version of
176libjpeg-turbo) or jpeg-static.lib (to use the static version of libjpeg-turbo.)
DRC101f09a2010-02-12 22:52:37 +0000177
DRC68fef832010-02-16 05:29:10 +0000178=====================
179Colorspace Extensions
180=====================
DRC101f09a2010-02-12 22:52:37 +0000181
DRC68fef832010-02-16 05:29:10 +0000182libjpeg-turbo includes extensions which allow JPEG images to be compressed
DRC646e5a82010-11-18 19:55:29 +0000183directly from (and decompressed directly to) buffers which use BGR, BGRX,
184RGBX, XBGR, and XRGB pixel ordering. This is implemented with six new
DRC68fef832010-02-16 05:29:10 +0000185colorspace constants:
DRC101f09a2010-02-12 22:52:37 +0000186
DRC68fef832010-02-16 05:29:10 +0000187 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 */
DRC101f09a2010-02-12 22:52:37 +0000193
DRC68fef832010-02-16 05:29:10 +0000194Setting cinfo.in_color_space (compression) or cinfo.out_color_space
195(decompression) to one of these values will cause libjpeg-turbo to read the
196red, green, and blue values from (or write them to) the appropriate position in
197the pixel when YUV conversion is performed.
DRC101f09a2010-02-12 22:52:37 +0000198
DRC68fef832010-02-16 05:29:10 +0000199Your application can check for the existence of these extensions at compile
200time with:
DRC101f09a2010-02-12 22:52:37 +0000201
DRC68fef832010-02-16 05:29:10 +0000202 #ifdef JCS_EXTENSIONS
DRC101f09a2010-02-12 22:52:37 +0000203
DRC68fef832010-02-16 05:29:10 +0000204At run time, attempting to use these extensions with a version of libjpeg
205that doesn't support them will result in a "Bogus input colorspace" error.
DRC77e39642010-10-12 03:02:31 +0000206
207=================================
208libjpeg v7 and v8 API/ABI support
209=================================
210
211libjpeg v7 and v8 added new features to the API/ABI, and, unfortunately, the
212compression and decompression structures were extended in a backward-
213incompatible manner to accommodate these features. Thus, programs which are
214built to use libjpeg v7 or v8 did not work with libjpeg-turbo, since it is
215based on the libjpeg v6b code base. Although libjpeg v7 and v8 are still not
216as widely used as v6b, enough programs (including a few Linux distros) have
217made the switch that it was desirable to provide support for the libjpeg v7/v8
218API/ABI in libjpeg-turbo.
219
220Some of the libjpeg v7 and v8 features -- DCT scaling, to name one -- involve
221deep modifications to the code which cannot be accommodated by libjpeg-turbo
222without either breaking compatibility with libjpeg v6b or producing an
223unsupportable mess. In order to fully support libjpeg v8 with all of its
224features, we would have to essentially port the SIMD extensions to the libjpeg
225v8 code base and maintain two separate code trees. We are hesitant to do this
226until/unless the newer libjpeg code bases garner more community support and
227involvement and until/unless we have some notion of whether future libjpeg
228releases will also be backward-incompatible.
229
DRC5559c902010-10-18 02:21:10 +0000230By passing an argument of --with-jpeg7 or --with-jpeg8 to configure, or an
231argument of -DWITH_JPEG7=1 or -DWITH_JPEG8=1 to cmake, you can build a version
DRCab4db652011-02-18 04:55:08 +0000232of libjpeg-turbo which emulates the libjpeg v7 or v8 API/ABI, so that programs
DRC5559c902010-10-18 02:21:10 +0000233which are built against libjpeg v7 or v8 can be run with libjpeg-turbo. The
234following section describes which libjpeg v7+ features are supported and which
235aren't.
DRC77e39642010-10-12 03:02:31 +0000236
237libjpeg v7 and v8 Features:
238---------------------------
239
240Fully supported:
241
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.)
246
247-- cjpeg: 32-bit BMP support
248
249-- jpegtran: lossless cropping
250
251-- jpegtran: -perfect option
252
253-- rdjpgcom: -raw option
254
255-- rdjpgcom: locale awareness
256
257
258Fully supported when using libjpeg v7/v8 emulation:
259
260-- libjpeg: In-memory source and destination managers
261
262
263Not supported:
264
265-- libjpeg: DCT scaling in compressor
266 cinfo.scale_num and cinfo.scale_denom are silently ignored.
267
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.)
271
272-- libjpeg: Fancy downsampling in compressor
273 cinfo.do_fancy_downsampling is silently ignored.
274
DRC77e39642010-10-12 03:02:31 +0000275-- jpegtran: Scaling
276 Seems to depend on the DCT scaling feature, which isn't supported.
DRCab4db652011-02-18 04:55:08 +0000277
278
279*******************************************************************************
280** Performance pitfalls
281*******************************************************************************
282
283===============
284Restart Markers
285===============
286
287The optimized Huffman decoder in libjpeg-turbo does not handle restart markers
288in a way that makes libjpeg happy, so it is necessary to use the slow Huffman
289decoder when decompressing a JPEG image that has restart markers. This can
290cause the decompression performance to drop by as much as 20%, but the
291performance will still be much much greater than that of libjpeg v6b. Many
292consumer packages, such as PhotoShop, use restart markers when generating JPEG
293images, so images generated by those programs will experience this issue.
294
295===============================================
296Fast Integer Forward DCT at High Quality Levels
297===============================================
298
299The algorithm used by the SIMD-accelerated quantization function cannot produce
300correct results whenever the fast integer forward DCT is used along with a JPEG
301quality of 98-100. Thus, libjpeg-turbo must use the non-SIMD quantization
302function in those cases. This causes performance to drop by as much as 40%.
303It is therefore strongly advised that you use the slow integer forward DCT
304whenever encoding images with a JPEG quality of 98 or higher.