Coverage Report

Created: 2023-06-07 06:03

/src/libjpeg-turbo.2.1.x/jccoefct.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jccoefct.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1994-1997, Thomas G. Lane.
6
 * It was modified by The libjpeg-turbo Project to include only code and
7
 * information relevant to libjpeg-turbo.
8
 * For conditions of distribution and use, see the accompanying README.ijg
9
 * file.
10
 *
11
 * This file contains the coefficient buffer controller for compression.
12
 * This controller is the top level of the JPEG compressor proper.
13
 * The coefficient buffer lies between forward-DCT and entropy encoding steps.
14
 */
15
16
#define JPEG_INTERNALS
17
#include "jinclude.h"
18
#include "jpeglib.h"
19
20
21
/* We use a full-image coefficient buffer when doing Huffman optimization,
22
 * and also for writing multiple-scan JPEG files.  In all cases, the DCT
23
 * step is run during the first pass, and subsequent passes need only read
24
 * the buffered coefficients.
25
 */
26
#ifdef ENTROPY_OPT_SUPPORTED
27
#define FULL_COEF_BUFFER_SUPPORTED
28
#else
29
#ifdef C_MULTISCAN_FILES_SUPPORTED
30
#define FULL_COEF_BUFFER_SUPPORTED
31
#endif
32
#endif
33
34
35
/* Private buffer controller object */
36
37
typedef struct {
38
  struct jpeg_c_coef_controller pub; /* public fields */
39
40
  JDIMENSION iMCU_row_num;      /* iMCU row # within image */
41
  JDIMENSION mcu_ctr;           /* counts MCUs processed in current row */
42
  int MCU_vert_offset;          /* counts MCU rows within iMCU row */
43
  int MCU_rows_per_iMCU_row;    /* number of such rows needed */
44
45
  /* For single-pass compression, it's sufficient to buffer just one MCU
46
   * (although this may prove a bit slow in practice).  We allocate a
47
   * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
48
   * MCU constructed and sent.  In multi-pass modes, this array points to the
49
   * current MCU's blocks within the virtual arrays.
50
   */
51
  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
52
53
  /* In multi-pass modes, we need a virtual block array for each component. */
54
  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
55
} my_coef_controller;
56
57
typedef my_coef_controller *my_coef_ptr;
58
59
60
/* Forward declarations */
61
METHODDEF(boolean) compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf);
62
#ifdef FULL_COEF_BUFFER_SUPPORTED
63
METHODDEF(boolean) compress_first_pass(j_compress_ptr cinfo,
64
                                       JSAMPIMAGE input_buf);
65
METHODDEF(boolean) compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf);
66
#endif
67
68
69
LOCAL(void)
70
start_iMCU_row(j_compress_ptr cinfo)
71
/* Reset within-iMCU-row counters for a new row */
72
8.52M
{
73
8.52M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
74
75
  /* In an interleaved scan, an MCU row is the same as an iMCU row.
76
   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
77
   * But at the bottom of the image, process only what's left.
78
   */
79
8.52M
  if (cinfo->comps_in_scan > 1) {
80
2.13M
    coef->MCU_rows_per_iMCU_row = 1;
81
6.38M
  } else {
82
6.38M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
83
6.35M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
84
34.4k
    else
85
34.4k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
86
6.38M
  }
87
88
8.52M
  coef->mcu_ctr = 0;
89
8.52M
  coef->MCU_vert_offset = 0;
90
8.52M
}
91
92
93
/*
94
 * Initialize for a processing pass.
95
 */
96
97
METHODDEF(void)
98
start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
99
23.9k
{
100
23.9k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
101
102
23.9k
  coef->iMCU_row_num = 0;
103
23.9k
  start_iMCU_row(cinfo);
104
105
23.9k
  switch (pass_mode) {
106
5.61k
  case JBUF_PASS_THRU:
107
5.61k
    if (coef->whole_image[0] != NULL)
108
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
109
5.61k
    coef->pub.compress_data = compress_data;
110
5.61k
    break;
111
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
112
965
  case JBUF_SAVE_AND_PASS:
113
965
    if (coef->whole_image[0] == NULL)
114
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
115
965
    coef->pub.compress_data = compress_first_pass;
116
965
    break;
117
17.3k
  case JBUF_CRANK_DEST:
118
17.3k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
17.3k
    coef->pub.compress_data = compress_output;
121
17.3k
    break;
122
0
#endif
123
0
  default:
124
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
0
    break;
126
23.9k
  }
127
23.9k
}
128
129
130
/*
131
 * Process some data in the single-pass case.
132
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
133
 * per call, ie, v_samp_factor block rows for each component in the image.
134
 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
135
 *
136
 * NB: input_buf contains a plane for each component in image,
137
 * which we index according to the component's SOF position.
138
 */
