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