00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #define JPEG_INTERNALS
00018 #include "jinclude.h"
00019 #include "jpeglib.h"
00020 #include "jdhuff.h"
00021
00022
00023 #ifdef D_PROGRESSIVE_SUPPORTED
00024
00025
00026
00027
00028
00029
00030
00031
00032 typedef struct {
00033 unsigned int EOBRUN;
00034 int last_dc_val[MAX_COMPS_IN_SCAN];
00035 } savable_state;
00036
00037
00038
00039
00040
00041
00042 #ifndef NO_STRUCT_ASSIGN
00043 #define ASSIGN_STATE(dest,src) ((dest) = (src))
00044 #else
00045 #if MAX_COMPS_IN_SCAN == 4
00046 #define ASSIGN_STATE(dest,src) \
00047 ((dest).EOBRUN = (src).EOBRUN, \
00048 (dest).last_dc_val[0] = (src).last_dc_val[0], \
00049 (dest).last_dc_val[1] = (src).last_dc_val[1], \
00050 (dest).last_dc_val[2] = (src).last_dc_val[2], \
00051 (dest).last_dc_val[3] = (src).last_dc_val[3])
00052 #endif
00053 #endif
00054
00055
00056 typedef struct {
00057 struct jpeg_entropy_decoder pub;
00058
00059
00060
00061
00062 bitread_perm_state bitstate;
00063 savable_state saved;
00064
00065
00066 unsigned int restarts_to_go;
00067
00068
00069 d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
00070
00071 d_derived_tbl * ac_derived_tbl;
00072 } phuff_entropy_decoder;
00073
00074 typedef phuff_entropy_decoder * phuff_entropy_ptr;
00075
00076
00077 METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
00078 JBLOCKROW *MCU_data));
00079 METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
00080 JBLOCKROW *MCU_data));
00081 METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
00082 JBLOCKROW *MCU_data));
00083 METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
00084 JBLOCKROW *MCU_data));
00085
00086
00087
00088
00089
00090
00091 METHODDEF(void)
00092 start_pass_phuff_decoder (j_decompress_ptr cinfo)
00093 {
00094 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00095 boolean is_DC_band, bad;
00096 int ci, coefi, tbl;
00097 int *coef_bit_ptr;
00098 jpeg_component_info * compptr;
00099
00100 is_DC_band = (cinfo->Ss == 0);
00101
00102
00103 bad = FALSE;
00104 if (is_DC_band) {
00105 if (cinfo->Se != 0)
00106 bad = TRUE;
00107 } else {
00108
00109 if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
00110 bad = TRUE;
00111
00112 if (cinfo->comps_in_scan != 1)
00113 bad = TRUE;
00114 }
00115 if (cinfo->Ah != 0) {
00116
00117 if (cinfo->Al != cinfo->Ah-1)
00118 bad = TRUE;
00119 }
00120 if (cinfo->Al > 13)
00121 bad = TRUE;
00122
00123
00124
00125
00126
00127
00128 if (bad)
00129 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
00130 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
00131
00132
00133
00134
00135 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00136 int cindex = cinfo->cur_comp_info[ci]->component_index;
00137 coef_bit_ptr = & cinfo->coef_bits[cindex][0];
00138 if (!is_DC_band && coef_bit_ptr[0] < 0)
00139 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
00140 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
00141 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
00142 if (cinfo->Ah != expected)
00143 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
00144 coef_bit_ptr[coefi] = cinfo->Al;
00145 }
00146 }
00147
00148
00149 if (cinfo->Ah == 0) {
00150 if (is_DC_band)
00151 entropy->pub.decode_mcu = decode_mcu_DC_first;
00152 else
00153 entropy->pub.decode_mcu = decode_mcu_AC_first;
00154 } else {
00155 if (is_DC_band)
00156 entropy->pub.decode_mcu = decode_mcu_DC_refine;
00157 else
00158 entropy->pub.decode_mcu = decode_mcu_AC_refine;
00159 }
00160
00161 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00162 compptr = cinfo->cur_comp_info[ci];
00163
00164
00165
00166 if (is_DC_band) {
00167 if (cinfo->Ah == 0) {
00168 tbl = compptr->dc_tbl_no;
00169 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
00170 & entropy->derived_tbls[tbl]);
00171 }
00172 } else {
00173 tbl = compptr->ac_tbl_no;
00174 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
00175 & entropy->derived_tbls[tbl]);
00176
00177 entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
00178 }
00179
00180 entropy->saved.last_dc_val[ci] = 0;
00181 }
00182
00183
00184 entropy->bitstate.bits_left = 0;
00185 entropy->bitstate.get_buffer = 0;
00186 entropy->pub.insufficient_data = FALSE;
00187
00188
00189 entropy->saved.EOBRUN = 0;
00190
00191
00192 entropy->restarts_to_go = cinfo->restart_interval;
00193 }
00194
00195
00196
00197
00198
00199
00200
00201 #ifdef AVOID_TABLES
00202
00203 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
00204
00205 #else
00206
00207 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
00208
00209 static const int extend_test[16] =
00210 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
00211 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
00212
00213 static const int extend_offset[16] =
00214 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
00215 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
00216 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
00217 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
00218
00219 #endif
00220
00221
00222
00223
00224
00225
00226
00227 LOCAL(boolean)
00228 process_restart (j_decompress_ptr cinfo)
00229 {
00230 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00231 int ci;
00232
00233
00234
00235 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
00236 entropy->bitstate.bits_left = 0;
00237
00238
00239 if (! (*cinfo->marker->read_restart_marker) (cinfo))
00240 return FALSE;
00241
00242
00243 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
00244 entropy->saved.last_dc_val[ci] = 0;
00245
00246 entropy->saved.EOBRUN = 0;
00247
00248
00249 entropy->restarts_to_go = cinfo->restart_interval;
00250
00251
00252
00253
00254
00255
00256 if (cinfo->unread_marker == 0)
00257 entropy->pub.insufficient_data = FALSE;
00258
00259 return TRUE;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 METHODDEF(boolean)
00286 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00287 {
00288 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00289 int Al = cinfo->Al;
00290 register int s, r;
00291 int blkn, ci;
00292 JBLOCKROW block;
00293 BITREAD_STATE_VARS;
00294 savable_state state;
00295 d_derived_tbl * tbl;
00296 jpeg_component_info * compptr;
00297
00298
00299 if (cinfo->restart_interval) {
00300 if (entropy->restarts_to_go == 0)
00301 if (! process_restart(cinfo))
00302 return FALSE;
00303 }
00304
00305
00306
00307
00308 if (! entropy->pub.insufficient_data) {
00309
00310
00311 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
00312 ASSIGN_STATE(state, entropy->saved);
00313
00314
00315
00316 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00317 block = MCU_data[blkn];
00318 ci = cinfo->MCU_membership[blkn];
00319 compptr = cinfo->cur_comp_info[ci];
00320 tbl = entropy->derived_tbls[compptr->dc_tbl_no];
00321
00322
00323
00324
00325 HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
00326 if (s) {
00327 CHECK_BIT_BUFFER(br_state, s, return FALSE);
00328 r = GET_BITS(s);
00329 s = HUFF_EXTEND(r, s);
00330 }
00331
00332
00333 s += state.last_dc_val[ci];
00334 state.last_dc_val[ci] = s;
00335
00336 (*block)[0] = (JCOEF) (s << Al);
00337 }
00338
00339
00340 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
00341 ASSIGN_STATE(entropy->saved, state);
00342 }
00343
00344
00345 entropy->restarts_to_go--;
00346
00347 return TRUE;
00348 }
00349
00350
00351
00352
00353
00354
00355
00356 METHODDEF(boolean)
00357 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00358 {
00359 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00360 int Se = cinfo->Se;
00361 int Al = cinfo->Al;
00362 register int s, k, r;
00363 unsigned int EOBRUN;
00364 JBLOCKROW block;
00365 BITREAD_STATE_VARS;
00366 d_derived_tbl * tbl;
00367
00368
00369 if (cinfo->restart_interval) {
00370 if (entropy->restarts_to_go == 0)
00371 if (! process_restart(cinfo))
00372 return FALSE;
00373 }
00374
00375
00376
00377
00378 if (! entropy->pub.insufficient_data) {
00379
00380
00381
00382
00383 EOBRUN = entropy->saved.EOBRUN;
00384
00385
00386
00387 if (EOBRUN > 0)
00388 EOBRUN--;
00389 else {
00390 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
00391 block = MCU_data[0];
00392 tbl = entropy->ac_derived_tbl;
00393
00394 for (k = cinfo->Ss; k <= Se; k++) {
00395 HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
00396 r = s >> 4;
00397 s &= 15;
00398 if (s) {
00399 k += r;
00400 CHECK_BIT_BUFFER(br_state, s, return FALSE);
00401 r = GET_BITS(s);
00402 s = HUFF_EXTEND(r, s);
00403
00404 (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
00405 } else {
00406 if (r == 15) {
00407 k += 15;
00408 } else {
00409 EOBRUN = 1 << r;
00410 if (r) {
00411 CHECK_BIT_BUFFER(br_state, r, return FALSE);
00412 r = GET_BITS(r);
00413 EOBRUN += r;
00414 }
00415 EOBRUN--;
00416 break;
00417 }
00418 }
00419 }
00420
00421 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
00422 }
00423
00424
00425 entropy->saved.EOBRUN = EOBRUN;
00426 }
00427
00428
00429 entropy->restarts_to_go--;
00430
00431 return TRUE;
00432 }
00433
00434
00435
00436
00437
00438
00439
00440
00441 METHODDEF(boolean)
00442 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00443 {
00444 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00445 int p1 = 1 << cinfo->Al;
00446 int blkn;
00447 JBLOCKROW block;
00448 BITREAD_STATE_VARS;
00449
00450
00451 if (cinfo->restart_interval) {
00452 if (entropy->restarts_to_go == 0)
00453 if (! process_restart(cinfo))
00454 return FALSE;
00455 }
00456
00457
00458
00459
00460
00461
00462 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
00463
00464
00465
00466 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00467 block = MCU_data[blkn];
00468
00469
00470 CHECK_BIT_BUFFER(br_state, 1, return FALSE);
00471 if (GET_BITS(1))
00472 (*block)[0] |= p1;
00473
00474 }
00475
00476
00477 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
00478
00479
00480 entropy->restarts_to_go--;
00481
00482 return TRUE;
00483 }
00484
00485
00486
00487
00488
00489
00490 METHODDEF(boolean)
00491 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00492 {
00493 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00494 int Se = cinfo->Se;
00495 int p1 = 1 << cinfo->Al;
00496 int m1 = (-1) << cinfo->Al;
00497 register int s, k, r;
00498 unsigned int EOBRUN;
00499 JBLOCKROW block;
00500 JCOEFPTR thiscoef;
00501 BITREAD_STATE_VARS;
00502 d_derived_tbl * tbl;
00503 int num_newnz;
00504 int newnz_pos[DCTSIZE2];
00505
00506
00507 if (cinfo->restart_interval) {
00508 if (entropy->restarts_to_go == 0)
00509 if (! process_restart(cinfo))
00510 return FALSE;
00511 }
00512
00513
00514
00515 if (! entropy->pub.insufficient_data) {
00516
00517
00518 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
00519 EOBRUN = entropy->saved.EOBRUN;
00520
00521
00522 block = MCU_data[0];
00523 tbl = entropy->ac_derived_tbl;
00524
00525
00526
00527
00528
00529
00530
00531 num_newnz = 0;
00532
00533
00534 k = cinfo->Ss;
00535
00536 if (EOBRUN == 0) {
00537 for (; k <= Se; k++) {
00538 HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
00539 r = s >> 4;
00540 s &= 15;
00541 if (s) {
00542 if (s != 1)
00543 WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
00544 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
00545 if (GET_BITS(1))
00546 s = p1;
00547 else
00548 s = m1;
00549 } else {
00550 if (r != 15) {
00551 EOBRUN = 1 << r;
00552 if (r) {
00553 CHECK_BIT_BUFFER(br_state, r, goto undoit);
00554 r = GET_BITS(r);
00555 EOBRUN += r;
00556 }
00557 break;
00558 }
00559
00560 }
00561
00562
00563
00564
00565 do {
00566 thiscoef = *block + jpeg_natural_order[k];
00567 if (*thiscoef != 0) {
00568 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
00569 if (GET_BITS(1)) {
00570 if ((*thiscoef & p1) == 0) {
00571 if (*thiscoef >= 0)
00572 *thiscoef += p1;
00573 else
00574 *thiscoef += m1;
00575 }
00576 }
00577 } else {
00578 if (--r < 0)
00579 break;
00580 }
00581 k++;
00582 } while (k <= Se);
00583 if (s) {
00584 int pos = jpeg_natural_order[k];
00585
00586 (*block)[pos] = (JCOEF) s;
00587
00588 newnz_pos[num_newnz++] = pos;
00589 }
00590 }
00591 }
00592
00593 if (EOBRUN > 0) {
00594
00595
00596
00597
00598
00599 for (; k <= Se; k++) {
00600 thiscoef = *block + jpeg_natural_order[k];
00601 if (*thiscoef != 0) {
00602 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
00603 if (GET_BITS(1)) {
00604 if ((*thiscoef & p1) == 0) {
00605 if (*thiscoef >= 0)
00606 *thiscoef += p1;
00607 else
00608 *thiscoef += m1;
00609 }
00610 }
00611 }
00612 }
00613
00614 EOBRUN--;
00615 }
00616
00617
00618 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
00619 entropy->saved.EOBRUN = EOBRUN;
00620 }
00621
00622
00623 entropy->restarts_to_go--;
00624
00625 return TRUE;
00626
00627 undoit:
00628
00629 while (num_newnz > 0)
00630 (*block)[newnz_pos[--num_newnz]] = 0;
00631
00632 return FALSE;
00633 }
00634
00635
00636
00637
00638
00639
00640 GLOBAL(void)
00641 jinit_phuff_decoder (j_decompress_ptr cinfo)
00642 {
00643 phuff_entropy_ptr entropy;
00644 int *coef_bit_ptr;
00645 int ci, i;
00646
00647 entropy = (phuff_entropy_ptr)
00648 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00649 SIZEOF(phuff_entropy_decoder));
00650 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
00651 entropy->pub.start_pass = start_pass_phuff_decoder;
00652
00653
00654 for (i = 0; i < NUM_HUFF_TBLS; i++) {
00655 entropy->derived_tbls[i] = NULL;
00656 }
00657
00658
00659 cinfo->coef_bits = (int (*)[DCTSIZE2])
00660 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00661 cinfo->num_components*DCTSIZE2*SIZEOF(int));
00662 coef_bit_ptr = & cinfo->coef_bits[0][0];
00663 for (ci = 0; ci < cinfo->num_components; ci++)
00664 for (i = 0; i < DCTSIZE2; i++)
00665 *coef_bit_ptr++ = -1;
00666 }
00667
00668 #endif