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