Coverage Report

Created: 2025-10-10 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/src/jcphuff.c
Line
Count
Source
1
/*
2
 * jcphuff.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1995-1997, Thomas G. Lane.
6
 * Lossless JPEG Modifications:
7
 * Copyright (C) 1999, Ken Murchison.
8
 * libjpeg-turbo Modifications:
9
 * Copyright (C) 2011, 2015, 2018, 2021-2022, 2024-2025, D. R. Commander.
10
 * Copyright (C) 2016, 2018, 2022, Matthieu Darbois.
11
 * Copyright (C) 2020, Arm Limited.
12
 * Copyright (C) 2021, Alex Richardson.
13
 * For conditions of distribution and use, see the accompanying README.ijg
14
 * file.
15
 *
16
 * This file contains Huffman entropy encoding routines for progressive JPEG.
17
 *
18
 * We do not support output suspension in this module, since the library
19
 * currently does not allow multiple-scan files to be written with output
20
 * suspension.
21
 */
22
23
#define JPEG_INTERNALS
24
#include "jinclude.h"
25
#include "jpeglib.h"
26
#ifdef WITH_SIMD
27
#include "../simd/jsimd.h"
28
#endif
29
#include "jchuff.h"             /* Declarations shared with jc*huff.c */
30
#include <limits.h>
31
32
#ifdef HAVE_INTRIN_H
33
#include <intrin.h>
34
#ifdef _MSC_VER
35
#ifdef HAVE_BITSCANFORWARD64
36
#pragma intrinsic(_BitScanForward64)
37
#endif
38
#ifdef HAVE_BITSCANFORWARD
39
#pragma intrinsic(_BitScanForward)
40
#endif
41
#endif
42
#endif
43
44
#ifdef C_PROGRESSIVE_SUPPORTED
45
46
#include "jpeg_nbits.h"
47
48
49
/* Expanded entropy encoder object for progressive Huffman encoding. */
50
51
typedef struct {
52
  struct jpeg_entropy_encoder pub; /* public fields */
53
54
  /* Pointer to routine to prepare data for encode_mcu_AC_first() */
55
  void (*AC_first_prepare) (const JCOEF *block,
56
                            const int *jpeg_natural_order_start, int Sl,
57
                            int Al, UJCOEF *values, size_t *zerobits);
58
  /* Pointer to routine to prepare data for encode_mcu_AC_refine() */
59
  int (*AC_refine_prepare) (const JCOEF *block,
60
                            const int *jpeg_natural_order_start, int Sl,
61
                            int Al, UJCOEF *absvalues, size_t *bits);
62
63
  /* Mode flag: TRUE for optimization, FALSE for actual data output */
64
  boolean gather_statistics;
65
66
  /* Bit-level coding status.
67
   * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
68
   */
69
  JOCTET *next_output_byte;     /* => next byte to write in buffer */
70
  size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
71
  size_t put_buffer;            /* current bit-accumulation buffer */
72
  int put_bits;                 /* # of bits now in it */
73
  j_compress_ptr cinfo;         /* link to cinfo (needed for dump_buffer) */
74
75
  /* Coding status for DC components */
76
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
77
78
  /* Coding status for AC components */
79
  int ac_tbl_no;                /* the table number of the single component */
80
  unsigned int EOBRUN;          /* run length of EOBs */
81
  unsigned int BE;              /* # of buffered correction bits before MCU */
82
  char *bit_buffer;             /* buffer for correction bits (1 per char) */
83
  /* packing correction bits tightly would save some space but cost time... */
84
85
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
86
  int next_restart_num;         /* next restart number to write (0-7) */
87
88
  /* Pointers to derived tables (these workspaces have image lifespan).
89
   * Since any one scan codes only DC or only AC, we only need one set
90
   * of tables, not one for DC and one for AC.
91
   */
92
  c_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
93
94
  /* Statistics tables for optimization; again, one set is enough */
95
  long *count_ptrs[NUM_HUFF_TBLS];
96
} phuff_entropy_encoder;
97
98
typedef phuff_entropy_encoder *phuff_entropy_ptr;
99
100
/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
101
 * buffer can hold.  Larger sizes may slightly improve compression, but
102
 * 1000 is already well into the realm of overkill.
103
 * The minimum safe size is 64 bits.
104
 */
105
106
402M
#define MAX_CORR_BITS  1000     /* Max # of correction bits I can buffer */
107
108
/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.
109
 * We assume that int right shift is unsigned if JLONG right shift is,
110
 * which should be safe.
111
 */
112
113
#ifdef RIGHT_SHIFT_IS_UNSIGNED
114
#define ISHIFT_TEMPS    int ishift_temp;
115
#define IRIGHT_SHIFT(x, shft) \
116
  ((ishift_temp = (x)) < 0 ? \
117
   (ishift_temp >> (shft)) | ((~0) << (16 - (shft))) : \
118
   (ishift_temp >> (shft)))
119
#else
120
#define ISHIFT_TEMPS
121
269M
#define IRIGHT_SHIFT(x, shft)   ((x) >> (shft))
122
#endif
123
124
815M
#define PAD(v, p)  ((v + (p) - 1) & (~((p) - 1)))
125
126
/* Forward declarations */
127
METHODDEF(boolean) encode_mcu_DC_first(j_compress_ptr cinfo,
128
                                       JBLOCKROW *MCU_data);
129
METHODDEF(void) encode_mcu_AC_first_prepare
130
  (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
131
   UJCOEF *values, size_t *zerobits);