139
140
METHODDEF(boolean)
141
compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
142
1.76M
{
143
1.76M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
144
1.76M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
145
1.76M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
146
1.76M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
147
1.76M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
148
1.76M
  JDIMENSION ypos, xpos;
149
1.76M
  jpeg_component_info *compptr;
150
151
  /* Loop to write as much as one whole iMCU row */
152
3.52M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
153
1.76M
       yoffset++) {
154
4.39M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
155
2.63M
         MCU_col_num++) {
156
      /* Determine where data comes from in input_buf and do the DCT thing.
157
       * Each call on forward_DCT processes a horizontal row of DCT blocks
158
       * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
159
       * sequentially.  Dummy blocks at the right or bottom edge are filled in
160
       * specially.  The data in them does not matter for image reconstruction,
161
       * so we fill them with values that will encode to the smallest amount of
162
       * data, viz: all zeroes in the AC entries, DC entries equal to previous
163
       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
164
       */
165
2.63M
      blkn = 0;
166
8.82M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
167
6.19M
        compptr = cinfo->cur_comp_info[ci];
168
6.19M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
169
6.19M
                                                  compptr->last_col_width;
170
6.19M
        xpos = MCU_col_num * compptr->MCU_sample_width;
171
6.19M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
172
13.3M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
173
7.18M
          if (coef->iMCU_row_num < last_iMCU_row ||
174
7.18M
              yoffset + yindex < compptr->last_row_height) {
175
6.77M
            (*cinfo->fdct->forward_DCT) (cinfo, compptr,
176
6.77M
                                         input_buf[compptr->component_index],
177
6.77M
                                         coef->MCU_buffer[blkn],
178
6.77M
                                         ypos, xpos, (JDIMENSION)blockcnt);
179
6.77M
            if (blockcnt < compptr->MCU_width) {
180
              /* Create some dummy blocks at the right edge of the image. */
181
708k
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
182
708k
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
183
2.12M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
184
1.41M
                coef->MCU_buffer[blkn + bi][0][0] =
185
1.41M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
186
1.41M
              }
187
708k
            }
188
6.77M
          } else {
189
            /* Create a row of dummy blocks at the bottom of the image. */
190
413k
            jzero_far((void *)coef->MCU_buffer[blkn],
191
413k
                      compptr->MCU_width * sizeof(JBLOCK));
192
911k
            for (bi = 0; bi < compptr->MCU_width; bi++) {
193
497k
              coef->MCU_buffer[blkn + bi][0][0] =
194
497k
                coef->MCU_buffer[blkn - 1][0][0];
195
497k
            }
196
413k
          }
197
7.18M
          blkn += compptr->MCU_width;
198
7.18M
          ypos += DCTSIZE;
199
7.18M
        }
200
6.19M
      }
201
      /* Try to write the MCU.  In event of a suspension failure, we will
202
       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
203
       */
204
2.63M
      if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
205
        /* Suspension forced; update state counters and exit */
206
0
        coef->MCU_vert_offset = yoffset;
207
0
        coef->mcu_ctr = MCU_col_num;
208
0
        return FALSE;
209
0
      }
210
2.63M
    }
211
    /* Completed an MCU row, but perhaps not an iMCU row */
212
1.76M
    coef->mcu_ctr = 0;
213
1.76M
  }
214
  /* Completed the iMCU row, advance counters for next one */
215
1.76M
  coef->iMCU_row_num++;
216
1.76M
  start_iMCU_row(cinfo);
217
1.76M
  return TRUE;
218
1.76M
}
219
220
221
#ifdef FULL_COEF_BUFFER_SUPPORTED
222
223
/*
224
 * Process some data in the first pass of a multi-pass case.
225
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
226
 * per call, ie, v_samp_factor block rows for each component in the image.
227
 * This amount of data is read from the source buffer, DCT'd and quantized,
228
 * and saved into the virtual arrays.  We also generate suitable dummy blocks
229
 * as needed at the right and lower edges.  (The dummy blocks are constructed
230
 * in the virtual arrays, which have been padded appropriately.)  This makes
231
 * it possible for subsequent passes not to worry about real vs. dummy blocks.
232
 *
233
 * We must also emit the data to the entropy encoder.  This is conveniently
234
 * done by calling compress_output() after we've loaded the current strip
235
 * of the virtual arrays.
236
 *
237
 * NB: input_buf contains a plane for each component in image.  All
238
 * components are DCT'd and loaded into the virtual arrays in this pass.
239
 * However, it may be that only a subset of the components are emitted to
240
 * the entropy encoder during this first pass; be careful about looking
241
 * at the scan-dependent variables (MCU dimensions, etc).
242
 */
