Coverage Report

Created: 2025-12-05 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/src/jcdctmgr.c
Line
Count
Source
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, 2022, 2024-2025, D. R. Commander.
10
 * For conditions of distribution and use, see the accompanying README.ijg
11
 * file.
12
 *
13
 * This file contains the forward-DCT management logic.
14
 * This code selects a particular DCT implementation to be used,
15
 * and it performs related housekeeping chores including coefficient
16
 * quantization.
17
 */
18
19
#define JPEG_INTERNALS
20
#include "jinclude.h"
21
#include "jpeglib.h"
22
#include "jdct.h"               /* Private declarations for DCT subsystem */
23
#ifdef WITH_SIMD
24
#include "../simd/jsimddct.h"
25
#endif
26
#ifdef WITH_PROFILE
27
#include "tjutil.h"
28
#endif
29
30
31
/* Private subobject for this module */
32
33
METHODDEF(void) quantize(JCOEFPTR, DCTELEM *, DCTELEM *);
34
35
typedef struct {
36
  struct jpeg_forward_dct pub;  /* public fields */
37
38
  /* Pointer to the DCT routine actually in use */
39
  forward_DCT_method_ptr dct;
40
  convsamp_method_ptr convsamp;
41
  quantize_method_ptr quantize;
42
43
  /* The actual post-DCT divisors --- not identical to the quant table
44
   * entries, because of scaling (especially for an unnormalized DCT).
45
   * Each table is given in normal array order.
46
   */
47
  DCTELEM *divisors[NUM_QUANT_TBLS];
48
49
  /* work area for FDCT subroutine */
50
  DCTELEM *workspace;
51
52
#ifdef DCT_FLOAT_SUPPORTED
53
  /* Same as above for the floating-point case. */
54
  float_DCT_method_ptr float_dct;
55
  float_convsamp_method_ptr float_convsamp;
56
  float_quantize_method_ptr float_quantize;
57
  FAST_FLOAT *float_divisors[NUM_QUANT_TBLS];
58
  FAST_FLOAT *float_workspace;
59
#endif
60
} my_fdct_controller;
61
62
typedef my_fdct_controller *my_fdct_ptr;
63
64
65
#if BITS_IN_JSAMPLE == 8
66
67
/*
68
 * Find the highest bit in an integer through binary search.
69
 */
70
71
LOCAL(int)
72
flss(UINT16 val)
73
0
{
74
0
  int bit;
75
76
0
  bit = 16;
77
78
0
  if (!val)
79
0
    return 0;
80
81
0
  if (!(val & 0xff00)) {
82
0
    bit -= 8;
83
0
    val <<= 8;
84
0
  }
85
0
  if (!(val & 0xf000)) {
86
0
    bit -= 4;
87
0
    val <<= 4;
88
0
  }
89
0
  if (!(val & 0xc000)) {
90
0
    bit -= 2;
91
0
    val <<= 2;
92
0
  }
93
0
  if (!(val & 0x8000)) {
94
0
    bit -= 1;
95
0
    val <<= 1;
96
0
  }
97
98
0
  return bit;
99
0
}
100
101
102
/*
103
 * Compute values to do a division using reciprocal.
104
 *
105
 * This implementation is based on an algorithm described in
106
 *   "Optimizing subroutines in assembly language:
107
 *   An optimization guide for x86 platforms" (https://agner.org/optimize).
108
 * More information about the basic algorithm can be found in
109
 * the paper "Integer Division Using Reciprocals" by Robert Alverson.
110
 *
111
 * The basic idea is to replace x/d by x * d^-1. In order to store
112
 * d^-1 with enough precision we shift it left a few places. It turns
113
 * out that this algoright gives just enough precision, and also fits
114
 * into DCTELEM:
115
 *
116
 *   b = (the number of significant bits in divisor) - 1
117
 *   r = (word size) + b
118
 *   f = 2^r / divisor
119
 *
120
 * f will not be an integer for most cases, so we need to compensate
121
 * for the rounding error introduced:
122
 *
123
 *   no fractional part:
124
 *
125
 *       result = input >> r
126
 *
127
 *   fractional part of f < 0.5:
128
 *
129
 *       round f down to nearest integer
130
 *       result = ((input + 1) * f) >> r
131
 *
132
 *   fractional part of f > 0.5:
133
 *
134
 *       round f up to nearest integer
135
 *       result = (input * f) >> r
136
 *
137
 * This is the original algorithm that gives truncated results. But we
138
 * want properly rounded results, so we replace "input" with
139
 * "input + divisor/2".
140
 *
141
 * In order to allow SIMD implementations we also tweak the values to
142
 * allow the same calculation to be made at all times:
143
 *
144
 *   dctbl[0] = f rounded to nearest integer
145
 *   dctbl[1] = divisor / 2 (+ 1 if fractional part of f < 0.5)
146
 *   dctbl[2] = 1 << ((word size) * 2 - r)
147
 *   dctbl[3] = r - (word size)
148
 *
149
 * dctbl[2] is for stupid instruction sets where the shift operation
150
 * isn't member wise (e.g. MMX).
151
 *
152
 * The reason dctbl[2] and dctbl[3] reduce the shift with (word size)
153
 * is that most SIMD implementations have a "multiply and store top
154
 * half" operation.
155
 *
156
 * Lastly, we store each of the values in their own table instead
157
 * of in a consecutive manner, yet again in order to allow SIMD
158
 * routines.
159
 */
