Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * jdmainct.c |
michael@0 | 3 | * |
michael@0 | 4 | * This file was part of the Independent JPEG Group's software: |
michael@0 | 5 | * Copyright (C) 1994-1996, Thomas G. Lane. |
michael@0 | 6 | * libjpeg-turbo Modifications: |
michael@0 | 7 | * Copyright (C) 2010, D. R. Commander. |
michael@0 | 8 | * For conditions of distribution and use, see the accompanying README file. |
michael@0 | 9 | * |
michael@0 | 10 | * This file contains the main buffer controller for decompression. |
michael@0 | 11 | * The main buffer lies between the JPEG decompressor proper and the |
michael@0 | 12 | * post-processor; it holds downsampled data in the JPEG colorspace. |
michael@0 | 13 | * |
michael@0 | 14 | * Note that this code is bypassed in raw-data mode, since the application |
michael@0 | 15 | * supplies the equivalent of the main buffer in that case. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #define JPEG_INTERNALS |
michael@0 | 19 | #include "jinclude.h" |
michael@0 | 20 | #include "jpeglib.h" |
michael@0 | 21 | #include "jpegcomp.h" |
michael@0 | 22 | |
michael@0 | 23 | |
michael@0 | 24 | /* |
michael@0 | 25 | * In the current system design, the main buffer need never be a full-image |
michael@0 | 26 | * buffer; any full-height buffers will be found inside the coefficient or |
michael@0 | 27 | * postprocessing controllers. Nonetheless, the main controller is not |
michael@0 | 28 | * trivial. Its responsibility is to provide context rows for upsampling/ |
michael@0 | 29 | * rescaling, and doing this in an efficient fashion is a bit tricky. |
michael@0 | 30 | * |
michael@0 | 31 | * Postprocessor input data is counted in "row groups". A row group |
michael@0 | 32 | * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) |
michael@0 | 33 | * sample rows of each component. (We require DCT_scaled_size values to be |
michael@0 | 34 | * chosen such that these numbers are integers. In practice DCT_scaled_size |
michael@0 | 35 | * values will likely be powers of two, so we actually have the stronger |
michael@0 | 36 | * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.) |
michael@0 | 37 | * Upsampling will typically produce max_v_samp_factor pixel rows from each |
michael@0 | 38 | * row group (times any additional scale factor that the upsampler is |
michael@0 | 39 | * applying). |
michael@0 | 40 | * |
michael@0 | 41 | * The coefficient controller will deliver data to us one iMCU row at a time; |
michael@0 | 42 | * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or |
michael@0 | 43 | * exactly min_DCT_scaled_size row groups. (This amount of data corresponds |
michael@0 | 44 | * to one row of MCUs when the image is fully interleaved.) Note that the |
michael@0 | 45 | * number of sample rows varies across components, but the number of row |
michael@0 | 46 | * groups does not. Some garbage sample rows may be included in the last iMCU |
michael@0 | 47 | * row at the bottom of the image. |
michael@0 | 48 | * |
michael@0 | 49 | * Depending on the vertical scaling algorithm used, the upsampler may need |
michael@0 | 50 | * access to the sample row(s) above and below its current input row group. |
michael@0 | 51 | * The upsampler is required to set need_context_rows TRUE at global selection |
michael@0 | 52 | * time if so. When need_context_rows is FALSE, this controller can simply |
michael@0 | 53 | * obtain one iMCU row at a time from the coefficient controller and dole it |
michael@0 | 54 | * out as row groups to the postprocessor. |
michael@0 | 55 | * |
michael@0 | 56 | * When need_context_rows is TRUE, this controller guarantees that the buffer |
michael@0 | 57 | * passed to postprocessing contains at least one row group's worth of samples |
michael@0 | 58 | * above and below the row group(s) being processed. Note that the context |
michael@0 | 59 | * rows "above" the first passed row group appear at negative row offsets in |
michael@0 | 60 | * the passed buffer. At the top and bottom of the image, the required |
michael@0 | 61 | * context rows are manufactured by duplicating the first or last real sample |
michael@0 | 62 | * row; this avoids having special cases in the upsampling inner loops. |
michael@0 | 63 | * |
michael@0 | 64 | * The amount of context is fixed at one row group just because that's a |
michael@0 | 65 | * convenient number for this controller to work with. The existing |
michael@0 | 66 | * upsamplers really only need one sample row of context. An upsampler |
michael@0 | 67 | * supporting arbitrary output rescaling might wish for more than one row |
michael@0 | 68 | * group of context when shrinking the image; tough, we don't handle that. |
michael@0 | 69 | * (This is justified by the assumption that downsizing will be handled mostly |
michael@0 | 70 | * by adjusting the DCT_scaled_size values, so that the actual scale factor at |
michael@0 | 71 | * the upsample step needn't be much less than one.) |
michael@0 | 72 | * |
michael@0 | 73 | * To provide the desired context, we have to retain the last two row groups |
michael@0 | 74 | * of one iMCU row while reading in the next iMCU row. (The last row group |
michael@0 | 75 | * can't be processed until we have another row group for its below-context, |
michael@0 | 76 | * and so we have to save the next-to-last group too for its above-context.) |
michael@0 | 77 | * We could do this most simply by copying data around in our buffer, but |
michael@0 | 78 | * that'd be very slow. We can avoid copying any data by creating a rather |
michael@0 | 79 | * strange pointer structure. Here's how it works. We allocate a workspace |
michael@0 | 80 | * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number |
michael@0 | 81 | * of row groups per iMCU row). We create two sets of redundant pointers to |
michael@0 | 82 | * the workspace. Labeling the physical row groups 0 to M+1, the synthesized |
michael@0 | 83 | * pointer lists look like this: |
michael@0 | 84 | * M+1 M-1 |
michael@0 | 85 | * master pointer --> 0 master pointer --> 0 |
michael@0 | 86 | * 1 1 |
michael@0 | 87 | * ... ... |
michael@0 | 88 | * M-3 M-3 |
michael@0 | 89 | * M-2 M |
michael@0 | 90 | * M-1 M+1 |
michael@0 | 91 | * M M-2 |
michael@0 | 92 | * M+1 M-1 |
michael@0 | 93 | * 0 0 |
michael@0 | 94 | * We read alternate iMCU rows using each master pointer; thus the last two |
michael@0 | 95 | * row groups of the previous iMCU row remain un-overwritten in the workspace. |
michael@0 | 96 | * The pointer lists are set up so that the required context rows appear to |
michael@0 | 97 | * be adjacent to the proper places when we pass the pointer lists to the |
michael@0 | 98 | * upsampler. |
michael@0 | 99 | * |
michael@0 | 100 | * The above pictures describe the normal state of the pointer lists. |
michael@0 | 101 | * At top and bottom of the image, we diddle the pointer lists to duplicate |
michael@0 | 102 | * the first or last sample row as necessary (this is cheaper than copying |
michael@0 | 103 | * sample rows around). |
michael@0 | 104 | * |
michael@0 | 105 | * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that |
michael@0 | 106 | * situation each iMCU row provides only one row group so the buffering logic |
michael@0 | 107 | * must be different (eg, we must read two iMCU rows before we can emit the |
michael@0 | 108 | * first row group). For now, we simply do not support providing context |
michael@0 | 109 | * rows when min_DCT_scaled_size is 1. That combination seems unlikely to |
michael@0 | 110 | * be worth providing --- if someone wants a 1/8th-size preview, they probably |
michael@0 | 111 | * want it quick and dirty, so a context-free upsampler is sufficient. |
michael@0 | 112 | */ |
michael@0 | 113 | |
michael@0 | 114 | |
michael@0 | 115 | /* Private buffer controller object */ |
michael@0 | 116 | |
michael@0 | 117 | typedef struct { |
michael@0 | 118 | struct jpeg_d_main_controller pub; /* public fields */ |
michael@0 | 119 | |
michael@0 | 120 | /* Pointer to allocated workspace (M or M+2 row groups). */ |
michael@0 | 121 | JSAMPARRAY buffer[MAX_COMPONENTS]; |
michael@0 | 122 | |
michael@0 | 123 | boolean buffer_full; /* Have we gotten an iMCU row from decoder? */ |
michael@0 | 124 | JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */ |
michael@0 | 125 | |
michael@0 | 126 | /* Remaining fields are only used in the context case. */ |
michael@0 | 127 | |
michael@0 | 128 | /* These are the master pointers to the funny-order pointer lists. */ |
michael@0 | 129 | JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */ |
michael@0 | 130 | |
michael@0 | 131 | int whichptr; /* indicates which pointer set is now in use */ |
michael@0 | 132 | int context_state; /* process_data state machine status */ |
michael@0 | 133 | JDIMENSION rowgroups_avail; /* row groups available to postprocessor */ |
michael@0 | 134 | JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */ |
michael@0 | 135 | } my_main_controller; |
michael@0 | 136 | |
michael@0 | 137 | typedef my_main_controller * my_main_ptr; |
michael@0 | 138 | |
michael@0 | 139 | /* context_state values: */ |
michael@0 | 140 | #define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */ |
michael@0 | 141 | #define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */ |
michael@0 | 142 | #define CTX_POSTPONED_ROW 2 /* feeding postponed row group */ |
michael@0 | 143 | |
michael@0 | 144 | |
michael@0 | 145 | /* Forward declarations */ |
michael@0 | 146 | METHODDEF(void) process_data_simple_main |
michael@0 | 147 | JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, |
michael@0 | 148 | JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); |
michael@0 | 149 | METHODDEF(void) process_data_context_main |
michael@0 | 150 | JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, |
michael@0 | 151 | JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); |
michael@0 | 152 | #ifdef QUANT_2PASS_SUPPORTED |
michael@0 | 153 | METHODDEF(void) process_data_crank_post |
michael@0 | 154 | JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, |
michael@0 | 155 | JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); |
michael@0 | 156 | #endif |
michael@0 | 157 | |
michael@0 | 158 | |
michael@0 | 159 | LOCAL(void) |
michael@0 | 160 | alloc_funny_pointers (j_decompress_ptr cinfo) |
michael@0 | 161 | /* Allocate space for the funny pointer lists. |
michael@0 | 162 | * This is done only once, not once per pass. |
michael@0 | 163 | */ |
michael@0 | 164 | { |
michael@0 | 165 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
michael@0 | 166 | int ci, rgroup; |
michael@0 | 167 | int M = cinfo->_min_DCT_scaled_size; |
michael@0 | 168 | jpeg_component_info *compptr; |
michael@0 | 169 | JSAMPARRAY xbuf; |
michael@0 | 170 | |
michael@0 | 171 | /* Get top-level space for component array pointers. |
michael@0 | 172 | * We alloc both arrays with one call to save a few cycles. |
michael@0 | 173 | */ |
michael@0 | 174 | main_ptr->xbuffer[0] = (JSAMPIMAGE) |
michael@0 | 175 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
michael@0 | 176 | cinfo->num_components * 2 * SIZEOF(JSAMPARRAY)); |
michael@0 | 177 | main_ptr->xbuffer[1] = main_ptr->xbuffer[0] + cinfo->num_components; |
michael@0 | 178 | |
michael@0 | 179 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
michael@0 | 180 | ci++, compptr++) { |
michael@0 | 181 | rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / |
michael@0 | 182 | cinfo->_min_DCT_scaled_size; /* height of a row group of component */ |
michael@0 | 183 | /* Get space for pointer lists --- M+4 row groups in each list. |
michael@0 | 184 | * We alloc both pointer lists with one call to save a few cycles. |
michael@0 | 185 | */ |
michael@0 | 186 | xbuf = (JSAMPARRAY) |
michael@0 | 187 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
michael@0 | 188 | 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW)); |
michael@0 | 189 | xbuf += rgroup; /* want one row group at negative offsets */ |
michael@0 | 190 | main_ptr->xbuffer[0][ci] = xbuf; |
michael@0 | 191 | xbuf += rgroup * (M + 4); |
michael@0 | 192 | main_ptr->xbuffer[1][ci] = xbuf; |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | |
michael@0 | 197 | LOCAL(void) |
michael@0 | 198 | make_funny_pointers (j_decompress_ptr cinfo) |
michael@0 | 199 | /* Create the funny pointer lists discussed in the comments above. |
michael@0 | 200 | * The actual workspace is already allocated (in main_ptr->buffer), |
michael@0 | 201 | * and the space for the pointer lists is allocated too. |
michael@0 | 202 | * This routine just fills in the curiously ordered lists. |
michael@0 | 203 | * This will be repeated at the beginning of each pass. |
michael@0 | 204 | */ |
michael@0 | 205 | { |
michael@0 | 206 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
michael@0 | 207 | int ci, i, rgroup; |
michael@0 | 208 | int M = cinfo->_min_DCT_scaled_size; |
michael@0 | 209 | jpeg_component_info *compptr; |
michael@0 | 210 | JSAMPARRAY buf, xbuf0, xbuf1; |
michael@0 | 211 | |
michael@0 | 212 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
michael@0 | 213 | ci++, compptr++) { |
michael@0 | 214 | rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / |
michael@0 | 215 | cinfo->_min_DCT_scaled_size; /* height of a row group of component */ |
michael@0 | 216 | xbuf0 = main_ptr->xbuffer[0][ci]; |
michael@0 | 217 | xbuf1 = main_ptr->xbuffer[1][ci]; |
michael@0 | 218 | /* First copy the workspace pointers as-is */ |
michael@0 | 219 | buf = main_ptr->buffer[ci]; |
michael@0 | 220 | for (i = 0; i < rgroup * (M + 2); i++) { |
michael@0 | 221 | xbuf0[i] = xbuf1[i] = buf[i]; |
michael@0 | 222 | } |
michael@0 | 223 | /* In the second list, put the last four row groups in swapped order */ |
michael@0 | 224 | for (i = 0; i < rgroup * 2; i++) { |
michael@0 | 225 | xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i]; |
michael@0 | 226 | xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i]; |
michael@0 | 227 | } |
michael@0 | 228 | /* The wraparound pointers at top and bottom will be filled later |
michael@0 | 229 | * (see set_wraparound_pointers, below). Initially we want the "above" |
michael@0 | 230 | * pointers to duplicate the first actual data line. This only needs |
michael@0 | 231 | * to happen in xbuffer[0]. |
michael@0 | 232 | */ |
michael@0 | 233 | for (i = 0; i < rgroup; i++) { |
michael@0 | 234 | xbuf0[i - rgroup] = xbuf0[0]; |
michael@0 | 235 | } |
michael@0 | 236 | } |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | |
michael@0 | 240 | LOCAL(void) |
michael@0 | 241 | set_wraparound_pointers (j_decompress_ptr cinfo) |
michael@0 | 242 | /* Set up the "wraparound" pointers at top and bottom of the pointer lists. |
michael@0 | 243 | * This changes the pointer list state from top-of-image to the normal state. |
michael@0 | 244 | */ |
michael@0 | 245 | { |
michael@0 | 246 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
michael@0 | 247 | int ci, i, rgroup; |
michael@0 | 248 | int M = cinfo->_min_DCT_scaled_size; |
michael@0 | 249 | jpeg_component_info *compptr; |
michael@0 | 250 | JSAMPARRAY xbuf0, xbuf1; |
michael@0 | 251 | |
michael@0 | 252 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
michael@0 | 253 | ci++, compptr++) { |
michael@0 | 254 | rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / |
michael@0 | 255 | cinfo->_min_DCT_scaled_size; /* height of a row group of component */ |
michael@0 | 256 | xbuf0 = main_ptr->xbuffer[0][ci]; |
michael@0 | 257 | xbuf1 = main_ptr->xbuffer[1][ci]; |
michael@0 | 258 | for (i = 0; i < rgroup; i++) { |
michael@0 | 259 | xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i]; |
michael@0 | 260 | xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i]; |
michael@0 | 261 | xbuf0[rgroup*(M+2) + i] = xbuf0[i]; |
michael@0 | 262 | xbuf1[rgroup*(M+2) + i] = xbuf1[i]; |
michael@0 | 263 | } |
michael@0 | 264 | } |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | |
michael@0 | 268 | LOCAL(void) |
michael@0 | 269 | set_bottom_pointers (j_decompress_ptr cinfo) |
michael@0 | 270 | /* Change the pointer lists to duplicate the last sample row at the bottom |
michael@0 | 271 | * of the image. whichptr indicates which xbuffer holds the final iMCU row. |
michael@0 | 272 | * Also sets rowgroups_avail to indicate number of nondummy row groups in row. |
michael@0 | 273 | */ |
michael@0 | 274 | { |
michael@0 | 275 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
michael@0 | 276 | int ci, i, rgroup, iMCUheight, rows_left; |
michael@0 | 277 | jpeg_component_info *compptr; |
michael@0 | 278 | JSAMPARRAY xbuf; |
michael@0 | 279 | |
michael@0 | 280 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
michael@0 | 281 | ci++, compptr++) { |
michael@0 | 282 | /* Count sample rows in one iMCU row and in one row group */ |
michael@0 | 283 | iMCUheight = compptr->v_samp_factor * compptr->_DCT_scaled_size; |
michael@0 | 284 | rgroup = iMCUheight / cinfo->_min_DCT_scaled_size; |
michael@0 | 285 | /* Count nondummy sample rows remaining for this component */ |
michael@0 | 286 | rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight); |
michael@0 | 287 | if (rows_left == 0) rows_left = iMCUheight; |
michael@0 | 288 | /* Count nondummy row groups. Should get same answer for each component, |
michael@0 | 289 | * so we need only do it once. |
michael@0 | 290 | */ |
michael@0 | 291 | if (ci == 0) { |
michael@0 | 292 | main_ptr->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1); |
michael@0 | 293 | } |
michael@0 | 294 | /* Duplicate the last real sample row rgroup*2 times; this pads out the |
michael@0 | 295 | * last partial rowgroup and ensures at least one full rowgroup of context. |
michael@0 | 296 | */ |
michael@0 | 297 | xbuf = main_ptr->xbuffer[main_ptr->whichptr][ci]; |
michael@0 | 298 | for (i = 0; i < rgroup * 2; i++) { |
michael@0 | 299 | xbuf[rows_left + i] = xbuf[rows_left-1]; |
michael@0 | 300 | } |
michael@0 | 301 | } |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | |
michael@0 | 305 | /* |
michael@0 | 306 | * Initialize for a processing pass. |
michael@0 | 307 | */ |
michael@0 | 308 | |
michael@0 | 309 | METHODDEF(void) |
michael@0 | 310 | start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode) |
michael@0 | 311 | { |
michael@0 | 312 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
michael@0 | 313 | |
michael@0 | 314 | switch (pass_mode) { |
michael@0 | 315 | case JBUF_PASS_THRU: |
michael@0 | 316 | if (cinfo->upsample->need_context_rows) { |
michael@0 | 317 | main_ptr->pub.process_data = process_data_context_main; |
michael@0 | 318 | make_funny_pointers(cinfo); /* Create the xbuffer[] lists */ |
michael@0 | 319 | main_ptr->whichptr = 0; /* Read first iMCU row into xbuffer[0] */ |
michael@0 | 320 | main_ptr->context_state = CTX_PREPARE_FOR_IMCU; |
michael@0 | 321 | main_ptr->iMCU_row_ctr = 0; |
michael@0 | 322 | } else { |
michael@0 | 323 | /* Simple case with no context needed */ |
michael@0 | 324 | main_ptr->pub.process_data = process_data_simple_main; |
michael@0 | 325 | } |
michael@0 | 326 | main_ptr->buffer_full = FALSE; /* Mark buffer empty */ |
michael@0 | 327 | main_ptr->rowgroup_ctr = 0; |
michael@0 | 328 | break; |
michael@0 | 329 | #ifdef QUANT_2PASS_SUPPORTED |
michael@0 | 330 | case JBUF_CRANK_DEST: |
michael@0 | 331 | /* For last pass of 2-pass quantization, just crank the postprocessor */ |
michael@0 | 332 | main_ptr->pub.process_data = process_data_crank_post; |
michael@0 | 333 | break; |
michael@0 | 334 | #endif |
michael@0 | 335 | default: |
michael@0 | 336 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
michael@0 | 337 | break; |
michael@0 | 338 | } |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | |
michael@0 | 342 | /* |
michael@0 | 343 | * Process some data. |
michael@0 | 344 | * This handles the simple case where no context is required. |
michael@0 | 345 | */ |
michael@0 | 346 | |
michael@0 | 347 | METHODDEF(void) |
michael@0 | 348 | process_data_simple_main (j_decompress_ptr cinfo, |
michael@0 | 349 | JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
michael@0 | 350 | JDIMENSION out_rows_avail) |
michael@0 | 351 | { |
michael@0 | 352 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
michael@0 | 353 | JDIMENSION rowgroups_avail; |
michael@0 | 354 | |
michael@0 | 355 | /* Read input data if we haven't filled the main buffer yet */ |
michael@0 | 356 | if (! main_ptr->buffer_full) { |
michael@0 | 357 | if (! (*cinfo->coef->decompress_data) (cinfo, main_ptr->buffer)) |
michael@0 | 358 | return; /* suspension forced, can do nothing more */ |
michael@0 | 359 | main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */ |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | /* There are always min_DCT_scaled_size row groups in an iMCU row. */ |
michael@0 | 363 | rowgroups_avail = (JDIMENSION) cinfo->_min_DCT_scaled_size; |
michael@0 | 364 | /* Note: at the bottom of the image, we may pass extra garbage row groups |
michael@0 | 365 | * to the postprocessor. The postprocessor has to check for bottom |
michael@0 | 366 | * of image anyway (at row resolution), so no point in us doing it too. |
michael@0 | 367 | */ |
michael@0 | 368 | |
michael@0 | 369 | /* Feed the postprocessor */ |
michael@0 | 370 | (*cinfo->post->post_process_data) (cinfo, main_ptr->buffer, |
michael@0 | 371 | &main_ptr->rowgroup_ctr, rowgroups_avail, |
michael@0 | 372 | output_buf, out_row_ctr, out_rows_avail); |
michael@0 | 373 | |
michael@0 | 374 | /* Has postprocessor consumed all the data yet? If so, mark buffer empty */ |
michael@0 | 375 | if (main_ptr->rowgroup_ctr >= rowgroups_avail) { |
michael@0 | 376 | main_ptr->buffer_full = FALSE; |
michael@0 | 377 | main_ptr->rowgroup_ctr = 0; |
michael@0 | 378 | } |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | |
michael@0 | 382 | /* |
michael@0 | 383 | * Process some data. |
michael@0 | 384 | * This handles the case where context rows must be provided. |
michael@0 | 385 | */ |
michael@0 | 386 | |
michael@0 | 387 | METHODDEF(void) |
michael@0 | 388 | process_data_context_main (j_decompress_ptr cinfo, |
michael@0 | 389 | JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
michael@0 | 390 | JDIMENSION out_rows_avail) |
michael@0 | 391 | { |
michael@0 | 392 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
michael@0 | 393 | |
michael@0 | 394 | /* Read input data if we haven't filled the main buffer yet */ |
michael@0 | 395 | if (! main_ptr->buffer_full) { |
michael@0 | 396 | if (! (*cinfo->coef->decompress_data) (cinfo, |
michael@0 | 397 | main_ptr->xbuffer[main_ptr->whichptr])) |
michael@0 | 398 | return; /* suspension forced, can do nothing more */ |
michael@0 | 399 | main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */ |
michael@0 | 400 | main_ptr->iMCU_row_ctr++; /* count rows received */ |
michael@0 | 401 | } |
michael@0 | 402 | |
michael@0 | 403 | /* Postprocessor typically will not swallow all the input data it is handed |
michael@0 | 404 | * in one call (due to filling the output buffer first). Must be prepared |
michael@0 | 405 | * to exit and restart. This switch lets us keep track of how far we got. |
michael@0 | 406 | * Note that each case falls through to the next on successful completion. |
michael@0 | 407 | */ |
michael@0 | 408 | switch (main_ptr->context_state) { |
michael@0 | 409 | case CTX_POSTPONED_ROW: |
michael@0 | 410 | /* Call postprocessor using previously set pointers for postponed row */ |
michael@0 | 411 | (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr], |
michael@0 | 412 | &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail, |
michael@0 | 413 | output_buf, out_row_ctr, out_rows_avail); |
michael@0 | 414 | if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail) |
michael@0 | 415 | return; /* Need to suspend */ |
michael@0 | 416 | main_ptr->context_state = CTX_PREPARE_FOR_IMCU; |
michael@0 | 417 | if (*out_row_ctr >= out_rows_avail) |
michael@0 | 418 | return; /* Postprocessor exactly filled output buf */ |
michael@0 | 419 | /*FALLTHROUGH*/ |
michael@0 | 420 | case CTX_PREPARE_FOR_IMCU: |
michael@0 | 421 | /* Prepare to process first M-1 row groups of this iMCU row */ |
michael@0 | 422 | main_ptr->rowgroup_ctr = 0; |
michael@0 | 423 | main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size - 1); |
michael@0 | 424 | /* Check for bottom of image: if so, tweak pointers to "duplicate" |
michael@0 | 425 | * the last sample row, and adjust rowgroups_avail to ignore padding rows. |
michael@0 | 426 | */ |
michael@0 | 427 | if (main_ptr->iMCU_row_ctr == cinfo->total_iMCU_rows) |
michael@0 | 428 | set_bottom_pointers(cinfo); |
michael@0 | 429 | main_ptr->context_state = CTX_PROCESS_IMCU; |
michael@0 | 430 | /*FALLTHROUGH*/ |
michael@0 | 431 | case CTX_PROCESS_IMCU: |
michael@0 | 432 | /* Call postprocessor using previously set pointers */ |
michael@0 | 433 | (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr], |
michael@0 | 434 | &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail, |
michael@0 | 435 | output_buf, out_row_ctr, out_rows_avail); |
michael@0 | 436 | if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail) |
michael@0 | 437 | return; /* Need to suspend */ |
michael@0 | 438 | /* After the first iMCU, change wraparound pointers to normal state */ |
michael@0 | 439 | if (main_ptr->iMCU_row_ctr == 1) |
michael@0 | 440 | set_wraparound_pointers(cinfo); |
michael@0 | 441 | /* Prepare to load new iMCU row using other xbuffer list */ |
michael@0 | 442 | main_ptr->whichptr ^= 1; /* 0=>1 or 1=>0 */ |
michael@0 | 443 | main_ptr->buffer_full = FALSE; |
michael@0 | 444 | /* Still need to process last row group of this iMCU row, */ |
michael@0 | 445 | /* which is saved at index M+1 of the other xbuffer */ |
michael@0 | 446 | main_ptr->rowgroup_ctr = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 1); |
michael@0 | 447 | main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 2); |
michael@0 | 448 | main_ptr->context_state = CTX_POSTPONED_ROW; |
michael@0 | 449 | } |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | |
michael@0 | 453 | /* |
michael@0 | 454 | * Process some data. |
michael@0 | 455 | * Final pass of two-pass quantization: just call the postprocessor. |
michael@0 | 456 | * Source data will be the postprocessor controller's internal buffer. |
michael@0 | 457 | */ |
michael@0 | 458 | |
michael@0 | 459 | #ifdef QUANT_2PASS_SUPPORTED |
michael@0 | 460 | |
michael@0 | 461 | METHODDEF(void) |
michael@0 | 462 | process_data_crank_post (j_decompress_ptr cinfo, |
michael@0 | 463 | JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
michael@0 | 464 | JDIMENSION out_rows_avail) |
michael@0 | 465 | { |
michael@0 | 466 | (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL, |
michael@0 | 467 | (JDIMENSION *) NULL, (JDIMENSION) 0, |
michael@0 | 468 | output_buf, out_row_ctr, out_rows_avail); |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | #endif /* QUANT_2PASS_SUPPORTED */ |
michael@0 | 472 | |
michael@0 | 473 | |
michael@0 | 474 | /* |
michael@0 | 475 | * Initialize main buffer controller. |
michael@0 | 476 | */ |
michael@0 | 477 | |
michael@0 | 478 | GLOBAL(void) |
michael@0 | 479 | jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer) |
michael@0 | 480 | { |
michael@0 | 481 | my_main_ptr main_ptr; |
michael@0 | 482 | int ci, rgroup, ngroups; |
michael@0 | 483 | jpeg_component_info *compptr; |
michael@0 | 484 | |
michael@0 | 485 | main_ptr = (my_main_ptr) |
michael@0 | 486 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
michael@0 | 487 | SIZEOF(my_main_controller)); |
michael@0 | 488 | cinfo->main = (struct jpeg_d_main_controller *) main_ptr; |
michael@0 | 489 | main_ptr->pub.start_pass = start_pass_main; |
michael@0 | 490 | |
michael@0 | 491 | if (need_full_buffer) /* shouldn't happen */ |
michael@0 | 492 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
michael@0 | 493 | |
michael@0 | 494 | /* Allocate the workspace. |
michael@0 | 495 | * ngroups is the number of row groups we need. |
michael@0 | 496 | */ |
michael@0 | 497 | if (cinfo->upsample->need_context_rows) { |
michael@0 | 498 | if (cinfo->_min_DCT_scaled_size < 2) /* unsupported, see comments above */ |
michael@0 | 499 | ERREXIT(cinfo, JERR_NOTIMPL); |
michael@0 | 500 | alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */ |
michael@0 | 501 | ngroups = cinfo->_min_DCT_scaled_size + 2; |
michael@0 | 502 | } else { |
michael@0 | 503 | ngroups = cinfo->_min_DCT_scaled_size; |
michael@0 | 504 | } |
michael@0 | 505 | |
michael@0 | 506 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
michael@0 | 507 | ci++, compptr++) { |
michael@0 | 508 | rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / |
michael@0 | 509 | cinfo->_min_DCT_scaled_size; /* height of a row group of component */ |
michael@0 | 510 | main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray) |
michael@0 | 511 | ((j_common_ptr) cinfo, JPOOL_IMAGE, |
michael@0 | 512 | compptr->width_in_blocks * compptr->_DCT_scaled_size, |
michael@0 | 513 | (JDIMENSION) (rgroup * ngroups)); |
michael@0 | 514 | } |
michael@0 | 515 | } |