243
244
METHODDEF(boolean)
245
compress_first_pass(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
246
354k
{
247
354k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
248
354k
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
249
354k
  JDIMENSION blocks_across, MCUs_across, MCUindex;
250
354k
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
251
354k
  JCOEF lastDC;
252
354k
  jpeg_component_info *compptr;
253
354k
  JBLOCKARRAY buffer;
254
354k
  JBLOCKROW thisblockrow, lastblockrow;
255
256
1.41M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
257
1.06M
       ci++, compptr++) {
258
    /* Align the virtual buffer for this component. */
259
1.06M
    buffer = (*cinfo->mem->access_virt_barray)
260
1.06M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
261
1.06M
       coef->iMCU_row_num * compptr->v_samp_factor,
262
1.06M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
263
    /* Count non-dummy DCT block rows in this iMCU row. */
264
1.06M
    if (coef->iMCU_row_num < last_iMCU_row)
265
1.06M
      block_rows = compptr->v_samp_factor;
266
2.89k
    else {
267
      /* NB: can't use last_row_height here, since may not be set! */
268
2.89k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
269
2.89k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
270
2.89k
    }
271
1.06M
    blocks_across = compptr->width_in_blocks;
272
1.06M
    h_samp_factor = compptr->h_samp_factor;
273
    /* Count number of dummy blocks to be added at the right margin. */
274
1.06M
    ndummy = (int)(blocks_across % h_samp_factor);
275
1.06M
    if (ndummy > 0)
276
354k
      ndummy = h_samp_factor - ndummy;
277
    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
278
     * on forward_DCT processes a complete horizontal row of DCT blocks.
279
     */
280
2.12M
    for (block_row = 0; block_row < block_rows; block_row++) {
281
1.06M
      thisblockrow = buffer[block_row];
282
1.06M
      (*cinfo->fdct->forward_DCT) (cinfo, compptr,
283
1.06M
                                   input_buf[ci], thisblockrow,
284
1.06M
                                   (JDIMENSION)(block_row * DCTSIZE),
285
1.06M
                                   (JDIMENSION)0, blocks_across);
286
1.06M
      if (ndummy > 0) {
287
        /* Create dummy blocks at the right edge of the image. */
288
354k
        thisblockrow += blocks_across; /* => first dummy block */
289
354k
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
290
354k
        lastDC = thisblockrow[-1][0];
291
708k
        for (bi = 0; bi < ndummy; bi++) {
292
354k
          thisblockrow[bi][0] = lastDC;
293
354k
        }
294
354k
      }
295
1.06M
    }
296
    /* If at end of image, create dummy block rows as needed.
297
     * The tricky part here is that within each MCU, we want the DC values
298
     * of the dummy blocks to match the last real block's DC value.
299
     * This squeezes a few more bytes out of the resulting file...
300
     */
301
1.06M
    if (coef->iMCU_row_num == last_iMCU_row) {
302
2.89k
      blocks_across += ndummy;  /* include lower right corner */
303
2.89k
      MCUs_across = blocks_across / h_samp_factor;
304
2.89k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
305
2.89k
           block_row++) {
306
0
        thisblockrow = buffer[block_row];
307
0
        lastblockrow = buffer[block_row - 1];
308
0
        jzero_far((void *)thisblockrow,
309
0
                  (size_t)(blocks_across * sizeof(JBLOCK)));
310
0
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
311
0
          lastDC = lastblockrow[h_samp_factor - 1][0];
312
0
          for (bi = 0; bi < h_samp_factor; bi++) {
313
0
            thisblockrow[bi][0] = lastDC;
314
0
          }
315
0
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
316
0
          lastblockrow += h_samp_factor;
317
0
        }
318
0
      }
319
2.89k
    }
320
1.06M
  }
321
  /* NB: compress_output will increment iMCU_row_num if successful.
322
   * A suspension return will result in redoing all the work above next time.
323
   */
324
325
  /* Emit data to the entropy encoder, sharing code with subsequent passes */
