jdcoefct.c revision 3147fbe7688fc353e6ae03825a37cf101a4ee01d
1/* 2 * jdcoefct.c 3 * 4 * Copyright (C) 1994-1997, Thomas G. Lane. 5 * This file is part of the Independent JPEG Group's software. 6 * For conditions of distribution and use, see the accompanying README file. 7 * 8 * This file contains the coefficient buffer controller for decompression. 9 * This controller is the top level of the JPEG decompressor proper. 10 * The coefficient buffer lies between entropy decoding and inverse-DCT steps. 11 * 12 * In buffered-image mode, this controller is the interface between 13 * input-oriented processing and output-oriented processing. 14 * Also, the input side (only) is used when reading a file for transcoding. 15 */ 16 17#define JPEG_INTERNALS 18#include "jinclude.h" 19#include "jpeglib.h" 20 21/* Block smoothing is only applicable for progressive JPEG, so: */ 22#ifndef D_PROGRESSIVE_SUPPORTED 23#undef BLOCK_SMOOTHING_SUPPORTED 24#endif 25 26/* Private buffer controller object */ 27 28typedef struct { 29 struct jpeg_d_coef_controller pub; /* public fields */ 30 31 /* These variables keep track of the current location of the input side. */ 32 /* cinfo->input_iMCU_row is also used for this. */ 33 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */ 34 int MCU_vert_offset; /* counts MCU rows within iMCU row */ 35 int MCU_rows_per_iMCU_row; /* number of such rows needed */ 36 37 /* The output side's location is represented by cinfo->output_iMCU_row. */ 38 39 /* In single-pass modes, it's sufficient to buffer just one MCU. 40 * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks, 41 * and let the entropy decoder write into that workspace each time. 42 * (On 80x86, the workspace is FAR even though it's not really very big; 43 * this is to keep the module interfaces unchanged when a large coefficient 44 * buffer is necessary.) 45 * In multi-pass modes, this array points to the current MCU's blocks 46 * within the virtual arrays; it is used only by the input side. 47 */ 48 JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU]; 49 50#ifdef D_MULTISCAN_FILES_SUPPORTED 51 /* In multi-pass modes, we need a virtual block array for each component. */ 52 jvirt_barray_ptr whole_image[MAX_COMPONENTS]; 53#endif 54 55#ifdef BLOCK_SMOOTHING_SUPPORTED 56 /* When doing block smoothing, we latch coefficient Al values here */ 57 int * coef_bits_latch; 58#define SAVED_COEFS 6 /* we save coef_bits[0..5] */ 59#endif 60} my_coef_controller; 61 62typedef my_coef_controller * my_coef_ptr; 63 64/* Forward declarations */ 65METHODDEF(int) decompress_onepass 66 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); 67#ifdef D_MULTISCAN_FILES_SUPPORTED 68METHODDEF(int) decompress_data 69 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); 70#endif 71#ifdef BLOCK_SMOOTHING_SUPPORTED 72LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo)); 73METHODDEF(int) decompress_smooth_data 74 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); 75#endif 76 77 78LOCAL(void) 79start_iMCU_row (j_decompress_ptr cinfo) 80/* Reset within-iMCU-row counters for a new row (input side) */ 81{ 82 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 83 84 /* In an interleaved scan, an MCU row is the same as an iMCU row. 85 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. 86 * But at the bottom of the image, process only what's left. 87 */ 88 if (cinfo->comps_in_scan > 1) { 89 coef->MCU_rows_per_iMCU_row = 1; 90 } else { 91 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1)) 92 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; 93 else 94 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; 95 } 96 97 coef->MCU_ctr = 0; 98 coef->MCU_vert_offset = 0; 99} 100 101 102/* 103 * Initialize for an input processing pass. 104 */ 105 106METHODDEF(void) 107start_input_pass (j_decompress_ptr cinfo) 108{ 109 cinfo->input_iMCU_row = 0; 110 start_iMCU_row(cinfo); 111} 112 113 114/* 115 * Initialize for an output processing pass. 116 */ 117 118METHODDEF(void) 119start_output_pass (j_decompress_ptr cinfo) 120{ 121#ifdef BLOCK_SMOOTHING_SUPPORTED 122 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 123 124 /* If multipass, check to see whether to use block smoothing on this pass */ 125 if (coef->pub.coef_arrays != NULL) { 126 if (cinfo->do_block_smoothing && smoothing_ok(cinfo)) 127 coef->pub.decompress_data = decompress_smooth_data; 128 else 129 coef->pub.decompress_data = decompress_data; 130 } 131#endif 132 cinfo->output_iMCU_row = 0; 133} 134 135 136/* 137 * Decompress and return some data in the single-pass case. 138 * Always attempts to emit one fully interleaved MCU row ("iMCU" row). 139 * Input and output must run in lockstep since we have only a one-MCU buffer. 140 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. 141 * 142 * NB: output_buf contains a plane for each component in image, 143 * which we index according to the component's SOF position. 144 */ 145 146METHODDEF(int) 147decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) 148{ 149 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 150 JDIMENSION MCU_col_num; /* index of current MCU within row */ 151 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; 152 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; 153 int blkn, ci, xindex, yindex, yoffset, useful_width; 154 JSAMPARRAY output_ptr; 155 JDIMENSION start_col, output_col; 156 jpeg_component_info *compptr; 157 inverse_DCT_method_ptr inverse_DCT; 158 159 /* Loop to process as much as one whole iMCU row */ 160 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 161 yoffset++) { 162 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col; 163 MCU_col_num++) { 164 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */ 165 jzero_far((void FAR *) coef->MCU_buffer[0], 166 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK))); 167 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { 168 /* Suspension forced; update state counters and exit */ 169 coef->MCU_vert_offset = yoffset; 170 coef->MCU_ctr = MCU_col_num; 171 return JPEG_SUSPENDED; 172 } 173 /* Determine where data should go in output_buf and do the IDCT thing. 174 * We skip dummy blocks at the right and bottom edges (but blkn gets 175 * incremented past them!). Note the inner loop relies on having 176 * allocated the MCU_buffer[] blocks sequentially. 177 */ 178 blkn = 0; /* index of current DCT block within MCU */ 179 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 180 compptr = cinfo->cur_comp_info[ci]; 181 /* Don't bother to IDCT an uninteresting component. */ 182 if (! compptr->component_needed) { 183 blkn += compptr->MCU_blocks; 184 continue; 185 } 186 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index]; 187 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width 188 : compptr->last_col_width; 189 output_ptr = output_buf[compptr->component_index] + 190 yoffset * compptr->DCT_scaled_size; 191 start_col = MCU_col_num * compptr->MCU_sample_width; 192 for (yindex = 0; yindex < compptr->MCU_height; yindex++) { 193 if (cinfo->input_iMCU_row < last_iMCU_row || 194 yoffset+yindex < compptr->last_row_height) { 195 output_col = start_col; 196 for (xindex = 0; xindex < useful_width; xindex++) { 197 (*inverse_DCT) (cinfo, compptr, 198 (JCOEFPTR) coef->MCU_buffer[blkn+xindex], 199 output_ptr, output_col); 200 output_col += compptr->DCT_scaled_size; 201 } 202 } 203 blkn += compptr->MCU_width; 204 output_ptr += compptr->DCT_scaled_size; 205 } 206 } 207 } 208 /* Completed an MCU row, but perhaps not an iMCU row */ 209 coef->MCU_ctr = 0; 210 } 211 /* Completed the iMCU row, advance counters for next one */ 212 cinfo->output_iMCU_row++; 213 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { 214 start_iMCU_row(cinfo); 215 return JPEG_ROW_COMPLETED; 216 } 217 /* Completed the scan */ 218 (*cinfo->inputctl->finish_input_pass) (cinfo); 219 return JPEG_SCAN_COMPLETED; 220} 221 222 223/* 224 * Dummy consume-input routine for single-pass operation. 225 */ 226 227METHODDEF(int) 228dummy_consume_data (j_decompress_ptr cinfo) 229{ 230 return JPEG_SUSPENDED; /* Always indicate nothing was done */ 231} 232 233 234#ifdef D_MULTISCAN_FILES_SUPPORTED 235 236/* 237 * Consume input data and store it in the full-image coefficient buffer. 238 * We read as much as one fully interleaved MCU row ("iMCU" row) per call, 239 * ie, v_samp_factor block rows for each component in the scan. 240 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. 241 */ 242 243METHODDEF(int) 244consume_data (j_decompress_ptr cinfo) 245{ 246 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 247 JDIMENSION MCU_col_num; /* index of current MCU within row */ 248 int blkn, ci, xindex, yindex, yoffset; 249 JDIMENSION start_col; 250 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; 251 JBLOCKROW buffer_ptr; 252 jpeg_component_info *compptr; 253 254 /* Align the virtual buffers for the components used in this scan. */ 255 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 256 compptr = cinfo->cur_comp_info[ci]; 257 buffer[ci] = (*cinfo->mem->access_virt_barray) 258 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], 259 cinfo->input_iMCU_row * compptr->v_samp_factor, 260 (JDIMENSION) compptr->v_samp_factor, TRUE); 261 /* Note: entropy decoder expects buffer to be zeroed, 262 * but this is handled automatically by the memory manager 263 * because we requested a pre-zeroed array. 264 */ 265 } 266 /* Loop to process one whole iMCU row */ 267 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 268 yoffset++) { 269 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; 270 MCU_col_num++) { 271 /* Construct list of pointers to DCT blocks belonging to this MCU */ 272 blkn = 0; /* index of current DCT block within MCU */ 273 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 274 compptr = cinfo->cur_comp_info[ci]; 275 start_col = MCU_col_num * compptr->MCU_width; 276 for (yindex = 0; yindex < compptr->MCU_height; yindex++) { 277 buffer_ptr = buffer[ci][yindex+yoffset] + start_col; 278 for (xindex = 0; xindex < compptr->MCU_width; xindex++) { 279 coef->MCU_buffer[blkn++] = buffer_ptr++; 280 } 281 } 282 } 283 /* Try to fetch the MCU. */ 284 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { 285 /* Suspension forced; update state counters and exit */ 286 coef->MCU_vert_offset = yoffset; 287 coef->MCU_ctr = MCU_col_num; 288 return JPEG_SUSPENDED; 289 } 290 } 291 /* Completed an MCU row, but perhaps not an iMCU row */ 292 coef->MCU_ctr = 0; 293 } 294 /* Completed the iMCU row, advance counters for next one */ 295 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { 296 start_iMCU_row(cinfo); 297 return JPEG_ROW_COMPLETED; 298 } 299 /* Completed the scan */ 300 (*cinfo->inputctl->finish_input_pass) (cinfo); 301 return JPEG_SCAN_COMPLETED; 302} 303 304#define rounded_division(A,B) ((A+B-1)/(B)) 305/* 306 * Same as consume_data, expect for saving the Huffman decode information 307 * - bitstream offset and DC coefficient to index. 308 */ 309 310METHODDEF(int) 311consume_data_with_huffman_index (j_decompress_ptr cinfo, huffman_index *index, 312 int current_scan) 313{ 314 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 315 JDIMENSION MCU_col_num; /* index of current MCU within row */ 316 int ci, xindex, yindex, yoffset; 317 JDIMENSION start_col; 318 JBLOCKROW buffer_ptr; 319 320 huffman_scan_header current_header = index->scan[current_scan]; 321 current_header.MCU_rows_per_iMCU_row = coef->MCU_rows_per_iMCU_row; 322 current_header.MCUs_per_row = cinfo->MCUs_per_row; 323 current_header.comps_in_scan = cinfo->comps_in_scan; 324 325 size_t allocate_size = coef->MCU_rows_per_iMCU_row 326 * rounded_division(cinfo->MCUs_per_row, index->MCU_sample_size) 327 * sizeof(huffman_offset_data); 328 current_header.offset[cinfo->input_iMCU_row] = (huffman_offset_data*)malloc(allocate_size); 329 index->mem_used += allocate_size; 330 331 huffman_offset_data *offset_data = current_header.offset[cinfo->input_iMCU_row]; 332 333 /* Loop to process one whole iMCU row */ 334 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 335 yoffset++) { 336 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; 337 MCU_col_num++) { 338 // Record huffman bit offset 339 if (MCU_col_num % index->MCU_sample_size == 0) { 340 jpeg_get_huffman_decoder_configuration(cinfo, 341 &offset_data->bitstream_offset, offset_data->prev_dc); 342 ++offset_data; 343 } 344 345 /* Try to fetch the MCU. */ 346 if (! (*cinfo->entropy->decode_mcu_discard_coef) (cinfo)) { 347 /* Suspension forced; update state counters and exit */ 348 coef->MCU_vert_offset = yoffset; 349 coef->MCU_ctr = MCU_col_num; 350 return JPEG_SUSPENDED; 351 } 352 } 353 /* Completed an MCU row, but perhaps not an iMCU row */ 354 coef->MCU_ctr = 0; 355 } 356 /* Completed the iMCU row, advance counters for next one */ 357 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { 358 start_iMCU_row(cinfo); 359 return JPEG_ROW_COMPLETED; 360 } 361 /* Completed the scan */ 362 (*cinfo->inputctl->finish_input_pass) (cinfo); 363 return JPEG_SCAN_COMPLETED; 364} 365 366 367/* 368 * Decompress and return some data in the multi-pass case. 369 * Always attempts to emit one fully interleaved MCU row ("iMCU" row). 370 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. 371 * 372 * NB: output_buf contains a plane for each component in image. 373 */ 374 375METHODDEF(int) 376decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) 377{ 378 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 379 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; 380 JDIMENSION block_num; 381 int ci, block_row, block_rows; 382 JBLOCKARRAY buffer; 383 JBLOCKROW buffer_ptr; 384 JSAMPARRAY output_ptr; 385 JDIMENSION output_col; 386 jpeg_component_info *compptr; 387 inverse_DCT_method_ptr inverse_DCT; 388 389 /* Force some input to be done if we are getting ahead of the input. */ 390 while (cinfo->input_scan_number < cinfo->output_scan_number || 391 (cinfo->input_scan_number == cinfo->output_scan_number && 392 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) { 393 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) 394 return JPEG_SUSPENDED; 395 } 396 397 /* OK, output from the virtual arrays. */ 398 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 399 ci++, compptr++) { 400 /* Don't bother to IDCT an uninteresting component. */ 401 if (! compptr->component_needed) 402 continue; 403 /* Align the virtual buffer for this component. */ 404 buffer = (*cinfo->mem->access_virt_barray) 405 ((j_common_ptr) cinfo, coef->whole_image[ci], 406 cinfo->output_iMCU_row * compptr->v_samp_factor, 407 (JDIMENSION) compptr->v_samp_factor, FALSE); 408 /* Count non-dummy DCT block rows in this iMCU row. */ 409 if (cinfo->output_iMCU_row < last_iMCU_row) 410 block_rows = compptr->v_samp_factor; 411 else { 412 /* NB: can't use last_row_height here; it is input-side-dependent! */ 413 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 414 if (block_rows == 0) block_rows = compptr->v_samp_factor; 415 } 416 inverse_DCT = cinfo->idct->inverse_DCT[ci]; 417 output_ptr = output_buf[ci]; 418 /* Loop over all DCT blocks to be processed. */ 419 for (block_row = 0; block_row < block_rows; block_row++) { 420 buffer_ptr = buffer[block_row]; 421 output_col = 0; 422 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) { 423 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr, 424 output_ptr, output_col); 425 buffer_ptr++; 426 output_col += compptr->DCT_scaled_size; 427 } 428 output_ptr += compptr->DCT_scaled_size; 429 } 430 } 431 432 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) 433 return JPEG_ROW_COMPLETED; 434 return JPEG_SCAN_COMPLETED; 435} 436 437#endif /* D_MULTISCAN_FILES_SUPPORTED */ 438 439 440#ifdef BLOCK_SMOOTHING_SUPPORTED 441 442/* 443 * This code applies interblock smoothing as described by section K.8 444 * of the JPEG standard: the first 5 AC coefficients are estimated from 445 * the DC values of a DCT block and its 8 neighboring blocks. 446 * We apply smoothing only for progressive JPEG decoding, and only if 447 * the coefficients it can estimate are not yet known to full precision. 448 */ 449 450/* Natural-order array positions of the first 5 zigzag-order coefficients */ 451#define Q01_POS 1 452#define Q10_POS 8 453#define Q20_POS 16 454#define Q11_POS 9 455#define Q02_POS 2 456 457/* 458 * Determine whether block smoothing is applicable and safe. 459 * We also latch the current states of the coef_bits[] entries for the 460 * AC coefficients; otherwise, if the input side of the decompressor 461 * advances into a new scan, we might think the coefficients are known 462 * more accurately than they really are. 463 */ 464 465LOCAL(boolean) 466smoothing_ok (j_decompress_ptr cinfo) 467{ 468 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 469 boolean smoothing_useful = FALSE; 470 int ci, coefi; 471 jpeg_component_info *compptr; 472 JQUANT_TBL * qtable; 473 int * coef_bits; 474 int * coef_bits_latch; 475 476 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL) 477 return FALSE; 478 479 /* Allocate latch area if not already done */ 480 if (coef->coef_bits_latch == NULL) 481 coef->coef_bits_latch = (int *) 482 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 483 cinfo->num_components * 484 (SAVED_COEFS * SIZEOF(int))); 485 coef_bits_latch = coef->coef_bits_latch; 486 487 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 488 ci++, compptr++) { 489 /* All components' quantization values must already be latched. */ 490 if ((qtable = compptr->quant_table) == NULL) 491 return FALSE; 492 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */ 493 if (qtable->quantval[0] == 0 || 494 qtable->quantval[Q01_POS] == 0 || 495 qtable->quantval[Q10_POS] == 0 || 496 qtable->quantval[Q20_POS] == 0 || 497 qtable->quantval[Q11_POS] == 0 || 498 qtable->quantval[Q02_POS] == 0) 499 return FALSE; 500 /* DC values must be at least partly known for all components. */ 501 coef_bits = cinfo->coef_bits[ci]; 502 if (coef_bits[0] < 0) 503 return FALSE; 504 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */ 505 for (coefi = 1; coefi <= 5; coefi++) { 506 coef_bits_latch[coefi] = coef_bits[coefi]; 507 if (coef_bits[coefi] != 0) 508 smoothing_useful = TRUE; 509 } 510 coef_bits_latch += SAVED_COEFS; 511 } 512 513 return smoothing_useful; 514} 515 516 517/* 518 * Variant of decompress_data for use when doing block smoothing. 519 */ 520 521METHODDEF(int) 522decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) 523{ 524 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 525 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; 526 JDIMENSION block_num, last_block_column; 527 int ci, block_row, block_rows, access_rows; 528 JBLOCKARRAY buffer; 529 JBLOCKROW buffer_ptr, prev_block_row, next_block_row; 530 JSAMPARRAY output_ptr; 531 JDIMENSION output_col; 532 jpeg_component_info *compptr; 533 inverse_DCT_method_ptr inverse_DCT; 534 boolean first_row, last_row; 535 JBLOCK workspace; 536 int *coef_bits; 537 JQUANT_TBL *quanttbl; 538 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num; 539 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9; 540 int Al, pred; 541 542 /* Force some input to be done if we are getting ahead of the input. */ 543 while (cinfo->input_scan_number <= cinfo->output_scan_number && 544 ! cinfo->inputctl->eoi_reached) { 545 if (cinfo->input_scan_number == cinfo->output_scan_number) { 546 /* If input is working on current scan, we ordinarily want it to 547 * have completed the current row. But if input scan is DC, 548 * we want it to keep one row ahead so that next block row's DC 549 * values are up to date. 550 */ 551 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0; 552 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta) 553 break; 554 } 555 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) 556 return JPEG_SUSPENDED; 557 } 558 559 /* OK, output from the virtual arrays. */ 560 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 561 ci++, compptr++) { 562 /* Don't bother to IDCT an uninteresting component. */ 563 if (! compptr->component_needed) 564 continue; 565 /* Count non-dummy DCT block rows in this iMCU row. */ 566 if (cinfo->output_iMCU_row < last_iMCU_row) { 567 block_rows = compptr->v_samp_factor; 568 access_rows = block_rows * 2; /* this and next iMCU row */ 569 last_row = FALSE; 570 } else { 571 /* NB: can't use last_row_height here; it is input-side-dependent! */ 572 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 573 if (block_rows == 0) block_rows = compptr->v_samp_factor; 574 access_rows = block_rows; /* this iMCU row only */ 575 last_row = TRUE; 576 } 577 /* Align the virtual buffer for this component. */ 578 if (cinfo->output_iMCU_row > 0) { 579 access_rows += compptr->v_samp_factor; /* prior iMCU row too */ 580 buffer = (*cinfo->mem->access_virt_barray) 581 ((j_common_ptr) cinfo, coef->whole_image[ci], 582 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor, 583 (JDIMENSION) access_rows, FALSE); 584 buffer += compptr->v_samp_factor; /* point to current iMCU row */ 585 first_row = FALSE; 586 } else { 587 buffer = (*cinfo->mem->access_virt_barray) 588 ((j_common_ptr) cinfo, coef->whole_image[ci], 589 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE); 590 first_row = TRUE; 591 } 592 /* Fetch component-dependent info */ 593 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS); 594 quanttbl = compptr->quant_table; 595 Q00 = quanttbl->quantval[0]; 596 Q01 = quanttbl->quantval[Q01_POS]; 597 Q10 = quanttbl->quantval[Q10_POS]; 598 Q20 = quanttbl->quantval[Q20_POS]; 599 Q11 = quanttbl->quantval[Q11_POS]; 600 Q02 = quanttbl->quantval[Q02_POS]; 601 inverse_DCT = cinfo->idct->inverse_DCT[ci]; 602 output_ptr = output_buf[ci]; 603 /* Loop over all DCT blocks to be processed. */ 604 for (block_row = 0; block_row < block_rows; block_row++) { 605 buffer_ptr = buffer[block_row]; 606 if (first_row && block_row == 0) 607 prev_block_row = buffer_ptr; 608 else 609 prev_block_row = buffer[block_row-1]; 610 if (last_row && block_row == block_rows-1) 611 next_block_row = buffer_ptr; 612 else 613 next_block_row = buffer[block_row+1]; 614 /* We fetch the surrounding DC values using a sliding-register approach. 615 * Initialize all nine here so as to do the right thing on narrow pics. 616 */ 617 DC1 = DC2 = DC3 = (int) prev_block_row[0][0]; 618 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0]; 619 DC7 = DC8 = DC9 = (int) next_block_row[0][0]; 620 output_col = 0; 621 last_block_column = compptr->width_in_blocks - 1; 622 for (block_num = 0; block_num <= last_block_column; block_num++) { 623 /* Fetch current DCT block into workspace so we can modify it. */ 624 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1); 625 /* Update DC values */ 626 if (block_num < last_block_column) { 627 DC3 = (int) prev_block_row[1][0]; 628 DC6 = (int) buffer_ptr[1][0]; 629 DC9 = (int) next_block_row[1][0]; 630 } 631 /* Compute coefficient estimates per K.8. 632 * An estimate is applied only if coefficient is still zero, 633 * and is not known to be fully accurate. 634 */ 635 /* AC01 */ 636 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) { 637 num = 36 * Q00 * (DC4 - DC6); 638 if (num >= 0) { 639 pred = (int) (((Q01<<7) + num) / (Q01<<8)); 640 if (Al > 0 && pred >= (1<<Al)) 641 pred = (1<<Al)-1; 642 } else { 643 pred = (int) (((Q01<<7) - num) / (Q01<<8)); 644 if (Al > 0 && pred >= (1<<Al)) 645 pred = (1<<Al)-1; 646 pred = -pred; 647 } 648 workspace[1] = (JCOEF) pred; 649 } 650 /* AC10 */ 651 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) { 652 num = 36 * Q00 * (DC2 - DC8); 653 if (num >= 0) { 654 pred = (int) (((Q10<<7) + num) / (Q10<<8)); 655 if (Al > 0 && pred >= (1<<Al)) 656 pred = (1<<Al)-1; 657 } else { 658 pred = (int) (((Q10<<7) - num) / (Q10<<8)); 659 if (Al > 0 && pred >= (1<<Al)) 660 pred = (1<<Al)-1; 661 pred = -pred; 662 } 663 workspace[8] = (JCOEF) pred; 664 } 665 /* AC20 */ 666 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) { 667 num = 9 * Q00 * (DC2 + DC8 - 2*DC5); 668 if (num >= 0) { 669 pred = (int) (((Q20<<7) + num) / (Q20<<8)); 670 if (Al > 0 && pred >= (1<<Al)) 671 pred = (1<<Al)-1; 672 } else { 673 pred = (int) (((Q20<<7) - num) / (Q20<<8)); 674 if (Al > 0 && pred >= (1<<Al)) 675 pred = (1<<Al)-1; 676 pred = -pred; 677 } 678 workspace[16] = (JCOEF) pred; 679 } 680 /* AC11 */ 681 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) { 682 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9); 683 if (num >= 0) { 684 pred = (int) (((Q11<<7) + num) / (Q11<<8)); 685 if (Al > 0 && pred >= (1<<Al)) 686 pred = (1<<Al)-1; 687 } else { 688 pred = (int) (((Q11<<7) - num) / (Q11<<8)); 689 if (Al > 0 && pred >= (1<<Al)) 690 pred = (1<<Al)-1; 691 pred = -pred; 692 } 693 workspace[9] = (JCOEF) pred; 694 } 695 /* AC02 */ 696 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) { 697 num = 9 * Q00 * (DC4 + DC6 - 2*DC5); 698 if (num >= 0) { 699 pred = (int) (((Q02<<7) + num) / (Q02<<8)); 700 if (Al > 0 && pred >= (1<<Al)) 701 pred = (1<<Al)-1; 702 } else { 703 pred = (int) (((Q02<<7) - num) / (Q02<<8)); 704 if (Al > 0 && pred >= (1<<Al)) 705 pred = (1<<Al)-1; 706 pred = -pred; 707 } 708 workspace[2] = (JCOEF) pred; 709 } 710 /* OK, do the IDCT */ 711 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace, 712 output_ptr, output_col); 713 /* Advance for next column */ 714 DC1 = DC2; DC2 = DC3; 715 DC4 = DC5; DC5 = DC6; 716 DC7 = DC8; DC8 = DC9; 717 buffer_ptr++, prev_block_row++, next_block_row++; 718 output_col += compptr->DCT_scaled_size; 719 } 720 output_ptr += compptr->DCT_scaled_size; 721 } 722 } 723 724 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) 725 return JPEG_ROW_COMPLETED; 726 return JPEG_SCAN_COMPLETED; 727} 728 729#endif /* BLOCK_SMOOTHING_SUPPORTED */ 730 731 732/* 733 * Initialize coefficient buffer controller. 734 */ 735 736GLOBAL(void) 737jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer) 738{ 739 my_coef_ptr coef; 740 741 coef = (my_coef_ptr) 742 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 743 SIZEOF(my_coef_controller)); 744 cinfo->coef = (struct jpeg_d_coef_controller *) coef; 745 coef->pub.start_input_pass = start_input_pass; 746 coef->pub.start_output_pass = start_output_pass; 747#ifdef BLOCK_SMOOTHING_SUPPORTED 748 coef->coef_bits_latch = NULL; 749#endif 750 751 /* Create the coefficient buffer. */ 752 if (need_full_buffer) { 753#ifdef D_MULTISCAN_FILES_SUPPORTED 754 /* Allocate a full-image virtual array for each component, */ 755 /* padded to a multiple of samp_factor DCT blocks in each direction. */ 756 /* Note we ask for a pre-zeroed array. */ 757 int ci, access_rows; 758 jpeg_component_info *compptr; 759 760 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 761 ci++, compptr++) { 762 access_rows = compptr->v_samp_factor; 763#ifdef BLOCK_SMOOTHING_SUPPORTED 764 /* If block smoothing could be used, need a bigger window */ 765 if (cinfo->progressive_mode) 766 access_rows *= 3; 767#endif 768 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) 769 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE, 770 (JDIMENSION) jround_up((long) compptr->width_in_blocks, 771 (long) compptr->h_samp_factor), 772 (JDIMENSION) jround_up((long) compptr->height_in_blocks, 773 (long) compptr->v_samp_factor), 774 (JDIMENSION) access_rows); 775 } 776 coef->pub.consume_data_with_huffman_index = consume_data_with_huffman_index; 777 coef->pub.consume_data = consume_data; 778 coef->pub.decompress_data = decompress_data; 779 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */ 780#else 781 ERREXIT(cinfo, JERR_NOT_COMPILED); 782#endif 783 } else { 784 /* We only need a single-MCU buffer. */ 785 JBLOCKROW buffer; 786 int i; 787 788 buffer = (JBLOCKROW) 789 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, 790 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); 791 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) { 792 coef->MCU_buffer[i] = buffer + i; 793 } 794 coef->pub.consume_data_with_huffman_index = consume_data_with_huffman_index; 795 coef->pub.consume_data = dummy_consume_data; 796 coef->pub.decompress_data = decompress_onepass; 797 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */ 798 } 799} 800