Coverage Report

Created: 2023-06-07 06:03

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