blob: 2ca1f37451d57d6c1afe72813213b35d4356e111 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jccoefct.c
3 *
4 * Copyright (C) 1994, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains the coefficient buffer controller for compression.
9 * This controller is the top level of the JPEG compressor proper.
10 * The coefficient buffer lies between forward-DCT and entropy encoding steps.
11 */
12
13#define JPEG_INTERNALS
14#include "jinclude.h"
15#include "jpeglib.h"
16
17
18/* We use a full-image coefficient buffer when doing Huffman optimization,
19 * and also for writing multiple-scan JPEG files. In all cases, the DCT
20 * step is run during the first pass, and subsequent passes need only read
21 * the buffered coefficients.
22 */
23#ifdef ENTROPY_OPT_SUPPORTED
24#define FULL_COEF_BUFFER_SUPPORTED
25#else
26#ifdef C_MULTISCAN_FILES_SUPPORTED
27#define FULL_COEF_BUFFER_SUPPORTED
28#endif
29#endif
30
31
32/* Private buffer controller object */
33
34typedef struct {
35 struct jpeg_c_coef_controller pub; /* public fields */
36
37 JDIMENSION MCU_row_num; /* keep track of MCU row # within image */
38
39 /* For single-pass compression, it's sufficient to buffer just one MCU
40 * (although this may prove a bit slow in practice). We allocate a
41 * workspace of MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
42 * MCU constructed and sent. (On 80x86, the workspace is FAR even though
43 * it's not really very big; this is to keep the module interfaces unchanged
44 * when a large coefficient buffer is necessary.)
45 * In multi-pass modes, this array points to the current MCU's blocks
46 * within the virtual arrays.
47 */
48 JBLOCKROW MCU_buffer[MAX_BLOCKS_IN_MCU];
49
50 /* In multi-pass modes, we need a virtual block array for each component. */
51 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
52} my_coef_controller;
53
54typedef my_coef_controller * my_coef_ptr;
55
56
57/* Forward declarations */
58METHODDEF void compress_data
59 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr));
60#ifdef FULL_COEF_BUFFER_SUPPORTED
61METHODDEF void compress_first_pass
62 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr));
63METHODDEF void compress_output
64 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr));
65#endif
66
67
68/*
69 * Initialize for a processing pass.
70 */
71
72METHODDEF void
73start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
74{
75 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
76
77 coef->MCU_row_num = 0;
78
79 switch (pass_mode) {
80 case JBUF_PASS_THRU:
81 if (coef->whole_image[0] != NULL)
82 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
83 coef->pub.compress_data = compress_data;
84 break;
85#ifdef FULL_COEF_BUFFER_SUPPORTED
86 case JBUF_SAVE_AND_PASS:
87 if (coef->whole_image[0] == NULL)
88 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
89 coef->pub.compress_data = compress_first_pass;
90 break;
91 case JBUF_CRANK_DEST:
92 if (coef->whole_image[0] == NULL)
93 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
94 coef->pub.compress_data = compress_output;
95 break;
96#endif
97 default:
98 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
99 break;
100 }
101}
102
103
104/*
105 * Process some data in the single-pass case.
106 * Up to one MCU row is processed (less if suspension is forced).
107 *
108 * NB: input_buf contains a plane for each component in image.
109 * For single pass, this is the same as the components in the scan.
110 */
111
112METHODDEF void
113compress_data (j_compress_ptr cinfo,
114 JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr)
115{
116 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
117 JDIMENSION MCU_col_num; /* index of current MCU within row */
118 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
119 JDIMENSION last_MCU_row = cinfo->MCU_rows_in_scan - 1;
120 int blkn, bi, ci, yindex, blockcnt;
121 JDIMENSION ypos, xpos;
122 jpeg_component_info *compptr;
123
124 /* Loop to write as much as one whole MCU row */
125
126 for (MCU_col_num = *in_mcu_ctr; MCU_col_num <= last_MCU_col; MCU_col_num++) {
127 /* Determine where data comes from in input_buf and do the DCT thing.
128 * Each call on forward_DCT processes a horizontal row of DCT blocks
129 * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
130 * sequentially. Dummy blocks at the right or bottom edge are filled in
131 * specially. The data in them does not matter for image reconstruction,
132 * so we fill them with values that will encode to the smallest amount of
133 * data, viz: all zeroes in the AC entries, DC entries equal to previous
134 * block's DC value. (Thanks to Thomas Kinsman for this idea.)
135 */
136 blkn = 0;
137 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
138 compptr = cinfo->cur_comp_info[ci];
139 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
140 : compptr->last_col_width;
141 xpos = MCU_col_num * compptr->MCU_sample_width;
142 ypos = 0;
143 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
144 if (coef->MCU_row_num < last_MCU_row ||
145 yindex < compptr->last_row_height) {
146 (*cinfo->fdct->forward_DCT) (cinfo, compptr,
147 input_buf[ci], coef->MCU_buffer[blkn],
148 ypos, xpos, (JDIMENSION) blockcnt);
149 if (blockcnt < compptr->MCU_width) {
150 /* Create some dummy blocks at the right edge of the image. */
151 jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
152 (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
153 for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
154 coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
155 }
156 }
157 } else {
158 /* Create a whole row of dummy blocks at the bottom of the image. */
159 jzero_far((void FAR *) coef->MCU_buffer[blkn],
160 compptr->MCU_width * SIZEOF(JBLOCK));
161 for (bi = 0; bi < compptr->MCU_width; bi++) {
162 coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
163 }
164 }
165 blkn += compptr->MCU_width;
166 ypos += DCTSIZE;
167 }
168 }
169 /* Try to write the MCU. In event of a suspension failure, we will
170 * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
171 */
172 if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer))
173 break; /* suspension forced; exit loop */
174 }
175 if (MCU_col_num > last_MCU_col)
176 coef->MCU_row_num++; /* advance if we finished the row */
177 *in_mcu_ctr = MCU_col_num;
178}
179
180
181#ifdef FULL_COEF_BUFFER_SUPPORTED
182
183/*
184 * Process some data in the first pass of a multi-pass case.
185 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
186 * per call, ie, v_samp_factor block rows for each component in the image.
187 * This amount of data is read from the source buffer, DCT'd and quantized,
188 * and saved into the virtual arrays. We also generate suitable dummy blocks
189 * as needed at the right and lower edges. (The dummy blocks are constructed
190 * in the virtual arrays, which have been padded appropriately.) This makes
191 * it possible for subsequent passes not to worry about real vs. dummy blocks.
192 *
193 * We must also emit the data to the entropy encoder. This is conveniently
194 * done by calling compress_output() after we've loaded the current strip
195 * of the virtual arrays.
196 *
197 * NB: input_buf contains a plane for each component in image. All
198 * components are DCT'd and loaded into the virtual arrays in this pass.
199 * However, it may be that only a subset of the components are emitted to
200 * the entropy encoder during this first pass; be careful about looking
201 * at the scan-dependent variables (MCU dimensions, etc).
202 */
203
204METHODDEF void
205compress_first_pass (j_compress_ptr cinfo,
206 JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr)
207{
208 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
209 JDIMENSION last_MCU_row = cinfo->total_iMCU_rows - 1;
210 JDIMENSION blocks_across, MCUs_across, MCUindex;
211 int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
212 JCOEF lastDC;
213 jpeg_component_info *compptr;
214 JBLOCKARRAY buffer;
215 JBLOCKROW thisblockrow, lastblockrow;
216
217 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
218 ci++, compptr++) {
219 /* Align the virtual buffer for this component. */
220 buffer = (*cinfo->mem->access_virt_barray)
221 ((j_common_ptr) cinfo, coef->whole_image[ci],
222 coef->MCU_row_num * compptr->v_samp_factor, TRUE);
223 /* Count non-dummy DCT block rows in this iMCU row. */
224 if (coef->MCU_row_num < last_MCU_row)
225 block_rows = compptr->v_samp_factor;
226 else {
227 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
228 if (block_rows == 0) block_rows = compptr->v_samp_factor;
229 }
230 blocks_across = compptr->width_in_blocks;
231 h_samp_factor = compptr->h_samp_factor;
232 /* Count number of dummy blocks to be added at the right margin. */
233 ndummy = (int) (blocks_across % h_samp_factor);
234 if (ndummy > 0)
235 ndummy = h_samp_factor - ndummy;
236 /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
237 * on forward_DCT processes a complete horizontal row of DCT blocks.
238 */
239 for (block_row = 0; block_row < block_rows; block_row++) {
240 thisblockrow = buffer[block_row];
241 (*cinfo->fdct->forward_DCT) (cinfo, compptr,
242 input_buf[ci], thisblockrow,
243 (JDIMENSION) (block_row * DCTSIZE),
244 (JDIMENSION) 0, blocks_across);
245 if (ndummy > 0) {
246 /* Create dummy blocks at the right edge of the image. */
247 thisblockrow += blocks_across; /* => first dummy block */
248 jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
249 lastDC = thisblockrow[-1][0];
250 for (bi = 0; bi < ndummy; bi++) {
251 thisblockrow[bi][0] = lastDC;
252 }
253 }
254 }
255 /* If at end of image, create dummy block rows as needed.
256 * The tricky part here is that within each MCU, we want the DC values
257 * of the dummy blocks to match the last real block's DC value.
258 * This squeezes a few more bytes out of the resulting file...
259 */
260 if (coef->MCU_row_num == last_MCU_row) {
261 blocks_across += ndummy; /* include lower right corner */
262 MCUs_across = blocks_across / h_samp_factor;
263 for (block_row = block_rows; block_row < compptr->v_samp_factor;
264 block_row++) {
265 thisblockrow = buffer[block_row];
266 lastblockrow = buffer[block_row-1];
267 jzero_far((void FAR *) thisblockrow,
268 (size_t) (blocks_across * SIZEOF(JBLOCK)));
269 for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
270 lastDC = lastblockrow[h_samp_factor-1][0];
271 for (bi = 0; bi < h_samp_factor; bi++) {
272 thisblockrow[bi][0] = lastDC;
273 }
274 thisblockrow += h_samp_factor; /* advance to next MCU in row */
275 lastblockrow += h_samp_factor;
276 }
277 }
278 }
279 }
280 /* NB: compress_output will increment MCU_row_num */
281
282 /* Emit data to the entropy encoder, sharing code with subsequent passes */
283 compress_output(cinfo, input_buf, in_mcu_ctr);
284}
285
286
287/*
288 * Process some data in subsequent passes of a multi-pass case.
289 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
290 * per call, ie, v_samp_factor block rows for each component in the scan.
291 * The data is obtained from the virtual arrays and fed to the entropy coder.
292 *
293 * Note that output suspension is not supported during multi-pass operation,
294 * so the complete MCU row will always be emitted to the entropy encoder
295 * before returning.
296 *
297 * NB: input_buf is ignored; it is likely to be a NULL pointer.
298 */
299
300METHODDEF void
301compress_output (j_compress_ptr cinfo,
302 JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr)
303{
304 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
305 JDIMENSION MCU_col_num; /* index of current MCU within row */
306 int blkn, ci, xindex, yindex, yoffset, num_MCU_rows;
307 JDIMENSION remaining_rows, start_col;
308 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
309 JBLOCKROW buffer_ptr;
310 jpeg_component_info *compptr;
311
312 /* Align the virtual buffers for the components used in this scan.
313 * NB: during first pass, this is safe only because the buffers will
314 * already be aligned properly, so jmemmgr.c won't need to do any I/O.
315 */
316 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
317 compptr = cinfo->cur_comp_info[ci];
318 buffer[ci] = (*cinfo->mem->access_virt_barray)
319 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
320 coef->MCU_row_num * compptr->v_samp_factor, FALSE);
321 }
322
323 /* In an interleaved scan, we process exactly one MCU row.
324 * In a noninterleaved scan, we need to process v_samp_factor MCU rows,
325 * each of which contains a single block row.
326 */
327 if (cinfo->comps_in_scan == 1) {
328 compptr = cinfo->cur_comp_info[0];
329 num_MCU_rows = compptr->v_samp_factor;
330 /* but watch out for the bottom of the image */
331 remaining_rows = cinfo->MCU_rows_in_scan -
332 coef->MCU_row_num * compptr->v_samp_factor;
333 if (remaining_rows < (JDIMENSION) num_MCU_rows)
334 num_MCU_rows = (int) remaining_rows;
335 } else {
336 num_MCU_rows = 1;
337 }
338
339 /* Loop to process one whole iMCU row */
340 for (yoffset = 0; yoffset < num_MCU_rows; yoffset++) {
341 for (MCU_col_num = 0; MCU_col_num < cinfo->MCUs_per_row; MCU_col_num++) {
342 /* Construct list of pointers to DCT blocks belonging to this MCU */
343 blkn = 0; /* index of current DCT block within MCU */
344 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
345 compptr = cinfo->cur_comp_info[ci];
346 start_col = MCU_col_num * compptr->MCU_width;
347 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
348 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
349 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
350 coef->MCU_buffer[blkn++] = buffer_ptr++;
351 }
352 }
353 }
354 /* Try to write the MCU. */
355 if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
356 ERREXIT(cinfo, JERR_CANT_SUSPEND); /* not supported */
357 }
358 }
359 }
360
361 coef->MCU_row_num++; /* advance to next iMCU row */
362 *in_mcu_ctr = cinfo->MCUs_per_row;
363}
364
365#endif /* FULL_COEF_BUFFER_SUPPORTED */
366
367
368/*
369 * Initialize coefficient buffer controller.
370 */
371
372GLOBAL void
373jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
374{
375 my_coef_ptr coef;
376 int ci, i;
377 jpeg_component_info *compptr;
378 JBLOCKROW buffer;
379
380 coef = (my_coef_ptr)
381 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
382 SIZEOF(my_coef_controller));
383 cinfo->coef = (struct jpeg_c_coef_controller *) coef;
384 coef->pub.start_pass = start_pass_coef;
385
386 /* Create the coefficient buffer. */
387 if (need_full_buffer) {
388#ifdef FULL_COEF_BUFFER_SUPPORTED
389 /* Allocate a full-image virtual array for each component, */
390 /* padded to a multiple of samp_factor DCT blocks in each direction. */
391 /* Note memmgr implicitly pads the vertical direction. */
392 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
393 ci++, compptr++) {
394 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
395 ((j_common_ptr) cinfo, JPOOL_IMAGE,
396 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
397 (long) compptr->h_samp_factor),
398 compptr->height_in_blocks,
399 (JDIMENSION) compptr->v_samp_factor);
400 }
401#else
402 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
403#endif
404 } else {
405 /* We only need a single-MCU buffer. */
406 buffer = (JBLOCKROW)
407 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
408 MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
409 for (i = 0; i < MAX_BLOCKS_IN_MCU; i++) {
410 coef->MCU_buffer[i] = buffer + i;
411 }
412 coef->whole_image[0] = NULL; /* flag for no virtual arrays */
413 }
414}