Coverage Report

Created: 2024-05-15 07:08

/src/libjpeg-turbo.main/jchuff.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jchuff.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1997, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright (C) 2009-2011, 2014-2016, 2018-2022, D. R. Commander.
8
 * Copyright (C) 2015, Matthieu Darbois.
9
 * Copyright (C) 2018, Matthias Räncker.
10
 * Copyright (C) 2020, Arm Limited.
11
 * For conditions of distribution and use, see the accompanying README.ijg
12
 * file.
13
 *
14
 * This file contains Huffman entropy encoding routines.
15
 *
16
 * Much of the complexity here has to do with supporting output suspension.
17
 * If the data destination module demands suspension, we want to be able to
18
 * back up to the start of the current MCU.  To do this, we copy state
19
 * variables into local working storage, and update them back to the
20
 * permanent JPEG objects only upon successful completion of an MCU.
21
 *
22
 * NOTE: All referenced figures are from
23
 * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
24
 */
25
26
#define JPEG_INTERNALS
27
#include "jinclude.h"
28
#include "jpeglib.h"
29
#include "jsimd.h"
30
#include <limits.h>
31
32
/*
33
 * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be
34
 * used for bit counting rather than the lookup table.  This will reduce the
35
 * memory footprint by 64k, which is important for some mobile applications
36
 * that create many isolated instances of libjpeg-turbo (web browsers, for
37
 * instance.)  This may improve performance on some mobile platforms as well.
38
 * This feature is enabled by default only on Arm processors, because some x86
39
 * chips have a slow implementation of bsr, and the use of clz/bsr cannot be
40
 * shown to have a significant performance impact even on the x86 chips that
41
 * have a fast implementation of it.  When building for Armv6, you can
42
 * explicitly disable the use of clz/bsr by adding -mthumb to the compiler
43
 * flags (this defines __thumb__).
44
 */
45
46
/* NOTE: Both GCC and Clang define __GNUC__ */
47
#if (defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))) || \
48
    defined(_M_ARM) || defined(_M_ARM64)
49
#if !defined(__thumb__) || defined(__thumb2__)
50
#define USE_CLZ_INTRINSIC
51
#endif
52
#endif
53
54
#ifdef USE_CLZ_INTRINSIC
55
#if defined(_MSC_VER) && !defined(__clang__)
56
#define JPEG_NBITS_NONZERO(x)  (32 - _CountLeadingZeros(x))
57
#else
58
#define JPEG_NBITS_NONZERO(x)  (32 - __builtin_clz(x))
59
#endif
60
#define JPEG_NBITS(x)          (x ? JPEG_NBITS_NONZERO(x) : 0)
61
#else
62
#include "jpeg_nbits_table.h"
63
0
#define JPEG_NBITS(x)          (jpeg_nbits_table[x])
64
0
#define JPEG_NBITS_NONZERO(x)  JPEG_NBITS(x)
65
#endif
66
67
68
/* Expanded entropy encoder object for Huffman encoding.
69
 *
70
 * The savable_state subrecord contains fields that change within an MCU,
71
 * but must not be updated permanently until we complete the MCU.
72
 */
73
74
#if defined(__x86_64__) && defined(__ILP32__)
75
typedef unsigned long long bit_buf_type;
76
#else
77
typedef size_t bit_buf_type;
78
#endif
79
80
/* NOTE: The more optimal Huffman encoding algorithm is only used by the
81
 * intrinsics implementation of the Arm Neon SIMD extensions, which is why we
82
 * retain the old Huffman encoder behavior when using the GAS implementation.
83
 */
84
#if defined(WITH_SIMD) && !(defined(__arm__) || defined(__aarch64__) || \
85
                            defined(_M_ARM) || defined(_M_ARM64))
86
typedef unsigned long long simd_bit_buf_type;
87
#else
88
typedef bit_buf_type simd_bit_buf_type;
89
#endif
90
91
#if (defined(SIZEOF_SIZE_T) && SIZEOF_SIZE_T == 8) || defined(_WIN64) || \
92
    (defined(__x86_64__) && defined(__ILP32__))
93
0
#define BIT_BUF_SIZE  64
94
#elif (defined(SIZEOF_SIZE_T) && SIZEOF_SIZE_T == 4) || defined(_WIN32)
95
#define BIT_BUF_SIZE  32
96
#else
97
#error Cannot determine word size
98
#endif
99
0
#define SIMD_BIT_BUF_SIZE  (sizeof(simd_bit_buf_type) * 8)
100
101
typedef struct {
102
  union {
103
    bit_buf_type c;
104
    simd_bit_buf_type simd;
105
  } put_buffer;                         /* current bit accumulation buffer */
106
  int free_bits;                        /* # of bits available in it */
107
                                        /* (Neon GAS: # of bits now in it) */
108
  int last_dc_val[MAX_COMPS_IN_SCAN];   /* last DC coef for each component */
109
} savable_state;
110
111
typedef struct {
112
  struct jpeg_entropy_encoder pub; /* public fields */
113
114
  savable_state saved;          /* Bit buffer & DC state at start of MCU */
115
116
  /* These fields are NOT loaded into local working state. */
117
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
118
  int next_restart_num;         /* next restart number to write (0-7) */
119
120
  /* Pointers to derived tables (these workspaces have image lifespan) */
121
  c_derived_tbl *dc_derived_tbls[NUM_HUFF_TBLS];
122
  c_derived_tbl *ac_derived_tbls[NUM_HUFF_TBLS];
123
124
#ifdef ENTROPY_OPT_SUPPORTED    /* Statistics tables for optimization */
125
  long *dc_count_ptrs[NUM_HUFF_TBLS];
126
  long *ac_count_ptrs[NUM_HUFF_TBLS];
127
#endif
128
129
  int simd;
130
} huff_entropy_encoder;
131
132
typedef huff_entropy_encoder *huff_entropy_ptr;
133
134
/* Working state while writing an MCU.
135
 * This struct contains all the fields that are needed by subroutines.
136
 */
