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