160
161
LOCAL(int)
162
compute_reciprocal(UINT16 divisor, DCTELEM *dtbl)
163
0
{
164
0
  UDCTELEM2 fq, fr;
165
0
  UDCTELEM c;
166
0
  int b, r;
167
168
0
  if (divisor == 1) {
169
    /* divisor == 1 means unquantized, so these reciprocal/correction/shift
170
     * values will cause the C quantization algorithm to act like the
171
     * identity function.  Since only the C quantization algorithm is used in
172
     * these cases, the scale value is irrelevant.
173
     */
174
0
    dtbl[DCTSIZE2 * 0] = (DCTELEM)1;                        /* reciprocal */
175
0
    dtbl[DCTSIZE2 * 1] = (DCTELEM)0;                        /* correction */
176
0
    dtbl[DCTSIZE2 * 2] = (DCTELEM)1;                        /* scale */
177
0
    dtbl[DCTSIZE2 * 3] = -(DCTELEM)(sizeof(DCTELEM) * 8);   /* shift */
178
0
    return 0;
179
0
  }
180
181
0
  b = flss(divisor) - 1;
182
0
  r  = sizeof(DCTELEM) * 8 + b;
183
184
0
  fq = ((UDCTELEM2)1 << r) / divisor;
185
0
  fr = ((UDCTELEM2)1 << r) % divisor;
186
187
0
  c = divisor / 2;                      /* for rounding */
188
189
0
  if (fr == 0) {                        /* divisor is power of two */
190
    /* fq will be one bit too large to fit in DCTELEM, so adjust */
191
0
    fq >>= 1;
192
0
    r--;
193
0
  } else if (fr <= (divisor / 2U)) {    /* fractional part is < 0.5 */
194
0
    c++;
195
0
  } else {                              /* fractional part is > 0.5 */
196
0
    fq++;
197
0
  }
198
199
0
  dtbl[DCTSIZE2 * 0] = (DCTELEM)fq;     /* reciprocal */
200
0
  dtbl[DCTSIZE2 * 1] = (DCTELEM)c;      /* correction + roundfactor */
201
0
#ifdef WITH_SIMD
202
0
  dtbl[DCTSIZE2 * 2] = (DCTELEM)(1 << (sizeof(DCTELEM) * 8 * 2 - r)); /* scale */
203
#else
204
  dtbl[DCTSIZE2 * 2] = 1;
205
#endif
206
0
  dtbl[DCTSIZE2 * 3] = (DCTELEM)r - sizeof(DCTELEM) * 8; /* shift */
207
208
0
  if (r <= 16) return 0;
209
0
  else return 1;
210
0
}
211
212
#endif
213
214
215
/*
216
 * Initialize for a processing pass.
217
 * Verify that all referenced Q-tables are present, and set up
218
 * the divisor table for each one.
219
 * In the current implementation, DCT of all components is done during
220
 * the first pass, even if only some components will be output in the
221
 * first scan.  Hence all components should be examined here.
222
 */
