nuclear@2: /* nuclear@2: * jcdctmgr.c nuclear@2: * nuclear@2: * Copyright (C) 1994-1996, Thomas G. Lane. nuclear@2: * This file is part of the Independent JPEG Group's software. nuclear@2: * For conditions of distribution and use, see the accompanying README file. nuclear@2: * nuclear@2: * This file contains the forward-DCT management logic. nuclear@2: * This code selects a particular DCT implementation to be used, nuclear@2: * and it performs related housekeeping chores including coefficient nuclear@2: * quantization. nuclear@2: */ nuclear@2: nuclear@2: #define JPEG_INTERNALS nuclear@2: #include "jinclude.h" nuclear@2: #include "jpeglib.h" nuclear@2: #include "jdct.h" /* Private declarations for DCT subsystem */ nuclear@2: nuclear@2: nuclear@2: /* Private subobject for this module */ nuclear@2: nuclear@2: typedef struct { nuclear@2: struct jpeg_forward_dct pub; /* public fields */ nuclear@2: nuclear@2: /* Pointer to the DCT routine actually in use */ nuclear@2: forward_DCT_method_ptr do_dct; nuclear@2: nuclear@2: /* The actual post-DCT divisors --- not identical to the quant table nuclear@2: * entries, because of scaling (especially for an unnormalized DCT). nuclear@2: * Each table is given in normal array order. nuclear@2: */ nuclear@2: DCTELEM * divisors[NUM_QUANT_TBLS]; nuclear@2: nuclear@2: #ifdef DCT_FLOAT_SUPPORTED nuclear@2: /* Same as above for the floating-point case. */ nuclear@2: float_DCT_method_ptr do_float_dct; nuclear@2: FAST_FLOAT * float_divisors[NUM_QUANT_TBLS]; nuclear@2: #endif nuclear@2: } my_fdct_controller; nuclear@2: nuclear@2: typedef my_fdct_controller * my_fdct_ptr; nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Initialize for a processing pass. nuclear@2: * Verify that all referenced Q-tables are present, and set up nuclear@2: * the divisor table for each one. nuclear@2: * In the current implementation, DCT of all components is done during nuclear@2: * the first pass, even if only some components will be output in the nuclear@2: * first scan. Hence all components should be examined here. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: start_pass_fdctmgr (j_compress_ptr cinfo) nuclear@2: { nuclear@2: my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; nuclear@2: int ci, qtblno, i; nuclear@2: jpeg_component_info *compptr; nuclear@2: JQUANT_TBL * qtbl; nuclear@2: DCTELEM * dtbl; nuclear@2: nuclear@2: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@2: ci++, compptr++) { nuclear@2: qtblno = compptr->quant_tbl_no; nuclear@2: /* Make sure specified quantization table is present */ nuclear@2: if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || nuclear@2: cinfo->quant_tbl_ptrs[qtblno] == NULL) nuclear@2: ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno); nuclear@2: qtbl = cinfo->quant_tbl_ptrs[qtblno]; nuclear@2: /* Compute divisors for this quant table */ nuclear@2: /* We may do this more than once for same table, but it's not a big deal */ nuclear@2: switch (cinfo->dct_method) { nuclear@2: #ifdef DCT_ISLOW_SUPPORTED nuclear@2: case JDCT_ISLOW: nuclear@2: /* For LL&M IDCT method, divisors are equal to raw quantization nuclear@2: * coefficients multiplied by 8 (to counteract scaling). nuclear@2: */ nuclear@2: if (fdct->divisors[qtblno] == NULL) { nuclear@2: fdct->divisors[qtblno] = (DCTELEM *) nuclear@2: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@2: DCTSIZE2 * SIZEOF(DCTELEM)); nuclear@2: } nuclear@2: dtbl = fdct->divisors[qtblno]; nuclear@2: for (i = 0; i < DCTSIZE2; i++) { nuclear@2: dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3; nuclear@2: } nuclear@2: break; nuclear@2: #endif nuclear@2: #ifdef DCT_IFAST_SUPPORTED nuclear@2: case JDCT_IFAST: nuclear@2: { nuclear@2: /* For AA&N IDCT method, divisors are equal to quantization nuclear@2: * coefficients scaled by scalefactor[row]*scalefactor[col], where nuclear@2: * scalefactor[0] = 1 nuclear@2: * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 nuclear@2: * We apply a further scale factor of 8. nuclear@2: */ nuclear@2: #define CONST_BITS 14 nuclear@2: static const INT16 aanscales[DCTSIZE2] = { nuclear@2: /* precomputed values scaled up by 14 bits */ nuclear@2: 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, nuclear@2: 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, nuclear@2: 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, nuclear@2: 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, nuclear@2: 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, nuclear@2: 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, nuclear@2: 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, nuclear@2: 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 nuclear@2: }; nuclear@2: SHIFT_TEMPS nuclear@2: nuclear@2: if (fdct->divisors[qtblno] == NULL) { nuclear@2: fdct->divisors[qtblno] = (DCTELEM *) nuclear@2: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@2: DCTSIZE2 * SIZEOF(DCTELEM)); nuclear@2: } nuclear@2: dtbl = fdct->divisors[qtblno]; nuclear@2: for (i = 0; i < DCTSIZE2; i++) { nuclear@2: dtbl[i] = (DCTELEM) nuclear@2: DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], nuclear@2: (INT32) aanscales[i]), nuclear@2: CONST_BITS-3); nuclear@2: } nuclear@2: } nuclear@2: break; nuclear@2: #endif nuclear@2: #ifdef DCT_FLOAT_SUPPORTED nuclear@2: case JDCT_FLOAT: nuclear@2: { nuclear@2: /* For float AA&N IDCT method, divisors are equal to quantization nuclear@2: * coefficients scaled by scalefactor[row]*scalefactor[col], where nuclear@2: * scalefactor[0] = 1 nuclear@2: * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 nuclear@2: * We apply a further scale factor of 8. nuclear@2: * What's actually stored is 1/divisor so that the inner loop can nuclear@2: * use a multiplication rather than a division. nuclear@2: */ nuclear@2: FAST_FLOAT * fdtbl; nuclear@2: int row, col; nuclear@2: static const double aanscalefactor[DCTSIZE] = { nuclear@2: 1.0, 1.387039845, 1.306562965, 1.175875602, nuclear@2: 1.0, 0.785694958, 0.541196100, 0.275899379 nuclear@2: }; nuclear@2: nuclear@2: if (fdct->float_divisors[qtblno] == NULL) { nuclear@2: fdct->float_divisors[qtblno] = (FAST_FLOAT *) nuclear@2: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@2: DCTSIZE2 * SIZEOF(FAST_FLOAT)); nuclear@2: } nuclear@2: fdtbl = fdct->float_divisors[qtblno]; nuclear@2: i = 0; nuclear@2: for (row = 0; row < DCTSIZE; row++) { nuclear@2: for (col = 0; col < DCTSIZE; col++) { nuclear@2: fdtbl[i] = (FAST_FLOAT) nuclear@2: (1.0 / (((double) qtbl->quantval[i] * nuclear@2: aanscalefactor[row] * aanscalefactor[col] * 8.0))); nuclear@2: i++; nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: break; nuclear@2: #endif nuclear@2: default: nuclear@2: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@2: break; nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Perform forward DCT on one or more blocks of a component. nuclear@2: * nuclear@2: * The input samples are taken from the sample_data[] array starting at nuclear@2: * position start_row/start_col, and moving to the right for any additional nuclear@2: * blocks. The quantized coefficients are returned in coef_blocks[]. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr, nuclear@2: JSAMPARRAY sample_data, JBLOCKROW coef_blocks, nuclear@2: JDIMENSION start_row, JDIMENSION start_col, nuclear@2: JDIMENSION num_blocks) nuclear@2: /* This version is used for integer DCT implementations. */ nuclear@2: { nuclear@2: /* This routine is heavily used, so it's worth coding it tightly. */ nuclear@2: my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; nuclear@2: forward_DCT_method_ptr do_dct = fdct->do_dct; nuclear@2: DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no]; nuclear@2: DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */ nuclear@2: JDIMENSION bi; nuclear@2: nuclear@2: sample_data += start_row; /* fold in the vertical offset once */ nuclear@2: nuclear@2: for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) { nuclear@2: /* Load data into workspace, applying unsigned->signed conversion */ nuclear@2: { register DCTELEM *workspaceptr; nuclear@2: register JSAMPROW elemptr; nuclear@2: register int elemr; nuclear@2: nuclear@2: workspaceptr = workspace; nuclear@2: for (elemr = 0; elemr < DCTSIZE; elemr++) { nuclear@2: elemptr = sample_data[elemr] + start_col; nuclear@2: #if DCTSIZE == 8 /* unroll the inner loop */ nuclear@2: *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; nuclear@2: *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; nuclear@2: *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; nuclear@2: *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; nuclear@2: *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; nuclear@2: *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; nuclear@2: *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; nuclear@2: *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; nuclear@2: #else nuclear@2: { register int elemc; nuclear@2: for (elemc = DCTSIZE; elemc > 0; elemc--) { nuclear@2: *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; nuclear@2: } nuclear@2: } nuclear@2: #endif nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: /* Perform the DCT */ nuclear@2: (*do_dct) (workspace); nuclear@2: nuclear@2: /* Quantize/descale the coefficients, and store into coef_blocks[] */ nuclear@2: { register DCTELEM temp, qval; nuclear@2: register int i; nuclear@2: register JCOEFPTR output_ptr = coef_blocks[bi]; nuclear@2: nuclear@2: for (i = 0; i < DCTSIZE2; i++) { nuclear@2: qval = divisors[i]; nuclear@2: temp = workspace[i]; nuclear@2: /* Divide the coefficient value by qval, ensuring proper rounding. nuclear@2: * Since C does not specify the direction of rounding for negative nuclear@2: * quotients, we have to force the dividend positive for portability. nuclear@2: * nuclear@2: * In most files, at least half of the output values will be zero nuclear@2: * (at default quantization settings, more like three-quarters...) nuclear@2: * so we should ensure that this case is fast. On many machines, nuclear@2: * a comparison is enough cheaper than a divide to make a special test nuclear@2: * a win. Since both inputs will be nonnegative, we need only test nuclear@2: * for a < b to discover whether a/b is 0. nuclear@2: * If your machine's division is fast enough, define FAST_DIVIDE. nuclear@2: */ nuclear@2: #ifdef FAST_DIVIDE nuclear@2: #define DIVIDE_BY(a,b) a /= b nuclear@2: #else nuclear@2: #define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0 nuclear@2: #endif nuclear@2: if (temp < 0) { nuclear@2: temp = -temp; nuclear@2: temp += qval>>1; /* for rounding */ nuclear@2: DIVIDE_BY(temp, qval); nuclear@2: temp = -temp; nuclear@2: } else { nuclear@2: temp += qval>>1; /* for rounding */ nuclear@2: DIVIDE_BY(temp, qval); nuclear@2: } nuclear@2: output_ptr[i] = (JCOEF) temp; nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: #ifdef DCT_FLOAT_SUPPORTED nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr, nuclear@2: JSAMPARRAY sample_data, JBLOCKROW coef_blocks, nuclear@2: JDIMENSION start_row, JDIMENSION start_col, nuclear@2: JDIMENSION num_blocks) nuclear@2: /* This version is used for floating-point DCT implementations. */ nuclear@2: { nuclear@2: /* This routine is heavily used, so it's worth coding it tightly. */ nuclear@2: my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; nuclear@2: float_DCT_method_ptr do_dct = fdct->do_float_dct; nuclear@2: FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no]; nuclear@2: FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */ nuclear@2: JDIMENSION bi; nuclear@2: nuclear@2: sample_data += start_row; /* fold in the vertical offset once */ nuclear@2: nuclear@2: for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) { nuclear@2: /* Load data into workspace, applying unsigned->signed conversion */ nuclear@2: { register FAST_FLOAT *workspaceptr; nuclear@2: register JSAMPROW elemptr; nuclear@2: register int elemr; nuclear@2: nuclear@2: workspaceptr = workspace; nuclear@2: for (elemr = 0; elemr < DCTSIZE; elemr++) { nuclear@2: elemptr = sample_data[elemr] + start_col; nuclear@2: #if DCTSIZE == 8 /* unroll the inner loop */ nuclear@2: *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); nuclear@2: *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); nuclear@2: *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); nuclear@2: *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); nuclear@2: *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); nuclear@2: *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); nuclear@2: *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); nuclear@2: *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); nuclear@2: #else nuclear@2: { register int elemc; nuclear@2: for (elemc = DCTSIZE; elemc > 0; elemc--) { nuclear@2: *workspaceptr++ = (FAST_FLOAT) nuclear@2: (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); nuclear@2: } nuclear@2: } nuclear@2: #endif nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: /* Perform the DCT */ nuclear@2: (*do_dct) (workspace); nuclear@2: nuclear@2: /* Quantize/descale the coefficients, and store into coef_blocks[] */ nuclear@2: { register FAST_FLOAT temp; nuclear@2: register int i; nuclear@2: register JCOEFPTR output_ptr = coef_blocks[bi]; nuclear@2: nuclear@2: for (i = 0; i < DCTSIZE2; i++) { nuclear@2: /* Apply the quantization and scaling factor */ nuclear@2: temp = workspace[i] * divisors[i]; nuclear@2: /* Round to nearest integer. nuclear@2: * Since C does not specify the direction of rounding for negative nuclear@2: * quotients, we have to force the dividend positive for portability. nuclear@2: * The maximum coefficient size is +-16K (for 12-bit data), so this nuclear@2: * code should work for either 16-bit or 32-bit ints. nuclear@2: */ nuclear@2: output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384); nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: #endif /* DCT_FLOAT_SUPPORTED */ nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Initialize FDCT manager. nuclear@2: */ nuclear@2: nuclear@2: GLOBAL(void) nuclear@2: jinit_forward_dct (j_compress_ptr cinfo) nuclear@2: { nuclear@2: my_fdct_ptr fdct; nuclear@2: int i; nuclear@2: nuclear@2: fdct = (my_fdct_ptr) nuclear@2: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@2: SIZEOF(my_fdct_controller)); nuclear@2: cinfo->fdct = (struct jpeg_forward_dct *) fdct; nuclear@2: fdct->pub.start_pass = start_pass_fdctmgr; nuclear@2: nuclear@2: switch (cinfo->dct_method) { nuclear@2: #ifdef DCT_ISLOW_SUPPORTED nuclear@2: case JDCT_ISLOW: nuclear@2: fdct->pub.forward_DCT = forward_DCT; nuclear@2: fdct->do_dct = jpeg_fdct_islow; nuclear@2: break; nuclear@2: #endif nuclear@2: #ifdef DCT_IFAST_SUPPORTED nuclear@2: case JDCT_IFAST: nuclear@2: fdct->pub.forward_DCT = forward_DCT; nuclear@2: fdct->do_dct = jpeg_fdct_ifast; nuclear@2: break; nuclear@2: #endif nuclear@2: #ifdef DCT_FLOAT_SUPPORTED nuclear@2: case JDCT_FLOAT: nuclear@2: fdct->pub.forward_DCT = forward_DCT_float; nuclear@2: fdct->do_float_dct = jpeg_fdct_float; nuclear@2: break; nuclear@2: #endif nuclear@2: default: nuclear@2: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@2: break; nuclear@2: } nuclear@2: nuclear@2: /* Mark divisor tables unallocated */ nuclear@2: for (i = 0; i < NUM_QUANT_TBLS; i++) { nuclear@2: fdct->divisors[i] = NULL; nuclear@2: #ifdef DCT_FLOAT_SUPPORTED nuclear@2: fdct->float_divisors[i] = NULL; nuclear@2: #endif nuclear@2: } nuclear@2: }