istereo

annotate libs/libjpeg/jfdctfst.c @ 26:862a3329a8f0

wohooo, added a shitload of code from zlib/libpng/libjpeg. When the good lord was raining shared libraries the iphone held a fucking umbrella...
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 06:28:38 +0300
parents
children
rev   line source
nuclear@26 1 /*
nuclear@26 2 * jfdctfst.c
nuclear@26 3 *
nuclear@26 4 * Copyright (C) 1994-1996, Thomas G. Lane.
nuclear@26 5 * This file is part of the Independent JPEG Group's software.
nuclear@26 6 * For conditions of distribution and use, see the accompanying README file.
nuclear@26 7 *
nuclear@26 8 * This file contains a fast, not so accurate integer implementation of the
nuclear@26 9 * forward DCT (Discrete Cosine Transform).
nuclear@26 10 *
nuclear@26 11 * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
nuclear@26 12 * on each column. Direct algorithms are also available, but they are
nuclear@26 13 * much more complex and seem not to be any faster when reduced to code.
nuclear@26 14 *
nuclear@26 15 * This implementation is based on Arai, Agui, and Nakajima's algorithm for
nuclear@26 16 * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
nuclear@26 17 * Japanese, but the algorithm is described in the Pennebaker & Mitchell
nuclear@26 18 * JPEG textbook (see REFERENCES section in file README). The following code
nuclear@26 19 * is based directly on figure 4-8 in P&M.
nuclear@26 20 * While an 8-point DCT cannot be done in less than 11 multiplies, it is
nuclear@26 21 * possible to arrange the computation so that many of the multiplies are
nuclear@26 22 * simple scalings of the final outputs. These multiplies can then be
nuclear@26 23 * folded into the multiplications or divisions by the JPEG quantization
nuclear@26 24 * table entries. The AA&N method leaves only 5 multiplies and 29 adds
nuclear@26 25 * to be done in the DCT itself.
nuclear@26 26 * The primary disadvantage of this method is that with fixed-point math,
nuclear@26 27 * accuracy is lost due to imprecise representation of the scaled
nuclear@26 28 * quantization values. The smaller the quantization table entry, the less
nuclear@26 29 * precise the scaled value, so this implementation does worse with high-
nuclear@26 30 * quality-setting files than with low-quality ones.
nuclear@26 31 */
nuclear@26 32
nuclear@26 33 #define JPEG_INTERNALS
nuclear@26 34 #include "jinclude.h"
nuclear@26 35 #include "jpeglib.h"
nuclear@26 36 #include "jdct.h" /* Private declarations for DCT subsystem */
nuclear@26 37
nuclear@26 38 #ifdef DCT_IFAST_SUPPORTED
nuclear@26 39
nuclear@26 40
nuclear@26 41 /*
nuclear@26 42 * This module is specialized to the case DCTSIZE = 8.
nuclear@26 43 */
nuclear@26 44
nuclear@26 45 #if DCTSIZE != 8
nuclear@26 46 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
nuclear@26 47 #endif
nuclear@26 48
nuclear@26 49
nuclear@26 50 /* Scaling decisions are generally the same as in the LL&M algorithm;
nuclear@26 51 * see jfdctint.c for more details. However, we choose to descale
nuclear@26 52 * (right shift) multiplication products as soon as they are formed,
nuclear@26 53 * rather than carrying additional fractional bits into subsequent additions.
nuclear@26 54 * This compromises accuracy slightly, but it lets us save a few shifts.
nuclear@26 55 * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
nuclear@26 56 * everywhere except in the multiplications proper; this saves a good deal
nuclear@26 57 * of work on 16-bit-int machines.
nuclear@26 58 *
nuclear@26 59 * Again to save a few shifts, the intermediate results between pass 1 and
nuclear@26 60 * pass 2 are not upscaled, but are represented only to integral precision.
nuclear@26 61 *
nuclear@26 62 * A final compromise is to represent the multiplicative constants to only
nuclear@26 63 * 8 fractional bits, rather than 13. This saves some shifting work on some
nuclear@26 64 * machines, and may also reduce the cost of multiplication (since there
nuclear@26 65 * are fewer one-bits in the constants).
nuclear@26 66 */
nuclear@26 67
nuclear@26 68 #define CONST_BITS 8
nuclear@26 69
nuclear@26 70
nuclear@26 71 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
nuclear@26 72 * causing a lot of useless floating-point operations at run time.
nuclear@26 73 * To get around this we use the following pre-calculated constants.
nuclear@26 74 * If you change CONST_BITS you may want to add appropriate values.
nuclear@26 75 * (With a reasonable C compiler, you can just rely on the FIX() macro...)
nuclear@26 76 */
nuclear@26 77
nuclear@26 78 #if CONST_BITS == 8
nuclear@26 79 #define FIX_0_382683433 ((INT32) 98) /* FIX(0.382683433) */
nuclear@26 80 #define FIX_0_541196100 ((INT32) 139) /* FIX(0.541196100) */
nuclear@26 81 #define FIX_0_707106781 ((INT32) 181) /* FIX(0.707106781) */
nuclear@26 82 #define FIX_1_306562965 ((INT32) 334) /* FIX(1.306562965) */
nuclear@26 83 #else
nuclear@26 84 #define FIX_0_382683433 FIX(0.382683433)
nuclear@26 85 #define FIX_0_541196100 FIX(0.541196100)
nuclear@26 86 #define FIX_0_707106781 FIX(0.707106781)
nuclear@26 87 #define FIX_1_306562965 FIX(1.306562965)
nuclear@26 88 #endif
nuclear@26 89
nuclear@26 90
nuclear@26 91 /* We can gain a little more speed, with a further compromise in accuracy,
nuclear@26 92 * by omitting the addition in a descaling shift. This yields an incorrectly
nuclear@26 93 * rounded result half the time...
nuclear@26 94 */
nuclear@26 95
nuclear@26 96 #ifndef USE_ACCURATE_ROUNDING
nuclear@26 97 #undef DESCALE
nuclear@26 98 #define DESCALE(x,n) RIGHT_SHIFT(x, n)
nuclear@26 99 #endif
nuclear@26 100
nuclear@26 101
nuclear@26 102 /* Multiply a DCTELEM variable by an INT32 constant, and immediately
nuclear@26 103 * descale to yield a DCTELEM result.
nuclear@26 104 */
nuclear@26 105
nuclear@26 106 #define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
nuclear@26 107
nuclear@26 108
nuclear@26 109 /*
nuclear@26 110 * Perform the forward DCT on one block of samples.
nuclear@26 111 */
nuclear@26 112
nuclear@26 113 GLOBAL(void)
nuclear@26 114 jpeg_fdct_ifast (DCTELEM * data)
nuclear@26 115 {
nuclear@26 116 DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
nuclear@26 117 DCTELEM tmp10, tmp11, tmp12, tmp13;
nuclear@26 118 DCTELEM z1, z2, z3, z4, z5, z11, z13;
nuclear@26 119 DCTELEM *dataptr;
nuclear@26 120 int ctr;
nuclear@26 121 SHIFT_TEMPS
nuclear@26 122
nuclear@26 123 /* Pass 1: process rows. */
nuclear@26 124
nuclear@26 125 dataptr = data;
nuclear@26 126 for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
nuclear@26 127 tmp0 = dataptr[0] + dataptr[7];
nuclear@26 128 tmp7 = dataptr[0] - dataptr[7];
nuclear@26 129 tmp1 = dataptr[1] + dataptr[6];
nuclear@26 130 tmp6 = dataptr[1] - dataptr[6];
nuclear@26 131 tmp2 = dataptr[2] + dataptr[5];
nuclear@26 132 tmp5 = dataptr[2] - dataptr[5];
nuclear@26 133 tmp3 = dataptr[3] + dataptr[4];
nuclear@26 134 tmp4 = dataptr[3] - dataptr[4];
nuclear@26 135
nuclear@26 136 /* Even part */
nuclear@26 137
nuclear@26 138 tmp10 = tmp0 + tmp3; /* phase 2 */
nuclear@26 139 tmp13 = tmp0 - tmp3;
nuclear@26 140 tmp11 = tmp1 + tmp2;
nuclear@26 141 tmp12 = tmp1 - tmp2;
nuclear@26 142
nuclear@26 143 dataptr[0] = tmp10 + tmp11; /* phase 3 */
nuclear@26 144 dataptr[4] = tmp10 - tmp11;
nuclear@26 145
nuclear@26 146 z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
nuclear@26 147 dataptr[2] = tmp13 + z1; /* phase 5 */
nuclear@26 148 dataptr[6] = tmp13 - z1;
nuclear@26 149
nuclear@26 150 /* Odd part */
nuclear@26 151
nuclear@26 152 tmp10 = tmp4 + tmp5; /* phase 2 */
nuclear@26 153 tmp11 = tmp5 + tmp6;
nuclear@26 154 tmp12 = tmp6 + tmp7;
nuclear@26 155
nuclear@26 156 /* The rotator is modified from fig 4-8 to avoid extra negations. */
nuclear@26 157 z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
nuclear@26 158 z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
nuclear@26 159 z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
nuclear@26 160 z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
nuclear@26 161
nuclear@26 162 z11 = tmp7 + z3; /* phase 5 */
nuclear@26 163 z13 = tmp7 - z3;
nuclear@26 164
nuclear@26 165 dataptr[5] = z13 + z2; /* phase 6 */
nuclear@26 166 dataptr[3] = z13 - z2;
nuclear@26 167 dataptr[1] = z11 + z4;
nuclear@26 168 dataptr[7] = z11 - z4;
nuclear@26 169
nuclear@26 170 dataptr += DCTSIZE; /* advance pointer to next row */
nuclear@26 171 }
nuclear@26 172
nuclear@26 173 /* Pass 2: process columns. */
nuclear@26 174
nuclear@26 175 dataptr = data;
nuclear@26 176 for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
nuclear@26 177 tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
nuclear@26 178 tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
nuclear@26 179 tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
nuclear@26 180 tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
nuclear@26 181 tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
nuclear@26 182 tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
nuclear@26 183 tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
nuclear@26 184 tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
nuclear@26 185
nuclear@26 186 /* Even part */
nuclear@26 187
nuclear@26 188 tmp10 = tmp0 + tmp3; /* phase 2 */
nuclear@26 189 tmp13 = tmp0 - tmp3;
nuclear@26 190 tmp11 = tmp1 + tmp2;
nuclear@26 191 tmp12 = tmp1 - tmp2;
nuclear@26 192
nuclear@26 193 dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
nuclear@26 194 dataptr[DCTSIZE*4] = tmp10 - tmp11;
nuclear@26 195
nuclear@26 196 z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
nuclear@26 197 dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
nuclear@26 198 dataptr[DCTSIZE*6] = tmp13 - z1;
nuclear@26 199
nuclear@26 200 /* Odd part */
nuclear@26 201
nuclear@26 202 tmp10 = tmp4 + tmp5; /* phase 2 */
nuclear@26 203 tmp11 = tmp5 + tmp6;
nuclear@26 204 tmp12 = tmp6 + tmp7;
nuclear@26 205
nuclear@26 206 /* The rotator is modified from fig 4-8 to avoid extra negations. */
nuclear@26 207 z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
nuclear@26 208 z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
nuclear@26 209 z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
nuclear@26 210 z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
nuclear@26 211
nuclear@26 212 z11 = tmp7 + z3; /* phase 5 */
nuclear@26 213 z13 = tmp7 - z3;
nuclear@26 214
nuclear@26 215 dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
nuclear@26 216 dataptr[DCTSIZE*3] = z13 - z2;
nuclear@26 217 dataptr[DCTSIZE*1] = z11 + z4;
nuclear@26 218 dataptr[DCTSIZE*7] = z11 - z4;
nuclear@26 219
nuclear@26 220 dataptr++; /* advance pointer to next column */
nuclear@26 221 }
nuclear@26 222 }
nuclear@26 223
nuclear@26 224 #endif /* DCT_IFAST_SUPPORTED */