jidctfst.c
Go to the documentation of this file.
00001 /*
00002  * jidctfst.c
00003  *
00004  * Copyright (C) 1994-1998, 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 a fast, not so accurate integer implementation of the
00009  * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
00010  * must also perform dequantization of the input coefficients.
00011  *
00012  * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
00013  * on each row (or vice versa, but it's more convenient to emit a row at
00014  * a time).  Direct algorithms are also available, but they are much more
00015  * complex and seem not to be any faster when reduced to code.
00016  *
00017  * This implementation is based on Arai, Agui, and Nakajima's algorithm for
00018  * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
00019  * Japanese, but the algorithm is described in the Pennebaker & Mitchell
00020  * JPEG textbook (see REFERENCES section in file README).  The following code
00021  * is based directly on figure 4-8 in P&M.
00022  * While an 8-point DCT cannot be done in less than 11 multiplies, it is
00023  * possible to arrange the computation so that many of the multiplies are
00024  * simple scalings of the final outputs.  These multiplies can then be
00025  * folded into the multiplications or divisions by the JPEG quantization
00026  * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
00027  * to be done in the DCT itself.
00028  * The primary disadvantage of this method is that with fixed-point math,
00029  * accuracy is lost due to imprecise representation of the scaled
00030  * quantization values.  The smaller the quantization table entry, the less
00031  * precise the scaled value, so this implementation does worse with high-
00032  * quality-setting files than with low-quality ones.
00033  */
00034 
00035 #define JPEG_INTERNALS
00036 #include "jinclude.h"
00037 #include "jpeglib.h"
00038 #include "jdct.h"               /* Private declarations for DCT subsystem */
00039 
00040 #ifdef DCT_IFAST_SUPPORTED
00041 
00042 
00043 /*
00044  * This module is specialized to the case DCTSIZE = 8.
00045  */
00046 
00047 #if DCTSIZE != 8
00048   Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
00049 #endif
00050 
00051 
00052 /* Scaling decisions are generally the same as in the LL&M algorithm;
00053  * see jidctint.c for more details.  However, we choose to descale
00054  * (right shift) multiplication products as soon as they are formed,
00055  * rather than carrying additional fractional bits into subsequent additions.
00056  * This compromises accuracy slightly, but it lets us save a few shifts.
00057  * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
00058  * everywhere except in the multiplications proper; this saves a good deal
00059  * of work on 16-bit-int machines.
00060  *
00061  * The dequantized coefficients are not integers because the AA&N scaling
00062  * factors have been incorporated.  We represent them scaled up by PASS1_BITS,
00063  * so that the first and second IDCT rounds have the same input scaling.
00064  * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
00065  * avoid a descaling shift; this compromises accuracy rather drastically
00066  * for small quantization table entries, but it saves a lot of shifts.
00067  * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
00068  * so we use a much larger scaling factor to preserve accuracy.
00069  *
00070  * A final compromise is to represent the multiplicative constants to only
00071  * 8 fractional bits, rather than 13.  This saves some shifting work on some
00072  * machines, and may also reduce the cost of multiplication (since there
00073  * are fewer one-bits in the constants).
00074  */
00075 
00076 #if BITS_IN_JSAMPLE == 8
00077 #define CONST_BITS  8
00078 #define PASS1_BITS  2
00079 #else
00080 #define CONST_BITS  8
00081 #define PASS1_BITS  1           /* lose a little precision to avoid overflow */
00082 #endif
00083 
00084 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
00085  * causing a lot of useless floating-point operations at run time.
00086  * To get around this we use the following pre-calculated constants.
00087  * If you change CONST_BITS you may want to add appropriate values.
00088  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
00089  */
00090 
00091 #if CONST_BITS == 8
00092 #define FIX_1_082392200  ((INT32)  277)         /* FIX(1.082392200) */
00093 #define FIX_1_414213562  ((INT32)  362)         /* FIX(1.414213562) */
00094 #define FIX_1_847759065  ((INT32)  473)         /* FIX(1.847759065) */
00095 #define FIX_2_613125930  ((INT32)  669)         /* FIX(2.613125930) */
00096 #else
00097 #define FIX_1_082392200  FIX(1.082392200)
00098 #define FIX_1_414213562  FIX(1.414213562)
00099 #define FIX_1_847759065  FIX(1.847759065)
00100 #define FIX_2_613125930  FIX(2.613125930)
00101 #endif
00102 
00103 
00104 /* We can gain a little more speed, with a further compromise in accuracy,
00105  * by omitting the addition in a descaling shift.  This yields an incorrectly
00106  * rounded result half the time...
00107  */
00108 
00109 #ifndef USE_ACCURATE_ROUNDING
00110 #undef DESCALE
00111 #define DESCALE(x,n)  RIGHT_SHIFT(x, n)
00112 #endif
00113 
00114 
00115 /* Multiply a DCTELEM variable by an INT32 constant, and immediately
00116  * descale to yield a DCTELEM result.
00117  */
00118 
00119 #define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
00120 
00121 
00122 /* Dequantize a coefficient by multiplying it by the multiplier-table
00123  * entry; produce a DCTELEM result.  For 8-bit data a 16x16->16
00124  * multiplication will do.  For 12-bit data, the multiplier table is
00125  * declared INT32, so a 32-bit multiply will be used.
00126  */
00127 
00128 #if BITS_IN_JSAMPLE == 8
00129 #define DEQUANTIZE(coef,quantval)  (((IFAST_MULT_TYPE) (coef)) * (quantval))
00130 #else
00131 #define DEQUANTIZE(coef,quantval)  \
00132         DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
00133 #endif
00134 
00135 
00136 /* Like DESCALE, but applies to a DCTELEM and produces an int.
00137  * We assume that int right shift is unsigned if INT32 right shift is.
00138  */
00139 
00140 #ifdef RIGHT_SHIFT_IS_UNSIGNED
00141 #define ISHIFT_TEMPS    DCTELEM ishift_temp;
00142 #if BITS_IN_JSAMPLE == 8
00143 #define DCTELEMBITS  16         /* DCTELEM may be 16 or 32 bits */
00144 #else
00145 #define DCTELEMBITS  32         /* DCTELEM must be 32 bits */
00146 #endif
00147 #define IRIGHT_SHIFT(x,shft)  \
00148     ((ishift_temp = (x)) < 0 ? \
00149      (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
00150      (ishift_temp >> (shft)))
00151 #else
00152 #define ISHIFT_TEMPS
00153 #define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
00154 #endif
00155 
00156 #ifdef USE_ACCURATE_ROUNDING
00157 #define IDESCALE(x,n)  ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
00158 #else
00159 #define IDESCALE(x,n)  ((int) IRIGHT_SHIFT(x, n))
00160 #endif
00161 
00162 
00163 /*
00164  * Perform dequantization and inverse DCT on one block of coefficients.
00165  */
00166 
00167 GLOBAL(void)
00168 jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00169                  JCOEFPTR coef_block,
00170                  JSAMPARRAY output_buf, JDIMENSION output_col)
00171 {
00172   DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
00173   DCTELEM tmp10, tmp11, tmp12, tmp13;
00174   DCTELEM z5, z10, z11, z12, z13;
00175   JCOEFPTR inptr;
00176   IFAST_MULT_TYPE * quantptr;
00177   int * wsptr;
00178   JSAMPROW outptr;
00179   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
00180   int ctr;
00181   int workspace[DCTSIZE2];      /* buffers data between passes */
00182   SHIFT_TEMPS                   /* for DESCALE */
00183   ISHIFT_TEMPS                  /* for IDESCALE */
00184 
00185   /* Pass 1: process columns from input, store into work array. */
00186 
00187   inptr = coef_block;
00188   quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
00189   wsptr = workspace;
00190   for (ctr = DCTSIZE; ctr > 0; ctr--) {
00191     /* Due to quantization, we will usually find that many of the input
00192      * coefficients are zero, especially the AC terms.  We can exploit this
00193      * by short-circuiting the IDCT calculation for any column in which all
00194      * the AC terms are zero.  In that case each output is equal to the
00195      * DC coefficient (with scale factor as needed).
00196      * With typical images and quantization tables, half or more of the
00197      * column DCT calculations can be simplified this way.
00198      */
00199     
00200     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
00201         inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
00202         inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
00203         inptr[DCTSIZE*7] == 0) {
00204       /* AC terms all zero */
00205       int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
00206 
00207       wsptr[DCTSIZE*0] = dcval;
00208       wsptr[DCTSIZE*1] = dcval;
00209       wsptr[DCTSIZE*2] = dcval;
00210       wsptr[DCTSIZE*3] = dcval;
00211       wsptr[DCTSIZE*4] = dcval;
00212       wsptr[DCTSIZE*5] = dcval;
00213       wsptr[DCTSIZE*6] = dcval;
00214       wsptr[DCTSIZE*7] = dcval;
00215       
00216       inptr++;                  /* advance pointers to next column */
00217       quantptr++;
00218       wsptr++;
00219       continue;
00220     }
00221     
00222     /* Even part */
00223 
00224     tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
00225     tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
00226     tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
00227     tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
00228 
00229     tmp10 = tmp0 + tmp2;        /* phase 3 */
00230     tmp11 = tmp0 - tmp2;
00231 
00232     tmp13 = tmp1 + tmp3;        /* phases 5-3 */
00233     tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
00234 
00235     tmp0 = tmp10 + tmp13;       /* phase 2 */
00236     tmp3 = tmp10 - tmp13;
00237     tmp1 = tmp11 + tmp12;
00238     tmp2 = tmp11 - tmp12;
00239     
00240     /* Odd part */
00241 
00242     tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
00243     tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
00244     tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
00245     tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
00246 
00247     z13 = tmp6 + tmp5;          /* phase 6 */
00248     z10 = tmp6 - tmp5;
00249     z11 = tmp4 + tmp7;
00250     z12 = tmp4 - tmp7;
00251 
00252     tmp7 = z11 + z13;           /* phase 5 */
00253     tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
00254 
00255     z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
00256     tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
00257     tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
00258 
00259     tmp6 = tmp12 - tmp7;        /* phase 2 */
00260     tmp5 = tmp11 - tmp6;
00261     tmp4 = tmp10 + tmp5;
00262 
00263     wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
00264     wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
00265     wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
00266     wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
00267     wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
00268     wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
00269     wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);
00270     wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);
00271 
00272     inptr++;                    /* advance pointers to next column */
00273     quantptr++;
00274     wsptr++;
00275   }
00276   
00277   /* Pass 2: process rows from work array, store into output array. */
00278   /* Note that we must descale the results by a factor of 8 == 2**3, */
00279   /* and also undo the PASS1_BITS scaling. */
00280 
00281   wsptr = workspace;
00282   for (ctr = 0; ctr < DCTSIZE; ctr++) {
00283     outptr = output_buf[ctr] + output_col;
00284     /* Rows of zeroes can be exploited in the same way as we did with columns.
00285      * However, the column calculation has created many nonzero AC terms, so
00286      * the simplification applies less often (typically 5% to 10% of the time).
00287      * On machines with very fast multiplication, it's possible that the
00288      * test takes more time than it's worth.  In that case this section
00289      * may be commented out.
00290      */
00291     
00292 #ifndef NO_ZERO_ROW_TEST
00293     if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
00294         wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
00295       /* AC terms all zero */
00296       JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
00297                                   & RANGE_MASK];
00298       
00299       outptr[0] = dcval;
00300       outptr[1] = dcval;
00301       outptr[2] = dcval;
00302       outptr[3] = dcval;
00303       outptr[4] = dcval;
00304       outptr[5] = dcval;
00305       outptr[6] = dcval;
00306       outptr[7] = dcval;
00307 
00308       wsptr += DCTSIZE;         /* advance pointer to next row */
00309       continue;
00310     }
00311 #endif
00312     
00313     /* Even part */
00314 
00315     tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
00316     tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);
00317 
00318     tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
00319     tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
00320             - tmp13;
00321 
00322     tmp0 = tmp10 + tmp13;
00323     tmp3 = tmp10 - tmp13;
00324     tmp1 = tmp11 + tmp12;
00325     tmp2 = tmp11 - tmp12;
00326 
00327     /* Odd part */
00328 
00329     z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
00330     z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
00331     z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
00332     z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];
00333 
00334     tmp7 = z11 + z13;           /* phase 5 */
00335     tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
00336 
00337     z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
00338     tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
00339     tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
00340 
00341     tmp6 = tmp12 - tmp7;        /* phase 2 */
00342     tmp5 = tmp11 - tmp6;
00343     tmp4 = tmp10 + tmp5;
00344 
00345     /* Final output stage: scale down by a factor of 8 and range-limit */
00346 
00347     outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
00348                             & RANGE_MASK];
00349     outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
00350                             & RANGE_MASK];
00351     outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
00352                             & RANGE_MASK];
00353     outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
00354                             & RANGE_MASK];
00355     outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
00356                             & RANGE_MASK];
00357     outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
00358                             & RANGE_MASK];
00359     outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
00360                             & RANGE_MASK];
00361     outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
00362                             & RANGE_MASK];
00363 
00364     wsptr += DCTSIZE;           /* advance pointer to next row */
00365   }
00366 }
00367 
00368 #endif /* DCT_IFAST_SUPPORTED */


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:17