Coverage Report

Created: 2025-10-13 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/src/jddctmgr.c
Line
Count
Source
1
/*
2
 * jddctmgr.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1994-1996, Thomas G. Lane.
6
 * Modified 2002-2010 by Guido Vollbeding.
7
 * libjpeg-turbo Modifications:
8
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
 * Copyright (C) 2010, 2015, 2022, 2025, D. R. Commander.
10
 * Copyright (C) 2013, MIPS Technologies, Inc., California.
11
 * For conditions of distribution and use, see the accompanying README.ijg
12
 * file.
13
 *
14
 * This file contains the inverse-DCT management logic.
15
 * This code selects a particular IDCT implementation to be used,
16
 * and it performs related housekeeping chores.  No code in this file
17
 * is executed per IDCT step, only during output pass setup.
18
 *
19
 * Note that the IDCT routines are responsible for performing coefficient
20
 * dequantization as well as the IDCT proper.  This module sets up the
21
 * dequantization multiplier table needed by the IDCT routine.
22
 */
23
24
#define JPEG_INTERNALS
25
#include "jinclude.h"
26
#include "jpeglib.h"
27
#include "jdct.h"               /* Private declarations for DCT subsystem */
28
#ifdef WITH_SIMD
29
#include "../simd/jsimddct.h"
30
#endif
31
#include "jpegapicomp.h"
32
33
34
/*
35
 * The decompressor input side (jdinput.c) saves away the appropriate
36
 * quantization table for each component at the start of the first scan
37
 * involving that component.  (This is necessary in order to correctly
38
 * decode files that reuse Q-table slots.)
39
 * When we are ready to make an output pass, the saved Q-table is converted
40
 * to a multiplier table that will actually be used by the IDCT routine.
41
 * The multiplier table contents are IDCT-method-dependent.  To support
42
 * application changes in IDCT method between scans, we can remake the
43
 * multiplier tables if necessary.
44
 * In buffered-image mode, the first output pass may occur before any data
45
 * has been seen for some components, and thus before their Q-tables have
46
 * been saved away.  To handle this case, multiplier tables are preset
47
 * to zeroes; the result of the IDCT will be a neutral gray level.
48
 */
49
50
51
/* Private subobject for this module */
52
53
typedef struct {
54
  struct jpeg_inverse_dct pub;  /* public fields */
55
56
  /* This array contains the IDCT method code that each multiplier table
57
   * is currently set up for, or -1 if it's not yet set up.
58
   * The actual multiplier tables are pointed to by dct_table in the
59
   * per-component comp_info structures.
60
   */
61
  int cur_method[MAX_COMPONENTS];
62
} my_idct_controller;
63
64
typedef my_idct_controller *my_idct_ptr;
65
66
67
/* Allocated multiplier tables: big enough for any supported variant */
68
69
typedef union {
70
  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
71
#ifdef DCT_IFAST_SUPPORTED
72
  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
73
#endif
74
#ifdef DCT_FLOAT_SUPPORTED
75
  FLOAT_MULT_TYPE float_array[DCTSIZE2];
76
#endif
77
} multiplier_table;
78
79
80
/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
81
 * so be sure to compile that code if either ISLOW or SCALING is requested.
82
 */
83
#ifdef DCT_ISLOW_SUPPORTED
84
#define PROVIDE_ISLOW_TABLES
85
#else
86
#ifdef IDCT_SCALING_SUPPORTED
87
#define PROVIDE_ISLOW_TABLES
88
#endif
89
#endif
90
91
92
/*
93
 * Prepare for an output pass.
94
 * Here we select the proper IDCT routine for each component and build
95
 * a matching multiplier table.
96
 */
