Coverage Report

Created: 2023-12-08 06:53

/src/freeimage-svn/FreeImage/trunk/Source/LibJPEG/jchuff.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jchuff.c
3
 *
4
 * Copyright (C) 1991-1997, Thomas G. Lane.
5
 * Modified 2006-2019 by Guido Vollbeding.
6
 * This file is part of the Independent JPEG Group's software.
7
 * For conditions of distribution and use, see the accompanying README file.
8
 *
9
 * This file contains Huffman entropy encoding routines.
10
 * Both sequential and progressive modes are supported in this single module.
11
 *
12
 * Much of the complexity here has to do with supporting output suspension.
13
 * If the data destination module demands suspension, we want to be able to
14
 * back up to the start of the current MCU.  To do this, we copy state
15
 * variables into local working storage, and update them back to the
16
 * permanent JPEG objects only upon successful completion of an MCU.
17
 *
18
 * We do not support output suspension for the progressive JPEG mode, since
19
 * the library currently does not allow multiple-scan files to be written
20
 * with output suspension.
21
 */
22
23
#define JPEG_INTERNALS
24
#include "jinclude.h"
25
#include "jpeglib.h"
26
27
28
/* The legal range of a DCT coefficient is
29
 *  -1024 .. +1023  for 8-bit data;
30
 * -16384 .. +16383 for 12-bit data.
31
 * Hence the magnitude should always fit in 10 or 14 bits respectively.
32
 */
33
34
#if BITS_IN_JSAMPLE == 8
35
0
#define MAX_COEF_BITS 10
36
#else
37
#define MAX_COEF_BITS 14
38
#endif
39
40
/* Derived data constructed for each Huffman table */
41
42
typedef struct {
43
  unsigned int ehufco[256]; /* code for each symbol */
44
  char ehufsi[256];   /* length of code for each symbol */
45
  /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
46
} c_derived_tbl;
47
48
49
/* Expanded entropy encoder object for Huffman encoding.
50
 *
51
 * The savable_state subrecord contains fields that change within an MCU,
52
 * but must not be updated permanently until we complete the MCU.
53
 */
54
55
typedef struct {
56
  INT32 put_buffer;   /* current bit-accumulation buffer */
57
  int put_bits;     /* # of bits now in it */
58
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
59
} savable_state;
60
61
/* This macro is to work around compilers with missing or broken
62
 * structure assignment.  You'll need to fix this code if you have
63
 * such a compiler and you change MAX_COMPS_IN_SCAN.
64
 */
65
66
#ifndef NO_STRUCT_ASSIGN
67
0
#define ASSIGN_STATE(dest,src)  ((dest) = (src))
68
#else
69
#if MAX_COMPS_IN_SCAN == 4
70
#define ASSIGN_STATE(dest,src)  \
71
  ((dest).put_buffer = (src).put_buffer, \
72
   (dest).put_bits = (src).put_bits, \
73
   (dest).last_dc_val[0] = (src).last_dc_val[0], \
74
   (dest).last_dc_val[1] = (src).last_dc_val[1], \
75
   (dest).last_dc_val[2] = (src).last_dc_val[2], \
76
   (dest).last_dc_val[3] = (src).last_dc_val[3])
77
#endif
78
#endif
79
80
81
typedef struct {
82
  struct jpeg_entropy_encoder pub; /* public fields */
83
84
  savable_state saved;    /* Bit buffer & DC state at start of MCU */
85
86
  /* These fields are NOT loaded into local working state. */
87
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
88
  int next_restart_num;   /* next restart number to write (0-7) */
89
90
  /* Pointers to derived tables (these workspaces have image lifespan) */
91
  c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
92
  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
93
94
  /* Statistics tables for optimization */
95
  long * dc_count_ptrs[NUM_HUFF_TBLS];
96
  long * ac_count_ptrs[NUM_HUFF_TBLS];
97
98
  /* Following fields used only in progressive mode */
99
100
  /* Mode flag: TRUE for optimization, FALSE for actual data output */
101
  boolean gather_statistics;
102
103
  /* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
104
   */
105
  JOCTET * next_output_byte;  /* => next byte to write in buffer */
106
  size_t free_in_buffer;  /* # of byte spaces remaining in buffer */
107
  j_compress_ptr cinfo;   /* link to cinfo (needed for dump_buffer) */
108
109
  /* Coding status for AC components */
110
  int ac_tbl_no;    /* the table number of the single component */
111
  unsigned int EOBRUN;    /* run length of EOBs */
112
  unsigned int BE;    /* # of buffered correction bits before MCU */
113
  char * bit_buffer;    /* buffer for correction bits (1 per char) */
114
  /* packing correction bits tightly would save some space but cost time... */
115
} huff_entropy_encoder;
116
117
typedef huff_entropy_encoder * huff_entropy_ptr;
118
119
/* Working state while writing an MCU (sequential mode).
120
 * This struct contains all the fields that are needed by subroutines.
121
 */
122
123
typedef struct {
124
  JOCTET * next_output_byte;  /* => next byte to write in buffer */
125
  size_t free_in_buffer;  /* # of byte spaces remaining in buffer */
126
  savable_state cur;    /* Current bit buffer & DC state */
127
  j_compress_ptr cinfo;   /* dump_buffer needs access to this */
128
} working_state;
129
130
/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
131
 * buffer can hold.  Larger sizes may slightly improve compression, but
132
 * 1000 is already well into the realm of overkill.
133
 * The minimum safe size is 64 bits.
134
 */
135
136
0
#define MAX_CORR_BITS  1000  /* Max # of correction bits I can buffer */
137
138
/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
139
 * We assume that int right shift is unsigned if INT32 right shift is,
140
 * which should be safe.
141
 */
142
143
#ifdef RIGHT_SHIFT_IS_UNSIGNED
144
#define ISHIFT_TEMPS  int ishift_temp;
145
#define IRIGHT_SHIFT(x,shft)  \
146
  ((ishift_temp = (x)) < 0 ? \
147
   (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
148
   (ishift_temp >> (shft)))
149
#else
150
#define ISHIFT_TEMPS
151
0
#define IRIGHT_SHIFT(x,shft)  ((x) >> (shft))
152
#endif
153
154
155
/*
156
 * Compute the derived values for a Huffman table.
157
 * This routine also performs some validation checks on the table.
158
 */
159
160
LOCAL(void)
161
jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
162
       c_derived_tbl ** pdtbl)
163
0
{
164
0
  JHUFF_TBL *htbl;
165
0
  c_derived_tbl *dtbl;
166
0
  int p, i, l, lastp, si, maxsymbol;
167
0
  char huffsize[257];
168
0
  unsigned int huffcode[257];
169
0
  unsigned int code;
170
171
  /* Note that huffsize[] and huffcode[] are filled in code-length order,
172
   * paralleling the order of the symbols themselves in htbl->huffval[].
173
   */
174
175
  /* Find the input Huffman table */
176
0
  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
177
0
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
178
0
  htbl =
179
0
    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
180
0
  if (htbl == NULL)
181
0
    htbl = jpeg_std_huff_table((j_common_ptr) cinfo, isDC, tblno);
182
183
  /* Allocate a workspace if we haven't already done so. */
184
0
  if (*pdtbl == NULL)
185
0
    *pdtbl = (c_derived_tbl *) (*cinfo->mem->alloc_small)
186
0
      ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(c_derived_tbl));
187
0
  dtbl = *pdtbl;
188
  
189
  /* Figure C.1: make table of Huffman code length for each symbol */
190
191
0
  p = 0;
192
0
  for (l = 1; l <= 16; l++) {
193
0
    i = (int) htbl->bits[l];
194
0
    if (i < 0 || p + i > 256) /* protect against table overrun */
195
0
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
196
0
    while (i--)
197
0
      huffsize[p++] = (char) l;
198
0
  }
