1/*
2 * jdmainct.c
3 *
4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1994-1996, Thomas G. Lane.
6 * libjpeg-turbo Modifications:
7 * Copyright (C) 2010, D. R. Commander.
8 * 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.
13 *
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.
16 */
17
18#define JPEG_INTERNALS
19#include "jinclude.h"
20#include "jpeglib.h"
21#include "jpegcomp.h"
22
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
123  boolean buffer_full;		/* Have we gotten an iMCU row from decoder? */
124  JDIMENSION rowgroup_ctr;	/* counts row groups output to postprocessor */
125
126  /* Remaining fields are only used in the context case. */
127
128  /* These are the master pointers to the funny-order pointer lists. */
129  JSAMPIMAGE xbuffer[2];	/* pointers to weird pointer lists */
130
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;
136
137typedef my_main_controller * my_main_ptr;
138
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 */
143
144
145/* Forward declarations */
146METHODDEF(void) process_data_simple_main
147	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
148	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
149METHODDEF(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
153METHODDEF(void) process_data_crank_post
154	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
155	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
156#endif
157
158
159LOCAL(void)
160alloc_funny_pointers (j_decompress_ptr cinfo)
161/* Allocate space for the funny pointer lists.
162 * This is done only once, not once per pass.
163 */
164{
165  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
166  int ci, rgroup;
167  int M = cinfo->_min_DCT_scaled_size;
168  jpeg_component_info *compptr;
169  JSAMPARRAY xbuf;
170
171  /* Get top-level space for component array pointers.
172   * We alloc both arrays with one call to save a few cycles.
173   */
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;
178
179  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
180       ci++, compptr++) {
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.
185     */
186    xbuf = (JSAMPARRAY)
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;
193  }
194}
195
196
197LOCAL(void)
198make_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.
204 */
205{
206  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
207  int ci, i, rgroup;
208  int M = cinfo->_min_DCT_scaled_size;
209  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++) {
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];
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
240LOCAL(void)
241set_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{
246  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
247  int ci, i, rgroup;
248  int M = cinfo->_min_DCT_scaled_size;
249  jpeg_component_info *compptr;
250  JSAMPARRAY xbuf0, xbuf1;
251
252  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
253       ci++, compptr++) {
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];
263    }
264  }
265}
266
267
268LOCAL(void)
269set_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{
275  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
276  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 */
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.
290     */
291    if (ci == 0) {
292      main_ptr->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
293    }
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     */
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];
300    }
301  }
302}
303
304
305/*
306 * Initialize for a processing pass.
307 */
308
309METHODDEF(void)
310start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
311{
312  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
313
314  switch (pass_mode) {
315  case JBUF_PASS_THRU:
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;
322    } else {
323      /* Simple case with no context needed */
324      main_ptr->pub.process_data = process_data_simple_main;
325    }
326    main_ptr->buffer_full = FALSE;	/* Mark buffer empty */
327    main_ptr->rowgroup_ctr = 0;
328    break;
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;
333    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
347METHODDEF(void)
348process_data_simple_main (j_decompress_ptr cinfo,
349			  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
350			  JDIMENSION out_rows_avail)
351{
352  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
353  JDIMENSION rowgroups_avail;
354
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 */
360  }
361
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.
367   */
368
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);
373
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;
378  }
379}
380
381
382/*
383 * Process some data.
384 * This handles the case where context rows must be provided.
385 */
386
387METHODDEF(void)
388process_data_context_main (j_decompress_ptr cinfo,
389			   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
390			   JDIMENSION out_rows_avail)
391{
392  my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
393
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 */
401  }
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   */
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 */
419    /*FALLTHROUGH*/
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.
426     */
427    if (main_ptr->iMCU_row_ctr == cinfo->total_iMCU_rows)
428      set_bottom_pointers(cinfo);
429    main_ptr->context_state = CTX_PROCESS_IMCU;
430    /*FALLTHROUGH*/
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;
449  }
450}
451
452
453/*
454 * Process some data.
455 * 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
461METHODDEF(void)
462process_data_crank_post (j_decompress_ptr cinfo,
463			 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
464			 JDIMENSION out_rows_avail)
465{
466  (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
467				     (JDIMENSION *) NULL, (JDIMENSION) 0,
468				     output_buf, out_row_ctr, out_rows_avail);
469}
470
471#endif /* QUANT_2PASS_SUPPORTED */
472
473
474/*
475 * Initialize main buffer controller.
476 */
477
478GLOBAL(void)
479jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
480{
481  my_main_ptr main_ptr;
482  int ci, rgroup, ngroups;
483  jpeg_component_info *compptr;
484
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;
490
491  if (need_full_buffer)		/* shouldn't happen */
492    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
493
494  /* Allocate the workspace.
495   * ngroups is the number of row groups we need.
496   */
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;
502  } else {
503    ngroups = cinfo->_min_DCT_scaled_size;
504  }
505
506  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
507       ci++, compptr++) {
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));
514  }
515}
516