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