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