blob: 88b47073354d17ef8d328bf40cfa66e7f8c6d40b [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jddctmgr.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
Guido Vollbedingf18f81b2010-02-28 00:00:00 +00006 * Modified 2002-2010 by Guido Vollbeding.
DRCa6ef2822013-09-28 03:23:49 +00007 * libjpeg-turbo Modifications:
Pierre Ossman59a39382009-03-09 13:15:56 +00008 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC36a6eec2010-10-08 08:05:44 +00009 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 * For conditions of distribution and use, see the accompanying README file.
11 *
12 * This file contains the inverse-DCT management logic.
13 * This code selects a particular IDCT implementation to be used,
14 * and it performs related housekeeping chores. No code in this file
Thomas G. Lanebc79e061995-08-02 00:00:00 +000015 * is executed per IDCT step, only during output pass setup.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016 *
17 * Note that the IDCT routines are responsible for performing coefficient
18 * dequantization as well as the IDCT proper. This module sets up the
19 * dequantization multiplier table needed by the IDCT routine.
20 */
21
22#define JPEG_INTERNALS
23#include "jinclude.h"
24#include "jpeglib.h"
25#include "jdct.h" /* Private declarations for DCT subsystem */
Pierre Ossman59a39382009-03-09 13:15:56 +000026#include "jsimddct.h"
DRC36a6eec2010-10-08 08:05:44 +000027#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000028
29
Thomas G. Lanebc79e061995-08-02 00:00:00 +000030/*
31 * The decompressor input side (jdinput.c) saves away the appropriate
32 * quantization table for each component at the start of the first scan
33 * involving that component. (This is necessary in order to correctly
34 * decode files that reuse Q-table slots.)
35 * When we are ready to make an output pass, the saved Q-table is converted
36 * to a multiplier table that will actually be used by the IDCT routine.
37 * The multiplier table contents are IDCT-method-dependent. To support
38 * application changes in IDCT method between scans, we can remake the
39 * multiplier tables if necessary.
40 * In buffered-image mode, the first output pass may occur before any data
41 * has been seen for some components, and thus before their Q-tables have
42 * been saved away. To handle this case, multiplier tables are preset
43 * to zeroes; the result of the IDCT will be a neutral gray level.
44 */
45
46
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047/* Private subobject for this module */
48
49typedef struct {
50 struct jpeg_inverse_dct pub; /* public fields */
51
Thomas G. Lanebc79e061995-08-02 00:00:00 +000052 /* This array contains the IDCT method code that each multiplier table
53 * is currently set up for, or -1 if it's not yet set up.
54 * The actual multiplier tables are pointed to by dct_table in the
55 * per-component comp_info structures.
56 */
57 int cur_method[MAX_COMPONENTS];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000058} my_idct_controller;
59
60typedef my_idct_controller * my_idct_ptr;
61
62
Thomas G. Lanebc79e061995-08-02 00:00:00 +000063/* Allocated multiplier tables: big enough for any supported variant */
64
65typedef union {
66 ISLOW_MULT_TYPE islow_array[DCTSIZE2];
67#ifdef DCT_IFAST_SUPPORTED
68 IFAST_MULT_TYPE ifast_array[DCTSIZE2];
69#endif
70#ifdef DCT_FLOAT_SUPPORTED
71 FLOAT_MULT_TYPE float_array[DCTSIZE2];
72#endif
73} multiplier_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074
75
76/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
77 * so be sure to compile that code if either ISLOW or SCALING is requested.
78 */
79#ifdef DCT_ISLOW_SUPPORTED
80#define PROVIDE_ISLOW_TABLES
81#else
82#ifdef IDCT_SCALING_SUPPORTED
83#define PROVIDE_ISLOW_TABLES
84#endif
85#endif
86
87
88/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000089 * Prepare for an output pass.
90 * Here we select the proper IDCT routine for each component and build
91 * a matching multiplier table.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092 */
93
Thomas G. Lane489583f1996-02-07 00:00:00 +000094METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000095start_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000096{
97 my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000098 int ci, i;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000099 jpeg_component_info *compptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000100 int method = 0;
101 inverse_DCT_method_ptr method_ptr = NULL;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000102 JQUANT_TBL * qtbl;
103
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000104 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
105 ci++, compptr++) {
106 /* Select the proper IDCT routine for this component's scaling */
DRC49967cd2010-10-09 19:57:51 +0000107 switch (compptr->_DCT_scaled_size) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000108#ifdef IDCT_SCALING_SUPPORTED
109 case 1:
110 method_ptr = jpeg_idct_1x1;
111 method = JDCT_ISLOW; /* jidctred uses islow-style table */
112 break;
113 case 2:
Pierre Ossman59a39382009-03-09 13:15:56 +0000114 if (jsimd_can_idct_2x2())
115 method_ptr = jsimd_idct_2x2;
116 else
117 method_ptr = jpeg_idct_2x2;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000118 method = JDCT_ISLOW; /* jidctred uses islow-style table */
119 break;
DRC27fb3fc2012-01-28 01:48:07 +0000120 case 3:
Guido Vollbeding5996a252009-06-27 00:00:00 +0000121 method_ptr = jpeg_idct_3x3;
122 method = JDCT_ISLOW; /* jidctint uses islow-style table */
123 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000124 case 4:
Pierre Ossman59a39382009-03-09 13:15:56 +0000125 if (jsimd_can_idct_4x4())
126 method_ptr = jsimd_idct_4x4;
127 else
128 method_ptr = jpeg_idct_4x4;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000129 method = JDCT_ISLOW; /* jidctred uses islow-style table */
130 break;
DRC27fb3fc2012-01-28 01:48:07 +0000131 case 5:
Guido Vollbeding5996a252009-06-27 00:00:00 +0000132 method_ptr = jpeg_idct_5x5;
133 method = JDCT_ISLOW; /* jidctint uses islow-style table */
134 break;
DRC27fb3fc2012-01-28 01:48:07 +0000135 case 6:
DRCe5005912013-09-27 17:51:08 +0000136#if defined(__mips__)
137 if (jsimd_can_idct_6x6())
138 method_ptr = jsimd_idct_6x6;
139 else
140#endif
Guido Vollbeding5996a252009-06-27 00:00:00 +0000141 method_ptr = jpeg_idct_6x6;
142 method = JDCT_ISLOW; /* jidctint uses islow-style table */
143 break;
DRC27fb3fc2012-01-28 01:48:07 +0000144 case 7:
Guido Vollbeding5996a252009-06-27 00:00:00 +0000145 method_ptr = jpeg_idct_7x7;
146 method = JDCT_ISLOW; /* jidctint uses islow-style table */
147 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000148#endif
149 case DCTSIZE:
150 switch (cinfo->dct_method) {
151#ifdef DCT_ISLOW_SUPPORTED
152 case JDCT_ISLOW:
Pierre Ossman59a39382009-03-09 13:15:56 +0000153 if (jsimd_can_idct_islow())
154 method_ptr = jsimd_idct_islow;
155 else
156 method_ptr = jpeg_idct_islow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000157 method = JDCT_ISLOW;
158 break;
159#endif
160#ifdef DCT_IFAST_SUPPORTED
161 case JDCT_IFAST:
Pierre Ossman59a39382009-03-09 13:15:56 +0000162 if (jsimd_can_idct_ifast())
163 method_ptr = jsimd_idct_ifast;
164 else
165 method_ptr = jpeg_idct_ifast;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000166 method = JDCT_IFAST;
167 break;
168#endif
169#ifdef DCT_FLOAT_SUPPORTED
170 case JDCT_FLOAT:
Pierre Ossman59a39382009-03-09 13:15:56 +0000171 if (jsimd_can_idct_float())
172 method_ptr = jsimd_idct_float;
173 else
174 method_ptr = jpeg_idct_float;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000175 method = JDCT_FLOAT;
176 break;
177#endif
178 default:
179 ERREXIT(cinfo, JERR_NOT_COMPILED);
180 break;
181 }
182 break;
DRC27fb3fc2012-01-28 01:48:07 +0000183 case 9:
184 method_ptr = jpeg_idct_9x9;
185 method = JDCT_ISLOW; /* jidctint uses islow-style table */
186 break;
187 case 10:
188 method_ptr = jpeg_idct_10x10;
189 method = JDCT_ISLOW; /* jidctint uses islow-style table */
190 break;
191 case 11:
192 method_ptr = jpeg_idct_11x11;
193 method = JDCT_ISLOW; /* jidctint uses islow-style table */
194 break;
195 case 12:
DRCe5005912013-09-27 17:51:08 +0000196#if defined(__mips__)
197 if (jsimd_can_idct_12x12())
198 method_ptr = jsimd_idct_12x12;
199 else
200#endif
DRC27fb3fc2012-01-28 01:48:07 +0000201 method_ptr = jpeg_idct_12x12;
202 method = JDCT_ISLOW; /* jidctint uses islow-style table */
203 break;
204 case 13:
205 method_ptr = jpeg_idct_13x13;
206 method = JDCT_ISLOW; /* jidctint uses islow-style table */
207 break;
208 case 14:
209 method_ptr = jpeg_idct_14x14;
210 method = JDCT_ISLOW; /* jidctint uses islow-style table */
211 break;
212 case 15:
213 method_ptr = jpeg_idct_15x15;
214 method = JDCT_ISLOW; /* jidctint uses islow-style table */
215 break;
216 case 16:
217 method_ptr = jpeg_idct_16x16;
218 method = JDCT_ISLOW; /* jidctint uses islow-style table */
219 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000220 default:
DRC49967cd2010-10-09 19:57:51 +0000221 ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000222 break;
223 }
224 idct->pub.inverse_DCT[ci] = method_ptr;
225 /* Create multiplier table from quant table.
226 * However, we can skip this if the component is uninteresting
227 * or if we already built the table. Also, if no quant table
228 * has yet been saved for the component, we leave the
229 * multiplier table all-zero; we'll be reading zeroes from the
230 * coefficient controller's buffer anyway.
231 */
232 if (! compptr->component_needed || idct->cur_method[ci] == method)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233 continue;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000234 qtbl = compptr->quant_table;
235 if (qtbl == NULL) /* happens if no data yet for component */
236 continue;
237 idct->cur_method[ci] = method;
238 switch (method) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000239#ifdef PROVIDE_ISLOW_TABLES
240 case JDCT_ISLOW:
241 {
242 /* For LL&M IDCT method, multipliers are equal to raw quantization
Thomas G. Lane489583f1996-02-07 00:00:00 +0000243 * coefficients, but are stored as ints to ensure access efficiency.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000244 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000245 ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000246 for (i = 0; i < DCTSIZE2; i++) {
Thomas G. Lane489583f1996-02-07 00:00:00 +0000247 ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000248 }
249 }
250 break;
251#endif
252#ifdef DCT_IFAST_SUPPORTED
253 case JDCT_IFAST:
254 {
255 /* For AA&N IDCT method, multipliers are equal to quantization
256 * coefficients scaled by scalefactor[row]*scalefactor[col], where
257 * scalefactor[0] = 1
258 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
259 * For integer operation, the multiplier table is to be scaled by
Thomas G. Lane489583f1996-02-07 00:00:00 +0000260 * IFAST_SCALE_BITS.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000261 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000262 IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000263#define CONST_BITS 14
264 static const INT16 aanscales[DCTSIZE2] = {
265 /* precomputed values scaled up by 14 bits */
266 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
267 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
268 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
269 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
270 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
271 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
272 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
273 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
274 };
275 SHIFT_TEMPS
276
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277 for (i = 0; i < DCTSIZE2; i++) {
278 ifmtbl[i] = (IFAST_MULT_TYPE)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000279 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000280 (INT32) aanscales[i]),
281 CONST_BITS-IFAST_SCALE_BITS);
282 }
283 }
284 break;
285#endif
286#ifdef DCT_FLOAT_SUPPORTED
287 case JDCT_FLOAT:
288 {
289 /* For float AA&N IDCT method, multipliers are equal to quantization
290 * coefficients scaled by scalefactor[row]*scalefactor[col], where
291 * scalefactor[0] = 1
292 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000293 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000294 FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000295 int row, col;
296 static const double aanscalefactor[DCTSIZE] = {
297 1.0, 1.387039845, 1.306562965, 1.175875602,
298 1.0, 0.785694958, 0.541196100, 0.275899379
299 };
300
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000301 i = 0;
302 for (row = 0; row < DCTSIZE; row++) {
303 for (col = 0; col < DCTSIZE; col++) {
304 fmtbl[i] = (FLOAT_MULT_TYPE)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000305 ((double) qtbl->quantval[i] *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000306 aanscalefactor[row] * aanscalefactor[col]);
307 i++;
308 }
309 }
310 }
311 break;
312#endif
313 default:
314 ERREXIT(cinfo, JERR_NOT_COMPILED);
315 break;
316 }
317 }
318}
319
320
321/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000322 * Initialize IDCT manager.
323 */
324
Thomas G. Lane489583f1996-02-07 00:00:00 +0000325GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000326jinit_inverse_dct (j_decompress_ptr cinfo)
327{
328 my_idct_ptr idct;
329 int ci;
330 jpeg_component_info *compptr;
331
332 idct = (my_idct_ptr)
333 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
334 SIZEOF(my_idct_controller));
335 cinfo->idct = (struct jpeg_inverse_dct *) idct;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000336 idct->pub.start_pass = start_pass;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000337
338 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
339 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000340 /* Allocate and pre-zero a multiplier table for each component */
341 compptr->dct_table =
342 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
343 SIZEOF(multiplier_table));
344 MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
345 /* Mark multiplier table not yet set up for any method */
346 idct->cur_method[ci] = -1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000347 }
348}