Coverage Report

Created: 2023-06-07 06:03

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