istereo2

annotate libs/libjpeg/jcdctmgr.c @ 35:643f4ab609a4

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