Path: blob/master/thirdparty/libjpeg-turbo/src/jdinput.c
9904 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* Lossless JPEG Modifications:6* Copyright (C) 1999, Ken Murchison.7* libjpeg-turbo Modifications:8* Copyright (C) 2010, 2016, 2018, 2022, 2024, D. R. Commander.9* Copyright (C) 2015, Google, Inc.10* For conditions of distribution and use, see the accompanying README.ijg11* file.12*13* This file contains input control logic for the JPEG decompressor.14* These routines are concerned with controlling the decompressor's input15* processing (marker reading and coefficient/difference decoding).16* The actual input reading is done in jdmarker.c, jdhuff.c, jdphuff.c,17* and jdlhuff.c.18*/1920#define JPEG_INTERNALS21#include "jinclude.h"22#include "jpeglib.h"23#include "jpegapicomp.h"242526/* Private state */2728typedef struct {29struct jpeg_input_controller pub; /* public fields */3031boolean inheaders; /* TRUE until first SOS is reached */32} my_input_controller;3334typedef my_input_controller *my_inputctl_ptr;353637/* Forward declarations */38METHODDEF(int) consume_markers(j_decompress_ptr cinfo);394041/*42* Routines to calculate various quantities related to the size of the image.43*/4445LOCAL(void)46initial_setup(j_decompress_ptr cinfo)47/* Called once, when first SOS marker is reached */48{49int ci;50jpeg_component_info *compptr;51int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;5253/* Make sure image isn't bigger than I can handle */54if ((long)cinfo->image_height > (long)JPEG_MAX_DIMENSION ||55(long)cinfo->image_width > (long)JPEG_MAX_DIMENSION)56ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);5758/* Lossy JPEG images must have 8 or 12 bits per sample. Lossless JPEG images59* can have 2 to 16 bits per sample.60*/61#ifdef D_LOSSLESS_SUPPORTED62if (cinfo->master->lossless) {63if (cinfo->data_precision < 2 || cinfo->data_precision > 16)64ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);65} else66#endif67{68if (cinfo->data_precision != 8 && cinfo->data_precision != 12)69ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);70}7172/* Check that number of components won't exceed internal array sizes */73if (cinfo->num_components > MAX_COMPONENTS)74ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,75MAX_COMPONENTS);7677/* Compute maximum sampling factors; check factor validity */78cinfo->max_h_samp_factor = 1;79cinfo->max_v_samp_factor = 1;80for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;81ci++, compptr++) {82if (compptr->h_samp_factor <= 0 ||83compptr->h_samp_factor > MAX_SAMP_FACTOR ||84compptr->v_samp_factor <= 0 ||85compptr->v_samp_factor > MAX_SAMP_FACTOR)86ERREXIT(cinfo, JERR_BAD_SAMPLING);87cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,88compptr->h_samp_factor);89cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,90compptr->v_samp_factor);91}9293#if JPEG_LIB_VERSION >= 8094cinfo->block_size = data_unit;95cinfo->natural_order = jpeg_natural_order;96cinfo->lim_Se = DCTSIZE2 - 1;97#endif9899/* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE in lossy100* mode. In the full decompressor, this will be overridden by jdmaster.c;101* but in the transcoder, jdmaster.c is not used, so we must do it here.102*/103#if JPEG_LIB_VERSION >= 70104cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = data_unit;105#else106cinfo->min_DCT_scaled_size = data_unit;107#endif108109/* Compute dimensions of components */110for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;111ci++, compptr++) {112#if JPEG_LIB_VERSION >= 70113compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = data_unit;114#else115compptr->DCT_scaled_size = data_unit;116#endif117/* Size in data units */118compptr->width_in_blocks = (JDIMENSION)119jdiv_round_up((long)cinfo->image_width * (long)compptr->h_samp_factor,120(long)(cinfo->max_h_samp_factor * data_unit));121compptr->height_in_blocks = (JDIMENSION)122jdiv_round_up((long)cinfo->image_height * (long)compptr->v_samp_factor,123(long)(cinfo->max_v_samp_factor * data_unit));124/* Set the first and last MCU columns to decompress from multi-scan images.125* By default, decompress all of the MCU columns.126*/127cinfo->master->first_MCU_col[ci] = 0;128cinfo->master->last_MCU_col[ci] = compptr->width_in_blocks - 1;129/* downsampled_width and downsampled_height will also be overridden by130* jdmaster.c if we are doing full decompression. The transcoder library131* doesn't use these values, but the calling application might.132*/133/* Size in samples */134compptr->downsampled_width = (JDIMENSION)135jdiv_round_up((long)cinfo->image_width * (long)compptr->h_samp_factor,136(long)cinfo->max_h_samp_factor);137compptr->downsampled_height = (JDIMENSION)138jdiv_round_up((long)cinfo->image_height * (long)compptr->v_samp_factor,139(long)cinfo->max_v_samp_factor);140/* Mark component needed, until color conversion says otherwise */141compptr->component_needed = TRUE;142/* Mark no quantization table yet saved for component */143compptr->quant_table = NULL;144}145146/* Compute number of fully interleaved MCU rows. */147cinfo->total_iMCU_rows = (JDIMENSION)148jdiv_round_up((long)cinfo->image_height,149(long)(cinfo->max_v_samp_factor * data_unit));150151/* Decide whether file contains multiple scans */152if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)153cinfo->inputctl->has_multiple_scans = TRUE;154else155cinfo->inputctl->has_multiple_scans = FALSE;156}157158159LOCAL(void)160per_scan_setup(j_decompress_ptr cinfo)161/* Do computations that are needed before processing a JPEG scan */162/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */163{164int ci, mcublks, tmp;165jpeg_component_info *compptr;166int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;167168if (cinfo->comps_in_scan == 1) {169170/* Noninterleaved (single-component) scan */171compptr = cinfo->cur_comp_info[0];172173/* Overall image size in MCUs */174cinfo->MCUs_per_row = compptr->width_in_blocks;175cinfo->MCU_rows_in_scan = compptr->height_in_blocks;176177/* For noninterleaved scan, always one data unit per MCU */178compptr->MCU_width = 1;179compptr->MCU_height = 1;180compptr->MCU_blocks = 1;181compptr->MCU_sample_width = compptr->_DCT_scaled_size;182compptr->last_col_width = 1;183/* For noninterleaved scans, it is convenient to define last_row_height184* as the number of data unit rows present in the last iMCU row.185*/186tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor);187if (tmp == 0) tmp = compptr->v_samp_factor;188compptr->last_row_height = tmp;189190/* Prepare array describing MCU composition */191cinfo->blocks_in_MCU = 1;192cinfo->MCU_membership[0] = 0;193194} else {195196/* Interleaved (multi-component) scan */197if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)198ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,199MAX_COMPS_IN_SCAN);200201/* Overall image size in MCUs */202cinfo->MCUs_per_row = (JDIMENSION)203jdiv_round_up((long)cinfo->image_width,204(long)(cinfo->max_h_samp_factor * data_unit));205cinfo->MCU_rows_in_scan = (JDIMENSION)206jdiv_round_up((long)cinfo->image_height,207(long)(cinfo->max_v_samp_factor * data_unit));208209cinfo->blocks_in_MCU = 0;210211for (ci = 0; ci < cinfo->comps_in_scan; ci++) {212compptr = cinfo->cur_comp_info[ci];213/* Sampling factors give # of data units of component in each MCU */214compptr->MCU_width = compptr->h_samp_factor;215compptr->MCU_height = compptr->v_samp_factor;216compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;217compptr->MCU_sample_width = compptr->MCU_width *218compptr->_DCT_scaled_size;219/* Figure number of non-dummy data units in last MCU column & row */220tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);221if (tmp == 0) tmp = compptr->MCU_width;222compptr->last_col_width = tmp;223tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);224if (tmp == 0) tmp = compptr->MCU_height;225compptr->last_row_height = tmp;226/* Prepare array describing MCU composition */227mcublks = compptr->MCU_blocks;228if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)229ERREXIT(cinfo, JERR_BAD_MCU_SIZE);230while (mcublks-- > 0) {231cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;232}233}234235}236}237238239/*240* Save away a copy of the Q-table referenced by each component present241* in the current scan, unless already saved during a prior scan.242*243* In a multiple-scan JPEG file, the encoder could assign different components244* the same Q-table slot number, but change table definitions between scans245* so that each component uses a different Q-table. (The IJG encoder is not246* currently capable of doing this, but other encoders might.) Since we want247* to be able to dequantize all the components at the end of the file, this248* means that we have to save away the table actually used for each component.249* We do this by copying the table at the start of the first scan containing250* the component.251* Rec. ITU-T T.81 | ISO/IEC 10918-1 prohibits the encoder from changing the252* contents of a Q-table slot between scans of a component using that slot. If253* the encoder does so anyway, this decoder will simply use the Q-table values254* that were current at the start of the first scan for the component.255*256* The decompressor output side looks only at the saved quant tables,257* not at the current Q-table slots.258*/259260LOCAL(void)261latch_quant_tables(j_decompress_ptr cinfo)262{263int ci, qtblno;264jpeg_component_info *compptr;265JQUANT_TBL *qtbl;266267for (ci = 0; ci < cinfo->comps_in_scan; ci++) {268compptr = cinfo->cur_comp_info[ci];269/* No work if we already saved Q-table for this component */270if (compptr->quant_table != NULL)271continue;272/* Make sure specified quantization table is present */273qtblno = compptr->quant_tbl_no;274if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||275cinfo->quant_tbl_ptrs[qtblno] == NULL)276ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);277/* OK, save away the quantization table */278qtbl = (JQUANT_TBL *)279(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,280sizeof(JQUANT_TBL));281memcpy(qtbl, cinfo->quant_tbl_ptrs[qtblno], sizeof(JQUANT_TBL));282compptr->quant_table = qtbl;283}284}285286287/*288* Initialize the input modules to read a scan of compressed data.289* The first call to this is done by jdmaster.c after initializing290* the entire decompressor (during jpeg_start_decompress).291* Subsequent calls come from consume_markers, below.292*/293294METHODDEF(void)295start_input_pass(j_decompress_ptr cinfo)296{297per_scan_setup(cinfo);298if (!cinfo->master->lossless)299latch_quant_tables(cinfo);300(*cinfo->entropy->start_pass) (cinfo);301(*cinfo->coef->start_input_pass) (cinfo);302cinfo->inputctl->consume_input = cinfo->coef->consume_data;303}304305306/*307* Finish up after inputting a compressed-data scan.308* This is called by the coefficient or difference controller after it's read309* all the expected data of the scan.310*/311312METHODDEF(void)313finish_input_pass(j_decompress_ptr cinfo)314{315cinfo->inputctl->consume_input = consume_markers;316}317318319/*320* Read JPEG markers before, between, or after compressed-data scans.321* Change state as necessary when a new scan is reached.322* Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.323*324* The consume_input method pointer points either here or to the325* coefficient or difference controller's consume_data routine, depending on326* whether we are reading a compressed data segment or inter-segment markers.327*/328329METHODDEF(int)330consume_markers(j_decompress_ptr cinfo)331{332my_inputctl_ptr inputctl = (my_inputctl_ptr)cinfo->inputctl;333int val;334335if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */336return JPEG_REACHED_EOI;337338val = (*cinfo->marker->read_markers) (cinfo);339340switch (val) {341case JPEG_REACHED_SOS: /* Found SOS */342if (inputctl->inheaders) { /* 1st SOS */343initial_setup(cinfo);344inputctl->inheaders = FALSE;345/* Note: start_input_pass must be called by jdmaster.c346* before any more input can be consumed. jdapimin.c is347* responsible for enforcing this sequencing.348*/349} else { /* 2nd or later SOS marker */350if (!inputctl->pub.has_multiple_scans)351ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */352start_input_pass(cinfo);353}354break;355case JPEG_REACHED_EOI: /* Found EOI */356inputctl->pub.eoi_reached = TRUE;357if (inputctl->inheaders) { /* Tables-only datastream, apparently */358if (cinfo->marker->saw_SOF)359ERREXIT(cinfo, JERR_SOF_NO_SOS);360} else {361/* Prevent infinite loop in coef ctlr's decompress_data routine362* if user set output_scan_number larger than number of scans.363*/364if (cinfo->output_scan_number > cinfo->input_scan_number)365cinfo->output_scan_number = cinfo->input_scan_number;366}367break;368case JPEG_SUSPENDED:369break;370}371372return val;373}374375376/*377* Reset state to begin a fresh datastream.378*/379380METHODDEF(void)381reset_input_controller(j_decompress_ptr cinfo)382{383my_inputctl_ptr inputctl = (my_inputctl_ptr)cinfo->inputctl;384385inputctl->pub.consume_input = consume_markers;386inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */387inputctl->pub.eoi_reached = FALSE;388inputctl->inheaders = TRUE;389/* Reset other modules */390(*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo);391(*cinfo->marker->reset_marker_reader) (cinfo);392/* Reset progression state -- would be cleaner if entropy decoder did this */393cinfo->coef_bits = NULL;394}395396397/*398* Initialize the input controller module.399* This is called only once, when the decompression object is created.400*/401402GLOBAL(void)403jinit_input_controller(j_decompress_ptr cinfo)404{405my_inputctl_ptr inputctl;406407/* Create subobject in permanent pool */408inputctl = (my_inputctl_ptr)409(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,410sizeof(my_input_controller));411cinfo->inputctl = (struct jpeg_input_controller *)inputctl;412/* Initialize method pointers */413inputctl->pub.consume_input = consume_markers;414inputctl->pub.reset_input_controller = reset_input_controller;415inputctl->pub.start_input_pass = start_input_pass;416inputctl->pub.finish_input_pass = finish_input_pass;417/* Initialize state: can't use reset_input_controller since we don't418* want to try to reset other modules yet.419*/420inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */421inputctl->pub.eoi_reached = FALSE;422inputctl->inheaders = TRUE;423}424425426