132
METHODDEF(boolean) encode_mcu_AC_first(j_compress_ptr cinfo,
133
                                       JBLOCKROW *MCU_data);
134
METHODDEF(boolean) encode_mcu_DC_refine(j_compress_ptr cinfo,
135
                                        JBLOCKROW *MCU_data);
136
METHODDEF(int) encode_mcu_AC_refine_prepare
137
  (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
138
   UJCOEF *absvalues, size_t *bits);
139
METHODDEF(boolean) encode_mcu_AC_refine(j_compress_ptr cinfo,
140
                                        JBLOCKROW *MCU_data);
141
METHODDEF(void) finish_pass_phuff(j_compress_ptr cinfo);
142
METHODDEF(void) finish_pass_gather_phuff(j_compress_ptr cinfo);
143
144
145
/* Count bit loop zeroes */
146
INLINE
147
METHODDEF(int)
148
count_zeroes(size_t *x)
149
1.29G
{
150
1.29G
#if defined(HAVE_BUILTIN_CTZL)
151
1.29G
  int result;
152
1.29G
  result = __builtin_ctzl(*x);
153
1.29G
  *x >>= result;
154
#elif defined(HAVE_BITSCANFORWARD64)
155
  unsigned long result;
156
  _BitScanForward64(&result, *x);
157
  *x >>= result;
158
#elif defined(HAVE_BITSCANFORWARD)
159
  unsigned long result;
160
  _BitScanForward(&result, *x);
161
  *x >>= result;
162
#else
163
  int result = 0;
164
  while ((*x & 1) == 0) {
165
    ++result;
166
    *x >>= 1;
167
  }
168
#endif
169
1.29G
  return (int)result;
170
1.29G
}
171
172
173
/*
174
 * Initialize for a Huffman-compressed scan using progressive JPEG.
175
 */
176
177
METHODDEF(void)
178
start_pass_phuff(j_compress_ptr cinfo, boolean gather_statistics)
179
152k
{
180
152k
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
181
152k
  boolean is_DC_band;
182
152k
  int ci, tbl;
183
152k
  jpeg_component_info *compptr;
184
185
152k
  entropy->cinfo = cinfo;
186
152k
  entropy->gather_statistics = gather_statistics;
187
188
152k
  is_DC_band = (cinfo->Ss == 0);
189
190
  /* We assume jcmaster.c already validated the scan parameters. */
191
192
  /* Select execution routines */
193
152k
  if (cinfo->Ah == 0) {
194
81.7k
    if (is_DC_band)
195
18.6k
      entropy->pub.encode_mcu = encode_mcu_DC_first;
196
63.0k
    else
197
63.0k
      entropy->pub.encode_mcu = encode_mcu_AC_first;
198
81.7k
#ifdef WITH_SIMD
199
81.7k
    if (!jsimd_set_encode_mcu_AC_first_prepare(cinfo,
200
81.7k
                                               &entropy->AC_first_prepare))
201
0
#endif
202
0
      entropy->AC_first_prepare = encode_mcu_AC_first_prepare;
203
81.7k
  } else {
204
71.1k
    if (is_DC_band)
205
8.77k
      entropy->pub.encode_mcu = encode_mcu_DC_refine;
206
62.3k
    else {
207
62.3k
      entropy->pub.encode_mcu = encode_mcu_AC_refine;
208
62.3k
#ifdef WITH_SIMD
209
62.3k
      if (!jsimd_set_encode_mcu_AC_refine_prepare(cinfo,
210
62.3k
                                                  &entropy->AC_refine_prepare))
211
0
#endif
212
0
        entropy->AC_refine_prepare = encode_mcu_AC_refine_prepare;
213
      /* AC refinement needs a correction bit buffer */
214
62.3k
      if (entropy->bit_buffer == NULL)
215
8.75k
        entropy->bit_buffer = (char *)
216
8.75k
          (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
217
8.75k
                                      MAX_CORR_BITS * sizeof(char));
218
62.3k
    }
219
71.1k
  }
220
152k
  if (gather_statistics)
221
72.4k
    entropy->pub.finish_pass = finish_pass_gather_phuff;
222
80.3k
  else
223
80.3k
    entropy->pub.finish_pass = finish_pass_phuff;
224
225
  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
226
   * for AC coefficients.
227
   */
228
344k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
229
191k
    compptr = cinfo->cur_comp_info[ci];
230
    /* Initialize DC predictions to 0 */
231
191k
    entropy->last_dc_val[ci] = 0;
232
    /* Get table index */
233
191k
    if (is_DC_band) {
234
66.3k
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
235
21.5k
        continue;
236
44.7k
      tbl = compptr->dc_tbl_no;
237
125k
    } else {
238
125k
      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
239
125k
    }
240
170k
    if (gather_statistics) {
241
      /* Check for invalid table index */
242
      /* (make_c_derived_tbl does this in the other path) */
243
85.6k
      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
244
0
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
245
      /* Allocate and zero the statistics tables */
246
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
247
85.6k
      if (entropy->count_ptrs[tbl] == NULL)
248
15.6k
        entropy->count_ptrs[tbl] = (long *)
249
15.6k
          (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
250
15.6k
                                      257 * sizeof(long));
251
85.6k
      memset(entropy->count_ptrs[tbl], 0, 257 * sizeof(long));
252
85.6k
    } else {
253
      /* Compute derived values for Huffman table */
254
      /* We may do this more than once for a table, but it's not expensive */
255
84.4k
      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
256
84.4k
                              &entropy->derived_tbls[tbl]);
257
84.4k
    }
258
170k
  }
259
260
  /* Initialize AC stuff */