199
0
  huffsize[p] = 0;
200
0
  lastp = p;
201
  
202
  /* Figure C.2: generate the codes themselves */
203
  /* We also validate that the counts represent a legal Huffman code tree. */
204
205
0
  code = 0;
206
0
  si = huffsize[0];
207
0
  p = 0;
208
0
  while (huffsize[p]) {
209
0
    while (((int) huffsize[p]) == si) {
210
0
      huffcode[p++] = code;
211
0
      code++;
212
0
    }
213
    /* code is now 1 more than the last code used for codelength si; but
214
     * it must still fit in si bits, since no code is allowed to be all ones.
215
     */
216
0
    if (((INT32) code) >= (((INT32) 1) << si))
217
0
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
218
0
    code <<= 1;
219
0
    si++;
220
0
  }
221
  
222
  /* Figure C.3: generate encoding tables */
223
  /* These are code and size indexed by symbol value */
224
225
  /* Set all codeless symbols to have code length 0;
226
   * this lets us detect duplicate VAL entries here, and later
227
   * allows emit_bits to detect any attempt to emit such symbols.
228
   */
229
0
  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
230
231
  /* This is also a convenient place to check for out-of-range
232
   * and duplicated VAL entries.  We allow 0..255 for AC symbols
233
   * but only 0..15 for DC.  (We could constrain them further
234
   * based on data depth and mode, but this seems enough.)
235
   */
236
0
  maxsymbol = isDC ? 15 : 255;
237
238
0
  for (p = 0; p < lastp; p++) {
239
0
    i = htbl->huffval[p];
240
0
    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
241
0
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
242
0
    dtbl->ehufco[i] = huffcode[p];
243
0
    dtbl->ehufsi[i] = huffsize[p];
244
0
  }
245
0
}
246
247
248
/* Outputting bytes to the file.
249
 * NB: these must be called only when actually outputting,
250
 * that is, entropy->gather_statistics == FALSE.
251
 */
252
253
/* Emit a byte, taking 'action' if must suspend. */
254
#define emit_byte_s(state,val,action)  \
255
0
  { *(state)->next_output_byte++ = (JOCTET) (val);  \
256
0
    if (--(state)->free_in_buffer == 0)  \
257
0
      if (! dump_buffer_s(state))  \
258
0
        { action; } }
259
260
/* Emit a byte */
261
#define emit_byte_e(entropy,val)  \
262
0
  { *(entropy)->next_output_byte++ = (JOCTET) (val);  \
263
0
    if (--(entropy)->free_in_buffer == 0)  \
264
0
      dump_buffer_e(entropy); }
265
266
267
LOCAL(boolean)
268
dump_buffer_s (working_state * state)
269
/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
270
0
{
271
0
  struct jpeg_destination_mgr * dest = state->cinfo->dest;
272
273
0
  if (! (*dest->empty_output_buffer) (state->cinfo))
274
0
    return FALSE;
275
  /* After a successful buffer dump, must reset buffer pointers */
276
0
  state->next_output_byte = dest->next_output_byte;
277
0
  state->free_in_buffer = dest->free_in_buffer;
278
0
  return TRUE;
279
0
}
280
281
282
LOCAL(void)
283
dump_buffer_e (huff_entropy_ptr entropy)
284
/* Empty the output buffer; we do not support suspension in this case. */
285
0
{
286
0
  struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
287
288
0
  if (! (*dest->empty_output_buffer) (entropy->cinfo))
289
0
    ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
290
  /* After a successful buffer dump, must reset buffer pointers */
291
0
  entropy->next_output_byte = dest->next_output_byte;
292
0
  entropy->free_in_buffer = dest->free_in_buffer;
293
0
}
294
295
296
/* Outputting bits to the file */
297
298
/* Only the right 24 bits of put_buffer are used; the valid bits are
299
 * left-justified in this part.  At most 16 bits can be passed to emit_bits
300
 * in one call, and we never retain more than 7 bits in put_buffer
301
 * between calls, so 24 bits are sufficient.
302
 */
303
304
INLINE
305
LOCAL(boolean)
306
emit_bits_s (working_state * state, unsigned int code, int size)
307
/* Emit some bits; return TRUE if successful, FALSE if must suspend */
308
0
{
309
  /* This routine is heavily used, so it's worth coding tightly. */
310
0
  register INT32 put_buffer;
311
0
  register int put_bits;
312
313
  /* if size is 0, caller used an invalid Huffman table entry */
314
0
  if (size == 0)
315
0
    ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
316
317
  /* mask off any extra bits in code */
318
0
  put_buffer = ((INT32) code) & ((((INT32) 1) << size) - 1);
319
320
  /* new number of bits in buffer */
321
0
  put_bits = size + state->cur.put_bits;
322
323
0
  put_buffer <<= 24 - put_bits; /* align incoming bits */
324
325
  /* and merge with old buffer contents */
326
0
  put_buffer |= state->cur.put_buffer;
327
328
0
  while (put_bits >= 8) {
329
0
    int c = (int) ((put_buffer >> 16) & 0xFF);
330
331
0
    emit_byte_s(state, c, return FALSE);
332
0
    if (c == 0xFF) {   /* need to stuff a zero byte? */
333
0
      emit_byte_s(state, 0, return FALSE);
334
0
    }
335
0
    put_buffer <<= 8;
336
0
    put_bits -= 8;
337
0
  }
338
339
0
  state->cur.put_buffer = put_buffer; /* update state variables */
340
0
  state->cur.put_bits = put_bits;
341
342
0
  return TRUE;
343
0
}
344
345
346
INLINE
347
LOCAL(void)
348
emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size)
349
/* Emit some bits, unless we are in gather mode */
350
0
{
351
  /* This routine is heavily used, so it's worth coding tightly. */
352
0
  register INT32 put_buffer;
353
0
  register int put_bits;
354
355
  /* if size is 0, caller used an invalid Huffman table entry */
356
0
  if (size == 0)
357
0
    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
358
359
0
  if (entropy->gather_statistics)
360
0
    return;     /* do nothing if we're only getting stats */
361
362
  /* mask off any extra bits in code */
363
0
  put_buffer = ((INT32) code) & ((((INT32) 1) << size) - 1);
364
365
  /* new number of bits in buffer */
366
0
  put_bits = size + entropy->saved.put_bits;
367
368
0
  put_buffer <<= 24 - put_bits; /* align incoming bits */
369
370
  /* and merge with old buffer contents */
371
0
  put_buffer |= entropy->saved.put_buffer;
372
373
0
  while (put_bits >= 8) {
374
0
    int c = (int) ((put_buffer >> 16) & 0xFF);
375
376
0
    emit_byte_e(entropy, c);
377
0
    if (c == 0xFF) {   /* need to stuff a zero byte? */
378
0
      emit_byte_e(entropy, 0);
379
0
    }
380
0
    put_buffer <<= 8;
381
0
    put_bits -= 8;
382
0
  }
383
384
0
  entropy->saved.put_buffer = put_buffer; /* update variables */
385
0
  entropy->saved.put_bits = put_bits;
386
0
}
387
388
389
LOCAL(boolean)
390
flush_bits_s (working_state * state)
391
0
{
392
0
  if (! emit_bits_s(state, 0x7F, 7)) /* fill any partial byte with ones */
393
0
    return FALSE;
394
0
  state->cur.put_buffer = 0;       /* and reset bit-buffer to empty */
395
0
  state->cur.put_bits = 0;
396
0
  return TRUE;
397
0
}
398
399
400
LOCAL(void)
401
flush_bits_e (huff_entropy_ptr entropy)
402
0
{
403
0
  emit_bits_e(entropy, 0x7F, 7); /* fill any partial byte with ones */
404
0
  entropy->saved.put_buffer = 0; /* and reset bit-buffer to empty */
405
0
  entropy->saved.put_bits = 0;
406
0
}
407
408
409
/*
410
 * Emit (or just count) a Huffman symbol.
411
 */
