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