blob: 9bf96124b460a3b76fc633c75740377c9930747a [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * jcphuff.c
3 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -04004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1995-1997, Thomas G. Lane.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04006 * libjpeg-turbo Modifications:
Jonathan Wright24e31052021-04-26 12:10:48 +01007 * Copyright (C) 2011, 2015, 2018, 2021, D. R. Commander.
Chris Blumecca8c4d2019-03-01 01:09:50 -08008 * Copyright (C) 2016, 2018, Matthieu Darbois.
Jonathan Wright518d8152021-01-12 11:33:28 +00009 * Copyright (C) 2020, Arm Limited.
Tom Hudson0d47d2d2016-05-04 13:22:56 -040010 * For conditions of distribution and use, see the accompanying README.ijg
11 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000012 *
13 * This file contains Huffman entropy encoding routines for progressive JPEG.
14 *
15 * We do not support output suspension in this module, since the library
16 * currently does not allow multiple-scan files to be written with output
17 * suspension.
18 */
19
20#define JPEG_INTERNALS
21#include "jinclude.h"
22#include "jpeglib.h"
Chris Blumecca8c4d2019-03-01 01:09:50 -080023#include "jsimd.h"
24#include "jconfigint.h"
25#include <limits.h>
26
27#ifdef HAVE_INTRIN_H
28#include <intrin.h>
29#ifdef _MSC_VER
30#ifdef HAVE_BITSCANFORWARD64
31#pragma intrinsic(_BitScanForward64)
32#endif
33#ifdef HAVE_BITSCANFORWARD
34#pragma intrinsic(_BitScanForward)
35#endif
36#endif
37#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000038
39#ifdef C_PROGRESSIVE_SUPPORTED
40
Chris Blumecca8c4d2019-03-01 01:09:50 -080041/*
42 * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be
43 * used for bit counting rather than the lookup table. This will reduce the
44 * memory footprint by 64k, which is important for some mobile applications
45 * that create many isolated instances of libjpeg-turbo (web browsers, for
46 * instance.) This may improve performance on some mobile platforms as well.
Jonathan Wrightbbb82822020-11-25 13:36:43 +000047 * This feature is enabled by default only on Arm processors, because some x86
Chris Blumecca8c4d2019-03-01 01:09:50 -080048 * chips have a slow implementation of bsr, and the use of clz/bsr cannot be
49 * shown to have a significant performance impact even on the x86 chips that
Jonathan Wrightbbb82822020-11-25 13:36:43 +000050 * have a fast implementation of it. When building for Armv6, you can
Chris Blumecca8c4d2019-03-01 01:09:50 -080051 * explicitly disable the use of clz/bsr by adding -mthumb to the compiler
52 * flags (this defines __thumb__).
53 */
54
Peter Kasting2eb7e202021-07-09 07:13:34 -070055/* NOTE: Both GCC and Clang define __GNUC__ */
56#if (defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))) || \
57 defined(_M_ARM) || defined(_M_ARM64)
Jonathan Wrightdb870df2020-08-05 11:42:22 +010058#if !defined(__thumb__) || defined(__thumb2__)
Chris Blumecca8c4d2019-03-01 01:09:50 -080059#define USE_CLZ_INTRINSIC
60#endif
61#endif
62
63#ifdef USE_CLZ_INTRINSIC
Jonathan Wright518d8152021-01-12 11:33:28 +000064#if defined(_MSC_VER) && !defined(__clang__)
65#define JPEG_NBITS_NONZERO(x) (32 - _CountLeadingZeros(x))
66#else
Chris Blumecca8c4d2019-03-01 01:09:50 -080067#define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x))
Jonathan Wright518d8152021-01-12 11:33:28 +000068#endif
Chris Blumecca8c4d2019-03-01 01:09:50 -080069#define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0)
70#else
71#include "jpeg_nbits_table.h"
72#define JPEG_NBITS(x) (jpeg_nbits_table[x])
73#define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x)
74#endif
75
76
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000077/* Expanded entropy encoder object for progressive Huffman encoding. */
78
79typedef struct {
80 struct jpeg_entropy_encoder pub; /* public fields */
81
Chris Blumecca8c4d2019-03-01 01:09:50 -080082 /* Pointer to routine to prepare data for encode_mcu_AC_first() */
83 void (*AC_first_prepare) (const JCOEF *block,
84 const int *jpeg_natural_order_start, int Sl,
85 int Al, JCOEF *values, size_t *zerobits);
86 /* Pointer to routine to prepare data for encode_mcu_AC_refine() */
87 int (*AC_refine_prepare) (const JCOEF *block,
88 const int *jpeg_natural_order_start, int Sl,
89 int Al, JCOEF *absvalues, size_t *bits);
90
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000091 /* Mode flag: TRUE for optimization, FALSE for actual data output */
92 boolean gather_statistics;
93
94 /* Bit-level coding status.
95 * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
96 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040097 JOCTET *next_output_byte; /* => next byte to write in buffer */
98 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
99 size_t put_buffer; /* current bit-accumulation buffer */
100 int put_bits; /* # of bits now in it */
101 j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000102
103 /* Coding status for DC components */
104 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
105
106 /* Coding status for AC components */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400107 int ac_tbl_no; /* the table number of the single component */
108 unsigned int EOBRUN; /* run length of EOBs */
109 unsigned int BE; /* # of buffered correction bits before MCU */
110 char *bit_buffer; /* buffer for correction bits (1 per char) */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000111 /* packing correction bits tightly would save some space but cost time... */
112
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400113 unsigned int restarts_to_go; /* MCUs left in this restart interval */
114 int next_restart_num; /* next restart number to write (0-7) */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000115
116 /* Pointers to derived tables (these workspaces have image lifespan).
117 * Since any one scan codes only DC or only AC, we only need one set
118 * of tables, not one for DC and one for AC.
119 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400120 c_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000121
122 /* Statistics tables for optimization; again, one set is enough */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400123 long *count_ptrs[NUM_HUFF_TBLS];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000124} phuff_entropy_encoder;
125
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400126typedef phuff_entropy_encoder *phuff_entropy_ptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000127
128/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
129 * buffer can hold. Larger sizes may slightly improve compression, but
130 * 1000 is already well into the realm of overkill.
131 * The minimum safe size is 64 bits.
132 */
133
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400134#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000135
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400136/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.
137 * We assume that int right shift is unsigned if JLONG right shift is,
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000138 * which should be safe.
139 */
140
141#ifdef RIGHT_SHIFT_IS_UNSIGNED
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400142#define ISHIFT_TEMPS int ishift_temp;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800143#define IRIGHT_SHIFT(x, shft) \
144 ((ishift_temp = (x)) < 0 ? \
145 (ishift_temp >> (shft)) | ((~0) << (16 - (shft))) : \
146 (ishift_temp >> (shft)))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000147#else
148#define ISHIFT_TEMPS
Chris Blumecca8c4d2019-03-01 01:09:50 -0800149#define IRIGHT_SHIFT(x, shft) ((x) >> (shft))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000150#endif
151
Chris Blumecca8c4d2019-03-01 01:09:50 -0800152#define PAD(v, p) ((v + (p) - 1) & (~((p) - 1)))
153
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000154/* Forward declarations */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800155METHODDEF(boolean) encode_mcu_DC_first(j_compress_ptr cinfo,
156 JBLOCKROW *MCU_data);
157METHODDEF(void) encode_mcu_AC_first_prepare
158 (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
159 JCOEF *values, size_t *zerobits);
160METHODDEF(boolean) encode_mcu_AC_first(j_compress_ptr cinfo,
161 JBLOCKROW *MCU_data);
162METHODDEF(boolean) encode_mcu_DC_refine(j_compress_ptr cinfo,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400163 JBLOCKROW *MCU_data);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800164METHODDEF(int) encode_mcu_AC_refine_prepare
165 (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
166 JCOEF *absvalues, size_t *bits);
167METHODDEF(boolean) encode_mcu_AC_refine(j_compress_ptr cinfo,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400168 JBLOCKROW *MCU_data);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800169METHODDEF(void) finish_pass_phuff(j_compress_ptr cinfo);
170METHODDEF(void) finish_pass_gather_phuff(j_compress_ptr cinfo);
171
172
173/* Count bit loop zeroes */
174INLINE
175METHODDEF(int)
176count_zeroes(size_t *x)
177{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800178#if defined(HAVE_BUILTIN_CTZL)
Jonathan Wright24e31052021-04-26 12:10:48 +0100179 int result;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800180 result = __builtin_ctzl(*x);
181 *x >>= result;
182#elif defined(HAVE_BITSCANFORWARD64)
Jonathan Wright24e31052021-04-26 12:10:48 +0100183 unsigned long result;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800184 _BitScanForward64(&result, *x);
185 *x >>= result;
186#elif defined(HAVE_BITSCANFORWARD)
Jonathan Wright24e31052021-04-26 12:10:48 +0100187 unsigned long result;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800188 _BitScanForward(&result, *x);
189 *x >>= result;
190#else
Jonathan Wright24e31052021-04-26 12:10:48 +0100191 int result = 0;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800192 while ((*x & 1) == 0) {
193 ++result;
194 *x >>= 1;
195 }
196#endif
Jonathan Wright24e31052021-04-26 12:10:48 +0100197 return (int)result;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800198}
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000199
200
201/*
202 * Initialize for a Huffman-compressed scan using progressive JPEG.
203 */
204
205METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800206start_pass_phuff(j_compress_ptr cinfo, boolean gather_statistics)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400207{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800208 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000209 boolean is_DC_band;
210 int ci, tbl;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400211 jpeg_component_info *compptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000212
213 entropy->cinfo = cinfo;
214 entropy->gather_statistics = gather_statistics;
215
216 is_DC_band = (cinfo->Ss == 0);
217
218 /* We assume jcmaster.c already validated the scan parameters. */
219
220 /* Select execution routines */
221 if (cinfo->Ah == 0) {
222 if (is_DC_band)
223 entropy->pub.encode_mcu = encode_mcu_DC_first;
224 else
225 entropy->pub.encode_mcu = encode_mcu_AC_first;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800226 if (jsimd_can_encode_mcu_AC_first_prepare())
227 entropy->AC_first_prepare = jsimd_encode_mcu_AC_first_prepare;
228 else
229 entropy->AC_first_prepare = encode_mcu_AC_first_prepare;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000230 } else {
231 if (is_DC_band)
232 entropy->pub.encode_mcu = encode_mcu_DC_refine;
233 else {
234 entropy->pub.encode_mcu = encode_mcu_AC_refine;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800235 if (jsimd_can_encode_mcu_AC_refine_prepare())
236 entropy->AC_refine_prepare = jsimd_encode_mcu_AC_refine_prepare;
237 else
238 entropy->AC_refine_prepare = encode_mcu_AC_refine_prepare;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000239 /* AC refinement needs a correction bit buffer */
240 if (entropy->bit_buffer == NULL)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400241 entropy->bit_buffer = (char *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800242 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400243 MAX_CORR_BITS * sizeof(char));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000244 }
245 }
246 if (gather_statistics)
247 entropy->pub.finish_pass = finish_pass_gather_phuff;
248 else
249 entropy->pub.finish_pass = finish_pass_phuff;
250
251 /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
252 * for AC coefficients.
253 */
254 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
255 compptr = cinfo->cur_comp_info[ci];
256 /* Initialize DC predictions to 0 */
257 entropy->last_dc_val[ci] = 0;
258 /* Get table index */
259 if (is_DC_band) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400260 if (cinfo->Ah != 0) /* DC refinement needs no table */
261 continue;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000262 tbl = compptr->dc_tbl_no;
263 } else {
264 entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
265 }
266 if (gather_statistics) {
267 /* Check for invalid table index */
268 /* (make_c_derived_tbl does this in the other path) */
269 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
270 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
271 /* Allocate and zero the statistics tables */
272 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
273 if (entropy->count_ptrs[tbl] == NULL)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400274 entropy->count_ptrs[tbl] = (long *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800275 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400276 257 * sizeof(long));
277 MEMZERO(entropy->count_ptrs[tbl], 257 * sizeof(long));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000278 } else {
279 /* Compute derived values for Huffman table */
280 /* We may do this more than once for a table, but it's not expensive */
281 jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
Chris Blumecca8c4d2019-03-01 01:09:50 -0800282 &entropy->derived_tbls[tbl]);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000283 }
284 }
285
286 /* Initialize AC stuff */
287 entropy->EOBRUN = 0;
288 entropy->BE = 0;
289
290 /* Initialize bit buffer to empty */
291 entropy->put_buffer = 0;
292 entropy->put_bits = 0;
293
294 /* Initialize restart stuff */
295 entropy->restarts_to_go = cinfo->restart_interval;
296 entropy->next_restart_num = 0;
297}
298
299
300/* Outputting bytes to the file.
301 * NB: these must be called only when actually outputting,
302 * that is, entropy->gather_statistics == FALSE.
303 */
304
305/* Emit a byte */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800306#define emit_byte(entropy, val) { \
307 *(entropy)->next_output_byte++ = (JOCTET)(val); \
308 if (--(entropy)->free_in_buffer == 0) \
309 dump_buffer(entropy); \
310}
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000311
312
313LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800314dump_buffer(phuff_entropy_ptr entropy)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000315/* Empty the output buffer; we do not support suspension in this module. */
316{
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400317 struct jpeg_destination_mgr *dest = entropy->cinfo->dest;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000318
Chris Blumecca8c4d2019-03-01 01:09:50 -0800319 if (!(*dest->empty_output_buffer) (entropy->cinfo))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000320 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
321 /* After a successful buffer dump, must reset buffer pointers */
322 entropy->next_output_byte = dest->next_output_byte;
323 entropy->free_in_buffer = dest->free_in_buffer;
324}
325
326
327/* Outputting bits to the file */
328
329/* Only the right 24 bits of put_buffer are used; the valid bits are
330 * left-justified in this part. At most 16 bits can be passed to emit_bits
331 * in one call, and we never retain more than 7 bits in put_buffer
332 * between calls, so 24 bits are sufficient.
333 */
334
335LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800336emit_bits(phuff_entropy_ptr entropy, unsigned int code, int size)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000337/* Emit some bits, unless we are in gather mode */
338{
339 /* This routine is heavily used, so it's worth coding tightly. */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800340 register size_t put_buffer = (size_t)code;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000341 register int put_bits = entropy->put_bits;
342
343 /* if size is 0, caller used an invalid Huffman table entry */
344 if (size == 0)
345 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
346
347 if (entropy->gather_statistics)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400348 return; /* do nothing if we're only getting stats */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000349
Chris Blumecca8c4d2019-03-01 01:09:50 -0800350 put_buffer &= (((size_t)1) << size) - 1; /* mask off any extra bits in code */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400351
352 put_bits += size; /* new number of bits in buffer */
353
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000354 put_buffer <<= 24 - put_bits; /* align incoming bits */
355
356 put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
357
358 while (put_bits >= 8) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800359 int c = (int)((put_buffer >> 16) & 0xFF);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400360
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000361 emit_byte(entropy, c);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400362 if (c == 0xFF) { /* need to stuff a zero byte? */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000363 emit_byte(entropy, 0);
364 }
365 put_buffer <<= 8;
366 put_bits -= 8;
367 }
368
369 entropy->put_buffer = put_buffer; /* update variables */
370 entropy->put_bits = put_bits;
371}
372
373
374LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800375flush_bits(phuff_entropy_ptr entropy)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000376{
377 emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
378 entropy->put_buffer = 0; /* and reset bit-buffer to empty */
379 entropy->put_bits = 0;
380}
381
382
383/*
384 * Emit (or just count) a Huffman symbol.
385 */
386
387LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800388emit_symbol(phuff_entropy_ptr entropy, int tbl_no, int symbol)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000389{
390 if (entropy->gather_statistics)
391 entropy->count_ptrs[tbl_no][symbol]++;
392 else {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400393 c_derived_tbl *tbl = entropy->derived_tbls[tbl_no];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000394 emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
395 }
396}
397
398
399/*
400 * Emit bits from a correction bit buffer.
401 */
402
403LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800404emit_buffered_bits(phuff_entropy_ptr entropy, char *bufstart,
405 unsigned int nbits)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000406{
407 if (entropy->gather_statistics)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400408 return; /* no real work */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000409
410 while (nbits > 0) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800411 emit_bits(entropy, (unsigned int)(*bufstart), 1);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000412 bufstart++;
413 nbits--;
414 }
415}
416
417
418/*
419 * Emit any pending EOBRUN symbol.
420 */
421
422LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800423emit_eobrun(phuff_entropy_ptr entropy)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000424{
425 register int temp, nbits;
426
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400427 if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000428 temp = entropy->EOBRUN;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800429 nbits = JPEG_NBITS_NONZERO(temp) - 1;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000430 /* safety check: shouldn't happen given limited correction-bit buffer */
431 if (nbits > 14)
432 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
433
434 emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
435 if (nbits)
436 emit_bits(entropy, entropy->EOBRUN, nbits);
437
438 entropy->EOBRUN = 0;
439
440 /* Emit any buffered correction bits */
441 emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
442 entropy->BE = 0;
443 }
444}
445
446
447/*
448 * Emit a restart marker & resynchronize predictions.
449 */
450
451LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800452emit_restart(phuff_entropy_ptr entropy, int restart_num)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000453{
454 int ci;
455
456 emit_eobrun(entropy);
457
Chris Blumecca8c4d2019-03-01 01:09:50 -0800458 if (!entropy->gather_statistics) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000459 flush_bits(entropy);
460 emit_byte(entropy, 0xFF);
461 emit_byte(entropy, JPEG_RST0 + restart_num);
462 }
463
464 if (entropy->cinfo->Ss == 0) {
465 /* Re-initialize DC predictions to 0 */
466 for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
467 entropy->last_dc_val[ci] = 0;
468 } else {
469 /* Re-initialize all AC-related fields to 0 */
470 entropy->EOBRUN = 0;
471 entropy->BE = 0;
472 }
473}
474
475
476/*
477 * MCU encoding for DC initial scan (either spectral selection,
478 * or first pass of successive approximation).
479 */
480
481METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800482encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000483{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800484 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
485 register int temp, temp2, temp3;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000486 register int nbits;
487 int blkn, ci;
488 int Al = cinfo->Al;
489 JBLOCKROW block;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400490 jpeg_component_info *compptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000491 ISHIFT_TEMPS
492
493 entropy->next_output_byte = cinfo->dest->next_output_byte;
494 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
495
496 /* Emit restart marker if needed */
497 if (cinfo->restart_interval)
498 if (entropy->restarts_to_go == 0)
499 emit_restart(entropy, entropy->next_restart_num);
500
501 /* Encode the MCU data blocks */
502 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
503 block = MCU_data[blkn];
504 ci = cinfo->MCU_membership[blkn];
505 compptr = cinfo->cur_comp_info[ci];
506
507 /* Compute the DC value after the required point transform by Al.
508 * This is simply an arithmetic right shift.
509 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800510 temp2 = IRIGHT_SHIFT((int)((*block)[0]), Al);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000511
512 /* DC differences are figured on the point-transformed values. */
513 temp = temp2 - entropy->last_dc_val[ci];
514 entropy->last_dc_val[ci] = temp2;
515
516 /* Encode the DC coefficient difference per section G.1.2.1 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800517
518 /* This is a well-known technique for obtaining the absolute value without
519 * a branch. It is derived from an assembly language technique presented
520 * in "How to Optimize for the Pentium Processors", Copyright (c) 1996,
521 * 1997 by Agner Fog.
522 */
523 temp3 = temp >> (CHAR_BIT * sizeof(int) - 1);
524 temp ^= temp3;
525 temp -= temp3; /* temp is abs value of input */
526 /* For a negative input, want temp2 = bitwise complement of abs(input) */
527 temp2 = temp ^ temp3;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400528
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000529 /* Find the number of bits needed for the magnitude of the coefficient */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800530 nbits = JPEG_NBITS(temp);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000531 /* Check for out-of-range coefficient values.
532 * Since we're encoding a difference, the range limit is twice as much.
533 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800534 if (nbits > MAX_COEF_BITS + 1)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000535 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400536
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000537 /* Count/emit the Huffman-coded symbol for the number of bits */
538 emit_symbol(entropy, compptr->dc_tbl_no, nbits);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400539
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000540 /* Emit that number of bits of the value, if positive, */
541 /* or the complement of its magnitude, if negative. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400542 if (nbits) /* emit_bits rejects calls with size 0 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800543 emit_bits(entropy, (unsigned int)temp2, nbits);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000544 }
545
546 cinfo->dest->next_output_byte = entropy->next_output_byte;
547 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
548
549 /* Update restart-interval state too */
550 if (cinfo->restart_interval) {
551 if (entropy->restarts_to_go == 0) {
552 entropy->restarts_to_go = cinfo->restart_interval;
553 entropy->next_restart_num++;
554 entropy->next_restart_num &= 7;
555 }
556 entropy->restarts_to_go--;
557 }
558
559 return TRUE;
560}
561
562
563/*
Chris Blumecca8c4d2019-03-01 01:09:50 -0800564 * Data preparation for encode_mcu_AC_first().
565 */
566
567#define COMPUTE_ABSVALUES_AC_FIRST(Sl) { \
568 for (k = 0; k < Sl; k++) { \
569 temp = block[jpeg_natural_order_start[k]]; \
570 if (temp == 0) \
571 continue; \
572 /* We must apply the point transform by Al. For AC coefficients this \
573 * is an integer division with rounding towards 0. To do this portably \
574 * in C, we shift after obtaining the absolute value; so the code is \
575 * interwoven with finding the abs value (temp) and output bits (temp2). \
576 */ \
577 temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
578 temp ^= temp2; \
579 temp -= temp2; /* temp is abs value of input */ \
580 temp >>= Al; /* apply the point transform */ \
581 /* Watch out for case that nonzero coef is zero after point transform */ \
582 if (temp == 0) \
583 continue; \
584 /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ \
585 temp2 ^= temp; \
586 values[k] = temp; \
587 values[k + DCTSIZE2] = temp2; \
588 zerobits |= ((size_t)1U) << k; \
589 } \
590}
591
592METHODDEF(void)
593encode_mcu_AC_first_prepare(const JCOEF *block,
594 const int *jpeg_natural_order_start, int Sl,
595 int Al, JCOEF *values, size_t *bits)
596{
597 register int k, temp, temp2;
598 size_t zerobits = 0U;
599 int Sl0 = Sl;
600
601#if SIZEOF_SIZE_T == 4
602 if (Sl0 > 32)
603 Sl0 = 32;
604#endif
605
606 COMPUTE_ABSVALUES_AC_FIRST(Sl0);
607
608 bits[0] = zerobits;
609#if SIZEOF_SIZE_T == 4
610 zerobits = 0U;
611
612 if (Sl > 32) {
613 Sl -= 32;
614 jpeg_natural_order_start += 32;
615 values += 32;
616
617 COMPUTE_ABSVALUES_AC_FIRST(Sl);
618 }
619 bits[1] = zerobits;
620#endif
621}
622
623/*
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000624 * MCU encoding for AC initial scan (either spectral selection,
625 * or first pass of successive approximation).
626 */
627
Chris Blumecca8c4d2019-03-01 01:09:50 -0800628#define ENCODE_COEFS_AC_FIRST(label) { \
629 while (zerobits) { \
630 r = count_zeroes(&zerobits); \
631 cvalue += r; \
632label \
633 temp = cvalue[0]; \
634 temp2 = cvalue[DCTSIZE2]; \
635 \
636 /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \
637 while (r > 15) { \
638 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
639 r -= 16; \
640 } \
641 \
642 /* Find the number of bits needed for the magnitude of the coefficient */ \
643 nbits = JPEG_NBITS_NONZERO(temp); /* there must be at least one 1 bit */ \
644 /* Check for out-of-range coefficient values */ \
645 if (nbits > MAX_COEF_BITS) \
646 ERREXIT(cinfo, JERR_BAD_DCT_COEF); \
647 \
648 /* Count/emit Huffman symbol for run length / number of bits */ \
649 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); \
650 \
651 /* Emit that number of bits of the value, if positive, */ \
652 /* or the complement of its magnitude, if negative. */ \
653 emit_bits(entropy, (unsigned int)temp2, nbits); \
654 \
655 cvalue++; \
656 zerobits >>= 1; \
657 } \
658}
659
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000660METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800661encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000662{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800663 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000664 register int temp, temp2;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800665 register int nbits, r;
666 int Sl = cinfo->Se - cinfo->Ss + 1;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000667 int Al = cinfo->Al;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800668 JCOEF values_unaligned[2 * DCTSIZE2 + 15];
669 JCOEF *values;
670 const JCOEF *cvalue;
671 size_t zerobits;
672 size_t bits[8 / SIZEOF_SIZE_T];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000673
674 entropy->next_output_byte = cinfo->dest->next_output_byte;
675 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
676
677 /* Emit restart marker if needed */
678 if (cinfo->restart_interval)
679 if (entropy->restarts_to_go == 0)
680 emit_restart(entropy, entropy->next_restart_num);
681
Chris Blumecca8c4d2019-03-01 01:09:50 -0800682#ifdef WITH_SIMD
683 cvalue = values = (JCOEF *)PAD((size_t)values_unaligned, 16);
684#else
685 /* Not using SIMD, so alignment is not needed */
686 cvalue = values = values_unaligned;
687#endif
688
689 /* Prepare data */
690 entropy->AC_first_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
691 Sl, Al, values, bits);
692
693 zerobits = bits[0];
694#if SIZEOF_SIZE_T == 4
695 zerobits |= bits[1];
696#endif
697
698 /* Emit any pending EOBRUN */
699 if (zerobits && (entropy->EOBRUN > 0))
700 emit_eobrun(entropy);
701
702#if SIZEOF_SIZE_T == 4
703 zerobits = bits[0];
704#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000705
706 /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400707
Chris Blumecca8c4d2019-03-01 01:09:50 -0800708 ENCODE_COEFS_AC_FIRST((void)0;);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400709
Chris Blumecca8c4d2019-03-01 01:09:50 -0800710#if SIZEOF_SIZE_T == 4
711 zerobits = bits[1];
712 if (zerobits) {
713 int diff = ((values + DCTSIZE2 / 2) - cvalue);
714 r = count_zeroes(&zerobits);
715 r += diff;
716 cvalue += r;
717 goto first_iter_ac_first;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000718 }
719
Chris Blumecca8c4d2019-03-01 01:09:50 -0800720 ENCODE_COEFS_AC_FIRST(first_iter_ac_first:);
721#endif
722
723 if (cvalue < (values + Sl)) { /* If there are trailing zeroes, */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400724 entropy->EOBRUN++; /* count an EOB */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000725 if (entropy->EOBRUN == 0x7FFF)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400726 emit_eobrun(entropy); /* force it out to avoid overflow */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000727 }
728
729 cinfo->dest->next_output_byte = entropy->next_output_byte;
730 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
731
732 /* Update restart-interval state too */
733 if (cinfo->restart_interval) {
734 if (entropy->restarts_to_go == 0) {
735 entropy->restarts_to_go = cinfo->restart_interval;
736 entropy->next_restart_num++;
737 entropy->next_restart_num &= 7;
738 }
739 entropy->restarts_to_go--;
740 }
741
742 return TRUE;
743}
744
745
746/*
747 * MCU encoding for DC successive approximation refinement scan.
748 * Note: we assume such scans can be multi-component, although the spec
749 * is not very clear on the point.
750 */
751
752METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800753encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000754{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800755 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000756 register int temp;
757 int blkn;
758 int Al = cinfo->Al;
759 JBLOCKROW block;
760
761 entropy->next_output_byte = cinfo->dest->next_output_byte;
762 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
763
764 /* Emit restart marker if needed */
765 if (cinfo->restart_interval)
766 if (entropy->restarts_to_go == 0)
767 emit_restart(entropy, entropy->next_restart_num);
768
769 /* Encode the MCU data blocks */
770 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
771 block = MCU_data[blkn];
772
773 /* We simply emit the Al'th bit of the DC coefficient value. */
774 temp = (*block)[0];
Chris Blumecca8c4d2019-03-01 01:09:50 -0800775 emit_bits(entropy, (unsigned int)(temp >> Al), 1);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000776 }
777
778 cinfo->dest->next_output_byte = entropy->next_output_byte;
779 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
780
781 /* Update restart-interval state too */
782 if (cinfo->restart_interval) {
783 if (entropy->restarts_to_go == 0) {
784 entropy->restarts_to_go = cinfo->restart_interval;
785 entropy->next_restart_num++;
786 entropy->next_restart_num &= 7;
787 }
788 entropy->restarts_to_go--;
789 }
790
791 return TRUE;
792}
793
794
795/*
Chris Blumecca8c4d2019-03-01 01:09:50 -0800796 * Data preparation for encode_mcu_AC_refine().
797 */
798
799#define COMPUTE_ABSVALUES_AC_REFINE(Sl, koffset) { \
800 /* It is convenient to make a pre-pass to determine the transformed \
801 * coefficients' absolute values and the EOB position. \
802 */ \
803 for (k = 0; k < Sl; k++) { \
804 temp = block[jpeg_natural_order_start[k]]; \
805 /* We must apply the point transform by Al. For AC coefficients this \
806 * is an integer division with rounding towards 0. To do this portably \
807 * in C, we shift after obtaining the absolute value. \
808 */ \
809 temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
810 temp ^= temp2; \
811 temp -= temp2; /* temp is abs value of input */ \
812 temp >>= Al; /* apply the point transform */ \
813 if (temp != 0) { \
814 zerobits |= ((size_t)1U) << k; \
815 signbits |= ((size_t)(temp2 + 1)) << k; \
816 } \
817 absvalues[k] = (JCOEF)temp; /* save abs value for main pass */ \
818 if (temp == 1) \
819 EOB = k + koffset; /* EOB = index of last newly-nonzero coef */ \
820 } \
821}
822
823METHODDEF(int)
824encode_mcu_AC_refine_prepare(const JCOEF *block,
825 const int *jpeg_natural_order_start, int Sl,
826 int Al, JCOEF *absvalues, size_t *bits)
827{
828 register int k, temp, temp2;
829 int EOB = 0;
830 size_t zerobits = 0U, signbits = 0U;
831 int Sl0 = Sl;
832
833#if SIZEOF_SIZE_T == 4
834 if (Sl0 > 32)
835 Sl0 = 32;
836#endif
837
838 COMPUTE_ABSVALUES_AC_REFINE(Sl0, 0);
839
840 bits[0] = zerobits;
841#if SIZEOF_SIZE_T == 8
842 bits[1] = signbits;
843#else
844 bits[2] = signbits;
845
846 zerobits = 0U;
847 signbits = 0U;
848
849 if (Sl > 32) {
850 Sl -= 32;
851 jpeg_natural_order_start += 32;
852 absvalues += 32;
853
854 COMPUTE_ABSVALUES_AC_REFINE(Sl, 32);
855 }
856
857 bits[1] = zerobits;
858 bits[3] = signbits;
859#endif
860
861 return EOB;
862}
863
864
865/*
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000866 * MCU encoding for AC successive approximation refinement scan.
867 */
868
Chris Blumecca8c4d2019-03-01 01:09:50 -0800869#define ENCODE_COEFS_AC_REFINE(label) { \
870 while (zerobits) { \
Jonathan Wright24e31052021-04-26 12:10:48 +0100871 idx = count_zeroes(&zerobits); \
Chris Blumecca8c4d2019-03-01 01:09:50 -0800872 r += idx; \
873 cabsvalue += idx; \
874 signbits >>= idx; \
875label \
876 /* Emit any required ZRLs, but not if they can be folded into EOB */ \
877 while (r > 15 && (cabsvalue <= EOBPTR)) { \
878 /* emit any pending EOBRUN and the BE correction bits */ \
879 emit_eobrun(entropy); \
880 /* Emit ZRL */ \
881 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
882 r -= 16; \
883 /* Emit buffered correction bits that must be associated with ZRL */ \
884 emit_buffered_bits(entropy, BR_buffer, BR); \
885 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
886 BR = 0; \
887 } \
888 \
889 temp = *cabsvalue++; \
890 \
891 /* If the coef was previously nonzero, it only needs a correction bit. \
892 * NOTE: a straight translation of the spec's figure G.7 would suggest \
893 * that we also need to test r > 15. But if r > 15, we can only get here \
894 * if k > EOB, which implies that this coefficient is not 1. \
895 */ \
896 if (temp > 1) { \
897 /* The correction bit is the next bit of the absolute value. */ \
898 BR_buffer[BR++] = (char)(temp & 1); \
899 signbits >>= 1; \
900 zerobits >>= 1; \
901 continue; \
902 } \
903 \
904 /* Emit any pending EOBRUN and the BE correction bits */ \
905 emit_eobrun(entropy); \
906 \
907 /* Count/emit Huffman symbol for run length / number of bits */ \
908 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); \
909 \
910 /* Emit output bit for newly-nonzero coef */ \
911 temp = signbits & 1; /* ((*block)[jpeg_natural_order_start[k]] < 0) ? 0 : 1 */ \
912 emit_bits(entropy, (unsigned int)temp, 1); \
913 \
914 /* Emit buffered correction bits that must be associated with this code */ \
915 emit_buffered_bits(entropy, BR_buffer, BR); \
916 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
917 BR = 0; \
918 r = 0; /* reset zero run length */ \
919 signbits >>= 1; \
920 zerobits >>= 1; \
921 } \
922}
923
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000924METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800925encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000926{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800927 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
Jonathan Wright24e31052021-04-26 12:10:48 +0100928 register int temp, r, idx;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000929 char *BR_buffer;
930 unsigned int BR;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800931 int Sl = cinfo->Se - cinfo->Ss + 1;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000932 int Al = cinfo->Al;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800933 JCOEF absvalues_unaligned[DCTSIZE2 + 15];
934 JCOEF *absvalues;
935 const JCOEF *cabsvalue, *EOBPTR;
936 size_t zerobits, signbits;
937 size_t bits[16 / SIZEOF_SIZE_T];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000938
939 entropy->next_output_byte = cinfo->dest->next_output_byte;
940 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
941
942 /* Emit restart marker if needed */
943 if (cinfo->restart_interval)
944 if (entropy->restarts_to_go == 0)
945 emit_restart(entropy, entropy->next_restart_num);
946
Chris Blumecca8c4d2019-03-01 01:09:50 -0800947#ifdef WITH_SIMD
948 cabsvalue = absvalues = (JCOEF *)PAD((size_t)absvalues_unaligned, 16);
949#else
950 /* Not using SIMD, so alignment is not needed */
951 cabsvalue = absvalues = absvalues_unaligned;
952#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000953
Chris Blumecca8c4d2019-03-01 01:09:50 -0800954 /* Prepare data */
955 EOBPTR = absvalues +
956 entropy->AC_refine_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
957 Sl, Al, absvalues, bits);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000958
959 /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400960
961 r = 0; /* r = run length of zeros */
962 BR = 0; /* BR = count of buffered bits added now */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000963 BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
964
Chris Blumecca8c4d2019-03-01 01:09:50 -0800965 zerobits = bits[0];
966#if SIZEOF_SIZE_T == 8
967 signbits = bits[1];
968#else
969 signbits = bits[2];
970#endif
971 ENCODE_COEFS_AC_REFINE((void)0;);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000972
Chris Blumecca8c4d2019-03-01 01:09:50 -0800973#if SIZEOF_SIZE_T == 4
974 zerobits = bits[1];
975 signbits = bits[3];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000976
Chris Blumecca8c4d2019-03-01 01:09:50 -0800977 if (zerobits) {
978 int diff = ((absvalues + DCTSIZE2 / 2) - cabsvalue);
Jonathan Wright24e31052021-04-26 12:10:48 +0100979 idx = count_zeroes(&zerobits);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800980 signbits >>= idx;
981 idx += diff;
982 r += idx;
983 cabsvalue += idx;
984 goto first_iter_ac_refine;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000985 }
986
Chris Blumecca8c4d2019-03-01 01:09:50 -0800987 ENCODE_COEFS_AC_REFINE(first_iter_ac_refine:);
988#endif
989
990 r |= (int)((absvalues + Sl) - cabsvalue);
991
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400992 if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
993 entropy->EOBRUN++; /* count an EOB */
994 entropy->BE += BR; /* concat my correction bits to older ones */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000995 /* We force out the EOB if we risk either:
996 * 1. overflow of the EOB counter;
997 * 2. overflow of the correction bit buffer during the next MCU.
998 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800999 if (entropy->EOBRUN == 0x7FFF ||
1000 entropy->BE > (MAX_CORR_BITS - DCTSIZE2 + 1))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001001 emit_eobrun(entropy);
1002 }
1003
1004 cinfo->dest->next_output_byte = entropy->next_output_byte;
1005 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1006
1007 /* Update restart-interval state too */
1008 if (cinfo->restart_interval) {
1009 if (entropy->restarts_to_go == 0) {
1010 entropy->restarts_to_go = cinfo->restart_interval;
1011 entropy->next_restart_num++;
1012 entropy->next_restart_num &= 7;
1013 }
1014 entropy->restarts_to_go--;
1015 }
1016
1017 return TRUE;
1018}
1019
1020
1021/*
1022 * Finish up at the end of a Huffman-compressed progressive scan.
1023 */
1024
1025METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -08001026finish_pass_phuff(j_compress_ptr cinfo)
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001027{
Chris Blumecca8c4d2019-03-01 01:09:50 -08001028 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001029
1030 entropy->next_output_byte = cinfo->dest->next_output_byte;
1031 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
1032
1033 /* Flush out any buffered data */
1034 emit_eobrun(entropy);
1035 flush_bits(entropy);
1036
1037 cinfo->dest->next_output_byte = entropy->next_output_byte;
1038 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1039}
1040
1041
1042/*
1043 * Finish up a statistics-gathering pass and create the new Huffman tables.
1044 */
1045
1046METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -08001047finish_pass_gather_phuff(j_compress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001048{
Chris Blumecca8c4d2019-03-01 01:09:50 -08001049 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001050 boolean is_DC_band;
1051 int ci, tbl;
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001052 jpeg_component_info *compptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001053 JHUFF_TBL **htblptr;
1054 boolean did[NUM_HUFF_TBLS];
1055
1056 /* Flush out buffered data (all we care about is counting the EOB symbol) */
1057 emit_eobrun(entropy);
1058
1059 is_DC_band = (cinfo->Ss == 0);
1060
1061 /* It's important not to apply jpeg_gen_optimal_table more than once
1062 * per table, because it clobbers the input frequency counts!
1063 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001064 MEMZERO(did, sizeof(did));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001065
1066 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1067 compptr = cinfo->cur_comp_info[ci];
1068 if (is_DC_band) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001069 if (cinfo->Ah != 0) /* DC refinement needs no table */
1070 continue;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001071 tbl = compptr->dc_tbl_no;
1072 } else {
1073 tbl = compptr->ac_tbl_no;
1074 }
Chris Blumecca8c4d2019-03-01 01:09:50 -08001075 if (!did[tbl]) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001076 if (is_DC_band)
Chris Blumecca8c4d2019-03-01 01:09:50 -08001077 htblptr = &cinfo->dc_huff_tbl_ptrs[tbl];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001078 else
Chris Blumecca8c4d2019-03-01 01:09:50 -08001079 htblptr = &cinfo->ac_huff_tbl_ptrs[tbl];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001080 if (*htblptr == NULL)
Chris Blumecca8c4d2019-03-01 01:09:50 -08001081 *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001082 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
1083 did[tbl] = TRUE;
1084 }
1085 }
1086}
1087
1088
1089/*
1090 * Module initialization routine for progressive Huffman entropy encoding.
1091 */
1092
1093GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -08001094jinit_phuff_encoder(j_compress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001095{
1096 phuff_entropy_ptr entropy;
1097 int i;
1098
1099 entropy = (phuff_entropy_ptr)
Chris Blumecca8c4d2019-03-01 01:09:50 -08001100 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001101 sizeof(phuff_entropy_encoder));
Chris Blumecca8c4d2019-03-01 01:09:50 -08001102 cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001103 entropy->pub.start_pass = start_pass_phuff;
1104
1105 /* Mark tables unallocated */
1106 for (i = 0; i < NUM_HUFF_TBLS; i++) {
1107 entropy->derived_tbls[i] = NULL;
1108 entropy->count_ptrs[i] = NULL;
1109 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001110 entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001111}
1112
1113#endif /* C_PROGRESSIVE_SUPPORTED */