1 /*
2  * jddctmgr.c
3  *
4  * Copyright (C) 1994-1996, Thomas G. Lane.
5  * This file is part of the Independent JPEG Group's software.
6  * For conditions of distribution and use, see the accompanying README file.
7  *
8  * This file contains the inverse-DCT management logic.
9  * This code selects a particular IDCT implementation to be used,
10  * and it performs related housekeeping chores.  No code in this file
11  * is executed per IDCT step, only during output pass setup.
12  *
13  * Note that the IDCT routines are responsible for performing coefficient
14  * dequantization as well as the IDCT proper.  This module sets up the
15  * dequantization multiplier table needed by the IDCT routine.
16  */
17 
18 #define JPEG_INTERNALS
19 #include "jinclude.h"
20 #include "jpeglib.h"
21 #include "jdct.h"		/* Private declarations for DCT subsystem */
22 
23 
24 /*
25  * The decompressor input side (jdinput.c) saves away the appropriate
26  * quantization table for each component at the start of the first scan
27  * involving that component.  (This is necessary in order to correctly
28  * decode files that reuse Q-table slots.)
29  * When we are ready to make an output pass, the saved Q-table is converted
30  * to a multiplier table that will actually be used by the IDCT routine.
31  * The multiplier table contents are IDCT-method-dependent.  To support
32  * application changes in IDCT method between scans, we can remake the
33  * multiplier tables if necessary.
34  * In buffered-image mode, the first output pass may occur before any data
35  * has been seen for some components, and thus before their Q-tables have
36  * been saved away.  To handle this case, multiplier tables are preset
37  * to zeroes; the result of the IDCT will be a neutral gray level.
38  */
39 
40 
41 /* Private subobject for this module */
42 
43 typedef struct {
44   struct jpeg_inverse_dct pub;	/* public fields */
45 
46   /* This array contains the IDCT method code that each multiplier table
47    * is currently set up for, or -1 if it's not yet set up.
48    * The actual multiplier tables are pointed to by dct_table in the
49    * per-component comp_info structures.
50    */
51   int cur_method[MAX_COMPONENTS];
52 } my_idct_controller;
53 
54 typedef my_idct_controller * my_idct_ptr;
55 
56 
57 /* Allocated multiplier tables: big enough for any supported variant */
58 
59 typedef union {
60   ISLOW_MULT_TYPE islow_array[DCTSIZE2];
61 #ifdef DCT_IFAST_SUPPORTED
62   IFAST_MULT_TYPE ifast_array[DCTSIZE2];
63 #endif
64 #ifdef DCT_FLOAT_SUPPORTED
65   FLOAT_MULT_TYPE float_array[DCTSIZE2];
66 #endif
67 } multiplier_table;
68 
69 
70 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
71  * so be sure to compile that code if either ISLOW or SCALING is requested.
72  */
73 #ifdef DCT_ISLOW_SUPPORTED
74 #define PROVIDE_ISLOW_TABLES
75 #else
76 #ifdef IDCT_SCALING_SUPPORTED
77 #define PROVIDE_ISLOW_TABLES
78 #endif
79 #endif
80 
81 
82 /*
83  * Prepare for an output pass.
84  * Here we select the proper IDCT routine for each component and build
85  * a matching multiplier table.
86  */
87 
88 METHODDEF(void)
start_pass(j_decompress_ptr cinfo)89 start_pass (j_decompress_ptr cinfo)
90 {
91   my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
92   int ci, i;
93   jpeg_component_info *compptr;
94   int method = 0;
95   inverse_DCT_method_ptr method_ptr = NULL;
96   JQUANT_TBL * qtbl;
97 
98   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
99        ci++, compptr++) {
100     /* Select the proper IDCT routine for this component's scaling */
101     switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) {
102 #ifdef IDCT_SCALING_SUPPORTED
103     case ((1 << 8) + 1):
104       method_ptr = jpeg_idct_1x1;
105       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
106       break;
107     case ((2 << 8) + 2):
108       method_ptr = jpeg_idct_2x2;
109       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
110       break;
111     case ((3 << 8) + 3):
112       method_ptr = jpeg_idct_3x3;
113       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
114       break;
115     case ((4 << 8) + 4):
116       method_ptr = jpeg_idct_4x4;
117       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
118       break;
119     case ((5 << 8) + 5):
120       method_ptr = jpeg_idct_5x5;
121       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
122       break;
123     case ((6 << 8) + 6):
124       method_ptr = jpeg_idct_6x6;
125       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
126       break;
127     case ((7 << 8) + 7):
128       method_ptr = jpeg_idct_7x7;
129       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
130       break;
131     case ((9 << 8) + 9):
132       method_ptr = jpeg_idct_9x9;
133       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
134       break;
135     case ((10 << 8) + 10):
136       method_ptr = jpeg_idct_10x10;
137       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
138       break;
139     case ((11 << 8) + 11):
140       method_ptr = jpeg_idct_11x11;
141       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
142       break;
143     case ((12 << 8) + 12):
144       method_ptr = jpeg_idct_12x12;
145       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
146       break;
147     case ((13 << 8) + 13):
148       method_ptr = jpeg_idct_13x13;
149       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
150       break;
151     case ((14 << 8) + 14):
152       method_ptr = jpeg_idct_14x14;
153       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
154       break;
155     case ((15 << 8) + 15):
156       method_ptr = jpeg_idct_15x15;
157       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
158       break;
159     case ((16 << 8) + 16):
160       method_ptr = jpeg_idct_16x16;
161       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
162       break;
163     case ((16 << 8) + 8):
164       method_ptr = jpeg_idct_16x8;
165       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
166       break;
167     case ((14 << 8) + 7):
168       method_ptr = jpeg_idct_14x7;
169       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
170       break;
171     case ((12 << 8) + 6):
172       method_ptr = jpeg_idct_12x6;
173       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
174       break;
175     case ((10 << 8) + 5):
176       method_ptr = jpeg_idct_10x5;
177       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
178       break;
179     case ((8 << 8) + 4):
180       method_ptr = jpeg_idct_8x4;
181       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
182       break;
183     case ((6 << 8) + 3):
184       method_ptr = jpeg_idct_6x3;
185       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
186       break;
187     case ((4 << 8) + 2):
188       method_ptr = jpeg_idct_4x2;
189       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
190       break;
191     case ((2 << 8) + 1):
192       method_ptr = jpeg_idct_2x1;
193       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
194       break;
195     case ((8 << 8) + 16):
196       method_ptr = jpeg_idct_8x16;
197       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
198       break;
199     case ((7 << 8) + 14):
200       method_ptr = jpeg_idct_7x14;
201       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
202       break;
203     case ((6 << 8) + 12):
204       method_ptr = jpeg_idct_6x12;
205       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
206       break;
207     case ((5 << 8) + 10):
208       method_ptr = jpeg_idct_5x10;
209       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
210       break;
211     case ((4 << 8) + 8):
212       method_ptr = jpeg_idct_4x8;
213       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
214       break;
215     case ((3 << 8) + 6):
216       method_ptr = jpeg_idct_3x6;
217       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
218       break;
219     case ((2 << 8) + 4):
220       method_ptr = jpeg_idct_2x4;
221       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
222       break;
223     case ((1 << 8) + 2):
224       method_ptr = jpeg_idct_1x2;
225       method = JDCT_ISLOW;	/* jidctint uses islow-style table */
226       break;
227 #endif
228     case ((DCTSIZE << 8) + DCTSIZE):
229       switch (cinfo->dct_method) {
230 #ifdef DCT_ISLOW_SUPPORTED
231       case JDCT_ISLOW:
232 	method_ptr = jpeg_idct_islow;
233 	method = JDCT_ISLOW;
234 	break;
235 #endif
236 #ifdef DCT_IFAST_SUPPORTED
237       case JDCT_IFAST:
238 	method_ptr = jpeg_idct_ifast;
239 	method = JDCT_IFAST;
240 	break;
241 #endif
242 #ifdef DCT_FLOAT_SUPPORTED
243       case JDCT_FLOAT:
244 	method_ptr = jpeg_idct_float;
245 	method = JDCT_FLOAT;
246 	break;
247 #endif
248       default:
249 	ERREXIT(cinfo, JERR_NOT_COMPILED);
250 	break;
251       }
252       break;
253     default:
254       ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
255 	       compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
256       break;
257     }
258     idct->pub.inverse_DCT[ci] = method_ptr;
259     /* Create multiplier table from quant table.
260      * However, we can skip this if the component is uninteresting
261      * or if we already built the table.  Also, if no quant table
262      * has yet been saved for the component, we leave the
263      * multiplier table all-zero; we'll be reading zeroes from the
264      * coefficient controller's buffer anyway.
265      */
266     if (! compptr->component_needed || idct->cur_method[ci] == method)
267       continue;
268     qtbl = compptr->quant_table;
269     if (qtbl == NULL)		/* happens if no data yet for component */
270       continue;
271     idct->cur_method[ci] = method;
272     switch (method) {
273 #ifdef PROVIDE_ISLOW_TABLES
274     case JDCT_ISLOW:
275       {
276 	/* For LL&M IDCT method, multipliers are equal to raw quantization
277 	 * coefficients, but are stored as ints to ensure access efficiency.
278 	 */
279 	ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
280 	for (i = 0; i < DCTSIZE2; i++) {
281 	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
282 	}
283       }
284       break;
285 #endif
286 #ifdef DCT_IFAST_SUPPORTED
287     case JDCT_IFAST:
288       {
289 	/* For 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
293 	 * For integer operation, the multiplier table is to be scaled by
294 	 * IFAST_SCALE_BITS.
295 	 */
296 	IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
297 #define CONST_BITS 14
298 	static const INT16 aanscales[DCTSIZE2] = {
299 	  /* precomputed values scaled up by 14 bits */
300 	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
301 	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
302 	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
303 	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
304 	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
305 	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
306 	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
307 	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
308 	};
309 	SHIFT_TEMPS
310 
311 	for (i = 0; i < DCTSIZE2; i++) {
312 	  ifmtbl[i] = (IFAST_MULT_TYPE)
313 	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
314 				  (INT32) aanscales[i]),
315 		    CONST_BITS-IFAST_SCALE_BITS);
316 	}
317       }
318       break;
319 #endif
320 #ifdef DCT_FLOAT_SUPPORTED
321     case JDCT_FLOAT:
322       {
323 	/* For float AA&N IDCT method, multipliers are equal to quantization
324 	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
325 	 *   scalefactor[0] = 1
326 	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
327 	 */
328 	FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
329 	int row, col;
330 	static const double aanscalefactor[DCTSIZE] = {
331 	  1.0, 1.387039845, 1.306562965, 1.175875602,
332 	  1.0, 0.785694958, 0.541196100, 0.275899379
333 	};
334 
335 	i = 0;
336 	for (row = 0; row < DCTSIZE; row++) {
337 	  for (col = 0; col < DCTSIZE; col++) {
338 	    fmtbl[i] = (FLOAT_MULT_TYPE)
339 	      ((double) qtbl->quantval[i] *
340 	       aanscalefactor[row] * aanscalefactor[col]);
341 	    i++;
342 	  }
343 	}
344       }
345       break;
346 #endif
347     default:
348       ERREXIT(cinfo, JERR_NOT_COMPILED);
349       break;
350     }
351   }
352 }
353 
354 
355 /*
356  * Initialize IDCT manager.
357  */
358 
359 GLOBAL(void)
jinit_inverse_dct(j_decompress_ptr cinfo)360 jinit_inverse_dct (j_decompress_ptr cinfo)
361 {
362   my_idct_ptr idct;
363   int ci;
364   jpeg_component_info *compptr;
365 
366   idct = (my_idct_ptr)
367     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
368 				SIZEOF(my_idct_controller));
369   cinfo->idct = (struct jpeg_inverse_dct *) idct;
370   idct->pub.start_pass = start_pass;
371 
372   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
373        ci++, compptr++) {
374     /* Allocate and pre-zero a multiplier table for each component */
375     compptr->dct_table =
376       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
377 				  SIZEOF(multiplier_table));
378     MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
379     /* Mark multiplier table not yet set up for any method */
380     idct->cur_method[ci] = -1;
381   }
382 }
383