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