261
152k
  entropy->EOBRUN = 0;
262
152k
  entropy->BE = 0;
263
264
  /* Initialize bit buffer to empty */
265
152k
  entropy->put_buffer = 0;
266
152k
  entropy->put_bits = 0;
267
268
  /* Initialize restart stuff */
269
152k
  entropy->restarts_to_go = cinfo->restart_interval;
270
152k
  entropy->next_restart_num = 0;
271
152k
}
272
273
274
/* Outputting bytes to the file.
275
 * NB: these must be called only when actually outputting,
276
 * that is, entropy->gather_statistics == FALSE.
277
 */
278
279
/* Emit a byte */
280
356M
#define emit_byte(entropy, val) { \
281
356M
  *(entropy)->next_output_byte++ = (JOCTET)(val); \
282
356M
  if (--(entropy)->free_in_buffer == 0) \
283
356M
    dump_buffer(entropy); \
284
356M
}
285
286
287
LOCAL(void)
288
dump_buffer(phuff_entropy_ptr entropy)
289
/* Empty the output buffer; we do not support suspension in this module. */
290
3.09k
{
291
3.09k
  struct jpeg_destination_mgr *dest = entropy->cinfo->dest;
292
293
3.09k
  if (!(*dest->empty_output_buffer) (entropy->cinfo))
294
0
    ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
295
  /* After a successful buffer dump, must reset buffer pointers */
296
3.09k
  entropy->next_output_byte = dest->next_output_byte;
297
3.09k
  entropy->free_in_buffer = dest->free_in_buffer;
298
3.09k
}
299
300
301
/* Outputting bits to the file */
302
303
/* Only the right 24 bits of put_buffer are used; the valid bits are
304
 * left-justified in this part.  At most 16 bits can be passed to emit_bits
305
 * in one call, and we never retain more than 7 bits in put_buffer
306
 * between calls, so 24 bits are sufficient.
307
 */
308
309
LOCAL(void)
310
emit_bits(phuff_entropy_ptr entropy, unsigned int code, int size)
311
/* Emit some bits, unless we are in gather mode */
312
1.62G
{
313
  /* This routine is heavily used, so it's worth coding tightly. */
314
1.62G
  register size_t put_buffer = (size_t)code;
315
1.62G
  register int put_bits = entropy->put_bits;
316
317
  /* if size is 0, caller used an invalid Huffman table entry */
318
1.62G
  if (size == 0)
319
0
    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
320
321
1.62G
  if (entropy->gather_statistics)
322
317M
    return;                     /* do nothing if we're only getting stats */
323
324
1.30G
  put_buffer &= (((size_t)1) << size) - 1; /* mask off any extra bits in code */
325
326
1.30G
  put_bits += size;             /* new number of bits in buffer */
327
328
1.30G
  put_buffer <<= 24 - put_bits; /* align incoming bits */
329
330
1.30G
  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
331
332
1.60G
  while (put_bits >= 8) {
333
300M
    int c = (int)((put_buffer >> 16) & 0xFF);
334
335
300M
    emit_byte(entropy, c);
336
300M
    if (c == 0xFF) {            /* need to stuff a zero byte? */
337
1.62M
      emit_byte(entropy, 0);
338
1.62M
    }
339
300M
    put_buffer <<= 8;
340
300M
    put_bits -= 8;
341
300M
  }
342
343
1.30G
  entropy->put_buffer = put_buffer; /* update variables */
344
1.30G
  entropy->put_bits = put_bits;
345
1.30G
}
346
347
348
LOCAL(void)
349
flush_bits(phuff_entropy_ptr entropy)
350
27.2M
{
351
27.2M
  emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
352
27.2M
  entropy->put_buffer = 0;     /* and reset bit-buffer to empty */
353
27.2M
  entropy->put_bits = 0;
354
27.2M
}
355
356
357
/*
358
 * Emit (or just count) a Huffman symbol.
359
 */
360
361
LOCAL(void)
362
emit_symbol(phuff_entropy_ptr entropy, int tbl_no, int symbol)
363
930M
{
364
930M
  if (entropy->gather_statistics)
365
467M
    entropy->count_ptrs[tbl_no][symbol]++;
366
462M
  else {
367
462M
    c_derived_tbl *tbl = entropy->derived_tbls[tbl_no];
368
462M
    emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
369
462M
  }
370
930M
}
371
372
373
/*
374
 * Emit bits from a correction bit buffer.
375
 */
376
377
LOCAL(void)
378
emit_buffered_bits(phuff_entropy_ptr entropy, char *bufstart,
379
                   unsigned int nbits)
380
282M
{
381
282M
  if (entropy->gather_statistics)
382
141M
    return;                     /* no real work */
383
384
517M
  while (nbits > 0) {
385
376M
    emit_bits(entropy, (unsigned int)(*bufstart), 1);
386
376M
    bufstart++;
387
376M
    nbits--;
388
376M
  }
389
141M
}
390
391
392
/*
393
 * Emit any pending EOBRUN symbol.
394
 */
395
396
LOCAL(void)
397
emit_eobrun(phuff_entropy_ptr entropy)
398
265M
{
399
265M
  register int temp, nbits;
400
401
265M
  if (entropy->EOBRUN > 0) {    /* if there is any pending EOBRUN */
402
101M
    temp = entropy->EOBRUN;
403
101M
    nbits = JPEG_NBITS_NONZERO(temp) - 1;
404
    /* safety check: shouldn't happen given limited correction-bit buffer */
405
101M
    if (nbits > 14)
406
0
      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
407
408
101M
    emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
409
101M
    if (nbits)
410
44.1M
      emit_bits(entropy, entropy->EOBRUN, nbits);
411
412
101M
    entropy->EOBRUN = 0;
413
414
    /* Emit any buffered correction bits */
415
101M
    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
416
101M
    entropy->BE = 0;
417
101M
  }
418
265M
}
419
420
421
/*
422
 * Emit a restart marker & resynchronize predictions.
423
 */
