Path: blob/master/3rdparty/libjpeg-turbo/src/jdarith.c
16337 views
/*1* jdarith.c2*3* This file was part of the Independent JPEG Group's software:4* Developed 1997-2015 by Guido Vollbeding.5* libjpeg-turbo Modifications:6* Copyright (C) 2015-2016, D. R. Commander.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains portable arithmetic entropy decoding routines for JPEG11* (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).12*13* Both sequential and progressive modes are supported in this single module.14*15* Suspension is not currently supported in this module.16*/1718#define JPEG_INTERNALS19#include "jinclude.h"20#include "jpeglib.h"212223#define NEG_1 ((unsigned int)-1)242526/* Expanded entropy decoder object for arithmetic decoding. */2728typedef struct {29struct jpeg_entropy_decoder pub; /* public fields */3031JLONG c; /* C register, base of coding interval + input bit buffer */32JLONG a; /* A register, normalized size of coding interval */33int ct; /* bit shift counter, # of bits left in bit buffer part of C */34/* init: ct = -16 */35/* run: ct = 0..7 */36/* error: ct = -1 */37int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */38int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */3940unsigned int restarts_to_go; /* MCUs left in this restart interval */4142/* Pointers to statistics areas (these workspaces have image lifespan) */43unsigned char *dc_stats[NUM_ARITH_TBLS];44unsigned char *ac_stats[NUM_ARITH_TBLS];4546/* Statistics bin for coding with fixed probability 0.5 */47unsigned char fixed_bin[4];48} arith_entropy_decoder;4950typedef arith_entropy_decoder *arith_entropy_ptr;5152/* The following two definitions specify the allocation chunk size53* for the statistics area.54* According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least55* 49 statistics bins for DC, and 245 statistics bins for AC coding.56*57* We use a compact representation with 1 byte per statistics bin,58* thus the numbers directly represent byte sizes.59* This 1 byte per statistics bin contains the meaning of the MPS60* (more probable symbol) in the highest bit (mask 0x80), and the61* index into the probability estimation state machine table62* in the lower bits (mask 0x7F).63*/6465#define DC_STAT_BINS 6466#define AC_STAT_BINS 256676869LOCAL(int)70get_byte (j_decompress_ptr cinfo)71/* Read next input byte; we do not support suspension in this module. */72{73struct jpeg_source_mgr *src = cinfo->src;7475if (src->bytes_in_buffer == 0)76if (! (*src->fill_input_buffer) (cinfo))77ERREXIT(cinfo, JERR_CANT_SUSPEND);78src->bytes_in_buffer--;79return GETJOCTET(*src->next_input_byte++);80}818283/*84* The core arithmetic decoding routine (common in JPEG and JBIG).85* This needs to go as fast as possible.86* Machine-dependent optimization facilities87* are not utilized in this portable implementation.88* However, this code should be fairly efficient and89* may be a good base for further optimizations anyway.90*91* Return value is 0 or 1 (binary decision).92*93* Note: I've changed the handling of the code base & bit94* buffer register C compared to other implementations95* based on the standards layout & procedures.96* While it also contains both the actual base of the97* coding interval (16 bits) and the next-bits buffer,98* the cut-point between these two parts is floating99* (instead of fixed) with the bit shift counter CT.100* Thus, we also need only one (variable instead of101* fixed size) shift for the LPS/MPS decision, and102* we can do away with any renormalization update103* of C (except for new data insertion, of course).104*105* I've also introduced a new scheme for accessing106* the probability estimation state machine table,107* derived from Markus Kuhn's JBIG implementation.108*/109110LOCAL(int)111arith_decode (j_decompress_ptr cinfo, unsigned char *st)112{113register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;114register unsigned char nl, nm;115register JLONG qe, temp;116register int sv, data;117118/* Renormalization & data input per section D.2.6 */119while (e->a < 0x8000L) {120if (--e->ct < 0) {121/* Need to fetch next data byte */122if (cinfo->unread_marker)123data = 0; /* stuff zero data */124else {125data = get_byte(cinfo); /* read next input byte */126if (data == 0xFF) { /* zero stuff or marker code */127do data = get_byte(cinfo);128while (data == 0xFF); /* swallow extra 0xFF bytes */129if (data == 0)130data = 0xFF; /* discard stuffed zero byte */131else {132/* Note: Different from the Huffman decoder, hitting133* a marker while processing the compressed data134* segment is legal in arithmetic coding.135* The convention is to supply zero data136* then until decoding is complete.137*/138cinfo->unread_marker = data;139data = 0;140}141}142}143e->c = (e->c << 8) | data; /* insert data into C register */144if ((e->ct += 8) < 0) /* update bit shift counter */145/* Need more initial bytes */146if (++e->ct == 0)147/* Got 2 initial bytes -> re-init A and exit loop */148e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */149}150e->a <<= 1;151}152153/* Fetch values from our compact representation of Table D.2:154* Qe values and probability estimation state machine155*/156sv = *st;157qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */158nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */159nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */160161/* Decode & estimation procedures per sections D.2.4 & D.2.5 */162temp = e->a - qe;163e->a = temp;164temp <<= e->ct;165if (e->c >= temp) {166e->c -= temp;167/* Conditional LPS (less probable symbol) exchange */168if (e->a < qe) {169e->a = qe;170*st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */171} else {172e->a = qe;173*st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */174sv ^= 0x80; /* Exchange LPS/MPS */175}176} else if (e->a < 0x8000L) {177/* Conditional MPS (more probable symbol) exchange */178if (e->a < qe) {179*st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */180sv ^= 0x80; /* Exchange LPS/MPS */181} else {182*st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */183}184}185186return sv >> 7;187}188189190/*191* Check for a restart marker & resynchronize decoder.192*/193194LOCAL(void)195process_restart (j_decompress_ptr cinfo)196{197arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;198int ci;199jpeg_component_info *compptr;200201/* Advance past the RSTn marker */202if (! (*cinfo->marker->read_restart_marker) (cinfo))203ERREXIT(cinfo, JERR_CANT_SUSPEND);204205/* Re-initialize statistics areas */206for (ci = 0; ci < cinfo->comps_in_scan; ci++) {207compptr = cinfo->cur_comp_info[ci];208if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {209MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);210/* Reset DC predictions to 0 */211entropy->last_dc_val[ci] = 0;212entropy->dc_context[ci] = 0;213}214if (!cinfo->progressive_mode || cinfo->Ss) {215MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);216}217}218219/* Reset arithmetic decoding variables */220entropy->c = 0;221entropy->a = 0;222entropy->ct = -16; /* force reading 2 initial bytes to fill C */223224/* Reset restart counter */225entropy->restarts_to_go = cinfo->restart_interval;226}227228229/*230* Arithmetic MCU decoding.231* Each of these routines decodes and returns one MCU's worth of232* arithmetic-compressed coefficients.233* The coefficients are reordered from zigzag order into natural array order,234* but are not dequantized.235*236* The i'th block of the MCU is stored into the block pointed to by237* MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.238*/239240/*241* MCU decoding for DC initial scan (either spectral selection,242* or first pass of successive approximation).243*/244245METHODDEF(boolean)246decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)247{248arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;249JBLOCKROW block;250unsigned char *st;251int blkn, ci, tbl, sign;252int v, m;253254/* Process restart marker if needed */255if (cinfo->restart_interval) {256if (entropy->restarts_to_go == 0)257process_restart(cinfo);258entropy->restarts_to_go--;259}260261if (entropy->ct == -1) return TRUE; /* if error do nothing */262263/* Outer loop handles each block in the MCU */264265for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {266block = MCU_data[blkn];267ci = cinfo->MCU_membership[blkn];268tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;269270/* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */271272/* Table F.4: Point to statistics bin S0 for DC coefficient coding */273st = entropy->dc_stats[tbl] + entropy->dc_context[ci];274275/* Figure F.19: Decode_DC_DIFF */276if (arith_decode(cinfo, st) == 0)277entropy->dc_context[ci] = 0;278else {279/* Figure F.21: Decoding nonzero value v */280/* Figure F.22: Decoding the sign of v */281sign = arith_decode(cinfo, st + 1);282st += 2; st += sign;283/* Figure F.23: Decoding the magnitude category of v */284if ((m = arith_decode(cinfo, st)) != 0) {285st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */286while (arith_decode(cinfo, st)) {287if ((m <<= 1) == 0x8000) {288WARNMS(cinfo, JWRN_ARITH_BAD_CODE);289entropy->ct = -1; /* magnitude overflow */290return TRUE;291}292st += 1;293}294}295/* Section F.1.4.4.1.2: Establish dc_context conditioning category */296if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))297entropy->dc_context[ci] = 0; /* zero diff category */298else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))299entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */300else301entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */302v = m;303/* Figure F.24: Decoding the magnitude bit pattern of v */304st += 14;305while (m >>= 1)306if (arith_decode(cinfo, st)) v |= m;307v += 1; if (sign) v = -v;308entropy->last_dc_val[ci] += v;309}310311/* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */312(*block)[0] = (JCOEF) LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al);313}314315return TRUE;316}317318319/*320* MCU decoding for AC initial scan (either spectral selection,321* or first pass of successive approximation).322*/323324METHODDEF(boolean)325decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)326{327arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;328JBLOCKROW block;329unsigned char *st;330int tbl, sign, k;331int v, m;332333/* Process restart marker if needed */334if (cinfo->restart_interval) {335if (entropy->restarts_to_go == 0)336process_restart(cinfo);337entropy->restarts_to_go--;338}339340if (entropy->ct == -1) return TRUE; /* if error do nothing */341342/* There is always only one block per MCU */343block = MCU_data[0];344tbl = cinfo->cur_comp_info[0]->ac_tbl_no;345346/* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */347348/* Figure F.20: Decode_AC_coefficients */349for (k = cinfo->Ss; k <= cinfo->Se; k++) {350st = entropy->ac_stats[tbl] + 3 * (k - 1);351if (arith_decode(cinfo, st)) break; /* EOB flag */352while (arith_decode(cinfo, st + 1) == 0) {353st += 3; k++;354if (k > cinfo->Se) {355WARNMS(cinfo, JWRN_ARITH_BAD_CODE);356entropy->ct = -1; /* spectral overflow */357return TRUE;358}359}360/* Figure F.21: Decoding nonzero value v */361/* Figure F.22: Decoding the sign of v */362sign = arith_decode(cinfo, entropy->fixed_bin);363st += 2;364/* Figure F.23: Decoding the magnitude category of v */365if ((m = arith_decode(cinfo, st)) != 0) {366if (arith_decode(cinfo, st)) {367m <<= 1;368st = entropy->ac_stats[tbl] +369(k <= cinfo->arith_ac_K[tbl] ? 189 : 217);370while (arith_decode(cinfo, st)) {371if ((m <<= 1) == 0x8000) {372WARNMS(cinfo, JWRN_ARITH_BAD_CODE);373entropy->ct = -1; /* magnitude overflow */374return TRUE;375}376st += 1;377}378}379}380v = m;381/* Figure F.24: Decoding the magnitude bit pattern of v */382st += 14;383while (m >>= 1)384if (arith_decode(cinfo, st)) v |= m;385v += 1; if (sign) v = -v;386/* Scale and output coefficient in natural (dezigzagged) order */387(*block)[jpeg_natural_order[k]] = (JCOEF) ((unsigned)v << cinfo->Al);388}389390return TRUE;391}392393394/*395* MCU decoding for DC successive approximation refinement scan.396*/397398METHODDEF(boolean)399decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)400{401arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;402unsigned char *st;403int p1, blkn;404405/* Process restart marker if needed */406if (cinfo->restart_interval) {407if (entropy->restarts_to_go == 0)408process_restart(cinfo);409entropy->restarts_to_go--;410}411412st = entropy->fixed_bin; /* use fixed probability estimation */413p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */414415/* Outer loop handles each block in the MCU */416417for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {418/* Encoded data is simply the next bit of the two's-complement DC value */419if (arith_decode(cinfo, st))420MCU_data[blkn][0][0] |= p1;421}422423return TRUE;424}425426427/*428* MCU decoding for AC successive approximation refinement scan.429*/430431METHODDEF(boolean)432decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)433{434arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;435JBLOCKROW block;436JCOEFPTR thiscoef;437unsigned char *st;438int tbl, k, kex;439int p1, m1;440441/* Process restart marker if needed */442if (cinfo->restart_interval) {443if (entropy->restarts_to_go == 0)444process_restart(cinfo);445entropy->restarts_to_go--;446}447448if (entropy->ct == -1) return TRUE; /* if error do nothing */449450/* There is always only one block per MCU */451block = MCU_data[0];452tbl = cinfo->cur_comp_info[0]->ac_tbl_no;453454p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */455m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */456457/* Establish EOBx (previous stage end-of-block) index */458for (kex = cinfo->Se; kex > 0; kex--)459if ((*block)[jpeg_natural_order[kex]]) break;460461for (k = cinfo->Ss; k <= cinfo->Se; k++) {462st = entropy->ac_stats[tbl] + 3 * (k - 1);463if (k > kex)464if (arith_decode(cinfo, st)) break; /* EOB flag */465for (;;) {466thiscoef = *block + jpeg_natural_order[k];467if (*thiscoef) { /* previously nonzero coef */468if (arith_decode(cinfo, st + 2)) {469if (*thiscoef < 0)470*thiscoef += m1;471else472*thiscoef += p1;473}474break;475}476if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */477if (arith_decode(cinfo, entropy->fixed_bin))478*thiscoef = m1;479else480*thiscoef = p1;481break;482}483st += 3; k++;484if (k > cinfo->Se) {485WARNMS(cinfo, JWRN_ARITH_BAD_CODE);486entropy->ct = -1; /* spectral overflow */487return TRUE;488}489}490}491492return TRUE;493}494495496/*497* Decode one MCU's worth of arithmetic-compressed coefficients.498*/499500METHODDEF(boolean)501decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)502{503arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;504jpeg_component_info *compptr;505JBLOCKROW block;506unsigned char *st;507int blkn, ci, tbl, sign, k;508int v, m;509510/* Process restart marker if needed */511if (cinfo->restart_interval) {512if (entropy->restarts_to_go == 0)513process_restart(cinfo);514entropy->restarts_to_go--;515}516517if (entropy->ct == -1) return TRUE; /* if error do nothing */518519/* Outer loop handles each block in the MCU */520521for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {522block = MCU_data ? MCU_data[blkn] : NULL;523ci = cinfo->MCU_membership[blkn];524compptr = cinfo->cur_comp_info[ci];525526/* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */527528tbl = compptr->dc_tbl_no;529530/* Table F.4: Point to statistics bin S0 for DC coefficient coding */531st = entropy->dc_stats[tbl] + entropy->dc_context[ci];532533/* Figure F.19: Decode_DC_DIFF */534if (arith_decode(cinfo, st) == 0)535entropy->dc_context[ci] = 0;536else {537/* Figure F.21: Decoding nonzero value v */538/* Figure F.22: Decoding the sign of v */539sign = arith_decode(cinfo, st + 1);540st += 2; st += sign;541/* Figure F.23: Decoding the magnitude category of v */542if ((m = arith_decode(cinfo, st)) != 0) {543st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */544while (arith_decode(cinfo, st)) {545if ((m <<= 1) == 0x8000) {546WARNMS(cinfo, JWRN_ARITH_BAD_CODE);547entropy->ct = -1; /* magnitude overflow */548return TRUE;549}550st += 1;551}552}553/* Section F.1.4.4.1.2: Establish dc_context conditioning category */554if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))555entropy->dc_context[ci] = 0; /* zero diff category */556else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))557entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */558else559entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */560v = m;561/* Figure F.24: Decoding the magnitude bit pattern of v */562st += 14;563while (m >>= 1)564if (arith_decode(cinfo, st)) v |= m;565v += 1; if (sign) v = -v;566entropy->last_dc_val[ci] += v;567}568569if (block)570(*block)[0] = (JCOEF) entropy->last_dc_val[ci];571572/* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */573574tbl = compptr->ac_tbl_no;575576/* Figure F.20: Decode_AC_coefficients */577for (k = 1; k <= DCTSIZE2 - 1; k++) {578st = entropy->ac_stats[tbl] + 3 * (k - 1);579if (arith_decode(cinfo, st)) break; /* EOB flag */580while (arith_decode(cinfo, st + 1) == 0) {581st += 3; k++;582if (k > DCTSIZE2 - 1) {583WARNMS(cinfo, JWRN_ARITH_BAD_CODE);584entropy->ct = -1; /* spectral overflow */585return TRUE;586}587}588/* Figure F.21: Decoding nonzero value v */589/* Figure F.22: Decoding the sign of v */590sign = arith_decode(cinfo, entropy->fixed_bin);591st += 2;592/* Figure F.23: Decoding the magnitude category of v */593if ((m = arith_decode(cinfo, st)) != 0) {594if (arith_decode(cinfo, st)) {595m <<= 1;596st = entropy->ac_stats[tbl] +597(k <= cinfo->arith_ac_K[tbl] ? 189 : 217);598while (arith_decode(cinfo, st)) {599if ((m <<= 1) == 0x8000) {600WARNMS(cinfo, JWRN_ARITH_BAD_CODE);601entropy->ct = -1; /* magnitude overflow */602return TRUE;603}604st += 1;605}606}607}608v = m;609/* Figure F.24: Decoding the magnitude bit pattern of v */610st += 14;611while (m >>= 1)612if (arith_decode(cinfo, st)) v |= m;613v += 1; if (sign) v = -v;614if (block)615(*block)[jpeg_natural_order[k]] = (JCOEF) v;616}617}618619return TRUE;620}621622623/*624* Initialize for an arithmetic-compressed scan.625*/626627METHODDEF(void)628start_pass (j_decompress_ptr cinfo)629{630arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;631int ci, tbl;632jpeg_component_info *compptr;633634if (cinfo->progressive_mode) {635/* Validate progressive scan parameters */636if (cinfo->Ss == 0) {637if (cinfo->Se != 0)638goto bad;639} else {640/* need not check Ss/Se < 0 since they came from unsigned bytes */641if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)642goto bad;643/* AC scans may have only one component */644if (cinfo->comps_in_scan != 1)645goto bad;646}647if (cinfo->Ah != 0) {648/* Successive approximation refinement scan: must have Al = Ah-1. */649if (cinfo->Ah-1 != cinfo->Al)650goto bad;651}652if (cinfo->Al > 13) { /* need not check for < 0 */653bad:654ERREXIT4(cinfo, JERR_BAD_PROGRESSION,655cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);656}657/* Update progression status, and verify that scan order is legal.658* Note that inter-scan inconsistencies are treated as warnings659* not fatal errors ... not clear if this is right way to behave.660*/661for (ci = 0; ci < cinfo->comps_in_scan; ci++) {662int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;663int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];664if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */665WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);666for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {667int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];668if (cinfo->Ah != expected)669WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);670coef_bit_ptr[coefi] = cinfo->Al;671}672}673/* Select MCU decoding routine */674if (cinfo->Ah == 0) {675if (cinfo->Ss == 0)676entropy->pub.decode_mcu = decode_mcu_DC_first;677else678entropy->pub.decode_mcu = decode_mcu_AC_first;679} else {680if (cinfo->Ss == 0)681entropy->pub.decode_mcu = decode_mcu_DC_refine;682else683entropy->pub.decode_mcu = decode_mcu_AC_refine;684}685} else {686/* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.687* This ought to be an error condition, but we make it a warning.688*/689if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||690(cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1))691WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);692/* Select MCU decoding routine */693entropy->pub.decode_mcu = decode_mcu;694}695696/* Allocate & initialize requested statistics areas */697for (ci = 0; ci < cinfo->comps_in_scan; ci++) {698compptr = cinfo->cur_comp_info[ci];699if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {700tbl = compptr->dc_tbl_no;701if (tbl < 0 || tbl >= NUM_ARITH_TBLS)702ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);703if (entropy->dc_stats[tbl] == NULL)704entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)705((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);706MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);707/* Initialize DC predictions to 0 */708entropy->last_dc_val[ci] = 0;709entropy->dc_context[ci] = 0;710}711if (!cinfo->progressive_mode || cinfo->Ss) {712tbl = compptr->ac_tbl_no;713if (tbl < 0 || tbl >= NUM_ARITH_TBLS)714ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);715if (entropy->ac_stats[tbl] == NULL)716entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)717((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);718MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);719}720}721722/* Initialize arithmetic decoding variables */723entropy->c = 0;724entropy->a = 0;725entropy->ct = -16; /* force reading 2 initial bytes to fill C */726727/* Initialize restart counter */728entropy->restarts_to_go = cinfo->restart_interval;729}730731732/*733* Module initialization routine for arithmetic entropy decoding.734*/735736GLOBAL(void)737jinit_arith_decoder (j_decompress_ptr cinfo)738{739arith_entropy_ptr entropy;740int i;741742entropy = (arith_entropy_ptr)743(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,744sizeof(arith_entropy_decoder));745cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;746entropy->pub.start_pass = start_pass;747748/* Mark tables unallocated */749for (i = 0; i < NUM_ARITH_TBLS; i++) {750entropy->dc_stats[i] = NULL;751entropy->ac_stats[i] = NULL;752}753754/* Initialize index for fixed probability estimation */755entropy->fixed_bin[0] = 113;756757if (cinfo->progressive_mode) {758/* Create progression status table */759int *coef_bit_ptr, ci;760cinfo->coef_bits = (int (*)[DCTSIZE2])761(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,762cinfo->num_components*DCTSIZE2*sizeof(int));763coef_bit_ptr = & cinfo->coef_bits[0][0];764for (ci = 0; ci < cinfo->num_components; ci++)765for (i = 0; i < DCTSIZE2; i++)766*coef_bit_ptr++ = -1;767}768}769770771