Path: blob/master/3rdparty/libjpeg-turbo/src/jdinput.c
16337 views
/*1* jdinput.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1997, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright (C) 2010, 2016, D. R. Commander.7* Copyright (C) 2015, Google, Inc.8* For conditions of distribution and use, see the accompanying README.ijg9* file.10*11* This file contains input control logic for the JPEG decompressor.12* These routines are concerned with controlling the decompressor's input13* processing (marker reading and coefficient decoding). The actual input14* reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.15*/1617#define JPEG_INTERNALS18#include "jinclude.h"19#include "jpeglib.h"20#include "jpegcomp.h"212223/* Private state */2425typedef struct {26struct jpeg_input_controller pub; /* public fields */2728boolean inheaders; /* TRUE until first SOS is reached */29} my_input_controller;3031typedef my_input_controller *my_inputctl_ptr;323334/* Forward declarations */35METHODDEF(int) consume_markers (j_decompress_ptr cinfo);363738/*39* Routines to calculate various quantities related to the size of the image.40*/4142LOCAL(void)43initial_setup (j_decompress_ptr cinfo)44/* Called once, when first SOS marker is reached */45{46int ci;47jpeg_component_info *compptr;4849/* Make sure image isn't bigger than I can handle */50if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||51(long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)52ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);5354/* For now, precision must match compiled-in value... */55if (cinfo->data_precision != BITS_IN_JSAMPLE)56ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);5758/* Check that number of components won't exceed internal array sizes */59if (cinfo->num_components > MAX_COMPONENTS)60ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,61MAX_COMPONENTS);6263/* Compute maximum sampling factors; check factor validity */64cinfo->max_h_samp_factor = 1;65cinfo->max_v_samp_factor = 1;66for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;67ci++, compptr++) {68if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||69compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)70ERREXIT(cinfo, JERR_BAD_SAMPLING);71cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,72compptr->h_samp_factor);73cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,74compptr->v_samp_factor);75}7677#if JPEG_LIB_VERSION >=8078cinfo->block_size = DCTSIZE;79cinfo->natural_order = jpeg_natural_order;80cinfo->lim_Se = DCTSIZE2-1;81#endif8283/* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.84* In the full decompressor, this will be overridden by jdmaster.c;85* but in the transcoder, jdmaster.c is not used, so we must do it here.86*/87#if JPEG_LIB_VERSION >= 7088cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE;89#else90cinfo->min_DCT_scaled_size = DCTSIZE;91#endif9293/* Compute dimensions of components */94for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;95ci++, compptr++) {96#if JPEG_LIB_VERSION >= 7097compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;98#else99compptr->DCT_scaled_size = DCTSIZE;100#endif101/* Size in DCT blocks */102compptr->width_in_blocks = (JDIMENSION)103jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,104(long) (cinfo->max_h_samp_factor * DCTSIZE));105compptr->height_in_blocks = (JDIMENSION)106jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,107(long) (cinfo->max_v_samp_factor * DCTSIZE));108/* Set the first and last MCU columns to decompress from multi-scan images.109* By default, decompress all of the MCU columns.110*/111cinfo->master->first_MCU_col[ci] = 0;112cinfo->master->last_MCU_col[ci] = compptr->width_in_blocks - 1;113/* downsampled_width and downsampled_height will also be overridden by114* jdmaster.c if we are doing full decompression. The transcoder library115* doesn't use these values, but the calling application might.116*/117/* Size in samples */118compptr->downsampled_width = (JDIMENSION)119jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,120(long) cinfo->max_h_samp_factor);121compptr->downsampled_height = (JDIMENSION)122jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,123(long) cinfo->max_v_samp_factor);124/* Mark component needed, until color conversion says otherwise */125compptr->component_needed = TRUE;126/* Mark no quantization table yet saved for component */127compptr->quant_table = NULL;128}129130/* Compute number of fully interleaved MCU rows. */131cinfo->total_iMCU_rows = (JDIMENSION)132jdiv_round_up((long) cinfo->image_height,133(long) (cinfo->max_v_samp_factor*DCTSIZE));134135/* Decide whether file contains multiple scans */136if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)137cinfo->inputctl->has_multiple_scans = TRUE;138else139cinfo->inputctl->has_multiple_scans = FALSE;140}141142143LOCAL(void)144per_scan_setup (j_decompress_ptr cinfo)145/* Do computations that are needed before processing a JPEG scan */146/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */147{148int ci, mcublks, tmp;149jpeg_component_info *compptr;150151if (cinfo->comps_in_scan == 1) {152153/* Noninterleaved (single-component) scan */154compptr = cinfo->cur_comp_info[0];155156/* Overall image size in MCUs */157cinfo->MCUs_per_row = compptr->width_in_blocks;158cinfo->MCU_rows_in_scan = compptr->height_in_blocks;159160/* For noninterleaved scan, always one block per MCU */161compptr->MCU_width = 1;162compptr->MCU_height = 1;163compptr->MCU_blocks = 1;164compptr->MCU_sample_width = compptr->_DCT_scaled_size;165compptr->last_col_width = 1;166/* For noninterleaved scans, it is convenient to define last_row_height167* as the number of block rows present in the last iMCU row.168*/169tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);170if (tmp == 0) tmp = compptr->v_samp_factor;171compptr->last_row_height = tmp;172173/* Prepare array describing MCU composition */174cinfo->blocks_in_MCU = 1;175cinfo->MCU_membership[0] = 0;176177} else {178179/* Interleaved (multi-component) scan */180if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)181ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,182MAX_COMPS_IN_SCAN);183184/* Overall image size in MCUs */185cinfo->MCUs_per_row = (JDIMENSION)186jdiv_round_up((long) cinfo->image_width,187(long) (cinfo->max_h_samp_factor*DCTSIZE));188cinfo->MCU_rows_in_scan = (JDIMENSION)189jdiv_round_up((long) cinfo->image_height,190(long) (cinfo->max_v_samp_factor*DCTSIZE));191192cinfo->blocks_in_MCU = 0;193194for (ci = 0; ci < cinfo->comps_in_scan; ci++) {195compptr = cinfo->cur_comp_info[ci];196/* Sampling factors give # of blocks of component in each MCU */197compptr->MCU_width = compptr->h_samp_factor;198compptr->MCU_height = compptr->v_samp_factor;199compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;200compptr->MCU_sample_width = compptr->MCU_width * compptr->_DCT_scaled_size;201/* Figure number of non-dummy blocks in last MCU column & row */202tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);203if (tmp == 0) tmp = compptr->MCU_width;204compptr->last_col_width = tmp;205tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);206if (tmp == 0) tmp = compptr->MCU_height;207compptr->last_row_height = tmp;208/* Prepare array describing MCU composition */209mcublks = compptr->MCU_blocks;210if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)211ERREXIT(cinfo, JERR_BAD_MCU_SIZE);212while (mcublks-- > 0) {213cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;214}215}216217}218}219220221/*222* Save away a copy of the Q-table referenced by each component present223* in the current scan, unless already saved during a prior scan.224*225* In a multiple-scan JPEG file, the encoder could assign different components226* the same Q-table slot number, but change table definitions between scans227* so that each component uses a different Q-table. (The IJG encoder is not228* currently capable of doing this, but other encoders might.) Since we want229* to be able to dequantize all the components at the end of the file, this230* means that we have to save away the table actually used for each component.231* We do this by copying the table at the start of the first scan containing232* the component.233* The JPEG spec prohibits the encoder from changing the contents of a Q-table234* slot between scans of a component using that slot. If the encoder does so235* anyway, this decoder will simply use the Q-table values that were current236* at the start of the first scan for the component.237*238* The decompressor output side looks only at the saved quant tables,239* not at the current Q-table slots.240*/241242LOCAL(void)243latch_quant_tables (j_decompress_ptr cinfo)244{245int ci, qtblno;246jpeg_component_info *compptr;247JQUANT_TBL *qtbl;248249for (ci = 0; ci < cinfo->comps_in_scan; ci++) {250compptr = cinfo->cur_comp_info[ci];251/* No work if we already saved Q-table for this component */252if (compptr->quant_table != NULL)253continue;254/* Make sure specified quantization table is present */255qtblno = compptr->quant_tbl_no;256if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||257cinfo->quant_tbl_ptrs[qtblno] == NULL)258ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);259/* OK, save away the quantization table */260qtbl = (JQUANT_TBL *)261(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,262sizeof(JQUANT_TBL));263MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], sizeof(JQUANT_TBL));264compptr->quant_table = qtbl;265}266}267268269/*270* Initialize the input modules to read a scan of compressed data.271* The first call to this is done by jdmaster.c after initializing272* the entire decompressor (during jpeg_start_decompress).273* Subsequent calls come from consume_markers, below.274*/275276METHODDEF(void)277start_input_pass (j_decompress_ptr cinfo)278{279per_scan_setup(cinfo);280latch_quant_tables(cinfo);281(*cinfo->entropy->start_pass) (cinfo);282(*cinfo->coef->start_input_pass) (cinfo);283cinfo->inputctl->consume_input = cinfo->coef->consume_data;284}285286287/*288* Finish up after inputting a compressed-data scan.289* This is called by the coefficient controller after it's read all290* the expected data of the scan.291*/292293METHODDEF(void)294finish_input_pass (j_decompress_ptr cinfo)295{296cinfo->inputctl->consume_input = consume_markers;297}298299300/*301* Read JPEG markers before, between, or after compressed-data scans.302* Change state as necessary when a new scan is reached.303* Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.304*305* The consume_input method pointer points either here or to the306* coefficient controller's consume_data routine, depending on whether307* we are reading a compressed data segment or inter-segment markers.308*/309310METHODDEF(int)311consume_markers (j_decompress_ptr cinfo)312{313my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;314int val;315316if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */317return JPEG_REACHED_EOI;318319val = (*cinfo->marker->read_markers) (cinfo);320321switch (val) {322case JPEG_REACHED_SOS: /* Found SOS */323if (inputctl->inheaders) { /* 1st SOS */324initial_setup(cinfo);325inputctl->inheaders = FALSE;326/* Note: start_input_pass must be called by jdmaster.c327* before any more input can be consumed. jdapimin.c is328* responsible for enforcing this sequencing.329*/330} else { /* 2nd or later SOS marker */331if (! inputctl->pub.has_multiple_scans)332ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */333start_input_pass(cinfo);334}335break;336case JPEG_REACHED_EOI: /* Found EOI */337inputctl->pub.eoi_reached = TRUE;338if (inputctl->inheaders) { /* Tables-only datastream, apparently */339if (cinfo->marker->saw_SOF)340ERREXIT(cinfo, JERR_SOF_NO_SOS);341} else {342/* Prevent infinite loop in coef ctlr's decompress_data routine343* if user set output_scan_number larger than number of scans.344*/345if (cinfo->output_scan_number > cinfo->input_scan_number)346cinfo->output_scan_number = cinfo->input_scan_number;347}348break;349case JPEG_SUSPENDED:350break;351}352353return val;354}355356357/*358* Reset state to begin a fresh datastream.359*/360361METHODDEF(void)362reset_input_controller (j_decompress_ptr cinfo)363{364my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;365366inputctl->pub.consume_input = consume_markers;367inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */368inputctl->pub.eoi_reached = FALSE;369inputctl->inheaders = TRUE;370/* Reset other modules */371(*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);372(*cinfo->marker->reset_marker_reader) (cinfo);373/* Reset progression state -- would be cleaner if entropy decoder did this */374cinfo->coef_bits = NULL;375}376377378/*379* Initialize the input controller module.380* This is called only once, when the decompression object is created.381*/382383GLOBAL(void)384jinit_input_controller (j_decompress_ptr cinfo)385{386my_inputctl_ptr inputctl;387388/* Create subobject in permanent pool */389inputctl = (my_inputctl_ptr)390(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,391sizeof(my_input_controller));392cinfo->inputctl = (struct jpeg_input_controller *) inputctl;393/* Initialize method pointers */394inputctl->pub.consume_input = consume_markers;395inputctl->pub.reset_input_controller = reset_input_controller;396inputctl->pub.start_input_pass = start_input_pass;397inputctl->pub.finish_input_pass = finish_input_pass;398/* Initialize state: can't use reset_input_controller since we don't399* want to try to reset other modules yet.400*/401inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */402inputctl->pub.eoi_reached = FALSE;403inputctl->inheaders = TRUE;404}405406407