blob: 922f649a7449906e28b2f953c663826902ce89d8 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jdmainct.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
DRC36a6eec2010-10-08 08:05:44 +00007 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains the main buffer controller for decompression.
11 * The main buffer lies between the JPEG decompressor proper and the
12 * post-processor; it holds downsampled data in the JPEG colorspace.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000013 *
14 * Note that this code is bypassed in raw-data mode, since the application
15 * supplies the equivalent of the main buffer in that case.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016 */
17
18#define JPEG_INTERNALS
19#include "jinclude.h"
20#include "jpeglib.h"
DRC36a6eec2010-10-08 08:05:44 +000021#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022
23
24/*
25 * In the current system design, the main buffer need never be a full-image
26 * buffer; any full-height buffers will be found inside the coefficient or
27 * postprocessing controllers. Nonetheless, the main controller is not
28 * trivial. Its responsibility is to provide context rows for upsampling/
29 * rescaling, and doing this in an efficient fashion is a bit tricky.
30 *
31 * Postprocessor input data is counted in "row groups". A row group
32 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
33 * sample rows of each component. (We require DCT_scaled_size values to be
34 * chosen such that these numbers are integers. In practice DCT_scaled_size
35 * values will likely be powers of two, so we actually have the stronger
36 * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
37 * Upsampling will typically produce max_v_samp_factor pixel rows from each
38 * row group (times any additional scale factor that the upsampler is
39 * applying).
40 *
41 * The coefficient controller will deliver data to us one iMCU row at a time;
42 * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
43 * exactly min_DCT_scaled_size row groups. (This amount of data corresponds
44 * to one row of MCUs when the image is fully interleaved.) Note that the
45 * number of sample rows varies across components, but the number of row
46 * groups does not. Some garbage sample rows may be included in the last iMCU
47 * row at the bottom of the image.
48 *
49 * Depending on the vertical scaling algorithm used, the upsampler may need
50 * access to the sample row(s) above and below its current input row group.
51 * The upsampler is required to set need_context_rows TRUE at global selection
52 * time if so. When need_context_rows is FALSE, this controller can simply
53 * obtain one iMCU row at a time from the coefficient controller and dole it
54 * out as row groups to the postprocessor.
55 *
56 * When need_context_rows is TRUE, this controller guarantees that the buffer
57 * passed to postprocessing contains at least one row group's worth of samples
58 * above and below the row group(s) being processed. Note that the context
59 * rows "above" the first passed row group appear at negative row offsets in
60 * the passed buffer. At the top and bottom of the image, the required
61 * context rows are manufactured by duplicating the first or last real sample
62 * row; this avoids having special cases in the upsampling inner loops.
63 *
64 * The amount of context is fixed at one row group just because that's a
65 * convenient number for this controller to work with. The existing
66 * upsamplers really only need one sample row of context. An upsampler
67 * supporting arbitrary output rescaling might wish for more than one row
68 * group of context when shrinking the image; tough, we don't handle that.
69 * (This is justified by the assumption that downsizing will be handled mostly
70 * by adjusting the DCT_scaled_size values, so that the actual scale factor at
71 * the upsample step needn't be much less than one.)
72 *
73 * To provide the desired context, we have to retain the last two row groups
74 * of one iMCU row while reading in the next iMCU row. (The last row group
75 * can't be processed until we have another row group for its below-context,
76 * and so we have to save the next-to-last group too for its above-context.)
77 * We could do this most simply by copying data around in our buffer, but
78 * that'd be very slow. We can avoid copying any data by creating a rather
79 * strange pointer structure. Here's how it works. We allocate a workspace
80 * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
81 * of row groups per iMCU row). We create two sets of redundant pointers to
82 * the workspace. Labeling the physical row groups 0 to M+1, the synthesized
83 * pointer lists look like this:
84 * M+1 M-1
85 * master pointer --> 0 master pointer --> 0
86 * 1 1
87 * ... ...
88 * M-3 M-3
89 * M-2 M
90 * M-1 M+1
91 * M M-2
92 * M+1 M-1
93 * 0 0
94 * We read alternate iMCU rows using each master pointer; thus the last two
95 * row groups of the previous iMCU row remain un-overwritten in the workspace.
96 * The pointer lists are set up so that the required context rows appear to
97 * be adjacent to the proper places when we pass the pointer lists to the
98 * upsampler.
99 *
100 * The above pictures describe the normal state of the pointer lists.
101 * At top and bottom of the image, we diddle the pointer lists to duplicate
102 * the first or last sample row as necessary (this is cheaper than copying
103 * sample rows around).
104 *
105 * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that
106 * situation each iMCU row provides only one row group so the buffering logic
107 * must be different (eg, we must read two iMCU rows before we can emit the
108 * first row group). For now, we simply do not support providing context
109 * rows when min_DCT_scaled_size is 1. That combination seems unlikely to
110 * be worth providing --- if someone wants a 1/8th-size preview, they probably
111 * want it quick and dirty, so a context-free upsampler is sufficient.
112 */
113
114
115/* Private buffer controller object */
116
117typedef struct {
118 struct jpeg_d_main_controller pub; /* public fields */
119
120 /* Pointer to allocated workspace (M or M+2 row groups). */
121 JSAMPARRAY buffer[MAX_COMPONENTS];
122
DRCe5eaf372014-05-09 18:00:32 +0000123 boolean buffer_full; /* Have we gotten an iMCU row from decoder? */
124 JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000125
126 /* Remaining fields are only used in the context case. */
127
128 /* These are the master pointers to the funny-order pointer lists. */
DRCe5eaf372014-05-09 18:00:32 +0000129 JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000130
DRCe5eaf372014-05-09 18:00:32 +0000131 int whichptr; /* indicates which pointer set is now in use */
132 int context_state; /* process_data state machine status */
133 JDIMENSION rowgroups_avail; /* row groups available to postprocessor */
134 JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000135} my_main_controller;
136
137typedef my_main_controller * my_main_ptr;
138
139/* context_state values: */
DRCe5eaf372014-05-09 18:00:32 +0000140#define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */
141#define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */
142#define CTX_POSTPONED_ROW 2 /* feeding postponed row group */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000143
144
145/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +0000146METHODDEF(void) process_data_simple_main
DRCe5eaf372014-05-09 18:00:32 +0000147 JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
148 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
Thomas G. Lane489583f1996-02-07 00:00:00 +0000149METHODDEF(void) process_data_context_main
DRCe5eaf372014-05-09 18:00:32 +0000150 JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
151 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000152#ifdef QUANT_2PASS_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +0000153METHODDEF(void) process_data_crank_post
DRCe5eaf372014-05-09 18:00:32 +0000154 JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
155 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000156#endif
157
158
Thomas G. Lane489583f1996-02-07 00:00:00 +0000159LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000160alloc_funny_pointers (j_decompress_ptr cinfo)
161/* Allocate space for the funny pointer lists.
162 * This is done only once, not once per pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000163 */
164{
DRC7ed7b572012-02-07 22:51:49 +0000165 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000166 int ci, rgroup;
DRC49967cd2010-10-09 19:57:51 +0000167 int M = cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000168 jpeg_component_info *compptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000169 JSAMPARRAY xbuf;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000170
171 /* Get top-level space for component array pointers.
172 * We alloc both arrays with one call to save a few cycles.
173 */
DRC7ed7b572012-02-07 22:51:49 +0000174 main_ptr->xbuffer[0] = (JSAMPIMAGE)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000175 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000176 cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
DRC7ed7b572012-02-07 22:51:49 +0000177 main_ptr->xbuffer[1] = main_ptr->xbuffer[0] + cinfo->num_components;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000178
179 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
180 ci++, compptr++) {
DRC49967cd2010-10-09 19:57:51 +0000181 rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
182 cinfo->_min_DCT_scaled_size; /* height of a row group of component */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000183 /* Get space for pointer lists --- M+4 row groups in each list.
184 * We alloc both pointer lists with one call to save a few cycles.
185 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000186 xbuf = (JSAMPARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000187 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000188 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
189 xbuf += rgroup; /* want one row group at negative offsets */
DRC7ed7b572012-02-07 22:51:49 +0000190 main_ptr->xbuffer[0][ci] = xbuf;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000191 xbuf += rgroup * (M + 4);
DRC7ed7b572012-02-07 22:51:49 +0000192 main_ptr->xbuffer[1][ci] = xbuf;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000193 }
194}
195
196
Thomas G. Lane489583f1996-02-07 00:00:00 +0000197LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000198make_funny_pointers (j_decompress_ptr cinfo)
199/* Create the funny pointer lists discussed in the comments above.
DRC7ed7b572012-02-07 22:51:49 +0000200 * The actual workspace is already allocated (in main_ptr->buffer),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000201 * and the space for the pointer lists is allocated too.
202 * This routine just fills in the curiously ordered lists.
203 * This will be repeated at the beginning of each pass.
204 */
205{
DRC7ed7b572012-02-07 22:51:49 +0000206 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000207 int ci, i, rgroup;
DRC49967cd2010-10-09 19:57:51 +0000208 int M = cinfo->_min_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000209 jpeg_component_info *compptr;
210 JSAMPARRAY buf, xbuf0, xbuf1;
211
212 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
213 ci++, compptr++) {
DRC49967cd2010-10-09 19:57:51 +0000214 rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
215 cinfo->_min_DCT_scaled_size; /* height of a row group of component */
DRC7ed7b572012-02-07 22:51:49 +0000216 xbuf0 = main_ptr->xbuffer[0][ci];
217 xbuf1 = main_ptr->xbuffer[1][ci];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 /* First copy the workspace pointers as-is */
DRC7ed7b572012-02-07 22:51:49 +0000219 buf = main_ptr->buffer[ci];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 for (i = 0; i < rgroup * (M + 2); i++) {
221 xbuf0[i] = xbuf1[i] = buf[i];
222 }
223 /* In the second list, put the last four row groups in swapped order */
224 for (i = 0; i < rgroup * 2; i++) {
225 xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
226 xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
227 }
228 /* The wraparound pointers at top and bottom will be filled later
229 * (see set_wraparound_pointers, below). Initially we want the "above"
230 * pointers to duplicate the first actual data line. This only needs
231 * to happen in xbuffer[0].
232 */
233 for (i = 0; i < rgroup; i++) {
234 xbuf0[i - rgroup] = xbuf0[0];
235 }
236 }
237}
238
239
Thomas G. Lane489583f1996-02-07 00:00:00 +0000240LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000241set_wraparound_pointers (j_decompress_ptr cinfo)
242/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
243 * This changes the pointer list state from top-of-image to the normal state.
244 */
245{
DRC7ed7b572012-02-07 22:51:49 +0000246 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000247 int ci, i, rgroup;
DRC49967cd2010-10-09 19:57:51 +0000248 int M = cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000249 jpeg_component_info *compptr;
250 JSAMPARRAY xbuf0, xbuf1;
251
252 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
253 ci++, compptr++) {
DRC49967cd2010-10-09 19:57:51 +0000254 rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
255 cinfo->_min_DCT_scaled_size; /* height of a row group of component */
DRC7ed7b572012-02-07 22:51:49 +0000256 xbuf0 = main_ptr->xbuffer[0][ci];
257 xbuf1 = main_ptr->xbuffer[1][ci];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000258 for (i = 0; i < rgroup; i++) {
259 xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
260 xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
261 xbuf0[rgroup*(M+2) + i] = xbuf0[i];
262 xbuf1[rgroup*(M+2) + i] = xbuf1[i];
263 }
264 }
265}
266
267
Thomas G. Lane489583f1996-02-07 00:00:00 +0000268LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000269set_bottom_pointers (j_decompress_ptr cinfo)
270/* Change the pointer lists to duplicate the last sample row at the bottom
271 * of the image. whichptr indicates which xbuffer holds the final iMCU row.
272 * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
273 */
274{
DRC7ed7b572012-02-07 22:51:49 +0000275 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000276 int ci, i, rgroup, iMCUheight, rows_left;
277 jpeg_component_info *compptr;
278 JSAMPARRAY xbuf;
279
280 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
281 ci++, compptr++) {
282 /* Count sample rows in one iMCU row and in one row group */
DRC49967cd2010-10-09 19:57:51 +0000283 iMCUheight = compptr->v_samp_factor * compptr->_DCT_scaled_size;
284 rgroup = iMCUheight / cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000285 /* Count nondummy sample rows remaining for this component */
286 rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
287 if (rows_left == 0) rows_left = iMCUheight;
288 /* Count nondummy row groups. Should get same answer for each component,
289 * so we need only do it once.
290 */
291 if (ci == 0) {
DRC7ed7b572012-02-07 22:51:49 +0000292 main_ptr->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000293 }
294 /* Duplicate the last real sample row rgroup*2 times; this pads out the
295 * last partial rowgroup and ensures at least one full rowgroup of context.
296 */
DRC7ed7b572012-02-07 22:51:49 +0000297 xbuf = main_ptr->xbuffer[main_ptr->whichptr][ci];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000298 for (i = 0; i < rgroup * 2; i++) {
299 xbuf[rows_left + i] = xbuf[rows_left-1];
300 }
301 }
302}
303
304
305/*
306 * Initialize for a processing pass.
307 */
308
Thomas G. Lane489583f1996-02-07 00:00:00 +0000309METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000310start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
311{
DRC7ed7b572012-02-07 22:51:49 +0000312 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000313
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000314 switch (pass_mode) {
315 case JBUF_PASS_THRU:
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000316 if (cinfo->upsample->need_context_rows) {
DRC7ed7b572012-02-07 22:51:49 +0000317 main_ptr->pub.process_data = process_data_context_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000318 make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
DRCe5eaf372014-05-09 18:00:32 +0000319 main_ptr->whichptr = 0; /* Read first iMCU row into xbuffer[0] */
DRC7ed7b572012-02-07 22:51:49 +0000320 main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
321 main_ptr->iMCU_row_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000322 } else {
323 /* Simple case with no context needed */
DRC7ed7b572012-02-07 22:51:49 +0000324 main_ptr->pub.process_data = process_data_simple_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000325 }
DRCe5eaf372014-05-09 18:00:32 +0000326 main_ptr->buffer_full = FALSE; /* Mark buffer empty */
DRC7ed7b572012-02-07 22:51:49 +0000327 main_ptr->rowgroup_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000328 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000329#ifdef QUANT_2PASS_SUPPORTED
330 case JBUF_CRANK_DEST:
331 /* For last pass of 2-pass quantization, just crank the postprocessor */
DRC7ed7b572012-02-07 22:51:49 +0000332 main_ptr->pub.process_data = process_data_crank_post;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000333 break;
334#endif
335 default:
336 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
337 break;
338 }
339}
340
341
342/*
343 * Process some data.
344 * This handles the simple case where no context is required.
345 */
346
Thomas G. Lane489583f1996-02-07 00:00:00 +0000347METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000348process_data_simple_main (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000349 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
350 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000351{
DRC7ed7b572012-02-07 22:51:49 +0000352 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000353 JDIMENSION rowgroups_avail;
354
355 /* Read input data if we haven't filled the main buffer yet */
DRC7ed7b572012-02-07 22:51:49 +0000356 if (! main_ptr->buffer_full) {
357 if (! (*cinfo->coef->decompress_data) (cinfo, main_ptr->buffer))
DRCe5eaf372014-05-09 18:00:32 +0000358 return; /* suspension forced, can do nothing more */
359 main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000360 }
361
362 /* There are always min_DCT_scaled_size row groups in an iMCU row. */
DRC49967cd2010-10-09 19:57:51 +0000363 rowgroups_avail = (JDIMENSION) cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000364 /* Note: at the bottom of the image, we may pass extra garbage row groups
365 * to the postprocessor. The postprocessor has to check for bottom
366 * of image anyway (at row resolution), so no point in us doing it too.
367 */
368
369 /* Feed the postprocessor */
DRC7ed7b572012-02-07 22:51:49 +0000370 (*cinfo->post->post_process_data) (cinfo, main_ptr->buffer,
DRCe5eaf372014-05-09 18:00:32 +0000371 &main_ptr->rowgroup_ctr, rowgroups_avail,
372 output_buf, out_row_ctr, out_rows_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373
374 /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
DRC7ed7b572012-02-07 22:51:49 +0000375 if (main_ptr->rowgroup_ctr >= rowgroups_avail) {
376 main_ptr->buffer_full = FALSE;
377 main_ptr->rowgroup_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000378 }
379}
380
381
382/*
383 * Process some data.
384 * This handles the case where context rows must be provided.
385 */
386
Thomas G. Lane489583f1996-02-07 00:00:00 +0000387METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000388process_data_context_main (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000389 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
390 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000391{
DRC7ed7b572012-02-07 22:51:49 +0000392 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000393
394 /* Read input data if we haven't filled the main buffer yet */
DRC7ed7b572012-02-07 22:51:49 +0000395 if (! main_ptr->buffer_full) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000396 if (! (*cinfo->coef->decompress_data) (cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000397 main_ptr->xbuffer[main_ptr->whichptr]))
398 return; /* suspension forced, can do nothing more */
399 main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
400 main_ptr->iMCU_row_ctr++; /* count rows received */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000401 }
402
403 /* Postprocessor typically will not swallow all the input data it is handed
404 * in one call (due to filling the output buffer first). Must be prepared
405 * to exit and restart. This switch lets us keep track of how far we got.
406 * Note that each case falls through to the next on successful completion.
407 */
DRC7ed7b572012-02-07 22:51:49 +0000408 switch (main_ptr->context_state) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000409 case CTX_POSTPONED_ROW:
410 /* Call postprocessor using previously set pointers for postponed row */
DRC7ed7b572012-02-07 22:51:49 +0000411 (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr],
DRCe5eaf372014-05-09 18:00:32 +0000412 &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail,
413 output_buf, out_row_ctr, out_rows_avail);
DRC7ed7b572012-02-07 22:51:49 +0000414 if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail)
DRCe5eaf372014-05-09 18:00:32 +0000415 return; /* Need to suspend */
DRC7ed7b572012-02-07 22:51:49 +0000416 main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000417 if (*out_row_ctr >= out_rows_avail)
DRCe5eaf372014-05-09 18:00:32 +0000418 return; /* Postprocessor exactly filled output buf */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000419 /*FALLTHROUGH*/
420 case CTX_PREPARE_FOR_IMCU:
421 /* Prepare to process first M-1 row groups of this iMCU row */
DRC7ed7b572012-02-07 22:51:49 +0000422 main_ptr->rowgroup_ctr = 0;
423 main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size - 1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000424 /* Check for bottom of image: if so, tweak pointers to "duplicate"
425 * the last sample row, and adjust rowgroups_avail to ignore padding rows.
426 */
DRC7ed7b572012-02-07 22:51:49 +0000427 if (main_ptr->iMCU_row_ctr == cinfo->total_iMCU_rows)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000428 set_bottom_pointers(cinfo);
DRC7ed7b572012-02-07 22:51:49 +0000429 main_ptr->context_state = CTX_PROCESS_IMCU;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000430 /*FALLTHROUGH*/
431 case CTX_PROCESS_IMCU:
432 /* Call postprocessor using previously set pointers */
DRC7ed7b572012-02-07 22:51:49 +0000433 (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr],
DRCe5eaf372014-05-09 18:00:32 +0000434 &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail,
435 output_buf, out_row_ctr, out_rows_avail);
DRC7ed7b572012-02-07 22:51:49 +0000436 if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail)
DRCe5eaf372014-05-09 18:00:32 +0000437 return; /* Need to suspend */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000438 /* After the first iMCU, change wraparound pointers to normal state */
DRC7ed7b572012-02-07 22:51:49 +0000439 if (main_ptr->iMCU_row_ctr == 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000440 set_wraparound_pointers(cinfo);
441 /* Prepare to load new iMCU row using other xbuffer list */
DRCe5eaf372014-05-09 18:00:32 +0000442 main_ptr->whichptr ^= 1; /* 0=>1 or 1=>0 */
DRC7ed7b572012-02-07 22:51:49 +0000443 main_ptr->buffer_full = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000444 /* Still need to process last row group of this iMCU row, */
445 /* which is saved at index M+1 of the other xbuffer */
DRC7ed7b572012-02-07 22:51:49 +0000446 main_ptr->rowgroup_ctr = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 1);
447 main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 2);
448 main_ptr->context_state = CTX_POSTPONED_ROW;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000449 }
450}
451
452
453/*
454 * Process some data.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000455 * Final pass of two-pass quantization: just call the postprocessor.
456 * Source data will be the postprocessor controller's internal buffer.
457 */
458
459#ifdef QUANT_2PASS_SUPPORTED
460
Thomas G. Lane489583f1996-02-07 00:00:00 +0000461METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000462process_data_crank_post (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000463 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
464 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000465{
466 (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
DRCe5eaf372014-05-09 18:00:32 +0000467 (JDIMENSION *) NULL, (JDIMENSION) 0,
468 output_buf, out_row_ctr, out_rows_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000469}
470
471#endif /* QUANT_2PASS_SUPPORTED */
472
473
474/*
475 * Initialize main buffer controller.
476 */
477
Thomas G. Lane489583f1996-02-07 00:00:00 +0000478GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000479jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
480{
DRC7ed7b572012-02-07 22:51:49 +0000481 my_main_ptr main_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000482 int ci, rgroup, ngroups;
483 jpeg_component_info *compptr;
484
DRC7ed7b572012-02-07 22:51:49 +0000485 main_ptr = (my_main_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000486 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000487 SIZEOF(my_main_controller));
DRC7ed7b572012-02-07 22:51:49 +0000488 cinfo->main = (struct jpeg_d_main_controller *) main_ptr;
489 main_ptr->pub.start_pass = start_pass_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000490
DRCe5eaf372014-05-09 18:00:32 +0000491 if (need_full_buffer) /* shouldn't happen */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000492 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
493
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000494 /* Allocate the workspace.
495 * ngroups is the number of row groups we need.
496 */
497 if (cinfo->upsample->need_context_rows) {
DRC49967cd2010-10-09 19:57:51 +0000498 if (cinfo->_min_DCT_scaled_size < 2) /* unsupported, see comments above */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000499 ERREXIT(cinfo, JERR_NOTIMPL);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000500 alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
DRC49967cd2010-10-09 19:57:51 +0000501 ngroups = cinfo->_min_DCT_scaled_size + 2;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000502 } else {
DRC49967cd2010-10-09 19:57:51 +0000503 ngroups = cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000504 }
505
506 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
507 ci++, compptr++) {
DRC49967cd2010-10-09 19:57:51 +0000508 rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
509 cinfo->_min_DCT_scaled_size; /* height of a row group of component */
DRC7ed7b572012-02-07 22:51:49 +0000510 main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000511 ((j_common_ptr) cinfo, JPOOL_IMAGE,
512 compptr->width_in_blocks * compptr->_DCT_scaled_size,
513 (JDIMENSION) (rgroup * ngroups));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000514 }
515}