source: trunk/src/3rdparty/libjpeg/jdcoefct.c@ 116

Last change on this file since 116 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 24.6 KB
Line 
1/*
2 * jdcoefct.c
3 *
4 * Copyright (C) 1994-1997, 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 decompression.
9 * This controller is the top level of the JPEG decompressor proper.
10 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
11 *
12 * In buffered-image mode, this controller is the interface between
13 * input-oriented processing and output-oriented processing.
14 * Also, the input side (only) is used when reading a file for transcoding.
15 */
16
17#define JPEG_INTERNALS
18#include "jinclude.h"
19#include "jpeglib.h"
20
21/* Block smoothing is only applicable for progressive JPEG, so: */
22#ifndef D_PROGRESSIVE_SUPPORTED
23#undef BLOCK_SMOOTHING_SUPPORTED
24#endif
25
26/* Private buffer controller object */
27
28typedef struct {
29 struct jpeg_d_coef_controller pub; /* public fields */
30
31 /* These variables keep track of the current location of the input side. */
32 /* cinfo->input_iMCU_row is also used for this. */
33 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
34 int MCU_vert_offset; /* counts MCU rows within iMCU row */
35 int MCU_rows_per_iMCU_row; /* number of such rows needed */
36
37 /* The output side's location is represented by cinfo->output_iMCU_row. */
38
39 /* In single-pass modes, it's sufficient to buffer just one MCU.
40 * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
41 * and let the entropy decoder write into that workspace each time.
42 * (On 80x86, the workspace is FAR even though it's not really very big;
43 * this is to keep the module interfaces unchanged when a large coefficient
44 * buffer is necessary.)
45 * In multi-pass modes, this array points to the current MCU's blocks
46 * within the virtual arrays; it is used only by the input side.
47 */
48 JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
49
50#ifdef D_MULTISCAN_FILES_SUPPORTED
51 /* In multi-pass modes, we need a virtual block array for each component. */
52 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
53#endif
54
55#ifdef BLOCK_SMOOTHING_SUPPORTED
56 /* When doing block smoothing, we latch coefficient Al values here */
57 int * coef_bits_latch;
58#define SAVED_COEFS 6 /* we save coef_bits[0..5] */
59#endif
60} my_coef_controller;
61
62typedef my_coef_controller * my_coef_ptr;
63
64/* Forward declarations */
65METHODDEF(int) decompress_onepass
66 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
67#ifdef D_MULTISCAN_FILES_SUPPORTED
68METHODDEF(int) decompress_data
69 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
70#endif
71#ifdef BLOCK_SMOOTHING_SUPPORTED
72LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
73METHODDEF(int) decompress_smooth_data
74 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
75#endif
76
77
78LOCAL(void)
79start_iMCU_row (j_decompress_ptr cinfo)
80/* Reset within-iMCU-row counters for a new row (input side) */
81{
82 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
83
84 /* In an interleaved scan, an MCU row is the same as an iMCU row.
85 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
86 * But at the bottom of the image, process only what's left.
87 */
88 if (cinfo->comps_in_scan > 1) {
89 coef->MCU_rows_per_iMCU_row = 1;
90 } else {
91 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
92 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
93 else
94 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
95 }
96
97 coef->MCU_ctr = 0;
98 coef->MCU_vert_offset = 0;
99}
100
101
102/*
103 * Initialize for an input processing pass.
104 */
105
106METHODDEF(void)
107start_input_pass (j_decompress_ptr cinfo)
108{
109 cinfo->input_iMCU_row = 0;
110 start_iMCU_row(cinfo);
111}
112
113
114/*
115 * Initialize for an output processing pass.
116 */
117
118METHODDEF(void)
119start_output_pass (j_decompress_ptr cinfo)
120{
121#ifdef BLOCK_SMOOTHING_SUPPORTED
122 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
123
124 /* If multipass, check to see whether to use block smoothing on this pass */
125 if (coef->pub.coef_arrays != NULL) {
126 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
127 coef->pub.decompress_data = decompress_smooth_data;
128 else
129 coef->pub.decompress_data = decompress_data;
130 }
131#endif
132 cinfo->output_iMCU_row = 0;
133}
134
135
136/*
137 * Decompress and return some data in the single-pass case.
138 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
139 * Input and output must run in lockstep since we have only a one-MCU buffer.
140 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
141 *
142 * NB: output_buf contains a plane for each component in image,
143 * which we index according to the component's SOF position.
144 */
145
146METHODDEF(int)
147decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
148{
149 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
150 JDIMENSION MCU_col_num; /* index of current MCU within row */
151 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
152 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
153 int blkn, ci, xindex, yindex, yoffset, useful_width;
154 JSAMPARRAY output_ptr;
155 JDIMENSION start_col, output_col;
156 jpeg_component_info *compptr;
157 inverse_DCT_method_ptr inverse_DCT;
158
159 /* Loop to process 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 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
165 jzero_far((void FAR *) coef->MCU_buffer[0],
166 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
167 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
168 /* Suspension forced; update state counters and exit */
169 coef->MCU_vert_offset = yoffset;
170 coef->MCU_ctr = MCU_col_num;
171 return JPEG_SUSPENDED;
172 }
173 /* Determine where data should go in output_buf and do the IDCT thing.
174 * We skip dummy blocks at the right and bottom edges (but blkn gets
175 * incremented past them!). Note the inner loop relies on having
176 * allocated the MCU_buffer[] blocks sequentially.
177 */
178 blkn = 0; /* index of current DCT block within MCU */
179 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
180 compptr = cinfo->cur_comp_info[ci];
181 /* Don't bother to IDCT an uninteresting component. */
182 if (! compptr->component_needed) {
183 blkn += compptr->MCU_blocks;
184 continue;
185 }
186 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
187 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
188 : compptr->last_col_width;
189 output_ptr = output_buf[compptr->component_index] +
190 yoffset * compptr->DCT_scaled_size;
191 start_col = MCU_col_num * compptr->MCU_sample_width;
192 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
193 if (cinfo->input_iMCU_row < last_iMCU_row ||
194 yoffset+yindex < compptr->last_row_height) {
195 output_col = start_col;
196 for (xindex = 0; xindex < useful_width; xindex++) {
197 (*inverse_DCT) (cinfo, compptr,
198 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
199 output_ptr, output_col);
200 output_col += compptr->DCT_scaled_size;
201 }
202 }
203 blkn += compptr->MCU_width;
204 output_ptr += compptr->DCT_scaled_size;
205 }
206 }
207 }
208 /* Completed an MCU row, but perhaps not an iMCU row */
209 coef->MCU_ctr = 0;
210 }
211 /* Completed the iMCU row, advance counters for next one */
212 cinfo->output_iMCU_row++;
213 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
214 start_iMCU_row(cinfo);
215 return JPEG_ROW_COMPLETED;
216 }
217 /* Completed the scan */
218 (*cinfo->inputctl->finish_input_pass) (cinfo);
219 return JPEG_SCAN_COMPLETED;
220}
221
222
223/*
224 * Dummy consume-input routine for single-pass operation.
225 */
226
227METHODDEF(int)
228dummy_consume_data (j_decompress_ptr cinfo)
229{
230 return JPEG_SUSPENDED; /* Always indicate nothing was done */
231}
232
233
234#ifdef D_MULTISCAN_FILES_SUPPORTED
235
236/*
237 * Consume input data and store it in the full-image coefficient buffer.
238 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
239 * ie, v_samp_factor block rows for each component in the scan.
240 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
241 */
242
243METHODDEF(int)
244consume_data (j_decompress_ptr cinfo)
245{
246 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
247 JDIMENSION MCU_col_num; /* index of current MCU within row */
248 int blkn, ci, xindex, yindex, yoffset;
249 JDIMENSION start_col;
250 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
251 JBLOCKROW buffer_ptr;
252 jpeg_component_info *compptr;
253
254 /* Align the virtual buffers for the components used in this scan. */
255 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
256 compptr = cinfo->cur_comp_info[ci];
257 buffer[ci] = (*cinfo->mem->access_virt_barray)
258 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
259 cinfo->input_iMCU_row * compptr->v_samp_factor,
260 (JDIMENSION) compptr->v_samp_factor, TRUE);
261 /* Note: entropy decoder expects buffer to be zeroed,
262 * but this is handled automatically by the memory manager
263 * because we requested a pre-zeroed array.
264 */
265 }
266
267 /* Loop to process one whole iMCU row */
268 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
269 yoffset++) {
270 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
271 MCU_col_num++) {
272 /* Construct list of pointers to DCT blocks belonging to this MCU */
273 blkn = 0; /* index of current DCT block within MCU */
274 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
275 compptr = cinfo->cur_comp_info[ci];
276 start_col = MCU_col_num * compptr->MCU_width;
277 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
278 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
279 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
280 coef->MCU_buffer[blkn++] = buffer_ptr++;
281 }
282 }
283 }
284 /* Try to fetch the MCU. */
285 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
286 /* Suspension forced; update state counters and exit */
287 coef->MCU_vert_offset = yoffset;
288 coef->MCU_ctr = MCU_col_num;
289 return JPEG_SUSPENDED;
290 }
291 }
292 /* Completed an MCU row, but perhaps not an iMCU row */
293 coef->MCU_ctr = 0;
294 }
295 /* Completed the iMCU row, advance counters for next one */
296 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
297 start_iMCU_row(cinfo);
298 return JPEG_ROW_COMPLETED;
299 }
300 /* Completed the scan */
301 (*cinfo->inputctl->finish_input_pass) (cinfo);
302 return JPEG_SCAN_COMPLETED;
303}
304
305
306/*
307 * Decompress and return some data in the multi-pass case.
308 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
309 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
310 *
311 * NB: output_buf contains a plane for each component in image.
312 */
313
314METHODDEF(int)
315decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
316{
317 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
318 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
319 JDIMENSION block_num;
320 int ci, block_row, block_rows;
321 JBLOCKARRAY buffer;
322 JBLOCKROW buffer_ptr;
323 JSAMPARRAY output_ptr;
324 JDIMENSION output_col;
325 jpeg_component_info *compptr;
326 inverse_DCT_method_ptr inverse_DCT;
327
328 /* Force some input to be done if we are getting ahead of the input. */
329 while (cinfo->input_scan_number < cinfo->output_scan_number ||
330 (cinfo->input_scan_number == cinfo->output_scan_number &&
331 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
332 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
333 return JPEG_SUSPENDED;
334 }
335
336 /* OK, output from the virtual arrays. */
337 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
338 ci++, compptr++) {
339 /* Don't bother to IDCT an uninteresting component. */
340 if (! compptr->component_needed)
341 continue;
342 /* Align the virtual buffer for this component. */
343 buffer = (*cinfo->mem->access_virt_barray)
344 ((j_common_ptr) cinfo, coef->whole_image[ci],
345 cinfo->output_iMCU_row * compptr->v_samp_factor,
346 (JDIMENSION) compptr->v_samp_factor, FALSE);
347 /* Count non-dummy DCT block rows in this iMCU row. */
348 if (cinfo->output_iMCU_row < last_iMCU_row)
349 block_rows = compptr->v_samp_factor;
350 else {
351 /* NB: can't use last_row_height here; it is input-side-dependent! */
352 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
353 if (block_rows == 0) block_rows = compptr->v_samp_factor;
354 }
355 inverse_DCT = cinfo->idct->inverse_DCT[ci];
356 output_ptr = output_buf[ci];
357 /* Loop over all DCT blocks to be processed. */
358 for (block_row = 0; block_row < block_rows; block_row++) {
359 buffer_ptr = buffer[block_row];
360 output_col = 0;
361 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
362 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
363 output_ptr, output_col);
364 buffer_ptr++;
365 output_col += compptr->DCT_scaled_size;
366 }
367 output_ptr += compptr->DCT_scaled_size;
368 }
369 }
370
371 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
372 return JPEG_ROW_COMPLETED;
373 return JPEG_SCAN_COMPLETED;
374}
375
376#endif /* D_MULTISCAN_FILES_SUPPORTED */
377
378
379#ifdef BLOCK_SMOOTHING_SUPPORTED
380
381/*
382 * This code applies interblock smoothing as described by section K.8
383 * of the JPEG standard: the first 5 AC coefficients are estimated from
384 * the DC values of a DCT block and its 8 neighboring blocks.
385 * We apply smoothing only for progressive JPEG decoding, and only if
386 * the coefficients it can estimate are not yet known to full precision.
387 */
388
389/* Natural-order array positions of the first 5 zigzag-order coefficients */
390#define Q01_POS 1
391#define Q10_POS 8
392#define Q20_POS 16
393#define Q11_POS 9
394#define Q02_POS 2
395
396/*
397 * Determine whether block smoothing is applicable and safe.
398 * We also latch the current states of the coef_bits[] entries for the
399 * AC coefficients; otherwise, if the input side of the decompressor
400 * advances into a new scan, we might think the coefficients are known
401 * more accurately than they really are.
402 */
403
404LOCAL(boolean)
405smoothing_ok (j_decompress_ptr cinfo)
406{
407 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
408 boolean smoothing_useful = FALSE;
409 int ci, coefi;
410 jpeg_component_info *compptr;
411 JQUANT_TBL * qtable;
412 int * coef_bits;
413 int * coef_bits_latch;
414
415 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
416 return FALSE;
417
418 /* Allocate latch area if not already done */
419 if (coef->coef_bits_latch == NULL)
420 coef->coef_bits_latch = (int *)
421 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
422 cinfo->num_components *
423 (SAVED_COEFS * SIZEOF(int)));
424 coef_bits_latch = coef->coef_bits_latch;
425
426 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
427 ci++, compptr++) {
428 /* All components' quantization values must already be latched. */
429 if ((qtable = compptr->quant_table) == NULL)
430 return FALSE;
431 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
432 if (qtable->quantval[0] == 0 ||
433 qtable->quantval[Q01_POS] == 0 ||
434 qtable->quantval[Q10_POS] == 0 ||
435 qtable->quantval[Q20_POS] == 0 ||
436 qtable->quantval[Q11_POS] == 0 ||
437 qtable->quantval[Q02_POS] == 0)
438 return FALSE;
439 /* DC values must be at least partly known for all components. */
440 coef_bits = cinfo->coef_bits[ci];
441 if (coef_bits[0] < 0)
442 return FALSE;
443 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
444 for (coefi = 1; coefi <= 5; coefi++) {
445 coef_bits_latch[coefi] = coef_bits[coefi];
446 if (coef_bits[coefi] != 0)
447 smoothing_useful = TRUE;
448 }
449 coef_bits_latch += SAVED_COEFS;
450 }
451
452 return smoothing_useful;
453}
454
455
456/*
457 * Variant of decompress_data for use when doing block smoothing.
458 */
459
460METHODDEF(int)
461decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
462{
463 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
464 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
465 JDIMENSION block_num, last_block_column;
466 int ci, block_row, block_rows, access_rows;
467 JBLOCKARRAY buffer;
468 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
469 JSAMPARRAY output_ptr;
470 JDIMENSION output_col;
471 jpeg_component_info *compptr;
472 inverse_DCT_method_ptr inverse_DCT;
473 boolean first_row, last_row;
474 JBLOCK workspace;
475 int *coef_bits;
476 JQUANT_TBL *quanttbl;
477 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
478 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
479 int Al, pred;
480
481 /* Force some input to be done if we are getting ahead of the input. */
482 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
483 ! cinfo->inputctl->eoi_reached) {
484 if (cinfo->input_scan_number == cinfo->output_scan_number) {
485 /* If input is working on current scan, we ordinarily want it to
486 * have completed the current row. But if input scan is DC,
487 * we want it to keep one row ahead so that next block row's DC
488 * values are up to date.
489 */
490 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
491 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
492 break;
493 }
494 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
495 return JPEG_SUSPENDED;
496 }
497
498 /* OK, output from the virtual arrays. */
499 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
500 ci++, compptr++) {
501 /* Don't bother to IDCT an uninteresting component. */
502 if (! compptr->component_needed)