media/libjpeg/jdcoefct.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial