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
0
#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
0
#define IRIGHT_SHIFT(x, shft)   ((x) >> (shft))
122
#endif
123
124
0
#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
0
{
150
0
#if defined(HAVE_BUILTIN_CTZL)
151
0
  int result;
152
0
  result = __builtin_ctzl(*x);
153
0
  *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
0
  return (int)result;
170
0
}
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
0
{
180
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
181
0
  boolean is_DC_band;
182
0
  int ci, tbl;
183
0
  jpeg_component_info *compptr;
184
185
0
  entropy->cinfo = cinfo;
186
0
  entropy->gather_statistics = gather_statistics;
187
188
0
  is_DC_band = (cinfo->Ss == 0);
189
190
  /* We assume jcmaster.c already validated the scan parameters. */
191
192
  /* Select execution routines */
193
0
  if (cinfo->Ah == 0) {
194
0
    if (is_DC_band)
195
0
      entropy->pub.encode_mcu = encode_mcu_DC_first;
196
0
    else
197
0
      entropy->pub.encode_mcu = encode_mcu_AC_first;
198
0
#ifdef WITH_SIMD
199
0
    if (!jsimd_set_encode_mcu_AC_first_prepare(cinfo,
200
0
                                               &entropy->AC_first_prepare))
201
0
#endif
202
0
      entropy->AC_first_prepare = encode_mcu_AC_first_prepare;
203
0
  } else {
204
0
    if (is_DC_band)
205
0
      entropy->pub.encode_mcu = encode_mcu_DC_refine;
206
0
    else {
207
0
      entropy->pub.encode_mcu = encode_mcu_AC_refine;
208
0
#ifdef WITH_SIMD
209
0
      if (!jsimd_set_encode_mcu_AC_refine_prepare(cinfo,
210
0
                                                  &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
0
      if (entropy->bit_buffer == NULL)
215
0
        entropy->bit_buffer = (char *)
216
0
          (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
217
0
                                      MAX_CORR_BITS * sizeof(char));
218
0
    }
219
0
  }
220
0
  if (gather_statistics)
221
0
    entropy->pub.finish_pass = finish_pass_gather_phuff;
222
0
  else
223
0
    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
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
229
0
    compptr = cinfo->cur_comp_info[ci];
230
    /* Initialize DC predictions to 0 */
231
0
    entropy->last_dc_val[ci] = 0;
232
    /* Get table index */
233
0
    if (is_DC_band) {
234
0
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
235
0
        continue;
236
0
      tbl = compptr->dc_tbl_no;
237
0
    } else {
238
0
      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
239
0
    }
240
0
    if (gather_statistics) {
241
      /* Check for invalid table index */
242
      /* (make_c_derived_tbl does this in the other path) */
243
0
      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
0
      if (entropy->count_ptrs[tbl] == NULL)
248
0
        entropy->count_ptrs[tbl] = (long *)
249
0
          (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
250
0
                                      257 * sizeof(long));
251
0
      memset(entropy->count_ptrs[tbl], 0, 257 * sizeof(long));
252
0
    } 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
0
      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
256
0
                              &entropy->derived_tbls[tbl]);
257
0
    }
258
0
  }
259
260
  /* Initialize AC stuff */
261
0
  entropy->EOBRUN = 0;
262
0
  entropy->BE = 0;
263
264
  /* Initialize bit buffer to empty */
265
0
  entropy->put_buffer = 0;
266
0
  entropy->put_bits = 0;
267
268
  /* Initialize restart stuff */
269
0
  entropy->restarts_to_go = cinfo->restart_interval;
270
0
  entropy->next_restart_num = 0;
271
0
}
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
0
#define emit_byte(entropy, val) { \
281
0
  *(entropy)->next_output_byte++ = (JOCTET)(val); \
282
0
  if (--(entropy)->free_in_buffer == 0) \
283
0
    dump_buffer(entropy); \