223
224
METHODDEF(void)
225
start_pass_fdctmgr(j_compress_ptr cinfo)
226
0
{
227
0
  my_fdct_ptr fdct = (my_fdct_ptr)cinfo->fdct;
228
0
  int ci, qtblno, i;
229
0
  jpeg_component_info *compptr;
230
0
  JQUANT_TBL *qtbl;
231
0
  DCTELEM *dtbl;
232
233
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
234
0
       ci++, compptr++) {
235
0
    qtblno = compptr->quant_tbl_no;
236
    /* Make sure specified quantization table is present */
237
0
    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
238
0
        cinfo->quant_tbl_ptrs[qtblno] == NULL)
239
0
      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
240
0
    qtbl = cinfo->quant_tbl_ptrs[qtblno];
241
    /* Compute divisors for this quant table */
242
    /* We may do this more than once for same table, but it's not a big deal */
243
0
    switch (cinfo->dct_method) {
244
0
#ifdef DCT_ISLOW_SUPPORTED
245
0
    case JDCT_ISLOW:
246
      /* For LL&M IDCT method, divisors are equal to raw quantization
247
       * coefficients multiplied by 8 (to counteract scaling).
248
       */
249
0
      if (fdct->divisors[qtblno] == NULL) {
250
0
        fdct->divisors[qtblno] = (DCTELEM *)
251
0
          (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
252
0
                                      (DCTSIZE2 * 4) * sizeof(DCTELEM));
253
0
      }
254
0
      dtbl = fdct->divisors[qtblno];
255
0
      for (i = 0; i < DCTSIZE2; i++) {
256
#if BITS_IN_JSAMPLE == 8
257
#ifdef WITH_SIMD
258
0
        if (!compute_reciprocal(qtbl->quantval[i] << 3, &dtbl[i]) &&
259
0
            fdct->quantize != quantize)
260
0
          fdct->quantize = quantize;
261
#else
262
        compute_reciprocal(qtbl->quantval[i] << 3, &dtbl[i]);
263
#endif
264
#else
265
        dtbl[i] = ((DCTELEM)qtbl->quantval[i]) << 3;
266
#endif
267
0
      }
268
0
      break;
269
0
#endif
270
0
#ifdef DCT_IFAST_SUPPORTED
271
0
    case JDCT_IFAST:
272
0
      {
273
        /* For AA&N IDCT method, divisors are equal to quantization
274
         * coefficients scaled by scalefactor[row]*scalefactor[col], where
275
         *   scalefactor[0] = 1
276
         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
277
         * We apply a further scale factor of 8.
278
         */
279
0
#define CONST_BITS  14
280
0
        static const INT16 aanscales[DCTSIZE2] = {
281
          /* precomputed values scaled up by 14 bits */
282
0
          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
283
0
          22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
284
0
          21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
285
0
          19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
286
0
          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
287
0
          12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
288
0
           8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
289
0
           4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
290
0
        };
291
0
        SHIFT_TEMPS
292
293
0
        if (fdct->divisors[qtblno] == NULL) {
294
0
          fdct->divisors[qtblno] = (DCTELEM *)
295
0
            (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
296
0
                                        (DCTSIZE2 * 4) * sizeof(DCTELEM));
297
0
        }
298
0
        dtbl = fdct->divisors[qtblno];
299
0
        for (i = 0; i < DCTSIZE2; i++) {
300
#if BITS_IN_JSAMPLE == 8
301
#ifdef WITH_SIMD
302
0
          if (!compute_reciprocal(
303
0
                DESCALE(MULTIPLY16V16((JLONG)qtbl->quantval[i],
304
0
                                      (JLONG)aanscales[i]),
305
0
                        CONST_BITS - 3), &dtbl[i]) &&
306
0
              fdct->quantize != quantize)
307
0
            fdct->quantize = quantize;
308
#else
309
          compute_reciprocal(
310
            DESCALE(MULTIPLY16V16((JLONG)qtbl->quantval[i],
311
                                  (JLONG)aanscales[i]),
312
                    CONST_BITS-3), &dtbl[i]);
313
#endif
314
#else
315
          dtbl[i] = (DCTELEM)
316
0
            DESCALE(MULTIPLY16V16((JLONG)qtbl->quantval[i],
317
                                  (JLONG)aanscales[i]),
318
                    CONST_BITS - 3);
319
#endif
320
0
        }
321
0
      }
322
0
      break;
323
0
#endif
324
0
#ifdef DCT_FLOAT_SUPPORTED
325
0
    case JDCT_FLOAT:
326
0
      {
327
        /* For float AA&N IDCT method, divisors are equal to quantization
328
         * coefficients scaled by scalefactor[row]*scalefactor[col], where
329
         *   scalefactor[0] = 1
330
         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
331
         * We apply a further scale factor of 8.
332
         * What's actually stored is 1/divisor so that the inner loop can
333
         * use a multiplication rather than a division.
334
         */
335
0
        FAST_FLOAT *fdtbl;
336
0
        int row, col;
337
0
        static const double aanscalefactor[DCTSIZE] = {
338
0
          1.0, 1.387039845, 1.306562965, 1.175875602,
339
0
          1.0, 0.785694958, 0.541196100, 0.275899379
340
0
        };
341
342
0
        if (fdct->float_divisors[qtblno] == NULL) {
343
0
          fdct->float_divisors[qtblno] = (FAST_FLOAT *)
344
0
            (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
345
0
                                        DCTSIZE2 * sizeof(FAST_FLOAT));
346
0
        }
347
0
        fdtbl = fdct->float_divisors[qtblno];
348
0
        i = 0;
349
0
        for (row = 0; row < DCTSIZE; row++) {
350
0
          for (col = 0; col < DCTSIZE; col++) {
351
0
            fdtbl[i] = (FAST_FLOAT)
352
0
              (1.0 / (((double)qtbl->quantval[i] *
353
0
                       aanscalefactor[row] * aanscalefactor[col] * 8.0)));
354
0
            i++;
355
0
          }
356
0
        }
357
0
      }
358
0
      break;
359
0
#endif
360
0
    default:
361
0
      ERREXIT(cinfo, JERR_NOT_COMPILED);
362
0
      break;
363
0
    }
