1 /*
2 * jcdctmgr.c
3 *
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains the forward-DCT management logic.
9 * This code selects a particular DCT implementation to be used,
10 * and it performs related housekeeping chores including coefficient
11 * quantization.
12 */
13
14 #define JPEG_INTERNALS
15 #include "jinclude.h"
16 #include "jpeglib.h"
17 #include "jdct.h" /* Private declarations for DCT subsystem */
18
19
20 /* Private subobject for this module */
21
22 typedef struct {
23 struct jpeg_forward_dct pub; /* public fields */
24
25 /* Pointer to the DCT routine actually in use */
26 forward_DCT_method_ptr do_dct[MAX_COMPONENTS];
27
28 /* The actual post-DCT divisors --- not identical to the quant table
29 * entries, because of scaling (especially for an unnormalized DCT).
30 * Each table is given in normal array order.
31 */
32 DCTELEM * divisors[NUM_QUANT_TBLS];
33
34 #ifdef DCT_FLOAT_SUPPORTED
35 /* Same as above for the floating-point case. */
36 float_DCT_method_ptr do_float_dct[MAX_COMPONENTS];
37 FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
38 #endif
39 } my_fdct_controller;
40
41 typedef my_fdct_controller * my_fdct_ptr;
42
43
44 /* The current scaled-DCT routines require ISLOW-style divisor tables,
45 * so be sure to compile that code if either ISLOW or SCALING is requested.
46 */
47 #ifdef DCT_ISLOW_SUPPORTED
48 #define PROVIDE_ISLOW_TABLES
49 #else
50 #ifdef DCT_SCALING_SUPPORTED
51 #define PROVIDE_ISLOW_TABLES
52 #endif
53 #endif
54
55
56 /*
57 * Perform forward DCT on one or more blocks of a component.
58 *
59 * The input samples are taken from the sample_data[] array starting at
60 * position start_row/start_col, and moving to the right for any additional
61 * blocks. The quantized coefficients are returned in coef_blocks[].
62 */
63
64 METHODDEF(void)
65 forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
66 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
67 JDIMENSION start_row, JDIMENSION start_col,
68 JDIMENSION num_blocks)
69 /* This version is used for integer DCT implementations. */
70 {
71 /* This routine is heavily used, so it's worth coding it tightly. */
72 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
73 forward_DCT_method_ptr do_dct = fdct->do_dct[compptr->component_index];
74 DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
75 DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
76 JDIMENSION bi;
77
78 sample_data += start_row; /* fold in the vertical offset once */
79
80 for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
81 /* Perform the DCT */
82 (*do_dct) (workspace, sample_data, start_col);
83
84 /* Quantize/descale the coefficients, and store into coef_blocks[] */
85 { register DCTELEM temp, qval;
86 register int i;
87 register JCOEFPTR output_ptr = coef_blocks[bi];
88
89 for (i = 0; i < DCTSIZE2; i++) {
90 qval = divisors[i];
91 temp = workspace[i];
92 /* Divide the coefficient value by qval, ensuring proper rounding.
93 * Since C does not specify the direction of rounding for negative
94 * quotients, we have to force the dividend positive for portability.
117 }
118 output_ptr[i] = (JCOEF) temp;
119 }
120 }
121 }
122 }
123
124
125 #ifdef DCT_FLOAT_SUPPORTED
126
127 METHODDEF(void)
128 forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
129 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
130 JDIMENSION start_row, JDIMENSION start_col,
131 JDIMENSION num_blocks)
132 /* This version is used for floating-point DCT implementations. */
133 {
134 /* This routine is heavily used, so it's worth coding it tightly. */
135 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
136 float_DCT_method_ptr do_dct = fdct->do_float_dct[compptr->component_index];
137 FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
138 FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
139 JDIMENSION bi;
140
141 sample_data += start_row; /* fold in the vertical offset once */
142
143 for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
144 /* Perform the DCT */
145 (*do_dct) (workspace, sample_data, start_col);
146
147 /* Quantize/descale the coefficients, and store into coef_blocks[] */
148 { register FAST_FLOAT temp;
149 register int i;
150 register JCOEFPTR output_ptr = coef_blocks[bi];
151
152 for (i = 0; i < DCTSIZE2; i++) {
153 /* Apply the quantization and scaling factor */
154 temp = workspace[i] * divisors[i];
155 /* Round to nearest integer.
156 * Since C does not specify the direction of rounding for negative
157 * quotients, we have to force the dividend positive for portability.
335 fdct->do_float_dct[ci] = jpeg_fdct_float;
336 method = JDCT_FLOAT;
337 break;
338 #endif
339 default:
340 ERREXIT(cinfo, JERR_NOT_COMPILED);
341 break;
342 }
343 break;
344 default:
345 ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
346 compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
347 break;
348 }
349 qtblno = compptr->quant_tbl_no;
350 /* Make sure specified quantization table is present */
351 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
352 cinfo->quant_tbl_ptrs[qtblno] == NULL)
353 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
354 qtbl = cinfo->quant_tbl_ptrs[qtblno];
355 /* Compute divisors for this quant table */
356 /* We may do this more than once for same table, but it's not a big deal */
357 switch (method) {
358 #ifdef PROVIDE_ISLOW_TABLES
359 case JDCT_ISLOW:
360 /* For LL&M IDCT method, divisors are equal to raw quantization
361 * coefficients multiplied by 8 (to counteract scaling).
362 */
363 if (fdct->divisors[qtblno] == NULL) {
364 fdct->divisors[qtblno] = (DCTELEM *)
365 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
366 DCTSIZE2 * SIZEOF(DCTELEM));
367 }
368 dtbl = fdct->divisors[qtblno];
369 for (i = 0; i < DCTSIZE2; i++) {
370 dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
371 }
372 fdct->pub.forward_DCT[ci] = forward_DCT;
373 break;
374 #endif
375 #ifdef DCT_IFAST_SUPPORTED
376 case JDCT_IFAST:
377 {
378 /* For AA&N IDCT method, divisors are equal to quantization
379 * coefficients scaled by scalefactor[row]*scalefactor[col], where
380 * scalefactor[0] = 1
381 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
382 * We apply a further scale factor of 8.
383 */
384 #define CONST_BITS 14
385 static const INT16 aanscales[DCTSIZE2] = {
386 /* precomputed values scaled up by 14 bits */
387 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
388 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
389 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
390 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
391 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
392 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
393 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
394 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
395 };
396 SHIFT_TEMPS
397
398 if (fdct->divisors[qtblno] == NULL) {
399 fdct->divisors[qtblno] = (DCTELEM *)
400 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
401 DCTSIZE2 * SIZEOF(DCTELEM));
402 }
403 dtbl = fdct->divisors[qtblno];
404 for (i = 0; i < DCTSIZE2; i++) {
405 dtbl[i] = (DCTELEM)
406 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
407 (INT32) aanscales[i]),
408 CONST_BITS-3);
409 }
410 }
411 fdct->pub.forward_DCT[ci] = forward_DCT;
412 break;
413 #endif
414 #ifdef DCT_FLOAT_SUPPORTED
415 case JDCT_FLOAT:
416 {
417 /* For float AA&N IDCT method, divisors are equal to quantization
418 * coefficients scaled by scalefactor[row]*scalefactor[col], where
419 * scalefactor[0] = 1
420 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
421 * We apply a further scale factor of 8.
422 * What's actually stored is 1/divisor so that the inner loop can
423 * use a multiplication rather than a division.
424 */
425 FAST_FLOAT * fdtbl;
426 int row, col;
427 static const double aanscalefactor[DCTSIZE] = {
428 1.0, 1.387039845, 1.306562965, 1.175875602,
429 1.0, 0.785694958, 0.541196100, 0.275899379
430 };
431
432 if (fdct->float_divisors[qtblno] == NULL) {
433 fdct->float_divisors[qtblno] = (FAST_FLOAT *)
434 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
435 DCTSIZE2 * SIZEOF(FAST_FLOAT));
436 }
437 fdtbl = fdct->float_divisors[qtblno];
438 i = 0;
439 for (row = 0; row < DCTSIZE; row++) {
440 for (col = 0; col < DCTSIZE; col++) {
441 fdtbl[i] = (FAST_FLOAT)
442 (1.0 / (((double) qtbl->quantval[i] *
443 aanscalefactor[row] * aanscalefactor[col] * 8.0)));
444 i++;
445 }
446 }
447 }
448 fdct->pub.forward_DCT[ci] = forward_DCT_float;
449 break;
450 #endif
451 default:
452 ERREXIT(cinfo, JERR_NOT_COMPILED);
453 break;
454 }
455 }
456 }
457
458
459 /*
460 * Initialize FDCT manager.
461 */
462
463 GLOBAL(void)
464 jinit_forward_dct (j_compress_ptr cinfo)
465 {
466 my_fdct_ptr fdct;
467 int i;
468
469 fdct = (my_fdct_ptr)
470 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
471 SIZEOF(my_fdct_controller));
472 cinfo->fdct = (struct jpeg_forward_dct *) fdct;
473 fdct->pub.start_pass = start_pass_fdctmgr;
474
475 /* Mark divisor tables unallocated */
476 for (i = 0; i < NUM_QUANT_TBLS; i++) {
477 fdct->divisors[i] = NULL;
478 #ifdef DCT_FLOAT_SUPPORTED
479 fdct->float_divisors[i] = NULL;
480 #endif
481 }
482 }
|
1 /*
2 * jcdctmgr.c
3 *
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Modified 2003-2013 by Guido Vollbeding.
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 */
19
20
21 /* Private subobject for this module */
22
23 typedef struct {
24 struct jpeg_forward_dct pub; /* public fields */
25
26 /* Pointer to the DCT routine actually in use */
27 forward_DCT_method_ptr do_dct[MAX_COMPONENTS];
28
29 #ifdef DCT_FLOAT_SUPPORTED
30 /* Same as above for the floating-point case. */
31 float_DCT_method_ptr do_float_dct[MAX_COMPONENTS];
32 #endif
33 } my_fdct_controller;
34
35 typedef my_fdct_controller * my_fdct_ptr;
36
37
38 /* The allocated post-DCT divisor tables -- big enough for any
39 * supported variant and not identical to the quant table entries,
40 * because of scaling (especially for an unnormalized DCT) --
41 * are pointed to by dct_table in the per-component comp_info
42 * structures. Each table is given in normal array order.
43 */
44
45 typedef union {
46 DCTELEM int_array[DCTSIZE2];
47 #ifdef DCT_FLOAT_SUPPORTED
48 FAST_FLOAT float_array[DCTSIZE2];
49 #endif
50 } divisor_table;
51
52
53 /* The current scaled-DCT routines require ISLOW-style divisor tables,
54 * so be sure to compile that code if either ISLOW or SCALING is requested.
55 */
56 #ifdef DCT_ISLOW_SUPPORTED
57 #define PROVIDE_ISLOW_TABLES
58 #else
59 #ifdef DCT_SCALING_SUPPORTED
60 #define PROVIDE_ISLOW_TABLES
61 #endif
62 #endif
63
64
65 /*
66 * Perform forward DCT on one or more blocks of a component.
67 *
68 * The input samples are taken from the sample_data[] array starting at
69 * position start_row/start_col, and moving to the right for any additional
70 * blocks. The quantized coefficients are returned in coef_blocks[].
71 */
72
73 METHODDEF(void)
74 forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
75 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
76 JDIMENSION start_row, JDIMENSION start_col,
77 JDIMENSION num_blocks)
78 /* This version is used for integer DCT implementations. */
79 {
80 /* This routine is heavily used, so it's worth coding it tightly. */
81 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
82 forward_DCT_method_ptr do_dct = fdct->do_dct[compptr->component_index];
83 DCTELEM * divisors = (DCTELEM *) compptr->dct_table;
84 DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
85 JDIMENSION bi;
86
87 sample_data += start_row; /* fold in the vertical offset once */
88
89 for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
90 /* Perform the DCT */
91 (*do_dct) (workspace, sample_data, start_col);
92
93 /* Quantize/descale the coefficients, and store into coef_blocks[] */
94 { register DCTELEM temp, qval;
95 register int i;
96 register JCOEFPTR output_ptr = coef_blocks[bi];
97
98 for (i = 0; i < DCTSIZE2; i++) {
99 qval = divisors[i];
100 temp = workspace[i];
101 /* Divide the coefficient value by qval, ensuring proper rounding.
102 * Since C does not specify the direction of rounding for negative
103 * quotients, we have to force the dividend positive for portability.
126 }
127 output_ptr[i] = (JCOEF) temp;
128 }
129 }
130 }
131 }
132
133
134 #ifdef DCT_FLOAT_SUPPORTED
135
136 METHODDEF(void)
137 forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
138 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
139 JDIMENSION start_row, JDIMENSION start_col,
140 JDIMENSION num_blocks)
141 /* This version is used for floating-point DCT implementations. */
142 {
143 /* This routine is heavily used, so it's worth coding it tightly. */
144 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
145 float_DCT_method_ptr do_dct = fdct->do_float_dct[compptr->component_index];
146 FAST_FLOAT * divisors = (FAST_FLOAT *) compptr->dct_table;
147 FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
148 JDIMENSION bi;
149
150 sample_data += start_row; /* fold in the vertical offset once */
151
152 for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
153 /* Perform the DCT */
154 (*do_dct) (workspace, sample_data, start_col);
155
156 /* Quantize/descale the coefficients, and store into coef_blocks[] */
157 { register FAST_FLOAT temp;
158 register int i;
159 register JCOEFPTR output_ptr = coef_blocks[bi];
160
161 for (i = 0; i < DCTSIZE2; i++) {
162 /* Apply the quantization and scaling factor */
163 temp = workspace[i] * divisors[i];
164 /* Round to nearest integer.
165 * Since C does not specify the direction of rounding for negative
166 * quotients, we have to force the dividend positive for portability.
344 fdct->do_float_dct[ci] = jpeg_fdct_float;
345 method = JDCT_FLOAT;
346 break;
347 #endif
348 default:
349 ERREXIT(cinfo, JERR_NOT_COMPILED);
350 break;
351 }
352 break;
353 default:
354 ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
355 compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
356 break;
357 }
358 qtblno = compptr->quant_tbl_no;
359 /* Make sure specified quantization table is present */
360 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
361 cinfo->quant_tbl_ptrs[qtblno] == NULL)
362 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
363 qtbl = cinfo->quant_tbl_ptrs[qtblno];
364 /* Create divisor table from quant table */
365 switch (method) {
366 #ifdef PROVIDE_ISLOW_TABLES
367 case JDCT_ISLOW:
368 /* For LL&M IDCT method, divisors are equal to raw quantization
369 * coefficients multiplied by 8 (to counteract scaling).
370 */
371 dtbl = (DCTELEM *) compptr->dct_table;
372 for (i = 0; i < DCTSIZE2; i++) {
373 dtbl[i] =
374 ((DCTELEM) qtbl->quantval[i]) << (compptr->component_needed ? 4 : 3);
375 }
376 fdct->pub.forward_DCT[ci] = forward_DCT;
377 break;
378 #endif
379 #ifdef DCT_IFAST_SUPPORTED
380 case JDCT_IFAST:
381 {
382 /* For AA&N IDCT method, divisors are equal to quantization
383 * coefficients scaled by scalefactor[row]*scalefactor[col], where
384 * scalefactor[0] = 1
385 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
386 * We apply a further scale factor of 8.
387 */
388 #define CONST_BITS 14
389 static const INT16 aanscales[DCTSIZE2] = {
390 /* precomputed values scaled up by 14 bits */
391 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
392 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
393 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
394 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
395 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
396 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
397 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
398 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
399 };
400 SHIFT_TEMPS
401
402 dtbl = (DCTELEM *) compptr->dct_table;
403 for (i = 0; i < DCTSIZE2; i++) {
404 dtbl[i] = (DCTELEM)
405 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
406 (INT32) aanscales[i]),
407 compptr->component_needed ? CONST_BITS-4 : CONST_BITS-3);
408 }
409 }
410 fdct->pub.forward_DCT[ci] = forward_DCT;
411 break;
412 #endif
413 #ifdef DCT_FLOAT_SUPPORTED
414 case JDCT_FLOAT:
415 {
416 /* For float AA&N IDCT method, divisors are equal to quantization
417 * coefficients scaled by scalefactor[row]*scalefactor[col], where
418 * scalefactor[0] = 1
419 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
420 * We apply a further scale factor of 8.
421 * What's actually stored is 1/divisor so that the inner loop can
422 * use a multiplication rather than a division.
423 */
424 FAST_FLOAT * fdtbl = (FAST_FLOAT *) compptr->dct_table;
425 int row, col;
426 static const double aanscalefactor[DCTSIZE] = {
427 1.0, 1.387039845, 1.306562965, 1.175875602,
428 1.0, 0.785694958, 0.541196100, 0.275899379
429 };
430
431 i = 0;
432 for (row = 0; row < DCTSIZE; row++) {
433 for (col = 0; col < DCTSIZE; col++) {
434 fdtbl[i] = (FAST_FLOAT)
435 (1.0 / ((double) qtbl->quantval[i] *
436 aanscalefactor[row] * aanscalefactor[col] *
437 (compptr->component_needed ? 16.0 : 8.0)));
438 i++;
439 }
440 }
441 }
442 fdct->pub.forward_DCT[ci] = forward_DCT_float;
443 break;
444 #endif
445 default:
446 ERREXIT(cinfo, JERR_NOT_COMPILED);
447 break;
448 }
449 }
450 }
451
452
453 /*
454 * Initialize FDCT manager.
455 */
456
457 GLOBAL(void)
458 jinit_forward_dct (j_compress_ptr cinfo)
459 {
460 my_fdct_ptr fdct;
461 int ci;
462 jpeg_component_info *compptr;
463
464 fdct = (my_fdct_ptr)
465 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
466 SIZEOF(my_fdct_controller));
467 cinfo->fdct = &fdct->pub;
468 fdct->pub.start_pass = start_pass_fdctmgr;
469
470 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
471 ci++, compptr++) {
472 /* Allocate a divisor table for each component */
473 compptr->dct_table =
474 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
475 SIZEOF(divisor_table));
476 }
477 }
|