412
413
INLINE
414
LOCAL(void)
415
emit_dc_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
416
0
{
417
0
  if (entropy->gather_statistics)
418
0
    entropy->dc_count_ptrs[tbl_no][symbol]++;
419
0
  else {
420
0
    c_derived_tbl * tbl = entropy->dc_derived_tbls[tbl_no];
421
0
    emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
422
0
  }
423
0
}
424
425
426
INLINE
427
LOCAL(void)
428
emit_ac_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
429
0
{
430
0
  if (entropy->gather_statistics)
431
0
    entropy->ac_count_ptrs[tbl_no][symbol]++;
432
0
  else {
433
0
    c_derived_tbl * tbl = entropy->ac_derived_tbls[tbl_no];
434
0
    emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
435
0
  }
436
0
}
437
438
439
/*
440
 * Emit bits from a correction bit buffer.
441
 */
442
443
LOCAL(void)
444
emit_buffered_bits (huff_entropy_ptr entropy, char * bufstart,
445
        unsigned int nbits)
446
0
{
447
0
  if (entropy->gather_statistics)
448
0
    return;     /* no real work */
449
450
0
  while (nbits > 0) {
451
0
    emit_bits_e(entropy, (unsigned int) (*bufstart), 1);
452
0
    bufstart++;
453
0
    nbits--;
454
0
  }
455
0
}
456
457
458
/*
459
 * Emit any pending EOBRUN symbol.
460
 */
461
462
LOCAL(void)
463
emit_eobrun (huff_entropy_ptr entropy)
464
0
{
465
0
  register int temp, nbits;
466
467
0
  if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
468
0
    temp = entropy->EOBRUN;
469
0
    nbits = 0;
470
0
    while ((temp >>= 1))
471
0
      nbits++;
472
    /* safety check: shouldn't happen given limited correction-bit buffer */
473
0
    if (nbits > 14)
474
0
      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
475
476
0
    emit_ac_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
477
0
    if (nbits)
478
0
      emit_bits_e(entropy, entropy->EOBRUN, nbits);
479
480
0
    entropy->EOBRUN = 0;
481
482
    /* Emit any buffered correction bits */
483
0
    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
484
0
    entropy->BE = 0;
485
0
  }
486
0
}
487
488
489
/*
490
 * Emit a restart marker & resynchronize predictions.
491
 */
492
493
LOCAL(boolean)
494
emit_restart_s (working_state * state, int restart_num)
495
0
{
496
0
  int ci;
497
498
0
  if (! flush_bits_s(state))
499
0
    return FALSE;
500
501
0
  emit_byte_s(state, 0xFF, return FALSE);
502
0
  emit_byte_s(state, JPEG_RST0 + restart_num, return FALSE);
503
504
  /* Re-initialize DC predictions to 0 */
505
0
  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
506
0
    state->cur.last_dc_val[ci] = 0;
507
508
  /* The restart counter is not updated until we successfully write the MCU. */
509
510
0
  return TRUE;
511
0
}
512
513
514
LOCAL(void)
515
emit_restart_e (huff_entropy_ptr entropy, int restart_num)
516
0
{
517
0
  int ci;
518
519
0
  emit_eobrun(entropy);
520
521
0
  if (! entropy->gather_statistics) {
522
0
    flush_bits_e(entropy);
523
0
    emit_byte_e(entropy, 0xFF);
524
0
    emit_byte_e(entropy, JPEG_RST0 + restart_num);
525
0
  }
526
527
0
  if (entropy->cinfo->Ss == 0) {
528
    /* Re-initialize DC predictions to 0 */
529
0
    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
530
0
      entropy->saved.last_dc_val[ci] = 0;
531
0
  } else {
532
    /* Re-initialize all AC-related fields to 0 */
533
0
    entropy->EOBRUN = 0;
534
0
    entropy->BE = 0;
535
0
  }
536
0
}
537
538
539
/*
540
 * MCU encoding for DC initial scan (either spectral selection,
541
 * or first pass of successive approximation).
542
 */
543
544
METHODDEF(boolean)
545
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
546
0
{
547
0
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
548
0
  register int temp, temp2;
549
0
  register int nbits;
550
0
  int blkn, ci, tbl;
551
0
  ISHIFT_TEMPS
552
553
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
554
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
555
556
  /* Emit restart marker if needed */
557
0
  if (cinfo->restart_interval)
558
0
    if (entropy->restarts_to_go == 0)
559
0
      emit_restart_e(entropy, entropy->next_restart_num);
560
561
  /* Encode the MCU data blocks */
562
0
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
563
0
    ci = cinfo->MCU_membership[blkn];
564
0
    tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
565
566
    /* Compute the DC value after the required point transform by Al.
567
     * This is simply an arithmetic right shift.
568
     */
569
0
    temp = IRIGHT_SHIFT((int) (MCU_data[blkn][0][0]), cinfo->Al);
570
571
    /* DC differences are figured on the point-transformed values. */
572
0
    temp2 = temp - entropy->saved.last_dc_val[ci];
573
0
    entropy->saved.last_dc_val[ci] = temp;
574
575
    /* Encode the DC coefficient difference per section G.1.2.1 */
576
0
    temp = temp2;
577
0
    if (temp < 0) {
578
0
      temp = -temp;   /* temp is abs value of input */
579
      /* For a negative input, want temp2 = bitwise complement of abs(input) */
580
      /* This code assumes we are on a two's complement machine */
581
0
      temp2--;
582
0
    }
583
584
    /* Find the number of bits needed for the magnitude of the coefficient */
585
0
    nbits = 0;
586
0
    while (temp) {
587
0
      nbits++;
588
0
      temp >>= 1;
589
0
    }
590
    /* Check for out-of-range coefficient values.
591
     * Since we're encoding a difference, the range limit is twice as much.
592
     */
593
0
    if (nbits > MAX_COEF_BITS+1)
594
0
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
595
596
    /* Count/emit the Huffman-coded symbol for the number of bits */
597
0
    emit_dc_symbol(entropy, tbl, nbits);
598
599
    /* Emit that number of bits of the value, if positive, */
600
    /* or the complement of its magnitude, if negative. */
601
0
    if (nbits)     /* emit_bits rejects calls with size 0 */
602
0
      emit_bits_e(entropy, (unsigned int) temp2, nbits);
603
0
  }
604
605
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
606
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
607
608
  /* Update restart-interval state too */
609
0
  if (cinfo->restart_interval) {
610
0
    if (entropy->restarts_to_go == 0) {
611
0
      entropy->restarts_to_go = cinfo->restart_interval;
612
0
      entropy->next_restart_num++;
613
0
      entropy->next_restart_num &= 7;
614
0
    }
615
0
    entropy->restarts_to_go--;
616
0
  }
617
618
0
  return TRUE;
619
0
}
620
621
622
/*
623
 * MCU encoding for AC initial scan (either spectral selection,
624
 * or first pass of successive approximation).
625
 */