284
0
}
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
0
{
291
0
  struct jpeg_destination_mgr *dest = entropy->cinfo->dest;
292
293
0
  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
0
  entropy->next_output_byte = dest->next_output_byte;
297
0
  entropy->free_in_buffer = dest->free_in_buffer;
298
0
}
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
0
{
313
  /* This routine is heavily used, so it's worth coding tightly. */
314
0
  register size_t put_buffer = (size_t)code;
315
0
  register int put_bits = entropy->put_bits;
316
317
  /* if size is 0, caller used an invalid Huffman table entry */
318
0
  if (size == 0)
319
0
    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
320
321
0
  if (entropy->gather_statistics)
322
0
    return;                     /* do nothing if we're only getting stats */
323
324
0
  put_buffer &= (((size_t)1) << size) - 1; /* mask off any extra bits in code */
325
326
0
  put_bits += size;             /* new number of bits in buffer */
327
328
0
  put_buffer <<= 24 - put_bits; /* align incoming bits */
329
330
0
  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
331
332
0
  while (put_bits >= 8) {
333
0
    int c = (int)((put_buffer >> 16) & 0xFF);
334
335
0
    emit_byte(entropy, c);
336
0
    if (c == 0xFF) {            /* need to stuff a zero byte? */
337
0
      emit_byte(entropy, 0);
338
0
    }
339
0
    put_buffer <<= 8;
340
0
    put_bits -= 8;
341
0
  }
342
343
0
  entropy->put_buffer = put_buffer; /* update variables */
344
0
  entropy->put_bits = put_bits;
345
0
}
346
347
348
LOCAL(void)
349
flush_bits(phuff_entropy_ptr entropy)
350
0
{
351
0
  emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
352
0
  entropy->put_buffer = 0;     /* and reset bit-buffer to empty */
353
0
  entropy->put_bits = 0;
354
0
}
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
0
{
364
0
  if (entropy->gather_statistics)
365
0
    entropy->count_ptrs[tbl_no][symbol]++;
366
0
  else {
367
0
    c_derived_tbl *tbl = entropy->derived_tbls[tbl_no];
368
0
    emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
369
0
  }
370
0
}
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
0
{
381
0
  if (entropy->gather_statistics)
382
0
    return;                     /* no real work */
383
384
0
  while (nbits > 0) {
385
0
    emit_bits(entropy, (unsigned int)(*bufstart), 1);
386
0
    bufstart++;
387
0
    nbits--;
388
0
  }
389
0
}
390
391
392
/*
393
 * Emit any pending EOBRUN symbol.
394
 */
395
396
LOCAL(void)
397
emit_eobrun(phuff_entropy_ptr entropy)
398
0
{
399
0
  register int temp, nbits;
400
401
0
  if (entropy->EOBRUN > 0) {    /* if there is any pending EOBRUN */
402
0
    temp = entropy->EOBRUN;
403
0
    nbits = JPEG_NBITS_NONZERO(temp) - 1;
404
    /* safety check: shouldn't happen given limited correction-bit buffer */
405
0
    if (nbits > 14)
406
0
      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
407
408
0
    emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
409
0
    if (nbits)
410
0
      emit_bits(entropy, entropy->EOBRUN, nbits);
411
412
0
    entropy->EOBRUN = 0;
413
414
    /* Emit any buffered correction bits */
415
0
    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
416
0
    entropy->BE = 0;
417
0
  }
418
0
}
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
0
{
428
0
  int ci;
429
430
0
  emit_eobrun(entropy);
431
432
0
  if (!entropy->gather_statistics) {
433
0
    flush_bits(entropy);
434
0
    emit_byte(entropy, 0xFF);
435
0
    emit_byte(entropy, JPEG_RST0 + restart_num);
436
0
  }
437
438
0
  if (entropy->cinfo->Ss == 0) {
439
    /* Re-initialize DC predictions to 0 */
440
0
    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
441
0
      entropy->last_dc_val[ci] = 0;
442
0
  } else {
443
    /* Re-initialize all AC-related fields to 0 */
444
0
    entropy->EOBRUN = 0;
445
0
    entropy->BE = 0;
446
0
  }
447
0
}
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
0
{
458
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
459
0
  register int temp, temp2, temp3;
460
0
  register int nbits;
461
0
  int blkn, ci;
462
0
  int Al = cinfo->Al;
463
0
  JBLOCKROW block;
464
0
  jpeg_component_info *compptr;
465
0
  ISHIFT_TEMPS
466
0
  int max_coef_bits = cinfo->data_precision + 2;
467
468
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
469
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
470
471
  /* Emit restart marker if needed */
472
0
  if (cinfo->restart_interval)
473
0
    if (entropy->restarts_to_go == 0)
474
0
      emit_restart(entropy, entropy->next_restart_num);
475
476
  /* Encode the MCU data blocks */
477
0
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
478
0
    block = MCU_data[blkn];
479
0
    ci = cinfo->MCU_membership[blkn];
480
0
    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
0
    temp2 = IRIGHT_SHIFT((int)((*block)[0]), Al);
486
487
    /* DC differences are figured on the point-transformed values. */
488
0
    temp = temp2 - entropy->last_dc_val[ci];
489
0
    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
0
    temp3 = temp >> (CHAR_BIT * sizeof(int) - 1);
499
0
    temp ^= temp3;
500
0
    temp -= temp3;              /* temp is abs value of input */
501
    /* For a negative input, want temp2 = bitwise complement of abs(input) */
502
0
    temp2 = temp ^ temp3;
503
504
    /* Find the number of bits needed for the magnitude of the coefficient */
505
0
    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
0
    if (nbits > max_coef_bits + 1)
510
0
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
511
512
    /* Count/emit the Huffman-coded symbol for the number of bits */
513
0
    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
0
    if (nbits)                  /* emit_bits rejects calls with size 0 */
518
0
      emit_bits(entropy, (unsigned int)temp2, nbits);
519
0
  }