326
354k
  return compress_output(cinfo, input_buf);
327
354k
}
328
329
330
/*
331
 * Process some data in subsequent passes of a multi-pass case.
332
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
333
 * per call, ie, v_samp_factor block rows for each component in the scan.
334
 * The data is obtained from the virtual arrays and fed to the entropy coder.
335
 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
336
 *
337
 * NB: input_buf is ignored; it is likely to be a NULL pointer.
338
 */
339
340
METHODDEF(boolean)
341
compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
342
6.73M
{
343
6.73M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
344
6.73M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
345
6.73M
  int blkn, ci, xindex, yindex, yoffset;
346
6.73M
  JDIMENSION start_col;
347
6.73M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
348
6.73M
  JBLOCKROW buffer_ptr;
349
6.73M
  jpeg_component_info *compptr;
350
351
  /* Align the virtual buffers for the components used in this scan.
352
   * NB: during first pass, this is safe only because the buffers will
353
   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
354
   */
355
15.5M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
356
8.86M
    compptr = cinfo->cur_comp_info[ci];
357
8.86M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
358
8.86M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
359
8.86M
       coef->iMCU_row_num * compptr->v_samp_factor,
360
8.86M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
361
8.86M
  }
362
363
  /* Loop to process one whole iMCU row */
364
13.4M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
365
6.73M
       yoffset++) {
366
16.2M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
367
9.50M
         MCU_col_num++) {
368
      /* Construct list of pointers to DCT blocks belonging to this MCU */
369
9.50M
      blkn = 0;                 /* index of current DCT block within MCU */
370
21.7M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
371
12.2M
        compptr = cinfo->cur_comp_info[ci];
372
12.2M
        start_col = MCU_col_num * compptr->MCU_width;
373
24.5M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
374
12.2M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
375
25.8M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
376
13.6M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
377
13.6M
          }
378
12.2M
        }
379
12.2M
      }
380
      /* Try to write the MCU. */
381
9.50M
      if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
382
        /* Suspension forced; update state counters and exit */
383
0
        coef->MCU_vert_offset = yoffset;
384
0
        coef->mcu_ctr = MCU_col_num;
385
0
        return FALSE;
386
0
      }
387
9.50M
    }
388
    /* Completed an MCU row, but perhaps not an iMCU row */
389
6.73M
    coef->mcu_ctr = 0;
390
6.73M
  }
391
  /* Completed the iMCU row, advance counters for next one */
392
6.73M
  coef->iMCU_row_num++;
393
6.73M
  start_iMCU_row(cinfo);
394
6.73M
  return TRUE;
395
6.73M
}
396
397
#endif /* FULL_COEF_BUFFER_SUPPORTED */
398
399
400
/*
401
 * Initialize coefficient buffer controller.
402
 */
403
404
GLOBAL(void)
405
jinit_c_coef_controller(j_compress_ptr cinfo, boolean need_full_buffer)
406
6.57k
{
407
6.57k
  my_coef_ptr coef;
408
409
6.57k
  coef = (my_coef_ptr)
410
6.57k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
411
6.57k
                                sizeof(my_coef_controller));
412
6.57k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
413
6.57k
  coef->pub.start_pass = start_pass_coef;
414
415
  /* Create the coefficient buffer. */
416
6.57k
  if (need_full_buffer) {
417
965
#ifdef FULL_COEF_BUFFER_SUPPORTED
418
    /* Allocate a full-image virtual array for each component, */
419
    /* padded to a multiple of samp_factor DCT blocks in each direction. */
420
965
    int ci;
421
965
    jpeg_component_info *compptr;
422
423
3.86k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
424
2.89k
         ci++, compptr++) {
425
2.89k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
426
2.89k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
427
2.89k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
428
2.89k
                               (long)compptr->h_samp_factor),
429
2.89k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
430
2.89k
                               (long)compptr->v_samp_factor),
431
2.89k
         (JDIMENSION)compptr->v_samp_factor);
432
2.89k
    }
433
#else
434
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
435
#endif
436
5.61k
  } else {
437
    /* We only need a single-MCU buffer. */
438
5.61k
    JBLOCKROW buffer;
439
5.61k
    int i;
440
441
5.61k
    buffer = (JBLOCKROW)
442
5.61k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
5.61k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
444
61.7k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
445
56.1k
      coef->MCU_buffer[i] = buffer + i;
446
56.1k
    }
447
5.61k
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
448
5.61k
  }
449
6.57k
}