vrshoot
diff libs/libjpeg/jidctfst.c @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/libjpeg/jidctfst.c Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,368 @@ 1.4 +/* 1.5 + * jidctfst.c 1.6 + * 1.7 + * Copyright (C) 1994-1998, Thomas G. Lane. 1.8 + * This file is part of the Independent JPEG Group's software. 1.9 + * For conditions of distribution and use, see the accompanying README file. 1.10 + * 1.11 + * This file contains a fast, not so accurate integer implementation of the 1.12 + * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine 1.13 + * must also perform dequantization of the input coefficients. 1.14 + * 1.15 + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT 1.16 + * on each row (or vice versa, but it's more convenient to emit a row at 1.17 + * a time). Direct algorithms are also available, but they are much more 1.18 + * complex and seem not to be any faster when reduced to code. 1.19 + * 1.20 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for 1.21 + * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in 1.22 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell 1.23 + * JPEG textbook (see REFERENCES section in file README). The following code 1.24 + * is based directly on figure 4-8 in P&M. 1.25 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is 1.26 + * possible to arrange the computation so that many of the multiplies are 1.27 + * simple scalings of the final outputs. These multiplies can then be 1.28 + * folded into the multiplications or divisions by the JPEG quantization 1.29 + * table entries. The AA&N method leaves only 5 multiplies and 29 adds 1.30 + * to be done in the DCT itself. 1.31 + * The primary disadvantage of this method is that with fixed-point math, 1.32 + * accuracy is lost due to imprecise representation of the scaled 1.33 + * quantization values. The smaller the quantization table entry, the less 1.34 + * precise the scaled value, so this implementation does worse with high- 1.35 + * quality-setting files than with low-quality ones. 1.36 + */ 1.37 + 1.38 +#define JPEG_INTERNALS 1.39 +#include "jinclude.h" 1.40 +#include "jpeglib.h" 1.41 +#include "jdct.h" /* Private declarations for DCT subsystem */ 1.42 + 1.43 +#ifdef DCT_IFAST_SUPPORTED 1.44 + 1.45 + 1.46 +/* 1.47 + * This module is specialized to the case DCTSIZE = 8. 1.48 + */ 1.49 + 1.50 +#if DCTSIZE != 8 1.51 + Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ 1.52 +#endif 1.53 + 1.54 + 1.55 +/* Scaling decisions are generally the same as in the LL&M algorithm; 1.56 + * see jidctint.c for more details. However, we choose to descale 1.57 + * (right shift) multiplication products as soon as they are formed, 1.58 + * rather than carrying additional fractional bits into subsequent additions. 1.59 + * This compromises accuracy slightly, but it lets us save a few shifts. 1.60 + * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples) 1.61 + * everywhere except in the multiplications proper; this saves a good deal 1.62 + * of work on 16-bit-int machines. 1.63 + * 1.64 + * The dequantized coefficients are not integers because the AA&N scaling 1.65 + * factors have been incorporated. We represent them scaled up by PASS1_BITS, 1.66 + * so that the first and second IDCT rounds have the same input scaling. 1.67 + * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to 1.68 + * avoid a descaling shift; this compromises accuracy rather drastically 1.69 + * for small quantization table entries, but it saves a lot of shifts. 1.70 + * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway, 1.71 + * so we use a much larger scaling factor to preserve accuracy. 1.72 + * 1.73 + * A final compromise is to represent the multiplicative constants to only 1.74 + * 8 fractional bits, rather than 13. This saves some shifting work on some 1.75 + * machines, and may also reduce the cost of multiplication (since there 1.76 + * are fewer one-bits in the constants). 1.77 + */ 1.78 + 1.79 +#if BITS_IN_JSAMPLE == 8 1.80 +#define CONST_BITS 8 1.81 +#define PASS1_BITS 2 1.82 +#else 1.83 +#define CONST_BITS 8 1.84 +#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ 1.85 +#endif 1.86 + 1.87 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus 1.88 + * causing a lot of useless floating-point operations at run time. 1.89 + * To get around this we use the following pre-calculated constants. 1.90 + * If you change CONST_BITS you may want to add appropriate values. 1.91 + * (With a reasonable C compiler, you can just rely on the FIX() macro...) 1.92 + */ 1.93 + 1.94 +#if CONST_BITS == 8 1.95 +#define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */ 1.96 +#define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */ 1.97 +#define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */ 1.98 +#define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */ 1.99 +#else 1.100 +#define FIX_1_082392200 FIX(1.082392200) 1.101 +#define FIX_1_414213562 FIX(1.414213562) 1.102 +#define FIX_1_847759065 FIX(1.847759065) 1.103 +#define FIX_2_613125930 FIX(2.613125930) 1.104 +#endif 1.105 + 1.106 + 1.107 +/* We can gain a little more speed, with a further compromise in accuracy, 1.108 + * by omitting the addition in a descaling shift. This yields an incorrectly 1.109 + * rounded result half the time... 1.110 + */ 1.111 + 1.112 +#ifndef USE_ACCURATE_ROUNDING 1.113 +#undef DESCALE 1.114 +#define DESCALE(x,n) RIGHT_SHIFT(x, n) 1.115 +#endif 1.116 + 1.117 + 1.118 +/* Multiply a DCTELEM variable by an INT32 constant, and immediately 1.119 + * descale to yield a DCTELEM result. 1.120 + */ 1.121 + 1.122 +#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS)) 1.123 + 1.124 + 1.125 +/* Dequantize a coefficient by multiplying it by the multiplier-table 1.126 + * entry; produce a DCTELEM result. For 8-bit data a 16x16->16 1.127 + * multiplication will do. For 12-bit data, the multiplier table is 1.128 + * declared INT32, so a 32-bit multiply will be used. 1.129 + */ 1.130 + 1.131 +#if BITS_IN_JSAMPLE == 8 1.132 +#define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval)) 1.133 +#else 1.134 +#define DEQUANTIZE(coef,quantval) \ 1.135 + DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS) 1.136 +#endif 1.137 + 1.138 + 1.139 +/* Like DESCALE, but applies to a DCTELEM and produces an int. 1.140 + * We assume that int right shift is unsigned if INT32 right shift is. 1.141 + */ 1.142 + 1.143 +#ifdef RIGHT_SHIFT_IS_UNSIGNED 1.144 +#define ISHIFT_TEMPS DCTELEM ishift_temp; 1.145 +#if BITS_IN_JSAMPLE == 8 1.146 +#define DCTELEMBITS 16 /* DCTELEM may be 16 or 32 bits */ 1.147 +#else 1.148 +#define DCTELEMBITS 32 /* DCTELEM must be 32 bits */ 1.149 +#endif 1.150 +#define IRIGHT_SHIFT(x,shft) \ 1.151 + ((ishift_temp = (x)) < 0 ? \ 1.152 + (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \ 1.153 + (ishift_temp >> (shft))) 1.154 +#else 1.155 +#define ISHIFT_TEMPS 1.156 +#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) 1.157 +#endif 1.158 + 1.159 +#ifdef USE_ACCURATE_ROUNDING 1.160 +#define IDESCALE(x,n) ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n)) 1.161 +#else 1.162 +#define IDESCALE(x,n) ((int) IRIGHT_SHIFT(x, n)) 1.163 +#endif 1.164 + 1.165 + 1.166 +/* 1.167 + * Perform dequantization and inverse DCT on one block of coefficients. 1.168 + */ 1.169 + 1.170 +GLOBAL(void) 1.171 +jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, 1.172 + JCOEFPTR coef_block, 1.173 + JSAMPARRAY output_buf, JDIMENSION output_col) 1.174 +{ 1.175 + DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; 1.176 + DCTELEM tmp10, tmp11, tmp12, tmp13; 1.177 + DCTELEM z5, z10, z11, z12, z13; 1.178 + JCOEFPTR inptr; 1.179 + IFAST_MULT_TYPE * quantptr; 1.180 + int * wsptr; 1.181 + JSAMPROW outptr; 1.182 + JSAMPLE *range_limit = IDCT_range_limit(cinfo); 1.183 + int ctr; 1.184 + int workspace[DCTSIZE2]; /* buffers data between passes */ 1.185 + SHIFT_TEMPS /* for DESCALE */ 1.186 + ISHIFT_TEMPS /* for IDESCALE */ 1.187 + 1.188 + /* Pass 1: process columns from input, store into work array. */ 1.189 + 1.190 + inptr = coef_block; 1.191 + quantptr = (IFAST_MULT_TYPE *) compptr->dct_table; 1.192 + wsptr = workspace; 1.193 + for (ctr = DCTSIZE; ctr > 0; ctr--) { 1.194 + /* Due to quantization, we will usually find that many of the input 1.195 + * coefficients are zero, especially the AC terms. We can exploit this 1.196 + * by short-circuiting the IDCT calculation for any column in which all 1.197 + * the AC terms are zero. In that case each output is equal to the 1.198 + * DC coefficient (with scale factor as needed). 1.199 + * With typical images and quantization tables, half or more of the 1.200 + * column DCT calculations can be simplified this way. 1.201 + */ 1.202 + 1.203 + if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && 1.204 + inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && 1.205 + inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && 1.206 + inptr[DCTSIZE*7] == 0) { 1.207 + /* AC terms all zero */ 1.208 + int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); 1.209 + 1.210 + wsptr[DCTSIZE*0] = dcval; 1.211 + wsptr[DCTSIZE*1] = dcval; 1.212 + wsptr[DCTSIZE*2] = dcval; 1.213 + wsptr[DCTSIZE*3] = dcval; 1.214 + wsptr[DCTSIZE*4] = dcval; 1.215 + wsptr[DCTSIZE*5] = dcval; 1.216 + wsptr[DCTSIZE*6] = dcval; 1.217 + wsptr[DCTSIZE*7] = dcval; 1.218 + 1.219 + inptr++; /* advance pointers to next column */ 1.220 + quantptr++; 1.221 + wsptr++; 1.222 + continue; 1.223 + } 1.224 + 1.225 + /* Even part */ 1.226 + 1.227 + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); 1.228 + tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); 1.229 + tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); 1.230 + tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); 1.231 + 1.232 + tmp10 = tmp0 + tmp2; /* phase 3 */ 1.233 + tmp11 = tmp0 - tmp2; 1.234 + 1.235 + tmp13 = tmp1 + tmp3; /* phases 5-3 */ 1.236 + tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */ 1.237 + 1.238 + tmp0 = tmp10 + tmp13; /* phase 2 */ 1.239 + tmp3 = tmp10 - tmp13; 1.240 + tmp1 = tmp11 + tmp12; 1.241 + tmp2 = tmp11 - tmp12; 1.242 + 1.243 + /* Odd part */ 1.244 + 1.245 + tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); 1.246 + tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); 1.247 + tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); 1.248 + tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); 1.249 + 1.250 + z13 = tmp6 + tmp5; /* phase 6 */ 1.251 + z10 = tmp6 - tmp5; 1.252 + z11 = tmp4 + tmp7; 1.253 + z12 = tmp4 - tmp7; 1.254 + 1.255 + tmp7 = z11 + z13; /* phase 5 */ 1.256 + tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ 1.257 + 1.258 + z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ 1.259 + tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ 1.260 + tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */ 1.261 + 1.262 + tmp6 = tmp12 - tmp7; /* phase 2 */ 1.263 + tmp5 = tmp11 - tmp6; 1.264 + tmp4 = tmp10 + tmp5; 1.265 + 1.266 + wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7); 1.267 + wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7); 1.268 + wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6); 1.269 + wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6); 1.270 + wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5); 1.271 + wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5); 1.272 + wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4); 1.273 + wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4); 1.274 + 1.275 + inptr++; /* advance pointers to next column */ 1.276 + quantptr++; 1.277 + wsptr++; 1.278 + } 1.279 + 1.280 + /* Pass 2: process rows from work array, store into output array. */ 1.281 + /* Note that we must descale the results by a factor of 8 == 2**3, */ 1.282 + /* and also undo the PASS1_BITS scaling. */ 1.283 + 1.284 + wsptr = workspace; 1.285 + for (ctr = 0; ctr < DCTSIZE; ctr++) { 1.286 + outptr = output_buf[ctr] + output_col; 1.287 + /* Rows of zeroes can be exploited in the same way as we did with columns. 1.288 + * However, the column calculation has created many nonzero AC terms, so 1.289 + * the simplification applies less often (typically 5% to 10% of the time). 1.290 + * On machines with very fast multiplication, it's possible that the 1.291 + * test takes more time than it's worth. In that case this section 1.292 + * may be commented out. 1.293 + */ 1.294 + 1.295 +#ifndef NO_ZERO_ROW_TEST 1.296 + if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 && 1.297 + wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { 1.298 + /* AC terms all zero */ 1.299 + JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3) 1.300 + & RANGE_MASK]; 1.301 + 1.302 + outptr[0] = dcval; 1.303 + outptr[1] = dcval; 1.304 + outptr[2] = dcval; 1.305 + outptr[3] = dcval; 1.306 + outptr[4] = dcval; 1.307 + outptr[5] = dcval; 1.308 + outptr[6] = dcval; 1.309 + outptr[7] = dcval; 1.310 + 1.311 + wsptr += DCTSIZE; /* advance pointer to next row */ 1.312 + continue; 1.313 + } 1.314 +#endif 1.315 + 1.316 + /* Even part */ 1.317 + 1.318 + tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]); 1.319 + tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]); 1.320 + 1.321 + tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]); 1.322 + tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562) 1.323 + - tmp13; 1.324 + 1.325 + tmp0 = tmp10 + tmp13; 1.326 + tmp3 = tmp10 - tmp13; 1.327 + tmp1 = tmp11 + tmp12; 1.328 + tmp2 = tmp11 - tmp12; 1.329 + 1.330 + /* Odd part */ 1.331 + 1.332 + z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3]; 1.333 + z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3]; 1.334 + z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7]; 1.335 + z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7]; 1.336 + 1.337 + tmp7 = z11 + z13; /* phase 5 */ 1.338 + tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ 1.339 + 1.340 + z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ 1.341 + tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ 1.342 + tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */ 1.343 + 1.344 + tmp6 = tmp12 - tmp7; /* phase 2 */ 1.345 + tmp5 = tmp11 - tmp6; 1.346 + tmp4 = tmp10 + tmp5; 1.347 + 1.348 + /* Final output stage: scale down by a factor of 8 and range-limit */ 1.349 + 1.350 + outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3) 1.351 + & RANGE_MASK]; 1.352 + outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3) 1.353 + & RANGE_MASK]; 1.354 + outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3) 1.355 + & RANGE_MASK]; 1.356 + outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3) 1.357 + & RANGE_MASK]; 1.358 + outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3) 1.359 + & RANGE_MASK]; 1.360 + outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3) 1.361 + & RANGE_MASK]; 1.362 + outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3) 1.363 + & RANGE_MASK]; 1.364 + outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3) 1.365 + & RANGE_MASK]; 1.366 + 1.367 + wsptr += DCTSIZE; /* advance pointer to next row */ 1.368 + } 1.369 +} 1.370 + 1.371 +#endif /* DCT_IFAST_SUPPORTED */