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