nuclear@26: /* nuclear@26: * jcphuff.c nuclear@26: * nuclear@26: * Copyright (C) 1995-1997, Thomas G. Lane. nuclear@26: * This file is part of the Independent JPEG Group's software. nuclear@26: * For conditions of distribution and use, see the accompanying README file. nuclear@26: * nuclear@26: * This file contains Huffman entropy encoding routines for progressive JPEG. nuclear@26: * nuclear@26: * We do not support output suspension in this module, since the library nuclear@26: * currently does not allow multiple-scan files to be written with output nuclear@26: * suspension. nuclear@26: */ nuclear@26: nuclear@26: #define JPEG_INTERNALS nuclear@26: #include "jinclude.h" nuclear@26: #include "jpeglib.h" nuclear@26: #include "jchuff.h" /* Declarations shared with jchuff.c */ nuclear@26: nuclear@26: #ifdef C_PROGRESSIVE_SUPPORTED nuclear@26: nuclear@26: /* Expanded entropy encoder object for progressive Huffman encoding. */ nuclear@26: nuclear@26: typedef struct { nuclear@26: struct jpeg_entropy_encoder pub; /* public fields */ nuclear@26: nuclear@26: /* Mode flag: TRUE for optimization, FALSE for actual data output */ nuclear@26: boolean gather_statistics; nuclear@26: nuclear@26: /* Bit-level coding status. nuclear@26: * next_output_byte/free_in_buffer are local copies of cinfo->dest fields. nuclear@26: */ nuclear@26: JOCTET * next_output_byte; /* => next byte to write in buffer */ nuclear@26: size_t free_in_buffer; /* # of byte spaces remaining in buffer */ nuclear@26: INT32 put_buffer; /* current bit-accumulation buffer */ nuclear@26: int put_bits; /* # of bits now in it */ nuclear@26: j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */ nuclear@26: nuclear@26: /* Coding status for DC components */ nuclear@26: int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ nuclear@26: nuclear@26: /* Coding status for AC components */ nuclear@26: int ac_tbl_no; /* the table number of the single component */ nuclear@26: unsigned int EOBRUN; /* run length of EOBs */ nuclear@26: unsigned int BE; /* # of buffered correction bits before MCU */ nuclear@26: char * bit_buffer; /* buffer for correction bits (1 per char) */ nuclear@26: /* packing correction bits tightly would save some space but cost time... */ nuclear@26: nuclear@26: unsigned int restarts_to_go; /* MCUs left in this restart interval */ nuclear@26: int next_restart_num; /* next restart number to write (0-7) */ nuclear@26: nuclear@26: /* Pointers to derived tables (these workspaces have image lifespan). nuclear@26: * Since any one scan codes only DC or only AC, we only need one set nuclear@26: * of tables, not one for DC and one for AC. nuclear@26: */ nuclear@26: c_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; nuclear@26: nuclear@26: /* Statistics tables for optimization; again, one set is enough */ nuclear@26: long * count_ptrs[NUM_HUFF_TBLS]; nuclear@26: } phuff_entropy_encoder; nuclear@26: nuclear@26: typedef phuff_entropy_encoder * phuff_entropy_ptr; nuclear@26: nuclear@26: /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit nuclear@26: * buffer can hold. Larger sizes may slightly improve compression, but nuclear@26: * 1000 is already well into the realm of overkill. nuclear@26: * The minimum safe size is 64 bits. nuclear@26: */ nuclear@26: nuclear@26: #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */ nuclear@26: nuclear@26: /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. nuclear@26: * We assume that int right shift is unsigned if INT32 right shift is, nuclear@26: * which should be safe. nuclear@26: */ nuclear@26: nuclear@26: #ifdef RIGHT_SHIFT_IS_UNSIGNED nuclear@26: #define ISHIFT_TEMPS int ishift_temp; nuclear@26: #define IRIGHT_SHIFT(x,shft) \ nuclear@26: ((ishift_temp = (x)) < 0 ? \ nuclear@26: (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ nuclear@26: (ishift_temp >> (shft))) nuclear@26: #else nuclear@26: #define ISHIFT_TEMPS nuclear@26: #define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) nuclear@26: #endif nuclear@26: nuclear@26: /* Forward declarations */ nuclear@26: METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo, nuclear@26: JBLOCKROW *MCU_data)); nuclear@26: METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo, nuclear@26: JBLOCKROW *MCU_data)); nuclear@26: METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo, nuclear@26: JBLOCKROW *MCU_data)); nuclear@26: METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo, nuclear@26: JBLOCKROW *MCU_data)); nuclear@26: METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo)); nuclear@26: METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo)); nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * Initialize for a Huffman-compressed scan using progressive JPEG. nuclear@26: */ nuclear@26: nuclear@26: METHODDEF(void) nuclear@26: start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics) nuclear@26: { nuclear@26: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; nuclear@26: boolean is_DC_band; nuclear@26: int ci, tbl; nuclear@26: jpeg_component_info * compptr; nuclear@26: nuclear@26: entropy->cinfo = cinfo; nuclear@26: entropy->gather_statistics = gather_statistics; nuclear@26: nuclear@26: is_DC_band = (cinfo->Ss == 0); nuclear@26: nuclear@26: /* We assume jcmaster.c already validated the scan parameters. */ nuclear@26: nuclear@26: /* Select execution routines */ nuclear@26: if (cinfo->Ah == 0) { nuclear@26: if (is_DC_band) nuclear@26: entropy->pub.encode_mcu = encode_mcu_DC_first; nuclear@26: else nuclear@26: entropy->pub.encode_mcu = encode_mcu_AC_first; nuclear@26: } else { nuclear@26: if (is_DC_band) nuclear@26: entropy->pub.encode_mcu = encode_mcu_DC_refine; nuclear@26: else { nuclear@26: entropy->pub.encode_mcu = encode_mcu_AC_refine; nuclear@26: /* AC refinement needs a correction bit buffer */ nuclear@26: if (entropy->bit_buffer == NULL) nuclear@26: entropy->bit_buffer = (char *) nuclear@26: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@26: MAX_CORR_BITS * SIZEOF(char)); nuclear@26: } nuclear@26: } nuclear@26: if (gather_statistics) nuclear@26: entropy->pub.finish_pass = finish_pass_gather_phuff; nuclear@26: else nuclear@26: entropy->pub.finish_pass = finish_pass_phuff; nuclear@26: nuclear@26: /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1 nuclear@26: * for AC coefficients. nuclear@26: */ nuclear@26: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { nuclear@26: compptr = cinfo->cur_comp_info[ci]; nuclear@26: /* Initialize DC predictions to 0 */ nuclear@26: entropy->last_dc_val[ci] = 0; nuclear@26: /* Get table index */ nuclear@26: if (is_DC_band) { nuclear@26: if (cinfo->Ah != 0) /* DC refinement needs no table */ nuclear@26: continue; nuclear@26: tbl = compptr->dc_tbl_no; nuclear@26: } else { nuclear@26: entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; nuclear@26: } nuclear@26: if (gather_statistics) { nuclear@26: /* Check for invalid table index */ nuclear@26: /* (make_c_derived_tbl does this in the other path) */ nuclear@26: if (tbl < 0 || tbl >= NUM_HUFF_TBLS) nuclear@26: ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); nuclear@26: /* Allocate and zero the statistics tables */ nuclear@26: /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ nuclear@26: if (entropy->count_ptrs[tbl] == NULL) nuclear@26: entropy->count_ptrs[tbl] = (long *) nuclear@26: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@26: 257 * SIZEOF(long)); nuclear@26: MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long)); nuclear@26: } else { nuclear@26: /* Compute derived values for Huffman table */ nuclear@26: /* We may do this more than once for a table, but it's not expensive */ nuclear@26: jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, nuclear@26: & entropy->derived_tbls[tbl]); nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: /* Initialize AC stuff */ nuclear@26: entropy->EOBRUN = 0; nuclear@26: entropy->BE = 0; nuclear@26: nuclear@26: /* Initialize bit buffer to empty */ nuclear@26: entropy->put_buffer = 0; nuclear@26: entropy->put_bits = 0; nuclear@26: nuclear@26: /* Initialize restart stuff */ nuclear@26: entropy->restarts_to_go = cinfo->restart_interval; nuclear@26: entropy->next_restart_num = 0; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* Outputting bytes to the file. nuclear@26: * NB: these must be called only when actually outputting, nuclear@26: * that is, entropy->gather_statistics == FALSE. nuclear@26: */ nuclear@26: nuclear@26: /* Emit a byte */ nuclear@26: #define emit_byte(entropy,val) \ nuclear@26: { *(entropy)->next_output_byte++ = (JOCTET) (val); \ nuclear@26: if (--(entropy)->free_in_buffer == 0) \ nuclear@26: dump_buffer(entropy); } nuclear@26: nuclear@26: nuclear@26: LOCAL(void) nuclear@26: dump_buffer (phuff_entropy_ptr entropy) nuclear@26: /* Empty the output buffer; we do not support suspension in this module. */ nuclear@26: { nuclear@26: struct jpeg_destination_mgr * dest = entropy->cinfo->dest; nuclear@26: nuclear@26: if (! (*dest->empty_output_buffer) (entropy->cinfo)) nuclear@26: ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); nuclear@26: /* After a successful buffer dump, must reset buffer pointers */ nuclear@26: entropy->next_output_byte = dest->next_output_byte; nuclear@26: entropy->free_in_buffer = dest->free_in_buffer; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* Outputting bits to the file */ nuclear@26: nuclear@26: /* Only the right 24 bits of put_buffer are used; the valid bits are nuclear@26: * left-justified in this part. At most 16 bits can be passed to emit_bits nuclear@26: * in one call, and we never retain more than 7 bits in put_buffer nuclear@26: * between calls, so 24 bits are sufficient. nuclear@26: */ nuclear@26: nuclear@26: INLINE nuclear@26: LOCAL(void) nuclear@26: emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size) nuclear@26: /* Emit some bits, unless we are in gather mode */ nuclear@26: { nuclear@26: /* This routine is heavily used, so it's worth coding tightly. */ nuclear@26: register INT32 put_buffer = (INT32) code; nuclear@26: register int put_bits = entropy->put_bits; nuclear@26: nuclear@26: /* if size is 0, caller used an invalid Huffman table entry */ nuclear@26: if (size == 0) nuclear@26: ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); nuclear@26: nuclear@26: if (entropy->gather_statistics) nuclear@26: return; /* do nothing if we're only getting stats */ nuclear@26: nuclear@26: put_buffer &= (((INT32) 1)<put_buffer; /* and merge with old buffer contents */ nuclear@26: nuclear@26: while (put_bits >= 8) { nuclear@26: int c = (int) ((put_buffer >> 16) & 0xFF); nuclear@26: nuclear@26: emit_byte(entropy, c); nuclear@26: if (c == 0xFF) { /* need to stuff a zero byte? */ nuclear@26: emit_byte(entropy, 0); nuclear@26: } nuclear@26: put_buffer <<= 8; nuclear@26: put_bits -= 8; nuclear@26: } nuclear@26: nuclear@26: entropy->put_buffer = put_buffer; /* update variables */ nuclear@26: entropy->put_bits = put_bits; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: LOCAL(void) nuclear@26: flush_bits (phuff_entropy_ptr entropy) nuclear@26: { nuclear@26: emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */ nuclear@26: entropy->put_buffer = 0; /* and reset bit-buffer to empty */ nuclear@26: entropy->put_bits = 0; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * Emit (or just count) a Huffman symbol. nuclear@26: */ nuclear@26: nuclear@26: INLINE nuclear@26: LOCAL(void) nuclear@26: emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol) nuclear@26: { nuclear@26: if (entropy->gather_statistics) nuclear@26: entropy->count_ptrs[tbl_no][symbol]++; nuclear@26: else { nuclear@26: c_derived_tbl * tbl = entropy->derived_tbls[tbl_no]; nuclear@26: emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * Emit bits from a correction bit buffer. nuclear@26: */ nuclear@26: nuclear@26: LOCAL(void) nuclear@26: emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart, nuclear@26: unsigned int nbits) nuclear@26: { nuclear@26: if (entropy->gather_statistics) nuclear@26: return; /* no real work */ nuclear@26: nuclear@26: while (nbits > 0) { nuclear@26: emit_bits(entropy, (unsigned int) (*bufstart), 1); nuclear@26: bufstart++; nuclear@26: nbits--; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * Emit any pending EOBRUN symbol. nuclear@26: */ nuclear@26: nuclear@26: LOCAL(void) nuclear@26: emit_eobrun (phuff_entropy_ptr entropy) nuclear@26: { nuclear@26: register int temp, nbits; nuclear@26: nuclear@26: if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */ nuclear@26: temp = entropy->EOBRUN; nuclear@26: nbits = 0; nuclear@26: while ((temp >>= 1)) nuclear@26: nbits++; nuclear@26: /* safety check: shouldn't happen given limited correction-bit buffer */ nuclear@26: if (nbits > 14) nuclear@26: ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); nuclear@26: nuclear@26: emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); nuclear@26: if (nbits) nuclear@26: emit_bits(entropy, entropy->EOBRUN, nbits); nuclear@26: nuclear@26: entropy->EOBRUN = 0; nuclear@26: nuclear@26: /* Emit any buffered correction bits */ nuclear@26: emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE); nuclear@26: entropy->BE = 0; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * Emit a restart marker & resynchronize predictions. nuclear@26: */ nuclear@26: nuclear@26: LOCAL(void) nuclear@26: emit_restart (phuff_entropy_ptr entropy, int restart_num) nuclear@26: { nuclear@26: int ci; nuclear@26: nuclear@26: emit_eobrun(entropy); nuclear@26: nuclear@26: if (! entropy->gather_statistics) { nuclear@26: flush_bits(entropy); nuclear@26: emit_byte(entropy, 0xFF); nuclear@26: emit_byte(entropy, JPEG_RST0 + restart_num); nuclear@26: } nuclear@26: nuclear@26: if (entropy->cinfo->Ss == 0) { nuclear@26: /* Re-initialize DC predictions to 0 */ nuclear@26: for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++) nuclear@26: entropy->last_dc_val[ci] = 0; nuclear@26: } else { nuclear@26: /* Re-initialize all AC-related fields to 0 */ nuclear@26: entropy->EOBRUN = 0; nuclear@26: entropy->BE = 0; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * MCU encoding for DC initial scan (either spectral selection, nuclear@26: * or first pass of successive approximation). nuclear@26: */ nuclear@26: nuclear@26: METHODDEF(boolean) nuclear@26: encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) nuclear@26: { nuclear@26: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; nuclear@26: register int temp, temp2; nuclear@26: register int nbits; nuclear@26: int blkn, ci; nuclear@26: int Al = cinfo->Al; nuclear@26: JBLOCKROW block; nuclear@26: jpeg_component_info * compptr; nuclear@26: ISHIFT_TEMPS nuclear@26: nuclear@26: entropy->next_output_byte = cinfo->dest->next_output_byte; nuclear@26: entropy->free_in_buffer = cinfo->dest->free_in_buffer; nuclear@26: nuclear@26: /* Emit restart marker if needed */ nuclear@26: if (cinfo->restart_interval) nuclear@26: if (entropy->restarts_to_go == 0) nuclear@26: emit_restart(entropy, entropy->next_restart_num); nuclear@26: nuclear@26: /* Encode the MCU data blocks */ nuclear@26: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { nuclear@26: block = MCU_data[blkn]; nuclear@26: ci = cinfo->MCU_membership[blkn]; nuclear@26: compptr = cinfo->cur_comp_info[ci]; nuclear@26: nuclear@26: /* Compute the DC value after the required point transform by Al. nuclear@26: * This is simply an arithmetic right shift. nuclear@26: */ nuclear@26: temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); nuclear@26: nuclear@26: /* DC differences are figured on the point-transformed values. */ nuclear@26: temp = temp2 - entropy->last_dc_val[ci]; nuclear@26: entropy->last_dc_val[ci] = temp2; nuclear@26: nuclear@26: /* Encode the DC coefficient difference per section G.1.2.1 */ nuclear@26: temp2 = temp; nuclear@26: if (temp < 0) { nuclear@26: temp = -temp; /* temp is abs value of input */ nuclear@26: /* For a negative input, want temp2 = bitwise complement of abs(input) */ nuclear@26: /* This code assumes we are on a two's complement machine */ nuclear@26: temp2--; nuclear@26: } nuclear@26: nuclear@26: /* Find the number of bits needed for the magnitude of the coefficient */ nuclear@26: nbits = 0; nuclear@26: while (temp) { nuclear@26: nbits++; nuclear@26: temp >>= 1; nuclear@26: } nuclear@26: /* Check for out-of-range coefficient values. nuclear@26: * Since we're encoding a difference, the range limit is twice as much. nuclear@26: */ nuclear@26: if (nbits > MAX_COEF_BITS+1) nuclear@26: ERREXIT(cinfo, JERR_BAD_DCT_COEF); nuclear@26: nuclear@26: /* Count/emit the Huffman-coded symbol for the number of bits */ nuclear@26: emit_symbol(entropy, compptr->dc_tbl_no, nbits); nuclear@26: nuclear@26: /* Emit that number of bits of the value, if positive, */ nuclear@26: /* or the complement of its magnitude, if negative. */ nuclear@26: if (nbits) /* emit_bits rejects calls with size 0 */ nuclear@26: emit_bits(entropy, (unsigned int) temp2, nbits); nuclear@26: } nuclear@26: nuclear@26: cinfo->dest->next_output_byte = entropy->next_output_byte; nuclear@26: cinfo->dest->free_in_buffer = entropy->free_in_buffer; nuclear@26: nuclear@26: /* Update restart-interval state too */ nuclear@26: if (cinfo->restart_interval) { nuclear@26: if (entropy->restarts_to_go == 0) { nuclear@26: entropy->restarts_to_go = cinfo->restart_interval; nuclear@26: entropy->next_restart_num++; nuclear@26: entropy->next_restart_num &= 7; nuclear@26: } nuclear@26: entropy->restarts_to_go--; nuclear@26: } nuclear@26: nuclear@26: return TRUE; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * MCU encoding for AC initial scan (either spectral selection, nuclear@26: * or first pass of successive approximation). nuclear@26: */ nuclear@26: nuclear@26: METHODDEF(boolean) nuclear@26: encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) nuclear@26: { nuclear@26: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; nuclear@26: register int temp, temp2; nuclear@26: register int nbits; nuclear@26: register int r, k; nuclear@26: int Se = cinfo->Se; nuclear@26: int Al = cinfo->Al; nuclear@26: JBLOCKROW block; nuclear@26: nuclear@26: entropy->next_output_byte = cinfo->dest->next_output_byte; nuclear@26: entropy->free_in_buffer = cinfo->dest->free_in_buffer; nuclear@26: nuclear@26: /* Emit restart marker if needed */ nuclear@26: if (cinfo->restart_interval) nuclear@26: if (entropy->restarts_to_go == 0) nuclear@26: emit_restart(entropy, entropy->next_restart_num); nuclear@26: nuclear@26: /* Encode the MCU data block */ nuclear@26: block = MCU_data[0]; nuclear@26: nuclear@26: /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ nuclear@26: nuclear@26: r = 0; /* r = run length of zeros */ nuclear@26: nuclear@26: for (k = cinfo->Ss; k <= Se; k++) { nuclear@26: if ((temp = (*block)[jpeg_natural_order[k]]) == 0) { nuclear@26: r++; nuclear@26: continue; nuclear@26: } nuclear@26: /* We must apply the point transform by Al. For AC coefficients this nuclear@26: * is an integer division with rounding towards 0. To do this portably nuclear@26: * in C, we shift after obtaining the absolute value; so the code is nuclear@26: * interwoven with finding the abs value (temp) and output bits (temp2). nuclear@26: */ nuclear@26: if (temp < 0) { nuclear@26: temp = -temp; /* temp is abs value of input */ nuclear@26: temp >>= Al; /* apply the point transform */ nuclear@26: /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ nuclear@26: temp2 = ~temp; nuclear@26: } else { nuclear@26: temp >>= Al; /* apply the point transform */ nuclear@26: temp2 = temp; nuclear@26: } nuclear@26: /* Watch out for case that nonzero coef is zero after point transform */ nuclear@26: if (temp == 0) { nuclear@26: r++; nuclear@26: continue; nuclear@26: } nuclear@26: nuclear@26: /* Emit any pending EOBRUN */ nuclear@26: if (entropy->EOBRUN > 0) nuclear@26: emit_eobrun(entropy); nuclear@26: /* if run length > 15, must emit special run-length-16 codes (0xF0) */ nuclear@26: while (r > 15) { nuclear@26: emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); nuclear@26: r -= 16; nuclear@26: } nuclear@26: nuclear@26: /* Find the number of bits needed for the magnitude of the coefficient */ nuclear@26: nbits = 1; /* there must be at least one 1 bit */ nuclear@26: while ((temp >>= 1)) nuclear@26: nbits++; nuclear@26: /* Check for out-of-range coefficient values */ nuclear@26: if (nbits > MAX_COEF_BITS) nuclear@26: ERREXIT(cinfo, JERR_BAD_DCT_COEF); nuclear@26: nuclear@26: /* Count/emit Huffman symbol for run length / number of bits */ nuclear@26: emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); nuclear@26: nuclear@26: /* Emit that number of bits of the value, if positive, */ nuclear@26: /* or the complement of its magnitude, if negative. */ nuclear@26: emit_bits(entropy, (unsigned int) temp2, nbits); nuclear@26: nuclear@26: r = 0; /* reset zero run length */ nuclear@26: } nuclear@26: nuclear@26: if (r > 0) { /* If there are trailing zeroes, */ nuclear@26: entropy->EOBRUN++; /* count an EOB */ nuclear@26: if (entropy->EOBRUN == 0x7FFF) nuclear@26: emit_eobrun(entropy); /* force it out to avoid overflow */ nuclear@26: } nuclear@26: nuclear@26: cinfo->dest->next_output_byte = entropy->next_output_byte; nuclear@26: cinfo->dest->free_in_buffer = entropy->free_in_buffer; nuclear@26: nuclear@26: /* Update restart-interval state too */ nuclear@26: if (cinfo->restart_interval) { nuclear@26: if (entropy->restarts_to_go == 0) { nuclear@26: entropy->restarts_to_go = cinfo->restart_interval; nuclear@26: entropy->next_restart_num++; nuclear@26: entropy->next_restart_num &= 7; nuclear@26: } nuclear@26: entropy->restarts_to_go--; nuclear@26: } nuclear@26: nuclear@26: return TRUE; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * MCU encoding for DC successive approximation refinement scan. nuclear@26: * Note: we assume such scans can be multi-component, although the spec nuclear@26: * is not very clear on the point. nuclear@26: */ nuclear@26: nuclear@26: METHODDEF(boolean) nuclear@26: encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) nuclear@26: { nuclear@26: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; nuclear@26: register int temp; nuclear@26: int blkn; nuclear@26: int Al = cinfo->Al; nuclear@26: JBLOCKROW block; nuclear@26: nuclear@26: entropy->next_output_byte = cinfo->dest->next_output_byte; nuclear@26: entropy->free_in_buffer = cinfo->dest->free_in_buffer; nuclear@26: nuclear@26: /* Emit restart marker if needed */ nuclear@26: if (cinfo->restart_interval) nuclear@26: if (entropy->restarts_to_go == 0) nuclear@26: emit_restart(entropy, entropy->next_restart_num); nuclear@26: nuclear@26: /* Encode the MCU data blocks */ nuclear@26: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { nuclear@26: block = MCU_data[blkn]; nuclear@26: nuclear@26: /* We simply emit the Al'th bit of the DC coefficient value. */ nuclear@26: temp = (*block)[0]; nuclear@26: emit_bits(entropy, (unsigned int) (temp >> Al), 1); nuclear@26: } nuclear@26: nuclear@26: cinfo->dest->next_output_byte = entropy->next_output_byte; nuclear@26: cinfo->dest->free_in_buffer = entropy->free_in_buffer; nuclear@26: nuclear@26: /* Update restart-interval state too */ nuclear@26: if (cinfo->restart_interval) { nuclear@26: if (entropy->restarts_to_go == 0) { nuclear@26: entropy->restarts_to_go = cinfo->restart_interval; nuclear@26: entropy->next_restart_num++; nuclear@26: entropy->next_restart_num &= 7; nuclear@26: } nuclear@26: entropy->restarts_to_go--; nuclear@26: } nuclear@26: nuclear@26: return TRUE; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * MCU encoding for AC successive approximation refinement scan. nuclear@26: */ nuclear@26: nuclear@26: METHODDEF(boolean) nuclear@26: encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) nuclear@26: { nuclear@26: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; nuclear@26: register int temp; nuclear@26: register int r, k; nuclear@26: int EOB; nuclear@26: char *BR_buffer; nuclear@26: unsigned int BR; nuclear@26: int Se = cinfo->Se; nuclear@26: int Al = cinfo->Al; nuclear@26: JBLOCKROW block; nuclear@26: int absvalues[DCTSIZE2]; nuclear@26: nuclear@26: entropy->next_output_byte = cinfo->dest->next_output_byte; nuclear@26: entropy->free_in_buffer = cinfo->dest->free_in_buffer; nuclear@26: nuclear@26: /* Emit restart marker if needed */ nuclear@26: if (cinfo->restart_interval) nuclear@26: if (entropy->restarts_to_go == 0) nuclear@26: emit_restart(entropy, entropy->next_restart_num); nuclear@26: nuclear@26: /* Encode the MCU data block */ nuclear@26: block = MCU_data[0]; nuclear@26: nuclear@26: /* It is convenient to make a pre-pass to determine the transformed nuclear@26: * coefficients' absolute values and the EOB position. nuclear@26: */ nuclear@26: EOB = 0; nuclear@26: for (k = cinfo->Ss; k <= Se; k++) { nuclear@26: temp = (*block)[jpeg_natural_order[k]]; nuclear@26: /* We must apply the point transform by Al. For AC coefficients this nuclear@26: * is an integer division with rounding towards 0. To do this portably nuclear@26: * in C, we shift after obtaining the absolute value. nuclear@26: */ nuclear@26: if (temp < 0) nuclear@26: temp = -temp; /* temp is abs value of input */ nuclear@26: temp >>= Al; /* apply the point transform */ nuclear@26: absvalues[k] = temp; /* save abs value for main pass */ nuclear@26: if (temp == 1) nuclear@26: EOB = k; /* EOB = index of last newly-nonzero coef */ nuclear@26: } nuclear@26: nuclear@26: /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ nuclear@26: nuclear@26: r = 0; /* r = run length of zeros */ nuclear@26: BR = 0; /* BR = count of buffered bits added now */ nuclear@26: BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ nuclear@26: nuclear@26: for (k = cinfo->Ss; k <= Se; k++) { nuclear@26: if ((temp = absvalues[k]) == 0) { nuclear@26: r++; nuclear@26: continue; nuclear@26: } nuclear@26: nuclear@26: /* Emit any required ZRLs, but not if they can be folded into EOB */ nuclear@26: while (r > 15 && k <= EOB) { nuclear@26: /* emit any pending EOBRUN and the BE correction bits */ nuclear@26: emit_eobrun(entropy); nuclear@26: /* Emit ZRL */ nuclear@26: emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); nuclear@26: r -= 16; nuclear@26: /* Emit buffered correction bits that must be associated with ZRL */ nuclear@26: emit_buffered_bits(entropy, BR_buffer, BR); nuclear@26: BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ nuclear@26: BR = 0; nuclear@26: } nuclear@26: nuclear@26: /* If the coef was previously nonzero, it only needs a correction bit. nuclear@26: * NOTE: a straight translation of the spec's figure G.7 would suggest nuclear@26: * that we also need to test r > 15. But if r > 15, we can only get here nuclear@26: * if k > EOB, which implies that this coefficient is not 1. nuclear@26: */ nuclear@26: if (temp > 1) { nuclear@26: /* The correction bit is the next bit of the absolute value. */ nuclear@26: BR_buffer[BR++] = (char) (temp & 1); nuclear@26: continue; nuclear@26: } nuclear@26: nuclear@26: /* Emit any pending EOBRUN and the BE correction bits */ nuclear@26: emit_eobrun(entropy); nuclear@26: nuclear@26: /* Count/emit Huffman symbol for run length / number of bits */ nuclear@26: emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); nuclear@26: nuclear@26: /* Emit output bit for newly-nonzero coef */ nuclear@26: temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1; nuclear@26: emit_bits(entropy, (unsigned int) temp, 1); nuclear@26: nuclear@26: /* Emit buffered correction bits that must be associated with this code */ nuclear@26: emit_buffered_bits(entropy, BR_buffer, BR); nuclear@26: BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ nuclear@26: BR = 0; nuclear@26: r = 0; /* reset zero run length */ nuclear@26: } nuclear@26: nuclear@26: if (r > 0 || BR > 0) { /* If there are trailing zeroes, */ nuclear@26: entropy->EOBRUN++; /* count an EOB */ nuclear@26: entropy->BE += BR; /* concat my correction bits to older ones */ nuclear@26: /* We force out the EOB if we risk either: nuclear@26: * 1. overflow of the EOB counter; nuclear@26: * 2. overflow of the correction bit buffer during the next MCU. nuclear@26: */ nuclear@26: if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) nuclear@26: emit_eobrun(entropy); nuclear@26: } nuclear@26: nuclear@26: cinfo->dest->next_output_byte = entropy->next_output_byte; nuclear@26: cinfo->dest->free_in_buffer = entropy->free_in_buffer; nuclear@26: nuclear@26: /* Update restart-interval state too */ nuclear@26: if (cinfo->restart_interval) { nuclear@26: if (entropy->restarts_to_go == 0) { nuclear@26: entropy->restarts_to_go = cinfo->restart_interval; nuclear@26: entropy->next_restart_num++; nuclear@26: entropy->next_restart_num &= 7; nuclear@26: } nuclear@26: entropy->restarts_to_go--; nuclear@26: } nuclear@26: nuclear@26: return TRUE; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * Finish up at the end of a Huffman-compressed progressive scan. nuclear@26: */ nuclear@26: nuclear@26: METHODDEF(void) nuclear@26: finish_pass_phuff (j_compress_ptr cinfo) nuclear@26: { nuclear@26: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; nuclear@26: nuclear@26: entropy->next_output_byte = cinfo->dest->next_output_byte; nuclear@26: entropy->free_in_buffer = cinfo->dest->free_in_buffer; nuclear@26: nuclear@26: /* Flush out any buffered data */ nuclear@26: emit_eobrun(entropy); nuclear@26: flush_bits(entropy); nuclear@26: nuclear@26: cinfo->dest->next_output_byte = entropy->next_output_byte; nuclear@26: cinfo->dest->free_in_buffer = entropy->free_in_buffer; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * Finish up a statistics-gathering pass and create the new Huffman tables. nuclear@26: */ nuclear@26: nuclear@26: METHODDEF(void) nuclear@26: finish_pass_gather_phuff (j_compress_ptr cinfo) nuclear@26: { nuclear@26: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; nuclear@26: boolean is_DC_band; nuclear@26: int ci, tbl; nuclear@26: jpeg_component_info * compptr; nuclear@26: JHUFF_TBL **htblptr; nuclear@26: boolean did[NUM_HUFF_TBLS]; nuclear@26: nuclear@26: /* Flush out buffered data (all we care about is counting the EOB symbol) */ nuclear@26: emit_eobrun(entropy); nuclear@26: nuclear@26: is_DC_band = (cinfo->Ss == 0); nuclear@26: nuclear@26: /* It's important not to apply jpeg_gen_optimal_table more than once nuclear@26: * per table, because it clobbers the input frequency counts! nuclear@26: */ nuclear@26: MEMZERO(did, SIZEOF(did)); nuclear@26: nuclear@26: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { nuclear@26: compptr = cinfo->cur_comp_info[ci]; nuclear@26: if (is_DC_band) { nuclear@26: if (cinfo->Ah != 0) /* DC refinement needs no table */ nuclear@26: continue; nuclear@26: tbl = compptr->dc_tbl_no; nuclear@26: } else { nuclear@26: tbl = compptr->ac_tbl_no; nuclear@26: } nuclear@26: if (! did[tbl]) { nuclear@26: if (is_DC_band) nuclear@26: htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; nuclear@26: else nuclear@26: htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; nuclear@26: if (*htblptr == NULL) nuclear@26: *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); nuclear@26: jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]); nuclear@26: did[tbl] = TRUE; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* nuclear@26: * Module initialization routine for progressive Huffman entropy encoding. nuclear@26: */ nuclear@26: nuclear@26: GLOBAL(void) nuclear@26: jinit_phuff_encoder (j_compress_ptr cinfo) nuclear@26: { nuclear@26: phuff_entropy_ptr entropy; nuclear@26: int i; nuclear@26: nuclear@26: entropy = (phuff_entropy_ptr) nuclear@26: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@26: SIZEOF(phuff_entropy_encoder)); nuclear@26: cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; nuclear@26: entropy->pub.start_pass = start_pass_phuff; nuclear@26: nuclear@26: /* Mark tables unallocated */ nuclear@26: for (i = 0; i < NUM_HUFF_TBLS; i++) { nuclear@26: entropy->derived_tbls[i] = NULL; nuclear@26: entropy->count_ptrs[i] = NULL; nuclear@26: } nuclear@26: entropy->bit_buffer = NULL; /* needed only in AC refinement scan */ nuclear@26: } nuclear@26: nuclear@26: #endif /* C_PROGRESSIVE_SUPPORTED */