520
521
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
522
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
523
524
  /* Update restart-interval state too */
525
0
  if (cinfo->restart_interval) {
526
0
    if (entropy->restarts_to_go == 0) {
527
0
      entropy->restarts_to_go = cinfo->restart_interval;
528
0
      entropy->next_restart_num++;
529
0
      entropy->next_restart_num &= 7;
530
0
    }
531
0
    entropy->restarts_to_go--;
532
0
  }
533
534
0
  return TRUE;
535
0
}
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
0
#define ENCODE_COEFS_AC_FIRST(label) { \
604
0
  while (zerobits) { \
605
0
    r = count_zeroes(&zerobits); \
606
0
    cvalue += r; \
607
0
label \
608
0
    temp  = cvalue[0]; \
609
0
    temp2 = cvalue[DCTSIZE2]; \
610
0
    \
611
0
    /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \
612
0
    while (r > 15) { \
613
0
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
614
0
      r -= 16; \
615
0
    } \
616
0
    \
617
0
    /* Find the number of bits needed for the magnitude of the coefficient */ \
618
0
    nbits = JPEG_NBITS_NONZERO(temp);  /* there must be at least one 1 bit */ \
619
0
    /* Check for out-of-range coefficient values */ \
620
0
    if (nbits > max_coef_bits) \
621
0
      ERREXIT(cinfo, JERR_BAD_DCT_COEF); \
622
0
    \
623
0
    /* Count/emit Huffman symbol for run length / number of bits */ \
624
0
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); \
625
0
    \
626
0
    /* Emit that number of bits of the value, if positive, */ \
627
0
    /* or the complement of its magnitude, if negative. */ \
628
0
    emit_bits(entropy, (unsigned int)temp2, nbits); \
629
0
    \
630
0
    cvalue++; \
631
0
    zerobits >>= 1; \
632
0
  } \
633
0
}
634
635
METHODDEF(boolean)
636
encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
637
0
{
638
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
639
0
  register int temp, temp2;
640
0
  register int nbits, r;
641
0
  int Sl = cinfo->Se - cinfo->Ss + 1;
642
0
  int Al = cinfo->Al;
643
0
  UJCOEF values_unaligned[2 * DCTSIZE2 + 15];
644
0
  UJCOEF *values;
645
0
  const UJCOEF *cvalue;
646
0
  size_t zerobits;
647
0
  size_t bits[8 / SIZEOF_SIZE_T];
648
0
  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
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
656
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
657
658
  /* Emit restart marker if needed */
659
0
  if (cinfo->restart_interval)
660
0
    if (entropy->restarts_to_go == 0)
661
0
      emit_restart(entropy, entropy->next_restart_num);
662
663
0
#ifdef WITH_SIMD
664
0
  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
0
  entropy->AC_first_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
672
0
                            Sl, Al, values, bits);
673
674
0
  zerobits = bits[0];
675
#if SIZEOF_SIZE_T == 4
676
  zerobits |= bits[1];
677
#endif
678
679
  /* Emit any pending EOBRUN */
680
0
  if (zerobits && (entropy->EOBRUN > 0))
681
0
    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
0
  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
0
  if (cvalue < (values + Sl)) { /* If there are trailing zeroes, */
705
0
    entropy->EOBRUN++;          /* count an EOB */
706
0
    if (entropy->EOBRUN == 0x7FFF)
707
0
      emit_eobrun(entropy);     /* force it out to avoid overflow */
708
0
  }
709
710
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
711
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
712
713
  /* Update restart-interval state too */