626
627
METHODDEF(boolean)
628
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
629
0
{
630
0
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
631
0
  const int * natural_order;
632
0
  JBLOCKROW block;
633
0
  register int temp, temp2;
634
0
  register int nbits;
635
0
  register int r, k;
636
0
  int Se, Al;
637
638
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
639
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
640
641
  /* Emit restart marker if needed */
642
0
  if (cinfo->restart_interval)
643
0
    if (entropy->restarts_to_go == 0)
644
0
      emit_restart_e(entropy, entropy->next_restart_num);
645
646
0
  Se = cinfo->Se;
647
0
  Al = cinfo->Al;
648
0
  natural_order = cinfo->natural_order;
649
650
  /* Encode the MCU data block */
651
0
  block = MCU_data[0];
652
653
  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
654
  
655
0
  r = 0;      /* r = run length of zeros */
656
   
657
0
  for (k = cinfo->Ss; k <= Se; k++) {
658
0
    if ((temp = (*block)[natural_order[k]]) == 0) {
659
0
      r++;
660
0
      continue;
661
0
    }
662
    /* We must apply the point transform by Al.  For AC coefficients this
663
     * is an integer division with rounding towards 0.  To do this portably
664
     * in C, we shift after obtaining the absolute value; so the code is
665
     * interwoven with finding the abs value (temp) and output bits (temp2).
666
     */
667
0
    if (temp < 0) {
668
0
      temp = -temp;   /* temp is abs value of input */
669
0
      temp >>= Al;    /* apply the point transform */
670
      /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
671
0
      temp2 = ~temp;
672
0
    } else {
673
0
      temp >>= Al;    /* apply the point transform */
674
0
      temp2 = temp;
675
0
    }
676
    /* Watch out for case that nonzero coef is zero after point transform */
677
0
    if (temp == 0) {
678
0
      r++;
679
0
      continue;
680
0
    }
681
682
    /* Emit any pending EOBRUN */
683
0
    if (entropy->EOBRUN > 0)
684
0
      emit_eobrun(entropy);
685
    /* if run length > 15, must emit special run-length-16 codes (0xF0) */
686
0
    while (r > 15) {
687
0
      emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
688
0
      r -= 16;
689
0
    }
690
691
    /* Find the number of bits needed for the magnitude of the coefficient */
692
0
    nbits = 1;      /* there must be at least one 1 bit */
693
0
    while ((temp >>= 1))
694
0
      nbits++;
695
    /* Check for out-of-range coefficient values */
696
0
    if (nbits > MAX_COEF_BITS)
697
0
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
698
699
    /* Count/emit Huffman symbol for run length / number of bits */
700
0
    emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
701
702
    /* Emit that number of bits of the value, if positive, */
703
    /* or the complement of its magnitude, if negative. */
704
0
    emit_bits_e(entropy, (unsigned int) temp2, nbits);
705
706
0
    r = 0;      /* reset zero run length */
707
0
  }
708
709
0
  if (r > 0) {     /* If there are trailing zeroes, */
710
0
    entropy->EOBRUN++;    /* count an EOB */
711
0
    if (entropy->EOBRUN == 0x7FFF)
712
0
      emit_eobrun(entropy); /* force it out to avoid overflow */
713
0
  }
714
715
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
716
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
717
718
  /* Update restart-interval state too */
719
0
  if (cinfo->restart_interval) {
720
0
    if (entropy->restarts_to_go == 0) {
721
0
      entropy->restarts_to_go = cinfo->restart_interval;
722
0
      entropy->next_restart_num++;
723
0
      entropy->next_restart_num &= 7;
724
0
    }
725
0
    entropy->restarts_to_go--;
726
0
  }
727
728
0
  return TRUE;
729
0
}
730
731
732
/*
733
 * MCU encoding for DC successive approximation refinement scan.
734
 * Note: we assume such scans can be multi-component,
735
 * although the spec is not very clear on the point.
736
 */
737
738
METHODDEF(boolean)
739
encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
740
0
{
741
0
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
742
0
  int Al, blkn;
743
744
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
745
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
746
747
  /* Emit restart marker if needed */
748
0
  if (cinfo->restart_interval)
749
0
    if (entropy->restarts_to_go == 0)
750
0
      emit_restart_e(entropy, entropy->next_restart_num);
751
752
0
  Al = cinfo->Al;
753
754
  /* Encode the MCU data blocks */
755
0
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
756
    /* We simply emit the Al'th bit of the DC coefficient value. */
757
0
    emit_bits_e(entropy, (unsigned int) (MCU_data[blkn][0][0] >> Al), 1);
758
0
  }
759
760
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
761
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
762
763
  /* Update restart-interval state too */
764
0
  if (cinfo->restart_interval) {
765
0
    if (entropy->restarts_to_go == 0) {
766
0
      entropy->restarts_to_go = cinfo->restart_interval;
767
0
      entropy->next_restart_num++;
768
0
      entropy->next_restart_num &= 7;
769
0
    }
770
0
    entropy->restarts_to_go--;
771
0
  }
772
773
0
  return TRUE;
774
0
}
775
776
777
/*
778
 * MCU encoding for AC successive approximation refinement scan.
779
 */
780
781
METHODDEF(boolean)
782
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
783
0
{
784
0
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
785
0
  const int * natural_order;
786
0
  JBLOCKROW block;
787
0
  register int temp;
788
0
  register int r, k;
789
0
  int Se, Al;
790
0
  int EOB;
791
0
  char *BR_buffer;
792
0
  unsigned int BR;
793
0
  int absvalues[DCTSIZE2];
794
795
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
796
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
797
798
  /* Emit restart marker if needed */
799
0
  if (cinfo->restart_interval)
800
0
    if (entropy->restarts_to_go == 0)
801
0
      emit_restart_e(entropy, entropy->next_restart_num);
802
803
0
  Se = cinfo->Se;
804
0
  Al = cinfo->Al;
805
0
  natural_order = cinfo->natural_order;
806
807
  /* Encode the MCU data block */
808
0
  block = MCU_data[0];
809
810
  /* It is convenient to make a pre-pass to determine the transformed
811
   * coefficients' absolute values and the EOB position.
812
   */
813
0
  EOB = 0;
814
0
  for (k = cinfo->Ss; k <= Se; k++) {
815
0
    temp = (*block)[natural_order[k]];
816
    /* We must apply the point transform by Al.  For AC coefficients this
817
     * is an integer division with rounding towards 0.  To do this portably
818
     * in C, we shift after obtaining the absolute value.
819
     */
820
0
    if (temp < 0)
821
0
      temp = -temp;   /* temp is abs value of input */
822
0
    temp >>= Al;    /* apply the point transform */
823
0
    absvalues[k] = temp;  /* save abs value for main pass */
824
0
    if (temp == 1)
825
0
      EOB = k;     /* EOB = index of last newly-nonzero coef */
826
0
  }
827
828
  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
829
  
830
0
  r = 0;      /* r = run length of zeros */
831
0
  BR = 0;     /* BR = count of buffered bits added now */
832
0
  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
833
834
0
  for (k = cinfo->Ss; k <= Se; k++) {
835
0
    if ((temp = absvalues[k]) == 0) {
836
0
      r++;
837
0
      continue;
838
0
    }
839
840
    /* Emit any required ZRLs, but not if they can be folded into EOB */
841
0
    while (r > 15 && k <= EOB) {
842
      /* emit any pending EOBRUN and the BE correction bits */
843
0
      emit_eobrun(entropy);
844
      /* Emit ZRL */
845
0
      emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
846
0
      r -= 16;
847
      /* Emit buffered correction bits that must be associated with ZRL */
848
0
      emit_buffered_bits(entropy, BR_buffer, BR);
849
0
      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
850
0
      BR = 0;
851
0
    }
852
853
    /* If the coef was previously nonzero, it only needs a correction bit.
854
     * NOTE: a straight translation of the spec's figure G.7 would suggest
855
     * that we also need to test r > 15.  But if r > 15, we can only get here
856
     * if k > EOB, which implies that this coefficient is not 1.
857
     */
858
0
    if (temp > 1) {
859
      /* The correction bit is the next bit of the absolute value. */
860
0
      BR_buffer[BR++] = (char) (temp & 1);
861
0
      continue;
862
0
    }
863
864
    /* Emit any pending EOBRUN and the BE correction bits */
865
0
    emit_eobrun(entropy);
866
867
    /* Count/emit Huffman symbol for run length / number of bits */
868
0
    emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
869
870
    /* Emit output bit for newly-nonzero coef */
871
0
    temp = ((*block)[natural_order[k]] < 0) ? 0 : 1;
872
0
    emit_bits_e(entropy, (unsigned int) temp, 1);
873
874
    /* Emit buffered correction bits that must be associated with this code */
875
0
    emit_buffered_bits(entropy, BR_buffer, BR);
876
0
    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
877
0
    BR = 0;
878
0
    r = 0;      /* reset zero run length */
879
0
  }
880
881
0
  if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
882
0
    entropy->EOBRUN++;    /* count an EOB */
883
0
    entropy->BE += BR;    /* concat my correction bits to older ones */
884
    /* We force out the EOB if we risk either:
885
     * 1. overflow of the EOB counter;
886
     * 2. overflow of the correction bit buffer during the next MCU.
887
     */
888
0
    if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
889
0
      emit_eobrun(entropy);
890
0
  }
