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