137
138
typedef struct {
139
  JOCTET *next_output_byte;     /* => next byte to write in buffer */
140
  size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
141
  savable_state cur;            /* Current bit buffer & DC state */
142
  j_compress_ptr cinfo;         /* dump_buffer needs access to this */
143
  int simd;
144
} working_state;
145
146
147
/* Forward declarations */
148
METHODDEF(boolean) encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data);
149
METHODDEF(void) finish_pass_huff(j_compress_ptr cinfo);
150
#ifdef ENTROPY_OPT_SUPPORTED
151
METHODDEF(boolean) encode_mcu_gather(j_compress_ptr cinfo,
152
                                     JBLOCKROW *MCU_data);
153
METHODDEF(void) finish_pass_gather(j_compress_ptr cinfo);
154
#endif
155
156
157
/*
158
 * Initialize for a Huffman-compressed scan.
159
 * If gather_statistics is TRUE, we do not output anything during the scan,
160
 * just count the Huffman symbols used and generate Huffman code tables.
161
 */
162
163
METHODDEF(void)
164
start_pass_huff(j_compress_ptr cinfo, boolean gather_statistics)
165
0
{
166
0
  huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
167
0
  int ci, dctbl, actbl;
168
0
  jpeg_component_info *compptr;
169
170
0
  if (gather_statistics) {
171
0
#ifdef ENTROPY_OPT_SUPPORTED
172
0
    entropy->pub.encode_mcu = encode_mcu_gather;
173
0
    entropy->pub.finish_pass = finish_pass_gather;
174
#else
175
    ERREXIT(cinfo, JERR_NOT_COMPILED);
176
#endif
177
0
  } else {
178
0
    entropy->pub.encode_mcu = encode_mcu_huff;
179
0
    entropy->pub.finish_pass = finish_pass_huff;
180
0
  }
181
182
0
  entropy->simd = jsimd_can_huff_encode_one_block();
183
184
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
185
0
    compptr = cinfo->cur_comp_info[ci];
186
0
    dctbl = compptr->dc_tbl_no;
187
0
    actbl = compptr->ac_tbl_no;
188
0
    if (gather_statistics) {
189
0
#ifdef ENTROPY_OPT_SUPPORTED
190
      /* Check for invalid table indexes */
191
      /* (make_c_derived_tbl does this in the other path) */
192
0
      if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
193
0
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
194
0
      if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
195
0
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
196
      /* Allocate and zero the statistics tables */
197
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
198
0
      if (entropy->dc_count_ptrs[dctbl] == NULL)
199
0
        entropy->dc_count_ptrs[dctbl] = (long *)
200
0
          (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
201
0
                                      257 * sizeof(long));
202
0
      memset(entropy->dc_count_ptrs[dctbl], 0, 257 * sizeof(long));
203
0
      if (entropy->ac_count_ptrs[actbl] == NULL)
204
0
        entropy->ac_count_ptrs[actbl] = (long *)
205
0
          (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
206
0
                                      257 * sizeof(long));
207
0
      memset(entropy->ac_count_ptrs[actbl], 0, 257 * sizeof(long));
208
0
#endif
209
0
    } else {
210
      /* Compute derived values for Huffman tables */
211
      /* We may do this more than once for a table, but it's not expensive */
212
0
      jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
213
0
                              &entropy->dc_derived_tbls[dctbl]);
214
0
      jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
215
0
                              &entropy->ac_derived_tbls[actbl]);
216
0
    }
217
    /* Initialize DC predictions to 0 */
218
0
    entropy->saved.last_dc_val[ci] = 0;
219
0
  }
220
221
  /* Initialize bit buffer to empty */
222
0
  if (entropy->simd) {
223
0
    entropy->saved.put_buffer.simd = 0;
224
#if defined(__aarch64__) && !defined(NEON_INTRINSICS)
225
    entropy->saved.free_bits = 0;
226
#else
227
0
    entropy->saved.free_bits = SIMD_BIT_BUF_SIZE;
228
0
#endif
229
0
  } else {
230
0
    entropy->saved.put_buffer.c = 0;
231
0
    entropy->saved.free_bits = BIT_BUF_SIZE;
232
0
  }
233
234
  /* Initialize restart stuff */
235
0
  entropy->restarts_to_go = cinfo->restart_interval;
236
0
  entropy->next_restart_num = 0;
237
0
}
238
239
240
/*
241
 * Compute the derived values for a Huffman table.
242
 * This routine also performs some validation checks on the table.
243
 *
244
 * Note this is also used by jcphuff.c.
245
 */
246
247
GLOBAL(void)
248
jpeg_make_c_derived_tbl(j_compress_ptr cinfo, boolean isDC, int tblno,
249
                        c_derived_tbl **pdtbl)
250
0
{
251
0
  JHUFF_TBL *htbl;
252
0
  c_derived_tbl *dtbl;
253
0
  int p, i, l, lastp, si, maxsymbol;
254
0
  char huffsize[257];
255
0
  unsigned int huffcode[257];
256
0
  unsigned int code;
257
258
  /* Note that huffsize[] and huffcode[] are filled in code-length order,
259
   * paralleling the order of the symbols themselves in htbl->huffval[].
260
   */
261
262
  /* Find the input Huffman table */
263
0
  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
264
0
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
265
0
  htbl =
266
0
    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
267
0
  if (htbl == NULL)
268
0
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
269
270
  /* Allocate a workspace if we haven't already done so. */
271
0
  if (*pdtbl == NULL)
272
0
    *pdtbl = (c_derived_tbl *)
273
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
274
0
                                  sizeof(c_derived_tbl));
275
0
  dtbl = *pdtbl;
276
277
  /* Figure C.1: make table of Huffman code length for each symbol */
278
279
0
  p = 0;
