Back to index

lightning-sunbird  0.9+nobinonly
jdcoefct.c
Go to the documentation of this file.
00001 /*
00002  * jdcoefct.c
00003  *
00004  * Copyright (C) 1994-1997, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains the coefficient buffer controller for decompression.
00009  * This controller is the top level of the JPEG decompressor proper.
00010  * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
00011  *
00012  * In buffered-image mode, this controller is the interface between
00013  * input-oriented processing and output-oriented processing.
00014  * Also, the input side (only) is used when reading a file for transcoding.
00015  */
00016 
00017 #define JPEG_INTERNALS
00018 #include "jinclude.h"
00019 #include "jpeglib.h"
00020 
00021 /* Block smoothing is only applicable for progressive JPEG, so: */
00022 #ifndef D_PROGRESSIVE_SUPPORTED
00023 #undef BLOCK_SMOOTHING_SUPPORTED
00024 #endif
00025 
00026 /* Private buffer controller object */
00027 
00028 typedef struct {
00029   struct jpeg_d_coef_controller pub; /* public fields */
00030 
00031   /* These variables keep track of the current location of the input side. */
00032   /* cinfo->input_iMCU_row is also used for this. */
00033   JDIMENSION MCU_ctr;              /* counts MCUs processed in current row */
00034   int MCU_vert_offset;             /* counts MCU rows within iMCU row */
00035   int MCU_rows_per_iMCU_row;       /* number of such rows needed */
00036 
00037   /* The output side's location is represented by cinfo->output_iMCU_row. */
00038 
00039   /* In single-pass modes, it's sufficient to buffer just one MCU.
00040    * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
00041    * and let the entropy decoder write into that workspace each time.
00042    * (On 80x86, the workspace is FAR even though it's not really very big;
00043    * this is to keep the module interfaces unchanged when a large coefficient
00044    * buffer is necessary.)
00045    * In multi-pass modes, this array points to the current MCU's blocks
00046    * within the virtual arrays; it is used only by the input side.
00047    */
00048   JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
00049 
00050 #ifdef D_MULTISCAN_FILES_SUPPORTED
00051   /* In multi-pass modes, we need a virtual block array for each component. */
00052   jvirt_barray_ptr whole_image[MAX_COMPONENTS];
00053 #endif
00054 
00055 #ifdef BLOCK_SMOOTHING_SUPPORTED
00056   /* When doing block smoothing, we latch coefficient Al values here */
00057   int * coef_bits_latch;
00058 #define SAVED_COEFS  6             /* we save coef_bits[0..5] */
00059 #endif
00060 } my_coef_controller;
00061 
00062 typedef my_coef_controller * my_coef_ptr;
00063 
00064 /* Forward declarations */
00065 METHODDEF(int) decompress_onepass
00066        JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
00067 #ifdef D_MULTISCAN_FILES_SUPPORTED
00068 METHODDEF(int) decompress_data
00069        JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
00070 #endif
00071 #ifdef BLOCK_SMOOTHING_SUPPORTED
00072 LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
00073 METHODDEF(int) decompress_smooth_data
00074        JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
00075 #endif
00076 
00077 
00078 LOCAL(void)
00079 start_iMCU_row (j_decompress_ptr cinfo)
00080 /* Reset within-iMCU-row counters for a new row (input side) */
00081 {
00082   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00083 
00084   /* In an interleaved scan, an MCU row is the same as an iMCU row.
00085    * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
00086    * But at the bottom of the image, process only what's left.
00087    */
00088   if (cinfo->comps_in_scan > 1) {
00089     coef->MCU_rows_per_iMCU_row = 1;
00090   } else {
00091     if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
00092       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
00093     else
00094       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
00095   }
00096 
00097   coef->MCU_ctr = 0;
00098   coef->MCU_vert_offset = 0;
00099 }
00100 
00101 
00102 /*
00103  * Initialize for an input processing pass.
00104  */
00105 
00106 METHODDEF(void)
00107 start_input_pass (j_decompress_ptr cinfo)
00108 {
00109   cinfo->input_iMCU_row = 0;
00110   start_iMCU_row(cinfo);
00111 }
00112 
00113 
00114 /*
00115  * Initialize for an output processing pass.
00116  */
00117 
00118 METHODDEF(void)
00119 start_output_pass (j_decompress_ptr cinfo)
00120 {
00121 #ifdef BLOCK_SMOOTHING_SUPPORTED
00122   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00123 
00124   /* If multipass, check to see whether to use block smoothing on this pass */
00125   if (coef->pub.coef_arrays != NULL) {
00126     if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
00127       coef->pub.decompress_data = decompress_smooth_data;
00128     else
00129       coef->pub.decompress_data = decompress_data;
00130   }
00131 #endif
00132   cinfo->output_iMCU_row = 0;
00133 }
00134 
00135 
00136 /*
00137  * Decompress and return some data in the single-pass case.
00138  * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
00139  * Input and output must run in lockstep since we have only a one-MCU buffer.
00140  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
00141  *
00142  * NB: output_buf contains a plane for each component in image,
00143  * which we index according to the component's SOF position.
00144  */
00145 
00146 METHODDEF(int)
00147 decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
00148 {
00149   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00150   JDIMENSION MCU_col_num;   /* index of current MCU within row */
00151   JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
00152   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
00153   int blkn, ci, xindex, yindex, yoffset, useful_width;
00154   JSAMPARRAY output_ptr;
00155   JDIMENSION start_col, output_col;
00156   jpeg_component_info *compptr;
00157   inverse_DCT_method_ptr inverse_DCT;
00158 
00159   /* Loop to process as much as one whole iMCU row */
00160   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
00161        yoffset++) {
00162     for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
00163         MCU_col_num++) {
00164       /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
00165       jzero_far((void FAR *) coef->MCU_buffer[0],
00166               (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
00167       if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
00168        /* Suspension forced; update state counters and exit */
00169        coef->MCU_vert_offset = yoffset;
00170        coef->MCU_ctr = MCU_col_num;
00171        return JPEG_SUSPENDED;
00172       }
00173       /* Determine where data should go in output_buf and do the IDCT thing.
00174        * We skip dummy blocks at the right and bottom edges (but blkn gets
00175        * incremented past them!).  Note the inner loop relies on having
00176        * allocated the MCU_buffer[] blocks sequentially.
00177        */
00178       blkn = 0;                    /* index of current DCT block within MCU */
00179       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00180        compptr = cinfo->cur_comp_info[ci];
00181        /* Don't bother to IDCT an uninteresting component. */
00182        if (! compptr->component_needed) {
00183          blkn += compptr->MCU_blocks;
00184          continue;
00185        }
00186        inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
00187        useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
00188                                               : compptr->last_col_width;
00189        output_ptr = output_buf[compptr->component_index] +
00190          yoffset * compptr->DCT_scaled_size;
00191        start_col = MCU_col_num * compptr->MCU_sample_width;
00192        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
00193          if (cinfo->input_iMCU_row < last_iMCU_row ||
00194              yoffset+yindex < compptr->last_row_height) {
00195            output_col = start_col;
00196            for (xindex = 0; xindex < useful_width; xindex++) {
00197              (*inverse_DCT) (cinfo, compptr,
00198                            (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
00199                            output_ptr, output_col);
00200              output_col += compptr->DCT_scaled_size;
00201            }
00202          }
00203          blkn += compptr->MCU_width;
00204          output_ptr += compptr->DCT_scaled_size;
00205        }
00206       }
00207     }
00208     /* Completed an MCU row, but perhaps not an iMCU row */
00209     coef->MCU_ctr = 0;
00210   }
00211   /* Completed the iMCU row, advance counters for next one */
00212   cinfo->output_iMCU_row++;
00213   if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
00214     start_iMCU_row(cinfo);
00215     return JPEG_ROW_COMPLETED;
00216   }
00217   /* Completed the scan */
00218   (*cinfo->inputctl->finish_input_pass) (cinfo);
00219   return JPEG_SCAN_COMPLETED;
00220 }
00221 
00222 
00223 /*
00224  * Dummy consume-input routine for single-pass operation.
00225  */
00226 
00227 METHODDEF(int)
00228 dummy_consume_data (j_decompress_ptr cinfo)
00229 {
00230   return JPEG_SUSPENDED;    /* Always indicate nothing was done */
00231 }
00232 
00233 
00234 #ifdef D_MULTISCAN_FILES_SUPPORTED
00235 
00236 /*
00237  * Consume input data and store it in the full-image coefficient buffer.
00238  * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
00239  * ie, v_samp_factor block rows for each component in the scan.
00240  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
00241  */
00242 
00243 METHODDEF(int)
00244 consume_data (j_decompress_ptr cinfo)
00245 {
00246   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00247   JDIMENSION MCU_col_num;   /* index of current MCU within row */
00248   int blkn, ci, xindex, yindex, yoffset;
00249   JDIMENSION start_col;
00250   JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
00251   JBLOCKROW buffer_ptr;
00252   jpeg_component_info *compptr;
00253 
00254   /* Align the virtual buffers for the components used in this scan. */
00255   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00256     compptr = cinfo->cur_comp_info[ci];
00257     buffer[ci] = (*cinfo->mem->access_virt_barray)
00258       ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
00259        cinfo->input_iMCU_row * compptr->v_samp_factor,
00260        (JDIMENSION) compptr->v_samp_factor, TRUE);
00261     /* Note: entropy decoder expects buffer to be zeroed,
00262      * but this is handled automatically by the memory manager
00263      * because we requested a pre-zeroed array.
00264      */
00265   }
00266 
00267   /* Loop to process one whole iMCU row */
00268   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
00269        yoffset++) {
00270     for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
00271         MCU_col_num++) {
00272       /* Construct list of pointers to DCT blocks belonging to this MCU */
00273       blkn = 0;                    /* index of current DCT block within MCU */
00274       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00275        compptr = cinfo->cur_comp_info[ci];
00276        start_col = MCU_col_num * compptr->MCU_width;
00277        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
00278          buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
00279          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
00280            coef->MCU_buffer[blkn++] = buffer_ptr++;
00281          }
00282        }
00283       }
00284       /* Try to fetch the MCU. */
00285       if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
00286        /* Suspension forced; update state counters and exit */
00287        coef->MCU_vert_offset = yoffset;
00288        coef->MCU_ctr = MCU_col_num;
00289        return JPEG_SUSPENDED;
00290       }
00291     }
00292     /* Completed an MCU row, but perhaps not an iMCU row */
00293     coef->MCU_ctr = 0;
00294   }
00295   /* Completed the iMCU row, advance counters for next one */
00296   if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
00297     start_iMCU_row(cinfo);
00298     return JPEG_ROW_COMPLETED;
00299   }
00300   /* Completed the scan */
00301   (*cinfo->inputctl->finish_input_pass) (cinfo);
00302   return JPEG_SCAN_COMPLETED;
00303 }
00304 
00305 
00306 /*
00307  * Decompress and return some data in the multi-pass case.
00308  * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
00309  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
00310  *
00311  * NB: output_buf contains a plane for each component in image.
00312  */
00313 
00314 METHODDEF(int)
00315 decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
00316 {
00317   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00318   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
00319   JDIMENSION block_num;
00320   int ci, block_row, block_rows;
00321   JBLOCKARRAY buffer;
00322   JBLOCKROW buffer_ptr;
00323   JSAMPARRAY output_ptr;
00324   JDIMENSION output_col;
00325   jpeg_component_info *compptr;
00326   inverse_DCT_method_ptr inverse_DCT;
00327 
00328   /* Force some input to be done if we are getting ahead of the input. */
00329   while (cinfo->input_scan_number < cinfo->output_scan_number ||
00330         (cinfo->input_scan_number == cinfo->output_scan_number &&
00331          cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
00332     if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
00333       return JPEG_SUSPENDED;
00334   }
00335 
00336   /* OK, output from the virtual arrays. */
00337   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00338        ci++, compptr++) {
00339     /* Don't bother to IDCT an uninteresting component. */
00340     if (! compptr->component_needed)
00341       continue;
00342     /* Align the virtual buffer for this component. */
00343     buffer = (*cinfo->mem->access_virt_barray)
00344       ((j_common_ptr) cinfo, coef->whole_image[ci],
00345        cinfo->output_iMCU_row * compptr->v_samp_factor,
00346        (JDIMENSION) compptr->v_samp_factor, FALSE);
00347     /* Count non-dummy DCT block rows in this iMCU row. */
00348     if (cinfo->output_iMCU_row < last_iMCU_row)
00349       block_rows = compptr->v_samp_factor;
00350     else {
00351       /* NB: can't use last_row_height here; it is input-side-dependent! */
00352       block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
00353       if (block_rows == 0) block_rows = compptr->v_samp_factor;
00354     }
00355     inverse_DCT = cinfo->idct->inverse_DCT[ci];
00356     output_ptr = output_buf[ci];
00357     /* Loop over all DCT blocks to be processed. */
00358     for (block_row = 0; block_row < block_rows; block_row++) {
00359       buffer_ptr = buffer[block_row];
00360       output_col = 0;
00361       for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
00362        (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
00363                      output_ptr, output_col);
00364        buffer_ptr++;
00365        output_col += compptr->DCT_scaled_size;
00366       }
00367       output_ptr += compptr->DCT_scaled_size;
00368     }
00369   }
00370 
00371   if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
00372     return JPEG_ROW_COMPLETED;
00373   return JPEG_SCAN_COMPLETED;
00374 }
00375 
00376 #endif /* D_MULTISCAN_FILES_SUPPORTED */
00377 
00378 
00379 #ifdef BLOCK_SMOOTHING_SUPPORTED
00380 
00381 /*
00382  * This code applies interblock smoothing as described by section K.8
00383  * of the JPEG standard: the first 5 AC coefficients are estimated from
00384  * the DC values of a DCT block and its 8 neighboring blocks.
00385  * We apply smoothing only for progressive JPEG decoding, and only if
00386  * the coefficients it can estimate are not yet known to full precision.
00387  */
00388 
00389 /* Natural-order array positions of the first 5 zigzag-order coefficients */
00390 #define Q01_POS  1
00391 #define Q10_POS  8
00392 #define Q20_POS  16
00393 #define Q11_POS  9
00394 #define Q02_POS  2
00395 
00396 /*
00397  * Determine whether block smoothing is applicable and safe.
00398  * We also latch the current states of the coef_bits[] entries for the
00399  * AC coefficients; otherwise, if the input side of the decompressor
00400  * advances into a new scan, we might think the coefficients are known
00401  * more accurately than they really are.
00402  */
00403 
00404 LOCAL(boolean)
00405 smoothing_ok (j_decompress_ptr cinfo)
00406 {
00407   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00408   boolean smoothing_useful = FALSE;
00409   int ci, coefi;
00410   jpeg_component_info *compptr;
00411   JQUANT_TBL * qtable;
00412   int * coef_bits;
00413   int * coef_bits_latch;
00414 
00415   if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
00416     return FALSE;
00417 
00418   /* Allocate latch area if not already done */
00419   if (coef->coef_bits_latch == NULL)
00420     coef->coef_bits_latch = (int *)
00421       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00422                               cinfo->num_components *
00423                               (SAVED_COEFS * SIZEOF(int)));
00424   coef_bits_latch = coef->coef_bits_latch;
00425 
00426   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00427        ci++, compptr++) {
00428     /* All components' quantization values must already be latched. */
00429     if ((qtable = compptr->quant_table) == NULL)
00430       return FALSE;
00431     /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
00432     if (qtable->quantval[0] == 0 ||
00433        qtable->quantval[Q01_POS] == 0 ||
00434        qtable->quantval[Q10_POS] == 0 ||
00435        qtable->quantval[Q20_POS] == 0 ||
00436        qtable->quantval[Q11_POS] == 0 ||
00437        qtable->quantval[Q02_POS] == 0)
00438       return FALSE;
00439     /* DC values must be at least partly known for all components. */
00440     coef_bits = cinfo->coef_bits[ci];
00441     if (coef_bits[0] < 0)
00442       return FALSE;
00443     /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
00444     for (coefi = 1; coefi <= 5; coefi++) {
00445       coef_bits_latch[coefi] = coef_bits[coefi];
00446       if (coef_bits[coefi] != 0)
00447        smoothing_useful = TRUE;
00448     }
00449     coef_bits_latch += SAVED_COEFS;
00450   }
00451 
00452   return smoothing_useful;
00453 }
00454 
00455 
00456 /*
00457  * Variant of decompress_data for use when doing block smoothing.
00458  */
00459 
00460 METHODDEF(int)
00461 decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
00462 {
00463   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00464   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
00465   JDIMENSION block_num, last_block_column;
00466   int ci, block_row, block_rows, access_rows;
00467   JBLOCKARRAY buffer;
00468   JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
00469   JSAMPARRAY output_ptr;
00470   JDIMENSION output_col;
00471   jpeg_component_info *compptr;
00472   inverse_DCT_method_ptr inverse_DCT;
00473   boolean first_row, last_row;
00474   JBLOCK workspace;
00475   int *coef_bits;
00476   JQUANT_TBL *quanttbl;
00477   INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
00478   int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
00479   int Al, pred;
00480 
00481   /* Force some input to be done if we are getting ahead of the input. */
00482   while (cinfo->input_scan_number <= cinfo->output_scan_number &&
00483         ! cinfo->inputctl->eoi_reached) {
00484     if (cinfo->input_scan_number == cinfo->output_scan_number) {
00485       /* If input is working on current scan, we ordinarily want it to
00486        * have completed the current row.  But if input scan is DC,
00487        * we want it to keep one row ahead so that next block row's DC
00488        * values are up to date.
00489        */
00490       JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
00491       if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
00492        break;
00493     }
00494     if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
00495       return JPEG_SUSPENDED;
00496   }
00497 
00498   /* OK, output from the virtual arrays. */
00499   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00500        ci++, compptr++) {
00501     /* Don't bother to IDCT an uninteresting component. */
00502     if (! compptr->component_needed)
00503       continue;
00504     /* Count non-dummy DCT block rows in this iMCU row. */
00505     if (cinfo->output_iMCU_row < last_iMCU_row) {
00506       block_rows = compptr->v_samp_factor;
00507       access_rows = block_rows * 2; /* this and next iMCU row */
00508       last_row = FALSE;
00509     } else {
00510       /* NB: can't use last_row_height here; it is input-side-dependent! */
00511       block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
00512       if (block_rows == 0) block_rows = compptr->v_samp_factor;
00513       access_rows = block_rows; /* this iMCU row only */
00514       last_row = TRUE;
00515     }
00516     /* Align the virtual buffer for this component. */
00517     if (cinfo->output_iMCU_row > 0) {
00518       access_rows += compptr->v_samp_factor; /* prior iMCU row too */
00519       buffer = (*cinfo->mem->access_virt_barray)
00520        ((j_common_ptr) cinfo, coef->whole_image[ci],
00521         (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
00522         (JDIMENSION) access_rows, FALSE);
00523       buffer += compptr->v_samp_factor;   /* point to current iMCU row */
00524       first_row = FALSE;
00525     } else {
00526       buffer = (*cinfo->mem->access_virt_barray)
00527        ((j_common_ptr) cinfo, coef->whole_image[ci],
00528         (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
00529       first_row = TRUE;
00530     }
00531     /* Fetch component-dependent info */
00532     coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
00533     quanttbl = compptr->quant_table;
00534     Q00 = quanttbl->quantval[0];
00535     Q01 = quanttbl->quantval[Q01_POS];
00536     Q10 = quanttbl->quantval[Q10_POS];
00537     Q20 = quanttbl->quantval[Q20_POS];
00538     Q11 = quanttbl->quantval[Q11_POS];
00539     Q02 = quanttbl->quantval[Q02_POS];
00540     inverse_DCT = cinfo->idct->inverse_DCT[ci];
00541     output_ptr = output_buf[ci];
00542     /* Loop over all DCT blocks to be processed. */
00543     for (block_row = 0; block_row < block_rows; block_row++) {
00544       buffer_ptr = buffer[block_row];
00545       if (first_row && block_row == 0)
00546        prev_block_row = buffer_ptr;
00547       else
00548        prev_block_row = buffer[block_row-1];
00549       if (last_row && block_row == block_rows-1)
00550        next_block_row = buffer_ptr;
00551       else
00552        next_block_row = buffer[block_row+1];
00553       /* We fetch the surrounding DC values using a sliding-register approach.
00554        * Initialize all nine here so as to do the right thing on narrow pics.
00555        */
00556       DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
00557       DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
00558       DC7 = DC8 = DC9 = (int) next_block_row[0][0];
00559       output_col = 0;
00560       last_block_column = compptr->width_in_blocks - 1;
00561       for (block_num = 0; block_num <= last_block_column; block_num++) {
00562        /* Fetch current DCT block into workspace so we can modify it. */
00563        jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
00564        /* Update DC values */
00565        if (block_num < last_block_column) {
00566          DC3 = (int) prev_block_row[1][0];
00567          DC6 = (int) buffer_ptr[1][0];
00568          DC9 = (int) next_block_row[1][0];
00569        }
00570        /* Compute coefficient estimates per K.8.
00571         * An estimate is applied only if coefficient is still zero,
00572         * and is not known to be fully accurate.
00573         */
00574        /* AC01 */
00575        if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
00576          num = 36 * Q00 * (DC4 - DC6);
00577          if (num >= 0) {
00578            pred = (int) (((Q01<<7) + num) / (Q01<<8));
00579            if (Al > 0 && pred >= (1<<Al))
00580              pred = (1<<Al)-1;
00581          } else {
00582            pred = (int) (((Q01<<7) - num) / (Q01<<8));
00583            if (Al > 0 && pred >= (1<<Al))
00584              pred = (1<<Al)-1;
00585            pred = -pred;
00586          }
00587          workspace[1] = (JCOEF) pred;
00588        }
00589        /* AC10 */
00590        if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
00591          num = 36 * Q00 * (DC2 - DC8);
00592          if (num >= 0) {
00593            pred = (int) (((Q10<<7) + num) / (Q10<<8));
00594            if (Al > 0 && pred >= (1<<Al))
00595              pred = (1<<Al)-1;
00596          } else {
00597            pred = (int) (((Q10<<7) - num) / (Q10<<8));
00598            if (Al > 0 && pred >= (1<<Al))
00599              pred = (1<<Al)-1;
00600            pred = -pred;
00601          }
00602          workspace[8] = (JCOEF) pred;
00603        }
00604        /* AC20 */
00605        if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
00606          num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
00607          if (num >= 0) {
00608            pred = (int) (((Q20<<7) + num) / (Q20<<8));
00609            if (Al > 0 && pred >= (1<<Al))
00610              pred = (1<<Al)-1;
00611          } else {
00612            pred = (int) (((Q20<<7) - num) / (Q20<<8));
00613            if (Al > 0 && pred >= (1<<Al))
00614              pred = (1<<Al)-1;
00615            pred = -pred;
00616          }
00617          workspace[16] = (JCOEF) pred;
00618        }
00619        /* AC11 */
00620        if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
00621          num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
00622          if (num >= 0) {
00623            pred = (int) (((Q11<<7) + num) / (Q11<<8));
00624            if (Al > 0 && pred >= (1<<Al))
00625              pred = (1<<Al)-1;
00626          } else {
00627            pred = (int) (((Q11<<7) - num) / (Q11<<8));
00628            if (Al > 0 && pred >= (1<<Al))
00629              pred = (1<<Al)-1;
00630            pred = -pred;
00631          }
00632          workspace[9] = (JCOEF) pred;
00633        }
00634        /* AC02 */
00635        if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
00636          num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
00637          if (num >= 0) {
00638            pred = (int) (((Q02<<7) + num) / (Q02<<8));
00639            if (Al > 0 && pred >= (1<<Al))
00640              pred = (1<<Al)-1;
00641          } else {
00642            pred = (int) (((Q02<<7) - num) / (Q02<<8));
00643            if (Al > 0 && pred >= (1<<Al))
00644              pred = (1<<Al)-1;
00645            pred = -pred;
00646          }
00647          workspace[2] = (JCOEF) pred;
00648        }
00649        /* OK, do the IDCT */
00650        (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
00651                      output_ptr, output_col);
00652        /* Advance for next column */
00653        DC1 = DC2; DC2 = DC3;
00654        DC4 = DC5; DC5 = DC6;
00655        DC7 = DC8; DC8 = DC9;
00656        buffer_ptr++, prev_block_row++, next_block_row++;
00657        output_col += compptr->DCT_scaled_size;
00658       }
00659       output_ptr += compptr->DCT_scaled_size;
00660     }
00661   }
00662 
00663   if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
00664     return JPEG_ROW_COMPLETED;
00665   return JPEG_SCAN_COMPLETED;
00666 }
00667 
00668 #endif /* BLOCK_SMOOTHING_SUPPORTED */
00669 
00670 
00671 /*
00672  * Initialize coefficient buffer controller.
00673  */
00674 
00675 GLOBAL(void)
00676 jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
00677 {
00678   my_coef_ptr coef;
00679 
00680   coef = (my_coef_ptr)
00681     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00682                             SIZEOF(my_coef_controller));
00683   cinfo->coef = (struct jpeg_d_coef_controller *) coef;
00684   coef->pub.start_input_pass = start_input_pass;
00685   coef->pub.start_output_pass = start_output_pass;
00686 #ifdef BLOCK_SMOOTHING_SUPPORTED
00687   coef->coef_bits_latch = NULL;
00688 #endif
00689 
00690   /* Create the coefficient buffer. */
00691   if (need_full_buffer) {
00692 #ifdef D_MULTISCAN_FILES_SUPPORTED
00693     /* Allocate a full-image virtual array for each component, */
00694     /* padded to a multiple of samp_factor DCT blocks in each direction. */
00695     /* Note we ask for a pre-zeroed array. */
00696     int ci, access_rows;
00697     jpeg_component_info *compptr;
00698 
00699     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00700         ci++, compptr++) {
00701       access_rows = compptr->v_samp_factor;
00702 #ifdef BLOCK_SMOOTHING_SUPPORTED
00703       /* If block smoothing could be used, need a bigger window */
00704       if (cinfo->progressive_mode)
00705        access_rows *= 3;
00706 #endif
00707       coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
00708        ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
00709         (JDIMENSION) jround_up((long) compptr->width_in_blocks,
00710                             (long) compptr->h_samp_factor),
00711         (JDIMENSION) jround_up((long) compptr->height_in_blocks,
00712                             (long) compptr->v_samp_factor),
00713         (JDIMENSION) access_rows);
00714     }
00715     coef->pub.consume_data = consume_data;
00716     coef->pub.decompress_data = decompress_data;
00717     coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
00718 #else
00719     ERREXIT(cinfo, JERR_NOT_COMPILED);
00720 #endif
00721   } else {
00722     /* We only need a single-MCU buffer. */
00723     JBLOCKROW buffer;
00724     int i;
00725 
00726     buffer = (JBLOCKROW)
00727       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00728                               D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
00729     for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
00730       coef->MCU_buffer[i] = buffer + i;
00731     }
00732     coef->pub.consume_data = dummy_consume_data;
00733     coef->pub.decompress_data = decompress_onepass;
00734     coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
00735   }
00736 }