hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jcphuff.c |
| 3 | * |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 1995-1997, Thomas G. Lane. |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 6 | * libjpeg-turbo Modifications: |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 7 | * Copyright (C) 2011, 2015, 2018, 2021, D. R. Commander. |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 8 | * Copyright (C) 2016, 2018, Matthieu Darbois. |
Jonathan Wright | 518d815 | 2021-01-12 11:33:28 +0000 | [diff] [blame] | 9 | * Copyright (C) 2020, Arm Limited. |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 10 | * For conditions of distribution and use, see the accompanying README.ijg |
| 11 | * file. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 12 | * |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 23 | #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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 38 | |
| 39 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 40 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 41 | /* |
| 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 Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 47 | * This feature is enabled by default only on Arm processors, because some x86 |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 48 | * 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 Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 50 | * have a fast implementation of it. When building for Armv6, you can |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 51 | * explicitly disable the use of clz/bsr by adding -mthumb to the compiler |
| 52 | * flags (this defines __thumb__). |
| 53 | */ |
| 54 | |
Peter Kasting | 2eb7e20 | 2021-07-09 07:13:34 -0700 | [diff] [blame] | 55 | /* NOTE: Both GCC and Clang define __GNUC__ */ |
| 56 | #if (defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))) || \ |
| 57 | defined(_M_ARM) || defined(_M_ARM64) |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 58 | #if !defined(__thumb__) || defined(__thumb2__) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 59 | #define USE_CLZ_INTRINSIC |
| 60 | #endif |
| 61 | #endif |
| 62 | |
| 63 | #ifdef USE_CLZ_INTRINSIC |
Jonathan Wright | 518d815 | 2021-01-12 11:33:28 +0000 | [diff] [blame] | 64 | #if defined(_MSC_VER) && !defined(__clang__) |
| 65 | #define JPEG_NBITS_NONZERO(x) (32 - _CountLeadingZeros(x)) |
| 66 | #else |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 67 | #define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x)) |
Jonathan Wright | 518d815 | 2021-01-12 11:33:28 +0000 | [diff] [blame] | 68 | #endif |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 69 | #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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 77 | /* Expanded entropy encoder object for progressive Huffman encoding. */ |
| 78 | |
| 79 | typedef struct { |
| 80 | struct jpeg_entropy_encoder pub; /* public fields */ |
| 81 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 82 | /* 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 91 | /* 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 97 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 102 | |
| 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 107 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 111 | /* packing correction bits tightly would save some space but cost time... */ |
| 112 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 113 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 115 | |
| 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 120 | c_derived_tbl *derived_tbls[NUM_HUFF_TBLS]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 121 | |
| 122 | /* Statistics tables for optimization; again, one set is enough */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 123 | long *count_ptrs[NUM_HUFF_TBLS]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 124 | } phuff_entropy_encoder; |
| 125 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 126 | typedef phuff_entropy_encoder *phuff_entropy_ptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 127 | |
| 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 134 | #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 135 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 136 | /* 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 138 | * which should be safe. |
| 139 | */ |
| 140 | |
| 141 | #ifdef RIGHT_SHIFT_IS_UNSIGNED |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 142 | #define ISHIFT_TEMPS int ishift_temp; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 143 | #define IRIGHT_SHIFT(x, shft) \ |
| 144 | ((ishift_temp = (x)) < 0 ? \ |
| 145 | (ishift_temp >> (shft)) | ((~0) << (16 - (shft))) : \ |
| 146 | (ishift_temp >> (shft))) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 147 | #else |
| 148 | #define ISHIFT_TEMPS |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 149 | #define IRIGHT_SHIFT(x, shft) ((x) >> (shft)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 152 | #define PAD(v, p) ((v + (p) - 1) & (~((p) - 1))) |
| 153 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 154 | /* Forward declarations */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 155 | METHODDEF(boolean) encode_mcu_DC_first(j_compress_ptr cinfo, |
| 156 | JBLOCKROW *MCU_data); |
| 157 | METHODDEF(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); |
| 160 | METHODDEF(boolean) encode_mcu_AC_first(j_compress_ptr cinfo, |
| 161 | JBLOCKROW *MCU_data); |
| 162 | METHODDEF(boolean) encode_mcu_DC_refine(j_compress_ptr cinfo, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 163 | JBLOCKROW *MCU_data); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 164 | METHODDEF(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); |
| 167 | METHODDEF(boolean) encode_mcu_AC_refine(j_compress_ptr cinfo, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 168 | JBLOCKROW *MCU_data); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 169 | METHODDEF(void) finish_pass_phuff(j_compress_ptr cinfo); |
| 170 | METHODDEF(void) finish_pass_gather_phuff(j_compress_ptr cinfo); |
| 171 | |
| 172 | |
| 173 | /* Count bit loop zeroes */ |
| 174 | INLINE |
| 175 | METHODDEF(int) |
| 176 | count_zeroes(size_t *x) |
| 177 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 178 | #if defined(HAVE_BUILTIN_CTZL) |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 179 | int result; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 180 | result = __builtin_ctzl(*x); |
| 181 | *x >>= result; |
| 182 | #elif defined(HAVE_BITSCANFORWARD64) |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 183 | unsigned long result; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 184 | _BitScanForward64(&result, *x); |
| 185 | *x >>= result; |
| 186 | #elif defined(HAVE_BITSCANFORWARD) |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 187 | unsigned long result; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 188 | _BitScanForward(&result, *x); |
| 189 | *x >>= result; |
| 190 | #else |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 191 | int result = 0; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 192 | while ((*x & 1) == 0) { |
| 193 | ++result; |
| 194 | *x >>= 1; |
| 195 | } |
| 196 | #endif |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 197 | return (int)result; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 198 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | /* |
| 202 | * Initialize for a Huffman-compressed scan using progressive JPEG. |
| 203 | */ |
| 204 | |
| 205 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 206 | start_pass_phuff(j_compress_ptr cinfo, boolean gather_statistics) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 207 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 208 | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 209 | boolean is_DC_band; |
| 210 | int ci, tbl; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 211 | jpeg_component_info *compptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 212 | |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 226 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 230 | } 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 235 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 239 | /* AC refinement needs a correction bit buffer */ |
| 240 | if (entropy->bit_buffer == NULL) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 241 | entropy->bit_buffer = (char *) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 242 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 243 | MAX_CORR_BITS * sizeof(char)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 244 | } |
| 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 260 | if (cinfo->Ah != 0) /* DC refinement needs no table */ |
| 261 | continue; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 262 | 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 274 | entropy->count_ptrs[tbl] = (long *) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 275 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 276 | 257 * sizeof(long)); |
| 277 | MEMZERO(entropy->count_ptrs[tbl], 257 * sizeof(long)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 278 | } 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 282 | &entropy->derived_tbls[tbl]); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 283 | } |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 306 | #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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 311 | |
| 312 | |
| 313 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 314 | dump_buffer(phuff_entropy_ptr entropy) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 315 | /* Empty the output buffer; we do not support suspension in this module. */ |
| 316 | { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 317 | struct jpeg_destination_mgr *dest = entropy->cinfo->dest; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 318 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 319 | if (!(*dest->empty_output_buffer) (entropy->cinfo)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 320 | 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 | |
| 335 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 336 | emit_bits(phuff_entropy_ptr entropy, unsigned int code, int size) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 337 | /* Emit some bits, unless we are in gather mode */ |
| 338 | { |
| 339 | /* This routine is heavily used, so it's worth coding tightly. */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 340 | register size_t put_buffer = (size_t)code; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 341 | 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 348 | return; /* do nothing if we're only getting stats */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 349 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 350 | put_buffer &= (((size_t)1) << size) - 1; /* mask off any extra bits in code */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 351 | |
| 352 | put_bits += size; /* new number of bits in buffer */ |
| 353 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 354 | 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 359 | int c = (int)((put_buffer >> 16) & 0xFF); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 360 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 361 | emit_byte(entropy, c); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 362 | if (c == 0xFF) { /* need to stuff a zero byte? */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 363 | 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 | |
| 374 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 375 | flush_bits(phuff_entropy_ptr entropy) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 376 | { |
| 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 | |
| 387 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 388 | emit_symbol(phuff_entropy_ptr entropy, int tbl_no, int symbol) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 389 | { |
| 390 | if (entropy->gather_statistics) |
| 391 | entropy->count_ptrs[tbl_no][symbol]++; |
| 392 | else { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 393 | c_derived_tbl *tbl = entropy->derived_tbls[tbl_no]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 394 | emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /* |
| 400 | * Emit bits from a correction bit buffer. |
| 401 | */ |
| 402 | |
| 403 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 404 | emit_buffered_bits(phuff_entropy_ptr entropy, char *bufstart, |
| 405 | unsigned int nbits) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 406 | { |
| 407 | if (entropy->gather_statistics) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 408 | return; /* no real work */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 409 | |
| 410 | while (nbits > 0) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 411 | emit_bits(entropy, (unsigned int)(*bufstart), 1); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 412 | bufstart++; |
| 413 | nbits--; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | |
| 418 | /* |
| 419 | * Emit any pending EOBRUN symbol. |
| 420 | */ |
| 421 | |
| 422 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 423 | emit_eobrun(phuff_entropy_ptr entropy) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 424 | { |
| 425 | register int temp, nbits; |
| 426 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 427 | if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 428 | temp = entropy->EOBRUN; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 429 | nbits = JPEG_NBITS_NONZERO(temp) - 1; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 430 | /* 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 | |
| 451 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 452 | emit_restart(phuff_entropy_ptr entropy, int restart_num) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 453 | { |
| 454 | int ci; |
| 455 | |
| 456 | emit_eobrun(entropy); |
| 457 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 458 | if (!entropy->gather_statistics) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 459 | 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 | |
| 481 | METHODDEF(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 482 | encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 483 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 484 | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
| 485 | register int temp, temp2, temp3; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 486 | register int nbits; |
| 487 | int blkn, ci; |
| 488 | int Al = cinfo->Al; |
| 489 | JBLOCKROW block; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 490 | jpeg_component_info *compptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 491 | 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 510 | temp2 = IRIGHT_SHIFT((int)((*block)[0]), Al); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 511 | |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 517 | |
| 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 528 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 529 | /* Find the number of bits needed for the magnitude of the coefficient */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 530 | nbits = JPEG_NBITS(temp); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 531 | /* Check for out-of-range coefficient values. |
| 532 | * Since we're encoding a difference, the range limit is twice as much. |
| 533 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 534 | if (nbits > MAX_COEF_BITS + 1) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 535 | ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 536 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 537 | /* Count/emit the Huffman-coded symbol for the number of bits */ |
| 538 | emit_symbol(entropy, compptr->dc_tbl_no, nbits); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 539 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 540 | /* Emit that number of bits of the value, if positive, */ |
| 541 | /* or the complement of its magnitude, if negative. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 542 | if (nbits) /* emit_bits rejects calls with size 0 */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 543 | emit_bits(entropy, (unsigned int)temp2, nbits); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 544 | } |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 564 | * 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 | |
| 592 | METHODDEF(void) |
| 593 | encode_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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 624 | * MCU encoding for AC initial scan (either spectral selection, |
| 625 | * or first pass of successive approximation). |
| 626 | */ |
| 627 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 628 | #define ENCODE_COEFS_AC_FIRST(label) { \ |
| 629 | while (zerobits) { \ |
| 630 | r = count_zeroes(&zerobits); \ |
| 631 | cvalue += r; \ |
| 632 | label \ |
| 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 660 | METHODDEF(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 661 | encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 662 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 663 | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 664 | register int temp, temp2; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 665 | register int nbits, r; |
| 666 | int Sl = cinfo->Se - cinfo->Ss + 1; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 667 | int Al = cinfo->Al; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 668 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 673 | |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 682 | #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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 705 | |
| 706 | /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 707 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 708 | ENCODE_COEFS_AC_FIRST((void)0;); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 709 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 710 | #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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 720 | ENCODE_COEFS_AC_FIRST(first_iter_ac_first:); |
| 721 | #endif |
| 722 | |
| 723 | if (cvalue < (values + Sl)) { /* If there are trailing zeroes, */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 724 | entropy->EOBRUN++; /* count an EOB */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 725 | if (entropy->EOBRUN == 0x7FFF) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 726 | emit_eobrun(entropy); /* force it out to avoid overflow */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 727 | } |
| 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 | |
| 752 | METHODDEF(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 753 | encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 754 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 755 | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 756 | 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 775 | emit_bits(entropy, (unsigned int)(temp >> Al), 1); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 776 | } |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 796 | * 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 | |
| 823 | METHODDEF(int) |
| 824 | encode_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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 866 | * MCU encoding for AC successive approximation refinement scan. |
| 867 | */ |
| 868 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 869 | #define ENCODE_COEFS_AC_REFINE(label) { \ |
| 870 | while (zerobits) { \ |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 871 | idx = count_zeroes(&zerobits); \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 872 | r += idx; \ |
| 873 | cabsvalue += idx; \ |
| 874 | signbits >>= idx; \ |
| 875 | label \ |
| 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 924 | METHODDEF(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 925 | encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 926 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 927 | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 928 | register int temp, r, idx; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 929 | char *BR_buffer; |
| 930 | unsigned int BR; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 931 | int Sl = cinfo->Se - cinfo->Ss + 1; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 932 | int Al = cinfo->Al; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 933 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 938 | |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 947 | #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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 953 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 954 | /* 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 958 | |
| 959 | /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 960 | |
| 961 | r = 0; /* r = run length of zeros */ |
| 962 | BR = 0; /* BR = count of buffered bits added now */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 963 | BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ |
| 964 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 965 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 972 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 973 | #if SIZEOF_SIZE_T == 4 |
| 974 | zerobits = bits[1]; |
| 975 | signbits = bits[3]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 976 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 977 | if (zerobits) { |
| 978 | int diff = ((absvalues + DCTSIZE2 / 2) - cabsvalue); |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 979 | idx = count_zeroes(&zerobits); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 980 | signbits >>= idx; |
| 981 | idx += diff; |
| 982 | r += idx; |
| 983 | cabsvalue += idx; |
| 984 | goto first_iter_ac_refine; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 987 | ENCODE_COEFS_AC_REFINE(first_iter_ac_refine:); |
| 988 | #endif |
| 989 | |
| 990 | r |= (int)((absvalues + Sl) - cabsvalue); |
| 991 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 992 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 995 | /* 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 999 | if (entropy->EOBRUN == 0x7FFF || |
| 1000 | entropy->BE > (MAX_CORR_BITS - DCTSIZE2 + 1)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1001 | 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 | |
| 1025 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1026 | finish_pass_phuff(j_compress_ptr cinfo) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1027 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1028 | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1029 | |
| 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 | |
| 1046 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1047 | finish_pass_gather_phuff(j_compress_ptr cinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1048 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1049 | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1050 | boolean is_DC_band; |
| 1051 | int ci, tbl; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1052 | jpeg_component_info *compptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1053 | 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1064 | MEMZERO(did, sizeof(did)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1065 | |
| 1066 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 1067 | compptr = cinfo->cur_comp_info[ci]; |
| 1068 | if (is_DC_band) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1069 | if (cinfo->Ah != 0) /* DC refinement needs no table */ |
| 1070 | continue; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1071 | tbl = compptr->dc_tbl_no; |
| 1072 | } else { |
| 1073 | tbl = compptr->ac_tbl_no; |
| 1074 | } |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1075 | if (!did[tbl]) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1076 | if (is_DC_band) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1077 | htblptr = &cinfo->dc_huff_tbl_ptrs[tbl]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1078 | else |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1079 | htblptr = &cinfo->ac_huff_tbl_ptrs[tbl]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1080 | if (*htblptr == NULL) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1081 | *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1082 | 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 | |
| 1093 | GLOBAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1094 | jinit_phuff_encoder(j_compress_ptr cinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1095 | { |
| 1096 | phuff_entropy_ptr entropy; |
| 1097 | int i; |
| 1098 | |
| 1099 | entropy = (phuff_entropy_ptr) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1100 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1101 | sizeof(phuff_entropy_encoder)); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1102 | cinfo->entropy = (struct jpeg_entropy_encoder *)entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1103 | 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1110 | entropy->bit_buffer = NULL; /* needed only in AC refinement scan */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | #endif /* C_PROGRESSIVE_SUPPORTED */ |