1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libjpeg/jcphuff.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,831 @@ 1.4 +/* 1.5 + * jcphuff.c 1.6 + * 1.7 + * Copyright (C) 1995-1997, Thomas G. Lane. 1.8 + * This file is part of the Independent JPEG Group's software. 1.9 + * For conditions of distribution and use, see the accompanying README file. 1.10 + * 1.11 + * This file contains Huffman entropy encoding routines for progressive JPEG. 1.12 + * 1.13 + * We do not support output suspension in this module, since the library 1.14 + * currently does not allow multiple-scan files to be written with output 1.15 + * suspension. 1.16 + */ 1.17 + 1.18 +#define JPEG_INTERNALS 1.19 +#include "jinclude.h" 1.20 +#include "jpeglib.h" 1.21 +#include "jchuff.h" /* Declarations shared with jchuff.c */ 1.22 + 1.23 +#ifdef C_PROGRESSIVE_SUPPORTED 1.24 + 1.25 +/* Expanded entropy encoder object for progressive Huffman encoding. */ 1.26 + 1.27 +typedef struct { 1.28 + struct jpeg_entropy_encoder pub; /* public fields */ 1.29 + 1.30 + /* Mode flag: TRUE for optimization, FALSE for actual data output */ 1.31 + boolean gather_statistics; 1.32 + 1.33 + /* Bit-level coding status. 1.34 + * next_output_byte/free_in_buffer are local copies of cinfo->dest fields. 1.35 + */ 1.36 + JOCTET * next_output_byte; /* => next byte to write in buffer */ 1.37 + size_t free_in_buffer; /* # of byte spaces remaining in buffer */ 1.38 + INT32 put_buffer; /* current bit-accumulation buffer */ 1.39 + int put_bits; /* # of bits now in it */ 1.40 + j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */ 1.41 + 1.42 + /* Coding status for DC components */ 1.43 + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ 1.44 + 1.45 + /* Coding status for AC components */ 1.46 + int ac_tbl_no; /* the table number of the single component */ 1.47 + unsigned int EOBRUN; /* run length of EOBs */ 1.48 + unsigned int BE; /* # of buffered correction bits before MCU */ 1.49 + char * bit_buffer; /* buffer for correction bits (1 per char) */ 1.50 + /* packing correction bits tightly would save some space but cost time... */ 1.51 + 1.52 + unsigned int restarts_to_go; /* MCUs left in this restart interval */ 1.53 + int next_restart_num; /* next restart number to write (0-7) */ 1.54 + 1.55 + /* Pointers to derived tables (these workspaces have image lifespan). 1.56 + * Since any one scan codes only DC or only AC, we only need one set 1.57 + * of tables, not one for DC and one for AC. 1.58 + */ 1.59 + c_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; 1.60 + 1.61 + /* Statistics tables for optimization; again, one set is enough */ 1.62 + long * count_ptrs[NUM_HUFF_TBLS]; 1.63 +} phuff_entropy_encoder; 1.64 + 1.65 +typedef phuff_entropy_encoder * phuff_entropy_ptr; 1.66 + 1.67 +/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit 1.68 + * buffer can hold. Larger sizes may slightly improve compression, but 1.69 + * 1000 is already well into the realm of overkill. 1.70 + * The minimum safe size is 64 bits. 1.71 + */ 1.72 + 1.73 +#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */ 1.74 + 1.75 +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. 1.76 + * We assume that int right shift is unsigned if INT32 right shift is, 1.77 + * which should be safe. 1.78 + */ 1.79 + 1.80 +#ifdef RIGHT_SHIFT_IS_UNSIGNED 1.81 +#define ISHIFT_TEMPS int ishift_temp; 1.82 +#define IRIGHT_SHIFT(x,shft) \ 1.83 + ((ishift_temp = (x)) < 0 ? \ 1.84 + (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ 1.85 + (ishift_temp >> (shft))) 1.86 +#else 1.87 +#define ISHIFT_TEMPS 1.88 +#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) 1.89 +#endif 1.90 + 1.91 +/* Forward declarations */ 1.92 +METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo, 1.93 + JBLOCKROW *MCU_data)); 1.94 +METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo, 1.95 + JBLOCKROW *MCU_data)); 1.96 +METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo, 1.97 + JBLOCKROW *MCU_data)); 1.98 +METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo, 1.99 + JBLOCKROW *MCU_data)); 1.100 +METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo)); 1.101 +METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo)); 1.102 + 1.103 + 1.104 +/* 1.105 + * Initialize for a Huffman-compressed scan using progressive JPEG. 1.106 + */ 1.107 + 1.108 +METHODDEF(void) 1.109 +start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics) 1.110 +{ 1.111 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.112 + boolean is_DC_band; 1.113 + int ci, tbl; 1.114 + jpeg_component_info * compptr; 1.115 + 1.116 + entropy->cinfo = cinfo; 1.117 + entropy->gather_statistics = gather_statistics; 1.118 + 1.119 + is_DC_band = (cinfo->Ss == 0); 1.120 + 1.121 + /* We assume jcmaster.c already validated the scan parameters. */ 1.122 + 1.123 + /* Select execution routines */ 1.124 + if (cinfo->Ah == 0) { 1.125 + if (is_DC_band) 1.126 + entropy->pub.encode_mcu = encode_mcu_DC_first; 1.127 + else 1.128 + entropy->pub.encode_mcu = encode_mcu_AC_first; 1.129 + } else { 1.130 + if (is_DC_band) 1.131 + entropy->pub.encode_mcu = encode_mcu_DC_refine; 1.132 + else { 1.133 + entropy->pub.encode_mcu = encode_mcu_AC_refine; 1.134 + /* AC refinement needs a correction bit buffer */ 1.135 + if (entropy->bit_buffer == NULL) 1.136 + entropy->bit_buffer = (char *) 1.137 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.138 + MAX_CORR_BITS * SIZEOF(char)); 1.139 + } 1.140 + } 1.141 + if (gather_statistics) 1.142 + entropy->pub.finish_pass = finish_pass_gather_phuff; 1.143 + else 1.144 + entropy->pub.finish_pass = finish_pass_phuff; 1.145 + 1.146 + /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1 1.147 + * for AC coefficients. 1.148 + */ 1.149 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.150 + compptr = cinfo->cur_comp_info[ci]; 1.151 + /* Initialize DC predictions to 0 */ 1.152 + entropy->last_dc_val[ci] = 0; 1.153 + /* Get table index */ 1.154 + if (is_DC_band) { 1.155 + if (cinfo->Ah != 0) /* DC refinement needs no table */ 1.156 + continue; 1.157 + tbl = compptr->dc_tbl_no; 1.158 + } else { 1.159 + entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; 1.160 + } 1.161 + if (gather_statistics) { 1.162 + /* Check for invalid table index */ 1.163 + /* (make_c_derived_tbl does this in the other path) */ 1.164 + if (tbl < 0 || tbl >= NUM_HUFF_TBLS) 1.165 + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); 1.166 + /* Allocate and zero the statistics tables */ 1.167 + /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ 1.168 + if (entropy->count_ptrs[tbl] == NULL) 1.169 + entropy->count_ptrs[tbl] = (long *) 1.170 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.171 + 257 * SIZEOF(long)); 1.172 + MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long)); 1.173 + } else { 1.174 + /* Compute derived values for Huffman table */ 1.175 + /* We may do this more than once for a table, but it's not expensive */ 1.176 + jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, 1.177 + & entropy->derived_tbls[tbl]); 1.178 + } 1.179 + } 1.180 + 1.181 + /* Initialize AC stuff */ 1.182 + entropy->EOBRUN = 0; 1.183 + entropy->BE = 0; 1.184 + 1.185 + /* Initialize bit buffer to empty */ 1.186 + entropy->put_buffer = 0; 1.187 + entropy->put_bits = 0; 1.188 + 1.189 + /* Initialize restart stuff */ 1.190 + entropy->restarts_to_go = cinfo->restart_interval; 1.191 + entropy->next_restart_num = 0; 1.192 +} 1.193 + 1.194 + 1.195 +/* Outputting bytes to the file. 1.196 + * NB: these must be called only when actually outputting, 1.197 + * that is, entropy->gather_statistics == FALSE. 1.198 + */ 1.199 + 1.200 +/* Emit a byte */ 1.201 +#define emit_byte(entropy,val) \ 1.202 + { *(entropy)->next_output_byte++ = (JOCTET) (val); \ 1.203 + if (--(entropy)->free_in_buffer == 0) \ 1.204 + dump_buffer(entropy); } 1.205 + 1.206 + 1.207 +LOCAL(void) 1.208 +dump_buffer (phuff_entropy_ptr entropy) 1.209 +/* Empty the output buffer; we do not support suspension in this module. */ 1.210 +{ 1.211 + struct jpeg_destination_mgr * dest = entropy->cinfo->dest; 1.212 + 1.213 + if (! (*dest->empty_output_buffer) (entropy->cinfo)) 1.214 + ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); 1.215 + /* After a successful buffer dump, must reset buffer pointers */ 1.216 + entropy->next_output_byte = dest->next_output_byte; 1.217 + entropy->free_in_buffer = dest->free_in_buffer; 1.218 +} 1.219 + 1.220 + 1.221 +/* Outputting bits to the file */ 1.222 + 1.223 +/* Only the right 24 bits of put_buffer are used; the valid bits are 1.224 + * left-justified in this part. At most 16 bits can be passed to emit_bits 1.225 + * in one call, and we never retain more than 7 bits in put_buffer 1.226 + * between calls, so 24 bits are sufficient. 1.227 + */ 1.228 + 1.229 +LOCAL(void) 1.230 +emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size) 1.231 +/* Emit some bits, unless we are in gather mode */ 1.232 +{ 1.233 + /* This routine is heavily used, so it's worth coding tightly. */ 1.234 + register INT32 put_buffer = (INT32) code; 1.235 + register int put_bits = entropy->put_bits; 1.236 + 1.237 + /* if size is 0, caller used an invalid Huffman table entry */ 1.238 + if (size == 0) 1.239 + ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); 1.240 + 1.241 + if (entropy->gather_statistics) 1.242 + return; /* do nothing if we're only getting stats */ 1.243 + 1.244 + put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */ 1.245 + 1.246 + put_bits += size; /* new number of bits in buffer */ 1.247 + 1.248 + put_buffer <<= 24 - put_bits; /* align incoming bits */ 1.249 + 1.250 + put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */ 1.251 + 1.252 + while (put_bits >= 8) { 1.253 + int c = (int) ((put_buffer >> 16) & 0xFF); 1.254 + 1.255 + emit_byte(entropy, c); 1.256 + if (c == 0xFF) { /* need to stuff a zero byte? */ 1.257 + emit_byte(entropy, 0); 1.258 + } 1.259 + put_buffer <<= 8; 1.260 + put_bits -= 8; 1.261 + } 1.262 + 1.263 + entropy->put_buffer = put_buffer; /* update variables */ 1.264 + entropy->put_bits = put_bits; 1.265 +} 1.266 + 1.267 + 1.268 +LOCAL(void) 1.269 +flush_bits (phuff_entropy_ptr entropy) 1.270 +{ 1.271 + emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */ 1.272 + entropy->put_buffer = 0; /* and reset bit-buffer to empty */ 1.273 + entropy->put_bits = 0; 1.274 +} 1.275 + 1.276 + 1.277 +/* 1.278 + * Emit (or just count) a Huffman symbol. 1.279 + */ 1.280 + 1.281 +LOCAL(void) 1.282 +emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol) 1.283 +{ 1.284 + if (entropy->gather_statistics) 1.285 + entropy->count_ptrs[tbl_no][symbol]++; 1.286 + else { 1.287 + c_derived_tbl * tbl = entropy->derived_tbls[tbl_no]; 1.288 + emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); 1.289 + } 1.290 +} 1.291 + 1.292 + 1.293 +/* 1.294 + * Emit bits from a correction bit buffer. 1.295 + */ 1.296 + 1.297 +LOCAL(void) 1.298 +emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart, 1.299 + unsigned int nbits) 1.300 +{ 1.301 + if (entropy->gather_statistics) 1.302 + return; /* no real work */ 1.303 + 1.304 + while (nbits > 0) { 1.305 + emit_bits(entropy, (unsigned int) (*bufstart), 1); 1.306 + bufstart++; 1.307 + nbits--; 1.308 + } 1.309 +} 1.310 + 1.311 + 1.312 +/* 1.313 + * Emit any pending EOBRUN symbol. 1.314 + */ 1.315 + 1.316 +LOCAL(void) 1.317 +emit_eobrun (phuff_entropy_ptr entropy) 1.318 +{ 1.319 + register int temp, nbits; 1.320 + 1.321 + if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */ 1.322 + temp = entropy->EOBRUN; 1.323 + nbits = 0; 1.324 + while ((temp >>= 1)) 1.325 + nbits++; 1.326 + /* safety check: shouldn't happen given limited correction-bit buffer */ 1.327 + if (nbits > 14) 1.328 + ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); 1.329 + 1.330 + emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); 1.331 + if (nbits) 1.332 + emit_bits(entropy, entropy->EOBRUN, nbits); 1.333 + 1.334 + entropy->EOBRUN = 0; 1.335 + 1.336 + /* Emit any buffered correction bits */ 1.337 + emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE); 1.338 + entropy->BE = 0; 1.339 + } 1.340 +} 1.341 + 1.342 + 1.343 +/* 1.344 + * Emit a restart marker & resynchronize predictions. 1.345 + */ 1.346 + 1.347 +LOCAL(void) 1.348 +emit_restart (phuff_entropy_ptr entropy, int restart_num) 1.349 +{ 1.350 + int ci; 1.351 + 1.352 + emit_eobrun(entropy); 1.353 + 1.354 + if (! entropy->gather_statistics) { 1.355 + flush_bits(entropy); 1.356 + emit_byte(entropy, 0xFF); 1.357 + emit_byte(entropy, JPEG_RST0 + restart_num); 1.358 + } 1.359 + 1.360 + if (entropy->cinfo->Ss == 0) { 1.361 + /* Re-initialize DC predictions to 0 */ 1.362 + for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++) 1.363 + entropy->last_dc_val[ci] = 0; 1.364 + } else { 1.365 + /* Re-initialize all AC-related fields to 0 */ 1.366 + entropy->EOBRUN = 0; 1.367 + entropy->BE = 0; 1.368 + } 1.369 +} 1.370 + 1.371 + 1.372 +/* 1.373 + * MCU encoding for DC initial scan (either spectral selection, 1.374 + * or first pass of successive approximation). 1.375 + */ 1.376 + 1.377 +METHODDEF(boolean) 1.378 +encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 1.379 +{ 1.380 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.381 + register int temp, temp2; 1.382 + register int nbits; 1.383 + int blkn, ci; 1.384 + int Al = cinfo->Al; 1.385 + JBLOCKROW block; 1.386 + jpeg_component_info * compptr; 1.387 + ISHIFT_TEMPS 1.388 + 1.389 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.390 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.391 + 1.392 + /* Emit restart marker if needed */ 1.393 + if (cinfo->restart_interval) 1.394 + if (entropy->restarts_to_go == 0) 1.395 + emit_restart(entropy, entropy->next_restart_num); 1.396 + 1.397 + /* Encode the MCU data blocks */ 1.398 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 1.399 + block = MCU_data[blkn]; 1.400 + ci = cinfo->MCU_membership[blkn]; 1.401 + compptr = cinfo->cur_comp_info[ci]; 1.402 + 1.403 + /* Compute the DC value after the required point transform by Al. 1.404 + * This is simply an arithmetic right shift. 1.405 + */ 1.406 + temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); 1.407 + 1.408 + /* DC differences are figured on the point-transformed values. */ 1.409 + temp = temp2 - entropy->last_dc_val[ci]; 1.410 + entropy->last_dc_val[ci] = temp2; 1.411 + 1.412 + /* Encode the DC coefficient difference per section G.1.2.1 */ 1.413 + temp2 = temp; 1.414 + if (temp < 0) { 1.415 + temp = -temp; /* temp is abs value of input */ 1.416 + /* For a negative input, want temp2 = bitwise complement of abs(input) */ 1.417 + /* This code assumes we are on a two's complement machine */ 1.418 + temp2--; 1.419 + } 1.420 + 1.421 + /* Find the number of bits needed for the magnitude of the coefficient */ 1.422 + nbits = 0; 1.423 + while (temp) { 1.424 + nbits++; 1.425 + temp >>= 1; 1.426 + } 1.427 + /* Check for out-of-range coefficient values. 1.428 + * Since we're encoding a difference, the range limit is twice as much. 1.429 + */ 1.430 + if (nbits > MAX_COEF_BITS+1) 1.431 + ERREXIT(cinfo, JERR_BAD_DCT_COEF); 1.432 + 1.433 + /* Count/emit the Huffman-coded symbol for the number of bits */ 1.434 + emit_symbol(entropy, compptr->dc_tbl_no, nbits); 1.435 + 1.436 + /* Emit that number of bits of the value, if positive, */ 1.437 + /* or the complement of its magnitude, if negative. */ 1.438 + if (nbits) /* emit_bits rejects calls with size 0 */ 1.439 + emit_bits(entropy, (unsigned int) temp2, nbits); 1.440 + } 1.441 + 1.442 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.443 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.444 + 1.445 + /* Update restart-interval state too */ 1.446 + if (cinfo->restart_interval) { 1.447 + if (entropy->restarts_to_go == 0) { 1.448 + entropy->restarts_to_go = cinfo->restart_interval; 1.449 + entropy->next_restart_num++; 1.450 + entropy->next_restart_num &= 7; 1.451 + } 1.452 + entropy->restarts_to_go--; 1.453 + } 1.454 + 1.455 + return TRUE; 1.456 +} 1.457 + 1.458 + 1.459 +/* 1.460 + * MCU encoding for AC initial scan (either spectral selection, 1.461 + * or first pass of successive approximation). 1.462 + */ 1.463 + 1.464 +METHODDEF(boolean) 1.465 +encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 1.466 +{ 1.467 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.468 + register int temp, temp2; 1.469 + register int nbits; 1.470 + register int r, k; 1.471 + int Se = cinfo->Se; 1.472 + int Al = cinfo->Al; 1.473 + JBLOCKROW block; 1.474 + 1.475 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.476 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.477 + 1.478 + /* Emit restart marker if needed */ 1.479 + if (cinfo->restart_interval) 1.480 + if (entropy->restarts_to_go == 0) 1.481 + emit_restart(entropy, entropy->next_restart_num); 1.482 + 1.483 + /* Encode the MCU data block */ 1.484 + block = MCU_data[0]; 1.485 + 1.486 + /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ 1.487 + 1.488 + r = 0; /* r = run length of zeros */ 1.489 + 1.490 + for (k = cinfo->Ss; k <= Se; k++) { 1.491 + if ((temp = (*block)[jpeg_natural_order[k]]) == 0) { 1.492 + r++; 1.493 + continue; 1.494 + } 1.495 + /* We must apply the point transform by Al. For AC coefficients this 1.496 + * is an integer division with rounding towards 0. To do this portably 1.497 + * in C, we shift after obtaining the absolute value; so the code is 1.498 + * interwoven with finding the abs value (temp) and output bits (temp2). 1.499 + */ 1.500 + if (temp < 0) { 1.501 + temp = -temp; /* temp is abs value of input */ 1.502 + temp >>= Al; /* apply the point transform */ 1.503 + /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ 1.504 + temp2 = ~temp; 1.505 + } else { 1.506 + temp >>= Al; /* apply the point transform */ 1.507 + temp2 = temp; 1.508 + } 1.509 + /* Watch out for case that nonzero coef is zero after point transform */ 1.510 + if (temp == 0) { 1.511 + r++; 1.512 + continue; 1.513 + } 1.514 + 1.515 + /* Emit any pending EOBRUN */ 1.516 + if (entropy->EOBRUN > 0) 1.517 + emit_eobrun(entropy); 1.518 + /* if run length > 15, must emit special run-length-16 codes (0xF0) */ 1.519 + while (r > 15) { 1.520 + emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); 1.521 + r -= 16; 1.522 + } 1.523 + 1.524 + /* Find the number of bits needed for the magnitude of the coefficient */ 1.525 + nbits = 1; /* there must be at least one 1 bit */ 1.526 + while ((temp >>= 1)) 1.527 + nbits++; 1.528 + /* Check for out-of-range coefficient values */ 1.529 + if (nbits > MAX_COEF_BITS) 1.530 + ERREXIT(cinfo, JERR_BAD_DCT_COEF); 1.531 + 1.532 + /* Count/emit Huffman symbol for run length / number of bits */ 1.533 + emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); 1.534 + 1.535 + /* Emit that number of bits of the value, if positive, */ 1.536 + /* or the complement of its magnitude, if negative. */ 1.537 + emit_bits(entropy, (unsigned int) temp2, nbits); 1.538 + 1.539 + r = 0; /* reset zero run length */ 1.540 + } 1.541 + 1.542 + if (r > 0) { /* If there are trailing zeroes, */ 1.543 + entropy->EOBRUN++; /* count an EOB */ 1.544 + if (entropy->EOBRUN == 0x7FFF) 1.545 + emit_eobrun(entropy); /* force it out to avoid overflow */ 1.546 + } 1.547 + 1.548 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.549 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.550 + 1.551 + /* Update restart-interval state too */ 1.552 + if (cinfo->restart_interval) { 1.553 + if (entropy->restarts_to_go == 0) { 1.554 + entropy->restarts_to_go = cinfo->restart_interval; 1.555 + entropy->next_restart_num++; 1.556 + entropy->next_restart_num &= 7; 1.557 + } 1.558 + entropy->restarts_to_go--; 1.559 + } 1.560 + 1.561 + return TRUE; 1.562 +} 1.563 + 1.564 + 1.565 +/* 1.566 + * MCU encoding for DC successive approximation refinement scan. 1.567 + * Note: we assume such scans can be multi-component, although the spec 1.568 + * is not very clear on the point. 1.569 + */ 1.570 + 1.571 +METHODDEF(boolean) 1.572 +encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 1.573 +{ 1.574 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.575 + register int temp; 1.576 + int blkn; 1.577 + int Al = cinfo->Al; 1.578 + JBLOCKROW block; 1.579 + 1.580 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.581 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.582 + 1.583 + /* Emit restart marker if needed */ 1.584 + if (cinfo->restart_interval) 1.585 + if (entropy->restarts_to_go == 0) 1.586 + emit_restart(entropy, entropy->next_restart_num); 1.587 + 1.588 + /* Encode the MCU data blocks */ 1.589 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 1.590 + block = MCU_data[blkn]; 1.591 + 1.592 + /* We simply emit the Al'th bit of the DC coefficient value. */ 1.593 + temp = (*block)[0]; 1.594 + emit_bits(entropy, (unsigned int) (temp >> Al), 1); 1.595 + } 1.596 + 1.597 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.598 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.599 + 1.600 + /* Update restart-interval state too */ 1.601 + if (cinfo->restart_interval) { 1.602 + if (entropy->restarts_to_go == 0) { 1.603 + entropy->restarts_to_go = cinfo->restart_interval; 1.604 + entropy->next_restart_num++; 1.605 + entropy->next_restart_num &= 7; 1.606 + } 1.607 + entropy->restarts_to_go--; 1.608 + } 1.609 + 1.610 + return TRUE; 1.611 +} 1.612 + 1.613 + 1.614 +/* 1.615 + * MCU encoding for AC successive approximation refinement scan. 1.616 + */ 1.617 + 1.618 +METHODDEF(boolean) 1.619 +encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 1.620 +{ 1.621 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.622 + register int temp; 1.623 + register int r, k; 1.624 + int EOB; 1.625 + char *BR_buffer; 1.626 + unsigned int BR; 1.627 + int Se = cinfo->Se; 1.628 + int Al = cinfo->Al; 1.629 + JBLOCKROW block; 1.630 + int absvalues[DCTSIZE2]; 1.631 + 1.632 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.633 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.634 + 1.635 + /* Emit restart marker if needed */ 1.636 + if (cinfo->restart_interval) 1.637 + if (entropy->restarts_to_go == 0) 1.638 + emit_restart(entropy, entropy->next_restart_num); 1.639 + 1.640 + /* Encode the MCU data block */ 1.641 + block = MCU_data[0]; 1.642 + 1.643 + /* It is convenient to make a pre-pass to determine the transformed 1.644 + * coefficients' absolute values and the EOB position. 1.645 + */ 1.646 + EOB = 0; 1.647 + for (k = cinfo->Ss; k <= Se; k++) { 1.648 + temp = (*block)[jpeg_natural_order[k]]; 1.649 + /* We must apply the point transform by Al. For AC coefficients this 1.650 + * is an integer division with rounding towards 0. To do this portably 1.651 + * in C, we shift after obtaining the absolute value. 1.652 + */ 1.653 + if (temp < 0) 1.654 + temp = -temp; /* temp is abs value of input */ 1.655 + temp >>= Al; /* apply the point transform */ 1.656 + absvalues[k] = temp; /* save abs value for main pass */ 1.657 + if (temp == 1) 1.658 + EOB = k; /* EOB = index of last newly-nonzero coef */ 1.659 + } 1.660 + 1.661 + /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ 1.662 + 1.663 + r = 0; /* r = run length of zeros */ 1.664 + BR = 0; /* BR = count of buffered bits added now */ 1.665 + BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ 1.666 + 1.667 + for (k = cinfo->Ss; k <= Se; k++) { 1.668 + if ((temp = absvalues[k]) == 0) { 1.669 + r++; 1.670 + continue; 1.671 + } 1.672 + 1.673 + /* Emit any required ZRLs, but not if they can be folded into EOB */ 1.674 + while (r > 15 && k <= EOB) { 1.675 + /* emit any pending EOBRUN and the BE correction bits */ 1.676 + emit_eobrun(entropy); 1.677 + /* Emit ZRL */ 1.678 + emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); 1.679 + r -= 16; 1.680 + /* Emit buffered correction bits that must be associated with ZRL */ 1.681 + emit_buffered_bits(entropy, BR_buffer, BR); 1.682 + BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ 1.683 + BR = 0; 1.684 + } 1.685 + 1.686 + /* If the coef was previously nonzero, it only needs a correction bit. 1.687 + * NOTE: a straight translation of the spec's figure G.7 would suggest 1.688 + * that we also need to test r > 15. But if r > 15, we can only get here 1.689 + * if k > EOB, which implies that this coefficient is not 1. 1.690 + */ 1.691 + if (temp > 1) { 1.692 + /* The correction bit is the next bit of the absolute value. */ 1.693 + BR_buffer[BR++] = (char) (temp & 1); 1.694 + continue; 1.695 + } 1.696 + 1.697 + /* Emit any pending EOBRUN and the BE correction bits */ 1.698 + emit_eobrun(entropy); 1.699 + 1.700 + /* Count/emit Huffman symbol for run length / number of bits */ 1.701 + emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); 1.702 + 1.703 + /* Emit output bit for newly-nonzero coef */ 1.704 + temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1; 1.705 + emit_bits(entropy, (unsigned int) temp, 1); 1.706 + 1.707 + /* Emit buffered correction bits that must be associated with this code */ 1.708 + emit_buffered_bits(entropy, BR_buffer, BR); 1.709 + BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ 1.710 + BR = 0; 1.711 + r = 0; /* reset zero run length */ 1.712 + } 1.713 + 1.714 + if (r > 0 || BR > 0) { /* If there are trailing zeroes, */ 1.715 + entropy->EOBRUN++; /* count an EOB */ 1.716 + entropy->BE += BR; /* concat my correction bits to older ones */ 1.717 + /* We force out the EOB if we risk either: 1.718 + * 1. overflow of the EOB counter; 1.719 + * 2. overflow of the correction bit buffer during the next MCU. 1.720 + */ 1.721 + if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) 1.722 + emit_eobrun(entropy); 1.723 + } 1.724 + 1.725 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.726 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.727 + 1.728 + /* Update restart-interval state too */ 1.729 + if (cinfo->restart_interval) { 1.730 + if (entropy->restarts_to_go == 0) { 1.731 + entropy->restarts_to_go = cinfo->restart_interval; 1.732 + entropy->next_restart_num++; 1.733 + entropy->next_restart_num &= 7; 1.734 + } 1.735 + entropy->restarts_to_go--; 1.736 + } 1.737 + 1.738 + return TRUE; 1.739 +} 1.740 + 1.741 + 1.742 +/* 1.743 + * Finish up at the end of a Huffman-compressed progressive scan. 1.744 + */ 1.745 + 1.746 +METHODDEF(void) 1.747 +finish_pass_phuff (j_compress_ptr cinfo) 1.748 +{ 1.749 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.750 + 1.751 + entropy->next_output_byte = cinfo->dest->next_output_byte; 1.752 + entropy->free_in_buffer = cinfo->dest->free_in_buffer; 1.753 + 1.754 + /* Flush out any buffered data */ 1.755 + emit_eobrun(entropy); 1.756 + flush_bits(entropy); 1.757 + 1.758 + cinfo->dest->next_output_byte = entropy->next_output_byte; 1.759 + cinfo->dest->free_in_buffer = entropy->free_in_buffer; 1.760 +} 1.761 + 1.762 + 1.763 +/* 1.764 + * Finish up a statistics-gathering pass and create the new Huffman tables. 1.765 + */ 1.766 + 1.767 +METHODDEF(void) 1.768 +finish_pass_gather_phuff (j_compress_ptr cinfo) 1.769 +{ 1.770 + phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; 1.771 + boolean is_DC_band; 1.772 + int ci, tbl; 1.773 + jpeg_component_info * compptr; 1.774 + JHUFF_TBL **htblptr; 1.775 + boolean did[NUM_HUFF_TBLS]; 1.776 + 1.777 + /* Flush out buffered data (all we care about is counting the EOB symbol) */ 1.778 + emit_eobrun(entropy); 1.779 + 1.780 + is_DC_band = (cinfo->Ss == 0); 1.781 + 1.782 + /* It's important not to apply jpeg_gen_optimal_table more than once 1.783 + * per table, because it clobbers the input frequency counts! 1.784 + */ 1.785 + MEMZERO(did, SIZEOF(did)); 1.786 + 1.787 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.788 + compptr = cinfo->cur_comp_info[ci]; 1.789 + if (is_DC_band) { 1.790 + if (cinfo->Ah != 0) /* DC refinement needs no table */ 1.791 + continue; 1.792 + tbl = compptr->dc_tbl_no; 1.793 + } else { 1.794 + tbl = compptr->ac_tbl_no; 1.795 + } 1.796 + if (! did[tbl]) { 1.797 + if (is_DC_band) 1.798 + htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; 1.799 + else 1.800 + htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; 1.801 + if (*htblptr == NULL) 1.802 + *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); 1.803 + jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]); 1.804 + did[tbl] = TRUE; 1.805 + } 1.806 + } 1.807 +} 1.808 + 1.809 + 1.810 +/* 1.811 + * Module initialization routine for progressive Huffman entropy encoding. 1.812 + */ 1.813 + 1.814 +GLOBAL(void) 1.815 +jinit_phuff_encoder (j_compress_ptr cinfo) 1.816 +{ 1.817 + phuff_entropy_ptr entropy; 1.818 + int i; 1.819 + 1.820 + entropy = (phuff_entropy_ptr) 1.821 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.822 + SIZEOF(phuff_entropy_encoder)); 1.823 + cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; 1.824 + entropy->pub.start_pass = start_pass_phuff; 1.825 + 1.826 + /* Mark tables unallocated */ 1.827 + for (i = 0; i < NUM_HUFF_TBLS; i++) { 1.828 + entropy->derived_tbls[i] = NULL; 1.829 + entropy->count_ptrs[i] = NULL; 1.830 + } 1.831 + entropy->bit_buffer = NULL; /* needed only in AC refinement scan */ 1.832 +} 1.833 + 1.834 +#endif /* C_PROGRESSIVE_SUPPORTED */