jdcoefct.c revision cc66ecf44d1407039b05ffd7b3342389f95c17b7
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#ifdef ANDROID_TILE_BASED_DECODE 160 if (cinfo->tile_decode) { 161 last_MCU_col = 162 (cinfo->coef->MCU_column_right_boundary - 163 cinfo->coef->MCU_column_left_boundary) - 1; 164 } 165#endif 166 167 /* Loop to process as much as one whole iMCU row */ 168 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 169 yoffset++) { 170 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col; 171 MCU_col_num++) { 172 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */ 173 jzero_far((void FAR *) coef->MCU_buffer[0], 174 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK))); 175 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { 176 /* Suspension forced; update state counters and exit */ 177 coef->MCU_vert_offset = yoffset; 178 coef->MCU_ctr = MCU_col_num; 179 return JPEG_SUSPENDED; 180 } 181 /* Determine where data should go in output_buf and do the IDCT thing. 182 * We skip dummy blocks at the right and bottom edges (but blkn gets 183 * incremented past them!). Note the inner loop relies on having 184 * allocated the MCU_buffer[] blocks sequentially. 185 */ 186 blkn = 0; /* index of current DCT block within MCU */ 187 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 188 compptr = cinfo->cur_comp_info[ci]; 189 /* Don't bother to IDCT an uninteresting component. */ 190 if (! compptr->component_needed) { 191 blkn += compptr->MCU_blocks; 192 continue; 193 } 194 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index]; 195 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width 196 : compptr->last_col_width; 197 output_ptr = output_buf[compptr->component_index] + 198 yoffset * compptr->DCT_scaled_size; 199 start_col = MCU_col_num * compptr->MCU_sample_width; 200 for (yindex = 0; yindex < compptr->MCU_height; yindex++) { 201 if (cinfo->input_iMCU_row < last_iMCU_row || 202 yoffset+yindex < compptr->last_row_height) { 203 output_col = start_col; 204 for (xindex = 0; xindex < useful_width; xindex++) { 205 (*inverse_DCT) (cinfo, compptr, 206 (JCOEFPTR) coef->MCU_buffer[blkn+xindex], 207 output_ptr, output_col); 208 output_col += compptr->DCT_scaled_size; 209 } 210 } 211 blkn += compptr->MCU_width; 212 output_ptr += compptr->DCT_scaled_size; 213 } 214 } 215 } 216 /* Completed an MCU row, but perhaps not an iMCU row */ 217 coef->MCU_ctr = 0; 218 } 219 /* Completed the iMCU row, advance counters for next one */ 220 cinfo->output_iMCU_row++; 221 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { 222 start_iMCU_row(cinfo); 223 return JPEG_ROW_COMPLETED; 224 } 225 /* Completed the scan */ 226 (*cinfo->inputctl->finish_input_pass) (cinfo); 227 return JPEG_SCAN_COMPLETED; 228} 229 230 231/* 232 * Dummy consume-input routine for single-pass operation. 233 */ 234 235METHODDEF(int) 236dummy_consume_data (j_decompress_ptr cinfo) 237{ 238 return JPEG_SUSPENDED; /* Always indicate nothing was done */ 239} 240 241#ifdef D_MULTISCAN_FILES_SUPPORTED 242/* 243 * Consume input data and store it in the full-image coefficient buffer. 244 * We read as much as one fully interleaved MCU row ("iMCU" row) per call, 245 * ie, v_samp_factor block rows for each component in the scan. 246 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. 247 */ 248 249METHODDEF(int) 250consume_data (j_decompress_ptr cinfo) 251{ 252 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 253 JDIMENSION MCU_col_num; /* index of current MCU within row */ 254 int blkn, ci, xindex, yindex, yoffset; 255 JDIMENSION start_col; 256 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; 257 JBLOCKROW buffer_ptr; 258 jpeg_component_info *compptr; 259 260 /* Align the virtual buffers for the components used in this scan. */ 261 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 262 compptr = cinfo->cur_comp_info[ci]; 263 buffer[ci] = (*cinfo->mem->access_virt_barray) 264 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], 265 cinfo->tile_decode ? 0 : cinfo->input_iMCU_row * compptr->v_samp_factor, 266 (JDIMENSION) compptr->v_samp_factor, TRUE); 267 /* Note: entropy decoder expects buffer to be zeroed, 268 * but this is handled automatically by the memory manager 269 * because we requested a pre-zeroed array. 270 */ 271 } 272 unsigned int MCUs_per_row = cinfo->MCUs_per_row; 273#ifdef ANDROID_TILE_BASED_DECODE 274 if (cinfo->tile_decode) { 275 MCUs_per_row = jmin(MCUs_per_row, 276 (cinfo->coef->column_right_boundary - cinfo->coef->column_left_boundary) 277 * cinfo->entropy->index->MCU_sample_size * cinfo->max_h_samp_factor); 278 } 279#endif 280 281 /* Loop to process one whole iMCU row */ 282 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 283 yoffset++) { 284#ifdef ANDROID_TILE_BASED_DECODE 285 if (cinfo->tile_decode) { 286 huffman_scan_header scan_header = 287 cinfo->entropy->index->scan[cinfo->input_scan_number]; 288 int col_offset = cinfo->coef->column_left_boundary; 289 (*cinfo->entropy->configure_huffman_decoder) (cinfo, 290 scan_header.offset[cinfo->input_iMCU_row] 291 [col_offset + yoffset * scan_header.MCUs_per_row]); 292 } 293#endif 294 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < MCUs_per_row; 295 MCU_col_num++) { 296 /* Construct list of pointers to DCT blocks belonging to this MCU */ 297 blkn = 0; /* index of current DCT block within MCU */ 298 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 299 compptr = cinfo->cur_comp_info[ci]; 300 start_col = MCU_col_num * compptr->MCU_width; 301 for (yindex = 0; yindex < compptr->MCU_height; yindex++) { 302 buffer_ptr = buffer[ci][yindex+yoffset] + start_col; 303 for (xindex = 0; xindex < compptr->MCU_width; xindex++) { 304 coef->MCU_buffer[blkn++] = buffer_ptr++; 305#ifdef ANDROID_TILE_BASED_DECODE 306 if (cinfo->tile_decode && cinfo->input_scan_number == 0) { 307 // need to do pre-zero ourself. 308 jzero_far((void FAR *) coef->MCU_buffer[blkn-1], 309 (size_t) (SIZEOF(JBLOCK))); 310 } 311#endif 312 } 313 } 314 } 315 /* Try to fetch the MCU. */ 316 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { 317 /* Suspension forced; update state counters and exit */ 318 coef->MCU_vert_offset = yoffset; 319 coef->MCU_ctr = MCU_col_num; 320 return JPEG_SUSPENDED; 321 } 322 } 323 /* Completed an MCU row, but perhaps not an iMCU row */ 324 coef->MCU_ctr = 0; 325 } 326 /* Completed the iMCU row, advance counters for next one */ 327 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { 328 start_iMCU_row(cinfo); 329 return JPEG_ROW_COMPLETED; 330 } 331 /* Completed the scan */ 332 (*cinfo->inputctl->finish_input_pass) (cinfo); 333 return JPEG_SCAN_COMPLETED; 334} 335 336/* 337 * Consume input data and store it in the coefficient buffer. 338 * Read one fully interleaved MCU row ("iMCU" row) per call. 339 */ 340 341METHODDEF(int) 342consume_data_multi_scan (j_decompress_ptr cinfo) 343{ 344 huffman_index *index = cinfo->entropy->index; 345 int i, retcode, ci; 346 int mcu = cinfo->input_iMCU_row; 347 jinit_phuff_decoder(cinfo); 348 for (i = 0; i < index->scan_count; i++) { 349 (*cinfo->inputctl->finish_input_pass) (cinfo); 350 jset_input_stream_position(cinfo, index->scan[i].bitstream_offset); 351 cinfo->output_iMCU_row = mcu; 352 cinfo->unread_marker = 0; 353 // Consume SOS and DHT headers 354 retcode = (*cinfo->inputctl->consume_markers) (cinfo, index, i); 355 cinfo->input_iMCU_row = mcu; 356 cinfo->input_scan_number = i; 357 cinfo->entropy->index = index; 358 // Consume scan block data 359 consume_data(cinfo); 360 } 361 cinfo->input_iMCU_row = mcu + 1; 362 cinfo->input_scan_number = 0; 363 cinfo->output_scan_number = 0; 364 return JPEG_ROW_COMPLETED; 365} 366 367/* 368 * Same as consume_data, expect for saving the Huffman decode information 369 * - bitstream offset and DC coefficient to index. 370 */ 371 372METHODDEF(int) 373consume_data_build_huffman_index_baseline (j_decompress_ptr cinfo, 374 huffman_index *index, int current_scan) 375{ 376 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 377 JDIMENSION MCU_col_num; /* index of current MCU within row */ 378 int ci, xindex, yindex, yoffset; 379 JDIMENSION start_col; 380 JBLOCKROW buffer_ptr; 381 382 huffman_scan_header *scan_header = index->scan + current_scan; 383 scan_header->MCU_rows_per_iMCU_row = coef->MCU_rows_per_iMCU_row; 384 385 size_t allocate_size = coef->MCU_rows_per_iMCU_row 386 * jdiv_round_up(cinfo->MCUs_per_row, index->MCU_sample_size) 387 * sizeof(huffman_offset_data); 388 scan_header->offset[cinfo->input_iMCU_row] = 389 (huffman_offset_data*)malloc(allocate_size); 390 index->mem_used += allocate_size; 391 392 huffman_offset_data *offset_data = scan_header->offset[cinfo->input_iMCU_row]; 393 394 /* Loop to process one whole iMCU row */ 395 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 396 yoffset++) { 397 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; 398 MCU_col_num++) { 399 // Record huffman bit offset 400 if (MCU_col_num % index->MCU_sample_size == 0) { 401 (*cinfo->entropy->get_huffman_decoder_configuration) 402 (cinfo, offset_data); 403 ++offset_data; 404 } 405 406 /* Try to fetch the MCU. */ 407 if (! (*cinfo->entropy->decode_mcu_discard_coef) (cinfo)) { 408 /* Suspension forced; update state counters and exit */ 409 coef->MCU_vert_offset = yoffset; 410 coef->MCU_ctr = MCU_col_num; 411 return JPEG_SUSPENDED; 412 } 413 } 414 /* Completed an MCU row, but perhaps not an iMCU row */ 415 coef->MCU_ctr = 0; 416 } 417 /* Completed the iMCU row, advance counters for next one */ 418 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { 419 start_iMCU_row(cinfo); 420 return JPEG_ROW_COMPLETED; 421 } 422 /* Completed the scan */ 423 (*cinfo->inputctl->finish_input_pass) (cinfo); 424 return JPEG_SCAN_COMPLETED; 425} 426 427/* 428 * Same as consume_data, expect for saving the Huffman decode information 429 * - bitstream offset and DC coefficient to index. 430 */ 431 432METHODDEF(int) 433consume_data_build_huffman_index_progressive (j_decompress_ptr cinfo, 434 huffman_index *index, int current_scan) 435{ 436 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 437 JDIMENSION MCU_col_num; /* index of current MCU within row */ 438 int blkn, ci, xindex, yindex, yoffset; 439 JDIMENSION start_col; 440 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; 441 JBLOCKROW buffer_ptr; 442 jpeg_component_info *compptr; 443 444 int factor = 4; // maximum factor is 4. 445 for (ci = 0; ci < cinfo->comps_in_scan; ci++) 446 factor = jmin(factor, cinfo->cur_comp_info[ci]->h_samp_factor); 447 448 int sample_size = index->MCU_sample_size * factor; 449 huffman_scan_header *scan_header = index->scan + current_scan; 450 scan_header->MCU_rows_per_iMCU_row = coef->MCU_rows_per_iMCU_row; 451 scan_header->MCUs_per_row = jdiv_round_up(cinfo->MCUs_per_row, sample_size); 452 scan_header->comps_in_scan = cinfo->comps_in_scan; 453 454 size_t allocate_size = coef->MCU_rows_per_iMCU_row 455 * scan_header->MCUs_per_row * sizeof(huffman_offset_data); 456 scan_header->offset[cinfo->input_iMCU_row] = 457 (huffman_offset_data*)malloc(allocate_size); 458 index->mem_used += allocate_size; 459 460 huffman_offset_data *offset_data = scan_header->offset[cinfo->input_iMCU_row]; 461 462 /* Align the virtual buffers for the components used in this scan. */ 463 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 464 compptr = cinfo->cur_comp_info[ci]; 465 buffer[ci] = (*cinfo->mem->access_virt_barray) 466 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], 467 0, // Only need one row buffer 468 (JDIMENSION) compptr->v_samp_factor, TRUE); 469 } 470 /* Loop to process one whole iMCU row */ 471 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 472 yoffset++) { 473 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; 474 MCU_col_num++) { 475 /* For each MCU, we loop through different color components. 476 * Then, for each color component we will get a list of pointers to DCT 477 * blocks in the virtual buffer. 478 */ 479 blkn = 0; /* index of current DCT block within MCU */ 480 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 481 compptr = cinfo->cur_comp_info[ci]; 482 start_col = MCU_col_num * compptr->MCU_width; 483 /* Get the list of pointers to DCT blocks in 484 * the virtual buffer in a color component of the MCU. 485 */ 486 for (yindex = 0; yindex < compptr->MCU_height; yindex++) { 487 buffer_ptr = buffer[ci][yindex+yoffset] + start_col; 488 for (xindex = 0; xindex < compptr->MCU_width; xindex++) { 489 coef->MCU_buffer[blkn++] = buffer_ptr++; 490 if (cinfo->input_scan_number == 0) { 491 // need to do pre-zero by ourself. 492 jzero_far((void FAR *) coef->MCU_buffer[blkn-1], 493 (size_t) (SIZEOF(JBLOCK))); 494 } 495 } 496 } 497 } 498 // Record huffman bit offset 499 if (MCU_col_num % sample_size == 0) { 500 (*cinfo->entropy->get_huffman_decoder_configuration) 501 (cinfo, offset_data); 502 ++offset_data; 503 } 504 /* Try to fetch the MCU. */ 505 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { 506 /* Suspension forced; update state counters and exit */ 507 coef->MCU_vert_offset = yoffset; 508 coef->MCU_ctr = MCU_col_num; 509 return JPEG_SUSPENDED; 510 } 511 } 512 /* Completed an MCU row, but perhaps not an iMCU row */ 513 coef->MCU_ctr = 0; 514 } 515 (*cinfo->entropy->get_huffman_decoder_configuration) 516 (cinfo, &scan_header->prev_MCU_offset); 517 /* Completed the iMCU row, advance counters for next one */ 518 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { 519 start_iMCU_row(cinfo); 520 return JPEG_ROW_COMPLETED; 521 } 522 /* Completed the scan */ 523 (*cinfo->inputctl->finish_input_pass) (cinfo); 524 return JPEG_SCAN_COMPLETED; 525} 526 527/* 528 * Decompress and return some data in the multi-pass case. 529 * Always attempts to emit one fully interleaved MCU row ("iMCU" row). 530 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. 531 * 532 * NB: output_buf contains a plane for each component in image. 533 */ 534 535METHODDEF(int) 536decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) 537{ 538 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 539 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; 540 JDIMENSION block_num; 541 int ci, block_row, block_rows; 542 JBLOCKARRAY buffer; 543 JBLOCKROW buffer_ptr; 544 JSAMPARRAY output_ptr; 545 JDIMENSION output_col; 546 jpeg_component_info *compptr; 547 inverse_DCT_method_ptr inverse_DCT; 548 549 /* Force some input to be done if we are getting ahead of the input. */ 550 while (cinfo->input_scan_number < cinfo->output_scan_number || 551 (cinfo->input_scan_number == cinfo->output_scan_number && 552 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) { 553 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) 554 return JPEG_SUSPENDED; 555 } 556 557 /* OK, output from the virtual arrays. */ 558 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 559 ci++, compptr++) { 560 /* Don't bother to IDCT an uninteresting component. */ 561 if (! compptr->component_needed) 562 continue; 563 /* Align the virtual buffer for this component. */ 564 buffer = (*cinfo->mem->access_virt_barray) 565 ((j_common_ptr) cinfo, coef->whole_image[ci], 566 cinfo->tile_decode ? 0 : cinfo->output_iMCU_row * compptr->v_samp_factor, 567 (JDIMENSION) compptr->v_samp_factor, FALSE); 568 /* Count non-dummy DCT block rows in this iMCU row. */ 569 if (cinfo->output_iMCU_row < last_iMCU_row) 570 block_rows = compptr->v_samp_factor; 571 else { 572 /* NB: can't use last_row_height here; it is input-side-dependent! */ 573 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 574 if (block_rows == 0) block_rows = compptr->v_samp_factor; 575 } 576 inverse_DCT = cinfo->idct->inverse_DCT[ci]; 577 output_ptr = output_buf[ci]; 578 /* Loop over all DCT blocks to be processed. */ 579 for (block_row = 0; block_row < block_rows; block_row++) { 580 buffer_ptr = buffer[block_row]; 581 output_col = 0; 582 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) { 583 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr, 584 output_ptr, output_col); 585 buffer_ptr++; 586 output_col += compptr->DCT_scaled_size; 587 } 588 output_ptr += compptr->DCT_scaled_size; 589 } 590 } 591 592 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) 593 return JPEG_ROW_COMPLETED; 594 return JPEG_SCAN_COMPLETED; 595} 596 597#endif /* D_MULTISCAN_FILES_SUPPORTED */ 598 599 600#ifdef BLOCK_SMOOTHING_SUPPORTED 601 602/* 603 * This code applies interblock smoothing as described by section K.8 604 * of the JPEG standard: the first 5 AC coefficients are estimated from 605 * the DC values of a DCT block and its 8 neighboring blocks. 606 * We apply smoothing only for progressive JPEG decoding, and only if 607 * the coefficients it can estimate are not yet known to full precision. 608 */ 609 610/* Natural-order array positions of the first 5 zigzag-order coefficients */ 611#define Q01_POS 1 612#define Q10_POS 8 613#define Q20_POS 16 614#define Q11_POS 9 615#define Q02_POS 2 616 617/* 618 * Determine whether block smoothing is applicable and safe. 619 * We also latch the current states of the coef_bits[] entries for the 620 * AC coefficients; otherwise, if the input side of the decompressor 621 * advances into a new scan, we might think the coefficients are known 622 * more accurately than they really are. 623 */ 624 625LOCAL(boolean) 626smoothing_ok (j_decompress_ptr cinfo) 627{ 628 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 629 boolean smoothing_useful = FALSE; 630 int ci, coefi; 631 jpeg_component_info *compptr; 632 JQUANT_TBL * qtable; 633 int * coef_bits; 634 int * coef_bits_latch; 635 636 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL) 637 return FALSE; 638 639 /* Allocate latch area if not already done */ 640 if (coef->coef_bits_latch == NULL) 641 coef->coef_bits_latch = (int *) 642 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 643 cinfo->num_components * 644 (SAVED_COEFS * SIZEOF(int))); 645 coef_bits_latch = coef->coef_bits_latch; 646 647 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 648 ci++, compptr++) { 649 /* All components' quantization values must already be latched. */ 650 if ((qtable = compptr->quant_table) == NULL) 651 return FALSE; 652 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */ 653 if (qtable->quantval[0] == 0 || 654 qtable->quantval[Q01_POS] == 0 || 655 qtable->quantval[Q10_POS] == 0 || 656 qtable->quantval[Q20_POS] == 0 || 657 qtable->quantval[Q11_POS] == 0 || 658 qtable->quantval[Q02_POS] == 0) 659 return FALSE; 660 /* DC values must be at least partly known for all components. */ 661 coef_bits = cinfo->coef_bits[ci]; 662 if (coef_bits[0] < 0) 663 return FALSE; 664 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */ 665 for (coefi = 1; coefi <= 5; coefi++) { 666 coef_bits_latch[coefi] = coef_bits[coefi]; 667 if (coef_bits[coefi] != 0) 668 smoothing_useful = TRUE; 669 } 670 coef_bits_latch += SAVED_COEFS; 671 } 672 673 return smoothing_useful; 674} 675 676 677/* 678 * Variant of decompress_data for use when doing block smoothing. 679 */ 680 681METHODDEF(int) 682decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) 683{ 684 my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 685 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; 686 JDIMENSION block_num, last_block_column; 687 int ci, block_row, block_rows, access_rows; 688 JBLOCKARRAY buffer; 689 JBLOCKROW buffer_ptr, prev_block_row, next_block_row; 690 JSAMPARRAY output_ptr; 691 JDIMENSION output_col; 692 jpeg_component_info *compptr; 693 inverse_DCT_method_ptr inverse_DCT; 694 boolean first_row, last_row; 695 JBLOCK workspace; 696 int *coef_bits; 697 JQUANT_TBL *quanttbl; 698 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num; 699 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9; 700 int Al, pred; 701 702 /* Force some input to be done if we are getting ahead of the input. */ 703 while (cinfo->input_scan_number <= cinfo->output_scan_number && 704 ! cinfo->inputctl->eoi_reached) { 705 if (cinfo->input_scan_number == cinfo->output_scan_number) { 706 /* If input is working on current scan, we ordinarily want it to 707 * have completed the current row. But if input scan is DC, 708 * we want it to keep one row ahead so that next block row's DC 709 * values are up to date. 710 */ 711 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0; 712 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta) 713 break; 714 } 715 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) 716 return JPEG_SUSPENDED; 717 } 718 719 /* OK, output from the virtual arrays. */ 720 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 721 ci++, compptr++) { 722 /* Don't bother to IDCT an uninteresting component. */ 723 if (! compptr->component_needed) 724 continue; 725 /* Count non-dummy DCT block rows in this iMCU row. */ 726 if (cinfo->output_iMCU_row < last_iMCU_row) { 727 block_rows = compptr->v_samp_factor; 728 access_rows = block_rows * 2; /* this and next iMCU row */ 729 last_row = FALSE; 730 } else { 731 /* NB: can't use last_row_height here; it is input-side-dependent! */ 732 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 733 if (block_rows == 0) block_rows = compptr->v_samp_factor; 734 access_rows = block_rows; /* this iMCU row only */ 735 last_row = TRUE; 736 } 737 /* Align the virtual buffer for this component. */ 738 if (cinfo->output_iMCU_row > 0) { 739 access_rows += compptr->v_samp_factor; /* prior iMCU row too */ 740 buffer = (*cinfo->mem->access_virt_barray) 741 ((j_common_ptr) cinfo, coef->whole_image[ci], 742 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor, 743 (JDIMENSION) access_rows, FALSE); 744 buffer += compptr->v_samp_factor; /* point to current iMCU row */ 745 first_row = FALSE; 746 } else { 747 buffer = (*cinfo->mem->access_virt_barray) 748 ((j_common_ptr) cinfo, coef->whole_image[ci], 749 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE); 750 first_row = TRUE; 751 } 752 /* Fetch component-dependent info */ 753 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS); 754 quanttbl = compptr->quant_table; 755 Q00 = quanttbl->quantval[0]; 756 Q01 = quanttbl->quantval[Q01_POS]; 757 Q10 = quanttbl->quantval[Q10_POS]; 758 Q20 = quanttbl->quantval[Q20_POS]; 759 Q11 = quanttbl->quantval[Q11_POS]; 760 Q02 = quanttbl->quantval[Q02_POS]; 761 inverse_DCT = cinfo->idct->inverse_DCT[ci]; 762 output_ptr = output_buf[ci]; 763 /* Loop over all DCT blocks to be processed. */ 764 for (block_row = 0; block_row < block_rows; block_row++) { 765 buffer_ptr = buffer[block_row]; 766 if (first_row && block_row == 0) 767 prev_block_row = buffer_ptr; 768 else 769 prev_block_row = buffer[block_row-1]; 770 if (last_row && block_row == block_rows-1) 771 next_block_row = buffer_ptr; 772 else 773 next_block_row = buffer[block_row+1]; 774 /* We fetch the surrounding DC values using a sliding-register approach. 775 * Initialize all nine here so as to do the right thing on narrow pics. 776 */ 777 DC1 = DC2 = DC3 = (int) prev_block_row[0][0]; 778 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0]; 779 DC7 = DC8 = DC9 = (int) next_block_row[0][0]; 780 output_col = 0; 781 last_block_column = compptr->width_in_blocks - 1; 782 for (block_num = 0; block_num <= last_block_column; block_num++) { 783 /* Fetch current DCT block into workspace so we can modify it. */ 784 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1); 785 /* Update DC values */ 786 if (block_num < last_block_column) { 787 DC3 = (int) prev_block_row[1][0]; 788 DC6 = (int) buffer_ptr[1][0]; 789 DC9 = (int) next_block_row[1][0]; 790 } 791 /* Compute coefficient estimates per K.8. 792 * An estimate is applied only if coefficient is still zero, 793 * and is not known to be fully accurate. 794 */ 795 /* AC01 */ 796 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) { 797 num = 36 * Q00 * (DC4 - DC6); 798 if (num >= 0) { 799 pred = (int) (((Q01<<7) + num) / (Q01<<8)); 800 if (Al > 0 && pred >= (1<<Al)) 801 pred = (1<<Al)-1; 802 } else { 803 pred = (int) (((Q01<<7) - num) / (Q01<<8)); 804 if (Al > 0 && pred >= (1<<Al)) 805 pred = (1<<Al)-1; 806 pred = -pred; 807 } 808 workspace[1] = (JCOEF) pred; 809 } 810 /* AC10 */ 811 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) { 812 num = 36 * Q00 * (DC2 - DC8); 813 if (num >= 0) { 814 pred = (int) (((Q10<<7) + num) / (Q10<<8)); 815 if (Al > 0 && pred >= (1<<Al)) 816 pred = (1<<Al)-1; 817 } else { 818 pred = (int) (((Q10<<7) - num) / (Q10<<8)); 819 if (Al > 0 && pred >= (1<<Al)) 820 pred = (1<<Al)-1; 821 pred = -pred; 822 } 823 workspace[8] = (JCOEF) pred; 824 } 825 /* AC20 */ 826 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) { 827 num = 9 * Q00 * (DC2 + DC8 - 2*DC5); 828 if (num >= 0) { 829 pred = (int) (((Q20<<7) + num) / (Q20<<8)); 830 if (Al > 0 && pred >= (1<<Al)) 831 pred = (1<<Al)-1; 832 } else { 833 pred = (int) (((Q20<<7) - num) / (Q20<<8)); 834 if (Al > 0 && pred >= (1<<Al)) 835 pred = (1<<Al)-1; 836 pred = -pred; 837 } 838 workspace[16] = (JCOEF) pred; 839 } 840 /* AC11 */ 841 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) { 842 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9); 843 if (num >= 0) { 844 pred = (int) (((Q11<<7) + num) / (Q11<<8)); 845 if (Al > 0 && pred >= (1<<Al)) 846 pred = (1<<Al)-1; 847 } else { 848 pred = (int) (((Q11<<7) - num) / (Q11<<8)); 849 if (Al > 0 && pred >= (1<<Al)) 850 pred = (1<<Al)-1; 851 pred = -pred; 852 } 853 workspace[9] = (JCOEF) pred; 854 } 855 /* AC02 */ 856 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) { 857 num = 9 * Q00 * (DC4 + DC6 - 2*DC5); 858 if (num >= 0) { 859 pred = (int) (((Q02<<7) + num) / (Q02<<8)); 860 if (Al > 0 && pred >= (1<<Al)) 861 pred = (1<<Al)-1; 862 } else { 863 pred = (int) (((Q02<<7) - num) / (Q02<<8)); 864 if (Al > 0 && pred >= (1<<Al)) 865 pred = (1<<Al)-1; 866 pred = -pred; 867 } 868 workspace[2] = (JCOEF) pred; 869 } 870 /* OK, do the IDCT */ 871 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace, 872 output_ptr, output_col); 873 /* Advance for next column */ 874 DC1 = DC2; DC2 = DC3; 875 DC4 = DC5; DC5 = DC6; 876 DC7 = DC8; DC8 = DC9; 877 buffer_ptr++, prev_block_row++, next_block_row++; 878 output_col += compptr->DCT_scaled_size; 879 } 880 output_ptr += compptr->DCT_scaled_size; 881 } 882 } 883 884 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) 885 return JPEG_ROW_COMPLETED; 886 return JPEG_SCAN_COMPLETED; 887} 888 889#endif /* BLOCK_SMOOTHING_SUPPORTED */ 890 891 892/* 893 * Initialize coefficient buffer controller. 894 */ 895 896GLOBAL(void) 897jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer) 898{ 899 my_coef_ptr coef; 900 901 coef = (my_coef_ptr) 902 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 903 SIZEOF(my_coef_controller)); 904 cinfo->coef = (struct jpeg_d_coef_controller *) coef; 905 coef->pub.start_input_pass = start_input_pass; 906 coef->pub.start_output_pass = start_output_pass; 907 coef->pub.column_left_boundary = 0; 908 coef->pub.column_right_boundary = 0; 909#ifdef BLOCK_SMOOTHING_SUPPORTED 910 coef->coef_bits_latch = NULL; 911#endif 912 913#ifdef ANDROID_TILE_BASED_DECODE 914 if (cinfo->tile_decode) { 915 if (cinfo->progressive_mode) { 916 /* Allocate one iMCU row virtual array, coef->whole_image[ci], 917 * for each color component, padded to a multiple of h_samp_factor 918 * DCT blocks in the horizontal direction. 919 */ 920 int ci, access_rows; 921 jpeg_component_info *compptr; 922 923 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 924 ci++, compptr++) { 925 access_rows = compptr->v_samp_factor; 926 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) 927 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE, 928 (JDIMENSION) jround_up((long) compptr->width_in_blocks, 929 (long) compptr->h_samp_factor), 930 (JDIMENSION) compptr->v_samp_factor, // one iMCU row 931 (JDIMENSION) access_rows); 932 } 933 coef->pub.consume_data_build_huffman_index = 934 consume_data_build_huffman_index_progressive; 935 coef->pub.consume_data = consume_data_multi_scan; 936 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */ 937 coef->pub.decompress_data = decompress_onepass; 938 } else { 939 /* We only need a single-MCU buffer. */ 940 JBLOCKROW buffer; 941 int i; 942 943 buffer = (JBLOCKROW) 944 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, 945 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); 946 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) { 947 coef->MCU_buffer[i] = buffer + i; 948 } 949 coef->pub.consume_data_build_huffman_index = 950 consume_data_build_huffman_index_baseline; 951 coef->pub.consume_data = dummy_consume_data; 952 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */ 953 coef->pub.decompress_data = decompress_onepass; 954 } 955 return; 956 } 957#endif 958 959 /* Create the coefficient buffer. */ 960 if (need_full_buffer) { 961#ifdef D_MULTISCAN_FILES_SUPPORTED 962 /* Allocate a full-image virtual array for each component, */ 963 /* padded to a multiple of samp_factor DCT blocks in each direction. */ 964 /* Note we ask for a pre-zeroed array. */ 965 int ci, access_rows; 966 jpeg_component_info *compptr; 967 968 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 969 ci++, compptr++) { 970 access_rows = compptr->v_samp_factor; 971#ifdef BLOCK_SMOOTHING_SUPPORTED 972 /* If block smoothing could be used, need a bigger window */ 973 if (cinfo->progressive_mode) 974 access_rows *= 3; 975#endif 976 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) 977 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE, 978 (JDIMENSION) jround_up((long) compptr->width_in_blocks, 979 (long) compptr->h_samp_factor), 980 (JDIMENSION) jround_up((long) compptr->height_in_blocks, 981 (long) compptr->v_samp_factor), 982 (JDIMENSION) access_rows); 983 } 984 coef->pub.consume_data = consume_data; 985 coef->pub.decompress_data = decompress_data; 986 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */ 987#else 988 ERREXIT(cinfo, JERR_NOT_COMPILED); 989#endif 990 } else { 991 /* We only need a single-MCU buffer. */ 992 JBLOCKROW buffer; 993 int i; 994 995 buffer = (JBLOCKROW) 996 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, 997 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); 998 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) { 999 coef->MCU_buffer[i] = buffer + i; 1000 } 1001 coef->pub.consume_data = dummy_consume_data; 1002 coef->pub.decompress_data = decompress_onepass; 1003 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */ 1004 } 1005} 1006