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