364
0
  }
365
0
}
Unexecuted instantiation: jcdctmgr-8.c:start_pass_fdctmgr
Unexecuted instantiation: jcdctmgr-12.c:start_pass_fdctmgr
366
367
368
/*
369
 * Load data into workspace, applying unsigned->signed conversion.
370
 */
371
372
METHODDEF(void)
373
convsamp(_JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM *workspace)
374
0
{
375
0
  register DCTELEM *workspaceptr;
376
0
  register _JSAMPROW elemptr;
377
0
  register int elemr;
378
379
0
  workspaceptr = workspace;
380
0
  for (elemr = 0; elemr < DCTSIZE; elemr++) {
381
0
    elemptr = sample_data[elemr] + start_col;
382
383
0
#if DCTSIZE == 8                /* unroll the inner loop */
384
0
    *workspaceptr++ = (*elemptr++) - _CENTERJSAMPLE;
385
0
    *workspaceptr++ = (*elemptr++) - _CENTERJSAMPLE;
386
0
    *workspaceptr++ = (*elemptr++) - _CENTERJSAMPLE;
387
0
    *workspaceptr++ = (*elemptr++) - _CENTERJSAMPLE;
388
0
    *workspaceptr++ = (*elemptr++) - _CENTERJSAMPLE;
389
0
    *workspaceptr++ = (*elemptr++) - _CENTERJSAMPLE;
390
0
    *workspaceptr++ = (*elemptr++) - _CENTERJSAMPLE;
391
0
    *workspaceptr++ = (*elemptr++) - _CENTERJSAMPLE;
392
#else
393
    {
394
      register int elemc;
395
      for (elemc = DCTSIZE; elemc > 0; elemc--)
396
        *workspaceptr++ = (*elemptr++) - _CENTERJSAMPLE;
397
    }
398
#endif
399
0
  }
400
0
}
Unexecuted instantiation: jcdctmgr-8.c:convsamp
Unexecuted instantiation: jcdctmgr-12.c:convsamp
401
402
403
/*
404
 * Quantize/descale the coefficients, and store into coef_blocks[].
405
 */