424
425
LOCAL(void)
426
emit_restart(phuff_entropy_ptr entropy, int restart_num)
427
51.2M
{
428
51.2M
  int ci;
429
430
51.2M
  emit_eobrun(entropy);
431
432
51.2M
  if (!entropy->gather_statistics) {
433
26.9M
    flush_bits(entropy);
434
26.9M
    emit_byte(entropy, 0xFF);
435
26.9M
    emit_byte(entropy, JPEG_RST0 + restart_num);
436
26.9M
  }
437
438
51.2M
  if (entropy->cinfo->Ss == 0) {
439
    /* Re-initialize DC predictions to 0 */
440
32.3M
    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
441
24.2M
      entropy->last_dc_val[ci] = 0;
442
43.1M
  } else {
443
    /* Re-initialize all AC-related fields to 0 */
444
43.1M
    entropy->EOBRUN = 0;
445
43.1M
    entropy->BE = 0;
446
43.1M
  }
447
51.2M
}
448
449
450
/*
451
 * MCU encoding for DC initial scan (either spectral selection,
452
 * or first pass of successive approximation).
453
 */
454
455
METHODDEF(boolean)
456
encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
457
144M
{
458
144M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
459
144M
  register int temp, temp2, temp3;
460
144M
  register int nbits;
461
144M
  int blkn, ci;
462
144M
  int Al = cinfo->Al;
463
144M
  JBLOCKROW block;
464
144M
  jpeg_component_info *compptr;
465
144M
  ISHIFT_TEMPS
466
144M
  int max_coef_bits = cinfo->data_precision + 2;
467
468
144M
  entropy->next_output_byte = cinfo->dest->next_output_byte;
469
144M
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
470
471
  /* Emit restart marker if needed */
472
144M
  if (cinfo->restart_interval)
473
13.7M
    if (entropy->restarts_to_go == 0)
474
5.39M
      emit_restart(entropy, entropy->next_restart_num);
475
476
  /* Encode the MCU data blocks */
477
413M
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
478
269M
    block = MCU_data[blkn];
479
269M
    ci = cinfo->MCU_membership[blkn];
480
269M
    compptr = cinfo->cur_comp_info[ci];
481
482
    /* Compute the DC value after the required point transform by Al.
483
     * This is simply an arithmetic right shift.
484
     */
485
269M
    temp2 = IRIGHT_SHIFT((int)((*block)[0]), Al);
486
487
    /* DC differences are figured on the point-transformed values. */
488
269M
    temp = temp2 - entropy->last_dc_val[ci];
489
269M
    entropy->last_dc_val[ci] = temp2;
490
491
    /* Encode the DC coefficient difference per section G.1.2.1 */
492
493
    /* This is a well-known technique for obtaining the absolute value without
494
     * a branch.  It is derived from an assembly language technique presented
495
     * in "How to Optimize for the Pentium Processors", Copyright (c) 1996,
496
     * 1997 by Agner Fog.
497
     */
498
269M
    temp3 = temp >> (CHAR_BIT * sizeof(int) - 1);
499
269M
    temp ^= temp3;
500
269M
    temp -= temp3;              /* temp is abs value of input */
501
    /* For a negative input, want temp2 = bitwise complement of abs(input) */
502
269M
    temp2 = temp ^ temp3;
503
504
    /* Find the number of bits needed for the magnitude of the coefficient */
505
269M
    nbits = JPEG_NBITS(temp);
506
    /* Check for out-of-range coefficient values.
507
     * Since we're encoding a difference, the range limit is twice as much.
508
     */
509
269M
    if (nbits > max_coef_bits + 1)
510
1.48k
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
511
512
    /* Count/emit the Huffman-coded symbol for the number of bits */
513
269M
    emit_symbol(entropy, compptr->dc_tbl_no, nbits);
514
515
    /* Emit that number of bits of the value, if positive, */
516
    /* or the complement of its magnitude, if negative. */
517
269M
    if (nbits)                  /* emit_bits rejects calls with size 0 */
518
46.7M
      emit_bits(entropy, (unsigned int)temp2, nbits);
519
269M
  }
520
521
144M
  cinfo->dest->next_output_byte = entropy->next_output_byte;
522
144M
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
523
524
  /* Update restart-interval state too */
525
144M
  if (cinfo->restart_interval) {
526
13.7M
    if (entropy->restarts_to_go == 0) {
527
5.39M
      entropy->restarts_to_go = cinfo->restart_interval;
528
5.39M
      entropy->next_restart_num++;
529
5.39M
      entropy->next_restart_num &= 7;
530
5.39M
    }
531
13.7M
    entropy->restarts_to_go--;
532
13.7M
  }
533
534
144M
  return TRUE;
535
144M
}
536
537
538
/*
539
 * Data preparation for encode_mcu_AC_first().
540
 */
