hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jchuff.c |
| 3 | * |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [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) 1991-1997, Thomas G. Lane. |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 6 | * libjpeg-turbo Modifications: |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 7 | * Copyright (C) 2009-2011, 2014-2016, 2018-2021, D. R. Commander. |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 8 | * Copyright (C) 2015, Matthieu Darbois. |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 9 | * Copyright (C) 2018, Matthias Räncker. |
| 10 | * Copyright (C) 2020, Arm Limited. |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 11 | * For conditions of distribution and use, see the accompanying README.ijg |
| 12 | * file. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 13 | * |
| 14 | * This file contains Huffman entropy encoding routines. |
| 15 | * |
| 16 | * Much of the complexity here has to do with supporting output suspension. |
| 17 | * If the data destination module demands suspension, we want to be able to |
| 18 | * back up to the start of the current MCU. To do this, we copy state |
| 19 | * variables into local working storage, and update them back to the |
| 20 | * permanent JPEG objects only upon successful completion of an MCU. |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 21 | * |
| 22 | * NOTE: All referenced figures are from |
| 23 | * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 26 | #define JPEG_INTERNALS |
| 27 | #include "jinclude.h" |
| 28 | #include "jpeglib.h" |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 29 | #include "jsimd.h" |
| 30 | #include "jconfigint.h" |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 31 | #include <limits.h> |
| 32 | |
noel@chromium.org | 841fff8 | 2014-05-23 23:38:59 +0000 | [diff] [blame] | 33 | /* |
| 34 | * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be |
| 35 | * used for bit counting rather than the lookup table. This will reduce the |
| 36 | * memory footprint by 64k, which is important for some mobile applications |
| 37 | * that create many isolated instances of libjpeg-turbo (web browsers, for |
| 38 | * instance.) This may improve performance on some mobile platforms as well. |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 39 | * This feature is enabled by default only on Arm processors, because some x86 |
noel@chromium.org | 841fff8 | 2014-05-23 23:38:59 +0000 | [diff] [blame] | 40 | * chips have a slow implementation of bsr, and the use of clz/bsr cannot be |
| 41 | * 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] | 42 | * have a fast implementation of it. When building for Armv6, you can |
noel@chromium.org | 841fff8 | 2014-05-23 23:38:59 +0000 | [diff] [blame] | 43 | * explicitly disable the use of clz/bsr by adding -mthumb to the compiler |
| 44 | * flags (this defines __thumb__). |
| 45 | */ |
| 46 | |
Peter Kasting | 2eb7e20 | 2021-07-09 07:13:34 -0700 | [diff] [blame] | 47 | /* NOTE: Both GCC and Clang define __GNUC__ */ |
| 48 | #if (defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))) || \ |
| 49 | defined(_M_ARM) || defined(_M_ARM64) |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 50 | #if !defined(__thumb__) || defined(__thumb2__) |
noel@chromium.org | 841fff8 | 2014-05-23 23:38:59 +0000 | [diff] [blame] | 51 | #define USE_CLZ_INTRINSIC |
| 52 | #endif |
| 53 | #endif |
| 54 | |
| 55 | #ifdef USE_CLZ_INTRINSIC |
Jonathan Wright | 518d815 | 2021-01-12 11:33:28 +0000 | [diff] [blame] | 56 | #if defined(_MSC_VER) && !defined(__clang__) |
| 57 | #define JPEG_NBITS_NONZERO(x) (32 - _CountLeadingZeros(x)) |
| 58 | #else |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 59 | #define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x)) |
Jonathan Wright | 518d815 | 2021-01-12 11:33:28 +0000 | [diff] [blame] | 60 | #endif |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 61 | #define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0) |
noel@chromium.org | 841fff8 | 2014-05-23 23:38:59 +0000 | [diff] [blame] | 62 | #else |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 63 | #include "jpeg_nbits_table.h" |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 64 | #define JPEG_NBITS(x) (jpeg_nbits_table[x]) |
| 65 | #define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 68 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 69 | /* Expanded entropy encoder object for Huffman encoding. |
| 70 | * |
| 71 | * The savable_state subrecord contains fields that change within an MCU, |
| 72 | * but must not be updated permanently until we complete the MCU. |
| 73 | */ |
| 74 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 75 | #if defined(__x86_64__) && defined(__ILP32__) |
| 76 | typedef unsigned long long bit_buf_type; |
| 77 | #else |
| 78 | typedef size_t bit_buf_type; |
| 79 | #endif |
| 80 | |
| 81 | /* NOTE: The more optimal Huffman encoding algorithm is only used by the |
| 82 | * intrinsics implementation of the Arm Neon SIMD extensions, which is why we |
| 83 | * retain the old Huffman encoder behavior when using the GAS implementation. |
| 84 | */ |
| 85 | #if defined(WITH_SIMD) && !(defined(__arm__) || defined(__aarch64__) || \ |
| 86 | defined(_M_ARM) || defined(_M_ARM64)) |
| 87 | typedef unsigned long long simd_bit_buf_type; |
| 88 | #else |
| 89 | typedef bit_buf_type simd_bit_buf_type; |
| 90 | #endif |
| 91 | |
| 92 | #if (defined(SIZEOF_SIZE_T) && SIZEOF_SIZE_T == 8) || defined(_WIN64) || \ |
| 93 | (defined(__x86_64__) && defined(__ILP32__)) |
| 94 | #define BIT_BUF_SIZE 64 |
| 95 | #elif (defined(SIZEOF_SIZE_T) && SIZEOF_SIZE_T == 4) || defined(_WIN32) |
| 96 | #define BIT_BUF_SIZE 32 |
| 97 | #else |
| 98 | #error Cannot determine word size |
| 99 | #endif |
| 100 | #define SIMD_BIT_BUF_SIZE (sizeof(simd_bit_buf_type) * 8) |
| 101 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 102 | typedef struct { |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 103 | union { |
| 104 | bit_buf_type c; |
| 105 | simd_bit_buf_type simd; |
| 106 | } put_buffer; /* current bit accumulation buffer */ |
| 107 | int free_bits; /* # of bits available in it */ |
| 108 | /* (Neon GAS: # of bits now in it) */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 109 | int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 110 | } savable_state; |
| 111 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 112 | typedef struct { |
| 113 | struct jpeg_entropy_encoder pub; /* public fields */ |
| 114 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 115 | savable_state saved; /* Bit buffer & DC state at start of MCU */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 116 | |
| 117 | /* These fields are NOT loaded into local working state. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 118 | unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
| 119 | int next_restart_num; /* next restart number to write (0-7) */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 120 | |
| 121 | /* Pointers to derived tables (these workspaces have image lifespan) */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 122 | c_derived_tbl *dc_derived_tbls[NUM_HUFF_TBLS]; |
| 123 | c_derived_tbl *ac_derived_tbls[NUM_HUFF_TBLS]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 124 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 125 | #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */ |
| 126 | long *dc_count_ptrs[NUM_HUFF_TBLS]; |
| 127 | long *ac_count_ptrs[NUM_HUFF_TBLS]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 128 | #endif |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 129 | |
| 130 | int simd; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 131 | } huff_entropy_encoder; |
| 132 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 133 | typedef huff_entropy_encoder *huff_entropy_ptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 134 | |
| 135 | /* Working state while writing an MCU. |
| 136 | * This struct contains all the fields that are needed by subroutines. |
| 137 | */ |
| 138 | |
| 139 | typedef struct { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 140 | JOCTET *next_output_byte; /* => next byte to write in buffer */ |
| 141 | size_t free_in_buffer; /* # of byte spaces remaining in buffer */ |
| 142 | savable_state cur; /* Current bit buffer & DC state */ |
| 143 | j_compress_ptr cinfo; /* dump_buffer needs access to this */ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 144 | int simd; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 145 | } working_state; |
| 146 | |
| 147 | |
| 148 | /* Forward declarations */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 149 | METHODDEF(boolean) encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data); |
| 150 | METHODDEF(void) finish_pass_huff(j_compress_ptr cinfo); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 151 | #ifdef ENTROPY_OPT_SUPPORTED |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 152 | METHODDEF(boolean) encode_mcu_gather(j_compress_ptr cinfo, |
| 153 | JBLOCKROW *MCU_data); |
| 154 | METHODDEF(void) finish_pass_gather(j_compress_ptr cinfo); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 155 | #endif |
| 156 | |
| 157 | |
| 158 | /* |
| 159 | * Initialize for a Huffman-compressed scan. |
| 160 | * If gather_statistics is TRUE, we do not output anything during the scan, |
| 161 | * just count the Huffman symbols used and generate Huffman code tables. |
| 162 | */ |
| 163 | |
| 164 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 165 | start_pass_huff(j_compress_ptr cinfo, boolean gather_statistics) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 166 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 167 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 168 | int ci, dctbl, actbl; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 169 | jpeg_component_info *compptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 170 | |
| 171 | if (gather_statistics) { |
| 172 | #ifdef ENTROPY_OPT_SUPPORTED |
| 173 | entropy->pub.encode_mcu = encode_mcu_gather; |
| 174 | entropy->pub.finish_pass = finish_pass_gather; |
| 175 | #else |
| 176 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 177 | #endif |
| 178 | } else { |
| 179 | entropy->pub.encode_mcu = encode_mcu_huff; |
| 180 | entropy->pub.finish_pass = finish_pass_huff; |
| 181 | } |
| 182 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 183 | entropy->simd = jsimd_can_huff_encode_one_block(); |
| 184 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 185 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 186 | compptr = cinfo->cur_comp_info[ci]; |
| 187 | dctbl = compptr->dc_tbl_no; |
| 188 | actbl = compptr->ac_tbl_no; |
| 189 | if (gather_statistics) { |
| 190 | #ifdef ENTROPY_OPT_SUPPORTED |
| 191 | /* Check for invalid table indexes */ |
| 192 | /* (make_c_derived_tbl does this in the other path) */ |
| 193 | if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 194 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 195 | if (actbl < 0 || actbl >= NUM_HUFF_TBLS) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 196 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 197 | /* Allocate and zero the statistics tables */ |
| 198 | /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ |
| 199 | if (entropy->dc_count_ptrs[dctbl] == NULL) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 200 | entropy->dc_count_ptrs[dctbl] = (long *) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 201 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 202 | 257 * sizeof(long)); |
| 203 | MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * sizeof(long)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 204 | if (entropy->ac_count_ptrs[actbl] == NULL) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 205 | entropy->ac_count_ptrs[actbl] = (long *) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 206 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 207 | 257 * sizeof(long)); |
| 208 | MEMZERO(entropy->ac_count_ptrs[actbl], 257 * sizeof(long)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 209 | #endif |
| 210 | } else { |
| 211 | /* Compute derived values for Huffman tables */ |
| 212 | /* We may do this more than once for a table, but it's not expensive */ |
| 213 | jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl, |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 214 | &entropy->dc_derived_tbls[dctbl]); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 215 | jpeg_make_c_derived_tbl(cinfo, FALSE, actbl, |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 216 | &entropy->ac_derived_tbls[actbl]); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 217 | } |
| 218 | /* Initialize DC predictions to 0 */ |
| 219 | entropy->saved.last_dc_val[ci] = 0; |
| 220 | } |
| 221 | |
| 222 | /* Initialize bit buffer to empty */ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 223 | if (entropy->simd) { |
| 224 | entropy->saved.put_buffer.simd = 0; |
| 225 | #if defined(__aarch64__) && !defined(NEON_INTRINSICS) |
| 226 | entropy->saved.free_bits = 0; |
| 227 | #else |
| 228 | entropy->saved.free_bits = SIMD_BIT_BUF_SIZE; |
| 229 | #endif |
| 230 | } else { |
| 231 | entropy->saved.put_buffer.c = 0; |
| 232 | entropy->saved.free_bits = BIT_BUF_SIZE; |
| 233 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 234 | |
| 235 | /* Initialize restart stuff */ |
| 236 | entropy->restarts_to_go = cinfo->restart_interval; |
| 237 | entropy->next_restart_num = 0; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /* |
| 242 | * Compute the derived values for a Huffman table. |
| 243 | * This routine also performs some validation checks on the table. |
| 244 | * |
| 245 | * Note this is also used by jcphuff.c. |
| 246 | */ |
| 247 | |
| 248 | GLOBAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 249 | jpeg_make_c_derived_tbl(j_compress_ptr cinfo, boolean isDC, int tblno, |
| 250 | c_derived_tbl **pdtbl) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 251 | { |
| 252 | JHUFF_TBL *htbl; |
| 253 | c_derived_tbl *dtbl; |
| 254 | int p, i, l, lastp, si, maxsymbol; |
| 255 | char huffsize[257]; |
| 256 | unsigned int huffcode[257]; |
| 257 | unsigned int code; |
| 258 | |
| 259 | /* Note that huffsize[] and huffcode[] are filled in code-length order, |
| 260 | * paralleling the order of the symbols themselves in htbl->huffval[]. |
| 261 | */ |
| 262 | |
| 263 | /* Find the input Huffman table */ |
| 264 | if (tblno < 0 || tblno >= NUM_HUFF_TBLS) |
| 265 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); |
| 266 | htbl = |
| 267 | isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; |
| 268 | if (htbl == NULL) |
| 269 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); |
| 270 | |
| 271 | /* Allocate a workspace if we haven't already done so. */ |
| 272 | if (*pdtbl == NULL) |
| 273 | *pdtbl = (c_derived_tbl *) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 274 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 275 | sizeof(c_derived_tbl)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 276 | dtbl = *pdtbl; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 277 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 278 | /* Figure C.1: make table of Huffman code length for each symbol */ |
| 279 | |
| 280 | p = 0; |
| 281 | for (l = 1; l <= 16; l++) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 282 | i = (int)htbl->bits[l]; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 283 | if (i < 0 || p + i > 256) /* protect against table overrun */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 284 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
| 285 | while (i--) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 286 | huffsize[p++] = (char)l; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 287 | } |
| 288 | huffsize[p] = 0; |
| 289 | lastp = p; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 290 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 291 | /* Figure C.2: generate the codes themselves */ |
| 292 | /* We also validate that the counts represent a legal Huffman code tree. */ |
| 293 | |
| 294 | code = 0; |
| 295 | si = huffsize[0]; |
| 296 | p = 0; |
| 297 | while (huffsize[p]) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 298 | while (((int)huffsize[p]) == si) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 299 | huffcode[p++] = code; |
| 300 | code++; |
| 301 | } |
| 302 | /* code is now 1 more than the last code used for codelength si; but |
| 303 | * it must still fit in si bits, since no code is allowed to be all ones. |
| 304 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 305 | if (((JLONG)code) >= (((JLONG)1) << si)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 306 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
| 307 | code <<= 1; |
| 308 | si++; |
| 309 | } |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 310 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 311 | /* Figure C.3: generate encoding tables */ |
| 312 | /* These are code and size indexed by symbol value */ |
| 313 | |
| 314 | /* Set all codeless symbols to have code length 0; |
| 315 | * this lets us detect duplicate VAL entries here, and later |
| 316 | * allows emit_bits to detect any attempt to emit such symbols. |
| 317 | */ |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 318 | MEMZERO(dtbl->ehufco, sizeof(dtbl->ehufco)); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 319 | MEMZERO(dtbl->ehufsi, sizeof(dtbl->ehufsi)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 320 | |
| 321 | /* This is also a convenient place to check for out-of-range |
| 322 | * and duplicated VAL entries. We allow 0..255 for AC symbols |
| 323 | * but only 0..15 for DC. (We could constrain them further |
| 324 | * based on data depth and mode, but this seems enough.) |
| 325 | */ |
| 326 | maxsymbol = isDC ? 15 : 255; |
| 327 | |
| 328 | for (p = 0; p < lastp; p++) { |
| 329 | i = htbl->huffval[p]; |
| 330 | if (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) |
| 331 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
| 332 | dtbl->ehufco[i] = huffcode[p]; |
| 333 | dtbl->ehufsi[i] = huffsize[p]; |
| 334 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
| 338 | /* Outputting bytes to the file */ |
| 339 | |
| 340 | /* Emit a byte, taking 'action' if must suspend. */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 341 | #define emit_byte(state, val, action) { \ |
| 342 | *(state)->next_output_byte++ = (JOCTET)(val); \ |
| 343 | if (--(state)->free_in_buffer == 0) \ |
| 344 | if (!dump_buffer(state)) \ |
| 345 | { action; } \ |
| 346 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 347 | |
| 348 | |
| 349 | LOCAL(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 350 | dump_buffer(working_state *state) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 351 | /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */ |
| 352 | { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 353 | struct jpeg_destination_mgr *dest = state->cinfo->dest; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 354 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 355 | if (!(*dest->empty_output_buffer) (state->cinfo)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 356 | return FALSE; |
| 357 | /* After a successful buffer dump, must reset buffer pointers */ |
| 358 | state->next_output_byte = dest->next_output_byte; |
| 359 | state->free_in_buffer = dest->free_in_buffer; |
| 360 | return TRUE; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | /* Outputting bits to the file */ |
| 365 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 366 | /* Output byte b and, speculatively, an additional 0 byte. 0xFF must be |
| 367 | * encoded as 0xFF 0x00, so the output buffer pointer is advanced by 2 if the |
| 368 | * byte is 0xFF. Otherwise, the output buffer pointer is advanced by 1, and |
| 369 | * the speculative 0 byte will be overwritten by the next byte. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 370 | */ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 371 | #define EMIT_BYTE(b) { \ |
| 372 | buffer[0] = (JOCTET)(b); \ |
| 373 | buffer[1] = 0; \ |
| 374 | buffer -= -2 + ((JOCTET)(b) < 0xFF); \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 375 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 376 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 377 | /* Output the entire bit buffer. If there are no 0xFF bytes in it, then write |
| 378 | * directly to the output buffer. Otherwise, use the EMIT_BYTE() macro to |
| 379 | * encode 0xFF as 0xFF 0x00. |
| 380 | */ |
| 381 | #if BIT_BUF_SIZE == 64 |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 382 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 383 | #define FLUSH() { \ |
| 384 | if (put_buffer & 0x8080808080808080 & ~(put_buffer + 0x0101010101010101)) { \ |
| 385 | EMIT_BYTE(put_buffer >> 56) \ |
| 386 | EMIT_BYTE(put_buffer >> 48) \ |
| 387 | EMIT_BYTE(put_buffer >> 40) \ |
| 388 | EMIT_BYTE(put_buffer >> 32) \ |
| 389 | EMIT_BYTE(put_buffer >> 24) \ |
| 390 | EMIT_BYTE(put_buffer >> 16) \ |
| 391 | EMIT_BYTE(put_buffer >> 8) \ |
| 392 | EMIT_BYTE(put_buffer ) \ |
| 393 | } else { \ |
| 394 | buffer[0] = (JOCTET)(put_buffer >> 56); \ |
| 395 | buffer[1] = (JOCTET)(put_buffer >> 48); \ |
| 396 | buffer[2] = (JOCTET)(put_buffer >> 40); \ |
| 397 | buffer[3] = (JOCTET)(put_buffer >> 32); \ |
| 398 | buffer[4] = (JOCTET)(put_buffer >> 24); \ |
| 399 | buffer[5] = (JOCTET)(put_buffer >> 16); \ |
| 400 | buffer[6] = (JOCTET)(put_buffer >> 8); \ |
| 401 | buffer[7] = (JOCTET)(put_buffer); \ |
| 402 | buffer += 8; \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 403 | } \ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 404 | } |
| 405 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 406 | #else |
| 407 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 408 | #define FLUSH() { \ |
| 409 | if (put_buffer & 0x80808080 & ~(put_buffer + 0x01010101)) { \ |
| 410 | EMIT_BYTE(put_buffer >> 24) \ |
| 411 | EMIT_BYTE(put_buffer >> 16) \ |
| 412 | EMIT_BYTE(put_buffer >> 8) \ |
| 413 | EMIT_BYTE(put_buffer ) \ |
| 414 | } else { \ |
| 415 | buffer[0] = (JOCTET)(put_buffer >> 24); \ |
| 416 | buffer[1] = (JOCTET)(put_buffer >> 16); \ |
| 417 | buffer[2] = (JOCTET)(put_buffer >> 8); \ |
| 418 | buffer[3] = (JOCTET)(put_buffer); \ |
| 419 | buffer += 4; \ |
| 420 | } \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 421 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 422 | |
| 423 | #endif |
| 424 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 425 | /* Fill the bit buffer to capacity with the leading bits from code, then output |
| 426 | * the bit buffer and put the remaining bits from code into the bit buffer. |
| 427 | */ |
| 428 | #define PUT_AND_FLUSH(code, size) { \ |
| 429 | put_buffer = (put_buffer << (size + free_bits)) | (code >> -free_bits); \ |
| 430 | FLUSH() \ |
| 431 | free_bits += BIT_BUF_SIZE; \ |
| 432 | put_buffer = code; \ |
| 433 | } |
| 434 | |
| 435 | /* Insert code into the bit buffer and output the bit buffer if needed. |
| 436 | * NOTE: We can't flush with free_bits == 0, since the left shift in |
| 437 | * PUT_AND_FLUSH() would have undefined behavior. |
| 438 | */ |
| 439 | #define PUT_BITS(code, size) { \ |
| 440 | free_bits -= size; \ |
| 441 | if (free_bits < 0) \ |
| 442 | PUT_AND_FLUSH(code, size) \ |
| 443 | else \ |
| 444 | put_buffer = (put_buffer << size) | code; \ |
| 445 | } |
| 446 | |
| 447 | #define PUT_CODE(code, size) { \ |
| 448 | temp &= (((JLONG)1) << nbits) - 1; \ |
| 449 | temp |= code << nbits; \ |
| 450 | nbits += size; \ |
| 451 | PUT_BITS(temp, nbits) \ |
| 452 | } |
| 453 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 454 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 455 | /* Although it is exceedingly rare, it is possible for a Huffman-encoded |
| 456 | * coefficient block to be larger than the 128-byte unencoded block. For each |
| 457 | * of the 64 coefficients, PUT_BITS is invoked twice, and each invocation can |
| 458 | * theoretically store 16 bits (for a maximum of 2048 bits or 256 bytes per |
| 459 | * encoded block.) If, for instance, one artificially sets the AC |
| 460 | * coefficients to alternating values of 32767 and -32768 (using the JPEG |
| 461 | * scanning order-- 1, 8, 16, etc.), then this will produce an encoded block |
| 462 | * larger than 200 bytes. |
| 463 | */ |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 464 | #define BUFSIZE (DCTSIZE2 * 8) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 465 | |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 466 | #define LOAD_BUFFER() { \ |
| 467 | if (state->free_in_buffer < BUFSIZE) { \ |
| 468 | localbuf = 1; \ |
| 469 | buffer = _buffer; \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 470 | } else \ |
| 471 | buffer = state->next_output_byte; \ |
| 472 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 473 | |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 474 | #define STORE_BUFFER() { \ |
| 475 | if (localbuf) { \ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 476 | size_t bytes, bytestocopy; \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 477 | bytes = buffer - _buffer; \ |
| 478 | buffer = _buffer; \ |
| 479 | while (bytes > 0) { \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 480 | bytestocopy = MIN(bytes, state->free_in_buffer); \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 481 | MEMCOPY(state->next_output_byte, buffer, bytestocopy); \ |
| 482 | state->next_output_byte += bytestocopy; \ |
| 483 | buffer += bytestocopy; \ |
| 484 | state->free_in_buffer -= bytestocopy; \ |
| 485 | if (state->free_in_buffer == 0) \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 486 | if (!dump_buffer(state)) return FALSE; \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 487 | bytes -= bytestocopy; \ |
| 488 | } \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 489 | } else { \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 490 | state->free_in_buffer -= (buffer - state->next_output_byte); \ |
| 491 | state->next_output_byte = buffer; \ |
| 492 | } \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 493 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 494 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 495 | |
| 496 | LOCAL(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 497 | flush_bits(working_state *state) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 498 | { |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 499 | JOCTET _buffer[BUFSIZE], *buffer, temp; |
| 500 | simd_bit_buf_type put_buffer; int put_bits; |
| 501 | int localbuf = 0; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 502 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 503 | if (state->simd) { |
| 504 | #if defined(__aarch64__) && !defined(NEON_INTRINSICS) |
| 505 | put_bits = state->cur.free_bits; |
| 506 | #else |
| 507 | put_bits = SIMD_BIT_BUF_SIZE - state->cur.free_bits; |
| 508 | #endif |
| 509 | put_buffer = state->cur.put_buffer.simd; |
| 510 | } else { |
| 511 | put_bits = BIT_BUF_SIZE - state->cur.free_bits; |
| 512 | put_buffer = state->cur.put_buffer.c; |
| 513 | } |
| 514 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 515 | LOAD_BUFFER() |
| 516 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 517 | while (put_bits >= 8) { |
| 518 | put_bits -= 8; |
| 519 | temp = (JOCTET)(put_buffer >> put_bits); |
| 520 | EMIT_BYTE(temp) |
| 521 | } |
| 522 | if (put_bits) { |
| 523 | /* fill partial byte with ones */ |
| 524 | temp = (JOCTET)((put_buffer << (8 - put_bits)) | (0xFF >> put_bits)); |
| 525 | EMIT_BYTE(temp) |
| 526 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 527 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 528 | if (state->simd) { /* and reset bit buffer to empty */ |
| 529 | state->cur.put_buffer.simd = 0; |
| 530 | #if defined(__aarch64__) && !defined(NEON_INTRINSICS) |
| 531 | state->cur.free_bits = 0; |
| 532 | #else |
| 533 | state->cur.free_bits = SIMD_BIT_BUF_SIZE; |
| 534 | #endif |
| 535 | } else { |
| 536 | state->cur.put_buffer.c = 0; |
| 537 | state->cur.free_bits = BIT_BUF_SIZE; |
| 538 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 539 | STORE_BUFFER() |
| 540 | |
| 541 | return TRUE; |
| 542 | } |
| 543 | |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 544 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 545 | /* Encode a single block's worth of coefficients */ |
| 546 | |
| 547 | LOCAL(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 548 | encode_one_block_simd(working_state *state, JCOEFPTR block, int last_dc_val, |
| 549 | c_derived_tbl *dctbl, c_derived_tbl *actbl) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 550 | { |
| 551 | JOCTET _buffer[BUFSIZE], *buffer; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 552 | int localbuf = 0; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 553 | |
| 554 | LOAD_BUFFER() |
| 555 | |
| 556 | buffer = jsimd_huff_encode_one_block(state, buffer, block, last_dc_val, |
| 557 | dctbl, actbl); |
| 558 | |
| 559 | STORE_BUFFER() |
| 560 | |
| 561 | return TRUE; |
| 562 | } |
| 563 | |
| 564 | LOCAL(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 565 | encode_one_block(working_state *state, JCOEFPTR block, int last_dc_val, |
| 566 | c_derived_tbl *dctbl, c_derived_tbl *actbl) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 567 | { |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 568 | int temp, nbits, free_bits; |
| 569 | bit_buf_type put_buffer; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 570 | JOCTET _buffer[BUFSIZE], *buffer; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 571 | int localbuf = 0; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 572 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 573 | free_bits = state->cur.free_bits; |
| 574 | put_buffer = state->cur.put_buffer.c; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 575 | LOAD_BUFFER() |
| 576 | |
| 577 | /* Encode the DC coefficient difference per section F.1.2.1 */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 578 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 579 | temp = block[0] - last_dc_val; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 580 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 581 | /* This is a well-known technique for obtaining the absolute value without a |
| 582 | * branch. It is derived from an assembly language technique presented in |
| 583 | * "How to Optimize for the Pentium Processors", Copyright (c) 1996, 1997 by |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 584 | * Agner Fog. This code assumes we are on a two's complement machine. |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 585 | */ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 586 | nbits = temp >> (CHAR_BIT * sizeof(int) - 1); |
| 587 | temp += nbits; |
| 588 | nbits ^= temp; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 589 | |
| 590 | /* Find the number of bits needed for the magnitude of the coefficient */ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 591 | nbits = JPEG_NBITS(nbits); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 592 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 593 | /* Emit the Huffman-coded symbol for the number of bits. |
| 594 | * Emit that number of bits of the value, if positive, |
| 595 | * or the complement of its magnitude, if negative. |
| 596 | */ |
| 597 | PUT_CODE(dctbl->ehufco[nbits], dctbl->ehufsi[nbits]) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 598 | |
| 599 | /* Encode the AC coefficients per section F.1.2.2 */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 600 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 601 | { |
| 602 | int r = 0; /* r = run length of zeros */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 603 | |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 604 | /* Manually unroll the k loop to eliminate the counter variable. This |
| 605 | * improves performance greatly on systems with a limited number of |
| 606 | * registers (such as x86.) |
| 607 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 608 | #define kloop(jpeg_natural_order_of_k) { \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 609 | if ((temp = block[jpeg_natural_order_of_k]) == 0) { \ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 610 | r += 16; \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 611 | } else { \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 612 | /* Branch-less absolute value, bitwise complement, etc., same as above */ \ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 613 | nbits = temp >> (CHAR_BIT * sizeof(int) - 1); \ |
| 614 | temp += nbits; \ |
| 615 | nbits ^= temp; \ |
| 616 | nbits = JPEG_NBITS_NONZERO(nbits); \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 617 | /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 618 | while (r >= 16 * 16) { \ |
| 619 | r -= 16 * 16; \ |
| 620 | PUT_BITS(actbl->ehufco[0xf0], actbl->ehufsi[0xf0]) \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 621 | } \ |
| 622 | /* Emit Huffman symbol for run length / number of bits */ \ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 623 | r += nbits; \ |
| 624 | PUT_CODE(actbl->ehufco[r], actbl->ehufsi[r]) \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 625 | r = 0; \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 626 | } \ |
| 627 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 628 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 629 | /* One iteration for each value in jpeg_natural_order[] */ |
| 630 | kloop(1); kloop(8); kloop(16); kloop(9); kloop(2); kloop(3); |
| 631 | kloop(10); kloop(17); kloop(24); kloop(32); kloop(25); kloop(18); |
| 632 | kloop(11); kloop(4); kloop(5); kloop(12); kloop(19); kloop(26); |
| 633 | kloop(33); kloop(40); kloop(48); kloop(41); kloop(34); kloop(27); |
| 634 | kloop(20); kloop(13); kloop(6); kloop(7); kloop(14); kloop(21); |
| 635 | kloop(28); kloop(35); kloop(42); kloop(49); kloop(56); kloop(57); |
| 636 | kloop(50); kloop(43); kloop(36); kloop(29); kloop(22); kloop(15); |
| 637 | kloop(23); kloop(30); kloop(37); kloop(44); kloop(51); kloop(58); |
| 638 | kloop(59); kloop(52); kloop(45); kloop(38); kloop(31); kloop(39); |
| 639 | kloop(46); kloop(53); kloop(60); kloop(61); kloop(54); kloop(47); |
| 640 | kloop(55); kloop(62); kloop(63); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 641 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 642 | /* If the last coef(s) were zero, emit an end-of-block code */ |
| 643 | if (r > 0) { |
| 644 | PUT_BITS(actbl->ehufco[0], actbl->ehufsi[0]) |
| 645 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 646 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 647 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 648 | state->cur.put_buffer.c = put_buffer; |
| 649 | state->cur.free_bits = free_bits; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 650 | STORE_BUFFER() |
| 651 | |
| 652 | return TRUE; |
| 653 | } |
| 654 | |
| 655 | |
| 656 | /* |
| 657 | * Emit a restart marker & resynchronize predictions. |
| 658 | */ |
| 659 | |
| 660 | LOCAL(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 661 | emit_restart(working_state *state, int restart_num) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 662 | { |
| 663 | int ci; |
| 664 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 665 | if (!flush_bits(state)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 666 | return FALSE; |
| 667 | |
| 668 | emit_byte(state, 0xFF, return FALSE); |
| 669 | emit_byte(state, JPEG_RST0 + restart_num, return FALSE); |
| 670 | |
| 671 | /* Re-initialize DC predictions to 0 */ |
| 672 | for (ci = 0; ci < state->cinfo->comps_in_scan; ci++) |
| 673 | state->cur.last_dc_val[ci] = 0; |
| 674 | |
| 675 | /* The restart counter is not updated until we successfully write the MCU. */ |
| 676 | |
| 677 | return TRUE; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | /* |
| 682 | * Encode and output one MCU's worth of Huffman-compressed coefficients. |
| 683 | */ |
| 684 | |
| 685 | METHODDEF(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 686 | encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 687 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 688 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 689 | working_state state; |
| 690 | int blkn, ci; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 691 | jpeg_component_info *compptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 692 | |
| 693 | /* Load up working state */ |
| 694 | state.next_output_byte = cinfo->dest->next_output_byte; |
| 695 | state.free_in_buffer = cinfo->dest->free_in_buffer; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 696 | state.cur = entropy->saved; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 697 | state.cinfo = cinfo; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 698 | state.simd = entropy->simd; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 699 | |
| 700 | /* Emit restart marker if needed */ |
| 701 | if (cinfo->restart_interval) { |
| 702 | if (entropy->restarts_to_go == 0) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 703 | if (!emit_restart(&state, entropy->next_restart_num)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 704 | return FALSE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /* Encode the MCU data blocks */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 708 | if (entropy->simd) { |
| 709 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 710 | ci = cinfo->MCU_membership[blkn]; |
| 711 | compptr = cinfo->cur_comp_info[ci]; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 712 | if (!encode_one_block_simd(&state, |
| 713 | MCU_data[blkn][0], state.cur.last_dc_val[ci], |
| 714 | entropy->dc_derived_tbls[compptr->dc_tbl_no], |
| 715 | entropy->ac_derived_tbls[compptr->ac_tbl_no])) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 716 | return FALSE; |
| 717 | /* Update last_dc_val */ |
| 718 | state.cur.last_dc_val[ci] = MCU_data[blkn][0][0]; |
| 719 | } |
| 720 | } else { |
| 721 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 722 | ci = cinfo->MCU_membership[blkn]; |
| 723 | compptr = cinfo->cur_comp_info[ci]; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 724 | if (!encode_one_block(&state, |
| 725 | MCU_data[blkn][0], state.cur.last_dc_val[ci], |
| 726 | entropy->dc_derived_tbls[compptr->dc_tbl_no], |
| 727 | entropy->ac_derived_tbls[compptr->ac_tbl_no])) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 728 | return FALSE; |
| 729 | /* Update last_dc_val */ |
| 730 | state.cur.last_dc_val[ci] = MCU_data[blkn][0][0]; |
| 731 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* Completed MCU, so update state */ |
| 735 | cinfo->dest->next_output_byte = state.next_output_byte; |
| 736 | cinfo->dest->free_in_buffer = state.free_in_buffer; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 737 | entropy->saved = state.cur; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 738 | |
| 739 | /* Update restart-interval state too */ |
| 740 | if (cinfo->restart_interval) { |
| 741 | if (entropy->restarts_to_go == 0) { |
| 742 | entropy->restarts_to_go = cinfo->restart_interval; |
| 743 | entropy->next_restart_num++; |
| 744 | entropy->next_restart_num &= 7; |
| 745 | } |
| 746 | entropy->restarts_to_go--; |
| 747 | } |
| 748 | |
| 749 | return TRUE; |
| 750 | } |
| 751 | |
| 752 | |
| 753 | /* |
| 754 | * Finish up at the end of a Huffman-compressed scan. |
| 755 | */ |
| 756 | |
| 757 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 758 | finish_pass_huff(j_compress_ptr cinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 759 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 760 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 761 | working_state state; |
| 762 | |
| 763 | /* Load up working state ... flush_bits needs it */ |
| 764 | state.next_output_byte = cinfo->dest->next_output_byte; |
| 765 | state.free_in_buffer = cinfo->dest->free_in_buffer; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 766 | state.cur = entropy->saved; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 767 | state.cinfo = cinfo; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 768 | state.simd = entropy->simd; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 769 | |
| 770 | /* Flush out the last data */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 771 | if (!flush_bits(&state)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 772 | ERREXIT(cinfo, JERR_CANT_SUSPEND); |
| 773 | |
| 774 | /* Update state */ |
| 775 | cinfo->dest->next_output_byte = state.next_output_byte; |
| 776 | cinfo->dest->free_in_buffer = state.free_in_buffer; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 777 | entropy->saved = state.cur; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | |
| 781 | /* |
| 782 | * Huffman coding optimization. |
| 783 | * |
| 784 | * We first scan the supplied data and count the number of uses of each symbol |
| 785 | * that is to be Huffman-coded. (This process MUST agree with the code above.) |
| 786 | * Then we build a Huffman coding tree for the observed counts. |
| 787 | * Symbols which are not needed at all for the particular image are not |
| 788 | * assigned any code, which saves space in the DHT marker as well as in |
| 789 | * the compressed data. |
| 790 | */ |
| 791 | |
| 792 | #ifdef ENTROPY_OPT_SUPPORTED |
| 793 | |
| 794 | |
| 795 | /* Process a single block's worth of coefficients */ |
| 796 | |
| 797 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 798 | htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val, |
| 799 | long dc_counts[], long ac_counts[]) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 800 | { |
| 801 | register int temp; |
| 802 | register int nbits; |
| 803 | register int k, r; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 804 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 805 | /* Encode the DC coefficient difference per section F.1.2.1 */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 806 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 807 | temp = block[0] - last_dc_val; |
| 808 | if (temp < 0) |
| 809 | temp = -temp; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 810 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 811 | /* Find the number of bits needed for the magnitude of the coefficient */ |
| 812 | nbits = 0; |
| 813 | while (temp) { |
| 814 | nbits++; |
| 815 | temp >>= 1; |
| 816 | } |
| 817 | /* Check for out-of-range coefficient values. |
| 818 | * Since we're encoding a difference, the range limit is twice as much. |
| 819 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 820 | if (nbits > MAX_COEF_BITS + 1) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 821 | ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
| 822 | |
| 823 | /* Count the Huffman symbol for the number of bits */ |
| 824 | dc_counts[nbits]++; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 825 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 826 | /* Encode the AC coefficients per section F.1.2.2 */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 827 | |
| 828 | r = 0; /* r = run length of zeros */ |
| 829 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 830 | for (k = 1; k < DCTSIZE2; k++) { |
| 831 | if ((temp = block[jpeg_natural_order[k]]) == 0) { |
| 832 | r++; |
| 833 | } else { |
| 834 | /* if run length > 15, must emit special run-length-16 codes (0xF0) */ |
| 835 | while (r > 15) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 836 | ac_counts[0xF0]++; |
| 837 | r -= 16; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 838 | } |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 839 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 840 | /* Find the number of bits needed for the magnitude of the coefficient */ |
| 841 | if (temp < 0) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 842 | temp = -temp; |
| 843 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 844 | /* Find the number of bits needed for the magnitude of the coefficient */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 845 | nbits = 1; /* there must be at least one 1 bit */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 846 | while ((temp >>= 1)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 847 | nbits++; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 848 | /* Check for out-of-range coefficient values */ |
| 849 | if (nbits > MAX_COEF_BITS) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 850 | ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
| 851 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 852 | /* Count Huffman symbol for run length / number of bits */ |
| 853 | ac_counts[(r << 4) + nbits]++; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 854 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 855 | r = 0; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* If the last coef(s) were zero, emit an end-of-block code */ |
| 860 | if (r > 0) |
| 861 | ac_counts[0]++; |
| 862 | } |
| 863 | |
| 864 | |
| 865 | /* |
| 866 | * Trial-encode one MCU's worth of Huffman-compressed coefficients. |
| 867 | * No data is actually output, so no suspension return is possible. |
| 868 | */ |
| 869 | |
| 870 | METHODDEF(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 871 | encode_mcu_gather(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 872 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 873 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 874 | int blkn, ci; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 875 | jpeg_component_info *compptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 876 | |
| 877 | /* Take care of restart intervals if needed */ |
| 878 | if (cinfo->restart_interval) { |
| 879 | if (entropy->restarts_to_go == 0) { |
| 880 | /* Re-initialize DC predictions to 0 */ |
| 881 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 882 | entropy->saved.last_dc_val[ci] = 0; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 883 | /* Update restart state */ |
| 884 | entropy->restarts_to_go = cinfo->restart_interval; |
| 885 | } |
| 886 | entropy->restarts_to_go--; |
| 887 | } |
| 888 | |
| 889 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 890 | ci = cinfo->MCU_membership[blkn]; |
| 891 | compptr = cinfo->cur_comp_info[ci]; |
| 892 | htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci], |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 893 | entropy->dc_count_ptrs[compptr->dc_tbl_no], |
| 894 | entropy->ac_count_ptrs[compptr->ac_tbl_no]); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 895 | entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0]; |
| 896 | } |
| 897 | |
| 898 | return TRUE; |
| 899 | } |
| 900 | |
| 901 | |
| 902 | /* |
| 903 | * Generate the best Huffman code table for the given counts, fill htbl. |
| 904 | * Note this is also used by jcphuff.c. |
| 905 | * |
| 906 | * The JPEG standard requires that no symbol be assigned a codeword of all |
| 907 | * one bits (so that padding bits added at the end of a compressed segment |
| 908 | * can't look like a valid code). Because of the canonical ordering of |
| 909 | * codewords, this just means that there must be an unused slot in the |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 910 | * longest codeword length category. Annex K (Clause K.2) of |
| 911 | * Rec. ITU-T T.81 (1992) | ISO/IEC 10918-1:1994 suggests reserving such a slot |
| 912 | * by pretending that symbol 256 is a valid symbol with count 1. In theory |
| 913 | * that's not optimal; giving it count zero but including it in the symbol set |
| 914 | * anyway should give a better Huffman code. But the theoretically better code |
| 915 | * actually seems to come out worse in practice, because it produces more |
| 916 | * all-ones bytes (which incur stuffed zero bytes in the final file). In any |
| 917 | * case the difference is tiny. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 918 | * |
| 919 | * The JPEG standard requires Huffman codes to be no more than 16 bits long. |
| 920 | * If some symbols have a very small but nonzero probability, the Huffman tree |
| 921 | * must be adjusted to meet the code length restriction. We currently use |
| 922 | * the adjustment method suggested in JPEG section K.2. This method is *not* |
| 923 | * optimal; it may not choose the best possible limited-length code. But |
| 924 | * typically only very-low-frequency symbols will be given less-than-optimal |
| 925 | * lengths, so the code is almost optimal. Experimental comparisons against |
| 926 | * an optimal limited-length-code algorithm indicate that the difference is |
| 927 | * microscopic --- usually less than a hundredth of a percent of total size. |
| 928 | * So the extra complexity of an optimal algorithm doesn't seem worthwhile. |
| 929 | */ |
| 930 | |
| 931 | GLOBAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 932 | jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[]) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 933 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 934 | #define MAX_CLEN 32 /* assumed maximum initial code length */ |
| 935 | UINT8 bits[MAX_CLEN + 1]; /* bits[k] = # of symbols with code length k */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 936 | int codesize[257]; /* codesize[k] = code length of symbol k */ |
| 937 | int others[257]; /* next symbol in current branch of tree */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 938 | int c1, c2; |
| 939 | int p, i, j; |
| 940 | long v; |
| 941 | |
| 942 | /* This algorithm is explained in section K.2 of the JPEG standard */ |
| 943 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 944 | MEMZERO(bits, sizeof(bits)); |
| 945 | MEMZERO(codesize, sizeof(codesize)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 946 | for (i = 0; i < 257; i++) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 947 | others[i] = -1; /* init links to empty */ |
| 948 | |
| 949 | freq[256] = 1; /* make sure 256 has a nonzero count */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 950 | /* Including the pseudo-symbol 256 in the Huffman procedure guarantees |
| 951 | * that no real symbol is given code-value of all ones, because 256 |
| 952 | * will be placed last in the largest codeword category. |
| 953 | */ |
| 954 | |
| 955 | /* Huffman's basic algorithm to assign optimal code lengths to symbols */ |
| 956 | |
| 957 | for (;;) { |
| 958 | /* Find the smallest nonzero frequency, set c1 = its symbol */ |
| 959 | /* In case of ties, take the larger symbol number */ |
| 960 | c1 = -1; |
| 961 | v = 1000000000L; |
| 962 | for (i = 0; i <= 256; i++) { |
| 963 | if (freq[i] && freq[i] <= v) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 964 | v = freq[i]; |
| 965 | c1 = i; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | /* Find the next smallest nonzero frequency, set c2 = its symbol */ |
| 970 | /* In case of ties, take the larger symbol number */ |
| 971 | c2 = -1; |
| 972 | v = 1000000000L; |
| 973 | for (i = 0; i <= 256; i++) { |
| 974 | if (freq[i] && freq[i] <= v && i != c1) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 975 | v = freq[i]; |
| 976 | c2 = i; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | /* Done if we've merged everything into one frequency */ |
| 981 | if (c2 < 0) |
| 982 | break; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 983 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 984 | /* Else merge the two counts/trees */ |
| 985 | freq[c1] += freq[c2]; |
| 986 | freq[c2] = 0; |
| 987 | |
| 988 | /* Increment the codesize of everything in c1's tree branch */ |
| 989 | codesize[c1]++; |
| 990 | while (others[c1] >= 0) { |
| 991 | c1 = others[c1]; |
| 992 | codesize[c1]++; |
| 993 | } |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 994 | |
| 995 | others[c1] = c2; /* chain c2 onto c1's tree branch */ |
| 996 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 997 | /* Increment the codesize of everything in c2's tree branch */ |
| 998 | codesize[c2]++; |
| 999 | while (others[c2] >= 0) { |
| 1000 | c2 = others[c2]; |
| 1001 | codesize[c2]++; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /* Now count the number of symbols of each code length */ |
| 1006 | for (i = 0; i <= 256; i++) { |
| 1007 | if (codesize[i]) { |
| 1008 | /* The JPEG standard seems to think that this can't happen, */ |
| 1009 | /* but I'm paranoid... */ |
| 1010 | if (codesize[i] > MAX_CLEN) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1011 | ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1012 | |
| 1013 | bits[codesize[i]]++; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure |
| 1018 | * Huffman procedure assigned any such lengths, we must adjust the coding. |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1019 | * Here is what Rec. ITU-T T.81 | ISO/IEC 10918-1 says about how this next |
| 1020 | * bit works: Since symbols are paired for the longest Huffman code, the |
| 1021 | * symbols are removed from this length category two at a time. The prefix |
| 1022 | * for the pair (which is one bit shorter) is allocated to one of the pair; |
| 1023 | * then, skipping the BITS entry for that prefix length, a code word from the |
| 1024 | * next shortest nonzero BITS entry is converted into a prefix for two code |
| 1025 | * words one bit longer. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1026 | */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1027 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1028 | for (i = MAX_CLEN; i > 16; i--) { |
| 1029 | while (bits[i] > 0) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1030 | j = i - 2; /* find length of new prefix to be used */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1031 | while (bits[j] == 0) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1032 | j--; |
| 1033 | |
| 1034 | bits[i] -= 2; /* remove two symbols */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1035 | bits[i - 1]++; /* one goes in this length */ |
| 1036 | bits[j + 1] += 2; /* two new symbols in this length */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1037 | bits[j]--; /* symbol of this length is now a prefix */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | /* Remove the count for the pseudo-symbol 256 from the largest codelength */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1042 | while (bits[i] == 0) /* find largest codelength still in use */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1043 | i--; |
| 1044 | bits[i]--; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1045 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1046 | /* Return final symbol counts (only for lengths 0..16) */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1047 | MEMCOPY(htbl->bits, bits, sizeof(htbl->bits)); |
| 1048 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1049 | /* Return a list of the symbols sorted by code length */ |
| 1050 | /* It's not real clear to me why we don't need to consider the codelength |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1051 | * changes made above, but Rec. ITU-T T.81 | ISO/IEC 10918-1 seems to think |
| 1052 | * this works. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1053 | */ |
| 1054 | p = 0; |
| 1055 | for (i = 1; i <= MAX_CLEN; i++) { |
| 1056 | for (j = 0; j <= 255; j++) { |
| 1057 | if (codesize[j] == i) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1058 | htbl->huffval[p] = (UINT8)j; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1059 | p++; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | /* Set sent_table FALSE so updated table will be written to JPEG file. */ |
| 1065 | htbl->sent_table = FALSE; |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | /* |
| 1070 | * Finish up a statistics-gathering pass and create the new Huffman tables. |
| 1071 | */ |
| 1072 | |
| 1073 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1074 | finish_pass_gather(j_compress_ptr cinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1075 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1076 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1077 | int ci, dctbl, actbl; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1078 | jpeg_component_info *compptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1079 | JHUFF_TBL **htblptr; |
| 1080 | boolean did_dc[NUM_HUFF_TBLS]; |
| 1081 | boolean did_ac[NUM_HUFF_TBLS]; |
| 1082 | |
| 1083 | /* It's important not to apply jpeg_gen_optimal_table more than once |
| 1084 | * per table, because it clobbers the input frequency counts! |
| 1085 | */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1086 | MEMZERO(did_dc, sizeof(did_dc)); |
| 1087 | MEMZERO(did_ac, sizeof(did_ac)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1088 | |
| 1089 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 1090 | compptr = cinfo->cur_comp_info[ci]; |
| 1091 | dctbl = compptr->dc_tbl_no; |
| 1092 | actbl = compptr->ac_tbl_no; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1093 | if (!did_dc[dctbl]) { |
| 1094 | htblptr = &cinfo->dc_huff_tbl_ptrs[dctbl]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1095 | if (*htblptr == NULL) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1096 | *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1097 | jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]); |
| 1098 | did_dc[dctbl] = TRUE; |
| 1099 | } |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1100 | if (!did_ac[actbl]) { |
| 1101 | htblptr = &cinfo->ac_huff_tbl_ptrs[actbl]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1102 | if (*htblptr == NULL) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1103 | *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1104 | jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]); |
| 1105 | did_ac[actbl] = TRUE; |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | #endif /* ENTROPY_OPT_SUPPORTED */ |
| 1112 | |
| 1113 | |
| 1114 | /* |
| 1115 | * Module initialization routine for Huffman entropy encoding. |
| 1116 | */ |
| 1117 | |
| 1118 | GLOBAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1119 | jinit_huff_encoder(j_compress_ptr cinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1120 | { |
| 1121 | huff_entropy_ptr entropy; |
| 1122 | int i; |
| 1123 | |
| 1124 | entropy = (huff_entropy_ptr) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1125 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 1126 | sizeof(huff_entropy_encoder)); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1127 | cinfo->entropy = (struct jpeg_entropy_encoder *)entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1128 | entropy->pub.start_pass = start_pass_huff; |
| 1129 | |
| 1130 | /* Mark tables unallocated */ |
| 1131 | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
| 1132 | entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; |
| 1133 | #ifdef ENTROPY_OPT_SUPPORTED |
| 1134 | entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL; |
| 1135 | #endif |
| 1136 | } |
| 1137 | } |