280
0
  for (l = 1; l <= 16; l++) {
281
0
    i = (int)htbl->bits[l];
282
0
    if (i < 0 || p + i > 256)   /* protect against table overrun */
283
0
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
284
0
    while (i--)
285
0
      huffsize[p++] = (char)l;
286
0
  }
287
0
  huffsize[p] = 0;
288
0
  lastp = p;
289
290
  /* Figure C.2: generate the codes themselves */
291
  /* We also validate that the counts represent a legal Huffman code tree. */
292
293
0
  code = 0;
294
0
  si = huffsize[0];
295
0
  p = 0;
296
0
  while (huffsize[p]) {
297
0
    while (((int)huffsize[p]) == si) {
298
0
      huffcode[p++] = code;
299
0
      code++;
300
0
    }
301
    /* code is now 1 more than the last code used for codelength si; but
302
     * it must still fit in si bits, since no code is allowed to be all ones.
303
     */
304
0
    if (((JLONG)code) >= (((JLONG)1) << si))
305
0
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
306
0
    code <<= 1;
307
0
    si++;
308
0
  }
309
310
  /* Figure C.3: generate encoding tables */
311
  /* These are code and size indexed by symbol value */
312
313
  /* Set all codeless symbols to have code length 0;
314
   * this lets us detect duplicate VAL entries here, and later
315
   * allows emit_bits to detect any attempt to emit such symbols.
316
   */
317
0
  memset(dtbl->ehufco, 0, sizeof(dtbl->ehufco));
318
0
  memset(dtbl->ehufsi, 0, sizeof(dtbl->ehufsi));
319
320
  /* This is also a convenient place to check for out-of-range
321
   * and duplicated VAL entries.  We allow 0..255 for AC symbols
322
   * but only 0..15 for DC.  (We could constrain them further
323
   * based on data depth and mode, but this seems enough.)
324
   */
325
0
  maxsymbol = isDC ? 15 : 255;
326
327
0
  for (p = 0; p < lastp; p++) {
328
0
    i = htbl->huffval[p];
329
0
    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
330
0
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
331
0
    dtbl->ehufco[i] = huffcode[p];
332
0
    dtbl->ehufsi[i] = huffsize[p];
333
0
  }
334
0
}
335
336
337
/* Outputting bytes to the file */
338
339
/* Emit a byte, taking 'action' if must suspend. */
340
0
#define emit_byte(state, val, action) { \
341
0
  *(state)->next_output_byte++ = (JOCTET)(val); \
342
0
  if (--(state)->free_in_buffer == 0) \
343
0
    if (!dump_buffer(state)) \
344
0
      { action; } \
345
0
}
346
347
348
LOCAL(boolean)
349
dump_buffer(working_state *state)
350
/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
351
0
{
352
0
  struct jpeg_destination_mgr *dest = state->cinfo->dest;
353
354
0
  if (!(*dest->empty_output_buffer) (state->cinfo))
355
0
    return FALSE;
356
  /* After a successful buffer dump, must reset buffer pointers */
357
0
  state->next_output_byte = dest->next_output_byte;
358
0
  state->free_in_buffer = dest->free_in_buffer;
359
0
  return TRUE;
360
0
}
361
362
363
/* Outputting bits to the file */
364
365
/* Output byte b and, speculatively, an additional 0 byte.  0xFF must be
366
 * encoded as 0xFF 0x00, so the output buffer pointer is advanced by 2 if the
367
 * byte is 0xFF.  Otherwise, the output buffer pointer is advanced by 1, and
368
 * the speculative 0 byte will be overwritten by the next byte.
369
 */
370
0
#define EMIT_BYTE(b) { \
371
0
  buffer[0] = (JOCTET)(b); \
372
0
  buffer[1] = 0; \
373
0
  buffer -= -2 + ((JOCTET)(b) < 0xFF); \
374
0
}
375
376
/* Output the entire bit buffer.  If there are no 0xFF bytes in it, then write
377
 * directly to the output buffer.  Otherwise, use the EMIT_BYTE() macro to
378
 * encode 0xFF as 0xFF 0x00.
379
 */
380
#if BIT_BUF_SIZE == 64
381
382
0
#define FLUSH() { \
383
0
  if (put_buffer & 0x8080808080808080 & ~(put_buffer + 0x0101010101010101)) { \
384
0
    EMIT_BYTE(put_buffer >> 56) \
385
0
    EMIT_BYTE(put_buffer >> 48) \
386
0
    EMIT_BYTE(put_buffer >> 40) \
387
0
    EMIT_BYTE(put_buffer >> 32) \
388
0
    EMIT_BYTE(put_buffer >> 24) \
389
0
    EMIT_BYTE(put_buffer >> 16) \
390
0
    EMIT_BYTE(put_buffer >>  8) \
391
0
    EMIT_BYTE(put_buffer      ) \
392
0
  } else { \
393
0
    buffer[0] = (JOCTET)(put_buffer >> 56); \
394
0
    buffer[1] = (JOCTET)(put_buffer >> 48); \
395
0
    buffer[2] = (JOCTET)(put_buffer >> 40); \
396
0
    buffer[3] = (JOCTET)(put_buffer >> 32); \
397
0
    buffer[4] = (JOCTET)(put_buffer >> 24); \
398
0
    buffer[5] = (JOCTET)(put_buffer >> 16); \
399
0
    buffer[6] = (JOCTET)(put_buffer >> 8); \
400
0
    buffer[7] = (JOCTET)(put_buffer); \
401
0
    buffer += 8; \
402
0
  } \
403
0
}
404
405
#else
406
407
#define FLUSH() { \
408
  if (put_buffer & 0x80808080 & ~(put_buffer + 0x01010101)) { \
409
    EMIT_BYTE(put_buffer >> 24) \
410
    EMIT_BYTE(put_buffer >> 16) \
411
    EMIT_BYTE(put_buffer >>  8) \
412
    EMIT_BYTE(put_buffer      ) \
413
  } else { \
414
    buffer[0] = (JOCTET)(put_buffer >> 24); \
415
    buffer[1] = (JOCTET)(put_buffer >> 16); \
416
    buffer[2] = (JOCTET)(put_buffer >> 8); \
417
    buffer[3] = (JOCTET)(put_buffer); \
418
    buffer += 4; \
419
  } \