541
542
0
#define COMPUTE_ABSVALUES_AC_FIRST(Sl) { \
543
0
  for (k = 0; k < Sl; k++) { \
544
0
    temp = block[jpeg_natural_order_start[k]]; \
545
0
    if (temp == 0) \
546
0
      continue; \
547
0
    /* We must apply the point transform by Al.  For AC coefficients this \
548
0
     * is an integer division with rounding towards 0.  To do this portably \
549
0
     * in C, we shift after obtaining the absolute value; so the code is \
550
0
     * interwoven with finding the abs value (temp) and output bits (temp2). \
551
0
     */ \
552
0
    temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
553
0
    temp ^= temp2; \
554
0
    temp -= temp2;              /* temp is abs value of input */ \
555
0
    temp >>= Al;                /* apply the point transform */ \
556
0
    /* Watch out for case that nonzero coef is zero after point transform */ \
557
0
    if (temp == 0) \
558
0
      continue; \
559
0
    /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ \
560
0
    temp2 ^= temp; \
561
0
    values[k] = (UJCOEF)temp; \
562
0
    values[k + DCTSIZE2] = (UJCOEF)temp2; \
563
0
    zerobits |= ((size_t)1U) << k; \
564
0
  } \
565
0
}
566
567
METHODDEF(void)
568
encode_mcu_AC_first_prepare(const JCOEF *block,
569
                            const int *jpeg_natural_order_start, int Sl,
570
                            int Al, UJCOEF *values, size_t *bits)
571
0
{
572
0
  register int k, temp, temp2;
573
0
  size_t zerobits = 0U;
574
0
  int Sl0 = Sl;
575
576
#if SIZEOF_SIZE_T == 4
577
  if (Sl0 > 32)
578
    Sl0 = 32;
579
#endif
580
581
0
  COMPUTE_ABSVALUES_AC_FIRST(Sl0);
582
583
0
  bits[0] = zerobits;
584
#if SIZEOF_SIZE_T == 4
585
  zerobits = 0U;
586
587
  if (Sl > 32) {
588
    Sl -= 32;
589
    jpeg_natural_order_start += 32;
590
    values += 32;
591
592
    COMPUTE_ABSVALUES_AC_FIRST(Sl);
593
  }
594
  bits[1] = zerobits;
595
#endif
596
0
}
597
598
/*
599
 * MCU encoding for AC initial scan (either spectral selection,
600
 * or first pass of successive approximation).
601
 */
602
603
411M
#define ENCODE_COEFS_AC_FIRST(label) { \
604
781M
  while (zerobits) { \
605
369M
    r = count_zeroes(&zerobits); \
606
369M
    cvalue += r; \
607
369M
label \
608
369M
    temp  = cvalue[0]; \
609
369M
    temp2 = cvalue[DCTSIZE2]; \
610
369M
    \
611
369M
    /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \
612
378M
    while (r > 15) { \
613
8.64M
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
614
8.64M
      r -= 16; \
615
8.64M
    } \
616
369M
    \
617
369M
    /* Find the number of bits needed for the magnitude of the coefficient */ \
618
369M
    nbits = JPEG_NBITS_NONZERO(temp);  /* there must be at least one 1 bit */ \
619
369M
    /* Check for out-of-range coefficient values */ \
620
369M
    if (nbits > max_coef_bits) \
621
369M
      ERREXIT(cinfo, JERR_BAD_DCT_COEF); \
622
369M
    \
623
369M
    /* Count/emit Huffman symbol for run length / number of bits */ \
624
369M
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); \
625
369M
    \
626
369M
    /* Emit that number of bits of the value, if positive, */ \
627
369M
    /* or the complement of its magnitude, if negative. */ \
628
369M
    emit_bits(entropy, (unsigned int)temp2, nbits); \
629
369M
    \
630
369M
    cvalue++; \
631
369M
    zerobits >>= 1; \
632
369M
  } \
633
411M
}
634
635
METHODDEF(boolean)
636
encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
637
411M
{
638
411M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
639
411M
  register int temp, temp2;
640
411M
  register int nbits, r;
641
411M
  int Sl = cinfo->Se - cinfo->Ss + 1;
642
411M
  int Al = cinfo->Al;
643
411M
  UJCOEF values_unaligned[2 * DCTSIZE2 + 15];
644
411M
  UJCOEF *values;
645
411M
  const UJCOEF *cvalue;
646
411M
  size_t zerobits;
647
411M
  size_t bits[8 / SIZEOF_SIZE_T];
648
411M
  int max_coef_bits = cinfo->data_precision + 2;
649
650
#ifdef ZERO_BUFFERS
651
  memset(values_unaligned, 0, sizeof(values_unaligned));
652
  memset(bits, 0, sizeof(bits));
653
#endif
654
655
411M
  entropy->next_output_byte = cinfo->dest->next_output_byte;
656
411M
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
657
658
  /* Emit restart marker if needed */
659
411M
  if (cinfo->restart_interval)
660
61.0M
    if (entropy->restarts_to_go == 0)
661
21.5M
      emit_restart(entropy, entropy->next_restart_num);
662
663
411M
#ifdef WITH_SIMD
664
411M
  cvalue = values = (UJCOEF *)PAD((JUINTPTR)values_unaligned, 16);
665
#else
666
  /* Not using SIMD, so alignment is not needed */
667
  cvalue = values = values_unaligned;
668
#endif
669
670
  /* Prepare data */
671
411M
  entropy->AC_first_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
672
411M
                            Sl, Al, values, bits);
673
674
411M
  zerobits = bits[0];
675
#if SIZEOF_SIZE_T == 4
676
  zerobits |= bits[1];
677
#endif
678
679
  /* Emit any pending EOBRUN */
680
411M
  if (zerobits && (entropy->EOBRUN > 0))
681
33.0M
    emit_eobrun(entropy);
682
683
#if SIZEOF_SIZE_T == 4
684
  zerobits = bits[0];