714
0
  if (cinfo->restart_interval) {
715
0
    if (entropy->restarts_to_go == 0) {
716
0
      entropy->restarts_to_go = cinfo->restart_interval;
717
0
      entropy->next_restart_num++;
718
0
      entropy->next_restart_num &= 7;
719
0
    }
720
0
    entropy->restarts_to_go--;
721
0
  }
722
723
0
  return TRUE;
724
0
}
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
0
{
736
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
737
0
  register int temp;
738
0
  int blkn;
739
0
  int Al = cinfo->Al;
740
0
  JBLOCKROW block;
741
742
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
743
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
744
745
  /* Emit restart marker if needed */
746
0
  if (cinfo->restart_interval)
747
0
    if (entropy->restarts_to_go == 0)
748
0
      emit_restart(entropy, entropy->next_restart_num);
749
750
  /* Encode the MCU data blocks */
751
0
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
752
0
    block = MCU_data[blkn];
753
754
    /* We simply emit the Al'th bit of the DC coefficient value. */
755
0
    temp = (*block)[0];
756
0
    emit_bits(entropy, (unsigned int)(temp >> Al), 1);
757
0
  }
758
759
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
760
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
761
762
  /* Update restart-interval state too */
763
0
  if (cinfo->restart_interval) {
764
0
    if (entropy->restarts_to_go == 0) {
765
0
      entropy->restarts_to_go = cinfo->restart_interval;
766
0
      entropy->next_restart_num++;
767
0
      entropy->next_restart_num &= 7;
768
0
    }
769
0
    entropy->restarts_to_go--;
770
0
  }
771
772
0
  return TRUE;
773
0
}
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
0
#define ENCODE_COEFS_AC_REFINE(label) { \
851
0
  while (zerobits) { \
852
0
    idx = count_zeroes(&zerobits); \
853
0
    r += idx; \
854
0
    cabsvalue += idx; \
855
0
    signbits >>= idx; \
856
0
label \
857
0
    /* Emit any required ZRLs, but not if they can be folded into EOB */ \
858
0
    while (r > 15 && (cabsvalue <= EOBPTR)) { \
859
0
      /* emit any pending EOBRUN and the BE correction bits */ \
860
0
      emit_eobrun(entropy); \
861
0
      /* Emit ZRL */ \
862
0
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
863
0
      r -= 16; \
864
0
      /* Emit buffered correction bits that must be associated with ZRL */ \
865
0
      emit_buffered_bits(entropy, BR_buffer, BR); \
866
0
      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
867
0
      BR = 0; \
868
0
    } \
869
0
    \
870
0
    temp = *cabsvalue++; \
871
0
    \
872
0
    /* If the coef was previously nonzero, it only needs a correction bit. \
873
0
     * NOTE: a straight translation of the spec's figure G.7 would suggest \
874
0
     * that we also need to test r > 15.  But if r > 15, we can only get here \
875
0
     * if k > EOB, which implies that this coefficient is not 1. \
876
0
     */ \
877
0
    if (temp > 1) { \
878
0
      /* The correction bit is the next bit of the absolute value. */ \
879
0
      BR_buffer[BR++] = (char)(temp & 1); \
880
0
      signbits >>= 1; \
881
0
      zerobits >>= 1; \
882
0
      continue; \
883
0
    } \
884
0
    \
885
0
    /* Emit any pending EOBRUN and the BE correction bits */ \
886
0
    emit_eobrun(entropy); \
887
0
    \
888
0
    /* Count/emit Huffman symbol for run length / number of bits */ \
889
0
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); \
890
0
    \
891
0
    /* Emit output bit for newly-nonzero coef */ \
892
0
    temp = signbits & 1; /* ((*block)[jpeg_natural_order_start[k]] < 0) ? 0 : 1 */ \
893
0
    emit_bits(entropy, (unsigned int)temp, 1); \
894
0
    \
895
0
    /* Emit buffered correction bits that must be associated with this code */ \
896
0
    emit_buffered_bits(entropy, BR_buffer, BR); \
897
0
    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
898
0
    BR = 0; \
899
0
    r = 0;                      /* reset zero run length */ \
900
0
    signbits >>= 1; \
901
0
    zerobits >>= 1; \
902
0
  } \