891
892
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
893
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
894
895
  /* Update restart-interval state too */
896
0
  if (cinfo->restart_interval) {
897
0
    if (entropy->restarts_to_go == 0) {
898
0
      entropy->restarts_to_go = cinfo->restart_interval;
899
0
      entropy->next_restart_num++;
900
0
      entropy->next_restart_num &= 7;
901
0
    }
902
0
    entropy->restarts_to_go--;
903
0
  }
904
905
0
  return TRUE;
906
0
}
907
908
909
/* Encode a single block's worth of coefficients */
910
911
LOCAL(boolean)
912
encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
913
      c_derived_tbl *dctbl, c_derived_tbl *actbl)
914
0
{
915
0
  register int temp, temp2;
916
0
  register int nbits;
917
0
  register int r, k;
918
0
  int Se = state->cinfo->lim_Se;
919
0
  const int * natural_order = state->cinfo->natural_order;
920
921
  /* Encode the DC coefficient difference per section F.1.2.1 */
922
923
0
  temp = temp2 = block[0] - last_dc_val;
924
925
0
  if (temp < 0) {
926
0
    temp = -temp;   /* temp is abs value of input */
927
    /* For a negative input, want temp2 = bitwise complement of abs(input) */
928
    /* This code assumes we are on a two's complement machine */
929
0
    temp2--;
930
0
  }
931
932
  /* Find the number of bits needed for the magnitude of the coefficient */
933
0
  nbits = 0;
934
0
  while (temp) {
935
0
    nbits++;
936
0
    temp >>= 1;
937
0
  }
938
  /* Check for out-of-range coefficient values.
939
   * Since we're encoding a difference, the range limit is twice as much.
940
   */
941
0
  if (nbits > MAX_COEF_BITS+1)
942
0
    ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
943
944
  /* Emit the Huffman-coded symbol for the number of bits */
945
0
  if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
946
0
    return FALSE;
947
948
  /* Emit that number of bits of the value, if positive, */
949
  /* or the complement of its magnitude, if negative. */
950
0
  if (nbits)     /* emit_bits rejects calls with size 0 */
951
0
    if (! emit_bits_s(state, (unsigned int) temp2, nbits))
952
0
      return FALSE;
953
954
  /* Encode the AC coefficients per section F.1.2.2 */
955
956
0
  r = 0;      /* r = run length of zeros */
957
958
0
  for (k = 1; k <= Se; k++) {
959
0
    if ((temp2 = block[natural_order[k]]) == 0) {
960
0
      r++;
961
0
    } else {
962
      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
963
0
      while (r > 15) {
964
0
  if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
965
0
    return FALSE;
966
0
  r -= 16;
967
0
      }
968
969
0
      temp = temp2;
970
0
      if (temp < 0) {
971
0
  temp = -temp;   /* temp is abs value of input */
972
  /* This code assumes we are on a two's complement machine */
973
0
  temp2--;
974
0
      }
975
976
      /* Find the number of bits needed for the magnitude of the coefficient */
977
0
      nbits = 1;    /* there must be at least one 1 bit */
978
0
      while ((temp >>= 1))
979
0
  nbits++;
980
      /* Check for out-of-range coefficient values */
981
0
      if (nbits > MAX_COEF_BITS)
982
0
  ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
983
984
      /* Emit Huffman symbol for run length / number of bits */
985
0
      temp = (r << 4) + nbits;
986
0
      if (! emit_bits_s(state, actbl->ehufco[temp], actbl->ehufsi[temp]))
987
0
  return FALSE;
988
989
      /* Emit that number of bits of the value, if positive, */
990
      /* or the complement of its magnitude, if negative. */
991
0
      if (! emit_bits_s(state, (unsigned int) temp2, nbits))
992
0
  return FALSE;
993
994
0
      r = 0;
995
0
    }
996
0
  }
997
998
  /* If the last coef(s) were zero, emit an end-of-block code */
999
0
  if (r > 0)
1000
0
    if (! emit_bits_s(state, actbl->ehufco[0], actbl->ehufsi[0]))
1001
0
      return FALSE;
1002
1003
0
  return TRUE;
1004
0
}
1005
1006
1007
/*
1008
 * Encode and output one MCU's worth of Huffman-compressed coefficients.
1009
 */
1010
1011
METHODDEF(boolean)
1012
encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1013
0
{
1014
0
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1015
0
  working_state state;
1016
0
  int blkn, ci;
1017
0
  jpeg_component_info * compptr;
1018
1019
  /* Load up working state */
1020
0
  state.next_output_byte = cinfo->dest->next_output_byte;
1021
0
  state.free_in_buffer = cinfo->dest->free_in_buffer;
1022
0
  ASSIGN_STATE(state.cur, entropy->saved);
1023
0
  state.cinfo = cinfo;
1024
1025
  /* Emit restart marker if needed */
1026
0
  if (cinfo->restart_interval) {
1027
0
    if (entropy->restarts_to_go == 0)
1028
0
      if (! emit_restart_s(&state, entropy->next_restart_num))
1029
0
  return FALSE;
1030
0
  }
1031
1032
  /* Encode the MCU data blocks */
1033
0
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
1034
0
    ci = cinfo->MCU_membership[blkn];
1035
0
    compptr = cinfo->cur_comp_info[ci];
1036
0
    if (! encode_one_block(&state,
1037
0
         MCU_data[blkn][0], state.cur.last_dc_val[ci],
1038
0
         entropy->dc_derived_tbls[compptr->dc_tbl_no],
1039
0
         entropy->ac_derived_tbls[compptr->ac_tbl_no]))
1040
0
      return FALSE;
1041
    /* Update last_dc_val */
1042
0
    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
1043
0
  }
1044
1045
  /* Completed MCU, so update state */
1046
0
  cinfo->dest->next_output_byte = state.next_output_byte;
1047
0
  cinfo->dest->free_in_buffer = state.free_in_buffer;
1048
0
  ASSIGN_STATE(entropy->saved, state.cur);
1049
1050
  /* Update restart-interval state too */
1051
0
  if (cinfo->restart_interval) {
1052
0
    if (entropy->restarts_to_go == 0) {
1053
0
      entropy->restarts_to_go = cinfo->restart_interval;
1054
0
      entropy->next_restart_num++;
1055
0
      entropy->next_restart_num &= 7;
1056
0
    }
1057
0
    entropy->restarts_to_go--;
1058
0
  }
1059
1060
0
  return TRUE;
1061
0
}
1062
1063
1064
/*
1065
 * Finish up at the end of a Huffman-compressed scan.
1066
 */