685
#endif
686
687
  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
688
689
411M
  ENCODE_COEFS_AC_FIRST((void)0;);
690
691
#if SIZEOF_SIZE_T == 4
692
  zerobits = bits[1];
693
  if (zerobits) {
694
    int diff = ((values + DCTSIZE2 / 2) - cvalue);
695
    r = count_zeroes(&zerobits);
696
    r += diff;
697
    cvalue += r;
698
    goto first_iter_ac_first;
699
  }
700
701
  ENCODE_COEFS_AC_FIRST(first_iter_ac_first:);
702
#endif
703
704
411M
  if (cvalue < (values + Sl)) { /* If there are trailing zeroes, */
705
400M
    entropy->EOBRUN++;          /* count an EOB */
706
400M
    if (entropy->EOBRUN == 0x7FFF)
707
0
      emit_eobrun(entropy);     /* force it out to avoid overflow */
708
400M
  }
709
710
411M
  cinfo->dest->next_output_byte = entropy->next_output_byte;
711
411M
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
712
713
  /* Update restart-interval state too */
714
411M
  if (cinfo->restart_interval) {
715
61.0M
    if (entropy->restarts_to_go == 0) {
716
21.5M
      entropy->restarts_to_go = cinfo->restart_interval;
717
21.5M
      entropy->next_restart_num++;
718
21.5M
      entropy->next_restart_num &= 7;
719
21.5M
    }
720
61.0M
    entropy->restarts_to_go--;
721
61.0M
  }
722
723
411M
  return TRUE;
724
411M
}
725
726
727
/*
728
 * MCU encoding for DC successive approximation refinement scan.
729
 * Note: we assume such scans can be multi-component, although the spec
730
 * is not very clear on the point.
731
 */
732
733
METHODDEF(boolean)
734
encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
735
66.8M
{
736
66.8M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
737
66.8M
  register int temp;
738
66.8M
  int blkn;
739
66.8M
  int Al = cinfo->Al;
740
66.8M
  JBLOCKROW block;
741
742
66.8M
  entropy->next_output_byte = cinfo->dest->next_output_byte;
743
66.8M
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
744
745
  /* Emit restart marker if needed */
746
66.8M
  if (cinfo->restart_interval)
747
6.87M
    if (entropy->restarts_to_go == 0)
748
2.69M
      emit_restart(entropy, entropy->next_restart_num);
749
750
  /* Encode the MCU data blocks */
751
192M
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
752
125M
    block = MCU_data[blkn];
753
754
    /* We simply emit the Al'th bit of the DC coefficient value. */
755
125M
    temp = (*block)[0];
756
125M
    emit_bits(entropy, (unsigned int)(temp >> Al), 1);
757
125M
  }
758
759
66.8M
  cinfo->dest->next_output_byte = entropy->next_output_byte;
760
66.8M
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
761
762
  /* Update restart-interval state too */
763
66.8M
  if (cinfo->restart_interval) {
764
6.87M
    if (entropy->restarts_to_go == 0) {
765
2.69M
      entropy->restarts_to_go = cinfo->restart_interval;
766
2.69M
      entropy->next_restart_num++;
767
2.69M
      entropy->next_restart_num &= 7;
768
2.69M
    }
769
6.87M
    entropy->restarts_to_go--;
770
6.87M
  }
771
772
66.8M
  return TRUE;
773
66.8M
}
774
775
776
/*
777
 * Data preparation for encode_mcu_AC_refine().
778
 */
779
780
0
#define COMPUTE_ABSVALUES_AC_REFINE(Sl, koffset) { \
781
0
  /* It is convenient to make a pre-pass to determine the transformed \
782
0
   * coefficients' absolute values and the EOB position. \
783
0
   */ \
784
0
  for (k = 0; k < Sl; k++) { \
785
0
    temp = block[jpeg_natural_order_start[k]]; \
786
0
    /* We must apply the point transform by Al.  For AC coefficients this \
787
0
     * is an integer division with rounding towards 0.  To do this portably \
788
0
     * in C, we shift after obtaining the absolute value. \
789
0
     */ \
790
0
    temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
791
0
    temp ^= temp2; \
792
0
    temp -= temp2;              /* temp is abs value of input */ \
793
0
    temp >>= Al;                /* apply the point transform */ \
794
0
    if (temp != 0) { \
795
0
      zerobits |= ((size_t)1U) << k; \
796
0
      signbits |= ((size_t)(temp2 + 1)) << k; \
797
0
    } \
798
0
    absvalues[k] = (UJCOEF)temp; /* save abs value for main pass */ \
799
0
    if (temp == 1) \
800
0
      EOB = k + koffset;        /* EOB = index of last newly-nonzero coef */ \
801
0
  } \
802
0
}
803
804
METHODDEF(int)
805
encode_mcu_AC_refine_prepare(const JCOEF *block,
806
                             const int *jpeg_natural_order_start, int Sl,
807
                             int Al, UJCOEF *absvalues, size_t *bits)