903
0
}
904
905
METHODDEF(boolean)
906
encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
907
0
{
908
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
909
0
  register int temp, r, idx;
910
0
  char *BR_buffer;
911
0
  unsigned int BR;
912
0
  int Sl = cinfo->Se - cinfo->Ss + 1;
913
0
  int Al = cinfo->Al;
914
0
  UJCOEF absvalues_unaligned[DCTSIZE2 + 15];
915
0
  UJCOEF *absvalues;
916
0
  const UJCOEF *cabsvalue, *EOBPTR;
917
0
  size_t zerobits, signbits;
918
0
  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
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
926
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
927
928
  /* Emit restart marker if needed */
929
0
  if (cinfo->restart_interval)
930
0
    if (entropy->restarts_to_go == 0)
931
0
      emit_restart(entropy, entropy->next_restart_num);
932
933
0
#ifdef WITH_SIMD
934
0
  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
0
  EOBPTR = absvalues +
942
0
    entropy->AC_refine_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
943
0
                               Sl, Al, absvalues, bits);
944
945
  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
946
947
0
  r = 0;                        /* r = run length of zeros */
948
0
  BR = 0;                       /* BR = count of buffered bits added now */
949
0
  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
950
951
0
  zerobits = bits[0];
952
0
#if SIZEOF_SIZE_T == 8
953
0
  signbits = bits[1];
954
#else
955
  signbits = bits[2];
956
#endif
957
0
  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
0
  r |= (int)((absvalues + Sl) - cabsvalue);
977
978
0
  if (r > 0 || BR > 0) {        /* If there are trailing zeroes, */
979
0
    entropy->EOBRUN++;          /* count an EOB */
980
0
    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
0
    if (entropy->EOBRUN == 0x7FFF ||
986
0
        entropy->BE > (MAX_CORR_BITS - DCTSIZE2 + 1))
987
0
      emit_eobrun(entropy);
988
0
  }
989
990
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
991
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
992
993
  /* Update restart-interval state too */
994
0
  if (cinfo->restart_interval) {
995
0
    if (entropy->restarts_to_go == 0) {
996
0
      entropy->restarts_to_go = cinfo->restart_interval;
997
0
      entropy->next_restart_num++;
998
0
      entropy->next_restart_num &= 7;
999
0
    }
1000
0
    entropy->restarts_to_go--;
1001
0
  }
1002
1003
0
  return TRUE;
1004
0
}
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
0
{
1014
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
1015
1016
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
1017
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
1018
1019
  /* Flush out any buffered data */
1020
0
  emit_eobrun(entropy);
1021
0
  flush_bits(entropy);
1022
1023
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
1024
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1025
0
}
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
0
{
1035
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
1036
0
  boolean is_DC_band;
1037
0
  int ci, tbl;
1038
0
  jpeg_component_info *compptr;
1039
0
  JHUFF_TBL **htblptr;
1040
0
  boolean did[NUM_HUFF_TBLS];
1041
1042
  /* Flush out buffered data (all we care about is counting the EOB symbol) */
1043
0
  emit_eobrun(entropy);
1044
1045
0
  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
0
  memset(did, 0, sizeof(did));
1051
1052
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1053
0
    compptr = cinfo->cur_comp_info[ci];
1054
0
    if (is_DC_band) {
1055
0
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
1056
0
        continue;
1057
0
      tbl = compptr->dc_tbl_no;
1058
0
    } else {
1059
0
      tbl = compptr->ac_tbl_no;
1060
0
    }
1061
0
    if (!did[tbl]) {
1062
0
      if (is_DC_band)
1063
0
        htblptr = &cinfo->dc_huff_tbl_ptrs[tbl];
1064
0
      else
1065
0
        htblptr = &cinfo->ac_huff_tbl_ptrs[tbl];
1066
0
      if (*htblptr == NULL)
1067
0
        *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
1068
0
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
1069
0
      did[tbl] = TRUE;
1070
0
    }
1071
0
  }
1072
0
}
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
0
{
1082
0
  phuff_entropy_ptr entropy;
1083
0
  int i;
1084
1085
0
  entropy = (phuff_entropy_ptr)
1086
0
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
1087
0
                                sizeof(phuff_entropy_encoder));
1088
0
  cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
1089
0
  entropy->pub.start_pass = start_pass_phuff;
1090
1091
  /* Mark tables unallocated */
1092
0
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
1093
0
    entropy->derived_tbls[i] = NULL;
1094
0
    entropy->count_ptrs[i] = NULL;
1095
0
  }
1096
  entropy->bit_buffer = NULL;   /* needed only in AC refinement scan */
1097
0
}
1098
1099
#endif /* C_PROGRESSIVE_SUPPORTED */