Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / main / jccoefct.c
blob17ebbd5fe85d38bbb843c114623119913325293a
1 /*
2 $Id$
3 */
5 /*
6 * jccoefct.c
8 * Copyright (C) 1994-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains the coefficient buffer controller for compression.
13 * This controller is the top level of the JPEG compressor proper.
14 * The coefficient buffer lies between forward-DCT and entropy encoding steps.
17 #define JPEG_INTERNALS
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jlossy.h" /* Private declarations for lossy codec */
23 /* We use a full-image coefficient buffer when doing Huffman optimization,
24 * and also for writing multiple-scan JPEG files. In all cases, the DCT
25 * step is run during the first pass, and subsequent passes need only read
26 * the buffered coefficients.
28 #ifdef ENTROPY_OPT_SUPPORTED
29 #define FULL_COEF_BUFFER_SUPPORTED
30 #else
31 #ifdef C_MULTISCAN_FILES_SUPPORTED
32 #define FULL_COEF_BUFFER_SUPPORTED
33 #endif
34 #endif
37 /* Private buffer controller object */
39 typedef struct {
40 JDIMENSION iMCU_row_num; /* iMCU row # within image */
41 JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
42 int MCU_vert_offset; /* counts MCU rows within iMCU row */
43 int MCU_rows_per_iMCU_row; /* number of such rows needed */
45 /* For single-pass compression, it's sufficient to buffer just one MCU
46 * (although this may prove a bit slow in practice). We allocate a
47 * workspace of C_MAX_DATA_UNITS_IN_MCU coefficient blocks, and reuse it for
48 * each MCU constructed and sent. (On 80x86, the workspace is FAR even
49 * though it's not really very big; this is to keep the module interfaces
50 * unchanged when a large coefficient buffer is necessary.)
51 * In multi-pass modes, this array points to the current MCU's blocks
52 * within the virtual arrays.
54 JBLOCKROW MCU_buffer[C_MAX_DATA_UNITS_IN_MCU];
56 /* In multi-pass modes, we need a virtual block array for each component. */
57 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
58 } c_coef_controller;
60 typedef c_coef_controller * c_coef_ptr;
63 /* Forward declarations */
64 METHODDEF(boolean) compress_data
65 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
66 #ifdef FULL_COEF_BUFFER_SUPPORTED
67 METHODDEF(boolean) compress_first_pass
68 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
69 METHODDEF(boolean) compress_output
70 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
71 #endif
74 LOCAL(void)
75 start_iMCU_row (j_compress_ptr cinfo)
76 /* Reset within-iMCU-row counters for a new row */
78 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
79 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
81 /* In an interleaved scan, an MCU row is the same as an iMCU row.
82 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
83 * But at the bottom of the image, process only what's left.
85 if (cinfo->comps_in_scan > 1) {
86 coef->MCU_rows_per_iMCU_row = 1;
87 } else {
88 if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
89 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
90 else
91 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
94 coef->mcu_ctr = 0;
95 coef->MCU_vert_offset = 0;
100 * Initialize for a processing pass.
103 METHODDEF(void)
104 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
106 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
107 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
109 coef->iMCU_row_num = 0;
110 start_iMCU_row(cinfo);
112 switch (pass_mode) {
113 case JBUF_PASS_THRU:
114 if (coef->whole_image[0] != NULL)
115 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
116 lossyc->pub.compress_data = compress_data;
117 break;
118 #ifdef FULL_COEF_BUFFER_SUPPORTED
119 case JBUF_SAVE_AND_PASS:
120 if (coef->whole_image[0] == NULL)
121 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
122 lossyc->pub.compress_data = compress_first_pass;
123 break;
124 case JBUF_CRANK_DEST:
125 if (coef->whole_image[0] == NULL)
126 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
127 lossyc->pub.compress_data = compress_output;
128 break;
129 #endif
130 default:
131 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
132 break;
138 * Process some data in the single-pass case.
139 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
140 * per call, ie, v_samp_factor block rows for each component in the image.
141 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
143 * NB: input_buf contains a plane for each component in image,
144 * which we index according to the component's SOF position.
147 METHODDEF(boolean)
148 compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
150 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
151 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
152 JDIMENSION MCU_col_num; /* index of current MCU within row */
153 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
154 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
155 int blkn, bi, ci, yindex, yoffset, blockcnt;
156 JDIMENSION ypos, xpos;
157 jpeg_component_info *compptr;
159 /* Loop to write as much as one whole iMCU row */
160 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
161 yoffset++) {
162 for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
163 MCU_col_num++) {
164 /* Determine where data comes from in input_buf and do the DCT thing.
165 * Each call on forward_DCT processes a horizontal row of DCT blocks
166 * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
167 * sequentially. Dummy blocks at the right or bottom edge are filled in
168 * specially. The data in them does not matter for image reconstruction,
169 * so we fill them with values that will encode to the smallest amount of
170 * data, viz: all zeroes in the AC entries, DC entries equal to previous
171 * block's DC value. (Thanks to Thomas Kinsman for this idea.)
173 blkn = 0;
174 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
175 compptr = cinfo->cur_comp_info[ci];
176 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
177 : compptr->last_col_width;
178 xpos = MCU_col_num * compptr->MCU_sample_width;
179 ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
180 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
181 if (coef->iMCU_row_num < last_iMCU_row ||
182 yoffset+yindex < compptr->last_row_height) {
183 (*lossyc->fdct_forward_DCT) (cinfo, compptr,
184 input_buf[compptr->component_index],
185 coef->MCU_buffer[blkn],
186 ypos, xpos, (JDIMENSION) blockcnt);
187 if (blockcnt < compptr->MCU_width) {
188 /* Create some dummy blocks at the right edge of the image. */
189 jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
190 (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
191 for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
192 coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
195 } else {
196 /* Create a row of dummy blocks at the bottom of the image. */
197 jzero_far((void FAR *) coef->MCU_buffer[blkn],
198 compptr->MCU_width * SIZEOF(JBLOCK));
199 for (bi = 0; bi < compptr->MCU_width; bi++) {
200 coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
203 blkn += compptr->MCU_width;
204 ypos += DCTSIZE;
207 /* Try to write the MCU. In event of a suspension failure, we will
208 * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
210 if (! (*lossyc->entropy_encode_mcu) (cinfo, coef->MCU_buffer)) {
211 /* Suspension forced; update state counters and exit */
212 coef->MCU_vert_offset = yoffset;
213 coef->mcu_ctr = MCU_col_num;
214 return FALSE;
217 /* Completed an MCU row, but perhaps not an iMCU row */
218 coef->mcu_ctr = 0;
220 /* Completed the iMCU row, advance counters for next one */
221 coef->iMCU_row_num++;
222 start_iMCU_row(cinfo);
223 return TRUE;
227 #ifdef FULL_COEF_BUFFER_SUPPORTED
230 * Process some data in the first pass of a multi-pass case.
231 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
232 * per call, ie, v_samp_factor block rows for each component in the image.
233 * This amount of data is read from the source buffer, DCT'd and quantized,
234 * and saved into the virtual arrays. We also generate suitable dummy blocks
235 * as needed at the right and lower edges. (The dummy blocks are constructed
236 * in the virtual arrays, which have been padded appropriately.) This makes
237 * it possible for subsequent passes not to worry about real vs. dummy blocks.
239 * We must also emit the data to the entropy encoder. This is conveniently
240 * done by calling compress_output() after we've loaded the current strip
241 * of the virtual arrays.
243 * NB: input_buf contains a plane for each component in image. All
244 * components are DCT'd and loaded into the virtual arrays in this pass.
245 * However, it may be that only a subset of the components are emitted to
246 * the entropy encoder during this first pass; be careful about looking
247 * at the scan-dependent variables (MCU dimensions, etc).
250 METHODDEF(boolean)
251 compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
253 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
254 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
255 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
256 JDIMENSION blocks_across, MCUs_across, MCUindex;
257 int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
258 JCOEF lastDC;
259 jpeg_component_info *compptr;
260 JBLOCKARRAY buffer;
261 JBLOCKROW thisblockrow, lastblockrow;
263 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
264 ci++, compptr++) {
265 /* Align the virtual buffer for this component. */
266 buffer = (*cinfo->mem->access_virt_barray)
267 ((j_common_ptr) cinfo, coef->whole_image[ci],
268 coef->iMCU_row_num * compptr->v_samp_factor,
269 (JDIMENSION) compptr->v_samp_factor, TRUE);
270 /* Count non-dummy DCT block rows in this iMCU row. */
271 if (coef->iMCU_row_num < last_iMCU_row)
272 block_rows = compptr->v_samp_factor;
273 else {
274 /* NB: can't use last_row_height here, since may not be set! */
275 block_rows = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
276 if (block_rows == 0) block_rows = compptr->v_samp_factor;
278 blocks_across = compptr->width_in_data_units;
279 h_samp_factor = compptr->h_samp_factor;
280 /* Count number of dummy blocks to be added at the right margin. */
281 ndummy = (int) (blocks_across % h_samp_factor);
282 if (ndummy > 0)
283 ndummy = h_samp_factor - ndummy;
284 /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
285 * on forward_DCT processes a complete horizontal row of DCT blocks.
287 for (block_row = 0; block_row < block_rows; block_row++) {
288 thisblockrow = buffer[block_row];
289 (*lossyc->fdct_forward_DCT) (cinfo, compptr,
290 input_buf[ci], thisblockrow,
291 (JDIMENSION) (block_row * DCTSIZE),
292 (JDIMENSION) 0, blocks_across);
293 if (ndummy > 0) {
294 /* Create dummy blocks at the right edge of the image. */
295 thisblockrow += blocks_across; /* => first dummy block */
296 jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
297 lastDC = thisblockrow[-1][0];
298 for (bi = 0; bi < ndummy; bi++) {
299 thisblockrow[bi][0] = lastDC;
303 /* If at end of image, create dummy block rows as needed.
304 * The tricky part here is that within each MCU, we want the DC values
305 * of the dummy blocks to match the last real block's DC value.
306 * This squeezes a few more bytes out of the resulting file...
308 if (coef->iMCU_row_num == last_iMCU_row) {
309 blocks_across += ndummy; /* include lower right corner */
310 MCUs_across = blocks_across / h_samp_factor;
311 for (block_row = block_rows; block_row < compptr->v_samp_factor;
312 block_row++) {
313 thisblockrow = buffer[block_row];
314 lastblockrow = buffer[block_row-1];
315 jzero_far((void FAR *) thisblockrow,
316 (size_t) (blocks_across * SIZEOF(JBLOCK)));
317 for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
318 lastDC = lastblockrow[h_samp_factor-1][0];
319 for (bi = 0; bi < h_samp_factor; bi++) {
320 thisblockrow[bi][0] = lastDC;
322 thisblockrow += h_samp_factor; /* advance to next MCU in row */
323 lastblockrow += h_samp_factor;
328 /* NB: compress_output will increment iMCU_row_num if successful.
329 * A suspension return will result in redoing all the work above next time.
332 /* Emit data to the entropy encoder, sharing code with subsequent passes */
333 return compress_output(cinfo, input_buf);
338 * Process some data in subsequent passes of a multi-pass case.
339 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
340 * per call, ie, v_samp_factor block rows for each component in the scan.
341 * The data is obtained from the virtual arrays and fed to the entropy coder.
342 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
344 * NB: input_buf is ignored; it is likely to be a NULL pointer.
347 METHODDEF(boolean)
348 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
350 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
351 c_coef_ptr coef = (c_coef_ptr) lossyc->coef_private;
352 JDIMENSION MCU_col_num; /* index of current MCU within row */
353 int blkn, ci, xindex, yindex, yoffset;
354 JDIMENSION start_col;
355 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
356 JBLOCKROW buffer_ptr;
357 jpeg_component_info *compptr;
359 /* Align the virtual buffers for the components used in this scan.
360 * NB: during first pass, this is safe only because the buffers will
361 * already be aligned properly, so jmemmgr.c won't need to do any I/O.
363 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
364 compptr = cinfo->cur_comp_info[ci];
365 buffer[ci] = (*cinfo->mem->access_virt_barray)
366 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
367 coef->iMCU_row_num * compptr->v_samp_factor,
368 (JDIMENSION) compptr->v_samp_factor, FALSE);
371 /* Loop to process one whole iMCU row */
372 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
373 yoffset++) {
374 for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
375 MCU_col_num++) {
376 /* Construct list of pointers to DCT blocks belonging to this MCU */
377 blkn = 0; /* index of current DCT block within MCU */
378 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
379 compptr = cinfo->cur_comp_info[ci];
380 start_col = MCU_col_num * compptr->MCU_width;
381 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
382 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
383 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
384 coef->MCU_buffer[blkn++] = buffer_ptr++;
388 /* Try to write the MCU. */
389 if (! (*lossyc->entropy_encode_mcu) (cinfo, coef->MCU_buffer)) {
390 /* Suspension forced; update state counters and exit */
391 coef->MCU_vert_offset = yoffset;
392 coef->mcu_ctr = MCU_col_num;
393 return FALSE;
396 /* Completed an MCU row, but perhaps not an iMCU row */
397 coef->mcu_ctr = 0;
399 /* Completed the iMCU row, advance counters for next one */
400 coef->iMCU_row_num++;
401 start_iMCU_row(cinfo);
402 return TRUE;
405 #endif /* FULL_COEF_BUFFER_SUPPORTED */
409 * Initialize coefficient buffer controller.
412 JGLOBAL(void)
413 jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
415 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
416 c_coef_ptr coef;
418 coef = (c_coef_ptr)
419 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
420 SIZEOF(c_coef_controller));
421 lossyc->coef_private = (struct jpeg_c_coef_controller *) coef;
422 lossyc->coef_start_pass = start_pass_coef;
424 /* Create the coefficient buffer. */
425 if (need_full_buffer) {
426 #ifdef FULL_COEF_BUFFER_SUPPORTED
427 /* Allocate a full-image virtual array for each component, */
428 /* padded to a multiple of samp_factor DCT blocks in each direction. */
429 int ci;
430 jpeg_component_info *compptr;
432 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
433 ci++, compptr++) {
434 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
435 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
436 (JDIMENSION) jround_up((long) compptr->width_in_data_units,
437 (long) compptr->h_samp_factor),
438 (JDIMENSION) jround_up((long) compptr->height_in_data_units,
439 (long) compptr->v_samp_factor),
440 (JDIMENSION) compptr->v_samp_factor);
442 #else
443 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
444 #endif
445 } else {
446 /* We only need a single-MCU buffer. */
447 JBLOCKROW buffer;
448 int i;
450 buffer = (JBLOCKROW)
451 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
452 C_MAX_DATA_UNITS_IN_MCU * SIZEOF(JBLOCK));
453 for (i = 0; i < C_MAX_DATA_UNITS_IN_MCU; i++) {
454 coef->MCU_buffer[i] = buffer + i;
456 coef->whole_image[0] = NULL; /* flag for no virtual arrays */