istereo

annotate libs/libjpeg/jcdctmgr.c @ 30:8dd271942543

fixed everything
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 14:52:13 +0300
parents
children
rev   line source
nuclear@26 1 /*
nuclear@26 2 * jcdctmgr.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 the forward-DCT management logic.
nuclear@26 9 * This code selects a particular DCT implementation to be used,
nuclear@26 10 * and it performs related housekeeping chores including coefficient
nuclear@26 11 * quantization.
nuclear@26 12 */
nuclear@26 13
nuclear@26 14 #define JPEG_INTERNALS
nuclear@26 15 #include "jinclude.h"
nuclear@26 16 #include "jpeglib.h"
nuclear@26 17 #include "jdct.h" /* Private declarations for DCT subsystem */
nuclear@26 18
nuclear@26 19
nuclear@26 20 /* Private subobject for this module */
nuclear@26 21
nuclear@26 22 typedef struct {
nuclear@26 23 struct jpeg_forward_dct pub; /* public fields */
nuclear@26 24
nuclear@26 25 /* Pointer to the DCT routine actually in use */
nuclear@26 26 forward_DCT_method_ptr do_dct;
nuclear@26 27
nuclear@26 28 /* The actual post-DCT divisors --- not identical to the quant table
nuclear@26 29 * entries, because of scaling (especially for an unnormalized DCT).
nuclear@26 30 * Each table is given in normal array order.
nuclear@26 31 */
nuclear@26 32 DCTELEM * divisors[NUM_QUANT_TBLS];
nuclear@26 33
nuclear@26 34 #ifdef DCT_FLOAT_SUPPORTED
nuclear@26 35 /* Same as above for the floating-point case. */
nuclear@26 36 float_DCT_method_ptr do_float_dct;
nuclear@26 37 FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
nuclear@26 38 #endif
nuclear@26 39 } my_fdct_controller;
nuclear@26 40
nuclear@26 41 typedef my_fdct_controller * my_fdct_ptr;
nuclear@26 42
nuclear@26 43
nuclear@26 44 /*
nuclear@26 45 * Initialize for a processing pass.
nuclear@26 46 * Verify that all referenced Q-tables are present, and set up
nuclear@26 47 * the divisor table for each one.
nuclear@26 48 * In the current implementation, DCT of all components is done during
nuclear@26 49 * the first pass, even if only some components will be output in the
nuclear@26 50 * first scan. Hence all components should be examined here.
nuclear@26 51 */
nuclear@26 52
nuclear@26 53 METHODDEF(void)
nuclear@26 54 start_pass_fdctmgr (j_compress_ptr cinfo)
nuclear@26 55 {
nuclear@26 56 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
nuclear@26 57 int ci, qtblno, i;
nuclear@26 58 jpeg_component_info *compptr;
nuclear@26 59 JQUANT_TBL * qtbl;
nuclear@26 60 DCTELEM * dtbl;
nuclear@26 61
nuclear@26 62 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
nuclear@26 63 ci++, compptr++) {
nuclear@26 64 qtblno = compptr->quant_tbl_no;
nuclear@26 65 /* Make sure specified quantization table is present */
nuclear@26 66 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
nuclear@26 67 cinfo->quant_tbl_ptrs[qtblno] == NULL)
nuclear@26 68 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
nuclear@26 69 qtbl = cinfo->quant_tbl_ptrs[qtblno];
nuclear@26 70 /* Compute divisors for this quant table */
nuclear@26 71 /* We may do this more than once for same table, but it's not a big deal */
nuclear@26 72 switch (cinfo->dct_method) {
nuclear@26 73 #ifdef DCT_ISLOW_SUPPORTED
nuclear@26 74 case JDCT_ISLOW:
nuclear@26 75 /* For LL&M IDCT method, divisors are equal to raw quantization
nuclear@26 76 * coefficients multiplied by 8 (to counteract scaling).
nuclear@26 77 */
nuclear@26 78 if (fdct->divisors[qtblno] == NULL) {
nuclear@26 79 fdct->divisors[qtblno] = (DCTELEM *)
nuclear@26 80 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@26 81 DCTSIZE2 * SIZEOF(DCTELEM));
nuclear@26 82 }
nuclear@26 83 dtbl = fdct->divisors[qtblno];
nuclear@26 84 for (i = 0; i < DCTSIZE2; i++) {
nuclear@26 85 dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
nuclear@26 86 }
nuclear@26 87 break;
nuclear@26 88 #endif
nuclear@26 89 #ifdef DCT_IFAST_SUPPORTED
nuclear@26 90 case JDCT_IFAST:
nuclear@26 91 {
nuclear@26 92 /* For AA&N IDCT method, divisors are equal to quantization
nuclear@26 93 * coefficients scaled by scalefactor[row]*scalefactor[col], where
nuclear@26 94 * scalefactor[0] = 1
nuclear@26 95 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
nuclear@26 96 * We apply a further scale factor of 8.
nuclear@26 97 */
nuclear@26 98 #define CONST_BITS 14
nuclear@26 99 static const INT16 aanscales[DCTSIZE2] = {
nuclear@26 100 /* precomputed values scaled up by 14 bits */
nuclear@26 101 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
nuclear@26 102 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
nuclear@26 103 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
nuclear@26 104 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
nuclear@26 105 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
nuclear@26 106 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
nuclear@26 107 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
nuclear@26 108 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
nuclear@26 109 };
nuclear@26 110 SHIFT_TEMPS
nuclear@26 111
nuclear@26 112 if (fdct->divisors[qtblno] == NULL) {
nuclear@26 113 fdct->divisors[qtblno] = (DCTELEM *)
nuclear@26 114 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@26 115 DCTSIZE2 * SIZEOF(DCTELEM));
nuclear@26 116 }
nuclear@26 117 dtbl = fdct->divisors[qtblno];
nuclear@26 118 for (i = 0; i < DCTSIZE2; i++) {
nuclear@26 119 dtbl[i] = (DCTELEM)
nuclear@26 120 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
nuclear@26 121 (INT32) aanscales[i]),
nuclear@26 122 CONST_BITS-3);
nuclear@26 123 }
nuclear@26 124 }
nuclear@26 125 break;
nuclear@26 126 #endif
nuclear@26 127 #ifdef DCT_FLOAT_SUPPORTED
nuclear@26 128 case JDCT_FLOAT:
nuclear@26 129 {
nuclear@26 130 /* For float AA&N IDCT method, divisors are equal to quantization
nuclear@26 131 * coefficients scaled by scalefactor[row]*scalefactor[col], where
nuclear@26 132 * scalefactor[0] = 1
nuclear@26 133 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
nuclear@26 134 * We apply a further scale factor of 8.
nuclear@26 135 * What's actually stored is 1/divisor so that the inner loop can
nuclear@26 136 * use a multiplication rather than a division.
nuclear@26 137 */
nuclear@26 138 FAST_FLOAT * fdtbl;
nuclear@26 139 int row, col;
nuclear@26 140 static const double aanscalefactor[DCTSIZE] = {
nuclear@26 141 1.0, 1.387039845, 1.306562965, 1.175875602,
nuclear@26 142 1.0, 0.785694958, 0.541196100, 0.275899379
nuclear@26 143 };
nuclear@26 144
nuclear@26 145 if (fdct->float_divisors[qtblno] == NULL) {
nuclear@26 146 fdct->float_divisors[qtblno] = (FAST_FLOAT *)
nuclear@26 147 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@26 148 DCTSIZE2 * SIZEOF(FAST_FLOAT));
nuclear@26 149 }
nuclear@26 150 fdtbl = fdct->float_divisors[qtblno];
nuclear@26 151 i = 0;
nuclear@26 152 for (row = 0; row < DCTSIZE; row++) {
nuclear@26 153 for (col = 0; col < DCTSIZE; col++) {
nuclear@26 154 fdtbl[i] = (FAST_FLOAT)
nuclear@26 155 (1.0 / (((double) qtbl->quantval[i] *
nuclear@26 156 aanscalefactor[row] * aanscalefactor[col] * 8.0)));
nuclear@26 157 i++;
nuclear@26 158 }
nuclear@26 159 }
nuclear@26 160 }
nuclear@26 161 break;
nuclear@26 162 #endif
nuclear@26 163 default:
nuclear@26 164 ERREXIT(cinfo, JERR_NOT_COMPILED);
nuclear@26 165 break;
nuclear@26 166 }
nuclear@26 167 }
nuclear@26 168 }
nuclear@26 169
nuclear@26 170
nuclear@26 171 /*
nuclear@26 172 * Perform forward DCT on one or more blocks of a component.
nuclear@26 173 *
nuclear@26 174 * The input samples are taken from the sample_data[] array starting at
nuclear@26 175 * position start_row/start_col, and moving to the right for any additional
nuclear@26 176 * blocks. The quantized coefficients are returned in coef_blocks[].
nuclear@26 177 */
nuclear@26 178
nuclear@26 179 METHODDEF(void)
nuclear@26 180 forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
nuclear@26 181 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
nuclear@26 182 JDIMENSION start_row, JDIMENSION start_col,
nuclear@26 183 JDIMENSION num_blocks)
nuclear@26 184 /* This version is used for integer DCT implementations. */
nuclear@26 185 {
nuclear@26 186 /* This routine is heavily used, so it's worth coding it tightly. */
nuclear@26 187 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
nuclear@26 188 forward_DCT_method_ptr do_dct = fdct->do_dct;
nuclear@26 189 DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
nuclear@26 190 DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
nuclear@26 191 JDIMENSION bi;
nuclear@26 192
nuclear@26 193 sample_data += start_row; /* fold in the vertical offset once */
nuclear@26 194
nuclear@26 195 for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
nuclear@26 196 /* Load data into workspace, applying unsigned->signed conversion */
nuclear@26 197 { register DCTELEM *workspaceptr;
nuclear@26 198 register JSAMPROW elemptr;
nuclear@26 199 register int elemr;
nuclear@26 200
nuclear@26 201 workspaceptr = workspace;
nuclear@26 202 for (elemr = 0; elemr < DCTSIZE; elemr++) {
nuclear@26 203 elemptr = sample_data[elemr] + start_col;
nuclear@26 204 #if DCTSIZE == 8 /* unroll the inner loop */
nuclear@26 205 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@26 206 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@26 207 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@26 208 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@26 209 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@26 210 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@26 211 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@26 212 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@26 213 #else
nuclear@26 214 { register int elemc;
nuclear@26 215 for (elemc = DCTSIZE; elemc > 0; elemc--) {
nuclear@26 216 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@26 217 }
nuclear@26 218 }
nuclear@26 219 #endif
nuclear@26 220 }
nuclear@26 221 }
nuclear@26 222
nuclear@26 223 /* Perform the DCT */
nuclear@26 224 (*do_dct) (workspace);
nuclear@26 225
nuclear@26 226 /* Quantize/descale the coefficients, and store into coef_blocks[] */
nuclear@26 227 { register DCTELEM temp, qval;
nuclear@26 228 register int i;
nuclear@26 229 register JCOEFPTR output_ptr = coef_blocks[bi];
nuclear@26 230
nuclear@26 231 for (i = 0; i < DCTSIZE2; i++) {
nuclear@26 232 qval = divisors[i];
nuclear@26 233 temp = workspace[i];
nuclear@26 234 /* Divide the coefficient value by qval, ensuring proper rounding.
nuclear@26 235 * Since C does not specify the direction of rounding for negative
nuclear@26 236 * quotients, we have to force the dividend positive for portability.
nuclear@26 237 *
nuclear@26 238 * In most files, at least half of the output values will be zero
nuclear@26 239 * (at default quantization settings, more like three-quarters...)
nuclear@26 240 * so we should ensure that this case is fast. On many machines,
nuclear@26 241 * a comparison is enough cheaper than a divide to make a special test
nuclear@26 242 * a win. Since both inputs will be nonnegative, we need only test
nuclear@26 243 * for a < b to discover whether a/b is 0.
nuclear@26 244 * If your machine's division is fast enough, define FAST_DIVIDE.
nuclear@26 245 */
nuclear@26 246 #ifdef FAST_DIVIDE
nuclear@26 247 #define DIVIDE_BY(a,b) a /= b
nuclear@26 248 #else
nuclear@26 249 #define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
nuclear@26 250 #endif
nuclear@26 251 if (temp < 0) {
nuclear@26 252 temp = -temp;
nuclear@26 253 temp += qval>>1; /* for rounding */
nuclear@26 254 DIVIDE_BY(temp, qval);
nuclear@26 255 temp = -temp;
nuclear@26 256 } else {
nuclear@26 257 temp += qval>>1; /* for rounding */
nuclear@26 258 DIVIDE_BY(temp, qval);
nuclear@26 259 }
nuclear@26 260 output_ptr[i] = (JCOEF) temp;
nuclear@26 261 }
nuclear@26 262 }
nuclear@26 263 }
nuclear@26 264 }
nuclear@26 265
nuclear@26 266
nuclear@26 267 #ifdef DCT_FLOAT_SUPPORTED
nuclear@26 268
nuclear@26 269 METHODDEF(void)
nuclear@26 270 forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
nuclear@26 271 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
nuclear@26 272 JDIMENSION start_row, JDIMENSION start_col,
nuclear@26 273 JDIMENSION num_blocks)
nuclear@26 274 /* This version is used for floating-point DCT implementations. */
nuclear@26 275 {
nuclear@26 276 /* This routine is heavily used, so it's worth coding it tightly. */
nuclear@26 277 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
nuclear@26 278 float_DCT_method_ptr do_dct = fdct->do_float_dct;
nuclear@26 279 FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
nuclear@26 280 FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
nuclear@26 281 JDIMENSION bi;
nuclear@26 282
nuclear@26 283 sample_data += start_row; /* fold in the vertical offset once */
nuclear@26 284
nuclear@26 285 for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
nuclear@26 286 /* Load data into workspace, applying unsigned->signed conversion */
nuclear@26 287 { register FAST_FLOAT *workspaceptr;
nuclear@26 288 register JSAMPROW elemptr;
nuclear@26 289 register int elemr;
nuclear@26 290
nuclear@26 291 workspaceptr = workspace;
nuclear@26 292 for (elemr = 0; elemr < DCTSIZE; elemr++) {
nuclear@26 293 elemptr = sample_data[elemr] + start_col;
nuclear@26 294 #if DCTSIZE == 8 /* unroll the inner loop */
nuclear@26 295 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@26 296 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@26 297 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@26 298 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@26 299 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@26 300 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@26 301 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@26 302 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@26 303 #else
nuclear@26 304 { register int elemc;
nuclear@26 305 for (elemc = DCTSIZE; elemc > 0; elemc--) {
nuclear@26 306 *workspaceptr++ = (FAST_FLOAT)
nuclear@26 307 (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@26 308 }
nuclear@26 309 }
nuclear@26 310 #endif
nuclear@26 311 }
nuclear@26 312 }
nuclear@26 313
nuclear@26 314 /* Perform the DCT */
nuclear@26 315 (*do_dct) (workspace);
nuclear@26 316
nuclear@26 317 /* Quantize/descale the coefficients, and store into coef_blocks[] */
nuclear@26 318 { register FAST_FLOAT temp;
nuclear@26 319 register int i;
nuclear@26 320 register JCOEFPTR output_ptr = coef_blocks[bi];
nuclear@26 321
nuclear@26 322 for (i = 0; i < DCTSIZE2; i++) {
nuclear@26 323 /* Apply the quantization and scaling factor */
nuclear@26 324 temp = workspace[i] * divisors[i];
nuclear@26 325 /* Round to nearest integer.
nuclear@26 326 * Since C does not specify the direction of rounding for negative
nuclear@26 327 * quotients, we have to force the dividend positive for portability.
nuclear@26 328 * The maximum coefficient size is +-16K (for 12-bit data), so this
nuclear@26 329 * code should work for either 16-bit or 32-bit ints.
nuclear@26 330 */
nuclear@26 331 output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
nuclear@26 332 }
nuclear@26 333 }
nuclear@26 334 }
nuclear@26 335 }
nuclear@26 336
nuclear@26 337 #endif /* DCT_FLOAT_SUPPORTED */
nuclear@26 338
nuclear@26 339
nuclear@26 340 /*
nuclear@26 341 * Initialize FDCT manager.
nuclear@26 342 */
nuclear@26 343
nuclear@26 344 GLOBAL(void)
nuclear@26 345 jinit_forward_dct (j_compress_ptr cinfo)
nuclear@26 346 {
nuclear@26 347 my_fdct_ptr fdct;
nuclear@26 348 int i;
nuclear@26 349
nuclear@26 350 fdct = (my_fdct_ptr)
nuclear@26 351 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@26 352 SIZEOF(my_fdct_controller));
nuclear@26 353 cinfo->fdct = (struct jpeg_forward_dct *) fdct;
nuclear@26 354 fdct->pub.start_pass = start_pass_fdctmgr;
nuclear@26 355
nuclear@26 356 switch (cinfo->dct_method) {
nuclear@26 357 #ifdef DCT_ISLOW_SUPPORTED
nuclear@26 358 case JDCT_ISLOW:
nuclear@26 359 fdct->pub.forward_DCT = forward_DCT;
nuclear@26 360 fdct->do_dct = jpeg_fdct_islow;
nuclear@26 361 break;
nuclear@26 362 #endif
nuclear@26 363 #ifdef DCT_IFAST_SUPPORTED
nuclear@26 364 case JDCT_IFAST:
nuclear@26 365 fdct->pub.forward_DCT = forward_DCT;
nuclear@26 366 fdct->do_dct = jpeg_fdct_ifast;
nuclear@26 367 break;
nuclear@26 368 #endif
nuclear@26 369 #ifdef DCT_FLOAT_SUPPORTED
nuclear@26 370 case JDCT_FLOAT:
nuclear@26 371 fdct->pub.forward_DCT = forward_DCT_float;
nuclear@26 372 fdct->do_float_dct = jpeg_fdct_float;
nuclear@26 373 break;
nuclear@26 374 #endif
nuclear@26 375 default:
nuclear@26 376 ERREXIT(cinfo, JERR_NOT_COMPILED);
nuclear@26 377 break;
nuclear@26 378 }
nuclear@26 379
nuclear@26 380 /* Mark divisor tables unallocated */
nuclear@26 381 for (i = 0; i < NUM_QUANT_TBLS; i++) {
nuclear@26 382 fdct->divisors[i] = NULL;
nuclear@26 383 #ifdef DCT_FLOAT_SUPPORTED
nuclear@26 384 fdct->float_divisors[i] = NULL;
nuclear@26 385 #endif
nuclear@26 386 }
nuclear@26 387 }