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