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