1067
1068
METHODDEF(void)
1069
finish_pass_huff (j_compress_ptr cinfo)
1070
0
{
1071
0
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1072
0
  working_state state;
1073
1074
0
  if (cinfo->progressive_mode) {
1075
0
    entropy->next_output_byte = cinfo->dest->next_output_byte;
1076
0
    entropy->free_in_buffer = cinfo->dest->free_in_buffer;
1077
1078
    /* Flush out any buffered data */
1079
0
    emit_eobrun(entropy);
1080
0
    flush_bits_e(entropy);
1081
1082
0
    cinfo->dest->next_output_byte = entropy->next_output_byte;
1083
0
    cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1084
0
  } else {
1085
    /* Load up working state ... flush_bits needs it */
1086
0
    state.next_output_byte = cinfo->dest->next_output_byte;
1087
0
    state.free_in_buffer = cinfo->dest->free_in_buffer;
1088
0
    ASSIGN_STATE(state.cur, entropy->saved);
1089
0
    state.cinfo = cinfo;
1090
1091
    /* Flush out the last data */
1092
0
    if (! flush_bits_s(&state))
1093
0
      ERREXIT(cinfo, JERR_CANT_SUSPEND);
1094
1095
    /* Update state */
1096
0
    cinfo->dest->next_output_byte = state.next_output_byte;
1097
0
    cinfo->dest->free_in_buffer = state.free_in_buffer;
1098
0
    ASSIGN_STATE(entropy->saved, state.cur);
1099
0
  }
1100
0
}
1101
1102
1103
/*
1104
 * Huffman coding optimization.
1105
 *
1106
 * We first scan the supplied data and count the number of uses of each symbol
1107
 * that is to be Huffman-coded. (This process MUST agree with the code above.)
1108
 * Then we build a Huffman coding tree for the observed counts.
1109
 * Symbols which are not needed at all for the particular image are not
1110
 * assigned any code, which saves space in the DHT marker as well as in
1111
 * the compressed data.
1112
 */
1113
1114
1115
/* Process a single block's worth of coefficients */
1116
1117
LOCAL(void)
1118
htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
1119
     long dc_counts[], long ac_counts[])
1120
0
{
1121
0
  register int temp;
1122
0
  register int nbits;
1123
0
  register int r, k;
1124
0
  int Se = cinfo->lim_Se;
1125
0
  const int * natural_order = cinfo->natural_order;
1126
1127
  /* Encode the DC coefficient difference per section F.1.2.1 */
1128
1129
0
  temp = block[0] - last_dc_val;
1130
0
  if (temp < 0)
1131
0
    temp = -temp;
1132
1133
  /* Find the number of bits needed for the magnitude of the coefficient */
1134
0
  nbits = 0;
1135
0
  while (temp) {
1136
0
    nbits++;
1137
0
    temp >>= 1;
1138
0
  }
1139
  /* Check for out-of-range coefficient values.
1140
   * Since we're encoding a difference, the range limit is twice as much.
1141
   */
1142
0
  if (nbits > MAX_COEF_BITS+1)
1143
0
    ERREXIT(cinfo, JERR_BAD_DCT_COEF);
1144
1145
  /* Count the Huffman symbol for the number of bits */
1146
0
  dc_counts[nbits]++;
1147
1148
  /* Encode the AC coefficients per section F.1.2.2 */
1149
1150
0
  r = 0;      /* r = run length of zeros */
1151
1152
0
  for (k = 1; k <= Se; k++) {
1153
0
    if ((temp = block[natural_order[k]]) == 0) {
1154
0
      r++;
1155
0
    } else {
1156
      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
1157
0
      while (r > 15) {
1158
0
  ac_counts[0xF0]++;
1159
0
  r -= 16;
1160
0
      }
1161
1162
      /* Find the number of bits needed for the magnitude of the coefficient */
1163
0
      if (temp < 0)
1164
0
  temp = -temp;
1165
1166
      /* Find the number of bits needed for the magnitude of the coefficient */
1167
0
      nbits = 1;    /* there must be at least one 1 bit */
1168
0
      while ((temp >>= 1))
1169
0
  nbits++;
1170
      /* Check for out-of-range coefficient values */
1171
0
      if (nbits > MAX_COEF_BITS)
1172
0
  ERREXIT(cinfo, JERR_BAD_DCT_COEF);
1173
1174
      /* Count Huffman symbol for run length / number of bits */
1175
0
      ac_counts[(r << 4) + nbits]++;
1176
1177
0
      r = 0;
1178
0
    }
1179
0
  }
1180
1181
  /* If the last coef(s) were zero, emit an end-of-block code */
1182
0
  if (r > 0)
1183
0
    ac_counts[0]++;
1184
0
}
1185
1186
1187
/*
1188
 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
1189
 * No data is actually output, so no suspension return is possible.
1190
 */
1191
1192
METHODDEF(boolean)
1193
encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1194
0
{
1195
0
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1196
0
  int blkn, ci;
1197
0
  jpeg_component_info * compptr;
1198
1199
  /* Take care of restart intervals if needed */
1200
0
  if (cinfo->restart_interval) {
1201
0
    if (entropy->restarts_to_go == 0) {
1202
      /* Re-initialize DC predictions to 0 */
1203
0
      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
1204
0
  entropy->saved.last_dc_val[ci] = 0;
1205
      /* Update restart state */
1206
0
      entropy->restarts_to_go = cinfo->restart_interval;
1207
0
    }
1208
0
    entropy->restarts_to_go--;
1209
0
  }
1210
1211
0
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
1212
0
    ci = cinfo->MCU_membership[blkn];
1213
0
    compptr = cinfo->cur_comp_info[ci];
1214
0
    htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
1215
0
        entropy->dc_count_ptrs[compptr->dc_tbl_no],
1216
0
        entropy->ac_count_ptrs[compptr->ac_tbl_no]);
1217
0
    entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
1218
0
  }
1219
1220
0
  return TRUE;
1221
0
}
1222
1223
1224
/*
1225
 * Generate the best Huffman code table for the given counts, fill htbl.
1226
 *
1227
 * The JPEG standard requires that no symbol be assigned a codeword of all
1228
 * one bits (so that padding bits added at the end of a compressed segment
1229
 * can't look like a valid code).  Because of the canonical ordering of
1230
 * codewords, this just means that there must be an unused slot in the
1231
 * longest codeword length category.  Section K.2 of the JPEG spec suggests
1232
 * reserving such a slot by pretending that symbol 256 is a valid symbol
1233
 * with count 1.  In theory that's not optimal; giving it count zero but
1234
 * including it in the symbol set anyway should give a better Huffman code.
1235
 * But the theoretically better code actually seems to come out worse in
1236
 * practice, because it produces more all-ones bytes (which incur stuffed
1237
 * zero bytes in the final file).  In any case the difference is tiny.
1238
 *
1239
 * The JPEG standard requires Huffman codes to be no more than 16 bits long.
1240
 * If some symbols have a very small but nonzero probability, the Huffman tree
1241
 * must be adjusted to meet the code length restriction.  We currently use
1242
 * the adjustment method suggested in JPEG section K.2.  This method is *not*
1243
 * optimal; it may not choose the best possible limited-length code.  But
1244
 * typically only very-low-frequency symbols will be given less-than-optimal
1245
 * lengths, so the code is almost optimal.  Experimental comparisons against
1246
 * an optimal limited-length-code algorithm indicate that the difference is
1247
 * microscopic --- usually less than a hundredth of a percent of total size.
1248
 * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
1249
 */
