blob: 4cac666483985063e7c28439616758a911183f5f [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jcdctmgr.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
Pierre Ossmandedc42e2009-03-09 13:23:04 +00007 * Copyright (C) 1999-2006, MIYASAKA Masaru.
Pierre Ossman59a39382009-03-09 13:15:56 +00008 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCb5a55e62015-08-29 18:05:43 -05009 * Copyright (C) 2011, 2014-2015 D. R. Commander
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 * 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_INTERNALS
19#include "jinclude.h"
20#include "jpeglib.h"
DRCe5eaf372014-05-09 18:00:32 +000021#include "jdct.h" /* Private declarations for DCT subsystem */
Pierre Ossman59a39382009-03-09 13:15:56 +000022#include "jsimddct.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000023
24
25/* Private subobject for this module */
26
DRCbc56b752014-05-16 10:43:44 +000027typedef void (*forward_DCT_method_ptr) (DCTELEM * data);
28typedef void (*float_DCT_method_ptr) (FAST_FLOAT * data);
Pierre Ossman49dcbfb2009-03-09 10:37:20 +000029
DRCbc56b752014-05-16 10:43:44 +000030typedef void (*convsamp_method_ptr) (JSAMPARRAY sample_data,
31 JDIMENSION start_col,
32 DCTELEM * workspace);
33typedef void (*float_convsamp_method_ptr) (JSAMPARRAY sample_data,
34 JDIMENSION start_col,
35 FAST_FLOAT *workspace);
Pierre Ossman49dcbfb2009-03-09 10:37:20 +000036
DRCbc56b752014-05-16 10:43:44 +000037typedef void (*quantize_method_ptr) (JCOEFPTR coef_block, DCTELEM * divisors,
38 DCTELEM * workspace);
39typedef void (*float_quantize_method_ptr) (JCOEFPTR coef_block,
40 FAST_FLOAT * divisors,
41 FAST_FLOAT * workspace);
Pierre Ossman49dcbfb2009-03-09 10:37:20 +000042
DRCa49c4e52011-02-18 20:50:08 +000043METHODDEF(void) quantize (JCOEFPTR, DCTELEM *, DCTELEM *);
44
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000045typedef struct {
DRCe5eaf372014-05-09 18:00:32 +000046 struct jpeg_forward_dct pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047
48 /* Pointer to the DCT routine actually in use */
Pierre Ossman49dcbfb2009-03-09 10:37:20 +000049 forward_DCT_method_ptr dct;
50 convsamp_method_ptr convsamp;
51 quantize_method_ptr quantize;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000052
53 /* The actual post-DCT divisors --- not identical to the quant table
54 * entries, because of scaling (especially for an unnormalized DCT).
Thomas G. Lane489583f1996-02-07 00:00:00 +000055 * Each table is given in normal array order.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000056 */
57 DCTELEM * divisors[NUM_QUANT_TBLS];
58
Pierre Ossman35c47192009-03-09 13:29:37 +000059 /* work area for FDCT subroutine */
60 DCTELEM * workspace;
61
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000062#ifdef DCT_FLOAT_SUPPORTED
63 /* Same as above for the floating-point case. */
Pierre Ossman49dcbfb2009-03-09 10:37:20 +000064 float_DCT_method_ptr float_dct;
65 float_convsamp_method_ptr float_convsamp;
66 float_quantize_method_ptr float_quantize;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067 FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
Pierre Ossman35c47192009-03-09 13:29:37 +000068 FAST_FLOAT * float_workspace;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069#endif
70} my_fdct_controller;
71
72typedef my_fdct_controller * my_fdct_ptr;
73
74
DRCaee4f722014-08-09 23:06:07 +000075#if BITS_IN_JSAMPLE == 8
76
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000077/*
Pierre Ossmandedc42e2009-03-09 13:23:04 +000078 * Find the highest bit in an integer through binary search.
79 */
DRCaee4f722014-08-09 23:06:07 +000080
Pierre Ossmandedc42e2009-03-09 13:23:04 +000081LOCAL(int)
DRCfc5dc4f2009-10-01 22:26:14 +000082flss (UINT16 val)
Pierre Ossmandedc42e2009-03-09 13:23:04 +000083{
84 int bit;
85
86 bit = 16;
87
88 if (!val)
89 return 0;
90
91 if (!(val & 0xff00)) {
92 bit -= 8;
93 val <<= 8;
94 }
95 if (!(val & 0xf000)) {
96 bit -= 4;
97 val <<= 4;
98 }
99 if (!(val & 0xc000)) {
100 bit -= 2;
101 val <<= 2;
102 }
103 if (!(val & 0x8000)) {
104 bit -= 1;
105 val <<= 1;
106 }
107
108 return bit;
109}
110
DRCaee4f722014-08-09 23:06:07 +0000111
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000112/*
113 * Compute values to do a division using reciprocal.
114 *
115 * This implementation is based on an algorithm described in
116 * "How to optimize for the Pentium family of microprocessors"
117 * (http://www.agner.org/assem/).
118 * More information about the basic algorithm can be found in
119 * the paper "Integer Division Using Reciprocals" by Robert Alverson.
120 *
121 * The basic idea is to replace x/d by x * d^-1. In order to store
122 * d^-1 with enough precision we shift it left a few places. It turns
123 * out that this algoright gives just enough precision, and also fits
124 * into DCTELEM:
125 *
126 * b = (the number of significant bits in divisor) - 1
127 * r = (word size) + b
128 * f = 2^r / divisor
129 *
130 * f will not be an integer for most cases, so we need to compensate
131 * for the rounding error introduced:
132 *
133 * no fractional part:
134 *
135 * result = input >> r
136 *
137 * fractional part of f < 0.5:
138 *
139 * round f down to nearest integer
140 * result = ((input + 1) * f) >> r
141 *
142 * fractional part of f > 0.5:
143 *
144 * round f up to nearest integer
145 * result = (input * f) >> r
146 *
147 * This is the original algorithm that gives truncated results. But we
148 * want properly rounded results, so we replace "input" with
149 * "input + divisor/2".
150 *
151 * In order to allow SIMD implementations we also tweak the values to
152 * allow the same calculation to be made at all times:
DRCe5eaf372014-05-09 18:00:32 +0000153 *
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000154 * dctbl[0] = f rounded to nearest integer
155 * dctbl[1] = divisor / 2 (+ 1 if fractional part of f < 0.5)
156 * dctbl[2] = 1 << ((word size) * 2 - r)
157 * dctbl[3] = r - (word size)
158 *
159 * dctbl[2] is for stupid instruction sets where the shift operation
160 * isn't member wise (e.g. MMX).
161 *
162 * The reason dctbl[2] and dctbl[3] reduce the shift with (word size)
163 * is that most SIMD implementations have a "multiply and store top
164 * half" operation.
165 *
166 * Lastly, we store each of the values in their own table instead
167 * of in a consecutive manner, yet again in order to allow SIMD
168 * routines.
169 */
DRCaee4f722014-08-09 23:06:07 +0000170
DRCa49c4e52011-02-18 20:50:08 +0000171LOCAL(int)
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000172compute_reciprocal (UINT16 divisor, DCTELEM * dtbl)
173{
174 UDCTELEM2 fq, fr;
175 UDCTELEM c;
176 int b, r;
177
DRCb5a55e62015-08-29 18:05:43 -0500178 if (divisor == 1) {
179 /* divisor == 1 means unquantized, so these reciprocal/correction/shift
180 * values will cause the C quantization algorithm to act like the
181 * identity function. Since only the C quantization algorithm is used in
182 * these cases, the scale value is irrelevant.
183 */
184 dtbl[DCTSIZE2 * 0] = (DCTELEM) 1; /* reciprocal */
185 dtbl[DCTSIZE2 * 1] = (DCTELEM) 0; /* correction */
186 dtbl[DCTSIZE2 * 2] = (DCTELEM) 1; /* scale */
187 dtbl[DCTSIZE2 * 3] = (DCTELEM) (-sizeof(DCTELEM) * 8); /* shift */
188 return 0;
189 }
190
DRCfc5dc4f2009-10-01 22:26:14 +0000191 b = flss(divisor) - 1;
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000192 r = sizeof(DCTELEM) * 8 + b;
193
194 fq = ((UDCTELEM2)1 << r) / divisor;
195 fr = ((UDCTELEM2)1 << r) % divisor;
196
197 c = divisor / 2; /* for rounding */
198
199 if (fr == 0) { /* divisor is power of two */
200 /* fq will be one bit too large to fit in DCTELEM, so adjust */
201 fq >>= 1;
202 r--;
DRCd65d99a2012-01-31 03:39:23 +0000203 } else if (fr <= (divisor / 2U)) { /* fractional part is < 0.5 */
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000204 c++;
205 } else { /* fractional part is > 0.5 */
206 fq++;
207 }
208
209 dtbl[DCTSIZE2 * 0] = (DCTELEM) fq; /* reciprocal */
210 dtbl[DCTSIZE2 * 1] = (DCTELEM) c; /* correction + roundfactor */
211 dtbl[DCTSIZE2 * 2] = (DCTELEM) (1 << (sizeof(DCTELEM)*8*2 - r)); /* scale */
212 dtbl[DCTSIZE2 * 3] = (DCTELEM) r - sizeof(DCTELEM)*8; /* shift */
DRCa49c4e52011-02-18 20:50:08 +0000213
214 if(r <= 16) return 0;
215 else return 1;
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000216}
217
DRCaee4f722014-08-09 23:06:07 +0000218#endif
219
220
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000221/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000222 * Initialize for a processing pass.
223 * Verify that all referenced Q-tables are present, and set up
224 * the divisor table for each one.
225 * In the current implementation, DCT of all components is done during
226 * the first pass, even if only some components will be output in the
227 * first scan. Hence all components should be examined here.
228 */
229
Thomas G. Lane489583f1996-02-07 00:00:00 +0000230METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000231start_pass_fdctmgr (j_compress_ptr cinfo)
232{
233 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
234 int ci, qtblno, i;
235 jpeg_component_info *compptr;
236 JQUANT_TBL * qtbl;
237 DCTELEM * dtbl;
238
239 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
240 ci++, compptr++) {
241 qtblno = compptr->quant_tbl_no;
242 /* Make sure specified quantization table is present */
243 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
DRCe5eaf372014-05-09 18:00:32 +0000244 cinfo->quant_tbl_ptrs[qtblno] == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000245 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
246 qtbl = cinfo->quant_tbl_ptrs[qtblno];
247 /* Compute divisors for this quant table */
248 /* We may do this more than once for same table, but it's not a big deal */
249 switch (cinfo->dct_method) {
250#ifdef DCT_ISLOW_SUPPORTED
251 case JDCT_ISLOW:
252 /* For LL&M IDCT method, divisors are equal to raw quantization
253 * coefficients multiplied by 8 (to counteract scaling).
254 */
255 if (fdct->divisors[qtblno] == NULL) {
DRCe5eaf372014-05-09 18:00:32 +0000256 fdct->divisors[qtblno] = (DCTELEM *)
257 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000258 (DCTSIZE2 * 4) * sizeof(DCTELEM));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000259 }
260 dtbl = fdct->divisors[qtblno];
261 for (i = 0; i < DCTSIZE2; i++) {
DRCaee4f722014-08-09 23:06:07 +0000262#if BITS_IN_JSAMPLE == 8
DRCe5eaf372014-05-09 18:00:32 +0000263 if(!compute_reciprocal(qtbl->quantval[i] << 3, &dtbl[i])
264 && fdct->quantize == jsimd_quantize)
265 fdct->quantize = quantize;
DRCaee4f722014-08-09 23:06:07 +0000266#else
267 dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
268#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000269 }
270 break;
271#endif
272#ifdef DCT_IFAST_SUPPORTED
273 case JDCT_IFAST:
274 {
DRCe5eaf372014-05-09 18:00:32 +0000275 /* For AA&N IDCT method, divisors are equal to quantization
276 * coefficients scaled by scalefactor[row]*scalefactor[col], where
277 * scalefactor[0] = 1
278 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
279 * We apply a further scale factor of 8.
280 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000281#define CONST_BITS 14
DRCe5eaf372014-05-09 18:00:32 +0000282 static const INT16 aanscales[DCTSIZE2] = {
283 /* precomputed values scaled up by 14 bits */
284 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
285 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
286 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
287 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
288 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
289 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
290 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
291 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
292 };
293 SHIFT_TEMPS
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000294
DRCe5eaf372014-05-09 18:00:32 +0000295 if (fdct->divisors[qtblno] == NULL) {
296 fdct->divisors[qtblno] = (DCTELEM *)
297 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000298 (DCTSIZE2 * 4) * sizeof(DCTELEM));
DRCe5eaf372014-05-09 18:00:32 +0000299 }
300 dtbl = fdct->divisors[qtblno];
301 for (i = 0; i < DCTSIZE2; i++) {
DRCaee4f722014-08-09 23:06:07 +0000302#if BITS_IN_JSAMPLE == 8
DRCe5eaf372014-05-09 18:00:32 +0000303 if(!compute_reciprocal(
304 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
305 (INT32) aanscales[i]),
306 CONST_BITS-3), &dtbl[i])
307 && fdct->quantize == jsimd_quantize)
308 fdct->quantize = quantize;
DRCaee4f722014-08-09 23:06:07 +0000309#else
310 dtbl[i] = (DCTELEM)
311 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
312 (INT32) aanscales[i]),
313 CONST_BITS-3);
314#endif
DRCe5eaf372014-05-09 18:00:32 +0000315 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000316 }
317 break;
318#endif
319#ifdef DCT_FLOAT_SUPPORTED
320 case JDCT_FLOAT:
321 {
DRCe5eaf372014-05-09 18:00:32 +0000322 /* For float AA&N IDCT method, divisors are equal to quantization
323 * coefficients scaled by scalefactor[row]*scalefactor[col], where
324 * scalefactor[0] = 1
325 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
326 * We apply a further scale factor of 8.
327 * What's actually stored is 1/divisor so that the inner loop can
328 * use a multiplication rather than a division.
329 */
330 FAST_FLOAT * fdtbl;
331 int row, col;
332 static const double aanscalefactor[DCTSIZE] = {
333 1.0, 1.387039845, 1.306562965, 1.175875602,
334 1.0, 0.785694958, 0.541196100, 0.275899379
335 };
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000336
DRCe5eaf372014-05-09 18:00:32 +0000337 if (fdct->float_divisors[qtblno] == NULL) {
338 fdct->float_divisors[qtblno] = (FAST_FLOAT *)
339 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000340 DCTSIZE2 * sizeof(FAST_FLOAT));
DRCe5eaf372014-05-09 18:00:32 +0000341 }
342 fdtbl = fdct->float_divisors[qtblno];
343 i = 0;
344 for (row = 0; row < DCTSIZE; row++) {
345 for (col = 0; col < DCTSIZE; col++) {
346 fdtbl[i] = (FAST_FLOAT)
347 (1.0 / (((double) qtbl->quantval[i] *
348 aanscalefactor[row] * aanscalefactor[col] * 8.0)));
349 i++;
350 }
351 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352 }
353 break;
354#endif
355 default:
356 ERREXIT(cinfo, JERR_NOT_COMPILED);
357 break;
358 }
359 }
360}
361
362
363/*
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000364 * Load data into workspace, applying unsigned->signed conversion.
365 */
366
367METHODDEF(void)
368convsamp (JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM * workspace)
369{
370 register DCTELEM *workspaceptr;
371 register JSAMPROW elemptr;
372 register int elemr;
373
374 workspaceptr = workspace;
375 for (elemr = 0; elemr < DCTSIZE; elemr++) {
376 elemptr = sample_data[elemr] + start_col;
377
DRCe5eaf372014-05-09 18:00:32 +0000378#if DCTSIZE == 8 /* unroll the inner loop */
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000379 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
380 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
381 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
382 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
383 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
384 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
385 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
386 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
387#else
388 {
389 register int elemc;
390 for (elemc = DCTSIZE; elemc > 0; elemc--)
391 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
392 }
393#endif
394 }
395}
396
397
398/*
399 * Quantize/descale the coefficients, and store into coef_blocks[].
400 */
401
402METHODDEF(void)
403quantize (JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace)
404{
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000405 int i;
406 DCTELEM temp;
DRCaee4f722014-08-09 23:06:07 +0000407 JCOEFPTR output_ptr = coef_block;
408
409#if BITS_IN_JSAMPLE == 8
410
DRCb5a55e62015-08-29 18:05:43 -0500411 UDCTELEM recip, corr;
412 int shift;
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000413 UDCTELEM2 product;
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000414
415 for (i = 0; i < DCTSIZE2; i++) {
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000416 temp = workspace[i];
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000417 recip = divisors[i + DCTSIZE2 * 0];
418 corr = divisors[i + DCTSIZE2 * 1];
419 shift = divisors[i + DCTSIZE2 * 3];
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000420
421 if (temp < 0) {
422 temp = -temp;
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000423 product = (UDCTELEM2)(temp + corr) * recip;
424 product >>= shift + sizeof(DCTELEM)*8;
425 temp = product;
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000426 temp = -temp;
427 } else {
Pierre Ossmandedc42e2009-03-09 13:23:04 +0000428 product = (UDCTELEM2)(temp + corr) * recip;
429 product >>= shift + sizeof(DCTELEM)*8;
430 temp = product;
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000431 }
432 output_ptr[i] = (JCOEF) temp;
433 }
DRCaee4f722014-08-09 23:06:07 +0000434
435#else
436
437 register DCTELEM qval;
438
439 for (i = 0; i < DCTSIZE2; i++) {
440 qval = divisors[i];
441 temp = workspace[i];
442 /* Divide the coefficient value by qval, ensuring proper rounding.
443 * Since C does not specify the direction of rounding for negative
444 * quotients, we have to force the dividend positive for portability.
445 *
446 * In most files, at least half of the output values will be zero
447 * (at default quantization settings, more like three-quarters...)
448 * so we should ensure that this case is fast. On many machines,
449 * a comparison is enough cheaper than a divide to make a special test
450 * a win. Since both inputs will be nonnegative, we need only test
451 * for a < b to discover whether a/b is 0.
452 * If your machine's division is fast enough, define FAST_DIVIDE.
453 */
454#ifdef FAST_DIVIDE
455#define DIVIDE_BY(a,b) a /= b
456#else
457#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
458#endif
459 if (temp < 0) {
460 temp = -temp;
461 temp += qval>>1; /* for rounding */
462 DIVIDE_BY(temp, qval);
DRCeca06372014-11-06 09:32:38 +0000463 temp = -temp;
DRCaee4f722014-08-09 23:06:07 +0000464 } else {
465 temp += qval>>1; /* for rounding */
466 DIVIDE_BY(temp, qval);
467 }
468 output_ptr[i] = (JCOEF) temp;
469 }
470
471#endif
472
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000473}
474
475
476/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000477 * Perform forward DCT on one or more blocks of a component.
478 *
479 * The input samples are taken from the sample_data[] array starting at
480 * position start_row/start_col, and moving to the right for any additional
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000481 * blocks. The quantized coefficients are returned in coef_blocks[].
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000482 */
483
Thomas G. Lane489583f1996-02-07 00:00:00 +0000484METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000485forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000486 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
487 JDIMENSION start_row, JDIMENSION start_col,
488 JDIMENSION num_blocks)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000489/* This version is used for integer DCT implementations. */
490{
491 /* This routine is heavily used, so it's worth coding it tightly. */
492 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000493 DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
Pierre Ossman35c47192009-03-09 13:29:37 +0000494 DCTELEM * workspace;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000495 JDIMENSION bi;
496
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000497 /* Make sure the compiler doesn't look up these every pass */
498 forward_DCT_method_ptr do_dct = fdct->dct;
499 convsamp_method_ptr do_convsamp = fdct->convsamp;
500 quantize_method_ptr do_quantize = fdct->quantize;
Pierre Ossmandc5db142009-03-13 12:17:26 +0000501 workspace = fdct->workspace;
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000502
DRCe5eaf372014-05-09 18:00:32 +0000503 sample_data += start_row; /* fold in the vertical offset once */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000504
505 for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
506 /* Load data into workspace, applying unsigned->signed conversion */
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000507 (*do_convsamp) (sample_data, start_col, workspace);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000508
509 /* Perform the DCT */
510 (*do_dct) (workspace);
511
512 /* Quantize/descale the coefficients, and store into coef_blocks[] */
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000513 (*do_quantize) (coef_blocks[bi], divisors, workspace);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000514 }
515}
516
517
518#ifdef DCT_FLOAT_SUPPORTED
519
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000520
521METHODDEF(void)
522convsamp_float (JSAMPARRAY sample_data, JDIMENSION start_col, FAST_FLOAT * workspace)
523{
524 register FAST_FLOAT *workspaceptr;
525 register JSAMPROW elemptr;
526 register int elemr;
527
528 workspaceptr = workspace;
529 for (elemr = 0; elemr < DCTSIZE; elemr++) {
530 elemptr = sample_data[elemr] + start_col;
DRCe5eaf372014-05-09 18:00:32 +0000531#if DCTSIZE == 8 /* unroll the inner loop */
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000532 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
533 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
534 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
535 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
536 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
537 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
538 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
539 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
540#else
541 {
542 register int elemc;
543 for (elemc = DCTSIZE; elemc > 0; elemc--)
544 *workspaceptr++ = (FAST_FLOAT)
545 (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
546 }
547#endif
548 }
549}
550
551
552METHODDEF(void)
553quantize_float (JCOEFPTR coef_block, FAST_FLOAT * divisors, FAST_FLOAT * workspace)
554{
555 register FAST_FLOAT temp;
556 register int i;
557 register JCOEFPTR output_ptr = coef_block;
558
559 for (i = 0; i < DCTSIZE2; i++) {
560 /* Apply the quantization and scaling factor */
561 temp = workspace[i] * divisors[i];
562
563 /* Round to nearest integer.
564 * Since C does not specify the direction of rounding for negative
565 * quotients, we have to force the dividend positive for portability.
566 * The maximum coefficient size is +-16K (for 12-bit data), so this
567 * code should work for either 16-bit or 32-bit ints.
568 */
569 output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
570 }
571}
572
573
Thomas G. Lane489583f1996-02-07 00:00:00 +0000574METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000575forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000576 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
577 JDIMENSION start_row, JDIMENSION start_col,
578 JDIMENSION num_blocks)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000579/* This version is used for floating-point DCT implementations. */
580{
581 /* This routine is heavily used, so it's worth coding it tightly. */
582 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000583 FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
Pierre Ossman35c47192009-03-09 13:29:37 +0000584 FAST_FLOAT * workspace;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000585 JDIMENSION bi;
586
Pierre Ossman35c47192009-03-09 13:29:37 +0000587
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000588 /* Make sure the compiler doesn't look up these every pass */
589 float_DCT_method_ptr do_dct = fdct->float_dct;
590 float_convsamp_method_ptr do_convsamp = fdct->float_convsamp;
591 float_quantize_method_ptr do_quantize = fdct->float_quantize;
Pierre Ossmandc5db142009-03-13 12:17:26 +0000592 workspace = fdct->float_workspace;
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000593
DRCe5eaf372014-05-09 18:00:32 +0000594 sample_data += start_row; /* fold in the vertical offset once */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000595
596 for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
597 /* Load data into workspace, applying unsigned->signed conversion */
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000598 (*do_convsamp) (sample_data, start_col, workspace);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000599
600 /* Perform the DCT */
601 (*do_dct) (workspace);
602
603 /* Quantize/descale the coefficients, and store into coef_blocks[] */
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000604 (*do_quantize) (coef_blocks[bi], divisors, workspace);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000605 }
606}
607
608#endif /* DCT_FLOAT_SUPPORTED */
609
610
611/*
612 * Initialize FDCT manager.
613 */
614
Thomas G. Lane489583f1996-02-07 00:00:00 +0000615GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000616jinit_forward_dct (j_compress_ptr cinfo)
617{
618 my_fdct_ptr fdct;
619 int i;
620
621 fdct = (my_fdct_ptr)
622 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000623 sizeof(my_fdct_controller));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000624 cinfo->fdct = (struct jpeg_forward_dct *) fdct;
625 fdct->pub.start_pass = start_pass_fdctmgr;
626
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000627 /* First determine the DCT... */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000628 switch (cinfo->dct_method) {
629#ifdef DCT_ISLOW_SUPPORTED
630 case JDCT_ISLOW:
631 fdct->pub.forward_DCT = forward_DCT;
Pierre Ossman59a39382009-03-09 13:15:56 +0000632 if (jsimd_can_fdct_islow())
633 fdct->dct = jsimd_fdct_islow;
634 else
635 fdct->dct = jpeg_fdct_islow;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000636 break;
637#endif
638#ifdef DCT_IFAST_SUPPORTED
639 case JDCT_IFAST:
640 fdct->pub.forward_DCT = forward_DCT;
Pierre Ossman59a39382009-03-09 13:15:56 +0000641 if (jsimd_can_fdct_ifast())
642 fdct->dct = jsimd_fdct_ifast;
643 else
644 fdct->dct = jpeg_fdct_ifast;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000645 break;
646#endif
647#ifdef DCT_FLOAT_SUPPORTED
648 case JDCT_FLOAT:
649 fdct->pub.forward_DCT = forward_DCT_float;
Pierre Ossman59a39382009-03-09 13:15:56 +0000650 if (jsimd_can_fdct_float())
651 fdct->float_dct = jsimd_fdct_float;
652 else
653 fdct->float_dct = jpeg_fdct_float;
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000654 break;
655#endif
656 default:
657 ERREXIT(cinfo, JERR_NOT_COMPILED);
658 break;
659 }
660
661 /* ...then the supporting stages. */
662 switch (cinfo->dct_method) {
663#ifdef DCT_ISLOW_SUPPORTED
664 case JDCT_ISLOW:
665#endif
666#ifdef DCT_IFAST_SUPPORTED
667 case JDCT_IFAST:
668#endif
669#if defined(DCT_ISLOW_SUPPORTED) || defined(DCT_IFAST_SUPPORTED)
Pierre Ossman59a39382009-03-09 13:15:56 +0000670 if (jsimd_can_convsamp())
671 fdct->convsamp = jsimd_convsamp;
672 else
673 fdct->convsamp = convsamp;
674 if (jsimd_can_quantize())
675 fdct->quantize = jsimd_quantize;
676 else
677 fdct->quantize = quantize;
Pierre Ossman49dcbfb2009-03-09 10:37:20 +0000678 break;
679#endif
680#ifdef DCT_FLOAT_SUPPORTED
681 case JDCT_FLOAT:
Pierre Ossman59a39382009-03-09 13:15:56 +0000682 if (jsimd_can_convsamp_float())
683 fdct->float_convsamp = jsimd_convsamp_float;
684 else
685 fdct->float_convsamp = convsamp_float;
686 if (jsimd_can_quantize_float())
687 fdct->float_quantize = jsimd_quantize_float;
688 else
689 fdct->float_quantize = quantize_float;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000690 break;
691#endif
692 default:
693 ERREXIT(cinfo, JERR_NOT_COMPILED);
694 break;
695 }
696
Pierre Ossman35c47192009-03-09 13:29:37 +0000697 /* Allocate workspace memory */
698#ifdef DCT_FLOAT_SUPPORTED
699 if (cinfo->dct_method == JDCT_FLOAT)
700 fdct->float_workspace = (FAST_FLOAT *)
701 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000702 sizeof(FAST_FLOAT) * DCTSIZE2);
Pierre Ossman35c47192009-03-09 13:29:37 +0000703 else
704#endif
705 fdct->workspace = (DCTELEM *)
706 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000707 sizeof(DCTELEM) * DCTSIZE2);
Pierre Ossman35c47192009-03-09 13:29:37 +0000708
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000709 /* Mark divisor tables unallocated */
710 for (i = 0; i < NUM_QUANT_TBLS; i++) {
711 fdct->divisors[i] = NULL;
712#ifdef DCT_FLOAT_SUPPORTED
713 fdct->float_divisors[i] = NULL;
714#endif
715 }
716}