Backed out changeset 1b14354719c0 (bug 1895254) for causing bustages on NavigationTra...
[gecko.git] / media / libjpeg / jddiffct.c
blobf1d7f61b5209a4ec9f4d763eac3d575229e262ae
1 /*
2 * jddiffct.c
4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1994-1997, Thomas G. Lane.
6 * Lossless JPEG Modifications:
7 * Copyright (C) 1999, Ken Murchison.
8 * libjpeg-turbo Modifications:
9 * Copyright (C) 2022, D. R. Commander.
10 * For conditions of distribution and use, see the accompanying README.ijg
11 * file.
13 * This file contains the [un]difference buffer controller for decompression.
14 * This controller is the top level of the lossless JPEG decompressor proper.
15 * The difference buffer lies between the entropy decoding and
16 * prediction/undifferencing steps. The undifference buffer lies between the
17 * prediction/undifferencing and scaling steps.
19 * In buffered-image mode, this controller is the interface between
20 * input-oriented processing and output-oriented processing.
23 #define JPEG_INTERNALS
24 #include "jinclude.h"
25 #include "jpeglib.h"
26 #include "jlossls.h" /* Private declarations for lossless codec */
29 #ifdef D_LOSSLESS_SUPPORTED
31 /* Private buffer controller object */
33 typedef struct {
34 struct jpeg_d_coef_controller pub; /* public fields */
36 /* These variables keep track of the current location of the input side. */
37 /* cinfo->input_iMCU_row is also used for this. */
38 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
39 unsigned int restart_rows_to_go; /* MCU rows left in this restart
40 interval */
41 unsigned int MCU_vert_offset; /* counts MCU rows within iMCU row */
42 unsigned int MCU_rows_per_iMCU_row; /* number of such rows needed */
44 /* The output side's location is represented by cinfo->output_iMCU_row. */
46 JDIFFARRAY diff_buf[MAX_COMPONENTS]; /* iMCU row of differences */
47 JDIFFARRAY undiff_buf[MAX_COMPONENTS]; /* iMCU row of undiff'd samples */
49 #ifdef D_MULTISCAN_FILES_SUPPORTED
50 /* In multi-pass modes, we need a virtual sample array for each component. */
51 jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
52 #endif
53 } my_diff_controller;
55 typedef my_diff_controller *my_diff_ptr;
57 /* Forward declarations */
58 METHODDEF(int) decompress_data(j_decompress_ptr cinfo, _JSAMPIMAGE output_buf);
59 #ifdef D_MULTISCAN_FILES_SUPPORTED
60 METHODDEF(int) output_data(j_decompress_ptr cinfo, _JSAMPIMAGE output_buf);
61 #endif
64 LOCAL(void)
65 start_iMCU_row(j_decompress_ptr cinfo)
66 /* Reset within-iMCU-row counters for a new row (input side) */
68 my_diff_ptr diff = (my_diff_ptr)cinfo->coef;
70 /* In an interleaved scan, an MCU row is the same as an iMCU row.
71 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
72 * But at the bottom of the image, process only what's left.
74 if (cinfo->comps_in_scan > 1) {
75 diff->MCU_rows_per_iMCU_row = 1;
76 } else {
77 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
78 diff->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
79 else
80 diff->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
83 diff->MCU_ctr = 0;
84 diff->MCU_vert_offset = 0;
89 * Initialize for an input processing pass.
92 METHODDEF(void)
93 start_input_pass(j_decompress_ptr cinfo)
95 my_diff_ptr diff = (my_diff_ptr)cinfo->coef;
97 /* Because it is hitching a ride on the jpeg_inverse_dct struct,
98 * start_pass_lossless() will be called at the start of the output pass.
99 * This ensures that it will be called at the start of the input pass as
100 * well.
102 (*cinfo->idct->start_pass) (cinfo);
104 /* Check that the restart interval is an integer multiple of the number
105 * of MCUs in an MCU row.
107 if (cinfo->restart_interval % cinfo->MCUs_per_row != 0)
108 ERREXIT2(cinfo, JERR_BAD_RESTART,
109 cinfo->restart_interval, cinfo->MCUs_per_row);
111 /* Initialize restart counter */
112 diff->restart_rows_to_go = cinfo->restart_interval / cinfo->MCUs_per_row;
114 cinfo->input_iMCU_row = 0;
115 start_iMCU_row(cinfo);
120 * Check for a restart marker & resynchronize decoder, undifferencer.
121 * Returns FALSE if must suspend.
124 METHODDEF(boolean)
125 process_restart(j_decompress_ptr cinfo)
127 my_diff_ptr diff = (my_diff_ptr)cinfo->coef;
129 if (!(*cinfo->entropy->process_restart) (cinfo))
130 return FALSE;
132 (*cinfo->idct->start_pass) (cinfo);
134 /* Reset restart counter */
135 diff->restart_rows_to_go = cinfo->restart_interval / cinfo->MCUs_per_row;
137 return TRUE;
142 * Initialize for an output processing pass.
145 METHODDEF(void)
146 start_output_pass(j_decompress_ptr cinfo)
148 cinfo->output_iMCU_row = 0;
153 * Decompress and return some data in the supplied buffer.
154 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
155 * Input and output must run in lockstep since we have only a one-MCU buffer.
156 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
158 * NB: output_buf contains a plane for each component in image,
159 * which we index according to the component's SOF position.
162 METHODDEF(int)
163 decompress_data(j_decompress_ptr cinfo, _JSAMPIMAGE output_buf)
165 my_diff_ptr diff = (my_diff_ptr)cinfo->coef;
166 lossless_decomp_ptr losslessd = (lossless_decomp_ptr)cinfo->idct;
167 JDIMENSION MCU_col_num; /* index of current MCU within row */
168 JDIMENSION MCU_count; /* number of MCUs decoded */
169 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
170 int ci, compi, row, prev_row;
171 unsigned int yoffset;
172 jpeg_component_info *compptr;
174 /* Loop to process as much as one whole iMCU row */
175 for (yoffset = diff->MCU_vert_offset; yoffset < diff->MCU_rows_per_iMCU_row;
176 yoffset++) {
178 /* Process restart marker if needed; may have to suspend */
179 if (cinfo->restart_interval) {
180 if (diff->restart_rows_to_go == 0)
181 if (!process_restart(cinfo))
182 return JPEG_SUSPENDED;
185 MCU_col_num = diff->MCU_ctr;
186 /* Try to fetch an MCU row (or remaining portion of suspended MCU row). */
187 MCU_count =
188 (*cinfo->entropy->decode_mcus) (cinfo,
189 diff->diff_buf, yoffset, MCU_col_num,
190 cinfo->MCUs_per_row - MCU_col_num);
191 if (MCU_count != cinfo->MCUs_per_row - MCU_col_num) {
192 /* Suspension forced; update state counters and exit */
193 diff->MCU_vert_offset = yoffset;
194 diff->MCU_ctr += MCU_count;
195 return JPEG_SUSPENDED;
198 /* Account for restart interval (no-op if not using restarts) */
199 if (cinfo->restart_interval)
200 diff->restart_rows_to_go--;
202 /* Completed an MCU row, but perhaps not an iMCU row */
203 diff->MCU_ctr = 0;
207 * Undifference and scale each scanline of the disassembled MCU row
208 * separately. We do not process dummy samples at the end of a scanline
209 * or dummy rows at the end of the image.
211 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
212 compptr = cinfo->cur_comp_info[ci];
213 compi = compptr->component_index;
214 for (row = 0, prev_row = compptr->v_samp_factor - 1;
215 row < (cinfo->input_iMCU_row == last_iMCU_row ?
216 compptr->last_row_height : compptr->v_samp_factor);
217 prev_row = row, row++) {
218 (*losslessd->predict_undifference[compi])
219 (cinfo, compi, diff->diff_buf[compi][row],
220 diff->undiff_buf[compi][prev_row], diff->undiff_buf[compi][row],
221 compptr->width_in_blocks);
222 (*losslessd->scaler_scale) (cinfo, diff->undiff_buf[compi][row],
223 output_buf[compi][row],
224 compptr->width_in_blocks);
228 /* Completed the iMCU row, advance counters for next one.
230 * NB: output_data will increment output_iMCU_row.
231 * This counter is not needed for the single-pass case
232 * or the input side of the multi-pass case.
234 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
235 start_iMCU_row(cinfo);
236 return JPEG_ROW_COMPLETED;
238 /* Completed the scan */
239 (*cinfo->inputctl->finish_input_pass) (cinfo);
240 return JPEG_SCAN_COMPLETED;
245 * Dummy consume-input routine for single-pass operation.
248 METHODDEF(int)
249 dummy_consume_data(j_decompress_ptr cinfo)
251 return JPEG_SUSPENDED; /* Always indicate nothing was done */
255 #ifdef D_MULTISCAN_FILES_SUPPORTED
258 * Consume input data and store it in the full-image sample buffer.
259 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
260 * ie, v_samp_factor rows for each component in the scan.
261 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
264 METHODDEF(int)
265 consume_data(j_decompress_ptr cinfo)
267 my_diff_ptr diff = (my_diff_ptr)cinfo->coef;
268 int ci, compi;
269 _JSAMPARRAY buffer[MAX_COMPS_IN_SCAN];
270 jpeg_component_info *compptr;
272 /* Align the virtual buffers for the components used in this scan. */
273 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
274 compptr = cinfo->cur_comp_info[ci];
275 compi = compptr->component_index;
276 buffer[compi] = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
277 ((j_common_ptr)cinfo, diff->whole_image[compi],
278 cinfo->input_iMCU_row * compptr->v_samp_factor,
279 (JDIMENSION)compptr->v_samp_factor, TRUE);
282 return decompress_data(cinfo, buffer);
287 * Output some data from the full-image sample buffer in the multi-pass case.
288 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
289 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
291 * NB: output_buf contains a plane for each component in image.
294 METHODDEF(int)
295 output_data(j_decompress_ptr cinfo, _JSAMPIMAGE output_buf)
297 my_diff_ptr diff = (my_diff_ptr)cinfo->coef;
298 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
299 int ci, samp_rows, row;
300 _JSAMPARRAY buffer;
301 jpeg_component_info *compptr;
303 /* Force some input to be done if we are getting ahead of the input. */
304 while (cinfo->input_scan_number < cinfo->output_scan_number ||
305 (cinfo->input_scan_number == cinfo->output_scan_number &&
306 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
307 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
308 return JPEG_SUSPENDED;
311 /* OK, output from the virtual arrays. */
312 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
313 ci++, compptr++) {
314 /* Align the virtual buffer for this component. */
315 buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
316 ((j_common_ptr)cinfo, diff->whole_image[ci],
317 cinfo->output_iMCU_row * compptr->v_samp_factor,
318 (JDIMENSION)compptr->v_samp_factor, FALSE);
320 if (cinfo->output_iMCU_row < last_iMCU_row)
321 samp_rows = compptr->v_samp_factor;
322 else {
323 /* NB: can't use last_row_height here; it is input-side-dependent! */
324 samp_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
325 if (samp_rows == 0) samp_rows = compptr->v_samp_factor;
328 for (row = 0; row < samp_rows; row++) {
329 memcpy(output_buf[ci][row], buffer[row],
330 compptr->width_in_blocks * sizeof(_JSAMPLE));
334 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
335 return JPEG_ROW_COMPLETED;
336 return JPEG_SCAN_COMPLETED;
339 #endif /* D_MULTISCAN_FILES_SUPPORTED */
343 * Initialize difference buffer controller.
346 GLOBAL(void)
347 _jinit_d_diff_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
349 my_diff_ptr diff;
350 int ci;
351 jpeg_component_info *compptr;
353 diff = (my_diff_ptr)
354 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
355 sizeof(my_diff_controller));
356 cinfo->coef = (struct jpeg_d_coef_controller *)diff;
357 diff->pub.start_input_pass = start_input_pass;
358 diff->pub.start_output_pass = start_output_pass;
360 /* Create the [un]difference buffers. */
361 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
362 ci++, compptr++) {
363 diff->diff_buf[ci] =
364 ALLOC_DARRAY(JPOOL_IMAGE,
365 (JDIMENSION)jround_up((long)compptr->width_in_blocks,
366 (long)compptr->h_samp_factor),
367 (JDIMENSION)compptr->v_samp_factor);
368 diff->undiff_buf[ci] =
369 ALLOC_DARRAY(JPOOL_IMAGE,
370 (JDIMENSION)jround_up((long)compptr->width_in_blocks,
371 (long)compptr->h_samp_factor),
372 (JDIMENSION)compptr->v_samp_factor);
375 if (need_full_buffer) {
376 #ifdef D_MULTISCAN_FILES_SUPPORTED
377 /* Allocate a full-image virtual array for each component. */
378 int access_rows;
380 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
381 ci++, compptr++) {
382 access_rows = compptr->v_samp_factor;
383 diff->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
384 ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
385 (JDIMENSION)jround_up((long)compptr->width_in_blocks,
386 (long)compptr->h_samp_factor),
387 (JDIMENSION)jround_up((long)compptr->height_in_blocks,
388 (long)compptr->v_samp_factor),
389 (JDIMENSION)access_rows);
391 diff->pub.consume_data = consume_data;
392 diff->pub._decompress_data = output_data;
393 #else
394 ERREXIT(cinfo, JERR_NOT_COMPILED);
395 #endif
396 } else {
397 diff->pub.consume_data = dummy_consume_data;
398 diff->pub._decompress_data = decompress_data;
399 diff->whole_image[0] = NULL; /* flag for no virtual arrays */
403 #endif /* D_LOSSLESS_SUPPORTED */