420
}
421
422
#endif
423
424
/* Fill the bit buffer to capacity with the leading bits from code, then output
425
 * the bit buffer and put the remaining bits from code into the bit buffer.
426
 */
427
0
#define PUT_AND_FLUSH(code, size) { \
428
0
  put_buffer = (put_buffer << (size + free_bits)) | (code >> -free_bits); \
429
0
  FLUSH() \
430
0
  free_bits += BIT_BUF_SIZE; \
431
0
  put_buffer = code; \
432
0
}
433
434
/* Insert code into the bit buffer and output the bit buffer if needed.
435
 * NOTE: We can't flush with free_bits == 0, since the left shift in
436
 * PUT_AND_FLUSH() would have undefined behavior.
437
 */
438
0
#define PUT_BITS(code, size) { \
439
0
  free_bits -= size; \
440
0
  if (free_bits < 0) \
441
0
    PUT_AND_FLUSH(code, size) \
442
0
  else \
443
0
    put_buffer = (put_buffer << size) | code; \
444
0
}
445
446
0
#define PUT_CODE(code, size) { \
447
0
  temp &= (((JLONG)1) << nbits) - 1; \
448
0
  temp |= code << nbits; \
449
0
  nbits += size; \
450
0
  PUT_BITS(temp, nbits) \
451
0
}
452
453
454
/* Although it is exceedingly rare, it is possible for a Huffman-encoded
455
 * coefficient block to be larger than the 128-byte unencoded block.  For each
456
 * of the 64 coefficients, PUT_BITS is invoked twice, and each invocation can
457
 * theoretically store 16 bits (for a maximum of 2048 bits or 256 bytes per
458
 * encoded block.)  If, for instance, one artificially sets the AC
459
 * coefficients to alternating values of 32767 and -32768 (using the JPEG
460
 * scanning order-- 1, 8, 16, etc.), then this will produce an encoded block
461
 * larger than 200 bytes.
462
 */
463
0
#define BUFSIZE  (DCTSIZE2 * 8)
464
465
0
#define LOAD_BUFFER() { \
466
0
  if (state->free_in_buffer < BUFSIZE) { \
467
0
    localbuf = 1; \
468
0
    buffer = _buffer; \
469
0
  } else \
470
0
    buffer = state->next_output_byte; \
471
0
}
472
473
0
#define STORE_BUFFER() { \
474
0
  if (localbuf) { \
475
0
    size_t bytes, bytestocopy; \
476
0
    bytes = buffer - _buffer; \
477
0
    buffer = _buffer; \
478
0
    while (bytes > 0) { \
479
0
      bytestocopy = MIN(bytes, state->free_in_buffer); \
480
0
      memcpy(state->next_output_byte, buffer, bytestocopy); \
481
0
      state->next_output_byte += bytestocopy; \
482
0
      buffer += bytestocopy; \
483
0
      state->free_in_buffer -= bytestocopy; \
484
0
      if (state->free_in_buffer == 0) \
485
0
        if (!dump_buffer(state)) return FALSE; \
486
0
      bytes -= bytestocopy; \
487
0
    } \
488
0
  } else { \
489
0
    state->free_in_buffer -= (buffer - state->next_output_byte); \
490
0
    state->next_output_byte = buffer; \
491
0
  } \
492
0
}
493
494
495
LOCAL(boolean)
496
flush_bits(working_state *state)
497
0
{
498
0
  JOCTET _buffer[BUFSIZE], *buffer, temp;
499
0
  simd_bit_buf_type put_buffer;  int put_bits;
500
0
  int localbuf = 0;
501
502
0
  if (state->simd) {
503
#if defined(__aarch64__) && !defined(NEON_INTRINSICS)
504
    put_bits = state->cur.free_bits;
505
#else
506
0
    put_bits = SIMD_BIT_BUF_SIZE - state->cur.free_bits;
507
0
#endif
508
0
    put_buffer = state->cur.put_buffer.simd;
509
0
  } else {
510
0
    put_bits = BIT_BUF_SIZE - state->cur.free_bits;
511
0
    put_buffer = state->cur.put_buffer.c;
512
0
  }
513
514
0
  LOAD_BUFFER()
515
516
0
  while (put_bits >= 8) {
517
0
    put_bits -= 8;
518
0
    temp = (JOCTET)(put_buffer >> put_bits);
519
0
    EMIT_BYTE(temp)
520
0
  }
521
0
  if (put_bits) {
522
    /* fill partial byte with ones */
523
0
    temp = (JOCTET)((put_buffer << (8 - put_bits)) | (0xFF >> put_bits));
524
0
    EMIT_BYTE(temp)
525
0
  }
526
527
0
  if (state->simd) {                    /* and reset bit buffer to empty */
528
0
    state->cur.put_buffer.simd = 0;
529
#if defined(__aarch64__) && !defined(NEON_INTRINSICS)
530
    state->cur.free_bits = 0;
531
#else
532
0
    state->cur.free_bits = SIMD_BIT_BUF_SIZE;
533
0
#endif
534
0
  } else {
535
0
    state->cur.put_buffer.c = 0;
536
0
    state->cur.free_bits = BIT_BUF_SIZE;
537
0
  }
538
0
  STORE_BUFFER()
539
540
0
  return TRUE;
541
0
}
542
543
544
/* Encode a single block's worth of coefficients */
545
546
LOCAL(boolean)
547
encode_one_block_simd(working_state *state, JCOEFPTR block, int last_dc_val,
548
                      c_derived_tbl *dctbl, c_derived_tbl *actbl)
