istereo

annotate libs/libjpeg/jdcoefct.c @ 39:ff055bff6a15

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