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