Coverage Report

Created: 2026-01-25 06:04

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