Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jchuff.c |
| 3 | * |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 4 | * Copyright (C) 1991, 1992, Thomas G. Lane. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 5 | * This file is part of the Independent JPEG Group's software. |
| 6 | * For conditions of distribution and use, see the accompanying README file. |
| 7 | * |
| 8 | * This file contains Huffman entropy encoding routines. |
| 9 | * These routines are invoked via the methods entropy_encode, |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 10 | * entropy_encode_init/term, and entropy_optimize. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include "jinclude.h" |
| 14 | |
| 15 | |
| 16 | /* Static variables to avoid passing 'round extra parameters */ |
| 17 | |
| 18 | static compress_info_ptr cinfo; |
| 19 | |
| 20 | static INT32 huff_put_buffer; /* current bit-accumulation buffer */ |
| 21 | static int huff_put_bits; /* # of bits now in it */ |
| 22 | |
| 23 | static char * output_buffer; /* output buffer */ |
| 24 | static int bytes_in_buffer; |
| 25 | |
| 26 | |
| 27 | |
| 28 | LOCAL void |
| 29 | fix_huff_tbl (HUFF_TBL * htbl) |
| 30 | /* Compute derived values for a Huffman table */ |
| 31 | { |
| 32 | int p, i, l, lastp, si; |
| 33 | char huffsize[257]; |
| 34 | UINT16 huffcode[257]; |
| 35 | UINT16 code; |
| 36 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 37 | /* 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] | 38 | /* Note that this is in code-length order. */ |
| 39 | |
| 40 | p = 0; |
| 41 | for (l = 1; l <= 16; l++) { |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 42 | for (i = 1; i <= (int) htbl->bits[l]; i++) |
| 43 | huffsize[p++] = (char) l; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 44 | } |
| 45 | huffsize[p] = 0; |
| 46 | lastp = p; |
| 47 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 48 | /* Figure C.2: generate the codes themselves */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 49 | /* Note that this is in code-length order. */ |
| 50 | |
| 51 | code = 0; |
| 52 | si = huffsize[0]; |
| 53 | p = 0; |
| 54 | while (huffsize[p]) { |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 55 | while (((int) huffsize[p]) == si) { |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 56 | huffcode[p++] = code; |
| 57 | code++; |
| 58 | } |
| 59 | code <<= 1; |
| 60 | si++; |
| 61 | } |
| 62 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 63 | /* Figure C.3: generate encoding tables */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 64 | /* These are code and size indexed by symbol value */ |
| 65 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 66 | /* Set any codeless symbols to have code length 0; |
| 67 | * this allows emit_bits to detect any attempt to emit such symbols. |
| 68 | */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 69 | MEMZERO(htbl->ehufsi, SIZEOF(htbl->ehufsi)); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 70 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 71 | for (p = 0; p < lastp; p++) { |
| 72 | htbl->ehufco[htbl->huffval[p]] = huffcode[p]; |
| 73 | htbl->ehufsi[htbl->huffval[p]] = huffsize[p]; |
| 74 | } |
| 75 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 76 | /* We don't bother to fill in the decoding tables mincode[], maxcode[], */ |
| 77 | /* and valptr[], since they are not used for encoding. */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
| 81 | /* Outputting bytes to the file */ |
| 82 | |
| 83 | LOCAL void |
| 84 | flush_bytes (void) |
| 85 | { |
| 86 | if (bytes_in_buffer) |
| 87 | (*cinfo->methods->entropy_output) (cinfo, output_buffer, bytes_in_buffer); |
| 88 | bytes_in_buffer = 0; |
| 89 | } |
| 90 | |
| 91 | |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 92 | #define emit_byte(val) \ |
| 93 | MAKESTMT( if (bytes_in_buffer >= JPEG_BUF_SIZE) \ |
| 94 | flush_bytes(); \ |
| 95 | output_buffer[bytes_in_buffer++] = (char) (val); ) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | |
| 99 | /* Outputting bits to the file */ |
| 100 | |
| 101 | /* Only the right 24 bits of huff_put_buffer are used; the valid bits are |
| 102 | * left-justified in this part. At most 16 bits can be passed to emit_bits |
| 103 | * in one call, and we never retain more than 7 bits in huff_put_buffer |
| 104 | * between calls, so 24 bits are sufficient. |
| 105 | */ |
| 106 | |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 107 | INLINE |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 108 | LOCAL void |
| 109 | emit_bits (UINT16 code, int size) |
| 110 | { |
| 111 | /* This routine is heavily used, so it's worth coding tightly. */ |
| 112 | register INT32 put_buffer = code; |
| 113 | register int put_bits = huff_put_bits; |
| 114 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 115 | /* if size is 0, caller used an invalid Huffman table entry */ |
| 116 | if (size == 0) |
| 117 | ERREXIT(cinfo->emethods, "Missing Huffman code table entry"); |
| 118 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 119 | put_buffer &= (((INT32) 1) << size) - 1; /* Mask off any excess bits in code */ |
| 120 | |
| 121 | put_bits += size; /* new number of bits in buffer */ |
| 122 | |
| 123 | put_buffer <<= 24 - put_bits; /* align incoming bits */ |
| 124 | |
| 125 | put_buffer |= huff_put_buffer; /* and merge with old buffer contents */ |
| 126 | |
| 127 | while (put_bits >= 8) { |
| 128 | int c = (int) ((put_buffer >> 16) & 0xFF); |
| 129 | |
| 130 | emit_byte(c); |
| 131 | if (c == 0xFF) { /* need to stuff a zero byte? */ |
| 132 | emit_byte(0); |
| 133 | } |
| 134 | put_buffer <<= 8; |
| 135 | put_bits -= 8; |
| 136 | } |
| 137 | |
| 138 | huff_put_buffer = put_buffer; /* Update global variables */ |
| 139 | huff_put_bits = put_bits; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | LOCAL void |
| 144 | flush_bits (void) |
| 145 | { |
| 146 | emit_bits((UINT16) 0x7F, 7); /* fill any partial byte with ones */ |
| 147 | huff_put_buffer = 0; /* and reset bit-buffer to empty */ |
| 148 | huff_put_bits = 0; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | /* Encode a single block's worth of coefficients */ |
| 154 | /* Note that the DC coefficient has already been converted to a difference */ |
| 155 | |
| 156 | LOCAL void |
| 157 | encode_one_block (JBLOCK block, HUFF_TBL *dctbl, HUFF_TBL *actbl) |
| 158 | { |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 159 | register int temp, temp2; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 160 | register int nbits; |
| 161 | register int k, r, i; |
| 162 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 163 | /* Encode the DC coefficient difference per section F.1.2.1 */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 164 | |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 165 | temp = temp2 = block[0]; |
| 166 | |
| 167 | if (temp < 0) { |
| 168 | temp = -temp; /* temp is abs value of input */ |
| 169 | /* For a negative input, want temp2 = bitwise complement of abs(input) */ |
| 170 | /* This code assumes we are on a two's complement machine */ |
| 171 | temp2--; |
| 172 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 173 | |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 174 | /* Find the number of bits needed for the magnitude of the coefficient */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 175 | nbits = 0; |
| 176 | while (temp) { |
| 177 | nbits++; |
| 178 | temp >>= 1; |
| 179 | } |
| 180 | |
| 181 | /* Emit the Huffman-coded symbol for the number of bits */ |
| 182 | emit_bits(dctbl->ehufco[nbits], dctbl->ehufsi[nbits]); |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 183 | |
| 184 | /* Emit that number of bits of the value, if positive, */ |
| 185 | /* or the complement of its magnitude, if negative. */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 186 | if (nbits) /* emit_bits rejects calls with size 0 */ |
| 187 | emit_bits((UINT16) temp2, nbits); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 188 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 189 | /* Encode the AC coefficients per section F.1.2.2 */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 190 | |
| 191 | r = 0; /* r = run length of zeros */ |
| 192 | |
| 193 | for (k = 1; k < DCTSIZE2; k++) { |
| 194 | if ((temp = block[k]) == 0) { |
| 195 | r++; |
| 196 | } else { |
| 197 | /* if run length > 15, must emit special run-length-16 codes (0xF0) */ |
| 198 | while (r > 15) { |
| 199 | emit_bits(actbl->ehufco[0xF0], actbl->ehufsi[0xF0]); |
| 200 | r -= 16; |
| 201 | } |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 202 | |
| 203 | temp2 = temp; |
| 204 | if (temp < 0) { |
| 205 | temp = -temp; /* temp is abs value of input */ |
| 206 | /* This code assumes we are on a two's complement machine */ |
| 207 | temp2--; |
| 208 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 209 | |
| 210 | /* Find the number of bits needed for the magnitude of the coefficient */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 211 | nbits = 1; /* there must be at least one 1 bit */ |
| 212 | while (temp >>= 1) |
| 213 | nbits++; |
| 214 | |
| 215 | /* Emit Huffman symbol for run length / number of bits */ |
| 216 | i = (r << 4) + nbits; |
| 217 | emit_bits(actbl->ehufco[i], actbl->ehufsi[i]); |
| 218 | |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 219 | /* Emit that number of bits of the value, if positive, */ |
| 220 | /* or the complement of its magnitude, if negative. */ |
| 221 | emit_bits((UINT16) temp2, nbits); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 222 | |
| 223 | r = 0; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* If the last coef(s) were zero, emit an end-of-block code */ |
| 228 | if (r > 0) |
| 229 | emit_bits(actbl->ehufco[0], actbl->ehufsi[0]); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | |
| 234 | /* |
| 235 | * Initialize for a Huffman-compressed scan. |
| 236 | * This is invoked after writing the SOS marker. |
| 237 | * The pipeline controller must establish the entropy_output method pointer |
| 238 | * before calling this routine. |
| 239 | */ |
| 240 | |
| 241 | METHODDEF void |
| 242 | huff_init (compress_info_ptr xinfo) |
| 243 | { |
| 244 | short ci; |
| 245 | jpeg_component_info * compptr; |
| 246 | |
| 247 | /* Initialize static variables */ |
| 248 | cinfo = xinfo; |
| 249 | huff_put_buffer = 0; |
| 250 | huff_put_bits = 0; |
| 251 | |
| 252 | /* Initialize the output buffer */ |
| 253 | output_buffer = (char *) (*cinfo->emethods->alloc_small) |
| 254 | ((size_t) JPEG_BUF_SIZE); |
| 255 | bytes_in_buffer = 0; |
| 256 | |
| 257 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 258 | compptr = cinfo->cur_comp_info[ci]; |
| 259 | /* Make sure requested tables are present */ |
| 260 | if (cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no] == NULL || |
| 261 | cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no] == NULL) |
| 262 | ERREXIT(cinfo->emethods, "Use of undefined Huffman table"); |
| 263 | /* Compute derived values for Huffman tables */ |
| 264 | /* We may do this more than once for same table, but it's not a big deal */ |
| 265 | fix_huff_tbl(cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no]); |
| 266 | fix_huff_tbl(cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]); |
| 267 | /* Initialize DC predictions to 0 */ |
| 268 | cinfo->last_dc_val[ci] = 0; |
| 269 | } |
| 270 | |
| 271 | /* Initialize restart stuff */ |
| 272 | cinfo->restarts_to_go = cinfo->restart_interval; |
| 273 | cinfo->next_restart_num = 0; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /* |
| 278 | * Emit a restart marker & resynchronize predictions. |
| 279 | */ |
| 280 | |
| 281 | LOCAL void |
| 282 | emit_restart (compress_info_ptr cinfo) |
| 283 | { |
| 284 | short ci; |
| 285 | |
| 286 | flush_bits(); |
| 287 | |
| 288 | emit_byte(0xFF); |
| 289 | emit_byte(RST0 + cinfo->next_restart_num); |
| 290 | |
| 291 | /* Re-initialize DC predictions to 0 */ |
| 292 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) |
| 293 | cinfo->last_dc_val[ci] = 0; |
| 294 | |
| 295 | /* Update restart state */ |
| 296 | cinfo->restarts_to_go = cinfo->restart_interval; |
| 297 | cinfo->next_restart_num++; |
| 298 | cinfo->next_restart_num &= 7; |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /* |
| 303 | * Encode and output one MCU's worth of Huffman-compressed coefficients. |
| 304 | */ |
| 305 | |
| 306 | METHODDEF void |
| 307 | huff_encode (compress_info_ptr cinfo, JBLOCK *MCU_data) |
| 308 | { |
| 309 | short blkn, ci; |
| 310 | jpeg_component_info * compptr; |
| 311 | JCOEF temp; |
| 312 | |
| 313 | /* Account for restart interval, emit restart marker if needed */ |
| 314 | if (cinfo->restart_interval) { |
| 315 | if (cinfo->restarts_to_go == 0) |
| 316 | emit_restart(cinfo); |
| 317 | cinfo->restarts_to_go--; |
| 318 | } |
| 319 | |
| 320 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 321 | ci = cinfo->MCU_membership[blkn]; |
| 322 | compptr = cinfo->cur_comp_info[ci]; |
| 323 | /* Convert DC value to difference, update last_dc_val */ |
| 324 | temp = MCU_data[blkn][0]; |
| 325 | MCU_data[blkn][0] -= cinfo->last_dc_val[ci]; |
| 326 | cinfo->last_dc_val[ci] = temp; |
| 327 | encode_one_block(MCU_data[blkn], |
| 328 | cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no], |
| 329 | cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /* |
| 335 | * Finish up at the end of a Huffman-compressed scan. |
| 336 | */ |
| 337 | |
| 338 | METHODDEF void |
| 339 | huff_term (compress_info_ptr cinfo) |
| 340 | { |
| 341 | /* Flush out the last data */ |
| 342 | flush_bits(); |
| 343 | flush_bytes(); |
| 344 | /* Release the I/O buffer */ |
| 345 | (*cinfo->emethods->free_small) ((void *) output_buffer); |
| 346 | } |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | /* |
| 352 | * Huffman coding optimization. |
| 353 | * |
| 354 | * This actually is optimization, in the sense that we find the best possible |
| 355 | * Huffman table(s) for the given data. We first scan the supplied data and |
| 356 | * count the number of uses of each symbol that is to be Huffman-coded. |
| 357 | * (This process must agree with the code above.) Then we build an |
| 358 | * optimal Huffman coding tree for the observed counts. |
| 359 | */ |
| 360 | |
| 361 | #ifdef ENTROPY_OPT_SUPPORTED |
| 362 | |
| 363 | |
| 364 | /* These are static so htest_one_block can find 'em */ |
| 365 | static long * dc_count_ptrs[NUM_HUFF_TBLS]; |
| 366 | static long * ac_count_ptrs[NUM_HUFF_TBLS]; |
| 367 | |
| 368 | |
| 369 | LOCAL void |
| 370 | gen_huff_coding (compress_info_ptr cinfo, HUFF_TBL *htbl, long freq[]) |
| 371 | /* Generate the optimal coding for the given counts */ |
| 372 | { |
| 373 | #define MAX_CLEN 32 /* assumed maximum initial code length */ |
| 374 | UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */ |
| 375 | short codesize[257]; /* codesize[k] = code length of symbol k */ |
| 376 | short others[257]; /* next symbol in current branch of tree */ |
| 377 | int c1, c2; |
| 378 | int p, i, j; |
| 379 | long v; |
| 380 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 381 | /* 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] | 382 | |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 383 | MEMZERO(bits, SIZEOF(bits)); |
| 384 | MEMZERO(codesize, SIZEOF(codesize)); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 385 | for (i = 0; i < 257; i++) |
| 386 | others[i] = -1; /* init links to empty */ |
| 387 | |
| 388 | freq[256] = 1; /* make sure there is a nonzero count */ |
| 389 | /* including the pseudo-symbol 256 in the Huffman procedure guarantees |
| 390 | * that no real symbol is given code-value of all ones, because 256 |
| 391 | * will be placed in the largest codeword category. |
| 392 | */ |
| 393 | |
| 394 | /* Huffman's basic algorithm to assign optimal code lengths to symbols */ |
| 395 | |
| 396 | for (;;) { |
| 397 | /* Find the smallest nonzero frequency, set c1 = its symbol */ |
| 398 | /* In case of ties, take the larger symbol number */ |
| 399 | c1 = -1; |
| 400 | v = 1000000000L; |
| 401 | for (i = 0; i <= 256; i++) { |
| 402 | if (freq[i] && freq[i] <= v) { |
| 403 | v = freq[i]; |
| 404 | c1 = i; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* Find the next smallest nonzero frequency, set c2 = its symbol */ |
| 409 | /* In case of ties, take the larger symbol number */ |
| 410 | c2 = -1; |
| 411 | v = 1000000000L; |
| 412 | for (i = 0; i <= 256; i++) { |
| 413 | if (freq[i] && freq[i] <= v && i != c1) { |
| 414 | v = freq[i]; |
| 415 | c2 = i; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /* Done if we've merged everything into one frequency */ |
| 420 | if (c2 < 0) |
| 421 | break; |
| 422 | |
| 423 | /* Else merge the two counts/trees */ |
| 424 | freq[c1] += freq[c2]; |
| 425 | freq[c2] = 0; |
| 426 | |
| 427 | /* Increment the codesize of everything in c1's tree branch */ |
| 428 | codesize[c1]++; |
| 429 | while (others[c1] >= 0) { |
| 430 | c1 = others[c1]; |
| 431 | codesize[c1]++; |
| 432 | } |
| 433 | |
| 434 | others[c1] = c2; /* chain c2 onto c1's tree branch */ |
| 435 | |
| 436 | /* Increment the codesize of everything in c2's tree branch */ |
| 437 | codesize[c2]++; |
| 438 | while (others[c2] >= 0) { |
| 439 | c2 = others[c2]; |
| 440 | codesize[c2]++; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /* Now count the number of symbols of each code length */ |
| 445 | for (i = 0; i <= 256; i++) { |
| 446 | if (codesize[i]) { |
| 447 | /* The JPEG standard seems to think that this can't happen, */ |
| 448 | /* but I'm paranoid... */ |
| 449 | if (codesize[i] > MAX_CLEN) |
| 450 | ERREXIT(cinfo->emethods, "Huffman code size table overflow"); |
| 451 | |
| 452 | bits[codesize[i]]++; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure |
| 457 | * Huffman procedure assigned any such lengths, we must adjust the coding. |
| 458 | * Here is what the JPEG spec says about how this next bit works: |
| 459 | * Since symbols are paired for the longest Huffman code, the symbols are |
| 460 | * removed from this length category two at a time. The prefix for the pair |
| 461 | * (which is one bit shorter) is allocated to one of the pair; then, |
| 462 | * skipping the BITS entry for that prefix length, a code word from the next |
| 463 | * shortest nonzero BITS entry is converted into a prefix for two code words |
| 464 | * one bit longer. |
| 465 | */ |
| 466 | |
| 467 | for (i = MAX_CLEN; i > 16; i--) { |
| 468 | while (bits[i] > 0) { |
| 469 | j = i - 2; /* find length of new prefix to be used */ |
| 470 | while (bits[j] == 0) |
| 471 | j--; |
| 472 | |
| 473 | bits[i] -= 2; /* remove two symbols */ |
| 474 | bits[i-1]++; /* one goes in this length */ |
| 475 | bits[j+1] += 2; /* two new symbols in this length */ |
| 476 | bits[j]--; /* symbol of this length is now a prefix */ |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /* Remove the count for the pseudo-symbol 256 from the largest codelength */ |
| 481 | while (bits[i] == 0) /* find largest codelength still in use */ |
| 482 | i--; |
| 483 | bits[i]--; |
| 484 | |
| 485 | /* Return final symbol counts (only for lengths 0..16) */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 486 | MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits)); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 487 | |
| 488 | /* Return a list of the symbols sorted by code length */ |
| 489 | /* It's not real clear to me why we don't need to consider the codelength |
| 490 | * changes made above, but the JPEG spec seems to think this works. |
| 491 | */ |
| 492 | p = 0; |
| 493 | for (i = 1; i <= MAX_CLEN; i++) { |
| 494 | for (j = 0; j <= 255; j++) { |
| 495 | if (codesize[j] == i) { |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 496 | htbl->huffval[p] = (UINT8) j; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 497 | p++; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /* Process a single block's worth of coefficients */ |
| 505 | /* Note that the DC coefficient has already been converted to a difference */ |
| 506 | |
| 507 | LOCAL void |
| 508 | htest_one_block (JBLOCK block, JCOEF block0, |
| 509 | long dc_counts[], long ac_counts[]) |
| 510 | { |
| 511 | register INT32 temp; |
| 512 | register int nbits; |
| 513 | register int k, r; |
| 514 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 515 | /* Encode the DC coefficient difference per section F.1.2.1 */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 516 | |
| 517 | /* Find the number of bits needed for the magnitude of the coefficient */ |
| 518 | temp = block0; |
| 519 | if (temp < 0) temp = -temp; |
| 520 | |
| 521 | for (nbits = 0; temp; nbits++) |
| 522 | temp >>= 1; |
| 523 | |
| 524 | /* Count the Huffman symbol for the number of bits */ |
| 525 | dc_counts[nbits]++; |
| 526 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 527 | /* Encode the AC coefficients per section F.1.2.2 */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 528 | |
| 529 | r = 0; /* r = run length of zeros */ |
| 530 | |
| 531 | for (k = 1; k < DCTSIZE2; k++) { |
| 532 | if ((temp = block[k]) == 0) { |
| 533 | r++; |
| 534 | } else { |
| 535 | /* if run length > 15, must emit special run-length-16 codes (0xF0) */ |
| 536 | while (r > 15) { |
| 537 | ac_counts[0xF0]++; |
| 538 | r -= 16; |
| 539 | } |
| 540 | |
| 541 | /* Find the number of bits needed for the magnitude of the coefficient */ |
| 542 | if (temp < 0) temp = -temp; |
| 543 | |
| 544 | for (nbits = 0; temp; nbits++) |
| 545 | temp >>= 1; |
| 546 | |
| 547 | /* Count Huffman symbol for run length / number of bits */ |
| 548 | ac_counts[(r << 4) + nbits]++; |
| 549 | |
| 550 | r = 0; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* If the last coef(s) were zero, emit an end-of-block code */ |
| 555 | if (r > 0) |
| 556 | ac_counts[0]++; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | |
| 561 | /* |
| 562 | * Trial-encode one MCU's worth of Huffman-compressed coefficients. |
| 563 | */ |
| 564 | |
| 565 | LOCAL void |
| 566 | htest_encode (compress_info_ptr cinfo, JBLOCK *MCU_data) |
| 567 | { |
| 568 | short blkn, ci; |
| 569 | jpeg_component_info * compptr; |
| 570 | |
| 571 | /* Take care of restart intervals if needed */ |
| 572 | if (cinfo->restart_interval) { |
| 573 | if (cinfo->restarts_to_go == 0) { |
| 574 | /* Re-initialize DC predictions to 0 */ |
| 575 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) |
| 576 | cinfo->last_dc_val[ci] = 0; |
| 577 | /* Update restart state */ |
| 578 | cinfo->restarts_to_go = cinfo->restart_interval; |
| 579 | } |
| 580 | cinfo->restarts_to_go--; |
| 581 | } |
| 582 | |
| 583 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 584 | ci = cinfo->MCU_membership[blkn]; |
| 585 | compptr = cinfo->cur_comp_info[ci]; |
| 586 | /* NB: unlike the real entropy encoder, we may not change the input data */ |
| 587 | htest_one_block(MCU_data[blkn], |
| 588 | (JCOEF) (MCU_data[blkn][0] - cinfo->last_dc_val[ci]), |
| 589 | dc_count_ptrs[compptr->dc_tbl_no], |
| 590 | ac_count_ptrs[compptr->ac_tbl_no]); |
| 591 | cinfo->last_dc_val[ci] = MCU_data[blkn][0]; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | |
| 596 | |
| 597 | /* |
| 598 | * Find the best coding parameters for a Huffman-coded scan. |
| 599 | * When called, the scan data has already been converted to a sequence of |
| 600 | * MCU groups of quantized coefficients, which are stored in a "big" array. |
| 601 | * The source_method knows how to iterate through that array. |
| 602 | * On return, the MCU data is unmodified, but the Huffman tables referenced |
| 603 | * by the scan components may have been altered. |
| 604 | */ |
| 605 | |
| 606 | METHODDEF void |
| 607 | huff_optimize (compress_info_ptr cinfo, MCU_output_caller_ptr source_method) |
| 608 | /* Optimize Huffman-coding parameters (Huffman symbol table) */ |
| 609 | { |
| 610 | int i, tbl; |
| 611 | HUFF_TBL **htblptr; |
| 612 | |
| 613 | /* Allocate and zero the count tables */ |
| 614 | /* Note that gen_huff_coding expects 257 entries in each table! */ |
| 615 | |
| 616 | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
| 617 | dc_count_ptrs[i] = NULL; |
| 618 | ac_count_ptrs[i] = NULL; |
| 619 | } |
| 620 | |
| 621 | for (i = 0; i < cinfo->comps_in_scan; i++) { |
| 622 | /* Create DC table */ |
| 623 | tbl = cinfo->cur_comp_info[i]->dc_tbl_no; |
| 624 | if (dc_count_ptrs[tbl] == NULL) { |
| 625 | dc_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small) |
| 626 | (257 * SIZEOF(long)); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 627 | MEMZERO(dc_count_ptrs[tbl], 257 * SIZEOF(long)); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 628 | } |
| 629 | /* Create AC table */ |
| 630 | tbl = cinfo->cur_comp_info[i]->ac_tbl_no; |
| 631 | if (ac_count_ptrs[tbl] == NULL) { |
| 632 | ac_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small) |
| 633 | (257 * SIZEOF(long)); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 634 | MEMZERO(ac_count_ptrs[tbl], 257 * SIZEOF(long)); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | /* Initialize DC predictions to 0 */ |
| 639 | for (i = 0; i < cinfo->comps_in_scan; i++) { |
| 640 | cinfo->last_dc_val[i] = 0; |
| 641 | } |
| 642 | /* Initialize restart stuff */ |
| 643 | cinfo->restarts_to_go = cinfo->restart_interval; |
| 644 | |
| 645 | /* Scan the MCU data, count symbol uses */ |
| 646 | (*source_method) (cinfo, htest_encode); |
| 647 | |
| 648 | /* Now generate optimal Huffman tables */ |
| 649 | for (tbl = 0; tbl < NUM_HUFF_TBLS; tbl++) { |
| 650 | if (dc_count_ptrs[tbl] != NULL) { |
| 651 | htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; |
| 652 | if (*htblptr == NULL) |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 653 | *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL)); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 654 | /* Set sent_table FALSE so updated table will be written to JPEG file. */ |
| 655 | (*htblptr)->sent_table = FALSE; |
| 656 | /* Compute the optimal Huffman encoding */ |
| 657 | gen_huff_coding(cinfo, *htblptr, dc_count_ptrs[tbl]); |
| 658 | /* Release the count table */ |
| 659 | (*cinfo->emethods->free_small) ((void *) dc_count_ptrs[tbl]); |
| 660 | } |
| 661 | if (ac_count_ptrs[tbl] != NULL) { |
| 662 | htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; |
| 663 | if (*htblptr == NULL) |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 664 | *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL)); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 665 | /* Set sent_table FALSE so updated table will be written to JPEG file. */ |
| 666 | (*htblptr)->sent_table = FALSE; |
| 667 | /* Compute the optimal Huffman encoding */ |
| 668 | gen_huff_coding(cinfo, *htblptr, ac_count_ptrs[tbl]); |
| 669 | /* Release the count table */ |
| 670 | (*cinfo->emethods->free_small) ((void *) ac_count_ptrs[tbl]); |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | |
| 676 | #endif /* ENTROPY_OPT_SUPPORTED */ |
| 677 | |
| 678 | |
| 679 | /* |
| 680 | * The method selection routine for Huffman entropy encoding. |
| 681 | */ |
| 682 | |
| 683 | GLOBAL void |
| 684 | jselchuffman (compress_info_ptr cinfo) |
| 685 | { |
| 686 | if (! cinfo->arith_code) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 687 | cinfo->methods->entropy_encode_init = huff_init; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 688 | cinfo->methods->entropy_encode = huff_encode; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 689 | cinfo->methods->entropy_encode_term = huff_term; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 690 | #ifdef ENTROPY_OPT_SUPPORTED |
| 691 | cinfo->methods->entropy_optimize = huff_optimize; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 692 | /* The standard Huffman tables are only valid for 8-bit data precision. |
| 693 | * If the precision is higher, force optimization on so that usable |
| 694 | * tables will be computed. This test can be removed if default tables |
| 695 | * are supplied that are valid for the desired precision. |
| 696 | */ |
| 697 | if (cinfo->data_precision > 8) |
| 698 | cinfo->optimize_coding = TRUE; |
| 699 | if (cinfo->optimize_coding) |
| 700 | cinfo->total_passes++; /* one pass needed for entropy optimization */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 701 | #endif |
| 702 | } |
| 703 | } |