123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773 |
- #define JPEG_INTERNALS
- #include "jinclude.h"
- #include "jpeglib.h"
- #define NEG_1 ((unsigned int)-1)
- typedef struct {
- struct jpeg_entropy_decoder pub;
- JLONG c;
- JLONG a;
- int ct;
-
-
-
- int last_dc_val[MAX_COMPS_IN_SCAN];
- int dc_context[MAX_COMPS_IN_SCAN];
- unsigned int restarts_to_go;
-
- unsigned char *dc_stats[NUM_ARITH_TBLS];
- unsigned char *ac_stats[NUM_ARITH_TBLS];
-
- unsigned char fixed_bin[4];
- } arith_entropy_decoder;
- typedef arith_entropy_decoder *arith_entropy_ptr;
- #define DC_STAT_BINS 64
- #define AC_STAT_BINS 256
- LOCAL(int)
- get_byte(j_decompress_ptr cinfo)
- {
- struct jpeg_source_mgr *src = cinfo->src;
- if (src->bytes_in_buffer == 0)
- if (!(*src->fill_input_buffer) (cinfo))
- ERREXIT(cinfo, JERR_CANT_SUSPEND);
- src->bytes_in_buffer--;
- return GETJOCTET(*src->next_input_byte++);
- }
- LOCAL(int)
- arith_decode(j_decompress_ptr cinfo, unsigned char *st)
- {
- register arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy;
- register unsigned char nl, nm;
- register JLONG qe, temp;
- register int sv, data;
-
- while (e->a < 0x8000L) {
- if (--e->ct < 0) {
-
- if (cinfo->unread_marker)
- data = 0;
- else {
- data = get_byte(cinfo);
- if (data == 0xFF) {
- do data = get_byte(cinfo);
- while (data == 0xFF);
- if (data == 0)
- data = 0xFF;
- else {
-
- cinfo->unread_marker = data;
- data = 0;
- }
- }
- }
- e->c = (e->c << 8) | data;
- if ((e->ct += 8) < 0)
-
- if (++e->ct == 0)
-
- e->a = 0x8000L;
- }
- e->a <<= 1;
- }
-
- sv = *st;
- qe = jpeg_aritab[sv & 0x7F];
- nl = qe & 0xFF; qe >>= 8;
- nm = qe & 0xFF; qe >>= 8;
-
- temp = e->a - qe;
- e->a = temp;
- temp <<= e->ct;
- if (e->c >= temp) {
- e->c -= temp;
-
- if (e->a < qe) {
- e->a = qe;
- *st = (sv & 0x80) ^ nm;
- } else {
- e->a = qe;
- *st = (sv & 0x80) ^ nl;
- sv ^= 0x80;
- }
- } else if (e->a < 0x8000L) {
-
- if (e->a < qe) {
- *st = (sv & 0x80) ^ nl;
- sv ^= 0x80;
- } else {
- *st = (sv & 0x80) ^ nm;
- }
- }
- return sv >> 7;
- }
- LOCAL(void)
- process_restart(j_decompress_ptr cinfo)
- {
- arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
- int ci;
- jpeg_component_info *compptr;
-
- if (!(*cinfo->marker->read_restart_marker) (cinfo))
- ERREXIT(cinfo, JERR_CANT_SUSPEND);
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
- MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
-
- entropy->last_dc_val[ci] = 0;
- entropy->dc_context[ci] = 0;
- }
- if (!cinfo->progressive_mode || cinfo->Ss) {
- MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
- }
- }
-
- entropy->c = 0;
- entropy->a = 0;
- entropy->ct = -16;
-
- entropy->restarts_to_go = cinfo->restart_interval;
- }
- METHODDEF(boolean)
- decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
- {
- arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
- JBLOCKROW block;
- unsigned char *st;
- int blkn, ci, tbl, sign;
- int v, m;
-
- if (cinfo->restart_interval) {
- if (entropy->restarts_to_go == 0)
- process_restart(cinfo);
- entropy->restarts_to_go--;
- }
- if (entropy->ct == -1) return TRUE;
-
- for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
- block = MCU_data[blkn];
- ci = cinfo->MCU_membership[blkn];
- tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
-
-
- st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
-
- if (arith_decode(cinfo, st) == 0)
- entropy->dc_context[ci] = 0;
- else {
-
-
- sign = arith_decode(cinfo, st + 1);
- st += 2; st += sign;
-
- if ((m = arith_decode(cinfo, st)) != 0) {
- st = entropy->dc_stats[tbl] + 20;
- while (arith_decode(cinfo, st)) {
- if ((m <<= 1) == 0x8000) {
- WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
- entropy->ct = -1;
- return TRUE;
- }
- st += 1;
- }
- }
-
- if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
- entropy->dc_context[ci] = 0;
- else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
- entropy->dc_context[ci] = 12 + (sign * 4);
- else
- entropy->dc_context[ci] = 4 + (sign * 4);
- v = m;
-
- st += 14;
- while (m >>= 1)
- if (arith_decode(cinfo, st)) v |= m;
- v += 1; if (sign) v = -v;
- entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;
- }
-
- (*block)[0] = (JCOEF)LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al);
- }
- return TRUE;
- }
- METHODDEF(boolean)
- decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
- {
- arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
- JBLOCKROW block;
- unsigned char *st;
- int tbl, sign, k;
- int v, m;
-
- if (cinfo->restart_interval) {
- if (entropy->restarts_to_go == 0)
- process_restart(cinfo);
- entropy->restarts_to_go--;
- }
- if (entropy->ct == -1) return TRUE;
-
- block = MCU_data[0];
- tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
-
-
- for (k = cinfo->Ss; k <= cinfo->Se; k++) {
- st = entropy->ac_stats[tbl] + 3 * (k - 1);
- if (arith_decode(cinfo, st)) break;
- while (arith_decode(cinfo, st + 1) == 0) {
- st += 3; k++;
- if (k > cinfo->Se) {
- WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
- entropy->ct = -1;
- return TRUE;
- }
- }
-
-
- sign = arith_decode(cinfo, entropy->fixed_bin);
- st += 2;
-
- if ((m = arith_decode(cinfo, st)) != 0) {
- if (arith_decode(cinfo, st)) {
- m <<= 1;
- st = entropy->ac_stats[tbl] +
- (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
- while (arith_decode(cinfo, st)) {
- if ((m <<= 1) == 0x8000) {
- WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
- entropy->ct = -1;
- return TRUE;
- }
- st += 1;
- }
- }
- }
- v = m;
-
- st += 14;
- while (m >>= 1)
- if (arith_decode(cinfo, st)) v |= m;
- v += 1; if (sign) v = -v;
-
- (*block)[jpeg_natural_order[k]] = (JCOEF)((unsigned)v << cinfo->Al);
- }
- return TRUE;
- }
- METHODDEF(boolean)
- decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
- {
- arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
- unsigned char *st;
- int p1, blkn;
-
- if (cinfo->restart_interval) {
- if (entropy->restarts_to_go == 0)
- process_restart(cinfo);
- entropy->restarts_to_go--;
- }
- st = entropy->fixed_bin;
- p1 = 1 << cinfo->Al;
-
- for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
-
- if (arith_decode(cinfo, st))
- MCU_data[blkn][0][0] |= p1;
- }
- return TRUE;
- }
- METHODDEF(boolean)
- decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
- {
- arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
- JBLOCKROW block;
- JCOEFPTR thiscoef;
- unsigned char *st;
- int tbl, k, kex;
- int p1, m1;
-
- if (cinfo->restart_interval) {
- if (entropy->restarts_to_go == 0)
- process_restart(cinfo);
- entropy->restarts_to_go--;
- }
- if (entropy->ct == -1) return TRUE;
-
- block = MCU_data[0];
- tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
- p1 = 1 << cinfo->Al;
- m1 = (NEG_1) << cinfo->Al;
-
- for (kex = cinfo->Se; kex > 0; kex--)
- if ((*block)[jpeg_natural_order[kex]]) break;
- for (k = cinfo->Ss; k <= cinfo->Se; k++) {
- st = entropy->ac_stats[tbl] + 3 * (k - 1);
- if (k > kex)
- if (arith_decode(cinfo, st)) break;
- for (;;) {
- thiscoef = *block + jpeg_natural_order[k];
- if (*thiscoef) {
- if (arith_decode(cinfo, st + 2)) {
- if (*thiscoef < 0)
- *thiscoef += m1;
- else
- *thiscoef += p1;
- }
- break;
- }
- if (arith_decode(cinfo, st + 1)) {
- if (arith_decode(cinfo, entropy->fixed_bin))
- *thiscoef = m1;
- else
- *thiscoef = p1;
- break;
- }
- st += 3; k++;
- if (k > cinfo->Se) {
- WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
- entropy->ct = -1;
- return TRUE;
- }
- }
- }
- return TRUE;
- }
- METHODDEF(boolean)
- decode_mcu(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
- {
- arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
- jpeg_component_info *compptr;
- JBLOCKROW block;
- unsigned char *st;
- int blkn, ci, tbl, sign, k;
- int v, m;
-
- if (cinfo->restart_interval) {
- if (entropy->restarts_to_go == 0)
- process_restart(cinfo);
- entropy->restarts_to_go--;
- }
- if (entropy->ct == -1) return TRUE;
-
- for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
- block = MCU_data ? MCU_data[blkn] : NULL;
- ci = cinfo->MCU_membership[blkn];
- compptr = cinfo->cur_comp_info[ci];
-
- tbl = compptr->dc_tbl_no;
-
- st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
-
- if (arith_decode(cinfo, st) == 0)
- entropy->dc_context[ci] = 0;
- else {
-
-
- sign = arith_decode(cinfo, st + 1);
- st += 2; st += sign;
-
- if ((m = arith_decode(cinfo, st)) != 0) {
- st = entropy->dc_stats[tbl] + 20;
- while (arith_decode(cinfo, st)) {
- if ((m <<= 1) == 0x8000) {
- WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
- entropy->ct = -1;
- return TRUE;
- }
- st += 1;
- }
- }
-
- if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
- entropy->dc_context[ci] = 0;
- else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
- entropy->dc_context[ci] = 12 + (sign * 4);
- else
- entropy->dc_context[ci] = 4 + (sign * 4);
- v = m;
-
- st += 14;
- while (m >>= 1)
- if (arith_decode(cinfo, st)) v |= m;
- v += 1; if (sign) v = -v;
- entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;
- }
- if (block)
- (*block)[0] = (JCOEF)entropy->last_dc_val[ci];
-
- tbl = compptr->ac_tbl_no;
-
- for (k = 1; k <= DCTSIZE2 - 1; k++) {
- st = entropy->ac_stats[tbl] + 3 * (k - 1);
- if (arith_decode(cinfo, st)) break;
- while (arith_decode(cinfo, st + 1) == 0) {
- st += 3; k++;
- if (k > DCTSIZE2 - 1) {
- WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
- entropy->ct = -1;
- return TRUE;
- }
- }
-
-
- sign = arith_decode(cinfo, entropy->fixed_bin);
- st += 2;
-
- if ((m = arith_decode(cinfo, st)) != 0) {
- if (arith_decode(cinfo, st)) {
- m <<= 1;
- st = entropy->ac_stats[tbl] +
- (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
- while (arith_decode(cinfo, st)) {
- if ((m <<= 1) == 0x8000) {
- WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
- entropy->ct = -1;
- return TRUE;
- }
- st += 1;
- }
- }
- }
- v = m;
-
- st += 14;
- while (m >>= 1)
- if (arith_decode(cinfo, st)) v |= m;
- v += 1; if (sign) v = -v;
- if (block)
- (*block)[jpeg_natural_order[k]] = (JCOEF)v;
- }
- }
- return TRUE;
- }
- METHODDEF(void)
- start_pass(j_decompress_ptr cinfo)
- {
- arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
- int ci, tbl;
- jpeg_component_info *compptr;
- if (cinfo->progressive_mode) {
-
- if (cinfo->Ss == 0) {
- if (cinfo->Se != 0)
- goto bad;
- } else {
-
- if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)
- goto bad;
-
- if (cinfo->comps_in_scan != 1)
- goto bad;
- }
- if (cinfo->Ah != 0) {
-
- if (cinfo->Ah - 1 != cinfo->Al)
- goto bad;
- }
- if (cinfo->Al > 13) {
- bad:
- ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
- cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
- }
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
- int *coef_bit_ptr = &cinfo->coef_bits[cindex][0];
- if (cinfo->Ss && coef_bit_ptr[0] < 0)
- WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
- for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
- int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
- if (cinfo->Ah != expected)
- WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
- coef_bit_ptr[coefi] = cinfo->Al;
- }
- }
-
- if (cinfo->Ah == 0) {
- if (cinfo->Ss == 0)
- entropy->pub.decode_mcu = decode_mcu_DC_first;
- else
- entropy->pub.decode_mcu = decode_mcu_AC_first;
- } else {
- if (cinfo->Ss == 0)
- entropy->pub.decode_mcu = decode_mcu_DC_refine;
- else
- entropy->pub.decode_mcu = decode_mcu_AC_refine;
- }
- } else {
-
- if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
- (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1))
- WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
-
- entropy->pub.decode_mcu = decode_mcu;
- }
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
- tbl = compptr->dc_tbl_no;
- if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
- ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
- if (entropy->dc_stats[tbl] == NULL)
- entropy->dc_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)
- ((j_common_ptr)cinfo, JPOOL_IMAGE, DC_STAT_BINS);
- MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
-
- entropy->last_dc_val[ci] = 0;
- entropy->dc_context[ci] = 0;
- }
- if (!cinfo->progressive_mode || cinfo->Ss) {
- tbl = compptr->ac_tbl_no;
- if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
- ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
- if (entropy->ac_stats[tbl] == NULL)
- entropy->ac_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)
- ((j_common_ptr)cinfo, JPOOL_IMAGE, AC_STAT_BINS);
- MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
- }
- }
-
- entropy->c = 0;
- entropy->a = 0;
- entropy->ct = -16;
-
- entropy->restarts_to_go = cinfo->restart_interval;
- }
- GLOBAL(void)
- jinit_arith_decoder(j_decompress_ptr cinfo)
- {
- arith_entropy_ptr entropy;
- int i;
- entropy = (arith_entropy_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
- sizeof(arith_entropy_decoder));
- cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
- entropy->pub.start_pass = start_pass;
-
- for (i = 0; i < NUM_ARITH_TBLS; i++) {
- entropy->dc_stats[i] = NULL;
- entropy->ac_stats[i] = NULL;
- }
-
- entropy->fixed_bin[0] = 113;
- if (cinfo->progressive_mode) {
-
- int *coef_bit_ptr, ci;
- cinfo->coef_bits = (int (*)[DCTSIZE2])
- (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
- cinfo->num_components * DCTSIZE2 *
- sizeof(int));
- coef_bit_ptr = &cinfo->coef_bits[0][0];
- for (ci = 0; ci < cinfo->num_components; ci++)
- for (i = 0; i < DCTSIZE2; i++)
- *coef_bit_ptr++ = -1;
- }
- }
|