1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libjpeg/jdcoefct.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,750 @@ 1.4 +/* 1.5 + * jdcoefct.c 1.6 + * 1.7 + * This file was part of the Independent JPEG Group's software: 1.8 + * Copyright (C) 1994-1997, Thomas G. Lane. 1.9 + * libjpeg-turbo Modifications: 1.10 + * Copyright (C) 2010, D. R. Commander. 1.11 + * For conditions of distribution and use, see the accompanying README file. 1.12 + * 1.13 + * This file contains the coefficient buffer controller for decompression. 1.14 + * This controller is the top level of the JPEG decompressor proper. 1.15 + * The coefficient buffer lies between entropy decoding and inverse-DCT steps. 1.16 + * 1.17 + * In buffered-image mode, this controller is the interface between 1.18 + * input-oriented processing and output-oriented processing. 1.19 + * Also, the input side (only) is used when reading a file for transcoding. 1.20 + */ 1.21 + 1.22 +#define JPEG_INTERNALS 1.23 +#include "jinclude.h" 1.24 +#include "jpeglib.h" 1.25 +#include "jpegcomp.h" 1.26 + 1.27 +/* Block smoothing is only applicable for progressive JPEG, so: */ 1.28 +#ifndef D_PROGRESSIVE_SUPPORTED 1.29 +#undef BLOCK_SMOOTHING_SUPPORTED 1.30 +#endif 1.31 + 1.32 +/* Private buffer controller object */ 1.33 + 1.34 +typedef struct { 1.35 + struct jpeg_d_coef_controller pub; /* public fields */ 1.36 + 1.37 + /* These variables keep track of the current location of the input side. */ 1.38 + /* cinfo->input_iMCU_row is also used for this. */ 1.39 + JDIMENSION MCU_ctr; /* counts MCUs processed in current row */ 1.40 + int MCU_vert_offset; /* counts MCU rows within iMCU row */ 1.41 + int MCU_rows_per_iMCU_row; /* number of such rows needed */ 1.42 + 1.43 + /* The output side's location is represented by cinfo->output_iMCU_row. */ 1.44 + 1.45 + /* In single-pass modes, it's sufficient to buffer just one MCU. 1.46 + * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks, 1.47 + * and let the entropy decoder write into that workspace each time. 1.48 + * (On 80x86, the workspace is FAR even though it's not really very big; 1.49 + * this is to keep the module interfaces unchanged when a large coefficient 1.50 + * buffer is necessary.) 1.51 + * In multi-pass modes, this array points to the current MCU's blocks 1.52 + * within the virtual arrays; it is used only by the input side. 1.53 + */ 1.54 + JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU]; 1.55 + 1.56 + /* Temporary workspace for one MCU */ 1.57 + JCOEF * workspace; 1.58 + 1.59 +#ifdef D_MULTISCAN_FILES_SUPPORTED 1.60 + /* In multi-pass modes, we need a virtual block array for each component. */ 1.61 + jvirt_barray_ptr whole_image[MAX_COMPONENTS]; 1.62 +#endif 1.63 + 1.64 +#ifdef BLOCK_SMOOTHING_SUPPORTED 1.65 + /* When doing block smoothing, we latch coefficient Al values here */ 1.66 + int * coef_bits_latch; 1.67 +#define SAVED_COEFS 6 /* we save coef_bits[0..5] */ 1.68 +#endif 1.69 +} my_coef_controller; 1.70 + 1.71 +typedef my_coef_controller * my_coef_ptr; 1.72 + 1.73 +/* Forward declarations */ 1.74 +METHODDEF(int) decompress_onepass 1.75 + JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); 1.76 +#ifdef D_MULTISCAN_FILES_SUPPORTED 1.77 +METHODDEF(int) decompress_data 1.78 + JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); 1.79 +#endif 1.80 +#ifdef BLOCK_SMOOTHING_SUPPORTED 1.81 +LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo)); 1.82 +METHODDEF(int) decompress_smooth_data 1.83 + JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); 1.84 +#endif 1.85 + 1.86 + 1.87 +LOCAL(void) 1.88 +start_iMCU_row (j_decompress_ptr cinfo) 1.89 +/* Reset within-iMCU-row counters for a new row (input side) */ 1.90 +{ 1.91 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.92 + 1.93 + /* In an interleaved scan, an MCU row is the same as an iMCU row. 1.94 + * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. 1.95 + * But at the bottom of the image, process only what's left. 1.96 + */ 1.97 + if (cinfo->comps_in_scan > 1) { 1.98 + coef->MCU_rows_per_iMCU_row = 1; 1.99 + } else { 1.100 + if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1)) 1.101 + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; 1.102 + else 1.103 + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; 1.104 + } 1.105 + 1.106 + coef->MCU_ctr = 0; 1.107 + coef->MCU_vert_offset = 0; 1.108 +} 1.109 + 1.110 + 1.111 +/* 1.112 + * Initialize for an input processing pass. 1.113 + */ 1.114 + 1.115 +METHODDEF(void) 1.116 +start_input_pass (j_decompress_ptr cinfo) 1.117 +{ 1.118 + cinfo->input_iMCU_row = 0; 1.119 + start_iMCU_row(cinfo); 1.120 +} 1.121 + 1.122 + 1.123 +/* 1.124 + * Initialize for an output processing pass. 1.125 + */ 1.126 + 1.127 +METHODDEF(void) 1.128 +start_output_pass (j_decompress_ptr cinfo) 1.129 +{ 1.130 +#ifdef BLOCK_SMOOTHING_SUPPORTED 1.131 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.132 + 1.133 + /* If multipass, check to see whether to use block smoothing on this pass */ 1.134 + if (coef->pub.coef_arrays != NULL) { 1.135 + if (cinfo->do_block_smoothing && smoothing_ok(cinfo)) 1.136 + coef->pub.decompress_data = decompress_smooth_data; 1.137 + else 1.138 + coef->pub.decompress_data = decompress_data; 1.139 + } 1.140 +#endif 1.141 + cinfo->output_iMCU_row = 0; 1.142 +} 1.143 + 1.144 + 1.145 +/* 1.146 + * Decompress and return some data in the single-pass case. 1.147 + * Always attempts to emit one fully interleaved MCU row ("iMCU" row). 1.148 + * Input and output must run in lockstep since we have only a one-MCU buffer. 1.149 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. 1.150 + * 1.151 + * NB: output_buf contains a plane for each component in image, 1.152 + * which we index according to the component's SOF position. 1.153 + */ 1.154 + 1.155 +METHODDEF(int) 1.156 +decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) 1.157 +{ 1.158 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.159 + JDIMENSION MCU_col_num; /* index of current MCU within row */ 1.160 + JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; 1.161 + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; 1.162 + int blkn, ci, xindex, yindex, yoffset, useful_width; 1.163 + JSAMPARRAY output_ptr; 1.164 + JDIMENSION start_col, output_col; 1.165 + jpeg_component_info *compptr; 1.166 + inverse_DCT_method_ptr inverse_DCT; 1.167 + 1.168 + /* Loop to process as much as one whole iMCU row */ 1.169 + for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 1.170 + yoffset++) { 1.171 + for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col; 1.172 + MCU_col_num++) { 1.173 + /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */ 1.174 + jzero_far((void FAR *) coef->MCU_buffer[0], 1.175 + (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK))); 1.176 + if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { 1.177 + /* Suspension forced; update state counters and exit */ 1.178 + coef->MCU_vert_offset = yoffset; 1.179 + coef->MCU_ctr = MCU_col_num; 1.180 + return JPEG_SUSPENDED; 1.181 + } 1.182 + /* Determine where data should go in output_buf and do the IDCT thing. 1.183 + * We skip dummy blocks at the right and bottom edges (but blkn gets 1.184 + * incremented past them!). Note the inner loop relies on having 1.185 + * allocated the MCU_buffer[] blocks sequentially. 1.186 + */ 1.187 + blkn = 0; /* index of current DCT block within MCU */ 1.188 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.189 + compptr = cinfo->cur_comp_info[ci]; 1.190 + /* Don't bother to IDCT an uninteresting component. */ 1.191 + if (! compptr->component_needed) { 1.192 + blkn += compptr->MCU_blocks; 1.193 + continue; 1.194 + } 1.195 + inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index]; 1.196 + useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width 1.197 + : compptr->last_col_width; 1.198 + output_ptr = output_buf[compptr->component_index] + 1.199 + yoffset * compptr->_DCT_scaled_size; 1.200 + start_col = MCU_col_num * compptr->MCU_sample_width; 1.201 + for (yindex = 0; yindex < compptr->MCU_height; yindex++) { 1.202 + if (cinfo->input_iMCU_row < last_iMCU_row || 1.203 + yoffset+yindex < compptr->last_row_height) { 1.204 + output_col = start_col; 1.205 + for (xindex = 0; xindex < useful_width; xindex++) { 1.206 + (*inverse_DCT) (cinfo, compptr, 1.207 + (JCOEFPTR) coef->MCU_buffer[blkn+xindex], 1.208 + output_ptr, output_col); 1.209 + output_col += compptr->_DCT_scaled_size; 1.210 + } 1.211 + } 1.212 + blkn += compptr->MCU_width; 1.213 + output_ptr += compptr->_DCT_scaled_size; 1.214 + } 1.215 + } 1.216 + } 1.217 + /* Completed an MCU row, but perhaps not an iMCU row */ 1.218 + coef->MCU_ctr = 0; 1.219 + } 1.220 + /* Completed the iMCU row, advance counters for next one */ 1.221 + cinfo->output_iMCU_row++; 1.222 + if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { 1.223 + start_iMCU_row(cinfo); 1.224 + return JPEG_ROW_COMPLETED; 1.225 + } 1.226 + /* Completed the scan */ 1.227 + (*cinfo->inputctl->finish_input_pass) (cinfo); 1.228 + return JPEG_SCAN_COMPLETED; 1.229 +} 1.230 + 1.231 + 1.232 +/* 1.233 + * Dummy consume-input routine for single-pass operation. 1.234 + */ 1.235 + 1.236 +METHODDEF(int) 1.237 +dummy_consume_data (j_decompress_ptr cinfo) 1.238 +{ 1.239 + return JPEG_SUSPENDED; /* Always indicate nothing was done */ 1.240 +} 1.241 + 1.242 + 1.243 +#ifdef D_MULTISCAN_FILES_SUPPORTED 1.244 + 1.245 +/* 1.246 + * Consume input data and store it in the full-image coefficient buffer. 1.247 + * We read as much as one fully interleaved MCU row ("iMCU" row) per call, 1.248 + * ie, v_samp_factor block rows for each component in the scan. 1.249 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. 1.250 + */ 1.251 + 1.252 +METHODDEF(int) 1.253 +consume_data (j_decompress_ptr cinfo) 1.254 +{ 1.255 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.256 + JDIMENSION MCU_col_num; /* index of current MCU within row */ 1.257 + int blkn, ci, xindex, yindex, yoffset; 1.258 + JDIMENSION start_col; 1.259 + JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; 1.260 + JBLOCKROW buffer_ptr; 1.261 + jpeg_component_info *compptr; 1.262 + 1.263 + /* Align the virtual buffers for the components used in this scan. */ 1.264 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.265 + compptr = cinfo->cur_comp_info[ci]; 1.266 + buffer[ci] = (*cinfo->mem->access_virt_barray) 1.267 + ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], 1.268 + cinfo->input_iMCU_row * compptr->v_samp_factor, 1.269 + (JDIMENSION) compptr->v_samp_factor, TRUE); 1.270 + /* Note: entropy decoder expects buffer to be zeroed, 1.271 + * but this is handled automatically by the memory manager 1.272 + * because we requested a pre-zeroed array. 1.273 + */ 1.274 + } 1.275 + 1.276 + /* Loop to process one whole iMCU row */ 1.277 + for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 1.278 + yoffset++) { 1.279 + for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; 1.280 + MCU_col_num++) { 1.281 + /* Construct list of pointers to DCT blocks belonging to this MCU */ 1.282 + blkn = 0; /* index of current DCT block within MCU */ 1.283 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.284 + compptr = cinfo->cur_comp_info[ci]; 1.285 + start_col = MCU_col_num * compptr->MCU_width; 1.286 + for (yindex = 0; yindex < compptr->MCU_height; yindex++) { 1.287 + buffer_ptr = buffer[ci][yindex+yoffset] + start_col; 1.288 + for (xindex = 0; xindex < compptr->MCU_width; xindex++) { 1.289 + coef->MCU_buffer[blkn++] = buffer_ptr++; 1.290 + } 1.291 + } 1.292 + } 1.293 + /* Try to fetch the MCU. */ 1.294 + if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { 1.295 + /* Suspension forced; update state counters and exit */ 1.296 + coef->MCU_vert_offset = yoffset; 1.297 + coef->MCU_ctr = MCU_col_num; 1.298 + return JPEG_SUSPENDED; 1.299 + } 1.300 + } 1.301 + /* Completed an MCU row, but perhaps not an iMCU row */ 1.302 + coef->MCU_ctr = 0; 1.303 + } 1.304 + /* Completed the iMCU row, advance counters for next one */ 1.305 + if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { 1.306 + start_iMCU_row(cinfo); 1.307 + return JPEG_ROW_COMPLETED; 1.308 + } 1.309 + /* Completed the scan */ 1.310 + (*cinfo->inputctl->finish_input_pass) (cinfo); 1.311 + return JPEG_SCAN_COMPLETED; 1.312 +} 1.313 + 1.314 + 1.315 +/* 1.316 + * Decompress and return some data in the multi-pass case. 1.317 + * Always attempts to emit one fully interleaved MCU row ("iMCU" row). 1.318 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. 1.319 + * 1.320 + * NB: output_buf contains a plane for each component in image. 1.321 + */ 1.322 + 1.323 +METHODDEF(int) 1.324 +decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) 1.325 +{ 1.326 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.327 + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; 1.328 + JDIMENSION block_num; 1.329 + int ci, block_row, block_rows; 1.330 + JBLOCKARRAY buffer; 1.331 + JBLOCKROW buffer_ptr; 1.332 + JSAMPARRAY output_ptr; 1.333 + JDIMENSION output_col; 1.334 + jpeg_component_info *compptr; 1.335 + inverse_DCT_method_ptr inverse_DCT; 1.336 + 1.337 + /* Force some input to be done if we are getting ahead of the input. */ 1.338 + while (cinfo->input_scan_number < cinfo->output_scan_number || 1.339 + (cinfo->input_scan_number == cinfo->output_scan_number && 1.340 + cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) { 1.341 + if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) 1.342 + return JPEG_SUSPENDED; 1.343 + } 1.344 + 1.345 + /* OK, output from the virtual arrays. */ 1.346 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.347 + ci++, compptr++) { 1.348 + /* Don't bother to IDCT an uninteresting component. */ 1.349 + if (! compptr->component_needed) 1.350 + continue; 1.351 + /* Align the virtual buffer for this component. */ 1.352 + buffer = (*cinfo->mem->access_virt_barray) 1.353 + ((j_common_ptr) cinfo, coef->whole_image[ci], 1.354 + cinfo->output_iMCU_row * compptr->v_samp_factor, 1.355 + (JDIMENSION) compptr->v_samp_factor, FALSE); 1.356 + /* Count non-dummy DCT block rows in this iMCU row. */ 1.357 + if (cinfo->output_iMCU_row < last_iMCU_row) 1.358 + block_rows = compptr->v_samp_factor; 1.359 + else { 1.360 + /* NB: can't use last_row_height here; it is input-side-dependent! */ 1.361 + block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 1.362 + if (block_rows == 0) block_rows = compptr->v_samp_factor; 1.363 + } 1.364 + inverse_DCT = cinfo->idct->inverse_DCT[ci]; 1.365 + output_ptr = output_buf[ci]; 1.366 + /* Loop over all DCT blocks to be processed. */ 1.367 + for (block_row = 0; block_row < block_rows; block_row++) { 1.368 + buffer_ptr = buffer[block_row]; 1.369 + output_col = 0; 1.370 + for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) { 1.371 + (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr, 1.372 + output_ptr, output_col); 1.373 + buffer_ptr++; 1.374 + output_col += compptr->_DCT_scaled_size; 1.375 + } 1.376 + output_ptr += compptr->_DCT_scaled_size; 1.377 + } 1.378 + } 1.379 + 1.380 + if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) 1.381 + return JPEG_ROW_COMPLETED; 1.382 + return JPEG_SCAN_COMPLETED; 1.383 +} 1.384 + 1.385 +#endif /* D_MULTISCAN_FILES_SUPPORTED */ 1.386 + 1.387 + 1.388 +#ifdef BLOCK_SMOOTHING_SUPPORTED 1.389 + 1.390 +/* 1.391 + * This code applies interblock smoothing as described by section K.8 1.392 + * of the JPEG standard: the first 5 AC coefficients are estimated from 1.393 + * the DC values of a DCT block and its 8 neighboring blocks. 1.394 + * We apply smoothing only for progressive JPEG decoding, and only if 1.395 + * the coefficients it can estimate are not yet known to full precision. 1.396 + */ 1.397 + 1.398 +/* Natural-order array positions of the first 5 zigzag-order coefficients */ 1.399 +#define Q01_POS 1 1.400 +#define Q10_POS 8 1.401 +#define Q20_POS 16 1.402 +#define Q11_POS 9 1.403 +#define Q02_POS 2 1.404 + 1.405 +/* 1.406 + * Determine whether block smoothing is applicable and safe. 1.407 + * We also latch the current states of the coef_bits[] entries for the 1.408 + * AC coefficients; otherwise, if the input side of the decompressor 1.409 + * advances into a new scan, we might think the coefficients are known 1.410 + * more accurately than they really are. 1.411 + */ 1.412 + 1.413 +LOCAL(boolean) 1.414 +smoothing_ok (j_decompress_ptr cinfo) 1.415 +{ 1.416 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.417 + boolean smoothing_useful = FALSE; 1.418 + int ci, coefi; 1.419 + jpeg_component_info *compptr; 1.420 + JQUANT_TBL * qtable; 1.421 + int * coef_bits; 1.422 + int * coef_bits_latch; 1.423 + 1.424 + if (! cinfo->progressive_mode || cinfo->coef_bits == NULL) 1.425 + return FALSE; 1.426 + 1.427 + /* Allocate latch area if not already done */ 1.428 + if (coef->coef_bits_latch == NULL) 1.429 + coef->coef_bits_latch = (int *) 1.430 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.431 + cinfo->num_components * 1.432 + (SAVED_COEFS * SIZEOF(int))); 1.433 + coef_bits_latch = coef->coef_bits_latch; 1.434 + 1.435 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.436 + ci++, compptr++) { 1.437 + /* All components' quantization values must already be latched. */ 1.438 + if ((qtable = compptr->quant_table) == NULL) 1.439 + return FALSE; 1.440 + /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */ 1.441 + if (qtable->quantval[0] == 0 || 1.442 + qtable->quantval[Q01_POS] == 0 || 1.443 + qtable->quantval[Q10_POS] == 0 || 1.444 + qtable->quantval[Q20_POS] == 0 || 1.445 + qtable->quantval[Q11_POS] == 0 || 1.446 + qtable->quantval[Q02_POS] == 0) 1.447 + return FALSE; 1.448 + /* DC values must be at least partly known for all components. */ 1.449 + coef_bits = cinfo->coef_bits[ci]; 1.450 + if (coef_bits[0] < 0) 1.451 + return FALSE; 1.452 + /* Block smoothing is helpful if some AC coefficients remain inaccurate. */ 1.453 + for (coefi = 1; coefi <= 5; coefi++) { 1.454 + coef_bits_latch[coefi] = coef_bits[coefi]; 1.455 + if (coef_bits[coefi] != 0) 1.456 + smoothing_useful = TRUE; 1.457 + } 1.458 + coef_bits_latch += SAVED_COEFS; 1.459 + } 1.460 + 1.461 + return smoothing_useful; 1.462 +} 1.463 + 1.464 + 1.465 +/* 1.466 + * Variant of decompress_data for use when doing block smoothing. 1.467 + */ 1.468 + 1.469 +METHODDEF(int) 1.470 +decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) 1.471 +{ 1.472 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.473 + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; 1.474 + JDIMENSION block_num, last_block_column; 1.475 + int ci, block_row, block_rows, access_rows; 1.476 + JBLOCKARRAY buffer; 1.477 + JBLOCKROW buffer_ptr, prev_block_row, next_block_row; 1.478 + JSAMPARRAY output_ptr; 1.479 + JDIMENSION output_col; 1.480 + jpeg_component_info *compptr; 1.481 + inverse_DCT_method_ptr inverse_DCT; 1.482 + boolean first_row, last_row; 1.483 + JCOEF * workspace; 1.484 + int *coef_bits; 1.485 + JQUANT_TBL *quanttbl; 1.486 + INT32 Q00,Q01,Q02,Q10,Q11,Q20, num; 1.487 + int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9; 1.488 + int Al, pred; 1.489 + 1.490 + /* Keep a local variable to avoid looking it up more than once */ 1.491 + workspace = coef->workspace; 1.492 + 1.493 + /* Force some input to be done if we are getting ahead of the input. */ 1.494 + while (cinfo->input_scan_number <= cinfo->output_scan_number && 1.495 + ! cinfo->inputctl->eoi_reached) { 1.496 + if (cinfo->input_scan_number == cinfo->output_scan_number) { 1.497 + /* If input is working on current scan, we ordinarily want it to 1.498 + * have completed the current row. But if input scan is DC, 1.499 + * we want it to keep one row ahead so that next block row's DC 1.500 + * values are up to date. 1.501 + */ 1.502 + JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0; 1.503 + if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta) 1.504 + break; 1.505 + } 1.506 + if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) 1.507 + return JPEG_SUSPENDED; 1.508 + } 1.509 + 1.510 + /* OK, output from the virtual arrays. */ 1.511 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.512 + ci++, compptr++) { 1.513 + /* Don't bother to IDCT an uninteresting component. */ 1.514 + if (! compptr->component_needed) 1.515 + continue; 1.516 + /* Count non-dummy DCT block rows in this iMCU row. */ 1.517 + if (cinfo->output_iMCU_row < last_iMCU_row) { 1.518 + block_rows = compptr->v_samp_factor; 1.519 + access_rows = block_rows * 2; /* this and next iMCU row */ 1.520 + last_row = FALSE; 1.521 + } else { 1.522 + /* NB: can't use last_row_height here; it is input-side-dependent! */ 1.523 + block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 1.524 + if (block_rows == 0) block_rows = compptr->v_samp_factor; 1.525 + access_rows = block_rows; /* this iMCU row only */ 1.526 + last_row = TRUE; 1.527 + } 1.528 + /* Align the virtual buffer for this component. */ 1.529 + if (cinfo->output_iMCU_row > 0) { 1.530 + access_rows += compptr->v_samp_factor; /* prior iMCU row too */ 1.531 + buffer = (*cinfo->mem->access_virt_barray) 1.532 + ((j_common_ptr) cinfo, coef->whole_image[ci], 1.533 + (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor, 1.534 + (JDIMENSION) access_rows, FALSE); 1.535 + buffer += compptr->v_samp_factor; /* point to current iMCU row */ 1.536 + first_row = FALSE; 1.537 + } else { 1.538 + buffer = (*cinfo->mem->access_virt_barray) 1.539 + ((j_common_ptr) cinfo, coef->whole_image[ci], 1.540 + (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE); 1.541 + first_row = TRUE; 1.542 + } 1.543 + /* Fetch component-dependent info */ 1.544 + coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS); 1.545 + quanttbl = compptr->quant_table; 1.546 + Q00 = quanttbl->quantval[0]; 1.547 + Q01 = quanttbl->quantval[Q01_POS]; 1.548 + Q10 = quanttbl->quantval[Q10_POS]; 1.549 + Q20 = quanttbl->quantval[Q20_POS]; 1.550 + Q11 = quanttbl->quantval[Q11_POS]; 1.551 + Q02 = quanttbl->quantval[Q02_POS]; 1.552 + inverse_DCT = cinfo->idct->inverse_DCT[ci]; 1.553 + output_ptr = output_buf[ci]; 1.554 + /* Loop over all DCT blocks to be processed. */ 1.555 + for (block_row = 0; block_row < block_rows; block_row++) { 1.556 + buffer_ptr = buffer[block_row]; 1.557 + if (first_row && block_row == 0) 1.558 + prev_block_row = buffer_ptr; 1.559 + else 1.560 + prev_block_row = buffer[block_row-1]; 1.561 + if (last_row && block_row == block_rows-1) 1.562 + next_block_row = buffer_ptr; 1.563 + else 1.564 + next_block_row = buffer[block_row+1]; 1.565 + /* We fetch the surrounding DC values using a sliding-register approach. 1.566 + * Initialize all nine here so as to do the right thing on narrow pics. 1.567 + */ 1.568 + DC1 = DC2 = DC3 = (int) prev_block_row[0][0]; 1.569 + DC4 = DC5 = DC6 = (int) buffer_ptr[0][0]; 1.570 + DC7 = DC8 = DC9 = (int) next_block_row[0][0]; 1.571 + output_col = 0; 1.572 + last_block_column = compptr->width_in_blocks - 1; 1.573 + for (block_num = 0; block_num <= last_block_column; block_num++) { 1.574 + /* Fetch current DCT block into workspace so we can modify it. */ 1.575 + jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1); 1.576 + /* Update DC values */ 1.577 + if (block_num < last_block_column) { 1.578 + DC3 = (int) prev_block_row[1][0]; 1.579 + DC6 = (int) buffer_ptr[1][0]; 1.580 + DC9 = (int) next_block_row[1][0]; 1.581 + } 1.582 + /* Compute coefficient estimates per K.8. 1.583 + * An estimate is applied only if coefficient is still zero, 1.584 + * and is not known to be fully accurate. 1.585 + */ 1.586 + /* AC01 */ 1.587 + if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) { 1.588 + num = 36 * Q00 * (DC4 - DC6); 1.589 + if (num >= 0) { 1.590 + pred = (int) (((Q01<<7) + num) / (Q01<<8)); 1.591 + if (Al > 0 && pred >= (1<<Al)) 1.592 + pred = (1<<Al)-1; 1.593 + } else { 1.594 + pred = (int) (((Q01<<7) - num) / (Q01<<8)); 1.595 + if (Al > 0 && pred >= (1<<Al)) 1.596 + pred = (1<<Al)-1; 1.597 + pred = -pred; 1.598 + } 1.599 + workspace[1] = (JCOEF) pred; 1.600 + } 1.601 + /* AC10 */ 1.602 + if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) { 1.603 + num = 36 * Q00 * (DC2 - DC8); 1.604 + if (num >= 0) { 1.605 + pred = (int) (((Q10<<7) + num) / (Q10<<8)); 1.606 + if (Al > 0 && pred >= (1<<Al)) 1.607 + pred = (1<<Al)-1; 1.608 + } else { 1.609 + pred = (int) (((Q10<<7) - num) / (Q10<<8)); 1.610 + if (Al > 0 && pred >= (1<<Al)) 1.611 + pred = (1<<Al)-1; 1.612 + pred = -pred; 1.613 + } 1.614 + workspace[8] = (JCOEF) pred; 1.615 + } 1.616 + /* AC20 */ 1.617 + if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) { 1.618 + num = 9 * Q00 * (DC2 + DC8 - 2*DC5); 1.619 + if (num >= 0) { 1.620 + pred = (int) (((Q20<<7) + num) / (Q20<<8)); 1.621 + if (Al > 0 && pred >= (1<<Al)) 1.622 + pred = (1<<Al)-1; 1.623 + } else { 1.624 + pred = (int) (((Q20<<7) - num) / (Q20<<8)); 1.625 + if (Al > 0 && pred >= (1<<Al)) 1.626 + pred = (1<<Al)-1; 1.627 + pred = -pred; 1.628 + } 1.629 + workspace[16] = (JCOEF) pred; 1.630 + } 1.631 + /* AC11 */ 1.632 + if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) { 1.633 + num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9); 1.634 + if (num >= 0) { 1.635 + pred = (int) (((Q11<<7) + num) / (Q11<<8)); 1.636 + if (Al > 0 && pred >= (1<<Al)) 1.637 + pred = (1<<Al)-1; 1.638 + } else { 1.639 + pred = (int) (((Q11<<7) - num) / (Q11<<8)); 1.640 + if (Al > 0 && pred >= (1<<Al)) 1.641 + pred = (1<<Al)-1; 1.642 + pred = -pred; 1.643 + } 1.644 + workspace[9] = (JCOEF) pred; 1.645 + } 1.646 + /* AC02 */ 1.647 + if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) { 1.648 + num = 9 * Q00 * (DC4 + DC6 - 2*DC5); 1.649 + if (num >= 0) { 1.650 + pred = (int) (((Q02<<7) + num) / (Q02<<8)); 1.651 + if (Al > 0 && pred >= (1<<Al)) 1.652 + pred = (1<<Al)-1; 1.653 + } else { 1.654 + pred = (int) (((Q02<<7) - num) / (Q02<<8)); 1.655 + if (Al > 0 && pred >= (1<<Al)) 1.656 + pred = (1<<Al)-1; 1.657 + pred = -pred; 1.658 + } 1.659 + workspace[2] = (JCOEF) pred; 1.660 + } 1.661 + /* OK, do the IDCT */ 1.662 + (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace, 1.663 + output_ptr, output_col); 1.664 + /* Advance for next column */ 1.665 + DC1 = DC2; DC2 = DC3; 1.666 + DC4 = DC5; DC5 = DC6; 1.667 + DC7 = DC8; DC8 = DC9; 1.668 + buffer_ptr++, prev_block_row++, next_block_row++; 1.669 + output_col += compptr->_DCT_scaled_size; 1.670 + } 1.671 + output_ptr += compptr->_DCT_scaled_size; 1.672 + } 1.673 + } 1.674 + 1.675 + if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) 1.676 + return JPEG_ROW_COMPLETED; 1.677 + return JPEG_SCAN_COMPLETED; 1.678 +} 1.679 + 1.680 +#endif /* BLOCK_SMOOTHING_SUPPORTED */ 1.681 + 1.682 + 1.683 +/* 1.684 + * Initialize coefficient buffer controller. 1.685 + */ 1.686 + 1.687 +GLOBAL(void) 1.688 +jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer) 1.689 +{ 1.690 + my_coef_ptr coef; 1.691 + 1.692 + coef = (my_coef_ptr) 1.693 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.694 + SIZEOF(my_coef_controller)); 1.695 + cinfo->coef = (struct jpeg_d_coef_controller *) coef; 1.696 + coef->pub.start_input_pass = start_input_pass; 1.697 + coef->pub.start_output_pass = start_output_pass; 1.698 +#ifdef BLOCK_SMOOTHING_SUPPORTED 1.699 + coef->coef_bits_latch = NULL; 1.700 +#endif 1.701 + 1.702 + /* Create the coefficient buffer. */ 1.703 + if (need_full_buffer) { 1.704 +#ifdef D_MULTISCAN_FILES_SUPPORTED 1.705 + /* Allocate a full-image virtual array for each component, */ 1.706 + /* padded to a multiple of samp_factor DCT blocks in each direction. */ 1.707 + /* Note we ask for a pre-zeroed array. */ 1.708 + int ci, access_rows; 1.709 + jpeg_component_info *compptr; 1.710 + 1.711 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.712 + ci++, compptr++) { 1.713 + access_rows = compptr->v_samp_factor; 1.714 +#ifdef BLOCK_SMOOTHING_SUPPORTED 1.715 + /* If block smoothing could be used, need a bigger window */ 1.716 + if (cinfo->progressive_mode) 1.717 + access_rows *= 3; 1.718 +#endif 1.719 + coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) 1.720 + ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE, 1.721 + (JDIMENSION) jround_up((long) compptr->width_in_blocks, 1.722 + (long) compptr->h_samp_factor), 1.723 + (JDIMENSION) jround_up((long) compptr->height_in_blocks, 1.724 + (long) compptr->v_samp_factor), 1.725 + (JDIMENSION) access_rows); 1.726 + } 1.727 + coef->pub.consume_data = consume_data; 1.728 + coef->pub.decompress_data = decompress_data; 1.729 + coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */ 1.730 +#else 1.731 + ERREXIT(cinfo, JERR_NOT_COMPILED); 1.732 +#endif 1.733 + } else { 1.734 + /* We only need a single-MCU buffer. */ 1.735 + JBLOCKROW buffer; 1.736 + int i; 1.737 + 1.738 + buffer = (JBLOCKROW) 1.739 + (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.740 + D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); 1.741 + for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) { 1.742 + coef->MCU_buffer[i] = buffer + i; 1.743 + } 1.744 + coef->pub.consume_data = dummy_consume_data; 1.745 + coef->pub.decompress_data = decompress_onepass; 1.746 + coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */ 1.747 + } 1.748 + 1.749 + /* Allocate the workspace buffer */ 1.750 + coef->workspace = (JCOEF *) 1.751 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.752 + SIZEOF(JCOEF) * DCTSIZE2); 1.753 +}