406
407
METHODDEF(void)
408
quantize(JCOEFPTR coef_block, DCTELEM *divisors, DCTELEM *workspace)
409
0
{
410
0
  int i;
411
0
  DCTELEM temp;
412
0
  JCOEFPTR output_ptr = coef_block;
413
414
#if BITS_IN_JSAMPLE == 8
415
416
  UDCTELEM recip, corr;
417
  int shift;
418
  UDCTELEM2 product;
419
420
0
  for (i = 0; i < DCTSIZE2; i++) {
421
0
    temp = workspace[i];
422
0
    recip = divisors[i + DCTSIZE2 * 0];
423
0
    corr =  divisors[i + DCTSIZE2 * 1];
424
0
    shift = divisors[i + DCTSIZE2 * 3];
425
426
0
    if (temp < 0) {
427
0
      temp = -temp;
428
0
      product = (UDCTELEM2)(temp + corr) * recip;
429
0
      product >>= shift + sizeof(DCTELEM) * 8;
430
0
      temp = (DCTELEM)product;
431
0
      temp = -temp;
432
0
    } else {
433
0
      product = (UDCTELEM2)(temp + corr) * recip;
434
0
      product >>= shift + sizeof(DCTELEM) * 8;
435
0
      temp = (DCTELEM)product;
436
0
    }
437
0
    output_ptr[i] = (JCOEF)temp;
438
0
  }
439
440
#else
441
442
  register DCTELEM qval;
443
444
0
  for (i = 0; i < DCTSIZE2; i++) {
445
0
    qval = divisors[i];
446
0
    temp = workspace[i];
447
    /* Divide the coefficient value by qval, ensuring proper rounding.
448
     * Since C does not specify the direction of rounding for negative
449
     * quotients, we have to force the dividend positive for portability.
450
     *
451
     * In most files, at least half of the output values will be zero
452
     * (at default quantization settings, more like three-quarters...)
453
     * so we should ensure that this case is fast.  On many machines,
454
     * a comparison is enough cheaper than a divide to make a special test
455
     * a win.  Since both inputs will be nonnegative, we need only test
456
     * for a < b to discover whether a/b is 0.
457
     * If your machine's division is fast enough, define FAST_DIVIDE.
458
     */
459
#ifdef FAST_DIVIDE
460
#define DIVIDE_BY(a, b)  a /= b
461
#else
462
0
#define DIVIDE_BY(a, b)  if (a >= b) a /= b;  else a = 0
463
0
#endif
464
0
    if (temp < 0) {
465
0
      temp = -temp;
466
0
      temp += qval >> 1;        /* for rounding */
467
0
      DIVIDE_BY(temp, qval);
468
0
      temp = -temp;
469
0
    } else {
470
0
      temp += qval >> 1;        /* for rounding */
471
0
      DIVIDE_BY(temp, qval);
472
0
    }
473
0
    output_ptr[i] = (JCOEF)temp;
474
0
  }
475
476
#endif
477
478
0
}
Unexecuted instantiation: jcdctmgr-8.c:quantize
Unexecuted instantiation: jcdctmgr-12.c:quantize
479
480
481
/*
482
 * Perform forward DCT on one or more blocks of a component.
483
 *
484
 * The input samples are taken from the sample_data[] array starting at
485
 * position start_row/start_col, and moving to the right for any additional
486
 * blocks. The quantized coefficients are returned in coef_blocks[].
487
 */
488
489
METHODDEF(void)
490
forward_DCT(j_compress_ptr cinfo, jpeg_component_info *compptr,
491
            _JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
492
            JDIMENSION start_row, JDIMENSION start_col, JDIMENSION num_blocks)