1250
1251
LOCAL(void)
1252
jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
1253
0
{
1254
0
#define MAX_CLEN 32    /* assumed maximum initial code length */
1255
0
  UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
1256
0
  int codesize[257];    /* codesize[k] = code length of symbol k */
1257
0
  int others[257];    /* next symbol in current branch of tree */
1258
0
  int c1, c2, i, j;
1259
0
  UINT8 *p;
1260
0
  long v;
1261
1262
0
  freq[256] = 1;    /* make sure 256 has a nonzero count */
1263
  /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
1264
   * that no real symbol is given code-value of all ones, because 256
1265
   * will be placed last in the largest codeword category.
1266
   * In the symbol list build procedure this element serves as sentinel
1267
   * for the zero run loop.
1268
   */
1269
1270
0
#ifndef DONT_USE_FANCY_HUFF_OPT
1271
1272
  /* Build list of symbols sorted in order of descending frequency */
1273
  /* This approach has several benefits (thank to John Korejwa for the idea):
1274
   *     1.
1275
   * If a codelength category is split during the length limiting procedure
1276
   * below, the feature that more frequent symbols are assigned shorter
1277
   * codewords remains valid for the adjusted code.
1278
   *     2.
1279
   * To reduce consecutive ones in a Huffman data stream (thus reducing the
1280
   * number of stuff bytes in JPEG) it is preferable to follow 0 branches
1281
   * (and avoid 1 branches) as much as possible.  This is easily done by
1282
   * assigning symbols to leaves of the Huffman tree in order of decreasing
1283
   * frequency, with no secondary sort based on codelengths.
1284
   *     3.
1285
   * The symbol list can be built independently from the assignment of code
1286
   * lengths by the Huffman procedure below.
1287
   * Note: The symbol list build procedure must be performed first, because
1288
   * the Huffman procedure assigning the codelengths clobbers the frequency
1289
   * counts!
1290
   */
1291
1292
  /* Here we use the others array as a linked list of nonzero frequencies
1293
   * to be sorted.  Already sorted elements are removed from the list.
1294
   */
1295
1296
  /* Building list */
1297
1298
  /* This item does not correspond to a valid symbol frequency and is used
1299
   * as starting index.
1300
   */
1301
0
  j = 256;
1302
1303
0
  for (i = 0;; i++) {
1304
0
    if (freq[i] == 0)   /* skip zero frequencies */
1305
0
      continue;
1306
0
    if (i > 255)
1307
0
      break;
1308
0
    others[j] = i;    /* this symbol value */
1309
0
    j = i;      /* previous symbol value */
1310
0
  }
1311
0
  others[j] = -1;   /* mark end of list */
1312
1313
  /* Sorting list */
1314
1315
0
  p = htbl->huffval;
1316
0
  while ((c1 = others[256]) >= 0) {
1317
0
    v = freq[c1];
1318
0
    i = c1;     /* first symbol value */
1319
0
    j = 256;      /* pseudo symbol value for starting index */
1320
0
    while ((c2 = others[c1]) >= 0) {
1321
0
      if (freq[c2] > v) {
1322
0
  v = freq[c2];
1323
0
  i = c2;     /* this symbol value */
1324
0
  j = c1;     /* previous symbol value */
1325
0
      }
1326
0
      c1 = c2;
1327
0
    }
1328
0
    others[j] = others[i];  /* remove this symbol i from list */
1329
0
    *p++ = (UINT8) i;
1330
0
  }
1331
1332
0
#endif /* DONT_USE_FANCY_HUFF_OPT */
1333
1334
  /* This algorithm is explained in section K.2 of the JPEG standard */
1335
1336
0
  MEMZERO(bits, SIZEOF(bits));
1337
0
  MEMZERO(codesize, SIZEOF(codesize));
1338
0
  for (i = 0; i < 257; i++)
1339
0
    others[i] = -1;   /* init links to empty */
1340
1341
  /* Huffman's basic algorithm to assign optimal code lengths to symbols */
1342
1343
0
  for (;;) {
1344
    /* Find the smallest nonzero frequency, set c1 = its symbol */
1345
    /* In case of ties, take the larger symbol number */
1346
0
    c1 = -1;
1347
0
    v = 1000000000L;
1348
0
    for (i = 0; i <= 256; i++) {
1349
0
      if (freq[i] && freq[i] <= v) {
1350
0
  v = freq[i];
1351
0
  c1 = i;
1352
0
      }
1353
0
    }
1354
1355
    /* Find the next smallest nonzero frequency, set c2 = its symbol */
1356
    /* In case of ties, take the larger symbol number */
1357
0
    c2 = -1;
1358
0
    v = 1000000000L;
1359
0
    for (i = 0; i <= 256; i++) {
1360
0
      if (freq[i] && freq[i] <= v && i != c1) {
1361
0
  v = freq[i];
1362
0
  c2 = i;
1363
0
      }
1364
0
    }
1365
1366
    /* Done if we've merged everything into one frequency */
1367
0
    if (c2 < 0)
1368
0
      break;
1369
1370
    /* Else merge the two counts/trees */
1371
0
    freq[c1] += freq[c2];
1372
0
    freq[c2] = 0;
1373
1374
    /* Increment the codesize of everything in c1's tree branch */
1375
0
    codesize[c1]++;
1376
0
    while (others[c1] >= 0) {
1377
0
      c1 = others[c1];
1378
0
      codesize[c1]++;
1379
0
    }
1380
1381
0
    others[c1] = c2;    /* chain c2 onto c1's tree branch */
1382
1383
    /* Increment the codesize of everything in c2's tree branch */
1384
0
    codesize[c2]++;
1385
0
    while (others[c2] >= 0) {
1386
0
      c2 = others[c2];
1387
0
      codesize[c2]++;
1388
0
    }
1389
0
  }
1390
1391
  /* Now count the number of symbols of each code length */
1392
0
  for (i = 0; i <= 256; i++) {
1393
0
    if (codesize[i]) {
1394
      /* The JPEG standard seems to think that this can't happen, */
1395
      /* but I'm paranoid... */
1396
0
      if (codesize[i] > MAX_CLEN)
1397
0
  ERREXIT(cinfo, JERR_HUFF_CLEN_OUTOFBOUNDS);
1398
1399
0
      bits[codesize[i]]++;
1400
0
    }
1401
0
  }
1402
1403
  /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
1404
   * Huffman procedure assigned any such lengths, we must adjust the coding.
1405
   * Here is what the JPEG spec says about how this next bit works:
1406
   * Since symbols are paired for the longest Huffman code, the symbols are
1407
   * removed from this length category two at a time.  The prefix for the pair
1408
   * (which is one bit shorter) is allocated to one of the pair; then,
1409
   * skipping the BITS entry for that prefix length, a code word from the next
1410
   * shortest nonzero BITS entry is converted into a prefix for two code words
1411
   * one bit longer.
1412
   */
1413
1414
0
  for (i = MAX_CLEN; i > 16; i--) {
1415
0
    while (bits[i] > 0) {
1416
0
      j = i - 2;    /* find length of new prefix to be used */
1417
0
      while (bits[j] == 0) {
1418
0
  if (j == 0)
1419
0
    ERREXIT(cinfo, JERR_HUFF_CLEN_OUTOFBOUNDS);
1420
0
  j--;
1421
0
      }
1422
1423
0
      bits[i] -= 2;   /* remove two symbols */
1424
0
      bits[i-1]++;    /* one goes in this length */
1425
0
      bits[j+1] += 2;   /* two new symbols in this length */
1426
0
      bits[j]--;    /* symbol of this length is now a prefix */
1427
0
    }
1428
0
  }
1429
1430
  /* Remove the count for the pseudo-symbol 256 from the largest codelength */
1431
0
  while (bits[i] == 0)   /* find largest codelength still in use */
1432
0
    i--;
1433
0
  bits[i]--;
1434
1435
  /* Return final symbol counts (only for lengths 0..16) */
1436
0
  MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
1437
1438
#ifdef DONT_USE_FANCY_HUFF_OPT
1439
1440
  /* Return a list of the symbols sorted by code length */
1441
  /* Note: Due to the codelength changes made above, it can happen
1442
   * that more frequent symbols are assigned longer codewords.
1443
   */
1444
  p = htbl->huffval;
1445
  for (i = 1; i <= MAX_CLEN; i++) {
1446
    for (j = 0; j <= 255; j++) {
1447
      if (codesize[j] == i) {
1448
  *p++ = (UINT8) j;
1449
      }
1450
    }
1451
  }
1452
1453
#endif /* DONT_USE_FANCY_HUFF_OPT */
1454
1455
  /* Set sent_table FALSE so updated table will be written to JPEG file. */
1456
0
  htbl->sent_table = FALSE;
1457
0
}
1458
1459
1460
/*
1461
 * Finish up a statistics-gathering pass and create the new Huffman tables.
1462
 */