549
0
{
550
0
  JOCTET _buffer[BUFSIZE], *buffer;
551
0
  int localbuf = 0;
552
553
0
  LOAD_BUFFER()
554
555
0
  buffer = jsimd_huff_encode_one_block(state, buffer, block, last_dc_val,
556
0
                                       dctbl, actbl);
557
558
0
  STORE_BUFFER()
559
560
0
  return TRUE;
561
0
}
562
563
LOCAL(boolean)
564
encode_one_block(working_state *state, JCOEFPTR block, int last_dc_val,
565
                 c_derived_tbl *dctbl, c_derived_tbl *actbl)
566
0
{
567
0
  int temp, nbits, free_bits;
568
0
  bit_buf_type put_buffer;
569
0
  JOCTET _buffer[BUFSIZE], *buffer;
570
0
  int localbuf = 0;
571
572
0
  free_bits = state->cur.free_bits;
573
0
  put_buffer = state->cur.put_buffer.c;
574
0
  LOAD_BUFFER()
575
576
  /* Encode the DC coefficient difference per section F.1.2.1 */
577
578
0
  temp = block[0] - last_dc_val;
579
580
  /* This is a well-known technique for obtaining the absolute value without a
581
   * branch.  It is derived from an assembly language technique presented in
582
   * "How to Optimize for the Pentium Processors", Copyright (c) 1996, 1997 by
583
   * Agner Fog.  This code assumes we are on a two's complement machine.
584
   */
585
0
  nbits = temp >> (CHAR_BIT * sizeof(int) - 1);
586
0
  temp += nbits;
587
0
  nbits ^= temp;
588
589
  /* Find the number of bits needed for the magnitude of the coefficient */
590
0
  nbits = JPEG_NBITS(nbits);
591
592
  /* Emit the Huffman-coded symbol for the number of bits.
593
   * Emit that number of bits of the value, if positive,
594
   * or the complement of its magnitude, if negative.
595
   */
596
0
  PUT_CODE(dctbl->ehufco[nbits], dctbl->ehufsi[nbits])
597
598
  /* Encode the AC coefficients per section F.1.2.2 */
599
600
0
  {
601
0
    int r = 0;                  /* r = run length of zeros */
602
603
/* Manually unroll the k loop to eliminate the counter variable.  This
604
 * improves performance greatly on systems with a limited number of
605
 * registers (such as x86.)
606
 */
607
0
#define kloop(jpeg_natural_order_of_k) { \
608
0
  if ((temp = block[jpeg_natural_order_of_k]) == 0) { \
609
0
    r += 16; \
610
0
  } else { \
611
    /* Branch-less absolute value, bitwise complement, etc., same as above */ \
612
0
    nbits = temp >> (CHAR_BIT * sizeof(int) - 1); \
613
0
    temp += nbits; \
614
0
    nbits ^= temp; \
615
0
    nbits = JPEG_NBITS_NONZERO(nbits); \
616
    /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \
617
0
    while (r >= 16 * 16) { \
618
0
      r -= 16 * 16; \
619
0
      PUT_BITS(actbl->ehufco[0xf0], actbl->ehufsi[0xf0]) \
620
0
    } \
621
    /* Emit Huffman symbol for run length / number of bits */ \
622
0
    r += nbits; \
623
0
    PUT_CODE(actbl->ehufco[r], actbl->ehufsi[r]) \
624
0
    r = 0; \
625
0
  } \
626
0
}
627
628
    /* One iteration for each value in jpeg_natural_order[] */
629
0
    kloop(1);   kloop(8);   kloop(16);  kloop(9);   kloop(2);   kloop(3);
630
0
    kloop(10);  kloop(17);  kloop(24);  kloop(32);  kloop(25);  kloop(18);
631
0
    kloop(11);  kloop(4);   kloop(5);   kloop(12);  kloop(19);  kloop(26);
632
0
    kloop(33);  kloop(40);  kloop(48);  kloop(41);  kloop(34);  kloop(27);
633
0
    kloop(20);  kloop(13);  kloop(6);   kloop(7);   kloop(14);  kloop(21);
634
0
    kloop(28);  kloop(35);  kloop(42);  kloop(49);  kloop(56);  kloop(57);
635
0
    kloop(50);  kloop(43);  kloop(36);  kloop(29);  kloop(22);  kloop(15);
636
0
    kloop(23);  kloop(30);  kloop(37);  kloop(44);  kloop(51);  kloop(58);
637
0
    kloop(59);  kloop(52);  kloop(45);  kloop(38);  kloop(31);  kloop(39);
638
0
    kloop(46);  kloop(53);  kloop(60);  kloop(61);  kloop(54);  kloop(47);
639
0
    kloop(55);  kloop(62);  kloop(63);
640
641
    /* If the last coef(s) were zero, emit an end-of-block code */
642
0
    if (r > 0) {
643
0
      PUT_BITS(actbl->ehufco[0], actbl->ehufsi[0])
644
0
    }
645
0
  }
646
647
0
  state->cur.put_buffer.c = put_buffer;
648
0
  state->cur.free_bits = free_bits;
649
0
  STORE_BUFFER()
650
651
0
  return TRUE;
652
0
}
653
654
655
/*
656
 * Emit a restart marker & resynchronize predictions.
657
 */
658
659
LOCAL(boolean)
660
emit_restart(working_state *state, int restart_num)
661
0
{
662
0
  int ci;
663
664
0
  if (!flush_bits(state))
665
0
    return FALSE;
666
667
0
  emit_byte(state, 0xFF, return FALSE);
668
0
  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
669
670
  /* Re-initialize DC predictions to 0 */
671
0
  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
672
0
    state->cur.last_dc_val[ci] = 0;
673
674
  /* The restart counter is not updated until we successfully write the MCU. */
675
676
0
  return TRUE;
677
0
}
678
679
680
/*
681
 * Encode and output one MCU's worth of Huffman-compressed coefficients.
682
 */
