Path: blob/master/thirdparty/libjpeg-turbo/src/jdcoefct.c
9904 views
/*1* jdcoefct.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1997, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB7* Copyright (C) 2010, 2015-2016, 2019-2020, 2022-2023, D. R. Commander.8* Copyright (C) 2015, 2020, Google, Inc.9* For conditions of distribution and use, see the accompanying README.ijg10* file.11*12* This file contains the coefficient buffer controller for decompression.13* This controller is the top level of the lossy JPEG decompressor proper.14* The coefficient buffer lies between entropy decoding and inverse-DCT steps.15*16* In buffered-image mode, this controller is the interface between17* input-oriented processing and output-oriented processing.18* Also, the input side (only) is used when reading a file for transcoding.19*/2021#include "jinclude.h"22#include "jdcoefct.h"23#include "jpegapicomp.h"24#include "jsamplecomp.h"252627/* Forward declarations */28METHODDEF(int) decompress_onepass(j_decompress_ptr cinfo,29_JSAMPIMAGE output_buf);30#ifdef D_MULTISCAN_FILES_SUPPORTED31METHODDEF(int) decompress_data(j_decompress_ptr cinfo, _JSAMPIMAGE output_buf);32#endif33#ifdef BLOCK_SMOOTHING_SUPPORTED34LOCAL(boolean) smoothing_ok(j_decompress_ptr cinfo);35METHODDEF(int) decompress_smooth_data(j_decompress_ptr cinfo,36_JSAMPIMAGE output_buf);37#endif383940/*41* Initialize for an input processing pass.42*/4344METHODDEF(void)45start_input_pass(j_decompress_ptr cinfo)46{47cinfo->input_iMCU_row = 0;48start_iMCU_row(cinfo);49}505152/*53* Initialize for an output processing pass.54*/5556METHODDEF(void)57start_output_pass(j_decompress_ptr cinfo)58{59#ifdef BLOCK_SMOOTHING_SUPPORTED60my_coef_ptr coef = (my_coef_ptr)cinfo->coef;6162/* If multipass, check to see whether to use block smoothing on this pass */63if (coef->pub.coef_arrays != NULL) {64if (cinfo->do_block_smoothing && smoothing_ok(cinfo))65coef->pub._decompress_data = decompress_smooth_data;66else67coef->pub._decompress_data = decompress_data;68}69#endif70cinfo->output_iMCU_row = 0;71}727374/*75* Decompress and return some data in the single-pass case.76* Always attempts to emit one fully interleaved MCU row ("iMCU" row).77* Input and output must run in lockstep since we have only a one-MCU buffer.78* Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.79*80* NB: output_buf contains a plane for each component in image,81* which we index according to the component's SOF position.82*/8384METHODDEF(int)85decompress_onepass(j_decompress_ptr cinfo, _JSAMPIMAGE output_buf)86{87my_coef_ptr coef = (my_coef_ptr)cinfo->coef;88JDIMENSION MCU_col_num; /* index of current MCU within row */89JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;90JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;91int blkn, ci, xindex, yindex, yoffset, useful_width;92_JSAMPARRAY output_ptr;93JDIMENSION start_col, output_col;94jpeg_component_info *compptr;95_inverse_DCT_method_ptr inverse_DCT;9697/* Loop to process as much as one whole iMCU row */98for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;99yoffset++) {100for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;101MCU_col_num++) {102/* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */103jzero_far((void *)coef->MCU_buffer[0],104(size_t)(cinfo->blocks_in_MCU * sizeof(JBLOCK)));105if (!cinfo->entropy->insufficient_data)106cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;107if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {108/* Suspension forced; update state counters and exit */109coef->MCU_vert_offset = yoffset;110coef->MCU_ctr = MCU_col_num;111return JPEG_SUSPENDED;112}113114/* Only perform the IDCT on blocks that are contained within the desired115* cropping region.116*/117if (MCU_col_num >= cinfo->master->first_iMCU_col &&118MCU_col_num <= cinfo->master->last_iMCU_col) {119/* Determine where data should go in output_buf and do the IDCT thing.120* We skip dummy blocks at the right and bottom edges (but blkn gets121* incremented past them!). Note the inner loop relies on having122* allocated the MCU_buffer[] blocks sequentially.123*/124blkn = 0; /* index of current DCT block within MCU */125for (ci = 0; ci < cinfo->comps_in_scan; ci++) {126compptr = cinfo->cur_comp_info[ci];127/* Don't bother to IDCT an uninteresting component. */128if (!compptr->component_needed) {129blkn += compptr->MCU_blocks;130continue;131}132inverse_DCT = cinfo->idct->_inverse_DCT[compptr->component_index];133useful_width = (MCU_col_num < last_MCU_col) ?134compptr->MCU_width : compptr->last_col_width;135output_ptr = output_buf[compptr->component_index] +136yoffset * compptr->_DCT_scaled_size;137start_col = (MCU_col_num - cinfo->master->first_iMCU_col) *138compptr->MCU_sample_width;139for (yindex = 0; yindex < compptr->MCU_height; yindex++) {140if (cinfo->input_iMCU_row < last_iMCU_row ||141yoffset + yindex < compptr->last_row_height) {142output_col = start_col;143for (xindex = 0; xindex < useful_width; xindex++) {144(*inverse_DCT) (cinfo, compptr,145(JCOEFPTR)coef->MCU_buffer[blkn + xindex],146output_ptr, output_col);147output_col += compptr->_DCT_scaled_size;148}149}150blkn += compptr->MCU_width;151output_ptr += compptr->_DCT_scaled_size;152}153}154}155}156/* Completed an MCU row, but perhaps not an iMCU row */157coef->MCU_ctr = 0;158}159/* Completed the iMCU row, advance counters for next one */160cinfo->output_iMCU_row++;161if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {162start_iMCU_row(cinfo);163return JPEG_ROW_COMPLETED;164}165/* Completed the scan */166(*cinfo->inputctl->finish_input_pass) (cinfo);167return JPEG_SCAN_COMPLETED;168}169170171/*172* Dummy consume-input routine for single-pass operation.173*/174175METHODDEF(int)176dummy_consume_data(j_decompress_ptr cinfo)177{178return JPEG_SUSPENDED; /* Always indicate nothing was done */179}180181182#ifdef D_MULTISCAN_FILES_SUPPORTED183184/*185* Consume input data and store it in the full-image coefficient buffer.186* We read as much as one fully interleaved MCU row ("iMCU" row) per call,187* ie, v_samp_factor block rows for each component in the scan.188* Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.189*/190191METHODDEF(int)192consume_data(j_decompress_ptr cinfo)193{194my_coef_ptr coef = (my_coef_ptr)cinfo->coef;195JDIMENSION MCU_col_num; /* index of current MCU within row */196int blkn, ci, xindex, yindex, yoffset;197JDIMENSION start_col;198JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];199JBLOCKROW buffer_ptr;200jpeg_component_info *compptr;201202/* Align the virtual buffers for the components used in this scan. */203for (ci = 0; ci < cinfo->comps_in_scan; ci++) {204compptr = cinfo->cur_comp_info[ci];205buffer[ci] = (*cinfo->mem->access_virt_barray)206((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],207cinfo->input_iMCU_row * compptr->v_samp_factor,208(JDIMENSION)compptr->v_samp_factor, TRUE);209/* Note: entropy decoder expects buffer to be zeroed,210* but this is handled automatically by the memory manager211* because we requested a pre-zeroed array.212*/213}214215/* Loop to process one whole iMCU row */216for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;217yoffset++) {218for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;219MCU_col_num++) {220/* Construct list of pointers to DCT blocks belonging to this MCU */221blkn = 0; /* index of current DCT block within MCU */222for (ci = 0; ci < cinfo->comps_in_scan; ci++) {223compptr = cinfo->cur_comp_info[ci];224start_col = MCU_col_num * compptr->MCU_width;225for (yindex = 0; yindex < compptr->MCU_height; yindex++) {226buffer_ptr = buffer[ci][yindex + yoffset] + start_col;227for (xindex = 0; xindex < compptr->MCU_width; xindex++) {228coef->MCU_buffer[blkn++] = buffer_ptr++;229}230}231}232if (!cinfo->entropy->insufficient_data)233cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;234/* Try to fetch the MCU. */235if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {236/* Suspension forced; update state counters and exit */237coef->MCU_vert_offset = yoffset;238coef->MCU_ctr = MCU_col_num;239return JPEG_SUSPENDED;240}241}242/* Completed an MCU row, but perhaps not an iMCU row */243coef->MCU_ctr = 0;244}245/* Completed the iMCU row, advance counters for next one */246if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {247start_iMCU_row(cinfo);248return JPEG_ROW_COMPLETED;249}250/* Completed the scan */251(*cinfo->inputctl->finish_input_pass) (cinfo);252return JPEG_SCAN_COMPLETED;253}254255256/*257* Decompress and return some data in the multi-pass case.258* Always attempts to emit one fully interleaved MCU row ("iMCU" row).259* Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.260*261* NB: output_buf contains a plane for each component in image.262*/263264METHODDEF(int)265decompress_data(j_decompress_ptr cinfo, _JSAMPIMAGE output_buf)266{267my_coef_ptr coef = (my_coef_ptr)cinfo->coef;268JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;269JDIMENSION block_num;270int ci, block_row, block_rows;271JBLOCKARRAY buffer;272JBLOCKROW buffer_ptr;273_JSAMPARRAY output_ptr;274JDIMENSION output_col;275jpeg_component_info *compptr;276_inverse_DCT_method_ptr inverse_DCT;277278/* Force some input to be done if we are getting ahead of the input. */279while (cinfo->input_scan_number < cinfo->output_scan_number ||280(cinfo->input_scan_number == cinfo->output_scan_number &&281cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {282if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)283return JPEG_SUSPENDED;284}285286/* OK, output from the virtual arrays. */287for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;288ci++, compptr++) {289/* Don't bother to IDCT an uninteresting component. */290if (!compptr->component_needed)291continue;292/* Align the virtual buffer for this component. */293buffer = (*cinfo->mem->access_virt_barray)294((j_common_ptr)cinfo, coef->whole_image[ci],295cinfo->output_iMCU_row * compptr->v_samp_factor,296(JDIMENSION)compptr->v_samp_factor, FALSE);297/* Count non-dummy DCT block rows in this iMCU row. */298if (cinfo->output_iMCU_row < last_iMCU_row)299block_rows = compptr->v_samp_factor;300else {301/* NB: can't use last_row_height here; it is input-side-dependent! */302block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);303if (block_rows == 0) block_rows = compptr->v_samp_factor;304}305inverse_DCT = cinfo->idct->_inverse_DCT[ci];306output_ptr = output_buf[ci];307/* Loop over all DCT blocks to be processed. */308for (block_row = 0; block_row < block_rows; block_row++) {309buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];310output_col = 0;311for (block_num = cinfo->master->first_MCU_col[ci];312block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {313(*inverse_DCT) (cinfo, compptr, (JCOEFPTR)buffer_ptr, output_ptr,314output_col);315buffer_ptr++;316output_col += compptr->_DCT_scaled_size;317}318output_ptr += compptr->_DCT_scaled_size;319}320}321322if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)323return JPEG_ROW_COMPLETED;324return JPEG_SCAN_COMPLETED;325}326327#endif /* D_MULTISCAN_FILES_SUPPORTED */328329330#ifdef BLOCK_SMOOTHING_SUPPORTED331332/*333* This code applies interblock smoothing; the first 9 AC coefficients are334* estimated from the DC values of a DCT block and its 24 neighboring blocks.335* We apply smoothing only for progressive JPEG decoding, and only if336* the coefficients it can estimate are not yet known to full precision.337*/338339/* Natural-order array positions of the first 9 zigzag-order coefficients */340#define Q01_POS 1341#define Q10_POS 8342#define Q20_POS 16343#define Q11_POS 9344#define Q02_POS 2345#define Q03_POS 3346#define Q12_POS 10347#define Q21_POS 17348#define Q30_POS 24349350/*351* Determine whether block smoothing is applicable and safe.352* We also latch the current states of the coef_bits[] entries for the353* AC coefficients; otherwise, if the input side of the decompressor354* advances into a new scan, we might think the coefficients are known355* more accurately than they really are.356*/357358LOCAL(boolean)359smoothing_ok(j_decompress_ptr cinfo)360{361my_coef_ptr coef = (my_coef_ptr)cinfo->coef;362boolean smoothing_useful = FALSE;363int ci, coefi;364jpeg_component_info *compptr;365JQUANT_TBL *qtable;366int *coef_bits, *prev_coef_bits;367int *coef_bits_latch, *prev_coef_bits_latch;368369if (!cinfo->progressive_mode || cinfo->coef_bits == NULL)370return FALSE;371372/* Allocate latch area if not already done */373if (coef->coef_bits_latch == NULL)374coef->coef_bits_latch = (int *)375(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,376cinfo->num_components * 2 *377(SAVED_COEFS * sizeof(int)));378coef_bits_latch = coef->coef_bits_latch;379prev_coef_bits_latch =380&coef->coef_bits_latch[cinfo->num_components * SAVED_COEFS];381382for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;383ci++, compptr++) {384/* All components' quantization values must already be latched. */385if ((qtable = compptr->quant_table) == NULL)386return FALSE;387/* Verify DC & first 9 AC quantizers are nonzero to avoid zero-divide. */388if (qtable->quantval[0] == 0 ||389qtable->quantval[Q01_POS] == 0 ||390qtable->quantval[Q10_POS] == 0 ||391qtable->quantval[Q20_POS] == 0 ||392qtable->quantval[Q11_POS] == 0 ||393qtable->quantval[Q02_POS] == 0 ||394qtable->quantval[Q03_POS] == 0 ||395qtable->quantval[Q12_POS] == 0 ||396qtable->quantval[Q21_POS] == 0 ||397qtable->quantval[Q30_POS] == 0)398return FALSE;399/* DC values must be at least partly known for all components. */400coef_bits = cinfo->coef_bits[ci];401prev_coef_bits = cinfo->coef_bits[ci + cinfo->num_components];402if (coef_bits[0] < 0)403return FALSE;404coef_bits_latch[0] = coef_bits[0];405/* Block smoothing is helpful if some AC coefficients remain inaccurate. */406for (coefi = 1; coefi < SAVED_COEFS; coefi++) {407if (cinfo->input_scan_number > 1)408prev_coef_bits_latch[coefi] = prev_coef_bits[coefi];409else410prev_coef_bits_latch[coefi] = -1;411coef_bits_latch[coefi] = coef_bits[coefi];412if (coef_bits[coefi] != 0)413smoothing_useful = TRUE;414}415coef_bits_latch += SAVED_COEFS;416prev_coef_bits_latch += SAVED_COEFS;417}418419return smoothing_useful;420}421422423/*424* Variant of decompress_data for use when doing block smoothing.425*/426427METHODDEF(int)428decompress_smooth_data(j_decompress_ptr cinfo, _JSAMPIMAGE output_buf)429{430my_coef_ptr coef = (my_coef_ptr)cinfo->coef;431JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;432JDIMENSION block_num, last_block_column;433int ci, block_row, block_rows, access_rows, image_block_row,434image_block_rows;435JBLOCKARRAY buffer;436JBLOCKROW buffer_ptr, prev_prev_block_row, prev_block_row;437JBLOCKROW next_block_row, next_next_block_row;438_JSAMPARRAY output_ptr;439JDIMENSION output_col;440jpeg_component_info *compptr;441_inverse_DCT_method_ptr inverse_DCT;442boolean change_dc;443JCOEF *workspace;444int *coef_bits;445JQUANT_TBL *quanttbl;446JLONG Q00, Q01, Q02, Q03 = 0, Q10, Q11, Q12 = 0, Q20, Q21 = 0, Q30 = 0, num;447int DC01, DC02, DC03, DC04, DC05, DC06, DC07, DC08, DC09, DC10, DC11, DC12,448DC13, DC14, DC15, DC16, DC17, DC18, DC19, DC20, DC21, DC22, DC23, DC24,449DC25;450int Al, pred;451452/* Keep a local variable to avoid looking it up more than once */453workspace = coef->workspace;454455/* Force some input to be done if we are getting ahead of the input. */456while (cinfo->input_scan_number <= cinfo->output_scan_number &&457!cinfo->inputctl->eoi_reached) {458if (cinfo->input_scan_number == cinfo->output_scan_number) {459/* If input is working on current scan, we ordinarily want it to460* have completed the current row. But if input scan is DC,461* we want it to keep two rows ahead so that next two block rows' DC462* values are up to date.463*/464JDIMENSION delta = (cinfo->Ss == 0) ? 2 : 0;465if (cinfo->input_iMCU_row > cinfo->output_iMCU_row + delta)466break;467}468if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)469return JPEG_SUSPENDED;470}471472/* OK, output from the virtual arrays. */473for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;474ci++, compptr++) {475/* Don't bother to IDCT an uninteresting component. */476if (!compptr->component_needed)477continue;478/* Count non-dummy DCT block rows in this iMCU row. */479if (cinfo->output_iMCU_row + 1 < last_iMCU_row) {480block_rows = compptr->v_samp_factor;481access_rows = block_rows * 3; /* this and next two iMCU rows */482} else if (cinfo->output_iMCU_row < last_iMCU_row) {483block_rows = compptr->v_samp_factor;484access_rows = block_rows * 2; /* this and next iMCU row */485} else {486/* NB: can't use last_row_height here; it is input-side-dependent! */487block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);488if (block_rows == 0) block_rows = compptr->v_samp_factor;489access_rows = block_rows; /* this iMCU row only */490}491/* Align the virtual buffer for this component. */492if (cinfo->output_iMCU_row > 1) {493access_rows += 2 * compptr->v_samp_factor; /* prior two iMCU rows too */494buffer = (*cinfo->mem->access_virt_barray)495((j_common_ptr)cinfo, coef->whole_image[ci],496(cinfo->output_iMCU_row - 2) * compptr->v_samp_factor,497(JDIMENSION)access_rows, FALSE);498buffer += 2 * compptr->v_samp_factor; /* point to current iMCU row */499} else if (cinfo->output_iMCU_row > 0) {500access_rows += compptr->v_samp_factor; /* prior iMCU row too */501buffer = (*cinfo->mem->access_virt_barray)502((j_common_ptr)cinfo, coef->whole_image[ci],503(cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,504(JDIMENSION)access_rows, FALSE);505buffer += compptr->v_samp_factor; /* point to current iMCU row */506} else {507buffer = (*cinfo->mem->access_virt_barray)508((j_common_ptr)cinfo, coef->whole_image[ci],509(JDIMENSION)0, (JDIMENSION)access_rows, FALSE);510}511/* Fetch component-dependent info.512* If the current scan is incomplete, then we use the component-dependent513* info from the previous scan.514*/515if (cinfo->output_iMCU_row > cinfo->master->last_good_iMCU_row)516coef_bits =517coef->coef_bits_latch + ((ci + cinfo->num_components) * SAVED_COEFS);518else519coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);520521/* We only do DC interpolation if no AC coefficient data is available. */522change_dc =523coef_bits[1] == -1 && coef_bits[2] == -1 && coef_bits[3] == -1 &&524coef_bits[4] == -1 && coef_bits[5] == -1 && coef_bits[6] == -1 &&525coef_bits[7] == -1 && coef_bits[8] == -1 && coef_bits[9] == -1;526527quanttbl = compptr->quant_table;528Q00 = quanttbl->quantval[0];529Q01 = quanttbl->quantval[Q01_POS];530Q10 = quanttbl->quantval[Q10_POS];531Q20 = quanttbl->quantval[Q20_POS];532Q11 = quanttbl->quantval[Q11_POS];533Q02 = quanttbl->quantval[Q02_POS];534if (change_dc) {535Q03 = quanttbl->quantval[Q03_POS];536Q12 = quanttbl->quantval[Q12_POS];537Q21 = quanttbl->quantval[Q21_POS];538Q30 = quanttbl->quantval[Q30_POS];539}540inverse_DCT = cinfo->idct->_inverse_DCT[ci];541output_ptr = output_buf[ci];542/* Loop over all DCT blocks to be processed. */543image_block_rows = block_rows * cinfo->total_iMCU_rows;544for (block_row = 0; block_row < block_rows; block_row++) {545image_block_row = cinfo->output_iMCU_row * block_rows + block_row;546buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];547548if (image_block_row > 0)549prev_block_row =550buffer[block_row - 1] + cinfo->master->first_MCU_col[ci];551else552prev_block_row = buffer_ptr;553554if (image_block_row > 1)555prev_prev_block_row =556buffer[block_row - 2] + cinfo->master->first_MCU_col[ci];557else558prev_prev_block_row = prev_block_row;559560if (image_block_row < image_block_rows - 1)561next_block_row =562buffer[block_row + 1] + cinfo->master->first_MCU_col[ci];563else564next_block_row = buffer_ptr;565566if (image_block_row < image_block_rows - 2)567next_next_block_row =568buffer[block_row + 2] + cinfo->master->first_MCU_col[ci];569else570next_next_block_row = next_block_row;571572/* We fetch the surrounding DC values using a sliding-register approach.573* Initialize all 25 here so as to do the right thing on narrow pics.574*/575DC01 = DC02 = DC03 = DC04 = DC05 = (int)prev_prev_block_row[0][0];576DC06 = DC07 = DC08 = DC09 = DC10 = (int)prev_block_row[0][0];577DC11 = DC12 = DC13 = DC14 = DC15 = (int)buffer_ptr[0][0];578DC16 = DC17 = DC18 = DC19 = DC20 = (int)next_block_row[0][0];579DC21 = DC22 = DC23 = DC24 = DC25 = (int)next_next_block_row[0][0];580output_col = 0;581last_block_column = compptr->width_in_blocks - 1;582for (block_num = cinfo->master->first_MCU_col[ci];583block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {584/* Fetch current DCT block into workspace so we can modify it. */585jcopy_block_row(buffer_ptr, (JBLOCKROW)workspace, (JDIMENSION)1);586/* Update DC values */587if (block_num == cinfo->master->first_MCU_col[ci] &&588block_num < last_block_column) {589DC04 = DC05 = (int)prev_prev_block_row[1][0];590DC09 = DC10 = (int)prev_block_row[1][0];591DC14 = DC15 = (int)buffer_ptr[1][0];592DC19 = DC20 = (int)next_block_row[1][0];593DC24 = DC25 = (int)next_next_block_row[1][0];594}595if (block_num + 1 < last_block_column) {596DC05 = (int)prev_prev_block_row[2][0];597DC10 = (int)prev_block_row[2][0];598DC15 = (int)buffer_ptr[2][0];599DC20 = (int)next_block_row[2][0];600DC25 = (int)next_next_block_row[2][0];601}602/* If DC interpolation is enabled, compute coefficient estimates using603* a Gaussian-like kernel, keeping the averages of the DC values.604*605* If DC interpolation is disabled, compute coefficient estimates using606* an algorithm similar to the one described in Section K.8 of the JPEG607* standard, except applied to a 5x5 window rather than a 3x3 window.608*609* An estimate is applied only if the coefficient is still zero and is610* not known to be fully accurate.611*/612/* AC01 */613if ((Al = coef_bits[1]) != 0 && workspace[1] == 0) {614num = Q00 * (change_dc ?615(-DC01 - DC02 + DC04 + DC05 - 3 * DC06 + 13 * DC07 -61613 * DC09 + 3 * DC10 - 3 * DC11 + 38 * DC12 - 38 * DC14 +6173 * DC15 - 3 * DC16 + 13 * DC17 - 13 * DC19 + 3 * DC20 -618DC21 - DC22 + DC24 + DC25) :619(-7 * DC11 + 50 * DC12 - 50 * DC14 + 7 * DC15));620if (num >= 0) {621pred = (int)(((Q01 << 7) + num) / (Q01 << 8));622if (Al > 0 && pred >= (1 << Al))623pred = (1 << Al) - 1;624} else {625pred = (int)(((Q01 << 7) - num) / (Q01 << 8));626if (Al > 0 && pred >= (1 << Al))627pred = (1 << Al) - 1;628pred = -pred;629}630workspace[1] = (JCOEF)pred;631}632/* AC10 */633if ((Al = coef_bits[2]) != 0 && workspace[8] == 0) {634num = Q00 * (change_dc ?635(-DC01 - 3 * DC02 - 3 * DC03 - 3 * DC04 - DC05 - DC06 +63613 * DC07 + 38 * DC08 + 13 * DC09 - DC10 + DC16 -63713 * DC17 - 38 * DC18 - 13 * DC19 + DC20 + DC21 +6383 * DC22 + 3 * DC23 + 3 * DC24 + DC25) :639(-7 * DC03 + 50 * DC08 - 50 * DC18 + 7 * DC23));640if (num >= 0) {641pred = (int)(((Q10 << 7) + num) / (Q10 << 8));642if (Al > 0 && pred >= (1 << Al))643pred = (1 << Al) - 1;644} else {645pred = (int)(((Q10 << 7) - num) / (Q10 << 8));646if (Al > 0 && pred >= (1 << Al))647pred = (1 << Al) - 1;648pred = -pred;649}650workspace[8] = (JCOEF)pred;651}652/* AC20 */653if ((Al = coef_bits[3]) != 0 && workspace[16] == 0) {654num = Q00 * (change_dc ?655(DC03 + 2 * DC07 + 7 * DC08 + 2 * DC09 - 5 * DC12 - 14 * DC13 -6565 * DC14 + 2 * DC17 + 7 * DC18 + 2 * DC19 + DC23) :657(-DC03 + 13 * DC08 - 24 * DC13 + 13 * DC18 - DC23));658if (num >= 0) {659pred = (int)(((Q20 << 7) + num) / (Q20 << 8));660if (Al > 0 && pred >= (1 << Al))661pred = (1 << Al) - 1;662} else {663pred = (int)(((Q20 << 7) - num) / (Q20 << 8));664if (Al > 0 && pred >= (1 << Al))665pred = (1 << Al) - 1;666pred = -pred;667}668workspace[16] = (JCOEF)pred;669}670/* AC11 */671if ((Al = coef_bits[4]) != 0 && workspace[9] == 0) {672num = Q00 * (change_dc ?673(-DC01 + DC05 + 9 * DC07 - 9 * DC09 - 9 * DC17 +6749 * DC19 + DC21 - DC25) :675(DC10 + DC16 - 10 * DC17 + 10 * DC19 - DC02 - DC20 + DC22 -676DC24 + DC04 - DC06 + 10 * DC07 - 10 * DC09));677if (num >= 0) {678pred = (int)(((Q11 << 7) + num) / (Q11 << 8));679if (Al > 0 && pred >= (1 << Al))680pred = (1 << Al) - 1;681} else {682pred = (int)(((Q11 << 7) - num) / (Q11 << 8));683if (Al > 0 && pred >= (1 << Al))684pred = (1 << Al) - 1;685pred = -pred;686}687workspace[9] = (JCOEF)pred;688}689/* AC02 */690if ((Al = coef_bits[5]) != 0 && workspace[2] == 0) {691num = Q00 * (change_dc ?692(2 * DC07 - 5 * DC08 + 2 * DC09 + DC11 + 7 * DC12 - 14 * DC13 +6937 * DC14 + DC15 + 2 * DC17 - 5 * DC18 + 2 * DC19) :694(-DC11 + 13 * DC12 - 24 * DC13 + 13 * DC14 - DC15));695if (num >= 0) {696pred = (int)(((Q02 << 7) + num) / (Q02 << 8));697if (Al > 0 && pred >= (1 << Al))698pred = (1 << Al) - 1;699} else {700pred = (int)(((Q02 << 7) - num) / (Q02 << 8));701if (Al > 0 && pred >= (1 << Al))702pred = (1 << Al) - 1;703pred = -pred;704}705workspace[2] = (JCOEF)pred;706}707if (change_dc) {708/* AC03 */709if ((Al = coef_bits[6]) != 0 && workspace[3] == 0) {710num = Q00 * (DC07 - DC09 + 2 * DC12 - 2 * DC14 + DC17 - DC19);711if (num >= 0) {712pred = (int)(((Q03 << 7) + num) / (Q03 << 8));713if (Al > 0 && pred >= (1 << Al))714pred = (1 << Al) - 1;715} else {716pred = (int)(((Q03 << 7) - num) / (Q03 << 8));717if (Al > 0 && pred >= (1 << Al))718pred = (1 << Al) - 1;719pred = -pred;720}721workspace[3] = (JCOEF)pred;722}723/* AC12 */724if ((Al = coef_bits[7]) != 0 && workspace[10] == 0) {725num = Q00 * (DC07 - 3 * DC08 + DC09 - DC17 + 3 * DC18 - DC19);726if (num >= 0) {727pred = (int)(((Q12 << 7) + num) / (Q12 << 8));728if (Al > 0 && pred >= (1 << Al))729pred = (1 << Al) - 1;730} else {731pred = (int)(((Q12 << 7) - num) / (Q12 << 8));732if (Al > 0 && pred >= (1 << Al))733pred = (1 << Al) - 1;734pred = -pred;735}736workspace[10] = (JCOEF)pred;737}738/* AC21 */739if ((Al = coef_bits[8]) != 0 && workspace[17] == 0) {740num = Q00 * (DC07 - DC09 - 3 * DC12 + 3 * DC14 + DC17 - DC19);741if (num >= 0) {742pred = (int)(((Q21 << 7) + num) / (Q21 << 8));743if (Al > 0 && pred >= (1 << Al))744pred = (1 << Al) - 1;745} else {746pred = (int)(((Q21 << 7) - num) / (Q21 << 8));747if (Al > 0 && pred >= (1 << Al))748pred = (1 << Al) - 1;749pred = -pred;750}751workspace[17] = (JCOEF)pred;752}753/* AC30 */754if ((Al = coef_bits[9]) != 0 && workspace[24] == 0) {755num = Q00 * (DC07 + 2 * DC08 + DC09 - DC17 - 2 * DC18 - DC19);756if (num >= 0) {757pred = (int)(((Q30 << 7) + num) / (Q30 << 8));758if (Al > 0 && pred >= (1 << Al))759pred = (1 << Al) - 1;760} else {761pred = (int)(((Q30 << 7) - num) / (Q30 << 8));762if (Al > 0 && pred >= (1 << Al))763pred = (1 << Al) - 1;764pred = -pred;765}766workspace[24] = (JCOEF)pred;767}768/* coef_bits[0] is non-negative. Otherwise this function would not769* be called.770*/771num = Q00 *772(-2 * DC01 - 6 * DC02 - 8 * DC03 - 6 * DC04 - 2 * DC05 -7736 * DC06 + 6 * DC07 + 42 * DC08 + 6 * DC09 - 6 * DC10 -7748 * DC11 + 42 * DC12 + 152 * DC13 + 42 * DC14 - 8 * DC15 -7756 * DC16 + 6 * DC17 + 42 * DC18 + 6 * DC19 - 6 * DC20 -7762 * DC21 - 6 * DC22 - 8 * DC23 - 6 * DC24 - 2 * DC25);777if (num >= 0) {778pred = (int)(((Q00 << 7) + num) / (Q00 << 8));779} else {780pred = (int)(((Q00 << 7) - num) / (Q00 << 8));781pred = -pred;782}783workspace[0] = (JCOEF)pred;784} /* change_dc */785786/* OK, do the IDCT */787(*inverse_DCT) (cinfo, compptr, (JCOEFPTR)workspace, output_ptr,788output_col);789/* Advance for next column */790DC01 = DC02; DC02 = DC03; DC03 = DC04; DC04 = DC05;791DC06 = DC07; DC07 = DC08; DC08 = DC09; DC09 = DC10;792DC11 = DC12; DC12 = DC13; DC13 = DC14; DC14 = DC15;793DC16 = DC17; DC17 = DC18; DC18 = DC19; DC19 = DC20;794DC21 = DC22; DC22 = DC23; DC23 = DC24; DC24 = DC25;795buffer_ptr++, prev_block_row++, next_block_row++,796prev_prev_block_row++, next_next_block_row++;797output_col += compptr->_DCT_scaled_size;798}799output_ptr += compptr->_DCT_scaled_size;800}801}802803if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)804return JPEG_ROW_COMPLETED;805return JPEG_SCAN_COMPLETED;806}807808#endif /* BLOCK_SMOOTHING_SUPPORTED */809810811/*812* Initialize coefficient buffer controller.813*/814815GLOBAL(void)816_jinit_d_coef_controller(j_decompress_ptr cinfo, boolean need_full_buffer)817{818my_coef_ptr coef;819820if (cinfo->data_precision != BITS_IN_JSAMPLE)821ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);822823coef = (my_coef_ptr)824(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,825sizeof(my_coef_controller));826cinfo->coef = (struct jpeg_d_coef_controller *)coef;827coef->pub.start_input_pass = start_input_pass;828coef->pub.start_output_pass = start_output_pass;829#ifdef BLOCK_SMOOTHING_SUPPORTED830coef->coef_bits_latch = NULL;831#endif832833/* Create the coefficient buffer. */834if (need_full_buffer) {835#ifdef D_MULTISCAN_FILES_SUPPORTED836/* Allocate a full-image virtual array for each component, */837/* padded to a multiple of samp_factor DCT blocks in each direction. */838/* Note we ask for a pre-zeroed array. */839int ci, access_rows;840jpeg_component_info *compptr;841842for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;843ci++, compptr++) {844access_rows = compptr->v_samp_factor;845#ifdef BLOCK_SMOOTHING_SUPPORTED846/* If block smoothing could be used, need a bigger window */847if (cinfo->progressive_mode)848access_rows *= 5;849#endif850coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)851((j_common_ptr)cinfo, JPOOL_IMAGE, TRUE,852(JDIMENSION)jround_up((long)compptr->width_in_blocks,853(long)compptr->h_samp_factor),854(JDIMENSION)jround_up((long)compptr->height_in_blocks,855(long)compptr->v_samp_factor),856(JDIMENSION)access_rows);857}858coef->pub.consume_data = consume_data;859coef->pub._decompress_data = decompress_data;860coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */861#else862ERREXIT(cinfo, JERR_NOT_COMPILED);863#endif864} else {865/* We only need a single-MCU buffer. */866JBLOCKROW buffer;867int i;868869buffer = (JBLOCKROW)870(*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,871D_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));872for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {873coef->MCU_buffer[i] = buffer + i;874}875coef->pub.consume_data = dummy_consume_data;876coef->pub._decompress_data = decompress_onepass;877coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */878}879880/* Allocate the workspace buffer */881coef->workspace = (JCOEF *)882(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,883sizeof(JCOEF) * DCTSIZE2);884}885886887