Path: blob/master/3rdparty/libjpeg-turbo/src/jcphuff.c
16337 views
/*1* jcphuff.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1995-1997, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright (C) 2015, D. R. Commander.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains Huffman entropy encoding routines for progressive JPEG.11*12* We do not support output suspension in this module, since the library13* currently does not allow multiple-scan files to be written with output14* suspension.15*/1617#define JPEG_INTERNALS18#include "jinclude.h"19#include "jpeglib.h"20#include "jchuff.h" /* Declarations shared with jchuff.c */2122#ifdef C_PROGRESSIVE_SUPPORTED2324/* Expanded entropy encoder object for progressive Huffman encoding. */2526typedef struct {27struct jpeg_entropy_encoder pub; /* public fields */2829/* Mode flag: TRUE for optimization, FALSE for actual data output */30boolean gather_statistics;3132/* Bit-level coding status.33* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.34*/35JOCTET *next_output_byte; /* => next byte to write in buffer */36size_t free_in_buffer; /* # of byte spaces remaining in buffer */37size_t put_buffer; /* current bit-accumulation buffer */38int put_bits; /* # of bits now in it */39j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */4041/* Coding status for DC components */42int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */4344/* Coding status for AC components */45int ac_tbl_no; /* the table number of the single component */46unsigned int EOBRUN; /* run length of EOBs */47unsigned int BE; /* # of buffered correction bits before MCU */48char *bit_buffer; /* buffer for correction bits (1 per char) */49/* packing correction bits tightly would save some space but cost time... */5051unsigned int restarts_to_go; /* MCUs left in this restart interval */52int next_restart_num; /* next restart number to write (0-7) */5354/* Pointers to derived tables (these workspaces have image lifespan).55* Since any one scan codes only DC or only AC, we only need one set56* of tables, not one for DC and one for AC.57*/58c_derived_tbl *derived_tbls[NUM_HUFF_TBLS];5960/* Statistics tables for optimization; again, one set is enough */61long *count_ptrs[NUM_HUFF_TBLS];62} phuff_entropy_encoder;6364typedef phuff_entropy_encoder *phuff_entropy_ptr;6566/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit67* buffer can hold. Larger sizes may slightly improve compression, but68* 1000 is already well into the realm of overkill.69* The minimum safe size is 64 bits.70*/7172#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */7374/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.75* We assume that int right shift is unsigned if JLONG right shift is,76* which should be safe.77*/7879#ifdef RIGHT_SHIFT_IS_UNSIGNED80#define ISHIFT_TEMPS int ishift_temp;81#define IRIGHT_SHIFT(x,shft) \82((ishift_temp = (x)) < 0 ? \83(ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \84(ishift_temp >> (shft)))85#else86#define ISHIFT_TEMPS87#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))88#endif8990/* Forward declarations */91METHODDEF(boolean) encode_mcu_DC_first (j_compress_ptr cinfo,92JBLOCKROW *MCU_data);93METHODDEF(boolean) encode_mcu_AC_first (j_compress_ptr cinfo,94JBLOCKROW *MCU_data);95METHODDEF(boolean) encode_mcu_DC_refine (j_compress_ptr cinfo,96JBLOCKROW *MCU_data);97METHODDEF(boolean) encode_mcu_AC_refine (j_compress_ptr cinfo,98JBLOCKROW *MCU_data);99METHODDEF(void) finish_pass_phuff (j_compress_ptr cinfo);100METHODDEF(void) finish_pass_gather_phuff (j_compress_ptr cinfo);101102103/*104* Initialize for a Huffman-compressed scan using progressive JPEG.105*/106107METHODDEF(void)108start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)109{110phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;111boolean is_DC_band;112int ci, tbl;113jpeg_component_info *compptr;114115entropy->cinfo = cinfo;116entropy->gather_statistics = gather_statistics;117118is_DC_band = (cinfo->Ss == 0);119120/* We assume jcmaster.c already validated the scan parameters. */121122/* Select execution routines */123if (cinfo->Ah == 0) {124if (is_DC_band)125entropy->pub.encode_mcu = encode_mcu_DC_first;126else127entropy->pub.encode_mcu = encode_mcu_AC_first;128} else {129if (is_DC_band)130entropy->pub.encode_mcu = encode_mcu_DC_refine;131else {132entropy->pub.encode_mcu = encode_mcu_AC_refine;133/* AC refinement needs a correction bit buffer */134if (entropy->bit_buffer == NULL)135entropy->bit_buffer = (char *)136(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,137MAX_CORR_BITS * sizeof(char));138}139}140if (gather_statistics)141entropy->pub.finish_pass = finish_pass_gather_phuff;142else143entropy->pub.finish_pass = finish_pass_phuff;144145/* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1146* for AC coefficients.147*/148for (ci = 0; ci < cinfo->comps_in_scan; ci++) {149compptr = cinfo->cur_comp_info[ci];150/* Initialize DC predictions to 0 */151entropy->last_dc_val[ci] = 0;152/* Get table index */153if (is_DC_band) {154if (cinfo->Ah != 0) /* DC refinement needs no table */155continue;156tbl = compptr->dc_tbl_no;157} else {158entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;159}160if (gather_statistics) {161/* Check for invalid table index */162/* (make_c_derived_tbl does this in the other path) */163if (tbl < 0 || tbl >= NUM_HUFF_TBLS)164ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);165/* Allocate and zero the statistics tables */166/* Note that jpeg_gen_optimal_table expects 257 entries in each table! */167if (entropy->count_ptrs[tbl] == NULL)168entropy->count_ptrs[tbl] = (long *)169(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,170257 * sizeof(long));171MEMZERO(entropy->count_ptrs[tbl], 257 * sizeof(long));172} else {173/* Compute derived values for Huffman table */174/* We may do this more than once for a table, but it's not expensive */175jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,176& entropy->derived_tbls[tbl]);177}178}179180/* Initialize AC stuff */181entropy->EOBRUN = 0;182entropy->BE = 0;183184/* Initialize bit buffer to empty */185entropy->put_buffer = 0;186entropy->put_bits = 0;187188/* Initialize restart stuff */189entropy->restarts_to_go = cinfo->restart_interval;190entropy->next_restart_num = 0;191}192193194/* Outputting bytes to the file.195* NB: these must be called only when actually outputting,196* that is, entropy->gather_statistics == FALSE.197*/198199/* Emit a byte */200#define emit_byte(entropy,val) \201{ *(entropy)->next_output_byte++ = (JOCTET) (val); \202if (--(entropy)->free_in_buffer == 0) \203dump_buffer(entropy); }204205206LOCAL(void)207dump_buffer (phuff_entropy_ptr entropy)208/* Empty the output buffer; we do not support suspension in this module. */209{210struct jpeg_destination_mgr *dest = entropy->cinfo->dest;211212if (! (*dest->empty_output_buffer) (entropy->cinfo))213ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);214/* After a successful buffer dump, must reset buffer pointers */215entropy->next_output_byte = dest->next_output_byte;216entropy->free_in_buffer = dest->free_in_buffer;217}218219220/* Outputting bits to the file */221222/* Only the right 24 bits of put_buffer are used; the valid bits are223* left-justified in this part. At most 16 bits can be passed to emit_bits224* in one call, and we never retain more than 7 bits in put_buffer225* between calls, so 24 bits are sufficient.226*/227228LOCAL(void)229emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)230/* Emit some bits, unless we are in gather mode */231{232/* This routine is heavily used, so it's worth coding tightly. */233register size_t put_buffer = (size_t) code;234register int put_bits = entropy->put_bits;235236/* if size is 0, caller used an invalid Huffman table entry */237if (size == 0)238ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);239240if (entropy->gather_statistics)241return; /* do nothing if we're only getting stats */242243put_buffer &= (((size_t) 1)<<size) - 1; /* mask off any extra bits in code */244245put_bits += size; /* new number of bits in buffer */246247put_buffer <<= 24 - put_bits; /* align incoming bits */248249put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */250251while (put_bits >= 8) {252int c = (int) ((put_buffer >> 16) & 0xFF);253254emit_byte(entropy, c);255if (c == 0xFF) { /* need to stuff a zero byte? */256emit_byte(entropy, 0);257}258put_buffer <<= 8;259put_bits -= 8;260}261262entropy->put_buffer = put_buffer; /* update variables */263entropy->put_bits = put_bits;264}265266267LOCAL(void)268flush_bits (phuff_entropy_ptr entropy)269{270emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */271entropy->put_buffer = 0; /* and reset bit-buffer to empty */272entropy->put_bits = 0;273}274275276/*277* Emit (or just count) a Huffman symbol.278*/279280LOCAL(void)281emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)282{283if (entropy->gather_statistics)284entropy->count_ptrs[tbl_no][symbol]++;285else {286c_derived_tbl *tbl = entropy->derived_tbls[tbl_no];287emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);288}289}290291292/*293* Emit bits from a correction bit buffer.294*/295296LOCAL(void)297emit_buffered_bits (phuff_entropy_ptr entropy, char *bufstart,298unsigned int nbits)299{300if (entropy->gather_statistics)301return; /* no real work */302303while (nbits > 0) {304emit_bits(entropy, (unsigned int) (*bufstart), 1);305bufstart++;306nbits--;307}308}309310311/*312* Emit any pending EOBRUN symbol.313*/314315LOCAL(void)316emit_eobrun (phuff_entropy_ptr entropy)317{318register int temp, nbits;319320if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */321temp = entropy->EOBRUN;322nbits = 0;323while ((temp >>= 1))324nbits++;325/* safety check: shouldn't happen given limited correction-bit buffer */326if (nbits > 14)327ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);328329emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);330if (nbits)331emit_bits(entropy, entropy->EOBRUN, nbits);332333entropy->EOBRUN = 0;334335/* Emit any buffered correction bits */336emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);337entropy->BE = 0;338}339}340341342/*343* Emit a restart marker & resynchronize predictions.344*/345346LOCAL(void)347emit_restart (phuff_entropy_ptr entropy, int restart_num)348{349int ci;350351emit_eobrun(entropy);352353if (! entropy->gather_statistics) {354flush_bits(entropy);355emit_byte(entropy, 0xFF);356emit_byte(entropy, JPEG_RST0 + restart_num);357}358359if (entropy->cinfo->Ss == 0) {360/* Re-initialize DC predictions to 0 */361for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)362entropy->last_dc_val[ci] = 0;363} else {364/* Re-initialize all AC-related fields to 0 */365entropy->EOBRUN = 0;366entropy->BE = 0;367}368}369370371/*372* MCU encoding for DC initial scan (either spectral selection,373* or first pass of successive approximation).374*/375376METHODDEF(boolean)377encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)378{379phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;380register int temp, temp2;381register int nbits;382int blkn, ci;383int Al = cinfo->Al;384JBLOCKROW block;385jpeg_component_info *compptr;386ISHIFT_TEMPS387388entropy->next_output_byte = cinfo->dest->next_output_byte;389entropy->free_in_buffer = cinfo->dest->free_in_buffer;390391/* Emit restart marker if needed */392if (cinfo->restart_interval)393if (entropy->restarts_to_go == 0)394emit_restart(entropy, entropy->next_restart_num);395396/* Encode the MCU data blocks */397for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {398block = MCU_data[blkn];399ci = cinfo->MCU_membership[blkn];400compptr = cinfo->cur_comp_info[ci];401402/* Compute the DC value after the required point transform by Al.403* This is simply an arithmetic right shift.404*/405temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);406407/* DC differences are figured on the point-transformed values. */408temp = temp2 - entropy->last_dc_val[ci];409entropy->last_dc_val[ci] = temp2;410411/* Encode the DC coefficient difference per section G.1.2.1 */412temp2 = temp;413if (temp < 0) {414temp = -temp; /* temp is abs value of input */415/* For a negative input, want temp2 = bitwise complement of abs(input) */416/* This code assumes we are on a two's complement machine */417temp2--;418}419420/* Find the number of bits needed for the magnitude of the coefficient */421nbits = 0;422while (temp) {423nbits++;424temp >>= 1;425}426/* Check for out-of-range coefficient values.427* Since we're encoding a difference, the range limit is twice as much.428*/429if (nbits > MAX_COEF_BITS+1)430ERREXIT(cinfo, JERR_BAD_DCT_COEF);431432/* Count/emit the Huffman-coded symbol for the number of bits */433emit_symbol(entropy, compptr->dc_tbl_no, nbits);434435/* Emit that number of bits of the value, if positive, */436/* or the complement of its magnitude, if negative. */437if (nbits) /* emit_bits rejects calls with size 0 */438emit_bits(entropy, (unsigned int) temp2, nbits);439}440441cinfo->dest->next_output_byte = entropy->next_output_byte;442cinfo->dest->free_in_buffer = entropy->free_in_buffer;443444/* Update restart-interval state too */445if (cinfo->restart_interval) {446if (entropy->restarts_to_go == 0) {447entropy->restarts_to_go = cinfo->restart_interval;448entropy->next_restart_num++;449entropy->next_restart_num &= 7;450}451entropy->restarts_to_go--;452}453454return TRUE;455}456457458/*459* MCU encoding for AC initial scan (either spectral selection,460* or first pass of successive approximation).461*/462463METHODDEF(boolean)464encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)465{466phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;467register int temp, temp2;468register int nbits;469register int r, k;470int Se = cinfo->Se;471int Al = cinfo->Al;472JBLOCKROW block;473474entropy->next_output_byte = cinfo->dest->next_output_byte;475entropy->free_in_buffer = cinfo->dest->free_in_buffer;476477/* Emit restart marker if needed */478if (cinfo->restart_interval)479if (entropy->restarts_to_go == 0)480emit_restart(entropy, entropy->next_restart_num);481482/* Encode the MCU data block */483block = MCU_data[0];484485/* Encode the AC coefficients per section G.1.2.2, fig. G.3 */486487r = 0; /* r = run length of zeros */488489for (k = cinfo->Ss; k <= Se; k++) {490if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {491r++;492continue;493}494/* We must apply the point transform by Al. For AC coefficients this495* is an integer division with rounding towards 0. To do this portably496* in C, we shift after obtaining the absolute value; so the code is497* interwoven with finding the abs value (temp) and output bits (temp2).498*/499if (temp < 0) {500temp = -temp; /* temp is abs value of input */501temp >>= Al; /* apply the point transform */502/* For a negative coef, want temp2 = bitwise complement of abs(coef) */503temp2 = ~temp;504} else {505temp >>= Al; /* apply the point transform */506temp2 = temp;507}508/* Watch out for case that nonzero coef is zero after point transform */509if (temp == 0) {510r++;511continue;512}513514/* Emit any pending EOBRUN */515if (entropy->EOBRUN > 0)516emit_eobrun(entropy);517/* if run length > 15, must emit special run-length-16 codes (0xF0) */518while (r > 15) {519emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);520r -= 16;521}522523/* Find the number of bits needed for the magnitude of the coefficient */524nbits = 1; /* there must be at least one 1 bit */525while ((temp >>= 1))526nbits++;527/* Check for out-of-range coefficient values */528if (nbits > MAX_COEF_BITS)529ERREXIT(cinfo, JERR_BAD_DCT_COEF);530531/* Count/emit Huffman symbol for run length / number of bits */532emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);533534/* Emit that number of bits of the value, if positive, */535/* or the complement of its magnitude, if negative. */536emit_bits(entropy, (unsigned int) temp2, nbits);537538r = 0; /* reset zero run length */539}540541if (r > 0) { /* If there are trailing zeroes, */542entropy->EOBRUN++; /* count an EOB */543if (entropy->EOBRUN == 0x7FFF)544emit_eobrun(entropy); /* force it out to avoid overflow */545}546547cinfo->dest->next_output_byte = entropy->next_output_byte;548cinfo->dest->free_in_buffer = entropy->free_in_buffer;549550/* Update restart-interval state too */551if (cinfo->restart_interval) {552if (entropy->restarts_to_go == 0) {553entropy->restarts_to_go = cinfo->restart_interval;554entropy->next_restart_num++;555entropy->next_restart_num &= 7;556}557entropy->restarts_to_go--;558}559560return TRUE;561}562563564/*565* MCU encoding for DC successive approximation refinement scan.566* Note: we assume such scans can be multi-component, although the spec567* is not very clear on the point.568*/569570METHODDEF(boolean)571encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)572{573phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;574register int temp;575int blkn;576int Al = cinfo->Al;577JBLOCKROW block;578579entropy->next_output_byte = cinfo->dest->next_output_byte;580entropy->free_in_buffer = cinfo->dest->free_in_buffer;581582/* Emit restart marker if needed */583if (cinfo->restart_interval)584if (entropy->restarts_to_go == 0)585emit_restart(entropy, entropy->next_restart_num);586587/* Encode the MCU data blocks */588for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {589block = MCU_data[blkn];590591/* We simply emit the Al'th bit of the DC coefficient value. */592temp = (*block)[0];593emit_bits(entropy, (unsigned int) (temp >> Al), 1);594}595596cinfo->dest->next_output_byte = entropy->next_output_byte;597cinfo->dest->free_in_buffer = entropy->free_in_buffer;598599/* Update restart-interval state too */600if (cinfo->restart_interval) {601if (entropy->restarts_to_go == 0) {602entropy->restarts_to_go = cinfo->restart_interval;603entropy->next_restart_num++;604entropy->next_restart_num &= 7;605}606entropy->restarts_to_go--;607}608609return TRUE;610}611612613/*614* MCU encoding for AC successive approximation refinement scan.615*/616617METHODDEF(boolean)618encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)619{620phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;621register int temp;622register int r, k;623int EOB;624char *BR_buffer;625unsigned int BR;626int Se = cinfo->Se;627int Al = cinfo->Al;628JBLOCKROW block;629int absvalues[DCTSIZE2];630631entropy->next_output_byte = cinfo->dest->next_output_byte;632entropy->free_in_buffer = cinfo->dest->free_in_buffer;633634/* Emit restart marker if needed */635if (cinfo->restart_interval)636if (entropy->restarts_to_go == 0)637emit_restart(entropy, entropy->next_restart_num);638639/* Encode the MCU data block */640block = MCU_data[0];641642/* It is convenient to make a pre-pass to determine the transformed643* coefficients' absolute values and the EOB position.644*/645EOB = 0;646for (k = cinfo->Ss; k <= Se; k++) {647temp = (*block)[jpeg_natural_order[k]];648/* We must apply the point transform by Al. For AC coefficients this649* is an integer division with rounding towards 0. To do this portably650* in C, we shift after obtaining the absolute value.651*/652if (temp < 0)653temp = -temp; /* temp is abs value of input */654temp >>= Al; /* apply the point transform */655absvalues[k] = temp; /* save abs value for main pass */656if (temp == 1)657EOB = k; /* EOB = index of last newly-nonzero coef */658}659660/* Encode the AC coefficients per section G.1.2.3, fig. G.7 */661662r = 0; /* r = run length of zeros */663BR = 0; /* BR = count of buffered bits added now */664BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */665666for (k = cinfo->Ss; k <= Se; k++) {667if ((temp = absvalues[k]) == 0) {668r++;669continue;670}671672/* Emit any required ZRLs, but not if they can be folded into EOB */673while (r > 15 && k <= EOB) {674/* emit any pending EOBRUN and the BE correction bits */675emit_eobrun(entropy);676/* Emit ZRL */677emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);678r -= 16;679/* Emit buffered correction bits that must be associated with ZRL */680emit_buffered_bits(entropy, BR_buffer, BR);681BR_buffer = entropy->bit_buffer; /* BE bits are gone now */682BR = 0;683}684685/* If the coef was previously nonzero, it only needs a correction bit.686* NOTE: a straight translation of the spec's figure G.7 would suggest687* that we also need to test r > 15. But if r > 15, we can only get here688* if k > EOB, which implies that this coefficient is not 1.689*/690if (temp > 1) {691/* The correction bit is the next bit of the absolute value. */692BR_buffer[BR++] = (char) (temp & 1);693continue;694}695696/* Emit any pending EOBRUN and the BE correction bits */697emit_eobrun(entropy);698699/* Count/emit Huffman symbol for run length / number of bits */700emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);701702/* Emit output bit for newly-nonzero coef */703temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;704emit_bits(entropy, (unsigned int) temp, 1);705706/* Emit buffered correction bits that must be associated with this code */707emit_buffered_bits(entropy, BR_buffer, BR);708BR_buffer = entropy->bit_buffer; /* BE bits are gone now */709BR = 0;710r = 0; /* reset zero run length */711}712713if (r > 0 || BR > 0) { /* If there are trailing zeroes, */714entropy->EOBRUN++; /* count an EOB */715entropy->BE += BR; /* concat my correction bits to older ones */716/* We force out the EOB if we risk either:717* 1. overflow of the EOB counter;718* 2. overflow of the correction bit buffer during the next MCU.719*/720if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))721emit_eobrun(entropy);722}723724cinfo->dest->next_output_byte = entropy->next_output_byte;725cinfo->dest->free_in_buffer = entropy->free_in_buffer;726727/* Update restart-interval state too */728if (cinfo->restart_interval) {729if (entropy->restarts_to_go == 0) {730entropy->restarts_to_go = cinfo->restart_interval;731entropy->next_restart_num++;732entropy->next_restart_num &= 7;733}734entropy->restarts_to_go--;735}736737return TRUE;738}739740741/*742* Finish up at the end of a Huffman-compressed progressive scan.743*/744745METHODDEF(void)746finish_pass_phuff (j_compress_ptr cinfo)747{748phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;749750entropy->next_output_byte = cinfo->dest->next_output_byte;751entropy->free_in_buffer = cinfo->dest->free_in_buffer;752753/* Flush out any buffered data */754emit_eobrun(entropy);755flush_bits(entropy);756757cinfo->dest->next_output_byte = entropy->next_output_byte;758cinfo->dest->free_in_buffer = entropy->free_in_buffer;759}760761762/*763* Finish up a statistics-gathering pass and create the new Huffman tables.764*/765766METHODDEF(void)767finish_pass_gather_phuff (j_compress_ptr cinfo)768{769phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;770boolean is_DC_band;771int ci, tbl;772jpeg_component_info *compptr;773JHUFF_TBL **htblptr;774boolean did[NUM_HUFF_TBLS];775776/* Flush out buffered data (all we care about is counting the EOB symbol) */777emit_eobrun(entropy);778779is_DC_band = (cinfo->Ss == 0);780781/* It's important not to apply jpeg_gen_optimal_table more than once782* per table, because it clobbers the input frequency counts!783*/784MEMZERO(did, sizeof(did));785786for (ci = 0; ci < cinfo->comps_in_scan; ci++) {787compptr = cinfo->cur_comp_info[ci];788if (is_DC_band) {789if (cinfo->Ah != 0) /* DC refinement needs no table */790continue;791tbl = compptr->dc_tbl_no;792} else {793tbl = compptr->ac_tbl_no;794}795if (! did[tbl]) {796if (is_DC_band)797htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];798else799htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];800if (*htblptr == NULL)801*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);802jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);803did[tbl] = TRUE;804}805}806}807808809/*810* Module initialization routine for progressive Huffman entropy encoding.811*/812813GLOBAL(void)814jinit_phuff_encoder (j_compress_ptr cinfo)815{816phuff_entropy_ptr entropy;817int i;818819entropy = (phuff_entropy_ptr)820(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,821sizeof(phuff_entropy_encoder));822cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;823entropy->pub.start_pass = start_pass_phuff;824825/* Mark tables unallocated */826for (i = 0; i < NUM_HUFF_TBLS; i++) {827entropy->derived_tbls[i] = NULL;828entropy->count_ptrs[i] = NULL;829}830entropy->bit_buffer = NULL; /* needed only in AC refinement scan */831}832833#endif /* C_PROGRESSIVE_SUPPORTED */834835836