michael@0: /* michael@0: * jdcoefct.c michael@0: * michael@0: * This file was part of the Independent JPEG Group's software: michael@0: * Copyright (C) 1994-1997, Thomas G. Lane. michael@0: * libjpeg-turbo Modifications: michael@0: * Copyright (C) 2010, D. R. Commander. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains the coefficient buffer controller for decompression. michael@0: * This controller is the top level of the JPEG decompressor proper. michael@0: * The coefficient buffer lies between entropy decoding and inverse-DCT steps. michael@0: * michael@0: * In buffered-image mode, this controller is the interface between michael@0: * input-oriented processing and output-oriented processing. michael@0: * Also, the input side (only) is used when reading a file for transcoding. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: #include "jpegcomp.h" michael@0: michael@0: /* Block smoothing is only applicable for progressive JPEG, so: */ michael@0: #ifndef D_PROGRESSIVE_SUPPORTED michael@0: #undef BLOCK_SMOOTHING_SUPPORTED michael@0: #endif michael@0: michael@0: /* Private buffer controller object */ michael@0: michael@0: typedef struct { michael@0: struct jpeg_d_coef_controller pub; /* public fields */ michael@0: michael@0: /* These variables keep track of the current location of the input side. */ michael@0: /* cinfo->input_iMCU_row is also used for this. */ michael@0: JDIMENSION MCU_ctr; /* counts MCUs processed in current row */ michael@0: int MCU_vert_offset; /* counts MCU rows within iMCU row */ michael@0: int MCU_rows_per_iMCU_row; /* number of such rows needed */ michael@0: michael@0: /* The output side's location is represented by cinfo->output_iMCU_row. */ michael@0: michael@0: /* In single-pass modes, it's sufficient to buffer just one MCU. michael@0: * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks, michael@0: * and let the entropy decoder write into that workspace each time. michael@0: * (On 80x86, the workspace is FAR even though it's not really very big; michael@0: * this is to keep the module interfaces unchanged when a large coefficient michael@0: * buffer is necessary.) michael@0: * In multi-pass modes, this array points to the current MCU's blocks michael@0: * within the virtual arrays; it is used only by the input side. michael@0: */ michael@0: JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU]; michael@0: michael@0: /* Temporary workspace for one MCU */ michael@0: JCOEF * workspace; michael@0: michael@0: #ifdef D_MULTISCAN_FILES_SUPPORTED michael@0: /* In multi-pass modes, we need a virtual block array for each component. */ michael@0: jvirt_barray_ptr whole_image[MAX_COMPONENTS]; michael@0: #endif michael@0: michael@0: #ifdef BLOCK_SMOOTHING_SUPPORTED michael@0: /* When doing block smoothing, we latch coefficient Al values here */ michael@0: int * coef_bits_latch; michael@0: #define SAVED_COEFS 6 /* we save coef_bits[0..5] */ michael@0: #endif michael@0: } my_coef_controller; michael@0: michael@0: typedef my_coef_controller * my_coef_ptr; michael@0: michael@0: /* Forward declarations */ michael@0: METHODDEF(int) decompress_onepass michael@0: JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); michael@0: #ifdef D_MULTISCAN_FILES_SUPPORTED michael@0: METHODDEF(int) decompress_data michael@0: JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); michael@0: #endif michael@0: #ifdef BLOCK_SMOOTHING_SUPPORTED michael@0: LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo)); michael@0: METHODDEF(int) decompress_smooth_data michael@0: JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); michael@0: #endif michael@0: michael@0: michael@0: LOCAL(void) michael@0: start_iMCU_row (j_decompress_ptr cinfo) michael@0: /* Reset within-iMCU-row counters for a new row (input side) */ michael@0: { michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: michael@0: /* In an interleaved scan, an MCU row is the same as an iMCU row. michael@0: * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. michael@0: * But at the bottom of the image, process only what's left. michael@0: */ michael@0: if (cinfo->comps_in_scan > 1) { michael@0: coef->MCU_rows_per_iMCU_row = 1; michael@0: } else { michael@0: if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1)) michael@0: coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; michael@0: else michael@0: coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; michael@0: } michael@0: michael@0: coef->MCU_ctr = 0; michael@0: coef->MCU_vert_offset = 0; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize for an input processing pass. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_input_pass (j_decompress_ptr cinfo) michael@0: { michael@0: cinfo->input_iMCU_row = 0; michael@0: start_iMCU_row(cinfo); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize for an output processing pass. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_output_pass (j_decompress_ptr cinfo) michael@0: { michael@0: #ifdef BLOCK_SMOOTHING_SUPPORTED michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: michael@0: /* If multipass, check to see whether to use block smoothing on this pass */ michael@0: if (coef->pub.coef_arrays != NULL) { michael@0: if (cinfo->do_block_smoothing && smoothing_ok(cinfo)) michael@0: coef->pub.decompress_data = decompress_smooth_data; michael@0: else michael@0: coef->pub.decompress_data = decompress_data; michael@0: } michael@0: #endif michael@0: cinfo->output_iMCU_row = 0; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Decompress and return some data in the single-pass case. michael@0: * Always attempts to emit one fully interleaved MCU row ("iMCU" row). michael@0: * Input and output must run in lockstep since we have only a one-MCU buffer. michael@0: * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. michael@0: * michael@0: * NB: output_buf contains a plane for each component in image, michael@0: * which we index according to the component's SOF position. michael@0: */ michael@0: michael@0: METHODDEF(int) michael@0: decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) michael@0: { michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: JDIMENSION MCU_col_num; /* index of current MCU within row */ michael@0: JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; michael@0: JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; michael@0: int blkn, ci, xindex, yindex, yoffset, useful_width; michael@0: JSAMPARRAY output_ptr; michael@0: JDIMENSION start_col, output_col; michael@0: jpeg_component_info *compptr; michael@0: inverse_DCT_method_ptr inverse_DCT; michael@0: michael@0: /* Loop to process as much as one whole iMCU row */ michael@0: for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; michael@0: yoffset++) { michael@0: for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col; michael@0: MCU_col_num++) { michael@0: /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */ michael@0: jzero_far((void FAR *) coef->MCU_buffer[0], michael@0: (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK))); michael@0: if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { michael@0: /* Suspension forced; update state counters and exit */ michael@0: coef->MCU_vert_offset = yoffset; michael@0: coef->MCU_ctr = MCU_col_num; michael@0: return JPEG_SUSPENDED; michael@0: } michael@0: /* Determine where data should go in output_buf and do the IDCT thing. michael@0: * We skip dummy blocks at the right and bottom edges (but blkn gets michael@0: * incremented past them!). Note the inner loop relies on having michael@0: * allocated the MCU_buffer[] blocks sequentially. michael@0: */ michael@0: blkn = 0; /* index of current DCT block within MCU */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: /* Don't bother to IDCT an uninteresting component. */ michael@0: if (! compptr->component_needed) { michael@0: blkn += compptr->MCU_blocks; michael@0: continue; michael@0: } michael@0: inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index]; michael@0: useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width michael@0: : compptr->last_col_width; michael@0: output_ptr = output_buf[compptr->component_index] + michael@0: yoffset * compptr->_DCT_scaled_size; michael@0: start_col = MCU_col_num * compptr->MCU_sample_width; michael@0: for (yindex = 0; yindex < compptr->MCU_height; yindex++) { michael@0: if (cinfo->input_iMCU_row < last_iMCU_row || michael@0: yoffset+yindex < compptr->last_row_height) { michael@0: output_col = start_col; michael@0: for (xindex = 0; xindex < useful_width; xindex++) { michael@0: (*inverse_DCT) (cinfo, compptr, michael@0: (JCOEFPTR) coef->MCU_buffer[blkn+xindex], michael@0: output_ptr, output_col); michael@0: output_col += compptr->_DCT_scaled_size; michael@0: } michael@0: } michael@0: blkn += compptr->MCU_width; michael@0: output_ptr += compptr->_DCT_scaled_size; michael@0: } michael@0: } michael@0: } michael@0: /* Completed an MCU row, but perhaps not an iMCU row */ michael@0: coef->MCU_ctr = 0; michael@0: } michael@0: /* Completed the iMCU row, advance counters for next one */ michael@0: cinfo->output_iMCU_row++; michael@0: if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { michael@0: start_iMCU_row(cinfo); michael@0: return JPEG_ROW_COMPLETED; michael@0: } michael@0: /* Completed the scan */ michael@0: (*cinfo->inputctl->finish_input_pass) (cinfo); michael@0: return JPEG_SCAN_COMPLETED; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Dummy consume-input routine for single-pass operation. michael@0: */ michael@0: michael@0: METHODDEF(int) michael@0: dummy_consume_data (j_decompress_ptr cinfo) michael@0: { michael@0: return JPEG_SUSPENDED; /* Always indicate nothing was done */ michael@0: } michael@0: michael@0: michael@0: #ifdef D_MULTISCAN_FILES_SUPPORTED michael@0: michael@0: /* michael@0: * Consume input data and store it in the full-image coefficient buffer. michael@0: * We read as much as one fully interleaved MCU row ("iMCU" row) per call, michael@0: * ie, v_samp_factor block rows for each component in the scan. michael@0: * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. michael@0: */ michael@0: michael@0: METHODDEF(int) michael@0: consume_data (j_decompress_ptr cinfo) michael@0: { michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: JDIMENSION MCU_col_num; /* index of current MCU within row */ michael@0: int blkn, ci, xindex, yindex, yoffset; michael@0: JDIMENSION start_col; michael@0: JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; michael@0: JBLOCKROW buffer_ptr; michael@0: jpeg_component_info *compptr; michael@0: michael@0: /* Align the virtual buffers for the components used in this scan. */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: buffer[ci] = (*cinfo->mem->access_virt_barray) michael@0: ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], michael@0: cinfo->input_iMCU_row * compptr->v_samp_factor, michael@0: (JDIMENSION) compptr->v_samp_factor, TRUE); michael@0: /* Note: entropy decoder expects buffer to be zeroed, michael@0: * but this is handled automatically by the memory manager michael@0: * because we requested a pre-zeroed array. michael@0: */ michael@0: } michael@0: michael@0: /* Loop to process one whole iMCU row */ michael@0: for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; michael@0: yoffset++) { michael@0: for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; michael@0: MCU_col_num++) { michael@0: /* Construct list of pointers to DCT blocks belonging to this MCU */ michael@0: blkn = 0; /* index of current DCT block within MCU */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: start_col = MCU_col_num * compptr->MCU_width; michael@0: for (yindex = 0; yindex < compptr->MCU_height; yindex++) { michael@0: buffer_ptr = buffer[ci][yindex+yoffset] + start_col; michael@0: for (xindex = 0; xindex < compptr->MCU_width; xindex++) { michael@0: coef->MCU_buffer[blkn++] = buffer_ptr++; michael@0: } michael@0: } michael@0: } michael@0: /* Try to fetch the MCU. */ michael@0: if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { michael@0: /* Suspension forced; update state counters and exit */ michael@0: coef->MCU_vert_offset = yoffset; michael@0: coef->MCU_ctr = MCU_col_num; michael@0: return JPEG_SUSPENDED; michael@0: } michael@0: } michael@0: /* Completed an MCU row, but perhaps not an iMCU row */ michael@0: coef->MCU_ctr = 0; michael@0: } michael@0: /* Completed the iMCU row, advance counters for next one */ michael@0: if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { michael@0: start_iMCU_row(cinfo); michael@0: return JPEG_ROW_COMPLETED; michael@0: } michael@0: /* Completed the scan */ michael@0: (*cinfo->inputctl->finish_input_pass) (cinfo); michael@0: return JPEG_SCAN_COMPLETED; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Decompress and return some data in the multi-pass case. michael@0: * Always attempts to emit one fully interleaved MCU row ("iMCU" row). michael@0: * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. michael@0: * michael@0: * NB: output_buf contains a plane for each component in image. michael@0: */ michael@0: michael@0: METHODDEF(int) michael@0: decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) michael@0: { michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; michael@0: JDIMENSION block_num; michael@0: int ci, block_row, block_rows; michael@0: JBLOCKARRAY buffer; michael@0: JBLOCKROW buffer_ptr; michael@0: JSAMPARRAY output_ptr; michael@0: JDIMENSION output_col; michael@0: jpeg_component_info *compptr; michael@0: inverse_DCT_method_ptr inverse_DCT; michael@0: michael@0: /* Force some input to be done if we are getting ahead of the input. */ michael@0: while (cinfo->input_scan_number < cinfo->output_scan_number || michael@0: (cinfo->input_scan_number == cinfo->output_scan_number && michael@0: cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) { michael@0: if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) michael@0: return JPEG_SUSPENDED; michael@0: } michael@0: michael@0: /* OK, output from the virtual arrays. */ michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: /* Don't bother to IDCT an uninteresting component. */ michael@0: if (! compptr->component_needed) michael@0: continue; michael@0: /* Align the virtual buffer for this component. */ michael@0: buffer = (*cinfo->mem->access_virt_barray) michael@0: ((j_common_ptr) cinfo, coef->whole_image[ci], michael@0: cinfo->output_iMCU_row * compptr->v_samp_factor, michael@0: (JDIMENSION) compptr->v_samp_factor, FALSE); michael@0: /* Count non-dummy DCT block rows in this iMCU row. */ michael@0: if (cinfo->output_iMCU_row < last_iMCU_row) michael@0: block_rows = compptr->v_samp_factor; michael@0: else { michael@0: /* NB: can't use last_row_height here; it is input-side-dependent! */ michael@0: block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); michael@0: if (block_rows == 0) block_rows = compptr->v_samp_factor; michael@0: } michael@0: inverse_DCT = cinfo->idct->inverse_DCT[ci]; michael@0: output_ptr = output_buf[ci]; michael@0: /* Loop over all DCT blocks to be processed. */ michael@0: for (block_row = 0; block_row < block_rows; block_row++) { michael@0: buffer_ptr = buffer[block_row]; michael@0: output_col = 0; michael@0: for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) { michael@0: (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr, michael@0: output_ptr, output_col); michael@0: buffer_ptr++; michael@0: output_col += compptr->_DCT_scaled_size; michael@0: } michael@0: output_ptr += compptr->_DCT_scaled_size; michael@0: } michael@0: } michael@0: michael@0: if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) michael@0: return JPEG_ROW_COMPLETED; michael@0: return JPEG_SCAN_COMPLETED; michael@0: } michael@0: michael@0: #endif /* D_MULTISCAN_FILES_SUPPORTED */ michael@0: michael@0: michael@0: #ifdef BLOCK_SMOOTHING_SUPPORTED michael@0: michael@0: /* michael@0: * This code applies interblock smoothing as described by section K.8 michael@0: * of the JPEG standard: the first 5 AC coefficients are estimated from michael@0: * the DC values of a DCT block and its 8 neighboring blocks. michael@0: * We apply smoothing only for progressive JPEG decoding, and only if michael@0: * the coefficients it can estimate are not yet known to full precision. michael@0: */ michael@0: michael@0: /* Natural-order array positions of the first 5 zigzag-order coefficients */ michael@0: #define Q01_POS 1 michael@0: #define Q10_POS 8 michael@0: #define Q20_POS 16 michael@0: #define Q11_POS 9 michael@0: #define Q02_POS 2 michael@0: michael@0: /* michael@0: * Determine whether block smoothing is applicable and safe. michael@0: * We also latch the current states of the coef_bits[] entries for the michael@0: * AC coefficients; otherwise, if the input side of the decompressor michael@0: * advances into a new scan, we might think the coefficients are known michael@0: * more accurately than they really are. michael@0: */ michael@0: michael@0: LOCAL(boolean) michael@0: smoothing_ok (j_decompress_ptr cinfo) michael@0: { michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: boolean smoothing_useful = FALSE; michael@0: int ci, coefi; michael@0: jpeg_component_info *compptr; michael@0: JQUANT_TBL * qtable; michael@0: int * coef_bits; michael@0: int * coef_bits_latch; michael@0: michael@0: if (! cinfo->progressive_mode || cinfo->coef_bits == NULL) michael@0: return FALSE; michael@0: michael@0: /* Allocate latch area if not already done */ michael@0: if (coef->coef_bits_latch == NULL) michael@0: coef->coef_bits_latch = (int *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: cinfo->num_components * michael@0: (SAVED_COEFS * SIZEOF(int))); michael@0: coef_bits_latch = coef->coef_bits_latch; michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: /* All components' quantization values must already be latched. */ michael@0: if ((qtable = compptr->quant_table) == NULL) michael@0: return FALSE; michael@0: /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */ michael@0: if (qtable->quantval[0] == 0 || michael@0: qtable->quantval[Q01_POS] == 0 || michael@0: qtable->quantval[Q10_POS] == 0 || michael@0: qtable->quantval[Q20_POS] == 0 || michael@0: qtable->quantval[Q11_POS] == 0 || michael@0: qtable->quantval[Q02_POS] == 0) michael@0: return FALSE; michael@0: /* DC values must be at least partly known for all components. */ michael@0: coef_bits = cinfo->coef_bits[ci]; michael@0: if (coef_bits[0] < 0) michael@0: return FALSE; michael@0: /* Block smoothing is helpful if some AC coefficients remain inaccurate. */ michael@0: for (coefi = 1; coefi <= 5; coefi++) { michael@0: coef_bits_latch[coefi] = coef_bits[coefi]; michael@0: if (coef_bits[coefi] != 0) michael@0: smoothing_useful = TRUE; michael@0: } michael@0: coef_bits_latch += SAVED_COEFS; michael@0: } michael@0: michael@0: return smoothing_useful; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Variant of decompress_data for use when doing block smoothing. michael@0: */ michael@0: michael@0: METHODDEF(int) michael@0: decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) michael@0: { michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; michael@0: JDIMENSION block_num, last_block_column; michael@0: int ci, block_row, block_rows, access_rows; michael@0: JBLOCKARRAY buffer; michael@0: JBLOCKROW buffer_ptr, prev_block_row, next_block_row; michael@0: JSAMPARRAY output_ptr; michael@0: JDIMENSION output_col; michael@0: jpeg_component_info *compptr; michael@0: inverse_DCT_method_ptr inverse_DCT; michael@0: boolean first_row, last_row; michael@0: JCOEF * workspace; michael@0: int *coef_bits; michael@0: JQUANT_TBL *quanttbl; michael@0: INT32 Q00,Q01,Q02,Q10,Q11,Q20, num; michael@0: int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9; michael@0: int Al, pred; michael@0: michael@0: /* Keep a local variable to avoid looking it up more than once */ michael@0: workspace = coef->workspace; michael@0: michael@0: /* Force some input to be done if we are getting ahead of the input. */ michael@0: while (cinfo->input_scan_number <= cinfo->output_scan_number && michael@0: ! cinfo->inputctl->eoi_reached) { michael@0: if (cinfo->input_scan_number == cinfo->output_scan_number) { michael@0: /* If input is working on current scan, we ordinarily want it to michael@0: * have completed the current row. But if input scan is DC, michael@0: * we want it to keep one row ahead so that next block row's DC michael@0: * values are up to date. michael@0: */ michael@0: JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0; michael@0: if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta) michael@0: break; michael@0: } michael@0: if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) michael@0: return JPEG_SUSPENDED; michael@0: } michael@0: michael@0: /* OK, output from the virtual arrays. */ michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: /* Don't bother to IDCT an uninteresting component. */ michael@0: if (! compptr->component_needed) michael@0: continue; michael@0: /* Count non-dummy DCT block rows in this iMCU row. */ michael@0: if (cinfo->output_iMCU_row < last_iMCU_row) { michael@0: block_rows = compptr->v_samp_factor; michael@0: access_rows = block_rows * 2; /* this and next iMCU row */ michael@0: last_row = FALSE; michael@0: } else { michael@0: /* NB: can't use last_row_height here; it is input-side-dependent! */ michael@0: block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); michael@0: if (block_rows == 0) block_rows = compptr->v_samp_factor; michael@0: access_rows = block_rows; /* this iMCU row only */ michael@0: last_row = TRUE; michael@0: } michael@0: /* Align the virtual buffer for this component. */ michael@0: if (cinfo->output_iMCU_row > 0) { michael@0: access_rows += compptr->v_samp_factor; /* prior iMCU row too */ michael@0: buffer = (*cinfo->mem->access_virt_barray) michael@0: ((j_common_ptr) cinfo, coef->whole_image[ci], michael@0: (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor, michael@0: (JDIMENSION) access_rows, FALSE); michael@0: buffer += compptr->v_samp_factor; /* point to current iMCU row */ michael@0: first_row = FALSE; michael@0: } else { michael@0: buffer = (*cinfo->mem->access_virt_barray) michael@0: ((j_common_ptr) cinfo, coef->whole_image[ci], michael@0: (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE); michael@0: first_row = TRUE; michael@0: } michael@0: /* Fetch component-dependent info */ michael@0: coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS); michael@0: quanttbl = compptr->quant_table; michael@0: Q00 = quanttbl->quantval[0]; michael@0: Q01 = quanttbl->quantval[Q01_POS]; michael@0: Q10 = quanttbl->quantval[Q10_POS]; michael@0: Q20 = quanttbl->quantval[Q20_POS]; michael@0: Q11 = quanttbl->quantval[Q11_POS]; michael@0: Q02 = quanttbl->quantval[Q02_POS]; michael@0: inverse_DCT = cinfo->idct->inverse_DCT[ci]; michael@0: output_ptr = output_buf[ci]; michael@0: /* Loop over all DCT blocks to be processed. */ michael@0: for (block_row = 0; block_row < block_rows; block_row++) { michael@0: buffer_ptr = buffer[block_row]; michael@0: if (first_row && block_row == 0) michael@0: prev_block_row = buffer_ptr; michael@0: else michael@0: prev_block_row = buffer[block_row-1]; michael@0: if (last_row && block_row == block_rows-1) michael@0: next_block_row = buffer_ptr; michael@0: else michael@0: next_block_row = buffer[block_row+1]; michael@0: /* We fetch the surrounding DC values using a sliding-register approach. michael@0: * Initialize all nine here so as to do the right thing on narrow pics. michael@0: */ michael@0: DC1 = DC2 = DC3 = (int) prev_block_row[0][0]; michael@0: DC4 = DC5 = DC6 = (int) buffer_ptr[0][0]; michael@0: DC7 = DC8 = DC9 = (int) next_block_row[0][0]; michael@0: output_col = 0; michael@0: last_block_column = compptr->width_in_blocks - 1; michael@0: for (block_num = 0; block_num <= last_block_column; block_num++) { michael@0: /* Fetch current DCT block into workspace so we can modify it. */ michael@0: jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1); michael@0: /* Update DC values */ michael@0: if (block_num < last_block_column) { michael@0: DC3 = (int) prev_block_row[1][0]; michael@0: DC6 = (int) buffer_ptr[1][0]; michael@0: DC9 = (int) next_block_row[1][0]; michael@0: } michael@0: /* Compute coefficient estimates per K.8. michael@0: * An estimate is applied only if coefficient is still zero, michael@0: * and is not known to be fully accurate. michael@0: */ michael@0: /* AC01 */ michael@0: if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) { michael@0: num = 36 * Q00 * (DC4 - DC6); michael@0: if (num >= 0) { michael@0: pred = (int) (((Q01<<7) + num) / (Q01<<8)); michael@0: if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { michael@0: pred = (int) (((Q10<<7) + num) / (Q10<<8)); michael@0: if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { michael@0: pred = (int) (((Q20<<7) + num) / (Q20<<8)); michael@0: if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { michael@0: pred = (int) (((Q11<<7) + num) / (Q11<<8)); michael@0: if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { michael@0: pred = (int) (((Q02<<7) + num) / (Q02<<8)); michael@0: if (Al > 0 && pred >= (1< 0 && pred >= (1<_DCT_scaled_size; michael@0: } michael@0: output_ptr += compptr->_DCT_scaled_size; michael@0: } michael@0: } michael@0: michael@0: if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) michael@0: return JPEG_ROW_COMPLETED; michael@0: return JPEG_SCAN_COMPLETED; michael@0: } michael@0: michael@0: #endif /* BLOCK_SMOOTHING_SUPPORTED */ michael@0: michael@0: michael@0: /* michael@0: * Initialize coefficient buffer controller. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer) michael@0: { michael@0: my_coef_ptr coef; michael@0: michael@0: coef = (my_coef_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(my_coef_controller)); michael@0: cinfo->coef = (struct jpeg_d_coef_controller *) coef; michael@0: coef->pub.start_input_pass = start_input_pass; michael@0: coef->pub.start_output_pass = start_output_pass; michael@0: #ifdef BLOCK_SMOOTHING_SUPPORTED michael@0: coef->coef_bits_latch = NULL; michael@0: #endif michael@0: michael@0: /* Create the coefficient buffer. */ michael@0: if (need_full_buffer) { michael@0: #ifdef D_MULTISCAN_FILES_SUPPORTED michael@0: /* Allocate a full-image virtual array for each component, */ michael@0: /* padded to a multiple of samp_factor DCT blocks in each direction. */ michael@0: /* Note we ask for a pre-zeroed array. */ michael@0: int ci, access_rows; michael@0: jpeg_component_info *compptr; michael@0: michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: access_rows = compptr->v_samp_factor; michael@0: #ifdef BLOCK_SMOOTHING_SUPPORTED michael@0: /* If block smoothing could be used, need a bigger window */ michael@0: if (cinfo->progressive_mode) michael@0: access_rows *= 3; michael@0: #endif michael@0: coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) michael@0: ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE, michael@0: (JDIMENSION) jround_up((long) compptr->width_in_blocks, michael@0: (long) compptr->h_samp_factor), michael@0: (JDIMENSION) jround_up((long) compptr->height_in_blocks, michael@0: (long) compptr->v_samp_factor), michael@0: (JDIMENSION) access_rows); michael@0: } michael@0: coef->pub.consume_data = consume_data; michael@0: coef->pub.decompress_data = decompress_data; michael@0: coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */ michael@0: #else michael@0: ERREXIT(cinfo, JERR_NOT_COMPILED); michael@0: #endif michael@0: } else { michael@0: /* We only need a single-MCU buffer. */ michael@0: JBLOCKROW buffer; michael@0: int i; michael@0: michael@0: buffer = (JBLOCKROW) michael@0: (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); michael@0: for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) { michael@0: coef->MCU_buffer[i] = buffer + i; michael@0: } michael@0: coef->pub.consume_data = dummy_consume_data; michael@0: coef->pub.decompress_data = decompress_onepass; michael@0: coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */ michael@0: } michael@0: michael@0: /* Allocate the workspace buffer */ michael@0: coef->workspace = (JCOEF *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(JCOEF) * DCTSIZE2); michael@0: }