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