Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/awt/image/jpeg/jdphuff.c
38918 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jdphuff.c6*7* Copyright (C) 1995-1997, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains Huffman entropy decoding routines for progressive JPEG.12*13* Much of the complexity here has to do with supporting input suspension.14* If the data source module demands suspension, we want to be able to back15* up to the start of the current MCU. To do this, we copy state variables16* into local working storage, and update them back to the permanent17* storage only upon successful completion of an MCU.18*/1920#define JPEG_INTERNALS21#include "jinclude.h"22#include "jpeglib.h"23#include "jdhuff.h" /* Declarations shared with jdhuff.c */242526#ifdef D_PROGRESSIVE_SUPPORTED2728/*29* Expanded entropy decoder object for progressive Huffman decoding.30*31* The savable_state subrecord contains fields that change within an MCU,32* but must not be updated permanently until we complete the MCU.33*/3435typedef struct {36unsigned int EOBRUN; /* remaining EOBs in EOBRUN */37int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */38} savable_state;3940/* This macro is to work around compilers with missing or broken41* structure assignment. You'll need to fix this code if you have42* such a compiler and you change MAX_COMPS_IN_SCAN.43*/4445#ifndef NO_STRUCT_ASSIGN46#define ASSIGN_STATE(dest,src) ((dest) = (src))47#else48#if MAX_COMPS_IN_SCAN == 449#define ASSIGN_STATE(dest,src) \50((dest).EOBRUN = (src).EOBRUN, \51(dest).last_dc_val[0] = (src).last_dc_val[0], \52(dest).last_dc_val[1] = (src).last_dc_val[1], \53(dest).last_dc_val[2] = (src).last_dc_val[2], \54(dest).last_dc_val[3] = (src).last_dc_val[3])55#endif56#endif575859typedef struct {60struct jpeg_entropy_decoder pub; /* public fields */6162/* These fields are loaded into local variables at start of each MCU.63* In case of suspension, we exit WITHOUT updating them.64*/65bitread_perm_state bitstate; /* Bit buffer at start of MCU */66savable_state saved; /* Other state at start of MCU */6768/* These fields are NOT loaded into local working state. */69unsigned int restarts_to_go; /* MCUs left in this restart interval */7071/* Pointers to derived tables (these workspaces have image lifespan) */72d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];7374d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */75} phuff_entropy_decoder;7677typedef phuff_entropy_decoder * phuff_entropy_ptr;7879/* Forward declarations */80METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,81JBLOCKROW *MCU_data));82METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,83JBLOCKROW *MCU_data));84METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,85JBLOCKROW *MCU_data));86METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,87JBLOCKROW *MCU_data));888990/*91* Initialize for a Huffman-compressed scan.92*/9394METHODDEF(void)95start_pass_phuff_decoder (j_decompress_ptr cinfo)96{97phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;98boolean is_DC_band, bad;99int ci, coefi, tbl;100int *coef_bit_ptr;101jpeg_component_info * compptr;102103is_DC_band = (cinfo->Ss == 0);104105/* Validate scan parameters */106bad = FALSE;107if (is_DC_band) {108if (cinfo->Se != 0)109bad = TRUE;110} else {111/* need not check Ss/Se < 0 since they came from unsigned bytes */112if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)113bad = TRUE;114/* AC scans may have only one component */115if (cinfo->comps_in_scan != 1)116bad = TRUE;117}118if (cinfo->Ah != 0) {119/* Successive approximation refinement scan: must have Al = Ah-1. */120if (cinfo->Al != cinfo->Ah-1)121bad = TRUE;122}123if (cinfo->Al > 13) /* need not check for < 0 */124bad = TRUE;125/* Arguably the maximum Al value should be less than 13 for 8-bit precision,126* but the spec doesn't say so, and we try to be liberal about what we127* accept. Note: large Al values could result in out-of-range DC128* coefficients during early scans, leading to bizarre displays due to129* overflows in the IDCT math. But we won't crash.130*/131if (bad)132ERREXIT4(cinfo, JERR_BAD_PROGRESSION,133cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);134/* Update progression status, and verify that scan order is legal.135* Note that inter-scan inconsistencies are treated as warnings136* not fatal errors ... not clear if this is right way to behave.137*/138for (ci = 0; ci < cinfo->comps_in_scan; ci++) {139int cindex = cinfo->cur_comp_info[ci]->component_index;140coef_bit_ptr = & cinfo->coef_bits[cindex][0];141if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */142WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);143for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {144int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];145if (cinfo->Ah != expected)146WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);147coef_bit_ptr[coefi] = cinfo->Al;148}149}150151/* Select MCU decoding routine */152if (cinfo->Ah == 0) {153if (is_DC_band)154entropy->pub.decode_mcu = decode_mcu_DC_first;155else156entropy->pub.decode_mcu = decode_mcu_AC_first;157} else {158if (is_DC_band)159entropy->pub.decode_mcu = decode_mcu_DC_refine;160else161entropy->pub.decode_mcu = decode_mcu_AC_refine;162}163164for (ci = 0; ci < cinfo->comps_in_scan; ci++) {165compptr = cinfo->cur_comp_info[ci];166/* Make sure requested tables are present, and compute derived tables.167* We may build same derived table more than once, but it's not expensive.168*/169if (is_DC_band) {170if (cinfo->Ah == 0) { /* DC refinement needs no table */171tbl = compptr->dc_tbl_no;172jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,173& entropy->derived_tbls[tbl]);174}175} else {176tbl = compptr->ac_tbl_no;177jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,178& entropy->derived_tbls[tbl]);179/* remember the single active table */180entropy->ac_derived_tbl = entropy->derived_tbls[tbl];181}182/* Initialize DC predictions to 0 */183entropy->saved.last_dc_val[ci] = 0;184}185186/* Initialize bitread state variables */187entropy->bitstate.bits_left = 0;188entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */189entropy->pub.insufficient_data = FALSE;190191/* Initialize private state variables */192entropy->saved.EOBRUN = 0;193194/* Initialize restart counter */195entropy->restarts_to_go = cinfo->restart_interval;196}197198199/*200* Figure F.12: extend sign bit.201* On some machines, a shift and add will be faster than a table lookup.202*/203204#ifdef AVOID_TABLES205206#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))207208#else209210#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))211212static const int extend_test[16] = /* entry n is 2**(n-1) */213{ 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,2140x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };215216static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */217{ 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,218((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,219((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,220((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };221222#endif /* AVOID_TABLES */223224225/*226* Check for a restart marker & resynchronize decoder.227* Returns FALSE if must suspend.228*/229230LOCAL(boolean)231process_restart (j_decompress_ptr cinfo)232{233phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;234int ci;235236/* Throw away any unused bits remaining in bit buffer; */237/* include any full bytes in next_marker's count of discarded bytes */238cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;239entropy->bitstate.bits_left = 0;240241/* Advance past the RSTn marker */242if (! (*cinfo->marker->read_restart_marker) (cinfo))243return FALSE;244245/* Re-initialize DC predictions to 0 */246for (ci = 0; ci < cinfo->comps_in_scan; ci++)247entropy->saved.last_dc_val[ci] = 0;248/* Re-init EOB run count, too */249entropy->saved.EOBRUN = 0;250251/* Reset restart counter */252entropy->restarts_to_go = cinfo->restart_interval;253254/* Reset out-of-data flag, unless read_restart_marker left us smack up255* against a marker. In that case we will end up treating the next data256* segment as empty, and we can avoid producing bogus output pixels by257* leaving the flag set.258*/259if (cinfo->unread_marker == 0)260entropy->pub.insufficient_data = FALSE;261262return TRUE;263}264265266/*267* Huffman MCU decoding.268* Each of these routines decodes and returns one MCU's worth of269* Huffman-compressed coefficients.270* The coefficients are reordered from zigzag order into natural array order,271* but are not dequantized.272*273* The i'th block of the MCU is stored into the block pointed to by274* MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.275*276* We return FALSE if data source requested suspension. In that case no277* changes have been made to permanent state. (Exception: some output278* coefficients may already have been assigned. This is harmless for279* spectral selection, since we'll just re-assign them on the next call.280* Successive approximation AC refinement has to be more careful, however.)281*/282283/*284* MCU decoding for DC initial scan (either spectral selection,285* or first pass of successive approximation).286*/287288METHODDEF(boolean)289decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)290{291phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;292int Al = cinfo->Al;293register int s, r;294int blkn, ci;295JBLOCKROW block;296BITREAD_STATE_VARS;297savable_state state;298d_derived_tbl * tbl;299jpeg_component_info * compptr;300301/* Process restart marker if needed; may have to suspend */302if (cinfo->restart_interval) {303if (entropy->restarts_to_go == 0)304if (! process_restart(cinfo))305return FALSE;306}307308/* If we've run out of data, just leave the MCU set to zeroes.309* This way, we return uniform gray for the remainder of the segment.310*/311if (! entropy->pub.insufficient_data) {312313/* Load up working state */314BITREAD_LOAD_STATE(cinfo,entropy->bitstate);315ASSIGN_STATE(state, entropy->saved);316317/* Outer loop handles each block in the MCU */318319for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {320block = MCU_data[blkn];321ci = cinfo->MCU_membership[blkn];322compptr = cinfo->cur_comp_info[ci];323tbl = entropy->derived_tbls[compptr->dc_tbl_no];324325/* Decode a single block's worth of coefficients */326327/* Section F.2.2.1: decode the DC coefficient difference */328HUFF_DECODE(s, br_state, tbl, return FALSE, label1);329if (s) {330CHECK_BIT_BUFFER(br_state, s, return FALSE);331r = GET_BITS(s);332s = HUFF_EXTEND(r, s);333}334335/* Convert DC difference to actual value, update last_dc_val */336s += state.last_dc_val[ci];337state.last_dc_val[ci] = s;338/* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */339(*block)[0] = (JCOEF) (s << Al);340}341342/* Completed MCU, so update state */343BITREAD_SAVE_STATE(cinfo,entropy->bitstate);344ASSIGN_STATE(entropy->saved, state);345}346347/* Account for restart interval (no-op if not using restarts) */348entropy->restarts_to_go--;349350return TRUE;351}352353354/*355* MCU decoding for AC initial scan (either spectral selection,356* or first pass of successive approximation).357*/358359METHODDEF(boolean)360decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)361{362phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;363int Se = cinfo->Se;364int Al = cinfo->Al;365register int s, k, r;366unsigned int EOBRUN;367JBLOCKROW block;368BITREAD_STATE_VARS;369d_derived_tbl * tbl;370371/* Process restart marker if needed; may have to suspend */372if (cinfo->restart_interval) {373if (entropy->restarts_to_go == 0)374if (! process_restart(cinfo))375return FALSE;376}377378/* If we've run out of data, just leave the MCU set to zeroes.379* This way, we return uniform gray for the remainder of the segment.380*/381if (! entropy->pub.insufficient_data) {382383/* Load up working state.384* We can avoid loading/saving bitread state if in an EOB run.385*/386EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */387388/* There is always only one block per MCU */389390if (EOBRUN > 0) /* if it's a band of zeroes... */391EOBRUN--; /* ...process it now (we do nothing) */392else {393BITREAD_LOAD_STATE(cinfo,entropy->bitstate);394block = MCU_data[0];395tbl = entropy->ac_derived_tbl;396397for (k = cinfo->Ss; k <= Se; k++) {398HUFF_DECODE(s, br_state, tbl, return FALSE, label2);399r = s >> 4;400s &= 15;401if (s) {402k += r;403CHECK_BIT_BUFFER(br_state, s, return FALSE);404r = GET_BITS(s);405s = HUFF_EXTEND(r, s);406/* Scale and output coefficient in natural (dezigzagged) order */407(*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);408} else {409if (r == 15) { /* ZRL */410k += 15; /* skip 15 zeroes in band */411} else { /* EOBr, run length is 2^r + appended bits */412EOBRUN = 1 << r;413if (r) { /* EOBr, r > 0 */414CHECK_BIT_BUFFER(br_state, r, return FALSE);415r = GET_BITS(r);416EOBRUN += r;417}418EOBRUN--; /* this band is processed at this moment */419break; /* force end-of-band */420}421}422}423424BITREAD_SAVE_STATE(cinfo,entropy->bitstate);425}426427/* Completed MCU, so update state */428entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */429}430431/* Account for restart interval (no-op if not using restarts) */432entropy->restarts_to_go--;433434return TRUE;435}436437438/*439* MCU decoding for DC successive approximation refinement scan.440* Note: we assume such scans can be multi-component, although the spec441* is not very clear on the point.442*/443444METHODDEF(boolean)445decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)446{447phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;448int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */449int blkn;450JBLOCKROW block;451BITREAD_STATE_VARS;452453/* Process restart marker if needed; may have to suspend */454if (cinfo->restart_interval) {455if (entropy->restarts_to_go == 0)456if (! process_restart(cinfo))457return FALSE;458}459460/* Not worth the cycles to check insufficient_data here,461* since we will not change the data anyway if we read zeroes.462*/463464/* Load up working state */465BITREAD_LOAD_STATE(cinfo,entropy->bitstate);466467/* Outer loop handles each block in the MCU */468469for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {470block = MCU_data[blkn];471472/* Encoded data is simply the next bit of the two's-complement DC value */473CHECK_BIT_BUFFER(br_state, 1, return FALSE);474if (GET_BITS(1))475(*block)[0] |= p1;476/* Note: since we use |=, repeating the assignment later is safe */477}478479/* Completed MCU, so update state */480BITREAD_SAVE_STATE(cinfo,entropy->bitstate);481482/* Account for restart interval (no-op if not using restarts) */483entropy->restarts_to_go--;484485return TRUE;486}487488489/*490* MCU decoding for AC successive approximation refinement scan.491*/492493METHODDEF(boolean)494decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)495{496phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;497int Se = cinfo->Se;498int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */499int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */500register int s, k, r;501unsigned int EOBRUN;502JBLOCKROW block;503JCOEFPTR thiscoef;504BITREAD_STATE_VARS;505d_derived_tbl * tbl;506int num_newnz;507int newnz_pos[DCTSIZE2];508509/* Process restart marker if needed; may have to suspend */510if (cinfo->restart_interval) {511if (entropy->restarts_to_go == 0)512if (! process_restart(cinfo))513return FALSE;514}515516/* If we've run out of data, don't modify the MCU.517*/518if (! entropy->pub.insufficient_data) {519520/* Load up working state */521BITREAD_LOAD_STATE(cinfo,entropy->bitstate);522EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */523524/* There is always only one block per MCU */525block = MCU_data[0];526tbl = entropy->ac_derived_tbl;527528/* If we are forced to suspend, we must undo the assignments to any newly529* nonzero coefficients in the block, because otherwise we'd get confused530* next time about which coefficients were already nonzero.531* But we need not undo addition of bits to already-nonzero coefficients;532* instead, we can test the current bit to see if we already did it.533*/534num_newnz = 0;535536/* initialize coefficient loop counter to start of band */537k = cinfo->Ss;538539if (EOBRUN == 0) {540for (; k <= Se; k++) {541HUFF_DECODE(s, br_state, tbl, goto undoit, label3);542r = s >> 4;543s &= 15;544if (s) {545if (s != 1) /* size of new coef should always be 1 */546WARNMS(cinfo, JWRN_HUFF_BAD_CODE);547CHECK_BIT_BUFFER(br_state, 1, goto undoit);548if (GET_BITS(1))549s = p1; /* newly nonzero coef is positive */550else551s = m1; /* newly nonzero coef is negative */552} else {553if (r != 15) {554EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */555if (r) {556CHECK_BIT_BUFFER(br_state, r, goto undoit);557r = GET_BITS(r);558EOBRUN += r;559}560break; /* rest of block is handled by EOB logic */561}562/* note s = 0 for processing ZRL */563}564/* Advance over already-nonzero coefs and r still-zero coefs,565* appending correction bits to the nonzeroes. A correction bit is 1566* if the absolute value of the coefficient must be increased.567*/568do {569thiscoef = *block + jpeg_natural_order[k];570if (*thiscoef != 0) {571CHECK_BIT_BUFFER(br_state, 1, goto undoit);572if (GET_BITS(1)) {573if ((*thiscoef & p1) == 0) { /* do nothing if already set it */574if (*thiscoef >= 0)575*thiscoef += p1;576else577*thiscoef += m1;578}579}580} else {581if (--r < 0)582break; /* reached target zero coefficient */583}584k++;585} while (k <= Se);586if (s) {587int pos = jpeg_natural_order[k];588/* Output newly nonzero coefficient */589(*block)[pos] = (JCOEF) s;590/* Remember its position in case we have to suspend */591newnz_pos[num_newnz++] = pos;592}593}594}595596if (EOBRUN > 0) {597/* Scan any remaining coefficient positions after the end-of-band598* (the last newly nonzero coefficient, if any). Append a correction599* bit to each already-nonzero coefficient. A correction bit is 1600* if the absolute value of the coefficient must be increased.601*/602for (; k <= Se; k++) {603thiscoef = *block + jpeg_natural_order[k];604if (*thiscoef != 0) {605CHECK_BIT_BUFFER(br_state, 1, goto undoit);606if (GET_BITS(1)) {607if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */608if (*thiscoef >= 0)609*thiscoef += p1;610else611*thiscoef += m1;612}613}614}615}616/* Count one block completed in EOB run */617EOBRUN--;618}619620/* Completed MCU, so update state */621BITREAD_SAVE_STATE(cinfo,entropy->bitstate);622entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */623}624625/* Account for restart interval (no-op if not using restarts) */626entropy->restarts_to_go--;627628return TRUE;629630undoit:631/* Re-zero any output coefficients that we made newly nonzero */632while (num_newnz > 0)633(*block)[newnz_pos[--num_newnz]] = 0;634635return FALSE;636}637638639/*640* Module initialization routine for progressive Huffman entropy decoding.641*/642643GLOBAL(void)644jinit_phuff_decoder (j_decompress_ptr cinfo)645{646phuff_entropy_ptr entropy;647int *coef_bit_ptr;648int ci, i;649650entropy = (phuff_entropy_ptr)651(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,652SIZEOF(phuff_entropy_decoder));653cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;654entropy->pub.start_pass = start_pass_phuff_decoder;655656/* Mark derived tables unallocated */657for (i = 0; i < NUM_HUFF_TBLS; i++) {658entropy->derived_tbls[i] = NULL;659}660661/* Create progression status table */662cinfo->coef_bits = (int (*)[DCTSIZE2])663(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,664cinfo->num_components*DCTSIZE2*SIZEOF(int));665coef_bit_ptr = & cinfo->coef_bits[0][0];666for (ci = 0; ci < cinfo->num_components; ci++)667for (i = 0; i < DCTSIZE2; i++)668*coef_bit_ptr++ = -1;669}670671#endif /* D_PROGRESSIVE_SUPPORTED */672673674