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