808
0
{
809
0
  register int k, temp, temp2;
810
0
  int EOB = 0;
811
0
  size_t zerobits = 0U, signbits = 0U;
812
0
  int Sl0 = Sl;
813
814
#if SIZEOF_SIZE_T == 4
815
  if (Sl0 > 32)
816
    Sl0 = 32;
817
#endif
818
819
0
  COMPUTE_ABSVALUES_AC_REFINE(Sl0, 0);
820
821
0
  bits[0] = zerobits;
822
0
#if SIZEOF_SIZE_T == 8
823
0
  bits[1] = signbits;
824
#else
825
  bits[2] = signbits;
826
827
  zerobits = 0U;
828
  signbits = 0U;
829
830
  if (Sl > 32) {
831
    Sl -= 32;
832
    jpeg_natural_order_start += 32;
833
    absvalues += 32;
834
835
    COMPUTE_ABSVALUES_AC_REFINE(Sl, 32);
836
  }
837
838
  bits[1] = zerobits;
839
  bits[3] = signbits;
840
#endif
841
842
0
  return EOB;
843
0
}
844
845
846
/*
847
 * MCU encoding for AC successive approximation refinement scan.
848
 */
849
850
404M
#define ENCODE_COEFS_AC_REFINE(label) { \
851
1.32G
  while (zerobits) { \
852
924M
    idx = count_zeroes(&zerobits); \
853
924M
    r += idx; \
854
924M
    cabsvalue += idx; \
855
924M
    signbits >>= idx; \
856
924M
label \
857
924M
    /* Emit any required ZRLs, but not if they can be folded into EOB */ \
858
933M
    while (r > 15 && (cabsvalue <= EOBPTR)) { \
859
8.69M
      /* emit any pending EOBRUN and the BE correction bits */ \
860
8.69M
      emit_eobrun(entropy); \
861
8.69M
      /* Emit ZRL */ \
862
8.69M
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
863
8.69M
      r -= 16; \
864
8.69M
      /* Emit buffered correction bits that must be associated with ZRL */ \
865
8.69M
      emit_buffered_bits(entropy, BR_buffer, BR); \
866
8.69M
      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
867
8.69M
      BR = 0; \
868
8.69M
    } \
869
924M
    \
870
924M
    temp = *cabsvalue++; \
871
924M
    \
872
924M
    /* If the coef was previously nonzero, it only needs a correction bit. \
873
924M
     * NOTE: a straight translation of the spec's figure G.7 would suggest \
874
924M
     * that we also need to test r > 15.  But if r > 15, we can only get here \
875
924M
     * if k > EOB, which implies that this coefficient is not 1. \
876
924M
     */ \
877
924M
    if (temp > 1) { \
878
752M
      /* The correction bit is the next bit of the absolute value. */ \
879
752M
      BR_buffer[BR++] = (char)(temp & 1); \
880
752M
      signbits >>= 1; \
881
752M
      zerobits >>= 1; \
882
752M
      continue; \
883
752M
    } \
884
924M
    \
885
924M
    /* Emit any pending EOBRUN and the BE correction bits */ \
886
924M
    emit_eobrun(entropy); \
887
171M
    \
888
171M
    /* Count/emit Huffman symbol for run length / number of bits */ \
889
171M
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); \
890
171M
    \
891
171M
    /* Emit output bit for newly-nonzero coef */ \
892
171M
    temp = signbits & 1; /* ((*block)[jpeg_natural_order_start[k]] < 0) ? 0 : 1 */ \
893
171M
    emit_bits(entropy, (unsigned int)temp, 1); \
894
171M
    \
895
171M
    /* Emit buffered correction bits that must be associated with this code */ \
896
171M
    emit_buffered_bits(entropy, BR_buffer, BR); \
897
171M
    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
898
171M
    BR = 0; \
899
171M
    r = 0;                      /* reset zero run length */ \
900
171M
    signbits >>= 1; \
901
171M
    zerobits >>= 1; \
902
171M
  } \
903
404M
}
904
905
METHODDEF(boolean)
906
encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
907
404M
{
908
404M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
909
404M
  register int temp, r, idx;
910
404M
  char *BR_buffer;
911
404M
  unsigned int BR;
912
404M
  int Sl = cinfo->Se - cinfo->Ss + 1;
913
404M
  int Al = cinfo->Al;
914
404M
  UJCOEF absvalues_unaligned[DCTSIZE2 + 15];
915
404M
  UJCOEF *absvalues;
916
404M
  const UJCOEF *cabsvalue, *EOBPTR;
917
404M
  size_t zerobits, signbits;
918
404M
  size_t bits[16 / SIZEOF_SIZE_T];
919
920
#ifdef ZERO_BUFFERS
921
  memset(absvalues_unaligned, 0, sizeof(absvalues_unaligned));
922
  memset(bits, 0, sizeof(bits));
923
#endif
924
925
404M
  entropy->next_output_byte = cinfo->dest->next_output_byte;
926
404M
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
927
928
  /* Emit restart marker if needed */
929
404M
  if (cinfo->restart_interval)
930
61.0M
    if (entropy->restarts_to_go == 0)
931
21.5M
      emit_restart(entropy, entropy->next_restart_num);
932
933
404M
#ifdef WITH_SIMD
934
404M
  cabsvalue = absvalues = (UJCOEF *)PAD((JUINTPTR)absvalues_unaligned, 16);
935
#else
936
  /* Not using SIMD, so alignment is not needed */
937
  cabsvalue = absvalues = absvalues_unaligned;
938
#endif
939
940
  /* Prepare data */
941
404M
  EOBPTR = absvalues +
942
404M
    entropy->AC_refine_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
943
404M
                               Sl, Al, absvalues, bits);
944
945
  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
946
947
404M
  r = 0;                        /* r = run length of zeros */
948
404M
  BR = 0;                       /* BR = count of buffered bits added now */
949
404M
  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
950
951
404M
  zerobits = bits[0];
952
404M
#if SIZEOF_SIZE_T == 8
953
404M
  signbits = bits[1];
954
#else
955
  signbits = bits[2];
