Coverage Report

Created: 2023-06-07 06:03

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