683
684
METHODDEF(boolean)
685
encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
686
0
{
687
0
  huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
688
0
  working_state state;
689
0
  int blkn, ci;
690
0
  jpeg_component_info *compptr;
691
692
  /* Load up working state */
693
0
  state.next_output_byte = cinfo->dest->next_output_byte;
694
0
  state.free_in_buffer = cinfo->dest->free_in_buffer;
695
0
  state.cur = entropy->saved;
696
0
  state.cinfo = cinfo;
697
0
  state.simd = entropy->simd;
698
699
  /* Emit restart marker if needed */
700
0
  if (cinfo->restart_interval) {
701
0
    if (entropy->restarts_to_go == 0)
702
0
      if (!emit_restart(&state, entropy->next_restart_num))
703
0
        return FALSE;
704
0
  }
705
706
  /* Encode the MCU data blocks */
707
0
  if (entropy->simd) {
708
0
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
709
0
      ci = cinfo->MCU_membership[blkn];
710
0
      compptr = cinfo->cur_comp_info[ci];
711
0
      if (!encode_one_block_simd(&state,
712
0
                                 MCU_data[blkn][0], state.cur.last_dc_val[ci],
713
0
                                 entropy->dc_derived_tbls[compptr->dc_tbl_no],
714
0
                                 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
715
0
        return FALSE;
716
      /* Update last_dc_val */
717
0
      state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
718
0
    }
719
0
  } else {
720
0
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
721
0
      ci = cinfo->MCU_membership[blkn];
722
0
      compptr = cinfo->cur_comp_info[ci];
723
0
      if (!encode_one_block(&state,
724
0
                            MCU_data[blkn][0], state.cur.last_dc_val[ci],
725
0
                            entropy->dc_derived_tbls[compptr->dc_tbl_no],
726
0
                            entropy->ac_derived_tbls[compptr->ac_tbl_no]))
727
0
        return FALSE;
728
      /* Update last_dc_val */
729
0
      state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
730
0
    }
731
0
  }
732
733
  /* Completed MCU, so update state */
734
0
  cinfo->dest->next_output_byte = state.next_output_byte;
735
0
  cinfo->dest->free_in_buffer = state.free_in_buffer;
736
0
  entropy->saved = state.cur;
737
738
  /* Update restart-interval state too */
739
0
  if (cinfo->restart_interval) {
740
0
    if (entropy->restarts_to_go == 0) {
741
0
      entropy->restarts_to_go = cinfo->restart_interval;
742
0
      entropy->next_restart_num++;
743
0
      entropy->next_restart_num &= 7;
744
0
    }
745
0
    entropy->restarts_to_go--;
746
0
  }
747
748
0
  return TRUE;
749
0
}
750
751
752
/*
753
 * Finish up at the end of a Huffman-compressed scan.
754
 */
755
756
METHODDEF(void)
757
finish_pass_huff(j_compress_ptr cinfo)
758
0
{
759
0
  huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
760
0
  working_state state;
761
762
  /* Load up working state ... flush_bits needs it */
763
0
  state.next_output_byte = cinfo->dest->next_output_byte;
764
0
  state.free_in_buffer = cinfo->dest->free_in_buffer;
765
0
  state.cur = entropy->saved;
766
0
  state.cinfo = cinfo;
767
0
  state.simd = entropy->simd;
768
769
  /* Flush out the last data */
770
0
  if (!flush_bits(&state))
771
0
    ERREXIT(cinfo, JERR_CANT_SUSPEND);
772
773
  /* Update state */
774
0
  cinfo->dest->next_output_byte = state.next_output_byte;
775
0
  cinfo->dest->free_in_buffer = state.free_in_buffer;
776
0
  entropy->saved = state.cur;
777
0
}
778
779
780
/*
781
 * Huffman coding optimization.
782
 *
783
 * We first scan the supplied data and count the number of uses of each symbol
784
 * that is to be Huffman-coded. (This process MUST agree with the code above.)
785
 * Then we build a Huffman coding tree for the observed counts.
786
 * Symbols which are not needed at all for the particular image are not
787
 * assigned any code, which saves space in the DHT marker as well as in
788
 * the compressed data.
789
 */
790
791
#ifdef ENTROPY_OPT_SUPPORTED
792
793
794
/* Process a single block's worth of coefficients */
795
796
LOCAL(void)
797
htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
798
                long dc_counts[], long ac_counts[])
799
0
{
800
0
  register int temp;
801
0
  register int nbits;
802
0
  register int k, r;
803
804
  /* Encode the DC coefficient difference per section F.1.2.1 */
805
806
0
  temp = block[0] - last_dc_val;
807
0
  if (temp < 0)
808
0
    temp = -temp;
809
810
  /* Find the number of bits needed for the magnitude of the coefficient */
811
0
  nbits = 0;
812
0
  while (temp) {
813
0
    nbits++;
814
0
    temp >>= 1;
815
0
  }
816
  /* Check for out-of-range coefficient values.
817
   * Since we're encoding a difference, the range limit is twice as much.
818
   */
819
0
  if (nbits > MAX_COEF_BITS + 1)
820
0
    ERREXIT(cinfo, JERR_BAD_DCT_COEF);
821
822
  /* Count the Huffman symbol for the number of bits */
823
0
  dc_counts[nbits]++;
824
825
  /* Encode the AC coefficients per section F.1.2.2 */
826
827
0
  r = 0;                        /* r = run length of zeros */
828
829
0
  for (k = 1; k < DCTSIZE2; k++) {
830
0
    if ((temp = block[jpeg_natural_order[k]]) == 0) {
831
0
      r++;
832
0
    } else {
833
      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
834
0
      while (r > 15) {
835
0
        ac_counts[0xF0]++;
836
0
        r -= 16;
837
0
      }
838
839
      /* Find the number of bits needed for the magnitude of the coefficient */
840
0
      if (temp < 0)
841
0
        temp = -temp;
842
843
      /* Find the number of bits needed for the magnitude of the coefficient */
844
0
      nbits = 1;                /* there must be at least one 1 bit */
845
0
      while ((temp >>= 1))
846
0
        nbits++;
847
      /* Check for out-of-range coefficient values */
848
0
      if (nbits > MAX_COEF_BITS)
849
0
        ERREXIT(cinfo, JERR_BAD_DCT_COEF);
850
851
      /* Count Huffman symbol for run length / number of bits */
852
0
      ac_counts[(r << 4) + nbits]++;
853
854
0
      r = 0;
855
0
    }
856
0
  }
857
858
  /* If the last coef(s) were zero, emit an end-of-block code */
859
0
  if (r > 0)
860
0
    ac_counts[0]++;
861
0
}
862
863
864
/*
865
 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
866
 * No data is actually output, so no suspension return is possible.
867
 */
