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