1463
1464
METHODDEF(void)
1465
finish_pass_gather (j_compress_ptr cinfo)
1466
0
{
1467
0
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1468
0
  int ci, tbl;
1469
0
  jpeg_component_info * compptr;
1470
0
  JHUFF_TBL **htblptr;
1471
0
  boolean did_dc[NUM_HUFF_TBLS];
1472
0
  boolean did_ac[NUM_HUFF_TBLS];
1473
1474
0
  if (cinfo->progressive_mode)
1475
    /* Flush out buffered data (all we care about is counting the EOB symbol) */
1476
0
    emit_eobrun(entropy);
1477
1478
  /* It's important not to apply jpeg_gen_optimal_table more than once
1479
   * per table, because it clobbers the input frequency counts!
1480
   */
1481
0
  MEMZERO(did_dc, SIZEOF(did_dc));
1482
0
  MEMZERO(did_ac, SIZEOF(did_ac));
1483
1484
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1485
0
    compptr = cinfo->cur_comp_info[ci];
1486
    /* DC needs no table for refinement scan */
1487
0
    if (cinfo->Ss == 0 && cinfo->Ah == 0) {
1488
0
      tbl = compptr->dc_tbl_no;
1489
0
      if (! did_dc[tbl]) {
1490
0
  htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
1491
0
  if (*htblptr == NULL)
1492
0
    *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1493
0
  jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
1494
0
  did_dc[tbl] = TRUE;
1495
0
      }
1496
0
    }
1497
    /* AC needs no table when not present */
1498
0
    if (cinfo->Se) {
1499
0
      tbl = compptr->ac_tbl_no;
1500
0
      if (! did_ac[tbl]) {
1501
0
  htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
1502
0
  if (*htblptr == NULL)
1503
0
    *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1504
0
  jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
1505
0
  did_ac[tbl] = TRUE;
1506
0
      }
1507
0
    }
1508
0
  }
1509
0
}
1510
1511
1512
/*
1513
 * Initialize for a Huffman-compressed scan.
1514
 * If gather_statistics is TRUE, we do not output anything during the scan,
1515
 * just count the Huffman symbols used and generate Huffman code tables.
1516
 */
1517
1518
METHODDEF(void)
1519
start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
1520
0
{
1521
0
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1522
0
  int ci, tbl;
1523
0
  jpeg_component_info * compptr;
1524
1525
0
  if (gather_statistics)
1526
0
    entropy->pub.finish_pass = finish_pass_gather;
1527
0
  else
1528
0
    entropy->pub.finish_pass = finish_pass_huff;
1529
1530
0
  if (cinfo->progressive_mode) {
1531
0
    entropy->cinfo = cinfo;
1532
0
    entropy->gather_statistics = gather_statistics;
1533
1534
    /* We assume jcmaster.c already validated the scan parameters. */
1535
1536
    /* Select execution routine */
1537
0
    if (cinfo->Ah == 0) {
1538
0
      if (cinfo->Ss == 0)
1539
0
  entropy->pub.encode_mcu = encode_mcu_DC_first;
1540
0
      else
1541
0
  entropy->pub.encode_mcu = encode_mcu_AC_first;
1542
0
    } else {
1543
0
      if (cinfo->Ss == 0)
1544
0
  entropy->pub.encode_mcu = encode_mcu_DC_refine;
1545
0
      else {
1546
0
  entropy->pub.encode_mcu = encode_mcu_AC_refine;
1547
  /* AC refinement needs a correction bit buffer */
1548
0
  if (entropy->bit_buffer == NULL)
1549
0
    entropy->bit_buffer = (char *) (*cinfo->mem->alloc_small)
1550
0
      ((j_common_ptr) cinfo, JPOOL_IMAGE, MAX_CORR_BITS * SIZEOF(char));
1551
0
      }
1552
0
    }
1553
1554
    /* Initialize AC stuff */
1555
0
    entropy->ac_tbl_no = cinfo->cur_comp_info[0]->ac_tbl_no;
1556
0
    entropy->EOBRUN = 0;
1557
0
    entropy->BE = 0;
1558
0
  } else {
1559
0
    if (gather_statistics)
1560
0
      entropy->pub.encode_mcu = encode_mcu_gather;
1561
0
    else
1562
0
      entropy->pub.encode_mcu = encode_mcu_huff;
1563
0
  }
1564
1565
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1566
0
    compptr = cinfo->cur_comp_info[ci];
1567
    /* DC needs no table for refinement scan */
1568
0
    if (cinfo->Ss == 0 && cinfo->Ah == 0) {
1569
0
      tbl = compptr->dc_tbl_no;
1570
0
      if (gather_statistics) {
1571
  /* Check for invalid table index */
1572
  /* (make_c_derived_tbl does this in the other path) */
1573
0
  if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
1574
0
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
1575
  /* Allocate and zero the statistics tables */
1576
  /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
1577
0
  if (entropy->dc_count_ptrs[tbl] == NULL)
1578
0
    entropy->dc_count_ptrs[tbl] = (long *) (*cinfo->mem->alloc_small)
1579
0
      ((j_common_ptr) cinfo, JPOOL_IMAGE, 257 * SIZEOF(long));
1580
0
  MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
1581
0
      } else {
1582
  /* Compute derived values for Huffman tables */
1583
  /* We may do this more than once for a table, but it's not expensive */
1584
0
  jpeg_make_c_derived_tbl(cinfo, TRUE, tbl,
1585
0
        & entropy->dc_derived_tbls[tbl]);
1586
0
      }
1587
      /* Initialize DC predictions to 0 */
1588
0
      entropy->saved.last_dc_val[ci] = 0;
1589
0
    }
1590
    /* AC needs no table when not present */
1591
0
    if (cinfo->Se) {
1592
0
      tbl = compptr->ac_tbl_no;
1593
0
      if (gather_statistics) {
1594
0
  if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
1595
0
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
1596
0
  if (entropy->ac_count_ptrs[tbl] == NULL)
1597
0
    entropy->ac_count_ptrs[tbl] = (long *) (*cinfo->mem->alloc_small)
1598
0
      ((j_common_ptr) cinfo, JPOOL_IMAGE, 257 * SIZEOF(long));
1599
0
  MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
1600
0
      } else {
1601
0
  jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
1602
0
        & entropy->ac_derived_tbls[tbl]);
1603
0
      }
1604
0
    }
1605
0
  }
1606
1607
  /* Initialize bit buffer to empty */
1608
0
  entropy->saved.put_buffer = 0;
1609
0
  entropy->saved.put_bits = 0;
1610
1611
  /* Initialize restart stuff */
1612
0
  entropy->restarts_to_go = cinfo->restart_interval;
1613
0
  entropy->next_restart_num = 0;
1614
0
}
1615
1616
1617
/*
1618
 * Module initialization routine for Huffman entropy encoding.
1619
 */
1620
1621
GLOBAL(void)
1622
jinit_huff_encoder (j_compress_ptr cinfo)
1623
0
{
1624
0
  huff_entropy_ptr entropy;
1625
0
  int i;
1626
1627
0
  entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small)
1628
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(huff_entropy_encoder));
1629
0
  cinfo->entropy = &entropy->pub;
1630
0
  entropy->pub.start_pass = start_pass_huff;
1631
1632
  /* Mark tables unallocated */
1633
0
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
1634
0
    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
1635
0
    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
1636
0
  }
1637
1638
0
  if (cinfo->progressive_mode)
1639
0
    entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
1640
0
}