istereo2

annotate libs/libjpeg/jdcoefct.c @ 9:64e15874f3bd

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