4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1994-1996, Thomas G. Lane.
7 * Copyright (C) 2010, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README file.
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.
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.
18 #define JPEG_INTERNALS
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.
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
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.
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.
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.
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.)
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:
85 * master pointer --> 0 master pointer --> 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
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).
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.
115 /* Private buffer controller object */
118 struct jpeg_d_main_controller pub
; /* public fields */
120 /* Pointer to allocated workspace (M or M+2 row groups). */
121 JSAMPARRAY buffer
[MAX_COMPONENTS
];
123 boolean buffer_full
; /* Have we gotten an iMCU row from decoder? */
124 JDIMENSION rowgroup_ctr
; /* counts row groups output to postprocessor */
126 /* Remaining fields are only used in the context case. */
128 /* These are the master pointers to the funny-order pointer lists. */
129 JSAMPIMAGE xbuffer
[2]; /* pointers to weird pointer lists */
131 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 */
135 } my_main_controller
;
137 typedef my_main_controller
* my_main_ptr
;
139 /* context_state values: */
140 #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 */
145 /* Forward declarations */
146 METHODDEF(void) process_data_simple_main
147 JPP((j_decompress_ptr cinfo
, JSAMPARRAY output_buf
,
148 JDIMENSION
*out_row_ctr
, JDIMENSION out_rows_avail
));
149 METHODDEF(void) process_data_context_main
150 JPP((j_decompress_ptr cinfo
, JSAMPARRAY output_buf
,
151 JDIMENSION
*out_row_ctr
, JDIMENSION out_rows_avail
));
152 #ifdef QUANT_2PASS_SUPPORTED
153 METHODDEF(void) process_data_crank_post
154 JPP((j_decompress_ptr cinfo
, JSAMPARRAY output_buf
,
155 JDIMENSION
*out_row_ctr
, JDIMENSION out_rows_avail
));
160 alloc_funny_pointers (j_decompress_ptr cinfo
)
161 /* Allocate space for the funny pointer lists.
162 * This is done only once, not once per pass.
165 my_main_ptr main_ptr
= (my_main_ptr
) cinfo
->main
;
167 int M
= cinfo
->_min_DCT_scaled_size
;
168 jpeg_component_info
*compptr
;
171 /* Get top-level space for component array pointers.
172 * We alloc both arrays with one call to save a few cycles.
174 main_ptr
->xbuffer
[0] = (JSAMPIMAGE
)
175 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
176 cinfo
->num_components
* 2 * SIZEOF(JSAMPARRAY
));
177 main_ptr
->xbuffer
[1] = main_ptr
->xbuffer
[0] + cinfo
->num_components
;
179 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
181 rgroup
= (compptr
->v_samp_factor
* compptr
->_DCT_scaled_size
) /
182 cinfo
->_min_DCT_scaled_size
; /* height of a row group of component */
183 /* 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.
187 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
188 2 * (rgroup
* (M
+ 4)) * SIZEOF(JSAMPROW
));
189 xbuf
+= rgroup
; /* want one row group at negative offsets */
190 main_ptr
->xbuffer
[0][ci
] = xbuf
;
191 xbuf
+= rgroup
* (M
+ 4);
192 main_ptr
->xbuffer
[1][ci
] = xbuf
;
198 make_funny_pointers (j_decompress_ptr cinfo
)
199 /* Create the funny pointer lists discussed in the comments above.
200 * The actual workspace is already allocated (in main_ptr->buffer),
201 * 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.
206 my_main_ptr main_ptr
= (my_main_ptr
) cinfo
->main
;
208 int M
= cinfo
->_min_DCT_scaled_size
;
209 jpeg_component_info
*compptr
;
210 JSAMPARRAY buf
, xbuf0
, xbuf1
;
212 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
214 rgroup
= (compptr
->v_samp_factor
* compptr
->_DCT_scaled_size
) /
215 cinfo
->_min_DCT_scaled_size
; /* height of a row group of component */
216 xbuf0
= main_ptr
->xbuffer
[0][ci
];
217 xbuf1
= main_ptr
->xbuffer
[1][ci
];
218 /* First copy the workspace pointers as-is */
219 buf
= main_ptr
->buffer
[ci
];
220 for (i
= 0; i
< rgroup
* (M
+ 2); i
++) {
221 xbuf0
[i
] = xbuf1
[i
] = buf
[i
];
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
];
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].
233 for (i
= 0; i
< rgroup
; i
++) {
234 xbuf0
[i
- rgroup
] = xbuf0
[0];
241 set_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.
246 my_main_ptr main_ptr
= (my_main_ptr
) cinfo
->main
;
248 int M
= cinfo
->_min_DCT_scaled_size
;
249 jpeg_component_info
*compptr
;
250 JSAMPARRAY xbuf0
, xbuf1
;
252 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
254 rgroup
= (compptr
->v_samp_factor
* compptr
->_DCT_scaled_size
) /
255 cinfo
->_min_DCT_scaled_size
; /* height of a row group of component */
256 xbuf0
= main_ptr
->xbuffer
[0][ci
];
257 xbuf1
= main_ptr
->xbuffer
[1][ci
];
258 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
];
269 set_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.
275 my_main_ptr main_ptr
= (my_main_ptr
) cinfo
->main
;
276 int ci
, i
, rgroup
, iMCUheight
, rows_left
;
277 jpeg_component_info
*compptr
;
280 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
282 /* Count sample rows in one iMCU row and in one row group */
283 iMCUheight
= compptr
->v_samp_factor
* compptr
->_DCT_scaled_size
;
284 rgroup
= iMCUheight
/ cinfo
->_min_DCT_scaled_size
;
285 /* 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.
292 main_ptr
->rowgroups_avail
= (JDIMENSION
) ((rows_left
-1) / rgroup
+ 1);
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.
297 xbuf
= main_ptr
->xbuffer
[main_ptr
->whichptr
][ci
];
298 for (i
= 0; i
< rgroup
* 2; i
++) {
299 xbuf
[rows_left
+ i
] = xbuf
[rows_left
-1];
306 * Initialize for a processing pass.
310 start_pass_main (j_decompress_ptr cinfo
, J_BUF_MODE pass_mode
)
312 my_main_ptr main_ptr
= (my_main_ptr
) cinfo
->main
;
316 if (cinfo
->upsample
->need_context_rows
) {
317 main_ptr
->pub
.process_data
= process_data_context_main
;
318 make_funny_pointers(cinfo
); /* Create the xbuffer[] lists */
319 main_ptr
->whichptr
= 0; /* Read first iMCU row into xbuffer[0] */
320 main_ptr
->context_state
= CTX_PREPARE_FOR_IMCU
;
321 main_ptr
->iMCU_row_ctr
= 0;
323 /* Simple case with no context needed */
324 main_ptr
->pub
.process_data
= process_data_simple_main
;
326 main_ptr
->buffer_full
= FALSE
; /* Mark buffer empty */
327 main_ptr
->rowgroup_ctr
= 0;
329 #ifdef QUANT_2PASS_SUPPORTED
330 case JBUF_CRANK_DEST
:
331 /* For last pass of 2-pass quantization, just crank the postprocessor */
332 main_ptr
->pub
.process_data
= process_data_crank_post
;
336 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
344 * This handles the simple case where no context is required.
348 process_data_simple_main (j_decompress_ptr cinfo
,
349 JSAMPARRAY output_buf
, JDIMENSION
*out_row_ctr
,
350 JDIMENSION out_rows_avail
)
352 my_main_ptr main_ptr
= (my_main_ptr
) cinfo
->main
;
353 JDIMENSION rowgroups_avail
;
355 /* Read input data if we haven't filled the main buffer yet */
356 if (! main_ptr
->buffer_full
) {
357 if (! (*cinfo
->coef
->decompress_data
) (cinfo
, main_ptr
->buffer
))
358 return; /* suspension forced, can do nothing more */
359 main_ptr
->buffer_full
= TRUE
; /* OK, we have an iMCU row to work with */
362 /* There are always min_DCT_scaled_size row groups in an iMCU row. */
363 rowgroups_avail
= (JDIMENSION
) cinfo
->_min_DCT_scaled_size
;
364 /* 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.
369 /* Feed the postprocessor */
370 (*cinfo
->post
->post_process_data
) (cinfo
, main_ptr
->buffer
,
371 &main_ptr
->rowgroup_ctr
, rowgroups_avail
,
372 output_buf
, out_row_ctr
, out_rows_avail
);
374 /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
375 if (main_ptr
->rowgroup_ctr
>= rowgroups_avail
) {
376 main_ptr
->buffer_full
= FALSE
;
377 main_ptr
->rowgroup_ctr
= 0;
384 * This handles the case where context rows must be provided.
388 process_data_context_main (j_decompress_ptr cinfo
,
389 JSAMPARRAY output_buf
, JDIMENSION
*out_row_ctr
,
390 JDIMENSION out_rows_avail
)
392 my_main_ptr main_ptr
= (my_main_ptr
) cinfo
->main
;
394 /* Read input data if we haven't filled the main buffer yet */
395 if (! main_ptr
->buffer_full
) {
396 if (! (*cinfo
->coef
->decompress_data
) (cinfo
,
397 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 */
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.
408 switch (main_ptr
->context_state
) {
409 case CTX_POSTPONED_ROW
:
410 /* Call postprocessor using previously set pointers for postponed row */
411 (*cinfo
->post
->post_process_data
) (cinfo
, main_ptr
->xbuffer
[main_ptr
->whichptr
],
412 &main_ptr
->rowgroup_ctr
, main_ptr
->rowgroups_avail
,
413 output_buf
, out_row_ctr
, out_rows_avail
);
414 if (main_ptr
->rowgroup_ctr
< main_ptr
->rowgroups_avail
)
415 return; /* Need to suspend */
416 main_ptr
->context_state
= CTX_PREPARE_FOR_IMCU
;
417 if (*out_row_ctr
>= out_rows_avail
)
418 return; /* Postprocessor exactly filled output buf */
420 case CTX_PREPARE_FOR_IMCU
:
421 /* Prepare to process first M-1 row groups of this iMCU row */
422 main_ptr
->rowgroup_ctr
= 0;
423 main_ptr
->rowgroups_avail
= (JDIMENSION
) (cinfo
->_min_DCT_scaled_size
- 1);
424 /* Check for bottom of image: if so, tweak pointers to "duplicate"
425 * the last sample row, and adjust rowgroups_avail to ignore padding rows.
427 if (main_ptr
->iMCU_row_ctr
== cinfo
->total_iMCU_rows
)
428 set_bottom_pointers(cinfo
);
429 main_ptr
->context_state
= CTX_PROCESS_IMCU
;
431 case CTX_PROCESS_IMCU
:
432 /* Call postprocessor using previously set pointers */
433 (*cinfo
->post
->post_process_data
) (cinfo
, main_ptr
->xbuffer
[main_ptr
->whichptr
],
434 &main_ptr
->rowgroup_ctr
, main_ptr
->rowgroups_avail
,
435 output_buf
, out_row_ctr
, out_rows_avail
);
436 if (main_ptr
->rowgroup_ctr
< main_ptr
->rowgroups_avail
)
437 return; /* Need to suspend */
438 /* After the first iMCU, change wraparound pointers to normal state */
439 if (main_ptr
->iMCU_row_ctr
== 1)
440 set_wraparound_pointers(cinfo
);
441 /* Prepare to load new iMCU row using other xbuffer list */
442 main_ptr
->whichptr
^= 1; /* 0=>1 or 1=>0 */
443 main_ptr
->buffer_full
= FALSE
;
444 /* Still need to process last row group of this iMCU row, */
445 /* which is saved at index M+1 of the other xbuffer */
446 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
;
455 * Final pass of two-pass quantization: just call the postprocessor.
456 * Source data will be the postprocessor controller's internal buffer.
459 #ifdef QUANT_2PASS_SUPPORTED
462 process_data_crank_post (j_decompress_ptr cinfo
,
463 JSAMPARRAY output_buf
, JDIMENSION
*out_row_ctr
,
464 JDIMENSION out_rows_avail
)
466 (*cinfo
->post
->post_process_data
) (cinfo
, (JSAMPIMAGE
) NULL
,
467 (JDIMENSION
*) NULL
, (JDIMENSION
) 0,
468 output_buf
, out_row_ctr
, out_rows_avail
);
471 #endif /* QUANT_2PASS_SUPPORTED */
475 * Initialize main buffer controller.
479 jinit_d_main_controller (j_decompress_ptr cinfo
, boolean need_full_buffer
)
481 my_main_ptr main_ptr
;
482 int ci
, rgroup
, ngroups
;
483 jpeg_component_info
*compptr
;
485 main_ptr
= (my_main_ptr
)
486 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
487 SIZEOF(my_main_controller
));
488 cinfo
->main
= (struct jpeg_d_main_controller
*) main_ptr
;
489 main_ptr
->pub
.start_pass
= start_pass_main
;
491 if (need_full_buffer
) /* shouldn't happen */
492 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
494 /* Allocate the workspace.
495 * ngroups is the number of row groups we need.
497 if (cinfo
->upsample
->need_context_rows
) {
498 if (cinfo
->_min_DCT_scaled_size
< 2) /* unsupported, see comments above */
499 ERREXIT(cinfo
, JERR_NOTIMPL
);
500 alloc_funny_pointers(cinfo
); /* Alloc space for xbuffer[] lists */
501 ngroups
= cinfo
->_min_DCT_scaled_size
+ 2;
503 ngroups
= cinfo
->_min_DCT_scaled_size
;
506 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
508 rgroup
= (compptr
->v_samp_factor
* compptr
->_DCT_scaled_size
) /
509 cinfo
->_min_DCT_scaled_size
; /* height of a row group of component */
510 main_ptr
->buffer
[ci
] = (*cinfo
->mem
->alloc_sarray
)
511 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
512 compptr
->width_in_blocks
* compptr
->_DCT_scaled_size
,
513 (JDIMENSION
) (rgroup
* ngroups
));