1 /*
2  * jcdctmgr.c
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1994-1996, Thomas G. Lane.
6  * libjpeg-turbo Modifications:
7  * Copyright (C) 1999-2006, MIYASAKA Masaru.
8  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
9  * Copyright (C) 2011, 2014-2015 D. R. Commander
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_INTERNALS
19 #include "jinclude.h"
20 #include "jpeglib.h"
21 #include "jdct.h"               /* Private declarations for DCT subsystem */
22 #include "jsimddct.h"
23 
24 
25 /* Private subobject for this module */
26 
27 typedef void (*forward_DCT_method_ptr) (DCTELEM * data);
28 typedef void (*float_DCT_method_ptr) (FAST_FLOAT * data);
29 
30 typedef void (*convsamp_method_ptr) (JSAMPARRAY sample_data,
31                                      JDIMENSION start_col,
32                                      DCTELEM * workspace);
33 typedef void (*float_convsamp_method_ptr) (JSAMPARRAY sample_data,
34                                            JDIMENSION start_col,
35                                            FAST_FLOAT *workspace);
36 
37 typedef void (*quantize_method_ptr) (JCOEFPTR coef_block, DCTELEM * divisors,
38                                      DCTELEM * workspace);
39 typedef void (*float_quantize_method_ptr) (JCOEFPTR coef_block,
40                                            FAST_FLOAT * divisors,
41                                            FAST_FLOAT * workspace);
42 
43 METHODDEF(void) quantize (JCOEFPTR, DCTELEM *, DCTELEM *);
44 
45 typedef struct {
46   struct jpeg_forward_dct pub;  /* public fields */
47 
48   /* Pointer to the DCT routine actually in use */
49   forward_DCT_method_ptr dct;
50   convsamp_method_ptr convsamp;
51   quantize_method_ptr quantize;
52 
53   /* The actual post-DCT divisors --- not identical to the quant table
54    * entries, because of scaling (especially for an unnormalized DCT).
55    * Each table is given in normal array order.
56    */
57   DCTELEM * divisors[NUM_QUANT_TBLS];
58 
59   /* work area for FDCT subroutine */
60   DCTELEM * workspace;
61 
62 #ifdef DCT_FLOAT_SUPPORTED
63   /* Same as above for the floating-point case. */
64   float_DCT_method_ptr float_dct;
65   float_convsamp_method_ptr float_convsamp;
66   float_quantize_method_ptr float_quantize;
67   FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
68   FAST_FLOAT * float_workspace;
69 #endif
70 } my_fdct_controller;
71 
72 typedef my_fdct_controller * my_fdct_ptr;
73 
74 
75 #if BITS_IN_JSAMPLE == 8
76 
77 /*
78  * Find the highest bit in an integer through binary search.
79  */
80 
81 LOCAL(int)
flss(UINT16 val)82 flss (UINT16 val)
83 {
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 
111 
112 /*
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:
153  *
154  *   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  */
170 
171 LOCAL(int)
compute_reciprocal(UINT16 divisor,DCTELEM * dtbl)172 compute_reciprocal (UINT16 divisor, DCTELEM * dtbl)
173 {
174   UDCTELEM2 fq, fr;
175   UDCTELEM c;
176   int b, r;
177 
178   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 
191   b = flss(divisor) - 1;
192   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--;
203   } else if (fr <= (divisor / 2U)) { /* fractional part is < 0.5 */
204     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 */
213 
214   if(r <= 16) return 0;
215   else return 1;
216 }
217 
218 #endif
219 
220 
221 /*
222  * 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 
230 METHODDEF(void)
start_pass_fdctmgr(j_compress_ptr cinfo)231 start_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 ||
244         cinfo->quant_tbl_ptrs[qtblno] == NULL)
245       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) {
256         fdct->divisors[qtblno] = (DCTELEM *)
257           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
258                                       (DCTSIZE2 * 4) * sizeof(DCTELEM));
259       }
260       dtbl = fdct->divisors[qtblno];
261       for (i = 0; i < DCTSIZE2; i++) {
262 #if BITS_IN_JSAMPLE == 8
263         if(!compute_reciprocal(qtbl->quantval[i] << 3, &dtbl[i])
264           && fdct->quantize == jsimd_quantize)
265           fdct->quantize = quantize;
266 #else
267         dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
268 #endif
269       }
270       break;
271 #endif
272 #ifdef DCT_IFAST_SUPPORTED
273     case JDCT_IFAST:
274       {
275         /* 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          */
281 #define CONST_BITS 14
282         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
294 
295         if (fdct->divisors[qtblno] == NULL) {
296           fdct->divisors[qtblno] = (DCTELEM *)
297             (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
298                                         (DCTSIZE2 * 4) * sizeof(DCTELEM));
299         }
300         dtbl = fdct->divisors[qtblno];
301         for (i = 0; i < DCTSIZE2; i++) {
302 #if BITS_IN_JSAMPLE == 8
303           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;
309 #else
310            dtbl[i] = (DCTELEM)
311              DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
312                                    (INT32) aanscales[i]),
313                      CONST_BITS-3);
314 #endif
315         }
316       }
317       break;
318 #endif
319 #ifdef DCT_FLOAT_SUPPORTED
320     case JDCT_FLOAT:
321       {
322         /* 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         };
336 
337         if (fdct->float_divisors[qtblno] == NULL) {
338           fdct->float_divisors[qtblno] = (FAST_FLOAT *)
339             (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
340                                         DCTSIZE2 * sizeof(FAST_FLOAT));
341         }
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         }
352       }
353       break;
354 #endif
355     default:
356       ERREXIT(cinfo, JERR_NOT_COMPILED);
357       break;
358     }
359   }
360 }
361 
362 
363 /*
364  * Load data into workspace, applying unsigned->signed conversion.
365  */
366 
367 METHODDEF(void)
convsamp(JSAMPARRAY sample_data,JDIMENSION start_col,DCTELEM * workspace)368 convsamp (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 
378 #if DCTSIZE == 8                /* unroll the inner loop */
379     *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 
402 METHODDEF(void)
quantize(JCOEFPTR coef_block,DCTELEM * divisors,DCTELEM * workspace)403 quantize (JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace)
404 {
405   int i;
406   DCTELEM temp;
407   JCOEFPTR output_ptr = coef_block;
408 
409 #if BITS_IN_JSAMPLE == 8
410 
411   UDCTELEM recip, corr;
412   int shift;
413   UDCTELEM2 product;
414 
415   for (i = 0; i < DCTSIZE2; i++) {
416     temp = workspace[i];
417     recip = divisors[i + DCTSIZE2 * 0];
418     corr =  divisors[i + DCTSIZE2 * 1];
419     shift = divisors[i + DCTSIZE2 * 3];
420 
421     if (temp < 0) {
422       temp = -temp;
423       product = (UDCTELEM2)(temp + corr) * recip;
424       product >>= shift + sizeof(DCTELEM)*8;
425       temp = product;
426       temp = -temp;
427     } else {
428       product = (UDCTELEM2)(temp + corr) * recip;
429       product >>= shift + sizeof(DCTELEM)*8;
430       temp = product;
431     }
432     output_ptr[i] = (JCOEF) temp;
433   }
434 
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);
463       temp = -temp;
464     } else {
465       temp += qval>>1;  /* for rounding */
466       DIVIDE_BY(temp, qval);
467     }
468     output_ptr[i] = (JCOEF) temp;
469   }
470 
471 #endif
472 
473 }
474 
475 
476 /*
477  * 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
481  * blocks. The quantized coefficients are returned in coef_blocks[].
482  */
483 
484 METHODDEF(void)
forward_DCT(j_compress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY sample_data,JBLOCKROW coef_blocks,JDIMENSION start_row,JDIMENSION start_col,JDIMENSION num_blocks)485 forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
486              JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
487              JDIMENSION start_row, JDIMENSION start_col,
488              JDIMENSION num_blocks)
489 /* 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;
493   DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
494   DCTELEM * workspace;
495   JDIMENSION bi;
496 
497   /* 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;
501   workspace = fdct->workspace;
502 
503   sample_data += start_row;     /* fold in the vertical offset once */
504 
505   for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
506     /* Load data into workspace, applying unsigned->signed conversion */
507     (*do_convsamp) (sample_data, start_col, workspace);
508 
509     /* Perform the DCT */
510     (*do_dct) (workspace);
511 
512     /* Quantize/descale the coefficients, and store into coef_blocks[] */
513     (*do_quantize) (coef_blocks[bi], divisors, workspace);
514   }
515 }
516 
517 
518 #ifdef DCT_FLOAT_SUPPORTED
519 
520 
521 METHODDEF(void)
convsamp_float(JSAMPARRAY sample_data,JDIMENSION start_col,FAST_FLOAT * workspace)522 convsamp_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;
531 #if DCTSIZE == 8                /* unroll the inner loop */
532     *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 
552 METHODDEF(void)
quantize_float(JCOEFPTR coef_block,FAST_FLOAT * divisors,FAST_FLOAT * workspace)553 quantize_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 
574 METHODDEF(void)
forward_DCT_float(j_compress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY sample_data,JBLOCKROW coef_blocks,JDIMENSION start_row,JDIMENSION start_col,JDIMENSION num_blocks)575 forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
576                    JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
577                    JDIMENSION start_row, JDIMENSION start_col,
578                    JDIMENSION num_blocks)
579 /* 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;
583   FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
584   FAST_FLOAT * workspace;
585   JDIMENSION bi;
586 
587 
588   /* 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;
592   workspace = fdct->float_workspace;
593 
594   sample_data += start_row;     /* fold in the vertical offset once */
595 
596   for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
597     /* Load data into workspace, applying unsigned->signed conversion */
598     (*do_convsamp) (sample_data, start_col, workspace);
599 
600     /* Perform the DCT */
601     (*do_dct) (workspace);
602 
603     /* Quantize/descale the coefficients, and store into coef_blocks[] */
604     (*do_quantize) (coef_blocks[bi], divisors, workspace);
605   }
606 }
607 
608 #endif /* DCT_FLOAT_SUPPORTED */
609 
610 
611 /*
612  * Initialize FDCT manager.
613  */
614 
615 GLOBAL(void)
jinit_forward_dct(j_compress_ptr cinfo)616 jinit_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,
623                                 sizeof(my_fdct_controller));
624   cinfo->fdct = (struct jpeg_forward_dct *) fdct;
625   fdct->pub.start_pass = start_pass_fdctmgr;
626 
627   /* First determine the DCT... */
628   switch (cinfo->dct_method) {
629 #ifdef DCT_ISLOW_SUPPORTED
630   case JDCT_ISLOW:
631     fdct->pub.forward_DCT = forward_DCT;
632     if (jsimd_can_fdct_islow())
633       fdct->dct = jsimd_fdct_islow;
634     else
635       fdct->dct = jpeg_fdct_islow;
636     break;
637 #endif
638 #ifdef DCT_IFAST_SUPPORTED
639   case JDCT_IFAST:
640     fdct->pub.forward_DCT = forward_DCT;
641     if (jsimd_can_fdct_ifast())
642       fdct->dct = jsimd_fdct_ifast;
643     else
644       fdct->dct = jpeg_fdct_ifast;
645     break;
646 #endif
647 #ifdef DCT_FLOAT_SUPPORTED
648   case JDCT_FLOAT:
649     fdct->pub.forward_DCT = forward_DCT_float;
650     if (jsimd_can_fdct_float())
651       fdct->float_dct = jsimd_fdct_float;
652     else
653       fdct->float_dct = jpeg_fdct_float;
654     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)
670     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;
678     break;
679 #endif
680 #ifdef DCT_FLOAT_SUPPORTED
681   case JDCT_FLOAT:
682     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;
690     break;
691 #endif
692   default:
693     ERREXIT(cinfo, JERR_NOT_COMPILED);
694     break;
695   }
696 
697   /* 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,
702                                   sizeof(FAST_FLOAT) * DCTSIZE2);
703   else
704 #endif
705     fdct->workspace = (DCTELEM *)
706       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
707                                   sizeof(DCTELEM) * DCTSIZE2);
708 
709   /* 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 }
717