493
/* This version is used for integer DCT implementations. */
494
0
{
495
  /* This routine is heavily used, so it's worth coding it tightly. */
496
0
  my_fdct_ptr fdct = (my_fdct_ptr)cinfo->fdct;
497
0
  DCTELEM *divisors = fdct->divisors[compptr->quant_tbl_no];
498
0
  DCTELEM *workspace;
499
0
  JDIMENSION bi;
500
501
  /* Make sure the compiler doesn't look up these every pass */
502
0
  forward_DCT_method_ptr do_dct = fdct->dct;
503
0
  convsamp_method_ptr do_convsamp = fdct->convsamp;
504
0
  quantize_method_ptr do_quantize = fdct->quantize;
505
0
  workspace = fdct->workspace;
506
507
0
  sample_data += start_row;     /* fold in the vertical offset once */
508
509
0
  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
510
    /* Load data into workspace, applying unsigned->signed conversion */
511
#ifdef WITH_PROFILE
512
    cinfo->master->start = getTime();
513
#endif
514
0
    (*do_convsamp) (sample_data, start_col, workspace);
515
#ifdef WITH_PROFILE
516
    cinfo->master->convsamp_elapsed += getTime() - cinfo->master->start;
517
    cinfo->master->convsamp_msamples += (double)DCTSIZE2 / 1000000.;
518
#endif
519
520
    /* Perform the DCT */
521
#ifdef WITH_PROFILE
522
    cinfo->master->start = getTime();
523
#endif
524
0
    (*do_dct) (workspace);
525
#ifdef WITH_PROFILE
526
    cinfo->master->fdct_elapsed += getTime() - cinfo->master->start;
527
    cinfo->master->fdct_mcoeffs += (double)DCTSIZE2 / 1000000.;
528
#endif
529
530
    /* Quantize/descale the coefficients, and store into coef_blocks[] */
531
#ifdef WITH_PROFILE
532
    cinfo->master->start = getTime();
533
#endif
534
0
    (*do_quantize) (coef_blocks[bi], divisors, workspace);
535
#ifdef WITH_PROFILE
536
    cinfo->master->quantize_elapsed += getTime() - cinfo->master->start;
537
    cinfo->master->quantize_mcoeffs += (double)DCTSIZE2 / 1000000.;
538
#endif
539
0
  }
540
0
}
Unexecuted instantiation: jcdctmgr-8.c:forward_DCT
Unexecuted instantiation: jcdctmgr-12.c:forward_DCT
541
542
543
#ifdef DCT_FLOAT_SUPPORTED
544
545
METHODDEF(void)
546
convsamp_float(_JSAMPARRAY sample_data, JDIMENSION start_col,
547
               FAST_FLOAT *workspace)
548
0
{
549
0
  register FAST_FLOAT *workspaceptr;
550
0
  register _JSAMPROW elemptr;
551
0
  register int elemr;
552
553
0
  workspaceptr = workspace;
554
0
  for (elemr = 0; elemr < DCTSIZE; elemr++) {
555
0
    elemptr = sample_data[elemr] + start_col;
556
0
#if DCTSIZE == 8                /* unroll the inner loop */
557
0
    *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - _CENTERJSAMPLE);
558
0
    *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - _CENTERJSAMPLE);
559
0
    *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - _CENTERJSAMPLE);
560
0
    *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - _CENTERJSAMPLE);
561
0
    *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - _CENTERJSAMPLE);
562
0
    *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - _CENTERJSAMPLE);
563
0
    *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - _CENTERJSAMPLE);
564
0
    *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - _CENTERJSAMPLE);
565
#else
566
    {
567
      register int elemc;
568
      for (elemc = DCTSIZE; elemc > 0; elemc--)
569
        *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - _CENTERJSAMPLE);
570
    }
571
#endif
572
0
  }
573
0
}
Unexecuted instantiation: jcdctmgr-8.c:convsamp_float
Unexecuted instantiation: jcdctmgr-12.c:convsamp_float
574
575
576
METHODDEF(void)
577
quantize_float(JCOEFPTR coef_block, FAST_FLOAT *divisors,
578
               FAST_FLOAT *workspace)
