00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #define JPEG_INTERNALS
00017 #include "jinclude.h"
00018 #include "jpeglib.h"
00019
00020
00021
00022
00023 typedef struct {
00024 struct jpeg_entropy_decoder pub;
00025
00026 INT32 c;
00027 INT32 a;
00028 int ct;
00029
00030
00031
00032 int last_dc_val[MAX_COMPS_IN_SCAN];
00033 int dc_context[MAX_COMPS_IN_SCAN];
00034
00035 unsigned int restarts_to_go;
00036
00037
00038 unsigned char * dc_stats[NUM_ARITH_TBLS];
00039 unsigned char * ac_stats[NUM_ARITH_TBLS];
00040
00041
00042 unsigned char fixed_bin[4];
00043 } arith_entropy_decoder;
00044
00045 typedef arith_entropy_decoder * arith_entropy_ptr;
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #define DC_STAT_BINS 64
00061 #define AC_STAT_BINS 256
00062
00063
00064 LOCAL(int)
00065 get_byte (j_decompress_ptr cinfo)
00066
00067 {
00068 struct jpeg_source_mgr * src = cinfo->src;
00069
00070 if (src->bytes_in_buffer == 0)
00071 if (! (*src->fill_input_buffer) (cinfo))
00072 ERREXIT(cinfo, JERR_CANT_SUSPEND);
00073 src->bytes_in_buffer--;
00074 return GETJOCTET(*src->next_input_byte++);
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 LOCAL(int)
00106 arith_decode (j_decompress_ptr cinfo, unsigned char *st)
00107 {
00108 register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
00109 register unsigned char nl, nm;
00110 register INT32 qe, temp;
00111 register int sv, data;
00112
00113
00114 while (e->a < 0x8000L) {
00115 if (--e->ct < 0) {
00116
00117 if (cinfo->unread_marker)
00118 data = 0;
00119 else {
00120 data = get_byte(cinfo);
00121 if (data == 0xFF) {
00122 do data = get_byte(cinfo);
00123 while (data == 0xFF);
00124 if (data == 0)
00125 data = 0xFF;
00126 else {
00127
00128
00129
00130
00131
00132
00133 cinfo->unread_marker = data;
00134 data = 0;
00135 }
00136 }
00137 }
00138 e->c = (e->c << 8) | data;
00139 if ((e->ct += 8) < 0)
00140
00141 if (++e->ct == 0)
00142
00143 e->a = 0x8000L;
00144 }
00145 e->a <<= 1;
00146 }
00147
00148
00149
00150
00151 sv = *st;
00152 qe = jpeg_aritab[sv & 0x7F];
00153 nl = qe & 0xFF; qe >>= 8;
00154 nm = qe & 0xFF; qe >>= 8;
00155
00156
00157 temp = e->a - qe;
00158 e->a = temp;
00159 temp <<= e->ct;
00160 if (e->c >= temp) {
00161 e->c -= temp;
00162
00163 if (e->a < qe) {
00164 e->a = qe;
00165 *st = (sv & 0x80) ^ nm;
00166 } else {
00167 e->a = qe;
00168 *st = (sv & 0x80) ^ nl;
00169 sv ^= 0x80;
00170 }
00171 } else if (e->a < 0x8000L) {
00172
00173 if (e->a < qe) {
00174 *st = (sv & 0x80) ^ nl;
00175 sv ^= 0x80;
00176 } else {
00177 *st = (sv & 0x80) ^ nm;
00178 }
00179 }
00180
00181 return sv >> 7;
00182 }
00183
00184
00185
00186
00187
00188
00189 LOCAL(void)
00190 process_restart (j_decompress_ptr cinfo)
00191 {
00192 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00193 int ci;
00194 jpeg_component_info * compptr;
00195
00196
00197 if (! (*cinfo->marker->read_restart_marker) (cinfo))
00198 ERREXIT(cinfo, JERR_CANT_SUSPEND);
00199
00200
00201 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00202 compptr = cinfo->cur_comp_info[ci];
00203 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
00204 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
00205
00206 entropy->last_dc_val[ci] = 0;
00207 entropy->dc_context[ci] = 0;
00208 }
00209 if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
00210 (cinfo->progressive_mode && cinfo->Ss)) {
00211 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
00212 }
00213 }
00214
00215
00216 entropy->c = 0;
00217 entropy->a = 0;
00218 entropy->ct = -16;
00219
00220
00221 entropy->restarts_to_go = cinfo->restart_interval;
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 METHODDEF(boolean)
00242 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00243 {
00244 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00245 JBLOCKROW block;
00246 unsigned char *st;
00247 int blkn, ci, tbl, sign;
00248 int v, m;
00249
00250
00251 if (cinfo->restart_interval) {
00252 if (entropy->restarts_to_go == 0)
00253 process_restart(cinfo);
00254 entropy->restarts_to_go--;
00255 }
00256
00257 if (entropy->ct == -1) return TRUE;
00258
00259
00260
00261 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00262 block = MCU_data[blkn];
00263 ci = cinfo->MCU_membership[blkn];
00264 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
00265
00266
00267
00268
00269 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
00270
00271
00272 if (arith_decode(cinfo, st) == 0)
00273 entropy->dc_context[ci] = 0;
00274 else {
00275
00276
00277 sign = arith_decode(cinfo, st + 1);
00278 st += 2; st += sign;
00279
00280 if ((m = arith_decode(cinfo, st)) != 0) {
00281 st = entropy->dc_stats[tbl] + 20;
00282 while (arith_decode(cinfo, st)) {
00283 if ((m <<= 1) == 0x8000) {
00284 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00285 entropy->ct = -1;
00286 return TRUE;
00287 }
00288 st += 1;
00289 }
00290 }
00291
00292 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
00293 entropy->dc_context[ci] = 0;
00294 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
00295 entropy->dc_context[ci] = 12 + (sign * 4);
00296 else
00297 entropy->dc_context[ci] = 4 + (sign * 4);
00298 v = m;
00299
00300 st += 14;
00301 while (m >>= 1)
00302 if (arith_decode(cinfo, st)) v |= m;
00303 v += 1; if (sign) v = -v;
00304 entropy->last_dc_val[ci] += v;
00305 }
00306
00307
00308 (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
00309 }
00310
00311 return TRUE;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320 METHODDEF(boolean)
00321 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00322 {
00323 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00324 JBLOCKROW block;
00325 unsigned char *st;
00326 int tbl, sign, k;
00327 int v, m;
00328 const int * natural_order;
00329
00330
00331 if (cinfo->restart_interval) {
00332 if (entropy->restarts_to_go == 0)
00333 process_restart(cinfo);
00334 entropy->restarts_to_go--;
00335 }
00336
00337 if (entropy->ct == -1) return TRUE;
00338
00339 natural_order = cinfo->natural_order;
00340
00341
00342 block = MCU_data[0];
00343 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
00344
00345
00346
00347
00348 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
00349 st = entropy->ac_stats[tbl] + 3 * (k - 1);
00350 if (arith_decode(cinfo, st)) break;
00351 while (arith_decode(cinfo, st + 1) == 0) {
00352 st += 3; k++;
00353 if (k > cinfo->Se) {
00354 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00355 entropy->ct = -1;
00356 return TRUE;
00357 }
00358 }
00359
00360
00361 sign = arith_decode(cinfo, entropy->fixed_bin);
00362 st += 2;
00363
00364 if ((m = arith_decode(cinfo, st)) != 0) {
00365 if (arith_decode(cinfo, st)) {
00366 m <<= 1;
00367 st = entropy->ac_stats[tbl] +
00368 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
00369 while (arith_decode(cinfo, st)) {
00370 if ((m <<= 1) == 0x8000) {
00371 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00372 entropy->ct = -1;
00373 return TRUE;
00374 }
00375 st += 1;
00376 }
00377 }
00378 }
00379 v = m;
00380
00381 st += 14;
00382 while (m >>= 1)
00383 if (arith_decode(cinfo, st)) v |= m;
00384 v += 1; if (sign) v = -v;
00385
00386 (*block)[natural_order[k]] = (JCOEF) (v << cinfo->Al);
00387 }
00388
00389 return TRUE;
00390 }
00391
00392
00393
00394
00395
00396
00397 METHODDEF(boolean)
00398 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00399 {
00400 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00401 unsigned char *st;
00402 int p1, blkn;
00403
00404
00405 if (cinfo->restart_interval) {
00406 if (entropy->restarts_to_go == 0)
00407 process_restart(cinfo);
00408 entropy->restarts_to_go--;
00409 }
00410
00411 st = entropy->fixed_bin;
00412 p1 = 1 << cinfo->Al;
00413
00414
00415
00416 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00417
00418 if (arith_decode(cinfo, st))
00419 MCU_data[blkn][0][0] |= p1;
00420 }
00421
00422 return TRUE;
00423 }
00424
00425
00426
00427
00428
00429
00430 METHODDEF(boolean)
00431 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00432 {
00433 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00434 JBLOCKROW block;
00435 JCOEFPTR thiscoef;
00436 unsigned char *st;
00437 int tbl, k, kex;
00438 int p1, m1;
00439 const int * natural_order;
00440
00441
00442 if (cinfo->restart_interval) {
00443 if (entropy->restarts_to_go == 0)
00444 process_restart(cinfo);
00445 entropy->restarts_to_go--;
00446 }
00447
00448 if (entropy->ct == -1) return TRUE;
00449
00450 natural_order = cinfo->natural_order;
00451
00452
00453 block = MCU_data[0];
00454 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
00455
00456 p1 = 1 << cinfo->Al;
00457 m1 = (-1) << cinfo->Al;
00458
00459
00460 for (kex = cinfo->Se; kex > 0; kex--)
00461 if ((*block)[natural_order[kex]]) break;
00462
00463 for (k = cinfo->Ss; k <= cinfo->Se; k++) {
00464 st = entropy->ac_stats[tbl] + 3 * (k - 1);
00465 if (k > kex)
00466 if (arith_decode(cinfo, st)) break;
00467 for (;;) {
00468 thiscoef = *block + natural_order[k];
00469 if (*thiscoef) {
00470 if (arith_decode(cinfo, st + 2)) {
00471 if (*thiscoef < 0)
00472 *thiscoef += m1;
00473 else
00474 *thiscoef += p1;
00475 }
00476 break;
00477 }
00478 if (arith_decode(cinfo, st + 1)) {
00479 if (arith_decode(cinfo, entropy->fixed_bin))
00480 *thiscoef = m1;
00481 else
00482 *thiscoef = p1;
00483 break;
00484 }
00485 st += 3; k++;
00486 if (k > cinfo->Se) {
00487 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00488 entropy->ct = -1;
00489 return TRUE;
00490 }
00491 }
00492 }
00493
00494 return TRUE;
00495 }
00496
00497
00498
00499
00500
00501
00502 METHODDEF(boolean)
00503 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00504 {
00505 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00506 jpeg_component_info * compptr;
00507 JBLOCKROW block;
00508 unsigned char *st;
00509 int blkn, ci, tbl, sign, k;
00510 int v, m;
00511 const int * natural_order;
00512
00513
00514 if (cinfo->restart_interval) {
00515 if (entropy->restarts_to_go == 0)
00516 process_restart(cinfo);
00517 entropy->restarts_to_go--;
00518 }
00519
00520 if (entropy->ct == -1) return TRUE;
00521
00522 natural_order = cinfo->natural_order;
00523
00524
00525
00526 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00527 block = MCU_data[blkn];
00528 ci = cinfo->MCU_membership[blkn];
00529 compptr = cinfo->cur_comp_info[ci];
00530
00531
00532
00533 tbl = compptr->dc_tbl_no;
00534
00535
00536 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
00537
00538
00539 if (arith_decode(cinfo, st) == 0)
00540 entropy->dc_context[ci] = 0;
00541 else {
00542
00543
00544 sign = arith_decode(cinfo, st + 1);
00545 st += 2; st += sign;
00546
00547 if ((m = arith_decode(cinfo, st)) != 0) {
00548 st = entropy->dc_stats[tbl] + 20;
00549 while (arith_decode(cinfo, st)) {
00550 if ((m <<= 1) == 0x8000) {
00551 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00552 entropy->ct = -1;
00553 return TRUE;
00554 }
00555 st += 1;
00556 }
00557 }
00558
00559 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
00560 entropy->dc_context[ci] = 0;
00561 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
00562 entropy->dc_context[ci] = 12 + (sign * 4);
00563 else
00564 entropy->dc_context[ci] = 4 + (sign * 4);
00565 v = m;
00566
00567 st += 14;
00568 while (m >>= 1)
00569 if (arith_decode(cinfo, st)) v |= m;
00570 v += 1; if (sign) v = -v;
00571 entropy->last_dc_val[ci] += v;
00572 }
00573
00574 (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
00575
00576
00577
00578 tbl = compptr->ac_tbl_no;
00579
00580
00581 for (k = 1; k <= cinfo->lim_Se; k++) {
00582 st = entropy->ac_stats[tbl] + 3 * (k - 1);
00583 if (arith_decode(cinfo, st)) break;
00584 while (arith_decode(cinfo, st + 1) == 0) {
00585 st += 3; k++;
00586 if (k > cinfo->lim_Se) {
00587 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00588 entropy->ct = -1;
00589 return TRUE;
00590 }
00591 }
00592
00593
00594 sign = arith_decode(cinfo, entropy->fixed_bin);
00595 st += 2;
00596
00597 if ((m = arith_decode(cinfo, st)) != 0) {
00598 if (arith_decode(cinfo, st)) {
00599 m <<= 1;
00600 st = entropy->ac_stats[tbl] +
00601 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
00602 while (arith_decode(cinfo, st)) {
00603 if ((m <<= 1) == 0x8000) {
00604 WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
00605 entropy->ct = -1;
00606 return TRUE;
00607 }
00608 st += 1;
00609 }
00610 }
00611 }
00612 v = m;
00613
00614 st += 14;
00615 while (m >>= 1)
00616 if (arith_decode(cinfo, st)) v |= m;
00617 v += 1; if (sign) v = -v;
00618 (*block)[natural_order[k]] = (JCOEF) v;
00619 }
00620 }
00621
00622 return TRUE;
00623 }
00624
00625
00626
00627
00628
00629
00630 METHODDEF(void)
00631 start_pass (j_decompress_ptr cinfo)
00632 {
00633 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
00634 int ci, tbl;
00635 jpeg_component_info * compptr;
00636
00637 if (cinfo->progressive_mode) {
00638
00639 if (cinfo->Ss == 0) {
00640 if (cinfo->Se != 0)
00641 goto bad;
00642 } else {
00643
00644 if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
00645 goto bad;
00646
00647 if (cinfo->comps_in_scan != 1)
00648 goto bad;
00649 }
00650 if (cinfo->Ah != 0) {
00651
00652 if (cinfo->Ah-1 != cinfo->Al)
00653 goto bad;
00654 }
00655 if (cinfo->Al > 13) {
00656 bad:
00657 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
00658 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
00659 }
00660
00661
00662
00663
00664 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00665 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
00666 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
00667 if (cinfo->Ss && coef_bit_ptr[0] < 0)
00668 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
00669 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
00670 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
00671 if (cinfo->Ah != expected)
00672 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
00673 coef_bit_ptr[coefi] = cinfo->Al;
00674 }
00675 }
00676
00677 if (cinfo->Ah == 0) {
00678 if (cinfo->Ss == 0)
00679 entropy->pub.decode_mcu = decode_mcu_DC_first;
00680 else
00681 entropy->pub.decode_mcu = decode_mcu_AC_first;
00682 } else {
00683 if (cinfo->Ss == 0)
00684 entropy->pub.decode_mcu = decode_mcu_DC_refine;
00685 else
00686 entropy->pub.decode_mcu = decode_mcu_AC_refine;
00687 }
00688 } else {
00689
00690
00691
00692 if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
00693 (cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se))
00694 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
00695
00696 entropy->pub.decode_mcu = decode_mcu;
00697 }
00698
00699
00700 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00701 compptr = cinfo->cur_comp_info[ci];
00702 if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
00703 tbl = compptr->dc_tbl_no;
00704 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
00705 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
00706 if (entropy->dc_stats[tbl] == NULL)
00707 entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
00708 ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
00709 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
00710
00711 entropy->last_dc_val[ci] = 0;
00712 entropy->dc_context[ci] = 0;
00713 }
00714 if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
00715 (cinfo->progressive_mode && cinfo->Ss)) {
00716 tbl = compptr->ac_tbl_no;
00717 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
00718 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
00719 if (entropy->ac_stats[tbl] == NULL)
00720 entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
00721 ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
00722 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
00723 }
00724 }
00725
00726
00727 entropy->c = 0;
00728 entropy->a = 0;
00729 entropy->ct = -16;
00730
00731
00732 entropy->restarts_to_go = cinfo->restart_interval;
00733 }
00734
00735
00736
00737
00738
00739
00740 GLOBAL(void)
00741 jinit_arith_decoder (j_decompress_ptr cinfo)
00742 {
00743 arith_entropy_ptr entropy;
00744 int i;
00745
00746 entropy = (arith_entropy_ptr)
00747 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00748 SIZEOF(arith_entropy_decoder));
00749 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
00750 entropy->pub.start_pass = start_pass;
00751
00752
00753 for (i = 0; i < NUM_ARITH_TBLS; i++) {
00754 entropy->dc_stats[i] = NULL;
00755 entropy->ac_stats[i] = NULL;
00756 }
00757
00758
00759 entropy->fixed_bin[0] = 113;
00760
00761 if (cinfo->progressive_mode) {
00762
00763 int *coef_bit_ptr, ci;
00764 cinfo->coef_bits = (int (*)[DCTSIZE2])
00765 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00766 cinfo->num_components*DCTSIZE2*SIZEOF(int));
00767 coef_bit_ptr = & cinfo->coef_bits[0][0];
00768 for (ci = 0; ci < cinfo->num_components; ci++)
00769 for (i = 0; i < DCTSIZE2; i++)
00770 *coef_bit_ptr++ = -1;
00771 }
00772 }