• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef NV_ARM_NEON
24 #include "jsimd_neon.h"
25 #endif
26 
27 #ifdef ANDROID_ARMV6_IDCT
28 
29 /* Intentionally declare the prototype with arguments of primitive types instead
30  * of type-defined ones. This will at least generate some warnings if jmorecfg.h
31  * is changed and becomes incompatible with the assembly code.
32  */
33 extern void armv6_idct(short *coefs, int *quans, unsigned char **rows, int col);
34 
jpeg_idct_armv6(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)35 void jpeg_idct_armv6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
36 		 JCOEFPTR coef_block,
37 		 JSAMPARRAY output_buf, JDIMENSION output_col)
38 {
39   IFAST_MULT_TYPE *dct_table = (IFAST_MULT_TYPE *)compptr->dct_table;
40   armv6_idct(coef_block, dct_table, output_buf, output_col);
41 }
42 
43 #endif
44 
45 #ifdef ANDROID_INTELSSE2_IDCT
46 extern short __attribute__((aligned(16))) quantptrSSE[DCTSIZE2];
47 extern void jpeg_idct_intelsse (j_decompress_ptr cinfo, jpeg_component_info * compptr,
48 		JCOEFPTR coef_block,
49 		JSAMPARRAY output_buf, JDIMENSION output_col);
50 #endif
51 
52 #ifdef ANDROID_MIPS_IDCT
53 extern void jpeg_idct_mips(j_decompress_ptr, jpeg_component_info *, JCOEFPTR, JSAMPARRAY, JDIMENSION);
54 #endif
55 
56 /*
57  * The decompressor input side (jdinput.c) saves away the appropriate
58  * quantization table for each component at the start of the first scan
59  * involving that component.  (This is necessary in order to correctly
60  * decode files that reuse Q-table slots.)
61  * When we are ready to make an output pass, the saved Q-table is converted
62  * to a multiplier table that will actually be used by the IDCT routine.
63  * The multiplier table contents are IDCT-method-dependent.  To support
64  * application changes in IDCT method between scans, we can remake the
65  * multiplier tables if necessary.
66  * In buffered-image mode, the first output pass may occur before any data
67  * has been seen for some components, and thus before their Q-tables have
68  * been saved away.  To handle this case, multiplier tables are preset
69  * to zeroes; the result of the IDCT will be a neutral gray level.
70  */
71 
72 
73 /* Private subobject for this module */
74 
75 typedef struct {
76   struct jpeg_inverse_dct pub;	/* public fields */
77 
78   /* This array contains the IDCT method code that each multiplier table
79    * is currently set up for, or -1 if it's not yet set up.
80    * The actual multiplier tables are pointed to by dct_table in the
81    * per-component comp_info structures.
82    */
83   int cur_method[MAX_COMPONENTS];
84 } my_idct_controller;
85 
86 typedef my_idct_controller * my_idct_ptr;
87 
88 
89 /* Allocated multiplier tables: big enough for any supported variant */
90 
91 typedef union {
92   ISLOW_MULT_TYPE islow_array[DCTSIZE2];
93 #ifdef DCT_IFAST_SUPPORTED
94   IFAST_MULT_TYPE ifast_array[DCTSIZE2];
95 #endif
96 #ifdef DCT_FLOAT_SUPPORTED
97   FLOAT_MULT_TYPE float_array[DCTSIZE2];
98 #endif
99 } multiplier_table;
100 
101 
102 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
103  * so be sure to compile that code if either ISLOW or SCALING is requested.
104  */
105 #ifdef DCT_ISLOW_SUPPORTED
106 #define PROVIDE_ISLOW_TABLES
107 #else
108 #ifdef IDCT_SCALING_SUPPORTED
109 #define PROVIDE_ISLOW_TABLES
110 #endif
111 #endif
112 
113 
114 /*
115  * Prepare for an output pass.
116  * Here we select the proper IDCT routine for each component and build
117  * a matching multiplier table.
118  */
119 
120 METHODDEF(void)
start_pass(j_decompress_ptr cinfo)121 start_pass (j_decompress_ptr cinfo)
122 {
123   my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
124   int ci, i;
125   jpeg_component_info *compptr;
126   int method = 0;
127   inverse_DCT_method_ptr method_ptr = NULL;
128   JQUANT_TBL * qtbl;
129 
130   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
131        ci++, compptr++) {
132     /* Select the proper IDCT routine for this component's scaling */
133     switch (compptr->DCT_scaled_size) {
134 #ifdef IDCT_SCALING_SUPPORTED
135     case 1:
136       method_ptr = jpeg_idct_1x1;
137       method = JDCT_ISLOW;	/* jidctred uses islow-style table */
138       break;
139     case 2:
140 #if defined(NV_ARM_NEON) && defined(__ARM_HAVE_NEON)
141       if (cap_neon_idct_2x2()) {
142         method_ptr = jsimd_idct_2x2;
143       } else {
144         method_ptr = jpeg_idct_2x2;
145       }
146 #else
147       method_ptr = jpeg_idct_2x2;
148 #endif
149       method = JDCT_ISLOW;	/* jidctred uses islow-style table */
150       break;
151     case 4:
152 #if defined(NV_ARM_NEON) && defined(__ARM_HAVE_NEON)
153 	  if (cap_neon_idct_4x4()) {
154         method_ptr = jsimd_idct_4x4;
155       } else {
156         method_ptr = jpeg_idct_4x4;
157       }
158 #else
159       method_ptr = jpeg_idct_4x4;
160 #endif
161       method = JDCT_ISLOW;	/* jidctred uses islow-style table */
162       break;
163 #endif
164     case DCTSIZE:
165       switch (cinfo->dct_method) {
166 #ifdef ANDROID_ARMV6_IDCT
167       case JDCT_ISLOW:
168       case JDCT_IFAST:
169 	method_ptr = jpeg_idct_armv6;
170 	method = JDCT_IFAST;
171 	break;
172 #else /* ANDROID_ARMV6_IDCT */
173 #ifdef ANDROID_INTELSSE2_IDCT
174       case JDCT_ISLOW:
175       case JDCT_IFAST:
176 	method_ptr = jpeg_idct_intelsse;
177 	method = JDCT_ISLOW; /* Use quant table of ISLOW.*/
178 	break;
179 #else /* ANDROID_INTELSSE2_IDCT */
180 #ifdef ANDROID_MIPS_IDCT
181       case JDCT_ISLOW:
182       case JDCT_IFAST:
183 	method_ptr = jpeg_idct_mips;
184 	method = JDCT_IFAST;
185 	break;
186 #else /* ANDROID_MIPS_IDCT */
187 #ifdef DCT_ISLOW_SUPPORTED
188       case JDCT_ISLOW:
189 	method_ptr = jpeg_idct_islow;
190 	method = JDCT_ISLOW;
191 	break;
192 #endif
193 #ifdef DCT_IFAST_SUPPORTED
194       case JDCT_IFAST:
195 #if defined(NV_ARM_NEON) && defined(__ARM_HAVE_NEON)
196         if (cap_neon_idct_ifast()) {
197           method_ptr = jsimd_idct_ifast;
198         } else {
199           method_ptr = jpeg_idct_ifast;
200         }
201 #else
202         method_ptr = jpeg_idct_ifast;
203 #endif
204 	method = JDCT_IFAST;
205 	break;
206 #endif
207 #endif /* ANDROID_MIPS_IDCT */
208 #endif /* ANDROID_INTELSSE2_IDCT*/
209 #endif /* ANDROID_ARMV6_IDCT */
210 #ifdef DCT_FLOAT_SUPPORTED
211       case JDCT_FLOAT:
212 	method_ptr = jpeg_idct_float;
213 	method = JDCT_FLOAT;
214 	break;
215 #endif
216       default:
217 	ERREXIT(cinfo, JERR_NOT_COMPILED);
218 	break;
219       }
220       break;
221     default:
222       ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
223       break;
224     }
225     idct->pub.inverse_DCT[ci] = method_ptr;
226     /* Create multiplier table from quant table.
227      * However, we can skip this if the component is uninteresting
228      * or if we already built the table.  Also, if no quant table
229      * has yet been saved for the component, we leave the
230      * multiplier table all-zero; we'll be reading zeroes from the
231      * coefficient controller's buffer anyway.
232      */
233     if (! compptr->component_needed || idct->cur_method[ci] == method)
234       continue;
235     qtbl = compptr->quant_table;
236     if (qtbl == NULL)		/* happens if no data yet for component */
237       continue;
238     idct->cur_method[ci] = method;
239     switch (method) {
240 #ifdef PROVIDE_ISLOW_TABLES
241     case JDCT_ISLOW:
242       {
243 	/* For LL&M IDCT method, multipliers are equal to raw quantization
244 	 * coefficients, but are stored as ints to ensure access efficiency.
245 	 */
246 	ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
247 	for (i = 0; i < DCTSIZE2; i++) {
248 	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
249 	}
250       }
251       break;
252 #endif
253 #ifdef DCT_IFAST_SUPPORTED
254     case JDCT_IFAST:
255       {
256 	/* For AA&N IDCT method, multipliers are equal to quantization
257 	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
258 	 *   scalefactor[0] = 1
259 	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
260 	 * For integer operation, the multiplier table is to be scaled by
261 	 * IFAST_SCALE_BITS.
262 	 */
263 	IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
264 #ifdef ANDROID_ARMV6_IDCT
265 	/* Precomputed values scaled up by 15 bits. */
266 	static const unsigned short scales[DCTSIZE2] = {
267 	  32768, 45451, 42813, 38531, 32768, 25746, 17734,  9041,
268 	  45451, 63042, 59384, 53444, 45451, 35710, 24598, 12540,
269 	  42813, 59384, 55938, 50343, 42813, 33638, 23170, 11812,
270 	  38531, 53444, 50343, 45308, 38531, 30274, 20853, 10631,
271 	  32768, 45451, 42813, 38531, 32768, 25746, 17734,  9041,
272 	  25746, 35710, 33638, 30274, 25746, 20228, 13933,  7103,
273 	  17734, 24598, 23170, 20853, 17734, 13933,  9598,  4893,
274 	   9041, 12540, 11812, 10631,  9041,  7103,  4893,  2494,
275 	};
276 	/* Inverse map of [7, 5, 1, 3, 0, 2, 4, 6]. */
277 	static const char orders[DCTSIZE] = {4, 2, 5, 3, 6, 1, 7, 0};
278 	/* Reorder the columns after transposing. */
279 	for (i = 0; i < DCTSIZE2; ++i) {
280 	  int j = ((i & 7) << 3) + orders[i >> 3];
281 	  ifmtbl[j] = (qtbl->quantval[i] * scales[i] + 2) >> 2;
282 	}
283 #else /* ANDROID_ARMV6_IDCT */
284 
285 #define CONST_BITS 14
286 	static const INT16 aanscales[DCTSIZE2] = {
287 	  /* precomputed values scaled up by 14 bits */
288 	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
289 	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
290 	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
291 	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
292 	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
293 	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
294 	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
295 	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
296 	};
297 	SHIFT_TEMPS
298 
299 	for (i = 0; i < DCTSIZE2; i++) {
300 	  ifmtbl[i] = (IFAST_MULT_TYPE)
301 	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
302 				  (INT32) aanscales[i]),
303 		    CONST_BITS-IFAST_SCALE_BITS);
304 	}
305 #endif /* ANDROID_ARMV6_IDCT */
306       }
307       break;
308 #endif
309 #ifdef DCT_FLOAT_SUPPORTED
310     case JDCT_FLOAT:
311       {
312 	/* For float AA&N IDCT method, multipliers are equal to quantization
313 	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
314 	 *   scalefactor[0] = 1
315 	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
316 	 */
317 	FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
318 	int row, col;
319 	static const double aanscalefactor[DCTSIZE] = {
320 	  1.0, 1.387039845, 1.306562965, 1.175875602,
321 	  1.0, 0.785694958, 0.541196100, 0.275899379
322 	};
323 
324 	i = 0;
325 	for (row = 0; row < DCTSIZE; row++) {
326 	  for (col = 0; col < DCTSIZE; col++) {
327 	    fmtbl[i] = (FLOAT_MULT_TYPE)
328 	      ((double) qtbl->quantval[i] *
329 	       aanscalefactor[row] * aanscalefactor[col]);
330 	    i++;
331 	  }
332 	}
333       }
334       break;
335 #endif
336     default:
337       ERREXIT(cinfo, JERR_NOT_COMPILED);
338       break;
339     }
340   }
341 }
342 
343 
344 /*
345  * Initialize IDCT manager.
346  */
347 
348 GLOBAL(void)
jinit_inverse_dct(j_decompress_ptr cinfo)349 jinit_inverse_dct (j_decompress_ptr cinfo)
350 {
351   my_idct_ptr idct;
352   int ci;
353   jpeg_component_info *compptr;
354 
355   idct = (my_idct_ptr)
356     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
357 				SIZEOF(my_idct_controller));
358   cinfo->idct = (struct jpeg_inverse_dct *) idct;
359   idct->pub.start_pass = start_pass;
360 
361   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
362        ci++, compptr++) {
363     /* Allocate and pre-zero a multiplier table for each component */
364     compptr->dct_table =
365       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
366 				  SIZEOF(multiplier_table));
367     MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
368     /* Mark multiplier table not yet set up for any method */
369     idct->cur_method[ci] = -1;
370   }
371 }
372