579
0
{
580
0
  register FAST_FLOAT temp;
581
0
  register int i;
582
0
  register JCOEFPTR output_ptr = coef_block;
583
584
0
  for (i = 0; i < DCTSIZE2; i++) {
585
    /* Apply the quantization and scaling factor */
586
0
    temp = workspace[i] * divisors[i];
587
588
    /* Round to nearest integer.
589
     * Since C does not specify the direction of rounding for negative
590
     * quotients, we have to force the dividend positive for portability.
591
     * The maximum coefficient size is +-16K (for 12-bit data), so this
592
     * code should work for either 16-bit or 32-bit ints.
593
     */
594
0
    output_ptr[i] = (JCOEF)((int)(temp + (FAST_FLOAT)16384.5) - 16384);
595
0
  }
596
0
}
Unexecuted instantiation: jcdctmgr-8.c:quantize_float
Unexecuted instantiation: jcdctmgr-12.c:quantize_float
597
598
599
METHODDEF(void)
600
forward_DCT_float(j_compress_ptr cinfo, jpeg_component_info *compptr,
601
                  _JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
602
                  JDIMENSION start_row, JDIMENSION start_col,
603
                  JDIMENSION num_blocks)
604
/* This version is used for floating-point DCT implementations. */
605
0
{
606
  /* This routine is heavily used, so it's worth coding it tightly. */
607
0
  my_fdct_ptr fdct = (my_fdct_ptr)cinfo->fdct;
608
0
  FAST_FLOAT *divisors = fdct->float_divisors[compptr->quant_tbl_no];
609
0
  FAST_FLOAT *workspace;
610
0
  JDIMENSION bi;
611
612
613
  /* Make sure the compiler doesn't look up these every pass */
614
0
  float_DCT_method_ptr do_dct = fdct->float_dct;
615
0
  float_convsamp_method_ptr do_convsamp = fdct->float_convsamp;
616
0
  float_quantize_method_ptr do_quantize = fdct->float_quantize;
617
0
  workspace = fdct->float_workspace;
618
619
0
  sample_data += start_row;     /* fold in the vertical offset once */
620
621
0
  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
622
    /* Load data into workspace, applying unsigned->signed conversion */
623
#ifdef WITH_PROFILE
624
    cinfo->master->start = getTime();
625
#endif
626
0
    (*do_convsamp) (sample_data, start_col, workspace);
627
#ifdef WITH_PROFILE
628
    cinfo->master->convsamp_elapsed += getTime() - cinfo->master->start;
629
    cinfo->master->convsamp_msamples += (double)DCTSIZE2 / 1000000.;
630
#endif
631
632
    /* Perform the DCT */
633
#ifdef WITH_PROFILE
634
    cinfo->master->start = getTime();
635
#endif
636
0
    (*do_dct) (workspace);
637
#ifdef WITH_PROFILE
638
    cinfo->master->fdct_elapsed += getTime() - cinfo->master->start;
639
    cinfo->master->fdct_mcoeffs += (double)DCTSIZE2 / 1000000.;
640
#endif
641
642
    /* Quantize/descale the coefficients, and store into coef_blocks[] */
643
#ifdef WITH_PROFILE
644
    cinfo->master->start = getTime();
645
#endif
646
0
    (*do_quantize) (coef_blocks[bi], divisors, workspace);
647
#ifdef WITH_PROFILE
648
    cinfo->master->quantize_elapsed += getTime() - cinfo->master->start;
649
    cinfo->master->quantize_mcoeffs += (double)DCTSIZE2 / 1000000.;
650
#endif
651
0
  }
652
0
}
Unexecuted instantiation: jcdctmgr-8.c:forward_DCT_float
Unexecuted instantiation: jcdctmgr-12.c:forward_DCT_float
653
654
#endif /* DCT_FLOAT_SUPPORTED */
655
656
657
/*
658
 * Initialize FDCT manager.
659
 */
660
661
GLOBAL(void)
662
_jinit_forward_dct(j_compress_ptr cinfo)
663
0
{
664
0
  my_fdct_ptr fdct;
665
0
  int i;
666
667
0
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
668
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
669
670
0
  fdct = (my_fdct_ptr)
671
0
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
672
0
                                sizeof(my_fdct_controller));
673
0
  cinfo->fdct = (struct jpeg_forward_dct *)fdct;
674
0
  fdct->pub.start_pass = start_pass_fdctmgr;
675
676
  /* First determine the DCT... */