97
98
METHODDEF(void)
99
start_pass(j_decompress_ptr cinfo)
100
0
{
101
0
  my_idct_ptr idct = (my_idct_ptr)cinfo->idct;
102
0
  int ci, i;
103
0
  jpeg_component_info *compptr;
104
0
  int method = 0;
105
0
  _inverse_DCT_method_ptr method_ptr = NULL;
106
0
  JQUANT_TBL *qtbl;
107
108
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
109
0
       ci++, compptr++) {
110
    /* Select the proper IDCT routine for this component's scaling */
111
0
    switch (compptr->_DCT_scaled_size) {
112
0
#ifdef IDCT_SCALING_SUPPORTED
113
0
    case 1:
114
0
      method_ptr = _jpeg_idct_1x1;
115
0
      method = JDCT_ISLOW;      /* jidctred uses islow-style table */
116
0
      break;
117
0
    case 2:
118
#ifdef WITH_SIMD
119
0
      if (jsimd_set_idct_2x2(cinfo))
120
0
        method_ptr = jsimd_idct_2x2;
121
0
      else
122
0
#endif
123
0
        method_ptr = _jpeg_idct_2x2;
124
0
      method = JDCT_ISLOW;      /* jidctred uses islow-style table */
125
0
      break;
126
0
    case 3:
127
0
      method_ptr = _jpeg_idct_3x3;
128
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
129
0
      break;
130
0
    case 4:
131
#ifdef WITH_SIMD
132
0
      if (jsimd_set_idct_4x4(cinfo))
133
0
        method_ptr = jsimd_idct_4x4;
134
0
      else
135
0
#endif
136
0
        method_ptr = _jpeg_idct_4x4;
137
0
      method = JDCT_ISLOW;      /* jidctred uses islow-style table */
138
0
      break;
139
0
    case 5:
140
0
      method_ptr = _jpeg_idct_5x5;
141
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
142
0
      break;
143
0
    case 6:
144
#if defined(WITH_SIMD) && SIMD_ARCHITECTURE == MIPS
145
      if (jsimd_set_idct_6x6(cinfo))
146
        method_ptr = jsimd_idct_6x6;
147
      else
148
#endif
149
0
        method_ptr = _jpeg_idct_6x6;
150
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
151
0
      break;
152
0
    case 7:
153
0
      method_ptr = _jpeg_idct_7x7;
154
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
155
0
      break;
156
0
#endif
157
0
    case DCTSIZE:
158
0
      switch (cinfo->dct_method) {
159
0
#ifdef DCT_ISLOW_SUPPORTED
160
0
      case JDCT_ISLOW:
161
#ifdef WITH_SIMD
162
0
        if (jsimd_set_idct_islow(cinfo))
163
0
          method_ptr = jsimd_idct_islow;
164
0
        else
165
0
#endif
166
0
          method_ptr = _jpeg_idct_islow;
167
0
        method = JDCT_ISLOW;
168
0
        break;
169
0
#endif
170
0
#ifdef DCT_IFAST_SUPPORTED
171
0
      case JDCT_IFAST:
172
#ifdef WITH_SIMD
173
0
        if (jsimd_set_idct_ifast(cinfo))
174
0
          method_ptr = jsimd_idct_ifast;
175
0
        else
176
0
#endif
177
0
          method_ptr = _jpeg_idct_ifast;
178
0
        method = JDCT_IFAST;
179
0
        break;
180
0
#endif
181
0
#ifdef DCT_FLOAT_SUPPORTED
182
0
      case JDCT_FLOAT:
183
#ifdef WITH_SIMD
184
0
        if (jsimd_set_idct_float(cinfo))
185
0
          method_ptr = jsimd_idct_float;
186
0
        else
187
0
#endif
188
0
          method_ptr = _jpeg_idct_float;
189
0
        method = JDCT_FLOAT;
190
0
        break;
191
0
#endif
192
0
      default:
193
0
        ERREXIT(cinfo, JERR_NOT_COMPILED);
194
0
        break;
195
0
      }
196
0
      break;
197
0
#ifdef IDCT_SCALING_SUPPORTED
198
0
    case 9:
199
0
      method_ptr = _jpeg_idct_9x9;
200
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
201
0
      break;
202
0
    case 10:
203
0
      method_ptr = _jpeg_idct_10x10;
204
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
205
0
      break;
206
0
    case 11:
207
0
      method_ptr = _jpeg_idct_11x11;
208
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
209
0
      break;
210
0
    case 12:
211
#if defined(WITH_SIMD) && SIMD_ARCHITECTURE == MIPS
212
      if (jsimd_set_idct_12x12(cinfo))
213
        method_ptr = jsimd_idct_12x12;
214
      else
215
#endif
216
0
        method_ptr = _jpeg_idct_12x12;
217
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
218
0
      break;
219
0
    case 13:
220
0
      method_ptr = _jpeg_idct_13x13;
221
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
222
0
      break;
223
0
    case 14:
224
0
      method_ptr = _jpeg_idct_14x14;
225
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
226
0
      break;
227
0
    case 15:
228
0
      method_ptr = _jpeg_idct_15x15;
229
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
230
0
      break;
231
0
    case 16:
232
0
      method_ptr = _jpeg_idct_16x16;
233
0
      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
234
0
      break;
235
0
#endif
236
0
    default:
237
0
      ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size);
238
0
      break;
239
0
    }
240
0
    idct->pub._inverse_DCT[ci] = method_ptr;
241
    /* Create multiplier table from quant table.
242
     * However, we can skip this if the component is uninteresting
243
     * or if we already built the table.  Also, if no quant table
244
     * has yet been saved for the component, we leave the
245
     * multiplier table all-zero; we'll be reading zeroes from the
246
     * coefficient controller's buffer anyway.
247
     */