868
869
METHODDEF(boolean)
870
encode_mcu_gather(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
871
0
{
872
0
  huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
873
0
  int blkn, ci;
874
0
  jpeg_component_info *compptr;
875
876
  /* Take care of restart intervals if needed */
877
0
  if (cinfo->restart_interval) {
878
0
    if (entropy->restarts_to_go == 0) {
879
      /* Re-initialize DC predictions to 0 */
880
0
      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
881
0
        entropy->saved.last_dc_val[ci] = 0;
882
      /* Update restart state */
883
0
      entropy->restarts_to_go = cinfo->restart_interval;
884
0
    }
885
0
    entropy->restarts_to_go--;
886
0
  }
887
888
0
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
889
0
    ci = cinfo->MCU_membership[blkn];
890
0
    compptr = cinfo->cur_comp_info[ci];
891
0
    htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
892
0
                    entropy->dc_count_ptrs[compptr->dc_tbl_no],
893
0
                    entropy->ac_count_ptrs[compptr->ac_tbl_no]);
894
0
    entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
895
0
  }
896
897
0
  return TRUE;
898
0
}
899
900
901
/*
902
 * Generate the best Huffman code table for the given counts, fill htbl.
903
 * Note this is also used by jcphuff.c.
904
 *
905
 * The JPEG standard requires that no symbol be assigned a codeword of all
906
 * one bits (so that padding bits added at the end of a compressed segment
907
 * can't look like a valid code).  Because of the canonical ordering of
908
 * codewords, this just means that there must be an unused slot in the
909
 * longest codeword length category.  Annex K (Clause K.2) of
910
 * Rec. ITU-T T.81 (1992) | ISO/IEC 10918-1:1994 suggests reserving such a slot
911
 * by pretending that symbol 256 is a valid symbol with count 1.  In theory
912
 * that's not optimal; giving it count zero but including it in the symbol set
913
 * anyway should give a better Huffman code.  But the theoretically better code
914
 * actually seems to come out worse in practice, because it produces more
915
 * all-ones bytes (which incur stuffed zero bytes in the final file).  In any
916
 * case the difference is tiny.
917
 *
918
 * The JPEG standard requires Huffman codes to be no more than 16 bits long.
919
 * If some symbols have a very small but nonzero probability, the Huffman tree
920
 * must be adjusted to meet the code length restriction.  We currently use
921
 * the adjustment method suggested in JPEG section K.2.  This method is *not*
922
 * optimal; it may not choose the best possible limited-length code.  But
923
 * typically only very-low-frequency symbols will be given less-than-optimal
924
 * lengths, so the code is almost optimal.  Experimental comparisons against
925
 * an optimal limited-length-code algorithm indicate that the difference is
926
 * microscopic --- usually less than a hundredth of a percent of total size.
927
 * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
928
 */