677
0
  switch (cinfo->dct_method) {
678
0
#ifdef DCT_ISLOW_SUPPORTED
679
0
  case JDCT_ISLOW:
680
0
    fdct->pub._forward_DCT = forward_DCT;
681
#ifdef WITH_SIMD
682
0
    if (!jsimd_set_fdct_islow(cinfo, &fdct->dct))
683
0
#endif
684
0
      fdct->dct = _jpeg_fdct_islow;
685
0
    break;
686
0
#endif
687
0
#ifdef DCT_IFAST_SUPPORTED
688
0
  case JDCT_IFAST:
689
0
    fdct->pub._forward_DCT = forward_DCT;
690
#ifdef WITH_SIMD
691
0
    if (!jsimd_set_fdct_ifast(cinfo, &fdct->dct))
692
0
#endif
693
0
      fdct->dct = _jpeg_fdct_ifast;
694
0
    break;
695
0
#endif
696
0
#ifdef DCT_FLOAT_SUPPORTED
697
0
  case JDCT_FLOAT:
698
0
    fdct->pub._forward_DCT = forward_DCT_float;
699
#ifdef WITH_SIMD
700
0
    if (!jsimd_set_fdct_float(cinfo, &fdct->float_dct))
701
0
#endif
702
0
      fdct->float_dct = jpeg_fdct_float;
703
0
    break;
704
0
#endif
705
0
  default:
706
0
    ERREXIT(cinfo, JERR_NOT_COMPILED);
707
0
    break;
708
0
  }
709
710
  /* ...then the supporting stages. */
711
0
  switch (cinfo->dct_method) {
712
0
#ifdef DCT_ISLOW_SUPPORTED
713
0
  case JDCT_ISLOW:
714
0
#endif
715
0
#ifdef DCT_IFAST_SUPPORTED
716
0
  case JDCT_IFAST:
717
0
#endif
718
0
#if defined(DCT_ISLOW_SUPPORTED) || defined(DCT_IFAST_SUPPORTED)
719
#ifdef WITH_SIMD
720
0
    if (!jsimd_set_convsamp(cinfo, &fdct->convsamp))
721
0
#endif
722
0
      fdct->convsamp = convsamp;
723
#ifdef WITH_SIMD
724
0
    if (!jsimd_set_quantize(cinfo, &fdct->quantize))
725
0
#endif
726
0
      fdct->quantize = quantize;
727
0
    break;
728
0
#endif
729
0
#ifdef DCT_FLOAT_SUPPORTED
730
0
  case JDCT_FLOAT:
731
#ifdef WITH_SIMD
732
0
    if (!jsimd_set_convsamp_float(cinfo, &fdct->float_convsamp))
733
0
#endif
734
0
      fdct->float_convsamp = convsamp_float;
735
#ifdef WITH_SIMD
736
0
    if (!jsimd_set_quantize_float(cinfo, &fdct->float_quantize))
737
0
#endif
738
0
      fdct->float_quantize = quantize_float;
739
0
    break;
740
0
#endif
741
0
  default:
742
0
    ERREXIT(cinfo, JERR_NOT_COMPILED);
743
0
    break;
744
0
  }
745
746
  /* Allocate workspace memory */
747
0
#ifdef DCT_FLOAT_SUPPORTED
748
0
  if (cinfo->dct_method == JDCT_FLOAT)
749
0
    fdct->float_workspace = (FAST_FLOAT *)
750
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
751
0
                                  sizeof(FAST_FLOAT) * DCTSIZE2);
752
0
  else
753
0
#endif
754
0
    fdct->workspace = (DCTELEM *)
755
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
756
0
                                  sizeof(DCTELEM) * DCTSIZE2);
757
758
  /* Mark divisor tables unallocated */
759
0
  for (i = 0; i < NUM_QUANT_TBLS; i++) {
760
0
    fdct->divisors[i] = NULL;
761
0
#ifdef DCT_FLOAT_SUPPORTED
762
    fdct->float_divisors[i] = NULL;
763
0
#endif
764
0
  }
765
0
}
Unexecuted instantiation: jinit_forward_dct
Unexecuted instantiation: j12init_forward_dct