Path: blob/master/3rdparty/libjpeg-turbo/src/jcmaster.c
16337 views
/*1* jcmaster.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1997, Thomas G. Lane.5* Modified 2003-2010 by Guido Vollbeding.6* libjpeg-turbo Modifications:7* Copyright (C) 2010, 2016, D. R. Commander.8* For conditions of distribution and use, see the accompanying README.ijg9* file.10*11* This file contains master control logic for the JPEG compressor.12* These routines are concerned with parameter validation, initial setup,13* and inter-pass control (determining the number of passes and the work14* to be done in each pass).15*/1617#define JPEG_INTERNALS18#include "jinclude.h"19#include "jpeglib.h"20#include "jpegcomp.h"21#include "jconfigint.h"222324/* Private state */2526typedef enum {27main_pass, /* input data, also do first output step */28huff_opt_pass, /* Huffman code optimization pass */29output_pass /* data output pass */30} c_pass_type;3132typedef struct {33struct jpeg_comp_master pub; /* public fields */3435c_pass_type pass_type; /* the type of the current pass */3637int pass_number; /* # of passes completed */38int total_passes; /* total # of passes needed */3940int scan_number; /* current index in scan_info[] */4142/*43* This is here so we can add libjpeg-turbo version/build information to the44* global string table without introducing a new global symbol. Adding this45* information to the global string table allows one to examine a binary46* object and determine which version of libjpeg-turbo it was built from or47* linked against.48*/49const char *jpeg_version;5051} my_comp_master;5253typedef my_comp_master *my_master_ptr;545556/*57* Support routines that do various essential calculations.58*/5960#if JPEG_LIB_VERSION >= 7061/*62* Compute JPEG image dimensions and related values.63* NOTE: this is exported for possible use by application.64* Hence it mustn't do anything that can't be done twice.65*/6667GLOBAL(void)68jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)69/* Do computations that are needed before master selection phase */70{71/* Hardwire it to "no scaling" */72cinfo->jpeg_width = cinfo->image_width;73cinfo->jpeg_height = cinfo->image_height;74cinfo->min_DCT_h_scaled_size = DCTSIZE;75cinfo->min_DCT_v_scaled_size = DCTSIZE;76}77#endif787980LOCAL(void)81initial_setup (j_compress_ptr cinfo, boolean transcode_only)82/* Do computations that are needed before master selection phase */83{84int ci;85jpeg_component_info *compptr;86long samplesperrow;87JDIMENSION jd_samplesperrow;8889#if JPEG_LIB_VERSION >= 7090#if JPEG_LIB_VERSION >= 8091if (!transcode_only)92#endif93jpeg_calc_jpeg_dimensions(cinfo);94#endif9596/* Sanity check on image dimensions */97if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 098|| cinfo->num_components <= 0 || cinfo->input_components <= 0)99ERREXIT(cinfo, JERR_EMPTY_IMAGE);100101/* Make sure image isn't bigger than I can handle */102if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION ||103(long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION)104ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);105106/* Width of an input scanline must be representable as JDIMENSION. */107samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;108jd_samplesperrow = (JDIMENSION) samplesperrow;109if ((long) jd_samplesperrow != samplesperrow)110ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);111112/* For now, precision must match compiled-in value... */113if (cinfo->data_precision != BITS_IN_JSAMPLE)114ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);115116/* Check that number of components won't exceed internal array sizes */117if (cinfo->num_components > MAX_COMPONENTS)118ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,119MAX_COMPONENTS);120121/* Compute maximum sampling factors; check factor validity */122cinfo->max_h_samp_factor = 1;123cinfo->max_v_samp_factor = 1;124for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;125ci++, compptr++) {126if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||127compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)128ERREXIT(cinfo, JERR_BAD_SAMPLING);129cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,130compptr->h_samp_factor);131cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,132compptr->v_samp_factor);133}134135/* Compute dimensions of components */136for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;137ci++, compptr++) {138/* Fill in the correct component_index value; don't rely on application */139compptr->component_index = ci;140/* For compression, we never do DCT scaling. */141#if JPEG_LIB_VERSION >= 70142compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;143#else144compptr->DCT_scaled_size = DCTSIZE;145#endif146/* Size in DCT blocks */147compptr->width_in_blocks = (JDIMENSION)148jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,149(long) (cinfo->max_h_samp_factor * DCTSIZE));150compptr->height_in_blocks = (JDIMENSION)151jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,152(long) (cinfo->max_v_samp_factor * DCTSIZE));153/* Size in samples */154compptr->downsampled_width = (JDIMENSION)155jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,156(long) cinfo->max_h_samp_factor);157compptr->downsampled_height = (JDIMENSION)158jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,159(long) cinfo->max_v_samp_factor);160/* Mark component needed (this flag isn't actually used for compression) */161compptr->component_needed = TRUE;162}163164/* Compute number of fully interleaved MCU rows (number of times that165* main controller will call coefficient controller).166*/167cinfo->total_iMCU_rows = (JDIMENSION)168jdiv_round_up((long) cinfo->_jpeg_height,169(long) (cinfo->max_v_samp_factor*DCTSIZE));170}171172173#ifdef C_MULTISCAN_FILES_SUPPORTED174175LOCAL(void)176validate_script (j_compress_ptr cinfo)177/* Verify that the scan script in cinfo->scan_info[] is valid; also178* determine whether it uses progressive JPEG, and set cinfo->progressive_mode.179*/180{181const jpeg_scan_info *scanptr;182int scanno, ncomps, ci, coefi, thisi;183int Ss, Se, Ah, Al;184boolean component_sent[MAX_COMPONENTS];185#ifdef C_PROGRESSIVE_SUPPORTED186int *last_bitpos_ptr;187int last_bitpos[MAX_COMPONENTS][DCTSIZE2];188/* -1 until that coefficient has been seen; then last Al for it */189#endif190191if (cinfo->num_scans <= 0)192ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);193194/* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;195* for progressive JPEG, no scan can have this.196*/197scanptr = cinfo->scan_info;198if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {199#ifdef C_PROGRESSIVE_SUPPORTED200cinfo->progressive_mode = TRUE;201last_bitpos_ptr = & last_bitpos[0][0];202for (ci = 0; ci < cinfo->num_components; ci++)203for (coefi = 0; coefi < DCTSIZE2; coefi++)204*last_bitpos_ptr++ = -1;205#else206ERREXIT(cinfo, JERR_NOT_COMPILED);207#endif208} else {209cinfo->progressive_mode = FALSE;210for (ci = 0; ci < cinfo->num_components; ci++)211component_sent[ci] = FALSE;212}213214for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {215/* Validate component indexes */216ncomps = scanptr->comps_in_scan;217if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)218ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);219for (ci = 0; ci < ncomps; ci++) {220thisi = scanptr->component_index[ci];221if (thisi < 0 || thisi >= cinfo->num_components)222ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);223/* Components must appear in SOF order within each scan */224if (ci > 0 && thisi <= scanptr->component_index[ci-1])225ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);226}227/* Validate progression parameters */228Ss = scanptr->Ss;229Se = scanptr->Se;230Ah = scanptr->Ah;231Al = scanptr->Al;232if (cinfo->progressive_mode) {233#ifdef C_PROGRESSIVE_SUPPORTED234/* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that235* seems wrong: the upper bound ought to depend on data precision.236* Perhaps they really meant 0..N+1 for N-bit precision.237* Here we allow 0..10 for 8-bit data; Al larger than 10 results in238* out-of-range reconstructed DC values during the first DC scan,239* which might cause problems for some decoders.240*/241#if BITS_IN_JSAMPLE == 8242#define MAX_AH_AL 10243#else244#define MAX_AH_AL 13245#endif246if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||247Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)248ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);249if (Ss == 0) {250if (Se != 0) /* DC and AC together not OK */251ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);252} else {253if (ncomps != 1) /* AC scans must be for only one component */254ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);255}256for (ci = 0; ci < ncomps; ci++) {257last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];258if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */259ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);260for (coefi = Ss; coefi <= Se; coefi++) {261if (last_bitpos_ptr[coefi] < 0) {262/* first scan of this coefficient */263if (Ah != 0)264ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);265} else {266/* not first scan */267if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)268ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);269}270last_bitpos_ptr[coefi] = Al;271}272}273#endif274} else {275/* For sequential JPEG, all progression parameters must be these: */276if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)277ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);278/* Make sure components are not sent twice */279for (ci = 0; ci < ncomps; ci++) {280thisi = scanptr->component_index[ci];281if (component_sent[thisi])282ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);283component_sent[thisi] = TRUE;284}285}286}287288/* Now verify that everything got sent. */289if (cinfo->progressive_mode) {290#ifdef C_PROGRESSIVE_SUPPORTED291/* For progressive mode, we only check that at least some DC data292* got sent for each component; the spec does not require that all bits293* of all coefficients be transmitted. Would it be wiser to enforce294* transmission of all coefficient bits??295*/296for (ci = 0; ci < cinfo->num_components; ci++) {297if (last_bitpos[ci][0] < 0)298ERREXIT(cinfo, JERR_MISSING_DATA);299}300#endif301} else {302for (ci = 0; ci < cinfo->num_components; ci++) {303if (! component_sent[ci])304ERREXIT(cinfo, JERR_MISSING_DATA);305}306}307}308309#endif /* C_MULTISCAN_FILES_SUPPORTED */310311312LOCAL(void)313select_scan_parameters (j_compress_ptr cinfo)314/* Set up the scan parameters for the current scan */315{316int ci;317318#ifdef C_MULTISCAN_FILES_SUPPORTED319if (cinfo->scan_info != NULL) {320/* Prepare for current scan --- the script is already validated */321my_master_ptr master = (my_master_ptr) cinfo->master;322const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;323324cinfo->comps_in_scan = scanptr->comps_in_scan;325for (ci = 0; ci < scanptr->comps_in_scan; ci++) {326cinfo->cur_comp_info[ci] =327&cinfo->comp_info[scanptr->component_index[ci]];328}329cinfo->Ss = scanptr->Ss;330cinfo->Se = scanptr->Se;331cinfo->Ah = scanptr->Ah;332cinfo->Al = scanptr->Al;333}334else335#endif336{337/* Prepare for single sequential-JPEG scan containing all components */338if (cinfo->num_components > MAX_COMPS_IN_SCAN)339ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,340MAX_COMPS_IN_SCAN);341cinfo->comps_in_scan = cinfo->num_components;342for (ci = 0; ci < cinfo->num_components; ci++) {343cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];344}345cinfo->Ss = 0;346cinfo->Se = DCTSIZE2-1;347cinfo->Ah = 0;348cinfo->Al = 0;349}350}351352353LOCAL(void)354per_scan_setup (j_compress_ptr cinfo)355/* Do computations that are needed before processing a JPEG scan */356/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */357{358int ci, mcublks, tmp;359jpeg_component_info *compptr;360361if (cinfo->comps_in_scan == 1) {362363/* Noninterleaved (single-component) scan */364compptr = cinfo->cur_comp_info[0];365366/* Overall image size in MCUs */367cinfo->MCUs_per_row = compptr->width_in_blocks;368cinfo->MCU_rows_in_scan = compptr->height_in_blocks;369370/* For noninterleaved scan, always one block per MCU */371compptr->MCU_width = 1;372compptr->MCU_height = 1;373compptr->MCU_blocks = 1;374compptr->MCU_sample_width = DCTSIZE;375compptr->last_col_width = 1;376/* For noninterleaved scans, it is convenient to define last_row_height377* as the number of block rows present in the last iMCU row.378*/379tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);380if (tmp == 0) tmp = compptr->v_samp_factor;381compptr->last_row_height = tmp;382383/* Prepare array describing MCU composition */384cinfo->blocks_in_MCU = 1;385cinfo->MCU_membership[0] = 0;386387} else {388389/* Interleaved (multi-component) scan */390if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)391ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,392MAX_COMPS_IN_SCAN);393394/* Overall image size in MCUs */395cinfo->MCUs_per_row = (JDIMENSION)396jdiv_round_up((long) cinfo->_jpeg_width,397(long) (cinfo->max_h_samp_factor*DCTSIZE));398cinfo->MCU_rows_in_scan = (JDIMENSION)399jdiv_round_up((long) cinfo->_jpeg_height,400(long) (cinfo->max_v_samp_factor*DCTSIZE));401402cinfo->blocks_in_MCU = 0;403404for (ci = 0; ci < cinfo->comps_in_scan; ci++) {405compptr = cinfo->cur_comp_info[ci];406/* Sampling factors give # of blocks of component in each MCU */407compptr->MCU_width = compptr->h_samp_factor;408compptr->MCU_height = compptr->v_samp_factor;409compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;410compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;411/* Figure number of non-dummy blocks in last MCU column & row */412tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);413if (tmp == 0) tmp = compptr->MCU_width;414compptr->last_col_width = tmp;415tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);416if (tmp == 0) tmp = compptr->MCU_height;417compptr->last_row_height = tmp;418/* Prepare array describing MCU composition */419mcublks = compptr->MCU_blocks;420if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)421ERREXIT(cinfo, JERR_BAD_MCU_SIZE);422while (mcublks-- > 0) {423cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;424}425}426427}428429/* Convert restart specified in rows to actual MCU count. */430/* Note that count must fit in 16 bits, so we provide limiting. */431if (cinfo->restart_in_rows > 0) {432long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;433cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);434}435}436437438/*439* Per-pass setup.440* This is called at the beginning of each pass. We determine which modules441* will be active during this pass and give them appropriate start_pass calls.442* We also set is_last_pass to indicate whether any more passes will be443* required.444*/445446METHODDEF(void)447prepare_for_pass (j_compress_ptr cinfo)448{449my_master_ptr master = (my_master_ptr) cinfo->master;450451switch (master->pass_type) {452case main_pass:453/* Initial pass: will collect input data, and do either Huffman454* optimization or data output for the first scan.455*/456select_scan_parameters(cinfo);457per_scan_setup(cinfo);458if (! cinfo->raw_data_in) {459(*cinfo->cconvert->start_pass) (cinfo);460(*cinfo->downsample->start_pass) (cinfo);461(*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);462}463(*cinfo->fdct->start_pass) (cinfo);464(*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);465(*cinfo->coef->start_pass) (cinfo,466(master->total_passes > 1 ?467JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));468(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);469if (cinfo->optimize_coding) {470/* No immediate data output; postpone writing frame/scan headers */471master->pub.call_pass_startup = FALSE;472} else {473/* Will write frame/scan headers at first jpeg_write_scanlines call */474master->pub.call_pass_startup = TRUE;475}476break;477#ifdef ENTROPY_OPT_SUPPORTED478case huff_opt_pass:479/* Do Huffman optimization for a scan after the first one. */480select_scan_parameters(cinfo);481per_scan_setup(cinfo);482if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {483(*cinfo->entropy->start_pass) (cinfo, TRUE);484(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);485master->pub.call_pass_startup = FALSE;486break;487}488/* Special case: Huffman DC refinement scans need no Huffman table489* and therefore we can skip the optimization pass for them.490*/491master->pass_type = output_pass;492master->pass_number++;493/*FALLTHROUGH*/494#endif495case output_pass:496/* Do a data-output pass. */497/* We need not repeat per-scan setup if prior optimization pass did it. */498if (! cinfo->optimize_coding) {499select_scan_parameters(cinfo);500per_scan_setup(cinfo);501}502(*cinfo->entropy->start_pass) (cinfo, FALSE);503(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);504/* We emit frame/scan headers now */505if (master->scan_number == 0)506(*cinfo->marker->write_frame_header) (cinfo);507(*cinfo->marker->write_scan_header) (cinfo);508master->pub.call_pass_startup = FALSE;509break;510default:511ERREXIT(cinfo, JERR_NOT_COMPILED);512}513514master->pub.is_last_pass = (master->pass_number == master->total_passes-1);515516/* Set up progress monitor's pass info if present */517if (cinfo->progress != NULL) {518cinfo->progress->completed_passes = master->pass_number;519cinfo->progress->total_passes = master->total_passes;520}521}522523524/*525* Special start-of-pass hook.526* This is called by jpeg_write_scanlines if call_pass_startup is TRUE.527* In single-pass processing, we need this hook because we don't want to528* write frame/scan headers during jpeg_start_compress; we want to let the529* application write COM markers etc. between jpeg_start_compress and the530* jpeg_write_scanlines loop.531* In multi-pass processing, this routine is not used.532*/533534METHODDEF(void)535pass_startup (j_compress_ptr cinfo)536{537cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */538539(*cinfo->marker->write_frame_header) (cinfo);540(*cinfo->marker->write_scan_header) (cinfo);541}542543544/*545* Finish up at end of pass.546*/547548METHODDEF(void)549finish_pass_master (j_compress_ptr cinfo)550{551my_master_ptr master = (my_master_ptr) cinfo->master;552553/* The entropy coder always needs an end-of-pass call,554* either to analyze statistics or to flush its output buffer.555*/556(*cinfo->entropy->finish_pass) (cinfo);557558/* Update state for next pass */559switch (master->pass_type) {560case main_pass:561/* next pass is either output of scan 0 (after optimization)562* or output of scan 1 (if no optimization).563*/564master->pass_type = output_pass;565if (! cinfo->optimize_coding)566master->scan_number++;567break;568case huff_opt_pass:569/* next pass is always output of current scan */570master->pass_type = output_pass;571break;572case output_pass:573/* next pass is either optimization or output of next scan */574if (cinfo->optimize_coding)575master->pass_type = huff_opt_pass;576master->scan_number++;577break;578}579580master->pass_number++;581}582583584/*585* Initialize master compression control.586*/587588GLOBAL(void)589jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)590{591my_master_ptr master;592593master = (my_master_ptr)594(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,595sizeof(my_comp_master));596cinfo->master = (struct jpeg_comp_master *) master;597master->pub.prepare_for_pass = prepare_for_pass;598master->pub.pass_startup = pass_startup;599master->pub.finish_pass = finish_pass_master;600master->pub.is_last_pass = FALSE;601602/* Validate parameters, determine derived values */603initial_setup(cinfo, transcode_only);604605if (cinfo->scan_info != NULL) {606#ifdef C_MULTISCAN_FILES_SUPPORTED607validate_script(cinfo);608#else609ERREXIT(cinfo, JERR_NOT_COMPILED);610#endif611} else {612cinfo->progressive_mode = FALSE;613cinfo->num_scans = 1;614}615616if (cinfo->progressive_mode && !cinfo->arith_code) /* TEMPORARY HACK ??? */617cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */618619/* Initialize my private state */620if (transcode_only) {621/* no main pass in transcoding */622if (cinfo->optimize_coding)623master->pass_type = huff_opt_pass;624else625master->pass_type = output_pass;626} else {627/* for normal compression, first pass is always this type: */628master->pass_type = main_pass;629}630master->scan_number = 0;631master->pass_number = 0;632if (cinfo->optimize_coding)633master->total_passes = cinfo->num_scans * 2;634else635master->total_passes = cinfo->num_scans;636637master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";638}639640641