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