956
#endif
957
404M
  ENCODE_COEFS_AC_REFINE((void)0;);
958
959
#if SIZEOF_SIZE_T == 4
960
  zerobits = bits[1];
961
  signbits = bits[3];
962
963
  if (zerobits) {
964
    int diff = ((absvalues + DCTSIZE2 / 2) - cabsvalue);
965
    idx = count_zeroes(&zerobits);
966
    signbits >>= idx;
967
    idx += diff;
968
    r += idx;
969
    cabsvalue += idx;
970
    goto first_iter_ac_refine;
971
  }
972
973
  ENCODE_COEFS_AC_REFINE(first_iter_ac_refine:);
974
#endif
975
976
404M
  r |= (int)((absvalues + Sl) - cabsvalue);
977
978
404M
  if (r > 0 || BR > 0) {        /* If there are trailing zeroes, */
979
402M
    entropy->EOBRUN++;          /* count an EOB */
980
402M
    entropy->BE += BR;          /* concat my correction bits to older ones */
981
    /* We force out the EOB if we risk either:
982
     * 1. overflow of the EOB counter;
983
     * 2. overflow of the correction bit buffer during the next MCU.
984
     */
985
402M
    if (entropy->EOBRUN == 0x7FFF ||
986
402M
        entropy->BE > (MAX_CORR_BITS - DCTSIZE2 + 1))
987
264k
      emit_eobrun(entropy);
988
402M
  }
989
990
404M
  cinfo->dest->next_output_byte = entropy->next_output_byte;
991
404M
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
992
993
  /* Update restart-interval state too */
994
404M
  if (cinfo->restart_interval) {
995
61.0M
    if (entropy->restarts_to_go == 0) {
996
21.5M
      entropy->restarts_to_go = cinfo->restart_interval;
997
21.5M
      entropy->next_restart_num++;
998
21.5M
      entropy->next_restart_num &= 7;
999
21.5M
    }
1000
61.0M
    entropy->restarts_to_go--;
1001
61.0M
  }
1002
1003
404M
  return TRUE;
1004
404M
}
1005
1006
1007
/*
1008
 * Finish up at the end of a Huffman-compressed progressive scan.
1009
 */
1010
1011
METHODDEF(void)
1012
finish_pass_phuff(j_compress_ptr cinfo)
1013
245k
{
1014
245k
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
1015
1016
245k
  entropy->next_output_byte = cinfo->dest->next_output_byte;
1017
245k
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
1018
1019
  /* Flush out any buffered data */
1020
245k
  emit_eobrun(entropy);
1021
245k
  flush_bits(entropy);
1022
1023
245k
  cinfo->dest->next_output_byte = entropy->next_output_byte;
1024
245k
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1025
245k
}
1026
1027
1028
/*
1029
 * Finish up a statistics-gathering pass and create the new Huffman tables.
1030
 */
1031
1032
METHODDEF(void)
1033
finish_pass_gather_phuff(j_compress_ptr cinfo)
1034
218k
{
1035
218k
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
1036
218k
  boolean is_DC_band;
1037
218k
  int ci, tbl;
1038
218k
  jpeg_component_info *compptr;
1039
218k
  JHUFF_TBL **htblptr;
1040
218k
  boolean did[NUM_HUFF_TBLS];
1041
1042
  /* Flush out buffered data (all we care about is counting the EOB symbol) */
1043
218k
  emit_eobrun(entropy);
1044
1045
218k
  is_DC_band = (cinfo->Ss == 0);
1046
1047
  /* It's important not to apply jpeg_gen_optimal_table more than once
1048
   * per table, because it clobbers the input frequency counts!
1049
   */
1050
218k
  memset(did, 0, sizeof(did));
1051
1052
475k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1053
257k
    compptr = cinfo->cur_comp_info[ci];
1054
257k
    if (is_DC_band) {
1055
66.8k
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
1056
0
        continue;
1057
66.8k
      tbl = compptr->dc_tbl_no;
1058
190k
    } else {
1059
190k
      tbl = compptr->ac_tbl_no;
1060
190k
    }
1061
257k
    if (!did[tbl]) {
1062
236k
      if (is_DC_band)
1063
46.3k
        htblptr = &cinfo->dc_huff_tbl_ptrs[tbl];
1064
190k
      else
1065
190k
        htblptr = &cinfo->ac_huff_tbl_ptrs[tbl];
1066
236k
      if (*htblptr == NULL)
1067
0
        *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
1068
236k
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
1069
236k
      did[tbl] = TRUE;
1070
236k
    }
1071
257k
  }
1072
218k
}
1073
1074
1075
/*
1076
 * Module initialization routine for progressive Huffman entropy encoding.
1077
 */
1078
1079
GLOBAL(void)
1080
jinit_phuff_encoder(j_compress_ptr cinfo)
1081
29.7k
{
1082
29.7k
  phuff_entropy_ptr entropy;
1083
29.7k
  int i;
1084
1085
29.7k
  entropy = (phuff_entropy_ptr)
1086
29.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
1087
29.7k
                                sizeof(phuff_entropy_encoder));
1088
29.7k
  cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
1089
29.7k
  entropy->pub.start_pass = start_pass_phuff;
1090
1091
  /* Mark tables unallocated */
1092
148k
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
1093
118k
    entropy->derived_tbls[i] = NULL;
1094
118k
    entropy->count_ptrs[i] = NULL;
1095
118k
  }
1096
  entropy->bit_buffer = NULL;   /* needed only in AC refinement scan */
1097
29.7k
}
1098
1099
#endif /* C_PROGRESSIVE_SUPPORTED */