jddctmgr.c

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

Generated on Tue Jul 5 14:13:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1