Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/jpeg/jcdctmgr.c
8669 views
1
/*
2
* jcdctmgr.c
3
*
4
* Copyright (C) 1994-1996, Thomas G. Lane.
5
* Modified 2003-2025 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
/*
54
* Perform forward DCT on one or more blocks of a component.
55
*
56
* The input samples are taken from the sample_data[] array starting at
57
* position start_col, and moving to the right for any additional blocks.
58
* The quantized coefficients are returned in coef_blocks[].
59
*/
60
61
METHODDEF(void)
62
forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
63
JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
64
JDIMENSION start_col, JDIMENSION num_blocks)
65
/* This version is used for integer DCT implementations. */
66
{
67
/* This routine is heavily used, so it's worth coding it tightly. */
68
my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
69
forward_DCT_method_ptr do_dct = fdct->do_dct[compptr->component_index];
70
DCTELEM * divisors = (DCTELEM *) compptr->dct_table;
71
DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
72
JDIMENSION bi;
73
74
for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
75
/* Perform the DCT */
76
(*do_dct) (workspace, sample_data, start_col);
77
78
/* Quantize/descale the coefficients, and store into coef_blocks[] */
79
{ register DCTELEM temp, qval;
80
register int i;
81
register JCOEFPTR output_ptr = coef_blocks[bi];
82
83
for (i = 0; i < DCTSIZE2; i++) {
84
qval = divisors[i];
85
temp = workspace[i];
86
/* Divide the coefficient value by qval, ensuring proper rounding.
87
* Since C does not specify the direction of rounding for negative
88
* quotients, we have to force the dividend positive for portability.
89
*
90
* In most files, at least half of the output values will be zero
91
* (at default quantization settings, more like three-quarters...)
92
* so we should ensure that this case is fast. On many machines,
93
* a comparison is enough cheaper than a divide to make a special
94
* test a win. Since both inputs will be nonnegative, we need
95
* only test for a < b to discover whether a/b is 0.
96
* If your machine's division is fast enough, define FAST_DIVIDE.
97
*/
98
#ifdef FAST_DIVIDE
99
#define DIVIDE_BY(a,b) a /= b
100
#else
101
#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
102
#endif
103
if (temp < 0) {
104
temp = -temp;
105
temp += qval>>1; /* for rounding */
106
DIVIDE_BY(temp, qval);
107
temp = -temp;
108
} else {
109
temp += qval>>1; /* for rounding */
110
DIVIDE_BY(temp, qval);
111
}
112
output_ptr[i] = (JCOEF) temp;
113
}
114
}
115
}
116
}
117
118
119
#ifdef DCT_FLOAT_SUPPORTED
120
121
METHODDEF(void)
122
forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
123
JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
124
JDIMENSION start_col, JDIMENSION num_blocks)
125
/* This version is used for floating-point DCT implementations. */
126
{
127
/* This routine is heavily used, so it's worth coding it tightly. */
128
my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
129
float_DCT_method_ptr do_dct = fdct->do_float_dct[compptr->component_index];
130
FAST_FLOAT * divisors = (FAST_FLOAT *) compptr->dct_table;
131
FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
132
JDIMENSION bi;
133
134
for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
135
/* Perform the DCT */
136
(*do_dct) (workspace, sample_data, start_col);
137
138
/* Quantize/descale the coefficients, and store into coef_blocks[] */
139
{ register FAST_FLOAT temp;
140
register int i;
141
register JCOEFPTR output_ptr = coef_blocks[bi];
142
143
for (i = 0; i < DCTSIZE2; i++) {
144
/* Apply the quantization and scaling factor */
145
temp = workspace[i] * divisors[i];
146
/* Round to nearest integer.
147
* Since C does not specify the direction of rounding for negative
148
* quotients, we have to force the dividend positive for portability.
149
* The maximum coefficient size is +-16K (for 12-bit data), so this
150
* code should work for either 16-bit or 32-bit ints.
151
*/
152
output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
153
}
154
}
155
}
156
}
157
158
#endif /* DCT_FLOAT_SUPPORTED */
159
160
161
/*
162
* Initialize for a processing pass.
163
* Verify that all referenced Q-tables are present, and set up
164
* the divisor table for each one.
165
* In the current implementation, DCT of all components is done during
166
* the first pass, even if only some components will be output in the
167
* first scan. Hence all components should be examined here.
168
*/
169
170
METHODDEF(void)
171
start_pass_fdctmgr (j_compress_ptr cinfo)
172
{
173
my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
174
int ci, qtblno, i;
175
jpeg_component_info *compptr;
176
J_DCT_METHOD method = JDCT_DEFAULT;
177
JQUANT_TBL * qtbl;
178
DCTELEM * dtbl;
179
180
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
181
ci++, compptr++) {
182
/* Select the proper DCT routine for this component's scaling */
183
switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) {
184
#ifdef DCT_SCALING_SUPPORTED
185
/*
186
* The current scaled-DCT routines require ISLOW-style divisor tables,
187
* so be sure to compile that code if either ISLOW or SCALING is requested.
188
*/
189
#ifndef PROVIDE_ISLOW_TABLES
190
#define PROVIDE_ISLOW_TABLES
191
#endif
192
case ((1 << 8) + 1):
193
fdct->do_dct[ci] = jpeg_fdct_1x1;
194
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
195
break;
196
case ((2 << 8) + 2):
197
fdct->do_dct[ci] = jpeg_fdct_2x2;
198
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
199
break;
200
case ((3 << 8) + 3):
201
fdct->do_dct[ci] = jpeg_fdct_3x3;
202
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
203
break;
204
case ((4 << 8) + 4):
205
fdct->do_dct[ci] = jpeg_fdct_4x4;
206
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
207
break;
208
case ((5 << 8) + 5):
209
fdct->do_dct[ci] = jpeg_fdct_5x5;
210
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
211
break;
212
case ((6 << 8) + 6):
213
fdct->do_dct[ci] = jpeg_fdct_6x6;
214
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
215
break;
216
case ((7 << 8) + 7):
217
fdct->do_dct[ci] = jpeg_fdct_7x7;
218
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
219
break;
220
case ((9 << 8) + 9):
221
fdct->do_dct[ci] = jpeg_fdct_9x9;
222
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
223
break;
224
case ((10 << 8) + 10):
225
fdct->do_dct[ci] = jpeg_fdct_10x10;
226
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
227
break;
228
case ((11 << 8) + 11):
229
fdct->do_dct[ci] = jpeg_fdct_11x11;
230
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
231
break;
232
case ((12 << 8) + 12):
233
fdct->do_dct[ci] = jpeg_fdct_12x12;
234
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
235
break;
236
case ((13 << 8) + 13):
237
fdct->do_dct[ci] = jpeg_fdct_13x13;
238
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
239
break;
240
case ((14 << 8) + 14):
241
fdct->do_dct[ci] = jpeg_fdct_14x14;
242
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
243
break;
244
case ((15 << 8) + 15):
245
fdct->do_dct[ci] = jpeg_fdct_15x15;
246
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
247
break;
248
case ((16 << 8) + 16):
249
fdct->do_dct[ci] = jpeg_fdct_16x16;
250
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
251
break;
252
case ((16 << 8) + 8):
253
fdct->do_dct[ci] = jpeg_fdct_16x8;
254
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
255
break;
256
case ((14 << 8) + 7):
257
fdct->do_dct[ci] = jpeg_fdct_14x7;
258
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
259
break;
260
case ((12 << 8) + 6):
261
fdct->do_dct[ci] = jpeg_fdct_12x6;
262
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
263
break;
264
case ((10 << 8) + 5):
265
fdct->do_dct[ci] = jpeg_fdct_10x5;
266
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
267
break;
268
case ((8 << 8) + 4):
269
fdct->do_dct[ci] = jpeg_fdct_8x4;
270
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
271
break;
272
case ((6 << 8) + 3):
273
fdct->do_dct[ci] = jpeg_fdct_6x3;
274
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
275
break;
276
case ((4 << 8) + 2):
277
fdct->do_dct[ci] = jpeg_fdct_4x2;
278
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
279
break;
280
case ((2 << 8) + 1):
281
fdct->do_dct[ci] = jpeg_fdct_2x1;
282
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
283
break;
284
case ((8 << 8) + 16):
285
fdct->do_dct[ci] = jpeg_fdct_8x16;
286
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
287
break;
288
case ((7 << 8) + 14):
289
fdct->do_dct[ci] = jpeg_fdct_7x14;
290
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
291
break;
292
case ((6 << 8) + 12):
293
fdct->do_dct[ci] = jpeg_fdct_6x12;
294
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
295
break;
296
case ((5 << 8) + 10):
297
fdct->do_dct[ci] = jpeg_fdct_5x10;
298
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
299
break;
300
case ((4 << 8) + 8):
301
fdct->do_dct[ci] = jpeg_fdct_4x8;
302
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
303
break;
304
case ((3 << 8) + 6):
305
fdct->do_dct[ci] = jpeg_fdct_3x6;
306
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
307
break;
308
case ((2 << 8) + 4):
309
fdct->do_dct[ci] = jpeg_fdct_2x4;
310
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
311
break;
312
case ((1 << 8) + 2):
313
fdct->do_dct[ci] = jpeg_fdct_1x2;
314
method = JDCT_ISLOW; /* jfdctint uses islow-style table */
315
break;
316
#endif
317
case ((DCTSIZE << 8) + DCTSIZE):
318
switch (cinfo->dct_method) {
319
#ifdef DCT_ISLOW_SUPPORTED
320
case JDCT_ISLOW:
321
#ifndef PROVIDE_ISLOW_TABLES
322
#define PROVIDE_ISLOW_TABLES
323
#endif
324
fdct->do_dct[ci] = jpeg_fdct_islow;
325
method = JDCT_ISLOW;
326
break;
327
#endif
328
#ifdef DCT_IFAST_SUPPORTED
329
case JDCT_IFAST:
330
#if BITS_IN_JSAMPLE < JPEG_DATA_PRECISION || \
331
BITS_IN_JSAMPLE > JPEG_DATA_PRECISION + 8
332
/*
333
* Adjustment of divisor tables in JDCT_IFAST
334
* below doesn't work well in this condition.
335
* Use JDCT_ISLOW instead.
336
*/
337
#ifndef PROVIDE_ISLOW_TABLES
338
#define PROVIDE_ISLOW_TABLES
339
#endif
340
fdct->do_dct[ci] = jpeg_fdct_islow;
341
method = JDCT_ISLOW;
342
#else
343
#ifndef PROVIDE_IFAST_TABLES
344
#define PROVIDE_IFAST_TABLES
345
#endif
346
fdct->do_dct[ci] = jpeg_fdct_ifast;
347
method = JDCT_IFAST;
348
#endif
349
break;
350
#endif
351
#ifdef DCT_FLOAT_SUPPORTED
352
case JDCT_FLOAT:
353
fdct->do_float_dct[ci] = jpeg_fdct_float;
354
method = JDCT_FLOAT;
355
break;
356
#endif
357
default:
358
ERREXIT(cinfo, JERR_NOT_COMPILED);
359
}
360
break;
361
default:
362
ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
363
compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
364
}
365
qtblno = compptr->quant_tbl_no;
366
/* Make sure specified quantization table is present */
367
if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
368
cinfo->quant_tbl_ptrs[qtblno] == NULL)
369
ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
370
qtbl = cinfo->quant_tbl_ptrs[qtblno];
371
/* Create divisor table from quant table */
372
switch (method) {
373
#ifdef PROVIDE_ISLOW_TABLES
374
case JDCT_ISLOW:
375
/* For LL&M FDCT method, divisors are equal to raw quantization
376
* coefficients multiplied by 8 (to counteract scaling).
377
*/
378
dtbl = (DCTELEM *) compptr->dct_table;
379
for (i = 0; i < DCTSIZE2; i++) {
380
dtbl[i] =
381
((DCTELEM) qtbl->quantval[i]) << (compptr->component_needed ? 4 : 3);
382
}
383
fdct->pub.forward_DCT[ci] = forward_DCT;
384
break;
385
#endif
386
#ifdef PROVIDE_IFAST_TABLES
387
case JDCT_IFAST:
388
{
389
/* For AA&N FDCT method, divisors are equal to quantization
390
* coefficients scaled by scalefactor[row]*scalefactor[col], where
391
* scalefactor[0] = 1
392
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
393
* We apply a further scale factor of 8
394
* with adjustment if necessary.
395
*/
396
#define CONST_BITS 14
397
static const INT16 aanscales[DCTSIZE2] = {
398
/* precomputed values scaled up by 14 bits */
399
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
400
22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
401
21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
402
19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
403
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
404
12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
405
8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
406
4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
407
};
408
SHIFT_TEMPS
409
410
dtbl = (DCTELEM *) compptr->dct_table;
411
if (compptr->component_needed) {
412
for (i = 0; i < DCTSIZE2; i++) {
413
dtbl[i] = (DCTELEM)
414
DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
415
(INT32) aanscales[i]),
416
CONST_BITS+JPEG_DATA_PRECISION-BITS_IN_JSAMPLE-4);
417
}
418
} else {
419
for (i = 0; i < DCTSIZE2; i++) {
420
dtbl[i] = (DCTELEM)
421
DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
422
(INT32) aanscales[i]),
423
CONST_BITS+JPEG_DATA_PRECISION-BITS_IN_JSAMPLE-3);
424
}
425
}
426
}
427
fdct->pub.forward_DCT[ci] = forward_DCT;
428
break;
429
#endif
430
#ifdef DCT_FLOAT_SUPPORTED
431
case JDCT_FLOAT:
432
{
433
/* For float AA&N FDCT method, divisors are equal to quantization
434
* coefficients scaled by scalefactor[row]*scalefactor[col], where
435
* scalefactor[0] = 1
436
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
437
* We apply a further scale factor of 8
438
* with adjustment if necessary.
439
* What's actually stored is 1/divisor so that the inner loop can
440
* use a multiplication rather than a division.
441
*/
442
FAST_FLOAT * fdtbl = (FAST_FLOAT *) compptr->dct_table;
443
int row, col;
444
static const double aanscalefactor[DCTSIZE] = {
445
1.0, 1.387039845, 1.306562965, 1.175875602,
446
1.0, 0.785694958, 0.541196100, 0.275899379
447
};
448
#if BITS_IN_JSAMPLE == JPEG_DATA_PRECISION
449
450
i = 0;
451
for (row = 0; row < DCTSIZE; row++) {
452
for (col = 0; col < DCTSIZE; col++) {
453
fdtbl[i] = (FAST_FLOAT)
454
(1.0 / ((double) qtbl->quantval[i] *
455
aanscalefactor[row] * aanscalefactor[col] *
456
(compptr->component_needed ? 16.0 : 8.0)));
457
#else
458
double extrafactor = compptr->component_needed ? 16.0 : 8.0;
459
460
/* Adjust extra factor */
461
#if BITS_IN_JSAMPLE < JPEG_DATA_PRECISION
462
i = JPEG_DATA_PRECISION - BITS_IN_JSAMPLE;
463
do { extrafactor *= 0.5; } while (--i);
464
#else
465
i = BITS_IN_JSAMPLE - JPEG_DATA_PRECISION;
466
do { extrafactor *= 2.0; } while (--i);
467
#endif
468
469
i = 0;
470
for (row = 0; row < DCTSIZE; row++) {
471
for (col = 0; col < DCTSIZE; col++) {
472
fdtbl[i] = (FAST_FLOAT)
473
(1.0 / ((double) qtbl->quantval[i] *
474
aanscalefactor[row] * aanscalefactor[col] *
475
extrafactor));
476
#endif
477
i++;
478
}
479
}
480
}
481
fdct->pub.forward_DCT[ci] = forward_DCT_float;
482
break;
483
#endif
484
default:
485
ERREXIT(cinfo, JERR_NOT_COMPILED);
486
}
487
}
488
}
489
490
491
/*
492
* Initialize FDCT manager.
493
*/
494
495
GLOBAL(void)
496
jinit_forward_dct (j_compress_ptr cinfo)
497
{
498
my_fdct_ptr fdct;
499
int ci;
500
jpeg_component_info *compptr;
501
502
fdct = (my_fdct_ptr) (*cinfo->mem->alloc_small)
503
((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_fdct_controller));
504
cinfo->fdct = &fdct->pub;
505
fdct->pub.start_pass = start_pass_fdctmgr;
506
507
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
508
ci++, compptr++) {
509
/* Allocate a divisor table for each component */
510
compptr->dct_table = (*cinfo->mem->alloc_small)
511
((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(divisor_table));
512
}
513
}
514
515