248
0
    if (!compptr->component_needed || idct->cur_method[ci] == method)
249
0
      continue;
250
0
    qtbl = compptr->quant_table;
251
0
    if (qtbl == NULL)           /* happens if no data yet for component */
252
0
      continue;
253
0
    idct->cur_method[ci] = method;
254
0
    switch (method) {
255
0
#ifdef PROVIDE_ISLOW_TABLES
256
0
    case JDCT_ISLOW:
257
0
      {
258
        /* For LL&M IDCT method, multipliers are equal to raw quantization
259
         * coefficients, but are stored as ints to ensure access efficiency.
260
         */
261
0
        ISLOW_MULT_TYPE *ismtbl = (ISLOW_MULT_TYPE *)compptr->dct_table;
262
0
        for (i = 0; i < DCTSIZE2; i++) {
263
0
          ismtbl[i] = (ISLOW_MULT_TYPE)qtbl->quantval[i];
264
0
        }
265
0
      }
266
0
      break;
267
0
#endif
268
0
#ifdef DCT_IFAST_SUPPORTED
269
0
    case JDCT_IFAST:
270
0
      {
271
        /* For AA&N IDCT method, multipliers are equal to quantization
272
         * coefficients scaled by scalefactor[row]*scalefactor[col], where
273
         *   scalefactor[0] = 1
274
         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
275
         * For integer operation, the multiplier table is to be scaled by
276
         * IFAST_SCALE_BITS.
277
         */
278
0
        IFAST_MULT_TYPE *ifmtbl = (IFAST_MULT_TYPE *)compptr->dct_table;
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
        for (i = 0; i < DCTSIZE2; i++) {
294
0
          ifmtbl[i] = (IFAST_MULT_TYPE)
295
0
            DESCALE(MULTIPLY16V16((JLONG)qtbl->quantval[i],
296
0
                                  (JLONG)aanscales[i]),
297
0
                    CONST_BITS - IFAST_SCALE_BITS);
298
0
        }
299
0
      }
300
0
      break;
301
0
#endif
302
0
#ifdef DCT_FLOAT_SUPPORTED
303
0
    case JDCT_FLOAT:
304
0
      {
305
        /* For float AA&N IDCT method, multipliers are equal to quantization
306
         * coefficients scaled by scalefactor[row]*scalefactor[col], where
307
         *   scalefactor[0] = 1
308
         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
309
         */
310
0
        FLOAT_MULT_TYPE *fmtbl = (FLOAT_MULT_TYPE *)compptr->dct_table;
311
0
        int row, col;
312
0
        static const double aanscalefactor[DCTSIZE] = {
313
0
          1.0, 1.387039845, 1.306562965, 1.175875602,
314
0
          1.0, 0.785694958, 0.541196100, 0.275899379
315
0
        };
316
317
0
        i = 0;
318
0
        for (row = 0; row < DCTSIZE; row++) {
319
0
          for (col = 0; col < DCTSIZE; col++) {
320
0
            fmtbl[i] = (FLOAT_MULT_TYPE)
321
0
              ((double)qtbl->quantval[i] *
322
0
               aanscalefactor[row] * aanscalefactor[col]);
323
0
            i++;
324
0
          }
325
0
        }
326
0
      }
327
0
      break;
328
0
#endif
329
0
    default:
330
0
      ERREXIT(cinfo, JERR_NOT_COMPILED);
331
0
      break;
332
0
    }
333
0
  }
334
0
}
Unexecuted instantiation: jddctmgr-8.c:start_pass
Unexecuted instantiation: jddctmgr-12.c:start_pass
335
336
337
/*
338
 * Initialize IDCT manager.
339
 */
340
341
GLOBAL(void)
342
_jinit_inverse_dct(j_decompress_ptr cinfo)
343
0
{
344
0
  my_idct_ptr idct;
345
0
  int ci;
346
0
  jpeg_component_info *compptr;
347
348
0
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
349
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
350
351
0
  idct = (my_idct_ptr)
352
0
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
353
0
                                sizeof(my_idct_controller));
354
0
  cinfo->idct = (struct jpeg_inverse_dct *)idct;
355
0
  idct->pub.start_pass = start_pass;
356
357
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
358
0
       ci++, compptr++) {
359
    /* Allocate and pre-zero a multiplier table for each component */
360
0
    compptr->dct_table =
361
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
362
0
                                  sizeof(multiplier_table));
363
0
    memset(compptr->dct_table, 0, sizeof(multiplier_table));
364
    /* Mark multiplier table not yet set up for any method */
365
0
    idct->cur_method[ci] = -1;
366
0
  }
367
0
}
Unexecuted instantiation: jinit_inverse_dct
Unexecuted instantiation: j12init_inverse_dct