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