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