Path: blob/master/thirdparty/libjpeg-turbo/src/jdarith.c
9904 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-2020, 2022, D. R. Commander.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains portable arithmetic entropy encoding routines for JPEG11* (implementing Recommendation ITU-T T.81 | ISO/IEC 10918-1).12*13* Both sequential and progressive modes are supported in this single module.14*15* Suspension is not currently supported in this module.16*17* NOTE: All referenced figures are from18* Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.19*/2021#define JPEG_INTERNALS22#include "jinclude.h"23#include "jpeglib.h"242526#define NEG_1 ((unsigned int)-1)272829/* Expanded entropy decoder object for arithmetic decoding. */3031typedef struct {32struct jpeg_entropy_decoder pub; /* public fields */3334JLONG c; /* C register, base of coding interval + input bit buffer */35JLONG a; /* A register, normalized size of coding interval */36int ct; /* bit shift counter, # of bits left in bit buffer part of C */37/* init: ct = -16 */38/* run: ct = 0..7 */39/* error: ct = -1 */40int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */41int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */4243unsigned int restarts_to_go; /* MCUs left in this restart interval */4445/* Pointers to statistics areas (these workspaces have image lifespan) */46unsigned char *dc_stats[NUM_ARITH_TBLS];47unsigned char *ac_stats[NUM_ARITH_TBLS];4849/* Statistics bin for coding with fixed probability 0.5 */50unsigned char fixed_bin[4];51} arith_entropy_decoder;5253typedef arith_entropy_decoder *arith_entropy_ptr;5455/* The following two definitions specify the allocation chunk size56* for the statistics area.57* According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least58* 49 statistics bins for DC, and 245 statistics bins for AC coding.59*60* We use a compact representation with 1 byte per statistics bin,61* thus the numbers directly represent byte sizes.62* This 1 byte per statistics bin contains the meaning of the MPS63* (more probable symbol) in the highest bit (mask 0x80), and the64* index into the probability estimation state machine table65* in the lower bits (mask 0x7F).66*/6768#define DC_STAT_BINS 6469#define AC_STAT_BINS 256707172LOCAL(int)73get_byte(j_decompress_ptr cinfo)74/* Read next input byte; we do not support suspension in this module. */75{76struct jpeg_source_mgr *src = cinfo->src;7778if (src->bytes_in_buffer == 0)79if (!(*src->fill_input_buffer) (cinfo))80ERREXIT(cinfo, JERR_CANT_SUSPEND);81src->bytes_in_buffer--;82return *src->next_input_byte++;83}848586/*87* The core arithmetic decoding routine (common in JPEG and JBIG).88* This needs to go as fast as possible.89* Machine-dependent optimization facilities90* are not utilized in this portable implementation.91* However, this code should be fairly efficient and92* may be a good base for further optimizations anyway.93*94* Return value is 0 or 1 (binary decision).95*96* Note: I've changed the handling of the code base & bit97* buffer register C compared to other implementations98* based on the standards layout & procedures.99* While it also contains both the actual base of the100* coding interval (16 bits) and the next-bits buffer,101* the cut-point between these two parts is floating102* (instead of fixed) with the bit shift counter CT.103* Thus, we also need only one (variable instead of104* fixed size) shift for the LPS/MPS decision, and105* we can do away with any renormalization update106* of C (except for new data insertion, of course).107*108* I've also introduced a new scheme for accessing109* the probability estimation state machine table,110* derived from Markus Kuhn's JBIG implementation.111*/112113LOCAL(int)114arith_decode(j_decompress_ptr cinfo, unsigned char *st)115{116register arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy;117register unsigned char nl, nm;118register JLONG qe, temp;119register int sv, data;120121/* Renormalization & data input per section D.2.6 */122while (e->a < 0x8000L) {123if (--e->ct < 0) {124/* Need to fetch next data byte */125if (cinfo->unread_marker)126data = 0; /* stuff zero data */127else {128data = get_byte(cinfo); /* read next input byte */129if (data == 0xFF) { /* zero stuff or marker code */130do data = get_byte(cinfo);131while (data == 0xFF); /* swallow extra 0xFF bytes */132if (data == 0)133data = 0xFF; /* discard stuffed zero byte */134else {135/* Note: Different from the Huffman decoder, hitting136* a marker while processing the compressed data137* segment is legal in arithmetic coding.138* The convention is to supply zero data139* then until decoding is complete.140*/141cinfo->unread_marker = data;142data = 0;143}144}145}146e->c = (e->c << 8) | data; /* insert data into C register */147if ((e->ct += 8) < 0) /* update bit shift counter */148/* Need more initial bytes */149if (++e->ct == 0)150/* Got 2 initial bytes -> re-init A and exit loop */151e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */152}153e->a <<= 1;154}155156/* Fetch values from our compact representation of Table D.2:157* Qe values and probability estimation state machine158*/159sv = *st;160qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */161nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */162nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */163164/* Decode & estimation procedures per sections D.2.4 & D.2.5 */165temp = e->a - qe;166e->a = temp;167temp <<= e->ct;168if (e->c >= temp) {169e->c -= temp;170/* Conditional LPS (less probable symbol) exchange */171if (e->a < qe) {172e->a = qe;173*st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */174} else {175e->a = qe;176*st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */177sv ^= 0x80; /* Exchange LPS/MPS */178}179} else if (e->a < 0x8000L) {180/* Conditional MPS (more probable symbol) exchange */181if (e->a < qe) {182*st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */183sv ^= 0x80; /* Exchange LPS/MPS */184} else {185*st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */186}187}188189return sv >> 7;190}191192193/*194* Check for a restart marker & resynchronize decoder.195*/196197LOCAL(void)198process_restart(j_decompress_ptr cinfo)199{200arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;201int ci;202jpeg_component_info *compptr;203204/* Advance past the RSTn marker */205if (!(*cinfo->marker->read_restart_marker) (cinfo))206ERREXIT(cinfo, JERR_CANT_SUSPEND);207208/* Re-initialize statistics areas */209for (ci = 0; ci < cinfo->comps_in_scan; ci++) {210compptr = cinfo->cur_comp_info[ci];211if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {212memset(entropy->dc_stats[compptr->dc_tbl_no], 0, DC_STAT_BINS);213/* Reset DC predictions to 0 */214entropy->last_dc_val[ci] = 0;215entropy->dc_context[ci] = 0;216}217if (!cinfo->progressive_mode || cinfo->Ss) {218memset(entropy->ac_stats[compptr->ac_tbl_no], 0, AC_STAT_BINS);219}220}221222/* Reset arithmetic decoding variables */223entropy->c = 0;224entropy->a = 0;225entropy->ct = -16; /* force reading 2 initial bytes to fill C */226227/* Reset restart counter */228entropy->restarts_to_go = cinfo->restart_interval;229}230231232/*233* Arithmetic MCU decoding.234* Each of these routines decodes and returns one MCU's worth of235* arithmetic-compressed coefficients.236* The coefficients are reordered from zigzag order into natural array order,237* but are not dequantized.238*239* The i'th block of the MCU is stored into the block pointed to by240* MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.241*/242243/*244* MCU decoding for DC initial scan (either spectral selection,245* or first pass of successive approximation).246*/247248METHODDEF(boolean)249decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)250{251arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;252JBLOCKROW block;253unsigned char *st;254int blkn, ci, tbl, sign;255int v, m;256257/* Process restart marker if needed */258if (cinfo->restart_interval) {259if (entropy->restarts_to_go == 0)260process_restart(cinfo);261entropy->restarts_to_go--;262}263264if (entropy->ct == -1) return TRUE; /* if error do nothing */265266/* Outer loop handles each block in the MCU */267268for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {269block = MCU_data[blkn];270ci = cinfo->MCU_membership[blkn];271tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;272273/* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */274275/* Table F.4: Point to statistics bin S0 for DC coefficient coding */276st = entropy->dc_stats[tbl] + entropy->dc_context[ci];277278/* Figure F.19: Decode_DC_DIFF */279if (arith_decode(cinfo, st) == 0)280entropy->dc_context[ci] = 0;281else {282/* Figure F.21: Decoding nonzero value v */283/* Figure F.22: Decoding the sign of v */284sign = arith_decode(cinfo, st + 1);285st += 2; st += sign;286/* Figure F.23: Decoding the magnitude category of v */287if ((m = arith_decode(cinfo, st)) != 0) {288st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */289while (arith_decode(cinfo, st)) {290if ((m <<= 1) == 0x8000) {291WARNMS(cinfo, JWRN_ARITH_BAD_CODE);292entropy->ct = -1; /* magnitude overflow */293return TRUE;294}295st += 1;296}297}298/* Section F.1.4.4.1.2: Establish dc_context conditioning category */299if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))300entropy->dc_context[ci] = 0; /* zero diff category */301else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))302entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */303else304entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */305v = m;306/* Figure F.24: Decoding the magnitude bit pattern of v */307st += 14;308while (m >>= 1)309if (arith_decode(cinfo, st)) v |= m;310v += 1; if (sign) v = -v;311entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;312}313314/* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */315(*block)[0] = (JCOEF)LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al);316}317318return TRUE;319}320321322/*323* MCU decoding for AC initial scan (either spectral selection,324* or first pass of successive approximation).325*/326327METHODDEF(boolean)328decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)329{330arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;331JBLOCKROW block;332unsigned char *st;333int tbl, sign, k;334int v, m;335336/* Process restart marker if needed */337if (cinfo->restart_interval) {338if (entropy->restarts_to_go == 0)339process_restart(cinfo);340entropy->restarts_to_go--;341}342343if (entropy->ct == -1) return TRUE; /* if error do nothing */344345/* There is always only one block per MCU */346block = MCU_data[0];347tbl = cinfo->cur_comp_info[0]->ac_tbl_no;348349/* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */350351/* Figure F.20: Decode_AC_coefficients */352for (k = cinfo->Ss; k <= cinfo->Se; k++) {353st = entropy->ac_stats[tbl] + 3 * (k - 1);354if (arith_decode(cinfo, st)) break; /* EOB flag */355while (arith_decode(cinfo, st + 1) == 0) {356st += 3; k++;357if (k > cinfo->Se) {358WARNMS(cinfo, JWRN_ARITH_BAD_CODE);359entropy->ct = -1; /* spectral overflow */360return TRUE;361}362}363/* Figure F.21: Decoding nonzero value v */364/* Figure F.22: Decoding the sign of v */365sign = arith_decode(cinfo, entropy->fixed_bin);366st += 2;367/* Figure F.23: Decoding the magnitude category of v */368if ((m = arith_decode(cinfo, st)) != 0) {369if (arith_decode(cinfo, st)) {370m <<= 1;371st = entropy->ac_stats[tbl] +372(k <= cinfo->arith_ac_K[tbl] ? 189 : 217);373while (arith_decode(cinfo, st)) {374if ((m <<= 1) == 0x8000) {375WARNMS(cinfo, JWRN_ARITH_BAD_CODE);376entropy->ct = -1; /* magnitude overflow */377return TRUE;378}379st += 1;380}381}382}383v = m;384/* Figure F.24: Decoding the magnitude bit pattern of v */385st += 14;386while (m >>= 1)387if (arith_decode(cinfo, st)) v |= m;388v += 1; if (sign) v = -v;389/* Scale and output coefficient in natural (dezigzagged) order */390(*block)[jpeg_natural_order[k]] = (JCOEF)((unsigned)v << cinfo->Al);391}392393return TRUE;394}395396397/*398* MCU decoding for DC successive approximation refinement scan.399*/400401METHODDEF(boolean)402decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)403{404arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;405unsigned char *st;406int p1, blkn;407408/* Process restart marker if needed */409if (cinfo->restart_interval) {410if (entropy->restarts_to_go == 0)411process_restart(cinfo);412entropy->restarts_to_go--;413}414415st = entropy->fixed_bin; /* use fixed probability estimation */416p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */417418/* Outer loop handles each block in the MCU */419420for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {421/* Encoded data is simply the next bit of the two's-complement DC value */422if (arith_decode(cinfo, st))423MCU_data[blkn][0][0] |= p1;424}425426return TRUE;427}428429430/*431* MCU decoding for AC successive approximation refinement scan.432*/433434METHODDEF(boolean)435decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)436{437arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;438JBLOCKROW block;439JCOEFPTR thiscoef;440unsigned char *st;441int tbl, k, kex;442int p1, m1;443444/* Process restart marker if needed */445if (cinfo->restart_interval) {446if (entropy->restarts_to_go == 0)447process_restart(cinfo);448entropy->restarts_to_go--;449}450451if (entropy->ct == -1) return TRUE; /* if error do nothing */452453/* There is always only one block per MCU */454block = MCU_data[0];455tbl = cinfo->cur_comp_info[0]->ac_tbl_no;456457p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */458m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */459460/* Establish EOBx (previous stage end-of-block) index */461for (kex = cinfo->Se; kex > 0; kex--)462if ((*block)[jpeg_natural_order[kex]]) break;463464for (k = cinfo->Ss; k <= cinfo->Se; k++) {465st = entropy->ac_stats[tbl] + 3 * (k - 1);466if (k > kex)467if (arith_decode(cinfo, st)) break; /* EOB flag */468for (;;) {469thiscoef = *block + jpeg_natural_order[k];470if (*thiscoef) { /* previously nonzero coef */471if (arith_decode(cinfo, st + 2)) {472if (*thiscoef < 0)473*thiscoef += (JCOEF)m1;474else475*thiscoef += (JCOEF)p1;476}477break;478}479if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */480if (arith_decode(cinfo, entropy->fixed_bin))481*thiscoef = (JCOEF)m1;482else483*thiscoef = (JCOEF)p1;484break;485}486st += 3; k++;487if (k > cinfo->Se) {488WARNMS(cinfo, JWRN_ARITH_BAD_CODE);489entropy->ct = -1; /* spectral overflow */490return TRUE;491}492}493}494495return TRUE;496}497498499/*500* Decode one MCU's worth of arithmetic-compressed coefficients.501*/502503METHODDEF(boolean)504decode_mcu(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)505{506arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;507jpeg_component_info *compptr;508JBLOCKROW block;509unsigned char *st;510int blkn, ci, tbl, sign, k;511int v, m;512513/* Process restart marker if needed */514if (cinfo->restart_interval) {515if (entropy->restarts_to_go == 0)516process_restart(cinfo);517entropy->restarts_to_go--;518}519520if (entropy->ct == -1) return TRUE; /* if error do nothing */521522/* Outer loop handles each block in the MCU */523524for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {525block = MCU_data ? MCU_data[blkn] : NULL;526ci = cinfo->MCU_membership[blkn];527compptr = cinfo->cur_comp_info[ci];528529/* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */530531tbl = compptr->dc_tbl_no;532533/* Table F.4: Point to statistics bin S0 for DC coefficient coding */534st = entropy->dc_stats[tbl] + entropy->dc_context[ci];535536/* Figure F.19: Decode_DC_DIFF */537if (arith_decode(cinfo, st) == 0)538entropy->dc_context[ci] = 0;539else {540/* Figure F.21: Decoding nonzero value v */541/* Figure F.22: Decoding the sign of v */542sign = arith_decode(cinfo, st + 1);543st += 2; st += sign;544/* Figure F.23: Decoding the magnitude category of v */545if ((m = arith_decode(cinfo, st)) != 0) {546st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */547while (arith_decode(cinfo, st)) {548if ((m <<= 1) == 0x8000) {549WARNMS(cinfo, JWRN_ARITH_BAD_CODE);550entropy->ct = -1; /* magnitude overflow */551return TRUE;552}553st += 1;554}555}556/* Section F.1.4.4.1.2: Establish dc_context conditioning category */557if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))558entropy->dc_context[ci] = 0; /* zero diff category */559else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))560entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */561else562entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */563v = m;564/* Figure F.24: Decoding the magnitude bit pattern of v */565st += 14;566while (m >>= 1)567if (arith_decode(cinfo, st)) v |= m;568v += 1; if (sign) v = -v;569entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;570}571572if (block)573(*block)[0] = (JCOEF)entropy->last_dc_val[ci];574575/* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */576577tbl = compptr->ac_tbl_no;578579/* Figure F.20: Decode_AC_coefficients */580for (k = 1; k <= DCTSIZE2 - 1; k++) {581st = entropy->ac_stats[tbl] + 3 * (k - 1);582if (arith_decode(cinfo, st)) break; /* EOB flag */583while (arith_decode(cinfo, st + 1) == 0) {584st += 3; k++;585if (k > DCTSIZE2 - 1) {586WARNMS(cinfo, JWRN_ARITH_BAD_CODE);587entropy->ct = -1; /* spectral overflow */588return TRUE;589}590}591/* Figure F.21: Decoding nonzero value v */592/* Figure F.22: Decoding the sign of v */593sign = arith_decode(cinfo, entropy->fixed_bin);594st += 2;595/* Figure F.23: Decoding the magnitude category of v */596if ((m = arith_decode(cinfo, st)) != 0) {597if (arith_decode(cinfo, st)) {598m <<= 1;599st = entropy->ac_stats[tbl] +600(k <= cinfo->arith_ac_K[tbl] ? 189 : 217);601while (arith_decode(cinfo, st)) {602if ((m <<= 1) == 0x8000) {603WARNMS(cinfo, JWRN_ARITH_BAD_CODE);604entropy->ct = -1; /* magnitude overflow */605return TRUE;606}607st += 1;608}609}610}611v = m;612/* Figure F.24: Decoding the magnitude bit pattern of v */613st += 14;614while (m >>= 1)615if (arith_decode(cinfo, st)) v |= m;616v += 1; if (sign) v = -v;617if (block)618(*block)[jpeg_natural_order[k]] = (JCOEF)v;619}620}621622return TRUE;623}624625626/*627* Initialize for an arithmetic-compressed scan.628*/629630METHODDEF(void)631start_pass(j_decompress_ptr cinfo)632{633arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;634int ci, tbl;635jpeg_component_info *compptr;636637if (cinfo->progressive_mode) {638/* Validate progressive scan parameters */639if (cinfo->Ss == 0) {640if (cinfo->Se != 0)641goto bad;642} else {643/* need not check Ss/Se < 0 since they came from unsigned bytes */644if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)645goto bad;646/* AC scans may have only one component */647if (cinfo->comps_in_scan != 1)648goto bad;649}650if (cinfo->Ah != 0) {651/* Successive approximation refinement scan: must have Al = Ah-1. */652if (cinfo->Ah - 1 != cinfo->Al)653goto bad;654}655if (cinfo->Al > 13) { /* need not check for < 0 */656bad:657ERREXIT4(cinfo, JERR_BAD_PROGRESSION,658cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);659}660/* Update progression status, and verify that scan order is legal.661* Note that inter-scan inconsistencies are treated as warnings662* not fatal errors ... not clear if this is right way to behave.663*/664for (ci = 0; ci < cinfo->comps_in_scan; ci++) {665int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;666int *coef_bit_ptr = &cinfo->coef_bits[cindex][0];667int *prev_coef_bit_ptr =668&cinfo->coef_bits[cindex + cinfo->num_components][0];669if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */670WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);671for (coefi = MIN(cinfo->Ss, 1); coefi <= MAX(cinfo->Se, 9); coefi++) {672if (cinfo->input_scan_number > 1)673prev_coef_bit_ptr[coefi] = coef_bit_ptr[coefi];674else675prev_coef_bit_ptr[coefi] = 0;676}677for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {678int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];679if (cinfo->Ah != expected)680WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);681coef_bit_ptr[coefi] = cinfo->Al;682}683}684/* Select MCU decoding routine */685if (cinfo->Ah == 0) {686if (cinfo->Ss == 0)687entropy->pub.decode_mcu = decode_mcu_DC_first;688else689entropy->pub.decode_mcu = decode_mcu_AC_first;690} else {691if (cinfo->Ss == 0)692entropy->pub.decode_mcu = decode_mcu_DC_refine;693else694entropy->pub.decode_mcu = decode_mcu_AC_refine;695}696} else {697/* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.698* This ought to be an error condition, but we make it a warning.699*/700if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2 - 1 ||701cinfo->Ah != 0 || cinfo->Al != 0)702WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);703/* Select MCU decoding routine */704entropy->pub.decode_mcu = decode_mcu;705}706707/* Allocate & initialize requested statistics areas */708for (ci = 0; ci < cinfo->comps_in_scan; ci++) {709compptr = cinfo->cur_comp_info[ci];710if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {711tbl = compptr->dc_tbl_no;712if (tbl < 0 || tbl >= NUM_ARITH_TBLS)713ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);714if (entropy->dc_stats[tbl] == NULL)715entropy->dc_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)716((j_common_ptr)cinfo, JPOOL_IMAGE, DC_STAT_BINS);717memset(entropy->dc_stats[tbl], 0, DC_STAT_BINS);718/* Initialize DC predictions to 0 */719entropy->last_dc_val[ci] = 0;720entropy->dc_context[ci] = 0;721}722if (!cinfo->progressive_mode || cinfo->Ss) {723tbl = compptr->ac_tbl_no;724if (tbl < 0 || tbl >= NUM_ARITH_TBLS)725ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);726if (entropy->ac_stats[tbl] == NULL)727entropy->ac_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)728((j_common_ptr)cinfo, JPOOL_IMAGE, AC_STAT_BINS);729memset(entropy->ac_stats[tbl], 0, AC_STAT_BINS);730}731}732733/* Initialize arithmetic decoding variables */734entropy->c = 0;735entropy->a = 0;736entropy->ct = -16; /* force reading 2 initial bytes to fill C */737entropy->pub.insufficient_data = FALSE;738739/* Initialize restart counter */740entropy->restarts_to_go = cinfo->restart_interval;741}742743744/*745* Module initialization routine for arithmetic entropy decoding.746*/747748GLOBAL(void)749jinit_arith_decoder(j_decompress_ptr cinfo)750{751arith_entropy_ptr entropy;752int i;753754entropy = (arith_entropy_ptr)755(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,756sizeof(arith_entropy_decoder));757cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;758entropy->pub.start_pass = start_pass;759760/* Mark tables unallocated */761for (i = 0; i < NUM_ARITH_TBLS; i++) {762entropy->dc_stats[i] = NULL;763entropy->ac_stats[i] = NULL;764}765766/* Initialize index for fixed probability estimation */767entropy->fixed_bin[0] = 113;768769if (cinfo->progressive_mode) {770/* Create progression status table */771int *coef_bit_ptr, ci;772cinfo->coef_bits = (int (*)[DCTSIZE2])773(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,774cinfo->num_components * 2 * DCTSIZE2 *775sizeof(int));776coef_bit_ptr = &cinfo->coef_bits[0][0];777for (ci = 0; ci < cinfo->num_components; ci++)778for (i = 0; i < DCTSIZE2; i++)779*coef_bit_ptr++ = -1;780}781}782783784