Path: blob/master/thirdparty/libjpeg-turbo/src/jdmainct.c
9904 views
/*1* jdmainct.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1996, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright (C) 2010, 2016, 2022, 2024, D. R. Commander.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains the main buffer controller for decompression.11* The main buffer lies between the JPEG decompressor proper and the12* post-processor; it holds downsampled data in the JPEG colorspace.13*14* Note that this code is bypassed in raw-data mode, since the application15* supplies the equivalent of the main buffer in that case.16*/1718#include "jinclude.h"19#include "jdmainct.h"202122#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)2324/*25* In the current system design, the main buffer need never be a full-image26* buffer; any full-height buffers will be found inside the coefficient,27* difference, or postprocessing controllers. Nonetheless, the main controller28* is not trivial. Its responsibility is to provide context rows for29* upsampling/rescaling, and doing this in an efficient fashion is a bit30* tricky.31*32* Postprocessor input data is counted in "row groups". A row group33* is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)34* sample rows of each component. (We require DCT_scaled_size values to be35* chosen such that these numbers are integers. In practice DCT_scaled_size36* values will likely be powers of two, so we actually have the stronger37* condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)38* Upsampling will typically produce max_v_samp_factor pixel rows from each39* row group (times any additional scale factor that the upsampler is40* applying).41*42* The coefficient or difference controller will deliver data to us one iMCU43* row at a time; each iMCU row contains v_samp_factor * DCT_scaled_size sample44* rows, or exactly min_DCT_scaled_size row groups. (This amount of data45* corresponds to one row of MCUs when the image is fully interleaved.) Note46* that the number of sample rows varies across components, but the number of47* row groups does not. Some garbage sample rows may be included in the last48* iMCU row at the bottom of the image.49*50* Depending on the vertical scaling algorithm used, the upsampler may need51* access to the sample row(s) above and below its current input row group.52* The upsampler is required to set need_context_rows TRUE at global selection53* time if so. When need_context_rows is FALSE, this controller can simply54* obtain one iMCU row at a time from the coefficient or difference controller55* and dole it out as row groups to the postprocessor.56*57* When need_context_rows is TRUE, this controller guarantees that the buffer58* passed to postprocessing contains at least one row group's worth of samples59* above and below the row group(s) being processed. Note that the context60* rows "above" the first passed row group appear at negative row offsets in61* the passed buffer. At the top and bottom of the image, the required62* context rows are manufactured by duplicating the first or last real sample63* row; this avoids having special cases in the upsampling inner loops.64*65* The amount of context is fixed at one row group just because that's a66* convenient number for this controller to work with. The existing67* upsamplers really only need one sample row of context. An upsampler68* supporting arbitrary output rescaling might wish for more than one row69* group of context when shrinking the image; tough, we don't handle that.70* (This is justified by the assumption that downsizing will be handled mostly71* by adjusting the DCT_scaled_size values, so that the actual scale factor at72* the upsample step needn't be much less than one.)73*74* To provide the desired context, we have to retain the last two row groups75* of one iMCU row while reading in the next iMCU row. (The last row group76* can't be processed until we have another row group for its below-context,77* and so we have to save the next-to-last group too for its above-context.)78* We could do this most simply by copying data around in our buffer, but79* that'd be very slow. We can avoid copying any data by creating a rather80* strange pointer structure. Here's how it works. We allocate a workspace81* consisting of M+2 row groups (where M = min_DCT_scaled_size is the number82* of row groups per iMCU row). We create two sets of redundant pointers to83* the workspace. Labeling the physical row groups 0 to M+1, the synthesized84* pointer lists look like this:85* M+1 M-186* master pointer --> 0 master pointer --> 087* 1 188* ... ...89* M-3 M-390* M-2 M91* M-1 M+192* M M-293* M+1 M-194* 0 095* We read alternate iMCU rows using each master pointer; thus the last two96* row groups of the previous iMCU row remain un-overwritten in the workspace.97* The pointer lists are set up so that the required context rows appear to98* be adjacent to the proper places when we pass the pointer lists to the99* upsampler.100*101* The above pictures describe the normal state of the pointer lists.102* At top and bottom of the image, we diddle the pointer lists to duplicate103* the first or last sample row as necessary (this is cheaper than copying104* sample rows around).105*106* This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that107* situation each iMCU row provides only one row group so the buffering logic108* must be different (eg, we must read two iMCU rows before we can emit the109* first row group). For now, we simply do not support providing context110* rows when min_DCT_scaled_size is 1. That combination seems unlikely to111* be worth providing --- if someone wants a 1/8th-size preview, they probably112* want it quick and dirty, so a context-free upsampler is sufficient.113*/114115116/* Forward declarations */117METHODDEF(void) process_data_simple_main(j_decompress_ptr cinfo,118_JSAMPARRAY output_buf,119JDIMENSION *out_row_ctr,120JDIMENSION out_rows_avail);121METHODDEF(void) process_data_context_main(j_decompress_ptr cinfo,122_JSAMPARRAY output_buf,123JDIMENSION *out_row_ctr,124JDIMENSION out_rows_avail);125#ifdef QUANT_2PASS_SUPPORTED126METHODDEF(void) process_data_crank_post(j_decompress_ptr cinfo,127_JSAMPARRAY output_buf,128JDIMENSION *out_row_ctr,129JDIMENSION out_rows_avail);130#endif131132133LOCAL(void)134alloc_funny_pointers(j_decompress_ptr cinfo)135/* Allocate space for the funny pointer lists.136* This is done only once, not once per pass.137*/138{139my_main_ptr main_ptr = (my_main_ptr)cinfo->main;140int ci, rgroup;141int M = cinfo->_min_DCT_scaled_size;142jpeg_component_info *compptr;143_JSAMPARRAY xbuf;144145/* Get top-level space for component array pointers.146* We alloc both arrays with one call to save a few cycles.147*/148main_ptr->xbuffer[0] = (_JSAMPIMAGE)149(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,150cinfo->num_components * 2 *151sizeof(_JSAMPARRAY));152main_ptr->xbuffer[1] = main_ptr->xbuffer[0] + cinfo->num_components;153154for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;155ci++, compptr++) {156rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /157cinfo->_min_DCT_scaled_size; /* height of a row group of component */158/* Get space for pointer lists --- M+4 row groups in each list.159* We alloc both pointer lists with one call to save a few cycles.160*/161xbuf = (_JSAMPARRAY)162(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,1632 * (rgroup * (M + 4)) * sizeof(_JSAMPROW));164xbuf += rgroup; /* want one row group at negative offsets */165main_ptr->xbuffer[0][ci] = xbuf;166xbuf += rgroup * (M + 4);167main_ptr->xbuffer[1][ci] = xbuf;168}169}170171172LOCAL(void)173make_funny_pointers(j_decompress_ptr cinfo)174/* Create the funny pointer lists discussed in the comments above.175* The actual workspace is already allocated (in main_ptr->buffer),176* and the space for the pointer lists is allocated too.177* This routine just fills in the curiously ordered lists.178* This will be repeated at the beginning of each pass.179*/180{181my_main_ptr main_ptr = (my_main_ptr)cinfo->main;182int ci, i, rgroup;183int M = cinfo->_min_DCT_scaled_size;184jpeg_component_info *compptr;185_JSAMPARRAY buf, xbuf0, xbuf1;186187for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;188ci++, compptr++) {189rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /190cinfo->_min_DCT_scaled_size; /* height of a row group of component */191xbuf0 = main_ptr->xbuffer[0][ci];192xbuf1 = main_ptr->xbuffer[1][ci];193/* First copy the workspace pointers as-is */194buf = main_ptr->buffer[ci];195for (i = 0; i < rgroup * (M + 2); i++) {196xbuf0[i] = xbuf1[i] = buf[i];197}198/* In the second list, put the last four row groups in swapped order */199for (i = 0; i < rgroup * 2; i++) {200xbuf1[rgroup * (M - 2) + i] = buf[rgroup * M + i];201xbuf1[rgroup * M + i] = buf[rgroup * (M - 2) + i];202}203/* The wraparound pointers at top and bottom will be filled later204* (see set_wraparound_pointers, below). Initially we want the "above"205* pointers to duplicate the first actual data line. This only needs206* to happen in xbuffer[0].207*/208for (i = 0; i < rgroup; i++) {209xbuf0[i - rgroup] = xbuf0[0];210}211}212}213214215LOCAL(void)216set_bottom_pointers(j_decompress_ptr cinfo)217/* Change the pointer lists to duplicate the last sample row at the bottom218* of the image. whichptr indicates which xbuffer holds the final iMCU row.219* Also sets rowgroups_avail to indicate number of nondummy row groups in row.220*/221{222my_main_ptr main_ptr = (my_main_ptr)cinfo->main;223int ci, i, rgroup, iMCUheight, rows_left;224jpeg_component_info *compptr;225_JSAMPARRAY xbuf;226227for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;228ci++, compptr++) {229/* Count sample rows in one iMCU row and in one row group */230iMCUheight = compptr->v_samp_factor * compptr->_DCT_scaled_size;231rgroup = iMCUheight / cinfo->_min_DCT_scaled_size;232/* Count nondummy sample rows remaining for this component */233rows_left = (int)(compptr->downsampled_height % (JDIMENSION)iMCUheight);234if (rows_left == 0) rows_left = iMCUheight;235/* Count nondummy row groups. Should get same answer for each component,236* so we need only do it once.237*/238if (ci == 0) {239main_ptr->rowgroups_avail = (JDIMENSION)((rows_left - 1) / rgroup + 1);240}241/* Duplicate the last real sample row rgroup*2 times; this pads out the242* last partial rowgroup and ensures at least one full rowgroup of context.243*/244xbuf = main_ptr->xbuffer[main_ptr->whichptr][ci];245for (i = 0; i < rgroup * 2; i++) {246xbuf[rows_left + i] = xbuf[rows_left - 1];247}248}249}250251252/*253* Initialize for a processing pass.254*/255256METHODDEF(void)257start_pass_main(j_decompress_ptr cinfo, J_BUF_MODE pass_mode)258{259my_main_ptr main_ptr = (my_main_ptr)cinfo->main;260261switch (pass_mode) {262case JBUF_PASS_THRU:263if (cinfo->upsample->need_context_rows) {264main_ptr->pub._process_data = process_data_context_main;265make_funny_pointers(cinfo); /* Create the xbuffer[] lists */266main_ptr->whichptr = 0; /* Read first iMCU row into xbuffer[0] */267main_ptr->context_state = CTX_PREPARE_FOR_IMCU;268main_ptr->iMCU_row_ctr = 0;269} else {270/* Simple case with no context needed */271main_ptr->pub._process_data = process_data_simple_main;272}273main_ptr->buffer_full = FALSE; /* Mark buffer empty */274main_ptr->rowgroup_ctr = 0;275break;276#ifdef QUANT_2PASS_SUPPORTED277case JBUF_CRANK_DEST:278/* For last pass of 2-pass quantization, just crank the postprocessor */279main_ptr->pub._process_data = process_data_crank_post;280break;281#endif282default:283ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);284break;285}286}287288289/*290* Process some data.291* This handles the simple case where no context is required.292*/293294METHODDEF(void)295process_data_simple_main(j_decompress_ptr cinfo, _JSAMPARRAY output_buf,296JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)297{298my_main_ptr main_ptr = (my_main_ptr)cinfo->main;299JDIMENSION rowgroups_avail;300301/* Read input data if we haven't filled the main buffer yet */302if (!main_ptr->buffer_full) {303if (!(*cinfo->coef->_decompress_data) (cinfo, main_ptr->buffer))304return; /* suspension forced, can do nothing more */305main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */306}307308/* There are always min_DCT_scaled_size row groups in an iMCU row. */309rowgroups_avail = (JDIMENSION)cinfo->_min_DCT_scaled_size;310/* Note: at the bottom of the image, we may pass extra garbage row groups311* to the postprocessor. The postprocessor has to check for bottom312* of image anyway (at row resolution), so no point in us doing it too.313*/314315/* Feed the postprocessor */316(*cinfo->post->_post_process_data) (cinfo, main_ptr->buffer,317&main_ptr->rowgroup_ctr, rowgroups_avail,318output_buf, out_row_ctr, out_rows_avail);319320/* Has postprocessor consumed all the data yet? If so, mark buffer empty */321if (main_ptr->rowgroup_ctr >= rowgroups_avail) {322main_ptr->buffer_full = FALSE;323main_ptr->rowgroup_ctr = 0;324}325}326327328/*329* Process some data.330* This handles the case where context rows must be provided.331*/332333METHODDEF(void)334process_data_context_main(j_decompress_ptr cinfo, _JSAMPARRAY output_buf,335JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)336{337my_main_ptr main_ptr = (my_main_ptr)cinfo->main;338339/* Read input data if we haven't filled the main buffer yet */340if (!main_ptr->buffer_full) {341if (!(*cinfo->coef->_decompress_data) (cinfo,342main_ptr->xbuffer[main_ptr->whichptr]))343return; /* suspension forced, can do nothing more */344main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */345main_ptr->iMCU_row_ctr++; /* count rows received */346}347348/* Postprocessor typically will not swallow all the input data it is handed349* in one call (due to filling the output buffer first). Must be prepared350* to exit and restart. This switch lets us keep track of how far we got.351* Note that each case falls through to the next on successful completion.352*/353switch (main_ptr->context_state) {354case CTX_POSTPONED_ROW:355/* Call postprocessor using previously set pointers for postponed row */356(*cinfo->post->_post_process_data) (cinfo,357main_ptr->xbuffer[main_ptr->whichptr],358&main_ptr->rowgroup_ctr,359main_ptr->rowgroups_avail, output_buf,360out_row_ctr, out_rows_avail);361if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail)362return; /* Need to suspend */363main_ptr->context_state = CTX_PREPARE_FOR_IMCU;364if (*out_row_ctr >= out_rows_avail)365return; /* Postprocessor exactly filled output buf */366FALLTHROUGH /*FALLTHROUGH*/367case CTX_PREPARE_FOR_IMCU:368/* Prepare to process first M-1 row groups of this iMCU row */369main_ptr->rowgroup_ctr = 0;370main_ptr->rowgroups_avail = (JDIMENSION)(cinfo->_min_DCT_scaled_size - 1);371/* Check for bottom of image: if so, tweak pointers to "duplicate"372* the last sample row, and adjust rowgroups_avail to ignore padding rows.373*/374if (main_ptr->iMCU_row_ctr == cinfo->total_iMCU_rows)375set_bottom_pointers(cinfo);376main_ptr->context_state = CTX_PROCESS_IMCU;377FALLTHROUGH /*FALLTHROUGH*/378case CTX_PROCESS_IMCU:379/* Call postprocessor using previously set pointers */380(*cinfo->post->_post_process_data) (cinfo,381main_ptr->xbuffer[main_ptr->whichptr],382&main_ptr->rowgroup_ctr,383main_ptr->rowgroups_avail, output_buf,384out_row_ctr, out_rows_avail);385if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail)386return; /* Need to suspend */387/* After the first iMCU, change wraparound pointers to normal state */388if (main_ptr->iMCU_row_ctr == 1)389set_wraparound_pointers(cinfo);390/* Prepare to load new iMCU row using other xbuffer list */391main_ptr->whichptr ^= 1; /* 0=>1 or 1=>0 */392main_ptr->buffer_full = FALSE;393/* Still need to process last row group of this iMCU row, */394/* which is saved at index M+1 of the other xbuffer */395main_ptr->rowgroup_ctr = (JDIMENSION)(cinfo->_min_DCT_scaled_size + 1);396main_ptr->rowgroups_avail = (JDIMENSION)(cinfo->_min_DCT_scaled_size + 2);397main_ptr->context_state = CTX_POSTPONED_ROW;398}399}400401402/*403* Process some data.404* Final pass of two-pass quantization: just call the postprocessor.405* Source data will be the postprocessor controller's internal buffer.406*/407408#ifdef QUANT_2PASS_SUPPORTED409410METHODDEF(void)411process_data_crank_post(j_decompress_ptr cinfo, _JSAMPARRAY output_buf,412JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)413{414(*cinfo->post->_post_process_data) (cinfo, (_JSAMPIMAGE)NULL,415(JDIMENSION *)NULL, (JDIMENSION)0,416output_buf, out_row_ctr, out_rows_avail);417}418419#endif /* QUANT_2PASS_SUPPORTED */420421422/*423* Initialize main buffer controller.424*/425426GLOBAL(void)427_jinit_d_main_controller(j_decompress_ptr cinfo, boolean need_full_buffer)428{429my_main_ptr main_ptr;430int ci, rgroup, ngroups;431jpeg_component_info *compptr;432433#ifdef D_LOSSLESS_SUPPORTED434if (cinfo->master->lossless) {435#if BITS_IN_JSAMPLE == 8436if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)437#else438if (cinfo->data_precision > BITS_IN_JSAMPLE ||439cinfo->data_precision < BITS_IN_JSAMPLE - 3)440#endif441ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);442} else443#endif444{445if (cinfo->data_precision != BITS_IN_JSAMPLE)446ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);447}448449main_ptr = (my_main_ptr)450(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,451sizeof(my_main_controller));452cinfo->main = (struct jpeg_d_main_controller *)main_ptr;453main_ptr->pub.start_pass = start_pass_main;454455if (need_full_buffer) /* shouldn't happen */456ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);457458/* Allocate the workspace.459* ngroups is the number of row groups we need.460*/461if (cinfo->upsample->need_context_rows) {462if (cinfo->_min_DCT_scaled_size < 2) /* unsupported, see comments above */463ERREXIT(cinfo, JERR_NOTIMPL);464alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */465ngroups = cinfo->_min_DCT_scaled_size + 2;466} else {467ngroups = cinfo->_min_DCT_scaled_size;468}469470for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;471ci++, compptr++) {472rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /473cinfo->_min_DCT_scaled_size; /* height of a row group of component */474main_ptr->buffer[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)475((j_common_ptr)cinfo, JPOOL_IMAGE,476compptr->width_in_blocks * compptr->_DCT_scaled_size,477(JDIMENSION)(rgroup * ngroups));478}479}480481#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */482483484