929
930
GLOBAL(void)
931
jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
932
0
{
933
0
#define MAX_CLEN  32            /* assumed maximum initial code length */
934
0
  UINT8 bits[MAX_CLEN + 1];     /* bits[k] = # of symbols with code length k */
935
0
  int codesize[257];            /* codesize[k] = code length of symbol k */
936
0
  int others[257];              /* next symbol in current branch of tree */
937
0
  int c1, c2;
938
0
  int p, i, j;
939
0
  long v;
940
941
  /* This algorithm is explained in section K.2 of the JPEG standard */
942
943
0
  memset(bits, 0, sizeof(bits));
944
0
  memset(codesize, 0, sizeof(codesize));
945
0
  for (i = 0; i < 257; i++)
946
0
    others[i] = -1;             /* init links to empty */
947
948
0
  freq[256] = 1;                /* make sure 256 has a nonzero count */
949
  /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
950
   * that no real symbol is given code-value of all ones, because 256
951
   * will be placed last in the largest codeword category.
952
   */
953
954
  /* Huffman's basic algorithm to assign optimal code lengths to symbols */
955
956
0
  for (;;) {
957
    /* Find the smallest nonzero frequency, set c1 = its symbol */
958
    /* In case of ties, take the larger symbol number */
959
0
    c1 = -1;
960
0
    v = 1000000000L;
961
0
    for (i = 0; i <= 256; i++) {
962
0
      if (freq[i] && freq[i] <= v) {
963
0
        v = freq[i];
964
0
        c1 = i;
965
0
      }
966
0
    }
967
968
    /* Find the next smallest nonzero frequency, set c2 = its symbol */
969
    /* In case of ties, take the larger symbol number */
970
0
    c2 = -1;
971
0
    v = 1000000000L;
972
0
    for (i = 0; i <= 256; i++) {
973
0
      if (freq[i] && freq[i] <= v && i != c1) {
974
0
        v = freq[i];
975
0
        c2 = i;
976
0
      }
977
0
    }
978
979
    /* Done if we've merged everything into one frequency */
980
0
    if (c2 < 0)
981
0
      break;
982
983
    /* Else merge the two counts/trees */
984
0
    freq[c1] += freq[c2];
985
0
    freq[c2] = 0;
986
987
    /* Increment the codesize of everything in c1's tree branch */
988
0
    codesize[c1]++;
989
0
    while (others[c1] >= 0) {
990
0
      c1 = others[c1];
991
0
      codesize[c1]++;
992
0
    }
993
994
0
    others[c1] = c2;            /* chain c2 onto c1's tree branch */
995
996
    /* Increment the codesize of everything in c2's tree branch */
997
0
    codesize[c2]++;
998
0
    while (others[c2] >= 0) {
999
0
      c2 = others[c2];
1000
0
      codesize[c2]++;
1001
0
    }
1002
0
  }
1003
1004
  /* Now count the number of symbols of each code length */
1005
0
  for (i = 0; i <= 256; i++) {
1006
0
    if (codesize[i]) {
1007
      /* The JPEG standard seems to think that this can't happen, */
1008
      /* but I'm paranoid... */
1009
0
      if (codesize[i] > MAX_CLEN)
1010
0
        ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
1011
1012
0
      bits[codesize[i]]++;
1013
0
    }
1014
0
  }
1015
1016
  /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
1017
   * Huffman procedure assigned any such lengths, we must adjust the coding.
1018
   * Here is what Rec. ITU-T T.81 | ISO/IEC 10918-1 says about how this next
1019
   * bit works: Since symbols are paired for the longest Huffman code, the
1020
   * symbols are removed from this length category two at a time.  The prefix
1021
   * for the pair (which is one bit shorter) is allocated to one of the pair;
1022
   * then, skipping the BITS entry for that prefix length, a code word from the
1023
   * next shortest nonzero BITS entry is converted into a prefix for two code
1024
   * words one bit longer.
1025
   */
1026
1027
0
  for (i = MAX_CLEN; i > 16; i--) {
1028
0
    while (bits[i] > 0) {
1029
0
      j = i - 2;                /* find length of new prefix to be used */
1030
0
      while (bits[j] == 0)
1031
0
        j--;
1032
1033
0
      bits[i] -= 2;             /* remove two symbols */
1034
0
      bits[i - 1]++;            /* one goes in this length */
1035
0
      bits[j + 1] += 2;         /* two new symbols in this length */
1036
0
      bits[j]--;                /* symbol of this length is now a prefix */
1037
0
    }
1038
0
  }
1039
1040
  /* Remove the count for the pseudo-symbol 256 from the largest codelength */
1041
0
  while (bits[i] == 0)          /* find largest codelength still in use */
1042
0
    i--;
1043
0
  bits[i]--;
1044
1045
  /* Return final symbol counts (only for lengths 0..16) */
1046
0
  memcpy(htbl->bits, bits, sizeof(htbl->bits));
1047
1048
  /* Return a list of the symbols sorted by code length */
1049
  /* It's not real clear to me why we don't need to consider the codelength
1050
   * changes made above, but Rec. ITU-T T.81 | ISO/IEC 10918-1 seems to think
1051
   * this works.
1052
   */
1053
0
  p = 0;
1054
0
  for (i = 1; i <= MAX_CLEN; i++) {
1055
0
    for (j = 0; j <= 255; j++) {
1056
0
      if (codesize[j] == i) {
1057
0
        htbl->huffval[p] = (UINT8)j;
1058
0
        p++;
1059
0
      }
1060
0
    }
1061
0
  }
1062
1063
  /* Set sent_table FALSE so updated table will be written to JPEG file. */
1064
0
  htbl->sent_table = FALSE;
1065
0
}
1066
1067
1068
/*
1069
 * Finish up a statistics-gathering pass and create the new Huffman tables.
1070
 */
1071
1072
METHODDEF(void)
1073
finish_pass_gather(j_compress_ptr cinfo)
1074
0
{
1075
0
  huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
1076
0
  int ci, dctbl, actbl;
1077
0
  jpeg_component_info *compptr;
1078
0
  JHUFF_TBL **htblptr;
1079
0
  boolean did_dc[NUM_HUFF_TBLS];
1080
0
  boolean did_ac[NUM_HUFF_TBLS];
1081
1082
  /* It's important not to apply jpeg_gen_optimal_table more than once
1083
   * per table, because it clobbers the input frequency counts!
1084
   */
1085
0
  memset(did_dc, 0, sizeof(did_dc));
1086
0
  memset(did_ac, 0, sizeof(did_ac));
1087
1088
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1089
0
    compptr = cinfo->cur_comp_info[ci];
1090
0
    dctbl = compptr->dc_tbl_no;
1091
0
    actbl = compptr->ac_tbl_no;
1092
0
    if (!did_dc[dctbl]) {
1093
0
      htblptr = &cinfo->dc_huff_tbl_ptrs[dctbl];
1094
0
      if (*htblptr == NULL)
1095
0
        *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
1096
0
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
1097
0
      did_dc[dctbl] = TRUE;
1098
0
    }
1099
0
    if (!did_ac[actbl]) {
1100
0
      htblptr = &cinfo->ac_huff_tbl_ptrs[actbl];
1101
0
      if (*htblptr == NULL)
1102
0
        *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
1103
0
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
1104
0
      did_ac[actbl] = TRUE;
1105
0
    }
1106
0
  }
1107
0
}
1108
1109
1110
#endif /* ENTROPY_OPT_SUPPORTED */
1111
1112
1113
/*
1114
 * Module initialization routine for Huffman entropy encoding.
1115
 */
1116
1117
GLOBAL(void)
1118
jinit_huff_encoder(j_compress_ptr cinfo)
1119
0
{
1120
0
  huff_entropy_ptr entropy;
1121
0
  int i;
1122
1123
0
  entropy = (huff_entropy_ptr)
1124
0
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
1125
0
                                sizeof(huff_entropy_encoder));
1126
0
  cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
1127
0
  entropy->pub.start_pass = start_pass_huff;
1128
1129
  /* Mark tables unallocated */
1130
0
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
1131
0
    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
1132
0
#ifdef ENTROPY_OPT_SUPPORTED
1133
0
    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
1134
0
#endif
1135
0
  }
1136
0
}