Back to index

lightning-sunbird  0.9+nobinonly
jddctmgr.c
Go to the documentation of this file.
00001 /*
00002  * jddctmgr.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains the inverse-DCT management logic.
00009  * This code selects a particular IDCT implementation to be used,
00010  * and it performs related housekeeping chores.  No code in this file
00011  * is executed per IDCT step, only during output pass setup.
00012  *
00013  * Note that the IDCT routines are responsible for performing coefficient
00014  * dequantization as well as the IDCT proper.  This module sets up the
00015  * dequantization multiplier table needed by the IDCT routine.
00016  */
00017 
00018 #define JPEG_INTERNALS
00019 #include "jinclude.h"
00020 #include "jpeglib.h"
00021 #include "jdct.h"           /* Private declarations for DCT subsystem */
00022 extern int SSE2Available;
00023 
00024 /*
00025  * The decompressor input side (jdinput.c) saves away the appropriate
00026  * quantization table for each component at the start of the first scan
00027  * involving that component.  (This is necessary in order to correctly
00028  * decode files that reuse Q-table slots.)
00029  * When we are ready to make an output pass, the saved Q-table is converted
00030  * to a multiplier table that will actually be used by the IDCT routine.
00031  * The multiplier table contents are IDCT-method-dependent.  To support
00032  * application changes in IDCT method between scans, we can remake the
00033  * multiplier tables if necessary.
00034  * In buffered-image mode, the first output pass may occur before any data
00035  * has been seen for some components, and thus before their Q-tables have
00036  * been saved away.  To handle this case, multiplier tables are preset
00037  * to zeroes; the result of the IDCT will be a neutral gray level.
00038  */
00039 
00040 
00041 /* Private subobject for this module */
00042 
00043 typedef struct {
00044   struct jpeg_inverse_dct pub;     /* public fields */
00045 
00046   /* This array contains the IDCT method code that each multiplier table
00047    * is currently set up for, or -1 if it's not yet set up.
00048    * The actual multiplier tables are pointed to by dct_table in the
00049    * per-component comp_info structures.
00050    */
00051   int cur_method[MAX_COMPONENTS];
00052 } my_idct_controller;
00053 
00054 typedef my_idct_controller * my_idct_ptr;
00055 
00056 
00057 /* Allocated multiplier tables: big enough for any supported variant */
00058 
00059 typedef union {
00060   ISLOW_MULT_TYPE islow_array[DCTSIZE2];
00061 #ifdef DCT_IFAST_SUPPORTED
00062   IFAST_MULT_TYPE ifast_array[DCTSIZE2];
00063 #endif
00064 #ifdef DCT_FLOAT_SUPPORTED
00065   FLOAT_MULT_TYPE float_array[DCTSIZE2];
00066 #endif
00067 } multiplier_table;
00068 
00069 
00070 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
00071  * so be sure to compile that code if either ISLOW or SCALING is requested.
00072  */
00073 #ifdef DCT_ISLOW_SUPPORTED
00074 #define PROVIDE_ISLOW_TABLES
00075 #else
00076 #ifdef IDCT_SCALING_SUPPORTED
00077 #define PROVIDE_ISLOW_TABLES
00078 #endif
00079 #endif
00080 
00081 GLOBAL(void)
00082 jpeg_idct_islow_sse2 (
00083        j_decompress_ptr cinfo, 
00084        jpeg_component_info * compptr,
00085        JCOEFPTR coef_block,
00086        JSAMPARRAY output_buf, 
00087        JDIMENSION output_col);
00088 
00089 
00090 /*
00091  * Prepare for an output pass.
00092  * Here we select the proper IDCT routine for each component and build
00093  * a matching multiplier table.
00094  */
00095 
00096 METHODDEF(void)
00097 start_pass (j_decompress_ptr cinfo)
00098 {
00099   my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
00100   int ci, i;
00101   jpeg_component_info *compptr;
00102   int method = 0;
00103   inverse_DCT_method_ptr method_ptr = NULL;
00104   JQUANT_TBL * qtbl;
00105 
00106   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00107        ci++, compptr++) {
00108     /* Select the proper IDCT routine for this component's scaling */
00109     switch (compptr->DCT_scaled_size) {
00110 #ifdef IDCT_SCALING_SUPPORTED
00111     case 1:
00112       method_ptr = jpeg_idct_1x1;
00113       method = JDCT_ISLOW;  /* jidctred uses islow-style table */
00114       break;
00115     case 2:
00116       method_ptr = jpeg_idct_2x2;
00117       method = JDCT_ISLOW;  /* jidctred uses islow-style table */
00118       break;
00119     case 4:
00120       method_ptr = jpeg_idct_4x4;
00121       method = JDCT_ISLOW;  /* jidctred uses islow-style table */
00122       break;
00123 #endif
00124     case DCTSIZE:
00125       switch (cinfo->dct_method) {
00126 #ifdef DCT_ISLOW_SUPPORTED
00127       case JDCT_ISLOW:
00128 #ifdef HAVE_SSE2_INTEL_MNEMONICS
00129               if(SSE2Available == 1)
00130               {
00131                      method_ptr = jpeg_idct_islow_sse2;
00132                      method = JDCT_ISLOW;
00133               }
00134               else
00135               {
00136                      method_ptr = jpeg_idct_islow;
00137                      method = JDCT_ISLOW;
00138               }
00139 #else
00140               method_ptr = jpeg_idct_islow;
00141               method = JDCT_ISLOW;
00142                 
00143 #endif /* HAVE_SSE2_INTEL_MNEMONICS */
00144        break;
00145 #endif
00146 #ifdef DCT_IFAST_SUPPORTED
00147       case JDCT_IFAST:
00148 #ifdef HAVE_SSE2_INTEL_MNEMONICS
00149               if (SSE2Available==1) 
00150               {
00151                      method_ptr = jpeg_idct_islow_sse2;
00152                      method = JDCT_ISLOW;
00153               }
00154               else
00155               {
00156                      method_ptr = jpeg_idct_ifast;
00157                      method = JDCT_IFAST;
00158               }
00159 #else
00160               method_ptr = jpeg_idct_ifast;
00161               method = JDCT_IFAST;
00162 #endif /* HAVE_SSE2_INTEL_MNEMONICS */
00163        break;
00164 
00165 #endif
00166 #ifdef DCT_FLOAT_SUPPORTED
00167       case JDCT_FLOAT:
00168        method_ptr = jpeg_idct_float;
00169        method = JDCT_FLOAT;
00170        break;
00171 #endif
00172       default:
00173        ERREXIT(cinfo, JERR_NOT_COMPILED);
00174        break;
00175       }
00176       break;
00177     default:
00178       ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
00179       break;
00180     }
00181     idct->pub.inverse_DCT[ci] = method_ptr;
00182     /* Create multiplier table from quant table.
00183      * However, we can skip this if the component is uninteresting
00184      * or if we already built the table.  Also, if no quant table
00185      * has yet been saved for the component, we leave the
00186      * multiplier table all-zero; we'll be reading zeroes from the
00187      * coefficient controller's buffer anyway.
00188      */
00189     if (! compptr->component_needed || idct->cur_method[ci] == method)
00190       continue;
00191     qtbl = compptr->quant_table;
00192     if (qtbl == NULL)              /* happens if no data yet for component */
00193       continue;
00194     idct->cur_method[ci] = method;
00195     switch (method) {
00196 #ifdef PROVIDE_ISLOW_TABLES
00197     case JDCT_ISLOW:
00198       {
00199        /* For LL&M IDCT method, multipliers are equal to raw quantization
00200         * coefficients, but are stored as ints to ensure access efficiency.
00201         */
00202        ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
00203        for (i = 0; i < DCTSIZE2; i++) {
00204          ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
00205        }
00206       }
00207       break;
00208 #endif
00209 #ifdef DCT_IFAST_SUPPORTED
00210     case JDCT_IFAST:
00211       {
00212        /* For AA&N IDCT method, multipliers are equal to quantization
00213         * coefficients scaled by scalefactor[row]*scalefactor[col], where
00214         *   scalefactor[0] = 1
00215         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
00216         * For integer operation, the multiplier table is to be scaled by
00217         * IFAST_SCALE_BITS.
00218         */
00219        IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
00220 #define CONST_BITS 14
00221        static const INT16 aanscales[DCTSIZE2] = {
00222          /* precomputed values scaled up by 14 bits */
00223          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
00224          22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
00225          21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
00226          19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
00227          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
00228          12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
00229           8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
00230           4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
00231        };
00232        SHIFT_TEMPS
00233 
00234        for (i = 0; i < DCTSIZE2; i++) {
00235          ifmtbl[i] = (IFAST_MULT_TYPE)
00236            DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
00237                               (INT32) aanscales[i]),
00238                   CONST_BITS-IFAST_SCALE_BITS);
00239        }
00240       }
00241       break;
00242 #endif
00243 #ifdef DCT_FLOAT_SUPPORTED
00244     case JDCT_FLOAT:
00245       {
00246        /* For float AA&N IDCT method, multipliers are equal to quantization
00247         * coefficients scaled by scalefactor[row]*scalefactor[col], where
00248         *   scalefactor[0] = 1
00249         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
00250         */
00251        FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
00252        int row, col;
00253        static const double aanscalefactor[DCTSIZE] = {
00254          1.0, 1.387039845, 1.306562965, 1.175875602,
00255          1.0, 0.785694958, 0.541196100, 0.275899379
00256        };
00257 
00258        i = 0;
00259        for (row = 0; row < DCTSIZE; row++) {
00260          for (col = 0; col < DCTSIZE; col++) {
00261            fmtbl[i] = (FLOAT_MULT_TYPE)
00262              ((double) qtbl->quantval[i] *
00263               aanscalefactor[row] * aanscalefactor[col]);
00264            i++;
00265          }
00266        }
00267       }
00268       break;
00269 #endif
00270     default:
00271       ERREXIT(cinfo, JERR_NOT_COMPILED);
00272       break;
00273     }
00274   }
00275 }
00276 
00277 
00278 /*
00279  * Initialize IDCT manager.
00280  */
00281 
00282 GLOBAL(void)
00283 jinit_inverse_dct (j_decompress_ptr cinfo)
00284 {
00285   my_idct_ptr idct;
00286   int ci;
00287   jpeg_component_info *compptr;
00288 
00289   idct = (my_idct_ptr)
00290     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00291                             SIZEOF(my_idct_controller));
00292   cinfo->idct = (struct jpeg_inverse_dct *) idct;
00293   idct->pub.start_pass = start_pass;
00294 
00295   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00296        ci++, compptr++) {
00297     /* Allocate and pre-zero a multiplier table for each component */
00298     compptr->dct_table =
00299       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00300                               SIZEOF(multiplier_table));
00301     MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
00302     /* Mark multiplier table not yet set up for any method */
00303     idct->cur_method[ci] = -1;
00304   }
00305 }