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