Path: blob/master/3rdparty/libjpeg-turbo/src/jdmainct.c
16337 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, 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/*23* In the current system design, the main buffer need never be a full-image24* buffer; any full-height buffers will be found inside the coefficient or25* postprocessing controllers. Nonetheless, the main controller is not26* trivial. Its responsibility is to provide context rows for upsampling/27* rescaling, and doing this in an efficient fashion is a bit tricky.28*29* Postprocessor input data is counted in "row groups". A row group30* is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)31* sample rows of each component. (We require DCT_scaled_size values to be32* chosen such that these numbers are integers. In practice DCT_scaled_size33* values will likely be powers of two, so we actually have the stronger34* condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)35* Upsampling will typically produce max_v_samp_factor pixel rows from each36* row group (times any additional scale factor that the upsampler is37* applying).38*39* The coefficient controller will deliver data to us one iMCU row at a time;40* each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or41* exactly min_DCT_scaled_size row groups. (This amount of data corresponds42* to one row of MCUs when the image is fully interleaved.) Note that the43* number of sample rows varies across components, but the number of row44* groups does not. Some garbage sample rows may be included in the last iMCU45* row at the bottom of the image.46*47* Depending on the vertical scaling algorithm used, the upsampler may need48* access to the sample row(s) above and below its current input row group.49* The upsampler is required to set need_context_rows TRUE at global selection50* time if so. When need_context_rows is FALSE, this controller can simply51* obtain one iMCU row at a time from the coefficient controller and dole it52* out as row groups to the postprocessor.53*54* When need_context_rows is TRUE, this controller guarantees that the buffer55* passed to postprocessing contains at least one row group's worth of samples56* above and below the row group(s) being processed. Note that the context57* rows "above" the first passed row group appear at negative row offsets in58* the passed buffer. At the top and bottom of the image, the required59* context rows are manufactured by duplicating the first or last real sample60* row; this avoids having special cases in the upsampling inner loops.61*62* The amount of context is fixed at one row group just because that's a63* convenient number for this controller to work with. The existing64* upsamplers really only need one sample row of context. An upsampler65* supporting arbitrary output rescaling might wish for more than one row66* group of context when shrinking the image; tough, we don't handle that.67* (This is justified by the assumption that downsizing will be handled mostly68* by adjusting the DCT_scaled_size values, so that the actual scale factor at69* the upsample step needn't be much less than one.)70*71* To provide the desired context, we have to retain the last two row groups72* of one iMCU row while reading in the next iMCU row. (The last row group73* can't be processed until we have another row group for its below-context,74* and so we have to save the next-to-last group too for its above-context.)75* We could do this most simply by copying data around in our buffer, but76* that'd be very slow. We can avoid copying any data by creating a rather77* strange pointer structure. Here's how it works. We allocate a workspace78* consisting of M+2 row groups (where M = min_DCT_scaled_size is the number79* of row groups per iMCU row). We create two sets of redundant pointers to80* the workspace. Labeling the physical row groups 0 to M+1, the synthesized81* pointer lists look like this:82* M+1 M-183* master pointer --> 0 master pointer --> 084* 1 185* ... ...86* M-3 M-387* M-2 M88* M-1 M+189* M M-290* M+1 M-191* 0 092* We read alternate iMCU rows using each master pointer; thus the last two93* row groups of the previous iMCU row remain un-overwritten in the workspace.94* The pointer lists are set up so that the required context rows appear to95* be adjacent to the proper places when we pass the pointer lists to the96* upsampler.97*98* The above pictures describe the normal state of the pointer lists.99* At top and bottom of the image, we diddle the pointer lists to duplicate100* the first or last sample row as necessary (this is cheaper than copying101* sample rows around).102*103* This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that104* situation each iMCU row provides only one row group so the buffering logic105* must be different (eg, we must read two iMCU rows before we can emit the106* first row group). For now, we simply do not support providing context107* rows when min_DCT_scaled_size is 1. That combination seems unlikely to108* be worth providing --- if someone wants a 1/8th-size preview, they probably109* want it quick and dirty, so a context-free upsampler is sufficient.110*/111112113/* Forward declarations */114METHODDEF(void) process_data_simple_main115(j_decompress_ptr cinfo, JSAMPARRAY output_buf,116JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);117METHODDEF(void) process_data_context_main118(j_decompress_ptr cinfo, JSAMPARRAY output_buf,119JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);120#ifdef QUANT_2PASS_SUPPORTED121METHODDEF(void) process_data_crank_post122(j_decompress_ptr cinfo, JSAMPARRAY output_buf,123JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail);124#endif125126127LOCAL(void)128alloc_funny_pointers (j_decompress_ptr cinfo)129/* Allocate space for the funny pointer lists.130* This is done only once, not once per pass.131*/132{133my_main_ptr main_ptr = (my_main_ptr) cinfo->main;134int ci, rgroup;135int M = cinfo->_min_DCT_scaled_size;136jpeg_component_info *compptr;137JSAMPARRAY xbuf;138139/* Get top-level space for component array pointers.140* We alloc both arrays with one call to save a few cycles.141*/142main_ptr->xbuffer[0] = (JSAMPIMAGE)143(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,144cinfo->num_components * 2 * sizeof(JSAMPARRAY));145main_ptr->xbuffer[1] = main_ptr->xbuffer[0] + cinfo->num_components;146147for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;148ci++, compptr++) {149rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /150cinfo->_min_DCT_scaled_size; /* height of a row group of component */151/* Get space for pointer lists --- M+4 row groups in each list.152* We alloc both pointer lists with one call to save a few cycles.153*/154xbuf = (JSAMPARRAY)155(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,1562 * (rgroup * (M + 4)) * sizeof(JSAMPROW));157xbuf += rgroup; /* want one row group at negative offsets */158main_ptr->xbuffer[0][ci] = xbuf;159xbuf += rgroup * (M + 4);160main_ptr->xbuffer[1][ci] = xbuf;161}162}163164165LOCAL(void)166make_funny_pointers (j_decompress_ptr cinfo)167/* Create the funny pointer lists discussed in the comments above.168* The actual workspace is already allocated (in main_ptr->buffer),169* and the space for the pointer lists is allocated too.170* This routine just fills in the curiously ordered lists.171* This will be repeated at the beginning of each pass.172*/173{174my_main_ptr main_ptr = (my_main_ptr) cinfo->main;175int ci, i, rgroup;176int M = cinfo->_min_DCT_scaled_size;177jpeg_component_info *compptr;178JSAMPARRAY buf, xbuf0, xbuf1;179180for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;181ci++, compptr++) {182rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /183cinfo->_min_DCT_scaled_size; /* height of a row group of component */184xbuf0 = main_ptr->xbuffer[0][ci];185xbuf1 = main_ptr->xbuffer[1][ci];186/* First copy the workspace pointers as-is */187buf = main_ptr->buffer[ci];188for (i = 0; i < rgroup * (M + 2); i++) {189xbuf0[i] = xbuf1[i] = buf[i];190}191/* In the second list, put the last four row groups in swapped order */192for (i = 0; i < rgroup * 2; i++) {193xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];194xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];195}196/* The wraparound pointers at top and bottom will be filled later197* (see set_wraparound_pointers, below). Initially we want the "above"198* pointers to duplicate the first actual data line. This only needs199* to happen in xbuffer[0].200*/201for (i = 0; i < rgroup; i++) {202xbuf0[i - rgroup] = xbuf0[0];203}204}205}206207208LOCAL(void)209set_bottom_pointers (j_decompress_ptr cinfo)210/* Change the pointer lists to duplicate the last sample row at the bottom211* of the image. whichptr indicates which xbuffer holds the final iMCU row.212* Also sets rowgroups_avail to indicate number of nondummy row groups in row.213*/214{215my_main_ptr main_ptr = (my_main_ptr) cinfo->main;216int ci, i, rgroup, iMCUheight, rows_left;217jpeg_component_info *compptr;218JSAMPARRAY xbuf;219220for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;221ci++, compptr++) {222/* Count sample rows in one iMCU row and in one row group */223iMCUheight = compptr->v_samp_factor * compptr->_DCT_scaled_size;224rgroup = iMCUheight / cinfo->_min_DCT_scaled_size;225/* Count nondummy sample rows remaining for this component */226rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);227if (rows_left == 0) rows_left = iMCUheight;228/* Count nondummy row groups. Should get same answer for each component,229* so we need only do it once.230*/231if (ci == 0) {232main_ptr->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);233}234/* Duplicate the last real sample row rgroup*2 times; this pads out the235* last partial rowgroup and ensures at least one full rowgroup of context.236*/237xbuf = main_ptr->xbuffer[main_ptr->whichptr][ci];238for (i = 0; i < rgroup * 2; i++) {239xbuf[rows_left + i] = xbuf[rows_left-1];240}241}242}243244245/*246* Initialize for a processing pass.247*/248249METHODDEF(void)250start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)251{252my_main_ptr main_ptr = (my_main_ptr) cinfo->main;253254switch (pass_mode) {255case JBUF_PASS_THRU:256if (cinfo->upsample->need_context_rows) {257main_ptr->pub.process_data = process_data_context_main;258make_funny_pointers(cinfo); /* Create the xbuffer[] lists */259main_ptr->whichptr = 0; /* Read first iMCU row into xbuffer[0] */260main_ptr->context_state = CTX_PREPARE_FOR_IMCU;261main_ptr->iMCU_row_ctr = 0;262} else {263/* Simple case with no context needed */264main_ptr->pub.process_data = process_data_simple_main;265}266main_ptr->buffer_full = FALSE; /* Mark buffer empty */267main_ptr->rowgroup_ctr = 0;268break;269#ifdef QUANT_2PASS_SUPPORTED270case JBUF_CRANK_DEST:271/* For last pass of 2-pass quantization, just crank the postprocessor */272main_ptr->pub.process_data = process_data_crank_post;273break;274#endif275default:276ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);277break;278}279}280281282/*283* Process some data.284* This handles the simple case where no context is required.285*/286287METHODDEF(void)288process_data_simple_main (j_decompress_ptr cinfo,289JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,290JDIMENSION out_rows_avail)291{292my_main_ptr main_ptr = (my_main_ptr) cinfo->main;293JDIMENSION rowgroups_avail;294295/* Read input data if we haven't filled the main buffer yet */296if (! main_ptr->buffer_full) {297if (! (*cinfo->coef->decompress_data) (cinfo, main_ptr->buffer))298return; /* suspension forced, can do nothing more */299main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */300}301302/* There are always min_DCT_scaled_size row groups in an iMCU row. */303rowgroups_avail = (JDIMENSION) cinfo->_min_DCT_scaled_size;304/* Note: at the bottom of the image, we may pass extra garbage row groups305* to the postprocessor. The postprocessor has to check for bottom306* of image anyway (at row resolution), so no point in us doing it too.307*/308309/* Feed the postprocessor */310(*cinfo->post->post_process_data) (cinfo, main_ptr->buffer,311&main_ptr->rowgroup_ctr, rowgroups_avail,312output_buf, out_row_ctr, out_rows_avail);313314/* Has postprocessor consumed all the data yet? If so, mark buffer empty */315if (main_ptr->rowgroup_ctr >= rowgroups_avail) {316main_ptr->buffer_full = FALSE;317main_ptr->rowgroup_ctr = 0;318}319}320321322/*323* Process some data.324* This handles the case where context rows must be provided.325*/326327METHODDEF(void)328process_data_context_main (j_decompress_ptr cinfo,329JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,330JDIMENSION out_rows_avail)331{332my_main_ptr main_ptr = (my_main_ptr) cinfo->main;333334/* Read input data if we haven't filled the main buffer yet */335if (! main_ptr->buffer_full) {336if (! (*cinfo->coef->decompress_data) (cinfo,337main_ptr->xbuffer[main_ptr->whichptr]))338return; /* suspension forced, can do nothing more */339main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */340main_ptr->iMCU_row_ctr++; /* count rows received */341}342343/* Postprocessor typically will not swallow all the input data it is handed344* in one call (due to filling the output buffer first). Must be prepared345* to exit and restart. This switch lets us keep track of how far we got.346* Note that each case falls through to the next on successful completion.347*/348switch (main_ptr->context_state) {349case CTX_POSTPONED_ROW:350/* Call postprocessor using previously set pointers for postponed row */351(*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr],352&main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail,353output_buf, out_row_ctr, out_rows_avail);354if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail)355return; /* Need to suspend */356main_ptr->context_state = CTX_PREPARE_FOR_IMCU;357if (*out_row_ctr >= out_rows_avail)358return; /* Postprocessor exactly filled output buf */359/*FALLTHROUGH*/360case CTX_PREPARE_FOR_IMCU:361/* Prepare to process first M-1 row groups of this iMCU row */362main_ptr->rowgroup_ctr = 0;363main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size - 1);364/* Check for bottom of image: if so, tweak pointers to "duplicate"365* the last sample row, and adjust rowgroups_avail to ignore padding rows.366*/367if (main_ptr->iMCU_row_ctr == cinfo->total_iMCU_rows)368set_bottom_pointers(cinfo);369main_ptr->context_state = CTX_PROCESS_IMCU;370/*FALLTHROUGH*/371case CTX_PROCESS_IMCU:372/* Call postprocessor using previously set pointers */373(*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr],374&main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail,375output_buf, out_row_ctr, out_rows_avail);376if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail)377return; /* Need to suspend */378/* After the first iMCU, change wraparound pointers to normal state */379if (main_ptr->iMCU_row_ctr == 1)380set_wraparound_pointers(cinfo);381/* Prepare to load new iMCU row using other xbuffer list */382main_ptr->whichptr ^= 1; /* 0=>1 or 1=>0 */383main_ptr->buffer_full = FALSE;384/* Still need to process last row group of this iMCU row, */385/* which is saved at index M+1 of the other xbuffer */386main_ptr->rowgroup_ctr = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 1);387main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 2);388main_ptr->context_state = CTX_POSTPONED_ROW;389}390}391392393/*394* Process some data.395* Final pass of two-pass quantization: just call the postprocessor.396* Source data will be the postprocessor controller's internal buffer.397*/398399#ifdef QUANT_2PASS_SUPPORTED400401METHODDEF(void)402process_data_crank_post (j_decompress_ptr cinfo,403JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,404JDIMENSION out_rows_avail)405{406(*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,407(JDIMENSION *) NULL, (JDIMENSION) 0,408output_buf, out_row_ctr, out_rows_avail);409}410411#endif /* QUANT_2PASS_SUPPORTED */412413414/*415* Initialize main buffer controller.416*/417418GLOBAL(void)419jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)420{421my_main_ptr main_ptr;422int ci, rgroup, ngroups;423jpeg_component_info *compptr;424425main_ptr = (my_main_ptr)426(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,427sizeof(my_main_controller));428cinfo->main = (struct jpeg_d_main_controller *) main_ptr;429main_ptr->pub.start_pass = start_pass_main;430431if (need_full_buffer) /* shouldn't happen */432ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);433434/* Allocate the workspace.435* ngroups is the number of row groups we need.436*/437if (cinfo->upsample->need_context_rows) {438if (cinfo->_min_DCT_scaled_size < 2) /* unsupported, see comments above */439ERREXIT(cinfo, JERR_NOTIMPL);440alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */441ngroups = cinfo->_min_DCT_scaled_size + 2;442} else {443ngroups = cinfo->_min_DCT_scaled_size;444}445446for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;447ci++, compptr++) {448rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /449cinfo->_min_DCT_scaled_size; /* height of a row group of component */450main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray)451((j_common_ptr) cinfo, JPOOL_IMAGE,452compptr->width_in_blocks * compptr->_DCT_scaled_size,453(JDIMENSION) (rgroup * ngroups));454}455}456457458